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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
// 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_ALGORITHM_IMPLEMENTATION_H_
#define CONTENT_CHILD_WEBCRYPTO_ALGORITHM_IMPLEMENTATION_H_
#include <stdint.h>
#include <vector>
#include "base/memory/scoped_ptr.h"
#include "third_party/WebKit/public/platform/WebCrypto.h"
namespace content {
namespace webcrypto {
class CryptoData;
class GenerateKeyResult;
class Status;
// AlgorithmImplementation is a base class for *executing* the operations of an
// algorithm (generating keys, encrypting, signing, etc.).
//
// This is in contrast to blink::WebCryptoAlgorithm which instead *describes*
// the operation and its parameters.
//
// AlgorithmImplementation has reasonable default implementations for all
// methods which behave as if the operation is it is unsupported, so
// implementations need only override the applicable methods.
//
// Unless stated otherwise methods of AlgorithmImplementation are responsible
// for sanitizing their inputs. The following can be assumed:
//
// * |algorithm.id()| and |key.algorithm.id()| matches the algorithm under
// which the implementation was registerd.
// * |algorithm| has the correct parameters type for the operation.
// * The key usages have already been verified. In fact in the case of calls
// to Encrypt()/Decrypt() the corresponding key usages may not be present
// (when wrapping/unwrapping).
class AlgorithmImplementation {
public:
virtual ~AlgorithmImplementation();
// This method corresponds to Web Crypto's crypto.subtle.encrypt().
virtual Status Encrypt(const blink::WebCryptoAlgorithm& algorithm,
const blink::WebCryptoKey& key,
const CryptoData& data,
std::vector<uint8_t>* buffer) const;
// This method corresponds to Web Crypto's crypto.subtle.decrypt().
virtual Status Decrypt(const blink::WebCryptoAlgorithm& algorithm,
const blink::WebCryptoKey& key,
const CryptoData& data,
std::vector<uint8_t>* buffer) const;
// This method corresponds to Web Crypto's crypto.subtle.sign().
virtual Status Sign(const blink::WebCryptoAlgorithm& algorithm,
const blink::WebCryptoKey& key,
const CryptoData& data,
std::vector<uint8_t>* buffer) const;
// This method corresponds to Web Crypto's crypto.subtle.verify().
virtual Status Verify(const blink::WebCryptoAlgorithm& algorithm,
const blink::WebCryptoKey& key,
const CryptoData& signature,
const CryptoData& data,
bool* signature_match) const;
// This method corresponds to Web Crypto's crypto.subtle.digest().
virtual Status Digest(const blink::WebCryptoAlgorithm& algorithm,
const CryptoData& data,
std::vector<uint8_t>* buffer) const;
// This method corresponds to Web Crypto's crypto.subtle.generateKey().
//
// Implementations MUST verify |usages| and return an error if it is not
// appropriate.
virtual Status GenerateKey(const blink::WebCryptoAlgorithm& algorithm,
bool extractable,
blink::WebCryptoKeyUsageMask usages,
GenerateKeyResult* result) const;
// -----------------------------------------------
// Key import
// -----------------------------------------------
// VerifyKeyUsagesBeforeImportKey() must be called before either
// importing a key, or unwrapping a key.
//
// Implementations should return an error if the requested usages are invalid
// when importing for the specified format.
//
// For instance, importing an RSA-SSA key with 'spki' format and Sign usage
// is invalid. The 'spki' format implies it will be a public key, and public
// keys do not support signing.
//
// When called with format=JWK the key type may be unknown. The
// ImportKeyJwk() must do the final usage check.
virtual Status VerifyKeyUsagesBeforeImportKey(
blink::WebCryptoKeyFormat format,
blink::WebCryptoKeyUsageMask usages) const;
// This method corresponds to Web Crypto's
// crypto.subtle.importKey(format='raw').
virtual Status ImportKeyRaw(const CryptoData& key_data,
const blink::WebCryptoAlgorithm& algorithm,
bool extractable,
blink::WebCryptoKeyUsageMask usages,
blink::WebCryptoKey* key) const;
// This method corresponds to Web Crypto's
// crypto.subtle.importKey(format='pkcs8').
virtual Status ImportKeyPkcs8(const CryptoData& key_data,
const blink::WebCryptoAlgorithm& algorithm,
bool extractable,
blink::WebCryptoKeyUsageMask usages,
blink::WebCryptoKey* key) const;
// This method corresponds to Web Crypto's
// crypto.subtle.importKey(format='spki').
virtual Status ImportKeySpki(const CryptoData& key_data,
const blink::WebCryptoAlgorithm& algorithm,
bool extractable,
blink::WebCryptoKeyUsageMask usages,
blink::WebCryptoKey* key) const;
// This method corresponds to Web Crypto's
// crypto.subtle.importKey(format='jwk').
virtual Status ImportKeyJwk(const CryptoData& key_data,
const blink::WebCryptoAlgorithm& algorithm,
bool extractable,
blink::WebCryptoKeyUsageMask usages,
blink::WebCryptoKey* key) const;
// -----------------------------------------------
// Key export
// -----------------------------------------------
virtual Status ExportKeyRaw(const blink::WebCryptoKey& key,
std::vector<uint8_t>* buffer) const;
virtual Status ExportKeyPkcs8(const blink::WebCryptoKey& key,
std::vector<uint8_t>* buffer) const;
virtual Status ExportKeySpki(const blink::WebCryptoKey& key,
std::vector<uint8_t>* buffer) const;
virtual Status ExportKeyJwk(const blink::WebCryptoKey& key,
std::vector<uint8_t>* buffer) const;
// -----------------------------------------------
// Structured clone
// -----------------------------------------------
// The Structured clone methods are used for synchronous serialization /
// deserialization of a WebCryptoKey.
//
// This serialized format is used by Blink to:
// * Copy WebCryptoKeys between threads (postMessage to WebWorkers)
// * Copy WebCryptoKeys between domains (postMessage)
// * Copy WebCryptoKeys within the same domain (postMessage)
// * Persist the key to storage (IndexedDB)
//
// Implementations of structured cloning must:
// * Be threadsafe (structured cloning is called directly on the Blink
// thread, in contrast to the other methods of AlgorithmImplementation).
// * Use a stable format (a serialized key must forever be de-serializable,
// and be able to survive future migrations to crypto libraries)
// * Work for all keys (including ones marked as non-extractable).
//
// Tests to verify structured cloning are available in:
// LayoutTests/crypto/clone-*.html
virtual Status SerializeKeyForClone(
const blink::WebCryptoKey& key,
blink::WebVector<uint8_t>* key_data) const;
virtual Status DeserializeKeyForClone(
const blink::WebCryptoKeyAlgorithm& algorithm,
blink::WebCryptoKeyType type,
bool extractable,
blink::WebCryptoKeyUsageMask usages,
const CryptoData& key_data,
blink::WebCryptoKey* key) const;
};
} // namespace webcrypto
} // namespace content
#endif // CONTENT_CHILD_WEBCRYPTO_ALGORITHM_IMPLEMENTATION_H_
|