OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 14 matching lines...) Expand all Loading... |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "modules/crypto/SubtleCrypto.h" | 32 #include "modules/crypto/SubtleCrypto.h" |
33 | 33 |
34 #include "bindings/v8/Dictionary.h" | 34 #include "bindings/v8/Dictionary.h" |
35 #include "bindings/v8/ExceptionState.h" | |
36 #include "modules/crypto/CryptoResultImpl.h" | 35 #include "modules/crypto/CryptoResultImpl.h" |
37 #include "modules/crypto/Key.h" | 36 #include "modules/crypto/Key.h" |
38 #include "modules/crypto/NormalizeAlgorithm.h" | 37 #include "modules/crypto/NormalizeAlgorithm.h" |
39 #include "public/platform/Platform.h" | 38 #include "public/platform/Platform.h" |
40 #include "public/platform/WebCrypto.h" | 39 #include "public/platform/WebCrypto.h" |
41 #include "public/platform/WebCryptoAlgorithm.h" | 40 #include "public/platform/WebCryptoAlgorithm.h" |
42 #include "wtf/ArrayBufferView.h" | 41 #include "wtf/ArrayBufferView.h" |
43 | 42 |
44 namespace WebCore { | 43 namespace WebCore { |
45 | 44 |
46 namespace { | 45 namespace { |
47 | 46 |
48 bool parseAlgorithm(const Dictionary& rawAlgorithm, AlgorithmOperation operation
Type, blink::WebCryptoAlgorithm &algorithm, ExceptionState& exceptionState, Cryp
toResult* result) | 47 // Seems like the generated bindings should take care of these however it |
| 48 // currently doesn't. See also http://crbug.com/264520 |
| 49 template <typename T> |
| 50 bool ensureNotNull(T* x, const char* paramName, CryptoResult* result) |
49 { | 51 { |
50 if (!rawAlgorithm.isObject()) { | 52 if (!x) { |
51 exceptionState.throwTypeError("Algorithm: Not an object"); | 53 String message = String("Invalid ") + paramName + String(" argument"); |
| 54 result->completeWithError(blink::WebString(message)); |
52 return false; | 55 return false; |
53 } | 56 } |
54 return parseAlgorithm(rawAlgorithm, operationType, algorithm, result); | 57 return true; |
55 } | 58 } |
56 | 59 |
57 ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, Alg
orithmOperation operationType, ArrayBufferView* signature, ArrayBufferView* data
Buffer, ExceptionState& exceptionState) | 60 ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, Alg
orithmOperation operationType, ArrayBufferView* signature, ArrayBufferView* data
Buffer) |
58 { | 61 { |
59 bool requiresKey = operationType != Digest; | |
60 | |
61 // Seems like the generated bindings should take care of these however it | |
62 // currently doesn't. See also http://crbugh.com/264520 | |
63 if (requiresKey && !key) { | |
64 exceptionState.throwTypeError("Invalid key argument"); | |
65 return ScriptPromise(); | |
66 } | |
67 if (operationType == Verify && !signature) { | |
68 exceptionState.throwTypeError("Invalid signature argument"); | |
69 return ScriptPromise(); | |
70 } | |
71 if (!dataBuffer) { | |
72 exceptionState.throwTypeError("Invalid dataBuffer argument"); | |
73 return ScriptPromise(); | |
74 } | |
75 | |
76 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); | 62 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); |
77 ScriptPromise promise = result->promise(); | 63 ScriptPromise promise = result->promise(); |
78 | 64 |
| 65 bool requiresKey = operationType != Digest; |
| 66 |
| 67 if (requiresKey && !ensureNotNull(key, "key", result.get())) |
| 68 return promise; |
| 69 if (operationType == Verify && !ensureNotNull(signature, "signature", result
.get())) |
| 70 return promise; |
| 71 if (!ensureNotNull(dataBuffer, "dataBuffer", result.get())) |
| 72 return promise; |
| 73 |
79 blink::WebCryptoAlgorithm algorithm; | 74 blink::WebCryptoAlgorithm algorithm; |
80 if (!parseAlgorithm(rawAlgorithm, operationType, algorithm, exceptionState,
result.get())) | 75 if (!parseAlgorithm(rawAlgorithm, operationType, algorithm, result.get())) |
81 return promise; | 76 return promise; |
82 | 77 |
83 if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, res
ult.get())) | 78 if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, res
ult.get())) |
84 return promise; | 79 return promise; |
85 | 80 |
86 const unsigned char* data = static_cast<const unsigned char*>(dataBuffer->ba
seAddress()); | 81 const unsigned char* data = static_cast<const unsigned char*>(dataBuffer->ba
seAddress()); |
87 unsigned dataSize = dataBuffer->byteLength(); | 82 unsigned dataSize = dataBuffer->byteLength(); |
88 | 83 |
89 switch (operationType) { | 84 switch (operationType) { |
90 case Encrypt: | 85 case Encrypt: |
(...skipping 19 matching lines...) Expand all Loading... |
110 return promise; | 105 return promise; |
111 } | 106 } |
112 | 107 |
113 } // namespace | 108 } // namespace |
114 | 109 |
115 SubtleCrypto::SubtleCrypto() | 110 SubtleCrypto::SubtleCrypto() |
116 { | 111 { |
117 ScriptWrappable::init(this); | 112 ScriptWrappable::init(this); |
118 } | 113 } |
119 | 114 |
120 ScriptPromise SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, Key* key, Ar
rayBufferView* data, ExceptionState& exceptionState) | 115 ScriptPromise SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, Key* key, Ar
rayBufferView* data) |
121 { | 116 { |
122 return startCryptoOperation(rawAlgorithm, key, Encrypt, 0, data, exceptionSt
ate); | 117 return startCryptoOperation(rawAlgorithm, key, Encrypt, 0, data); |
123 } | 118 } |
124 | 119 |
125 ScriptPromise SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, Key* key, Ar
rayBufferView* data, ExceptionState& exceptionState) | 120 ScriptPromise SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, Key* key, Ar
rayBufferView* data) |
126 { | 121 { |
127 return startCryptoOperation(rawAlgorithm, key, Decrypt, 0, data, exceptionSt
ate); | 122 return startCryptoOperation(rawAlgorithm, key, Decrypt, 0, data); |
128 } | 123 } |
129 | 124 |
130 ScriptPromise SubtleCrypto::sign(const Dictionary& rawAlgorithm, Key* key, Array
BufferView* data, ExceptionState& exceptionState) | 125 ScriptPromise SubtleCrypto::sign(const Dictionary& rawAlgorithm, Key* key, Array
BufferView* data) |
131 { | 126 { |
132 return startCryptoOperation(rawAlgorithm, key, Sign, 0, data, exceptionState
); | 127 return startCryptoOperation(rawAlgorithm, key, Sign, 0, data); |
133 } | 128 } |
134 | 129 |
135 ScriptPromise SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, Key*
key, ArrayBufferView* signature, ArrayBufferView* data, ExceptionState& excepti
onState) | 130 ScriptPromise SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, Key*
key, ArrayBufferView* signature, ArrayBufferView* data) |
136 { | 131 { |
137 return startCryptoOperation(rawAlgorithm, key, Verify, signature, data, exce
ptionState); | 132 return startCryptoOperation(rawAlgorithm, key, Verify, signature, data); |
138 } | 133 } |
139 | 134 |
140 ScriptPromise SubtleCrypto::digest(const Dictionary& rawAlgorithm, ArrayBufferVi
ew* data, ExceptionState& exceptionState) | 135 ScriptPromise SubtleCrypto::digest(const Dictionary& rawAlgorithm, ArrayBufferVi
ew* data) |
141 { | 136 { |
142 return startCryptoOperation(rawAlgorithm, 0, Digest, 0, data, exceptionState
); | 137 return startCryptoOperation(rawAlgorithm, 0, Digest, 0, data); |
143 } | 138 } |
144 | 139 |
145 ScriptPromise SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool ext
ractable, const Vector<String>& rawKeyUsages, ExceptionState& exceptionState) | 140 ScriptPromise SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool ext
ractable, const Vector<String>& rawKeyUsages) |
146 { | 141 { |
147 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); | 142 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); |
148 ScriptPromise promise = result->promise(); | 143 ScriptPromise promise = result->promise(); |
149 | 144 |
150 blink::WebCryptoKeyUsageMask keyUsages; | 145 blink::WebCryptoKeyUsageMask keyUsages; |
151 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) | 146 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) |
152 return promise; | 147 return promise; |
153 | 148 |
154 blink::WebCryptoAlgorithm algorithm; | 149 blink::WebCryptoAlgorithm algorithm; |
155 if (!parseAlgorithm(rawAlgorithm, GenerateKey, algorithm, exceptionState, re
sult.get())) | 150 if (!parseAlgorithm(rawAlgorithm, GenerateKey, algorithm, result.get())) |
156 return promise; | 151 return promise; |
157 | 152 |
158 blink::Platform::current()->crypto()->generateKey(algorithm, extractable, ke
yUsages, result->result()); | 153 blink::Platform::current()->crypto()->generateKey(algorithm, extractable, ke
yUsages, result->result()); |
159 return promise; | 154 return promise; |
160 } | 155 } |
161 | 156 |
162 ScriptPromise SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView*
keyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>&
rawKeyUsages, ExceptionState& exceptionState) | 157 ScriptPromise SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView*
keyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>&
rawKeyUsages) |
163 { | 158 { |
164 if (!keyData) { | |
165 exceptionState.throwTypeError("Invalid keyData argument"); | |
166 return ScriptPromise(); | |
167 } | |
168 | |
169 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); | 159 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); |
170 ScriptPromise promise = result->promise(); | 160 ScriptPromise promise = result->promise(); |
171 | 161 |
| 162 if (!ensureNotNull(keyData, "keyData", result.get())) |
| 163 return promise; |
| 164 |
172 blink::WebCryptoKeyFormat format; | 165 blink::WebCryptoKeyFormat format; |
173 if (!Key::parseFormat(rawFormat, format, result.get())) | 166 if (!Key::parseFormat(rawFormat, format, result.get())) |
174 return promise; | 167 return promise; |
175 | 168 |
176 blink::WebCryptoKeyUsageMask keyUsages; | 169 blink::WebCryptoKeyUsageMask keyUsages; |
177 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) | 170 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) |
178 return promise; | 171 return promise; |
179 | 172 |
180 blink::WebCryptoAlgorithm algorithm; | 173 blink::WebCryptoAlgorithm algorithm; |
181 if (!parseAlgorithm(rawAlgorithm, ImportKey, algorithm, exceptionState, resu
lt.get())) | 174 if (!parseAlgorithm(rawAlgorithm, ImportKey, algorithm, result.get())) |
182 return promise; | 175 return promise; |
183 | 176 |
184 const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->bas
eAddress()); | 177 const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->bas
eAddress()); |
185 | 178 |
186 blink::Platform::current()->crypto()->importKey(format, keyDataBytes, keyDat
a->byteLength(), algorithm, extractable, keyUsages, result->result()); | 179 blink::Platform::current()->crypto()->importKey(format, keyDataBytes, keyDat
a->byteLength(), algorithm, extractable, keyUsages, result->result()); |
187 return promise; | 180 return promise; |
188 } | 181 } |
189 | 182 |
190 ScriptPromise SubtleCrypto::exportKey(const String& rawFormat, Key* key, Excepti
onState& exceptionState) | 183 ScriptPromise SubtleCrypto::exportKey(const String& rawFormat, Key* key) |
191 { | 184 { |
192 if (!key) { | |
193 exceptionState.throwTypeError("Invalid key argument"); | |
194 return ScriptPromise(); | |
195 } | |
196 | |
197 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); | 185 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); |
198 ScriptPromise promise = result->promise(); | 186 ScriptPromise promise = result->promise(); |
199 | 187 |
| 188 if (!ensureNotNull(key, "key", result.get())) |
| 189 return promise; |
| 190 |
200 blink::WebCryptoKeyFormat format; | 191 blink::WebCryptoKeyFormat format; |
201 if (!Key::parseFormat(rawFormat, format, result.get())) | 192 if (!Key::parseFormat(rawFormat, format, result.get())) |
202 return promise; | 193 return promise; |
203 | 194 |
204 if (!key->extractable()) { | 195 if (!key->extractable()) { |
205 result->completeWithError("key is not extractable"); | 196 result->completeWithError("key is not extractable"); |
206 return promise; | 197 return promise; |
207 } | 198 } |
208 | 199 |
209 blink::Platform::current()->crypto()->exportKey(format, key->key(), result->
result()); | 200 blink::Platform::current()->crypto()->exportKey(format, key->key(), result->
result()); |
210 return promise; | 201 return promise; |
211 } | 202 } |
212 | 203 |
213 ScriptPromise SubtleCrypto::wrapKey(const String& rawFormat, Key* key, Key* wrap
pingKey, const Dictionary& rawWrapAlgorithm, ExceptionState& exceptionState) | 204 ScriptPromise SubtleCrypto::wrapKey(const String& rawFormat, Key* key, Key* wrap
pingKey, const Dictionary& rawWrapAlgorithm) |
214 { | 205 { |
215 if (!key) { | |
216 exceptionState.throwTypeError("Invalid key argument"); | |
217 return ScriptPromise(); | |
218 } | |
219 | |
220 if (!wrappingKey) { | |
221 exceptionState.throwTypeError("Invalid wrappingKey argument"); | |
222 return ScriptPromise(); | |
223 } | |
224 | |
225 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); | 206 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); |
226 ScriptPromise promise = result->promise(); | 207 ScriptPromise promise = result->promise(); |
227 | 208 |
| 209 if (!ensureNotNull(key, "key", result.get())) |
| 210 return promise; |
| 211 |
| 212 if (!ensureNotNull(wrappingKey, "wrappingKey", result.get())) |
| 213 return promise; |
| 214 |
228 blink::WebCryptoKeyFormat format; | 215 blink::WebCryptoKeyFormat format; |
229 if (!Key::parseFormat(rawFormat, format, result.get())) | 216 if (!Key::parseFormat(rawFormat, format, result.get())) |
230 return promise; | 217 return promise; |
231 | 218 |
232 blink::WebCryptoAlgorithm wrapAlgorithm; | 219 blink::WebCryptoAlgorithm wrapAlgorithm; |
233 if (!parseAlgorithm(rawWrapAlgorithm, WrapKey, wrapAlgorithm, exceptionState
, result.get())) | 220 if (!parseAlgorithm(rawWrapAlgorithm, WrapKey, wrapAlgorithm, result.get())) |
234 return promise; | 221 return promise; |
235 | 222 |
236 if (!key->extractable()) { | 223 if (!key->extractable()) { |
237 result->completeWithError("key is not extractable"); | 224 result->completeWithError("key is not extractable"); |
238 return promise; | 225 return promise; |
239 } | 226 } |
240 | 227 |
241 if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, WrapKey, result.get()
)) | 228 if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, WrapKey, result.get()
)) |
242 return promise; | 229 return promise; |
243 | 230 |
244 blink::Platform::current()->crypto()->wrapKey(format, key->key(), wrappingKe
y->key(), wrapAlgorithm, result->result()); | 231 blink::Platform::current()->crypto()->wrapKey(format, key->key(), wrappingKe
y->key(), wrapAlgorithm, result->result()); |
245 return promise; | 232 return promise; |
246 } | 233 } |
247 | 234 |
248 ScriptPromise SubtleCrypto::unwrapKey(const String& rawFormat, ArrayBufferView*
wrappedKey, Key* unwrappingKey, const Dictionary& rawUnwrapAlgorithm, const Dict
ionary& rawUnwrappedKeyAlgorithm, bool extractable, const Vector<String>& rawKey
Usages, ExceptionState& exceptionState) | 235 ScriptPromise SubtleCrypto::unwrapKey(const String& rawFormat, ArrayBufferView*
wrappedKey, Key* unwrappingKey, const Dictionary& rawUnwrapAlgorithm, const Dict
ionary& rawUnwrappedKeyAlgorithm, bool extractable, const Vector<String>& rawKey
Usages) |
249 { | 236 { |
250 if (!wrappedKey) { | |
251 exceptionState.throwTypeError("Invalid wrappedKey argument"); | |
252 return ScriptPromise(); | |
253 } | |
254 | |
255 if (!unwrappingKey) { | |
256 exceptionState.throwTypeError("Invalid unwrappingKey argument"); | |
257 return ScriptPromise(); | |
258 } | |
259 | |
260 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); | 237 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(); |
261 ScriptPromise promise = result->promise(); | 238 ScriptPromise promise = result->promise(); |
262 | 239 |
| 240 if (!ensureNotNull(wrappedKey, "wrappedKey", result.get())) |
| 241 return promise; |
| 242 if (!ensureNotNull(unwrappingKey, "unwrappingKey", result.get())) |
| 243 return promise; |
| 244 |
263 blink::WebCryptoKeyFormat format; | 245 blink::WebCryptoKeyFormat format; |
264 if (!Key::parseFormat(rawFormat, format, result.get())) | 246 if (!Key::parseFormat(rawFormat, format, result.get())) |
265 return promise; | 247 return promise; |
266 | 248 |
267 blink::WebCryptoKeyUsageMask keyUsages; | 249 blink::WebCryptoKeyUsageMask keyUsages; |
268 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) | 250 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) |
269 return promise; | 251 return promise; |
270 | 252 |
271 blink::WebCryptoAlgorithm unwrapAlgorithm; | 253 blink::WebCryptoAlgorithm unwrapAlgorithm; |
272 if (!parseAlgorithm(rawUnwrapAlgorithm, UnwrapKey, unwrapAlgorithm, exceptio
nState, result.get())) | 254 if (!parseAlgorithm(rawUnwrapAlgorithm, UnwrapKey, unwrapAlgorithm, result.g
et())) |
273 return promise; | 255 return promise; |
274 | 256 |
275 blink::WebCryptoAlgorithm unwrappedKeyAlgorithm; | 257 blink::WebCryptoAlgorithm unwrappedKeyAlgorithm; |
276 if (!parseAlgorithm(rawUnwrappedKeyAlgorithm, ImportKey, unwrappedKeyAlgorit
hm, exceptionState, result.get())) | 258 if (!parseAlgorithm(rawUnwrappedKeyAlgorithm, ImportKey, unwrappedKeyAlgorit
hm, result.get())) |
277 return promise; | 259 return promise; |
278 | 260 |
279 if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, UnwrapKey, result
.get())) | 261 if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, UnwrapKey, result
.get())) |
280 return promise; | 262 return promise; |
281 | 263 |
282 const unsigned char* wrappedKeyData = static_cast<const unsigned char*>(wrap
pedKey->baseAddress()); | 264 const unsigned char* wrappedKeyData = static_cast<const unsigned char*>(wrap
pedKey->baseAddress()); |
283 unsigned wrappedKeyDataSize = wrappedKey->byteLength(); | 265 unsigned wrappedKeyDataSize = wrappedKey->byteLength(); |
284 | 266 |
285 blink::Platform::current()->crypto()->unwrapKey(format, wrappedKeyData, wrap
pedKeyDataSize, unwrappingKey->key(), unwrapAlgorithm, unwrappedKeyAlgorithm, ex
tractable, keyUsages, result->result()); | 267 blink::Platform::current()->crypto()->unwrapKey(format, wrappedKeyData, wrap
pedKeyDataSize, unwrappingKey->key(), unwrapAlgorithm, unwrappedKeyAlgorithm, ex
tractable, keyUsages, result->result()); |
286 return promise; | 268 return promise; |
287 } | 269 } |
288 | 270 |
289 } // namespace WebCore | 271 } // namespace WebCore |
OLD | NEW |