Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / components / webcrypto / test / rsa_oaep_unittest.cc
blob343529679dd5bd198e8ec2c64bd1902a37166162
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
3 // found in the LICENSE file.
5 #include "base/logging.h"
6 #include "base/stl_util.h"
7 #include "components/webcrypto/algorithm_dispatch.h"
8 #include "components/webcrypto/crypto_data.h"
9 #include "components/webcrypto/jwk.h"
10 #include "components/webcrypto/status.h"
11 #include "components/webcrypto/test/test_helpers.h"
12 #include "components/webcrypto/webcrypto_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
15 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
17 namespace webcrypto {
19 namespace {
21 // Creates an RSA-OAEP algorithm
22 blink::WebCryptoAlgorithm CreateRsaOaepAlgorithm(
23 const std::vector<uint8_t>& label) {
24 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
25 blink::WebCryptoAlgorithmIdRsaOaep,
26 new blink::WebCryptoRsaOaepParams(
27 !label.empty(), vector_as_array(&label),
28 static_cast<unsigned int>(label.size())));
31 scoped_ptr<base::DictionaryValue> CreatePublicKeyJwkDict() {
32 scoped_ptr<base::DictionaryValue> jwk(new base::DictionaryValue());
33 jwk->SetString("kty", "RSA");
34 jwk->SetString("n",
35 Base64EncodeUrlSafe(HexStringToBytes(kPublicKeyModulusHex)));
36 jwk->SetString("e",
37 Base64EncodeUrlSafe(HexStringToBytes(kPublicKeyExponentHex)));
38 return jwk.Pass();
41 // Import a PKCS#8 private key that uses RSAPrivateKey with the
42 // id-rsaEncryption OID.
43 TEST(WebCryptoRsaOaepTest, ImportPkcs8WithRsaEncryption) {
44 if (!SupportsRsaOaep()) {
45 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
46 return;
49 blink::WebCryptoKey private_key;
50 ASSERT_EQ(Status::Success(),
51 ImportKey(blink::WebCryptoKeyFormatPkcs8,
52 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)),
53 CreateRsaHashedImportAlgorithm(
54 blink::WebCryptoAlgorithmIdRsaOaep,
55 blink::WebCryptoAlgorithmIdSha1),
56 true, blink::WebCryptoKeyUsageDecrypt, &private_key));
59 TEST(WebCryptoRsaOaepTest, ImportPublicJwkWithNoAlg) {
60 if (!SupportsRsaOaep()) {
61 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
62 return;
65 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict());
67 blink::WebCryptoKey public_key;
68 ASSERT_EQ(
69 Status::Success(),
70 ImportKeyJwkFromDict(*jwk.get(), CreateRsaHashedImportAlgorithm(
71 blink::WebCryptoAlgorithmIdRsaOaep,
72 blink::WebCryptoAlgorithmIdSha1),
73 true, blink::WebCryptoKeyUsageEncrypt, &public_key));
76 TEST(WebCryptoRsaOaepTest, ImportPublicJwkWithMatchingAlg) {
77 if (!SupportsRsaOaep()) {
78 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
79 return;
82 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict());
83 jwk->SetString("alg", "RSA-OAEP");
85 blink::WebCryptoKey public_key;
86 ASSERT_EQ(
87 Status::Success(),
88 ImportKeyJwkFromDict(*jwk.get(), CreateRsaHashedImportAlgorithm(
89 blink::WebCryptoAlgorithmIdRsaOaep,
90 blink::WebCryptoAlgorithmIdSha1),
91 true, blink::WebCryptoKeyUsageEncrypt, &public_key));
94 TEST(WebCryptoRsaOaepTest, ImportPublicJwkWithMismatchedAlgFails) {
95 if (!SupportsRsaOaep()) {
96 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
97 return;
100 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict());
101 jwk->SetString("alg", "RSA-OAEP-512");
103 blink::WebCryptoKey public_key;
104 ASSERT_EQ(
105 Status::ErrorJwkAlgorithmInconsistent(),
106 ImportKeyJwkFromDict(*jwk.get(), CreateRsaHashedImportAlgorithm(
107 blink::WebCryptoAlgorithmIdRsaOaep,
108 blink::WebCryptoAlgorithmIdSha1),
109 true, blink::WebCryptoKeyUsageEncrypt, &public_key));
112 TEST(WebCryptoRsaOaepTest, ImportPublicJwkWithMismatchedTypeFails) {
113 if (!SupportsRsaOaep()) {
114 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
115 return;
118 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict());
119 jwk->SetString("kty", "oct");
120 jwk->SetString("alg", "RSA-OAEP");
122 blink::WebCryptoKey public_key;
123 ASSERT_EQ(
124 Status::ErrorJwkUnexpectedKty("RSA"),
125 ImportKeyJwkFromDict(*jwk.get(), CreateRsaHashedImportAlgorithm(
126 blink::WebCryptoAlgorithmIdRsaOaep,
127 blink::WebCryptoAlgorithmIdSha1),
128 true, blink::WebCryptoKeyUsageEncrypt, &public_key));
131 TEST(WebCryptoRsaOaepTest, ExportPublicJwk) {
132 if (!SupportsRsaOaep()) {
133 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
134 return;
137 struct TestData {
138 blink::WebCryptoAlgorithmId hash_alg;
139 const char* expected_jwk_alg;
140 } kTestData[] = {{blink::WebCryptoAlgorithmIdSha1, "RSA-OAEP"},
141 {blink::WebCryptoAlgorithmIdSha256, "RSA-OAEP-256"},
142 {blink::WebCryptoAlgorithmIdSha384, "RSA-OAEP-384"},
143 {blink::WebCryptoAlgorithmIdSha512, "RSA-OAEP-512"}};
144 for (size_t i = 0; i < arraysize(kTestData); ++i) {
145 const TestData& test_data = kTestData[i];
146 SCOPED_TRACE(test_data.expected_jwk_alg);
148 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict());
149 jwk->SetString("alg", test_data.expected_jwk_alg);
151 // Import the key in a known-good format
152 blink::WebCryptoKey public_key;
153 ASSERT_EQ(Status::Success(),
154 ImportKeyJwkFromDict(
155 *jwk.get(),
156 CreateRsaHashedImportAlgorithm(
157 blink::WebCryptoAlgorithmIdRsaOaep, test_data.hash_alg),
158 true, blink::WebCryptoKeyUsageEncrypt, &public_key));
160 // Now export the key as JWK and verify its contents
161 std::vector<uint8_t> jwk_data;
162 ASSERT_EQ(Status::Success(),
163 ExportKey(blink::WebCryptoKeyFormatJwk, public_key, &jwk_data));
164 EXPECT_TRUE(VerifyPublicJwk(jwk_data, test_data.expected_jwk_alg,
165 kPublicKeyModulusHex, kPublicKeyExponentHex,
166 blink::WebCryptoKeyUsageEncrypt));
170 TEST(WebCryptoRsaOaepTest, EncryptDecryptKnownAnswerTest) {
171 if (!SupportsRsaOaep()) {
172 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
173 return;
176 scoped_ptr<base::ListValue> tests;
177 ASSERT_TRUE(ReadJsonTestFileToList("rsa_oaep.json", &tests));
179 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
180 SCOPED_TRACE(test_index);
182 base::DictionaryValue* test = NULL;
183 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
185 blink::WebCryptoAlgorithm digest_algorithm =
186 GetDigestAlgorithm(test, "hash");
187 ASSERT_FALSE(digest_algorithm.isNull());
188 std::vector<uint8_t> public_key_der =
189 GetBytesFromHexString(test, "public_key");
190 std::vector<uint8_t> private_key_der =
191 GetBytesFromHexString(test, "private_key");
192 std::vector<uint8_t> ciphertext = GetBytesFromHexString(test, "ciphertext");
193 std::vector<uint8_t> plaintext = GetBytesFromHexString(test, "plaintext");
194 std::vector<uint8_t> label = GetBytesFromHexString(test, "label");
196 blink::WebCryptoAlgorithm import_algorithm = CreateRsaHashedImportAlgorithm(
197 blink::WebCryptoAlgorithmIdRsaOaep, digest_algorithm.id());
198 blink::WebCryptoKey public_key;
199 blink::WebCryptoKey private_key;
201 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(
202 public_key_der, private_key_der, import_algorithm, false,
203 blink::WebCryptoKeyUsageEncrypt, blink::WebCryptoKeyUsageDecrypt,
204 &public_key, &private_key));
206 blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label);
207 std::vector<uint8_t> decrypted_data;
208 ASSERT_EQ(Status::Success(),
209 Decrypt(op_algorithm, private_key, CryptoData(ciphertext),
210 &decrypted_data));
211 EXPECT_BYTES_EQ(plaintext, decrypted_data);
212 std::vector<uint8_t> encrypted_data;
213 ASSERT_EQ(Status::Success(),
214 Encrypt(op_algorithm, public_key, CryptoData(plaintext),
215 &encrypted_data));
216 std::vector<uint8_t> redecrypted_data;
217 ASSERT_EQ(Status::Success(),
218 Decrypt(op_algorithm, private_key, CryptoData(encrypted_data),
219 &redecrypted_data));
220 EXPECT_BYTES_EQ(plaintext, redecrypted_data);
224 TEST(WebCryptoRsaOaepTest, EncryptWithLargeMessageFails) {
225 if (!SupportsRsaOaep()) {
226 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
227 return;
230 const blink::WebCryptoAlgorithmId kHash = blink::WebCryptoAlgorithmIdSha1;
231 const size_t kHashSize = 20;
233 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict());
235 blink::WebCryptoKey public_key;
236 ASSERT_EQ(Status::Success(),
237 ImportKeyJwkFromDict(
238 *jwk.get(), CreateRsaHashedImportAlgorithm(
239 blink::WebCryptoAlgorithmIdRsaOaep, kHash),
240 true, blink::WebCryptoKeyUsageEncrypt, &public_key));
242 // The maximum size of an encrypted message is:
243 // modulus length
244 // - 1 (leading octet)
245 // - hash size (maskedSeed)
246 // - hash size (lHash portion of maskedDB)
247 // - 1 (at least one octet for the padding string)
248 size_t kMaxMessageSize = (kModulusLengthBits / 8) - 2 - (2 * kHashSize);
250 // The label has no influence on the maximum message size. For simplicity,
251 // use the empty string.
252 std::vector<uint8_t> label;
253 blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label);
255 // Test that a message just before the boundary succeeds.
256 std::string large_message;
257 large_message.resize(kMaxMessageSize - 1, 'A');
259 std::vector<uint8_t> ciphertext;
260 ASSERT_EQ(Status::Success(), Encrypt(op_algorithm, public_key,
261 CryptoData(large_message), &ciphertext));
263 // Test that a message at the boundary succeeds.
264 large_message.resize(kMaxMessageSize, 'A');
265 ciphertext.clear();
267 ASSERT_EQ(Status::Success(), Encrypt(op_algorithm, public_key,
268 CryptoData(large_message), &ciphertext));
270 // Test that a message greater than the largest size fails.
271 large_message.resize(kMaxMessageSize + 1, 'A');
272 ciphertext.clear();
274 ASSERT_EQ(Status::OperationError(),
275 Encrypt(op_algorithm, public_key, CryptoData(large_message),
276 &ciphertext));
279 // Ensures that if the selected hash algorithm for the RSA-OAEP message is too
280 // large, then it is rejected, independent of the actual message to be
281 // encrypted.
282 // For example, a 1024-bit RSA key is too small to accomodate a message that
283 // uses OAEP with SHA-512, since it requires 1040 bits to encode
284 // (2 * hash size + 2 padding bytes).
285 TEST(WebCryptoRsaOaepTest, EncryptWithLargeDigestFails) {
286 if (!SupportsRsaOaep()) {
287 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
288 return;
291 const blink::WebCryptoAlgorithmId kHash = blink::WebCryptoAlgorithmIdSha512;
293 scoped_ptr<base::DictionaryValue> jwk(CreatePublicKeyJwkDict());
295 blink::WebCryptoKey public_key;
296 ASSERT_EQ(Status::Success(),
297 ImportKeyJwkFromDict(
298 *jwk.get(), CreateRsaHashedImportAlgorithm(
299 blink::WebCryptoAlgorithmIdRsaOaep, kHash),
300 true, blink::WebCryptoKeyUsageEncrypt, &public_key));
302 // The label has no influence on the maximum message size. For simplicity,
303 // use the empty string.
304 std::vector<uint8_t> label;
305 blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label);
307 std::string small_message("A");
308 std::vector<uint8_t> ciphertext;
309 // This is an operation error, as the internal consistency checking of the
310 // algorithm parameters is up to the implementation.
311 ASSERT_EQ(Status::OperationError(),
312 Encrypt(op_algorithm, public_key, CryptoData(small_message),
313 &ciphertext));
316 TEST(WebCryptoRsaOaepTest, DecryptWithLargeMessageFails) {
317 if (!SupportsRsaOaep()) {
318 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
319 return;
322 blink::WebCryptoKey private_key;
323 ASSERT_EQ(Status::Success(),
324 ImportKey(blink::WebCryptoKeyFormatPkcs8,
325 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)),
326 CreateRsaHashedImportAlgorithm(
327 blink::WebCryptoAlgorithmIdRsaOaep,
328 blink::WebCryptoAlgorithmIdSha1),
329 true, blink::WebCryptoKeyUsageDecrypt, &private_key));
331 // The label has no influence on the maximum message size. For simplicity,
332 // use the empty string.
333 std::vector<uint8_t> label;
334 blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label);
336 std::string large_dummy_message(kModulusLengthBits / 8, 'A');
337 std::vector<uint8_t> plaintext;
339 ASSERT_EQ(Status::OperationError(),
340 Decrypt(op_algorithm, private_key, CryptoData(large_dummy_message),
341 &plaintext));
344 TEST(WebCryptoRsaOaepTest, WrapUnwrapRawKey) {
345 if (!SupportsRsaOaep()) {
346 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
347 return;
350 blink::WebCryptoAlgorithm import_algorithm = CreateRsaHashedImportAlgorithm(
351 blink::WebCryptoAlgorithmIdRsaOaep, blink::WebCryptoAlgorithmIdSha1);
352 blink::WebCryptoKey public_key;
353 blink::WebCryptoKey private_key;
355 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(
356 HexStringToBytes(kPublicKeySpkiDerHex),
357 HexStringToBytes(kPrivateKeyPkcs8DerHex), import_algorithm, false,
358 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageWrapKey,
359 blink::WebCryptoKeyUsageDecrypt | blink::WebCryptoKeyUsageUnwrapKey,
360 &public_key, &private_key));
362 std::vector<uint8_t> label;
363 blink::WebCryptoAlgorithm wrapping_algorithm = CreateRsaOaepAlgorithm(label);
365 const std::string key_hex = "000102030405060708090A0B0C0D0E0F";
366 const blink::WebCryptoAlgorithm key_algorithm =
367 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc);
369 blink::WebCryptoKey key =
370 ImportSecretKeyFromRaw(HexStringToBytes(key_hex), key_algorithm,
371 blink::WebCryptoKeyUsageEncrypt);
372 ASSERT_FALSE(key.isNull());
374 std::vector<uint8_t> wrapped_key;
375 ASSERT_EQ(Status::Success(),
376 WrapKey(blink::WebCryptoKeyFormatRaw, key, public_key,
377 wrapping_algorithm, &wrapped_key));
379 // Verify that |wrapped_key| can be decrypted and yields the key data.
380 // Because |private_key| supports both decrypt and unwrap, this is valid.
381 std::vector<uint8_t> decrypted_key;
382 ASSERT_EQ(Status::Success(),
383 Decrypt(wrapping_algorithm, private_key, CryptoData(wrapped_key),
384 &decrypted_key));
385 EXPECT_BYTES_EQ_HEX(key_hex, decrypted_key);
387 // Now attempt to unwrap the key, which should also decrypt the data.
388 blink::WebCryptoKey unwrapped_key;
389 ASSERT_EQ(Status::Success(),
390 UnwrapKey(blink::WebCryptoKeyFormatRaw, CryptoData(wrapped_key),
391 private_key, wrapping_algorithm, key_algorithm, true,
392 blink::WebCryptoKeyUsageEncrypt, &unwrapped_key));
393 ASSERT_FALSE(unwrapped_key.isNull());
395 std::vector<uint8_t> raw_key;
396 ASSERT_EQ(Status::Success(),
397 ExportKey(blink::WebCryptoKeyFormatRaw, unwrapped_key, &raw_key));
398 EXPECT_BYTES_EQ_HEX(key_hex, raw_key);
401 TEST(WebCryptoRsaOaepTest, WrapUnwrapJwkSymKey) {
402 if (!SupportsRsaOaep()) {
403 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
404 return;
407 // The public and private portions of a 2048-bit RSA key with the
408 // id-rsaEncryption OID
409 const char kPublicKey2048SpkiDerHex[] =
410 "30820122300d06092a864886f70d01010105000382010f003082010a0282010100c5d8ce"
411 "137a38168c8ab70229cfa5accc640567159750a312ce2e7d54b6e2fdd59b300c6a6c9764"
412 "f8de6f00519cdb90111453d273a967462786480621f9e7cee5b73d63358448e7183a3a68"
413 "e991186359f26aa88fbca5f53e673e502e4c5a2ba5068aeba60c9d0c44d872458d1b1e2f"
414 "7f339f986076d516e93dc750f0b7680b6f5f02bc0d5590495be04c4ae59d34ba17bc5d08"
415 "a93c75cfda2828f4a55b153af912038438276cb4a14f8116ca94db0ea9893652d02fc606"
416 "36f19975e3d79a4d8ea8bfed6f8e0a24b63d243b08ea70a086ad56dd6341d733711c89ca"
417 "749d4a80b3e6ecd2f8e53731eadeac2ea77788ee55d7b4b47c0f2523fbd61b557c16615d"
418 "5d0203010001";
419 const char kPrivateKey2048Pkcs8DerHex[] =
420 "308204bd020100300d06092a864886f70d0101010500048204a7308204a3020100028201"
421 "0100c5d8ce137a38168c8ab70229cfa5accc640567159750a312ce2e7d54b6e2fdd59b30"
422 "0c6a6c9764f8de6f00519cdb90111453d273a967462786480621f9e7cee5b73d63358448"
423 "e7183a3a68e991186359f26aa88fbca5f53e673e502e4c5a2ba5068aeba60c9d0c44d872"
424 "458d1b1e2f7f339f986076d516e93dc750f0b7680b6f5f02bc0d5590495be04c4ae59d34"
425 "ba17bc5d08a93c75cfda2828f4a55b153af912038438276cb4a14f8116ca94db0ea98936"
426 "52d02fc60636f19975e3d79a4d8ea8bfed6f8e0a24b63d243b08ea70a086ad56dd6341d7"
427 "33711c89ca749d4a80b3e6ecd2f8e53731eadeac2ea77788ee55d7b4b47c0f2523fbd61b"
428 "557c16615d5d02030100010282010074b70feb41a0b0fcbc207670400556c9450042ede3"
429 "d4383fb1ce8f3558a6d4641d26dd4c333fa4db842d2b9cf9d2354d3e16ad027a9f682d8c"
430 "f4145a1ad97b9edcd8a41c402bd9d8db10f62f43df854cdccbbb2100834f083f53ed6d42"
431 "b1b729a59072b004a4e945fc027db15e9c121d1251464d320d4774d5732df6b3dbf751f4"
432 "9b19c9db201e19989c883bbaad5333db47f64f6f7a95b8d4936b10d945aa3f794cfaab62"
433 "e7d47686129358914f3b8085f03698a650ab5b8c7e45813f2b0515ec05b6e5195b6a7c2a"
434 "0d36969745f431ded4fd059f6aa361a4649541016d356297362b778e90f077d48815b339"
435 "ec6f43aba345df93e67fcb6c2cb5b4544e9be902818100e9c90abe5f9f32468c5b6d630c"
436 "54a4d7d75e29a72cf792f21e242aac78fd7995c42dfd4ae871d2619ff7096cb05baa78e3"
437 "23ecab338401a8059adf7a0d8be3b21edc9a9c82c5605634a2ec81ec053271721351868a"
438 "4c2e50c689d7cef94e31ff23658af5843366e2b289c5bf81d72756a7b93487dd8770d69c"
439 "1f4e089d6d89f302818100d8a58a727c4e209132afd9933b98c89aca862a01cc0be74133"
440 "bee517909e5c379e526895ac4af11780c1fe91194c777c9670b6423f0f5a32fd7691a622"
441 "113eef4bed2ef863363a335fd55b0e75088c582437237d7f3ed3f0a643950237bc6e6277"
442 "ccd0d0a1b4170aa1047aa7ffa7c8c54be10e8c7327ae2e0885663963817f6f02818100e5"
443 "aed9ba4d71b7502e6748a1ce247ecb7bd10c352d6d9256031cdf3c11a65e44b0b7ca2945"
444 "134671195af84c6b3bb3d10ebf65ae916f38bd5dbc59a0ad1c69b8beaf57cb3a8335f19b"
445 "c7117b576987b48331cd9fd3d1a293436b7bb5e1a35c6560de4b5688ea834367cb0997eb"
446 "b578f59ed4cb724c47dba94d3b484c1876dcd70281807f15bc7d2406007cac2b138a96af"
447 "2d1e00276b84da593132c253fcb73212732dfd25824c2a615bc3d9b7f2c8d2fa542d3562"
448 "b0c7738e61eeff580a6056239fb367ea9e5efe73d4f846033602e90c36a78db6fa8ea792"
449 "0769675ec58e237bd994d189c8045a96f5dd3a4f12547257ce224e3c9af830a4da3c0eab"
450 "9227a0035ae9028180067caea877e0b23090fc689322b71fbcce63d6596e66ab5fcdbaa0"
451 "0d49e93aba8effb4518c2da637f209028401a68f344865b4956b032c69acde51d29177ca"
452 "3db99fdbf5e74848ed4fa7bdfc2ebb60e2aaa5354770a763e1399ab7a2099762d525fea0"
453 "37f3e1972c45a477e66db95c9609bb27f862700ef93379930786cf751b";
454 blink::WebCryptoAlgorithm import_algorithm = CreateRsaHashedImportAlgorithm(
455 blink::WebCryptoAlgorithmIdRsaOaep, blink::WebCryptoAlgorithmIdSha1);
456 blink::WebCryptoKey public_key;
457 blink::WebCryptoKey private_key;
459 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(
460 HexStringToBytes(kPublicKey2048SpkiDerHex),
461 HexStringToBytes(kPrivateKey2048Pkcs8DerHex), import_algorithm, false,
462 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageWrapKey,
463 blink::WebCryptoKeyUsageDecrypt | blink::WebCryptoKeyUsageUnwrapKey,
464 &public_key, &private_key));
466 std::vector<uint8_t> label;
467 blink::WebCryptoAlgorithm wrapping_algorithm = CreateRsaOaepAlgorithm(label);
469 const std::string key_hex = "000102030405060708090a0b0c0d0e0f";
470 const blink::WebCryptoAlgorithm key_algorithm =
471 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc);
473 blink::WebCryptoKey key =
474 ImportSecretKeyFromRaw(HexStringToBytes(key_hex), key_algorithm,
475 blink::WebCryptoKeyUsageEncrypt);
476 ASSERT_FALSE(key.isNull());
478 std::vector<uint8_t> wrapped_key;
479 ASSERT_EQ(Status::Success(),
480 WrapKey(blink::WebCryptoKeyFormatJwk, key, public_key,
481 wrapping_algorithm, &wrapped_key));
483 // Verify that |wrapped_key| can be decrypted and yields a valid JWK object.
484 // Because |private_key| supports both decrypt and unwrap, this is valid.
485 std::vector<uint8_t> decrypted_jwk;
486 ASSERT_EQ(Status::Success(),
487 Decrypt(wrapping_algorithm, private_key, CryptoData(wrapped_key),
488 &decrypted_jwk));
489 EXPECT_TRUE(VerifySecretJwk(decrypted_jwk, "A128CBC", key_hex,
490 blink::WebCryptoKeyUsageEncrypt));
492 // Now attempt to unwrap the key, which should also decrypt the data.
493 blink::WebCryptoKey unwrapped_key;
494 ASSERT_EQ(Status::Success(),
495 UnwrapKey(blink::WebCryptoKeyFormatJwk, CryptoData(wrapped_key),
496 private_key, wrapping_algorithm, key_algorithm, true,
497 blink::WebCryptoKeyUsageEncrypt, &unwrapped_key));
498 ASSERT_FALSE(unwrapped_key.isNull());
500 std::vector<uint8_t> raw_key;
501 ASSERT_EQ(Status::Success(),
502 ExportKey(blink::WebCryptoKeyFormatRaw, unwrapped_key, &raw_key));
503 EXPECT_BYTES_EQ_HEX(key_hex, raw_key);
506 TEST(WebCryptoRsaOaepTest, ImportExportJwkRsaPublicKey) {
507 if (!SupportsRsaOaep()) {
508 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
509 return;
512 struct TestCase {
513 const blink::WebCryptoAlgorithmId hash;
514 const blink::WebCryptoKeyUsageMask usage;
515 const char* const jwk_alg;
517 const TestCase kTests[] = {{blink::WebCryptoAlgorithmIdSha1,
518 blink::WebCryptoKeyUsageEncrypt,
519 "RSA-OAEP"},
520 {blink::WebCryptoAlgorithmIdSha256,
521 blink::WebCryptoKeyUsageEncrypt,
522 "RSA-OAEP-256"},
523 {blink::WebCryptoAlgorithmIdSha384,
524 blink::WebCryptoKeyUsageEncrypt,
525 "RSA-OAEP-384"},
526 {blink::WebCryptoAlgorithmIdSha512,
527 blink::WebCryptoKeyUsageEncrypt,
528 "RSA-OAEP-512"}};
530 for (size_t test_index = 0; test_index < arraysize(kTests); ++test_index) {
531 SCOPED_TRACE(test_index);
532 const TestCase& test = kTests[test_index];
534 const blink::WebCryptoAlgorithm import_algorithm =
535 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaOaep,
536 test.hash);
538 // Import the spki to create a public key
539 blink::WebCryptoKey public_key;
540 ASSERT_EQ(Status::Success(),
541 ImportKey(blink::WebCryptoKeyFormatSpki,
542 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)),
543 import_algorithm, true, test.usage, &public_key));
545 // Export the public key as JWK and verify its contents
546 std::vector<uint8_t> jwk;
547 ASSERT_EQ(Status::Success(),
548 ExportKey(blink::WebCryptoKeyFormatJwk, public_key, &jwk));
549 EXPECT_TRUE(VerifyPublicJwk(jwk, test.jwk_alg, kPublicKeyModulusHex,
550 kPublicKeyExponentHex, test.usage));
552 // Import the JWK back in to create a new key
553 blink::WebCryptoKey public_key2;
554 ASSERT_EQ(Status::Success(),
555 ImportKey(blink::WebCryptoKeyFormatJwk, CryptoData(jwk),
556 import_algorithm, true, test.usage, &public_key2));
557 ASSERT_TRUE(public_key2.handle());
558 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key2.type());
559 EXPECT_TRUE(public_key2.extractable());
560 EXPECT_EQ(import_algorithm.id(), public_key2.algorithm().id());
562 // TODO(eroman): Export the SPKI and verify matches.
566 } // namespace
568 } // namespace webcrypto