1 // Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <openssl/evp.h>
16 
17 #include <openssl/bn.h>
18 #include <openssl/bytestring.h>
19 #include <openssl/digest.h>
20 #include <openssl/err.h>
21 #include <openssl/mem.h>
22 #include <openssl/rsa.h>
23 
24 #include "../fipsmodule/rsa/internal.h"
25 #include "internal.h"
26 
27 
rsa_pub_encode(CBB * out,const EVP_PKEY * key)28 static int rsa_pub_encode(CBB *out, const EVP_PKEY *key) {
29   // See RFC 3279, section 2.3.1.
30   const RSA *rsa = reinterpret_cast<const RSA *>(key->pkey);
31   CBB spki, algorithm, null, key_bitstring;
32   if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) ||
33       !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) ||
34       !CBB_add_asn1_element(&algorithm, CBS_ASN1_OBJECT, rsa_asn1_meth.oid,
35                             rsa_asn1_meth.oid_len) ||
36       !CBB_add_asn1(&algorithm, &null, CBS_ASN1_NULL) ||
37       !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) ||
38       !CBB_add_u8(&key_bitstring, 0 /* padding */) ||
39       !RSA_marshal_public_key(&key_bitstring, rsa) ||  //
40       !CBB_flush(out)) {
41     OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
42     return 0;
43   }
44 
45   return 1;
46 }
47 
rsa_pub_decode(EVP_PKEY * out,CBS * params,CBS * key)48 static int rsa_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
49   // See RFC 3279, section 2.3.1.
50 
51   // The parameters must be NULL.
52   CBS null;
53   if (!CBS_get_asn1(params, &null, CBS_ASN1_NULL) || CBS_len(&null) != 0 ||
54       CBS_len(params) != 0) {
55     OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
56     return 0;
57   }
58 
59   RSA *rsa = RSA_parse_public_key(key);
60   if (rsa == NULL || CBS_len(key) != 0) {
61     OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
62     RSA_free(rsa);
63     return 0;
64   }
65 
66   EVP_PKEY_assign_RSA(out, rsa);
67   return 1;
68 }
69 
rsa_pub_cmp(const EVP_PKEY * a,const EVP_PKEY * b)70 static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
71   const RSA *a_rsa = reinterpret_cast<const RSA *>(a->pkey);
72   const RSA *b_rsa = reinterpret_cast<const RSA *>(b->pkey);
73   return BN_cmp(RSA_get0_n(b_rsa), RSA_get0_n(a_rsa)) == 0 &&
74          BN_cmp(RSA_get0_e(b_rsa), RSA_get0_e(a_rsa)) == 0;
75 }
76 
rsa_priv_encode(CBB * out,const EVP_PKEY * key)77 static int rsa_priv_encode(CBB *out, const EVP_PKEY *key) {
78   const RSA *rsa = reinterpret_cast<const RSA *>(key->pkey);
79   CBB pkcs8, algorithm, null, private_key;
80   if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) ||
81       !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) ||
82       !CBB_add_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) ||
83       !CBB_add_asn1_element(&algorithm, CBS_ASN1_OBJECT, rsa_asn1_meth.oid,
84                             rsa_asn1_meth.oid_len) ||
85       !CBB_add_asn1(&algorithm, &null, CBS_ASN1_NULL) ||
86       !CBB_add_asn1(&pkcs8, &private_key, CBS_ASN1_OCTETSTRING) ||
87       !RSA_marshal_private_key(&private_key, rsa) ||  //
88       !CBB_flush(out)) {
89     OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
90     return 0;
91   }
92 
93   return 1;
94 }
95 
rsa_priv_decode(EVP_PKEY * out,CBS * params,CBS * key)96 static int rsa_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
97   // Per RFC 3447, A.1, the parameters have type NULL.
98   CBS null;
99   if (!CBS_get_asn1(params, &null, CBS_ASN1_NULL) || CBS_len(&null) != 0 ||
100       CBS_len(params) != 0) {
101     OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
102     return 0;
103   }
104 
105   RSA *rsa = RSA_parse_private_key(key);
106   if (rsa == NULL || CBS_len(key) != 0) {
107     OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
108     RSA_free(rsa);
109     return 0;
110   }
111 
112   EVP_PKEY_assign_RSA(out, rsa);
113   return 1;
114 }
115 
rsa_opaque(const EVP_PKEY * pkey)116 static int rsa_opaque(const EVP_PKEY *pkey) {
117   const RSA *rsa = reinterpret_cast<const RSA *>(pkey->pkey);
118   return RSA_is_opaque(rsa);
119 }
120 
int_rsa_size(const EVP_PKEY * pkey)121 static int int_rsa_size(const EVP_PKEY *pkey) {
122   const RSA *rsa = reinterpret_cast<const RSA *>(pkey->pkey);
123   return RSA_size(rsa);
124 }
125 
rsa_bits(const EVP_PKEY * pkey)126 static int rsa_bits(const EVP_PKEY *pkey) {
127   const RSA *rsa = reinterpret_cast<const RSA *>(pkey->pkey);
128   return RSA_bits(rsa);
129 }
130 
int_rsa_free(EVP_PKEY * pkey)131 static void int_rsa_free(EVP_PKEY *pkey) {
132   RSA_free(reinterpret_cast<RSA *>(pkey->pkey));
133   pkey->pkey = NULL;
134 }
135 
136 const EVP_PKEY_ASN1_METHOD rsa_asn1_meth = {
137     EVP_PKEY_RSA,
138     // 1.2.840.113549.1.1.1
139     {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01},
140     9,
141 
142     &rsa_pkey_meth,
143 
144     rsa_pub_decode,
145     rsa_pub_encode,
146     rsa_pub_cmp,
147 
148     rsa_priv_decode,
149     rsa_priv_encode,
150 
151     /*set_priv_raw=*/NULL,
152     /*set_pub_raw=*/NULL,
153     /*get_priv_raw=*/NULL,
154     /*get_pub_raw=*/NULL,
155     /*set1_tls_encodedpoint=*/NULL,
156     /*get1_tls_encodedpoint=*/NULL,
157 
158     rsa_opaque,
159 
160     int_rsa_size,
161     rsa_bits,
162 
163     0,
164     0,
165     0,
166 
167     int_rsa_free,
168 };
169 
EVP_PKEY_set1_RSA(EVP_PKEY * pkey,RSA * key)170 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) {
171   if (EVP_PKEY_assign_RSA(pkey, key)) {
172     RSA_up_ref(key);
173     return 1;
174   }
175   return 0;
176 }
177 
EVP_PKEY_assign_RSA(EVP_PKEY * pkey,RSA * key)178 int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key) {
179   evp_pkey_set_method(pkey, &rsa_asn1_meth);
180   pkey->pkey = key;
181   return key != NULL;
182 }
183 
EVP_PKEY_get0_RSA(const EVP_PKEY * pkey)184 RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey) {
185   if (EVP_PKEY_id(pkey) != EVP_PKEY_RSA) {
186     OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_RSA_KEY);
187     return NULL;
188   }
189   return reinterpret_cast<RSA *>(pkey->pkey);
190 }
191 
EVP_PKEY_get1_RSA(const EVP_PKEY * pkey)192 RSA *EVP_PKEY_get1_RSA(const EVP_PKEY *pkey) {
193   RSA *rsa = EVP_PKEY_get0_RSA(pkey);
194   if (rsa != NULL) {
195     RSA_up_ref(rsa);
196   }
197   return rsa;
198 }
199