Skip to content
Open
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
204 changes: 195 additions & 9 deletions deps/ncrypto/ncrypto.cc

Large diffs are not rendered by default.

46 changes: 41 additions & 5 deletions deps/ncrypto/ncrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@
#ifdef OPENSSL_IS_BORINGSSL
#define NCRYPTO_USE_BORINGSSL 1
#define NCRYPTO_USE_OPENSSL_PROVIDER 0
#else
#elif OPENSSL_VERSION_PREREQ(3, 0)
#define NCRYPTO_USE_BORINGSSL 0
#define NCRYPTO_USE_OPENSSL_PROVIDER 1
#else
#error "Unsupported crypto backend"
#endif

#define NCRYPTO_USE_LEGACY_KEY_TYPES NCRYPTO_USE_BORINGSSL
Expand Down Expand Up @@ -404,6 +406,8 @@ class DigestCache final {
static_cast<void>(id);
static_cast<void>(generation);
return {};
#else
#error "Unsupported crypto backend"
#endif
}
Result insert(const char* name, const EVP_MD* digest, uint64_t generation);
Expand Down Expand Up @@ -591,6 +595,8 @@ class Dsa final {
explicit Dsa(const EVP_PKEY* pkey);
#elif NCRYPTO_USE_BORINGSSL
Dsa(OSSL3_CONST DSA* dsa);
#else
#error "Unsupported crypto backend"
#endif
NCRYPTO_DISALLOW_COPY_AND_MOVE(Dsa)

Expand All @@ -600,6 +606,8 @@ class Dsa final {
}
#elif NCRYPTO_USE_BORINGSSL
inline operator bool() const { return dsa_ != nullptr; }
#else
#error "Unsupported crypto backend"
#endif
#if NCRYPTO_USE_LEGACY_KEY_TYPES
inline operator OSSL3_CONST DSA*() const { return dsa_; }
Expand All @@ -617,6 +625,8 @@ class Dsa final {
DeleteFnPtr<BIGNUM, BN_free> q_;
#elif NCRYPTO_USE_BORINGSSL
OSSL3_CONST DSA* dsa_;
#else
#error "Unsupported crypto backend"
#endif
};

Expand All @@ -632,6 +642,8 @@ class Rsa final {
explicit Rsa(const EVP_PKEY* pkey, Selection selection = Selection::Private);
#elif NCRYPTO_USE_BORINGSSL
Rsa(OSSL3_CONST RSA* rsa);
#else
#error "Unsupported crypto backend"
#endif
NCRYPTO_DISALLOW_COPY_AND_MOVE(Rsa)

Expand All @@ -641,6 +653,8 @@ class Rsa final {
}
#elif NCRYPTO_USE_BORINGSSL
inline operator bool() const { return rsa_ != nullptr; }
#else
#error "Unsupported crypto backend"
#endif
#if NCRYPTO_USE_LEGACY_KEY_TYPES
inline operator OSSL3_CONST RSA*() const { return rsa_; }
Expand Down Expand Up @@ -724,6 +738,8 @@ class Rsa final {
std::optional<PssParams> pss_params_;
#elif NCRYPTO_USE_BORINGSSL
OSSL3_CONST RSA* rsa_;
#else
#error "Unsupported crypto backend"
#endif
};

Expand All @@ -734,6 +750,8 @@ class Ec final {
explicit Ec(const EVP_PKEY* pkey);
#elif NCRYPTO_USE_BORINGSSL
Ec(OSSL3_CONST EC_KEY* key);
#else
#error "Unsupported crypto backend"
#endif
NCRYPTO_DISALLOW_COPY_AND_MOVE(Ec)

Expand Down Expand Up @@ -772,6 +790,8 @@ class Ec final {
point_conversion_form_t form_ = POINT_CONVERSION_UNCOMPRESSED;
#elif NCRYPTO_USE_BORINGSSL
OSSL3_CONST EC_KEY* ec_ = nullptr;
#else
#error "Unsupported crypto backend"
#endif
};

Expand Down Expand Up @@ -1216,6 +1236,8 @@ class EVPKeyPointer final {
static EVPKeyPointer NewRSA(const Rsa& rsa);
#elif NCRYPTO_USE_BORINGSSL
static EVPKeyPointer NewRSA(RSAPointer&& rsa);
#else
#error "Unsupported crypto backend"
#endif

enum class PKEncodingType {
Expand Down Expand Up @@ -1404,6 +1426,8 @@ class DHPointer final {
DHPointer(BignumPointer&& p, BignumPointer&& g, const char* group_name);
#elif NCRYPTO_USE_BORINGSSL
explicit DHPointer(DH* dh);
#else
#error "Unsupported crypto backend"
#endif
DHPointer(DHPointer&& other) noexcept;
DHPointer& operator=(DHPointer&& other) noexcept;
Expand All @@ -1420,17 +1444,21 @@ class DHPointer final {
#elif NCRYPTO_USE_BORINGSSL
inline bool operator==(std::nullptr_t) noexcept { return dh_ == nullptr; }
inline operator bool() const { return dh_ != nullptr; }
#else
#error "Unsupported crypto backend"
#endif
#if NCRYPTO_USE_LEGACY_KEY_TYPES
#if NCRYPTO_USE_BORINGSSL
inline DH* get() const { return dh_.get(); }
void reset(DH* dh = nullptr);
DH* release();
#else
#elif NCRYPTO_USE_OPENSSL_PROVIDER
inline EVP_PKEY* get() const {
return dh_.get();
}
void reset(EVP_PKEY* dh = nullptr);
EVP_PKEY* release();
#else
#error "Unsupported crypto backend"
#endif

enum class CheckResult {
Expand Down Expand Up @@ -1488,6 +1516,8 @@ class DHPointer final {
const char* group_name_ = nullptr;
#elif NCRYPTO_USE_BORINGSSL
DeleteFnPtr<DH, DH_free> dh_;
#else
#error "Unsupported crypto backend"
#endif
};

Expand Down Expand Up @@ -1824,14 +1854,18 @@ class ECKeyPointer final {
#elif NCRYPTO_USE_BORINGSSL
inline bool operator==(std::nullptr_t) noexcept { return key_ == nullptr; }
inline operator bool() const { return key_ != nullptr; }
#else
#error "Unsupported crypto backend"
#endif
#if NCRYPTO_USE_LEGACY_KEY_TYPES
#if NCRYPTO_USE_BORINGSSL
inline EC_KEY* get() const { return key_.get(); }
inline operator EC_KEY*() const { return key_.get(); }
void reset(EC_KEY* key = nullptr);
EC_KEY* release();
#else
#elif NCRYPTO_USE_OPENSSL_PROVIDER
void reset();
#else
#error "Unsupported crypto backend"
#endif

ECKeyPointer clone() const;
Expand Down Expand Up @@ -1865,6 +1899,8 @@ class ECKeyPointer final {
DeleteFnPtr<BIGNUM, BN_clear_free> priv_;
#elif NCRYPTO_USE_BORINGSSL
DeleteFnPtr<EC_KEY, EC_KEY_free> key_;
#else
#error "Unsupported crypto backend"
#endif
};

Expand Down
14 changes: 14 additions & 0 deletions src/crypto/crypto_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1876,6 +1876,8 @@ void SecureContext::Init(const FunctionCallbackInfo<Value>& args) {
TicketCompatibilityCallback);
#elif NCRYPTO_USE_BORINGSSL
SSL_CTX_set_tlsext_ticket_key_cb(sc->ctx_.get(), TicketCompatibilityCallback);
#else
#error "Unsupported crypto backend"
#endif
}

Expand Down Expand Up @@ -2140,6 +2142,8 @@ void SecureContext::SetDHParam(const FunctionCallbackInfo<Value>& args) {
if (params && params.isA(KeyAlgorithm::DH)) dh.reset(params.release());
#elif NCRYPTO_USE_BORINGSSL
dh.reset(PEM_read_bio_DHparams(bio.get(), nullptr, nullptr, nullptr));
#else
#error "Unsupported crypto backend"
#endif
}

Expand All @@ -2164,6 +2168,8 @@ void SecureContext::SetDHParam(const FunctionCallbackInfo<Value>& args) {
if (!SSL_CTX_set0_tmp_dh_pkey(sc->ctx_.get(), dh_pkey.get())) {
#elif NCRYPTO_USE_BORINGSSL
if (!SSL_CTX_set_tmp_dh(sc->ctx_.get(), dh.get())) {
#else
#error "Unsupported crypto backend"
#endif
return THROW_ERR_CRYPTO_OPERATION_FAILED(
env, "Error setting temp DH parameter");
Expand Down Expand Up @@ -2515,6 +2521,8 @@ void SecureContext::EnableTicketKeyCallback(
SSL_CTX_set_tlsext_ticket_key_evp_cb(wrap->ctx_.get(), TicketKeyCallback);
#elif NCRYPTO_USE_BORINGSSL
SSL_CTX_set_tlsext_ticket_key_cb(wrap->ctx_.get(), TicketKeyCallback);
#else
#error "Unsupported crypto backend"
#endif
}

Expand All @@ -2536,6 +2544,8 @@ bool InitTicketHmac(EVP_MAC_CTX* hctx,
bool InitTicketHmac(HMAC_CTX* hctx, const unsigned char* key, size_t key_len) {
return HMAC_Init_ex(hctx, key, key_len, Digest::SHA256, nullptr) == 1;
}
#else
#error "Unsupported crypto backend"
#endif
} // namespace

Expand All @@ -2547,6 +2557,8 @@ int SecureContext::TicketKeyCallback(SSL* ssl,
EVP_MAC_CTX* hctx,
#elif NCRYPTO_USE_BORINGSSL
HMAC_CTX* hctx,
#else
#error "Unsupported crypto backend"
#endif
int enc) {
static const int kTicketPartSize = 16;
Expand Down Expand Up @@ -2644,6 +2656,8 @@ int SecureContext::TicketCompatibilityCallback(SSL* ssl,
EVP_MAC_CTX* hctx,
#elif NCRYPTO_USE_BORINGSSL
HMAC_CTX* hctx,
#else
#error "Unsupported crypto backend"
#endif
int enc) {
SecureContext* sc = static_cast<SecureContext*>(
Expand Down
4 changes: 4 additions & 0 deletions src/crypto/crypto_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ class SecureContext final : public BaseObject {
EVP_MAC_CTX* hctx,
#elif NCRYPTO_USE_BORINGSSL
HMAC_CTX* hctx,
#else
#error "Unsupported crypto backend"
#endif
int enc);

Expand All @@ -181,6 +183,8 @@ class SecureContext final : public BaseObject {
EVP_MAC_CTX* hctx,
#elif NCRYPTO_USE_BORINGSSL
HMAC_CTX* hctx,
#else
#error "Unsupported crypto backend"
#endif
int enc);

Expand Down
12 changes: 12 additions & 0 deletions src/crypto/crypto_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ void SaveSupportedHashAlgorithms(const EVP_MD* md,
Environment* env = static_cast<Environment*>(arg);
env->supported_hash_algorithms.push_back(from);
}
#else
#error "Unsupported crypto backend"
#endif // NCRYPTO_USE_OPENSSL_PROVIDER

const std::vector<std::string>& GetSupportedHashAlgorithms(Environment* env) {
Expand All @@ -255,6 +257,8 @@ const std::vector<std::string>& GetSupportedHashAlgorithms(Environment* env) {
EVP_MD_do_all_provided(nullptr, SaveSupportedProviderHashAlgorithms, env);
#elif NCRYPTO_USE_BORINGSSL
EVP_MD_do_all_sorted(SaveSupportedHashAlgorithms, env);
#else
#error "Unsupported crypto backend"
#endif
#if NCRYPTO_USE_BORINGSSL
// SHA-3 descriptors are not included in BoringSSL's name registry.
Expand Down Expand Up @@ -354,6 +358,8 @@ const EVP_MD* GetDigestImplementation(
#elif NCRYPTO_USE_BORINGSSL
Utf8Value utf8(env->isolate(), algorithm);
return ncrypto::getDigestByName(*utf8);
#else
#error "Unsupported crypto backend"
#endif
}

Expand All @@ -362,6 +368,8 @@ void MarkInvalidXofLength() {
ERR_raise(ERR_LIB_EVP, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
#elif NCRYPTO_USE_BORINGSSL
EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
#else
#error "Unsupported crypto backend"
#endif
}

Expand All @@ -379,6 +387,8 @@ bool IsShakeDigest(const EVP_MD* md) {
const char* name = OBJ_nid2sn(EVP_MD_type(md));
return name != nullptr &&
(strcmp(name, "SHAKE128") == 0 || strcmp(name, "SHAKE256") == 0);
#else
#error "Unsupported crypto backend"
#endif
}
#endif
Expand Down Expand Up @@ -550,6 +560,8 @@ void Hash::OneShotDigest(const FunctionCallbackInfo<Value>& args) {
Utf8Value utf8(env->isolate(), args[0]);
return OneShotDigestWithMD(
env, args, ncrypto::getDigestByName(*utf8), nullptr);
#else
#error "Unsupported crypto backend"
#endif
}

Expand Down
6 changes: 6 additions & 0 deletions src/crypto/crypto_rsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ bool IsRsaPssDigestEncodable(const Digest& digest) {
#elif NCRYPTO_USE_BORINGSSL
static_cast<void>(digest);
return true;
#else
#error "Unsupported crypto backend"
#endif
}
} // namespace
Expand Down Expand Up @@ -403,6 +405,8 @@ KeyObjectData ImportJWKRsaKey(Environment* env, Local<Object> jwk) {
}

ncrypto::Rsa rsa_view(rsa.get());
#else
#error "Unsupported crypto backend"
#endif

ByteSource n = ByteSource::FromEncodedString(env, n_value.As<String>());
Expand Down Expand Up @@ -511,6 +515,8 @@ KeyObjectData ImportJWKRsaKey(Environment* env, Local<Object> jwk) {
auto pkey = EVPKeyPointer::NewRSA(rsa_view);
#elif NCRYPTO_USE_BORINGSSL
auto pkey = EVPKeyPointer::NewRSA(std::move(rsa));
#else
#error "Unsupported crypto backend"
#endif
if (!pkey) {
THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Unable to create key pointer");
Expand Down
2 changes: 2 additions & 0 deletions src/crypto/crypto_tls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,8 @@ void TLSWrap::ClearOut() {
const char* fs = nullptr;
#elif NCRYPTO_USE_BORINGSSL
const char* fs = ERR_func_error_string(ssl_err);
#else
#error "Unsupported crypto backend"
#endif
const char* rs = ERR_reason_error_string(ssl_err);
if (!Set(env(), obj, env()->library_string(), ls) ||
Expand Down
2 changes: 2 additions & 0 deletions src/crypto/crypto_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,8 @@ Maybe<void> Decorate(Environment* env,
const char* fs = nullptr;
#elif NCRYPTO_USE_BORINGSSL
const char* fs = ERR_func_error_string(err);
#else
#error "Unsupported crypto backend"
#endif
const char* rs = ERR_reason_error_string(err);

Expand Down
8 changes: 8 additions & 0 deletions test/cctest/test_node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,8 @@ TEST(NodeCrypto, EcKeyComponents) {
EXPECT_EQ(memcmp(raw_public.get(), point.get(), point.size()), 0);
#elif NCRYPTO_USE_BORINGSSL
EXPECT_FALSE(raw_public);
#else
#error "Unsupported crypto backend"
#endif
EXPECT_FALSE(
ncrypto::Ec::TryExportPublic(key, POINT_CONVERSION_COMPRESSED));
Expand Down Expand Up @@ -1015,6 +1017,8 @@ TEST(NodeCrypto, X509FormattingPreservesUnrelatedErrors) {
ERR_raise(ERR_LIB_USER, 1);
#elif NCRYPTO_USE_BORINGSSL
ERR_put_error(ERR_LIB_USER, 0, 1, __FILE__, __LINE__);
#else
#error "Unsupported crypto backend"
#endif
const unsigned long expected = ERR_peek_error(); // NOLINT(runtime/int)
ASSERT_NE(expected, 0UL);
Expand Down Expand Up @@ -1048,6 +1052,8 @@ TEST(NodeCrypto, X509KeyVisitorsHandleMissingPublicKey) {
ERR_raise(ERR_LIB_USER, 1);
#elif NCRYPTO_USE_BORINGSSL
ERR_put_error(ERR_LIB_USER, 0, 1, __FILE__, __LINE__);
#else
#error "Unsupported crypto backend"
#endif
const unsigned long expected = ERR_peek_error(); // NOLINT(runtime/int)
ASSERT_NE(expected, 0UL);
Expand Down Expand Up @@ -1080,6 +1086,8 @@ TEST(NodeCrypto, X509UsageVisitorPreservesCallbackErrors) {
ERR_raise(ERR_LIB_USER, 1);
#elif NCRYPTO_USE_BORINGSSL
ERR_put_error(ERR_LIB_USER, 0, 1, __FILE__, __LINE__);
#else
#error "Unsupported crypto backend"
#endif
}));
EXPECT_EQ(calls, 1);
Expand Down
Loading