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 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 const unsigned short kKeyLength[] = {128, 256}; | 254 const unsigned short kKeyLength[] = {128, 256}; |
255 for (size_t key_length_i = 0; key_length_i < ARRAYSIZE_UNSAFE(kKeyLength); | 255 for (size_t key_length_i = 0; key_length_i < ARRAYSIZE_UNSAFE(kKeyLength); |
256 ++key_length_i) { | 256 ++key_length_i) { |
257 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 257 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
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( |
265 GenerateSecretKey( | 265 Status::Success(), |
266 CreateAesCbcKeyGenAlgorithm(kKeyLength[key_length_i]), | 266 GenerateKey(CreateAesCbcKeyGenAlgorithm(kKeyLength[key_length_i]), |
267 true, | 267 true, |
268 0, | 268 0, |
269 &key)); | 269 NULL, |
| 270 &key)); |
270 EXPECT_TRUE(key.handle()); | 271 EXPECT_TRUE(key.handle()); |
271 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | 272 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
272 ASSERT_EQ(Status::Success(), | 273 ASSERT_EQ(Status::Success(), |
273 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_bytes)); | 274 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_bytes)); |
274 EXPECT_EQ(key_bytes.size() * 8, | 275 EXPECT_EQ(key_bytes.size() * 8, |
275 key.algorithm().aesParams()->lengthBits()); | 276 key.algorithm().aesParams()->lengthBits()); |
276 keys.push_back(key_bytes); | 277 keys.push_back(key_bytes); |
277 } | 278 } |
278 // Ensure all entries in the key sample set are unique. This is a simplistic | 279 // Ensure all entries in the key sample set are unique. This is a simplistic |
279 // estimate of whether the generated keys appear random. | 280 // estimate of whether the generated keys appear random. |
280 EXPECT_FALSE(CopiesExist(keys)); | 281 EXPECT_FALSE(CopiesExist(keys)); |
281 } | 282 } |
282 } | 283 } |
283 | 284 |
284 TEST(WebCryptoAesCbcTest, GenerateKeyBadLength) { | 285 TEST(WebCryptoAesCbcTest, GenerateKeyBadLength) { |
285 const unsigned short kKeyLen[] = {0, 127, 257}; | 286 const unsigned short kKeyLen[] = {0, 127, 257}; |
286 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 287 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
287 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kKeyLen); ++i) { | 288 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kKeyLen); ++i) { |
288 SCOPED_TRACE(i); | 289 SCOPED_TRACE(i); |
289 EXPECT_EQ(Status::ErrorGenerateKeyLength(), | 290 EXPECT_EQ( |
290 GenerateSecretKey( | 291 Status::ErrorGenerateKeyLength(), |
291 CreateAesCbcKeyGenAlgorithm(kKeyLen[i]), true, 0, &key)); | 292 GenerateKey( |
| 293 CreateAesCbcKeyGenAlgorithm(kKeyLen[i]), true, 0, NULL, &key)); |
292 } | 294 } |
293 } | 295 } |
294 | 296 |
295 // If key_ops is specified but empty, no key usages are allowed for the key. | 297 // If key_ops is specified but empty, no key usages are allowed for the key. |
296 TEST(WebCryptoAesCbcTest, ImportKeyJwkEmptyKeyOps) { | 298 TEST(WebCryptoAesCbcTest, ImportKeyJwkEmptyKeyOps) { |
297 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 299 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
298 base::DictionaryValue dict; | 300 base::DictionaryValue dict; |
299 dict.SetString("kty", "oct"); | 301 dict.SetString("kty", "oct"); |
300 dict.SetBoolean("ext", false); | 302 dict.SetBoolean("ext", false); |
301 dict.SetString("k", "GADWrMRHwQfoNaXU5fZvTg=="); | 303 dict.SetString("k", "GADWrMRHwQfoNaXU5fZvTg=="); |
(...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
834 ImportKeyJwkFromDict(dict, | 836 ImportKeyJwkFromDict(dict, |
835 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), | 837 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), |
836 false, | 838 false, |
837 blink::WebCryptoKeyUsageEncrypt, | 839 blink::WebCryptoKeyUsageEncrypt, |
838 &key)); | 840 &key)); |
839 } | 841 } |
840 | 842 |
841 // AES 192-bit is not allowed: http://crbug.com/381829 | 843 // AES 192-bit is not allowed: http://crbug.com/381829 |
842 TEST(WebCryptoAesCbcTest, GenerateAesCbc192) { | 844 TEST(WebCryptoAesCbcTest, GenerateAesCbc192) { |
843 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 845 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
844 Status status = GenerateSecretKey(CreateAesCbcKeyGenAlgorithm(192), | 846 Status status = GenerateKey(CreateAesCbcKeyGenAlgorithm(192), |
845 true, | 847 true, |
846 blink::WebCryptoKeyUsageEncrypt, | 848 blink::WebCryptoKeyUsageEncrypt, |
847 &key); | 849 NULL, |
| 850 &key); |
848 ASSERT_EQ(Status::ErrorAes192BitUnsupported(), status); | 851 ASSERT_EQ(Status::ErrorAes192BitUnsupported(), status); |
849 } | 852 } |
850 | 853 |
851 // AES 192-bit is not allowed: http://crbug.com/381829 | 854 // AES 192-bit is not allowed: http://crbug.com/381829 |
852 TEST(WebCryptoAesCbcTest, UnwrapAesCbc192) { | 855 TEST(WebCryptoAesCbcTest, UnwrapAesCbc192) { |
853 std::vector<uint8_t> wrapping_key_data(16, 0); | 856 std::vector<uint8_t> wrapping_key_data(16, 0); |
854 std::vector<uint8_t> wrapped_key = HexStringToBytes( | 857 std::vector<uint8_t> wrapped_key = HexStringToBytes( |
855 "1A07ACAB6C906E50883173C29441DB1DE91D34F45C435B5F99C822867FB3956F"); | 858 "1A07ACAB6C906E50883173C29441DB1DE91D34F45C435B5F99C822867FB3956F"); |
856 | 859 |
857 blink::WebCryptoKey wrapping_key = | 860 blink::WebCryptoKey wrapping_key = |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
907 blink::WebCryptoKeyUsageMask bad_usages[] = { | 910 blink::WebCryptoKeyUsageMask bad_usages[] = { |
908 blink::WebCryptoKeyUsageSign, blink::WebCryptoKeyUsageVerify, | 911 blink::WebCryptoKeyUsageSign, blink::WebCryptoKeyUsageVerify, |
909 blink::WebCryptoKeyUsageDecrypt | blink::WebCryptoKeyUsageVerify, | 912 blink::WebCryptoKeyUsageDecrypt | blink::WebCryptoKeyUsageVerify, |
910 }; | 913 }; |
911 | 914 |
912 for (size_t i = 0; i < arraysize(bad_usages); ++i) { | 915 for (size_t i = 0; i < arraysize(bad_usages); ++i) { |
913 SCOPED_TRACE(i); | 916 SCOPED_TRACE(i); |
914 | 917 |
915 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 918 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
916 | 919 |
917 ASSERT_EQ(Status::ErrorCreateKeyBadUsages(), | 920 ASSERT_EQ( |
918 GenerateSecretKey( | 921 Status::ErrorCreateKeyBadUsages(), |
919 CreateAesCbcKeyGenAlgorithm(128), true, bad_usages[i], &key)); | 922 GenerateKey( |
| 923 CreateAesCbcKeyGenAlgorithm(128), true, bad_usages[i], NULL, &key)); |
920 } | 924 } |
921 } | 925 } |
922 | 926 |
923 // Generate an AES-CBC key and an RSA key pair. Use the AES-CBC key to wrap the | 927 // Generate an AES-CBC key and an RSA key pair. Use the AES-CBC key to wrap the |
924 // key pair (using SPKI format for public key, PKCS8 format for private key). | 928 // key pair (using SPKI format for public key, PKCS8 format for private key). |
925 // Then unwrap the wrapped key pair and verify that the key data is the same. | 929 // Then unwrap the wrapped key pair and verify that the key data is the same. |
926 TEST(WebCryptoAesCbcTest, WrapUnwrapRoundtripSpkiPkcs8) { | 930 TEST(WebCryptoAesCbcTest, WrapUnwrapRoundtripSpkiPkcs8) { |
927 if (!SupportsRsaPrivateKeyImport()) | 931 if (!SupportsRsaPrivateKeyImport()) |
928 return; | 932 return; |
929 | 933 |
930 // Generate the wrapping key. | 934 // Generate the wrapping key. |
931 blink::WebCryptoKey wrapping_key = blink::WebCryptoKey::createNull(); | 935 blink::WebCryptoKey wrapping_key = blink::WebCryptoKey::createNull(); |
932 ASSERT_EQ(Status::Success(), | 936 ASSERT_EQ(Status::Success(), |
933 GenerateSecretKey(CreateAesCbcKeyGenAlgorithm(128), | 937 GenerateKey(CreateAesCbcKeyGenAlgorithm(128), |
934 true, | 938 true, |
935 blink::WebCryptoKeyUsageWrapKey | | 939 blink::WebCryptoKeyUsageWrapKey | |
936 blink::WebCryptoKeyUsageUnwrapKey, | 940 blink::WebCryptoKeyUsageUnwrapKey, |
937 &wrapping_key)); | 941 NULL, |
| 942 &wrapping_key)); |
938 | 943 |
939 // Generate an RSA key pair to be wrapped. | 944 // Generate an RSA key pair to be wrapped. |
940 const unsigned int modulus_length = 256; | 945 const unsigned int modulus_length = 256; |
941 const std::vector<uint8_t> public_exponent = HexStringToBytes("010001"); | 946 const std::vector<uint8_t> public_exponent = HexStringToBytes("010001"); |
942 | 947 |
943 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | 948 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); |
944 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); | 949 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); |
945 ASSERT_EQ(Status::Success(), | 950 ASSERT_EQ(Status::Success(), |
946 GenerateKeyPair(CreateRsaHashedKeyGenAlgorithm( | 951 GenerateKey(CreateRsaHashedKeyGenAlgorithm( |
947 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 952 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
948 blink::WebCryptoAlgorithmIdSha256, | 953 blink::WebCryptoAlgorithmIdSha256, |
949 modulus_length, | 954 modulus_length, |
950 public_exponent), | 955 public_exponent), |
951 true, | 956 true, |
952 0, | 957 0, |
953 &public_key, | 958 &public_key, |
954 &private_key)); | 959 &private_key)); |
955 | 960 |
956 // Export key pair as SPKI + PKCS8 | 961 // Export key pair as SPKI + PKCS8 |
957 std::vector<uint8_t> public_key_spki; | 962 std::vector<uint8_t> public_key_spki; |
958 ASSERT_EQ( | 963 ASSERT_EQ( |
959 Status::Success(), | 964 Status::Success(), |
960 ExportKey(blink::WebCryptoKeyFormatSpki, public_key, &public_key_spki)); | 965 ExportKey(blink::WebCryptoKeyFormatSpki, public_key, &public_key_spki)); |
961 | 966 |
962 std::vector<uint8_t> private_key_pkcs8; | 967 std::vector<uint8_t> private_key_pkcs8; |
963 ASSERT_EQ( | 968 ASSERT_EQ( |
964 Status::Success(), | 969 Status::Success(), |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1032 | 1037 |
1033 EXPECT_NE(public_key_spki, wrapped_public_key); | 1038 EXPECT_NE(public_key_spki, wrapped_public_key); |
1034 EXPECT_NE(private_key_pkcs8, wrapped_private_key); | 1039 EXPECT_NE(private_key_pkcs8, wrapped_private_key); |
1035 } | 1040 } |
1036 | 1041 |
1037 } // namespace | 1042 } // namespace |
1038 | 1043 |
1039 } // namespace webcrypto | 1044 } // namespace webcrypto |
1040 | 1045 |
1041 } // namespace content | 1046 } // namespace content |
OLD | NEW |