Chromium Code Reviews| 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 19 matching lines...) Expand all Loading... | |
| 30 } | 30 } |
| 31 | 31 |
| 32 PK11SymKey* key() { return key_.get(); } | 32 PK11SymKey* key() { return key_.get(); } |
| 33 | 33 |
| 34 private: | 34 private: |
| 35 crypto::ScopedPK11SymKey key_; | 35 crypto::ScopedPK11SymKey key_; |
| 36 | 36 |
| 37 DISALLOW_COPY_AND_ASSIGN(SymKeyHandle); | 37 DISALLOW_COPY_AND_ASSIGN(SymKeyHandle); |
| 38 }; | 38 }; |
| 39 | 39 |
| 40 class PublicKeyHandle : public WebKit::WebCryptoKeyHandle { | |
| 41 public: | |
| 42 explicit PublicKeyHandle(crypto::ScopedSECKEYPublicKey key) { | |
| 43 DCHECK(!key_.get()); | |
| 44 key_ = key.Pass(); | |
| 45 } | |
| 46 | |
| 47 SECKEYPublicKey* key() { return key_.get(); } | |
| 48 | |
| 49 private: | |
| 50 crypto::ScopedSECKEYPublicKey key_; | |
| 51 | |
| 52 DISALLOW_COPY_AND_ASSIGN(PublicKeyHandle); | |
| 53 }; | |
| 54 | |
| 55 class PrivateKeyHandle : public WebKit::WebCryptoKeyHandle { | |
| 56 public: | |
| 57 explicit PrivateKeyHandle(crypto::ScopedSECKEYPrivateKey key) { | |
| 58 DCHECK(!key_.get()); | |
|
Ryan Sleevi
2013/10/24 00:33:04
This seems unnecessary - it's a constructor, how i
padolph
2013/10/24 01:40:47
Good point. Picked up through copy/paste. Will del
| |
| 59 key_ = key.Pass(); | |
| 60 } | |
| 61 | |
| 62 SECKEYPrivateKey* key() { return key_.get(); } | |
| 63 | |
| 64 private: | |
| 65 crypto::ScopedSECKEYPrivateKey key_; | |
| 66 | |
| 67 DISALLOW_COPY_AND_ASSIGN(PrivateKeyHandle); | |
| 68 }; | |
| 69 | |
| 40 HASH_HashType WebCryptoAlgorithmToNSSHashType( | 70 HASH_HashType WebCryptoAlgorithmToNSSHashType( |
| 41 const WebKit::WebCryptoAlgorithm& algorithm) { | 71 const WebKit::WebCryptoAlgorithm& algorithm) { |
| 42 switch (algorithm.id()) { | 72 switch (algorithm.id()) { |
| 43 case WebKit::WebCryptoAlgorithmIdSha1: | 73 case WebKit::WebCryptoAlgorithmIdSha1: |
| 44 return HASH_AlgSHA1; | 74 return HASH_AlgSHA1; |
| 45 case WebKit::WebCryptoAlgorithmIdSha224: | 75 case WebKit::WebCryptoAlgorithmIdSha224: |
| 46 return HASH_AlgSHA224; | 76 return HASH_AlgSHA224; |
| 47 case WebKit::WebCryptoAlgorithmIdSha256: | 77 case WebKit::WebCryptoAlgorithmIdSha256: |
| 48 return HASH_AlgSHA256; | 78 return HASH_AlgSHA256; |
| 49 case WebKit::WebCryptoAlgorithmIdSha384: | 79 case WebKit::WebCryptoAlgorithmIdSha384: |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 316 if (!pk11_key) { | 346 if (!pk11_key) { |
| 317 return false; | 347 return false; |
| 318 } | 348 } |
| 319 | 349 |
| 320 key->reset(new SymKeyHandle(pk11_key.Pass())); | 350 key->reset(new SymKeyHandle(pk11_key.Pass())); |
| 321 *type = key_type; | 351 *type = key_type; |
| 322 | 352 |
| 323 return true; | 353 return true; |
| 324 } | 354 } |
| 325 | 355 |
| 356 bool WebCryptoImpl::GenerateKeyPairInternal( | |
| 357 const WebKit::WebCryptoAlgorithm& algorithm, | |
| 358 scoped_ptr<WebKit::WebCryptoKeyHandle>* public_key_handle, | |
| 359 scoped_ptr<WebKit::WebCryptoKeyHandle>* private_key_handle) { | |
| 360 | |
| 361 // TODO(padolph) Handle other asymmetric algorithm key generation | |
| 362 switch (algorithm.id()) { | |
| 363 case WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5: | |
| 364 case WebKit::WebCryptoAlgorithmIdRsaOaep: | |
| 365 case WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5: { | |
| 366 const WebKit::WebCryptoRsaKeyGenParams* const params = | |
| 367 algorithm.rsaKeyGenParams(); | |
| 368 DCHECK(params); | |
| 369 | |
| 370 crypto::ScopedPK11Slot slot(PK11_GetInternalKeySlot()); | |
| 371 if (!slot || !params->modulusLength() || | |
| 372 !params->publicExponent().size()) { | |
| 373 return false; | |
| 374 } | |
| 375 | |
| 376 // The Web Crypto API says params->m_publicExponent is in big-endian | |
| 377 // order: the first element in the vector is the most significant digit. | |
| 378 // Leading zeros may or may not be present. | |
| 379 | |
| 380 // First offset past any leading zeros in publicExponent. | |
| 381 size_t start_index; | |
| 382 for (start_index = 0; start_index < params->publicExponent().size(); | |
| 383 ++start_index) { | |
| 384 if (params->publicExponent()[start_index] != 0x0) | |
| 385 break; | |
| 386 } | |
| 387 const size_t exponent_size = | |
| 388 params->publicExponent().size() - start_index; | |
| 389 if (start_index == params->publicExponent().size() || | |
| 390 exponent_size > sizeof(unsigned long)) { | |
| 391 return false; | |
| 392 } | |
| 393 | |
| 394 // Then convert the remaining data to an unsigned long. | |
| 395 const size_t end_index = params->publicExponent().size() - 1; | |
| 396 unsigned long public_exponent = 0; | |
| 397 for (int i = end_index; i >= static_cast<int>(start_index); --i) { | |
| 398 public_exponent |= params->publicExponent()[i] << (8 * (end_index - i)); | |
| 399 } | |
| 400 // TODO(padolph): should we limit the public exponent to the 'safe' set of | |
| 401 // {3, 5, 17, 257, 65537}? | |
| 402 | |
| 403 PK11RSAGenParams param; | |
| 404 param.keySizeInBits = params->modulusLength(); | |
| 405 param.pe = public_exponent; | |
| 406 | |
| 407 // Flags are verified at the Blink layer; here the flags are set to all | |
| 408 // possible operations for the given key type. | |
| 409 CK_FLAGS operation_flags; | |
| 410 switch (algorithm.id()) { | |
| 411 case WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5: | |
| 412 operation_flags = CKF_ENCRYPT | CKF_DECRYPT; | |
| 413 break; | |
| 414 case WebKit::WebCryptoAlgorithmIdRsaOaep: | |
| 415 operation_flags = CKF_WRAP | CKF_UNWRAP; | |
|
Ryan Sleevi
2013/10/24 00:33:04
BUG: This should also include CKF_ENCRYPT | CKF_DE
padolph
2013/10/24 01:40:47
Done.
| |
| 416 break; | |
| 417 case WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5: | |
| 418 operation_flags = CKF_SIGN | CKF_VERIFY; | |
| 419 break; | |
| 420 default: | |
| 421 NOTREACHED(); | |
| 422 return false; | |
| 423 } | |
| 424 const CK_FLAGS operation_flags_mask = CKF_ENCRYPT | CKF_DECRYPT | | |
| 425 CKF_SIGN | CKF_VERIFY | CKF_WRAP | | |
| 426 CKF_UNWRAP; | |
| 427 const PK11AttrFlags attribute_flags = 0; // default all PK11_ATTR_ flags | |
| 428 | |
| 429 SECKEYPublicKey* sec_public_key; | |
| 430 crypto::ScopedSECKEYPrivateKey private_key( | |
| 431 PK11_GenerateKeyPairWithOpFlags(slot.get(), | |
| 432 CKM_RSA_PKCS_KEY_PAIR_GEN, | |
| 433 ¶m, | |
| 434 &sec_public_key, | |
| 435 attribute_flags, | |
| 436 operation_flags, | |
| 437 operation_flags_mask, | |
| 438 NULL)); | |
| 439 if (!private_key) { | |
| 440 return false; | |
| 441 } | |
| 442 crypto::ScopedSECKEYPublicKey public_key(sec_public_key); | |
| 443 | |
| 444 public_key_handle->reset(new PublicKeyHandle(public_key.Pass())); | |
| 445 private_key_handle->reset(new PrivateKeyHandle(private_key.Pass())); | |
| 446 | |
| 447 return true; | |
| 448 } | |
| 449 default: | |
| 450 return false; | |
| 451 } | |
| 452 } | |
| 326 | 453 |
| 327 bool WebCryptoImpl::ImportKeyInternal( | 454 bool WebCryptoImpl::ImportKeyInternal( |
| 328 WebKit::WebCryptoKeyFormat format, | 455 WebKit::WebCryptoKeyFormat format, |
| 329 const unsigned char* key_data, | 456 const unsigned char* key_data, |
| 330 unsigned key_data_size, | 457 unsigned key_data_size, |
| 331 const WebKit::WebCryptoAlgorithm& algorithm, | 458 const WebKit::WebCryptoAlgorithm& algorithm, |
| 332 WebKit::WebCryptoKeyUsageMask usage_mask, | 459 WebKit::WebCryptoKeyUsageMask usage_mask, |
| 333 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle, | 460 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle, |
| 334 WebKit::WebCryptoKeyType* type) { | 461 WebKit::WebCryptoKeyType* type) { |
| 335 switch (algorithm.id()) { | 462 switch (algorithm.id()) { |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 499 break; | 626 break; |
| 500 } | 627 } |
| 501 default: | 628 default: |
| 502 return false; | 629 return false; |
| 503 } | 630 } |
| 504 | 631 |
| 505 return true; | 632 return true; |
| 506 } | 633 } |
| 507 | 634 |
| 508 } // namespace content | 635 } // namespace content |
| OLD | NEW |