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