1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci#include <stddef.h> 11e1051a39Sopenharmony_ci#include <openssl/crypto.h> 12e1051a39Sopenharmony_ci#include "internal/provider.h" 13e1051a39Sopenharmony_ci#include "testutil.h" 14e1051a39Sopenharmony_ci 15e1051a39Sopenharmony_ciextern OSSL_provider_init_fn PROVIDER_INIT_FUNCTION_NAME; 16e1051a39Sopenharmony_ci 17e1051a39Sopenharmony_cistatic char buf[256]; 18e1051a39Sopenharmony_cistatic OSSL_PARAM greeting_request[] = { 19e1051a39Sopenharmony_ci { "greeting", OSSL_PARAM_UTF8_STRING, buf, sizeof(buf), 0 }, 20e1051a39Sopenharmony_ci { NULL, 0, NULL, 0, 0 } 21e1051a39Sopenharmony_ci}; 22e1051a39Sopenharmony_ci 23e1051a39Sopenharmony_cistatic int test_provider(OSSL_PROVIDER *prov, const char *expected_greeting) 24e1051a39Sopenharmony_ci{ 25e1051a39Sopenharmony_ci const char *greeting = NULL; 26e1051a39Sopenharmony_ci int ret = 0; 27e1051a39Sopenharmony_ci 28e1051a39Sopenharmony_ci ret = 29e1051a39Sopenharmony_ci TEST_true(ossl_provider_activate(prov, 1, 0)) 30e1051a39Sopenharmony_ci && TEST_true(ossl_provider_get_params(prov, greeting_request)) 31e1051a39Sopenharmony_ci && TEST_ptr(greeting = greeting_request[0].data) 32e1051a39Sopenharmony_ci && TEST_size_t_gt(greeting_request[0].data_size, 0) 33e1051a39Sopenharmony_ci && TEST_str_eq(greeting, expected_greeting) 34e1051a39Sopenharmony_ci && TEST_true(ossl_provider_deactivate(prov, 1)); 35e1051a39Sopenharmony_ci 36e1051a39Sopenharmony_ci TEST_info("Got this greeting: %s\n", greeting); 37e1051a39Sopenharmony_ci ossl_provider_free(prov); 38e1051a39Sopenharmony_ci return ret; 39e1051a39Sopenharmony_ci} 40e1051a39Sopenharmony_ci 41e1051a39Sopenharmony_cistatic const char *expected_greeting1(const char *name) 42e1051a39Sopenharmony_ci{ 43e1051a39Sopenharmony_ci static char expected_greeting[256] = ""; 44e1051a39Sopenharmony_ci 45e1051a39Sopenharmony_ci BIO_snprintf(expected_greeting, sizeof(expected_greeting), 46e1051a39Sopenharmony_ci "Hello OpenSSL %.20s, greetings from %s!", 47e1051a39Sopenharmony_ci OPENSSL_VERSION_STR, name); 48e1051a39Sopenharmony_ci 49e1051a39Sopenharmony_ci return expected_greeting; 50e1051a39Sopenharmony_ci} 51e1051a39Sopenharmony_ci 52e1051a39Sopenharmony_cistatic int test_builtin_provider(void) 53e1051a39Sopenharmony_ci{ 54e1051a39Sopenharmony_ci const char *name = "p_test_builtin"; 55e1051a39Sopenharmony_ci OSSL_PROVIDER *prov = NULL; 56e1051a39Sopenharmony_ci int ret; 57e1051a39Sopenharmony_ci 58e1051a39Sopenharmony_ci /* 59e1051a39Sopenharmony_ci * We set properties that we know the providers we are using don't have. 60e1051a39Sopenharmony_ci * This should mean that the p_test provider will fail any fetches - which 61e1051a39Sopenharmony_ci * is something we test inside the provider. 62e1051a39Sopenharmony_ci */ 63e1051a39Sopenharmony_ci EVP_set_default_properties(NULL, "fips=yes"); 64e1051a39Sopenharmony_ci 65e1051a39Sopenharmony_ci ret = 66e1051a39Sopenharmony_ci TEST_ptr(prov = 67e1051a39Sopenharmony_ci ossl_provider_new(NULL, name, PROVIDER_INIT_FUNCTION_NAME, 0)) 68e1051a39Sopenharmony_ci && test_provider(prov, expected_greeting1(name)); 69e1051a39Sopenharmony_ci 70e1051a39Sopenharmony_ci EVP_set_default_properties(NULL, ""); 71e1051a39Sopenharmony_ci 72e1051a39Sopenharmony_ci return ret; 73e1051a39Sopenharmony_ci} 74e1051a39Sopenharmony_ci 75e1051a39Sopenharmony_ci#ifndef NO_PROVIDER_MODULE 76e1051a39Sopenharmony_cistatic int test_loaded_provider(void) 77e1051a39Sopenharmony_ci{ 78e1051a39Sopenharmony_ci const char *name = "p_test"; 79e1051a39Sopenharmony_ci OSSL_PROVIDER *prov = NULL; 80e1051a39Sopenharmony_ci 81e1051a39Sopenharmony_ci return 82e1051a39Sopenharmony_ci TEST_ptr(prov = ossl_provider_new(NULL, name, NULL, 0)) 83e1051a39Sopenharmony_ci && test_provider(prov, expected_greeting1(name)); 84e1051a39Sopenharmony_ci} 85e1051a39Sopenharmony_ci 86e1051a39Sopenharmony_cistatic int test_configured_provider(void) 87e1051a39Sopenharmony_ci{ 88e1051a39Sopenharmony_ci const char *name = "p_test_configured"; 89e1051a39Sopenharmony_ci OSSL_PROVIDER *prov = NULL; 90e1051a39Sopenharmony_ci /* This MUST match the config file */ 91e1051a39Sopenharmony_ci const char *expected_greeting = 92e1051a39Sopenharmony_ci "Hello OpenSSL, greetings from Test Provider"; 93e1051a39Sopenharmony_ci 94e1051a39Sopenharmony_ci return 95e1051a39Sopenharmony_ci TEST_ptr(prov = ossl_provider_find(NULL, name, 0)) 96e1051a39Sopenharmony_ci && test_provider(prov, expected_greeting); 97e1051a39Sopenharmony_ci} 98e1051a39Sopenharmony_ci#endif 99e1051a39Sopenharmony_ci 100e1051a39Sopenharmony_cistatic int test_cache_flushes(void) 101e1051a39Sopenharmony_ci{ 102e1051a39Sopenharmony_ci OSSL_LIB_CTX *ctx; 103e1051a39Sopenharmony_ci OSSL_PROVIDER *prov = NULL; 104e1051a39Sopenharmony_ci EVP_MD *md = NULL; 105e1051a39Sopenharmony_ci int ret = 0; 106e1051a39Sopenharmony_ci 107e1051a39Sopenharmony_ci if (!TEST_ptr(ctx = OSSL_LIB_CTX_new()) 108e1051a39Sopenharmony_ci || !TEST_ptr(prov = OSSL_PROVIDER_load(ctx, "default")) 109e1051a39Sopenharmony_ci || !TEST_true(OSSL_PROVIDER_available(ctx, "default")) 110e1051a39Sopenharmony_ci || !TEST_ptr(md = EVP_MD_fetch(ctx, "SHA256", NULL))) 111e1051a39Sopenharmony_ci goto err; 112e1051a39Sopenharmony_ci EVP_MD_free(md); 113e1051a39Sopenharmony_ci md = NULL; 114e1051a39Sopenharmony_ci OSSL_PROVIDER_unload(prov); 115e1051a39Sopenharmony_ci prov = NULL; 116e1051a39Sopenharmony_ci 117e1051a39Sopenharmony_ci if (!TEST_false(OSSL_PROVIDER_available(ctx, "default"))) 118e1051a39Sopenharmony_ci goto err; 119e1051a39Sopenharmony_ci 120e1051a39Sopenharmony_ci if (!TEST_ptr_null(md = EVP_MD_fetch(ctx, "SHA256", NULL))) { 121e1051a39Sopenharmony_ci const char *provname = OSSL_PROVIDER_get0_name(EVP_MD_get0_provider(md)); 122e1051a39Sopenharmony_ci 123e1051a39Sopenharmony_ci if (OSSL_PROVIDER_available(NULL, provname)) 124e1051a39Sopenharmony_ci TEST_info("%s provider is available\n", provname); 125e1051a39Sopenharmony_ci else 126e1051a39Sopenharmony_ci TEST_info("%s provider is not available\n", provname); 127e1051a39Sopenharmony_ci } 128e1051a39Sopenharmony_ci 129e1051a39Sopenharmony_ci ret = 1; 130e1051a39Sopenharmony_ci err: 131e1051a39Sopenharmony_ci OSSL_PROVIDER_unload(prov); 132e1051a39Sopenharmony_ci EVP_MD_free(md); 133e1051a39Sopenharmony_ci OSSL_LIB_CTX_free(ctx); 134e1051a39Sopenharmony_ci return ret; 135e1051a39Sopenharmony_ci} 136e1051a39Sopenharmony_ci 137e1051a39Sopenharmony_ciint setup_tests(void) 138e1051a39Sopenharmony_ci{ 139e1051a39Sopenharmony_ci ADD_TEST(test_builtin_provider); 140e1051a39Sopenharmony_ci#ifndef NO_PROVIDER_MODULE 141e1051a39Sopenharmony_ci ADD_TEST(test_loaded_provider); 142e1051a39Sopenharmony_ci ADD_TEST(test_configured_provider); 143e1051a39Sopenharmony_ci#endif 144e1051a39Sopenharmony_ci ADD_TEST(test_cache_flushes); 145e1051a39Sopenharmony_ci return 1; 146e1051a39Sopenharmony_ci} 147e1051a39Sopenharmony_ci 148