Chromium Code Reviews| 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 "content/renderer/webcrypto/webcrypto_impl.h" |
| 6 | |
| 7 #include <string> | |
| 8 #include <vector> | |
| 6 | 9 |
| 7 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 8 #include "base/logging.h" | 11 #include "base/logging.h" |
| 9 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 10 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 11 #include "content/public/renderer/content_renderer_client.h" | 14 #include "content/public/renderer/content_renderer_client.h" |
| 12 #include "content/renderer/renderer_webkitplatformsupport_impl.h" | 15 #include "content/renderer/renderer_webkitplatformsupport_impl.h" |
| 13 #include "content/renderer/webcrypto/webcrypto_impl.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | 17 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
| 16 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 18 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| 17 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 19 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 18 #include "third_party/WebKit/public/platform/WebCryptoKey.h" | 20 #include "third_party/WebKit/public/platform/WebCryptoKey.h" |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 std::vector<uint8> HexStringToBytes(const std::string& hex) { | 24 std::vector<uint8> HexStringToBytes(const std::string& hex) { |
| 23 std::vector<uint8> bytes; | 25 std::vector<uint8> bytes; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 } | 67 } |
| 66 | 68 |
| 67 WebKit::WebCryptoAlgorithm CreateAesCbcAlgorithm( | 69 WebKit::WebCryptoAlgorithm CreateAesCbcAlgorithm( |
| 68 const std::vector<uint8>& iv) { | 70 const std::vector<uint8>& iv) { |
| 69 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | 71 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 70 WebKit::WebCryptoAlgorithmIdAesCbc, | 72 WebKit::WebCryptoAlgorithmIdAesCbc, |
| 71 new WebKit::WebCryptoAesCbcParams(Start(iv), iv.size())); | 73 new WebKit::WebCryptoAesCbcParams(Start(iv), iv.size())); |
| 72 } | 74 } |
| 73 | 75 |
| 74 WebKit::WebCryptoAlgorithm CreateAesCbcAlgorithm( | 76 WebKit::WebCryptoAlgorithm CreateAesCbcAlgorithm( |
| 75 unsigned short key_length_bits) { | 77 unsigned short key_length_bits) { // NOLINT |
|
eroman
2013/11/05 01:06:39
what is the lint error that this line gives?
| |
| 76 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | 78 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 77 WebKit::WebCryptoAlgorithmIdAesCbc, | 79 WebKit::WebCryptoAlgorithmIdAesCbc, |
| 78 new WebKit::WebCryptoAesKeyGenParams(key_length_bits)); | 80 new WebKit::WebCryptoAesKeyGenParams(key_length_bits)); |
| 79 } | 81 } |
| 80 | 82 |
| 81 WebKit::WebCryptoAlgorithm CreateRsaAlgorithm( | 83 WebKit::WebCryptoAlgorithm CreateRsaAlgorithm( |
| 82 WebKit::WebCryptoAlgorithmId algorithm_id, | 84 WebKit::WebCryptoAlgorithmId algorithm_id, |
| 83 unsigned modulus_length, | 85 unsigned modulus_length, |
| 84 const std::vector<uint8>& public_exponent) { | 86 const std::vector<uint8>& public_exponent) { |
| 85 DCHECK(algorithm_id == WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 || | 87 DCHECK(algorithm_id == WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 || |
| (...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 630 if (cipher_text.size() > 3) { | 632 if (cipher_text.size() > 3) { |
| 631 EXPECT_FALSE(DecryptInternal(CreateAesCbcAlgorithm(iv), | 633 EXPECT_FALSE(DecryptInternal(CreateAesCbcAlgorithm(iv), |
| 632 key, | 634 key, |
| 633 &cipher_text[0], | 635 &cipher_text[0], |
| 634 cipher_text.size() - 3, | 636 cipher_text.size() - 3, |
| 635 &output)); | 637 &output)); |
| 636 } | 638 } |
| 637 } | 639 } |
| 638 } | 640 } |
| 639 | 641 |
| 640 // TODO (padolph): Add test to verify generated symmetric keys appear random. | 642 // TODO(padolph): Add test to verify generated symmetric keys appear random. |
| 641 | |
| 642 | 643 |
| 643 TEST_F(WebCryptoImplTest, GenerateKeyAes) { | 644 TEST_F(WebCryptoImplTest, GenerateKeyAes) { |
| 644 WebKit::WebCryptoKey key = WebKit::WebCryptoKey::createNull(); | 645 WebKit::WebCryptoKey key = WebKit::WebCryptoKey::createNull(); |
| 645 ASSERT_TRUE(GenerateKeyInternal(CreateAesCbcAlgorithm(128), &key)); | 646 ASSERT_TRUE(GenerateKeyInternal(CreateAesCbcAlgorithm(128), &key)); |
| 646 EXPECT_TRUE(key.handle()); | 647 EXPECT_TRUE(key.handle()); |
| 647 EXPECT_EQ(WebKit::WebCryptoKeyTypeSecret, key.type()); | 648 EXPECT_EQ(WebKit::WebCryptoKeyTypeSecret, key.type()); |
| 648 } | 649 } |
| 649 | 650 |
| 650 TEST_F(WebCryptoImplTest, GenerateKeyAesBadLength) { | 651 TEST_F(WebCryptoImplTest, GenerateKeyAesBadLength) { |
| 651 WebKit::WebCryptoKey key = WebKit::WebCryptoKey::createNull(); | 652 WebKit::WebCryptoKey key = WebKit::WebCryptoKey::createNull(); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 679 WebKit::WebCryptoKeyFormatRaw, | 680 WebKit::WebCryptoKeyFormatRaw, |
| 680 HexStringToBytes("00000000000000000000"), | 681 HexStringToBytes("00000000000000000000"), |
| 681 WebKit::WebCryptoAlgorithm::createNull(), | 682 WebKit::WebCryptoAlgorithm::createNull(), |
| 682 WebKit::WebCryptoKeyUsageSign, | 683 WebKit::WebCryptoKeyUsageSign, |
| 683 &key)); | 684 &key)); |
| 684 } | 685 } |
| 685 | 686 |
| 686 #if !defined(USE_OPENSSL) | 687 #if !defined(USE_OPENSSL) |
| 687 | 688 |
| 688 TEST_F(WebCryptoImplTest, GenerateKeyPairRsa) { | 689 TEST_F(WebCryptoImplTest, GenerateKeyPairRsa) { |
| 689 | |
| 690 // Note: using unrealistic short key lengths here to avoid bogging down tests. | 690 // Note: using unrealistic short key lengths here to avoid bogging down tests. |
| 691 | 691 |
| 692 // Successful WebCryptoAlgorithmIdRsaEsPkcs1v1_5 key generation. | 692 // Successful WebCryptoAlgorithmIdRsaEsPkcs1v1_5 key generation. |
| 693 const unsigned modulus_length = 256; | 693 const unsigned modulus_length = 256; |
| 694 const std::vector<uint8> public_exponent = HexStringToBytes("010001"); | 694 const std::vector<uint8> public_exponent = HexStringToBytes("010001"); |
| 695 WebKit::WebCryptoAlgorithm algorithm = | 695 WebKit::WebCryptoAlgorithm algorithm = |
| 696 CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, | 696 CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, |
| 697 modulus_length, | 697 modulus_length, |
| 698 public_exponent); | 698 public_exponent); |
| 699 const bool extractable = false; | 699 bool extractable = false; |
| 700 const WebKit::WebCryptoKeyUsageMask usage_mask = 0; | 700 const WebKit::WebCryptoKeyUsageMask usage_mask = 0; |
| 701 WebKit::WebCryptoKey public_key = WebKit::WebCryptoKey::createNull(); | 701 WebKit::WebCryptoKey public_key = WebKit::WebCryptoKey::createNull(); |
| 702 WebKit::WebCryptoKey private_key = WebKit::WebCryptoKey::createNull(); | 702 WebKit::WebCryptoKey private_key = WebKit::WebCryptoKey::createNull(); |
| 703 EXPECT_TRUE(GenerateKeyPairInternal( | 703 EXPECT_TRUE(GenerateKeyPairInternal( |
| 704 algorithm, extractable, usage_mask, &public_key, &private_key)); | 704 algorithm, extractable, usage_mask, &public_key, &private_key)); |
| 705 EXPECT_FALSE(public_key.isNull()); | 705 EXPECT_FALSE(public_key.isNull()); |
| 706 EXPECT_FALSE(private_key.isNull()); | 706 EXPECT_FALSE(private_key.isNull()); |
| 707 EXPECT_EQ(WebKit::WebCryptoKeyTypePublic, public_key.type()); | 707 EXPECT_EQ(WebKit::WebCryptoKeyTypePublic, public_key.type()); |
| 708 EXPECT_EQ(WebKit::WebCryptoKeyTypePrivate, private_key.type()); | 708 EXPECT_EQ(WebKit::WebCryptoKeyTypePrivate, private_key.type()); |
| 709 EXPECT_EQ(extractable, public_key.extractable()); | 709 EXPECT_EQ(extractable, public_key.extractable()); |
| 710 EXPECT_EQ(extractable, private_key.extractable()); | 710 EXPECT_EQ(extractable, private_key.extractable()); |
| 711 EXPECT_EQ(usage_mask, public_key.usages()); | 711 EXPECT_EQ(usage_mask, public_key.usages()); |
| 712 EXPECT_EQ(usage_mask, private_key.usages()); | 712 EXPECT_EQ(usage_mask, private_key.usages()); |
| 713 | 713 |
| 714 // Fail with bad modulus. | 714 // Fail with bad modulus. |
| 715 algorithm = CreateRsaAlgorithm( | 715 algorithm = CreateRsaAlgorithm( |
| 716 WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 0, public_exponent); | 716 WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 0, public_exponent); |
| 717 EXPECT_FALSE(GenerateKeyPairInternal( | 717 EXPECT_FALSE(GenerateKeyPairInternal( |
| 718 algorithm, extractable, usage_mask, &public_key, &private_key)); | 718 algorithm, extractable, usage_mask, &public_key, &private_key)); |
| 719 | 719 |
| 720 // Fail with bad exponent: larger than unsigned long. | 720 // Fail with bad exponent: larger than unsigned long. |
| 721 unsigned exponent_length = sizeof(unsigned long) + 1; | 721 unsigned exponent_length = sizeof(unsigned long) + 1; // NOLINT |
|
eroman
2013/11/05 01:06:39
what is the lint error that this line gives?
| |
| 722 const std::vector<uint8> long_exponent(exponent_length, 0x01); | 722 const std::vector<uint8> long_exponent(exponent_length, 0x01); |
| 723 algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, | 723 algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, |
| 724 modulus_length, | 724 modulus_length, |
| 725 long_exponent); | 725 long_exponent); |
| 726 EXPECT_FALSE(GenerateKeyPairInternal( | 726 EXPECT_FALSE(GenerateKeyPairInternal( |
| 727 algorithm, extractable, usage_mask, &public_key, &private_key)); | 727 algorithm, extractable, usage_mask, &public_key, &private_key)); |
| 728 | 728 |
| 729 // Fail with bad exponent: empty. | 729 // Fail with bad exponent: empty. |
| 730 const std::vector<uint8> empty_exponent; | 730 const std::vector<uint8> empty_exponent; |
| 731 algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, | 731 algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 786 EXPECT_EQ(WebKit::WebCryptoKeyTypePrivate, private_key.type()); | 786 EXPECT_EQ(WebKit::WebCryptoKeyTypePrivate, private_key.type()); |
| 787 EXPECT_EQ(extractable, public_key.extractable()); | 787 EXPECT_EQ(extractable, public_key.extractable()); |
| 788 EXPECT_EQ(extractable, private_key.extractable()); | 788 EXPECT_EQ(extractable, private_key.extractable()); |
| 789 EXPECT_EQ(usage_mask, public_key.usages()); | 789 EXPECT_EQ(usage_mask, public_key.usages()); |
| 790 EXPECT_EQ(usage_mask, private_key.usages()); | 790 EXPECT_EQ(usage_mask, private_key.usages()); |
| 791 } | 791 } |
| 792 | 792 |
| 793 #endif // #if !defined(USE_OPENSSL) | 793 #endif // #if !defined(USE_OPENSSL) |
| 794 | 794 |
| 795 } // namespace content | 795 } // namespace content |
| OLD | NEW |