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