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