| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2707,4 +2707,46 @@ ECGroupPointer ECGroupPointer::NewByCurveName(int nid) { | |||
| 2707 | 2707 | return ECGroupPointer(EC_GROUP_new_by_curve_name(nid)); | |
| 2708 | 2708 | } | |
| 2709 | 2709 | ||
| 2710 | + // ============================================================================ | ||
| 2711 | + | ||
| 2712 | + ECPointPointer::ECPointPointer() : point_(nullptr) {} | ||
| 2713 | + | ||
| 2714 | + ECPointPointer::ECPointPointer(EC_POINT* point) : point_(point) {} | ||
| 2715 | + | ||
| 2716 | + ECPointPointer::ECPointPointer(ECPointPointer&& other) noexcept | ||
| 2717 | + : point_(other.release()) {} | ||
| 2718 | + | ||
| 2719 | + ECPointPointer& ECPointPointer::operator=(ECPointPointer&& other) noexcept { | ||
| 2720 | + point_.reset(other.release()); | ||
| 2721 | + return *this; | ||
| 2722 | + } | ||
| 2723 | + | ||
| 2724 | + ECPointPointer::~ECPointPointer() { | ||
| 2725 | + reset(); | ||
| 2726 | + } | ||
| 2727 | + | ||
| 2728 | + void ECPointPointer::reset(EC_POINT* point) { | ||
| 2729 | + point_.reset(point); | ||
| 2730 | + } | ||
| 2731 | + | ||
| 2732 | + EC_POINT* ECPointPointer::release() { | ||
| 2733 | + return point_.release(); | ||
| 2734 | + } | ||
| 2735 | + | ||
| 2736 | + ECPointPointer ECPointPointer::New(const EC_GROUP* group) { | ||
| 2737 | + return ECPointPointer(EC_POINT_new(group)); | ||
| 2738 | + } | ||
| 2739 | + | ||
| 2740 | + bool ECPointPointer::setFromBuffer(const Buffer<const unsigned char>& buffer, | ||
| 2741 | + const EC_GROUP* group) { | ||
| 2742 | + if (!point_) return false; | ||
| 2743 | + return EC_POINT_oct2point( | ||
| 2744 | + group, point_.get(), buffer.data, buffer.len, nullptr); | ||
| 2745 | + } | ||
| 2746 | + | ||
| 2747 | + bool ECPointPointer::mul(const EC_GROUP* group, const BIGNUM* priv_key) { | ||
| 2748 | + if (!point_) return false; | ||
| 2749 | + return EC_POINT_mul(group, point_.get(), priv_key, nullptr, nullptr, nullptr); | ||
| 2750 | + } | ||
| 2751 | + | ||
| 2710 | 2752 | } // namespace ncrypto | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -198,7 +198,6 @@ using DeleteFnPtr = typename FunctionDeleter<T, function>::Pointer; | |||
| 198 | 198 | using BignumCtxPointer = DeleteFnPtr<BN_CTX, BN_CTX_free>; | |
| 199 | 199 | using BignumGenCallbackPointer = DeleteFnPtr<BN_GENCB, BN_GENCB_free>; | |
| 200 | 200 | using ECKeyPointer = DeleteFnPtr<EC_KEY, EC_KEY_free>; | |
| 201 | - using ECPointPointer = DeleteFnPtr<EC_POINT, EC_POINT_free>; | ||
| 202 | 201 | using EVPKeyCtxPointer = DeleteFnPtr<EVP_PKEY_CTX, EVP_PKEY_CTX_free>; | |
| 203 | 202 | using EVPMDCtxPointer = DeleteFnPtr<EVP_MD_CTX, EVP_MD_CTX_free>; | |
| 204 | 203 | using HMACCtxPointer = DeleteFnPtr<HMAC_CTX, HMAC_CTX_free>; | |
@@ -873,6 +872,32 @@ class ECGroupPointer final { | |||
| 873 | 872 | DeleteFnPtr<EC_GROUP, EC_GROUP_free> group_; | |
| 874 | 873 | }; | |
| 875 | 874 | ||
| 875 | + class ECPointPointer final { | ||
| 876 | + public: | ||
| 877 | + ECPointPointer(); | ||
| 878 | + explicit ECPointPointer(EC_POINT* point); | ||
| 879 | + ECPointPointer(ECPointPointer&& other) noexcept; | ||
| 880 | + ECPointPointer& operator=(ECPointPointer&& other) noexcept; | ||
| 881 | + NCRYPTO_DISALLOW_COPY(ECPointPointer) | ||
| 882 | + ~ECPointPointer(); | ||
| 883 | + | ||
| 884 | + inline bool operator==(std::nullptr_t) noexcept { return point_ == nullptr; } | ||
| 885 | + inline operator bool() const { return point_ != nullptr; } | ||
| 886 | + inline EC_POINT* get() const { return point_.get(); } | ||
| 887 | + inline operator EC_POINT*() const { return point_.get(); } | ||
| 888 | + void reset(EC_POINT* point = nullptr); | ||
| 889 | + EC_POINT* release(); | ||
| 890 | + | ||
| 891 | + bool setFromBuffer(const Buffer<const unsigned char>& buffer, | ||
| 892 | + const EC_GROUP* group); | ||
| 893 | + bool mul(const EC_GROUP* group, const BIGNUM* priv_key); | ||
| 894 | + | ||
| 895 | + static ECPointPointer New(const EC_GROUP* group); | ||
| 896 | + | ||
| 897 | + private: | ||
| 898 | + DeleteFnPtr<EC_POINT, EC_POINT_free> point_; | ||
| 899 | + }; | ||
| 900 | + | ||
| 876 | 901 | #ifndef OPENSSL_NO_ENGINE | |
| 877 | 902 | class EnginePointer final { | |
| 878 | 903 | public: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -157,28 +157,26 @@ void ECDH::GenerateKeys(const FunctionCallbackInfo<Value>& args) { | |||
| 157 | 157 | ECPointPointer ECDH::BufferToPoint(Environment* env, | |
| 158 | 158 | const EC_GROUP* group, | |
| 159 | 159 | Local<Value> buf) { | |
| 160 | - int r; | ||
| 160 | + ArrayBufferOrViewContents<unsigned char> input(buf); | ||
| 161 | + if (!input.CheckSizeInt32()) [[unlikely]] { | ||
| 162 | + THROW_ERR_OUT_OF_RANGE(env, "buffer is too big"); | ||
| 163 | + return {}; | ||
| 164 | + } | ||
| 161 | 165 | ||
| 162 | - ECPointPointer pub(EC_POINT_new(group)); | ||
| 166 | + auto pub = ECPointPointer::New(group); | ||
| 163 | 167 | if (!pub) { | |
| 164 | 168 | THROW_ERR_CRYPTO_OPERATION_FAILED(env, | |
| 165 | 169 | "Failed to allocate EC_POINT for a public key"); | |
| 166 | 170 | return pub; | |
| 167 | 171 | } | |
| 168 | 172 | ||
| 169 | - ArrayBufferOrViewContents<unsigned char> input(buf); | ||
| 170 | - if (!input.CheckSizeInt32()) [[unlikely]] { | ||
| 171 | - THROW_ERR_OUT_OF_RANGE(env, "buffer is too big"); | ||
| 172 | - return ECPointPointer(); | ||
| 173 | + ncrypto::Buffer<const unsigned char> buffer{ | ||
| 174 | + .data = input.data(), | ||
| 175 | + .len = input.size(), | ||
| 176 | + }; | ||
| 177 | + if (!pub.setFromBuffer(buffer, group)) { | ||
| 178 | + return {}; | ||
| 173 | 179 | } | |
| 174 | - r = EC_POINT_oct2point( | ||
| 175 | - group, | ||
| 176 | - pub.get(), | ||
| 177 | - input.data(), | ||
| 178 | - input.size(), | ||
| 179 | - nullptr); | ||
| 180 | - if (!r) | ||
| 181 | - return ECPointPointer(); | ||
| 182 | 180 | ||
| 183 | 181 | return pub; | |
| 184 | 182 | } | |
@@ -196,10 +194,7 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) { | |||
| 196 | 194 | if (!ecdh->IsKeyPairValid()) | |
| 197 | 195 | return THROW_ERR_CRYPTO_INVALID_KEYPAIR(env); | |
| 198 | 196 | ||
| 199 | - ECPointPointer pub( | ||
| 200 | - ECDH::BufferToPoint(env, | ||
| 201 | - ecdh->group_, | ||
| 202 | - args[0])); | ||
| 197 | + auto pub = ECDH::BufferToPoint(env, ecdh->group_, args[0]); | ||
| 203 | 198 | if (!pub) { | |
| 204 | 199 | args.GetReturnValue().Set( | |
| 205 | 200 | FIXED_ONE_BYTE_STRING(env->isolate(), | |
@@ -217,7 +212,7 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) { | |||
| 217 | 212 | } | |
| 218 | 213 | ||
| 219 | 214 | if (!ECDH_compute_key( | |
| 220 | - bs->Data(), bs->ByteLength(), pub.get(), ecdh->key_.get(), nullptr)) | ||
| 215 | + bs->Data(), bs->ByteLength(), pub, ecdh->key_.get(), nullptr)) | ||
| 221 | 216 | return THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to compute ECDH key"); | |
| 222 | 217 | ||
| 223 | 218 | Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(bs)); | |
@@ -317,16 +312,15 @@ void ECDH::SetPrivateKey(const FunctionCallbackInfo<Value>& args) { | |||
| 317 | 312 | const BIGNUM* priv_key = EC_KEY_get0_private_key(new_key.get()); | |
| 318 | 313 | CHECK_NOT_NULL(priv_key); | |
| 319 | 314 | ||
| 320 | - ECPointPointer pub(EC_POINT_new(ecdh->group_)); | ||
| 315 | + auto pub = ECPointPointer::New(ecdh->group_); | ||
| 321 | 316 | CHECK(pub); | |
| 322 | 317 | ||
| 323 | - if (!EC_POINT_mul(ecdh->group_, pub.get(), priv_key, | ||
| 324 | - nullptr, nullptr, nullptr)) { | ||
| 318 | + if (!pub.mul(ecdh->group_, priv_key)) { | ||
| 325 | 319 | return THROW_ERR_CRYPTO_OPERATION_FAILED(env, | |
| 326 | 320 | "Failed to generate ECDH public key"); | |
| 327 | 321 | } | |
| 328 | 322 | ||
| 329 | - if (!EC_KEY_set_public_key(new_key.get(), pub.get())) | ||
| 323 | + if (!EC_KEY_set_public_key(new_key.get(), pub)) | ||
| 330 | 324 | return THROW_ERR_CRYPTO_OPERATION_FAILED(env, | |
| 331 | 325 | "Failed to set generated public key"); | |
| 332 | 326 | ||
@@ -344,16 +338,13 @@ void ECDH::SetPublicKey(const FunctionCallbackInfo<Value>& args) { | |||
| 344 | 338 | ||
| 345 | 339 | MarkPopErrorOnReturn mark_pop_error_on_return; | |
| 346 | 340 | ||
| 347 | - ECPointPointer pub( | ||
| 348 | - ECDH::BufferToPoint(env, | ||
| 349 | - ecdh->group_, | ||
| 350 | - args[0])); | ||
| 341 | + auto pub = ECDH::BufferToPoint(env, ecdh->group_, args[0]); | ||
| 351 | 342 | if (!pub) { | |
| 352 | 343 | return THROW_ERR_CRYPTO_OPERATION_FAILED(env, | |
| 353 | 344 | "Failed to convert Buffer to EC_POINT"); | |
| 354 | 345 | } | |
| 355 | 346 | ||
| 356 | - int r = EC_KEY_set_public_key(ecdh->key_.get(), pub.get()); | ||
| 347 | + int r = EC_KEY_set_public_key(ecdh->key_.get(), pub); | ||
| 357 | 348 | if (!r) { | |
| 358 | 349 | return THROW_ERR_CRYPTO_OPERATION_FAILED(env, | |
| 359 | 350 | "Failed to set EC_POINT as the public key"); | |
@@ -403,9 +394,8 @@ void ECDH::ConvertKey(const FunctionCallbackInfo<Value>& args) { | |||
| 403 | 394 | if (!group) | |
| 404 | 395 | return THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to get EC_GROUP"); | |
| 405 | 396 | ||
| 406 | - ECPointPointer pub(ECDH::BufferToPoint(env, group, args[0])); | ||
| 407 | - | ||
| 408 | - if (pub == nullptr) { | ||
| 397 | + auto pub = ECDH::BufferToPoint(env, group, args[0]); | ||
| 398 | + if (!pub) { | ||
| 409 | 399 | return THROW_ERR_CRYPTO_OPERATION_FAILED(env, | |
| 410 | 400 | "Failed to convert Buffer to EC_POINT"); | |
| 411 | 401 | } | |
@@ -416,7 +406,7 @@ void ECDH::ConvertKey(const FunctionCallbackInfo<Value>& args) { | |||
| 416 | 406 | ||
| 417 | 407 | const char* error; | |
| 418 | 408 | Local<Object> buf; | |
| 419 | - if (!ECPointToBuffer(env, group, pub.get(), form, &error).ToLocal(&buf)) | ||
| 409 | + if (!ECPointToBuffer(env, group, pub, form, &error).ToLocal(&buf)) | ||
| 420 | 410 | return THROW_ERR_CRYPTO_OPERATION_FAILED(env, error); | |
| 421 | 411 | args.GetReturnValue().Set(buf); | |
| 422 | 412 | } | |
@@ -698,14 +688,13 @@ WebCryptoKeyExportStatus ECKeyExportTraits::DoExport( | |||
| 698 | 688 | if (have == 0) return WebCryptoKeyExportStatus::FAILED; | |
| 699 | 689 | ECKeyPointer ec(EC_KEY_new()); | |
| 700 | 690 | CHECK_EQ(1, EC_KEY_set_group(ec.get(), group)); | |
| 701 | - ECPointPointer uncompressed(EC_POINT_new(group)); | ||
| 702 | - CHECK_EQ(1, | ||
| 703 | - EC_POINT_oct2point(group, | ||
| 704 | - uncompressed.get(), | ||
| 705 | - data.data<unsigned char>(), | ||
| 706 | - data.size(), | ||
| 707 | - nullptr)); | ||
| 708 | - CHECK_EQ(1, EC_KEY_set_public_key(ec.get(), uncompressed.get())); | ||
| 691 | + auto uncompressed = ECPointPointer::New(group); | ||
| 692 | + ncrypto::Buffer<const unsigned char> buffer{ | ||
| 693 | + .data = data.data<unsigned char>(), | ||
| 694 | + .len = data.size(), | ||
| 695 | + }; | ||
| 696 | + CHECK(uncompressed.setFromBuffer(buffer, group)); | ||
| 697 | + CHECK_EQ(1, EC_KEY_set_public_key(ec.get(), uncompressed)); | ||
| 709 | 698 | auto pkey = EVPKeyPointer::New(); | |
| 710 | 699 | CHECK_EQ(1, EVP_PKEY_set1_EC_KEY(pkey.get(), ec.get())); | |
| 711 | 700 | auto bio = pkey.derPublicKey(); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments