proxygen
wangle::SSLSessionCallbacks Class Referenceabstract

#include <SSLSessionCallbacks.h>

Inheritance diagram for wangle::SSLSessionCallbacks:
FakeSessionCallbacks FakeSessionCallbacks wangle::SSLSessionPersistentCacheBase< std::string > wangle::MockSSLSessionCallbacks wangle::SSLSessionPersistentCacheBase< K > wangle::ThreadSafeSSLSessionCache wangle::SSLSessionPersistentCache

Public Member Functions

virtual void setSSLSession (const std::string &identity, SSLSessionPtr session) noexcept=0
 
virtual SSLSessionPtr getSSLSession (const std::string &identity) const noexcept=0
 
virtual bool removeSSLSession (const std::string &identity) noexcept=0
 
virtual bool supportsPersistence () const noexcept
 
virtual size_t size () const
 
virtual ~SSLSessionCallbacks ()
 

Static Public Member Functions

static void attachCallbacksToContext (SSL_CTX *ctx, SSLSessionCallbacks *callbacks)
 
static void detachCallbacksFromContext (SSL_CTX *ctx, SSLSessionCallbacks *callbacks)
 
static SSLSessionCallbacksgetCacheFromContext (SSL_CTX *ctx)
 

Static Private Member Functions

static std::string getSessionKeyFromSSL (SSL *ssl)
 
static int newSessionCallback (SSL *ssl, SSL_SESSION *session)
 
static void removeSessionCallback (SSL_CTX *ctx, SSL_SESSION *session)
 
static int32_tgetCacheIndex ()
 

Detailed Description

Callbacks related to SSL session cache

This class contains three methods, setSSLSession() to store existing SSL session data to cache, getSSLSession() to retreive cached session data in cache, and removeSSLSession() to remove session data from cache.

Definition at line 41 of file SSLSessionCallbacks.h.

Constructor & Destructor Documentation

virtual wangle::SSLSessionCallbacks::~SSLSessionCallbacks ( )
inlinevirtual

Member Function Documentation

void wangle::SSLSessionCallbacks::attachCallbacksToContext ( SSL_CTX *  ctx,
SSLSessionCallbacks callbacks 
)
static

Sets up SSL Session callbacks on a context. The application is responsible for detaching the callbacks from the context.

Definition at line 24 of file SSLSessionCallbacks.cpp.

Referenced by main(), TEST(), and ~SSLSessionCallbacks().

26  {
27  SSL_CTX_set_session_cache_mode(
28  ctx,
29  SSL_SESS_CACHE_NO_INTERNAL | SSL_SESS_CACHE_CLIENT |
30  SSL_SESS_CACHE_NO_AUTO_CLEAR);
31  // Only initializes the cache index the first time.
33  SSL_CTX_set_ex_data(ctx, getCacheIndex(), callbacks);
34  SSL_CTX_sess_set_new_cb(ctx, SSLSessionCallbacks::newSessionCallback);
35  SSL_CTX_sess_set_remove_cb(ctx, SSLSessionCallbacks::removeSessionCallback);
36 }
static int newSessionCallback(SSL *ssl, SSL_SESSION *session)
static void getSSLCtxExIndex(int *pindex)
Definition: SSLUtil.h:74
static int32_t & getCacheIndex()
static void removeSessionCallback(SSL_CTX *ctx, SSL_SESSION *session)
void wangle::SSLSessionCallbacks::detachCallbacksFromContext ( SSL_CTX *  ctx,
SSLSessionCallbacks callbacks 
)
static

Detach the passed in callbacks from the context. If the callbacks are not set on the context, it is unchanged.

Definition at line 39 of file SSLSessionCallbacks.cpp.

Referenced by TEST(), and ~SSLSessionCallbacks().

41  {
42  auto sslSessionCache = getCacheFromContext(ctx);
43  if (sslSessionCache != callbacks) {
44  return;
45  }
46  // We don't unset flags here because we cannot assume that we are the only
47  // code that sets the cache flags.
48  SSL_CTX_set_ex_data(ctx, getCacheIndex(), nullptr);
49  SSL_CTX_sess_set_new_cb(ctx, nullptr);
50  SSL_CTX_sess_set_remove_cb(ctx, nullptr);
51 }
static int32_t & getCacheIndex()
static SSLSessionCallbacks * getCacheFromContext(SSL_CTX *ctx)
SSLSessionCallbacks * wangle::SSLSessionCallbacks::getCacheFromContext ( SSL_CTX *  ctx)
static

Definition at line 54 of file SSLSessionCallbacks.cpp.

Referenced by TEST(), and ~SSLSessionCallbacks().

54  {
55  return static_cast<SSLSessionCallbacks*>(
56  SSL_CTX_get_ex_data(ctx, getCacheIndex()));
57 }
static int32_t & getCacheIndex()
static int32_t& wangle::SSLSessionCallbacks::getCacheIndex ( )
inlinestaticprivate

Definition at line 95 of file SSLSessionCallbacks.h.

References int32_t.

95  {
96  static int32_t sExDataIndex = -1;
97  return sExDataIndex;
98  }
std::string wangle::SSLSessionCallbacks::getSessionKeyFromSSL ( SSL *  ssl)
staticprivate

Definition at line 60 of file SSLSessionCallbacks.cpp.

References folly::AsyncSSLSocket::getFromSSL().

Referenced by ~SSLSessionCallbacks().

60  {
61  auto sock = folly::AsyncSSLSocket::getFromSSL(ssl);
62  return sock ? sock->getSessionKey() : "";
63 }
static AsyncSSLSocket * getFromSSL(const SSL *ssl)
int wangle::SSLSessionCallbacks::newSessionCallback ( SSL *  ssl,
SSL_SESSION *  session 
)
staticprivate

Definition at line 66 of file SSLSessionCallbacks.cpp.

References folly::AsyncSSLSocket::getSSLServerNameFromSSL(), folly::gen::move, name, wangle::setSessionServiceIdentity(), and string.

Referenced by ~SSLSessionCallbacks().

66  {
67  SSLSessionPtr sessionPtr(session);
68  SSL_CTX* ctx = SSL_get_SSL_CTX(ssl);
69  auto sslSessionCache = getCacheFromContext(ctx);
70  std::string sessionKey = getSessionKeyFromSSL(ssl);
71  if (sessionKey.empty()) {
73  sessionKey = name ? name : "";
74  }
75  if (!sessionKey.empty()) {
76  setSessionServiceIdentity(session, sessionKey);
77  sslSessionCache->setSSLSession(sessionKey, std::move(sessionPtr));
78  return 1;
79  }
80  return -1;
81 }
bool setSessionServiceIdentity(SSL_SESSION *session, const std::string &str)
static const char * getSSLServerNameFromSSL(SSL *ssl)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
static std::string getSessionKeyFromSSL(SSL *ssl)
std::unique_ptr< SSL_SESSION, SessionDestructor > SSLSessionPtr
Definition: SSLSession.h:32
const char * name
Definition: http_parser.c:437
const char * string
Definition: Conv.cpp:212
static SSLSessionCallbacks * getCacheFromContext(SSL_CTX *ctx)
void wangle::SSLSessionCallbacks::removeSessionCallback ( SSL_CTX *  ctx,
SSL_SESSION *  session 
)
staticprivate

Definition at line 84 of file SSLSessionCallbacks.cpp.

References wangle::getSessionServiceIdentity(), folly::portability::ssl::SSL_SESSION_get0_hostname(), and string.

Referenced by ~SSLSessionCallbacks().

86  {
87  auto sslSessionCache = getCacheFromContext(ctx);
88  auto identity = getSessionServiceIdentity(session);
89  if (identity && !identity->empty()) {
90  sslSessionCache->removeSSLSession(*identity);
91  }
92 #if OPENSSL_TICKETS
93  else {
94  auto hostname = SSL_SESSION_get0_hostname(session);
95  if (hostname) {
96  sslSessionCache->removeSSLSession(std::string(hostname));
97  }
98  }
99 #endif
100 }
folly::Optional< std::string > getSessionServiceIdentity(SSL_SESSION *session)
const char * SSL_SESSION_get0_hostname(const SSL_SESSION *s)
Definition: OpenSSL.cpp:195
const char * string
Definition: Conv.cpp:212
static SSLSessionCallbacks * getCacheFromContext(SSL_CTX *ctx)
virtual size_t wangle::SSLSessionCallbacks::size ( ) const
inlinevirtual
virtual bool wangle::SSLSessionCallbacks::supportsPersistence ( ) const
inlinevirtualnoexcept

The documentation for this class was generated from the following files: