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 "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 |
| 23 namespace { | |
| 24 | |
| 25 // Binds a specific key length value to a compatible factory function. | |
| 26 typedef WebKit::WebCryptoAlgorithm (*AlgFactoryFuncWithOneShortArg)( | |
| 27 unsigned short); | |
| 28 template <AlgFactoryFuncWithOneShortArg func, unsigned short key_length> | |
| 29 WebKit::WebCryptoAlgorithm BindAlgFactoryWithKeyLen() { | |
| 30 return func(key_length); | |
| 31 } | |
| 32 | |
| 33 // Binds a WebCryptoAlgorithmId value to a compatible factory function. | |
| 34 typedef WebKit::WebCryptoAlgorithm (*AlgFactoryFuncWithWebCryptoAlgIdArg)( | |
| 35 WebKit::WebCryptoAlgorithmId); | |
| 36 template <AlgFactoryFuncWithWebCryptoAlgIdArg func, | |
| 37 WebKit::WebCryptoAlgorithmId algorithm_id> | |
| 38 WebKit::WebCryptoAlgorithm BindAlgFactoryAlgorithmId() { | |
| 39 return func(algorithm_id); | |
| 40 } | |
| 41 | |
| 42 // Defines a map between a JWK 'alg' string ID and a corresponding Web Crypto | |
| 43 // factory function. | |
| 44 typedef WebKit::WebCryptoAlgorithm (*AlgFactoryFuncNoArgs)(); | |
| 45 typedef std::map<std::string, AlgFactoryFuncNoArgs> JwkAlgFactoryMap; | |
| 46 | |
| 47 class JwkAlgorithmFactoryMap { | |
| 48 public: | |
| 49 JwkAlgorithmFactoryMap() { | |
| 50 map_["HS256"] = | |
| 51 &BindAlgFactoryWithKeyLen<CreateHmacAlgorithmByDigestLen, 256>; | |
| 52 map_["HS256"] = | |
|
eroman
2013/11/04 21:01:15
This is a duplication of the one above.
padolph
2013/11/05 03:30:55
Done.
| |
| 53 &BindAlgFactoryWithKeyLen<CreateHmacAlgorithmByDigestLen, 256>; | |
| 54 map_["HS384"] = | |
| 55 &BindAlgFactoryWithKeyLen<CreateHmacAlgorithmByDigestLen, 384>; | |
| 56 map_["HS384"] = | |
|
eroman
2013/11/04 21:01:15
This is a duplication of the key above. I presume
padolph
2013/11/05 03:30:55
Done.
| |
| 57 &BindAlgFactoryWithKeyLen<CreateHmacAlgorithmByDigestLen, 512>; | |
| 58 map_["RS256"] = | |
| 59 &BindAlgFactoryAlgorithmId<CreateRsaSsaAlgorithm, | |
| 60 WebKit::WebCryptoAlgorithmIdSha256>; | |
| 61 map_["RS384"] = | |
| 62 &BindAlgFactoryAlgorithmId<CreateRsaSsaAlgorithm, | |
| 63 WebKit::WebCryptoAlgorithmIdSha384>; | |
| 64 map_["RS512"] = | |
| 65 &BindAlgFactoryAlgorithmId<CreateRsaSsaAlgorithm, | |
| 66 WebKit::WebCryptoAlgorithmIdSha512>; | |
| 67 map_["RSA1_5"] = &CreateRsaEsAlgorithm; | |
| 68 map_["RSA-OAEP"] = | |
| 69 &BindAlgFactoryAlgorithmId<CreateRsaOaepAlgorithm, | |
| 70 WebKit::WebCryptoAlgorithmIdSha1>; | |
| 71 map_["A128KW"] = &WebKit::WebCryptoAlgorithm::createNull; | |
| 72 map_["A256KW"] = &WebKit::WebCryptoAlgorithm::createNull; | |
| 73 map_["A128GCM"] = | |
| 74 &BindAlgFactoryWithKeyLen<CreateAesGcmKeyGenAlgorithm, 128>; | |
|
eroman
2013/11/04 21:01:15
Are you sure it is correct for these to be using t
padolph
2013/11/05 03:30:55
The final call to ImportKey() at the end of Import
eroman
2013/11/07 05:49:41
Background: The spec alas does not define what if
padolph
2013/11/07 17:58:21
Thanks for the background. I struggled with this i
| |
| 75 map_["A256GCM"] = | |
| 76 &BindAlgFactoryWithKeyLen<CreateAesGcmKeyGenAlgorithm, 256>; | |
|
eroman
2013/11/04 21:01:15
same comment here regarding the "Gen" version of t
padolph
2013/11/05 03:30:55
See above.
| |
| 77 map_["A128CBC"] = | |
| 78 &BindAlgFactoryWithKeyLen<CreateAesCbcKeyGenAlgorithm, 128>; | |
| 79 map_["A256CBC"] = | |
| 80 &BindAlgFactoryWithKeyLen<CreateAesCbcKeyGenAlgorithm, 256>; | |
| 81 map_["A384CBC"] = | |
| 82 &BindAlgFactoryWithKeyLen<CreateAesCbcKeyGenAlgorithm, 384>; | |
| 83 map_["A512CBC"] = | |
| 84 &BindAlgFactoryWithKeyLen<CreateAesCbcKeyGenAlgorithm, 512>; | |
| 85 } | |
| 86 | |
| 87 WebKit::WebCryptoAlgorithm CreateAlgorithmFromName( | |
| 88 const std::string& alg_id) { | |
|
eroman
2013/11/04 21:01:15
please tag this function as "const".
padolph
2013/11/05 03:30:55
Done.
| |
| 89 const JwkAlgFactoryMap::const_iterator pos = map_.find(alg_id); | |
| 90 if (pos == map_.end()) | |
| 91 return WebKit::WebCryptoAlgorithm::createNull(); | |
| 92 return pos->second(); | |
| 93 } | |
| 94 | |
| 95 private: | |
| 96 JwkAlgFactoryMap map_; | |
| 97 }; | |
| 98 | |
| 99 base::LazyInstance<JwkAlgorithmFactoryMap> jwk_alg_factory = | |
| 100 LAZY_INSTANCE_INITIALIZER; | |
| 101 | |
| 102 // TODO(padolph): Verify this logic is sufficient to judge algorithm | |
| 103 // "consistency" for JWK import, and for all supported algorithms. | |
| 104 bool WebCryptoAlgorithmsConsistent(const WebKit::WebCryptoAlgorithm& alg1, | |
| 105 const WebKit::WebCryptoAlgorithm& alg2) { | |
| 106 DCHECK(!alg1.isNull()); | |
| 107 DCHECK(!alg2.isNull()); | |
| 108 if (alg1.id() == alg2.id()) { | |
| 109 if (alg1.id() == WebKit::WebCryptoAlgorithmIdHmac || | |
| 110 alg1.id() == WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || | |
| 111 alg1.id() == WebKit::WebCryptoAlgorithmIdRsaOaep) { | |
| 112 if (WebCryptoAlgorithmsConsistent(GetInnerHashAlgorithm(alg1), | |
| 113 GetInnerHashAlgorithm(alg2))) { | |
| 114 return true; | |
| 115 } | |
| 116 return false; | |
| 117 } | |
| 118 return true; | |
| 119 } | |
| 120 return false; | |
| 121 } | |
| 122 | |
| 123 } // namespace | |
| 124 | |
| 15 WebCryptoImpl::WebCryptoImpl() { | 125 WebCryptoImpl::WebCryptoImpl() { |
| 16 Init(); | 126 Init(); |
| 17 } | 127 } |
| 18 | 128 |
| 19 // static | 129 WebCryptoImpl::~WebCryptoImpl() {} |
| 20 // TODO(eroman): This works by re-allocating a new buffer. It would be better if | |
| 21 // the WebArrayBuffer could just be truncated instead. | |
| 22 void WebCryptoImpl::ShrinkBuffer( | |
| 23 WebKit::WebArrayBuffer* buffer, | |
| 24 unsigned new_size) { | |
| 25 DCHECK_LE(new_size, buffer->byteLength()); | |
| 26 | |
| 27 if (new_size == buffer->byteLength()) | |
| 28 return; | |
| 29 | |
| 30 WebKit::WebArrayBuffer new_buffer = | |
| 31 WebKit::WebArrayBuffer::create(new_size, 1); | |
| 32 DCHECK(!new_buffer.isNull()); | |
| 33 memcpy(new_buffer.data(), buffer->data(), new_size); | |
| 34 *buffer = new_buffer; | |
| 35 } | |
| 36 | 130 |
| 37 // static | 131 // static |
| 38 // TODO(eroman): Expose functionality in Blink instead. | 132 // TODO(eroman): Expose functionality in Blink instead. |
| 39 WebKit::WebCryptoKey WebCryptoImpl::NullKey() { | 133 WebKit::WebCryptoKey WebCryptoImpl::NullKey() { |
| 40 // Needs a non-null algorithm to succeed. | 134 // Needs a non-null algorithm to succeed. |
| 41 return WebKit::WebCryptoKey::create( | 135 return WebKit::WebCryptoKey::create( |
| 42 NULL, WebKit::WebCryptoKeyTypeSecret, false, | 136 NULL, WebKit::WebCryptoKeyTypeSecret, false, |
| 43 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | 137 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 44 WebKit::WebCryptoAlgorithmIdAesGcm, NULL), 0); | 138 WebKit::WebCryptoAlgorithmIdAesGcm, NULL), 0); |
| 45 } | 139 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 108 | 202 |
| 109 void WebCryptoImpl::importKey( | 203 void WebCryptoImpl::importKey( |
| 110 WebKit::WebCryptoKeyFormat format, | 204 WebKit::WebCryptoKeyFormat format, |
| 111 const unsigned char* key_data, | 205 const unsigned char* key_data, |
| 112 unsigned key_data_size, | 206 unsigned key_data_size, |
| 113 const WebKit::WebCryptoAlgorithm& algorithm_or_null, | 207 const WebKit::WebCryptoAlgorithm& algorithm_or_null, |
| 114 bool extractable, | 208 bool extractable, |
| 115 WebKit::WebCryptoKeyUsageMask usage_mask, | 209 WebKit::WebCryptoKeyUsageMask usage_mask, |
| 116 WebKit::WebCryptoResult result) { | 210 WebKit::WebCryptoResult result) { |
| 117 WebKit::WebCryptoKey key = NullKey(); | 211 WebKit::WebCryptoKey key = NullKey(); |
| 118 if (!ImportKeyInternal(format, | 212 if (format == WebKit::WebCryptoKeyFormatJwk) { |
| 119 key_data, | 213 if (!ImportKeyJwk(key_data, |
| 120 key_data_size, | 214 key_data_size, |
| 121 algorithm_or_null, | 215 algorithm_or_null, |
| 122 extractable, | 216 extractable, |
| 123 usage_mask, | 217 usage_mask, |
| 124 &key)) { | 218 &key)) { |
| 125 result.completeWithError(); | 219 result.completeWithError(); |
| 126 return; | 220 } |
| 221 } else { | |
| 222 if (!ImportKeyInternal(format, | |
| 223 key_data, | |
| 224 key_data_size, | |
| 225 algorithm_or_null, | |
| 226 extractable, | |
| 227 usage_mask, | |
| 228 &key)) { | |
| 229 result.completeWithError(); | |
| 230 } | |
| 127 } | 231 } |
| 128 DCHECK(key.handle()); | 232 DCHECK(key.handle()); |
| 129 DCHECK(!key.algorithm().isNull()); | 233 DCHECK(!key.algorithm().isNull()); |
| 130 DCHECK_EQ(extractable, key.extractable()); | 234 DCHECK_EQ(extractable, key.extractable()); |
| 131 result.completeWithKey(key); | 235 result.completeWithKey(key); |
| 132 } | 236 } |
| 133 | 237 |
| 134 void WebCryptoImpl::sign( | 238 void WebCryptoImpl::sign( |
| 135 const WebKit::WebCryptoAlgorithm& algorithm, | 239 const WebKit::WebCryptoAlgorithm& algorithm, |
| 136 const WebKit::WebCryptoKey& key, | 240 const WebKit::WebCryptoKey& key, |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 162 signature_size, | 266 signature_size, |
| 163 data, | 267 data, |
| 164 data_size, | 268 data_size, |
| 165 &signature_match)) { | 269 &signature_match)) { |
| 166 result.completeWithError(); | 270 result.completeWithError(); |
| 167 } else { | 271 } else { |
| 168 result.completeWithBoolean(signature_match); | 272 result.completeWithBoolean(signature_match); |
| 169 } | 273 } |
| 170 } | 274 } |
| 171 | 275 |
| 276 bool WebCryptoImpl::ImportKeyJwk( | |
| 277 const unsigned char* key_data, | |
| 278 unsigned key_data_size, | |
| 279 const WebKit::WebCryptoAlgorithm& input_algorithm, | |
|
eroman
2013/11/04 21:01:15
This name differs from the declaration. Can they b
padolph
2013/11/05 03:30:55
Done.
| |
| 280 bool extractable, | |
| 281 WebKit::WebCryptoKeyUsageMask usage_mask, | |
| 282 WebKit::WebCryptoKey* key) { | |
| 283 | |
| 284 // JSON Web Key Format (JWK) | |
| 285 // http://tools.ietf.org/html/draft-ietf-jose-json-web-key-16 | |
| 286 // TODO(padolph): Not all possible values are handled by this code right now | |
| 287 // | |
| 288 // A JWK is a simple JSON dictionary with the following entries | |
| 289 // - "kty" (Key Type) Parameter, REQUIRED | |
| 290 // - <kty-specific parameters, see below>, REQUIRED | |
| 291 // - "use" (Key Use) Parameter, OPTIONAL | |
| 292 // - "alg" (Algorithm) Parameter, OPTIONAL | |
| 293 // - "extractable" (Key Exportability), OPTIONAL [NOTE: not yet part of JOSE] | |
| 294 // (all other entries are ignored) | |
| 295 // | |
| 296 // Input key_data contains the JWK. To build a Web Crypto Key, the JWK values | |
| 297 // are parsed out and used as follows: | |
| 298 // Web Crypto Key type <-- (deduced) | |
| 299 // Web Crypto Key extractable <-- extractable | |
| 300 // Web Crypto Key algorithm <-- alg | |
| 301 // Web Crypto Key keyUsage <-- usage | |
| 302 // Web Crypto Key keying material <-- kty-specific parameters | |
| 303 // | |
| 304 // Values for each entry are case-sensitive and defined in | |
| 305 // http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-16. | |
| 306 // Note that not all values specified by JOSE are handled by this code. Only | |
| 307 // handled values are listed. | |
| 308 // - kty (Key Type) | |
| 309 // +-------+--------------------------------------------------------------+ | |
| 310 // | "RSA" | RSA [RFC3447] | | |
| 311 // | "oct" | Octet sequence (used to represent symmetric keys) | | |
| 312 // +-------+--------------------------------------------------------------+ | |
| 313 // - use (Key Use) | |
| 314 // +-------+--------------------------------------------------------------+ | |
| 315 // | "enc" | encrypt and decrypt operations | | |
| 316 // | "sig" | sign and verify (MAC) operations | | |
| 317 // | "wrap"| key wrap and unwrap [not yet part of JOSE] | | |
| 318 // +-------+--------------------------------------------------------------+ | |
| 319 // - extractable (Key Exportability) | |
| 320 // +-------+--------------------------------------------------------------+ | |
| 321 // | true | Key may be exported from the trusted environment | | |
| 322 // | false | Key cannot exit the trusted environment | | |
| 323 // +-------+--------------------------------------------------------------+ | |
| 324 // - alg (Algorithm) | |
| 325 // See http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-16 | |
| 326 // +--------------+-------------------------------------------------------+ | |
| 327 // | Digital Signature or MAC Algorithm | | |
| 328 // +--------------+-------------------------------------------------------+ | |
| 329 // | "HS256" | HMAC using SHA-256 hash algorithm | | |
| 330 // | "HS384" | HMAC using SHA-384 hash algorithm | | |
| 331 // | "HS512" | HMAC using SHA-512 hash algorithm | | |
| 332 // | "RS256" | RSASSA using SHA-256 hash algorithm | | |
| 333 // | "RS384" | RSASSA using SHA-384 hash algorithm | | |
| 334 // | "RS512" | RSASSA using SHA-512 hash algorithm | | |
| 335 // +--------------+-------------------------------------------------------| | |
| 336 // | Key Management Algorithm | | |
| 337 // +--------------+-------------------------------------------------------+ | |
| 338 // | "RSA1_5" | RSAES-PKCS1-V1_5 [RFC3447] | | |
| 339 // | "RSA-OAEP" | RSAES using Optimal Asymmetric Encryption Padding | | |
| 340 // | | (OAEP) [RFC3447], with the default parameters | | |
| 341 // | | specified by RFC3447 in Section A.2.1 | | |
| 342 // | "A128KW" | Advanced Encryption Standard (AES) Key Wrap Algorithm | | |
| 343 // | | [RFC3394] using 128 bit keys | | |
| 344 // | "A256KW" | AES Key Wrap Algorithm using 256 bit keys | | |
| 345 // | "A128GCM" | AES in Galois/Counter Mode (GCM) [NIST.800-38D] using | | |
| 346 // | | 128 bit keys | | |
| 347 // | "A256GCM" | AES GCM using 256 bit keys | | |
| 348 // | "A128CBC" | AES in Cipher Block Chaining Mode (CBC) with PKCS #5 | | |
| 349 // | | padding [NIST.800-38A] [not yet part of JOSE] | | |
| 350 // | "A256CBC" | AES CBC using 256 bit keys [not yet part of JOSE] | | |
| 351 // | "A384CBC" | AES CBC using 384 bit keys [not yet part of JOSE] | | |
| 352 // | "A512CBC" | AES CBC using 512 bit keys [not yet part of JOSE] | | |
| 353 // +--------------+-------------------------------------------------------+ | |
| 354 // | |
| 355 // kty-specific parameters | |
| 356 // The value of kty determines the type and content of the keying material | |
| 357 // carried in the JWK to be imported. Currently only two possibilities are | |
| 358 // supported: a raw key or an RSA public key. RSA private keys are not | |
| 359 // supported because typical applications seldom need to import a private key, | |
| 360 // and the large number of JWK parameters required to describe one. | |
| 361 // - kty == "oct" (symmetric or other raw key) | |
| 362 // +-------+--------------------------------------------------------------+ | |
| 363 // | "k" | Contains the value of the symmetric (or other single-valued) | | |
| 364 // | | key. It is represented as the base64url encoding of the | | |
| 365 // | | octet sequence containing the key value. | | |
| 366 // +-------+--------------------------------------------------------------+ | |
| 367 // - kty == "RSA" (RSA public key) | |
| 368 // +-------+--------------------------------------------------------------+ | |
| 369 // | "n" | Contains the modulus value for the RSA public key. It is | | |
| 370 // | | represented as the base64url encoding of the value's | | |
| 371 // | | unsigned big endian representation as an octet sequence. | | |
| 372 // +-------+--------------------------------------------------------------+ | |
| 373 // | "e" | Contains the exponent value for the RSA public key. It is | | |
| 374 // | | represented as the base64url encoding of the value's | | |
| 375 // | | unsigned big endian representation as an octet sequence. | | |
| 376 // +-------+--------------------------------------------------------------+ | |
| 377 // | |
| 378 // Conflict resolution | |
| 379 // The input_algorithm, extractable, and usage_mask input parameters may be | |
| 380 // different from similar values inside the JWK. The Web Crypto spec says that | |
| 381 // if a JWK value is present but is inconsistent with the input value, it is | |
| 382 // an error and the operation must fail. Conversely, should they be missing | |
| 383 // from the JWK, the input values should be used to "fill-in" for the | |
| 384 // corresponding JWK-optional values (use, alg, extractable). | |
| 385 | |
| 386 DCHECK(key); | |
| 387 | |
| 388 // Parse the incoming JWK JSON. | |
| 389 if (!key_data || !key_data_size) | |
| 390 return false; | |
| 391 base::StringPiece json_string(reinterpret_cast<const char*>(key_data), | |
| 392 key_data_size); | |
| 393 scoped_ptr<base::Value> value(base::JSONReader::Read(json_string)); | |
| 394 // Note, bare pointer dict_value is ok since it points into scoped value. | |
| 395 base::DictionaryValue* dict_value = NULL; | |
| 396 if (!value.get() || !value->GetAsDictionary(&dict_value) || !dict_value) | |
| 397 return false; | |
| 398 | |
| 399 // JWK "kty". Exit early if this required JWK parameter is missing. | |
| 400 std::string jwk_kty_value; | |
| 401 if (!dict_value->GetString("kty", &jwk_kty_value)) | |
| 402 return false; | |
| 403 | |
| 404 // JWK "extractable" (optional) --> extractable parameter | |
| 405 { | |
| 406 bool jwk_extractable_value; | |
| 407 if (dict_value->GetBoolean("extractable", &jwk_extractable_value)) { | |
| 408 if (jwk_extractable_value != extractable) | |
| 409 return false; | |
| 410 } | |
| 411 } | |
| 412 | |
| 413 // JWK "alg" (optional) --> algorithm parameter | |
| 414 // Note: input algorithm is also optional, so we have six cases to handle. | |
| 415 // 1. JWK alg present but unrecognized: error | |
| 416 // 2. JWK alg valid AND input algorithm isNull: use JWK value | |
| 417 // 3. JWK alg valid AND input algorithm specified, but JWK value | |
| 418 // inconsistent with input: error | |
| 419 // 4. JWK alg valid AND input algorithm specified, both consistent: use | |
| 420 // input value (because it has potentially more details) | |
| 421 // 5. JWK alg missing AND input algorithm isNull: error | |
| 422 // 6. JWK alg missing AND input algorithm specified: use input value | |
| 423 WebKit::WebCryptoAlgorithm algorithm = | |
| 424 WebKit::WebCryptoAlgorithm::createNull(); | |
| 425 std::string jwk_alg_value; | |
| 426 if (dict_value->GetString("alg", &jwk_alg_value)) { | |
| 427 // JWK alg present | |
| 428 const WebKit::WebCryptoAlgorithm jwk_algorithm = | |
| 429 jwk_alg_factory.Get().CreateAlgorithmFromName(jwk_alg_value); | |
| 430 if (jwk_algorithm.isNull()) { | |
| 431 // JWK alg unrecognized | |
| 432 return false; // case 1 | |
| 433 } | |
| 434 // JWK alg valid | |
| 435 if (input_algorithm.isNull()) { | |
| 436 // input algorithm not specified | |
| 437 algorithm = jwk_algorithm; // case 2 | |
| 438 } else { | |
| 439 // input algorithm specified | |
| 440 if (!WebCryptoAlgorithmsConsistent(jwk_algorithm, input_algorithm)) | |
| 441 return false; // case 3 | |
| 442 algorithm = input_algorithm; // case 4 | |
| 443 } | |
| 444 } else { | |
| 445 // JWK alg missing | |
| 446 if (input_algorithm.isNull()) | |
| 447 return false; // case 5 | |
| 448 algorithm = input_algorithm; // case 6 | |
| 449 } | |
| 450 DCHECK(!algorithm.isNull()); | |
| 451 | |
| 452 // JWK "use" (optional) --> usage_mask parameter | |
| 453 std::string jwk_use_value; | |
| 454 if (dict_value->GetString("use", &jwk_use_value)) { | |
| 455 WebKit::WebCryptoKeyUsageMask jwk_usage_mask = 0; | |
| 456 if (jwk_use_value == "enc") { | |
| 457 jwk_usage_mask = | |
| 458 WebKit::WebCryptoKeyUsageEncrypt | WebKit::WebCryptoKeyUsageDecrypt; | |
| 459 } else if (jwk_use_value == "sig") { | |
| 460 jwk_usage_mask = | |
| 461 WebKit::WebCryptoKeyUsageSign | WebKit::WebCryptoKeyUsageVerify; | |
| 462 } else if (jwk_use_value == "wrap") { | |
| 463 jwk_usage_mask = | |
| 464 WebKit::WebCryptoKeyUsageWrapKey | WebKit::WebCryptoKeyUsageUnwrapKey; | |
| 465 } else { | |
| 466 return false; | |
| 467 } | |
| 468 if ((jwk_usage_mask & usage_mask) != usage_mask) { | |
| 469 // A usage_mask must be a subset of jwk_usage_mask. | |
| 470 return false; | |
| 471 } | |
| 472 } | |
| 473 | |
| 474 // JWK keying material --> ImportKeyInternal() | |
| 475 if (jwk_kty_value == "oct") { | |
| 476 std::string jwk_k_value_url64; | |
| 477 if (!dict_value->GetString("k", &jwk_k_value_url64)) | |
| 478 return false; | |
| 479 std::string jwk_k_value; | |
| 480 if (!Base64DecodeUrlSafe(jwk_k_value_url64, &jwk_k_value) || | |
| 481 !jwk_k_value.size()) { | |
| 482 return false; | |
| 483 } | |
| 484 return ImportKeyInternal(WebKit::WebCryptoKeyFormatRaw, | |
| 485 reinterpret_cast<const uint8*>(jwk_k_value.data()), | |
| 486 jwk_k_value.size(), | |
| 487 algorithm, | |
| 488 extractable, | |
| 489 usage_mask, | |
| 490 key); | |
| 491 } else if (jwk_kty_value == "RSA") { | |
| 492 // TODO(padolph): JWK import RSA public key | |
| 493 return false; | |
| 494 } else { | |
| 495 return false; | |
| 496 } | |
| 497 | |
| 498 return true; | |
| 499 } | |
| 500 | |
| 172 } // namespace content | 501 } // namespace content |
| OLD | NEW |