ah: allow configurable ah hold timeout
[platform/upstream/libwebsockets.git] / lib / ssl-http2.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2014 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  *
21  * Some or all of this file is based on code from nghttp2, which has the
22  * following license.  Since it's more liberal than lws license, you're also
23  * at liberty to get the original code from
24  * https://github.com/tatsuhiro-t/nghttp2 under his liberal terms alone.
25  *
26  * nghttp2 - HTTP/2.0 C Library
27  *
28  * Copyright (c) 2012 Tatsuhiro Tsujikawa
29  *
30  * Permission is hereby granted, free of charge, to any person obtaining
31  * a copy of this software and associated documentation files (the
32  * "Software"), to deal in the Software without restriction, including
33  * without limitation the rights to use, copy, modify, merge, publish,
34  * distribute, sublicense, and/or sell copies of the Software, and to
35  * permit persons to whom the Software is furnished to do so, subject to
36  * the following conditions:
37  *
38  * The above copyright notice and this permission notice shall be
39  * included in all copies or substantial portions of the Software.
40  *
41  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
42  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
43  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
44  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
45  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
46  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
47  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
48  */
49
50 #include "private-libwebsockets.h"
51
52 #ifndef LWS_NO_SERVER
53 #ifdef LWS_OPENSSL_SUPPORT
54
55 #if OPENSSL_VERSION_NUMBER >= 0x10002000L
56
57 struct alpn_ctx {
58         unsigned char *data;
59         unsigned short len;
60 };
61
62 static int
63 npn_cb(SSL *s, const unsigned char **data, unsigned int *len, void *arg)
64 {
65         struct alpn_ctx *alpn_ctx = arg;
66
67         lwsl_info("%s\n", __func__);
68         *data = alpn_ctx->data;
69         *len = alpn_ctx->len;
70
71         return SSL_TLSEXT_ERR_OK;
72 }
73
74 static int
75 alpn_cb(SSL *s, const unsigned char **out, unsigned char *outlen,
76         const unsigned char *in, unsigned int inlen, void *arg)
77 {
78         struct alpn_ctx *alpn_ctx = arg;
79
80         if (SSL_select_next_proto((unsigned char **)out, outlen, alpn_ctx->data,
81                                   alpn_ctx->len, in, inlen) !=
82             OPENSSL_NPN_NEGOTIATED)
83                 return SSL_TLSEXT_ERR_NOACK;
84
85         return SSL_TLSEXT_ERR_OK;
86 }
87 #endif
88
89 LWS_VISIBLE void
90 lws_context_init_http2_ssl(struct lws_vhost *vhost)
91 {
92 #if OPENSSL_VERSION_NUMBER >= 0x10002000L
93         static struct alpn_ctx protos = { (unsigned char *)"\x02h2"
94                                           "\x08http/1.1", 6 + 9 };
95
96         SSL_CTX_set_next_protos_advertised_cb(vhost->ssl_ctx, npn_cb, &protos);
97
98         // ALPN selection callback
99         SSL_CTX_set_alpn_select_cb(vhost->ssl_ctx, alpn_cb, &protos);
100         lwsl_notice(" HTTP2 / ALPN enabled\n");
101 #else
102         lwsl_notice(
103                 " HTTP2 / ALPN configured but not supported by OpenSSL 0x%lx\n",
104                     OPENSSL_VERSION_NUMBER);
105 #endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
106 }
107
108 void lws_http2_configure_if_upgraded(struct lws *wsi)
109 {
110 #if OPENSSL_VERSION_NUMBER >= 0x10002000L
111         struct allocated_headers *ah;
112         const char *method = "alpn";
113         const unsigned char *name;
114         unsigned len;
115
116         SSL_get0_alpn_selected(wsi->ssl, &name, &len);
117
118         if (!len) {
119                 SSL_get0_next_proto_negotiated(wsi->ssl, &name, &len);
120                 method = "npn";
121         }
122
123         if (!len) {
124                 lwsl_info("no npn/alpn upgrade\n");
125                 return;
126         }
127
128         (void)method;
129         lwsl_info("negotiated %s using %s\n", name, method);
130         wsi->use_ssl = 1;
131         if (strncmp((char *)name, "http/1.1", 8) == 0)
132                 return;
133
134         /* http2 */
135
136         /* adopt the header info */
137
138         ah = wsi->u.hdr.ah;
139
140         lws_union_transition(wsi, LWSCM_HTTP2_SERVING);
141         wsi->state = LWSS_HTTP2_AWAIT_CLIENT_PREFACE;
142
143         /* http2 union member has http union struct at start */
144         wsi->u.http.ah = ah;
145
146         lws_http2_init(&wsi->u.http2.peer_settings);
147         lws_http2_init(&wsi->u.http2.my_settings);
148
149         /* HTTP2 union */
150 #endif
151 }
152
153 #endif
154 #endif