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_NSS_KEY_NSS_H_
6 #define COMPONENTS_WEBCRYPTO_NSS_KEY_NSS_H_
11 #include "crypto/scoped_nss_types.h"
12 #include "third_party/WebKit/public/platform/WebCryptoKey.h"
21 // Base key class for all NSS keys, used to safely cast between types. Each key
22 // maintains a copy of its serialized form in either 'raw', 'pkcs8', or 'spki'
23 // format. This is to allow structured cloning of keys synchronously from the
24 // target Blink thread without having to lock access to the key.
25 class KeyNss
: public blink::WebCryptoKeyHandle
{
27 explicit KeyNss(const CryptoData
& serialized_key_data
);
30 virtual SymKeyNss
* AsSymKey();
31 virtual PublicKeyNss
* AsPublicKey();
32 virtual PrivateKeyNss
* AsPrivateKey();
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 SymKeyNss
: public KeyNss
{
44 ~SymKeyNss() override
;
45 SymKeyNss(crypto::ScopedPK11SymKey key
, const CryptoData
& raw_key_data
);
47 static SymKeyNss
* Cast(const blink::WebCryptoKey
& key
);
49 PK11SymKey
* key() { return key_
.get(); }
50 SymKeyNss
* AsSymKey() override
;
52 const std::vector
<uint8_t>& raw_key_data() const {
53 return serialized_key_data();
57 crypto::ScopedPK11SymKey key_
;
59 DISALLOW_COPY_AND_ASSIGN(SymKeyNss
);
62 class PublicKeyNss
: public KeyNss
{
64 ~PublicKeyNss() override
;
65 PublicKeyNss(crypto::ScopedSECKEYPublicKey key
, const CryptoData
& spki_data
);
67 static PublicKeyNss
* Cast(const blink::WebCryptoKey
& key
);
69 SECKEYPublicKey
* key() { return key_
.get(); }
70 PublicKeyNss
* AsPublicKey() override
;
72 const std::vector
<uint8_t>& spki_data() const {
73 return serialized_key_data();
77 crypto::ScopedSECKEYPublicKey key_
;
79 DISALLOW_COPY_AND_ASSIGN(PublicKeyNss
);
82 class PrivateKeyNss
: public KeyNss
{
84 ~PrivateKeyNss() override
;
85 PrivateKeyNss(crypto::ScopedSECKEYPrivateKey key
,
86 const CryptoData
& pkcs8_data
);
88 static PrivateKeyNss
* Cast(const blink::WebCryptoKey
& key
);
90 SECKEYPrivateKey
* key() { return key_
.get(); }
91 PrivateKeyNss
* AsPrivateKey() override
;
93 const std::vector
<uint8_t>& pkcs8_data() const {
94 return serialized_key_data();
98 crypto::ScopedSECKEYPrivateKey key_
;
100 DISALLOW_COPY_AND_ASSIGN(PrivateKeyNss
);
103 } // namespace webcrypto
105 #endif // COMPONENTS_WEBCRYPTO_NSS_KEY_NSS_H_