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

Side by Side Diff: net/base/x509_certificate_nss.cc

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

Powered by Google App Engine
This is Rietveld 408576698