| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 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" | |
| 13 | |
| 14 namespace webcrypto { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 blink::WebCryptoAlgorithm CreateAesKwKeyGenAlgorithm( | |
| 19 unsigned short key_length_bits) { | |
| 20 return CreateAesKeyGenAlgorithm(blink::WebCryptoAlgorithmIdAesKw, | |
| 21 key_length_bits); | |
| 22 } | |
| 23 | |
| 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) { | |
| 28 SCOPED_TRACE(i); | |
| 29 EXPECT_EQ(Status::ErrorGenerateAesKeyLength(), | |
| 30 GenerateSecretKey(CreateAesKwKeyGenAlgorithm(kKeyLen[i]), true, | |
| 31 blink::WebCryptoKeyUsageWrapKey, &key)); | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 TEST(WebCryptoAesKwTest, GenerateKeyEmptyUsage) { | |
| 36 blink::WebCryptoKey key; | |
| 37 EXPECT_EQ(Status::ErrorCreateKeyEmptyUsages(), | |
| 38 GenerateSecretKey(CreateAesKwKeyGenAlgorithm(256), true, 0, &key)); | |
| 39 } | |
| 40 | |
| 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, | |
| 47 0, &key)); | |
| 48 } | |
| 49 | |
| 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. | |
| 57 | |
| 58 key_ops->AppendString("wrapKey"); | |
| 59 | |
| 60 EXPECT_EQ(Status::Success(), | |
| 61 ImportKeyJwkFromDict( | |
| 62 dict, CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw), false, | |
| 63 blink::WebCryptoKeyUsageWrapKey, &key)); | |
| 64 | |
| 65 EXPECT_EQ(blink::WebCryptoKeyUsageWrapKey, key.usages()); | |
| 66 | |
| 67 key_ops->AppendString("unwrapKey"); | |
| 68 | |
| 69 EXPECT_EQ(Status::Success(), | |
| 70 ImportKeyJwkFromDict( | |
| 71 dict, CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw), false, | |
| 72 blink::WebCryptoKeyUsageUnwrapKey, &key)); | |
| 73 | |
| 74 EXPECT_EQ(blink::WebCryptoKeyUsageUnwrapKey, key.usages()); | |
| 75 } | |
| 76 | |
| 77 TEST(WebCryptoAesKwTest, ImportExportJwk) { | |
| 78 const blink::WebCryptoAlgorithm algorithm = | |
| 79 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); | |
| 80 | |
| 81 // AES-KW 128 | |
| 82 ImportExportJwkSymmetricKey( | |
| 83 128, algorithm, | |
| 84 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey, | |
| 85 "A128KW"); | |
| 86 | |
| 87 // AES-KW 256 | |
| 88 ImportExportJwkSymmetricKey( | |
| 89 256, algorithm, | |
| 90 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey, | |
| 91 "A256KW"); | |
| 92 } | |
| 93 | |
| 94 TEST(WebCryptoAesKwTest, AesKwKeyImport) { | |
| 95 blink::WebCryptoKey key; | |
| 96 blink::WebCryptoAlgorithm algorithm = | |
| 97 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); | |
| 98 | |
| 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); | |
| 109 | |
| 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)); | |
| 116 | |
| 117 // Import a 256-bit Key Encryption Key (KEK) | |
| 118 key_raw_hex_in = | |
| 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); | |
| 127 | |
| 128 // Fail import of 0 length key | |
| 129 EXPECT_EQ( | |
| 130 Status::ErrorImportAesKeyLength(), | |
| 131 ImportKey(blink::WebCryptoKeyFormatRaw, CryptoData(HexStringToBytes("")), | |
| 132 algorithm, true, blink::WebCryptoKeyUsageWrapKey, &key)); | |
| 133 | |
| 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)); | |
| 140 | |
| 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)); | |
| 147 | |
| 148 // Fail import of 260-bit KEK | |
| 149 key_raw_hex_in = | |
| 150 "72d4e475ff34215416c9ad9c8281247a4d730c5f275ac23f376e73e3bce8d7d5a"; | |
| 151 EXPECT_EQ(Status::ErrorImportAesKeyLength(), | |
| 152 ImportKey(blink::WebCryptoKeyFormatRaw, | |
| 153 CryptoData(HexStringToBytes(key_raw_hex_in)), algorithm, | |
| 154 true, blink::WebCryptoKeyUsageWrapKey, &key)); | |
| 155 } | |
| 156 | |
| 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"); | |
| 166 | |
| 167 blink::WebCryptoKey unwrapped_key; | |
| 168 | |
| 169 // Using a wrapping algorithm that does not match the wrapping key algorithm | |
| 170 // should fail. | |
| 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), | |
| 176 wrapping_key, | |
| 177 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), | |
| 178 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), true, | |
| 179 blink::WebCryptoKeyUsageEncrypt, &unwrapped_key)); | |
| 180 } | |
| 181 | |
| 182 TEST(WebCryptoAesKwTest, AesKwRawSymkeyWrapUnwrapKnownAnswer) { | |
| 183 scoped_ptr<base::ListValue> tests; | |
| 184 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests)); | |
| 185 | |
| 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); | |
| 196 | |
| 197 // Import the wrapping key. | |
| 198 blink::WebCryptoKey wrapping_key = ImportSecretKeyFromRaw( | |
| 199 test_kek, wrapping_algorithm, | |
| 200 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey); | |
| 201 | |
| 202 // Import the key to be wrapped. | |
| 203 blink::WebCryptoKey key = ImportSecretKeyFromRaw( | |
| 204 test_key, | |
| 205 CreateHmacImportAlgorithmNoLength(blink::WebCryptoAlgorithmIdSha1), | |
| 206 blink::WebCryptoKeyUsageSign); | |
| 207 | |
| 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); | |
| 214 | |
| 215 // Unwrap the known ciphertext to get a new test_key. | |
| 216 blink::WebCryptoKey unwrapped_key; | |
| 217 ASSERT_EQ( | |
| 218 Status::Success(), | |
| 219 UnwrapKey( | |
| 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()); | |
| 230 | |
| 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); | |
| 236 } | |
| 237 } | |
| 238 | |
| 239 // Unwrap a HMAC key using AES-KW, and then try doing a sign/verify with the | |
| 240 // unwrapped key | |
| 241 TEST(WebCryptoAesKwTest, AesKwRawSymkeyUnwrapSignVerifyHmac) { | |
| 242 scoped_ptr<base::ListValue> tests; | |
| 243 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests)); | |
| 244 | |
| 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); | |
| 252 | |
| 253 // Import the wrapping key. | |
| 254 blink::WebCryptoKey wrapping_key = ImportSecretKeyFromRaw( | |
| 255 test_kek, wrapping_algorithm, blink::WebCryptoKeyUsageUnwrapKey); | |
| 256 | |
| 257 // Unwrap the known ciphertext. | |
| 258 blink::WebCryptoKey key; | |
| 259 ASSERT_EQ( | |
| 260 Status::Success(), | |
| 261 UnwrapKey( | |
| 262 blink::WebCryptoKeyFormatRaw, CryptoData(test_ciphertext), | |
| 263 wrapping_key, wrapping_algorithm, | |
| 264 CreateHmacImportAlgorithmNoLength(blink::WebCryptoAlgorithmIdSha1), | |
| 265 false, blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify, | |
| 266 &key)); | |
| 267 | |
| 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, | |
| 272 key.usages()); | |
| 273 | |
| 274 // Sign an empty message and ensure it is verified. | |
| 275 std::vector<uint8_t> test_message; | |
| 276 std::vector<uint8_t> signature; | |
| 277 | |
| 278 ASSERT_EQ(Status::Success(), | |
| 279 Sign(CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac), key, | |
| 280 CryptoData(test_message), &signature)); | |
| 281 | |
| 282 EXPECT_GT(signature.size(), 0u); | |
| 283 | |
| 284 bool verify_result; | |
| 285 ASSERT_EQ( | |
| 286 Status::Success(), | |
| 287 Verify(CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac), key, | |
| 288 CryptoData(signature), CryptoData(test_message), &verify_result)); | |
| 289 } | |
| 290 | |
| 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); | |
| 313 | |
| 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)); | |
| 322 | |
| 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)); | |
| 330 } | |
| 331 | |
| 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); | |
| 344 | |
| 345 // Import the wrapping key. | |
| 346 blink::WebCryptoKey wrapping_key = ImportSecretKeyFromRaw( | |
| 347 test_kek, wrapping_algorithm, | |
| 348 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey); | |
| 349 | |
| 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, | |
| 356 wrapping_algorithm, | |
| 357 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), true, | |
| 358 blink::WebCryptoKeyUsageEncrypt, &unwrapped_key)); | |
| 359 } | |
| 360 | |
| 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); | |
| 380 | |
| 381 // Import the wrapping key. | |
| 382 blink::WebCryptoKey wrapping_key = ImportSecretKeyFromRaw( | |
| 383 wrapping_key_data, wrapping_algorithm, blink::WebCryptoKeyUsageUnwrapKey); | |
| 384 | |
| 385 // Unwrap the known wrapped key data to produce a new key | |
| 386 blink::WebCryptoKey unwrapped_key; | |
| 387 ASSERT_EQ( | |
| 388 Status::Success(), | |
| 389 UnwrapKey( | |
| 390 blink::WebCryptoKeyFormatJwk, CryptoData(wrapped_key_data), | |
| 391 wrapping_key, wrapping_algorithm, | |
| 392 CreateHmacImportAlgorithmNoLength(blink::WebCryptoAlgorithmIdSha256), | |
| 393 true, blink::WebCryptoKeyUsageVerify, &unwrapped_key)); | |
| 394 | |
| 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()); | |
| 405 | |
| 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); | |
| 411 } | |
| 412 | |
| 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); | |
| 419 | |
| 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, | |
| 427 }; | |
| 428 | |
| 429 std::vector<uint8_t> key_bytes(16); | |
| 430 | |
| 431 for (size_t i = 0; i < arraysize(bad_usages); ++i) { | |
| 432 SCOPED_TRACE(i); | |
| 433 | |
| 434 blink::WebCryptoKey key; | |
| 435 ASSERT_EQ(Status::ErrorCreateKeyBadUsages(), | |
| 436 ImportKey(blink::WebCryptoKeyFormatRaw, CryptoData(key_bytes), | |
| 437 algorithm, true, bad_usages[i], &key)); | |
| 438 } | |
| 439 } | |
| 440 | |
| 441 // Try unwrapping an HMAC key with unsupported usages using JWK format and | |
| 442 // AES-KW. HMAC keys support the following usages: | |
| 443 // 'sign', 'verify' | |
| 444 TEST(WebCryptoAesKwTest, UnwrapHmacKeyBadUsage_JWK) { | |
| 445 const blink::WebCryptoAlgorithm unwrap_algorithm = | |
| 446 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); | |
| 447 | |
| 448 blink::WebCryptoKeyUsageMask bad_usages[] = { | |
| 449 blink::WebCryptoKeyUsageEncrypt, | |
| 450 blink::WebCryptoKeyUsageDecrypt, | |
| 451 blink::WebCryptoKeyUsageWrapKey, | |
| 452 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageWrapKey, | |
| 453 blink::WebCryptoKeyUsageVerify | blink::WebCryptoKeyUsageDeriveKey, | |
| 454 }; | |
| 455 | |
| 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)); | |
| 462 | |
| 463 // The JWK plain text is: | |
| 464 // {"kty":"oct","alg":"HS256","k":"GADWrMRHwQfoNaXU5fZvTg"} | |
| 465 const char* kWrappedJwk = | |
| 466 "C2B7F19A32EE31372CD40C9C969B8CD67553E5AEA7FD1144874584E46ABCD79FDC308848" | |
| 467 "B2DD8BD36A2D61062B9C5B8B499B8D6EF8EB320D87A614952B4EE771"; | |
| 468 | |
| 469 for (size_t i = 0; i < arraysize(bad_usages); ++i) { | |
| 470 SCOPED_TRACE(i); | |
| 471 | |
| 472 blink::WebCryptoKey key; | |
| 473 | |
| 474 ASSERT_EQ( | |
| 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)); | |
| 481 } | |
| 482 } | |
| 483 | |
| 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: | |
| 486 // 'verify' | |
| 487 TEST(WebCryptoAesKwTest, UnwrapRsaSsaPublicKeyBadUsage_JWK) { | |
| 488 const blink::WebCryptoAlgorithm unwrap_algorithm = | |
| 489 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); | |
| 490 | |
| 491 blink::WebCryptoKeyUsageMask bad_usages[] = { | |
| 492 blink::WebCryptoKeyUsageEncrypt, | |
| 493 blink::WebCryptoKeyUsageSign, | |
| 494 blink::WebCryptoKeyUsageDecrypt, | |
| 495 blink::WebCryptoKeyUsageWrapKey, | |
| 496 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageWrapKey, | |
| 497 }; | |
| 498 | |
| 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)); | |
| 505 | |
| 506 // The JWK plaintext is: | |
| 507 // { "kty": "RSA","alg": "RS256","n": "...","e": "AQAB"} | |
| 508 | |
| 509 const char* kWrappedJwk = | |
| 510 "CE8DAEF99E977EE58958B8C4494755C846E883B2ECA575C5366622839AF71AB30875F152" | |
| 511 "E8E33E15A7817A3A2874EB53EFE05C774D98BC936BA9BA29BEB8BB3F3C3CE2323CB3359D" | |
| 512 "E3F426605CF95CCF0E01E870ABD7E35F62E030B5FB6E520A5885514D1D850FB64B57806D" | |
| 513 "1ADA57C6E27DF345D8292D80F6B074F1BE51C4CF3D76ECC8886218551308681B44FAC60B" | |
| 514 "8CF6EA439BC63239103D0AE81ADB96F908680586C6169284E32EB7DD09D31103EBDAC0C2" | |
| 515 "40C72DCF0AEA454113CC47457B13305B25507CBEAB9BDC8D8E0F867F9167F9DCEF0D9F9B" | |
| 516 "30F2EE83CEDFD51136852C8A5939B768"; | |
| 517 | |
| 518 for (size_t i = 0; i < arraysize(bad_usages); ++i) { | |
| 519 SCOPED_TRACE(i); | |
| 520 | |
| 521 blink::WebCryptoKey key; | |
| 522 | |
| 523 ASSERT_EQ(Status::ErrorCreateKeyBadUsages(), | |
| 524 UnwrapKey(blink::WebCryptoKeyFormatJwk, | |
| 525 CryptoData(HexStringToBytes(kWrappedJwk)), wrapping_key, | |
| 526 unwrap_algorithm, | |
| 527 CreateRsaHashedImportAlgorithm( | |
| 528 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | |
| 529 blink::WebCryptoAlgorithmIdSha256), | |
| 530 true, bad_usages[i], &key)); | |
| 531 } | |
| 532 } | |
| 533 | |
| 534 } // namespace | |
| 535 | |
| 536 } // namespace webcrypto | |
| OLD | NEW |