blob: 84c39f508335bc6cc5f2db94381cb9b13ea7df28 [file] [log] [blame]
Andy Green2eedea92014-04-03 14:33:48 +08001/*
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
Andy Green0c512392014-10-17 08:47:51 +080053#ifdef LWS_OPENSSL_SUPPORT
Andy Green2eedea92014-04-03 14:33:48 +080054
55#if OPENSSL_VERSION_NUMBER >= 0x10002000L
Andy Green7df53c52014-10-22 15:37:28 +080056
57struct alpn_ctx {
58 unsigned char *data;
59 unsigned short len;
60};
61
62static int npn_cb(SSL *s, const unsigned char **data, unsigned int *len, void *arg)
Andy Green2eedea92014-04-03 14:33:48 +080063{
Andy Green7df53c52014-10-22 15:37:28 +080064 struct alpn_ctx *alpn_ctx = arg;
65
66 lwsl_info("%s\n", __func__);
67 *data = alpn_ctx->data;
68 *len = alpn_ctx->len;
69
70 return SSL_TLSEXT_ERR_OK;
71}
72
73static int alpn_cb(SSL *s, const unsigned char **out,
74 unsigned char *outlen, const unsigned char *in,
75 unsigned int inlen, void *arg)
76{
77 struct alpn_ctx *alpn_ctx = arg;
78
79 if (SSL_select_next_proto((unsigned char **)out, outlen,
80 alpn_ctx->data, alpn_ctx->len, in, inlen) !=
81 OPENSSL_NPN_NEGOTIATED)
82 return SSL_TLSEXT_ERR_NOACK;
83
84 return SSL_TLSEXT_ERR_OK;
Andy Green2eedea92014-04-03 14:33:48 +080085}
86#endif
87
88LWS_VISIBLE void
Andy Green4b85c1d2015-12-04 11:08:32 +080089lws_context_init_http2_ssl(struct lws_context *context)
Andy Green2eedea92014-04-03 14:33:48 +080090{
91#if OPENSSL_VERSION_NUMBER >= 0x10002000L
Andy Green7df53c52014-10-22 15:37:28 +080092 static struct alpn_ctx protos = { (unsigned char *)
93 "\x05h2-14"
94 "\x08http/1.1",
95 6 + 9 };
96
97 SSL_CTX_set_next_protos_advertised_cb(context->ssl_ctx, npn_cb, &protos);
98
Andy Green2eedea92014-04-03 14:33:48 +080099 // ALPN selection callback
Andy Green7df53c52014-10-22 15:37:28 +0800100 SSL_CTX_set_alpn_select_cb(context->ssl_ctx, alpn_cb, &protos);
Andy Green2eedea92014-04-03 14:33:48 +0800101 lwsl_notice(" HTTP2 / ALPN enabled\n");
102#else
Andy Greenbbbf07a2014-10-27 16:46:44 +0800103 lwsl_notice(
104 " HTTP2 / ALPN configured but not supported by OpenSSL 0x%x\n",
105 OPENSSL_VERSION_NUMBER);
Andy Green2eedea92014-04-03 14:33:48 +0800106#endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
107}
108
Andy Green4b85c1d2015-12-04 11:08:32 +0800109void lws_http2_configure_if_upgraded(struct lws *wsi)
Andy Green7df53c52014-10-22 15:37:28 +0800110{
111#if OPENSSL_VERSION_NUMBER >= 0x10002000L
112 struct allocated_headers *ah;
113 const unsigned char *name;
114 unsigned len;
115 const char *method = "alpn";
116
117 SSL_get0_alpn_selected(wsi->ssl, &name, &len);
118
119 if (!len) {
120 SSL_get0_next_proto_negotiated(wsi->ssl, &name, &len);
121 method = "npn";
122 }
123
Andy Greenbbbf07a2014-10-27 16:46:44 +0800124 if (!len) {
Andy Green7df53c52014-10-22 15:37:28 +0800125 lwsl_info("no npn/alpn upgrade\n");
Andy Greenbbbf07a2014-10-27 16:46:44 +0800126 return;
127 }
128
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 */
Andy Greenbbbf07a2014-10-27 16:46:44 +0800135
136 /* adopt the header info */
137
138 ah = wsi->u.hdr.ah;
139
Andy Green44c11612014-11-08 11:18:47 +0800140 lws_union_transition(wsi, LWS_CONNMODE_HTTP2_SERVING);
141 wsi->state = WSI_STATE_HTTP2_AWAIT_CLIENT_PREFACE;
Andy Greenbbbf07a2014-10-27 16:46:44 +0800142
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 */
Andy Green7df53c52014-10-22 15:37:28 +0800150#endif
151}
152
Andy Green0c512392014-10-17 08:47:51 +0800153#endif
Andy Green2eedea92014-04-03 14:33:48 +0800154#endif