/src/boringssl/crypto/rsa_extra/rsa_asn1.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* 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 | | |
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 | | |
72 | 0 | static int parse_integer(CBS *cbs, BIGNUM **out) { |
73 | 0 | assert(*out == NULL); |
74 | 0 | *out = BN_new(); |
75 | 0 | if (*out == NULL) { |
76 | 0 | return 0; |
77 | 0 | } |
78 | 0 | return BN_parse_asn1_unsigned(cbs, *out); |
79 | 0 | } |
80 | | |
81 | 0 | static int marshal_integer(CBB *cbb, BIGNUM *bn) { |
82 | 0 | if (bn == NULL) { |
83 | | // An RSA object may be missing some components. |
84 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING); |
85 | 0 | return 0; |
86 | 0 | } |
87 | 0 | return BN_marshal_asn1(cbb, bn); |
88 | 0 | } |
89 | | |
90 | 0 | RSA *RSA_parse_public_key(CBS *cbs) { |
91 | 0 | RSA *ret = RSA_new(); |
92 | 0 | if (ret == NULL) { |
93 | 0 | return NULL; |
94 | 0 | } |
95 | 0 | CBS child; |
96 | 0 | if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) || |
97 | 0 | !parse_integer(&child, &ret->n) || |
98 | 0 | !parse_integer(&child, &ret->e) || |
99 | 0 | CBS_len(&child) != 0) { |
100 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING); |
101 | 0 | RSA_free(ret); |
102 | 0 | return NULL; |
103 | 0 | } |
104 | | |
105 | 0 | if (!RSA_check_key(ret)) { |
106 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS); |
107 | 0 | RSA_free(ret); |
108 | 0 | return NULL; |
109 | 0 | } |
110 | | |
111 | 0 | return ret; |
112 | 0 | } |
113 | | |
114 | 0 | RSA *RSA_public_key_from_bytes(const uint8_t *in, size_t in_len) { |
115 | 0 | CBS cbs; |
116 | 0 | CBS_init(&cbs, in, in_len); |
117 | 0 | RSA *ret = RSA_parse_public_key(&cbs); |
118 | 0 | if (ret == NULL || CBS_len(&cbs) != 0) { |
119 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING); |
120 | 0 | RSA_free(ret); |
121 | 0 | return NULL; |
122 | 0 | } |
123 | 0 | return ret; |
124 | 0 | } |
125 | | |
126 | 0 | int RSA_marshal_public_key(CBB *cbb, const RSA *rsa) { |
127 | 0 | CBB child; |
128 | 0 | if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) || |
129 | 0 | !marshal_integer(&child, rsa->n) || |
130 | 0 | !marshal_integer(&child, rsa->e) || |
131 | 0 | !CBB_flush(cbb)) { |
132 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR); |
133 | 0 | return 0; |
134 | 0 | } |
135 | 0 | return 1; |
136 | 0 | } |
137 | | |
138 | | int RSA_public_key_to_bytes(uint8_t **out_bytes, size_t *out_len, |
139 | 0 | const RSA *rsa) { |
140 | 0 | CBB cbb; |
141 | 0 | CBB_zero(&cbb); |
142 | 0 | if (!CBB_init(&cbb, 0) || |
143 | 0 | !RSA_marshal_public_key(&cbb, rsa) || |
144 | 0 | !CBB_finish(&cbb, out_bytes, out_len)) { |
145 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR); |
146 | 0 | CBB_cleanup(&cbb); |
147 | 0 | return 0; |
148 | 0 | } |
149 | 0 | return 1; |
150 | 0 | } |
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 | | |
156 | 0 | RSA *RSA_parse_private_key(CBS *cbs) { |
157 | 0 | RSA *ret = RSA_new(); |
158 | 0 | if (ret == NULL) { |
159 | 0 | return NULL; |
160 | 0 | } |
161 | | |
162 | 0 | CBS child; |
163 | 0 | uint64_t version; |
164 | 0 | if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) || |
165 | 0 | !CBS_get_asn1_uint64(&child, &version)) { |
166 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING); |
167 | 0 | goto err; |
168 | 0 | } |
169 | | |
170 | 0 | if (version != kVersionTwoPrime) { |
171 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_VERSION); |
172 | 0 | goto err; |
173 | 0 | } |
174 | | |
175 | 0 | if (!parse_integer(&child, &ret->n) || |
176 | 0 | !parse_integer(&child, &ret->e) || |
177 | 0 | !parse_integer(&child, &ret->d) || |
178 | 0 | !parse_integer(&child, &ret->p) || |
179 | 0 | !parse_integer(&child, &ret->q) || |
180 | 0 | !parse_integer(&child, &ret->dmp1) || |
181 | 0 | !parse_integer(&child, &ret->dmq1) || |
182 | 0 | !parse_integer(&child, &ret->iqmp)) { |
183 | 0 | goto err; |
184 | 0 | } |
185 | | |
186 | 0 | if (CBS_len(&child) != 0) { |
187 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING); |
188 | 0 | goto err; |
189 | 0 | } |
190 | | |
191 | 0 | if (!RSA_check_key(ret)) { |
192 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS); |
193 | 0 | goto err; |
194 | 0 | } |
195 | | |
196 | 0 | return ret; |
197 | | |
198 | 0 | err: |
199 | 0 | RSA_free(ret); |
200 | 0 | return NULL; |
201 | 0 | } |
202 | | |
203 | 0 | RSA *RSA_private_key_from_bytes(const uint8_t *in, size_t in_len) { |
204 | 0 | CBS cbs; |
205 | 0 | CBS_init(&cbs, in, in_len); |
206 | 0 | RSA *ret = RSA_parse_private_key(&cbs); |
207 | 0 | if (ret == NULL || CBS_len(&cbs) != 0) { |
208 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING); |
209 | 0 | RSA_free(ret); |
210 | 0 | return NULL; |
211 | 0 | } |
212 | 0 | return ret; |
213 | 0 | } |
214 | | |
215 | 0 | int RSA_marshal_private_key(CBB *cbb, const RSA *rsa) { |
216 | 0 | CBB child; |
217 | 0 | if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) || |
218 | 0 | !CBB_add_asn1_uint64(&child, kVersionTwoPrime) || |
219 | 0 | !marshal_integer(&child, rsa->n) || |
220 | 0 | !marshal_integer(&child, rsa->e) || |
221 | 0 | !marshal_integer(&child, rsa->d) || |
222 | 0 | !marshal_integer(&child, rsa->p) || |
223 | 0 | !marshal_integer(&child, rsa->q) || |
224 | 0 | !marshal_integer(&child, rsa->dmp1) || |
225 | 0 | !marshal_integer(&child, rsa->dmq1) || |
226 | 0 | !marshal_integer(&child, rsa->iqmp) || |
227 | 0 | !CBB_flush(cbb)) { |
228 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR); |
229 | 0 | return 0; |
230 | 0 | } |
231 | 0 | return 1; |
232 | 0 | } |
233 | | |
234 | | int RSA_private_key_to_bytes(uint8_t **out_bytes, size_t *out_len, |
235 | 0 | const RSA *rsa) { |
236 | 0 | CBB cbb; |
237 | 0 | CBB_zero(&cbb); |
238 | 0 | if (!CBB_init(&cbb, 0) || |
239 | 0 | !RSA_marshal_private_key(&cbb, rsa) || |
240 | 0 | !CBB_finish(&cbb, out_bytes, out_len)) { |
241 | 0 | OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR); |
242 | 0 | CBB_cleanup(&cbb); |
243 | 0 | return 0; |
244 | 0 | } |
245 | 0 | return 1; |
246 | 0 | } |
247 | | |
248 | 0 | RSA *d2i_RSAPublicKey(RSA **out, const uint8_t **inp, long len) { |
249 | 0 | if (len < 0) { |
250 | 0 | return NULL; |
251 | 0 | } |
252 | 0 | CBS cbs; |
253 | 0 | CBS_init(&cbs, *inp, (size_t)len); |
254 | 0 | RSA *ret = RSA_parse_public_key(&cbs); |
255 | 0 | if (ret == NULL) { |
256 | 0 | return NULL; |
257 | 0 | } |
258 | 0 | if (out != NULL) { |
259 | 0 | RSA_free(*out); |
260 | 0 | *out = ret; |
261 | 0 | } |
262 | 0 | *inp = CBS_data(&cbs); |
263 | 0 | return ret; |
264 | 0 | } |
265 | | |
266 | 0 | int i2d_RSAPublicKey(const RSA *in, uint8_t **outp) { |
267 | 0 | CBB cbb; |
268 | 0 | if (!CBB_init(&cbb, 0) || |
269 | 0 | !RSA_marshal_public_key(&cbb, in)) { |
270 | 0 | CBB_cleanup(&cbb); |
271 | 0 | return -1; |
272 | 0 | } |
273 | 0 | return CBB_finish_i2d(&cbb, outp); |
274 | 0 | } |
275 | | |
276 | 0 | RSA *d2i_RSAPrivateKey(RSA **out, const uint8_t **inp, long len) { |
277 | 0 | if (len < 0) { |
278 | 0 | return NULL; |
279 | 0 | } |
280 | 0 | CBS cbs; |
281 | 0 | CBS_init(&cbs, *inp, (size_t)len); |
282 | 0 | RSA *ret = RSA_parse_private_key(&cbs); |
283 | 0 | if (ret == NULL) { |
284 | 0 | return NULL; |
285 | 0 | } |
286 | 0 | if (out != NULL) { |
287 | 0 | RSA_free(*out); |
288 | 0 | *out = ret; |
289 | 0 | } |
290 | 0 | *inp = CBS_data(&cbs); |
291 | 0 | return ret; |
292 | 0 | } |
293 | | |
294 | 0 | int i2d_RSAPrivateKey(const RSA *in, uint8_t **outp) { |
295 | 0 | CBB cbb; |
296 | 0 | if (!CBB_init(&cbb, 0) || |
297 | 0 | !RSA_marshal_private_key(&cbb, in)) { |
298 | 0 | CBB_cleanup(&cbb); |
299 | 0 | return -1; |
300 | 0 | } |
301 | 0 | return CBB_finish_i2d(&cbb, outp); |
302 | 0 | } |
303 | | |
304 | 0 | RSA *RSAPublicKey_dup(const RSA *rsa) { |
305 | 0 | uint8_t *der; |
306 | 0 | size_t der_len; |
307 | 0 | if (!RSA_public_key_to_bytes(&der, &der_len, rsa)) { |
308 | 0 | return NULL; |
309 | 0 | } |
310 | 0 | RSA *ret = RSA_public_key_from_bytes(der, der_len); |
311 | 0 | OPENSSL_free(der); |
312 | 0 | return ret; |
313 | 0 | } |
314 | | |
315 | 0 | RSA *RSAPrivateKey_dup(const RSA *rsa) { |
316 | 0 | uint8_t *der; |
317 | 0 | size_t der_len; |
318 | 0 | if (!RSA_private_key_to_bytes(&der, &der_len, rsa)) { |
319 | 0 | return NULL; |
320 | 0 | } |
321 | 0 | RSA *ret = RSA_private_key_from_bytes(der, der_len); |
322 | 0 | OPENSSL_free(der); |
323 | 0 | return ret; |
324 | 0 | } |