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/base/x509_certificate.h" | 5 #include "net/base/x509_certificate.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <map> | 10 #include <map> |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 Entry() : cert_handle(NULL), ref_count(0) {} | 84 Entry() : cert_handle(NULL), ref_count(0) {} |
85 | 85 |
86 X509Certificate::OSCertHandle cert_handle; | 86 X509Certificate::OSCertHandle cert_handle; |
87 | 87 |
88 // Increased by each call to InsertOrUpdate(), and balanced by each call | 88 // Increased by each call to InsertOrUpdate(), and balanced by each call |
89 // to Remove(). When it equals 0, all references created by | 89 // to Remove(). When it equals 0, all references created by |
90 // InsertOrUpdate() have been released, so the cache entry will be removed | 90 // InsertOrUpdate() have been released, so the cache entry will be removed |
91 // the cached OS certificate handle will be freed. | 91 // the cached OS certificate handle will be freed. |
92 int ref_count; | 92 int ref_count; |
93 }; | 93 }; |
94 typedef std::map<SHA1Fingerprint, Entry, SHA1FingerprintLessThan> CertMap; | 94 typedef std::map<SHA1HashValue, Entry, SHA1HashValueLessThan> CertMap; |
95 | 95 |
96 // Obtain an instance of X509CertificateCache via a LazyInstance. | 96 // Obtain an instance of X509CertificateCache via a LazyInstance. |
97 X509CertificateCache() {} | 97 X509CertificateCache() {} |
98 ~X509CertificateCache() {} | 98 ~X509CertificateCache() {} |
99 friend struct base::DefaultLazyInstanceTraits<X509CertificateCache>; | 99 friend struct base::DefaultLazyInstanceTraits<X509CertificateCache>; |
100 | 100 |
101 // You must acquire this lock before using any private data of this object | 101 // You must acquire this lock before using any private data of this object |
102 // You must not block while holding this lock. | 102 // You must not block while holding this lock. |
103 base::Lock lock_; | 103 base::Lock lock_; |
104 | 104 |
105 // The certificate cache. You must acquire |lock_| before using |cache_|. | 105 // The certificate cache. You must acquire |lock_| before using |cache_|. |
106 CertMap cache_; | 106 CertMap cache_; |
107 | 107 |
108 DISALLOW_COPY_AND_ASSIGN(X509CertificateCache); | 108 DISALLOW_COPY_AND_ASSIGN(X509CertificateCache); |
109 }; | 109 }; |
110 | 110 |
111 base::LazyInstance<X509CertificateCache>::Leaky | 111 base::LazyInstance<X509CertificateCache>::Leaky |
112 g_x509_certificate_cache = LAZY_INSTANCE_INITIALIZER; | 112 g_x509_certificate_cache = LAZY_INSTANCE_INITIALIZER; |
113 | 113 |
114 void X509CertificateCache::InsertOrUpdate( | 114 void X509CertificateCache::InsertOrUpdate( |
115 X509Certificate::OSCertHandle* cert_handle) { | 115 X509Certificate::OSCertHandle* cert_handle) { |
116 DCHECK(cert_handle); | 116 DCHECK(cert_handle); |
117 SHA1Fingerprint fingerprint = | 117 SHA1HashValue fingerprint = |
118 X509Certificate::CalculateFingerprint(*cert_handle); | 118 X509Certificate::CalculateFingerprint(*cert_handle); |
119 | 119 |
120 X509Certificate::OSCertHandle old_handle = NULL; | 120 X509Certificate::OSCertHandle old_handle = NULL; |
121 { | 121 { |
122 base::AutoLock lock(lock_); | 122 base::AutoLock lock(lock_); |
123 CertMap::iterator pos = cache_.find(fingerprint); | 123 CertMap::iterator pos = cache_.find(fingerprint); |
124 if (pos == cache_.end()) { | 124 if (pos == cache_.end()) { |
125 // A cached entry was not found, so initialize a new entry. The entry | 125 // A cached entry was not found, so initialize a new entry. The entry |
126 // assumes ownership of the current |*cert_handle|. | 126 // assumes ownership of the current |*cert_handle|. |
127 Entry cache_entry; | 127 Entry cache_entry; |
(...skipping 25 matching lines...) Expand all Loading... |
153 // |old_handle| may be the only handle for this particular certificate, so | 153 // |old_handle| may be the only handle for this particular certificate, so |
154 // freeing it may be complex or resource-intensive and does not need to | 154 // freeing it may be complex or resource-intensive and does not need to |
155 // be guarded by the lock. | 155 // be guarded by the lock. |
156 if (old_handle) { | 156 if (old_handle) { |
157 X509Certificate::FreeOSCertHandle(old_handle); | 157 X509Certificate::FreeOSCertHandle(old_handle); |
158 DHISTOGRAM_COUNTS("X509CertificateReuseCount", 1); | 158 DHISTOGRAM_COUNTS("X509CertificateReuseCount", 1); |
159 } | 159 } |
160 } | 160 } |
161 | 161 |
162 void X509CertificateCache::Remove(X509Certificate::OSCertHandle cert_handle) { | 162 void X509CertificateCache::Remove(X509Certificate::OSCertHandle cert_handle) { |
163 SHA1Fingerprint fingerprint = | 163 SHA1HashValue fingerprint = |
164 X509Certificate::CalculateFingerprint(cert_handle); | 164 X509Certificate::CalculateFingerprint(cert_handle); |
165 base::AutoLock lock(lock_); | 165 base::AutoLock lock(lock_); |
166 | 166 |
167 CertMap::iterator pos = cache_.find(fingerprint); | 167 CertMap::iterator pos = cache_.find(fingerprint); |
168 if (pos == cache_.end()) | 168 if (pos == cache_.end()) |
169 return; // A hash collision where the winning cert was already freed. | 169 return; // A hash collision where the winning cert was already freed. |
170 | 170 |
171 bool is_same_cert = X509Certificate::IsSameOSCert(cert_handle, | 171 bool is_same_cert = X509Certificate::IsSameOSCert(cert_handle, |
172 pos->second.cert_handle); | 172 pos->second.cert_handle); |
173 if (!is_same_cert) | 173 if (!is_same_cert) |
(...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
692 RemoveFromCache(cert_handle_); | 692 RemoveFromCache(cert_handle_); |
693 FreeOSCertHandle(cert_handle_); | 693 FreeOSCertHandle(cert_handle_); |
694 } | 694 } |
695 for (size_t i = 0; i < intermediate_ca_certs_.size(); ++i) { | 695 for (size_t i = 0; i < intermediate_ca_certs_.size(); ++i) { |
696 RemoveFromCache(intermediate_ca_certs_[i]); | 696 RemoveFromCache(intermediate_ca_certs_[i]); |
697 FreeOSCertHandle(intermediate_ca_certs_[i]); | 697 FreeOSCertHandle(intermediate_ca_certs_[i]); |
698 } | 698 } |
699 } | 699 } |
700 | 700 |
701 } // namespace net | 701 } // namespace net |
OLD | NEW |