Implemented fixes allowing libwebsockets to be built under Windows using MinGM/MSYS
[platform/upstream/libwebsockets.git] / lib / ssl.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
22 #include "private-libwebsockets.h"
23 #ifndef USE_WOLFSSL
24  #include <openssl/err.h>
25 #endif
26
27 int openssl_websocket_private_data_index;
28
29 static int lws_context_init_ssl_pem_passwd_cb(char * buf, int size, int rwflag, void *userdata)
30 {
31         struct lws_context_creation_info * info = (struct lws_context_creation_info *)userdata;
32
33         strncpy(buf, info->ssl_private_key_password, size);
34         buf[size - 1] = '\0';
35
36         return strlen(buf);
37 }
38
39 static void lws_ssl_bind_passphrase(SSL_CTX *ssl_ctx,
40                                     struct lws_context_creation_info *info)
41 {
42         if (!info->ssl_private_key_password)
43                 return;
44         /*
45          * password provided, set ssl callback and user data
46          * for checking password which will be trigered during
47          * SSL_CTX_use_PrivateKey_file function
48          */
49         SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, (void *)info);
50         SSL_CTX_set_default_passwd_cb(ssl_ctx,
51                                       lws_context_init_ssl_pem_passwd_cb);
52 }
53
54 #ifndef LWS_NO_SERVER
55 static int
56 OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
57 {
58         SSL *ssl;
59         int n;
60         struct libwebsocket_context *context;
61
62         ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
63                 SSL_get_ex_data_X509_STORE_CTX_idx());
64
65         /*
66          * !!! nasty openssl requires the index to come as a library-scope
67          * static
68          */
69         context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
70
71         n = context->protocols[0].callback(NULL, NULL,
72                 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
73                                                    x509_ctx, ssl, preverify_ok);
74
75         /* convert return code from 0 = OK to 1 = OK */
76         return !n;
77 }
78
79 LWS_VISIBLE int
80 lws_context_init_server_ssl(struct lws_context_creation_info *info,
81                      struct libwebsocket_context *context)
82 {
83         SSL_METHOD *method;
84         int error;
85         int n;
86
87         if (info->port != CONTEXT_PORT_NO_LISTEN) {
88
89                 context->use_ssl = info->ssl_cert_filepath != NULL;
90
91 #ifdef USE_WOLFSSL
92 #ifdef USE_OLD_CYASSL
93                 lwsl_notice(" Compiled with CyaSSL support\n");
94 #else
95                 lwsl_notice(" Compiled with wolfSSL support\n");
96 #endif
97 #else
98                 lwsl_notice(" Compiled with OpenSSL support\n");
99 #endif
100                 
101                 if (info->ssl_cipher_list)
102                         lwsl_notice(" SSL ciphers: '%s'\n", info->ssl_cipher_list);
103
104                 if (context->use_ssl)
105                         lwsl_notice(" Using SSL mode\n");
106                 else
107                         lwsl_notice(" Using non-SSL mode\n");
108         }
109
110         /* basic openssl init */
111
112         SSL_library_init();
113
114         OpenSSL_add_all_algorithms();
115         SSL_load_error_strings();
116
117         openssl_websocket_private_data_index =
118                 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
119
120         /*
121          * Firefox insists on SSLv23 not SSLv3
122          * Konq disables SSLv2 by default now, SSLv23 works
123          *
124          * SSLv23_server_method() is the openssl method for "allow all TLS
125          * versions", compared to e.g. TLSv1_2_server_method() which only allows
126          * tlsv1.2. Unwanted versions must be disabled using SSL_CTX_set_options()
127          */
128
129         method = (SSL_METHOD *)SSLv23_server_method();
130         if (!method) {
131                 error = ERR_get_error();
132                 lwsl_err("problem creating ssl method %lu: %s\n", 
133                         error, ERR_error_string(error,
134                                               (char *)context->service_buffer));
135                 return 1;
136         }
137         context->ssl_ctx = SSL_CTX_new(method); /* create context */
138         if (!context->ssl_ctx) {
139                 error = ERR_get_error();
140                 lwsl_err("problem creating ssl context %lu: %s\n",
141                         error, ERR_error_string(error,
142                                               (char *)context->service_buffer));
143                 return 1;
144         }
145
146         /* Disable SSLv2 and SSLv3 */
147         SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
148 #ifdef SSL_OP_NO_COMPRESSION
149         SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
150 #endif
151         SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
152         if (info->ssl_cipher_list)
153                 SSL_CTX_set_cipher_list(context->ssl_ctx,
154                                                 info->ssl_cipher_list);
155
156         /* as a server, are we requiring clients to identify themselves? */
157
158         if (info->options &
159                         LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
160                 
161                 int verify_options = SSL_VERIFY_PEER;
162         
163                 if (!(info->options & LWS_SERVER_OPTION_PEER_CERT_NOT_REQUIRED))
164                         verify_options |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
165                 
166                 SSL_CTX_set_session_id_context(context->ssl_ctx,
167                                 (unsigned char *)context, sizeof(void *));
168
169                 /* absolutely require the client cert */
170
171                 SSL_CTX_set_verify(context->ssl_ctx,
172                        verify_options, OpenSSL_verify_callback);
173
174                 /*
175                  * give user code a chance to load certs into the server
176                  * allowing it to verify incoming client certs
177                  */
178
179                 context->protocols[0].callback(context, NULL,
180                         LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
181                                                      context->ssl_ctx, NULL, 0);
182         }
183
184         if (info->options & LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT) {
185                 /* Normally SSL listener rejects non-ssl, optionally allow */
186                 context->allow_non_ssl_on_ssl_port = 1;
187         }
188
189         if (context->use_ssl) {
190
191                 /* openssl init for server sockets */
192
193                 /* set the local certificate from CertFile */
194                 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
195                                         info->ssl_cert_filepath);
196                 if (n != 1) {
197                         error = ERR_get_error();
198                         lwsl_err("problem getting cert '%s' %lu: %s\n",
199                                 info->ssl_cert_filepath,
200                                 error,
201                                 ERR_error_string(error,
202                                               (char *)context->service_buffer));
203                         return 1;
204                 }
205                 lws_ssl_bind_passphrase(context->ssl_ctx, info);
206
207                 if (info->ssl_private_key_filepath != NULL) {
208                         /* set the private key from KeyFile */
209                         if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
210                                      info->ssl_private_key_filepath,
211                                                        SSL_FILETYPE_PEM) != 1) {
212                                 error = ERR_get_error();
213                                 lwsl_err("ssl problem getting key '%s' %lu: %s\n",
214                                         info->ssl_private_key_filepath,
215                                                 error,
216                                                 ERR_error_string(error,
217                                                       (char *)context->service_buffer));
218                                 return 1;
219                         }
220                 }
221                 else {
222                         if (context->protocols[0].callback(context, NULL,
223                                 LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY,
224                                                 context->ssl_ctx, NULL, 0)) {
225                                 lwsl_err("ssl private key not set\n");
226                                 return 1;
227                         }
228                 }
229
230                 /* verify private key */
231                 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
232                         lwsl_err("Private SSL key doesn't match cert\n");
233                         return 1;
234                 }
235
236                 /*
237                  * SSL is happy and has a cert it's content with
238                  * If we're supporting HTTP2, initialize that
239                  */
240                 
241                 lws_context_init_http2_ssl(context);
242         }
243         
244         return 0;
245 }
246 #endif
247
248 LWS_VISIBLE void
249 lws_ssl_destroy(struct libwebsocket_context *context)
250 {
251         if (context->ssl_ctx)
252                 SSL_CTX_free(context->ssl_ctx);
253         if (!context->user_supplied_ssl_ctx && context->ssl_client_ctx)
254                 SSL_CTX_free(context->ssl_client_ctx);
255
256 #if (OPENSSL_VERSION_NUMBER < 0x01000000) || defined(USE_WOLFSSL)
257         ERR_remove_state(0);
258 #else
259         ERR_remove_thread_state(NULL);
260 #endif
261         ERR_free_strings();
262         EVP_cleanup();
263         CRYPTO_cleanup_all_ex_data();
264 }
265
266 LWS_VISIBLE void
267 libwebsockets_decode_ssl_error(void)
268 {
269         char buf[256];
270         u_long err;
271
272         while ((err = ERR_get_error()) != 0) {
273                 ERR_error_string_n(err, buf, sizeof(buf));
274                 lwsl_err("*** %lu %s\n", err, buf);
275         }
276 }
277
278 #ifndef LWS_NO_CLIENT
279
280 int lws_context_init_client_ssl(struct lws_context_creation_info *info,
281                             struct libwebsocket_context *context)
282 {
283         int error;
284         int n;
285         SSL_METHOD *method;
286
287         if (info->provided_client_ssl_ctx) {
288                 /* use the provided OpenSSL context if given one */
289                 context->ssl_client_ctx = info->provided_client_ssl_ctx;
290                 /* nothing for lib to delete */
291                 context->user_supplied_ssl_ctx = 1;
292                 return 0;
293         }
294
295         if (info->port != CONTEXT_PORT_NO_LISTEN)
296                 return 0;
297
298         /* basic openssl init */
299
300         SSL_library_init();
301
302         OpenSSL_add_all_algorithms();
303         SSL_load_error_strings();
304
305         method = (SSL_METHOD *)SSLv23_client_method();
306         if (!method) {
307                 error = ERR_get_error();
308                 lwsl_err("problem creating ssl method %lu: %s\n",
309                         error, ERR_error_string(error,
310                                       (char *)context->service_buffer));
311                 return 1;
312         }
313         /* create context */
314         context->ssl_client_ctx = SSL_CTX_new(method);
315         if (!context->ssl_client_ctx) {
316                 error = ERR_get_error();
317                 lwsl_err("problem creating ssl context %lu: %s\n",
318                         error, ERR_error_string(error,
319                                       (char *)context->service_buffer));
320                 return 1;
321         }
322
323 #ifdef SSL_OP_NO_COMPRESSION
324         SSL_CTX_set_options(context->ssl_client_ctx,
325                                                  SSL_OP_NO_COMPRESSION);
326 #endif
327         SSL_CTX_set_options(context->ssl_client_ctx,
328                                        SSL_OP_CIPHER_SERVER_PREFERENCE);
329         if (info->ssl_cipher_list)
330                 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
331                                                 info->ssl_cipher_list);
332
333 #ifdef LWS_SSL_CLIENT_USE_OS_CA_CERTS
334         if (!(info->options & LWS_SERVER_OPTION_DISABLE_OS_CA_CERTS))
335                 /* loads OS default CA certs */
336                 SSL_CTX_set_default_verify_paths(context->ssl_client_ctx);
337 #endif
338
339         /* openssl init for cert verification (for client sockets) */
340         if (!info->ssl_ca_filepath) {
341                 if (!SSL_CTX_load_verify_locations(
342                         context->ssl_client_ctx, NULL,
343                                              LWS_OPENSSL_CLIENT_CERTS))
344                         lwsl_err(
345                             "Unable to load SSL Client certs from %s "
346                             "(set by --with-client-cert-dir= "
347                             "in configure) --  client ssl isn't "
348                             "going to work", LWS_OPENSSL_CLIENT_CERTS);
349         } else
350                 if (!SSL_CTX_load_verify_locations(
351                         context->ssl_client_ctx, info->ssl_ca_filepath,
352                                                           NULL))
353                         lwsl_err(
354                                 "Unable to load SSL Client certs "
355                                 "file from %s -- client ssl isn't "
356                                 "going to work", info->ssl_ca_filepath);
357                 else
358                         lwsl_info("loaded ssl_ca_filepath\n");
359
360         /*
361          * callback allowing user code to load extra verification certs
362          * helping the client to verify server identity
363          */
364
365         /* support for client-side certificate authentication */
366         if (info->ssl_cert_filepath) {
367                 n = SSL_CTX_use_certificate_chain_file(
368                         context->ssl_client_ctx,
369                                         info->ssl_cert_filepath);
370                 if (n != 1) {
371                         lwsl_err("problem getting cert '%s' %lu: %s\n",
372                                 info->ssl_cert_filepath,
373                                 ERR_get_error(),
374                                 ERR_error_string(ERR_get_error(),
375                                 (char *)context->service_buffer));
376                         return 1;
377                 }
378         } 
379         if (info->ssl_private_key_filepath) {
380                 lws_ssl_bind_passphrase(context->ssl_client_ctx, info);
381                 /* set the private key from KeyFile */
382                 if (SSL_CTX_use_PrivateKey_file(context->ssl_client_ctx,
383                     info->ssl_private_key_filepath, SSL_FILETYPE_PEM) != 1) {
384                         lwsl_err("use_PrivateKey_file '%s' %lu: %s\n",
385                                 info->ssl_private_key_filepath,
386                                 ERR_get_error(),
387                                 ERR_error_string(ERR_get_error(),
388                                       (char *)context->service_buffer));
389                         return 1;
390                 }
391
392                 /* verify private key */
393                 if (!SSL_CTX_check_private_key(
394                                         context->ssl_client_ctx)) {
395                         lwsl_err("Private SSL key doesn't match cert\n");
396                         return 1;
397                 }
398         } 
399
400         context->protocols[0].callback(context, NULL,
401                 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
402                 context->ssl_client_ctx, NULL, 0);
403         
404         return 0;
405 }
406 #endif
407
408 LWS_VISIBLE void
409 lws_ssl_remove_wsi_from_buffered_list(struct libwebsocket_context *context,
410                      struct libwebsocket *wsi)
411 {
412         if (!wsi->pending_read_list_prev &&
413             !wsi->pending_read_list_next &&
414             context->pending_read_list != wsi)
415                 /* we are not on the list */
416                 return;
417
418         /* point previous guy's next to our next */
419         if (!wsi->pending_read_list_prev)
420                 context->pending_read_list = wsi->pending_read_list_next;
421         else
422                 wsi->pending_read_list_prev->pending_read_list_next =
423                         wsi->pending_read_list_next;
424
425         /* point next guy's previous to our previous */
426         if (wsi->pending_read_list_next)
427                 wsi->pending_read_list_next->pending_read_list_prev =
428                         wsi->pending_read_list_prev;
429
430         wsi->pending_read_list_prev = NULL;
431         wsi->pending_read_list_next = NULL;
432 }
433
434 LWS_VISIBLE int
435 lws_ssl_capable_read(struct libwebsocket_context *context,
436                      struct libwebsocket *wsi, unsigned char *buf, int len)
437 {
438         int n;
439
440         if (!wsi->ssl)
441                 return lws_ssl_capable_read_no_ssl(context, wsi, buf, len);
442
443         n = SSL_read(wsi->ssl, buf, len);
444         if (n >= 0) {
445                 /* 
446                  * if it was our buffer that limited what we read,
447                  * check if SSL has additional data pending inside SSL buffers.
448                  * 
449                  * Because these won't signal at the network layer with POLLIN
450                  * and if we don't realize, this data will sit there forever
451                  */
452                 if (n == len && wsi->ssl && SSL_pending(wsi->ssl)) {
453                         if (!wsi->pending_read_list_next && !wsi->pending_read_list_prev) {
454                                 if (context->pending_read_list != wsi) {
455                                         /* add us to the linked list of guys with pending ssl */
456                                         if (context->pending_read_list)
457                                                 context->pending_read_list->pending_read_list_prev = wsi;
458                                         wsi->pending_read_list_next = context->pending_read_list;
459                                         wsi->pending_read_list_prev = NULL;
460                                         context->pending_read_list = wsi;
461                                 }
462                         }
463                 } else
464                         lws_ssl_remove_wsi_from_buffered_list(context, wsi);
465
466                 return n;
467         }
468         n = SSL_get_error(wsi->ssl, n);
469         if (n ==  SSL_ERROR_WANT_READ || n ==  SSL_ERROR_WANT_WRITE)
470                 return LWS_SSL_CAPABLE_MORE_SERVICE;
471
472         return LWS_SSL_CAPABLE_ERROR; 
473 }
474
475 LWS_VISIBLE int
476 lws_ssl_pending(struct libwebsocket *wsi)
477 {
478         if (!wsi->ssl)
479                 return 0;
480
481         return SSL_pending(wsi->ssl);
482 }
483
484 LWS_VISIBLE int
485 lws_ssl_capable_write(struct libwebsocket *wsi, unsigned char *buf, int len)
486 {
487         int n;
488
489         if (!wsi->ssl)
490                 return lws_ssl_capable_write_no_ssl(wsi, buf, len);
491         
492         n = SSL_write(wsi->ssl, buf, len);
493         if (n >= 0)
494                 return n;
495
496         n = SSL_get_error(wsi->ssl, n);
497         if (n == SSL_ERROR_WANT_READ || n == SSL_ERROR_WANT_WRITE) {
498                 if (n == SSL_ERROR_WANT_WRITE)
499                         lws_set_blocking_send(wsi);
500                 return LWS_SSL_CAPABLE_MORE_SERVICE;
501         }
502
503         return LWS_SSL_CAPABLE_ERROR;
504 }
505
506 LWS_VISIBLE int
507 lws_ssl_close(struct libwebsocket *wsi)
508 {
509         int n;
510
511         if (!wsi->ssl)
512                 return 0; /* not handled */
513
514         n = SSL_get_fd(wsi->ssl);
515         SSL_shutdown(wsi->ssl);
516         compatible_close(n);
517         SSL_free(wsi->ssl);
518
519         return 1; /* handled */
520 }
521
522 LWS_VISIBLE int
523 lws_server_socket_service_ssl(struct libwebsocket_context *context,
524                 struct libwebsocket **pwsi, struct libwebsocket *new_wsi,
525                         int accept_fd, struct libwebsocket_pollfd *pollfd)
526 {
527         int n, m;
528         struct libwebsocket *wsi = *pwsi;
529 #ifndef USE_WOLFSSL
530         BIO *bio;
531 #endif
532
533         if (!LWS_SSL_ENABLED(context))
534                 return 0;
535
536         switch (wsi->mode) {
537         case LWS_CONNMODE_SERVER_LISTENER:
538
539                 if (!new_wsi) {
540                         lwsl_err("no new_wsi\n");
541                         return 0;
542                 }
543
544                 new_wsi->ssl = SSL_new(context->ssl_ctx);
545                 if (new_wsi->ssl == NULL) {
546                         lwsl_err("SSL_new failed: %s\n",
547                                         ERR_error_string(SSL_get_error(new_wsi->ssl, 0), NULL));
548                         libwebsockets_decode_ssl_error();
549
550                         // TODO: Shouldn't the caller handle this?
551                         compatible_close(accept_fd);
552                         goto fail;
553                 }
554
555                 SSL_set_ex_data(new_wsi->ssl,
556                         openssl_websocket_private_data_index, context);
557
558                 SSL_set_fd(new_wsi->ssl, accept_fd);
559
560 #ifdef USE_WOLFSSL
561 #ifdef USE_OLD_CYASSL
562                 CyaSSL_set_using_nonblock(new_wsi->ssl, 1);
563 #else
564                 wolfSSL_set_using_nonblock(new_wsi->ssl, 1);
565 #endif
566 #else
567                 SSL_set_mode(new_wsi->ssl, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
568                 bio = SSL_get_rbio(new_wsi->ssl);
569                 if (bio)
570                         BIO_set_nbio(bio, 1); /* nonblocking */
571                 else
572                         lwsl_notice("NULL rbio\n");
573                 bio = SSL_get_wbio(new_wsi->ssl);
574                 if (bio)
575                         BIO_set_nbio(bio, 1); /* nonblocking */
576                 else
577                         lwsl_notice("NULL rbio\n");
578 #endif
579
580                 /*
581                  * we are not accepted yet, but we need to enter ourselves
582                  * as a live connection.  That way we can retry when more
583                  * pieces come if we're not sorted yet
584                  */
585
586                 *pwsi = new_wsi;
587                 wsi = *pwsi;
588                 wsi->mode = LWS_CONNMODE_SSL_ACK_PENDING;
589                 if (insert_wsi_socket_into_fds(context, wsi))
590                         goto fail;
591
592                 libwebsocket_set_timeout(wsi, PENDING_TIMEOUT_SSL_ACCEPT,
593                                                         AWAITING_TIMEOUT);
594
595                 lwsl_info("inserted SSL accept into fds, trying SSL_accept\n");
596
597                 /* fallthru */
598
599         case LWS_CONNMODE_SSL_ACK_PENDING:
600
601                 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0))
602                         goto fail;
603
604                 lws_libev_io(context, wsi, LWS_EV_STOP | LWS_EV_WRITE);
605
606                 lws_latency_pre(context, wsi);
607
608                 n = recv(wsi->sock, (char *)context->service_buffer,
609                         sizeof(context->service_buffer), MSG_PEEK);
610
611                 /*
612                  * optionally allow non-SSL connect on SSL listening socket
613                  * This is disabled by default, if enabled it goes around any
614                  * SSL-level access control (eg, client-side certs) so leave
615                  * it disabled unless you know it's not a problem for you
616                  */
617
618                 if (context->allow_non_ssl_on_ssl_port) {
619                         if (n >= 1 && context->service_buffer[0] >= ' ') {
620                                 /*
621                                 * TLS content-type for Handshake is 0x16, and
622                                 * for ChangeCipherSpec Record, it's 0x14
623                                 *
624                                 * A non-ssl session will start with the HTTP
625                                 * method in ASCII.  If we see it's not a legit
626                                 * SSL handshake kill the SSL for this
627                                 * connection and try to handle as a HTTP
628                                 * connection upgrade directly.
629                                 */
630                                 wsi->use_ssl = 0;
631                                 SSL_shutdown(wsi->ssl);
632                                 SSL_free(wsi->ssl);
633                                 wsi->ssl = NULL;
634                                 goto accepted;
635                         }
636                         if (n == 0 || LWS_ERRNO == LWS_EAGAIN ||
637                             LWS_ERRNO == LWS_EWOULDBLOCK) {
638                                 /*
639                                  * well, we get no way to know ssl or not
640                                  * so go around again waiting for something
641                                  * to come and give us a hint, or timeout the
642                                  * connection.
643                                  */
644                                 m = SSL_ERROR_WANT_READ;
645                                 goto go_again;
646                         }
647                 }
648
649                 /* normal SSL connection processing path */
650
651                 n = SSL_accept(wsi->ssl);
652                 lws_latency(context, wsi,
653                         "SSL_accept LWS_CONNMODE_SSL_ACK_PENDING\n", n, n == 1);
654
655                 if (n == 1)
656                         goto accepted;
657
658                 m = SSL_get_error(wsi->ssl, n);
659                 lwsl_debug("SSL_accept failed %d / %s\n",
660                                                   m, ERR_error_string(m, NULL));
661 go_again:
662                 if (m == SSL_ERROR_WANT_READ) {
663                         if (lws_change_pollfd(wsi, 0, LWS_POLLIN))
664                                 goto fail;
665
666                         lws_libev_io(context, wsi, LWS_EV_START | LWS_EV_READ);
667
668                         lwsl_info("SSL_ERROR_WANT_READ\n");
669                         break;
670                 }
671                 if (m == SSL_ERROR_WANT_WRITE) {
672                         if (lws_change_pollfd(wsi, 0, LWS_POLLOUT))
673                                 goto fail;
674
675                         lws_libev_io(context, wsi, LWS_EV_START | LWS_EV_WRITE);
676                         break;
677                 }
678                 lwsl_debug("SSL_accept failed skt %u: %s\n",
679                                          pollfd->fd, ERR_error_string(m, NULL));
680                 goto fail;
681
682 accepted:
683                 /* OK, we are accepted... give him some time to negotiate */
684                 libwebsocket_set_timeout(wsi,
685                         PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
686                                                         AWAITING_TIMEOUT);
687
688                 wsi->mode = LWS_CONNMODE_HTTP_SERVING;
689
690                 lws_http2_configure_if_upgraded(wsi);
691
692                 lwsl_debug("accepted new SSL conn\n");
693                 break;
694         }
695
696         return 0;
697         
698 fail:
699         return 1;
700 }
701
702 LWS_VISIBLE void
703 lws_ssl_context_destroy(struct libwebsocket_context *context)
704 {
705         if (context->ssl_ctx)
706                 SSL_CTX_free(context->ssl_ctx);
707         if (!context->user_supplied_ssl_ctx && context->ssl_client_ctx)
708                 SSL_CTX_free(context->ssl_client_ctx);
709
710 #if (OPENSSL_VERSION_NUMBER < 0x01000000) || defined(USE_WOLFSSL)
711         ERR_remove_state(0);
712 #else
713         ERR_remove_thread_state(NULL);
714 #endif
715         ERR_free_strings();
716         EVP_cleanup();
717         CRYPTO_cleanup_all_ex_data();
718 }