aboutsummaryrefslogtreecommitdiff
path: root/test/param_build_test.c
diff options
context:
space:
mode:
authorShane Lontis <shane.lontis@oracle.com>2021-04-07 11:27:18 +1000
committerShane Lontis <shane.lontis@oracle.com>2021-04-12 16:55:29 +1000
commit884314cab786a980189206b2cab5f62878a97669 (patch)
treed9b618c36ea0ab2a5401cbf592f1c1282f6d6de5 /test/param_build_test.c
parentd36114d7cd363d505940326f5a2512d9661a67ea (diff)
downloadopenssl-884314cab786a980189206b2cab5f62878a97669.zip
openssl-884314cab786a980189206b2cab5f62878a97669.tar.gz
openssl-884314cab786a980189206b2cab5f62878a97669.tar.bz2
Add OSSL_PARAM_dup() and OSSL_PARAM_merge().
These functions are prerequisites for implementing EVP_PKEY_todata(). OSSL_PARAM_dup() is required to make a deep copy of the exported params (since the provider export() uses a OSSL_PARAM_BLD which throws away the data after the call), and then use OSSL_PARAM_merge() to add some additional params that can be passed to the EVP_PKEY_todata(). Reviewed-by: Paul Dale <pauli@openssl.org> (Merged from https://github.com/openssl/openssl/pull/14785)
Diffstat (limited to 'test/param_build_test.c')
-rw-r--r--test/param_build_test.c193
1 files changed, 178 insertions, 15 deletions
diff --git a/test/param_build_test.c b/test/param_build_test.c
index 270cb45..6419582 100644
--- a/test/param_build_test.c
+++ b/test/param_build_test.c
@@ -14,10 +14,12 @@
#include "internal/nelem.h"
#include "testutil.h"
-static int template_public_test(void)
+static const OSSL_PARAM params_empty[] = { OSSL_PARAM_END };
+
+static int template_public_test(int tstid)
{
OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
- OSSL_PARAM *params = NULL, *p;
+ OSSL_PARAM *params = NULL, *params_blt = NULL, *p1 = NULL, *p;
BIGNUM *bn = NULL, *bn_res = NULL;
int i;
long int l;
@@ -30,7 +32,6 @@ static int template_public_test(void)
int res = 0;
if (!TEST_ptr(bld)
- || !TEST_true(OSSL_PARAM_BLD_push_int(bld, "i", -6))
|| !TEST_true(OSSL_PARAM_BLD_push_long(bld, "l", 42))
|| !TEST_true(OSSL_PARAM_BLD_push_int32(bld, "i32", 1532))
|| !TEST_true(OSSL_PARAM_BLD_push_int64(bld, "i64", -9999999))
@@ -43,9 +44,31 @@ static int template_public_test(void)
sizeof("foo")))
|| !TEST_true(OSSL_PARAM_BLD_push_utf8_ptr(bld, "utf8_p", "bar-boom",
0))
- || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
- /* Check int */
- || !TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
+ || !TEST_true(OSSL_PARAM_BLD_push_int(bld, "i", -6))
+ || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld)))
+ goto err;
+
+ switch(tstid) {
+ case 0:
+ params = params_blt;
+ break;
+ case 1:
+ params = OSSL_PARAM_merge(params_blt, params_empty);
+ break;
+ case 2:
+ params = OSSL_PARAM_dup(params_blt);
+ break;
+ case 3:
+ p1 = OSSL_PARAM_merge(params_blt, params_empty);
+ params = OSSL_PARAM_dup(p1);
+ break;
+ default:
+ p1 = OSSL_PARAM_dup(params_blt);
+ params = OSSL_PARAM_merge(p1, params_empty);
+ break;
+ }
+ /* Check int */
+ if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
|| !TEST_true(OSSL_PARAM_get_int(p, &i))
|| !TEST_str_eq(p->key, "i")
|| !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
@@ -104,7 +127,10 @@ static int template_public_test(void)
goto err;
res = 1;
err:
- OSSL_PARAM_BLD_free_params(params);
+ OPENSSL_free(p1);
+ if (params != params_blt)
+ OPENSSL_free(params);
+ OSSL_PARAM_BLD_free_params(params_blt);
OSSL_PARAM_BLD_free(bld);
OPENSSL_free(utf);
BN_free(bn);
@@ -112,7 +138,7 @@ err:
return res;
}
-static int template_private_test(void)
+static int template_private_test(int tstid)
{
int *data1 = NULL, *data2 = NULL, j;
const int data1_num = 12;
@@ -120,7 +146,7 @@ static int template_private_test(void)
const int data2_num = 5;
const int data2_size = data2_num * sizeof(int);
OSSL_PARAM_BLD *bld = NULL;
- OSSL_PARAM *params = NULL, *p;
+ OSSL_PARAM *params = NULL, *params_blt = NULL, *p1 = NULL, *p;
unsigned int i;
unsigned long int l;
uint32_t i32;
@@ -151,9 +177,29 @@ static int template_private_test(void)
data1_size))
|| !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld, "oct_p", data2,
data2_size))
- || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
- /* Check unsigned int */
- || !TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
+ || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld)))
+ goto err;
+ switch(tstid) {
+ case 0:
+ params = params_blt;
+ break;
+ case 1:
+ params = OSSL_PARAM_merge(params_blt, params_empty);
+ break;
+ case 2:
+ params = OSSL_PARAM_dup(params_blt);
+ break;
+ case 3:
+ p1 = OSSL_PARAM_merge(params_blt, params_empty);
+ params = OSSL_PARAM_dup(p1);
+ break;
+ default:
+ p1 = OSSL_PARAM_dup(params_blt);
+ params = OSSL_PARAM_merge(p1, params_empty);
+ break;
+ }
+ /* Check unsigned int */
+ if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
|| !TEST_false(CRYPTO_secure_allocated(p->data))
|| !TEST_true(OSSL_PARAM_get_uint(p, &i))
|| !TEST_str_eq(p->key, "i")
@@ -216,7 +262,10 @@ static int template_private_test(void)
goto err;
res = 1;
err:
- OSSL_PARAM_BLD_free_params(params);
+ OSSL_PARAM_free(p1);
+ if (params != params_blt)
+ OSSL_PARAM_free(params);
+ OSSL_PARAM_BLD_free_params(params_blt);
OSSL_PARAM_BLD_free(bld);
OPENSSL_secure_free(data1);
OPENSSL_secure_free(data2);
@@ -268,12 +317,126 @@ err:
return res;
}
+static int builder_merge_test(void)
+{
+ static int data1[] = { 2, 3, 5, 7, 11, 15, 17 };
+ static unsigned char data2[] = { 2, 4, 6, 8, 10 };
+ OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
+ OSSL_PARAM_BLD *bld2 = OSSL_PARAM_BLD_new();
+ OSSL_PARAM *params = NULL, *params_blt = NULL, *params2_blt = NULL, *p;
+ unsigned int i;
+ unsigned long int l;
+ uint32_t i32;
+ uint64_t i64;
+ size_t st;
+ BIGNUM *bn_priv = NULL, *bn_priv_res = NULL;
+ BIGNUM *bn_pub = NULL, *bn_pub_res = NULL;
+ int res = 0;
+
+ if (!TEST_ptr(bld)
+ || !TEST_true(OSSL_PARAM_BLD_push_uint(bld, "i", 6))
+ || !TEST_true(OSSL_PARAM_BLD_push_ulong(bld, "l", 42))
+ || !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_size_t(bld, "st", 65537))
+ || !TEST_ptr(bn_priv = BN_secure_new())
+ || !TEST_true(BN_set_word(bn_priv, 1729))
+ || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber_priv", bn_priv))
+ || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld)))
+ goto err;
+
+ if (!TEST_ptr(bld2)
+ || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld2, "oct_s", data1,
+ sizeof(data1)))
+ || !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld2, "oct_p", data2,
+ sizeof(data2)))
+ || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld2, "i32", 99))
+ || !TEST_ptr(bn_pub = BN_new())
+ || !TEST_true(BN_set_word(bn_pub, 0x42))
+ || !TEST_true(OSSL_PARAM_BLD_push_BN(bld2, "bignumber_pub", bn_pub))
+ || !TEST_ptr(params2_blt = OSSL_PARAM_BLD_to_param(bld2)))
+ goto err;
+
+ if (!TEST_ptr(params = OSSL_PARAM_merge(params_blt, params2_blt)))
+ goto err;
+
+ if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
+ || !TEST_true(OSSL_PARAM_get_uint(p, &i))
+ || !TEST_str_eq(p->key, "i")
+ || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
+ || !TEST_size_t_eq(p->data_size, sizeof(int))
+ || !TEST_uint_eq(i, 6)
+ /* Check unsigned int32 */
+ || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32"))
+ || !TEST_true(OSSL_PARAM_get_uint32(p, &i32))
+ || !TEST_str_eq(p->key, "i32")
+ || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
+ || !TEST_size_t_eq(p->data_size, sizeof(int32_t))
+ || !TEST_uint_eq((unsigned int)i32, 99)
+ /* Check unsigned int64 */
+ || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64"))
+ || !TEST_str_eq(p->key, "i64")
+ || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
+ || !TEST_size_t_eq(p->data_size, sizeof(int64_t))
+ || !TEST_true(OSSL_PARAM_get_uint64(p, &i64))
+ || !TEST_ulong_eq((unsigned long)i64, 9999999)
+ /* Check unsigned long int */
+ || !TEST_ptr(p = OSSL_PARAM_locate(params, "l"))
+ || !TEST_str_eq(p->key, "l")
+ || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
+ || !TEST_size_t_eq(p->data_size, sizeof(unsigned long int))
+ || !TEST_true(OSSL_PARAM_get_ulong(p, &l))
+ || !TEST_ulong_eq(l, 42)
+ /* Check size_t */
+ || !TEST_ptr(p = OSSL_PARAM_locate(params, "st"))
+ || !TEST_str_eq(p->key, "st")
+ || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
+ || !TEST_size_t_eq(p->data_size, sizeof(size_t))
+ || !TEST_true(OSSL_PARAM_get_size_t(p, &st))
+ || !TEST_size_t_eq(st, 65537)
+ /* Check octet string */
+ || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_s"))
+ || !TEST_str_eq(p->key, "oct_s")
+ || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_STRING)
+ || !TEST_mem_eq(p->data, p->data_size, data1, sizeof(data1))
+ /* Check octet pointer */
+ || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_p"))
+ || !TEST_str_eq(p->key, "oct_p")
+ || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_PTR)
+ || !TEST_mem_eq(*(void **)p->data, p->data_size, data2, sizeof(data2))
+ /* Check BN */
+ || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber_pub"))
+ || !TEST_str_eq(p->key, "bignumber_pub")
+ || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
+ || !TEST_true(OSSL_PARAM_get_BN(p, &bn_pub_res))
+ || !TEST_int_eq(BN_cmp(bn_pub_res, bn_pub), 0)
+ || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber_priv"))
+ || !TEST_str_eq(p->key, "bignumber_priv")
+ || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
+ || !TEST_true(OSSL_PARAM_get_BN(p, &bn_priv_res))
+ || !TEST_int_eq(BN_cmp(bn_priv_res, bn_priv), 0))
+ goto err;
+ res = 1;
+err:
+ OSSL_PARAM_free(params);
+ OSSL_PARAM_free(params_blt);
+ OSSL_PARAM_free(params2_blt);
+ OSSL_PARAM_BLD_free(bld);
+ OSSL_PARAM_BLD_free(bld2);
+ BN_free(bn_priv);
+ BN_free(bn_priv_res);
+ BN_free(bn_pub);
+ BN_free(bn_pub_res);
+ return res;
+}
+
int setup_tests(void)
{
- ADD_TEST(template_public_test);
+ ADD_ALL_TESTS(template_public_test, 5);
/* Only run the secure memory testing if we have secure memory available */
if (CRYPTO_secure_malloc_init(1<<16, 16))
- ADD_TEST(template_private_test);
+ ADD_ALL_TESTS(template_private_test, 5);
ADD_TEST(builder_limit_test);
+ ADD_TEST(builder_merge_test);
return 1;
}