removing non-existing file from the build
[folly.git] / folly / wangle / ssl / SSLContextManager.h
1 /*
2  *  Copyright (c) 2015, Facebook, Inc.
3  *  All rights reserved.
4  *
5  *  This source code is licensed under the BSD-style license found in the
6  *  LICENSE file in the root directory of this source tree. An additional grant
7  *  of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10 #pragma once
11
12 #include <folly/io/async/EventBase.h>
13 #include <folly/io/async/SSLContext.h>
14
15 #include <glog/logging.h>
16 #include <list>
17 #include <memory>
18 #include <folly/wangle/ssl/SSLContextConfig.h>
19 #include <folly/wangle/ssl/SSLSessionCacheManager.h>
20 #include <folly/wangle/ssl/TLSTicketKeySeeds.h>
21 #include <folly/wangle/acceptor/DomainNameMisc.h>
22 #include <vector>
23
24 namespace folly {
25
26 class SocketAddress;
27 class SSLContext;
28 class ClientHelloExtStats;
29 struct SSLCacheOptions;
30 class SSLStats;
31 class TLSTicketKeyManager;
32 struct TLSTicketKeySeeds;
33
34 class SSLContextManager {
35  public:
36
37   explicit SSLContextManager(EventBase* eventBase,
38                              const std::string& vipName, bool strict,
39                              SSLStats* stats);
40   virtual ~SSLContextManager();
41
42   /**
43    * Add a new X509 to SSLContextManager.  The details of a X509
44    * is passed as a SSLContextConfig object.
45    *
46    * @param ctxConfig     Details of a X509, its private key, password, etc.
47    * @param cacheOptions  Options for how to do session caching.
48    * @param ticketSeeds   If non-null, the initial ticket key seeds to use.
49    * @param vipAddress    Which VIP are the X509(s) used for? It is only for
50    *                      for user friendly log message
51    * @param externalCache Optional external provider for the session cache;
52    *                      may be null
53    */
54   void addSSLContextConfig(
55     const SSLContextConfig& ctxConfig,
56     const SSLCacheOptions& cacheOptions,
57     const TLSTicketKeySeeds* ticketSeeds,
58     const folly::SocketAddress& vipAddress,
59     const std::shared_ptr<SSLCacheProvider> &externalCache);
60
61   /**
62    * Get the default SSL_CTX for a VIP
63    */
64   std::shared_ptr<SSLContext>
65     getDefaultSSLCtx() const;
66
67   /**
68    * Search by the _one_ level up subdomain
69    */
70   std::shared_ptr<SSLContext>
71     getSSLCtxBySuffix(const DNString& dnstr) const;
72
73   /**
74    * Search by the full-string domain name
75    */
76   std::shared_ptr<SSLContext>
77     getSSLCtx(const DNString& dnstr) const;
78
79   /**
80    * Insert a SSLContext by domain name.
81    */
82   void insertSSLCtxByDomainName(
83     const char* dn,
84     size_t len,
85     std::shared_ptr<SSLContext> sslCtx);
86
87   void insertSSLCtxByDomainNameImpl(
88     const char* dn,
89     size_t len,
90     std::shared_ptr<SSLContext> sslCtx);
91
92   void reloadTLSTicketKeys(const std::vector<std::string>& oldSeeds,
93                            const std::vector<std::string>& currentSeeds,
94                            const std::vector<std::string>& newSeeds);
95
96   /**
97    * SSLContextManager only collects SNI stats now
98    */
99
100   void setClientHelloExtStats(ClientHelloExtStats* stats) {
101     clientHelloTLSExtStats_ = stats;
102   }
103
104  protected:
105   virtual void enableAsyncCrypto(
106     const std::shared_ptr<SSLContext>& sslCtx) {
107     LOG(FATAL) << "Unsupported in base SSLContextManager";
108   }
109   SSLStats* stats_{nullptr};
110
111  private:
112   SSLContextManager(const SSLContextManager&) = delete;
113
114   void ctxSetupByOpensslFeature(
115     std::shared_ptr<SSLContext> sslCtx,
116     const SSLContextConfig& ctxConfig);
117
118   /**
119    * Callback function from openssl to find the right X509 to
120    * use during SSL handshake
121    */
122 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && \
123     !defined(OPENSSL_NO_TLSEXT) && \
124     defined(SSL_CTRL_SET_TLSEXT_SERVERNAME_CB)
125 # define PROXYGEN_HAVE_SERVERNAMECALLBACK
126   SSLContext::ServerNameCallbackResult
127     serverNameCallback(SSL* ssl);
128 #endif
129
130   /**
131    * The following functions help to maintain the data structure for
132    * domain name matching in SNI.  Some notes:
133    *
134    * 1. It is a best match.
135    *
136    * 2. It allows wildcard CN and wildcard subject alternative name in a X509.
137    *    The wildcard name must be _prefixed_ by '*.'.  It errors out whenever
138    *    it sees '*' in any other locations.
139    *
140    * 3. It uses one std::unordered_map<DomainName, SSL_CTX> object to
141    *    do this.  For wildcard name like "*.facebook.com", ".facebook.com"
142    *    is used as the key.
143    *
144    * 4. After getting tlsext_hostname from the client hello message, it
145    *    will do a full string search first and then try one level up to
146    *    match any wildcard name (if any) in the X509.
147    *    [Note, browser also only looks one level up when matching the requesting
148    *     domain name with the wildcard name in the server X509].
149    */
150
151   void insert(
152     std::shared_ptr<SSLContext> sslCtx,
153     std::unique_ptr<SSLSessionCacheManager> cmanager,
154     std::unique_ptr<TLSTicketKeyManager> tManager,
155     bool defaultFallback);
156
157   /**
158    * Container to own the SSLContext, SSLSessionCacheManager and
159    * TLSTicketKeyManager.
160    */
161   std::vector<std::shared_ptr<SSLContext>> ctxs_;
162   std::vector<std::unique_ptr<SSLSessionCacheManager>>
163     sessionCacheManagers_;
164   std::vector<std::unique_ptr<TLSTicketKeyManager>> ticketManagers_;
165
166   std::shared_ptr<SSLContext> defaultCtx_;
167
168   /**
169    * Container to store the (DomainName -> SSL_CTX) mapping
170    */
171   std::unordered_map<
172     DNString,
173     std::shared_ptr<SSLContext>,
174     DNStringHash> dnMap_;
175
176   EventBase* eventBase_;
177   ClientHelloExtStats* clientHelloTLSExtStats_{nullptr};
178   SSLContextConfig::SNINoMatchFn noMatchFn_;
179   bool strict_{true};
180 };
181
182 } // namespace