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

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 TEST(WebCryptoAesCbcTest, ImportKeyEmptyUsage) {
203 blink::WebCryptoKey key;
204 std::vector<uint8_t> key_bytes(16);
205 ASSERT_EQ(Status::ErrorImportEmptyKeyUsage(),
206 ImportKey(blink::WebCryptoKeyFormatRaw,
207 CryptoData(key_bytes),
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 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
517 // Unwrap the key pair. 520 // Unwrap the key pair.
518 blink::WebCryptoAlgorithm rsa_import_algorithm = 521 blink::WebCryptoAlgorithm rsa_import_algorithm =
519 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 522 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
520 blink::WebCryptoAlgorithmIdSha256); 523 blink::WebCryptoAlgorithmIdSha256);
521 524
522 blink::WebCryptoKey unwrapped_public_key; 525 blink::WebCryptoKey unwrapped_public_key;
523 526
524 ASSERT_EQ( 527 ASSERT_EQ(
525 Status::Success(), 528 Status::Success(),
526 UnwrapKey(blink::WebCryptoKeyFormatSpki, CryptoData(wrapped_public_key), 529 UnwrapKey(blink::WebCryptoKeyFormatSpki, CryptoData(wrapped_public_key),
527 wrapping_key, wrap_algorithm, rsa_import_algorithm, true, 0, 530 wrapping_key, wrap_algorithm, rsa_import_algorithm, true,
528 &unwrapped_public_key)); 531 blink::WebCryptoKeyUsageVerify, &unwrapped_public_key));
529 532
530 blink::WebCryptoKey unwrapped_private_key; 533 blink::WebCryptoKey unwrapped_private_key;
531 534
532 ASSERT_EQ( 535 ASSERT_EQ(
533 Status::Success(), 536 Status::Success(),
534 UnwrapKey(blink::WebCryptoKeyFormatPkcs8, CryptoData(wrapped_private_key), 537 UnwrapKey(blink::WebCryptoKeyFormatPkcs8, CryptoData(wrapped_private_key),
535 wrapping_key, wrap_algorithm, rsa_import_algorithm, true, 0, 538 wrapping_key, wrap_algorithm, rsa_import_algorithm, true,
536 &unwrapped_private_key)); 539 blink::WebCryptoKeyUsageSign, &unwrapped_private_key));
537 540
538 // Export unwrapped key pair as SPKI + PKCS8 541 // Export unwrapped key pair as SPKI + PKCS8
539 std::vector<uint8_t> unwrapped_public_key_spki; 542 std::vector<uint8_t> unwrapped_public_key_spki;
540 ASSERT_EQ(Status::Success(), 543 ASSERT_EQ(Status::Success(),
541 ExportKey(blink::WebCryptoKeyFormatSpki, unwrapped_public_key, 544 ExportKey(blink::WebCryptoKeyFormatSpki, unwrapped_public_key,
542 &unwrapped_public_key_spki)); 545 &unwrapped_public_key_spki));
543 546
544 std::vector<uint8_t> unwrapped_private_key_pkcs8; 547 std::vector<uint8_t> unwrapped_private_key_pkcs8;
545 ASSERT_EQ(Status::Success(), 548 ASSERT_EQ(Status::Success(),
546 ExportKey(blink::WebCryptoKeyFormatPkcs8, unwrapped_private_key, 549 ExportKey(blink::WebCryptoKeyFormatPkcs8, unwrapped_private_key,
547 &unwrapped_private_key_pkcs8)); 550 &unwrapped_private_key_pkcs8));
548 551
549 EXPECT_EQ(public_key_spki, unwrapped_public_key_spki); 552 EXPECT_EQ(public_key_spki, unwrapped_public_key_spki);
550 EXPECT_EQ(private_key_pkcs8, unwrapped_private_key_pkcs8); 553 EXPECT_EQ(private_key_pkcs8, unwrapped_private_key_pkcs8);
551 554
552 EXPECT_NE(public_key_spki, wrapped_public_key); 555 EXPECT_NE(public_key_spki, wrapped_public_key);
553 EXPECT_NE(private_key_pkcs8, wrapped_private_key); 556 EXPECT_NE(private_key_pkcs8, wrapped_private_key);
554 } 557 }
555 558
556 } // namespace 559 } // namespace
557 560
558 } // namespace webcrypto 561 } // namespace webcrypto
559 562
560 } // namespace content 563 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698