1 // Copyright 2014 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 #ifndef COMPONENTS_WEBCRYPTO_OPENSSL_KEY_OPENSSL_H_
6 #define COMPONENTS_WEBCRYPTO_OPENSSL_KEY_OPENSSL_H_
8 #include <openssl/ossl_typ.h>
12 #include "base/macros.h"
13 #include "crypto/scoped_openssl_types.h"
14 #include "third_party/WebKit/public/platform/WebCryptoKey.h"
22 // Base key class for all OpenSSL keys, used to safely cast between types. Each
23 // key maintains a copy of its serialized form in either 'raw', 'pkcs8', or
24 // 'spki' format. This is to allow structured cloning of keys synchronously from
25 // the target Blink thread without having to lock access to the key.
26 class KeyOpenSsl
: public blink::WebCryptoKeyHandle
{
28 explicit KeyOpenSsl(const CryptoData
& serialized_key_data
);
29 ~KeyOpenSsl() override
;
31 virtual SymKeyOpenSsl
* AsSymKey();
32 virtual AsymKeyOpenSsl
* AsAsymKey();
34 const std::vector
<uint8_t>& serialized_key_data() const {
35 return serialized_key_data_
;
39 const std::vector
<uint8_t> serialized_key_data_
;
42 class SymKeyOpenSsl
: public KeyOpenSsl
{
44 ~SymKeyOpenSsl() override
;
45 explicit SymKeyOpenSsl(const CryptoData
& raw_key_data
);
47 static SymKeyOpenSsl
* Cast(const blink::WebCryptoKey
& key
);
49 SymKeyOpenSsl
* AsSymKey() override
;
51 const std::vector
<uint8_t>& raw_key_data() const {
52 return serialized_key_data();
56 DISALLOW_COPY_AND_ASSIGN(SymKeyOpenSsl
);
59 class AsymKeyOpenSsl
: public KeyOpenSsl
{
61 ~AsymKeyOpenSsl() override
;
62 AsymKeyOpenSsl(crypto::ScopedEVP_PKEY key
,
63 const CryptoData
& serialized_key_data
);
65 static AsymKeyOpenSsl
* Cast(const blink::WebCryptoKey
& key
);
67 AsymKeyOpenSsl
* AsAsymKey() override
;
69 EVP_PKEY
* key() { return key_
.get(); }
72 crypto::ScopedEVP_PKEY key_
;
74 DISALLOW_COPY_AND_ASSIGN(AsymKeyOpenSsl
);
77 } // namespace webcrypto
79 #endif // COMPONENTS_WEBCRYPTO_OPENSSL_KEY_OPENSSL_H_