params_api_test.c 28 KB

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