Andy Green | 2eedea9 | 2014-04-03 14:33:48 +0800 | [diff] [blame] | 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 |
Andy Green | 0c51239 | 2014-10-17 08:47:51 +0800 | [diff] [blame] | 53 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 2eedea9 | 2014-04-03 14:33:48 +0800 | [diff] [blame] | 54 | |
| 55 | #if OPENSSL_VERSION_NUMBER >= 0x10002000L |
Andy Green | 7df53c5 | 2014-10-22 15:37:28 +0800 | [diff] [blame] | 56 | |
| 57 | struct alpn_ctx { |
| 58 | unsigned char *data; |
| 59 | unsigned short len; |
| 60 | }; |
| 61 | |
| 62 | static int npn_cb(SSL *s, const unsigned char **data, unsigned int *len, void *arg) |
Andy Green | 2eedea9 | 2014-04-03 14:33:48 +0800 | [diff] [blame] | 63 | { |
Andy Green | 7df53c5 | 2014-10-22 15:37:28 +0800 | [diff] [blame] | 64 | 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 | |
| 73 | static 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 Green | 2eedea9 | 2014-04-03 14:33:48 +0800 | [diff] [blame] | 85 | } |
| 86 | #endif |
| 87 | |
| 88 | LWS_VISIBLE void |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame^] | 89 | lws_context_init_http2_ssl(struct lws_context *context) |
Andy Green | 2eedea9 | 2014-04-03 14:33:48 +0800 | [diff] [blame] | 90 | { |
| 91 | #if OPENSSL_VERSION_NUMBER >= 0x10002000L |
Andy Green | 7df53c5 | 2014-10-22 15:37:28 +0800 | [diff] [blame] | 92 | 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 Green | 2eedea9 | 2014-04-03 14:33:48 +0800 | [diff] [blame] | 99 | // ALPN selection callback |
Andy Green | 7df53c5 | 2014-10-22 15:37:28 +0800 | [diff] [blame] | 100 | SSL_CTX_set_alpn_select_cb(context->ssl_ctx, alpn_cb, &protos); |
Andy Green | 2eedea9 | 2014-04-03 14:33:48 +0800 | [diff] [blame] | 101 | lwsl_notice(" HTTP2 / ALPN enabled\n"); |
| 102 | #else |
Andy Green | bbbf07a | 2014-10-27 16:46:44 +0800 | [diff] [blame] | 103 | lwsl_notice( |
| 104 | " HTTP2 / ALPN configured but not supported by OpenSSL 0x%x\n", |
| 105 | OPENSSL_VERSION_NUMBER); |
Andy Green | 2eedea9 | 2014-04-03 14:33:48 +0800 | [diff] [blame] | 106 | #endif // OPENSSL_VERSION_NUMBER >= 0x10002000L |
| 107 | } |
| 108 | |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame^] | 109 | void lws_http2_configure_if_upgraded(struct lws *wsi) |
Andy Green | 7df53c5 | 2014-10-22 15:37:28 +0800 | [diff] [blame] | 110 | { |
| 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 Green | bbbf07a | 2014-10-27 16:46:44 +0800 | [diff] [blame] | 124 | if (!len) { |
Andy Green | 7df53c5 | 2014-10-22 15:37:28 +0800 | [diff] [blame] | 125 | lwsl_info("no npn/alpn upgrade\n"); |
Andy Green | bbbf07a | 2014-10-27 16:46:44 +0800 | [diff] [blame] | 126 | 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 Green | bbbf07a | 2014-10-27 16:46:44 +0800 | [diff] [blame] | 135 | |
| 136 | /* adopt the header info */ |
| 137 | |
| 138 | ah = wsi->u.hdr.ah; |
| 139 | |
Andy Green | 44c1161 | 2014-11-08 11:18:47 +0800 | [diff] [blame] | 140 | lws_union_transition(wsi, LWS_CONNMODE_HTTP2_SERVING); |
| 141 | wsi->state = WSI_STATE_HTTP2_AWAIT_CLIENT_PREFACE; |
Andy Green | bbbf07a | 2014-10-27 16:46:44 +0800 | [diff] [blame] | 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 */ |
Andy Green | 7df53c5 | 2014-10-22 15:37:28 +0800 | [diff] [blame] | 150 | #endif |
| 151 | } |
| 152 | |
Andy Green | 0c51239 | 2014-10-17 08:47:51 +0800 | [diff] [blame] | 153 | #endif |
Andy Green | 2eedea9 | 2014-04-03 14:33:48 +0800 | [diff] [blame] | 154 | #endif |