Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / child / webcrypto / nss / key_nss.h
blob9eaf7c427b081030ff1497b4e2ae8b5b285944a2
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_
8 #include <stdint.h>
9 #include <vector>
11 #include "crypto/scoped_nss_types.h"
12 #include "third_party/WebKit/public/platform/WebCryptoKey.h"
14 namespace content {
16 namespace webcrypto {
18 class CryptoData;
19 class PrivateKeyNss;
20 class PublicKeyNss;
21 class SymKeyNss;
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 {
28 public:
29 explicit KeyNss(const CryptoData& serialized_key_data);
30 ~KeyNss() override;
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_;
40 private:
41 const std::vector<uint8_t> serialized_key_data_;
44 class SymKeyNss : public KeyNss {
45 public:
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();
58 private:
59 crypto::ScopedPK11SymKey key_;
61 DISALLOW_COPY_AND_ASSIGN(SymKeyNss);
64 class PublicKeyNss : public KeyNss {
65 public:
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();
78 private:
79 crypto::ScopedSECKEYPublicKey key_;
81 DISALLOW_COPY_AND_ASSIGN(PublicKeyNss);
84 class PrivateKeyNss : public KeyNss {
85 public:
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();
99 private:
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_