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