| 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <functional> | 6 #include <functional> |
| 7 #include <map> | 7 #include <map> |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 681 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 692 algorithm_info->IsInvalidKeyByteLength(jwk_k_value.size())) { | 692 algorithm_info->IsInvalidKeyByteLength(jwk_k_value.size())) { |
| 693 return Status::ErrorJwkIncorrectKeyLength(); | 693 return Status::ErrorJwkIncorrectKeyLength(); |
| 694 } | 694 } |
| 695 | 695 |
| 696 return ImportKey(blink::WebCryptoKeyFormatRaw, | 696 return ImportKey(blink::WebCryptoKeyFormatRaw, |
| 697 CryptoData(jwk_k_value), | 697 CryptoData(jwk_k_value), |
| 698 algorithm, | 698 algorithm, |
| 699 extractable, | 699 extractable, |
| 700 usage_mask, | 700 usage_mask, |
| 701 key); | 701 key); |
| 702 } else if (jwk_kty_value == "RSA") { | 702 } |
| 703 if (jwk_kty_value == "RSA") { |
| 703 // An RSA public key must have an "n" (modulus) and an "e" (exponent) entry | 704 // An RSA public key must have an "n" (modulus) and an "e" (exponent) entry |
| 704 // in the JWK, while an RSA private key must have those, plus at least a "d" | 705 // in the JWK, while an RSA private key must have those, plus at least a "d" |
| 705 // (private exponent) entry. | 706 // (private exponent) entry. |
| 706 // See http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-18, | 707 // See http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-18, |
| 707 // section 6.3. | 708 // section 6.3. |
| 708 | 709 |
| 709 // RSA private key import is not currently supported, so fail here if a "d" | 710 // RSA private key import is not currently supported, so fail here if a "d" |
| 710 // entry is found. | 711 // entry is found. |
| 711 // TODO(padolph): Support RSA private key import. | 712 // TODO(padolph): Support RSA private key import. |
| 712 if (dict_value->HasKey("d")) | 713 if (dict_value->HasKey("d")) |
| 713 return Status::ErrorJwkRsaPrivateKeyUnsupported(); | 714 return Status::ErrorJwkRsaPrivateKeyUnsupported(); |
| 714 | 715 |
| 715 std::string jwk_n_value; | 716 std::string jwk_n_value; |
| 716 status = GetJwkBytes(dict_value, "n", &jwk_n_value); | 717 status = GetJwkBytes(dict_value, "n", &jwk_n_value); |
| 717 if (status.IsError()) | 718 if (status.IsError()) |
| 718 return status; | 719 return status; |
| 719 std::string jwk_e_value; | 720 std::string jwk_e_value; |
| 720 status = GetJwkBytes(dict_value, "e", &jwk_e_value); | 721 status = GetJwkBytes(dict_value, "e", &jwk_e_value); |
| 721 if (status.IsError()) | 722 if (status.IsError()) |
| 722 return status; | 723 return status; |
| 723 | 724 |
| 724 return platform::ImportRsaPublicKey(algorithm, | 725 return platform::ImportRsaPublicKey(algorithm, |
| 725 extractable, | 726 extractable, |
| 726 usage_mask, | 727 usage_mask, |
| 727 CryptoData(jwk_n_value), | 728 CryptoData(jwk_n_value), |
| 728 CryptoData(jwk_e_value), | 729 CryptoData(jwk_e_value), |
| 729 key); | 730 key); |
| 730 | 731 |
| 731 } else { | |
| 732 return Status::ErrorJwkUnrecognizedKty(); | |
| 733 } | 732 } |
| 734 | 733 |
| 735 return Status::Success(); | 734 return Status::ErrorJwkUnrecognizedKty(); |
| 736 } | 735 } |
| 737 | 736 |
| 738 Status ExportKeyJwk(const blink::WebCryptoKey& key, | 737 Status ExportKeyJwk(const blink::WebCryptoKey& key, |
| 739 blink::WebArrayBuffer* buffer) { | 738 blink::WebArrayBuffer* buffer) { |
| 740 base::DictionaryValue jwk_dict; | 739 base::DictionaryValue jwk_dict; |
| 741 Status status = Status::Error(); | 740 Status status = Status::Error(); |
| 742 blink::WebArrayBuffer exported_key; | 741 blink::WebArrayBuffer exported_key; |
| 743 | 742 |
| 744 if (key.type() == blink::WebCryptoKeyTypeSecret) { | 743 if (key.type() == blink::WebCryptoKeyTypeSecret) { |
| 745 status = ExportKey(blink::WebCryptoKeyFormatRaw, key, &exported_key); | 744 status = ExportKey(blink::WebCryptoKeyFormatRaw, key, &exported_key); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 760 std::string json; | 759 std::string json; |
| 761 base::JSONWriter::Write(&jwk_dict, &json); | 760 base::JSONWriter::Write(&jwk_dict, &json); |
| 762 *buffer = CreateArrayBuffer(reinterpret_cast<const uint8*>(json.data()), | 761 *buffer = CreateArrayBuffer(reinterpret_cast<const uint8*>(json.data()), |
| 763 json.size()); | 762 json.size()); |
| 764 return Status::Success(); | 763 return Status::Success(); |
| 765 } | 764 } |
| 766 | 765 |
| 767 } // namespace webcrypto | 766 } // namespace webcrypto |
| 768 | 767 |
| 769 } // namespace content | 768 } // namespace content |
| OLD | NEW |