OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 <cryptohi.h> | 7 #include <cryptohi.h> |
8 #include <pk11pub.h> | 8 #include <pk11pub.h> |
9 #include <sechash.h> | 9 #include <sechash.h> |
10 | 10 |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 NULL)); | 318 NULL)); |
319 if (!pk11_sym_key.get()) { | 319 if (!pk11_sym_key.get()) { |
320 return false; | 320 return false; |
321 } | 321 } |
322 | 322 |
323 *key = blink::WebCryptoKey::create(new SymKeyHandle(pk11_sym_key.Pass()), | 323 *key = blink::WebCryptoKey::create(new SymKeyHandle(pk11_sym_key.Pass()), |
324 type, extractable, algorithm, usage_mask); | 324 type, extractable, algorithm, usage_mask); |
325 return true; | 325 return true; |
326 } | 326 } |
327 | 327 |
| 328 bool ExportKeyInternalRaw( |
| 329 const blink::WebCryptoKey& key, |
| 330 blink::WebArrayBuffer* buffer) { |
| 331 |
| 332 DCHECK(key.handle()); |
| 333 DCHECK(buffer); |
| 334 |
| 335 if (key.type() != blink::WebCryptoKeyTypeSecret || !key.extractable()) |
| 336 return false; |
| 337 |
| 338 SymKeyHandle* sym_key = reinterpret_cast<SymKeyHandle*>(key.handle()); |
| 339 |
| 340 if (PK11_ExtractKeyValue(sym_key->key()) != SECSuccess) |
| 341 return false; |
| 342 |
| 343 const SECItem* key_data = PK11_GetKeyData(sym_key->key()); |
| 344 if (!key_data) |
| 345 return false; |
| 346 |
| 347 *buffer = webcrypto::CreateArrayBuffer(key_data->data, key_data->len); |
| 348 |
| 349 return true; |
| 350 } |
| 351 |
328 typedef scoped_ptr<CERTSubjectPublicKeyInfo, | 352 typedef scoped_ptr<CERTSubjectPublicKeyInfo, |
329 crypto::NSSDestroyer<CERTSubjectPublicKeyInfo, | 353 crypto::NSSDestroyer<CERTSubjectPublicKeyInfo, |
330 SECKEY_DestroySubjectPublicKeyInfo> > | 354 SECKEY_DestroySubjectPublicKeyInfo> > |
331 ScopedCERTSubjectPublicKeyInfo; | 355 ScopedCERTSubjectPublicKeyInfo; |
332 | 356 |
333 // Validates an NSS KeyType against a WebCrypto algorithm. Some NSS KeyTypes | 357 // Validates an NSS KeyType against a WebCrypto algorithm. Some NSS KeyTypes |
334 // contain enough information to fabricate a Web Crypto algorithm, which is | 358 // contain enough information to fabricate a Web Crypto algorithm, which is |
335 // returned if the input algorithm isNull(). This function indicates failure by | 359 // returned if the input algorithm isNull(). This function indicates failure by |
336 // returning a Null algorithm. | 360 // returning a Null algorithm. |
337 blink::WebCryptoAlgorithm ResolveNssKeyTypeWithInputAlgorithm( | 361 blink::WebCryptoAlgorithm ResolveNssKeyTypeWithInputAlgorithm( |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 reinterpret_cast<PublicKeyHandle*>(key.handle()); | 441 reinterpret_cast<PublicKeyHandle*>(key.handle()); |
418 | 442 |
419 const crypto::ScopedSECItem spki_der( | 443 const crypto::ScopedSECItem spki_der( |
420 SECKEY_EncodeDERSubjectPublicKeyInfo(pub_key->key())); | 444 SECKEY_EncodeDERSubjectPublicKeyInfo(pub_key->key())); |
421 if (!spki_der) | 445 if (!spki_der) |
422 return false; | 446 return false; |
423 | 447 |
424 DCHECK(spki_der->data); | 448 DCHECK(spki_der->data); |
425 DCHECK(spki_der->len); | 449 DCHECK(spki_der->len); |
426 | 450 |
427 *buffer = blink::WebArrayBuffer::create(spki_der->len, 1); | 451 *buffer = webcrypto::CreateArrayBuffer(spki_der->data, spki_der->len); |
428 memcpy(buffer->data(), spki_der->data, spki_der->len); | |
429 | 452 |
430 return true; | 453 return true; |
431 } | 454 } |
432 | 455 |
433 bool ImportKeyInternalPkcs8( | 456 bool ImportKeyInternalPkcs8( |
434 const unsigned char* key_data, | 457 const unsigned char* key_data, |
435 unsigned key_data_size, | 458 unsigned key_data_size, |
436 const blink::WebCryptoAlgorithm& algorithm_or_null, | 459 const blink::WebCryptoAlgorithm& algorithm_or_null, |
437 bool extractable, | 460 bool extractable, |
438 blink::WebCryptoKeyUsageMask usage_mask, | 461 blink::WebCryptoKeyUsageMask usage_mask, |
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
820 return false; | 843 return false; |
821 } | 844 } |
822 } | 845 } |
823 | 846 |
824 bool WebCryptoImpl::ExportKeyInternal( | 847 bool WebCryptoImpl::ExportKeyInternal( |
825 blink::WebCryptoKeyFormat format, | 848 blink::WebCryptoKeyFormat format, |
826 const blink::WebCryptoKey& key, | 849 const blink::WebCryptoKey& key, |
827 blink::WebArrayBuffer* buffer) { | 850 blink::WebArrayBuffer* buffer) { |
828 switch (format) { | 851 switch (format) { |
829 case blink::WebCryptoKeyFormatRaw: | 852 case blink::WebCryptoKeyFormatRaw: |
830 // TODO(padolph): Implement raw export | 853 return ExportKeyInternalRaw(key, buffer); |
831 return false; | |
832 case blink::WebCryptoKeyFormatSpki: | 854 case blink::WebCryptoKeyFormatSpki: |
833 return ExportKeyInternalSpki(key, buffer); | 855 return ExportKeyInternalSpki(key, buffer); |
834 case blink::WebCryptoKeyFormatPkcs8: | 856 case blink::WebCryptoKeyFormatPkcs8: |
835 // TODO(padolph): Implement pkcs8 export | 857 // TODO(padolph): Implement pkcs8 export |
836 return false; | 858 return false; |
837 default: | 859 default: |
838 return false; | 860 return false; |
839 } | 861 } |
840 } | 862 } |
841 | 863 |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
988 | 1010 |
989 *key = blink::WebCryptoKey::create(new PublicKeyHandle(pubkey.Pass()), | 1011 *key = blink::WebCryptoKey::create(new PublicKeyHandle(pubkey.Pass()), |
990 blink::WebCryptoKeyTypePublic, | 1012 blink::WebCryptoKeyTypePublic, |
991 extractable, | 1013 extractable, |
992 algorithm, | 1014 algorithm, |
993 usage_mask); | 1015 usage_mask); |
994 return true; | 1016 return true; |
995 } | 1017 } |
996 | 1018 |
997 } // namespace content | 1019 } // namespace content |
OLD | NEW |