xref: /openbsd-src/lib/libcrypto/dsa/dsa_lib.c (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1 /* $OpenBSD: dsa_lib.c,v 1.44 2023/08/12 06:14:36 tb 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 *dsa;
131 
132 	if ((dsa = calloc(1, sizeof(DSA))) == NULL) {
133 		DSAerror(ERR_R_MALLOC_FAILURE);
134 		goto err;
135 	}
136 
137 	dsa->meth = DSA_get_default_method();
138 	dsa->flags = dsa->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
139 	dsa->references = 1;
140 
141 #ifndef OPENSSL_NO_ENGINE
142 	if (engine) {
143 		if (!ENGINE_init(engine)) {
144 			DSAerror(ERR_R_ENGINE_LIB);
145 			goto err;
146 		}
147 		dsa->engine = engine;
148 	} else
149 		dsa->engine = ENGINE_get_default_DSA();
150 	if (dsa->engine != NULL) {
151 		if ((dsa->meth = ENGINE_get_DSA(dsa->engine)) == NULL) {
152 			DSAerror(ERR_R_ENGINE_LIB);
153 			goto err;
154 		}
155 		dsa->flags = dsa->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
156 	}
157 #endif
158 
159 	if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, dsa, &dsa->ex_data))
160 		goto err;
161 	if (dsa->meth->init != NULL && !dsa->meth->init(dsa))
162 		goto err;
163 
164 	return dsa;
165 
166  err:
167 	DSA_free(dsa);
168 
169 	return NULL;
170 }
171 LCRYPTO_ALIAS(DSA_new_method);
172 
173 void
174 DSA_free(DSA *r)
175 {
176 	int i;
177 
178 	if (r == NULL)
179 		return;
180 
181 	i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DSA);
182 	if (i > 0)
183 		return;
184 
185 	if (r->meth != NULL && r->meth->finish != NULL)
186 		r->meth->finish(r);
187 #ifndef OPENSSL_NO_ENGINE
188 	ENGINE_finish(r->engine);
189 #endif
190 
191 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
192 
193 	BN_free(r->p);
194 	BN_free(r->q);
195 	BN_free(r->g);
196 	BN_free(r->pub_key);
197 	BN_free(r->priv_key);
198 	BN_free(r->kinv);
199 	BN_free(r->r);
200 	free(r);
201 }
202 LCRYPTO_ALIAS(DSA_free);
203 
204 int
205 DSA_up_ref(DSA *r)
206 {
207 	int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DSA);
208 	return i > 1 ? 1 : 0;
209 }
210 LCRYPTO_ALIAS(DSA_up_ref);
211 
212 int
213 DSA_size(const DSA *r)
214 {
215 	DSA_SIG signature;
216 	int ret = 0;
217 
218 	signature.r = r->q;
219 	signature.s = r->q;
220 
221 	if ((ret = i2d_DSA_SIG(&signature, NULL)) < 0)
222 		ret = 0;
223 
224 	return ret;
225 }
226 LCRYPTO_ALIAS(DSA_size);
227 
228 int
229 DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
230     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
231 {
232 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, argl, argp,
233 	    new_func, dup_func, free_func);
234 }
235 LCRYPTO_ALIAS(DSA_get_ex_new_index);
236 
237 int
238 DSA_set_ex_data(DSA *d, int idx, void *arg)
239 {
240 	return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
241 }
242 LCRYPTO_ALIAS(DSA_set_ex_data);
243 
244 void *
245 DSA_get_ex_data(DSA *d, int idx)
246 {
247 	return CRYPTO_get_ex_data(&d->ex_data, idx);
248 }
249 LCRYPTO_ALIAS(DSA_get_ex_data);
250 
251 int
252 DSA_security_bits(const DSA *d)
253 {
254 	if (d->p == NULL || d->q == NULL)
255 		return -1;
256 
257 	return BN_security_bits(BN_num_bits(d->p), BN_num_bits(d->q));
258 }
259 LCRYPTO_ALIAS(DSA_security_bits);
260 
261 #ifndef OPENSSL_NO_DH
262 DH *
263 DSA_dup_DH(const DSA *r)
264 {
265 	/*
266 	 * DSA has p, q, g, optional pub_key, optional priv_key.
267 	 * DH has p, optional length, g, optional pub_key, optional priv_key,
268 	 * optional q.
269 	 */
270 	DH *ret = NULL;
271 
272 	if (r == NULL)
273 		goto err;
274 	ret = DH_new();
275 	if (ret == NULL)
276 		goto err;
277 	if (r->p != NULL)
278 		if ((ret->p = BN_dup(r->p)) == NULL)
279 			goto err;
280 	if (r->q != NULL) {
281 		ret->length = BN_num_bits(r->q);
282 		if ((ret->q = BN_dup(r->q)) == NULL)
283 			goto err;
284 	}
285 	if (r->g != NULL)
286 		if ((ret->g = BN_dup(r->g)) == NULL)
287 			goto err;
288 	if (r->pub_key != NULL)
289 		if ((ret->pub_key = BN_dup(r->pub_key)) == NULL)
290 			goto err;
291 	if (r->priv_key != NULL)
292 		if ((ret->priv_key = BN_dup(r->priv_key)) == NULL)
293 			goto err;
294 
295 	return ret;
296 
297 err:
298 	DH_free(ret);
299 	return NULL;
300 }
301 LCRYPTO_ALIAS(DSA_dup_DH);
302 #endif
303 
304 void
305 DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
306 {
307 	if (p != NULL)
308 		*p = d->p;
309 	if (q != NULL)
310 		*q = d->q;
311 	if (g != NULL)
312 		*g = d->g;
313 }
314 LCRYPTO_ALIAS(DSA_get0_pqg);
315 
316 int
317 DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
318 {
319 	if ((d->p == NULL && p == NULL) || (d->q == NULL && q == NULL) ||
320 	    (d->g == NULL && g == NULL))
321 		return 0;
322 
323 	if (p != NULL) {
324 		BN_free(d->p);
325 		d->p = p;
326 	}
327 	if (q != NULL) {
328 		BN_free(d->q);
329 		d->q = q;
330 	}
331 	if (g != NULL) {
332 		BN_free(d->g);
333 		d->g = g;
334 	}
335 
336 	return 1;
337 }
338 LCRYPTO_ALIAS(DSA_set0_pqg);
339 
340 void
341 DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key)
342 {
343 	if (pub_key != NULL)
344 		*pub_key = d->pub_key;
345 	if (priv_key != NULL)
346 		*priv_key = d->priv_key;
347 }
348 LCRYPTO_ALIAS(DSA_get0_key);
349 
350 int
351 DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
352 {
353 	if (d->pub_key == NULL && pub_key == NULL)
354 		return 0;
355 
356 	if (pub_key != NULL) {
357 		BN_free(d->pub_key);
358 		d->pub_key = pub_key;
359 	}
360 	if (priv_key != NULL) {
361 		BN_free(d->priv_key);
362 		d->priv_key = priv_key;
363 	}
364 
365 	return 1;
366 }
367 LCRYPTO_ALIAS(DSA_set0_key);
368 
369 const BIGNUM *
370 DSA_get0_p(const DSA *d)
371 {
372 	return d->p;
373 }
374 LCRYPTO_ALIAS(DSA_get0_p);
375 
376 const BIGNUM *
377 DSA_get0_q(const DSA *d)
378 {
379 	return d->q;
380 }
381 LCRYPTO_ALIAS(DSA_get0_q);
382 
383 const BIGNUM *
384 DSA_get0_g(const DSA *d)
385 {
386 	return d->g;
387 }
388 LCRYPTO_ALIAS(DSA_get0_g);
389 
390 const BIGNUM *
391 DSA_get0_pub_key(const DSA *d)
392 {
393 	return d->pub_key;
394 }
395 LCRYPTO_ALIAS(DSA_get0_pub_key);
396 
397 const BIGNUM *
398 DSA_get0_priv_key(const DSA *d)
399 {
400 	return d->priv_key;
401 }
402 LCRYPTO_ALIAS(DSA_get0_priv_key);
403 
404 void
405 DSA_clear_flags(DSA *d, int flags)
406 {
407 	d->flags &= ~flags;
408 }
409 LCRYPTO_ALIAS(DSA_clear_flags);
410 
411 int
412 DSA_test_flags(const DSA *d, int flags)
413 {
414 	return d->flags & flags;
415 }
416 LCRYPTO_ALIAS(DSA_test_flags);
417 
418 void
419 DSA_set_flags(DSA *d, int flags)
420 {
421 	d->flags |= flags;
422 }
423 LCRYPTO_ALIAS(DSA_set_flags);
424 
425 ENGINE *
426 DSA_get0_engine(DSA *d)
427 {
428 	return d->engine;
429 }
430 LCRYPTO_ALIAS(DSA_get0_engine);
431 
432 int
433 DSA_bits(const DSA *dsa)
434 {
435 	return BN_num_bits(dsa->p);
436 }
437 LCRYPTO_ALIAS(DSA_bits);
438 
439 int
440 dsa_check_key(const DSA *dsa)
441 {
442 	int p_bits, q_bits;
443 
444 	if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {
445 		DSAerror(DSA_R_MISSING_PARAMETERS);
446 		return 0;
447 	}
448 
449 	/* Checking that p and q are primes is expensive. Check they are odd. */
450 	if (!BN_is_odd(dsa->p) || !BN_is_odd(dsa->q)) {
451 		DSAerror(DSA_R_INVALID_PARAMETERS);
452 		return 0;
453 	}
454 
455 	/* FIPS 186-4: 1 < g < p. */
456 	if (BN_cmp(dsa->g, BN_value_one()) <= 0 ||
457 	    BN_cmp(dsa->g, dsa->p) >= 0) {
458 		DSAerror(DSA_R_INVALID_PARAMETERS);
459 		return 0;
460 	}
461 
462 	/* We know p and g are positive. The next two checks imply q > 0. */
463 	if (BN_is_negative(dsa->q)) {
464 		DSAerror(DSA_R_BAD_Q_VALUE);
465 		return 0;
466 	}
467 
468 	/* FIPS 186-4 only allows three sizes for q. */
469 	q_bits = BN_num_bits(dsa->q);
470 	if (q_bits != 160 && q_bits != 224 && q_bits != 256) {
471 		DSAerror(DSA_R_BAD_Q_VALUE);
472 		return 0;
473 	}
474 
475 	/*
476 	 * XXX - FIPS 186-4 only allows 1024, 2048, and 3072 bits for p.
477 	 * Cap the size to reduce DoS risks. Poor defaults make keys with
478 	 * incorrect p sizes >= 512 bits common, so only enforce a weak
479 	 * lower bound.
480 	 */
481 	p_bits = BN_num_bits(dsa->p);
482 	if (p_bits > OPENSSL_DSA_MAX_MODULUS_BITS) {
483 		DSAerror(DSA_R_MODULUS_TOO_LARGE);
484 		return 0;
485 	}
486 	if (p_bits < 512) {
487 		DSAerror(DSA_R_INVALID_PARAMETERS);
488 		return 0;
489 	}
490 
491 	/* The public key must be in the multiplicative group (mod p). */
492 	if (dsa->pub_key != NULL) {
493 		if (BN_cmp(dsa->pub_key, BN_value_one()) <= 0 ||
494 		    BN_cmp(dsa->pub_key, dsa->p) >= 0) {
495 			DSAerror(DSA_R_INVALID_PARAMETERS);
496 			return 0;
497 		}
498 	}
499 
500 	/* The private key must be nonzero and in GF(q). */
501 	if (dsa->priv_key != NULL) {
502 		if (BN_cmp(dsa->priv_key, BN_value_one()) < 0 ||
503 		    BN_cmp(dsa->priv_key, dsa->q) >= 0) {
504 			DSAerror(DSA_R_INVALID_PARAMETERS);
505 			return 0;
506 		}
507 	}
508 
509 	return 1;
510 }
511