Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(494)

Side by Side Diff: content/renderer/webcrypto/webcrypto_impl_unittest.cc

Issue 106873003: Rework how webcrypto tests are disabled for inprogress OpenSSL implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix the macro Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "content/renderer/webcrypto/webcrypto_impl.h" 5 #include "content/renderer/webcrypto/webcrypto_impl.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/json/json_writer.h" 12 #include "base/json/json_writer.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
16 #include "content/public/renderer/content_renderer_client.h" 16 #include "content/public/renderer/content_renderer_client.h"
17 #include "content/renderer/renderer_webkitplatformsupport_impl.h" 17 #include "content/renderer/renderer_webkitplatformsupport_impl.h"
18 #include "content/renderer/webcrypto/webcrypto_util.h" 18 #include "content/renderer/webcrypto/webcrypto_util.h"
19 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" 20 #include "third_party/WebKit/public/platform/WebArrayBuffer.h"
21 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" 21 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
22 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" 22 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
23 #include "third_party/WebKit/public/platform/WebCryptoKey.h" 23 #include "third_party/WebKit/public/platform/WebCryptoKey.h"
24 24
25 // The OpenSSL implementation of WebCrypto is less complete, so don't run all of
26 // the tests: http://crbug.com/267888
27 #if defined(USE_OPENSSL)
28 #define NSSONLY(test_name) DISABLED_##test_name
29 #else
30 #define NSSONLY(test_name) test_name
31 #endif
Ryan Sleevi 2013/12/08 05:20:48 Please do not use macros like this. As we discuss
32
25 namespace content { 33 namespace content {
26 34
27 namespace { 35 namespace {
28 36
29 std::vector<uint8> HexStringToBytes(const std::string& hex) { 37 std::vector<uint8> HexStringToBytes(const std::string& hex) {
30 std::vector<uint8> bytes; 38 std::vector<uint8> bytes;
31 base::HexStringToBytes(hex, &bytes); 39 base::HexStringToBytes(hex, &bytes);
32 return bytes; 40 return bytes;
33 } 41 }
34 42
(...skipping 18 matching lines...) Expand all
53 // dictionary to a good state 61 // dictionary to a good state
54 void RestoreJwkOctDictionary(base::DictionaryValue* dict) { 62 void RestoreJwkOctDictionary(base::DictionaryValue* dict) {
55 dict->Clear(); 63 dict->Clear();
56 dict->SetString("kty", "oct"); 64 dict->SetString("kty", "oct");
57 dict->SetString("alg", "A128CBC"); 65 dict->SetString("alg", "A128CBC");
58 dict->SetString("use", "enc"); 66 dict->SetString("use", "enc");
59 dict->SetBoolean("extractable", false); 67 dict->SetBoolean("extractable", false);
60 dict->SetString("k", "GADWrMRHwQfoNaXU5fZvTg=="); 68 dict->SetString("k", "GADWrMRHwQfoNaXU5fZvTg==");
61 } 69 }
62 70
63 #if !defined(USE_OPENSSL)
64
65 // Helper for ImportJwkRsaFailures. Restores the JWK JSON 71 // Helper for ImportJwkRsaFailures. Restores the JWK JSON
66 // dictionary to a good state 72 // dictionary to a good state
67 void RestoreJwkRsaDictionary(base::DictionaryValue* dict) { 73 void RestoreJwkRsaDictionary(base::DictionaryValue* dict) {
68 dict->Clear(); 74 dict->Clear();
69 dict->SetString("kty", "RSA"); 75 dict->SetString("kty", "RSA");
70 dict->SetString("alg", "RSA1_5"); 76 dict->SetString("alg", "RSA1_5");
71 dict->SetString("use", "enc"); 77 dict->SetString("use", "enc");
72 dict->SetBoolean("extractable", false); 78 dict->SetBoolean("extractable", false);
73 dict->SetString("n", 79 dict->SetString("n",
74 "qLOyhK-OtQs4cDSoYPFGxJGfMYdjzWxVmMiuSBGh4KvEx-CwgtaTpef87Wdc9GaFEncsDLxk" 80 "qLOyhK-OtQs4cDSoYPFGxJGfMYdjzWxVmMiuSBGh4KvEx-CwgtaTpef87Wdc9GaFEncsDLxk"
75 "p0LGxjD1M8jMcvYq6DPEC_JYQumEu3i9v5fAEH1VvbZi9cTg-rmEXLUUjvc5LdOq_5OuHmtm" 81 "p0LGxjD1M8jMcvYq6DPEC_JYQumEu3i9v5fAEH1VvbZi9cTg-rmEXLUUjvc5LdOq_5OuHmtm"
76 "e7PUJHYW1PW6ENTP0ibeiNOfFvs"); 82 "e7PUJHYW1PW6ENTP0ibeiNOfFvs");
77 dict->SetString("e", "AQAB"); 83 dict->SetString("e", "AQAB");
78 } 84 }
79 85
80 blink::WebCryptoAlgorithm CreateRsaKeyGenAlgorithm(
81 blink::WebCryptoAlgorithmId algorithm_id,
82 unsigned modulus_length,
83 const std::vector<uint8>& public_exponent) {
84 DCHECK(algorithm_id == blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 ||
85 algorithm_id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 ||
86 algorithm_id == blink::WebCryptoAlgorithmIdRsaOaep);
87 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
88 algorithm_id,
89 new blink::WebCryptoRsaKeyGenParams(
90 modulus_length,
91 webcrypto::Uint8VectorStart(public_exponent),
92 public_exponent.size()));
93 }
94
95 // Determines if two ArrayBuffers have identical content. 86 // Determines if two ArrayBuffers have identical content.
96 bool ArrayBuffersEqual( 87 bool ArrayBuffersEqual(
97 const blink::WebArrayBuffer& a, 88 const blink::WebArrayBuffer& a,
98 const blink::WebArrayBuffer& b) { 89 const blink::WebArrayBuffer& b) {
99 return a.byteLength() == b.byteLength() && 90 return a.byteLength() == b.byteLength() &&
100 memcmp(a.data(), b.data(), a.byteLength()) == 0; 91 memcmp(a.data(), b.data(), a.byteLength()) == 0;
101 } 92 }
102 93
103 // Given a vector of WebArrayBuffers, determines if there are any copies. 94 // Given a vector of WebArrayBuffers, determines if there are any copies.
104 bool CopiesExist(std::vector<blink::WebArrayBuffer> bufs) { 95 bool CopiesExist(std::vector<blink::WebArrayBuffer> bufs) {
105 for (size_t i = 0; i < bufs.size(); ++i) { 96 for (size_t i = 0; i < bufs.size(); ++i) {
106 for (size_t j = i + 1; j < bufs.size(); ++j) { 97 for (size_t j = i + 1; j < bufs.size(); ++j) {
107 if (ArrayBuffersEqual(bufs[i], bufs[j])) 98 if (ArrayBuffersEqual(bufs[i], bufs[j]))
108 return true; 99 return true;
109 } 100 }
110 } 101 }
111 return false; 102 return false;
112 } 103 }
113 104
114 #endif // #if !defined(USE_OPENSSL)
115
116 } // namespace 105 } // namespace
117 106
118 class WebCryptoImplTest : public testing::Test { 107 class WebCryptoImplTest : public testing::Test {
119 protected: 108 protected:
120 blink::WebCryptoKey ImportSecretKeyFromRawHexString( 109 blink::WebCryptoKey ImportSecretKeyFromRawHexString(
121 const std::string& key_hex, 110 const std::string& key_hex,
122 const blink::WebCryptoAlgorithm& algorithm, 111 const blink::WebCryptoAlgorithm& algorithm,
123 blink::WebCryptoKeyUsageMask usage) { 112 blink::WebCryptoKeyUsageMask usage) {
124 std::vector<uint8> key_raw = HexStringToBytes(key_hex); 113 std::vector<uint8> key_raw = HexStringToBytes(key_hex);
125 114
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 algorithm, 479 algorithm,
491 key, 480 key,
492 kLongSignature, 481 kLongSignature,
493 sizeof(kLongSignature), 482 sizeof(kLongSignature),
494 message_raw, 483 message_raw,
495 &signature_match)); 484 &signature_match));
496 EXPECT_FALSE(signature_match); 485 EXPECT_FALSE(signature_match);
497 } 486 }
498 } 487 }
499 488
500 #if !defined(USE_OPENSSL)
501
502 TEST_F(WebCryptoImplTest, AesCbcFailures) { 489 TEST_F(WebCryptoImplTest, AesCbcFailures) {
503 const std::string key_hex = "2b7e151628aed2a6abf7158809cf4f3c"; 490 const std::string key_hex = "2b7e151628aed2a6abf7158809cf4f3c";
504 blink::WebCryptoKey key = ImportSecretKeyFromRawHexString( 491 blink::WebCryptoKey key = ImportSecretKeyFromRawHexString(
505 key_hex, 492 key_hex,
506 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 493 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
507 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); 494 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
508 495
509 // Verify exported raw key is identical to the imported data 496 // Verify exported raw key is identical to the imported data
510 blink::WebArrayBuffer raw_key; 497 blink::WebArrayBuffer raw_key;
511 EXPECT_TRUE(ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &raw_key)); 498 EXPECT_TRUE(ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &raw_key));
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
563 blink::WebCryptoKeyUsageEncrypt, 550 blink::WebCryptoKeyUsageEncrypt,
564 &key)); 551 &key));
565 } 552 }
566 553
567 // Fail exporting the key in SPKI and PKCS#8 formats (not allowed for secret 554 // Fail exporting the key in SPKI and PKCS#8 formats (not allowed for secret
568 // keys). 555 // keys).
569 EXPECT_FALSE(ExportKeyInternal(blink::WebCryptoKeyFormatSpki, key, &output)); 556 EXPECT_FALSE(ExportKeyInternal(blink::WebCryptoKeyFormatSpki, key, &output));
570 EXPECT_FALSE(ExportKeyInternal(blink::WebCryptoKeyFormatPkcs8, key, &output)); 557 EXPECT_FALSE(ExportKeyInternal(blink::WebCryptoKeyFormatPkcs8, key, &output));
571 } 558 }
572 559
573 TEST_F(WebCryptoImplTest, AesCbcSampleSets) { 560 TEST_F(WebCryptoImplTest, NSSONLY(AesCbcSampleSets)) {
574 struct TestCase { 561 struct TestCase {
575 const char* key; 562 const char* key;
576 const char* iv; 563 const char* iv;
577 const char* plain_text; 564 const char* plain_text;
578 const char* cipher_text; 565 const char* cipher_text;
579 }; 566 };
580 567
581 TestCase kTests[] = { 568 TestCase kTests[] = {
582 // F.2.1 (CBC-AES128.Encrypt) 569 // F.2.1 (CBC-AES128.Encrypt)
583 // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf 570 // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
699 if (cipher_text.size() > 3) { 686 if (cipher_text.size() > 3) {
700 EXPECT_FALSE(DecryptInternal(webcrypto::CreateAesCbcAlgorithm(iv), 687 EXPECT_FALSE(DecryptInternal(webcrypto::CreateAesCbcAlgorithm(iv),
701 key, 688 key,
702 &cipher_text[0], 689 &cipher_text[0],
703 cipher_text.size() - 3, 690 cipher_text.size() - 3,
704 &output)); 691 &output));
705 } 692 }
706 } 693 }
707 } 694 }
708 695
709 TEST_F(WebCryptoImplTest, GenerateKeyAes) { 696 TEST_F(WebCryptoImplTest, NSSONLY(GenerateKeyAes)) {
710 // Generate a small sample of AES keys. 697 // Generate a small sample of AES keys.
711 std::vector<blink::WebArrayBuffer> keys; 698 std::vector<blink::WebArrayBuffer> keys;
712 blink::WebArrayBuffer key_bytes; 699 blink::WebArrayBuffer key_bytes;
713 for (int i = 0; i < 16; ++i) { 700 for (int i = 0; i < 16; ++i) {
714 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 701 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
715 ASSERT_TRUE( 702 ASSERT_TRUE(
716 GenerateKeyInternal(webcrypto::CreateAesCbcKeyGenAlgorithm(128), &key)); 703 GenerateKeyInternal(webcrypto::CreateAesCbcKeyGenAlgorithm(128), &key));
717 EXPECT_TRUE(key.handle()); 704 EXPECT_TRUE(key.handle());
718 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 705 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
719 ASSERT_TRUE( 706 ASSERT_TRUE(
720 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &key_bytes)); 707 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &key_bytes));
721 keys.push_back(key_bytes); 708 keys.push_back(key_bytes);
722 } 709 }
723 // Ensure all entries in the key sample set are unique. This is a simplistic 710 // Ensure all entries in the key sample set are unique. This is a simplistic
724 // estimate of whether the generated keys appear random. 711 // estimate of whether the generated keys appear random.
725 EXPECT_FALSE(CopiesExist(keys)); 712 EXPECT_FALSE(CopiesExist(keys));
726 } 713 }
727 714
728 TEST_F(WebCryptoImplTest, GenerateKeyAesBadLength) { 715 TEST_F(WebCryptoImplTest, NSSONLY(GenerateKeyAesBadLength)) {
729 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 716 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
730 EXPECT_FALSE( 717 EXPECT_FALSE(
731 GenerateKeyInternal(webcrypto::CreateAesCbcKeyGenAlgorithm(0), &key)); 718 GenerateKeyInternal(webcrypto::CreateAesCbcKeyGenAlgorithm(0), &key));
732 EXPECT_FALSE( 719 EXPECT_FALSE(
733 GenerateKeyInternal(webcrypto::CreateAesCbcKeyGenAlgorithm(0), &key)); 720 GenerateKeyInternal(webcrypto::CreateAesCbcKeyGenAlgorithm(0), &key));
734 EXPECT_FALSE( 721 EXPECT_FALSE(
735 GenerateKeyInternal(webcrypto::CreateAesCbcKeyGenAlgorithm(129), &key)); 722 GenerateKeyInternal(webcrypto::CreateAesCbcKeyGenAlgorithm(129), &key));
736 } 723 }
737 724
738 TEST_F(WebCryptoImplTest, GenerateKeyHmac) { 725 TEST_F(WebCryptoImplTest, NSSONLY(GenerateKeyHmac)) {
739 // Generate a small sample of HMAC keys. 726 // Generate a small sample of HMAC keys.
740 std::vector<blink::WebArrayBuffer> keys; 727 std::vector<blink::WebArrayBuffer> keys;
741 for (int i = 0; i < 16; ++i) { 728 for (int i = 0; i < 16; ++i) {
742 blink::WebArrayBuffer key_bytes; 729 blink::WebArrayBuffer key_bytes;
743 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 730 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
744 blink::WebCryptoAlgorithm algorithm = webcrypto::CreateHmacKeyGenAlgorithm( 731 blink::WebCryptoAlgorithm algorithm = webcrypto::CreateHmacKeyGenAlgorithm(
745 blink::WebCryptoAlgorithmIdSha1, 128); 732 blink::WebCryptoAlgorithmIdSha1, 128);
746 ASSERT_TRUE(GenerateKeyInternal(algorithm, &key)); 733 ASSERT_TRUE(GenerateKeyInternal(algorithm, &key));
747 EXPECT_FALSE(key.isNull()); 734 EXPECT_FALSE(key.isNull());
748 EXPECT_TRUE(key.handle()); 735 EXPECT_TRUE(key.handle());
749 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 736 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
750 } 737 }
751 // Ensure all entries in the key sample set are unique. This is a simplistic 738 // Ensure all entries in the key sample set are unique. This is a simplistic
752 // estimate of whether the generated keys appear random. 739 // estimate of whether the generated keys appear random.
753 EXPECT_FALSE(CopiesExist(keys)); 740 EXPECT_FALSE(CopiesExist(keys));
754 } 741 }
755 742
756 TEST_F(WebCryptoImplTest, GenerateKeyHmacNoLength) { 743 TEST_F(WebCryptoImplTest, NSSONLY(GenerateKeyHmacNoLength)) {
757 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 744 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
758 blink::WebCryptoAlgorithm algorithm = 745 blink::WebCryptoAlgorithm algorithm =
759 webcrypto::CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0); 746 webcrypto::CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0);
760 ASSERT_TRUE(GenerateKeyInternal(algorithm, &key)); 747 ASSERT_TRUE(GenerateKeyInternal(algorithm, &key));
761 EXPECT_TRUE(key.handle()); 748 EXPECT_TRUE(key.handle());
762 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 749 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
763 } 750 }
764 751
765 TEST_F(WebCryptoImplTest, ImportSecretKeyNoAlgorithm) { 752 TEST_F(WebCryptoImplTest, NSSONLY(ImportSecretKeyNoAlgorithm)) {
766 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 753 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
767 754
768 // This fails because the algorithm is null. 755 // This fails because the algorithm is null.
769 EXPECT_FALSE(ImportKeyInternal( 756 EXPECT_FALSE(ImportKeyInternal(
770 blink::WebCryptoKeyFormatRaw, 757 blink::WebCryptoKeyFormatRaw,
771 HexStringToBytes("00000000000000000000"), 758 HexStringToBytes("00000000000000000000"),
772 blink::WebCryptoAlgorithm::createNull(), 759 blink::WebCryptoAlgorithm::createNull(),
773 true, 760 true,
774 blink::WebCryptoKeyUsageEncrypt, 761 blink::WebCryptoKeyUsageEncrypt,
775 &key)); 762 &key));
776 } 763 }
777 764
778 #endif //#if !defined(USE_OPENSSL)
779 765
780 TEST_F(WebCryptoImplTest, ImportJwkFailures) { 766 TEST_F(WebCryptoImplTest, ImportJwkFailures) {
781 767
782 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 768 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
783 blink::WebCryptoAlgorithm algorithm = 769 blink::WebCryptoAlgorithm algorithm =
784 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc); 770 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc);
785 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageEncrypt; 771 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageEncrypt;
786 772
787 // Baseline pass: each test below breaks a single item, so we start with a 773 // Baseline pass: each test below breaks a single item, so we start with a
788 // passing case to make sure each failure is caused by the isolated break. 774 // passing case to make sure each failure is caused by the isolated break.
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
878 RestoreJwkOctDictionary(&dict); 864 RestoreJwkOctDictionary(&dict);
879 865
880 // Fail on k actual length (120 bits) inconsistent with the embedded JWK alg 866 // Fail on k actual length (120 bits) inconsistent with the embedded JWK alg
881 // value (128) for an AES key. 867 // value (128) for an AES key.
882 dict.SetString("k", "AVj42h0Y5aqGtE3yluKL"); 868 dict.SetString("k", "AVj42h0Y5aqGtE3yluKL");
883 EXPECT_FALSE(ImportKeyJwk( 869 EXPECT_FALSE(ImportKeyJwk(
884 MakeJsonVector(dict), algorithm, false, usage_mask, &key)); 870 MakeJsonVector(dict), algorithm, false, usage_mask, &key));
885 RestoreJwkOctDictionary(&dict); 871 RestoreJwkOctDictionary(&dict);
886 } 872 }
887 873
888 #if !defined(USE_OPENSSL) 874 TEST_F(WebCryptoImplTest, NSSONLY(ImportJwkRsaFailures)) {
889
890 TEST_F(WebCryptoImplTest, ImportJwkRsaFailures) {
891 875
892 base::DictionaryValue dict; 876 base::DictionaryValue dict;
893 RestoreJwkRsaDictionary(&dict); 877 RestoreJwkRsaDictionary(&dict);
894 blink::WebCryptoAlgorithm algorithm = 878 blink::WebCryptoAlgorithm algorithm =
895 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); 879 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5);
896 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageEncrypt; 880 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageEncrypt;
897 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 881 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
898 882
899 // An RSA public key JWK _must_ have an "n" (modulus) and an "e" (exponent) 883 // An RSA public key JWK _must_ have an "n" (modulus) and an "e" (exponent)
900 // entry, while an RSA private key must have those plus at least a "d" 884 // entry, while an RSA private key must have those plus at least a "d"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
936 } 920 }
937 921
938 // Fail if "d" parameter is present, implying the JWK is a private key, which 922 // Fail if "d" parameter is present, implying the JWK is a private key, which
939 // is not supported. 923 // is not supported.
940 dict.SetString("d", "Qk3f0Dsyt"); 924 dict.SetString("d", "Qk3f0Dsyt");
941 EXPECT_FALSE(ImportKeyJwk( 925 EXPECT_FALSE(ImportKeyJwk(
942 MakeJsonVector(dict), algorithm, false, usage_mask, &key)); 926 MakeJsonVector(dict), algorithm, false, usage_mask, &key));
943 RestoreJwkRsaDictionary(&dict); 927 RestoreJwkRsaDictionary(&dict);
944 } 928 }
945 929
946 #endif // #if !defined(USE_OPENSSL) 930 TEST_F(WebCryptoImplTest, NSSONLY(ImportJwkInputConsistency)) {
947
948 TEST_F(WebCryptoImplTest, ImportJwkInputConsistency) {
949 // The Web Crypto spec says that if a JWK value is present, but is 931 // The Web Crypto spec says that if a JWK value is present, but is
950 // inconsistent with the input value, the operation must fail. 932 // inconsistent with the input value, the operation must fail.
951 933
952 // Consistency rules when JWK value is not present: Inputs should be used. 934 // Consistency rules when JWK value is not present: Inputs should be used.
953 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 935 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
954 bool extractable = false; 936 bool extractable = false;
955 blink::WebCryptoAlgorithm algorithm = 937 blink::WebCryptoAlgorithm algorithm =
956 webcrypto::CreateHmacAlgorithmByHashId(blink::WebCryptoAlgorithmIdSha256); 938 webcrypto::CreateHmacAlgorithmByHashId(blink::WebCryptoAlgorithmIdSha256);
957 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageVerify; 939 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageVerify;
958 base::DictionaryValue dict; 940 base::DictionaryValue dict;
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
1047 EXPECT_FALSE( 1029 EXPECT_FALSE(
1048 ImportKeyJwk(json_vec, algorithm, extractable, usage_mask, &key)); 1030 ImportKeyJwk(json_vec, algorithm, extractable, usage_mask, &key));
1049 usage_mask = blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify; 1031 usage_mask = blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify;
1050 1032
1051 // TODO(padolph): kty vs alg consistency tests: Depending on the kty value, 1033 // TODO(padolph): kty vs alg consistency tests: Depending on the kty value,
1052 // only certain alg values are permitted. For example, when kty = "RSA" alg 1034 // only certain alg values are permitted. For example, when kty = "RSA" alg
1053 // must be of the RSA family, or when kty = "oct" alg must be symmetric 1035 // must be of the RSA family, or when kty = "oct" alg must be symmetric
1054 // algorithm. 1036 // algorithm.
1055 } 1037 }
1056 1038
1057 TEST_F(WebCryptoImplTest, ImportJwkHappy) { 1039 TEST_F(WebCryptoImplTest, NSSONLY(ImportJwkHappy)) {
1058 1040
1059 // This test verifies the happy path of JWK import, including the application 1041 // This test verifies the happy path of JWK import, including the application
1060 // of the imported key material. 1042 // of the imported key material.
1061 1043
1062 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1044 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1063 bool extractable = false; 1045 bool extractable = false;
1064 blink::WebCryptoAlgorithm algorithm = 1046 blink::WebCryptoAlgorithm algorithm =
1065 webcrypto::CreateHmacAlgorithmByHashId(blink::WebCryptoAlgorithmIdSha256); 1047 webcrypto::CreateHmacAlgorithmByHashId(blink::WebCryptoAlgorithmIdSha256);
1066 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageSign; 1048 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageSign;
1067 1049
(...skipping 21 matching lines...) Expand all
1089 ASSERT_TRUE(SignInternal(algorithm, key, message_raw, &output)); 1071 ASSERT_TRUE(SignInternal(algorithm, key, message_raw, &output));
1090 1072
1091 const std::string mac_raw = 1073 const std::string mac_raw =
1092 "769f00d3e6a6cc1fb426a14a4f76c6462e6149726e0dee0ec0cf97a16605ac8b"; 1074 "769f00d3e6a6cc1fb426a14a4f76c6462e6149726e0dee0ec0cf97a16605ac8b";
1093 1075
1094 ExpectArrayBufferMatchesHex(mac_raw, output); 1076 ExpectArrayBufferMatchesHex(mac_raw, output);
1095 1077
1096 // TODO(padolph): Import an RSA public key JWK and use it 1078 // TODO(padolph): Import an RSA public key JWK and use it
1097 } 1079 }
1098 1080
1099 #if !defined(USE_OPENSSL) 1081 TEST_F(WebCryptoImplTest, NSSONLY(ImportExportSpki)) {
1100
1101 TEST_F(WebCryptoImplTest, ImportExportSpki) {
1102 // openssl genrsa -out pair.pem 2048 1082 // openssl genrsa -out pair.pem 2048
1103 // openssl rsa -in pair.pem -out pubkey.der -outform DER -pubout 1083 // openssl rsa -in pair.pem -out pubkey.der -outform DER -pubout
1104 // xxd -p pubkey.der 1084 // xxd -p pubkey.der
1105 const std::string hex_rsa_spki_der = 1085 const std::string hex_rsa_spki_der =
1106 "30820122300d06092a864886f70d01010105000382010f003082010a0282" 1086 "30820122300d06092a864886f70d01010105000382010f003082010a0282"
1107 "010100f19e40f94e3780858701577a571cca000cb9795db89ddf8e98ab0e" 1087 "010100f19e40f94e3780858701577a571cca000cb9795db89ddf8e98ab0e"
1108 "5eecfa47516cb08dc591cae5ab7fa43d6db402e95991d4a2de52e7cd3a66" 1088 "5eecfa47516cb08dc591cae5ab7fa43d6db402e95991d4a2de52e7cd3a66"
1109 "4f58284be2eb4675d5a849a2582c585d2b3c6c225a8f2c53a0414d5dbd06" 1089 "4f58284be2eb4675d5a849a2582c585d2b3c6c225a8f2c53a0414d5dbd06"
1110 "172371cefdf953e9ec3000fc9ad000743023f74e82d12aa93917a2c9b832" 1090 "172371cefdf953e9ec3000fc9ad000743023f74e82d12aa93917a2c9b832"
1111 "696085ee0711154cf98a6d098f44cee00ea3b7584236503a5483ba8b6792" 1091 "696085ee0711154cf98a6d098f44cee00ea3b7584236503a5483ba8b6792"
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
1182 HexStringToBytes(hex_rsa_spki_der), 1162 HexStringToBytes(hex_rsa_spki_der),
1183 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5), 1163 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5),
1184 false, 1164 false,
1185 blink::WebCryptoKeyUsageEncrypt, 1165 blink::WebCryptoKeyUsageEncrypt,
1186 &key)); 1166 &key));
1187 EXPECT_TRUE(key.handle()); 1167 EXPECT_TRUE(key.handle());
1188 EXPECT_FALSE(key.extractable()); 1168 EXPECT_FALSE(key.extractable());
1189 EXPECT_FALSE(ExportKeyInternal(blink::WebCryptoKeyFormatSpki, key, &output)); 1169 EXPECT_FALSE(ExportKeyInternal(blink::WebCryptoKeyFormatSpki, key, &output));
1190 } 1170 }
1191 1171
1192 TEST_F(WebCryptoImplTest, ImportPkcs8) { 1172 TEST_F(WebCryptoImplTest, NSSONLY(ImportPkcs8)) {
1193 1173
1194 // The following is a DER-encoded PKCS#8 representation of the RSA key from 1174 // The following is a DER-encoded PKCS#8 representation of the RSA key from
1195 // Example 1 of NIST's "Test vectors for RSA PKCS#1 v1.5 Signature". 1175 // Example 1 of NIST's "Test vectors for RSA PKCS#1 v1.5 Signature".
1196 // ftp://ftp.rsa.com/pub/rsalabs/tmp/pkcs1v15sign-vectors.txt 1176 // ftp://ftp.rsa.com/pub/rsalabs/tmp/pkcs1v15sign-vectors.txt
1197 const std::string hex_rsa_pkcs8_der = 1177 const std::string hex_rsa_pkcs8_der =
1198 "30820275020100300D06092A864886F70D01010105000482025F3082025B020100028181" 1178 "30820275020100300D06092A864886F70D01010105000482025F3082025B020100028181"
1199 "00A56E4A0E701017589A5187DC7EA841D156F2EC0E36AD52A44DFEB1E61F7AD991D8C510" 1179 "00A56E4A0E701017589A5187DC7EA841D156F2EC0E36AD52A44DFEB1E61F7AD991D8C510"
1200 "56FFEDB162B4C0F283A12A88A394DFF526AB7291CBB307CEABFCE0B1DFD5CD9508096D5B" 1180 "56FFEDB162B4C0F283A12A88A394DFF526AB7291CBB307CEABFCE0B1DFD5CD9508096D5B"
1201 "2B8B6DF5D671EF6377C0921CB23C270A70E2598E6FF89D19F105ACC2D3F0CB35F29280E1" 1181 "2B8B6DF5D671EF6377C0921CB23C270A70E2598E6FF89D19F105ACC2D3F0CB35F29280E1"
1202 "386B6F64C4EF22E1E1F20D0CE8CFFB2249BD9A2137020301000102818033A5042A90B27D" 1182 "386B6F64C4EF22E1E1F20D0CE8CFFB2249BD9A2137020301000102818033A5042A90B27D"
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
1260 // Failing case: Import RSA key but provide an inconsistent input algorithm. 1240 // Failing case: Import RSA key but provide an inconsistent input algorithm.
1261 EXPECT_FALSE(ImportKeyInternal( 1241 EXPECT_FALSE(ImportKeyInternal(
1262 blink::WebCryptoKeyFormatPkcs8, 1242 blink::WebCryptoKeyFormatPkcs8,
1263 HexStringToBytes(hex_rsa_pkcs8_der), 1243 HexStringToBytes(hex_rsa_pkcs8_der),
1264 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 1244 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
1265 true, 1245 true,
1266 blink::WebCryptoKeyUsageSign, 1246 blink::WebCryptoKeyUsageSign,
1267 &key)); 1247 &key));
1268 } 1248 }
1269 1249
1270 TEST_F(WebCryptoImplTest, GenerateKeyPairRsa) { 1250 TEST_F(WebCryptoImplTest, NSSONLY(GenerateKeyPairRsa)) {
1271 // Note: using unrealistic short key lengths here to avoid bogging down tests. 1251 // Note: using unrealistic short key lengths here to avoid bogging down tests.
1272 1252
1273 // Successful WebCryptoAlgorithmIdRsaEsPkcs1v1_5 key generation. 1253 // Successful WebCryptoAlgorithmIdRsaEsPkcs1v1_5 key generation.
1274 const unsigned modulus_length = 256; 1254 const unsigned modulus_length = 256;
1275 const std::vector<uint8> public_exponent = HexStringToBytes("010001"); 1255 const std::vector<uint8> public_exponent = HexStringToBytes("010001");
1276 blink::WebCryptoAlgorithm algorithm = webcrypto::CreateRsaKeyGenAlgorithm( 1256 blink::WebCryptoAlgorithm algorithm = webcrypto::CreateRsaKeyGenAlgorithm(
1277 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 1257 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
1278 modulus_length, 1258 modulus_length,
1279 public_exponent); 1259 public_exponent);
1280 bool extractable = false; 1260 bool extractable = false;
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
1374 EXPECT_EQ(usage_mask, private_key.usages()); 1354 EXPECT_EQ(usage_mask, private_key.usages());
1375 1355
1376 // Fail SPKI export of private key. This is an ExportKey test, but do it here 1356 // Fail SPKI export of private key. This is an ExportKey test, but do it here
1377 // since it is expensive to generate an RSA key pair and we already have a 1357 // since it is expensive to generate an RSA key pair and we already have a
1378 // private key here. 1358 // private key here.
1379 blink::WebArrayBuffer output; 1359 blink::WebArrayBuffer output;
1380 EXPECT_FALSE( 1360 EXPECT_FALSE(
1381 ExportKeyInternal(blink::WebCryptoKeyFormatSpki, private_key, &output)); 1361 ExportKeyInternal(blink::WebCryptoKeyFormatSpki, private_key, &output));
1382 } 1362 }
1383 1363
1384 TEST_F(WebCryptoImplTest, RsaEsRoundTrip) { 1364 TEST_F(WebCryptoImplTest, NSSONLY(RsaEsRoundTrip)) {
1385 // Note: using unrealistic short key length here to avoid bogging down tests. 1365 // Note: using unrealistic short key length here to avoid bogging down tests.
1386 1366
1387 // Create a key pair. 1367 // Create a key pair.
1388 const unsigned kModulusLength = 256; 1368 const unsigned kModulusLength = 256;
1389 blink::WebCryptoAlgorithm algorithm = 1369 blink::WebCryptoAlgorithm algorithm =
1390 CreateRsaKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 1370 webcrypto::CreateRsaKeyGenAlgorithm(
1391 kModulusLength, 1371 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
1392 HexStringToBytes("010001")); 1372 kModulusLength,
1373 HexStringToBytes("010001"));
1393 const blink::WebCryptoKeyUsageMask usage_mask = 1374 const blink::WebCryptoKeyUsageMask usage_mask =
1394 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt; 1375 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt;
1395 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 1376 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
1396 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 1377 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
1397 EXPECT_TRUE(GenerateKeyPairInternal( 1378 EXPECT_TRUE(GenerateKeyPairInternal(
1398 algorithm, false, usage_mask, &public_key, &private_key)); 1379 algorithm, false, usage_mask, &public_key, &private_key));
1399 EXPECT_FALSE(public_key.isNull()); 1380 EXPECT_FALSE(public_key.isNull());
1400 EXPECT_FALSE(private_key.isNull()); 1381 EXPECT_FALSE(private_key.isNull());
1401 1382
1402 // Make a maximum-length data message. RSAES can operate on messages up to 1383 // Make a maximum-length data message. RSAES can operate on messages up to
(...skipping 27 matching lines...) Expand all
1430 ASSERT_TRUE(DecryptInternal( 1411 ASSERT_TRUE(DecryptInternal(
1431 algorithm, 1412 algorithm,
1432 private_key, 1413 private_key,
1433 reinterpret_cast<const unsigned char*>(encrypted_data.data()), 1414 reinterpret_cast<const unsigned char*>(encrypted_data.data()),
1434 encrypted_data.byteLength(), 1415 encrypted_data.byteLength(),
1435 &decrypted_data)); 1416 &decrypted_data));
1436 ExpectArrayBufferMatchesHex(kTestDataHex[i], decrypted_data); 1417 ExpectArrayBufferMatchesHex(kTestDataHex[i], decrypted_data);
1437 } 1418 }
1438 } 1419 }
1439 1420
1440 TEST_F(WebCryptoImplTest, RsaEsKnownAnswer) { 1421 TEST_F(WebCryptoImplTest, NSSONLY(RsaEsKnownAnswer)) {
1441 // Because the random data in PKCS1.5 padding makes the encryption output non- 1422 // Because the random data in PKCS1.5 padding makes the encryption output non-
1442 // deterministic, we cannot easily do a typical known-answer test for RSA 1423 // deterministic, we cannot easily do a typical known-answer test for RSA
1443 // encryption / decryption. Instead we will take a known-good encrypted 1424 // encryption / decryption. Instead we will take a known-good encrypted
1444 // message, decrypt it, re-encrypt it, then decrypt again, verifying that the 1425 // message, decrypt it, re-encrypt it, then decrypt again, verifying that the
1445 // original known cleartext is the result. 1426 // original known cleartext is the result.
1446 1427
1447 // The RSA public and private keys used for this test are produced by the 1428 // The RSA public and private keys used for this test are produced by the
1448 // openssl command line: 1429 // openssl command line:
1449 // % openssl genrsa -out pair.pem 1024 1430 // % openssl genrsa -out pair.pem 1024
1450 // % openssl rsa -in pair.pem -out spki.der -outform DER -pubout 1431 // % openssl rsa -in pair.pem -out spki.der -outform DER -pubout
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
1556 ASSERT_TRUE(DecryptInternal( 1537 ASSERT_TRUE(DecryptInternal(
1557 algorithm, 1538 algorithm,
1558 private_key, 1539 private_key,
1559 reinterpret_cast<const unsigned char*>(encrypted_data.data()), 1540 reinterpret_cast<const unsigned char*>(encrypted_data.data()),
1560 encrypted_data.byteLength(), 1541 encrypted_data.byteLength(),
1561 &decrypted_data)); 1542 &decrypted_data));
1562 EXPECT_FALSE(decrypted_data.isNull()); 1543 EXPECT_FALSE(decrypted_data.isNull());
1563 ExpectArrayBufferMatchesHex(cleartext_hex, decrypted_data); 1544 ExpectArrayBufferMatchesHex(cleartext_hex, decrypted_data);
1564 } 1545 }
1565 1546
1566 TEST_F(WebCryptoImplTest, RsaEsFailures) { 1547 TEST_F(WebCryptoImplTest, NSSONLY(RsaEsFailures)) {
1567 // Note: using unrealistic short key length here to avoid bogging down tests. 1548 // Note: using unrealistic short key length here to avoid bogging down tests.
1568 1549
1569 // Create a key pair. 1550 // Create a key pair.
1570 const unsigned kModulusLength = 256; 1551 const unsigned kModulusLength = 256;
1571 blink::WebCryptoAlgorithm algorithm = 1552 blink::WebCryptoAlgorithm algorithm =
1572 CreateRsaKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 1553 webcrypto::CreateRsaKeyGenAlgorithm(
1573 kModulusLength, 1554 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
1574 HexStringToBytes("010001")); 1555 kModulusLength,
1556 HexStringToBytes("010001"));
1575 const blink::WebCryptoKeyUsageMask usage_mask = 1557 const blink::WebCryptoKeyUsageMask usage_mask =
1576 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt; 1558 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt;
1577 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 1559 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
1578 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 1560 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
1579 EXPECT_TRUE(GenerateKeyPairInternal( 1561 EXPECT_TRUE(GenerateKeyPairInternal(
1580 algorithm, false, usage_mask, &public_key, &private_key)); 1562 algorithm, false, usage_mask, &public_key, &private_key));
1581 EXPECT_FALSE(public_key.isNull()); 1563 EXPECT_FALSE(public_key.isNull());
1582 EXPECT_FALSE(private_key.isNull()); 1564 EXPECT_FALSE(private_key.isNull());
1583 1565
1584 // Fail encrypt with a private key. 1566 // Fail encrypt with a private key.
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1629 // Do a successful decrypt with good data just for confirmation. 1611 // Do a successful decrypt with good data just for confirmation.
1630 EXPECT_TRUE(DecryptInternal( 1612 EXPECT_TRUE(DecryptInternal(
1631 algorithm, 1613 algorithm,
1632 private_key, 1614 private_key,
1633 reinterpret_cast<const unsigned char*>(encrypted_data.data()), 1615 reinterpret_cast<const unsigned char*>(encrypted_data.data()),
1634 encrypted_data.byteLength(), 1616 encrypted_data.byteLength(),
1635 &decrypted_data)); 1617 &decrypted_data));
1636 ExpectArrayBufferMatchesHex(message_hex_str, decrypted_data); 1618 ExpectArrayBufferMatchesHex(message_hex_str, decrypted_data);
1637 } 1619 }
1638 1620
1639 #endif // #if !defined(USE_OPENSSL)
1640
1641 } // namespace content 1621 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698