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 "content/child/webcrypto/algorithm_dispatch.h" |
8 #include "content/child/webcrypto/crypto_data.h" | 8 #include "content/child/webcrypto/crypto_data.h" |
9 #include "content/child/webcrypto/status.h" | 9 #include "content/child/webcrypto/status.h" |
10 #include "content/child/webcrypto/test/test_helpers.h" | 10 #include "content/child/webcrypto/test/test_helpers.h" |
(...skipping 797 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
808 EXPECT_EQ(blink::WebCryptoKeyUsageSign, private_key.usages()); | 808 EXPECT_EQ(blink::WebCryptoKeyUsageSign, private_key.usages()); |
809 } | 809 } |
810 | 810 |
811 TEST(WebCryptoRsaSsaTest, GenerateKeyPairEmptyUsages) { | 811 TEST(WebCryptoRsaSsaTest, GenerateKeyPairEmptyUsages) { |
812 const unsigned int modulus_length = 256; | 812 const unsigned int modulus_length = 256; |
813 const std::vector<uint8_t> public_exponent = HexStringToBytes("010001"); | 813 const std::vector<uint8_t> public_exponent = HexStringToBytes("010001"); |
814 | 814 |
815 blink::WebCryptoKey public_key; | 815 blink::WebCryptoKey public_key; |
816 blink::WebCryptoKey private_key; | 816 blink::WebCryptoKey private_key; |
817 | 817 |
818 ASSERT_EQ(Status::ErrorCreateKeyEmptyUsages(), | 818 ASSERT_EQ(Status::ErrorKeyEmptyUsages(), |
819 GenerateKeyPair(CreateRsaHashedKeyGenAlgorithm( | 819 GenerateKeyPair(CreateRsaHashedKeyGenAlgorithm( |
820 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 820 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
821 blink::WebCryptoAlgorithmIdSha256, | 821 blink::WebCryptoAlgorithmIdSha256, |
822 modulus_length, public_exponent), | 822 modulus_length, public_exponent), |
823 true, 0, &public_key, &private_key)); | 823 true, 0, &public_key, &private_key)); |
824 } | 824 } |
825 | 825 |
| 826 TEST(WebCryptoRsaSsaTest, ImportKeyEmptyUsages) { |
| 827 blink::WebCryptoKey public_key; |
| 828 blink::WebCryptoKey private_key; |
| 829 blink::WebCryptoKey key; |
| 830 |
| 831 // Public without usage does not throw an error. |
| 832 ASSERT_EQ(Status::Success(), |
| 833 ImportKey(blink::WebCryptoKeyFormatSpki, |
| 834 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)), |
| 835 CreateRsaHashedImportAlgorithm( |
| 836 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 837 blink::WebCryptoAlgorithmIdSha256), |
| 838 true, 0, &public_key)); |
| 839 |
| 840 // Private empty usage will throw an error. |
| 841 ASSERT_EQ(Status::ErrorKeyEmptyUsages(), |
| 842 ImportKey(blink::WebCryptoKeyFormatPkcs8, |
| 843 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)), |
| 844 CreateRsaHashedImportAlgorithm( |
| 845 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 846 blink::WebCryptoAlgorithmIdSha1), |
| 847 true, 0, &private_key)); |
| 848 |
| 849 std::vector<uint8_t> public_jwk; |
| 850 ASSERT_EQ(Status::Success(), |
| 851 ExportKey(blink::WebCryptoKeyFormatJwk, public_key, &public_jwk)); |
| 852 |
| 853 ASSERT_EQ(Status::Success(), |
| 854 ImportKey(blink::WebCryptoKeyFormatJwk, |
| 855 CryptoData(public_jwk), |
| 856 CreateRsaHashedImportAlgorithm( |
| 857 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 858 blink::WebCryptoAlgorithmIdSha256), |
| 859 true, 0, &key)); |
| 860 |
| 861 // With correct usage to get correct imported private_key |
| 862 std::vector<uint8_t> private_jwk; |
| 863 ImportKey(blink::WebCryptoKeyFormatPkcs8, |
| 864 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)), |
| 865 CreateRsaHashedImportAlgorithm( |
| 866 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 867 blink::WebCryptoAlgorithmIdSha1), |
| 868 true, blink::WebCryptoKeyUsageSign, &private_key); |
| 869 |
| 870 ASSERT_EQ(Status::Success(), |
| 871 ExportKey(blink::WebCryptoKeyFormatJwk, private_key, &private_jwk)); |
| 872 |
| 873 ASSERT_EQ(Status::ErrorKeyEmptyUsages(), |
| 874 ImportKey(blink::WebCryptoKeyFormatJwk, |
| 875 CryptoData(private_jwk), |
| 876 CreateRsaHashedImportAlgorithm( |
| 877 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 878 blink::WebCryptoAlgorithmIdSha1), |
| 879 true, 0, &key)); |
| 880 } |
| 881 |
826 TEST(WebCryptoRsaSsaTest, ImportExportJwkRsaPublicKey) { | 882 TEST(WebCryptoRsaSsaTest, ImportExportJwkRsaPublicKey) { |
827 struct TestCase { | 883 struct TestCase { |
828 const blink::WebCryptoAlgorithmId hash; | 884 const blink::WebCryptoAlgorithmId hash; |
829 const blink::WebCryptoKeyUsageMask usage; | 885 const blink::WebCryptoKeyUsageMask usage; |
830 const char* const jwk_alg; | 886 const char* const jwk_alg; |
831 }; | 887 }; |
832 const TestCase kTests[] = { | 888 const TestCase kTests[] = { |
833 {blink::WebCryptoAlgorithmIdSha1, blink::WebCryptoKeyUsageVerify, "RS1"}, | 889 {blink::WebCryptoAlgorithmIdSha1, blink::WebCryptoKeyUsageVerify, "RS1"}, |
834 {blink::WebCryptoAlgorithmIdSha256, | 890 {blink::WebCryptoAlgorithmIdSha256, |
835 blink::WebCryptoKeyUsageVerify, | 891 blink::WebCryptoKeyUsageVerify, |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
984 true, usages, &key); | 1040 true, usages, &key); |
985 EXPECT_EQ(test_error, StatusToString(status)); | 1041 EXPECT_EQ(test_error, StatusToString(status)); |
986 } | 1042 } |
987 } | 1043 } |
988 | 1044 |
989 } // namespace | 1045 } // namespace |
990 | 1046 |
991 } // namespace webcrypto | 1047 } // namespace webcrypto |
992 | 1048 |
993 } // namespace content | 1049 } // namespace content |
OLD | NEW |