| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/child/webcrypto/shared_crypto.h" | |
| 6 | |
| 7 #include <algorithm> | 5 #include <algorithm> |
| 8 #include <string> | 6 #include <string> |
| 9 #include <vector> | 7 #include <vector> |
| 10 | 8 |
| 11 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 12 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 13 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 14 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
| 15 #include "base/logging.h" | 13 #include "base/logging.h" |
| 16 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 17 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 18 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 20 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 19 #include "content/child/webcrypto/algorithm_dispatch.h" |
| 21 #include "content/child/webcrypto/crypto_data.h" | 20 #include "content/child/webcrypto/crypto_data.h" |
| 22 #include "content/child/webcrypto/status.h" | 21 #include "content/child/webcrypto/status.h" |
| 23 #include "content/child/webcrypto/webcrypto_util.h" | 22 #include "content/child/webcrypto/webcrypto_util.h" |
| 24 #include "content/public/common/content_paths.h" | 23 #include "content/public/common/content_paths.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 25 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| 27 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 26 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 28 #include "third_party/WebKit/public/platform/WebCryptoKey.h" | 27 #include "third_party/WebKit/public/platform/WebCryptoKey.h" |
| 29 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | 28 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
| 30 #include "third_party/re2/re2/re2.h" | 29 #include "third_party/re2/re2/re2.h" |
| 31 | 30 |
| 32 #if !defined(USE_OPENSSL) | 31 #if !defined(USE_OPENSSL) |
| 33 #include <nss.h> | 32 #include <nss.h> |
| 34 #include <pk11pub.h> | 33 #include <pk11pub.h> |
| 35 | 34 |
| 35 #include "crypto/nss_util.h" |
| 36 #include "crypto/scoped_nss_types.h" | 36 #include "crypto/scoped_nss_types.h" |
| 37 #endif | 37 #endif |
| 38 | 38 |
| 39 // The OpenSSL implementation of WebCrypto is less complete, so don't run all of | 39 // The OpenSSL implementation of WebCrypto is less complete, so don't run all of |
| 40 // the tests: http://crbug.com/267888 | 40 // the tests: http://crbug.com/267888 |
| 41 #if defined(USE_OPENSSL) | 41 #if defined(USE_OPENSSL) |
| 42 #define MAYBE(test_name) DISABLED_##test_name | 42 #define MAYBE(test_name) DISABLED_##test_name |
| 43 #else | 43 #else |
| 44 #define MAYBE(test_name) test_name | 44 #define MAYBE(test_name) test_name |
| 45 #endif | 45 #endif |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 | 115 |
| 116 if (status.IsError()) | 116 if (status.IsError()) |
| 117 EXPECT_EQ(blink::WebCryptoErrorTypeNotSupported, status.error_type()); | 117 EXPECT_EQ(blink::WebCryptoErrorTypeNotSupported, status.error_type()); |
| 118 return status.IsSuccess(); | 118 return status.IsSuccess(); |
| 119 } | 119 } |
| 120 | 120 |
| 121 bool SupportsRsaOaep() { | 121 bool SupportsRsaOaep() { |
| 122 #if defined(USE_OPENSSL) | 122 #if defined(USE_OPENSSL) |
| 123 return false; | 123 return false; |
| 124 #else | 124 #else |
| 125 crypto::EnsureNSSInit(); |
| 125 // TODO(eroman): Exclude version test for OS_CHROMEOS | 126 // TODO(eroman): Exclude version test for OS_CHROMEOS |
| 126 #if defined(USE_NSS) | 127 #if defined(USE_NSS) |
| 127 if (!NSS_VersionCheck("3.16.2")) | 128 if (!NSS_VersionCheck("3.16.2")) |
| 128 return false; | 129 return false; |
| 129 #endif | 130 #endif |
| 130 crypto::ScopedPK11Slot slot(PK11_GetInternalKeySlot()); | 131 crypto::ScopedPK11Slot slot(PK11_GetInternalKeySlot()); |
| 131 return !!PK11_DoesMechanism(slot.get(), CKM_RSA_PKCS_OAEP); | 132 return !!PK11_DoesMechanism(slot.get(), CKM_RSA_PKCS_OAEP); |
| 132 #endif | 133 #endif |
| 133 } | 134 } |
| 134 | 135 |
| 135 bool SupportsRsaKeyImport() { | 136 bool SupportsRsaKeyImport() { |
| 136 // TODO(eroman): Exclude version test for OS_CHROMEOS | 137 // TODO(eroman): Exclude version test for OS_CHROMEOS |
| 137 #if defined(USE_NSS) | 138 #if defined(USE_NSS) |
| 139 crypto::EnsureNSSInit(); |
| 138 if (!NSS_VersionCheck("3.16.2")) { | 140 if (!NSS_VersionCheck("3.16.2")) { |
| 139 LOG(WARNING) << "RSA key import is not supported by this version of NSS. " | 141 LOG(WARNING) << "RSA key import is not supported by this version of NSS. " |
| 140 "Skipping some tests"; | 142 "Skipping some tests"; |
| 141 return false; | 143 return false; |
| 142 } | 144 } |
| 143 #endif | 145 #endif |
| 144 return true; | 146 return true; |
| 145 } | 147 } |
| 146 | 148 |
| 147 blink::WebCryptoAlgorithm CreateRsaHashedKeyGenAlgorithm( | 149 blink::WebCryptoAlgorithm CreateRsaHashedKeyGenAlgorithm( |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 "584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb2190acf832b847f13a3d24" | 440 "584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb2190acf832b847f13a3d24" |
| 439 "a79f4d"; | 441 "a79f4d"; |
| 440 // The modulus and exponent (in hex) of kPublicKeySpkiDerHex | 442 // The modulus and exponent (in hex) of kPublicKeySpkiDerHex |
| 441 const char* const kPublicKeyModulusHex = | 443 const char* const kPublicKeyModulusHex = |
| 442 "A56E4A0E701017589A5187DC7EA841D156F2EC0E36AD52A44DFEB1E61F7AD991D8C51056" | 444 "A56E4A0E701017589A5187DC7EA841D156F2EC0E36AD52A44DFEB1E61F7AD991D8C51056" |
| 443 "FFEDB162B4C0F283A12A88A394DFF526AB7291CBB307CEABFCE0B1DFD5CD9508096D5B2B" | 445 "FFEDB162B4C0F283A12A88A394DFF526AB7291CBB307CEABFCE0B1DFD5CD9508096D5B2B" |
| 444 "8B6DF5D671EF6377C0921CB23C270A70E2598E6FF89D19F105ACC2D3F0CB35F29280E138" | 446 "8B6DF5D671EF6377C0921CB23C270A70E2598E6FF89D19F105ACC2D3F0CB35F29280E138" |
| 445 "6B6F64C4EF22E1E1F20D0CE8CFFB2249BD9A2137"; | 447 "6B6F64C4EF22E1E1F20D0CE8CFFB2249BD9A2137"; |
| 446 const char* const kPublicKeyExponentHex = "010001"; | 448 const char* const kPublicKeyExponentHex = "010001"; |
| 447 | 449 |
| 450 // TODO(eroman): Remove unnecessary test fixture. |
| 448 class SharedCryptoTest : public testing::Test { | 451 class SharedCryptoTest : public testing::Test { |
| 449 protected: | |
| 450 virtual void SetUp() OVERRIDE { Init(); } | |
| 451 }; | 452 }; |
| 452 | 453 |
| 453 blink::WebCryptoKey ImportSecretKeyFromRaw( | 454 blink::WebCryptoKey ImportSecretKeyFromRaw( |
| 454 const std::vector<uint8>& key_raw, | 455 const std::vector<uint8>& key_raw, |
| 455 const blink::WebCryptoAlgorithm& algorithm, | 456 const blink::WebCryptoAlgorithm& algorithm, |
| 456 blink::WebCryptoKeyUsageMask usage) { | 457 blink::WebCryptoKeyUsageMask usage) { |
| 457 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 458 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 458 bool extractable = true; | 459 bool extractable = true; |
| 459 EXPECT_EQ(Status::Success(), | 460 EXPECT_EQ(Status::Success(), |
| 460 ImportKey(blink::WebCryptoKeyFormatRaw, | 461 ImportKey(blink::WebCryptoKeyFormatRaw, |
| (...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1002 ImportKey(blink::WebCryptoKeyFormatRaw, | 1003 ImportKey(blink::WebCryptoKeyFormatRaw, |
| 1003 CryptoData(key_raw), | 1004 CryptoData(key_raw), |
| 1004 CreateAesCbcAlgorithm(iv), | 1005 CreateAesCbcAlgorithm(iv), |
| 1005 true, | 1006 true, |
| 1006 blink::WebCryptoKeyUsageEncrypt, | 1007 blink::WebCryptoKeyUsageEncrypt, |
| 1007 &key)); | 1008 &key)); |
| 1008 } | 1009 } |
| 1009 | 1010 |
| 1010 // Fail exporting the key in SPKI and PKCS#8 formats (not allowed for secret | 1011 // Fail exporting the key in SPKI and PKCS#8 formats (not allowed for secret |
| 1011 // keys). | 1012 // keys). |
| 1012 EXPECT_EQ(Status::ErrorUnexpectedKeyType(), | 1013 EXPECT_EQ(Status::ErrorUnsupportedExportKeyFormat(), |
| 1013 ExportKey(blink::WebCryptoKeyFormatSpki, key, &output)); | 1014 ExportKey(blink::WebCryptoKeyFormatSpki, key, &output)); |
| 1014 EXPECT_EQ(Status::ErrorUnexpectedKeyType(), | 1015 EXPECT_EQ(Status::ErrorUnsupportedExportKeyFormat(), |
| 1015 ExportKey(blink::WebCryptoKeyFormatPkcs8, key, &output)); | 1016 ExportKey(blink::WebCryptoKeyFormatPkcs8, key, &output)); |
| 1016 } | 1017 } |
| 1017 | 1018 |
| 1019 TEST_F(SharedCryptoTest, ImportAesCbcSpkiFailure) { |
| 1020 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 1021 ASSERT_EQ(Status::ErrorUnsupportedImportKeyFormat(), |
| 1022 ImportKey(blink::WebCryptoKeyFormatSpki, |
| 1023 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)), |
| 1024 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), |
| 1025 true, |
| 1026 blink::WebCryptoKeyUsageEncrypt, |
| 1027 &key)); |
| 1028 } |
| 1029 |
| 1018 TEST_F(SharedCryptoTest, MAYBE(AesCbcSampleSets)) { | 1030 TEST_F(SharedCryptoTest, MAYBE(AesCbcSampleSets)) { |
| 1019 scoped_ptr<base::ListValue> tests; | 1031 scoped_ptr<base::ListValue> tests; |
| 1020 ASSERT_TRUE(ReadJsonTestFileToList("aes_cbc.json", &tests)); | 1032 ASSERT_TRUE(ReadJsonTestFileToList("aes_cbc.json", &tests)); |
| 1021 | 1033 |
| 1022 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { | 1034 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { |
| 1023 SCOPED_TRACE(test_index); | 1035 SCOPED_TRACE(test_index); |
| 1024 base::DictionaryValue* test; | 1036 base::DictionaryValue* test; |
| 1025 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); | 1037 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); |
| 1026 | 1038 |
| 1027 std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); | 1039 std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1360 // Fail on invalid JSON. | 1372 // Fail on invalid JSON. |
| 1361 const std::vector<uint8> bad_json_vec = MakeJsonVector( | 1373 const std::vector<uint8> bad_json_vec = MakeJsonVector( |
| 1362 "{" | 1374 "{" |
| 1363 "\"kty\" : \"oct\"," | 1375 "\"kty\" : \"oct\"," |
| 1364 "\"alg\" : \"HS256\"," | 1376 "\"alg\" : \"HS256\"," |
| 1365 "\"use\" : "); | 1377 "\"use\" : "); |
| 1366 EXPECT_EQ(Status::ErrorJwkNotDictionary(), | 1378 EXPECT_EQ(Status::ErrorJwkNotDictionary(), |
| 1367 ImportKeyJwk( | 1379 ImportKeyJwk( |
| 1368 CryptoData(bad_json_vec), algorithm, false, usage_mask, &key)); | 1380 CryptoData(bad_json_vec), algorithm, false, usage_mask, &key)); |
| 1369 | 1381 |
| 1370 // Fail on JWK alg present but unrecognized. | 1382 // Fail on JWK alg present but incorrect (expecting A128CBC). |
| 1371 dict.SetString("alg", "A127CBC"); | 1383 dict.SetString("alg", "A127CBC"); |
| 1372 EXPECT_EQ(Status::ErrorJwkUnrecognizedAlgorithm(), | 1384 EXPECT_EQ(Status::ErrorJwkAlgorithmInconsistent(), |
| 1373 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); | 1385 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); |
| 1374 RestoreJwkOctDictionary(&dict); | 1386 RestoreJwkOctDictionary(&dict); |
| 1375 | 1387 |
| 1376 // Fail on invalid kty. | 1388 // Fail on invalid kty. |
| 1377 dict.SetString("kty", "foo"); | 1389 dict.SetString("kty", "foo"); |
| 1378 EXPECT_EQ(Status::ErrorJwkUnrecognizedKty(), | 1390 EXPECT_EQ(Status::ErrorJwkUnexpectedKty("oct"), |
| 1379 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); | 1391 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); |
| 1380 RestoreJwkOctDictionary(&dict); | 1392 RestoreJwkOctDictionary(&dict); |
| 1381 | 1393 |
| 1382 // Fail on missing kty. | 1394 // Fail on missing kty. |
| 1383 dict.Remove("kty", NULL); | 1395 dict.Remove("kty", NULL); |
| 1384 EXPECT_EQ(Status::ErrorJwkPropertyMissing("kty"), | 1396 EXPECT_EQ(Status::ErrorJwkPropertyMissing("kty"), |
| 1385 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); | 1397 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); |
| 1386 RestoreJwkOctDictionary(&dict); | 1398 RestoreJwkOctDictionary(&dict); |
| 1387 | 1399 |
| 1388 // Fail on kty wrong type. | 1400 // Fail on kty wrong type. |
| (...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1727 EXPECT_EQ(Status::Success(), | 1739 EXPECT_EQ(Status::Success(), |
| 1728 ImportKeyJwkFromDict(dict, algorithm, true, usage_mask, &key)); | 1740 ImportKeyJwkFromDict(dict, algorithm, true, usage_mask, &key)); |
| 1729 EXPECT_TRUE(key.extractable()); | 1741 EXPECT_TRUE(key.extractable()); |
| 1730 EXPECT_EQ(Status::Success(), | 1742 EXPECT_EQ(Status::Success(), |
| 1731 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); | 1743 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); |
| 1732 EXPECT_FALSE(key.extractable()); | 1744 EXPECT_FALSE(key.extractable()); |
| 1733 dict.SetBoolean("ext", true); // restore previous value | 1745 dict.SetBoolean("ext", true); // restore previous value |
| 1734 | 1746 |
| 1735 // Fail: Input algorithm (AES-CBC) is inconsistent with JWK value | 1747 // Fail: Input algorithm (AES-CBC) is inconsistent with JWK value |
| 1736 // (HMAC SHA256). | 1748 // (HMAC SHA256). |
| 1737 EXPECT_EQ(Status::ErrorJwkAlgorithmInconsistent(), | 1749 dict.Clear(); |
| 1750 dict.SetString("kty", "oct"); |
| 1751 dict.SetString("alg", "HS256"); |
| 1752 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg"); |
| 1753 EXPECT_EQ( |
| 1754 Status::ErrorJwkAlgorithmInconsistent(), |
| 1755 ImportKeyJwkFromDict(dict, |
| 1756 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), |
| 1757 extractable, |
| 1758 blink::WebCryptoKeyUsageEncrypt, |
| 1759 &key)); |
| 1760 // Fail: Input usage (encrypt) is inconsistent with JWK value (use=sig). |
| 1761 EXPECT_EQ(Status::ErrorJwkUseInconsistent(), |
| 1738 ImportKeyJwk(CryptoData(json_vec), | 1762 ImportKeyJwk(CryptoData(json_vec), |
| 1739 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), | 1763 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), |
| 1740 extractable, | 1764 extractable, |
| 1741 blink::WebCryptoKeyUsageEncrypt, | 1765 blink::WebCryptoKeyUsageEncrypt, |
| 1742 &key)); | 1766 &key)); |
| 1743 | 1767 |
| 1744 // Fail: Input algorithm (HMAC SHA1) is inconsistent with JWK value | 1768 // Fail: Input algorithm (HMAC SHA1) is inconsistent with JWK value |
| 1745 // (HMAC SHA256). | 1769 // (HMAC SHA256). |
| 1746 EXPECT_EQ( | 1770 EXPECT_EQ( |
| 1747 Status::ErrorJwkAlgorithmInconsistent(), | 1771 Status::ErrorJwkAlgorithmInconsistent(), |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2030 EXPECT_EQ( | 2054 EXPECT_EQ( |
| 2031 Status::DataError(), | 2055 Status::DataError(), |
| 2032 ImportKey(blink::WebCryptoKeyFormatSpki, | 2056 ImportKey(blink::WebCryptoKeyFormatSpki, |
| 2033 CryptoData(HexStringToBytes("618333c4cb")), | 2057 CryptoData(HexStringToBytes("618333c4cb")), |
| 2034 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5), | 2058 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5), |
| 2035 true, | 2059 true, |
| 2036 blink::WebCryptoKeyUsageVerify, | 2060 blink::WebCryptoKeyUsageVerify, |
| 2037 &key)); | 2061 &key)); |
| 2038 | 2062 |
| 2039 // Failing case: Import RSA key but provide an inconsistent input algorithm. | 2063 // Failing case: Import RSA key but provide an inconsistent input algorithm. |
| 2040 EXPECT_EQ(Status::DataError(), | 2064 EXPECT_EQ(Status::ErrorUnsupportedImportKeyFormat(), |
| 2041 ImportKey(blink::WebCryptoKeyFormatSpki, | 2065 ImportKey(blink::WebCryptoKeyFormatSpki, |
| 2042 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)), | 2066 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)), |
| 2043 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), | 2067 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), |
| 2044 true, | 2068 true, |
| 2045 blink::WebCryptoKeyUsageEncrypt, | 2069 blink::WebCryptoKeyUsageEncrypt, |
| 2046 &key)); | 2070 &key)); |
| 2047 | 2071 |
| 2048 // Passing case: Export a previously imported RSA public key in SPKI format | 2072 // Passing case: Export a previously imported RSA public key in SPKI format |
| 2049 // and compare to original data. | 2073 // and compare to original data. |
| 2050 std::vector<uint8> output; | 2074 std::vector<uint8> output; |
| 2051 ASSERT_EQ(Status::Success(), | 2075 ASSERT_EQ(Status::Success(), |
| 2052 ExportKey(blink::WebCryptoKeyFormatSpki, key, &output)); | 2076 ExportKey(blink::WebCryptoKeyFormatSpki, key, &output)); |
| 2053 EXPECT_BYTES_EQ_HEX(kPublicKeySpkiDerHex, output); | 2077 EXPECT_BYTES_EQ_HEX(kPublicKeySpkiDerHex, output); |
| 2054 | 2078 |
| 2055 // Failing case: Try to export a previously imported RSA public key in raw | 2079 // Failing case: Try to export a previously imported RSA public key in raw |
| 2056 // format (not allowed for a public key). | 2080 // format (not allowed for a public key). |
| 2057 EXPECT_EQ(Status::ErrorUnexpectedKeyType(), | 2081 EXPECT_EQ(Status::ErrorUnsupportedExportKeyFormat(), |
| 2058 ExportKey(blink::WebCryptoKeyFormatRaw, key, &output)); | 2082 ExportKey(blink::WebCryptoKeyFormatRaw, key, &output)); |
| 2059 | 2083 |
| 2060 // Failing case: Try to export a non-extractable key | 2084 // Failing case: Try to export a non-extractable key |
| 2061 ASSERT_EQ(Status::Success(), | 2085 ASSERT_EQ(Status::Success(), |
| 2062 ImportKey(blink::WebCryptoKeyFormatSpki, | 2086 ImportKey(blink::WebCryptoKeyFormatSpki, |
| 2063 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)), | 2087 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)), |
| 2064 CreateRsaHashedImportAlgorithm( | 2088 CreateRsaHashedImportAlgorithm( |
| 2065 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 2089 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 2066 blink::WebCryptoAlgorithmIdSha256), | 2090 blink::WebCryptoAlgorithmIdSha256), |
| 2067 false, | 2091 false, |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2130 CryptoData(HexStringToBytes("618333c4cb")), | 2154 CryptoData(HexStringToBytes("618333c4cb")), |
| 2131 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5), | 2155 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5), |
| 2132 true, | 2156 true, |
| 2133 blink::WebCryptoKeyUsageSign, | 2157 blink::WebCryptoKeyUsageSign, |
| 2134 &key)); | 2158 &key)); |
| 2135 | 2159 |
| 2136 // Failing case: Import RSA key but provide an inconsistent input algorithm | 2160 // Failing case: Import RSA key but provide an inconsistent input algorithm |
| 2137 // and usage. Several issues here: | 2161 // and usage. Several issues here: |
| 2138 // * AES-CBC doesn't support PKCS8 key format | 2162 // * AES-CBC doesn't support PKCS8 key format |
| 2139 // * AES-CBC doesn't support "sign" usage | 2163 // * AES-CBC doesn't support "sign" usage |
| 2140 EXPECT_EQ(Status::ErrorCreateKeyBadUsages(), | 2164 EXPECT_EQ(Status::ErrorUnsupportedImportKeyFormat(), |
| 2141 ImportKey(blink::WebCryptoKeyFormatPkcs8, | 2165 ImportKey(blink::WebCryptoKeyFormatPkcs8, |
| 2142 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)), | 2166 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)), |
| 2143 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), | 2167 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), |
| 2144 true, | 2168 true, |
| 2145 blink::WebCryptoKeyUsageSign, | 2169 blink::WebCryptoKeyUsageSign, |
| 2146 &key)); | 2170 &key)); |
| 2147 } | 2171 } |
| 2148 | 2172 |
| 2149 // Tests JWK import and export by doing a roundtrip key conversion and ensuring | 2173 // Tests JWK import and export by doing a roundtrip key conversion and ensuring |
| 2150 // it was lossless: | 2174 // it was lossless: |
| (...skipping 1272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3423 wrapping_key, | 3447 wrapping_key, |
| 3424 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw), | 3448 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw), |
| 3425 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), | 3449 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), |
| 3426 true, | 3450 true, |
| 3427 blink::WebCryptoKeyUsageEncrypt, | 3451 blink::WebCryptoKeyUsageEncrypt, |
| 3428 &unwrapped_key)); | 3452 &unwrapped_key)); |
| 3429 } | 3453 } |
| 3430 | 3454 |
| 3431 class SharedCryptoRsaOaepTest : public ::testing::Test { | 3455 class SharedCryptoRsaOaepTest : public ::testing::Test { |
| 3432 public: | 3456 public: |
| 3433 SharedCryptoRsaOaepTest() { Init(); } | |
| 3434 | |
| 3435 scoped_ptr<base::DictionaryValue> CreatePublicKeyJwkDict() { | 3457 scoped_ptr<base::DictionaryValue> CreatePublicKeyJwkDict() { |
| 3436 scoped_ptr<base::DictionaryValue> jwk(new base::DictionaryValue()); | 3458 scoped_ptr<base::DictionaryValue> jwk(new base::DictionaryValue()); |
| 3437 jwk->SetString("kty", "RSA"); | 3459 jwk->SetString("kty", "RSA"); |
| 3438 jwk->SetString("n", | 3460 jwk->SetString("n", |
| 3439 Base64EncodeUrlSafe(HexStringToBytes(kPublicKeyModulusHex))); | 3461 Base64EncodeUrlSafe(HexStringToBytes(kPublicKeyModulusHex))); |
| 3440 jwk->SetString( | 3462 jwk->SetString( |
| 3441 "e", Base64EncodeUrlSafe(HexStringToBytes(kPublicKeyExponentHex))); | 3463 "e", Base64EncodeUrlSafe(HexStringToBytes(kPublicKeyExponentHex))); |
| 3442 return jwk.Pass(); | 3464 return jwk.Pass(); |
| 3443 } | 3465 } |
| 3444 }; | 3466 }; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3526 if (!SupportsRsaOaep()) { | 3548 if (!SupportsRsaOaep()) { |
| 3527 LOG(WARNING) << "RSA-OAEP support not present; skipping."; | 3549 LOG(WARNING) << "RSA-OAEP support not present; skipping."; |
| 3528 return; | 3550 return; |
| 3529 } | 3551 } |
| 3530 | 3552 |
| 3531 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict()); | 3553 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict()); |
| 3532 jwk->SetString("kty", "oct"); | 3554 jwk->SetString("kty", "oct"); |
| 3533 jwk->SetString("alg", "RSA-OAEP"); | 3555 jwk->SetString("alg", "RSA-OAEP"); |
| 3534 | 3556 |
| 3535 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | 3557 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); |
| 3536 ASSERT_EQ(Status::ErrorJwkPropertyMissing("k"), | 3558 ASSERT_EQ(Status::ErrorJwkUnexpectedKty("RSA"), |
| 3537 ImportKeyJwkFromDict(*jwk.get(), | 3559 ImportKeyJwkFromDict(*jwk.get(), |
| 3538 CreateRsaHashedImportAlgorithm( | 3560 CreateRsaHashedImportAlgorithm( |
| 3539 blink::WebCryptoAlgorithmIdRsaOaep, | 3561 blink::WebCryptoAlgorithmIdRsaOaep, |
| 3540 blink::WebCryptoAlgorithmIdSha1), | 3562 blink::WebCryptoAlgorithmIdSha1), |
| 3541 true, | 3563 true, |
| 3542 blink::WebCryptoKeyUsageEncrypt, | 3564 blink::WebCryptoKeyUsageEncrypt, |
| 3543 &public_key)); | 3565 &public_key)); |
| 3544 } | 3566 } |
| 3545 | 3567 |
| 3546 TEST_F(SharedCryptoRsaOaepTest, ExportPublicJwk) { | 3568 TEST_F(SharedCryptoRsaOaepTest, ExportPublicJwk) { |
| (...skipping 852 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4399 EXPECT_EQ(public_key_spki, unwrapped_public_key_spki); | 4421 EXPECT_EQ(public_key_spki, unwrapped_public_key_spki); |
| 4400 EXPECT_EQ(private_key_pkcs8, unwrapped_private_key_pkcs8); | 4422 EXPECT_EQ(private_key_pkcs8, unwrapped_private_key_pkcs8); |
| 4401 | 4423 |
| 4402 EXPECT_NE(public_key_spki, wrapped_public_key); | 4424 EXPECT_NE(public_key_spki, wrapped_public_key); |
| 4403 EXPECT_NE(private_key_pkcs8, wrapped_private_key); | 4425 EXPECT_NE(private_key_pkcs8, wrapped_private_key); |
| 4404 } | 4426 } |
| 4405 | 4427 |
| 4406 } // namespace webcrypto | 4428 } // namespace webcrypto |
| 4407 | 4429 |
| 4408 } // namespace content | 4430 } // namespace content |
| OLD | NEW |