| 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 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 }; | 368 }; |
| 369 | 369 |
| 370 // -------------------------------------------------------------------- | 370 // -------------------------------------------------------------------- |
| 371 // Wrapper functions | 371 // Wrapper functions |
| 372 // -------------------------------------------------------------------- | 372 // -------------------------------------------------------------------- |
| 373 // | 373 // |
| 374 // * The methods named Do*() run on the crypto thread. | 374 // * The methods named Do*() run on the crypto thread. |
| 375 // * The methods named Do*Reply() run on the target Blink thread | 375 // * The methods named Do*Reply() run on the target Blink thread |
| 376 | 376 |
| 377 void DoEncryptReply(scoped_ptr<EncryptState> state) { | 377 void DoEncryptReply(scoped_ptr<EncryptState> state) { |
| 378 if (state->cancelled()) |
| 379 return; |
| 378 CompleteWithBufferOrError(state->status, state->buffer, &state->result); | 380 CompleteWithBufferOrError(state->status, state->buffer, &state->result); |
| 379 } | 381 } |
| 380 | 382 |
| 381 void DoEncrypt(scoped_ptr<EncryptState> passed_state) { | 383 void DoEncrypt(scoped_ptr<EncryptState> passed_state) { |
| 382 EncryptState* state = passed_state.get(); | 384 EncryptState* state = passed_state.get(); |
| 383 if (state->cancelled()) | 385 if (state->cancelled()) |
| 384 return; | 386 return; |
| 385 state->status = | 387 state->status = |
| 386 webcrypto::Encrypt(state->algorithm, state->key, | 388 webcrypto::Encrypt(state->algorithm, state->key, |
| 387 webcrypto::CryptoData(state->data), &state->buffer); | 389 webcrypto::CryptoData(state->data), &state->buffer); |
| 388 state->origin_thread->PostTask( | 390 state->origin_thread->PostTask( |
| 389 FROM_HERE, base::Bind(DoEncryptReply, Passed(&passed_state))); | 391 FROM_HERE, base::Bind(DoEncryptReply, Passed(&passed_state))); |
| 390 } | 392 } |
| 391 | 393 |
| 392 void DoDecryptReply(scoped_ptr<DecryptState> state) { | 394 void DoDecryptReply(scoped_ptr<DecryptState> state) { |
| 395 if (state->cancelled()) |
| 396 return; |
| 393 CompleteWithBufferOrError(state->status, state->buffer, &state->result); | 397 CompleteWithBufferOrError(state->status, state->buffer, &state->result); |
| 394 } | 398 } |
| 395 | 399 |
| 396 void DoDecrypt(scoped_ptr<DecryptState> passed_state) { | 400 void DoDecrypt(scoped_ptr<DecryptState> passed_state) { |
| 397 DecryptState* state = passed_state.get(); | 401 DecryptState* state = passed_state.get(); |
| 398 if (state->cancelled()) | 402 if (state->cancelled()) |
| 399 return; | 403 return; |
| 400 state->status = | 404 state->status = |
| 401 webcrypto::Decrypt(state->algorithm, state->key, | 405 webcrypto::Decrypt(state->algorithm, state->key, |
| 402 webcrypto::CryptoData(state->data), &state->buffer); | 406 webcrypto::CryptoData(state->data), &state->buffer); |
| 403 state->origin_thread->PostTask( | 407 state->origin_thread->PostTask( |
| 404 FROM_HERE, base::Bind(DoDecryptReply, Passed(&passed_state))); | 408 FROM_HERE, base::Bind(DoDecryptReply, Passed(&passed_state))); |
| 405 } | 409 } |
| 406 | 410 |
| 407 void DoDigestReply(scoped_ptr<DigestState> state) { | 411 void DoDigestReply(scoped_ptr<DigestState> state) { |
| 412 if (state->cancelled()) |
| 413 return; |
| 408 CompleteWithBufferOrError(state->status, state->buffer, &state->result); | 414 CompleteWithBufferOrError(state->status, state->buffer, &state->result); |
| 409 } | 415 } |
| 410 | 416 |
| 411 void DoDigest(scoped_ptr<DigestState> passed_state) { | 417 void DoDigest(scoped_ptr<DigestState> passed_state) { |
| 412 DigestState* state = passed_state.get(); | 418 DigestState* state = passed_state.get(); |
| 413 if (state->cancelled()) | 419 if (state->cancelled()) |
| 414 return; | 420 return; |
| 415 state->status = webcrypto::Digest( | 421 state->status = webcrypto::Digest( |
| 416 state->algorithm, webcrypto::CryptoData(state->data), &state->buffer); | 422 state->algorithm, webcrypto::CryptoData(state->data), &state->buffer); |
| 417 state->origin_thread->PostTask( | 423 state->origin_thread->PostTask( |
| 418 FROM_HERE, base::Bind(DoDigestReply, Passed(&passed_state))); | 424 FROM_HERE, base::Bind(DoDigestReply, Passed(&passed_state))); |
| 419 } | 425 } |
| 420 | 426 |
| 421 void DoGenerateKeyReply(scoped_ptr<GenerateKeyState> state) { | 427 void DoGenerateKeyReply(scoped_ptr<GenerateKeyState> state) { |
| 428 if (state->cancelled()) |
| 429 return; |
| 422 if (state->status.IsError()) { | 430 if (state->status.IsError()) { |
| 423 CompleteWithError(state->status, &state->result); | 431 CompleteWithError(state->status, &state->result); |
| 424 } else { | 432 } else { |
| 425 state->generate_key_result.Complete(&state->result); | 433 state->generate_key_result.Complete(&state->result); |
| 426 } | 434 } |
| 427 } | 435 } |
| 428 | 436 |
| 429 void DoGenerateKey(scoped_ptr<GenerateKeyState> passed_state) { | 437 void DoGenerateKey(scoped_ptr<GenerateKeyState> passed_state) { |
| 430 GenerateKeyState* state = passed_state.get(); | 438 GenerateKeyState* state = passed_state.get(); |
| 431 if (state->cancelled()) | 439 if (state->cancelled()) |
| 432 return; | 440 return; |
| 433 state->status = | 441 state->status = |
| 434 webcrypto::GenerateKey(state->algorithm, state->extractable, | 442 webcrypto::GenerateKey(state->algorithm, state->extractable, |
| 435 state->usages, &state->generate_key_result); | 443 state->usages, &state->generate_key_result); |
| 436 state->origin_thread->PostTask( | 444 state->origin_thread->PostTask( |
| 437 FROM_HERE, base::Bind(DoGenerateKeyReply, Passed(&passed_state))); | 445 FROM_HERE, base::Bind(DoGenerateKeyReply, Passed(&passed_state))); |
| 438 } | 446 } |
| 439 | 447 |
| 440 void DoImportKeyReply(scoped_ptr<ImportKeyState> state) { | 448 void DoImportKeyReply(scoped_ptr<ImportKeyState> state) { |
| 449 if (state->cancelled()) |
| 450 return; |
| 441 CompleteWithKeyOrError(state->status, state->key, &state->result); | 451 CompleteWithKeyOrError(state->status, state->key, &state->result); |
| 442 } | 452 } |
| 443 | 453 |
| 444 void DoImportKey(scoped_ptr<ImportKeyState> passed_state) { | 454 void DoImportKey(scoped_ptr<ImportKeyState> passed_state) { |
| 445 ImportKeyState* state = passed_state.get(); | 455 ImportKeyState* state = passed_state.get(); |
| 446 if (state->cancelled()) | 456 if (state->cancelled()) |
| 447 return; | 457 return; |
| 448 state->status = webcrypto::ImportKey( | 458 state->status = webcrypto::ImportKey( |
| 449 state->format, webcrypto::CryptoData(state->key_data), state->algorithm, | 459 state->format, webcrypto::CryptoData(state->key_data), state->algorithm, |
| 450 state->extractable, state->usages, &state->key); | 460 state->extractable, state->usages, &state->key); |
| 451 if (state->status.IsSuccess()) { | 461 if (state->status.IsSuccess()) { |
| 452 DCHECK(state->key.handle()); | 462 DCHECK(state->key.handle()); |
| 453 DCHECK(!state->key.algorithm().isNull()); | 463 DCHECK(!state->key.algorithm().isNull()); |
| 454 DCHECK_EQ(state->extractable, state->key.extractable()); | 464 DCHECK_EQ(state->extractable, state->key.extractable()); |
| 455 } | 465 } |
| 456 | 466 |
| 457 state->origin_thread->PostTask( | 467 state->origin_thread->PostTask( |
| 458 FROM_HERE, base::Bind(DoImportKeyReply, Passed(&passed_state))); | 468 FROM_HERE, base::Bind(DoImportKeyReply, Passed(&passed_state))); |
| 459 } | 469 } |
| 460 | 470 |
| 461 void DoExportKeyReply(scoped_ptr<ExportKeyState> state) { | 471 void DoExportKeyReply(scoped_ptr<ExportKeyState> state) { |
| 472 if (state->cancelled()) |
| 473 return; |
| 462 if (state->format != blink::WebCryptoKeyFormatJwk) { | 474 if (state->format != blink::WebCryptoKeyFormatJwk) { |
| 463 CompleteWithBufferOrError(state->status, state->buffer, &state->result); | 475 CompleteWithBufferOrError(state->status, state->buffer, &state->result); |
| 464 return; | 476 return; |
| 465 } | 477 } |
| 466 | 478 |
| 467 if (state->status.IsError()) { | 479 if (state->status.IsError()) { |
| 468 CompleteWithError(state->status, &state->result); | 480 CompleteWithError(state->status, &state->result); |
| 469 } else { | 481 } else { |
| 470 state->result.completeWithJson( | 482 state->result.completeWithJson( |
| 471 reinterpret_cast<const char*>(vector_as_array(&state->buffer)), | 483 reinterpret_cast<const char*>(vector_as_array(&state->buffer)), |
| 472 static_cast<unsigned int>(state->buffer.size())); | 484 static_cast<unsigned int>(state->buffer.size())); |
| 473 } | 485 } |
| 474 } | 486 } |
| 475 | 487 |
| 476 void DoExportKey(scoped_ptr<ExportKeyState> passed_state) { | 488 void DoExportKey(scoped_ptr<ExportKeyState> passed_state) { |
| 477 ExportKeyState* state = passed_state.get(); | 489 ExportKeyState* state = passed_state.get(); |
| 478 if (state->cancelled()) | 490 if (state->cancelled()) |
| 479 return; | 491 return; |
| 480 state->status = | 492 state->status = |
| 481 webcrypto::ExportKey(state->format, state->key, &state->buffer); | 493 webcrypto::ExportKey(state->format, state->key, &state->buffer); |
| 482 state->origin_thread->PostTask( | 494 state->origin_thread->PostTask( |
| 483 FROM_HERE, base::Bind(DoExportKeyReply, Passed(&passed_state))); | 495 FROM_HERE, base::Bind(DoExportKeyReply, Passed(&passed_state))); |
| 484 } | 496 } |
| 485 | 497 |
| 486 void DoSignReply(scoped_ptr<SignState> state) { | 498 void DoSignReply(scoped_ptr<SignState> state) { |
| 499 if (state->cancelled()) |
| 500 return; |
| 487 CompleteWithBufferOrError(state->status, state->buffer, &state->result); | 501 CompleteWithBufferOrError(state->status, state->buffer, &state->result); |
| 488 } | 502 } |
| 489 | 503 |
| 490 void DoSign(scoped_ptr<SignState> passed_state) { | 504 void DoSign(scoped_ptr<SignState> passed_state) { |
| 491 SignState* state = passed_state.get(); | 505 SignState* state = passed_state.get(); |
| 492 if (state->cancelled()) | 506 if (state->cancelled()) |
| 493 return; | 507 return; |
| 494 state->status = | 508 state->status = |
| 495 webcrypto::Sign(state->algorithm, state->key, | 509 webcrypto::Sign(state->algorithm, state->key, |
| 496 webcrypto::CryptoData(state->data), &state->buffer); | 510 webcrypto::CryptoData(state->data), &state->buffer); |
| 497 | 511 |
| 498 state->origin_thread->PostTask( | 512 state->origin_thread->PostTask( |
| 499 FROM_HERE, base::Bind(DoSignReply, Passed(&passed_state))); | 513 FROM_HERE, base::Bind(DoSignReply, Passed(&passed_state))); |
| 500 } | 514 } |
| 501 | 515 |
| 502 void DoVerifyReply(scoped_ptr<VerifySignatureState> state) { | 516 void DoVerifyReply(scoped_ptr<VerifySignatureState> state) { |
| 517 if (state->cancelled()) |
| 518 return; |
| 503 if (state->status.IsError()) { | 519 if (state->status.IsError()) { |
| 504 CompleteWithError(state->status, &state->result); | 520 CompleteWithError(state->status, &state->result); |
| 505 } else { | 521 } else { |
| 506 state->result.completeWithBoolean(state->verify_result); | 522 state->result.completeWithBoolean(state->verify_result); |
| 507 } | 523 } |
| 508 } | 524 } |
| 509 | 525 |
| 510 void DoVerify(scoped_ptr<VerifySignatureState> passed_state) { | 526 void DoVerify(scoped_ptr<VerifySignatureState> passed_state) { |
| 511 VerifySignatureState* state = passed_state.get(); | 527 VerifySignatureState* state = passed_state.get(); |
| 512 if (state->cancelled()) | 528 if (state->cancelled()) |
| 513 return; | 529 return; |
| 514 state->status = webcrypto::Verify( | 530 state->status = webcrypto::Verify( |
| 515 state->algorithm, state->key, webcrypto::CryptoData(state->signature), | 531 state->algorithm, state->key, webcrypto::CryptoData(state->signature), |
| 516 webcrypto::CryptoData(state->data), &state->verify_result); | 532 webcrypto::CryptoData(state->data), &state->verify_result); |
| 517 | 533 |
| 518 state->origin_thread->PostTask( | 534 state->origin_thread->PostTask( |
| 519 FROM_HERE, base::Bind(DoVerifyReply, Passed(&passed_state))); | 535 FROM_HERE, base::Bind(DoVerifyReply, Passed(&passed_state))); |
| 520 } | 536 } |
| 521 | 537 |
| 522 void DoWrapKeyReply(scoped_ptr<WrapKeyState> state) { | 538 void DoWrapKeyReply(scoped_ptr<WrapKeyState> state) { |
| 539 if (state->cancelled()) |
| 540 return; |
| 523 CompleteWithBufferOrError(state->status, state->buffer, &state->result); | 541 CompleteWithBufferOrError(state->status, state->buffer, &state->result); |
| 524 } | 542 } |
| 525 | 543 |
| 526 void DoWrapKey(scoped_ptr<WrapKeyState> passed_state) { | 544 void DoWrapKey(scoped_ptr<WrapKeyState> passed_state) { |
| 527 WrapKeyState* state = passed_state.get(); | 545 WrapKeyState* state = passed_state.get(); |
| 528 if (state->cancelled()) | 546 if (state->cancelled()) |
| 529 return; | 547 return; |
| 530 state->status = | 548 state->status = |
| 531 webcrypto::WrapKey(state->format, state->key, state->wrapping_key, | 549 webcrypto::WrapKey(state->format, state->key, state->wrapping_key, |
| 532 state->wrap_algorithm, &state->buffer); | 550 state->wrap_algorithm, &state->buffer); |
| 533 | 551 |
| 534 state->origin_thread->PostTask( | 552 state->origin_thread->PostTask( |
| 535 FROM_HERE, base::Bind(DoWrapKeyReply, Passed(&passed_state))); | 553 FROM_HERE, base::Bind(DoWrapKeyReply, Passed(&passed_state))); |
| 536 } | 554 } |
| 537 | 555 |
| 538 void DoUnwrapKeyReply(scoped_ptr<UnwrapKeyState> state) { | 556 void DoUnwrapKeyReply(scoped_ptr<UnwrapKeyState> state) { |
| 557 if (state->cancelled()) |
| 558 return; |
| 539 CompleteWithKeyOrError(state->status, state->unwrapped_key, &state->result); | 559 CompleteWithKeyOrError(state->status, state->unwrapped_key, &state->result); |
| 540 } | 560 } |
| 541 | 561 |
| 542 void DoUnwrapKey(scoped_ptr<UnwrapKeyState> passed_state) { | 562 void DoUnwrapKey(scoped_ptr<UnwrapKeyState> passed_state) { |
| 543 UnwrapKeyState* state = passed_state.get(); | 563 UnwrapKeyState* state = passed_state.get(); |
| 544 if (state->cancelled()) | 564 if (state->cancelled()) |
| 545 return; | 565 return; |
| 546 state->status = webcrypto::UnwrapKey( | 566 state->status = webcrypto::UnwrapKey( |
| 547 state->format, webcrypto::CryptoData(state->wrapped_key), | 567 state->format, webcrypto::CryptoData(state->wrapped_key), |
| 548 state->wrapping_key, state->unwrap_algorithm, | 568 state->wrapping_key, state->unwrap_algorithm, |
| 549 state->unwrapped_key_algorithm, state->extractable, state->usages, | 569 state->unwrapped_key_algorithm, state->extractable, state->usages, |
| 550 &state->unwrapped_key); | 570 &state->unwrapped_key); |
| 551 | 571 |
| 552 state->origin_thread->PostTask( | 572 state->origin_thread->PostTask( |
| 553 FROM_HERE, base::Bind(DoUnwrapKeyReply, Passed(&passed_state))); | 573 FROM_HERE, base::Bind(DoUnwrapKeyReply, Passed(&passed_state))); |
| 554 } | 574 } |
| 555 | 575 |
| 556 void DoDeriveBitsReply(scoped_ptr<DeriveBitsState> state) { | 576 void DoDeriveBitsReply(scoped_ptr<DeriveBitsState> state) { |
| 577 if (state->cancelled()) |
| 578 return; |
| 557 CompleteWithBufferOrError(state->status, state->derived_bytes, | 579 CompleteWithBufferOrError(state->status, state->derived_bytes, |
| 558 &state->result); | 580 &state->result); |
| 559 } | 581 } |
| 560 | 582 |
| 561 void DoDeriveBits(scoped_ptr<DeriveBitsState> passed_state) { | 583 void DoDeriveBits(scoped_ptr<DeriveBitsState> passed_state) { |
| 562 DeriveBitsState* state = passed_state.get(); | 584 DeriveBitsState* state = passed_state.get(); |
| 563 if (state->cancelled()) | 585 if (state->cancelled()) |
| 564 return; | 586 return; |
| 565 state->status = | 587 state->status = |
| 566 webcrypto::DeriveBits(state->algorithm, state->base_key, | 588 webcrypto::DeriveBits(state->algorithm, state->base_key, |
| 567 state->length_bits, &state->derived_bytes); | 589 state->length_bits, &state->derived_bytes); |
| 568 state->origin_thread->PostTask( | 590 state->origin_thread->PostTask( |
| 569 FROM_HERE, base::Bind(DoDeriveBitsReply, Passed(&passed_state))); | 591 FROM_HERE, base::Bind(DoDeriveBitsReply, Passed(&passed_state))); |
| 570 } | 592 } |
| 571 | 593 |
| 572 void DoDeriveKeyReply(scoped_ptr<DeriveKeyState> state) { | 594 void DoDeriveKeyReply(scoped_ptr<DeriveKeyState> state) { |
| 595 if (state->cancelled()) |
| 596 return; |
| 573 CompleteWithKeyOrError(state->status, state->derived_key, &state->result); | 597 CompleteWithKeyOrError(state->status, state->derived_key, &state->result); |
| 574 } | 598 } |
| 575 | 599 |
| 576 void DoDeriveKey(scoped_ptr<DeriveKeyState> passed_state) { | 600 void DoDeriveKey(scoped_ptr<DeriveKeyState> passed_state) { |
| 577 DeriveKeyState* state = passed_state.get(); | 601 DeriveKeyState* state = passed_state.get(); |
| 578 if (state->cancelled()) | 602 if (state->cancelled()) |
| 579 return; | 603 return; |
| 580 state->status = webcrypto::DeriveKey( | 604 state->status = webcrypto::DeriveKey( |
| 581 state->algorithm, state->base_key, state->import_algorithm, | 605 state->algorithm, state->base_key, state->import_algorithm, |
| 582 state->key_length_algorithm, state->extractable, state->usages, | 606 state->key_length_algorithm, state->extractable, state->usages, |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 783 webcrypto::CryptoData(key_data, key_data_size), &key); | 807 webcrypto::CryptoData(key_data, key_data_size), &key); |
| 784 } | 808 } |
| 785 | 809 |
| 786 bool WebCryptoImpl::serializeKeyForClone( | 810 bool WebCryptoImpl::serializeKeyForClone( |
| 787 const blink::WebCryptoKey& key, | 811 const blink::WebCryptoKey& key, |
| 788 blink::WebVector<unsigned char>& key_data) { | 812 blink::WebVector<unsigned char>& key_data) { |
| 789 return webcrypto::SerializeKeyForClone(key, &key_data); | 813 return webcrypto::SerializeKeyForClone(key, &key_data); |
| 790 } | 814 } |
| 791 | 815 |
| 792 } // namespace webcrypto | 816 } // namespace webcrypto |
| OLD | NEW |