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