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 CONTENT_CHILD_WEBCRYPTO_NSS_KEY_NSS_H_
6 #define CONTENT_CHILD_WEBCRYPTO_NSS_KEY_NSS_H_
11 #include "crypto/scoped_nss_types.h"
12 #include "third_party/WebKit/public/platform/WebCryptoKey.h"
23 // Base key class for all NSS keys, used to safely cast between types. Each key
24 // maintains a copy of its serialized form in either 'raw', 'pkcs8', or 'spki'
25 // format. This is to allow structured cloning of keys synchronously from the
26 // target Blink thread without having to lock access to the key.
27 class KeyNss
: public blink::WebCryptoKeyHandle
{
29 explicit KeyNss(const CryptoData
& serialized_key_data
);
32 virtual SymKeyNss
* AsSymKey();
33 virtual PublicKeyNss
* AsPublicKey();
34 virtual PrivateKeyNss
* AsPrivateKey();
36 const std::vector
<uint8_t>& serialized_key_data() const {
37 return serialized_key_data_
;
41 const std::vector
<uint8_t> serialized_key_data_
;
44 class SymKeyNss
: public KeyNss
{
46 ~SymKeyNss() override
;
47 SymKeyNss(crypto::ScopedPK11SymKey key
, const CryptoData
& raw_key_data
);
49 static SymKeyNss
* Cast(const blink::WebCryptoKey
& key
);
51 PK11SymKey
* key() { return key_
.get(); }
52 SymKeyNss
* AsSymKey() override
;
54 const std::vector
<uint8_t>& raw_key_data() const {
55 return serialized_key_data();
59 crypto::ScopedPK11SymKey key_
;
61 DISALLOW_COPY_AND_ASSIGN(SymKeyNss
);
64 class PublicKeyNss
: public KeyNss
{
66 ~PublicKeyNss() override
;
67 PublicKeyNss(crypto::ScopedSECKEYPublicKey key
, const CryptoData
& spki_data
);
69 static PublicKeyNss
* Cast(const blink::WebCryptoKey
& key
);
71 SECKEYPublicKey
* key() { return key_
.get(); }
72 PublicKeyNss
* AsPublicKey() override
;
74 const std::vector
<uint8_t>& spki_data() const {
75 return serialized_key_data();
79 crypto::ScopedSECKEYPublicKey key_
;
81 DISALLOW_COPY_AND_ASSIGN(PublicKeyNss
);
84 class PrivateKeyNss
: public KeyNss
{
86 ~PrivateKeyNss() override
;
87 PrivateKeyNss(crypto::ScopedSECKEYPrivateKey key
,
88 const CryptoData
& pkcs8_data
);
90 static PrivateKeyNss
* Cast(const blink::WebCryptoKey
& key
);
92 SECKEYPrivateKey
* key() { return key_
.get(); }
93 PrivateKeyNss
* AsPrivateKey() override
;
95 const std::vector
<uint8_t>& pkcs8_data() const {
96 return serialized_key_data();
100 crypto::ScopedSECKEYPrivateKey key_
;
102 DISALLOW_COPY_AND_ASSIGN(PrivateKeyNss
);
105 } // namespace webcrypto
107 } // namespace content
109 #endif // CONTENT_CHILD_WEBCRYPTO_NSS_KEY_NSS_H_