OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "content/renderer/webcrypto/webcrypto_impl.h" | 5 #include "content/renderer/webcrypto/webcrypto_impl.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 #include <functional> |
| 9 #include <map> |
| 10 #include "base/json/json_reader.h" |
| 11 #include "base/lazy_instance.h" |
7 #include "base/logging.h" | 12 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
9 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | 14 #include "base/strings/string_piece.h" |
| 15 #include "base/values.h" |
| 16 #include "content/renderer/webcrypto/webcrypto_util.h" |
10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 17 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| 18 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
11 #include "third_party/WebKit/public/platform/WebCryptoKey.h" | 19 #include "third_party/WebKit/public/platform/WebCryptoKey.h" |
12 | 20 |
13 namespace content { | 21 namespace content { |
14 | 22 |
15 namespace { | 23 namespace { |
16 | 24 |
17 bool IsAlgorithmAsymmetric(const blink::WebCryptoAlgorithm& algorithm) { | 25 bool IsAlgorithmAsymmetric(const blink::WebCryptoAlgorithm& algorithm) { |
18 // TODO(padolph): include all other asymmetric algorithms once they are | 26 // TODO(padolph): include all other asymmetric algorithms once they are |
19 // defined, e.g. EC and DH. | 27 // defined, e.g. EC and DH. |
20 return (algorithm.id() == blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 || | 28 return (algorithm.id() == blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 || |
21 algorithm.id() == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || | 29 algorithm.id() == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || |
22 algorithm.id() == blink::WebCryptoAlgorithmIdRsaOaep); | 30 algorithm.id() == blink::WebCryptoAlgorithmIdRsaOaep); |
23 } | 31 } |
24 | 32 |
| 33 // Binds a specific key length value to a compatible factory function. |
| 34 typedef blink::WebCryptoAlgorithm (*AlgFactoryFuncWithOneShortArg)( |
| 35 unsigned short); |
| 36 template <AlgFactoryFuncWithOneShortArg func, unsigned short key_length> |
| 37 blink::WebCryptoAlgorithm BindAlgFactoryWithKeyLen() { |
| 38 return func(key_length); |
| 39 } |
| 40 |
| 41 // Binds a WebCryptoAlgorithmId value to a compatible factory function. |
| 42 typedef blink::WebCryptoAlgorithm (*AlgFactoryFuncWithWebCryptoAlgIdArg)( |
| 43 blink::WebCryptoAlgorithmId); |
| 44 template <AlgFactoryFuncWithWebCryptoAlgIdArg func, |
| 45 blink::WebCryptoAlgorithmId algorithm_id> |
| 46 blink::WebCryptoAlgorithm BindAlgFactoryAlgorithmId() { |
| 47 return func(algorithm_id); |
| 48 } |
| 49 |
| 50 // Defines a map between a JWK 'alg' string ID and a corresponding Web Crypto |
| 51 // factory function. |
| 52 typedef blink::WebCryptoAlgorithm (*AlgFactoryFuncNoArgs)(); |
| 53 typedef std::map<std::string, AlgFactoryFuncNoArgs> JwkAlgFactoryMap; |
| 54 |
| 55 class JwkAlgorithmFactoryMap { |
| 56 public: |
| 57 JwkAlgorithmFactoryMap() { |
| 58 map_["HS256"] = |
| 59 &BindAlgFactoryWithKeyLen<CreateHmacAlgorithmByHashOutputLen, 256>; |
| 60 map_["HS384"] = |
| 61 &BindAlgFactoryWithKeyLen<CreateHmacAlgorithmByHashOutputLen, 384>; |
| 62 map_["HS512"] = |
| 63 &BindAlgFactoryWithKeyLen<CreateHmacAlgorithmByHashOutputLen, 512>; |
| 64 map_["RS256"] = |
| 65 &BindAlgFactoryAlgorithmId<CreateRsaSsaAlgorithm, |
| 66 blink::WebCryptoAlgorithmIdSha256>; |
| 67 map_["RS384"] = |
| 68 &BindAlgFactoryAlgorithmId<CreateRsaSsaAlgorithm, |
| 69 blink::WebCryptoAlgorithmIdSha384>; |
| 70 map_["RS512"] = |
| 71 &BindAlgFactoryAlgorithmId<CreateRsaSsaAlgorithm, |
| 72 blink::WebCryptoAlgorithmIdSha512>; |
| 73 map_["RSA1_5"] = |
| 74 &BindAlgFactoryAlgorithmId<CreateAlgorithm, |
| 75 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5>; |
| 76 map_["RSA-OAEP"] = |
| 77 &BindAlgFactoryAlgorithmId<CreateRsaOaepAlgorithm, |
| 78 blink::WebCryptoAlgorithmIdSha1>; |
| 79 // TODO(padolph): The Web Crypto spec does not enumerate AES-KW 128 yet |
| 80 map_["A128KW"] = &blink::WebCryptoAlgorithm::createNull; |
| 81 // TODO(padolph): The Web Crypto spec does not enumerate AES-KW 256 yet |
| 82 map_["A256KW"] = &blink::WebCryptoAlgorithm::createNull; |
| 83 map_["A128GCM"] = |
| 84 &BindAlgFactoryAlgorithmId<CreateAlgorithm, |
| 85 blink::WebCryptoAlgorithmIdAesGcm>; |
| 86 map_["A256GCM"] = |
| 87 &BindAlgFactoryAlgorithmId<CreateAlgorithm, |
| 88 blink::WebCryptoAlgorithmIdAesGcm>; |
| 89 map_["A128CBC"] = |
| 90 &BindAlgFactoryAlgorithmId<CreateAlgorithm, |
| 91 blink::WebCryptoAlgorithmIdAesCbc>; |
| 92 map_["A192CBC"] = |
| 93 &BindAlgFactoryAlgorithmId<CreateAlgorithm, |
| 94 blink::WebCryptoAlgorithmIdAesCbc>; |
| 95 map_["A256CBC"] = |
| 96 &BindAlgFactoryAlgorithmId<CreateAlgorithm, |
| 97 blink::WebCryptoAlgorithmIdAesCbc>; |
| 98 } |
| 99 |
| 100 blink::WebCryptoAlgorithm CreateAlgorithmFromName( |
| 101 const std::string& alg_id) const { |
| 102 const JwkAlgFactoryMap::const_iterator pos = map_.find(alg_id); |
| 103 if (pos == map_.end()) |
| 104 return blink::WebCryptoAlgorithm::createNull(); |
| 105 return pos->second(); |
| 106 } |
| 107 |
| 108 private: |
| 109 JwkAlgFactoryMap map_; |
| 110 }; |
| 111 |
| 112 base::LazyInstance<JwkAlgorithmFactoryMap> jwk_alg_factory = |
| 113 LAZY_INSTANCE_INITIALIZER; |
| 114 |
| 115 // TODO(padolph): Verify this logic is sufficient to judge algorithm |
| 116 // "consistency" for JWK import, and for all supported algorithms. |
| 117 bool WebCryptoAlgorithmsConsistent(const blink::WebCryptoAlgorithm& alg1, |
| 118 const blink::WebCryptoAlgorithm& alg2) { |
| 119 DCHECK(!alg1.isNull()); |
| 120 DCHECK(!alg2.isNull()); |
| 121 if (alg1.id() == alg2.id()) { |
| 122 if (alg1.id() == blink::WebCryptoAlgorithmIdHmac || |
| 123 alg1.id() == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || |
| 124 alg1.id() == blink::WebCryptoAlgorithmIdRsaOaep) { |
| 125 if (WebCryptoAlgorithmsConsistent(GetInnerHashAlgorithm(alg1), |
| 126 GetInnerHashAlgorithm(alg2))) { |
| 127 return true; |
| 128 } |
| 129 return false; |
| 130 } |
| 131 return true; |
| 132 } |
| 133 return false; |
| 134 } |
| 135 |
25 } // namespace | 136 } // namespace |
26 | 137 |
27 WebCryptoImpl::WebCryptoImpl() { | 138 WebCryptoImpl::WebCryptoImpl() { |
28 Init(); | 139 Init(); |
29 } | 140 } |
30 | 141 |
31 // static | |
32 // TODO(eroman): This works by re-allocating a new buffer. It would be better if | |
33 // the WebArrayBuffer could just be truncated instead. | |
34 void WebCryptoImpl::ShrinkBuffer( | |
35 blink::WebArrayBuffer* buffer, | |
36 unsigned new_size) { | |
37 DCHECK_LE(new_size, buffer->byteLength()); | |
38 | |
39 if (new_size == buffer->byteLength()) | |
40 return; | |
41 | |
42 blink::WebArrayBuffer new_buffer = | |
43 blink::WebArrayBuffer::create(new_size, 1); | |
44 DCHECK(!new_buffer.isNull()); | |
45 memcpy(new_buffer.data(), buffer->data(), new_size); | |
46 *buffer = new_buffer; | |
47 } | |
48 | |
49 void WebCryptoImpl::encrypt( | 142 void WebCryptoImpl::encrypt( |
50 const blink::WebCryptoAlgorithm& algorithm, | 143 const blink::WebCryptoAlgorithm& algorithm, |
51 const blink::WebCryptoKey& key, | 144 const blink::WebCryptoKey& key, |
52 const unsigned char* data, | 145 const unsigned char* data, |
53 unsigned data_size, | 146 unsigned data_size, |
54 blink::WebCryptoResult result) { | 147 blink::WebCryptoResult result) { |
55 DCHECK(!algorithm.isNull()); | 148 DCHECK(!algorithm.isNull()); |
56 blink::WebArrayBuffer buffer; | 149 blink::WebArrayBuffer buffer; |
57 if (!EncryptInternal(algorithm, key, data, data_size, &buffer)) { | 150 if (!EncryptInternal(algorithm, key, data, data_size, &buffer)) { |
58 result.completeWithError(); | 151 result.completeWithError(); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 | 226 |
134 void WebCryptoImpl::importKey( | 227 void WebCryptoImpl::importKey( |
135 blink::WebCryptoKeyFormat format, | 228 blink::WebCryptoKeyFormat format, |
136 const unsigned char* key_data, | 229 const unsigned char* key_data, |
137 unsigned key_data_size, | 230 unsigned key_data_size, |
138 const blink::WebCryptoAlgorithm& algorithm_or_null, | 231 const blink::WebCryptoAlgorithm& algorithm_or_null, |
139 bool extractable, | 232 bool extractable, |
140 blink::WebCryptoKeyUsageMask usage_mask, | 233 blink::WebCryptoKeyUsageMask usage_mask, |
141 blink::WebCryptoResult result) { | 234 blink::WebCryptoResult result) { |
142 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 235 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
143 if (!ImportKeyInternal(format, | 236 if (format == blink::WebCryptoKeyFormatJwk) { |
144 key_data, | 237 if (!ImportKeyJwk(key_data, |
145 key_data_size, | 238 key_data_size, |
146 algorithm_or_null, | 239 algorithm_or_null, |
147 extractable, | 240 extractable, |
148 usage_mask, | 241 usage_mask, |
149 &key)) { | 242 &key)) { |
150 result.completeWithError(); | 243 result.completeWithError(); |
151 return; | 244 } |
| 245 } else { |
| 246 if (!ImportKeyInternal(format, |
| 247 key_data, |
| 248 key_data_size, |
| 249 algorithm_or_null, |
| 250 extractable, |
| 251 usage_mask, |
| 252 &key)) { |
| 253 result.completeWithError(); |
| 254 } |
152 } | 255 } |
153 DCHECK(key.handle()); | 256 DCHECK(key.handle()); |
154 DCHECK(!key.algorithm().isNull()); | 257 DCHECK(!key.algorithm().isNull()); |
155 DCHECK_EQ(extractable, key.extractable()); | 258 DCHECK_EQ(extractable, key.extractable()); |
156 result.completeWithKey(key); | 259 result.completeWithKey(key); |
157 } | 260 } |
158 | 261 |
159 void WebCryptoImpl::sign( | 262 void WebCryptoImpl::sign( |
160 const blink::WebCryptoAlgorithm& algorithm, | 263 const blink::WebCryptoAlgorithm& algorithm, |
161 const blink::WebCryptoKey& key, | 264 const blink::WebCryptoKey& key, |
(...skipping 25 matching lines...) Expand all Loading... |
187 signature_size, | 290 signature_size, |
188 data, | 291 data, |
189 data_size, | 292 data_size, |
190 &signature_match)) { | 293 &signature_match)) { |
191 result.completeWithError(); | 294 result.completeWithError(); |
192 } else { | 295 } else { |
193 result.completeWithBoolean(signature_match); | 296 result.completeWithBoolean(signature_match); |
194 } | 297 } |
195 } | 298 } |
196 | 299 |
| 300 bool WebCryptoImpl::ImportKeyJwk( |
| 301 const unsigned char* key_data, |
| 302 unsigned key_data_size, |
| 303 const blink::WebCryptoAlgorithm& algorithm_or_null, |
| 304 bool extractable, |
| 305 blink::WebCryptoKeyUsageMask usage_mask, |
| 306 blink::WebCryptoKey* key) { |
| 307 |
| 308 // The goal of this method is to extract key material and meta data from the |
| 309 // incoming JWK, combine them with the input parameters, and ultimately import |
| 310 // a Web Crypto Key. |
| 311 // |
| 312 // JSON Web Key Format (JWK) |
| 313 // http://tools.ietf.org/html/draft-ietf-jose-json-web-key-16 |
| 314 // TODO(padolph): Not all possible values are handled by this code right now |
| 315 // |
| 316 // A JWK is a simple JSON dictionary with the following entries |
| 317 // - "kty" (Key Type) Parameter, REQUIRED |
| 318 // - <kty-specific parameters, see below>, REQUIRED |
| 319 // - "use" (Key Use) Parameter, OPTIONAL |
| 320 // - "alg" (Algorithm) Parameter, OPTIONAL |
| 321 // - "extractable" (Key Exportability), OPTIONAL [NOTE: not yet part of JOSE] |
| 322 // (all other entries are ignored) |
| 323 // |
| 324 // OPTIONAL here means that this code does not require the entry to be present |
| 325 // in the incoming JWK, because the method input parameters contain similar |
| 326 // information. If the optional JWK entry is present, it will be validated |
| 327 // against the corresponding input parameter for consistency and combined with |
| 328 // it according to rules defined below. A special case is that the method |
| 329 // input parameter 'algorithm' is also optional. If it is null, the JWK 'alg' |
| 330 // value (if present) is used as a fallback. |
| 331 // |
| 332 // Input 'key_data' contains the JWK. To build a Web Crypto Key, the JWK |
| 333 // values are parsed out and combined with the method input parameters to |
| 334 // build a Web Crypto Key: |
| 335 // Web Crypto Key type <-- (deduced) |
| 336 // Web Crypto Key extractable <-- JWK extractable + input extractable |
| 337 // Web Crypto Key algorithm <-- JWK alg + input algorithm |
| 338 // Web Crypto Key keyUsage <-- JWK use + input usage_mask |
| 339 // Web Crypto Key keying material <-- kty-specific parameters |
| 340 // |
| 341 // Values for each JWK entry are case-sensitive and defined in |
| 342 // http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-16. |
| 343 // Note that not all values specified by JOSE are handled by this code. Only |
| 344 // handled values are listed. |
| 345 // - kty (Key Type) |
| 346 // +-------+--------------------------------------------------------------+ |
| 347 // | "RSA" | RSA [RFC3447] | |
| 348 // | "oct" | Octet sequence (used to represent symmetric keys) | |
| 349 // +-------+--------------------------------------------------------------+ |
| 350 // - use (Key Use) |
| 351 // +-------+--------------------------------------------------------------+ |
| 352 // | "enc" | encrypt and decrypt operations | |
| 353 // | "sig" | sign and verify (MAC) operations | |
| 354 // | "wrap"| key wrap and unwrap [not yet part of JOSE] | |
| 355 // +-------+--------------------------------------------------------------+ |
| 356 // - extractable (Key Exportability) |
| 357 // +-------+--------------------------------------------------------------+ |
| 358 // | true | Key may be exported from the trusted environment | |
| 359 // | false | Key cannot exit the trusted environment | |
| 360 // +-------+--------------------------------------------------------------+ |
| 361 // - alg (Algorithm) |
| 362 // See http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-16 |
| 363 // +--------------+-------------------------------------------------------+ |
| 364 // | Digital Signature or MAC Algorithm | |
| 365 // +--------------+-------------------------------------------------------+ |
| 366 // | "HS256" | HMAC using SHA-256 hash algorithm | |
| 367 // | "HS384" | HMAC using SHA-384 hash algorithm | |
| 368 // | "HS512" | HMAC using SHA-512 hash algorithm | |
| 369 // | "RS256" | RSASSA using SHA-256 hash algorithm | |
| 370 // | "RS384" | RSASSA using SHA-384 hash algorithm | |
| 371 // | "RS512" | RSASSA using SHA-512 hash algorithm | |
| 372 // +--------------+-------------------------------------------------------| |
| 373 // | Key Management Algorithm | |
| 374 // +--------------+-------------------------------------------------------+ |
| 375 // | "RSA1_5" | RSAES-PKCS1-V1_5 [RFC3447] | |
| 376 // | "RSA-OAEP" | RSAES using Optimal Asymmetric Encryption Padding | |
| 377 // | | (OAEP) [RFC3447], with the default parameters | |
| 378 // | | specified by RFC3447 in Section A.2.1 | |
| 379 // | "A128KW" | Advanced Encryption Standard (AES) Key Wrap Algorithm | |
| 380 // | | [RFC3394] using 128 bit keys | |
| 381 // | "A256KW" | AES Key Wrap Algorithm using 256 bit keys | |
| 382 // | "A128GCM" | AES in Galois/Counter Mode (GCM) [NIST.800-38D] using | |
| 383 // | | 128 bit keys | |
| 384 // | "A256GCM" | AES GCM using 256 bit keys | |
| 385 // | "A128CBC" | AES in Cipher Block Chaining Mode (CBC) with PKCS #5 | |
| 386 // | | padding [NIST.800-38A] [not yet part of JOSE] | |
| 387 // | "A192CBC" | AES CBC using 192 bit keys [not yet part of JOSE] | |
| 388 // | "A256CBC" | AES CBC using 256 bit keys [not yet part of JOSE] | |
| 389 // +--------------+-------------------------------------------------------+ |
| 390 // |
| 391 // kty-specific parameters |
| 392 // The value of kty determines the type and content of the keying material |
| 393 // carried in the JWK to be imported. Currently only two possibilities are |
| 394 // supported: a raw key or an RSA public key. RSA private keys are not |
| 395 // supported because typical applications seldom need to import a private key, |
| 396 // and the large number of JWK parameters required to describe one. |
| 397 // - kty == "oct" (symmetric or other raw key) |
| 398 // +-------+--------------------------------------------------------------+ |
| 399 // | "k" | Contains the value of the symmetric (or other single-valued) | |
| 400 // | | key. It is represented as the base64url encoding of the | |
| 401 // | | octet sequence containing the key value. | |
| 402 // +-------+--------------------------------------------------------------+ |
| 403 // - kty == "RSA" (RSA public key) |
| 404 // +-------+--------------------------------------------------------------+ |
| 405 // | "n" | Contains the modulus value for the RSA public key. It is | |
| 406 // | | represented as the base64url encoding of the value's | |
| 407 // | | unsigned big endian representation as an octet sequence. | |
| 408 // +-------+--------------------------------------------------------------+ |
| 409 // | "e" | Contains the exponent value for the RSA public key. It is | |
| 410 // | | represented as the base64url encoding of the value's | |
| 411 // | | unsigned big endian representation as an octet sequence. | |
| 412 // +-------+--------------------------------------------------------------+ |
| 413 // |
| 414 // Consistency and conflict resolution |
| 415 // The 'algorithm_or_null', 'extractable', and 'usage_mask' input parameters |
| 416 // may be different than the corresponding values inside the JWK. The Web |
| 417 // Crypto spec says that if a JWK value is present but is inconsistent with |
| 418 // the input value, it is an error and the operation must fail. If no |
| 419 // inconsistency is found, the input and JWK values are combined as follows: |
| 420 // |
| 421 // algorithm |
| 422 // If an algorithm is provided by both the input parameter and the JWK, |
| 423 // consistency between the two is based only on algorithm ID's (including an |
| 424 // inner hash algorithm if present). In this case if the consistency |
| 425 // check is passed, the input algorithm is used. If only one of either the |
| 426 // input algorithm and JWK alg is provided, it is used as the final |
| 427 // algorithm. |
| 428 // |
| 429 // extractable |
| 430 // If the JWK extractable is true but the input parameter is false, make the |
| 431 // Web Crypto Key non-extractable. Conversely, if the JWK extractable is |
| 432 // false but the input parameter is true, it is an inconsistency. If both |
| 433 // are true or both are false, use that value. |
| 434 // |
| 435 // usage_mask |
| 436 // The input usage_mask must be a strict subset of the interpreted JWK use |
| 437 // value, else it is judged inconsistent. In all cases the input usage_mask |
| 438 // is used as the final usage_mask. |
| 439 // |
| 440 |
| 441 if (!key_data_size) |
| 442 return false; |
| 443 DCHECK(key); |
| 444 |
| 445 // Parse the incoming JWK JSON. |
| 446 base::StringPiece json_string(reinterpret_cast<const char*>(key_data), |
| 447 key_data_size); |
| 448 scoped_ptr<base::Value> value(base::JSONReader::Read(json_string)); |
| 449 // Note, bare pointer dict_value is ok since it points into scoped value. |
| 450 base::DictionaryValue* dict_value = NULL; |
| 451 if (!value.get() || !value->GetAsDictionary(&dict_value) || !dict_value) |
| 452 return false; |
| 453 |
| 454 // JWK "kty". Exit early if this required JWK parameter is missing. |
| 455 std::string jwk_kty_value; |
| 456 if (!dict_value->GetString("kty", &jwk_kty_value)) |
| 457 return false; |
| 458 |
| 459 // JWK "extractable" (optional) --> extractable parameter |
| 460 { |
| 461 bool jwk_extractable_value; |
| 462 if (dict_value->GetBoolean("extractable", &jwk_extractable_value)) { |
| 463 if (!jwk_extractable_value && extractable) |
| 464 return false; |
| 465 extractable = extractable && jwk_extractable_value; |
| 466 } |
| 467 } |
| 468 |
| 469 // JWK "alg" (optional) --> algorithm parameter |
| 470 // Note: input algorithm is also optional, so we have six cases to handle. |
| 471 // 1. JWK alg present but unrecognized: error |
| 472 // 2. JWK alg valid AND input algorithm isNull: use JWK value |
| 473 // 3. JWK alg valid AND input algorithm specified, but JWK value |
| 474 // inconsistent with input: error |
| 475 // 4. JWK alg valid AND input algorithm specified, both consistent: use |
| 476 // input value (because it has potentially more details) |
| 477 // 5. JWK alg missing AND input algorithm isNull: error |
| 478 // 6. JWK alg missing AND input algorithm specified: use input value |
| 479 blink::WebCryptoAlgorithm algorithm = |
| 480 blink::WebCryptoAlgorithm::createNull(); |
| 481 std::string jwk_alg_value; |
| 482 if (dict_value->GetString("alg", &jwk_alg_value)) { |
| 483 // JWK alg present |
| 484 const blink::WebCryptoAlgorithm jwk_algorithm = |
| 485 jwk_alg_factory.Get().CreateAlgorithmFromName(jwk_alg_value); |
| 486 if (jwk_algorithm.isNull()) { |
| 487 // JWK alg unrecognized |
| 488 return false; // case 1 |
| 489 } |
| 490 // JWK alg valid |
| 491 if (algorithm_or_null.isNull()) { |
| 492 // input algorithm not specified |
| 493 algorithm = jwk_algorithm; // case 2 |
| 494 } else { |
| 495 // input algorithm specified |
| 496 if (!WebCryptoAlgorithmsConsistent(jwk_algorithm, algorithm_or_null)) |
| 497 return false; // case 3 |
| 498 algorithm = algorithm_or_null; // case 4 |
| 499 } |
| 500 } else { |
| 501 // JWK alg missing |
| 502 if (algorithm_or_null.isNull()) |
| 503 return false; // case 5 |
| 504 algorithm = algorithm_or_null; // case 6 |
| 505 } |
| 506 DCHECK(!algorithm.isNull()); |
| 507 |
| 508 // JWK "use" (optional) --> usage_mask parameter |
| 509 std::string jwk_use_value; |
| 510 if (dict_value->GetString("use", &jwk_use_value)) { |
| 511 blink::WebCryptoKeyUsageMask jwk_usage_mask = 0; |
| 512 if (jwk_use_value == "enc") { |
| 513 jwk_usage_mask = |
| 514 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt; |
| 515 } else if (jwk_use_value == "sig") { |
| 516 jwk_usage_mask = |
| 517 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify; |
| 518 } else if (jwk_use_value == "wrap") { |
| 519 jwk_usage_mask = |
| 520 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey; |
| 521 } else { |
| 522 return false; |
| 523 } |
| 524 if ((jwk_usage_mask & usage_mask) != usage_mask) { |
| 525 // A usage_mask must be a subset of jwk_usage_mask. |
| 526 return false; |
| 527 } |
| 528 } |
| 529 |
| 530 // JWK keying material --> ImportKeyInternal() |
| 531 if (jwk_kty_value == "oct") { |
| 532 std::string jwk_k_value_url64; |
| 533 if (!dict_value->GetString("k", &jwk_k_value_url64)) |
| 534 return false; |
| 535 std::string jwk_k_value; |
| 536 if (!Base64DecodeUrlSafe(jwk_k_value_url64, &jwk_k_value) || |
| 537 !jwk_k_value.size()) { |
| 538 return false; |
| 539 } |
| 540 |
| 541 // TODO(padolph): Some JWK alg ID's embed information about the key length |
| 542 // in the alg ID string. For example "A128" implies the JWK carries 128 bits |
| 543 // of key material, and "HS512" implies the JWK carries _at least_ 512 bits |
| 544 // of key material. For such keys validate the actual key length against the |
| 545 // value in the ID. |
| 546 |
| 547 return ImportKeyInternal(blink::WebCryptoKeyFormatRaw, |
| 548 reinterpret_cast<const uint8*>(jwk_k_value.data()), |
| 549 jwk_k_value.size(), |
| 550 algorithm, |
| 551 extractable, |
| 552 usage_mask, |
| 553 key); |
| 554 } else if (jwk_kty_value == "RSA") { |
| 555 // TODO(padolph): JWK import RSA public key |
| 556 return false; |
| 557 } else { |
| 558 return false; |
| 559 } |
| 560 |
| 561 return true; |
| 562 } |
| 563 |
197 } // namespace content | 564 } // namespace content |
OLD | NEW |