OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/child/webcrypto/webcrypto_impl.h" | 5 #include "content/child/webcrypto/webcrypto_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 | 204 |
205 std::vector<uint8_t> buffer; | 205 std::vector<uint8_t> buffer; |
206 }; | 206 }; |
207 | 207 |
208 typedef EncryptState DecryptState; | 208 typedef EncryptState DecryptState; |
209 typedef EncryptState DigestState; | 209 typedef EncryptState DigestState; |
210 | 210 |
211 struct GenerateKeyState : public BaseState { | 211 struct GenerateKeyState : public BaseState { |
212 GenerateKeyState(const blink::WebCryptoAlgorithm& algorithm, | 212 GenerateKeyState(const blink::WebCryptoAlgorithm& algorithm, |
213 bool extractable, | 213 bool extractable, |
214 blink::WebCryptoKeyUsageMask usage_mask, | 214 blink::WebCryptoKeyUsageMask usages, |
215 const blink::WebCryptoResult& result) | 215 const blink::WebCryptoResult& result) |
216 : BaseState(result), | 216 : BaseState(result), |
217 algorithm(algorithm), | 217 algorithm(algorithm), |
218 extractable(extractable), | 218 extractable(extractable), |
219 usage_mask(usage_mask) {} | 219 usages(usages) {} |
220 | 220 |
221 const blink::WebCryptoAlgorithm algorithm; | 221 const blink::WebCryptoAlgorithm algorithm; |
222 const bool extractable; | 222 const bool extractable; |
223 const blink::WebCryptoKeyUsageMask usage_mask; | 223 const blink::WebCryptoKeyUsageMask usages; |
224 | 224 |
225 webcrypto::GenerateKeyResult generate_key_result; | 225 webcrypto::GenerateKeyResult generate_key_result; |
226 }; | 226 }; |
227 | 227 |
228 struct ImportKeyState : public BaseState { | 228 struct ImportKeyState : public BaseState { |
229 ImportKeyState(blink::WebCryptoKeyFormat format, | 229 ImportKeyState(blink::WebCryptoKeyFormat format, |
230 const unsigned char* key_data, | 230 const unsigned char* key_data, |
231 unsigned int key_data_size, | 231 unsigned int key_data_size, |
232 const blink::WebCryptoAlgorithm& algorithm, | 232 const blink::WebCryptoAlgorithm& algorithm, |
233 bool extractable, | 233 bool extractable, |
234 blink::WebCryptoKeyUsageMask usage_mask, | 234 blink::WebCryptoKeyUsageMask usages, |
235 const blink::WebCryptoResult& result) | 235 const blink::WebCryptoResult& result) |
236 : BaseState(result), | 236 : BaseState(result), |
237 format(format), | 237 format(format), |
238 key_data(key_data, key_data + key_data_size), | 238 key_data(key_data, key_data + key_data_size), |
239 algorithm(algorithm), | 239 algorithm(algorithm), |
240 extractable(extractable), | 240 extractable(extractable), |
241 usage_mask(usage_mask) {} | 241 usages(usages) {} |
242 | 242 |
243 const blink::WebCryptoKeyFormat format; | 243 const blink::WebCryptoKeyFormat format; |
244 const std::vector<uint8_t> key_data; | 244 const std::vector<uint8_t> key_data; |
245 const blink::WebCryptoAlgorithm algorithm; | 245 const blink::WebCryptoAlgorithm algorithm; |
246 const bool extractable; | 246 const bool extractable; |
247 const blink::WebCryptoKeyUsageMask usage_mask; | 247 const blink::WebCryptoKeyUsageMask usages; |
248 | 248 |
249 blink::WebCryptoKey key; | 249 blink::WebCryptoKey key; |
250 }; | 250 }; |
251 | 251 |
252 struct ExportKeyState : public BaseState { | 252 struct ExportKeyState : public BaseState { |
253 ExportKeyState(blink::WebCryptoKeyFormat format, | 253 ExportKeyState(blink::WebCryptoKeyFormat format, |
254 const blink::WebCryptoKey& key, | 254 const blink::WebCryptoKey& key, |
255 const blink::WebCryptoResult& result) | 255 const blink::WebCryptoResult& result) |
256 : BaseState(result), format(format), key(key) {} | 256 : BaseState(result), format(format), key(key) {} |
257 | 257 |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 state->generate_key_result.Complete(&state->result); | 396 state->generate_key_result.Complete(&state->result); |
397 } | 397 } |
398 } | 398 } |
399 | 399 |
400 void DoGenerateKey(scoped_ptr<GenerateKeyState> passed_state) { | 400 void DoGenerateKey(scoped_ptr<GenerateKeyState> passed_state) { |
401 GenerateKeyState* state = passed_state.get(); | 401 GenerateKeyState* state = passed_state.get(); |
402 if (state->cancelled()) | 402 if (state->cancelled()) |
403 return; | 403 return; |
404 state->status = webcrypto::GenerateKey(state->algorithm, | 404 state->status = webcrypto::GenerateKey(state->algorithm, |
405 state->extractable, | 405 state->extractable, |
406 state->usage_mask, | 406 state->usages, |
407 &state->generate_key_result); | 407 &state->generate_key_result); |
408 state->origin_thread->PostTask( | 408 state->origin_thread->PostTask( |
409 FROM_HERE, base::Bind(DoGenerateKeyReply, Passed(&passed_state))); | 409 FROM_HERE, base::Bind(DoGenerateKeyReply, Passed(&passed_state))); |
410 } | 410 } |
411 | 411 |
412 void DoImportKeyReply(scoped_ptr<ImportKeyState> state) { | 412 void DoImportKeyReply(scoped_ptr<ImportKeyState> state) { |
413 CompleteWithKeyOrError(state->status, state->key, &state->result); | 413 CompleteWithKeyOrError(state->status, state->key, &state->result); |
414 } | 414 } |
415 | 415 |
416 void DoImportKey(scoped_ptr<ImportKeyState> passed_state) { | 416 void DoImportKey(scoped_ptr<ImportKeyState> passed_state) { |
417 ImportKeyState* state = passed_state.get(); | 417 ImportKeyState* state = passed_state.get(); |
418 if (state->cancelled()) | 418 if (state->cancelled()) |
419 return; | 419 return; |
420 state->status = webcrypto::ImportKey(state->format, | 420 state->status = webcrypto::ImportKey(state->format, |
421 webcrypto::CryptoData(state->key_data), | 421 webcrypto::CryptoData(state->key_data), |
422 state->algorithm, | 422 state->algorithm, |
423 state->extractable, | 423 state->extractable, |
424 state->usage_mask, | 424 state->usages, |
425 &state->key); | 425 &state->key); |
426 if (state->status.IsSuccess()) { | 426 if (state->status.IsSuccess()) { |
427 DCHECK(state->key.handle()); | 427 DCHECK(state->key.handle()); |
428 DCHECK(!state->key.algorithm().isNull()); | 428 DCHECK(!state->key.algorithm().isNull()); |
429 DCHECK_EQ(state->extractable, state->key.extractable()); | 429 DCHECK_EQ(state->extractable, state->key.extractable()); |
430 } | 430 } |
431 | 431 |
432 state->origin_thread->PostTask( | 432 state->origin_thread->PostTask( |
433 FROM_HERE, base::Bind(DoImportKeyReply, Passed(&passed_state))); | 433 FROM_HERE, base::Bind(DoImportKeyReply, Passed(&passed_state))); |
434 } | 434 } |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
584 scoped_ptr<DigestState> state(new DigestState( | 584 scoped_ptr<DigestState> state(new DigestState( |
585 algorithm, blink::WebCryptoKey::createNull(), data, data_size, result)); | 585 algorithm, blink::WebCryptoKey::createNull(), data, data_size, result)); |
586 if (!CryptoThreadPool::PostTask(FROM_HERE, | 586 if (!CryptoThreadPool::PostTask(FROM_HERE, |
587 base::Bind(DoDigest, Passed(&state)))) { | 587 base::Bind(DoDigest, Passed(&state)))) { |
588 CompleteWithThreadPoolError(&result); | 588 CompleteWithThreadPoolError(&result); |
589 } | 589 } |
590 } | 590 } |
591 | 591 |
592 void WebCryptoImpl::generateKey(const blink::WebCryptoAlgorithm& algorithm, | 592 void WebCryptoImpl::generateKey(const blink::WebCryptoAlgorithm& algorithm, |
593 bool extractable, | 593 bool extractable, |
594 blink::WebCryptoKeyUsageMask usage_mask, | 594 blink::WebCryptoKeyUsageMask usages, |
595 blink::WebCryptoResult result) { | 595 blink::WebCryptoResult result) { |
596 DCHECK(!algorithm.isNull()); | 596 DCHECK(!algorithm.isNull()); |
597 | 597 |
598 scoped_ptr<GenerateKeyState> state( | 598 scoped_ptr<GenerateKeyState> state( |
599 new GenerateKeyState(algorithm, extractable, usage_mask, result)); | 599 new GenerateKeyState(algorithm, extractable, usages, result)); |
600 if (!CryptoThreadPool::PostTask(FROM_HERE, | 600 if (!CryptoThreadPool::PostTask(FROM_HERE, |
601 base::Bind(DoGenerateKey, Passed(&state)))) { | 601 base::Bind(DoGenerateKey, Passed(&state)))) { |
602 CompleteWithThreadPoolError(&result); | 602 CompleteWithThreadPoolError(&result); |
603 } | 603 } |
604 } | 604 } |
605 | 605 |
606 void WebCryptoImpl::importKey(blink::WebCryptoKeyFormat format, | 606 void WebCryptoImpl::importKey(blink::WebCryptoKeyFormat format, |
607 const unsigned char* key_data, | 607 const unsigned char* key_data, |
608 unsigned int key_data_size, | 608 unsigned int key_data_size, |
609 const blink::WebCryptoAlgorithm& algorithm, | 609 const blink::WebCryptoAlgorithm& algorithm, |
610 bool extractable, | 610 bool extractable, |
611 blink::WebCryptoKeyUsageMask usage_mask, | 611 blink::WebCryptoKeyUsageMask usages, |
612 blink::WebCryptoResult result) { | 612 blink::WebCryptoResult result) { |
613 scoped_ptr<ImportKeyState> state(new ImportKeyState(format, | 613 scoped_ptr<ImportKeyState> state(new ImportKeyState( |
614 key_data, | 614 format, key_data, key_data_size, algorithm, extractable, usages, result)); |
615 key_data_size, | |
616 algorithm, | |
617 extractable, | |
618 usage_mask, | |
619 result)); | |
620 if (!CryptoThreadPool::PostTask(FROM_HERE, | 615 if (!CryptoThreadPool::PostTask(FROM_HERE, |
621 base::Bind(DoImportKey, Passed(&state)))) { | 616 base::Bind(DoImportKey, Passed(&state)))) { |
622 CompleteWithThreadPoolError(&result); | 617 CompleteWithThreadPoolError(&result); |
623 } | 618 } |
624 } | 619 } |
625 | 620 |
626 void WebCryptoImpl::exportKey(blink::WebCryptoKeyFormat format, | 621 void WebCryptoImpl::exportKey(blink::WebCryptoKeyFormat format, |
627 const blink::WebCryptoKey& key, | 622 const blink::WebCryptoKey& key, |
628 blink::WebCryptoResult result) { | 623 blink::WebCryptoResult result) { |
629 scoped_ptr<ExportKeyState> state(new ExportKeyState(format, key, result)); | 624 scoped_ptr<ExportKeyState> state(new ExportKeyState(format, key, result)); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
723 &key); | 718 &key); |
724 } | 719 } |
725 | 720 |
726 bool WebCryptoImpl::serializeKeyForClone( | 721 bool WebCryptoImpl::serializeKeyForClone( |
727 const blink::WebCryptoKey& key, | 722 const blink::WebCryptoKey& key, |
728 blink::WebVector<unsigned char>& key_data) { | 723 blink::WebVector<unsigned char>& key_data) { |
729 return webcrypto::SerializeKeyForClone(key, &key_data); | 724 return webcrypto::SerializeKeyForClone(key, &key_data); |
730 } | 725 } |
731 | 726 |
732 } // namespace content | 727 } // namespace content |
OLD | NEW |