xref: /netbsd-src/crypto/external/bsd/openssl/dist/test/provider_fallback_test.c (revision 97e3c58506797315d86c0608cba9d3f55de0c735)
1b0d17251Schristos /*
2*97e3c585Schristos  * Copyright 2020-2024 The OpenSSL Project Authors. All Rights Reserved.
3b0d17251Schristos  *
4b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5b0d17251Schristos  * this file except in compliance with the License.  You can obtain a copy
6b0d17251Schristos  * in the file LICENSE in the source distribution or at
7b0d17251Schristos  * https://www.openssl.org/source/license.html
8b0d17251Schristos  */
9b0d17251Schristos 
10b0d17251Schristos #include <stddef.h>
11b0d17251Schristos #include <openssl/provider.h>
12b0d17251Schristos #include <openssl/evp.h>
13b0d17251Schristos #include "testutil.h"
14b0d17251Schristos 
15b0d17251Schristos static int test_provider(OSSL_LIB_CTX *ctx)
16b0d17251Schristos {
17b0d17251Schristos     EVP_KEYMGMT *rsameth = NULL;
18b0d17251Schristos     const OSSL_PROVIDER *prov = NULL;
19b0d17251Schristos     int ok;
20b0d17251Schristos 
21b0d17251Schristos     ok = TEST_true(OSSL_PROVIDER_available(ctx, "default"))
22b0d17251Schristos         && TEST_ptr(rsameth = EVP_KEYMGMT_fetch(ctx, "RSA", NULL))
23b0d17251Schristos         && TEST_ptr(prov = EVP_KEYMGMT_get0_provider(rsameth))
24b0d17251Schristos         && TEST_str_eq(OSSL_PROVIDER_get0_name(prov), "default");
25b0d17251Schristos 
26b0d17251Schristos     EVP_KEYMGMT_free(rsameth);
27b0d17251Schristos     return ok;
28b0d17251Schristos }
29b0d17251Schristos 
30b0d17251Schristos static int test_fallback_provider(void)
31b0d17251Schristos {
32b0d17251Schristos     return test_provider(NULL);
33b0d17251Schristos }
34b0d17251Schristos 
35b0d17251Schristos static int test_explicit_provider(void)
36b0d17251Schristos {
37b0d17251Schristos     OSSL_LIB_CTX *ctx = NULL;
38b0d17251Schristos     OSSL_PROVIDER *prov = NULL;
39b0d17251Schristos     int ok;
40b0d17251Schristos 
41b0d17251Schristos     ok = TEST_ptr(ctx = OSSL_LIB_CTX_new())
42*97e3c585Schristos         && TEST_ptr(prov = OSSL_PROVIDER_load(ctx, "default"));
43*97e3c585Schristos 
44*97e3c585Schristos     if (ok) {
45*97e3c585Schristos         ok = test_provider(ctx);
46*97e3c585Schristos         if (ok)
47*97e3c585Schristos             ok = TEST_true(OSSL_PROVIDER_unload(prov));
48*97e3c585Schristos         else
49*97e3c585Schristos             OSSL_PROVIDER_unload(prov);
50*97e3c585Schristos     }
51b0d17251Schristos 
52b0d17251Schristos     OSSL_LIB_CTX_free(ctx);
53b0d17251Schristos     return ok;
54b0d17251Schristos }
55b0d17251Schristos 
56b0d17251Schristos 
57b0d17251Schristos int setup_tests(void)
58b0d17251Schristos {
59b0d17251Schristos     ADD_TEST(test_fallback_provider);
60b0d17251Schristos     ADD_TEST(test_explicit_provider);
61b0d17251Schristos     return 1;
62b0d17251Schristos }
63b0d17251Schristos 
64