Latest update.
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
|
||||
$DH_GOAL=../../libimplementations.a
|
||||
$DSA_GOAL=../../libimplementations.a
|
||||
$RSA_GOAL=../../libimplementations.a
|
||||
|
||||
IF[{- !$disabled{dh} -}]
|
||||
SOURCE[$DH_GOAL]=dh_kmgmt.c
|
||||
@@ -10,3 +11,4 @@ ENDIF
|
||||
IF[{- !$disabled{dsa} -}]
|
||||
SOURCE[$DSA_GOAL]=dsa_kmgmt.c
|
||||
ENDIF
|
||||
SOURCE[$RSA_GOAL]=rsa_kmgmt.c
|
||||
@@ -12,12 +12,16 @@
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/dh.h>
|
||||
#include <openssl/params.h>
|
||||
#include "internal/param_build.h"
|
||||
#include "prov/implementations.h"
|
||||
#include "prov/providercommon.h"
|
||||
|
||||
static OSSL_OP_keymgmt_importdomparams_fn dh_importdomparams;
|
||||
static OSSL_OP_keymgmt_exportdomparams_fn dh_exportdomparams;
|
||||
static OSSL_OP_keymgmt_get_key_params_fn dh_get_domparam_params;
|
||||
static OSSL_OP_keymgmt_importkey_fn dh_importkey;
|
||||
static OSSL_OP_keymgmt_exportkey_fn dh_exportkey;
|
||||
static OSSL_OP_keymgmt_get_key_params_fn dh_get_key_params;
|
||||
|
||||
static int params_to_domparams(DH *dh, const OSSL_PARAM params[])
|
||||
{
|
||||
@@ -45,20 +49,19 @@ static int params_to_domparams(DH *dh, const OSSL_PARAM params[])
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int domparams_to_params(DH *dh, OSSL_PARAM params[])
|
||||
static int domparams_to_params(DH *dh, OSSL_PARAM_BLD *tmpl)
|
||||
{
|
||||
OSSL_PARAM *p;
|
||||
const BIGNUM *dh_p = NULL, *dh_g = NULL;
|
||||
|
||||
if (dh == NULL)
|
||||
return 0;
|
||||
|
||||
DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_FFC_P)) != NULL
|
||||
&& !OSSL_PARAM_set_BN(p, dh_p))
|
||||
if (dh_p != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dh_p))
|
||||
return 0;
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_FFC_G)) != NULL
|
||||
&& !OSSL_PARAM_set_BN(p, dh_g))
|
||||
if (dh_g != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dh_g))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
@@ -105,24 +108,21 @@ static int params_to_key(DH *dh, const OSSL_PARAM params[])
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int key_to_params(DH *dh, OSSL_PARAM params[])
|
||||
static int key_to_params(DH *dh, OSSL_PARAM_BLD *tmpl)
|
||||
{
|
||||
OSSL_PARAM *p;
|
||||
const BIGNUM *priv_key = NULL, *pub_key = NULL;
|
||||
|
||||
if (dh == NULL)
|
||||
return 0;
|
||||
if (!domparams_to_params(dh, params))
|
||||
if (!domparams_to_params(dh, tmpl))
|
||||
return 0;
|
||||
|
||||
DH_get0_key(dh, &pub_key, &priv_key);
|
||||
if ((p = OSSL_PARAM_locate(params,
|
||||
OSSL_PKEY_PARAM_DH_PRIV_KEY)) != NULL
|
||||
&& !OSSL_PARAM_set_BN(p, priv_key))
|
||||
if (priv_key != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_DH_PRIV_KEY, priv_key))
|
||||
return 0;
|
||||
if ((p = OSSL_PARAM_locate(params,
|
||||
OSSL_PKEY_PARAM_DH_PUB_KEY)) != NULL
|
||||
&& !OSSL_PARAM_set_BN(p, pub_key))
|
||||
if (pub_key != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_DH_PUB_KEY, pub_key))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
@@ -140,11 +140,22 @@ static void *dh_importdomparams(void *provctx, const OSSL_PARAM params[])
|
||||
return dh;
|
||||
}
|
||||
|
||||
static int dh_exportdomparams(void *domparams, OSSL_PARAM params[])
|
||||
static int dh_exportdomparams(void *domparams, OSSL_CALLBACK *param_cb,
|
||||
void *cbarg)
|
||||
{
|
||||
DH *dh = domparams;
|
||||
OSSL_PARAM_BLD tmpl;
|
||||
OSSL_PARAM *params = NULL;
|
||||
int ret;
|
||||
|
||||
return dh != NULL && !domparams_to_params(dh, params);
|
||||
ossl_param_bld_init(&tmpl);
|
||||
if (dh == NULL
|
||||
|| !domparams_to_params(dh, &tmpl)
|
||||
|| (params = ossl_param_bld_to_param(&tmpl)) == NULL)
|
||||
return 0;
|
||||
ret = param_cb(params, cbarg);
|
||||
ossl_param_bld_free(params);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void *dh_importkey(void *provctx, const OSSL_PARAM params[])
|
||||
@@ -159,11 +170,56 @@ static void *dh_importkey(void *provctx, const OSSL_PARAM params[])
|
||||
return dh;
|
||||
}
|
||||
|
||||
static int dh_exportkey(void *key, OSSL_PARAM params[])
|
||||
static int dh_exportkey(void *key, OSSL_CALLBACK *param_cb, void *cbarg)
|
||||
{
|
||||
DH *dh = key;
|
||||
OSSL_PARAM_BLD tmpl;
|
||||
OSSL_PARAM *params = NULL;
|
||||
int ret;
|
||||
|
||||
return dh != NULL && !key_to_params(dh, params);
|
||||
ossl_param_bld_init(&tmpl);
|
||||
if (dh == NULL
|
||||
|| !key_to_params(dh, &tmpl)
|
||||
|| (params = ossl_param_bld_to_param(&tmpl)) == NULL)
|
||||
return 0;
|
||||
ret = param_cb(params, cbarg);
|
||||
ossl_param_bld_free(params);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Same function for domain parameters and for keys.
|
||||
* "dpk" = "domain parameters & keys"
|
||||
*/
|
||||
static ossl_inline int dh_get_dpk_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;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* We have wrapper functions to make sure we get signatures right, see
|
||||
* the forward declarations at the beginning of this file.
|
||||
*/
|
||||
static int dh_get_domparam_params(void *domparams, OSSL_PARAM params[])
|
||||
{
|
||||
return dh_get_dpk_params(domparams, params);
|
||||
}
|
||||
|
||||
static int dh_get_key_params(void *key, OSSL_PARAM params[])
|
||||
{
|
||||
return dh_get_dpk_params(key, params);
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH dh_keymgmt_functions[] = {
|
||||
@@ -173,9 +229,12 @@ const OSSL_DISPATCH dh_keymgmt_functions[] = {
|
||||
*/
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORTDOMPARAMS, (void (*)(void))dh_importdomparams },
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORTDOMPARAMS, (void (*)(void))dh_exportdomparams },
|
||||
{ OSSL_FUNC_KEYMGMT_GET_DOMPARAM_PARAMS,
|
||||
(void (*) (void))dh_get_domparam_params },
|
||||
{ OSSL_FUNC_KEYMGMT_FREEDOMPARAMS, (void (*)(void))DH_free },
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORTKEY, (void (*)(void))dh_importkey },
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORTKEY, (void (*)(void))dh_exportkey },
|
||||
{ OSSL_FUNC_KEYMGMT_FREEKEY, (void (*)(void))DH_free },
|
||||
{ OSSL_FUNC_KEYMGMT_GET_KEY_PARAMS, (void (*) (void))dh_get_key_params },
|
||||
{ 0, NULL }
|
||||
};
|
||||
@@ -10,14 +10,19 @@
|
||||
#include <openssl/core_numbers.h>
|
||||
#include <openssl/core_names.h>
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/dsa.h>
|
||||
#include <openssl/params.h>
|
||||
#include "internal/param_build.h"
|
||||
#include "prov/implementations.h"
|
||||
#include "prov/providercommon.h"
|
||||
#include "prov/provider_ctx.h"
|
||||
#include "crypto/dsa.h"
|
||||
|
||||
static OSSL_OP_keymgmt_importdomparams_fn dsa_importdomparams;
|
||||
static OSSL_OP_keymgmt_exportdomparams_fn dsa_exportdomparams;
|
||||
static OSSL_OP_keymgmt_get_domparam_params_fn dsa_get_domparam_params;
|
||||
static OSSL_OP_keymgmt_importkey_fn dsa_importkey;
|
||||
static OSSL_OP_keymgmt_exportkey_fn dsa_exportkey;
|
||||
static OSSL_OP_keymgmt_get_key_params_fn dsa_get_key_params;
|
||||
|
||||
static int params_to_domparams(DSA *dsa, const OSSL_PARAM params[])
|
||||
{
|
||||
@@ -48,23 +53,22 @@ static int params_to_domparams(DSA *dsa, const OSSL_PARAM params[])
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int domparams_to_params(DSA *dsa, OSSL_PARAM params[])
|
||||
static int domparams_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
|
||||
{
|
||||
OSSL_PARAM *p;
|
||||
const BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL;
|
||||
|
||||
if (dsa == NULL)
|
||||
return 0;
|
||||
|
||||
DSA_get0_pqg(dsa, &dsa_p, &dsa_q, &dsa_g);
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_FFC_P)) != NULL
|
||||
&& !OSSL_PARAM_set_BN(p, dsa_p))
|
||||
if (dsa_p != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dsa_p))
|
||||
return 0;
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_FFC_Q)) != NULL
|
||||
&& !OSSL_PARAM_set_BN(p, dsa_q))
|
||||
if (dsa_q != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, dsa_q))
|
||||
return 0;
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_FFC_G)) != NULL
|
||||
&& !OSSL_PARAM_set_BN(p, dsa_g))
|
||||
if (dsa_g != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dsa_g))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
@@ -110,22 +114,21 @@ static int params_to_key(DSA *dsa, const OSSL_PARAM params[])
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int key_to_params(DSA *dsa, OSSL_PARAM params[])
|
||||
static int key_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
|
||||
{
|
||||
OSSL_PARAM *p;
|
||||
const BIGNUM *priv_key = NULL, *pub_key = NULL;
|
||||
|
||||
if (dsa == NULL)
|
||||
return 0;
|
||||
if (!domparams_to_params(dsa, params))
|
||||
if (!domparams_to_params(dsa, tmpl))
|
||||
return 0;
|
||||
|
||||
DSA_get0_key(dsa, &pub_key, &priv_key);
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DSA_PRIV_KEY)) != NULL
|
||||
&& !OSSL_PARAM_set_BN(p, priv_key))
|
||||
if (priv_key != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_DSA_PRIV_KEY, priv_key))
|
||||
return 0;
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DSA_PUB_KEY)) != NULL
|
||||
&& !OSSL_PARAM_set_BN(p, pub_key))
|
||||
if (pub_key != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_DSA_PUB_KEY, pub_key))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
@@ -143,11 +146,22 @@ static void *dsa_importdomparams(void *provctx, const OSSL_PARAM params[])
|
||||
return dsa;
|
||||
}
|
||||
|
||||
static int dsa_exportdomparams(void *domparams, OSSL_PARAM params[])
|
||||
static int dsa_exportdomparams(void *domparams,
|
||||
OSSL_CALLBACK *param_cb, void *cbarg)
|
||||
{
|
||||
DSA *dsa = domparams;
|
||||
OSSL_PARAM_BLD tmpl;
|
||||
OSSL_PARAM *params = NULL;
|
||||
int ret;
|
||||
|
||||
return dsa != NULL && !domparams_to_params(dsa, params);
|
||||
ossl_param_bld_init(&tmpl);
|
||||
if (dsa == NULL
|
||||
|| !domparams_to_params(dsa, &tmpl)
|
||||
|| (params = ossl_param_bld_to_param(&tmpl)) == NULL)
|
||||
return 0;
|
||||
ret = param_cb(params, cbarg);
|
||||
ossl_param_bld_free(params);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void *dsa_importkey(void *provctx, const OSSL_PARAM params[])
|
||||
@@ -162,23 +176,67 @@ static void *dsa_importkey(void *provctx, const OSSL_PARAM params[])
|
||||
return dsa;
|
||||
}
|
||||
|
||||
static int dsa_exportkey(void *key, OSSL_PARAM params[])
|
||||
static int dsa_exportkey(void *key, OSSL_CALLBACK *param_cb, void *cbarg)
|
||||
{
|
||||
DSA *dsa = key;
|
||||
OSSL_PARAM_BLD tmpl;
|
||||
OSSL_PARAM *params = NULL;
|
||||
int ret;
|
||||
|
||||
return dsa != NULL && !key_to_params(dsa, params);
|
||||
ossl_param_bld_init(&tmpl);
|
||||
if (dsa == NULL
|
||||
|| !key_to_params(dsa, &tmpl)
|
||||
|| (params = ossl_param_bld_to_param(&tmpl)) == NULL)
|
||||
return 0;
|
||||
ret = param_cb(params, cbarg);
|
||||
ossl_param_bld_free(params);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Same function for domain parameters and for keys.
|
||||
* "dpk" = "domain parameters & keys"
|
||||
*/
|
||||
static ossl_inline int dsa_get_dpk_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;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* We have wrapper functions to make sure we get signatures right, see
|
||||
* the forward declarations at the beginning of this file.
|
||||
*/
|
||||
static int dsa_get_domparam_params(void *domparams, OSSL_PARAM params[])
|
||||
{
|
||||
return dsa_get_dpk_params(domparams, params);
|
||||
}
|
||||
|
||||
static int dsa_get_key_params(void *key, OSSL_PARAM params[])
|
||||
{
|
||||
return dsa_get_dpk_params(key, params);
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH dsa_keymgmt_functions[] = {
|
||||
/*
|
||||
* TODO(3.0) When implementing OSSL_FUNC_KEYMGMT_GENKEY, remember to also
|
||||
* implement OSSL_FUNC_KEYMGMT_EXPORTKEY.
|
||||
*/
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORTDOMPARAMS, (void (*)(void))dsa_importdomparams },
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORTDOMPARAMS, (void (*)(void))dsa_exportdomparams },
|
||||
{ OSSL_FUNC_KEYMGMT_FREEDOMPARAMS, (void (*)(void))DSA_free },
|
||||
{ OSSL_FUNC_KEYMGMT_GET_DOMPARAM_PARAMS,
|
||||
(void (*) (void))dsa_get_domparam_params },
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORTKEY, (void (*)(void))dsa_importkey },
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORTKEY, (void (*)(void))dsa_exportkey },
|
||||
{ OSSL_FUNC_KEYMGMT_FREEKEY, (void (*)(void))DSA_free },
|
||||
{ OSSL_FUNC_KEYMGMT_GET_KEY_PARAMS, (void (*) (void))dsa_get_key_params },
|
||||
{ 0, NULL }
|
||||
};
|
||||
@@ -0,0 +1,273 @@
|
||||
/*
|
||||
* Copyright 2019 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
|
||||
*/
|
||||
|
||||
#include <openssl/core_numbers.h>
|
||||
#include <openssl/core_names.h>
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/rsa.h>
|
||||
#include <openssl/params.h>
|
||||
#include <openssl/types.h>
|
||||
#include "internal/param_build.h"
|
||||
#include "prov/implementations.h"
|
||||
#include "prov/providercommon.h"
|
||||
#include "crypto/rsa.h"
|
||||
|
||||
static OSSL_OP_keymgmt_importkey_fn rsa_importkey;
|
||||
static OSSL_OP_keymgmt_exportkey_fn rsa_exportkey;
|
||||
static OSSL_OP_keymgmt_get_key_params_fn rsa_get_key_params;
|
||||
|
||||
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)
|
||||
{
|
||||
int ret = 0;
|
||||
const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
|
||||
STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null();
|
||||
STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null();
|
||||
STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null();
|
||||
|
||||
if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)
|
||||
goto err;
|
||||
|
||||
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))
|
||||
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);
|
||||
sk_BIGNUM_const_free(exps);
|
||||
sk_BIGNUM_const_free(coeffs);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void *rsa_importkey(void *provctx, const OSSL_PARAM params[])
|
||||
{
|
||||
RSA *rsa;
|
||||
|
||||
if ((rsa = RSA_new()) == NULL
|
||||
|| !params_to_key(rsa, params)) {
|
||||
RSA_free(rsa);
|
||||
rsa = NULL;
|
||||
}
|
||||
return rsa;
|
||||
}
|
||||
|
||||
static int rsa_exportkey(void *key, OSSL_CALLBACK *param_callback, void *cbarg)
|
||||
{
|
||||
RSA *rsa = key;
|
||||
OSSL_PARAM_BLD tmpl;
|
||||
OSSL_PARAM *params = NULL;
|
||||
int ret;
|
||||
|
||||
ossl_param_bld_init(&tmpl);
|
||||
if (rsa == NULL
|
||||
|| !key_to_params(rsa, &tmpl)
|
||||
|| (params = ossl_param_bld_to_param(&tmpl)) == NULL)
|
||||
return 0;
|
||||
ret = param_callback(params, cbarg);
|
||||
ossl_param_bld_free(params);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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[] = {
|
||||
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),
|
||||
};
|
||||
/*
|
||||
* 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_exportkey_types(void)
|
||||
{
|
||||
return rsa_key_types;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM *rsa_importkey_types(void)
|
||||
{
|
||||
return rsa_key_types;
|
||||
}
|
||||
|
||||
static int rsa_get_key_params(void *key, OSSL_PARAM params[])
|
||||
{
|
||||
RSA *rsa = key;
|
||||
OSSL_PARAM *p;
|
||||
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
|
||||
&& !OSSL_PARAM_set_int(p, RSA_bits(rsa)))
|
||||
return 0;
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
|
||||
&& !OSSL_PARAM_set_int(p, RSA_security_bits(rsa)))
|
||||
return 0;
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
|
||||
&& !OSSL_PARAM_set_int(p, RSA_size(rsa)))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH rsa_keymgmt_functions[] = {
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORTKEY, (void (*)(void))rsa_importkey },
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORTKEY_TYPES, (void (*)(void))rsa_importkey_types },
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORTKEY, (void (*)(void))rsa_exportkey },
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORTKEY_TYPES, (void (*)(void))rsa_exportkey_types },
|
||||
{ OSSL_FUNC_KEYMGMT_FREEKEY, (void (*)(void))RSA_free },
|
||||
{ OSSL_FUNC_KEYMGMT_GET_KEY_PARAMS, (void (*) (void))rsa_get_key_params },
|
||||
{ 0, NULL }
|
||||
};
|
||||
Reference in New Issue
Block a user