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

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

Issue 8296014: Use NSS to generate Origin-Bound Certs on Win and Mac. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review changes Created 9 years, 2 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
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>
10 #include <nss.h> 9 #include <nss.h>
11 #include <pk11pub.h> 10 #include <pk11pub.h>
12 #include <prerror.h> 11 #include <prerror.h>
13 #include <prtime.h> 12 #include <prtime.h>
14 #include <secder.h> 13 #include <secder.h>
15 #include <secerr.h> 14 #include <secerr.h>
16 #include <sechash.h> 15 #include <sechash.h>
17 #include <sslerr.h> 16 #include <sslerr.h>
18 17
19 #include "base/logging.h" 18 #include "base/logging.h"
20 #include "base/memory/scoped_ptr.h" 19 #include "base/memory/scoped_ptr.h"
21 #include "base/memory/singleton.h"
22 #include "base/pickle.h" 20 #include "base/pickle.h"
23 #include "base/time.h" 21 #include "base/time.h"
24 #include "crypto/nss_util.h" 22 #include "crypto/nss_util.h"
25 #include "crypto/rsa_private_key.h" 23 #include "crypto/rsa_private_key.h"
26 #include "net/base/cert_status_flags.h" 24 #include "net/base/cert_status_flags.h"
27 #include "net/base/cert_verify_result.h" 25 #include "net/base/cert_verify_result.h"
28 #include "net/base/ev_root_ca_metadata.h" 26 #include "net/base/ev_root_ca_metadata.h"
29 #include "net/base/net_errors.h" 27 #include "net/base/net_errors.h"
28 #include "net/base/x509_util_nss.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
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(
677 crypto::RSAPrivateKey* key,
678 const std::string& subject,
679 uint32 serial_number,
680 base::TimeDelta valid_duration) {
681 // Create info about public key.
682 CERTSubjectPublicKeyInfo* spki =
683 SECKEY_CreateSubjectPublicKeyInfo(key->public_key());
684 if (!spki)
685 return NULL;
686
687 // Create the certificate request.
688 CERTName* subject_name =
689 CERT_AsciiToName(const_cast<char*>(subject.c_str()));
690 CERTCertificateRequest* cert_request =
691 CERT_CreateCertificateRequest(subject_name, spki, NULL);
692 SECKEY_DestroySubjectPublicKeyInfo(spki);
693
694 if (!cert_request) {
695 PRErrorCode prerr = PR_GetError();
696 LOG(ERROR) << "Failed to create certificate request: " << prerr;
697 CERT_DestroyName(subject_name);
698 return NULL;
699 }
700
701 PRTime now = PR_Now();
702 PRTime not_after = now + valid_duration.InMicroseconds();
703
704 // Note that the time is now in micro-second unit.
705 CERTValidity* validity = CERT_CreateValidity(now, not_after);
706 CERTCertificate* cert = CERT_CreateCertificate(serial_number, subject_name,
707 validity, cert_request);
708 if (!cert) {
709 PRErrorCode prerr = PR_GetError();
710 LOG(ERROR) << "Failed to create certificate: " << prerr;
711 }
712
713 // Cleanup for resources used to generate the cert.
714 CERT_DestroyName(subject_name);
715 CERT_DestroyValidity(validity);
716 CERT_DestroyCertificateRequest(cert_request);
717
718 return cert;
719 }
720
721 // Signs a certificate object, with |key| generating a new X509Certificate
722 // and destroying the passed certificate object (even when NULL is returned).
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) {
736 // |arena| is used to encode the cert.
737 PRArenaPool* arena = cert->arena;
738 SECOidTag algo_id = SEC_GetSignatureAlgorithmOidTag(key->key()->keyType,
739 SEC_OID_SHA1);
740 if (algo_id == SEC_OID_UNKNOWN) {
741 CERT_DestroyCertificate(cert);
742 return NULL;
743 }
744
745 SECStatus rv = SECOID_SetAlgorithmID(arena, &cert->signature, algo_id, 0);
746 if (rv != SECSuccess) {
747 CERT_DestroyCertificate(cert);
748 return NULL;
749 }
750
751 // Generate a cert of version 3.
752 *(cert->version.data) = 2;
753 cert->version.len = 1;
754
755 SECItem der;
756 der.len = 0;
757 der.data = NULL;
758
759 // Use ASN1 DER to encode the cert.
760 void* encode_result = SEC_ASN1EncodeItem(
761 arena, &der, cert, SEC_ASN1_GET(CERT_CertificateTemplate));
762 if (!encode_result) {
763 CERT_DestroyCertificate(cert);
764 return NULL;
765 }
766
767 // Allocate space to contain the signed cert.
768 SECItem* result = SECITEM_AllocItem(arena, NULL, 0);
769 if (!result) {
770 CERT_DestroyCertificate(cert);
771 return NULL;
772 }
773
774 // Sign the ASN1 encoded cert and save it to |result|.
775 rv = SEC_DerSignData(arena, result, der.data, der.len, key->key(), algo_id);
776 if (rv != SECSuccess) {
777 CERT_DestroyCertificate(cert);
778 return NULL;
779 }
780
781 // Save the signed result to the cert.
782 cert->derCert = *result;
783
784 X509Certificate* x509_cert =
785 X509Certificate::CreateFromHandle(cert, X509Certificate::OSCertHandles());
786 CERT_DestroyCertificate(cert);
787 return x509_cert;
788 }
789
790 // static 626 // static
791 X509Certificate* X509Certificate::CreateSelfSigned( 627 X509Certificate* X509Certificate::CreateSelfSigned(
792 crypto::RSAPrivateKey* key, 628 crypto::RSAPrivateKey* key,
793 const std::string& subject, 629 const std::string& subject,
794 uint32 serial_number, 630 uint32 serial_number,
795 base::TimeDelta valid_duration) { 631 base::TimeDelta valid_duration) {
796 DCHECK(key); 632 DCHECK(key);
797 633
798 CERTCertificate* cert = CreateCertificate(key, 634 CERTCertificate* cert = x509_util::CreateSelfSignedCert(key->public_key(),
799 subject, 635 key->key(),
800 serial_number, 636 subject,
801 valid_duration); 637 serial_number,
638 valid_duration);
802 639
803 if (!cert) 640 if (!cert)
804 return NULL; 641 return NULL;
805 642
806 X509Certificate* x509_cert = SignCertificate(cert, key); 643 return X509Certificate::CreateFromHandle(cert,
807 644 X509Certificate::OSCertHandles());
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 } 645 }
869 646
870 void X509Certificate::GetSubjectAltName( 647 void X509Certificate::GetSubjectAltName(
871 std::vector<std::string>* dns_names, 648 std::vector<std::string>* dns_names,
872 std::vector<std::string>* ip_addrs) const { 649 std::vector<std::string>* ip_addrs) const {
873 if (dns_names) 650 if (dns_names)
874 dns_names->clear(); 651 dns_names->clear();
875 if (ip_addrs) 652 if (ip_addrs)
876 ip_addrs->clear(); 653 ip_addrs->clear();
877 654
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
1149 926
1150 // static 927 // static
1151 bool X509Certificate::WriteOSCertHandleToPickle(OSCertHandle cert_handle, 928 bool X509Certificate::WriteOSCertHandleToPickle(OSCertHandle cert_handle,
1152 Pickle* pickle) { 929 Pickle* pickle) {
1153 return pickle->WriteData( 930 return pickle->WriteData(
1154 reinterpret_cast<const char*>(cert_handle->derCert.data), 931 reinterpret_cast<const char*>(cert_handle->derCert.data),
1155 cert_handle->derCert.len); 932 cert_handle->derCert.len);
1156 } 933 }
1157 934
1158 } // namespace net 935 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698