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

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: Fix ifdef 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
« no previous file with comments | « content/child/webcrypto/shared_crypto.cc ('k') | content/child/webcrypto/webcrypto_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_OPENSSL)
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"))
121 return false;
122 crypto::ScopedPK11Slot slot(PK11_GetInternalKeySlot());
123 return !!PK11_DoesMechanism(slot.get(), CKM_RSA_PKCS_OAEP);
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
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"
436 "FFEDB162B4C0F283A12A88A394DFF526AB7291CBB307CEABFCE0B1DFD5CD9508096D5B2B"
437 "8B6DF5D671EF6377C0921CB23C270A70E2598E6FF89D19F105ACC2D3F0CB35F29280E138"
438 "6B6F64C4EF22E1E1F20D0CE8CFFB2249BD9A2137";
439 const char* const kPublicKeyExponentHex = "010001";
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(),
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"
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;
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
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
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(
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(); }
3552
3553 scoped_ptr<base::DictionaryValue> CreatePublicKeyJwkDict() {
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) {
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, ImportPublicJwkWithNoAlg) {
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));
3601 }
3602
3603 TEST_F(SharedCryptoRsaOaepTest, ImportPublicJwkWithMatchingAlg) {
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, ImportPublicJwkWithMismatchedAlgFails) {
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, ImportPublicJwkWithMismatchedTypFails) {
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, ExportPublicJwk) {
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 const blink::WebCryptoAlgorithmId kHash = blink::WebCryptoAlgorithmIdSha1;
3777 const size_t kHashSize = 20;
3778
3779 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict());
3780
3781 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
3782 ASSERT_EQ(Status::Success(),
3783 ImportKeyJwkFromDict(*jwk.get(),
3784 CreateRsaHashedImportAlgorithm(
3785 blink::WebCryptoAlgorithmIdRsaOaep, kHash),
3786 true,
3787 blink::WebCryptoKeyUsageEncrypt,
3788 &public_key));
3789
3790 // The maximum size of an encrypted message is:
3791 // modulus length
3792 // - 1 (leading octet)
3793 // - hash size (maskedSeed)
3794 // - hash size (lHash portion of maskedDB)
3795 // - 1 (at least one octet for the padding string)
3796 size_t kMaxMessageSize = (kModulusLengthBits / 8) - 2 - (2 * kHashSize);
3797
3798 // The label has no influence on the maximum message size. For simplicity,
3799 // use the empty string.
3800 std::vector<uint8> label;
3801 blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label);
3802
3803 // Test that a message just before the boundary succeeds.
3804 std::string large_message;
3805 large_message.resize(kMaxMessageSize - 1, 'A');
3806
3807 std::vector<uint8> ciphertext;
3808 ASSERT_EQ(
3809 Status::Success(),
3810 Encrypt(
3811 op_algorithm, public_key, CryptoData(large_message), &ciphertext));
3812
3813 // Test that a message at the boundary succeeds.
3814 large_message.resize(kMaxMessageSize, 'A');
3815 ciphertext.clear();
3816
3817 ASSERT_EQ(
3818 Status::Success(),
3819 Encrypt(
3820 op_algorithm, public_key, CryptoData(large_message), &ciphertext));
3821
3822 // Test that a message greater than the largest size fails.
3823 large_message.resize(kMaxMessageSize + 1, 'A');
3824 ciphertext.clear();
3825
3826 ASSERT_EQ(
3827 Status::OperationError(),
3828 Encrypt(
3829 op_algorithm, public_key, CryptoData(large_message), &ciphertext));
3830 }
3831
3832 // Ensures that if the selected hash algorithm for the RSA-OAEP message is too
3833 // large, then it is rejected, independent of the actual message to be
3834 // encrypted.
3835 // For example, a 1024-bit RSA key is too small to accomodate a message that
3836 // uses OAEP with SHA-512, since it requires 1040 bits to encode
3837 // (2 * hash size + 2 padding bytes).
3838 TEST_F(SharedCryptoRsaOaepTest, EncryptWithLargeDigestFails) {
3839 if (!SupportsRsaOaep()) {
3840 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3841 return;
3842 }
3843
3844 const blink::WebCryptoAlgorithmId kHash = blink::WebCryptoAlgorithmIdSha512;
3845
3846 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict());
3847
3848 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
3849 ASSERT_EQ(Status::Success(),
3850 ImportKeyJwkFromDict(*jwk.get(),
3851 CreateRsaHashedImportAlgorithm(
3852 blink::WebCryptoAlgorithmIdRsaOaep, kHash),
3853 true,
3854 blink::WebCryptoKeyUsageEncrypt,
3855 &public_key));
3856
3857 // The label has no influence on the maximum message size. For simplicity,
3858 // use the empty string.
3859 std::vector<uint8> label;
3860 blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label);
3861
3862 std::string small_message("A");
3863 std::vector<uint8> ciphertext;
3864 // This is an operation error, as the internal consistency checking of the
3865 // algorithm parameters is up to the implementation.
3866 ASSERT_EQ(
3867 Status::OperationError(),
3868 Encrypt(
3869 op_algorithm, public_key, CryptoData(small_message), &ciphertext));
3870 }
3871
3872 TEST_F(SharedCryptoRsaOaepTest, DecryptWithLargeMessageFails) {
3873 if (!SupportsRsaOaep()) {
3874 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3875 return;
3876 }
3877
3878 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
3879 ASSERT_EQ(Status::Success(),
3880 ImportKey(blink::WebCryptoKeyFormatPkcs8,
3881 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)),
3882 CreateRsaHashedImportAlgorithm(
3883 blink::WebCryptoAlgorithmIdRsaOaep,
3884 blink::WebCryptoAlgorithmIdSha1),
3885 true,
3886 blink::WebCryptoKeyUsageDecrypt,
3887 &private_key));
3888
3889 // The label has no influence on the maximum message size. For simplicity,
3890 // use the empty string.
3891 std::vector<uint8> label;
3892 blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label);
3893
3894 std::string large_dummy_message(kModulusLengthBits / 8, 'A');
3895 std::vector<uint8> plaintext;
3896
3897 ASSERT_EQ(Status::OperationError(),
3898 Decrypt(op_algorithm,
3899 private_key,
3900 CryptoData(large_dummy_message),
3901 &plaintext));
3902 }
3903
3904 TEST_F(SharedCryptoRsaOaepTest, WrapUnwrapRawKey) {
3905 if (!SupportsRsaOaep()) {
3906 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3907 return;
3908 }
3909
3910 blink::WebCryptoAlgorithm import_algorithm = CreateRsaHashedImportAlgorithm(
3911 blink::WebCryptoAlgorithmIdRsaOaep, blink::WebCryptoAlgorithmIdSha1);
3912 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
3913 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
3914
3915 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(
3916 HexStringToBytes(kPublicKeySpkiDerHex),
3917 HexStringToBytes(kPrivateKeyPkcs8DerHex),
3918 import_algorithm,
3919 false,
3920 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt |
3921 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey,
3922 &public_key,
3923 &private_key));
3924
3925 std::vector<uint8> label;
3926 blink::WebCryptoAlgorithm wrapping_algorithm = CreateRsaOaepAlgorithm(label);
3927
3928 const std::string key_hex = "000102030405060708090A0B0C0D0E0F";
3929 const blink::WebCryptoAlgorithm key_algorithm =
3930 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc);
3931
3932 blink::WebCryptoKey key =
3933 ImportSecretKeyFromRaw(HexStringToBytes(key_hex),
3934 key_algorithm,
3935 blink::WebCryptoKeyUsageEncrypt);
3936 ASSERT_FALSE(key.isNull());
3937
3938 std::vector<uint8> wrapped_key;
3939 ASSERT_EQ(Status::Success(),
3940 WrapKey(blink::WebCryptoKeyFormatRaw,
3941 key,
3942 public_key,
3943 wrapping_algorithm,
3944 &wrapped_key));
3945
3946 // Verify that |wrapped_key| can be decrypted and yields the key data.
3947 // Because |private_key| supports both decrypt and unwrap, this is valid.
3948 std::vector<uint8> decrypted_key;
3949 ASSERT_EQ(Status::Success(),
3950 Decrypt(wrapping_algorithm,
3951 private_key,
3952 CryptoData(wrapped_key),
3953 &decrypted_key));
3954 EXPECT_BYTES_EQ_HEX(key_hex, decrypted_key);
3955
3956 // Now attempt to unwrap the key, which should also decrypt the data.
3957 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull();
3958 ASSERT_EQ(Status::Success(),
3959 UnwrapKey(blink::WebCryptoKeyFormatRaw,
3960 CryptoData(wrapped_key),
3961 private_key,
3962 wrapping_algorithm,
3963 key_algorithm,
3964 true,
3965 blink::WebCryptoKeyUsageEncrypt,
3966 &unwrapped_key));
3967 ASSERT_FALSE(unwrapped_key.isNull());
3968
3969 std::vector<uint8> raw_key;
3970 ASSERT_EQ(Status::Success(),
3971 ExportKey(blink::WebCryptoKeyFormatRaw, unwrapped_key, &raw_key));
3972 EXPECT_BYTES_EQ_HEX(key_hex, raw_key);
3973 }
3974
3975 TEST_F(SharedCryptoRsaOaepTest, WrapUnwrapJwkSymKey) {
3976 if (!SupportsRsaOaep()) {
3977 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3978 return;
3979 }
3980
3981 // The public and private portions of a 2048-bit RSA key with the
3982 // id-rsaEncryption OID
3983 const char kPublicKey2048SpkiDerHex[] =
3984 "30820122300d06092a864886f70d01010105000382010f003082010a0282010100c5d8ce"
3985 "137a38168c8ab70229cfa5accc640567159750a312ce2e7d54b6e2fdd59b300c6a6c9764"
3986 "f8de6f00519cdb90111453d273a967462786480621f9e7cee5b73d63358448e7183a3a68"
3987 "e991186359f26aa88fbca5f53e673e502e4c5a2ba5068aeba60c9d0c44d872458d1b1e2f"
3988 "7f339f986076d516e93dc750f0b7680b6f5f02bc0d5590495be04c4ae59d34ba17bc5d08"
3989 "a93c75cfda2828f4a55b153af912038438276cb4a14f8116ca94db0ea9893652d02fc606"
3990 "36f19975e3d79a4d8ea8bfed6f8e0a24b63d243b08ea70a086ad56dd6341d733711c89ca"
3991 "749d4a80b3e6ecd2f8e53731eadeac2ea77788ee55d7b4b47c0f2523fbd61b557c16615d"
3992 "5d0203010001";
3993 const char kPrivateKey2048Pkcs8DerHex[] =
3994 "308204bd020100300d06092a864886f70d0101010500048204a7308204a3020100028201"
3995 "0100c5d8ce137a38168c8ab70229cfa5accc640567159750a312ce2e7d54b6e2fdd59b30"
3996 "0c6a6c9764f8de6f00519cdb90111453d273a967462786480621f9e7cee5b73d63358448"
3997 "e7183a3a68e991186359f26aa88fbca5f53e673e502e4c5a2ba5068aeba60c9d0c44d872"
3998 "458d1b1e2f7f339f986076d516e93dc750f0b7680b6f5f02bc0d5590495be04c4ae59d34"
3999 "ba17bc5d08a93c75cfda2828f4a55b153af912038438276cb4a14f8116ca94db0ea98936"
4000 "52d02fc60636f19975e3d79a4d8ea8bfed6f8e0a24b63d243b08ea70a086ad56dd6341d7"
4001 "33711c89ca749d4a80b3e6ecd2f8e53731eadeac2ea77788ee55d7b4b47c0f2523fbd61b"
4002 "557c16615d5d02030100010282010074b70feb41a0b0fcbc207670400556c9450042ede3"
4003 "d4383fb1ce8f3558a6d4641d26dd4c333fa4db842d2b9cf9d2354d3e16ad027a9f682d8c"
4004 "f4145a1ad97b9edcd8a41c402bd9d8db10f62f43df854cdccbbb2100834f083f53ed6d42"
4005 "b1b729a59072b004a4e945fc027db15e9c121d1251464d320d4774d5732df6b3dbf751f4"
4006 "9b19c9db201e19989c883bbaad5333db47f64f6f7a95b8d4936b10d945aa3f794cfaab62"
4007 "e7d47686129358914f3b8085f03698a650ab5b8c7e45813f2b0515ec05b6e5195b6a7c2a"
4008 "0d36969745f431ded4fd059f6aa361a4649541016d356297362b778e90f077d48815b339"
4009 "ec6f43aba345df93e67fcb6c2cb5b4544e9be902818100e9c90abe5f9f32468c5b6d630c"
4010 "54a4d7d75e29a72cf792f21e242aac78fd7995c42dfd4ae871d2619ff7096cb05baa78e3"
4011 "23ecab338401a8059adf7a0d8be3b21edc9a9c82c5605634a2ec81ec053271721351868a"
4012 "4c2e50c689d7cef94e31ff23658af5843366e2b289c5bf81d72756a7b93487dd8770d69c"
4013 "1f4e089d6d89f302818100d8a58a727c4e209132afd9933b98c89aca862a01cc0be74133"
4014 "bee517909e5c379e526895ac4af11780c1fe91194c777c9670b6423f0f5a32fd7691a622"
4015 "113eef4bed2ef863363a335fd55b0e75088c582437237d7f3ed3f0a643950237bc6e6277"
4016 "ccd0d0a1b4170aa1047aa7ffa7c8c54be10e8c7327ae2e0885663963817f6f02818100e5"
4017 "aed9ba4d71b7502e6748a1ce247ecb7bd10c352d6d9256031cdf3c11a65e44b0b7ca2945"
4018 "134671195af84c6b3bb3d10ebf65ae916f38bd5dbc59a0ad1c69b8beaf57cb3a8335f19b"
4019 "c7117b576987b48331cd9fd3d1a293436b7bb5e1a35c6560de4b5688ea834367cb0997eb"
4020 "b578f59ed4cb724c47dba94d3b484c1876dcd70281807f15bc7d2406007cac2b138a96af"
4021 "2d1e00276b84da593132c253fcb73212732dfd25824c2a615bc3d9b7f2c8d2fa542d3562"
4022 "b0c7738e61eeff580a6056239fb367ea9e5efe73d4f846033602e90c36a78db6fa8ea792"
4023 "0769675ec58e237bd994d189c8045a96f5dd3a4f12547257ce224e3c9af830a4da3c0eab"
4024 "9227a0035ae9028180067caea877e0b23090fc689322b71fbcce63d6596e66ab5fcdbaa0"
4025 "0d49e93aba8effb4518c2da637f209028401a68f344865b4956b032c69acde51d29177ca"
4026 "3db99fdbf5e74848ed4fa7bdfc2ebb60e2aaa5354770a763e1399ab7a2099762d525fea0"
4027 "37f3e1972c45a477e66db95c9609bb27f862700ef93379930786cf751b";
4028 blink::WebCryptoAlgorithm import_algorithm = CreateRsaHashedImportAlgorithm(
4029 blink::WebCryptoAlgorithmIdRsaOaep, blink::WebCryptoAlgorithmIdSha1);
4030 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
4031 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
4032
4033 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(
4034 HexStringToBytes(kPublicKey2048SpkiDerHex),
4035 HexStringToBytes(kPrivateKey2048Pkcs8DerHex),
4036 import_algorithm,
4037 false,
4038 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt |
4039 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey,
4040 &public_key,
4041 &private_key));
4042
4043 std::vector<uint8> label;
4044 blink::WebCryptoAlgorithm wrapping_algorithm = CreateRsaOaepAlgorithm(label);
4045
4046 const std::string key_hex = "000102030405060708090a0b0c0d0e0f";
4047 const blink::WebCryptoAlgorithm key_algorithm =
4048 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc);
4049
4050 blink::WebCryptoKey key =
4051 ImportSecretKeyFromRaw(HexStringToBytes(key_hex),
4052 key_algorithm,
4053 blink::WebCryptoKeyUsageEncrypt);
4054 ASSERT_FALSE(key.isNull());
4055
4056 std::vector<uint8> wrapped_key;
4057 ASSERT_EQ(Status::Success(),
4058 WrapKey(blink::WebCryptoKeyFormatJwk,
4059 key,
4060 public_key,
4061 wrapping_algorithm,
4062 &wrapped_key));
4063
4064 // Verify that |wrapped_key| can be decrypted and yields a valid JWK object.
4065 // Because |private_key| supports both decrypt and unwrap, this is valid.
4066 std::vector<uint8> decrypted_jwk;
4067 ASSERT_EQ(Status::Success(),
4068 Decrypt(wrapping_algorithm,
4069 private_key,
4070 CryptoData(wrapped_key),
4071 &decrypted_jwk));
4072 EXPECT_TRUE(VerifySecretJwk(
4073 decrypted_jwk, "A128CBC", key_hex, blink::WebCryptoKeyUsageEncrypt));
4074
4075 // Now attempt to unwrap the key, which should also decrypt the data.
4076 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull();
4077 ASSERT_EQ(Status::Success(),
4078 UnwrapKey(blink::WebCryptoKeyFormatJwk,
4079 CryptoData(wrapped_key),
4080 private_key,
4081 wrapping_algorithm,
4082 key_algorithm,
4083 true,
4084 blink::WebCryptoKeyUsageEncrypt,
4085 &unwrapped_key));
4086 ASSERT_FALSE(unwrapped_key.isNull());
4087
4088 std::vector<uint8> raw_key;
4089 ASSERT_EQ(Status::Success(),
4090 ExportKey(blink::WebCryptoKeyFormatRaw, unwrapped_key, &raw_key));
4091 EXPECT_BYTES_EQ_HEX(key_hex, raw_key);
4092 }
4093
3482 } // namespace webcrypto 4094 } // namespace webcrypto
3483 4095
3484 } // namespace content 4096 } // namespace content
OLDNEW
« no previous file with comments | « content/child/webcrypto/shared_crypto.cc ('k') | content/child/webcrypto/webcrypto_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698