Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "net/base/x509_certificate.h" | 5 #include "net/base/x509_certificate.h" |
| 6 | 6 |
| 7 #include <cert.h> | 7 #include <cert.h> |
| 8 #include <cryptohi.h> | 8 #include <cryptohi.h> |
| 9 #include <keyhi.h> | 9 #include <keyhi.h> |
| 10 #include <nss.h> | 10 #include <nss.h> |
| 11 #include <pk11pub.h> | 11 #include <pk11pub.h> |
| 12 #include <prerror.h> | 12 #include <prerror.h> |
| 13 #include <prtime.h> | 13 #include <prtime.h> |
| 14 #include <secder.h> | 14 #include <secder.h> |
| 15 #include <secerr.h> | 15 #include <secerr.h> |
| 16 #include <sechash.h> | 16 #include <sechash.h> |
| 17 #include <sslerr.h> | 17 #include <sslerr.h> |
| 18 | 18 |
| 19 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
| 21 #include "base/memory/singleton.h" | |
| 21 #include "base/pickle.h" | 22 #include "base/pickle.h" |
| 22 #include "base/time.h" | 23 #include "base/time.h" |
| 23 #include "crypto/nss_util.h" | 24 #include "crypto/nss_util.h" |
| 24 #include "crypto/rsa_private_key.h" | 25 #include "crypto/rsa_private_key.h" |
| 25 #include "net/base/cert_status_flags.h" | 26 #include "net/base/cert_status_flags.h" |
| 26 #include "net/base/cert_verify_result.h" | 27 #include "net/base/cert_verify_result.h" |
| 27 #include "net/base/ev_root_ca_metadata.h" | 28 #include "net/base/ev_root_ca_metadata.h" |
| 28 #include "net/base/net_errors.h" | 29 #include "net/base/net_errors.h" |
| 29 | 30 |
| 30 namespace net { | 31 namespace net { |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| 33 | 34 |
| 35 class ObCertOIDWrapper { | |
| 36 public: | |
| 37 static ObCertOIDWrapper* GetInstance() { | |
| 38 return Singleton<ObCertOIDWrapper, | |
| 39 LeakySingletonTraits<ObCertOIDWrapper> >::get(); | |
| 40 } | |
|
wtc
2011/08/23 01:32:21
I would format this method as follows:
static O
mdietz
2011/08/23 20:52:56
Done.
| |
| 41 | |
| 42 SECOidTag ob_cert_oid_tag() const { | |
| 43 return ob_cert_oid_tag_; | |
| 44 } | |
| 45 | |
| 46 private: | |
| 47 SECOidTag ob_cert_oid_tag_; | |
| 48 | |
| 49 ObCertOIDWrapper(); | |
| 50 | |
| 51 friend struct DefaultSingletonTraits<ObCertOIDWrapper>; | |
| 52 | |
| 53 DISALLOW_COPY_AND_ASSIGN(ObCertOIDWrapper); | |
| 54 }; | |
|
wtc
2011/08/23 01:32:21
Please fix the indentation in this class.
"public
mdietz
2011/08/23 20:52:56
Done.
| |
| 55 | |
| 56 ObCertOIDWrapper::ObCertOIDWrapper() | |
| 57 : ob_cert_oid_tag_(SEC_OID_UNKNOWN) { | |
|
wtc
2011/08/23 01:32:21
The Style Guide recommends indenting the colon of
mdietz
2011/08/23 20:52:56
Done.
| |
| 58 // 1.3.6.1.4.1.11129.2.1.6 | |
| 59 // (iso.org.dod.internet.private.enterprises.google.googleSecurity. | |
| 60 // certificateExtensions.originBoundCertificate) | |
| 61 static const uint8 kObCertOID[] = { | |
| 62 0x2b, 0x06, 0x01, 0x04, 0x01, 0xd6, 0x79, 0x02, 0x01, 0x06 | |
| 63 }; | |
| 64 SECOidData oid_data; | |
| 65 memset(&oid_data, 0, sizeof(oid_data)); | |
| 66 oid_data.oid.data = const_cast<uint8*>(kObCertOID); | |
| 67 oid_data.oid.len = sizeof(kObCertOID); | |
| 68 oid_data.offset = SEC_OID_UNKNOWN; | |
| 69 oid_data.desc = "Origin Bound Certificate"; | |
| 70 oid_data.mechanism = CKM_INVALID_MECHANISM; | |
| 71 oid_data.supportedExtension = SUPPORTED_CERT_EXTENSION; | |
| 72 ob_cert_oid_tag_ = SECOID_AddEntry(&oid_data); | |
| 73 if (ob_cert_oid_tag_ == SEC_OID_UNKNOWN) | |
| 74 LOG(ERROR) << "OB_CERT OID tag creation failed"; | |
| 75 } | |
| 76 | |
| 34 class ScopedCERTCertificatePolicies { | 77 class ScopedCERTCertificatePolicies { |
| 35 public: | 78 public: |
| 36 explicit ScopedCERTCertificatePolicies(CERTCertificatePolicies* policies) | 79 explicit ScopedCERTCertificatePolicies(CERTCertificatePolicies* policies) |
| 37 : policies_(policies) {} | 80 : policies_(policies) {} |
| 38 | 81 |
| 39 ~ScopedCERTCertificatePolicies() { | 82 ~ScopedCERTCertificatePolicies() { |
| 40 if (policies_) | 83 if (policies_) |
| 41 CERT_DestroyCertificatePoliciesExtension(policies_); | 84 CERT_DestroyCertificatePoliciesExtension(policies_); |
| 42 } | 85 } |
| 43 | 86 |
| (...skipping 572 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 616 fingerprint_ = CalculateFingerprint(cert_handle_); | 659 fingerprint_ = CalculateFingerprint(cert_handle_); |
| 617 | 660 |
| 618 serial_number_ = std::string( | 661 serial_number_ = std::string( |
| 619 reinterpret_cast<char*>(cert_handle_->serialNumber.data), | 662 reinterpret_cast<char*>(cert_handle_->serialNumber.data), |
| 620 cert_handle_->serialNumber.len); | 663 cert_handle_->serialNumber.len); |
| 621 // Remove leading zeros. | 664 // Remove leading zeros. |
| 622 while (serial_number_.size() > 1 && serial_number_[0] == 0) | 665 while (serial_number_.size() > 1 && serial_number_[0] == 0) |
| 623 serial_number_ = serial_number_.substr(1, serial_number_.size() - 1); | 666 serial_number_ = serial_number_.substr(1, serial_number_.size() - 1); |
| 624 } | 667 } |
| 625 | 668 |
| 626 // static | 669 // Creates a Certificate object that may be passed to the SignCertificate |
| 627 X509Certificate* X509Certificate::CreateSelfSigned( | 670 // method to generate an X509 certificate. |
| 671 // Returns NULL if an error is encountered in the certificate creation | |
| 672 // process. | |
| 673 // Caller responsible for freeing returned certificate object. | |
| 674 static CERTCertificate* CreateCertificate( | |
| 628 crypto::RSAPrivateKey* key, | 675 crypto::RSAPrivateKey* key, |
| 629 const std::string& subject, | 676 const std::string& subject, |
| 630 uint32 serial_number, | 677 uint32 serial_number, |
| 631 base::TimeDelta valid_duration) { | 678 base::TimeDelta valid_duration) { |
| 632 DCHECK(key); | |
| 633 | |
| 634 // Create info about public key. | 679 // Create info about public key. |
| 635 CERTSubjectPublicKeyInfo* spki = | 680 CERTSubjectPublicKeyInfo* spki = |
| 636 SECKEY_CreateSubjectPublicKeyInfo(key->public_key()); | 681 SECKEY_CreateSubjectPublicKeyInfo(key->public_key()); |
| 637 if (!spki) | 682 if (!spki) |
| 638 return NULL; | 683 return NULL; |
| 639 | 684 |
| 640 // Create the certificate request. | 685 // Create the certificate request. |
| 641 CERTName* subject_name = | 686 CERTName* subject_name = |
| 642 CERT_AsciiToName(const_cast<char*>(subject.c_str())); | 687 CERT_AsciiToName(const_cast<char*>(subject.c_str())); |
| 643 CERTCertificateRequest* cert_request = | 688 CERTCertificateRequest* cert_request = |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 661 if (!cert) { | 706 if (!cert) { |
| 662 PRErrorCode prerr = PR_GetError(); | 707 PRErrorCode prerr = PR_GetError(); |
| 663 LOG(ERROR) << "Failed to create certificate: " << prerr; | 708 LOG(ERROR) << "Failed to create certificate: " << prerr; |
| 664 } | 709 } |
| 665 | 710 |
| 666 // Cleanup for resources used to generate the cert. | 711 // Cleanup for resources used to generate the cert. |
| 667 CERT_DestroyName(subject_name); | 712 CERT_DestroyName(subject_name); |
| 668 CERT_DestroyValidity(validity); | 713 CERT_DestroyValidity(validity); |
| 669 CERT_DestroyCertificateRequest(cert_request); | 714 CERT_DestroyCertificateRequest(cert_request); |
| 670 | 715 |
| 671 if (!cert) | 716 return cert; |
| 672 return NULL; | 717 } |
| 673 | 718 |
| 674 // Sign the cert here. The logic of this method references SignCert() in NSS | 719 // Signs a certificate object, with |key| generating a new X509Certificate |
| 675 // utility certutil: http://mxr.mozilla.org/security/ident?i=SignCert. | 720 // and destroying the passed certificate object (even when NULL is returned). |
| 676 | 721 // The logic of this method references SignCert() in NSS utility certutil: |
| 722 // http://mxr.mozilla.org/security/ident?i=SignCert. | |
| 723 // Returns NULL if an error is encountered in the certificate signing | |
| 724 // process. | |
| 725 // Caller responsible for freeing returned X509Certificate object. | |
| 726 // | |
| 727 // TODO: change this function to return | |
| 728 // a success/failure status, and not create an X509Certificate | |
| 729 // object, and not destroy |cert| on failure. Let the caller | |
| 730 // create the X509Certificate object and destroy |cert|. | |
| 731 static X509Certificate* SignCertificate( | |
| 732 CERTCertificate* cert, | |
| 733 crypto::RSAPrivateKey* key) { | |
| 677 // |arena| is used to encode the cert. | 734 // |arena| is used to encode the cert. |
| 678 PRArenaPool* arena = cert->arena; | 735 PRArenaPool* arena = cert->arena; |
| 679 SECOidTag algo_id = SEC_GetSignatureAlgorithmOidTag(key->key()->keyType, | 736 SECOidTag algo_id = SEC_GetSignatureAlgorithmOidTag(key->key()->keyType, |
| 680 SEC_OID_SHA1); | 737 SEC_OID_SHA1); |
| 681 if (algo_id == SEC_OID_UNKNOWN) { | 738 if (algo_id == SEC_OID_UNKNOWN) { |
| 682 CERT_DestroyCertificate(cert); | 739 CERT_DestroyCertificate(cert); |
| 683 return NULL; | 740 return NULL; |
| 684 } | 741 } |
| 685 | 742 |
| 686 SECStatus rv = SECOID_SetAlgorithmID(arena, &cert->signature, algo_id, 0); | 743 SECStatus rv = SECOID_SetAlgorithmID(arena, &cert->signature, algo_id, 0); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 715 // Sign the ASN1 encoded cert and save it to |result|. | 772 // Sign the ASN1 encoded cert and save it to |result|. |
| 716 rv = SEC_DerSignData(arena, result, der.data, der.len, key->key(), algo_id); | 773 rv = SEC_DerSignData(arena, result, der.data, der.len, key->key(), algo_id); |
| 717 if (rv != SECSuccess) { | 774 if (rv != SECSuccess) { |
| 718 CERT_DestroyCertificate(cert); | 775 CERT_DestroyCertificate(cert); |
| 719 return NULL; | 776 return NULL; |
| 720 } | 777 } |
| 721 | 778 |
| 722 // Save the signed result to the cert. | 779 // Save the signed result to the cert. |
| 723 cert->derCert = *result; | 780 cert->derCert = *result; |
| 724 | 781 |
| 725 X509Certificate* x509_cert = CreateFromHandle(cert, OSCertHandles()); | 782 X509Certificate* x509_cert = |
| 783 X509Certificate::CreateFromHandle(cert, X509Certificate::OSCertHandles()); | |
|
wtc
2011/08/23 01:32:21
Nit: indent this line by four spaces.
mdietz
2011/08/23 20:52:56
Done.
| |
| 726 CERT_DestroyCertificate(cert); | 784 CERT_DestroyCertificate(cert); |
| 727 return x509_cert; | 785 return x509_cert; |
| 728 } | 786 } |
| 729 | 787 |
| 788 X509Certificate* X509Certificate::CreateSelfSigned( | |
|
wtc
2011/08/23 01:32:21
Resurrect the "// static" comment for this method
mdietz
2011/08/23 20:52:56
Done.
| |
| 789 crypto::RSAPrivateKey* key, | |
| 790 const std::string& subject, | |
| 791 uint32 serial_number, | |
| 792 base::TimeDelta valid_duration) { | |
| 793 DCHECK(key); | |
| 794 | |
| 795 CERTCertificate* cert = CreateCertificate(key, | |
| 796 subject, | |
| 797 serial_number, | |
| 798 valid_duration); | |
| 799 | |
| 800 if(!cert) | |
|
wtc
2011/08/23 01:32:21
Add a space after "if" here and on line 820 below.
mdietz
2011/08/23 20:52:56
Done.
| |
| 801 return NULL; | |
| 802 | |
| 803 X509Certificate* x509_cert = SignCertificate(cert, key); | |
| 804 | |
| 805 return x509_cert; | |
| 806 } | |
| 807 | |
| 808 X509Certificate* X509Certificate::CreateOriginBound( | |
| 809 crypto::RSAPrivateKey* key, | |
| 810 const std::string& origin, | |
| 811 uint32 serial_number, | |
| 812 base::TimeDelta valid_duration) { | |
| 813 DCHECK(key); | |
| 814 | |
| 815 CERTCertificate* cert = CreateCertificate(key, | |
| 816 "CN=anonymous.invalid", | |
| 817 serial_number, | |
| 818 valid_duration); | |
| 819 | |
| 820 if(!cert) | |
| 821 return NULL; | |
| 822 | |
| 823 // Create opaque handle used to add extensions later. | |
| 824 void* cert_handle; | |
| 825 if ((cert_handle = CERT_StartCertExtensions(cert)) == NULL) { | |
| 826 LOG(ERROR) << "Unable to get opaque handle for adding extensions"; | |
| 827 return NULL; | |
| 828 } | |
| 829 | |
| 830 // Create SECItem for IA5String encoding. | |
| 831 SECItem origin_string_item = { | |
| 832 siAsciiString, | |
| 833 (unsigned char*)origin.data(), | |
| 834 origin.size() | |
| 835 }; | |
| 836 | |
| 837 // IA5Encode and arena allocate SECItem | |
| 838 SECItem* asn1_origin_string = SEC_ASN1EncodeItem( | |
| 839 cert->arena, NULL, &origin_string_item, | |
| 840 SEC_ASN1_GET(SEC_IA5StringTemplate)); | |
| 841 if (asn1_origin_string == NULL) { | |
| 842 LOG(ERROR) << "Unable to get ASN1 encoding for origin in ob_cert extension"; | |
| 843 return NULL; | |
| 844 } | |
| 845 | |
| 846 // Add the extension to the opaque handle | |
| 847 if (CERT_AddExtension(cert_handle, | |
| 848 ObCertOIDWrapper::GetInstance()->ob_cert_oid_tag(), | |
| 849 asn1_origin_string, | |
| 850 PR_TRUE, PR_TRUE) | |
| 851 != SECSuccess){ | |
|
wtc
2011/08/23 01:32:21
Nit: move this to the previous line.
mdietz
2011/08/23 20:52:56
Done.
| |
| 852 LOG(ERROR) << "Unable to add origin bound cert extension to opaque handle"; | |
| 853 return NULL; | |
| 854 } | |
| 855 | |
| 856 // Copy extension into x509 cert | |
| 857 if (CERT_FinishExtensions(cert_handle) != SECSuccess){ | |
| 858 LOG(ERROR) << "Unable to copy extension to X509 cert"; | |
| 859 return NULL; | |
| 860 } | |
| 861 | |
| 862 X509Certificate* x509_cert = SignCertificate(cert, key); | |
| 863 | |
| 864 return x509_cert; | |
| 865 } | |
| 866 | |
| 730 void X509Certificate::GetSubjectAltName( | 867 void X509Certificate::GetSubjectAltName( |
| 731 std::vector<std::string>* dns_names, | 868 std::vector<std::string>* dns_names, |
| 732 std::vector<std::string>* ip_addrs) const { | 869 std::vector<std::string>* ip_addrs) const { |
| 733 if (dns_names) | 870 if (dns_names) |
| 734 dns_names->clear(); | 871 dns_names->clear(); |
| 735 if (ip_addrs) | 872 if (ip_addrs) |
| 736 ip_addrs->clear(); | 873 ip_addrs->clear(); |
| 737 | 874 |
| 738 SECItem alt_name; | 875 SECItem alt_name; |
| 739 SECStatus rv = CERT_FindCertExtension(cert_handle_, | 876 SECStatus rv = CERT_FindCertExtension(cert_handle_, |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1009 | 1146 |
| 1010 // static | 1147 // static |
| 1011 bool X509Certificate::WriteOSCertHandleToPickle(OSCertHandle cert_handle, | 1148 bool X509Certificate::WriteOSCertHandleToPickle(OSCertHandle cert_handle, |
| 1012 Pickle* pickle) { | 1149 Pickle* pickle) { |
| 1013 return pickle->WriteData( | 1150 return pickle->WriteData( |
| 1014 reinterpret_cast<const char*>(cert_handle->derCert.data), | 1151 reinterpret_cast<const char*>(cert_handle->derCert.data), |
| 1015 cert_handle->derCert.len); | 1152 cert_handle->derCert.len); |
| 1016 } | 1153 } |
| 1017 | 1154 |
| 1018 } // namespace net | 1155 } // namespace net |
| OLD | NEW |