Commit 82070c96 authored by Kyle Nekritz's avatar Kyle Nekritz Committed by Facebook GitHub Bot

Back out "Give ownership of new session callbacks to SSLContext"

Summary:
Original commit changeset: f1dc8bd17150

This accidentally enabled the openssl internal session cache which can use
excessive memory.

Reviewed By: jalopezsilva, mingtaoy

Differential Revision: D21433514

fbshipit-source-id: 91358f6fd2decc0d18eff7ca8194b677f3c8629c
parent 6808eb56
...@@ -35,6 +35,10 @@ int getExDataIndex() { ...@@ -35,6 +35,10 @@ int getExDataIndex() {
return index; return index;
} }
void setExData(folly::SSLContext* context, SSL_CTX* ctx) {
SSL_CTX_set_ex_data(ctx, getExDataIndex(), context);
}
} // namespace } // namespace
namespace folly { namespace folly {
...@@ -88,7 +92,7 @@ SSLContext::SSLContext(SSLVersion version) { ...@@ -88,7 +92,7 @@ SSLContext::SSLContext(SSLVersion version) {
sslAcceptRunner_ = std::make_unique<SSLAcceptRunner>(); sslAcceptRunner_ = std::make_unique<SSLAcceptRunner>();
setupCtx(ctx_); setExData(this, ctx_);
#if FOLLY_OPENSSL_HAS_SNI #if FOLLY_OPENSSL_HAS_SNI
SSL_CTX_set_tlsext_servername_callback(ctx_, baseServerNameOpenSSLCallback); SSL_CTX_set_tlsext_servername_callback(ctx_, baseServerNameOpenSSLCallback);
...@@ -155,7 +159,7 @@ void SSLContext::setServerECCurve(const std::string& curveName) { ...@@ -155,7 +159,7 @@ void SSLContext::setServerECCurve(const std::string& curveName) {
} }
SSLContext::SSLContext(SSL_CTX* ctx) : ctx_(ctx) { SSLContext::SSLContext(SSL_CTX* ctx) : ctx_(ctx) {
setupCtx(ctx); setExData(this, ctx);
if (SSL_CTX_up_ref(ctx) == 0) { if (SSL_CTX_up_ref(ctx) == 0) {
throw std::runtime_error("Failed to increment SSL_CTX refcount"); throw std::runtime_error("Failed to increment SSL_CTX refcount");
} }
...@@ -668,43 +672,10 @@ void SSLContext::enableTLS13() { ...@@ -668,43 +672,10 @@ void SSLContext::enableTLS13() {
#endif #endif
} }
void SSLContext::setupCtx(SSL_CTX* ctx) {
// Client caching required for receiving sessions in TLS 1.3
// Default value from OpenSSL is SSL_SESS_CACHE_SERVER
SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_BOTH);
SSL_CTX_set_ex_data(ctx, getExDataIndex(), this);
SSL_CTX_sess_set_new_cb(ctx, SSLContext::newSessionCallback);
}
SSLContext* SSLContext::getFromSSLCtx(const SSL_CTX* ctx) { SSLContext* SSLContext::getFromSSLCtx(const SSL_CTX* ctx) {
return static_cast<SSLContext*>(SSL_CTX_get_ex_data(ctx, getExDataIndex())); return static_cast<SSLContext*>(SSL_CTX_get_ex_data(ctx, getExDataIndex()));
} }
int SSLContext::newSessionCallback(SSL* ssl, SSL_SESSION* session) {
SSL_CTX* ctx = SSL_get_SSL_CTX(ssl);
SSLContext* context = getFromSSLCtx(ctx);
auto& cb = context->sessionLifecycleCallbacks_;
if (cb != nullptr && cb) {
SSL_SESSION_up_ref(session);
auto sessionPtr = folly::ssl::SSLSessionUniquePtr(session);
cb->onNewSession(ssl, std::move(sessionPtr));
}
SSL_SESSION_free(session);
return 1;
}
void SSLContext::attachSessionLifecycleCallbacks(
std::unique_ptr<SessionLifecycleCallbacks> cb) {
sessionLifecycleCallbacks_ = std::move(cb);
}
void SSLContext::removeSessionLifecycleCallbacks() {
sessionLifecycleCallbacks_ = nullptr;
}
std::ostream& operator<<(std::ostream& os, const PasswordCollector& collector) { std::ostream& operator<<(std::ostream& os, const PasswordCollector& collector) {
os << collector.describe(); os << collector.describe();
return os; return os;
......
...@@ -84,11 +84,6 @@ class SSLAcceptRunner { ...@@ -84,11 +84,6 @@ class SSLAcceptRunner {
*/ */
class SSLContext { class SSLContext {
public: public:
struct SessionLifecycleCallbacks {
virtual void onNewSession(SSL*, folly::ssl::SSLSessionUniquePtr) = 0;
virtual ~SessionLifecycleCallbacks() = default;
};
enum SSLVersion { enum SSLVersion {
SSLv2, SSLv2,
SSLv3, SSLv3,
...@@ -569,11 +564,6 @@ class SSLContext { ...@@ -569,11 +564,6 @@ class SSLContext {
*/ */
static SSLContext* getFromSSLCtx(const SSL_CTX* ctx); static SSLContext* getFromSSLCtx(const SSL_CTX* ctx);
void attachSessionLifecycleCallbacks(
std::unique_ptr<SessionLifecycleCallbacks> cb);
void removeSessionLifecycleCallbacks();
[[deprecated("Use folly::ssl::init")]] static void initializeOpenSSL(); [[deprecated("Use folly::ssl::init")]] static void initializeOpenSSL();
protected: protected:
...@@ -648,13 +638,6 @@ class SSLContext { ...@@ -648,13 +638,6 @@ class SSLContext {
#endif #endif
std::string providedCiphersString_; std::string providedCiphersString_;
void setupCtx(SSL_CTX* ctx);
std::unique_ptr<SessionLifecycleCallbacks> sessionLifecycleCallbacks_{
nullptr};
static int newSessionCallback(SSL* ssl, SSL_SESSION* session);
}; };
typedef std::shared_ptr<SSLContext> SSLContextPtr; typedef std::shared_ptr<SSLContext> SSLContextPtr;
......
...@@ -30,18 +30,18 @@ using folly::ssl::detail::OpenSSLSession; ...@@ -30,18 +30,18 @@ using folly::ssl::detail::OpenSSLSession;
namespace folly { namespace folly {
class SimpleCallbackManager class SimpleCallbackManager {
: public folly::SSLContext::SessionLifecycleCallbacks {
public: public:
void onNewSession(SSL* ssl, folly::ssl::SSLSessionUniquePtr session) static int sessionCallback(SSL* ssl, SSL_SESSION* session) {
override { auto sessionPtr = folly::ssl::SSLSessionUniquePtr(session);
auto socket = folly::AsyncSSLSocket::getFromSSL(ssl); auto socket = folly::AsyncSSLSocket::getFromSSL(ssl);
auto sslSession = auto sslSession =
std::dynamic_pointer_cast<folly::ssl::detail::OpenSSLSession>( std::dynamic_pointer_cast<folly::ssl::detail::OpenSSLSession>(
socket->getSSLSessionV2()); socket->getSSLSessionV2());
if (sslSession) { if (sslSession) {
sslSession->setActiveSession(std::move(session)); sslSession->setActiveSession(std::move(sessionPtr));
} }
return 1;
} }
}; };
...@@ -62,8 +62,13 @@ void getctx( ...@@ -62,8 +62,13 @@ void getctx(
std::shared_ptr<folly::SSLContext> serverCtx) { std::shared_ptr<folly::SSLContext> serverCtx) {
clientCtx->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); clientCtx->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
clientCtx->loadTrustedCertificates(kTestCA); clientCtx->loadTrustedCertificates(kTestCA);
clientCtx->attachSessionLifecycleCallbacks( auto clientCtx_ = clientCtx->getSSLCtx();
std::make_unique<SimpleCallbackManager>());
// The following two actions (setting session cache mode and
// setting the session callback) will eventually be done in
// SSLContext instead
SSL_CTX_set_session_cache_mode(clientCtx_, SSL_SESS_CACHE_CLIENT);
SSL_CTX_sess_set_new_cb(clientCtx_, SimpleCallbackManager::sessionCallback);
serverCtx->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); serverCtx->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
serverCtx->loadCertificate(kTestCert); serverCtx->loadCertificate(kTestCert);
...@@ -171,6 +176,7 @@ TEST_F(SSLSessionTest, BasicRegressionTest) { ...@@ -171,6 +176,7 @@ TEST_F(SSLSessionTest, BasicRegressionTest) {
sslSession = clientPtr->getSSLSession(); sslSession = clientPtr->getSSLSession();
ASSERT_NE(sslSession, nullptr); ASSERT_NE(sslSession, nullptr);
SSL_SESSION_free(sslSession);
} }
// Session resumption // Session resumption
...@@ -192,7 +198,6 @@ TEST_F(SSLSessionTest, BasicRegressionTest) { ...@@ -192,7 +198,6 @@ TEST_F(SSLSessionTest, BasicRegressionTest) {
eventBase.loop(); eventBase.loop();
ASSERT_TRUE(client.handshakeSuccess_); ASSERT_TRUE(client.handshakeSuccess_);
ASSERT_TRUE(clientPtr->getSSLSessionReused()); ASSERT_TRUE(clientPtr->getSSLSessionReused());
SSL_SESSION_free(sslSession);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment