2 * Copyright 2020 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 test_provider_status(void)
109 unsigned int status = 0;
110 OSSL_PROVIDER *prov = NULL;
111 OSSL_PARAM params[2];
112 EVP_MD *fetch = NULL;
114 if (!TEST_ptr(prov = OSSL_PROVIDER_load(libctx, provider_name)))
117 /* Test that the provider status is ok */
118 params[0] = OSSL_PARAM_construct_uint(OSSL_PROV_PARAM_STATUS, &status);
119 params[1] = OSSL_PARAM_construct_end();
120 if (!TEST_true(OSSL_PROVIDER_get_params(prov, params))
121 || !TEST_true(status == 1))
123 if (!TEST_ptr(fetch = EVP_MD_fetch(libctx, "SHA256", NULL)))
128 /* Test that the provider self test is ok */
129 self_test_args.count = 0;
130 OSSL_SELF_TEST_set_callback(libctx, self_test_on_demand, &self_test_args);
131 if (!TEST_true(OSSL_PROVIDER_self_test(prov)))
134 /* Setup a callback that corrupts the self tests and causes status failures */
135 self_test_args.count = 0;
136 OSSL_SELF_TEST_set_callback(libctx, self_test_on_demand_fail, &self_test_args);
137 if (!TEST_false(OSSL_PROVIDER_self_test(prov)))
139 if (!TEST_true(OSSL_PROVIDER_get_params(prov, params))
140 || !TEST_uint_eq(status, 0))
142 if (!TEST_ptr_null(fetch = EVP_MD_fetch(libctx, "SHA256", NULL)))
148 OSSL_PROVIDER_unload(prov);
152 int setup_tests(void)
155 char *config_file = NULL;
157 while ((o = opt_next()) != OPT_EOF) {
159 case OPT_CONFIG_FILE:
160 config_file = opt_arg();
162 case OPT_PROVIDER_NAME:
163 provider_name = opt_arg();
173 libctx = OSSL_LIB_CTX_new();
176 self_test_args.count = 0;
177 OSSL_SELF_TEST_set_callback(libctx, self_test_on_load, &self_test_args);
179 if (!OSSL_LIB_CTX_load_config(libctx, config_file)) {
180 opt_printf_stderr("Failed to load config\n");
183 ADD_TEST(test_provider_status);