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 "content/child/webcrypto/shared_crypto.h" | 5 #include "content/child/webcrypto/shared_crypto.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 a.error_details() == b.error_details(); | 60 a.error_details() == b.error_details(); |
61 } | 61 } |
62 | 62 |
63 bool operator!=(const content::webcrypto::Status& a, | 63 bool operator!=(const content::webcrypto::Status& a, |
64 const content::webcrypto::Status& b) { | 64 const content::webcrypto::Status& b) { |
65 return !(a == b); | 65 return !(a == b); |
66 } | 66 } |
67 | 67 |
68 namespace { | 68 namespace { |
69 | 69 |
| 70 // ----------------------------------------------------------------------------- |
| 71 // TODO(eroman): Remove these helpers and convert all of the tests to using the |
| 72 // std::vector<> flavor of functions directly. |
| 73 // ----------------------------------------------------------------------------- |
| 74 |
| 75 blink::WebArrayBuffer CreateArrayBuffer(const uint8* data, |
| 76 unsigned int data_size) { |
| 77 blink::WebArrayBuffer buffer = blink::WebArrayBuffer::create(data_size, 1); |
| 78 DCHECK(!buffer.isNull()); |
| 79 if (data_size) // data_size == 0 might mean the data pointer is invalid |
| 80 memcpy(buffer.data(), data, data_size); |
| 81 return buffer; |
| 82 } |
| 83 |
| 84 void AssignWebArrayBuffer(const std::vector<uint8>& in, |
| 85 blink::WebArrayBuffer* out) { |
| 86 *out = CreateArrayBuffer(Uint8VectorStart(in), in.size()); |
| 87 } |
| 88 |
| 89 Status Encrypt(const blink::WebCryptoAlgorithm& algorithm, |
| 90 const blink::WebCryptoKey& key, |
| 91 const CryptoData& data, |
| 92 blink::WebArrayBuffer* web_buffer) { |
| 93 std::vector<uint8> buffer; |
| 94 Status status = Encrypt(algorithm, key, data, &buffer); |
| 95 AssignWebArrayBuffer(buffer, web_buffer); |
| 96 return status; |
| 97 } |
| 98 |
| 99 Status Decrypt(const blink::WebCryptoAlgorithm& algorithm, |
| 100 const blink::WebCryptoKey& key, |
| 101 const CryptoData& data, |
| 102 blink::WebArrayBuffer* web_buffer) { |
| 103 std::vector<uint8> buffer; |
| 104 Status status = Decrypt(algorithm, key, data, &buffer); |
| 105 AssignWebArrayBuffer(buffer, web_buffer); |
| 106 return status; |
| 107 } |
| 108 |
| 109 Status Digest(const blink::WebCryptoAlgorithm& algorithm, |
| 110 const CryptoData& data, |
| 111 blink::WebArrayBuffer* web_buffer) { |
| 112 std::vector<uint8> buffer; |
| 113 Status status = Digest(algorithm, data, &buffer); |
| 114 AssignWebArrayBuffer(buffer, web_buffer); |
| 115 return status; |
| 116 } |
| 117 |
| 118 Status ExportKey(blink::WebCryptoKeyFormat format, |
| 119 const blink::WebCryptoKey& key, |
| 120 blink::WebArrayBuffer* web_buffer) { |
| 121 std::vector<uint8> buffer; |
| 122 Status status = webcrypto::ExportKey(format, key, &buffer); |
| 123 AssignWebArrayBuffer(buffer, web_buffer); |
| 124 return status; |
| 125 } |
| 126 |
| 127 Status Sign(const blink::WebCryptoAlgorithm& algorithm, |
| 128 const blink::WebCryptoKey& key, |
| 129 const CryptoData& data, |
| 130 blink::WebArrayBuffer* web_buffer) { |
| 131 std::vector<uint8> buffer; |
| 132 |
| 133 Status status = Sign(algorithm, key, data, &buffer); |
| 134 AssignWebArrayBuffer(buffer, web_buffer); |
| 135 return status; |
| 136 } |
| 137 |
| 138 Status WrapKey(blink::WebCryptoKeyFormat format, |
| 139 const blink::WebCryptoKey& wrapping_key, |
| 140 const blink::WebCryptoKey& key_to_wrap, |
| 141 const blink::WebCryptoAlgorithm& wrapping_algorithm, |
| 142 blink::WebArrayBuffer* web_buffer) { |
| 143 std::vector<uint8> buffer; |
| 144 |
| 145 Status status = webcrypto::WrapKey( |
| 146 format, wrapping_key, key_to_wrap, wrapping_algorithm, &buffer); |
| 147 AssignWebArrayBuffer(buffer, web_buffer); |
| 148 return status; |
| 149 } |
| 150 |
| 151 // ----------------------------------------------------------------------------- |
| 152 |
70 // TODO(eroman): For Linux builds using system NSS, AES-GCM support is a | 153 // TODO(eroman): For Linux builds using system NSS, AES-GCM support is a |
71 // runtime dependency. Test it by trying to import a key. | 154 // runtime dependency. Test it by trying to import a key. |
72 // TODO(padolph): Consider caching the result of the import key test. | 155 // TODO(padolph): Consider caching the result of the import key test. |
73 bool SupportsAesGcm() { | 156 bool SupportsAesGcm() { |
74 std::vector<uint8> key_raw(16, 0); | 157 std::vector<uint8> key_raw(16, 0); |
75 | 158 |
76 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 159 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
77 Status status = ImportKey(blink::WebCryptoKeyFormatRaw, | 160 Status status = ImportKey(blink::WebCryptoKeyFormatRaw, |
78 CryptoData(key_raw), | 161 CryptoData(key_raw), |
79 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm), | 162 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm), |
(...skipping 1788 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1868 EXPECT_EQ(test.usage, key.usages()); | 1951 EXPECT_EQ(test.usage, key.usages()); |
1869 | 1952 |
1870 // Export the key in raw format and compare to the original. | 1953 // Export the key in raw format and compare to the original. |
1871 blink::WebArrayBuffer key_raw_out; | 1954 blink::WebArrayBuffer key_raw_out; |
1872 ASSERT_EQ(Status::Success(), | 1955 ASSERT_EQ(Status::Success(), |
1873 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_raw_out)); | 1956 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_raw_out)); |
1874 ExpectArrayBufferMatchesHex(test.key_hex, key_raw_out); | 1957 ExpectArrayBufferMatchesHex(test.key_hex, key_raw_out); |
1875 } | 1958 } |
1876 } | 1959 } |
1877 | 1960 |
| 1961 TEST_F(SharedCryptoTest, MAYBE(ExportJwkEmptySymmetricKey)) { |
| 1962 const blink::WebCryptoAlgorithm import_algorithm = |
| 1963 webcrypto::CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1); |
| 1964 |
| 1965 blink::WebCryptoKeyUsageMask usages = blink::WebCryptoKeyUsageSign; |
| 1966 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 1967 |
| 1968 // Import a zero-byte HMAC key. |
| 1969 const char key_data_hex[] = ""; |
| 1970 key = ImportSecretKeyFromRaw( |
| 1971 HexStringToBytes(key_data_hex), import_algorithm, usages); |
| 1972 EXPECT_EQ(0u, key.algorithm().hmacParams()->lengthBits()); |
| 1973 |
| 1974 // Export the key in JWK format and validate. |
| 1975 blink::WebArrayBuffer json; |
| 1976 ASSERT_EQ(Status::Success(), |
| 1977 ExportKey(blink::WebCryptoKeyFormatJwk, key, &json)); |
| 1978 EXPECT_TRUE(VerifySecretJwk(json, "HS1", key_data_hex, usages)); |
| 1979 |
| 1980 // Now try re-importing the JWK key. |
| 1981 key = blink::WebCryptoKey::createNull(); |
| 1982 EXPECT_EQ(Status::Success(), |
| 1983 ImportKey(blink::WebCryptoKeyFormatJwk, |
| 1984 CryptoData(json), |
| 1985 import_algorithm, |
| 1986 true, |
| 1987 usages, |
| 1988 &key)); |
| 1989 |
| 1990 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
| 1991 EXPECT_EQ(0u, key.algorithm().hmacParams()->lengthBits()); |
| 1992 |
| 1993 blink::WebArrayBuffer exported_key_data; |
| 1994 EXPECT_EQ(Status::Success(), |
| 1995 ExportKey(blink::WebCryptoKeyFormatRaw, key, &exported_key_data)); |
| 1996 |
| 1997 EXPECT_EQ(0u, exported_key_data.byteLength()); |
| 1998 } |
| 1999 |
1878 TEST_F(SharedCryptoTest, MAYBE(ImportExportSpki)) { | 2000 TEST_F(SharedCryptoTest, MAYBE(ImportExportSpki)) { |
1879 // Passing case: Import a valid RSA key in SPKI format. | 2001 // Passing case: Import a valid RSA key in SPKI format. |
1880 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 2002 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
1881 ASSERT_EQ( | 2003 ASSERT_EQ( |
1882 Status::Success(), | 2004 Status::Success(), |
1883 ImportKey(blink::WebCryptoKeyFormatSpki, | 2005 ImportKey(blink::WebCryptoKeyFormatSpki, |
1884 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)), | 2006 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)), |
1885 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5), | 2007 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5), |
1886 true, | 2008 true, |
1887 blink::WebCryptoKeyUsageEncrypt, | 2009 blink::WebCryptoKeyUsageEncrypt, |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2121 algorithm, extractable, usage_mask, &public_key, &private_key)); | 2243 algorithm, extractable, usage_mask, &public_key, &private_key)); |
2122 EXPECT_FALSE(public_key.isNull()); | 2244 EXPECT_FALSE(public_key.isNull()); |
2123 EXPECT_FALSE(private_key.isNull()); | 2245 EXPECT_FALSE(private_key.isNull()); |
2124 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); | 2246 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); |
2125 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); | 2247 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); |
2126 EXPECT_TRUE(public_key.extractable()); | 2248 EXPECT_TRUE(public_key.extractable()); |
2127 EXPECT_EQ(extractable, private_key.extractable()); | 2249 EXPECT_EQ(extractable, private_key.extractable()); |
2128 EXPECT_EQ(usage_mask, public_key.usages()); | 2250 EXPECT_EQ(usage_mask, public_key.usages()); |
2129 EXPECT_EQ(usage_mask, private_key.usages()); | 2251 EXPECT_EQ(usage_mask, private_key.usages()); |
2130 | 2252 |
2131 // Successful WebCryptoAlgorithmIdRsaOaep key generation. | 2253 // Successful WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 key generation (sha256) |
2132 algorithm = CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaOaep, | 2254 algorithm = |
2133 blink::WebCryptoAlgorithmIdSha256, | 2255 CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
2134 modulus_length, | 2256 blink::WebCryptoAlgorithmIdSha256, |
2135 public_exponent); | 2257 modulus_length, |
| 2258 public_exponent); |
2136 EXPECT_EQ(Status::Success(), | 2259 EXPECT_EQ(Status::Success(), |
2137 GenerateKeyPair( | 2260 GenerateKeyPair( |
2138 algorithm, extractable, usage_mask, &public_key, &private_key)); | 2261 algorithm, extractable, usage_mask, &public_key, &private_key)); |
2139 EXPECT_FALSE(public_key.isNull()); | 2262 EXPECT_FALSE(public_key.isNull()); |
2140 EXPECT_FALSE(private_key.isNull()); | 2263 EXPECT_FALSE(private_key.isNull()); |
2141 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); | 2264 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); |
2142 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); | 2265 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); |
2143 EXPECT_EQ(modulus_length, | 2266 EXPECT_EQ(modulus_length, |
2144 public_key.algorithm().rsaHashedParams()->modulusLengthBits()); | 2267 public_key.algorithm().rsaHashedParams()->modulusLengthBits()); |
2145 EXPECT_EQ(modulus_length, | 2268 EXPECT_EQ(modulus_length, |
(...skipping 1252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3398 algorithm, | 3521 algorithm, |
3399 CreateAesCbcAlgorithm(std::vector<uint8>(0, 16)), | 3522 CreateAesCbcAlgorithm(std::vector<uint8>(0, 16)), |
3400 true, | 3523 true, |
3401 blink::WebCryptoKeyUsageEncrypt, | 3524 blink::WebCryptoKeyUsageEncrypt, |
3402 &unwrapped_key)); | 3525 &unwrapped_key)); |
3403 } | 3526 } |
3404 | 3527 |
3405 } // namespace webcrypto | 3528 } // namespace webcrypto |
3406 | 3529 |
3407 } // namespace content | 3530 } // namespace content |
OLD | NEW |