Adding in curl and openssl repos

This commit is contained in:
2025-08-14 12:09:30 -04:00
parent af2117b574
commit 0ace93e303
21174 changed files with 3607720 additions and 2 deletions

View File

@@ -0,0 +1,43 @@
# We make separate GOAL variables for each algorithm, to make it easy to
# switch each to the Legacy provider when needed.
$DH_GOAL=../../libdefault.a ../../libfips.a
$DSA_GOAL=../../libdefault.a ../../libfips.a
$EC_GOAL=../../libdefault.a ../../libfips.a
$ECX_GOAL=../../libdefault.a ../../libfips.a
$KDF_GOAL=../../libdefault.a ../../libfips.a
$MAC_GOAL=../../libdefault.a ../../libfips.a
$RSA_GOAL=../../libdefault.a ../../libfips.a
IF[{- !$disabled{dh} -}]
SOURCE[$DH_GOAL]=dh_kmgmt.c
ENDIF
IF[{- !$disabled{dsa} -}]
SOURCE[$DSA_GOAL]=dsa_kmgmt.c
ENDIF
IF[{- !$disabled{ec} -}]
SOURCE[$EC_GOAL]=ec_kmgmt.c
ENDIF
IF[{- !$disabled{asm} -}]
$ECDEF_s390x=S390X_EC_ASM
# Now that we have defined all the arch specific variables, use the
# appropriate one, and define the appropriate macros
IF[$ECASM_{- $target{asm_arch} -}]
$ECDEF=$ECDEF_{- $target{asm_arch} -}
ENDIF
ENDIF
IF[{- !$disabled{ec} -}]
IF[{- !$disabled{ecx} -}]
SOURCE[$ECX_GOAL]=ecx_kmgmt.c
DEFINE[$ECX_GOAL]=$ECDEF
ENDIF
ENDIF
SOURCE[$RSA_GOAL]=rsa_kmgmt.c
SOURCE[$KDF_GOAL]=kdf_legacy_kmgmt.c
SOURCE[$MAC_GOAL]=mac_legacy_kmgmt.c

View File

@@ -0,0 +1,901 @@
/*
* Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* 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
*/
/*
* DH low level APIs are deprecated for public use, but still ok for
* internal use.
*/
#include "internal/deprecated.h"
#include "internal/common.h"
#include <string.h> /* strcmp */
#include <openssl/core_dispatch.h>
#include <openssl/core_names.h>
#include <openssl/bn.h>
#include <openssl/err.h>
#include "prov/implementations.h"
#include "prov/providercommon.h"
#include "prov/provider_ctx.h"
#include "crypto/dh.h"
#include "internal/sizes.h"
static OSSL_FUNC_keymgmt_new_fn dh_newdata;
static OSSL_FUNC_keymgmt_free_fn dh_freedata;
static OSSL_FUNC_keymgmt_gen_init_fn dh_gen_init;
static OSSL_FUNC_keymgmt_gen_init_fn dhx_gen_init;
static OSSL_FUNC_keymgmt_gen_set_template_fn dh_gen_set_template;
static OSSL_FUNC_keymgmt_gen_set_params_fn dh_gen_set_params;
static OSSL_FUNC_keymgmt_gen_settable_params_fn dh_gen_settable_params;
static OSSL_FUNC_keymgmt_gen_fn dh_gen;
static OSSL_FUNC_keymgmt_gen_cleanup_fn dh_gen_cleanup;
static OSSL_FUNC_keymgmt_load_fn dh_load;
static OSSL_FUNC_keymgmt_get_params_fn dh_get_params;
static OSSL_FUNC_keymgmt_gettable_params_fn dh_gettable_params;
static OSSL_FUNC_keymgmt_set_params_fn dh_set_params;
static OSSL_FUNC_keymgmt_settable_params_fn dh_settable_params;
static OSSL_FUNC_keymgmt_has_fn dh_has;
static OSSL_FUNC_keymgmt_match_fn dh_match;
static OSSL_FUNC_keymgmt_validate_fn dh_validate;
static OSSL_FUNC_keymgmt_import_fn dh_import;
static OSSL_FUNC_keymgmt_import_types_fn dh_import_types;
static OSSL_FUNC_keymgmt_export_fn dh_export;
static OSSL_FUNC_keymgmt_export_types_fn dh_export_types;
static OSSL_FUNC_keymgmt_dup_fn dh_dup;
#define DH_POSSIBLE_SELECTIONS \
(OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
struct dh_gen_ctx {
OSSL_LIB_CTX *libctx;
FFC_PARAMS *ffc_params;
int selection;
/* All these parameters are used for parameter generation only */
/* If there is a group name then the remaining parameters are not needed */
int group_nid;
size_t pbits;
size_t qbits;
unsigned char *seed; /* optional FIPS186-4 param for testing */
size_t seedlen;
int gindex; /* optional FIPS186-4 generator index (ignored if -1) */
int gen_type; /* see dhtype2id */
int generator; /* Used by DH_PARAMGEN_TYPE_GENERATOR in non fips mode only */
int pcounter;
int hindex;
int priv_len;
char *mdname;
char *mdprops;
OSSL_CALLBACK *cb;
void *cbarg;
int dh_type;
};
static int dh_gen_type_name2id_w_default(const char *name, int type)
{
if (strcmp(name, "default") == 0) {
#ifdef FIPS_MODULE
if (type == DH_FLAG_TYPE_DHX)
return DH_PARAMGEN_TYPE_FIPS_186_4;
return DH_PARAMGEN_TYPE_GROUP;
#else
if (type == DH_FLAG_TYPE_DHX)
return DH_PARAMGEN_TYPE_FIPS_186_2;
return DH_PARAMGEN_TYPE_GENERATOR;
#endif
}
return ossl_dh_gen_type_name2id(name, type);
}
static void *dh_newdata(void *provctx)
{
DH *dh = NULL;
if (ossl_prov_is_running()) {
dh = ossl_dh_new_ex(PROV_LIBCTX_OF(provctx));
if (dh != NULL) {
DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
DH_set_flags(dh, DH_FLAG_TYPE_DH);
}
}
return dh;
}
static void *dhx_newdata(void *provctx)
{
DH *dh = NULL;
dh = ossl_dh_new_ex(PROV_LIBCTX_OF(provctx));
if (dh != NULL) {
DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
DH_set_flags(dh, DH_FLAG_TYPE_DHX);
}
return dh;
}
static void dh_freedata(void *keydata)
{
DH_free(keydata);
}
static int dh_has(const void *keydata, int selection)
{
const DH *dh = keydata;
int ok = 1;
if (!ossl_prov_is_running() || dh == NULL)
return 0;
if ((selection & DH_POSSIBLE_SELECTIONS) == 0)
return 1; /* the selection is not missing */
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
ok = ok && (DH_get0_pub_key(dh) != NULL);
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
ok = ok && (DH_get0_priv_key(dh) != NULL);
if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
ok = ok && (DH_get0_p(dh) != NULL && DH_get0_g(dh) != NULL);
return ok;
}
static int dh_match(const void *keydata1, const void *keydata2, int selection)
{
const DH *dh1 = keydata1;
const DH *dh2 = keydata2;
int ok = 1;
if (!ossl_prov_is_running())
return 0;
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
int key_checked = 0;
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
const BIGNUM *pa = DH_get0_pub_key(dh1);
const BIGNUM *pb = DH_get0_pub_key(dh2);
if (pa != NULL && pb != NULL) {
ok = ok && BN_cmp(pa, pb) == 0;
key_checked = 1;
}
}
if (!key_checked
&& (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
const BIGNUM *pa = DH_get0_priv_key(dh1);
const BIGNUM *pb = DH_get0_priv_key(dh2);
if (pa != NULL && pb != NULL) {
ok = ok && BN_cmp(pa, pb) == 0;
key_checked = 1;
}
}
ok = ok && key_checked;
}
if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
FFC_PARAMS *dhparams1 = ossl_dh_get0_params((DH *)dh1);
FFC_PARAMS *dhparams2 = ossl_dh_get0_params((DH *)dh2);
ok = ok && ossl_ffc_params_cmp(dhparams1, dhparams2, 1);
}
return ok;
}
static int dh_import(void *keydata, int selection, const OSSL_PARAM params[])
{
DH *dh = keydata;
int ok = 1;
if (!ossl_prov_is_running() || dh == NULL)
return 0;
if ((selection & DH_POSSIBLE_SELECTIONS) == 0)
return 0;
/* a key without parameters is meaningless */
ok = ok && ossl_dh_params_fromdata(dh, params);
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
int include_private =
selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
ok = ok && ossl_dh_key_fromdata(dh, params, include_private);
}
return ok;
}
static int dh_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
void *cbarg)
{
DH *dh = keydata;
OSSL_PARAM_BLD *tmpl = NULL;
OSSL_PARAM *params = NULL;
int ok = 1;
if (!ossl_prov_is_running() || dh == NULL)
return 0;
if ((selection & DH_POSSIBLE_SELECTIONS) == 0)
return 0;
tmpl = OSSL_PARAM_BLD_new();
if (tmpl == NULL)
return 0;
if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
ok = ok && ossl_dh_params_todata(dh, tmpl, NULL);
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
int include_private =
selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
ok = ok && ossl_dh_key_todata(dh, tmpl, NULL, include_private);
}
if (!ok || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
ok = 0;
goto err;
}
ok = param_cb(params, cbarg);
OSSL_PARAM_free(params);
err:
OSSL_PARAM_BLD_free(tmpl);
return ok;
}
/* IMEXPORT = IMPORT + EXPORT */
# define DH_IMEXPORTABLE_PARAMETERS \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_P, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_Q, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_G, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_COFACTOR, NULL, 0), \
OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL), \
OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL), \
OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL), \
OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_PRIV_LEN, NULL), \
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0), \
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0)
# define DH_IMEXPORTABLE_PUBLIC_KEY \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
# define DH_IMEXPORTABLE_PRIVATE_KEY \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
static const OSSL_PARAM dh_all_types[] = {
DH_IMEXPORTABLE_PARAMETERS,
DH_IMEXPORTABLE_PUBLIC_KEY,
DH_IMEXPORTABLE_PRIVATE_KEY,
OSSL_PARAM_END
};
static const OSSL_PARAM dh_parameter_types[] = {
DH_IMEXPORTABLE_PARAMETERS,
OSSL_PARAM_END
};
static const OSSL_PARAM dh_key_types[] = {
DH_IMEXPORTABLE_PUBLIC_KEY,
DH_IMEXPORTABLE_PRIVATE_KEY,
OSSL_PARAM_END
};
static const OSSL_PARAM *dh_types[] = {
NULL, /* Index 0 = none of them */
dh_parameter_types, /* Index 1 = parameter types */
dh_key_types, /* Index 2 = key types */
dh_all_types /* Index 3 = 1 + 2 */
};
static const OSSL_PARAM *dh_imexport_types(int selection)
{
int type_select = 0;
if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
type_select += 1;
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
type_select += 2;
return dh_types[type_select];
}
static const OSSL_PARAM *dh_import_types(int selection)
{
return dh_imexport_types(selection);
}
static const OSSL_PARAM *dh_export_types(int selection)
{
return dh_imexport_types(selection);
}
static ossl_inline int dh_get_params(void *key, OSSL_PARAM params[])
{
DH *dh = key;
OSSL_PARAM *p;
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
&& !OSSL_PARAM_set_int(p, DH_bits(dh)))
return 0;
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
&& !OSSL_PARAM_set_int(p, DH_security_bits(dh)))
return 0;
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
&& !OSSL_PARAM_set_int(p, DH_size(dh)))
return 0;
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY)) != NULL) {
if (p->data_type != OSSL_PARAM_OCTET_STRING)
return 0;
p->return_size = ossl_dh_key2buf(dh, (unsigned char **)&p->data,
p->data_size, 0);
if (p->return_size == 0)
return 0;
}
return ossl_dh_params_todata(dh, NULL, params)
&& ossl_dh_key_todata(dh, NULL, params, 1);
}
static const OSSL_PARAM dh_params[] = {
OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
DH_IMEXPORTABLE_PARAMETERS,
DH_IMEXPORTABLE_PUBLIC_KEY,
DH_IMEXPORTABLE_PRIVATE_KEY,
OSSL_PARAM_END
};
static const OSSL_PARAM *dh_gettable_params(void *provctx)
{
return dh_params;
}
static const OSSL_PARAM dh_known_settable_params[] = {
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
OSSL_PARAM_END
};
static const OSSL_PARAM *dh_settable_params(void *provctx)
{
return dh_known_settable_params;
}
static int dh_set_params(void *key, const OSSL_PARAM params[])
{
DH *dh = key;
const OSSL_PARAM *p;
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY);
if (p != NULL
&& (p->data_type != OSSL_PARAM_OCTET_STRING
|| !ossl_dh_buf2key(dh, p->data, p->data_size)))
return 0;
return 1;
}
static int dh_validate_public(const DH *dh, int checktype)
{
const BIGNUM *pub_key = NULL;
int res = 0;
DH_get0_key(dh, &pub_key, NULL);
if (pub_key == NULL)
return 0;
/*
* The partial test is only valid for named group's with q = (p - 1) / 2
* but for that case it is also fully sufficient to check the key validity.
*/
if (ossl_dh_is_named_safe_prime_group(dh))
return ossl_dh_check_pub_key_partial(dh, pub_key, &res);
return DH_check_pub_key_ex(dh, pub_key);
}
static int dh_validate_private(const DH *dh)
{
int status = 0;
const BIGNUM *priv_key = NULL;
DH_get0_key(dh, NULL, &priv_key);
if (priv_key == NULL)
return 0;
return ossl_dh_check_priv_key(dh, priv_key, &status);
}
static int dh_validate(const void *keydata, int selection, int checktype)
{
const DH *dh = keydata;
int ok = 1;
if (!ossl_prov_is_running())
return 0;
if ((selection & DH_POSSIBLE_SELECTIONS) == 0)
return 1; /* nothing to validate */
if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
/*
* Both of these functions check parameters. DH_check_params_ex()
* performs a lightweight check (e.g. it does not check that p is a
* safe prime)
*/
if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK)
ok = ok && DH_check_params_ex(dh);
else
ok = ok && DH_check_ex(dh);
}
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
ok = ok && dh_validate_public(dh, checktype);
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
ok = ok && dh_validate_private(dh);
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR)
== OSSL_KEYMGMT_SELECT_KEYPAIR)
ok = ok && ossl_dh_check_pairwise(dh);
return ok;
}
static void *dh_gen_init_base(void *provctx, int selection,
const OSSL_PARAM params[], int type)
{
OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
struct dh_gen_ctx *gctx = NULL;
if (!ossl_prov_is_running())
return NULL;
if ((selection & (OSSL_KEYMGMT_SELECT_KEYPAIR
| OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)) == 0)
return NULL;
if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
gctx->selection = selection;
gctx->libctx = libctx;
gctx->pbits = 2048;
gctx->qbits = 224;
gctx->mdname = NULL;
#ifdef FIPS_MODULE
gctx->gen_type = (type == DH_FLAG_TYPE_DHX)
? DH_PARAMGEN_TYPE_FIPS_186_4
: DH_PARAMGEN_TYPE_GROUP;
#else
gctx->gen_type = (type == DH_FLAG_TYPE_DHX)
? DH_PARAMGEN_TYPE_FIPS_186_2
: DH_PARAMGEN_TYPE_GENERATOR;
#endif
gctx->gindex = -1;
gctx->hindex = 0;
gctx->pcounter = -1;
gctx->generator = DH_GENERATOR_2;
gctx->dh_type = type;
}
if (!dh_gen_set_params(gctx, params)) {
OPENSSL_free(gctx);
gctx = NULL;
}
return gctx;
}
static void *dh_gen_init(void *provctx, int selection,
const OSSL_PARAM params[])
{
return dh_gen_init_base(provctx, selection, params, DH_FLAG_TYPE_DH);
}
static void *dhx_gen_init(void *provctx, int selection,
const OSSL_PARAM params[])
{
return dh_gen_init_base(provctx, selection, params, DH_FLAG_TYPE_DHX);
}
static int dh_gen_set_template(void *genctx, void *templ)
{
struct dh_gen_ctx *gctx = genctx;
DH *dh = templ;
if (!ossl_prov_is_running() || gctx == NULL || dh == NULL)
return 0;
gctx->ffc_params = ossl_dh_get0_params(dh);
return 1;
}
static int dh_set_gen_seed(struct dh_gen_ctx *gctx, unsigned char *seed,
size_t seedlen)
{
OPENSSL_clear_free(gctx->seed, gctx->seedlen);
gctx->seed = NULL;
gctx->seedlen = 0;
if (seed != NULL && seedlen > 0) {
gctx->seed = OPENSSL_memdup(seed, seedlen);
if (gctx->seed == NULL)
return 0;
gctx->seedlen = seedlen;
}
return 1;
}
static int dh_gen_common_set_params(void *genctx, const OSSL_PARAM params[])
{
struct dh_gen_ctx *gctx = genctx;
const OSSL_PARAM *p;
int gen_type = -1;
if (gctx == NULL)
return 0;
if (params == NULL)
return 1;
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_TYPE);
if (p != NULL) {
if (p->data_type != OSSL_PARAM_UTF8_STRING
|| ((gen_type =
dh_gen_type_name2id_w_default(p->data, gctx->dh_type)) == -1)) {
ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
return 0;
}
if (gen_type != -1)
gctx->gen_type = gen_type;
}
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
if (p != NULL) {
const DH_NAMED_GROUP *group = NULL;
if (p->data_type != OSSL_PARAM_UTF8_STRING
|| p->data == NULL
|| (group = ossl_ffc_name_to_dh_named_group(p->data)) == NULL
|| ((gctx->group_nid =
ossl_ffc_named_group_get_uid(group)) == NID_undef)) {
ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
return 0;
}
}
if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PBITS)) != NULL
&& !OSSL_PARAM_get_size_t(p, &gctx->pbits))
return 0;
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PRIV_LEN);
if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->priv_len))
return 0;
return 1;
}
static const OSSL_PARAM *dh_gen_settable_params(ossl_unused void *genctx,
ossl_unused void *provctx)
{
static const OSSL_PARAM dh_gen_settable[] = {
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_PRIV_LEN, NULL),
OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_PBITS, NULL),
OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_GENERATOR, NULL),
OSSL_PARAM_END
};
return dh_gen_settable;
}
static const OSSL_PARAM *dhx_gen_settable_params(ossl_unused void *genctx,
ossl_unused void *provctx)
{
static const OSSL_PARAM dhx_gen_settable[] = {
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_PRIV_LEN, NULL),
OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_PBITS, NULL),
OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_QBITS, NULL),
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS, NULL, 0),
OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL),
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0),
OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL),
OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL),
OSSL_PARAM_END
};
return dhx_gen_settable;
}
static int dhx_gen_set_params(void *genctx, const OSSL_PARAM params[])
{
struct dh_gen_ctx *gctx = genctx;
const OSSL_PARAM *p;
if (!dh_gen_common_set_params(genctx, params))
return 0;
/* Parameters related to fips186-4 and fips186-2 */
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX);
if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->gindex))
return 0;
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER);
if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->pcounter))
return 0;
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H);
if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->hindex))
return 0;
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED);
if (p != NULL
&& (p->data_type != OSSL_PARAM_OCTET_STRING
|| !dh_set_gen_seed(gctx, p->data, p->data_size)))
return 0;
if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_QBITS)) != NULL
&& !OSSL_PARAM_get_size_t(p, &gctx->qbits))
return 0;
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST);
if (p != NULL) {
if (p->data_type != OSSL_PARAM_UTF8_STRING)
return 0;
OPENSSL_free(gctx->mdname);
gctx->mdname = OPENSSL_strdup(p->data);
if (gctx->mdname == NULL)
return 0;
}
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS);
if (p != NULL) {
if (p->data_type != OSSL_PARAM_UTF8_STRING)
return 0;
OPENSSL_free(gctx->mdprops);
gctx->mdprops = OPENSSL_strdup(p->data);
if (gctx->mdprops == NULL)
return 0;
}
/* Parameters that are not allowed for DHX */
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_GENERATOR);
if (p != NULL) {
ERR_raise(ERR_LIB_PROV, ERR_R_UNSUPPORTED);
return 0;
}
return 1;
}
static int dh_gen_set_params(void *genctx, const OSSL_PARAM params[])
{
struct dh_gen_ctx *gctx = genctx;
const OSSL_PARAM *p;
if (!dh_gen_common_set_params(genctx, params))
return 0;
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_GENERATOR);
if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->generator))
return 0;
/* Parameters that are not allowed for DH */
if (OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX) != NULL
|| OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER) != NULL
|| OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H) != NULL
|| OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED) != NULL
|| OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_QBITS) != NULL
|| OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST) != NULL
|| OSSL_PARAM_locate_const(params,
OSSL_PKEY_PARAM_FFC_DIGEST_PROPS) != NULL) {
ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
return 0;
}
return 1;
}
static int dh_gencb(int p, int n, BN_GENCB *cb)
{
struct dh_gen_ctx *gctx = BN_GENCB_get_arg(cb);
OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p);
params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n);
return gctx->cb(params, gctx->cbarg);
}
static void *dh_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
{
int ret = 0;
struct dh_gen_ctx *gctx = genctx;
DH *dh = NULL;
BN_GENCB *gencb = NULL;
FFC_PARAMS *ffc;
if (!ossl_prov_is_running() || gctx == NULL)
return NULL;
/*
* If a group name is selected then the type is group regardless of what
* the user selected. This overrides rather than errors for backwards
* compatibility.
*/
if (gctx->group_nid != NID_undef)
gctx->gen_type = DH_PARAMGEN_TYPE_GROUP;
/*
* Do a bounds check on context gen_type. Must be in range:
* DH_PARAMGEN_TYPE_GENERATOR <= gen_type <= DH_PARAMGEN_TYPE_GROUP
* Noted here as this needs to be adjusted if a new group type is
* added.
*/
if (!ossl_assert((gctx->gen_type >= DH_PARAMGEN_TYPE_GENERATOR)
&& (gctx->gen_type <= DH_PARAMGEN_TYPE_GROUP))) {
ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,
"gen_type set to unsupported value %d", gctx->gen_type);
return NULL;
}
/* For parameter generation - If there is a group name just create it */
if (gctx->gen_type == DH_PARAMGEN_TYPE_GROUP
&& gctx->ffc_params == NULL) {
/* Select a named group if there is not one already */
if (gctx->group_nid == NID_undef)
gctx->group_nid = ossl_dh_get_named_group_uid_from_size(gctx->pbits);
if (gctx->group_nid == NID_undef)
return NULL;
dh = ossl_dh_new_by_nid_ex(gctx->libctx, gctx->group_nid);
if (dh == NULL)
return NULL;
ffc = ossl_dh_get0_params(dh);
} else {
dh = ossl_dh_new_ex(gctx->libctx);
if (dh == NULL)
return NULL;
ffc = ossl_dh_get0_params(dh);
/* Copy the template value if one was passed */
if (gctx->ffc_params != NULL
&& !ossl_ffc_params_copy(ffc, gctx->ffc_params))
goto end;
if (!ossl_ffc_params_set_seed(ffc, gctx->seed, gctx->seedlen))
goto end;
if (gctx->gindex != -1) {
ossl_ffc_params_set_gindex(ffc, gctx->gindex);
if (gctx->pcounter != -1)
ossl_ffc_params_set_pcounter(ffc, gctx->pcounter);
} else if (gctx->hindex != 0) {
ossl_ffc_params_set_h(ffc, gctx->hindex);
}
if (gctx->mdname != NULL)
ossl_ffc_set_digest(ffc, gctx->mdname, gctx->mdprops);
gctx->cb = osslcb;
gctx->cbarg = cbarg;
gencb = BN_GENCB_new();
if (gencb != NULL)
BN_GENCB_set(gencb, dh_gencb, genctx);
if ((gctx->selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
/*
* NOTE: The old safe prime generator code is not used in fips mode,
* (i.e internally it ignores the generator and chooses a named
* group based on pbits.
*/
if (gctx->gen_type == DH_PARAMGEN_TYPE_GENERATOR)
ret = DH_generate_parameters_ex(dh, gctx->pbits,
gctx->generator, gencb);
else
ret = ossl_dh_generate_ffc_parameters(dh, gctx->gen_type,
gctx->pbits, gctx->qbits,
gencb);
if (ret <= 0)
goto end;
}
}
if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
if (ffc->p == NULL || ffc->g == NULL)
goto end;
if (gctx->priv_len > 0)
DH_set_length(dh, (long)gctx->priv_len);
ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_LEGACY,
gctx->gen_type == DH_PARAMGEN_TYPE_FIPS_186_2);
if (DH_generate_key(dh) <= 0)
goto end;
}
DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
DH_set_flags(dh, gctx->dh_type);
ret = 1;
end:
if (ret <= 0) {
DH_free(dh);
dh = NULL;
}
BN_GENCB_free(gencb);
return dh;
}
static void dh_gen_cleanup(void *genctx)
{
struct dh_gen_ctx *gctx = genctx;
if (gctx == NULL)
return;
OPENSSL_free(gctx->mdname);
OPENSSL_free(gctx->mdprops);
OPENSSL_clear_free(gctx->seed, gctx->seedlen);
OPENSSL_free(gctx);
}
static void *dh_load(const void *reference, size_t reference_sz)
{
DH *dh = NULL;
if (ossl_prov_is_running() && reference_sz == sizeof(dh)) {
/* The contents of the reference is the address to our object */
dh = *(DH **)reference;
/* We grabbed, so we detach it */
*(DH **)reference = NULL;
return dh;
}
return NULL;
}
static void *dh_dup(const void *keydata_from, int selection)
{
if (ossl_prov_is_running())
return ossl_dh_dup(keydata_from, selection);
return NULL;
}
const OSSL_DISPATCH ossl_dh_keymgmt_functions[] = {
{ OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dh_newdata },
{ OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dh_gen_init },
{ OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, (void (*)(void))dh_gen_set_template },
{ OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))dh_gen_set_params },
{ OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
(void (*)(void))dh_gen_settable_params },
{ OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))dh_gen },
{ OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))dh_gen_cleanup },
{ OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))dh_load },
{ OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dh_freedata },
{ OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dh_get_params },
{ OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dh_gettable_params },
{ OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))dh_set_params },
{ OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))dh_settable_params },
{ OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dh_has },
{ OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dh_match },
{ OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dh_validate },
{ OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dh_import },
{ OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dh_import_types },
{ OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dh_export },
{ OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dh_export_types },
{ OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))dh_dup },
OSSL_DISPATCH_END
};
/* For any DH key, we use the "DH" algorithms regardless of sub-type. */
static const char *dhx_query_operation_name(int operation_id)
{
return "DH";
}
const OSSL_DISPATCH ossl_dhx_keymgmt_functions[] = {
{ OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dhx_newdata },
{ OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dhx_gen_init },
{ OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, (void (*)(void))dh_gen_set_template },
{ OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))dhx_gen_set_params },
{ OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
(void (*)(void))dhx_gen_settable_params },
{ OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))dh_gen },
{ OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))dh_gen_cleanup },
{ OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))dh_load },
{ OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dh_freedata },
{ OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dh_get_params },
{ OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dh_gettable_params },
{ OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))dh_set_params },
{ OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))dh_settable_params },
{ OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dh_has },
{ OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dh_match },
{ OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dh_validate },
{ OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dh_import },
{ OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dh_import_types },
{ OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dh_export },
{ OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dh_export_types },
{ OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
(void (*)(void))dhx_query_operation_name },
{ OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))dh_dup },
OSSL_DISPATCH_END
};

View File

@@ -0,0 +1,748 @@
/*
* Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* 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
*/
/*
* DSA low level APIs are deprecated for public use, but still ok for
* internal use.
*/
#include "internal/deprecated.h"
#include <openssl/core_dispatch.h>
#include <openssl/core_names.h>
#include <openssl/bn.h>
#include <openssl/err.h>
#include "prov/securitycheck.h"
#include "prov/providercommon.h"
#include "prov/implementations.h"
#include "prov/provider_ctx.h"
#include "crypto/dsa.h"
#include "internal/sizes.h"
#include "internal/nelem.h"
#include "internal/param_build_set.h"
static OSSL_FUNC_keymgmt_new_fn dsa_newdata;
static OSSL_FUNC_keymgmt_free_fn dsa_freedata;
static OSSL_FUNC_keymgmt_gen_init_fn dsa_gen_init;
static OSSL_FUNC_keymgmt_gen_set_template_fn dsa_gen_set_template;
static OSSL_FUNC_keymgmt_gen_set_params_fn dsa_gen_set_params;
static OSSL_FUNC_keymgmt_gen_settable_params_fn dsa_gen_settable_params;
static OSSL_FUNC_keymgmt_gen_get_params_fn dsa_gen_get_params;
static OSSL_FUNC_keymgmt_gen_gettable_params_fn dsa_gen_gettable_params;
static OSSL_FUNC_keymgmt_gen_fn dsa_gen;
static OSSL_FUNC_keymgmt_gen_cleanup_fn dsa_gen_cleanup;
static OSSL_FUNC_keymgmt_load_fn dsa_load;
static OSSL_FUNC_keymgmt_get_params_fn dsa_get_params;
static OSSL_FUNC_keymgmt_gettable_params_fn dsa_gettable_params;
static OSSL_FUNC_keymgmt_has_fn dsa_has;
static OSSL_FUNC_keymgmt_match_fn dsa_match;
static OSSL_FUNC_keymgmt_validate_fn dsa_validate;
static OSSL_FUNC_keymgmt_import_fn dsa_import;
static OSSL_FUNC_keymgmt_import_types_fn dsa_import_types;
static OSSL_FUNC_keymgmt_export_fn dsa_export;
static OSSL_FUNC_keymgmt_export_types_fn dsa_export_types;
static OSSL_FUNC_keymgmt_dup_fn dsa_dup;
#define DSA_DEFAULT_MD "SHA256"
#define DSA_POSSIBLE_SELECTIONS \
(OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
struct dsa_gen_ctx {
OSSL_LIB_CTX *libctx;
FFC_PARAMS *ffc_params;
int selection;
/* All these parameters are used for parameter generation only */
size_t pbits;
size_t qbits;
unsigned char *seed; /* optional FIPS186-4 param for testing */
size_t seedlen;
int gindex; /* optional FIPS186-4 generator index (ignored if -1) */
int gen_type; /* DSA_PARAMGEN_TYPE_FIPS_186_2 or DSA_PARAMGEN_TYPE_FIPS_186_4 */
int pcounter;
int hindex;
char *mdname;
char *mdprops;
OSSL_CALLBACK *cb;
void *cbarg;
OSSL_FIPS_IND_DECLARE
};
typedef struct dh_name2id_st{
const char *name;
int id;
} DSA_GENTYPE_NAME2ID;
static const DSA_GENTYPE_NAME2ID dsatype2id[] = {
#ifdef FIPS_MODULE
{ "default", DSA_PARAMGEN_TYPE_FIPS_186_4 },
#else
{ "default", DSA_PARAMGEN_TYPE_FIPS_DEFAULT },
#endif
{ "fips186_4", DSA_PARAMGEN_TYPE_FIPS_186_4 },
{ "fips186_2", DSA_PARAMGEN_TYPE_FIPS_186_2 },
};
static int dsa_gen_type_name2id(const char *name)
{
size_t i;
for (i = 0; i < OSSL_NELEM(dsatype2id); ++i) {
if (OPENSSL_strcasecmp(dsatype2id[i].name, name) == 0)
return dsatype2id[i].id;
}
return -1;
}
static int dsa_key_todata(DSA *dsa, OSSL_PARAM_BLD *bld, OSSL_PARAM params[],
int include_private)
{
const BIGNUM *priv = NULL, *pub = NULL;
if (dsa == NULL)
return 0;
DSA_get0_key(dsa, &pub, &priv);
if (include_private
&& priv != NULL
&& !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PRIV_KEY, priv))
return 0;
if (pub != NULL
&& !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PUB_KEY, pub))
return 0;
return 1;
}
static void *dsa_newdata(void *provctx)
{
if (!ossl_prov_is_running())
return NULL;
return ossl_dsa_new(PROV_LIBCTX_OF(provctx));
}
static void dsa_freedata(void *keydata)
{
DSA_free(keydata);
}
static int dsa_has(const void *keydata, int selection)
{
const DSA *dsa = keydata;
int ok = 1;
if (!ossl_prov_is_running() || dsa == NULL)
return 0;
if ((selection & DSA_POSSIBLE_SELECTIONS) == 0)
return 1; /* the selection is not missing */
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
ok = ok && (DSA_get0_pub_key(dsa) != NULL);
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
ok = ok && (DSA_get0_priv_key(dsa) != NULL);
if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
ok = ok && (DSA_get0_p(dsa) != NULL && DSA_get0_g(dsa) != NULL);
return ok;
}
static int dsa_match(const void *keydata1, const void *keydata2, int selection)
{
const DSA *dsa1 = keydata1;
const DSA *dsa2 = keydata2;
int ok = 1;
if (!ossl_prov_is_running())
return 0;
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
int key_checked = 0;
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
const BIGNUM *pa = DSA_get0_pub_key(dsa1);
const BIGNUM *pb = DSA_get0_pub_key(dsa2);
if (pa != NULL && pb != NULL) {
ok = ok && BN_cmp(pa, pb) == 0;
key_checked = 1;
}
}
if (!key_checked
&& (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
const BIGNUM *pa = DSA_get0_priv_key(dsa1);
const BIGNUM *pb = DSA_get0_priv_key(dsa2);
if (pa != NULL && pb != NULL) {
ok = ok && BN_cmp(pa, pb) == 0;
key_checked = 1;
}
}
ok = ok && key_checked;
}
if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
FFC_PARAMS *dsaparams1 = ossl_dsa_get0_params((DSA *)dsa1);
FFC_PARAMS *dsaparams2 = ossl_dsa_get0_params((DSA *)dsa2);
ok = ok && ossl_ffc_params_cmp(dsaparams1, dsaparams2, 1);
}
return ok;
}
static int dsa_import(void *keydata, int selection, const OSSL_PARAM params[])
{
DSA *dsa = keydata;
int ok = 1;
if (!ossl_prov_is_running() || dsa == NULL)
return 0;
if ((selection & DSA_POSSIBLE_SELECTIONS) == 0)
return 0;
/* a key without parameters is meaningless */
ok = ok && ossl_dsa_ffc_params_fromdata(dsa, params);
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
int include_private =
selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
ok = ok && ossl_dsa_key_fromdata(dsa, params, include_private);
}
return ok;
}
static int dsa_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
void *cbarg)
{
DSA *dsa = keydata;
OSSL_PARAM_BLD *tmpl;
OSSL_PARAM *params = NULL;
int ok = 1;
if (!ossl_prov_is_running() || dsa == NULL)
return 0;
if ((selection & DSA_POSSIBLE_SELECTIONS) == 0)
return 0;
tmpl = OSSL_PARAM_BLD_new();
if (tmpl == NULL)
return 0;
if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
ok = ok && ossl_ffc_params_todata(ossl_dsa_get0_params(dsa), tmpl, NULL);
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
int include_private =
selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
ok = ok && dsa_key_todata(dsa, tmpl, NULL, include_private);
}
if (!ok || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
ok = 0;
goto err;
}
ok = param_cb(params, cbarg);
OSSL_PARAM_free(params);
err:
OSSL_PARAM_BLD_free(tmpl);
return ok;
}
/* IMEXPORT = IMPORT + EXPORT */
# define DSA_IMEXPORTABLE_PARAMETERS \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_P, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_Q, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_G, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_COFACTOR, NULL, 0), \
OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL), \
OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL), \
OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL), \
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0)
# define DSA_IMEXPORTABLE_PUBLIC_KEY \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
# define DSA_IMEXPORTABLE_PRIVATE_KEY \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
static const OSSL_PARAM dsa_all_types[] = {
DSA_IMEXPORTABLE_PARAMETERS,
DSA_IMEXPORTABLE_PUBLIC_KEY,
DSA_IMEXPORTABLE_PRIVATE_KEY,
OSSL_PARAM_END
};
static const OSSL_PARAM dsa_parameter_types[] = {
DSA_IMEXPORTABLE_PARAMETERS,
OSSL_PARAM_END
};
static const OSSL_PARAM dsa_key_types[] = {
DSA_IMEXPORTABLE_PUBLIC_KEY,
DSA_IMEXPORTABLE_PRIVATE_KEY,
OSSL_PARAM_END
};
static const OSSL_PARAM *dsa_types[] = {
NULL, /* Index 0 = none of them */
dsa_parameter_types, /* Index 1 = parameter types */
dsa_key_types, /* Index 2 = key types */
dsa_all_types /* Index 3 = 1 + 2 */
};
static const OSSL_PARAM *dsa_imexport_types(int selection)
{
int type_select = 0;
if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
type_select += 1;
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
type_select += 2;
return dsa_types[type_select];
}
static const OSSL_PARAM *dsa_import_types(int selection)
{
return dsa_imexport_types(selection);
}
static const OSSL_PARAM *dsa_export_types(int selection)
{
return dsa_imexport_types(selection);
}
static ossl_inline int dsa_get_params(void *key, OSSL_PARAM params[])
{
DSA *dsa = key;
OSSL_PARAM *p;
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
&& !OSSL_PARAM_set_int(p, DSA_bits(dsa)))
return 0;
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
&& !OSSL_PARAM_set_int(p, DSA_security_bits(dsa)))
return 0;
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
&& !OSSL_PARAM_set_int(p, DSA_size(dsa)))
return 0;
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
&& !OSSL_PARAM_set_utf8_string(p, DSA_DEFAULT_MD))
return 0;
return ossl_ffc_params_todata(ossl_dsa_get0_params(dsa), NULL, params)
&& dsa_key_todata(dsa, NULL, params, 1);
}
static const OSSL_PARAM dsa_params[] = {
OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
DSA_IMEXPORTABLE_PARAMETERS,
DSA_IMEXPORTABLE_PUBLIC_KEY,
DSA_IMEXPORTABLE_PRIVATE_KEY,
OSSL_PARAM_END
};
static const OSSL_PARAM *dsa_gettable_params(void *provctx)
{
return dsa_params;
}
static int dsa_validate_domparams(const DSA *dsa, int checktype)
{
int status = 0;
return ossl_dsa_check_params(dsa, checktype, &status);
}
static int dsa_validate_public(const DSA *dsa)
{
int status = 0;
const BIGNUM *pub_key = NULL;
DSA_get0_key(dsa, &pub_key, NULL);
if (pub_key == NULL)
return 0;
return ossl_dsa_check_pub_key(dsa, pub_key, &status);
}
static int dsa_validate_private(const DSA *dsa)
{
int status = 0;
const BIGNUM *priv_key = NULL;
DSA_get0_key(dsa, NULL, &priv_key);
if (priv_key == NULL)
return 0;
return ossl_dsa_check_priv_key(dsa, priv_key, &status);
}
static int dsa_validate(const void *keydata, int selection, int checktype)
{
const DSA *dsa = keydata;
int ok = 1;
if (!ossl_prov_is_running())
return 0;
if ((selection & DSA_POSSIBLE_SELECTIONS) == 0)
return 1; /* nothing to validate */
if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
ok = ok && dsa_validate_domparams(dsa, checktype);
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
ok = ok && dsa_validate_public(dsa);
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
ok = ok && dsa_validate_private(dsa);
/* If the whole key is selected, we do a pairwise validation */
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR)
== OSSL_KEYMGMT_SELECT_KEYPAIR)
ok = ok && ossl_dsa_check_pairwise(dsa);
return ok;
}
static void *dsa_gen_init(void *provctx, int selection,
const OSSL_PARAM params[])
{
OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
struct dsa_gen_ctx *gctx = NULL;
if (!ossl_prov_is_running() || (selection & DSA_POSSIBLE_SELECTIONS) == 0)
return NULL;
if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
gctx->selection = selection;
gctx->libctx = libctx;
gctx->pbits = 2048;
gctx->qbits = 224;
#ifdef FIPS_MODULE
gctx->gen_type = DSA_PARAMGEN_TYPE_FIPS_186_4;
#else
gctx->gen_type = DSA_PARAMGEN_TYPE_FIPS_DEFAULT;
#endif
gctx->gindex = -1;
gctx->pcounter = -1;
gctx->hindex = 0;
OSSL_FIPS_IND_INIT(gctx)
}
if (!dsa_gen_set_params(gctx, params)) {
dsa_gen_cleanup(gctx);
gctx = NULL;
}
return gctx;
}
static int dsa_gen_set_template(void *genctx, void *templ)
{
struct dsa_gen_ctx *gctx = genctx;
DSA *dsa = templ;
if (!ossl_prov_is_running() || gctx == NULL || dsa == NULL)
return 0;
gctx->ffc_params = ossl_dsa_get0_params(dsa);
return 1;
}
static int dsa_set_gen_seed(struct dsa_gen_ctx *gctx, unsigned char *seed,
size_t seedlen)
{
OPENSSL_clear_free(gctx->seed, gctx->seedlen);
gctx->seed = NULL;
gctx->seedlen = 0;
if (seed != NULL && seedlen > 0) {
gctx->seed = OPENSSL_memdup(seed, seedlen);
if (gctx->seed == NULL)
return 0;
gctx->seedlen = seedlen;
}
return 1;
}
static int dsa_gen_set_params(void *genctx, const OSSL_PARAM params[])
{
struct dsa_gen_ctx *gctx = genctx;
const OSSL_PARAM *p;
int gen_type = -1;
if (gctx == NULL)
return 0;
if (params == NULL)
return 1;
if (!OSSL_FIPS_IND_SET_CTX_PARAM(gctx, OSSL_FIPS_IND_SETTABLE0, params,
OSSL_PKEY_PARAM_FIPS_SIGN_CHECK))
return 0;
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_TYPE);
if (p != NULL) {
if (p->data_type != OSSL_PARAM_UTF8_STRING
|| ((gen_type = dsa_gen_type_name2id(p->data)) == -1)) {
ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
return 0;
}
/*
* Only assign context gen_type if it was set by dsa_gen_type_name2id
* must be in range:
* DSA_PARAMGEN_TYPE_FIPS_186_4 <= gen_type <= DSA_PARAMGEN_TYPE_FIPS_DEFAULT
*/
if (gen_type != -1)
gctx->gen_type = gen_type;
}
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX);
if (p != NULL
&& !OSSL_PARAM_get_int(p, &gctx->gindex))
return 0;
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER);
if (p != NULL
&& !OSSL_PARAM_get_int(p, &gctx->pcounter))
return 0;
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H);
if (p != NULL
&& !OSSL_PARAM_get_int(p, &gctx->hindex))
return 0;
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED);
if (p != NULL
&& (p->data_type != OSSL_PARAM_OCTET_STRING
|| !dsa_set_gen_seed(gctx, p->data, p->data_size)))
return 0;
if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PBITS)) != NULL
&& !OSSL_PARAM_get_size_t(p, &gctx->pbits))
return 0;
if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_QBITS)) != NULL
&& !OSSL_PARAM_get_size_t(p, &gctx->qbits))
return 0;
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST);
if (p != NULL) {
if (p->data_type != OSSL_PARAM_UTF8_STRING)
return 0;
OPENSSL_free(gctx->mdname);
gctx->mdname = OPENSSL_strdup(p->data);
if (gctx->mdname == NULL)
return 0;
}
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS);
if (p != NULL) {
if (p->data_type != OSSL_PARAM_UTF8_STRING)
return 0;
OPENSSL_free(gctx->mdprops);
gctx->mdprops = OPENSSL_strdup(p->data);
if (gctx->mdprops == NULL)
return 0;
}
return 1;
}
static const OSSL_PARAM *dsa_gen_settable_params(ossl_unused void *genctx,
ossl_unused void *provctx)
{
static OSSL_PARAM settable[] = {
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE, NULL, 0),
OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_PBITS, NULL),
OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_QBITS, NULL),
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS, NULL, 0),
OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL),
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0),
OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL),
OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL),
OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_PKEY_PARAM_FIPS_SIGN_CHECK)
OSSL_PARAM_END
};
return settable;
}
static int dsa_gen_get_params(void *genctx, OSSL_PARAM *params)
{
struct dsa_gen_ctx *gctx = genctx;
if (gctx == NULL)
return 0;
if (params == NULL)
return 1;
if (!OSSL_FIPS_IND_GET_CTX_PARAM(gctx, params))
return 0;
return 1;
}
static const OSSL_PARAM *dsa_gen_gettable_params(ossl_unused void *ctx,
ossl_unused void *provctx)
{
static const OSSL_PARAM dsa_gen_gettable_params_table[] = {
OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
OSSL_PARAM_END
};
return dsa_gen_gettable_params_table;
}
static int dsa_gencb(int p, int n, BN_GENCB *cb)
{
struct dsa_gen_ctx *gctx = BN_GENCB_get_arg(cb);
OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p);
params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n);
return gctx->cb(params, gctx->cbarg);
}
static void *dsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
{
struct dsa_gen_ctx *gctx = genctx;
DSA *dsa = NULL;
BN_GENCB *gencb = NULL;
int ret = 0;
FFC_PARAMS *ffc;
if (!ossl_prov_is_running() || gctx == NULL)
return NULL;
#ifdef FIPS_MODULE
/*
* DSA signing is not approved in FIPS 140-3, so there is no
* need for DSA keygen either.
*/
if (!OSSL_FIPS_IND_ON_UNAPPROVED(gctx, OSSL_FIPS_IND_SETTABLE0,
gctx->libctx, "DSA", "Keygen",
ossl_fips_config_dsa_sign_disallowed))
return 0;
#endif
dsa = ossl_dsa_new(gctx->libctx);
if (dsa == NULL)
return NULL;
if (gctx->gen_type == DSA_PARAMGEN_TYPE_FIPS_DEFAULT)
gctx->gen_type = (gctx->pbits >= 2048 ? DSA_PARAMGEN_TYPE_FIPS_186_4 :
DSA_PARAMGEN_TYPE_FIPS_186_2);
/*
* Do a bounds check on context gen_type. Must be in range:
* DSA_PARAMGEN_TYPE_FIPS_186_4 <= gen_type <= DSA_PARAMGEN_TYPE_FIPS_DEFAULT
* Noted here as this needs to be adjusted if a new type is
* added.
*/
if (!ossl_assert((gctx->gen_type >= DSA_PARAMGEN_TYPE_FIPS_186_4)
&& (gctx->gen_type <= DSA_PARAMGEN_TYPE_FIPS_DEFAULT))) {
ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,
"gen_type set to unsupported value %d", gctx->gen_type);
goto end;
}
gctx->cb = osslcb;
gctx->cbarg = cbarg;
gencb = BN_GENCB_new();
if (gencb != NULL)
BN_GENCB_set(gencb, dsa_gencb, genctx);
ffc = ossl_dsa_get0_params(dsa);
/* Copy the template value if one was passed */
if (gctx->ffc_params != NULL
&& !ossl_ffc_params_copy(ffc, gctx->ffc_params))
goto end;
if (gctx->seed != NULL
&& !ossl_ffc_params_set_seed(ffc, gctx->seed, gctx->seedlen))
goto end;
if (gctx->gindex != -1) {
ossl_ffc_params_set_gindex(ffc, gctx->gindex);
if (gctx->pcounter != -1)
ossl_ffc_params_set_pcounter(ffc, gctx->pcounter);
} else if (gctx->hindex != 0) {
ossl_ffc_params_set_h(ffc, gctx->hindex);
}
if (gctx->mdname != NULL)
ossl_ffc_set_digest(ffc, gctx->mdname, gctx->mdprops);
if ((gctx->selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
if (ossl_dsa_generate_ffc_parameters(dsa, gctx->gen_type,
gctx->pbits, gctx->qbits,
gencb) <= 0)
goto end;
}
ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_LEGACY,
gctx->gen_type == DSA_PARAMGEN_TYPE_FIPS_186_2);
if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
if (ffc->p == NULL
|| ffc->q == NULL
|| ffc->g == NULL)
goto end;
if (DSA_generate_key(dsa) <= 0)
goto end;
}
ret = 1;
end:
if (ret <= 0) {
DSA_free(dsa);
dsa = NULL;
}
BN_GENCB_free(gencb);
return dsa;
}
static void dsa_gen_cleanup(void *genctx)
{
struct dsa_gen_ctx *gctx = genctx;
if (gctx == NULL)
return;
OPENSSL_free(gctx->mdname);
OPENSSL_free(gctx->mdprops);
OPENSSL_clear_free(gctx->seed, gctx->seedlen);
OPENSSL_free(gctx);
}
static void *dsa_load(const void *reference, size_t reference_sz)
{
DSA *dsa = NULL;
if (ossl_prov_is_running() && reference_sz == sizeof(dsa)) {
/* The contents of the reference is the address to our object */
dsa = *(DSA **)reference;
/* We grabbed, so we detach it */
*(DSA **)reference = NULL;
return dsa;
}
return NULL;
}
static void *dsa_dup(const void *keydata_from, int selection)
{
if (ossl_prov_is_running())
return ossl_dsa_dup(keydata_from, selection);
return NULL;
}
const OSSL_DISPATCH ossl_dsa_keymgmt_functions[] = {
{ OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dsa_newdata },
{ OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dsa_gen_init },
{ OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, (void (*)(void))dsa_gen_set_template },
{ OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))dsa_gen_set_params },
{ OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
(void (*)(void))dsa_gen_settable_params },
{ OSSL_FUNC_KEYMGMT_GEN_GET_PARAMS, (void (*)(void))dsa_gen_get_params },
{ OSSL_FUNC_KEYMGMT_GEN_GETTABLE_PARAMS,
(void (*)(void))dsa_gen_gettable_params },
{ OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))dsa_gen },
{ OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))dsa_gen_cleanup },
{ OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))dsa_load },
{ OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dsa_freedata },
{ OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dsa_get_params },
{ OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dsa_gettable_params },
{ OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dsa_has },
{ OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dsa_match },
{ OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dsa_validate },
{ OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dsa_import },
{ OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dsa_import_types },
{ OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dsa_export },
{ OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dsa_export_types },
{ OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))dsa_dup },
OSSL_DISPATCH_END
};

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,109 @@
/*
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* 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
*/
/*
* This file is meant to be included from ec_kmgmt.c
*/
static const OSSL_PARAM ec_private_key_types[] = {
EC_IMEXPORTABLE_PRIVATE_KEY,
OSSL_PARAM_END
};
static const OSSL_PARAM ec_public_key_types[] = {
EC_IMEXPORTABLE_PUBLIC_KEY,
OSSL_PARAM_END
};
static const OSSL_PARAM ec_key_types[] = {
EC_IMEXPORTABLE_PRIVATE_KEY,
EC_IMEXPORTABLE_PUBLIC_KEY,
OSSL_PARAM_END
};
static const OSSL_PARAM ec_dom_parameters_types[] = {
EC_IMEXPORTABLE_DOM_PARAMETERS,
OSSL_PARAM_END
};
static const OSSL_PARAM ec_5_types[] = {
EC_IMEXPORTABLE_PRIVATE_KEY,
EC_IMEXPORTABLE_DOM_PARAMETERS,
OSSL_PARAM_END
};
static const OSSL_PARAM ec_6_types[] = {
EC_IMEXPORTABLE_PUBLIC_KEY,
EC_IMEXPORTABLE_DOM_PARAMETERS,
OSSL_PARAM_END
};
static const OSSL_PARAM ec_key_domp_types[] = {
EC_IMEXPORTABLE_PRIVATE_KEY,
EC_IMEXPORTABLE_PUBLIC_KEY,
EC_IMEXPORTABLE_DOM_PARAMETERS,
OSSL_PARAM_END
};
static const OSSL_PARAM ec_other_parameters_types[] = {
EC_IMEXPORTABLE_OTHER_PARAMETERS,
OSSL_PARAM_END
};
static const OSSL_PARAM ec_9_types[] = {
EC_IMEXPORTABLE_PRIVATE_KEY,
EC_IMEXPORTABLE_OTHER_PARAMETERS,
OSSL_PARAM_END
};
static const OSSL_PARAM ec_10_types[] = {
EC_IMEXPORTABLE_PUBLIC_KEY,
EC_IMEXPORTABLE_OTHER_PARAMETERS,
OSSL_PARAM_END
};
static const OSSL_PARAM ec_11_types[] = {
EC_IMEXPORTABLE_PRIVATE_KEY,
EC_IMEXPORTABLE_PUBLIC_KEY,
EC_IMEXPORTABLE_OTHER_PARAMETERS,
OSSL_PARAM_END
};
static const OSSL_PARAM ec_all_parameters_types[] = {
EC_IMEXPORTABLE_DOM_PARAMETERS,
EC_IMEXPORTABLE_OTHER_PARAMETERS,
OSSL_PARAM_END
};
static const OSSL_PARAM ec_13_types[] = {
EC_IMEXPORTABLE_PRIVATE_KEY,
EC_IMEXPORTABLE_DOM_PARAMETERS,
EC_IMEXPORTABLE_OTHER_PARAMETERS,
OSSL_PARAM_END
};
static const OSSL_PARAM ec_14_types[] = {
EC_IMEXPORTABLE_PUBLIC_KEY,
EC_IMEXPORTABLE_DOM_PARAMETERS,
EC_IMEXPORTABLE_OTHER_PARAMETERS,
OSSL_PARAM_END
};
static const OSSL_PARAM ec_all_types[] = {
EC_IMEXPORTABLE_PRIVATE_KEY,
EC_IMEXPORTABLE_PUBLIC_KEY,
EC_IMEXPORTABLE_DOM_PARAMETERS,
EC_IMEXPORTABLE_OTHER_PARAMETERS,
OSSL_PARAM_END
};
static const OSSL_PARAM *ec_types[] = {
NULL,
ec_private_key_types,
ec_public_key_types,
ec_key_types,
ec_dom_parameters_types,
ec_5_types,
ec_6_types,
ec_key_domp_types,
ec_other_parameters_types,
ec_9_types,
ec_10_types,
ec_11_types,
ec_all_parameters_types,
ec_13_types,
ec_14_types,
ec_all_types
};

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,102 @@
/*
* Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* 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
*/
/*
* This implemments a dummy key manager for legacy KDFs that still support the
* old way of performing a KDF via EVP_PKEY_derive(). New KDFs should not be
* implemented this way. In reality there is no key data for such KDFs, so this
* key manager does very little.
*/
#include <openssl/core_dispatch.h>
#include <openssl/core_names.h>
#include <openssl/err.h>
#include "prov/implementations.h"
#include "prov/providercommon.h"
#include "prov/provider_ctx.h"
#include "prov/kdfexchange.h"
static OSSL_FUNC_keymgmt_new_fn kdf_newdata;
static OSSL_FUNC_keymgmt_free_fn kdf_freedata;
static OSSL_FUNC_keymgmt_has_fn kdf_has;
KDF_DATA *ossl_kdf_data_new(void *provctx)
{
KDF_DATA *kdfdata;
if (!ossl_prov_is_running())
return NULL;
kdfdata = OPENSSL_zalloc(sizeof(*kdfdata));
if (kdfdata == NULL)
return NULL;
if (!CRYPTO_NEW_REF(&kdfdata->refcnt, 1)) {
OPENSSL_free(kdfdata);
return NULL;
}
kdfdata->libctx = PROV_LIBCTX_OF(provctx);
return kdfdata;
}
void ossl_kdf_data_free(KDF_DATA *kdfdata)
{
int ref = 0;
if (kdfdata == NULL)
return;
CRYPTO_DOWN_REF(&kdfdata->refcnt, &ref);
if (ref > 0)
return;
CRYPTO_FREE_REF(&kdfdata->refcnt);
OPENSSL_free(kdfdata);
}
int ossl_kdf_data_up_ref(KDF_DATA *kdfdata)
{
int ref = 0;
/* This is effectively doing a new operation on the KDF_DATA and should be
* adequately guarded again modules' error states. However, both current
* calls here are guarded properly in exchange/kdf_exch.c. Thus, it
* could be removed here. The concern is that something in the future
* might call this function without adequate guards. It's a cheap call,
* it seems best to leave it even though it is currently redundant.
*/
if (!ossl_prov_is_running())
return 0;
CRYPTO_UP_REF(&kdfdata->refcnt, &ref);
return 1;
}
static void *kdf_newdata(void *provctx)
{
return ossl_kdf_data_new(provctx);
}
static void kdf_freedata(void *kdfdata)
{
ossl_kdf_data_free(kdfdata);
}
static int kdf_has(const void *keydata, int selection)
{
return 1; /* nothing is missing */
}
const OSSL_DISPATCH ossl_kdf_keymgmt_functions[] = {
{ OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))kdf_newdata },
{ OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))kdf_freedata },
{ OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))kdf_has },
OSSL_DISPATCH_END
};

View File

@@ -0,0 +1,23 @@
providers/implementations/keymgmt/libdefault-lib-dh_kmgmt.o: \
providers/implementations/keymgmt/dh_kmgmt.c \
include/internal/deprecated.h include/openssl/configuration.h \
include/openssl/macros.h include/openssl/opensslconf.h \
include/openssl/opensslv.h include/internal/common.h \
include/internal/e_os.h include/openssl/e_os2.h include/openssl/crypto.h \
include/openssl/safestack.h include/openssl/stack.h \
include/openssl/types.h include/openssl/cryptoerr.h \
include/openssl/symhacks.h include/openssl/cryptoerr_legacy.h \
include/openssl/core.h include/internal/numbers.h \
include/internal/nelem.h include/openssl/core_dispatch.h \
include/openssl/indicator.h include/openssl/params.h \
include/openssl/bn.h include/openssl/bnerr.h \
include/openssl/core_names.h include/openssl/err.h include/openssl/bio.h \
include/openssl/bioerr.h include/openssl/lhash.h \
providers/implementations/include/prov/implementations.h \
providers/common/include/prov/providercommon.h \
include/openssl/provider.h providers/common/include/prov/provider_ctx.h \
include/crypto/dh.h include/openssl/dh.h include/openssl/asn1.h \
include/openssl/asn1err.h include/openssl/dherr.h include/internal/ffc.h \
include/openssl/evp.h include/openssl/evperr.h include/openssl/objects.h \
include/openssl/obj_mac.h include/openssl/objectserr.h \
include/openssl/param_build.h include/internal/sizes.h

View File

@@ -0,0 +1,29 @@
providers/implementations/keymgmt/libdefault-lib-dsa_kmgmt.o: \
providers/implementations/keymgmt/dsa_kmgmt.c \
include/internal/deprecated.h include/openssl/configuration.h \
include/openssl/macros.h include/openssl/opensslconf.h \
include/openssl/opensslv.h include/openssl/core_dispatch.h \
include/openssl/core.h include/openssl/types.h include/openssl/e_os2.h \
include/openssl/safestack.h include/openssl/stack.h \
include/openssl/indicator.h include/openssl/params.h \
include/openssl/bn.h include/openssl/crypto.h \
include/openssl/cryptoerr.h include/openssl/symhacks.h \
include/openssl/cryptoerr_legacy.h include/openssl/bnerr.h \
include/openssl/core_names.h include/openssl/err.h include/openssl/bio.h \
include/openssl/bioerr.h include/openssl/lhash.h \
providers/common/include/prov/securitycheck.h include/crypto/types.h \
include/openssl/ec.h include/openssl/asn1.h include/openssl/asn1err.h \
include/openssl/ecerr.h providers/fips/include/fips/fipsindicator.h \
providers/common/include/prov/providercommon.h \
include/openssl/provider.h \
providers/implementations/include/prov/implementations.h \
providers/common/include/prov/provider_ctx.h include/crypto/dsa.h \
include/openssl/dsa.h include/openssl/dh.h include/openssl/dherr.h \
include/openssl/dsaerr.h include/internal/ffc.h include/openssl/evp.h \
include/openssl/evperr.h include/openssl/objects.h \
include/openssl/obj_mac.h include/openssl/objectserr.h \
include/openssl/param_build.h include/internal/sizes.h \
include/internal/nelem.h include/internal/param_build_set.h \
include/internal/cryptlib.h include/internal/common.h \
include/internal/e_os.h include/internal/numbers.h \
include/openssl/buffer.h include/openssl/buffererr.h

View File

@@ -0,0 +1,30 @@
providers/implementations/keymgmt/libdefault-lib-ec_kmgmt.o: \
providers/implementations/keymgmt/ec_kmgmt.c \
include/internal/deprecated.h include/openssl/configuration.h \
include/openssl/macros.h include/openssl/opensslconf.h \
include/openssl/opensslv.h include/openssl/core_dispatch.h \
include/openssl/core.h include/openssl/types.h include/openssl/e_os2.h \
include/openssl/safestack.h include/openssl/stack.h \
include/openssl/indicator.h include/openssl/params.h \
include/openssl/bn.h include/openssl/crypto.h \
include/openssl/cryptoerr.h include/openssl/symhacks.h \
include/openssl/cryptoerr_legacy.h include/openssl/bnerr.h \
include/openssl/core_names.h include/openssl/err.h include/openssl/bio.h \
include/openssl/bioerr.h include/openssl/lhash.h \
include/openssl/objects.h include/openssl/obj_mac.h \
include/openssl/asn1.h include/openssl/asn1err.h \
include/openssl/objectserr.h include/openssl/proverr.h \
include/crypto/bn.h include/crypto/ec.h include/openssl/evp.h \
include/openssl/evperr.h include/openssl/ec.h include/openssl/ecerr.h \
include/crypto/types.h \
providers/implementations/include/prov/implementations.h \
providers/common/include/prov/providercommon.h \
include/openssl/provider.h providers/common/include/prov/provider_ctx.h \
providers/common/include/prov/securitycheck.h \
providers/fips/include/fips/fipsindicator.h \
include/internal/param_build_set.h include/openssl/param_build.h \
include/internal/cryptlib.h include/internal/common.h \
include/internal/e_os.h include/internal/numbers.h \
include/internal/nelem.h include/openssl/buffer.h \
include/openssl/buffererr.h include/crypto/sm2.h \
providers/implementations/keymgmt/ec_kmgmt_imexport.inc

View File

@@ -0,0 +1,30 @@
providers/implementations/keymgmt/libdefault-lib-ecx_kmgmt.o: \
providers/implementations/keymgmt/ecx_kmgmt.c \
include/openssl/core_dispatch.h include/openssl/core.h \
include/openssl/types.h include/openssl/e_os2.h include/openssl/macros.h \
include/openssl/opensslconf.h include/openssl/configuration.h \
include/openssl/opensslv.h include/openssl/safestack.h \
include/openssl/stack.h include/openssl/indicator.h \
include/openssl/params.h include/openssl/bn.h include/openssl/crypto.h \
include/openssl/cryptoerr.h include/openssl/symhacks.h \
include/openssl/cryptoerr_legacy.h include/openssl/bnerr.h \
include/openssl/core_names.h include/openssl/err.h include/openssl/bio.h \
include/openssl/bioerr.h include/openssl/lhash.h \
include/openssl/proverr.h include/openssl/evp.h include/openssl/evperr.h \
include/openssl/objects.h include/openssl/obj_mac.h \
include/openssl/asn1.h include/openssl/asn1err.h \
include/openssl/objectserr.h include/openssl/rand.h \
include/openssl/randerr.h include/openssl/self_test.h \
include/internal/param_build_set.h include/openssl/param_build.h \
include/internal/cryptlib.h include/internal/common.h \
include/internal/e_os.h include/internal/numbers.h \
include/internal/nelem.h include/openssl/buffer.h \
include/openssl/buffererr.h include/crypto/ecx.h \
include/internal/refcount.h include/openssl/trace.h \
include/crypto/types.h \
providers/implementations/include/prov/implementations.h \
providers/common/include/prov/providercommon.h \
include/openssl/provider.h providers/common/include/prov/provider_ctx.h \
providers/implementations/include/prov/ecx.h \
providers/common/include/prov/securitycheck.h include/openssl/ec.h \
include/openssl/ecerr.h providers/fips/include/fips/fipsindicator.h

View File

@@ -0,0 +1,17 @@
providers/implementations/keymgmt/libdefault-lib-kdf_legacy_kmgmt.o: \
providers/implementations/keymgmt/kdf_legacy_kmgmt.c \
include/openssl/core_dispatch.h include/openssl/core.h \
include/openssl/types.h include/openssl/e_os2.h include/openssl/macros.h \
include/openssl/opensslconf.h include/openssl/configuration.h \
include/openssl/opensslv.h include/openssl/safestack.h \
include/openssl/stack.h include/openssl/indicator.h \
include/openssl/params.h include/openssl/bn.h include/openssl/crypto.h \
include/openssl/cryptoerr.h include/openssl/symhacks.h \
include/openssl/cryptoerr_legacy.h include/openssl/bnerr.h \
include/openssl/core_names.h include/openssl/err.h include/openssl/bio.h \
include/openssl/bioerr.h include/openssl/lhash.h \
providers/implementations/include/prov/implementations.h \
providers/common/include/prov/providercommon.h \
include/openssl/provider.h providers/common/include/prov/provider_ctx.h \
providers/implementations/include/prov/kdfexchange.h \
include/internal/refcount.h include/openssl/trace.h

View File

@@ -0,0 +1,36 @@
providers/implementations/keymgmt/libdefault-lib-mac_legacy_kmgmt.o: \
providers/implementations/keymgmt/mac_legacy_kmgmt.c \
include/openssl/core_dispatch.h include/openssl/core.h \
include/openssl/types.h include/openssl/e_os2.h include/openssl/macros.h \
include/openssl/opensslconf.h include/openssl/configuration.h \
include/openssl/opensslv.h include/openssl/safestack.h \
include/openssl/stack.h include/openssl/indicator.h \
include/openssl/params.h include/openssl/bn.h include/openssl/crypto.h \
include/openssl/cryptoerr.h include/openssl/symhacks.h \
include/openssl/cryptoerr_legacy.h include/openssl/bnerr.h \
include/openssl/core_names.h include/openssl/err.h include/openssl/bio.h \
include/openssl/bioerr.h include/openssl/lhash.h include/openssl/evp.h \
include/openssl/evperr.h include/openssl/objects.h \
include/openssl/obj_mac.h include/openssl/asn1.h \
include/openssl/asn1err.h include/openssl/objectserr.h \
include/openssl/proverr.h include/openssl/param_build.h \
include/openssl/engine.h include/openssl/rsa.h include/openssl/rsaerr.h \
include/openssl/dsa.h include/openssl/dh.h include/openssl/dherr.h \
include/openssl/dsaerr.h include/openssl/ec.h include/openssl/ecerr.h \
include/openssl/rand.h include/openssl/randerr.h include/openssl/ui.h \
include/openssl/pem.h include/openssl/x509.h include/openssl/buffer.h \
include/openssl/buffererr.h include/openssl/sha.h \
include/openssl/x509err.h include/openssl/x509_vfy.h \
include/openssl/pkcs7.h include/openssl/pkcs7err.h \
include/openssl/http.h include/openssl/conf.h include/openssl/conferr.h \
include/openssl/conftypes.h include/openssl/pemerr.h \
include/openssl/uierr.h include/openssl/engineerr.h \
include/internal/param_build_set.h include/internal/cryptlib.h \
include/internal/common.h include/internal/e_os.h \
include/internal/numbers.h include/internal/nelem.h \
providers/implementations/include/prov/implementations.h \
providers/common/include/prov/providercommon.h \
include/openssl/provider.h providers/common/include/prov/provider_ctx.h \
providers/implementations/include/prov/macsignature.h \
include/internal/refcount.h include/openssl/trace.h \
providers/common/include/prov/provider_util.h

View File

@@ -0,0 +1,26 @@
providers/implementations/keymgmt/libdefault-lib-rsa_kmgmt.o: \
providers/implementations/keymgmt/rsa_kmgmt.c \
include/internal/deprecated.h include/openssl/configuration.h \
include/openssl/macros.h include/openssl/opensslconf.h \
include/openssl/opensslv.h include/openssl/core_dispatch.h \
include/openssl/core.h include/openssl/types.h include/openssl/e_os2.h \
include/openssl/safestack.h include/openssl/stack.h \
include/openssl/indicator.h include/openssl/params.h \
include/openssl/bn.h include/openssl/crypto.h \
include/openssl/cryptoerr.h include/openssl/symhacks.h \
include/openssl/cryptoerr_legacy.h include/openssl/bnerr.h \
include/openssl/core_names.h include/openssl/err.h include/openssl/bio.h \
include/openssl/bioerr.h include/openssl/lhash.h include/openssl/rsa.h \
include/openssl/asn1.h include/openssl/asn1err.h \
include/openssl/rsaerr.h include/openssl/evp.h include/openssl/evperr.h \
include/openssl/objects.h include/openssl/obj_mac.h \
include/openssl/objectserr.h include/openssl/proverr.h \
providers/implementations/include/prov/implementations.h \
providers/common/include/prov/providercommon.h \
include/openssl/provider.h providers/common/include/prov/provider_ctx.h \
include/crypto/rsa.h include/crypto/types.h include/crypto/cryptlib.h \
include/internal/cryptlib.h include/internal/common.h \
include/internal/e_os.h include/internal/numbers.h \
include/internal/nelem.h include/openssl/buffer.h \
include/openssl/buffererr.h include/internal/param_build_set.h \
include/openssl/param_build.h

View File

@@ -0,0 +1,573 @@
/*
* Copyright 2020-2025 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* 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
*/
/* We need to use some engine deprecated APIs */
#define OPENSSL_SUPPRESS_DEPRECATED
#include <string.h>
#include <openssl/core_dispatch.h>
#include <openssl/core_names.h>
#include <openssl/params.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/proverr.h>
#include <openssl/param_build.h>
#ifndef FIPS_MODULE
# include <openssl/engine.h>
#endif
#include "internal/param_build_set.h"
#include "prov/implementations.h"
#include "prov/providercommon.h"
#include "prov/provider_ctx.h"
#include "prov/macsignature.h"
static OSSL_FUNC_keymgmt_new_fn mac_new;
static OSSL_FUNC_keymgmt_free_fn mac_free;
static OSSL_FUNC_keymgmt_gen_init_fn mac_gen_init;
static OSSL_FUNC_keymgmt_gen_fn mac_gen;
static OSSL_FUNC_keymgmt_gen_cleanup_fn mac_gen_cleanup;
static OSSL_FUNC_keymgmt_gen_set_params_fn mac_gen_set_params;
static OSSL_FUNC_keymgmt_gen_settable_params_fn mac_gen_settable_params;
static OSSL_FUNC_keymgmt_get_params_fn mac_get_params;
static OSSL_FUNC_keymgmt_gettable_params_fn mac_gettable_params;
static OSSL_FUNC_keymgmt_set_params_fn mac_set_params;
static OSSL_FUNC_keymgmt_settable_params_fn mac_settable_params;
static OSSL_FUNC_keymgmt_has_fn mac_has;
static OSSL_FUNC_keymgmt_match_fn mac_match;
static OSSL_FUNC_keymgmt_import_fn mac_import;
static OSSL_FUNC_keymgmt_import_types_fn mac_imexport_types;
static OSSL_FUNC_keymgmt_export_fn mac_export;
static OSSL_FUNC_keymgmt_export_types_fn mac_imexport_types;
static OSSL_FUNC_keymgmt_new_fn mac_new_cmac;
static OSSL_FUNC_keymgmt_gettable_params_fn cmac_gettable_params;
static OSSL_FUNC_keymgmt_import_types_fn cmac_imexport_types;
static OSSL_FUNC_keymgmt_export_types_fn cmac_imexport_types;
static OSSL_FUNC_keymgmt_gen_init_fn cmac_gen_init;
static OSSL_FUNC_keymgmt_gen_set_params_fn cmac_gen_set_params;
static OSSL_FUNC_keymgmt_gen_settable_params_fn cmac_gen_settable_params;
struct mac_gen_ctx {
OSSL_LIB_CTX *libctx;
int selection;
unsigned char *priv_key;
size_t priv_key_len;
PROV_CIPHER cipher;
};
MAC_KEY *ossl_mac_key_new(OSSL_LIB_CTX *libctx, int cmac)
{
MAC_KEY *mackey;
if (!ossl_prov_is_running())
return NULL;
mackey = OPENSSL_zalloc(sizeof(*mackey));
if (mackey == NULL)
return NULL;
if (!CRYPTO_NEW_REF(&mackey->refcnt, 1)) {
OPENSSL_free(mackey);
return NULL;
}
mackey->libctx = libctx;
mackey->cmac = cmac;
return mackey;
}
void ossl_mac_key_free(MAC_KEY *mackey)
{
int ref = 0;
if (mackey == NULL)
return;
CRYPTO_DOWN_REF(&mackey->refcnt, &ref);
if (ref > 0)
return;
OPENSSL_secure_clear_free(mackey->priv_key, mackey->priv_key_len);
OPENSSL_free(mackey->properties);
ossl_prov_cipher_reset(&mackey->cipher);
CRYPTO_FREE_REF(&mackey->refcnt);
OPENSSL_free(mackey);
}
int ossl_mac_key_up_ref(MAC_KEY *mackey)
{
int ref = 0;
/* This is effectively doing a new operation on the MAC_KEY and should be
* adequately guarded again modules' error states. However, both current
* calls here are guarded properly in signature/mac_legacy.c. Thus, it
* could be removed here. The concern is that something in the future
* might call this function without adequate guards. It's a cheap call,
* it seems best to leave it even though it is currently redundant.
*/
if (!ossl_prov_is_running())
return 0;
CRYPTO_UP_REF(&mackey->refcnt, &ref);
return 1;
}
static void *mac_new(void *provctx)
{
return ossl_mac_key_new(PROV_LIBCTX_OF(provctx), 0);
}
static void *mac_new_cmac(void *provctx)
{
return ossl_mac_key_new(PROV_LIBCTX_OF(provctx), 1);
}
static void mac_free(void *mackey)
{
ossl_mac_key_free(mackey);
}
static int mac_has(const void *keydata, int selection)
{
const MAC_KEY *key = keydata;
int ok = 0;
if (ossl_prov_is_running() && key != NULL) {
/*
* MAC keys always have all the parameters they need (i.e. none).
* Therefore we always return with 1, if asked about parameters.
* Similarly for public keys.
*/
ok = 1;
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
ok = key->priv_key != NULL;
}
return ok;
}
static int mac_match(const void *keydata1, const void *keydata2, int selection)
{
const MAC_KEY *key1 = keydata1;
const MAC_KEY *key2 = keydata2;
int ok = 1;
if (!ossl_prov_is_running())
return 0;
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
if ((key1->priv_key == NULL && key2->priv_key != NULL)
|| (key1->priv_key != NULL && key2->priv_key == NULL)
|| key1->priv_key_len != key2->priv_key_len
|| (key1->cipher.cipher == NULL && key2->cipher.cipher != NULL)
|| (key1->cipher.cipher != NULL && key2->cipher.cipher == NULL))
ok = 0;
else
ok = ok && (key1->priv_key == NULL /* implies key2->privkey == NULL */
|| CRYPTO_memcmp(key1->priv_key, key2->priv_key,
key1->priv_key_len) == 0);
if (key1->cipher.cipher != NULL)
ok = ok && EVP_CIPHER_is_a(key1->cipher.cipher,
EVP_CIPHER_get0_name(key2->cipher.cipher));
}
return ok;
}
static int mac_key_fromdata(MAC_KEY *key, const OSSL_PARAM params[])
{
const OSSL_PARAM *p;
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
if (p != NULL) {
if (p->data_type != OSSL_PARAM_OCTET_STRING) {
ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
return 0;
}
OPENSSL_secure_clear_free(key->priv_key, key->priv_key_len);
/* allocate at least one byte to distinguish empty key from no key set */
key->priv_key = OPENSSL_secure_malloc(p->data_size > 0 ? p->data_size : 1);
if (key->priv_key == NULL)
return 0;
memcpy(key->priv_key, p->data, p->data_size);
key->priv_key_len = p->data_size;
}
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PROPERTIES);
if (p != NULL) {
if (p->data_type != OSSL_PARAM_UTF8_STRING) {
ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
return 0;
}
OPENSSL_free(key->properties);
key->properties = OPENSSL_strdup(p->data);
if (key->properties == NULL)
return 0;
}
if (key->cmac && !ossl_prov_cipher_load_from_params(&key->cipher, params,
key->libctx)) {
ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
return 0;
}
if (key->priv_key != NULL)
return 1;
return 0;
}
static int mac_import(void *keydata, int selection, const OSSL_PARAM params[])
{
MAC_KEY *key = keydata;
if (!ossl_prov_is_running() || key == NULL)
return 0;
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0)
return 0;
return mac_key_fromdata(key, params);
}
static int key_to_params(MAC_KEY *key, OSSL_PARAM_BLD *tmpl,
OSSL_PARAM params[])
{
if (key == NULL)
return 0;
if (key->priv_key != NULL
&& !ossl_param_build_set_octet_string(tmpl, params,
OSSL_PKEY_PARAM_PRIV_KEY,
key->priv_key, key->priv_key_len))
return 0;
if (key->cipher.cipher != NULL
&& !ossl_param_build_set_utf8_string(tmpl, params,
OSSL_PKEY_PARAM_CIPHER,
EVP_CIPHER_get0_name(key->cipher.cipher)))
return 0;
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
if (key->cipher.engine != NULL
&& !ossl_param_build_set_utf8_string(tmpl, params,
OSSL_PKEY_PARAM_ENGINE,
ENGINE_get_id(key->cipher.engine)))
return 0;
#endif
return 1;
}
static int mac_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
void *cbarg)
{
MAC_KEY *key = keydata;
OSSL_PARAM_BLD *tmpl;
OSSL_PARAM *params = NULL;
int ret = 0;
if (!ossl_prov_is_running() || key == NULL)
return 0;
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0)
return 0;
tmpl = OSSL_PARAM_BLD_new();
if (tmpl == NULL)
return 0;
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
&& !key_to_params(key, tmpl, NULL))
goto err;
params = OSSL_PARAM_BLD_to_param(tmpl);
if (params == NULL)
goto err;
ret = param_cb(params, cbarg);
OSSL_PARAM_free(params);
err:
OSSL_PARAM_BLD_free(tmpl);
return ret;
}
static const OSSL_PARAM mac_key_types[] = {
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_PROPERTIES, NULL, 0),
OSSL_PARAM_END
};
static const OSSL_PARAM *mac_imexport_types(int selection)
{
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
return mac_key_types;
return NULL;
}
static const OSSL_PARAM cmac_key_types[] = {
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_CIPHER, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_ENGINE, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_PROPERTIES, NULL, 0),
OSSL_PARAM_END
};
static const OSSL_PARAM *cmac_imexport_types(int selection)
{
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
return cmac_key_types;
return NULL;
}
static int mac_get_params(void *key, OSSL_PARAM params[])
{
return key_to_params(key, NULL, params);
}
static const OSSL_PARAM *mac_gettable_params(void *provctx)
{
static const OSSL_PARAM gettable_params[] = {
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
OSSL_PARAM_END
};
return gettable_params;
}
static const OSSL_PARAM *cmac_gettable_params(void *provctx)
{
static const OSSL_PARAM gettable_params[] = {
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_CIPHER, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_ENGINE, NULL, 0),
OSSL_PARAM_END
};
return gettable_params;
}
static int mac_set_params(void *keydata, const OSSL_PARAM params[])
{
MAC_KEY *key = keydata;
const OSSL_PARAM *p;
if (key == NULL)
return 0;
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
if (p != NULL)
return mac_key_fromdata(key, params);
return 1;
}
static const OSSL_PARAM *mac_settable_params(void *provctx)
{
static const OSSL_PARAM settable_params[] = {
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
OSSL_PARAM_END
};
return settable_params;
}
static void *mac_gen_init_common(void *provctx, int selection)
{
OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
struct mac_gen_ctx *gctx = NULL;
if (!ossl_prov_is_running())
return NULL;
if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
gctx->libctx = libctx;
gctx->selection = selection;
}
return gctx;
}
static void *mac_gen_init(void *provctx, int selection,
const OSSL_PARAM params[])
{
struct mac_gen_ctx *gctx = mac_gen_init_common(provctx, selection);
if (gctx != NULL && !mac_gen_set_params(gctx, params)) {
mac_gen_cleanup(gctx);
gctx = NULL;
}
return gctx;
}
static void *cmac_gen_init(void *provctx, int selection,
const OSSL_PARAM params[])
{
struct mac_gen_ctx *gctx = mac_gen_init_common(provctx, selection);
if (gctx != NULL && !cmac_gen_set_params(gctx, params)) {
mac_gen_cleanup(gctx);
gctx = NULL;
}
return gctx;
}
static int mac_gen_set_params(void *genctx, const OSSL_PARAM params[])
{
struct mac_gen_ctx *gctx = genctx;
const OSSL_PARAM *p;
if (gctx == NULL)
return 0;
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
if (p != NULL) {
if (p->data_type != OSSL_PARAM_OCTET_STRING) {
ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
return 0;
}
gctx->priv_key = OPENSSL_secure_malloc(p->data_size);
if (gctx->priv_key == NULL)
return 0;
memcpy(gctx->priv_key, p->data, p->data_size);
gctx->priv_key_len = p->data_size;
}
return 1;
}
static int cmac_gen_set_params(void *genctx, const OSSL_PARAM params[])
{
struct mac_gen_ctx *gctx = genctx;
if (!mac_gen_set_params(genctx, params))
return 0;
if (!ossl_prov_cipher_load_from_params(&gctx->cipher, params,
gctx->libctx)) {
ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
return 0;
}
return 1;
}
static const OSSL_PARAM *mac_gen_settable_params(ossl_unused void *genctx,
ossl_unused void *provctx)
{
static OSSL_PARAM settable[] = {
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
OSSL_PARAM_END
};
return settable;
}
static const OSSL_PARAM *cmac_gen_settable_params(ossl_unused void *genctx,
ossl_unused void *provctx)
{
static OSSL_PARAM settable[] = {
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_CIPHER, NULL, 0),
OSSL_PARAM_END
};
return settable;
}
static void *mac_gen(void *genctx, OSSL_CALLBACK *cb, void *cbarg)
{
struct mac_gen_ctx *gctx = genctx;
MAC_KEY *key;
if (!ossl_prov_is_running() || gctx == NULL)
return NULL;
if ((key = ossl_mac_key_new(gctx->libctx, 0)) == NULL) {
ERR_raise(ERR_LIB_PROV, ERR_R_PROV_LIB);
return NULL;
}
/* If we're doing parameter generation then we just return a blank key */
if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
return key;
if (gctx->priv_key == NULL) {
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
ossl_mac_key_free(key);
return NULL;
}
/*
* This is horrible but required for backwards compatibility. We don't
* actually do real key generation at all. We simply copy the key that was
* previously set in the gctx. Hopefully at some point in the future all
* of this can be removed and we will only support the EVP_KDF APIs.
*/
if (!ossl_prov_cipher_copy(&key->cipher, &gctx->cipher)) {
ossl_mac_key_free(key);
ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
return NULL;
}
ossl_prov_cipher_reset(&gctx->cipher);
key->priv_key = gctx->priv_key;
key->priv_key_len = gctx->priv_key_len;
gctx->priv_key_len = 0;
gctx->priv_key = NULL;
return key;
}
static void mac_gen_cleanup(void *genctx)
{
struct mac_gen_ctx *gctx = genctx;
if (gctx == NULL)
return;
OPENSSL_secure_clear_free(gctx->priv_key, gctx->priv_key_len);
ossl_prov_cipher_reset(&gctx->cipher);
OPENSSL_free(gctx);
}
const OSSL_DISPATCH ossl_mac_legacy_keymgmt_functions[] = {
{ OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))mac_new },
{ OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))mac_free },
{ OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))mac_get_params },
{ OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))mac_gettable_params },
{ OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))mac_set_params },
{ OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))mac_settable_params },
{ OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))mac_has },
{ OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))mac_match },
{ OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))mac_import },
{ OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))mac_imexport_types },
{ OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))mac_export },
{ OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))mac_imexport_types },
{ OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))mac_gen_init },
{ OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))mac_gen_set_params },
{ OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
(void (*)(void))mac_gen_settable_params },
{ OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))mac_gen },
{ OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))mac_gen_cleanup },
OSSL_DISPATCH_END
};
const OSSL_DISPATCH ossl_cmac_legacy_keymgmt_functions[] = {
{ OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))mac_new_cmac },
{ OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))mac_free },
{ OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))mac_get_params },
{ OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))cmac_gettable_params },
{ OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))mac_set_params },
{ OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))mac_settable_params },
{ OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))mac_has },
{ OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))mac_match },
{ OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))mac_import },
{ OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))cmac_imexport_types },
{ OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))mac_export },
{ OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))cmac_imexport_types },
{ OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))cmac_gen_init },
{ OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))cmac_gen_set_params },
{ OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
(void (*)(void))cmac_gen_settable_params },
{ OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))mac_gen },
{ OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))mac_gen_cleanup },
OSSL_DISPATCH_END
};

View File

@@ -0,0 +1,744 @@
/*
* Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* 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
*/
/*
* RSA low level APIs are deprecated for public use, but still ok for
* internal use.
*/
#include "internal/deprecated.h"
#include <openssl/core_dispatch.h>
#include <openssl/core_names.h>
#include <openssl/bn.h>
#include <openssl/err.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/proverr.h>
#include "prov/implementations.h"
#include "prov/providercommon.h"
#include "prov/provider_ctx.h"
#include "crypto/rsa.h"
#include "crypto/cryptlib.h"
#include "internal/param_build_set.h"
static OSSL_FUNC_keymgmt_new_fn rsa_newdata;
static OSSL_FUNC_keymgmt_new_fn rsapss_newdata;
static OSSL_FUNC_keymgmt_gen_init_fn rsa_gen_init;
static OSSL_FUNC_keymgmt_gen_init_fn rsapss_gen_init;
static OSSL_FUNC_keymgmt_gen_set_params_fn rsa_gen_set_params;
static OSSL_FUNC_keymgmt_gen_settable_params_fn rsa_gen_settable_params;
static OSSL_FUNC_keymgmt_gen_settable_params_fn rsapss_gen_settable_params;
static OSSL_FUNC_keymgmt_gen_fn rsa_gen;
static OSSL_FUNC_keymgmt_gen_cleanup_fn rsa_gen_cleanup;
static OSSL_FUNC_keymgmt_load_fn rsa_load;
static OSSL_FUNC_keymgmt_load_fn rsapss_load;
static OSSL_FUNC_keymgmt_free_fn rsa_freedata;
static OSSL_FUNC_keymgmt_get_params_fn rsa_get_params;
static OSSL_FUNC_keymgmt_gettable_params_fn rsa_gettable_params;
static OSSL_FUNC_keymgmt_has_fn rsa_has;
static OSSL_FUNC_keymgmt_match_fn rsa_match;
static OSSL_FUNC_keymgmt_validate_fn rsa_validate;
static OSSL_FUNC_keymgmt_import_fn rsa_import;
static OSSL_FUNC_keymgmt_import_types_fn rsa_import_types;
static OSSL_FUNC_keymgmt_export_fn rsa_export;
static OSSL_FUNC_keymgmt_export_types_fn rsa_export_types;
static OSSL_FUNC_keymgmt_query_operation_name_fn rsa_query_operation_name;
static OSSL_FUNC_keymgmt_dup_fn rsa_dup;
#define RSA_DEFAULT_MD "SHA256"
#define RSA_PSS_DEFAULT_MD OSSL_DIGEST_NAME_SHA1
#define RSA_POSSIBLE_SELECTIONS \
(OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS)
DEFINE_STACK_OF(BIGNUM)
DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
static int pss_params_fromdata(RSA_PSS_PARAMS_30 *pss_params, int *defaults_set,
const OSSL_PARAM params[], int rsa_type,
OSSL_LIB_CTX *libctx)
{
if (!ossl_rsa_pss_params_30_fromdata(pss_params, defaults_set,
params, libctx))
return 0;
/* If not a PSS type RSA, sending us PSS parameters is wrong */
if (rsa_type != RSA_FLAG_TYPE_RSASSAPSS
&& !ossl_rsa_pss_params_30_is_unrestricted(pss_params))
return 0;
return 1;
}
static void *rsa_newdata(void *provctx)
{
OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
RSA *rsa;
if (!ossl_prov_is_running())
return NULL;
rsa = ossl_rsa_new_with_ctx(libctx);
if (rsa != NULL) {
RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA);
}
return rsa;
}
static void *rsapss_newdata(void *provctx)
{
OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
RSA *rsa;
if (!ossl_prov_is_running())
return NULL;
rsa = ossl_rsa_new_with_ctx(libctx);
if (rsa != NULL) {
RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS);
}
return rsa;
}
static void rsa_freedata(void *keydata)
{
RSA_free(keydata);
}
static int rsa_has(const void *keydata, int selection)
{
const RSA *rsa = keydata;
int ok = 1;
if (rsa == NULL || !ossl_prov_is_running())
return 0;
if ((selection & RSA_POSSIBLE_SELECTIONS) == 0)
return 1; /* the selection is not missing */
/* OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS are always available even if empty */
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
ok = ok && (RSA_get0_n(rsa) != NULL);
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
ok = ok && (RSA_get0_e(rsa) != NULL);
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
ok = ok && (RSA_get0_d(rsa) != NULL);
return ok;
}
static int rsa_match(const void *keydata1, const void *keydata2, int selection)
{
const RSA *rsa1 = keydata1;
const RSA *rsa2 = keydata2;
int ok = 1;
if (!ossl_prov_is_running())
return 0;
/* There is always an |e| */
ok = ok && BN_cmp(RSA_get0_e(rsa1), RSA_get0_e(rsa2)) == 0;
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
int key_checked = 0;
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
const BIGNUM *pa = RSA_get0_n(rsa1);
const BIGNUM *pb = RSA_get0_n(rsa2);
if (pa != NULL && pb != NULL) {
ok = ok && BN_cmp(pa, pb) == 0;
key_checked = 1;
}
}
if (!key_checked
&& (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
const BIGNUM *pa = RSA_get0_d(rsa1);
const BIGNUM *pb = RSA_get0_d(rsa2);
if (pa != NULL && pb != NULL) {
ok = ok && BN_cmp(pa, pb) == 0;
key_checked = 1;
}
}
ok = ok && key_checked;
}
return ok;
}
static int rsa_import(void *keydata, int selection, const OSSL_PARAM params[])
{
RSA *rsa = keydata;
int rsa_type;
int ok = 1;
int pss_defaults_set = 0;
if (!ossl_prov_is_running() || rsa == NULL)
return 0;
if ((selection & RSA_POSSIBLE_SELECTIONS) == 0)
return 0;
rsa_type = RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK);
if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
ok = ok && pss_params_fromdata(ossl_rsa_get0_pss_params_30(rsa),
&pss_defaults_set,
params, rsa_type,
ossl_rsa_get0_libctx(rsa));
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
int include_private =
selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
ok = ok && ossl_rsa_fromdata(rsa, params, include_private);
}
return ok;
}
static int rsa_export(void *keydata, int selection,
OSSL_CALLBACK *param_callback, void *cbarg)
{
RSA *rsa = keydata;
const RSA_PSS_PARAMS_30 *pss_params = ossl_rsa_get0_pss_params_30(rsa);
OSSL_PARAM_BLD *tmpl;
OSSL_PARAM *params = NULL;
int ok = 1;
if (!ossl_prov_is_running() || rsa == NULL)
return 0;
if ((selection & RSA_POSSIBLE_SELECTIONS) == 0)
return 0;
tmpl = OSSL_PARAM_BLD_new();
if (tmpl == NULL)
return 0;
if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
ok = ok && (ossl_rsa_pss_params_30_is_unrestricted(pss_params)
|| ossl_rsa_pss_params_30_todata(pss_params, tmpl, NULL));
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
int include_private =
selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
ok = ok && ossl_rsa_todata(rsa, tmpl, NULL, include_private);
}
if (!ok || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
ok = 0;
goto err;
}
ok = param_callback(params, cbarg);
OSSL_PARAM_free(params);
err:
OSSL_PARAM_BLD_free(tmpl);
return ok;
}
#ifdef FIPS_MODULE
/* In fips mode there are no multi-primes. */
# define RSA_KEY_MP_TYPES() \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, NULL, 0),
#else
/*
* We allow up to 10 prime factors (starting with p, q).
* NOTE: there is only 9 OSSL_PKEY_PARAM_RSA_COEFFICIENT
*/
# define RSA_KEY_MP_TYPES() \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR3, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR4, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR5, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR6, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR7, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR8, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR9, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR10, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT3, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT4, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT5, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT6, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT7, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT8, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT9, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT10, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT2, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT3, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT4, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT5, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT6, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT7, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT8, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT9, NULL, 0),
#endif
#define RSA_KEY_TYPES() \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, NULL, 0), \
RSA_KEY_MP_TYPES()
/*
* This provider can export everything in an RSA key, so we use the exact
* same type description for export as for import. Other providers might
* choose to import full keys, but only export the public parts, and will
* therefore have the importkey_types and importkey_types functions return
* different arrays.
*/
static const OSSL_PARAM rsa_key_types[] = {
RSA_KEY_TYPES()
OSSL_PARAM_END
};
/*
* We lied about the amount of factors, exponents and coefficients, the
* export and import functions can really deal with an infinite amount
* of these numbers. However, RSA keys with too many primes are futile,
* so we at least pretend to have some limits.
*/
static const OSSL_PARAM *rsa_imexport_types(int selection)
{
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
return rsa_key_types;
return NULL;
}
static const OSSL_PARAM *rsa_import_types(int selection)
{
return rsa_imexport_types(selection);
}
static const OSSL_PARAM *rsa_export_types(int selection)
{
return rsa_imexport_types(selection);
}
static int rsa_get_params(void *key, OSSL_PARAM params[])
{
RSA *rsa = key;
const RSA_PSS_PARAMS_30 *pss_params = ossl_rsa_get0_pss_params_30(rsa);
int rsa_type = RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK);
OSSL_PARAM *p;
int empty = RSA_get0_n(rsa) == NULL;
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
&& (empty || !OSSL_PARAM_set_int(p, RSA_bits(rsa))))
return 0;
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
&& (empty || !OSSL_PARAM_set_int(p, RSA_security_bits(rsa))))
return 0;
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
&& (empty || !OSSL_PARAM_set_int(p, RSA_size(rsa))))
return 0;
/*
* For restricted RSA-PSS keys, we ignore the default digest request.
* With RSA-OAEP keys, this may need to be amended.
*/
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
&& (rsa_type != RSA_FLAG_TYPE_RSASSAPSS
|| ossl_rsa_pss_params_30_is_unrestricted(pss_params))) {
if (!OSSL_PARAM_set_utf8_string(p, RSA_DEFAULT_MD))
return 0;
}
/*
* For non-RSA-PSS keys, we ignore the mandatory digest request.
* With RSA-OAEP keys, this may need to be amended.
*/
if ((p = OSSL_PARAM_locate(params,
OSSL_PKEY_PARAM_MANDATORY_DIGEST)) != NULL
&& rsa_type == RSA_FLAG_TYPE_RSASSAPSS
&& !ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
const char *mdname =
ossl_rsa_oaeppss_nid2name(ossl_rsa_pss_params_30_hashalg(pss_params));
if (mdname == NULL || !OSSL_PARAM_set_utf8_string(p, mdname))
return 0;
}
return (rsa_type != RSA_FLAG_TYPE_RSASSAPSS
|| ossl_rsa_pss_params_30_todata(pss_params, NULL, params))
&& ossl_rsa_todata(rsa, NULL, params, 1);
}
static const OSSL_PARAM rsa_params[] = {
OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
RSA_KEY_TYPES()
OSSL_PARAM_END
};
static const OSSL_PARAM *rsa_gettable_params(void *provctx)
{
return rsa_params;
}
static int rsa_validate(const void *keydata, int selection, int checktype)
{
const RSA *rsa = keydata;
int ok = 1;
if (!ossl_prov_is_running())
return 0;
if ((selection & RSA_POSSIBLE_SELECTIONS) == 0)
return 1; /* nothing to validate */
/* If the whole key is selected, we do a pairwise validation */
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR)
== OSSL_KEYMGMT_SELECT_KEYPAIR) {
ok = ok && ossl_rsa_validate_pairwise(rsa);
} else {
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
ok = ok && ossl_rsa_validate_private(rsa);
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
ok = ok && ossl_rsa_validate_public(rsa);
}
return ok;
}
struct rsa_gen_ctx {
OSSL_LIB_CTX *libctx;
const char *propq;
int rsa_type;
size_t nbits;
BIGNUM *pub_exp;
size_t primes;
/* For PSS */
RSA_PSS_PARAMS_30 pss_params;
int pss_defaults_set;
/* For generation callback */
OSSL_CALLBACK *cb;
void *cbarg;
#if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
/* ACVP test parameters */
OSSL_PARAM *acvp_test_params;
#endif
};
static int rsa_gencb(int p, int n, BN_GENCB *cb)
{
struct rsa_gen_ctx *gctx = BN_GENCB_get_arg(cb);
OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p);
params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n);
return gctx->cb(params, gctx->cbarg);
}
static void *gen_init(void *provctx, int selection, int rsa_type,
const OSSL_PARAM params[])
{
OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
struct rsa_gen_ctx *gctx = NULL;
if (!ossl_prov_is_running())
return NULL;
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
return NULL;
if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
gctx->libctx = libctx;
if ((gctx->pub_exp = BN_new()) == NULL
|| !BN_set_word(gctx->pub_exp, RSA_F4)) {
goto err;
}
gctx->nbits = 2048;
gctx->primes = RSA_DEFAULT_PRIME_NUM;
gctx->rsa_type = rsa_type;
} else {
goto err;
}
if (!rsa_gen_set_params(gctx, params))
goto err;
return gctx;
err:
if (gctx != NULL)
BN_free(gctx->pub_exp);
OPENSSL_free(gctx);
return NULL;
}
static void *rsa_gen_init(void *provctx, int selection,
const OSSL_PARAM params[])
{
return gen_init(provctx, selection, RSA_FLAG_TYPE_RSA, params);
}
static void *rsapss_gen_init(void *provctx, int selection,
const OSSL_PARAM params[])
{
return gen_init(provctx, selection, RSA_FLAG_TYPE_RSASSAPSS, params);
}
/*
* This function is common for all RSA sub-types, to detect possible
* misuse, such as PSS parameters being passed when a plain RSA key
* is generated.
*/
static int rsa_gen_set_params(void *genctx, const OSSL_PARAM params[])
{
struct rsa_gen_ctx *gctx = genctx;
const OSSL_PARAM *p;
if (params == NULL)
return 1;
if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_BITS)) != NULL) {
if (!OSSL_PARAM_get_size_t(p, &gctx->nbits))
return 0;
if (gctx->nbits < RSA_MIN_MODULUS_BITS) {
ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SIZE_TOO_SMALL);
return 0;
}
}
if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_PRIMES)) != NULL
&& !OSSL_PARAM_get_size_t(p, &gctx->primes))
return 0;
if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E)) != NULL
&& !OSSL_PARAM_get_BN(p, &gctx->pub_exp))
return 0;
/* Only attempt to get PSS parameters when generating an RSA-PSS key */
if (gctx->rsa_type == RSA_FLAG_TYPE_RSASSAPSS
&& !pss_params_fromdata(&gctx->pss_params, &gctx->pss_defaults_set, params,
gctx->rsa_type, gctx->libctx))
return 0;
#if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
/* Any ACVP test related parameters are copied into a params[] */
if (!ossl_rsa_acvp_test_gen_params_new(&gctx->acvp_test_params, params))
return 0;
#endif
return 1;
}
#define rsa_gen_basic \
OSSL_PARAM_size_t(OSSL_PKEY_PARAM_RSA_BITS, NULL), \
OSSL_PARAM_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, NULL), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0)
/*
* The following must be kept in sync with ossl_rsa_pss_params_30_fromdata()
* in crypto/rsa/rsa_backend.c
*/
#define rsa_gen_pss \
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_RSA_DIGEST, NULL, 0), \
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_RSA_DIGEST_PROPS, NULL, 0), \
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_RSA_MASKGENFUNC, NULL, 0), \
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_RSA_MGF1_DIGEST, NULL, 0), \
OSSL_PARAM_int(OSSL_PKEY_PARAM_RSA_PSS_SALTLEN, NULL)
static const OSSL_PARAM *rsa_gen_settable_params(ossl_unused void *genctx,
ossl_unused void *provctx)
{
static OSSL_PARAM settable[] = {
rsa_gen_basic,
OSSL_PARAM_END
};
return settable;
}
static const OSSL_PARAM *rsapss_gen_settable_params(ossl_unused void *genctx,
ossl_unused void *provctx)
{
static OSSL_PARAM settable[] = {
rsa_gen_basic,
rsa_gen_pss,
OSSL_PARAM_END
};
return settable;
}
static void *rsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
{
struct rsa_gen_ctx *gctx = genctx;
RSA *rsa = NULL, *rsa_tmp = NULL;
BN_GENCB *gencb = NULL;
if (!ossl_prov_is_running() || gctx == NULL)
return NULL;
switch (gctx->rsa_type) {
case RSA_FLAG_TYPE_RSA:
/* For plain RSA keys, PSS parameters must not be set */
if (!ossl_rsa_pss_params_30_is_unrestricted(&gctx->pss_params))
goto err;
break;
case RSA_FLAG_TYPE_RSASSAPSS:
/*
* For plain RSA-PSS keys, PSS parameters may be set but don't have
* to, so not check.
*/
break;
default:
/* Unsupported RSA key sub-type... */
return NULL;
}
if ((rsa_tmp = ossl_rsa_new_with_ctx(gctx->libctx)) == NULL)
return NULL;
gctx->cb = osslcb;
gctx->cbarg = cbarg;
gencb = BN_GENCB_new();
if (gencb != NULL)
BN_GENCB_set(gencb, rsa_gencb, genctx);
#if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
if (gctx->acvp_test_params != NULL) {
if (!ossl_rsa_acvp_test_set_params(rsa_tmp, gctx->acvp_test_params))
goto err;
}
#endif
if (!RSA_generate_multi_prime_key(rsa_tmp,
(int)gctx->nbits, (int)gctx->primes,
gctx->pub_exp, gencb))
goto err;
if (!ossl_rsa_pss_params_30_copy(ossl_rsa_get0_pss_params_30(rsa_tmp),
&gctx->pss_params))
goto err;
RSA_clear_flags(rsa_tmp, RSA_FLAG_TYPE_MASK);
RSA_set_flags(rsa_tmp, gctx->rsa_type);
rsa = rsa_tmp;
rsa_tmp = NULL;
err:
BN_GENCB_free(gencb);
RSA_free(rsa_tmp);
return rsa;
}
static void rsa_gen_cleanup(void *genctx)
{
struct rsa_gen_ctx *gctx = genctx;
if (gctx == NULL)
return;
#if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
ossl_rsa_acvp_test_gen_params_free(gctx->acvp_test_params);
gctx->acvp_test_params = NULL;
#endif
BN_clear_free(gctx->pub_exp);
OPENSSL_free(gctx);
}
static void *common_load(const void *reference, size_t reference_sz,
int expected_rsa_type)
{
RSA *rsa = NULL;
if (ossl_prov_is_running() && reference_sz == sizeof(rsa)) {
/* The contents of the reference is the address to our object */
rsa = *(RSA **)reference;
if (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK) != expected_rsa_type)
return NULL;
/* We grabbed, so we detach it */
*(RSA **)reference = NULL;
return rsa;
}
return NULL;
}
static void *rsa_load(const void *reference, size_t reference_sz)
{
return common_load(reference, reference_sz, RSA_FLAG_TYPE_RSA);
}
static void *rsapss_load(const void *reference, size_t reference_sz)
{
return common_load(reference, reference_sz, RSA_FLAG_TYPE_RSASSAPSS);
}
static void *rsa_dup(const void *keydata_from, int selection)
{
if (ossl_prov_is_running()
/* do not allow creating empty keys by duplication */
&& (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
return ossl_rsa_dup(keydata_from, selection);
return NULL;
}
/* For any RSA key, we use the "RSA" algorithms regardless of sub-type. */
static const char *rsa_query_operation_name(int operation_id)
{
return "RSA";
}
const OSSL_DISPATCH ossl_rsa_keymgmt_functions[] = {
{ OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))rsa_newdata },
{ OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))rsa_gen_init },
{ OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS,
(void (*)(void))rsa_gen_set_params },
{ OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
(void (*)(void))rsa_gen_settable_params },
{ OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))rsa_gen },
{ OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))rsa_gen_cleanup },
{ OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))rsa_load },
{ OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))rsa_freedata },
{ OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))rsa_get_params },
{ OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))rsa_gettable_params },
{ OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))rsa_has },
{ OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))rsa_match },
{ OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))rsa_validate },
{ OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))rsa_import },
{ OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))rsa_import_types },
{ OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))rsa_export },
{ OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))rsa_export_types },
{ OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))rsa_dup },
OSSL_DISPATCH_END
};
const OSSL_DISPATCH ossl_rsapss_keymgmt_functions[] = {
{ OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))rsapss_newdata },
{ OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))rsapss_gen_init },
{ OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))rsa_gen_set_params },
{ OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
(void (*)(void))rsapss_gen_settable_params },
{ OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))rsa_gen },
{ OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))rsa_gen_cleanup },
{ OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))rsapss_load },
{ OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))rsa_freedata },
{ OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))rsa_get_params },
{ OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))rsa_gettable_params },
{ OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))rsa_has },
{ OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))rsa_match },
{ OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))rsa_validate },
{ OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))rsa_import },
{ OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))rsa_import_types },
{ OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))rsa_export },
{ OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))rsa_export_types },
{ OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
(void (*)(void))rsa_query_operation_name },
{ OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))rsa_dup },
OSSL_DISPATCH_END
};