2
0

params_api_test.c 26 KB

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