mirror of
https://github.com/QuasarApp/openssl.git
synced 2025-05-21 13:59:40 +00:00
Add param builder free function.
This means include deallocation information in the return from the ossl_param_bld_to_param function. Reviewed-by: Richard Levitte <levitte@openssl.org> (Merged from https://github.com/openssl/openssl/pull/9404)
This commit is contained in:
parent
38f6f99cdf
commit
7312ef3fc4
@ -15,6 +15,8 @@
|
|||||||
#include "internal/cryptlib.h"
|
#include "internal/cryptlib.h"
|
||||||
#include "internal/param_build.h"
|
#include "internal/param_build.h"
|
||||||
|
|
||||||
|
#define OSSL_PARAM_ALLOCATED_END 127
|
||||||
|
|
||||||
typedef union {
|
typedef union {
|
||||||
OSSL_UNION_ALIGN;
|
OSSL_UNION_ALIGN;
|
||||||
} OSSL_PARAM_BLD_BLOCK;
|
} OSSL_PARAM_BLD_BLOCK;
|
||||||
@ -274,40 +276,50 @@ static OSSL_PARAM *param_bld_convert(OSSL_PARAM_BLD *bld, OSSL_PARAM *param,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
param[i] = OSSL_PARAM_construct_end();
|
param[i] = OSSL_PARAM_construct_end();
|
||||||
return param;
|
return param + i;
|
||||||
}
|
}
|
||||||
|
|
||||||
OSSL_PARAM *ossl_param_bld_to_param(OSSL_PARAM_BLD *bld, void **secure)
|
OSSL_PARAM *ossl_param_bld_to_param(OSSL_PARAM_BLD *bld)
|
||||||
{
|
{
|
||||||
OSSL_PARAM_BLD_BLOCK *blk, *s = NULL;
|
OSSL_PARAM_BLD_BLOCK *blk, *s = NULL;
|
||||||
OSSL_PARAM *param;
|
OSSL_PARAM *params, *last;
|
||||||
const size_t p_blks = bytes_to_blocks((bld->curr + 1) * sizeof(*param));
|
const size_t p_blks = bytes_to_blocks((1 + bld->curr) * sizeof(*params));
|
||||||
const size_t total = ALIGN_SIZE * (p_blks + bld->total_blocks);
|
const size_t total = ALIGN_SIZE * (p_blks + bld->total_blocks);
|
||||||
|
const size_t ss = ALIGN_SIZE * bld->secure_blocks;
|
||||||
|
|
||||||
if (bld->secure_blocks > 0) {
|
if (ss > 0) {
|
||||||
if (secure == NULL) {
|
s = OPENSSL_secure_malloc(ss);
|
||||||
CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_TO_PARAM,
|
|
||||||
CRYPTO_R_INVALID_NULL_ARGUMENT);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
s = OPENSSL_secure_malloc(bld->secure_blocks * ALIGN_SIZE);
|
|
||||||
if (s == NULL) {
|
if (s == NULL) {
|
||||||
CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_TO_PARAM,
|
CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_TO_PARAM,
|
||||||
CRYPTO_R_SECURE_MALLOC_FAILURE);
|
CRYPTO_R_SECURE_MALLOC_FAILURE);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
param = OPENSSL_malloc(total);
|
params = OPENSSL_malloc(total);
|
||||||
if (param == NULL) {
|
if (params == NULL) {
|
||||||
CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_TO_PARAM, ERR_R_MALLOC_FAILURE);
|
CRYPTOerr(CRYPTO_F_OSSL_PARAM_BLD_TO_PARAM, ERR_R_MALLOC_FAILURE);
|
||||||
OPENSSL_secure_free(s);
|
OPENSSL_secure_free(s);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (secure != NULL)
|
blk = p_blks + (OSSL_PARAM_BLD_BLOCK *)(params);
|
||||||
*secure = s;
|
last = param_bld_convert(bld, params, blk, s);
|
||||||
blk = p_blks + (OSSL_PARAM_BLD_BLOCK *)(param);
|
last->data_size = ss;
|
||||||
param_bld_convert(bld, param, blk, s);
|
last->data = s;
|
||||||
return param;
|
last->data_type = OSSL_PARAM_ALLOCATED_END;
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ossl_param_bld_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);
|
||||||
|
OPENSSL_free(params);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
OSSL_PARAM *ossl_param_bld_to_param_ex(OSSL_PARAM_BLD *bld, OSSL_PARAM *params,
|
OSSL_PARAM *ossl_param_bld_to_param_ex(OSSL_PARAM_BLD *bld, OSSL_PARAM *params,
|
||||||
|
@ -2,32 +2,33 @@
|
|||||||
|
|
||||||
=head1 NAME
|
=head1 NAME
|
||||||
|
|
||||||
ossl_param_bld_init,
|
ossl_param_bld_init, ossl_param_bld_to_param, ossl_param_bld_to_param_ex,
|
||||||
ossl_param_bld_to_param, ossl_param_bld_push_int,
|
ossl_param_bld_free, ossl_param_bld_push_int, ossl_param_bld_push_uint,
|
||||||
ossl_param_bld_push_uint, ossl_param_bld_push_long,
|
ossl_param_bld_push_long, ossl_param_bld_push_ulong,
|
||||||
ossl_param_bld_push_ulong, ossl_param_bld_push_int32,
|
ossl_param_bld_push_int32, ossl_param_bld_push_uint32,
|
||||||
ossl_param_bld_push_uint32, ossl_param_bld_push_int64,
|
ossl_param_bld_push_int64, ossl_param_bld_push_uint64,
|
||||||
ossl_param_bld_push_uint64, ossl_param_bld_push_size_t,
|
ossl_param_bld_push_size_t, ossl_param_bld_push_double,
|
||||||
ossl_param_bld_push_double, ossl_param_bld_push_BN,
|
ossl_param_bld_push_BN, ossl_param_bld_push_utf8_string,
|
||||||
ossl_param_bld_push_utf8_string, ossl_param_bld_push_utf8_ptr,
|
ossl_param_bld_push_utf8_ptr, ossl_param_bld_push_octet_string,
|
||||||
ossl_param_bld_push_octet_string, ossl_param_bld_push_octet_ptr
|
ossl_param_bld_push_octet_ptr
|
||||||
- functions to assist in the creation of OSSL_PARAM arrays
|
- functions to assist in the creation of OSSL_PARAM arrays
|
||||||
|
|
||||||
=head1 SYNOPSIS
|
=head1 SYNOPSIS
|
||||||
|
|
||||||
=for comment generic
|
=for comment generic
|
||||||
|
|
||||||
#include "internal/params_template.h"
|
#include "internal/params_build.h"
|
||||||
|
|
||||||
#define OSSL_PARAM_BLD_MAX 10
|
#define OSSL_PARAM_BLD_MAX 10
|
||||||
typedef struct { ... } OSSL_PARAM_BLD;
|
typedef struct { ... } OSSL_PARAM_BLD;
|
||||||
|
|
||||||
void ossl_param_bld_init(OSSL_PARAM_BLD *bld);
|
void ossl_param_bld_init(OSSL_PARAM_BLD *bld);
|
||||||
OSSL_PARAM *ossl_param_bld_to_param(OSSL_PARAM_BLD *bld, void **secure);
|
OSSL_PARAM *ossl_param_bld_to_param(OSSL_PARAM_BLD *bld);
|
||||||
OSSL_PARAM *ossl_param_bld_to_param_ex(OSSL_PARAM_BLD *bld,
|
OSSL_PARAM *ossl_param_bld_to_param_ex(OSSL_PARAM_BLD *bld,
|
||||||
OSSL_PARAM *params, size_t param_n,
|
OSSL_PARAM *params, size_t param_n,
|
||||||
void *data, size_t data_n,
|
void *data, size_t data_n,
|
||||||
void *secure, size_t secure_n);
|
void *secure, size_t secure_n);
|
||||||
|
void ossl_param_bld_free(OSSL_PARAM *params);
|
||||||
|
|
||||||
int ossl_param_bld_push_TYPE(OSSL_PARAM_BLD *bld, const char *key, TYPE val);
|
int ossl_param_bld_push_TYPE(OSSL_PARAM_BLD *bld, const char *key, TYPE val);
|
||||||
|
|
||||||
@ -55,11 +56,11 @@ Any existing values are cleared.
|
|||||||
|
|
||||||
ossl_param_bld_to_param() converts a built up OSSL_PARAM_BLD structure
|
ossl_param_bld_to_param() converts a built up OSSL_PARAM_BLD structure
|
||||||
B<bld> into an allocated OSSL_PARAM array.
|
B<bld> into an allocated OSSL_PARAM array.
|
||||||
The pointer referenced by the B<secure> argument is set to point to an
|
The OSSL_PARAM array and all associated storage must be freed by calling
|
||||||
allocated block of secure memory if required and to NULL it not.
|
ossl_param_bld_free() with the functions return value.
|
||||||
The OSSL_PARAM array and all associated storage can be freed by calling
|
|
||||||
OPENSSL_free() with the functions return value and OPENSSL_secure_free()
|
ossl_param_bld_free() deallocates the memory allocated by
|
||||||
with the pointer referenced by B<secure>.
|
ossl_param_bld_to_param().
|
||||||
|
|
||||||
ossl_param_bld_to_param_ex() behaves like ossl_param_bld_to_param(), except that
|
ossl_param_bld_to_param_ex() behaves like ossl_param_bld_to_param(), except that
|
||||||
no additional memory is allocated.
|
no additional memory is allocated.
|
||||||
@ -76,8 +77,8 @@ B<val> is stored by value and an expression or auto variable can be used.
|
|||||||
|
|
||||||
ossl_param_bld_push_BN() is a function that will create an OSSL_PARAM object
|
ossl_param_bld_push_BN() is a function that will create an OSSL_PARAM object
|
||||||
that holds the specified BIGNUM B<bn>.
|
that holds the specified BIGNUM B<bn>.
|
||||||
If B<bn> is marked as being securely allocated, the secure flag is
|
If B<bn> is marked as being securely allocated, it's OSSL_PARAM representation
|
||||||
set in the OSSL_PARAM_BLD structure.
|
will also be securely allocated.
|
||||||
The B<bn> argument is stored by reference and the underlying BIGNUM object
|
The B<bn> argument is stored by reference and the underlying BIGNUM object
|
||||||
must exist until after ossl_param_bld_to_param() has been called.
|
must exist until after ossl_param_bld_to_param() has been called.
|
||||||
|
|
||||||
@ -137,7 +138,6 @@ private key.
|
|||||||
|
|
||||||
OSSL_PARAM_BLD bld;
|
OSSL_PARAM_BLD bld;
|
||||||
OSSL_PARAM *params;
|
OSSL_PARAM *params;
|
||||||
void *secure;
|
|
||||||
|
|
||||||
ossl_param_bld_init(&bld, &secure);
|
ossl_param_bld_init(&bld, &secure);
|
||||||
if (!ossl_param_bld_push_BN(&bld, "p", p)
|
if (!ossl_param_bld_push_BN(&bld, "p", p)
|
||||||
@ -149,8 +149,7 @@ private key.
|
|||||||
goto err;
|
goto err;
|
||||||
/* Use params */
|
/* Use params */
|
||||||
...
|
...
|
||||||
OPENSSL_free(params);
|
ossl_param_bld_free(params);
|
||||||
OPENSSL_secure_free(secure);
|
|
||||||
|
|
||||||
=head2 Example 2
|
=head2 Example 2
|
||||||
|
|
||||||
@ -159,7 +158,6 @@ public key.
|
|||||||
|
|
||||||
OSSL_PARAM_BLD bld;
|
OSSL_PARAM_BLD bld;
|
||||||
OSSL_PARAM *params;
|
OSSL_PARAM *params;
|
||||||
void *secure;
|
|
||||||
|
|
||||||
ossl_param_bld_init(&bld, &secure);
|
ossl_param_bld_init(&bld, &secure);
|
||||||
if (!ossl_param_bld_push_BN(&bld, "n", n)
|
if (!ossl_param_bld_push_BN(&bld, "n", n)
|
||||||
@ -168,8 +166,7 @@ public key.
|
|||||||
goto err;
|
goto err;
|
||||||
/* Use params */
|
/* Use params */
|
||||||
...
|
...
|
||||||
OPENSSL_free(params);
|
ossl_param_bld_free(params);
|
||||||
OPENSSL_secure_free(secure);
|
|
||||||
|
|
||||||
=head1 SEE ALSO
|
=head1 SEE ALSO
|
||||||
|
|
||||||
|
@ -40,7 +40,8 @@ typedef struct {
|
|||||||
} OSSL_PARAM_BLD;
|
} OSSL_PARAM_BLD;
|
||||||
|
|
||||||
void ossl_param_bld_init(OSSL_PARAM_BLD *bld);
|
void ossl_param_bld_init(OSSL_PARAM_BLD *bld);
|
||||||
OSSL_PARAM *ossl_param_bld_to_param(OSSL_PARAM_BLD *bld, void **secure);
|
OSSL_PARAM *ossl_param_bld_to_param(OSSL_PARAM_BLD *bld);
|
||||||
|
void ossl_param_bld_free(OSSL_PARAM *params);
|
||||||
OSSL_PARAM *ossl_param_bld_to_param_ex(OSSL_PARAM_BLD *bld,
|
OSSL_PARAM *ossl_param_bld_to_param_ex(OSSL_PARAM_BLD *bld,
|
||||||
OSSL_PARAM *params, size_t param_n,
|
OSSL_PARAM *params, size_t param_n,
|
||||||
void *data, size_t data_n,
|
void *data, size_t data_n,
|
||||||
|
@ -18,7 +18,7 @@ static int template_public_test(void)
|
|||||||
{
|
{
|
||||||
OSSL_PARAM_BLD bld;
|
OSSL_PARAM_BLD bld;
|
||||||
OSSL_PARAM *params = NULL, *p;
|
OSSL_PARAM *params = NULL, *p;
|
||||||
void *secure = (void *)"abc";
|
BIGNUM *bn = NULL, *bn_res = NULL;
|
||||||
int i;
|
int i;
|
||||||
long int l;
|
long int l;
|
||||||
int32_t i32;
|
int32_t i32;
|
||||||
@ -34,12 +34,14 @@ static int template_public_test(void)
|
|||||||
|| !TEST_true(ossl_param_bld_push_int32(&bld, "i32", 1532))
|
|| !TEST_true(ossl_param_bld_push_int32(&bld, "i32", 1532))
|
||||||
|| !TEST_true(ossl_param_bld_push_int64(&bld, "i64", -9999999))
|
|| !TEST_true(ossl_param_bld_push_int64(&bld, "i64", -9999999))
|
||||||
|| !TEST_true(ossl_param_bld_push_double(&bld, "d", 1.61803398875))
|
|| !TEST_true(ossl_param_bld_push_double(&bld, "d", 1.61803398875))
|
||||||
|
|| !TEST_ptr(bn = BN_new())
|
||||||
|
|| !TEST_true(BN_set_word(bn, 1729))
|
||||||
|
|| !TEST_true(ossl_param_bld_push_BN(&bld, "bignumber", bn))
|
||||||
|| !TEST_true(ossl_param_bld_push_utf8_string(&bld, "utf8_s", "foo",
|
|| !TEST_true(ossl_param_bld_push_utf8_string(&bld, "utf8_s", "foo",
|
||||||
sizeof("foo")))
|
sizeof("foo")))
|
||||||
|| !TEST_true(ossl_param_bld_push_utf8_ptr(&bld, "utf8_p", "bar-boom",
|
|| !TEST_true(ossl_param_bld_push_utf8_ptr(&bld, "utf8_p", "bar-boom",
|
||||||
0))
|
0))
|
||||||
|| !TEST_ptr(params = ossl_param_bld_to_param(&bld, &secure))
|
|| !TEST_ptr(params = ossl_param_bld_to_param(&bld))
|
||||||
|| !TEST_ptr_null(secure)
|
|
||||||
/* Check int */
|
/* Check int */
|
||||||
|| !TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
|
|| !TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
|
||||||
|| !TEST_true(OSSL_PARAM_get_int(p, &i))
|
|| !TEST_true(OSSL_PARAM_get_int(p, &i))
|
||||||
@ -83,13 +85,20 @@ static int template_public_test(void)
|
|||||||
/* Check UTF8 pointer */
|
/* Check UTF8 pointer */
|
||||||
|| !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_p"))
|
|| !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_p"))
|
||||||
|| !TEST_true(OSSL_PARAM_get_utf8_ptr(p, &cutf))
|
|| !TEST_true(OSSL_PARAM_get_utf8_ptr(p, &cutf))
|
||||||
|| !TEST_str_eq(cutf, "bar-boom"))
|
|| !TEST_str_eq(cutf, "bar-boom")
|
||||||
|
/* Check BN */
|
||||||
|
|| !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber"))
|
||||||
|
|| !TEST_str_eq(p->key, "bignumber")
|
||||||
|
|| !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
|
||||||
|
|| !TEST_true(OSSL_PARAM_get_BN(p, &bn_res))
|
||||||
|
|| !TEST_int_eq(BN_cmp(bn_res, bn), 0))
|
||||||
goto err;
|
goto err;
|
||||||
res = 1;
|
res = 1;
|
||||||
err:
|
err:
|
||||||
OPENSSL_free(params);
|
ossl_param_bld_free(params);
|
||||||
OPENSSL_secure_free(secure);
|
|
||||||
OPENSSL_free(utf);
|
OPENSSL_free(utf);
|
||||||
|
BN_free(bn);
|
||||||
|
BN_free(bn_res);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,7 +108,6 @@ static int template_private_test(void)
|
|||||||
static unsigned char data2[] = { 2, 4, 6, 8, 10 };
|
static unsigned char data2[] = { 2, 4, 6, 8, 10 };
|
||||||
OSSL_PARAM_BLD bld;
|
OSSL_PARAM_BLD bld;
|
||||||
OSSL_PARAM *params = NULL, *p;
|
OSSL_PARAM *params = NULL, *p;
|
||||||
void *secure = (void *)"abc";
|
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
unsigned long int l;
|
unsigned long int l;
|
||||||
uint32_t i32;
|
uint32_t i32;
|
||||||
@ -114,14 +122,14 @@ static int template_private_test(void)
|
|||||||
|| !TEST_true(ossl_param_bld_push_uint32(&bld, "i32", 1532))
|
|| !TEST_true(ossl_param_bld_push_uint32(&bld, "i32", 1532))
|
||||||
|| !TEST_true(ossl_param_bld_push_uint64(&bld, "i64", 9999999))
|
|| !TEST_true(ossl_param_bld_push_uint64(&bld, "i64", 9999999))
|
||||||
|| !TEST_true(ossl_param_bld_push_size_t(&bld, "st", 65537))
|
|| !TEST_true(ossl_param_bld_push_size_t(&bld, "st", 65537))
|
||||||
|| !TEST_ptr(bn = BN_new())
|
|| !TEST_ptr(bn = BN_secure_new())
|
||||||
|| !TEST_true(BN_set_word(bn, 1729))
|
|| !TEST_true(BN_set_word(bn, 1729))
|
||||||
|| !TEST_true(ossl_param_bld_push_BN(&bld, "bignumber", bn))
|
|| !TEST_true(ossl_param_bld_push_BN(&bld, "bignumber", bn))
|
||||||
|| !TEST_true(ossl_param_bld_push_octet_string(&bld, "oct_s", data1,
|
|| !TEST_true(ossl_param_bld_push_octet_string(&bld, "oct_s", data1,
|
||||||
sizeof(data1)))
|
sizeof(data1)))
|
||||||
|| !TEST_true(ossl_param_bld_push_octet_ptr(&bld, "oct_p", data2,
|
|| !TEST_true(ossl_param_bld_push_octet_ptr(&bld, "oct_p", data2,
|
||||||
sizeof(data2)))
|
sizeof(data2)))
|
||||||
|| !TEST_ptr(params = ossl_param_bld_to_param(&bld, &secure))
|
|| !TEST_ptr(params = ossl_param_bld_to_param(&bld))
|
||||||
/* Check unsigned int */
|
/* Check unsigned int */
|
||||||
|| !TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
|
|| !TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
|
||||||
|| !TEST_true(OSSL_PARAM_get_uint(p, &i))
|
|| !TEST_true(OSSL_PARAM_get_uint(p, &i))
|
||||||
@ -176,8 +184,7 @@ static int template_private_test(void)
|
|||||||
goto err;
|
goto err;
|
||||||
res = 1;
|
res = 1;
|
||||||
err:
|
err:
|
||||||
OPENSSL_secure_free(secure);
|
ossl_param_bld_free(params);
|
||||||
OPENSSL_free(params);
|
|
||||||
BN_free(bn);
|
BN_free(bn);
|
||||||
BN_free(bn_res);
|
BN_free(bn_res);
|
||||||
return res;
|
return res;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user