| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1790,6 +1790,21 @@ BIOPointer EVPKeyPointer::derPublicKey() const { | |||
| 1790 | 1790 | return bio; | |
| 1791 | 1791 | } | |
| 1792 | 1792 | ||
| 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 | + | ||
| 1793 | 1808 | namespace { | |
| 1794 | 1809 | EVPKeyPointer::ParseKeyResult TryParsePublicKeyInner(const BIOPointer& bp, | |
| 1795 | 1810 | const char* name, | |
@@ -2749,4 +2764,109 @@ bool ECPointPointer::mul(const EC_GROUP* group, const BIGNUM* priv_key) { | |||
| 2749 | 2764 | return EC_POINT_mul(group, point_.get(), priv_key, nullptr, nullptr, nullptr); | |
| 2750 | 2765 | } | |
| 2751 | 2766 | ||
| 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 | + | ||
| 2752 | 2872 | } // namespace ncrypto | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,6 +28,12 @@ | |||
| 28 | 28 | #include <openssl/fips.h> | |
| 29 | 29 | #endif // OPENSSL_FIPS | |
| 30 | 30 | ||
| 31 | + #if OPENSSL_VERSION_MAJOR >= 3 | ||
| 32 | + #define OSSL3_CONST const | ||
| 33 | + #else | ||
| 34 | + #define OSSL3_CONST | ||
| 35 | + #endif | ||
| 36 | + | ||
| 31 | 37 | #ifdef __GNUC__ | |
| 32 | 38 | #define NCRYPTO_MUST_USE_RESULT __attribute__((warn_unused_result)) | |
| 33 | 39 | #else | |
@@ -197,7 +203,6 @@ using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer; | |||
| 197 | 203 | ||
| 198 | 204 | using BignumCtxPointer = DeleteFnPtr<BN_CTX, BN_CTX_free>; | |
| 199 | 205 | using BignumGenCallbackPointer = DeleteFnPtr<BN_GENCB, BN_GENCB_free>; | |
| 200 | - using ECKeyPointer = DeleteFnPtr<EC_KEY, EC_KEY_free>; | ||
| 201 | 206 | using EVPKeyCtxPointer = DeleteFnPtr<EVP_PKEY_CTX, EVP_PKEY_CTX_free>; | |
| 202 | 207 | using EVPMDCtxPointer = DeleteFnPtr<EVP_MD_CTX, EVP_MD_CTX_free>; | |
| 203 | 208 | using HMACCtxPointer = DeleteFnPtr<HMAC_CTX, HMAC_CTX_free>; | |
@@ -207,6 +212,7 @@ using RSAPointer = DeleteFnPtr<RSA, RSA_free>; | |||
| 207 | 212 | using SSLSessionPointer = DeleteFnPtr<SSL_SESSION, SSL_SESSION_free>; | |
| 208 | 213 | ||
| 209 | 214 | class CipherCtxPointer; | |
| 215 | + class ECKeyPointer; | ||
| 210 | 216 | ||
| 211 | 217 | struct StackOfXASN1Deleter { | |
| 212 | 218 | void operator()(STACK_OF(ASN1_OBJECT) * p) const { | |
@@ -537,6 +543,10 @@ class EVPKeyPointer final { | |||
| 537 | 543 | NCRYPTO_DISALLOW_COPY(EVPKeyPointer) | |
| 538 | 544 | ~EVPKeyPointer(); | |
| 539 | 545 | ||
| 546 | + bool assign(const ECKeyPointer& eckey); | ||
| 547 | + bool set(const ECKeyPointer& eckey); | ||
| 548 | + operator const EC_KEY*() const; | ||
| 549 | + | ||
| 540 | 550 | inline bool operator==(std::nullptr_t) const noexcept { | |
| 541 | 551 | return pkey_ == nullptr; | |
| 542 | 552 | } | |
@@ -898,6 +908,46 @@ class ECPointPointer final { | |||
| 898 | 908 | DeleteFnPtr<EC_POINT, EC_POINT_free> point_; | |
| 899 | 909 | }; | |
| 900 | 910 | ||
| 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 | + | ||
| 901 | 951 | #ifndef OPENSSL_NO_ENGINE | |
| 902 | 952 | class EnginePointer final { | |
| 903 | 953 | public: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,6 +28,7 @@ | |||
| 28 | 28 | namespace node { | |
| 29 | 29 | ||
| 30 | 30 | using ncrypto::ClearErrorOnReturn; | |
| 31 | + using ncrypto::ECKeyPointer; | ||
| 31 | 32 | using ncrypto::EVPKeyPointer; | |
| 32 | 33 | using ncrypto::SSLPointer; | |
| 33 | 34 | using ncrypto::SSLSessionPointer; | |
@@ -271,8 +272,7 @@ MaybeLocal<Object> GetEphemeralKey(Environment* env, const SSLPointer& ssl) { | |||
| 271 | 272 | { | |
| 272 | 273 | const char* curve_name; | |
| 273 | 274 | 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); | ||
| 276 | 276 | curve_name = OBJ_nid2sn(nid); | |
| 277 | 277 | } else { | |
| 278 | 278 | curve_name = OBJ_nid2sn(kid); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,14 +11,6 @@ | |||
| 11 | 11 | ||
| 12 | 12 | #include <string> | |
| 13 | 13 | ||
| 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 | - | ||
| 22 | 14 | namespace node { | |
| 23 | 15 | namespace crypto { | |
| 24 | 16 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments