Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: content/child/webcrypto/nss/rsa_key_nss.cc

Issue 401233004: Refactor RSA key generation for WebCrypto's NSS implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix bad merge Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | content/child/webcrypto/shared_crypto_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/nss/rsa_key_nss.h" 5 #include "content/child/webcrypto/nss/rsa_key_nss.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/numerics/safe_math.h"
9 #include "content/child/webcrypto/crypto_data.h" 8 #include "content/child/webcrypto/crypto_data.h"
10 #include "content/child/webcrypto/jwk.h" 9 #include "content/child/webcrypto/jwk.h"
11 #include "content/child/webcrypto/nss/key_nss.h" 10 #include "content/child/webcrypto/nss/key_nss.h"
12 #include "content/child/webcrypto/nss/util_nss.h" 11 #include "content/child/webcrypto/nss/util_nss.h"
13 #include "content/child/webcrypto/status.h" 12 #include "content/child/webcrypto/status.h"
14 #include "content/child/webcrypto/webcrypto_util.h" 13 #include "content/child/webcrypto/webcrypto_util.h"
15 #include "crypto/scoped_nss_types.h" 14 #include "crypto/scoped_nss_types.h"
16 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" 15 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
17 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" 16 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
18 17
19 namespace content { 18 namespace content {
20 19
21 namespace webcrypto { 20 namespace webcrypto {
22 21
23 namespace { 22 namespace {
24 23
25 // Converts a (big-endian) WebCrypto BigInteger, with or without leading zeros,
26 // to unsigned long.
27 bool BigIntegerToLong(const uint8_t* data,
28 unsigned int data_size,
29 unsigned long* result) {
30 // TODO(eroman): Fix handling of empty biginteger. http://crubg.com/373552
31 if (data_size == 0)
32 return false;
33
34 *result = 0;
35 for (size_t i = 0; i < data_size; ++i) {
36 size_t reverse_i = data_size - i - 1;
37
38 if (reverse_i >= sizeof(unsigned long) && data[i])
39 return false; // Too large for a long.
40
41 *result |= data[i] << 8 * reverse_i;
42 }
43 return true;
44 }
45
46 bool CreatePublicKeyAlgorithm(const blink::WebCryptoAlgorithm& algorithm, 24 bool CreatePublicKeyAlgorithm(const blink::WebCryptoAlgorithm& algorithm,
47 SECKEYPublicKey* key, 25 SECKEYPublicKey* key,
48 blink::WebCryptoKeyAlgorithm* key_algorithm) { 26 blink::WebCryptoKeyAlgorithm* key_algorithm) {
49 // TODO(eroman): What about other key types rsaPss, rsaOaep. 27 // TODO(eroman): What about other key types rsaPss, rsaOaep.
50 if (!key || key->keyType != rsaKey) 28 if (!key || key->keyType != rsaKey)
51 return false; 29 return false;
52 30
53 unsigned int modulus_length_bits = SECKEY_PublicKeyStrength(key) * 8; 31 unsigned int modulus_length_bits = SECKEY_PublicKeyStrength(key) * 8;
54 CryptoData public_exponent(key->u.rsa.publicExponent.data, 32 CryptoData public_exponent(key->u.rsa.publicExponent.data,
55 key->u.rsa.publicExponent.len); 33 key->u.rsa.publicExponent.len);
(...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 return Status::Success(); 550 return Status::Success();
573 } 551 }
574 552
575 Status RsaHashedAlgorithm::GenerateKeyPair( 553 Status RsaHashedAlgorithm::GenerateKeyPair(
576 const blink::WebCryptoAlgorithm& algorithm, 554 const blink::WebCryptoAlgorithm& algorithm,
577 bool extractable, 555 bool extractable,
578 blink::WebCryptoKeyUsageMask public_usage_mask, 556 blink::WebCryptoKeyUsageMask public_usage_mask,
579 blink::WebCryptoKeyUsageMask private_usage_mask, 557 blink::WebCryptoKeyUsageMask private_usage_mask,
580 blink::WebCryptoKey* public_key, 558 blink::WebCryptoKey* public_key,
581 blink::WebCryptoKey* private_key) const { 559 blink::WebCryptoKey* private_key) const {
582 const blink::WebCryptoRsaHashedKeyGenParams* params = 560 unsigned int public_exponent = 0;
583 algorithm.rsaHashedKeyGenParams(); 561 unsigned int modulus_length_bits = 0;
584 562 Status status = GetRsaKeyGenParameters(algorithm.rsaHashedKeyGenParams(),
585 if (!params->modulusLengthBits()) 563 &public_exponent,
586 return Status::ErrorGenerateRsaZeroModulus(); 564 &modulus_length_bits);
587 565 if (status.IsError())
588 unsigned long public_exponent = 0; 566 return status;
589 if (!BigIntegerToLong(params->publicExponent().data(),
590 params->publicExponent().size(),
591 &public_exponent) ||
592 (public_exponent != 3 && public_exponent != 65537)) {
593 return Status::ErrorGenerateKeyPublicExponent();
594 }
595 567
596 crypto::ScopedPK11Slot slot(PK11_GetInternalKeySlot()); 568 crypto::ScopedPK11Slot slot(PK11_GetInternalKeySlot());
597 if (!slot) 569 if (!slot)
598 return Status::OperationError(); 570 return Status::OperationError();
599 571
600 PK11RSAGenParams rsa_gen_params; 572 PK11RSAGenParams rsa_gen_params;
601 // keySizeInBits is a signed type, don't pass in a negative value. 573 rsa_gen_params.keySizeInBits = modulus_length_bits;
602 base::CheckedNumeric<int> signed_modulus(params->modulusLengthBits());
603 if (!signed_modulus.IsValid())
604 return Status::OperationError();
605 rsa_gen_params.keySizeInBits = signed_modulus.ValueOrDie();
606 rsa_gen_params.pe = public_exponent; 574 rsa_gen_params.pe = public_exponent;
607 575
608 const CK_FLAGS operation_flags_mask = 576 const CK_FLAGS operation_flags_mask =
609 CKF_ENCRYPT | CKF_DECRYPT | CKF_SIGN | CKF_VERIFY | CKF_WRAP | CKF_UNWRAP; 577 CKF_ENCRYPT | CKF_DECRYPT | CKF_SIGN | CKF_VERIFY | CKF_WRAP | CKF_UNWRAP;
610 578
611 // The private key must be marked as insensitive and extractable, otherwise it 579 // The private key must be marked as insensitive and extractable, otherwise it
612 // cannot later be exported in unencrypted form or structured-cloned. 580 // cannot later be exported in unencrypted form or structured-cloned.
613 const PK11AttrFlags attribute_flags = 581 const PK11AttrFlags attribute_flags =
614 PK11_ATTR_INSENSITIVE | PK11_ATTR_EXTRACTABLE; 582 PK11_ATTR_INSENSITIVE | PK11_ATTR_EXTRACTABLE;
615 583
(...skipping 10 matching lines...) Expand all
626 operation_flags_mask, 594 operation_flags_mask,
627 NULL)); 595 NULL));
628 if (!scoped_sec_private_key) 596 if (!scoped_sec_private_key)
629 return Status::OperationError(); 597 return Status::OperationError();
630 598
631 blink::WebCryptoKeyAlgorithm key_algorithm; 599 blink::WebCryptoKeyAlgorithm key_algorithm;
632 if (!CreatePublicKeyAlgorithm(algorithm, sec_public_key, &key_algorithm)) 600 if (!CreatePublicKeyAlgorithm(algorithm, sec_public_key, &key_algorithm))
633 return Status::ErrorUnexpected(); 601 return Status::ErrorUnexpected();
634 602
635 std::vector<uint8_t> spki_data; 603 std::vector<uint8_t> spki_data;
636 Status status = ExportKeySpkiNss(sec_public_key, &spki_data); 604 status = ExportKeySpkiNss(sec_public_key, &spki_data);
637 if (status.IsError()) 605 if (status.IsError())
638 return status; 606 return status;
639 607
640 scoped_ptr<PublicKeyNss> public_key_handle(new PublicKeyNss( 608 scoped_ptr<PublicKeyNss> public_key_handle(new PublicKeyNss(
641 crypto::ScopedSECKEYPublicKey(sec_public_key), CryptoData(spki_data))); 609 crypto::ScopedSECKEYPublicKey(sec_public_key), CryptoData(spki_data)));
642 610
643 std::vector<uint8_t> pkcs8_data; 611 std::vector<uint8_t> pkcs8_data;
644 status = ExportKeyPkcs8Nss(scoped_sec_private_key.get(), &pkcs8_data); 612 status = ExportKeyPkcs8Nss(scoped_sec_private_key.get(), &pkcs8_data);
645 if (status.IsError()) 613 if (status.IsError())
646 return status; 614 return status;
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
890 return Status::Success(); 858 return Status::Success();
891 } 859 }
892 default: 860 default:
893 return Status::ErrorUnexpected(); 861 return Status::ErrorUnexpected();
894 } 862 }
895 } 863 }
896 864
897 } // namespace webcrypto 865 } // namespace webcrypto
898 866
899 } // namespace content 867 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/child/webcrypto/shared_crypto_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698