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
3 // found in the LICENSE file.
5 #include "components/webcrypto/key.h"
7 #include "base/logging.h"
8 #include "base/macros.h"
9 #include "components/webcrypto/crypto_data.h"
10 #include "components/webcrypto/status.h"
11 #include "components/webcrypto/webcrypto_util.h"
20 // Base class for wrapping OpenSSL keys in a type that can be passed to
21 // Blink (blink::WebCryptoKeyHandle).
23 // In addition to the key's internal OpenSSL representation (EVP_PKEY or just
24 // raw bytes), each key maintains a copy of its serialized form in either
25 // 'raw', 'pkcs8', or 'spki' format. This is to allow structured cloning of
26 // keys to be done synchronously from the target Blink thread, without having to
27 // lock access to the key throughout the code.
28 class Key
: public blink::WebCryptoKeyHandle
{
30 explicit Key(const CryptoData
& serialized_key_data
)
31 : serialized_key_data_(
32 serialized_key_data
.bytes(),
33 serialized_key_data
.bytes() + serialized_key_data
.byte_length()) {}
37 // Helpers to add some safety to casting.
38 virtual SymKey
* AsSymKey() { return nullptr; }
39 virtual AsymKey
* AsAsymKey() { return nullptr; }
41 const std::vector
<uint8_t>& serialized_key_data() const {
42 return serialized_key_data_
;
46 const std::vector
<uint8_t> serialized_key_data_
;
49 class SymKey
: public Key
{
51 explicit SymKey(const CryptoData
& raw_key_data
) : Key(raw_key_data
) {}
53 SymKey
* AsSymKey() override
{ return this; }
55 const std::vector
<uint8_t>& raw_key_data() const {
56 return serialized_key_data();
60 DISALLOW_COPY_AND_ASSIGN(SymKey
);
63 class AsymKey
: public Key
{
65 AsymKey(crypto::ScopedEVP_PKEY pkey
,
66 const std::vector
<uint8_t>& serialized_key_data
)
67 : Key(CryptoData(serialized_key_data
)), pkey_(pkey
.Pass()) {}
69 AsymKey
* AsAsymKey() override
{ return this; }
71 EVP_PKEY
* pkey() { return pkey_
.get(); }
74 crypto::ScopedEVP_PKEY pkey_
;
76 DISALLOW_COPY_AND_ASSIGN(AsymKey
);
79 Key
* GetKey(const blink::WebCryptoKey
& key
) {
80 return reinterpret_cast<Key
*>(key
.handle());
85 const std::vector
<uint8_t>& GetSymmetricKeyData(
86 const blink::WebCryptoKey
& key
) {
87 DCHECK_EQ(blink::WebCryptoKeyTypeSecret
, key
.type());
88 return GetKey(key
)->AsSymKey()->raw_key_data();
91 EVP_PKEY
* GetEVP_PKEY(const blink::WebCryptoKey
& key
) {
92 DCHECK_NE(blink::WebCryptoKeyTypeSecret
, key
.type());
93 return GetKey(key
)->AsAsymKey()->pkey();
96 const std::vector
<uint8_t>& GetSerializedKeyData(
97 const blink::WebCryptoKey
& key
) {
98 return GetKey(key
)->serialized_key_data();
101 blink::WebCryptoKeyHandle
* CreateSymmetricKeyHandle(
102 const CryptoData
& key_bytes
) {
103 return new SymKey(key_bytes
);
106 blink::WebCryptoKeyHandle
* CreateAsymmetricKeyHandle(
107 crypto::ScopedEVP_PKEY pkey
,
108 const std::vector
<uint8_t>& serialized_key_data
) {
109 return new AsymKey(pkey
.Pass(), serialized_key_data
);
112 } // namespace webcrypto