/src/boringssl/crypto/dsa/dsa_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 | | * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved. |
5 | | * |
6 | | * Redistribution and use in source and binary forms, with or without |
7 | | * modification, are permitted provided that the following conditions |
8 | | * are met: |
9 | | * |
10 | | * 1. Redistributions of source code must retain the above copyright |
11 | | * notice, this list of conditions and the following disclaimer. |
12 | | * |
13 | | * 2. Redistributions in binary form must reproduce the above copyright |
14 | | * notice, this list of conditions and the following disclaimer in |
15 | | * the documentation and/or other materials provided with the |
16 | | * distribution. |
17 | | * |
18 | | * 3. All advertising materials mentioning features or use of this |
19 | | * software must display the following acknowledgment: |
20 | | * "This product includes software developed by the OpenSSL Project |
21 | | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
22 | | * |
23 | | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
24 | | * endorse or promote products derived from this software without |
25 | | * prior written permission. For written permission, please contact |
26 | | * licensing@OpenSSL.org. |
27 | | * |
28 | | * 5. Products derived from this software may not be called "OpenSSL" |
29 | | * nor may "OpenSSL" appear in their names without prior written |
30 | | * permission of the OpenSSL Project. |
31 | | * |
32 | | * 6. Redistributions of any form whatsoever must retain the following |
33 | | * acknowledgment: |
34 | | * "This product includes software developed by the OpenSSL Project |
35 | | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
36 | | * |
37 | | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
38 | | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
39 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
40 | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
41 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
42 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
43 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
44 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
45 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
46 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
47 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
48 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
49 | | * ==================================================================== |
50 | | * |
51 | | * This product includes cryptographic software written by Eric Young |
52 | | * (eay@cryptsoft.com). This product includes software written by Tim |
53 | | * Hudson (tjh@cryptsoft.com). */ |
54 | | |
55 | | #include <openssl/dsa.h> |
56 | | |
57 | | #include <assert.h> |
58 | | |
59 | | #include <openssl/bn.h> |
60 | | #include <openssl/bytestring.h> |
61 | | #include <openssl/err.h> |
62 | | #include <openssl/mem.h> |
63 | | |
64 | | #include "internal.h" |
65 | | #include "../bytestring/internal.h" |
66 | | |
67 | | |
68 | 3.03k | #define OPENSSL_DSA_MAX_MODULUS_BITS 10000 |
69 | | |
70 | | // This function is in dsa_asn1.c rather than dsa.c because it is reachable from |
71 | | // |EVP_PKEY| parsers. This makes it easier for the static linker to drop most |
72 | | // of the DSA implementation. |
73 | 4.55k | int dsa_check_key(const DSA *dsa) { |
74 | 4.55k | if (!dsa->p || !dsa->q || !dsa->g) { |
75 | 0 | OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS); |
76 | 0 | return 0; |
77 | 0 | } |
78 | | |
79 | | // Fully checking for invalid DSA groups is expensive, so security and |
80 | | // correctness of the signature scheme depend on how |dsa| was computed. I.e. |
81 | | // we leave "assurance of domain parameter validity" from FIPS 186-4 to the |
82 | | // caller. However, we check bounds on all values to avoid DoS vectors even |
83 | | // when domain parameters are invalid. In particular, signing will infinite |
84 | | // loop if |g| is zero. |
85 | 4.55k | if (BN_is_negative(dsa->p) || BN_is_negative(dsa->q) || BN_is_zero(dsa->p) || |
86 | 4.55k | BN_is_zero(dsa->q) || !BN_is_odd(dsa->p) || !BN_is_odd(dsa->q) || |
87 | | // |q| must be a prime divisor of |p - 1|, which implies |q < p|. |
88 | 4.55k | BN_cmp(dsa->q, dsa->p) >= 0 || |
89 | | // |g| is in the multiplicative group of |p|. |
90 | 4.55k | BN_is_negative(dsa->g) || BN_is_zero(dsa->g) || |
91 | 4.55k | BN_cmp(dsa->g, dsa->p) >= 0) { |
92 | 618 | OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS); |
93 | 618 | return 0; |
94 | 618 | } |
95 | | |
96 | | // FIPS 186-4 allows only three different sizes for q. |
97 | 3.93k | unsigned q_bits = BN_num_bits(dsa->q); |
98 | 3.93k | if (q_bits != 160 && q_bits != 224 && q_bits != 256) { |
99 | 902 | OPENSSL_PUT_ERROR(DSA, DSA_R_BAD_Q_VALUE); |
100 | 902 | return 0; |
101 | 902 | } |
102 | | |
103 | | // Bound |dsa->p| to avoid a DoS vector. Note this limit is much larger than |
104 | | // the one in FIPS 186-4, which only allows L = 1024, 2048, and 3072. |
105 | 3.03k | if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) { |
106 | 18 | OPENSSL_PUT_ERROR(DSA, DSA_R_MODULUS_TOO_LARGE); |
107 | 18 | return 0; |
108 | 18 | } |
109 | | |
110 | 3.01k | if (dsa->pub_key != NULL) { |
111 | | // The public key is also in the multiplicative group of |p|. |
112 | 9 | if (BN_is_negative(dsa->pub_key) || BN_is_zero(dsa->pub_key) || |
113 | 9 | BN_cmp(dsa->pub_key, dsa->p) >= 0) { |
114 | 3 | OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS); |
115 | 3 | return 0; |
116 | 3 | } |
117 | 9 | } |
118 | | |
119 | 3.01k | if (dsa->priv_key != NULL) { |
120 | | // The private key is a non-zero element of the scalar field, determined by |
121 | | // |q|. |
122 | 1.43k | if (BN_is_negative(dsa->priv_key) || BN_is_zero(dsa->priv_key) || |
123 | 1.43k | BN_cmp(dsa->priv_key, dsa->q) >= 0) { |
124 | 19 | OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS); |
125 | 19 | return 0; |
126 | 19 | } |
127 | 1.43k | } |
128 | | |
129 | 2.99k | return 1; |
130 | 3.01k | } |
131 | | |
132 | 10.6k | static int parse_integer(CBS *cbs, BIGNUM **out) { |
133 | 10.6k | assert(*out == NULL); |
134 | 10.6k | *out = BN_new(); |
135 | 10.6k | if (*out == NULL) { |
136 | 0 | return 0; |
137 | 0 | } |
138 | 10.6k | return BN_parse_asn1_unsigned(cbs, *out); |
139 | 10.6k | } |
140 | | |
141 | 3.37k | static int marshal_integer(CBB *cbb, BIGNUM *bn) { |
142 | 3.37k | if (bn == NULL) { |
143 | | // A DSA object may be missing some components. |
144 | 0 | OPENSSL_PUT_ERROR(DSA, ERR_R_PASSED_NULL_PARAMETER); |
145 | 0 | return 0; |
146 | 0 | } |
147 | 3.37k | return BN_marshal_asn1(cbb, bn); |
148 | 3.37k | } |
149 | | |
150 | 0 | DSA_SIG *DSA_SIG_parse(CBS *cbs) { |
151 | 0 | DSA_SIG *ret = DSA_SIG_new(); |
152 | 0 | if (ret == NULL) { |
153 | 0 | return NULL; |
154 | 0 | } |
155 | 0 | CBS child; |
156 | 0 | if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) || |
157 | 0 | !parse_integer(&child, &ret->r) || |
158 | 0 | !parse_integer(&child, &ret->s) || |
159 | 0 | CBS_len(&child) != 0) { |
160 | 0 | OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR); |
161 | 0 | DSA_SIG_free(ret); |
162 | 0 | return NULL; |
163 | 0 | } |
164 | 0 | return ret; |
165 | 0 | } |
166 | | |
167 | 0 | int DSA_SIG_marshal(CBB *cbb, const DSA_SIG *sig) { |
168 | 0 | CBB child; |
169 | 0 | if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) || |
170 | 0 | !marshal_integer(&child, sig->r) || |
171 | 0 | !marshal_integer(&child, sig->s) || |
172 | 0 | !CBB_flush(cbb)) { |
173 | 0 | OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR); |
174 | 0 | return 0; |
175 | 0 | } |
176 | 0 | return 1; |
177 | 0 | } |
178 | | |
179 | 0 | DSA *DSA_parse_public_key(CBS *cbs) { |
180 | 0 | DSA *ret = DSA_new(); |
181 | 0 | if (ret == NULL) { |
182 | 0 | return NULL; |
183 | 0 | } |
184 | 0 | CBS child; |
185 | 0 | if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) || |
186 | 0 | !parse_integer(&child, &ret->pub_key) || |
187 | 0 | !parse_integer(&child, &ret->p) || |
188 | 0 | !parse_integer(&child, &ret->q) || |
189 | 0 | !parse_integer(&child, &ret->g) || |
190 | 0 | CBS_len(&child) != 0) { |
191 | 0 | OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR); |
192 | 0 | goto err; |
193 | 0 | } |
194 | 0 | if (!dsa_check_key(ret)) { |
195 | 0 | goto err; |
196 | 0 | } |
197 | 0 | return ret; |
198 | | |
199 | 0 | err: |
200 | 0 | DSA_free(ret); |
201 | 0 | return NULL; |
202 | 0 | } |
203 | | |
204 | 0 | int DSA_marshal_public_key(CBB *cbb, const DSA *dsa) { |
205 | 0 | CBB child; |
206 | 0 | if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) || |
207 | 0 | !marshal_integer(&child, dsa->pub_key) || |
208 | 0 | !marshal_integer(&child, dsa->p) || |
209 | 0 | !marshal_integer(&child, dsa->q) || |
210 | 0 | !marshal_integer(&child, dsa->g) || |
211 | 0 | !CBB_flush(cbb)) { |
212 | 0 | OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR); |
213 | 0 | return 0; |
214 | 0 | } |
215 | 0 | return 1; |
216 | 0 | } |
217 | | |
218 | 3.20k | DSA *DSA_parse_parameters(CBS *cbs) { |
219 | 3.20k | DSA *ret = DSA_new(); |
220 | 3.20k | if (ret == NULL) { |
221 | 0 | return NULL; |
222 | 0 | } |
223 | 3.20k | CBS child; |
224 | 3.20k | if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) || |
225 | 3.20k | !parse_integer(&child, &ret->p) || |
226 | 3.20k | !parse_integer(&child, &ret->q) || |
227 | 3.20k | !parse_integer(&child, &ret->g) || |
228 | 3.20k | CBS_len(&child) != 0) { |
229 | 389 | OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR); |
230 | 389 | goto err; |
231 | 389 | } |
232 | 2.81k | if (!dsa_check_key(ret)) { |
233 | 1.23k | goto err; |
234 | 1.23k | } |
235 | 1.57k | return ret; |
236 | | |
237 | 1.62k | err: |
238 | 1.62k | DSA_free(ret); |
239 | 1.62k | return NULL; |
240 | 2.81k | } |
241 | | |
242 | 1.12k | int DSA_marshal_parameters(CBB *cbb, const DSA *dsa) { |
243 | 1.12k | CBB child; |
244 | 1.12k | if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) || |
245 | 1.12k | !marshal_integer(&child, dsa->p) || |
246 | 1.12k | !marshal_integer(&child, dsa->q) || |
247 | 1.12k | !marshal_integer(&child, dsa->g) || |
248 | 1.12k | !CBB_flush(cbb)) { |
249 | 0 | OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR); |
250 | 0 | return 0; |
251 | 0 | } |
252 | 1.12k | return 1; |
253 | 1.12k | } |
254 | | |
255 | 442 | DSA *DSA_parse_private_key(CBS *cbs) { |
256 | 442 | DSA *ret = DSA_new(); |
257 | 442 | if (ret == NULL) { |
258 | 0 | return NULL; |
259 | 0 | } |
260 | | |
261 | 442 | CBS child; |
262 | 442 | uint64_t version; |
263 | 442 | if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) || |
264 | 442 | !CBS_get_asn1_uint64(&child, &version)) { |
265 | 1 | OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR); |
266 | 1 | goto err; |
267 | 1 | } |
268 | | |
269 | 441 | if (version != 0) { |
270 | 125 | OPENSSL_PUT_ERROR(DSA, DSA_R_BAD_VERSION); |
271 | 125 | goto err; |
272 | 125 | } |
273 | | |
274 | 316 | if (!parse_integer(&child, &ret->p) || |
275 | 316 | !parse_integer(&child, &ret->q) || |
276 | 316 | !parse_integer(&child, &ret->g) || |
277 | 316 | !parse_integer(&child, &ret->pub_key) || |
278 | 316 | !parse_integer(&child, &ret->priv_key) || |
279 | 316 | CBS_len(&child) != 0) { |
280 | 7 | OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR); |
281 | 7 | goto err; |
282 | 7 | } |
283 | 309 | if (!dsa_check_key(ret)) { |
284 | 306 | goto err; |
285 | 306 | } |
286 | | |
287 | 3 | return ret; |
288 | | |
289 | 439 | err: |
290 | 439 | DSA_free(ret); |
291 | 439 | return NULL; |
292 | 309 | } |
293 | | |
294 | 0 | int DSA_marshal_private_key(CBB *cbb, const DSA *dsa) { |
295 | 0 | CBB child; |
296 | 0 | if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) || |
297 | 0 | !CBB_add_asn1_uint64(&child, 0 /* version */) || |
298 | 0 | !marshal_integer(&child, dsa->p) || |
299 | 0 | !marshal_integer(&child, dsa->q) || |
300 | 0 | !marshal_integer(&child, dsa->g) || |
301 | 0 | !marshal_integer(&child, dsa->pub_key) || |
302 | 0 | !marshal_integer(&child, dsa->priv_key) || |
303 | 0 | !CBB_flush(cbb)) { |
304 | 0 | OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR); |
305 | 0 | return 0; |
306 | 0 | } |
307 | 0 | return 1; |
308 | 0 | } |
309 | | |
310 | 0 | DSA_SIG *d2i_DSA_SIG(DSA_SIG **out_sig, const uint8_t **inp, long len) { |
311 | 0 | if (len < 0) { |
312 | 0 | return NULL; |
313 | 0 | } |
314 | 0 | CBS cbs; |
315 | 0 | CBS_init(&cbs, *inp, (size_t)len); |
316 | 0 | DSA_SIG *ret = DSA_SIG_parse(&cbs); |
317 | 0 | if (ret == NULL) { |
318 | 0 | return NULL; |
319 | 0 | } |
320 | 0 | if (out_sig != NULL) { |
321 | 0 | DSA_SIG_free(*out_sig); |
322 | 0 | *out_sig = ret; |
323 | 0 | } |
324 | 0 | *inp = CBS_data(&cbs); |
325 | 0 | return ret; |
326 | 0 | } |
327 | | |
328 | 0 | int i2d_DSA_SIG(const DSA_SIG *in, uint8_t **outp) { |
329 | 0 | CBB cbb; |
330 | 0 | if (!CBB_init(&cbb, 0) || |
331 | 0 | !DSA_SIG_marshal(&cbb, in)) { |
332 | 0 | CBB_cleanup(&cbb); |
333 | 0 | return -1; |
334 | 0 | } |
335 | 0 | return CBB_finish_i2d(&cbb, outp); |
336 | 0 | } |
337 | | |
338 | 0 | DSA *d2i_DSAPublicKey(DSA **out, const uint8_t **inp, long len) { |
339 | 0 | if (len < 0) { |
340 | 0 | return NULL; |
341 | 0 | } |
342 | 0 | CBS cbs; |
343 | 0 | CBS_init(&cbs, *inp, (size_t)len); |
344 | 0 | DSA *ret = DSA_parse_public_key(&cbs); |
345 | 0 | if (ret == NULL) { |
346 | 0 | return NULL; |
347 | 0 | } |
348 | 0 | if (out != NULL) { |
349 | 0 | DSA_free(*out); |
350 | 0 | *out = ret; |
351 | 0 | } |
352 | 0 | *inp = CBS_data(&cbs); |
353 | 0 | return ret; |
354 | 0 | } |
355 | | |
356 | 0 | int i2d_DSAPublicKey(const DSA *in, uint8_t **outp) { |
357 | 0 | CBB cbb; |
358 | 0 | if (!CBB_init(&cbb, 0) || |
359 | 0 | !DSA_marshal_public_key(&cbb, in)) { |
360 | 0 | CBB_cleanup(&cbb); |
361 | 0 | return -1; |
362 | 0 | } |
363 | 0 | return CBB_finish_i2d(&cbb, outp); |
364 | 0 | } |
365 | | |
366 | 0 | DSA *d2i_DSAPrivateKey(DSA **out, const uint8_t **inp, long len) { |
367 | 0 | if (len < 0) { |
368 | 0 | return NULL; |
369 | 0 | } |
370 | 0 | CBS cbs; |
371 | 0 | CBS_init(&cbs, *inp, (size_t)len); |
372 | 0 | DSA *ret = DSA_parse_private_key(&cbs); |
373 | 0 | if (ret == NULL) { |
374 | 0 | return NULL; |
375 | 0 | } |
376 | 0 | if (out != NULL) { |
377 | 0 | DSA_free(*out); |
378 | 0 | *out = ret; |
379 | 0 | } |
380 | 0 | *inp = CBS_data(&cbs); |
381 | 0 | return ret; |
382 | 0 | } |
383 | | |
384 | 0 | int i2d_DSAPrivateKey(const DSA *in, uint8_t **outp) { |
385 | 0 | CBB cbb; |
386 | 0 | if (!CBB_init(&cbb, 0) || |
387 | 0 | !DSA_marshal_private_key(&cbb, in)) { |
388 | 0 | CBB_cleanup(&cbb); |
389 | 0 | return -1; |
390 | 0 | } |
391 | 0 | return CBB_finish_i2d(&cbb, outp); |
392 | 0 | } |
393 | | |
394 | 0 | DSA *d2i_DSAparams(DSA **out, const uint8_t **inp, long len) { |
395 | 0 | if (len < 0) { |
396 | 0 | return NULL; |
397 | 0 | } |
398 | 0 | CBS cbs; |
399 | 0 | CBS_init(&cbs, *inp, (size_t)len); |
400 | 0 | DSA *ret = DSA_parse_parameters(&cbs); |
401 | 0 | if (ret == NULL) { |
402 | 0 | return NULL; |
403 | 0 | } |
404 | 0 | if (out != NULL) { |
405 | 0 | DSA_free(*out); |
406 | 0 | *out = ret; |
407 | 0 | } |
408 | 0 | *inp = CBS_data(&cbs); |
409 | 0 | return ret; |
410 | 0 | } |
411 | | |
412 | 0 | int i2d_DSAparams(const DSA *in, uint8_t **outp) { |
413 | 0 | CBB cbb; |
414 | 0 | if (!CBB_init(&cbb, 0) || |
415 | 0 | !DSA_marshal_parameters(&cbb, in)) { |
416 | 0 | CBB_cleanup(&cbb); |
417 | 0 | return -1; |
418 | 0 | } |
419 | 0 | return CBB_finish_i2d(&cbb, outp); |
420 | 0 | } |