| 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 "content/child/webcrypto/test/test_helpers.h" | 5 #include "content/child/webcrypto/test/test_helpers.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 EXPECT_EQ(algorithm.id(), key.algorithm().id()); | 360 EXPECT_EQ(algorithm.id(), key.algorithm().id()); |
| 361 EXPECT_EQ(extractable, key.extractable()); | 361 EXPECT_EQ(extractable, key.extractable()); |
| 362 EXPECT_EQ(usage, key.usages()); | 362 EXPECT_EQ(usage, key.usages()); |
| 363 return key; | 363 return key; |
| 364 } | 364 } |
| 365 | 365 |
| 366 void ImportRsaKeyPair(const std::vector<uint8_t>& spki_der, | 366 void ImportRsaKeyPair(const std::vector<uint8_t>& spki_der, |
| 367 const std::vector<uint8_t>& pkcs8_der, | 367 const std::vector<uint8_t>& pkcs8_der, |
| 368 const blink::WebCryptoAlgorithm& algorithm, | 368 const blink::WebCryptoAlgorithm& algorithm, |
| 369 bool extractable, | 369 bool extractable, |
| 370 blink::WebCryptoKeyUsageMask public_key_usage_mask, | 370 blink::WebCryptoKeyUsageMask public_key_usages, |
| 371 blink::WebCryptoKeyUsageMask private_key_usage_mask, | 371 blink::WebCryptoKeyUsageMask private_key_usages, |
| 372 blink::WebCryptoKey* public_key, | 372 blink::WebCryptoKey* public_key, |
| 373 blink::WebCryptoKey* private_key) { | 373 blink::WebCryptoKey* private_key) { |
| 374 ASSERT_EQ(Status::Success(), | 374 ASSERT_EQ(Status::Success(), |
| 375 ImportKey(blink::WebCryptoKeyFormatSpki, | 375 ImportKey(blink::WebCryptoKeyFormatSpki, |
| 376 CryptoData(spki_der), | 376 CryptoData(spki_der), |
| 377 algorithm, | 377 algorithm, |
| 378 true, | 378 true, |
| 379 public_key_usage_mask, | 379 public_key_usages, |
| 380 public_key)); | 380 public_key)); |
| 381 EXPECT_FALSE(public_key->isNull()); | 381 EXPECT_FALSE(public_key->isNull()); |
| 382 EXPECT_TRUE(public_key->handle()); | 382 EXPECT_TRUE(public_key->handle()); |
| 383 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key->type()); | 383 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key->type()); |
| 384 EXPECT_EQ(algorithm.id(), public_key->algorithm().id()); | 384 EXPECT_EQ(algorithm.id(), public_key->algorithm().id()); |
| 385 EXPECT_TRUE(public_key->extractable()); | 385 EXPECT_TRUE(public_key->extractable()); |
| 386 EXPECT_EQ(public_key_usage_mask, public_key->usages()); | 386 EXPECT_EQ(public_key_usages, public_key->usages()); |
| 387 | 387 |
| 388 ASSERT_EQ(Status::Success(), | 388 ASSERT_EQ(Status::Success(), |
| 389 ImportKey(blink::WebCryptoKeyFormatPkcs8, | 389 ImportKey(blink::WebCryptoKeyFormatPkcs8, |
| 390 CryptoData(pkcs8_der), | 390 CryptoData(pkcs8_der), |
| 391 algorithm, | 391 algorithm, |
| 392 extractable, | 392 extractable, |
| 393 private_key_usage_mask, | 393 private_key_usages, |
| 394 private_key)); | 394 private_key)); |
| 395 EXPECT_FALSE(private_key->isNull()); | 395 EXPECT_FALSE(private_key->isNull()); |
| 396 EXPECT_TRUE(private_key->handle()); | 396 EXPECT_TRUE(private_key->handle()); |
| 397 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key->type()); | 397 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key->type()); |
| 398 EXPECT_EQ(algorithm.id(), private_key->algorithm().id()); | 398 EXPECT_EQ(algorithm.id(), private_key->algorithm().id()); |
| 399 EXPECT_EQ(extractable, private_key->extractable()); | 399 EXPECT_EQ(extractable, private_key->extractable()); |
| 400 EXPECT_EQ(private_key_usage_mask, private_key->usages()); | 400 EXPECT_EQ(private_key_usages, private_key->usages()); |
| 401 } | 401 } |
| 402 | 402 |
| 403 Status ImportKeyJwkFromDict(const base::DictionaryValue& dict, | 403 Status ImportKeyJwkFromDict(const base::DictionaryValue& dict, |
| 404 const blink::WebCryptoAlgorithm& algorithm, | 404 const blink::WebCryptoAlgorithm& algorithm, |
| 405 bool extractable, | 405 bool extractable, |
| 406 blink::WebCryptoKeyUsageMask usage_mask, | 406 blink::WebCryptoKeyUsageMask usages, |
| 407 blink::WebCryptoKey* key) { | 407 blink::WebCryptoKey* key) { |
| 408 return ImportKey(blink::WebCryptoKeyFormatJwk, | 408 return ImportKey(blink::WebCryptoKeyFormatJwk, |
| 409 CryptoData(MakeJsonVector(dict)), | 409 CryptoData(MakeJsonVector(dict)), |
| 410 algorithm, | 410 algorithm, |
| 411 extractable, | 411 extractable, |
| 412 usage_mask, | 412 usages, |
| 413 key); | 413 key); |
| 414 } | 414 } |
| 415 | 415 |
| 416 scoped_ptr<base::DictionaryValue> GetJwkDictionary( | 416 scoped_ptr<base::DictionaryValue> GetJwkDictionary( |
| 417 const std::vector<uint8_t>& json) { | 417 const std::vector<uint8_t>& json) { |
| 418 base::StringPiece json_string( | 418 base::StringPiece json_string( |
| 419 reinterpret_cast<const char*>(vector_as_array(&json)), json.size()); | 419 reinterpret_cast<const char*>(vector_as_array(&json)), json.size()); |
| 420 base::Value* value = base::JSONReader::Read(json_string); | 420 base::Value* value = base::JSONReader::Read(json_string); |
| 421 EXPECT_TRUE(value); | 421 EXPECT_TRUE(value); |
| 422 base::DictionaryValue* dict_value = NULL; | 422 base::DictionaryValue* dict_value = NULL; |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 594 | 594 |
| 595 // Export the key in raw format and compare to the original. | 595 // Export the key in raw format and compare to the original. |
| 596 std::vector<uint8_t> key_raw_out; | 596 std::vector<uint8_t> key_raw_out; |
| 597 ASSERT_EQ(Status::Success(), | 597 ASSERT_EQ(Status::Success(), |
| 598 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_raw_out)); | 598 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_raw_out)); |
| 599 EXPECT_BYTES_EQ_HEX(key_hex, key_raw_out); | 599 EXPECT_BYTES_EQ_HEX(key_hex, key_raw_out); |
| 600 } | 600 } |
| 601 | 601 |
| 602 Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm, | 602 Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm, |
| 603 bool extractable, | 603 bool extractable, |
| 604 blink::WebCryptoKeyUsageMask usage_mask, | 604 blink::WebCryptoKeyUsageMask usages, |
| 605 blink::WebCryptoKey* key) { | 605 blink::WebCryptoKey* key) { |
| 606 GenerateKeyResult result; | 606 GenerateKeyResult result; |
| 607 Status status = GenerateKey(algorithm, extractable, usage_mask, &result); | 607 Status status = GenerateKey(algorithm, extractable, usages, &result); |
| 608 if (status.IsError()) | 608 if (status.IsError()) |
| 609 return status; | 609 return status; |
| 610 | 610 |
| 611 if (result.type() != GenerateKeyResult::TYPE_SECRET_KEY) | 611 if (result.type() != GenerateKeyResult::TYPE_SECRET_KEY) |
| 612 return Status::ErrorUnexpected(); | 612 return Status::ErrorUnexpected(); |
| 613 | 613 |
| 614 *key = result.secret_key(); | 614 *key = result.secret_key(); |
| 615 | 615 |
| 616 return Status::Success(); | 616 return Status::Success(); |
| 617 } | 617 } |
| 618 | 618 |
| 619 Status GenerateKeyPair(const blink::WebCryptoAlgorithm& algorithm, | 619 Status GenerateKeyPair(const blink::WebCryptoAlgorithm& algorithm, |
| 620 bool extractable, | 620 bool extractable, |
| 621 blink::WebCryptoKeyUsageMask usage_mask, | 621 blink::WebCryptoKeyUsageMask usages, |
| 622 blink::WebCryptoKey* public_key, | 622 blink::WebCryptoKey* public_key, |
| 623 blink::WebCryptoKey* private_key) { | 623 blink::WebCryptoKey* private_key) { |
| 624 GenerateKeyResult result; | 624 GenerateKeyResult result; |
| 625 Status status = GenerateKey(algorithm, extractable, usage_mask, &result); | 625 Status status = GenerateKey(algorithm, extractable, usages, &result); |
| 626 if (status.IsError()) | 626 if (status.IsError()) |
| 627 return status; | 627 return status; |
| 628 | 628 |
| 629 if (result.type() != GenerateKeyResult::TYPE_PUBLIC_PRIVATE_KEY_PAIR) | 629 if (result.type() != GenerateKeyResult::TYPE_PUBLIC_PRIVATE_KEY_PAIR) |
| 630 return Status::ErrorUnexpected(); | 630 return Status::ErrorUnexpected(); |
| 631 | 631 |
| 632 *public_key = result.public_key(); | 632 *public_key = result.public_key(); |
| 633 *private_key = result.private_key(); | 633 *private_key = result.private_key(); |
| 634 | 634 |
| 635 return Status::Success(); | 635 return Status::Success(); |
| 636 } | 636 } |
| 637 | 637 |
| 638 } // namespace webcrypto | 638 } // namespace webcrypto |
| 639 | 639 |
| 640 } // namesapce content | 640 } // namesapce content |
| OLD | NEW |