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/jwk.h" | 9 #include "content/child/webcrypto/jwk.h" |
10 #include "content/child/webcrypto/status.h" | 10 #include "content/child/webcrypto/status.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 } | 40 } |
41 | 41 |
42 // Import a PKCS#8 private key that uses RSAPrivateKey with the | 42 // Import a PKCS#8 private key that uses RSAPrivateKey with the |
43 // id-rsaEncryption OID. | 43 // id-rsaEncryption OID. |
44 TEST(WebCryptoRsaOaepTest, ImportPkcs8WithRsaEncryption) { | 44 TEST(WebCryptoRsaOaepTest, ImportPkcs8WithRsaEncryption) { |
45 if (!SupportsRsaOaep()) { | 45 if (!SupportsRsaOaep()) { |
46 LOG(WARNING) << "RSA-OAEP support not present; skipping."; | 46 LOG(WARNING) << "RSA-OAEP support not present; skipping."; |
47 return; | 47 return; |
48 } | 48 } |
49 | 49 |
50 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); | 50 blink::WebCryptoKey private_key; |
51 ASSERT_EQ(Status::Success(), | 51 ASSERT_EQ(Status::Success(), |
52 ImportKey(blink::WebCryptoKeyFormatPkcs8, | 52 ImportKey(blink::WebCryptoKeyFormatPkcs8, |
53 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)), | 53 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)), |
54 CreateRsaHashedImportAlgorithm( | 54 CreateRsaHashedImportAlgorithm( |
55 blink::WebCryptoAlgorithmIdRsaOaep, | 55 blink::WebCryptoAlgorithmIdRsaOaep, |
56 blink::WebCryptoAlgorithmIdSha1), | 56 blink::WebCryptoAlgorithmIdSha1), |
57 true, | 57 true, |
58 blink::WebCryptoKeyUsageDecrypt, | 58 blink::WebCryptoKeyUsageDecrypt, |
59 &private_key)); | 59 &private_key)); |
60 } | 60 } |
61 | 61 |
62 TEST(WebCryptoRsaOaepTest, ImportPublicJwkWithNoAlg) { | 62 TEST(WebCryptoRsaOaepTest, ImportPublicJwkWithNoAlg) { |
63 if (!SupportsRsaOaep()) { | 63 if (!SupportsRsaOaep()) { |
64 LOG(WARNING) << "RSA-OAEP support not present; skipping."; | 64 LOG(WARNING) << "RSA-OAEP support not present; skipping."; |
65 return; | 65 return; |
66 } | 66 } |
67 | 67 |
68 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict()); | 68 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict()); |
69 | 69 |
70 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | 70 blink::WebCryptoKey public_key; |
71 ASSERT_EQ(Status::Success(), | 71 ASSERT_EQ(Status::Success(), |
72 ImportKeyJwkFromDict(*jwk.get(), | 72 ImportKeyJwkFromDict(*jwk.get(), |
73 CreateRsaHashedImportAlgorithm( | 73 CreateRsaHashedImportAlgorithm( |
74 blink::WebCryptoAlgorithmIdRsaOaep, | 74 blink::WebCryptoAlgorithmIdRsaOaep, |
75 blink::WebCryptoAlgorithmIdSha1), | 75 blink::WebCryptoAlgorithmIdSha1), |
76 true, | 76 true, |
77 blink::WebCryptoKeyUsageEncrypt, | 77 blink::WebCryptoKeyUsageEncrypt, |
78 &public_key)); | 78 &public_key)); |
79 } | 79 } |
80 | 80 |
81 TEST(WebCryptoRsaOaepTest, ImportPublicJwkWithMatchingAlg) { | 81 TEST(WebCryptoRsaOaepTest, ImportPublicJwkWithMatchingAlg) { |
82 if (!SupportsRsaOaep()) { | 82 if (!SupportsRsaOaep()) { |
83 LOG(WARNING) << "RSA-OAEP support not present; skipping."; | 83 LOG(WARNING) << "RSA-OAEP support not present; skipping."; |
84 return; | 84 return; |
85 } | 85 } |
86 | 86 |
87 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict()); | 87 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict()); |
88 jwk->SetString("alg", "RSA-OAEP"); | 88 jwk->SetString("alg", "RSA-OAEP"); |
89 | 89 |
90 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | 90 blink::WebCryptoKey public_key; |
91 ASSERT_EQ(Status::Success(), | 91 ASSERT_EQ(Status::Success(), |
92 ImportKeyJwkFromDict(*jwk.get(), | 92 ImportKeyJwkFromDict(*jwk.get(), |
93 CreateRsaHashedImportAlgorithm( | 93 CreateRsaHashedImportAlgorithm( |
94 blink::WebCryptoAlgorithmIdRsaOaep, | 94 blink::WebCryptoAlgorithmIdRsaOaep, |
95 blink::WebCryptoAlgorithmIdSha1), | 95 blink::WebCryptoAlgorithmIdSha1), |
96 true, | 96 true, |
97 blink::WebCryptoKeyUsageEncrypt, | 97 blink::WebCryptoKeyUsageEncrypt, |
98 &public_key)); | 98 &public_key)); |
99 } | 99 } |
100 | 100 |
101 TEST(WebCryptoRsaOaepTest, ImportPublicJwkWithMismatchedAlgFails) { | 101 TEST(WebCryptoRsaOaepTest, ImportPublicJwkWithMismatchedAlgFails) { |
102 if (!SupportsRsaOaep()) { | 102 if (!SupportsRsaOaep()) { |
103 LOG(WARNING) << "RSA-OAEP support not present; skipping."; | 103 LOG(WARNING) << "RSA-OAEP support not present; skipping."; |
104 return; | 104 return; |
105 } | 105 } |
106 | 106 |
107 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict()); | 107 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict()); |
108 jwk->SetString("alg", "RSA-OAEP-512"); | 108 jwk->SetString("alg", "RSA-OAEP-512"); |
109 | 109 |
110 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | 110 blink::WebCryptoKey public_key; |
111 ASSERT_EQ(Status::ErrorJwkAlgorithmInconsistent(), | 111 ASSERT_EQ(Status::ErrorJwkAlgorithmInconsistent(), |
112 ImportKeyJwkFromDict(*jwk.get(), | 112 ImportKeyJwkFromDict(*jwk.get(), |
113 CreateRsaHashedImportAlgorithm( | 113 CreateRsaHashedImportAlgorithm( |
114 blink::WebCryptoAlgorithmIdRsaOaep, | 114 blink::WebCryptoAlgorithmIdRsaOaep, |
115 blink::WebCryptoAlgorithmIdSha1), | 115 blink::WebCryptoAlgorithmIdSha1), |
116 true, | 116 true, |
117 blink::WebCryptoKeyUsageEncrypt, | 117 blink::WebCryptoKeyUsageEncrypt, |
118 &public_key)); | 118 &public_key)); |
119 } | 119 } |
120 | 120 |
121 TEST(WebCryptoRsaOaepTest, ImportPublicJwkWithMismatchedTypeFails) { | 121 TEST(WebCryptoRsaOaepTest, ImportPublicJwkWithMismatchedTypeFails) { |
122 if (!SupportsRsaOaep()) { | 122 if (!SupportsRsaOaep()) { |
123 LOG(WARNING) << "RSA-OAEP support not present; skipping."; | 123 LOG(WARNING) << "RSA-OAEP support not present; skipping."; |
124 return; | 124 return; |
125 } | 125 } |
126 | 126 |
127 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict()); | 127 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict()); |
128 jwk->SetString("kty", "oct"); | 128 jwk->SetString("kty", "oct"); |
129 jwk->SetString("alg", "RSA-OAEP"); | 129 jwk->SetString("alg", "RSA-OAEP"); |
130 | 130 |
131 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | 131 blink::WebCryptoKey public_key; |
132 ASSERT_EQ(Status::ErrorJwkUnexpectedKty("RSA"), | 132 ASSERT_EQ(Status::ErrorJwkUnexpectedKty("RSA"), |
133 ImportKeyJwkFromDict(*jwk.get(), | 133 ImportKeyJwkFromDict(*jwk.get(), |
134 CreateRsaHashedImportAlgorithm( | 134 CreateRsaHashedImportAlgorithm( |
135 blink::WebCryptoAlgorithmIdRsaOaep, | 135 blink::WebCryptoAlgorithmIdRsaOaep, |
136 blink::WebCryptoAlgorithmIdSha1), | 136 blink::WebCryptoAlgorithmIdSha1), |
137 true, | 137 true, |
138 blink::WebCryptoKeyUsageEncrypt, | 138 blink::WebCryptoKeyUsageEncrypt, |
139 &public_key)); | 139 &public_key)); |
140 } | 140 } |
141 | 141 |
(...skipping 11 matching lines...) Expand all Loading... |
153 {blink::WebCryptoAlgorithmIdSha384, "RSA-OAEP-384"}, | 153 {blink::WebCryptoAlgorithmIdSha384, "RSA-OAEP-384"}, |
154 {blink::WebCryptoAlgorithmIdSha512, "RSA-OAEP-512"}}; | 154 {blink::WebCryptoAlgorithmIdSha512, "RSA-OAEP-512"}}; |
155 for (size_t i = 0; i < arraysize(kTestData); ++i) { | 155 for (size_t i = 0; i < arraysize(kTestData); ++i) { |
156 const TestData& test_data = kTestData[i]; | 156 const TestData& test_data = kTestData[i]; |
157 SCOPED_TRACE(test_data.expected_jwk_alg); | 157 SCOPED_TRACE(test_data.expected_jwk_alg); |
158 | 158 |
159 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict()); | 159 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict()); |
160 jwk->SetString("alg", test_data.expected_jwk_alg); | 160 jwk->SetString("alg", test_data.expected_jwk_alg); |
161 | 161 |
162 // Import the key in a known-good format | 162 // Import the key in a known-good format |
163 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | 163 blink::WebCryptoKey public_key; |
164 ASSERT_EQ(Status::Success(), | 164 ASSERT_EQ(Status::Success(), |
165 ImportKeyJwkFromDict( | 165 ImportKeyJwkFromDict( |
166 *jwk.get(), | 166 *jwk.get(), |
167 CreateRsaHashedImportAlgorithm( | 167 CreateRsaHashedImportAlgorithm( |
168 blink::WebCryptoAlgorithmIdRsaOaep, test_data.hash_alg), | 168 blink::WebCryptoAlgorithmIdRsaOaep, test_data.hash_alg), |
169 true, | 169 true, |
170 blink::WebCryptoKeyUsageEncrypt, | 170 blink::WebCryptoKeyUsageEncrypt, |
171 &public_key)); | 171 &public_key)); |
172 | 172 |
173 // Now export the key as JWK and verify its contents | 173 // Now export the key as JWK and verify its contents |
(...skipping 29 matching lines...) Expand all Loading... |
203 std::vector<uint8_t> public_key_der = | 203 std::vector<uint8_t> public_key_der = |
204 GetBytesFromHexString(test, "public_key"); | 204 GetBytesFromHexString(test, "public_key"); |
205 std::vector<uint8_t> private_key_der = | 205 std::vector<uint8_t> private_key_der = |
206 GetBytesFromHexString(test, "private_key"); | 206 GetBytesFromHexString(test, "private_key"); |
207 std::vector<uint8_t> ciphertext = GetBytesFromHexString(test, "ciphertext"); | 207 std::vector<uint8_t> ciphertext = GetBytesFromHexString(test, "ciphertext"); |
208 std::vector<uint8_t> plaintext = GetBytesFromHexString(test, "plaintext"); | 208 std::vector<uint8_t> plaintext = GetBytesFromHexString(test, "plaintext"); |
209 std::vector<uint8_t> label = GetBytesFromHexString(test, "label"); | 209 std::vector<uint8_t> label = GetBytesFromHexString(test, "label"); |
210 | 210 |
211 blink::WebCryptoAlgorithm import_algorithm = CreateRsaHashedImportAlgorithm( | 211 blink::WebCryptoAlgorithm import_algorithm = CreateRsaHashedImportAlgorithm( |
212 blink::WebCryptoAlgorithmIdRsaOaep, digest_algorithm.id()); | 212 blink::WebCryptoAlgorithmIdRsaOaep, digest_algorithm.id()); |
213 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | 213 blink::WebCryptoKey public_key; |
214 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); | 214 blink::WebCryptoKey private_key; |
215 | 215 |
216 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(public_key_der, | 216 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(public_key_der, |
217 private_key_der, | 217 private_key_der, |
218 import_algorithm, | 218 import_algorithm, |
219 false, | 219 false, |
220 blink::WebCryptoKeyUsageEncrypt, | 220 blink::WebCryptoKeyUsageEncrypt, |
221 blink::WebCryptoKeyUsageDecrypt, | 221 blink::WebCryptoKeyUsageDecrypt, |
222 &public_key, | 222 &public_key, |
223 &private_key)); | 223 &private_key)); |
224 | 224 |
(...skipping 24 matching lines...) Expand all Loading... |
249 if (!SupportsRsaOaep()) { | 249 if (!SupportsRsaOaep()) { |
250 LOG(WARNING) << "RSA-OAEP support not present; skipping."; | 250 LOG(WARNING) << "RSA-OAEP support not present; skipping."; |
251 return; | 251 return; |
252 } | 252 } |
253 | 253 |
254 const blink::WebCryptoAlgorithmId kHash = blink::WebCryptoAlgorithmIdSha1; | 254 const blink::WebCryptoAlgorithmId kHash = blink::WebCryptoAlgorithmIdSha1; |
255 const size_t kHashSize = 20; | 255 const size_t kHashSize = 20; |
256 | 256 |
257 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict()); | 257 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict()); |
258 | 258 |
259 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | 259 blink::WebCryptoKey public_key; |
260 ASSERT_EQ(Status::Success(), | 260 ASSERT_EQ(Status::Success(), |
261 ImportKeyJwkFromDict(*jwk.get(), | 261 ImportKeyJwkFromDict(*jwk.get(), |
262 CreateRsaHashedImportAlgorithm( | 262 CreateRsaHashedImportAlgorithm( |
263 blink::WebCryptoAlgorithmIdRsaOaep, kHash), | 263 blink::WebCryptoAlgorithmIdRsaOaep, kHash), |
264 true, | 264 true, |
265 blink::WebCryptoKeyUsageEncrypt, | 265 blink::WebCryptoKeyUsageEncrypt, |
266 &public_key)); | 266 &public_key)); |
267 | 267 |
268 // The maximum size of an encrypted message is: | 268 // The maximum size of an encrypted message is: |
269 // modulus length | 269 // modulus length |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 TEST(WebCryptoRsaOaepTest, EncryptWithLargeDigestFails) { | 316 TEST(WebCryptoRsaOaepTest, EncryptWithLargeDigestFails) { |
317 if (!SupportsRsaOaep()) { | 317 if (!SupportsRsaOaep()) { |
318 LOG(WARNING) << "RSA-OAEP support not present; skipping."; | 318 LOG(WARNING) << "RSA-OAEP support not present; skipping."; |
319 return; | 319 return; |
320 } | 320 } |
321 | 321 |
322 const blink::WebCryptoAlgorithmId kHash = blink::WebCryptoAlgorithmIdSha512; | 322 const blink::WebCryptoAlgorithmId kHash = blink::WebCryptoAlgorithmIdSha512; |
323 | 323 |
324 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict()); | 324 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict()); |
325 | 325 |
326 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | 326 blink::WebCryptoKey public_key; |
327 ASSERT_EQ(Status::Success(), | 327 ASSERT_EQ(Status::Success(), |
328 ImportKeyJwkFromDict(*jwk.get(), | 328 ImportKeyJwkFromDict(*jwk.get(), |
329 CreateRsaHashedImportAlgorithm( | 329 CreateRsaHashedImportAlgorithm( |
330 blink::WebCryptoAlgorithmIdRsaOaep, kHash), | 330 blink::WebCryptoAlgorithmIdRsaOaep, kHash), |
331 true, | 331 true, |
332 blink::WebCryptoKeyUsageEncrypt, | 332 blink::WebCryptoKeyUsageEncrypt, |
333 &public_key)); | 333 &public_key)); |
334 | 334 |
335 // The label has no influence on the maximum message size. For simplicity, | 335 // The label has no influence on the maximum message size. For simplicity, |
336 // use the empty string. | 336 // use the empty string. |
337 std::vector<uint8_t> label; | 337 std::vector<uint8_t> label; |
338 blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label); | 338 blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label); |
339 | 339 |
340 std::string small_message("A"); | 340 std::string small_message("A"); |
341 std::vector<uint8_t> ciphertext; | 341 std::vector<uint8_t> ciphertext; |
342 // This is an operation error, as the internal consistency checking of the | 342 // This is an operation error, as the internal consistency checking of the |
343 // algorithm parameters is up to the implementation. | 343 // algorithm parameters is up to the implementation. |
344 ASSERT_EQ( | 344 ASSERT_EQ( |
345 Status::OperationError(), | 345 Status::OperationError(), |
346 Encrypt( | 346 Encrypt( |
347 op_algorithm, public_key, CryptoData(small_message), &ciphertext)); | 347 op_algorithm, public_key, CryptoData(small_message), &ciphertext)); |
348 } | 348 } |
349 | 349 |
350 TEST(WebCryptoRsaOaepTest, DecryptWithLargeMessageFails) { | 350 TEST(WebCryptoRsaOaepTest, DecryptWithLargeMessageFails) { |
351 if (!SupportsRsaOaep()) { | 351 if (!SupportsRsaOaep()) { |
352 LOG(WARNING) << "RSA-OAEP support not present; skipping."; | 352 LOG(WARNING) << "RSA-OAEP support not present; skipping."; |
353 return; | 353 return; |
354 } | 354 } |
355 | 355 |
356 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); | 356 blink::WebCryptoKey private_key; |
357 ASSERT_EQ(Status::Success(), | 357 ASSERT_EQ(Status::Success(), |
358 ImportKey(blink::WebCryptoKeyFormatPkcs8, | 358 ImportKey(blink::WebCryptoKeyFormatPkcs8, |
359 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)), | 359 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)), |
360 CreateRsaHashedImportAlgorithm( | 360 CreateRsaHashedImportAlgorithm( |
361 blink::WebCryptoAlgorithmIdRsaOaep, | 361 blink::WebCryptoAlgorithmIdRsaOaep, |
362 blink::WebCryptoAlgorithmIdSha1), | 362 blink::WebCryptoAlgorithmIdSha1), |
363 true, | 363 true, |
364 blink::WebCryptoKeyUsageDecrypt, | 364 blink::WebCryptoKeyUsageDecrypt, |
365 &private_key)); | 365 &private_key)); |
366 | 366 |
(...skipping 13 matching lines...) Expand all Loading... |
380 } | 380 } |
381 | 381 |
382 TEST(WebCryptoRsaOaepTest, WrapUnwrapRawKey) { | 382 TEST(WebCryptoRsaOaepTest, WrapUnwrapRawKey) { |
383 if (!SupportsRsaOaep()) { | 383 if (!SupportsRsaOaep()) { |
384 LOG(WARNING) << "RSA-OAEP support not present; skipping."; | 384 LOG(WARNING) << "RSA-OAEP support not present; skipping."; |
385 return; | 385 return; |
386 } | 386 } |
387 | 387 |
388 blink::WebCryptoAlgorithm import_algorithm = CreateRsaHashedImportAlgorithm( | 388 blink::WebCryptoAlgorithm import_algorithm = CreateRsaHashedImportAlgorithm( |
389 blink::WebCryptoAlgorithmIdRsaOaep, blink::WebCryptoAlgorithmIdSha1); | 389 blink::WebCryptoAlgorithmIdRsaOaep, blink::WebCryptoAlgorithmIdSha1); |
390 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | 390 blink::WebCryptoKey public_key; |
391 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); | 391 blink::WebCryptoKey private_key; |
392 | 392 |
393 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair( | 393 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair( |
394 HexStringToBytes(kPublicKeySpkiDerHex), | 394 HexStringToBytes(kPublicKeySpkiDerHex), |
395 HexStringToBytes(kPrivateKeyPkcs8DerHex), | 395 HexStringToBytes(kPrivateKeyPkcs8DerHex), |
396 import_algorithm, | 396 import_algorithm, |
397 false, | 397 false, |
398 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageWrapKey, | 398 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageWrapKey, |
399 blink::WebCryptoKeyUsageDecrypt | blink::WebCryptoKeyUsageUnwrapKey, | 399 blink::WebCryptoKeyUsageDecrypt | blink::WebCryptoKeyUsageUnwrapKey, |
400 &public_key, | 400 &public_key, |
401 &private_key)); | 401 &private_key)); |
(...skipping 23 matching lines...) Expand all Loading... |
425 // Because |private_key| supports both decrypt and unwrap, this is valid. | 425 // Because |private_key| supports both decrypt and unwrap, this is valid. |
426 std::vector<uint8_t> decrypted_key; | 426 std::vector<uint8_t> decrypted_key; |
427 ASSERT_EQ(Status::Success(), | 427 ASSERT_EQ(Status::Success(), |
428 Decrypt(wrapping_algorithm, | 428 Decrypt(wrapping_algorithm, |
429 private_key, | 429 private_key, |
430 CryptoData(wrapped_key), | 430 CryptoData(wrapped_key), |
431 &decrypted_key)); | 431 &decrypted_key)); |
432 EXPECT_BYTES_EQ_HEX(key_hex, decrypted_key); | 432 EXPECT_BYTES_EQ_HEX(key_hex, decrypted_key); |
433 | 433 |
434 // Now attempt to unwrap the key, which should also decrypt the data. | 434 // Now attempt to unwrap the key, which should also decrypt the data. |
435 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull(); | 435 blink::WebCryptoKey unwrapped_key; |
436 ASSERT_EQ(Status::Success(), | 436 ASSERT_EQ(Status::Success(), |
437 UnwrapKey(blink::WebCryptoKeyFormatRaw, | 437 UnwrapKey(blink::WebCryptoKeyFormatRaw, |
438 CryptoData(wrapped_key), | 438 CryptoData(wrapped_key), |
439 private_key, | 439 private_key, |
440 wrapping_algorithm, | 440 wrapping_algorithm, |
441 key_algorithm, | 441 key_algorithm, |
442 true, | 442 true, |
443 blink::WebCryptoKeyUsageEncrypt, | 443 blink::WebCryptoKeyUsageEncrypt, |
444 &unwrapped_key)); | 444 &unwrapped_key)); |
445 ASSERT_FALSE(unwrapped_key.isNull()); | 445 ASSERT_FALSE(unwrapped_key.isNull()); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 "b578f59ed4cb724c47dba94d3b484c1876dcd70281807f15bc7d2406007cac2b138a96af" | 498 "b578f59ed4cb724c47dba94d3b484c1876dcd70281807f15bc7d2406007cac2b138a96af" |
499 "2d1e00276b84da593132c253fcb73212732dfd25824c2a615bc3d9b7f2c8d2fa542d3562" | 499 "2d1e00276b84da593132c253fcb73212732dfd25824c2a615bc3d9b7f2c8d2fa542d3562" |
500 "b0c7738e61eeff580a6056239fb367ea9e5efe73d4f846033602e90c36a78db6fa8ea792" | 500 "b0c7738e61eeff580a6056239fb367ea9e5efe73d4f846033602e90c36a78db6fa8ea792" |
501 "0769675ec58e237bd994d189c8045a96f5dd3a4f12547257ce224e3c9af830a4da3c0eab" | 501 "0769675ec58e237bd994d189c8045a96f5dd3a4f12547257ce224e3c9af830a4da3c0eab" |
502 "9227a0035ae9028180067caea877e0b23090fc689322b71fbcce63d6596e66ab5fcdbaa0" | 502 "9227a0035ae9028180067caea877e0b23090fc689322b71fbcce63d6596e66ab5fcdbaa0" |
503 "0d49e93aba8effb4518c2da637f209028401a68f344865b4956b032c69acde51d29177ca" | 503 "0d49e93aba8effb4518c2da637f209028401a68f344865b4956b032c69acde51d29177ca" |
504 "3db99fdbf5e74848ed4fa7bdfc2ebb60e2aaa5354770a763e1399ab7a2099762d525fea0" | 504 "3db99fdbf5e74848ed4fa7bdfc2ebb60e2aaa5354770a763e1399ab7a2099762d525fea0" |
505 "37f3e1972c45a477e66db95c9609bb27f862700ef93379930786cf751b"; | 505 "37f3e1972c45a477e66db95c9609bb27f862700ef93379930786cf751b"; |
506 blink::WebCryptoAlgorithm import_algorithm = CreateRsaHashedImportAlgorithm( | 506 blink::WebCryptoAlgorithm import_algorithm = CreateRsaHashedImportAlgorithm( |
507 blink::WebCryptoAlgorithmIdRsaOaep, blink::WebCryptoAlgorithmIdSha1); | 507 blink::WebCryptoAlgorithmIdRsaOaep, blink::WebCryptoAlgorithmIdSha1); |
508 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | 508 blink::WebCryptoKey public_key; |
509 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); | 509 blink::WebCryptoKey private_key; |
510 | 510 |
511 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair( | 511 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair( |
512 HexStringToBytes(kPublicKey2048SpkiDerHex), | 512 HexStringToBytes(kPublicKey2048SpkiDerHex), |
513 HexStringToBytes(kPrivateKey2048Pkcs8DerHex), | 513 HexStringToBytes(kPrivateKey2048Pkcs8DerHex), |
514 import_algorithm, | 514 import_algorithm, |
515 false, | 515 false, |
516 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageWrapKey, | 516 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageWrapKey, |
517 blink::WebCryptoKeyUsageDecrypt | blink::WebCryptoKeyUsageUnwrapKey, | 517 blink::WebCryptoKeyUsageDecrypt | blink::WebCryptoKeyUsageUnwrapKey, |
518 &public_key, | 518 &public_key, |
519 &private_key)); | 519 &private_key)); |
(...skipping 24 matching lines...) Expand all Loading... |
544 std::vector<uint8_t> decrypted_jwk; | 544 std::vector<uint8_t> decrypted_jwk; |
545 ASSERT_EQ(Status::Success(), | 545 ASSERT_EQ(Status::Success(), |
546 Decrypt(wrapping_algorithm, | 546 Decrypt(wrapping_algorithm, |
547 private_key, | 547 private_key, |
548 CryptoData(wrapped_key), | 548 CryptoData(wrapped_key), |
549 &decrypted_jwk)); | 549 &decrypted_jwk)); |
550 EXPECT_TRUE(VerifySecretJwk( | 550 EXPECT_TRUE(VerifySecretJwk( |
551 decrypted_jwk, "A128CBC", key_hex, blink::WebCryptoKeyUsageEncrypt)); | 551 decrypted_jwk, "A128CBC", key_hex, blink::WebCryptoKeyUsageEncrypt)); |
552 | 552 |
553 // Now attempt to unwrap the key, which should also decrypt the data. | 553 // Now attempt to unwrap the key, which should also decrypt the data. |
554 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull(); | 554 blink::WebCryptoKey unwrapped_key; |
555 ASSERT_EQ(Status::Success(), | 555 ASSERT_EQ(Status::Success(), |
556 UnwrapKey(blink::WebCryptoKeyFormatJwk, | 556 UnwrapKey(blink::WebCryptoKeyFormatJwk, |
557 CryptoData(wrapped_key), | 557 CryptoData(wrapped_key), |
558 private_key, | 558 private_key, |
559 wrapping_algorithm, | 559 wrapping_algorithm, |
560 key_algorithm, | 560 key_algorithm, |
561 true, | 561 true, |
562 blink::WebCryptoKeyUsageEncrypt, | 562 blink::WebCryptoKeyUsageEncrypt, |
563 &unwrapped_key)); | 563 &unwrapped_key)); |
564 ASSERT_FALSE(unwrapped_key.isNull()); | 564 ASSERT_FALSE(unwrapped_key.isNull()); |
(...skipping 26 matching lines...) Expand all Loading... |
591 | 591 |
592 for (size_t test_index = 0; test_index < arraysize(kTests); ++test_index) { | 592 for (size_t test_index = 0; test_index < arraysize(kTests); ++test_index) { |
593 SCOPED_TRACE(test_index); | 593 SCOPED_TRACE(test_index); |
594 const TestCase& test = kTests[test_index]; | 594 const TestCase& test = kTests[test_index]; |
595 | 595 |
596 const blink::WebCryptoAlgorithm import_algorithm = | 596 const blink::WebCryptoAlgorithm import_algorithm = |
597 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaOaep, | 597 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaOaep, |
598 test.hash); | 598 test.hash); |
599 | 599 |
600 // Import the spki to create a public key | 600 // Import the spki to create a public key |
601 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | 601 blink::WebCryptoKey public_key; |
602 ASSERT_EQ(Status::Success(), | 602 ASSERT_EQ(Status::Success(), |
603 ImportKey(blink::WebCryptoKeyFormatSpki, | 603 ImportKey(blink::WebCryptoKeyFormatSpki, |
604 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)), | 604 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)), |
605 import_algorithm, | 605 import_algorithm, |
606 true, | 606 true, |
607 test.usage, | 607 test.usage, |
608 &public_key)); | 608 &public_key)); |
609 | 609 |
610 // Export the public key as JWK and verify its contents | 610 // Export the public key as JWK and verify its contents |
611 std::vector<uint8_t> jwk; | 611 std::vector<uint8_t> jwk; |
612 ASSERT_EQ(Status::Success(), | 612 ASSERT_EQ(Status::Success(), |
613 ExportKey(blink::WebCryptoKeyFormatJwk, public_key, &jwk)); | 613 ExportKey(blink::WebCryptoKeyFormatJwk, public_key, &jwk)); |
614 EXPECT_TRUE(VerifyPublicJwk(jwk, | 614 EXPECT_TRUE(VerifyPublicJwk(jwk, |
615 test.jwk_alg, | 615 test.jwk_alg, |
616 kPublicKeyModulusHex, | 616 kPublicKeyModulusHex, |
617 kPublicKeyExponentHex, | 617 kPublicKeyExponentHex, |
618 test.usage)); | 618 test.usage)); |
619 | 619 |
620 // Import the JWK back in to create a new key | 620 // Import the JWK back in to create a new key |
621 blink::WebCryptoKey public_key2 = blink::WebCryptoKey::createNull(); | 621 blink::WebCryptoKey public_key2; |
622 ASSERT_EQ(Status::Success(), | 622 ASSERT_EQ(Status::Success(), |
623 ImportKey(blink::WebCryptoKeyFormatJwk, | 623 ImportKey(blink::WebCryptoKeyFormatJwk, |
624 CryptoData(jwk), | 624 CryptoData(jwk), |
625 import_algorithm, | 625 import_algorithm, |
626 true, | 626 true, |
627 test.usage, | 627 test.usage, |
628 &public_key2)); | 628 &public_key2)); |
629 ASSERT_TRUE(public_key2.handle()); | 629 ASSERT_TRUE(public_key2.handle()); |
630 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key2.type()); | 630 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key2.type()); |
631 EXPECT_TRUE(public_key2.extractable()); | 631 EXPECT_TRUE(public_key2.extractable()); |
632 EXPECT_EQ(import_algorithm.id(), public_key2.algorithm().id()); | 632 EXPECT_EQ(import_algorithm.id(), public_key2.algorithm().id()); |
633 | 633 |
634 // TODO(eroman): Export the SPKI and verify matches. | 634 // TODO(eroman): Export the SPKI and verify matches. |
635 } | 635 } |
636 } | 636 } |
637 | 637 |
638 } // namespace | 638 } // namespace |
639 | 639 |
640 } // namespace webcrypto | 640 } // namespace webcrypto |
641 | 641 |
642 } // namespace content | 642 } // namespace content |
OLD | NEW |