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