2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2012 Tatsuhiro Tsujikawa
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 #include <openssl/ssl.h>
34 #include <openssl/err.h>
38 #ifdef HAVE_NEVERBLEED
39 # include <neverbleed.h>
40 #endif // HAVE_NEVERBLEED
43 #include "shrpx_config.h"
44 #include "shrpx_router.h"
50 class DownstreamConnectionPool;
51 struct DownstreamAddr;
56 struct TLSSessionCache {
57 // ASN1 representation of SSL_SESSION object. See
58 // i2d_SSL_SESSION(3SSL).
59 std::vector<uint8_t> session_data;
60 // The last time stamp when this cache entry is created or updated.
61 ev_tstamp last_updated;
64 // This struct stores the additional information per SSL_CTX. This is
65 // attached to SSL_CTX using SSL_CTX_set_app_data().
66 struct TLSContextData {
67 // SCT data formatted so that this can be directly sent as
68 // extension_data of signed_certificate_timestamp.
69 std::vector<uint8_t> sct_data;
70 #ifndef HAVE_ATOMIC_STD_SHARED_PTR
71 // Protects ocsp_data;
73 #endif // !HAVE_ATOMIC_STD_SHARED_PTR
75 std::shared_ptr<std::vector<uint8_t>> ocsp_data;
77 // Path to certificate file
78 const char *cert_file;
81 // Create server side SSL_CTX
82 SSL_CTX *create_ssl_context(const char *private_key_file, const char *cert_file,
83 const std::vector<uint8_t> &sct_data
85 #ifdef HAVE_NEVERBLEED
88 #endif // HAVE_NEVERBLEED
91 // Create client side SSL_CTX. This does not configure ALPN settings.
92 // |next_proto_select_cb| is for NPN.
93 SSL_CTX *create_ssl_client_context(
94 #ifdef HAVE_NEVERBLEED
96 #endif // HAVE_NEVERBLEED
97 const StringRef &cacert, const StringRef &cert_file,
98 const StringRef &private_key_file,
99 int (*next_proto_select_cb)(SSL *s, unsigned char **out,
100 unsigned char *outlen, const unsigned char *in,
101 unsigned int inlen, void *arg));
104 SSL_CTX *create_quic_ssl_client_context(
105 # ifdef HAVE_NEVERBLEED
107 # endif // HAVE_NEVERBLEED
108 const StringRef &cacert, const StringRef &cert_file,
109 const StringRef &private_key_file,
110 int (*next_proto_select_cb)(SSL *s, unsigned char **out,
111 unsigned char *outlen, const unsigned char *in,
112 unsigned int inlen, void *arg));
113 #endif // ENABLE_HTTP3
115 ClientHandler *accept_connection(Worker *worker, int fd, sockaddr *addr,
116 int addrlen, const UpstreamAddr *faddr);
118 // Check peer's certificate against given |address| and |host|.
119 int check_cert(SSL *ssl, const Address *addr, const StringRef &host);
120 // Check peer's certificate against given host name described in
121 // |addr| and numeric address in |raddr|. Note that |raddr| might not
122 // point to &addr->addr.
123 int check_cert(SSL *ssl, const DownstreamAddr *addr, const Address *raddr);
125 struct WildcardRevPrefix {
126 WildcardRevPrefix(const StringRef &prefix, size_t idx)
127 : prefix(std::begin(prefix), std::end(prefix)), idx(idx) {}
129 // "Prefix" of wildcard pattern. It is reversed from original form.
130 // For example, if the original wildcard is "test*.nghttp2.org",
131 // prefix would be "tset".
132 ImmutableString prefix;
133 // The index of SSL_CTX. See ConnectionHandler::get_ssl_ctx().
137 struct WildcardPattern {
138 // Wildcard host sharing only suffix is probably rare, so we just do
140 std::vector<WildcardRevPrefix> rev_prefix;
143 class CertLookupTree {
147 // Adds hostname pattern |hostname| to the lookup tree, associating
148 // value |index|. When the queried host matches this pattern,
149 // |index| is returned. We support wildcard pattern. The left most
150 // '*' is considered as wildcard character, and it must match at
151 // least one character. If the same pattern has been already added,
152 // this function does not alter the tree, and returns the existing
155 // The caller should lower-case |hostname| since this function does
156 // do that, and lookup function performs case-sensitive match.
158 // TODO Treat wildcard pattern described as RFC 6125.
160 // This function returns the index. It returns -1 if it fails
161 // (e.g., hostname is too long). If the returned index equals to
162 // |index|, then hostname is added to the tree with the value
163 // |index|. If it is not -1, and does not equal to |index|, same
164 // hostname has already been added to the tree.
165 ssize_t add_cert(const StringRef &hostname, size_t index);
167 // Looks up index using the given |hostname|. The exact match takes
168 // precedence over wildcard match. For wildcard match, longest
169 // match (sum of matched suffix and prefix length in bytes) is
170 // preferred, breaking a tie with longer suffix.
172 // The caller should lower-case |hostname| since this function
173 // performs case-sensitive match.
174 ssize_t lookup(const StringRef &hostname);
176 // Dumps the contents of this lookup tree to stderr.
182 // Wildcard reversed suffix match. The returned index is into
183 // wildcard_patterns_.
184 Router rev_wildcard_router_;
185 // Stores wildcard suffix patterns.
186 std::vector<WildcardPattern> wildcard_patterns_;
189 // Adds hostnames in certificate in |ssl_ctx| to lookup tree |lt|.
190 // The subjectAltNames and commonName are considered as eligible
191 // hostname. If there is at least one dNSName in subjectAltNames,
192 // commonName is not considered. |ssl_ctx| is also added to
193 // |indexed_ssl_ctx|. This function returns 0 if it succeeds, or -1.
194 int cert_lookup_tree_add_ssl_ctx(
195 CertLookupTree *lt, std::vector<std::vector<SSL_CTX *>> &indexed_ssl_ctx,
198 // Returns true if |proto| is included in the
199 // protocol list |protos|.
200 bool in_proto_list(const std::vector<StringRef> &protos,
201 const StringRef &proto);
203 // Returns true if security requirement for HTTP/2 is fulfilled.
204 bool check_http2_requirement(SSL *ssl);
206 // Returns SSL/TLS option mask to disable SSL/TLS protocol version not
207 // included in |tls_proto_list|. The returned mask can be directly
208 // passed to SSL_CTX_set_options().
209 long int create_tls_proto_mask(const std::vector<StringRef> &tls_proto_list);
211 int set_alpn_prefs(std::vector<unsigned char> &out,
212 const std::vector<StringRef> &protos);
214 // Setups server side SSL_CTX. This function inspects get_config()
215 // and if upstream_no_tls is true, returns nullptr. Otherwise
216 // construct default SSL_CTX. If subcerts are available
217 // (get_config()->subcerts), caller should provide CertLookupTree
218 // object as |cert_tree| parameter, otherwise SNI does not work. All
219 // the created SSL_CTX is stored into |all_ssl_ctx|. They are also
220 // added to |indexed_ssl_ctx|. |cert_tree| uses its index to
221 // associate hostname to the SSL_CTX.
223 setup_server_ssl_context(std::vector<SSL_CTX *> &all_ssl_ctx,
224 std::vector<std::vector<SSL_CTX *>> &indexed_ssl_ctx,
225 CertLookupTree *cert_tree
226 #ifdef HAVE_NEVERBLEED
229 #endif // HAVE_NEVERBLEED
233 SSL_CTX *setup_quic_server_ssl_context(
234 std::vector<SSL_CTX *> &all_ssl_ctx,
235 std::vector<std::vector<SSL_CTX *>> &indexed_ssl_ctx,
236 CertLookupTree *cert_tree
237 # ifdef HAVE_NEVERBLEED
240 # endif // HAVE_NEVERBLEED
242 #endif // ENABLE_HTTP3
244 // Setups client side SSL_CTX.
245 SSL_CTX *setup_downstream_client_ssl_context(
246 #ifdef HAVE_NEVERBLEED
248 #endif // HAVE_NEVERBLEED
251 // Sets ALPN settings in |SSL| suitable for HTTP/2 use.
252 void setup_downstream_http2_alpn(SSL *ssl);
253 // Sets ALPN settings in |SSL| suitable for HTTP/1.1 use.
254 void setup_downstream_http1_alpn(SSL *ssl);
256 // Creates CertLookupTree. If frontend is configured not to use TLS,
257 // this function returns nullptr.
258 std::unique_ptr<CertLookupTree> create_cert_lookup_tree();
260 SSL *create_ssl(SSL_CTX *ssl_ctx);
262 // Returns true if SSL/TLS is enabled on upstream
263 bool upstream_tls_enabled(const ConnectionConfig &connconf);
265 // Performs TLS hostname match. |pattern| can contain wildcard
266 // character '*', which matches prefix of target hostname. There are
267 // several restrictions to make wildcard work. The matching algorithm
268 // is based on RFC 6125.
269 bool tls_hostname_match(const StringRef &pattern, const StringRef &hostname);
271 // Caches |session|. |session| is serialized into ASN1
272 // representation, and stored. |t| is used as a time stamp.
273 // Depending on the existing cache's time stamp, |session| might not
275 void try_cache_tls_session(TLSSessionCache *cache, SSL_SESSION *session,
278 // Returns cached session associated |addr|. If no cache entry is
279 // found associated to |addr|, nullptr will be returned.
280 SSL_SESSION *reuse_tls_session(const TLSSessionCache &addr);
282 // Loads certificate form file |filename|. The caller should delete
283 // the returned object using X509_free().
284 X509 *load_certificate(const char *filename);
286 // Returns TLS version from |v|. The returned value is defined in
287 // OpenSSL header file. This function returns -1 if |v| is not valid
288 // TLS version string.
289 int proto_version_from_string(const StringRef &v);
291 // Verifies OCSP response |ocsp_resp| of length |ocsp_resplen|. This
292 // function returns 0 if it succeeds, or -1.
293 int verify_ocsp_response(SSL_CTX *ssl_ctx, const uint8_t *ocsp_resp,
294 size_t ocsp_resplen);
296 // Stores fingerprint of |x| in |dst| of length |dstlen|. |md|
297 // specifies hash function to use, and |dstlen| must be large enough
298 // to include hash value (e.g., 32 bytes for SHA-256). This function
299 // returns the number of bytes written in |dst|, or -1.
300 ssize_t get_x509_fingerprint(uint8_t *dst, size_t dstlen, const X509 *x,
303 // Returns subject name of |x|. If this function fails to get subject
304 // name, it returns an empty string.
305 StringRef get_x509_subject_name(BlockAllocator &balloc, X509 *x);
307 // Returns issuer name of |x|. If this function fails to get issuer
308 // name, it returns an empty string.
309 StringRef get_x509_issuer_name(BlockAllocator &balloc, X509 *x);
311 // Returns serial number of |x|. If this function fails to get serial
312 // number, it returns an empty string. number
313 StringRef get_x509_serial(BlockAllocator &balloc, X509 *x);
315 // Fills NotBefore of |x| in |t|. This function returns 0 if it
317 int get_x509_not_before(time_t &t, X509 *x);
319 // Fills NotAfter of |x| in |t|. This function returns 0 if it
321 int get_x509_not_after(time_t &t, X509 *x);
327 #endif // SHRPX_TLS_H