OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/socket/ssl_session_cache_openssl.h" | 5 #include "net/socket/ssl_session_cache_openssl.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include <openssl/rand.h> | 10 #include <openssl/rand.h> |
11 #include <openssl/ssl.h> | 11 #include <openssl/ssl.h> |
12 | 12 |
13 #include "base/callback.h" | |
13 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
14 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
15 #include "base/logging.h" | 16 #include "base/logging.h" |
16 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
17 | 18 |
18 namespace net { | 19 namespace net { |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 // A helper class to lazily create a new EX_DATA index to map SSL_CTX handles | 23 // A helper class to lazily create a new EX_DATA index to map SSL_CTX handles |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
229 return false; // Session has not yet been marked good. Treat as a miss. | 230 return false; // Session has not yet been marked good. Treat as a miss. |
230 | 231 |
231 // Move to front of MRU list. | 232 // Move to front of MRU list. |
232 ordering_.push_front(session); | 233 ordering_.push_front(session); |
233 ordering_.erase(it->second); | 234 ordering_.erase(it->second); |
234 it->second = ordering_.begin(); | 235 it->second = ordering_.begin(); |
235 | 236 |
236 return SSL_set_session(ssl, session) == 1; | 237 return SSL_set_session(ssl, session) == 1; |
237 } | 238 } |
238 | 239 |
240 bool SSLSessionIsInCache(const std::string& cache_key) const { | |
241 base::AutoLock locked(lock_); | |
242 KeyIndex::const_iterator it = key_index_.find(cache_key); | |
243 if (it == key_index_.end()) | |
244 return false; | |
245 | |
246 SSL_SESSION* session = *it->second; | |
247 DCHECK(session); | |
248 | |
249 void* session_is_good = | |
250 SSL_SESSION_get_ex_data(session, GetSSLSessionExIndex()); | |
251 | |
252 return session_is_good; | |
253 } | |
254 | |
255 void SetSessionAddedCallback(SSL* ssl, const base::Closure& callback) { | |
256 // Add this SSL* to the SSLtoCallbackMap. | |
257 // ssl_to_callback_map_[ssl] = CallbackAndCompletionCount(callback, 0); | |
wtc
2014/07/23 02:03:27
Delete this line (which is commented out).
I susp
mshelley
2014/07/23 03:49:57
Done.
| |
258 ssl_to_callback_map_.insert(SSLToCallbackMap::value_type( | |
259 ssl, CallbackAndCompletionCount(callback, 0))); | |
260 } | |
261 | |
262 // Determines if the session for |ssl| is in the cache, and calls the | |
263 // appropriate callback if that is the case. | |
264 void CheckIfSessionFinished(SSL* ssl) { | |
265 SSLToCallbackMap::iterator it = ssl_to_callback_map_.find(ssl); | |
266 if (it == ssl_to_callback_map_.end()) | |
267 return; | |
268 // Increment the session's completion count. | |
269 if (++it->second.count == 2) { | |
270 // The session has been MarkedAsGood and Added, so it can be used. | |
271 // These two events can occur in either order. | |
272 base::Closure callback = it->second.callback; | |
273 ssl_to_callback_map_.erase(it); | |
274 callback.Run(); | |
275 } | |
276 } | |
277 | |
278 void RemoveSessionAddedCallback(SSL* ssl) { ssl_to_callback_map_.erase(ssl); } | |
279 | |
239 void MarkSSLSessionAsGood(SSL* ssl) { | 280 void MarkSSLSessionAsGood(SSL* ssl) { |
240 SSL_SESSION* session = SSL_get_session(ssl); | 281 SSL_SESSION* session = SSL_get_session(ssl); |
241 if (!session) | 282 |
283 if (!session) { | |
284 SSLToCallbackMap::iterator it = ssl_to_callback_map_.find(ssl); | |
285 base::Closure callback = it->second.callback; | |
286 ssl_to_callback_map_.erase(ssl); | |
287 callback.Run(); | |
242 return; | 288 return; |
289 } | |
wtc
2014/07/23 02:03:27
I still suggest simply replacing this (lines 283-2
mshelley
2014/07/23 03:49:57
Done.
| |
243 | 290 |
244 // Mark the session as good, allowing it to be used for future connections. | 291 // Mark the session as good, allowing it to be used for future connections. |
245 SSL_SESSION_set_ex_data( | 292 SSL_SESSION_set_ex_data( |
246 session, GetSSLSessionExIndex(), reinterpret_cast<void*>(1)); | 293 session, GetSSLSessionExIndex(), reinterpret_cast<void*>(1)); |
294 | |
295 CheckIfSessionFinished(ssl); | |
247 } | 296 } |
248 | 297 |
249 // Flush all entries from the cache. | 298 // Flush all entries from the cache. |
250 void Flush() { | 299 void Flush() { |
251 base::AutoLock lock(lock_); | 300 base::AutoLock lock(lock_); |
252 id_index_.clear(); | 301 id_index_.clear(); |
253 key_index_.clear(); | 302 key_index_.clear(); |
254 while (!ordering_.empty()) { | 303 while (!ordering_.empty()) { |
255 SSL_SESSION* session = ordering_.front(); | 304 SSL_SESSION* session = ordering_.front(); |
256 ordering_.pop_front(); | 305 ordering_.pop_front(); |
257 SSL_SESSION_free(session); | 306 SSL_SESSION_free(session); |
258 } | 307 } |
259 } | 308 } |
260 | 309 |
261 private: | 310 private: |
311 // CallbackAndCompletionCounts are used to group a callback that should be | |
312 // run when a certian sesssion is added to the session cache with an integer | |
313 // indicating the status of that session. | |
314 struct CallbackAndCompletionCount { | |
315 CallbackAndCompletionCount(const base::Closure& completion_callback, | |
316 int completion_count) | |
317 : callback(completion_callback), count(completion_count) {} | |
318 | |
319 const base::Closure callback; | |
320 // |count| < 2 means that the ssl session associated with this object | |
321 // has not been added to the session cache or has not been marked as good. | |
322 // |count| is incremented when a session is added to the cache or marked as | |
323 // good, thus |count| == 2 means that the session is ready for use. | |
324 int count; | |
325 }; | |
326 | |
262 // Type for list of SSL_SESSION handles, ordered in MRU order. | 327 // Type for list of SSL_SESSION handles, ordered in MRU order. |
263 typedef std::list<SSL_SESSION*> MRUSessionList; | 328 typedef std::list<SSL_SESSION*> MRUSessionList; |
264 // Type for a dictionary from unique cache keys to session list nodes. | 329 // Type for a dictionary from unique cache keys to session list nodes. |
265 typedef base::hash_map<std::string, MRUSessionList::iterator> KeyIndex; | 330 typedef base::hash_map<std::string, MRUSessionList::iterator> KeyIndex; |
266 // Type for a dictionary from SessionId values to key index nodes. | 331 // Type for a dictionary from SessionId values to key index nodes. |
267 typedef base::hash_map<SessionId, KeyIndex::iterator> SessionIdIndex; | 332 typedef base::hash_map<SessionId, KeyIndex::iterator> SessionIdIndex; |
333 // Type for a map from SSL* to associated callbacks | |
334 typedef std::map<SSL*, CallbackAndCompletionCount> SSLToCallbackMap; | |
268 | 335 |
269 // Return the key associated with a given session, or the empty string if | 336 // Return the key associated with a given session, or the empty string if |
270 // none exist. This shall only be used for debugging. | 337 // none exist. This shall only be used for debugging. |
271 std::string SessionKey(SSL_SESSION* session) { | 338 std::string SessionKey(SSL_SESSION* session) { |
272 if (!session) | 339 if (!session) |
273 return std::string("<null-session>"); | 340 return std::string("<null-session>"); |
274 | 341 |
275 if (session->session_id_length == 0) | 342 if (session->session_id_length == 0) |
276 return std::string("<empty-session-id>"); | 343 return std::string("<empty-session-id>"); |
277 | 344 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
335 DCHECK(result); | 402 DCHECK(result); |
336 return reinterpret_cast<SSLSessionCacheOpenSSLImpl*>(result); | 403 return reinterpret_cast<SSLSessionCacheOpenSSLImpl*>(result); |
337 } | 404 } |
338 | 405 |
339 // Called by OpenSSL when a new |session| was created and added to a given | 406 // Called by OpenSSL when a new |session| was created and added to a given |
340 // |ssl| connection. Note that the session's reference count was already | 407 // |ssl| connection. Note that the session's reference count was already |
341 // incremented before the function is entered. The function must return 1 | 408 // incremented before the function is entered. The function must return 1 |
342 // to indicate that it took ownership of the session, i.e. that the caller | 409 // to indicate that it took ownership of the session, i.e. that the caller |
343 // should not decrement its reference count after completion. | 410 // should not decrement its reference count after completion. |
344 static int NewSessionCallbackStatic(SSL* ssl, SSL_SESSION* session) { | 411 static int NewSessionCallbackStatic(SSL* ssl, SSL_SESSION* session) { |
345 GetCache(ssl->ctx)->OnSessionAdded(ssl, session); | 412 SSLSessionCacheOpenSSLImpl* cache = GetCache(ssl->ctx); |
413 cache->OnSessionAdded(ssl, session); | |
414 cache->CheckIfSessionFinished(ssl); | |
346 return 1; | 415 return 1; |
347 } | 416 } |
348 | 417 |
349 // Called by OpenSSL to indicate that a session must be removed from the | 418 // Called by OpenSSL to indicate that a session must be removed from the |
350 // cache. This happens when SSL_CTX is destroyed. | 419 // cache. This happens when SSL_CTX is destroyed. |
351 static void RemoveSessionCallbackStatic(SSL_CTX* ctx, SSL_SESSION* session) { | 420 static void RemoveSessionCallbackStatic(SSL_CTX* ctx, SSL_SESSION* session) { |
352 GetCache(ctx)->OnSessionRemoved(session); | 421 GetCache(ctx)->OnSessionRemoved(session); |
353 } | 422 } |
354 | 423 |
355 // Called by OpenSSL to generate a new session ID. This happens during a | 424 // Called by OpenSSL to generate a new session ID. This happens during a |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
459 if (id_index_.find(SessionId(id, id_len)) == id_index_.end()) | 528 if (id_index_.find(SessionId(id, id_len)) == id_index_.end()) |
460 return true; | 529 return true; |
461 } | 530 } |
462 DLOG(ERROR) << "Couldn't generate unique session ID of " << id_len | 531 DLOG(ERROR) << "Couldn't generate unique session ID of " << id_len |
463 << "bytes after " << kMaxTries << " tries."; | 532 << "bytes after " << kMaxTries << " tries."; |
464 return false; | 533 return false; |
465 } | 534 } |
466 | 535 |
467 SSL_CTX* ctx_; | 536 SSL_CTX* ctx_; |
468 SSLSessionCacheOpenSSL::Config config_; | 537 SSLSessionCacheOpenSSL::Config config_; |
538 SSLToCallbackMap ssl_to_callback_map_; | |
469 | 539 |
470 // method to get the index which can later be used with SSL_CTX_get_ex_data() | 540 // method to get the index which can later be used with SSL_CTX_get_ex_data() |
471 // or SSL_CTX_set_ex_data(). | 541 // or SSL_CTX_set_ex_data(). |
472 base::Lock lock_; // Protects access to containers below. | 542 mutable base::Lock lock_; // Protects access to containers below. |
473 | 543 |
474 MRUSessionList ordering_; | 544 MRUSessionList ordering_; |
475 KeyIndex key_index_; | 545 KeyIndex key_index_; |
476 SessionIdIndex id_index_; | 546 SessionIdIndex id_index_; |
477 | 547 |
478 size_t expiration_check_; | 548 size_t expiration_check_; |
479 }; | 549 }; |
480 | 550 |
481 SSLSessionCacheOpenSSL::~SSLSessionCacheOpenSSL() { delete impl_; } | 551 SSLSessionCacheOpenSSL::~SSLSessionCacheOpenSSL() { delete impl_; } |
482 | 552 |
483 size_t SSLSessionCacheOpenSSL::size() const { return impl_->size(); } | 553 size_t SSLSessionCacheOpenSSL::size() const { return impl_->size(); } |
484 | 554 |
485 void SSLSessionCacheOpenSSL::Reset(SSL_CTX* ctx, const Config& config) { | 555 void SSLSessionCacheOpenSSL::Reset(SSL_CTX* ctx, const Config& config) { |
486 if (impl_) | 556 if (impl_) |
487 delete impl_; | 557 delete impl_; |
488 | 558 |
489 impl_ = new SSLSessionCacheOpenSSLImpl(ctx, config); | 559 impl_ = new SSLSessionCacheOpenSSLImpl(ctx, config); |
490 } | 560 } |
491 | 561 |
492 bool SSLSessionCacheOpenSSL::SetSSLSession(SSL* ssl) { | 562 bool SSLSessionCacheOpenSSL::SetSSLSession(SSL* ssl) { |
493 return impl_->SetSSLSession(ssl); | 563 return impl_->SetSSLSession(ssl); |
494 } | 564 } |
495 | 565 |
496 bool SSLSessionCacheOpenSSL::SetSSLSessionWithKey( | 566 bool SSLSessionCacheOpenSSL::SetSSLSessionWithKey( |
497 SSL* ssl, | 567 SSL* ssl, |
498 const std::string& cache_key) { | 568 const std::string& cache_key) { |
499 return impl_->SetSSLSessionWithKey(ssl, cache_key); | 569 return impl_->SetSSLSessionWithKey(ssl, cache_key); |
500 } | 570 } |
501 | 571 |
572 bool SSLSessionCacheOpenSSL::SSLSessionIsInCache( | |
573 const std::string& cache_key) const { | |
574 return impl_->SSLSessionIsInCache(cache_key); | |
575 } | |
576 | |
577 void SSLSessionCacheOpenSSL::RemoveSessionAddedCallback(SSL* ssl) { | |
578 impl_->RemoveSessionAddedCallback(ssl); | |
579 } | |
580 | |
581 void SSLSessionCacheOpenSSL::SetSessionAddedCallback(SSL* ssl, | |
582 const base::Closure& cb) { | |
583 impl_->SetSessionAddedCallback(ssl, cb); | |
584 } | |
585 | |
502 void SSLSessionCacheOpenSSL::MarkSSLSessionAsGood(SSL* ssl) { | 586 void SSLSessionCacheOpenSSL::MarkSSLSessionAsGood(SSL* ssl) { |
503 return impl_->MarkSSLSessionAsGood(ssl); | 587 return impl_->MarkSSLSessionAsGood(ssl); |
504 } | 588 } |
505 | 589 |
506 void SSLSessionCacheOpenSSL::Flush() { impl_->Flush(); } | 590 void SSLSessionCacheOpenSSL::Flush() { impl_->Flush(); } |
507 | 591 |
508 } // namespace net | 592 } // namespace net |
OLD | NEW |