params_api_test.c 20 KB

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