| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/cert/x509_certificate.h" | 5 #include "net/cert/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 <prtime.h> | 12 #include <prtime.h> |
| 13 #include <seccomon.h> | 13 #include <seccomon.h> |
| 14 #include <secder.h> | 14 #include <secder.h> |
| 15 #include <sechash.h> | 15 #include <sechash.h> |
| 16 | 16 |
| 17 #include "base/trace_event/trace_event.h" |
| 17 #include "base/logging.h" | 18 #include "base/logging.h" |
| 18 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
| 19 #include "base/numerics/safe_conversions.h" | 20 #include "base/numerics/safe_conversions.h" |
| 20 #include "base/pickle.h" | 21 #include "base/pickle.h" |
| 21 #include "base/strings/stringprintf.h" | 22 #include "base/strings/stringprintf.h" |
| 22 #include "base/time/time.h" | 23 #include "base/time/time.h" |
| 23 #include "crypto/nss_util.h" | 24 #include "crypto/nss_util.h" |
| 24 #include "crypto/scoped_nss_types.h" | 25 #include "crypto/scoped_nss_types.h" |
| 25 #include "net/cert/x509_util_nss.h" | 26 #include "net/cert/x509_util_nss.h" |
| 26 | 27 |
| 27 namespace net { | 28 namespace net { |
| 28 | 29 |
| 29 void X509Certificate::Initialize() { | 30 void X509Certificate::Initialize() { |
| 31 TRACE_EVENT0("toplevel", "X509Certificate::Initialize NSS A"); |
| 30 x509_util::ParsePrincipal(&cert_handle_->subject, &subject_); | 32 x509_util::ParsePrincipal(&cert_handle_->subject, &subject_); |
| 31 x509_util::ParsePrincipal(&cert_handle_->issuer, &issuer_); | 33 x509_util::ParsePrincipal(&cert_handle_->issuer, &issuer_); |
| 32 | 34 TRACE_EVENT0("toplevel", "X509Certificate::Initialize NSS B"); |
| 33 x509_util::ParseDate(&cert_handle_->validity.notBefore, &valid_start_); | 35 x509_util::ParseDate(&cert_handle_->validity.notBefore, &valid_start_); |
| 34 x509_util::ParseDate(&cert_handle_->validity.notAfter, &valid_expiry_); | 36 x509_util::ParseDate(&cert_handle_->validity.notAfter, &valid_expiry_); |
| 35 | 37 TRACE_EVENT0("toplevel", "X509Certificate::Initialize NSS C"); |
| 36 fingerprint_ = CalculateFingerprint(cert_handle_); | 38 fingerprint_ = CalculateFingerprint(cert_handle_); |
| 39 TRACE_EVENT0("toplevel", "X509Certificate::Initialize NSS D"); |
| 37 ca_fingerprint_ = CalculateCAFingerprint(intermediate_ca_certs_); | 40 ca_fingerprint_ = CalculateCAFingerprint(intermediate_ca_certs_); |
| 38 | 41 TRACE_EVENT0("toplevel", "X509Certificate::Initialize NSS E"); |
| 39 serial_number_ = x509_util::ParseSerialNumber(cert_handle_); | 42 serial_number_ = x509_util::ParseSerialNumber(cert_handle_); |
| 40 } | 43 } |
| 41 | 44 |
| 42 // static | 45 // static |
| 43 scoped_refptr<X509Certificate> X509Certificate::CreateFromBytesWithNickname( | 46 scoped_refptr<X509Certificate> X509Certificate::CreateFromBytesWithNickname( |
| 44 const char* data, | 47 const char* data, |
| 45 size_t length, | 48 size_t length, |
| 46 const char* nickname) { | 49 const char* nickname) { |
| 47 OSCertHandle cert_handle = CreateOSCertHandleFromBytesWithNickname(data, | 50 OSCertHandle cert_handle = CreateOSCertHandleFromBytesWithNickname(data, |
| 48 length, | 51 length, |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 SECStatus rv = HASH_HashBuf( | 237 SECStatus rv = HASH_HashBuf( |
| 235 HASH_AlgSHA256, sha256.data, cert->derCert.data, cert->derCert.len); | 238 HASH_AlgSHA256, sha256.data, cert->derCert.data, cert->derCert.len); |
| 236 DCHECK_EQ(SECSuccess, rv); | 239 DCHECK_EQ(SECSuccess, rv); |
| 237 | 240 |
| 238 return sha256; | 241 return sha256; |
| 239 } | 242 } |
| 240 | 243 |
| 241 // static | 244 // static |
| 242 SHA1HashValue X509Certificate::CalculateCAFingerprint( | 245 SHA1HashValue X509Certificate::CalculateCAFingerprint( |
| 243 const OSCertHandles& intermediates) { | 246 const OSCertHandles& intermediates) { |
| 247 TRACE_EVENT0("toplevel", "X509Certificate::CalculateCAFingerprint"); |
| 244 SHA1HashValue sha1; | 248 SHA1HashValue sha1; |
| 245 memset(sha1.data, 0, sizeof(sha1.data)); | 249 memset(sha1.data, 0, sizeof(sha1.data)); |
| 246 | 250 |
| 247 HASHContext* sha1_ctx = HASH_Create(HASH_AlgSHA1); | 251 HASHContext* sha1_ctx = HASH_Create(HASH_AlgSHA1); |
| 248 if (!sha1_ctx) | 252 if (!sha1_ctx) |
| 249 return sha1; | 253 return sha1; |
| 250 HASH_Begin(sha1_ctx); | 254 HASH_Begin(sha1_ctx); |
| 251 for (size_t i = 0; i < intermediates.size(); ++i) { | 255 for (size_t i = 0; i < intermediates.size(); ++i) { |
| 252 CERTCertificate* ca_cert = intermediates[i]; | 256 CERTCertificate* ca_cert = intermediates[i]; |
| 253 HASH_Update(sha1_ctx, ca_cert->derCert.data, ca_cert->derCert.len); | 257 HASH_Update(sha1_ctx, ca_cert->derCert.data, ca_cert->derCert.len); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 283 // static | 287 // static |
| 284 bool X509Certificate::IsSelfSigned(OSCertHandle cert_handle) { | 288 bool X509Certificate::IsSelfSigned(OSCertHandle cert_handle) { |
| 285 crypto::ScopedSECKEYPublicKey public_key(CERT_ExtractPublicKey(cert_handle)); | 289 crypto::ScopedSECKEYPublicKey public_key(CERT_ExtractPublicKey(cert_handle)); |
| 286 if (!public_key.get()) | 290 if (!public_key.get()) |
| 287 return false; | 291 return false; |
| 288 return SECSuccess == CERT_VerifySignedDataWithPublicKey( | 292 return SECSuccess == CERT_VerifySignedDataWithPublicKey( |
| 289 &cert_handle->signatureWrap, public_key.get(), NULL); | 293 &cert_handle->signatureWrap, public_key.get(), NULL); |
| 290 } | 294 } |
| 291 | 295 |
| 292 } // namespace net | 296 } // namespace net |
| OLD | NEW |