Skip to content

Commit

Permalink
src: update ECGroupPointer in ncrypto
Browse files Browse the repository at this point in the history
PR-URL: #56526
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
  • Loading branch information
jasnell authored and nodejs-github-bot committed Jan 14, 2025
1 parent 6e23885 commit d3cb7c0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
30 changes: 30 additions & 0 deletions deps/ncrypto/ncrypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2677,4 +2677,34 @@ Buffer<unsigned char> 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
23 changes: 22 additions & 1 deletion deps/ncrypto/ncrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer;

using BignumCtxPointer = DeleteFnPtr<BN_CTX, BN_CTX_free>;
using BignumGenCallbackPointer = DeleteFnPtr<BN_GENCB, BN_GENCB_free>;
using ECGroupPointer = DeleteFnPtr<EC_GROUP, EC_GROUP_free>;
using ECKeyPointer = DeleteFnPtr<EC_KEY, EC_KEY_free>;
using ECPointPointer = DeleteFnPtr<EC_POINT, EC_POINT_free>;
using EVPKeyCtxPointer = DeleteFnPtr<EVP_PKEY_CTX, EVP_PKEY_CTX_free>;
Expand Down Expand Up @@ -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<EC_GROUP, EC_GROUP_free> group_;
};

#ifndef OPENSSL_NO_ENGINE
class EnginePointer final {
public:
Expand Down
12 changes: 4 additions & 8 deletions src/crypto/crypto_ec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,11 @@ void ECDH::ConvertKey(const FunctionCallbackInfo<Value>& 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,
Expand All @@ -420,7 +416,7 @@ void ECDH::ConvertKey(const FunctionCallbackInfo<Value>& args) {

const char* error;
Local<Object> 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);
}
Expand Down

0 comments on commit d3cb7c0

Please sign in to comment.