ossl_store_test.c 5.9 KB

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