| 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 <openssl/ssl.h> | 7 #include <openssl/ssl.h> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "crypto/openssl_util.h" | 12 #include "crypto/openssl_util.h" |
| 13 #include "crypto/scoped_openssl_types.h" |
| 13 | 14 |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 // This is an internal OpenSSL function that can be used to create a new | 17 // This is an internal OpenSSL function that can be used to create a new |
| 17 // session for an existing SSL object. This shall force a call to the | 18 // session for an existing SSL object. This shall force a call to the |
| 18 // 'generate_session_id' callback from the SSL's session context. | 19 // 'generate_session_id' callback from the SSL's session context. |
| 19 // |s| is the target SSL connection handle. | 20 // |s| is the target SSL connection handle. |
| 20 // |session| is non-0 to ask for the creation of a new session. If 0, | 21 // |session| is non-0 to ask for the creation of a new session. If 0, |
| 21 // this will set an empty session with no ID instead. | 22 // this will set an empty session with no ID instead. |
| 22 extern "C" int ssl_get_new_session(SSL* s, int session); | 23 extern "C" int ssl_get_new_session(SSL* s, int session); |
| 23 | 24 |
| 24 // This is an internal OpenSSL function which is used internally to add | 25 // This is an internal OpenSSL function which is used internally to add |
| 25 // a new session to the cache. It is normally triggered by a succesful | 26 // a new session to the cache. It is normally triggered by a succesful |
| 26 // connection. However, this unit test does not use the network at all. | 27 // connection. However, this unit test does not use the network at all. |
| 27 extern "C" void ssl_update_cache(SSL* s, int mode); | 28 extern "C" void ssl_update_cache(SSL* s, int mode); |
| 28 | 29 |
| 29 namespace net { | 30 namespace net { |
| 30 | 31 |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| 33 typedef crypto::ScopedOpenSSL<SSL, SSL_free> ScopedSSL; | 34 typedef scoped_ptr<SSL, crypto::OpenSSLDestroyer<SSL, SSL_free> > ScopedSSL; |
| 35 typedef scoped_ptr<SSL_CTX, crypto::OpenSSLDestroyer<SSL_CTX, SSL_CTX_free> > |
| 36 ScopedSSL_CTX; |
| 34 | 37 |
| 35 // Helper class used to associate arbitrary std::string keys with SSL objects. | 38 // Helper class used to associate arbitrary std::string keys with SSL objects. |
| 36 class SSLKeyHelper { | 39 class SSLKeyHelper { |
| 37 public: | 40 public: |
| 38 // Return the string associated with a given SSL handle |ssl|, or the | 41 // Return the string associated with a given SSL handle |ssl|, or the |
| 39 // empty string if none exists. | 42 // empty string if none exists. |
| 40 static std::string Get(const SSL* ssl) { | 43 static std::string Get(const SSL* ssl) { |
| 41 return GetInstance()->GetValue(ssl); | 44 return GetInstance()->GetValue(ssl); |
| 42 } | 45 } |
| 43 | 46 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 static void ResetSessionID(SSL* ssl) { ssl_get_new_session(ssl, 1); } | 138 static void ResetSessionID(SSL* ssl) { ssl_get_new_session(ssl, 1); } |
| 136 | 139 |
| 137 // Add a given SSL object and its session to the cache. | 140 // Add a given SSL object and its session to the cache. |
| 138 void AddToCache(SSL* ssl) { | 141 void AddToCache(SSL* ssl) { |
| 139 ssl_update_cache(ssl, ctx_.get()->session_cache_mode); | 142 ssl_update_cache(ssl, ctx_.get()->session_cache_mode); |
| 140 } | 143 } |
| 141 | 144 |
| 142 static const SSLSessionCacheOpenSSL::Config kDefaultConfig; | 145 static const SSLSessionCacheOpenSSL::Config kDefaultConfig; |
| 143 | 146 |
| 144 protected: | 147 protected: |
| 145 crypto::ScopedOpenSSL<SSL_CTX, SSL_CTX_free> ctx_; | 148 ScopedSSL_CTX ctx_; |
| 146 // |cache_| must be destroyed before |ctx_| and thus appears after it. | 149 // |cache_| must be destroyed before |ctx_| and thus appears after it. |
| 147 SSLSessionCacheOpenSSL cache_; | 150 SSLSessionCacheOpenSSL cache_; |
| 148 }; | 151 }; |
| 149 | 152 |
| 150 // static | 153 // static |
| 151 const SSLSessionCacheOpenSSL::Config | 154 const SSLSessionCacheOpenSSL::Config |
| 152 SSLSessionCacheOpenSSLTest::kDefaultConfig = { | 155 SSLSessionCacheOpenSSLTest::kDefaultConfig = { |
| 153 &SSLKeyHelper::Get, // key_func | 156 &SSLKeyHelper::Get, // key_func |
| 154 1024, // max_entries | 157 1024, // max_entries |
| 155 256, // expiration_check_count | 158 256, // expiration_check_count |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 | 372 |
| 370 // Call SetSSLSession another time, this shall expire all sessions except | 373 // Call SetSSLSession another time, this shall expire all sessions except |
| 371 // the last one. | 374 // the last one. |
| 372 ScopedSSL bad_ssl(NewSSL("unknown-key")); | 375 ScopedSSL bad_ssl(NewSSL("unknown-key")); |
| 373 cache_.SetSSLSession(bad_ssl.get()); | 376 cache_.SetSSLSession(bad_ssl.get()); |
| 374 bad_ssl.reset(NULL); | 377 bad_ssl.reset(NULL); |
| 375 EXPECT_EQ(1U, cache_.size()); | 378 EXPECT_EQ(1U, cache_.size()); |
| 376 } | 379 } |
| 377 | 380 |
| 378 } // namespace net | 381 } // namespace net |
| OLD | NEW |