xref: /aosp_15_r20/external/cronet/third_party/boringssl/src/crypto/rsa_extra/rsa_asn1.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 /* Written by Dr Stephen N Henson ([email protected]) for the OpenSSL
2  * project 2000.
3  */
4 /* ====================================================================
5  * Copyright (c) 2000-2005 The OpenSSL Project.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. All advertising materials mentioning features or use of this
20  *    software must display the following acknowledgment:
21  *    "This product includes software developed by the OpenSSL Project
22  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
23  *
24  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25  *    endorse or promote products derived from this software without
26  *    prior written permission. For written permission, please contact
27  *    [email protected].
28  *
29  * 5. Products derived from this software may not be called "OpenSSL"
30  *    nor may "OpenSSL" appear in their names without prior written
31  *    permission of the OpenSSL Project.
32  *
33  * 6. Redistributions of any form whatsoever must retain the following
34  *    acknowledgment:
35  *    "This product includes software developed by the OpenSSL Project
36  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49  * OF THE POSSIBILITY OF SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This product includes cryptographic software written by Eric Young
53  * ([email protected]).  This product includes software written by Tim
54  * Hudson ([email protected]). */
55 
56 #include <openssl/rsa.h>
57 
58 #include <assert.h>
59 #include <limits.h>
60 #include <string.h>
61 
62 #include <openssl/bn.h>
63 #include <openssl/bytestring.h>
64 #include <openssl/err.h>
65 #include <openssl/mem.h>
66 
67 #include "../fipsmodule/rsa/internal.h"
68 #include "../bytestring/internal.h"
69 #include "../internal.h"
70 
71 
parse_integer(CBS * cbs,BIGNUM ** out)72 static int parse_integer(CBS *cbs, BIGNUM **out) {
73   assert(*out == NULL);
74   *out = BN_new();
75   if (*out == NULL) {
76     return 0;
77   }
78   return BN_parse_asn1_unsigned(cbs, *out);
79 }
80 
marshal_integer(CBB * cbb,BIGNUM * bn)81 static int marshal_integer(CBB *cbb, BIGNUM *bn) {
82   if (bn == NULL) {
83     // An RSA object may be missing some components.
84     OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
85     return 0;
86   }
87   return BN_marshal_asn1(cbb, bn);
88 }
89 
RSA_parse_public_key(CBS * cbs)90 RSA *RSA_parse_public_key(CBS *cbs) {
91   RSA *ret = RSA_new();
92   if (ret == NULL) {
93     return NULL;
94   }
95   CBS child;
96   if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
97       !parse_integer(&child, &ret->n) ||
98       !parse_integer(&child, &ret->e) ||
99       CBS_len(&child) != 0) {
100     OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
101     RSA_free(ret);
102     return NULL;
103   }
104 
105   if (!RSA_check_key(ret)) {
106     OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS);
107     RSA_free(ret);
108     return NULL;
109   }
110 
111   return ret;
112 }
113 
RSA_public_key_from_bytes(const uint8_t * in,size_t in_len)114 RSA *RSA_public_key_from_bytes(const uint8_t *in, size_t in_len) {
115   CBS cbs;
116   CBS_init(&cbs, in, in_len);
117   RSA *ret = RSA_parse_public_key(&cbs);
118   if (ret == NULL || CBS_len(&cbs) != 0) {
119     OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
120     RSA_free(ret);
121     return NULL;
122   }
123   return ret;
124 }
125 
RSA_marshal_public_key(CBB * cbb,const RSA * rsa)126 int RSA_marshal_public_key(CBB *cbb, const RSA *rsa) {
127   CBB child;
128   if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
129       !marshal_integer(&child, rsa->n) ||
130       !marshal_integer(&child, rsa->e) ||
131       !CBB_flush(cbb)) {
132     OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
133     return 0;
134   }
135   return 1;
136 }
137 
RSA_public_key_to_bytes(uint8_t ** out_bytes,size_t * out_len,const RSA * rsa)138 int RSA_public_key_to_bytes(uint8_t **out_bytes, size_t *out_len,
139                             const RSA *rsa) {
140   CBB cbb;
141   CBB_zero(&cbb);
142   if (!CBB_init(&cbb, 0) ||
143       !RSA_marshal_public_key(&cbb, rsa) ||
144       !CBB_finish(&cbb, out_bytes, out_len)) {
145     OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
146     CBB_cleanup(&cbb);
147     return 0;
148   }
149   return 1;
150 }
151 
152 // kVersionTwoPrime is the value of the version field for a two-prime
153 // RSAPrivateKey structure (RFC 3447).
154 static const uint64_t kVersionTwoPrime = 0;
155 
RSA_parse_private_key(CBS * cbs)156 RSA *RSA_parse_private_key(CBS *cbs) {
157   RSA *ret = RSA_new();
158   if (ret == NULL) {
159     return NULL;
160   }
161 
162   CBS child;
163   uint64_t version;
164   if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
165       !CBS_get_asn1_uint64(&child, &version)) {
166     OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
167     goto err;
168   }
169 
170   if (version != kVersionTwoPrime) {
171     OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_VERSION);
172     goto err;
173   }
174 
175   if (!parse_integer(&child, &ret->n) ||
176       !parse_integer(&child, &ret->e) ||
177       !parse_integer(&child, &ret->d) ||
178       !parse_integer(&child, &ret->p) ||
179       !parse_integer(&child, &ret->q) ||
180       !parse_integer(&child, &ret->dmp1) ||
181       !parse_integer(&child, &ret->dmq1) ||
182       !parse_integer(&child, &ret->iqmp)) {
183     goto err;
184   }
185 
186   if (CBS_len(&child) != 0) {
187     OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
188     goto err;
189   }
190 
191   if (!RSA_check_key(ret)) {
192     OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS);
193     goto err;
194   }
195 
196   return ret;
197 
198 err:
199   RSA_free(ret);
200   return NULL;
201 }
202 
RSA_private_key_from_bytes(const uint8_t * in,size_t in_len)203 RSA *RSA_private_key_from_bytes(const uint8_t *in, size_t in_len) {
204   CBS cbs;
205   CBS_init(&cbs, in, in_len);
206   RSA *ret = RSA_parse_private_key(&cbs);
207   if (ret == NULL || CBS_len(&cbs) != 0) {
208     OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
209     RSA_free(ret);
210     return NULL;
211   }
212   return ret;
213 }
214 
RSA_marshal_private_key(CBB * cbb,const RSA * rsa)215 int RSA_marshal_private_key(CBB *cbb, const RSA *rsa) {
216   CBB child;
217   if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
218       !CBB_add_asn1_uint64(&child, kVersionTwoPrime) ||
219       !marshal_integer(&child, rsa->n) ||
220       !marshal_integer(&child, rsa->e) ||
221       !marshal_integer(&child, rsa->d) ||
222       !marshal_integer(&child, rsa->p) ||
223       !marshal_integer(&child, rsa->q) ||
224       !marshal_integer(&child, rsa->dmp1) ||
225       !marshal_integer(&child, rsa->dmq1) ||
226       !marshal_integer(&child, rsa->iqmp) ||
227       !CBB_flush(cbb)) {
228     OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
229     return 0;
230   }
231   return 1;
232 }
233 
RSA_private_key_to_bytes(uint8_t ** out_bytes,size_t * out_len,const RSA * rsa)234 int RSA_private_key_to_bytes(uint8_t **out_bytes, size_t *out_len,
235                              const RSA *rsa) {
236   CBB cbb;
237   CBB_zero(&cbb);
238   if (!CBB_init(&cbb, 0) ||
239       !RSA_marshal_private_key(&cbb, rsa) ||
240       !CBB_finish(&cbb, out_bytes, out_len)) {
241     OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
242     CBB_cleanup(&cbb);
243     return 0;
244   }
245   return 1;
246 }
247 
d2i_RSAPublicKey(RSA ** out,const uint8_t ** inp,long len)248 RSA *d2i_RSAPublicKey(RSA **out, const uint8_t **inp, long len) {
249   if (len < 0) {
250     return NULL;
251   }
252   CBS cbs;
253   CBS_init(&cbs, *inp, (size_t)len);
254   RSA *ret = RSA_parse_public_key(&cbs);
255   if (ret == NULL) {
256     return NULL;
257   }
258   if (out != NULL) {
259     RSA_free(*out);
260     *out = ret;
261   }
262   *inp = CBS_data(&cbs);
263   return ret;
264 }
265 
i2d_RSAPublicKey(const RSA * in,uint8_t ** outp)266 int i2d_RSAPublicKey(const RSA *in, uint8_t **outp) {
267   CBB cbb;
268   if (!CBB_init(&cbb, 0) ||
269       !RSA_marshal_public_key(&cbb, in)) {
270     CBB_cleanup(&cbb);
271     return -1;
272   }
273   return CBB_finish_i2d(&cbb, outp);
274 }
275 
d2i_RSAPrivateKey(RSA ** out,const uint8_t ** inp,long len)276 RSA *d2i_RSAPrivateKey(RSA **out, const uint8_t **inp, long len) {
277   if (len < 0) {
278     return NULL;
279   }
280   CBS cbs;
281   CBS_init(&cbs, *inp, (size_t)len);
282   RSA *ret = RSA_parse_private_key(&cbs);
283   if (ret == NULL) {
284     return NULL;
285   }
286   if (out != NULL) {
287     RSA_free(*out);
288     *out = ret;
289   }
290   *inp = CBS_data(&cbs);
291   return ret;
292 }
293 
i2d_RSAPrivateKey(const RSA * in,uint8_t ** outp)294 int i2d_RSAPrivateKey(const RSA *in, uint8_t **outp) {
295   CBB cbb;
296   if (!CBB_init(&cbb, 0) ||
297       !RSA_marshal_private_key(&cbb, in)) {
298     CBB_cleanup(&cbb);
299     return -1;
300   }
301   return CBB_finish_i2d(&cbb, outp);
302 }
303 
RSAPublicKey_dup(const RSA * rsa)304 RSA *RSAPublicKey_dup(const RSA *rsa) {
305   uint8_t *der;
306   size_t der_len;
307   if (!RSA_public_key_to_bytes(&der, &der_len, rsa)) {
308     return NULL;
309   }
310   RSA *ret = RSA_public_key_from_bytes(der, der_len);
311   OPENSSL_free(der);
312   return ret;
313 }
314 
RSAPrivateKey_dup(const RSA * rsa)315 RSA *RSAPrivateKey_dup(const RSA *rsa) {
316   uint8_t *der;
317   size_t der_len;
318   if (!RSA_private_key_to_bytes(&der, &der_len, rsa)) {
319     return NULL;
320   }
321   RSA *ret = RSA_private_key_from_bytes(der, der_len);
322   OPENSSL_free(der);
323   return ret;
324 }
325