Home | History | Annotate | Line # | Download | only in apps
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  *
      4      1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6      1.1  christos  * in the file LICENSE in the source distribution or at
      7      1.1  christos  * https://www.openssl.org/source/license.html
      8      1.1  christos  */
      9      1.1  christos 
     10      1.1  christos #include <openssl/evp.h>
     11      1.1  christos #include <openssl/err.h>
     12      1.1  christos #include <openssl/provider.h>
     13      1.1  christos #include <openssl/params.h>
     14      1.1  christos #include <openssl/fips_names.h>
     15      1.1  christos #include <openssl/core_names.h>
     16      1.1  christos #include <openssl/self_test.h>
     17      1.1  christos #include <openssl/fipskey.h>
     18      1.1  christos #include "apps.h"
     19      1.1  christos #include "progs.h"
     20      1.1  christos 
     21      1.1  christos #define BUFSIZE 4096
     22      1.1  christos 
     23      1.1  christos /* Configuration file values */
     24  1.1.1.2  christos #define VERSION_KEY "version"
     25  1.1.1.2  christos #define VERSION_VAL "1"
     26      1.1  christos #define INSTALL_STATUS_VAL "INSTALL_SELF_TEST_KATS_RUN"
     27      1.1  christos 
     28      1.1  christos static OSSL_CALLBACK self_test_events;
     29      1.1  christos static char *self_test_corrupt_desc = NULL;
     30      1.1  christos static char *self_test_corrupt_type = NULL;
     31      1.1  christos static int self_test_log = 1;
     32      1.1  christos static int quiet = 0;
     33      1.1  christos 
     34      1.1  christos typedef enum OPTION_choice {
     35      1.1  christos     OPT_COMMON,
     36  1.1.1.2  christos     OPT_IN,
     37  1.1.1.2  christos     OPT_OUT,
     38  1.1.1.2  christos     OPT_MODULE,
     39  1.1.1.2  christos     OPT_PEDANTIC,
     40  1.1.1.2  christos     OPT_PROV_NAME,
     41  1.1.1.2  christos     OPT_SECTION_NAME,
     42  1.1.1.2  christos     OPT_MAC_NAME,
     43  1.1.1.2  christos     OPT_MACOPT,
     44  1.1.1.2  christos     OPT_VERIFY,
     45  1.1.1.2  christos     OPT_NO_LOG,
     46  1.1.1.2  christos     OPT_CORRUPT_DESC,
     47  1.1.1.2  christos     OPT_CORRUPT_TYPE,
     48  1.1.1.2  christos     OPT_QUIET,
     49  1.1.1.2  christos     OPT_CONFIG,
     50      1.1  christos     OPT_NO_CONDITIONAL_ERRORS,
     51      1.1  christos     OPT_NO_SECURITY_CHECKS,
     52  1.1.1.2  christos     OPT_TLS_PRF_EMS_CHECK,
     53  1.1.1.2  christos     OPT_NO_SHORT_MAC,
     54  1.1.1.2  christos     OPT_DISALLOW_PKCS15_PADDING,
     55  1.1.1.2  christos     OPT_RSA_PSS_SALTLEN_CHECK,
     56      1.1  christos     OPT_DISALLOW_SIGNATURE_X931_PADDING,
     57  1.1.1.2  christos     OPT_HMAC_KEY_CHECK,
     58  1.1.1.2  christos     OPT_KMAC_KEY_CHECK,
     59      1.1  christos     OPT_DISALLOW_DRGB_TRUNC_DIGEST,
     60      1.1  christos     OPT_SIGNATURE_DIGEST_CHECK,
     61      1.1  christos     OPT_HKDF_DIGEST_CHECK,
     62      1.1  christos     OPT_TLS13_KDF_DIGEST_CHECK,
     63      1.1  christos     OPT_TLS1_PRF_DIGEST_CHECK,
     64      1.1  christos     OPT_SSHKDF_DIGEST_CHECK,
     65      1.1  christos     OPT_SSKDF_DIGEST_CHECK,
     66      1.1  christos     OPT_X963KDF_DIGEST_CHECK,
     67      1.1  christos     OPT_DISALLOW_DSA_SIGN,
     68      1.1  christos     OPT_DISALLOW_TDES_ENCRYPT,
     69      1.1  christos     OPT_HKDF_KEY_CHECK,
     70      1.1  christos     OPT_KBKDF_KEY_CHECK,
     71      1.1  christos     OPT_TLS13_KDF_KEY_CHECK,
     72      1.1  christos     OPT_TLS1_PRF_KEY_CHECK,
     73      1.1  christos     OPT_SSHKDF_KEY_CHECK,
     74      1.1  christos     OPT_SSKDF_KEY_CHECK,
     75      1.1  christos     OPT_X963KDF_KEY_CHECK,
     76      1.1  christos     OPT_X942KDF_KEY_CHECK,
     77      1.1  christos     OPT_NO_PBKDF2_LOWER_BOUND_CHECK,
     78      1.1  christos     OPT_ECDH_COFACTOR_CHECK,
     79  1.1.1.2  christos     OPT_SELF_TEST_ONLOAD,
     80  1.1.1.2  christos     OPT_SELF_TEST_ONINSTALL
     81      1.1  christos } OPTION_CHOICE;
     82      1.1  christos 
     83      1.1  christos const OPTIONS fipsinstall_options[] = {
     84      1.1  christos     OPT_SECTION("General"),
     85  1.1.1.2  christos     { "help", OPT_HELP, '-', "Display this summary" },
     86  1.1.1.2  christos     { "pedantic", OPT_PEDANTIC, '-', "Set options for strict FIPS compliance" },
     87  1.1.1.2  christos     { "verify", OPT_VERIFY, '-',
     88  1.1.1.2  christos         "Verify a config file instead of generating one" },
     89  1.1.1.2  christos     { "module", OPT_MODULE, '<', "File name of the provider module" },
     90  1.1.1.2  christos     { "provider_name", OPT_PROV_NAME, 's', "FIPS provider name" },
     91  1.1.1.2  christos     { "section_name", OPT_SECTION_NAME, 's',
     92  1.1.1.2  christos         "FIPS Provider config section name (optional)" },
     93  1.1.1.2  christos     { "no_conditional_errors", OPT_NO_CONDITIONAL_ERRORS, '-',
     94  1.1.1.2  christos         "Disable the ability of the fips module to enter an error state if"
     95  1.1.1.2  christos         " any conditional self tests fail" },
     96  1.1.1.2  christos     { "no_security_checks", OPT_NO_SECURITY_CHECKS, '-',
     97  1.1.1.2  christos         "Disable the run-time FIPS security checks in the module" },
     98  1.1.1.2  christos     { "self_test_onload", OPT_SELF_TEST_ONLOAD, '-',
     99  1.1.1.2  christos         "Forces self tests to always run on module load" },
    100  1.1.1.2  christos     { "self_test_oninstall", OPT_SELF_TEST_ONINSTALL, '-',
    101  1.1.1.2  christos         "Forces self tests to run once on module installation" },
    102  1.1.1.2  christos     { "ems_check", OPT_TLS_PRF_EMS_CHECK, '-',
    103  1.1.1.2  christos         "Enable the run-time FIPS check for EMS during TLS1_PRF" },
    104  1.1.1.2  christos     { "no_short_mac", OPT_NO_SHORT_MAC, '-', "Disallow short MAC output" },
    105  1.1.1.2  christos     { "no_drbg_truncated_digests", OPT_DISALLOW_DRGB_TRUNC_DIGEST, '-',
    106  1.1.1.2  christos         "Disallow truncated digests with Hash and HMAC DRBGs" },
    107  1.1.1.2  christos     { "signature_digest_check", OPT_SIGNATURE_DIGEST_CHECK, '-',
    108  1.1.1.2  christos         "Enable checking for approved digests for signatures" },
    109  1.1.1.2  christos     { "hmac_key_check", OPT_HMAC_KEY_CHECK, '-', "Enable key check for HMAC" },
    110  1.1.1.2  christos     { "kmac_key_check", OPT_KMAC_KEY_CHECK, '-', "Enable key check for KMAC" },
    111  1.1.1.2  christos     { "hkdf_digest_check", OPT_HKDF_DIGEST_CHECK, '-',
    112  1.1.1.2  christos         "Enable digest check for HKDF" },
    113  1.1.1.2  christos     { "tls13_kdf_digest_check", OPT_TLS13_KDF_DIGEST_CHECK, '-',
    114  1.1.1.2  christos         "Enable digest check for TLS13-KDF" },
    115  1.1.1.2  christos     { "tls1_prf_digest_check", OPT_TLS1_PRF_DIGEST_CHECK, '-',
    116  1.1.1.2  christos         "Enable digest check for TLS1-PRF" },
    117  1.1.1.2  christos     { "sshkdf_digest_check", OPT_SSHKDF_DIGEST_CHECK, '-',
    118  1.1.1.2  christos         "Enable digest check for SSHKDF" },
    119  1.1.1.2  christos     { "sskdf_digest_check", OPT_SSKDF_DIGEST_CHECK, '-',
    120  1.1.1.2  christos         "Enable digest check for SSKDF" },
    121  1.1.1.2  christos     { "x963kdf_digest_check", OPT_X963KDF_DIGEST_CHECK, '-',
    122  1.1.1.2  christos         "Enable digest check for X963KDF" },
    123  1.1.1.2  christos     { "dsa_sign_disabled", OPT_DISALLOW_DSA_SIGN, '-',
    124  1.1.1.2  christos         "Disallow DSA signing" },
    125  1.1.1.2  christos     { "tdes_encrypt_disabled", OPT_DISALLOW_TDES_ENCRYPT, '-',
    126  1.1.1.2  christos         "Disallow Triple-DES encryption" },
    127  1.1.1.2  christos     { "rsa_pkcs15_padding_disabled", OPT_DISALLOW_PKCS15_PADDING, '-',
    128  1.1.1.2  christos         "Disallow PKCS#1 version 1.5 padding for RSA encryption" },
    129  1.1.1.2  christos     { "rsa_pss_saltlen_check", OPT_RSA_PSS_SALTLEN_CHECK, '-',
    130  1.1.1.2  christos         "Enable salt length check for RSA-PSS signature operations" },
    131  1.1.1.2  christos     { "rsa_sign_x931_disabled", OPT_DISALLOW_SIGNATURE_X931_PADDING, '-',
    132  1.1.1.2  christos         "Disallow X931 Padding for RSA signing" },
    133  1.1.1.2  christos     { "hkdf_key_check", OPT_HKDF_KEY_CHECK, '-',
    134  1.1.1.2  christos         "Enable key check for HKDF" },
    135  1.1.1.2  christos     { "kbkdf_key_check", OPT_KBKDF_KEY_CHECK, '-',
    136  1.1.1.2  christos         "Enable key check for KBKDF" },
    137  1.1.1.2  christos     { "tls13_kdf_key_check", OPT_TLS13_KDF_KEY_CHECK, '-',
    138  1.1.1.2  christos         "Enable key check for TLS13-KDF" },
    139  1.1.1.2  christos     { "tls1_prf_key_check", OPT_TLS1_PRF_KEY_CHECK, '-',
    140  1.1.1.2  christos         "Enable key check for TLS1-PRF" },
    141  1.1.1.2  christos     { "sshkdf_key_check", OPT_SSHKDF_KEY_CHECK, '-',
    142  1.1.1.2  christos         "Enable key check for SSHKDF" },
    143  1.1.1.2  christos     { "sskdf_key_check", OPT_SSKDF_KEY_CHECK, '-',
    144  1.1.1.2  christos         "Enable key check for SSKDF" },
    145  1.1.1.2  christos     { "x963kdf_key_check", OPT_X963KDF_KEY_CHECK, '-',
    146  1.1.1.2  christos         "Enable key check for X963KDF" },
    147  1.1.1.2  christos     { "x942kdf_key_check", OPT_X942KDF_KEY_CHECK, '-',
    148  1.1.1.2  christos         "Enable key check for X942KDF" },
    149  1.1.1.2  christos     { "no_pbkdf2_lower_bound_check", OPT_NO_PBKDF2_LOWER_BOUND_CHECK, '-',
    150  1.1.1.2  christos         "Disable lower bound check for PBKDF2" },
    151  1.1.1.2  christos     { "ecdh_cofactor_check", OPT_ECDH_COFACTOR_CHECK, '-',
    152  1.1.1.2  christos         "Enable Cofactor check for ECDH" },
    153      1.1  christos     OPT_SECTION("Input"),
    154  1.1.1.2  christos     { "in", OPT_IN, '<', "Input config file, used when verifying" },
    155      1.1  christos 
    156      1.1  christos     OPT_SECTION("Output"),
    157  1.1.1.2  christos     { "out", OPT_OUT, '>', "Output config file, used when generating" },
    158  1.1.1.2  christos     { "mac_name", OPT_MAC_NAME, 's', "MAC name" },
    159  1.1.1.2  christos     { "macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form." },
    160  1.1.1.2  christos     { OPT_MORE_STR, 0, 0, "See 'PARAMETER NAMES' in the EVP_MAC_ docs" },
    161  1.1.1.2  christos     { "noout", OPT_NO_LOG, '-', "Disable logging of self test events" },
    162  1.1.1.2  christos     { "corrupt_desc", OPT_CORRUPT_DESC, 's', "Corrupt a self test by description" },
    163  1.1.1.2  christos     { "corrupt_type", OPT_CORRUPT_TYPE, 's', "Corrupt a self test by type" },
    164  1.1.1.2  christos     { "config", OPT_CONFIG, '<', "The parent config to verify" },
    165  1.1.1.2  christos     { "quiet", OPT_QUIET, '-', "No messages, just exit status" },
    166  1.1.1.2  christos     { NULL }
    167      1.1  christos };
    168      1.1  christos 
    169      1.1  christos typedef struct {
    170      1.1  christos     unsigned int self_test_onload : 1;
    171      1.1  christos     unsigned int conditional_errors : 1;
    172      1.1  christos     unsigned int security_checks : 1;
    173      1.1  christos     unsigned int hmac_key_check : 1;
    174      1.1  christos     unsigned int kmac_key_check : 1;
    175      1.1  christos     unsigned int tls_prf_ems_check : 1;
    176      1.1  christos     unsigned int no_short_mac : 1;
    177      1.1  christos     unsigned int drgb_no_trunc_dgst : 1;
    178      1.1  christos     unsigned int signature_digest_check : 1;
    179      1.1  christos     unsigned int hkdf_digest_check : 1;
    180      1.1  christos     unsigned int tls13_kdf_digest_check : 1;
    181      1.1  christos     unsigned int tls1_prf_digest_check : 1;
    182      1.1  christos     unsigned int sshkdf_digest_check : 1;
    183      1.1  christos     unsigned int sskdf_digest_check : 1;
    184      1.1  christos     unsigned int x963kdf_digest_check : 1;
    185      1.1  christos     unsigned int dsa_sign_disabled : 1;
    186      1.1  christos     unsigned int tdes_encrypt_disabled : 1;
    187      1.1  christos     unsigned int rsa_pkcs15_padding_disabled : 1;
    188      1.1  christos     unsigned int rsa_pss_saltlen_check : 1;
    189      1.1  christos     unsigned int sign_x931_padding_disabled : 1;
    190      1.1  christos     unsigned int hkdf_key_check : 1;
    191      1.1  christos     unsigned int kbkdf_key_check : 1;
    192      1.1  christos     unsigned int tls13_kdf_key_check : 1;
    193      1.1  christos     unsigned int tls1_prf_key_check : 1;
    194      1.1  christos     unsigned int sshkdf_key_check : 1;
    195      1.1  christos     unsigned int sskdf_key_check : 1;
    196      1.1  christos     unsigned int x963kdf_key_check : 1;
    197      1.1  christos     unsigned int x942kdf_key_check : 1;
    198      1.1  christos     unsigned int pbkdf2_lower_bound_check : 1;
    199      1.1  christos     unsigned int ecdh_cofactor_check : 1;
    200      1.1  christos } FIPS_OPTS;
    201      1.1  christos 
    202      1.1  christos /* Pedantic FIPS compliance */
    203      1.1  christos static const FIPS_OPTS pedantic_opts = {
    204  1.1.1.2  christos     1, /* self_test_onload */
    205  1.1.1.2  christos     1, /* conditional_errors */
    206  1.1.1.2  christos     1, /* security_checks */
    207  1.1.1.2  christos     1, /* hmac_key_check */
    208  1.1.1.2  christos     1, /* kmac_key_check */
    209  1.1.1.2  christos     1, /* tls_prf_ems_check */
    210  1.1.1.2  christos     1, /* no_short_mac */
    211  1.1.1.2  christos     1, /* drgb_no_trunc_dgst */
    212  1.1.1.2  christos     1, /* signature_digest_check */
    213  1.1.1.2  christos     1, /* hkdf_digest_check */
    214  1.1.1.2  christos     1, /* tls13_kdf_digest_check */
    215  1.1.1.2  christos     1, /* tls1_prf_digest_check */
    216  1.1.1.2  christos     1, /* sshkdf_digest_check */
    217  1.1.1.2  christos     1, /* sskdf_digest_check */
    218  1.1.1.2  christos     1, /* x963kdf_digest_check */
    219  1.1.1.2  christos     1, /* dsa_sign_disabled */
    220  1.1.1.2  christos     1, /* tdes_encrypt_disabled */
    221  1.1.1.2  christos     1, /* rsa_pkcs15_padding_disabled */
    222  1.1.1.2  christos     1, /* rsa_pss_saltlen_check */
    223  1.1.1.2  christos     1, /* sign_x931_padding_disabled */
    224  1.1.1.2  christos     1, /* hkdf_key_check */
    225  1.1.1.2  christos     1, /* kbkdf_key_check */
    226  1.1.1.2  christos     1, /* tls13_kdf_key_check */
    227  1.1.1.2  christos     1, /* tls1_prf_key_check */
    228  1.1.1.2  christos     1, /* sshkdf_key_check */
    229  1.1.1.2  christos     1, /* sskdf_key_check */
    230  1.1.1.2  christos     1, /* x963kdf_key_check */
    231  1.1.1.2  christos     1, /* x942kdf_key_check */
    232  1.1.1.2  christos     1, /* pbkdf2_lower_bound_check */
    233  1.1.1.2  christos     1, /* ecdh_cofactor_check */
    234      1.1  christos };
    235      1.1  christos 
    236      1.1  christos /* Default FIPS settings for backward compatibility */
    237      1.1  christos static FIPS_OPTS fips_opts = {
    238  1.1.1.2  christos     1, /* self_test_onload */
    239  1.1.1.2  christos     1, /* conditional_errors */
    240  1.1.1.2  christos     1, /* security_checks */
    241  1.1.1.2  christos     0, /* hmac_key_check */
    242  1.1.1.2  christos     0, /* kmac_key_check */
    243  1.1.1.2  christos     0, /* tls_prf_ems_check */
    244  1.1.1.2  christos     0, /* no_short_mac */
    245  1.1.1.2  christos     0, /* drgb_no_trunc_dgst */
    246  1.1.1.2  christos     0, /* signature_digest_check */
    247  1.1.1.2  christos     0, /* hkdf_digest_check */
    248  1.1.1.2  christos     0, /* tls13_kdf_digest_check */
    249  1.1.1.2  christos     0, /* tls1_prf_digest_check */
    250  1.1.1.2  christos     0, /* sshkdf_digest_check */
    251  1.1.1.2  christos     0, /* sskdf_digest_check */
    252  1.1.1.2  christos     0, /* x963kdf_digest_check */
    253  1.1.1.2  christos     0, /* dsa_sign_disabled */
    254  1.1.1.2  christos     0, /* tdes_encrypt_disabled */
    255  1.1.1.2  christos     0, /* rsa_pkcs15_padding_disabled */
    256  1.1.1.2  christos     0, /* rsa_pss_saltlen_check */
    257  1.1.1.2  christos     0, /* sign_x931_padding_disabled */
    258  1.1.1.2  christos     0, /* hkdf_key_check */
    259  1.1.1.2  christos     0, /* kbkdf_key_check */
    260  1.1.1.2  christos     0, /* tls13_kdf_key_check */
    261  1.1.1.2  christos     0, /* tls1_prf_key_check */
    262  1.1.1.2  christos     0, /* sshkdf_key_check */
    263  1.1.1.2  christos     0, /* sskdf_key_check */
    264  1.1.1.2  christos     0, /* x963kdf_key_check */
    265  1.1.1.2  christos     0, /* x942kdf_key_check */
    266  1.1.1.2  christos     1, /* pbkdf2_lower_bound_check */
    267  1.1.1.2  christos     0, /* ecdh_cofactor_check */
    268      1.1  christos };
    269      1.1  christos 
    270      1.1  christos static int check_non_pedantic_fips(int pedantic, const char *name)
    271      1.1  christos {
    272      1.1  christos     if (pedantic) {
    273      1.1  christos         BIO_printf(bio_err, "Cannot specify -%s after -pedantic\n", name);
    274      1.1  christos         return 0;
    275      1.1  christos     }
    276      1.1  christos     return 1;
    277      1.1  christos }
    278      1.1  christos 
    279      1.1  christos static int do_mac(EVP_MAC_CTX *ctx, unsigned char *tmp, BIO *in,
    280  1.1.1.2  christos     unsigned char *out, size_t *out_len)
    281      1.1  christos {
    282      1.1  christos     int ret = 0;
    283      1.1  christos     int i;
    284      1.1  christos     size_t outsz = *out_len;
    285      1.1  christos 
    286      1.1  christos     if (!EVP_MAC_init(ctx, NULL, 0, NULL))
    287      1.1  christos         goto err;
    288      1.1  christos     if (EVP_MAC_CTX_get_mac_size(ctx) > outsz)
    289      1.1  christos         goto end;
    290      1.1  christos     while ((i = BIO_read(in, (char *)tmp, BUFSIZE)) != 0) {
    291      1.1  christos         if (i < 0 || !EVP_MAC_update(ctx, tmp, i))
    292      1.1  christos             goto err;
    293      1.1  christos     }
    294      1.1  christos end:
    295      1.1  christos     if (!EVP_MAC_final(ctx, out, out_len, outsz))
    296      1.1  christos         goto err;
    297      1.1  christos     ret = 1;
    298      1.1  christos err:
    299      1.1  christos     return ret;
    300      1.1  christos }
    301      1.1  christos 
    302      1.1  christos static int load_fips_prov_and_run_self_test(const char *prov_name,
    303  1.1.1.2  christos     int *is_fips_140_2_prov)
    304      1.1  christos {
    305      1.1  christos     int ret = 0;
    306      1.1  christos     OSSL_PROVIDER *prov = NULL;
    307      1.1  christos     OSSL_PARAM params[4], *p = params;
    308      1.1  christos     char *name = "", *vers = "", *build = "";
    309      1.1  christos 
    310      1.1  christos     prov = OSSL_PROVIDER_load(NULL, prov_name);
    311      1.1  christos     if (prov == NULL) {
    312      1.1  christos         BIO_printf(bio_err, "Failed to load FIPS module\n");
    313      1.1  christos         goto end;
    314      1.1  christos     }
    315      1.1  christos     if (!quiet) {
    316      1.1  christos         *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_NAME,
    317  1.1.1.2  christos             &name, sizeof(name));
    318      1.1  christos         *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_VERSION,
    319  1.1.1.2  christos             &vers, sizeof(vers));
    320      1.1  christos         *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_BUILDINFO,
    321  1.1.1.2  christos             &build, sizeof(build));
    322      1.1  christos         *p = OSSL_PARAM_construct_end();
    323      1.1  christos         if (!OSSL_PROVIDER_get_params(prov, params)) {
    324      1.1  christos             BIO_printf(bio_err, "Failed to query FIPS module parameters\n");
    325      1.1  christos             goto end;
    326      1.1  christos         }
    327      1.1  christos         if (OSSL_PARAM_modified(params))
    328      1.1  christos             BIO_printf(bio_err, "\t%-10s\t%s\n", "name:", name);
    329      1.1  christos         if (OSSL_PARAM_modified(params + 1))
    330      1.1  christos             BIO_printf(bio_err, "\t%-10s\t%s\n", "version:", vers);
    331      1.1  christos         if (OSSL_PARAM_modified(params + 2))
    332      1.1  christos             BIO_printf(bio_err, "\t%-10s\t%s\n", "build:", build);
    333      1.1  christos     } else {
    334      1.1  christos         *p++ = OSSL_PARAM_construct_utf8_ptr(OSSL_PROV_PARAM_VERSION,
    335  1.1.1.2  christos             &vers, sizeof(vers));
    336      1.1  christos         *p = OSSL_PARAM_construct_end();
    337      1.1  christos         if (!OSSL_PROVIDER_get_params(prov, params)) {
    338      1.1  christos             BIO_printf(bio_err, "Failed to query FIPS module parameters\n");
    339      1.1  christos             goto end;
    340      1.1  christos         }
    341      1.1  christos     }
    342      1.1  christos     *is_fips_140_2_prov = (strncmp("3.0.", vers, 4) == 0);
    343      1.1  christos     ret = 1;
    344      1.1  christos end:
    345      1.1  christos     OSSL_PROVIDER_unload(prov);
    346      1.1  christos     return ret;
    347      1.1  christos }
    348      1.1  christos 
    349      1.1  christos static int print_mac(BIO *bio, const char *label, const unsigned char *mac,
    350  1.1.1.2  christos     size_t len)
    351      1.1  christos {
    352      1.1  christos     int ret;
    353      1.1  christos     char *hexstr = NULL;
    354      1.1  christos 
    355      1.1  christos     hexstr = OPENSSL_buf2hexstr(mac, (long)len);
    356      1.1  christos     if (hexstr == NULL)
    357      1.1  christos         return 0;
    358      1.1  christos     ret = BIO_printf(bio, "%s = %s\n", label, hexstr);
    359      1.1  christos     OPENSSL_free(hexstr);
    360      1.1  christos     return ret;
    361      1.1  christos }
    362      1.1  christos 
    363      1.1  christos static int write_config_header(BIO *out, const char *prov_name,
    364  1.1.1.2  christos     const char *section)
    365      1.1  christos {
    366      1.1  christos     return BIO_printf(out, "openssl_conf = openssl_init\n\n")
    367  1.1.1.2  christos         && BIO_printf(out, "[openssl_init]\n")
    368  1.1.1.2  christos         && BIO_printf(out, "providers = provider_section\n\n")
    369  1.1.1.2  christos         && BIO_printf(out, "[provider_section]\n")
    370  1.1.1.2  christos         && BIO_printf(out, "%s = %s\n\n", prov_name, section);
    371      1.1  christos }
    372      1.1  christos 
    373      1.1  christos /*
    374      1.1  christos  * Outputs a fips related config file that contains entries for the fips
    375      1.1  christos  * module checksum, installation indicator checksum and the options
    376      1.1  christos  * conditional_errors and security_checks.
    377      1.1  christos  *
    378      1.1  christos  * Returns 1 if the config file is written otherwise it returns 0 on error.
    379      1.1  christos  */
    380      1.1  christos static int write_config_fips_section(BIO *out, const char *section,
    381  1.1.1.2  christos     unsigned char *module_mac,
    382  1.1.1.2  christos     size_t module_mac_len,
    383  1.1.1.2  christos     const FIPS_OPTS *opts,
    384  1.1.1.2  christos     unsigned char *install_mac,
    385  1.1.1.2  christos     size_t install_mac_len)
    386      1.1  christos {
    387      1.1  christos     int ret = 0;
    388      1.1  christos 
    389      1.1  christos     if (BIO_printf(out, "[%s]\n", section) <= 0
    390      1.1  christos         || BIO_printf(out, "activate = 1\n") <= 0
    391      1.1  christos         || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_VERSION,
    392  1.1.1.2  christos                VERSION_VAL)
    393  1.1.1.2  christos             <= 0
    394      1.1  christos         || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_CONDITIONAL_ERRORS,
    395  1.1.1.2  christos                opts->conditional_errors ? "1" : "0")
    396  1.1.1.2  christos             <= 0
    397      1.1  christos         || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_SECURITY_CHECKS,
    398  1.1.1.2  christos                opts->security_checks ? "1" : "0")
    399  1.1.1.2  christos             <= 0
    400      1.1  christos         || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_HMAC_KEY_CHECK,
    401  1.1.1.2  christos                opts->hmac_key_check ? "1" : "0")
    402  1.1.1.2  christos             <= 0
    403      1.1  christos         || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_KMAC_KEY_CHECK,
    404  1.1.1.2  christos                opts->kmac_key_check ? "1" : "0")
    405  1.1.1.2  christos             <= 0
    406      1.1  christos         || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_TLS1_PRF_EMS_CHECK,
    407  1.1.1.2  christos                opts->tls_prf_ems_check ? "1" : "0")
    408  1.1.1.2  christos             <= 0
    409      1.1  christos         || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_NO_SHORT_MAC,
    410  1.1.1.2  christos                opts->no_short_mac ? "1" : "0")
    411  1.1.1.2  christos             <= 0
    412      1.1  christos         || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_DRBG_TRUNC_DIGEST,
    413  1.1.1.2  christos                opts->drgb_no_trunc_dgst ? "1" : "0")
    414  1.1.1.2  christos             <= 0
    415      1.1  christos         || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_SIGNATURE_DIGEST_CHECK,
    416  1.1.1.2  christos                opts->signature_digest_check ? "1" : "0")
    417  1.1.1.2  christos             <= 0
    418      1.1  christos         || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_HKDF_DIGEST_CHECK,
    419  1.1.1.2  christos                opts->hkdf_digest_check ? "1" : "0")
    420  1.1.1.2  christos             <= 0
    421      1.1  christos         || BIO_printf(out, "%s = %s\n",
    422  1.1.1.2  christos                OSSL_PROV_PARAM_TLS13_KDF_DIGEST_CHECK,
    423  1.1.1.2  christos                opts->tls13_kdf_digest_check ? "1" : "0")
    424  1.1.1.2  christos             <= 0
    425      1.1  christos         || BIO_printf(out, "%s = %s\n",
    426  1.1.1.2  christos                OSSL_PROV_PARAM_TLS1_PRF_DIGEST_CHECK,
    427  1.1.1.2  christos                opts->tls1_prf_digest_check ? "1" : "0")
    428  1.1.1.2  christos             <= 0
    429      1.1  christos         || BIO_printf(out, "%s = %s\n",
    430  1.1.1.2  christos                OSSL_PROV_PARAM_SSHKDF_DIGEST_CHECK,
    431  1.1.1.2  christos                opts->sshkdf_digest_check ? "1" : "0")
    432  1.1.1.2  christos             <= 0
    433      1.1  christos         || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_SSKDF_DIGEST_CHECK,
    434  1.1.1.2  christos                opts->sskdf_digest_check ? "1" : "0")
    435  1.1.1.2  christos             <= 0
    436      1.1  christos         || BIO_printf(out, "%s = %s\n",
    437  1.1.1.2  christos                OSSL_PROV_PARAM_X963KDF_DIGEST_CHECK,
    438  1.1.1.2  christos                opts->x963kdf_digest_check ? "1" : "0")
    439  1.1.1.2  christos             <= 0
    440      1.1  christos         || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_DSA_SIGN_DISABLED,
    441  1.1.1.2  christos                opts->dsa_sign_disabled ? "1" : "0")
    442  1.1.1.2  christos             <= 0
    443      1.1  christos         || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_TDES_ENCRYPT_DISABLED,
    444  1.1.1.2  christos                opts->tdes_encrypt_disabled ? "1" : "0")
    445  1.1.1.2  christos             <= 0
    446      1.1  christos         || BIO_printf(out, "%s = %s\n",
    447  1.1.1.2  christos                OSSL_PROV_PARAM_RSA_PKCS15_PAD_DISABLED,
    448  1.1.1.2  christos                opts->rsa_pkcs15_padding_disabled ? "1" : "0")
    449  1.1.1.2  christos             <= 0
    450      1.1  christos         || BIO_printf(out, "%s = %s\n",
    451  1.1.1.2  christos                OSSL_PROV_PARAM_RSA_PSS_SALTLEN_CHECK,
    452  1.1.1.2  christos                opts->rsa_pss_saltlen_check ? "1" : "0")
    453  1.1.1.2  christos             <= 0
    454      1.1  christos         || BIO_printf(out, "%s = %s\n",
    455  1.1.1.2  christos                OSSL_PROV_PARAM_RSA_SIGN_X931_PAD_DISABLED,
    456  1.1.1.2  christos                opts->sign_x931_padding_disabled ? "1" : "0")
    457  1.1.1.2  christos             <= 0
    458      1.1  christos         || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_HKDF_KEY_CHECK,
    459  1.1.1.2  christos                opts->hkdf_key_check ? "1" : "0")
    460  1.1.1.2  christos             <= 0
    461      1.1  christos         || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_KBKDF_KEY_CHECK,
    462  1.1.1.2  christos                opts->kbkdf_key_check ? "1" : "0")
    463  1.1.1.2  christos             <= 0
    464      1.1  christos         || BIO_printf(out, "%s = %s\n",
    465  1.1.1.2  christos                OSSL_PROV_PARAM_TLS13_KDF_KEY_CHECK,
    466  1.1.1.2  christos                opts->tls13_kdf_key_check ? "1" : "0")
    467  1.1.1.2  christos             <= 0
    468      1.1  christos         || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_TLS1_PRF_KEY_CHECK,
    469  1.1.1.2  christos                opts->tls1_prf_key_check ? "1" : "0")
    470  1.1.1.2  christos             <= 0
    471      1.1  christos         || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_SSHKDF_KEY_CHECK,
    472  1.1.1.2  christos                opts->sshkdf_key_check ? "1" : "0")
    473  1.1.1.2  christos             <= 0
    474      1.1  christos         || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_SSKDF_KEY_CHECK,
    475  1.1.1.2  christos                opts->sskdf_key_check ? "1" : "0")
    476  1.1.1.2  christos             <= 0
    477      1.1  christos         || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_X963KDF_KEY_CHECK,
    478  1.1.1.2  christos                opts->x963kdf_key_check ? "1" : "0")
    479  1.1.1.2  christos             <= 0
    480      1.1  christos         || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_X942KDF_KEY_CHECK,
    481  1.1.1.2  christos                opts->x942kdf_key_check ? "1" : "0")
    482  1.1.1.2  christos             <= 0
    483      1.1  christos         || BIO_printf(out, "%s = %s\n",
    484  1.1.1.2  christos                OSSL_PROV_PARAM_PBKDF2_LOWER_BOUND_CHECK,
    485  1.1.1.2  christos                opts->pbkdf2_lower_bound_check ? "1" : "0")
    486  1.1.1.2  christos             <= 0
    487      1.1  christos         || BIO_printf(out, "%s = %s\n", OSSL_PROV_PARAM_ECDH_COFACTOR_CHECK,
    488  1.1.1.2  christos                opts->ecdh_cofactor_check ? "1" : "0")
    489  1.1.1.2  christos             <= 0
    490      1.1  christos         || !print_mac(out, OSSL_PROV_FIPS_PARAM_MODULE_MAC, module_mac,
    491  1.1.1.2  christos             module_mac_len))
    492      1.1  christos         goto end;
    493      1.1  christos 
    494      1.1  christos     if (install_mac != NULL
    495  1.1.1.2  christos         && install_mac_len > 0
    496  1.1.1.2  christos         && opts->self_test_onload == 0) {
    497      1.1  christos         if (!print_mac(out, OSSL_PROV_FIPS_PARAM_INSTALL_MAC, install_mac,
    498  1.1.1.2  christos                 install_mac_len)
    499      1.1  christos             || BIO_printf(out, "%s = %s\n", OSSL_PROV_FIPS_PARAM_INSTALL_STATUS,
    500  1.1.1.2  christos                    INSTALL_STATUS_VAL)
    501  1.1.1.2  christos                 <= 0)
    502      1.1  christos             goto end;
    503      1.1  christos     }
    504      1.1  christos     ret = 1;
    505      1.1  christos end:
    506      1.1  christos     return ret;
    507      1.1  christos }
    508      1.1  christos 
    509      1.1  christos static CONF *generate_config_and_load(const char *prov_name,
    510  1.1.1.2  christos     const char *section,
    511  1.1.1.2  christos     unsigned char *module_mac,
    512  1.1.1.2  christos     size_t module_mac_len,
    513  1.1.1.2  christos     const FIPS_OPTS *opts)
    514      1.1  christos {
    515      1.1  christos     BIO *mem_bio = NULL;
    516      1.1  christos     CONF *conf = NULL;
    517      1.1  christos 
    518      1.1  christos     mem_bio = BIO_new(BIO_s_mem());
    519      1.1  christos     if (mem_bio == NULL)
    520      1.1  christos         return 0;
    521      1.1  christos     if (!write_config_header(mem_bio, prov_name, section)
    522      1.1  christos         || !write_config_fips_section(mem_bio, section,
    523  1.1.1.2  christos             module_mac, module_mac_len,
    524  1.1.1.2  christos             opts, NULL, 0))
    525      1.1  christos         goto end;
    526      1.1  christos 
    527      1.1  christos     conf = app_load_config_bio(mem_bio, NULL);
    528      1.1  christos     if (conf == NULL)
    529      1.1  christos         goto end;
    530      1.1  christos 
    531      1.1  christos     if (CONF_modules_load(conf, NULL, 0) <= 0)
    532      1.1  christos         goto end;
    533      1.1  christos     BIO_free(mem_bio);
    534      1.1  christos     return conf;
    535      1.1  christos end:
    536      1.1  christos     NCONF_free(conf);
    537      1.1  christos     BIO_free(mem_bio);
    538      1.1  christos     return NULL;
    539      1.1  christos }
    540      1.1  christos 
    541      1.1  christos static void free_config_and_unload(CONF *conf)
    542      1.1  christos {
    543      1.1  christos     if (conf != NULL) {
    544      1.1  christos         NCONF_free(conf);
    545      1.1  christos         CONF_modules_unload(1);
    546      1.1  christos     }
    547      1.1  christos }
    548      1.1  christos 
    549      1.1  christos static int verify_module_load(const char *parent_config_file)
    550      1.1  christos {
    551      1.1  christos     return OSSL_LIB_CTX_load_config(NULL, parent_config_file);
    552      1.1  christos }
    553      1.1  christos 
    554      1.1  christos /*
    555      1.1  christos  * Returns 1 if the config file entries match the passed in module_mac and
    556      1.1  christos  * install_mac values, otherwise it returns 0.
    557      1.1  christos  */
    558      1.1  christos static int verify_config(const char *infile, const char *section,
    559  1.1.1.2  christos     unsigned char *module_mac, size_t module_mac_len,
    560  1.1.1.2  christos     unsigned char *install_mac, size_t install_mac_len)
    561      1.1  christos {
    562      1.1  christos     int ret = 0;
    563      1.1  christos     char *s = NULL;
    564      1.1  christos     unsigned char *buf1 = NULL, *buf2 = NULL;
    565      1.1  christos     long len;
    566      1.1  christos     CONF *conf = NULL;
    567      1.1  christos 
    568      1.1  christos     /* read in the existing values and check they match the saved values */
    569      1.1  christos     conf = app_load_config(infile);
    570      1.1  christos     if (conf == NULL)
    571      1.1  christos         goto end;
    572      1.1  christos 
    573      1.1  christos     s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_VERSION);
    574      1.1  christos     if (s == NULL || strcmp(s, VERSION_VAL) != 0) {
    575      1.1  christos         BIO_printf(bio_err, "version not found\n");
    576      1.1  christos         goto end;
    577      1.1  christos     }
    578      1.1  christos     s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_MODULE_MAC);
    579      1.1  christos     if (s == NULL) {
    580      1.1  christos         BIO_printf(bio_err, "Module integrity MAC not found\n");
    581      1.1  christos         goto end;
    582      1.1  christos     }
    583      1.1  christos     buf1 = OPENSSL_hexstr2buf(s, &len);
    584      1.1  christos     if (buf1 == NULL
    585  1.1.1.2  christos         || (size_t)len != module_mac_len
    586  1.1.1.2  christos         || memcmp(module_mac, buf1, module_mac_len) != 0) {
    587      1.1  christos         BIO_printf(bio_err, "Module integrity mismatch\n");
    588      1.1  christos         goto end;
    589      1.1  christos     }
    590      1.1  christos     if (install_mac != NULL && install_mac_len > 0) {
    591      1.1  christos         s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_STATUS);
    592      1.1  christos         if (s == NULL || strcmp(s, INSTALL_STATUS_VAL) != 0) {
    593      1.1  christos             BIO_printf(bio_err, "install status not found\n");
    594      1.1  christos             goto end;
    595      1.1  christos         }
    596      1.1  christos         s = NCONF_get_string(conf, section, OSSL_PROV_FIPS_PARAM_INSTALL_MAC);
    597      1.1  christos         if (s == NULL) {
    598      1.1  christos             BIO_printf(bio_err, "Install indicator MAC not found\n");
    599      1.1  christos             goto end;
    600      1.1  christos         }
    601      1.1  christos         buf2 = OPENSSL_hexstr2buf(s, &len);
    602      1.1  christos         if (buf2 == NULL
    603  1.1.1.2  christos             || (size_t)len != install_mac_len
    604  1.1.1.2  christos             || memcmp(install_mac, buf2, install_mac_len) != 0) {
    605      1.1  christos             BIO_printf(bio_err, "Install indicator status mismatch\n");
    606      1.1  christos             goto end;
    607      1.1  christos         }
    608      1.1  christos     }
    609      1.1  christos     ret = 1;
    610      1.1  christos end:
    611      1.1  christos     OPENSSL_free(buf1);
    612      1.1  christos     OPENSSL_free(buf2);
    613      1.1  christos     NCONF_free(conf);
    614      1.1  christos     return ret;
    615      1.1  christos }
    616      1.1  christos 
    617      1.1  christos int fipsinstall_main(int argc, char **argv)
    618      1.1  christos {
    619      1.1  christos     int ret = 1, verify = 0, gotkey = 0, gotdigest = 0, pedantic = 0;
    620      1.1  christos     int is_fips_140_2_prov = 0, set_selftest_onload_option = 0;
    621      1.1  christos     const char *section_name = "fips_sect";
    622      1.1  christos     const char *mac_name = "HMAC";
    623      1.1  christos     const char *prov_name = "fips";
    624      1.1  christos     BIO *module_bio = NULL, *mem_bio = NULL, *fout = NULL;
    625      1.1  christos     char *in_fname = NULL, *out_fname = NULL, *prog;
    626      1.1  christos     char *module_fname = NULL, *parent_config = NULL, *module_path = NULL;
    627      1.1  christos     const char *tail;
    628      1.1  christos     EVP_MAC_CTX *ctx = NULL, *ctx2 = NULL;
    629      1.1  christos     STACK_OF(OPENSSL_STRING) *opts = NULL;
    630      1.1  christos     OPTION_CHOICE o;
    631      1.1  christos     unsigned char *read_buffer = NULL;
    632      1.1  christos     unsigned char module_mac[EVP_MAX_MD_SIZE];
    633      1.1  christos     size_t module_mac_len = EVP_MAX_MD_SIZE;
    634      1.1  christos     unsigned char install_mac[EVP_MAX_MD_SIZE];
    635      1.1  christos     size_t install_mac_len = EVP_MAX_MD_SIZE;
    636      1.1  christos     EVP_MAC *mac = NULL;
    637      1.1  christos     CONF *conf = NULL;
    638      1.1  christos 
    639      1.1  christos     if ((opts = sk_OPENSSL_STRING_new_null()) == NULL)
    640      1.1  christos         goto end;
    641      1.1  christos 
    642      1.1  christos     prog = opt_init(argc, argv, fipsinstall_options);
    643      1.1  christos     while ((o = opt_next()) != OPT_EOF) {
    644      1.1  christos         switch (o) {
    645      1.1  christos         case OPT_EOF:
    646      1.1  christos         case OPT_ERR:
    647  1.1.1.2  christos         opthelp:
    648      1.1  christos             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
    649      1.1  christos             goto cleanup;
    650      1.1  christos         case OPT_HELP:
    651      1.1  christos             opt_help(fipsinstall_options);
    652      1.1  christos             ret = 0;
    653      1.1  christos             goto end;
    654      1.1  christos         case OPT_IN:
    655      1.1  christos             in_fname = opt_arg();
    656      1.1  christos             break;
    657      1.1  christos         case OPT_OUT:
    658      1.1  christos             out_fname = opt_arg();
    659      1.1  christos             break;
    660      1.1  christos         case OPT_PEDANTIC:
    661      1.1  christos             fips_opts = pedantic_opts;
    662      1.1  christos             pedantic = 1;
    663      1.1  christos             break;
    664      1.1  christos         case OPT_NO_CONDITIONAL_ERRORS:
    665      1.1  christos             if (!check_non_pedantic_fips(pedantic, "no_conditional_errors"))
    666      1.1  christos                 goto end;
    667      1.1  christos             fips_opts.conditional_errors = 0;
    668      1.1  christos             break;
    669      1.1  christos         case OPT_NO_SECURITY_CHECKS:
    670      1.1  christos             if (!check_non_pedantic_fips(pedantic, "no_security_checks"))
    671      1.1  christos                 goto end;
    672      1.1  christos             fips_opts.security_checks = 0;
    673      1.1  christos             break;
    674      1.1  christos         case OPT_HMAC_KEY_CHECK:
    675      1.1  christos             fips_opts.hmac_key_check = 1;
    676      1.1  christos             break;
    677      1.1  christos         case OPT_KMAC_KEY_CHECK:
    678      1.1  christos             fips_opts.kmac_key_check = 1;
    679      1.1  christos             break;
    680      1.1  christos         case OPT_TLS_PRF_EMS_CHECK:
    681      1.1  christos             fips_opts.tls_prf_ems_check = 1;
    682      1.1  christos             break;
    683      1.1  christos         case OPT_NO_SHORT_MAC:
    684      1.1  christos             fips_opts.no_short_mac = 1;
    685      1.1  christos             break;
    686      1.1  christos         case OPT_DISALLOW_DRGB_TRUNC_DIGEST:
    687      1.1  christos             fips_opts.drgb_no_trunc_dgst = 1;
    688      1.1  christos             break;
    689      1.1  christos         case OPT_SIGNATURE_DIGEST_CHECK:
    690      1.1  christos             fips_opts.signature_digest_check = 1;
    691      1.1  christos             break;
    692      1.1  christos         case OPT_HKDF_DIGEST_CHECK:
    693      1.1  christos             fips_opts.hkdf_digest_check = 1;
    694      1.1  christos             break;
    695      1.1  christos         case OPT_TLS13_KDF_DIGEST_CHECK:
    696      1.1  christos             fips_opts.tls13_kdf_digest_check = 1;
    697      1.1  christos             break;
    698      1.1  christos         case OPT_TLS1_PRF_DIGEST_CHECK:
    699      1.1  christos             fips_opts.tls1_prf_digest_check = 1;
    700      1.1  christos             break;
    701      1.1  christos         case OPT_SSHKDF_DIGEST_CHECK:
    702      1.1  christos             fips_opts.sshkdf_digest_check = 1;
    703      1.1  christos             break;
    704      1.1  christos         case OPT_SSKDF_DIGEST_CHECK:
    705      1.1  christos             fips_opts.sskdf_digest_check = 1;
    706      1.1  christos             break;
    707      1.1  christos         case OPT_X963KDF_DIGEST_CHECK:
    708      1.1  christos             fips_opts.x963kdf_digest_check = 1;
    709      1.1  christos             break;
    710      1.1  christos         case OPT_DISALLOW_DSA_SIGN:
    711      1.1  christos             fips_opts.dsa_sign_disabled = 1;
    712      1.1  christos             break;
    713      1.1  christos         case OPT_DISALLOW_TDES_ENCRYPT:
    714      1.1  christos             fips_opts.tdes_encrypt_disabled = 1;
    715      1.1  christos             break;
    716      1.1  christos         case OPT_RSA_PSS_SALTLEN_CHECK:
    717      1.1  christos             fips_opts.rsa_pss_saltlen_check = 1;
    718      1.1  christos             break;
    719      1.1  christos         case OPT_DISALLOW_SIGNATURE_X931_PADDING:
    720      1.1  christos             fips_opts.sign_x931_padding_disabled = 1;
    721      1.1  christos             break;
    722      1.1  christos         case OPT_DISALLOW_PKCS15_PADDING:
    723      1.1  christos             fips_opts.rsa_pkcs15_padding_disabled = 1;
    724      1.1  christos             break;
    725      1.1  christos         case OPT_HKDF_KEY_CHECK:
    726      1.1  christos             fips_opts.hkdf_key_check = 1;
    727      1.1  christos             break;
    728      1.1  christos         case OPT_KBKDF_KEY_CHECK:
    729      1.1  christos             fips_opts.kbkdf_key_check = 1;
    730      1.1  christos             break;
    731      1.1  christos         case OPT_TLS13_KDF_KEY_CHECK:
    732      1.1  christos             fips_opts.tls13_kdf_key_check = 1;
    733      1.1  christos             break;
    734      1.1  christos         case OPT_TLS1_PRF_KEY_CHECK:
    735      1.1  christos             fips_opts.tls1_prf_key_check = 1;
    736      1.1  christos             break;
    737      1.1  christos         case OPT_SSHKDF_KEY_CHECK:
    738      1.1  christos             fips_opts.sshkdf_key_check = 1;
    739      1.1  christos             break;
    740      1.1  christos         case OPT_SSKDF_KEY_CHECK:
    741      1.1  christos             fips_opts.sskdf_key_check = 1;
    742      1.1  christos             break;
    743      1.1  christos         case OPT_X963KDF_KEY_CHECK:
    744      1.1  christos             fips_opts.x963kdf_key_check = 1;
    745      1.1  christos             break;
    746      1.1  christos         case OPT_X942KDF_KEY_CHECK:
    747      1.1  christos             fips_opts.x942kdf_key_check = 1;
    748      1.1  christos             break;
    749      1.1  christos         case OPT_NO_PBKDF2_LOWER_BOUND_CHECK:
    750      1.1  christos             if (!check_non_pedantic_fips(pedantic, "no_pbkdf2_lower_bound_check"))
    751      1.1  christos                 goto end;
    752      1.1  christos             fips_opts.pbkdf2_lower_bound_check = 0;
    753      1.1  christos             break;
    754      1.1  christos         case OPT_ECDH_COFACTOR_CHECK:
    755      1.1  christos             fips_opts.ecdh_cofactor_check = 1;
    756      1.1  christos             break;
    757      1.1  christos         case OPT_QUIET:
    758      1.1  christos             quiet = 1;
    759      1.1  christos             /* FALLTHROUGH */
    760      1.1  christos         case OPT_NO_LOG:
    761      1.1  christos             self_test_log = 0;
    762      1.1  christos             break;
    763      1.1  christos         case OPT_CORRUPT_DESC:
    764      1.1  christos             self_test_corrupt_desc = opt_arg();
    765      1.1  christos             break;
    766      1.1  christos         case OPT_CORRUPT_TYPE:
    767      1.1  christos             self_test_corrupt_type = opt_arg();
    768      1.1  christos             break;
    769      1.1  christos         case OPT_PROV_NAME:
    770      1.1  christos             prov_name = opt_arg();
    771      1.1  christos             break;
    772      1.1  christos         case OPT_MODULE:
    773      1.1  christos             module_fname = opt_arg();
    774      1.1  christos             break;
    775      1.1  christos         case OPT_SECTION_NAME:
    776      1.1  christos             section_name = opt_arg();
    777      1.1  christos             break;
    778      1.1  christos         case OPT_MAC_NAME:
    779      1.1  christos             mac_name = opt_arg();
    780      1.1  christos             break;
    781      1.1  christos         case OPT_CONFIG:
    782      1.1  christos             parent_config = opt_arg();
    783      1.1  christos             break;
    784      1.1  christos         case OPT_MACOPT:
    785      1.1  christos             if (!sk_OPENSSL_STRING_push(opts, opt_arg()))
    786      1.1  christos                 goto opthelp;
    787      1.1  christos             if (HAS_PREFIX(opt_arg(), "hexkey:"))
    788      1.1  christos                 gotkey = 1;
    789      1.1  christos             else if (HAS_PREFIX(opt_arg(), "digest:"))
    790      1.1  christos                 gotdigest = 1;
    791      1.1  christos             break;
    792      1.1  christos         case OPT_VERIFY:
    793      1.1  christos             verify = 1;
    794      1.1  christos             break;
    795      1.1  christos         case OPT_SELF_TEST_ONLOAD:
    796      1.1  christos             set_selftest_onload_option = 1;
    797      1.1  christos             fips_opts.self_test_onload = 1;
    798      1.1  christos             break;
    799      1.1  christos         case OPT_SELF_TEST_ONINSTALL:
    800      1.1  christos             if (!check_non_pedantic_fips(pedantic, "self_test_oninstall"))
    801      1.1  christos                 goto end;
    802      1.1  christos             set_selftest_onload_option = 1;
    803      1.1  christos             fips_opts.self_test_onload = 0;
    804      1.1  christos             break;
    805      1.1  christos         }
    806      1.1  christos     }
    807      1.1  christos 
    808      1.1  christos     /* No extra arguments. */
    809      1.1  christos     if (!opt_check_rest_arg(NULL))
    810      1.1  christos         goto opthelp;
    811      1.1  christos     if (verify && in_fname == NULL) {
    812      1.1  christos         BIO_printf(bio_err, "Missing -in option for -verify\n");
    813      1.1  christos         goto opthelp;
    814      1.1  christos     }
    815      1.1  christos 
    816      1.1  christos     if (parent_config != NULL) {
    817      1.1  christos         /* Test that a parent config can load the module */
    818      1.1  christos         if (verify_module_load(parent_config)) {
    819      1.1  christos             ret = OSSL_PROVIDER_available(NULL, prov_name) ? 0 : 1;
    820      1.1  christos             if (!quiet) {
    821      1.1  christos                 BIO_printf(bio_err, "FIPS provider is %s\n",
    822  1.1.1.2  christos                     ret == 0 ? "available" : "not available");
    823      1.1  christos             }
    824      1.1  christos         }
    825      1.1  christos         goto end;
    826      1.1  christos     }
    827      1.1  christos     if (module_fname == NULL)
    828      1.1  christos         goto opthelp;
    829      1.1  christos 
    830      1.1  christos     tail = opt_path_end(module_fname);
    831      1.1  christos     if (tail != NULL) {
    832      1.1  christos         module_path = OPENSSL_strdup(module_fname);
    833      1.1  christos         if (module_path == NULL)
    834      1.1  christos             goto end;
    835      1.1  christos         module_path[tail - module_fname] = '\0';
    836      1.1  christos         if (!OSSL_PROVIDER_set_default_search_path(NULL, module_path))
    837      1.1  christos             goto end;
    838      1.1  christos     }
    839      1.1  christos 
    840      1.1  christos     if (self_test_log
    841  1.1.1.2  christos         || self_test_corrupt_desc != NULL
    842  1.1.1.2  christos         || self_test_corrupt_type != NULL)
    843      1.1  christos         OSSL_SELF_TEST_set_callback(NULL, self_test_events, NULL);
    844      1.1  christos 
    845      1.1  christos     /* Use the default FIPS HMAC digest and key if not specified. */
    846      1.1  christos     if (!gotdigest && !sk_OPENSSL_STRING_push(opts, "digest:SHA256"))
    847      1.1  christos         goto end;
    848      1.1  christos     if (!gotkey && !sk_OPENSSL_STRING_push(opts, "hexkey:" FIPS_KEY_STRING))
    849      1.1  christos         goto end;
    850      1.1  christos 
    851      1.1  christos     module_bio = bio_open_default(module_fname, 'r', FORMAT_BINARY);
    852      1.1  christos     if (module_bio == NULL) {
    853      1.1  christos         BIO_printf(bio_err, "Failed to open module file\n");
    854      1.1  christos         goto end;
    855      1.1  christos     }
    856      1.1  christos 
    857      1.1  christos     read_buffer = app_malloc(BUFSIZE, "I/O buffer");
    858      1.1  christos     if (read_buffer == NULL)
    859      1.1  christos         goto end;
    860      1.1  christos 
    861      1.1  christos     mac = EVP_MAC_fetch(app_get0_libctx(), mac_name, app_get0_propq());
    862      1.1  christos     if (mac == NULL) {
    863      1.1  christos         BIO_printf(bio_err, "Unable to get MAC of type %s\n", mac_name);
    864      1.1  christos         goto end;
    865      1.1  christos     }
    866      1.1  christos 
    867      1.1  christos     ctx = EVP_MAC_CTX_new(mac);
    868      1.1  christos     if (ctx == NULL) {
    869      1.1  christos         BIO_printf(bio_err, "Unable to create MAC CTX for module check\n");
    870      1.1  christos         goto end;
    871      1.1  christos     }
    872      1.1  christos 
    873      1.1  christos     if (opts != NULL) {
    874      1.1  christos         int ok = 1;
    875  1.1.1.2  christos         OSSL_PARAM *params = app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac));
    876      1.1  christos 
    877      1.1  christos         if (params == NULL)
    878      1.1  christos             goto end;
    879      1.1  christos 
    880      1.1  christos         if (!EVP_MAC_CTX_set_params(ctx, params)) {
    881      1.1  christos             BIO_printf(bio_err, "MAC parameter error\n");
    882      1.1  christos             ERR_print_errors(bio_err);
    883      1.1  christos             ok = 0;
    884      1.1  christos         }
    885      1.1  christos         app_params_free(params);
    886      1.1  christos         if (!ok)
    887      1.1  christos             goto end;
    888      1.1  christos     }
    889      1.1  christos 
    890      1.1  christos     ctx2 = EVP_MAC_CTX_dup(ctx);
    891      1.1  christos     if (ctx2 == NULL) {
    892      1.1  christos         BIO_printf(bio_err, "Unable to create MAC CTX for install indicator\n");
    893      1.1  christos         goto end;
    894      1.1  christos     }
    895      1.1  christos 
    896      1.1  christos     if (!do_mac(ctx, read_buffer, module_bio, module_mac, &module_mac_len))
    897      1.1  christos         goto end;
    898      1.1  christos 
    899      1.1  christos     /* Calculate the MAC for the indicator status - it may not be used */
    900      1.1  christos     mem_bio = BIO_new_mem_buf((const void *)INSTALL_STATUS_VAL,
    901  1.1.1.2  christos         strlen(INSTALL_STATUS_VAL));
    902      1.1  christos     if (mem_bio == NULL) {
    903      1.1  christos         BIO_printf(bio_err, "Unable to create memory BIO\n");
    904      1.1  christos         goto end;
    905      1.1  christos     }
    906      1.1  christos     if (!do_mac(ctx2, read_buffer, mem_bio, install_mac, &install_mac_len))
    907      1.1  christos         goto end;
    908      1.1  christos 
    909      1.1  christos     if (verify) {
    910      1.1  christos         if (fips_opts.self_test_onload == 1)
    911      1.1  christos             install_mac_len = 0;
    912      1.1  christos         if (!verify_config(in_fname, section_name, module_mac, module_mac_len,
    913  1.1.1.2  christos                 install_mac, install_mac_len))
    914      1.1  christos             goto end;
    915      1.1  christos         if (!quiet)
    916      1.1  christos             BIO_printf(bio_err, "VERIFY PASSED\n");
    917      1.1  christos     } else {
    918      1.1  christos         conf = generate_config_and_load(prov_name, section_name, module_mac,
    919  1.1.1.2  christos             module_mac_len, &fips_opts);
    920      1.1  christos         if (conf == NULL)
    921      1.1  christos             goto end;
    922      1.1  christos         if (!load_fips_prov_and_run_self_test(prov_name, &is_fips_140_2_prov))
    923      1.1  christos             goto end;
    924      1.1  christos 
    925      1.1  christos         /*
    926      1.1  christos          * In OpenSSL 3.1 the code was changed so that the status indicator is
    927      1.1  christos          * not written out by default since this is a FIPS 140-3 requirement.
    928      1.1  christos          * For backwards compatibility - if the detected FIPS provider is 3.0.X
    929      1.1  christos          * (Which was a FIPS 140-2 validation), then the indicator status will
    930      1.1  christos          * be written to the config file unless 'self_test_onload' is set on the
    931      1.1  christos          * command line.
    932      1.1  christos          */
    933      1.1  christos         if (set_selftest_onload_option == 0 && is_fips_140_2_prov)
    934      1.1  christos             fips_opts.self_test_onload = 0;
    935      1.1  christos 
    936  1.1.1.2  christos         fout = out_fname == NULL ? dup_bio_out(FORMAT_TEXT)
    937  1.1.1.2  christos                                  : bio_open_default(out_fname, 'w', FORMAT_TEXT);
    938      1.1  christos         if (fout == NULL) {
    939      1.1  christos             BIO_printf(bio_err, "Failed to open file\n");
    940      1.1  christos             goto end;
    941      1.1  christos         }
    942      1.1  christos 
    943      1.1  christos         if (!write_config_fips_section(fout, section_name,
    944  1.1.1.2  christos                 module_mac, module_mac_len, &fips_opts,
    945  1.1.1.2  christos                 install_mac, install_mac_len))
    946      1.1  christos             goto end;
    947      1.1  christos         if (!quiet)
    948      1.1  christos             BIO_printf(bio_err, "INSTALL PASSED\n");
    949      1.1  christos     }
    950      1.1  christos 
    951      1.1  christos     ret = 0;
    952      1.1  christos end:
    953      1.1  christos     if (ret == 1) {
    954      1.1  christos         if (!quiet)
    955      1.1  christos             BIO_printf(bio_err, "%s FAILED\n", verify ? "VERIFY" : "INSTALL");
    956      1.1  christos         ERR_print_errors(bio_err);
    957      1.1  christos     }
    958      1.1  christos 
    959      1.1  christos cleanup:
    960      1.1  christos     OPENSSL_free(module_path);
    961      1.1  christos     BIO_free(fout);
    962      1.1  christos     BIO_free(mem_bio);
    963      1.1  christos     BIO_free(module_bio);
    964      1.1  christos     sk_OPENSSL_STRING_free(opts);
    965      1.1  christos     EVP_MAC_free(mac);
    966      1.1  christos     EVP_MAC_CTX_free(ctx2);
    967      1.1  christos     EVP_MAC_CTX_free(ctx);
    968      1.1  christos     OPENSSL_free(read_buffer);
    969      1.1  christos     free_config_and_unload(conf);
    970      1.1  christos     return ret;
    971      1.1  christos }
    972      1.1  christos 
    973      1.1  christos static int self_test_events(const OSSL_PARAM params[], void *arg)
    974      1.1  christos {
    975      1.1  christos     const OSSL_PARAM *p = NULL;
    976      1.1  christos     const char *phase = NULL, *type = NULL, *desc = NULL;
    977      1.1  christos     int ret = 0;
    978      1.1  christos 
    979      1.1  christos     p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_PHASE);
    980      1.1  christos     if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
    981      1.1  christos         goto err;
    982      1.1  christos     phase = (const char *)p->data;
    983      1.1  christos 
    984      1.1  christos     p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_DESC);
    985      1.1  christos     if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
    986      1.1  christos         goto err;
    987      1.1  christos     desc = (const char *)p->data;
    988      1.1  christos 
    989      1.1  christos     p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_TYPE);
    990      1.1  christos     if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
    991      1.1  christos         goto err;
    992      1.1  christos     type = (const char *)p->data;
    993      1.1  christos 
    994      1.1  christos     if (self_test_log) {
    995      1.1  christos         if (strcmp(phase, OSSL_SELF_TEST_PHASE_START) == 0)
    996      1.1  christos             BIO_printf(bio_err, "%s : (%s) : ", desc, type);
    997      1.1  christos         else if (strcmp(phase, OSSL_SELF_TEST_PHASE_PASS) == 0
    998  1.1.1.2  christos             || strcmp(phase, OSSL_SELF_TEST_PHASE_FAIL) == 0)
    999      1.1  christos             BIO_printf(bio_err, "%s\n", phase);
   1000      1.1  christos     }
   1001      1.1  christos     /*
   1002      1.1  christos      * The self test code will internally corrupt the KAT test result if an
   1003      1.1  christos      * error is returned during the corrupt phase.
   1004      1.1  christos      */
   1005      1.1  christos     if (strcmp(phase, OSSL_SELF_TEST_PHASE_CORRUPT) == 0
   1006  1.1.1.2  christos         && (self_test_corrupt_desc != NULL
   1007  1.1.1.2  christos             || self_test_corrupt_type != NULL)) {
   1008      1.1  christos         if (self_test_corrupt_desc != NULL
   1009  1.1.1.2  christos             && strcmp(self_test_corrupt_desc, desc) != 0)
   1010      1.1  christos             goto end;
   1011      1.1  christos         if (self_test_corrupt_type != NULL
   1012  1.1.1.2  christos             && strcmp(self_test_corrupt_type, type) != 0)
   1013      1.1  christos             goto end;
   1014      1.1  christos         BIO_printf(bio_err, "%s ", phase);
   1015      1.1  christos         goto err;
   1016      1.1  christos     }
   1017      1.1  christos end:
   1018      1.1  christos     ret = 1;
   1019      1.1  christos err:
   1020      1.1  christos     return ret;
   1021      1.1  christos }
   1022