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

Side by Side Diff: content/child/webcrypto/test/aes_cbc_unittest.cc

Issue 777403004: [WebCrypto] Throw syntaxError if keyUsage is empty in ImportKey (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years 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
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 "base/stl_util.h" 5 #include "base/stl_util.h"
6 #include "content/child/webcrypto/algorithm_dispatch.h" 6 #include "content/child/webcrypto/algorithm_dispatch.h"
7 #include "content/child/webcrypto/crypto_data.h" 7 #include "content/child/webcrypto/crypto_data.h"
8 #include "content/child/webcrypto/status.h" 8 #include "content/child/webcrypto/status.h"
9 #include "content/child/webcrypto/test/test_helpers.h" 9 #include "content/child/webcrypto/test/test_helpers.h"
10 #include "content/child/webcrypto/webcrypto_util.h" 10 #include "content/child/webcrypto/webcrypto_util.h"
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 const unsigned short kKeyLen[] = {0, 127, 257}; 192 const unsigned short kKeyLen[] = {0, 127, 257};
193 blink::WebCryptoKey key; 193 blink::WebCryptoKey key;
194 for (size_t i = 0; i < arraysize(kKeyLen); ++i) { 194 for (size_t i = 0; i < arraysize(kKeyLen); ++i) {
195 SCOPED_TRACE(i); 195 SCOPED_TRACE(i);
196 EXPECT_EQ(Status::ErrorGenerateAesKeyLength(), 196 EXPECT_EQ(Status::ErrorGenerateAesKeyLength(),
197 GenerateSecretKey(CreateAesCbcKeyGenAlgorithm(kKeyLen[i]), true, 197 GenerateSecretKey(CreateAesCbcKeyGenAlgorithm(kKeyLen[i]), true,
198 0, &key)); 198 0, &key));
199 } 199 }
200 } 200 }
201 201
202 // Import key with empty usages, should throw a syntax error.
203 TEST(WebCryptoAesCbcTest, ImportKeyEmptyUsage) {
204 blink::WebCryptoKey key;
205 ASSERT_EQ(Status::ErrorImportKeyEmptyUsages(),
206 ImportKey(blink::WebCryptoKeyFormatRaw,
207 CryptoData(std::vector<uint8_t>(16)),
208 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
209 true, 0, &key));
210 }
211
202 // If key_ops is specified but empty, no key usages are allowed for the key. 212 // If key_ops is specified but empty, no key usages are allowed for the key.
203 TEST(WebCryptoAesCbcTest, ImportKeyJwkEmptyKeyOps) { 213 TEST(WebCryptoAesCbcTest, ImportKeyJwkEmptyKeyOps) {
204 blink::WebCryptoKey key; 214 blink::WebCryptoKey key;
205 base::DictionaryValue dict; 215 base::DictionaryValue dict;
206 dict.SetString("kty", "oct"); 216 dict.SetString("kty", "oct");
207 dict.SetBoolean("ext", false); 217 dict.SetBoolean("ext", false);
208 dict.SetString("k", "GADWrMRHwQfoNaXU5fZvTg"); 218 dict.SetString("k", "GADWrMRHwQfoNaXU5fZvTg");
209 dict.Set("key_ops", new base::ListValue); // Takes ownership. 219 dict.Set("key_ops", new base::ListValue); // Takes ownership.
210 220
211 EXPECT_EQ(Status::Success(),
212 ImportKeyJwkFromDict(
213 dict, CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), false,
214 0, &key));
215
216 EXPECT_EQ(0, key.usages());
217
218 // The JWK does not contain encrypt usages. 221 // The JWK does not contain encrypt usages.
219 EXPECT_EQ(Status::ErrorJwkKeyopsInconsistent(), 222 EXPECT_EQ(Status::ErrorJwkKeyopsInconsistent(),
220 ImportKeyJwkFromDict( 223 ImportKeyJwkFromDict(
221 dict, CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), false, 224 dict, CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), false,
222 blink::WebCryptoKeyUsageEncrypt, &key)); 225 blink::WebCryptoKeyUsageEncrypt, &key));
223 226
224 // The JWK does not contain sign usage (nor is it applicable). 227 // The JWK does not contain sign usage (nor is it applicable).
225 EXPECT_EQ(Status::ErrorCreateKeyBadUsages(), 228 EXPECT_EQ(Status::ErrorCreateKeyBadUsages(),
226 ImportKeyJwkFromDict( 229 ImportKeyJwkFromDict(
227 dict, CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), false, 230 dict, CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), false,
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
526 // Unwrap the key pair. 529 // Unwrap the key pair.
527 blink::WebCryptoAlgorithm rsa_import_algorithm = 530 blink::WebCryptoAlgorithm rsa_import_algorithm =
528 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 531 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
529 blink::WebCryptoAlgorithmIdSha256); 532 blink::WebCryptoAlgorithmIdSha256);
530 533
531 blink::WebCryptoKey unwrapped_public_key; 534 blink::WebCryptoKey unwrapped_public_key;
532 535
533 ASSERT_EQ( 536 ASSERT_EQ(
534 Status::Success(), 537 Status::Success(),
535 UnwrapKey(blink::WebCryptoKeyFormatSpki, CryptoData(wrapped_public_key), 538 UnwrapKey(blink::WebCryptoKeyFormatSpki, CryptoData(wrapped_public_key),
536 wrapping_key, wrap_algorithm, rsa_import_algorithm, true, 0, 539 wrapping_key, wrap_algorithm, rsa_import_algorithm, true,
537 &unwrapped_public_key)); 540 blink::WebCryptoKeyUsageVerify, &unwrapped_public_key));
538 541
539 blink::WebCryptoKey unwrapped_private_key; 542 blink::WebCryptoKey unwrapped_private_key;
540 543
541 ASSERT_EQ( 544 ASSERT_EQ(
542 Status::Success(), 545 Status::Success(),
543 UnwrapKey(blink::WebCryptoKeyFormatPkcs8, CryptoData(wrapped_private_key), 546 UnwrapKey(blink::WebCryptoKeyFormatPkcs8, CryptoData(wrapped_private_key),
544 wrapping_key, wrap_algorithm, rsa_import_algorithm, true, 547 wrapping_key, wrap_algorithm, rsa_import_algorithm, true,
545 blink::WebCryptoKeyUsageSign, &unwrapped_private_key)); 548 blink::WebCryptoKeyUsageSign, &unwrapped_private_key));
546 549
547 // Export unwrapped key pair as SPKI + PKCS8 550 // Export unwrapped key pair as SPKI + PKCS8
(...skipping 12 matching lines...) Expand all
560 563
561 EXPECT_NE(public_key_spki, wrapped_public_key); 564 EXPECT_NE(public_key_spki, wrapped_public_key);
562 EXPECT_NE(private_key_pkcs8, wrapped_private_key); 565 EXPECT_NE(private_key_pkcs8, wrapped_private_key);
563 } 566 }
564 567
565 } // namespace 568 } // namespace
566 569
567 } // namespace webcrypto 570 } // namespace webcrypto
568 571
569 } // namespace content 572 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698