Rename OPENSSL_CTX prefix to OSSL_LIB_CTX
[openssl.git] / test / provider_status_test.c
1 /*
2  * Copyright 2020 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 <string.h>
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>
17 #include "testutil.h"
18
19 typedef enum OPTION_choice {
20     OPT_ERR = -1,
21     OPT_EOF = 0,
22     OPT_PROVIDER_NAME,
23     OPT_CONFIG_FILE,
24     OPT_TEST_ENUM
25 } OPTION_CHOICE;
26
27 struct self_test_arg {
28     int count;
29 };
30
31 static OSSL_LIB_CTX *libctx = NULL;
32 static char *provider_name = NULL;
33 static struct self_test_arg self_test_args = { 0 };
34
35 const OPTIONS *test_get_options(void)
36 {
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" },
43         { NULL }
44     };
45     return test_options;
46 }
47
48 static int self_test_events(const OSSL_PARAM params[], void *arg,
49                             const char *title, int corrupt)
50 {
51     struct self_test_arg *args = arg;
52     const OSSL_PARAM *p = NULL;
53     const char *phase = NULL, *type = NULL, *desc = NULL;
54     int ret = 0;
55
56     if (args->count == 0)
57         BIO_printf(bio_out, "\n%s\n", title);
58     args->count++;
59
60     p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_PHASE);
61     if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
62         goto err;
63     phase = (const char *)p->data;
64
65     p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_DESC);
66     if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
67         goto err;
68     desc = (const char *)p->data;
69
70     p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_TYPE);
71     if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
72         goto err;
73     type = (const char *)p->data;
74
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);
80     /*
81      * The self test code will internally corrupt the KAT test result if an
82      * error is returned during the corrupt phase.
83      */
84     if (corrupt && strcmp(phase, OSSL_SELF_TEST_PHASE_CORRUPT) == 0)
85         goto err;
86     ret = 1;
87 err:
88     return ret;
89 }
90
91 static int self_test_on_demand_fail(const OSSL_PARAM params[], void *arg)
92 {
93     return self_test_events(params, arg, "On Demand Failure", 1);
94 }
95
96 static int self_test_on_demand(const OSSL_PARAM params[], void *arg)
97 {
98     return self_test_events(params, arg, "On Demand", 0);
99 }
100
101 static int self_test_on_load(const OSSL_PARAM params[], void *arg)
102 {
103     return self_test_events(params, arg, "On Loading", 0);
104 }
105
106 static int test_provider_status(void)
107 {
108     int ret = 0;
109     unsigned int status = 0;
110     OSSL_PROVIDER *prov = NULL;
111     OSSL_PARAM params[2];
112     EVP_MD *fetch = NULL;
113
114     if (!TEST_ptr(prov = OSSL_PROVIDER_load(libctx, provider_name)))
115         goto err;
116
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))
122         goto err;
123     if (!TEST_ptr(fetch = EVP_MD_fetch(libctx, "SHA256", NULL)))
124         goto err;
125     EVP_MD_free(fetch);
126     fetch = NULL;
127
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)))
132         goto err;
133
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)))
138         goto err;
139     if (!TEST_true(OSSL_PROVIDER_get_params(prov, params))
140         || !TEST_uint_eq(status, 0))
141         goto err;
142     if (!TEST_ptr_null(fetch = EVP_MD_fetch(libctx, "SHA256", NULL)))
143         goto err;
144
145     ret = 1;
146 err:
147     EVP_MD_free(fetch);
148     OSSL_PROVIDER_unload(prov);
149     return ret;
150 }
151
152 int setup_tests(void)
153 {
154     OPTION_CHOICE o;
155     char *config_file = NULL;
156
157     while ((o = opt_next()) != OPT_EOF) {
158         switch (o) {
159         case OPT_CONFIG_FILE:
160             config_file = opt_arg();
161             break;
162         case OPT_PROVIDER_NAME:
163             provider_name = opt_arg();
164             break;
165         case OPT_TEST_CASES:
166            break;
167         default:
168         case OPT_ERR:
169             return 0;
170         }
171     }
172
173     libctx = OSSL_LIB_CTX_new();
174     if (libctx == NULL)
175         return 0;
176     self_test_args.count = 0;
177     OSSL_SELF_TEST_set_callback(libctx, self_test_on_load, &self_test_args);
178
179     if (!OSSL_LIB_CTX_load_config(libctx, config_file)) {
180         opt_printf_stderr("Failed to load config\n");
181         return 0;
182     }
183     ADD_TEST(test_provider_status);
184     return 1;
185 }