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>
42 class DownstreamConnectionPool;
46 // This struct stores the additional information per SSL_CTX. This is
47 // attached to SSL_CTX using SSL_CTX_set_app_data().
48 struct TLSContextData {
49 // Protects ocsp_data;
52 std::vector<uint8_t> ocsp_data;
54 // Path to certificate file
55 const char *cert_file;
58 // Create server side SSL_CTX
59 SSL_CTX *create_ssl_context(const char *private_key_file,
60 const char *cert_file);
62 // Create client side SSL_CTX
63 SSL_CTX *create_ssl_client_context();
65 ClientHandler *accept_connection(Worker *worker, int fd, sockaddr *addr,
68 // Check peer's certificate against first downstream address in
69 // Config::downstream_addrs. We only consider first downstream since
70 // we use this function for HTTP/2 downstream link only.
71 int check_cert(SSL *ssl);
73 // Retrieves DNS and IP address in subjectAltNames and commonName from
75 void get_altnames(X509 *cert, std::vector<std::string> &dns_names,
76 std::vector<std::string> &ip_addrs, std::string &common_name);
78 // CertLookupTree forms lookup tree to get SSL_CTX whose DNS or
79 // commonName matches hostname in query. The tree is patricia trie
80 // data structure formed from the tail of the hostname pattern. Each
81 // CertNode contains part of hostname str member in range [first,
82 // last) member and the next member contains the following CertNode
83 // pointers ('following' means character before the current one). The
84 // CertNode where a hostname pattern ends contains its SSL_CTX pointer
85 // in the ssl_ctx member. For wildcard hostname pattern, we store the
86 // its pattern and SSL_CTX in CertNode one before first "*" found from
89 // When querying SSL_CTX with particular hostname, we match from its
90 // tail in our lookup tree. If the query goes to the first character
91 // of the hostname and current CertNode has non-NULL ssl_ctx member,
92 // then it is the exact match. The ssl_ctx member is returned. Along
93 // the way, if CertNode which contains non-empty wildcard_certs member
94 // is encountered, wildcard hostname matching is performed against
95 // them. If there is a match, its SSL_CTX is returned. If none
96 // matches, query is continued to the next character.
99 // list of wildcard domain name and its SSL_CTX pair, the wildcard
100 // '*' appears in this position.
101 std::vector<std::pair<char *, SSL_CTX *>> wildcard_certs;
102 // Next CertNode index of CertLookupTree::nodes
103 std::vector<std::unique_ptr<CertNode>> next;
104 // SSL_CTX for exact match
107 // [first, last) in the reverse direction in str, first >=
108 // last. This indices only work for str member.
112 class CertLookupTree {
116 // Adds |ssl_ctx| with hostname pattern |hostname| with length |len|
117 // to the lookup tree. The |hostname| must be NULL-terminated.
118 void add_cert(SSL_CTX *ssl_ctx, const char *hostname, size_t len);
120 // Looks up SSL_CTX using the given |hostname| with length |len|.
121 // If more than one SSL_CTX which matches the query, it is undefined
122 // which one is returned. The |hostname| must be NULL-terminated.
123 // If no matching SSL_CTX found, returns NULL.
124 SSL_CTX *lookup(const char *hostname, size_t len);
128 // Stores pointers to copied hostname when adding hostname and
130 std::vector<std::unique_ptr<char[]>> hosts_;
133 // Adds |ssl_ctx| to lookup tree |lt| using hostnames read from
134 // |certfile|. The subjectAltNames and commonName are considered as
135 // eligible hostname. This function returns 0 if it succeeds, or -1.
136 // Even if no ssl_ctx is added to tree, this function returns 0.
137 int cert_lookup_tree_add_cert_from_file(CertLookupTree *lt, SSL_CTX *ssl_ctx,
138 const char *certfile);
140 // Returns true if |needle| which has |len| bytes is included in the
141 // protocol list |protos|.
142 bool in_proto_list(const std::vector<char *> &protos,
143 const unsigned char *needle, size_t len);
145 // Returns true if security requirement for HTTP/2 is fulfilled.
146 bool check_http2_requirement(SSL *ssl);
148 // Returns SSL/TLS option mask to disable SSL/TLS protocol version not
149 // included in |tls_proto_list|. The returned mask can be directly
150 // passed to SSL_CTX_set_options().
151 long int create_tls_proto_mask(const std::vector<char *> &tls_proto_list);
153 std::vector<unsigned char> set_alpn_prefs(const std::vector<char *> &protos);
155 // Setups server side SSL_CTX. This function inspects get_config()
156 // and if upstream_no_tls is true, returns nullptr. Otherwise
157 // construct default SSL_CTX. If subcerts are available
158 // (get_config()->subcerts), caller should provide CertLookupTree
159 // object as |cert_tree| parameter, otherwise SNI does not work. All
160 // the created SSL_CTX is stored into |all_ssl_ctx|.
161 SSL_CTX *setup_server_ssl_context(std::vector<SSL_CTX *> &all_ssl_ctx,
162 CertLookupTree *cert_tree);
164 // Setups client side SSL_CTX. This function inspects get_config()
165 // and if downstream_no_tls is true, returns nullptr. Otherwise, only
166 // construct SSL_CTX if either client_mode or http2_bridge is true.
167 SSL_CTX *setup_client_ssl_context();
169 // Creates CertLookupTree. If frontend is configured not to use TLS,
170 // this function returns nullptr.
171 CertLookupTree *create_cert_lookup_tree();
177 #endif // SHRPX_SSL_H