Latest update.
This commit is contained in:
@@ -4,7 +4,6 @@
|
||||
$DH_GOAL=../../libimplementations.a
|
||||
$DSA_GOAL=../../libimplementations.a
|
||||
$EC_GOAL=../../libimplementations.a
|
||||
$RSA_GOAL=../../libimplementations.a
|
||||
$ECX_GOAL=../../libimplementations.a
|
||||
|
||||
IF[{- !$disabled{dh} -}]
|
||||
@@ -16,7 +15,9 @@ ENDIF
|
||||
IF[{- !$disabled{ec} -}]
|
||||
SOURCE[$EC_GOAL]=ec_kmgmt.c
|
||||
ENDIF
|
||||
SOURCE[$RSA_GOAL]=rsa_kmgmt.c
|
||||
IF[{- !$disabled{ec} -}]
|
||||
SOURCE[$ECX_GOAL]=ecx_kmgmt.c
|
||||
ENDIF
|
||||
|
||||
SOURCE[../../libfips.a]=rsa_kmgmt.c
|
||||
SOURCE[../../libnonfips.a]=rsa_kmgmt.c
|
||||
@@ -22,7 +22,7 @@
|
||||
#include "prov/providercommon.h"
|
||||
#include "prov/provider_ctx.h"
|
||||
#include "crypto/dh.h"
|
||||
#include "internal/param_build.h"
|
||||
#include "openssl/param_build.h"
|
||||
|
||||
static OSSL_OP_keymgmt_new_fn dh_newdata;
|
||||
static OSSL_OP_keymgmt_free_fn dh_freedata;
|
||||
@@ -39,32 +39,6 @@ static OSSL_OP_keymgmt_export_types_fn dh_export_types;
|
||||
#define DH_POSSIBLE_SELECTIONS \
|
||||
(OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
|
||||
|
||||
static int params_to_domparams(DH *dh, const OSSL_PARAM params[])
|
||||
{
|
||||
const OSSL_PARAM *param_p, *param_g;
|
||||
BIGNUM *p = NULL, *g = NULL;
|
||||
|
||||
if (dh == NULL)
|
||||
return 0;
|
||||
|
||||
param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P);
|
||||
param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G);
|
||||
|
||||
if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
|
||||
|| (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g)))
|
||||
goto err;
|
||||
|
||||
if (!DH_set0_pqg(dh, p, NULL, g))
|
||||
goto err;
|
||||
|
||||
return 1;
|
||||
|
||||
err:
|
||||
BN_free(p);
|
||||
BN_free(g);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int domparams_to_params(DH *dh, OSSL_PARAM_BLD *tmpl)
|
||||
{
|
||||
const BIGNUM *dh_p = NULL, *dh_g = NULL;
|
||||
@@ -74,56 +48,15 @@ static int domparams_to_params(DH *dh, OSSL_PARAM_BLD *tmpl)
|
||||
|
||||
DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
|
||||
if (dh_p != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dh_p))
|
||||
&& !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dh_p))
|
||||
return 0;
|
||||
if (dh_g != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dh_g))
|
||||
&& !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dh_g))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int params_to_key(DH *dh, const OSSL_PARAM params[])
|
||||
{
|
||||
const OSSL_PARAM *param_priv_key, *param_pub_key;
|
||||
BIGNUM *priv_key = NULL, *pub_key = NULL;
|
||||
|
||||
if (dh == NULL)
|
||||
return 0;
|
||||
|
||||
if (!params_to_domparams(dh, params))
|
||||
return 0;
|
||||
|
||||
param_priv_key =
|
||||
OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
|
||||
param_pub_key =
|
||||
OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
|
||||
|
||||
/*
|
||||
* DH documentation says that a public key must be present if a
|
||||
* private key is present.
|
||||
* We want to have at least a public key either way, so we end up
|
||||
* requiring it unconditionally.
|
||||
*/
|
||||
if (param_pub_key == NULL)
|
||||
return 0;
|
||||
|
||||
if ((param_priv_key != NULL
|
||||
&& !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
|
||||
|| !OSSL_PARAM_get_BN(param_pub_key, &pub_key))
|
||||
goto err;
|
||||
|
||||
if (!DH_set0_key(dh, pub_key, priv_key))
|
||||
goto err;
|
||||
|
||||
return 1;
|
||||
|
||||
err:
|
||||
BN_clear_free(priv_key);
|
||||
BN_free(pub_key);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int key_to_params(DH *dh, OSSL_PARAM_BLD *tmpl)
|
||||
{
|
||||
const BIGNUM *priv_key = NULL, *pub_key = NULL;
|
||||
@@ -135,10 +68,10 @@ static int key_to_params(DH *dh, OSSL_PARAM_BLD *tmpl)
|
||||
|
||||
DH_get0_key(dh, &pub_key, &priv_key);
|
||||
if (priv_key != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY, priv_key))
|
||||
&& !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY, priv_key))
|
||||
return 0;
|
||||
if (pub_key != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, pub_key))
|
||||
&& !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, pub_key))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
@@ -159,15 +92,17 @@ static int dh_has(void *keydata, int selection)
|
||||
DH *dh = keydata;
|
||||
int ok = 0;
|
||||
|
||||
if ((selection & DH_POSSIBLE_SELECTIONS) != 0)
|
||||
ok = 1;
|
||||
if (dh != NULL) {
|
||||
if ((selection & DH_POSSIBLE_SELECTIONS) != 0)
|
||||
ok = 1;
|
||||
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -202,9 +137,9 @@ static int dh_import(void *keydata, int selection, const OSSL_PARAM params[])
|
||||
ok = 1;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
|
||||
ok = ok && params_to_domparams(dh, params);
|
||||
ok = ok && ffc_fromdata(dh_get0_params(dh), params);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ok = ok && params_to_key(dh, params);
|
||||
ok = ok && dh_key_fromdata(dh, params);
|
||||
|
||||
return ok;
|
||||
}
|
||||
@@ -213,26 +148,31 @@ static int dh_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
|
||||
void *cbarg)
|
||||
{
|
||||
DH *dh = keydata;
|
||||
OSSL_PARAM_BLD tmpl;
|
||||
OSSL_PARAM_BLD *tmpl;
|
||||
OSSL_PARAM *params = NULL;
|
||||
int ok = 1;
|
||||
|
||||
if (dh == NULL)
|
||||
return 0;
|
||||
|
||||
ossl_param_bld_init(&tmpl);
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
|
||||
ok = ok && domparams_to_params(dh, &tmpl);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ok = ok && key_to_params(dh, &tmpl);
|
||||
|
||||
if (!ok
|
||||
|| (params = ossl_param_bld_to_param(&tmpl)) == NULL)
|
||||
tmpl = OSSL_PARAM_BLD_new();
|
||||
if (tmpl == NULL)
|
||||
return 0;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
|
||||
ok = ok && domparams_to_params(dh, tmpl);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ok = ok && key_to_params(dh, tmpl);
|
||||
|
||||
if (!ok
|
||||
|| (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
|
||||
OSSL_PARAM_BLD_free(tmpl);
|
||||
return 0;
|
||||
}
|
||||
OSSL_PARAM_BLD_free(tmpl);
|
||||
|
||||
ok = param_cb(params, cbarg);
|
||||
ossl_param_bld_free(params);
|
||||
OSSL_PARAM_BLD_free_params(params);
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
#include "prov/providercommon.h"
|
||||
#include "prov/provider_ctx.h"
|
||||
#include "crypto/dsa.h"
|
||||
#include "internal/param_build.h"
|
||||
#include "openssl/param_build.h"
|
||||
|
||||
static OSSL_OP_keymgmt_new_fn dsa_newdata;
|
||||
static OSSL_OP_keymgmt_free_fn dsa_freedata;
|
||||
@@ -39,35 +39,6 @@ static OSSL_OP_keymgmt_export_types_fn dsa_export_types;
|
||||
#define DSA_POSSIBLE_SELECTIONS \
|
||||
(OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
|
||||
|
||||
static int params_to_domparams(DSA *dsa, const OSSL_PARAM params[])
|
||||
{
|
||||
const OSSL_PARAM *param_p, *param_q, *param_g;
|
||||
BIGNUM *p = NULL, *q = NULL, *g = NULL;
|
||||
|
||||
if (dsa == NULL)
|
||||
return 0;
|
||||
|
||||
param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P);
|
||||
param_q = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_Q);
|
||||
param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G);
|
||||
|
||||
if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
|
||||
|| (param_q != NULL && !OSSL_PARAM_get_BN(param_q, &q))
|
||||
|| (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g)))
|
||||
goto err;
|
||||
|
||||
if (!DSA_set0_pqg(dsa, p, q, g))
|
||||
goto err;
|
||||
|
||||
return 1;
|
||||
|
||||
err:
|
||||
BN_free(p);
|
||||
BN_free(q);
|
||||
BN_free(g);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int domparams_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
|
||||
{
|
||||
const BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL;
|
||||
@@ -77,58 +48,18 @@ static int domparams_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
|
||||
|
||||
DSA_get0_pqg(dsa, &dsa_p, &dsa_q, &dsa_g);
|
||||
if (dsa_p != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dsa_p))
|
||||
&& !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dsa_p))
|
||||
return 0;
|
||||
if (dsa_q != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, dsa_q))
|
||||
&& !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, dsa_q))
|
||||
return 0;
|
||||
if (dsa_g != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dsa_g))
|
||||
&& !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dsa_g))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int params_to_key(DSA *dsa, const OSSL_PARAM params[])
|
||||
{
|
||||
const OSSL_PARAM *param_priv_key, *param_pub_key;
|
||||
BIGNUM *priv_key = NULL, *pub_key = NULL;
|
||||
|
||||
if (dsa == NULL)
|
||||
return 0;
|
||||
|
||||
if (!params_to_domparams(dsa, params))
|
||||
return 0;
|
||||
|
||||
param_priv_key =
|
||||
OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
|
||||
param_pub_key =
|
||||
OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
|
||||
|
||||
/*
|
||||
* DSA documentation says that a public key must be present if a private key
|
||||
* is.
|
||||
*/
|
||||
if (param_priv_key != NULL && param_pub_key == NULL)
|
||||
return 0;
|
||||
|
||||
if ((param_priv_key != NULL
|
||||
&& !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
|
||||
|| (param_pub_key != NULL
|
||||
&& !OSSL_PARAM_get_BN(param_pub_key, &pub_key)))
|
||||
goto err;
|
||||
|
||||
if (pub_key != NULL && !DSA_set0_key(dsa, pub_key, priv_key))
|
||||
goto err;
|
||||
|
||||
return 1;
|
||||
|
||||
err:
|
||||
BN_clear_free(priv_key);
|
||||
BN_free(pub_key);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int key_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
|
||||
{
|
||||
const BIGNUM *priv_key = NULL, *pub_key = NULL;
|
||||
@@ -140,10 +71,10 @@ static int key_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
|
||||
|
||||
DSA_get0_key(dsa, &pub_key, &priv_key);
|
||||
if (priv_key != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY, priv_key))
|
||||
&& !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY, priv_key))
|
||||
return 0;
|
||||
if (pub_key != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, pub_key))
|
||||
&& !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, pub_key))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
@@ -164,15 +95,17 @@ static int dsa_has(void *keydata, int selection)
|
||||
DSA *dsa = keydata;
|
||||
int ok = 0;
|
||||
|
||||
if ((selection & DSA_POSSIBLE_SELECTIONS) != 0)
|
||||
ok = 1;
|
||||
if (dsa != NULL) {
|
||||
if ((selection & DSA_POSSIBLE_SELECTIONS) != 0)
|
||||
ok = 1;
|
||||
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -209,9 +142,9 @@ static int dsa_import(void *keydata, int selection, const OSSL_PARAM params[])
|
||||
ok = 1;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
|
||||
ok = ok && params_to_domparams(dsa, params);
|
||||
ok = ok && ffc_fromdata(dsa_get0_params(dsa), params);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ok = ok && params_to_key(dsa, params);
|
||||
ok = ok && dsa_key_fromdata(dsa, params);
|
||||
|
||||
return ok;
|
||||
}
|
||||
@@ -220,26 +153,26 @@ static int dsa_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
|
||||
void *cbarg)
|
||||
{
|
||||
DSA *dsa = keydata;
|
||||
OSSL_PARAM_BLD tmpl;
|
||||
OSSL_PARAM_BLD *tmpl = OSSL_PARAM_BLD_new();
|
||||
OSSL_PARAM *params = NULL;
|
||||
int ok = 1;
|
||||
|
||||
if (dsa == NULL)
|
||||
return 0;
|
||||
|
||||
ossl_param_bld_init(&tmpl);
|
||||
goto err;;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
|
||||
ok = ok && domparams_to_params(dsa, &tmpl);
|
||||
ok = ok && domparams_to_params(dsa, tmpl);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ok = ok && key_to_params(dsa, &tmpl);
|
||||
ok = ok && key_to_params(dsa, tmpl);
|
||||
|
||||
if (!ok
|
||||
|| (params = ossl_param_bld_to_param(&tmpl)) == NULL)
|
||||
return 0;
|
||||
|| (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
|
||||
goto err;;
|
||||
|
||||
ok = param_cb(params, cbarg);
|
||||
ossl_param_bld_free(params);
|
||||
OSSL_PARAM_BLD_free_params(params);
|
||||
err:
|
||||
OSSL_PARAM_BLD_free(tmpl);
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,15 +16,25 @@
|
||||
#include <openssl/core_numbers.h>
|
||||
#include <openssl/core_names.h>
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/ec.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/objects.h>
|
||||
#include <openssl/params.h>
|
||||
#include "crypto/bn.h"
|
||||
#include "internal/param_build.h"
|
||||
#include "crypto/ec.h"
|
||||
#include "prov/implementations.h"
|
||||
#include "prov/providercommon.h"
|
||||
#include "prov/providercommonerr.h"
|
||||
#include "prov/provider_ctx.h"
|
||||
#include "internal/param_build_set.h"
|
||||
|
||||
static OSSL_OP_keymgmt_new_fn ec_newdata;
|
||||
static OSSL_OP_keymgmt_gen_init_fn ec_gen_init;
|
||||
static OSSL_OP_keymgmt_gen_set_template_fn ec_gen_set_template;
|
||||
static OSSL_OP_keymgmt_gen_set_params_fn ec_gen_set_params;
|
||||
static OSSL_OP_keymgmt_gen_settable_params_fn ec_gen_settable_params;
|
||||
static OSSL_OP_keymgmt_gen_get_params_fn ec_gen_get_params;
|
||||
static OSSL_OP_keymgmt_gen_gettable_params_fn ec_gen_gettable_params;
|
||||
static OSSL_OP_keymgmt_gen_fn ec_gen;
|
||||
static OSSL_OP_keymgmt_gen_cleanup_fn ec_gen_cleanup;
|
||||
static OSSL_OP_keymgmt_free_fn ec_freedata;
|
||||
static OSSL_OP_keymgmt_get_params_fn ec_get_params;
|
||||
static OSSL_OP_keymgmt_gettable_params_fn ec_gettable_params;
|
||||
@@ -32,14 +42,15 @@ static OSSL_OP_keymgmt_set_params_fn ec_set_params;
|
||||
static OSSL_OP_keymgmt_settable_params_fn ec_settable_params;
|
||||
static OSSL_OP_keymgmt_has_fn ec_has;
|
||||
static OSSL_OP_keymgmt_match_fn ec_match;
|
||||
static OSSL_OP_keymgmt_validate_fn ec_validate;
|
||||
static OSSL_OP_keymgmt_import_fn ec_import;
|
||||
static OSSL_OP_keymgmt_import_types_fn ec_import_types;
|
||||
static OSSL_OP_keymgmt_export_fn ec_export;
|
||||
static OSSL_OP_keymgmt_export_types_fn ec_export_types;
|
||||
static OSSL_OP_keymgmt_query_operation_name_fn ec_query_operation_name;
|
||||
|
||||
#define EC_POSSIBLE_SELECTIONS \
|
||||
(OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS )
|
||||
#define EC_POSSIBLE_SELECTIONS \
|
||||
(OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
|
||||
|
||||
static
|
||||
const char *ec_query_operation_name(int operation_id)
|
||||
@@ -47,64 +58,15 @@ const char *ec_query_operation_name(int operation_id)
|
||||
switch (operation_id) {
|
||||
case OSSL_OP_KEYEXCH:
|
||||
return "ECDH";
|
||||
#if 0
|
||||
case OSSL_OP_SIGNATURE:
|
||||
return deflt_signature;
|
||||
#endif
|
||||
return "ECDSA";
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static ossl_inline
|
||||
int params_to_domparams(EC_KEY *ec, const OSSL_PARAM params[])
|
||||
{
|
||||
const OSSL_PARAM *param_ec_name;
|
||||
EC_GROUP *ecg = NULL;
|
||||
char *curve_name = NULL;
|
||||
int ok = 0;
|
||||
|
||||
if (ec == NULL)
|
||||
return 0;
|
||||
|
||||
param_ec_name = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_NAME);
|
||||
if (param_ec_name == NULL) {
|
||||
/* explicit parameters */
|
||||
|
||||
/*
|
||||
* TODO(3.0): should we support explicit parameters curves?
|
||||
*/
|
||||
return 0;
|
||||
} else {
|
||||
/* named curve */
|
||||
int curve_nid;
|
||||
|
||||
if (!OSSL_PARAM_get_utf8_string(param_ec_name, &curve_name, 0)
|
||||
|| curve_name == NULL
|
||||
|| (curve_nid = OBJ_sn2nid(curve_name)) == NID_undef)
|
||||
goto err;
|
||||
|
||||
if ((ecg = EC_GROUP_new_by_curve_name(curve_nid)) == NULL)
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!EC_KEY_set_group(ec, ecg))
|
||||
goto err;
|
||||
|
||||
/*
|
||||
* TODO(3.0): if the group has changed, should we invalidate the private and
|
||||
* public key?
|
||||
*/
|
||||
|
||||
ok = 1;
|
||||
|
||||
err:
|
||||
OPENSSL_free(curve_name);
|
||||
EC_GROUP_free(ecg);
|
||||
return ok;
|
||||
}
|
||||
|
||||
static ossl_inline
|
||||
int domparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl)
|
||||
int domparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl,
|
||||
OSSL_PARAM params[])
|
||||
{
|
||||
const EC_GROUP *ecg;
|
||||
int curve_nid;
|
||||
@@ -119,135 +81,24 @@ int domparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl)
|
||||
curve_nid = EC_GROUP_get_curve_name(ecg);
|
||||
|
||||
if (curve_nid == NID_undef) {
|
||||
/* explicit parameters */
|
||||
|
||||
/*
|
||||
* TODO(3.0): should we support explicit parameters curves?
|
||||
*/
|
||||
/* TODO(3.0): should we support explicit parameters curves? */
|
||||
return 0;
|
||||
} else {
|
||||
/* named curve */
|
||||
const char *curve_name = NULL;
|
||||
|
||||
if ((curve_name = OBJ_nid2sn(curve_nid)) == NULL)
|
||||
if ((curve_name = ec_curve_nid2name(curve_nid)) == NULL)
|
||||
return 0;
|
||||
if (!ossl_param_build_set_utf8_string(tmpl, params,
|
||||
OSSL_PKEY_PARAM_EC_NAME,
|
||||
curve_name))
|
||||
|
||||
if (!ossl_param_bld_push_utf8_string(tmpl, OSSL_PKEY_PARAM_EC_NAME, curve_name, 0))
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Callers of params_to_key MUST make sure that params_to_domparams has been
|
||||
* called before!
|
||||
*
|
||||
* This function only imports the bare keypair, domain parameters and other
|
||||
* parameters are imported separately, and domain parameters are required to
|
||||
* define a keypair.
|
||||
*/
|
||||
static ossl_inline
|
||||
int params_to_key(EC_KEY *ec, const OSSL_PARAM params[], int include_private)
|
||||
{
|
||||
const OSSL_PARAM *param_priv_key, *param_pub_key;
|
||||
BIGNUM *priv_key = NULL;
|
||||
unsigned char *pub_key = NULL;
|
||||
size_t pub_key_len;
|
||||
const EC_GROUP *ecg = NULL;
|
||||
EC_POINT *pub_point = NULL;
|
||||
int ok = 0;
|
||||
|
||||
ecg = EC_KEY_get0_group(ec);
|
||||
if (ecg == NULL)
|
||||
return 0;
|
||||
|
||||
param_priv_key =
|
||||
OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
|
||||
param_pub_key =
|
||||
OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
|
||||
|
||||
/*
|
||||
* We want to have at least a public key either way, so we end up
|
||||
* requiring it unconditionally.
|
||||
*/
|
||||
if (param_pub_key == NULL
|
||||
|| !OSSL_PARAM_get_octet_string(param_pub_key,
|
||||
(void **)&pub_key, 0, &pub_key_len)
|
||||
|| (pub_point = EC_POINT_new(ecg)) == NULL
|
||||
|| !EC_POINT_oct2point(ecg, pub_point,
|
||||
pub_key, pub_key_len, NULL))
|
||||
goto err;
|
||||
|
||||
if (param_priv_key != NULL && include_private) {
|
||||
int fixed_top;
|
||||
const BIGNUM *order;
|
||||
|
||||
/*
|
||||
* Key import/export should never leak the bit length of the secret
|
||||
* scalar in the key.
|
||||
*
|
||||
* For this reason, on export we use padded BIGNUMs with fixed length.
|
||||
*
|
||||
* When importing we also should make sure that, even if short lived,
|
||||
* the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
|
||||
* soon as possible, so that any processing of this BIGNUM might opt for
|
||||
* constant time implementations in the backend.
|
||||
*
|
||||
* Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
|
||||
* to preallocate the BIGNUM internal buffer to a fixed public size big
|
||||
* enough that operations performed during the processing never trigger
|
||||
* a realloc which would leak the size of the scalar through memory
|
||||
* accesses.
|
||||
*
|
||||
* Fixed Length
|
||||
* ------------
|
||||
*
|
||||
* The order of the large prime subgroup of the curve is our choice for
|
||||
* a fixed public size, as that is generally the upper bound for
|
||||
* generating a private key in EC cryptosystems and should fit all valid
|
||||
* secret scalars.
|
||||
*
|
||||
* For padding on export we just use the bit length of the order
|
||||
* converted to bytes (rounding up).
|
||||
*
|
||||
* For preallocating the BIGNUM storage we look at the number of "words"
|
||||
* required for the internal representation of the order, and we
|
||||
* preallocate 2 extra "words" in case any of the subsequent processing
|
||||
* might temporarily overflow the order length.
|
||||
*/
|
||||
order = EC_GROUP_get0_order(ecg);
|
||||
if (order == NULL || BN_is_zero(order))
|
||||
goto err;
|
||||
|
||||
fixed_top = bn_get_top(order) + 2;
|
||||
|
||||
if ((priv_key = BN_new()) == NULL)
|
||||
goto err;
|
||||
if (bn_wexpand(priv_key, fixed_top) == NULL)
|
||||
goto err;
|
||||
BN_set_flags(priv_key, BN_FLG_CONSTTIME);
|
||||
|
||||
if (!OSSL_PARAM_get_BN(param_priv_key, &priv_key))
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (priv_key != NULL
|
||||
&& !EC_KEY_set_private_key(ec, priv_key))
|
||||
goto err;
|
||||
|
||||
if (!EC_KEY_set_public_key(ec, pub_point))
|
||||
goto err;
|
||||
|
||||
ok = 1;
|
||||
|
||||
err:
|
||||
BN_clear_free(priv_key);
|
||||
OPENSSL_free(pub_key);
|
||||
EC_POINT_free(pub_point);
|
||||
return ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* Callers of key_to_params MUST make sure that domparams_to_params is also
|
||||
* called!
|
||||
@@ -256,34 +107,33 @@ int params_to_key(EC_KEY *ec, const OSSL_PARAM params[], int include_private)
|
||||
* parameters are exported separately.
|
||||
*/
|
||||
static ossl_inline
|
||||
int key_to_params(const EC_KEY *eckey, OSSL_PARAM_BLD *tmpl, int include_private)
|
||||
int key_to_params(const EC_KEY *eckey, OSSL_PARAM_BLD *tmpl,
|
||||
OSSL_PARAM params[], int include_private,
|
||||
unsigned char **pub_key)
|
||||
{
|
||||
const BIGNUM *priv_key = NULL;
|
||||
const EC_POINT *pub_point = NULL;
|
||||
const EC_GROUP *ecg = NULL;
|
||||
unsigned char *pub_key = NULL;
|
||||
size_t pub_key_len = 0;
|
||||
int ret = 0;
|
||||
|
||||
if (eckey == NULL)
|
||||
if (eckey == NULL
|
||||
|| (ecg = EC_KEY_get0_group(eckey)) == NULL)
|
||||
return 0;
|
||||
|
||||
ecg = EC_KEY_get0_group(eckey);
|
||||
priv_key = EC_KEY_get0_private_key(eckey);
|
||||
pub_point = EC_KEY_get0_public_key(eckey);
|
||||
|
||||
/* group and public_key must be present, priv_key is optional */
|
||||
if (ecg == NULL || pub_point == NULL)
|
||||
return 0;
|
||||
if ((pub_key_len = EC_POINT_point2buf(ecg, pub_point,
|
||||
POINT_CONVERSION_COMPRESSED,
|
||||
&pub_key, NULL)) == 0)
|
||||
return 0;
|
||||
|
||||
if (!ossl_param_bld_push_octet_string(tmpl,
|
||||
OSSL_PKEY_PARAM_PUB_KEY,
|
||||
pub_key, pub_key_len))
|
||||
goto err;
|
||||
if (pub_point != NULL) {
|
||||
/* convert pub_point to a octet string according to the SECG standard */
|
||||
if ((pub_key_len = EC_POINT_point2buf(ecg, pub_point,
|
||||
POINT_CONVERSION_COMPRESSED,
|
||||
pub_key, NULL)) == 0
|
||||
|| !ossl_param_build_set_octet_string(tmpl, params,
|
||||
OSSL_PKEY_PARAM_PUB_KEY,
|
||||
*pub_key, pub_key_len))
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (priv_key != NULL && include_private) {
|
||||
size_t sz;
|
||||
@@ -326,71 +176,20 @@ int key_to_params(const EC_KEY *eckey, OSSL_PARAM_BLD *tmpl, int include_private
|
||||
if (ecbits <= 0)
|
||||
goto err;
|
||||
sz = (ecbits + 7 ) / 8;
|
||||
if (!ossl_param_bld_push_BN_pad(tmpl,
|
||||
OSSL_PKEY_PARAM_PRIV_KEY,
|
||||
priv_key, sz))
|
||||
|
||||
if (!ossl_param_build_set_bn_pad(tmpl, params,
|
||||
OSSL_PKEY_PARAM_PRIV_KEY,
|
||||
priv_key, sz))
|
||||
goto err;
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
|
||||
err:
|
||||
OPENSSL_free(pub_key);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ossl_inline
|
||||
int ec_set_param_ecdh_cofactor_mode(EC_KEY *ec, const OSSL_PARAM *p)
|
||||
{
|
||||
const EC_GROUP *ecg = EC_KEY_get0_group(ec);
|
||||
const BIGNUM *cofactor;
|
||||
int mode;
|
||||
|
||||
if (!OSSL_PARAM_get_int(p, &mode))
|
||||
return 0;
|
||||
|
||||
/*
|
||||
* mode can be only 0 for disable, or 1 for enable here.
|
||||
*
|
||||
* This is in contrast with the same parameter on an ECDH EVP_PKEY_CTX that
|
||||
* also supports mode == -1 with the meaning of "reset to the default for
|
||||
* the associated key".
|
||||
*/
|
||||
if (mode < 0 || mode > 1)
|
||||
return 0;
|
||||
|
||||
if ((cofactor = EC_GROUP_get0_cofactor(ecg)) == NULL )
|
||||
return 0;
|
||||
|
||||
/* ECDH cofactor mode has no effect if cofactor is 1 */
|
||||
if (BN_is_one(cofactor))
|
||||
return 1;
|
||||
|
||||
if (mode == 1)
|
||||
EC_KEY_set_flags(ec, EC_FLAG_COFACTOR_ECDH);
|
||||
else if (mode == 0)
|
||||
EC_KEY_clear_flags(ec, EC_FLAG_COFACTOR_ECDH);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static ossl_inline
|
||||
int params_to_otherparams(EC_KEY *ec, const OSSL_PARAM params[])
|
||||
{
|
||||
const OSSL_PARAM *p;
|
||||
|
||||
if (ec == NULL)
|
||||
return 0;
|
||||
|
||||
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
|
||||
if (p != NULL && !ec_set_param_ecdh_cofactor_mode(ec, p))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static ossl_inline
|
||||
int otherparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl)
|
||||
int otherparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl,
|
||||
OSSL_PARAM params[])
|
||||
{
|
||||
int ecdh_cofactor_mode = 0;
|
||||
|
||||
@@ -399,18 +198,15 @@ int otherparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl)
|
||||
|
||||
ecdh_cofactor_mode =
|
||||
(EC_KEY_get_flags(ec) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
|
||||
if (!ossl_param_bld_push_int(tmpl,
|
||||
OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
|
||||
ecdh_cofactor_mode))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
return ossl_param_build_set_int(tmpl, params,
|
||||
OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
|
||||
ecdh_cofactor_mode);
|
||||
}
|
||||
|
||||
static
|
||||
void *ec_newdata(void *provctx)
|
||||
{
|
||||
return EC_KEY_new();
|
||||
return EC_KEY_new_ex(PROV_LIBRARY_CONTEXT_OF(provctx));
|
||||
}
|
||||
|
||||
static
|
||||
@@ -425,21 +221,22 @@ int ec_has(void *keydata, int selection)
|
||||
EC_KEY *ec = keydata;
|
||||
int ok = 0;
|
||||
|
||||
if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
|
||||
ok = 1;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
|
||||
ok = ok && (EC_KEY_get0_public_key(ec) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
||||
ok = ok && (EC_KEY_get0_private_key(ec) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
|
||||
ok = ok && (EC_KEY_get0_group(ec) != NULL);
|
||||
/*
|
||||
* We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be available,
|
||||
* so no extra check is needed other than the previous one against
|
||||
* EC_POSSIBLE_SELECTIONS.
|
||||
*/
|
||||
if (ec != NULL) {
|
||||
if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
|
||||
ok = 1;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
|
||||
ok = ok && (EC_KEY_get0_public_key(ec) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
||||
ok = ok && (EC_KEY_get0_private_key(ec) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
|
||||
ok = ok && (EC_KEY_get0_group(ec) != NULL);
|
||||
/*
|
||||
* We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be
|
||||
* available, so no extra check is needed other than the previous one
|
||||
* against EC_POSSIBLE_SELECTIONS.
|
||||
*/
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
@@ -501,15 +298,15 @@ int ec_import(void *keydata, int selection, const OSSL_PARAM params[])
|
||||
return 0;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
|
||||
ok = ok && params_to_domparams(ec, params);
|
||||
ok = ok && ec_key_domparams_fromdata(ec, params);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
|
||||
int include_private =
|
||||
selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
|
||||
|
||||
ok = ok && params_to_key(ec, params, include_private);
|
||||
ok = ok && ec_key_fromdata(ec, params, include_private);
|
||||
}
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
|
||||
ok = ok && params_to_otherparams(ec, params);
|
||||
ok = ok && ec_key_otherparams_fromdata(ec, params);
|
||||
|
||||
return ok;
|
||||
}
|
||||
@@ -519,8 +316,9 @@ int ec_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
|
||||
void *cbarg)
|
||||
{
|
||||
EC_KEY *ec = keydata;
|
||||
OSSL_PARAM_BLD tmpl;
|
||||
OSSL_PARAM_BLD *tmpl;
|
||||
OSSL_PARAM *params = NULL;
|
||||
unsigned char *pub_key = NULL;
|
||||
int ok = 1;
|
||||
|
||||
if (ec == NULL)
|
||||
@@ -548,25 +346,28 @@ int ec_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
|
||||
&& (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
|
||||
return 0;
|
||||
|
||||
ossl_param_bld_init(&tmpl);
|
||||
tmpl = OSSL_PARAM_BLD_new();
|
||||
if (tmpl == NULL)
|
||||
return 0;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
|
||||
ok = ok && domparams_to_params(ec, &tmpl);
|
||||
ok = ok && domparams_to_params(ec, tmpl, NULL);
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
|
||||
int include_private =
|
||||
selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
|
||||
|
||||
ok = ok && key_to_params(ec, &tmpl, include_private);
|
||||
ok = ok && key_to_params(ec, tmpl, NULL, include_private, &pub_key);
|
||||
}
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
|
||||
ok = ok && otherparams_to_params(ec, &tmpl);
|
||||
ok = ok && otherparams_to_params(ec, tmpl, NULL);
|
||||
|
||||
if (!ok
|
||||
|| (params = ossl_param_bld_to_param(&tmpl)) == NULL)
|
||||
return 0;
|
||||
if (ok && (params = OSSL_PARAM_BLD_to_param(tmpl)) != NULL)
|
||||
ok = param_cb(params, cbarg);
|
||||
|
||||
ok = param_cb(params, cbarg);
|
||||
ossl_param_bld_free(params);
|
||||
OSSL_PARAM_BLD_free_params(params);
|
||||
OSSL_PARAM_BLD_free(tmpl);
|
||||
OPENSSL_free(pub_key);
|
||||
return ok;
|
||||
}
|
||||
|
||||
@@ -626,9 +427,11 @@ const OSSL_PARAM *ec_export_types(int selection)
|
||||
static
|
||||
int ec_get_params(void *key, OSSL_PARAM params[])
|
||||
{
|
||||
int ret;
|
||||
EC_KEY *eck = key;
|
||||
const EC_GROUP *ecg = NULL;
|
||||
OSSL_PARAM *p;
|
||||
unsigned char *pub_key = NULL;
|
||||
|
||||
ecg = EC_KEY_get0_group(eck);
|
||||
if (ecg == NULL)
|
||||
@@ -688,15 +491,21 @@ int ec_get_params(void *key, OSSL_PARAM params[])
|
||||
if (!OSSL_PARAM_set_int(p, ecdh_cofactor_mode))
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
ret = domparams_to_params(eck, NULL, params)
|
||||
&& key_to_params(eck, NULL, params, 1, &pub_key)
|
||||
&& otherparams_to_params(eck, NULL, params);
|
||||
OPENSSL_free(pub_key);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM ec_known_gettable_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_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
|
||||
EC_IMEXPORTABLE_DOM_PARAMETERS,
|
||||
EC_IMEXPORTABLE_PUBLIC_KEY,
|
||||
EC_IMEXPORTABLE_PRIVATE_KEY,
|
||||
EC_IMEXPORTABLE_OTHER_PARAMETERS,
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
|
||||
@@ -730,8 +539,224 @@ int ec_set_params(void *key, const OSSL_PARAM params[])
|
||||
return 1;
|
||||
}
|
||||
|
||||
static
|
||||
int ec_validate(void *keydata, int selection)
|
||||
{
|
||||
EC_KEY *eck = keydata;
|
||||
int ok = 0;
|
||||
BN_CTX *ctx = BN_CTX_new_ex(ec_key_get_libctx(eck));
|
||||
|
||||
if (ctx == NULL)
|
||||
return 0;
|
||||
|
||||
if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
|
||||
ok = 1;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
|
||||
ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx);
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
|
||||
ok = ok && ec_key_public_check(eck, ctx);
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
||||
ok = ok && ec_key_private_check(eck);
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
|
||||
ok = ok && ec_key_pairwise_check(eck, ctx);
|
||||
|
||||
BN_CTX_free(ctx);
|
||||
return ok;
|
||||
}
|
||||
|
||||
struct ec_gen_ctx {
|
||||
OPENSSL_CTX *libctx;
|
||||
|
||||
EC_GROUP *gen_group;
|
||||
int selection;
|
||||
};
|
||||
|
||||
static void *ec_gen_init(void *provctx, int selection)
|
||||
{
|
||||
OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
|
||||
struct ec_gen_ctx *gctx = NULL;
|
||||
|
||||
if ((selection & (EC_POSSIBLE_SELECTIONS)) == 0)
|
||||
return NULL;
|
||||
|
||||
if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
|
||||
gctx->libctx = libctx;
|
||||
gctx->gen_group = NULL;
|
||||
gctx->selection = selection;
|
||||
}
|
||||
return gctx;
|
||||
}
|
||||
|
||||
static int ec_gen_set_group(void *genctx, int nid)
|
||||
{
|
||||
struct ec_gen_ctx *gctx = genctx;
|
||||
EC_GROUP *group;
|
||||
|
||||
group = EC_GROUP_new_by_curve_name_ex(gctx->libctx, nid);
|
||||
if (group == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CURVE);
|
||||
return 0;
|
||||
}
|
||||
EC_GROUP_free(gctx->gen_group);
|
||||
gctx->gen_group = group;
|
||||
return 1;
|
||||
}
|
||||
static int ec_gen_set_template(void *genctx, void *templ)
|
||||
{
|
||||
struct ec_gen_ctx *gctx = genctx;
|
||||
EC_KEY *ec = templ;
|
||||
const EC_GROUP *ec_group;
|
||||
|
||||
if (gctx == NULL || ec == NULL)
|
||||
return 0;
|
||||
if ((ec_group = EC_KEY_get0_group(ec)) == NULL)
|
||||
return 0;
|
||||
return ec_gen_set_group(gctx, EC_GROUP_get_curve_name(ec_group));
|
||||
}
|
||||
|
||||
static int ec_gen_set_params(void *genctx, const OSSL_PARAM params[])
|
||||
{
|
||||
struct ec_gen_ctx *gctx = genctx;
|
||||
const OSSL_PARAM *p;
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_NAME))
|
||||
!= NULL) {
|
||||
const char *curve_name = NULL;
|
||||
int ret = 0;
|
||||
|
||||
switch (p->data_type) {
|
||||
case OSSL_PARAM_UTF8_STRING:
|
||||
/* The OSSL_PARAM functions have no support for this */
|
||||
curve_name = p->data;
|
||||
ret = (curve_name != NULL);
|
||||
break;
|
||||
case OSSL_PARAM_UTF8_PTR:
|
||||
ret = OSSL_PARAM_get_utf8_ptr(p, &curve_name);
|
||||
break;
|
||||
}
|
||||
|
||||
if (ret) {
|
||||
int nid = ec_curve_name2nid(curve_name);
|
||||
|
||||
if (nid == NID_undef) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CURVE);
|
||||
ret = 0;
|
||||
} else {
|
||||
ret = ec_gen_set_group(gctx, nid);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM *ec_gen_settable_params(void *provctx)
|
||||
{
|
||||
static OSSL_PARAM settable[] = {
|
||||
{ OSSL_PKEY_PARAM_EC_NAME, OSSL_PARAM_UTF8_STRING, NULL, 0, 0 },
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
|
||||
return settable;
|
||||
}
|
||||
|
||||
static int ec_gen_get_params(void *genctx, OSSL_PARAM params[])
|
||||
{
|
||||
struct ec_gen_ctx *gctx = genctx;
|
||||
OSSL_PARAM *p;
|
||||
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_EC_NAME)) != NULL) {
|
||||
int nid = EC_GROUP_get_curve_name(gctx->gen_group);
|
||||
int ret = 0;
|
||||
const char *curve_name = ec_curve_nid2name(nid);
|
||||
|
||||
switch (p->data_type) {
|
||||
case OSSL_PARAM_UTF8_STRING:
|
||||
ret = OSSL_PARAM_set_utf8_string(p, curve_name);
|
||||
break;
|
||||
case OSSL_PARAM_UTF8_PTR:
|
||||
ret = OSSL_PARAM_set_utf8_ptr(p, curve_name);
|
||||
break;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM *ec_gen_gettable_params(void *provctx)
|
||||
{
|
||||
static OSSL_PARAM gettable[] = {
|
||||
{ OSSL_PKEY_PARAM_EC_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
|
||||
return gettable;
|
||||
}
|
||||
|
||||
static int ec_gen_assign_group(EC_KEY *ec, EC_GROUP *group)
|
||||
{
|
||||
if (group == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_NO_PARAMETERS_SET);
|
||||
return 0;
|
||||
}
|
||||
return EC_KEY_set_group(ec, group) > 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
|
||||
*/
|
||||
static void *ec_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
|
||||
{
|
||||
struct ec_gen_ctx *gctx = genctx;
|
||||
EC_KEY *ec = NULL;
|
||||
int ret = 1; /* Start optimistically */
|
||||
|
||||
if (gctx == NULL
|
||||
|| (ec = EC_KEY_new_ex(gctx->libctx)) == NULL)
|
||||
return NULL;
|
||||
|
||||
/* We must always assign a group, no matter what */
|
||||
ret = ec_gen_assign_group(ec, gctx->gen_group);
|
||||
/* Whether you want it or not, you get a keypair, not just one half */
|
||||
if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ret = ret && EC_KEY_generate_key(ec);
|
||||
|
||||
if (ret)
|
||||
return ec;
|
||||
|
||||
/* Something went wrong, throw the key away */
|
||||
EC_KEY_free(ec);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void ec_gen_cleanup(void *genctx)
|
||||
{
|
||||
struct ec_gen_ctx *gctx = genctx;
|
||||
|
||||
if (gctx == NULL)
|
||||
return;
|
||||
|
||||
EC_GROUP_free(gctx->gen_group);
|
||||
OPENSSL_free(gctx);
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH ec_keymgmt_functions[] = {
|
||||
{ OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ec_newdata },
|
||||
{ OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))ec_gen_init },
|
||||
{ OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE,
|
||||
(void (*)(void))ec_gen_set_template },
|
||||
{ OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ec_gen_set_params },
|
||||
{ OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
|
||||
(void (*)(void))ec_gen_settable_params },
|
||||
{ OSSL_FUNC_KEYMGMT_GEN_GET_PARAMS, (void (*)(void))ec_gen_get_params },
|
||||
{ OSSL_FUNC_KEYMGMT_GEN_GETTABLE_PARAMS,
|
||||
(void (*)(void))ec_gen_gettable_params },
|
||||
{ OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))ec_gen },
|
||||
{ OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ec_gen_cleanup },
|
||||
{ OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata },
|
||||
{ OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))ec_get_params },
|
||||
{ OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ec_gettable_params },
|
||||
@@ -739,11 +764,12 @@ const OSSL_DISPATCH ec_keymgmt_functions[] = {
|
||||
{ OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))ec_settable_params },
|
||||
{ OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has },
|
||||
{ OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match },
|
||||
{ OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))ec_validate },
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ec_import },
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types },
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export },
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types },
|
||||
{ OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
|
||||
(void (*)(void))ec_query_operation_name },
|
||||
(void (*)(void))ec_query_operation_name },
|
||||
{ 0, NULL }
|
||||
};
|
||||
@@ -10,16 +10,19 @@
|
||||
#include <assert.h>
|
||||
#include <openssl/core_numbers.h>
|
||||
#include <openssl/core_names.h>
|
||||
#include <openssl/params.h>
|
||||
#include "internal/param_build.h"
|
||||
#include "crypto/ecx.h"
|
||||
#include "prov/implementations.h"
|
||||
#include "prov/providercommon.h"
|
||||
#include "internal/param_build_set.h"
|
||||
|
||||
static OSSL_OP_keymgmt_new_fn x25519_new_key;
|
||||
static OSSL_OP_keymgmt_new_fn x448_new_key;
|
||||
static OSSL_OP_keymgmt_new_fn ed25519_new_key;
|
||||
static OSSL_OP_keymgmt_new_fn ed448_new_key;
|
||||
static OSSL_OP_keymgmt_get_params_fn x25519_get_params;
|
||||
static OSSL_OP_keymgmt_get_params_fn x448_get_params;
|
||||
static OSSL_OP_keymgmt_get_params_fn ed25519_get_params;
|
||||
static OSSL_OP_keymgmt_get_params_fn ed448_get_params;
|
||||
static OSSL_OP_keymgmt_gettable_params_fn ecx_gettable_params;
|
||||
static OSSL_OP_keymgmt_has_fn ecx_has;
|
||||
static OSSL_OP_keymgmt_import_fn ecx_import;
|
||||
@@ -31,37 +34,47 @@ static OSSL_OP_keymgmt_export_types_fn ecx_imexport_types;
|
||||
|
||||
static void *x25519_new_key(void *provctx)
|
||||
{
|
||||
return ecx_key_new(X25519_KEYLEN, 0);
|
||||
return ecx_key_new(ECX_KEY_TYPE_X25519, 0);
|
||||
}
|
||||
|
||||
static void *x448_new_key(void *provctx)
|
||||
{
|
||||
return ecx_key_new(X448_KEYLEN, 0);
|
||||
return ecx_key_new(ECX_KEY_TYPE_X448, 0);
|
||||
}
|
||||
|
||||
static void *ed25519_new_key(void *provctx)
|
||||
{
|
||||
return ecx_key_new(ECX_KEY_TYPE_ED25519, 0);
|
||||
}
|
||||
|
||||
static void *ed448_new_key(void *provctx)
|
||||
{
|
||||
return ecx_key_new(ECX_KEY_TYPE_ED448, 0);
|
||||
}
|
||||
|
||||
static int ecx_has(void *keydata, int selection)
|
||||
{
|
||||
ECX_KEY *key = keydata;
|
||||
int ok = 1;
|
||||
int ok = 0;
|
||||
|
||||
if ((selection & ECX_POSSIBLE_SELECTIONS) == 0)
|
||||
return 0;
|
||||
if (key != NULL) {
|
||||
if ((selection & ECX_POSSIBLE_SELECTIONS) != 0)
|
||||
ok = 1;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
|
||||
ok = ok && key->haspubkey;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
||||
ok = ok && key->privkey != NULL;
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
|
||||
ok = ok && key->haspubkey;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
||||
ok = ok && key->privkey != NULL;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int ecx_import(void *keydata, int selection, const OSSL_PARAM params[])
|
||||
{
|
||||
ECX_KEY *key = keydata;
|
||||
size_t privkeylen = 0, pubkeylen;
|
||||
const OSSL_PARAM *param_priv_key = NULL, *param_pub_key;
|
||||
unsigned char *pubkey;
|
||||
int ok = 1;
|
||||
int include_private = 0;
|
||||
|
||||
if (key == NULL)
|
||||
return 0;
|
||||
@@ -69,52 +82,28 @@ static int ecx_import(void *keydata, int selection, const OSSL_PARAM params[])
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
|
||||
return 0;
|
||||
|
||||
param_pub_key =
|
||||
OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
|
||||
include_private = ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ok = ok && ecx_key_fromdata(key, params, include_private);
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
||||
param_priv_key =
|
||||
OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
|
||||
/*
|
||||
* If a private key is present then a public key must also be present.
|
||||
* Alternatively we've just got a public key.
|
||||
*/
|
||||
if (param_pub_key == NULL)
|
||||
return 0;
|
||||
|
||||
if (param_priv_key != NULL
|
||||
&& !OSSL_PARAM_get_octet_string(param_priv_key,
|
||||
(void **)&key->privkey, key->keylen,
|
||||
&privkeylen))
|
||||
return 0;
|
||||
|
||||
pubkey = key->pubkey;
|
||||
if (!OSSL_PARAM_get_octet_string(param_pub_key,
|
||||
(void **)&pubkey,
|
||||
sizeof(key->pubkey), &pubkeylen))
|
||||
return 0;
|
||||
|
||||
if (pubkeylen != key->keylen
|
||||
|| (param_priv_key != NULL && privkeylen != key->keylen))
|
||||
return 0;
|
||||
|
||||
key->haspubkey = 1;
|
||||
|
||||
return 1;
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int key_to_params(ECX_KEY *key, OSSL_PARAM_BLD *tmpl)
|
||||
static int key_to_params(ECX_KEY *key, OSSL_PARAM_BLD *tmpl,
|
||||
OSSL_PARAM params[])
|
||||
{
|
||||
if (key == NULL)
|
||||
return 0;
|
||||
|
||||
if (!ossl_param_bld_push_octet_string(tmpl, OSSL_PKEY_PARAM_PUB_KEY,
|
||||
key->pubkey, key->keylen))
|
||||
if (!ossl_param_build_set_octet_string(tmpl, params,
|
||||
OSSL_PKEY_PARAM_PUB_KEY,
|
||||
key->pubkey, key->keylen))
|
||||
return 0;
|
||||
|
||||
if (key->privkey != NULL
|
||||
&& !ossl_param_bld_push_octet_string(tmpl, OSSL_PKEY_PARAM_PRIV_KEY,
|
||||
key->privkey, key->keylen))
|
||||
&& !ossl_param_build_set_octet_string(tmpl, params,
|
||||
OSSL_PKEY_PARAM_PRIV_KEY,
|
||||
key->privkey, key->keylen))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
@@ -124,32 +113,42 @@ static int ecx_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
|
||||
void *cbarg)
|
||||
{
|
||||
ECX_KEY *key = keydata;
|
||||
OSSL_PARAM_BLD tmpl;
|
||||
OSSL_PARAM_BLD *tmpl;
|
||||
OSSL_PARAM *params = NULL;
|
||||
int ret;
|
||||
int ret = 0;
|
||||
|
||||
if (key == NULL)
|
||||
return 0;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0
|
||||
&& !key_to_params(key, &tmpl))
|
||||
tmpl = OSSL_PARAM_BLD_new();
|
||||
if (tmpl == NULL)
|
||||
return 0;
|
||||
|
||||
ossl_param_bld_init(&tmpl);
|
||||
params = ossl_param_bld_to_param(&tmpl);
|
||||
if (params == NULL) {
|
||||
ossl_param_bld_free(params);
|
||||
return 0;
|
||||
}
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0
|
||||
&& !key_to_params(key, tmpl, NULL))
|
||||
goto err;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 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_bld_free(params);
|
||||
OSSL_PARAM_BLD_free_params(params);
|
||||
err:
|
||||
OSSL_PARAM_BLD_free(tmpl);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define ECX_KEY_TYPES() \
|
||||
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0), \
|
||||
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
|
||||
|
||||
static const OSSL_PARAM ecx_key_types[] = {
|
||||
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0),
|
||||
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
|
||||
ECX_KEY_TYPES(),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
static const OSSL_PARAM *ecx_imexport_types(int selection)
|
||||
@@ -159,9 +158,10 @@ static const OSSL_PARAM *ecx_imexport_types(int selection)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int ecx_get_params(OSSL_PARAM params[], int bits, int secbits,
|
||||
static int ecx_get_params(void *key, OSSL_PARAM params[], int bits, int secbits,
|
||||
int size)
|
||||
{
|
||||
ECX_KEY *ecx = key;
|
||||
OSSL_PARAM *p;
|
||||
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
|
||||
@@ -173,23 +173,38 @@ static int ecx_get_params(OSSL_PARAM params[], int bits, int secbits,
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
|
||||
&& !OSSL_PARAM_set_int(p, size))
|
||||
return 0;
|
||||
return 1;
|
||||
return key_to_params(ecx, NULL, params);
|
||||
}
|
||||
|
||||
static int x25519_get_params(void *key, OSSL_PARAM params[])
|
||||
{
|
||||
return ecx_get_params(params, X25519_BITS, X25519_SECURITY_BITS, X25519_KEYLEN);
|
||||
return ecx_get_params(key, params, X25519_BITS, X25519_SECURITY_BITS,
|
||||
X25519_KEYLEN);
|
||||
}
|
||||
|
||||
static int x448_get_params(void *key, OSSL_PARAM params[])
|
||||
{
|
||||
return ecx_get_params(params, X448_BITS, X448_SECURITY_BITS, X448_KEYLEN);
|
||||
return ecx_get_params(key, params, X448_BITS, X448_SECURITY_BITS,
|
||||
X448_KEYLEN);
|
||||
}
|
||||
|
||||
static int ed25519_get_params(void *key, OSSL_PARAM params[])
|
||||
{
|
||||
return ecx_get_params(key, params, ED25519_BITS, ED25519_SECURITY_BITS,
|
||||
ED25519_KEYLEN);
|
||||
}
|
||||
|
||||
static int ed448_get_params(void *key, OSSL_PARAM params[])
|
||||
{
|
||||
return ecx_get_params(key, params, ED448_BITS, ED448_SECURITY_BITS,
|
||||
ED448_KEYLEN);
|
||||
}
|
||||
|
||||
static const OSSL_PARAM ecx_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),
|
||||
ECX_KEY_TYPES(),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
|
||||
@@ -198,28 +213,21 @@ static const OSSL_PARAM *ecx_gettable_params(void)
|
||||
return ecx_params;
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH x25519_keymgmt_functions[] = {
|
||||
{ OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))x25519_new_key },
|
||||
{ OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ecx_key_free },
|
||||
{ OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))x25519_get_params },
|
||||
{ OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ecx_gettable_params },
|
||||
{ OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ecx_has },
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ecx_import },
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ecx_imexport_types },
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ecx_export },
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ecx_imexport_types },
|
||||
{ 0, NULL }
|
||||
};
|
||||
#define MAKE_KEYMGMT_FUNCTIONS(alg) \
|
||||
const OSSL_DISPATCH alg##_keymgmt_functions[] = { \
|
||||
{ OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))alg##_new_key }, \
|
||||
{ OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ecx_key_free }, \
|
||||
{ OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))alg##_get_params }, \
|
||||
{ OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ecx_gettable_params }, \
|
||||
{ OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ecx_has }, \
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ecx_import }, \
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ecx_imexport_types }, \
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ecx_export }, \
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ecx_imexport_types }, \
|
||||
{ 0, NULL } \
|
||||
};
|
||||
|
||||
const OSSL_DISPATCH x448_keymgmt_functions[] = {
|
||||
{ OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))x448_new_key },
|
||||
{ OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ecx_key_free },
|
||||
{ OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))x448_get_params },
|
||||
{ OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ecx_gettable_params },
|
||||
{ OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ecx_has },
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ecx_import },
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ecx_imexport_types },
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ecx_export },
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ecx_imexport_types },
|
||||
{ 0, NULL }
|
||||
};
|
||||
MAKE_KEYMGMT_FUNCTIONS(x25519)
|
||||
MAKE_KEYMGMT_FUNCTIONS(x448)
|
||||
MAKE_KEYMGMT_FUNCTIONS(ed25519)
|
||||
MAKE_KEYMGMT_FUNCTIONS(ed448)
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 2019-2020 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
|
||||
@@ -13,15 +13,18 @@
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/rsa.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/params.h>
|
||||
#include <openssl/types.h>
|
||||
#include "internal/param_build.h"
|
||||
#include "prov/implementations.h"
|
||||
#include "prov/providercommon.h"
|
||||
#include "prov/provider_ctx.h"
|
||||
#include "crypto/rsa.h"
|
||||
#include "internal/param_build_set.h"
|
||||
|
||||
static OSSL_OP_keymgmt_new_fn rsa_newdata;
|
||||
static OSSL_OP_keymgmt_gen_init_fn rsa_gen_init;
|
||||
static OSSL_OP_keymgmt_gen_set_params_fn rsa_gen_set_params;
|
||||
static OSSL_OP_keymgmt_gen_settable_params_fn rsa_gen_settable_params;
|
||||
static OSSL_OP_keymgmt_gen_fn rsa_gen;
|
||||
static OSSL_OP_keymgmt_gen_cleanup_fn rsa_gen_cleanup;
|
||||
static OSSL_OP_keymgmt_free_fn rsa_freedata;
|
||||
static OSSL_OP_keymgmt_get_params_fn rsa_get_params;
|
||||
static OSSL_OP_keymgmt_gettable_params_fn rsa_gettable_params;
|
||||
@@ -34,106 +37,13 @@ static OSSL_OP_keymgmt_export_fn rsa_export;
|
||||
static OSSL_OP_keymgmt_export_types_fn rsa_export_types;
|
||||
|
||||
#define RSA_DEFAULT_MD "SHA256"
|
||||
#define RSA_POSSIBLE_SELECTIONS \
|
||||
#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 collect_numbers(STACK_OF(BIGNUM) *numbers,
|
||||
const OSSL_PARAM params[], const char *key)
|
||||
{
|
||||
const OSSL_PARAM *p = NULL;
|
||||
|
||||
if (numbers == NULL)
|
||||
return 0;
|
||||
|
||||
for (p = params; (p = OSSL_PARAM_locate_const(p, key)) != NULL; p++) {
|
||||
BIGNUM *tmp = NULL;
|
||||
|
||||
if (!OSSL_PARAM_get_BN(p, &tmp))
|
||||
return 0;
|
||||
sk_BIGNUM_push(numbers, tmp);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int params_to_key(RSA *rsa, const OSSL_PARAM params[])
|
||||
{
|
||||
const OSSL_PARAM *param_n, *param_e, *param_d;
|
||||
BIGNUM *n = NULL, *e = NULL, *d = NULL;
|
||||
STACK_OF(BIGNUM) *factors = NULL, *exps = NULL, *coeffs = NULL;
|
||||
int is_private = 0;
|
||||
|
||||
if (rsa == NULL)
|
||||
return 0;
|
||||
|
||||
param_n = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N);
|
||||
param_e = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E);
|
||||
param_d = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D);
|
||||
|
||||
if ((param_n != NULL && !OSSL_PARAM_get_BN(param_n, &n))
|
||||
|| (param_e != NULL && !OSSL_PARAM_get_BN(param_e, &e))
|
||||
|| (param_d != NULL && !OSSL_PARAM_get_BN(param_d, &d)))
|
||||
goto err;
|
||||
|
||||
is_private = (d != NULL);
|
||||
|
||||
if (!RSA_set0_key(rsa, n, e, d))
|
||||
goto err;
|
||||
n = e = d = NULL;
|
||||
|
||||
if (is_private) {
|
||||
if (!collect_numbers(factors = sk_BIGNUM_new_null(), params,
|
||||
OSSL_PKEY_PARAM_RSA_FACTOR)
|
||||
|| !collect_numbers(exps = sk_BIGNUM_new_null(), params,
|
||||
OSSL_PKEY_PARAM_RSA_EXPONENT)
|
||||
|| !collect_numbers(coeffs = sk_BIGNUM_new_null(), params,
|
||||
OSSL_PKEY_PARAM_RSA_COEFFICIENT))
|
||||
goto err;
|
||||
|
||||
/* It's ok if this private key just has n, e and d */
|
||||
if (sk_BIGNUM_num(factors) != 0
|
||||
&& !rsa_set0_all_params(rsa, factors, exps, coeffs))
|
||||
goto err;
|
||||
}
|
||||
|
||||
sk_BIGNUM_free(factors);
|
||||
sk_BIGNUM_free(exps);
|
||||
sk_BIGNUM_free(coeffs);
|
||||
return 1;
|
||||
|
||||
err:
|
||||
BN_free(n);
|
||||
BN_free(e);
|
||||
BN_free(d);
|
||||
sk_BIGNUM_pop_free(factors, BN_free);
|
||||
sk_BIGNUM_pop_free(exps, BN_free);
|
||||
sk_BIGNUM_pop_free(coeffs, BN_free);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int export_numbers(OSSL_PARAM_BLD *tmpl, const char *key,
|
||||
STACK_OF(BIGNUM_const) *numbers)
|
||||
{
|
||||
int i, nnum;
|
||||
|
||||
if (numbers == NULL)
|
||||
return 0;
|
||||
|
||||
nnum = sk_BIGNUM_const_num(numbers);
|
||||
|
||||
for (i = 0; i < nnum; i++) {
|
||||
if (!ossl_param_bld_push_BN(tmpl, key,
|
||||
sk_BIGNUM_const_value(numbers, i)))
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int key_to_params(RSA *rsa, OSSL_PARAM_BLD *tmpl)
|
||||
static int key_to_params(RSA *rsa, OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
|
||||
{
|
||||
int ret = 0;
|
||||
const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
|
||||
@@ -147,21 +57,16 @@ static int key_to_params(RSA *rsa, OSSL_PARAM_BLD *tmpl)
|
||||
RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
|
||||
rsa_get0_all_params(rsa, factors, exps, coeffs);
|
||||
|
||||
if (rsa_n != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_N, rsa_n))
|
||||
if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_N, rsa_n)
|
||||
|| !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_E, rsa_e)
|
||||
|| !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_D, rsa_d)
|
||||
|| !ossl_param_build_set_multi_key_bn(bld, params, rsa_mp_factor_names,
|
||||
factors)
|
||||
|| !ossl_param_build_set_multi_key_bn(bld, params, rsa_mp_exp_names,
|
||||
exps)
|
||||
|| !ossl_param_build_set_multi_key_bn(bld, params, rsa_mp_coeff_names,
|
||||
coeffs))
|
||||
goto err;
|
||||
if (rsa_e != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_E, rsa_e))
|
||||
goto err;
|
||||
if (rsa_d != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_D, rsa_d))
|
||||
goto err;
|
||||
|
||||
if (!export_numbers(tmpl, OSSL_PKEY_PARAM_RSA_FACTOR, factors)
|
||||
|| !export_numbers(tmpl, OSSL_PKEY_PARAM_RSA_EXPONENT, exps)
|
||||
|| !export_numbers(tmpl, OSSL_PKEY_PARAM_RSA_COEFFICIENT, coeffs))
|
||||
goto err;
|
||||
|
||||
ret = 1;
|
||||
err:
|
||||
sk_BIGNUM_const_free(factors);
|
||||
@@ -187,14 +92,19 @@ static int rsa_has(void *keydata, int selection)
|
||||
RSA *rsa = keydata;
|
||||
int ok = 0;
|
||||
|
||||
if ((selection & RSA_POSSIBLE_SELECTIONS) != 0)
|
||||
ok = 1;
|
||||
if (rsa != NULL) {
|
||||
if ((selection & RSA_POSSIBLE_SELECTIONS) != 0)
|
||||
ok = 1;
|
||||
|
||||
ok = ok && (RSA_get0_e(rsa) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
|
||||
ok = ok && (RSA_get0_n(rsa) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
||||
ok = ok && (RSA_get0_d(rsa) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
|
||||
ok = ok && 0; /* This will change with PSS and OAEP */
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ok = ok && (RSA_get0_e(rsa) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
|
||||
ok = ok && (RSA_get0_n(rsa) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
||||
ok = ok && (RSA_get0_d(rsa) != NULL);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
@@ -224,7 +134,7 @@ static int rsa_import(void *keydata, int selection, const OSSL_PARAM params[])
|
||||
/* TODO(3.0) PSS and OAEP should bring on parameters */
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ok = ok && params_to_key(rsa, params);
|
||||
ok = ok && rsa_fromdata(rsa, params);
|
||||
|
||||
return ok;
|
||||
}
|
||||
@@ -233,7 +143,7 @@ static int rsa_export(void *keydata, int selection,
|
||||
OSSL_CALLBACK *param_callback, void *cbarg)
|
||||
{
|
||||
RSA *rsa = keydata;
|
||||
OSSL_PARAM_BLD tmpl;
|
||||
OSSL_PARAM_BLD *tmpl;
|
||||
OSSL_PARAM *params = NULL;
|
||||
int ok = 1;
|
||||
|
||||
@@ -242,20 +152,75 @@ static int rsa_export(void *keydata, int selection,
|
||||
|
||||
/* TODO(3.0) PSS and OAEP should bring on parameters */
|
||||
|
||||
ossl_param_bld_init(&tmpl);
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ok = ok && key_to_params(rsa, &tmpl);
|
||||
|
||||
if (!ok
|
||||
|| (params = ossl_param_bld_to_param(&tmpl)) == NULL)
|
||||
tmpl = OSSL_PARAM_BLD_new();
|
||||
if (tmpl == NULL)
|
||||
return 0;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ok = ok && key_to_params(rsa, tmpl, NULL);
|
||||
|
||||
if (!ok
|
||||
|| (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
|
||||
goto err;
|
||||
|
||||
ok = param_callback(params, cbarg);
|
||||
ossl_param_bld_free(params);
|
||||
OSSL_PARAM_BLD_free_params(params);
|
||||
err:
|
||||
OSSL_PARAM_BLD_free(tmpl);
|
||||
return ok;
|
||||
}
|
||||
|
||||
#ifdef FIPS_MODE
|
||||
/* 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
|
||||
@@ -264,41 +229,8 @@ static int rsa_export(void *keydata, int selection,
|
||||
* different arrays.
|
||||
*/
|
||||
static const OSSL_PARAM 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),
|
||||
/* We tolerate up to 10 factors... */
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
|
||||
/* ..., up to 10 CRT exponents... */
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
|
||||
/* ..., and up to 9 CRT coefficients */
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
|
||||
RSA_KEY_TYPES()
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
/*
|
||||
* We lied about the amount of factors, exponents and coefficients, the
|
||||
@@ -319,7 +251,6 @@ 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);
|
||||
@@ -365,8 +296,7 @@ static int rsa_get_params(void *key, OSSL_PARAM params[])
|
||||
if (!OSSL_PARAM_set_utf8_string(p, RSA_DEFAULT_MD))
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
return key_to_params(rsa, NULL, params);
|
||||
}
|
||||
|
||||
static const OSSL_PARAM rsa_params[] = {
|
||||
@@ -374,6 +304,7 @@ static const OSSL_PARAM rsa_params[] = {
|
||||
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
|
||||
};
|
||||
|
||||
@@ -403,8 +334,127 @@ static int rsa_validate(void *keydata, int selection)
|
||||
return ok;
|
||||
}
|
||||
|
||||
struct rsa_gen_ctx {
|
||||
OPENSSL_CTX *libctx;
|
||||
|
||||
size_t nbits;
|
||||
BIGNUM *pub_exp;
|
||||
size_t primes;
|
||||
|
||||
/* For generation callback */
|
||||
OSSL_CALLBACK *cb;
|
||||
void *cbarg;
|
||||
};
|
||||
|
||||
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 *rsa_gen_init(void *provctx, int selection)
|
||||
{
|
||||
OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
|
||||
struct rsa_gen_ctx *gctx = 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)) {
|
||||
BN_free(gctx->pub_exp);
|
||||
gctx = NULL;
|
||||
} else {
|
||||
gctx->nbits = 2048;
|
||||
gctx->primes = RSA_DEFAULT_PRIME_NUM;
|
||||
}
|
||||
}
|
||||
return gctx;
|
||||
}
|
||||
|
||||
static int rsa_gen_set_params(void *genctx, const OSSL_PARAM params[])
|
||||
{
|
||||
struct rsa_gen_ctx *gctx = genctx;
|
||||
const OSSL_PARAM *p;
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_BITS)) != NULL
|
||||
&& !OSSL_PARAM_get_size_t(p, &gctx->nbits))
|
||||
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;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM *rsa_gen_settable_params(void *provctx)
|
||||
{
|
||||
static OSSL_PARAM settable[] = {
|
||||
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),
|
||||
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;
|
||||
BN_GENCB *gencb = NULL;
|
||||
|
||||
if (gctx == NULL
|
||||
|| (rsa = 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 (!RSA_generate_multi_prime_key(rsa, (int)gctx->nbits, (int)gctx->primes,
|
||||
gctx->pub_exp, gencb)) {
|
||||
RSA_free(rsa);
|
||||
rsa = NULL;
|
||||
}
|
||||
|
||||
BN_GENCB_free(gencb);
|
||||
|
||||
return rsa;
|
||||
}
|
||||
|
||||
static void rsa_gen_cleanup(void *genctx)
|
||||
{
|
||||
struct rsa_gen_ctx *gctx = genctx;
|
||||
|
||||
if (gctx == NULL)
|
||||
return;
|
||||
|
||||
BN_clear_free(gctx->pub_exp);
|
||||
OPENSSL_free(gctx);
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH 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_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 },
|
||||
|
||||
Reference in New Issue
Block a user