2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2014 Andy Green <andy@warmcat.com>
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.
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.
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,
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.
26 * nghttp2 - HTTP/2.0 C Library
28 * Copyright (c) 2012 Tatsuhiro Tsujikawa
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:
38 * The above copyright notice and this permission notice shall be
39 * included in all copies or substantial portions of the Software.
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.
50 #include "private-libwebsockets.h"
53 #ifdef LWS_OPENSSL_SUPPORT
55 #if OPENSSL_VERSION_NUMBER >= 0x10002000L
63 npn_cb(SSL *s, const unsigned char **data, unsigned int *len, void *arg)
65 struct alpn_ctx *alpn_ctx = arg;
67 lwsl_info("%s\n", __func__);
68 *data = alpn_ctx->data;
71 return SSL_TLSEXT_ERR_OK;
75 alpn_cb(SSL *s, const unsigned char **out, unsigned char *outlen,
76 const unsigned char *in, unsigned int inlen, void *arg)
78 struct alpn_ctx *alpn_ctx = arg;
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;
85 return SSL_TLSEXT_ERR_OK;
90 lws_context_init_http2_ssl(struct lws_vhost *vhost)
92 #if OPENSSL_VERSION_NUMBER >= 0x10002000L
93 static struct alpn_ctx protos = { (unsigned char *)"\x02h2"
94 "\x08http/1.1", 6 + 9 };
96 SSL_CTX_set_next_protos_advertised_cb(vhost->ssl_ctx, npn_cb, &protos);
98 // ALPN selection callback
99 SSL_CTX_set_alpn_select_cb(vhost->ssl_ctx, alpn_cb, &protos);
100 lwsl_notice(" HTTP2 / ALPN enabled\n");
103 " HTTP2 / ALPN configured but not supported by OpenSSL 0x%lx\n",
104 OPENSSL_VERSION_NUMBER);
105 #endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
108 void lws_http2_configure_if_upgraded(struct lws *wsi)
110 #if OPENSSL_VERSION_NUMBER >= 0x10002000L
111 struct allocated_headers *ah;
112 const char *method = "alpn";
113 const unsigned char *name;
116 SSL_get0_alpn_selected(wsi->ssl, &name, &len);
119 SSL_get0_next_proto_negotiated(wsi->ssl, &name, &len);
124 lwsl_info("no npn/alpn upgrade\n");
129 lwsl_info("negotiated %s using %s\n", name, method);
131 if (strncmp((char *)name, "http/1.1", 8) == 0)
136 /* adopt the header info */
140 lws_union_transition(wsi, LWSCM_HTTP2_SERVING);
141 wsi->state = LWSS_HTTP2_AWAIT_CLIENT_PREFACE;
143 /* http2 union member has http union struct at start */
146 lws_http2_init(&wsi->u.http2.peer_settings);
147 lws_http2_init(&wsi->u.http2.my_settings);