proxygen
SSLSessionPersistentCache-inl.h
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 #pragma once
17 
19 
20 #include <folly/io/IOBuf.h>
21 #include <folly/Memory.h>
24 
25 namespace wangle {
26 
27 template<typename K>
29  std::shared_ptr<PersistentCache<K, SSLSessionCacheData>> cache) :
30  persistentCache_(cache),
31  timeUtil_(new TimeUtil()) {}
32 
33 template <typename K>
35  std::shared_ptr<folly::Executor> executor,
36  const std::string& filename,
37  std::size_t cacheCapacity,
38  std::chrono::seconds syncInterval)
41  std::move(executor),
42  filename,
43  cacheCapacity,
44  syncInterval,
45  client::persistence::DEFAULT_CACHE_SYNC_RETRIES)) {}
46 
47 template <typename K>
49  const std::string& filename,
50  std::size_t cacheCapacity,
51  std::chrono::seconds syncInterval)
54  filename,
55  cacheCapacity,
56  syncInterval)) {}
57 
58 template<typename K>
60  const std::string& identity, SSLSessionPtr session) noexcept {
61  if (!session) {
62  return;
63  }
64 
65  // We do not cache the session itself, but cache the session data from it in
66  // order to recreate a new session later.
67  auto sessionCacheData = getCacheDataForSession(session.get());
68  if (sessionCacheData) {
69  auto key = getKey(identity);
70  sessionCacheData->addedTime = timeUtil_->now();
71  persistentCache_->put(key, *sessionCacheData);
72  }
73 }
74 
75 template<typename K>
77  const std::string& identity) const noexcept {
78  auto key = getKey(identity);
79  auto hit = persistentCache_->get(key);
80  if (!hit) {
81  return nullptr;
82  }
83 
84  // Create a SSL_SESSION and return. In failure it returns nullptr.
85  auto& value = hit.value();
87 
88 #if OPENSSL_TICKETS
89  if (sess &&
90  SSL_SESSION_has_ticket(sess.get()) &&
91  SSL_SESSION_get_ticket_lifetime_hint(sess.get()) > 0) {
92  auto now = timeUtil_->now();
93  auto secsBetween =
94  std::chrono::duration_cast<std::chrono::seconds>(now - value.addedTime);
95  if (secsBetween >= std::chrono::seconds(SSL_SESSION_get_ticket_lifetime_hint(sess.get()))) {
96  return nullptr;
97  }
98  }
99 #endif
100 
101  return sess;
102 }
103 
104 template<typename K>
106  const std::string& identity) noexcept {
107  auto key = getKey(identity);
108  return persistentCache_->remove(key);
109 }
110 
111 template<typename K>
113  return persistentCache_->size();
114 }
115 
116 }
std::chrono::steady_clock::time_point now()
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
folly::Optional< SSLSessionCacheData > getCacheDataForSession(SSL_SESSION *sess)
requires E e noexcept(noexcept(s.error(std::move(e))))
PUSHMI_INLINE_VAR constexpr __adl::get_executor_fn executor
void setSSLSession(const std::string &identity, SSLSessionPtr session) noexceptoverride
std::shared_ptr< PersistentCache< K, SSLSessionCacheData > > persistentCache_
std::unique_ptr< SSL_SESSION, SessionDestructor > SSLSessionPtr
Definition: SSLSession.h:32
bool removeSSLSession(const std::string &identity) noexceptoverride
int SSL_SESSION_has_ticket(const SSL_SESSION *s)
Definition: OpenSSL.cpp:203
SSL_SESSION * getSessionFromCacheData(const SSLSessionCacheData &data)
SSLSessionPtr getSSLSession(const std::string &identity) const noexceptoverride
static const char *const value
Definition: Conv.cpp:50
unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s)
Definition: OpenSSL.cpp:207
const char * string
Definition: Conv.cpp:212
constexpr int DEFAULT_CACHE_SYNC_RETRIES
virtual K getKey(const std::string &identity) const =0
SSLSessionPersistentCacheBase(std::shared_ptr< PersistentCache< K, SSLSessionCacheData >> cache)