params_api_test.c 23 KB

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