Chromium Code Reviews| 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 <sstream> | |
|
eroman
2013/11/07 20:54:37
remove
padolph
2013/11/09 00:33:38
Done.
| |
| 11 #include "base/json/json_reader.h" | |
| 12 #include "base/lazy_instance.h" | |
| 7 #include "base/logging.h" | 13 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 9 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | 15 #include "base/strings/string_piece.h" |
| 16 #include "base/values.h" | |
| 17 #include "content/renderer/webcrypto/webcrypto_util.h" | |
| 10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 18 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| 19 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | |
| 11 #include "third_party/WebKit/public/platform/WebCryptoKey.h" | 20 #include "third_party/WebKit/public/platform/WebCryptoKey.h" |
| 12 | 21 |
| 13 namespace content { | 22 namespace content { |
| 14 | 23 |
| 15 namespace { | 24 namespace { |
| 16 | 25 |
| 17 bool IsAlgorithmAsymmetric(const WebKit::WebCryptoAlgorithm& algorithm) { | 26 bool IsAlgorithmAsymmetric(const WebKit::WebCryptoAlgorithm& algorithm) { |
| 18 // TODO(padolph): include all other asymmetric algorithms once they are | 27 // TODO(padolph): include all other asymmetric algorithms once they are |
| 19 // defined, e.g. EC and DH. | 28 // defined, e.g. EC and DH. |
| 20 return (algorithm.id() == WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 || | 29 return (algorithm.id() == WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 || |
| 21 algorithm.id() == WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || | 30 algorithm.id() == WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || |
| 22 algorithm.id() == WebKit::WebCryptoAlgorithmIdRsaOaep); | 31 algorithm.id() == WebKit::WebCryptoAlgorithmIdRsaOaep); |
| 23 } | 32 } |
| 24 | 33 |
| 34 // Binds a specific key length value to a compatible factory function. | |
| 35 typedef WebKit::WebCryptoAlgorithm (*AlgFactoryFuncWithOneShortArg)( | |
| 36 unsigned short); | |
| 37 template <AlgFactoryFuncWithOneShortArg func, unsigned short key_length> | |
| 38 WebKit::WebCryptoAlgorithm BindAlgFactoryWithKeyLen() { | |
| 39 return func(key_length); | |
| 40 } | |
| 41 | |
| 42 // Binds a WebCryptoAlgorithmId value to a compatible factory function. | |
| 43 typedef WebKit::WebCryptoAlgorithm (*AlgFactoryFuncWithWebCryptoAlgIdArg)( | |
| 44 WebKit::WebCryptoAlgorithmId); | |
| 45 template <AlgFactoryFuncWithWebCryptoAlgIdArg func, | |
| 46 WebKit::WebCryptoAlgorithmId algorithm_id> | |
| 47 WebKit::WebCryptoAlgorithm BindAlgFactoryAlgorithmId() { | |
| 48 return func(algorithm_id); | |
| 49 } | |
| 50 | |
| 51 // Defines a map between a JWK 'alg' string ID and a corresponding Web Crypto | |
| 52 // factory function. | |
| 53 typedef WebKit::WebCryptoAlgorithm (*AlgFactoryFuncNoArgs)(); | |
| 54 typedef std::map<std::string, AlgFactoryFuncNoArgs> JwkAlgFactoryMap; | |
| 55 | |
| 56 class JwkAlgorithmFactoryMap { | |
| 57 public: | |
| 58 JwkAlgorithmFactoryMap() { | |
| 59 map_["HS256"] = | |
| 60 &BindAlgFactoryWithKeyLen<CreateHmacAlgorithmByDigestLen, 256>; | |
| 61 map_["HS384"] = | |
| 62 &BindAlgFactoryWithKeyLen<CreateHmacAlgorithmByDigestLen, 384>; | |
| 63 map_["HS512"] = | |
| 64 &BindAlgFactoryWithKeyLen<CreateHmacAlgorithmByDigestLen, 512>; | |
| 65 map_["RS256"] = | |
| 66 &BindAlgFactoryAlgorithmId<CreateRsaSsaAlgorithm, | |
| 67 WebKit::WebCryptoAlgorithmIdSha256>; | |
| 68 map_["RS384"] = | |
| 69 &BindAlgFactoryAlgorithmId<CreateRsaSsaAlgorithm, | |
| 70 WebKit::WebCryptoAlgorithmIdSha384>; | |
| 71 map_["RS512"] = | |
| 72 &BindAlgFactoryAlgorithmId<CreateRsaSsaAlgorithm, | |
| 73 WebKit::WebCryptoAlgorithmIdSha512>; | |
| 74 map_["RSA1_5"] = | |
| 75 &BindAlgFactoryAlgorithmId<CreateAlgorithm, | |
| 76 WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5>; | |
| 77 map_["RSA-OAEP"] = | |
| 78 &BindAlgFactoryAlgorithmId<CreateRsaOaepAlgorithm, | |
| 79 WebKit::WebCryptoAlgorithmIdSha1>; | |
| 80 map_["A128KW"] = &WebKit::WebCryptoAlgorithm::createNull; | |
|
eroman
2013/11/07 20:54:37
Either add a TODO for these, or delete the "null"
padolph
2013/11/09 00:33:38
Added a TODO.
rsleevi: Would you consider adding
| |
| 81 map_["A256KW"] = &WebKit::WebCryptoAlgorithm::createNull; | |
| 82 map_["A128GCM"] = | |
| 83 &BindAlgFactoryAlgorithmId<CreateAlgorithm, | |
| 84 WebKit::WebCryptoAlgorithmIdAesGcm>; | |
| 85 map_["A256GCM"] = | |
| 86 &BindAlgFactoryAlgorithmId<CreateAlgorithm, | |
| 87 WebKit::WebCryptoAlgorithmIdAesGcm>; | |
| 88 map_["A128CBC"] = | |
| 89 &BindAlgFactoryAlgorithmId<CreateAlgorithm, | |
| 90 WebKit::WebCryptoAlgorithmIdAesCbc>; | |
| 91 map_["A256CBC"] = | |
| 92 &BindAlgFactoryAlgorithmId<CreateAlgorithm, | |
| 93 WebKit::WebCryptoAlgorithmIdAesCbc>; | |
| 94 map_["A384CBC"] = | |
| 95 &BindAlgFactoryAlgorithmId<CreateAlgorithm, | |
| 96 WebKit::WebCryptoAlgorithmIdAesCbc>; | |
| 97 map_["A512CBC"] = | |
| 98 &BindAlgFactoryAlgorithmId<CreateAlgorithm, | |
| 99 WebKit::WebCryptoAlgorithmIdAesCbc>; | |
| 100 } | |
| 101 | |
| 102 WebKit::WebCryptoAlgorithm CreateAlgorithmFromName( | |
| 103 const std::string& alg_id) const { | |
| 104 const JwkAlgFactoryMap::const_iterator pos = map_.find(alg_id); | |
| 105 if (pos == map_.end()) | |
| 106 return WebKit::WebCryptoAlgorithm::createNull(); | |
| 107 return pos->second(); | |
| 108 } | |
| 109 | |
| 110 private: | |
| 111 JwkAlgFactoryMap map_; | |
| 112 }; | |
| 113 | |
| 114 base::LazyInstance<JwkAlgorithmFactoryMap> jwk_alg_factory = | |
| 115 LAZY_INSTANCE_INITIALIZER; | |
| 116 | |
| 117 // TODO(padolph): Verify this logic is sufficient to judge algorithm | |
| 118 // "consistency" for JWK import, and for all supported algorithms. | |
| 119 bool WebCryptoAlgorithmsConsistent(const WebKit::WebCryptoAlgorithm& alg1, | |
| 120 const WebKit::WebCryptoAlgorithm& alg2) { | |
| 121 DCHECK(!alg1.isNull()); | |
| 122 DCHECK(!alg2.isNull()); | |
| 123 if (alg1.id() == alg2.id()) { | |
| 124 if (alg1.id() == WebKit::WebCryptoAlgorithmIdHmac || | |
| 125 alg1.id() == WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || | |
| 126 alg1.id() == WebKit::WebCryptoAlgorithmIdRsaOaep) { | |
| 127 if (WebCryptoAlgorithmsConsistent(GetInnerHashAlgorithm(alg1), | |
| 128 GetInnerHashAlgorithm(alg2))) { | |
| 129 return true; | |
| 130 } | |
| 131 return false; | |
| 132 } | |
| 133 return true; | |
| 134 } | |
| 135 return false; | |
| 136 } | |
| 137 | |
| 25 } // namespace | 138 } // namespace |
| 26 | 139 |
| 27 WebCryptoImpl::WebCryptoImpl() { | 140 WebCryptoImpl::WebCryptoImpl() { |
| 28 Init(); | 141 Init(); |
| 29 } | 142 } |
| 30 | 143 |
| 31 // static | 144 // 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 WebKit::WebArrayBuffer* buffer, | |
| 36 unsigned new_size) { | |
| 37 DCHECK_LE(new_size, buffer->byteLength()); | |
| 38 | |
| 39 if (new_size == buffer->byteLength()) | |
| 40 return; | |
| 41 | |
| 42 WebKit::WebArrayBuffer new_buffer = | |
| 43 WebKit::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 // static | |
| 50 // TODO(eroman): Expose functionality in Blink instead. | 145 // TODO(eroman): Expose functionality in Blink instead. |
| 51 WebKit::WebCryptoKey WebCryptoImpl::NullKey() { | 146 WebKit::WebCryptoKey WebCryptoImpl::NullKey() { |
| 52 // Needs a non-null algorithm to succeed. | 147 // Needs a non-null algorithm to succeed. |
| 53 return WebKit::WebCryptoKey::create( | 148 return WebKit::WebCryptoKey::create( |
| 54 NULL, WebKit::WebCryptoKeyTypeSecret, false, | 149 NULL, WebKit::WebCryptoKeyTypeSecret, false, |
| 55 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | 150 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 56 WebKit::WebCryptoAlgorithmIdAesGcm, NULL), 0); | 151 WebKit::WebCryptoAlgorithmIdAesGcm, NULL), 0); |
| 57 } | 152 } |
| 58 | 153 |
| 59 void WebCryptoImpl::encrypt( | 154 void WebCryptoImpl::encrypt( |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 | 238 |
| 144 void WebCryptoImpl::importKey( | 239 void WebCryptoImpl::importKey( |
| 145 WebKit::WebCryptoKeyFormat format, | 240 WebKit::WebCryptoKeyFormat format, |
| 146 const unsigned char* key_data, | 241 const unsigned char* key_data, |
| 147 unsigned key_data_size, | 242 unsigned key_data_size, |
| 148 const WebKit::WebCryptoAlgorithm& algorithm_or_null, | 243 const WebKit::WebCryptoAlgorithm& algorithm_or_null, |
| 149 bool extractable, | 244 bool extractable, |
| 150 WebKit::WebCryptoKeyUsageMask usage_mask, | 245 WebKit::WebCryptoKeyUsageMask usage_mask, |
| 151 WebKit::WebCryptoResult result) { | 246 WebKit::WebCryptoResult result) { |
| 152 WebKit::WebCryptoKey key = WebKit::WebCryptoKey::createNull(); | 247 WebKit::WebCryptoKey key = WebKit::WebCryptoKey::createNull(); |
| 153 if (!ImportKeyInternal(format, | 248 if (format == WebKit::WebCryptoKeyFormatJwk) { |
| 154 key_data, | 249 if (!ImportKeyJwk(key_data, |
| 155 key_data_size, | 250 key_data_size, |
| 156 algorithm_or_null, | 251 algorithm_or_null, |
| 157 extractable, | 252 extractable, |
| 158 usage_mask, | 253 usage_mask, |
| 159 &key)) { | 254 &key)) { |
| 160 result.completeWithError(); | 255 result.completeWithError(); |
| 161 return; | 256 } |
| 257 } else { | |
| 258 if (!ImportKeyInternal(format, | |
| 259 key_data, | |
| 260 key_data_size, | |
| 261 algorithm_or_null, | |
| 262 extractable, | |
| 263 usage_mask, | |
| 264 &key)) { | |
| 265 result.completeWithError(); | |
| 266 } | |
| 162 } | 267 } |
| 163 DCHECK(key.handle()); | 268 DCHECK(key.handle()); |
| 164 DCHECK(!key.algorithm().isNull()); | 269 DCHECK(!key.algorithm().isNull()); |
| 165 DCHECK_EQ(extractable, key.extractable()); | 270 DCHECK_EQ(extractable, key.extractable()); |
| 166 result.completeWithKey(key); | 271 result.completeWithKey(key); |
| 167 } | 272 } |
| 168 | 273 |
| 169 void WebCryptoImpl::sign( | 274 void WebCryptoImpl::sign( |
| 170 const WebKit::WebCryptoAlgorithm& algorithm, | 275 const WebKit::WebCryptoAlgorithm& algorithm, |
| 171 const WebKit::WebCryptoKey& key, | 276 const WebKit::WebCryptoKey& key, |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 197 signature_size, | 302 signature_size, |
| 198 data, | 303 data, |
| 199 data_size, | 304 data_size, |
| 200 &signature_match)) { | 305 &signature_match)) { |
| 201 result.completeWithError(); | 306 result.completeWithError(); |
| 202 } else { | 307 } else { |
| 203 result.completeWithBoolean(signature_match); | 308 result.completeWithBoolean(signature_match); |
| 204 } | 309 } |
| 205 } | 310 } |
| 206 | 311 |
| 312 bool WebCryptoImpl::ImportKeyJwk( | |
| 313 const unsigned char* key_data, | |
| 314 unsigned key_data_size, | |
| 315 const WebKit::WebCryptoAlgorithm& algorithm_or_null, | |
| 316 bool extractable, | |
| 317 WebKit::WebCryptoKeyUsageMask usage_mask, | |
| 318 WebKit::WebCryptoKey* key) { | |
| 319 | |
| 320 // JSON Web Key Format (JWK) | |
| 321 // http://tools.ietf.org/html/draft-ietf-jose-json-web-key-16 | |
| 322 // TODO(padolph): Not all possible values are handled by this code right now | |
| 323 // | |
| 324 // A JWK is a simple JSON dictionary with the following entries | |
| 325 // - "kty" (Key Type) Parameter, REQUIRED | |
| 326 // - <kty-specific parameters, see below>, REQUIRED | |
| 327 // - "use" (Key Use) Parameter, OPTIONAL | |
|
eroman
2013/11/07 20:54:37
It is worth also mentioning what OPTIONAL means. (
padolph
2013/11/09 00:33:38
I added some text to clarify this. Please take a l
| |
| 328 // - "alg" (Algorithm) Parameter, OPTIONAL | |
| 329 // - "extractable" (Key Exportability), OPTIONAL [NOTE: not yet part of JOSE] | |
| 330 // (all other entries are ignored) | |
| 331 // | |
| 332 // Input key_data contains the JWK. To build a Web Crypto Key, the JWK values | |
| 333 // are parsed out and used as follows: | |
| 334 // Web Crypto Key type <-- (deduced) | |
| 335 // Web Crypto Key extractable <-- extractable | |
| 336 // Web Crypto Key algorithm <-- alg | |
| 337 // Web Crypto Key keyUsage <-- usage | |
| 338 // Web Crypto Key keying material <-- kty-specific parameters | |
| 339 // | |
| 340 // Values for each entry are case-sensitive and defined in | |
| 341 // http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-16. | |
| 342 // Note that not all values specified by JOSE are handled by this code. Only | |
| 343 // handled values are listed. | |
| 344 // - kty (Key Type) | |
| 345 // +-------+--------------------------------------------------------------+ | |
| 346 // | "RSA" | RSA [RFC3447] | | |
| 347 // | "oct" | Octet sequence (used to represent symmetric keys) | | |
| 348 // +-------+--------------------------------------------------------------+ | |
| 349 // - use (Key Use) | |
| 350 // +-------+--------------------------------------------------------------+ | |
| 351 // | "enc" | encrypt and decrypt operations | | |
| 352 // | "sig" | sign and verify (MAC) operations | | |
| 353 // | "wrap"| key wrap and unwrap [not yet part of JOSE] | | |
| 354 // +-------+--------------------------------------------------------------+ | |
| 355 // - extractable (Key Exportability) | |
| 356 // +-------+--------------------------------------------------------------+ | |
| 357 // | true | Key may be exported from the trusted environment | | |
| 358 // | false | Key cannot exit the trusted environment | | |
| 359 // +-------+--------------------------------------------------------------+ | |
| 360 // - alg (Algorithm) | |
| 361 // See http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-16 | |
| 362 // +--------------+-------------------------------------------------------+ | |
| 363 // | Digital Signature or MAC Algorithm | | |
| 364 // +--------------+-------------------------------------------------------+ | |
| 365 // | "HS256" | HMAC using SHA-256 hash algorithm | | |
| 366 // | "HS384" | HMAC using SHA-384 hash algorithm | | |
| 367 // | "HS512" | HMAC using SHA-512 hash algorithm | | |
| 368 // | "RS256" | RSASSA using SHA-256 hash algorithm | | |
| 369 // | "RS384" | RSASSA using SHA-384 hash algorithm | | |
| 370 // | "RS512" | RSASSA using SHA-512 hash algorithm | | |
| 371 // +--------------+-------------------------------------------------------| | |
| 372 // | Key Management Algorithm | | |
| 373 // +--------------+-------------------------------------------------------+ | |
| 374 // | "RSA1_5" | RSAES-PKCS1-V1_5 [RFC3447] | | |
| 375 // | "RSA-OAEP" | RSAES using Optimal Asymmetric Encryption Padding | | |
| 376 // | | (OAEP) [RFC3447], with the default parameters | | |
| 377 // | | specified by RFC3447 in Section A.2.1 | | |
| 378 // | "A128KW" | Advanced Encryption Standard (AES) Key Wrap Algorithm | | |
| 379 // | | [RFC3394] using 128 bit keys | | |
| 380 // | "A256KW" | AES Key Wrap Algorithm using 256 bit keys | | |
| 381 // | "A128GCM" | AES in Galois/Counter Mode (GCM) [NIST.800-38D] using | | |
| 382 // | | 128 bit keys | | |
| 383 // | "A256GCM" | AES GCM using 256 bit keys | | |
| 384 // | "A128CBC" | AES in Cipher Block Chaining Mode (CBC) with PKCS #5 | | |
| 385 // | | padding [NIST.800-38A] [not yet part of JOSE] | | |
| 386 // | "A256CBC" | AES CBC using 256 bit keys [not yet part of JOSE] | | |
| 387 // | "A384CBC" | AES CBC using 384 bit keys [not yet part of JOSE] | | |
| 388 // | "A512CBC" | AES CBC using 512 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 // Conflict resolution | |
| 415 // The algorithm_or_null, extractable, and usage_mask input parameters may be | |
| 416 // different from similar values inside the JWK. The Web Crypto spec says that | |
| 417 // if a JWK value is present but is inconsistent with the input value, it is | |
| 418 // an error and the operation must fail. Conversely, should they be missing | |
| 419 // from the JWK, the input values should be used to "fill-in" for the | |
| 420 // corresponding JWK-optional values (use, alg, extractable). | |
| 421 | |
| 422 DCHECK(key); | |
| 423 | |
| 424 // Parse the incoming JWK JSON. | |
| 425 if (!key_data || !key_data_size) | |
|
eroman
2013/11/07 20:54:37
Same comment as on the other CL, I would prefer th
padolph
2013/11/09 00:33:38
Done.
| |
| 426 return false; | |
| 427 base::StringPiece json_string(reinterpret_cast<const char*>(key_data), | |
| 428 key_data_size); | |
| 429 scoped_ptr<base::Value> value(base::JSONReader::Read(json_string)); | |
| 430 // Note, bare pointer dict_value is ok since it points into scoped value. | |
| 431 base::DictionaryValue* dict_value = NULL; | |
| 432 if (!value.get() || !value->GetAsDictionary(&dict_value) || !dict_value) | |
| 433 return false; | |
| 434 | |
| 435 // JWK "kty". Exit early if this required JWK parameter is missing. | |
| 436 std::string jwk_kty_value; | |
| 437 if (!dict_value->GetString("kty", &jwk_kty_value)) | |
| 438 return false; | |
| 439 | |
| 440 // JWK "extractable" (optional) --> extractable parameter | |
| 441 { | |
| 442 bool jwk_extractable_value; | |
| 443 if (dict_value->GetBoolean("extractable", &jwk_extractable_value)) { | |
| 444 if (jwk_extractable_value != extractable) | |
|
eroman
2013/11/07 20:54:37
To be "consistent", it seems like it should alway
padolph
2013/11/09 00:33:38
Done.
| |
| 445 return false; | |
| 446 } | |
| 447 } | |
| 448 | |
| 449 // JWK "alg" (optional) --> algorithm parameter | |
| 450 // Note: input algorithm is also optional, so we have six cases to handle. | |
| 451 // 1. JWK alg present but unrecognized: error | |
| 452 // 2. JWK alg valid AND input algorithm isNull: use JWK value | |
| 453 // 3. JWK alg valid AND input algorithm specified, but JWK value | |
| 454 // inconsistent with input: error | |
| 455 // 4. JWK alg valid AND input algorithm specified, both consistent: use | |
| 456 // input value (because it has potentially more details) | |
| 457 // 5. JWK alg missing AND input algorithm isNull: error | |
| 458 // 6. JWK alg missing AND input algorithm specified: use input value | |
| 459 WebKit::WebCryptoAlgorithm algorithm = | |
| 460 WebKit::WebCryptoAlgorithm::createNull(); | |
| 461 std::string jwk_alg_value; | |
| 462 if (dict_value->GetString("alg", &jwk_alg_value)) { | |
| 463 // JWK alg present | |
| 464 const WebKit::WebCryptoAlgorithm jwk_algorithm = | |
| 465 jwk_alg_factory.Get().CreateAlgorithmFromName(jwk_alg_value); | |
| 466 if (jwk_algorithm.isNull()) { | |
| 467 // JWK alg unrecognized | |
| 468 return false; // case 1 | |
| 469 } | |
| 470 // JWK alg valid | |
| 471 if (algorithm_or_null.isNull()) { | |
| 472 // input algorithm not specified | |
| 473 algorithm = jwk_algorithm; // case 2 | |
| 474 } else { | |
| 475 // input algorithm specified | |
| 476 if (!WebCryptoAlgorithmsConsistent(jwk_algorithm, algorithm_or_null)) | |
| 477 return false; // case 3 | |
|
eroman
2013/11/07 20:54:37
Please indent this comment by just 2 spaces (style
padolph
2013/11/09 00:33:38
Done.
eroman
2013/11/09 02:22:14
not quite. What I meant is:
return false; //
padolph
2013/11/11 00:53:26
Ic. The current format is what clang-format told m
| |
| 478 algorithm = algorithm_or_null; // case 4 | |
| 479 } | |
| 480 } else { | |
| 481 // JWK alg missing | |
| 482 if (algorithm_or_null.isNull()) | |
| 483 return false; // case 5 | |
|
eroman
2013/11/07 20:54:37
same
padolph
2013/11/09 00:33:38
Done.
eroman
2013/11/09 02:22:14
Same.
padolph
2013/11/11 00:53:26
Done.
| |
| 484 algorithm = algorithm_or_null; // case 6 | |
| 485 } | |
| 486 DCHECK(!algorithm.isNull()); | |
| 487 | |
| 488 // JWK "use" (optional) --> usage_mask parameter | |
| 489 std::string jwk_use_value; | |
| 490 if (dict_value->GetString("use", &jwk_use_value)) { | |
| 491 WebKit::WebCryptoKeyUsageMask jwk_usage_mask = 0; | |
| 492 if (jwk_use_value == "enc") { | |
| 493 jwk_usage_mask = | |
| 494 WebKit::WebCryptoKeyUsageEncrypt | WebKit::WebCryptoKeyUsageDecrypt; | |
| 495 } else if (jwk_use_value == "sig") { | |
| 496 jwk_usage_mask = | |
| 497 WebKit::WebCryptoKeyUsageSign | WebKit::WebCryptoKeyUsageVerify; | |
| 498 } else if (jwk_use_value == "wrap") { | |
| 499 jwk_usage_mask = | |
| 500 WebKit::WebCryptoKeyUsageWrapKey | WebKit::WebCryptoKeyUsageUnwrapKey; | |
| 501 } else { | |
| 502 return false; | |
| 503 } | |
| 504 if ((jwk_usage_mask & usage_mask) != usage_mask) { | |
| 505 // A usage_mask must be a subset of jwk_usage_mask. | |
| 506 return false; | |
| 507 } | |
| 508 } | |
| 509 | |
| 510 // JWK keying material --> ImportKeyInternal() | |
| 511 if (jwk_kty_value == "oct") { | |
| 512 std::string jwk_k_value_url64; | |
| 513 if (!dict_value->GetString("k", &jwk_k_value_url64)) | |
| 514 return false; | |
| 515 std::string jwk_k_value; | |
| 516 if (!Base64DecodeUrlSafe(jwk_k_value_url64, &jwk_k_value) || | |
| 517 !jwk_k_value.size()) { | |
| 518 return false; | |
| 519 } | |
| 520 // TODO(padolph): AES JWK alg ID's embed the intended key length in the alg | |
| 521 // ID string. For such keys validate the actual key length against the value | |
| 522 // in the ID. | |
| 523 return ImportKeyInternal(WebKit::WebCryptoKeyFormatRaw, | |
| 524 reinterpret_cast<const uint8*>(jwk_k_value.data()), | |
| 525 jwk_k_value.size(), | |
| 526 algorithm, | |
| 527 extractable, | |
| 528 usage_mask, | |
| 529 key); | |
| 530 } else if (jwk_kty_value == "RSA") { | |
| 531 // TODO(padolph): JWK import RSA public key | |
| 532 return false; | |
| 533 } else { | |
| 534 return false; | |
| 535 } | |
| 536 | |
| 537 return true; | |
| 538 } | |
| 539 | |
| 207 } // namespace content | 540 } // namespace content |
| OLD | NEW |