Imported Upstream version 1.0.0
[platform/upstream/nghttp2.git] / src / shrpx_ssl.h
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2012 Tatsuhiro Tsujikawa
5  *
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:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
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.
24  */
25 #ifndef SHRPX_SSL_H
26 #define SHRPX_SSL_H
27
28 #include "shrpx.h"
29
30 #include <vector>
31 #include <mutex>
32
33 #include <openssl/ssl.h>
34 #include <openssl/err.h>
35
36 #include <ev.h>
37
38 namespace shrpx {
39
40 class ClientHandler;
41 class Worker;
42 class DownstreamConnectionPool;
43
44 namespace ssl {
45
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;
50   std::mutex mu;
51   // OCSP resonse
52   std::vector<uint8_t> ocsp_data;
53
54   // Path to certificate file
55   const char *cert_file;
56 };
57
58 // Create server side SSL_CTX
59 SSL_CTX *create_ssl_context(const char *private_key_file,
60                             const char *cert_file);
61
62 // Create client side SSL_CTX
63 SSL_CTX *create_ssl_client_context();
64
65 ClientHandler *accept_connection(Worker *worker, int fd, sockaddr *addr,
66                                  int addrlen);
67
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);
72
73 // Retrieves DNS and IP address in subjectAltNames and commonName from
74 // the |cert|.
75 void get_altnames(X509 *cert, std::vector<std::string> &dns_names,
76                   std::vector<std::string> &ip_addrs, std::string &common_name);
77
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
87 // the tail.
88 //
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.
97
98 struct CertNode {
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
105   SSL_CTX *ssl_ctx;
106   char *str;
107   // [first, last) in the reverse direction in str, first >=
108   // last. This indices only work for str member.
109   int first, last;
110 };
111
112 class CertLookupTree {
113 public:
114   CertLookupTree();
115
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);
119
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);
125
126 private:
127   CertNode root_;
128   // Stores pointers to copied hostname when adding hostname and
129   // ssl_ctx pair.
130   std::vector<std::unique_ptr<char[]>> hosts_;
131 };
132
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);
139
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);
144
145 // Returns true if security requirement for HTTP/2 is fulfilled.
146 bool check_http2_requirement(SSL *ssl);
147
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);
152
153 std::vector<unsigned char> set_alpn_prefs(const std::vector<char *> &protos);
154
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);
163
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();
168
169 // Creates CertLookupTree.  If frontend is configured not to use TLS,
170 // this function returns nullptr.
171 CertLookupTree *create_cert_lookup_tree();
172
173 } // namespace ssl
174
175 } // namespace shrpx
176
177 #endif // SHRPX_SSL_H