| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/webcrypto/blink_key_handle.h" | 5 #include "components/webcrypto/blink_key_handle.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "components/webcrypto/crypto_data.h" | 11 #include "components/webcrypto/crypto_data.h" |
| 12 #include "components/webcrypto/status.h" | 12 #include "components/webcrypto/status.h" |
| 13 | 13 |
| 14 namespace webcrypto { | 14 namespace webcrypto { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 class SymKey; | 18 class SymKey; |
| 19 class AsymKey; | 19 class AsymKey; |
| 20 | 20 |
| 21 // Base class for wrapping OpenSSL keys in a type that can be passed to | 21 // Base class for wrapping OpenSSL keys in a type that can be passed to |
| 22 // Blink (blink::WebCryptoKeyHandle). | 22 // Blink (blink::WebCryptoKeyHandle). |
| 23 // | 23 // |
| 24 // In addition to the key's internal OpenSSL representation (EVP_PKEY or just | 24 // In addition to the key's internal OpenSSL representation (EVP_PKEY or just |
| 25 // raw bytes), each key maintains a copy of its serialized form in either | 25 // raw bytes), each key maintains a copy of its serialized form in either |
| 26 // 'raw', 'pkcs8', or 'spki' format. This is to allow structured cloning of | 26 // 'raw', 'pkcs8', or 'spki' format. This is to allow structured cloning of |
| 27 // keys to be done synchronously from the target Blink thread, without having to | 27 // keys to be done synchronously from the target Blink thread, without having to |
| 28 // lock access to the key throughout the code. | 28 // lock access to the key throughout the code. |
| 29 // |
| 30 // TODO(eroman): Should be able to do the key export needed for structured |
| 31 // clone synchronously. |
| 29 class Key : public blink::WebCryptoKeyHandle { | 32 class Key : public blink::WebCryptoKeyHandle { |
| 30 public: | 33 public: |
| 31 explicit Key(const CryptoData& serialized_key_data) | 34 explicit Key(const CryptoData& serialized_key_data) |
| 32 : serialized_key_data_( | 35 : serialized_key_data_( |
| 33 serialized_key_data.bytes(), | 36 serialized_key_data.bytes(), |
| 34 serialized_key_data.bytes() + serialized_key_data.byte_length()) {} | 37 serialized_key_data.bytes() + serialized_key_data.byte_length()) {} |
| 35 | 38 |
| 36 ~Key() override {} | 39 ~Key() override {} |
| 37 | 40 |
| 38 // Helpers to add some safety to casting. | 41 // Helpers to add some safety to casting. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 56 const std::vector<uint8_t>& raw_key_data() const { | 59 const std::vector<uint8_t>& raw_key_data() const { |
| 57 return serialized_key_data(); | 60 return serialized_key_data(); |
| 58 } | 61 } |
| 59 | 62 |
| 60 private: | 63 private: |
| 61 DISALLOW_COPY_AND_ASSIGN(SymKey); | 64 DISALLOW_COPY_AND_ASSIGN(SymKey); |
| 62 }; | 65 }; |
| 63 | 66 |
| 64 class AsymKey : public Key { | 67 class AsymKey : public Key { |
| 65 public: | 68 public: |
| 69 // After construction the |pkey| should NOT be mutated. |
| 66 AsymKey(crypto::ScopedEVP_PKEY pkey, | 70 AsymKey(crypto::ScopedEVP_PKEY pkey, |
| 67 const std::vector<uint8_t>& serialized_key_data) | 71 const std::vector<uint8_t>& serialized_key_data) |
| 68 : Key(CryptoData(serialized_key_data)), pkey_(std::move(pkey)) {} | 72 : Key(CryptoData(serialized_key_data)), pkey_(std::move(pkey)) {} |
| 69 | 73 |
| 70 AsymKey* AsAsymKey() override { return this; } | 74 AsymKey* AsAsymKey() override { return this; } |
| 71 | 75 |
| 76 // The caller should NOT mutate this EVP_PKEY. |
| 72 EVP_PKEY* pkey() { return pkey_.get(); } | 77 EVP_PKEY* pkey() { return pkey_.get(); } |
| 73 | 78 |
| 74 private: | 79 private: |
| 75 crypto::ScopedEVP_PKEY pkey_; | 80 crypto::ScopedEVP_PKEY pkey_; |
| 76 | 81 |
| 77 DISALLOW_COPY_AND_ASSIGN(AsymKey); | 82 DISALLOW_COPY_AND_ASSIGN(AsymKey); |
| 78 }; | 83 }; |
| 79 | 84 |
| 80 Key* GetKey(const blink::WebCryptoKey& key) { | 85 Key* GetKey(const blink::WebCryptoKey& key) { |
| 81 return reinterpret_cast<Key*>(key.handle()); | 86 return reinterpret_cast<Key*>(key.handle()); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 104 return new SymKey(key_bytes); | 109 return new SymKey(key_bytes); |
| 105 } | 110 } |
| 106 | 111 |
| 107 blink::WebCryptoKeyHandle* CreateAsymmetricKeyHandle( | 112 blink::WebCryptoKeyHandle* CreateAsymmetricKeyHandle( |
| 108 crypto::ScopedEVP_PKEY pkey, | 113 crypto::ScopedEVP_PKEY pkey, |
| 109 const std::vector<uint8_t>& serialized_key_data) { | 114 const std::vector<uint8_t>& serialized_key_data) { |
| 110 return new AsymKey(std::move(pkey), serialized_key_data); | 115 return new AsymKey(std::move(pkey), serialized_key_data); |
| 111 } | 116 } |
| 112 | 117 |
| 113 } // namespace webcrypto | 118 } // namespace webcrypto |
| OLD | NEW |