2016-05-18 02:51:26 +08:00
|
|
|
/*
|
2021-01-28 20:54:57 +08:00
|
|
|
* Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2018-12-06 20:36:26 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:51:26 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
|
|
|
|
2020-01-30 05:23:39 +08:00
|
|
|
/*
|
|
|
|
* DSA low level APIs are deprecated for public use, but still ok for
|
|
|
|
* internal use.
|
|
|
|
*/
|
|
|
|
#include "internal/deprecated.h"
|
|
|
|
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/bn.h>
|
2016-03-19 02:30:20 +08:00
|
|
|
#include <openssl/engine.h>
|
2021-02-23 23:51:43 +08:00
|
|
|
#include "internal/cryptlib.h"
|
|
|
|
#include "internal/refcount.h"
|
2020-01-12 09:32:12 +08:00
|
|
|
#include "crypto/dsa.h"
|
2020-01-24 12:09:33 +08:00
|
|
|
#include "crypto/dh.h" /* required by DSA_dup_DH() */
|
2021-02-23 23:51:43 +08:00
|
|
|
#include "dsa_local.h"
|
2020-01-12 09:32:12 +08:00
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
static DSA *dsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx);
|
2020-02-16 11:03:46 +08:00
|
|
|
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2020-01-12 09:32:12 +08:00
|
|
|
int DSA_set_ex_data(DSA *d, int idx, void *arg)
|
|
|
|
{
|
|
|
|
return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
|
|
|
|
}
|
|
|
|
|
2020-03-23 15:30:37 +08:00
|
|
|
void *DSA_get_ex_data(const DSA *d, int idx)
|
2020-01-12 09:32:12 +08:00
|
|
|
{
|
|
|
|
return CRYPTO_get_ex_data(&d->ex_data, idx);
|
|
|
|
}
|
|
|
|
|
2020-01-24 12:09:33 +08:00
|
|
|
# ifndef OPENSSL_NO_DH
|
2020-01-12 09:32:12 +08:00
|
|
|
DH *DSA_dup_DH(const DSA *r)
|
|
|
|
{
|
|
|
|
/*
|
2020-01-24 12:09:33 +08:00
|
|
|
* DSA has p, q, g, optional pub_key, optional priv_key.
|
|
|
|
* DH has p, optional length, g, optional pub_key,
|
|
|
|
* optional priv_key, optional q.
|
2020-01-12 09:32:12 +08:00
|
|
|
*/
|
|
|
|
DH *ret = NULL;
|
2020-01-24 12:09:33 +08:00
|
|
|
BIGNUM *pub_key = NULL, *priv_key = NULL;
|
2020-01-12 09:32:12 +08:00
|
|
|
|
|
|
|
if (r == NULL)
|
|
|
|
goto err;
|
|
|
|
ret = DH_new();
|
|
|
|
if (ret == NULL)
|
|
|
|
goto err;
|
2020-01-24 12:09:33 +08:00
|
|
|
|
ffc: add _ossl to exported but internal functions
The functions updated are:
ffc_generate_private_key, ffc_named_group_from_uid,
ffc_named_group_to_uid, ffc_params_FIPS186_2_gen_verify,
ffc_params_FIPS186_2_generate, ffc_params_FIPS186_2_validate,
ffc_params_FIPS186_4_gen_verify, ffc_params_FIPS186_4_generate,
ffc_params_FIPS186_4_validate, ffc_params_cleanup, ffc_params_cmp,
ffc_params_copy, ffc_params_enable_flags, ffc_params_flags_from_name,
ffc_params_flags_to_name, ffc_params_fromdata,
ffc_params_get0_pqg, ffc_params_get_validate_params,
ffc_params_init, ffc_params_print, ffc_params_set0_j,
ffc_params_set0_pqg, ffc_params_set_flags, ffc_params_set_gindex,
ffc_params_set_h, ffc_params_set_pcounter, ffc_params_set_seed,
ffc_params_set_validate_params, ffc_params_simple_validate,
ffc_params_todata, ffc_params_validate_unverifiable_g, ffc_set_digest,
ffc_set_group_pqg, ffc_validate_private_key, ffc_validate_public_key
and ffc_validate_public_key_partial.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13041)
2020-09-30 13:07:24 +08:00
|
|
|
if (!ossl_ffc_params_copy(dh_get0_params(ret), &r->params))
|
2020-01-24 12:09:33 +08:00
|
|
|
goto err;
|
2020-01-12 09:32:12 +08:00
|
|
|
|
|
|
|
if (r->pub_key != NULL) {
|
|
|
|
pub_key = BN_dup(r->pub_key);
|
|
|
|
if (pub_key == NULL)
|
|
|
|
goto err;
|
|
|
|
if (r->priv_key != NULL) {
|
|
|
|
priv_key = BN_dup(r->priv_key);
|
|
|
|
if (priv_key == NULL)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!DH_set0_key(ret, pub_key, priv_key))
|
|
|
|
goto err;
|
|
|
|
} else if (r->priv_key != NULL) {
|
|
|
|
/* Shouldn't happen */
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
err:
|
|
|
|
BN_free(pub_key);
|
|
|
|
BN_free(priv_key);
|
|
|
|
DH_free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-01-24 12:09:33 +08:00
|
|
|
# endif /* OPENSSL_NO_DH */
|
2020-01-12 09:32:12 +08:00
|
|
|
|
|
|
|
void DSA_clear_flags(DSA *d, int flags)
|
|
|
|
{
|
|
|
|
d->flags &= ~flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DSA_test_flags(const DSA *d, int flags)
|
|
|
|
{
|
|
|
|
return d->flags & flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DSA_set_flags(DSA *d, int flags)
|
|
|
|
{
|
|
|
|
d->flags |= flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
ENGINE *DSA_get0_engine(DSA *d)
|
|
|
|
{
|
|
|
|
return d->engine;
|
|
|
|
}
|
|
|
|
|
2001-09-26 04:23:40 +08:00
|
|
|
int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* NB: The caller is specifically setting a method, so it's not up to us
|
|
|
|
* to deal with which ENGINE it comes from.
|
|
|
|
*/
|
|
|
|
const DSA_METHOD *mtmp;
|
|
|
|
mtmp = dsa->meth;
|
|
|
|
if (mtmp->finish)
|
|
|
|
mtmp->finish(dsa);
|
2003-01-31 01:39:26 +08:00
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
2016-02-26 01:09:06 +08:00
|
|
|
ENGINE_finish(dsa->engine);
|
|
|
|
dsa->engine = NULL;
|
2003-01-31 01:39:26 +08:00
|
|
|
#endif
|
2015-01-22 11:40:55 +08:00
|
|
|
dsa->meth = meth;
|
|
|
|
if (meth->init)
|
|
|
|
meth->init(dsa);
|
|
|
|
return 1;
|
|
|
|
}
|
2020-04-14 04:34:56 +08:00
|
|
|
#endif /* FIPS_MODULE */
|
2020-01-12 09:32:12 +08:00
|
|
|
|
1999-08-23 01:57:38 +08:00
|
|
|
|
2016-03-31 00:18:55 +08:00
|
|
|
const DSA_METHOD *DSA_get_method(DSA *d)
|
|
|
|
{
|
|
|
|
return d->meth;
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
static DSA *dsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2016-03-09 03:11:48 +08:00
|
|
|
DSA *ret = OPENSSL_zalloc(sizeof(*ret));
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
if (ret == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
|
2016-03-04 23:43:46 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2016-03-09 03:11:48 +08:00
|
|
|
|
|
|
|
ret->references = 1;
|
|
|
|
ret->lock = CRYPTO_THREAD_lock_new();
|
|
|
|
if (ret->lock == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
|
2016-03-09 03:11:48 +08:00
|
|
|
OPENSSL_free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-02-16 11:03:46 +08:00
|
|
|
ret->libctx = libctx;
|
2015-01-22 11:40:55 +08:00
|
|
|
ret->meth = DSA_get_default_method();
|
2020-04-14 04:34:56 +08:00
|
|
|
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
|
2016-03-09 03:11:48 +08:00
|
|
|
ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */
|
2015-01-22 11:40:55 +08:00
|
|
|
if (engine) {
|
|
|
|
if (!ENGINE_init(engine)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_DSA, ERR_R_ENGINE_LIB);
|
2016-03-09 03:11:48 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
ret->engine = engine;
|
|
|
|
} else
|
|
|
|
ret->engine = ENGINE_get_default_DSA();
|
|
|
|
if (ret->engine) {
|
|
|
|
ret->meth = ENGINE_get_DSA(ret->engine);
|
2016-02-26 01:09:06 +08:00
|
|
|
if (ret->meth == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_DSA, ERR_R_ENGINE_LIB);
|
2016-03-09 03:11:48 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
}
|
2003-01-31 01:39:26 +08:00
|
|
|
#endif
|
2001-06-24 07:07:34 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
|
2016-03-04 23:43:46 +08:00
|
|
|
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE
|
2020-01-12 09:32:12 +08:00
|
|
|
if (!crypto_new_ex_data_ex(libctx, CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data))
|
2016-03-09 03:11:48 +08:00
|
|
|
goto err;
|
2020-01-14 09:32:42 +08:00
|
|
|
#endif
|
2016-03-04 23:43:46 +08:00
|
|
|
|
|
|
|
if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_DSA, ERR_R_INIT_FAIL);
|
2018-09-05 17:08:12 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
2016-03-04 23:43:46 +08:00
|
|
|
return ret;
|
2018-09-05 17:08:12 +08:00
|
|
|
|
|
|
|
err:
|
|
|
|
DSA_free(ret);
|
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2020-01-12 09:32:12 +08:00
|
|
|
DSA *DSA_new_method(ENGINE *engine)
|
|
|
|
{
|
2020-02-16 11:03:46 +08:00
|
|
|
return dsa_new_intern(engine, NULL);
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
DSA *dsa_new_with_ctx(OSSL_LIB_CTX *libctx)
|
2020-02-16 11:03:46 +08:00
|
|
|
{
|
|
|
|
return dsa_new_intern(NULL, libctx);
|
2020-01-12 09:32:12 +08:00
|
|
|
}
|
|
|
|
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE
|
2020-01-14 09:32:42 +08:00
|
|
|
DSA *DSA_new(void)
|
2020-01-12 09:32:12 +08:00
|
|
|
{
|
2020-02-16 11:03:46 +08:00
|
|
|
return dsa_new_intern(NULL, NULL);
|
2020-01-12 09:32:12 +08:00
|
|
|
}
|
2020-02-16 11:03:46 +08:00
|
|
|
#endif
|
2020-01-12 09:32:12 +08:00
|
|
|
|
1999-04-20 05:31:43 +08:00
|
|
|
void DSA_free(DSA *r)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
int i;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (r == NULL)
|
|
|
|
return;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2016-08-27 22:01:08 +08:00
|
|
|
CRYPTO_DOWN_REF(&r->references, &i, r->lock);
|
2016-01-31 01:04:25 +08:00
|
|
|
REF_PRINT_COUNT("DSA", r);
|
2015-01-22 11:40:55 +08:00
|
|
|
if (i > 0)
|
|
|
|
return;
|
2016-01-31 01:04:25 +08:00
|
|
|
REF_ASSERT_ISNT(i < 0);
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2018-09-05 16:58:55 +08:00
|
|
|
if (r->meth != NULL && r->meth->finish != NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
r->meth->finish(r);
|
2020-04-14 04:34:56 +08:00
|
|
|
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
|
2016-02-28 23:01:41 +08:00
|
|
|
ENGINE_finish(r->engine);
|
2003-01-31 01:39:26 +08:00
|
|
|
#endif
|
1999-08-23 01:57:38 +08:00
|
|
|
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE
|
2015-01-22 11:40:55 +08:00
|
|
|
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
|
2020-01-14 09:32:42 +08:00
|
|
|
#endif
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-03-04 23:43:46 +08:00
|
|
|
CRYPTO_THREAD_lock_free(r->lock);
|
|
|
|
|
ffc: add _ossl to exported but internal functions
The functions updated are:
ffc_generate_private_key, ffc_named_group_from_uid,
ffc_named_group_to_uid, ffc_params_FIPS186_2_gen_verify,
ffc_params_FIPS186_2_generate, ffc_params_FIPS186_2_validate,
ffc_params_FIPS186_4_gen_verify, ffc_params_FIPS186_4_generate,
ffc_params_FIPS186_4_validate, ffc_params_cleanup, ffc_params_cmp,
ffc_params_copy, ffc_params_enable_flags, ffc_params_flags_from_name,
ffc_params_flags_to_name, ffc_params_fromdata,
ffc_params_get0_pqg, ffc_params_get_validate_params,
ffc_params_init, ffc_params_print, ffc_params_set0_j,
ffc_params_set0_pqg, ffc_params_set_flags, ffc_params_set_gindex,
ffc_params_set_h, ffc_params_set_pcounter, ffc_params_set_seed,
ffc_params_set_validate_params, ffc_params_simple_validate,
ffc_params_todata, ffc_params_validate_unverifiable_g, ffc_set_digest,
ffc_set_group_pqg, ffc_validate_private_key, ffc_validate_public_key
and ffc_validate_public_key_partial.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13041)
2020-09-30 13:07:24 +08:00
|
|
|
ossl_ffc_params_cleanup(&r->params);
|
2015-05-01 09:37:06 +08:00
|
|
|
BN_clear_free(r->pub_key);
|
|
|
|
BN_clear_free(r->priv_key);
|
2015-01-22 11:40:55 +08:00
|
|
|
OPENSSL_free(r);
|
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2001-09-03 21:40:07 +08:00
|
|
|
int DSA_up_ref(DSA *r)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2016-03-04 23:43:46 +08:00
|
|
|
int i;
|
|
|
|
|
2016-08-27 22:01:08 +08:00
|
|
|
if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
|
2016-03-04 23:43:46 +08:00
|
|
|
return 0;
|
2016-01-31 01:04:25 +08:00
|
|
|
|
|
|
|
REF_PRINT_COUNT("DSA", r);
|
|
|
|
REF_ASSERT_ISNT(i < 2);
|
2015-01-22 11:40:55 +08:00
|
|
|
return ((i > 1) ? 1 : 0);
|
|
|
|
}
|
2001-08-26 01:24:21 +08:00
|
|
|
|
2020-12-11 18:01:09 +08:00
|
|
|
void ossl_dsa_set0_libctx(DSA *d, OSSL_LIB_CTX *libctx)
|
|
|
|
{
|
|
|
|
d->libctx = libctx;
|
|
|
|
}
|
|
|
|
|
2016-06-14 21:48:16 +08:00
|
|
|
void DSA_get0_pqg(const DSA *d,
|
|
|
|
const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
|
2016-03-30 22:21:39 +08:00
|
|
|
{
|
ffc: add _ossl to exported but internal functions
The functions updated are:
ffc_generate_private_key, ffc_named_group_from_uid,
ffc_named_group_to_uid, ffc_params_FIPS186_2_gen_verify,
ffc_params_FIPS186_2_generate, ffc_params_FIPS186_2_validate,
ffc_params_FIPS186_4_gen_verify, ffc_params_FIPS186_4_generate,
ffc_params_FIPS186_4_validate, ffc_params_cleanup, ffc_params_cmp,
ffc_params_copy, ffc_params_enable_flags, ffc_params_flags_from_name,
ffc_params_flags_to_name, ffc_params_fromdata,
ffc_params_get0_pqg, ffc_params_get_validate_params,
ffc_params_init, ffc_params_print, ffc_params_set0_j,
ffc_params_set0_pqg, ffc_params_set_flags, ffc_params_set_gindex,
ffc_params_set_h, ffc_params_set_pcounter, ffc_params_set_seed,
ffc_params_set_validate_params, ffc_params_simple_validate,
ffc_params_todata, ffc_params_validate_unverifiable_g, ffc_set_digest,
ffc_set_group_pqg, ffc_validate_private_key, ffc_validate_public_key
and ffc_validate_public_key_partial.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13041)
2020-09-30 13:07:24 +08:00
|
|
|
ossl_ffc_params_get0_pqg(&d->params, p, q, g);
|
2016-03-30 22:21:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
|
|
|
|
{
|
2016-06-14 21:48:16 +08:00
|
|
|
/* If the fields p, q and g in d are NULL, the corresponding input
|
RSA, DSA, DH: Allow some given input to be NULL on already initialised keys
The diverse {RSA,DSA,DH}_set0_* functions are made to allow some
parameters to be NULL IF the corresponding numbers in the given key
structure have already been previously initialised. Specifically,
this allows the addition of private components to be added to a key
that already has the public half, approximately like this:
RSA_get0_key(rsa, NULL, &e, NULL);
RSA_get0_factors(rsa, &p, &q);
/* calculate new d */
RSA_set0_key(rsa, NULL, NULL, d);
Reviewed-by: Matt Caswell <matt@openssl.org>
2016-04-26 02:28:54 +08:00
|
|
|
* parameters MUST be non-NULL.
|
|
|
|
*/
|
2020-01-24 12:09:33 +08:00
|
|
|
if ((d->params.p == NULL && p == NULL)
|
|
|
|
|| (d->params.q == NULL && q == NULL)
|
|
|
|
|| (d->params.g == NULL && g == NULL))
|
2016-03-30 22:21:39 +08:00
|
|
|
return 0;
|
RSA, DSA, DH: Allow some given input to be NULL on already initialised keys
The diverse {RSA,DSA,DH}_set0_* functions are made to allow some
parameters to be NULL IF the corresponding numbers in the given key
structure have already been previously initialised. Specifically,
this allows the addition of private components to be added to a key
that already has the public half, approximately like this:
RSA_get0_key(rsa, NULL, &e, NULL);
RSA_get0_factors(rsa, &p, &q);
/* calculate new d */
RSA_set0_key(rsa, NULL, NULL, d);
Reviewed-by: Matt Caswell <matt@openssl.org>
2016-04-26 02:28:54 +08:00
|
|
|
|
ffc: add _ossl to exported but internal functions
The functions updated are:
ffc_generate_private_key, ffc_named_group_from_uid,
ffc_named_group_to_uid, ffc_params_FIPS186_2_gen_verify,
ffc_params_FIPS186_2_generate, ffc_params_FIPS186_2_validate,
ffc_params_FIPS186_4_gen_verify, ffc_params_FIPS186_4_generate,
ffc_params_FIPS186_4_validate, ffc_params_cleanup, ffc_params_cmp,
ffc_params_copy, ffc_params_enable_flags, ffc_params_flags_from_name,
ffc_params_flags_to_name, ffc_params_fromdata,
ffc_params_get0_pqg, ffc_params_get_validate_params,
ffc_params_init, ffc_params_print, ffc_params_set0_j,
ffc_params_set0_pqg, ffc_params_set_flags, ffc_params_set_gindex,
ffc_params_set_h, ffc_params_set_pcounter, ffc_params_set_seed,
ffc_params_set_validate_params, ffc_params_simple_validate,
ffc_params_todata, ffc_params_validate_unverifiable_g, ffc_set_digest,
ffc_set_group_pqg, ffc_validate_private_key, ffc_validate_public_key
and ffc_validate_public_key_partial.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13041)
2020-09-30 13:07:24 +08:00
|
|
|
ossl_ffc_params_set0_pqg(&d->params, p, q, g);
|
2019-08-30 20:33:37 +08:00
|
|
|
d->dirty_cnt++;
|
2016-03-30 22:21:39 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
Redesign the KEYMGMT libcrypto <-> provider interface - the basics
The KEYMGMT libcrypto <-> provider interface currently makes a few
assumptions:
1. provider side domain parameters and key data isn't mutable. In
other words, as soon as a key has been created in any (loaded,
imported data, ...), it's set in stone.
2. provider side domain parameters can be strictly separated from the
key data.
This does work for the most part, but there are places where that's a
bit too rigid for the functionality that the EVP_PKEY API delivers.
Key data needs to be mutable to allow the flexibility that functions
like EVP_PKEY_copy_parameters promise, as well as to provide the
combinations of data that an EVP_PKEY is generally assumed to be able
to hold:
- domain parameters only
- public key only
- public key + private key
- domain parameters + public key
- domain parameters + public key + private key
To remedy all this, we:
1. let go of the distinction between domain parameters and key
material proper in the libcrypto <-> provider interface.
As a consequence, functions that still need it gain a selection
argument, which is a set of bits that indicate what parts of the
key object are to be considered in a specific call. This allows
a reduction of very similar functions into one.
2. Rework the libcrypto <-> provider interface so provider side key
objects are created and destructed with a separate function, and
get their data filled and extracted in through import and export.
(future work will see other key object constructors and other
functions to fill them with data)
Fixes #10979
squash! Redesign the KEYMGMT libcrypto <-> provider interface - the basics
Remedy 1 needs a rewrite:
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/11006)
2020-02-03 01:56:07 +08:00
|
|
|
const BIGNUM *DSA_get0_p(const DSA *d)
|
|
|
|
{
|
|
|
|
return d->params.p;
|
|
|
|
}
|
|
|
|
|
|
|
|
const BIGNUM *DSA_get0_q(const DSA *d)
|
|
|
|
{
|
|
|
|
return d->params.q;
|
|
|
|
}
|
|
|
|
|
|
|
|
const BIGNUM *DSA_get0_g(const DSA *d)
|
|
|
|
{
|
|
|
|
return d->params.g;
|
|
|
|
}
|
|
|
|
|
|
|
|
const BIGNUM *DSA_get0_pub_key(const DSA *d)
|
|
|
|
{
|
|
|
|
return d->pub_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
const BIGNUM *DSA_get0_priv_key(const DSA *d)
|
|
|
|
{
|
|
|
|
return d->priv_key;
|
|
|
|
}
|
|
|
|
|
2016-06-14 21:48:16 +08:00
|
|
|
void DSA_get0_key(const DSA *d,
|
|
|
|
const BIGNUM **pub_key, const BIGNUM **priv_key)
|
2016-03-30 22:21:39 +08:00
|
|
|
{
|
2016-03-31 00:18:55 +08:00
|
|
|
if (pub_key != NULL)
|
|
|
|
*pub_key = d->pub_key;
|
|
|
|
if (priv_key != NULL)
|
|
|
|
*priv_key = d->priv_key;
|
2016-03-30 22:21:39 +08:00
|
|
|
}
|
|
|
|
|
2016-03-31 00:18:55 +08:00
|
|
|
int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
|
2016-03-30 22:21:39 +08:00
|
|
|
{
|
RSA, DSA, DH: Allow some given input to be NULL on already initialised keys
The diverse {RSA,DSA,DH}_set0_* functions are made to allow some
parameters to be NULL IF the corresponding numbers in the given key
structure have already been previously initialised. Specifically,
this allows the addition of private components to be added to a key
that already has the public half, approximately like this:
RSA_get0_key(rsa, NULL, &e, NULL);
RSA_get0_factors(rsa, &p, &q);
/* calculate new d */
RSA_set0_key(rsa, NULL, NULL, d);
Reviewed-by: Matt Caswell <matt@openssl.org>
2016-04-26 02:28:54 +08:00
|
|
|
if (pub_key != NULL) {
|
|
|
|
BN_free(d->pub_key);
|
|
|
|
d->pub_key = pub_key;
|
|
|
|
}
|
|
|
|
if (priv_key != NULL) {
|
|
|
|
BN_free(d->priv_key);
|
|
|
|
d->priv_key = priv_key;
|
|
|
|
}
|
2019-08-30 20:33:37 +08:00
|
|
|
d->dirty_cnt++;
|
2016-03-30 22:21:39 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-01-13 19:28:05 +08:00
|
|
|
int DSA_security_bits(const DSA *d)
|
|
|
|
{
|
2020-01-24 12:09:33 +08:00
|
|
|
if (d->params.p != NULL && d->params.q != NULL)
|
|
|
|
return BN_security_bits(BN_num_bits(d->params.p),
|
|
|
|
BN_num_bits(d->params.q));
|
2020-01-13 19:28:05 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DSA_bits(const DSA *dsa)
|
|
|
|
{
|
2020-12-04 15:55:19 +08:00
|
|
|
if (dsa->params.p != NULL)
|
|
|
|
return BN_num_bits(dsa->params.p);
|
|
|
|
return -1;
|
2020-01-13 19:28:05 +08:00
|
|
|
}
|
2020-02-06 16:53:15 +08:00
|
|
|
|
|
|
|
FFC_PARAMS *dsa_get0_params(DSA *dsa)
|
|
|
|
{
|
|
|
|
return &dsa->params;
|
|
|
|
}
|
2020-04-15 19:02:52 +08:00
|
|
|
|
|
|
|
int dsa_ffc_params_fromdata(DSA *dsa, const OSSL_PARAM params[])
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
FFC_PARAMS *ffc;
|
|
|
|
|
|
|
|
if (dsa == NULL)
|
|
|
|
return 0;
|
|
|
|
ffc = dsa_get0_params(dsa);
|
|
|
|
if (ffc == NULL)
|
|
|
|
return 0;
|
|
|
|
|
ffc: add _ossl to exported but internal functions
The functions updated are:
ffc_generate_private_key, ffc_named_group_from_uid,
ffc_named_group_to_uid, ffc_params_FIPS186_2_gen_verify,
ffc_params_FIPS186_2_generate, ffc_params_FIPS186_2_validate,
ffc_params_FIPS186_4_gen_verify, ffc_params_FIPS186_4_generate,
ffc_params_FIPS186_4_validate, ffc_params_cleanup, ffc_params_cmp,
ffc_params_copy, ffc_params_enable_flags, ffc_params_flags_from_name,
ffc_params_flags_to_name, ffc_params_fromdata,
ffc_params_get0_pqg, ffc_params_get_validate_params,
ffc_params_init, ffc_params_print, ffc_params_set0_j,
ffc_params_set0_pqg, ffc_params_set_flags, ffc_params_set_gindex,
ffc_params_set_h, ffc_params_set_pcounter, ffc_params_set_seed,
ffc_params_set_validate_params, ffc_params_simple_validate,
ffc_params_todata, ffc_params_validate_unverifiable_g, ffc_set_digest,
ffc_set_group_pqg, ffc_validate_private_key, ffc_validate_public_key
and ffc_validate_public_key_partial.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13041)
2020-09-30 13:07:24 +08:00
|
|
|
ret = ossl_ffc_params_fromdata(ffc, params);
|
2020-04-15 19:02:52 +08:00
|
|
|
if (ret)
|
|
|
|
dsa->dirty_cnt++;
|
|
|
|
return ret;
|
|
|
|
}
|