Compare commits

...

3 Commits

Author SHA1 Message Date
Simo Sorce 6c319f2c38 Clear keymgmt params in pedantic mode
Signed-off-by: Simo Sorce <simo@redhat.com>
2025-07-28 15:38:34 -04:00
Simo Sorce 7b6d6a3fa2 Test OSSL_PARAM_clear_free()
Signed-off-by: Simo Sorce <simo@redhat.com>
2025-07-28 15:38:34 -04:00
Simo Sorce db1db44889 Add a way to cleanse params arrays
This uses the return_size field of the last terminating parameter
similaraly to how secure memory uses the data and data_size fields,
to hold the total size of memory allocated for params.
This is then used to be able to call OPENSSL_cleanse on the params
fields via the new OSSL_PARAM_clear_free() call.

Signed-off-by: Simo Sorce <simo@redhat.com>
2025-07-28 15:33:50 -04:00
14 changed files with 92 additions and 2 deletions

View File

@ -382,6 +382,7 @@ OSSL_PARAM *OSSL_PARAM_BLD_to_param(OSSL_PARAM_BLD *bld)
blk = p_blks + (OSSL_PARAM_ALIGNED_BLOCK *)(params);
last = param_bld_convert(bld, params, blk, s);
ossl_param_set_secure_block(last, s, ss);
last->return_size = total;
/* Reset builder for reuse */
bld->total_blocks = 0;

View File

@ -132,6 +132,7 @@ OSSL_PARAM *OSSL_PARAM_dup(const OSSL_PARAM *src)
/* Store the allocated secure memory buffer in the last param block */
ossl_param_set_secure_block(last, buf[OSSL_PARAM_BUF_SECURE].alloc,
buf[OSSL_PARAM_BUF_SECURE].alloc_sz);
last->return_size = buf[OSSL_PARAM_BUF_PUBLIC].alloc_sz;
return dst;
}
@ -234,3 +235,18 @@ void OSSL_PARAM_free(OSSL_PARAM *params)
OPENSSL_free(params);
}
}
void OSSL_PARAM_clear_free(OSSL_PARAM *params)
{
if (params != NULL) {
OSSL_PARAM *p;
for (p = params; p->key != NULL; p++)
;
if (p->data_type == OSSL_PARAM_ALLOCATED_END)
OPENSSL_secure_clear_free(p->data, p->data_size);
if (p->return_size > 0 && p->return_size != OSSL_PARAM_UNMODIFIED)
OPENSSL_cleanse(params, p->return_size);
OPENSSL_free(params);
}
}

View File

@ -2,7 +2,7 @@
=head1 NAME
OSSL_PARAM_dup, OSSL_PARAM_merge, OSSL_PARAM_free
OSSL_PARAM_dup, OSSL_PARAM_merge, OSSL_PARAM_free, OSSL_PARAM_clear_free
- OSSL_PARAM array copy functions
=head1 SYNOPSIS
@ -12,6 +12,7 @@ OSSL_PARAM_dup, OSSL_PARAM_merge, OSSL_PARAM_free
OSSL_PARAM *OSSL_PARAM_dup(const OSSL_PARAM *params);
OSSL_PARAM *OSSL_PARAM_merge(const OSSL_PARAM *params, const OSSL_PARAM *params1);
void OSSL_PARAM_free(OSSL_PARAM *params);
void OSSL_PARAM_clear_free(OSSL_PARAM *params);
=head1 DESCRIPTION
@ -34,6 +35,12 @@ OSSL_PARAM_free() frees the parameter array I<params> that was created using
OSSL_PARAM_dup(), OSSL_PARAM_merge() or OSSL_PARAM_BLD_to_param().
If the argument to OSSL_PARAM_free() is NULL, nothing is done.
OSSL_PARAM_clear_free() performs the same function as OSSL_PARAM_free() but
additionally calls OPENSSL_cleanse() on the contents copied in. Note: only
params built via the OSSL_PARAM_dup() or OSSL_PARAM_BLD_to_param() functions
will be effectively cleared, parameters built any other way wil still be freed
but no cleanse operation will be perfomred.
=head1 RETURN VALUES
The functions OSSL_PARAM_dup() and OSSL_PARAM_merge() return a newly allocated
@ -46,7 +53,8 @@ L<OSSL_PARAM(3)>, L<OSSL_PARAM_BLD(3)>
=head1 HISTORY
The functions were added in OpenSSL 3.0.
The OSSL_PARAM_dup, OSSL_PARAM_merge and OSSL_PARAM_free functions were added
in OpenSSL 3.0. OSSL_PARAM_clear_free was added in OpenSSL 3.6.0.
=head1 COPYRIGHT

View File

@ -156,6 +156,7 @@ void OSSL_PARAM_set_all_unmodified(OSSL_PARAM *p);
OSSL_PARAM *OSSL_PARAM_dup(const OSSL_PARAM *p);
OSSL_PARAM *OSSL_PARAM_merge(const OSSL_PARAM *p1, const OSSL_PARAM *p2);
void OSSL_PARAM_free(OSSL_PARAM *p);
void OSSL_PARAM_clear_free(OSSL_PARAM *p);
int OSSL_PARAM_set_octet_string_or_ptr(OSSL_PARAM *p, const void *val,
size_t len);

View File

@ -246,7 +246,11 @@ static int dh_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
}
ok = param_cb(params, cbarg);
#ifdef OPENSSL_PEDANTIC_ZEROIZATION
OSSL_PARAM_clear_free(params);
#else
OSSL_PARAM_free(params);
#endif
err:
OSSL_PARAM_BLD_free(tmpl);
return ok;

View File

@ -248,7 +248,11 @@ static int dsa_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
}
ok = param_cb(params, cbarg);
#ifdef OPENSSL_PEDANTIC_ZEROIZATION
OSSL_PARAM_clear_free(params);
#else
OSSL_PARAM_free(params);
#endif
err:
OSSL_PARAM_BLD_free(tmpl);
return ok;

View File

@ -513,7 +513,11 @@ int ec_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
}
ok = param_cb(params, cbarg);
#ifdef OPENSSL_PEDANTIC_ZEROIZATION
OSSL_PARAM_clear_free(params);
#else
OSSL_PARAM_free(params);
#endif
end:
OSSL_PARAM_BLD_free(tmpl);
OPENSSL_free(pub_key);

View File

@ -262,7 +262,11 @@ static int ecx_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
goto err;
ret = param_cb(params, cbarg);
#ifdef OPENSSL_PEDANTIC_ZEROIZATION
OSSL_PARAM_clear_free(params);
#else
OSSL_PARAM_free(params);
#endif
err:
OSSL_PARAM_BLD_free(tmpl);
return ret;

View File

@ -116,7 +116,11 @@ static int lms_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
goto err;
ret = param_cb(params, cbarg);
#ifdef OPENSSL_PEDANTIC_ZEROIZATION
OSSL_PARAM_clear_free(params);
#else
OSSL_PARAM_free(params);
#endif
err:
OSSL_PARAM_BLD_free(tmpl);
return ret;

View File

@ -291,7 +291,11 @@ static int mac_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
goto err;
ret = param_cb(params, cbarg);
#ifdef OPENSSL_PEDANTIC_ZEROIZATION
OSSL_PARAM_clear_free(params);
#else
OSSL_PARAM_free(params);
#endif
err:
OSSL_PARAM_BLD_free(tmpl);
return ret;

View File

@ -234,7 +234,11 @@ static int rsa_export(void *keydata, int selection,
}
ok = param_callback(params, cbarg);
#ifdef OPENSSL_PEDANTIC_ZEROIZATION
OSSL_PARAM_clear_free(params);
#else
OSSL_PARAM_free(params);
#endif
err:
OSSL_PARAM_BLD_free(tmpl);
return ok;

View File

@ -241,7 +241,11 @@ static int slh_dsa_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
goto err;
ret = param_cb(params, cbarg);
#ifdef OPENSSL_PEDANTIC_ZEROIZATION
OSSL_PARAM_clear_free(params);
#else
OSSL_PARAM_free(params);
#endif
err:
OSSL_PARAM_BLD_free(tmpl);
return ret;

View File

@ -883,6 +883,36 @@ static int test_param_merge(void)
OSSL_PARAM_free(p);
return ret;
}
static int test_param_clear_free(void)
{
int values[] = {1, 2};
OSSL_PARAM *dup, *manual;
OSSL_PARAM base[3];
int ret = 0;
base[0] = OSSL_PARAM_construct_int("int", &values[0]);
base[1] = OSSL_PARAM_construct_int("int", &values[1]);
base[2] = OSSL_PARAM_construct_end();
if (!TEST_ptr(dup = OSSL_PARAM_dup(base)))
goto err;
OSSL_PARAM_clear_free(dup);
if (!TEST_ptr(manual = OPENSSL_zalloc(sizeof(OSSL_PARAM) * 3)))
goto err;
manual[0] = OSSL_PARAM_construct_int("int", &values[0]);
manual[1] = OSSL_PARAM_construct_int("int", &values[1]);
manual[2] = OSSL_PARAM_construct_end();
OSSL_PARAM_clear_free(manual);
ret = 1;
err:
return ret;
}
int setup_tests(void)
{
@ -903,5 +933,6 @@ int setup_tests(void)
ADD_TEST(test_param_modified);
ADD_TEST(test_param_copy_null);
ADD_TEST(test_param_merge);
ADD_TEST(test_param_clear_free);
return 1;
}

View File

@ -5931,3 +5931,4 @@ i2d_PKCS8PrivateKey ? 3_6_0 EXIST::FUNCTION:
OSSL_PARAM_set_octet_string_or_ptr ? 3_6_0 EXIST::FUNCTION:
OSSL_STORE_LOADER_settable_ctx_params ? 3_6_0 EXIST::FUNCTION:
X509_CRL_get0_tbs_sigalg ? 3_6_0 EXIST::FUNCTION:
OSSL_PARAM_clear_free ? 3_6_0 EXIST::FUNCTION: