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

Side by Side Diff: content/child/webcrypto/shared_crypto_unittest.cc

Issue 275943004: Add support for RSA-OAEP when using NSS 3.16.2 or later (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Unit tests Created 6 years, 7 months 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
OLDNEW
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/child/webcrypto/shared_crypto.h" 5 #include "content/child/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
(...skipping 11 matching lines...) Expand all
22 #include "content/child/webcrypto/status.h" 22 #include "content/child/webcrypto/status.h"
23 #include "content/child/webcrypto/webcrypto_util.h" 23 #include "content/child/webcrypto/webcrypto_util.h"
24 #include "content/public/common/content_paths.h" 24 #include "content/public/common/content_paths.h"
25 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" 26 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
27 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" 27 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
28 #include "third_party/WebKit/public/platform/WebCryptoKey.h" 28 #include "third_party/WebKit/public/platform/WebCryptoKey.h"
29 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" 29 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
30 #include "third_party/re2/re2/re2.h" 30 #include "third_party/re2/re2/re2.h"
31 31
32 #if defined(USE_NSS)
33 #include <nss.h>
34 #include <pk11pub.h>
35
36 #include "crypto/scoped_nss_types.h"
37 #endif
38
32 // The OpenSSL implementation of WebCrypto is less complete, so don't run all of 39 // The OpenSSL implementation of WebCrypto is less complete, so don't run all of
33 // the tests: http://crbug.com/267888 40 // the tests: http://crbug.com/267888
34 #if defined(USE_OPENSSL) 41 #if defined(USE_OPENSSL)
35 #define MAYBE(test_name) DISABLED_##test_name 42 #define MAYBE(test_name) DISABLED_##test_name
36 #else 43 #else
37 #define MAYBE(test_name) test_name 44 #define MAYBE(test_name) test_name
38 #endif 45 #endif
39 46
40 #define EXPECT_BYTES_EQ(expected, actual) \ 47 #define EXPECT_BYTES_EQ(expected, actual) \
41 EXPECT_EQ(CryptoData(expected), CryptoData(actual)) 48 EXPECT_EQ(CryptoData(expected), CryptoData(actual))
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm), 106 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm),
100 true, 107 true,
101 blink::WebCryptoKeyUsageEncrypt, 108 blink::WebCryptoKeyUsageEncrypt,
102 &key); 109 &key);
103 110
104 if (status.IsError()) 111 if (status.IsError())
105 EXPECT_EQ(Status::ErrorUnsupported(), status); 112 EXPECT_EQ(Status::ErrorUnsupported(), status);
106 return status.IsSuccess(); 113 return status.IsSuccess();
107 } 114 }
108 115
116 bool SupportsRsaOaep() {
117 #if defined(USE_OPENSSL)
118 return false;
119 #else
120 if (!NSS_VersionCheck("3.16.2"))
eroman 2014/05/16 20:29:51 I prefer to not have this sort of knowledge in uni
Ryan Sleevi 2014/05/16 22:43:05 I strongly disagree with this. 1) Performance - t
121 return false;
122 crypto::ScopedPK11Slot slot(PK11_GetInternalKeySlot());
123 return !!PK11_DoesMechanism(slot.get(), CKM_RSA_PKCS_OAEP);
eroman 2014/05/16 20:29:51 Same question as earlier, why the !! rather than i
Ryan Sleevi 2014/05/16 22:43:05 Because PR_Boolean is an int, and bool is a narrow
124 #endif
125 }
126
109 blink::WebCryptoAlgorithm CreateRsaKeyGenAlgorithm( 127 blink::WebCryptoAlgorithm CreateRsaKeyGenAlgorithm(
110 blink::WebCryptoAlgorithmId algorithm_id, 128 blink::WebCryptoAlgorithmId algorithm_id,
111 unsigned int modulus_length, 129 unsigned int modulus_length,
112 const std::vector<uint8>& public_exponent) { 130 const std::vector<uint8>& public_exponent) {
113 DCHECK_EQ(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, algorithm_id); 131 DCHECK_EQ(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, algorithm_id);
114 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( 132 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
115 algorithm_id, 133 algorithm_id,
116 new blink::WebCryptoRsaKeyGenParams( 134 new blink::WebCryptoRsaKeyGenParams(
117 modulus_length, 135 modulus_length,
118 webcrypto::Uint8VectorStart(public_exponent), 136 webcrypto::Uint8VectorStart(public_exponent),
(...skipping 10 matching lines...) Expand all
129 DCHECK(blink::WebCryptoAlgorithm::isHash(hash_id)); 147 DCHECK(blink::WebCryptoAlgorithm::isHash(hash_id));
130 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( 148 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
131 algorithm_id, 149 algorithm_id,
132 new blink::WebCryptoRsaHashedKeyGenParams( 150 new blink::WebCryptoRsaHashedKeyGenParams(
133 CreateAlgorithm(hash_id), 151 CreateAlgorithm(hash_id),
134 modulus_length, 152 modulus_length,
135 webcrypto::Uint8VectorStart(public_exponent), 153 webcrypto::Uint8VectorStart(public_exponent),
136 public_exponent.size())); 154 public_exponent.size()));
137 } 155 }
138 156
157 // Creates an RSA-OAEP algorithm
eroman 2014/05/16 20:29:51 Might want to mention "for encryption/decryption/w
158 blink::WebCryptoAlgorithm CreateRsaOaepAlgorithm(
159 const std::vector<uint8>& label) {
160 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
161 blink::WebCryptoAlgorithmIdRsaOaep,
162 new blink::WebCryptoRsaOaepParams(
163 !label.empty(), Uint8VectorStart(label), label.size()));
164 }
165
139 // Creates an AES-CBC algorithm. 166 // Creates an AES-CBC algorithm.
140 blink::WebCryptoAlgorithm CreateAesCbcAlgorithm(const std::vector<uint8>& iv) { 167 blink::WebCryptoAlgorithm CreateAesCbcAlgorithm(const std::vector<uint8>& iv) {
141 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( 168 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
142 blink::WebCryptoAlgorithmIdAesCbc, 169 blink::WebCryptoAlgorithmIdAesCbc,
143 new blink::WebCryptoAesCbcParams(Uint8VectorStart(iv), iv.size())); 170 new blink::WebCryptoAesCbcParams(Uint8VectorStart(iv), iv.size()));
144 } 171 }
145 172
146 // Creates and AES-GCM algorithm. 173 // Creates and AES-GCM algorithm.
147 blink::WebCryptoAlgorithm CreateAesGcmAlgorithm( 174 blink::WebCryptoAlgorithm CreateAesGcmAlgorithm(
148 const std::vector<uint8>& iv, 175 const std::vector<uint8>& iv,
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 "3b6dcd3eda8e6443024100b69dca1cf7d4d7ec81e75b90fcca874abcde12" 423 "3b6dcd3eda8e6443024100b69dca1cf7d4d7ec81e75b90fcca874abcde12"
397 "3fd2700180aa90479b6e48de8d67ed24f9f19d85ba275874f542cd20dc72" 424 "3fd2700180aa90479b6e48de8d67ed24f9f19d85ba275874f542cd20dc72"
398 "3e6963364a1f9425452b269a6799fd024028fa13938655be1f8a159cbaca" 425 "3e6963364a1f9425452b269a6799fd024028fa13938655be1f8a159cbaca"
399 "5a72ea190c30089e19cd274a556f36c4f6e19f554b34c077790427bbdd8d" 426 "5a72ea190c30089e19cd274a556f36c4f6e19f554b34c077790427bbdd8d"
400 "d3ede2448328f385d81b30e8e43b2fffa02786197902401a8b38f398fa71" 427 "d3ede2448328f385d81b30e8e43b2fffa02786197902401a8b38f398fa71"
401 "2049898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd" 428 "2049898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd"
402 "48be455eaeb6e1678255827580a8e4e8e14151d1510a82a3f2e729024027" 429 "48be455eaeb6e1678255827580a8e4e8e14151d1510a82a3f2e729024027"
403 "156aba4126d24a81f3a528cbfb27f56886f840a9f6e86e17a44b94fe9319" 430 "156aba4126d24a81f3a528cbfb27f56886f840a9f6e86e17a44b94fe9319"
404 "584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb2190acf832b847f13a3d24" 431 "584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb2190acf832b847f13a3d24"
405 "a79f4d"; 432 "a79f4d";
433 // The modulus and exponent (in hex) of kPublicKeySpkiDerHex
434 const char* const kPublicKeyModulusHex =
435 "A56E4A0E701017589A5187DC7EA841D156F2EC0E36AD52A44DFEB1E61F7AD991D8C51056"
eroman 2014/05/16 20:29:51 [optional] OCD nit: The hex string above uses lowe
Ryan Sleevi 2014/05/16 22:43:05 This was your string - from 1445.
436 "FFEDB162B4C0F283A12A88A394DFF526AB7291CBB307CEABFCE0B1DFD5CD9508096D5B2B"
437 "8B6DF5D671EF6377C0921CB23C270A70E2598E6FF89D19F105ACC2D3F0CB35F29280E138"
438 "6B6F64C4EF22E1E1F20D0CE8CFFB2249BD9A2137";
439 const char* const kPublicKeyExponentHex = "010001";
eroman 2014/05/16 20:29:51 Consider adding a TODO to replace the existing usa
Ryan Sleevi 2014/05/16 22:43:05 I already fixed the existing usages. I'm not sure
eroman 2014/05/19 19:04:33 There are other occurrences of "010001" in the fil
406 440
407 class SharedCryptoTest : public testing::Test { 441 class SharedCryptoTest : public testing::Test {
408 protected: 442 protected:
409 virtual void SetUp() OVERRIDE { Init(); } 443 virtual void SetUp() OVERRIDE { Init(); }
410 }; 444 };
411 445
412 blink::WebCryptoKey ImportSecretKeyFromRaw( 446 blink::WebCryptoKey ImportSecretKeyFromRaw(
413 const std::vector<uint8>& key_raw, 447 const std::vector<uint8>& key_raw,
414 const blink::WebCryptoAlgorithm& algorithm, 448 const blink::WebCryptoAlgorithm& algorithm,
415 blink::WebCryptoKeyUsageMask usage) { 449 blink::WebCryptoKeyUsageMask usage) {
(...skipping 16 matching lines...) Expand all
432 return key; 466 return key;
433 } 467 }
434 468
435 void ImportRsaKeyPair(const std::vector<uint8>& spki_der, 469 void ImportRsaKeyPair(const std::vector<uint8>& spki_der,
436 const std::vector<uint8>& pkcs8_der, 470 const std::vector<uint8>& pkcs8_der,
437 const blink::WebCryptoAlgorithm& algorithm, 471 const blink::WebCryptoAlgorithm& algorithm,
438 bool extractable, 472 bool extractable,
439 blink::WebCryptoKeyUsageMask usage_mask, 473 blink::WebCryptoKeyUsageMask usage_mask,
440 blink::WebCryptoKey* public_key, 474 blink::WebCryptoKey* public_key,
441 blink::WebCryptoKey* private_key) { 475 blink::WebCryptoKey* private_key) {
442 EXPECT_EQ(Status::Success(), 476 ASSERT_EQ(Status::Success(),
Ryan Sleevi 2014/05/16 05:17:22 Ran into CHECK failures here when testing and I di
443 ImportKey(blink::WebCryptoKeyFormatSpki, 477 ImportKey(blink::WebCryptoKeyFormatSpki,
444 CryptoData(spki_der), 478 CryptoData(spki_der),
445 algorithm, 479 algorithm,
446 true, 480 true,
447 usage_mask, 481 usage_mask,
448 public_key)); 482 public_key));
449 EXPECT_FALSE(public_key->isNull()); 483 EXPECT_FALSE(public_key->isNull());
450 EXPECT_TRUE(public_key->handle()); 484 EXPECT_TRUE(public_key->handle());
451 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key->type()); 485 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key->type());
452 EXPECT_EQ(algorithm.id(), public_key->algorithm().id()); 486 EXPECT_EQ(algorithm.id(), public_key->algorithm().id());
453 EXPECT_EQ(extractable, extractable); 487 EXPECT_EQ(extractable, extractable);
454 EXPECT_EQ(usage_mask, public_key->usages()); 488 EXPECT_EQ(usage_mask, public_key->usages());
455 489
456 EXPECT_EQ(Status::Success(), 490 ASSERT_EQ(Status::Success(),
457 ImportKey(blink::WebCryptoKeyFormatPkcs8, 491 ImportKey(blink::WebCryptoKeyFormatPkcs8,
458 CryptoData(pkcs8_der), 492 CryptoData(pkcs8_der),
459 algorithm, 493 algorithm,
460 extractable, 494 extractable,
461 usage_mask, 495 usage_mask,
462 private_key)); 496 private_key));
463 EXPECT_FALSE(private_key->isNull()); 497 EXPECT_FALSE(private_key->isNull());
464 EXPECT_TRUE(private_key->handle()); 498 EXPECT_TRUE(private_key->handle());
465 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key->type()); 499 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key->type());
466 EXPECT_EQ(algorithm.id(), private_key->algorithm().id()); 500 EXPECT_EQ(algorithm.id(), private_key->algorithm().id());
(...skipping 965 matching lines...) Expand 10 before | Expand all | Expand 10 after
1432 1466
1433 // Fail on k actual length (192 bits) inconsistent with the embedded JWK alg 1467 // Fail on k actual length (192 bits) inconsistent with the embedded JWK alg
1434 // value (128) for an AES key. 1468 // value (128) for an AES key.
1435 dict.SetString("k", "dGhpcyAgaXMgIDI0ICBieXRlcyBsb25n"); 1469 dict.SetString("k", "dGhpcyAgaXMgIDI0ICBieXRlcyBsb25n");
1436 EXPECT_EQ(Status::ErrorJwkIncorrectKeyLength(), 1470 EXPECT_EQ(Status::ErrorJwkIncorrectKeyLength(),
1437 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); 1471 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key));
1438 RestoreJwkOctDictionary(&dict); 1472 RestoreJwkOctDictionary(&dict);
1439 } 1473 }
1440 1474
1441 TEST_F(SharedCryptoTest, MAYBE(ImportExportJwkRsaPublicKey)) { 1475 TEST_F(SharedCryptoTest, MAYBE(ImportExportJwkRsaPublicKey)) {
1442 // This test uses kPublicKeySpkiDerHex as the RSA key. The data below 1476 const bool supports_rsa_oaep = SupportsRsaOaep();
1443 // represents the modulus and public exponent extracted from this SPKI blob. 1477 if (!supports_rsa_oaep) {
1444 // These values appear explicitly in the JWK rendering of the key. 1478 LOG(WARNING) << "RSA-OAEP not supported on this platform. Skipping some"
eroman 2014/05/16 20:29:51 sometests --> some tests
1445 const std::string n_hex = 1479 << "tests.";
1446 "A56E4A0E701017589A5187DC7EA841D156F2EC0E36AD52A44DFEB1E61F7AD991D8C51056" 1480 }
1447 "FFEDB162B4C0F283A12A88A394DFF526AB7291CBB307CEABFCE0B1DFD5CD9508096D5B2B"
1448 "8B6DF5D671EF6377C0921CB23C270A70E2598E6FF89D19F105ACC2D3F0CB35F29280E138"
1449 "6B6F64C4EF22E1E1F20D0CE8CFFB2249BD9A2137";
1450 const std::string e_hex = "010001";
1451 1481
1452 struct TestCase { 1482 struct TestCase {
1453 const blink::WebCryptoAlgorithm algorithm; 1483 const blink::WebCryptoAlgorithm algorithm;
1454 const blink::WebCryptoKeyUsageMask usage; 1484 const blink::WebCryptoKeyUsageMask usage;
1455 const char* const jwk_alg; 1485 const char* const jwk_alg;
1456 }; 1486 };
1457 const TestCase kTests[] = { 1487 const TestCase kTests[] = {
1458 // RSAES-PKCS1-v1_5 1488 // RSAES-PKCS1-v1_5
1459 {CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5), 1489 {CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5),
1460 blink::WebCryptoKeyUsageEncrypt, "RSA1_5"}, 1490 blink::WebCryptoKeyUsageEncrypt, "RSA1_5"},
1461 // RSASSA-PKCS1-v1_5 SHA-1 1491 // RSASSA-PKCS1-v1_5 SHA-1
1462 {CreateRsaHashedImportAlgorithm( 1492 {CreateRsaHashedImportAlgorithm(
1463 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 1493 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
1464 blink::WebCryptoAlgorithmIdSha1), 1494 blink::WebCryptoAlgorithmIdSha1),
1465 blink::WebCryptoKeyUsageSign, "RS1"}, 1495 blink::WebCryptoKeyUsageSign, "RS1"},
1466 // RSASSA-PKCS1-v1_5 SHA-256 1496 // RSASSA-PKCS1-v1_5 SHA-256
1467 {CreateRsaHashedImportAlgorithm( 1497 {CreateRsaHashedImportAlgorithm(
1468 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 1498 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
1469 blink::WebCryptoAlgorithmIdSha256), 1499 blink::WebCryptoAlgorithmIdSha256),
1470 blink::WebCryptoKeyUsageSign, "RS256"}, 1500 blink::WebCryptoKeyUsageSign, "RS256"},
1471 // RSASSA-PKCS1-v1_5 SHA-384 1501 // RSASSA-PKCS1-v1_5 SHA-384
1472 {CreateRsaHashedImportAlgorithm( 1502 {CreateRsaHashedImportAlgorithm(
1473 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 1503 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
1474 blink::WebCryptoAlgorithmIdSha384), 1504 blink::WebCryptoAlgorithmIdSha384),
1475 blink::WebCryptoKeyUsageSign, "RS384"}, 1505 blink::WebCryptoKeyUsageSign, "RS384"},
1476 // RSASSA-PKCS1-v1_5 SHA-512 1506 // RSASSA-PKCS1-v1_5 SHA-512
1477 {CreateRsaHashedImportAlgorithm( 1507 {CreateRsaHashedImportAlgorithm(
1478 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 1508 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
1479 blink::WebCryptoAlgorithmIdSha512), 1509 blink::WebCryptoAlgorithmIdSha512),
1480 blink::WebCryptoKeyUsageSign, "RS512"}}; 1510 blink::WebCryptoKeyUsageSign, "RS512"},
1511 // RSA-OAEP with SHA-1 and MGF-1 / SHA-1
1512 {CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaOaep,
1513 blink::WebCryptoAlgorithmIdSha1),
1514 blink::WebCryptoKeyUsageEncrypt, "RSA-OAEP"},
1515 // RSA-OAEP with SHA-256 and MGF-1 / SHA-256
1516 {CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaOaep,
1517 blink::WebCryptoAlgorithmIdSha256),
1518 blink::WebCryptoKeyUsageEncrypt, "RSA-OAEP-256"},
1519 // RSA-OAEP with SHA-384 and MGF-1 / SHA-384
1520 {CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaOaep,
1521 blink::WebCryptoAlgorithmIdSha384),
1522 blink::WebCryptoKeyUsageEncrypt, "RSA-OAEP-384"},
1523 // RSA-OAEP with SHA-512 and MGF-1 / SHA-512
1524 {CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaOaep,
1525 blink::WebCryptoAlgorithmIdSha512),
1526 blink::WebCryptoKeyUsageEncrypt, "RSA-OAEP-512"}};
1481 1527
1482 for (size_t test_index = 0; test_index < ARRAYSIZE_UNSAFE(kTests); 1528 for (size_t test_index = 0; test_index < ARRAYSIZE_UNSAFE(kTests);
1483 ++test_index) { 1529 ++test_index) {
1484 SCOPED_TRACE(test_index); 1530 SCOPED_TRACE(test_index);
1485 const TestCase& test = kTests[test_index]; 1531 const TestCase& test = kTests[test_index];
1532 if (!supports_rsa_oaep &&
1533 test.algorithm.id() == blink::WebCryptoAlgorithmIdRsaOaep) {
1534 continue;
eroman 2014/05/16 20:29:51 Consider adding the skip warning here. The outter
Ryan Sleevi 2014/05/16 22:43:05 I generally oppose logging skipped tests on princi
1535 }
1486 1536
1487 // Import the spki to create a public key 1537 // Import the spki to create a public key
1488 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 1538 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
1489 ASSERT_EQ(Status::Success(), 1539 ASSERT_EQ(Status::Success(),
1490 ImportKey(blink::WebCryptoKeyFormatSpki, 1540 ImportKey(blink::WebCryptoKeyFormatSpki,
1491 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)), 1541 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)),
1492 test.algorithm, 1542 test.algorithm,
1493 true, 1543 true,
1494 test.usage, 1544 test.usage,
1495 &public_key)); 1545 &public_key));
1496 1546
1497 // Export the public key as JWK and verify its contents 1547 // Export the public key as JWK and verify its contents
1498 std::vector<uint8> jwk; 1548 std::vector<uint8> jwk;
1499 ASSERT_EQ(Status::Success(), 1549 ASSERT_EQ(Status::Success(),
1500 ExportKey(blink::WebCryptoKeyFormatJwk, public_key, &jwk)); 1550 ExportKey(blink::WebCryptoKeyFormatJwk, public_key, &jwk));
1501 EXPECT_TRUE(VerifyPublicJwk(jwk, test.jwk_alg, n_hex, e_hex, test.usage)); 1551 EXPECT_TRUE(VerifyPublicJwk(jwk,
1552 test.jwk_alg,
1553 kPublicKeyModulusHex,
1554 kPublicKeyExponentHex,
1555 test.usage));
1502 1556
1503 // Import the JWK back in to create a new key 1557 // Import the JWK back in to create a new key
1504 blink::WebCryptoKey public_key2 = blink::WebCryptoKey::createNull(); 1558 blink::WebCryptoKey public_key2 = blink::WebCryptoKey::createNull();
1505 EXPECT_EQ( 1559 ASSERT_EQ(
1506 Status::Success(), 1560 Status::Success(),
1507 ImportKeyJwk( 1561 ImportKeyJwk(
1508 CryptoData(jwk), test.algorithm, true, test.usage, &public_key2)); 1562 CryptoData(jwk), test.algorithm, true, test.usage, &public_key2));
1509 EXPECT_TRUE(public_key2.handle()); 1563 ASSERT_TRUE(public_key2.handle());
1510 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key2.type()); 1564 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key2.type());
1511 EXPECT_EQ(true, public_key2.extractable()); 1565 EXPECT_EQ(true, public_key2.extractable());
1512 EXPECT_EQ(test.algorithm.id(), public_key2.algorithm().id()); 1566 EXPECT_EQ(test.algorithm.id(), public_key2.algorithm().id());
1513 1567
1514 // Export the new key as spki and compare to the original. 1568 // Only perform SPKI consistency test for RSA-ES and RSA-SSA, as their
1515 std::vector<uint8> spki; 1569 // export format is the same as kPublicKeySpkiDerHex
Ryan Sleevi 2014/05/16 05:17:22 These should probably be moved into the JSON prope
eroman 2014/05/16 20:29:51 What is the difference in format, just the OID? Fe
Ryan Sleevi 2014/05/16 22:43:05 You already filed the bug - https://code.google.co
1516 ASSERT_EQ(Status::Success(), 1570 if (test.algorithm.id() == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 ||
1517 ExportKey(blink::WebCryptoKeyFormatSpki, public_key2, &spki)); 1571 test.algorithm.id() == blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5) {
1518 EXPECT_BYTES_EQ_HEX(kPublicKeySpkiDerHex, CryptoData(spki)); 1572 // Export the new key as spki and compare to the original.
1573 std::vector<uint8> spki;
1574 ASSERT_EQ(Status::Success(),
1575 ExportKey(blink::WebCryptoKeyFormatSpki, public_key2, &spki));
1576 EXPECT_BYTES_EQ_HEX(kPublicKeySpkiDerHex, CryptoData(spki));
1577 }
1519 } 1578 }
1520 } 1579 }
1521 1580
1522 TEST_F(SharedCryptoTest, MAYBE(ImportJwkRsaFailures)) { 1581 TEST_F(SharedCryptoTest, MAYBE(ImportJwkRsaFailures)) {
1523 base::DictionaryValue dict; 1582 base::DictionaryValue dict;
1524 RestoreJwkRsaDictionary(&dict); 1583 RestoreJwkRsaDictionary(&dict);
1525 blink::WebCryptoAlgorithm algorithm = 1584 blink::WebCryptoAlgorithm algorithm =
1526 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); 1585 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5);
1527 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageEncrypt; 1586 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageEncrypt;
1528 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1587 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after
1973 ImportKey(blink::WebCryptoKeyFormatSpki, 2032 ImportKey(blink::WebCryptoKeyFormatSpki,
1974 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)), 2033 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)),
1975 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5), 2034 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5),
1976 false, 2035 false,
1977 blink::WebCryptoKeyUsageEncrypt, 2036 blink::WebCryptoKeyUsageEncrypt,
1978 &key)); 2037 &key));
1979 EXPECT_TRUE(key.handle()); 2038 EXPECT_TRUE(key.handle());
1980 EXPECT_FALSE(key.extractable()); 2039 EXPECT_FALSE(key.extractable());
1981 EXPECT_EQ(Status::ErrorKeyNotExtractable(), 2040 EXPECT_EQ(Status::ErrorKeyNotExtractable(),
1982 ExportKey(blink::WebCryptoKeyFormatSpki, key, &output)); 2041 ExportKey(blink::WebCryptoKeyFormatSpki, key, &output));
2042
2043 // TODO(eroman): Failing test: Import a SPKI with an unrecognized hash OID
2044 // TODO(eroman): Failing test: Import a SPKI with invalid algorithm params
2045 // TODO(eroman): Failing test: Import a SPKI with inconsistent parameters
2046 // (e.g. SHA-1 in OID, SHA-256 in params)
2047 // TODO(eroman): Failing test: Import a SPKI for RSA-SSA, but with params
2048 // as OAEP/PSS
Ryan Sleevi 2014/05/16 05:17:22 Just documenting various SPKI failures.
eroman 2014/05/16 20:29:51 why TODO(eroman) and not TODO(rsleevi)? FYI TODO
Ryan Sleevi 2014/05/16 22:43:05 Because you filed the bug on Import/Export SPKI an
1983 } 2049 }
1984 2050
1985 TEST_F(SharedCryptoTest, MAYBE(ImportExportPkcs8)) { 2051 TEST_F(SharedCryptoTest, MAYBE(ImportExportPkcs8)) {
1986 // Passing case: Import a valid RSA key in PKCS#8 format. 2052 // Passing case: Import a valid RSA key in PKCS#8 format.
1987 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 2053 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1988 ASSERT_EQ(Status::Success(), 2054 ASSERT_EQ(Status::Success(),
1989 ImportKey(blink::WebCryptoKeyFormatPkcs8, 2055 ImportKey(blink::WebCryptoKeyFormatPkcs8,
1990 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)), 2056 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)),
1991 CreateRsaHashedImportAlgorithm( 2057 CreateRsaHashedImportAlgorithm(
1992 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 2058 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
2227 EXPECT_EQ(Status::ErrorUnexpectedKeyType(), 2293 EXPECT_EQ(Status::ErrorUnexpectedKeyType(),
2228 ExportKey(blink::WebCryptoKeyFormatSpki, private_key, &output)); 2294 ExportKey(blink::WebCryptoKeyFormatSpki, private_key, &output));
2229 } 2295 }
2230 2296
2231 TEST_F(SharedCryptoTest, MAYBE(RsaEsRoundTrip)) { 2297 TEST_F(SharedCryptoTest, MAYBE(RsaEsRoundTrip)) {
2232 // Import a key pair. 2298 // Import a key pair.
2233 blink::WebCryptoAlgorithm algorithm = 2299 blink::WebCryptoAlgorithm algorithm =
2234 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); 2300 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5);
2235 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 2301 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
2236 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 2302 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
2237 ImportRsaKeyPair( 2303 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(
Ryan Sleevi 2014/05/16 05:17:22 *slightly* unrelated, but the OAEP correction (to
2238 HexStringToBytes(kPublicKeySpkiDerHex), 2304 HexStringToBytes(kPublicKeySpkiDerHex),
2239 HexStringToBytes(kPrivateKeyPkcs8DerHex), 2305 HexStringToBytes(kPrivateKeyPkcs8DerHex),
2240 algorithm, 2306 algorithm,
2241 false, 2307 false,
2242 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt, 2308 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt,
2243 &public_key, 2309 &public_key,
2244 &private_key); 2310 &private_key));
2245 2311
2246 // Make a maximum-length data message. RSAES can operate on messages up to 2312 // Make a maximum-length data message. RSAES can operate on messages up to
2247 // length of k - 11 bytes, where k is the octet length of the RSA modulus. 2313 // length of k - 11 bytes, where k is the octet length of the RSA modulus.
2248 const unsigned int kMaxMsgSizeBytes = kModulusLengthBits / 8 - 11; 2314 const unsigned int kMaxMsgSizeBytes = kModulusLengthBits / 8 - 11;
2249 // There are two hex chars for each byte. 2315 // There are two hex chars for each byte.
2250 const unsigned int kMsgHexSize = kMaxMsgSizeBytes * 2; 2316 const unsigned int kMsgHexSize = kMaxMsgSizeBytes * 2;
2251 char max_data_hex[kMsgHexSize + 1]; 2317 char max_data_hex[kMsgHexSize + 1];
2252 std::fill(&max_data_hex[0], &max_data_hex[0] + kMsgHexSize, 'a'); 2318 std::fill(&max_data_hex[0], &max_data_hex[0] + kMsgHexSize, 'a');
2253 max_data_hex[kMsgHexSize] = '\0'; 2319 max_data_hex[kMsgHexSize] = '\0';
2254 2320
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
2295 GetBytesFromHexString(test, "rsa_pkcs8_der"); 2361 GetBytesFromHexString(test, "rsa_pkcs8_der");
2296 const std::vector<uint8> ciphertext = 2362 const std::vector<uint8> ciphertext =
2297 GetBytesFromHexString(test, "ciphertext"); 2363 GetBytesFromHexString(test, "ciphertext");
2298 const std::vector<uint8> cleartext = GetBytesFromHexString(test, "cleartext"); 2364 const std::vector<uint8> cleartext = GetBytesFromHexString(test, "cleartext");
2299 2365
2300 // Import the key pair. 2366 // Import the key pair.
2301 blink::WebCryptoAlgorithm algorithm = 2367 blink::WebCryptoAlgorithm algorithm =
2302 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); 2368 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5);
2303 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 2369 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
2304 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 2370 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
2305 ImportRsaKeyPair( 2371 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(
2306 rsa_spki_der, 2372 rsa_spki_der,
2307 rsa_pkcs8_der, 2373 rsa_pkcs8_der,
2308 algorithm, 2374 algorithm,
2309 false, 2375 false,
2310 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt, 2376 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt,
2311 &public_key, 2377 &public_key,
2312 &private_key); 2378 &private_key));
2313 2379
2314 // Decrypt the known-good ciphertext with the private key. As a check we must 2380 // Decrypt the known-good ciphertext with the private key. As a check we must
2315 // get the known original cleartext. 2381 // get the known original cleartext.
2316 std::vector<uint8> decrypted_data; 2382 std::vector<uint8> decrypted_data;
2317 ASSERT_EQ( 2383 ASSERT_EQ(
2318 Status::Success(), 2384 Status::Success(),
2319 Decrypt(algorithm, private_key, CryptoData(ciphertext), &decrypted_data)); 2385 Decrypt(algorithm, private_key, CryptoData(ciphertext), &decrypted_data));
2320 EXPECT_BYTES_EQ(cleartext, decrypted_data); 2386 EXPECT_BYTES_EQ(cleartext, decrypted_data);
2321 2387
2322 // Encrypt this decrypted data with the public key. 2388 // Encrypt this decrypted data with the public key.
(...skipping 13 matching lines...) Expand all
2336 algorithm, private_key, CryptoData(encrypted_data), &decrypted_data)); 2402 algorithm, private_key, CryptoData(encrypted_data), &decrypted_data));
2337 EXPECT_EQ(cleartext, decrypted_data); 2403 EXPECT_EQ(cleartext, decrypted_data);
2338 } 2404 }
2339 2405
2340 TEST_F(SharedCryptoTest, MAYBE(RsaEsFailures)) { 2406 TEST_F(SharedCryptoTest, MAYBE(RsaEsFailures)) {
2341 // Import a key pair. 2407 // Import a key pair.
2342 blink::WebCryptoAlgorithm algorithm = 2408 blink::WebCryptoAlgorithm algorithm =
2343 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); 2409 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5);
2344 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 2410 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
2345 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 2411 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
2346 ImportRsaKeyPair( 2412 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(
2347 HexStringToBytes(kPublicKeySpkiDerHex), 2413 HexStringToBytes(kPublicKeySpkiDerHex),
2348 HexStringToBytes(kPrivateKeyPkcs8DerHex), 2414 HexStringToBytes(kPrivateKeyPkcs8DerHex),
2349 algorithm, 2415 algorithm,
2350 false, 2416 false,
2351 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt, 2417 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt,
2352 &public_key, 2418 &public_key,
2353 &private_key); 2419 &private_key));
2354 2420
2355 // Fail encrypt with a private key. 2421 // Fail encrypt with a private key.
2356 std::vector<uint8> encrypted_data; 2422 std::vector<uint8> encrypted_data;
2357 const std::string message_hex_str("0102030405060708090a0b0c0d0e0f"); 2423 const std::string message_hex_str("0102030405060708090a0b0c0d0e0f");
2358 const std::vector<uint8> message_hex(HexStringToBytes(message_hex_str)); 2424 const std::vector<uint8> message_hex(HexStringToBytes(message_hex_str));
2359 EXPECT_EQ( 2425 EXPECT_EQ(
2360 Status::ErrorUnexpectedKeyType(), 2426 Status::ErrorUnexpectedKeyType(),
2361 Encrypt( 2427 Encrypt(
2362 algorithm, private_key, CryptoData(message_hex), &encrypted_data)); 2428 algorithm, private_key, CryptoData(message_hex), &encrypted_data));
2363 2429
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
2409 2475
2410 TEST_F(SharedCryptoTest, MAYBE(RsaSsaSignVerifyFailures)) { 2476 TEST_F(SharedCryptoTest, MAYBE(RsaSsaSignVerifyFailures)) {
2411 // Import a key pair. 2477 // Import a key pair.
2412 blink::WebCryptoKeyUsageMask usage_mask = 2478 blink::WebCryptoKeyUsageMask usage_mask =
2413 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify; 2479 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify;
2414 blink::WebCryptoAlgorithm importAlgorithm = 2480 blink::WebCryptoAlgorithm importAlgorithm =
2415 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 2481 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
2416 blink::WebCryptoAlgorithmIdSha1); 2482 blink::WebCryptoAlgorithmIdSha1);
2417 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 2483 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
2418 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 2484 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
2419 ImportRsaKeyPair(HexStringToBytes(kPublicKeySpkiDerHex), 2485 ASSERT_NO_FATAL_FAILURE(
2420 HexStringToBytes(kPrivateKeyPkcs8DerHex), 2486 ImportRsaKeyPair(HexStringToBytes(kPublicKeySpkiDerHex),
2421 importAlgorithm, 2487 HexStringToBytes(kPrivateKeyPkcs8DerHex),
2422 false, 2488 importAlgorithm,
2423 usage_mask, 2489 false,
2424 &public_key, 2490 usage_mask,
2425 &private_key); 2491 &public_key,
2492 &private_key));
2426 2493
2427 blink::WebCryptoAlgorithm algorithm = 2494 blink::WebCryptoAlgorithm algorithm =
2428 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5); 2495 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5);
2429 2496
2430 std::vector<uint8> signature; 2497 std::vector<uint8> signature;
2431 bool signature_match; 2498 bool signature_match;
2432 2499
2433 // Compute a signature. 2500 // Compute a signature.
2434 const std::vector<uint8> data = HexStringToBytes("010203040506070809"); 2501 const std::vector<uint8> data = HexStringToBytes("010203040506070809");
2435 ASSERT_EQ(Status::Success(), 2502 ASSERT_EQ(Status::Success(),
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
2552 TEST_F(SharedCryptoTest, MAYBE(RsaSignVerifyKnownAnswer)) { 2619 TEST_F(SharedCryptoTest, MAYBE(RsaSignVerifyKnownAnswer)) {
2553 scoped_ptr<base::ListValue> tests; 2620 scoped_ptr<base::ListValue> tests;
2554 ASSERT_TRUE(ReadJsonTestFileToList("pkcs1v15_sign.json", &tests)); 2621 ASSERT_TRUE(ReadJsonTestFileToList("pkcs1v15_sign.json", &tests));
2555 2622
2556 // Import the key pair. 2623 // Import the key pair.
2557 blink::WebCryptoAlgorithm importAlgorithm = 2624 blink::WebCryptoAlgorithm importAlgorithm =
2558 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 2625 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
2559 blink::WebCryptoAlgorithmIdSha1); 2626 blink::WebCryptoAlgorithmIdSha1);
2560 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 2627 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
2561 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 2628 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
2562 ImportRsaKeyPair( 2629 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(
2563 HexStringToBytes(kPublicKeySpkiDerHex), 2630 HexStringToBytes(kPublicKeySpkiDerHex),
2564 HexStringToBytes(kPrivateKeyPkcs8DerHex), 2631 HexStringToBytes(kPrivateKeyPkcs8DerHex),
2565 importAlgorithm, 2632 importAlgorithm,
2566 false, 2633 false,
2567 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify, 2634 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify,
2568 &public_key, 2635 &public_key,
2569 &private_key); 2636 &private_key));
2570 2637
2571 blink::WebCryptoAlgorithm algorithm = 2638 blink::WebCryptoAlgorithm algorithm =
2572 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5); 2639 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5);
2573 2640
2574 // Validate the signatures are computed and verified as expected. 2641 // Validate the signatures are computed and verified as expected.
2575 std::vector<uint8> signature; 2642 std::vector<uint8> signature;
2576 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { 2643 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
2577 SCOPED_TRACE(test_index); 2644 SCOPED_TRACE(test_index);
2578 2645
2579 base::DictionaryValue* test; 2646 base::DictionaryValue* test;
(...skipping 553 matching lines...) Expand 10 before | Expand all | Expand 10 after
3133 GetBytesFromHexString(test, "ciphertext"); 3200 GetBytesFromHexString(test, "ciphertext");
3134 const std::vector<uint8> cleartext = GetBytesFromHexString(test, "cleartext"); 3201 const std::vector<uint8> cleartext = GetBytesFromHexString(test, "cleartext");
3135 blink::WebCryptoAlgorithm key_algorithm = 3202 blink::WebCryptoAlgorithm key_algorithm =
3136 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha256); 3203 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha256);
3137 3204
3138 // Import the RSA key pair. 3205 // Import the RSA key pair.
3139 blink::WebCryptoAlgorithm algorithm = 3206 blink::WebCryptoAlgorithm algorithm =
3140 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); 3207 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5);
3141 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 3208 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
3142 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 3209 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
3143 ImportRsaKeyPair( 3210 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(
3144 rsa_spki_der, 3211 rsa_spki_der,
3145 rsa_pkcs8_der, 3212 rsa_pkcs8_der,
3146 algorithm, 3213 algorithm,
3147 false, 3214 false,
3148 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey, 3215 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey,
3149 &public_key, 3216 &public_key,
3150 &private_key); 3217 &private_key));
3151 3218
3152 // Import the symmetric key. 3219 // Import the symmetric key.
3153 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 3220 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
3154 ASSERT_EQ(Status::Success(), 3221 ASSERT_EQ(Status::Success(),
3155 ImportKey(blink::WebCryptoKeyFormatRaw, 3222 ImportKey(blink::WebCryptoKeyFormatRaw,
3156 CryptoData(cleartext), 3223 CryptoData(cleartext),
3157 key_algorithm, 3224 key_algorithm,
3158 true, 3225 true,
3159 blink::WebCryptoKeyUsageSign, 3226 blink::WebCryptoKeyUsageSign,
3160 &key)); 3227 &key));
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
3210 TEST_F(SharedCryptoTest, MAYBE(RsaEsRawSymkeyWrapUnwrapErrors)) { 3277 TEST_F(SharedCryptoTest, MAYBE(RsaEsRawSymkeyWrapUnwrapErrors)) {
3211 const std::vector<uint8> data(64, 0); 3278 const std::vector<uint8> data(64, 0);
3212 blink::WebCryptoAlgorithm key_algorithm = 3279 blink::WebCryptoAlgorithm key_algorithm =
3213 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha256); 3280 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha256);
3214 3281
3215 // Import the RSA key pair. 3282 // Import the RSA key pair.
3216 blink::WebCryptoAlgorithm wrapping_algorithm = 3283 blink::WebCryptoAlgorithm wrapping_algorithm =
3217 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); 3284 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5);
3218 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 3285 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
3219 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 3286 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
3220 ImportRsaKeyPair( 3287 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(
3221 HexStringToBytes(kPublicKeySpkiDerHex), 3288 HexStringToBytes(kPublicKeySpkiDerHex),
3222 HexStringToBytes(kPrivateKeyPkcs8DerHex), 3289 HexStringToBytes(kPrivateKeyPkcs8DerHex),
3223 wrapping_algorithm, 3290 wrapping_algorithm,
3224 false, 3291 false,
3225 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey, 3292 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey,
3226 &public_key, 3293 &public_key,
3227 &private_key); 3294 &private_key));
3228 3295
3229 // Import the symmetric key. 3296 // Import the symmetric key.
3230 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 3297 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
3231 ASSERT_EQ(Status::Success(), 3298 ASSERT_EQ(Status::Success(),
3232 ImportKey(blink::WebCryptoKeyFormatRaw, 3299 ImportKey(blink::WebCryptoKeyFormatRaw,
3233 CryptoData(data), 3300 CryptoData(data),
3234 key_algorithm, 3301 key_algorithm,
3235 true, 3302 true,
3236 blink::WebCryptoKeyUsageSign, 3303 blink::WebCryptoKeyUsageSign,
3237 &key)); 3304 &key));
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
3363 ASSERT_EQ( 3430 ASSERT_EQ(
3364 Status::Success(), 3431 Status::Success(),
3365 GenerateSecretKey( 3432 GenerateSecretKey(
3366 gen_algorithm, true, blink::WebCryptoKeyUsageEncrypt, &key_to_wrap)); 3433 gen_algorithm, true, blink::WebCryptoKeyUsageEncrypt, &key_to_wrap));
3367 3434
3368 // Import the wrapping key pair. 3435 // Import the wrapping key pair.
3369 const blink::WebCryptoAlgorithm wrapping_algorithm = 3436 const blink::WebCryptoAlgorithm wrapping_algorithm =
3370 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); 3437 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5);
3371 blink::WebCryptoKey public_wrapping_key = blink::WebCryptoKey::createNull(); 3438 blink::WebCryptoKey public_wrapping_key = blink::WebCryptoKey::createNull();
3372 blink::WebCryptoKey private_wrapping_key = blink::WebCryptoKey::createNull(); 3439 blink::WebCryptoKey private_wrapping_key = blink::WebCryptoKey::createNull();
3373 ImportRsaKeyPair( 3440 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(
3374 HexStringToBytes(kPublicKeySpkiDerHex), 3441 HexStringToBytes(kPublicKeySpkiDerHex),
3375 HexStringToBytes(kPrivateKeyPkcs8DerHex), 3442 HexStringToBytes(kPrivateKeyPkcs8DerHex),
3376 wrapping_algorithm, 3443 wrapping_algorithm,
3377 false, 3444 false,
3378 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey, 3445 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey,
3379 &public_wrapping_key, 3446 &public_wrapping_key,
3380 &private_wrapping_key); 3447 &private_wrapping_key));
3381 3448
3382 // Wrap the symkey in JWK format, using the public wrapping key. 3449 // Wrap the symkey in JWK format, using the public wrapping key.
3383 std::vector<uint8> wrapped_data; 3450 std::vector<uint8> wrapped_data;
3384 ASSERT_EQ(Status::Success(), 3451 ASSERT_EQ(Status::Success(),
3385 WrapKey(blink::WebCryptoKeyFormatJwk, 3452 WrapKey(blink::WebCryptoKeyFormatJwk,
3386 key_to_wrap, 3453 key_to_wrap,
3387 public_wrapping_key, 3454 public_wrapping_key,
3388 wrapping_algorithm, 3455 wrapping_algorithm,
3389 &wrapped_data)); 3456 &wrapped_data));
3390 3457
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
3472 UnwrapKey(blink::WebCryptoKeyFormatJwk, 3539 UnwrapKey(blink::WebCryptoKeyFormatJwk,
3473 CryptoData(ciphertext), 3540 CryptoData(ciphertext),
3474 private_wrapping_key, 3541 private_wrapping_key,
3475 algorithm, 3542 algorithm,
3476 CreateAesCbcAlgorithm(std::vector<uint8>(0, 16)), 3543 CreateAesCbcAlgorithm(std::vector<uint8>(0, 16)),
3477 true, 3544 true,
3478 blink::WebCryptoKeyUsageEncrypt, 3545 blink::WebCryptoKeyUsageEncrypt,
3479 &unwrapped_key)); 3546 &unwrapped_key));
3480 } 3547 }
3481 3548
3549 class SharedCryptoRsaOaepTest : public ::testing::Test {
3550 public:
3551 SharedCryptoRsaOaepTest() { Init(); }
eroman 2014/05/16 20:29:51 Would inheriting from SharedCryptoTest instead wor
3552
3553 scoped_ptr<base::DictionaryValue> CreatePublicKeyJwkDict() {
eroman 2014/05/16 20:29:51 why introduce a new test fixture, can't this be a
Ryan Sleevi 2014/05/16 22:43:05 It can. A new fixture was created, because in my i
eroman 2014/05/19 19:04:33 Ok
3554 scoped_ptr<base::DictionaryValue> jwk(new base::DictionaryValue());
3555 jwk->SetString("kty", "RSA");
3556 jwk->SetString("n",
3557 Base64EncodeUrlSafe(HexStringToBytes(kPublicKeyModulusHex)));
3558 jwk->SetString(
3559 "e", Base64EncodeUrlSafe(HexStringToBytes(kPublicKeyExponentHex)));
3560 return jwk.Pass();
3561 }
3562 };
3563
3564 // Import a PKCS#8 private key that uses RSAPrivateKey with the
3565 // id-rsaEncryption OID.
3566 TEST_F(SharedCryptoRsaOaepTest, ImportPkcs8WithRsaEncryption) {
Ryan Sleevi 2014/05/16 05:17:22 More tests needed: Variations of SPKI param handli
eroman 2014/05/16 20:29:51 These tests are going to fail when run on Android
Ryan Sleevi 2014/05/16 22:43:05 No. Because SupportsRsaOaep().
3567 if (!SupportsRsaOaep()) {
3568 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3569 return;
3570 }
3571
3572 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
3573 ASSERT_EQ(Status::Success(),
3574 ImportKey(blink::WebCryptoKeyFormatPkcs8,
3575 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)),
3576 CreateRsaHashedImportAlgorithm(
3577 blink::WebCryptoAlgorithmIdRsaOaep,
3578 blink::WebCryptoAlgorithmIdSha1),
3579 true,
3580 blink::WebCryptoKeyUsageDecrypt,
3581 &private_key));
3582 }
3583
3584 TEST_F(SharedCryptoRsaOaepTest, ImportJwkWithNoAlg) {
3585 if (!SupportsRsaOaep()) {
3586 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3587 return;
3588 }
3589
3590 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict());
3591
3592 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
3593 ASSERT_EQ(Status::Success(),
3594 ImportKeyJwkFromDict(*jwk.get(),
3595 CreateRsaHashedImportAlgorithm(
3596 blink::WebCryptoAlgorithmIdRsaOaep,
3597 blink::WebCryptoAlgorithmIdSha1),
3598 true,
3599 blink::WebCryptoKeyUsageEncrypt,
3600 &public_key));
eroman 2014/05/16 20:29:51 Assert that the resulting key.algorithm() matches?
3601 }
3602
3603 TEST_F(SharedCryptoRsaOaepTest, ImportJwkWithMatchingAlg) {
3604 if (!SupportsRsaOaep()) {
3605 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3606 return;
3607 }
3608
3609 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict());
3610 jwk->SetString("alg", "RSA-OAEP");
3611
3612 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
3613 ASSERT_EQ(Status::Success(),
3614 ImportKeyJwkFromDict(*jwk.get(),
3615 CreateRsaHashedImportAlgorithm(
3616 blink::WebCryptoAlgorithmIdRsaOaep,
3617 blink::WebCryptoAlgorithmIdSha1),
3618 true,
3619 blink::WebCryptoKeyUsageEncrypt,
3620 &public_key));
3621 }
3622
3623 TEST_F(SharedCryptoRsaOaepTest, ImportJwkWithMismatchedAlgFails) {
3624 if (!SupportsRsaOaep()) {
3625 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3626 return;
3627 }
3628
3629 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict());
3630 jwk->SetString("alg", "RSA-OAEP-512");
3631
3632 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
3633 ASSERT_EQ(Status::ErrorJwkAlgorithmInconsistent(),
3634 ImportKeyJwkFromDict(*jwk.get(),
3635 CreateRsaHashedImportAlgorithm(
3636 blink::WebCryptoAlgorithmIdRsaOaep,
3637 blink::WebCryptoAlgorithmIdSha1),
3638 true,
3639 blink::WebCryptoKeyUsageEncrypt,
3640 &public_key));
3641 }
3642
3643 TEST_F(SharedCryptoRsaOaepTest, ImportJwkWithMismatchedTypFails) {
3644 if (!SupportsRsaOaep()) {
3645 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3646 return;
3647 }
3648
3649 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict());
3650 jwk->SetString("kty", "oct");
3651 jwk->SetString("alg", "RSA-OAEP");
3652
3653 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
3654 ASSERT_EQ(Status::ErrorJwkPropertyMissing("k"),
3655 ImportKeyJwkFromDict(*jwk.get(),
3656 CreateRsaHashedImportAlgorithm(
3657 blink::WebCryptoAlgorithmIdRsaOaep,
3658 blink::WebCryptoAlgorithmIdSha1),
3659 true,
3660 blink::WebCryptoKeyUsageEncrypt,
3661 &public_key));
3662 }
3663
3664 TEST_F(SharedCryptoRsaOaepTest, ExportJwk) {
eroman 2014/05/16 20:29:51 Include "Public" somewhere in the name.
3665 if (!SupportsRsaOaep()) {
3666 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3667 return;
3668 }
3669
3670 struct TestData {
3671 blink::WebCryptoAlgorithmId hash_alg;
3672 const char* expected_jwk_alg;
3673 } kTestData[] = {{blink::WebCryptoAlgorithmIdSha1, "RSA-OAEP"},
3674 {blink::WebCryptoAlgorithmIdSha256, "RSA-OAEP-256"},
3675 {blink::WebCryptoAlgorithmIdSha384, "RSA-OAEP-384"},
3676 {blink::WebCryptoAlgorithmIdSha512, "RSA-OAEP-512"}};
3677 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestData); ++i) {
3678 const TestData& test_data = kTestData[i];
3679 SCOPED_TRACE(test_data.expected_jwk_alg);
3680
3681 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict());
3682 jwk->SetString("alg", test_data.expected_jwk_alg);
3683
3684 // Import the key in a known-good format
3685 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
3686 ASSERT_EQ(Status::Success(),
3687 ImportKeyJwkFromDict(
3688 *jwk.get(),
3689 CreateRsaHashedImportAlgorithm(
3690 blink::WebCryptoAlgorithmIdRsaOaep, test_data.hash_alg),
3691 true,
3692 blink::WebCryptoKeyUsageEncrypt,
3693 &public_key));
3694
3695 // Now export the key as JWK and verify its contents
3696 std::vector<uint8> jwk_data;
3697 ASSERT_EQ(Status::Success(),
3698 ExportKey(blink::WebCryptoKeyFormatJwk, public_key, &jwk_data));
3699 EXPECT_TRUE(VerifyPublicJwk(jwk_data,
3700 test_data.expected_jwk_alg,
3701 kPublicKeyModulusHex,
3702 kPublicKeyExponentHex,
3703 blink::WebCryptoKeyUsageEncrypt));
3704 }
3705 }
3706
3707 TEST_F(SharedCryptoRsaOaepTest, EncryptDecryptKnownAnswerTest) {
3708 if (!SupportsRsaOaep()) {
3709 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3710 return;
3711 }
3712
3713 scoped_ptr<base::ListValue> tests;
3714 ASSERT_TRUE(ReadJsonTestFileToList("rsa_oaep.json", &tests));
3715
3716 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
3717 SCOPED_TRACE(test_index);
3718
3719 base::DictionaryValue* test = NULL;
3720 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
3721
3722 blink::WebCryptoAlgorithm digest_algorithm =
3723 GetDigestAlgorithm(test, "hash");
3724 ASSERT_FALSE(digest_algorithm.isNull());
3725 std::vector<uint8> public_key_der =
3726 GetBytesFromHexString(test, "public_key");
3727 std::vector<uint8> private_key_der =
3728 GetBytesFromHexString(test, "private_key");
3729 std::vector<uint8> ciphertext = GetBytesFromHexString(test, "ciphertext");
3730 std::vector<uint8> plaintext = GetBytesFromHexString(test, "plaintext");
3731 std::vector<uint8> label = GetBytesFromHexString(test, "label");
3732
3733 blink::WebCryptoAlgorithm import_algorithm = CreateRsaHashedImportAlgorithm(
3734 blink::WebCryptoAlgorithmIdRsaOaep, digest_algorithm.id());
3735 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
3736 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
3737
3738 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(
3739 public_key_der,
3740 private_key_der,
3741 import_algorithm,
3742 false,
3743 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt,
3744 &public_key,
3745 &private_key));
3746
3747 blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label);
3748 std::vector<uint8> decrypted_data;
3749 ASSERT_EQ(Status::Success(),
3750 Decrypt(op_algorithm,
3751 private_key,
3752 CryptoData(ciphertext),
3753 &decrypted_data));
3754 EXPECT_BYTES_EQ(plaintext, decrypted_data);
3755 std::vector<uint8> encrypted_data;
3756 ASSERT_EQ(
3757 Status::Success(),
3758 Encrypt(
3759 op_algorithm, public_key, CryptoData(plaintext), &encrypted_data));
3760 std::vector<uint8> redecrypted_data;
3761 ASSERT_EQ(Status::Success(),
3762 Decrypt(op_algorithm,
3763 private_key,
3764 CryptoData(encrypted_data),
3765 &redecrypted_data));
3766 EXPECT_BYTES_EQ(plaintext, redecrypted_data);
3767 }
3768 }
3769
3770 TEST_F(SharedCryptoRsaOaepTest, EncryptWithLargeMessageFails) {
3771 if (!SupportsRsaOaep()) {
3772 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3773 return;
3774 }
3775
3776 FAIL();
eroman 2014/05/16 20:29:51 Wut? Either fill these out, make them into TODOs,
3777 }
3778
3779 TEST_F(SharedCryptoRsaOaepTest, DecryptWithLargeMessageFails) {
3780 if (!SupportsRsaOaep()) {
3781 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3782 return;
3783 }
3784
3785 FAIL();
3786 }
3787
3788 TEST_F(SharedCryptoRsaOaepTest, DecryptWithCorruptedHashFails) {
3789 if (!SupportsRsaOaep()) {
3790 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3791 return;
3792 }
3793
3794 FAIL();
3795 }
3796
3797 TEST_F(SharedCryptoRsaOaepTest, DecryptWithCorruptedPaddingFails) {
3798 if (!SupportsRsaOaep()) {
3799 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3800 return;
3801 }
3802
3803 FAIL();
3804 }
3805
3806 TEST_F(SharedCryptoRsaOaepTest, WrapUnwrapRawKey) {
3807 if (!SupportsRsaOaep()) {
3808 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3809 return;
3810 }
3811
3812 FAIL();
3813 }
3814
3815 TEST_F(SharedCryptoRsaOaepTest, WrapUnwrapJwkSymKey) {
3816 if (!SupportsRsaOaep()) {
3817 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3818 return;
3819 }
3820
3821 FAIL();
3822 }
3823
3824 TEST_F(SharedCryptoRsaOaepTest, WrapWithLargeKeyFails) {
3825 if (!SupportsRsaOaep()) {
3826 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3827 return;
3828 }
3829
3830 FAIL();
3831 }
3832
3833 TEST_F(SharedCryptoRsaOaepTest, UnwrapWithCorruptedKeyFails) {
3834 if (!SupportsRsaOaep()) {
3835 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3836 return;
3837 }
3838
3839 FAIL();
3840 }
3841
3842 TEST_F(SharedCryptoRsaOaepTest, UnwrapWithInvalidKeyFails) {
3843 if (!SupportsRsaOaep()) {
3844 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3845 return;
3846 }
3847
3848 FAIL();
3849 }
3850
3482 } // namespace webcrypto 3851 } // namespace webcrypto
3483 3852
3484 } // namespace content 3853 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698