Skip to content

Commit b5d7a77

Browse files
committed
feat: route configurable devId through key init calls
Adds int devId to WOLFPROV_CTX (initialized to INVALID_DEVID) and exposes it as a settable OSSL_PARAM ("wolfprovider_devid") so callers can route provider operations through a wolfHSM device callback. Routes devId through: - RSA: wc_InitRsaKey -> wc_InitRsaKey_ex - ECC: wc_ecc_init_ex (was hardcoding INVALID_DEVID) - DH: wc_InitDhKey_ex (was hardcoding INVALID_DEVID) - ECX gen-context RNG: wc_InitRng -> wc_InitRng_ex Known gap: ECX key init functions (wc_curve25519_init, wc_ed25519_init, wc_ed448_init) use WP_ECX_INIT function pointers with no devId parameter; fixing them requires a table-shape change tracked separately.
1 parent 53b3f85 commit b5d7a77

6 files changed

Lines changed: 59 additions & 8 deletions

File tree

include/wolfprovider/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ typedef struct WOLFPROV_CTX {
146146
wolfSSL_Mutex rng_mutex;
147147
#endif
148148
BIO_METHOD *coreBioMethod;
149+
int devId;
149150
} WOLFPROV_CTX;
150151

151152
#if defined(WP_HAVE_SEED_SRC) && defined(WP_HAVE_RANDOM)

src/wp_dh_kmgmt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ static wp_Dh* wp_dh_new(WOLFPROV_CTX *provCtx)
396396
int ok = 1;
397397
int rc;
398398

399-
rc = wc_InitDhKey_ex(&dh->key, NULL, INVALID_DEVID);
399+
rc = wc_InitDhKey_ex(&dh->key, NULL, provCtx->devId);
400400
if (rc != 0) {
401401
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_InitDhKey_ex", rc);
402402
ok = 0;

src/wp_ecc_kmgmt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ static wp_Ecc* wp_ecc_new(WOLFPROV_CTX *provCtx)
334334
int ok = 1;
335335
int rc;
336336

337-
rc = wc_ecc_init_ex(&ecc->key, NULL, INVALID_DEVID);
337+
rc = wc_ecc_init_ex(&ecc->key, NULL, provCtx->devId);
338338
if (rc != 0) {
339339
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_ecc_init_ex", rc);
340340
ok = 0;

src/wp_ecx_kmgmt.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,13 +1203,14 @@ static wp_EcxGenCtx* wp_ecx_gen_init(WOLFPROV_CTX* provCtx,
12031203
int rc;
12041204
int ok = 1;
12051205

1206-
rc = wc_InitRng(&ctx->rng);
1206+
/* provCtx assigned before RNG init: ctx->provCtx->devId must be valid */
1207+
ctx->provCtx = provCtx;
1208+
rc = wc_InitRng_ex(&ctx->rng, NULL, ctx->provCtx->devId);
12071209
if (rc != 0) {
1208-
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_InitRng", rc);
1210+
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_InitRng_ex", rc);
12091211
ok = 0;
12101212
}
12111213
if (ok) {
1212-
ctx->provCtx = provCtx;
12131214
ctx->name = name;
12141215
if (!wp_ecx_gen_set_params(ctx, params)) {
12151216
wc_FreeRng(&ctx->rng);

src/wp_rsa_kmgmt.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,9 @@ static wp_Rsa* wp_rsa_base_new(WOLFPROV_CTX* provCtx, int type)
464464
int ok = 1;
465465
int rc;
466466

467-
rc = wc_InitRsaKey(&rsa->key, NULL);
467+
rc = wc_InitRsaKey_ex(&rsa->key, NULL, provCtx->devId);
468468
if (rc != 0) {
469-
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_InitRsaKey", rc);
469+
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_InitRsaKey_ex", rc);
470470
ok = 0;
471471
}
472472

@@ -1533,7 +1533,7 @@ static wp_RsaGenCtx* wp_rsa_base_gen_init(WOLFPROV_CTX* provCtx,
15331533
int ok = 1;
15341534
int rc;
15351535

1536-
rc = wc_InitRng_ex(&ctx->rng, NULL, INVALID_DEVID);
1536+
rc = wc_InitRng_ex(&ctx->rng, NULL, provCtx->devId);
15371537
if (rc != 0) {
15381538
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_InitRng_ex", rc);
15391539
ok = 0;

src/wp_wolfprov.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ static const OSSL_PARAM wolfssl_param_types[] = {
4949
OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
5050
OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
5151
OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),
52+
OSSL_PARAM_int("wolfprovider_devid", NULL),
5253
OSSL_PARAM_END
5354
};
5455

@@ -218,6 +219,9 @@ static WOLFPROV_CTX* wolfssl_prov_ctx_new(void)
218219
WP_CHECK_FIPS_ALGO_PTR(WP_CAST_ALGO_DRBG);
219220

220221
ctx = (WOLFPROV_CTX*)OPENSSL_zalloc(sizeof(*ctx));
222+
if (ctx != NULL) {
223+
ctx->devId = INVALID_DEVID;
224+
}
221225
if ((ctx != NULL) && (wc_InitRng(&ctx->rng) != 0)) {
222226
OPENSSL_free(ctx);
223227
ctx = NULL;
@@ -372,6 +376,49 @@ static int wolfprov_get_params(void* provCtx, OSSL_PARAM params[])
372376
return ok;
373377
}
374378

379+
/*
380+
* Get the table of parameters that can be set on wolfProv.
381+
*
382+
* @param [in] provCtx Unused.
383+
* @return Table of settable parameters.
384+
*/
385+
static const OSSL_PARAM* wolfprov_settable_params(void* provCtx)
386+
{
387+
static const OSSL_PARAM settable[] = {
388+
OSSL_PARAM_int("wolfprovider_devid", NULL),
389+
OSSL_PARAM_END
390+
};
391+
(void)provCtx;
392+
return settable;
393+
}
394+
395+
/*
396+
* Set parameters on the provider context.
397+
*
398+
* @param [in] provCtx Provider context.
399+
* @param [in] params Parameters to set.
400+
* @return 1 on success.
401+
* @return 0 on failure.
402+
*/
403+
static int wolfprov_set_params(void* provCtx, const OSSL_PARAM params[])
404+
{
405+
int ok = 1;
406+
const OSSL_PARAM* p;
407+
WOLFPROV_CTX* ctx = (WOLFPROV_CTX*)provCtx;
408+
409+
WOLFPROV_ENTER(WP_LOG_COMP_PROVIDER, "wolfprov_set_params");
410+
411+
p = OSSL_PARAM_locate_const(params, "wolfprovider_devid");
412+
if (p != NULL) {
413+
if (!OSSL_PARAM_get_int(p, &ctx->devId)) {
414+
ok = 0;
415+
}
416+
}
417+
418+
WOLFPROV_LEAVE(WP_LOG_COMP_PROVIDER, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
419+
return ok;
420+
}
421+
375422
#ifdef HAVE_FIPS
376423
/* Properties of wolfSSL provider: name and FIPS wolfSSL. */
377424
#define WOLFPROV_PROPERTIES "provider=wolfprov,fips=yes"
@@ -1214,6 +1261,8 @@ static const OSSL_DISPATCH wolfprov_dispatch_table[] = {
12141261
{ OSSL_FUNC_PROVIDER_TEARDOWN, (DFUNC)wolfprov_teardown },
12151262
{ OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (DFUNC)wolfprov_gettable_params },
12161263
{ OSSL_FUNC_PROVIDER_GET_PARAMS, (DFUNC)wolfprov_get_params },
1264+
{ OSSL_FUNC_PROVIDER_SETTABLE_PARAMS, (DFUNC)wolfprov_settable_params },
1265+
{ OSSL_FUNC_PROVIDER_SET_PARAMS, (DFUNC)wolfprov_set_params },
12171266
{ OSSL_FUNC_PROVIDER_QUERY_OPERATION, (DFUNC)wolfprov_query },
12181267
{ OSSL_FUNC_PROVIDER_GET_CAPABILITIES,
12191268
(DFUNC)wolfssl_prov_get_capabilities },

0 commit comments

Comments
 (0)