2 * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include <openssl/provider.h>
13 #include <openssl/params.h>
14 #include <openssl/core_names.h>
15 #include <openssl/self_test.h>
16 #include <openssl/evp.h>
19 typedef enum OPTION_choice {
27 struct self_test_arg {
31 static OSSL_LIB_CTX *libctx = NULL;
32 static char *provider_name = NULL;
33 static struct self_test_arg self_test_args = { 0 };
35 const OPTIONS *test_get_options(void)
37 static const OPTIONS test_options[] = {
38 OPT_TEST_OPTIONS_DEFAULT_USAGE,
39 { "provider_name", OPT_PROVIDER_NAME, 's',
40 "The name of the provider to load" },
41 { "config", OPT_CONFIG_FILE, '<',
42 "The configuration file to use for the libctx" },
48 static int self_test_events(const OSSL_PARAM params[], void *arg,
49 const char *title, int corrupt)
51 struct self_test_arg *args = arg;
52 const OSSL_PARAM *p = NULL;
53 const char *phase = NULL, *type = NULL, *desc = NULL;
57 BIO_printf(bio_out, "\n%s\n", title);
60 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_PHASE);
61 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
63 phase = (const char *)p->data;
65 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_DESC);
66 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
68 desc = (const char *)p->data;
70 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_TYPE);
71 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
73 type = (const char *)p->data;
75 if (strcmp(phase, OSSL_SELF_TEST_PHASE_START) == 0)
76 BIO_printf(bio_out, "%s : (%s) : ", desc, type);
77 else if (strcmp(phase, OSSL_SELF_TEST_PHASE_PASS) == 0
78 || strcmp(phase, OSSL_SELF_TEST_PHASE_FAIL) == 0)
79 BIO_printf(bio_out, "%s\n", phase);
81 * The self test code will internally corrupt the KAT test result if an
82 * error is returned during the corrupt phase.
84 if (corrupt && strcmp(phase, OSSL_SELF_TEST_PHASE_CORRUPT) == 0)
91 static int self_test_on_demand_fail(const OSSL_PARAM params[], void *arg)
93 return self_test_events(params, arg, "On Demand Failure", 1);
96 static int self_test_on_demand(const OSSL_PARAM params[], void *arg)
98 return self_test_events(params, arg, "On Demand", 0);
101 static int self_test_on_load(const OSSL_PARAM params[], void *arg)
103 return self_test_events(params, arg, "On Loading", 0);
106 static int get_provider_params(const OSSL_PROVIDER *prov)
109 OSSL_PARAM params[5];
110 char *name, *version, *buildinfo;
112 const OSSL_PARAM *gettable, *p;
114 if (!TEST_ptr(gettable = OSSL_PROVIDER_gettable_params(prov))
115 || !TEST_ptr(p = OSSL_PARAM_locate_const(gettable, OSSL_PROV_PARAM_NAME))
116 || !TEST_ptr(p = OSSL_PARAM_locate_const(gettable, OSSL_PROV_PARAM_VERSION))
117 || !TEST_ptr(p = OSSL_PARAM_locate_const(gettable, OSSL_PROV_PARAM_STATUS))
118 || !TEST_ptr(p = OSSL_PARAM_locate_const(gettable, OSSL_PROV_PARAM_BUILDINFO)))
121 params[0] = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_NAME, &name, 0);
122 params[1] = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_VERSION,
124 params[2] = OSSL_PARAM_construct_int(OSSL_PROV_PARAM_STATUS, &status);
125 params[3] = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_BUILDINFO,
127 params[4] = OSSL_PARAM_construct_end();
128 OSSL_PARAM_set_all_unmodified(params);
129 if (!TEST_true(OSSL_PROVIDER_get_params(prov, params)))
131 if (!TEST_true(OSSL_PARAM_modified(params + 0))
132 || !TEST_true(OSSL_PARAM_modified(params + 1))
133 || !TEST_true(OSSL_PARAM_modified(params + 2))
134 || !TEST_true(OSSL_PARAM_modified(params + 3))
135 || !TEST_true(status == 1))
143 static int test_provider_status(void)
146 unsigned int status = 0;
147 OSSL_PROVIDER *prov = NULL;
148 OSSL_PARAM params[2];
149 EVP_MD *fetch = NULL;
151 if (!TEST_ptr(prov = OSSL_PROVIDER_load(libctx, provider_name)))
153 if (!get_provider_params(prov))
156 /* Test that the provider status is ok */
157 params[0] = OSSL_PARAM_construct_uint(OSSL_PROV_PARAM_STATUS, &status);
158 params[1] = OSSL_PARAM_construct_end();
159 if (!TEST_true(OSSL_PROVIDER_get_params(prov, params))
160 || !TEST_true(status == 1))
162 if (!TEST_ptr(fetch = EVP_MD_fetch(libctx, "SHA256", NULL)))
167 /* Test that the provider self test is ok */
168 self_test_args.count = 0;
169 OSSL_SELF_TEST_set_callback(libctx, self_test_on_demand, &self_test_args);
170 if (!TEST_true(OSSL_PROVIDER_self_test(prov)))
173 /* Setup a callback that corrupts the self tests and causes status failures */
174 self_test_args.count = 0;
175 OSSL_SELF_TEST_set_callback(libctx, self_test_on_demand_fail, &self_test_args);
176 if (!TEST_false(OSSL_PROVIDER_self_test(prov)))
178 if (!TEST_true(OSSL_PROVIDER_get_params(prov, params))
179 || !TEST_uint_eq(status, 0))
181 if (!TEST_ptr_null(fetch = EVP_MD_fetch(libctx, "SHA256", NULL)))
187 OSSL_PROVIDER_unload(prov);
191 static int test_provider_gettable_params(void)
196 if (!TEST_ptr(prov = OSSL_PROVIDER_load(libctx, provider_name)))
198 ret = get_provider_params(prov);
199 OSSL_PROVIDER_unload(prov);
203 int setup_tests(void)
206 char *config_file = NULL;
208 while ((o = opt_next()) != OPT_EOF) {
210 case OPT_CONFIG_FILE:
211 config_file = opt_arg();
213 case OPT_PROVIDER_NAME:
214 provider_name = opt_arg();
224 libctx = OSSL_LIB_CTX_new();
228 if (strcmp(provider_name, "fips") == 0) {
229 self_test_args.count = 0;
230 OSSL_SELF_TEST_set_callback(libctx, self_test_on_load, &self_test_args);
231 if (!OSSL_LIB_CTX_load_config(libctx, config_file)) {
232 opt_printf_stderr("Failed to load config\n");
235 ADD_TEST(test_provider_status);
237 ADD_TEST(test_provider_gettable_params);
242 void cleanup_tests(void)
244 OSSL_LIB_CTX_free(libctx);