| 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/logging.h" | 5 #include "base/logging.h" |
| 6 #include "base/stl_util.h" | 6 #include "base/stl_util.h" |
| 7 #include "content/child/webcrypto/algorithm_dispatch.h" | 7 #include "components/webcrypto/algorithm_dispatch.h" |
| 8 #include "content/child/webcrypto/crypto_data.h" | 8 #include "components/webcrypto/crypto_data.h" |
| 9 #include "content/child/webcrypto/status.h" | 9 #include "components/webcrypto/status.h" |
| 10 #include "content/child/webcrypto/test/test_helpers.h" | 10 #include "components/webcrypto/test/test_helpers.h" |
| 11 #include "content/child/webcrypto/webcrypto_util.h" | 11 #include "components/webcrypto/webcrypto_util.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 13 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 14 #include "third_party/WebKit/public/platform/WebCryptoKey.h" | 14 #include "third_party/WebKit/public/platform/WebCryptoKey.h" |
| 15 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | 15 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
| 16 | 16 |
| 17 namespace content { | 17 namespace components { |
| 18 | 18 |
| 19 namespace webcrypto { | 19 namespace webcrypto { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 // Helper for ImportJwkRsaFailures. Restores the JWK JSON | 23 // Helper for ImportJwkRsaFailures. Restores the JWK JSON |
| 24 // dictionary to a good state | 24 // dictionary to a good state |
| 25 void RestoreJwkRsaDictionary(base::DictionaryValue* dict) { | 25 void RestoreJwkRsaDictionary(base::DictionaryValue* dict) { |
| 26 dict->Clear(); | 26 dict->Clear(); |
| 27 dict->SetString("kty", "RSA"); | 27 dict->SetString("kty", "RSA"); |
| (...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 blink::WebCryptoAlgorithmIdSha256, modulus_length, | 412 blink::WebCryptoAlgorithmIdSha256, modulus_length, |
| 413 exponent_with_leading_zeros); | 413 exponent_with_leading_zeros); |
| 414 EXPECT_EQ(Status::ErrorGenerateKeyPublicExponent(), | 414 EXPECT_EQ(Status::ErrorGenerateKeyPublicExponent(), |
| 415 GenerateKeyPair(algorithm, extractable, usages, &public_key, | 415 GenerateKeyPair(algorithm, extractable, usages, &public_key, |
| 416 &private_key)); | 416 &private_key)); |
| 417 | 417 |
| 418 // Key generation success using exponent with leading zeros. | 418 // Key generation success using exponent with leading zeros. |
| 419 exponent_with_leading_zeros.insert(exponent_with_leading_zeros.end(), | 419 exponent_with_leading_zeros.insert(exponent_with_leading_zeros.end(), |
| 420 public_exponent.begin(), | 420 public_exponent.begin(), |
| 421 public_exponent.end()); | 421 public_exponent.end()); |
| 422 algorithm = | 422 algorithm = CreateRsaHashedKeyGenAlgorithm( |
| 423 CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 423 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 424 blink::WebCryptoAlgorithmIdSha256, | 424 blink::WebCryptoAlgorithmIdSha256, modulus_length, |
| 425 modulus_length, | 425 exponent_with_leading_zeros); |
| 426 exponent_with_leading_zeros); | |
| 427 EXPECT_EQ(Status::Success(), GenerateKeyPair(algorithm, extractable, usages, | 426 EXPECT_EQ(Status::Success(), GenerateKeyPair(algorithm, extractable, usages, |
| 428 &public_key, &private_key)); | 427 &public_key, &private_key)); |
| 429 EXPECT_FALSE(public_key.isNull()); | 428 EXPECT_FALSE(public_key.isNull()); |
| 430 EXPECT_FALSE(private_key.isNull()); | 429 EXPECT_FALSE(private_key.isNull()); |
| 431 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); | 430 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); |
| 432 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); | 431 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); |
| 433 EXPECT_TRUE(public_key.extractable()); | 432 EXPECT_TRUE(public_key.extractable()); |
| 434 EXPECT_EQ(extractable, private_key.extractable()); | 433 EXPECT_EQ(extractable, private_key.extractable()); |
| 435 EXPECT_EQ(public_usages, public_key.usages()); | 434 EXPECT_EQ(public_usages, public_key.usages()); |
| 436 EXPECT_EQ(private_usages, private_key.usages()); | 435 EXPECT_EQ(private_usages, private_key.usages()); |
| 437 | 436 |
| 438 // Successful WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 key generation (sha1) | 437 // Successful WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 key generation (sha1) |
| 439 algorithm = | 438 algorithm = CreateRsaHashedKeyGenAlgorithm( |
| 440 CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 439 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 441 blink::WebCryptoAlgorithmIdSha1, | 440 blink::WebCryptoAlgorithmIdSha1, modulus_length, public_exponent); |
| 442 modulus_length, | 441 EXPECT_EQ(Status::Success(), GenerateKeyPair(algorithm, false, usages, |
| 443 public_exponent); | 442 &public_key, &private_key)); |
| 444 EXPECT_EQ( | |
| 445 Status::Success(), | |
| 446 GenerateKeyPair(algorithm, false, usages, &public_key, &private_key)); | |
| 447 EXPECT_FALSE(public_key.isNull()); | 443 EXPECT_FALSE(public_key.isNull()); |
| 448 EXPECT_FALSE(private_key.isNull()); | 444 EXPECT_FALSE(private_key.isNull()); |
| 449 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); | 445 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); |
| 450 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); | 446 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); |
| 451 EXPECT_EQ(modulus_length, | 447 EXPECT_EQ(modulus_length, |
| 452 public_key.algorithm().rsaHashedParams()->modulusLengthBits()); | 448 public_key.algorithm().rsaHashedParams()->modulusLengthBits()); |
| 453 EXPECT_EQ(modulus_length, | 449 EXPECT_EQ(modulus_length, |
| 454 private_key.algorithm().rsaHashedParams()->modulusLengthBits()); | 450 private_key.algorithm().rsaHashedParams()->modulusLengthBits()); |
| 455 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, | 451 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, |
| 456 public_key.algorithm().rsaHashedParams()->hash().id()); | 452 public_key.algorithm().rsaHashedParams()->hash().id()); |
| (...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 847 CreateRsaHashedImportAlgorithm( | 843 CreateRsaHashedImportAlgorithm( |
| 848 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 844 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 849 blink::WebCryptoAlgorithmIdSha1), | 845 blink::WebCryptoAlgorithmIdSha1), |
| 850 true, 0, &private_key)); | 846 true, 0, &private_key)); |
| 851 | 847 |
| 852 std::vector<uint8_t> public_jwk; | 848 std::vector<uint8_t> public_jwk; |
| 853 ASSERT_EQ(Status::Success(), | 849 ASSERT_EQ(Status::Success(), |
| 854 ExportKey(blink::WebCryptoKeyFormatJwk, public_key, &public_jwk)); | 850 ExportKey(blink::WebCryptoKeyFormatJwk, public_key, &public_jwk)); |
| 855 | 851 |
| 856 ASSERT_EQ(Status::Success(), | 852 ASSERT_EQ(Status::Success(), |
| 857 ImportKey(blink::WebCryptoKeyFormatJwk, | 853 ImportKey(blink::WebCryptoKeyFormatJwk, CryptoData(public_jwk), |
| 858 CryptoData(public_jwk), | |
| 859 CreateRsaHashedImportAlgorithm( | 854 CreateRsaHashedImportAlgorithm( |
| 860 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 855 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 861 blink::WebCryptoAlgorithmIdSha256), | 856 blink::WebCryptoAlgorithmIdSha256), |
| 862 true, 0, &public_key)); | 857 true, 0, &public_key)); |
| 863 EXPECT_EQ(0, public_key.usages()); | 858 EXPECT_EQ(0, public_key.usages()); |
| 864 | 859 |
| 865 // With correct usage to get correct imported private_key | 860 // With correct usage to get correct imported private_key |
| 866 std::vector<uint8_t> private_jwk; | 861 std::vector<uint8_t> private_jwk; |
| 867 ImportKey(blink::WebCryptoKeyFormatPkcs8, | 862 ImportKey( |
| 868 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)), | 863 blink::WebCryptoKeyFormatPkcs8, |
| 869 CreateRsaHashedImportAlgorithm( | 864 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)), |
| 870 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 865 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 871 blink::WebCryptoAlgorithmIdSha1), | 866 blink::WebCryptoAlgorithmIdSha1), |
| 872 true, blink::WebCryptoKeyUsageSign, &private_key); | 867 true, blink::WebCryptoKeyUsageSign, &private_key); |
| 873 | 868 |
| 874 ASSERT_EQ(Status::Success(), | 869 ASSERT_EQ(Status::Success(), |
| 875 ExportKey(blink::WebCryptoKeyFormatJwk, private_key, &private_jwk)); | 870 ExportKey(blink::WebCryptoKeyFormatJwk, private_key, &private_jwk)); |
| 876 | 871 |
| 877 ASSERT_EQ(Status::ErrorCreateKeyEmptyUsages(), | 872 ASSERT_EQ(Status::ErrorCreateKeyEmptyUsages(), |
| 878 ImportKey(blink::WebCryptoKeyFormatJwk, | 873 ImportKey(blink::WebCryptoKeyFormatJwk, CryptoData(private_jwk), |
| 879 CryptoData(private_jwk), | 874 CreateRsaHashedImportAlgorithm( |
| 880 CreateRsaHashedImportAlgorithm( | 875 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 881 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 876 blink::WebCryptoAlgorithmIdSha1), |
| 882 blink::WebCryptoAlgorithmIdSha1), | 877 true, 0, &private_key)); |
| 883 true, 0, &private_key)); | |
| 884 } | 878 } |
| 885 | 879 |
| 886 TEST(WebCryptoRsaSsaTest, ImportExportJwkRsaPublicKey) { | 880 TEST(WebCryptoRsaSsaTest, ImportExportJwkRsaPublicKey) { |
| 887 struct TestCase { | 881 struct TestCase { |
| 888 const blink::WebCryptoAlgorithmId hash; | 882 const blink::WebCryptoAlgorithmId hash; |
| 889 const blink::WebCryptoKeyUsageMask usage; | 883 const blink::WebCryptoKeyUsageMask usage; |
| 890 const char* const jwk_alg; | 884 const char* const jwk_alg; |
| 891 }; | 885 }; |
| 892 const TestCase kTests[] = { | 886 const TestCase kTests[] = { |
| 893 {blink::WebCryptoAlgorithmIdSha1, blink::WebCryptoKeyUsageVerify, "RS1"}, | 887 {blink::WebCryptoAlgorithmIdSha1, blink::WebCryptoKeyUsageVerify, "RS1"}, |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1043 blink::WebCryptoAlgorithmIdSha256), | 1037 blink::WebCryptoAlgorithmIdSha256), |
| 1044 true, usages, &key); | 1038 true, usages, &key); |
| 1045 EXPECT_EQ(test_error, StatusToString(status)); | 1039 EXPECT_EQ(test_error, StatusToString(status)); |
| 1046 } | 1040 } |
| 1047 } | 1041 } |
| 1048 | 1042 |
| 1049 } // namespace | 1043 } // namespace |
| 1050 | 1044 |
| 1051 } // namespace webcrypto | 1045 } // namespace webcrypto |
| 1052 | 1046 |
| 1053 } // namespace content | 1047 } // namespace components |
| OLD | NEW |