OLD | NEW |
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 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 } | 188 } |
189 } | 189 } |
190 | 190 |
191 TEST(WebCryptoAesCbcTest, GenerateKeyBadLength) { | 191 TEST(WebCryptoAesCbcTest, GenerateKeyBadLength) { |
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 blink::WebCryptoKeyUsageEncrypt, &key)); |
199 } | 199 } |
200 } | 200 } |
201 | 201 |
| 202 TEST(WebCryptoAesCbcTest, ImportKeyEmptyUsage) { |
| 203 blink::WebCryptoKey key; |
| 204 ASSERT_EQ(Status::ErrorCreateKeyEmptyUsages(), |
| 205 ImportKey(blink::WebCryptoKeyFormatRaw, |
| 206 CryptoData(std::vector<uint8_t>(16)), |
| 207 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), |
| 208 true, 0, &key)); |
| 209 } |
| 210 |
202 // If key_ops is specified but empty, no key usages are allowed for the key. | 211 // If key_ops is specified but empty, no key usages are allowed for the key. |
203 TEST(WebCryptoAesCbcTest, ImportKeyJwkEmptyKeyOps) { | 212 TEST(WebCryptoAesCbcTest, ImportKeyJwkEmptyKeyOps) { |
204 blink::WebCryptoKey key; | 213 blink::WebCryptoKey key; |
205 base::DictionaryValue dict; | 214 base::DictionaryValue dict; |
206 dict.SetString("kty", "oct"); | 215 dict.SetString("kty", "oct"); |
207 dict.SetBoolean("ext", false); | 216 dict.SetBoolean("ext", false); |
208 dict.SetString("k", "GADWrMRHwQfoNaXU5fZvTg"); | 217 dict.SetString("k", "GADWrMRHwQfoNaXU5fZvTg"); |
209 dict.Set("key_ops", new base::ListValue); // Takes ownership. | 218 dict.Set("key_ops", new base::ListValue); // Takes ownership. |
210 | 219 |
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. | 220 // The JWK does not contain encrypt usages. |
219 EXPECT_EQ(Status::ErrorJwkKeyopsInconsistent(), | 221 EXPECT_EQ(Status::ErrorJwkKeyopsInconsistent(), |
220 ImportKeyJwkFromDict( | 222 ImportKeyJwkFromDict( |
221 dict, CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), false, | 223 dict, CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), false, |
222 blink::WebCryptoKeyUsageEncrypt, &key)); | 224 blink::WebCryptoKeyUsageEncrypt, &key)); |
223 | 225 |
224 // The JWK does not contain sign usage (nor is it applicable). | 226 // The JWK does not contain sign usage (nor is it applicable). |
225 EXPECT_EQ(Status::ErrorCreateKeyBadUsages(), | 227 EXPECT_EQ(Status::ErrorCreateKeyBadUsages(), |
226 ImportKeyJwkFromDict( | 228 ImportKeyJwkFromDict( |
227 dict, CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), false, | 229 dict, CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), false, |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 // Unwrap the key pair. | 528 // Unwrap the key pair. |
527 blink::WebCryptoAlgorithm rsa_import_algorithm = | 529 blink::WebCryptoAlgorithm rsa_import_algorithm = |
528 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 530 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
529 blink::WebCryptoAlgorithmIdSha256); | 531 blink::WebCryptoAlgorithmIdSha256); |
530 | 532 |
531 blink::WebCryptoKey unwrapped_public_key; | 533 blink::WebCryptoKey unwrapped_public_key; |
532 | 534 |
533 ASSERT_EQ( | 535 ASSERT_EQ( |
534 Status::Success(), | 536 Status::Success(), |
535 UnwrapKey(blink::WebCryptoKeyFormatSpki, CryptoData(wrapped_public_key), | 537 UnwrapKey(blink::WebCryptoKeyFormatSpki, CryptoData(wrapped_public_key), |
536 wrapping_key, wrap_algorithm, rsa_import_algorithm, true, 0, | 538 wrapping_key, wrap_algorithm, rsa_import_algorithm, true, |
537 &unwrapped_public_key)); | 539 blink::WebCryptoKeyUsageVerify, &unwrapped_public_key)); |
538 | 540 |
539 blink::WebCryptoKey unwrapped_private_key; | 541 blink::WebCryptoKey unwrapped_private_key; |
540 | 542 |
541 ASSERT_EQ( | 543 ASSERT_EQ( |
542 Status::Success(), | 544 Status::Success(), |
543 UnwrapKey(blink::WebCryptoKeyFormatPkcs8, CryptoData(wrapped_private_key), | 545 UnwrapKey(blink::WebCryptoKeyFormatPkcs8, CryptoData(wrapped_private_key), |
544 wrapping_key, wrap_algorithm, rsa_import_algorithm, true, | 546 wrapping_key, wrap_algorithm, rsa_import_algorithm, true, |
545 blink::WebCryptoKeyUsageSign, &unwrapped_private_key)); | 547 blink::WebCryptoKeyUsageSign, &unwrapped_private_key)); |
546 | 548 |
547 // Export unwrapped key pair as SPKI + PKCS8 | 549 // Export unwrapped key pair as SPKI + PKCS8 |
(...skipping 12 matching lines...) Expand all Loading... |
560 | 562 |
561 EXPECT_NE(public_key_spki, wrapped_public_key); | 563 EXPECT_NE(public_key_spki, wrapped_public_key); |
562 EXPECT_NE(private_key_pkcs8, wrapped_private_key); | 564 EXPECT_NE(private_key_pkcs8, wrapped_private_key); |
563 } | 565 } |
564 | 566 |
565 } // namespace | 567 } // namespace |
566 | 568 |
567 } // namespace webcrypto | 569 } // namespace webcrypto |
568 | 570 |
569 } // namespace content | 571 } // namespace content |
OLD | NEW |