From 21a0fe9fc5bff1372a6ac646e6914dd1d596c9de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Sun, 10 May 2020 15:37:09 +0000 Subject: [PATCH] src: rename internal key handles to KeyObjectHandle PR-URL: https://github.com/nodejs/node/pull/33360 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- lib/internal/crypto/keys.js | 2 +- src/env.h | 1 + src/node_crypto.cc | 89 ++++++++++++++++++++----------------- src/node_crypto.h | 12 ++--- 4 files changed, 56 insertions(+), 48 deletions(-) diff --git a/lib/internal/crypto/keys.js b/lib/internal/crypto/keys.js index bfb30dda3fd752..09e8f54bfa85d6 100644 --- a/lib/internal/crypto/keys.js +++ b/lib/internal/crypto/keys.js @@ -6,7 +6,7 @@ const { } = primordials; const { - KeyObject: KeyObjectHandle, + KeyObjectHandle, kKeyTypeSecret, kKeyTypePublic, kKeyTypePrivate, diff --git a/src/env.h b/src/env.h index a35448dc191690..014e66ac3b907d 100644 --- a/src/env.h +++ b/src/env.h @@ -451,6 +451,7 @@ constexpr size_t kFsStatsBufferLength = V(async_hooks_promise_resolve_function, v8::Function) \ V(buffer_prototype_object, v8::Object) \ V(crypto_key_object_constructor, v8::Function) \ + V(crypto_key_object_handle_constructor, v8::Function) \ V(domexception_function, v8::Function) \ V(enhance_fatal_stack_after_inspector, v8::Function) \ V(enhance_fatal_stack_before_inspector, v8::Function) \ diff --git a/src/node_crypto.cc b/src/node_crypto.cc index d570b9df333c8e..906b6b7fad733c 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -2886,9 +2886,9 @@ ByteSource ByteSource::NullTerminatedCopy(Environment* env, : FromString(env, value.As(), true); } -ByteSource ByteSource::FromSymmetricKeyObject(Local handle) { +ByteSource ByteSource::FromSymmetricKeyObjectHandle(Local handle) { CHECK(handle->IsObject()); - KeyObject* key = Unwrap(handle.As()); + KeyObjectHandle* key = Unwrap(handle.As()); CHECK_NOT_NULL(key); return Foreign(key->GetSymmetricKey(), key->GetSymmetricKeySize()); } @@ -3034,7 +3034,7 @@ static ManagedEVPPKey GetPrivateKeyFromJs( "Failed to read private key"); } else { CHECK(args[*offset]->IsObject() && allow_key_object); - KeyObject* key; + KeyObjectHandle* key; ASSIGN_OR_RETURN_UNWRAP(&key, args[*offset].As(), ManagedEVPPKey()); CHECK_EQ(key->GetKeyType(), kKeyTypePrivate); (*offset) += 4; @@ -3094,7 +3094,7 @@ static ManagedEVPPKey GetPublicOrPrivateKeyFromJs( "Failed to read asymmetric key"); } else { CHECK(args[*offset]->IsObject()); - KeyObject* key = Unwrap(args[*offset].As()); + KeyObjectHandle* key = Unwrap(args[*offset].As()); CHECK_NOT_NULL(key); CHECK_NE(key->GetKeyType(), kKeyTypeSecret); (*offset) += 4; @@ -3205,10 +3205,11 @@ EVP_PKEY* ManagedEVPPKey::get() const { return pkey_.get(); } -Local KeyObject::Initialize(Environment* env, Local target) { +Local KeyObjectHandle::Initialize(Environment* env, + Local target) { Local t = env->NewFunctionTemplate(New); t->InstanceTemplate()->SetInternalFieldCount( - KeyObject::kInternalFieldCount); + KeyObjectHandle::kInternalFieldCount); t->Inherit(BaseObject::GetConstructorTemplate(env)); env->SetProtoMethod(t, "init", Init); @@ -3220,25 +3221,25 @@ Local KeyObject::Initialize(Environment* env, Local target) { auto function = t->GetFunction(env->context()).ToLocalChecked(); target->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "KeyObject"), + FIXED_ONE_BYTE_STRING(env->isolate(), "KeyObjectHandle"), function).Check(); return function; } -MaybeLocal KeyObject::Create(Environment* env, - KeyType key_type, - const ManagedEVPPKey& pkey) { +MaybeLocal KeyObjectHandle::Create(Environment* env, + KeyType key_type, + const ManagedEVPPKey& pkey) { CHECK_NE(key_type, kKeyTypeSecret); Local type = Integer::New(env->isolate(), key_type); Local obj; - if (!env->crypto_key_object_constructor() + if (!env->crypto_key_object_handle_constructor() ->NewInstance(env->context(), 1, &type) .ToLocal(&obj)) { return MaybeLocal(); } - KeyObject* key = Unwrap(obj); + KeyObjectHandle* key = Unwrap(obj); CHECK_NOT_NULL(key); if (key_type == kKeyTypePublic) key->InitPublic(pkey); @@ -3247,44 +3248,44 @@ MaybeLocal KeyObject::Create(Environment* env, return obj; } -ManagedEVPPKey KeyObject::GetAsymmetricKey() const { +ManagedEVPPKey KeyObjectHandle::GetAsymmetricKey() const { CHECK_NE(key_type_, kKeyTypeSecret); return this->asymmetric_key_; } -const char* KeyObject::GetSymmetricKey() const { +const char* KeyObjectHandle::GetSymmetricKey() const { CHECK_EQ(key_type_, kKeyTypeSecret); return this->symmetric_key_.get(); } -size_t KeyObject::GetSymmetricKeySize() const { +size_t KeyObjectHandle::GetSymmetricKeySize() const { CHECK_EQ(key_type_, kKeyTypeSecret); return this->symmetric_key_len_; } -void KeyObject::New(const FunctionCallbackInfo& args) { +void KeyObjectHandle::New(const FunctionCallbackInfo& args) { CHECK(args.IsConstructCall()); CHECK(args[0]->IsInt32()); KeyType key_type = static_cast(args[0].As()->Value()); Environment* env = Environment::GetCurrent(args); - new KeyObject(env, args.This(), key_type); + new KeyObjectHandle(env, args.This(), key_type); } -KeyType KeyObject::GetKeyType() const { +KeyType KeyObjectHandle::GetKeyType() const { return this->key_type_; } -KeyObject::KeyObject(Environment* env, - Local wrap, - KeyType key_type) +KeyObjectHandle::KeyObjectHandle(Environment* env, + Local wrap, + KeyType key_type) : BaseObject(env, wrap), key_type_(key_type), symmetric_key_(nullptr, nullptr) { MakeWeak(); } -void KeyObject::Init(const FunctionCallbackInfo& args) { - KeyObject* key; +void KeyObjectHandle::Init(const FunctionCallbackInfo& args) { + KeyObjectHandle* key; ASSIGN_OR_RETURN_UNWRAP(&key, args.Holder()); MarkPopErrorOnReturn mark_pop_error_on_return; @@ -3320,7 +3321,7 @@ void KeyObject::Init(const FunctionCallbackInfo& args) { } } -void KeyObject::InitSecret(Local abv) { +void KeyObjectHandle::InitSecret(Local abv) { CHECK_EQ(this->key_type_, kKeyTypeSecret); size_t key_len = abv->ByteLength(); @@ -3333,19 +3334,19 @@ void KeyObject::InitSecret(Local abv) { this->symmetric_key_len_ = key_len; } -void KeyObject::InitPublic(const ManagedEVPPKey& pkey) { +void KeyObjectHandle::InitPublic(const ManagedEVPPKey& pkey) { CHECK_EQ(this->key_type_, kKeyTypePublic); CHECK(pkey); this->asymmetric_key_ = pkey; } -void KeyObject::InitPrivate(const ManagedEVPPKey& pkey) { +void KeyObjectHandle::InitPrivate(const ManagedEVPPKey& pkey) { CHECK_EQ(this->key_type_, kKeyTypePrivate); CHECK(pkey); this->asymmetric_key_ = pkey; } -Local KeyObject::GetAsymmetricKeyType() const { +Local KeyObjectHandle::GetAsymmetricKeyType() const { CHECK_NE(this->key_type_, kKeyTypeSecret); switch (EVP_PKEY_id(this->asymmetric_key_.get())) { case EVP_PKEY_RSA: @@ -3371,21 +3372,23 @@ Local KeyObject::GetAsymmetricKeyType() const { } } -void KeyObject::GetAsymmetricKeyType(const FunctionCallbackInfo& args) { - KeyObject* key; +void KeyObjectHandle::GetAsymmetricKeyType( + const FunctionCallbackInfo& args) { + KeyObjectHandle* key; ASSIGN_OR_RETURN_UNWRAP(&key, args.Holder()); args.GetReturnValue().Set(key->GetAsymmetricKeyType()); } -void KeyObject::GetSymmetricKeySize(const FunctionCallbackInfo& args) { - KeyObject* key; +void KeyObjectHandle::GetSymmetricKeySize( + const FunctionCallbackInfo& args) { + KeyObjectHandle* key; ASSIGN_OR_RETURN_UNWRAP(&key, args.Holder()); args.GetReturnValue().Set(static_cast(key->GetSymmetricKeySize())); } -void KeyObject::Export(const FunctionCallbackInfo& args) { - KeyObject* key; +void KeyObjectHandle::Export(const FunctionCallbackInfo& args) { + KeyObjectHandle* key; ASSIGN_OR_RETURN_UNWRAP(&key, args.Holder()); MaybeLocal result; @@ -3412,17 +3415,17 @@ void KeyObject::Export(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(result.ToLocalChecked()); } -Local KeyObject::ExportSecretKey() const { +Local KeyObjectHandle::ExportSecretKey() const { return Buffer::Copy(env(), symmetric_key_.get(), symmetric_key_len_) .ToLocalChecked(); } -MaybeLocal KeyObject::ExportPublicKey( +MaybeLocal KeyObjectHandle::ExportPublicKey( const PublicKeyEncodingConfig& config) const { return WritePublicKey(env(), asymmetric_key_.get(), config); } -MaybeLocal KeyObject::ExportPrivateKey( +MaybeLocal KeyObjectHandle::ExportPrivateKey( const PrivateKeyEncodingConfig& config) const { return WritePrivateKey(env(), asymmetric_key_.get(), config); } @@ -3625,7 +3628,7 @@ static ByteSource GetSecretKeyBytes(Environment* env, Local value) { // in JS to avoid creating an unprotected copy on the heap. return value->IsString() || Buffer::HasInstance(value) ? ByteSource::FromStringOrBuffer(env, value) : - ByteSource::FromSymmetricKeyObject(value); + ByteSource::FromSymmetricKeyObjectHandle(value); } void CipherBase::InitIv(const FunctionCallbackInfo& args) { @@ -6287,7 +6290,8 @@ class GenerateKeyPairJob : public CryptoJob { if (public_key_encoding_.output_key_object_) { // Note that this has the downside of containing sensitive data of the // private key. - if (!KeyObject::Create(env(), kKeyTypePublic, pkey_).ToLocal(pubkey)) + if (!KeyObjectHandle::Create(env(), kKeyTypePublic, pkey_) + .ToLocal(pubkey)) return false; } else { if (!WritePublicKey(env(), pkey_.get(), public_key_encoding_) @@ -6297,7 +6301,7 @@ class GenerateKeyPairJob : public CryptoJob { // Now do the same for the private key. if (private_key_encoding_.output_key_object_) { - if (!KeyObject::Create(env(), kKeyTypePrivate, pkey_) + if (!KeyObjectHandle::Create(env(), kKeyTypePrivate, pkey_) .ToLocal(privkey)) return false; } else { @@ -6741,10 +6745,10 @@ void StatelessDiffieHellman(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); CHECK(args[0]->IsObject() && args[1]->IsObject()); - KeyObject* our_key_object; + KeyObjectHandle* our_key_object; ASSIGN_OR_RETURN_UNWRAP(&our_key_object, args[0].As()); CHECK_EQ(our_key_object->GetKeyType(), kKeyTypePrivate); - KeyObject* their_key_object; + KeyObjectHandle* their_key_object; ASSIGN_OR_RETURN_UNWRAP(&their_key_object, args[1].As()); CHECK_NE(their_key_object->GetKeyType(), kKeyTypeSecret); @@ -6895,7 +6899,8 @@ void Initialize(Local target, Environment* env = Environment::GetCurrent(context); SecureContext::Initialize(env, target); - env->set_crypto_key_object_constructor(KeyObject::Initialize(env, target)); + env->set_crypto_key_object_handle_constructor( + KeyObjectHandle::Initialize(env, target)); CipherBase::Initialize(env, target); DiffieHellman::Initialize(env, target); ECDH::Initialize(env, target); diff --git a/src/node_crypto.h b/src/node_crypto.h index 772a34a7da7699..c6e4824896718b 100644 --- a/src/node_crypto.h +++ b/src/node_crypto.h @@ -342,7 +342,7 @@ class ByteSource { static ByteSource NullTerminatedCopy(Environment* env, v8::Local value); - static ByteSource FromSymmetricKeyObject(v8::Local handle); + static ByteSource FromSymmetricKeyObjectHandle(v8::Local handle); ByteSource(const ByteSource&) = delete; ByteSource& operator=(const ByteSource&) = delete; @@ -407,7 +407,7 @@ class ManagedEVPPKey { EVPKeyPointer pkey_; }; -class KeyObject : public BaseObject { +class KeyObjectHandle : public BaseObject { public: static v8::Local Initialize(Environment* env, v8::Local target); @@ -418,8 +418,8 @@ class KeyObject : public BaseObject { // TODO(tniessen): track the memory used by OpenSSL types SET_NO_MEMORY_INFO() - SET_MEMORY_INFO_NAME(KeyObject) - SET_SELF_SIZE(KeyObject) + SET_MEMORY_INFO_NAME(KeyObjectHandle) + SET_SELF_SIZE(KeyObjectHandle) KeyType GetKeyType() const; @@ -451,7 +451,9 @@ class KeyObject : public BaseObject { v8::MaybeLocal ExportPrivateKey( const PrivateKeyEncodingConfig& config) const; - KeyObject(Environment* env, v8::Local wrap, KeyType key_type); + KeyObjectHandle(Environment* env, + v8::Local wrap, + KeyType key_type); private: const KeyType key_type_;