| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/child/webcrypto/webcrypto_impl.h" | 5 #include "content/child/webcrypto/webcrypto_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 #include "base/task_runner.h" | 14 #include "base/task_runner.h" |
| 15 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
| 16 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/threading/sequenced_worker_pool.h" |
| 17 #include "base/threading/worker_pool.h" | 17 #include "base/threading/worker_pool.h" |
| 18 #include "content/child/webcrypto/algorithm_dispatch.h" | 18 #include "content/child/webcrypto/algorithm_dispatch.h" |
| 19 #include "content/child/webcrypto/crypto_data.h" | 19 #include "content/child/webcrypto/crypto_data.h" |
| 20 #include "content/child/webcrypto/generate_key_result.h" |
| 20 #include "content/child/webcrypto/status.h" | 21 #include "content/child/webcrypto/status.h" |
| 21 #include "content/child/webcrypto/structured_clone.h" | 22 #include "content/child/webcrypto/structured_clone.h" |
| 22 #include "content/child/webcrypto/webcrypto_util.h" | 23 #include "content/child/webcrypto/webcrypto_util.h" |
| 23 #include "content/child/worker_thread_task_runner.h" | 24 #include "content/child/worker_thread_task_runner.h" |
| 24 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | 25 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
| 25 #include "third_party/WebKit/public/platform/WebString.h" | 26 #include "third_party/WebKit/public/platform/WebString.h" |
| 26 | 27 |
| 27 namespace content { | 28 namespace content { |
| 28 | 29 |
| 29 using webcrypto::Status; | 30 using webcrypto::Status; |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 typedef EncryptState DigestState; | 209 typedef EncryptState DigestState; |
| 209 | 210 |
| 210 struct GenerateKeyState : public BaseState { | 211 struct GenerateKeyState : public BaseState { |
| 211 GenerateKeyState(const blink::WebCryptoAlgorithm& algorithm, | 212 GenerateKeyState(const blink::WebCryptoAlgorithm& algorithm, |
| 212 bool extractable, | 213 bool extractable, |
| 213 blink::WebCryptoKeyUsageMask usage_mask, | 214 blink::WebCryptoKeyUsageMask usage_mask, |
| 214 const blink::WebCryptoResult& result) | 215 const blink::WebCryptoResult& result) |
| 215 : BaseState(result), | 216 : BaseState(result), |
| 216 algorithm(algorithm), | 217 algorithm(algorithm), |
| 217 extractable(extractable), | 218 extractable(extractable), |
| 218 usage_mask(usage_mask), | 219 usage_mask(usage_mask) {} |
| 219 public_key(blink::WebCryptoKey::createNull()), | |
| 220 private_key(blink::WebCryptoKey::createNull()), | |
| 221 is_asymmetric(false) {} | |
| 222 | 220 |
| 223 const blink::WebCryptoAlgorithm algorithm; | 221 const blink::WebCryptoAlgorithm algorithm; |
| 224 const bool extractable; | 222 const bool extractable; |
| 225 const blink::WebCryptoKeyUsageMask usage_mask; | 223 const blink::WebCryptoKeyUsageMask usage_mask; |
| 226 | 224 |
| 227 // If |is_asymmetric| is false, then |public_key| is understood to mean the | 225 webcrypto::GenerateKeyResult generate_key_result; |
| 228 // symmetric key, and |private_key| is unused. | |
| 229 blink::WebCryptoKey public_key; | |
| 230 blink::WebCryptoKey private_key; | |
| 231 bool is_asymmetric; | |
| 232 }; | 226 }; |
| 233 | 227 |
| 234 struct ImportKeyState : public BaseState { | 228 struct ImportKeyState : public BaseState { |
| 235 ImportKeyState(blink::WebCryptoKeyFormat format, | 229 ImportKeyState(blink::WebCryptoKeyFormat format, |
| 236 const unsigned char* key_data, | 230 const unsigned char* key_data, |
| 237 unsigned int key_data_size, | 231 unsigned int key_data_size, |
| 238 const blink::WebCryptoAlgorithm& algorithm, | 232 const blink::WebCryptoAlgorithm& algorithm, |
| 239 bool extractable, | 233 bool extractable, |
| 240 blink::WebCryptoKeyUsageMask usage_mask, | 234 blink::WebCryptoKeyUsageMask usage_mask, |
| 241 const blink::WebCryptoResult& result) | 235 const blink::WebCryptoResult& result) |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 state->status = webcrypto::Digest( | 388 state->status = webcrypto::Digest( |
| 395 state->algorithm, webcrypto::CryptoData(state->data), &state->buffer); | 389 state->algorithm, webcrypto::CryptoData(state->data), &state->buffer); |
| 396 state->origin_thread->PostTask( | 390 state->origin_thread->PostTask( |
| 397 FROM_HERE, base::Bind(DoDigestReply, Passed(&passed_state))); | 391 FROM_HERE, base::Bind(DoDigestReply, Passed(&passed_state))); |
| 398 } | 392 } |
| 399 | 393 |
| 400 void DoGenerateKeyReply(scoped_ptr<GenerateKeyState> state) { | 394 void DoGenerateKeyReply(scoped_ptr<GenerateKeyState> state) { |
| 401 if (state->status.IsError()) { | 395 if (state->status.IsError()) { |
| 402 CompleteWithError(state->status, &state->result); | 396 CompleteWithError(state->status, &state->result); |
| 403 } else { | 397 } else { |
| 404 if (state->is_asymmetric) | 398 state->generate_key_result.Complete(&state->result); |
| 405 state->result.completeWithKeyPair(state->public_key, state->private_key); | |
| 406 else | |
| 407 state->result.completeWithKey(state->public_key); | |
| 408 } | 399 } |
| 409 } | 400 } |
| 410 | 401 |
| 411 void DoGenerateKey(scoped_ptr<GenerateKeyState> passed_state) { | 402 void DoGenerateKey(scoped_ptr<GenerateKeyState> passed_state) { |
| 412 GenerateKeyState* state = passed_state.get(); | 403 GenerateKeyState* state = passed_state.get(); |
| 413 if (state->cancelled()) | 404 if (state->cancelled()) |
| 414 return; | 405 return; |
| 415 state->is_asymmetric = | 406 state->status = webcrypto::GenerateKey(state->algorithm, |
| 416 webcrypto::IsAlgorithmAsymmetric(state->algorithm.id()); | 407 state->extractable, |
| 417 if (state->is_asymmetric) { | 408 state->usage_mask, |
| 418 state->status = webcrypto::GenerateKeyPair(state->algorithm, | 409 &state->generate_key_result); |
| 419 state->extractable, | |
| 420 state->usage_mask, | |
| 421 &state->public_key, | |
| 422 &state->private_key); | |
| 423 | |
| 424 if (state->status.IsSuccess()) { | |
| 425 DCHECK(state->public_key.handle()); | |
| 426 DCHECK(state->private_key.handle()); | |
| 427 DCHECK_EQ(state->algorithm.id(), state->public_key.algorithm().id()); | |
| 428 DCHECK_EQ(state->algorithm.id(), state->private_key.algorithm().id()); | |
| 429 DCHECK_EQ(true, state->public_key.extractable()); | |
| 430 DCHECK_EQ(state->extractable, state->private_key.extractable()); | |
| 431 } | |
| 432 } else { | |
| 433 blink::WebCryptoKey* key = &state->public_key; | |
| 434 | |
| 435 state->status = webcrypto::GenerateSecretKey( | |
| 436 state->algorithm, state->extractable, state->usage_mask, key); | |
| 437 | |
| 438 if (state->status.IsSuccess()) { | |
| 439 DCHECK(key->handle()); | |
| 440 DCHECK_EQ(state->algorithm.id(), key->algorithm().id()); | |
| 441 DCHECK_EQ(state->extractable, key->extractable()); | |
| 442 DCHECK_EQ(state->usage_mask, key->usages()); | |
| 443 } | |
| 444 } | |
| 445 | |
| 446 state->origin_thread->PostTask( | 410 state->origin_thread->PostTask( |
| 447 FROM_HERE, base::Bind(DoGenerateKeyReply, Passed(&passed_state))); | 411 FROM_HERE, base::Bind(DoGenerateKeyReply, Passed(&passed_state))); |
| 448 } | 412 } |
| 449 | 413 |
| 450 void DoImportKeyReply(scoped_ptr<ImportKeyState> state) { | 414 void DoImportKeyReply(scoped_ptr<ImportKeyState> state) { |
| 451 CompleteWithKeyOrError(state->status, state->key, &state->result); | 415 CompleteWithKeyOrError(state->status, state->key, &state->result); |
| 452 } | 416 } |
| 453 | 417 |
| 454 void DoImportKey(scoped_ptr<ImportKeyState> passed_state) { | 418 void DoImportKey(scoped_ptr<ImportKeyState> passed_state) { |
| 455 ImportKeyState* state = passed_state.get(); | 419 ImportKeyState* state = passed_state.get(); |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 761 &key); | 725 &key); |
| 762 } | 726 } |
| 763 | 727 |
| 764 bool WebCryptoImpl::serializeKeyForClone( | 728 bool WebCryptoImpl::serializeKeyForClone( |
| 765 const blink::WebCryptoKey& key, | 729 const blink::WebCryptoKey& key, |
| 766 blink::WebVector<unsigned char>& key_data) { | 730 blink::WebVector<unsigned char>& key_data) { |
| 767 return webcrypto::SerializeKeyForClone(key, &key_data); | 731 return webcrypto::SerializeKeyForClone(key, &key_data); |
| 768 } | 732 } |
| 769 | 733 |
| 770 } // namespace content | 734 } // namespace content |
| OLD | NEW |