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