From d3cb7c0b96888a440eddd602ed3cc97161fecac0 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Wed, 8 Jan 2025 08:45:24 -0800 Subject: [PATCH] src: update ECGroupPointer in ncrypto PR-URL: https://github.com/nodejs/node/pull/56526 Reviewed-By: Yagiz Nizipli Reviewed-By: Antoine du Hamel --- deps/ncrypto/ncrypto.cc | 30 ++++++++++++++++++++++++++++++ deps/ncrypto/ncrypto.h | 23 ++++++++++++++++++++++- src/crypto/crypto_ec.cc | 12 ++++-------- 3 files changed, 56 insertions(+), 9 deletions(-) diff --git a/deps/ncrypto/ncrypto.cc b/deps/ncrypto/ncrypto.cc index 728970d2aa03ca..fd96af6c53d1cd 100644 --- a/deps/ncrypto/ncrypto.cc +++ b/deps/ncrypto/ncrypto.cc @@ -2677,4 +2677,34 @@ Buffer ECDSASigPointer::encode() const { return buf; } +// ============================================================================ + +ECGroupPointer::ECGroupPointer() : group_(nullptr) {} + +ECGroupPointer::ECGroupPointer(EC_GROUP* group) : group_(group) {} + +ECGroupPointer::ECGroupPointer(ECGroupPointer&& other) noexcept + : group_(other.release()) {} + +ECGroupPointer& ECGroupPointer::operator=(ECGroupPointer&& other) noexcept { + group_.reset(other.release()); + return *this; +} + +ECGroupPointer::~ECGroupPointer() { + reset(); +} + +void ECGroupPointer::reset(EC_GROUP* group) { + group_.reset(); +} + +EC_GROUP* ECGroupPointer::release() { + return group_.release(); +} + +ECGroupPointer ECGroupPointer::NewByCurveName(int nid) { + return ECGroupPointer(EC_GROUP_new_by_curve_name(nid)); +} + } // namespace ncrypto diff --git a/deps/ncrypto/ncrypto.h b/deps/ncrypto/ncrypto.h index 8dc5e968521a42..a4bef279ce3d2d 100644 --- a/deps/ncrypto/ncrypto.h +++ b/deps/ncrypto/ncrypto.h @@ -197,7 +197,6 @@ using DeleteFnPtr = typename FunctionDeleter::Pointer; using BignumCtxPointer = DeleteFnPtr; using BignumGenCallbackPointer = DeleteFnPtr; -using ECGroupPointer = DeleteFnPtr; using ECKeyPointer = DeleteFnPtr; using ECPointPointer = DeleteFnPtr; using EVPKeyCtxPointer = DeleteFnPtr; @@ -852,6 +851,28 @@ class ECDSASigPointer final { const BIGNUM* ps_ = nullptr; }; +class ECGroupPointer final { + public: + explicit ECGroupPointer(); + explicit ECGroupPointer(EC_GROUP* group); + ECGroupPointer(ECGroupPointer&& other) noexcept; + ECGroupPointer& operator=(ECGroupPointer&& other) noexcept; + NCRYPTO_DISALLOW_COPY(ECGroupPointer) + ~ECGroupPointer(); + + inline bool operator==(std::nullptr_t) noexcept { return group_ == nullptr; } + inline operator bool() const { return group_ != nullptr; } + inline EC_GROUP* get() const { return group_.get(); } + inline operator EC_GROUP*() const { return group_.get(); } + void reset(EC_GROUP* group = nullptr); + EC_GROUP* release(); + + static ECGroupPointer NewByCurveName(int nid); + + private: + DeleteFnPtr group_; +}; + #ifndef OPENSSL_NO_ENGINE class EnginePointer final { public: diff --git a/src/crypto/crypto_ec.cc b/src/crypto/crypto_ec.cc index 84b1dd9c2ff3b5..0a1ae82e0b825e 100644 --- a/src/crypto/crypto_ec.cc +++ b/src/crypto/crypto_ec.cc @@ -399,15 +399,11 @@ void ECDH::ConvertKey(const FunctionCallbackInfo& args) { if (nid == NID_undef) return THROW_ERR_CRYPTO_INVALID_CURVE(env); - ECGroupPointer group( - EC_GROUP_new_by_curve_name(nid)); - if (group == nullptr) + auto group = ECGroupPointer::NewByCurveName(nid); + if (!group) return THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to get EC_GROUP"); - ECPointPointer pub( - ECDH::BufferToPoint(env, - group.get(), - args[0])); + ECPointPointer pub(ECDH::BufferToPoint(env, group, args[0])); if (pub == nullptr) { return THROW_ERR_CRYPTO_OPERATION_FAILED(env, @@ -420,7 +416,7 @@ void ECDH::ConvertKey(const FunctionCallbackInfo& args) { const char* error; Local buf; - if (!ECPointToBuffer(env, group.get(), pub.get(), form, &error).ToLocal(&buf)) + if (!ECPointToBuffer(env, group, pub.get(), form, &error).ToLocal(&buf)) return THROW_ERR_CRYPTO_OPERATION_FAILED(env, error); args.GetReturnValue().Set(buf); }