| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "bindings/modules/v8/serialization/V8ScriptValueSerializerForModules.h" | 5 #include "bindings/modules/v8/serialization/V8ScriptValueSerializerForModules.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ExceptionStatePlaceholder.h" | 7 #include "bindings/core/v8/ExceptionStatePlaceholder.h" |
| 8 #include "bindings/core/v8/ToV8.h" | 8 #include "bindings/core/v8/ToV8.h" |
| 9 #include "bindings/core/v8/V8ArrayBuffer.h" | 9 #include "bindings/core/v8/V8ArrayBuffer.h" |
| 10 #include "bindings/core/v8/V8BindingForTesting.h" | 10 #include "bindings/core/v8/V8BindingForTesting.h" |
| (...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 421 | 421 |
| 422 // Check that the keys have the same raw representation. | 422 // Check that the keys have the same raw representation. |
| 423 WebVector<uint8_t> keyRaw = | 423 WebVector<uint8_t> keyRaw = |
| 424 syncExportKey(scriptState, WebCryptoKeyFormatRaw, key->key()); | 424 syncExportKey(scriptState, WebCryptoKeyFormatRaw, key->key()); |
| 425 WebVector<uint8_t> newKeyRaw = | 425 WebVector<uint8_t> newKeyRaw = |
| 426 syncExportKey(scriptState, WebCryptoKeyFormatRaw, newKey->key()); | 426 syncExportKey(scriptState, WebCryptoKeyFormatRaw, newKey->key()); |
| 427 EXPECT_THAT(newKeyRaw, ElementsAreArray(keyRaw)); | 427 EXPECT_THAT(newKeyRaw, ElementsAreArray(keyRaw)); |
| 428 | 428 |
| 429 // Check that one can decrypt data encrypted with the other. | 429 // Check that one can decrypt data encrypted with the other. |
| 430 Vector<unsigned char> iv(16, 0); | 430 Vector<unsigned char> iv(16, 0); |
| 431 WebCryptoAlgorithm encryptAlgorithm(WebCryptoAlgorithmIdAesCbc, | 431 WebCryptoAlgorithm encryptAlgorithm( |
| 432 makeUnique<WebCryptoAesCbcParams>(iv)); | 432 WebCryptoAlgorithmIdAesCbc, WTF::makeUnique<WebCryptoAesCbcParams>(iv)); |
| 433 Vector<unsigned char> plaintext{1, 2, 3}; | 433 Vector<unsigned char> plaintext{1, 2, 3}; |
| 434 WebVector<uint8_t> ciphertext = | 434 WebVector<uint8_t> ciphertext = |
| 435 syncEncrypt(scriptState, encryptAlgorithm, key->key(), plaintext); | 435 syncEncrypt(scriptState, encryptAlgorithm, key->key(), plaintext); |
| 436 WebVector<uint8_t> newPlaintext = | 436 WebVector<uint8_t> newPlaintext = |
| 437 syncDecrypt(scriptState, encryptAlgorithm, newKey->key(), ciphertext); | 437 syncDecrypt(scriptState, encryptAlgorithm, newKey->key(), ciphertext); |
| 438 EXPECT_THAT(newPlaintext, ElementsAre(1, 2, 3)); | 438 EXPECT_THAT(newPlaintext, ElementsAre(1, 2, 3)); |
| 439 } | 439 } |
| 440 | 440 |
| 441 TEST(V8ScriptValueSerializerForModulesTest, DecodeCryptoKeyAES) { | 441 TEST(V8ScriptValueSerializerForModulesTest, DecodeCryptoKeyAES) { |
| 442 ScopedEnableV8BasedStructuredClone enable; | 442 ScopedEnableV8BasedStructuredClone enable; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 453 ASSERT_TRUE(V8CryptoKey::hasInstance(result, scope.isolate())); | 453 ASSERT_TRUE(V8CryptoKey::hasInstance(result, scope.isolate())); |
| 454 CryptoKey* newKey = V8CryptoKey::toImpl(result.As<v8::Object>()); | 454 CryptoKey* newKey = V8CryptoKey::toImpl(result.As<v8::Object>()); |
| 455 EXPECT_EQ("secret", newKey->type()); | 455 EXPECT_EQ("secret", newKey->type()); |
| 456 EXPECT_FALSE(newKey->extractable()); | 456 EXPECT_FALSE(newKey->extractable()); |
| 457 EXPECT_THAT(newKey->usages(), UnorderedElementsAre("decrypt")); | 457 EXPECT_THAT(newKey->usages(), UnorderedElementsAre("decrypt")); |
| 458 | 458 |
| 459 // Check that it can successfully decrypt data. | 459 // Check that it can successfully decrypt data. |
| 460 Vector<uint8_t> iv(16, 0); | 460 Vector<uint8_t> iv(16, 0); |
| 461 Vector<uint8_t> ciphertext{0x33, 0x26, 0xe7, 0x64, 0x11, 0x5e, 0xf4, 0x60, | 461 Vector<uint8_t> ciphertext{0x33, 0x26, 0xe7, 0x64, 0x11, 0x5e, 0xf4, 0x60, |
| 462 0x96, 0x08, 0x11, 0xaf, 0x65, 0x8b, 0x87, 0x04}; | 462 0x96, 0x08, 0x11, 0xaf, 0x65, 0x8b, 0x87, 0x04}; |
| 463 WebCryptoAlgorithm encryptAlgorithm(WebCryptoAlgorithmIdAesCbc, | 463 WebCryptoAlgorithm encryptAlgorithm( |
| 464 makeUnique<WebCryptoAesCbcParams>(iv)); | 464 WebCryptoAlgorithmIdAesCbc, WTF::makeUnique<WebCryptoAesCbcParams>(iv)); |
| 465 WebVector<uint8_t> plaintext = | 465 WebVector<uint8_t> plaintext = |
| 466 syncDecrypt(scriptState, encryptAlgorithm, newKey->key(), ciphertext); | 466 syncDecrypt(scriptState, encryptAlgorithm, newKey->key(), ciphertext); |
| 467 EXPECT_THAT(plaintext, ElementsAre(1, 2, 3)); | 467 EXPECT_THAT(plaintext, ElementsAre(1, 2, 3)); |
| 468 } | 468 } |
| 469 | 469 |
| 470 // HMAC-SHA256 uses HMAC key params. | 470 // HMAC-SHA256 uses HMAC key params. |
| 471 TEST(V8ScriptValueSerializerForModulesTest, RoundTripCryptoKeyHMAC) { | 471 TEST(V8ScriptValueSerializerForModulesTest, RoundTripCryptoKeyHMAC) { |
| 472 ScopedEnableV8BasedStructuredClone enable; | 472 ScopedEnableV8BasedStructuredClone enable; |
| 473 V8TestingScope scope; | 473 V8TestingScope scope; |
| 474 ScriptState* scriptState = scope.getScriptState(); | 474 ScriptState* scriptState = scope.getScriptState(); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 571 // Check that the keys have the same PKCS8 representation. | 571 // Check that the keys have the same PKCS8 representation. |
| 572 WebVector<uint8_t> keyRaw = | 572 WebVector<uint8_t> keyRaw = |
| 573 syncExportKey(scriptState, WebCryptoKeyFormatPkcs8, privateKey->key()); | 573 syncExportKey(scriptState, WebCryptoKeyFormatPkcs8, privateKey->key()); |
| 574 WebVector<uint8_t> newKeyRaw = | 574 WebVector<uint8_t> newKeyRaw = |
| 575 syncExportKey(scriptState, WebCryptoKeyFormatPkcs8, newPrivateKey->key()); | 575 syncExportKey(scriptState, WebCryptoKeyFormatPkcs8, newPrivateKey->key()); |
| 576 EXPECT_THAT(newKeyRaw, ElementsAreArray(keyRaw)); | 576 EXPECT_THAT(newKeyRaw, ElementsAreArray(keyRaw)); |
| 577 | 577 |
| 578 // Check that one can verify a message signed by the other. | 578 // Check that one can verify a message signed by the other. |
| 579 Vector<uint8_t> message{1, 2, 3}; | 579 Vector<uint8_t> message{1, 2, 3}; |
| 580 WebCryptoAlgorithm algorithm(WebCryptoAlgorithmIdRsaPss, | 580 WebCryptoAlgorithm algorithm(WebCryptoAlgorithmIdRsaPss, |
| 581 makeUnique<WebCryptoRsaPssParams>(16)); | 581 WTF::makeUnique<WebCryptoRsaPssParams>(16)); |
| 582 WebVector<uint8_t> signature = | 582 WebVector<uint8_t> signature = |
| 583 syncSign(scriptState, algorithm, newPrivateKey->key(), message); | 583 syncSign(scriptState, algorithm, newPrivateKey->key(), message); |
| 584 EXPECT_TRUE(syncVerifySignature(scriptState, algorithm, publicKey->key(), | 584 EXPECT_TRUE(syncVerifySignature(scriptState, algorithm, publicKey->key(), |
| 585 signature, message)); | 585 signature, message)); |
| 586 } | 586 } |
| 587 | 587 |
| 588 TEST(V8ScriptValueSerializerForModulesTest, DecodeCryptoKeyRSAHashed) { | 588 TEST(V8ScriptValueSerializerForModulesTest, DecodeCryptoKeyRSAHashed) { |
| 589 ScopedEnableV8BasedStructuredClone enable; | 589 ScopedEnableV8BasedStructuredClone enable; |
| 590 V8TestingScope scope; | 590 V8TestingScope scope; |
| 591 ScriptState* scriptState = scope.getScriptState(); | 591 ScriptState* scriptState = scope.getScriptState(); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 623 0x98, 0x1f, 0xf3, 0x9d, 0x32, 0x87, 0xdc, 0xbb, 0xb6, 0x3a, 0xa4, 0x6d, | 623 0x98, 0x1f, 0xf3, 0x9d, 0x32, 0x87, 0xdc, 0xbb, 0xb6, 0x3a, 0xa4, 0x6d, |
| 624 0xd4, 0xb5, 0x52, 0x83, 0x24, 0x02, 0xc7, 0x62, 0x1f, 0xb7, 0x27, 0x2b, | 624 0xd4, 0xb5, 0x52, 0x83, 0x24, 0x02, 0xc7, 0x62, 0x1f, 0xb7, 0x27, 0x2b, |
| 625 0x5a, 0x54, 0x59, 0x17, 0x81, 0x8a, 0xf5, 0x0c, 0x17, 0x01, 0x45, 0x3f, | 625 0x5a, 0x54, 0x59, 0x17, 0x81, 0x8a, 0xf5, 0x0c, 0x17, 0x01, 0x45, 0x3f, |
| 626 0x14, 0xf2, 0x3c, 0x27, 0x4d, 0xfa, 0xc0, 0x0a, 0x82, 0x4b, 0xb2, 0xf4, | 626 0x14, 0xf2, 0x3c, 0x27, 0x4d, 0xfa, 0xc0, 0x0a, 0x82, 0x4b, 0xb2, 0xf4, |
| 627 0x7b, 0x14, 0x1b, 0xd8, 0xbc, 0xe9, 0x2e, 0xd4, 0x55, 0x27, 0x62, 0x83, | 627 0x7b, 0x14, 0x1b, 0xd8, 0xbc, 0xe9, 0x2e, 0xd4, 0x55, 0x27, 0x62, 0x83, |
| 628 0x11, 0xed, 0xc2, 0x81, 0x7d, 0xa9, 0x4f, 0xe0, 0xef, 0x0e, 0xa5, 0xa5, | 628 0x11, 0xed, 0xc2, 0x81, 0x7d, 0xa9, 0x4f, 0xe0, 0xef, 0x0e, 0xa5, 0xa5, |
| 629 0xc6, 0x40, 0x46, 0xbf, 0x90, 0x19, 0xfc, 0xc8, 0x51, 0x0e, 0x0f, 0x62, | 629 0xc6, 0x40, 0x46, 0xbf, 0x90, 0x19, 0xfc, 0xc8, 0x51, 0x0e, 0x0f, 0x62, |
| 630 0xeb, 0x17, 0x68, 0x1f, 0xbd, 0xfa, 0xf7, 0xd6, 0x1f, 0xa4, 0x7c, 0x9e, | 630 0xeb, 0x17, 0x68, 0x1f, 0xbd, 0xfa, 0xf7, 0xd6, 0x1f, 0xa4, 0x7c, 0x9e, |
| 631 0x9e, 0xb1, 0x96, 0x8f, 0xe6, 0x5e, 0x89, 0x99}; | 631 0x9e, 0xb1, 0x96, 0x8f, 0xe6, 0x5e, 0x89, 0x99}; |
| 632 WebCryptoAlgorithm algorithm(WebCryptoAlgorithmIdRsaPss, | 632 WebCryptoAlgorithm algorithm(WebCryptoAlgorithmIdRsaPss, |
| 633 makeUnique<WebCryptoRsaPssParams>(16)); | 633 WTF::makeUnique<WebCryptoRsaPssParams>(16)); |
| 634 EXPECT_TRUE(syncVerifySignature(scriptState, algorithm, newPublicKey->key(), | 634 EXPECT_TRUE(syncVerifySignature(scriptState, algorithm, newPublicKey->key(), |
| 635 signature, message)); | 635 signature, message)); |
| 636 } | 636 } |
| 637 | 637 |
| 638 // ECDSA uses EC key params. | 638 // ECDSA uses EC key params. |
| 639 TEST(V8ScriptValueSerializerForModulesTest, RoundTripCryptoKeyEC) { | 639 TEST(V8ScriptValueSerializerForModulesTest, RoundTripCryptoKeyEC) { |
| 640 ScopedEnableV8BasedStructuredClone enable; | 640 ScopedEnableV8BasedStructuredClone enable; |
| 641 V8TestingScope scope; | 641 V8TestingScope scope; |
| 642 ScriptState* scriptState = scope.getScriptState(); | 642 ScriptState* scriptState = scope.getScriptState(); |
| 643 | 643 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 665 WebVector<uint8_t> keyRaw = | 665 WebVector<uint8_t> keyRaw = |
| 666 syncExportKey(scriptState, WebCryptoKeyFormatPkcs8, privateKey->key()); | 666 syncExportKey(scriptState, WebCryptoKeyFormatPkcs8, privateKey->key()); |
| 667 WebVector<uint8_t> newKeyRaw = | 667 WebVector<uint8_t> newKeyRaw = |
| 668 syncExportKey(scriptState, WebCryptoKeyFormatPkcs8, newPrivateKey->key()); | 668 syncExportKey(scriptState, WebCryptoKeyFormatPkcs8, newPrivateKey->key()); |
| 669 EXPECT_THAT(newKeyRaw, ElementsAreArray(keyRaw)); | 669 EXPECT_THAT(newKeyRaw, ElementsAreArray(keyRaw)); |
| 670 | 670 |
| 671 // Check that one can verify a message signed by the other. | 671 // Check that one can verify a message signed by the other. |
| 672 WebCryptoAlgorithm hash(WebCryptoAlgorithmIdSha256, nullptr); | 672 WebCryptoAlgorithm hash(WebCryptoAlgorithmIdSha256, nullptr); |
| 673 Vector<uint8_t> message{1, 2, 3}; | 673 Vector<uint8_t> message{1, 2, 3}; |
| 674 WebCryptoAlgorithm algorithm(WebCryptoAlgorithmIdEcdsa, | 674 WebCryptoAlgorithm algorithm(WebCryptoAlgorithmIdEcdsa, |
| 675 makeUnique<WebCryptoEcdsaParams>(hash)); | 675 WTF::makeUnique<WebCryptoEcdsaParams>(hash)); |
| 676 WebVector<uint8_t> signature = | 676 WebVector<uint8_t> signature = |
| 677 syncSign(scriptState, algorithm, newPrivateKey->key(), message); | 677 syncSign(scriptState, algorithm, newPrivateKey->key(), message); |
| 678 EXPECT_TRUE(syncVerifySignature(scriptState, algorithm, publicKey->key(), | 678 EXPECT_TRUE(syncVerifySignature(scriptState, algorithm, publicKey->key(), |
| 679 signature, message)); | 679 signature, message)); |
| 680 } | 680 } |
| 681 | 681 |
| 682 TEST(V8ScriptValueSerializerForModulesTest, DecodeCryptoKeyEC) { | 682 TEST(V8ScriptValueSerializerForModulesTest, DecodeCryptoKeyEC) { |
| 683 ScopedEnableV8BasedStructuredClone enable; | 683 ScopedEnableV8BasedStructuredClone enable; |
| 684 V8TestingScope scope; | 684 V8TestingScope scope; |
| 685 ScriptState* scriptState = scope.getScriptState(); | 685 ScriptState* scriptState = scope.getScriptState(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 707 Vector<uint8_t> message{1, 2, 3}; | 707 Vector<uint8_t> message{1, 2, 3}; |
| 708 Vector<uint8_t> signature{ | 708 Vector<uint8_t> signature{ |
| 709 0xee, 0x63, 0xa2, 0xa3, 0x87, 0x6c, 0x9f, 0xc5, 0x64, 0x12, 0x87, | 709 0xee, 0x63, 0xa2, 0xa3, 0x87, 0x6c, 0x9f, 0xc5, 0x64, 0x12, 0x87, |
| 710 0x0d, 0xc7, 0xff, 0x3c, 0xd2, 0x6c, 0x2b, 0x2c, 0x0b, 0x2b, 0x8d, | 710 0x0d, 0xc7, 0xff, 0x3c, 0xd2, 0x6c, 0x2b, 0x2c, 0x0b, 0x2b, 0x8d, |
| 711 0x3c, 0xe0, 0x3f, 0xd3, 0xfc, 0x28, 0xf0, 0xa1, 0x22, 0x69, 0x0a, | 711 0x3c, 0xe0, 0x3f, 0xd3, 0xfc, 0x28, 0xf0, 0xa1, 0x22, 0x69, 0x0a, |
| 712 0x33, 0x4d, 0x48, 0x97, 0xad, 0x67, 0xa9, 0x6e, 0x24, 0xe7, 0x31, | 712 0x33, 0x4d, 0x48, 0x97, 0xad, 0x67, 0xa9, 0x6e, 0x24, 0xe7, 0x31, |
| 713 0x09, 0xdb, 0xa8, 0x92, 0x48, 0x70, 0xa6, 0x6c, 0x46, 0x4d, 0x0b, | 713 0x09, 0xdb, 0xa8, 0x92, 0x48, 0x70, 0xa6, 0x6c, 0x46, 0x4d, 0x0b, |
| 714 0x83, 0x27, 0x37, 0x69, 0x4d, 0x32, 0x63, 0x1e, 0x82}; | 714 0x83, 0x27, 0x37, 0x69, 0x4d, 0x32, 0x63, 0x1e, 0x82}; |
| 715 WebCryptoAlgorithm hash(WebCryptoAlgorithmIdSha256, nullptr); | 715 WebCryptoAlgorithm hash(WebCryptoAlgorithmIdSha256, nullptr); |
| 716 WebCryptoAlgorithm algorithm(WebCryptoAlgorithmIdEcdsa, | 716 WebCryptoAlgorithm algorithm(WebCryptoAlgorithmIdEcdsa, |
| 717 makeUnique<WebCryptoEcdsaParams>(hash)); | 717 WTF::makeUnique<WebCryptoEcdsaParams>(hash)); |
| 718 EXPECT_TRUE(syncVerifySignature(scriptState, algorithm, newPublicKey->key(), | 718 EXPECT_TRUE(syncVerifySignature(scriptState, algorithm, newPublicKey->key(), |
| 719 signature, message)); | 719 signature, message)); |
| 720 } | 720 } |
| 721 | 721 |
| 722 TEST(V8ScriptValueSerializerForModulesTest, RoundTripCryptoKeyNoParams) { | 722 TEST(V8ScriptValueSerializerForModulesTest, RoundTripCryptoKeyNoParams) { |
| 723 ScopedEnableV8BasedStructuredClone enable; | 723 ScopedEnableV8BasedStructuredClone enable; |
| 724 V8TestingScope scope; | 724 V8TestingScope scope; |
| 725 ScriptState* scriptState = scope.getScriptState(); | 725 ScriptState* scriptState = scope.getScriptState(); |
| 726 | 726 |
| 727 // Import some data into a PBKDF2 state. | 727 // Import some data into a PBKDF2 state. |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 971 0x63, 0x6f, 0x6d, 0x2f, 0x70, 0x65, 0x72, 0x73, 0x69, | 971 0x63, 0x6f, 0x6d, 0x2f, 0x70, 0x65, 0x72, 0x73, 0x69, |
| 972 0x73, 0x74, 0x65, 0x6e, 0x74, 0x2f | 972 0x73, 0x74, 0x65, 0x6e, 0x74, 0x2f |
| 973 | 973 |
| 974 })) | 974 })) |
| 975 .deserialize() | 975 .deserialize() |
| 976 ->IsNull()); | 976 ->IsNull()); |
| 977 } | 977 } |
| 978 | 978 |
| 979 } // namespace | 979 } // namespace |
| 980 } // namespace blink | 980 } // namespace blink |
| OLD | NEW |