Chromium Code Reviews| 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 218 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. | 229 return false; // Session has not yet been marked good. Treat as a miss. |
| 230 | 230 |
| 231 // Move to front of MRU list. | 231 // Move to front of MRU list. |
| 232 ordering_.push_front(session); | 232 ordering_.push_front(session); |
| 233 ordering_.erase(it->second); | 233 ordering_.erase(it->second); |
| 234 it->second = ordering_.begin(); | 234 it->second = ordering_.begin(); |
| 235 | 235 |
| 236 return SSL_set_session(ssl, session) == 1; | 236 return SSL_set_session(ssl, session) == 1; |
| 237 } | 237 } |
| 238 | 238 |
| 239 bool SSLSessionIsInCache(const std::string& cache_key) const { | |
| 240 base::AutoLock locked(lock_); | |
| 241 KeyIndex::const_iterator it = key_index_.find(cache_key); | |
| 242 if (it == key_index_.end()) | |
| 243 return false; | |
| 244 return true; | |
| 245 } | |
| 246 | |
| 247 void NotifyOnSessionAdded(SSL* ssl, const base::Closure& cb) { | |
| 248 // Add this SSL* to the SSLtoCallbackMap. | |
| 249 std::pair<const base::Closure&, int> temp(cb, 0); | |
| 250 ssl_to_callback_map_.insert(SSLtoCallbackMap::value_type(ssl, temp)); | |
|
Ryan Sleevi
2014/06/26 01:47:16
Note: You can just use
ssl_to_callback_map_[ssl]
mshelley
2014/07/01 02:35:23
So I tried that initially, but received "cannot us
| |
| 251 } | |
| 252 | |
| 253 // Determines if the session for |ssl| is in the cache, and calls the | |
| 254 // appropriate callback if that is the case. | |
| 255 void CheckIfSessionAdded(SSL* ssl) { | |
| 256 SSLtoCallbackMap::iterator it = ssl_to_callback_map_.find(ssl); | |
| 257 if (it == ssl_to_callback_map_.end()) | |
| 258 return; // this shouldn't actually happen ever | |
|
Ryan Sleevi
2014/06/26 01:47:16
So then add a NOTREACHED() here.
Why shouldn't it
mshelley
2014/07/01 02:35:23
Oops -- honestly I put that statement/comment in w
| |
| 259 | |
| 260 // Increment the session's completion count. | |
| 261 it->second.second++; | |
| 262 if (it->second.second == 2) { | |
| 263 // The session has been MarkedAsGood and Added, so it can be used. | |
| 264 it->second.first.Run(); | |
| 265 } | |
| 266 } | |
| 267 | |
| 239 void MarkSSLSessionAsGood(SSL* ssl) { | 268 void MarkSSLSessionAsGood(SSL* ssl) { |
| 240 SSL_SESSION* session = SSL_get_session(ssl); | 269 SSL_SESSION* session = SSL_get_session(ssl); |
| 241 if (!session) | 270 if (!session) |
| 242 return; | 271 return; |
| 243 | 272 |
| 244 // Mark the session as good, allowing it to be used for future connections. | 273 // Mark the session as good, allowing it to be used for future connections. |
| 245 SSL_SESSION_set_ex_data( | 274 SSL_SESSION_set_ex_data( |
| 246 session, GetSSLSessionExIndex(), reinterpret_cast<void*>(1)); | 275 session, GetSSLSessionExIndex(), reinterpret_cast<void*>(1)); |
| 276 | |
| 277 CheckIfSessionAdded(ssl); | |
| 247 } | 278 } |
| 248 | 279 |
| 249 // Flush all entries from the cache. | 280 // Flush all entries from the cache. |
| 250 void Flush() { | 281 void Flush() { |
| 251 base::AutoLock lock(lock_); | 282 base::AutoLock lock(lock_); |
| 252 id_index_.clear(); | 283 id_index_.clear(); |
| 253 key_index_.clear(); | 284 key_index_.clear(); |
| 254 while (!ordering_.empty()) { | 285 while (!ordering_.empty()) { |
| 255 SSL_SESSION* session = ordering_.front(); | 286 SSL_SESSION* session = ordering_.front(); |
| 256 ordering_.pop_front(); | 287 ordering_.pop_front(); |
| 257 SSL_SESSION_free(session); | 288 SSL_SESSION_free(session); |
| 258 } | 289 } |
| 259 } | 290 } |
| 260 | 291 |
| 261 private: | 292 private: |
| 262 // Type for list of SSL_SESSION handles, ordered in MRU order. | 293 // Type for list of SSL_SESSION handles, ordered in MRU order. |
| 263 typedef std::list<SSL_SESSION*> MRUSessionList; | 294 typedef std::list<SSL_SESSION*> MRUSessionList; |
| 264 // Type for a dictionary from unique cache keys to session list nodes. | 295 // Type for a dictionary from unique cache keys to session list nodes. |
| 265 typedef base::hash_map<std::string, MRUSessionList::iterator> KeyIndex; | 296 typedef base::hash_map<std::string, MRUSessionList::iterator> KeyIndex; |
| 266 // Type for a dictionary from SessionId values to key index nodes. | 297 // Type for a dictionary from SessionId values to key index nodes. |
| 267 typedef base::hash_map<SessionId, KeyIndex::iterator> SessionIdIndex; | 298 typedef base::hash_map<SessionId, KeyIndex::iterator> SessionIdIndex; |
| 299 // Type for a map from SSL* to associated callbacks | |
| 300 typedef std::map<SSL*, std::pair<const base::Closure&, int> > | |
|
wtc
2014/06/27 00:36:50
What is the "int" in the std::pair?
mshelley
2014/07/01 02:35:23
The int is used to determine whether or not the se
| |
| 301 SSLtoCallbackMap; | |
|
wtc
2014/06/27 00:36:50
Nit: the "to" in "SSLtoCallbackMap" should be capi
mshelley
2014/07/01 02:35:23
Done.
| |
| 268 | 302 |
| 269 // Return the key associated with a given session, or the empty string if | 303 // Return the key associated with a given session, or the empty string if |
| 270 // none exist. This shall only be used for debugging. | 304 // none exist. This shall only be used for debugging. |
| 271 std::string SessionKey(SSL_SESSION* session) { | 305 std::string SessionKey(SSL_SESSION* session) { |
| 272 if (!session) | 306 if (!session) |
| 273 return std::string("<null-session>"); | 307 return std::string("<null-session>"); |
| 274 | 308 |
| 275 if (session->session_id_length == 0) | 309 if (session->session_id_length == 0) |
| 276 return std::string("<empty-session-id>"); | 310 return std::string("<empty-session-id>"); |
| 277 | 311 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 336 return reinterpret_cast<SSLSessionCacheOpenSSLImpl*>(result); | 370 return reinterpret_cast<SSLSessionCacheOpenSSLImpl*>(result); |
| 337 } | 371 } |
| 338 | 372 |
| 339 // Called by OpenSSL when a new |session| was created and added to a given | 373 // 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 | 374 // |ssl| connection. Note that the session's reference count was already |
| 341 // incremented before the function is entered. The function must return 1 | 375 // 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 | 376 // to indicate that it took ownership of the session, i.e. that the caller |
| 343 // should not decrement its reference count after completion. | 377 // should not decrement its reference count after completion. |
| 344 static int NewSessionCallbackStatic(SSL* ssl, SSL_SESSION* session) { | 378 static int NewSessionCallbackStatic(SSL* ssl, SSL_SESSION* session) { |
| 345 GetCache(ssl->ctx)->OnSessionAdded(ssl, session); | 379 GetCache(ssl->ctx)->OnSessionAdded(ssl, session); |
| 380 GetCache(ssl->ctx)->CheckIfSessionAdded(ssl); | |
| 346 return 1; | 381 return 1; |
| 347 } | 382 } |
| 348 | 383 |
| 349 // Called by OpenSSL to indicate that a session must be removed from the | 384 // Called by OpenSSL to indicate that a session must be removed from the |
| 350 // cache. This happens when SSL_CTX is destroyed. | 385 // cache. This happens when SSL_CTX is destroyed. |
| 351 static void RemoveSessionCallbackStatic(SSL_CTX* ctx, SSL_SESSION* session) { | 386 static void RemoveSessionCallbackStatic(SSL_CTX* ctx, SSL_SESSION* session) { |
| 352 GetCache(ctx)->OnSessionRemoved(session); | 387 GetCache(ctx)->OnSessionRemoved(session); |
| 353 } | 388 } |
| 354 | 389 |
| 355 // Called by OpenSSL to generate a new session ID. This happens during a | 390 // Called by OpenSSL to generate a new session ID. This happens during a |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 462 DLOG(ERROR) << "Couldn't generate unique session ID of " << id_len | 497 DLOG(ERROR) << "Couldn't generate unique session ID of " << id_len |
| 463 << "bytes after " << kMaxTries << " tries."; | 498 << "bytes after " << kMaxTries << " tries."; |
| 464 return false; | 499 return false; |
| 465 } | 500 } |
| 466 | 501 |
| 467 SSL_CTX* ctx_; | 502 SSL_CTX* ctx_; |
| 468 SSLSessionCacheOpenSSL::Config config_; | 503 SSLSessionCacheOpenSSL::Config config_; |
| 469 | 504 |
| 470 // method to get the index which can later be used with SSL_CTX_get_ex_data() | 505 // method to get the index which can later be used with SSL_CTX_get_ex_data() |
| 471 // or SSL_CTX_set_ex_data(). | 506 // or SSL_CTX_set_ex_data(). |
| 472 base::Lock lock_; // Protects access to containers below. | 507 mutable base::Lock lock_; // Protects access to containers below. |
| 473 | 508 |
| 474 MRUSessionList ordering_; | 509 MRUSessionList ordering_; |
| 475 KeyIndex key_index_; | 510 KeyIndex key_index_; |
| 476 SessionIdIndex id_index_; | 511 SessionIdIndex id_index_; |
| 477 | 512 |
| 513 SSLtoCallbackMap ssl_to_callback_map_; | |
|
wtc
2014/06/27 00:36:50
Is this member protected by |lock_|?
mshelley
2014/07/01 02:35:23
No it is not...sorry didn't notice the comment abo
| |
| 514 | |
| 478 size_t expiration_check_; | 515 size_t expiration_check_; |
| 479 }; | 516 }; |
| 480 | 517 |
| 481 SSLSessionCacheOpenSSL::~SSLSessionCacheOpenSSL() { delete impl_; } | 518 SSLSessionCacheOpenSSL::~SSLSessionCacheOpenSSL() { delete impl_; } |
| 482 | 519 |
| 483 size_t SSLSessionCacheOpenSSL::size() const { return impl_->size(); } | 520 size_t SSLSessionCacheOpenSSL::size() const { return impl_->size(); } |
| 484 | 521 |
| 485 void SSLSessionCacheOpenSSL::Reset(SSL_CTX* ctx, const Config& config) { | 522 void SSLSessionCacheOpenSSL::Reset(SSL_CTX* ctx, const Config& config) { |
| 486 if (impl_) | 523 if (impl_) |
| 487 delete impl_; | 524 delete impl_; |
| 488 | 525 |
| 489 impl_ = new SSLSessionCacheOpenSSLImpl(ctx, config); | 526 impl_ = new SSLSessionCacheOpenSSLImpl(ctx, config); |
| 490 } | 527 } |
| 491 | 528 |
| 492 bool SSLSessionCacheOpenSSL::SetSSLSession(SSL* ssl) { | 529 bool SSLSessionCacheOpenSSL::SetSSLSession(SSL* ssl) { |
| 493 return impl_->SetSSLSession(ssl); | 530 return impl_->SetSSLSession(ssl); |
| 494 } | 531 } |
| 495 | 532 |
| 496 bool SSLSessionCacheOpenSSL::SetSSLSessionWithKey( | 533 bool SSLSessionCacheOpenSSL::SetSSLSessionWithKey( |
| 497 SSL* ssl, | 534 SSL* ssl, |
| 498 const std::string& cache_key) { | 535 const std::string& cache_key) { |
| 499 return impl_->SetSSLSessionWithKey(ssl, cache_key); | 536 return impl_->SetSSLSessionWithKey(ssl, cache_key); |
| 500 } | 537 } |
| 501 | 538 |
| 539 bool SSLSessionCacheOpenSSL::SSLSessionIsInCache( | |
| 540 const std::string& cache_key) const { | |
| 541 return impl_->SSLSessionIsInCache(cache_key); | |
| 542 } | |
| 543 | |
| 544 void SSLSessionCacheOpenSSL::NotifyOnSessionAdded(SSL* ssl, | |
| 545 const base::Closure& cb) { | |
| 546 impl_->NotifyOnSessionAdded(ssl, cb); | |
| 547 } | |
| 548 | |
| 502 void SSLSessionCacheOpenSSL::MarkSSLSessionAsGood(SSL* ssl) { | 549 void SSLSessionCacheOpenSSL::MarkSSLSessionAsGood(SSL* ssl) { |
| 503 return impl_->MarkSSLSessionAsGood(ssl); | 550 return impl_->MarkSSLSessionAsGood(ssl); |
| 504 } | 551 } |
| 505 | 552 |
| 506 void SSLSessionCacheOpenSSL::Flush() { impl_->Flush(); } | 553 void SSLSessionCacheOpenSSL::Flush() { impl_->Flush(); } |
| 507 | 554 |
| 508 } // namespace net | 555 } // namespace net |
| OLD | NEW |