ossl_store_test.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright 2020-2021 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. #include <limits.h>
  10. #include <openssl/store.h>
  11. #include <openssl/ui.h>
  12. #include "testutil.h"
  13. #ifndef PATH_MAX
  14. # if defined(_WIN32) && defined(_MAX_PATH)
  15. # define PATH_MAX _MAX_PATH
  16. # else
  17. # define PATH_MAX 4096
  18. # endif
  19. #endif
  20. typedef enum OPTION_choice {
  21. OPT_ERR = -1,
  22. OPT_EOF = 0,
  23. OPT_INPUTDIR,
  24. OPT_INFILE,
  25. OPT_SM2FILE,
  26. OPT_DATADIR,
  27. OPT_TEST_ENUM
  28. } OPTION_CHOICE;
  29. static const char *inputdir = NULL;
  30. static const char *infile = NULL;
  31. static const char *sm2file = NULL;
  32. static const char *datadir = NULL;
  33. static int test_store_open(void)
  34. {
  35. int ret = 0;
  36. OSSL_STORE_CTX *sctx = NULL;
  37. OSSL_STORE_SEARCH *search = NULL;
  38. UI_METHOD *ui_method = NULL;
  39. char *input = test_mk_file_path(inputdir, infile);
  40. ret = TEST_ptr(input)
  41. && TEST_ptr(search = OSSL_STORE_SEARCH_by_alias("nothing"))
  42. && TEST_ptr(ui_method= UI_create_method("DummyUI"))
  43. && TEST_ptr(sctx = OSSL_STORE_open_ex(input, NULL, NULL, ui_method,
  44. NULL, NULL, NULL, NULL))
  45. && TEST_false(OSSL_STORE_find(sctx, NULL))
  46. && TEST_true(OSSL_STORE_find(sctx, search));
  47. UI_destroy_method(ui_method);
  48. OSSL_STORE_SEARCH_free(search);
  49. OSSL_STORE_close(sctx);
  50. OPENSSL_free(input);
  51. return ret;
  52. }
  53. static int test_store_search_by_key_fingerprint_fail(void)
  54. {
  55. int ret;
  56. OSSL_STORE_SEARCH *search = NULL;
  57. ret = TEST_ptr_null(search = OSSL_STORE_SEARCH_by_key_fingerprint(
  58. EVP_sha256(), NULL, 0));
  59. OSSL_STORE_SEARCH_free(search);
  60. return ret;
  61. }
  62. static int get_params(const char *uri, const char *type)
  63. {
  64. EVP_PKEY *pkey = NULL;
  65. OSSL_STORE_CTX *ctx = NULL;
  66. OSSL_STORE_INFO *info;
  67. int ret = 0;
  68. ctx = OSSL_STORE_open_ex(uri, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  69. if (!TEST_ptr(ctx))
  70. goto err;
  71. while (!OSSL_STORE_eof(ctx)
  72. && (info = OSSL_STORE_load(ctx)) != NULL
  73. && pkey == NULL) {
  74. if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) {
  75. pkey = OSSL_STORE_INFO_get1_PARAMS(info);
  76. }
  77. OSSL_STORE_INFO_free(info);
  78. info = NULL;
  79. }
  80. if (pkey != NULL)
  81. ret = EVP_PKEY_is_a(pkey, type);
  82. EVP_PKEY_free(pkey);
  83. err:
  84. OSSL_STORE_close(ctx);
  85. return ret;
  86. }
  87. static int test_store_get_params(int idx)
  88. {
  89. const char *type;
  90. char uri[PATH_MAX];
  91. switch(idx) {
  92. #ifndef OPENSSL_NO_DH
  93. case 0:
  94. type = "DH";
  95. break;
  96. case 1:
  97. type = "DHX";
  98. break;
  99. #else
  100. case 0:
  101. case 1:
  102. return 1;
  103. #endif
  104. case 2:
  105. #ifndef OPENSSL_NO_DSA
  106. type = "DSA";
  107. break;
  108. #else
  109. return 1;
  110. #endif
  111. default:
  112. TEST_error("Invalid test index");
  113. return 0;
  114. }
  115. if (!TEST_true(BIO_snprintf(uri, sizeof(uri), "%s/%s-params.pem",
  116. datadir, type)))
  117. return 0;
  118. TEST_info("Testing uri: %s", uri);
  119. if (!TEST_true(get_params(uri, type)))
  120. return 0;
  121. return 1;
  122. }
  123. /*
  124. * This test verifies that calling OSSL_STORE_ATTACH does not set an
  125. * "unregistered scheme" error when called.
  126. */
  127. static int test_store_attach_unregistered_scheme(void)
  128. {
  129. int ret;
  130. OSSL_STORE_CTX *store_ctx = NULL;
  131. OSSL_PROVIDER *provider = NULL;
  132. OSSL_LIB_CTX *libctx = NULL;
  133. BIO *bio = NULL;
  134. char *input = test_mk_file_path(inputdir, sm2file);
  135. ret = TEST_ptr(input)
  136. && TEST_ptr(libctx = OSSL_LIB_CTX_new())
  137. && TEST_ptr(provider = OSSL_PROVIDER_load(libctx, "default"))
  138. && TEST_ptr(bio = BIO_new_file(input, "r"))
  139. && TEST_ptr(store_ctx = OSSL_STORE_attach(bio, "file", libctx, NULL,
  140. NULL, NULL, NULL, NULL, NULL))
  141. && TEST_int_ne(ERR_GET_LIB(ERR_peek_error()), ERR_LIB_OSSL_STORE)
  142. && TEST_int_ne(ERR_GET_REASON(ERR_peek_error()),
  143. OSSL_STORE_R_UNREGISTERED_SCHEME);
  144. BIO_free(bio);
  145. OSSL_STORE_close(store_ctx);
  146. OSSL_PROVIDER_unload(provider);
  147. OSSL_LIB_CTX_free(libctx);
  148. OPENSSL_free(input);
  149. return ret;
  150. }
  151. const OPTIONS *test_get_options(void)
  152. {
  153. static const OPTIONS test_options[] = {
  154. OPT_TEST_OPTIONS_DEFAULT_USAGE,
  155. { "dir", OPT_INPUTDIR, '/' },
  156. { "in", OPT_INFILE, '<' },
  157. { "sm2", OPT_SM2FILE, '<' },
  158. { "data", OPT_DATADIR, 's' },
  159. { NULL }
  160. };
  161. return test_options;
  162. }
  163. int setup_tests(void)
  164. {
  165. OPTION_CHOICE o;
  166. while ((o = opt_next()) != OPT_EOF) {
  167. switch (o) {
  168. case OPT_INPUTDIR:
  169. inputdir = opt_arg();
  170. break;
  171. case OPT_INFILE:
  172. infile = opt_arg();
  173. break;
  174. case OPT_SM2FILE:
  175. sm2file = opt_arg();
  176. break;
  177. case OPT_DATADIR:
  178. datadir = opt_arg();
  179. break;
  180. case OPT_TEST_CASES:
  181. break;
  182. default:
  183. case OPT_ERR:
  184. return 0;
  185. }
  186. }
  187. if (datadir == NULL) {
  188. TEST_error("No data directory specified");
  189. return 0;
  190. }
  191. if (inputdir == NULL) {
  192. TEST_error("No input directory specified");
  193. return 0;
  194. }
  195. if (infile != NULL)
  196. ADD_TEST(test_store_open);
  197. ADD_TEST(test_store_search_by_key_fingerprint_fail);
  198. ADD_ALL_TESTS(test_store_get_params, 3);
  199. if (sm2file != NULL)
  200. ADD_TEST(test_store_attach_unregistered_scheme);
  201. return 1;
  202. }