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