Skip to content

Commit

Permalink
src: cleanup some obsolete includes in crypto_util
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Mar 6, 2025
1 parent df29de0 commit 76afdc6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 35 deletions.
19 changes: 9 additions & 10 deletions deps/ncrypto/ncrypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ DataPointer DataPointer::Alloc(size_t len) {

DataPointer DataPointer::SecureAlloc(size_t len) {
#ifndef OPENSSL_IS_BORINGSSL
printf("... %zu\n", len);
auto ptr = OPENSSL_secure_zalloc(len);
if (ptr == nullptr) return {};
return DataPointer(ptr, len, true);
Expand Down Expand Up @@ -236,9 +235,10 @@ bool setFipsEnabled(bool enable, CryptoErrorList* errors) {
if (isFipsEnabled() == enable) return true;
ClearErrorOnReturn clearErrorOnReturn(errors);
#if OPENSSL_VERSION_MAJOR >= 3
return EVP_default_properties_enable_fips(nullptr, enable ? 1 : 0) == 1;
return EVP_default_properties_enable_fips(nullptr, enable ? 1 : 0) == 1 &&
EVP_default_properties_is_fips_enabled(nullptr);
#else
return FIPS_mode_set(enable ? 1 : 0) == 1;
return FIPS_mode() == 0 ? FIPS_mode_set(enable ? 1 : 0) == 1 : true;
#endif
}

Expand All @@ -249,18 +249,17 @@ bool testFipsEnabled() {
if (OSSL_PROVIDER_available(nullptr, "fips")) {
fips_provider = OSSL_PROVIDER_load(nullptr, "fips");
}
const auto enabled = fips_provider == nullptr ? 0
: OSSL_PROVIDER_self_test(fips_provider) ? 1
: 0;
if (fips_provider == nullptr) return false;
int result = OSSL_PROVIDER_self_test(fips_provider);
OSSL_PROVIDER_unload(fips_provider);
return result;
#else
#ifdef OPENSSL_FIPS
const auto enabled = FIPS_selftest() ? 1 : 0;
return FIPS_selftest();
#else // OPENSSL_FIPS
const auto enabled = 0;
return false;
#endif // OPENSSL_FIPS
#endif

return enabled;
}

// ============================================================================
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_hkdf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ bool HKDFTraits::DeriveBits(
params.length);
if (!dp) return false;

DCHECK(!data.isSecure());
DCHECK(!dp.isSecure());
*out = ByteSource::Allocated(dp.release());
return true;
}
Expand Down
8 changes: 5 additions & 3 deletions src/crypto/crypto_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
#include "openssl/provider.h"
#endif

#include <openssl/rand.h>

namespace node {

using ncrypto::BignumPointer;
Expand Down Expand Up @@ -85,8 +83,12 @@ bool ProcessFipsOptions() {
/* Override FIPS settings in configuration file, if needed. */
if (per_process::cli_options->enable_fips_crypto ||
per_process::cli_options->force_fips_crypto) {
#if OPENSSL_VERSION_MAJOR >= 3
if (!ncrypto::testFipsEnabled()) return false;
return ncrypto::setFipsEnabled(true, nullptr) && ncrypto::isFipsEnabled();
return ncrypto::setFipsEnabled(true, nullptr);
#else
if (FIPS_mode() == 0) return FIPS_mode_set(1);
#endif
}
return true;
}
Expand Down
23 changes: 2 additions & 21 deletions src/crypto/crypto_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,6 @@

#include "ncrypto.h"

#include <openssl/dsa.h>
#include <openssl/ec.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/kdf.h>
#include <openssl/rsa.h>
#include <openssl/ssl.h>

// The FIPS-related functions are only available
// when the OpenSSL itself was compiled with FIPS support.
#if defined(OPENSSL_FIPS) && OPENSSL_VERSION_MAJOR < 3
# include <openssl/fips.h>
#endif // OPENSSL_FIPS

#include <algorithm>
#include <climits>
#include <cstdio>
Expand All @@ -37,9 +22,7 @@
#include <string>
#include <vector>

namespace node {

namespace crypto {
namespace node::crypto {
// Currently known sizes of commonly used OpenSSL struct sizes.
// OpenSSL considers it's various structs to be opaque and the
// sizes may change from one version of OpenSSL to another, so
Expand Down Expand Up @@ -665,9 +648,7 @@ namespace Util {
void Initialize(Environment* env, v8::Local<v8::Object> target);
void RegisterExternalReferences(ExternalReferenceRegistry* registry);
} // namespace Util

} // namespace crypto
} // namespace node
} // namespace node::crypto

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#endif // SRC_CRYPTO_CRYPTO_UTIL_H_

0 comments on commit 76afdc6

Please sign in to comment.