2
0

evp_fetch_prov_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * SHA256 low level APIs are deprecated for public use, but still ok for
  11. * internal use. Note, that due to symbols not being exported, only the
  12. * #defines can be accessed. In this case SHA256_CBLOCK.
  13. */
  14. #include "internal/deprecated.h"
  15. #include <string.h>
  16. #include <openssl/sha.h>
  17. #include <openssl/evp.h>
  18. #include <openssl/provider.h>
  19. #include "internal/sizes.h"
  20. #include "testutil.h"
  21. static char *config_file = NULL;
  22. static char *alg = "digest";
  23. static int use_default_ctx = 0;
  24. static char *fetch_property = NULL;
  25. static int expected_fetch_result = 1;
  26. typedef enum OPTION_choice {
  27. OPT_ERR = -1,
  28. OPT_EOF = 0,
  29. OPT_ALG_FETCH_TYPE,
  30. OPT_FETCH_PROPERTY,
  31. OPT_FETCH_FAILURE,
  32. OPT_USE_DEFAULTCTX,
  33. OPT_CONFIG_FILE,
  34. OPT_TEST_ENUM
  35. } OPTION_CHOICE;
  36. const OPTIONS *test_get_options(void)
  37. {
  38. static const OPTIONS test_options[] = {
  39. OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[provname...]\n"),
  40. { "config", OPT_CONFIG_FILE, '<', "The configuration file to use for the libctx" },
  41. { "type", OPT_ALG_FETCH_TYPE, 's', "The fetch type to test" },
  42. { "property", OPT_FETCH_PROPERTY, 's', "The fetch property e.g. provider=fips" },
  43. { "fetchfail", OPT_FETCH_FAILURE, '-', "fetch is expected to fail" },
  44. { "defaultctx", OPT_USE_DEFAULTCTX, '-',
  45. "Use the default context if this is set" },
  46. { OPT_HELP_STR, 1, '-', "file\tProvider names to explicitly load\n" },
  47. { NULL }
  48. };
  49. return test_options;
  50. }
  51. static int calculate_digest(const EVP_MD *md, const char *msg, size_t len,
  52. const unsigned char *exptd)
  53. {
  54. unsigned char out[SHA256_DIGEST_LENGTH];
  55. EVP_MD_CTX *ctx;
  56. int ret = 0;
  57. if (!TEST_ptr(ctx = EVP_MD_CTX_new())
  58. || !TEST_true(EVP_DigestInit_ex(ctx, md, NULL))
  59. || !TEST_true(EVP_DigestUpdate(ctx, msg, len))
  60. || !TEST_true(EVP_DigestFinal_ex(ctx, out, NULL))
  61. || !TEST_mem_eq(out, SHA256_DIGEST_LENGTH, exptd,
  62. SHA256_DIGEST_LENGTH)
  63. || !TEST_true(md == EVP_MD_CTX_get0_md(ctx)))
  64. goto err;
  65. ret = 1;
  66. err:
  67. EVP_MD_CTX_free(ctx);
  68. return ret;
  69. }
  70. static int load_providers(OSSL_LIB_CTX **libctx, OSSL_PROVIDER *prov[])
  71. {
  72. OSSL_LIB_CTX *ctx = NULL;
  73. int ret = 0;
  74. size_t i;
  75. ctx = OSSL_LIB_CTX_new();
  76. if (!TEST_ptr(ctx))
  77. goto err;
  78. if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, config_file)))
  79. goto err;
  80. if (test_get_argument_count() > 2)
  81. goto err;
  82. for (i = 0; i < test_get_argument_count(); ++i) {
  83. char *provname = test_get_argument(i);
  84. prov[i] = OSSL_PROVIDER_load(ctx, provname);
  85. if (!TEST_ptr(prov[i]))
  86. goto err;
  87. }
  88. ret = 1;
  89. *libctx = ctx;
  90. err:
  91. if (ret == 0)
  92. OSSL_LIB_CTX_free(ctx);
  93. return ret;
  94. }
  95. static void unload_providers(OSSL_LIB_CTX **libctx, OSSL_PROVIDER *prov[])
  96. {
  97. if (prov[0] != NULL)
  98. OSSL_PROVIDER_unload(prov[0]);
  99. if (prov[1] != NULL)
  100. OSSL_PROVIDER_unload(prov[1]);
  101. /* Not normally needed, but we would like to test that
  102. * OPENSSL_thread_stop_ex() behaves as expected.
  103. */
  104. if (libctx != NULL && *libctx != NULL) {
  105. OPENSSL_thread_stop_ex(*libctx);
  106. OSSL_LIB_CTX_free(*libctx);
  107. }
  108. }
  109. static int test_legacy_provider_unloaded(void)
  110. {
  111. OSSL_LIB_CTX *ctx = NULL;
  112. int rc = 0;
  113. ctx = OSSL_LIB_CTX_new();
  114. if (!TEST_ptr(ctx))
  115. goto err;
  116. if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, config_file)))
  117. goto err;
  118. if (!TEST_int_eq(OSSL_PROVIDER_available(ctx, "legacy"), 0))
  119. goto err;
  120. rc = 1;
  121. err:
  122. OSSL_LIB_CTX_free(ctx);
  123. return rc;
  124. }
  125. static X509_ALGOR *make_algor(int nid)
  126. {
  127. X509_ALGOR *algor;
  128. if (!TEST_ptr(algor = X509_ALGOR_new())
  129. || !TEST_true(X509_ALGOR_set0(algor, OBJ_nid2obj(nid),
  130. V_ASN1_UNDEF, NULL))) {
  131. X509_ALGOR_free(algor);
  132. return NULL;
  133. }
  134. return algor;
  135. }
  136. /*
  137. * Test EVP_MD_fetch()
  138. */
  139. static int test_md(const EVP_MD *md)
  140. {
  141. const char testmsg[] = "Hello world";
  142. const unsigned char exptd[] = {
  143. 0x27, 0x51, 0x8b, 0xa9, 0x68, 0x30, 0x11, 0xf6, 0xb3, 0x96, 0x07, 0x2c,
  144. 0x05, 0xf6, 0x65, 0x6d, 0x04, 0xf5, 0xfb, 0xc3, 0x78, 0x7c, 0xf9, 0x24,
  145. 0x90, 0xec, 0x60, 0x6e, 0x50, 0x92, 0xe3, 0x26
  146. };
  147. return TEST_ptr(md)
  148. && TEST_true(EVP_MD_is_a(md, "SHA256"))
  149. && TEST_true(calculate_digest(md, testmsg, sizeof(testmsg), exptd))
  150. && TEST_int_eq(EVP_MD_get_size(md), SHA256_DIGEST_LENGTH)
  151. && TEST_int_eq(EVP_MD_get_block_size(md), SHA256_CBLOCK);
  152. }
  153. static int test_implicit_EVP_MD_fetch(void)
  154. {
  155. OSSL_LIB_CTX *ctx = NULL;
  156. OSSL_PROVIDER *prov[2] = {NULL, NULL};
  157. int ret = 0;
  158. ret = (use_default_ctx == 0 || load_providers(&ctx, prov))
  159. && test_md(EVP_sha256());
  160. unload_providers(&ctx, prov);
  161. return ret;
  162. }
  163. static int test_explicit_EVP_MD_fetch(const char *id)
  164. {
  165. OSSL_LIB_CTX *ctx = NULL;
  166. EVP_MD *md = NULL;
  167. OSSL_PROVIDER *prov[2] = {NULL, NULL};
  168. int ret = 0;
  169. if (use_default_ctx == 0 && !load_providers(&ctx, prov))
  170. goto err;
  171. md = EVP_MD_fetch(ctx, id, fetch_property);
  172. if (expected_fetch_result != 0) {
  173. if (!test_md(md))
  174. goto err;
  175. /* Also test EVP_MD_up_ref() while we're doing this */
  176. if (!TEST_true(EVP_MD_up_ref(md)))
  177. goto err;
  178. /* Ref count should now be 2. Release first one here */
  179. EVP_MD_free(md);
  180. } else {
  181. if (!TEST_ptr_null(md))
  182. goto err;
  183. }
  184. ret = 1;
  185. err:
  186. EVP_MD_free(md);
  187. unload_providers(&ctx, prov);
  188. return ret;
  189. }
  190. static int test_explicit_EVP_MD_fetch_by_name(void)
  191. {
  192. return test_explicit_EVP_MD_fetch("SHA256");
  193. }
  194. /*
  195. * idx 0: Allow names from OBJ_obj2txt()
  196. * idx 1: Force an OID in text form from OBJ_obj2txt()
  197. */
  198. static int test_explicit_EVP_MD_fetch_by_X509_ALGOR(int idx)
  199. {
  200. int ret = 0;
  201. X509_ALGOR *algor = make_algor(NID_sha256);
  202. const ASN1_OBJECT *obj;
  203. char id[OSSL_MAX_NAME_SIZE] = { 0 };
  204. if (algor == NULL)
  205. return 0;
  206. X509_ALGOR_get0(&obj, NULL, NULL, algor);
  207. switch (idx) {
  208. case 0:
  209. if (!TEST_int_gt(OBJ_obj2txt(id, sizeof(id), obj, 0), 0))
  210. goto end;
  211. break;
  212. case 1:
  213. if (!TEST_int_gt(OBJ_obj2txt(id, sizeof(id), obj, 1), 0))
  214. goto end;
  215. break;
  216. }
  217. ret = test_explicit_EVP_MD_fetch(id);
  218. end:
  219. X509_ALGOR_free(algor);
  220. return ret;
  221. }
  222. /*
  223. * Test EVP_CIPHER_fetch()
  224. */
  225. static int encrypt_decrypt(const EVP_CIPHER *cipher, const unsigned char *msg,
  226. size_t len)
  227. {
  228. int ret = 0, ctlen, ptlen;
  229. EVP_CIPHER_CTX *ctx = NULL;
  230. unsigned char key[128 / 8];
  231. unsigned char ct[64], pt[64];
  232. memset(key, 0, sizeof(key));
  233. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  234. || !TEST_true(EVP_CipherInit_ex(ctx, cipher, NULL, key, NULL, 1))
  235. || !TEST_true(EVP_CipherUpdate(ctx, ct, &ctlen, msg, len))
  236. || !TEST_true(EVP_CipherFinal_ex(ctx, ct, &ctlen))
  237. || !TEST_true(EVP_CipherInit_ex(ctx, cipher, NULL, key, NULL, 0))
  238. || !TEST_true(EVP_CipherUpdate(ctx, pt, &ptlen, ct, ctlen))
  239. || !TEST_true(EVP_CipherFinal_ex(ctx, pt, &ptlen))
  240. || !TEST_mem_eq(pt, ptlen, msg, len))
  241. goto err;
  242. ret = 1;
  243. err:
  244. EVP_CIPHER_CTX_free(ctx);
  245. return ret;
  246. }
  247. static int test_cipher(const EVP_CIPHER *cipher)
  248. {
  249. const unsigned char testmsg[] = "Hello world";
  250. return TEST_ptr(cipher)
  251. && TEST_true(encrypt_decrypt(cipher, testmsg, sizeof(testmsg)));
  252. }
  253. static int test_implicit_EVP_CIPHER_fetch(void)
  254. {
  255. OSSL_LIB_CTX *ctx = NULL;
  256. OSSL_PROVIDER *prov[2] = {NULL, NULL};
  257. int ret = 0;
  258. ret = (use_default_ctx == 0 || load_providers(&ctx, prov))
  259. && test_cipher(EVP_aes_128_cbc());
  260. unload_providers(&ctx, prov);
  261. return ret;
  262. }
  263. static int test_explicit_EVP_CIPHER_fetch(const char *id)
  264. {
  265. OSSL_LIB_CTX *ctx = NULL;
  266. EVP_CIPHER *cipher = NULL;
  267. OSSL_PROVIDER *prov[2] = {NULL, NULL};
  268. int ret = 0;
  269. if (use_default_ctx == 0 && !load_providers(&ctx, prov))
  270. goto err;
  271. cipher = EVP_CIPHER_fetch(ctx, id, fetch_property);
  272. if (expected_fetch_result != 0) {
  273. if (!test_cipher(cipher))
  274. goto err;
  275. if (!TEST_true(EVP_CIPHER_up_ref(cipher)))
  276. goto err;
  277. /* Ref count should now be 2. Release first one here */
  278. EVP_CIPHER_free(cipher);
  279. } else {
  280. if (!TEST_ptr_null(cipher))
  281. goto err;
  282. }
  283. ret = 1;
  284. err:
  285. EVP_CIPHER_free(cipher);
  286. unload_providers(&ctx, prov);
  287. return ret;
  288. }
  289. static int test_explicit_EVP_CIPHER_fetch_by_name(void)
  290. {
  291. return test_explicit_EVP_CIPHER_fetch("AES-128-CBC");
  292. }
  293. /*
  294. * idx 0: Allow names from OBJ_obj2txt()
  295. * idx 1: Force an OID in text form from OBJ_obj2txt()
  296. */
  297. static int test_explicit_EVP_CIPHER_fetch_by_X509_ALGOR(int idx)
  298. {
  299. int ret = 0;
  300. X509_ALGOR *algor = make_algor(NID_aes_128_cbc);
  301. const ASN1_OBJECT *obj;
  302. char id[OSSL_MAX_NAME_SIZE] = { 0 };
  303. if (algor == NULL)
  304. return 0;
  305. X509_ALGOR_get0(&obj, NULL, NULL, algor);
  306. switch (idx) {
  307. case 0:
  308. if (!TEST_int_gt(OBJ_obj2txt(id, sizeof(id), obj, 0), 0))
  309. goto end;
  310. break;
  311. case 1:
  312. if (!TEST_int_gt(OBJ_obj2txt(id, sizeof(id), obj, 1), 0))
  313. goto end;
  314. break;
  315. }
  316. ret = test_explicit_EVP_CIPHER_fetch(id);
  317. end:
  318. X509_ALGOR_free(algor);
  319. return ret;
  320. }
  321. int setup_tests(void)
  322. {
  323. OPTION_CHOICE o;
  324. while ((o = opt_next()) != OPT_EOF) {
  325. switch (o) {
  326. case OPT_CONFIG_FILE:
  327. config_file = opt_arg();
  328. break;
  329. case OPT_ALG_FETCH_TYPE:
  330. alg = opt_arg();
  331. break;
  332. case OPT_FETCH_PROPERTY:
  333. fetch_property = opt_arg();
  334. break;
  335. case OPT_FETCH_FAILURE:
  336. expected_fetch_result = 0;
  337. break;
  338. case OPT_USE_DEFAULTCTX:
  339. use_default_ctx = 1;
  340. break;
  341. case OPT_TEST_CASES:
  342. break;
  343. default:
  344. case OPT_ERR:
  345. return 0;
  346. }
  347. }
  348. ADD_TEST(test_legacy_provider_unloaded);
  349. if (strcmp(alg, "digest") == 0) {
  350. ADD_TEST(test_implicit_EVP_MD_fetch);
  351. ADD_TEST(test_explicit_EVP_MD_fetch_by_name);
  352. ADD_ALL_TESTS_NOSUBTEST(test_explicit_EVP_MD_fetch_by_X509_ALGOR, 2);
  353. } else {
  354. ADD_TEST(test_implicit_EVP_CIPHER_fetch);
  355. ADD_TEST(test_explicit_EVP_CIPHER_fetch_by_name);
  356. ADD_ALL_TESTS_NOSUBTEST(test_explicit_EVP_CIPHER_fetch_by_X509_ALGOR, 2);
  357. }
  358. return 1;
  359. }