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 <sechash.h> // Implement CalculateChainFingerprint() with NSS. | |
8 | |
7 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "base/pickle.h" | 11 #include "base/pickle.h" |
10 #include "base/sha1.h" | 12 #include "base/sha1.h" |
11 #include "base/string_tokenizer.h" | 13 #include "base/string_tokenizer.h" |
12 #include "base/string_util.h" | 14 #include "base/string_util.h" |
13 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
14 #include "crypto/rsa_private_key.h" | 16 #include "crypto/rsa_private_key.h" |
15 #include "crypto/scoped_capi_types.h" | 17 #include "crypto/scoped_capi_types.h" |
16 #include "net/base/asn1_util.h" | 18 #include "net/base/asn1_util.h" |
(...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
534 &cert_handle_->pCertInfo->Issuer, | 536 &cert_handle_->pCertInfo->Issuer, |
535 CERT_X500_NAME_STR | CERT_NAME_STR_CRLF_FLAG, | 537 CERT_X500_NAME_STR | CERT_NAME_STR_CRLF_FLAG, |
536 WriteInto(&issuer_info, name_size), name_size); | 538 WriteInto(&issuer_info, name_size), name_size); |
537 ParsePrincipal(WideToUTF8(subject_info), &subject_); | 539 ParsePrincipal(WideToUTF8(subject_info), &subject_); |
538 ParsePrincipal(WideToUTF8(issuer_info), &issuer_); | 540 ParsePrincipal(WideToUTF8(issuer_info), &issuer_); |
539 | 541 |
540 valid_start_ = Time::FromFileTime(cert_handle_->pCertInfo->NotBefore); | 542 valid_start_ = Time::FromFileTime(cert_handle_->pCertInfo->NotBefore); |
541 valid_expiry_ = Time::FromFileTime(cert_handle_->pCertInfo->NotAfter); | 543 valid_expiry_ = Time::FromFileTime(cert_handle_->pCertInfo->NotAfter); |
542 | 544 |
543 fingerprint_ = CalculateFingerprint(cert_handle_); | 545 fingerprint_ = CalculateFingerprint(cert_handle_); |
546 chain_fingerprint_ = CalculateChainFingerprint(); | |
544 | 547 |
545 const CRYPT_INTEGER_BLOB* serial = &cert_handle_->pCertInfo->SerialNumber; | 548 const CRYPT_INTEGER_BLOB* serial = &cert_handle_->pCertInfo->SerialNumber; |
546 scoped_array<uint8> serial_bytes(new uint8[serial->cbData]); | 549 scoped_array<uint8> serial_bytes(new uint8[serial->cbData]); |
547 for (unsigned i = 0; i < serial->cbData; i++) | 550 for (unsigned i = 0; i < serial->cbData; i++) |
548 serial_bytes[i] = serial->pbData[serial->cbData - i - 1]; | 551 serial_bytes[i] = serial->pbData[serial->cbData - i - 1]; |
549 serial_number_ = std::string( | 552 serial_number_ = std::string( |
550 reinterpret_cast<char*>(serial_bytes.get()), serial->cbData); | 553 reinterpret_cast<char*>(serial_bytes.get()), serial->cbData); |
551 // Remove leading zeros. | 554 // Remove leading zeros. |
552 while (serial_number_.size() > 1 && serial_number_[0] == 0) | 555 while (serial_number_.size() > 1 && serial_number_[0] == 0) |
553 serial_number_ = serial_number_.substr(1, serial_number_.size() - 1); | 556 serial_number_ = serial_number_.substr(1, serial_number_.size() - 1); |
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1011 SHA1Fingerprint sha1; | 1014 SHA1Fingerprint sha1; |
1012 DWORD sha1_size = sizeof(sha1.data); | 1015 DWORD sha1_size = sizeof(sha1.data); |
1013 rv = CryptHashCertificate(NULL, CALG_SHA1, 0, cert->pbCertEncoded, | 1016 rv = CryptHashCertificate(NULL, CALG_SHA1, 0, cert->pbCertEncoded, |
1014 cert->cbCertEncoded, sha1.data, &sha1_size); | 1017 cert->cbCertEncoded, sha1.data, &sha1_size); |
1015 DCHECK(rv && sha1_size == sizeof(sha1.data)); | 1018 DCHECK(rv && sha1_size == sizeof(sha1.data)); |
1016 if (!rv) | 1019 if (!rv) |
1017 memset(sha1.data, 0, sizeof(sha1.data)); | 1020 memset(sha1.data, 0, sizeof(sha1.data)); |
1018 return sha1; | 1021 return sha1; |
1019 } | 1022 } |
1020 | 1023 |
1024 SHA1Fingerprint X509Certificate::CalculateChainFingerprint() const { | |
1025 SHA1Fingerprint sha1; | |
Ryan Sleevi
2011/10/29 02:53:15
Perhaps document here (or on line 7), why NSS was
wtc
2011/10/29 05:08:34
Done.
In Patch Set 5 I also switched to the NSS l
| |
1026 memset(sha1.data, 0, sizeof(sha1.data)); | |
1027 | |
1028 HASHContext* sha1_ctx = HASH_Create(HASH_AlgSHA1); | |
1029 if (!sha1_ctx) | |
1030 return sha1; | |
1031 HASH_Begin(sha1_ctx); | |
1032 HASH_Update(sha1_ctx, cert_handle_->pbCertEncoded, | |
1033 cert_handle_->cbCertEncoded); | |
1034 for (size_t i = 0; i < intermediate_ca_certs_.size(); ++i) { | |
1035 PCCERT_CONTEXT ca_cert = intermediate_ca_certs_[i]; | |
1036 HASH_Update(sha1_ctx, ca_cert->pbCertEncoded, ca_cert->cbCertEncoded); | |
1037 } | |
1038 unsigned int result_len; | |
1039 HASH_End(sha1_ctx, sha1.data, &result_len, HASH_ResultLenContext(sha1_ctx)); | |
1040 HASH_Destroy(sha1_ctx); | |
1041 | |
1042 return sha1; | |
1043 } | |
1044 | |
1021 // static | 1045 // static |
1022 X509Certificate::OSCertHandle | 1046 X509Certificate::OSCertHandle |
1023 X509Certificate::ReadOSCertHandleFromPickle(const Pickle& pickle, | 1047 X509Certificate::ReadOSCertHandleFromPickle(const Pickle& pickle, |
1024 void** pickle_iter) { | 1048 void** pickle_iter) { |
1025 const char* data; | 1049 const char* data; |
1026 int length; | 1050 int length; |
1027 if (!pickle.ReadData(pickle_iter, &data, &length)) | 1051 if (!pickle.ReadData(pickle_iter, &data, &length)) |
1028 return NULL; | 1052 return NULL; |
1029 | 1053 |
1030 OSCertHandle cert_handle = NULL; | 1054 OSCertHandle cert_handle = NULL; |
(...skipping 21 matching lines...) Expand all Loading... | |
1052 if (!CertSerializeCertificateStoreElement(cert_handle, 0, &buffer[0], | 1076 if (!CertSerializeCertificateStoreElement(cert_handle, 0, &buffer[0], |
1053 &length)) { | 1077 &length)) { |
1054 return false; | 1078 return false; |
1055 } | 1079 } |
1056 | 1080 |
1057 return pickle->WriteData(reinterpret_cast<const char*>(&buffer[0]), | 1081 return pickle->WriteData(reinterpret_cast<const char*>(&buffer[0]), |
1058 length); | 1082 length); |
1059 } | 1083 } |
1060 | 1084 |
1061 } // namespace net | 1085 } // namespace net |
OLD | NEW |