Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crypto with --enable-fips accepts OpenSSL 3 lib without fips provider #8762

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib/crypto/c_src/algorithms.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,16 @@ void init_curve_types(ErlNifEnv* env) {
if (FIPS_MODE()) {
// FIPS enabled
get_curve_cnt(env, 1);
FIPS_mode_set(0); // disable
(void) FIPS_mode_set(0); // disable
get_curve_cnt(env, 0);
FIPS_mode_set(1); // re-enable
(void) FIPS_mode_set(1); // re-enable
} else {
// FIPS disabled but available
get_curve_cnt(env, 0);
FIPS_mode_set(1); // enable
get_curve_cnt(env, 1);
FIPS_mode_set(0); // re-disable
if (FIPS_mode_set(1)) { // enable
get_curve_cnt(env, 1);
(void) FIPS_mode_set(0); // re-disable
}
}
#else
// FIPS mode is not available
Expand Down
36 changes: 18 additions & 18 deletions lib/crypto/c_src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,23 @@ static int initialize(ErlNifEnv* env, ERL_NIF_TERM load_info)
ret = __LINE__; goto done;
}

#if OPENSSL_VERSION_NUMBER >= PACKED_OPENSSL_VERSION_PLAIN(1,1,0) && !defined(HAS_LIBRESSL)
enif_set_option(env, ERL_NIF_OPT_ON_UNLOAD_THREAD, unload_thread);
#endif

if (library_initialized) {
/* Repeated loading of this library (module upgrade).
* Atoms and callbacks are already set, we are done.
*/
ret = 0;
goto done;
}

#ifdef HAS_3_0_API
prov_cnt = 0;
# ifdef FIPS_SUPPORT
if (!(prov[prov_cnt++] = OSSL_PROVIDER_load(NULL, "fips"))) {
ret = __LINE__; goto done;
if ((prov[prov_cnt] = OSSL_PROVIDER_load(NULL, "fips"))) {
prov_cnt++;
}
# endif
if (!(prov[prov_cnt++] = OSSL_PROVIDER_load(NULL, "default"))) {
Expand All @@ -253,18 +265,6 @@ static int initialize(ErlNifEnv* env, ERL_NIF_TERM load_info)
}
#endif

#if OPENSSL_VERSION_NUMBER >= PACKED_OPENSSL_VERSION_PLAIN(1,1,0) && !defined(HAS_LIBRESSL)
enif_set_option(env, ERL_NIF_OPT_ON_UNLOAD_THREAD, unload_thread);
#endif

if (library_initialized) {
/* Repeated loading of this library (module upgrade).
* Atoms and callbacks are already set, we are done.
*/
ret = 0;
goto done;
}

if (!init_atoms(env)) {
ret = __LINE__; goto done;
}
Expand Down Expand Up @@ -384,12 +384,12 @@ static void unload(ErlNifEnv* env, void* priv_data)
if (--library_refc == 0) {
destroy_curve_mutex();
destroy_engine_mutex(env);
}

#ifdef HAS_3_0_API
while (prov_cnt>0)
OSSL_PROVIDER_unload(prov[--prov_cnt]);
while (prov_cnt > 0) {
OSSL_PROVIDER_unload(prov[--prov_cnt]);
}
#endif

}
}

4 changes: 3 additions & 1 deletion lib/crypto/c_src/openssl_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,9 @@ do { \
#if defined(FIPS_SUPPORT) && \
defined(HAS_3_0_API)
# define FIPS_mode() EVP_default_properties_is_fips_enabled(NULL)
# define FIPS_mode_set(enable) EVP_default_properties_enable_fips(NULL, enable)
# define FIPS_mode_set(enable) \
((!enable || OSSL_PROVIDER_available(NULL, "fips")) \
&& EVP_default_properties_enable_fips(NULL, enable))
#endif


Expand Down
Loading