2017-10-18 09:41:44 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
2017-10-18 09:41:44 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2017-10-18 09:41:44 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2017-10-18 09:41:44 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
2017-10-18 09:41:44 +08:00
|
|
|
*/
|
|
|
|
|
2019-08-15 10:49:52 +01:00
|
|
|
#include "private-lib-core.h"
|
|
|
|
#include "private-lib-tls-openssl.h"
|
2017-10-18 09:41:44 +08:00
|
|
|
|
|
|
|
int openssl_websocket_private_data_index,
|
|
|
|
openssl_SSL_CTX_private_data_index;
|
|
|
|
|
2018-12-13 20:05:12 +08:00
|
|
|
/*
|
|
|
|
* Care: many openssl apis return 1 for success. These are translated to the
|
|
|
|
* lws convention of 0 for success.
|
|
|
|
*/
|
|
|
|
|
2018-10-20 07:54:51 +08:00
|
|
|
int lws_openssl_describe_cipher(struct lws *wsi)
|
|
|
|
{
|
2020-03-20 09:35:54 +00:00
|
|
|
#if !defined(LWS_WITH_NO_LOGS) && !defined(USE_WOLFSSL)
|
2018-10-20 07:54:51 +08:00
|
|
|
int np = -1;
|
|
|
|
SSL *s = wsi->tls.ssl;
|
|
|
|
|
|
|
|
SSL_get_cipher_bits(s, &np);
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_info("%s: %s: %s, %s, %d bits, %s\n", __func__, lws_wsi_tag(wsi),
|
2018-10-20 07:54:51 +08:00
|
|
|
SSL_get_cipher_name(s), SSL_get_cipher(s), np,
|
|
|
|
SSL_get_cipher_version(s));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-18 09:41:44 +08:00
|
|
|
int lws_ssl_get_error(struct lws *wsi, int n)
|
|
|
|
{
|
|
|
|
int m;
|
2024-09-23 06:57:29 +01:00
|
|
|
unsigned long l;
|
|
|
|
char buf[160];
|
2017-10-18 09:41:44 +08:00
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
if (!wsi->tls.ssl)
|
2017-10-18 09:41:44 +08:00
|
|
|
return 99;
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
m = SSL_get_error(wsi->tls.ssl, n);
|
2021-07-20 10:18:59 +01:00
|
|
|
lwsl_debug("%s: %p %d -> %d (errno %d)\n", __func__, wsi->tls.ssl, n, m, LWS_ERRNO);
|
2024-09-23 06:57:29 +01:00
|
|
|
if (m == SSL_ERROR_SSL) {
|
|
|
|
if (!wsi->tls.err_helper[0]) {
|
|
|
|
/* Append first error for clarity */
|
|
|
|
l = ERR_get_error();
|
|
|
|
if (l) {
|
|
|
|
ERR_error_string_n(
|
|
|
|
#if defined(LWS_WITH_BORINGSSL)
|
|
|
|
(uint32_t)
|
|
|
|
#endif
|
|
|
|
l, buf, sizeof(buf) - 1);
|
|
|
|
buf[sizeof(buf) - 1] = '\0';
|
|
|
|
lws_strncpy(wsi->tls.err_helper, buf,
|
|
|
|
sizeof(wsi->tls.err_helper));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Describe other errors
|
|
|
|
lws_tls_err_describe_clear();
|
|
|
|
}
|
2017-10-18 09:41:44 +08:00
|
|
|
|
2021-07-20 10:18:59 +01:00
|
|
|
// assert (LWS_ERRNO != 9);
|
2020-02-29 12:37:24 +00:00
|
|
|
|
2017-10-18 09:41:44 +08:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2020-06-17 11:13:01 +01:00
|
|
|
#if defined(LWS_WITH_SERVER)
|
2017-10-18 09:41:44 +08:00
|
|
|
static int
|
2019-06-21 07:18:48 +01:00
|
|
|
lws_context_init_ssl_pem_passwd_cb(char *buf, int size, int rwflag,
|
2017-10-18 09:41:44 +08:00
|
|
|
void *userdata)
|
|
|
|
{
|
|
|
|
struct lws_context_creation_info * info =
|
|
|
|
(struct lws_context_creation_info *)userdata;
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
strncpy(buf, info->ssl_private_key_password, (unsigned int)size);
|
2017-10-18 09:41:44 +08:00
|
|
|
buf[size - 1] = '\0';
|
|
|
|
|
2017-10-25 08:00:23 +08:00
|
|
|
return (int)strlen(buf);
|
2017-10-18 09:41:44 +08:00
|
|
|
}
|
2020-06-17 11:13:01 +01:00
|
|
|
#endif
|
2017-10-18 09:41:44 +08:00
|
|
|
|
2020-06-17 11:13:01 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2019-06-21 07:18:48 +01:00
|
|
|
static int
|
|
|
|
lws_context_init_ssl_pem_passwd_client_cb(char *buf, int size, int rwflag,
|
|
|
|
void *userdata)
|
|
|
|
{
|
|
|
|
struct lws_context_creation_info * info =
|
|
|
|
(struct lws_context_creation_info *)userdata;
|
|
|
|
const char *p = info->ssl_private_key_password;
|
|
|
|
|
|
|
|
if (info->client_ssl_private_key_password)
|
|
|
|
p = info->client_ssl_private_key_password;
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
strncpy(buf, p, (unsigned int)size);
|
2019-06-21 07:18:48 +01:00
|
|
|
buf[size - 1] = '\0';
|
|
|
|
|
|
|
|
return (int)strlen(buf);
|
|
|
|
}
|
2020-06-17 11:13:01 +01:00
|
|
|
#endif
|
2019-06-21 07:18:48 +01:00
|
|
|
|
2017-10-18 09:41:44 +08:00
|
|
|
void
|
2019-06-21 07:18:48 +01:00
|
|
|
lws_ssl_bind_passphrase(SSL_CTX *ssl_ctx, int is_client,
|
2018-04-27 08:27:16 +08:00
|
|
|
const struct lws_context_creation_info *info)
|
2017-10-18 09:41:44 +08:00
|
|
|
{
|
2020-06-17 11:13:01 +01:00
|
|
|
if (
|
|
|
|
#if defined(LWS_WITH_SERVER)
|
|
|
|
!info->ssl_private_key_password
|
|
|
|
#endif
|
|
|
|
#if defined(LWS_WITH_SERVER) && defined(LWS_WITH_CLIENT)
|
|
|
|
&&
|
|
|
|
#endif
|
|
|
|
#if defined(LWS_WITH_CLIENT)
|
|
|
|
!info->client_ssl_private_key_password
|
|
|
|
#endif
|
|
|
|
)
|
2017-10-18 09:41:44 +08:00
|
|
|
return;
|
|
|
|
/*
|
|
|
|
* password provided, set ssl callback and user data
|
|
|
|
* for checking password which will be trigered during
|
|
|
|
* SSL_CTX_use_PrivateKey_file function
|
|
|
|
*/
|
|
|
|
SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, (void *)info);
|
2019-06-21 07:18:48 +01:00
|
|
|
SSL_CTX_set_default_passwd_cb(ssl_ctx, is_client ?
|
2020-06-17 11:13:01 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2019-06-21 07:18:48 +01:00
|
|
|
lws_context_init_ssl_pem_passwd_client_cb:
|
2020-06-17 11:13:01 +01:00
|
|
|
#else
|
|
|
|
NULL:
|
|
|
|
#endif
|
|
|
|
#if defined(LWS_WITH_SERVER)
|
|
|
|
lws_context_init_ssl_pem_passwd_cb
|
|
|
|
#else
|
|
|
|
NULL
|
|
|
|
#endif
|
|
|
|
);
|
2017-10-18 09:41:44 +08:00
|
|
|
}
|
|
|
|
|
2020-06-17 11:13:01 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2019-03-17 10:03:22 +08:00
|
|
|
static void
|
|
|
|
lws_ssl_destroy_client_ctx(struct lws_vhost *vhost)
|
|
|
|
{
|
|
|
|
if (vhost->tls.user_supplied_ssl_ctx || !vhost->tls.ssl_client_ctx)
|
|
|
|
return;
|
|
|
|
|
2021-01-19 14:12:56 +00:00
|
|
|
if (vhost->tls.tcr && --vhost->tls.tcr->refcount)
|
2019-03-17 10:03:22 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
SSL_CTX_free(vhost->tls.ssl_client_ctx);
|
|
|
|
vhost->tls.ssl_client_ctx = NULL;
|
|
|
|
|
|
|
|
vhost->context->tls.count_client_contexts--;
|
|
|
|
|
2021-01-19 14:12:56 +00:00
|
|
|
if (vhost->tls.tcr) {
|
|
|
|
lws_dll2_remove(&vhost->tls.tcr->cc_list);
|
|
|
|
lws_free(vhost->tls.tcr);
|
|
|
|
vhost->tls.tcr = NULL;
|
|
|
|
}
|
2019-03-17 10:03:22 +08:00
|
|
|
}
|
2020-06-17 11:13:01 +01:00
|
|
|
#endif
|
2020-01-02 08:32:23 +00:00
|
|
|
void
|
2017-10-18 09:41:44 +08:00
|
|
|
lws_ssl_destroy(struct lws_vhost *vhost)
|
|
|
|
{
|
|
|
|
if (!lws_check_opt(vhost->context->options,
|
|
|
|
LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT))
|
|
|
|
return;
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
if (vhost->tls.ssl_ctx)
|
|
|
|
SSL_CTX_free(vhost->tls.ssl_ctx);
|
2020-06-17 11:13:01 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2019-03-17 10:03:22 +08:00
|
|
|
lws_ssl_destroy_client_ctx(vhost);
|
2020-06-17 11:13:01 +01:00
|
|
|
#endif
|
2017-10-18 09:41:44 +08:00
|
|
|
|
|
|
|
// after 1.1.0 no need
|
|
|
|
#if (OPENSSL_VERSION_NUMBER < 0x10100000)
|
|
|
|
// <= 1.0.1f = old api, 1.0.1g+ = new api
|
|
|
|
#if (OPENSSL_VERSION_NUMBER <= 0x1000106f) || defined(USE_WOLFSSL)
|
|
|
|
ERR_remove_state(0);
|
|
|
|
#else
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x1010005f && \
|
|
|
|
!defined(LIBRESSL_VERSION_NUMBER) && \
|
|
|
|
!defined(OPENSSL_IS_BORINGSSL)
|
|
|
|
ERR_remove_thread_state();
|
|
|
|
#else
|
|
|
|
ERR_remove_thread_state(NULL);
|
|
|
|
#endif
|
|
|
|
#endif
|
2018-11-23 08:47:56 +08:00
|
|
|
/* not needed after 1.1.0 */
|
|
|
|
#if (OPENSSL_VERSION_NUMBER >= 0x10002000) && \
|
|
|
|
(OPENSSL_VERSION_NUMBER <= 0x10100000)
|
2017-10-18 09:41:44 +08:00
|
|
|
SSL_COMP_free_compression_methods();
|
|
|
|
#endif
|
|
|
|
ERR_free_strings();
|
|
|
|
EVP_cleanup();
|
|
|
|
CRYPTO_cleanup_all_ex_data();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-01-02 08:32:23 +00:00
|
|
|
int
|
2020-12-12 06:21:40 +00:00
|
|
|
lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, size_t len)
|
2017-10-18 09:41:44 +08:00
|
|
|
{
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
struct lws_context *context = wsi->a.context;
|
2017-10-18 09:41:44 +08:00
|
|
|
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
|
|
|
int n = 0, m;
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
if (!wsi->tls.ssl)
|
2017-10-18 09:41:44 +08:00
|
|
|
return lws_ssl_capable_read_no_ssl(wsi, buf, len);
|
|
|
|
|
2021-07-20 10:18:59 +01:00
|
|
|
#ifndef WIN32
|
2017-10-18 09:41:44 +08:00
|
|
|
errno = 0;
|
2021-07-20 10:18:59 +01:00
|
|
|
#else
|
|
|
|
WSASetLastError(0);
|
|
|
|
#endif
|
2019-07-10 20:34:24 +01:00
|
|
|
ERR_clear_error();
|
2020-12-12 06:21:40 +00:00
|
|
|
n = SSL_read(wsi->tls.ssl, buf, (int)(ssize_t)len);
|
2019-08-18 10:35:43 +01:00
|
|
|
#if defined(LWS_PLAT_FREERTOS)
|
2018-09-05 14:45:10 +08:00
|
|
|
if (!n && errno == LWS_ENOTCONN) {
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_debug("%s: SSL_read ENOTCONN\n", lws_wsi_tag(wsi));
|
2017-10-18 09:41:44 +08:00
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_debug("%s: SSL_read says %d\n", lws_wsi_tag(wsi), n);
|
2018-09-10 08:53:41 +08:00
|
|
|
/* manpage: returning 0 means connection shut down
|
|
|
|
*
|
|
|
|
* 2018-09-10: https://github.com/openssl/openssl/issues/1903
|
|
|
|
*
|
|
|
|
* So, in summary, if you get a 0 or -1 return from SSL_read() /
|
|
|
|
* SSL_write(), you should call SSL_get_error():
|
|
|
|
*
|
|
|
|
* - If you get back SSL_ERROR_RETURN_ZERO then you know the connection
|
|
|
|
* has been cleanly shutdown by the peer. To fully close the
|
|
|
|
* connection you may choose to call SSL_shutdown() to send a
|
|
|
|
* close_notify back.
|
|
|
|
*
|
|
|
|
* - If you get back SSL_ERROR_SSL then some kind of internal or
|
|
|
|
* protocol error has occurred. More details will be on the SSL error
|
|
|
|
* queue. You can also call SSL_get_shutdown(). If this indicates a
|
|
|
|
* state of SSL_RECEIVED_SHUTDOWN then you know a fatal alert has
|
|
|
|
* been received from the peer (if it had been a close_notify then
|
|
|
|
* SSL_get_error() would have returned SSL_ERROR_RETURN_ZERO).
|
|
|
|
* SSL_ERROR_SSL is considered fatal - you should not call
|
|
|
|
* SSL_shutdown() in this case.
|
|
|
|
*
|
|
|
|
* - If you get back SSL_ERROR_SYSCALL then some kind of fatal (i.e.
|
|
|
|
* non-retryable) error has occurred in a system call.
|
|
|
|
*/
|
|
|
|
if (n <= 0) {
|
|
|
|
m = lws_ssl_get_error(wsi, n);
|
2021-07-20 10:18:59 +01:00
|
|
|
lwsl_debug("%s: ssl err %d errno %d\n", lws_wsi_tag(wsi), m, LWS_ERRNO);
|
2018-09-10 08:53:41 +08:00
|
|
|
if (m == SSL_ERROR_ZERO_RETURN) /* cleanly shut down */
|
2021-01-06 15:08:22 +00:00
|
|
|
goto do_err;
|
2017-10-18 09:41:44 +08:00
|
|
|
|
2023-10-20 17:42:35 -04:00
|
|
|
if (m == SSL_ERROR_SSL)
|
|
|
|
lws_tls_err_describe_clear();
|
|
|
|
|
2018-09-10 08:53:41 +08:00
|
|
|
/* hm not retryable.. could be 0 size pkt or error */
|
|
|
|
|
|
|
|
if (m == SSL_ERROR_SSL || m == SSL_ERROR_SYSCALL ||
|
2021-07-20 10:18:59 +01:00
|
|
|
LWS_ERRNO == LWS_ENOTCONN) {
|
2018-09-10 08:53:41 +08:00
|
|
|
|
|
|
|
/* unclean, eg closed conn */
|
|
|
|
|
|
|
|
wsi->socket_is_permanently_unusable = 1;
|
2021-01-06 15:08:22 +00:00
|
|
|
do_err:
|
|
|
|
#if defined(LWS_WITH_SYS_METRICS)
|
|
|
|
if (wsi->a.vhost)
|
|
|
|
lws_metric_event(wsi->a.vhost->mt_traffic_rx,
|
|
|
|
METRES_NOGO, 0);
|
|
|
|
#endif
|
2017-10-18 09:41:44 +08:00
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
2018-09-10 08:53:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* retryable? */
|
2017-10-18 09:41:44 +08:00
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
if (SSL_want_read(wsi->tls.ssl)) {
|
2017-10-18 09:41:44 +08:00
|
|
|
lwsl_debug("%s: WANT_READ\n", __func__);
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_debug("%s: LWS_SSL_CAPABLE_MORE_SERVICE\n", lws_wsi_tag(wsi));
|
2017-10-18 09:41:44 +08:00
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
|
|
|
}
|
2018-05-01 12:41:42 +08:00
|
|
|
if (SSL_want_write(wsi->tls.ssl)) {
|
2021-08-31 05:06:06 +01:00
|
|
|
lwsl_info("%s: WANT_WRITE\n", __func__);
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_debug("%s: LWS_SSL_CAPABLE_MORE_SERVICE\n", lws_wsi_tag(wsi));
|
2021-08-31 05:06:06 +01:00
|
|
|
wsi->tls_read_wanted_write = 1;
|
|
|
|
lws_callback_on_writable(wsi);
|
2017-10-18 09:41:44 +08:00
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
|
|
|
}
|
|
|
|
|
2018-09-10 08:53:41 +08:00
|
|
|
/* keep on trucking it seems */
|
2017-10-18 09:41:44 +08:00
|
|
|
}
|
|
|
|
|
2021-01-23 20:56:01 +00:00
|
|
|
#if defined(LWS_TLS_LOG_PLAINTEXT_RX)
|
2020-07-30 16:23:59 +01:00
|
|
|
/*
|
|
|
|
* If using openssl type tls library, this is the earliest point for all
|
|
|
|
* paths to dump what was received as decrypted data from the tls tunnel
|
|
|
|
*/
|
|
|
|
lwsl_notice("%s: len %d\n", __func__, n);
|
2021-01-06 15:08:22 +00:00
|
|
|
lwsl_hexdump_notice(buf, (unsigned int)n);
|
2020-07-30 16:23:59 +01:00
|
|
|
#endif
|
|
|
|
|
2021-01-06 15:08:22 +00:00
|
|
|
#if defined(LWS_WITH_SYS_METRICS)
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
if (wsi->a.vhost)
|
2021-01-06 15:08:22 +00:00
|
|
|
lws_metric_event(wsi->a.vhost->mt_traffic_rx, METRES_GO, (u_mt_t)n);
|
2019-08-23 16:10:36 +01:00
|
|
|
#endif
|
|
|
|
|
2017-10-18 09:41:44 +08:00
|
|
|
/*
|
|
|
|
* if it was our buffer that limited what we read,
|
|
|
|
* check if SSL has additional data pending inside SSL buffers.
|
|
|
|
*
|
|
|
|
* Because these won't signal at the network layer with POLLIN
|
|
|
|
* and if we don't realize, this data will sit there forever
|
|
|
|
*/
|
2020-12-12 06:21:40 +00:00
|
|
|
if (n != (int)(ssize_t)len)
|
2017-10-18 09:41:44 +08:00
|
|
|
goto bail;
|
2018-05-01 12:41:42 +08:00
|
|
|
if (!wsi->tls.ssl)
|
2017-10-18 09:41:44 +08:00
|
|
|
goto bail;
|
|
|
|
|
2020-05-18 06:49:01 +01:00
|
|
|
if (SSL_pending(wsi->tls.ssl)) {
|
|
|
|
if (lws_dll2_is_detached(&wsi->tls.dll_pending_tls))
|
|
|
|
lws_dll2_add_head(&wsi->tls.dll_pending_tls,
|
|
|
|
&pt->tls.dll_pending_tls_owner);
|
|
|
|
} else
|
|
|
|
__lws_ssl_remove_wsi_from_buffered_list(wsi);
|
2017-10-18 09:41:44 +08:00
|
|
|
|
|
|
|
return n;
|
|
|
|
bail:
|
|
|
|
lws_ssl_remove_wsi_from_buffered_list(wsi);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2020-01-02 08:32:23 +00:00
|
|
|
int
|
2017-10-18 09:41:44 +08:00
|
|
|
lws_ssl_pending(struct lws *wsi)
|
|
|
|
{
|
2018-05-01 12:41:42 +08:00
|
|
|
if (!wsi->tls.ssl)
|
2017-10-18 09:41:44 +08:00
|
|
|
return 0;
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
return SSL_pending(wsi->tls.ssl);
|
2017-10-18 09:41:44 +08:00
|
|
|
}
|
|
|
|
|
2020-01-02 08:32:23 +00:00
|
|
|
int
|
2020-12-12 06:21:40 +00:00
|
|
|
lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, size_t len)
|
2017-10-18 09:41:44 +08:00
|
|
|
{
|
|
|
|
int n, m;
|
|
|
|
|
2021-01-23 20:56:01 +00:00
|
|
|
|
|
|
|
#if defined(LWS_TLS_LOG_PLAINTEXT_TX)
|
2020-07-30 16:23:59 +01:00
|
|
|
/*
|
|
|
|
* If using OpenSSL type tls library, this is the last point for all
|
|
|
|
* paths before sending data into the tls tunnel, where you can dump it
|
|
|
|
* and see what is being sent.
|
|
|
|
*/
|
2021-01-06 15:08:22 +00:00
|
|
|
lwsl_notice("%s: len %u\n", __func__, (unsigned int)len);
|
2020-07-30 16:23:59 +01:00
|
|
|
lwsl_hexdump_notice(buf, len);
|
|
|
|
#endif
|
2020-01-02 08:32:23 +00:00
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
if (!wsi->tls.ssl)
|
2017-10-18 09:41:44 +08:00
|
|
|
return lws_ssl_capable_write_no_ssl(wsi, buf, len);
|
|
|
|
|
2019-07-10 20:34:24 +01:00
|
|
|
errno = 0;
|
|
|
|
ERR_clear_error();
|
2020-12-12 06:21:40 +00:00
|
|
|
n = SSL_write(wsi->tls.ssl, buf, (int)(ssize_t)len);
|
2021-01-06 15:08:22 +00:00
|
|
|
if (n > 0) {
|
|
|
|
#if defined(LWS_WITH_SYS_METRICS)
|
|
|
|
if (wsi->a.vhost)
|
|
|
|
lws_metric_event(wsi->a.vhost->mt_traffic_tx,
|
|
|
|
METRES_GO, (u_mt_t)n);
|
|
|
|
#endif
|
2017-10-18 09:41:44 +08:00
|
|
|
return n;
|
2021-01-06 15:08:22 +00:00
|
|
|
}
|
2017-10-18 09:41:44 +08:00
|
|
|
|
|
|
|
m = lws_ssl_get_error(wsi, n);
|
|
|
|
if (m != SSL_ERROR_SYSCALL) {
|
2018-05-01 12:41:42 +08:00
|
|
|
if (m == SSL_ERROR_WANT_READ || SSL_want_read(wsi->tls.ssl)) {
|
2017-10-18 09:41:44 +08:00
|
|
|
lwsl_notice("%s: want read\n", __func__);
|
|
|
|
|
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
|
|
|
}
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
if (m == SSL_ERROR_WANT_WRITE || SSL_want_write(wsi->tls.ssl)) {
|
2017-10-18 09:41:44 +08:00
|
|
|
lws_set_blocking_send(wsi);
|
|
|
|
|
2018-08-25 05:43:31 +08:00
|
|
|
lwsl_debug("%s: want write\n", __func__);
|
2017-10-18 09:41:44 +08:00
|
|
|
|
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
lwsl_debug("%s failed: %s\n",__func__, ERR_error_string((unsigned int)m, NULL));
|
2019-07-13 13:28:14 -07:00
|
|
|
lws_tls_err_describe_clear();
|
2017-10-18 09:41:44 +08:00
|
|
|
|
|
|
|
wsi->socket_is_permanently_unusable = 1;
|
|
|
|
|
2021-01-06 15:08:22 +00:00
|
|
|
#if defined(LWS_WITH_SYS_METRICS)
|
|
|
|
if (wsi->a.vhost)
|
|
|
|
lws_metric_event(wsi->a.vhost->mt_traffic_tx,
|
|
|
|
METRES_NOGO, 0);
|
|
|
|
#endif
|
|
|
|
|
2017-10-18 09:41:44 +08:00
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lws_ssl_info_callback(const SSL *ssl, int where, int ret)
|
|
|
|
{
|
|
|
|
struct lws *wsi;
|
|
|
|
struct lws_context *context;
|
|
|
|
struct lws_ssl_info si;
|
2020-05-27 15:06:33 +09:00
|
|
|
int fd;
|
2017-10-18 09:41:44 +08:00
|
|
|
|
2018-04-28 07:54:09 +08:00
|
|
|
#ifndef USE_WOLFSSL
|
2017-10-18 09:41:44 +08:00
|
|
|
context = (struct lws_context *)SSL_CTX_get_ex_data(
|
|
|
|
SSL_get_SSL_CTX(ssl),
|
|
|
|
openssl_SSL_CTX_private_data_index);
|
2018-04-28 07:54:09 +08:00
|
|
|
#else
|
|
|
|
context = (struct lws_context *)SSL_CTX_get_ex_data(
|
|
|
|
SSL_get_SSL_CTX((SSL*) ssl),
|
|
|
|
openssl_SSL_CTX_private_data_index);
|
|
|
|
#endif
|
2017-10-18 09:41:44 +08:00
|
|
|
if (!context)
|
|
|
|
return;
|
2020-05-27 15:06:33 +09:00
|
|
|
|
|
|
|
fd = SSL_get_fd(ssl);
|
|
|
|
if (fd < 0 || (fd - lws_plat_socket_offset()) < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
wsi = wsi_from_fd(context, fd);
|
2017-10-18 09:41:44 +08:00
|
|
|
if (!wsi)
|
|
|
|
return;
|
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
if (!(where & wsi->a.vhost->tls.ssl_info_event_mask))
|
2017-10-18 09:41:44 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
si.where = where;
|
|
|
|
si.ret = ret;
|
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
if (user_callback_handle_rxflow(wsi->a.protocol->callback,
|
2018-11-23 08:47:56 +08:00
|
|
|
wsi, LWS_CALLBACK_SSL_INFO,
|
|
|
|
wsi->user_space, &si, 0))
|
2017-10-18 09:41:44 +08:00
|
|
|
lws_set_timeout(wsi, PENDING_TIMEOUT_KILLED_BY_SSL_INFO, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-02 08:32:23 +00:00
|
|
|
int
|
2017-10-18 09:41:44 +08:00
|
|
|
lws_ssl_close(struct lws *wsi)
|
|
|
|
{
|
|
|
|
lws_sockfd_type n;
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
if (!wsi->tls.ssl)
|
2017-10-18 09:41:44 +08:00
|
|
|
return 0; /* not handled */
|
|
|
|
|
|
|
|
#if defined (LWS_HAVE_SSL_SET_INFO_CALLBACK)
|
2019-07-10 20:34:24 +01:00
|
|
|
/* kill ssl callbacks, because we will remove the fd from the
|
2017-10-18 09:41:44 +08:00
|
|
|
* table linking it to the wsi
|
|
|
|
*/
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
if (wsi->a.vhost->tls.ssl_info_event_mask)
|
2018-05-01 12:41:42 +08:00
|
|
|
SSL_set_info_callback(wsi->tls.ssl, NULL);
|
2017-10-18 09:41:44 +08:00
|
|
|
#endif
|
|
|
|
|
2021-04-28 16:54:08 +01:00
|
|
|
#if defined(LWS_TLS_SYNTHESIZE_CB)
|
|
|
|
lws_sul_cancel(&wsi->tls.sul_cb_synth);
|
|
|
|
/*
|
|
|
|
* ... check the session in case it did not live long enough to get
|
|
|
|
* the scheduled callback to sample it
|
|
|
|
*/
|
|
|
|
lws_sess_cache_synth_cb(&wsi->tls.sul_cb_synth);
|
|
|
|
#endif
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
n = SSL_get_fd(wsi->tls.ssl);
|
2017-10-18 09:41:44 +08:00
|
|
|
if (!wsi->socket_is_permanently_unusable)
|
2018-05-01 12:41:42 +08:00
|
|
|
SSL_shutdown(wsi->tls.ssl);
|
2017-10-18 09:41:44 +08:00
|
|
|
compatible_close(n);
|
2018-05-01 12:41:42 +08:00
|
|
|
SSL_free(wsi->tls.ssl);
|
|
|
|
wsi->tls.ssl = NULL;
|
2017-10-18 09:41:44 +08:00
|
|
|
|
2021-09-22 09:34:56 +01:00
|
|
|
lws_tls_restrict_return(wsi);
|
2017-11-26 09:22:42 +08:00
|
|
|
|
|
|
|
// lwsl_notice("%s: ssl restr %d, simul %d\n", __func__,
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
// wsi->a.context->simultaneous_ssl_restriction,
|
|
|
|
// wsi->a.context->simultaneous_ssl);
|
2017-11-26 09:22:42 +08:00
|
|
|
|
2017-10-18 09:41:44 +08:00
|
|
|
return 1; /* handled */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lws_ssl_SSL_CTX_destroy(struct lws_vhost *vhost)
|
|
|
|
{
|
2018-05-01 12:41:42 +08:00
|
|
|
if (vhost->tls.ssl_ctx)
|
|
|
|
SSL_CTX_free(vhost->tls.ssl_ctx);
|
2017-10-18 09:41:44 +08:00
|
|
|
|
2020-06-17 11:13:01 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2019-03-17 10:03:22 +08:00
|
|
|
lws_ssl_destroy_client_ctx(vhost);
|
2020-06-17 11:13:01 +01:00
|
|
|
#endif
|
2019-03-17 10:03:22 +08:00
|
|
|
|
2017-11-14 11:25:54 +08:00
|
|
|
#if defined(LWS_WITH_ACME)
|
2017-10-28 11:33:34 +08:00
|
|
|
lws_tls_acme_sni_cert_destroy(vhost);
|
2017-11-14 11:25:54 +08:00
|
|
|
#endif
|
2017-10-18 09:41:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lws_ssl_context_destroy(struct lws_context *context)
|
|
|
|
{
|
|
|
|
// after 1.1.0 no need
|
|
|
|
#if (OPENSSL_VERSION_NUMBER < 0x10100000)
|
|
|
|
// <= 1.0.1f = old api, 1.0.1g+ = new api
|
|
|
|
#if (OPENSSL_VERSION_NUMBER <= 0x1000106f) || defined(USE_WOLFSSL)
|
|
|
|
ERR_remove_state(0);
|
|
|
|
#else
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x1010005f && \
|
|
|
|
!defined(LIBRESSL_VERSION_NUMBER) && \
|
|
|
|
!defined(OPENSSL_IS_BORINGSSL)
|
|
|
|
ERR_remove_thread_state();
|
|
|
|
#else
|
|
|
|
ERR_remove_thread_state(NULL);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
// after 1.1.0 no need
|
|
|
|
#if (OPENSSL_VERSION_NUMBER >= 0x10002000) && (OPENSSL_VERSION_NUMBER <= 0x10100000)
|
|
|
|
SSL_COMP_free_compression_methods();
|
|
|
|
#endif
|
|
|
|
ERR_free_strings();
|
|
|
|
EVP_cleanup();
|
|
|
|
CRYPTO_cleanup_all_ex_data();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
lws_tls_ctx *
|
|
|
|
lws_tls_ctx_from_wsi(struct lws *wsi)
|
|
|
|
{
|
2018-05-01 12:41:42 +08:00
|
|
|
if (!wsi->tls.ssl)
|
2018-02-27 07:48:25 +08:00
|
|
|
return NULL;
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
return SSL_get_SSL_CTX(wsi->tls.ssl);
|
2017-10-18 09:41:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
enum lws_ssl_capable_status
|
2018-03-11 11:26:06 +08:00
|
|
|
__lws_tls_shutdown(struct lws *wsi)
|
2017-10-18 09:41:44 +08:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
2021-07-20 10:18:59 +01:00
|
|
|
#ifndef WIN32
|
2019-07-10 20:34:24 +01:00
|
|
|
errno = 0;
|
2021-07-20 10:18:59 +01:00
|
|
|
#else
|
|
|
|
WSASetLastError(0);
|
|
|
|
#endif
|
2019-07-10 20:34:24 +01:00
|
|
|
ERR_clear_error();
|
2018-05-01 12:41:42 +08:00
|
|
|
n = SSL_shutdown(wsi->tls.ssl);
|
2017-10-18 09:41:44 +08:00
|
|
|
lwsl_debug("SSL_shutdown=%d for fd %d\n", n, wsi->desc.sockfd);
|
|
|
|
switch (n) {
|
|
|
|
case 1: /* successful completion */
|
|
|
|
n = shutdown(wsi->desc.sockfd, SHUT_WR);
|
|
|
|
return LWS_SSL_CAPABLE_DONE;
|
|
|
|
|
|
|
|
case 0: /* needs a retry */
|
2018-03-11 11:26:06 +08:00
|
|
|
__lws_change_pollfd(wsi, 0, LWS_POLLIN);
|
2017-10-18 09:41:44 +08:00
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
|
|
|
|
|
|
|
default: /* fatal error, or WANT */
|
2018-05-01 12:41:42 +08:00
|
|
|
n = SSL_get_error(wsi->tls.ssl, n);
|
2017-10-18 09:41:44 +08:00
|
|
|
if (n != SSL_ERROR_SYSCALL && n != SSL_ERROR_SSL) {
|
2018-05-01 12:41:42 +08:00
|
|
|
if (SSL_want_read(wsi->tls.ssl)) {
|
2017-10-18 09:41:44 +08:00
|
|
|
lwsl_debug("(wants read)\n");
|
2018-03-11 11:26:06 +08:00
|
|
|
__lws_change_pollfd(wsi, 0, LWS_POLLIN);
|
2017-10-18 09:41:44 +08:00
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE_READ;
|
|
|
|
}
|
2018-05-01 12:41:42 +08:00
|
|
|
if (SSL_want_write(wsi->tls.ssl)) {
|
2017-10-18 09:41:44 +08:00
|
|
|
lwsl_debug("(wants write)\n");
|
2018-03-11 11:26:06 +08:00
|
|
|
__lws_change_pollfd(wsi, 0, LWS_POLLOUT);
|
2017-10-18 09:41:44 +08:00
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE_WRITE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2017-10-26 09:54:25 +08:00
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
|
|
|
|
static int
|
|
|
|
tops_fake_POLLIN_for_buffered_openssl(struct lws_context_per_thread *pt)
|
|
|
|
{
|
|
|
|
return lws_tls_fake_POLLIN_for_buffered(pt);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct lws_tls_ops tls_ops_openssl = {
|
|
|
|
/* fake_POLLIN_for_buffered */ tops_fake_POLLIN_for_buffered_openssl,
|
|
|
|
};
|