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