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/stl_util.h"
6 #include "components/webcrypto/algorithm_dispatch.h"
7 #include "components/webcrypto/crypto_data.h"
8 #include "components/webcrypto/status.h"
9 #include "components/webcrypto/test/test_helpers.h"
10 #include "components/webcrypto/webcrypto_util.h"
11 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
12 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
18 blink::WebCryptoAlgorithm
CreateAesKwKeyGenAlgorithm(
19 unsigned short key_length_bits
) {
20 return CreateAesKeyGenAlgorithm(blink::WebCryptoAlgorithmIdAesKw
,
24 TEST(WebCryptoAesKwTest
, GenerateKeyBadLength
) {
25 const unsigned short kKeyLen
[] = {0, 127, 257};
26 blink::WebCryptoKey key
;
27 for (size_t i
= 0; i
< arraysize(kKeyLen
); ++i
) {
29 EXPECT_EQ(Status::ErrorGenerateAesKeyLength(),
30 GenerateSecretKey(CreateAesKwKeyGenAlgorithm(kKeyLen
[i
]), true,
31 blink::WebCryptoKeyUsageWrapKey
, &key
));
35 TEST(WebCryptoAesKwTest
, GenerateKeyEmptyUsage
) {
36 blink::WebCryptoKey key
;
37 EXPECT_EQ(Status::ErrorCreateKeyEmptyUsages(),
38 GenerateSecretKey(CreateAesKwKeyGenAlgorithm(256), true, 0, &key
));
41 TEST(WebCryptoAesKwTest
, ImportKeyEmptyUsage
) {
42 blink::WebCryptoKey key
;
43 EXPECT_EQ(Status::ErrorCreateKeyEmptyUsages(),
44 ImportKey(blink::WebCryptoKeyFormatRaw
,
45 CryptoData(std::vector
<uint8_t>(16)),
46 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw
), true,
50 TEST(WebCryptoAesKwTest
, ImportKeyJwkKeyOpsWrapUnwrap
) {
51 blink::WebCryptoKey key
;
52 base::DictionaryValue dict
;
53 dict
.SetString("kty", "oct");
54 dict
.SetString("k", "GADWrMRHwQfoNaXU5fZvTg");
55 base::ListValue
* key_ops
= new base::ListValue
;
56 dict
.Set("key_ops", key_ops
); // Takes ownership.
58 key_ops
->AppendString("wrapKey");
60 EXPECT_EQ(Status::Success(),
62 dict
, CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw
), false,
63 blink::WebCryptoKeyUsageWrapKey
, &key
));
65 EXPECT_EQ(blink::WebCryptoKeyUsageWrapKey
, key
.usages());
67 key_ops
->AppendString("unwrapKey");
69 EXPECT_EQ(Status::Success(),
71 dict
, CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw
), false,
72 blink::WebCryptoKeyUsageUnwrapKey
, &key
));
74 EXPECT_EQ(blink::WebCryptoKeyUsageUnwrapKey
, key
.usages());
77 TEST(WebCryptoAesKwTest
, ImportExportJwk
) {
78 const blink::WebCryptoAlgorithm algorithm
=
79 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw
);
82 ImportExportJwkSymmetricKey(
84 blink::WebCryptoKeyUsageWrapKey
| blink::WebCryptoKeyUsageUnwrapKey
,
88 ImportExportJwkSymmetricKey(
90 blink::WebCryptoKeyUsageWrapKey
| blink::WebCryptoKeyUsageUnwrapKey
,
94 TEST(WebCryptoAesKwTest
, AesKwKeyImport
) {
95 blink::WebCryptoKey key
;
96 blink::WebCryptoAlgorithm algorithm
=
97 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw
);
99 // Import a 128-bit Key Encryption Key (KEK)
100 std::string key_raw_hex_in
= "025a8cf3f08b4f6c5f33bbc76a471939";
101 ASSERT_EQ(Status::Success(),
102 ImportKey(blink::WebCryptoKeyFormatRaw
,
103 CryptoData(HexStringToBytes(key_raw_hex_in
)), algorithm
,
104 true, blink::WebCryptoKeyUsageWrapKey
, &key
));
105 std::vector
<uint8_t> key_raw_out
;
106 EXPECT_EQ(Status::Success(),
107 ExportKey(blink::WebCryptoKeyFormatRaw
, key
, &key_raw_out
));
108 EXPECT_BYTES_EQ_HEX(key_raw_hex_in
, key_raw_out
);
110 // Import a 192-bit KEK
111 key_raw_hex_in
= "c0192c6466b2370decbb62b2cfef4384544ffeb4d2fbc103";
112 ASSERT_EQ(Status::ErrorAes192BitUnsupported(),
113 ImportKey(blink::WebCryptoKeyFormatRaw
,
114 CryptoData(HexStringToBytes(key_raw_hex_in
)), algorithm
,
115 true, blink::WebCryptoKeyUsageWrapKey
, &key
));
117 // Import a 256-bit Key Encryption Key (KEK)
119 "e11fe66380d90fa9ebefb74e0478e78f95664d0c67ca20ce4a0b5842863ac46f";
120 ASSERT_EQ(Status::Success(),
121 ImportKey(blink::WebCryptoKeyFormatRaw
,
122 CryptoData(HexStringToBytes(key_raw_hex_in
)), algorithm
,
123 true, blink::WebCryptoKeyUsageWrapKey
, &key
));
124 EXPECT_EQ(Status::Success(),
125 ExportKey(blink::WebCryptoKeyFormatRaw
, key
, &key_raw_out
));
126 EXPECT_BYTES_EQ_HEX(key_raw_hex_in
, key_raw_out
);
128 // Fail import of 0 length key
130 Status::ErrorImportAesKeyLength(),
131 ImportKey(blink::WebCryptoKeyFormatRaw
, CryptoData(HexStringToBytes("")),
132 algorithm
, true, blink::WebCryptoKeyUsageWrapKey
, &key
));
134 // Fail import of 124-bit KEK
135 key_raw_hex_in
= "3e4566a2bdaa10cb68134fa66c15ddb";
136 EXPECT_EQ(Status::ErrorImportAesKeyLength(),
137 ImportKey(blink::WebCryptoKeyFormatRaw
,
138 CryptoData(HexStringToBytes(key_raw_hex_in
)), algorithm
,
139 true, blink::WebCryptoKeyUsageWrapKey
, &key
));
141 // Fail import of 200-bit KEK
142 key_raw_hex_in
= "0a1d88608a5ad9fec64f1ada269ebab4baa2feeb8d95638c0e";
143 EXPECT_EQ(Status::ErrorImportAesKeyLength(),
144 ImportKey(blink::WebCryptoKeyFormatRaw
,
145 CryptoData(HexStringToBytes(key_raw_hex_in
)), algorithm
,
146 true, blink::WebCryptoKeyUsageWrapKey
, &key
));
148 // Fail import of 260-bit KEK
150 "72d4e475ff34215416c9ad9c8281247a4d730c5f275ac23f376e73e3bce8d7d5a";
151 EXPECT_EQ(Status::ErrorImportAesKeyLength(),
152 ImportKey(blink::WebCryptoKeyFormatRaw
,
153 CryptoData(HexStringToBytes(key_raw_hex_in
)), algorithm
,
154 true, blink::WebCryptoKeyUsageWrapKey
, &key
));
157 TEST(WebCryptoAesKwTest
, UnwrapFailures
) {
158 // This test exercises the code path common to all unwrap operations.
159 scoped_ptr
<base::ListValue
> tests
;
160 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests
));
161 base::DictionaryValue
* test
;
162 ASSERT_TRUE(tests
->GetDictionary(0, &test
));
163 const std::vector
<uint8_t> test_kek
= GetBytesFromHexString(test
, "kek");
164 const std::vector
<uint8_t> test_ciphertext
=
165 GetBytesFromHexString(test
, "ciphertext");
167 blink::WebCryptoKey unwrapped_key
;
169 // Using a wrapping algorithm that does not match the wrapping key algorithm
171 blink::WebCryptoKey wrapping_key
= ImportSecretKeyFromRaw(
172 test_kek
, CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw
),
173 blink::WebCryptoKeyUsageUnwrapKey
);
174 EXPECT_EQ(Status::ErrorUnexpected(),
175 UnwrapKey(blink::WebCryptoKeyFormatRaw
, CryptoData(test_ciphertext
),
177 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc
),
178 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc
), true,
179 blink::WebCryptoKeyUsageEncrypt
, &unwrapped_key
));
182 TEST(WebCryptoAesKwTest
, AesKwRawSymkeyWrapUnwrapKnownAnswer
) {
183 scoped_ptr
<base::ListValue
> tests
;
184 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests
));
186 for (size_t test_index
= 0; test_index
< tests
->GetSize(); ++test_index
) {
187 SCOPED_TRACE(test_index
);
188 base::DictionaryValue
* test
;
189 ASSERT_TRUE(tests
->GetDictionary(test_index
, &test
));
190 const std::vector
<uint8_t> test_kek
= GetBytesFromHexString(test
, "kek");
191 const std::vector
<uint8_t> test_key
= GetBytesFromHexString(test
, "key");
192 const std::vector
<uint8_t> test_ciphertext
=
193 GetBytesFromHexString(test
, "ciphertext");
194 const blink::WebCryptoAlgorithm wrapping_algorithm
=
195 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw
);
197 // Import the wrapping key.
198 blink::WebCryptoKey wrapping_key
= ImportSecretKeyFromRaw(
199 test_kek
, wrapping_algorithm
,
200 blink::WebCryptoKeyUsageWrapKey
| blink::WebCryptoKeyUsageUnwrapKey
);
202 // Import the key to be wrapped.
203 blink::WebCryptoKey key
= ImportSecretKeyFromRaw(
205 CreateHmacImportAlgorithmNoLength(blink::WebCryptoAlgorithmIdSha1
),
206 blink::WebCryptoKeyUsageSign
);
208 // Wrap the key and verify the ciphertext result against the known answer.
209 std::vector
<uint8_t> wrapped_key
;
210 ASSERT_EQ(Status::Success(),
211 WrapKey(blink::WebCryptoKeyFormatRaw
, key
, wrapping_key
,
212 wrapping_algorithm
, &wrapped_key
));
213 EXPECT_BYTES_EQ(test_ciphertext
, wrapped_key
);
215 // Unwrap the known ciphertext to get a new test_key.
216 blink::WebCryptoKey unwrapped_key
;
220 blink::WebCryptoKeyFormatRaw
, CryptoData(test_ciphertext
),
221 wrapping_key
, wrapping_algorithm
,
222 CreateHmacImportAlgorithmNoLength(blink::WebCryptoAlgorithmIdSha1
),
223 true, blink::WebCryptoKeyUsageSign
, &unwrapped_key
));
224 EXPECT_FALSE(key
.isNull());
225 EXPECT_TRUE(key
.handle());
226 EXPECT_EQ(blink::WebCryptoKeyTypeSecret
, key
.type());
227 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac
, key
.algorithm().id());
228 EXPECT_EQ(true, key
.extractable());
229 EXPECT_EQ(blink::WebCryptoKeyUsageSign
, key
.usages());
231 // Export the new key and compare its raw bytes with the original known key.
232 std::vector
<uint8_t> raw_key
;
233 EXPECT_EQ(Status::Success(),
234 ExportKey(blink::WebCryptoKeyFormatRaw
, unwrapped_key
, &raw_key
));
235 EXPECT_BYTES_EQ(test_key
, raw_key
);
239 // Unwrap a HMAC key using AES-KW, and then try doing a sign/verify with the
241 TEST(WebCryptoAesKwTest
, AesKwRawSymkeyUnwrapSignVerifyHmac
) {
242 scoped_ptr
<base::ListValue
> tests
;
243 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests
));
245 base::DictionaryValue
* test
;
246 ASSERT_TRUE(tests
->GetDictionary(0, &test
));
247 const std::vector
<uint8_t> test_kek
= GetBytesFromHexString(test
, "kek");
248 const std::vector
<uint8_t> test_ciphertext
=
249 GetBytesFromHexString(test
, "ciphertext");
250 const blink::WebCryptoAlgorithm wrapping_algorithm
=
251 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw
);
253 // Import the wrapping key.
254 blink::WebCryptoKey wrapping_key
= ImportSecretKeyFromRaw(
255 test_kek
, wrapping_algorithm
, blink::WebCryptoKeyUsageUnwrapKey
);
257 // Unwrap the known ciphertext.
258 blink::WebCryptoKey key
;
262 blink::WebCryptoKeyFormatRaw
, CryptoData(test_ciphertext
),
263 wrapping_key
, wrapping_algorithm
,
264 CreateHmacImportAlgorithmNoLength(blink::WebCryptoAlgorithmIdSha1
),
265 false, blink::WebCryptoKeyUsageSign
| blink::WebCryptoKeyUsageVerify
,
268 EXPECT_EQ(blink::WebCryptoKeyTypeSecret
, key
.type());
269 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac
, key
.algorithm().id());
270 EXPECT_FALSE(key
.extractable());
271 EXPECT_EQ(blink::WebCryptoKeyUsageSign
| blink::WebCryptoKeyUsageVerify
,
274 // Sign an empty message and ensure it is verified.
275 std::vector
<uint8_t> test_message
;
276 std::vector
<uint8_t> signature
;
278 ASSERT_EQ(Status::Success(),
279 Sign(CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac
), key
,
280 CryptoData(test_message
), &signature
));
282 EXPECT_GT(signature
.size(), 0u);
287 Verify(CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac
), key
,
288 CryptoData(signature
), CryptoData(test_message
), &verify_result
));
291 TEST(WebCryptoAesKwTest
, AesKwRawSymkeyWrapUnwrapErrors
) {
292 scoped_ptr
<base::ListValue
> tests
;
293 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests
));
294 base::DictionaryValue
* test
;
295 // Use 256 bits of data with a 256-bit KEK
296 ASSERT_TRUE(tests
->GetDictionary(3, &test
));
297 const std::vector
<uint8_t> test_kek
= GetBytesFromHexString(test
, "kek");
298 const std::vector
<uint8_t> test_key
= GetBytesFromHexString(test
, "key");
299 const std::vector
<uint8_t> test_ciphertext
=
300 GetBytesFromHexString(test
, "ciphertext");
301 const blink::WebCryptoAlgorithm wrapping_algorithm
=
302 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw
);
303 const blink::WebCryptoAlgorithm key_algorithm
=
304 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc
);
305 // Import the wrapping key.
306 blink::WebCryptoKey wrapping_key
= ImportSecretKeyFromRaw(
307 test_kek
, wrapping_algorithm
,
308 blink::WebCryptoKeyUsageWrapKey
| blink::WebCryptoKeyUsageUnwrapKey
);
309 // Import the key to be wrapped.
310 blink::WebCryptoKey key
= ImportSecretKeyFromRaw(
311 test_key
, CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc
),
312 blink::WebCryptoKeyUsageEncrypt
);
314 // Unwrap with wrapped data too small must fail.
315 const std::vector
<uint8_t> small_data(test_ciphertext
.begin(),
316 test_ciphertext
.begin() + 23);
317 blink::WebCryptoKey unwrapped_key
;
318 EXPECT_EQ(Status::ErrorDataTooSmall(),
319 UnwrapKey(blink::WebCryptoKeyFormatRaw
, CryptoData(small_data
),
320 wrapping_key
, wrapping_algorithm
, key_algorithm
, true,
321 blink::WebCryptoKeyUsageEncrypt
, &unwrapped_key
));
323 // Unwrap with wrapped data size not a multiple of 8 bytes must fail.
324 const std::vector
<uint8_t> unaligned_data(test_ciphertext
.begin(),
325 test_ciphertext
.end() - 2);
326 EXPECT_EQ(Status::ErrorInvalidAesKwDataLength(),
327 UnwrapKey(blink::WebCryptoKeyFormatRaw
, CryptoData(unaligned_data
),
328 wrapping_key
, wrapping_algorithm
, key_algorithm
, true,
329 blink::WebCryptoKeyUsageEncrypt
, &unwrapped_key
));
332 TEST(WebCryptoAesKwTest
, AesKwRawSymkeyUnwrapCorruptData
) {
333 scoped_ptr
<base::ListValue
> tests
;
334 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests
));
335 base::DictionaryValue
* test
;
336 // Use 256 bits of data with a 256-bit KEK
337 ASSERT_TRUE(tests
->GetDictionary(3, &test
));
338 const std::vector
<uint8_t> test_kek
= GetBytesFromHexString(test
, "kek");
339 const std::vector
<uint8_t> test_key
= GetBytesFromHexString(test
, "key");
340 const std::vector
<uint8_t> test_ciphertext
=
341 GetBytesFromHexString(test
, "ciphertext");
342 const blink::WebCryptoAlgorithm wrapping_algorithm
=
343 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw
);
345 // Import the wrapping key.
346 blink::WebCryptoKey wrapping_key
= ImportSecretKeyFromRaw(
347 test_kek
, wrapping_algorithm
,
348 blink::WebCryptoKeyUsageWrapKey
| blink::WebCryptoKeyUsageUnwrapKey
);
350 // Unwrap of a corrupted version of the known ciphertext should fail, due to
351 // AES-KW's built-in integrity check.
352 blink::WebCryptoKey unwrapped_key
;
353 EXPECT_EQ(Status::OperationError(),
354 UnwrapKey(blink::WebCryptoKeyFormatRaw
,
355 CryptoData(Corrupted(test_ciphertext
)), wrapping_key
,
357 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc
), true,
358 blink::WebCryptoKeyUsageEncrypt
, &unwrapped_key
));
361 TEST(WebCryptoAesKwTest
, AesKwJwkSymkeyUnwrapKnownData
) {
362 // The following data lists a known HMAC SHA-256 key, then a JWK
363 // representation of this key which was encrypted ("wrapped") using AES-KW and
364 // the following wrapping key.
365 // For reference, the intermediate clear JWK is
366 // {"alg":"HS256","ext":true,"k":<b64urlKey>,"key_ops":["verify"],"kty":"oct"}
367 // (Not shown is space padding to ensure the cleartext meets the size
368 // requirements of the AES-KW algorithm.)
369 const std::vector
<uint8_t> key_data
= HexStringToBytes(
370 "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F");
371 const std::vector
<uint8_t> wrapped_key_data
= HexStringToBytes(
372 "14E6380B35FDC5B72E1994764B6CB7BFDD64E7832894356AAEE6C3768FC3D0F115E6B0"
373 "6729756225F999AA99FDF81FD6A359F1576D3D23DE6CB69C3937054EB497AC1E8C38D5"
374 "5E01B9783A20C8D930020932CF25926103002213D0FC37279888154FEBCEDF31832158"
375 "97938C5CFE5B10B4254D0C399F39D0");
376 const std::vector
<uint8_t> wrapping_key_data
=
377 HexStringToBytes("000102030405060708090A0B0C0D0E0F");
378 const blink::WebCryptoAlgorithm wrapping_algorithm
=
379 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw
);
381 // Import the wrapping key.
382 blink::WebCryptoKey wrapping_key
= ImportSecretKeyFromRaw(
383 wrapping_key_data
, wrapping_algorithm
, blink::WebCryptoKeyUsageUnwrapKey
);
385 // Unwrap the known wrapped key data to produce a new key
386 blink::WebCryptoKey unwrapped_key
;
390 blink::WebCryptoKeyFormatJwk
, CryptoData(wrapped_key_data
),
391 wrapping_key
, wrapping_algorithm
,
392 CreateHmacImportAlgorithmNoLength(blink::WebCryptoAlgorithmIdSha256
),
393 true, blink::WebCryptoKeyUsageVerify
, &unwrapped_key
));
395 // Validate the new key's attributes.
396 EXPECT_FALSE(unwrapped_key
.isNull());
397 EXPECT_TRUE(unwrapped_key
.handle());
398 EXPECT_EQ(blink::WebCryptoKeyTypeSecret
, unwrapped_key
.type());
399 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac
, unwrapped_key
.algorithm().id());
400 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256
,
401 unwrapped_key
.algorithm().hmacParams()->hash().id());
402 EXPECT_EQ(256u, unwrapped_key
.algorithm().hmacParams()->lengthBits());
403 EXPECT_EQ(true, unwrapped_key
.extractable());
404 EXPECT_EQ(blink::WebCryptoKeyUsageVerify
, unwrapped_key
.usages());
406 // Export the new key's raw data and compare to the known original.
407 std::vector
<uint8_t> raw_key
;
408 EXPECT_EQ(Status::Success(),
409 ExportKey(blink::WebCryptoKeyFormatRaw
, unwrapped_key
, &raw_key
));
410 EXPECT_BYTES_EQ(key_data
, raw_key
);
413 // Try importing an AES-KW key with unsupported key usages using raw
414 // format. AES-KW keys support the following usages:
415 // 'wrapKey', 'unwrapKey'
416 TEST(WebCryptoAesKwTest
, ImportKeyBadUsage_Raw
) {
417 const blink::WebCryptoAlgorithm algorithm
=
418 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw
);
420 blink::WebCryptoKeyUsageMask bad_usages
[] = {
421 blink::WebCryptoKeyUsageEncrypt
,
422 blink::WebCryptoKeyUsageDecrypt
,
423 blink::WebCryptoKeyUsageSign
,
424 blink::WebCryptoKeyUsageSign
| blink::WebCryptoKeyUsageUnwrapKey
,
425 blink::WebCryptoKeyUsageDeriveBits
,
426 blink::WebCryptoKeyUsageUnwrapKey
| blink::WebCryptoKeyUsageVerify
,
429 std::vector
<uint8_t> key_bytes(16);
431 for (size_t i
= 0; i
< arraysize(bad_usages
); ++i
) {
434 blink::WebCryptoKey key
;
435 ASSERT_EQ(Status::ErrorCreateKeyBadUsages(),
436 ImportKey(blink::WebCryptoKeyFormatRaw
, CryptoData(key_bytes
),
437 algorithm
, true, bad_usages
[i
], &key
));
441 // Try unwrapping an HMAC key with unsupported usages using JWK format and
442 // AES-KW. HMAC keys support the following usages:
444 TEST(WebCryptoAesKwTest
, UnwrapHmacKeyBadUsage_JWK
) {
445 const blink::WebCryptoAlgorithm unwrap_algorithm
=
446 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw
);
448 blink::WebCryptoKeyUsageMask bad_usages
[] = {
449 blink::WebCryptoKeyUsageEncrypt
,
450 blink::WebCryptoKeyUsageDecrypt
,
451 blink::WebCryptoKeyUsageWrapKey
,
452 blink::WebCryptoKeyUsageSign
| blink::WebCryptoKeyUsageWrapKey
,
453 blink::WebCryptoKeyUsageVerify
| blink::WebCryptoKeyUsageDeriveKey
,
456 // Import the wrapping key.
457 blink::WebCryptoKey wrapping_key
;
458 ASSERT_EQ(Status::Success(),
459 ImportKey(blink::WebCryptoKeyFormatRaw
,
460 CryptoData(std::vector
<uint8_t>(16)), unwrap_algorithm
,
461 true, blink::WebCryptoKeyUsageUnwrapKey
, &wrapping_key
));
463 // The JWK plain text is:
464 // {"kty":"oct","alg":"HS256","k":"GADWrMRHwQfoNaXU5fZvTg"}
465 const char* kWrappedJwk
=
466 "C2B7F19A32EE31372CD40C9C969B8CD67553E5AEA7FD1144874584E46ABCD79FDC308848"
467 "B2DD8BD36A2D61062B9C5B8B499B8D6EF8EB320D87A614952B4EE771";
469 for (size_t i
= 0; i
< arraysize(bad_usages
); ++i
) {
472 blink::WebCryptoKey key
;
475 Status::ErrorCreateKeyBadUsages(),
476 UnwrapKey(blink::WebCryptoKeyFormatJwk
,
477 CryptoData(HexStringToBytes(kWrappedJwk
)), wrapping_key
,
478 unwrap_algorithm
, CreateHmacImportAlgorithmNoLength(
479 blink::WebCryptoAlgorithmIdSha256
),
480 true, bad_usages
[i
], &key
));
484 // Try unwrapping an RSA-SSA public key with unsupported usages using JWK format
485 // and AES-KW. RSA-SSA public keys support the following usages:
487 TEST(WebCryptoAesKwTest
, UnwrapRsaSsaPublicKeyBadUsage_JWK
) {
488 const blink::WebCryptoAlgorithm unwrap_algorithm
=
489 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw
);
491 blink::WebCryptoKeyUsageMask bad_usages
[] = {
492 blink::WebCryptoKeyUsageEncrypt
,
493 blink::WebCryptoKeyUsageSign
,
494 blink::WebCryptoKeyUsageDecrypt
,
495 blink::WebCryptoKeyUsageWrapKey
,
496 blink::WebCryptoKeyUsageSign
| blink::WebCryptoKeyUsageWrapKey
,
499 // Import the wrapping key.
500 blink::WebCryptoKey wrapping_key
;
501 ASSERT_EQ(Status::Success(),
502 ImportKey(blink::WebCryptoKeyFormatRaw
,
503 CryptoData(std::vector
<uint8_t>(16)), unwrap_algorithm
,
504 true, blink::WebCryptoKeyUsageUnwrapKey
, &wrapping_key
));
506 // The JWK plaintext is:
507 // { "kty": "RSA","alg": "RS256","n": "...","e": "AQAB"}
509 const char* kWrappedJwk
=
510 "CE8DAEF99E977EE58958B8C4494755C846E883B2ECA575C5366622839AF71AB30875F152"
511 "E8E33E15A7817A3A2874EB53EFE05C774D98BC936BA9BA29BEB8BB3F3C3CE2323CB3359D"
512 "E3F426605CF95CCF0E01E870ABD7E35F62E030B5FB6E520A5885514D1D850FB64B57806D"
513 "1ADA57C6E27DF345D8292D80F6B074F1BE51C4CF3D76ECC8886218551308681B44FAC60B"
514 "8CF6EA439BC63239103D0AE81ADB96F908680586C6169284E32EB7DD09D31103EBDAC0C2"
515 "40C72DCF0AEA454113CC47457B13305B25507CBEAB9BDC8D8E0F867F9167F9DCEF0D9F9B"
516 "30F2EE83CEDFD51136852C8A5939B768";
518 for (size_t i
= 0; i
< arraysize(bad_usages
); ++i
) {
521 blink::WebCryptoKey key
;
523 ASSERT_EQ(Status::ErrorCreateKeyBadUsages(),
524 UnwrapKey(blink::WebCryptoKeyFormatJwk
,
525 CryptoData(HexStringToBytes(kWrappedJwk
)), wrapping_key
,
527 CreateRsaHashedImportAlgorithm(
528 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5
,
529 blink::WebCryptoAlgorithmIdSha256
),
530 true, bad_usages
[i
], &key
));
536 } // namespace webcrypto