OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "webcrypto_impl.h" | 5 #include "webcrypto_impl.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 } // namespace | 80 } // namespace |
81 | 81 |
82 namespace content { | 82 namespace content { |
83 | 83 |
84 class WebCryptoImplTest : public testing::Test { | 84 class WebCryptoImplTest : public testing::Test { |
85 protected: | 85 protected: |
86 WebKit::WebCryptoKey ImportSecretKeyFromRawHexString( | 86 WebKit::WebCryptoKey ImportSecretKeyFromRawHexString( |
87 const std::string& key_hex, | 87 const std::string& key_hex, |
88 const WebKit::WebCryptoAlgorithm& algorithm, | 88 const WebKit::WebCryptoAlgorithm& algorithm, |
89 WebKit::WebCryptoKeyUsageMask usage) { | 89 WebKit::WebCryptoKeyUsageMask usage) { |
90 WebKit::WebCryptoKeyType type; | |
91 scoped_ptr<WebKit::WebCryptoKeyHandle> handle; | |
92 | |
93 std::vector<uint8> key_raw = HexStringToBytes(key_hex); | 90 std::vector<uint8> key_raw = HexStringToBytes(key_hex); |
94 | 91 |
| 92 WebKit::WebCryptoKey key = WebCryptoImpl::NullKey(); |
| 93 bool extractable = true; |
95 EXPECT_TRUE(crypto_.ImportKeyInternal(WebKit::WebCryptoKeyFormatRaw, | 94 EXPECT_TRUE(crypto_.ImportKeyInternal(WebKit::WebCryptoKeyFormatRaw, |
96 Start(key_raw), | 95 Start(key_raw), |
97 key_raw.size(), | 96 key_raw.size(), |
98 algorithm, | 97 algorithm, |
| 98 extractable, |
99 usage, | 99 usage, |
100 &handle, | 100 &key)); |
101 &type)); | |
102 | 101 |
103 EXPECT_EQ(WebKit::WebCryptoKeyTypeSecret, type); | 102 EXPECT_EQ(WebKit::WebCryptoKeyTypeSecret, key.type()); |
104 EXPECT_TRUE(handle.get()); | 103 EXPECT_TRUE(key.handle()); |
105 | 104 return key; |
106 return WebKit::WebCryptoKey::create( | |
107 handle.release(), type, false, algorithm, usage); | |
108 } | 105 } |
109 | 106 |
110 // Forwarding methods to gain access to protected methods of | 107 // Forwarding methods to gain access to protected methods of |
111 // WebCryptoImpl. | 108 // WebCryptoImpl. |
112 | 109 |
113 bool DigestInternal( | 110 bool DigestInternal( |
114 const WebKit::WebCryptoAlgorithm& algorithm, | 111 const WebKit::WebCryptoAlgorithm& algorithm, |
115 const std::vector<uint8>& data, | 112 const std::vector<uint8>& data, |
116 WebKit::WebArrayBuffer* buffer) { | 113 WebKit::WebArrayBuffer* buffer) { |
117 return crypto_.DigestInternal(algorithm, Start(data), data.size(), buffer); | 114 return crypto_.DigestInternal(algorithm, Start(data), data.size(), buffer); |
118 } | 115 } |
119 | 116 |
120 bool GenerateKeyInternal( | 117 bool GenerateKeyInternal( |
121 const WebKit::WebCryptoAlgorithm& algorithm, | 118 const WebKit::WebCryptoAlgorithm& algorithm, |
122 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle, | 119 WebKit::WebCryptoKey* key) { |
123 WebKit::WebCryptoKeyType* type) { | 120 bool extractable = true; |
124 return crypto_.GenerateKeyInternal(algorithm, handle, type); | 121 WebKit::WebCryptoKeyUsageMask usage_mask = 0; |
| 122 return crypto_.GenerateKeyInternal(algorithm, extractable, usage_mask, key); |
125 } | 123 } |
126 | 124 |
127 bool ImportKeyInternal( | 125 bool ImportKeyInternal( |
128 WebKit::WebCryptoKeyFormat format, | 126 WebKit::WebCryptoKeyFormat format, |
129 const std::vector<uint8>& key_data, | 127 const std::vector<uint8>& key_data, |
130 const WebKit::WebCryptoAlgorithm& algorithm, | 128 const WebKit::WebCryptoAlgorithm& algorithm, |
131 WebKit::WebCryptoKeyUsageMask usage_mask, | 129 WebKit::WebCryptoKeyUsageMask usage_mask, |
132 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle, | 130 WebKit::WebCryptoKey* key) { |
133 WebKit::WebCryptoKeyType* type) { | 131 bool extractable = true; |
134 return crypto_.ImportKeyInternal(format, | 132 return crypto_.ImportKeyInternal(format, |
135 Start(key_data), | 133 Start(key_data), |
136 key_data.size(), | 134 key_data.size(), |
137 algorithm, | 135 algorithm, |
| 136 extractable, |
138 usage_mask, | 137 usage_mask, |
139 handle, | 138 key); |
140 type); | |
141 } | 139 } |
142 | 140 |
143 bool SignInternal( | 141 bool SignInternal( |
144 const WebKit::WebCryptoAlgorithm& algorithm, | 142 const WebKit::WebCryptoAlgorithm& algorithm, |
145 const WebKit::WebCryptoKey& key, | 143 const WebKit::WebCryptoKey& key, |
146 const std::vector<uint8>& data, | 144 const std::vector<uint8>& data, |
147 WebKit::WebArrayBuffer* buffer) { | 145 WebKit::WebArrayBuffer* buffer) { |
148 return crypto_.SignInternal( | 146 return crypto_.SignInternal( |
149 algorithm, key, Start(data), data.size(), buffer); | 147 algorithm, key, Start(data), data.size(), buffer); |
150 } | 148 } |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 unsigned input_len = INT_MAX - 3; | 463 unsigned input_len = INT_MAX - 3; |
466 | 464 |
467 EXPECT_FALSE(EncryptInternal( | 465 EXPECT_FALSE(EncryptInternal( |
468 CreateAesCbcAlgorithm(iv), key, input, input_len, &output)); | 466 CreateAesCbcAlgorithm(iv), key, input, input_len, &output)); |
469 EXPECT_FALSE(DecryptInternal( | 467 EXPECT_FALSE(DecryptInternal( |
470 CreateAesCbcAlgorithm(iv), key, input, input_len, &output)); | 468 CreateAesCbcAlgorithm(iv), key, input, input_len, &output)); |
471 } | 469 } |
472 | 470 |
473 // Fail importing the key (too few bytes specified) | 471 // Fail importing the key (too few bytes specified) |
474 { | 472 { |
475 WebKit::WebCryptoKeyType type; | |
476 scoped_ptr<WebKit::WebCryptoKeyHandle> handle; | |
477 | |
478 std::vector<uint8> key_raw(1); | 473 std::vector<uint8> key_raw(1); |
479 std::vector<uint8> iv(16); | 474 std::vector<uint8> iv(16); |
480 | 475 |
| 476 WebKit::WebCryptoKey key = WebCryptoImpl::NullKey(); |
481 EXPECT_FALSE(ImportKeyInternal(WebKit::WebCryptoKeyFormatRaw, | 477 EXPECT_FALSE(ImportKeyInternal(WebKit::WebCryptoKeyFormatRaw, |
482 key_raw, | 478 key_raw, |
483 CreateAesCbcAlgorithm(iv), | 479 CreateAesCbcAlgorithm(iv), |
484 WebKit::WebCryptoKeyUsageDecrypt, | 480 WebKit::WebCryptoKeyUsageDecrypt, |
485 &handle, | 481 &key)); |
486 &type)); | |
487 } | 482 } |
488 } | 483 } |
489 | 484 |
490 TEST_F(WebCryptoImplTest, AesCbcSampleSets) { | 485 TEST_F(WebCryptoImplTest, AesCbcSampleSets) { |
491 struct TestCase { | 486 struct TestCase { |
492 const char* key; | 487 const char* key; |
493 const char* iv; | 488 const char* iv; |
494 const char* plain_text; | 489 const char* plain_text; |
495 const char* cipher_text; | 490 const char* cipher_text; |
496 }; | 491 }; |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
615 cipher_text.size() - 3, | 610 cipher_text.size() - 3, |
616 &output)); | 611 &output)); |
617 } | 612 } |
618 } | 613 } |
619 } | 614 } |
620 | 615 |
621 // TODO (padolph): Add test to verify generated symmetric keys appear random. | 616 // TODO (padolph): Add test to verify generated symmetric keys appear random. |
622 | 617 |
623 | 618 |
624 TEST_F(WebCryptoImplTest, GenerateKeyAes) { | 619 TEST_F(WebCryptoImplTest, GenerateKeyAes) { |
625 scoped_ptr<WebKit::WebCryptoKeyHandle> result; | 620 WebKit::WebCryptoKey key = WebCryptoImpl::NullKey(); |
626 WebKit::WebCryptoKeyType type = WebKit::WebCryptoKeyTypePublic; | 621 ASSERT_TRUE(GenerateKeyInternal(CreateAesCbcAlgorithm(128), &key)); |
627 ASSERT_TRUE(GenerateKeyInternal(CreateAesCbcAlgorithm(128), &result, &type)); | 622 EXPECT_TRUE(key.handle()); |
628 EXPECT_TRUE(result); | 623 EXPECT_EQ(WebKit::WebCryptoKeyTypeSecret, key.type()); |
629 EXPECT_EQ(type, WebKit::WebCryptoKeyTypeSecret); | |
630 } | 624 } |
631 | 625 |
632 TEST_F(WebCryptoImplTest, GenerateKeyAesBadLength) { | 626 TEST_F(WebCryptoImplTest, GenerateKeyAesBadLength) { |
633 scoped_ptr<WebKit::WebCryptoKeyHandle> result; | 627 WebKit::WebCryptoKey key = WebCryptoImpl::NullKey(); |
634 WebKit::WebCryptoKeyType type = WebKit::WebCryptoKeyTypePublic; | 628 EXPECT_FALSE(GenerateKeyInternal(CreateAesCbcAlgorithm(0), &key)); |
635 EXPECT_FALSE(GenerateKeyInternal(CreateAesCbcAlgorithm(0), &result, &type)); | 629 EXPECT_FALSE(GenerateKeyInternal(CreateAesCbcAlgorithm(129), &key)); |
636 EXPECT_FALSE(GenerateKeyInternal(CreateAesCbcAlgorithm(129), &result, &type)); | |
637 } | 630 } |
638 | 631 |
639 TEST_F(WebCryptoImplTest, GenerateKeyHmac) { | 632 TEST_F(WebCryptoImplTest, GenerateKeyHmac) { |
640 scoped_ptr<WebKit::WebCryptoKeyHandle> result; | 633 WebKit::WebCryptoKey key = WebCryptoImpl::NullKey(); |
641 WebKit::WebCryptoKeyType type = WebKit::WebCryptoKeyTypePublic; | |
642 WebKit::WebCryptoAlgorithm algorithm = | 634 WebKit::WebCryptoAlgorithm algorithm = |
643 CreateHmacKeyAlgorithm(WebKit::WebCryptoAlgorithmIdSha1, 128); | 635 CreateHmacKeyAlgorithm(WebKit::WebCryptoAlgorithmIdSha1, 128); |
644 ASSERT_TRUE(GenerateKeyInternal(algorithm, &result, &type)); | 636 ASSERT_TRUE(GenerateKeyInternal(algorithm, &key)); |
645 EXPECT_TRUE(result); | 637 EXPECT_TRUE(key.handle()); |
646 EXPECT_EQ(type, WebKit::WebCryptoKeyTypeSecret); | 638 EXPECT_EQ(WebKit::WebCryptoKeyTypeSecret, key.type()); |
647 } | 639 } |
648 | 640 |
649 TEST_F(WebCryptoImplTest, GenerateKeyHmacNoLength) { | 641 TEST_F(WebCryptoImplTest, GenerateKeyHmacNoLength) { |
650 scoped_ptr<WebKit::WebCryptoKeyHandle> result; | 642 WebKit::WebCryptoKey key = WebCryptoImpl::NullKey(); |
651 WebKit::WebCryptoKeyType type = WebKit::WebCryptoKeyTypePublic; | |
652 WebKit::WebCryptoAlgorithm algorithm = | 643 WebKit::WebCryptoAlgorithm algorithm = |
653 CreateHmacKeyAlgorithm(WebKit::WebCryptoAlgorithmIdSha1, 0); | 644 CreateHmacKeyAlgorithm(WebKit::WebCryptoAlgorithmIdSha1, 0); |
654 ASSERT_TRUE(GenerateKeyInternal(algorithm, &result, &type)); | 645 ASSERT_TRUE(GenerateKeyInternal(algorithm, &key)); |
655 EXPECT_TRUE(result); | 646 EXPECT_TRUE(key.handle()); |
656 EXPECT_EQ(type, WebKit::WebCryptoKeyTypeSecret); | 647 EXPECT_EQ(WebKit::WebCryptoKeyTypeSecret, key.type()); |
| 648 } |
| 649 |
| 650 TEST_F(WebCryptoImplTest, ImportSecretKeyNoAlgorithm) { |
| 651 WebKit::WebCryptoKey key = WebCryptoImpl::NullKey(); |
| 652 |
| 653 // This fails because the algorithm is null. |
| 654 EXPECT_FALSE(ImportKeyInternal( |
| 655 WebKit::WebCryptoKeyFormatRaw, |
| 656 HexStringToBytes("00000000000000000000"), |
| 657 WebKit::WebCryptoAlgorithm::createNull(), |
| 658 WebKit::WebCryptoKeyUsageSign, |
| 659 &key)); |
657 } | 660 } |
658 | 661 |
659 } // namespace content | 662 } // namespace content |
OLD | NEW |