| 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 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 | 158 |
| 159 base::LazyInstance<JwkAlgorithmRegistry> jwk_alg_registry = | 159 base::LazyInstance<JwkAlgorithmRegistry> jwk_alg_registry = |
| 160 LAZY_INSTANCE_INITIALIZER; | 160 LAZY_INSTANCE_INITIALIZER; |
| 161 | 161 |
| 162 bool ImportAlgorithmsConsistent(const blink::WebCryptoAlgorithm& alg1, | 162 bool ImportAlgorithmsConsistent(const blink::WebCryptoAlgorithm& alg1, |
| 163 const blink::WebCryptoAlgorithm& alg2) { | 163 const blink::WebCryptoAlgorithm& alg2) { |
| 164 DCHECK(!alg1.isNull()); | 164 DCHECK(!alg1.isNull()); |
| 165 DCHECK(!alg2.isNull()); | 165 DCHECK(!alg2.isNull()); |
| 166 if (alg1.id() != alg2.id()) | 166 if (alg1.id() != alg2.id()) |
| 167 return false; | 167 return false; |
| 168 if (alg1.paramsType() != alg2.paramsType()) | 168 // Inner hash algorithms must be compared too, but only if present. |
| 169 return false; | 169 if (alg1.paramsType() == |
| 170 switch (alg1.paramsType()) { | 170 blink::WebCryptoAlgorithmParamsTypeRsaHashedImportParams || |
| 171 case blink::WebCryptoAlgorithmParamsTypeNone: | 171 alg1.paramsType() == |
| 172 return true; | 172 blink::WebCryptoAlgorithmParamsTypeHmacImportParams) { |
| 173 case blink::WebCryptoAlgorithmParamsTypeRsaHashedImportParams: | 173 if (alg1.paramsType() != alg2.paramsType()) |
| 174 return ImportAlgorithmsConsistent(alg1.rsaHashedImportParams()->hash(), | |
| 175 alg2.rsaHashedImportParams()->hash()); | |
| 176 case blink::WebCryptoAlgorithmParamsTypeHmacImportParams: | |
| 177 return ImportAlgorithmsConsistent(alg1.hmacImportParams()->hash(), | |
| 178 alg2.hmacImportParams()->hash()); | |
| 179 default: | |
| 180 return false; | 174 return false; |
| 175 switch (alg1.paramsType()) { |
| 176 case blink::WebCryptoAlgorithmParamsTypeRsaHashedImportParams: |
| 177 return ImportAlgorithmsConsistent(alg1.rsaHashedImportParams()->hash(), |
| 178 alg2.rsaHashedImportParams()->hash()); |
| 179 case blink::WebCryptoAlgorithmParamsTypeHmacImportParams: |
| 180 return ImportAlgorithmsConsistent(alg1.hmacImportParams()->hash(), |
| 181 alg2.hmacImportParams()->hash()); |
| 182 default: |
| 183 return false; |
| 184 } |
| 181 } | 185 } |
| 186 return true; |
| 182 } | 187 } |
| 183 | 188 |
| 184 // Extracts the required string property with key |path| from |dict| and saves | 189 // Extracts the required string property with key |path| from |dict| and saves |
| 185 // the result to |*result|. If the property does not exist or is not a string, | 190 // the result to |*result|. If the property does not exist or is not a string, |
| 186 // returns an error. | 191 // returns an error. |
| 187 Status GetJwkString(base::DictionaryValue* dict, | 192 Status GetJwkString(base::DictionaryValue* dict, |
| 188 const std::string& path, | 193 const std::string& path, |
| 189 std::string* result) { | 194 std::string* result) { |
| 190 base::Value* value = NULL; | 195 base::Value* value = NULL; |
| 191 if (!dict->Get(path, &value)) | 196 if (!dict->Get(path, &value)) |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 } // namespace | 386 } // namespace |
| 382 | 387 |
| 383 Status ImportKeyJwk(const CryptoData& key_data, | 388 Status ImportKeyJwk(const CryptoData& key_data, |
| 384 const blink::WebCryptoAlgorithm& algorithm_or_null, | 389 const blink::WebCryptoAlgorithm& algorithm_or_null, |
| 385 bool extractable, | 390 bool extractable, |
| 386 blink::WebCryptoKeyUsageMask usage_mask, | 391 blink::WebCryptoKeyUsageMask usage_mask, |
| 387 blink::WebCryptoKey* key) { | 392 blink::WebCryptoKey* key) { |
| 388 // TODO(padolph): Generalize this comment to include export, and move to top | 393 // TODO(padolph): Generalize this comment to include export, and move to top |
| 389 // of file. | 394 // of file. |
| 390 | 395 |
| 391 // TODO(padolph): Generalize this comment to include export, and move to top | |
| 392 // of file. | |
| 393 | |
| 394 // The goal of this method is to extract key material and meta data from the | 396 // The goal of this method is to extract key material and meta data from the |
| 395 // incoming JWK, combine them with the input parameters, and ultimately import | 397 // incoming JWK, combine them with the input parameters, and ultimately import |
| 396 // a Web Crypto Key. | 398 // a Web Crypto Key. |
| 397 // | 399 // |
| 398 // JSON Web Key Format (JWK) | 400 // JSON Web Key Format (JWK) |
| 399 // http://tools.ietf.org/html/draft-ietf-jose-json-web-key-21 | 401 // http://tools.ietf.org/html/draft-ietf-jose-json-web-key-21 |
| 400 // | 402 // |
| 401 // A JWK is a simple JSON dictionary with the following entries | 403 // A JWK is a simple JSON dictionary with the following entries |
| 402 // - "kty" (Key Type) Parameter, REQUIRED | 404 // - "kty" (Key Type) Parameter, REQUIRED |
| 403 // - <kty-specific parameters, see below>, REQUIRED | 405 // - <kty-specific parameters, see below>, REQUIRED |
| (...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 763 std::string json; | 765 std::string json; |
| 764 base::JSONWriter::Write(&jwk_dict, &json); | 766 base::JSONWriter::Write(&jwk_dict, &json); |
| 765 *buffer = CreateArrayBuffer(reinterpret_cast<const uint8*>(json.data()), | 767 *buffer = CreateArrayBuffer(reinterpret_cast<const uint8*>(json.data()), |
| 766 json.size()); | 768 json.size()); |
| 767 return Status::Success(); | 769 return Status::Success(); |
| 768 } | 770 } |
| 769 | 771 |
| 770 } // namespace webcrypto | 772 } // namespace webcrypto |
| 771 | 773 |
| 772 } // namespace content | 774 } // namespace content |
| OLD | NEW |