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/stl_util.h" | 5 #include "base/stl_util.h" |
6 #include "content/child/webcrypto/algorithm_dispatch.h" | 6 #include "content/child/webcrypto/algorithm_dispatch.h" |
7 #include "content/child/webcrypto/crypto_data.h" | 7 #include "content/child/webcrypto/crypto_data.h" |
8 #include "content/child/webcrypto/jwk.h" | 8 #include "content/child/webcrypto/jwk.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 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { | 294 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { |
295 SCOPED_TRACE(test_index); | 295 SCOPED_TRACE(test_index); |
296 | 296 |
297 const base::DictionaryValue* test; | 297 const base::DictionaryValue* test; |
298 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); | 298 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); |
299 | 299 |
300 blink::WebCryptoNamedCurve curve = GetCurveNameFromDictionary(test); | 300 blink::WebCryptoNamedCurve curve = GetCurveNameFromDictionary(test); |
301 const base::DictionaryValue* jwk_dict; | 301 const base::DictionaryValue* jwk_dict; |
302 EXPECT_TRUE(test->GetDictionary("jwk", &jwk_dict)); | 302 EXPECT_TRUE(test->GetDictionary("jwk", &jwk_dict)); |
303 std::vector<uint8_t> jwk_bytes = MakeJsonVector(*jwk_dict); | 303 std::vector<uint8_t> jwk_bytes = MakeJsonVector(*jwk_dict); |
304 std::vector<uint8_t> pkcs8_bytes = GetBytesFromHexString(test, "pkcs8"); | 304 std::vector<uint8_t> pkcs8_bytes = GetBytesFromHexString( |
| 305 test, test->HasKey("exported_pkcs8") ? "exported_pkcs8" : "pkcs8"); |
305 | 306 |
306 // ------------------------------------------------- | 307 // ------------------------------------------------- |
307 // Test from JWK, and then export to {JWK, PKCS8} | 308 // Test from JWK, and then export to {JWK, PKCS8} |
308 // ------------------------------------------------- | 309 // ------------------------------------------------- |
309 | 310 |
310 // Import the key using JWK | 311 // Import the key using JWK |
311 blink::WebCryptoKey key; | 312 blink::WebCryptoKey key; |
312 ASSERT_EQ(Status::Success(), | 313 ASSERT_EQ(Status::Success(), |
313 ImportKey(blink::WebCryptoKeyFormatJwk, CryptoData(jwk_bytes), | 314 ImportKey(blink::WebCryptoKeyFormatJwk, CryptoData(jwk_bytes), |
314 CreateEcdsaImportAlgorithm(curve), true, | 315 CreateEcdsaImportAlgorithm(curve), true, |
(...skipping 23 matching lines...) Expand all Loading... |
338 | 339 |
339 // Export the key as PKCS8 | 340 // Export the key as PKCS8 |
340 ASSERT_EQ(Status::Success(), | 341 ASSERT_EQ(Status::Success(), |
341 ExportKey(blink::WebCryptoKeyFormatPkcs8, key, &exported_bytes)); | 342 ExportKey(blink::WebCryptoKeyFormatPkcs8, key, &exported_bytes)); |
342 EXPECT_EQ(CryptoData(pkcs8_bytes), CryptoData(exported_bytes)); | 343 EXPECT_EQ(CryptoData(pkcs8_bytes), CryptoData(exported_bytes)); |
343 | 344 |
344 // ------------------------------------------------- | 345 // ------------------------------------------------- |
345 // Test from PKCS8, and then export to {JWK, PKCS8} | 346 // Test from PKCS8, and then export to {JWK, PKCS8} |
346 // ------------------------------------------------- | 347 // ------------------------------------------------- |
347 | 348 |
| 349 // The imported PKCS8 bytes may differ from the exported bytes (in the case |
| 350 // where the publicKey was missing, it will be synthesized and written back |
| 351 // during export). |
| 352 std::vector<uint8_t> pkcs8_input_bytes = GetBytesFromHexString( |
| 353 test, test->HasKey("original_pkcs8") ? "original_pkcs8" : "pkcs8"); |
| 354 CryptoData pkcs8_input_data(pkcs8_input_bytes.empty() ? pkcs8_bytes |
| 355 : pkcs8_input_bytes); |
| 356 |
348 // Import the key using PKCS8 | 357 // Import the key using PKCS8 |
349 ASSERT_EQ(Status::Success(), | 358 ASSERT_EQ(Status::Success(), |
350 ImportKey(blink::WebCryptoKeyFormatPkcs8, CryptoData(pkcs8_bytes), | 359 ImportKey(blink::WebCryptoKeyFormatPkcs8, pkcs8_input_data, |
351 CreateEcdsaImportAlgorithm(curve), true, | 360 CreateEcdsaImportAlgorithm(curve), true, |
352 blink::WebCryptoKeyUsageSign, &key)); | 361 blink::WebCryptoKeyUsageSign, &key)); |
353 | 362 |
354 // Export the key as PKCS8 | 363 // Export the key as PKCS8 |
355 ASSERT_EQ(Status::Success(), | 364 ASSERT_EQ(Status::Success(), |
356 ExportKey(blink::WebCryptoKeyFormatPkcs8, key, &exported_bytes)); | 365 ExportKey(blink::WebCryptoKeyFormatPkcs8, key, &exported_bytes)); |
357 EXPECT_EQ(CryptoData(pkcs8_bytes), CryptoData(exported_bytes)); | 366 EXPECT_EQ(CryptoData(pkcs8_bytes), CryptoData(exported_bytes)); |
358 | 367 |
359 // Export the key as JWK | 368 // Export the key as JWK |
360 ASSERT_EQ(Status::Success(), | 369 ASSERT_EQ(Status::Success(), |
361 ExportKey(blink::WebCryptoKeyFormatJwk, key, &exported_bytes)); | 370 ExportKey(blink::WebCryptoKeyFormatJwk, key, &exported_bytes)); |
362 EXPECT_EQ(CryptoData(jwk_bytes), CryptoData(exported_bytes)); | 371 EXPECT_EQ(CryptoData(jwk_bytes), CryptoData(exported_bytes)); |
363 } | 372 } |
364 } | 373 } |
365 | 374 |
366 } // namespace | 375 } // namespace |
367 | 376 |
368 } // namespace webcrypto | 377 } // namespace webcrypto |
369 | 378 |
370 } // namespace content | 379 } // namespace content |
OLD | NEW |