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,
22 #include "private-libwebsockets.h"
24 extern int openssl_websocket_private_data_index,
25 openssl_SSL_CTX_private_data_index;
28 lws_ssl_bind_passphrase(SSL_CTX *ssl_ctx, struct lws_context_creation_info *info);
31 OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
38 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
39 SSL_get_ex_data_X509_STORE_CTX_idx());
42 * !!! nasty openssl requires the index to come as a library-scope
45 vh = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
48 * give him a fake wsi with context set, so he can use lws_get_context()
51 memset(&wsi, 0, sizeof(wsi));
53 wsi.context = vh->context;
55 n = vh->protocols[0].callback(&wsi,
56 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
57 x509_ctx, ssl, preverify_ok);
59 /* convert return code from 0 = OK to 1 = OK */
64 lws_context_ssl_init_ecdh(struct lws_vhost *vhost)
66 #ifdef LWS_SSL_SERVER_WITH_ECDH_CERT
67 EC_KEY *EC_key = NULL;
72 if (!lws_check_opt(vhost->context->options, LWS_SERVER_OPTION_SSL_ECDH))
75 lwsl_notice(" Using ECDH certificate support\n");
77 /* Get X509 certificate from ssl context */
78 x = sk_X509_value(vhost->ssl_ctx->extra_certs, 0);
80 lwsl_err("%s: x is NULL\n", __func__);
83 /* Get the public key from certificate */
84 pkey = X509_get_pubkey(x);
86 lwsl_err("%s: pkey is NULL\n", __func__);
90 /* Get the key type */
91 KeyType = EVP_PKEY_type(pkey->type);
93 if (EVP_PKEY_EC != KeyType) {
94 lwsl_notice("Key type is not EC\n");
98 EC_key = EVP_PKEY_get1_EC_KEY(pkey);
99 /* Set ECDH parameter */
101 lwsl_err("%s: ECDH key is NULL \n", __func__);
104 SSL_CTX_set_tmp_ecdh(vhost->ssl_ctx, EC_key);
111 lws_context_ssl_init_ecdh_curve(struct lws_context_creation_info *info,
112 struct lws_vhost *vhost)
114 #ifdef LWS_HAVE_OPENSSL_ECDH_H
117 const char *ecdh_curve = "prime256v1";
119 if (info->ecdh_curve)
120 ecdh_curve = info->ecdh_curve;
122 ecdh_nid = OBJ_sn2nid(ecdh_curve);
123 if (NID_undef == ecdh_nid) {
124 lwsl_err("SSL: Unknown curve name '%s'", ecdh_curve);
128 ecdh = EC_KEY_new_by_curve_name(ecdh_nid);
130 lwsl_err("SSL: Unable to create curve '%s'", ecdh_curve);
133 SSL_CTX_set_tmp_ecdh(vhost->ssl_ctx, ecdh);
136 SSL_CTX_set_options(vhost->ssl_ctx, SSL_OP_SINGLE_ECDH_USE);
138 lwsl_notice(" SSL ECDH curve '%s'\n", ecdh_curve);
140 lwsl_notice(" OpenSSL doesn't support ECDH\n");
145 #ifndef OPENSSL_NO_TLSEXT
147 lws_ssl_server_name_cb(SSL *ssl, int *ad, void *arg)
149 struct lws_context *context;
150 struct lws_vhost *vhost, *vh;
151 const char *servername;
155 return SSL_TLSEXT_ERR_NOACK;
157 context = (struct lws_context *)SSL_CTX_get_ex_data(
158 SSL_get_SSL_CTX(ssl),
159 openssl_SSL_CTX_private_data_index);
162 * We can only get ssl accepted connections by using a vhost's ssl_ctx
163 * find out which listening one took us and only match vhosts on the
166 vh = context->vhost_list;
168 if (vh->ssl_ctx == SSL_get_SSL_CTX(ssl))
173 assert(vh); /* we cannot get an ssl without using a vhost ssl_ctx */
174 port = vh->listen_port;
176 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
179 vhost = lws_select_vhost(context, port, servername);
181 lwsl_debug("SNI: Found: %s (port %d)\n",
183 SSL_set_SSL_CTX(ssl, vhost->ssl_ctx);
184 return SSL_TLSEXT_ERR_OK;
186 lwsl_err("SNI: Unknown ServerName: %s\n", servername);
189 return SSL_TLSEXT_ERR_OK;
194 lws_context_init_server_ssl(struct lws_context_creation_info *info,
195 struct lws_vhost *vhost)
198 struct lws_context *context = vhost->context;
203 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT)) {
208 if (info->port != CONTEXT_PORT_NO_LISTEN) {
210 vhost->use_ssl = info->ssl_cert_filepath != NULL;
212 if (vhost->use_ssl && info->ssl_cipher_list)
213 lwsl_notice(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
216 lwsl_notice(" Using SSL mode\n");
218 lwsl_notice(" Using non-SSL mode\n");
222 * give him a fake wsi with context + vhost set, so he can use
223 * lws_get_context() in the callback
225 memset(&wsi, 0, sizeof(wsi));
227 wsi.context = vhost->context;
230 * Firefox insists on SSLv23 not SSLv3
231 * Konq disables SSLv2 by default now, SSLv23 works
233 * SSLv23_server_method() is the openssl method for "allow all TLS
234 * versions", compared to e.g. TLSv1_2_server_method() which only allows
235 * tlsv1.2. Unwanted versions must be disabled using SSL_CTX_set_options()
238 method = (SSL_METHOD *)SSLv23_server_method();
240 error = ERR_get_error();
241 lwsl_err("problem creating ssl method %lu: %s\n",
242 error, ERR_error_string(error,
243 (char *)context->pt[0].serv_buf));
246 vhost->ssl_ctx = SSL_CTX_new(method); /* create context */
247 if (!vhost->ssl_ctx) {
248 error = ERR_get_error();
249 lwsl_err("problem creating ssl context %lu: %s\n",
250 error, ERR_error_string(error,
251 (char *)context->pt[0].serv_buf));
255 /* associate the lws context with the SSL_CTX */
257 SSL_CTX_set_ex_data(vhost->ssl_ctx,
258 openssl_SSL_CTX_private_data_index, vhost->context);
260 /* Disable SSLv2 and SSLv3 */
261 SSL_CTX_set_options(vhost->ssl_ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
262 #ifdef SSL_OP_NO_COMPRESSION
263 SSL_CTX_set_options(vhost->ssl_ctx, SSL_OP_NO_COMPRESSION);
265 SSL_CTX_set_options(vhost->ssl_ctx, SSL_OP_SINGLE_DH_USE);
266 SSL_CTX_set_options(vhost->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
267 if (info->ssl_cipher_list)
268 SSL_CTX_set_cipher_list(vhost->ssl_ctx,
269 info->ssl_cipher_list);
271 /* as a server, are we requiring clients to identify themselves? */
273 if (lws_check_opt(info->options, LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT)) {
274 int verify_options = SSL_VERIFY_PEER;
276 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_PEER_CERT_NOT_REQUIRED))
277 verify_options |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
279 SSL_CTX_set_session_id_context(vhost->ssl_ctx,
280 (unsigned char *)context, sizeof(void *));
282 /* absolutely require the client cert */
284 SSL_CTX_set_verify(vhost->ssl_ctx,
285 verify_options, OpenSSL_verify_callback);
288 #ifndef OPENSSL_NO_TLSEXT
289 SSL_CTX_set_tlsext_servername_callback(vhost->ssl_ctx,
290 lws_ssl_server_name_cb);
294 * give user code a chance to load certs into the server
295 * allowing it to verify incoming client certs
298 if (info->ssl_ca_filepath &&
299 !SSL_CTX_load_verify_locations(vhost->ssl_ctx,
300 info->ssl_ca_filepath, NULL)) {
301 lwsl_err("%s: SSL_CTX_load_verify_locations unhappy\n", __func__);
304 if (vhost->use_ssl) {
305 if (lws_context_ssl_init_ecdh_curve(info, vhost))
308 vhost->protocols[0].callback(&wsi,
309 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
310 vhost->ssl_ctx, NULL, 0);
313 if (lws_check_opt(info->options, LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT))
314 /* Normally SSL listener rejects non-ssl, optionally allow */
315 vhost->allow_non_ssl_on_ssl_port = 1;
317 if (vhost->use_ssl) {
318 /* openssl init for server sockets */
320 /* set the local certificate from CertFile */
321 n = SSL_CTX_use_certificate_chain_file(vhost->ssl_ctx,
322 info->ssl_cert_filepath);
324 error = ERR_get_error();
325 lwsl_err("problem getting cert '%s' %lu: %s\n",
326 info->ssl_cert_filepath,
328 ERR_error_string(error,
329 (char *)context->pt[0].serv_buf));
332 lws_ssl_bind_passphrase(vhost->ssl_ctx, info);
334 if (info->ssl_private_key_filepath != NULL) {
335 /* set the private key from KeyFile */
336 if (SSL_CTX_use_PrivateKey_file(vhost->ssl_ctx,
337 info->ssl_private_key_filepath,
338 SSL_FILETYPE_PEM) != 1) {
339 error = ERR_get_error();
340 lwsl_err("ssl problem getting key '%s' %lu: %s\n",
341 info->ssl_private_key_filepath, error,
342 ERR_error_string(error,
343 (char *)context->pt[0].serv_buf));
347 if (vhost->protocols[0].callback(&wsi,
348 LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY,
349 vhost->ssl_ctx, NULL, 0)) {
350 lwsl_err("ssl private key not set\n");
355 /* verify private key */
356 if (!SSL_CTX_check_private_key(vhost->ssl_ctx)) {
357 lwsl_err("Private SSL key doesn't match cert\n");
361 if (lws_context_ssl_init_ecdh(vhost))
365 * SSL is happy and has a cert it's content with
366 * If we're supporting HTTP2, initialize that
369 lws_context_init_http2_ssl(context);