1 /* 2 * Copyright 2019-2026 The OpenSSL Project Authors. All Rights Reserved. 3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 4 * 5 * Licensed under the Apache License 2.0 (the "License"). You may not use 6 * this file except in compliance with the License. You can obtain a copy 7 * in the file LICENSE in the source distribution or at 8 * https://www.openssl.org/source/license.html 9 */ 10 11 #include <string.h> 12 #include <openssl/params.h> 13 #include <openssl/param_build.h> 14 #include "internal/nelem.h" 15 #include "testutil.h" 16 17 static const OSSL_PARAM params_empty[] = { OSSL_PARAM_END }; 18 19 static int template_public_single_zero_test(int idx) 20 { 21 OSSL_PARAM_BLD *bld = NULL; 22 OSSL_PARAM *params = NULL, *params_blt = NULL, *p; 23 BIGNUM *zbn = NULL, *zbn_res = NULL; 24 int res = 0; 25 26 if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) 27 || !TEST_ptr(zbn = BN_new()) 28 || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "zeronumber", 29 idx == 0 ? zbn : NULL)) 30 || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld))) 31 goto err; 32 33 params = params_blt; 34 /* Check BN (zero BN becomes unsigned integer) */ 35 if (!TEST_ptr(p = OSSL_PARAM_locate(params, "zeronumber")) 36 || !TEST_str_eq(p->key, "zeronumber") 37 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) 38 || !TEST_true(OSSL_PARAM_get_BN(p, &zbn_res)) 39 || !TEST_BN_eq(zbn_res, zbn)) 40 goto err; 41 res = 1; 42 err: 43 if (params != params_blt) 44 OPENSSL_free(params); 45 OSSL_PARAM_free(params_blt); 46 OSSL_PARAM_BLD_free(bld); 47 BN_free(zbn); 48 BN_free(zbn_res); 49 return res; 50 } 51 52 static int template_private_single_zero_test(void) 53 { 54 OSSL_PARAM_BLD *bld = NULL; 55 OSSL_PARAM *params = NULL, *params_blt = NULL, *p; 56 BIGNUM *zbn = NULL, *zbn_res = NULL; 57 int res = 0; 58 59 if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) 60 || !TEST_ptr(zbn = BN_secure_new()) 61 || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "zeronumber", zbn)) 62 || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld))) 63 goto err; 64 65 params = params_blt; 66 /* Check BN (zero BN becomes unsigned integer) */ 67 if (!TEST_ptr(p = OSSL_PARAM_locate(params, "zeronumber")) 68 || !TEST_true(CRYPTO_secure_allocated(p->data)) 69 || !TEST_str_eq(p->key, "zeronumber") 70 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) 71 || !TEST_true(OSSL_PARAM_get_BN(p, &zbn_res)) 72 || !TEST_int_eq(BN_get_flags(zbn, BN_FLG_SECURE), BN_FLG_SECURE) 73 || !TEST_BN_eq(zbn_res, zbn)) 74 goto err; 75 res = 1; 76 err: 77 if (params != params_blt) 78 OPENSSL_free(params); 79 OSSL_PARAM_free(params_blt); 80 OSSL_PARAM_BLD_free(bld); 81 BN_free(zbn); 82 BN_free(zbn_res); 83 return res; 84 } 85 86 static int template_public_test(int tstid) 87 { 88 OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new(); 89 OSSL_PARAM *params = NULL, *params_blt = NULL, *p1 = NULL, *p; 90 BIGNUM *zbn = NULL, *zbn_res = NULL; 91 BIGNUM *pbn = NULL, *pbn_res = NULL; 92 BIGNUM *nbn = NULL, *nbn_res = NULL; 93 int i; 94 long int l; 95 int32_t i32; 96 int64_t i64; 97 double d; 98 time_t t; 99 char *utf = NULL; 100 const char *cutf; 101 int res = 0; 102 103 if (!TEST_ptr(bld) 104 || !TEST_true(OSSL_PARAM_BLD_push_long(bld, "l", 42)) 105 || !TEST_true(OSSL_PARAM_BLD_push_int32(bld, "i32", 1532)) 106 || !TEST_true(OSSL_PARAM_BLD_push_int64(bld, "i64", -9999999)) 107 || !TEST_true(OSSL_PARAM_BLD_push_time_t(bld, "t", 11224)) 108 || !TEST_true(OSSL_PARAM_BLD_push_double(bld, "d", 1.61803398875)) 109 || !TEST_ptr(zbn = BN_new()) 110 || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "zeronumber", zbn)) 111 || !TEST_ptr(pbn = BN_new()) 112 || !TEST_true(BN_set_word(pbn, 1729)) 113 || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", pbn)) 114 || !TEST_ptr(nbn = BN_secure_new()) 115 || !TEST_true(BN_set_word(nbn, 1733)) 116 || !TEST_true((BN_set_negative(nbn, 1), 1)) 117 || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "negativebignumber", nbn)) 118 || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, "utf8_s", "foo", 119 sizeof("foo"))) 120 || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, "octet_s", NULL, 121 0)) 122 || !TEST_true(OSSL_PARAM_BLD_push_utf8_ptr(bld, "utf8_p", "bar-boom", 123 0)) 124 || !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld, "octet_p", NULL, 125 0)) 126 || !TEST_true(OSSL_PARAM_BLD_push_int(bld, "i", -6)) 127 || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld))) 128 goto err; 129 130 switch (tstid) { 131 case 0: 132 params = params_blt; 133 break; 134 case 1: 135 params = OSSL_PARAM_merge(params_blt, params_empty); 136 break; 137 case 2: 138 params = OSSL_PARAM_dup(params_blt); 139 break; 140 case 3: 141 p1 = OSSL_PARAM_merge(params_blt, params_empty); 142 params = OSSL_PARAM_dup(p1); 143 break; 144 default: 145 p1 = OSSL_PARAM_dup(params_blt); 146 params = OSSL_PARAM_merge(p1, params_empty); 147 break; 148 } 149 /* Check int */ 150 if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i")) 151 || !TEST_true(OSSL_PARAM_get_int(p, &i)) 152 || !TEST_str_eq(p->key, "i") 153 || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) 154 || !TEST_size_t_eq(p->data_size, sizeof(int)) 155 || !TEST_int_eq(i, -6) 156 /* Check int32 */ 157 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32")) 158 || !TEST_true(OSSL_PARAM_get_int32(p, &i32)) 159 || !TEST_str_eq(p->key, "i32") 160 || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) 161 || !TEST_size_t_eq(p->data_size, sizeof(int32_t)) 162 || !TEST_int_eq((int)i32, 1532) 163 /* Check int64 */ 164 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64")) 165 || !TEST_str_eq(p->key, "i64") 166 || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) 167 || !TEST_size_t_eq(p->data_size, sizeof(int64_t)) 168 || !TEST_true(OSSL_PARAM_get_int64(p, &i64)) 169 || !TEST_long_eq((long)i64, -9999999) 170 /* Check long */ 171 || !TEST_ptr(p = OSSL_PARAM_locate(params, "l")) 172 || !TEST_str_eq(p->key, "l") 173 || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) 174 || !TEST_size_t_eq(p->data_size, sizeof(long int)) 175 || !TEST_true(OSSL_PARAM_get_long(p, &l)) 176 || !TEST_long_eq(l, 42) 177 /* Check time_t */ 178 || !TEST_ptr(p = OSSL_PARAM_locate(params, "t")) 179 || !TEST_str_eq(p->key, "t") 180 || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) 181 || !TEST_size_t_eq(p->data_size, sizeof(time_t)) 182 || !TEST_true(OSSL_PARAM_get_time_t(p, &t)) 183 || !TEST_time_t_eq(t, 11224) 184 /* Check double */ 185 || !TEST_ptr(p = OSSL_PARAM_locate(params, "d")) 186 || !TEST_true(OSSL_PARAM_get_double(p, &d)) 187 || !TEST_str_eq(p->key, "d") 188 || !TEST_uint_eq(p->data_type, OSSL_PARAM_REAL) 189 || !TEST_size_t_eq(p->data_size, sizeof(double)) 190 || !TEST_double_eq(d, 1.61803398875) 191 /* Check UTF8 string */ 192 || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_s")) 193 || !TEST_str_eq(p->data, "foo") 194 || !TEST_true(OSSL_PARAM_get_utf8_string(p, &utf, 0)) 195 || !TEST_str_eq(utf, "foo") 196 /* Check NULL octet string */ 197 || !TEST_ptr(p = OSSL_PARAM_locate(params, "octet_s")) 198 || !TEST_size_t_eq(p->data_size, 0) 199 /* Check UTF8 pointer */ 200 || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_p")) 201 || !TEST_true(OSSL_PARAM_get_utf8_ptr(p, &cutf)) 202 || !TEST_str_eq(cutf, "bar-boom") 203 /* Check NULL octet ptr */ 204 || !TEST_ptr(p = OSSL_PARAM_locate(params, "octet_p")) 205 || !TEST_size_t_eq(p->data_size, 0) 206 /* Check BN (zero BN becomes unsigned integer) */ 207 || !TEST_ptr(p = OSSL_PARAM_locate(params, "zeronumber")) 208 || !TEST_str_eq(p->key, "zeronumber") 209 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) 210 || !TEST_true(OSSL_PARAM_get_BN(p, &zbn_res)) 211 || !TEST_BN_eq(zbn_res, zbn) 212 /* Check BN (positive BN becomes unsigned integer) */ 213 || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber")) 214 || !TEST_str_eq(p->key, "bignumber") 215 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) 216 || !TEST_true(OSSL_PARAM_get_BN(p, &pbn_res)) 217 || !TEST_BN_eq(pbn_res, pbn) 218 /* Check BN (negative BN becomes signed integer) */ 219 || !TEST_ptr(p = OSSL_PARAM_locate(params, "negativebignumber")) 220 || !TEST_str_eq(p->key, "negativebignumber") 221 || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) 222 || !TEST_true(OSSL_PARAM_get_BN(p, &nbn_res)) 223 || !TEST_BN_eq(nbn_res, nbn)) 224 goto err; 225 res = 1; 226 err: 227 OPENSSL_free(p1); 228 if (params != params_blt) 229 OPENSSL_free(params); 230 OSSL_PARAM_free(params_blt); 231 OSSL_PARAM_BLD_free(bld); 232 OPENSSL_free(utf); 233 BN_free(zbn); 234 BN_free(zbn_res); 235 BN_free(pbn); 236 BN_free(pbn_res); 237 BN_free(nbn); 238 BN_free(nbn_res); 239 return res; 240 } 241 242 static int template_private_test(int tstid) 243 { 244 int *data1 = NULL, *data2 = NULL, j; 245 const int data1_num = 12; 246 const int data1_size = data1_num * sizeof(int); 247 const int data2_num = 5; 248 const int data2_size = data2_num * sizeof(int); 249 OSSL_PARAM_BLD *bld = NULL; 250 OSSL_PARAM *params = NULL, *params_blt = NULL, *p1 = NULL, *p; 251 unsigned int i; 252 unsigned long int l; 253 uint32_t i32; 254 uint64_t i64; 255 size_t st; 256 BIGNUM *zbn = NULL, *zbn_res = NULL; 257 BIGNUM *pbn = NULL, *pbn_res = NULL; 258 BIGNUM *nbn = NULL, *nbn_res = NULL; 259 int res = 0; 260 261 if (!TEST_ptr(data1 = OPENSSL_secure_malloc(data1_size)) 262 || !TEST_ptr(data2 = OPENSSL_secure_malloc(data2_size)) 263 || !TEST_ptr(bld = OSSL_PARAM_BLD_new())) 264 goto err; 265 266 for (j = 0; j < data1_num; j++) 267 data1[j] = -16 * j; 268 for (j = 0; j < data2_num; j++) 269 data2[j] = 2 * j; 270 271 if (!TEST_true(OSSL_PARAM_BLD_push_uint(bld, "i", 6)) 272 || !TEST_true(OSSL_PARAM_BLD_push_ulong(bld, "l", 42)) 273 || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld, "i32", 1532)) 274 || !TEST_true(OSSL_PARAM_BLD_push_uint64(bld, "i64", 9999999)) 275 || !TEST_true(OSSL_PARAM_BLD_push_size_t(bld, "st", 65537)) 276 || !TEST_ptr(zbn = BN_secure_new()) 277 || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "zeronumber", zbn)) 278 || !TEST_ptr(pbn = BN_secure_new()) 279 || !TEST_true(BN_set_word(pbn, 1729)) 280 || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", pbn)) 281 || !TEST_ptr(nbn = BN_secure_new()) 282 || !TEST_true(BN_set_word(nbn, 1733)) 283 || !TEST_true((BN_set_negative(nbn, 1), 1)) 284 || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "negativebignumber", nbn)) 285 || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, "oct_s", data1, 286 data1_size)) 287 || !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld, "oct_p", data2, 288 data2_size)) 289 || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld))) 290 goto err; 291 switch (tstid) { 292 case 0: 293 params = params_blt; 294 break; 295 case 1: 296 params = OSSL_PARAM_merge(params_blt, params_empty); 297 break; 298 case 2: 299 params = OSSL_PARAM_dup(params_blt); 300 break; 301 case 3: 302 p1 = OSSL_PARAM_merge(params_blt, params_empty); 303 params = OSSL_PARAM_dup(p1); 304 break; 305 default: 306 p1 = OSSL_PARAM_dup(params_blt); 307 params = OSSL_PARAM_merge(p1, params_empty); 308 break; 309 } 310 /* Check unsigned int */ 311 if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i")) 312 || !TEST_false(CRYPTO_secure_allocated(p->data)) 313 || !TEST_true(OSSL_PARAM_get_uint(p, &i)) 314 || !TEST_str_eq(p->key, "i") 315 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) 316 || !TEST_size_t_eq(p->data_size, sizeof(int)) 317 || !TEST_uint_eq(i, 6) 318 /* Check unsigned int32 */ 319 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32")) 320 || !TEST_false(CRYPTO_secure_allocated(p->data)) 321 || !TEST_true(OSSL_PARAM_get_uint32(p, &i32)) 322 || !TEST_str_eq(p->key, "i32") 323 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) 324 || !TEST_size_t_eq(p->data_size, sizeof(int32_t)) 325 || !TEST_uint_eq((unsigned int)i32, 1532) 326 /* Check unsigned int64 */ 327 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64")) 328 || !TEST_false(CRYPTO_secure_allocated(p->data)) 329 || !TEST_str_eq(p->key, "i64") 330 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) 331 || !TEST_size_t_eq(p->data_size, sizeof(int64_t)) 332 || !TEST_true(OSSL_PARAM_get_uint64(p, &i64)) 333 || !TEST_ulong_eq((unsigned long)i64, 9999999) 334 /* Check unsigned long int */ 335 || !TEST_ptr(p = OSSL_PARAM_locate(params, "l")) 336 || !TEST_false(CRYPTO_secure_allocated(p->data)) 337 || !TEST_str_eq(p->key, "l") 338 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) 339 || !TEST_size_t_eq(p->data_size, sizeof(unsigned long int)) 340 || !TEST_true(OSSL_PARAM_get_ulong(p, &l)) 341 || !TEST_ulong_eq(l, 42) 342 /* Check size_t */ 343 || !TEST_ptr(p = OSSL_PARAM_locate(params, "st")) 344 || !TEST_false(CRYPTO_secure_allocated(p->data)) 345 || !TEST_str_eq(p->key, "st") 346 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) 347 || !TEST_size_t_eq(p->data_size, sizeof(size_t)) 348 || !TEST_true(OSSL_PARAM_get_size_t(p, &st)) 349 || !TEST_size_t_eq(st, 65537) 350 /* Check octet string */ 351 || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_s")) 352 || !TEST_true(CRYPTO_secure_allocated(p->data)) 353 || !TEST_str_eq(p->key, "oct_s") 354 || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_STRING) 355 || !TEST_mem_eq(p->data, p->data_size, data1, data1_size) 356 /* Check octet pointer */ 357 || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_p")) 358 || !TEST_false(CRYPTO_secure_allocated(p->data)) 359 || !TEST_true(CRYPTO_secure_allocated(*(void **)p->data)) 360 || !TEST_str_eq(p->key, "oct_p") 361 || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_PTR) 362 || !TEST_mem_eq(*(void **)p->data, p->data_size, data2, data2_size) 363 /* Check BN (zero BN becomes unsigned integer) */ 364 || !TEST_ptr(p = OSSL_PARAM_locate(params, "zeronumber")) 365 || !TEST_true(CRYPTO_secure_allocated(p->data)) 366 || !TEST_str_eq(p->key, "zeronumber") 367 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) 368 || !TEST_true(OSSL_PARAM_get_BN(p, &zbn_res)) 369 || !TEST_int_eq(BN_get_flags(pbn, BN_FLG_SECURE), BN_FLG_SECURE) 370 || !TEST_BN_eq(zbn_res, zbn) 371 /* Check BN (positive BN becomes unsigned integer) */ 372 || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber")) 373 || !TEST_true(CRYPTO_secure_allocated(p->data)) 374 || !TEST_str_eq(p->key, "bignumber") 375 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) 376 || !TEST_true(OSSL_PARAM_get_BN(p, &pbn_res)) 377 || !TEST_int_eq(BN_get_flags(pbn, BN_FLG_SECURE), BN_FLG_SECURE) 378 || !TEST_BN_eq(pbn_res, pbn) 379 /* Check BN (negative BN becomes signed integer) */ 380 || !TEST_ptr(p = OSSL_PARAM_locate(params, "negativebignumber")) 381 || !TEST_true(CRYPTO_secure_allocated(p->data)) 382 || !TEST_str_eq(p->key, "negativebignumber") 383 || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER) 384 || !TEST_true(OSSL_PARAM_get_BN(p, &nbn_res)) 385 || !TEST_int_eq(BN_get_flags(nbn, BN_FLG_SECURE), BN_FLG_SECURE) 386 || !TEST_BN_eq(nbn_res, nbn)) 387 goto err; 388 res = 1; 389 err: 390 OSSL_PARAM_free(p1); 391 if (params != params_blt) 392 OSSL_PARAM_free(params); 393 OSSL_PARAM_free(params_blt); 394 OSSL_PARAM_BLD_free(bld); 395 OPENSSL_secure_free(data1); 396 OPENSSL_secure_free(data2); 397 BN_free(zbn); 398 BN_free(zbn_res); 399 BN_free(pbn); 400 BN_free(pbn_res); 401 BN_free(nbn); 402 BN_free(nbn_res); 403 return res; 404 } 405 406 static int builder_limit_test(void) 407 { 408 const int n = 100; 409 char names[100][3]; 410 OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new(); 411 OSSL_PARAM *params = NULL; 412 int i, res = 0; 413 414 if (!TEST_ptr(bld)) 415 goto err; 416 417 for (i = 0; i < n; i++) { 418 names[i][0] = 'A' + (i / 26) - 1; 419 names[i][1] = 'a' + (i % 26) - 1; 420 names[i][2] = '\0'; 421 if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, names[i], 3 * i + 1))) 422 goto err; 423 } 424 if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))) 425 goto err; 426 /* Count the elements in the params array, expecting n */ 427 for (i = 0; params[i].key != NULL; i++) 428 ; 429 if (!TEST_int_eq(i, n)) 430 goto err; 431 432 /* Verify that the build, cleared the builder structure */ 433 OSSL_PARAM_free(params); 434 params = NULL; 435 436 if (!TEST_true(OSSL_PARAM_BLD_push_int(bld, "g", 2)) 437 || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))) 438 goto err; 439 /* Count the elements in the params array, expecting 1 */ 440 for (i = 0; params[i].key != NULL; i++) 441 ; 442 if (!TEST_int_eq(i, 1)) 443 goto err; 444 res = 1; 445 err: 446 OSSL_PARAM_free(params); 447 OSSL_PARAM_BLD_free(bld); 448 return res; 449 } 450 451 static int builder_merge_test(void) 452 { 453 static int data1[] = { 2, 3, 5, 7, 11, 15, 17 }; 454 static unsigned char data2[] = { 2, 4, 6, 8, 10 }; 455 OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new(); 456 OSSL_PARAM_BLD *bld2 = OSSL_PARAM_BLD_new(); 457 OSSL_PARAM *params = NULL, *params_blt = NULL, *params2_blt = NULL, *p; 458 unsigned int i; 459 unsigned long int l; 460 uint32_t i32; 461 uint64_t i64; 462 size_t st; 463 BIGNUM *bn_priv = NULL, *bn_priv_res = NULL; 464 BIGNUM *bn_pub = NULL, *bn_pub_res = NULL; 465 int res = 0; 466 467 if (!TEST_ptr(bld) 468 || !TEST_true(OSSL_PARAM_BLD_push_uint(bld, "i", 6)) 469 || !TEST_true(OSSL_PARAM_BLD_push_ulong(bld, "l", 42)) 470 || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld, "i32", 1532)) 471 || !TEST_true(OSSL_PARAM_BLD_push_uint64(bld, "i64", 9999999)) 472 || !TEST_true(OSSL_PARAM_BLD_push_size_t(bld, "st", 65537)) 473 || !TEST_ptr(bn_priv = BN_secure_new()) 474 || !TEST_true(BN_set_word(bn_priv, 1729)) 475 || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber_priv", bn_priv)) 476 || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld))) 477 goto err; 478 479 if (!TEST_ptr(bld2) 480 || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld2, "oct_s", data1, 481 sizeof(data1))) 482 || !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld2, "oct_p", data2, 483 sizeof(data2))) 484 || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld2, "i32", 99)) 485 || !TEST_ptr(bn_pub = BN_new()) 486 || !TEST_true(BN_set_word(bn_pub, 0x42)) 487 || !TEST_true(OSSL_PARAM_BLD_push_BN(bld2, "bignumber_pub", bn_pub)) 488 || !TEST_ptr(params2_blt = OSSL_PARAM_BLD_to_param(bld2))) 489 goto err; 490 491 if (!TEST_ptr(params = OSSL_PARAM_merge(params_blt, params2_blt))) 492 goto err; 493 494 if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i")) 495 || !TEST_true(OSSL_PARAM_get_uint(p, &i)) 496 || !TEST_str_eq(p->key, "i") 497 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) 498 || !TEST_size_t_eq(p->data_size, sizeof(int)) 499 || !TEST_uint_eq(i, 6) 500 /* Check unsigned int32 */ 501 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32")) 502 || !TEST_true(OSSL_PARAM_get_uint32(p, &i32)) 503 || !TEST_str_eq(p->key, "i32") 504 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) 505 || !TEST_size_t_eq(p->data_size, sizeof(int32_t)) 506 || !TEST_uint_eq((unsigned int)i32, 99) 507 /* Check unsigned int64 */ 508 || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64")) 509 || !TEST_str_eq(p->key, "i64") 510 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) 511 || !TEST_size_t_eq(p->data_size, sizeof(int64_t)) 512 || !TEST_true(OSSL_PARAM_get_uint64(p, &i64)) 513 || !TEST_ulong_eq((unsigned long)i64, 9999999) 514 /* Check unsigned long int */ 515 || !TEST_ptr(p = OSSL_PARAM_locate(params, "l")) 516 || !TEST_str_eq(p->key, "l") 517 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) 518 || !TEST_size_t_eq(p->data_size, sizeof(unsigned long int)) 519 || !TEST_true(OSSL_PARAM_get_ulong(p, &l)) 520 || !TEST_ulong_eq(l, 42) 521 /* Check size_t */ 522 || !TEST_ptr(p = OSSL_PARAM_locate(params, "st")) 523 || !TEST_str_eq(p->key, "st") 524 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) 525 || !TEST_size_t_eq(p->data_size, sizeof(size_t)) 526 || !TEST_true(OSSL_PARAM_get_size_t(p, &st)) 527 || !TEST_size_t_eq(st, 65537) 528 /* Check octet string */ 529 || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_s")) 530 || !TEST_str_eq(p->key, "oct_s") 531 || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_STRING) 532 || !TEST_mem_eq(p->data, p->data_size, data1, sizeof(data1)) 533 /* Check octet pointer */ 534 || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_p")) 535 || !TEST_str_eq(p->key, "oct_p") 536 || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_PTR) 537 || !TEST_mem_eq(*(void **)p->data, p->data_size, data2, sizeof(data2)) 538 /* Check BN */ 539 || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber_pub")) 540 || !TEST_str_eq(p->key, "bignumber_pub") 541 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) 542 || !TEST_true(OSSL_PARAM_get_BN(p, &bn_pub_res)) 543 || !TEST_int_eq(BN_cmp(bn_pub_res, bn_pub), 0) 544 || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber_priv")) 545 || !TEST_str_eq(p->key, "bignumber_priv") 546 || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER) 547 || !TEST_true(OSSL_PARAM_get_BN(p, &bn_priv_res)) 548 || !TEST_int_eq(BN_cmp(bn_priv_res, bn_priv), 0)) 549 goto err; 550 res = 1; 551 err: 552 OSSL_PARAM_free(params); 553 OSSL_PARAM_free(params_blt); 554 OSSL_PARAM_free(params2_blt); 555 OSSL_PARAM_BLD_free(bld); 556 OSSL_PARAM_BLD_free(bld2); 557 BN_free(bn_priv); 558 BN_free(bn_priv_res); 559 BN_free(bn_pub); 560 BN_free(bn_pub_res); 561 return res; 562 } 563 564 int setup_tests(void) 565 { 566 ADD_ALL_TESTS(template_public_single_zero_test, 2); 567 ADD_ALL_TESTS(template_public_test, 5); 568 /* Only run the secure memory testing if we have secure memory available */ 569 if (CRYPTO_secure_malloc_init(1 << 16, 16)) { 570 ADD_TEST(template_private_single_zero_test); 571 ADD_ALL_TESTS(template_private_test, 5); 572 } 573 ADD_TEST(builder_limit_test); 574 ADD_TEST(builder_merge_test); 575 return 1; 576 } 577