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> |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
250 SSL_SESSION_get_ex_data(session, GetSSLSessionExIndex()); | 250 SSL_SESSION_get_ex_data(session, GetSSLSessionExIndex()); |
251 | 251 |
252 return session_is_good; | 252 return session_is_good; |
253 } | 253 } |
254 | 254 |
255 void SetSessionAddedCallback(SSL* ssl, const base::Closure& callback) { | 255 void SetSessionAddedCallback(SSL* ssl, const base::Closure& callback) { |
256 // Add this SSL* to the SSLtoCallbackMap. | 256 // Add this SSL* to the SSLtoCallbackMap. |
257 ssl_to_callback_map_.insert(SSLToCallbackMap::value_type( | 257 ssl_to_callback_map_.insert(SSLToCallbackMap::value_type( |
258 ssl, CallbackAndCompletionCount(callback, 0))); | 258 ssl, CallbackAndCompletionCount(callback, 0))); |
259 } | 259 } |
260 | 260 |
davidben
2014/07/29 19:16:01
The completion count thing is a little non-obvious
mshelley
2014/08/05 23:17:11
Done.
| |
261 // Determines if the session for |ssl| is in the cache, and calls the | 261 void CheckIfSessionFinished(const SSL* ssl) { |
262 // appropriate callback if that is the case. | |
263 void CheckIfSessionFinished(SSL* ssl) { | |
264 SSLToCallbackMap::iterator it = ssl_to_callback_map_.find(ssl); | 262 SSLToCallbackMap::iterator it = ssl_to_callback_map_.find(ssl); |
265 if (it == ssl_to_callback_map_.end()) | 263 if (it == ssl_to_callback_map_.end()) |
266 return; | 264 return; |
267 // Increment the session's completion count. | 265 // Increment the session's completion count. |
268 if (++it->second.count == 2) { | 266 if (++it->second.count == 2) { |
269 // The session has been MarkedAsGood and Added, so it can be used. | 267 // The session has been MarkedAsGood and Added, so it can be used. |
270 // These two events can occur in either order. | 268 // These two events can occur in either order. |
271 base::Closure callback = it->second.callback; | 269 base::Closure callback = it->second.callback; |
272 ssl_to_callback_map_.erase(it); | 270 ssl_to_callback_map_.erase(it); |
273 callback.Run(); | 271 callback.Run(); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
320 int count; | 318 int count; |
321 }; | 319 }; |
322 | 320 |
323 // Type for list of SSL_SESSION handles, ordered in MRU order. | 321 // Type for list of SSL_SESSION handles, ordered in MRU order. |
324 typedef std::list<SSL_SESSION*> MRUSessionList; | 322 typedef std::list<SSL_SESSION*> MRUSessionList; |
325 // Type for a dictionary from unique cache keys to session list nodes. | 323 // Type for a dictionary from unique cache keys to session list nodes. |
326 typedef base::hash_map<std::string, MRUSessionList::iterator> KeyIndex; | 324 typedef base::hash_map<std::string, MRUSessionList::iterator> KeyIndex; |
327 // Type for a dictionary from SessionId values to key index nodes. | 325 // Type for a dictionary from SessionId values to key index nodes. |
328 typedef base::hash_map<SessionId, KeyIndex::iterator> SessionIdIndex; | 326 typedef base::hash_map<SessionId, KeyIndex::iterator> SessionIdIndex; |
329 // Type for a map from SSL* to associated callbacks | 327 // Type for a map from SSL* to associated callbacks |
330 typedef std::map<SSL*, CallbackAndCompletionCount> SSLToCallbackMap; | 328 typedef std::map<const SSL*, CallbackAndCompletionCount> SSLToCallbackMap; |
331 | 329 |
332 // Return the key associated with a given session, or the empty string if | 330 // Return the key associated with a given session, or the empty string if |
333 // none exist. This shall only be used for debugging. | 331 // none exist. This shall only be used for debugging. |
334 std::string SessionKey(SSL_SESSION* session) { | 332 std::string SessionKey(SSL_SESSION* session) { |
335 if (!session) | 333 if (!session) |
336 return std::string("<null-session>"); | 334 return std::string("<null-session>"); |
337 | 335 |
338 if (session->session_id_length == 0) | 336 if (session->session_id_length == 0) |
339 return std::string("<empty-session-id>"); | 337 return std::string("<empty-session-id>"); |
340 | 338 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
400 } | 398 } |
401 | 399 |
402 // Called by OpenSSL when a new |session| was created and added to a given | 400 // Called by OpenSSL when a new |session| was created and added to a given |
403 // |ssl| connection. Note that the session's reference count was already | 401 // |ssl| connection. Note that the session's reference count was already |
404 // incremented before the function is entered. The function must return 1 | 402 // incremented before the function is entered. The function must return 1 |
405 // to indicate that it took ownership of the session, i.e. that the caller | 403 // to indicate that it took ownership of the session, i.e. that the caller |
406 // should not decrement its reference count after completion. | 404 // should not decrement its reference count after completion. |
407 static int NewSessionCallbackStatic(SSL* ssl, SSL_SESSION* session) { | 405 static int NewSessionCallbackStatic(SSL* ssl, SSL_SESSION* session) { |
408 SSLSessionCacheOpenSSLImpl* cache = GetCache(ssl->ctx); | 406 SSLSessionCacheOpenSSLImpl* cache = GetCache(ssl->ctx); |
409 cache->OnSessionAdded(ssl, session); | 407 cache->OnSessionAdded(ssl, session); |
410 cache->CheckIfSessionFinished(ssl); | |
411 return 1; | 408 return 1; |
412 } | 409 } |
413 | 410 |
414 // Called by OpenSSL to indicate that a session must be removed from the | 411 // Called by OpenSSL to indicate that a session must be removed from the |
415 // cache. This happens when SSL_CTX is destroyed. | 412 // cache. This happens when SSL_CTX is destroyed. |
416 static void RemoveSessionCallbackStatic(SSL_CTX* ctx, SSL_SESSION* session) { | 413 static void RemoveSessionCallbackStatic(SSL_CTX* ctx, SSL_SESSION* session) { |
417 GetCache(ctx)->OnSessionRemoved(session); | 414 GetCache(ctx)->OnSessionRemoved(session); |
418 } | 415 } |
419 | 416 |
420 // Called by OpenSSL to generate a new session ID. This happens during a | 417 // 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... | |
524 if (id_index_.find(SessionId(id, id_len)) == id_index_.end()) | 521 if (id_index_.find(SessionId(id, id_len)) == id_index_.end()) |
525 return true; | 522 return true; |
526 } | 523 } |
527 DLOG(ERROR) << "Couldn't generate unique session ID of " << id_len | 524 DLOG(ERROR) << "Couldn't generate unique session ID of " << id_len |
528 << "bytes after " << kMaxTries << " tries."; | 525 << "bytes after " << kMaxTries << " tries."; |
529 return false; | 526 return false; |
530 } | 527 } |
531 | 528 |
532 SSL_CTX* ctx_; | 529 SSL_CTX* ctx_; |
533 SSLSessionCacheOpenSSL::Config config_; | 530 SSLSessionCacheOpenSSL::Config config_; |
534 SSLToCallbackMap ssl_to_callback_map_; | 531 SSLToCallbackMap ssl_to_callback_map_; |
davidben
2014/07/29 19:16:01
Now that the completion hook comes from SSLClientS
mshelley
2014/08/05 23:17:11
Done.
| |
535 | 532 |
536 // method to get the index which can later be used with SSL_CTX_get_ex_data() | 533 // method to get the index which can later be used with SSL_CTX_get_ex_data() |
537 // or SSL_CTX_set_ex_data(). | 534 // or SSL_CTX_set_ex_data(). |
538 mutable base::Lock lock_; // Protects access to containers below. | 535 mutable base::Lock lock_; // Protects access to containers below. |
539 | 536 |
540 MRUSessionList ordering_; | 537 MRUSessionList ordering_; |
541 KeyIndex key_index_; | 538 KeyIndex key_index_; |
542 SessionIdIndex id_index_; | 539 SessionIdIndex id_index_; |
543 | 540 |
544 size_t expiration_check_; | 541 size_t expiration_check_; |
(...skipping 18 matching lines...) Expand all Loading... | |
563 SSL* ssl, | 560 SSL* ssl, |
564 const std::string& cache_key) { | 561 const std::string& cache_key) { |
565 return impl_->SetSSLSessionWithKey(ssl, cache_key); | 562 return impl_->SetSSLSessionWithKey(ssl, cache_key); |
566 } | 563 } |
567 | 564 |
568 bool SSLSessionCacheOpenSSL::SSLSessionIsInCache( | 565 bool SSLSessionCacheOpenSSL::SSLSessionIsInCache( |
569 const std::string& cache_key) const { | 566 const std::string& cache_key) const { |
570 return impl_->SSLSessionIsInCache(cache_key); | 567 return impl_->SSLSessionIsInCache(cache_key); |
571 } | 568 } |
572 | 569 |
570 void SSLSessionCacheOpenSSL::CheckIfSessionFinished(const SSL* ssl) { | |
571 impl_->CheckIfSessionFinished(ssl); | |
572 } | |
573 | |
573 void SSLSessionCacheOpenSSL::RemoveSessionAddedCallback(SSL* ssl) { | 574 void SSLSessionCacheOpenSSL::RemoveSessionAddedCallback(SSL* ssl) { |
574 impl_->RemoveSessionAddedCallback(ssl); | 575 impl_->RemoveSessionAddedCallback(ssl); |
575 } | 576 } |
576 | 577 |
577 void SSLSessionCacheOpenSSL::SetSessionAddedCallback(SSL* ssl, | 578 void SSLSessionCacheOpenSSL::SetSessionAddedCallback(SSL* ssl, |
578 const base::Closure& cb) { | 579 const base::Closure& cb) { |
579 impl_->SetSessionAddedCallback(ssl, cb); | 580 impl_->SetSessionAddedCallback(ssl, cb); |
580 } | 581 } |
581 | 582 |
582 void SSLSessionCacheOpenSSL::MarkSSLSessionAsGood(SSL* ssl) { | 583 void SSLSessionCacheOpenSSL::MarkSSLSessionAsGood(SSL* ssl) { |
583 return impl_->MarkSSLSessionAsGood(ssl); | 584 return impl_->MarkSSLSessionAsGood(ssl); |
584 } | 585 } |
585 | 586 |
586 void SSLSessionCacheOpenSSL::Flush() { impl_->Flush(); } | 587 void SSLSessionCacheOpenSSL::Flush() { impl_->Flush(); } |
587 | 588 |
588 } // namespace net | 589 } // namespace net |
OLD | NEW |