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