| 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 "components/webcrypto/webcrypto_impl.h" | 5 #include "components/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 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 // | 155 // |
| 156 // Ownership of the State object is passed between the crypto thread and the | 156 // Ownership of the State object is passed between the crypto thread and the |
| 157 // Blink thread. Under normal completion it is destroyed on the Blink thread. | 157 // Blink thread. Under normal completion it is destroyed on the Blink thread. |
| 158 // However it may also be destroyed on the crypto thread if the Blink thread | 158 // However it may also be destroyed on the crypto thread if the Blink thread |
| 159 // has vanished (which can happen for Blink web worker threads). | 159 // has vanished (which can happen for Blink web worker threads). |
| 160 | 160 |
| 161 struct BaseState { | 161 struct BaseState { |
| 162 explicit BaseState(const blink::WebCryptoResult& result) | 162 explicit BaseState(const blink::WebCryptoResult& result) |
| 163 : origin_thread(GetCurrentBlinkThread()), result(result) {} | 163 : origin_thread(GetCurrentBlinkThread()), result(result) {} |
| 164 | 164 |
| 165 bool cancelled() { return result.cancelled(); } | |
| 166 | |
| 167 scoped_refptr<base::TaskRunner> origin_thread; | 165 scoped_refptr<base::TaskRunner> origin_thread; |
| 168 | 166 |
| 169 webcrypto::Status status; | 167 webcrypto::Status status; |
| 170 blink::WebCryptoResult result; | 168 blink::WebCryptoResult result; |
| 171 | 169 |
| 172 protected: | 170 protected: |
| 173 // Since there is no virtual destructor, must not delete directly as a | 171 // Since there is no virtual destructor, must not delete directly as a |
| 174 // BaseState. | 172 // BaseState. |
| 175 ~BaseState() {} | 173 ~BaseState() {} |
| 176 }; | 174 }; |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 // | 371 // |
| 374 // * The methods named Do*() run on the crypto thread. | 372 // * The methods named Do*() run on the crypto thread. |
| 375 // * The methods named Do*Reply() run on the target Blink thread | 373 // * The methods named Do*Reply() run on the target Blink thread |
| 376 | 374 |
| 377 void DoEncryptReply(scoped_ptr<EncryptState> state) { | 375 void DoEncryptReply(scoped_ptr<EncryptState> state) { |
| 378 CompleteWithBufferOrError(state->status, state->buffer, &state->result); | 376 CompleteWithBufferOrError(state->status, state->buffer, &state->result); |
| 379 } | 377 } |
| 380 | 378 |
| 381 void DoEncrypt(scoped_ptr<EncryptState> passed_state) { | 379 void DoEncrypt(scoped_ptr<EncryptState> passed_state) { |
| 382 EncryptState* state = passed_state.get(); | 380 EncryptState* state = passed_state.get(); |
| 383 if (state->cancelled()) | |
| 384 return; | |
| 385 state->status = | 381 state->status = |
| 386 webcrypto::Encrypt(state->algorithm, state->key, | 382 webcrypto::Encrypt(state->algorithm, state->key, |
| 387 webcrypto::CryptoData(state->data), &state->buffer); | 383 webcrypto::CryptoData(state->data), &state->buffer); |
| 388 state->origin_thread->PostTask( | 384 state->origin_thread->PostTask( |
| 389 FROM_HERE, base::Bind(DoEncryptReply, Passed(&passed_state))); | 385 FROM_HERE, base::Bind(DoEncryptReply, Passed(&passed_state))); |
| 390 } | 386 } |
| 391 | 387 |
| 392 void DoDecryptReply(scoped_ptr<DecryptState> state) { | 388 void DoDecryptReply(scoped_ptr<DecryptState> state) { |
| 393 CompleteWithBufferOrError(state->status, state->buffer, &state->result); | 389 CompleteWithBufferOrError(state->status, state->buffer, &state->result); |
| 394 } | 390 } |
| 395 | 391 |
| 396 void DoDecrypt(scoped_ptr<DecryptState> passed_state) { | 392 void DoDecrypt(scoped_ptr<DecryptState> passed_state) { |
| 397 DecryptState* state = passed_state.get(); | 393 DecryptState* state = passed_state.get(); |
| 398 if (state->cancelled()) | |
| 399 return; | |
| 400 state->status = | 394 state->status = |
| 401 webcrypto::Decrypt(state->algorithm, state->key, | 395 webcrypto::Decrypt(state->algorithm, state->key, |
| 402 webcrypto::CryptoData(state->data), &state->buffer); | 396 webcrypto::CryptoData(state->data), &state->buffer); |
| 403 state->origin_thread->PostTask( | 397 state->origin_thread->PostTask( |
| 404 FROM_HERE, base::Bind(DoDecryptReply, Passed(&passed_state))); | 398 FROM_HERE, base::Bind(DoDecryptReply, Passed(&passed_state))); |
| 405 } | 399 } |
| 406 | 400 |
| 407 void DoDigestReply(scoped_ptr<DigestState> state) { | 401 void DoDigestReply(scoped_ptr<DigestState> state) { |
| 408 CompleteWithBufferOrError(state->status, state->buffer, &state->result); | 402 CompleteWithBufferOrError(state->status, state->buffer, &state->result); |
| 409 } | 403 } |
| 410 | 404 |
| 411 void DoDigest(scoped_ptr<DigestState> passed_state) { | 405 void DoDigest(scoped_ptr<DigestState> passed_state) { |
| 412 DigestState* state = passed_state.get(); | 406 DigestState* state = passed_state.get(); |
| 413 if (state->cancelled()) | |
| 414 return; | |
| 415 state->status = webcrypto::Digest( | 407 state->status = webcrypto::Digest( |
| 416 state->algorithm, webcrypto::CryptoData(state->data), &state->buffer); | 408 state->algorithm, webcrypto::CryptoData(state->data), &state->buffer); |
| 417 state->origin_thread->PostTask( | 409 state->origin_thread->PostTask( |
| 418 FROM_HERE, base::Bind(DoDigestReply, Passed(&passed_state))); | 410 FROM_HERE, base::Bind(DoDigestReply, Passed(&passed_state))); |
| 419 } | 411 } |
| 420 | 412 |
| 421 void DoGenerateKeyReply(scoped_ptr<GenerateKeyState> state) { | 413 void DoGenerateKeyReply(scoped_ptr<GenerateKeyState> state) { |
| 422 if (state->status.IsError()) { | 414 if (state->status.IsError()) { |
| 423 CompleteWithError(state->status, &state->result); | 415 CompleteWithError(state->status, &state->result); |
| 424 } else { | 416 } else { |
| 425 state->generate_key_result.Complete(&state->result); | 417 state->generate_key_result.Complete(&state->result); |
| 426 } | 418 } |
| 427 } | 419 } |
| 428 | 420 |
| 429 void DoGenerateKey(scoped_ptr<GenerateKeyState> passed_state) { | 421 void DoGenerateKey(scoped_ptr<GenerateKeyState> passed_state) { |
| 430 GenerateKeyState* state = passed_state.get(); | 422 GenerateKeyState* state = passed_state.get(); |
| 431 if (state->cancelled()) | |
| 432 return; | |
| 433 state->status = | 423 state->status = |
| 434 webcrypto::GenerateKey(state->algorithm, state->extractable, | 424 webcrypto::GenerateKey(state->algorithm, state->extractable, |
| 435 state->usages, &state->generate_key_result); | 425 state->usages, &state->generate_key_result); |
| 436 state->origin_thread->PostTask( | 426 state->origin_thread->PostTask( |
| 437 FROM_HERE, base::Bind(DoGenerateKeyReply, Passed(&passed_state))); | 427 FROM_HERE, base::Bind(DoGenerateKeyReply, Passed(&passed_state))); |
| 438 } | 428 } |
| 439 | 429 |
| 440 void DoImportKeyReply(scoped_ptr<ImportKeyState> state) { | 430 void DoImportKeyReply(scoped_ptr<ImportKeyState> state) { |
| 441 CompleteWithKeyOrError(state->status, state->key, &state->result); | 431 CompleteWithKeyOrError(state->status, state->key, &state->result); |
| 442 } | 432 } |
| 443 | 433 |
| 444 void DoImportKey(scoped_ptr<ImportKeyState> passed_state) { | 434 void DoImportKey(scoped_ptr<ImportKeyState> passed_state) { |
| 445 ImportKeyState* state = passed_state.get(); | 435 ImportKeyState* state = passed_state.get(); |
| 446 if (state->cancelled()) | |
| 447 return; | |
| 448 state->status = webcrypto::ImportKey( | 436 state->status = webcrypto::ImportKey( |
| 449 state->format, webcrypto::CryptoData(state->key_data), state->algorithm, | 437 state->format, webcrypto::CryptoData(state->key_data), state->algorithm, |
| 450 state->extractable, state->usages, &state->key); | 438 state->extractable, state->usages, &state->key); |
| 451 if (state->status.IsSuccess()) { | 439 if (state->status.IsSuccess()) { |
| 452 DCHECK(state->key.handle()); | 440 DCHECK(state->key.handle()); |
| 453 DCHECK(!state->key.algorithm().isNull()); | 441 DCHECK(!state->key.algorithm().isNull()); |
| 454 DCHECK_EQ(state->extractable, state->key.extractable()); | 442 DCHECK_EQ(state->extractable, state->key.extractable()); |
| 455 } | 443 } |
| 456 | 444 |
| 457 state->origin_thread->PostTask( | 445 state->origin_thread->PostTask( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 468 CompleteWithError(state->status, &state->result); | 456 CompleteWithError(state->status, &state->result); |
| 469 } else { | 457 } else { |
| 470 state->result.completeWithJson( | 458 state->result.completeWithJson( |
| 471 reinterpret_cast<const char*>(vector_as_array(&state->buffer)), | 459 reinterpret_cast<const char*>(vector_as_array(&state->buffer)), |
| 472 static_cast<unsigned int>(state->buffer.size())); | 460 static_cast<unsigned int>(state->buffer.size())); |
| 473 } | 461 } |
| 474 } | 462 } |
| 475 | 463 |
| 476 void DoExportKey(scoped_ptr<ExportKeyState> passed_state) { | 464 void DoExportKey(scoped_ptr<ExportKeyState> passed_state) { |
| 477 ExportKeyState* state = passed_state.get(); | 465 ExportKeyState* state = passed_state.get(); |
| 478 if (state->cancelled()) | |
| 479 return; | |
| 480 state->status = | 466 state->status = |
| 481 webcrypto::ExportKey(state->format, state->key, &state->buffer); | 467 webcrypto::ExportKey(state->format, state->key, &state->buffer); |
| 482 state->origin_thread->PostTask( | 468 state->origin_thread->PostTask( |
| 483 FROM_HERE, base::Bind(DoExportKeyReply, Passed(&passed_state))); | 469 FROM_HERE, base::Bind(DoExportKeyReply, Passed(&passed_state))); |
| 484 } | 470 } |
| 485 | 471 |
| 486 void DoSignReply(scoped_ptr<SignState> state) { | 472 void DoSignReply(scoped_ptr<SignState> state) { |
| 487 CompleteWithBufferOrError(state->status, state->buffer, &state->result); | 473 CompleteWithBufferOrError(state->status, state->buffer, &state->result); |
| 488 } | 474 } |
| 489 | 475 |
| 490 void DoSign(scoped_ptr<SignState> passed_state) { | 476 void DoSign(scoped_ptr<SignState> passed_state) { |
| 491 SignState* state = passed_state.get(); | 477 SignState* state = passed_state.get(); |
| 492 if (state->cancelled()) | |
| 493 return; | |
| 494 state->status = | 478 state->status = |
| 495 webcrypto::Sign(state->algorithm, state->key, | 479 webcrypto::Sign(state->algorithm, state->key, |
| 496 webcrypto::CryptoData(state->data), &state->buffer); | 480 webcrypto::CryptoData(state->data), &state->buffer); |
| 497 | 481 |
| 498 state->origin_thread->PostTask( | 482 state->origin_thread->PostTask( |
| 499 FROM_HERE, base::Bind(DoSignReply, Passed(&passed_state))); | 483 FROM_HERE, base::Bind(DoSignReply, Passed(&passed_state))); |
| 500 } | 484 } |
| 501 | 485 |
| 502 void DoVerifyReply(scoped_ptr<VerifySignatureState> state) { | 486 void DoVerifyReply(scoped_ptr<VerifySignatureState> state) { |
| 503 if (state->status.IsError()) { | 487 if (state->status.IsError()) { |
| 504 CompleteWithError(state->status, &state->result); | 488 CompleteWithError(state->status, &state->result); |
| 505 } else { | 489 } else { |
| 506 state->result.completeWithBoolean(state->verify_result); | 490 state->result.completeWithBoolean(state->verify_result); |
| 507 } | 491 } |
| 508 } | 492 } |
| 509 | 493 |
| 510 void DoVerify(scoped_ptr<VerifySignatureState> passed_state) { | 494 void DoVerify(scoped_ptr<VerifySignatureState> passed_state) { |
| 511 VerifySignatureState* state = passed_state.get(); | 495 VerifySignatureState* state = passed_state.get(); |
| 512 if (state->cancelled()) | |
| 513 return; | |
| 514 state->status = webcrypto::Verify( | 496 state->status = webcrypto::Verify( |
| 515 state->algorithm, state->key, webcrypto::CryptoData(state->signature), | 497 state->algorithm, state->key, webcrypto::CryptoData(state->signature), |
| 516 webcrypto::CryptoData(state->data), &state->verify_result); | 498 webcrypto::CryptoData(state->data), &state->verify_result); |
| 517 | 499 |
| 518 state->origin_thread->PostTask( | 500 state->origin_thread->PostTask( |
| 519 FROM_HERE, base::Bind(DoVerifyReply, Passed(&passed_state))); | 501 FROM_HERE, base::Bind(DoVerifyReply, Passed(&passed_state))); |
| 520 } | 502 } |
| 521 | 503 |
| 522 void DoWrapKeyReply(scoped_ptr<WrapKeyState> state) { | 504 void DoWrapKeyReply(scoped_ptr<WrapKeyState> state) { |
| 523 CompleteWithBufferOrError(state->status, state->buffer, &state->result); | 505 CompleteWithBufferOrError(state->status, state->buffer, &state->result); |
| 524 } | 506 } |
| 525 | 507 |
| 526 void DoWrapKey(scoped_ptr<WrapKeyState> passed_state) { | 508 void DoWrapKey(scoped_ptr<WrapKeyState> passed_state) { |
| 527 WrapKeyState* state = passed_state.get(); | 509 WrapKeyState* state = passed_state.get(); |
| 528 if (state->cancelled()) | |
| 529 return; | |
| 530 state->status = | 510 state->status = |
| 531 webcrypto::WrapKey(state->format, state->key, state->wrapping_key, | 511 webcrypto::WrapKey(state->format, state->key, state->wrapping_key, |
| 532 state->wrap_algorithm, &state->buffer); | 512 state->wrap_algorithm, &state->buffer); |
| 533 | 513 |
| 534 state->origin_thread->PostTask( | 514 state->origin_thread->PostTask( |
| 535 FROM_HERE, base::Bind(DoWrapKeyReply, Passed(&passed_state))); | 515 FROM_HERE, base::Bind(DoWrapKeyReply, Passed(&passed_state))); |
| 536 } | 516 } |
| 537 | 517 |
| 538 void DoUnwrapKeyReply(scoped_ptr<UnwrapKeyState> state) { | 518 void DoUnwrapKeyReply(scoped_ptr<UnwrapKeyState> state) { |
| 539 CompleteWithKeyOrError(state->status, state->unwrapped_key, &state->result); | 519 CompleteWithKeyOrError(state->status, state->unwrapped_key, &state->result); |
| 540 } | 520 } |
| 541 | 521 |
| 542 void DoUnwrapKey(scoped_ptr<UnwrapKeyState> passed_state) { | 522 void DoUnwrapKey(scoped_ptr<UnwrapKeyState> passed_state) { |
| 543 UnwrapKeyState* state = passed_state.get(); | 523 UnwrapKeyState* state = passed_state.get(); |
| 544 if (state->cancelled()) | |
| 545 return; | |
| 546 state->status = webcrypto::UnwrapKey( | 524 state->status = webcrypto::UnwrapKey( |
| 547 state->format, webcrypto::CryptoData(state->wrapped_key), | 525 state->format, webcrypto::CryptoData(state->wrapped_key), |
| 548 state->wrapping_key, state->unwrap_algorithm, | 526 state->wrapping_key, state->unwrap_algorithm, |
| 549 state->unwrapped_key_algorithm, state->extractable, state->usages, | 527 state->unwrapped_key_algorithm, state->extractable, state->usages, |
| 550 &state->unwrapped_key); | 528 &state->unwrapped_key); |
| 551 | 529 |
| 552 state->origin_thread->PostTask( | 530 state->origin_thread->PostTask( |
| 553 FROM_HERE, base::Bind(DoUnwrapKeyReply, Passed(&passed_state))); | 531 FROM_HERE, base::Bind(DoUnwrapKeyReply, Passed(&passed_state))); |
| 554 } | 532 } |
| 555 | 533 |
| 556 void DoDeriveBitsReply(scoped_ptr<DeriveBitsState> state) { | 534 void DoDeriveBitsReply(scoped_ptr<DeriveBitsState> state) { |
| 557 CompleteWithBufferOrError(state->status, state->derived_bytes, | 535 CompleteWithBufferOrError(state->status, state->derived_bytes, |
| 558 &state->result); | 536 &state->result); |
| 559 } | 537 } |
| 560 | 538 |
| 561 void DoDeriveBits(scoped_ptr<DeriveBitsState> passed_state) { | 539 void DoDeriveBits(scoped_ptr<DeriveBitsState> passed_state) { |
| 562 DeriveBitsState* state = passed_state.get(); | 540 DeriveBitsState* state = passed_state.get(); |
| 563 if (state->cancelled()) | |
| 564 return; | |
| 565 state->status = | 541 state->status = |
| 566 webcrypto::DeriveBits(state->algorithm, state->base_key, | 542 webcrypto::DeriveBits(state->algorithm, state->base_key, |
| 567 state->length_bits, &state->derived_bytes); | 543 state->length_bits, &state->derived_bytes); |
| 568 state->origin_thread->PostTask( | 544 state->origin_thread->PostTask( |
| 569 FROM_HERE, base::Bind(DoDeriveBitsReply, Passed(&passed_state))); | 545 FROM_HERE, base::Bind(DoDeriveBitsReply, Passed(&passed_state))); |
| 570 } | 546 } |
| 571 | 547 |
| 572 void DoDeriveKeyReply(scoped_ptr<DeriveKeyState> state) { | 548 void DoDeriveKeyReply(scoped_ptr<DeriveKeyState> state) { |
| 573 CompleteWithKeyOrError(state->status, state->derived_key, &state->result); | 549 CompleteWithKeyOrError(state->status, state->derived_key, &state->result); |
| 574 } | 550 } |
| 575 | 551 |
| 576 void DoDeriveKey(scoped_ptr<DeriveKeyState> passed_state) { | 552 void DoDeriveKey(scoped_ptr<DeriveKeyState> passed_state) { |
| 577 DeriveKeyState* state = passed_state.get(); | 553 DeriveKeyState* state = passed_state.get(); |
| 578 if (state->cancelled()) | |
| 579 return; | |
| 580 state->status = webcrypto::DeriveKey( | 554 state->status = webcrypto::DeriveKey( |
| 581 state->algorithm, state->base_key, state->import_algorithm, | 555 state->algorithm, state->base_key, state->import_algorithm, |
| 582 state->key_length_algorithm, state->extractable, state->usages, | 556 state->key_length_algorithm, state->extractable, state->usages, |
| 583 &state->derived_key); | 557 &state->derived_key); |
| 584 state->origin_thread->PostTask( | 558 state->origin_thread->PostTask( |
| 585 FROM_HERE, base::Bind(DoDeriveKeyReply, Passed(&passed_state))); | 559 FROM_HERE, base::Bind(DoDeriveKeyReply, Passed(&passed_state))); |
| 586 } | 560 } |
| 587 | 561 |
| 588 } // namespace | 562 } // namespace |
| 589 | 563 |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 783 webcrypto::CryptoData(key_data, key_data_size), &key); | 757 webcrypto::CryptoData(key_data, key_data_size), &key); |
| 784 } | 758 } |
| 785 | 759 |
| 786 bool WebCryptoImpl::serializeKeyForClone( | 760 bool WebCryptoImpl::serializeKeyForClone( |
| 787 const blink::WebCryptoKey& key, | 761 const blink::WebCryptoKey& key, |
| 788 blink::WebVector<unsigned char>& key_data) { | 762 blink::WebVector<unsigned char>& key_data) { |
| 789 return webcrypto::SerializeKeyForClone(key, &key_data); | 763 return webcrypto::SerializeKeyForClone(key, &key_data); |
| 790 } | 764 } |
| 791 | 765 |
| 792 } // namespace webcrypto | 766 } // namespace webcrypto |
| OLD | NEW |