params_test.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * https://www.openssl.org/source/license.html
  8. * or in the file LICENSE in the source distribution.
  9. */
  10. /*
  11. * This program tests the use of OSSL_PARAM, currently in raw form.
  12. */
  13. #include <string.h>
  14. #include <openssl/bn.h>
  15. #include <openssl/core.h>
  16. #include <openssl/err.h>
  17. #include <openssl/params.h>
  18. #include "internal/numbers.h"
  19. #include "internal/nelem.h"
  20. #include "testutil.h"
  21. /*-
  22. * PROVIDER SECTION
  23. * ================
  24. *
  25. * Even though it's not necessarily ONLY providers doing this part,
  26. * they are naturally going to be the most common users of
  27. * set_params and get_params functions.
  28. */
  29. /*
  30. * In real use cases, setters and getters would take an object with
  31. * which the parameters are associated. This structure is a cheap
  32. * simulation.
  33. */
  34. struct object_st {
  35. /*
  36. * Documented as a native integer, of the size given by sizeof(int).
  37. * Assumed data type OSSL_PARAM_INTEGER
  38. */
  39. int p1;
  40. /*
  41. * Documented as a native double, of the size given by sizeof(double).
  42. * Assumed data type OSSL_PARAM_REAL
  43. */
  44. double p2;
  45. /*
  46. * Documented as an arbitrarily large unsigned integer.
  47. * The data size must be large enough to accommodate.
  48. * Assumed data type OSSL_PARAM_UNSIGNED_INTEGER
  49. */
  50. BIGNUM *p3;
  51. /*
  52. * Documented as a C string.
  53. * The data size must be large enough to accommodate.
  54. * Assumed data type OSSL_PARAM_UTF8_STRING
  55. */
  56. char *p4;
  57. size_t p4_l;
  58. /*
  59. * Documented as a C string.
  60. * Assumed data type OSSL_PARAM_UTF8_STRING
  61. */
  62. char p5[256];
  63. size_t p5_l;
  64. /*
  65. * Documented as a pointer to a constant C string.
  66. * Assumed data type OSSL_PARAM_UTF8_PTR
  67. */
  68. const char *p6;
  69. size_t p6_l;
  70. };
  71. #define p1_init 42 /* The ultimate answer */
  72. #define p2_init 6.283 /* Magic number */
  73. /* Stolen from evp_data, BLAKE2s256 test */
  74. #define p3_init \
  75. "4142434445464748494a4b4c4d4e4f50" \
  76. "5152535455565758595a616263646566" \
  77. "6768696a6b6c6d6e6f70717273747576" \
  78. "7778797a30313233343536373839"
  79. #define p4_init "BLAKE2s256" /* Random string */
  80. #define p5_init "Hellow World" /* Random string */
  81. #define p6_init OPENSSL_FULL_VERSION_STR /* Static string */
  82. static void cleanup_object(void *vobj)
  83. {
  84. struct object_st *obj = vobj;
  85. BN_free(obj->p3);
  86. obj->p3 = NULL;
  87. OPENSSL_free(obj->p4);
  88. obj->p4 = NULL;
  89. OPENSSL_free(obj);
  90. }
  91. static void *init_object(void)
  92. {
  93. struct object_st *obj;
  94. if (!TEST_ptr(obj = OPENSSL_zalloc(sizeof(*obj))))
  95. return NULL;
  96. obj->p1 = p1_init;
  97. obj->p2 = p2_init;
  98. if (!TEST_true(BN_hex2bn(&obj->p3, p3_init)))
  99. goto fail;
  100. if (!TEST_ptr(obj->p4 = OPENSSL_strdup(p4_init)))
  101. goto fail;
  102. strcpy(obj->p5, p5_init);
  103. obj->p6 = p6_init;
  104. return obj;
  105. fail:
  106. cleanup_object(obj);
  107. obj = NULL;
  108. return NULL;
  109. }
  110. /*
  111. * RAW provider, which handles the parameters in a very raw manner,
  112. * with no fancy API and very minimal checking. The application that
  113. * calls these to set or request parameters MUST get its OSSL_PARAM
  114. * array right.
  115. */
  116. static int raw_set_params(void *vobj, const OSSL_PARAM *params)
  117. {
  118. struct object_st *obj = vobj;
  119. for (; params->key != NULL; params++)
  120. if (strcmp(params->key, "p1") == 0) {
  121. obj->p1 = *(int *)params->data;
  122. } else if (strcmp(params->key, "p2") == 0) {
  123. obj->p2 = *(double *)params->data;
  124. } else if (strcmp(params->key, "p3") == 0) {
  125. BN_free(obj->p3);
  126. if (!TEST_ptr(obj->p3 = BN_native2bn(params->data,
  127. params->data_size, NULL)))
  128. return 0;
  129. } else if (strcmp(params->key, "p4") == 0) {
  130. OPENSSL_free(obj->p4);
  131. if (!TEST_ptr(obj->p4 = OPENSSL_strndup(params->data,
  132. params->data_size)))
  133. return 0;
  134. obj->p4_l = strlen(obj->p4);
  135. } else if (strcmp(params->key, "p5") == 0) {
  136. /*
  137. * Protect obj->p5 against too much data. This should not
  138. * happen, we don't use that long strings.
  139. */
  140. size_t data_length =
  141. OPENSSL_strnlen(params->data, params->data_size);
  142. if (!TEST_size_t_lt(data_length, sizeof(obj->p5)))
  143. return 0;
  144. strncpy(obj->p5, params->data, data_length);
  145. obj->p5[data_length] = '\0';
  146. obj->p5_l = strlen(obj->p5);
  147. } else if (strcmp(params->key, "p6") == 0) {
  148. obj->p6 = *(const char **)params->data;
  149. obj->p6_l = params->data_size;
  150. }
  151. return 1;
  152. }
  153. static int raw_get_params(void *vobj, OSSL_PARAM *params)
  154. {
  155. struct object_st *obj = vobj;
  156. for (; params->key != NULL; params++)
  157. if (strcmp(params->key, "p1") == 0) {
  158. params->return_size = sizeof(obj->p1);
  159. *(int *)params->data = obj->p1;
  160. } else if (strcmp(params->key, "p2") == 0) {
  161. params->return_size = sizeof(obj->p2);
  162. *(double *)params->data = obj->p2;
  163. } else if (strcmp(params->key, "p3") == 0) {
  164. params->return_size = BN_num_bytes(obj->p3);
  165. if (!TEST_size_t_ge(params->data_size, params->return_size))
  166. return 0;
  167. BN_bn2nativepad(obj->p3, params->data, params->return_size);
  168. } else if (strcmp(params->key, "p4") == 0) {
  169. params->return_size = strlen(obj->p4);
  170. if (!TEST_size_t_gt(params->data_size, params->return_size))
  171. return 0;
  172. strcpy(params->data, obj->p4);
  173. } else if (strcmp(params->key, "p5") == 0) {
  174. params->return_size = strlen(obj->p5);
  175. if (!TEST_size_t_gt(params->data_size, params->return_size))
  176. return 0;
  177. strcpy(params->data, obj->p5);
  178. } else if (strcmp(params->key, "p6") == 0) {
  179. params->return_size = strlen(obj->p6);
  180. *(const char **)params->data = obj->p6;
  181. }
  182. return 1;
  183. }
  184. /*
  185. * API provider, which handles the parameters using the API from params.h
  186. */
  187. static int api_set_params(void *vobj, const OSSL_PARAM *params)
  188. {
  189. struct object_st *obj = vobj;
  190. const OSSL_PARAM *p = NULL;
  191. if ((p = OSSL_PARAM_locate_const(params, "p1")) != NULL
  192. && !TEST_true(OSSL_PARAM_get_int(p, &obj->p1)))
  193. return 0;
  194. if ((p = OSSL_PARAM_locate_const(params, "p2")) != NULL
  195. && !TEST_true(OSSL_PARAM_get_double(p, &obj->p2)))
  196. return 0;
  197. if ((p = OSSL_PARAM_locate_const(params, "p3")) != NULL
  198. && !TEST_true(OSSL_PARAM_get_BN(p, &obj->p3)))
  199. return 0;
  200. if ((p = OSSL_PARAM_locate_const(params, "p4")) != NULL) {
  201. OPENSSL_free(obj->p4);
  202. obj->p4 = NULL;
  203. /* If the value pointer is NULL, we get it automatically allocated */
  204. if (!TEST_true(OSSL_PARAM_get_utf8_string(p, &obj->p4, 0)))
  205. return 0;
  206. }
  207. if ((p = OSSL_PARAM_locate_const(params, "p5")) != NULL) {
  208. char *p5_ptr = obj->p5;
  209. if (!TEST_true(OSSL_PARAM_get_utf8_string(p, &p5_ptr, sizeof(obj->p5))))
  210. return 0;
  211. obj->p5_l = strlen(obj->p5);
  212. }
  213. if ((p = OSSL_PARAM_locate_const(params, "p6")) != NULL) {
  214. if (!TEST_true(OSSL_PARAM_get_utf8_ptr(p, &obj->p6)))
  215. return 0;
  216. obj->p6_l = strlen(obj->p6);
  217. }
  218. return 1;
  219. }
  220. static int api_get_params(void *vobj, OSSL_PARAM *params)
  221. {
  222. struct object_st *obj = vobj;
  223. OSSL_PARAM *p = NULL;
  224. if ((p = OSSL_PARAM_locate(params, "p1")) != NULL
  225. && !TEST_true(OSSL_PARAM_set_int(p, obj->p1)))
  226. return 0;
  227. if ((p = OSSL_PARAM_locate(params, "p2")) != NULL
  228. && !TEST_true(OSSL_PARAM_set_double(p, obj->p2)))
  229. return 0;
  230. if ((p = OSSL_PARAM_locate(params, "p3")) != NULL
  231. && !TEST_true(OSSL_PARAM_set_BN(p, obj->p3)))
  232. return 0;
  233. if ((p = OSSL_PARAM_locate(params, "p4")) != NULL
  234. && !TEST_true(OSSL_PARAM_set_utf8_string(p, obj->p4)))
  235. return 0;
  236. if ((p = OSSL_PARAM_locate(params, "p5")) != NULL
  237. && !TEST_true(OSSL_PARAM_set_utf8_string(p, obj->p5)))
  238. return 0;
  239. if ((p = OSSL_PARAM_locate(params, "p6")) != NULL
  240. && !TEST_true(OSSL_PARAM_set_utf8_ptr(p, obj->p6)))
  241. return 0;
  242. return 1;
  243. }
  244. /*
  245. * This structure only simulates a provider dispatch, the real deal is
  246. * a bit more code that's not necessary in these tests.
  247. */
  248. struct provider_dispatch_st {
  249. int (*set_params)(void *obj, const OSSL_PARAM *params);
  250. int (*get_params)(void *obj, OSSL_PARAM *params);
  251. };
  252. /* "raw" provider */
  253. static const struct provider_dispatch_st provider_raw = {
  254. raw_set_params, raw_get_params
  255. };
  256. /* "api" provider */
  257. static const struct provider_dispatch_st provider_api = {
  258. api_set_params, api_get_params
  259. };
  260. /*-
  261. * APPLICATION SECTION
  262. * ===================
  263. */
  264. /* In all our tests, these are variables that get manipulated as parameters
  265. *
  266. * These arrays consistently do nothing with the "p2" parameter, and
  267. * always include a "foo" parameter. This is to check that the
  268. * set_params and get_params calls ignore the lack of parameters that
  269. * the application isn't interested in, as well as ignore parameters
  270. * they don't understand (the application may have one big bag of
  271. * parameters).
  272. */
  273. static int app_p1; /* "p1" */
  274. static double app_p2; /* "p2" is ignored */
  275. static BIGNUM *app_p3 = NULL; /* "p3" */
  276. static unsigned char bignumbin[4096]; /* "p3" */
  277. static char app_p4[256]; /* "p4" */
  278. static char app_p5[256]; /* "p5" */
  279. static const char *app_p6 = NULL; /* "p6" */
  280. static unsigned char foo[1]; /* "foo" */
  281. #define app_p1_init 17 /* A random number */
  282. #define app_p2_init 47.11 /* Another random number */
  283. #define app_p3_init "deadbeef" /* Classic */
  284. #define app_p4_init "Hello"
  285. #define app_p5_init "World"
  286. #define app_p6_init "Cookie"
  287. #define app_foo_init 'z'
  288. static int cleanup_app_variables(void)
  289. {
  290. BN_free(app_p3);
  291. app_p3 = NULL;
  292. return 1;
  293. }
  294. static int init_app_variables(void)
  295. {
  296. int l = 0;
  297. cleanup_app_variables();
  298. app_p1 = app_p1_init;
  299. app_p2 = app_p2_init;
  300. if (!BN_hex2bn(&app_p3, app_p3_init)
  301. || (l = BN_bn2nativepad(app_p3, bignumbin, sizeof(bignumbin))) < 0)
  302. return 0;
  303. strcpy(app_p4, app_p4_init);
  304. strcpy(app_p5, app_p5_init);
  305. app_p6 = app_p6_init;
  306. foo[0] = app_foo_init;
  307. return 1;
  308. }
  309. /*
  310. * Here, we define test OSSL_PARAM arrays
  311. */
  312. /* An array of OSSL_PARAM, specific in the most raw manner possible */
  313. static OSSL_PARAM static_raw_params[] = {
  314. { "p1", OSSL_PARAM_INTEGER, &app_p1, sizeof(app_p1), 0 },
  315. { "p3", OSSL_PARAM_UNSIGNED_INTEGER, &bignumbin, sizeof(bignumbin), 0 },
  316. { "p4", OSSL_PARAM_UTF8_STRING, &app_p4, sizeof(app_p4), 0 },
  317. { "p5", OSSL_PARAM_UTF8_STRING, &app_p5, sizeof(app_p5), 0 },
  318. /* sizeof(app_p6_init) - 1, because we know that's what we're using */
  319. { "p6", OSSL_PARAM_UTF8_PTR, &app_p6, sizeof(app_p6_init) - 1, 0 },
  320. { "foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo), 0 },
  321. { NULL, 0, NULL, 0, 0 }
  322. };
  323. /* The same array of OSSL_PARAM, specified with the macros from params.h */
  324. static OSSL_PARAM static_api_params[] = {
  325. OSSL_PARAM_int("p1", &app_p1),
  326. OSSL_PARAM_BN("p3", &bignumbin, sizeof(bignumbin)),
  327. OSSL_PARAM_DEFN("p4", OSSL_PARAM_UTF8_STRING, &app_p4, sizeof(app_p4)),
  328. OSSL_PARAM_DEFN("p5", OSSL_PARAM_UTF8_STRING, &app_p5, sizeof(app_p5)),
  329. /* sizeof(app_p6_init), because we know that's what we're using */
  330. OSSL_PARAM_DEFN("p6", OSSL_PARAM_UTF8_PTR, &app_p6,
  331. sizeof(app_p6_init) - 1),
  332. OSSL_PARAM_DEFN("foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo)),
  333. OSSL_PARAM_END
  334. };
  335. /*
  336. * The same array again, but constructed at run-time
  337. * This exercises the OSSL_PARAM constructor functions
  338. */
  339. static OSSL_PARAM *construct_api_params(void)
  340. {
  341. size_t n = 0;
  342. static OSSL_PARAM params[10];
  343. params[n++] = OSSL_PARAM_construct_int("p1", &app_p1);
  344. params[n++] = OSSL_PARAM_construct_BN("p3", bignumbin, sizeof(bignumbin));
  345. params[n++] = OSSL_PARAM_construct_utf8_string("p4", app_p4,
  346. sizeof(app_p4));
  347. params[n++] = OSSL_PARAM_construct_utf8_string("p5", app_p5,
  348. sizeof(app_p5));
  349. /* sizeof(app_p6_init), because we know that's what we're using */
  350. params[n++] = OSSL_PARAM_construct_utf8_ptr("p6", (char **)&app_p6,
  351. sizeof(app_p6_init));
  352. params[n++] = OSSL_PARAM_construct_octet_string("foo", &foo, sizeof(foo));
  353. params[n++] = OSSL_PARAM_construct_end();
  354. return params;
  355. }
  356. struct param_owner_st {
  357. OSSL_PARAM *static_params;
  358. OSSL_PARAM *(*constructed_params)(void);
  359. };
  360. static const struct param_owner_st raw_params = {
  361. static_raw_params, NULL
  362. };
  363. static const struct param_owner_st api_params = {
  364. static_api_params, construct_api_params
  365. };
  366. /*-
  367. * TESTING
  368. * =======
  369. */
  370. /*
  371. * Test cases to combine parameters with "provider side" functions
  372. */
  373. static struct {
  374. const struct provider_dispatch_st *prov;
  375. const struct param_owner_st *app;
  376. const char *desc;
  377. } test_cases[] = {
  378. /* Tests within specific methods */
  379. { &provider_raw, &raw_params, "raw provider vs raw params" },
  380. { &provider_api, &api_params, "api provider vs api params" },
  381. /* Mixed methods */
  382. { &provider_raw, &api_params, "raw provider vs api params" },
  383. { &provider_api, &raw_params, "api provider vs raw params" },
  384. };
  385. /* Generic tester of combinations of "providers" and params */
  386. static int test_case_variant(OSSL_PARAM *params, const struct provider_dispatch_st *prov)
  387. {
  388. BIGNUM *verify_p3 = NULL;
  389. void *obj = NULL;
  390. int errcnt = 0;
  391. OSSL_PARAM *p;
  392. /*
  393. * Initialize
  394. */
  395. if (!TEST_ptr(obj = init_object())
  396. || !TEST_true(BN_hex2bn(&verify_p3, p3_init))) {
  397. errcnt++;
  398. goto fin;
  399. }
  400. /*
  401. * Get parameters a first time, just to see that getting works and
  402. * gets us the values we expect.
  403. */
  404. init_app_variables();
  405. if (!TEST_true(prov->get_params(obj, params))
  406. || !TEST_int_eq(app_p1, p1_init) /* "provider" value */
  407. || !TEST_double_eq(app_p2, app_p2_init) /* Should remain untouched */
  408. || !TEST_ptr(p = OSSL_PARAM_locate(params, "p3"))
  409. || !TEST_ptr(BN_native2bn(bignumbin, p->return_size, app_p3))
  410. || !TEST_BN_eq(app_p3, verify_p3) /* "provider" value */
  411. || !TEST_str_eq(app_p4, p4_init) /* "provider" value */
  412. || !TEST_ptr(p = OSSL_PARAM_locate(params, "p5"))
  413. || !TEST_size_t_eq(p->return_size,
  414. sizeof(p5_init) - 1) /* "provider" value */
  415. || !TEST_str_eq(app_p5, p5_init) /* "provider" value */
  416. || !TEST_ptr(p = OSSL_PARAM_locate(params, "p6"))
  417. || !TEST_size_t_eq(p->return_size,
  418. sizeof(p6_init) - 1) /* "provider" value */
  419. || !TEST_str_eq(app_p6, p6_init) /* "provider" value */
  420. || !TEST_char_eq(foo[0], app_foo_init) /* Should remain untouched */
  421. || !TEST_ptr(p = OSSL_PARAM_locate(params, "foo")))
  422. errcnt++;
  423. /*
  424. * Set parameters, then sneak into the object itself and check
  425. * that its attributes got set (or ignored) properly.
  426. */
  427. init_app_variables();
  428. if (!TEST_true(prov->set_params(obj, params))) {
  429. errcnt++;
  430. } else {
  431. struct object_st *sneakpeek = obj;
  432. if (!TEST_int_eq(sneakpeek->p1, app_p1) /* app value set */
  433. || !TEST_double_eq(sneakpeek->p2, p2_init) /* Should remain untouched */
  434. || !TEST_BN_eq(sneakpeek->p3, app_p3) /* app value set */
  435. || !TEST_str_eq(sneakpeek->p4, app_p4) /* app value set */
  436. || !TEST_str_eq(sneakpeek->p5, app_p5) /* app value set */
  437. || !TEST_str_eq(sneakpeek->p6, app_p6)) /* app value set */
  438. errcnt++;
  439. }
  440. /*
  441. * Get parameters again, checking that we get different values
  442. * than earlier where relevant.
  443. */
  444. BN_free(verify_p3);
  445. verify_p3 = NULL;
  446. if (!TEST_true(BN_hex2bn(&verify_p3, app_p3_init))) {
  447. errcnt++;
  448. goto fin;
  449. }
  450. if (!TEST_true(prov->get_params(obj, params))
  451. || !TEST_int_eq(app_p1, app_p1_init) /* app value */
  452. || !TEST_double_eq(app_p2, app_p2_init) /* Should remain untouched */
  453. || !TEST_ptr(p = OSSL_PARAM_locate(params, "p3"))
  454. || !TEST_ptr(BN_native2bn(bignumbin, p->return_size, app_p3))
  455. || !TEST_BN_eq(app_p3, verify_p3) /* app value */
  456. || !TEST_str_eq(app_p4, app_p4_init) /* app value */
  457. || !TEST_ptr(p = OSSL_PARAM_locate(params, "p5"))
  458. || !TEST_size_t_eq(p->return_size,
  459. sizeof(app_p5_init) - 1) /* app value */
  460. || !TEST_str_eq(app_p5, app_p5_init) /* app value */
  461. || !TEST_ptr(p = OSSL_PARAM_locate(params, "p6"))
  462. || !TEST_size_t_eq(p->return_size,
  463. sizeof(app_p6_init) - 1) /* app value */
  464. || !TEST_str_eq(app_p6, app_p6_init) /* app value */
  465. || !TEST_char_eq(foo[0], app_foo_init) /* Should remain untouched */
  466. || !TEST_ptr(p = OSSL_PARAM_locate(params, "foo")))
  467. errcnt++;
  468. fin:
  469. BN_free(verify_p3);
  470. verify_p3 = NULL;
  471. cleanup_app_variables();
  472. cleanup_object(obj);
  473. return errcnt == 0;
  474. }
  475. static int test_case(int i)
  476. {
  477. TEST_info("Case: %s", test_cases[i].desc);
  478. return test_case_variant(test_cases[i].app->static_params,
  479. test_cases[i].prov)
  480. && (test_cases[i].app->constructed_params == NULL
  481. || test_case_variant(test_cases[i].app->constructed_params(),
  482. test_cases[i].prov));
  483. }
  484. /*-
  485. * OSSL_PARAM_allocate_from_text() tests
  486. * =====================================
  487. */
  488. static const OSSL_PARAM params_from_text[] = {
  489. /* Fixed size buffer */
  490. OSSL_PARAM_int32("int", NULL),
  491. OSSL_PARAM_DEFN("short", OSSL_PARAM_INTEGER, NULL, sizeof(int16_t)),
  492. OSSL_PARAM_DEFN("ushort", OSSL_PARAM_UNSIGNED_INTEGER, NULL, sizeof(uint16_t)),
  493. /* Arbitrary size buffer. Make sure the result fits in a long */
  494. OSSL_PARAM_DEFN("num", OSSL_PARAM_INTEGER, NULL, 0),
  495. OSSL_PARAM_DEFN("unum", OSSL_PARAM_UNSIGNED_INTEGER, NULL, 0),
  496. OSSL_PARAM_DEFN("octets", OSSL_PARAM_OCTET_STRING, NULL, 0),
  497. OSSL_PARAM_END,
  498. };
  499. struct int_from_text_test_st {
  500. const char *argname;
  501. const char *strval;
  502. long int expected_intval;
  503. int expected_res;
  504. size_t expected_bufsize;
  505. };
  506. static struct int_from_text_test_st int_from_text_test_cases[] = {
  507. { "int", "", 0, 0, 0 },
  508. { "int", "0", 0, 1, 4 },
  509. { "int", "101", 101, 1, 4 },
  510. { "int", "-102", -102, 1, 4 },
  511. { "int", "12A", 12, 1, 4 }, /* incomplete */
  512. { "int", "0x12B", 0x12B, 1, 4 },
  513. { "hexint", "12C", 0x12C, 1, 4 },
  514. { "hexint", "0x12D", 0, 1, 4 }, /* zero */
  515. /* test check of the target buffer size */
  516. { "int", "0x7fffffff", INT32_MAX, 1, 4 },
  517. { "int", "2147483647", INT32_MAX, 1, 4 },
  518. { "int", "2147483648", 0, 0, 0 }, /* too small buffer */
  519. { "int", "-2147483648", INT32_MIN, 1, 4 },
  520. { "int", "-2147483649", 0, 0, 4 }, /* too small buffer */
  521. { "short", "0x7fff", INT16_MAX, 1, 2 },
  522. { "short", "32767", INT16_MAX, 1, 2 },
  523. { "short", "32768", 0, 0, 0 }, /* too small buffer */
  524. { "ushort", "0xffff", UINT16_MAX, 1, 2 },
  525. { "ushort", "65535", UINT16_MAX, 1, 2 },
  526. { "ushort", "65536", 0, 0, 0 }, /* too small buffer */
  527. /* test check of sign extension in arbitrary size results */
  528. { "num", "0", 0, 1, 1 },
  529. { "num", "0", 0, 1, 1 },
  530. { "num", "0xff", 0xff, 1, 2 }, /* sign extension */
  531. { "num", "-0xff", -0xff, 1, 2 }, /* sign extension */
  532. { "num", "0x7f", 0x7f, 1, 1 }, /* no sign extension */
  533. { "num", "-0x7f", -0x7f, 1, 1 }, /* no sign extension */
  534. { "num", "0x80", 0x80, 1, 2 }, /* sign extension */
  535. { "num", "-0x80", -0x80, 1, 1 }, /* no sign extension */
  536. { "num", "0x81", 0x81, 1, 2 }, /* sign extension */
  537. { "num", "-0x81", -0x81, 1, 2 }, /* sign extension */
  538. { "unum", "0xff", 0xff, 1, 1 },
  539. { "unum", "-0xff", -0xff, 0, 0 }, /* invalid neg number */
  540. { "unum", "0x7f", 0x7f, 1, 1 },
  541. { "unum", "-0x7f", -0x7f, 0, 0 }, /* invalid neg number */
  542. { "unum", "0x80", 0x80, 1, 1 },
  543. { "unum", "-0x80", -0x80, 0, 0 }, /* invalid neg number */
  544. { "unum", "0x81", 0x81, 1, 1 },
  545. { "unum", "-0x81", -0x81, 0, 0 }, /* invalid neg number */
  546. };
  547. static int check_int_from_text(const struct int_from_text_test_st a)
  548. {
  549. OSSL_PARAM param;
  550. long int val = 0;
  551. int res;
  552. if (!OSSL_PARAM_allocate_from_text(&param, params_from_text,
  553. a.argname, a.strval, 0, NULL)) {
  554. if (a.expected_res)
  555. TEST_error("unexpected OSSL_PARAM_allocate_from_text() return for %s \"%s\"",
  556. a.argname, a.strval);
  557. return !a.expected_res;
  558. }
  559. /* For data size zero, OSSL_PARAM_get_long() may crash */
  560. if (param.data_size == 0) {
  561. OPENSSL_free(param.data);
  562. TEST_error("unexpected zero size for %s \"%s\"",
  563. a.argname, a.strval);
  564. return 0;
  565. }
  566. res = OSSL_PARAM_get_long(&param, &val);
  567. OPENSSL_free(param.data);
  568. if (res ^ a.expected_res) {
  569. TEST_error("unexpected OSSL_PARAM_get_long() return for %s \"%s\": "
  570. "%d != %d", a.argname, a.strval, a.expected_res, res);
  571. return 0;
  572. }
  573. if (val != a.expected_intval) {
  574. TEST_error("unexpected result for %s \"%s\": %li != %li",
  575. a.argname, a.strval, a.expected_intval, val);
  576. return 0;
  577. }
  578. if (param.data_size != a.expected_bufsize) {
  579. TEST_error("unexpected size for %s \"%s\": %d != %d",
  580. a.argname, a.strval,
  581. (int)a.expected_bufsize, (int)param.data_size);
  582. return 0;
  583. }
  584. return a.expected_res;
  585. }
  586. static int check_octetstr_from_hexstr(void)
  587. {
  588. OSSL_PARAM param;
  589. static const char *values[] = { "", "F", "FF", "FFF", "FFFF", NULL };
  590. int i;
  591. int errcnt = 0;
  592. /* Test odd vs even number of hex digits */
  593. for (i = 0; values[i] != NULL; i++) {
  594. int expected = (strlen(values[i]) % 2) != 1;
  595. int result;
  596. ERR_clear_error();
  597. memset(&param, 0, sizeof(param));
  598. if (expected)
  599. result =
  600. TEST_true(OSSL_PARAM_allocate_from_text(&param,
  601. params_from_text,
  602. "hexoctets", values[i], 0,
  603. NULL));
  604. else
  605. result =
  606. TEST_false(OSSL_PARAM_allocate_from_text(&param,
  607. params_from_text,
  608. "hexoctets", values[i], 0,
  609. NULL));
  610. if (!result) {
  611. TEST_error("unexpected OSSL_PARAM_allocate_from_text() %s for 'octets' \"%s\"",
  612. (expected ? "failure" : "success"), values[i]);
  613. errcnt++;
  614. }
  615. OPENSSL_free(param.data);
  616. }
  617. return errcnt == 0;
  618. }
  619. static int test_allocate_from_text(int i)
  620. {
  621. return check_int_from_text(int_from_text_test_cases[i]);
  622. }
  623. static int test_more_allocate_from_text(void)
  624. {
  625. return check_octetstr_from_hexstr();
  626. }
  627. int setup_tests(void)
  628. {
  629. ADD_ALL_TESTS(test_case, OSSL_NELEM(test_cases));
  630. ADD_ALL_TESTS(test_allocate_from_text, OSSL_NELEM(int_from_text_test_cases));
  631. ADD_TEST(test_more_allocate_from_text);
  632. return 1;
  633. }