2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * DSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
18 #include "internal/refcount.h"
19 #include <openssl/bn.h>
20 #include <openssl/asn1.h>
21 #include <openssl/engine.h>
22 #include <openssl/core_names.h>
23 #include "dsa_local.h"
24 #include "crypto/evp.h"
25 #include "crypto/dsa.h"
26 #include "crypto/dh.h" /* required by DSA_dup_DH() */
28 static DSA *dsa_new_intern(ENGINE *engine, OPENSSL_CTX *libctx);
32 int DSA_set_ex_data(DSA *d, int idx, void *arg)
34 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
37 void *DSA_get_ex_data(const DSA *d, int idx)
39 return CRYPTO_get_ex_data(&d->ex_data, idx);
42 # ifndef OPENSSL_NO_DH
43 DH *DSA_dup_DH(const DSA *r)
46 * DSA has p, q, g, optional pub_key, optional priv_key.
47 * DH has p, optional length, g, optional pub_key,
48 * optional priv_key, optional q.
51 BIGNUM *pub_key = NULL, *priv_key = NULL;
59 if (!ffc_params_copy(dh_get0_params(ret), &r->params))
62 if (r->pub_key != NULL) {
63 pub_key = BN_dup(r->pub_key);
66 if (r->priv_key != NULL) {
67 priv_key = BN_dup(r->priv_key);
71 if (!DH_set0_key(ret, pub_key, priv_key))
73 } else if (r->priv_key != NULL) {
74 /* Shouldn't happen */
86 # endif /* OPENSSL_NO_DH */
88 void DSA_clear_flags(DSA *d, int flags)
93 int DSA_test_flags(const DSA *d, int flags)
95 return d->flags & flags;
98 void DSA_set_flags(DSA *d, int flags)
103 ENGINE *DSA_get0_engine(DSA *d)
108 int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
111 * NB: The caller is specifically setting a method, so it's not up to us
112 * to deal with which ENGINE it comes from.
114 const DSA_METHOD *mtmp;
118 #ifndef OPENSSL_NO_ENGINE
119 ENGINE_finish(dsa->engine);
127 #endif /* FIPS_MODE */
130 const DSA_METHOD *DSA_get_method(DSA *d)
135 static DSA *dsa_new_intern(ENGINE *engine, OPENSSL_CTX *libctx)
137 DSA *ret = OPENSSL_zalloc(sizeof(*ret));
140 DSAerr(0, ERR_R_MALLOC_FAILURE);
145 ret->lock = CRYPTO_THREAD_lock_new();
146 if (ret->lock == NULL) {
147 DSAerr(0, ERR_R_MALLOC_FAILURE);
152 ret->libctx = libctx;
153 ret->meth = DSA_get_default_method();
154 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
155 ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */
157 if (!ENGINE_init(engine)) {
158 DSAerr(0, ERR_R_ENGINE_LIB);
161 ret->engine = engine;
163 ret->engine = ENGINE_get_default_DSA();
165 ret->meth = ENGINE_get_DSA(ret->engine);
166 if (ret->meth == NULL) {
167 DSAerr(0, ERR_R_ENGINE_LIB);
173 ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
176 if (!crypto_new_ex_data_ex(libctx, CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data))
180 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
181 DSAerr(0, ERR_R_INIT_FAIL);
192 DSA *DSA_new_method(ENGINE *engine)
194 return dsa_new_intern(engine, NULL);
197 DSA *dsa_new_with_ctx(OPENSSL_CTX *libctx)
199 return dsa_new_intern(NULL, libctx);
205 return dsa_new_intern(NULL, NULL);
209 void DSA_free(DSA *r)
216 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
217 REF_PRINT_COUNT("DSA", r);
220 REF_ASSERT_ISNT(i < 0);
222 if (r->meth != NULL && r->meth->finish != NULL)
224 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
225 ENGINE_finish(r->engine);
229 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
232 CRYPTO_THREAD_lock_free(r->lock);
234 ffc_params_cleanup(&r->params);
235 BN_clear_free(r->pub_key);
236 BN_clear_free(r->priv_key);
240 int DSA_up_ref(DSA *r)
244 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
247 REF_PRINT_COUNT("DSA", r);
248 REF_ASSERT_ISNT(i < 2);
249 return ((i > 1) ? 1 : 0);
252 void DSA_get0_pqg(const DSA *d,
253 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
255 ffc_params_get0_pqg(&d->params, p, q, g);
258 int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
260 /* If the fields p, q and g in d are NULL, the corresponding input
261 * parameters MUST be non-NULL.
263 if ((d->params.p == NULL && p == NULL)
264 || (d->params.q == NULL && q == NULL)
265 || (d->params.g == NULL && g == NULL))
268 ffc_params_set0_pqg(&d->params, p, q, g);
274 const BIGNUM *DSA_get0_p(const DSA *d)
279 const BIGNUM *DSA_get0_q(const DSA *d)
284 const BIGNUM *DSA_get0_g(const DSA *d)
289 const BIGNUM *DSA_get0_pub_key(const DSA *d)
294 const BIGNUM *DSA_get0_priv_key(const DSA *d)
299 void DSA_get0_key(const DSA *d,
300 const BIGNUM **pub_key, const BIGNUM **priv_key)
303 *pub_key = d->pub_key;
304 if (priv_key != NULL)
305 *priv_key = d->priv_key;
308 int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
310 /* If the field pub_key in d is NULL, the corresponding input
311 * parameters MUST be non-NULL. The priv_key field may
314 if (d->pub_key == NULL && pub_key == NULL)
317 if (pub_key != NULL) {
319 d->pub_key = pub_key;
321 if (priv_key != NULL) {
322 BN_free(d->priv_key);
323 d->priv_key = priv_key;
330 int DSA_security_bits(const DSA *d)
332 if (d->params.p != NULL && d->params.q != NULL)
333 return BN_security_bits(BN_num_bits(d->params.p),
334 BN_num_bits(d->params.q));
338 int DSA_bits(const DSA *dsa)
340 return BN_num_bits(dsa->params.p);
343 FFC_PARAMS *dsa_get0_params(DSA *dsa)
348 int dsa_ffc_params_fromdata(DSA *dsa, const OSSL_PARAM params[])
355 ffc = dsa_get0_params(dsa);
359 ret = ffc_params_fromdata(ffc, params);
365 static int dsa_paramgen_check(EVP_PKEY_CTX *ctx)
367 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
368 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
369 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
372 /* If key type not DSA return error */
373 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_DSA)
378 int EVP_PKEY_CTX_set_dsa_paramgen_type(EVP_PKEY_CTX *ctx, const char *name)
381 OSSL_PARAM params[2], *p = params;
383 if ((ret = dsa_paramgen_check(ctx)) <= 0)
386 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,
388 *p++ = OSSL_PARAM_construct_end();
390 return EVP_PKEY_CTX_set_params(ctx, params);
393 int EVP_PKEY_CTX_set_dsa_paramgen_gindex(EVP_PKEY_CTX *ctx, int gindex)
396 OSSL_PARAM params[2], *p = params;
398 if ((ret = dsa_paramgen_check(ctx)) <= 0)
401 *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX, &gindex);
402 *p++ = OSSL_PARAM_construct_end();
404 return EVP_PKEY_CTX_set_params(ctx, params);
407 int EVP_PKEY_CTX_set_dsa_paramgen_seed(EVP_PKEY_CTX *ctx,
408 const unsigned char *seed,
412 OSSL_PARAM params[2], *p = params;
414 if ((ret = dsa_paramgen_check(ctx)) <= 0)
417 *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED,
418 (void *)seed, seedlen);
419 *p++ = OSSL_PARAM_construct_end();
421 return EVP_PKEY_CTX_set_params(ctx, params);
424 int EVP_PKEY_CTX_set_dsa_paramgen_bits(EVP_PKEY_CTX *ctx, int nbits)
427 OSSL_PARAM params[2], *p = params;
430 if ((ret = dsa_paramgen_check(ctx)) <= 0)
433 #if !defined(FIPS_MODE)
434 /* TODO(3.0): Remove this eventually when no more legacy */
435 if (ctx->op.keymgmt.genctx == NULL)
436 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
437 EVP_PKEY_CTRL_DSA_PARAMGEN_BITS, nbits, NULL);
440 *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_FFC_PBITS, &bits);
441 *p++ = OSSL_PARAM_construct_end();
443 return EVP_PKEY_CTX_set_params(ctx, params);
446 int EVP_PKEY_CTX_set_dsa_paramgen_q_bits(EVP_PKEY_CTX *ctx, int qbits)
449 OSSL_PARAM params[2], *p = params;
450 size_t bits2 = qbits;
452 if ((ret = dsa_paramgen_check(ctx)) <= 0)
455 #if !defined(FIPS_MODE)
456 /* TODO(3.0): Remove this eventually when no more legacy */
457 if (ctx->op.keymgmt.genctx == NULL)
458 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
459 EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS, qbits, NULL);
462 *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_FFC_QBITS, &bits2);
463 *p++ = OSSL_PARAM_construct_end();
465 return EVP_PKEY_CTX_set_params(ctx, params);
468 int EVP_PKEY_CTX_set_dsa_paramgen_md_props(EVP_PKEY_CTX *ctx,
470 const char *md_properties)
473 OSSL_PARAM params[3], *p = params;
475 if ((ret = dsa_paramgen_check(ctx)) <= 0)
478 #if !defined(FIPS_MODE)
479 /* TODO(3.0): Remove this eventually when no more legacy */
480 if (ctx->op.keymgmt.genctx == NULL) {
481 const EVP_MD *md = EVP_get_digestbyname(md_name);
483 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
484 EVP_PKEY_CTRL_DSA_PARAMGEN_MD, 0, (void *)(md));
488 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST,
490 if (md_properties != NULL)
491 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
492 (char *)md_properties, 0);
493 *p++ = OSSL_PARAM_construct_end();
495 return EVP_PKEY_CTX_set_params(ctx, params);
498 #if !defined(FIPS_MODE)
499 int EVP_PKEY_CTX_set_dsa_paramgen_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
501 const char *md_name = (md == NULL) ? "" : EVP_MD_name(md);
503 return EVP_PKEY_CTX_set_dsa_paramgen_md_props(ctx, md_name, NULL);