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> | |
|
wtc
2011/12/08 00:07:43
The new code you added doesn't seem to require <ke
Greg Spencer (Chromium)
2011/12/09 18:51:38
Done.
| |
| 9 #include <nss.h> | 10 #include <nss.h> |
| 10 #include <pk11pub.h> | 11 #include <pk11pub.h> |
| 11 #include <prerror.h> | 12 #include <prerror.h> |
| 12 #include <prtime.h> | 13 #include <prtime.h> |
| 13 #include <secder.h> | 14 #include <secder.h> |
| 14 #include <secerr.h> | 15 #include <secerr.h> |
| 15 #include <sechash.h> | 16 #include <sechash.h> |
| 16 #include <sslerr.h> | 17 #include <sslerr.h> |
| 17 | 18 |
| 18 #include "base/logging.h" | 19 #include "base/logging.h" |
| (...skipping 640 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 659 void AppendPublicKeyHashes(CERTCertList* cert_list, | 660 void AppendPublicKeyHashes(CERTCertList* cert_list, |
| 660 CERTCertificate* root_cert, | 661 CERTCertificate* root_cert, |
| 661 std::vector<SHA1Fingerprint>* hashes) { | 662 std::vector<SHA1Fingerprint>* hashes) { |
| 662 for (CERTCertListNode* node = CERT_LIST_HEAD(cert_list); | 663 for (CERTCertListNode* node = CERT_LIST_HEAD(cert_list); |
| 663 !CERT_LIST_END(node, cert_list); | 664 !CERT_LIST_END(node, cert_list); |
| 664 node = CERT_LIST_NEXT(node)) { | 665 node = CERT_LIST_NEXT(node)) { |
| 665 hashes->push_back(CertPublicKeyHash(node->cert)); | 666 hashes->push_back(CertPublicKeyHash(node->cert)); |
| 666 } | 667 } |
| 667 hashes->push_back(CertPublicKeyHash(root_cert)); | 668 hashes->push_back(CertPublicKeyHash(root_cert)); |
| 668 } | 669 } |
| 669 | |
|
wtc
2011/12/08 00:07:43
Nit: add this blank line back. This matches the b
Greg Spencer (Chromium)
2011/12/09 18:51:38
Done.
| |
| 670 } // namespace | 670 } // namespace |
| 671 | 671 |
| 672 void X509Certificate::Initialize() { | 672 void X509Certificate::Initialize() { |
| 673 ParsePrincipal(&cert_handle_->subject, &subject_); | 673 ParsePrincipal(&cert_handle_->subject, &subject_); |
| 674 ParsePrincipal(&cert_handle_->issuer, &issuer_); | 674 ParsePrincipal(&cert_handle_->issuer, &issuer_); |
| 675 | 675 |
| 676 ParseDate(&cert_handle_->validity.notBefore, &valid_start_); | 676 ParseDate(&cert_handle_->validity.notBefore, &valid_start_); |
| 677 ParseDate(&cert_handle_->validity.notAfter, &valid_expiry_); | 677 ParseDate(&cert_handle_->validity.notAfter, &valid_expiry_); |
| 678 | 678 |
| 679 fingerprint_ = CalculateFingerprint(cert_handle_); | 679 fingerprint_ = CalculateFingerprint(cert_handle_); |
| 680 ca_fingerprint_ = CalculateCAFingerprint(intermediate_ca_certs_); | 680 ca_fingerprint_ = CalculateCAFingerprint(intermediate_ca_certs_); |
| 681 | 681 |
| 682 serial_number_ = std::string( | 682 serial_number_ = std::string( |
| 683 reinterpret_cast<char*>(cert_handle_->serialNumber.data), | 683 reinterpret_cast<char*>(cert_handle_->serialNumber.data), |
| 684 cert_handle_->serialNumber.len); | 684 cert_handle_->serialNumber.len); |
| 685 } | 685 } |
| 686 | 686 |
| 687 // static | 687 // static |
| 688 X509Certificate* X509Certificate::CreateFromBytesWithNickname( | |
| 689 const char* data, | |
| 690 int length, | |
| 691 const char* nickname) { | |
| 692 OSCertHandle cert_handle = CreateOSCertHandleFromBytesWithNickname(data, | |
| 693 length, | |
| 694 nickname); | |
| 695 if (!cert_handle) | |
| 696 return NULL; | |
| 697 | |
| 698 X509Certificate* cert = CreateFromHandle(cert_handle, OSCertHandles()); | |
| 699 FreeOSCertHandle(cert_handle); | |
| 700 return cert; | |
| 701 } | |
| 702 | |
| 703 // static | |
| 688 X509Certificate* X509Certificate::CreateSelfSigned( | 704 X509Certificate* X509Certificate::CreateSelfSigned( |
| 689 crypto::RSAPrivateKey* key, | 705 crypto::RSAPrivateKey* key, |
| 690 const std::string& subject, | 706 const std::string& subject, |
| 691 uint32 serial_number, | 707 uint32 serial_number, |
| 692 base::TimeDelta valid_duration) { | 708 base::TimeDelta valid_duration) { |
| 693 DCHECK(key); | 709 DCHECK(key); |
| 694 | 710 |
| 695 CERTCertificate* cert = x509_util::CreateSelfSignedCert(key->public_key(), | 711 CERTCertificate* cert = x509_util::CreateSelfSignedCert(key->public_key(), |
| 696 key->key(), | 712 key->key(), |
| 697 subject, | 713 subject, |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 915 DCHECK(a && b); | 931 DCHECK(a && b); |
| 916 if (a == b) | 932 if (a == b) |
| 917 return true; | 933 return true; |
| 918 return a->derCert.len == b->derCert.len && | 934 return a->derCert.len == b->derCert.len && |
| 919 memcmp(a->derCert.data, b->derCert.data, a->derCert.len) == 0; | 935 memcmp(a->derCert.data, b->derCert.data, a->derCert.len) == 0; |
| 920 } | 936 } |
| 921 | 937 |
| 922 // static | 938 // static |
| 923 X509Certificate::OSCertHandle X509Certificate::CreateOSCertHandleFromBytes( | 939 X509Certificate::OSCertHandle X509Certificate::CreateOSCertHandleFromBytes( |
| 924 const char* data, int length) { | 940 const char* data, int length) { |
| 941 return CreateOSCertHandleFromBytesWithNickname(data, length, NULL); | |
| 942 } | |
| 943 | |
| 944 // static | |
| 945 X509Certificate::OSCertHandle | |
| 946 X509Certificate::CreateOSCertHandleFromBytesWithNickname( | |
| 947 const char* data, int length, const char* nickname) { | |
| 925 if (length < 0) | 948 if (length < 0) |
| 926 return NULL; | 949 return NULL; |
| 927 | 950 |
| 928 crypto::EnsureNSSInit(); | 951 crypto::EnsureNSSInit(); |
| 929 | 952 |
| 930 if (!NSS_IsInitialized()) | 953 if (!NSS_IsInitialized()) |
| 931 return NULL; | 954 return NULL; |
| 932 | 955 |
| 933 SECItem der_cert; | 956 SECItem der_cert; |
| 934 der_cert.data = reinterpret_cast<unsigned char*>(const_cast<char*>(data)); | 957 der_cert.data = reinterpret_cast<unsigned char*>(const_cast<char*>(data)); |
| 935 der_cert.len = length; | 958 der_cert.len = length; |
| 936 der_cert.type = siDERCertBuffer; | 959 der_cert.type = siDERCertBuffer; |
| 937 | 960 |
| 938 // Parse into a certificate structure. | 961 // Parse into a certificate structure. |
| 939 return CERT_NewTempCertificate(CERT_GetDefaultCertDB(), &der_cert, NULL, | 962 X509Certificate::OSCertHandle result = |
| 940 PR_FALSE, PR_TRUE); | 963 CERT_NewTempCertificate(CERT_GetDefaultCertDB(), &der_cert, |
| 964 const_cast<char*>(nickname), | |
| 965 PR_FALSE, PR_TRUE); | |
| 966 | |
| 967 return result; | |
| 941 } | 968 } |
| 942 | 969 |
| 943 // static | 970 // static |
| 944 X509Certificate::OSCertHandles X509Certificate::CreateOSCertHandlesFromBytes( | 971 X509Certificate::OSCertHandles X509Certificate::CreateOSCertHandlesFromBytes( |
| 945 const char* data, int length, Format format) { | 972 const char* data, int length, Format format) { |
| 946 OSCertHandles results; | 973 OSCertHandles results; |
| 947 if (length < 0) | 974 if (length < 0) |
| 948 return results; | 975 return results; |
| 949 | 976 |
| 950 crypto::EnsureNSSInit(); | 977 crypto::EnsureNSSInit(); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1039 | 1066 |
| 1040 // static | 1067 // static |
| 1041 bool X509Certificate::WriteOSCertHandleToPickle(OSCertHandle cert_handle, | 1068 bool X509Certificate::WriteOSCertHandleToPickle(OSCertHandle cert_handle, |
| 1042 Pickle* pickle) { | 1069 Pickle* pickle) { |
| 1043 return pickle->WriteData( | 1070 return pickle->WriteData( |
| 1044 reinterpret_cast<const char*>(cert_handle->derCert.data), | 1071 reinterpret_cast<const char*>(cert_handle->derCert.data), |
| 1045 cert_handle->derCert.len); | 1072 cert_handle->derCert.len); |
| 1046 } | 1073 } |
| 1047 | 1074 |
| 1048 } // namespace net | 1075 } // namespace net |
| OLD | NEW |