X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=providers%2Ffips%2Ffipsprov.c;h=d847d5c903eaffceba6341b19dc2c53cc998410e;hb=23c48d94d4d34eedc15fa65e0fa0e38a6137e09f;hp=faf74831ebd48d00b6d2b8e21276c57748088fe2;hpb=f844f9eb44186df2f8b0cfd3264b4eb003d8c61a;p=oweals%2Fopenssl.git diff --git a/providers/fips/fipsprov.c b/providers/fips/fipsprov.c index faf74831eb..d847d5c903 100644 --- a/providers/fips/fipsprov.c +++ b/providers/fips/fipsprov.c @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include #include @@ -34,6 +34,15 @@ #include "prov/provider_util.h" #include "self_test.h" +/* + * Forward declarations to ensure that interface functions are correctly + * defined. + */ +static OSSL_provider_teardown_fn fips_teardown; +static OSSL_provider_gettable_params_fn fips_gettable_params; +static OSSL_provider_get_params_fn fips_get_params; +static OSSL_provider_query_operation_fn fips_query; + #define ALGC(NAMES, FUNC, CHECK) { { NAMES, "provider=fips,fips=yes", FUNC }, CHECK } #define ALG(NAMES, FUNC) ALGC(NAMES, FUNC, NULL) @@ -72,7 +81,7 @@ static OSSL_CRYPTO_secure_allocated_fn *c_CRYPTO_secure_allocated; static OSSL_BIO_vsnprintf_fn *c_BIO_vsnprintf; typedef struct fips_global_st { - const OSSL_PROVIDER *prov; + const OSSL_CORE_HANDLE *handle; } FIPS_GLOBAL; static void *fips_prov_ossl_ctx_new(OPENSSL_CTX *libctx) @@ -104,11 +113,12 @@ static const OSSL_PARAM fips_param_types[] = { /* * Parameters to retrieve from the core provider - required for self testing. * NOTE: inside core_get_params() these will be loaded from config items - * stored inside prov->parameters (except for OSSL_PROV_PARAM_MODULE_FILENAME). + * stored inside prov->parameters (except for + * OSSL_PROV_PARAM_CORE_MODULE_FILENAME). */ static OSSL_PARAM core_params[] = { - OSSL_PARAM_utf8_ptr(OSSL_PROV_PARAM_MODULE_FILENAME, + OSSL_PARAM_utf8_ptr(OSSL_PROV_PARAM_CORE_MODULE_FILENAME, selftest_params.module_filename, sizeof(selftest_params.module_filename)), OSSL_PARAM_utf8_ptr(OSSL_PROV_FIPS_PARAM_MODULE_MAC, @@ -127,9 +137,8 @@ static OSSL_PARAM core_params[] = }; /* TODO(3.0): To be removed */ -static int dummy_evp_call(void *provctx) +static int dummy_evp_call(OPENSSL_CTX *libctx) { - OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx); EVP_MD_CTX *ctx = EVP_MD_CTX_new(); EVP_MD *sha256 = EVP_MD_fetch(libctx, "SHA256", NULL); EVP_KDF *kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_PBKDF2, NULL); @@ -185,7 +194,7 @@ static int dummy_evp_call(void *provctx) #ifndef OPENSSL_NO_EC /* Do some dummy EC calls */ - key = EC_KEY_new_by_curve_name_ex(libctx, NID_X9_62_prime256v1); + key = EC_KEY_new_by_curve_name_with_libctx(libctx, NULL, NID_X9_62_prime256v1); if (key == NULL) goto err; @@ -208,12 +217,12 @@ static int dummy_evp_call(void *provctx) return ret; } -static const OSSL_PARAM *fips_gettable_params(const OSSL_PROVIDER *prov) +static const OSSL_PARAM *fips_gettable_params(void *provctx) { return fips_param_types; } -static int fips_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[]) +static int fips_get_params(void *provctx, OSSL_PARAM params[]) { OSSL_PARAM *p; @@ -368,8 +377,17 @@ static const OSSL_ALGORITHM fips_digests[] = { { "SHA3-384", "provider=fips,fips=yes", sha3_384_functions }, { "SHA3-512", "provider=fips,fips=yes", sha3_512_functions }, - /* Non-FIPS algorithm to support oneshot_hash in the Ed448 code */ - { "SHAKE-256:SHAKE256", "provider=fips,fips=no", shake_256_functions }, + { "SHAKE-128:SHAKE128", "provider=fips,fips=yes", shake_128_functions }, + { "SHAKE-256:SHAKE256", "provider=fips,fips=yes", shake_256_functions }, + + /* + * KECCAK-KMAC-128 and KECCAK-KMAC-256 as hashes are mostly useful for + * KMAC128 and KMAC256. + */ + { "KECCAK-KMAC-128:KECCAK-KMAC128", "provider=fips,fips=yes", + keccak_kmac_128_functions }, + { "KECCAK-KMAC-256:KECCAK-KMAC256", "provider=fips,fips=yes", + keccak_kmac_256_functions }, { NULL, NULL, NULL } }; @@ -381,6 +399,18 @@ static const OSSL_ALGORITHM_CAPABLE fips_ciphers[] = { ALG("AES-256-CBC", aes256cbc_functions), ALG("AES-192-CBC", aes192cbc_functions), ALG("AES-128-CBC", aes128cbc_functions), + ALG("AES-256-OFB", aes256ofb_functions), + ALG("AES-192-OFB", aes192ofb_functions), + ALG("AES-128-OFB", aes128ofb_functions), + ALG("AES-256-CFB", aes256cfb_functions), + ALG("AES-192-CFB", aes192cfb_functions), + ALG("AES-128-CFB", aes128cfb_functions), + ALG("AES-256-CFB1", aes256cfb1_functions), + ALG("AES-192-CFB1", aes192cfb1_functions), + ALG("AES-128-CFB1", aes128cfb1_functions), + ALG("AES-256-CFB8", aes256cfb8_functions), + ALG("AES-192-CFB8", aes192cfb8_functions), + ALG("AES-128-CFB8", aes128cfb8_functions), ALG("AES-256-CTR", aes256ctr_functions), ALG("AES-192-CTR", aes192ctr_functions), ALG("AES-128-CTR", aes128ctr_functions), @@ -418,8 +448,13 @@ static const OSSL_ALGORITHM_CAPABLE fips_ciphers[] = { static OSSL_ALGORITHM exported_fips_ciphers[OSSL_NELEM(fips_ciphers)]; static const OSSL_ALGORITHM fips_macs[] = { +#ifndef OPENSSL_NO_CMAC + { "CMAC", "provider=fips,fips=yes", cmac_functions }, +#endif { "GMAC", "provider=fips,fips=yes", gmac_functions }, { "HMAC", "provider=fips,fips=yes", hmac_functions }, + { "KMAC-128:KMAC128", "provider=fips,fips=yes", kmac128_functions }, + { "KMAC-256:KMAC256", "provider=fips,fips=yes", kmac256_functions }, { NULL, NULL, NULL } }; @@ -427,7 +462,18 @@ static const OSSL_ALGORITHM fips_kdfs[] = { { "HKDF", "provider=fips,fips=yes", kdf_hkdf_functions }, { "SSKDF", "provider=fips,fips=yes", kdf_sskdf_functions }, { "PBKDF2", "provider=fips,fips=yes", kdf_pbkdf2_functions }, + { "SSHKDF", "provider=fips,fips=yes", kdf_sshkdf_functions }, + { "X963KDF", "provider=fips,fips=yes", kdf_x963_kdf_functions }, { "TLS1-PRF", "provider=fips,fips=yes", kdf_tls1_prf_functions }, + { "KBKDF", "provider=fips,fips=yes", kdf_kbkdf_functions }, + { NULL, NULL, NULL } +}; + +static const OSSL_ALGORITHM fips_rands[] = { + { "CTR-DRBG", "provider=fips", drbg_ctr_functions }, + { "HASH-DRBG", "provider=fips", drbg_hash_functions }, + { "HMAC-DRBG", "provider=fips", drbg_hmac_functions }, + { "TEST-RAND", "provider=fips", test_rng_functions }, { NULL, NULL, NULL } }; @@ -469,6 +515,8 @@ static const OSSL_ALGORITHM fips_keymgmt[] = { { "DSA", "provider=fips,fips=yes", dsa_keymgmt_functions }, #endif { "RSA:rsaEncryption", "provider=fips,fips=yes", rsa_keymgmt_functions }, + { "RSA-PSS:RSASSA-PSS", "provider=fips,fips=yes", + rsapss_keymgmt_functions }, #ifndef OPENSSL_NO_EC { "EC:id-ecPublicKey", "provider=fips,fips=yes", ec_keymgmt_functions }, { "X25519", "provider=fips,fips=no", x25519_keymgmt_functions }, @@ -479,9 +527,8 @@ static const OSSL_ALGORITHM fips_keymgmt[] = { { NULL, NULL, NULL } }; -static const OSSL_ALGORITHM *fips_query(OSSL_PROVIDER *prov, - int operation_id, - int *no_cache) +static const OSSL_ALGORITHM *fips_query(void *provctx, int operation_id, + int *no_cache) { *no_cache = 0; switch (operation_id) { @@ -494,6 +541,8 @@ static const OSSL_ALGORITHM *fips_query(OSSL_PROVIDER *prov, return fips_macs; case OSSL_OP_KDF: return fips_kdfs; + case OSSL_OP_RAND: + return fips_rands; case OSSL_OP_KEYMGMT: return fips_keymgmt; case OSSL_OP_KEYEXCH: @@ -506,33 +555,46 @@ static const OSSL_ALGORITHM *fips_query(OSSL_PROVIDER *prov, return NULL; } -/* Functions we provide to the core */ -static const OSSL_DISPATCH fips_dispatch_table[] = { +static void fips_teardown(void *provctx) +{ + OPENSSL_CTX_free(PROV_LIBRARY_CONTEXT_OF(provctx)); + PROV_CTX_free(provctx); +} + +static void fips_intern_teardown(void *provctx) +{ /* - * To release our resources we just need to free the OPENSSL_CTX so we just - * use OPENSSL_CTX_free directly as our teardown function + * We know that the library context is the same as for the outer provider, + * so no need to destroy it here. */ - { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OPENSSL_CTX_free }, + PROV_CTX_free(provctx); +} + +/* Functions we provide to the core */ +static const OSSL_DISPATCH fips_dispatch_table[] = { + { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_teardown }, { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))fips_gettable_params }, { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))fips_get_params }, { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query }, + { OSSL_FUNC_PROVIDER_GET_CAPABILITIES, (void (*)(void))provider_get_capabilities }, { 0, NULL } }; /* Functions we provide to ourself */ static const OSSL_DISPATCH intern_dispatch_table[] = { + { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))fips_intern_teardown }, { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query }, { 0, NULL } }; -int OSSL_provider_init(const OSSL_PROVIDER *provider, +int OSSL_provider_init(const OSSL_CORE_HANDLE *handle, const OSSL_DISPATCH *in, const OSSL_DISPATCH **out, void **provctx) { FIPS_GLOBAL *fgbl; - OPENSSL_CTX *ctx; + OPENSSL_CTX *libctx = NULL; OSSL_self_test_cb_fn *stcbfn = NULL; OSSL_core_get_library_context_fn *c_get_libctx = NULL; @@ -627,7 +689,7 @@ int OSSL_provider_init(const OSSL_PROVIDER *provider, } if (stcbfn != NULL && c_get_libctx != NULL) { - stcbfn(c_get_libctx(provider), &selftest_params.cb, + stcbfn(c_get_libctx(handle), &selftest_params.cb, &selftest_params.cb_arg); } else { @@ -635,39 +697,47 @@ int OSSL_provider_init(const OSSL_PROVIDER *provider, selftest_params.cb_arg = NULL; } - if (!c_get_params(provider, core_params)) + if (!c_get_params(handle, core_params)) return 0; /* Create a context. */ - if ((ctx = OPENSSL_CTX_new()) == NULL) - return 0; - if ((fgbl = openssl_ctx_get_data(ctx, OPENSSL_CTX_FIPS_PROV_INDEX, - &fips_prov_ossl_ctx_method)) == NULL) { - OPENSSL_CTX_free(ctx); - return 0; + if ((*provctx = PROV_CTX_new()) == NULL + || (libctx = OPENSSL_CTX_new()) == NULL) { + /* + * We free libctx separately here and only here because it hasn't + * been attached to *provctx. All other error paths below rely + * solely on fips_teardown. + */ + OPENSSL_CTX_free(libctx); + goto err; } + PROV_CTX_set0_library_context(*provctx, libctx); + PROV_CTX_set0_handle(*provctx, handle); - fgbl->prov = provider; + if ((fgbl = openssl_ctx_get_data(libctx, OPENSSL_CTX_FIPS_PROV_INDEX, + &fips_prov_ossl_ctx_method)) == NULL) + goto err; - selftest_params.libctx = PROV_LIBRARY_CONTEXT_OF(ctx); - if (!SELF_TEST_post(&selftest_params, 0)) { - OPENSSL_CTX_free(ctx); - return 0; - } + fgbl->handle = handle; + + selftest_params.libctx = libctx; + if (!SELF_TEST_post(&selftest_params, 0)) + goto err; /* * TODO(3.0): Remove me. This is just a dummy call to demonstrate making * EVP calls from within the FIPS module. */ - if (!dummy_evp_call(ctx)) { - OPENSSL_CTX_free(ctx); - return 0; - } + if (!dummy_evp_call(libctx)) + goto err; *out = fips_dispatch_table; - *provctx = ctx; return 1; + err: + fips_teardown(*provctx); + *provctx = NULL; + return 0; } /* @@ -678,7 +748,7 @@ int OSSL_provider_init(const OSSL_PROVIDER *provider, * that was used in the EVP call that initiated this recursive call. */ OSSL_provider_init_fn fips_intern_provider_init; -int fips_intern_provider_init(const OSSL_PROVIDER *provider, +int fips_intern_provider_init(const OSSL_CORE_HANDLE *handle, const OSSL_DISPATCH *in, const OSSL_DISPATCH **out, void **provctx) @@ -698,14 +768,15 @@ int fips_intern_provider_init(const OSSL_PROVIDER *provider, if (c_get_libctx == NULL) return 0; - *provctx = c_get_libctx(provider); - + if ((*provctx = PROV_CTX_new()) == NULL) + return 0; /* - * Safety measure... we should get the library context that was - * created up in OSSL_provider_init(). + * Using the parent library context only works because we are a built-in + * internal provider. This is not something that most providers would be + * able to do. */ - if (*provctx == NULL) - return 0; + PROV_CTX_set0_library_context(*provctx, (OPENSSL_CTX *)c_get_libctx(handle)); + PROV_CTX_set0_handle(*provctx, handle); *out = intern_dispatch_table; return 1; @@ -750,15 +821,23 @@ int ERR_pop_to_mark(void) return c_pop_error_to_mark(NULL); } -const OSSL_PROVIDER *FIPS_get_provider(OPENSSL_CTX *ctx) +/* + * This must take a library context, since it's called from the depths + * of crypto/initthread.c code, where it's (correctly) assumed that the + * passed caller argument is an OPENSSL_CTX pointer (since the same routine + * is also called from other parts of libcrypto, which all pass around a + * OPENSSL_CTX pointer) + */ +const OSSL_CORE_HANDLE *FIPS_get_core_handle(OPENSSL_CTX *libctx) { - FIPS_GLOBAL *fgbl = openssl_ctx_get_data(ctx, OPENSSL_CTX_FIPS_PROV_INDEX, + FIPS_GLOBAL *fgbl = openssl_ctx_get_data(libctx, + OPENSSL_CTX_FIPS_PROV_INDEX, &fips_prov_ossl_ctx_method); if (fgbl == NULL) return NULL; - return fgbl->prov; + return fgbl->handle; } void *CRYPTO_malloc(size_t num, const char *file, int line)