xref: /openbsd-src/lib/libcrypto/dsa/dsa_lib.c (revision 615b62e1894ea2dfd7654e41efd430c00eafe472)
1 /* $OpenBSD: dsa_lib.c,v 1.43 2023/07/08 14:28:15 beck Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 /* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */
60 
61 #include <stdio.h>
62 
63 #include <openssl/opensslconf.h>
64 
65 #include <openssl/asn1.h>
66 #include <openssl/bn.h>
67 #include <openssl/dsa.h>
68 #include <openssl/err.h>
69 
70 #ifndef OPENSSL_NO_DH
71 #include <openssl/dh.h>
72 #endif
73 #ifndef OPENSSL_NO_ENGINE
74 #include <openssl/engine.h>
75 #endif
76 
77 #include "dh_local.h"
78 #include "dsa_local.h"
79 
80 static const DSA_METHOD *default_DSA_method = NULL;
81 
82 void
83 DSA_set_default_method(const DSA_METHOD *meth)
84 {
85 	default_DSA_method = meth;
86 }
87 LCRYPTO_ALIAS(DSA_set_default_method);
88 
89 const DSA_METHOD *
90 DSA_get_default_method(void)
91 {
92 	if (!default_DSA_method)
93 		default_DSA_method = DSA_OpenSSL();
94 	return default_DSA_method;
95 }
96 LCRYPTO_ALIAS(DSA_get_default_method);
97 
98 DSA *
99 DSA_new(void)
100 {
101 	return DSA_new_method(NULL);
102 }
103 LCRYPTO_ALIAS(DSA_new);
104 
105 int
106 DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
107 {
108 	/*
109 	 * NB: The caller is specifically setting a method, so it's not up to us
110 	 * to deal with which ENGINE it comes from.
111 	 */
112 	const DSA_METHOD *mtmp;
113 	mtmp = dsa->meth;
114 	if (mtmp->finish)
115 		mtmp->finish(dsa);
116 #ifndef OPENSSL_NO_ENGINE
117 	ENGINE_finish(dsa->engine);
118 	dsa->engine = NULL;
119 #endif
120 	dsa->meth = meth;
121 	if (meth->init)
122 		meth->init(dsa);
123 	return 1;
124 }
125 LCRYPTO_ALIAS(DSA_set_method);
126 
127 DSA *
128 DSA_new_method(ENGINE *engine)
129 {
130 	DSA *ret;
131 
132 	ret = malloc(sizeof(DSA));
133 	if (ret == NULL) {
134 		DSAerror(ERR_R_MALLOC_FAILURE);
135 		return NULL;
136 	}
137 	ret->meth = DSA_get_default_method();
138 #ifndef OPENSSL_NO_ENGINE
139 	if (engine) {
140 		if (!ENGINE_init(engine)) {
141 			DSAerror(ERR_R_ENGINE_LIB);
142 			free(ret);
143 			return NULL;
144 		}
145 		ret->engine = engine;
146 	} else
147 		ret->engine = ENGINE_get_default_DSA();
148 	if (ret->engine) {
149 		ret->meth = ENGINE_get_DSA(ret->engine);
150 		if (ret->meth == NULL) {
151 			DSAerror(ERR_R_ENGINE_LIB);
152 			ENGINE_finish(ret->engine);
153 			free(ret);
154 			return NULL;
155 		}
156 	}
157 #endif
158 
159 	ret->pad = 0;
160 	ret->version = 0;
161 	ret->p = NULL;
162 	ret->q = NULL;
163 	ret->g = NULL;
164 
165 	ret->pub_key = NULL;
166 	ret->priv_key = NULL;
167 
168 	ret->kinv = NULL;
169 	ret->r = NULL;
170 	ret->method_mont_p = NULL;
171 
172 	ret->references = 1;
173 	ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
174 	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data);
175 	if (ret->meth->init != NULL && !ret->meth->init(ret)) {
176 #ifndef OPENSSL_NO_ENGINE
177 		ENGINE_finish(ret->engine);
178 #endif
179 		CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data);
180 		free(ret);
181 		ret = NULL;
182 	}
183 
184 	return ret;
185 }
186 LCRYPTO_ALIAS(DSA_new_method);
187 
188 void
189 DSA_free(DSA *r)
190 {
191 	int i;
192 
193 	if (r == NULL)
194 		return;
195 
196 	i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DSA);
197 	if (i > 0)
198 		return;
199 
200 	if (r->meth->finish)
201 		r->meth->finish(r);
202 #ifndef OPENSSL_NO_ENGINE
203 	ENGINE_finish(r->engine);
204 #endif
205 
206 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
207 
208 	BN_free(r->p);
209 	BN_free(r->q);
210 	BN_free(r->g);
211 	BN_free(r->pub_key);
212 	BN_free(r->priv_key);
213 	BN_free(r->kinv);
214 	BN_free(r->r);
215 	free(r);
216 }
217 LCRYPTO_ALIAS(DSA_free);
218 
219 int
220 DSA_up_ref(DSA *r)
221 {
222 	int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DSA);
223 	return i > 1 ? 1 : 0;
224 }
225 LCRYPTO_ALIAS(DSA_up_ref);
226 
227 int
228 DSA_size(const DSA *r)
229 {
230 	DSA_SIG signature;
231 	int ret = 0;
232 
233 	signature.r = r->q;
234 	signature.s = r->q;
235 
236 	if ((ret = i2d_DSA_SIG(&signature, NULL)) < 0)
237 		ret = 0;
238 
239 	return ret;
240 }
241 LCRYPTO_ALIAS(DSA_size);
242 
243 int
244 DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
245     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
246 {
247 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, argl, argp,
248 	    new_func, dup_func, free_func);
249 }
250 LCRYPTO_ALIAS(DSA_get_ex_new_index);
251 
252 int
253 DSA_set_ex_data(DSA *d, int idx, void *arg)
254 {
255 	return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
256 }
257 LCRYPTO_ALIAS(DSA_set_ex_data);
258 
259 void *
260 DSA_get_ex_data(DSA *d, int idx)
261 {
262 	return CRYPTO_get_ex_data(&d->ex_data, idx);
263 }
264 LCRYPTO_ALIAS(DSA_get_ex_data);
265 
266 int
267 DSA_security_bits(const DSA *d)
268 {
269 	if (d->p == NULL || d->q == NULL)
270 		return -1;
271 
272 	return BN_security_bits(BN_num_bits(d->p), BN_num_bits(d->q));
273 }
274 LCRYPTO_ALIAS(DSA_security_bits);
275 
276 #ifndef OPENSSL_NO_DH
277 DH *
278 DSA_dup_DH(const DSA *r)
279 {
280 	/*
281 	 * DSA has p, q, g, optional pub_key, optional priv_key.
282 	 * DH has p, optional length, g, optional pub_key, optional priv_key,
283 	 * optional q.
284 	 */
285 	DH *ret = NULL;
286 
287 	if (r == NULL)
288 		goto err;
289 	ret = DH_new();
290 	if (ret == NULL)
291 		goto err;
292 	if (r->p != NULL)
293 		if ((ret->p = BN_dup(r->p)) == NULL)
294 			goto err;
295 	if (r->q != NULL) {
296 		ret->length = BN_num_bits(r->q);
297 		if ((ret->q = BN_dup(r->q)) == NULL)
298 			goto err;
299 	}
300 	if (r->g != NULL)
301 		if ((ret->g = BN_dup(r->g)) == NULL)
302 			goto err;
303 	if (r->pub_key != NULL)
304 		if ((ret->pub_key = BN_dup(r->pub_key)) == NULL)
305 			goto err;
306 	if (r->priv_key != NULL)
307 		if ((ret->priv_key = BN_dup(r->priv_key)) == NULL)
308 			goto err;
309 
310 	return ret;
311 
312 err:
313 	DH_free(ret);
314 	return NULL;
315 }
316 LCRYPTO_ALIAS(DSA_dup_DH);
317 #endif
318 
319 void
320 DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
321 {
322 	if (p != NULL)
323 		*p = d->p;
324 	if (q != NULL)
325 		*q = d->q;
326 	if (g != NULL)
327 		*g = d->g;
328 }
329 LCRYPTO_ALIAS(DSA_get0_pqg);
330 
331 int
332 DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
333 {
334 	if ((d->p == NULL && p == NULL) || (d->q == NULL && q == NULL) ||
335 	    (d->g == NULL && g == NULL))
336 		return 0;
337 
338 	if (p != NULL) {
339 		BN_free(d->p);
340 		d->p = p;
341 	}
342 	if (q != NULL) {
343 		BN_free(d->q);
344 		d->q = q;
345 	}
346 	if (g != NULL) {
347 		BN_free(d->g);
348 		d->g = g;
349 	}
350 
351 	return 1;
352 }
353 LCRYPTO_ALIAS(DSA_set0_pqg);
354 
355 void
356 DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key)
357 {
358 	if (pub_key != NULL)
359 		*pub_key = d->pub_key;
360 	if (priv_key != NULL)
361 		*priv_key = d->priv_key;
362 }
363 LCRYPTO_ALIAS(DSA_get0_key);
364 
365 int
366 DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
367 {
368 	if (d->pub_key == NULL && pub_key == NULL)
369 		return 0;
370 
371 	if (pub_key != NULL) {
372 		BN_free(d->pub_key);
373 		d->pub_key = pub_key;
374 	}
375 	if (priv_key != NULL) {
376 		BN_free(d->priv_key);
377 		d->priv_key = priv_key;
378 	}
379 
380 	return 1;
381 }
382 LCRYPTO_ALIAS(DSA_set0_key);
383 
384 const BIGNUM *
385 DSA_get0_p(const DSA *d)
386 {
387 	return d->p;
388 }
389 LCRYPTO_ALIAS(DSA_get0_p);
390 
391 const BIGNUM *
392 DSA_get0_q(const DSA *d)
393 {
394 	return d->q;
395 }
396 LCRYPTO_ALIAS(DSA_get0_q);
397 
398 const BIGNUM *
399 DSA_get0_g(const DSA *d)
400 {
401 	return d->g;
402 }
403 LCRYPTO_ALIAS(DSA_get0_g);
404 
405 const BIGNUM *
406 DSA_get0_pub_key(const DSA *d)
407 {
408 	return d->pub_key;
409 }
410 LCRYPTO_ALIAS(DSA_get0_pub_key);
411 
412 const BIGNUM *
413 DSA_get0_priv_key(const DSA *d)
414 {
415 	return d->priv_key;
416 }
417 LCRYPTO_ALIAS(DSA_get0_priv_key);
418 
419 void
420 DSA_clear_flags(DSA *d, int flags)
421 {
422 	d->flags &= ~flags;
423 }
424 LCRYPTO_ALIAS(DSA_clear_flags);
425 
426 int
427 DSA_test_flags(const DSA *d, int flags)
428 {
429 	return d->flags & flags;
430 }
431 LCRYPTO_ALIAS(DSA_test_flags);
432 
433 void
434 DSA_set_flags(DSA *d, int flags)
435 {
436 	d->flags |= flags;
437 }
438 LCRYPTO_ALIAS(DSA_set_flags);
439 
440 ENGINE *
441 DSA_get0_engine(DSA *d)
442 {
443 	return d->engine;
444 }
445 LCRYPTO_ALIAS(DSA_get0_engine);
446 
447 int
448 DSA_bits(const DSA *dsa)
449 {
450 	return BN_num_bits(dsa->p);
451 }
452 LCRYPTO_ALIAS(DSA_bits);
453 
454 int
455 dsa_check_key(const DSA *dsa)
456 {
457 	int p_bits, q_bits;
458 
459 	if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {
460 		DSAerror(DSA_R_MISSING_PARAMETERS);
461 		return 0;
462 	}
463 
464 	/* Checking that p and q are primes is expensive. Check they are odd. */
465 	if (!BN_is_odd(dsa->p) || !BN_is_odd(dsa->q)) {
466 		DSAerror(DSA_R_INVALID_PARAMETERS);
467 		return 0;
468 	}
469 
470 	/* FIPS 186-4: 1 < g < p. */
471 	if (BN_cmp(dsa->g, BN_value_one()) <= 0 ||
472 	    BN_cmp(dsa->g, dsa->p) >= 0) {
473 		DSAerror(DSA_R_INVALID_PARAMETERS);
474 		return 0;
475 	}
476 
477 	/* We know p and g are positive. The next two checks imply q > 0. */
478 	if (BN_is_negative(dsa->q)) {
479 		DSAerror(DSA_R_BAD_Q_VALUE);
480 		return 0;
481 	}
482 
483 	/* FIPS 186-4 only allows three sizes for q. */
484 	q_bits = BN_num_bits(dsa->q);
485 	if (q_bits != 160 && q_bits != 224 && q_bits != 256) {
486 		DSAerror(DSA_R_BAD_Q_VALUE);
487 		return 0;
488 	}
489 
490 	/*
491 	 * XXX - FIPS 186-4 only allows 1024, 2048, and 3072 bits for p.
492 	 * Cap the size to reduce DoS risks. Poor defaults make keys with
493 	 * incorrect p sizes >= 512 bits common, so only enforce a weak
494 	 * lower bound.
495 	 */
496 	p_bits = BN_num_bits(dsa->p);
497 	if (p_bits > OPENSSL_DSA_MAX_MODULUS_BITS) {
498 		DSAerror(DSA_R_MODULUS_TOO_LARGE);
499 		return 0;
500 	}
501 	if (p_bits < 512) {
502 		DSAerror(DSA_R_INVALID_PARAMETERS);
503 		return 0;
504 	}
505 
506 	/* The public key must be in the multiplicative group (mod p). */
507 	if (dsa->pub_key != NULL) {
508 		if (BN_cmp(dsa->pub_key, BN_value_one()) <= 0 ||
509 		    BN_cmp(dsa->pub_key, dsa->p) >= 0) {
510 			DSAerror(DSA_R_INVALID_PARAMETERS);
511 			return 0;
512 		}
513 	}
514 
515 	/* The private key must be nonzero and in GF(q). */
516 	if (dsa->priv_key != NULL) {
517 		if (BN_cmp(dsa->priv_key, BN_value_one()) < 0 ||
518 		    BN_cmp(dsa->priv_key, dsa->q) >= 0) {
519 			DSAerror(DSA_R_INVALID_PARAMETERS);
520 			return 0;
521 		}
522 	}
523 
524 	return 1;
525 }
526