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