param_build_test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <string.h>
  11. #include <openssl/params.h>
  12. #include "internal/param_build.h"
  13. #include "internal/nelem.h"
  14. #include "testutil.h"
  15. static int template_public_test(void)
  16. {
  17. OSSL_PARAM_BLD bld;
  18. OSSL_PARAM *params = NULL, *p;
  19. BIGNUM *bn = NULL, *bn_res = NULL;
  20. int i;
  21. long int l;
  22. int32_t i32;
  23. int64_t i64;
  24. double d;
  25. char *utf = NULL;
  26. const char *cutf;
  27. int res = 0;
  28. ossl_param_bld_init(&bld);
  29. if (!TEST_true(ossl_param_bld_push_int(&bld, "i", -6))
  30. || !TEST_true(ossl_param_bld_push_long(&bld, "l", 42))
  31. || !TEST_true(ossl_param_bld_push_int32(&bld, "i32", 1532))
  32. || !TEST_true(ossl_param_bld_push_int64(&bld, "i64", -9999999))
  33. || !TEST_true(ossl_param_bld_push_double(&bld, "d", 1.61803398875))
  34. || !TEST_ptr(bn = BN_new())
  35. || !TEST_true(BN_set_word(bn, 1729))
  36. || !TEST_true(ossl_param_bld_push_BN(&bld, "bignumber", bn))
  37. || !TEST_true(ossl_param_bld_push_utf8_string(&bld, "utf8_s", "foo",
  38. sizeof("foo")))
  39. || !TEST_true(ossl_param_bld_push_utf8_ptr(&bld, "utf8_p", "bar-boom",
  40. 0))
  41. || !TEST_ptr(params = ossl_param_bld_to_param(&bld))
  42. /* Check int */
  43. || !TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
  44. || !TEST_true(OSSL_PARAM_get_int(p, &i))
  45. || !TEST_str_eq(p->key, "i")
  46. || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
  47. || !TEST_size_t_eq(p->data_size, sizeof(int))
  48. || !TEST_int_eq(i, -6)
  49. /* Check int32 */
  50. || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32"))
  51. || !TEST_true(OSSL_PARAM_get_int32(p, &i32))
  52. || !TEST_str_eq(p->key, "i32")
  53. || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
  54. || !TEST_size_t_eq(p->data_size, sizeof(int32_t))
  55. || !TEST_int_eq((int)i32, 1532)
  56. /* Check int64 */
  57. || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64"))
  58. || !TEST_str_eq(p->key, "i64")
  59. || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
  60. || !TEST_size_t_eq(p->data_size, sizeof(int64_t))
  61. || !TEST_true(OSSL_PARAM_get_int64(p, &i64))
  62. || !TEST_long_eq((long)i64, -9999999)
  63. /* Check long */
  64. || !TEST_ptr(p = OSSL_PARAM_locate(params, "l"))
  65. || !TEST_str_eq(p->key, "l")
  66. || !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
  67. || !TEST_size_t_eq(p->data_size, sizeof(long int))
  68. || !TEST_true(OSSL_PARAM_get_long(p, &l))
  69. || !TEST_long_eq(l, 42)
  70. /* Check double */
  71. || !TEST_ptr(p = OSSL_PARAM_locate(params, "d"))
  72. || !TEST_true(OSSL_PARAM_get_double(p, &d))
  73. || !TEST_str_eq(p->key, "d")
  74. || !TEST_uint_eq(p->data_type, OSSL_PARAM_REAL)
  75. || !TEST_size_t_eq(p->data_size, sizeof(double))
  76. || !TEST_double_eq(d, 1.61803398875)
  77. /* Check UTF8 string */
  78. || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_s"))
  79. || !TEST_str_eq(p->data, "foo")
  80. || !TEST_true(OSSL_PARAM_get_utf8_string(p, &utf, 0))
  81. || !TEST_str_eq(utf, "foo")
  82. /* Check UTF8 pointer */
  83. || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_p"))
  84. || !TEST_true(OSSL_PARAM_get_utf8_ptr(p, &cutf))
  85. || !TEST_str_eq(cutf, "bar-boom")
  86. /* Check BN */
  87. || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber"))
  88. || !TEST_str_eq(p->key, "bignumber")
  89. || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
  90. || !TEST_true(OSSL_PARAM_get_BN(p, &bn_res))
  91. || !TEST_int_eq(BN_cmp(bn_res, bn), 0))
  92. goto err;
  93. res = 1;
  94. err:
  95. ossl_param_bld_free(params);
  96. OPENSSL_free(utf);
  97. BN_free(bn);
  98. BN_free(bn_res);
  99. return res;
  100. }
  101. static int template_private_test(void)
  102. {
  103. static int data1[] = { 2, 3, 5, 7, 11, 15, 17 };
  104. static unsigned char data2[] = { 2, 4, 6, 8, 10 };
  105. OSSL_PARAM_BLD bld;
  106. OSSL_PARAM *params = NULL, *p;
  107. unsigned int i;
  108. unsigned long int l;
  109. uint32_t i32;
  110. uint64_t i64;
  111. size_t st;
  112. BIGNUM *bn = NULL, *bn_res = NULL;
  113. int res = 0;
  114. ossl_param_bld_init(&bld);
  115. if (!TEST_true(ossl_param_bld_push_uint(&bld, "i", 6))
  116. || !TEST_true(ossl_param_bld_push_ulong(&bld, "l", 42))
  117. || !TEST_true(ossl_param_bld_push_uint32(&bld, "i32", 1532))
  118. || !TEST_true(ossl_param_bld_push_uint64(&bld, "i64", 9999999))
  119. || !TEST_true(ossl_param_bld_push_size_t(&bld, "st", 65537))
  120. || !TEST_ptr(bn = BN_secure_new())
  121. || !TEST_true(BN_set_word(bn, 1729))
  122. || !TEST_true(ossl_param_bld_push_BN(&bld, "bignumber", bn))
  123. || !TEST_true(ossl_param_bld_push_octet_string(&bld, "oct_s", data1,
  124. sizeof(data1)))
  125. || !TEST_true(ossl_param_bld_push_octet_ptr(&bld, "oct_p", data2,
  126. sizeof(data2)))
  127. || !TEST_ptr(params = ossl_param_bld_to_param(&bld))
  128. /* Check unsigned int */
  129. || !TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
  130. || !TEST_true(OSSL_PARAM_get_uint(p, &i))
  131. || !TEST_str_eq(p->key, "i")
  132. || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
  133. || !TEST_size_t_eq(p->data_size, sizeof(int))
  134. || !TEST_uint_eq(i, 6)
  135. /* Check unsigned int32 */
  136. || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32"))
  137. || !TEST_true(OSSL_PARAM_get_uint32(p, &i32))
  138. || !TEST_str_eq(p->key, "i32")
  139. || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
  140. || !TEST_size_t_eq(p->data_size, sizeof(int32_t))
  141. || !TEST_uint_eq((unsigned int)i32, 1532)
  142. /* Check unsigned int64 */
  143. || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64"))
  144. || !TEST_str_eq(p->key, "i64")
  145. || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
  146. || !TEST_size_t_eq(p->data_size, sizeof(int64_t))
  147. || !TEST_true(OSSL_PARAM_get_uint64(p, &i64))
  148. || !TEST_ulong_eq((unsigned long)i64, 9999999)
  149. /* Check unsigned long int */
  150. || !TEST_ptr(p = OSSL_PARAM_locate(params, "l"))
  151. || !TEST_str_eq(p->key, "l")
  152. || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
  153. || !TEST_size_t_eq(p->data_size, sizeof(unsigned long int))
  154. || !TEST_true(OSSL_PARAM_get_ulong(p, &l))
  155. || !TEST_ulong_eq(l, 42)
  156. /* Check size_t */
  157. || !TEST_ptr(p = OSSL_PARAM_locate(params, "st"))
  158. || !TEST_str_eq(p->key, "st")
  159. || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
  160. || !TEST_size_t_eq(p->data_size, sizeof(size_t))
  161. || !TEST_true(OSSL_PARAM_get_size_t(p, &st))
  162. || !TEST_size_t_eq(st, 65537)
  163. /* Check octet string */
  164. || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_s"))
  165. || !TEST_str_eq(p->key, "oct_s")
  166. || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_STRING)
  167. || !TEST_mem_eq(p->data, p->data_size, data1, sizeof(data1))
  168. /* Check octet pointer */
  169. || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_p"))
  170. || !TEST_str_eq(p->key, "oct_p")
  171. || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_PTR)
  172. || !TEST_mem_eq(*(void **)p->data, p->data_size, data2, sizeof(data2))
  173. /* Check BN */
  174. || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber"))
  175. || !TEST_str_eq(p->key, "bignumber")
  176. || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
  177. || !TEST_true(OSSL_PARAM_get_BN(p, &bn_res))
  178. || !TEST_int_eq(BN_cmp(bn_res, bn), 0))
  179. goto err;
  180. res = 1;
  181. err:
  182. ossl_param_bld_free(params);
  183. BN_free(bn);
  184. BN_free(bn_res);
  185. return res;
  186. }
  187. static int template_static_params_test(int n)
  188. {
  189. unsigned char data[1000], secure[500];
  190. OSSL_PARAM_BLD bld;
  191. OSSL_PARAM params[20], *p;
  192. BIGNUM *bn = NULL, *bn_r = NULL;
  193. unsigned int i;
  194. char *utf = NULL;
  195. int res = 0;
  196. ossl_param_bld_init(&bld);
  197. if (!TEST_true(ossl_param_bld_push_uint(&bld, "i", 6))
  198. || !TEST_ptr(bn = (n & 1) == 0 ? BN_new() : BN_secure_new())
  199. || !TEST_true(BN_set_word(bn, 1337))
  200. || !TEST_true(ossl_param_bld_push_BN(&bld, "bn", bn))
  201. || !TEST_true(ossl_param_bld_push_utf8_string(&bld, "utf8_s", "bar",
  202. 0))
  203. || !TEST_ptr(ossl_param_bld_to_param_ex(&bld, params,
  204. OSSL_NELEM(params),
  205. data, sizeof(data),
  206. secure, sizeof(secure)))
  207. /* Check unsigned int */
  208. || !TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
  209. || !TEST_true(OSSL_PARAM_get_uint(p, &i))
  210. || !TEST_str_eq(p->key, "i")
  211. || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
  212. || !TEST_size_t_eq(p->data_size, sizeof(int))
  213. || !TEST_uint_eq(i, 6)
  214. /* Check BIGNUM */
  215. || !TEST_ptr(p = OSSL_PARAM_locate(params, "bn"))
  216. || !TEST_true(OSSL_PARAM_get_BN(p, &bn_r))
  217. || !TEST_str_eq(p->key, "bn")
  218. || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
  219. || !TEST_size_t_le(p->data_size, sizeof(BN_ULONG))
  220. || !TEST_uint_eq((unsigned int)BN_get_word(bn_r), 1337)
  221. /* Check UTF8 string */
  222. || !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_s"))
  223. || !TEST_str_eq(p->data, "bar")
  224. || !TEST_true(OSSL_PARAM_get_utf8_string(p, &utf, 0))
  225. || !TEST_str_eq(utf, "bar"))
  226. goto err;
  227. res = 1;
  228. err:
  229. OPENSSL_free(utf);
  230. BN_free(bn);
  231. BN_free(bn_r);
  232. return res;
  233. }
  234. static int template_static_fail_test(int n)
  235. {
  236. unsigned char data[10000], secure[500];
  237. OSSL_PARAM_BLD bld;
  238. OSSL_PARAM prms[20];
  239. BIGNUM *bn = NULL;
  240. int res = 0;
  241. ossl_param_bld_init(&bld);
  242. if (!TEST_true(ossl_param_bld_push_uint(&bld, "i", 6))
  243. || !TEST_ptr(bn = (n & 1) == 0 ? BN_new() : BN_secure_new())
  244. || !TEST_true(BN_hex2bn(&bn, "ABCDEF78901234567890ABCDEF0987987654321"))
  245. || !TEST_true(ossl_param_bld_push_BN(&bld, "bn", bn))
  246. || !TEST_true(ossl_param_bld_push_utf8_string(&bld, "utf8_s", "abc",
  247. 1000))
  248. /* No OSSL_PARAMS */
  249. || !TEST_ptr_null(ossl_param_bld_to_param_ex(&bld, NULL, 0, data,
  250. sizeof(data), secure,
  251. sizeof(secure)))
  252. /* Short OSSL_PARAMS */
  253. || !TEST_ptr_null(ossl_param_bld_to_param_ex(&bld, prms, 2,
  254. data, sizeof(data),
  255. secure, sizeof(secure)))
  256. /* No normal data */
  257. || !TEST_ptr_null(ossl_param_bld_to_param_ex(&bld, prms,
  258. OSSL_NELEM(prms),
  259. NULL, 0, secure,
  260. sizeof(secure)))
  261. /* Not enough normal data */
  262. || !TEST_ptr_null(ossl_param_bld_to_param_ex(&bld, prms,
  263. OSSL_NELEM(prms),
  264. data, 50, secure,
  265. sizeof(secure))))
  266. goto err;
  267. if ((n & 1) == 1) {
  268. /* No secure data */
  269. if (!TEST_ptr_null(ossl_param_bld_to_param_ex(&bld, prms,
  270. OSSL_NELEM(prms),
  271. data, sizeof(data),
  272. NULL, 0))
  273. /* Not enough secure data */
  274. || !TEST_ptr_null(ossl_param_bld_to_param_ex(&bld, prms,
  275. OSSL_NELEM(prms),
  276. data, sizeof(data),
  277. secure, 4)))
  278. goto err;
  279. }
  280. res = 1;
  281. err:
  282. BN_free(bn);
  283. return res;
  284. }
  285. int setup_tests(void)
  286. {
  287. ADD_TEST(template_public_test);
  288. ADD_TEST(template_private_test);
  289. ADD_ALL_TESTS(template_static_params_test, 2);
  290. ADD_ALL_TESTS(template_static_fail_test, 2);
  291. return 1;
  292. }