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

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

Issue 7384002: Added CreateOriginBound method to x509_certificate.h. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Fixed that indentation error that keeps popping up. 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"
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 // 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
34 class ScopedCERTCertificatePolicies { 79 class ScopedCERTCertificatePolicies {
35 public: 80 public:
36 explicit ScopedCERTCertificatePolicies(CERTCertificatePolicies* policies) 81 explicit ScopedCERTCertificatePolicies(CERTCertificatePolicies* policies)
37 : policies_(policies) {} 82 : policies_(policies) {}
38 83
39 ~ScopedCERTCertificatePolicies() { 84 ~ScopedCERTCertificatePolicies() {
40 if (policies_) 85 if (policies_)
41 CERT_DestroyCertificatePoliciesExtension(policies_); 86 CERT_DestroyCertificatePoliciesExtension(policies_);
42 } 87 }
43 88
(...skipping 572 matching lines...) Expand 10 before | Expand all | Expand 10 after
616 fingerprint_ = CalculateFingerprint(cert_handle_); 661 fingerprint_ = CalculateFingerprint(cert_handle_);
617 662
618 serial_number_ = std::string( 663 serial_number_ = std::string(
619 reinterpret_cast<char*>(cert_handle_->serialNumber.data), 664 reinterpret_cast<char*>(cert_handle_->serialNumber.data),
620 cert_handle_->serialNumber.len); 665 cert_handle_->serialNumber.len);
621 // Remove leading zeros. 666 // Remove leading zeros.
622 while (serial_number_.size() > 1 && serial_number_[0] == 0) 667 while (serial_number_.size() > 1 && serial_number_[0] == 0)
623 serial_number_ = serial_number_.substr(1, serial_number_.size() - 1); 668 serial_number_ = serial_number_.substr(1, serial_number_.size() - 1);
624 } 669 }
625 670
626 // static 671 // Creates a Certificate object that may be passed to the SignCertificate
627 X509Certificate* X509Certificate::CreateSelfSigned( 672 // method to generate an X509 certificate.
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(
628 crypto::RSAPrivateKey* key, 677 crypto::RSAPrivateKey* key,
629 const std::string& subject, 678 const std::string& subject,
630 uint32 serial_number, 679 uint32 serial_number,
631 base::TimeDelta valid_duration) { 680 base::TimeDelta valid_duration) {
632 DCHECK(key);
633
634 // Create info about public key. 681 // Create info about public key.
635 CERTSubjectPublicKeyInfo* spki = 682 CERTSubjectPublicKeyInfo* spki =
636 SECKEY_CreateSubjectPublicKeyInfo(key->public_key()); 683 SECKEY_CreateSubjectPublicKeyInfo(key->public_key());
637 if (!spki) 684 if (!spki)
638 return NULL; 685 return NULL;
639 686
640 // Create the certificate request. 687 // Create the certificate request.
641 CERTName* subject_name = 688 CERTName* subject_name =
642 CERT_AsciiToName(const_cast<char*>(subject.c_str())); 689 CERT_AsciiToName(const_cast<char*>(subject.c_str()));
643 CERTCertificateRequest* cert_request = 690 CERTCertificateRequest* cert_request =
(...skipping 17 matching lines...) Expand all
661 if (!cert) { 708 if (!cert) {
662 PRErrorCode prerr = PR_GetError(); 709 PRErrorCode prerr = PR_GetError();
663 LOG(ERROR) << "Failed to create certificate: " << prerr; 710 LOG(ERROR) << "Failed to create certificate: " << prerr;
664 } 711 }
665 712
666 // Cleanup for resources used to generate the cert. 713 // Cleanup for resources used to generate the cert.
667 CERT_DestroyName(subject_name); 714 CERT_DestroyName(subject_name);
668 CERT_DestroyValidity(validity); 715 CERT_DestroyValidity(validity);
669 CERT_DestroyCertificateRequest(cert_request); 716 CERT_DestroyCertificateRequest(cert_request);
670 717
671 if (!cert) 718 return cert;
672 return NULL; 719 }
673 720
674 // Sign the cert here. The logic of this method references SignCert() in NSS 721 // Signs a certificate object, with |key| generating a new X509Certificate
675 // utility certutil: http://mxr.mozilla.org/security/ident?i=SignCert. 722 // and destroying the passed certificate object (even when NULL is returned).
676 723 // The logic of this method references SignCert() in NSS utility certutil:
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) {
677 // |arena| is used to encode the cert. 736 // |arena| is used to encode the cert.
678 PRArenaPool* arena = cert->arena; 737 PRArenaPool* arena = cert->arena;
679 SECOidTag algo_id = SEC_GetSignatureAlgorithmOidTag(key->key()->keyType, 738 SECOidTag algo_id = SEC_GetSignatureAlgorithmOidTag(key->key()->keyType,
680 SEC_OID_SHA1); 739 SEC_OID_SHA1);
681 if (algo_id == SEC_OID_UNKNOWN) { 740 if (algo_id == SEC_OID_UNKNOWN) {
682 CERT_DestroyCertificate(cert); 741 CERT_DestroyCertificate(cert);
683 return NULL; 742 return NULL;
684 } 743 }
685 744
686 SECStatus rv = SECOID_SetAlgorithmID(arena, &cert->signature, algo_id, 0); 745 SECStatus rv = SECOID_SetAlgorithmID(arena, &cert->signature, algo_id, 0);
(...skipping 28 matching lines...) Expand all
715 // Sign the ASN1 encoded cert and save it to |result|. 774 // Sign the ASN1 encoded cert and save it to |result|.
716 rv = SEC_DerSignData(arena, result, der.data, der.len, key->key(), algo_id); 775 rv = SEC_DerSignData(arena, result, der.data, der.len, key->key(), algo_id);
717 if (rv != SECSuccess) { 776 if (rv != SECSuccess) {
718 CERT_DestroyCertificate(cert); 777 CERT_DestroyCertificate(cert);
719 return NULL; 778 return NULL;
720 } 779 }
721 780
722 // Save the signed result to the cert. 781 // Save the signed result to the cert.
723 cert->derCert = *result; 782 cert->derCert = *result;
724 783
725 X509Certificate* x509_cert = CreateFromHandle(cert, OSCertHandles()); 784 X509Certificate* x509_cert =
785 X509Certificate::CreateFromHandle(cert, X509Certificate::OSCertHandles());
726 CERT_DestroyCertificate(cert); 786 CERT_DestroyCertificate(cert);
727 return x509_cert; 787 return x509_cert;
728 } 788 }
729 789
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
730 void X509Certificate::GetSubjectAltName( 870 void X509Certificate::GetSubjectAltName(
731 std::vector<std::string>* dns_names, 871 std::vector<std::string>* dns_names,
732 std::vector<std::string>* ip_addrs) const { 872 std::vector<std::string>* ip_addrs) const {
733 if (dns_names) 873 if (dns_names)
734 dns_names->clear(); 874 dns_names->clear();
735 if (ip_addrs) 875 if (ip_addrs)
736 ip_addrs->clear(); 876 ip_addrs->clear();
737 877
738 SECItem alt_name; 878 SECItem alt_name;
739 SECStatus rv = CERT_FindCertExtension(cert_handle_, 879 SECStatus rv = CERT_FindCertExtension(cert_handle_,
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
1009 1149
1010 // static 1150 // static
1011 bool X509Certificate::WriteOSCertHandleToPickle(OSCertHandle cert_handle, 1151 bool X509Certificate::WriteOSCertHandleToPickle(OSCertHandle cert_handle,
1012 Pickle* pickle) { 1152 Pickle* pickle) {
1013 return pickle->WriteData( 1153 return pickle->WriteData(
1014 reinterpret_cast<const char*>(cert_handle->derCert.data), 1154 reinterpret_cast<const char*>(cert_handle->derCert.data),
1015 cert_handle->derCert.len); 1155 cert_handle->derCert.len);
1016 } 1156 }
1017 1157
1018 } // namespace net 1158 } // 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