params_api_test.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*
  2. * Copyright 2019-2021 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 "testutil.h"
  12. #include "internal/nelem.h"
  13. #include "internal/endian.h"
  14. #include <openssl/params.h>
  15. #include <openssl/bn.h>
  16. /* The maximum size of the static buffers used to test most things */
  17. #define MAX_LEN 20
  18. static void swap_copy(unsigned char *out, const void *in, size_t len)
  19. {
  20. size_t j;
  21. for (j = 0; j < len; j++)
  22. out[j] = ((unsigned char *)in)[len - j - 1];
  23. }
  24. /*
  25. * A memory copy that converts the native byte ordering either to or from
  26. * little endian format.
  27. *
  28. * On a little endian machine copying either is just a memcpy(3), on a
  29. * big endian machine copying from native to or from little endian involves
  30. * byte reversal.
  31. */
  32. static void le_copy(unsigned char *out, const void *in, size_t len)
  33. {
  34. DECLARE_IS_ENDIAN;
  35. if (IS_LITTLE_ENDIAN)
  36. memcpy(out, in, len);
  37. else
  38. swap_copy(out, in, len);
  39. }
  40. static const struct {
  41. size_t len;
  42. unsigned char value[MAX_LEN];
  43. } raw_values[] = {
  44. { 1, { 0x47 } },
  45. { 1, { 0xd0 } },
  46. { 2, { 0x01, 0xe9 } },
  47. { 2, { 0xff, 0x53 } },
  48. { 3, { 0x16, 0xff, 0x7c } },
  49. { 3, { 0xa8, 0x9c, 0x0e } },
  50. { 4, { 0x38, 0x27, 0xbf, 0x3b } },
  51. { 4, { 0x9f, 0x26, 0x48, 0x22 } },
  52. { 5, { 0x30, 0x65, 0xfa, 0xe4, 0x81 } },
  53. { 5, { 0xd1, 0x76, 0x01, 0x1b, 0xcd } },
  54. { 8, { 0x59, 0xb2, 0x1a, 0xe9, 0x2a, 0xd8, 0x46, 0x40 } },
  55. { 8, { 0xb4, 0xae, 0xbd, 0xb4, 0xdd, 0x04, 0xb1, 0x4c } },
  56. { 16, { 0x61, 0xe8, 0x7e, 0x31, 0xe9, 0x33, 0x83, 0x3d,
  57. 0x87, 0x99, 0xc7, 0xd8, 0x5d, 0xa9, 0x8b, 0x42 } },
  58. { 16, { 0xee, 0x6e, 0x8b, 0xc3, 0xec, 0xcf, 0x37, 0xcc,
  59. 0x89, 0x67, 0xf2, 0x68, 0x33, 0xa0, 0x14, 0xb0 } },
  60. };
  61. static int test_param_type_extra(OSSL_PARAM *param, const unsigned char *cmp,
  62. size_t width)
  63. {
  64. int32_t i32;
  65. int64_t i64;
  66. size_t s, sz;
  67. unsigned char buf[MAX_LEN];
  68. const int bit32 = param->data_size <= sizeof(int32_t);
  69. const int sizet = param->data_size <= sizeof(size_t);
  70. const int signd = param->data_type == OSSL_PARAM_INTEGER;
  71. /*
  72. * Set the unmodified sentinal directly because there is no param array
  73. * for these tests.
  74. */
  75. param->return_size = OSSL_PARAM_UNMODIFIED;
  76. if (signd) {
  77. if ((bit32 && !TEST_true(OSSL_PARAM_get_int32(param, &i32)))
  78. || !TEST_true(OSSL_PARAM_get_int64(param, &i64)))
  79. return 0;
  80. } else {
  81. if ((bit32
  82. && !TEST_true(OSSL_PARAM_get_uint32(param, (uint32_t *)&i32)))
  83. || !TEST_true(OSSL_PARAM_get_uint64(param, (uint64_t *)&i64))
  84. || (sizet && !TEST_true(OSSL_PARAM_get_size_t(param, &s))))
  85. return 0;
  86. }
  87. if (!TEST_false(OSSL_PARAM_modified(param)))
  88. return 0;
  89. /* Check signed types */
  90. if (bit32) {
  91. le_copy(buf, &i32, sizeof(i32));
  92. sz = sizeof(i32) < width ? sizeof(i32) : width;
  93. if (!TEST_mem_eq(buf, sz, cmp, sz))
  94. return 0;
  95. }
  96. le_copy(buf, &i64, sizeof(i64));
  97. sz = sizeof(i64) < width ? sizeof(i64) : width;
  98. if (!TEST_mem_eq(buf, sz, cmp, sz))
  99. return 0;
  100. if (sizet && !signd) {
  101. le_copy(buf, &s, sizeof(s));
  102. sz = sizeof(s) < width ? sizeof(s) : width;
  103. if (!TEST_mem_eq(buf, sz, cmp, sz))
  104. return 0;
  105. }
  106. /* Check a widening write if possible */
  107. if (sizeof(size_t) > width) {
  108. if (signd) {
  109. if (!TEST_true(OSSL_PARAM_set_int32(param, 12345))
  110. || !TEST_true(OSSL_PARAM_get_int64(param, &i64))
  111. || !TEST_size_t_eq((size_t)i64, 12345))
  112. return 0;
  113. } else {
  114. if (!TEST_true(OSSL_PARAM_set_uint32(param, 12345))
  115. || !TEST_true(OSSL_PARAM_get_uint64(param, (uint64_t *)&i64))
  116. || !TEST_size_t_eq((size_t)i64, 12345))
  117. return 0;
  118. }
  119. if (!TEST_true(OSSL_PARAM_modified(param)))
  120. return 0;
  121. }
  122. return 1;
  123. }
  124. /*
  125. * The test cases for each of the bastic integral types are similar.
  126. * For each type, a param of that type is set and an attempt to read it
  127. * get is made. Finally, the above function is called to verify that
  128. * the params can be read as other types.
  129. *
  130. * All the real work is done via byte buffers which are converted to machine
  131. * byte order and to little endian for comparisons. Narrower values are best
  132. * compared using little endian because their values and positions don't
  133. * change.
  134. */
  135. static int test_param_int(int n)
  136. {
  137. int in, out;
  138. unsigned char buf[MAX_LEN], cmp[sizeof(int)];
  139. const size_t len = raw_values[n].len >= sizeof(int) ?
  140. sizeof(int) : raw_values[n].len;
  141. OSSL_PARAM param = OSSL_PARAM_int("a", NULL);
  142. memset(buf, 0, sizeof(buf));
  143. le_copy(buf, raw_values[n].value, sizeof(in));
  144. memcpy(&in, buf, sizeof(in));
  145. param.data = &out;
  146. if (!TEST_true(OSSL_PARAM_set_int(&param, in)))
  147. return 0;
  148. le_copy(cmp, &out, sizeof(out));
  149. if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
  150. return 0;
  151. in = 0;
  152. if (!TEST_true(OSSL_PARAM_get_int(&param, &in)))
  153. return 0;
  154. le_copy(cmp, &in, sizeof(in));
  155. if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
  156. return 0;
  157. param.data = &out;
  158. return test_param_type_extra(&param, raw_values[n].value, sizeof(int));
  159. }
  160. static int test_param_long(int n)
  161. {
  162. long int in, out;
  163. unsigned char buf[MAX_LEN], cmp[sizeof(long int)];
  164. const size_t len = raw_values[n].len >= sizeof(long int)
  165. ? sizeof(long int) : raw_values[n].len;
  166. OSSL_PARAM param = OSSL_PARAM_long("a", NULL);
  167. memset(buf, 0, sizeof(buf));
  168. le_copy(buf, raw_values[n].value, sizeof(in));
  169. memcpy(&in, buf, sizeof(in));
  170. param.data = &out;
  171. if (!TEST_true(OSSL_PARAM_set_long(&param, in)))
  172. return 0;
  173. le_copy(cmp, &out, sizeof(out));
  174. if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
  175. return 0;
  176. in = 0;
  177. if (!TEST_true(OSSL_PARAM_get_long(&param, &in)))
  178. return 0;
  179. le_copy(cmp, &in, sizeof(in));
  180. if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
  181. return 0;
  182. param.data = &out;
  183. return test_param_type_extra(&param, raw_values[n].value, sizeof(long int));
  184. }
  185. static int test_param_uint(int n)
  186. {
  187. unsigned int in, out;
  188. unsigned char buf[MAX_LEN], cmp[sizeof(unsigned int)];
  189. const size_t len = raw_values[n].len >= sizeof(unsigned int) ? sizeof(unsigned int) : raw_values[n].len;
  190. OSSL_PARAM param = OSSL_PARAM_uint("a", NULL);
  191. memset(buf, 0, sizeof(buf));
  192. le_copy(buf, raw_values[n].value, sizeof(in));
  193. memcpy(&in, buf, sizeof(in));
  194. param.data = &out;
  195. if (!TEST_true(OSSL_PARAM_set_uint(&param, in)))
  196. return 0;
  197. le_copy(cmp, &out, sizeof(out));
  198. if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
  199. return 0;
  200. in = 0;
  201. if (!TEST_true(OSSL_PARAM_get_uint(&param, &in)))
  202. return 0;
  203. le_copy(cmp, &in, sizeof(in));
  204. if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
  205. return 0;
  206. param.data = &out;
  207. return test_param_type_extra(&param, raw_values[n].value, sizeof(unsigned int));
  208. }
  209. static int test_param_ulong(int n)
  210. {
  211. unsigned long int in, out;
  212. unsigned char buf[MAX_LEN], cmp[sizeof(unsigned long int)];
  213. const size_t len = raw_values[n].len >= sizeof(unsigned long int)
  214. ? sizeof(unsigned long int) : raw_values[n].len;
  215. OSSL_PARAM param = OSSL_PARAM_ulong("a", NULL);
  216. memset(buf, 0, sizeof(buf));
  217. le_copy(buf, raw_values[n].value, sizeof(in));
  218. memcpy(&in, buf, sizeof(in));
  219. param.data = &out;
  220. if (!TEST_true(OSSL_PARAM_set_ulong(&param, in)))
  221. return 0;
  222. le_copy(cmp, &out, sizeof(out));
  223. if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
  224. return 0;
  225. in = 0;
  226. if (!TEST_true(OSSL_PARAM_get_ulong(&param, &in)))
  227. return 0;
  228. le_copy(cmp, &in, sizeof(in));
  229. if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
  230. return 0;
  231. param.data = &out;
  232. return test_param_type_extra(&param, raw_values[n].value, sizeof(unsigned long int));
  233. }
  234. static int test_param_int32(int n)
  235. {
  236. int32_t in, out;
  237. unsigned char buf[MAX_LEN], cmp[sizeof(int32_t)];
  238. const size_t len = raw_values[n].len >= sizeof(int32_t)
  239. ? sizeof(int32_t) : raw_values[n].len;
  240. OSSL_PARAM param = OSSL_PARAM_int32("a", NULL);
  241. memset(buf, 0, sizeof(buf));
  242. le_copy(buf, raw_values[n].value, sizeof(in));
  243. memcpy(&in, buf, sizeof(in));
  244. param.data = &out;
  245. if (!TEST_true(OSSL_PARAM_set_int32(&param, in)))
  246. return 0;
  247. le_copy(cmp, &out, sizeof(out));
  248. if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
  249. return 0;
  250. in = 0;
  251. if (!TEST_true(OSSL_PARAM_get_int32(&param, &in)))
  252. return 0;
  253. le_copy(cmp, &in, sizeof(in));
  254. if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
  255. return 0;
  256. param.data = &out;
  257. return test_param_type_extra(&param, raw_values[n].value, sizeof(int32_t));
  258. }
  259. static int test_param_uint32(int n)
  260. {
  261. uint32_t in, out;
  262. unsigned char buf[MAX_LEN], cmp[sizeof(uint32_t)];
  263. const size_t len = raw_values[n].len >= sizeof(uint32_t)
  264. ? sizeof(uint32_t) : raw_values[n].len;
  265. OSSL_PARAM param = OSSL_PARAM_uint32("a", NULL);
  266. memset(buf, 0, sizeof(buf));
  267. le_copy(buf, raw_values[n].value, sizeof(in));
  268. memcpy(&in, buf, sizeof(in));
  269. param.data = &out;
  270. if (!TEST_true(OSSL_PARAM_set_uint32(&param, in)))
  271. return 0;
  272. le_copy(cmp, &out, sizeof(out));
  273. if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
  274. return 0;
  275. in = 0;
  276. if (!TEST_true(OSSL_PARAM_get_uint32(&param, &in)))
  277. return 0;
  278. le_copy(cmp, &in, sizeof(in));
  279. if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
  280. return 0;
  281. param.data = &out;
  282. return test_param_type_extra(&param, raw_values[n].value, sizeof(uint32_t));
  283. }
  284. static int test_param_int64(int n)
  285. {
  286. int64_t in, out;
  287. unsigned char buf[MAX_LEN], cmp[sizeof(int64_t)];
  288. const size_t len = raw_values[n].len >= sizeof(int64_t)
  289. ? sizeof(int64_t) : raw_values[n].len;
  290. OSSL_PARAM param = OSSL_PARAM_int64("a", NULL);
  291. memset(buf, 0, sizeof(buf));
  292. le_copy(buf, raw_values[n].value, sizeof(in));
  293. memcpy(&in, buf, sizeof(in));
  294. param.data = &out;
  295. if (!TEST_true(OSSL_PARAM_set_int64(&param, in)))
  296. return 0;
  297. le_copy(cmp, &out, sizeof(out));
  298. if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
  299. return 0;
  300. in = 0;
  301. if (!TEST_true(OSSL_PARAM_get_int64(&param, &in)))
  302. return 0;
  303. le_copy(cmp, &in, sizeof(in));
  304. if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
  305. return 0;
  306. param.data = &out;
  307. return test_param_type_extra(&param, raw_values[n].value, sizeof(int64_t));
  308. }
  309. static int test_param_uint64(int n)
  310. {
  311. uint64_t in, out;
  312. unsigned char buf[MAX_LEN], cmp[sizeof(uint64_t)];
  313. const size_t len = raw_values[n].len >= sizeof(uint64_t)
  314. ? sizeof(uint64_t) : raw_values[n].len;
  315. OSSL_PARAM param = OSSL_PARAM_uint64("a", NULL);
  316. memset(buf, 0, sizeof(buf));
  317. le_copy(buf, raw_values[n].value, sizeof(in));
  318. memcpy(&in, buf, sizeof(in));
  319. param.data = &out;
  320. if (!TEST_true(OSSL_PARAM_set_uint64(&param, in)))
  321. return 0;
  322. le_copy(cmp, &out, sizeof(out));
  323. if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
  324. return 0;
  325. in = 0;
  326. if (!TEST_true(OSSL_PARAM_get_uint64(&param, &in)))
  327. return 0;
  328. le_copy(cmp, &in, sizeof(in));
  329. if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
  330. return 0;
  331. param.data = &out;
  332. return test_param_type_extra(&param, raw_values[n].value, sizeof(uint64_t));
  333. }
  334. static int test_param_size_t(int n)
  335. {
  336. size_t in, out;
  337. unsigned char buf[MAX_LEN], cmp[sizeof(size_t)];
  338. const size_t len = raw_values[n].len >= sizeof(size_t)
  339. ? sizeof(size_t) : raw_values[n].len;
  340. OSSL_PARAM param = OSSL_PARAM_size_t("a", NULL);
  341. memset(buf, 0, sizeof(buf));
  342. le_copy(buf, raw_values[n].value, sizeof(in));
  343. memcpy(&in, buf, sizeof(in));
  344. param.data = &out;
  345. if (!TEST_true(OSSL_PARAM_set_size_t(&param, in)))
  346. return 0;
  347. le_copy(cmp, &out, sizeof(out));
  348. if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
  349. return 0;
  350. in = 0;
  351. if (!TEST_true(OSSL_PARAM_get_size_t(&param, &in)))
  352. return 0;
  353. le_copy(cmp, &in, sizeof(in));
  354. if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
  355. return 0;
  356. param.data = &out;
  357. return test_param_type_extra(&param, raw_values[n].value, sizeof(size_t));
  358. }
  359. static int test_param_time_t(int n)
  360. {
  361. time_t in, out;
  362. unsigned char buf[MAX_LEN], cmp[sizeof(time_t)];
  363. const size_t len = raw_values[n].len >= sizeof(time_t)
  364. ? sizeof(time_t) : raw_values[n].len;
  365. OSSL_PARAM param = OSSL_PARAM_time_t("a", NULL);
  366. memset(buf, 0, sizeof(buf));
  367. le_copy(buf, raw_values[n].value, sizeof(in));
  368. memcpy(&in, buf, sizeof(in));
  369. param.data = &out;
  370. if (!TEST_true(OSSL_PARAM_set_time_t(&param, in)))
  371. return 0;
  372. le_copy(cmp, &out, sizeof(out));
  373. if (!TEST_mem_eq(cmp, len, raw_values[n].value, len))
  374. return 0;
  375. in = 0;
  376. if (!TEST_true(OSSL_PARAM_get_time_t(&param, &in)))
  377. return 0;
  378. le_copy(cmp, &in, sizeof(in));
  379. if (!TEST_mem_eq(cmp, sizeof(in), raw_values[n].value, sizeof(in)))
  380. return 0;
  381. param.data = &out;
  382. return test_param_type_extra(&param, raw_values[n].value, sizeof(size_t));
  383. }
  384. static int test_param_bignum(int n)
  385. {
  386. unsigned char buf[MAX_LEN], bnbuf[MAX_LEN];
  387. const size_t len = raw_values[n].len;
  388. BIGNUM *b = NULL, *c = NULL;
  389. OSSL_PARAM param = OSSL_PARAM_DEFN("bn", OSSL_PARAM_UNSIGNED_INTEGER,
  390. NULL, 0);
  391. int ret = 0;
  392. param.data = bnbuf;
  393. param.data_size = len;
  394. le_copy(buf, raw_values[n].value, len);
  395. if (!TEST_ptr(b = BN_lebin2bn(raw_values[n].value, (int)len, NULL)))
  396. goto err;
  397. if (!TEST_true(OSSL_PARAM_set_BN(&param, b))
  398. || !TEST_mem_eq(bnbuf, param.return_size, buf, param.return_size))
  399. goto err;
  400. param.data_size = param.return_size;
  401. if (!TEST_true(OSSL_PARAM_get_BN(&param, &c))
  402. || !TEST_BN_eq(b, c))
  403. goto err;
  404. ret = 1;
  405. err:
  406. BN_free(b);
  407. BN_free(c);
  408. return ret;
  409. }
  410. static int test_param_real(void)
  411. {
  412. double p;
  413. OSSL_PARAM param = OSSL_PARAM_double("r", NULL);
  414. param.data = &p;
  415. return TEST_true(OSSL_PARAM_set_double(&param, 3.14159))
  416. && TEST_double_eq(p, 3.14159);
  417. }
  418. static int test_param_construct(int tstid)
  419. {
  420. static const char *int_names[] = {
  421. "int", "long", "int32", "int64"
  422. };
  423. static const char *uint_names[] = {
  424. "uint", "ulong", "uint32", "uint64", "size_t"
  425. };
  426. static const unsigned char bn_val[16] = {
  427. 0xac, 0x75, 0x22, 0x7d, 0x81, 0x06, 0x7a, 0x23,
  428. 0xa6, 0xed, 0x87, 0xc7, 0xab, 0xf4, 0x73, 0x22
  429. };
  430. OSSL_PARAM *p = NULL, *p1 = NULL;
  431. static const OSSL_PARAM params_empty[] = {
  432. OSSL_PARAM_END
  433. };
  434. OSSL_PARAM params[20];
  435. char buf[100], buf2[100], *bufp, *bufp2;
  436. unsigned char ubuf[100];
  437. void *vp, *vpn = NULL, *vp2;
  438. OSSL_PARAM *cp;
  439. int i, n = 0, ret = 0;
  440. unsigned int u;
  441. long int l;
  442. unsigned long int ul;
  443. int32_t i32;
  444. uint32_t u32;
  445. int64_t i64;
  446. uint64_t u64;
  447. size_t j, k, s;
  448. double d, d2;
  449. BIGNUM *bn = NULL, *bn2 = NULL;
  450. params[n++] = OSSL_PARAM_construct_int("int", &i);
  451. params[n++] = OSSL_PARAM_construct_uint("uint", &u);
  452. params[n++] = OSSL_PARAM_construct_long("long", &l);
  453. params[n++] = OSSL_PARAM_construct_ulong("ulong", &ul);
  454. params[n++] = OSSL_PARAM_construct_int32("int32", &i32);
  455. params[n++] = OSSL_PARAM_construct_int64("int64", &i64);
  456. params[n++] = OSSL_PARAM_construct_uint32("uint32", &u32);
  457. params[n++] = OSSL_PARAM_construct_uint64("uint64", &u64);
  458. params[n++] = OSSL_PARAM_construct_size_t("size_t", &s);
  459. params[n++] = OSSL_PARAM_construct_double("double", &d);
  460. params[n++] = OSSL_PARAM_construct_BN("bignum", ubuf, sizeof(ubuf));
  461. params[n++] = OSSL_PARAM_construct_utf8_string("utf8str", buf, sizeof(buf));
  462. params[n++] = OSSL_PARAM_construct_octet_string("octstr", buf, sizeof(buf));
  463. params[n++] = OSSL_PARAM_construct_utf8_ptr("utf8ptr", &bufp, 0);
  464. params[n++] = OSSL_PARAM_construct_octet_ptr("octptr", &vp, 0);
  465. params[n] = OSSL_PARAM_construct_end();
  466. switch(tstid) {
  467. case 0:
  468. p = params;
  469. break;
  470. case 1:
  471. p = OSSL_PARAM_merge(params, params_empty);
  472. break;
  473. case 2:
  474. p = OSSL_PARAM_dup(params);
  475. break;
  476. default:
  477. p1 = OSSL_PARAM_dup(params);
  478. p = OSSL_PARAM_merge(p1, params_empty);
  479. break;
  480. }
  481. /* Search failure */
  482. if (!TEST_ptr_null(OSSL_PARAM_locate(p, "fnord")))
  483. goto err;
  484. /* All signed integral types */
  485. for (j = 0; j < OSSL_NELEM(int_names); j++) {
  486. if (!TEST_ptr(cp = OSSL_PARAM_locate(p, int_names[j]))
  487. || !TEST_true(OSSL_PARAM_set_int32(cp, (int32_t)(3 + j)))
  488. || !TEST_true(OSSL_PARAM_get_int64(cp, &i64))
  489. || !TEST_size_t_eq(cp->data_size, cp->return_size)
  490. || !TEST_size_t_eq((size_t)i64, 3 + j)) {
  491. TEST_note("iteration %zu var %s", j + 1, int_names[j]);
  492. goto err;
  493. }
  494. }
  495. /* All unsigned integral types */
  496. for (j = 0; j < OSSL_NELEM(uint_names); j++) {
  497. if (!TEST_ptr(cp = OSSL_PARAM_locate(p, uint_names[j]))
  498. || !TEST_true(OSSL_PARAM_set_uint32(cp, (uint32_t)(3 + j)))
  499. || !TEST_true(OSSL_PARAM_get_uint64(cp, &u64))
  500. || !TEST_size_t_eq(cp->data_size, cp->return_size)
  501. || !TEST_size_t_eq((size_t)u64, 3 + j)) {
  502. TEST_note("iteration %zu var %s", j + 1, uint_names[j]);
  503. goto err;
  504. }
  505. }
  506. /* Real */
  507. if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "double"))
  508. || !TEST_true(OSSL_PARAM_set_double(cp, 3.14))
  509. || !TEST_true(OSSL_PARAM_get_double(cp, &d2))
  510. || !TEST_size_t_eq(cp->return_size, sizeof(double))
  511. || !TEST_double_eq(d2, 3.14)
  512. || (tstid <= 1 && !TEST_double_eq(d, d2)))
  513. goto err;
  514. /* UTF8 string */
  515. bufp = NULL;
  516. if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "utf8str"))
  517. || !TEST_true(OSSL_PARAM_set_utf8_string(cp, "abcdef"))
  518. || !TEST_size_t_eq(cp->return_size, sizeof("abcdef") - 1)
  519. || !TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, 0))
  520. || !TEST_str_eq(bufp, "abcdef")) {
  521. OPENSSL_free(bufp);
  522. goto err;
  523. }
  524. OPENSSL_free(bufp);
  525. bufp = buf2;
  526. if (!TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, sizeof(buf2)))
  527. || !TEST_str_eq(buf2, "abcdef"))
  528. goto err;
  529. /* UTF8 pointer */
  530. /* Note that the size of a UTF8 string does *NOT* include the NUL byte */
  531. bufp = buf;
  532. if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "utf8ptr"))
  533. || !TEST_true(OSSL_PARAM_set_utf8_ptr(cp, "tuvwxyz"))
  534. || !TEST_size_t_eq(cp->return_size, sizeof("tuvwxyz") - 1)
  535. || !TEST_true(OSSL_PARAM_get_utf8_ptr(cp, (const char **)&bufp2))
  536. || !TEST_str_eq(bufp2, "tuvwxyz")
  537. || (tstid <= 1 && !TEST_ptr_eq(bufp2, bufp)))
  538. goto err;
  539. /* OCTET string */
  540. if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "octstr"))
  541. || !TEST_true(OSSL_PARAM_set_octet_string(cp, "abcdefghi",
  542. sizeof("abcdefghi")))
  543. || !TEST_size_t_eq(cp->return_size, sizeof("abcdefghi")))
  544. goto err;
  545. /* Match the return size to avoid trailing garbage bytes */
  546. cp->data_size = cp->return_size;
  547. if (!TEST_true(OSSL_PARAM_get_octet_string(cp, &vpn, 0, &s))
  548. || !TEST_size_t_eq(s, sizeof("abcdefghi"))
  549. || !TEST_mem_eq(vpn, sizeof("abcdefghi"),
  550. "abcdefghi", sizeof("abcdefghi")))
  551. goto err;
  552. vp = buf2;
  553. if (!TEST_true(OSSL_PARAM_get_octet_string(cp, &vp, sizeof(buf2), &s))
  554. || !TEST_size_t_eq(s, sizeof("abcdefghi"))
  555. || !TEST_mem_eq(vp, sizeof("abcdefghi"),
  556. "abcdefghi", sizeof("abcdefghi")))
  557. goto err;
  558. /* OCTET pointer */
  559. vp = &l;
  560. if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "octptr"))
  561. || !TEST_true(OSSL_PARAM_set_octet_ptr(cp, &ul, sizeof(ul)))
  562. || !TEST_size_t_eq(cp->return_size, sizeof(ul))
  563. || (tstid <= 1 && !TEST_ptr_eq(vp, &ul)))
  564. goto err;
  565. /* Match the return size to avoid trailing garbage bytes */
  566. cp->data_size = cp->return_size;
  567. if (!TEST_true(OSSL_PARAM_get_octet_ptr(cp, (const void **)&vp2, &k))
  568. || !TEST_size_t_eq(k, sizeof(ul))
  569. || (tstid <= 1 && !TEST_ptr_eq(vp2, vp)))
  570. goto err;
  571. /* BIGNUM */
  572. if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "bignum"))
  573. || !TEST_ptr(bn = BN_lebin2bn(bn_val, (int)sizeof(bn_val), NULL))
  574. || !TEST_true(OSSL_PARAM_set_BN(cp, bn))
  575. || !TEST_size_t_eq(cp->data_size, cp->return_size))
  576. goto err;
  577. /* Match the return size to avoid trailing garbage bytes */
  578. cp->data_size = cp->return_size;
  579. if(!TEST_true(OSSL_PARAM_get_BN(cp, &bn2))
  580. || !TEST_BN_eq(bn, bn2))
  581. goto err;
  582. ret = 1;
  583. err:
  584. if (p != params)
  585. OPENSSL_free(p);
  586. OPENSSL_free(p1);
  587. OPENSSL_free(vpn);
  588. BN_free(bn);
  589. BN_free(bn2);
  590. return ret;
  591. }
  592. static int test_param_modified(void)
  593. {
  594. OSSL_PARAM param[3] = { OSSL_PARAM_int("a", NULL),
  595. OSSL_PARAM_int("b", NULL),
  596. OSSL_PARAM_END };
  597. int a, b;
  598. param->data = &a;
  599. param[1].data = &b;
  600. if (!TEST_false(OSSL_PARAM_modified(param))
  601. && !TEST_true(OSSL_PARAM_set_int32(param, 1234))
  602. && !TEST_true(OSSL_PARAM_modified(param))
  603. && !TEST_false(OSSL_PARAM_modified(param + 1))
  604. && !TEST_true(OSSL_PARAM_set_int32(param + 1, 1))
  605. && !TEST_true(OSSL_PARAM_modified(param + 1)))
  606. return 0;
  607. OSSL_PARAM_set_all_unmodified(param);
  608. if (!TEST_false(OSSL_PARAM_modified(param))
  609. && !TEST_true(OSSL_PARAM_set_int32(param, 4321))
  610. && !TEST_true(OSSL_PARAM_modified(param))
  611. && !TEST_false(OSSL_PARAM_modified(param + 1))
  612. && !TEST_true(OSSL_PARAM_set_int32(param + 1, 2))
  613. && !TEST_true(OSSL_PARAM_modified(param + 1)))
  614. return 0;
  615. return 1;
  616. }
  617. static int test_param_copy_null(void)
  618. {
  619. int ret, val;
  620. int a = 1, b = 2, i = 0;
  621. OSSL_PARAM *cp1 = NULL, *cp2 = NULL, *p;
  622. OSSL_PARAM param[3];
  623. param[i++] = OSSL_PARAM_construct_int("a", &a);
  624. param[i++] = OSSL_PARAM_construct_int("b", &b);
  625. param[i] = OSSL_PARAM_construct_end();
  626. ret = TEST_ptr_null(OSSL_PARAM_dup(NULL))
  627. && TEST_ptr(cp1 = OSSL_PARAM_merge(NULL, param))
  628. && TEST_ptr(p = OSSL_PARAM_locate(cp1, "a"))
  629. && TEST_true(OSSL_PARAM_get_int(p, &val))
  630. && TEST_int_eq(val, 1)
  631. && TEST_ptr(p = OSSL_PARAM_locate(cp1, "b"))
  632. && TEST_true(OSSL_PARAM_get_int(p, &val))
  633. && TEST_int_eq(val, 2)
  634. && TEST_ptr(cp2 = OSSL_PARAM_merge(param, NULL))
  635. && TEST_ptr(p = OSSL_PARAM_locate(cp2, "a"))
  636. && TEST_true(OSSL_PARAM_get_int(p, &val))
  637. && TEST_int_eq(val, 1)
  638. && TEST_ptr(p = OSSL_PARAM_locate(cp2, "b"))
  639. && TEST_true(OSSL_PARAM_get_int(p, &val))
  640. && TEST_int_eq(val, 2)
  641. && TEST_ptr_null(OSSL_PARAM_merge(NULL, NULL));
  642. OSSL_PARAM_free(cp2);
  643. OSSL_PARAM_free(cp1);
  644. return ret;
  645. }
  646. int setup_tests(void)
  647. {
  648. ADD_ALL_TESTS(test_param_int, OSSL_NELEM(raw_values));
  649. ADD_ALL_TESTS(test_param_long, OSSL_NELEM(raw_values));
  650. ADD_ALL_TESTS(test_param_uint, OSSL_NELEM(raw_values));
  651. ADD_ALL_TESTS(test_param_ulong, OSSL_NELEM(raw_values));
  652. ADD_ALL_TESTS(test_param_int32, OSSL_NELEM(raw_values));
  653. ADD_ALL_TESTS(test_param_uint32, OSSL_NELEM(raw_values));
  654. ADD_ALL_TESTS(test_param_size_t, OSSL_NELEM(raw_values));
  655. ADD_ALL_TESTS(test_param_time_t, OSSL_NELEM(raw_values));
  656. ADD_ALL_TESTS(test_param_int64, OSSL_NELEM(raw_values));
  657. ADD_ALL_TESTS(test_param_uint64, OSSL_NELEM(raw_values));
  658. ADD_ALL_TESTS(test_param_bignum, OSSL_NELEM(raw_values));
  659. ADD_TEST(test_param_real);
  660. ADD_ALL_TESTS(test_param_construct, 4);
  661. ADD_TEST(test_param_modified);
  662. ADD_TEST(test_param_copy_null);
  663. return 1;
  664. }