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