Debian Codegraph @

external/boringssl/crypto/rsa/rsa_asn1.c

raw
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 "internal.h"
68 #include "../bytestring/internal.h"
69
70
71 static int parse_integer_buggy(CBS *cbs, BIGNUM **out, int buggy) {
72 assert(*out == NULL);
73 *out = BN_new();
74 if (*out == NULL) {
75 return 0;
76 }
77 if (buggy) {
78 return BN_parse_asn1_unsigned_buggy(cbs, *out);
79 }
80 return BN_parse_asn1_unsigned(cbs, *out);
81 }
82
83 static int parse_integer(CBS *cbs, BIGNUM **out) {
84 return parse_integer_buggy(cbs, out, 0 /* not buggy */);
85 }
86
87 static int marshal_integer(CBB *cbb, BIGNUM *bn) {
88 if (bn == NULL) {
89 /* An RSA object may be missing some components. */
90 OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
91 return 0;
92 }
93 return BN_marshal_asn1(cbb, bn);
94 }
95
96 static RSA *parse_public_key(CBS *cbs, int buggy) {
97 RSA *ret = RSA_new();
98 if (ret == NULL) {
99 return NULL;
100 }
101 CBS child;
102 if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
103 !parse_integer_buggy(&child, &ret->n, buggy) ||
104 !parse_integer(&child, &ret->e) ||
105 CBS_len(&child) != 0) {
106 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
107 RSA_free(ret);
108 return NULL;
109 }
110
111 if (!BN_is_odd(ret->e) ||
112 BN_num_bits(ret->e) < 2) {
113 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS);
114 RSA_free(ret);
115 return NULL;
116 }
117
118 return ret;
119 }
120
121 RSA *RSA_parse_public_key(CBS *cbs) {
122 return parse_public_key(cbs, 0 /* not buggy */);
123 }
124
125 RSA *RSA_parse_public_key_buggy(CBS *cbs) {
126 /* Estonian IDs issued between September 2014 to September 2015 are
127 * broken. See https://crbug.com/532048 and https://crbug.com/534766.
128 *
129 * TODO(davidben): Remove this code and callers in March 2016. */
130 return parse_public_key(cbs, 1 /* buggy */);
131 }
132
133 RSA *RSA_public_key_from_bytes(const uint8_t *in, size_t in_len) {
134 CBS cbs;
135 CBS_init(&cbs, in, in_len);
136 RSA *ret = RSA_parse_public_key(&cbs);
137 if (ret == NULL || CBS_len(&cbs) != 0) {
138 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
139 RSA_free(ret);
140 return NULL;
141 }
142 return ret;
143 }
144
145 int RSA_marshal_public_key(CBB *cbb, const RSA *rsa) {
146 CBB child;
147 if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
148 !marshal_integer(&child, rsa->n) ||
149 !marshal_integer(&child, rsa->e) ||
150 !CBB_flush(cbb)) {
151 OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
152 return 0;
153 }
154 return 1;
155 }
156
157 int RSA_public_key_to_bytes(uint8_t **out_bytes, size_t *out_len,
158 const RSA *rsa) {
159 CBB cbb;
160 CBB_zero(&cbb);
161 if (!CBB_init(&cbb, 0) ||
162 !RSA_marshal_public_key(&cbb, rsa) ||
163 !CBB_finish(&cbb, out_bytes, out_len)) {
164 OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
165 CBB_cleanup(&cbb);
166 return 0;
167 }
168 return 1;
169 }
170
171 /* kVersionTwoPrime and kVersionMulti are the supported values of the version
172 * field of an RSAPrivateKey structure (RFC 3447). */
173 static const uint64_t kVersionTwoPrime = 0;
174 static const uint64_t kVersionMulti = 1;
175
176 /* rsa_parse_additional_prime parses a DER-encoded OtherPrimeInfo from |cbs| and
177 * advances |cbs|. It returns a newly-allocated |RSA_additional_prime| on
178 * success or NULL on error. The |r| and |mont| fields of the result are set to
179 * NULL. */
180 static RSA_additional_prime *rsa_parse_additional_prime(CBS *cbs) {
181 RSA_additional_prime *ret = OPENSSL_malloc(sizeof(RSA_additional_prime));
182 if (ret == NULL) {
183 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
184 return 0;
185 }
186 memset(ret, 0, sizeof(RSA_additional_prime));
187
188 CBS child;
189 if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
190 !parse_integer(&child, &ret->prime) ||
191 !parse_integer(&child, &ret->exp) ||
192 !parse_integer(&child, &ret->coeff) ||
193 CBS_len(&child) != 0) {
194 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
195 RSA_additional_prime_free(ret);
196 return NULL;
197 }
198
199 return ret;
200 }
201
202 RSA *RSA_parse_private_key(CBS *cbs) {
203 BN_CTX *ctx = NULL;
204 BIGNUM *product_of_primes_so_far = NULL;
205 RSA *ret = RSA_new();
206 if (ret == NULL) {
207 return NULL;
208 }
209
210 CBS child;
211 uint64_t version;
212 if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
213 !CBS_get_asn1_uint64(&child, &version)) {
214 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
215 goto err;
216 }
217
218 if (version != kVersionTwoPrime && version != kVersionMulti) {
219 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_VERSION);
220 goto err;
221 }
222
223 if (!parse_integer(&child, &ret->n) ||
224 !parse_integer(&child, &ret->e) ||
225 !parse_integer(&child, &ret->d) ||
226 !parse_integer(&child, &ret->p) ||
227 !parse_integer(&child, &ret->q) ||
228 !parse_integer(&child, &ret->dmp1) ||
229 !parse_integer(&child, &ret->dmq1) ||
230 !parse_integer(&child, &ret->iqmp)) {
231 goto err;
232 }
233
234 if (version == kVersionMulti) {
235 /* Although otherPrimeInfos is written as OPTIONAL in RFC 3447, it later
236 * says "[otherPrimeInfos] shall be omitted if version is 0 and shall
237 * contain at least one instance of OtherPrimeInfo if version is 1." The
238 * OPTIONAL is just so both versions share a single definition. */
239 CBS other_prime_infos;
240 if (!CBS_get_asn1(&child, &other_prime_infos, CBS_ASN1_SEQUENCE) ||
241 CBS_len(&other_prime_infos) == 0) {
242 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
243 goto err;
244 }
245 ret->additional_primes = sk_RSA_additional_prime_new_null();
246 if (ret->additional_primes == NULL) {
247 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
248 goto err;
249 }
250
251 ctx = BN_CTX_new();
252 product_of_primes_so_far = BN_new();
253 if (ctx == NULL ||
254 product_of_primes_so_far == NULL ||
255 !BN_mul(product_of_primes_so_far, ret->p, ret->q, ctx)) {
256 goto err;
257 }
258
259 while (CBS_len(&other_prime_infos) > 0) {
260 RSA_additional_prime *ap = rsa_parse_additional_prime(&other_prime_infos);
261 if (ap == NULL) {
262 goto err;
263 }
264 if (!sk_RSA_additional_prime_push(ret->additional_primes, ap)) {
265 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
266 RSA_additional_prime_free(ap);
267 goto err;
268 }
269 ap->r = BN_dup(product_of_primes_so_far);
270 if (ap->r == NULL ||
271 !BN_mul(product_of_primes_so_far, product_of_primes_so_far,
272 ap->prime, ctx)) {
273 goto err;
274 }
275 }
276 }
277
278 if (CBS_len(&child) != 0) {
279 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
280 goto err;
281 }
282
283 BN_CTX_free(ctx);
284 BN_free(product_of_primes_so_far);
285 return ret;
286
287 err:
288 BN_CTX_free(ctx);
289 BN_free(product_of_primes_so_far);
290 RSA_free(ret);
291 return NULL;
292 }
293
294 RSA *RSA_private_key_from_bytes(const uint8_t *in, size_t in_len) {
295 CBS cbs;
296 CBS_init(&cbs, in, in_len);
297 RSA *ret = RSA_parse_private_key(&cbs);
298 if (ret == NULL || CBS_len(&cbs) != 0) {
299 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
300 RSA_free(ret);
301 return NULL;
302 }
303 return ret;
304 }
305
306 int RSA_marshal_private_key(CBB *cbb, const RSA *rsa) {
307 const int is_multiprime =
308 sk_RSA_additional_prime_num(rsa->additional_primes) > 0;
309
310 CBB child;
311 if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
312 !CBB_add_asn1_uint64(&child,
313 is_multiprime ? kVersionMulti : kVersionTwoPrime) ||
314 !marshal_integer(&child, rsa->n) ||
315 !marshal_integer(&child, rsa->e) ||
316 !marshal_integer(&child, rsa->d) ||
317 !marshal_integer(&child, rsa->p) ||
318 !marshal_integer(&child, rsa->q) ||
319 !marshal_integer(&child, rsa->dmp1) ||
320 !marshal_integer(&child, rsa->dmq1) ||
321 !marshal_integer(&child, rsa->iqmp)) {
322 OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
323 return 0;
324 }
325
326 if (is_multiprime) {
327 CBB other_prime_infos;
328 if (!CBB_add_asn1(&child, &other_prime_infos, CBS_ASN1_SEQUENCE)) {
329 OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
330 return 0;
331 }
332 size_t i;
333 for (i = 0; i < sk_RSA_additional_prime_num(rsa->additional_primes); i++) {
334 RSA_additional_prime *ap =
335 sk_RSA_additional_prime_value(rsa->additional_primes, i);
336 CBB other_prime_info;
337 if (!CBB_add_asn1(&other_prime_infos, &other_prime_info,
338 CBS_ASN1_SEQUENCE) ||
339 !marshal_integer(&other_prime_info, ap->prime) ||
340 !marshal_integer(&other_prime_info, ap->exp) ||
341 !marshal_integer(&other_prime_info, ap->coeff)) {
342 OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
343 return 0;
344 }
345 }
346 }
347
348 if (!CBB_flush(cbb)) {
349 OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
350 return 0;
351 }
352 return 1;
353 }
354
355 int RSA_private_key_to_bytes(uint8_t **out_bytes, size_t *out_len,
356 const RSA *rsa) {
357 CBB cbb;
358 CBB_zero(&cbb);
359 if (!CBB_init(&cbb, 0) ||
360 !RSA_marshal_private_key(&cbb, rsa) ||
361 !CBB_finish(&cbb, out_bytes, out_len)) {
362 OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
363 CBB_cleanup(&cbb);
364 return 0;
365 }
366 return 1;
367 }
368
369 RSA *d2i_RSAPublicKey(RSA **out, const uint8_t **inp, long len) {
370 if (len < 0) {
371 return NULL;
372 }
373 CBS cbs;
374 CBS_init(&cbs, *inp, (size_t)len);
375 RSA *ret = RSA_parse_public_key(&cbs);
376 if (ret == NULL) {
377 return NULL;
378 }
379 if (out != NULL) {
380 RSA_free(*out);
381 *out = ret;
382 }
383 *inp = CBS_data(&cbs);
384 return ret;
385 }
386
387 int i2d_RSAPublicKey(const RSA *in, uint8_t **outp) {
388 CBB cbb;
389 if (!CBB_init(&cbb, 0) ||
390 !RSA_marshal_public_key(&cbb, in)) {
391 CBB_cleanup(&cbb);
392 return -1;
393 }
394 return CBB_finish_i2d(&cbb, outp);
395 }
396
397 RSA *d2i_RSAPrivateKey(RSA **out, const uint8_t **inp, long len) {
398 if (len < 0) {
399 return NULL;
400 }
401 CBS cbs;
402 CBS_init(&cbs, *inp, (size_t)len);
403 RSA *ret = RSA_parse_private_key(&cbs);
404 if (ret == NULL) {
405 return NULL;
406 }
407 if (out != NULL) {
408 RSA_free(*out);
409 *out = ret;
410 }
411 *inp = CBS_data(&cbs);
412 return ret;
413 }
414
415 int i2d_RSAPrivateKey(const RSA *in, uint8_t **outp) {
416 CBB cbb;
417 if (!CBB_init(&cbb, 0) ||
418 !RSA_marshal_private_key(&cbb, in)) {
419 CBB_cleanup(&cbb);
420 return -1;
421 }
422 return CBB_finish_i2d(&cbb, outp);
423 }
424
425 RSA *RSAPublicKey_dup(const RSA *rsa) {
426 uint8_t *der;
427 size_t der_len;
428 if (!RSA_public_key_to_bytes(&der, &der_len, rsa)) {
429 return NULL;
430 }
431 RSA *ret = RSA_public_key_from_bytes(der, der_len);
432 OPENSSL_free(der);
433 return ret;
434 }
435
436 RSA *RSAPrivateKey_dup(const RSA *rsa) {
437 uint8_t *der;
438 size_t der_len;
439 if (!RSA_private_key_to_bytes(&der, &der_len, rsa)) {
440 return NULL;
441 }
442 RSA *ret = RSA_private_key_from_bytes(der, der_len);
443 OPENSSL_free(der);
444 return ret;
445 }