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/algorithms/test_helpers.h"
9 #include "components/webcrypto/crypto_data.h"
10 #include "components/webcrypto/jwk.h"
11 #include "components/webcrypto/status.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"
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");
35 Base64EncodeUrlSafe(HexStringToBytes(kPublicKeyModulusHex
)));
37 Base64EncodeUrlSafe(HexStringToBytes(kPublicKeyExponentHex
)));
41 class WebCryptoRsaOaepTest
: public WebCryptoTestBase
{};
43 // Import a PKCS#8 private key that uses RSAPrivateKey with the
44 // id-rsaEncryption OID.
45 TEST_F(WebCryptoRsaOaepTest
, ImportPkcs8WithRsaEncryption
) {
46 blink::WebCryptoKey private_key
;
47 ASSERT_EQ(Status::Success(),
48 ImportKey(blink::WebCryptoKeyFormatPkcs8
,
49 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex
)),
50 CreateRsaHashedImportAlgorithm(
51 blink::WebCryptoAlgorithmIdRsaOaep
,
52 blink::WebCryptoAlgorithmIdSha1
),
53 true, blink::WebCryptoKeyUsageDecrypt
, &private_key
));
56 TEST_F(WebCryptoRsaOaepTest
, ImportPublicJwkWithNoAlg
) {
57 scoped_ptr
<base::DictionaryValue
> jwk(CreatePublicKeyJwkDict());
59 blink::WebCryptoKey public_key
;
62 ImportKeyJwkFromDict(*jwk
.get(), CreateRsaHashedImportAlgorithm(
63 blink::WebCryptoAlgorithmIdRsaOaep
,
64 blink::WebCryptoAlgorithmIdSha1
),
65 true, blink::WebCryptoKeyUsageEncrypt
, &public_key
));
68 TEST_F(WebCryptoRsaOaepTest
, ImportPublicJwkWithMatchingAlg
) {
69 scoped_ptr
<base::DictionaryValue
> jwk(CreatePublicKeyJwkDict());
70 jwk
->SetString("alg", "RSA-OAEP");
72 blink::WebCryptoKey public_key
;
75 ImportKeyJwkFromDict(*jwk
.get(), CreateRsaHashedImportAlgorithm(
76 blink::WebCryptoAlgorithmIdRsaOaep
,
77 blink::WebCryptoAlgorithmIdSha1
),
78 true, blink::WebCryptoKeyUsageEncrypt
, &public_key
));
81 TEST_F(WebCryptoRsaOaepTest
, ImportPublicJwkWithMismatchedAlgFails
) {
82 scoped_ptr
<base::DictionaryValue
> jwk(CreatePublicKeyJwkDict());
83 jwk
->SetString("alg", "RSA-OAEP-512");
85 blink::WebCryptoKey public_key
;
87 Status::ErrorJwkAlgorithmInconsistent(),
88 ImportKeyJwkFromDict(*jwk
.get(), CreateRsaHashedImportAlgorithm(
89 blink::WebCryptoAlgorithmIdRsaOaep
,
90 blink::WebCryptoAlgorithmIdSha1
),
91 true, blink::WebCryptoKeyUsageEncrypt
, &public_key
));
94 TEST_F(WebCryptoRsaOaepTest
, ImportPublicJwkWithMismatchedTypeFails
) {
95 scoped_ptr
<base::DictionaryValue
> jwk(CreatePublicKeyJwkDict());
96 jwk
->SetString("kty", "oct");
97 jwk
->SetString("alg", "RSA-OAEP");
99 blink::WebCryptoKey public_key
;
101 Status::ErrorJwkUnexpectedKty("RSA"),
102 ImportKeyJwkFromDict(*jwk
.get(), CreateRsaHashedImportAlgorithm(
103 blink::WebCryptoAlgorithmIdRsaOaep
,
104 blink::WebCryptoAlgorithmIdSha1
),
105 true, blink::WebCryptoKeyUsageEncrypt
, &public_key
));
108 TEST_F(WebCryptoRsaOaepTest
, ExportPublicJwk
) {
110 blink::WebCryptoAlgorithmId hash_alg
;
111 const char* expected_jwk_alg
;
112 } kTestData
[] = {{blink::WebCryptoAlgorithmIdSha1
, "RSA-OAEP"},
113 {blink::WebCryptoAlgorithmIdSha256
, "RSA-OAEP-256"},
114 {blink::WebCryptoAlgorithmIdSha384
, "RSA-OAEP-384"},
115 {blink::WebCryptoAlgorithmIdSha512
, "RSA-OAEP-512"}};
116 for (size_t i
= 0; i
< arraysize(kTestData
); ++i
) {
117 const TestData
& test_data
= kTestData
[i
];
118 SCOPED_TRACE(test_data
.expected_jwk_alg
);
120 scoped_ptr
<base::DictionaryValue
> jwk(CreatePublicKeyJwkDict());
121 jwk
->SetString("alg", test_data
.expected_jwk_alg
);
123 // Import the key in a known-good format
124 blink::WebCryptoKey public_key
;
125 ASSERT_EQ(Status::Success(),
126 ImportKeyJwkFromDict(
128 CreateRsaHashedImportAlgorithm(
129 blink::WebCryptoAlgorithmIdRsaOaep
, test_data
.hash_alg
),
130 true, blink::WebCryptoKeyUsageEncrypt
, &public_key
));
132 // Now export the key as JWK and verify its contents
133 std::vector
<uint8_t> jwk_data
;
134 ASSERT_EQ(Status::Success(),
135 ExportKey(blink::WebCryptoKeyFormatJwk
, public_key
, &jwk_data
));
136 EXPECT_TRUE(VerifyPublicJwk(jwk_data
, test_data
.expected_jwk_alg
,
137 kPublicKeyModulusHex
, kPublicKeyExponentHex
,
138 blink::WebCryptoKeyUsageEncrypt
));
142 TEST_F(WebCryptoRsaOaepTest
, EncryptDecryptKnownAnswerTest
) {
143 scoped_ptr
<base::ListValue
> tests
;
144 ASSERT_TRUE(ReadJsonTestFileToList("rsa_oaep.json", &tests
));
146 for (size_t test_index
= 0; test_index
< tests
->GetSize(); ++test_index
) {
147 SCOPED_TRACE(test_index
);
149 base::DictionaryValue
* test
= NULL
;
150 ASSERT_TRUE(tests
->GetDictionary(test_index
, &test
));
152 blink::WebCryptoAlgorithm digest_algorithm
=
153 GetDigestAlgorithm(test
, "hash");
154 ASSERT_FALSE(digest_algorithm
.isNull());
155 std::vector
<uint8_t> public_key_der
=
156 GetBytesFromHexString(test
, "public_key");
157 std::vector
<uint8_t> private_key_der
=
158 GetBytesFromHexString(test
, "private_key");
159 std::vector
<uint8_t> ciphertext
= GetBytesFromHexString(test
, "ciphertext");
160 std::vector
<uint8_t> plaintext
= GetBytesFromHexString(test
, "plaintext");
161 std::vector
<uint8_t> label
= GetBytesFromHexString(test
, "label");
163 blink::WebCryptoAlgorithm import_algorithm
= CreateRsaHashedImportAlgorithm(
164 blink::WebCryptoAlgorithmIdRsaOaep
, digest_algorithm
.id());
165 blink::WebCryptoKey public_key
;
166 blink::WebCryptoKey private_key
;
168 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(
169 public_key_der
, private_key_der
, import_algorithm
, false,
170 blink::WebCryptoKeyUsageEncrypt
, blink::WebCryptoKeyUsageDecrypt
,
171 &public_key
, &private_key
));
173 blink::WebCryptoAlgorithm op_algorithm
= CreateRsaOaepAlgorithm(label
);
174 std::vector
<uint8_t> decrypted_data
;
175 ASSERT_EQ(Status::Success(),
176 Decrypt(op_algorithm
, private_key
, CryptoData(ciphertext
),
178 EXPECT_BYTES_EQ(plaintext
, decrypted_data
);
179 std::vector
<uint8_t> encrypted_data
;
180 ASSERT_EQ(Status::Success(),
181 Encrypt(op_algorithm
, public_key
, CryptoData(plaintext
),
183 std::vector
<uint8_t> redecrypted_data
;
184 ASSERT_EQ(Status::Success(),
185 Decrypt(op_algorithm
, private_key
, CryptoData(encrypted_data
),
187 EXPECT_BYTES_EQ(plaintext
, redecrypted_data
);
191 TEST_F(WebCryptoRsaOaepTest
, EncryptWithLargeMessageFails
) {
192 const blink::WebCryptoAlgorithmId kHash
= blink::WebCryptoAlgorithmIdSha1
;
193 const size_t kHashSize
= 20;
195 scoped_ptr
<base::DictionaryValue
> jwk(CreatePublicKeyJwkDict());
197 blink::WebCryptoKey public_key
;
198 ASSERT_EQ(Status::Success(),
199 ImportKeyJwkFromDict(
200 *jwk
.get(), CreateRsaHashedImportAlgorithm(
201 blink::WebCryptoAlgorithmIdRsaOaep
, kHash
),
202 true, blink::WebCryptoKeyUsageEncrypt
, &public_key
));
204 // The maximum size of an encrypted message is:
206 // - 1 (leading octet)
207 // - hash size (maskedSeed)
208 // - hash size (lHash portion of maskedDB)
209 // - 1 (at least one octet for the padding string)
210 size_t kMaxMessageSize
= (kModulusLengthBits
/ 8) - 2 - (2 * kHashSize
);
212 // The label has no influence on the maximum message size. For simplicity,
213 // use the empty string.
214 std::vector
<uint8_t> label
;
215 blink::WebCryptoAlgorithm op_algorithm
= CreateRsaOaepAlgorithm(label
);
217 // Test that a message just before the boundary succeeds.
218 std::string large_message
;
219 large_message
.resize(kMaxMessageSize
- 1, 'A');
221 std::vector
<uint8_t> ciphertext
;
222 ASSERT_EQ(Status::Success(), Encrypt(op_algorithm
, public_key
,
223 CryptoData(large_message
), &ciphertext
));
225 // Test that a message at the boundary succeeds.
226 large_message
.resize(kMaxMessageSize
, 'A');
229 ASSERT_EQ(Status::Success(), Encrypt(op_algorithm
, public_key
,
230 CryptoData(large_message
), &ciphertext
));
232 // Test that a message greater than the largest size fails.
233 large_message
.resize(kMaxMessageSize
+ 1, 'A');
236 ASSERT_EQ(Status::OperationError(),
237 Encrypt(op_algorithm
, public_key
, CryptoData(large_message
),
241 // Ensures that if the selected hash algorithm for the RSA-OAEP message is too
242 // large, then it is rejected, independent of the actual message to be
244 // For example, a 1024-bit RSA key is too small to accomodate a message that
245 // uses OAEP with SHA-512, since it requires 1040 bits to encode
246 // (2 * hash size + 2 padding bytes).
247 TEST_F(WebCryptoRsaOaepTest
, EncryptWithLargeDigestFails
) {
248 const blink::WebCryptoAlgorithmId kHash
= blink::WebCryptoAlgorithmIdSha512
;
250 scoped_ptr
<base::DictionaryValue
> jwk(CreatePublicKeyJwkDict());
252 blink::WebCryptoKey public_key
;
253 ASSERT_EQ(Status::Success(),
254 ImportKeyJwkFromDict(
255 *jwk
.get(), CreateRsaHashedImportAlgorithm(
256 blink::WebCryptoAlgorithmIdRsaOaep
, kHash
),
257 true, blink::WebCryptoKeyUsageEncrypt
, &public_key
));
259 // The label has no influence on the maximum message size. For simplicity,
260 // use the empty string.
261 std::vector
<uint8_t> label
;
262 blink::WebCryptoAlgorithm op_algorithm
= CreateRsaOaepAlgorithm(label
);
264 std::string
small_message("A");
265 std::vector
<uint8_t> ciphertext
;
266 // This is an operation error, as the internal consistency checking of the
267 // algorithm parameters is up to the implementation.
268 ASSERT_EQ(Status::OperationError(),
269 Encrypt(op_algorithm
, public_key
, CryptoData(small_message
),
273 TEST_F(WebCryptoRsaOaepTest
, DecryptWithLargeMessageFails
) {
274 blink::WebCryptoKey private_key
;
275 ASSERT_EQ(Status::Success(),
276 ImportKey(blink::WebCryptoKeyFormatPkcs8
,
277 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex
)),
278 CreateRsaHashedImportAlgorithm(
279 blink::WebCryptoAlgorithmIdRsaOaep
,
280 blink::WebCryptoAlgorithmIdSha1
),
281 true, blink::WebCryptoKeyUsageDecrypt
, &private_key
));
283 // The label has no influence on the maximum message size. For simplicity,
284 // use the empty string.
285 std::vector
<uint8_t> label
;
286 blink::WebCryptoAlgorithm op_algorithm
= CreateRsaOaepAlgorithm(label
);
288 std::string
large_dummy_message(kModulusLengthBits
/ 8, 'A');
289 std::vector
<uint8_t> plaintext
;
291 ASSERT_EQ(Status::OperationError(),
292 Decrypt(op_algorithm
, private_key
, CryptoData(large_dummy_message
),
296 TEST_F(WebCryptoRsaOaepTest
, WrapUnwrapRawKey
) {
297 blink::WebCryptoAlgorithm import_algorithm
= CreateRsaHashedImportAlgorithm(
298 blink::WebCryptoAlgorithmIdRsaOaep
, blink::WebCryptoAlgorithmIdSha1
);
299 blink::WebCryptoKey public_key
;
300 blink::WebCryptoKey private_key
;
302 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(
303 HexStringToBytes(kPublicKeySpkiDerHex
),
304 HexStringToBytes(kPrivateKeyPkcs8DerHex
), import_algorithm
, false,
305 blink::WebCryptoKeyUsageEncrypt
| blink::WebCryptoKeyUsageWrapKey
,
306 blink::WebCryptoKeyUsageDecrypt
| blink::WebCryptoKeyUsageUnwrapKey
,
307 &public_key
, &private_key
));
309 std::vector
<uint8_t> label
;
310 blink::WebCryptoAlgorithm wrapping_algorithm
= CreateRsaOaepAlgorithm(label
);
312 const std::string key_hex
= "000102030405060708090A0B0C0D0E0F";
313 const blink::WebCryptoAlgorithm key_algorithm
=
314 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc
);
316 blink::WebCryptoKey key
=
317 ImportSecretKeyFromRaw(HexStringToBytes(key_hex
), key_algorithm
,
318 blink::WebCryptoKeyUsageEncrypt
);
319 ASSERT_FALSE(key
.isNull());
321 std::vector
<uint8_t> wrapped_key
;
322 ASSERT_EQ(Status::Success(),
323 WrapKey(blink::WebCryptoKeyFormatRaw
, key
, public_key
,
324 wrapping_algorithm
, &wrapped_key
));
326 // Verify that |wrapped_key| can be decrypted and yields the key data.
327 // Because |private_key| supports both decrypt and unwrap, this is valid.
328 std::vector
<uint8_t> decrypted_key
;
329 ASSERT_EQ(Status::Success(),
330 Decrypt(wrapping_algorithm
, private_key
, CryptoData(wrapped_key
),
332 EXPECT_BYTES_EQ_HEX(key_hex
, decrypted_key
);
334 // Now attempt to unwrap the key, which should also decrypt the data.
335 blink::WebCryptoKey unwrapped_key
;
336 ASSERT_EQ(Status::Success(),
337 UnwrapKey(blink::WebCryptoKeyFormatRaw
, CryptoData(wrapped_key
),
338 private_key
, wrapping_algorithm
, key_algorithm
, true,
339 blink::WebCryptoKeyUsageEncrypt
, &unwrapped_key
));
340 ASSERT_FALSE(unwrapped_key
.isNull());
342 std::vector
<uint8_t> raw_key
;
343 ASSERT_EQ(Status::Success(),
344 ExportKey(blink::WebCryptoKeyFormatRaw
, unwrapped_key
, &raw_key
));
345 EXPECT_BYTES_EQ_HEX(key_hex
, raw_key
);
348 TEST_F(WebCryptoRsaOaepTest
, WrapUnwrapJwkSymKey
) {
349 // The public and private portions of a 2048-bit RSA key with the
350 // id-rsaEncryption OID
351 const char kPublicKey2048SpkiDerHex
[] =
352 "30820122300d06092a864886f70d01010105000382010f003082010a0282010100c5d8ce"
353 "137a38168c8ab70229cfa5accc640567159750a312ce2e7d54b6e2fdd59b300c6a6c9764"
354 "f8de6f00519cdb90111453d273a967462786480621f9e7cee5b73d63358448e7183a3a68"
355 "e991186359f26aa88fbca5f53e673e502e4c5a2ba5068aeba60c9d0c44d872458d1b1e2f"
356 "7f339f986076d516e93dc750f0b7680b6f5f02bc0d5590495be04c4ae59d34ba17bc5d08"
357 "a93c75cfda2828f4a55b153af912038438276cb4a14f8116ca94db0ea9893652d02fc606"
358 "36f19975e3d79a4d8ea8bfed6f8e0a24b63d243b08ea70a086ad56dd6341d733711c89ca"
359 "749d4a80b3e6ecd2f8e53731eadeac2ea77788ee55d7b4b47c0f2523fbd61b557c16615d"
361 const char kPrivateKey2048Pkcs8DerHex
[] =
362 "308204bd020100300d06092a864886f70d0101010500048204a7308204a3020100028201"
363 "0100c5d8ce137a38168c8ab70229cfa5accc640567159750a312ce2e7d54b6e2fdd59b30"
364 "0c6a6c9764f8de6f00519cdb90111453d273a967462786480621f9e7cee5b73d63358448"
365 "e7183a3a68e991186359f26aa88fbca5f53e673e502e4c5a2ba5068aeba60c9d0c44d872"
366 "458d1b1e2f7f339f986076d516e93dc750f0b7680b6f5f02bc0d5590495be04c4ae59d34"
367 "ba17bc5d08a93c75cfda2828f4a55b153af912038438276cb4a14f8116ca94db0ea98936"
368 "52d02fc60636f19975e3d79a4d8ea8bfed6f8e0a24b63d243b08ea70a086ad56dd6341d7"
369 "33711c89ca749d4a80b3e6ecd2f8e53731eadeac2ea77788ee55d7b4b47c0f2523fbd61b"
370 "557c16615d5d02030100010282010074b70feb41a0b0fcbc207670400556c9450042ede3"
371 "d4383fb1ce8f3558a6d4641d26dd4c333fa4db842d2b9cf9d2354d3e16ad027a9f682d8c"
372 "f4145a1ad97b9edcd8a41c402bd9d8db10f62f43df854cdccbbb2100834f083f53ed6d42"
373 "b1b729a59072b004a4e945fc027db15e9c121d1251464d320d4774d5732df6b3dbf751f4"
374 "9b19c9db201e19989c883bbaad5333db47f64f6f7a95b8d4936b10d945aa3f794cfaab62"
375 "e7d47686129358914f3b8085f03698a650ab5b8c7e45813f2b0515ec05b6e5195b6a7c2a"
376 "0d36969745f431ded4fd059f6aa361a4649541016d356297362b778e90f077d48815b339"
377 "ec6f43aba345df93e67fcb6c2cb5b4544e9be902818100e9c90abe5f9f32468c5b6d630c"
378 "54a4d7d75e29a72cf792f21e242aac78fd7995c42dfd4ae871d2619ff7096cb05baa78e3"
379 "23ecab338401a8059adf7a0d8be3b21edc9a9c82c5605634a2ec81ec053271721351868a"
380 "4c2e50c689d7cef94e31ff23658af5843366e2b289c5bf81d72756a7b93487dd8770d69c"
381 "1f4e089d6d89f302818100d8a58a727c4e209132afd9933b98c89aca862a01cc0be74133"
382 "bee517909e5c379e526895ac4af11780c1fe91194c777c9670b6423f0f5a32fd7691a622"
383 "113eef4bed2ef863363a335fd55b0e75088c582437237d7f3ed3f0a643950237bc6e6277"
384 "ccd0d0a1b4170aa1047aa7ffa7c8c54be10e8c7327ae2e0885663963817f6f02818100e5"
385 "aed9ba4d71b7502e6748a1ce247ecb7bd10c352d6d9256031cdf3c11a65e44b0b7ca2945"
386 "134671195af84c6b3bb3d10ebf65ae916f38bd5dbc59a0ad1c69b8beaf57cb3a8335f19b"
387 "c7117b576987b48331cd9fd3d1a293436b7bb5e1a35c6560de4b5688ea834367cb0997eb"
388 "b578f59ed4cb724c47dba94d3b484c1876dcd70281807f15bc7d2406007cac2b138a96af"
389 "2d1e00276b84da593132c253fcb73212732dfd25824c2a615bc3d9b7f2c8d2fa542d3562"
390 "b0c7738e61eeff580a6056239fb367ea9e5efe73d4f846033602e90c36a78db6fa8ea792"
391 "0769675ec58e237bd994d189c8045a96f5dd3a4f12547257ce224e3c9af830a4da3c0eab"
392 "9227a0035ae9028180067caea877e0b23090fc689322b71fbcce63d6596e66ab5fcdbaa0"
393 "0d49e93aba8effb4518c2da637f209028401a68f344865b4956b032c69acde51d29177ca"
394 "3db99fdbf5e74848ed4fa7bdfc2ebb60e2aaa5354770a763e1399ab7a2099762d525fea0"
395 "37f3e1972c45a477e66db95c9609bb27f862700ef93379930786cf751b";
396 blink::WebCryptoAlgorithm import_algorithm
= CreateRsaHashedImportAlgorithm(
397 blink::WebCryptoAlgorithmIdRsaOaep
, blink::WebCryptoAlgorithmIdSha1
);
398 blink::WebCryptoKey public_key
;
399 blink::WebCryptoKey private_key
;
401 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(
402 HexStringToBytes(kPublicKey2048SpkiDerHex
),
403 HexStringToBytes(kPrivateKey2048Pkcs8DerHex
), import_algorithm
, false,
404 blink::WebCryptoKeyUsageEncrypt
| blink::WebCryptoKeyUsageWrapKey
,
405 blink::WebCryptoKeyUsageDecrypt
| blink::WebCryptoKeyUsageUnwrapKey
,
406 &public_key
, &private_key
));
408 std::vector
<uint8_t> label
;
409 blink::WebCryptoAlgorithm wrapping_algorithm
= CreateRsaOaepAlgorithm(label
);
411 const std::string key_hex
= "000102030405060708090a0b0c0d0e0f";
412 const blink::WebCryptoAlgorithm key_algorithm
=
413 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc
);
415 blink::WebCryptoKey key
=
416 ImportSecretKeyFromRaw(HexStringToBytes(key_hex
), key_algorithm
,
417 blink::WebCryptoKeyUsageEncrypt
);
418 ASSERT_FALSE(key
.isNull());
420 std::vector
<uint8_t> wrapped_key
;
421 ASSERT_EQ(Status::Success(),
422 WrapKey(blink::WebCryptoKeyFormatJwk
, key
, public_key
,
423 wrapping_algorithm
, &wrapped_key
));
425 // Verify that |wrapped_key| can be decrypted and yields a valid JWK object.
426 // Because |private_key| supports both decrypt and unwrap, this is valid.
427 std::vector
<uint8_t> decrypted_jwk
;
428 ASSERT_EQ(Status::Success(),
429 Decrypt(wrapping_algorithm
, private_key
, CryptoData(wrapped_key
),
431 EXPECT_TRUE(VerifySecretJwk(decrypted_jwk
, "A128CBC", key_hex
,
432 blink::WebCryptoKeyUsageEncrypt
));
434 // Now attempt to unwrap the key, which should also decrypt the data.
435 blink::WebCryptoKey unwrapped_key
;
436 ASSERT_EQ(Status::Success(),
437 UnwrapKey(blink::WebCryptoKeyFormatJwk
, CryptoData(wrapped_key
),
438 private_key
, wrapping_algorithm
, key_algorithm
, true,
439 blink::WebCryptoKeyUsageEncrypt
, &unwrapped_key
));
440 ASSERT_FALSE(unwrapped_key
.isNull());
442 std::vector
<uint8_t> raw_key
;
443 ASSERT_EQ(Status::Success(),
444 ExportKey(blink::WebCryptoKeyFormatRaw
, unwrapped_key
, &raw_key
));
445 EXPECT_BYTES_EQ_HEX(key_hex
, raw_key
);
448 TEST_F(WebCryptoRsaOaepTest
, ImportExportJwkRsaPublicKey
) {
450 const blink::WebCryptoAlgorithmId hash
;
451 const blink::WebCryptoKeyUsageMask usage
;
452 const char* const jwk_alg
;
454 const TestCase kTests
[] = {{blink::WebCryptoAlgorithmIdSha1
,
455 blink::WebCryptoKeyUsageEncrypt
,
457 {blink::WebCryptoAlgorithmIdSha256
,
458 blink::WebCryptoKeyUsageEncrypt
,
460 {blink::WebCryptoAlgorithmIdSha384
,
461 blink::WebCryptoKeyUsageEncrypt
,
463 {blink::WebCryptoAlgorithmIdSha512
,
464 blink::WebCryptoKeyUsageEncrypt
,
467 for (size_t test_index
= 0; test_index
< arraysize(kTests
); ++test_index
) {
468 SCOPED_TRACE(test_index
);
469 const TestCase
& test
= kTests
[test_index
];
471 const blink::WebCryptoAlgorithm import_algorithm
=
472 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaOaep
,
475 // Import the spki to create a public key
476 blink::WebCryptoKey public_key
;
477 ASSERT_EQ(Status::Success(),
478 ImportKey(blink::WebCryptoKeyFormatSpki
,
479 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex
)),
480 import_algorithm
, true, test
.usage
, &public_key
));
482 // Export the public key as JWK and verify its contents
483 std::vector
<uint8_t> jwk
;
484 ASSERT_EQ(Status::Success(),
485 ExportKey(blink::WebCryptoKeyFormatJwk
, public_key
, &jwk
));
486 EXPECT_TRUE(VerifyPublicJwk(jwk
, test
.jwk_alg
, kPublicKeyModulusHex
,
487 kPublicKeyExponentHex
, test
.usage
));
489 // Import the JWK back in to create a new key
490 blink::WebCryptoKey public_key2
;
491 ASSERT_EQ(Status::Success(),
492 ImportKey(blink::WebCryptoKeyFormatJwk
, CryptoData(jwk
),
493 import_algorithm
, true, test
.usage
, &public_key2
));
494 ASSERT_TRUE(public_key2
.handle());
495 EXPECT_EQ(blink::WebCryptoKeyTypePublic
, public_key2
.type());
496 EXPECT_TRUE(public_key2
.extractable());
497 EXPECT_EQ(import_algorithm
.id(), public_key2
.algorithm().id());
499 // TODO(eroman): Export the SPKI and verify matches.
505 } // namespace webcrypto