ossl_store_test.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 2020 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_INFILE,
  24. OPT_DATADIR,
  25. OPT_TEST_ENUM
  26. } OPTION_CHOICE;
  27. static const char *infile = NULL;
  28. static const char *datadir = NULL;
  29. static int test_store_open(void)
  30. {
  31. int ret = 0;
  32. OSSL_STORE_CTX *sctx = NULL;
  33. OSSL_STORE_SEARCH *search = NULL;
  34. UI_METHOD *ui_method = NULL;
  35. ret = TEST_ptr(search = OSSL_STORE_SEARCH_by_alias("nothing"))
  36. && TEST_ptr(ui_method= UI_create_method("DummyUI"))
  37. && TEST_ptr(sctx = OSSL_STORE_open_ex(infile, NULL, NULL, ui_method,
  38. NULL, NULL, NULL))
  39. && TEST_false(OSSL_STORE_find(sctx, NULL))
  40. && TEST_true(OSSL_STORE_find(sctx, search));
  41. UI_destroy_method(ui_method);
  42. OSSL_STORE_SEARCH_free(search);
  43. OSSL_STORE_close(sctx);
  44. return ret;
  45. }
  46. static int test_store_search_by_key_fingerprint_fail(void)
  47. {
  48. int ret;
  49. OSSL_STORE_SEARCH *search = NULL;
  50. ret = TEST_ptr_null(search = OSSL_STORE_SEARCH_by_key_fingerprint(
  51. EVP_sha256(), NULL, 0));
  52. OSSL_STORE_SEARCH_free(search);
  53. return ret;
  54. }
  55. static int get_params(const char *uri, const char *type)
  56. {
  57. EVP_PKEY *pkey = NULL;
  58. OSSL_STORE_CTX *ctx = NULL;
  59. OSSL_STORE_INFO *info;
  60. int ret = 0;
  61. ctx = OSSL_STORE_open_ex(uri, NULL, NULL, NULL, NULL, NULL, NULL);
  62. if (!TEST_ptr(ctx))
  63. goto err;
  64. while (!OSSL_STORE_eof(ctx)
  65. && (info = OSSL_STORE_load(ctx)) != NULL
  66. && pkey == NULL) {
  67. if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) {
  68. pkey = OSSL_STORE_INFO_get1_PARAMS(info);
  69. }
  70. OSSL_STORE_INFO_free(info);
  71. info = NULL;
  72. }
  73. if (pkey != NULL)
  74. ret = EVP_PKEY_is_a(pkey, type);
  75. EVP_PKEY_free(pkey);
  76. err:
  77. OSSL_STORE_close(ctx);
  78. return ret;
  79. }
  80. static int test_store_get_params(int idx)
  81. {
  82. const char *type;
  83. char uri[PATH_MAX];
  84. switch(idx) {
  85. #ifndef OPENSSL_NO_DH
  86. case 0:
  87. type = "DH";
  88. break;
  89. case 1:
  90. type = "DHX";
  91. break;
  92. #else
  93. case 0:
  94. case 1:
  95. return 1;
  96. #endif
  97. case 2:
  98. #ifndef OPENSSL_NO_DSA
  99. type = "DSA";
  100. break;
  101. #else
  102. return 1;
  103. #endif
  104. default:
  105. TEST_error("Invalid test index");
  106. return 0;
  107. }
  108. if (!TEST_true(BIO_snprintf(uri, sizeof(uri), "%s/%s-params.pem",
  109. datadir, type)))
  110. return 0;
  111. TEST_info("Testing uri: %s", uri);
  112. if (!TEST_true(get_params(uri, type)))
  113. return 0;
  114. return 1;
  115. }
  116. const OPTIONS *test_get_options(void)
  117. {
  118. static const OPTIONS test_options[] = {
  119. OPT_TEST_OPTIONS_DEFAULT_USAGE,
  120. { "in", OPT_INFILE, '<', },
  121. { "data", OPT_DATADIR, 's' },
  122. { NULL }
  123. };
  124. return test_options;
  125. }
  126. int setup_tests(void)
  127. {
  128. OPTION_CHOICE o;
  129. while ((o = opt_next()) != OPT_EOF) {
  130. switch (o) {
  131. case OPT_INFILE:
  132. infile = opt_arg();
  133. break;
  134. case OPT_DATADIR:
  135. datadir = opt_arg();
  136. break;
  137. case OPT_TEST_CASES:
  138. break;
  139. default:
  140. case OPT_ERR:
  141. return 0;
  142. }
  143. }
  144. if (datadir == NULL) {
  145. TEST_error("No datadir specified");
  146. return 0;
  147. }
  148. ADD_TEST(test_store_open);
  149. ADD_TEST(test_store_search_by_key_fingerprint_fail);
  150. ADD_ALL_TESTS(test_store_get_params, 3);
  151. return 1;
  152. }