| 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 "jwk.h" |
| 6 |
| 5 #include <algorithm> | 7 #include <algorithm> |
| 6 #include <functional> | 8 #include <functional> |
| 7 #include <map> | 9 #include <map> |
| 10 |
| 8 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 9 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
| 10 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/strings/string_piece.h" |
| 11 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 12 #include "content/child/webcrypto/crypto_data.h" | 16 #include "content/child/webcrypto/crypto_data.h" |
| 13 #include "content/child/webcrypto/platform_crypto.h" | 17 #include "content/child/webcrypto/platform_crypto.h" |
| 14 #include "content/child/webcrypto/shared_crypto.h" | 18 #include "content/child/webcrypto/shared_crypto.h" |
| 15 #include "content/child/webcrypto/status.h" | 19 #include "content/child/webcrypto/status.h" |
| 16 #include "content/child/webcrypto/webcrypto_util.h" | 20 #include "content/child/webcrypto/webcrypto_util.h" |
| 17 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | 21 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
| 18 | 22 |
| 19 namespace content { | 23 namespace content { |
| 20 | 24 |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 return Status::Success(); | 296 return Status::Success(); |
| 293 } | 297 } |
| 294 | 298 |
| 295 // Returns true if the set bits in b make up a subset of the set bits in a. | 299 // Returns true if the set bits in b make up a subset of the set bits in a. |
| 296 bool ContainsKeyUsages(blink::WebCryptoKeyUsageMask a, | 300 bool ContainsKeyUsages(blink::WebCryptoKeyUsageMask a, |
| 297 blink::WebCryptoKeyUsageMask b) { | 301 blink::WebCryptoKeyUsageMask b) { |
| 298 return (a & b) == b; | 302 return (a & b) == b; |
| 299 } | 303 } |
| 300 | 304 |
| 301 // Writes a secret/symmetric key to a JWK dictionary. | 305 // Writes a secret/symmetric key to a JWK dictionary. |
| 302 void WriteSecretKey(const blink::WebArrayBuffer& raw_key, | 306 void WriteSecretKey(const std::vector<uint8>& raw_key, |
| 303 base::DictionaryValue* jwk_dict) { | 307 base::DictionaryValue* jwk_dict) { |
| 304 DCHECK(jwk_dict); | 308 DCHECK(jwk_dict); |
| 305 jwk_dict->SetString("kty", "oct"); | 309 jwk_dict->SetString("kty", "oct"); |
| 306 // For a secret/symmetric key, the only extra JWK field is 'k', containing the | 310 // For a secret/symmetric key, the only extra JWK field is 'k', containing the |
| 307 // base64url encoding of the raw key. | 311 // base64url encoding of the raw key. |
| 308 DCHECK(!raw_key.isNull()); | 312 const base::StringPiece key_str( |
| 309 DCHECK(raw_key.data()); | 313 reinterpret_cast<const char*>(Uint8VectorStart(raw_key)), raw_key.size()); |
| 310 DCHECK(raw_key.byteLength()); | |
| 311 unsigned int key_length_bytes = raw_key.byteLength(); | |
| 312 const base::StringPiece key_str(static_cast<const char*>(raw_key.data()), | |
| 313 key_length_bytes); | |
| 314 jwk_dict->SetString("k", Base64EncodeUrlSafe(key_str)); | 314 jwk_dict->SetString("k", Base64EncodeUrlSafe(key_str)); |
| 315 } | 315 } |
| 316 | 316 |
| 317 // Writes an RSA public key to a JWK dictionary | 317 // Writes an RSA public key to a JWK dictionary |
| 318 void WriteRsaPublicKey(const std::vector<uint8>& modulus, | 318 void WriteRsaPublicKey(const std::vector<uint8>& modulus, |
| 319 const std::vector<uint8>& public_exponent, | 319 const std::vector<uint8>& public_exponent, |
| 320 base::DictionaryValue* jwk_dict) { | 320 base::DictionaryValue* jwk_dict) { |
| 321 DCHECK(jwk_dict); | 321 DCHECK(jwk_dict); |
| 322 DCHECK(modulus.size()); | 322 DCHECK(modulus.size()); |
| 323 DCHECK(public_exponent.size()); | 323 DCHECK(public_exponent.size()); |
| (...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 784 CryptoData(jwk_n_value), | 784 CryptoData(jwk_n_value), |
| 785 CryptoData(jwk_e_value), | 785 CryptoData(jwk_e_value), |
| 786 key); | 786 key); |
| 787 | 787 |
| 788 } | 788 } |
| 789 | 789 |
| 790 return Status::ErrorJwkUnrecognizedKty(); | 790 return Status::ErrorJwkUnrecognizedKty(); |
| 791 } | 791 } |
| 792 | 792 |
| 793 Status ExportKeyJwk(const blink::WebCryptoKey& key, | 793 Status ExportKeyJwk(const blink::WebCryptoKey& key, |
| 794 blink::WebArrayBuffer* buffer) { | 794 std::vector<uint8>* buffer) { |
| 795 DCHECK(key.extractable()); | 795 DCHECK(key.extractable()); |
| 796 base::DictionaryValue jwk_dict; | 796 base::DictionaryValue jwk_dict; |
| 797 Status status = Status::OperationError(); | 797 Status status = Status::OperationError(); |
| 798 | 798 |
| 799 switch (key.type()) { | 799 switch (key.type()) { |
| 800 case blink::WebCryptoKeyTypeSecret: { | 800 case blink::WebCryptoKeyTypeSecret: { |
| 801 blink::WebArrayBuffer exported_key; | 801 std::vector<uint8> exported_key; |
| 802 status = ExportKey(blink::WebCryptoKeyFormatRaw, key, &exported_key); | 802 status = ExportKey(blink::WebCryptoKeyFormatRaw, key, &exported_key); |
| 803 if (status.IsError()) | 803 if (status.IsError()) |
| 804 return status; | 804 return status; |
| 805 WriteSecretKey(exported_key, &jwk_dict); | 805 WriteSecretKey(exported_key, &jwk_dict); |
| 806 break; | 806 break; |
| 807 } | 807 } |
| 808 case blink::WebCryptoKeyTypePublic: { | 808 case blink::WebCryptoKeyTypePublic: { |
| 809 // Currently only RSA public key export is supported. | 809 // Currently only RSA public key export is supported. |
| 810 if (!IsRsaPublicKey(key)) | 810 if (!IsRsaPublicKey(key)) |
| 811 return Status::ErrorUnsupported(); | 811 return Status::ErrorUnsupported(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 828 } | 828 } |
| 829 | 829 |
| 830 WriteKeyOps(key.usages(), &jwk_dict); | 830 WriteKeyOps(key.usages(), &jwk_dict); |
| 831 WriteExt(key.extractable(), &jwk_dict); | 831 WriteExt(key.extractable(), &jwk_dict); |
| 832 status = WriteAlg(key.algorithm(), &jwk_dict); | 832 status = WriteAlg(key.algorithm(), &jwk_dict); |
| 833 if (status.IsError()) | 833 if (status.IsError()) |
| 834 return status; | 834 return status; |
| 835 | 835 |
| 836 std::string json; | 836 std::string json; |
| 837 base::JSONWriter::Write(&jwk_dict, &json); | 837 base::JSONWriter::Write(&jwk_dict, &json); |
| 838 *buffer = CreateArrayBuffer(reinterpret_cast<const uint8*>(json.data()), | 838 buffer->assign(json.data(), json.data() + json.size()); |
| 839 json.size()); | |
| 840 return Status::Success(); | 839 return Status::Success(); |
| 841 } | 840 } |
| 842 | 841 |
| 843 } // namespace webcrypto | 842 } // namespace webcrypto |
| 844 | 843 |
| 845 } // namespace content | 844 } // namespace content |
| OLD | NEW |