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