blob: 785044e4be8e01c97a0a6b6d61698912d0680270 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* Written by Dr Stephen N Henson (steve@openssl.org) 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 * licensing@OpenSSL.org.
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 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com). */
55
56#include <openssl/rsa.h>
57
Kenny Rootb8494592015-09-25 02:29:14 +000058#include <assert.h>
59#include <limits.h>
60#include <string.h>
61
Kenny Rootb8494592015-09-25 02:29:14 +000062#include <openssl/bn.h>
63#include <openssl/bytestring.h>
64#include <openssl/err.h>
65#include <openssl/mem.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080066
Robert Sloan8ff03552017-06-14 12:40:58 -070067#include "../fipsmodule/rsa/internal.h"
David Benjamin4969cc92016-04-22 15:02:23 -040068#include "../bytestring/internal.h"
Robert Sloan69939df2017-01-09 10:53:07 -080069#include "../internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -080070
71
Kenny Rootb8494592015-09-25 02:29:14 +000072static int parse_integer_buggy(CBS *cbs, BIGNUM **out, int buggy) {
73 assert(*out == NULL);
74 *out = BN_new();
75 if (*out == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -080076 return 0;
Kenny Rootb8494592015-09-25 02:29:14 +000077 }
78 if (buggy) {
David Benjamin4969cc92016-04-22 15:02:23 -040079 return BN_parse_asn1_unsigned_buggy(cbs, *out);
Kenny Rootb8494592015-09-25 02:29:14 +000080 }
David Benjamin4969cc92016-04-22 15:02:23 -040081 return BN_parse_asn1_unsigned(cbs, *out);
Kenny Rootb8494592015-09-25 02:29:14 +000082}
83
84static int parse_integer(CBS *cbs, BIGNUM **out) {
85 return parse_integer_buggy(cbs, out, 0 /* not buggy */);
86}
87
88static int marshal_integer(CBB *cbb, BIGNUM *bn) {
89 if (bn == NULL) {
90 /* An RSA object may be missing some components. */
91 OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
92 return 0;
93 }
David Benjamin4969cc92016-04-22 15:02:23 -040094 return BN_marshal_asn1(cbb, bn);
Kenny Rootb8494592015-09-25 02:29:14 +000095}
96
97static RSA *parse_public_key(CBS *cbs, int buggy) {
98 RSA *ret = RSA_new();
99 if (ret == NULL) {
100 return NULL;
101 }
102 CBS child;
103 if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
104 !parse_integer_buggy(&child, &ret->n, buggy) ||
105 !parse_integer(&child, &ret->e) ||
106 CBS_len(&child) != 0) {
107 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
108 RSA_free(ret);
109 return NULL;
110 }
Adam Langley4139edb2016-01-13 15:00:54 -0800111
112 if (!BN_is_odd(ret->e) ||
113 BN_num_bits(ret->e) < 2) {
114 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS);
115 RSA_free(ret);
116 return NULL;
117 }
118
Kenny Rootb8494592015-09-25 02:29:14 +0000119 return ret;
120}
121
122RSA *RSA_parse_public_key(CBS *cbs) {
123 return parse_public_key(cbs, 0 /* not buggy */);
124}
125
126RSA *RSA_parse_public_key_buggy(CBS *cbs) {
127 /* Estonian IDs issued between September 2014 to September 2015 are
128 * broken. See https://crbug.com/532048 and https://crbug.com/534766.
129 *
130 * TODO(davidben): Remove this code and callers in March 2016. */
131 return parse_public_key(cbs, 1 /* buggy */);
132}
133
134RSA *RSA_public_key_from_bytes(const uint8_t *in, size_t in_len) {
135 CBS cbs;
136 CBS_init(&cbs, in, in_len);
137 RSA *ret = RSA_parse_public_key(&cbs);
138 if (ret == NULL || CBS_len(&cbs) != 0) {
139 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
140 RSA_free(ret);
141 return NULL;
142 }
143 return ret;
144}
145
146int RSA_marshal_public_key(CBB *cbb, const RSA *rsa) {
147 CBB child;
148 if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
149 !marshal_integer(&child, rsa->n) ||
150 !marshal_integer(&child, rsa->e) ||
151 !CBB_flush(cbb)) {
152 OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
153 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800154 }
155 return 1;
156}
157
Kenny Rootb8494592015-09-25 02:29:14 +0000158int RSA_public_key_to_bytes(uint8_t **out_bytes, size_t *out_len,
159 const RSA *rsa) {
160 CBB cbb;
161 CBB_zero(&cbb);
162 if (!CBB_init(&cbb, 0) ||
163 !RSA_marshal_public_key(&cbb, rsa) ||
164 !CBB_finish(&cbb, out_bytes, out_len)) {
165 OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
166 CBB_cleanup(&cbb);
167 return 0;
168 }
169 return 1;
170}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800171
Robert Sloan572a4e22017-04-17 10:52:19 -0700172/* kVersionTwoPrime is the value of the version field for a two-prime
173 * RSAPrivateKey structure (RFC 3447). */
Kenny Rootb8494592015-09-25 02:29:14 +0000174static const uint64_t kVersionTwoPrime = 0;
Kenny Rootb8494592015-09-25 02:29:14 +0000175
176RSA *RSA_parse_private_key(CBS *cbs) {
Kenny Rootb8494592015-09-25 02:29:14 +0000177 RSA *ret = RSA_new();
178 if (ret == NULL) {
179 return NULL;
180 }
181
182 CBS child;
183 uint64_t version;
184 if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
Kenny Roote99801b2015-11-06 15:31:15 -0800185 !CBS_get_asn1_uint64(&child, &version)) {
186 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
187 goto err;
188 }
189
Robert Sloan572a4e22017-04-17 10:52:19 -0700190 if (version != kVersionTwoPrime) {
Kenny Roote99801b2015-11-06 15:31:15 -0800191 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_VERSION);
192 goto err;
193 }
194
195 if (!parse_integer(&child, &ret->n) ||
Kenny Rootb8494592015-09-25 02:29:14 +0000196 !parse_integer(&child, &ret->e) ||
197 !parse_integer(&child, &ret->d) ||
198 !parse_integer(&child, &ret->p) ||
199 !parse_integer(&child, &ret->q) ||
200 !parse_integer(&child, &ret->dmp1) ||
201 !parse_integer(&child, &ret->dmq1) ||
202 !parse_integer(&child, &ret->iqmp)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000203 goto err;
204 }
205
Kenny Rootb8494592015-09-25 02:29:14 +0000206 if (CBS_len(&child) != 0) {
207 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
208 goto err;
209 }
210
Robert Sloan69939df2017-01-09 10:53:07 -0800211 if (!RSA_check_key(ret)) {
212 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS);
213 goto err;
214 }
215
Kenny Rootb8494592015-09-25 02:29:14 +0000216 return ret;
217
218err:
Kenny Rootb8494592015-09-25 02:29:14 +0000219 RSA_free(ret);
220 return NULL;
221}
222
223RSA *RSA_private_key_from_bytes(const uint8_t *in, size_t in_len) {
224 CBS cbs;
225 CBS_init(&cbs, in, in_len);
226 RSA *ret = RSA_parse_private_key(&cbs);
227 if (ret == NULL || CBS_len(&cbs) != 0) {
228 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
229 RSA_free(ret);
230 return NULL;
231 }
232 return ret;
233}
234
235int RSA_marshal_private_key(CBB *cbb, const RSA *rsa) {
Kenny Rootb8494592015-09-25 02:29:14 +0000236 CBB child;
237 if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
Robert Sloan572a4e22017-04-17 10:52:19 -0700238 !CBB_add_asn1_uint64(&child, kVersionTwoPrime) ||
Kenny Rootb8494592015-09-25 02:29:14 +0000239 !marshal_integer(&child, rsa->n) ||
240 !marshal_integer(&child, rsa->e) ||
241 !marshal_integer(&child, rsa->d) ||
242 !marshal_integer(&child, rsa->p) ||
243 !marshal_integer(&child, rsa->q) ||
244 !marshal_integer(&child, rsa->dmp1) ||
245 !marshal_integer(&child, rsa->dmq1) ||
Robert Sloan572a4e22017-04-17 10:52:19 -0700246 !marshal_integer(&child, rsa->iqmp) ||
247 !CBB_flush(cbb)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000248 OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
249 return 0;
250 }
251 return 1;
252}
253
254int RSA_private_key_to_bytes(uint8_t **out_bytes, size_t *out_len,
255 const RSA *rsa) {
256 CBB cbb;
257 CBB_zero(&cbb);
258 if (!CBB_init(&cbb, 0) ||
259 !RSA_marshal_private_key(&cbb, rsa) ||
260 !CBB_finish(&cbb, out_bytes, out_len)) {
261 OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
262 CBB_cleanup(&cbb);
263 return 0;
264 }
265 return 1;
266}
267
268RSA *d2i_RSAPublicKey(RSA **out, const uint8_t **inp, long len) {
269 if (len < 0) {
270 return NULL;
271 }
272 CBS cbs;
273 CBS_init(&cbs, *inp, (size_t)len);
274 RSA *ret = RSA_parse_public_key(&cbs);
275 if (ret == NULL) {
276 return NULL;
277 }
278 if (out != NULL) {
279 RSA_free(*out);
280 *out = ret;
281 }
David Benjamin4969cc92016-04-22 15:02:23 -0400282 *inp = CBS_data(&cbs);
Kenny Rootb8494592015-09-25 02:29:14 +0000283 return ret;
284}
285
286int i2d_RSAPublicKey(const RSA *in, uint8_t **outp) {
David Benjamin4969cc92016-04-22 15:02:23 -0400287 CBB cbb;
288 if (!CBB_init(&cbb, 0) ||
289 !RSA_marshal_public_key(&cbb, in)) {
290 CBB_cleanup(&cbb);
Kenny Rootb8494592015-09-25 02:29:14 +0000291 return -1;
292 }
David Benjamin4969cc92016-04-22 15:02:23 -0400293 return CBB_finish_i2d(&cbb, outp);
Kenny Rootb8494592015-09-25 02:29:14 +0000294}
295
296RSA *d2i_RSAPrivateKey(RSA **out, const uint8_t **inp, long len) {
297 if (len < 0) {
298 return NULL;
299 }
300 CBS cbs;
301 CBS_init(&cbs, *inp, (size_t)len);
302 RSA *ret = RSA_parse_private_key(&cbs);
303 if (ret == NULL) {
304 return NULL;
305 }
306 if (out != NULL) {
307 RSA_free(*out);
308 *out = ret;
309 }
David Benjamin4969cc92016-04-22 15:02:23 -0400310 *inp = CBS_data(&cbs);
Kenny Rootb8494592015-09-25 02:29:14 +0000311 return ret;
312}
313
314int i2d_RSAPrivateKey(const RSA *in, uint8_t **outp) {
David Benjamin4969cc92016-04-22 15:02:23 -0400315 CBB cbb;
316 if (!CBB_init(&cbb, 0) ||
317 !RSA_marshal_private_key(&cbb, in)) {
318 CBB_cleanup(&cbb);
Kenny Rootb8494592015-09-25 02:29:14 +0000319 return -1;
320 }
David Benjamin4969cc92016-04-22 15:02:23 -0400321 return CBB_finish_i2d(&cbb, outp);
Kenny Rootb8494592015-09-25 02:29:14 +0000322}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800323
Adam Langleyd9e397b2015-01-22 14:27:53 -0800324RSA *RSAPublicKey_dup(const RSA *rsa) {
Kenny Rootb8494592015-09-25 02:29:14 +0000325 uint8_t *der;
326 size_t der_len;
327 if (!RSA_public_key_to_bytes(&der, &der_len, rsa)) {
328 return NULL;
329 }
330 RSA *ret = RSA_public_key_from_bytes(der, der_len);
331 OPENSSL_free(der);
332 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800333}
334
335RSA *RSAPrivateKey_dup(const RSA *rsa) {
Kenny Rootb8494592015-09-25 02:29:14 +0000336 uint8_t *der;
337 size_t der_len;
338 if (!RSA_private_key_to_bytes(&der, &der_len, rsa)) {
339 return NULL;
340 }
341 RSA *ret = RSA_private_key_from_bytes(der, der_len);
342 OPENSSL_free(der);
343 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800344}