Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/dsa/dsa_asn1.c

raw
1 // Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project 2000
2 // Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
4
5 #include <openssl/dsa.h>
6
7 #include <assert.h>
8
9 #include <openssl/bn.h>
10 #include <openssl/bytestring.h>
11 #include <openssl/err.h>
12 #include <openssl/mem.h>
13
14 #include "internal.h"
15 #include "../bytestring/internal.h"
16 #include "../crypto/internal.h"
17
18
19 // This function is in dsa_asn1.c rather than dsa.c because it is reachable from
20 // |EVP_PKEY| parsers. This makes it easier for the static linker to drop most
21 // of the DSA implementation.
22 int dsa_check_key(const DSA *dsa) {
23 if (!dsa->p || !dsa->q || !dsa->g) {
24 OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
25 return 0;
26 }
27
28 // Fully checking for invalid DSA groups is expensive, so security and
29 // correctness of the signature scheme depend on how |dsa| was computed. I.e.
30 // we leave "assurance of domain parameter validity" from FIPS 186-4 to the
31 // caller. However, we check bounds on all values to avoid DoS vectors even
32 // when domain parameters are invalid. In particular, signing will infinite
33 // loop if |g| is zero.
34 if (BN_is_negative(dsa->p) || BN_is_negative(dsa->q) || BN_is_zero(dsa->p) ||
35 BN_is_zero(dsa->q) || !BN_is_odd(dsa->p) || !BN_is_odd(dsa->q) ||
36 // |q| must be a prime divisor of |p - 1|, which implies |q < p|.
37 BN_cmp(dsa->q, dsa->p) >= 0 ||
38 // |g| is in the multiplicative group of |p|.
39 BN_is_negative(dsa->g) || BN_is_zero(dsa->g) ||
40 BN_cmp(dsa->g, dsa->p) >= 0) {
41 OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS);
42 return 0;
43 }
44
45 // FIPS 186-4 allows only three different sizes for q.
46 unsigned q_bits = BN_num_bits(dsa->q);
47 if (q_bits != 160 && q_bits != 224 && q_bits != 256) {
48 OPENSSL_PUT_ERROR(DSA, DSA_R_BAD_Q_VALUE);
49 return 0;
50 }
51
52 // Bound |dsa->p| to avoid a DoS vector. Note this limit is much larger than
53 // the one in FIPS 186-4, which only allows L = 1024, 2048, and 3072.
54 if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
55 OPENSSL_PUT_ERROR(DSA, DSA_R_MODULUS_TOO_LARGE);
56 return 0;
57 }
58
59 if (dsa->pub_key != NULL) {
60 // The public key is also in the multiplicative group of |p|.
61 if (BN_is_negative(dsa->pub_key) || BN_is_zero(dsa->pub_key) ||
62 BN_cmp(dsa->pub_key, dsa->p) >= 0) {
63 OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS);
64 return 0;
65 }
66 }
67
68 if (dsa->priv_key != NULL) {
69 // The private key is a non-zero element of the scalar field, determined by
70 // |q|.
71 if (BN_is_negative(dsa->priv_key) ||
72 constant_time_declassify_int(BN_is_zero(dsa->priv_key)) ||
73 constant_time_declassify_int(BN_cmp(dsa->priv_key, dsa->q) >= 0)) {
74 OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS);
75 return 0;
76 }
77 }
78
79 return 1;
80 }
81
82 static int parse_integer(CBS *cbs, BIGNUM **out) {
83 assert(*out == NULL);
84 *out = BN_new();
85 if (*out == NULL) {
86 return 0;
87 }
88 return BN_parse_asn1_unsigned(cbs, *out);
89 }
90
91 static int marshal_integer(CBB *cbb, BIGNUM *bn) {
92 if (bn == NULL) {
93 // A DSA object may be missing some components.
94 OPENSSL_PUT_ERROR(DSA, ERR_R_PASSED_NULL_PARAMETER);
95 return 0;
96 }
97 return BN_marshal_asn1(cbb, bn);
98 }
99
100 DSA_SIG *DSA_SIG_parse(CBS *cbs) {
101 DSA_SIG *ret = DSA_SIG_new();
102 if (ret == NULL) {
103 return NULL;
104 }
105 CBS child;
106 if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
107 !parse_integer(&child, &ret->r) ||
108 !parse_integer(&child, &ret->s) ||
109 CBS_len(&child) != 0) {
110 OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
111 DSA_SIG_free(ret);
112 return NULL;
113 }
114 return ret;
115 }
116
117 int DSA_SIG_marshal(CBB *cbb, const DSA_SIG *sig) {
118 CBB child;
119 if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
120 !marshal_integer(&child, sig->r) ||
121 !marshal_integer(&child, sig->s) ||
122 !CBB_flush(cbb)) {
123 OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR);
124 return 0;
125 }
126 return 1;
127 }
128
129 DSA *DSA_parse_public_key(CBS *cbs) {
130 DSA *ret = DSA_new();
131 if (ret == NULL) {
132 return NULL;
133 }
134 CBS child;
135 if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
136 !parse_integer(&child, &ret->pub_key) ||
137 !parse_integer(&child, &ret->p) ||
138 !parse_integer(&child, &ret->q) ||
139 !parse_integer(&child, &ret->g) ||
140 CBS_len(&child) != 0) {
141 OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
142 goto err;
143 }
144 if (!dsa_check_key(ret)) {
145 goto err;
146 }
147 return ret;
148
149 err:
150 DSA_free(ret);
151 return NULL;
152 }
153
154 int DSA_marshal_public_key(CBB *cbb, const DSA *dsa) {
155 CBB child;
156 if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
157 !marshal_integer(&child, dsa->pub_key) ||
158 !marshal_integer(&child, dsa->p) ||
159 !marshal_integer(&child, dsa->q) ||
160 !marshal_integer(&child, dsa->g) ||
161 !CBB_flush(cbb)) {
162 OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR);
163 return 0;
164 }
165 return 1;
166 }
167
168 DSA *DSA_parse_parameters(CBS *cbs) {
169 DSA *ret = DSA_new();
170 if (ret == NULL) {
171 return NULL;
172 }
173 CBS child;
174 if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
175 !parse_integer(&child, &ret->p) ||
176 !parse_integer(&child, &ret->q) ||
177 !parse_integer(&child, &ret->g) ||
178 CBS_len(&child) != 0) {
179 OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
180 goto err;
181 }
182 if (!dsa_check_key(ret)) {
183 goto err;
184 }
185 return ret;
186
187 err:
188 DSA_free(ret);
189 return NULL;
190 }
191
192 int DSA_marshal_parameters(CBB *cbb, const DSA *dsa) {
193 CBB child;
194 if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
195 !marshal_integer(&child, dsa->p) ||
196 !marshal_integer(&child, dsa->q) ||
197 !marshal_integer(&child, dsa->g) ||
198 !CBB_flush(cbb)) {
199 OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR);
200 return 0;
201 }
202 return 1;
203 }
204
205 DSA *DSA_parse_private_key(CBS *cbs) {
206 DSA *ret = DSA_new();
207 if (ret == NULL) {
208 return NULL;
209 }
210
211 CBS child;
212 uint64_t version;
213 if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
214 !CBS_get_asn1_uint64(&child, &version)) {
215 OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
216 goto err;
217 }
218
219 if (version != 0) {
220 OPENSSL_PUT_ERROR(DSA, DSA_R_BAD_VERSION);
221 goto err;
222 }
223
224 if (!parse_integer(&child, &ret->p) ||
225 !parse_integer(&child, &ret->q) ||
226 !parse_integer(&child, &ret->g) ||
227 !parse_integer(&child, &ret->pub_key) ||
228 !parse_integer(&child, &ret->priv_key) ||
229 CBS_len(&child) != 0) {
230 OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
231 goto err;
232 }
233 if (!dsa_check_key(ret)) {
234 goto err;
235 }
236
237 return ret;
238
239 err:
240 DSA_free(ret);
241 return NULL;
242 }
243
244 int DSA_marshal_private_key(CBB *cbb, const DSA *dsa) {
245 CBB child;
246 if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
247 !CBB_add_asn1_uint64(&child, 0 /* version */) ||
248 !marshal_integer(&child, dsa->p) ||
249 !marshal_integer(&child, dsa->q) ||
250 !marshal_integer(&child, dsa->g) ||
251 !marshal_integer(&child, dsa->pub_key) ||
252 !marshal_integer(&child, dsa->priv_key) ||
253 !CBB_flush(cbb)) {
254 OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR);
255 return 0;
256 }
257 return 1;
258 }
259
260 DSA_SIG *d2i_DSA_SIG(DSA_SIG **out_sig, const uint8_t **inp, long len) {
261 if (len < 0) {
262 return NULL;
263 }
264 CBS cbs;
265 CBS_init(&cbs, *inp, (size_t)len);
266 DSA_SIG *ret = DSA_SIG_parse(&cbs);
267 if (ret == NULL) {
268 return NULL;
269 }
270 if (out_sig != NULL) {
271 DSA_SIG_free(*out_sig);
272 *out_sig = ret;
273 }
274 *inp = CBS_data(&cbs);
275 return ret;
276 }
277
278 int i2d_DSA_SIG(const DSA_SIG *in, uint8_t **outp) {
279 CBB cbb;
280 if (!CBB_init(&cbb, 0) ||
281 !DSA_SIG_marshal(&cbb, in)) {
282 CBB_cleanup(&cbb);
283 return -1;
284 }
285 return CBB_finish_i2d(&cbb, outp);
286 }
287
288 DSA *d2i_DSAPublicKey(DSA **out, const uint8_t **inp, long len) {
289 if (len < 0) {
290 return NULL;
291 }
292 CBS cbs;
293 CBS_init(&cbs, *inp, (size_t)len);
294 DSA *ret = DSA_parse_public_key(&cbs);
295 if (ret == NULL) {
296 return NULL;
297 }
298 if (out != NULL) {
299 DSA_free(*out);
300 *out = ret;
301 }
302 *inp = CBS_data(&cbs);
303 return ret;
304 }
305
306 int i2d_DSAPublicKey(const DSA *in, uint8_t **outp) {
307 CBB cbb;
308 if (!CBB_init(&cbb, 0) ||
309 !DSA_marshal_public_key(&cbb, in)) {
310 CBB_cleanup(&cbb);
311 return -1;
312 }
313 return CBB_finish_i2d(&cbb, outp);
314 }
315
316 DSA *d2i_DSAPrivateKey(DSA **out, const uint8_t **inp, long len) {
317 if (len < 0) {
318 return NULL;
319 }
320 CBS cbs;
321 CBS_init(&cbs, *inp, (size_t)len);
322 DSA *ret = DSA_parse_private_key(&cbs);
323 if (ret == NULL) {
324 return NULL;
325 }
326 if (out != NULL) {
327 DSA_free(*out);
328 *out = ret;
329 }
330 *inp = CBS_data(&cbs);
331 return ret;
332 }
333
334 int i2d_DSAPrivateKey(const DSA *in, uint8_t **outp) {
335 CBB cbb;
336 if (!CBB_init(&cbb, 0) ||
337 !DSA_marshal_private_key(&cbb, in)) {
338 CBB_cleanup(&cbb);
339 return -1;
340 }
341 return CBB_finish_i2d(&cbb, outp);
342 }
343
344 DSA *d2i_DSAparams(DSA **out, const uint8_t **inp, long len) {
345 if (len < 0) {
346 return NULL;
347 }
348 CBS cbs;
349 CBS_init(&cbs, *inp, (size_t)len);
350 DSA *ret = DSA_parse_parameters(&cbs);
351 if (ret == NULL) {
352 return NULL;
353 }
354 if (out != NULL) {
355 DSA_free(*out);
356 *out = ret;
357 }
358 *inp = CBS_data(&cbs);
359 return ret;
360 }
361
362 int i2d_DSAparams(const DSA *in, uint8_t **outp) {
363 CBB cbb;
364 if (!CBB_init(&cbb, 0) ||
365 !DSA_marshal_parameters(&cbb, in)) {
366 CBB_cleanup(&cbb);
367 return -1;
368 }
369 return CBB_finish_i2d(&cbb, outp);
370 }