proxygen
SSLContextManager.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 
20 #include <folly/SharedMutex.h>
21 
22 #include <glog/logging.h>
23 #include <list>
24 #include <memory>
29 #include <vector>
30 
31 namespace folly {
32 
33 class SocketAddress;
34 class SSLContext;
35 
36 }
37 
38 namespace wangle {
39 
40 class ClientHelloExtStats;
41 struct SSLCacheOptions;
42 class SSLStats;
43 class TLSTicketKeyManager;
44 struct TLSTicketKeySeeds;
45 class ServerSSLContext;
46 
48  private:
49  struct SslContexts {
50  void clear();
51  void swap(SslContexts& other) noexcept;
52 
53  std::vector<std::shared_ptr<ServerSSLContext>> ctxs;
54  std::shared_ptr<ServerSSLContext> defaultCtx;
56 
60  std::unordered_map<
62  std::shared_ptr<folly::SSLContext>,
64  };
65 
66  public:
67 
73 
74  // no-op. Should be overridden if actual
75  // verification is required
76  virtual void attachSSLContext(
77  const std::shared_ptr<folly::SSLContext>& sslCtx) = 0;
79  };
80 
81 
82  explicit SSLContextManager(folly::EventBase* eventBase,
83  const std::string& vipName, bool strict,
84  SSLStats* stats);
85  virtual ~SSLContextManager();
86 
99  void addSSLContextConfig(
100  const SSLContextConfig& ctxConfig,
101  const SSLCacheOptions& cacheOptions,
102  const TLSTicketKeySeeds* ticketSeeds,
103  const folly::SocketAddress& vipAddress,
104  const std::shared_ptr<SSLCacheProvider> &externalCache,
105  SslContexts* contexts = nullptr);
106 
118  void resetSSLContextConfigs(
119  const std::vector<SSLContextConfig>& ctxConfig,
120  const SSLCacheOptions& cacheOptions,
121  const TLSTicketKeySeeds* ticketSeeds,
122  const folly::SocketAddress& vipAddress,
123  const std::shared_ptr<SSLCacheProvider> &externalCache);
124 
128  void clear();
129 
133  std::shared_ptr<folly::SSLContext>
134  getDefaultSSLCtx() const;
135 
139  std::shared_ptr<folly::SSLContext>
140  getSSLCtx(const SSLContextKey& key) const;
141 
145  std::shared_ptr<folly::SSLContext>
146  getSSLCtxBySuffix(const SSLContextKey& key) const;
147 
151  std::shared_ptr<folly::SSLContext>
152  getSSLCtxByExactDomain(const SSLContextKey& key) const;
153 
154  void reloadTLSTicketKeys(const std::vector<std::string>& oldSeeds,
155  const std::vector<std::string>& currentSeeds,
156  const std::vector<std::string>& newSeeds);
157 
158  void setSSLStats(SSLStats* stats) {
159  stats_ = stats;
160  }
161 
166  clientHelloTLSExtStats_ = stats;
167  }
168 
169  void setClientVerifyCallback(std::unique_ptr<ClientCertVerifyCallback> cb) {
170  clientCertVerifyCallback_ = std::move(cb);
171  }
172 
173  protected:
175  const std::shared_ptr<folly::SSLContext>&,
176  const SSLContextConfig&,
177  const std::string& /* certificateFile */) {
178  LOG(FATAL) << "Unsupported in base SSLContextManager";
179  }
180 
181  virtual void overrideConfiguration(
182  const std::shared_ptr<folly::SSLContext>&,
183  const SSLContextConfig&) {}
184 
186  SSLStats* stats_{nullptr};
187 
191  void insertSSLCtxByDomainName(
192  const char* dn,
193  size_t len,
194  std::shared_ptr<folly::SSLContext> sslCtx,
195  SslContexts& contexts,
196  CertCrypto certCrypto = CertCrypto::BEST_AVAILABLE);
197 
199  const char* dn,
200  size_t len,
201  std::shared_ptr<folly::SSLContext> sslCtx,
202  CertCrypto certCrypto = CertCrypto::BEST_AVAILABLE) {
203  insertSSLCtxByDomainName(dn, len, sslCtx, contexts_, certCrypto);
204  }
205 
206  private:
207  SSLContextManager(const SSLContextManager&) = delete;
208 
209  void ctxSetupByOpensslFeature(
210  std::shared_ptr<ServerSSLContext> sslCtx,
211  const SSLContextConfig& ctxConfig,
212  SslContexts& contexts);
213 
218 #if FOLLY_OPENSSL_HAS_SNI
219 # define PROXYGEN_HAVE_SERVERNAMECALLBACK
220  folly::SSLContext::ServerNameCallbackResult
221  serverNameCallback(SSL* ssl);
222 #endif
223 
245  void insert(
246  std::shared_ptr<ServerSSLContext> sslCtx,
247  bool defaultFallback,
248  SslContexts& contexts);
249 
250  void insertSSLCtxByDomainNameImpl(
251  const char* dn,
252  size_t len,
253  std::shared_ptr<folly::SSLContext> sslCtx,
254  SslContexts& contexts,
255  CertCrypto certCrypto);
256 
257  void insertIntoDnMap(SSLContextKey key,
258  std::shared_ptr<folly::SSLContext> sslCtx,
259  bool overwrite,
260  SslContexts& contexts);
261 
264  ClientHelloExtStats* clientHelloTLSExtStats_{nullptr};
266  bool strict_{true};
267  std::unique_ptr<ClientCertVerifyCallback> clientCertVerifyCallback_{nullptr};
268 };
269 
270 } // namespace wangle
virtual void loadCertKeyPairExternal(const std::shared_ptr< folly::SSLContext > &, const SSLContextConfig &, const std::string &)
SSLContextConfig::SNINoMatchFn noMatchFn_
std::vector< std::shared_ptr< ServerSSLContext > > ctxs
std::shared_ptr< ServerSSLContext > defaultCtx
virtual void overrideConfiguration(const std::shared_ptr< folly::SSLContext > &, const SSLContextConfig &)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void insertSSLCtxByDomainName(const char *dn, size_t len, std::shared_ptr< folly::SSLContext > sslCtx, CertCrypto certCrypto=CertCrypto::BEST_AVAILABLE)
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
std::function< bool(char const *server_name)> SNINoMatchFn
void setClientHelloExtStats(ClientHelloExtStats *stats)
std::unordered_map< SSLContextKey, std::shared_ptr< folly::SSLContext >, SSLContextKeyHash > dnMap
void setClientVerifyCallback(std::unique_ptr< ClientCertVerifyCallback > cb)
void swap(exception_wrapper &a, exception_wrapper &b) noexcept
const char * string
Definition: Conv.cpp:212
folly::EventBase * eventBase_
void setSSLStats(SSLStats *stats)