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