Fix fipsinstall module path
[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, OPT_CONFIG
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     {"config", OPT_CONFIG, '<', "The parent config to verify"},
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 static int verify_module_load(const char *parent_config_file)
207 {
208     return OPENSSL_CTX_load_config(NULL, parent_config_file);
209 }
210
211 /*
212  * Returns 1 if the config file entries match the passed in module_mac and
213  * install_mac values, otherwise it returns 0.
214  */
215 static int verify_config(const char *infile, const char *section,
216                          unsigned char *module_mac, size_t module_mac_len,
217                          unsigned char *install_mac, size_t install_mac_len)
218 {
219     int ret = 0;
220     char *s = NULL;
221     unsigned char *buf1 = NULL, *buf2 = NULL;
222     long len;
223     CONF *conf = NULL;
224
225     /* read in the existing values and check they match the saved values */
226     conf = app_load_config(infile);
227     if (conf == NULL)
228         goto end;
229
230     s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_VERSION);
231     if (s == NULL || strcmp(s, VERSION_VAL) != 0) {
232         BIO_printf(bio_err, "version not found\n");
233         goto end;
234     }
235     s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_STATUS);
236     if (s == NULL || strcmp(s, INSTALL_STATUS_VAL) != 0) {
237         BIO_printf(bio_err, "install status not found\n");
238         goto end;
239     }
240     s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_MODULE_MAC);
241     if (s == NULL) {
242         BIO_printf(bio_err, "Module integrity MAC not found\n");
243         goto end;
244     }
245     buf1 = OPENSSL_hexstr2buf(s, &len);
246     if (buf1 == NULL
247             || (size_t)len != module_mac_len
248             || memcmp(module_mac, buf1, module_mac_len) != 0) {
249         BIO_printf(bio_err, "Module integrity mismatch\n");
250         goto end;
251     }
252     s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_MAC);
253     if (s == NULL) {
254         BIO_printf(bio_err, "Install indicator MAC not found\n");
255         goto end;
256     }
257     buf2 = OPENSSL_hexstr2buf(s, &len);
258     if (buf2 == NULL
259             || (size_t)len != install_mac_len
260             || memcmp(install_mac, buf2, install_mac_len) != 0) {
261         BIO_printf(bio_err, "Install indicator status mismatch\n");
262         goto end;
263     }
264     ret = 1;
265 end:
266     OPENSSL_free(buf1);
267     OPENSSL_free(buf2);
268     NCONF_free(conf);
269     return ret;
270 }
271
272 int fipsinstall_main(int argc, char **argv)
273 {
274     int ret = 1, verify = 0, gotkey = 0, gotdigest = 0;
275     const char *section_name = "fips_sect";
276     const char *mac_name = "HMAC";
277     const char *prov_name = "fips";
278     BIO *module_bio = NULL, *mem_bio = NULL, *fout = NULL;
279     char *in_fname = NULL, *out_fname = NULL, *prog;
280     char *module_fname = NULL, *parent_config = NULL, *module_path = NULL;
281     const char *tail;
282     EVP_MAC_CTX *ctx = NULL, *ctx2 = NULL;
283     STACK_OF(OPENSSL_STRING) *opts = NULL;
284     OPTION_CHOICE o;
285     unsigned char *read_buffer = NULL;
286     unsigned char module_mac[EVP_MAX_MD_SIZE];
287     size_t module_mac_len = EVP_MAX_MD_SIZE;
288     unsigned char install_mac[EVP_MAX_MD_SIZE];
289     size_t install_mac_len = EVP_MAX_MD_SIZE;
290     EVP_MAC *mac = NULL;
291     CONF *conf = NULL;
292
293     if ((opts = sk_OPENSSL_STRING_new_null()) == NULL)
294         goto end;
295
296     prog = opt_init(argc, argv, fipsinstall_options);
297     while ((o = opt_next()) != OPT_EOF) {
298         switch (o) {
299         case OPT_EOF:
300         case OPT_ERR:
301 opthelp:
302             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
303             goto cleanup;
304         case OPT_HELP:
305             opt_help(fipsinstall_options);
306             ret = 0;
307             goto end;
308         case OPT_IN:
309             in_fname = opt_arg();
310             break;
311         case OPT_OUT:
312             out_fname = opt_arg();
313             break;
314         case OPT_QUIET:
315             quiet = 1;
316             /* FALLTHROUGH */
317         case OPT_NO_LOG:
318             self_test_log = 0;
319             break;
320         case OPT_CORRUPT_DESC:
321             self_test_corrupt_desc = opt_arg();
322             break;
323         case OPT_CORRUPT_TYPE:
324             self_test_corrupt_type = opt_arg();
325             break;
326         case OPT_PROV_NAME:
327             prov_name = opt_arg();
328             break;
329         case OPT_MODULE:
330             module_fname = opt_arg();
331             break;
332         case OPT_SECTION_NAME:
333             section_name = opt_arg();
334             break;
335         case OPT_MAC_NAME:
336             mac_name = opt_arg();
337             break;
338         case OPT_CONFIG:
339             parent_config = opt_arg();
340             break;
341         case OPT_MACOPT:
342             if (!sk_OPENSSL_STRING_push(opts, opt_arg()))
343                 goto opthelp;
344             if (strncmp(opt_arg(), "hexkey:", 7) == 0)
345                 gotkey = 1;
346             else if (strncmp(opt_arg(), "digest:", 7) == 0)
347                 gotdigest = 1;
348             break;
349         case OPT_VERIFY:
350             verify = 1;
351             break;
352         }
353     }
354     argc = opt_num_rest();
355
356     if (parent_config != NULL) {
357         /* Test that a parent config can load the module */
358         if (verify_module_load(parent_config)) {
359             ret = OSSL_PROVIDER_available(NULL, prov_name) ? 0 : 1;
360             if (!quiet)
361                 BIO_printf(bio_out, "FIPS provider is %s\n",
362                            ret == 0 ? "available" : " not available");
363         }
364         goto end;
365     }
366     if (module_fname == NULL
367         || (verify && in_fname == NULL)
368         || (!verify && out_fname == NULL)
369         || argc != 0)
370         goto opthelp;
371
372     tail = opt_path_end(module_fname);
373     if (tail != NULL) {
374         module_path = OPENSSL_strdup(module_fname);
375         if (module_path == NULL)
376             goto end;
377         module_path[tail - module_fname] = '\0';
378         if (!OSSL_PROVIDER_set_default_search_path(NULL, module_path))
379             goto end;
380     }
381
382     if (self_test_log
383             || self_test_corrupt_desc != NULL
384             || self_test_corrupt_type != NULL)
385         OSSL_SELF_TEST_set_callback(NULL, self_test_events, NULL);
386
387     /* Use the default FIPS HMAC digest and key if not specified. */
388     if (!gotdigest && !sk_OPENSSL_STRING_push(opts, "digest:SHA256"))
389         goto end;
390     if (!gotkey && !sk_OPENSSL_STRING_push(opts, "hexkey:" FIPS_KEY_STRING))
391         goto end;
392
393     module_bio = bio_open_default(module_fname, 'r', FORMAT_BINARY);
394     if (module_bio == NULL) {
395         BIO_printf(bio_err, "Failed to open module file\n");
396         goto end;
397     }
398
399     read_buffer = app_malloc(BUFSIZE, "I/O buffer");
400     if (read_buffer == NULL)
401         goto end;
402
403     mac = EVP_MAC_fetch(NULL, mac_name, NULL);
404     if (mac == NULL) {
405         BIO_printf(bio_err, "Unable to get MAC of type %s\n", mac_name);
406         goto end;
407     }
408
409     ctx = EVP_MAC_CTX_new(mac);
410     if (ctx == NULL) {
411         BIO_printf(bio_err, "Unable to create MAC CTX for module check\n");
412         goto end;
413     }
414
415     if (opts != NULL) {
416         int ok = 1;
417         OSSL_PARAM *params =
418             app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac));
419
420         if (params == NULL)
421             goto end;
422
423         if (!EVP_MAC_CTX_set_params(ctx, params)) {
424             BIO_printf(bio_err, "MAC parameter error\n");
425             ERR_print_errors(bio_err);
426             ok = 0;
427         }
428         app_params_free(params);
429         if (!ok)
430             goto end;
431     }
432
433     ctx2 = EVP_MAC_CTX_dup(ctx);
434     if (ctx2 == NULL) {
435         BIO_printf(bio_err, "Unable to create MAC CTX for install indicator\n");
436         goto end;
437     }
438
439     if (!do_mac(ctx, read_buffer, module_bio, module_mac, &module_mac_len))
440         goto end;
441
442     mem_bio = BIO_new_mem_buf((const void *)INSTALL_STATUS_VAL,
443                               strlen(INSTALL_STATUS_VAL));
444     if (mem_bio == NULL) {
445         BIO_printf(bio_err, "Unable to create memory BIO\n");
446         goto end;
447     }
448     if (!do_mac(ctx2, read_buffer, mem_bio, install_mac, &install_mac_len))
449         goto end;
450
451     if (verify) {
452         if (!verify_config(in_fname, section_name, module_mac, module_mac_len,
453                            install_mac, install_mac_len))
454             goto end;
455         if (!quiet)
456             BIO_printf(bio_out, "VERIFY PASSED\n");
457     } else {
458
459         conf = generate_config_and_load(prov_name, section_name, module_mac,
460                                         module_mac_len);
461         if (conf == NULL)
462             goto end;
463         if (!load_fips_prov_and_run_self_test(prov_name))
464             goto end;
465
466         fout = bio_open_default(out_fname, 'w', FORMAT_TEXT);
467         if (fout == NULL) {
468             BIO_printf(bio_err, "Failed to open file\n");
469             goto end;
470         }
471         if (!write_config_fips_section(fout, section_name, module_mac,
472                                        module_mac_len, install_mac,
473                                        install_mac_len))
474             goto end;
475         if (!quiet)
476             BIO_printf(bio_out, "INSTALL PASSED\n");
477     }
478
479     ret = 0;
480 end:
481     if (ret == 1) {
482         if (!quiet)
483             BIO_printf(bio_err, "%s FAILED\n", verify ? "VERIFY" : "INSTALL");
484         ERR_print_errors(bio_err);
485     }
486
487 cleanup:
488     OPENSSL_free(module_path);
489     BIO_free(fout);
490     BIO_free(mem_bio);
491     BIO_free(module_bio);
492     sk_OPENSSL_STRING_free(opts);
493     EVP_MAC_free(mac);
494     EVP_MAC_CTX_free(ctx2);
495     EVP_MAC_CTX_free(ctx);
496     OPENSSL_free(read_buffer);
497     free_config_and_unload(conf);
498     return ret;
499 }
500
501 static int self_test_events(const OSSL_PARAM params[], void *arg)
502 {
503     const OSSL_PARAM *p = NULL;
504     const char *phase = NULL, *type = NULL, *desc = NULL;
505     int ret = 0;
506
507     p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_PHASE);
508     if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
509         goto err;
510     phase = (const char *)p->data;
511
512     p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_DESC);
513     if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
514         goto err;
515     desc = (const char *)p->data;
516
517     p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_TYPE);
518     if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
519         goto err;
520     type = (const char *)p->data;
521
522     if (self_test_log) {
523         if (strcmp(phase, OSSL_SELF_TEST_PHASE_START) == 0)
524             BIO_printf(bio_out, "%s : (%s) : ", desc, type);
525         else if (strcmp(phase, OSSL_SELF_TEST_PHASE_PASS) == 0
526                  || strcmp(phase, OSSL_SELF_TEST_PHASE_FAIL) == 0)
527             BIO_printf(bio_out, "%s\n", phase);
528     }
529     /*
530      * The self test code will internally corrupt the KAT test result if an
531      * error is returned during the corrupt phase.
532      */
533     if (strcmp(phase, OSSL_SELF_TEST_PHASE_CORRUPT) == 0
534             && (self_test_corrupt_desc != NULL
535                 || self_test_corrupt_type != NULL)) {
536         if (self_test_corrupt_desc != NULL
537                 && strcmp(self_test_corrupt_desc, desc) != 0)
538             goto end;
539         if (self_test_corrupt_type != NULL
540                 && strcmp(self_test_corrupt_type, type) != 0)
541             goto end;
542         BIO_printf(bio_out, "%s ", phase);
543         goto err;
544     }
545 end:
546     ret = 1;
547 err:
548     return ret;
549 }