summaryrefslogtreecommitdiff
path: root/chromium/content/child/webcrypto/nss/key_nss.h
blob: 9eaf7c427b081030ff1497b4e2ae8b5b285944a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_CHILD_WEBCRYPTO_NSS_KEY_NSS_H_
#define CONTENT_CHILD_WEBCRYPTO_NSS_KEY_NSS_H_

#include <stdint.h>
#include <vector>

#include "crypto/scoped_nss_types.h"
#include "third_party/WebKit/public/platform/WebCryptoKey.h"

namespace content {

namespace webcrypto {

class CryptoData;
class PrivateKeyNss;
class PublicKeyNss;
class SymKeyNss;

// Base key class for all NSS keys, used to safely cast between types. Each key
// maintains a copy of its serialized form in either 'raw', 'pkcs8', or 'spki'
// format. This is to allow structured cloning of keys synchronously from the
// target Blink thread without having to lock access to the key.
class KeyNss : public blink::WebCryptoKeyHandle {
 public:
  explicit KeyNss(const CryptoData& serialized_key_data);
  ~KeyNss() override;

  virtual SymKeyNss* AsSymKey();
  virtual PublicKeyNss* AsPublicKey();
  virtual PrivateKeyNss* AsPrivateKey();

  const std::vector<uint8_t>& serialized_key_data() const {
    return serialized_key_data_;
  }

 private:
  const std::vector<uint8_t> serialized_key_data_;
};

class SymKeyNss : public KeyNss {
 public:
  ~SymKeyNss() override;
  SymKeyNss(crypto::ScopedPK11SymKey key, const CryptoData& raw_key_data);

  static SymKeyNss* Cast(const blink::WebCryptoKey& key);

  PK11SymKey* key() { return key_.get(); }
  SymKeyNss* AsSymKey() override;

  const std::vector<uint8_t>& raw_key_data() const {
    return serialized_key_data();
  }

 private:
  crypto::ScopedPK11SymKey key_;

  DISALLOW_COPY_AND_ASSIGN(SymKeyNss);
};

class PublicKeyNss : public KeyNss {
 public:
  ~PublicKeyNss() override;
  PublicKeyNss(crypto::ScopedSECKEYPublicKey key, const CryptoData& spki_data);

  static PublicKeyNss* Cast(const blink::WebCryptoKey& key);

  SECKEYPublicKey* key() { return key_.get(); }
  PublicKeyNss* AsPublicKey() override;

  const std::vector<uint8_t>& spki_data() const {
    return serialized_key_data();
  }

 private:
  crypto::ScopedSECKEYPublicKey key_;

  DISALLOW_COPY_AND_ASSIGN(PublicKeyNss);
};

class PrivateKeyNss : public KeyNss {
 public:
  ~PrivateKeyNss() override;
  PrivateKeyNss(crypto::ScopedSECKEYPrivateKey key,
                const CryptoData& pkcs8_data);

  static PrivateKeyNss* Cast(const blink::WebCryptoKey& key);

  SECKEYPrivateKey* key() { return key_.get(); }
  PrivateKeyNss* AsPrivateKey() override;

  const std::vector<uint8_t>& pkcs8_data() const {
    return serialized_key_data();
  }

 private:
  crypto::ScopedSECKEYPrivateKey key_;

  DISALLOW_COPY_AND_ASSIGN(PrivateKeyNss);
};

}  // namespace webcrypto

}  // namespace content

#endif  // CONTENT_CHILD_WEBCRYPTO_NSS_KEY_NSS_H_