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" |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 void CompleteWithKeyOrError(const Status& status, | 127 void CompleteWithKeyOrError(const Status& status, |
128 const blink::WebCryptoKey& key, | 128 const blink::WebCryptoKey& key, |
129 blink::WebCryptoResult* result) { | 129 blink::WebCryptoResult* result) { |
130 if (status.IsError()) { | 130 if (status.IsError()) { |
131 CompleteWithError(status, result); | 131 CompleteWithError(status, result); |
132 } else { | 132 } else { |
133 result->completeWithKey(key); | 133 result->completeWithKey(key); |
134 } | 134 } |
135 } | 135 } |
136 | 136 |
137 bool IsAlgorithmAsymmetric(const blink::WebCryptoAlgorithm& algorithm) { | |
138 // TODO(padolph): include all other asymmetric algorithms once they are | |
139 // defined, e.g. EC and DH. | |
140 return webcrypto::IsAlgorithmRsa(algorithm.id()); | |
141 } | |
142 | |
143 // Gets a task runner for the current thread. The current thread is either: | 137 // Gets a task runner for the current thread. The current thread is either: |
144 // | 138 // |
145 // * The main Blink thread | 139 // * The main Blink thread |
146 // * A Blink web worker thread | 140 // * A Blink web worker thread |
147 // | 141 // |
148 // A different mechanism is needed for posting to these threads. The main | 142 // A different mechanism is needed for posting to these threads. The main |
149 // thread has an associated message loop and can simply use | 143 // thread has an associated message loop and can simply use |
150 // base::ThreadTaskRunnerHandle. Whereas the web worker threads are managed by | 144 // base::ThreadTaskRunnerHandle. Whereas the web worker threads are managed by |
151 // Blink and need to be indirected through WorkerThreadTaskRunner. | 145 // Blink and need to be indirected through WorkerThreadTaskRunner. |
152 scoped_refptr<base::TaskRunner> GetCurrentBlinkThread() { | 146 scoped_refptr<base::TaskRunner> GetCurrentBlinkThread() { |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 } else { | 392 } else { |
399 if (state->is_asymmetric) | 393 if (state->is_asymmetric) |
400 state->result.completeWithKeyPair(state->public_key, state->private_key); | 394 state->result.completeWithKeyPair(state->public_key, state->private_key); |
401 else | 395 else |
402 state->result.completeWithKey(state->public_key); | 396 state->result.completeWithKey(state->public_key); |
403 } | 397 } |
404 } | 398 } |
405 | 399 |
406 void DoGenerateKey(scoped_ptr<GenerateKeyState> passed_state) { | 400 void DoGenerateKey(scoped_ptr<GenerateKeyState> passed_state) { |
407 GenerateKeyState* state = passed_state.get(); | 401 GenerateKeyState* state = passed_state.get(); |
408 state->is_asymmetric = IsAlgorithmAsymmetric(state->algorithm); | 402 state->is_asymmetric = |
| 403 webcrypto::IsAlgorithmAsymmetric(state->algorithm.id()); |
409 if (state->is_asymmetric) { | 404 if (state->is_asymmetric) { |
410 state->status = webcrypto::GenerateKeyPair(state->algorithm, | 405 state->status = webcrypto::GenerateKeyPair(state->algorithm, |
411 state->extractable, | 406 state->extractable, |
412 state->usage_mask, | 407 state->usage_mask, |
413 &state->public_key, | 408 &state->public_key, |
414 &state->private_key); | 409 &state->private_key); |
415 | 410 |
416 if (state->status.IsSuccess()) { | 411 if (state->status.IsSuccess()) { |
417 DCHECK(state->public_key.handle()); | 412 DCHECK(state->public_key.handle()); |
418 DCHECK(state->private_key.handle()); | 413 DCHECK(state->private_key.handle()); |
419 DCHECK_EQ(state->algorithm.id(), state->public_key.algorithm().id()); | 414 DCHECK_EQ(state->algorithm.id(), state->public_key.algorithm().id()); |
420 DCHECK_EQ(state->algorithm.id(), state->private_key.algorithm().id()); | 415 DCHECK_EQ(state->algorithm.id(), state->private_key.algorithm().id()); |
421 DCHECK_EQ(true, state->public_key.extractable()); | 416 DCHECK_EQ(true, state->public_key.extractable()); |
422 DCHECK_EQ(state->extractable, state->private_key.extractable()); | 417 DCHECK_EQ(state->extractable, state->private_key.extractable()); |
423 DCHECK_EQ(state->usage_mask, state->public_key.usages()); | |
424 DCHECK_EQ(state->usage_mask, state->private_key.usages()); | |
425 } | 418 } |
426 } else { | 419 } else { |
427 blink::WebCryptoKey* key = &state->public_key; | 420 blink::WebCryptoKey* key = &state->public_key; |
428 | 421 |
429 state->status = webcrypto::GenerateSecretKey( | 422 state->status = webcrypto::GenerateSecretKey( |
430 state->algorithm, state->extractable, state->usage_mask, key); | 423 state->algorithm, state->extractable, state->usage_mask, key); |
431 | 424 |
432 if (state->status.IsSuccess()) { | 425 if (state->status.IsSuccess()) { |
433 DCHECK(key->handle()); | 426 DCHECK(key->handle()); |
434 DCHECK_EQ(state->algorithm.id(), key->algorithm().id()); | 427 DCHECK_EQ(state->algorithm.id(), key->algorithm().id()); |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
737 &key); | 730 &key); |
738 } | 731 } |
739 | 732 |
740 bool WebCryptoImpl::serializeKeyForClone( | 733 bool WebCryptoImpl::serializeKeyForClone( |
741 const blink::WebCryptoKey& key, | 734 const blink::WebCryptoKey& key, |
742 blink::WebVector<unsigned char>& key_data) { | 735 blink::WebVector<unsigned char>& key_data) { |
743 return webcrypto::SerializeKeyForClone(key, &key_data); | 736 return webcrypto::SerializeKeyForClone(key, &key_data); |
744 } | 737 } |
745 | 738 |
746 } // namespace content | 739 } // namespace content |
OLD | NEW |