xref: /freebsd/crypto/openssl/apps/fipsinstall.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery  * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6*b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery  */
9*b077aed3SPierre Pronchery 
10*b077aed3SPierre Pronchery #include <string.h>
11*b077aed3SPierre Pronchery #include <openssl/evp.h>
12*b077aed3SPierre Pronchery #include <openssl/err.h>
13*b077aed3SPierre Pronchery #include <openssl/provider.h>
14*b077aed3SPierre Pronchery #include <openssl/params.h>
15*b077aed3SPierre Pronchery #include <openssl/fips_names.h>
16*b077aed3SPierre Pronchery #include <openssl/core_names.h>
17*b077aed3SPierre Pronchery #include <openssl/self_test.h>
18*b077aed3SPierre Pronchery #include <openssl/fipskey.h>
19*b077aed3SPierre Pronchery #include "apps.h"
20*b077aed3SPierre Pronchery #include "progs.h"
21*b077aed3SPierre Pronchery 
22*b077aed3SPierre Pronchery #define BUFSIZE 4096
23*b077aed3SPierre Pronchery 
24*b077aed3SPierre Pronchery /* Configuration file values */
25*b077aed3SPierre Pronchery #define VERSION_KEY  "version"
26*b077aed3SPierre Pronchery #define VERSION_VAL  "1"
27*b077aed3SPierre Pronchery #define INSTALL_STATUS_VAL "INSTALL_SELF_TEST_KATS_RUN"
28*b077aed3SPierre Pronchery 
29*b077aed3SPierre Pronchery static OSSL_CALLBACK self_test_events;
30*b077aed3SPierre Pronchery static char *self_test_corrupt_desc = NULL;
31*b077aed3SPierre Pronchery static char *self_test_corrupt_type = NULL;
32*b077aed3SPierre Pronchery static int self_test_log = 1;
33*b077aed3SPierre Pronchery static int quiet = 0;
34*b077aed3SPierre Pronchery 
35*b077aed3SPierre Pronchery typedef enum OPTION_choice {
36*b077aed3SPierre Pronchery     OPT_COMMON,
37*b077aed3SPierre Pronchery     OPT_IN, OPT_OUT, OPT_MODULE,
38*b077aed3SPierre Pronchery     OPT_PROV_NAME, OPT_SECTION_NAME, OPT_MAC_NAME, OPT_MACOPT, OPT_VERIFY,
39*b077aed3SPierre Pronchery     OPT_NO_LOG, OPT_CORRUPT_DESC, OPT_CORRUPT_TYPE, OPT_QUIET, OPT_CONFIG,
40*b077aed3SPierre Pronchery     OPT_NO_CONDITIONAL_ERRORS,
41*b077aed3SPierre Pronchery     OPT_NO_SECURITY_CHECKS,
42*b077aed3SPierre Pronchery     OPT_SELF_TEST_ONLOAD
43*b077aed3SPierre Pronchery } OPTION_CHOICE;
44*b077aed3SPierre Pronchery 
45*b077aed3SPierre Pronchery const OPTIONS fipsinstall_options[] = {
46*b077aed3SPierre Pronchery     OPT_SECTION("General"),
47*b077aed3SPierre Pronchery     {"help", OPT_HELP, '-', "Display this summary"},
48*b077aed3SPierre Pronchery     {"verify", OPT_VERIFY, '-',
49*b077aed3SPierre Pronchery         "Verify a config file instead of generating one"},
50*b077aed3SPierre Pronchery     {"module", OPT_MODULE, '<', "File name of the provider module"},
51*b077aed3SPierre Pronchery     {"provider_name", OPT_PROV_NAME, 's', "FIPS provider name"},
52*b077aed3SPierre Pronchery     {"section_name", OPT_SECTION_NAME, 's',
53*b077aed3SPierre Pronchery      "FIPS Provider config section name (optional)"},
54*b077aed3SPierre Pronchery      {"no_conditional_errors", OPT_NO_CONDITIONAL_ERRORS, '-',
55*b077aed3SPierre Pronchery       "Disable the ability of the fips module to enter an error state if"
56*b077aed3SPierre Pronchery       " any conditional self tests fail"},
57*b077aed3SPierre Pronchery     {"no_security_checks", OPT_NO_SECURITY_CHECKS, '-',
58*b077aed3SPierre Pronchery      "Disable the run-time FIPS security checks in the module"},
59*b077aed3SPierre Pronchery     {"self_test_onload", OPT_SELF_TEST_ONLOAD, '-',
60*b077aed3SPierre Pronchery      "Forces self tests to always run on module load"},
61*b077aed3SPierre Pronchery     OPT_SECTION("Input"),
62*b077aed3SPierre Pronchery     {"in", OPT_IN, '<', "Input config file, used when verifying"},
63*b077aed3SPierre Pronchery 
64*b077aed3SPierre Pronchery     OPT_SECTION("Output"),
65*b077aed3SPierre Pronchery     {"out", OPT_OUT, '>', "Output config file, used when generating"},
66*b077aed3SPierre Pronchery     {"mac_name", OPT_MAC_NAME, 's', "MAC name"},
67*b077aed3SPierre Pronchery     {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form. "
68*b077aed3SPierre Pronchery                                 "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
69*b077aed3SPierre Pronchery     {"noout", OPT_NO_LOG, '-', "Disable logging of self test events"},
70*b077aed3SPierre Pronchery     {"corrupt_desc", OPT_CORRUPT_DESC, 's', "Corrupt a self test by description"},
71*b077aed3SPierre Pronchery     {"corrupt_type", OPT_CORRUPT_TYPE, 's', "Corrupt a self test by type"},
72*b077aed3SPierre Pronchery     {"config", OPT_CONFIG, '<', "The parent config to verify"},
73*b077aed3SPierre Pronchery     {"quiet", OPT_QUIET, '-', "No messages, just exit status"},
74*b077aed3SPierre Pronchery     {NULL}
75*b077aed3SPierre Pronchery };
76*b077aed3SPierre Pronchery 
do_mac(EVP_MAC_CTX * ctx,unsigned char * tmp,BIO * in,unsigned char * out,size_t * out_len)77*b077aed3SPierre Pronchery static int do_mac(EVP_MAC_CTX *ctx, unsigned char *tmp, BIO *in,
78*b077aed3SPierre Pronchery                   unsigned char *out, size_t *out_len)
79*b077aed3SPierre Pronchery {
80*b077aed3SPierre Pronchery     int ret = 0;
81*b077aed3SPierre Pronchery     int i;
82*b077aed3SPierre Pronchery     size_t outsz = *out_len;
83*b077aed3SPierre Pronchery 
84*b077aed3SPierre Pronchery     if (!EVP_MAC_init(ctx, NULL, 0, NULL))
85*b077aed3SPierre Pronchery         goto err;
86*b077aed3SPierre Pronchery     if (EVP_MAC_CTX_get_mac_size(ctx) > outsz)
87*b077aed3SPierre Pronchery         goto end;
88*b077aed3SPierre Pronchery     while ((i = BIO_read(in, (char *)tmp, BUFSIZE)) != 0) {
89*b077aed3SPierre Pronchery         if (i < 0 || !EVP_MAC_update(ctx, tmp, i))
90*b077aed3SPierre Pronchery             goto err;
91*b077aed3SPierre Pronchery     }
92*b077aed3SPierre Pronchery end:
93*b077aed3SPierre Pronchery     if (!EVP_MAC_final(ctx, out, out_len, outsz))
94*b077aed3SPierre Pronchery         goto err;
95*b077aed3SPierre Pronchery     ret = 1;
96*b077aed3SPierre Pronchery err:
97*b077aed3SPierre Pronchery     return ret;
98*b077aed3SPierre Pronchery }
99*b077aed3SPierre Pronchery 
load_fips_prov_and_run_self_test(const char * prov_name)100*b077aed3SPierre Pronchery static int load_fips_prov_and_run_self_test(const char *prov_name)
101*b077aed3SPierre Pronchery {
102*b077aed3SPierre Pronchery     int ret = 0;
103*b077aed3SPierre Pronchery     OSSL_PROVIDER *prov = NULL;
104*b077aed3SPierre Pronchery 
105*b077aed3SPierre Pronchery     prov = OSSL_PROVIDER_load(NULL, prov_name);
106*b077aed3SPierre Pronchery     if (prov == NULL) {
107*b077aed3SPierre Pronchery         BIO_printf(bio_err, "Failed to load FIPS module\n");
108*b077aed3SPierre Pronchery         goto end;
109*b077aed3SPierre Pronchery     }
110*b077aed3SPierre Pronchery     ret = 1;
111*b077aed3SPierre Pronchery end:
112*b077aed3SPierre Pronchery     OSSL_PROVIDER_unload(prov);
113*b077aed3SPierre Pronchery     return ret;
114*b077aed3SPierre Pronchery }
115*b077aed3SPierre Pronchery 
print_mac(BIO * bio,const char * label,const unsigned char * mac,size_t len)116*b077aed3SPierre Pronchery static int print_mac(BIO *bio, const char *label, const unsigned char *mac,
117*b077aed3SPierre Pronchery                      size_t len)
118*b077aed3SPierre Pronchery {
119*b077aed3SPierre Pronchery     int ret;
120*b077aed3SPierre Pronchery     char *hexstr = NULL;
121*b077aed3SPierre Pronchery 
122*b077aed3SPierre Pronchery     hexstr = OPENSSL_buf2hexstr(mac, (long)len);
123*b077aed3SPierre Pronchery     if (hexstr == NULL)
124*b077aed3SPierre Pronchery         return 0;
125*b077aed3SPierre Pronchery     ret = BIO_printf(bio, "%s = %s\n", label, hexstr);
126*b077aed3SPierre Pronchery     OPENSSL_free(hexstr);
127*b077aed3SPierre Pronchery     return ret;
128*b077aed3SPierre Pronchery }
129*b077aed3SPierre Pronchery 
write_config_header(BIO * out,const char * prov_name,const char * section)130*b077aed3SPierre Pronchery static int write_config_header(BIO *out, const char *prov_name,
131*b077aed3SPierre Pronchery                                const char *section)
132*b077aed3SPierre Pronchery {
133*b077aed3SPierre Pronchery     return BIO_printf(out, "openssl_conf = openssl_init\n\n")
134*b077aed3SPierre Pronchery            && BIO_printf(out, "[openssl_init]\n")
135*b077aed3SPierre Pronchery            && BIO_printf(out, "providers = provider_section\n\n")
136*b077aed3SPierre Pronchery            && BIO_printf(out, "[provider_section]\n")
137*b077aed3SPierre Pronchery            && BIO_printf(out, "%s = %s\n\n", prov_name, section);
138*b077aed3SPierre Pronchery }
139*b077aed3SPierre Pronchery 
140*b077aed3SPierre Pronchery /*
141*b077aed3SPierre Pronchery  * Outputs a fips related config file that contains entries for the fips
142*b077aed3SPierre Pronchery  * module checksum, installation indicator checksum and the options
143*b077aed3SPierre Pronchery  * conditional_errors and security_checks.
144*b077aed3SPierre Pronchery  *
145*b077aed3SPierre Pronchery  * Returns 1 if the config file is written otherwise it returns 0 on error.
146*b077aed3SPierre Pronchery  */
write_config_fips_section(BIO * out,const char * section,unsigned char * module_mac,size_t module_mac_len,int conditional_errors,int security_checks,unsigned char * install_mac,size_t install_mac_len)147*b077aed3SPierre Pronchery static int write_config_fips_section(BIO *out, const char *section,
148*b077aed3SPierre Pronchery                                      unsigned char *module_mac,
149*b077aed3SPierre Pronchery                                      size_t module_mac_len,
150*b077aed3SPierre Pronchery                                      int conditional_errors,
151*b077aed3SPierre Pronchery                                      int security_checks,
152*b077aed3SPierre Pronchery                                      unsigned char *install_mac,
153*b077aed3SPierre Pronchery                                      size_t install_mac_len)
154*b077aed3SPierre Pronchery {
155*b077aed3SPierre Pronchery     int ret = 0;
156*b077aed3SPierre Pronchery 
157*b077aed3SPierre Pronchery     if (BIO_printf(out, "[%s]\n", section) <= 0
158*b077aed3SPierre Pronchery         || BIO_printf(out, "activate = 1\n") <= 0
159*b077aed3SPierre Pronchery         || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
160*b077aed3SPierre Pronchery                       VERSION_VAL) <= 0
161*b077aed3SPierre Pronchery         || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_CONDITIONAL_ERRORS,
162*b077aed3SPierre Pronchery                       conditional_errors ? "1" : "0") <= 0
163*b077aed3SPierre Pronchery         || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_SECURITY_CHECKS,
164*b077aed3SPierre Pronchery                       security_checks ? "1" : "0") <= 0
165*b077aed3SPierre Pronchery         || !print_mac(out, OSSL_PROV_FIPS_PARAM_MODULE_MAC, module_mac,
166*b077aed3SPierre Pronchery                       module_mac_len))
167*b077aed3SPierre Pronchery         goto end;
168*b077aed3SPierre Pronchery 
169*b077aed3SPierre Pronchery     if (install_mac != NULL && install_mac_len > 0) {
170*b077aed3SPierre Pronchery         if (!print_mac(out, OSSL_PROV_FIPS_PARAM_INSTALL_MAC, install_mac,
171*b077aed3SPierre Pronchery                        install_mac_len)
172*b077aed3SPierre Pronchery             || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
173*b077aed3SPierre Pronchery                           INSTALL_STATUS_VAL) <= 0)
174*b077aed3SPierre Pronchery         goto end;
175*b077aed3SPierre Pronchery     }
176*b077aed3SPierre Pronchery     ret = 1;
177*b077aed3SPierre Pronchery end:
178*b077aed3SPierre Pronchery     return ret;
179*b077aed3SPierre Pronchery }
180*b077aed3SPierre Pronchery 
generate_config_and_load(const char * prov_name,const char * section,unsigned char * module_mac,size_t module_mac_len,int conditional_errors,int security_checks)181*b077aed3SPierre Pronchery static CONF *generate_config_and_load(const char *prov_name,
182*b077aed3SPierre Pronchery                                       const char *section,
183*b077aed3SPierre Pronchery                                       unsigned char *module_mac,
184*b077aed3SPierre Pronchery                                       size_t module_mac_len,
185*b077aed3SPierre Pronchery                                       int conditional_errors,
186*b077aed3SPierre Pronchery                                       int security_checks)
187*b077aed3SPierre Pronchery {
188*b077aed3SPierre Pronchery     BIO *mem_bio = NULL;
189*b077aed3SPierre Pronchery     CONF *conf = NULL;
190*b077aed3SPierre Pronchery 
191*b077aed3SPierre Pronchery     mem_bio = BIO_new(BIO_s_mem());
192*b077aed3SPierre Pronchery     if (mem_bio  == NULL)
193*b077aed3SPierre Pronchery         return 0;
194*b077aed3SPierre Pronchery     if (!write_config_header(mem_bio, prov_name, section)
195*b077aed3SPierre Pronchery          || !write_config_fips_section(mem_bio, section,
196*b077aed3SPierre Pronchery                                        module_mac, module_mac_len,
197*b077aed3SPierre Pronchery                                        conditional_errors,
198*b077aed3SPierre Pronchery                                        security_checks,
199*b077aed3SPierre Pronchery                                        NULL, 0))
200*b077aed3SPierre Pronchery         goto end;
201*b077aed3SPierre Pronchery 
202*b077aed3SPierre Pronchery     conf = app_load_config_bio(mem_bio, NULL);
203*b077aed3SPierre Pronchery     if (conf == NULL)
204*b077aed3SPierre Pronchery         goto end;
205*b077aed3SPierre Pronchery 
206*b077aed3SPierre Pronchery     if (CONF_modules_load(conf, NULL, 0) <= 0)
207*b077aed3SPierre Pronchery         goto end;
208*b077aed3SPierre Pronchery     BIO_free(mem_bio);
209*b077aed3SPierre Pronchery     return conf;
210*b077aed3SPierre Pronchery end:
211*b077aed3SPierre Pronchery     NCONF_free(conf);
212*b077aed3SPierre Pronchery     BIO_free(mem_bio);
213*b077aed3SPierre Pronchery     return NULL;
214*b077aed3SPierre Pronchery }
215*b077aed3SPierre Pronchery 
free_config_and_unload(CONF * conf)216*b077aed3SPierre Pronchery static void free_config_and_unload(CONF *conf)
217*b077aed3SPierre Pronchery {
218*b077aed3SPierre Pronchery     if (conf != NULL) {
219*b077aed3SPierre Pronchery         NCONF_free(conf);
220*b077aed3SPierre Pronchery         CONF_modules_unload(1);
221*b077aed3SPierre Pronchery     }
222*b077aed3SPierre Pronchery }
223*b077aed3SPierre Pronchery 
verify_module_load(const char * parent_config_file)224*b077aed3SPierre Pronchery static int verify_module_load(const char *parent_config_file)
225*b077aed3SPierre Pronchery {
226*b077aed3SPierre Pronchery     return OSSL_LIB_CTX_load_config(NULL, parent_config_file);
227*b077aed3SPierre Pronchery }
228*b077aed3SPierre Pronchery 
229*b077aed3SPierre Pronchery /*
230*b077aed3SPierre Pronchery  * Returns 1 if the config file entries match the passed in module_mac and
231*b077aed3SPierre Pronchery  * install_mac values, otherwise it returns 0.
232*b077aed3SPierre Pronchery  */
verify_config(const char * infile,const char * section,unsigned char * module_mac,size_t module_mac_len,unsigned char * install_mac,size_t install_mac_len)233*b077aed3SPierre Pronchery static int verify_config(const char *infile, const char *section,
234*b077aed3SPierre Pronchery                          unsigned char *module_mac, size_t module_mac_len,
235*b077aed3SPierre Pronchery                          unsigned char *install_mac, size_t install_mac_len)
236*b077aed3SPierre Pronchery {
237*b077aed3SPierre Pronchery     int ret = 0;
238*b077aed3SPierre Pronchery     char *s = NULL;
239*b077aed3SPierre Pronchery     unsigned char *buf1 = NULL, *buf2 = NULL;
240*b077aed3SPierre Pronchery     long len;
241*b077aed3SPierre Pronchery     CONF *conf = NULL;
242*b077aed3SPierre Pronchery 
243*b077aed3SPierre Pronchery     /* read in the existing values and check they match the saved values */
244*b077aed3SPierre Pronchery     conf = app_load_config(infile);
245*b077aed3SPierre Pronchery     if (conf == NULL)
246*b077aed3SPierre Pronchery         goto end;
247*b077aed3SPierre Pronchery 
248*b077aed3SPierre Pronchery     s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_VERSION);
249*b077aed3SPierre Pronchery     if (s == NULL || strcmp(s, VERSION_VAL) != 0) {
250*b077aed3SPierre Pronchery         BIO_printf(bio_err, "version not found\n");
251*b077aed3SPierre Pronchery         goto end;
252*b077aed3SPierre Pronchery     }
253*b077aed3SPierre Pronchery     s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_MODULE_MAC);
254*b077aed3SPierre Pronchery     if (s == NULL) {
255*b077aed3SPierre Pronchery         BIO_printf(bio_err, "Module integrity MAC not found\n");
256*b077aed3SPierre Pronchery         goto end;
257*b077aed3SPierre Pronchery     }
258*b077aed3SPierre Pronchery     buf1 = OPENSSL_hexstr2buf(s, &len);
259*b077aed3SPierre Pronchery     if (buf1 == NULL
260*b077aed3SPierre Pronchery             || (size_t)len != module_mac_len
261*b077aed3SPierre Pronchery             || memcmp(module_mac, buf1, module_mac_len) != 0) {
262*b077aed3SPierre Pronchery         BIO_printf(bio_err, "Module integrity mismatch\n");
263*b077aed3SPierre Pronchery         goto end;
264*b077aed3SPierre Pronchery     }
265*b077aed3SPierre Pronchery     if (install_mac != NULL && install_mac_len > 0) {
266*b077aed3SPierre Pronchery         s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_STATUS);
267*b077aed3SPierre Pronchery         if (s == NULL || strcmp(s, INSTALL_STATUS_VAL) != 0) {
268*b077aed3SPierre Pronchery             BIO_printf(bio_err, "install status not found\n");
269*b077aed3SPierre Pronchery             goto end;
270*b077aed3SPierre Pronchery         }
271*b077aed3SPierre Pronchery         s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_MAC);
272*b077aed3SPierre Pronchery         if (s == NULL) {
273*b077aed3SPierre Pronchery             BIO_printf(bio_err, "Install indicator MAC not found\n");
274*b077aed3SPierre Pronchery             goto end;
275*b077aed3SPierre Pronchery         }
276*b077aed3SPierre Pronchery         buf2 = OPENSSL_hexstr2buf(s, &len);
277*b077aed3SPierre Pronchery         if (buf2 == NULL
278*b077aed3SPierre Pronchery                 || (size_t)len != install_mac_len
279*b077aed3SPierre Pronchery                 || memcmp(install_mac, buf2, install_mac_len) != 0) {
280*b077aed3SPierre Pronchery             BIO_printf(bio_err, "Install indicator status mismatch\n");
281*b077aed3SPierre Pronchery             goto end;
282*b077aed3SPierre Pronchery         }
283*b077aed3SPierre Pronchery     }
284*b077aed3SPierre Pronchery     ret = 1;
285*b077aed3SPierre Pronchery end:
286*b077aed3SPierre Pronchery     OPENSSL_free(buf1);
287*b077aed3SPierre Pronchery     OPENSSL_free(buf2);
288*b077aed3SPierre Pronchery     NCONF_free(conf);
289*b077aed3SPierre Pronchery     return ret;
290*b077aed3SPierre Pronchery }
291*b077aed3SPierre Pronchery 
fipsinstall_main(int argc,char ** argv)292*b077aed3SPierre Pronchery int fipsinstall_main(int argc, char **argv)
293*b077aed3SPierre Pronchery {
294*b077aed3SPierre Pronchery     int ret = 1, verify = 0, gotkey = 0, gotdigest = 0, self_test_onload = 0;
295*b077aed3SPierre Pronchery     int enable_conditional_errors = 1, enable_security_checks = 1;
296*b077aed3SPierre Pronchery     const char *section_name = "fips_sect";
297*b077aed3SPierre Pronchery     const char *mac_name = "HMAC";
298*b077aed3SPierre Pronchery     const char *prov_name = "fips";
299*b077aed3SPierre Pronchery     BIO *module_bio = NULL, *mem_bio = NULL, *fout = NULL;
300*b077aed3SPierre Pronchery     char *in_fname = NULL, *out_fname = NULL, *prog;
301*b077aed3SPierre Pronchery     char *module_fname = NULL, *parent_config = NULL, *module_path = NULL;
302*b077aed3SPierre Pronchery     const char *tail;
303*b077aed3SPierre Pronchery     EVP_MAC_CTX *ctx = NULL, *ctx2 = NULL;
304*b077aed3SPierre Pronchery     STACK_OF(OPENSSL_STRING) *opts = NULL;
305*b077aed3SPierre Pronchery     OPTION_CHOICE o;
306*b077aed3SPierre Pronchery     unsigned char *read_buffer = NULL;
307*b077aed3SPierre Pronchery     unsigned char module_mac[EVP_MAX_MD_SIZE];
308*b077aed3SPierre Pronchery     size_t module_mac_len = EVP_MAX_MD_SIZE;
309*b077aed3SPierre Pronchery     unsigned char install_mac[EVP_MAX_MD_SIZE];
310*b077aed3SPierre Pronchery     size_t install_mac_len = EVP_MAX_MD_SIZE;
311*b077aed3SPierre Pronchery     EVP_MAC *mac = NULL;
312*b077aed3SPierre Pronchery     CONF *conf = NULL;
313*b077aed3SPierre Pronchery 
314*b077aed3SPierre Pronchery     if ((opts = sk_OPENSSL_STRING_new_null()) == NULL)
315*b077aed3SPierre Pronchery         goto end;
316*b077aed3SPierre Pronchery 
317*b077aed3SPierre Pronchery     prog = opt_init(argc, argv, fipsinstall_options);
318*b077aed3SPierre Pronchery     while ((o = opt_next()) != OPT_EOF) {
319*b077aed3SPierre Pronchery         switch (o) {
320*b077aed3SPierre Pronchery         case OPT_EOF:
321*b077aed3SPierre Pronchery         case OPT_ERR:
322*b077aed3SPierre Pronchery opthelp:
323*b077aed3SPierre Pronchery             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
324*b077aed3SPierre Pronchery             goto cleanup;
325*b077aed3SPierre Pronchery         case OPT_HELP:
326*b077aed3SPierre Pronchery             opt_help(fipsinstall_options);
327*b077aed3SPierre Pronchery             ret = 0;
328*b077aed3SPierre Pronchery             goto end;
329*b077aed3SPierre Pronchery         case OPT_IN:
330*b077aed3SPierre Pronchery             in_fname = opt_arg();
331*b077aed3SPierre Pronchery             break;
332*b077aed3SPierre Pronchery         case OPT_OUT:
333*b077aed3SPierre Pronchery             out_fname = opt_arg();
334*b077aed3SPierre Pronchery             break;
335*b077aed3SPierre Pronchery         case OPT_NO_CONDITIONAL_ERRORS:
336*b077aed3SPierre Pronchery             enable_conditional_errors = 0;
337*b077aed3SPierre Pronchery             break;
338*b077aed3SPierre Pronchery         case OPT_NO_SECURITY_CHECKS:
339*b077aed3SPierre Pronchery             enable_security_checks = 0;
340*b077aed3SPierre Pronchery             break;
341*b077aed3SPierre Pronchery         case OPT_QUIET:
342*b077aed3SPierre Pronchery             quiet = 1;
343*b077aed3SPierre Pronchery             /* FALLTHROUGH */
344*b077aed3SPierre Pronchery         case OPT_NO_LOG:
345*b077aed3SPierre Pronchery             self_test_log = 0;
346*b077aed3SPierre Pronchery             break;
347*b077aed3SPierre Pronchery         case OPT_CORRUPT_DESC:
348*b077aed3SPierre Pronchery             self_test_corrupt_desc = opt_arg();
349*b077aed3SPierre Pronchery             break;
350*b077aed3SPierre Pronchery         case OPT_CORRUPT_TYPE:
351*b077aed3SPierre Pronchery             self_test_corrupt_type = opt_arg();
352*b077aed3SPierre Pronchery             break;
353*b077aed3SPierre Pronchery         case OPT_PROV_NAME:
354*b077aed3SPierre Pronchery             prov_name = opt_arg();
355*b077aed3SPierre Pronchery             break;
356*b077aed3SPierre Pronchery         case OPT_MODULE:
357*b077aed3SPierre Pronchery             module_fname = opt_arg();
358*b077aed3SPierre Pronchery             break;
359*b077aed3SPierre Pronchery         case OPT_SECTION_NAME:
360*b077aed3SPierre Pronchery             section_name = opt_arg();
361*b077aed3SPierre Pronchery             break;
362*b077aed3SPierre Pronchery         case OPT_MAC_NAME:
363*b077aed3SPierre Pronchery             mac_name = opt_arg();
364*b077aed3SPierre Pronchery             break;
365*b077aed3SPierre Pronchery         case OPT_CONFIG:
366*b077aed3SPierre Pronchery             parent_config = opt_arg();
367*b077aed3SPierre Pronchery             break;
368*b077aed3SPierre Pronchery         case OPT_MACOPT:
369*b077aed3SPierre Pronchery             if (!sk_OPENSSL_STRING_push(opts, opt_arg()))
370*b077aed3SPierre Pronchery                 goto opthelp;
371*b077aed3SPierre Pronchery             if (strncmp(opt_arg(), "hexkey:", 7) == 0)
372*b077aed3SPierre Pronchery                 gotkey = 1;
373*b077aed3SPierre Pronchery             else if (strncmp(opt_arg(), "digest:", 7) == 0)
374*b077aed3SPierre Pronchery                 gotdigest = 1;
375*b077aed3SPierre Pronchery             break;
376*b077aed3SPierre Pronchery         case OPT_VERIFY:
377*b077aed3SPierre Pronchery             verify = 1;
378*b077aed3SPierre Pronchery             break;
379*b077aed3SPierre Pronchery         case OPT_SELF_TEST_ONLOAD:
380*b077aed3SPierre Pronchery             self_test_onload = 1;
381*b077aed3SPierre Pronchery             break;
382*b077aed3SPierre Pronchery         }
383*b077aed3SPierre Pronchery     }
384*b077aed3SPierre Pronchery 
385*b077aed3SPierre Pronchery     /* No extra arguments. */
386*b077aed3SPierre Pronchery     argc = opt_num_rest();
387*b077aed3SPierre Pronchery     if (argc != 0 || (verify && in_fname == NULL))
388*b077aed3SPierre Pronchery         goto opthelp;
389*b077aed3SPierre Pronchery 
390*b077aed3SPierre Pronchery     if (parent_config != NULL) {
391*b077aed3SPierre Pronchery         /* Test that a parent config can load the module */
392*b077aed3SPierre Pronchery         if (verify_module_load(parent_config)) {
393*b077aed3SPierre Pronchery             ret = OSSL_PROVIDER_available(NULL, prov_name) ? 0 : 1;
394*b077aed3SPierre Pronchery             if (!quiet)
395*b077aed3SPierre Pronchery                 BIO_printf(bio_err, "FIPS provider is %s\n",
396*b077aed3SPierre Pronchery                            ret == 0 ? "available" : " not available");
397*b077aed3SPierre Pronchery         }
398*b077aed3SPierre Pronchery         goto end;
399*b077aed3SPierre Pronchery     }
400*b077aed3SPierre Pronchery     if (module_fname == NULL)
401*b077aed3SPierre Pronchery         goto opthelp;
402*b077aed3SPierre Pronchery 
403*b077aed3SPierre Pronchery     tail = opt_path_end(module_fname);
404*b077aed3SPierre Pronchery     if (tail != NULL) {
405*b077aed3SPierre Pronchery         module_path = OPENSSL_strdup(module_fname);
406*b077aed3SPierre Pronchery         if (module_path == NULL)
407*b077aed3SPierre Pronchery             goto end;
408*b077aed3SPierre Pronchery         module_path[tail - module_fname] = '\0';
409*b077aed3SPierre Pronchery         if (!OSSL_PROVIDER_set_default_search_path(NULL, module_path))
410*b077aed3SPierre Pronchery             goto end;
411*b077aed3SPierre Pronchery     }
412*b077aed3SPierre Pronchery 
413*b077aed3SPierre Pronchery     if (self_test_log
414*b077aed3SPierre Pronchery             || self_test_corrupt_desc != NULL
415*b077aed3SPierre Pronchery             || self_test_corrupt_type != NULL)
416*b077aed3SPierre Pronchery         OSSL_SELF_TEST_set_callback(NULL, self_test_events, NULL);
417*b077aed3SPierre Pronchery 
418*b077aed3SPierre Pronchery     /* Use the default FIPS HMAC digest and key if not specified. */
419*b077aed3SPierre Pronchery     if (!gotdigest && !sk_OPENSSL_STRING_push(opts, "digest:SHA256"))
420*b077aed3SPierre Pronchery         goto end;
421*b077aed3SPierre Pronchery     if (!gotkey && !sk_OPENSSL_STRING_push(opts, "hexkey:" FIPS_KEY_STRING))
422*b077aed3SPierre Pronchery         goto end;
423*b077aed3SPierre Pronchery 
424*b077aed3SPierre Pronchery     module_bio = bio_open_default(module_fname, 'r', FORMAT_BINARY);
425*b077aed3SPierre Pronchery     if (module_bio == NULL) {
426*b077aed3SPierre Pronchery         BIO_printf(bio_err, "Failed to open module file\n");
427*b077aed3SPierre Pronchery         goto end;
428*b077aed3SPierre Pronchery     }
429*b077aed3SPierre Pronchery 
430*b077aed3SPierre Pronchery     read_buffer = app_malloc(BUFSIZE, "I/O buffer");
431*b077aed3SPierre Pronchery     if (read_buffer == NULL)
432*b077aed3SPierre Pronchery         goto end;
433*b077aed3SPierre Pronchery 
434*b077aed3SPierre Pronchery     mac = EVP_MAC_fetch(app_get0_libctx(), mac_name, app_get0_propq());
435*b077aed3SPierre Pronchery     if (mac == NULL) {
436*b077aed3SPierre Pronchery         BIO_printf(bio_err, "Unable to get MAC of type %s\n", mac_name);
437*b077aed3SPierre Pronchery         goto end;
438*b077aed3SPierre Pronchery     }
439*b077aed3SPierre Pronchery 
440*b077aed3SPierre Pronchery     ctx = EVP_MAC_CTX_new(mac);
441*b077aed3SPierre Pronchery     if (ctx == NULL) {
442*b077aed3SPierre Pronchery         BIO_printf(bio_err, "Unable to create MAC CTX for module check\n");
443*b077aed3SPierre Pronchery         goto end;
444*b077aed3SPierre Pronchery     }
445*b077aed3SPierre Pronchery 
446*b077aed3SPierre Pronchery     if (opts != NULL) {
447*b077aed3SPierre Pronchery         int ok = 1;
448*b077aed3SPierre Pronchery         OSSL_PARAM *params =
449*b077aed3SPierre Pronchery             app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac));
450*b077aed3SPierre Pronchery 
451*b077aed3SPierre Pronchery         if (params == NULL)
452*b077aed3SPierre Pronchery             goto end;
453*b077aed3SPierre Pronchery 
454*b077aed3SPierre Pronchery         if (!EVP_MAC_CTX_set_params(ctx, params)) {
455*b077aed3SPierre Pronchery             BIO_printf(bio_err, "MAC parameter error\n");
456*b077aed3SPierre Pronchery             ERR_print_errors(bio_err);
457*b077aed3SPierre Pronchery             ok = 0;
458*b077aed3SPierre Pronchery         }
459*b077aed3SPierre Pronchery         app_params_free(params);
460*b077aed3SPierre Pronchery         if (!ok)
461*b077aed3SPierre Pronchery             goto end;
462*b077aed3SPierre Pronchery     }
463*b077aed3SPierre Pronchery 
464*b077aed3SPierre Pronchery     ctx2 = EVP_MAC_CTX_dup(ctx);
465*b077aed3SPierre Pronchery     if (ctx2 == NULL) {
466*b077aed3SPierre Pronchery         BIO_printf(bio_err, "Unable to create MAC CTX for install indicator\n");
467*b077aed3SPierre Pronchery         goto end;
468*b077aed3SPierre Pronchery     }
469*b077aed3SPierre Pronchery 
470*b077aed3SPierre Pronchery     if (!do_mac(ctx, read_buffer, module_bio, module_mac, &module_mac_len))
471*b077aed3SPierre Pronchery         goto end;
472*b077aed3SPierre Pronchery 
473*b077aed3SPierre Pronchery     if (self_test_onload == 0) {
474*b077aed3SPierre Pronchery         mem_bio = BIO_new_mem_buf((const void *)INSTALL_STATUS_VAL,
475*b077aed3SPierre Pronchery                                   strlen(INSTALL_STATUS_VAL));
476*b077aed3SPierre Pronchery         if (mem_bio == NULL) {
477*b077aed3SPierre Pronchery             BIO_printf(bio_err, "Unable to create memory BIO\n");
478*b077aed3SPierre Pronchery             goto end;
479*b077aed3SPierre Pronchery         }
480*b077aed3SPierre Pronchery         if (!do_mac(ctx2, read_buffer, mem_bio, install_mac, &install_mac_len))
481*b077aed3SPierre Pronchery             goto end;
482*b077aed3SPierre Pronchery     } else {
483*b077aed3SPierre Pronchery         install_mac_len = 0;
484*b077aed3SPierre Pronchery     }
485*b077aed3SPierre Pronchery 
486*b077aed3SPierre Pronchery     if (verify) {
487*b077aed3SPierre Pronchery         if (!verify_config(in_fname, section_name, module_mac, module_mac_len,
488*b077aed3SPierre Pronchery                            install_mac, install_mac_len))
489*b077aed3SPierre Pronchery             goto end;
490*b077aed3SPierre Pronchery         if (!quiet)
491*b077aed3SPierre Pronchery             BIO_printf(bio_err, "VERIFY PASSED\n");
492*b077aed3SPierre Pronchery     } else {
493*b077aed3SPierre Pronchery 
494*b077aed3SPierre Pronchery         conf = generate_config_and_load(prov_name, section_name, module_mac,
495*b077aed3SPierre Pronchery                                         module_mac_len,
496*b077aed3SPierre Pronchery                                         enable_conditional_errors,
497*b077aed3SPierre Pronchery                                         enable_security_checks);
498*b077aed3SPierre Pronchery         if (conf == NULL)
499*b077aed3SPierre Pronchery             goto end;
500*b077aed3SPierre Pronchery         if (!load_fips_prov_and_run_self_test(prov_name))
501*b077aed3SPierre Pronchery             goto end;
502*b077aed3SPierre Pronchery 
503*b077aed3SPierre Pronchery         fout =
504*b077aed3SPierre Pronchery             out_fname == NULL ? dup_bio_out(FORMAT_TEXT)
505*b077aed3SPierre Pronchery                               : bio_open_default(out_fname, 'w', FORMAT_TEXT);
506*b077aed3SPierre Pronchery         if (fout == NULL) {
507*b077aed3SPierre Pronchery             BIO_printf(bio_err, "Failed to open file\n");
508*b077aed3SPierre Pronchery             goto end;
509*b077aed3SPierre Pronchery         }
510*b077aed3SPierre Pronchery         if (!write_config_fips_section(fout, section_name,
511*b077aed3SPierre Pronchery                                        module_mac, module_mac_len,
512*b077aed3SPierre Pronchery                                        enable_conditional_errors,
513*b077aed3SPierre Pronchery                                        enable_security_checks,
514*b077aed3SPierre Pronchery                                        install_mac, install_mac_len))
515*b077aed3SPierre Pronchery             goto end;
516*b077aed3SPierre Pronchery         if (!quiet)
517*b077aed3SPierre Pronchery             BIO_printf(bio_err, "INSTALL PASSED\n");
518*b077aed3SPierre Pronchery     }
519*b077aed3SPierre Pronchery 
520*b077aed3SPierre Pronchery     ret = 0;
521*b077aed3SPierre Pronchery end:
522*b077aed3SPierre Pronchery     if (ret == 1) {
523*b077aed3SPierre Pronchery         if (!quiet)
524*b077aed3SPierre Pronchery             BIO_printf(bio_err, "%s FAILED\n", verify ? "VERIFY" : "INSTALL");
525*b077aed3SPierre Pronchery         ERR_print_errors(bio_err);
526*b077aed3SPierre Pronchery     }
527*b077aed3SPierre Pronchery 
528*b077aed3SPierre Pronchery cleanup:
529*b077aed3SPierre Pronchery     OPENSSL_free(module_path);
530*b077aed3SPierre Pronchery     BIO_free(fout);
531*b077aed3SPierre Pronchery     BIO_free(mem_bio);
532*b077aed3SPierre Pronchery     BIO_free(module_bio);
533*b077aed3SPierre Pronchery     sk_OPENSSL_STRING_free(opts);
534*b077aed3SPierre Pronchery     EVP_MAC_free(mac);
535*b077aed3SPierre Pronchery     EVP_MAC_CTX_free(ctx2);
536*b077aed3SPierre Pronchery     EVP_MAC_CTX_free(ctx);
537*b077aed3SPierre Pronchery     OPENSSL_free(read_buffer);
538*b077aed3SPierre Pronchery     free_config_and_unload(conf);
539*b077aed3SPierre Pronchery     return ret;
540*b077aed3SPierre Pronchery }
541*b077aed3SPierre Pronchery 
self_test_events(const OSSL_PARAM params[],void * arg)542*b077aed3SPierre Pronchery static int self_test_events(const OSSL_PARAM params[], void *arg)
543*b077aed3SPierre Pronchery {
544*b077aed3SPierre Pronchery     const OSSL_PARAM *p = NULL;
545*b077aed3SPierre Pronchery     const char *phase = NULL, *type = NULL, *desc = NULL;
546*b077aed3SPierre Pronchery     int ret = 0;
547*b077aed3SPierre Pronchery 
548*b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_PHASE);
549*b077aed3SPierre Pronchery     if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
550*b077aed3SPierre Pronchery         goto err;
551*b077aed3SPierre Pronchery     phase = (const char *)p->data;
552*b077aed3SPierre Pronchery 
553*b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_DESC);
554*b077aed3SPierre Pronchery     if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
555*b077aed3SPierre Pronchery         goto err;
556*b077aed3SPierre Pronchery     desc = (const char *)p->data;
557*b077aed3SPierre Pronchery 
558*b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_TYPE);
559*b077aed3SPierre Pronchery     if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
560*b077aed3SPierre Pronchery         goto err;
561*b077aed3SPierre Pronchery     type = (const char *)p->data;
562*b077aed3SPierre Pronchery 
563*b077aed3SPierre Pronchery     if (self_test_log) {
564*b077aed3SPierre Pronchery         if (strcmp(phase, OSSL_SELF_TEST_PHASE_START) == 0)
565*b077aed3SPierre Pronchery             BIO_printf(bio_err, "%s : (%s) : ", desc, type);
566*b077aed3SPierre Pronchery         else if (strcmp(phase, OSSL_SELF_TEST_PHASE_PASS) == 0
567*b077aed3SPierre Pronchery                  || strcmp(phase, OSSL_SELF_TEST_PHASE_FAIL) == 0)
568*b077aed3SPierre Pronchery             BIO_printf(bio_err, "%s\n", phase);
569*b077aed3SPierre Pronchery     }
570*b077aed3SPierre Pronchery     /*
571*b077aed3SPierre Pronchery      * The self test code will internally corrupt the KAT test result if an
572*b077aed3SPierre Pronchery      * error is returned during the corrupt phase.
573*b077aed3SPierre Pronchery      */
574*b077aed3SPierre Pronchery     if (strcmp(phase, OSSL_SELF_TEST_PHASE_CORRUPT) == 0
575*b077aed3SPierre Pronchery             && (self_test_corrupt_desc != NULL
576*b077aed3SPierre Pronchery                 || self_test_corrupt_type != NULL)) {
577*b077aed3SPierre Pronchery         if (self_test_corrupt_desc != NULL
578*b077aed3SPierre Pronchery                 && strcmp(self_test_corrupt_desc, desc) != 0)
579*b077aed3SPierre Pronchery             goto end;
580*b077aed3SPierre Pronchery         if (self_test_corrupt_type != NULL
581*b077aed3SPierre Pronchery                 && strcmp(self_test_corrupt_type, type) != 0)
582*b077aed3SPierre Pronchery             goto end;
583*b077aed3SPierre Pronchery         BIO_printf(bio_err, "%s ", phase);
584*b077aed3SPierre Pronchery         goto err;
585*b077aed3SPierre Pronchery     }
586*b077aed3SPierre Pronchery end:
587*b077aed3SPierre Pronchery     ret = 1;
588*b077aed3SPierre Pronchery err:
589*b077aed3SPierre Pronchery     return ret;
590*b077aed3SPierre Pronchery }
591