src: update ECKeyPointer in ncrypto · nodejs/node@33f5345 · GitHub
Skip to content

Commit 33f5345

Browse files
jasnelladuh95
authored andcommitted
src: update ECKeyPointer in ncrypto
PR-URL: #56526 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent c7b95fc commit 33f5345

8 files changed

Lines changed: 227 additions & 73 deletions

File tree

deps/ncrypto/ncrypto.cc

Lines changed: 120 additions & 0 deletions

deps/ncrypto/ncrypto.h

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
#include <openssl/fips.h>
2929
#endif // OPENSSL_FIPS
3030

31+
#if OPENSSL_VERSION_MAJOR >= 3
32+
#define OSSL3_CONST const
33+
#else
34+
#define OSSL3_CONST
35+
#endif
36+
3137
#ifdef __GNUC__
3238
#define NCRYPTO_MUST_USE_RESULT __attribute__((warn_unused_result))
3339
#else
@@ -197,7 +203,6 @@ using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer;
197203

198204
using BignumCtxPointer = DeleteFnPtr<BN_CTX, BN_CTX_free>;
199205
using BignumGenCallbackPointer = DeleteFnPtr<BN_GENCB, BN_GENCB_free>;
200-
using ECKeyPointer = DeleteFnPtr<EC_KEY, EC_KEY_free>;
201206
using EVPKeyCtxPointer = DeleteFnPtr<EVP_PKEY_CTX, EVP_PKEY_CTX_free>;
202207
using EVPMDCtxPointer = DeleteFnPtr<EVP_MD_CTX, EVP_MD_CTX_free>;
203208
using HMACCtxPointer = DeleteFnPtr<HMAC_CTX, HMAC_CTX_free>;
@@ -207,6 +212,7 @@ using RSAPointer = DeleteFnPtr<RSA, RSA_free>;
207212
using SSLSessionPointer = DeleteFnPtr<SSL_SESSION, SSL_SESSION_free>;
208213

209214
class CipherCtxPointer;
215+
class ECKeyPointer;
210216

211217
struct StackOfXASN1Deleter {
212218
void operator()(STACK_OF(ASN1_OBJECT) * p) const {
@@ -537,6 +543,10 @@ class EVPKeyPointer final {
537543
NCRYPTO_DISALLOW_COPY(EVPKeyPointer)
538544
~EVPKeyPointer();
539545

546+
bool assign(const ECKeyPointer& eckey);
547+
bool set(const ECKeyPointer& eckey);
548+
operator const EC_KEY*() const;
549+
540550
inline bool operator==(std::nullptr_t) const noexcept {
541551
return pkey_ == nullptr;
542552
}
@@ -898,6 +908,46 @@ class ECPointPointer final {
898908
DeleteFnPtr<EC_POINT, EC_POINT_free> point_;
899909
};
900910

911+
class ECKeyPointer final {
912+
public:
913+
ECKeyPointer();
914+
explicit ECKeyPointer(EC_KEY* key);
915+
ECKeyPointer(ECKeyPointer&& other) noexcept;
916+
ECKeyPointer& operator=(ECKeyPointer&& other) noexcept;
917+
NCRYPTO_DISALLOW_COPY(ECKeyPointer)
918+
~ECKeyPointer();
919+
920+
inline bool operator==(std::nullptr_t) noexcept { return key_ == nullptr; }
921+
inline operator bool() const { return key_ != nullptr; }
922+
inline EC_KEY* get() const { return key_.get(); }
923+
inline operator EC_KEY*() const { return key_.get(); }
924+
void reset(EC_KEY* key = nullptr);
925+
EC_KEY* release();
926+
927+
ECKeyPointer clone() const;
928+
bool setPrivateKey(const BignumPointer& priv);
929+
bool setPublicKey(const ECPointPointer& pub);
930+
bool setPublicKeyRaw(const BignumPointer& x, const BignumPointer& y);
931+
bool generate();
932+
bool checkKey() const;
933+
934+
const EC_GROUP* getGroup() const;
935+
const BIGNUM* getPrivateKey() const;
936+
const EC_POINT* getPublicKey() const;
937+
938+
static ECKeyPointer New(const EC_GROUP* group);
939+
static ECKeyPointer NewByCurveName(int nid);
940+
941+
static const EC_POINT* GetPublicKey(const EC_KEY* key);
942+
static const BIGNUM* GetPrivateKey(const EC_KEY* key);
943+
static const EC_GROUP* GetGroup(const EC_KEY* key);
944+
static int GetGroupName(const EC_KEY* key);
945+
static bool Check(const EC_KEY* key);
946+
947+
private:
948+
DeleteFnPtr<EC_KEY, EC_KEY_free> key_;
949+
};
950+
901951
#ifndef OPENSSL_NO_ENGINE
902952
class EnginePointer final {
903953
public:

src/crypto/crypto_common.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
namespace node {
2929

3030
using ncrypto::ClearErrorOnReturn;
31+
using ncrypto::ECKeyPointer;
3132
using ncrypto::EVPKeyPointer;
3233
using ncrypto::SSLPointer;
3334
using ncrypto::SSLSessionPointer;
@@ -271,8 +272,7 @@ MaybeLocal<Object> GetEphemeralKey(Environment* env, const SSLPointer& ssl) {
271272
{
272273
const char* curve_name;
273274
if (kid == EVP_PKEY_EC) {
274-
OSSL3_CONST EC_KEY* ec = EVP_PKEY_get0_EC_KEY(key.get());
275-
int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
275+
int nid = ECKeyPointer::GetGroupName(key);
276276
curve_name = OBJ_nid2sn(nid);
277277
} else {
278278
curve_name = OBJ_nid2sn(kid);

src/crypto/crypto_common.h

Lines changed: 0 additions & 8 deletions

0 commit comments

Comments
 (0)