1 | /*
|
---|
2 | * Copyright 2019-2023 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 <string.h>
|
---|
11 | #include <openssl/evp.h>
|
---|
12 | #include <openssl/err.h>
|
---|
13 | #include <openssl/provider.h>
|
---|
14 | #include <openssl/params.h>
|
---|
15 | #include <openssl/fips_names.h>
|
---|
16 | #include <openssl/core_names.h>
|
---|
17 | #include <openssl/self_test.h>
|
---|
18 | #include <openssl/fipskey.h>
|
---|
19 | #include "apps.h"
|
---|
20 | #include "progs.h"
|
---|
21 |
|
---|
22 | #define BUFSIZE 4096
|
---|
23 |
|
---|
24 | /* Configuration file values */
|
---|
25 | #define VERSION_KEY "version"
|
---|
26 | #define VERSION_VAL "1"
|
---|
27 | #define INSTALL_STATUS_VAL "INSTALL_SELF_TEST_KATS_RUN"
|
---|
28 |
|
---|
29 | static OSSL_CALLBACK self_test_events;
|
---|
30 | static char *self_test_corrupt_desc = NULL;
|
---|
31 | static char *self_test_corrupt_type = NULL;
|
---|
32 | static int self_test_log = 1;
|
---|
33 | static int quiet = 0;
|
---|
34 |
|
---|
35 | typedef enum OPTION_choice {
|
---|
36 | OPT_COMMON,
|
---|
37 | OPT_IN, OPT_OUT, OPT_MODULE,
|
---|
38 | OPT_PROV_NAME, OPT_SECTION_NAME, OPT_MAC_NAME, OPT_MACOPT, OPT_VERIFY,
|
---|
39 | OPT_NO_LOG, OPT_CORRUPT_DESC, OPT_CORRUPT_TYPE, OPT_QUIET, OPT_CONFIG,
|
---|
40 | OPT_NO_CONDITIONAL_ERRORS,
|
---|
41 | OPT_NO_SECURITY_CHECKS,
|
---|
42 | OPT_TLS_PRF_EMS_CHECK,
|
---|
43 | OPT_SELF_TEST_ONLOAD, OPT_SELF_TEST_ONINSTALL
|
---|
44 | } OPTION_CHOICE;
|
---|
45 |
|
---|
46 | const OPTIONS fipsinstall_options[] = {
|
---|
47 | OPT_SECTION("General"),
|
---|
48 | {"help", OPT_HELP, '-', "Display this summary"},
|
---|
49 | {"verify", OPT_VERIFY, '-',
|
---|
50 | "Verify a config file instead of generating one"},
|
---|
51 | {"module", OPT_MODULE, '<', "File name of the provider module"},
|
---|
52 | {"provider_name", OPT_PROV_NAME, 's', "FIPS provider name"},
|
---|
53 | {"section_name", OPT_SECTION_NAME, 's',
|
---|
54 | "FIPS Provider config section name (optional)"},
|
---|
55 | {"no_conditional_errors", OPT_NO_CONDITIONAL_ERRORS, '-',
|
---|
56 | "Disable the ability of the fips module to enter an error state if"
|
---|
57 | " any conditional self tests fail"},
|
---|
58 | {"no_security_checks", OPT_NO_SECURITY_CHECKS, '-',
|
---|
59 | "Disable the run-time FIPS security checks in the module"},
|
---|
60 | {"self_test_onload", OPT_SELF_TEST_ONLOAD, '-',
|
---|
61 | "Forces self tests to always run on module load"},
|
---|
62 | {"self_test_oninstall", OPT_SELF_TEST_ONINSTALL, '-',
|
---|
63 | "Forces self tests to run once on module installation"},
|
---|
64 | {"ems_check", OPT_TLS_PRF_EMS_CHECK, '-',
|
---|
65 | "Enable the run-time FIPS check for EMS during TLS1_PRF"},
|
---|
66 | OPT_SECTION("Input"),
|
---|
67 | {"in", OPT_IN, '<', "Input config file, used when verifying"},
|
---|
68 |
|
---|
69 | OPT_SECTION("Output"),
|
---|
70 | {"out", OPT_OUT, '>', "Output config file, used when generating"},
|
---|
71 | {"mac_name", OPT_MAC_NAME, 's', "MAC name"},
|
---|
72 | {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form. "
|
---|
73 | "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
|
---|
74 | {"noout", OPT_NO_LOG, '-', "Disable logging of self test events"},
|
---|
75 | {"corrupt_desc", OPT_CORRUPT_DESC, 's', "Corrupt a self test by description"},
|
---|
76 | {"corrupt_type", OPT_CORRUPT_TYPE, 's', "Corrupt a self test by type"},
|
---|
77 | {"config", OPT_CONFIG, '<', "The parent config to verify"},
|
---|
78 | {"quiet", OPT_QUIET, '-', "No messages, just exit status"},
|
---|
79 | {NULL}
|
---|
80 | };
|
---|
81 |
|
---|
82 | static int do_mac(EVP_MAC_CTX *ctx, unsigned char *tmp, BIO *in,
|
---|
83 | unsigned char *out, size_t *out_len)
|
---|
84 | {
|
---|
85 | int ret = 0;
|
---|
86 | int i;
|
---|
87 | size_t outsz = *out_len;
|
---|
88 |
|
---|
89 | if (!EVP_MAC_init(ctx, NULL, 0, NULL))
|
---|
90 | goto err;
|
---|
91 | if (EVP_MAC_CTX_get_mac_size(ctx) > outsz)
|
---|
92 | goto end;
|
---|
93 | while ((i = BIO_read(in, (char *)tmp, BUFSIZE)) != 0) {
|
---|
94 | if (i < 0 || !EVP_MAC_update(ctx, tmp, i))
|
---|
95 | goto err;
|
---|
96 | }
|
---|
97 | end:
|
---|
98 | if (!EVP_MAC_final(ctx, out, out_len, outsz))
|
---|
99 | goto err;
|
---|
100 | ret = 1;
|
---|
101 | err:
|
---|
102 | return ret;
|
---|
103 | }
|
---|
104 |
|
---|
105 | static int load_fips_prov_and_run_self_test(const char *prov_name)
|
---|
106 | {
|
---|
107 | int ret = 0;
|
---|
108 | OSSL_PROVIDER *prov = NULL;
|
---|
109 | OSSL_PARAM params[4], *p = params;
|
---|
110 | char *name = "", *vers = "", *build = "";
|
---|
111 |
|
---|
112 | prov = OSSL_PROVIDER_load(NULL, prov_name);
|
---|
113 | if (prov == NULL) {
|
---|
114 | BIO_printf(bio_err, "Failed to load FIPS module\n");
|
---|
115 | goto end;
|
---|
116 | }
|
---|
117 | if (!quiet) {
|
---|
118 | *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_NAME,
|
---|
119 | &name, sizeof(name));
|
---|
120 | *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_VERSION,
|
---|
121 | &vers, sizeof(vers));
|
---|
122 | *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_BUILDINFO,
|
---|
123 | &build, sizeof(build));
|
---|
124 | *p = OSSL_PARAM_construct_end();
|
---|
125 | if (!OSSL_PROVIDER_get_params(prov, params)) {
|
---|
126 | BIO_printf(bio_err, "Failed to query FIPS module parameters\n");
|
---|
127 | goto end;
|
---|
128 | }
|
---|
129 | if (OSSL_PARAM_modified(params))
|
---|
130 | BIO_printf(bio_err, "\t%-10s\t%s\n", "name:", name);
|
---|
131 | if (OSSL_PARAM_modified(params + 1))
|
---|
132 | BIO_printf(bio_err, "\t%-10s\t%s\n", "version:", vers);
|
---|
133 | if (OSSL_PARAM_modified(params + 2))
|
---|
134 | BIO_printf(bio_err, "\t%-10s\t%s\n", "build:", build);
|
---|
135 | }
|
---|
136 | ret = 1;
|
---|
137 | end:
|
---|
138 | OSSL_PROVIDER_unload(prov);
|
---|
139 | return ret;
|
---|
140 | }
|
---|
141 |
|
---|
142 | static int print_mac(BIO *bio, const char *label, const unsigned char *mac,
|
---|
143 | size_t len)
|
---|
144 | {
|
---|
145 | int ret;
|
---|
146 | char *hexstr = NULL;
|
---|
147 |
|
---|
148 | hexstr = OPENSSL_buf2hexstr(mac, (long)len);
|
---|
149 | if (hexstr == NULL)
|
---|
150 | return 0;
|
---|
151 | ret = BIO_printf(bio, "%s = %s\n", label, hexstr);
|
---|
152 | OPENSSL_free(hexstr);
|
---|
153 | return ret;
|
---|
154 | }
|
---|
155 |
|
---|
156 | static int write_config_header(BIO *out, const char *prov_name,
|
---|
157 | const char *section)
|
---|
158 | {
|
---|
159 | return BIO_printf(out, "openssl_conf = openssl_init\n\n")
|
---|
160 | && BIO_printf(out, "[openssl_init]\n")
|
---|
161 | && BIO_printf(out, "providers = provider_section\n\n")
|
---|
162 | && BIO_printf(out, "[provider_section]\n")
|
---|
163 | && BIO_printf(out, "%s = %s\n\n", prov_name, section);
|
---|
164 | }
|
---|
165 |
|
---|
166 | /*
|
---|
167 | * Outputs a fips related config file that contains entries for the fips
|
---|
168 | * module checksum, installation indicator checksum and the options
|
---|
169 | * conditional_errors and security_checks.
|
---|
170 | *
|
---|
171 | * Returns 1 if the config file is written otherwise it returns 0 on error.
|
---|
172 | */
|
---|
173 | static int write_config_fips_section(BIO *out, const char *section,
|
---|
174 | unsigned char *module_mac,
|
---|
175 | size_t module_mac_len,
|
---|
176 | int conditional_errors,
|
---|
177 | int security_checks,
|
---|
178 | int ems_check,
|
---|
179 | unsigned char *install_mac,
|
---|
180 | size_t install_mac_len)
|
---|
181 | {
|
---|
182 | int ret = 0;
|
---|
183 |
|
---|
184 | if (BIO_printf(out, "[%s]\n", section) <= 0
|
---|
185 | || BIO_printf(out, "activate = 1\n") <= 0
|
---|
186 | || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
|
---|
187 | VERSION_VAL) <= 0
|
---|
188 | || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_CONDITIONAL_ERRORS,
|
---|
189 | conditional_errors ? "1" : "0") <= 0
|
---|
190 | || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS,
|
---|
191 | security_checks ? "1" : "0") <= 0
|
---|
192 | || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_TLS1_PRF_EMS_CHECK,
|
---|
193 | ems_check ? "1" : "0") <= 0
|
---|
194 | || !print_mac(out, OSSL_PROV_FIPS_PARAM_MODULE_MAC, module_mac,
|
---|
195 | module_mac_len))
|
---|
196 | goto end;
|
---|
197 |
|
---|
198 | if (install_mac != NULL && install_mac_len > 0) {
|
---|
199 | if (!print_mac(out, OSSL_PROV_FIPS_PARAM_INSTALL_MAC, install_mac,
|
---|
200 | install_mac_len)
|
---|
201 | || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
|
---|
202 | INSTALL_STATUS_VAL) <= 0)
|
---|
203 | goto end;
|
---|
204 | }
|
---|
205 | ret = 1;
|
---|
206 | end:
|
---|
207 | return ret;
|
---|
208 | }
|
---|
209 |
|
---|
210 | static CONF *generate_config_and_load(const char *prov_name,
|
---|
211 | const char *section,
|
---|
212 | unsigned char *module_mac,
|
---|
213 | size_t module_mac_len,
|
---|
214 | int conditional_errors,
|
---|
215 | int security_checks,
|
---|
216 | int ems_check)
|
---|
217 | {
|
---|
218 | BIO *mem_bio = NULL;
|
---|
219 | CONF *conf = NULL;
|
---|
220 |
|
---|
221 | mem_bio = BIO_new(BIO_s_mem());
|
---|
222 | if (mem_bio == NULL)
|
---|
223 | return 0;
|
---|
224 | if (!write_config_header(mem_bio, prov_name, section)
|
---|
225 | || !write_config_fips_section(mem_bio, section,
|
---|
226 | module_mac, module_mac_len,
|
---|
227 | conditional_errors,
|
---|
228 | security_checks,
|
---|
229 | ems_check,
|
---|
230 | NULL, 0))
|
---|
231 | goto end;
|
---|
232 |
|
---|
233 | conf = app_load_config_bio(mem_bio, NULL);
|
---|
234 | if (conf == NULL)
|
---|
235 | goto end;
|
---|
236 |
|
---|
237 | if (CONF_modules_load(conf, NULL, 0) <= 0)
|
---|
238 | goto end;
|
---|
239 | BIO_free(mem_bio);
|
---|
240 | return conf;
|
---|
241 | end:
|
---|
242 | NCONF_free(conf);
|
---|
243 | BIO_free(mem_bio);
|
---|
244 | return NULL;
|
---|
245 | }
|
---|
246 |
|
---|
247 | static void free_config_and_unload(CONF *conf)
|
---|
248 | {
|
---|
249 | if (conf != NULL) {
|
---|
250 | NCONF_free(conf);
|
---|
251 | CONF_modules_unload(1);
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | static int verify_module_load(const char *parent_config_file)
|
---|
256 | {
|
---|
257 | return OSSL_LIB_CTX_load_config(NULL, parent_config_file);
|
---|
258 | }
|
---|
259 |
|
---|
260 | /*
|
---|
261 | * Returns 1 if the config file entries match the passed in module_mac and
|
---|
262 | * install_mac values, otherwise it returns 0.
|
---|
263 | */
|
---|
264 | static int verify_config(const char *infile, const char *section,
|
---|
265 | unsigned char *module_mac, size_t module_mac_len,
|
---|
266 | unsigned char *install_mac, size_t install_mac_len)
|
---|
267 | {
|
---|
268 | int ret = 0;
|
---|
269 | char *s = NULL;
|
---|
270 | unsigned char *buf1 = NULL, *buf2 = NULL;
|
---|
271 | long len;
|
---|
272 | CONF *conf = NULL;
|
---|
273 |
|
---|
274 | /* read in the existing values and check they match the saved values */
|
---|
275 | conf = app_load_config(infile);
|
---|
276 | if (conf == NULL)
|
---|
277 | goto end;
|
---|
278 |
|
---|
279 | s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_VERSION);
|
---|
280 | if (s == NULL || strcmp(s, VERSION_VAL) != 0) {
|
---|
281 | BIO_printf(bio_err, "version not found\n");
|
---|
282 | goto end;
|
---|
283 | }
|
---|
284 | s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_MODULE_MAC);
|
---|
285 | if (s == NULL) {
|
---|
286 | BIO_printf(bio_err, "Module integrity MAC not found\n");
|
---|
287 | goto end;
|
---|
288 | }
|
---|
289 | buf1 = OPENSSL_hexstr2buf(s, &len);
|
---|
290 | if (buf1 == NULL
|
---|
291 | || (size_t)len != module_mac_len
|
---|
292 | || memcmp(module_mac, buf1, module_mac_len) != 0) {
|
---|
293 | BIO_printf(bio_err, "Module integrity mismatch\n");
|
---|
294 | goto end;
|
---|
295 | }
|
---|
296 | if (install_mac != NULL && install_mac_len > 0) {
|
---|
297 | s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_STATUS);
|
---|
298 | if (s == NULL || strcmp(s, INSTALL_STATUS_VAL) != 0) {
|
---|
299 | BIO_printf(bio_err, "install status not found\n");
|
---|
300 | goto end;
|
---|
301 | }
|
---|
302 | s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_MAC);
|
---|
303 | if (s == NULL) {
|
---|
304 | BIO_printf(bio_err, "Install indicator MAC not found\n");
|
---|
305 | goto end;
|
---|
306 | }
|
---|
307 | buf2 = OPENSSL_hexstr2buf(s, &len);
|
---|
308 | if (buf2 == NULL
|
---|
309 | || (size_t)len != install_mac_len
|
---|
310 | || memcmp(install_mac, buf2, install_mac_len) != 0) {
|
---|
311 | BIO_printf(bio_err, "Install indicator status mismatch\n");
|
---|
312 | goto end;
|
---|
313 | }
|
---|
314 | }
|
---|
315 | ret = 1;
|
---|
316 | end:
|
---|
317 | OPENSSL_free(buf1);
|
---|
318 | OPENSSL_free(buf2);
|
---|
319 | NCONF_free(conf);
|
---|
320 | return ret;
|
---|
321 | }
|
---|
322 |
|
---|
323 | int fipsinstall_main(int argc, char **argv)
|
---|
324 | {
|
---|
325 | int ret = 1, verify = 0, gotkey = 0, gotdigest = 0, self_test_onload = 1;
|
---|
326 | int enable_conditional_errors = 1, enable_security_checks = 1;
|
---|
327 | int enable_tls_prf_ems_check = 0; /* This is off by default */
|
---|
328 | const char *section_name = "fips_sect";
|
---|
329 | const char *mac_name = "HMAC";
|
---|
330 | const char *prov_name = "fips";
|
---|
331 | BIO *module_bio = NULL, *mem_bio = NULL, *fout = NULL;
|
---|
332 | char *in_fname = NULL, *out_fname = NULL, *prog;
|
---|
333 | char *module_fname = NULL, *parent_config = NULL, *module_path = NULL;
|
---|
334 | const char *tail;
|
---|
335 | EVP_MAC_CTX *ctx = NULL, *ctx2 = NULL;
|
---|
336 | STACK_OF(OPENSSL_STRING) *opts = NULL;
|
---|
337 | OPTION_CHOICE o;
|
---|
338 | unsigned char *read_buffer = NULL;
|
---|
339 | unsigned char module_mac[EVP_MAX_MD_SIZE];
|
---|
340 | size_t module_mac_len = EVP_MAX_MD_SIZE;
|
---|
341 | unsigned char install_mac[EVP_MAX_MD_SIZE];
|
---|
342 | size_t install_mac_len = EVP_MAX_MD_SIZE;
|
---|
343 | EVP_MAC *mac = NULL;
|
---|
344 | CONF *conf = NULL;
|
---|
345 |
|
---|
346 | if ((opts = sk_OPENSSL_STRING_new_null()) == NULL)
|
---|
347 | goto end;
|
---|
348 |
|
---|
349 | prog = opt_init(argc, argv, fipsinstall_options);
|
---|
350 | while ((o = opt_next()) != OPT_EOF) {
|
---|
351 | switch (o) {
|
---|
352 | case OPT_EOF:
|
---|
353 | case OPT_ERR:
|
---|
354 | opthelp:
|
---|
355 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
|
---|
356 | goto cleanup;
|
---|
357 | case OPT_HELP:
|
---|
358 | opt_help(fipsinstall_options);
|
---|
359 | ret = 0;
|
---|
360 | goto end;
|
---|
361 | case OPT_IN:
|
---|
362 | in_fname = opt_arg();
|
---|
363 | break;
|
---|
364 | case OPT_OUT:
|
---|
365 | out_fname = opt_arg();
|
---|
366 | break;
|
---|
367 | case OPT_NO_CONDITIONAL_ERRORS:
|
---|
368 | enable_conditional_errors = 0;
|
---|
369 | break;
|
---|
370 | case OPT_NO_SECURITY_CHECKS:
|
---|
371 | enable_security_checks = 0;
|
---|
372 | break;
|
---|
373 | case OPT_TLS_PRF_EMS_CHECK:
|
---|
374 | enable_tls_prf_ems_check = 1;
|
---|
375 | break;
|
---|
376 | case OPT_QUIET:
|
---|
377 | quiet = 1;
|
---|
378 | /* FALLTHROUGH */
|
---|
379 | case OPT_NO_LOG:
|
---|
380 | self_test_log = 0;
|
---|
381 | break;
|
---|
382 | case OPT_CORRUPT_DESC:
|
---|
383 | self_test_corrupt_desc = opt_arg();
|
---|
384 | break;
|
---|
385 | case OPT_CORRUPT_TYPE:
|
---|
386 | self_test_corrupt_type = opt_arg();
|
---|
387 | break;
|
---|
388 | case OPT_PROV_NAME:
|
---|
389 | prov_name = opt_arg();
|
---|
390 | break;
|
---|
391 | case OPT_MODULE:
|
---|
392 | module_fname = opt_arg();
|
---|
393 | break;
|
---|
394 | case OPT_SECTION_NAME:
|
---|
395 | section_name = opt_arg();
|
---|
396 | break;
|
---|
397 | case OPT_MAC_NAME:
|
---|
398 | mac_name = opt_arg();
|
---|
399 | break;
|
---|
400 | case OPT_CONFIG:
|
---|
401 | parent_config = opt_arg();
|
---|
402 | break;
|
---|
403 | case OPT_MACOPT:
|
---|
404 | if (!sk_OPENSSL_STRING_push(opts, opt_arg()))
|
---|
405 | goto opthelp;
|
---|
406 | if (strncmp(opt_arg(), "hexkey:", 7) == 0)
|
---|
407 | gotkey = 1;
|
---|
408 | else if (strncmp(opt_arg(), "digest:", 7) == 0)
|
---|
409 | gotdigest = 1;
|
---|
410 | break;
|
---|
411 | case OPT_VERIFY:
|
---|
412 | verify = 1;
|
---|
413 | break;
|
---|
414 | case OPT_SELF_TEST_ONLOAD:
|
---|
415 | self_test_onload = 1;
|
---|
416 | break;
|
---|
417 | case OPT_SELF_TEST_ONINSTALL:
|
---|
418 | self_test_onload = 0;
|
---|
419 | break;
|
---|
420 | }
|
---|
421 | }
|
---|
422 |
|
---|
423 | /* No extra arguments. */
|
---|
424 | argc = opt_num_rest();
|
---|
425 | if (argc != 0 || (verify && in_fname == NULL))
|
---|
426 | goto opthelp;
|
---|
427 |
|
---|
428 | if (parent_config != NULL) {
|
---|
429 | /* Test that a parent config can load the module */
|
---|
430 | if (verify_module_load(parent_config)) {
|
---|
431 | ret = OSSL_PROVIDER_available(NULL, prov_name) ? 0 : 1;
|
---|
432 | if (!quiet) {
|
---|
433 | BIO_printf(bio_err, "FIPS provider is %s\n",
|
---|
434 | ret == 0 ? "available" : " not available");
|
---|
435 | }
|
---|
436 | }
|
---|
437 | goto end;
|
---|
438 | }
|
---|
439 | if (module_fname == NULL)
|
---|
440 | goto opthelp;
|
---|
441 |
|
---|
442 | tail = opt_path_end(module_fname);
|
---|
443 | if (tail != NULL) {
|
---|
444 | module_path = OPENSSL_strdup(module_fname);
|
---|
445 | if (module_path == NULL)
|
---|
446 | goto end;
|
---|
447 | module_path[tail - module_fname] = '\0';
|
---|
448 | if (!OSSL_PROVIDER_set_default_search_path(NULL, module_path))
|
---|
449 | goto end;
|
---|
450 | }
|
---|
451 |
|
---|
452 | if (self_test_log
|
---|
453 | || self_test_corrupt_desc != NULL
|
---|
454 | || self_test_corrupt_type != NULL)
|
---|
455 | OSSL_SELF_TEST_set_callback(NULL, self_test_events, NULL);
|
---|
456 |
|
---|
457 | /* Use the default FIPS HMAC digest and key if not specified. */
|
---|
458 | if (!gotdigest && !sk_OPENSSL_STRING_push(opts, "digest:SHA256"))
|
---|
459 | goto end;
|
---|
460 | if (!gotkey && !sk_OPENSSL_STRING_push(opts, "hexkey:" FIPS_KEY_STRING))
|
---|
461 | goto end;
|
---|
462 |
|
---|
463 | module_bio = bio_open_default(module_fname, 'r', FORMAT_BINARY);
|
---|
464 | if (module_bio == NULL) {
|
---|
465 | BIO_printf(bio_err, "Failed to open module file\n");
|
---|
466 | goto end;
|
---|
467 | }
|
---|
468 |
|
---|
469 | read_buffer = app_malloc(BUFSIZE, "I/O buffer");
|
---|
470 | if (read_buffer == NULL)
|
---|
471 | goto end;
|
---|
472 |
|
---|
473 | mac = EVP_MAC_fetch(app_get0_libctx(), mac_name, app_get0_propq());
|
---|
474 | if (mac == NULL) {
|
---|
475 | BIO_printf(bio_err, "Unable to get MAC of type %s\n", mac_name);
|
---|
476 | goto end;
|
---|
477 | }
|
---|
478 |
|
---|
479 | ctx = EVP_MAC_CTX_new(mac);
|
---|
480 | if (ctx == NULL) {
|
---|
481 | BIO_printf(bio_err, "Unable to create MAC CTX for module check\n");
|
---|
482 | goto end;
|
---|
483 | }
|
---|
484 |
|
---|
485 | if (opts != NULL) {
|
---|
486 | int ok = 1;
|
---|
487 | OSSL_PARAM *params =
|
---|
488 | app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac));
|
---|
489 |
|
---|
490 | if (params == NULL)
|
---|
491 | goto end;
|
---|
492 |
|
---|
493 | if (!EVP_MAC_CTX_set_params(ctx, params)) {
|
---|
494 | BIO_printf(bio_err, "MAC parameter error\n");
|
---|
495 | ERR_print_errors(bio_err);
|
---|
496 | ok = 0;
|
---|
497 | }
|
---|
498 | app_params_free(params);
|
---|
499 | if (!ok)
|
---|
500 | goto end;
|
---|
501 | }
|
---|
502 |
|
---|
503 | ctx2 = EVP_MAC_CTX_dup(ctx);
|
---|
504 | if (ctx2 == NULL) {
|
---|
505 | BIO_printf(bio_err, "Unable to create MAC CTX for install indicator\n");
|
---|
506 | goto end;
|
---|
507 | }
|
---|
508 |
|
---|
509 | if (!do_mac(ctx, read_buffer, module_bio, module_mac, &module_mac_len))
|
---|
510 | goto end;
|
---|
511 |
|
---|
512 | if (self_test_onload == 0) {
|
---|
513 | mem_bio = BIO_new_mem_buf((const void *)INSTALL_STATUS_VAL,
|
---|
514 | strlen(INSTALL_STATUS_VAL));
|
---|
515 | if (mem_bio == NULL) {
|
---|
516 | BIO_printf(bio_err, "Unable to create memory BIO\n");
|
---|
517 | goto end;
|
---|
518 | }
|
---|
519 | if (!do_mac(ctx2, read_buffer, mem_bio, install_mac, &install_mac_len))
|
---|
520 | goto end;
|
---|
521 | } else {
|
---|
522 | install_mac_len = 0;
|
---|
523 | }
|
---|
524 |
|
---|
525 | if (verify) {
|
---|
526 | if (!verify_config(in_fname, section_name, module_mac, module_mac_len,
|
---|
527 | install_mac, install_mac_len))
|
---|
528 | goto end;
|
---|
529 | if (!quiet)
|
---|
530 | BIO_printf(bio_err, "VERIFY PASSED\n");
|
---|
531 | } else {
|
---|
532 |
|
---|
533 | conf = generate_config_and_load(prov_name, section_name, module_mac,
|
---|
534 | module_mac_len,
|
---|
535 | enable_conditional_errors,
|
---|
536 | enable_security_checks,
|
---|
537 | enable_tls_prf_ems_check);
|
---|
538 | if (conf == NULL)
|
---|
539 | goto end;
|
---|
540 | if (!load_fips_prov_and_run_self_test(prov_name))
|
---|
541 | goto end;
|
---|
542 |
|
---|
543 | fout =
|
---|
544 | out_fname == NULL ? dup_bio_out(FORMAT_TEXT)
|
---|
545 | : bio_open_default(out_fname, 'w', FORMAT_TEXT);
|
---|
546 | if (fout == NULL) {
|
---|
547 | BIO_printf(bio_err, "Failed to open file\n");
|
---|
548 | goto end;
|
---|
549 | }
|
---|
550 | if (!write_config_fips_section(fout, section_name,
|
---|
551 | module_mac, module_mac_len,
|
---|
552 | enable_conditional_errors,
|
---|
553 | enable_security_checks,
|
---|
554 | enable_tls_prf_ems_check,
|
---|
555 | install_mac, install_mac_len))
|
---|
556 | goto end;
|
---|
557 | if (!quiet)
|
---|
558 | BIO_printf(bio_err, "INSTALL PASSED\n");
|
---|
559 | }
|
---|
560 |
|
---|
561 | ret = 0;
|
---|
562 | end:
|
---|
563 | if (ret == 1) {
|
---|
564 | if (!quiet)
|
---|
565 | BIO_printf(bio_err, "%s FAILED\n", verify ? "VERIFY" : "INSTALL");
|
---|
566 | ERR_print_errors(bio_err);
|
---|
567 | }
|
---|
568 |
|
---|
569 | cleanup:
|
---|
570 | OPENSSL_free(module_path);
|
---|
571 | BIO_free(fout);
|
---|
572 | BIO_free(mem_bio);
|
---|
573 | BIO_free(module_bio);
|
---|
574 | sk_OPENSSL_STRING_free(opts);
|
---|
575 | EVP_MAC_free(mac);
|
---|
576 | EVP_MAC_CTX_free(ctx2);
|
---|
577 | EVP_MAC_CTX_free(ctx);
|
---|
578 | OPENSSL_free(read_buffer);
|
---|
579 | free_config_and_unload(conf);
|
---|
580 | return ret;
|
---|
581 | }
|
---|
582 |
|
---|
583 | static int self_test_events(const OSSL_PARAM params[], void *arg)
|
---|
584 | {
|
---|
585 | const OSSL_PARAM *p = NULL;
|
---|
586 | const char *phase = NULL, *type = NULL, *desc = NULL;
|
---|
587 | int ret = 0;
|
---|
588 |
|
---|
589 | p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_PHASE);
|
---|
590 | if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
|
---|
591 | goto err;
|
---|
592 | phase = (const char *)p->data;
|
---|
593 |
|
---|
594 | p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_DESC);
|
---|
595 | if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
|
---|
596 | goto err;
|
---|
597 | desc = (const char *)p->data;
|
---|
598 |
|
---|
599 | p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_TYPE);
|
---|
600 | if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
|
---|
601 | goto err;
|
---|
602 | type = (const char *)p->data;
|
---|
603 |
|
---|
604 | if (self_test_log) {
|
---|
605 | if (strcmp(phase, OSSL_SELF_TEST_PHASE_START) == 0)
|
---|
606 | BIO_printf(bio_err, "%s : (%s) : ", desc, type);
|
---|
607 | else if (strcmp(phase, OSSL_SELF_TEST_PHASE_PASS) == 0
|
---|
608 | || strcmp(phase, OSSL_SELF_TEST_PHASE_FAIL) == 0)
|
---|
609 | BIO_printf(bio_err, "%s\n", phase);
|
---|
610 | }
|
---|
611 | /*
|
---|
612 | * The self test code will internally corrupt the KAT test result if an
|
---|
613 | * error is returned during the corrupt phase.
|
---|
614 | */
|
---|
615 | if (strcmp(phase, OSSL_SELF_TEST_PHASE_CORRUPT) == 0
|
---|
616 | && (self_test_corrupt_desc != NULL
|
---|
617 | || self_test_corrupt_type != NULL)) {
|
---|
618 | if (self_test_corrupt_desc != NULL
|
---|
619 | && strcmp(self_test_corrupt_desc, desc) != 0)
|
---|
620 | goto end;
|
---|
621 | if (self_test_corrupt_type != NULL
|
---|
622 | && strcmp(self_test_corrupt_type, type) != 0)
|
---|
623 | goto end;
|
---|
624 | BIO_printf(bio_err, "%s ", phase);
|
---|
625 | goto err;
|
---|
626 | }
|
---|
627 | end:
|
---|
628 | ret = 1;
|
---|
629 | err:
|
---|
630 | return ret;
|
---|
631 | }
|
---|