2017-10-18 09:41:44 +08:00
|
|
|
/*
|
2019-08-14 10:44:14 +01:00
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
2017-10-18 09:41:44 +08:00
|
|
|
*
|
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"
|
2017-10-31 18:50:59 +08:00
|
|
|
#include <mbedtls/x509_csr.h>
|
2020-05-14 21:28:48 +01:00
|
|
|
#include <errno.h>
|
2017-10-18 09:41:44 +08:00
|
|
|
|
|
|
|
int
|
2017-11-06 06:28:55 +08:00
|
|
|
lws_tls_server_client_cert_verify_config(struct lws_vhost *vh)
|
2017-10-18 09:41:44 +08:00
|
|
|
{
|
2017-10-31 14:37:41 +01:00
|
|
|
int verify_options = SSL_VERIFY_PEER;
|
|
|
|
|
|
|
|
/* as a server, are we requiring clients to identify themselves? */
|
2017-11-06 06:28:55 +08:00
|
|
|
if (!lws_check_opt(vh->options,
|
|
|
|
LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT)) {
|
|
|
|
lwsl_notice("no client cert required\n");
|
2017-10-31 14:37:41 +01:00
|
|
|
return 0;
|
2017-11-06 06:28:55 +08:00
|
|
|
}
|
2017-10-31 14:37:41 +01:00
|
|
|
|
2021-05-23 08:17:25 +01:00
|
|
|
if (!lws_check_opt(vh->options, LWS_SERVER_OPTION_PEER_CERT_NOT_REQUIRED))
|
2024-10-30 22:19:37 +09:00
|
|
|
verify_options |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
|
2017-10-31 14:37:41 +01:00
|
|
|
|
2017-11-06 06:28:55 +08:00
|
|
|
lwsl_notice("%s: vh %s requires client cert %d\n", __func__, vh->name,
|
|
|
|
verify_options);
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
SSL_CTX_set_verify(vh->tls.ssl_ctx, verify_options, NULL);
|
2017-10-31 19:18:38 +08:00
|
|
|
|
2017-10-18 09:41:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-31 18:50:59 +08:00
|
|
|
static int
|
|
|
|
lws_mbedtls_sni_cb(void *arg, mbedtls_ssl_context *mbedtls_ctx,
|
|
|
|
const unsigned char *servername, size_t len)
|
|
|
|
{
|
|
|
|
SSL *ssl = SSL_SSL_from_mbedtls_ssl_context(mbedtls_ctx);
|
|
|
|
struct lws_context *context = (struct lws_context *)arg;
|
|
|
|
struct lws_vhost *vhost, *vh;
|
|
|
|
|
|
|
|
lwsl_notice("%s: %s\n", __func__, servername);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We can only get ssl accepted connections by using a vhost's ssl_ctx
|
|
|
|
* find out which listening one took us and only match vhosts on the
|
|
|
|
* same port.
|
|
|
|
*/
|
|
|
|
vh = context->vhost_list;
|
|
|
|
while (vh) {
|
|
|
|
if (!vh->being_destroyed &&
|
2018-05-01 12:41:42 +08:00
|
|
|
vh->tls.ssl_ctx == SSL_get_SSL_CTX(ssl))
|
2017-10-31 18:50:59 +08:00
|
|
|
break;
|
|
|
|
vh = vh->vhost_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!vh) {
|
|
|
|
assert(vh); /* can't match the incoming vh? */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
vhost = lws_select_vhost(context, vh->listen_port,
|
|
|
|
(const char *)servername);
|
|
|
|
if (!vhost) {
|
|
|
|
lwsl_info("SNI: none: %s:%d\n", servername, vh->listen_port);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-06 06:28:55 +08:00
|
|
|
lwsl_info("SNI: Found: %s:%d at vhost '%s'\n", servername,
|
|
|
|
vh->listen_port, vhost->name);
|
2017-10-31 18:50:59 +08:00
|
|
|
|
2019-03-16 09:54:52 +08:00
|
|
|
if (!vhost->tls.ssl_ctx) {
|
|
|
|
lwsl_err("%s: vhost %s matches SNI but no valid cert\n",
|
|
|
|
__func__, vh->name);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-10-31 18:50:59 +08:00
|
|
|
/* select the ssl ctx from the selected vhost for this conn */
|
2018-05-01 12:41:42 +08:00
|
|
|
SSL_set_SSL_CTX(ssl, vhost->tls.ssl_ctx);
|
2017-10-31 18:50:59 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-18 09:41:44 +08:00
|
|
|
int
|
2017-11-02 09:20:17 +08:00
|
|
|
lws_tls_server_certs_load(struct lws_vhost *vhost, struct lws *wsi,
|
|
|
|
const char *cert, const char *private_key,
|
2019-02-14 14:35:24 +08:00
|
|
|
const char *mem_cert, size_t mem_cert_len,
|
2017-11-02 09:20:17 +08:00
|
|
|
const char *mem_privkey, size_t mem_privkey_len)
|
2017-10-18 09:41:44 +08:00
|
|
|
{
|
|
|
|
lws_filepos_t flen;
|
2019-02-14 14:35:24 +08:00
|
|
|
uint8_t *p = NULL;
|
2017-11-02 09:20:17 +08:00
|
|
|
long err;
|
2019-02-14 14:35:24 +08:00
|
|
|
int n;
|
2017-10-18 09:41:44 +08:00
|
|
|
|
2018-05-05 10:16:54 +08:00
|
|
|
if ((!cert || !private_key) && (!mem_cert || !mem_privkey)) {
|
|
|
|
lwsl_notice("%s: no usable input\n", __func__);
|
2017-11-08 14:21:03 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
n = (int)lws_tls_generic_cert_checks(vhost, cert, private_key);
|
2017-11-08 14:21:03 +08:00
|
|
|
|
2017-11-02 09:20:17 +08:00
|
|
|
if (n == LWS_TLS_EXTANT_NO && (!mem_cert || !mem_privkey))
|
2017-10-18 09:41:44 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
2017-11-02 09:20:17 +08:00
|
|
|
* we can't read the root-privs files. But if mem_cert is provided,
|
|
|
|
* we should use that.
|
2017-10-18 09:41:44 +08:00
|
|
|
*/
|
2017-11-02 09:20:17 +08:00
|
|
|
if (n == LWS_TLS_EXTANT_NO)
|
|
|
|
n = LWS_TLS_EXTANT_ALTERNATIVE;
|
|
|
|
|
|
|
|
if (n == LWS_TLS_EXTANT_ALTERNATIVE && (!mem_cert || !mem_privkey))
|
|
|
|
return 1; /* no alternative */
|
|
|
|
|
|
|
|
if (n == LWS_TLS_EXTANT_ALTERNATIVE) {
|
|
|
|
/*
|
|
|
|
* Although we have prepared update certs, we no longer have
|
|
|
|
* the rights to read our own cert + key we saved.
|
|
|
|
*
|
|
|
|
* If we were passed copies in memory buffers, use those
|
|
|
|
* instead.
|
|
|
|
*
|
|
|
|
* The passed memory-buffer cert image is in DER, and the
|
|
|
|
* memory-buffer private key image is PEM.
|
|
|
|
*/
|
2019-02-14 14:35:24 +08:00
|
|
|
cert = NULL;
|
|
|
|
private_key = NULL;
|
2017-10-18 09:41:44 +08:00
|
|
|
}
|
2019-02-14 14:35:24 +08:00
|
|
|
if (lws_tls_alloc_pem_to_der_file(vhost->context, cert, mem_cert,
|
|
|
|
mem_cert_len, &p, &flen)) {
|
2022-03-06 15:54:01 +00:00
|
|
|
lwsl_err("couldn't load cert file %s\n", cert);
|
2019-02-14 14:35:24 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
err = SSL_CTX_use_certificate_ASN1(vhost->tls.ssl_ctx, (int)flen, p);
|
2019-02-14 14:35:24 +08:00
|
|
|
lws_free_set_NULL(p);
|
2017-10-18 09:41:44 +08:00
|
|
|
if (!err) {
|
|
|
|
lwsl_err("Problem loading cert\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2017-11-02 09:20:17 +08:00
|
|
|
|
2019-02-14 14:35:24 +08:00
|
|
|
if (lws_tls_alloc_pem_to_der_file(vhost->context, private_key,
|
|
|
|
(char *)mem_privkey, mem_privkey_len,
|
|
|
|
&p, &flen)) {
|
|
|
|
lwsl_err("couldn't find private key\n");
|
2017-10-18 09:41:44 +08:00
|
|
|
|
2019-02-14 14:35:24 +08:00
|
|
|
return 1;
|
2017-10-18 09:41:44 +08:00
|
|
|
}
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
err = SSL_CTX_use_PrivateKey_ASN1(0, vhost->tls.ssl_ctx, p, (long)flen);
|
2019-02-14 14:35:24 +08:00
|
|
|
lws_free_set_NULL(p);
|
|
|
|
if (!err) {
|
|
|
|
lwsl_err("Problem loading key\n");
|
|
|
|
|
|
|
|
return 1;
|
2017-11-02 09:20:17 +08:00
|
|
|
}
|
2017-10-18 09:41:44 +08:00
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
vhost->tls.skipped_certs = 0;
|
2017-11-02 09:20:17 +08:00
|
|
|
|
2017-10-18 09:41:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-02 09:20:17 +08:00
|
|
|
int
|
2018-04-27 08:27:16 +08:00
|
|
|
lws_tls_server_vhost_backend_init(const struct lws_context_creation_info *info,
|
2017-11-02 09:20:17 +08:00
|
|
|
struct lws_vhost *vhost, struct lws *wsi)
|
|
|
|
{
|
|
|
|
const SSL_METHOD *method = TLS_server_method();
|
2017-11-06 06:28:55 +08:00
|
|
|
uint8_t *p;
|
|
|
|
lws_filepos_t flen;
|
2017-11-26 09:22:42 +08:00
|
|
|
int n;
|
2017-11-02 09:20:17 +08:00
|
|
|
|
2021-07-09 18:04:08 +01:00
|
|
|
vhost->tls.ssl_ctx = SSL_CTX_new(method, &vhost->context->mcdc); /* create context */
|
2018-05-01 12:41:42 +08:00
|
|
|
if (!vhost->tls.ssl_ctx) {
|
2017-11-02 09:20:17 +08:00
|
|
|
lwsl_err("problem creating ssl context\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-02-14 14:35:24 +08:00
|
|
|
if (!vhost->tls.use_ssl ||
|
|
|
|
(!info->ssl_cert_filepath && !info->server_ssl_cert_mem))
|
2017-11-02 09:20:17 +08:00
|
|
|
return 0;
|
|
|
|
|
2017-11-06 06:28:55 +08:00
|
|
|
if (info->ssl_ca_filepath) {
|
|
|
|
lwsl_notice("%s: vh %s: loading CA filepath %s\n", __func__,
|
|
|
|
vhost->name, info->ssl_ca_filepath);
|
|
|
|
if (lws_tls_alloc_pem_to_der_file(vhost->context,
|
|
|
|
info->ssl_ca_filepath, NULL, 0, &p, &flen)) {
|
|
|
|
lwsl_err("couldn't find client CA file %s\n",
|
|
|
|
info->ssl_ca_filepath);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
if (SSL_CTX_add_client_CA_ASN1(vhost->tls.ssl_ctx, (int)flen, p) != 1) {
|
2017-11-06 06:28:55 +08:00
|
|
|
lwsl_err("%s: SSL_CTX_add_client_CA_ASN1 unhappy\n",
|
|
|
|
__func__);
|
|
|
|
free(p);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
free(p);
|
2019-02-14 14:35:24 +08:00
|
|
|
} else {
|
|
|
|
if (info->server_ssl_ca_mem && info->server_ssl_ca_mem_len &&
|
2019-05-10 06:29:35 +01:00
|
|
|
SSL_CTX_add_client_CA_ASN1(vhost->tls.ssl_ctx,
|
|
|
|
(int)info->server_ssl_ca_mem_len,
|
|
|
|
info->server_ssl_ca_mem) != 1) {
|
2019-02-14 14:35:24 +08:00
|
|
|
lwsl_err("%s: mem SSL_CTX_add_client_CA_ASN1 unhappy\n",
|
|
|
|
__func__);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
lwsl_notice("%s: vh %s: mem CA OK\n", __func__, vhost->name);
|
2017-11-06 06:28:55 +08:00
|
|
|
}
|
|
|
|
|
2017-11-26 09:22:42 +08:00
|
|
|
n = lws_tls_server_certs_load(vhost, wsi, info->ssl_cert_filepath,
|
2019-02-14 14:35:24 +08:00
|
|
|
info->ssl_private_key_filepath,
|
|
|
|
info->server_ssl_cert_mem,
|
|
|
|
info->server_ssl_cert_mem_len,
|
|
|
|
info->server_ssl_private_key_mem,
|
|
|
|
info->server_ssl_private_key_mem_len);
|
2017-11-26 09:22:42 +08:00
|
|
|
if (n)
|
|
|
|
return n;
|
|
|
|
|
|
|
|
return 0;
|
2017-11-02 09:20:17 +08:00
|
|
|
}
|
|
|
|
|
2017-10-18 09:41:44 +08:00
|
|
|
int
|
|
|
|
lws_tls_server_new_nonblocking(struct lws *wsi, lws_sockfd_type accept_fd)
|
|
|
|
{
|
|
|
|
errno = 0;
|
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->tls.ssl = SSL_new(wsi->a.vhost->tls.ssl_ctx);
|
2018-05-01 12:41:42 +08:00
|
|
|
if (wsi->tls.ssl == NULL) {
|
2017-10-18 09:41:44 +08:00
|
|
|
lwsl_err("SSL_new failed: errno %d\n", errno);
|
|
|
|
|
2019-07-13 13:28:14 -07:00
|
|
|
lws_tls_err_describe_clear();
|
2017-10-18 09:41:44 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:28:21 +00:00
|
|
|
SSL_set_fd(wsi->tls.ssl, (int)accept_fd);
|
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
|
|
|
if (wsi->a.vhost->tls.ssl_info_event_mask)
|
2018-05-01 12:41:42 +08:00
|
|
|
SSL_set_info_callback(wsi->tls.ssl, lws_ssl_info_callback);
|
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
|
|
|
SSL_set_sni_callback(wsi->tls.ssl, lws_mbedtls_sni_cb, wsi->a.context);
|
2017-10-31 18:50:59 +08:00
|
|
|
|
2017-10-18 09:41:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-05 05:04:17 +01:00
|
|
|
enum lws_ssl_capable_status
|
2017-10-18 09:41:44 +08:00
|
|
|
lws_tls_server_abort_connection(struct lws *wsi)
|
|
|
|
{
|
2021-09-28 14:56:22 +08:00
|
|
|
if (wsi->tls.use_ssl)
|
|
|
|
__lws_tls_shutdown(wsi);
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
SSL_free(wsi->tls.ssl);
|
2017-10-18 09:41:44 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum lws_ssl_capable_status
|
|
|
|
lws_tls_server_accept(struct lws *wsi)
|
|
|
|
{
|
2017-10-26 09:54:25 +08:00
|
|
|
union lws_tls_cert_info_results ir;
|
2017-11-26 09:22:42 +08:00
|
|
|
int m, n;
|
2017-10-18 09:41:44 +08:00
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
n = SSL_accept(wsi->tls.ssl);
|
2020-03-06 10:16:12 +00:00
|
|
|
|
|
|
|
wsi->skip_fallback = 1;
|
2017-10-26 09:54:25 +08:00
|
|
|
if (n == 1) {
|
2017-11-26 09:22:42 +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
|
|
|
if (strstr(wsi->a.vhost->name, ".invalid")) {
|
2018-11-23 08:47:56 +08:00
|
|
|
lwsl_notice("%s: vhost has .invalid, "
|
|
|
|
"rejecting accept\n", __func__);
|
2017-11-26 09:22:42 +08:00
|
|
|
|
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
|
|
|
}
|
|
|
|
|
2018-11-23 08:47:56 +08:00
|
|
|
n = lws_tls_peer_cert_info(wsi, LWS_TLS_CERT_INFO_COMMON_NAME,
|
|
|
|
&ir, sizeof(ir.ns.name));
|
2017-10-26 09:54:25 +08:00
|
|
|
if (!n)
|
|
|
|
lwsl_notice("%s: client cert CN '%s'\n",
|
|
|
|
__func__, ir.ns.name);
|
|
|
|
else
|
2018-11-23 08:47:56 +08:00
|
|
|
lwsl_info("%s: couldn't get client cert CN\n",
|
|
|
|
__func__);
|
2017-10-18 09:41:44 +08:00
|
|
|
return LWS_SSL_CAPABLE_DONE;
|
2017-10-26 09:54:25 +08:00
|
|
|
}
|
2017-10-18 09:41:44 +08:00
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
m = SSL_get_error(wsi->tls.ssl, n);
|
2021-01-18 14:20:37 +00:00
|
|
|
lwsl_debug("%s: %s: accept SSL_get_error %d errno %d\n", __func__,
|
2020-12-25 05:54:19 +00:00
|
|
|
lws_wsi_tag(wsi), m, errno);
|
2017-10-18 09:41:44 +08:00
|
|
|
|
|
|
|
// mbedtls wrapper only
|
|
|
|
if (m == SSL_ERROR_SYSCALL && errno == 11)
|
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE_READ;
|
|
|
|
|
2020-04-16 14:16:29 +01:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
if (m == SSL_ERROR_SYSCALL && errno == 35)
|
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE_READ;
|
|
|
|
#endif
|
|
|
|
|
2019-02-07 06:24:14 +08:00
|
|
|
#if defined(WIN32)
|
|
|
|
if (m == SSL_ERROR_SYSCALL && errno == 0)
|
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE_READ;
|
|
|
|
#endif
|
|
|
|
|
2017-10-18 09:41:44 +08:00
|
|
|
if (m == SSL_ERROR_SYSCALL || m == SSL_ERROR_SSL)
|
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
|
|
|
|
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
|
|
|
if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) {
|
2018-11-23 08:47:56 +08:00
|
|
|
lwsl_info("%s: WANT_READ change_pollfd failed\n",
|
|
|
|
__func__);
|
2017-10-18 09:41:44 +08:00
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
lwsl_info("SSL_ERROR_WANT_READ\n");
|
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE_READ;
|
|
|
|
}
|
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
|
|
|
lwsl_debug("%s: WANT_WRITE\n", __func__);
|
|
|
|
|
|
|
|
if (lws_change_pollfd(wsi, 0, LWS_POLLOUT)) {
|
2018-11-23 08:47:56 +08:00
|
|
|
lwsl_info("%s: WANT_WRITE change_pollfd failed\n",
|
|
|
|
__func__);
|
2017-10-18 09:41:44 +08:00
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
|
|
|
}
|
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE_WRITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
|
|
|
}
|
|
|
|
|
2017-11-14 11:25:54 +08:00
|
|
|
#if defined(LWS_WITH_ACME)
|
2017-10-31 18:50:59 +08:00
|
|
|
/*
|
|
|
|
* mbedtls doesn't support SAN for cert creation. So we use a known-good
|
|
|
|
* tls-sni-01 cert from OpenSSL that worked on Let's Encrypt, and just replace
|
|
|
|
* the pubkey n part and the signature part.
|
|
|
|
*
|
|
|
|
* This will need redoing for tls-sni-02...
|
|
|
|
*/
|
2017-10-18 09:41:44 +08:00
|
|
|
|
2017-10-31 18:50:59 +08:00
|
|
|
static uint8_t ss_cert_leadin[] = {
|
|
|
|
0x30, 0x82,
|
2017-11-26 18:38:37 +08:00
|
|
|
0x05, 0x56, /* total length: LEN1 (+2 / +3) (correct for 513 + 512)*/
|
2017-10-31 18:50:59 +08:00
|
|
|
|
2017-11-26 18:38:37 +08:00
|
|
|
0x30, 0x82, /* length: LEN2 (+6 / +7) (correct for 513) */
|
|
|
|
0x03, 0x3e,
|
2017-10-31 18:50:59 +08:00
|
|
|
|
|
|
|
/* addition: v3 cert (+5 bytes)*/
|
|
|
|
0xa0, 0x03,
|
|
|
|
0x02, 0x01, 0x02,
|
|
|
|
|
|
|
|
0x02, 0x01, 0x01,
|
|
|
|
0x30, 0x0d, 0x06, 0x09, 0x2a,
|
|
|
|
0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x3f,
|
|
|
|
0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x47,
|
|
|
|
0x42, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0b,
|
|
|
|
0x73, 0x6f, 0x6d, 0x65, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x6e, 0x79, 0x31,
|
|
|
|
0x1a, 0x30, 0x18, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x11, 0x74, 0x65,
|
|
|
|
0x6d, 0x70, 0x2e, 0x61, 0x63, 0x6d, 0x65, 0x2e, 0x69, 0x6e, 0x76, 0x61,
|
|
|
|
0x6c, 0x69, 0x64, 0x30, 0x1e, 0x17, 0x0d,
|
|
|
|
|
|
|
|
/* from 2017-10-29 ... */
|
|
|
|
0x31, 0x37, 0x31, 0x30, 0x32, 0x39, 0x31, 0x31, 0x34, 0x39, 0x34, 0x35,
|
|
|
|
0x5a, 0x17, 0x0d,
|
|
|
|
|
2017-11-26 18:38:37 +08:00
|
|
|
/* thru 2049-10-29 we immediately discard the private key, no worries */
|
2017-10-31 18:50:59 +08:00
|
|
|
0x34, 0x39, 0x31, 0x30, 0x32, 0x39, 0x31, 0x32, 0x34, 0x39, 0x34, 0x35,
|
|
|
|
0x5a,
|
|
|
|
|
|
|
|
0x30, 0x3f, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
|
|
|
|
0x02, 0x47, 0x42, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a,
|
|
|
|
0x0c, 0x0b, 0x73, 0x6f, 0x6d, 0x65, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x6e,
|
|
|
|
0x79, 0x31, 0x1a, 0x30, 0x18, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x11,
|
|
|
|
0x74, 0x65, 0x6d, 0x70, 0x2e, 0x61, 0x63, 0x6d, 0x65, 0x2e, 0x69, 0x6e,
|
2017-11-26 18:38:37 +08:00
|
|
|
0x76, 0x61, 0x6c, 0x69, 0x64, 0x30,
|
|
|
|
|
|
|
|
0x82,
|
|
|
|
0x02, 0x22, /* LEN3 (+C3 / C4) */
|
|
|
|
0x30, 0x0d, 0x06,
|
2017-10-31 18:50:59 +08:00
|
|
|
0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00,
|
2017-11-26 18:38:37 +08:00
|
|
|
0x03,
|
|
|
|
|
|
|
|
0x82,
|
|
|
|
0x02, 0x0f, /* LEN4 (+D6 / D7) */
|
|
|
|
|
|
|
|
0x00, 0x30, 0x82,
|
|
|
|
|
|
|
|
0x02, 0x0a, /* LEN5 (+ DB / DC) */
|
|
|
|
|
|
|
|
0x02, 0x82,
|
2017-10-31 18:50:59 +08:00
|
|
|
|
2017-11-26 18:38:37 +08:00
|
|
|
//0x02, 0x01, /* length of n in bytes (including leading 00 if any) */
|
2017-10-31 18:50:59 +08:00
|
|
|
},
|
|
|
|
|
2017-11-26 18:38:37 +08:00
|
|
|
/* 1 + (keybits / 8) bytes N */
|
2017-10-31 18:50:59 +08:00
|
|
|
|
|
|
|
ss_cert_san_leadin[] = {
|
|
|
|
/* e - fixed */
|
|
|
|
0x02, 0x03, 0x01, 0x00, 0x01,
|
|
|
|
|
|
|
|
0xa3, 0x5d, 0x30, 0x5b, 0x30, 0x59, 0x06, 0x03, 0x55, 0x1d,
|
|
|
|
0x11, 0x04, 0x52, 0x30, 0x50, /* <-- SAN length + 2 */
|
|
|
|
|
|
|
|
0x82, 0x4e, /* <-- SAN length */
|
|
|
|
},
|
|
|
|
|
|
|
|
/* 78 bytes of SAN (tls-sni-01)
|
|
|
|
0x61, 0x64, 0x34, 0x31, 0x61, 0x66, 0x62, 0x65, 0x30, 0x63, 0x61, 0x34,
|
|
|
|
0x36, 0x34, 0x32, 0x66, 0x30, 0x61, 0x34, 0x34, 0x39, 0x64, 0x39, 0x63,
|
|
|
|
0x61, 0x37, 0x36, 0x65, 0x62, 0x61, 0x61, 0x62, 0x2e, 0x32, 0x38, 0x39,
|
|
|
|
0x34, 0x64, 0x34, 0x31, 0x36, 0x63, 0x39, 0x38, 0x33, 0x66, 0x31, 0x32,
|
|
|
|
0x65, 0x64, 0x37, 0x33, 0x31, 0x61, 0x33, 0x30, 0x66, 0x35, 0x63, 0x34,
|
|
|
|
0x34, 0x37, 0x37, 0x66, 0x65, 0x2e, 0x61, 0x63, 0x6d, 0x65, 0x2e, 0x69,
|
|
|
|
0x6e, 0x76, 0x61, 0x6c, 0x69, 0x64, */
|
2017-11-26 18:38:37 +08:00
|
|
|
|
|
|
|
/* end of LEN2 area */
|
|
|
|
|
2017-10-31 18:50:59 +08:00
|
|
|
ss_cert_sig_leadin[] = {
|
|
|
|
/* it's saying that the signature is SHA256 + RSA */
|
|
|
|
0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
|
2017-11-26 18:38:37 +08:00
|
|
|
0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
|
|
|
|
|
|
|
|
0x82,
|
|
|
|
0x02, 0x01,
|
|
|
|
0x00,
|
2017-10-31 18:50:59 +08:00
|
|
|
};
|
|
|
|
|
2017-11-26 18:38:37 +08:00
|
|
|
/* (keybits / 8) bytes signature to end of LEN1 area */
|
|
|
|
|
|
|
|
#define SAN_A_LENGTH 78
|
2017-10-28 11:33:34 +08:00
|
|
|
|
2020-01-02 08:32:23 +00:00
|
|
|
int
|
2017-10-28 11:33:34 +08:00
|
|
|
lws_tls_acme_sni_cert_create(struct lws_vhost *vhost, const char *san_a,
|
|
|
|
const char *san_b)
|
|
|
|
{
|
2017-10-31 18:50:59 +08:00
|
|
|
int buflen = 0x560;
|
2020-12-12 06:21:40 +00:00
|
|
|
uint8_t *buf = lws_malloc((unsigned int)buflen, "tmp cert buf"), *p = buf, *pkey_asn1;
|
2017-10-31 18:50:59 +08:00
|
|
|
struct lws_genrsa_ctx ctx;
|
2019-11-06 16:02:33 +00:00
|
|
|
struct lws_gencrypto_keyelem el[LWS_GENCRYPTO_RSA_KEYEL_COUNT];
|
2017-10-31 18:50:59 +08:00
|
|
|
uint8_t digest[32];
|
|
|
|
struct lws_genhash_ctx hash_ctx;
|
|
|
|
int pkey_asn1_len = 3 * 1024;
|
2017-11-26 09:22:42 +08:00
|
|
|
int n, m, keybits = lws_plat_recommended_rsa_bits(), adj;
|
2017-10-31 18:50:59 +08:00
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
return 1;
|
|
|
|
|
2019-11-06 16:02:33 +00:00
|
|
|
n = lws_genrsa_new_keypair(vhost->context, &ctx, LGRSAM_PKCS1_1_5,
|
|
|
|
&el[0], keybits);
|
2017-10-31 18:50:59 +08:00
|
|
|
if (n < 0) {
|
2019-11-06 16:02:33 +00:00
|
|
|
lws_genrsa_destroy_elements(&el[0]);
|
2017-10-31 18:50:59 +08:00
|
|
|
goto bail1;
|
|
|
|
}
|
|
|
|
|
2017-11-26 18:38:37 +08:00
|
|
|
n = sizeof(ss_cert_leadin);
|
2020-12-12 06:21:40 +00:00
|
|
|
memcpy(p, ss_cert_leadin, (unsigned int)n);
|
2017-11-26 18:38:37 +08:00
|
|
|
p += n;
|
|
|
|
|
|
|
|
adj = (0x0556 - 0x401) + (keybits / 4) + 1;
|
2020-12-12 06:21:40 +00:00
|
|
|
buf[2] = (uint8_t)(adj >> 8);
|
|
|
|
buf[3] = (uint8_t)(adj & 0xff);
|
2017-11-26 18:38:37 +08:00
|
|
|
|
|
|
|
adj = (0x033e - 0x201) + (keybits / 8) + 1;
|
2020-12-12 06:21:40 +00:00
|
|
|
buf[6] = (uint8_t)(adj >> 8);
|
|
|
|
buf[7] = (uint8_t)(adj & 0xff);
|
2017-11-26 18:38:37 +08:00
|
|
|
|
|
|
|
adj = (0x0222 - 0x201) + (keybits / 8) + 1;
|
2020-12-12 06:21:40 +00:00
|
|
|
buf[0xc3] = (uint8_t)(adj >> 8);
|
|
|
|
buf[0xc4] = (uint8_t)(adj & 0xff);
|
2017-11-26 18:38:37 +08:00
|
|
|
|
|
|
|
adj = (0x020f - 0x201) + (keybits / 8) + 1;
|
2020-12-12 06:21:40 +00:00
|
|
|
buf[0xd6] = (uint8_t)(adj >> 8);
|
|
|
|
buf[0xd7] = (uint8_t)(adj & 0xff);
|
2017-11-26 18:38:37 +08:00
|
|
|
|
|
|
|
adj = (0x020a - 0x201) + (keybits / 8) + 1;
|
2020-12-12 06:21:40 +00:00
|
|
|
buf[0xdb] = (uint8_t)(adj >> 8);
|
|
|
|
buf[0xdc] = (uint8_t)(adj & 0xff);
|
2017-10-31 18:50:59 +08:00
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
*p++ = (uint8_t)(((keybits / 8) + 1) >> 8);
|
|
|
|
*p++ = (uint8_t)(((keybits / 8) + 1) & 0xff);
|
2017-11-26 18:38:37 +08:00
|
|
|
|
|
|
|
/* we need to drop 1 + (keybits / 8) bytes of n in here, 00 + key */
|
2017-10-31 18:50:59 +08:00
|
|
|
|
|
|
|
*p++ = 0x00;
|
2019-11-06 16:02:33 +00:00
|
|
|
memcpy(p, el[LWS_GENCRYPTO_RSA_KEYEL_N].buf, el[LWS_GENCRYPTO_RSA_KEYEL_N].len);
|
|
|
|
p += el[LWS_GENCRYPTO_RSA_KEYEL_N].len;
|
2017-10-31 18:50:59 +08:00
|
|
|
|
|
|
|
memcpy(p, ss_cert_san_leadin, sizeof(ss_cert_san_leadin));
|
|
|
|
p += sizeof(ss_cert_san_leadin);
|
|
|
|
|
|
|
|
/* drop in 78 bytes of san_a */
|
|
|
|
|
2017-11-26 18:38:37 +08:00
|
|
|
memcpy(p, san_a, SAN_A_LENGTH);
|
|
|
|
p += SAN_A_LENGTH;
|
2017-10-31 18:50:59 +08:00
|
|
|
memcpy(p, ss_cert_sig_leadin, sizeof(ss_cert_sig_leadin));
|
2017-11-26 18:38:37 +08:00
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
p[17] = (uint8_t)(((keybits / 8) + 1) >> 8);
|
|
|
|
p[18] = (uint8_t)(((keybits / 8) + 1) & 0xff);
|
2017-11-26 18:38:37 +08:00
|
|
|
|
2017-10-31 18:50:59 +08:00
|
|
|
p += sizeof(ss_cert_sig_leadin);
|
|
|
|
|
|
|
|
/* hash the cert plaintext */
|
|
|
|
|
|
|
|
if (lws_genhash_init(&hash_ctx, LWS_GENHASH_TYPE_SHA256))
|
|
|
|
goto bail2;
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
if (lws_genhash_update(&hash_ctx, buf, lws_ptr_diff_size_t(p, buf))) {
|
2017-10-31 18:50:59 +08:00
|
|
|
lws_genhash_destroy(&hash_ctx, NULL);
|
|
|
|
|
|
|
|
goto bail2;
|
|
|
|
}
|
|
|
|
if (lws_genhash_destroy(&hash_ctx, digest))
|
|
|
|
goto bail2;
|
|
|
|
|
|
|
|
/* sign the hash */
|
|
|
|
|
2018-12-05 13:11:03 +08:00
|
|
|
n = lws_genrsa_hash_sign(&ctx, digest, LWS_GENHASH_TYPE_SHA256, p,
|
2020-12-12 06:21:40 +00:00
|
|
|
(size_t)((size_t)buflen - lws_ptr_diff_size_t(p, buf)));
|
2017-10-31 18:50:59 +08:00
|
|
|
if (n < 0)
|
|
|
|
goto bail2;
|
|
|
|
p += n;
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
pkey_asn1 = lws_malloc((unsigned int)pkey_asn1_len, "mbed crt tmp");
|
2017-10-31 18:50:59 +08:00
|
|
|
if (!pkey_asn1)
|
|
|
|
goto bail2;
|
2017-10-28 11:33:34 +08:00
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
m = lws_genrsa_render_pkey_asn1(&ctx, 1, pkey_asn1, (size_t)pkey_asn1_len);
|
2017-11-26 09:22:42 +08:00
|
|
|
if (m < 0) {
|
2017-10-31 18:50:59 +08:00
|
|
|
lws_free(pkey_asn1);
|
|
|
|
goto bail2;
|
|
|
|
}
|
|
|
|
|
2017-11-26 09:22:42 +08:00
|
|
|
// lwsl_hexdump_level(LLL_DEBUG, buf, lws_ptr_diff(p, buf));
|
2018-05-01 12:41:42 +08:00
|
|
|
n = SSL_CTX_use_certificate_ASN1(vhost->tls.ssl_ctx,
|
2017-11-26 09:22:42 +08:00
|
|
|
lws_ptr_diff(p, buf), buf);
|
2017-10-31 18:50:59 +08:00
|
|
|
if (n != 1) {
|
2017-11-26 09:22:42 +08:00
|
|
|
lws_free(pkey_asn1);
|
|
|
|
lwsl_err("%s: generated cert failed to load 0x%x\n",
|
|
|
|
__func__, -n);
|
|
|
|
} else {
|
|
|
|
//lwsl_debug("private key\n");
|
|
|
|
//lwsl_hexdump_level(LLL_DEBUG, pkey_asn1, n);
|
|
|
|
|
|
|
|
/* and to use our generated private key */
|
2018-11-23 08:47:56 +08:00
|
|
|
n = SSL_CTX_use_PrivateKey_ASN1(0, vhost->tls.ssl_ctx,
|
|
|
|
pkey_asn1, m);
|
2017-11-26 09:22:42 +08:00
|
|
|
lws_free(pkey_asn1);
|
|
|
|
if (n != 1) {
|
|
|
|
lwsl_err("%s: SSL_CTX_use_PrivateKey_ASN1 failed\n",
|
|
|
|
__func__);
|
|
|
|
}
|
2017-10-31 18:50:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
lws_genrsa_destroy(&ctx);
|
2019-11-06 16:02:33 +00:00
|
|
|
lws_genrsa_destroy_elements(&el[0]);
|
2017-10-31 18:50:59 +08:00
|
|
|
|
|
|
|
lws_free(buf);
|
|
|
|
|
|
|
|
return n != 1;
|
|
|
|
|
|
|
|
bail2:
|
|
|
|
lws_genrsa_destroy(&ctx);
|
2019-11-06 16:02:33 +00:00
|
|
|
lws_genrsa_destroy_elements(&el[0]);
|
2017-10-31 18:50:59 +08:00
|
|
|
bail1:
|
|
|
|
lws_free(buf);
|
|
|
|
|
|
|
|
return -1;
|
2017-10-28 11:33:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lws_tls_acme_sni_cert_destroy(struct lws_vhost *vhost)
|
|
|
|
{
|
|
|
|
}
|
2017-10-31 18:50:59 +08:00
|
|
|
|
2018-12-13 20:05:12 +08:00
|
|
|
#if defined(LWS_WITH_JOSE)
|
2017-10-31 18:50:59 +08:00
|
|
|
static int
|
|
|
|
_rngf(void *context, unsigned char *buf, size_t len)
|
|
|
|
{
|
|
|
|
if ((size_t)lws_get_random(context, buf, len) == len)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-11-26 09:22:42 +08:00
|
|
|
static const char *x5[] = { "C", "ST", "L", "O", "CN" };
|
|
|
|
|
2017-10-31 18:50:59 +08:00
|
|
|
/*
|
|
|
|
* CSR is output formatted as b64url(DER)
|
|
|
|
* Private key is output as a PEM in memory
|
|
|
|
*/
|
2020-01-02 08:32:23 +00:00
|
|
|
int
|
2017-10-31 18:50:59 +08:00
|
|
|
lws_tls_acme_sni_csr_create(struct lws_context *context, const char *elements[],
|
|
|
|
uint8_t *dcsr, size_t csr_len, char **privkey_pem,
|
|
|
|
size_t *privkey_len)
|
|
|
|
{
|
|
|
|
mbedtls_x509write_csr csr;
|
|
|
|
mbedtls_pk_context mpk;
|
|
|
|
int buf_size = 4096, n;
|
2017-11-26 09:22:42 +08:00
|
|
|
char subject[200], *p = subject, *end = p + sizeof(subject) - 1;
|
2020-12-12 06:21:40 +00:00
|
|
|
uint8_t *buf = malloc((unsigned int)buf_size); /* malloc because given to user code */
|
2017-10-31 18:50:59 +08:00
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
mbedtls_x509write_csr_init(&csr);
|
|
|
|
|
|
|
|
mbedtls_pk_init(&mpk);
|
|
|
|
if (mbedtls_pk_setup(&mpk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA))) {
|
|
|
|
lwsl_notice("%s: pk_setup failed\n", __func__);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = mbedtls_rsa_gen_key(mbedtls_pk_rsa(mpk), _rngf, context,
|
2020-12-12 06:21:40 +00:00
|
|
|
(unsigned int)lws_plat_recommended_rsa_bits(), 65537);
|
2017-10-31 18:50:59 +08:00
|
|
|
if (n) {
|
|
|
|
lwsl_notice("%s: failed to generate keys\n", __func__);
|
|
|
|
|
|
|
|
goto fail1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* subject must be formatted like "C=TW,O=warmcat,CN=myserver" */
|
|
|
|
|
2018-08-16 19:10:32 +08:00
|
|
|
for (n = 0; n < (int)LWS_ARRAY_SIZE(x5); n++) {
|
2017-11-26 09:22:42 +08:00
|
|
|
if (p != subject)
|
|
|
|
*p++ = ',';
|
|
|
|
if (elements[n])
|
2020-12-12 06:21:40 +00:00
|
|
|
p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), "%s=%s", x5[n],
|
2017-11-26 09:22:42 +08:00
|
|
|
elements[n]);
|
|
|
|
}
|
|
|
|
|
2017-10-31 18:50:59 +08:00
|
|
|
if (mbedtls_x509write_csr_set_subject_name(&csr, subject))
|
|
|
|
goto fail1;
|
|
|
|
|
|
|
|
mbedtls_x509write_csr_set_key(&csr, &mpk);
|
|
|
|
mbedtls_x509write_csr_set_md_alg(&csr, MBEDTLS_MD_SHA256);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* data is written at the end of the buffer! Use the
|
|
|
|
* return value to determine where you should start
|
|
|
|
* using the buffer
|
|
|
|
*/
|
2020-12-12 06:21:40 +00:00
|
|
|
n = mbedtls_x509write_csr_der(&csr, buf, (size_t)buf_size, _rngf, context);
|
2017-10-31 18:50:59 +08:00
|
|
|
if (n < 0) {
|
|
|
|
lwsl_notice("%s: write csr der failed\n", __func__);
|
|
|
|
goto fail1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we have it in DER, we need it in b64URL */
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
n = lws_jws_base64_enc((char *)(buf + buf_size) - n, (size_t)n,
|
2017-10-31 18:50:59 +08:00
|
|
|
(char *)dcsr, csr_len);
|
|
|
|
if (n < 0)
|
|
|
|
goto fail1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* okay, the CSR is done, last we need the private key in PEM
|
|
|
|
* re-use the DER CSR buf as the result buffer since we cn do it in
|
|
|
|
* one step
|
|
|
|
*/
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
if (mbedtls_pk_write_key_pem(&mpk, buf, (size_t)buf_size)) {
|
2017-10-31 18:50:59 +08:00
|
|
|
lwsl_notice("write key pem failed\n");
|
|
|
|
goto fail1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*privkey_pem = (char *)buf;
|
|
|
|
*privkey_len = strlen((const char *)buf);
|
|
|
|
|
|
|
|
mbedtls_pk_free(&mpk);
|
|
|
|
mbedtls_x509write_csr_free(&csr);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
|
|
|
|
fail1:
|
|
|
|
mbedtls_pk_free(&mpk);
|
|
|
|
fail:
|
|
|
|
mbedtls_x509write_csr_free(&csr);
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2017-11-14 11:25:54 +08:00
|
|
|
#endif
|