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> |
(...skipping 737 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
748 | 748 |
749 // Save the signed result to the cert. | 749 // Save the signed result to the cert. |
750 cert->derCert = *result; | 750 cert->derCert = *result; |
751 | 751 |
752 X509Certificate* x509_cert = | 752 X509Certificate* x509_cert = |
753 CreateFromHandle(cert, SOURCE_LONE_CERT_IMPORT, OSCertHandles()); | 753 CreateFromHandle(cert, SOURCE_LONE_CERT_IMPORT, OSCertHandles()); |
754 CERT_DestroyCertificate(cert); | 754 CERT_DestroyCertificate(cert); |
755 return x509_cert; | 755 return x509_cert; |
756 } | 756 } |
757 | 757 |
758 // static | |
759 X509Certificate* X509Certificate::CreateOriginBound( | |
wtc
2011/08/04 00:37:53
The CreateOriginBound function is identical to Cre
mdietz
2011/08/18 00:02:45
The create origin bound code block in the middle c
| |
760 crypto::RSAPrivateKey* key, | |
761 const std::string& subject, | |
762 const std::string& origin, | |
763 uint32 serial_number, | |
764 base::TimeDelta valid_duration) { | |
765 DCHECK(key); | |
766 | |
767 // Create info about public key. | |
768 CERTSubjectPublicKeyInfo* spki = | |
769 SECKEY_CreateSubjectPublicKeyInfo(key->public_key()); | |
770 if (!spki) | |
771 return NULL; | |
772 | |
773 // Create the certificate request. | |
774 CERTName* subject_name = | |
775 CERT_AsciiToName(const_cast<char*>(subject.c_str())); | |
776 CERTCertificateRequest* cert_request = | |
777 CERT_CreateCertificateRequest(subject_name, spki, NULL); | |
778 SECKEY_DestroySubjectPublicKeyInfo(spki); | |
779 | |
780 if (!cert_request) { | |
781 PRErrorCode prerr = PR_GetError(); | |
782 LOG(ERROR) << "Failed to create certificate request: " << prerr; | |
783 CERT_DestroyName(subject_name); | |
784 return NULL; | |
785 } | |
786 | |
787 PRTime now = PR_Now(); | |
788 PRTime not_after = now + valid_duration.InMicroseconds(); | |
789 | |
790 // Note that the time is now in micro-second unit. | |
791 CERTValidity* validity = CERT_CreateValidity(now, not_after); | |
792 CERTCertificate* cert = CERT_CreateCertificate(serial_number, subject_name, | |
793 validity, cert_request); | |
794 if (!cert) { | |
795 PRErrorCode prerr = PR_GetError(); | |
796 LOG(ERROR) << "Failed to create certificate: " << prerr; | |
797 } | |
798 | |
799 // Cleanup for resources used to generate the cert. | |
800 CERT_DestroyName(subject_name); | |
801 CERT_DestroyValidity(validity); | |
802 CERT_DestroyCertificateRequest(cert_request); | |
803 | |
804 if (!cert) | |
805 return NULL; | |
806 | |
807 /* Configure X509 library to handle the OB_CERT X509 Extension OID */ | |
wtc
2011/08/04 00:37:53
Use the C++ comment delimiter //.
The Style Guide
mdietz
2011/08/18 00:02:45
Done.
| |
808 static bool ob_cert_oid_tag_defined; | |
809 static SECOidTag ob_cert_oid_tag; | |
810 | |
811 if (!ob_cert_oid_tag_defined) { | |
wtc
2011/08/04 00:37:53
Chrome has a Singleton and LazyInstance class to d
mdietz
2011/08/18 00:02:45
Implemented as a Sinlgeton wrapper around a SECOid
| |
812 // It's harmless if multiple threads enter this block concurrently. | |
813 static const uint8 kObCertOID[] = | |
814 // 1.3.6.1.4.1.11129.2.1.6 | |
815 // (iso.org.dod.internet.private.enterprises.google.googleSecurity. | |
816 // certificateExtensions.originBoundCertificate) | |
817 {0x2b, 0x06, 0x01, 0x04, 0x01, 0xd6, 0x79, 0x02, 0x01, 0x06}; | |
wtc
2011/08/04 00:37:53
I would format this as follows:
static const uin
mdietz
2011/08/18 00:02:45
Done.
mdietz
2011/08/18 00:02:45
Done.
| |
818 SECOidData oid_data; | |
819 memset(&oid_data, 0, sizeof(oid_data)); | |
820 oid_data.oid.data = const_cast<uint8*>(kObCertOID); | |
821 oid_data.oid.len = sizeof(kObCertOID); | |
wtc
2011/08/04 00:37:53
Add
oid_data.offset = SEC_OID_UNKNOWN;
This is
mdietz
2011/08/18 00:02:45
Done.
| |
822 oid_data.desc = "Origin Bound Certificate"; | |
wtc
2011/08/04 00:37:53
Add
oid_data.mechanism = CKM_INVALID_MECHANISM;
mdietz
2011/08/18 00:02:45
Done.
| |
823 oid_data.supportedExtension = SUPPORTED_CERT_EXTENSION; | |
824 ob_cert_oid_tag = SECOID_AddEntry(&oid_data); | |
825 DCHECK_NE(SEC_OID_UNKNOWN, ob_cert_oid_tag); | |
wtc
2011/08/04 00:37:53
Remove this DCHECK_NE because it has proper error
mdietz
2011/08/18 00:02:45
Done.
| |
826 if(ob_cert_oid_tag == SEC_OID_UNKNOWN) { | |
827 LOG(ERROR) << "OB_CERT OID tag creation failed"; | |
828 return NULL; | |
829 } | |
830 ob_cert_oid_tag_defined = true; | |
831 } | |
832 | |
833 SECStatus ok; | |
834 | |
835 // Create opaque handle to add extensions | |
836 void* cert_handle; | |
837 if((cert_handle = CERT_StartCertExtensions(cert)) == NULL){ | |
wtc
2011/08/04 00:37:53
Add a space after "if".
Add a space before the op
mdietz
2011/08/18 00:02:45
Done.
| |
838 LOG(ERROR) << "Unable to get opaque handle for adding extensions"; | |
839 return NULL; | |
840 } | |
841 | |
842 // Create stack allocated SECItem for IA5String encoding | |
843 SECItem origin_string_item = {siAsciiString, | |
844 (unsigned char*)origin.c_str(), | |
845 origin.size()+1}; | |
wtc
2011/08/04 00:37:53
The terminating null should not be included.
I wo
mdietz
2011/08/18 00:02:45
Done.
Note to self: Apply same to unittest.
| |
846 | |
847 // IA5Encode and arena allocate SECItem | |
848 SECItem *asn1_origin_string; | |
wtc
2011/08/04 00:37:53
Put * next to the type.
| |
849 if((asn1_origin_string = SEC_ASN1EncodeItem( | |
850 cert->arena, | |
851 NULL, | |
852 &origin_string_item, | |
853 SEC_ASN1_GET(SEC_IA5StringTemplate))) | |
wtc
2011/08/04 00:37:53
Indent the wrapped parameters by four spaces.
mdietz
2011/08/18 00:02:45
Done.
This might exceed 80 lines on the last line
| |
854 == NULL){ | |
855 LOG(ERROR) << "Unable to get ASN1 encoding for origin in ob_cert extension"; | |
856 return NULL; | |
857 } | |
858 | |
859 // Add the extension to the opaque handle | |
860 if((ok = CERT_AddExtension(cert_handle, ob_cert_oid_tag, asn1_origin_string, | |
861 PR_TRUE, PR_TRUE)) | |
862 != SECSuccess){ | |
863 LOG(ERROR) << "Unable to add origin bound cert extension to opaque handle"; | |
864 return NULL; | |
865 } | |
866 | |
867 // Copy extension into x509 cert | |
868 if((ok = CERT_FinishExtensions(cert_handle)) != SECSuccess){ | |
869 LOG(ERROR) << "Unable to copy extension to X509 cert"; | |
870 return NULL; | |
871 } | |
872 | |
873 // Sign the cert here. The logic of this method references SignCert() in NSS | |
874 // utility certutil: http://mxr.mozilla.org/security/ident?i=SignCert. | |
875 | |
876 // |arena| is used to encode the cert. | |
877 PRArenaPool* arena = cert->arena; | |
878 SECOidTag algo_id = SEC_GetSignatureAlgorithmOidTag(key->key()->keyType, | |
879 SEC_OID_SHA1); | |
880 if (algo_id == SEC_OID_UNKNOWN) { | |
881 CERT_DestroyCertificate(cert); | |
882 return NULL; | |
883 } | |
884 | |
885 SECStatus rv = SECOID_SetAlgorithmID(arena, &cert->signature, algo_id, 0); | |
886 if (rv != SECSuccess) { | |
887 CERT_DestroyCertificate(cert); | |
888 return NULL; | |
889 } | |
890 | |
891 // Generate a cert of version 3. | |
892 *(cert->version.data) = 2; | |
893 cert->version.len = 1; | |
894 | |
895 SECItem der; | |
896 der.len = 0; | |
897 der.data = NULL; | |
898 | |
899 // Use ASN1 DER to encode the cert. | |
900 void* encode_result = SEC_ASN1EncodeItem( | |
901 arena, &der, cert, SEC_ASN1_GET(CERT_CertificateTemplate)); | |
902 if (!encode_result) { | |
903 CERT_DestroyCertificate(cert); | |
904 return NULL; | |
905 } | |
906 | |
907 // Allocate space to contain the signed cert. | |
908 SECItem* result = SECITEM_AllocItem(arena, NULL, 0); | |
909 if (!result) { | |
910 CERT_DestroyCertificate(cert); | |
911 return NULL; | |
912 } | |
913 | |
914 // Sign the ASN1 encoded cert and save it to |result|. | |
915 rv = SEC_DerSignData(arena, result, der.data, der.len, key->key(), algo_id); | |
916 if (rv != SECSuccess) { | |
917 CERT_DestroyCertificate(cert); | |
918 return NULL; | |
919 } | |
920 | |
921 // Save the signed result to the cert. | |
922 cert->derCert = *result; | |
923 | |
924 X509Certificate* x509_cert = | |
925 CreateFromHandle(cert, SOURCE_LONE_CERT_IMPORT, OSCertHandles()); | |
926 CERT_DestroyCertificate(cert); | |
927 return x509_cert; | |
928 } | |
929 | |
758 void X509Certificate::GetDNSNames(std::vector<std::string>* dns_names) const { | 930 void X509Certificate::GetDNSNames(std::vector<std::string>* dns_names) const { |
759 dns_names->clear(); | 931 dns_names->clear(); |
760 | 932 |
761 // Compare with CERT_VerifyCertName(). | 933 // Compare with CERT_VerifyCertName(). |
762 GetCertSubjectAltNamesOfType(cert_handle_, certDNSName, dns_names); | 934 GetCertSubjectAltNamesOfType(cert_handle_, certDNSName, dns_names); |
763 | 935 |
764 if (dns_names->empty()) | 936 if (dns_names->empty()) |
765 dns_names->push_back(subject_.common_name); | 937 dns_names->push_back(subject_.common_name); |
766 } | 938 } |
767 | 939 |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1003 | 1175 |
1004 // static | 1176 // static |
1005 bool X509Certificate::WriteOSCertHandleToPickle(OSCertHandle cert_handle, | 1177 bool X509Certificate::WriteOSCertHandleToPickle(OSCertHandle cert_handle, |
1006 Pickle* pickle) { | 1178 Pickle* pickle) { |
1007 return pickle->WriteData( | 1179 return pickle->WriteData( |
1008 reinterpret_cast<const char*>(cert_handle->derCert.data), | 1180 reinterpret_cast<const char*>(cert_handle->derCert.data), |
1009 cert_handle->derCert.len); | 1181 cert_handle->derCert.len); |
1010 } | 1182 } |
1011 | 1183 |
1012 } // namespace net | 1184 } // namespace net |
OLD | NEW |