Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(459)

Side by Side Diff: content/child/webcrypto/shared_crypto_unittest.cc

Issue 379383002: Refactor WebCrypto code (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase onto master (no longer has BoringSSL) Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/child/webcrypto/shared_crypto.cc ('k') | content/child/webcrypto/status.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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 422 matching lines...) Expand 10 before | Expand all | Expand 10 after
883 884
884 std::vector<uint8> output; 885 std::vector<uint8> output;
885 886
886 ASSERT_EQ(Status::Success(), 887 ASSERT_EQ(Status::Success(),
887 Sign(algorithm, key, CryptoData(test_message), &output)); 888 Sign(algorithm, key, CryptoData(test_message), &output));
888 889
889 EXPECT_BYTES_EQ(test_mac, output); 890 EXPECT_BYTES_EQ(test_mac, output);
890 891
891 bool signature_match = false; 892 bool signature_match = false;
892 EXPECT_EQ(Status::Success(), 893 EXPECT_EQ(Status::Success(),
893 VerifySignature(algorithm, 894 Verify(algorithm,
894 key, 895 key,
895 CryptoData(output), 896 CryptoData(output),
896 CryptoData(test_message), 897 CryptoData(test_message),
897 &signature_match)); 898 &signature_match));
898 EXPECT_TRUE(signature_match); 899 EXPECT_TRUE(signature_match);
899 900
900 // Ensure truncated signature does not verify by passing one less byte. 901 // Ensure truncated signature does not verify by passing one less byte.
901 EXPECT_EQ( 902 EXPECT_EQ(Status::Success(),
902 Status::Success(), 903 Verify(algorithm,
903 VerifySignature(algorithm, 904 key,
904 key, 905 CryptoData(Uint8VectorStart(output), output.size() - 1),
905 CryptoData(Uint8VectorStart(output), output.size() - 1), 906 CryptoData(test_message),
906 CryptoData(test_message), 907 &signature_match));
907 &signature_match));
908 EXPECT_FALSE(signature_match); 908 EXPECT_FALSE(signature_match);
909 909
910 // Ensure truncated signature does not verify by passing no bytes. 910 // Ensure truncated signature does not verify by passing no bytes.
911 EXPECT_EQ(Status::Success(), 911 EXPECT_EQ(Status::Success(),
912 VerifySignature(algorithm, 912 Verify(algorithm,
913 key, 913 key,
914 CryptoData(), 914 CryptoData(),
915 CryptoData(test_message), 915 CryptoData(test_message),
916 &signature_match)); 916 &signature_match));
917 EXPECT_FALSE(signature_match); 917 EXPECT_FALSE(signature_match);
918 918
919 // Ensure extra long signature does not cause issues and fails. 919 // Ensure extra long signature does not cause issues and fails.
920 const unsigned char kLongSignature[1024] = {0}; 920 const unsigned char kLongSignature[1024] = {0};
921 EXPECT_EQ( 921 EXPECT_EQ(Status::Success(),
922 Status::Success(), 922 Verify(algorithm,
923 VerifySignature(algorithm, 923 key,
924 key, 924 CryptoData(kLongSignature, sizeof(kLongSignature)),
925 CryptoData(kLongSignature, sizeof(kLongSignature)), 925 CryptoData(test_message),
926 CryptoData(test_message), 926 &signature_match));
927 &signature_match));
928 EXPECT_FALSE(signature_match); 927 EXPECT_FALSE(signature_match);
929 } 928 }
930 } 929 }
931 930
932 TEST_F(SharedCryptoTest, AesCbcFailures) { 931 TEST_F(SharedCryptoTest, AesCbcFailures) {
933 const std::string key_hex = "2b7e151628aed2a6abf7158809cf4f3c"; 932 const std::string key_hex = "2b7e151628aed2a6abf7158809cf4f3c";
934 blink::WebCryptoKey key = ImportSecretKeyFromRaw( 933 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
935 HexStringToBytes(key_hex), 934 HexStringToBytes(key_hex),
936 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 935 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
937 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); 936 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
1002 ImportKey(blink::WebCryptoKeyFormatRaw, 1001 ImportKey(blink::WebCryptoKeyFormatRaw,
1003 CryptoData(key_raw), 1002 CryptoData(key_raw),
1004 CreateAesCbcAlgorithm(iv), 1003 CreateAesCbcAlgorithm(iv),
1005 true, 1004 true,
1006 blink::WebCryptoKeyUsageEncrypt, 1005 blink::WebCryptoKeyUsageEncrypt,
1007 &key)); 1006 &key));
1008 } 1007 }
1009 1008
1010 // Fail exporting the key in SPKI and PKCS#8 formats (not allowed for secret 1009 // Fail exporting the key in SPKI and PKCS#8 formats (not allowed for secret
1011 // keys). 1010 // keys).
1012 EXPECT_EQ(Status::ErrorUnexpectedKeyType(), 1011 EXPECT_EQ(Status::ErrorUnsupportedExportKeyFormat(),
1013 ExportKey(blink::WebCryptoKeyFormatSpki, key, &output)); 1012 ExportKey(blink::WebCryptoKeyFormatSpki, key, &output));
1014 EXPECT_EQ(Status::ErrorUnexpectedKeyType(), 1013 EXPECT_EQ(Status::ErrorUnsupportedExportKeyFormat(),
1015 ExportKey(blink::WebCryptoKeyFormatPkcs8, key, &output)); 1014 ExportKey(blink::WebCryptoKeyFormatPkcs8, key, &output));
1016 } 1015 }
1017 1016
1017 TEST_F(SharedCryptoTest, ImportAesCbcSpkiFailure) {
1018 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1019 ASSERT_EQ(Status::ErrorUnsupportedImportKeyFormat(),
1020 ImportKey(blink::WebCryptoKeyFormatSpki,
1021 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)),
1022 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
1023 true,
1024 blink::WebCryptoKeyUsageEncrypt,
1025 &key));
1026 }
1027
1018 TEST_F(SharedCryptoTest, MAYBE(AesCbcSampleSets)) { 1028 TEST_F(SharedCryptoTest, MAYBE(AesCbcSampleSets)) {
1019 scoped_ptr<base::ListValue> tests; 1029 scoped_ptr<base::ListValue> tests;
1020 ASSERT_TRUE(ReadJsonTestFileToList("aes_cbc.json", &tests)); 1030 ASSERT_TRUE(ReadJsonTestFileToList("aes_cbc.json", &tests));
1021 1031
1022 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { 1032 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
1023 SCOPED_TRACE(test_index); 1033 SCOPED_TRACE(test_index);
1024 base::DictionaryValue* test; 1034 base::DictionaryValue* test;
1025 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); 1035 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
1026 1036
1027 std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); 1037 std::vector<uint8> test_key = GetBytesFromHexString(test, "key");
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after
1360 // Fail on invalid JSON. 1370 // Fail on invalid JSON.
1361 const std::vector<uint8> bad_json_vec = MakeJsonVector( 1371 const std::vector<uint8> bad_json_vec = MakeJsonVector(
1362 "{" 1372 "{"
1363 "\"kty\" : \"oct\"," 1373 "\"kty\" : \"oct\","
1364 "\"alg\" : \"HS256\"," 1374 "\"alg\" : \"HS256\","
1365 "\"use\" : "); 1375 "\"use\" : ");
1366 EXPECT_EQ(Status::ErrorJwkNotDictionary(), 1376 EXPECT_EQ(Status::ErrorJwkNotDictionary(),
1367 ImportKeyJwk( 1377 ImportKeyJwk(
1368 CryptoData(bad_json_vec), algorithm, false, usage_mask, &key)); 1378 CryptoData(bad_json_vec), algorithm, false, usage_mask, &key));
1369 1379
1370 // Fail on JWK alg present but unrecognized. 1380 // Fail on JWK alg present but incorrect (expecting A128CBC).
1371 dict.SetString("alg", "A127CBC"); 1381 dict.SetString("alg", "A127CBC");
1372 EXPECT_EQ(Status::ErrorJwkUnrecognizedAlgorithm(), 1382 EXPECT_EQ(Status::ErrorJwkAlgorithmInconsistent(),
1373 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); 1383 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key));
1374 RestoreJwkOctDictionary(&dict); 1384 RestoreJwkOctDictionary(&dict);
1375 1385
1376 // Fail on invalid kty. 1386 // Fail on invalid kty.
1377 dict.SetString("kty", "foo"); 1387 dict.SetString("kty", "foo");
1378 EXPECT_EQ(Status::ErrorJwkUnrecognizedKty(), 1388 EXPECT_EQ(Status::ErrorJwkUnexpectedKty("oct"),
1379 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); 1389 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key));
1380 RestoreJwkOctDictionary(&dict); 1390 RestoreJwkOctDictionary(&dict);
1381 1391
1382 // Fail on missing kty. 1392 // Fail on missing kty.
1383 dict.Remove("kty", NULL); 1393 dict.Remove("kty", NULL);
1384 EXPECT_EQ(Status::ErrorJwkPropertyMissing("kty"), 1394 EXPECT_EQ(Status::ErrorJwkPropertyMissing("kty"),
1385 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); 1395 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key));
1386 RestoreJwkOctDictionary(&dict); 1396 RestoreJwkOctDictionary(&dict);
1387 1397
1388 // Fail on kty wrong type. 1398 // Fail on kty wrong type.
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after
1727 EXPECT_EQ(Status::Success(), 1737 EXPECT_EQ(Status::Success(),
1728 ImportKeyJwkFromDict(dict, algorithm, true, usage_mask, &key)); 1738 ImportKeyJwkFromDict(dict, algorithm, true, usage_mask, &key));
1729 EXPECT_TRUE(key.extractable()); 1739 EXPECT_TRUE(key.extractable());
1730 EXPECT_EQ(Status::Success(), 1740 EXPECT_EQ(Status::Success(),
1731 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); 1741 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key));
1732 EXPECT_FALSE(key.extractable()); 1742 EXPECT_FALSE(key.extractable());
1733 dict.SetBoolean("ext", true); // restore previous value 1743 dict.SetBoolean("ext", true); // restore previous value
1734 1744
1735 // Fail: Input algorithm (AES-CBC) is inconsistent with JWK value 1745 // Fail: Input algorithm (AES-CBC) is inconsistent with JWK value
1736 // (HMAC SHA256). 1746 // (HMAC SHA256).
1737 EXPECT_EQ(Status::ErrorJwkAlgorithmInconsistent(), 1747 dict.Clear();
1748 dict.SetString("kty", "oct");
1749 dict.SetString("alg", "HS256");
1750 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg");
1751 EXPECT_EQ(
1752 Status::ErrorJwkAlgorithmInconsistent(),
1753 ImportKeyJwkFromDict(dict,
1754 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
1755 extractable,
1756 blink::WebCryptoKeyUsageEncrypt,
1757 &key));
1758 // Fail: Input usage (encrypt) is inconsistent with JWK value (use=sig).
1759 EXPECT_EQ(Status::ErrorJwkUseInconsistent(),
1738 ImportKeyJwk(CryptoData(json_vec), 1760 ImportKeyJwk(CryptoData(json_vec),
1739 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 1761 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
1740 extractable, 1762 extractable,
1741 blink::WebCryptoKeyUsageEncrypt, 1763 blink::WebCryptoKeyUsageEncrypt,
1742 &key)); 1764 &key));
1743 1765
1744 // Fail: Input algorithm (HMAC SHA1) is inconsistent with JWK value 1766 // Fail: Input algorithm (HMAC SHA1) is inconsistent with JWK value
1745 // (HMAC SHA256). 1767 // (HMAC SHA256).
1746 EXPECT_EQ( 1768 EXPECT_EQ(
1747 Status::ErrorJwkAlgorithmInconsistent(), 1769 Status::ErrorJwkAlgorithmInconsistent(),
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
2030 EXPECT_EQ( 2052 EXPECT_EQ(
2031 Status::DataError(), 2053 Status::DataError(),
2032 ImportKey(blink::WebCryptoKeyFormatSpki, 2054 ImportKey(blink::WebCryptoKeyFormatSpki,
2033 CryptoData(HexStringToBytes("618333c4cb")), 2055 CryptoData(HexStringToBytes("618333c4cb")),
2034 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5), 2056 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5),
2035 true, 2057 true,
2036 blink::WebCryptoKeyUsageVerify, 2058 blink::WebCryptoKeyUsageVerify,
2037 &key)); 2059 &key));
2038 2060
2039 // Failing case: Import RSA key but provide an inconsistent input algorithm. 2061 // Failing case: Import RSA key but provide an inconsistent input algorithm.
2040 EXPECT_EQ(Status::DataError(), 2062 EXPECT_EQ(Status::ErrorUnsupportedImportKeyFormat(),
2041 ImportKey(blink::WebCryptoKeyFormatSpki, 2063 ImportKey(blink::WebCryptoKeyFormatSpki,
2042 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)), 2064 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)),
2043 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 2065 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
2044 true, 2066 true,
2045 blink::WebCryptoKeyUsageEncrypt, 2067 blink::WebCryptoKeyUsageEncrypt,
2046 &key)); 2068 &key));
2047 2069
2048 // Passing case: Export a previously imported RSA public key in SPKI format 2070 // Passing case: Export a previously imported RSA public key in SPKI format
2049 // and compare to original data. 2071 // and compare to original data.
2050 std::vector<uint8> output; 2072 std::vector<uint8> output;
2051 ASSERT_EQ(Status::Success(), 2073 ASSERT_EQ(Status::Success(),
2052 ExportKey(blink::WebCryptoKeyFormatSpki, key, &output)); 2074 ExportKey(blink::WebCryptoKeyFormatSpki, key, &output));
2053 EXPECT_BYTES_EQ_HEX(kPublicKeySpkiDerHex, output); 2075 EXPECT_BYTES_EQ_HEX(kPublicKeySpkiDerHex, output);
2054 2076
2055 // Failing case: Try to export a previously imported RSA public key in raw 2077 // Failing case: Try to export a previously imported RSA public key in raw
2056 // format (not allowed for a public key). 2078 // format (not allowed for a public key).
2057 EXPECT_EQ(Status::ErrorUnexpectedKeyType(), 2079 EXPECT_EQ(Status::ErrorUnsupportedExportKeyFormat(),
2058 ExportKey(blink::WebCryptoKeyFormatRaw, key, &output)); 2080 ExportKey(blink::WebCryptoKeyFormatRaw, key, &output));
2059 2081
2060 // Failing case: Try to export a non-extractable key 2082 // Failing case: Try to export a non-extractable key
2061 ASSERT_EQ(Status::Success(), 2083 ASSERT_EQ(Status::Success(),
2062 ImportKey(blink::WebCryptoKeyFormatSpki, 2084 ImportKey(blink::WebCryptoKeyFormatSpki,
2063 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)), 2085 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)),
2064 CreateRsaHashedImportAlgorithm( 2086 CreateRsaHashedImportAlgorithm(
2065 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 2087 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
2066 blink::WebCryptoAlgorithmIdSha256), 2088 blink::WebCryptoAlgorithmIdSha256),
2067 false, 2089 false,
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
2130 CryptoData(HexStringToBytes("618333c4cb")), 2152 CryptoData(HexStringToBytes("618333c4cb")),
2131 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5), 2153 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5),
2132 true, 2154 true,
2133 blink::WebCryptoKeyUsageSign, 2155 blink::WebCryptoKeyUsageSign,
2134 &key)); 2156 &key));
2135 2157
2136 // Failing case: Import RSA key but provide an inconsistent input algorithm 2158 // Failing case: Import RSA key but provide an inconsistent input algorithm
2137 // and usage. Several issues here: 2159 // and usage. Several issues here:
2138 // * AES-CBC doesn't support PKCS8 key format 2160 // * AES-CBC doesn't support PKCS8 key format
2139 // * AES-CBC doesn't support "sign" usage 2161 // * AES-CBC doesn't support "sign" usage
2140 EXPECT_EQ(Status::ErrorCreateKeyBadUsages(), 2162 EXPECT_EQ(Status::ErrorUnsupportedImportKeyFormat(),
2141 ImportKey(blink::WebCryptoKeyFormatPkcs8, 2163 ImportKey(blink::WebCryptoKeyFormatPkcs8,
2142 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)), 2164 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)),
2143 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 2165 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
2144 true, 2166 true,
2145 blink::WebCryptoKeyUsageSign, 2167 blink::WebCryptoKeyUsageSign,
2146 &key)); 2168 &key));
2147 } 2169 }
2148 2170
2149 // Tests JWK import and export by doing a roundtrip key conversion and ensuring 2171 // Tests JWK import and export by doing a roundtrip key conversion and ensuring
2150 // it was lossless: 2172 // it was lossless:
(...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after
2690 2712
2691 std::vector<uint8> signature; 2713 std::vector<uint8> signature;
2692 bool signature_match; 2714 bool signature_match;
2693 2715
2694 // Compute a signature. 2716 // Compute a signature.
2695 const std::vector<uint8> data = HexStringToBytes("010203040506070809"); 2717 const std::vector<uint8> data = HexStringToBytes("010203040506070809");
2696 ASSERT_EQ(Status::Success(), 2718 ASSERT_EQ(Status::Success(),
2697 Sign(algorithm, private_key, CryptoData(data), &signature)); 2719 Sign(algorithm, private_key, CryptoData(data), &signature));
2698 2720
2699 // Ensure truncated signature does not verify by passing one less byte. 2721 // Ensure truncated signature does not verify by passing one less byte.
2700 EXPECT_EQ(Status::Success(), 2722 EXPECT_EQ(
2701 VerifySignature( 2723 Status::Success(),
2702 algorithm, 2724 Verify(algorithm,
2703 public_key, 2725 public_key,
2704 CryptoData(Uint8VectorStart(signature), signature.size() - 1), 2726 CryptoData(Uint8VectorStart(signature), signature.size() - 1),
2705 CryptoData(data), 2727 CryptoData(data),
2706 &signature_match)); 2728 &signature_match));
2707 EXPECT_FALSE(signature_match); 2729 EXPECT_FALSE(signature_match);
2708 2730
2709 // Ensure truncated signature does not verify by passing no bytes. 2731 // Ensure truncated signature does not verify by passing no bytes.
2710 EXPECT_EQ(Status::Success(), 2732 EXPECT_EQ(Status::Success(),
2711 VerifySignature(algorithm, 2733 Verify(algorithm,
2712 public_key, 2734 public_key,
2713 CryptoData(), 2735 CryptoData(),
2714 CryptoData(data), 2736 CryptoData(data),
2715 &signature_match)); 2737 &signature_match));
2716 EXPECT_FALSE(signature_match); 2738 EXPECT_FALSE(signature_match);
2717 2739
2718 // Ensure corrupted signature does not verify. 2740 // Ensure corrupted signature does not verify.
2719 std::vector<uint8> corrupt_sig = signature; 2741 std::vector<uint8> corrupt_sig = signature;
2720 corrupt_sig[corrupt_sig.size() / 2] ^= 0x1; 2742 corrupt_sig[corrupt_sig.size() / 2] ^= 0x1;
2721 EXPECT_EQ(Status::Success(), 2743 EXPECT_EQ(Status::Success(),
2722 VerifySignature(algorithm, 2744 Verify(algorithm,
2723 public_key, 2745 public_key,
2724 CryptoData(corrupt_sig), 2746 CryptoData(corrupt_sig),
2725 CryptoData(data), 2747 CryptoData(data),
2726 &signature_match)); 2748 &signature_match));
2727 EXPECT_FALSE(signature_match); 2749 EXPECT_FALSE(signature_match);
2728 2750
2729 // Ensure signatures that are greater than the modulus size fail. 2751 // Ensure signatures that are greater than the modulus size fail.
2730 const unsigned int long_message_size_bytes = 1024; 2752 const unsigned int long_message_size_bytes = 1024;
2731 DCHECK_GT(long_message_size_bytes, kModulusLengthBits / 8); 2753 DCHECK_GT(long_message_size_bytes, kModulusLengthBits / 8);
2732 const unsigned char kLongSignature[long_message_size_bytes] = {0}; 2754 const unsigned char kLongSignature[long_message_size_bytes] = {0};
2733 EXPECT_EQ(Status::Success(), 2755 EXPECT_EQ(Status::Success(),
2734 VerifySignature(algorithm, 2756 Verify(algorithm,
2735 public_key, 2757 public_key,
2736 CryptoData(kLongSignature, sizeof(kLongSignature)), 2758 CryptoData(kLongSignature, sizeof(kLongSignature)),
2737 CryptoData(data), 2759 CryptoData(data),
2738 &signature_match)); 2760 &signature_match));
2739 EXPECT_FALSE(signature_match); 2761 EXPECT_FALSE(signature_match);
2740 2762
2741 // Ensure that signing and verifying with an incompatible algorithm fails. 2763 // Ensure that signing and verifying with an incompatible algorithm fails.
2742 algorithm = CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaOaep); 2764 algorithm = CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaOaep);
2743 2765
2744 EXPECT_EQ(Status::ErrorUnexpected(), 2766 EXPECT_EQ(Status::ErrorUnexpected(),
2745 Sign(algorithm, private_key, CryptoData(data), &signature)); 2767 Sign(algorithm, private_key, CryptoData(data), &signature));
2746 EXPECT_EQ(Status::ErrorUnexpected(), 2768 EXPECT_EQ(Status::ErrorUnexpected(),
2747 VerifySignature(algorithm, 2769 Verify(algorithm,
2748 public_key, 2770 public_key,
2749 CryptoData(signature), 2771 CryptoData(signature),
2750 CryptoData(data), 2772 CryptoData(data),
2751 &signature_match)); 2773 &signature_match));
2752 2774
2753 // Some crypto libraries (NSS) can automatically select the RSA SSA inner hash 2775 // Some crypto libraries (NSS) can automatically select the RSA SSA inner hash
2754 // based solely on the contents of the input signature data. In the Web Crypto 2776 // based solely on the contents of the input signature data. In the Web Crypto
2755 // implementation, the inner hash should be specified uniquely by the key 2777 // implementation, the inner hash should be specified uniquely by the key
2756 // algorithm parameter. To validate this behavior, call Verify with a computed 2778 // algorithm parameter. To validate this behavior, call Verify with a computed
2757 // signature that used one hash type (SHA-1), but pass in a key with a 2779 // signature that used one hash type (SHA-1), but pass in a key with a
2758 // different inner hash type (SHA-256). If the hash type is determined by the 2780 // different inner hash type (SHA-256). If the hash type is determined by the
2759 // signature itself (undesired), the verify will pass, while if the hash type 2781 // signature itself (undesired), the verify will pass, while if the hash type
2760 // is specified by the key algorithm (desired), the verify will fail. 2782 // is specified by the key algorithm (desired), the verify will fail.
2761 2783
(...skipping 20 matching lines...) Expand all
2782 // NOTE: public_key was produced by generateKey, and so its associated 2804 // NOTE: public_key was produced by generateKey, and so its associated
2783 // algorithm has WebCryptoRsaKeyGenParams and not WebCryptoRsaSsaParams. Thus 2805 // algorithm has WebCryptoRsaKeyGenParams and not WebCryptoRsaSsaParams. Thus
2784 // it has no inner hash to conflict with the input algorithm. 2806 // it has no inner hash to conflict with the input algorithm.
2785 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, 2807 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1,
2786 private_key.algorithm().rsaHashedParams()->hash().id()); 2808 private_key.algorithm().rsaHashedParams()->hash().id());
2787 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256, 2809 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256,
2788 public_key_256.algorithm().rsaHashedParams()->hash().id()); 2810 public_key_256.algorithm().rsaHashedParams()->hash().id());
2789 2811
2790 bool is_match; 2812 bool is_match;
2791 EXPECT_EQ(Status::Success(), 2813 EXPECT_EQ(Status::Success(),
2792 VerifySignature( 2814 Verify(CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5),
2793 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5), 2815 public_key_256,
2794 public_key_256, 2816 CryptoData(signature),
2795 CryptoData(signature), 2817 CryptoData(data),
2796 CryptoData(data), 2818 &is_match));
2797 &is_match));
2798 EXPECT_FALSE(is_match); 2819 EXPECT_FALSE(is_match);
2799 } 2820 }
2800 2821
2801 TEST_F(SharedCryptoTest, MAYBE(RsaSignVerifyKnownAnswer)) { 2822 TEST_F(SharedCryptoTest, MAYBE(RsaSignVerifyKnownAnswer)) {
2802 if (!SupportsRsaKeyImport()) 2823 if (!SupportsRsaKeyImport())
2803 return; 2824 return;
2804 2825
2805 scoped_ptr<base::ListValue> tests; 2826 scoped_ptr<base::ListValue> tests;
2806 ASSERT_TRUE(ReadJsonTestFileToList("pkcs1v15_sign.json", &tests)); 2827 ASSERT_TRUE(ReadJsonTestFileToList("pkcs1v15_sign.json", &tests));
2807 2828
(...skipping 30 matching lines...) Expand all
2838 GetBytesFromHexString(test, "signature_hex"); 2859 GetBytesFromHexString(test, "signature_hex");
2839 2860
2840 signature.clear(); 2861 signature.clear();
2841 ASSERT_EQ( 2862 ASSERT_EQ(
2842 Status::Success(), 2863 Status::Success(),
2843 Sign(algorithm, private_key, CryptoData(test_message), &signature)); 2864 Sign(algorithm, private_key, CryptoData(test_message), &signature));
2844 EXPECT_BYTES_EQ(test_signature, signature); 2865 EXPECT_BYTES_EQ(test_signature, signature);
2845 2866
2846 bool is_match = false; 2867 bool is_match = false;
2847 ASSERT_EQ(Status::Success(), 2868 ASSERT_EQ(Status::Success(),
2848 VerifySignature(algorithm, 2869 Verify(algorithm,
2849 public_key, 2870 public_key,
2850 CryptoData(test_signature), 2871 CryptoData(test_signature),
2851 CryptoData(test_message), 2872 CryptoData(test_message),
2852 &is_match)); 2873 &is_match));
2853 EXPECT_TRUE(is_match); 2874 EXPECT_TRUE(is_match);
2854 } 2875 }
2855 } 2876 }
2856 2877
2857 TEST_F(SharedCryptoTest, MAYBE(AesKwKeyImport)) { 2878 TEST_F(SharedCryptoTest, MAYBE(AesKwKeyImport)) {
2858 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 2879 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
2859 blink::WebCryptoAlgorithm algorithm = 2880 blink::WebCryptoAlgorithm algorithm =
2860 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); 2881 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw);
2861 2882
2862 // Import a 128-bit Key Encryption Key (KEK) 2883 // Import a 128-bit Key Encryption Key (KEK)
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
3076 ASSERT_EQ(Status::Success(), 3097 ASSERT_EQ(Status::Success(),
3077 Sign(CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac), 3098 Sign(CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac),
3078 key, 3099 key,
3079 CryptoData(test_message), 3100 CryptoData(test_message),
3080 &signature)); 3101 &signature));
3081 3102
3082 EXPECT_GT(signature.size(), 0u); 3103 EXPECT_GT(signature.size(), 0u);
3083 3104
3084 bool verify_result; 3105 bool verify_result;
3085 ASSERT_EQ(Status::Success(), 3106 ASSERT_EQ(Status::Success(),
3086 VerifySignature(CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac), 3107 Verify(CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac),
3087 key, 3108 key,
3088 CryptoData(signature), 3109 CryptoData(signature),
3089 CryptoData(test_message), 3110 CryptoData(test_message),
3090 &verify_result)); 3111 &verify_result));
3091 } 3112 }
3092 3113
3093 TEST_F(SharedCryptoTest, MAYBE(AesKwRawSymkeyWrapUnwrapErrors)) { 3114 TEST_F(SharedCryptoTest, MAYBE(AesKwRawSymkeyWrapUnwrapErrors)) {
3094 scoped_ptr<base::ListValue> tests; 3115 scoped_ptr<base::ListValue> tests;
3095 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests)); 3116 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests));
3096 base::DictionaryValue* test; 3117 base::DictionaryValue* test;
3097 // Use 256 bits of data with a 256-bit KEK 3118 // Use 256 bits of data with a 256-bit KEK
3098 ASSERT_TRUE(tests->GetDictionary(3, &test)); 3119 ASSERT_TRUE(tests->GetDictionary(3, &test));
3099 const std::vector<uint8> test_kek = GetBytesFromHexString(test, "kek"); 3120 const std::vector<uint8> test_kek = GetBytesFromHexString(test, "kek");
3100 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); 3121 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key");
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
3423 wrapping_key, 3444 wrapping_key,
3424 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw), 3445 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw),
3425 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 3446 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
3426 true, 3447 true,
3427 blink::WebCryptoKeyUsageEncrypt, 3448 blink::WebCryptoKeyUsageEncrypt,
3428 &unwrapped_key)); 3449 &unwrapped_key));
3429 } 3450 }
3430 3451
3431 class SharedCryptoRsaOaepTest : public ::testing::Test { 3452 class SharedCryptoRsaOaepTest : public ::testing::Test {
3432 public: 3453 public:
3433 SharedCryptoRsaOaepTest() { Init(); }
3434
3435 scoped_ptr<base::DictionaryValue> CreatePublicKeyJwkDict() { 3454 scoped_ptr<base::DictionaryValue> CreatePublicKeyJwkDict() {
3436 scoped_ptr<base::DictionaryValue> jwk(new base::DictionaryValue()); 3455 scoped_ptr<base::DictionaryValue> jwk(new base::DictionaryValue());
3437 jwk->SetString("kty", "RSA"); 3456 jwk->SetString("kty", "RSA");
3438 jwk->SetString("n", 3457 jwk->SetString("n",
3439 Base64EncodeUrlSafe(HexStringToBytes(kPublicKeyModulusHex))); 3458 Base64EncodeUrlSafe(HexStringToBytes(kPublicKeyModulusHex)));
3440 jwk->SetString( 3459 jwk->SetString(
3441 "e", Base64EncodeUrlSafe(HexStringToBytes(kPublicKeyExponentHex))); 3460 "e", Base64EncodeUrlSafe(HexStringToBytes(kPublicKeyExponentHex)));
3442 return jwk.Pass(); 3461 return jwk.Pass();
3443 } 3462 }
3444 }; 3463 };
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
3526 if (!SupportsRsaOaep()) { 3545 if (!SupportsRsaOaep()) {
3527 LOG(WARNING) << "RSA-OAEP support not present; skipping."; 3546 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3528 return; 3547 return;
3529 } 3548 }
3530 3549
3531 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict()); 3550 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict());
3532 jwk->SetString("kty", "oct"); 3551 jwk->SetString("kty", "oct");
3533 jwk->SetString("alg", "RSA-OAEP"); 3552 jwk->SetString("alg", "RSA-OAEP");
3534 3553
3535 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 3554 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
3536 ASSERT_EQ(Status::ErrorJwkPropertyMissing("k"), 3555 ASSERT_EQ(Status::ErrorJwkUnexpectedKty("RSA"),
3537 ImportKeyJwkFromDict(*jwk.get(), 3556 ImportKeyJwkFromDict(*jwk.get(),
3538 CreateRsaHashedImportAlgorithm( 3557 CreateRsaHashedImportAlgorithm(
3539 blink::WebCryptoAlgorithmIdRsaOaep, 3558 blink::WebCryptoAlgorithmIdRsaOaep,
3540 blink::WebCryptoAlgorithmIdSha1), 3559 blink::WebCryptoAlgorithmIdSha1),
3541 true, 3560 true,
3542 blink::WebCryptoKeyUsageEncrypt, 3561 blink::WebCryptoKeyUsageEncrypt,
3543 &public_key)); 3562 &public_key));
3544 } 3563 }
3545 3564
3546 TEST_F(SharedCryptoRsaOaepTest, ExportPublicJwk) { 3565 TEST_F(SharedCryptoRsaOaepTest, ExportPublicJwk) {
(...skipping 852 matching lines...) Expand 10 before | Expand all | Expand 10 after
4399 EXPECT_EQ(public_key_spki, unwrapped_public_key_spki); 4418 EXPECT_EQ(public_key_spki, unwrapped_public_key_spki);
4400 EXPECT_EQ(private_key_pkcs8, unwrapped_private_key_pkcs8); 4419 EXPECT_EQ(private_key_pkcs8, unwrapped_private_key_pkcs8);
4401 4420
4402 EXPECT_NE(public_key_spki, wrapped_public_key); 4421 EXPECT_NE(public_key_spki, wrapped_public_key);
4403 EXPECT_NE(private_key_pkcs8, wrapped_private_key); 4422 EXPECT_NE(private_key_pkcs8, wrapped_private_key);
4404 } 4423 }
4405 4424
4406 } // namespace webcrypto 4425 } // namespace webcrypto
4407 4426
4408 } // namespace content 4427 } // namespace content
OLDNEW
« no previous file with comments | « content/child/webcrypto/shared_crypto.cc ('k') | content/child/webcrypto/status.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698