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