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/ScriptPromiseResolverWithContext.h" |
| 36 #include "core/dom/DOMException.h" |
35 #include "core/dom/ExecutionContext.h" | 37 #include "core/dom/ExecutionContext.h" |
| 38 #include "modules/crypto/CrossThreadCryptoResult.h" |
36 #include "modules/crypto/CryptoResultImpl.h" | 39 #include "modules/crypto/CryptoResultImpl.h" |
37 #include "modules/crypto/Key.h" | 40 #include "modules/crypto/Key.h" |
| 41 #include "modules/crypto/KeyPair.h" |
38 #include "modules/crypto/NormalizeAlgorithm.h" | 42 #include "modules/crypto/NormalizeAlgorithm.h" |
39 #include "public/platform/Platform.h" | 43 #include "public/platform/Platform.h" |
| 44 #include "public/platform/WebArrayBuffer.h" |
40 #include "public/platform/WebCrypto.h" | 45 #include "public/platform/WebCrypto.h" |
41 #include "public/platform/WebCryptoAlgorithm.h" | 46 #include "public/platform/WebCryptoAlgorithm.h" |
42 #include "wtf/ArrayBufferView.h" | 47 #include "wtf/ArrayBufferView.h" |
43 | 48 |
44 namespace WebCore { | 49 namespace WebCore { |
45 | 50 |
| 51 class SubtleCrypto::PendingResult : public CryptoResult { |
| 52 public: |
| 53 virtual ~PendingResult() { } |
| 54 |
| 55 static PassRefPtr<PendingResult> create(ScriptState* scriptState, RawPtr<Sub
tleCrypto> owner) |
| 56 { |
| 57 return adoptRef(new PendingResult(scriptState, owner)); |
| 58 } |
| 59 |
| 60 virtual void completeWithError(blink::WebCryptoErrorType errorType, const bl
ink::WebString& errorDetails) OVERRIDE |
| 61 { |
| 62 m_resolver->reject(DOMException::create(webCryptoErrorToExceptionCode(er
rorType), errorDetails)); |
| 63 unregisterThis(); |
| 64 } |
| 65 |
| 66 virtual void completeWithBuffer(const blink::WebArrayBuffer& buffer) OVERRID
E |
| 67 { |
| 68 m_resolver->resolve(PassRefPtr<ArrayBuffer>(buffer)); |
| 69 unregisterThis(); |
| 70 } |
| 71 |
| 72 virtual void completeWithBoolean(bool b) OVERRIDE |
| 73 { |
| 74 m_resolver->resolve(b); |
| 75 unregisterThis(); |
| 76 } |
| 77 |
| 78 virtual void completeWithKey(const blink::WebCryptoKey& key) OVERRIDE |
| 79 { |
| 80 m_resolver->resolve(Key::create(key)); |
| 81 unregisterThis(); |
| 82 } |
| 83 |
| 84 virtual void completeWithKeyPair(const blink::WebCryptoKey& publicKey, const
blink::WebCryptoKey& privateKey) OVERRIDE |
| 85 { |
| 86 m_resolver->resolve(KeyPair::create(publicKey, privateKey)); |
| 87 unregisterThis(); |
| 88 } |
| 89 |
| 90 ScriptPromise promise() { return m_resolver->promise(); } |
| 91 |
| 92 WeakPtr<PendingResult> createWeakPtr() { return m_factory.createWeakPtr(); } |
| 93 |
| 94 private: |
| 95 PendingResult(ScriptState* scriptState, RawPtr<SubtleCrypto> owner) |
| 96 : m_resolver(ScriptPromiseResolverWithContext::create(scriptState)) |
| 97 , m_owner(owner) |
| 98 , m_factory(this) |
| 99 { |
| 100 m_resolver->keepObjectWhilePending(owner); |
| 101 m_owner->m_pendingResults.add(this); |
| 102 } |
| 103 |
| 104 void unregisterThis() |
| 105 { |
| 106 if (m_owner->m_pendingResults.contains(this)) |
| 107 m_owner->m_pendingResults.remove(this); |
| 108 } |
| 109 |
| 110 RefPtr<ScriptPromiseResolverWithContext> m_resolver; |
| 111 Persistent<SubtleCrypto> m_owner; |
| 112 WeakPtrFactory<PendingResult> m_factory; |
| 113 }; |
| 114 |
46 // Seems like the generated bindings should take care of these however it | 115 // Seems like the generated bindings should take care of these however it |
47 // currently doesn't. See also http://crbug.com/264520 | 116 // currently doesn't. See also http://crbug.com/264520 |
48 static bool ensureNotNull(const ArrayPiece& x, const char* paramName, CryptoResu
lt* result) | 117 static bool ensureNotNull(const ArrayPiece& x, const char* paramName, CryptoResu
lt* result) |
49 { | 118 { |
50 if (x.isNull()) { | 119 if (x.isNull()) { |
51 String message = String("Invalid ") + paramName + String(" argument"); | 120 String message = String("Invalid ") + paramName + String(" argument"); |
52 result->completeWithError(blink::WebCryptoErrorTypeType, blink::WebStrin
g(message)); | 121 result->completeWithError(blink::WebCryptoErrorTypeType, blink::WebStrin
g(message)); |
53 return false; | 122 return false; |
54 } | 123 } |
55 return true; | 124 return true; |
(...skipping 22 matching lines...) Expand all Loading... |
78 { | 147 { |
79 const SecurityOrigin* origin = scriptState->executionContext()->securityOrig
in(); | 148 const SecurityOrigin* origin = scriptState->executionContext()->securityOrig
in(); |
80 if (!origin->canAccessFeatureRequiringSecureOrigin()) { | 149 if (!origin->canAccessFeatureRequiringSecureOrigin()) { |
81 result->completeWithError(blink::WebCryptoErrorTypeNotSupported, "WebCry
pto is only supported over secure origins. See http://crbug.com/373032"); | 150 result->completeWithError(blink::WebCryptoErrorTypeNotSupported, "WebCry
pto is only supported over secure origins. See http://crbug.com/373032"); |
82 return false; | 151 return false; |
83 } | 152 } |
84 | 153 |
85 return true; | 154 return true; |
86 } | 155 } |
87 | 156 |
88 static ScriptPromise startCryptoOperation(ScriptState* scriptState, const Dictio
nary& rawAlgorithm, Key* key, blink::WebCryptoOperation operationType, const Arr
ayPiece& signature, const ArrayPiece& dataBuffer) | 157 ScriptPromise SubtleCrypto::startCryptoOperation(ScriptState* scriptState, const
Dictionary& rawAlgorithm, Key* key, blink::WebCryptoOperation operationType, co
nst ArrayPiece& signature, const ArrayPiece& dataBuffer) |
89 { | 158 { |
90 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); | 159 RefPtr<PendingResult> pending = PendingResult::create(scriptState, this); |
91 ScriptPromise promise = result->promise(); | 160 RefPtr<CrossThreadCryptoResult<PendingResult> > result = CrossThreadCryptoRe
sult<PendingResult>::create(pending->createWeakPtr()); |
| 161 ScriptPromise promise = pending->promise(); |
92 | 162 |
93 if (!canAccessWebCrypto(scriptState, result.get())) | 163 if (!canAccessWebCrypto(scriptState, result.get())) |
94 return promise; | 164 return promise; |
95 | 165 |
96 bool requiresKey = operationType != blink::WebCryptoOperationDigest; | 166 bool requiresKey = operationType != blink::WebCryptoOperationDigest; |
97 | 167 |
98 if (requiresKey && !ensureNotNull(key, "key", result.get())) | 168 if (requiresKey && !ensureNotNull(key, "key", result.get())) |
99 return promise; | 169 return promise; |
100 if (operationType == blink::WebCryptoOperationVerify && !ensureNotNull(signa
ture, "signature", result.get())) | 170 if (operationType == blink::WebCryptoOperationVerify && !ensureNotNull(signa
ture, "signature", result.get())) |
101 return promise; | 171 return promise; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 return startCryptoOperation(scriptState, rawAlgorithm, key, blink::WebCrypto
OperationVerify, signature, data); | 231 return startCryptoOperation(scriptState, rawAlgorithm, key, blink::WebCrypto
OperationVerify, signature, data); |
162 } | 232 } |
163 | 233 |
164 ScriptPromise SubtleCrypto::digest(ScriptState* scriptState, const Dictionary& r
awAlgorithm, const ArrayPiece& data) | 234 ScriptPromise SubtleCrypto::digest(ScriptState* scriptState, const Dictionary& r
awAlgorithm, const ArrayPiece& data) |
165 { | 235 { |
166 return startCryptoOperation(scriptState, rawAlgorithm, 0, blink::WebCryptoOp
erationDigest, ArrayPiece(), data); | 236 return startCryptoOperation(scriptState, rawAlgorithm, 0, blink::WebCryptoOp
erationDigest, ArrayPiece(), data); |
167 } | 237 } |
168 | 238 |
169 ScriptPromise SubtleCrypto::generateKey(ScriptState* scriptState, const Dictiona
ry& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages) | 239 ScriptPromise SubtleCrypto::generateKey(ScriptState* scriptState, const Dictiona
ry& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages) |
170 { | 240 { |
171 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); | 241 RefPtr<PendingResult> pending = PendingResult::create(scriptState, this); |
172 ScriptPromise promise = result->promise(); | 242 RefPtr<CrossThreadCryptoResult<PendingResult> > result = CrossThreadCryptoRe
sult<PendingResult>::create(pending->createWeakPtr()); |
| 243 ScriptPromise promise = pending->promise(); |
173 | 244 |
174 if (!canAccessWebCrypto(scriptState, result.get())) | 245 if (!canAccessWebCrypto(scriptState, result.get())) |
175 return promise; | 246 return promise; |
176 | 247 |
177 blink::WebCryptoKeyUsageMask keyUsages; | 248 blink::WebCryptoKeyUsageMask keyUsages; |
178 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) | 249 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) |
179 return promise; | 250 return promise; |
180 | 251 |
181 blink::WebCryptoAlgorithm algorithm; | 252 blink::WebCryptoAlgorithm algorithm; |
182 if (!parseAlgorithm(rawAlgorithm, blink::WebCryptoOperationGenerateKey, algo
rithm, result.get())) | 253 if (!parseAlgorithm(rawAlgorithm, blink::WebCryptoOperationGenerateKey, algo
rithm, result.get())) |
183 return promise; | 254 return promise; |
184 | 255 |
185 blink::Platform::current()->crypto()->generateKey(algorithm, extractable, ke
yUsages, result->result()); | 256 blink::Platform::current()->crypto()->generateKey(algorithm, extractable, ke
yUsages, result->result()); |
186 return promise; | 257 return promise; |
187 } | 258 } |
188 | 259 |
189 ScriptPromise SubtleCrypto::importKey(ScriptState* scriptState, const String& ra
wFormat, const ArrayPiece& keyData, const Dictionary& rawAlgorithm, bool extract
able, const Vector<String>& rawKeyUsages) | 260 ScriptPromise SubtleCrypto::importKey(ScriptState* scriptState, const String& ra
wFormat, const ArrayPiece& keyData, const Dictionary& rawAlgorithm, bool extract
able, const Vector<String>& rawKeyUsages) |
190 { | 261 { |
191 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); | 262 RefPtr<PendingResult> pending = PendingResult::create(scriptState, this); |
192 ScriptPromise promise = result->promise(); | 263 RefPtr<CrossThreadCryptoResult<PendingResult> > result = CrossThreadCryptoRe
sult<PendingResult>::create(pending->createWeakPtr()); |
| 264 ScriptPromise promise = pending->promise(); |
193 | 265 |
194 if (!canAccessWebCrypto(scriptState, result.get())) | 266 if (!canAccessWebCrypto(scriptState, result.get())) |
195 return promise; | 267 return promise; |
196 | 268 |
197 if (!ensureNotNull(keyData, "keyData", result.get())) | 269 if (!ensureNotNull(keyData, "keyData", result.get())) |
198 return promise; | 270 return promise; |
199 | 271 |
200 blink::WebCryptoKeyFormat format; | 272 blink::WebCryptoKeyFormat format; |
201 if (!Key::parseFormat(rawFormat, format, result.get())) | 273 if (!Key::parseFormat(rawFormat, format, result.get())) |
202 return promise; | 274 return promise; |
203 | 275 |
204 blink::WebCryptoKeyUsageMask keyUsages; | 276 blink::WebCryptoKeyUsageMask keyUsages; |
205 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) | 277 if (!Key::parseUsageMask(rawKeyUsages, keyUsages, result.get())) |
206 return promise; | 278 return promise; |
207 | 279 |
208 blink::WebCryptoAlgorithm algorithm; | 280 blink::WebCryptoAlgorithm algorithm; |
209 if (!parseAlgorithm(rawAlgorithm, blink::WebCryptoOperationImportKey, algori
thm, result.get())) | 281 if (!parseAlgorithm(rawAlgorithm, blink::WebCryptoOperationImportKey, algori
thm, result.get())) |
210 return promise; | 282 return promise; |
211 | 283 |
212 blink::Platform::current()->crypto()->importKey(format, keyData.bytes(), key
Data.byteLength(), algorithm, extractable, keyUsages, result->result()); | 284 blink::Platform::current()->crypto()->importKey(format, keyData.bytes(), key
Data.byteLength(), algorithm, extractable, keyUsages, result->result()); |
213 return promise; | 285 return promise; |
214 } | 286 } |
215 | 287 |
216 ScriptPromise SubtleCrypto::exportKey(ScriptState* scriptState, const String& ra
wFormat, Key* key) | 288 ScriptPromise SubtleCrypto::exportKey(ScriptState* scriptState, const String& ra
wFormat, Key* key) |
217 { | 289 { |
218 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); | 290 RefPtr<PendingResult> pending = PendingResult::create(scriptState, this); |
219 ScriptPromise promise = result->promise(); | 291 RefPtr<CrossThreadCryptoResult<PendingResult> > result = CrossThreadCryptoRe
sult<PendingResult>::create(pending->createWeakPtr()); |
| 292 ScriptPromise promise = pending->promise(); |
220 | 293 |
221 if (!canAccessWebCrypto(scriptState, result.get())) | 294 if (!canAccessWebCrypto(scriptState, result.get())) |
222 return promise; | 295 return promise; |
223 | 296 |
224 if (!ensureNotNull(key, "key", result.get())) | 297 if (!ensureNotNull(key, "key", result.get())) |
225 return promise; | 298 return promise; |
226 | 299 |
227 blink::WebCryptoKeyFormat format; | 300 blink::WebCryptoKeyFormat format; |
228 if (!Key::parseFormat(rawFormat, format, result.get())) | 301 if (!Key::parseFormat(rawFormat, format, result.get())) |
229 return promise; | 302 return promise; |
230 | 303 |
231 if (!key->extractable()) { | 304 if (!key->extractable()) { |
232 result->completeWithError(blink::WebCryptoErrorTypeInvalidAccess, "key i
s not extractable"); | 305 result->completeWithError(blink::WebCryptoErrorTypeInvalidAccess, "key i
s not extractable"); |
233 return promise; | 306 return promise; |
234 } | 307 } |
235 | 308 |
236 blink::Platform::current()->crypto()->exportKey(format, key->key(), result->
result()); | 309 blink::Platform::current()->crypto()->exportKey(format, key->key(), result->
result()); |
237 return promise; | 310 return promise; |
238 } | 311 } |
239 | 312 |
240 ScriptPromise SubtleCrypto::wrapKey(ScriptState* scriptState, const String& rawF
ormat, Key* key, Key* wrappingKey, const Dictionary& rawWrapAlgorithm) | 313 ScriptPromise SubtleCrypto::wrapKey(ScriptState* scriptState, const String& rawF
ormat, Key* key, Key* wrappingKey, const Dictionary& rawWrapAlgorithm) |
241 { | 314 { |
242 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); | 315 RefPtr<PendingResult> pending = PendingResult::create(scriptState, this); |
243 ScriptPromise promise = result->promise(); | 316 RefPtr<CrossThreadCryptoResult<PendingResult> > result = CrossThreadCryptoRe
sult<PendingResult>::create(pending->createWeakPtr()); |
| 317 ScriptPromise promise = pending->promise(); |
244 | 318 |
245 if (!canAccessWebCrypto(scriptState, result.get())) | 319 if (!canAccessWebCrypto(scriptState, result.get())) |
246 return promise; | 320 return promise; |
247 | 321 |
248 if (!ensureNotNull(key, "key", result.get())) | 322 if (!ensureNotNull(key, "key", result.get())) |
249 return promise; | 323 return promise; |
250 | 324 |
251 if (!ensureNotNull(wrappingKey, "wrappingKey", result.get())) | 325 if (!ensureNotNull(wrappingKey, "wrappingKey", result.get())) |
252 return promise; | 326 return promise; |
253 | 327 |
(...skipping 12 matching lines...) Expand all Loading... |
266 | 340 |
267 if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, blink::WebCryptoOpera
tionWrapKey, result.get())) | 341 if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, blink::WebCryptoOpera
tionWrapKey, result.get())) |
268 return promise; | 342 return promise; |
269 | 343 |
270 blink::Platform::current()->crypto()->wrapKey(format, key->key(), wrappingKe
y->key(), wrapAlgorithm, result->result()); | 344 blink::Platform::current()->crypto()->wrapKey(format, key->key(), wrappingKe
y->key(), wrapAlgorithm, result->result()); |
271 return promise; | 345 return promise; |
272 } | 346 } |
273 | 347 |
274 ScriptPromise SubtleCrypto::unwrapKey(ScriptState* scriptState, const String& ra
wFormat, const ArrayPiece& wrappedKey, Key* unwrappingKey, const Dictionary& raw
UnwrapAlgorithm, const Dictionary& rawUnwrappedKeyAlgorithm, bool extractable, c
onst Vector<String>& rawKeyUsages) | 348 ScriptPromise SubtleCrypto::unwrapKey(ScriptState* scriptState, const String& ra
wFormat, const ArrayPiece& wrappedKey, Key* unwrappingKey, const Dictionary& raw
UnwrapAlgorithm, const Dictionary& rawUnwrappedKeyAlgorithm, bool extractable, c
onst Vector<String>& rawKeyUsages) |
275 { | 349 { |
276 RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); | 350 RefPtr<PendingResult> pending = PendingResult::create(scriptState, this); |
277 ScriptPromise promise = result->promise(); | 351 RefPtr<CrossThreadCryptoResult<PendingResult> > result = CrossThreadCryptoRe
sult<PendingResult>::create(pending->createWeakPtr()); |
| 352 ScriptPromise promise = pending->promise(); |
278 | 353 |
279 if (!canAccessWebCrypto(scriptState, result.get())) | 354 if (!canAccessWebCrypto(scriptState, result.get())) |
280 return promise; | 355 return promise; |
281 | 356 |
282 if (!ensureNotNull(wrappedKey, "wrappedKey", result.get())) | 357 if (!ensureNotNull(wrappedKey, "wrappedKey", result.get())) |
283 return promise; | 358 return promise; |
284 if (!ensureNotNull(unwrappingKey, "unwrappingKey", result.get())) | 359 if (!ensureNotNull(unwrappingKey, "unwrappingKey", result.get())) |
285 return promise; | 360 return promise; |
286 | 361 |
287 blink::WebCryptoKeyFormat format; | 362 blink::WebCryptoKeyFormat format; |
(...skipping 13 matching lines...) Expand all Loading... |
301 return promise; | 376 return promise; |
302 | 377 |
303 if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, blink::WebCryptoO
perationUnwrapKey, result.get())) | 378 if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, blink::WebCryptoO
perationUnwrapKey, result.get())) |
304 return promise; | 379 return promise; |
305 | 380 |
306 blink::Platform::current()->crypto()->unwrapKey(format, wrappedKey.bytes(),
wrappedKey.byteLength(), unwrappingKey->key(), unwrapAlgorithm, unwrappedKeyAlgo
rithm, extractable, keyUsages, result->result()); | 381 blink::Platform::current()->crypto()->unwrapKey(format, wrappedKey.bytes(),
wrappedKey.byteLength(), unwrappingKey->key(), unwrapAlgorithm, unwrappedKeyAlgo
rithm, extractable, keyUsages, result->result()); |
307 return promise; | 382 return promise; |
308 } | 383 } |
309 | 384 |
310 } // namespace WebCore | 385 } // namespace WebCore |
OLD | NEW |