ssl: only apply restriction if nonzero for vhost
[platform/upstream/libwebsockets.git] / lib / ssl.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2017 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
24 int lws_alloc_vfs_file(struct lws_context *context, const char *filename, uint8_t **buf,
25                 lws_filepos_t *amount)
26 {
27         lws_filepos_t len;
28         lws_fop_flags_t flags = LWS_O_RDONLY;
29         lws_fop_fd_t fops_fd = lws_vfs_file_open(
30                                 lws_get_fops(context), filename, &flags);
31         int ret = 1;
32
33         if (!fops_fd)
34                 return 1;
35
36         len = lws_vfs_get_length(fops_fd);
37
38         *buf = malloc(len);
39         if (!buf)
40                 goto bail;
41
42         if (lws_vfs_file_read(fops_fd, amount, *buf, len))
43                 goto bail;
44
45         ret = 0;
46 bail:
47         lws_vfs_file_close(&fops_fd);
48
49         return ret;
50 }
51
52 #if defined(LWS_WITH_ESP32)
53 int alloc_file(struct lws_context *context, const char *filename, uint8_t **buf,
54                lws_filepos_t *amount)
55 {
56         nvs_handle nvh;
57         size_t s;
58         int n = 0;
59
60         ESP_ERROR_CHECK(nvs_open("lws-station", NVS_READWRITE, &nvh));
61         if (nvs_get_blob(nvh, filename, NULL, &s) != ESP_OK) {
62                 n = 1;
63                 goto bail;
64         }
65         *buf = malloc(s);
66         if (!*buf) {
67                 n = 2;
68                 goto bail;
69         }
70         if (nvs_get_blob(nvh, filename, (char *)*buf, &s) != ESP_OK)
71                 n = 1;
72
73         *amount = s;
74
75 bail:
76         nvs_close(nvh);
77
78         return n;
79 }
80 #endif
81
82 int openssl_websocket_private_data_index,
83     openssl_SSL_CTX_private_data_index;
84
85 int lws_ssl_get_error(struct lws *wsi, int n)
86 {
87         if (!wsi->ssl)
88                 return 99;
89         lwsl_debug("%s: %p %d\n", __func__, wsi->ssl, n);
90         return SSL_get_error(wsi->ssl, n);
91 }
92
93 /* Copies a string describing the code returned by lws_ssl_get_error(),
94  * which may also contain system error information in the case of SSL_ERROR_SYSCALL,
95  * into buf up to len.
96  * Returns a pointer to buf.
97  *
98  * Note: the lws_ssl_get_error() code is *not* an error code that can be passed
99  * to ERR_error_string(),
100  *
101  * ret is the return value originally passed to lws_ssl_get_error(), needed to disambiguate
102  * SYS_ERROR_SYSCALL.
103  *
104  * See man page for SSL_get_error().
105  *
106  * Not thread safe, uses strerror()
107  */
108 char* lws_ssl_get_error_string(int status, int ret, char *buf, size_t len) {
109         switch (status) {
110         case SSL_ERROR_NONE: return strncpy(buf, "SSL_ERROR_NONE", len);
111         case SSL_ERROR_ZERO_RETURN: return strncpy(buf, "SSL_ERROR_ZERO_RETURN", len);
112         case SSL_ERROR_WANT_READ: return strncpy(buf, "SSL_ERROR_WANT_READ", len);
113         case SSL_ERROR_WANT_WRITE: return strncpy(buf, "SSL_ERROR_WANT_WRITE", len);
114         case SSL_ERROR_WANT_CONNECT: return strncpy(buf, "SSL_ERROR_WANT_CONNECT", len);
115         case SSL_ERROR_WANT_ACCEPT: return strncpy(buf, "SSL_ERROR_WANT_ACCEPT", len);
116         case SSL_ERROR_WANT_X509_LOOKUP: return strncpy(buf, "SSL_ERROR_WANT_X509_LOOKUP", len);
117         case SSL_ERROR_SYSCALL:
118                 switch (ret) {
119                 case 0:
120                         snprintf(buf, len, "SSL_ERROR_SYSCALL: EOF");
121                         return buf;
122                 case -1:
123 #ifndef LWS_PLAT_OPTEE
124                         snprintf(buf, len, "SSL_ERROR_SYSCALL: %s", strerror(errno));
125 #else
126                         snprintf(buf, len, "SSL_ERROR_SYSCALL: %d", errno);
127 #endif
128                         return buf;
129                 default:
130                         return strncpy(buf, "SSL_ERROR_SYSCALL", len);
131         }
132         case SSL_ERROR_SSL: return "SSL_ERROR_SSL";
133         default: return "SSL_ERROR_UNKNOWN";
134         }
135 }
136
137 void
138 lws_ssl_elaborate_error(void)
139 {
140 #if defined(LWS_WITH_ESP32)
141 #else
142         char buf[256];
143         u_long err;
144
145         while ((err = ERR_get_error()) != 0) {
146                 ERR_error_string_n(err, buf, sizeof(buf));
147                 lwsl_err("*** %s\n", buf);
148         }
149 #endif
150 }
151
152 #if !defined(LWS_WITH_ESP32)
153
154 static int
155 lws_context_init_ssl_pem_passwd_cb(char * buf, int size, int rwflag, void *userdata)
156 {
157         struct lws_context_creation_info * info =
158                         (struct lws_context_creation_info *)userdata;
159
160         strncpy(buf, info->ssl_private_key_password, size);
161         buf[size - 1] = '\0';
162
163         return strlen(buf);
164 }
165
166 void
167 lws_ssl_bind_passphrase(SSL_CTX *ssl_ctx, struct lws_context_creation_info *info)
168 {
169         if (!info->ssl_private_key_password)
170                 return;
171         /*
172          * password provided, set ssl callback and user data
173          * for checking password which will be trigered during
174          * SSL_CTX_use_PrivateKey_file function
175          */
176         SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, (void *)info);
177         SSL_CTX_set_default_passwd_cb(ssl_ctx, lws_context_init_ssl_pem_passwd_cb);
178 }
179 #endif
180
181 int
182 lws_context_init_ssl_library(struct lws_context_creation_info *info)
183 {
184 #ifdef USE_WOLFSSL
185 #ifdef USE_OLD_CYASSL
186         lwsl_notice(" Compiled with CyaSSL support\n");
187 #else
188         lwsl_notice(" Compiled with wolfSSL support\n");
189 #endif
190 #else
191 #if defined(LWS_USE_BORINGSSL)
192         lwsl_notice(" Compiled with BoringSSL support\n");
193 #else
194         lwsl_notice(" Compiled with OpenSSL support\n");
195 #endif
196 #endif
197         if (!lws_check_opt(info->options, LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT)) {
198                 lwsl_notice(" SSL disabled: no LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT\n");
199                 return 0;
200         }
201
202         /* basic openssl init */
203
204         lwsl_notice("Doing SSL library init\n");
205
206 #if !defined(LWS_WITH_ESP32)
207         SSL_library_init();
208         OpenSSL_add_all_algorithms();
209         SSL_load_error_strings();
210
211         openssl_websocket_private_data_index =
212                 SSL_get_ex_new_index(0, "lws", NULL, NULL, NULL);
213
214         openssl_SSL_CTX_private_data_index = SSL_CTX_get_ex_new_index(0,
215                         NULL, NULL, NULL, NULL);
216 #endif
217
218         return 0;
219 }
220
221 LWS_VISIBLE void
222 lws_ssl_destroy(struct lws_vhost *vhost)
223 {
224         if (!lws_check_opt(vhost->context->options,
225                            LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT))
226                 return;
227
228         if (vhost->ssl_ctx)
229                 SSL_CTX_free(vhost->ssl_ctx);
230         if (!vhost->user_supplied_ssl_ctx && vhost->ssl_client_ctx)
231                 SSL_CTX_free(vhost->ssl_client_ctx);
232 #if !defined(LWS_WITH_ESP32)
233
234 // after 1.1.0 no need
235 #if (OPENSSL_VERSION_NUMBER <  0x10100000)
236 // <= 1.0.1f = old api, 1.0.1g+ = new api
237 #if (OPENSSL_VERSION_NUMBER <= 0x1000106f) || defined(USE_WOLFSSL)
238         ERR_remove_state(0);
239 #else
240 #if OPENSSL_VERSION_NUMBER >= 0x1010005f && \
241     !defined(LIBRESSL_VERSION_NUMBER) && \
242     !defined(OPENSSL_IS_BORINGSSL)
243         ERR_remove_thread_state();
244 #else
245         ERR_remove_thread_state(NULL);
246 #endif
247 #endif
248         ERR_free_strings();
249         EVP_cleanup();
250         CRYPTO_cleanup_all_ex_data();
251 #endif
252 #endif
253 }
254
255 LWS_VISIBLE void
256 lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi)
257 {
258         struct lws_context *context = wsi->context;
259         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
260
261         if (!wsi->pending_read_list_prev &&
262             !wsi->pending_read_list_next &&
263             pt->pending_read_list != wsi)
264                 /* we are not on the list */
265                 return;
266
267         /* point previous guy's next to our next */
268         if (!wsi->pending_read_list_prev)
269                 pt->pending_read_list = wsi->pending_read_list_next;
270         else
271                 wsi->pending_read_list_prev->pending_read_list_next =
272                         wsi->pending_read_list_next;
273
274         /* point next guy's previous to our previous */
275         if (wsi->pending_read_list_next)
276                 wsi->pending_read_list_next->pending_read_list_prev =
277                         wsi->pending_read_list_prev;
278
279         wsi->pending_read_list_prev = NULL;
280         wsi->pending_read_list_next = NULL;
281 }
282
283 LWS_VISIBLE int
284 lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len)
285 {
286         struct lws_context *context = wsi->context;
287         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
288         int n = 0;
289 #if !defined(LWS_WITH_ESP32)
290         int ssl_read_errno = 0;
291 #endif
292
293         if (!wsi->ssl)
294                 return lws_ssl_capable_read_no_ssl(wsi, buf, len);
295
296         errno = 0;
297         n = SSL_read(wsi->ssl, buf, len);
298 #if defined(LWS_WITH_ESP32)
299         if (!n && errno == ENOTCONN) {
300                 lwsl_debug("%p: SSL_read ENOTCONN\n", wsi);
301                 return LWS_SSL_CAPABLE_ERROR;
302         }
303 #endif
304
305         lwsl_debug("%p: SSL_read says %d\n", wsi, n);
306         /* manpage: returning 0 means connection shut down */
307         if (!n) {
308                 n = lws_ssl_get_error(wsi, n);
309                 lwsl_debug("%p: ssl err %d errno %d\n", wsi, n, errno);
310                 if (n == SSL_ERROR_ZERO_RETURN)
311                         return LWS_SSL_CAPABLE_ERROR;
312
313                 if (n == SSL_ERROR_SYSCALL) {
314 #if !defined(LWS_WITH_ESP32)
315                         int err = ERR_get_error();
316                         if (err == 0 && (ssl_read_errno == EPIPE ||
317                                          ssl_read_errno == ECONNABORTED ||
318                                          ssl_read_errno == 0))
319                                 return LWS_SSL_CAPABLE_ERROR;
320 #endif
321                 }
322
323                 lwsl_err("%s failed: %s\n",__func__,
324                          ERR_error_string(lws_ssl_get_error(wsi, 0), NULL));
325                 lws_ssl_elaborate_error();
326
327                 return LWS_SSL_CAPABLE_ERROR;
328         }
329
330         if (n < 0) {
331                 n = lws_ssl_get_error(wsi, n);
332                 if (n ==  SSL_ERROR_WANT_READ || SSL_want_read(wsi->ssl)) {
333                         lwsl_debug("%s: WANT_READ\n", __func__);
334                         lwsl_debug("%p: LWS_SSL_CAPABLE_MORE_SERVICE\n", wsi);
335                         return LWS_SSL_CAPABLE_MORE_SERVICE;
336                 }
337                 if (n ==  SSL_ERROR_WANT_WRITE || SSL_want_write(wsi->ssl)) {
338                         lwsl_debug("%s: WANT_WRITE\n", __func__);
339                         lwsl_debug("%p: LWS_SSL_CAPABLE_MORE_SERVICE\n", wsi);
340                         return LWS_SSL_CAPABLE_MORE_SERVICE;
341                 }
342
343
344                 lwsl_err("%s failed2: %s\n",__func__,
345                                  ERR_error_string(lws_ssl_get_error(wsi, 0), NULL));
346                         lws_ssl_elaborate_error();
347
348                 return LWS_SSL_CAPABLE_ERROR;
349         }
350
351         if (wsi->vhost)
352                 wsi->vhost->conn_stats.rx += n;
353
354         lws_restart_ws_ping_pong_timer(wsi);
355
356         /*
357          * if it was our buffer that limited what we read,
358          * check if SSL has additional data pending inside SSL buffers.
359          *
360          * Because these won't signal at the network layer with POLLIN
361          * and if we don't realize, this data will sit there forever
362          */
363         if (n != len)
364                 goto bail;
365         if (!wsi->ssl)
366                 goto bail;
367
368         if (!SSL_pending(wsi->ssl))
369                 goto bail;
370
371         if (wsi->pending_read_list_next)
372                 return n;
373         if (wsi->pending_read_list_prev)
374                 return n;
375         if (pt->pending_read_list == wsi)
376                 return n;
377
378         /* add us to the linked list of guys with pending ssl */
379         if (pt->pending_read_list)
380                 pt->pending_read_list->pending_read_list_prev = wsi;
381
382         wsi->pending_read_list_next = pt->pending_read_list;
383         wsi->pending_read_list_prev = NULL;
384         pt->pending_read_list = wsi;
385
386         return n;
387 bail:
388         lws_ssl_remove_wsi_from_buffered_list(wsi);
389
390         return n;
391 }
392
393 LWS_VISIBLE int
394 lws_ssl_pending(struct lws *wsi)
395 {
396         if (!wsi->ssl)
397                 return 0;
398
399         return SSL_pending(wsi->ssl);
400 }
401
402 LWS_VISIBLE int
403 lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, int len)
404 {
405         int n;
406 #if !defined(LWS_WITH_ESP32)
407         int ssl_read_errno = 0;
408 #endif
409
410         if (!wsi->ssl)
411                 return lws_ssl_capable_write_no_ssl(wsi, buf, len);
412
413         n = SSL_write(wsi->ssl, buf, len);
414         if (n > 0)
415                 return n;
416
417         n = lws_ssl_get_error(wsi, n);
418         if (n == SSL_ERROR_WANT_READ || n == SSL_ERROR_WANT_WRITE) {
419                 if (n == SSL_ERROR_WANT_WRITE) {
420                         lwsl_debug("%s: WANT_WRITE\n", __func__);
421                         lws_set_blocking_send(wsi);
422                 }
423                 return LWS_SSL_CAPABLE_MORE_SERVICE;
424         }
425
426  if (n == SSL_ERROR_ZERO_RETURN)
427   return LWS_SSL_CAPABLE_ERROR;
428
429 #if !defined(LWS_WITH_ESP32)
430  if (n == SSL_ERROR_SYSCALL) {
431
432   int err = ERR_get_error();
433   if (err == 0
434     && (ssl_read_errno == EPIPE
435      || ssl_read_errno == ECONNABORTED
436      || ssl_read_errno == 0))
437     return LWS_SSL_CAPABLE_ERROR;
438  }
439 #endif
440
441  lwsl_err("%s failed: %s\n",__func__,
442    ERR_error_string(lws_ssl_get_error(wsi, 0), NULL));
443         lws_ssl_elaborate_error();
444
445         return LWS_SSL_CAPABLE_ERROR;
446 }
447
448 static int
449 lws_gate_accepts(struct lws_context *context, int on)
450 {
451         struct lws_vhost *v = context->vhost_list;
452
453         lwsl_info("gating accepts %d\n", on);
454
455         while (v) {
456                 if (v->use_ssl &&  v->lserv_wsi) /* gate ability to accept incoming connections */
457                         if (lws_change_pollfd(v->lserv_wsi, (LWS_POLLIN) * !on, (LWS_POLLIN) * on))
458                                 lwsl_err("Unable to set accept POLLIN %d\n", on);
459
460                 v = v->vhost_next;
461         }
462
463         return 0;
464 }
465
466 LWS_VISIBLE int
467 lws_ssl_close(struct lws *wsi)
468 {
469         lws_sockfd_type n;
470
471         if (!wsi->ssl)
472                 return 0; /* not handled */
473
474         n = SSL_get_fd(wsi->ssl);
475         SSL_shutdown(wsi->ssl);
476         compatible_close(n);
477         SSL_free(wsi->ssl);
478         wsi->ssl = NULL;
479
480         if (wsi->context->simultaneous_ssl_restriction &&
481             wsi->context->simultaneous_ssl-- ==
482                             wsi->context->simultaneous_ssl_restriction)
483                 /* we made space and can do an accept */
484                 lws_gate_accepts(wsi->context, 1);
485
486         return 1; /* handled */
487 }
488
489 /* leave all wsi close processing to the caller */
490
491 LWS_VISIBLE int
492 lws_server_socket_service_ssl(struct lws *wsi, lws_sockfd_type accept_fd)
493 {
494         struct lws_context *context = wsi->context;
495         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
496         int n, m;
497 #if !defined(USE_WOLFSSL) && !defined(LWS_WITH_ESP32)
498         BIO *bio;
499 #endif
500         char buf[256];
501
502         if (!LWS_SSL_ENABLED(wsi->vhost))
503                 return 0;
504
505         switch (wsi->mode) {
506         case LWSCM_SSL_INIT:
507         case LWSCM_SSL_INIT_RAW:
508                 if (wsi->ssl)
509                         lwsl_err("%s: leaking ssl\n", __func__);
510                 if (accept_fd == LWS_SOCK_INVALID)
511                         assert(0);
512                 if (context->simultaneous_ssl_restriction &&
513                     context->simultaneous_ssl >= context->simultaneous_ssl_restriction) {
514                         lwsl_notice("unable to deal with SSL connection\n");
515                         return 1;
516                 }
517                 errno = 0;
518                 wsi->ssl = SSL_new(wsi->vhost->ssl_ctx);
519                 if (wsi->ssl == NULL) {
520                         lwsl_err("SSL_new failed: %d (errno %d)\n",
521                                  lws_ssl_get_error(wsi, 0), errno);
522
523                         lws_ssl_elaborate_error();
524                         if (accept_fd != LWS_SOCK_INVALID)
525                                 compatible_close(accept_fd);
526                         goto fail;
527                 }
528                 if (context->simultaneous_ssl_restriction &&
529                     ++context->simultaneous_ssl == context->simultaneous_ssl_restriction)
530                         /* that was the last allowed SSL connection */
531                         lws_gate_accepts(context, 0);
532
533 #if !defined(LWS_WITH_ESP32)
534                 SSL_set_ex_data(wsi->ssl,
535                         openssl_websocket_private_data_index, wsi);
536 #endif
537                 SSL_set_fd(wsi->ssl, accept_fd);
538
539 #ifdef USE_WOLFSSL
540 #ifdef USE_OLD_CYASSL
541                 CyaSSL_set_using_nonblock(wsi->ssl, 1);
542 #else
543                 wolfSSL_set_using_nonblock(wsi->ssl, 1);
544 #endif
545 #else
546 #if defined(LWS_WITH_ESP32)
547                 lws_plat_set_socket_options(wsi->vhost, accept_fd);
548 #else
549                 SSL_set_mode(wsi->ssl, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
550                 bio = SSL_get_rbio(wsi->ssl);
551                 if (bio)
552                         BIO_set_nbio(bio, 1); /* nonblocking */
553                 else
554                         lwsl_notice("NULL rbio\n");
555                 bio = SSL_get_wbio(wsi->ssl);
556                 if (bio)
557                         BIO_set_nbio(bio, 1); /* nonblocking */
558                 else
559                         lwsl_notice("NULL rbio\n");
560 #endif
561 #endif
562
563                 /*
564                  * we are not accepted yet, but we need to enter ourselves
565                  * as a live connection.  That way we can retry when more
566                  * pieces come if we're not sorted yet
567                  */
568
569                 if (wsi->mode == LWSCM_SSL_INIT)
570                         wsi->mode = LWSCM_SSL_ACK_PENDING;
571                 else
572                         wsi->mode = LWSCM_SSL_ACK_PENDING_RAW;
573
574                 if (insert_wsi_socket_into_fds(context, wsi)) {
575                         lwsl_err("%s: failed to insert into fds\n", __func__);
576                         goto fail;
577                 }
578
579                 lws_set_timeout(wsi, PENDING_TIMEOUT_SSL_ACCEPT,
580                                 context->timeout_secs);
581
582                 lwsl_debug("inserted SSL accept into fds, trying SSL_accept\n");
583
584                 /* fallthru */
585
586         case LWSCM_SSL_ACK_PENDING:
587         case LWSCM_SSL_ACK_PENDING_RAW:
588                 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
589                         lwsl_err("%s: lws_change_pollfd failed\n", __func__);
590                         goto fail;
591                 }
592
593                 lws_latency_pre(context, wsi);
594
595                 if (wsi->vhost->allow_non_ssl_on_ssl_port) {
596
597                         n = recv(wsi->desc.sockfd, (char *)pt->serv_buf, context->pt_serv_buf_size,
598                                  MSG_PEEK);
599
600                 /*
601                  * optionally allow non-SSL connect on SSL listening socket
602                  * This is disabled by default, if enabled it goes around any
603                  * SSL-level access control (eg, client-side certs) so leave
604                  * it disabled unless you know it's not a problem for you
605                  */
606
607                         if (n >= 1 && pt->serv_buf[0] >= ' ') {
608                                 /*
609                                 * TLS content-type for Handshake is 0x16, and
610                                 * for ChangeCipherSpec Record, it's 0x14
611                                 *
612                                 * A non-ssl session will start with the HTTP
613                                 * method in ASCII.  If we see it's not a legit
614                                 * SSL handshake kill the SSL for this
615                                 * connection and try to handle as a HTTP
616                                 * connection upgrade directly.
617                                 */
618                                 wsi->use_ssl = 0;
619
620                                 SSL_shutdown(wsi->ssl);
621                                 SSL_free(wsi->ssl);
622                                 wsi->ssl = NULL;
623                                 if (lws_check_opt(context->options,
624                                     LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS))
625                                         wsi->redirect_to_https = 1;
626                                 goto accepted;
627                         }
628                         if (!n) /*
629                                  * connection is gone, or nothing to read
630                                  * if it's gone, we will timeout on
631                                  * PENDING_TIMEOUT_SSL_ACCEPT
632                                  */
633                                 break;
634                         if (n < 0 && (LWS_ERRNO == LWS_EAGAIN ||
635                                       LWS_ERRNO == LWS_EWOULDBLOCK)) {
636                                 /*
637                                  * well, we get no way to know ssl or not
638                                  * so go around again waiting for something
639                                  * to come and give us a hint, or timeout the
640                                  * connection.
641                                  */
642                                 m = SSL_ERROR_WANT_READ;
643                                 goto go_again;
644                         }
645                 }
646
647                 /* normal SSL connection processing path */
648
649                 n = SSL_accept(wsi->ssl);
650                 lws_latency(context, wsi,
651                         "SSL_accept LWSCM_SSL_ACK_PENDING\n", n, n == 1);
652                 lwsl_info("SSL_accept says %d\n", n);
653                 if (n == 1)
654                         goto accepted;
655
656                 m = lws_ssl_get_error(wsi, n);
657
658 #if defined(LWS_WITH_ESP32)
659                 if (m == 5 && errno == 11)
660                         m = SSL_ERROR_WANT_READ;
661 #endif
662
663 go_again:
664                 if (m == SSL_ERROR_WANT_READ || SSL_want_read(wsi->ssl)) {
665                         if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) {
666                                 lwsl_err("%s: WANT_READ change_pollfd failed\n", __func__);
667                                 goto fail;
668                         }
669
670                         lwsl_info("SSL_ERROR_WANT_READ\n");
671                         break;
672                 }
673                 if (m == SSL_ERROR_WANT_WRITE || SSL_want_write(wsi->ssl)) {
674                         lwsl_debug("%s: WANT_WRITE\n", __func__);
675
676                         if (lws_change_pollfd(wsi, 0, LWS_POLLOUT)) {
677                                 lwsl_err("%s: WANT_WRITE change_pollfd failed\n", __func__);
678                                 goto fail;
679                         }
680
681                         break;
682                 }
683
684                 lwsl_err("SSL_accept failed socket %u: %s\n", wsi->desc.sockfd,
685                          lws_ssl_get_error_string(m, n, buf, sizeof(buf)));
686                 lws_ssl_elaborate_error();
687                 goto fail;
688
689 accepted:
690                 /* OK, we are accepted... give him some time to negotiate */
691                 lws_set_timeout(wsi, PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
692                                 context->timeout_secs);
693
694                 if (wsi->mode == LWSCM_SSL_ACK_PENDING_RAW)
695                         wsi->mode = LWSCM_RAW;
696                 else
697                         wsi->mode = LWSCM_HTTP_SERVING;
698
699                 lws_http2_configure_if_upgraded(wsi);
700
701                 lwsl_debug("accepted new SSL conn\n");
702                 break;
703         }
704
705         return 0;
706
707 fail:
708         return 1;
709 }
710
711 void
712 lws_ssl_SSL_CTX_destroy(struct lws_vhost *vhost)
713 {
714         if (vhost->ssl_ctx)
715                 SSL_CTX_free(vhost->ssl_ctx);
716
717         if (!vhost->user_supplied_ssl_ctx && vhost->ssl_client_ctx)
718                 SSL_CTX_free(vhost->ssl_client_ctx);
719 }
720
721 void
722 lws_ssl_context_destroy(struct lws_context *context)
723 {
724
725 #if !defined(LWS_WITH_ESP32)
726
727 // after 1.1.0 no need
728 #if (OPENSSL_VERSION_NUMBER <  0x10100000)
729 // <= 1.0.1f = old api, 1.0.1g+ = new api
730 #if (OPENSSL_VERSION_NUMBER <= 0x1000106f) || defined(USE_WOLFSSL)
731         ERR_remove_state(0);
732 #else
733 #if OPENSSL_VERSION_NUMBER >= 0x1010005f && \
734     !defined(LIBRESSL_VERSION_NUMBER) && \
735     !defined(OPENSSL_IS_BORINGSSL)
736         ERR_remove_thread_state();
737 #else
738         ERR_remove_thread_state(NULL);
739 #endif
740 #endif
741         ERR_free_strings();
742         EVP_cleanup();
743         CRYPTO_cleanup_all_ex_data();
744 #endif
745 #endif
746 }