proxygen
SSLSessionCallbacks.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 //
18 
19 using namespace std::chrono;
20 using namespace folly::ssl;
21 
22 namespace wangle {
23 // static
24 void SSLSessionCallbacks::attachCallbacksToContext(
25  SSL_CTX* ctx,
26  SSLSessionCallbacks* callbacks) {
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.
32  SSLUtil::getSSLCtxExIndex(&getCacheIndex());
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 }
37 
38 // static
39 void SSLSessionCallbacks::detachCallbacksFromContext(
40  SSL_CTX* ctx,
41  SSLSessionCallbacks* callbacks) {
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 }
52 
53 // static
54 SSLSessionCallbacks* SSLSessionCallbacks::getCacheFromContext(SSL_CTX* ctx) {
55  return static_cast<SSLSessionCallbacks*>(
56  SSL_CTX_get_ex_data(ctx, getCacheIndex()));
57 }
58 
59 // static
60 std::string SSLSessionCallbacks::getSessionKeyFromSSL(SSL* ssl) {
61  auto sock = folly::AsyncSSLSocket::getFromSSL(ssl);
62  return sock ? sock->getSessionKey() : "";
63 }
64 
65 // static
66 int SSLSessionCallbacks::newSessionCallback(SSL* ssl, SSL_SESSION* session) {
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 }
82 
83 // static
84 void SSLSessionCallbacks::removeSessionCallback(
85  SSL_CTX* ctx,
86  SSL_SESSION* session) {
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 }
101 } // wangle
bool setSessionServiceIdentity(SSL_SESSION *session, const std::string &str)
static const char * getSSLServerNameFromSSL(SSL *ssl)
folly::Optional< std::string > getSessionServiceIdentity(SSL_SESSION *session)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::unique_ptr< SSL_SESSION, SessionDestructor > SSLSessionPtr
Definition: SSLSession.h:32
const char * name
Definition: http_parser.c:437
const char * SSL_SESSION_get0_hostname(const SSL_SESSION *s)
Definition: OpenSSL.cpp:195
const char * string
Definition: Conv.cpp:212
static AsyncSSLSocket * getFromSSL(const SSL *ssl)