2
0

provider_internal_test.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 2019-2023 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 <stddef.h>
  10. #include <openssl/crypto.h>
  11. #include "internal/provider.h"
  12. #include "testutil.h"
  13. extern OSSL_provider_init_fn PROVIDER_INIT_FUNCTION_NAME;
  14. static char buf[256];
  15. static OSSL_PARAM greeting_request[] = {
  16. { "greeting", OSSL_PARAM_UTF8_STRING, buf, sizeof(buf), 0 },
  17. { NULL, 0, NULL, 0, 0 }
  18. };
  19. static int test_provider(OSSL_PROVIDER *prov, const char *expected_greeting)
  20. {
  21. const char *greeting = NULL;
  22. int ret = 0;
  23. ret =
  24. TEST_true(ossl_provider_activate(prov, 1, 0))
  25. && TEST_true(ossl_provider_get_params(prov, greeting_request))
  26. && TEST_ptr(greeting = greeting_request[0].data)
  27. && TEST_size_t_gt(greeting_request[0].data_size, 0)
  28. && TEST_str_eq(greeting, expected_greeting)
  29. && TEST_true(ossl_provider_deactivate(prov, 1));
  30. TEST_info("Got this greeting: %s\n", greeting);
  31. ossl_provider_free(prov);
  32. return ret;
  33. }
  34. static const char *expected_greeting1(const char *name)
  35. {
  36. static char expected_greeting[256] = "";
  37. BIO_snprintf(expected_greeting, sizeof(expected_greeting),
  38. "Hello OpenSSL %.20s, greetings from %s!",
  39. OPENSSL_VERSION_STR, name);
  40. return expected_greeting;
  41. }
  42. static int test_builtin_provider(void)
  43. {
  44. const char *name = "p_test_builtin";
  45. OSSL_PROVIDER *prov = NULL;
  46. int ret;
  47. /*
  48. * We set properties that we know the providers we are using don't have.
  49. * This should mean that the p_test provider will fail any fetches - which
  50. * is something we test inside the provider.
  51. */
  52. EVP_set_default_properties(NULL, "fips=yes");
  53. ret =
  54. TEST_ptr(prov =
  55. ossl_provider_new(NULL, name, PROVIDER_INIT_FUNCTION_NAME, NULL, 0))
  56. && test_provider(prov, expected_greeting1(name));
  57. EVP_set_default_properties(NULL, "");
  58. return ret;
  59. }
  60. #ifndef NO_PROVIDER_MODULE
  61. static int test_loaded_provider(void)
  62. {
  63. const char *name = "p_test";
  64. OSSL_PROVIDER *prov = NULL;
  65. return
  66. TEST_ptr(prov = ossl_provider_new(NULL, name, NULL, NULL, 0))
  67. && test_provider(prov, expected_greeting1(name));
  68. }
  69. # ifndef OPENSSL_NO_AUTOLOAD_CONFIG
  70. static int test_configured_provider(void)
  71. {
  72. const char *name = "p_test_configured";
  73. OSSL_PROVIDER *prov = NULL;
  74. /* This MUST match the config file */
  75. const char *expected_greeting =
  76. "Hello OpenSSL, greetings from Test Provider";
  77. return
  78. TEST_ptr(prov = ossl_provider_find(NULL, name, 0))
  79. && test_provider(prov, expected_greeting);
  80. }
  81. # endif
  82. #endif
  83. static int test_cache_flushes(void)
  84. {
  85. OSSL_LIB_CTX *ctx;
  86. OSSL_PROVIDER *prov = NULL;
  87. EVP_MD *md = NULL;
  88. int ret = 0;
  89. if (!TEST_ptr(ctx = OSSL_LIB_CTX_new())
  90. || !TEST_ptr(prov = OSSL_PROVIDER_load(ctx, "default"))
  91. || !TEST_true(OSSL_PROVIDER_available(ctx, "default"))
  92. || !TEST_ptr(md = EVP_MD_fetch(ctx, "SHA256", NULL)))
  93. goto err;
  94. EVP_MD_free(md);
  95. md = NULL;
  96. OSSL_PROVIDER_unload(prov);
  97. prov = NULL;
  98. if (!TEST_false(OSSL_PROVIDER_available(ctx, "default")))
  99. goto err;
  100. if (!TEST_ptr_null(md = EVP_MD_fetch(ctx, "SHA256", NULL))) {
  101. const char *provname = OSSL_PROVIDER_get0_name(EVP_MD_get0_provider(md));
  102. if (OSSL_PROVIDER_available(NULL, provname))
  103. TEST_info("%s provider is available\n", provname);
  104. else
  105. TEST_info("%s provider is not available\n", provname);
  106. }
  107. ret = 1;
  108. err:
  109. OSSL_PROVIDER_unload(prov);
  110. EVP_MD_free(md);
  111. OSSL_LIB_CTX_free(ctx);
  112. return ret;
  113. }
  114. int setup_tests(void)
  115. {
  116. ADD_TEST(test_builtin_provider);
  117. #ifndef NO_PROVIDER_MODULE
  118. ADD_TEST(test_loaded_provider);
  119. # ifndef OPENSSL_NO_AUTOLOAD_CONFIG
  120. ADD_TEST(test_configured_provider);
  121. # endif
  122. #endif
  123. ADD_TEST(test_cache_flushes);
  124. return 1;
  125. }