Chromium Code Reviews| 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/renderer/webcrypto/shared_crypto.h" | 5 #include "content/renderer/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 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
| 14 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/values.h" | |
| 20 #include "content/public/common/content_paths.h" | 19 #include "content/public/common/content_paths.h" |
| 21 #include "content/public/renderer/content_renderer_client.h" | 20 #include "content/public/renderer/content_renderer_client.h" |
| 22 #include "content/renderer/renderer_webkitplatformsupport_impl.h" | 21 #include "content/renderer/renderer_webkitplatformsupport_impl.h" |
| 23 #include "content/renderer/webcrypto/crypto_data.h" | 22 #include "content/renderer/webcrypto/crypto_data.h" |
| 24 #include "content/renderer/webcrypto/webcrypto_util.h" | 23 #include "content/renderer/webcrypto/webcrypto_util.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | 25 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
| 27 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 26 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| 28 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | 27 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
| 29 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 28 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 273 return blink::WebCryptoAlgorithm::createNull(); | 272 return blink::WebCryptoAlgorithm::createNull(); |
| 274 } | 273 } |
| 275 | 274 |
| 276 // Helper for ImportJwkFailures and ImportJwkOctFailures. Restores the JWK JSON | 275 // Helper for ImportJwkFailures and ImportJwkOctFailures. Restores the JWK JSON |
| 277 // dictionary to a good state | 276 // dictionary to a good state |
| 278 void RestoreJwkOctDictionary(base::DictionaryValue* dict) { | 277 void RestoreJwkOctDictionary(base::DictionaryValue* dict) { |
| 279 dict->Clear(); | 278 dict->Clear(); |
| 280 dict->SetString("kty", "oct"); | 279 dict->SetString("kty", "oct"); |
| 281 dict->SetString("alg", "A128CBC"); | 280 dict->SetString("alg", "A128CBC"); |
| 282 dict->SetString("use", "enc"); | 281 dict->SetString("use", "enc"); |
| 283 dict->SetBoolean("extractable", false); | 282 dict->SetBoolean("ext", false); |
| 284 dict->SetString("k", "GADWrMRHwQfoNaXU5fZvTg=="); | 283 dict->SetString("k", "GADWrMRHwQfoNaXU5fZvTg=="); |
| 285 } | 284 } |
| 286 | 285 |
| 287 // Helper for ImportJwkRsaFailures. Restores the JWK JSON | 286 // Helper for ImportJwkRsaFailures. Restores the JWK JSON |
| 288 // dictionary to a good state | 287 // dictionary to a good state |
| 289 void RestoreJwkRsaDictionary(base::DictionaryValue* dict) { | 288 void RestoreJwkRsaDictionary(base::DictionaryValue* dict) { |
| 290 dict->Clear(); | 289 dict->Clear(); |
| 291 dict->SetString("kty", "RSA"); | 290 dict->SetString("kty", "RSA"); |
| 292 dict->SetString("alg", "RSA1_5"); | 291 dict->SetString("alg", "RSA1_5"); |
| 293 dict->SetString("use", "enc"); | 292 dict->SetString("use", "enc"); |
| 294 dict->SetBoolean("extractable", false); | 293 dict->SetBoolean("ext", false); |
| 295 dict->SetString( | 294 dict->SetString( |
| 296 "n", | 295 "n", |
| 297 "qLOyhK-OtQs4cDSoYPFGxJGfMYdjzWxVmMiuSBGh4KvEx-CwgtaTpef87Wdc9GaFEncsDLxk" | 296 "qLOyhK-OtQs4cDSoYPFGxJGfMYdjzWxVmMiuSBGh4KvEx-CwgtaTpef87Wdc9GaFEncsDLxk" |
| 298 "p0LGxjD1M8jMcvYq6DPEC_JYQumEu3i9v5fAEH1VvbZi9cTg-rmEXLUUjvc5LdOq_5OuHmtm" | 297 "p0LGxjD1M8jMcvYq6DPEC_JYQumEu3i9v5fAEH1VvbZi9cTg-rmEXLUUjvc5LdOq_5OuHmtm" |
| 299 "e7PUJHYW1PW6ENTP0ibeiNOfFvs"); | 298 "e7PUJHYW1PW6ENTP0ibeiNOfFvs"); |
| 300 dict->SetString("e", "AQAB"); | 299 dict->SetString("e", "AQAB"); |
| 301 } | 300 } |
| 302 | 301 |
| 303 blink::WebCryptoAlgorithm CreateRsaHashedImportAlgorithm( | 302 blink::WebCryptoAlgorithm CreateRsaHashedImportAlgorithm( |
| 304 blink::WebCryptoAlgorithmId algorithm_id, | 303 blink::WebCryptoAlgorithmId algorithm_id, |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 527 bool extractable, | 526 bool extractable, |
| 528 blink::WebCryptoKeyUsageMask usage_mask, | 527 blink::WebCryptoKeyUsageMask usage_mask, |
| 529 blink::WebCryptoKey* key) { | 528 blink::WebCryptoKey* key) { |
| 530 return ImportKeyJwk(CryptoData(MakeJsonVector(dict)), | 529 return ImportKeyJwk(CryptoData(MakeJsonVector(dict)), |
| 531 algorithm, | 530 algorithm, |
| 532 extractable, | 531 extractable, |
| 533 usage_mask, | 532 usage_mask, |
| 534 key); | 533 key); |
| 535 } | 534 } |
| 536 | 535 |
| 536 // Parses an ArrayBuffer of JSON into a dictionary. | |
| 537 scoped_ptr<base::DictionaryValue> GetJwkDictionary( | |
| 538 const blink::WebArrayBuffer& json) { | |
| 539 base::StringPiece json_string(reinterpret_cast<const char*>(json.data()), | |
| 540 json.byteLength()); | |
| 541 base::Value* value = base::JSONReader::Read(json_string); | |
|
eroman
2014/03/04 02:55:14
Read() can return NULL on failure.
At a minimum E
padolph
2014/03/05 03:08:51
Done.
| |
| 542 base::DictionaryValue* dict_value = NULL; | |
| 543 value->GetAsDictionary(&dict_value); | |
| 544 return scoped_ptr<base::DictionaryValue>(dict_value); | |
| 545 } | |
| 546 | |
| 547 bool CaseInsensitiveStringCompare(const std::string& a, const std::string& b) { | |
|
eroman
2014/03/04 02:55:14
I am surprised this flavor doesn't already exist i
padolph
2014/03/05 03:08:51
Done.
| |
| 548 return std::equal( | |
| 549 a.begin(), a.end(), b.begin(), base::CaseInsensitiveCompareASCII<char>()); | |
| 550 } | |
| 551 | |
| 552 // Verifies that the JSON in the input ArrayBuffer contains the provided | |
| 553 // expected values. Exact matches are required on the fields examined. | |
| 554 ::testing::AssertionResult VerifySymmetricJwk( | |
| 555 const blink::WebArrayBuffer& json, | |
| 556 const std::string& alg_expected, | |
| 557 const std::string& k_expected_hex, | |
| 558 blink::WebCryptoKeyUsageMask use_mask_expected) { | |
| 559 | |
| 560 scoped_ptr<base::DictionaryValue> dict = GetJwkDictionary(json); | |
| 561 if (!dict.get() || dict->empty()) | |
| 562 return ::testing::AssertionFailure() << "JSON parsing failed"; | |
| 563 | |
| 564 // ---- kty | |
| 565 std::string value_string; | |
| 566 if (!dict->GetString("kty", &value_string)) | |
| 567 return ::testing::AssertionFailure() << "Missing 'kty'"; | |
| 568 if (value_string != "oct") | |
| 569 return ::testing::AssertionFailure() | |
| 570 << "Expected 'kty' to be 'oct' but found " << value_string; | |
| 571 | |
| 572 // ---- alg | |
| 573 if (!dict->GetString("alg", &value_string)) | |
| 574 return ::testing::AssertionFailure() << "Missing 'alg'"; | |
| 575 if (value_string != alg_expected) | |
| 576 return ::testing::AssertionFailure() << "Expected 'alg' to be " | |
| 577 << alg_expected << " but found " | |
| 578 << value_string; | |
| 579 | |
| 580 // ---- k | |
| 581 if (!dict->GetString("k", &value_string)) | |
| 582 return ::testing::AssertionFailure() << "Missing 'k'"; | |
| 583 std::string k_value; | |
| 584 if (!webcrypto::Base64DecodeUrlSafe(value_string, &k_value)) | |
| 585 return ::testing::AssertionFailure() << "Base64DecodeUrlSafe(k) failed"; | |
| 586 if (!CaseInsensitiveStringCompare( | |
| 587 k_expected_hex, base::HexEncode(k_value.data(), k_value.size()))) { | |
| 588 return ::testing::AssertionFailure() << "Expected 'k' to be " | |
| 589 << k_expected_hex | |
| 590 << " but found something different"; | |
| 591 } | |
| 592 // ---- ext | |
| 593 // always expect ext == true in this case | |
| 594 bool ext_value; | |
| 595 if (!dict->GetBoolean("ext", &ext_value)) | |
| 596 return ::testing::AssertionFailure() << "Missing 'ext'"; | |
| 597 if (!ext_value) | |
| 598 return ::testing::AssertionFailure() | |
| 599 << "Expected 'ext' to be true but found false"; | |
| 600 | |
| 601 // ---- key_ops | |
| 602 base::ListValue* key_ops; | |
| 603 if (!dict->GetList("key_ops", &key_ops)) | |
| 604 return ::testing::AssertionFailure() << "Missing 'key_ops'"; | |
| 605 blink::WebCryptoKeyUsageMask key_ops_mask = 0; | |
| 606 Status status = GetUsagesFromJwkKeyOps(key_ops, &key_ops_mask); | |
| 607 if (status.IsError()) | |
| 608 return ::testing::AssertionFailure() << "Failure extracting 'key_ops'"; | |
| 609 if (key_ops_mask != use_mask_expected) | |
| 610 return ::testing::AssertionFailure() | |
| 611 << "Expected 'key_ops' mask to be " << use_mask_expected | |
| 612 << " but found " << key_ops_mask << " (" << value_string << ")"; | |
| 613 | |
| 614 return ::testing::AssertionSuccess(); | |
| 615 } | |
| 616 | |
| 537 } // namespace | 617 } // namespace |
| 538 | 618 |
| 539 TEST_F(SharedCryptoTest, StatusToString) { | 619 TEST_F(SharedCryptoTest, StatusToString) { |
| 540 EXPECT_EQ("Success", Status::Success().ToString()); | 620 EXPECT_EQ("Success", Status::Success().ToString()); |
| 541 EXPECT_EQ("", Status::Error().ToString()); | 621 EXPECT_EQ("", Status::Error().ToString()); |
| 542 EXPECT_EQ("The requested operation is unsupported", | 622 EXPECT_EQ("The requested operation is unsupported", |
| 543 Status::ErrorUnsupported().ToString()); | 623 Status::ErrorUnsupported().ToString()); |
| 544 EXPECT_EQ("The required JWK property \"kty\" was missing", | 624 EXPECT_EQ("The required JWK property \"kty\" was missing", |
| 545 Status::ErrorJwkPropertyMissing("kty").ToString()); | 625 Status::ErrorJwkPropertyMissing("kty").ToString()); |
| 546 EXPECT_EQ("The JWK property \"kty\" must be a string", | 626 EXPECT_EQ("The JWK property \"kty\" must be a string", |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 566 blink::WebArrayBuffer output; | 646 blink::WebArrayBuffer output; |
| 567 ASSERT_STATUS_SUCCESS( | 647 ASSERT_STATUS_SUCCESS( |
| 568 Digest(test_algorithm, CryptoData(test_input), &output)); | 648 Digest(test_algorithm, CryptoData(test_input), &output)); |
| 569 ExpectArrayBufferMatches(test_output, output); | 649 ExpectArrayBufferMatches(test_output, output); |
| 570 } | 650 } |
| 571 } | 651 } |
| 572 | 652 |
| 573 TEST_F(SharedCryptoTest, HMACSampleSets) { | 653 TEST_F(SharedCryptoTest, HMACSampleSets) { |
| 574 scoped_ptr<base::ListValue> tests; | 654 scoped_ptr<base::ListValue> tests; |
| 575 ASSERT_TRUE(ReadJsonTestFileToList("hmac.json", &tests)); | 655 ASSERT_TRUE(ReadJsonTestFileToList("hmac.json", &tests)); |
| 576 | 656 // TODO(padolph): Missing known answer tests for HMAC SHA224, SHA384, and |
| 657 // SHA512. | |
| 577 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { | 658 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { |
| 578 SCOPED_TRACE(test_index); | 659 SCOPED_TRACE(test_index); |
| 579 base::DictionaryValue* test; | 660 base::DictionaryValue* test; |
| 580 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); | 661 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); |
| 581 | 662 |
| 582 blink::WebCryptoAlgorithm test_hash = GetDigestAlgorithm(test, "hash"); | 663 blink::WebCryptoAlgorithm test_hash = GetDigestAlgorithm(test, "hash"); |
| 583 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); | 664 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); |
| 584 const std::vector<uint8> test_message = | 665 const std::vector<uint8> test_message = |
| 585 GetBytesFromHexString(test, "message"); | 666 GetBytesFromHexString(test, "message"); |
| 586 const std::vector<uint8> test_mac = GetBytesFromHexString(test, "mac"); | 667 const std::vector<uint8> test_mac = GetBytesFromHexString(test, "mac"); |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 912 // This fails because the algorithm is null. | 993 // This fails because the algorithm is null. |
| 913 EXPECT_STATUS(Status::ErrorMissingAlgorithmImportRawKey(), | 994 EXPECT_STATUS(Status::ErrorMissingAlgorithmImportRawKey(), |
| 914 ImportKey(blink::WebCryptoKeyFormatRaw, | 995 ImportKey(blink::WebCryptoKeyFormatRaw, |
| 915 CryptoData(HexStringToBytes("00000000000000000000")), | 996 CryptoData(HexStringToBytes("00000000000000000000")), |
| 916 blink::WebCryptoAlgorithm::createNull(), | 997 blink::WebCryptoAlgorithm::createNull(), |
| 917 true, | 998 true, |
| 918 blink::WebCryptoKeyUsageEncrypt, | 999 blink::WebCryptoKeyUsageEncrypt, |
| 919 &key)); | 1000 &key)); |
| 920 } | 1001 } |
| 921 | 1002 |
| 1003 TEST_F(SharedCryptoTest, ImportJwkKeyUsage) { | |
|
eroman
2014/03/04 02:55:14
I haven't read through this in detail yet (will do
padolph
2014/03/05 03:08:51
I'm not sure I agree, for the following reasons:
| |
| 1004 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | |
| 1005 base::DictionaryValue dict; | |
| 1006 dict.SetString("kty", "oct"); | |
| 1007 dict.SetBoolean("ext", false); | |
| 1008 dict.SetString("k", "GADWrMRHwQfoNaXU5fZvTg=="); | |
| 1009 const blink::WebCryptoAlgorithm aes_cbc_algorithm = | |
| 1010 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc); | |
| 1011 const blink::WebCryptoAlgorithm hmac_algorithm = | |
| 1012 webcrypto::CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha256); | |
| 1013 const blink::WebCryptoAlgorithm aes_kw_algorithm = | |
| 1014 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); | |
| 1015 | |
| 1016 // Test null usage. | |
| 1017 base::ListValue* key_ops = new base::ListValue; | |
| 1018 // Note: the following call makes dict assume ownership of key_ops. | |
| 1019 dict.Set("key_ops", key_ops); | |
| 1020 EXPECT_STATUS_SUCCESS( | |
| 1021 ImportKeyJwkFromDict(dict, aes_cbc_algorithm, false, 0, &key)); | |
| 1022 EXPECT_EQ(0, key.usages()); | |
| 1023 | |
| 1024 // Test each key_ops value translates to the correct Web Crypto value. | |
| 1025 struct TestCase { | |
| 1026 const char* jwk_key_op; | |
| 1027 const char* jwk_alg; | |
| 1028 const blink::WebCryptoAlgorithm algorithm; | |
| 1029 const blink::WebCryptoKeyUsage usage; | |
| 1030 }; | |
| 1031 // TODO(padolph): Add 'deriveBits' key_ops value once it is supported. | |
| 1032 const TestCase test_case[] = { | |
| 1033 {"encrypt", "A128CBC", aes_cbc_algorithm, | |
| 1034 blink::WebCryptoKeyUsageEncrypt}, | |
| 1035 {"decrypt", "A128CBC", aes_cbc_algorithm, | |
| 1036 blink::WebCryptoKeyUsageDecrypt}, | |
| 1037 {"sign", "HS256", hmac_algorithm, blink::WebCryptoKeyUsageSign}, | |
| 1038 {"verify", "HS256", hmac_algorithm, blink::WebCryptoKeyUsageVerify}, | |
| 1039 {"wrapKey", "A128KW", aes_kw_algorithm, blink::WebCryptoKeyUsageWrapKey}, | |
| 1040 {"unwrapKey", "A128KW", aes_kw_algorithm, | |
| 1041 blink::WebCryptoKeyUsageUnwrapKey}, | |
| 1042 {"deriveKey", "HS256", hmac_algorithm, | |
| 1043 blink::WebCryptoKeyUsageDeriveKey}}; | |
| 1044 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_case); ++i) { | |
| 1045 dict.SetString("alg", test_case[i].jwk_alg); | |
| 1046 key_ops->Clear(); | |
| 1047 key_ops->AppendString(test_case[i].jwk_key_op); | |
| 1048 EXPECT_STATUS_SUCCESS(ImportKeyJwkFromDict( | |
| 1049 dict, test_case[i].algorithm, false, test_case[i].usage, &key)); | |
| 1050 EXPECT_EQ(test_case[i].usage, key.usages()); | |
| 1051 } | |
| 1052 | |
| 1053 // Test discrete multiple usages. | |
| 1054 dict.SetString("alg", "A128CBC"); | |
| 1055 key_ops->Clear(); | |
| 1056 key_ops->AppendString("encrypt"); | |
| 1057 key_ops->AppendString("decrypt"); | |
| 1058 EXPECT_STATUS_SUCCESS(ImportKeyJwkFromDict( | |
| 1059 dict, | |
| 1060 aes_cbc_algorithm, | |
| 1061 false, | |
| 1062 blink::WebCryptoKeyUsageDecrypt | blink::WebCryptoKeyUsageEncrypt, | |
| 1063 &key)); | |
| 1064 EXPECT_EQ(blink::WebCryptoKeyUsageDecrypt | blink::WebCryptoKeyUsageEncrypt, | |
| 1065 key.usages()); | |
| 1066 | |
| 1067 // Test constrained key usage (input usage is a subset of JWK usage). | |
| 1068 key_ops->Clear(); | |
| 1069 key_ops->AppendString("encrypt"); | |
| 1070 key_ops->AppendString("decrypt"); | |
| 1071 EXPECT_STATUS_SUCCESS(ImportKeyJwkFromDict( | |
| 1072 dict, aes_cbc_algorithm, false, blink::WebCryptoKeyUsageDecrypt, &key)); | |
| 1073 EXPECT_EQ(blink::WebCryptoKeyUsageDecrypt, key.usages()); | |
| 1074 | |
| 1075 // Test failure if input usage is NOT a strict subset of the JWK usage. | |
| 1076 key_ops->Clear(); | |
| 1077 key_ops->AppendString("encrypt"); | |
| 1078 EXPECT_STATUS(Status::ErrorJwkKeyopsInconsistent(), | |
| 1079 ImportKeyJwkFromDict(dict, | |
| 1080 aes_cbc_algorithm, | |
| 1081 false, | |
| 1082 blink::WebCryptoKeyUsageEncrypt | | |
| 1083 blink::WebCryptoKeyUsageDecrypt, | |
| 1084 &key)); | |
| 1085 | |
| 1086 // Test 'use' inconsistent with 'key_ops'. | |
| 1087 dict.SetString("alg", "HS256"); | |
| 1088 dict.SetString("use", "sig"); | |
| 1089 key_ops->AppendString("sign"); | |
| 1090 key_ops->AppendString("verify"); | |
| 1091 key_ops->AppendString("encrypt"); | |
| 1092 EXPECT_STATUS(Status::ErrorJwkUseAndKeyopsInconsistent(), | |
| 1093 ImportKeyJwkFromDict(dict, | |
| 1094 hmac_algorithm, | |
| 1095 false, | |
| 1096 blink::WebCryptoKeyUsageSign | | |
| 1097 blink::WebCryptoKeyUsageVerify, | |
| 1098 &key)); | |
| 1099 | |
| 1100 // Test JWK composite 'sig' use | |
| 1101 dict.Remove("key_ops", NULL); | |
| 1102 dict.SetString("use", "sig"); | |
| 1103 EXPECT_STATUS_SUCCESS(ImportKeyJwkFromDict( | |
| 1104 dict, | |
| 1105 hmac_algorithm, | |
| 1106 false, | |
| 1107 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify, | |
| 1108 &key)); | |
| 1109 EXPECT_EQ(blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify, | |
| 1110 key.usages()); | |
| 1111 | |
| 1112 // Test JWK composite use 'enc' usage | |
| 1113 dict.SetString("alg", "A128CBC"); | |
| 1114 dict.SetString("use", "enc"); | |
| 1115 EXPECT_STATUS_SUCCESS(ImportKeyJwkFromDict( | |
| 1116 dict, | |
| 1117 aes_cbc_algorithm, | |
| 1118 false, | |
| 1119 blink::WebCryptoKeyUsageDecrypt | blink::WebCryptoKeyUsageEncrypt | | |
| 1120 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey | | |
| 1121 blink::WebCryptoKeyUsageDeriveKey, | |
| 1122 &key)); | |
| 1123 EXPECT_EQ(blink::WebCryptoKeyUsageDecrypt | blink::WebCryptoKeyUsageEncrypt | | |
| 1124 blink::WebCryptoKeyUsageWrapKey | | |
| 1125 blink::WebCryptoKeyUsageUnwrapKey | | |
| 1126 blink::WebCryptoKeyUsageDeriveKey, | |
| 1127 key.usages()); | |
| 1128 } | |
| 1129 | |
| 922 TEST_F(SharedCryptoTest, ImportJwkFailures) { | 1130 TEST_F(SharedCryptoTest, ImportJwkFailures) { |
| 923 | 1131 |
| 924 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 1132 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 925 blink::WebCryptoAlgorithm algorithm = | 1133 blink::WebCryptoAlgorithm algorithm = |
| 926 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc); | 1134 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc); |
| 927 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageEncrypt; | 1135 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageEncrypt; |
| 928 | 1136 |
| 929 // Baseline pass: each test below breaks a single item, so we start with a | 1137 // Baseline pass: each test below breaks a single item, so we start with a |
| 930 // passing case to make sure each failure is caused by the isolated break. | 1138 // passing case to make sure each failure is caused by the isolated break. |
| 931 // Each breaking subtest below resets the dictionary to this passing case when | 1139 // Each breaking subtest below resets the dictionary to this passing case when |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 981 RestoreJwkOctDictionary(&dict); | 1189 RestoreJwkOctDictionary(&dict); |
| 982 | 1190 |
| 983 // Fail on kty wrong type. | 1191 // Fail on kty wrong type. |
| 984 dict.SetDouble("kty", 0.1); | 1192 dict.SetDouble("kty", 0.1); |
| 985 EXPECT_STATUS(Status::ErrorJwkPropertyWrongType("kty", "string"), | 1193 EXPECT_STATUS(Status::ErrorJwkPropertyWrongType("kty", "string"), |
| 986 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); | 1194 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); |
| 987 RestoreJwkOctDictionary(&dict); | 1195 RestoreJwkOctDictionary(&dict); |
| 988 | 1196 |
| 989 // Fail on invalid use. | 1197 // Fail on invalid use. |
| 990 dict.SetString("use", "foo"); | 1198 dict.SetString("use", "foo"); |
| 991 EXPECT_STATUS(Status::ErrorJwkUnrecognizedUsage(), | 1199 EXPECT_STATUS(Status::ErrorJwkUnrecognizedUse(), |
| 992 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); | 1200 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); |
| 993 RestoreJwkOctDictionary(&dict); | 1201 RestoreJwkOctDictionary(&dict); |
| 994 | 1202 |
| 995 // Fail on invalid use (wrong type). | 1203 // Fail on invalid use (wrong type). |
| 996 dict.SetBoolean("use", true); | 1204 dict.SetBoolean("use", true); |
| 997 EXPECT_STATUS(Status::ErrorJwkPropertyWrongType("use", "string"), | 1205 EXPECT_STATUS(Status::ErrorJwkPropertyWrongType("use", "string"), |
| 998 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); | 1206 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); |
| 999 RestoreJwkOctDictionary(&dict); | 1207 RestoreJwkOctDictionary(&dict); |
| 1000 | 1208 |
| 1001 // Fail on invalid extractable (wrong type). | 1209 // Fail on invalid extractable (wrong type). |
| 1002 dict.SetInteger("extractable", 0); | 1210 dict.SetInteger("ext", 0); |
| 1003 EXPECT_STATUS(Status::ErrorJwkPropertyWrongType("extractable", "boolean"), | 1211 EXPECT_STATUS(Status::ErrorJwkPropertyWrongType("ext", "boolean"), |
| 1212 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); | |
| 1213 RestoreJwkOctDictionary(&dict); | |
| 1214 | |
| 1215 // Fail on invalid key_ops (wrong type). | |
| 1216 dict.SetBoolean("key_ops", true); | |
| 1217 EXPECT_STATUS(Status::ErrorJwkPropertyWrongType("key_ops", "list"), | |
| 1218 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); | |
| 1219 RestoreJwkOctDictionary(&dict); | |
| 1220 | |
| 1221 // Fail on invalid key_ops (wrong element value). | |
| 1222 base::ListValue* key_ops = new base::ListValue; | |
| 1223 // Note: the following call makes dict assume ownership of key_ops. | |
| 1224 dict.Set("key_ops", key_ops); | |
| 1225 key_ops->AppendString("foo"); | |
| 1226 EXPECT_STATUS(Status::ErrorJwkUnrecognizedKeyop(), | |
| 1004 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); | 1227 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); |
| 1005 RestoreJwkOctDictionary(&dict); | 1228 RestoreJwkOctDictionary(&dict); |
| 1006 } | 1229 } |
| 1007 | 1230 |
| 1008 TEST_F(SharedCryptoTest, ImportJwkOctFailures) { | 1231 TEST_F(SharedCryptoTest, ImportJwkOctFailures) { |
| 1009 | 1232 |
| 1010 base::DictionaryValue dict; | 1233 base::DictionaryValue dict; |
| 1011 RestoreJwkOctDictionary(&dict); | 1234 RestoreJwkOctDictionary(&dict); |
| 1012 blink::WebCryptoAlgorithm algorithm = | 1235 blink::WebCryptoAlgorithm algorithm = |
| 1013 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc); | 1236 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc); |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1138 EXPECT_EQ(blink::WebCryptoKeyUsageVerify, key.usages()); | 1361 EXPECT_EQ(blink::WebCryptoKeyUsageVerify, key.usages()); |
| 1139 key = blink::WebCryptoKey::createNull(); | 1362 key = blink::WebCryptoKey::createNull(); |
| 1140 | 1363 |
| 1141 // Consistency rules when JWK value exists: Fail if inconsistency is found. | 1364 // Consistency rules when JWK value exists: Fail if inconsistency is found. |
| 1142 | 1365 |
| 1143 // Pass: All input values are consistent with the JWK values. | 1366 // Pass: All input values are consistent with the JWK values. |
| 1144 dict.Clear(); | 1367 dict.Clear(); |
| 1145 dict.SetString("kty", "oct"); | 1368 dict.SetString("kty", "oct"); |
| 1146 dict.SetString("alg", "HS256"); | 1369 dict.SetString("alg", "HS256"); |
| 1147 dict.SetString("use", "sig"); | 1370 dict.SetString("use", "sig"); |
| 1148 dict.SetBoolean("extractable", false); | 1371 dict.SetBoolean("ext", false); |
| 1149 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg"); | 1372 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg"); |
| 1150 json_vec = MakeJsonVector(dict); | 1373 json_vec = MakeJsonVector(dict); |
| 1151 EXPECT_STATUS_SUCCESS(ImportKeyJwk( | 1374 EXPECT_STATUS_SUCCESS(ImportKeyJwk( |
| 1152 CryptoData(json_vec), algorithm, extractable, usage_mask, &key)); | 1375 CryptoData(json_vec), algorithm, extractable, usage_mask, &key)); |
| 1153 | 1376 |
| 1154 // Extractable cases: | 1377 // Extractable cases: |
| 1155 // 1. input=T, JWK=F ==> fail (inconsistent) | 1378 // 1. input=T, JWK=F ==> fail (inconsistent) |
| 1156 // 4. input=F, JWK=F ==> pass, result extractable is F | 1379 // 4. input=F, JWK=F ==> pass, result extractable is F |
| 1157 // 2. input=T, JWK=T ==> pass, result extractable is T | 1380 // 2. input=T, JWK=T ==> pass, result extractable is T |
| 1158 // 3. input=F, JWK=T ==> pass, result extractable is F | 1381 // 3. input=F, JWK=T ==> pass, result extractable is F |
| 1159 EXPECT_STATUS( | 1382 EXPECT_STATUS( |
| 1160 Status::ErrorJwkExtractableInconsistent(), | 1383 Status::ErrorJwkExtInconsistent(), |
| 1161 ImportKeyJwk(CryptoData(json_vec), algorithm, true, usage_mask, &key)); | 1384 ImportKeyJwk(CryptoData(json_vec), algorithm, true, usage_mask, &key)); |
| 1162 EXPECT_STATUS_SUCCESS( | 1385 EXPECT_STATUS_SUCCESS( |
| 1163 ImportKeyJwk(CryptoData(json_vec), algorithm, false, usage_mask, &key)); | 1386 ImportKeyJwk(CryptoData(json_vec), algorithm, false, usage_mask, &key)); |
| 1164 EXPECT_FALSE(key.extractable()); | 1387 EXPECT_FALSE(key.extractable()); |
| 1165 dict.SetBoolean("extractable", true); | 1388 dict.SetBoolean("ext", true); |
| 1166 EXPECT_STATUS_SUCCESS( | 1389 EXPECT_STATUS_SUCCESS( |
| 1167 ImportKeyJwkFromDict(dict, algorithm, true, usage_mask, &key)); | 1390 ImportKeyJwkFromDict(dict, algorithm, true, usage_mask, &key)); |
| 1168 EXPECT_TRUE(key.extractable()); | 1391 EXPECT_TRUE(key.extractable()); |
| 1169 EXPECT_STATUS_SUCCESS( | 1392 EXPECT_STATUS_SUCCESS( |
| 1170 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); | 1393 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); |
| 1171 EXPECT_FALSE(key.extractable()); | 1394 EXPECT_FALSE(key.extractable()); |
| 1172 dict.SetBoolean("extractable", true); // restore previous value | 1395 dict.SetBoolean("ext", true); // restore previous value |
| 1173 | 1396 |
| 1174 // Fail: Input algorithm (AES-CBC) is inconsistent with JWK value | 1397 // Fail: Input algorithm (AES-CBC) is inconsistent with JWK value |
| 1175 // (HMAC SHA256). | 1398 // (HMAC SHA256). |
| 1176 EXPECT_STATUS(Status::ErrorJwkAlgorithmInconsistent(), | 1399 EXPECT_STATUS(Status::ErrorJwkAlgorithmInconsistent(), |
| 1177 ImportKeyJwk(CryptoData(json_vec), | 1400 ImportKeyJwk(CryptoData(json_vec), |
| 1178 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), | 1401 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), |
| 1179 extractable, | 1402 extractable, |
| 1180 usage_mask, | 1403 usage_mask, |
| 1181 &key)); | 1404 &key)); |
| 1182 | 1405 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 1204 dict, | 1427 dict, |
| 1205 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha256), | 1428 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha256), |
| 1206 extractable, | 1429 extractable, |
| 1207 usage_mask, | 1430 usage_mask, |
| 1208 &key)); | 1431 &key)); |
| 1209 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, algorithm.id()); | 1432 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, algorithm.id()); |
| 1210 dict.SetString("alg", "HS256"); | 1433 dict.SetString("alg", "HS256"); |
| 1211 | 1434 |
| 1212 // Fail: Input usage_mask (encrypt) is not a subset of the JWK value | 1435 // Fail: Input usage_mask (encrypt) is not a subset of the JWK value |
| 1213 // (sign|verify) | 1436 // (sign|verify) |
| 1214 EXPECT_STATUS(Status::ErrorJwkUsageInconsistent(), | 1437 EXPECT_STATUS(Status::ErrorJwkUseInconsistent(), |
| 1215 ImportKeyJwk(CryptoData(json_vec), | 1438 ImportKeyJwk(CryptoData(json_vec), |
| 1216 algorithm, | 1439 algorithm, |
| 1217 extractable, | 1440 extractable, |
| 1218 blink::WebCryptoKeyUsageEncrypt, | 1441 blink::WebCryptoKeyUsageEncrypt, |
| 1219 &key)); | 1442 &key)); |
| 1220 | 1443 |
| 1221 // Fail: Input usage_mask (encrypt|sign|verify) is not a subset of the JWK | 1444 // Fail: Input usage_mask (encrypt|sign|verify) is not a subset of the JWK |
| 1222 // value (sign|verify) | 1445 // value (sign|verify) |
| 1223 usage_mask = blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageSign | | 1446 usage_mask = blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageSign | |
| 1224 blink::WebCryptoKeyUsageVerify; | 1447 blink::WebCryptoKeyUsageVerify; |
| 1225 EXPECT_STATUS( | 1448 EXPECT_STATUS( |
| 1226 Status::ErrorJwkUsageInconsistent(), | 1449 Status::ErrorJwkUseInconsistent(), |
| 1227 ImportKeyJwk( | 1450 ImportKeyJwk( |
| 1228 CryptoData(json_vec), algorithm, extractable, usage_mask, &key)); | 1451 CryptoData(json_vec), algorithm, extractable, usage_mask, &key)); |
| 1229 | 1452 |
| 1230 // TODO(padolph): kty vs alg consistency tests: Depending on the kty value, | 1453 // TODO(padolph): kty vs alg consistency tests: Depending on the kty value, |
| 1231 // only certain alg values are permitted. For example, when kty = "RSA" alg | 1454 // only certain alg values are permitted. For example, when kty = "RSA" alg |
| 1232 // must be of the RSA family, or when kty = "oct" alg must be symmetric | 1455 // must be of the RSA family, or when kty = "oct" alg must be symmetric |
| 1233 // algorithm. | 1456 // algorithm. |
| 1457 | |
| 1458 // TODO(padolph): key_ops consistency tests | |
| 1234 } | 1459 } |
| 1235 | 1460 |
| 1236 TEST_F(SharedCryptoTest, MAYBE(ImportJwkHappy)) { | 1461 TEST_F(SharedCryptoTest, MAYBE(ImportJwkHappy)) { |
| 1237 | 1462 |
| 1238 // This test verifies the happy path of JWK import, including the application | 1463 // This test verifies the happy path of JWK import, including the application |
| 1239 // of the imported key material. | 1464 // of the imported key material. |
| 1240 | 1465 |
| 1241 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 1466 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 1242 bool extractable = false; | 1467 bool extractable = false; |
| 1243 blink::WebCryptoAlgorithm algorithm = | 1468 blink::WebCryptoAlgorithm algorithm = |
| 1244 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha256); | 1469 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha256); |
| 1245 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageSign; | 1470 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageSign; |
| 1246 | 1471 |
| 1247 // Import a symmetric key JWK and HMAC-SHA256 sign() | 1472 // Import a symmetric key JWK and HMAC-SHA256 sign() |
| 1248 // Uses the first SHA256 test vector from the HMAC sample set above. | 1473 // Uses the first SHA256 test vector from the HMAC sample set above. |
| 1249 | 1474 |
| 1250 base::DictionaryValue dict; | 1475 base::DictionaryValue dict; |
| 1251 dict.SetString("kty", "oct"); | 1476 dict.SetString("kty", "oct"); |
| 1252 dict.SetString("alg", "HS256"); | 1477 dict.SetString("alg", "HS256"); |
| 1253 dict.SetString("use", "sig"); | 1478 dict.SetString("use", "sig"); |
| 1254 dict.SetBoolean("extractable", false); | 1479 dict.SetBoolean("ext", false); |
| 1255 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg"); | 1480 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg"); |
| 1256 | 1481 |
| 1257 ASSERT_STATUS_SUCCESS( | 1482 ASSERT_STATUS_SUCCESS( |
| 1258 ImportKeyJwkFromDict(dict, algorithm, extractable, usage_mask, &key)); | 1483 ImportKeyJwkFromDict(dict, algorithm, extractable, usage_mask, &key)); |
| 1259 | 1484 |
| 1260 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256, | 1485 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256, |
| 1261 key.algorithm().hmacParams()->hash().id()); | 1486 key.algorithm().hmacParams()->hash().id()); |
| 1262 | 1487 |
| 1263 const std::vector<uint8> message_raw = HexStringToBytes( | 1488 const std::vector<uint8> message_raw = HexStringToBytes( |
| 1264 "b1689c2591eaf3c9e66070f8a77954ffb81749f1b00346f9dfe0b2ee905dcc288baf4a" | 1489 "b1689c2591eaf3c9e66070f8a77954ffb81749f1b00346f9dfe0b2ee905dcc288baf4a" |
| 1265 "92de3f4001dd9f44c468c3d07d6c6ee82faceafc97c2fc0fc0601719d2dcd0aa2aec92" | 1490 "92de3f4001dd9f44c468c3d07d6c6ee82faceafc97c2fc0fc0601719d2dcd0aa2aec92" |
| 1266 "d1b0ae933c65eb06a03c9c935c2bad0459810241347ab87e9f11adb30415424c6c7f5f" | 1491 "d1b0ae933c65eb06a03c9c935c2bad0459810241347ab87e9f11adb30415424c6c7f5f" |
| 1267 "22a003b8ab8de54f6ded0e3ab9245fa79568451dfa258e"); | 1492 "22a003b8ab8de54f6ded0e3ab9245fa79568451dfa258e"); |
| 1268 | 1493 |
| 1269 blink::WebArrayBuffer output; | 1494 blink::WebArrayBuffer output; |
| 1270 | 1495 |
| 1271 ASSERT_STATUS_SUCCESS(Sign(CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac), | 1496 ASSERT_STATUS_SUCCESS(Sign(CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac), |
| 1272 key, | 1497 key, |
| 1273 CryptoData(message_raw), | 1498 CryptoData(message_raw), |
| 1274 &output)); | 1499 &output)); |
| 1275 | 1500 |
| 1276 const std::string mac_raw = | 1501 const std::string mac_raw = |
| 1277 "769f00d3e6a6cc1fb426a14a4f76c6462e6149726e0dee0ec0cf97a16605ac8b"; | 1502 "769f00d3e6a6cc1fb426a14a4f76c6462e6149726e0dee0ec0cf97a16605ac8b"; |
| 1278 | 1503 |
| 1279 ExpectArrayBufferMatchesHex(mac_raw, output); | 1504 ExpectArrayBufferMatchesHex(mac_raw, output); |
| 1280 | 1505 |
| 1281 // TODO(padolph): Import an RSA public key JWK and use it | 1506 // TODO(padolph): Import an RSA public key JWK and use it |
| 1282 } | 1507 } |
| 1283 | 1508 |
| 1509 TEST_F(SharedCryptoTest, MAYBE(ImportExportJwkSymmetricKey)) { | |
| 1510 // Raw keys are generated by openssl: | |
| 1511 // % openssl rand -hex <key length bytes> | |
| 1512 // TODO(padolph): Move this data to external file? | |
| 1513 const char* const key_hex_128("3f1e7cd4f6f8543f6b1e16002e688623"); | |
| 1514 const char* const key_hex_192( | |
| 1515 "ed91f916dc034eba68a0f9e7f34ddd48b98bd2848109e243"); | |
| 1516 const char* const key_hex_256( | |
| 1517 "bd08286b81a74783fd1ccf46b7e05af84ee25ae021210074159e0c4d9d907692"); | |
| 1518 const char* const key_hex_384( | |
| 1519 "a22c5441c8b185602283d64c7221de1d0951e706bfc09539435ec0e0ed614e1d406623f2" | |
| 1520 "b31d31819fec30993380dd82"); | |
| 1521 const char* const key_hex_512( | |
| 1522 "5834f639000d4cf82de124fbfd26fb88d463e99f839a76ba41ac88967c80a3f61e1239a4" | |
| 1523 "52e573dba0750e988152988576efd75b8d0229b7aca2ada2afd392ee"); | |
| 1524 const blink::WebCryptoAlgorithm aes_cbc_alg = | |
| 1525 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc); | |
| 1526 const blink::WebCryptoAlgorithm aes_gcm_alg = | |
| 1527 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm); | |
| 1528 const blink::WebCryptoAlgorithm aes_kw_alg = | |
| 1529 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); | |
| 1530 const blink::WebCryptoAlgorithm hmac_sha_256_alg = | |
| 1531 webcrypto::CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha256); | |
| 1532 const blink::WebCryptoAlgorithm hmac_sha_384_alg = | |
| 1533 webcrypto::CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha384); | |
| 1534 const blink::WebCryptoAlgorithm hmac_sha_512_alg = | |
| 1535 webcrypto::CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha512); | |
| 1536 | |
| 1537 struct TestCase { | |
| 1538 const char* const key_hex; | |
| 1539 const blink::WebCryptoAlgorithm algorithm; | |
| 1540 const blink::WebCryptoKeyUsageMask usage; | |
| 1541 const char* const jwk_alg; | |
| 1542 }; | |
| 1543 | |
| 1544 // TODO(padolph): Test AES-CTR JWK export, once AES-CTR import works. | |
| 1545 const TestCase kTests[] = { | |
| 1546 // AES-CBC 128 | |
| 1547 {key_hex_128, aes_cbc_alg, | |
| 1548 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt, | |
| 1549 "A128CBC"}, | |
| 1550 // AES-CBC 192 | |
| 1551 {key_hex_192, aes_cbc_alg, blink::WebCryptoKeyUsageEncrypt, "A192CBC"}, | |
| 1552 // AES-CBC 256 | |
| 1553 {key_hex_256, aes_cbc_alg, blink::WebCryptoKeyUsageDecrypt, "A256CBC"}, | |
| 1554 // AES-GCM 128 | |
| 1555 {key_hex_128, aes_gcm_alg, | |
| 1556 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt, | |
| 1557 "A128GCM"}, | |
| 1558 // AES-CGM 192 | |
| 1559 {key_hex_192, aes_gcm_alg, blink::WebCryptoKeyUsageEncrypt, "A192GCM"}, | |
| 1560 // AES-GCM 256 | |
| 1561 {key_hex_256, aes_gcm_alg, blink::WebCryptoKeyUsageDecrypt, "A256GCM"}, | |
| 1562 // AES-KW 128 | |
| 1563 {key_hex_128, aes_kw_alg, | |
| 1564 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey, | |
| 1565 "A128KW"}, | |
| 1566 // AES-KW 192 | |
| 1567 {key_hex_192, aes_kw_alg, | |
| 1568 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey, | |
| 1569 "A192KW"}, | |
| 1570 // AES-KW 256 | |
| 1571 {key_hex_256, aes_kw_alg, | |
| 1572 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey, | |
| 1573 "A256KW"}, | |
| 1574 // HMAC SHA-256 | |
| 1575 {key_hex_256, hmac_sha_256_alg, | |
| 1576 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify, "HS256"}, | |
| 1577 // HMAC SHA-384 | |
| 1578 {key_hex_384, hmac_sha_384_alg, blink::WebCryptoKeyUsageSign, "HS384"}, | |
| 1579 // HMAC SHA-512 | |
| 1580 {key_hex_512, hmac_sha_512_alg, blink::WebCryptoKeyUsageVerify, "HS512"}, | |
| 1581 // Large usage value | |
| 1582 {key_hex_256, aes_cbc_alg, | |
| 1583 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt | | |
| 1584 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey, | |
| 1585 "A256CBC"}, | |
| 1586 // Zero usage value | |
| 1587 {key_hex_512, hmac_sha_512_alg, 0, "HS512"}, }; | |
| 1588 | |
| 1589 // Round-trip import/export each key. | |
| 1590 | |
| 1591 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | |
| 1592 blink::WebArrayBuffer json; | |
| 1593 for (size_t test_index = 0; test_index < ARRAYSIZE_UNSAFE(kTests); | |
| 1594 ++test_index) { | |
| 1595 SCOPED_TRACE(test_index); | |
| 1596 const TestCase& test = kTests[test_index]; | |
| 1597 | |
| 1598 // Import a raw key. | |
| 1599 key = ImportSecretKeyFromRaw( | |
| 1600 HexStringToBytes(test.key_hex), test.algorithm, test.usage); | |
| 1601 | |
| 1602 // Export the key in JWK format and validate. | |
| 1603 ASSERT_STATUS_SUCCESS(ExportKeyJwk(key, &json)); | |
| 1604 EXPECT_TRUE( | |
| 1605 VerifySymmetricJwk(json, test.jwk_alg, test.key_hex, test.usage)); | |
| 1606 | |
| 1607 // Import the JWK-formatted key. | |
| 1608 ASSERT_STATUS_SUCCESS( | |
| 1609 ImportKeyJwk(CryptoData(json), test.algorithm, true, test.usage, &key)); | |
| 1610 EXPECT_TRUE(key.handle()); | |
| 1611 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | |
| 1612 EXPECT_EQ(test.algorithm.id(), key.algorithm().id()); | |
| 1613 EXPECT_EQ(true, key.extractable()); | |
| 1614 EXPECT_EQ(test.usage, key.usages()); | |
| 1615 | |
| 1616 // Export the key in raw format and compare to the original. | |
| 1617 blink::WebArrayBuffer key_raw_out; | |
| 1618 ASSERT_STATUS_SUCCESS( | |
| 1619 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_raw_out)); | |
| 1620 ExpectArrayBufferMatchesHex(test.key_hex, key_raw_out); | |
| 1621 } | |
| 1622 | |
| 1623 // HMAC SHA-1 and SHA-224 are not allowed by the JWK(JWA) spec. | |
|
eroman
2014/03/04 02:55:14
But they are allowed by webcrypto (see spec update
padolph
2014/03/05 03:08:51
My mistake. Fixed.
| |
| 1624 key = ImportSecretKeyFromRaw( | |
| 1625 HexStringToBytes(key_hex_512), | |
| 1626 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1), | |
| 1627 blink::WebCryptoKeyUsageVerify); | |
| 1628 EXPECT_STATUS(ExportKeyJwk(key, &json), | |
| 1629 Status::ErrorJwkUnsupportedHmacHash()); | |
| 1630 key = ImportSecretKeyFromRaw( | |
| 1631 HexStringToBytes(key_hex_512), | |
| 1632 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha224), | |
| 1633 blink::WebCryptoKeyUsageVerify); | |
| 1634 EXPECT_STATUS(ExportKeyJwk(key, &json), | |
| 1635 Status::ErrorJwkUnsupportedHmacHash()); | |
| 1636 } | |
| 1637 | |
| 1284 TEST_F(SharedCryptoTest, MAYBE(ImportExportSpki)) { | 1638 TEST_F(SharedCryptoTest, MAYBE(ImportExportSpki)) { |
| 1285 // Passing case: Import a valid RSA key in SPKI format. | 1639 // Passing case: Import a valid RSA key in SPKI format. |
| 1286 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 1640 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 1287 ASSERT_STATUS_SUCCESS( | 1641 ASSERT_STATUS_SUCCESS( |
| 1288 ImportKey(blink::WebCryptoKeyFormatSpki, | 1642 ImportKey(blink::WebCryptoKeyFormatSpki, |
| 1289 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)), | 1643 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)), |
| 1290 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5), | 1644 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5), |
| 1291 true, | 1645 true, |
| 1292 blink::WebCryptoKeyUsageEncrypt, | 1646 blink::WebCryptoKeyUsageEncrypt, |
| 1293 &key)); | 1647 &key)); |
| (...skipping 970 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2264 test_cipher_text, | 2618 test_cipher_text, |
| 2265 test_authentication_tag, | 2619 test_authentication_tag, |
| 2266 &plain_text)); | 2620 &plain_text)); |
| 2267 } | 2621 } |
| 2268 } | 2622 } |
| 2269 } | 2623 } |
| 2270 | 2624 |
| 2271 } // namespace webcrypto | 2625 } // namespace webcrypto |
| 2272 | 2626 |
| 2273 } // namespace content | 2627 } // namespace content |
| OLD | NEW |