OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #ifndef CONTENT_CHILD_WEBCRYPTO_CRYPTO_ALGORITHM_H_ | |
6 #define CONTENT_CHILD_WEBCRYPTO_CRYPTO_ALGORITHM_H_ | |
7 | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "third_party/WebKit/public/platform/WebCrypto.h" | |
10 | |
11 namespace content { | |
12 | |
13 namespace webcrypto { | |
14 | |
15 class Status; | |
16 class CryptoData; | |
17 | |
18 // AlgorithmImplementation is a base class for *executing* the operations of an | |
19 // algorithm (generating keys, encrypting, signing, etc.). | |
20 // | |
21 // This is in contrast to blink::WebCryptoAlgorithm which instead *describes* | |
22 // the operation and its parameters. | |
23 // | |
24 // AlgorithmImplementation has reasonable default implementations for all | |
25 // methods which behave as if the operation is it is unsupported, so | |
26 // implementations need only override the applicable methods. | |
27 // | |
28 // Unless stated otherwise methods of AlgorithmImplementation are responsible | |
29 // for sanitizing their inputs. The following can be assumed: | |
30 // | |
31 // * |algorithm.id()| and |key.algorithm.id()| matches the algorithm under | |
32 // which the implementation was registerd. | |
33 // * |algorithm| has the correct parameters type for the operation. | |
34 // * The key usages have already been verified. In fact in the case of calls | |
35 // to Encrypt()/Decrypt() the corresponding key usages may not be present | |
36 // (when wrapping/unwrapping). | |
37 class AlgorithmImplementation { | |
Ryan Sleevi
2014/07/10 23:20:54
We generally encourage pure virtual interfaces whe
| |
38 public: | |
39 virtual ~AlgorithmImplementation(); | |
40 | |
41 virtual Status Encrypt(const blink::WebCryptoAlgorithm& algorithm, | |
42 const blink::WebCryptoKey& key, | |
43 const CryptoData& data, | |
44 std::vector<uint8>* buffer) const; | |
45 | |
46 virtual Status Decrypt(const blink::WebCryptoAlgorithm& algorithm, | |
47 const blink::WebCryptoKey& key, | |
48 const CryptoData& data, | |
49 std::vector<uint8>* buffer) const; | |
50 | |
51 virtual Status Sign(const blink::WebCryptoAlgorithm& algorithm, | |
52 const blink::WebCryptoKey& key, | |
53 const CryptoData& data, | |
54 std::vector<uint8>* buffer) const; | |
55 | |
56 virtual Status VerifySignature(const blink::WebCryptoAlgorithm& algorithm, | |
Ryan Sleevi
2014/07/10 23:20:54
Why is this VerifySignature, when the correspondin
eroman
2014/07/11 00:27:36
History: the Blink name for the method is verifySi
| |
57 const blink::WebCryptoKey& key, | |
58 const CryptoData& signature, | |
59 const CryptoData& data, | |
60 bool* signature_match) const; | |
61 | |
62 virtual Status Digest(const blink::WebCryptoAlgorithm& algorithm, | |
63 const CryptoData& data, | |
64 std::vector<uint8>* buffer) const; | |
65 | |
66 virtual scoped_ptr<blink::WebCryptoDigestor> CreateDigestor( | |
67 blink::WebCryptoAlgorithmId algorithm) const; | |
Ryan Sleevi
2014/07/10 23:20:54
This feels like it's at the wrong layer.
eroman
2014/07/11 00:27:36
OK I'll try to move this out. It was convenient ha
| |
68 | |
69 // ----------------------------------------------- | |
70 // Key generation | |
71 // ----------------------------------------------- | |
72 // When generating a key, VerifyKeyUsagesVeforeGenerateKey() will always be | |
73 // called before GenerateSecretKey(). Similarly when generating a keypair | |
74 // VerifyKeyUsagesBeforeGenerateKey() will always be called before | |
75 // GenerateKeyPair(). | |
76 | |
77 virtual Status VerifyKeyUsagesBeforeGenerateKey( | |
78 blink::WebCryptoKeyUsageMask usage_mask) const; | |
79 | |
80 virtual Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm, | |
81 bool extractable, | |
82 blink::WebCryptoKeyUsageMask usage_mask, | |
83 blink::WebCryptoKey* key) const; | |
84 | |
85 virtual Status VerifyKeyUsagesBeforeGenerateKeyPair( | |
86 blink::WebCryptoKeyUsageMask combined_usage_mask, | |
87 blink::WebCryptoKeyUsageMask* public_usage_mask, | |
88 blink::WebCryptoKeyUsageMask* private_usage_mask) const; | |
89 | |
90 virtual Status GenerateKeyPair( | |
91 const blink::WebCryptoAlgorithm& algorithm, | |
92 bool extractable, | |
93 blink::WebCryptoKeyUsageMask public_usage_mask, | |
94 blink::WebCryptoKeyUsageMask private_usage_mask, | |
95 blink::WebCryptoKey* public_key, | |
96 blink::WebCryptoKey* private_key) const; | |
97 | |
98 // ----------------------------------------------- | |
99 // Key import | |
100 // ----------------------------------------------- | |
101 // VerifyKeyUsagesBeforeImportKey() will always be called before either | |
102 // importing a key, or unwrapping a key. | |
103 // | |
104 // Note that when the format is JWK it may be unknown what the valid key | |
105 // usages are (since the key type will not be known yet). | |
106 | |
107 virtual Status VerifyKeyUsagesBeforeImportKey( | |
108 blink::WebCryptoKeyFormat format, | |
109 blink::WebCryptoKeyUsageMask usage_mask) const; | |
110 | |
111 virtual Status ImportKeyRaw(const CryptoData& key_data, | |
112 const blink::WebCryptoAlgorithm& algorithm, | |
113 bool extractable, | |
114 blink::WebCryptoKeyUsageMask usage_mask, | |
115 blink::WebCryptoKey* key) const; | |
116 | |
117 virtual Status ImportKeyPkcs8(const CryptoData& key_data, | |
118 const blink::WebCryptoAlgorithm& algorithm, | |
119 bool extractable, | |
120 blink::WebCryptoKeyUsageMask usage_mask, | |
121 blink::WebCryptoKey* key) const; | |
122 | |
123 virtual Status ImportKeySpki(const CryptoData& key_data, | |
124 const blink::WebCryptoAlgorithm& algorithm, | |
125 bool extractable, | |
126 blink::WebCryptoKeyUsageMask usage_mask, | |
127 blink::WebCryptoKey* key) const; | |
128 | |
129 virtual Status ImportKeyJwk(const CryptoData& key_data, | |
130 const blink::WebCryptoAlgorithm& algorithm, | |
131 bool extractable, | |
132 blink::WebCryptoKeyUsageMask usage_mask, | |
133 blink::WebCryptoKey* key) const; | |
Ryan Sleevi
2014/07/10 23:20:54
Not sure why this multitude of methods, rather tha
eroman
2014/07/11 00:27:36
This is about reducing code duplication.
Without
| |
134 | |
135 // ----------------------------------------------- | |
136 // Key export | |
137 // ----------------------------------------------- | |
138 | |
139 virtual Status ExportKeyRaw(const blink::WebCryptoKey& key, | |
140 std::vector<uint8>* buffer) const; | |
141 | |
142 virtual Status ExportKeyPkcs8(const blink::WebCryptoKey& key, | |
143 std::vector<uint8>* buffer) const; | |
144 | |
145 virtual Status ExportKeySpki(const blink::WebCryptoKey& key, | |
146 std::vector<uint8>* buffer) const; | |
147 | |
148 virtual Status ExportKeyJwk(const blink::WebCryptoKey& key, | |
149 std::vector<uint8>* buffer) const; | |
150 }; | |
151 | |
152 } // namespace webcrypto | |
153 | |
154 } // namespace content | |
155 | |
156 #endif // CONTENT_CHILD_WEBCRYPTO_CRYPTO_ALGORITHM_H_ | |
OLD | NEW |