2
0

param_build_test.c 22 KB

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