Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 33f5345

Browse filesBrowse 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
Copy full SHA for 33f5345

File tree

Expand file treeCollapse file tree

8 files changed

+227
-73
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

8 files changed

+227
-73
lines changed
Open diff view settings
Collapse file

‎deps/ncrypto/ncrypto.cc‎

Copy file name to clipboardExpand all lines: deps/ncrypto/ncrypto.cc
+120Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,21 @@ BIOPointer EVPKeyPointer::derPublicKey() const {
17901790
return bio;
17911791
}
17921792

1793+
bool EVPKeyPointer::assign(const ECKeyPointer& eckey) {
1794+
if (!pkey_ || !eckey) return {};
1795+
return EVP_PKEY_assign_EC_KEY(pkey_.get(), eckey.get());
1796+
}
1797+
1798+
bool EVPKeyPointer::set(const ECKeyPointer& eckey) {
1799+
if (!pkey_ || !eckey) return false;
1800+
return EVP_PKEY_set1_EC_KEY(pkey_.get(), eckey);
1801+
}
1802+
1803+
EVPKeyPointer::operator const EC_KEY*() const {
1804+
if (!pkey_) return nullptr;
1805+
return EVP_PKEY_get0_EC_KEY(pkey_.get());
1806+
}
1807+
17931808
namespace {
17941809
EVPKeyPointer::ParseKeyResult TryParsePublicKeyInner(const BIOPointer& bp,
17951810
const char* name,
@@ -2749,4 +2764,109 @@ bool ECPointPointer::mul(const EC_GROUP* group, const BIGNUM* priv_key) {
27492764
return EC_POINT_mul(group, point_.get(), priv_key, nullptr, nullptr, nullptr);
27502765
}
27512766

2767+
// ============================================================================
2768+
2769+
ECKeyPointer::ECKeyPointer() : key_(nullptr) {}
2770+
2771+
ECKeyPointer::ECKeyPointer(EC_KEY* key) : key_(key) {}
2772+
2773+
ECKeyPointer::ECKeyPointer(ECKeyPointer&& other) noexcept
2774+
: key_(other.release()) {}
2775+
2776+
ECKeyPointer& ECKeyPointer::operator=(ECKeyPointer&& other) noexcept {
2777+
key_.reset(other.release());
2778+
return *this;
2779+
}
2780+
2781+
ECKeyPointer::~ECKeyPointer() {
2782+
reset();
2783+
}
2784+
2785+
void ECKeyPointer::reset(EC_KEY* key) {
2786+
key_.reset(key);
2787+
}
2788+
2789+
EC_KEY* ECKeyPointer::release() {
2790+
return key_.release();
2791+
}
2792+
2793+
ECKeyPointer ECKeyPointer::clone() const {
2794+
if (!key_) return {};
2795+
return ECKeyPointer(EC_KEY_dup(key_.get()));
2796+
}
2797+
2798+
bool ECKeyPointer::generate() {
2799+
if (!key_) return false;
2800+
return EC_KEY_generate_key(key_.get());
2801+
}
2802+
2803+
bool ECKeyPointer::setPublicKey(const ECPointPointer& pub) {
2804+
if (!key_) return false;
2805+
return EC_KEY_set_public_key(key_.get(), pub.get()) == 1;
2806+
}
2807+
2808+
bool ECKeyPointer::setPublicKeyRaw(const BignumPointer& x,
2809+
const BignumPointer& y) {
2810+
if (!key_) return false;
2811+
return EC_KEY_set_public_key_affine_coordinates(
2812+
key_.get(), x.get(), y.get()) == 1;
2813+
}
2814+
2815+
bool ECKeyPointer::setPrivateKey(const BignumPointer& priv) {
2816+
if (!key_) return false;
2817+
return EC_KEY_set_private_key(key_.get(), priv.get()) == 1;
2818+
}
2819+
2820+
const BIGNUM* ECKeyPointer::getPrivateKey() const {
2821+
if (!key_) return nullptr;
2822+
return GetPrivateKey(key_.get());
2823+
}
2824+
2825+
const BIGNUM* ECKeyPointer::GetPrivateKey(const EC_KEY* key) {
2826+
return EC_KEY_get0_private_key(key);
2827+
}
2828+
2829+
const EC_POINT* ECKeyPointer::getPublicKey() const {
2830+
if (!key_) return nullptr;
2831+
return GetPublicKey(key_.get());
2832+
}
2833+
2834+
const EC_POINT* ECKeyPointer::GetPublicKey(const EC_KEY* key) {
2835+
return EC_KEY_get0_public_key(key);
2836+
}
2837+
2838+
const EC_GROUP* ECKeyPointer::getGroup() const {
2839+
if (!key_) return nullptr;
2840+
return GetGroup(key_.get());
2841+
}
2842+
2843+
const EC_GROUP* ECKeyPointer::GetGroup(const EC_KEY* key) {
2844+
return EC_KEY_get0_group(key);
2845+
}
2846+
2847+
int ECKeyPointer::GetGroupName(const EC_KEY* key) {
2848+
const EC_GROUP* group = GetGroup(key);
2849+
return group ? EC_GROUP_get_curve_name(group) : 0;
2850+
}
2851+
2852+
bool ECKeyPointer::Check(const EC_KEY* key) {
2853+
return EC_KEY_check_key(key) == 1;
2854+
}
2855+
2856+
bool ECKeyPointer::checkKey() const {
2857+
if (!key_) return false;
2858+
return Check(key_.get());
2859+
}
2860+
2861+
ECKeyPointer ECKeyPointer::NewByCurveName(int nid) {
2862+
return ECKeyPointer(EC_KEY_new_by_curve_name(nid));
2863+
}
2864+
2865+
ECKeyPointer ECKeyPointer::New(const EC_GROUP* group) {
2866+
auto ptr = ECKeyPointer(EC_KEY_new());
2867+
if (!ptr) return {};
2868+
if (!EC_KEY_set_group(ptr.get(), group)) return {};
2869+
return ptr;
2870+
}
2871+
27522872
} // namespace ncrypto
Collapse file

‎deps/ncrypto/ncrypto.h‎

Copy file name to clipboardExpand all lines: deps/ncrypto/ncrypto.h
+51-1Lines 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:
Collapse file

‎src/crypto/crypto_common.cc‎

Copy file name to clipboardExpand all lines: src/crypto/crypto_common.cc
+2-2Lines 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);
Collapse file

‎src/crypto/crypto_common.h‎

Copy file name to clipboardExpand all lines: src/crypto/crypto_common.h
-8Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@
1111

1212
#include <string>
1313

14-
// Some OpenSSL 1.1.1 functions unnecessarily operate on and return non-const
15-
// pointers, whereas the same functions in OpenSSL 3 use const pointers.
16-
#if OPENSSL_VERSION_MAJOR >= 3
17-
#define OSSL3_CONST const
18-
#else
19-
#define OSSL3_CONST
20-
#endif
21-
2214
namespace node {
2315
namespace crypto {
2416

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.