2
0

provider_internal_test.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2019-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 <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, 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, 0))
  67. && test_provider(prov, expected_greeting1(name));
  68. }
  69. static int test_configured_provider(void)
  70. {
  71. const char *name = "p_test_configured";
  72. OSSL_PROVIDER *prov = NULL;
  73. /* This MUST match the config file */
  74. const char *expected_greeting =
  75. "Hello OpenSSL, greetings from Test Provider";
  76. return
  77. TEST_ptr(prov = ossl_provider_find(NULL, name, 0))
  78. && test_provider(prov, expected_greeting);
  79. }
  80. #endif
  81. static int test_cache_flushes(void)
  82. {
  83. OSSL_LIB_CTX *ctx;
  84. OSSL_PROVIDER *prov = NULL;
  85. EVP_MD *md = NULL;
  86. int ret = 0;
  87. if (!TEST_ptr(ctx = OSSL_LIB_CTX_new())
  88. || !TEST_ptr(prov = OSSL_PROVIDER_load(ctx, "default"))
  89. || !TEST_true(OSSL_PROVIDER_available(ctx, "default"))
  90. || !TEST_ptr(md = EVP_MD_fetch(ctx, "SHA256", NULL)))
  91. goto err;
  92. EVP_MD_free(md);
  93. md = NULL;
  94. OSSL_PROVIDER_unload(prov);
  95. prov = NULL;
  96. if (!TEST_false(OSSL_PROVIDER_available(ctx, "default")))
  97. goto err;
  98. if (!TEST_ptr_null(md = EVP_MD_fetch(ctx, "SHA256", NULL))) {
  99. const char *provname = OSSL_PROVIDER_get0_name(EVP_MD_get0_provider(md));
  100. if (OSSL_PROVIDER_available(NULL, provname))
  101. TEST_info("%s provider is available\n", provname);
  102. else
  103. TEST_info("%s provider is not available\n", provname);
  104. }
  105. ret = 1;
  106. err:
  107. OSSL_PROVIDER_unload(prov);
  108. EVP_MD_free(md);
  109. OSSL_LIB_CTX_free(ctx);
  110. return ret;
  111. }
  112. int setup_tests(void)
  113. {
  114. ADD_TEST(test_builtin_provider);
  115. #ifndef NO_PROVIDER_MODULE
  116. ADD_TEST(test_loaded_provider);
  117. ADD_TEST(test_configured_provider);
  118. #endif
  119. ADD_TEST(test_cache_flushes);
  120. return 1;
  121. }