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 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 struct GenerateKeyState : public BaseState { | 210 struct GenerateKeyState : public BaseState { |
211 GenerateKeyState(const blink::WebCryptoAlgorithm& algorithm, | 211 GenerateKeyState(const blink::WebCryptoAlgorithm& algorithm, |
212 bool extractable, | 212 bool extractable, |
213 blink::WebCryptoKeyUsageMask usage_mask, | 213 blink::WebCryptoKeyUsageMask usage_mask, |
214 const blink::WebCryptoResult& result) | 214 const blink::WebCryptoResult& result) |
215 : BaseState(result), | 215 : BaseState(result), |
216 algorithm(algorithm), | 216 algorithm(algorithm), |
217 extractable(extractable), | 217 extractable(extractable), |
218 usage_mask(usage_mask), | 218 usage_mask(usage_mask), |
219 public_key(blink::WebCryptoKey::createNull()), | 219 public_key(blink::WebCryptoKey::createNull()), |
220 private_key(blink::WebCryptoKey::createNull()), | 220 private_key(blink::WebCryptoKey::createNull()) {} |
221 is_asymmetric(false) {} | |
222 | 221 |
223 const blink::WebCryptoAlgorithm algorithm; | 222 const blink::WebCryptoAlgorithm algorithm; |
224 const bool extractable; | 223 const bool extractable; |
225 const blink::WebCryptoKeyUsageMask usage_mask; | 224 const blink::WebCryptoKeyUsageMask usage_mask; |
226 | 225 |
227 // If |is_asymmetric| is false, then |public_key| is understood to mean the | 226 // private_key may be a secret key, in which case public_key is unused. |
228 // symmetric key, and |private_key| is unused. | |
229 blink::WebCryptoKey public_key; | 227 blink::WebCryptoKey public_key; |
230 blink::WebCryptoKey private_key; | 228 blink::WebCryptoKey private_key; |
231 bool is_asymmetric; | |
232 }; | 229 }; |
233 | 230 |
234 struct ImportKeyState : public BaseState { | 231 struct ImportKeyState : public BaseState { |
235 ImportKeyState(blink::WebCryptoKeyFormat format, | 232 ImportKeyState(blink::WebCryptoKeyFormat format, |
236 const unsigned char* key_data, | 233 const unsigned char* key_data, |
237 unsigned int key_data_size, | 234 unsigned int key_data_size, |
238 const blink::WebCryptoAlgorithm& algorithm, | 235 const blink::WebCryptoAlgorithm& algorithm, |
239 bool extractable, | 236 bool extractable, |
240 blink::WebCryptoKeyUsageMask usage_mask, | 237 blink::WebCryptoKeyUsageMask usage_mask, |
241 const blink::WebCryptoResult& result) | 238 const blink::WebCryptoResult& result) |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 state->status = webcrypto::Digest( | 391 state->status = webcrypto::Digest( |
395 state->algorithm, webcrypto::CryptoData(state->data), &state->buffer); | 392 state->algorithm, webcrypto::CryptoData(state->data), &state->buffer); |
396 state->origin_thread->PostTask( | 393 state->origin_thread->PostTask( |
397 FROM_HERE, base::Bind(DoDigestReply, Passed(&passed_state))); | 394 FROM_HERE, base::Bind(DoDigestReply, Passed(&passed_state))); |
398 } | 395 } |
399 | 396 |
400 void DoGenerateKeyReply(scoped_ptr<GenerateKeyState> state) { | 397 void DoGenerateKeyReply(scoped_ptr<GenerateKeyState> state) { |
401 if (state->status.IsError()) { | 398 if (state->status.IsError()) { |
402 CompleteWithError(state->status, &state->result); | 399 CompleteWithError(state->status, &state->result); |
403 } else { | 400 } else { |
404 if (state->is_asymmetric) | 401 if (state->private_key.type() == blink::WebCryptoKeyTypeSecret) |
| 402 state->result.completeWithKey(state->private_key); |
| 403 else |
405 state->result.completeWithKeyPair(state->public_key, state->private_key); | 404 state->result.completeWithKeyPair(state->public_key, state->private_key); |
406 else | |
407 state->result.completeWithKey(state->public_key); | |
408 } | 405 } |
409 } | 406 } |
410 | 407 |
411 void DoGenerateKey(scoped_ptr<GenerateKeyState> passed_state) { | 408 void DoGenerateKey(scoped_ptr<GenerateKeyState> passed_state) { |
412 GenerateKeyState* state = passed_state.get(); | 409 GenerateKeyState* state = passed_state.get(); |
413 if (state->cancelled()) | 410 if (state->cancelled()) |
414 return; | 411 return; |
415 state->is_asymmetric = | 412 state->status = webcrypto::GenerateKey(state->algorithm, |
416 webcrypto::IsAlgorithmAsymmetric(state->algorithm.id()); | 413 state->extractable, |
417 if (state->is_asymmetric) { | 414 state->usage_mask, |
418 state->status = webcrypto::GenerateKeyPair(state->algorithm, | 415 &state->public_key, |
419 state->extractable, | 416 &state->private_key); |
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( | 417 state->origin_thread->PostTask( |
447 FROM_HERE, base::Bind(DoGenerateKeyReply, Passed(&passed_state))); | 418 FROM_HERE, base::Bind(DoGenerateKeyReply, Passed(&passed_state))); |
448 } | 419 } |
449 | 420 |
450 void DoImportKeyReply(scoped_ptr<ImportKeyState> state) { | 421 void DoImportKeyReply(scoped_ptr<ImportKeyState> state) { |
451 CompleteWithKeyOrError(state->status, state->key, &state->result); | 422 CompleteWithKeyOrError(state->status, state->key, &state->result); |
452 } | 423 } |
453 | 424 |
454 void DoImportKey(scoped_ptr<ImportKeyState> passed_state) { | 425 void DoImportKey(scoped_ptr<ImportKeyState> passed_state) { |
455 ImportKeyState* state = passed_state.get(); | 426 ImportKeyState* state = passed_state.get(); |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
761 &key); | 732 &key); |
762 } | 733 } |
763 | 734 |
764 bool WebCryptoImpl::serializeKeyForClone( | 735 bool WebCryptoImpl::serializeKeyForClone( |
765 const blink::WebCryptoKey& key, | 736 const blink::WebCryptoKey& key, |
766 blink::WebVector<unsigned char>& key_data) { | 737 blink::WebVector<unsigned char>& key_data) { |
767 return webcrypto::SerializeKeyForClone(key, &key_data); | 738 return webcrypto::SerializeKeyForClone(key, &key_data); |
768 } | 739 } |
769 | 740 |
770 } // namespace content | 741 } // namespace content |
OLD | NEW |