params_test.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * Copyright 2019 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/params.h>
  17. #include "internal/nelem.h"
  18. #include "testutil.h"
  19. /*-
  20. * PROVIDER SECTION
  21. * ================
  22. *
  23. * Even though it's not necessarily ONLY providers doing this part,
  24. * they are naturally going to be the most common users of
  25. * set_params and get_params functions.
  26. */
  27. /*
  28. * In real use cases, setters and getters would take an object with
  29. * which the parameters are associated. This structure is a cheap
  30. * simulation.
  31. */
  32. struct object_st {
  33. /*
  34. * Documented as a native integer, of the size given by sizeof(int).
  35. * Assumed data type OSSL_PARAM_INTEGER
  36. */
  37. int p1;
  38. /*
  39. * Documented as a native double, of the size given by sizeof(double).
  40. * Assumed data type OSSL_PARAM_REAL
  41. */
  42. double p2;
  43. /*
  44. * Documented as an arbitrarly large unsigned integer.
  45. * The data size must be large enough to accomodate.
  46. * Assumed data type OSSL_PARAM_UNSIGNED_INTEGER
  47. */
  48. BIGNUM *p3;
  49. /*
  50. * Documented as a C string.
  51. * The data size must be large enough to accomodate.
  52. * Assumed data type OSSL_PARAM_UTF8_STRING
  53. */
  54. char *p4;
  55. size_t p4_l;
  56. /*
  57. * Documented as a C string.
  58. * Assumed data type OSSL_PARAM_UTF8_STRING
  59. */
  60. char p5[256];
  61. size_t p5_l;
  62. /*
  63. * Documented as a pointer to a constant C string.
  64. * Assumed data type OSSL_PARAM_UTF8_PTR
  65. */
  66. const char *p6;
  67. size_t p6_l;
  68. };
  69. #define p1_init 42 /* The ultimate answer */
  70. #define p2_init 6.283 /* Magic number */
  71. /* Stolen from evp_data, BLAKE2s256 test */
  72. #define p3_init \
  73. "4142434445464748494a4b4c4d4e4f50" \
  74. "5152535455565758595a616263646566" \
  75. "6768696a6b6c6d6e6f70717273747576" \
  76. "7778797a30313233343536373839"
  77. #define p4_init "BLAKE2s256" /* Random string */
  78. #define p5_init "Hellow World" /* Random string */
  79. #define p6_init OPENSSL_FULL_VERSION_STR /* Static string */
  80. static void cleanup_object(void *vobj)
  81. {
  82. struct object_st *obj = vobj;
  83. BN_free(obj->p3);
  84. obj->p3 = NULL;
  85. OPENSSL_free(obj->p4);
  86. obj->p4 = NULL;
  87. OPENSSL_free(obj);
  88. }
  89. static void *init_object(void)
  90. {
  91. struct object_st *obj = OPENSSL_zalloc(sizeof(*obj));
  92. obj->p1 = p1_init;
  93. obj->p2 = p2_init;
  94. if (!TEST_true(BN_hex2bn(&obj->p3, p3_init)))
  95. goto fail;
  96. if (!TEST_ptr(obj->p4 = OPENSSL_strdup(p4_init)))
  97. goto fail;
  98. strcpy(obj->p5, p5_init);
  99. obj->p6 = p6_init;
  100. return obj;
  101. fail:
  102. cleanup_object(obj);
  103. obj = NULL;
  104. return NULL;
  105. }
  106. /*
  107. * RAW provider, which handles the parameters in a very raw manner,
  108. * with no fancy API and very minimal checking. The application that
  109. * calls these to set or request parameters MUST get its OSSL_PARAM
  110. * array right.
  111. */
  112. static int raw_set_params(void *vobj, const OSSL_PARAM *params)
  113. {
  114. struct object_st *obj = vobj;
  115. for (; params->key != NULL; params++)
  116. if (strcmp(params->key, "p1") == 0) {
  117. obj->p1 = *(int *)params->data;
  118. } else if (strcmp(params->key, "p2") == 0) {
  119. obj->p2 = *(double *)params->data;
  120. } else if (strcmp(params->key, "p3") == 0) {
  121. BN_free(obj->p3);
  122. if (!TEST_ptr(obj->p3 = BN_native2bn(params->data,
  123. params->data_size, NULL)))
  124. return 0;
  125. } else if (strcmp(params->key, "p4") == 0) {
  126. OPENSSL_free(obj->p4);
  127. if (!TEST_ptr(obj->p4 = OPENSSL_strndup(params->data,
  128. params->data_size)))
  129. return 0;
  130. } else if (strcmp(params->key, "p5") == 0) {
  131. strncpy(obj->p5, params->data, params->data_size);
  132. obj->p5_l = strlen(obj->p5) + 1;
  133. } else if (strcmp(params->key, "p6") == 0) {
  134. obj->p6 = *(const char **)params->data;
  135. obj->p6_l = params->data_size;
  136. }
  137. return 1;
  138. }
  139. static int raw_get_params(void *vobj, const OSSL_PARAM *params)
  140. {
  141. struct object_st *obj = vobj;
  142. for (; params->key != NULL; params++)
  143. if (strcmp(params->key, "p1") == 0) {
  144. if (params->return_size != NULL)
  145. *params->return_size = sizeof(obj->p1);
  146. *(int *)params->data = obj->p1;
  147. } else if (strcmp(params->key, "p2") == 0) {
  148. if (params->return_size != NULL)
  149. *params->return_size = sizeof(obj->p2);
  150. *(double *)params->data = obj->p2;
  151. } else if (strcmp(params->key, "p3") == 0) {
  152. size_t bytes = BN_num_bytes(obj->p3);
  153. if (params->return_size != NULL)
  154. *params->return_size = bytes;
  155. if (!TEST_size_t_ge(params->data_size, bytes))
  156. return 0;
  157. BN_bn2nativepad(obj->p3, params->data, bytes);
  158. } else if (strcmp(params->key, "p4") == 0) {
  159. size_t bytes = strlen(obj->p4) + 1;
  160. if (params->return_size != NULL)
  161. *params->return_size = bytes;
  162. if (!TEST_size_t_ge(params->data_size, bytes))
  163. return 0;
  164. strcpy(params->data, obj->p4);
  165. } else if (strcmp(params->key, "p5") == 0) {
  166. size_t bytes = strlen(obj->p5) + 1;
  167. if (params->return_size != NULL)
  168. *params->return_size = bytes;
  169. if (!TEST_size_t_ge(params->data_size, bytes))
  170. return 0;
  171. strcpy(params->data, obj->p5);
  172. } else if (strcmp(params->key, "p6") == 0) {
  173. /*
  174. * We COULD also use OPENSSL_FULL_VERSION_STR directly and
  175. * use sizeof(OPENSSL_FULL_VERSION_STR) instead of calling
  176. * strlen().
  177. * The caller wouldn't know the difference.
  178. */
  179. size_t bytes = strlen(obj->p6) + 1;
  180. if (params->return_size != NULL)
  181. *params->return_size = bytes;
  182. *(const char **)params->data = obj->p6;
  183. }
  184. return 1;
  185. }
  186. /*
  187. * API provider, which handles the parameters using the API from params.h
  188. */
  189. static int api_set_params(void *vobj, const OSSL_PARAM *params)
  190. {
  191. struct object_st *obj = vobj;
  192. const OSSL_PARAM *p = NULL;
  193. if ((p = OSSL_PARAM_locate(params, "p1")) != NULL
  194. && !TEST_true(OSSL_PARAM_get_int(p, &obj->p1)))
  195. return 0;
  196. if ((p = OSSL_PARAM_locate(params, "p2")) != NULL
  197. && !TEST_true(OSSL_PARAM_get_double(p, &obj->p2)))
  198. return 0;
  199. if ((p = OSSL_PARAM_locate(params, "p3")) != NULL
  200. && !TEST_true(OSSL_PARAM_get_BN(p, &obj->p3)))
  201. return 0;
  202. if ((p = OSSL_PARAM_locate(params, "p4")) != NULL) {
  203. OPENSSL_free(obj->p4);
  204. obj->p4 = NULL;
  205. /* If the value pointer is NULL, we get it automatically allocated */
  206. if (!TEST_true(OSSL_PARAM_get_utf8_string(p, &obj->p4, 0)))
  207. return 0;
  208. }
  209. if ((p = OSSL_PARAM_locate(params, "p5")) != NULL) {
  210. char *p5_ptr = obj->p5;
  211. if (!TEST_true(OSSL_PARAM_get_utf8_string(p, &p5_ptr, sizeof(obj->p5))))
  212. return 0;
  213. obj->p5_l = strlen(obj->p5) + 1;
  214. }
  215. if ((p = OSSL_PARAM_locate(params, "p6")) != NULL) {
  216. if (!TEST_true(OSSL_PARAM_get_utf8_ptr(p, &obj->p6)))
  217. return 0;
  218. obj->p6_l = strlen(obj->p6) + 1;
  219. }
  220. return 1;
  221. }
  222. static int api_get_params(void *vobj, const OSSL_PARAM *params)
  223. {
  224. struct object_st *obj = vobj;
  225. const OSSL_PARAM *p = NULL;
  226. if ((p = OSSL_PARAM_locate(params, "p1")) != NULL
  227. && !TEST_true(OSSL_PARAM_set_int(p, obj->p1)))
  228. return 0;
  229. if ((p = OSSL_PARAM_locate(params, "p2")) != NULL
  230. && !TEST_true(OSSL_PARAM_set_double(p, obj->p2)))
  231. return 0;
  232. if ((p = OSSL_PARAM_locate(params, "p3")) != NULL
  233. && !TEST_true(OSSL_PARAM_set_BN(p, obj->p3)))
  234. return 0;
  235. if ((p = OSSL_PARAM_locate(params, "p4")) != NULL
  236. && !TEST_true(OSSL_PARAM_set_utf8_string(p, obj->p4)))
  237. return 0;
  238. if ((p = OSSL_PARAM_locate(params, "p5")) != NULL
  239. && !TEST_true(OSSL_PARAM_set_utf8_string(p, obj->p5)))
  240. return 0;
  241. if ((p = OSSL_PARAM_locate(params, "p6")) != NULL
  242. && !TEST_true(OSSL_PARAM_set_utf8_ptr(p, obj->p6)))
  243. return 0;
  244. return 1;
  245. }
  246. /*
  247. * This structure only simulates a provider dispatch, the real deal is
  248. * a bit more code that's not necessary in these tests.
  249. */
  250. struct provider_dispatch_st {
  251. int (*set_params)(void *obj, const OSSL_PARAM *params);
  252. int (*get_params)(void *obj, const OSSL_PARAM *params);
  253. };
  254. /* "raw" provider */
  255. static const struct provider_dispatch_st provider_raw = {
  256. raw_set_params, raw_get_params
  257. };
  258. /* "api" provider */
  259. static const struct provider_dispatch_st provider_api = {
  260. api_set_params, api_get_params
  261. };
  262. /*-
  263. * APPLICATION SECTION
  264. * ===================
  265. */
  266. /* In all our tests, these are variables that get manipulated as parameters
  267. *
  268. * These arrays consistenly do nothing with the "p2" parameter, and
  269. * always include a "foo" parameter. This is to check that the
  270. * set_params and get_params calls ignore the lack of parameters that
  271. * the application isn't interested in, as well as ignore parameters
  272. * they don't understand (the application may have one big bag of
  273. * parameters).
  274. */
  275. static int app_p1; /* "p1" */
  276. static double app_p2; /* "p2" is ignored */
  277. static BIGNUM *app_p3 = NULL; /* "p3" */
  278. static unsigned char bignumbin[4096]; /* "p3" */
  279. static size_t bignumbin_l; /* "p3" */
  280. static char app_p4[256]; /* "p4" */
  281. static size_t app_p4_l; /* "p4" */
  282. static char app_p5[256]; /* "p5" */
  283. static size_t app_p5_l; /* "p5" */
  284. static const char *app_p6 = NULL; /* "p6" */
  285. static size_t app_p6_l; /* "p6" */
  286. static unsigned char foo[1]; /* "foo" */
  287. static size_t foo_l; /* "foo" */
  288. #define app_p1_init 17 /* A random number */
  289. #define app_p2_init 47.11 /* Another random number */
  290. #define app_p3_init "deadbeef" /* Classic */
  291. #define app_p4_init "Hello"
  292. #define app_p5_init "World"
  293. #define app_p6_init "Cookie"
  294. #define app_foo_init 'z'
  295. static int cleanup_app_variables(void)
  296. {
  297. BN_free(app_p3);
  298. app_p3 = NULL;
  299. return 1;
  300. }
  301. static int init_app_variables(void)
  302. {
  303. int l = 0;
  304. cleanup_app_variables();
  305. app_p1 = app_p1_init;
  306. app_p2 = app_p2_init;
  307. if (!BN_hex2bn(&app_p3, app_p3_init)
  308. || (l = BN_bn2nativepad(app_p3, bignumbin, sizeof(bignumbin))) < 0)
  309. return 0;
  310. bignumbin_l = (size_t)l;
  311. strcpy(app_p4, app_p4_init);
  312. app_p4_l = sizeof(app_p4_init);
  313. strcpy(app_p5, app_p5_init);
  314. app_p5_l = sizeof(app_p5_init);
  315. app_p6 = app_p6_init;
  316. foo[0] = app_foo_init;
  317. foo_l = sizeof(app_foo_init);
  318. return 1;
  319. }
  320. /*
  321. * Here, we define test OSSL_PARAM arrays
  322. */
  323. /* An array of OSSL_PARAM, specific in the most raw manner possible */
  324. static const OSSL_PARAM static_raw_params[] = {
  325. { "p1", OSSL_PARAM_INTEGER, &app_p1, sizeof(app_p1), NULL },
  326. { "p3", OSSL_PARAM_UNSIGNED_INTEGER, &bignumbin, sizeof(bignumbin),
  327. &bignumbin_l },
  328. { "p4", OSSL_PARAM_UTF8_STRING, &app_p4, sizeof(app_p4), &app_p4_l },
  329. { "p5", OSSL_PARAM_UTF8_STRING, &app_p5, sizeof(app_p5), &app_p5_l },
  330. /* sizeof(app_p6_init), because we know that's what we're using */
  331. { "p6", OSSL_PARAM_UTF8_PTR, &app_p6, sizeof(app_p6_init), &app_p6_l },
  332. { "foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo), &foo_l },
  333. { NULL, 0, NULL, 0, NULL }
  334. };
  335. /* The same array of OSSL_PARAM, specified with the macros from params.h */
  336. static const OSSL_PARAM static_api_params[] = {
  337. OSSL_PARAM_int("p1", &app_p1),
  338. OSSL_PARAM_SIZED_BN("p3", &bignumbin, sizeof(bignumbin), bignumbin_l),
  339. OSSL_PARAM_DEFN("p4", OSSL_PARAM_UTF8_STRING,
  340. &app_p4, sizeof(app_p4), &app_p4_l),
  341. OSSL_PARAM_DEFN("p5", OSSL_PARAM_UTF8_STRING,
  342. &app_p5, sizeof(app_p5), &app_p5_l),
  343. /* sizeof(app_p6_init), because we know that's what we're using */
  344. OSSL_PARAM_DEFN("p6", OSSL_PARAM_UTF8_PTR,
  345. &app_p6, sizeof(app_p6_init), &app_p6_l),
  346. OSSL_PARAM_DEFN("foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo), &foo_l),
  347. OSSL_PARAM_END
  348. };
  349. /*
  350. * The same array again, but constructed at run-time
  351. * This exercises the OSSL_PARAM constructor functions
  352. */
  353. static OSSL_PARAM *construct_api_params(void)
  354. {
  355. size_t n = 0;
  356. static OSSL_PARAM params[10];
  357. params[n++] = OSSL_PARAM_construct_int("p1", &app_p1, NULL);
  358. params[n++] = OSSL_PARAM_construct_BN("p3", bignumbin, sizeof(bignumbin),
  359. &bignumbin_l);
  360. params[n++] = OSSL_PARAM_construct_utf8_string("p4", app_p4, sizeof(app_p4),
  361. &app_p4_l);
  362. params[n++] = OSSL_PARAM_construct_utf8_string("p5", app_p5,
  363. sizeof(app_p5), &app_p5_l);
  364. /* sizeof(app_p6_init), because we know that's what we're using */
  365. params[n++] = OSSL_PARAM_construct_utf8_ptr("p6", (char **)&app_p6,
  366. sizeof(app_p6_init), &app_p6_l);
  367. params[n++] = OSSL_PARAM_construct_octet_string("foo", &foo, sizeof(foo),
  368. &foo_l);
  369. params[n++] = OSSL_PARAM_construct_end();
  370. return params;
  371. }
  372. struct param_owner_st {
  373. const OSSL_PARAM *static_params;
  374. OSSL_PARAM *(*constructed_params)(void);
  375. };
  376. static const struct param_owner_st raw_params = {
  377. static_raw_params, NULL
  378. };
  379. static const struct param_owner_st api_params = {
  380. static_api_params, construct_api_params
  381. };
  382. /*-
  383. * TESTING
  384. * =======
  385. */
  386. /*
  387. * Test cases to combine parameters with "provider side" functions
  388. */
  389. static struct {
  390. const struct provider_dispatch_st *prov;
  391. const struct param_owner_st *app;
  392. const char *desc;
  393. } test_cases[] = {
  394. /* Tests within specific methods */
  395. { &provider_raw, &raw_params, "raw provider vs raw params" },
  396. { &provider_api, &api_params, "api provider vs api params" },
  397. /* Mixed methods */
  398. { &provider_raw, &api_params, "raw provider vs api params" },
  399. { &provider_api, &raw_params, "api provider vs raw params" },
  400. };
  401. /* Generic tester of combinations of "providers" and params */
  402. static int test_case_variant(const OSSL_PARAM *params,
  403. const struct provider_dispatch_st *prov)
  404. {
  405. BIGNUM *verify_p3 = NULL;
  406. void *obj = NULL;
  407. int errcnt = 0;
  408. /*
  409. * Initialize
  410. */
  411. if (!TEST_ptr(obj = init_object())
  412. || !TEST_true(BN_hex2bn(&verify_p3, p3_init))) {
  413. errcnt++;
  414. goto fin;
  415. }
  416. /*
  417. * Get parameters a first time, just to see that getting works and
  418. * gets us the values we expect.
  419. */
  420. init_app_variables();
  421. if (!TEST_true(prov->get_params(obj, params))
  422. || !TEST_int_eq(app_p1, p1_init) /* "provider" value */
  423. || !TEST_double_eq(app_p2, app_p2_init) /* Should remain untouched */
  424. || !TEST_ptr(BN_native2bn(bignumbin, bignumbin_l, app_p3))
  425. || !TEST_BN_eq(app_p3, verify_p3) /* "provider" value */
  426. || !TEST_str_eq(app_p4, p4_init) /* "provider" value */
  427. || !TEST_size_t_eq(app_p5_l, sizeof(p5_init)) /* "provider" value */
  428. || !TEST_str_eq(app_p5, p5_init) /* "provider" value */
  429. || !TEST_size_t_eq(app_p6_l, sizeof(p6_init)) /* "provider" value */
  430. || !TEST_str_eq(app_p6, p6_init) /* "provider" value */
  431. || !TEST_char_eq(foo[0], app_foo_init) /* Should remain untouched */
  432. || !TEST_int_eq(foo_l, sizeof(app_foo_init)))
  433. errcnt++;
  434. /*
  435. * Set parameters, then sneak into the object itself and check
  436. * that its attributes got set (or ignored) properly.
  437. */
  438. init_app_variables();
  439. if (!TEST_true(prov->set_params(obj, params))) {
  440. errcnt++;
  441. } else {
  442. struct object_st *sneakpeek = obj;
  443. if (!TEST_int_eq(sneakpeek->p1, app_p1) /* app value set */
  444. || !TEST_double_eq(sneakpeek->p2, p2_init) /* Should remain untouched */
  445. || !TEST_BN_eq(sneakpeek->p3, app_p3) /* app value set */
  446. || !TEST_str_eq(sneakpeek->p4, app_p4) /* app value set */
  447. || !TEST_size_t_eq(sneakpeek->p5_l, app_p5_l) /* app value set */
  448. || !TEST_str_eq(sneakpeek->p5, app_p5) /* app value set */
  449. || !TEST_size_t_eq(sneakpeek->p6_l,
  450. sizeof(app_p6_init)) /* app value set */
  451. || !TEST_str_eq(sneakpeek->p6, app_p6)) /* app value set */
  452. errcnt++;
  453. }
  454. /*
  455. * Get parameters again, checking that we get different values
  456. * than earlier where relevant.
  457. */
  458. BN_free(verify_p3);
  459. verify_p3 = NULL;
  460. if (!TEST_true(BN_hex2bn(&verify_p3, app_p3_init))) {
  461. errcnt++;
  462. goto fin;
  463. }
  464. if (!TEST_true(prov->get_params(obj, params))
  465. || !TEST_int_eq(app_p1, app_p1_init) /* app value */
  466. || !TEST_double_eq(app_p2, app_p2_init) /* Should remain untouched */
  467. || !TEST_ptr(BN_native2bn(bignumbin, bignumbin_l, app_p3))
  468. || !TEST_BN_eq(app_p3, verify_p3) /* app value */
  469. || !TEST_str_eq(app_p4, app_p4_init) /* app value */
  470. || !TEST_size_t_eq(app_p5_l,
  471. sizeof(app_p5_init)) /* app value */
  472. || !TEST_str_eq(app_p5, app_p5_init) /* app value */
  473. || !TEST_size_t_eq(app_p6_l,
  474. sizeof(app_p6_init)) /* app value */
  475. || !TEST_str_eq(app_p6, app_p6_init) /* app value */
  476. || !TEST_char_eq(foo[0], app_foo_init) /* Should remain untouched */
  477. || !TEST_int_eq(foo_l, sizeof(app_foo_init)))
  478. errcnt++;
  479. fin:
  480. BN_free(verify_p3);
  481. verify_p3 = NULL;
  482. cleanup_app_variables();
  483. cleanup_object(obj);
  484. return errcnt == 0;
  485. }
  486. static int test_case(int i)
  487. {
  488. TEST_info("Case: %s", test_cases[i].desc);
  489. return test_case_variant(test_cases[i].app->static_params,
  490. test_cases[i].prov)
  491. && (test_cases[i].app->constructed_params == NULL
  492. || test_case_variant(test_cases[i].app->constructed_params(),
  493. test_cases[i].prov));
  494. }
  495. int setup_tests(void)
  496. {
  497. ADD_ALL_TESTS(test_case, OSSL_NELEM(test_cases));
  498. return 1;
  499. }