| 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 "base/stl_util.h" | 5 #include "base/stl_util.h" |
| 6 #include "content/child/webcrypto/algorithm_dispatch.h" | 6 #include "content/child/webcrypto/algorithm_dispatch.h" |
| 7 #include "content/child/webcrypto/crypto_data.h" | 7 #include "content/child/webcrypto/crypto_data.h" |
| 8 #include "content/child/webcrypto/status.h" | 8 #include "content/child/webcrypto/status.h" |
| 9 #include "content/child/webcrypto/test/test_helpers.h" | 9 #include "content/child/webcrypto/test/test_helpers.h" |
| 10 #include "content/child/webcrypto/webcrypto_util.h" | 10 #include "content/child/webcrypto/webcrypto_util.h" |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 ++key_length_i) { | 256 ++key_length_i) { |
| 257 blink::WebCryptoKey key; | 257 blink::WebCryptoKey key; |
| 258 | 258 |
| 259 std::vector<std::vector<uint8_t> > keys; | 259 std::vector<std::vector<uint8_t> > keys; |
| 260 std::vector<uint8_t> key_bytes; | 260 std::vector<uint8_t> key_bytes; |
| 261 | 261 |
| 262 // Generate a small sample of keys. | 262 // Generate a small sample of keys. |
| 263 for (int j = 0; j < 16; ++j) { | 263 for (int j = 0; j < 16; ++j) { |
| 264 ASSERT_EQ(Status::Success(), | 264 ASSERT_EQ(Status::Success(), |
| 265 GenerateSecretKey( | 265 GenerateSecretKey( |
| 266 CreateAesCbcKeyGenAlgorithm(kKeyLength[key_length_i]), | 266 CreateAesCbcKeyGenAlgorithm(kKeyLength[key_length_i]), true, |
| 267 true, | 267 blink::WebCryptoKeyUsageEncrypt, &key)); |
| 268 0, | |
| 269 &key)); | |
| 270 EXPECT_TRUE(key.handle()); | 268 EXPECT_TRUE(key.handle()); |
| 271 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | 269 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
| 272 ASSERT_EQ(Status::Success(), | 270 ASSERT_EQ(Status::Success(), |
| 273 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_bytes)); | 271 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_bytes)); |
| 274 EXPECT_EQ(key_bytes.size() * 8, | 272 EXPECT_EQ(key_bytes.size() * 8, |
| 275 key.algorithm().aesParams()->lengthBits()); | 273 key.algorithm().aesParams()->lengthBits()); |
| 276 keys.push_back(key_bytes); | 274 keys.push_back(key_bytes); |
| 277 } | 275 } |
| 278 // Ensure all entries in the key sample set are unique. This is a simplistic | 276 // Ensure all entries in the key sample set are unique. This is a simplistic |
| 279 // estimate of whether the generated keys appear random. | 277 // estimate of whether the generated keys appear random. |
| (...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 955 SCOPED_TRACE(i); | 953 SCOPED_TRACE(i); |
| 956 | 954 |
| 957 blink::WebCryptoKey key; | 955 blink::WebCryptoKey key; |
| 958 | 956 |
| 959 ASSERT_EQ(Status::ErrorCreateKeyBadUsages(), | 957 ASSERT_EQ(Status::ErrorCreateKeyBadUsages(), |
| 960 GenerateSecretKey( | 958 GenerateSecretKey( |
| 961 CreateAesCbcKeyGenAlgorithm(128), true, bad_usages[i], &key)); | 959 CreateAesCbcKeyGenAlgorithm(128), true, bad_usages[i], &key)); |
| 962 } | 960 } |
| 963 } | 961 } |
| 964 | 962 |
| 963 // Generate an AES-CBC key with no usages. |
| 964 TEST(WebCryptoAesCbcTest, GenerateKeyEmptyUsages) { |
| 965 blink::WebCryptoKey key; |
| 966 |
| 967 ASSERT_EQ(Status::ErrorCreateKeyEmptyUsages(), |
| 968 GenerateSecretKey(CreateAesCbcKeyGenAlgorithm(128), true, 0, &key)); |
| 969 } |
| 970 |
| 965 // Generate an AES-CBC key and an RSA key pair. Use the AES-CBC key to wrap the | 971 // Generate an AES-CBC key and an RSA key pair. Use the AES-CBC key to wrap the |
| 966 // key pair (using SPKI format for public key, PKCS8 format for private key). | 972 // key pair (using SPKI format for public key, PKCS8 format for private key). |
| 967 // Then unwrap the wrapped key pair and verify that the key data is the same. | 973 // Then unwrap the wrapped key pair and verify that the key data is the same. |
| 968 TEST(WebCryptoAesCbcTest, WrapUnwrapRoundtripSpkiPkcs8) { | 974 TEST(WebCryptoAesCbcTest, WrapUnwrapRoundtripSpkiPkcs8) { |
| 969 if (!SupportsRsaPrivateKeyImport()) | 975 if (!SupportsRsaPrivateKeyImport()) |
| 970 return; | 976 return; |
| 971 | 977 |
| 972 // Generate the wrapping key. | 978 // Generate the wrapping key. |
| 973 blink::WebCryptoKey wrapping_key; | 979 blink::WebCryptoKey wrapping_key; |
| 974 ASSERT_EQ(Status::Success(), | 980 ASSERT_EQ(Status::Success(), |
| 975 GenerateSecretKey(CreateAesCbcKeyGenAlgorithm(128), | 981 GenerateSecretKey(CreateAesCbcKeyGenAlgorithm(128), |
| 976 true, | 982 true, |
| 977 blink::WebCryptoKeyUsageWrapKey | | 983 blink::WebCryptoKeyUsageWrapKey | |
| 978 blink::WebCryptoKeyUsageUnwrapKey, | 984 blink::WebCryptoKeyUsageUnwrapKey, |
| 979 &wrapping_key)); | 985 &wrapping_key)); |
| 980 | 986 |
| 981 // Generate an RSA key pair to be wrapped. | 987 // Generate an RSA key pair to be wrapped. |
| 982 const unsigned int modulus_length = 256; | 988 const unsigned int modulus_length = 256; |
| 983 const std::vector<uint8_t> public_exponent = HexStringToBytes("010001"); | 989 const std::vector<uint8_t> public_exponent = HexStringToBytes("010001"); |
| 984 | 990 |
| 985 blink::WebCryptoKey public_key; | 991 blink::WebCryptoKey public_key; |
| 986 blink::WebCryptoKey private_key; | 992 blink::WebCryptoKey private_key; |
| 987 ASSERT_EQ(Status::Success(), | 993 ASSERT_EQ(Status::Success(), |
| 988 GenerateKeyPair(CreateRsaHashedKeyGenAlgorithm( | 994 GenerateKeyPair(CreateRsaHashedKeyGenAlgorithm( |
| 989 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 995 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 990 blink::WebCryptoAlgorithmIdSha256, | 996 blink::WebCryptoAlgorithmIdSha256, |
| 991 modulus_length, | 997 modulus_length, public_exponent), |
| 992 public_exponent), | 998 true, blink::WebCryptoKeyUsageSign, &public_key, |
| 993 true, | |
| 994 0, | |
| 995 &public_key, | |
| 996 &private_key)); | 999 &private_key)); |
| 997 | 1000 |
| 998 // Export key pair as SPKI + PKCS8 | 1001 // Export key pair as SPKI + PKCS8 |
| 999 std::vector<uint8_t> public_key_spki; | 1002 std::vector<uint8_t> public_key_spki; |
| 1000 ASSERT_EQ( | 1003 ASSERT_EQ( |
| 1001 Status::Success(), | 1004 Status::Success(), |
| 1002 ExportKey(blink::WebCryptoKeyFormatSpki, public_key, &public_key_spki)); | 1005 ExportKey(blink::WebCryptoKeyFormatSpki, public_key, &public_key_spki)); |
| 1003 | 1006 |
| 1004 std::vector<uint8_t> private_key_pkcs8; | 1007 std::vector<uint8_t> private_key_pkcs8; |
| 1005 ASSERT_EQ( | 1008 ASSERT_EQ( |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1039 CryptoData(wrapped_public_key), | 1042 CryptoData(wrapped_public_key), |
| 1040 wrapping_key, | 1043 wrapping_key, |
| 1041 wrap_algorithm, | 1044 wrap_algorithm, |
| 1042 rsa_import_algorithm, | 1045 rsa_import_algorithm, |
| 1043 true, | 1046 true, |
| 1044 0, | 1047 0, |
| 1045 &unwrapped_public_key)); | 1048 &unwrapped_public_key)); |
| 1046 | 1049 |
| 1047 blink::WebCryptoKey unwrapped_private_key; | 1050 blink::WebCryptoKey unwrapped_private_key; |
| 1048 | 1051 |
| 1049 ASSERT_EQ(Status::Success(), | 1052 ASSERT_EQ( |
| 1050 UnwrapKey(blink::WebCryptoKeyFormatPkcs8, | 1053 Status::Success(), |
| 1051 CryptoData(wrapped_private_key), | 1054 UnwrapKey(blink::WebCryptoKeyFormatPkcs8, CryptoData(wrapped_private_key), |
| 1052 wrapping_key, | 1055 wrapping_key, wrap_algorithm, rsa_import_algorithm, true, |
| 1053 wrap_algorithm, | 1056 blink::WebCryptoKeyUsageSign, &unwrapped_private_key)); |
| 1054 rsa_import_algorithm, | |
| 1055 true, | |
| 1056 0, | |
| 1057 &unwrapped_private_key)); | |
| 1058 | 1057 |
| 1059 // Export unwrapped key pair as SPKI + PKCS8 | 1058 // Export unwrapped key pair as SPKI + PKCS8 |
| 1060 std::vector<uint8_t> unwrapped_public_key_spki; | 1059 std::vector<uint8_t> unwrapped_public_key_spki; |
| 1061 ASSERT_EQ(Status::Success(), | 1060 ASSERT_EQ(Status::Success(), |
| 1062 ExportKey(blink::WebCryptoKeyFormatSpki, | 1061 ExportKey(blink::WebCryptoKeyFormatSpki, |
| 1063 unwrapped_public_key, | 1062 unwrapped_public_key, |
| 1064 &unwrapped_public_key_spki)); | 1063 &unwrapped_public_key_spki)); |
| 1065 | 1064 |
| 1066 std::vector<uint8_t> unwrapped_private_key_pkcs8; | 1065 std::vector<uint8_t> unwrapped_private_key_pkcs8; |
| 1067 ASSERT_EQ(Status::Success(), | 1066 ASSERT_EQ(Status::Success(), |
| 1068 ExportKey(blink::WebCryptoKeyFormatPkcs8, | 1067 ExportKey(blink::WebCryptoKeyFormatPkcs8, |
| 1069 unwrapped_private_key, | 1068 unwrapped_private_key, |
| 1070 &unwrapped_private_key_pkcs8)); | 1069 &unwrapped_private_key_pkcs8)); |
| 1071 | 1070 |
| 1072 EXPECT_EQ(public_key_spki, unwrapped_public_key_spki); | 1071 EXPECT_EQ(public_key_spki, unwrapped_public_key_spki); |
| 1073 EXPECT_EQ(private_key_pkcs8, unwrapped_private_key_pkcs8); | 1072 EXPECT_EQ(private_key_pkcs8, unwrapped_private_key_pkcs8); |
| 1074 | 1073 |
| 1075 EXPECT_NE(public_key_spki, wrapped_public_key); | 1074 EXPECT_NE(public_key_spki, wrapped_public_key); |
| 1076 EXPECT_NE(private_key_pkcs8, wrapped_private_key); | 1075 EXPECT_NE(private_key_pkcs8, wrapped_private_key); |
| 1077 } | 1076 } |
| 1078 | 1077 |
| 1079 } // namespace | 1078 } // namespace |
| 1080 | 1079 |
| 1081 } // namespace webcrypto | 1080 } // namespace webcrypto |
| 1082 | 1081 |
| 1083 } // namespace content | 1082 } // namespace content |
| OLD | NEW |