diff options
author | Tobias Nießen <tniessen@tnie.de> | 2020-05-11 20:55:37 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2020-06-22 21:50:38 +0300 |
commit | 7b019fd3c9a1348857927587a84458f7e3449b69 (patch) | |
tree | e7a5bd0fd4c79d96f798e45cbdba9673f0d791d5 /src/node_crypto.h | |
parent | 7f185ec3c417f66ce485084d06084f1438faa5af (diff) |
crypto: allow KeyObjects in postMessage
This change allows sharing KeyObjects between threads via postMessage.
The receiver acquires a new KeyObject and a new KeyObjectHandle, but
refers to the same KeyObjectData:
+-------------------+
| NativeKeyObject 1 | ------------------------------------------+
+-------------------+ |
^ |
extends |
| |
+-------------------+ +-------------------+ |
| KeyObject 1 (JS) | -> | KeyObjectHandle 1 | --------------+ |
+-------------------+ +-------------------+ | |
| |
| |
| |
| |
| |
+-------------------+ | |
| NativeKeyObject 2 | ------------------------------------+ | |
+-------------------+ | | |
^ | | |
extends | | |
| | | |
+-------------------+ +-------------------+ | | |
| KeyObject 2 (JS) | -> | KeyObjectHandle 2 | --------+ | | |
+-------------------+ +-------------------+ | | | |
| | | |
| | | |
| | | |
| | | |
| | | |
+-------------------+ | | | |
| NativeKeyObject 3 | ------------------------------+ | | | |
+-------------------+ | | | | |
^ | | | | |
extends | | | | |
| v v v v v
+-------------------+ +-------------------+ +---------------+
| KeyObject 3 (JS) | -> | KeyObjectHandle 3 | -> | KeyObjectData |
+-------------------+ +-------------------+ +---------------+
Co-authored-by: Anna Henningsen <anna@addaleax.net>
PR-URL: https://github.com/nodejs/node/pull/33360
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/node_crypto.h')
-rw-r--r-- | src/node_crypto.h | 67 |
1 files changed, 56 insertions, 11 deletions
diff --git a/src/node_crypto.h b/src/node_crypto.h index 9aaa188baa9..a61089f0f13 100644 --- a/src/node_crypto.h +++ b/src/node_crypto.h @@ -31,6 +31,7 @@ #include "env.h" #include "base_object.h" #include "util.h" +#include "node_messaging.h" #include "v8.h" @@ -408,11 +409,13 @@ class ManagedEVPPKey { EVPKeyPointer pkey_; }; +// Objects of this class can safely be shared among threads. class KeyObjectData { public: - static KeyObjectData* CreateSecret(v8::Local<v8::ArrayBufferView> abv); - static KeyObjectData* CreateAsymmetric(KeyType type, - const ManagedEVPPKey& pkey); + static std::shared_ptr<KeyObjectData> CreateSecret( + v8::Local<v8::ArrayBufferView> abv); + static std::shared_ptr<KeyObjectData> CreateAsymmetric( + KeyType type, const ManagedEVPPKey& pkey); KeyType GetKeyType() const; @@ -423,10 +426,23 @@ class KeyObjectData { size_t GetSymmetricKeySize() const; private: - KeyType key_type_; - std::unique_ptr<char, std::function<void(char*)>> symmetric_key_; - unsigned int symmetric_key_len_; - ManagedEVPPKey asymmetric_key_; + KeyObjectData(std::unique_ptr<char, std::function<void(char*)>> symmetric_key, + unsigned int symmetric_key_len) + : key_type_(KeyType::kKeyTypeSecret), + symmetric_key_(std::move(symmetric_key)), + symmetric_key_len_(symmetric_key_len), + asymmetric_key_() {} + + KeyObjectData(KeyType type, const ManagedEVPPKey& pkey) + : key_type_(type), + symmetric_key_(), + symmetric_key_len_(0), + asymmetric_key_{pkey} {} + + const KeyType key_type_; + const std::unique_ptr<char, std::function<void(char*)>> symmetric_key_; + const unsigned int symmetric_key_len_; + const ManagedEVPPKey asymmetric_key_; }; class KeyObjectHandle : public BaseObject { @@ -435,15 +451,14 @@ class KeyObjectHandle : public BaseObject { v8::Local<v8::Object> target); static v8::MaybeLocal<v8::Object> Create(Environment* env, - KeyType type, - const ManagedEVPPKey& pkey); + std::shared_ptr<KeyObjectData> data); // TODO(tniessen): track the memory used by OpenSSL types SET_NO_MEMORY_INFO() SET_MEMORY_INFO_NAME(KeyObjectHandle) SET_SELF_SIZE(KeyObjectHandle) - const KeyObjectData* Data(); + const std::shared_ptr<KeyObjectData>& Data(); protected: static void New(const v8::FunctionCallbackInfo<v8::Value>& args); @@ -468,7 +483,7 @@ class KeyObjectHandle : public BaseObject { v8::Local<v8::Object> wrap); private: - std::unique_ptr<KeyObjectData> data_; + std::shared_ptr<KeyObjectData> data_; }; class NativeKeyObject : public BaseObject { @@ -478,6 +493,36 @@ class NativeKeyObject : public BaseObject { SET_NO_MEMORY_INFO() SET_MEMORY_INFO_NAME(NativeKeyObject) SET_SELF_SIZE(NativeKeyObject) + + class KeyObjectTransferData : public worker::TransferData { + public: + explicit KeyObjectTransferData(const std::shared_ptr<KeyObjectData>& data) + : data_(data) {} + + BaseObjectPtr<BaseObject> Deserialize( + Environment* env, + v8::Local<v8::Context> context, + std::unique_ptr<worker::TransferData> self) override; + + SET_MEMORY_INFO_NAME(KeyObjectTransferData) + SET_SELF_SIZE(KeyObjectTransferData) + SET_NO_MEMORY_INFO() + + private: + std::shared_ptr<KeyObjectData> data_; + }; + + BaseObject::TransferMode GetTransferMode() const override; + std::unique_ptr<worker::TransferData> CloneForMessaging() const override; + + private: + NativeKeyObject(Environment* env, + v8::Local<v8::Object> wrap, + const std::shared_ptr<KeyObjectData>& handle_data) + : BaseObject(env, wrap), + handle_data_(handle_data) {} + + std::shared_ptr<KeyObjectData> handle_data_; }; class CipherBase : public BaseObject { |