| 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/renderer/webcrypto/shared_crypto.h" | 5 #include "content/renderer/webcrypto/shared_crypto.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
| 14 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/values.h" | 19 #include "base/values.h" |
| 20 #include "content/public/common/content_paths.h" | 20 #include "content/public/common/content_paths.h" |
| 21 #include "content/public/renderer/content_renderer_client.h" | 21 #include "content/public/renderer/content_renderer_client.h" |
| 22 #include "content/renderer/renderer_webkitplatformsupport_impl.h" | 22 #include "content/renderer/renderer_webkitplatformsupport_impl.h" |
| 23 #include "content/renderer/webcrypto/crypto_data.h" | 23 #include "content/renderer/webcrypto/crypto_data.h" |
| 24 #include "content/renderer/webcrypto/webcrypto_util.h" | 24 #include "content/renderer/webcrypto/webcrypto_util.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | 26 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
| 27 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 27 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| 28 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM | |
| 29 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | 28 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
| 30 #endif | |
| 31 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 29 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 32 #include "third_party/WebKit/public/platform/WebCryptoKey.h" | 30 #include "third_party/WebKit/public/platform/WebCryptoKey.h" |
| 33 #include "third_party/re2/re2/re2.h" | 31 #include "third_party/re2/re2/re2.h" |
| 34 | 32 |
| 35 // The OpenSSL implementation of WebCrypto is less complete, so don't run all of | 33 // The OpenSSL implementation of WebCrypto is less complete, so don't run all of |
| 36 // the tests: http://crbug.com/267888 | 34 // the tests: http://crbug.com/267888 |
| 37 #if defined(USE_OPENSSL) | 35 #if defined(USE_OPENSSL) |
| 38 #define MAYBE(test_name) DISABLED_##test_name | 36 #define MAYBE(test_name) DISABLED_##test_name |
| 39 #else | 37 #else |
| 40 #define MAYBE(test_name) test_name | 38 #define MAYBE(test_name) test_name |
| (...skipping 30 matching lines...) Expand all Loading... |
| 71 blink::WebCryptoAlgorithm CreateRsaHashedKeyGenAlgorithm( | 69 blink::WebCryptoAlgorithm CreateRsaHashedKeyGenAlgorithm( |
| 72 blink::WebCryptoAlgorithmId algorithm_id, | 70 blink::WebCryptoAlgorithmId algorithm_id, |
| 73 const blink::WebCryptoAlgorithmId hash_id, | 71 const blink::WebCryptoAlgorithmId hash_id, |
| 74 unsigned int modulus_length, | 72 unsigned int modulus_length, |
| 75 const std::vector<uint8>& public_exponent) { | 73 const std::vector<uint8>& public_exponent) { |
| 76 DCHECK(algorithm_id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || | 74 DCHECK(algorithm_id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || |
| 77 algorithm_id == blink::WebCryptoAlgorithmIdRsaOaep); | 75 algorithm_id == blink::WebCryptoAlgorithmIdRsaOaep); |
| 78 DCHECK(IsHashAlgorithm(hash_id)); | 76 DCHECK(IsHashAlgorithm(hash_id)); |
| 79 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 77 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 80 algorithm_id, | 78 algorithm_id, |
| 81 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM | |
| 82 new blink::WebCryptoRsaHashedKeyGenParams( | 79 new blink::WebCryptoRsaHashedKeyGenParams( |
| 83 CreateAlgorithm(hash_id), | 80 CreateAlgorithm(hash_id), |
| 84 #else | |
| 85 new blink::WebCryptoRsaKeyGenParams( | |
| 86 #endif | |
| 87 modulus_length, | 81 modulus_length, |
| 88 webcrypto::Uint8VectorStart(public_exponent), | 82 webcrypto::Uint8VectorStart(public_exponent), |
| 89 public_exponent.size())); | 83 public_exponent.size())); |
| 90 } | 84 } |
| 91 | 85 |
| 92 // Creates an AES-CBC algorithm. | 86 // Creates an AES-CBC algorithm. |
| 93 blink::WebCryptoAlgorithm CreateAesCbcAlgorithm(const std::vector<uint8>& iv) { | 87 blink::WebCryptoAlgorithm CreateAesCbcAlgorithm(const std::vector<uint8>& iv) { |
| 94 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 88 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 95 blink::WebCryptoAlgorithmIdAesCbc, | 89 blink::WebCryptoAlgorithmIdAesCbc, |
| 96 new blink::WebCryptoAesCbcParams(Uint8VectorStart(iv), iv.size())); | 90 new blink::WebCryptoAesCbcParams(Uint8VectorStart(iv), iv.size())); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 115 // Creates an HMAC algorithm whose parameters struct is compatible with key | 109 // Creates an HMAC algorithm whose parameters struct is compatible with key |
| 116 // generation. It is an error to call this with a hash_id that is not a SHA*. | 110 // generation. It is an error to call this with a hash_id that is not a SHA*. |
| 117 // The key_length_bytes parameter is optional, with zero meaning unspecified. | 111 // The key_length_bytes parameter is optional, with zero meaning unspecified. |
| 118 blink::WebCryptoAlgorithm CreateHmacKeyGenAlgorithm( | 112 blink::WebCryptoAlgorithm CreateHmacKeyGenAlgorithm( |
| 119 blink::WebCryptoAlgorithmId hash_id, | 113 blink::WebCryptoAlgorithmId hash_id, |
| 120 unsigned int key_length_bytes) { | 114 unsigned int key_length_bytes) { |
| 121 DCHECK(IsHashAlgorithm(hash_id)); | 115 DCHECK(IsHashAlgorithm(hash_id)); |
| 122 // key_length_bytes == 0 means unspecified | 116 // key_length_bytes == 0 means unspecified |
| 123 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 117 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 124 blink::WebCryptoAlgorithmIdHmac, | 118 blink::WebCryptoAlgorithmIdHmac, |
| 125 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM | |
| 126 new blink::WebCryptoHmacKeyGenParams( | 119 new blink::WebCryptoHmacKeyGenParams( |
| 127 #else | |
| 128 new blink::WebCryptoHmacKeyParams( | |
| 129 #endif | |
| 130 CreateAlgorithm(hash_id), (key_length_bytes != 0), key_length_bytes)); | 120 CreateAlgorithm(hash_id), (key_length_bytes != 0), key_length_bytes)); |
| 131 } | 121 } |
| 132 | 122 |
| 133 // Returns a slightly modified version of the input vector. | 123 // Returns a slightly modified version of the input vector. |
| 134 // | 124 // |
| 135 // - For non-empty inputs a single bit is inverted. | 125 // - For non-empty inputs a single bit is inverted. |
| 136 // - For empty inputs, a byte is added. | 126 // - For empty inputs, a byte is added. |
| 137 std::vector<uint8> Corrupted(const std::vector<uint8>& input) { | 127 std::vector<uint8> Corrupted(const std::vector<uint8>& input) { |
| 138 std::vector<uint8> corrupted_data(input); | 128 std::vector<uint8> corrupted_data(input); |
| 139 if (corrupted_data.empty()) | 129 if (corrupted_data.empty()) |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 } | 301 } |
| 312 | 302 |
| 313 blink::WebCryptoAlgorithm CreateRsaHashedImportAlgorithm( | 303 blink::WebCryptoAlgorithm CreateRsaHashedImportAlgorithm( |
| 314 blink::WebCryptoAlgorithmId algorithm_id, | 304 blink::WebCryptoAlgorithmId algorithm_id, |
| 315 blink::WebCryptoAlgorithmId hash_id) { | 305 blink::WebCryptoAlgorithmId hash_id) { |
| 316 DCHECK(algorithm_id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || | 306 DCHECK(algorithm_id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || |
| 317 algorithm_id == blink::WebCryptoAlgorithmIdRsaOaep); | 307 algorithm_id == blink::WebCryptoAlgorithmIdRsaOaep); |
| 318 DCHECK(IsHashAlgorithm(hash_id)); | 308 DCHECK(IsHashAlgorithm(hash_id)); |
| 319 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 309 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 320 algorithm_id, | 310 algorithm_id, |
| 321 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM | |
| 322 new blink::WebCryptoRsaHashedImportParams(CreateAlgorithm(hash_id))); | 311 new blink::WebCryptoRsaHashedImportParams(CreateAlgorithm(hash_id))); |
| 323 #else | |
| 324 // Good enough for the tests. | |
| 325 new blink::WebCryptoRsaSsaParams(CreateAlgorithm(hash_id))); | |
| 326 #endif | |
| 327 } | 312 } |
| 328 | 313 |
| 329 // Determines if two ArrayBuffers have identical content. | 314 // Determines if two ArrayBuffers have identical content. |
| 330 bool ArrayBuffersEqual(const blink::WebArrayBuffer& a, | 315 bool ArrayBuffersEqual(const blink::WebArrayBuffer& a, |
| 331 const blink::WebArrayBuffer& b) { | 316 const blink::WebArrayBuffer& b) { |
| 332 return a.byteLength() == b.byteLength() && | 317 return a.byteLength() == b.byteLength() && |
| 333 memcmp(a.data(), b.data(), a.byteLength()) == 0; | 318 memcmp(a.data(), b.data(), a.byteLength()) == 0; |
| 334 } | 319 } |
| 335 | 320 |
| 336 // Given a vector of WebArrayBuffers, determines if there are any copies. | 321 // Given a vector of WebArrayBuffers, determines if there are any copies. |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 604 CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac); | 589 CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac); |
| 605 | 590 |
| 606 blink::WebCryptoAlgorithm importAlgorithm = | 591 blink::WebCryptoAlgorithm importAlgorithm = |
| 607 CreateHmacImportAlgorithm(test_hash.id()); | 592 CreateHmacImportAlgorithm(test_hash.id()); |
| 608 | 593 |
| 609 blink::WebCryptoKey key = ImportSecretKeyFromRaw( | 594 blink::WebCryptoKey key = ImportSecretKeyFromRaw( |
| 610 test_key, | 595 test_key, |
| 611 importAlgorithm, | 596 importAlgorithm, |
| 612 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify); | 597 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify); |
| 613 | 598 |
| 614 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM | |
| 615 EXPECT_EQ(test_hash.id(), key.algorithm().hmacParams()->hash().id()); | 599 EXPECT_EQ(test_hash.id(), key.algorithm().hmacParams()->hash().id()); |
| 616 #endif | |
| 617 | 600 |
| 618 // Verify exported raw key is identical to the imported data | 601 // Verify exported raw key is identical to the imported data |
| 619 blink::WebArrayBuffer raw_key; | 602 blink::WebArrayBuffer raw_key; |
| 620 EXPECT_STATUS_SUCCESS( | 603 EXPECT_STATUS_SUCCESS( |
| 621 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 604 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
| 622 ExpectArrayBufferMatches(test_key, raw_key); | 605 ExpectArrayBufferMatches(test_key, raw_key); |
| 623 | 606 |
| 624 blink::WebArrayBuffer output; | 607 blink::WebArrayBuffer output; |
| 625 | 608 |
| 626 ASSERT_STATUS_SUCCESS( | 609 ASSERT_STATUS_SUCCESS( |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 765 std::vector<uint8> test_plain_text = | 748 std::vector<uint8> test_plain_text = |
| 766 GetBytesFromHexString(test, "plain_text"); | 749 GetBytesFromHexString(test, "plain_text"); |
| 767 std::vector<uint8> test_cipher_text = | 750 std::vector<uint8> test_cipher_text = |
| 768 GetBytesFromHexString(test, "cipher_text"); | 751 GetBytesFromHexString(test, "cipher_text"); |
| 769 | 752 |
| 770 blink::WebCryptoKey key = ImportSecretKeyFromRaw( | 753 blink::WebCryptoKey key = ImportSecretKeyFromRaw( |
| 771 test_key, | 754 test_key, |
| 772 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), | 755 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), |
| 773 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); | 756 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); |
| 774 | 757 |
| 775 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM | |
| 776 EXPECT_EQ(test_key.size() * 8, key.algorithm().aesParams()->lengthBits()); | 758 EXPECT_EQ(test_key.size() * 8, key.algorithm().aesParams()->lengthBits()); |
| 777 #endif | |
| 778 | 759 |
| 779 // Verify exported raw key is identical to the imported data | 760 // Verify exported raw key is identical to the imported data |
| 780 blink::WebArrayBuffer raw_key; | 761 blink::WebArrayBuffer raw_key; |
| 781 EXPECT_STATUS_SUCCESS( | 762 EXPECT_STATUS_SUCCESS( |
| 782 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 763 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
| 783 ExpectArrayBufferMatches(test_key, raw_key); | 764 ExpectArrayBufferMatches(test_key, raw_key); |
| 784 | 765 |
| 785 blink::WebArrayBuffer output; | 766 blink::WebArrayBuffer output; |
| 786 | 767 |
| 787 // Test encryption. | 768 // Test encryption. |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 843 for (size_t i = 0; i < algorithm.size(); ++i) { | 824 for (size_t i = 0; i < algorithm.size(); ++i) { |
| 844 SCOPED_TRACE(i); | 825 SCOPED_TRACE(i); |
| 845 // Generate a small sample of keys. | 826 // Generate a small sample of keys. |
| 846 keys.clear(); | 827 keys.clear(); |
| 847 for (int j = 0; j < 16; ++j) { | 828 for (int j = 0; j < 16; ++j) { |
| 848 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm[i], true, 0, &key)); | 829 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm[i], true, 0, &key)); |
| 849 EXPECT_TRUE(key.handle()); | 830 EXPECT_TRUE(key.handle()); |
| 850 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | 831 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
| 851 ASSERT_STATUS_SUCCESS( | 832 ASSERT_STATUS_SUCCESS( |
| 852 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_bytes)); | 833 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_bytes)); |
| 853 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM | |
| 854 EXPECT_EQ(key_bytes.byteLength() * 8, | 834 EXPECT_EQ(key_bytes.byteLength() * 8, |
| 855 key.algorithm().aesParams()->lengthBits()); | 835 key.algorithm().aesParams()->lengthBits()); |
| 856 #endif | |
| 857 keys.push_back(key_bytes); | 836 keys.push_back(key_bytes); |
| 858 } | 837 } |
| 859 // Ensure all entries in the key sample set are unique. This is a simplistic | 838 // Ensure all entries in the key sample set are unique. This is a simplistic |
| 860 // estimate of whether the generated keys appear random. | 839 // estimate of whether the generated keys appear random. |
| 861 EXPECT_FALSE(CopiesExist(keys)); | 840 EXPECT_FALSE(CopiesExist(keys)); |
| 862 } | 841 } |
| 863 } | 842 } |
| 864 | 843 |
| 865 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyAesBadLength)) { | 844 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyAesBadLength)) { |
| 866 const unsigned short kKeyLen[] = {0, 127, 257}; | 845 const unsigned short kKeyLen[] = {0, 127, 257}; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 885 for (int i = 0; i < 16; ++i) { | 864 for (int i = 0; i < 16; ++i) { |
| 886 blink::WebArrayBuffer key_bytes; | 865 blink::WebArrayBuffer key_bytes; |
| 887 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 866 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 888 blink::WebCryptoAlgorithm algorithm = | 867 blink::WebCryptoAlgorithm algorithm = |
| 889 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 64); | 868 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 64); |
| 890 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm, true, 0, &key)); | 869 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm, true, 0, &key)); |
| 891 EXPECT_FALSE(key.isNull()); | 870 EXPECT_FALSE(key.isNull()); |
| 892 EXPECT_TRUE(key.handle()); | 871 EXPECT_TRUE(key.handle()); |
| 893 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | 872 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
| 894 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); | 873 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); |
| 895 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM | |
| 896 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, | 874 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, |
| 897 key.algorithm().hmacParams()->hash().id()); | 875 key.algorithm().hmacParams()->hash().id()); |
| 898 #endif | |
| 899 | 876 |
| 900 blink::WebArrayBuffer raw_key; | 877 blink::WebArrayBuffer raw_key; |
| 901 ASSERT_STATUS_SUCCESS( | 878 ASSERT_STATUS_SUCCESS( |
| 902 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 879 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
| 903 EXPECT_EQ(64U, raw_key.byteLength()); | 880 EXPECT_EQ(64U, raw_key.byteLength()); |
| 904 keys.push_back(raw_key); | 881 keys.push_back(raw_key); |
| 905 } | 882 } |
| 906 // Ensure all entries in the key sample set are unique. This is a simplistic | 883 // Ensure all entries in the key sample set are unique. This is a simplistic |
| 907 // estimate of whether the generated keys appear random. | 884 // estimate of whether the generated keys appear random. |
| 908 EXPECT_FALSE(CopiesExist(keys)); | 885 EXPECT_FALSE(CopiesExist(keys)); |
| 909 } | 886 } |
| 910 | 887 |
| 911 // If the key length is not provided, then the block size is used. | 888 // If the key length is not provided, then the block size is used. |
| 912 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyHmacNoLength)) { | 889 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyHmacNoLength)) { |
| 913 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 890 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 914 blink::WebCryptoAlgorithm algorithm = | 891 blink::WebCryptoAlgorithm algorithm = |
| 915 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0); | 892 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0); |
| 916 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm, true, 0, &key)); | 893 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm, true, 0, &key)); |
| 917 EXPECT_TRUE(key.handle()); | 894 EXPECT_TRUE(key.handle()); |
| 918 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | 895 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
| 919 blink::WebArrayBuffer raw_key; | 896 blink::WebArrayBuffer raw_key; |
| 920 ASSERT_STATUS_SUCCESS(ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 897 ASSERT_STATUS_SUCCESS(ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
| 921 EXPECT_EQ(64U, raw_key.byteLength()); | 898 EXPECT_EQ(64U, raw_key.byteLength()); |
| 922 | 899 |
| 923 // The block size for HMAC SHA-512 is larger. | 900 // The block size for HMAC SHA-512 is larger. |
| 924 algorithm = CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha512, 0); | 901 algorithm = CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha512, 0); |
| 925 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm, true, 0, &key)); | 902 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm, true, 0, &key)); |
| 926 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM | |
| 927 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha512, | 903 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha512, |
| 928 key.algorithm().hmacParams()->hash().id()); | 904 key.algorithm().hmacParams()->hash().id()); |
| 929 #endif | |
| 930 ASSERT_STATUS_SUCCESS(ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 905 ASSERT_STATUS_SUCCESS(ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
| 931 EXPECT_EQ(128U, raw_key.byteLength()); | 906 EXPECT_EQ(128U, raw_key.byteLength()); |
| 932 } | 907 } |
| 933 | 908 |
| 934 TEST_F(SharedCryptoTest, MAYBE(ImportSecretKeyNoAlgorithm)) { | 909 TEST_F(SharedCryptoTest, MAYBE(ImportSecretKeyNoAlgorithm)) { |
| 935 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 910 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 936 | 911 |
| 937 // This fails because the algorithm is null. | 912 // This fails because the algorithm is null. |
| 938 EXPECT_STATUS(Status::ErrorMissingAlgorithmImportRawKey(), | 913 EXPECT_STATUS(Status::ErrorMissingAlgorithmImportRawKey(), |
| 939 ImportKey(blink::WebCryptoKeyFormatRaw, | 914 ImportKey(blink::WebCryptoKeyFormatRaw, |
| (...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1275 base::DictionaryValue dict; | 1250 base::DictionaryValue dict; |
| 1276 dict.SetString("kty", "oct"); | 1251 dict.SetString("kty", "oct"); |
| 1277 dict.SetString("alg", "HS256"); | 1252 dict.SetString("alg", "HS256"); |
| 1278 dict.SetString("use", "sig"); | 1253 dict.SetString("use", "sig"); |
| 1279 dict.SetBoolean("extractable", false); | 1254 dict.SetBoolean("extractable", false); |
| 1280 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg"); | 1255 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg"); |
| 1281 | 1256 |
| 1282 ASSERT_STATUS_SUCCESS( | 1257 ASSERT_STATUS_SUCCESS( |
| 1283 ImportKeyJwkFromDict(dict, algorithm, extractable, usage_mask, &key)); | 1258 ImportKeyJwkFromDict(dict, algorithm, extractable, usage_mask, &key)); |
| 1284 | 1259 |
| 1285 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM | |
| 1286 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256, | 1260 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256, |
| 1287 key.algorithm().hmacParams()->hash().id()); | 1261 key.algorithm().hmacParams()->hash().id()); |
| 1288 #endif | |
| 1289 | 1262 |
| 1290 const std::vector<uint8> message_raw = HexStringToBytes( | 1263 const std::vector<uint8> message_raw = HexStringToBytes( |
| 1291 "b1689c2591eaf3c9e66070f8a77954ffb81749f1b00346f9dfe0b2ee905dcc288baf4a" | 1264 "b1689c2591eaf3c9e66070f8a77954ffb81749f1b00346f9dfe0b2ee905dcc288baf4a" |
| 1292 "92de3f4001dd9f44c468c3d07d6c6ee82faceafc97c2fc0fc0601719d2dcd0aa2aec92" | 1265 "92de3f4001dd9f44c468c3d07d6c6ee82faceafc97c2fc0fc0601719d2dcd0aa2aec92" |
| 1293 "d1b0ae933c65eb06a03c9c935c2bad0459810241347ab87e9f11adb30415424c6c7f5f" | 1266 "d1b0ae933c65eb06a03c9c935c2bad0459810241347ab87e9f11adb30415424c6c7f5f" |
| 1294 "22a003b8ab8de54f6ded0e3ab9245fa79568451dfa258e"); | 1267 "22a003b8ab8de54f6ded0e3ab9245fa79568451dfa258e"); |
| 1295 | 1268 |
| 1296 blink::WebArrayBuffer output; | 1269 blink::WebArrayBuffer output; |
| 1297 | 1270 |
| 1298 ASSERT_STATUS_SUCCESS(Sign(CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac), | 1271 ASSERT_STATUS_SUCCESS(Sign(CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac), |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1315 ImportKey(blink::WebCryptoKeyFormatSpki, | 1288 ImportKey(blink::WebCryptoKeyFormatSpki, |
| 1316 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)), | 1289 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)), |
| 1317 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5), | 1290 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5), |
| 1318 true, | 1291 true, |
| 1319 blink::WebCryptoKeyUsageEncrypt, | 1292 blink::WebCryptoKeyUsageEncrypt, |
| 1320 &key)); | 1293 &key)); |
| 1321 EXPECT_TRUE(key.handle()); | 1294 EXPECT_TRUE(key.handle()); |
| 1322 EXPECT_EQ(blink::WebCryptoKeyTypePublic, key.type()); | 1295 EXPECT_EQ(blink::WebCryptoKeyTypePublic, key.type()); |
| 1323 EXPECT_TRUE(key.extractable()); | 1296 EXPECT_TRUE(key.extractable()); |
| 1324 EXPECT_EQ(blink::WebCryptoKeyUsageEncrypt, key.usages()); | 1297 EXPECT_EQ(blink::WebCryptoKeyUsageEncrypt, key.usages()); |
| 1325 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM | |
| 1326 EXPECT_EQ(kModulusLength, key.algorithm().rsaParams()->modulusLengthBits()); | 1298 EXPECT_EQ(kModulusLength, key.algorithm().rsaParams()->modulusLengthBits()); |
| 1327 ExpectCryptoDataMatchesHex( | 1299 ExpectCryptoDataMatchesHex( |
| 1328 "010001", CryptoData(key.algorithm().rsaParams()->publicExponent())); | 1300 "010001", CryptoData(key.algorithm().rsaParams()->publicExponent())); |
| 1329 #endif | |
| 1330 | 1301 |
| 1331 // Failing case: Empty SPKI data | 1302 // Failing case: Empty SPKI data |
| 1332 EXPECT_STATUS(Status::ErrorImportEmptyKeyData(), | 1303 EXPECT_STATUS(Status::ErrorImportEmptyKeyData(), |
| 1333 ImportKey(blink::WebCryptoKeyFormatSpki, | 1304 ImportKey(blink::WebCryptoKeyFormatSpki, |
| 1334 CryptoData(std::vector<uint8>()), | 1305 CryptoData(std::vector<uint8>()), |
| 1335 blink::WebCryptoAlgorithm::createNull(), | 1306 blink::WebCryptoAlgorithm::createNull(), |
| 1336 true, | 1307 true, |
| 1337 blink::WebCryptoKeyUsageEncrypt, | 1308 blink::WebCryptoKeyUsageEncrypt, |
| 1338 &key)); | 1309 &key)); |
| 1339 | 1310 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1400 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)), | 1371 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)), |
| 1401 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 1372 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 1402 blink::WebCryptoAlgorithmIdSha1), | 1373 blink::WebCryptoAlgorithmIdSha1), |
| 1403 true, | 1374 true, |
| 1404 blink::WebCryptoKeyUsageSign, | 1375 blink::WebCryptoKeyUsageSign, |
| 1405 &key)); | 1376 &key)); |
| 1406 EXPECT_TRUE(key.handle()); | 1377 EXPECT_TRUE(key.handle()); |
| 1407 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, key.type()); | 1378 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, key.type()); |
| 1408 EXPECT_TRUE(key.extractable()); | 1379 EXPECT_TRUE(key.extractable()); |
| 1409 EXPECT_EQ(blink::WebCryptoKeyUsageSign, key.usages()); | 1380 EXPECT_EQ(blink::WebCryptoKeyUsageSign, key.usages()); |
| 1410 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM | |
| 1411 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, | 1381 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, |
| 1412 key.algorithm().rsaHashedParams()->hash().id()); | 1382 key.algorithm().rsaHashedParams()->hash().id()); |
| 1413 EXPECT_EQ(kModulusLength, | 1383 EXPECT_EQ(kModulusLength, |
| 1414 key.algorithm().rsaHashedParams()->modulusLengthBits()); | 1384 key.algorithm().rsaHashedParams()->modulusLengthBits()); |
| 1415 ExpectCryptoDataMatchesHex( | 1385 ExpectCryptoDataMatchesHex( |
| 1416 "010001", | 1386 "010001", |
| 1417 CryptoData(key.algorithm().rsaHashedParams()->publicExponent())); | 1387 CryptoData(key.algorithm().rsaHashedParams()->publicExponent())); |
| 1418 #endif | |
| 1419 | 1388 |
| 1420 // Failing case: Empty PKCS#8 data | 1389 // Failing case: Empty PKCS#8 data |
| 1421 EXPECT_STATUS(Status::ErrorImportEmptyKeyData(), | 1390 EXPECT_STATUS(Status::ErrorImportEmptyKeyData(), |
| 1422 ImportKey(blink::WebCryptoKeyFormatPkcs8, | 1391 ImportKey(blink::WebCryptoKeyFormatPkcs8, |
| 1423 CryptoData(std::vector<uint8>()), | 1392 CryptoData(std::vector<uint8>()), |
| 1424 blink::WebCryptoAlgorithm::createNull(), | 1393 blink::WebCryptoAlgorithm::createNull(), |
| 1425 true, | 1394 true, |
| 1426 blink::WebCryptoKeyUsageSign, | 1395 blink::WebCryptoKeyUsageSign, |
| 1427 &key)); | 1396 &key)); |
| 1428 | 1397 |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1545 algorithm = CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaOaep, | 1514 algorithm = CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaOaep, |
| 1546 blink::WebCryptoAlgorithmIdSha256, | 1515 blink::WebCryptoAlgorithmIdSha256, |
| 1547 modulus_length, | 1516 modulus_length, |
| 1548 public_exponent); | 1517 public_exponent); |
| 1549 EXPECT_STATUS_SUCCESS(GenerateKeyPair( | 1518 EXPECT_STATUS_SUCCESS(GenerateKeyPair( |
| 1550 algorithm, extractable, usage_mask, &public_key, &private_key)); | 1519 algorithm, extractable, usage_mask, &public_key, &private_key)); |
| 1551 EXPECT_FALSE(public_key.isNull()); | 1520 EXPECT_FALSE(public_key.isNull()); |
| 1552 EXPECT_FALSE(private_key.isNull()); | 1521 EXPECT_FALSE(private_key.isNull()); |
| 1553 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); | 1522 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); |
| 1554 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); | 1523 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); |
| 1555 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM | |
| 1556 EXPECT_EQ(modulus_length, | 1524 EXPECT_EQ(modulus_length, |
| 1557 public_key.algorithm().rsaHashedParams()->modulusLengthBits()); | 1525 public_key.algorithm().rsaHashedParams()->modulusLengthBits()); |
| 1558 EXPECT_EQ(modulus_length, | 1526 EXPECT_EQ(modulus_length, |
| 1559 private_key.algorithm().rsaHashedParams()->modulusLengthBits()); | 1527 private_key.algorithm().rsaHashedParams()->modulusLengthBits()); |
| 1560 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256, | 1528 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256, |
| 1561 public_key.algorithm().rsaHashedParams()->hash().id()); | 1529 public_key.algorithm().rsaHashedParams()->hash().id()); |
| 1562 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256, | 1530 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256, |
| 1563 private_key.algorithm().rsaHashedParams()->hash().id()); | 1531 private_key.algorithm().rsaHashedParams()->hash().id()); |
| 1564 #endif | |
| 1565 EXPECT_TRUE(public_key.extractable()); | 1532 EXPECT_TRUE(public_key.extractable()); |
| 1566 EXPECT_EQ(extractable, private_key.extractable()); | 1533 EXPECT_EQ(extractable, private_key.extractable()); |
| 1567 EXPECT_EQ(usage_mask, public_key.usages()); | 1534 EXPECT_EQ(usage_mask, public_key.usages()); |
| 1568 EXPECT_EQ(usage_mask, private_key.usages()); | 1535 EXPECT_EQ(usage_mask, private_key.usages()); |
| 1569 | 1536 |
| 1570 // Successful WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 key generation. | 1537 // Successful WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 key generation. |
| 1571 algorithm = | 1538 algorithm = |
| 1572 CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 1539 CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 1573 blink::WebCryptoAlgorithmIdSha1, | 1540 blink::WebCryptoAlgorithmIdSha1, |
| 1574 modulus_length, | 1541 modulus_length, |
| 1575 public_exponent); | 1542 public_exponent); |
| 1576 EXPECT_STATUS_SUCCESS( | 1543 EXPECT_STATUS_SUCCESS( |
| 1577 GenerateKeyPair(algorithm, false, usage_mask, &public_key, &private_key)); | 1544 GenerateKeyPair(algorithm, false, usage_mask, &public_key, &private_key)); |
| 1578 EXPECT_FALSE(public_key.isNull()); | 1545 EXPECT_FALSE(public_key.isNull()); |
| 1579 EXPECT_FALSE(private_key.isNull()); | 1546 EXPECT_FALSE(private_key.isNull()); |
| 1580 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); | 1547 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); |
| 1581 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); | 1548 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); |
| 1582 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM | |
| 1583 EXPECT_EQ(modulus_length, | 1549 EXPECT_EQ(modulus_length, |
| 1584 public_key.algorithm().rsaHashedParams()->modulusLengthBits()); | 1550 public_key.algorithm().rsaHashedParams()->modulusLengthBits()); |
| 1585 EXPECT_EQ(modulus_length, | 1551 EXPECT_EQ(modulus_length, |
| 1586 private_key.algorithm().rsaHashedParams()->modulusLengthBits()); | 1552 private_key.algorithm().rsaHashedParams()->modulusLengthBits()); |
| 1587 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, | 1553 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, |
| 1588 public_key.algorithm().rsaHashedParams()->hash().id()); | 1554 public_key.algorithm().rsaHashedParams()->hash().id()); |
| 1589 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, | 1555 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, |
| 1590 private_key.algorithm().rsaHashedParams()->hash().id()); | 1556 private_key.algorithm().rsaHashedParams()->hash().id()); |
| 1591 #endif | |
| 1592 // Even though "extractable" was set to false, the public key remains | 1557 // Even though "extractable" was set to false, the public key remains |
| 1593 // extractable. | 1558 // extractable. |
| 1594 EXPECT_TRUE(public_key.extractable()); | 1559 EXPECT_TRUE(public_key.extractable()); |
| 1595 EXPECT_FALSE(private_key.extractable()); | 1560 EXPECT_FALSE(private_key.extractable()); |
| 1596 EXPECT_EQ(usage_mask, public_key.usages()); | 1561 EXPECT_EQ(usage_mask, public_key.usages()); |
| 1597 EXPECT_EQ(usage_mask, private_key.usages()); | 1562 EXPECT_EQ(usage_mask, private_key.usages()); |
| 1598 | 1563 |
| 1599 // Exporting a private key as SPKI format doesn't make sense. However this | 1564 // Exporting a private key as SPKI format doesn't make sense. However this |
| 1600 // will first fail because the key is not extractable. | 1565 // will first fail because the key is not extractable. |
| 1601 blink::WebArrayBuffer output; | 1566 blink::WebArrayBuffer output; |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1794 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | 1759 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); |
| 1795 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); | 1760 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); |
| 1796 ImportRsaKeyPair(HexStringToBytes(kPublicKeySpkiDerHex), | 1761 ImportRsaKeyPair(HexStringToBytes(kPublicKeySpkiDerHex), |
| 1797 HexStringToBytes(kPrivateKeyPkcs8DerHex), | 1762 HexStringToBytes(kPrivateKeyPkcs8DerHex), |
| 1798 importAlgorithm, | 1763 importAlgorithm, |
| 1799 false, | 1764 false, |
| 1800 usage_mask, | 1765 usage_mask, |
| 1801 &public_key, | 1766 &public_key, |
| 1802 &private_key); | 1767 &private_key); |
| 1803 | 1768 |
| 1804 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM | |
| 1805 blink::WebCryptoAlgorithm algorithm = | 1769 blink::WebCryptoAlgorithm algorithm = |
| 1806 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5); | 1770 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5); |
| 1807 #else | |
| 1808 blink::WebCryptoAlgorithm algorithm = importAlgorithm; | |
| 1809 #endif | |
| 1810 | 1771 |
| 1811 blink::WebArrayBuffer signature; | 1772 blink::WebArrayBuffer signature; |
| 1812 bool signature_match; | 1773 bool signature_match; |
| 1813 | 1774 |
| 1814 // Compute a signature. | 1775 // Compute a signature. |
| 1815 const std::vector<uint8> data = HexStringToBytes("010203040506070809"); | 1776 const std::vector<uint8> data = HexStringToBytes("010203040506070809"); |
| 1816 ASSERT_STATUS_SUCCESS( | 1777 ASSERT_STATUS_SUCCESS( |
| 1817 Sign(algorithm, private_key, CryptoData(data), &signature)); | 1778 Sign(algorithm, private_key, CryptoData(data), &signature)); |
| 1818 | 1779 |
| 1819 // Ensure truncated signature does not verify by passing one less byte. | 1780 // Ensure truncated signature does not verify by passing one less byte. |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1872 | 1833 |
| 1873 EXPECT_STATUS(Status::ErrorUnexpected(), | 1834 EXPECT_STATUS(Status::ErrorUnexpected(), |
| 1874 Sign(algorithm, private_key, CryptoData(data), &signature)); | 1835 Sign(algorithm, private_key, CryptoData(data), &signature)); |
| 1875 EXPECT_STATUS(Status::ErrorUnexpected(), | 1836 EXPECT_STATUS(Status::ErrorUnexpected(), |
| 1876 VerifySignature(algorithm, | 1837 VerifySignature(algorithm, |
| 1877 public_key, | 1838 public_key, |
| 1878 CryptoData(signature), | 1839 CryptoData(signature), |
| 1879 CryptoData(data), | 1840 CryptoData(data), |
| 1880 &signature_match)); | 1841 &signature_match)); |
| 1881 | 1842 |
| 1882 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM | |
| 1883 // Some crypto libraries (NSS) can automatically select the RSA SSA inner hash | 1843 // Some crypto libraries (NSS) can automatically select the RSA SSA inner hash |
| 1884 // based solely on the contents of the input signature data. In the Web Crypto | 1844 // based solely on the contents of the input signature data. In the Web Crypto |
| 1885 // implementation, the inner hash should be specified uniquely by the key | 1845 // implementation, the inner hash should be specified uniquely by the key |
| 1886 // algorithm parameter. To validate this behavior, call Verify with a computed | 1846 // algorithm parameter. To validate this behavior, call Verify with a computed |
| 1887 // signature that used one hash type (SHA-1), but pass in a key with a | 1847 // signature that used one hash type (SHA-1), but pass in a key with a |
| 1888 // different inner hash type (SHA-256). If the hash type is determined by the | 1848 // different inner hash type (SHA-256). If the hash type is determined by the |
| 1889 // signature itself (undesired), the verify will pass, while if the hash type | 1849 // signature itself (undesired), the verify will pass, while if the hash type |
| 1890 // is specified by the key algorithm (desired), the verify will fail. | 1850 // is specified by the key algorithm (desired), the verify will fail. |
| 1891 | 1851 |
| 1892 // Compute a signature using SHA-1 as the inner hash. | 1852 // Compute a signature using SHA-1 as the inner hash. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 1917 public_key_256.algorithm().rsaHashedParams()->hash().id()); | 1877 public_key_256.algorithm().rsaHashedParams()->hash().id()); |
| 1918 | 1878 |
| 1919 bool is_match; | 1879 bool is_match; |
| 1920 EXPECT_STATUS_SUCCESS(VerifySignature( | 1880 EXPECT_STATUS_SUCCESS(VerifySignature( |
| 1921 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5), | 1881 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5), |
| 1922 public_key_256, | 1882 public_key_256, |
| 1923 CryptoData(signature), | 1883 CryptoData(signature), |
| 1924 CryptoData(data), | 1884 CryptoData(data), |
| 1925 &is_match)); | 1885 &is_match)); |
| 1926 EXPECT_FALSE(is_match); | 1886 EXPECT_FALSE(is_match); |
| 1927 #endif | |
| 1928 } | 1887 } |
| 1929 | 1888 |
| 1930 TEST_F(SharedCryptoTest, MAYBE(RsaSignVerifyKnownAnswer)) { | 1889 TEST_F(SharedCryptoTest, MAYBE(RsaSignVerifyKnownAnswer)) { |
| 1931 scoped_ptr<base::ListValue> tests; | 1890 scoped_ptr<base::ListValue> tests; |
| 1932 ASSERT_TRUE(ReadJsonTestFileToList("pkcs1v15_sign.json", &tests)); | 1891 ASSERT_TRUE(ReadJsonTestFileToList("pkcs1v15_sign.json", &tests)); |
| 1933 | 1892 |
| 1934 // Import the key pair. | 1893 // Import the key pair. |
| 1935 blink::WebCryptoAlgorithm importAlgorithm = | 1894 blink::WebCryptoAlgorithm importAlgorithm = |
| 1936 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 1895 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 1937 blink::WebCryptoAlgorithmIdSha1); | 1896 blink::WebCryptoAlgorithmIdSha1); |
| 1938 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | 1897 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); |
| 1939 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); | 1898 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); |
| 1940 ImportRsaKeyPair( | 1899 ImportRsaKeyPair( |
| 1941 HexStringToBytes(kPublicKeySpkiDerHex), | 1900 HexStringToBytes(kPublicKeySpkiDerHex), |
| 1942 HexStringToBytes(kPrivateKeyPkcs8DerHex), | 1901 HexStringToBytes(kPrivateKeyPkcs8DerHex), |
| 1943 importAlgorithm, | 1902 importAlgorithm, |
| 1944 false, | 1903 false, |
| 1945 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify, | 1904 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify, |
| 1946 &public_key, | 1905 &public_key, |
| 1947 &private_key); | 1906 &private_key); |
| 1948 | 1907 |
| 1949 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM | |
| 1950 blink::WebCryptoAlgorithm algorithm = | 1908 blink::WebCryptoAlgorithm algorithm = |
| 1951 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5); | 1909 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5); |
| 1952 #else | |
| 1953 const blink::WebCryptoAlgorithm& algorithm = importAlgorithm; | |
| 1954 #endif | |
| 1955 | 1910 |
| 1956 // Validate the signatures are computed and verified as expected. | 1911 // Validate the signatures are computed and verified as expected. |
| 1957 blink::WebArrayBuffer signature; | 1912 blink::WebArrayBuffer signature; |
| 1958 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { | 1913 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { |
| 1959 SCOPED_TRACE(test_index); | 1914 SCOPED_TRACE(test_index); |
| 1960 | 1915 |
| 1961 base::DictionaryValue* test; | 1916 base::DictionaryValue* test; |
| 1962 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); | 1917 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); |
| 1963 | 1918 |
| 1964 std::vector<uint8> test_message = | 1919 std::vector<uint8> test_message = |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2181 test_cipher_text, | 2136 test_cipher_text, |
| 2182 test_authentication_tag, | 2137 test_authentication_tag, |
| 2183 &plain_text)); | 2138 &plain_text)); |
| 2184 } | 2139 } |
| 2185 } | 2140 } |
| 2186 } | 2141 } |
| 2187 | 2142 |
| 2188 } // namespace webcrypto | 2143 } // namespace webcrypto |
| 2189 | 2144 |
| 2190 } // namespace content | 2145 } // namespace content |
| OLD | NEW |