Skip to content

Commit 27ec9f6

Browse files
committed
bearssl
BearSSL just doesn't address DTLS. Otherwise it should be a full client-server TLS implementation.
1 parent 7d80570 commit 27ec9f6

41 files changed

Lines changed: 2970 additions & 33 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.sai.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@
116116
"cmake": "-DLWS_WITH_GNUTLS=1",
117117
"platforms": "none, rocky9/aarch64-a72a55-rk3588/gcc"
118118
},
119+
"bearssl": {
120+
"cmake": "-DLWS_WITH_BEARSSL=1 -DLWS_BEARSSL_INCLUDE_DIRS=/opt/BearSSL/inc -DLWS_BEARSSL_LIBRARIES=/opt/BearSSL/build/libbearssl.so",
121+
"platforms": "none, rocky9/aarch64-a72a55-rk3588/gcc"
122+
},
119123
"default-examples-awslc": {
120124
"cmake": "-DLWS_WITH_AWSLC=1 -DLWS_OPENSSL_INCLUDE_DIRS=\"/usr/aws-lc/include\" -DLWS_OPENSSL_LIBRARIES=\"/usr/aws-lc/lib64/libssl.so;/usr/aws-lc/lib64/libcrypto.so\" -DLWS_WITH_MINIMAL_EXAMPLES=1",
121125
"platforms": "none, rocky9/aarch64-a72a55-rk3588/gcc"

CMakeLists-implied-options.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ if(IOS)
2727
set(LWS_DETECTED_PLAT_IOS 1)
2828
endif()
2929

30-
if (LWS_WITH_SCHANNEL OR LWS_WITH_GNUTLS OR LWS_WITH_MBEDTLS)
30+
if (LWS_WITH_SCHANNEL OR LWS_WITH_GNUTLS OR LWS_WITH_MBEDTLS OR LWS_WITH_BEARSSL)
3131
set(LWS_WITH_SSL 1)
3232
endif()
3333

@@ -479,7 +479,7 @@ if (LWS_SSL_SERVER_WITH_ECDH_CERT)
479479
endif()
480480

481481
# LWS_OPENSSL_SUPPORT deprecated... use LWS_WITH_TLS
482-
if (LWS_WITH_SSL OR LWS_WITH_MBEDTLS)
482+
if (LWS_WITH_SSL OR LWS_WITH_MBEDTLS OR LWS_WITH_BEARSSL)
483483
set(LWS_OPENSSL_SUPPORT 1)
484484
set(LWS_WITH_TLS 1)
485485
endif()

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ option(LWS_CTEST_INTERNET_AVAILABLE "CTest will performs tests that need the Int
232232
#
233233
option(LWS_WITH_SSL "Include SSL support (defaults to OpenSSL or similar, mbedTLS if LWS_WITH_MBEDTLS is set)" ON)
234234
option(LWS_WITH_MBEDTLS "Use mbedTLS (>=2.0) replacement for OpenSSL. When setting this, you also may need to specify LWS_MBEDTLS_LIBRARIES and LWS_MBEDTLS_INCLUDE_DIRS" OFF)
235+
option(LWS_WITH_BEARSSL "Use BearSSL replacement for OpenSSL. When setting this, you also may need to specify LWS_BEARSSL_LIBRARIES and LWS_BEARSSL_INCLUDE_DIRS" OFF)
236+
set(LWS_BEARSSL_PROFILE "full" CACHE STRING "BearSSL profile to use (e.g. full, client, minimal)")
235237
option(LWS_WITH_SCHANNEL "Use Windows SChannel for SSL" OFF)
236238
option(LWS_WITH_BORINGSSL "Use BoringSSL replacement for OpenSSL" OFF)
237239
option(LWS_WITH_GNUTLS "Use GnuTLS for SSL" OFF)

READMEs/README.build-windows.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ additional CMake options on lws:
122122
-DLWS_WITH_MBEDTLS=TRUE
123123
```
124124

125+
### Alternative: BearSSL (or OpenSSL/MbedTLS, see above)
126+
127+
BearSSL is a highly optimized, minimalistic alternative to OpenSSL and MbedTLS. It is easily cross-compiled or built on Windows. Note that BearSSL currently does not support DTLS. To use it, simply provide the include and library paths:
128+
129+
```
130+
-DLWS_WITH_BEARSSL=TRUE
131+
-DLWS_BEARSSL_INCLUDE_DIRS=C:/path/to/bearssl/inc
132+
-DLWS_BEARSSL_LIBRARIES=C:/path/to/bearssl/build/bearssl.lib
133+
```
134+
125135
### Powershell
126136

127137
CMake wants it and the version that comes with windows is too old to have pwsh.exe.

READMEs/README.build.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,8 @@ plugins and lwsws.
334334
- If you are really restricted on memory, code size, or don't care about TLS
335335
speed, mbedTLS is a good choice: `cmake .. -DLWS_WITH_MBEDTLS=1`
336336

337+
- If you want an extremely lightweight, highly optimized TLS library with a minimal memory footprint and fast execution speed, BearSSL is a strong alternative: `cmake .. -DLWS_WITH_BEARSSL=1`. Note that BearSSL currently does not support DTLS.
338+
337339
- If cpu and memory is not super restricted and you care about TLS speed,
338340
OpenSSL or a directly compatible variant like Boring SSL is a good choice.
339341

@@ -354,12 +356,18 @@ Lws supports both almost the same, so instead of taking my word for it you are
354356
invited to try it both ways and see which the results (including, eg, binary
355357
size and memory usage as well as speed) suggest you use.
356358

357-
NOTE: one major difference with mbedTLS is it does not load the system trust
358-
store by default. That has advantages and disadvantages, but the disadvantage
359-
is you must provide the CA cert to lws built against mbedTLS for it to be able
360-
to validate it, ie, use -A with the test client. The minimal test clients
361-
have the CA cert for warmcat.com and libwebsockets.org and use it if they see
362-
they were built with mbedTLS.
359+
NOTE: one major difference with mbedTLS and BearSSL is they do not natively load the OS trust
360+
store by default in the same way OpenSSL does.
361+
362+
For mbedTLS, you must provide the CA cert to lws for it to be able
363+
to validate it, ie, use `-A` with the test client.
364+
365+
For BearSSL, LWS implements a multi-cert PEM parser and fallback sequence to emulate OpenSSL's behavior:
366+
1. It checks the `SSL_CERT_FILE` and `SSL_CERT_DIR` environment variables for runtime overrides.
367+
2. It falls back to probing standard OS locations (e.g. `/etc/ssl/certs/ca-certificates.crt`).
368+
3. It defaults to the CMake-configured `LWS_OPENSSL_CLIENT_CERTS` if all else fails.
369+
370+
This allows BearSSL to validate most system certificates out of the box on Linux. The minimal test clients also automatically include the CA cert for warmcat.com if they see they were built with mbedTLS or BearSSL.
363371

364372
@section optee Building for OP-TEE
365373

cmake/lws_config.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@
208208
#cmakedefine LWS_WITH_LWSAC
209209
#cmakedefine LWS_LOGS_TIMESTAMP
210210
#cmakedefine LWS_WITH_MBEDTLS
211+
#cmakedefine LWS_WITH_BEARSSL
211212
#cmakedefine LWS_WITH_SCHANNEL
212213
#cmakedefine LWS_WITH_GNUTLS
213214
#cmakedefine LWS_WITH_MINIZ

include/libwebsockets.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,9 @@ lws_fx_string(const lws_fx_t *a, char *buf, size_t size);
907907
#include <mbedtls/sha256.h>
908908
#include <mbedtls/sha512.h>
909909
#endif
910+
#if defined(LWS_WITH_BEARSSL)
911+
#include <bearssl.h>
912+
#endif
910913

911914
#include <libwebsockets/lws-genhash.h>
912915
#include <libwebsockets/lws-genrsa.h>

include/libwebsockets/lws-context-vhost.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ struct lws_context_creation_info {
597597

598598
#endif
599599

600-
#if !defined(LWS_WITH_MBEDTLS)
600+
#if !defined(LWS_WITH_MBEDTLS) && !defined(LWS_WITH_BEARSSL)
601601
SSL_CTX *provided_client_ssl_ctx;
602602
/**< CONTEXT: If non-null, swap out libwebsockets ssl
603603
* implementation for the one provided by provided_ssl_ctx.

include/libwebsockets/lws-genaes.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ struct lws_genaes_ctx {
9191
#elif defined(LWS_WITH_GNUTLS)
9292
gnutls_cipher_hd_t ctx;
9393
int gnutls_gcm_initialized;
94+
#elif defined(LWS_WITH_BEARSSL)
95+
union {
96+
br_aes_ct_cbcenc_keys cbcenc;
97+
br_aes_ct_cbcdec_keys cbcdec;
98+
br_aes_ct_ctr_keys ctr;
99+
} u;
100+
br_gcm_context gcm;
101+
const br_block_cbcenc_class *cbcenc_vtable;
102+
const br_block_cbcdec_class *cbcdec_vtable;
103+
const br_block_ctr_class *ctr_vtable;
94104
#else
95105
EVP_CIPHER_CTX *ctx;
96106
const EVP_CIPHER *cipher;

include/libwebsockets/lws-genec.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ struct lws_genec_ctx {
4545
#elif defined(LWS_WITH_GNUTLS)
4646
gnutls_privkey_t priv;
4747
gnutls_pubkey_t pub;
48+
#elif defined(LWS_WITH_BEARSSL)
49+
br_ec_public_key pub;
50+
br_ec_private_key priv;
51+
void *kbuf_priv;
52+
void *kbuf_pub;
4853
#else
4954
EVP_PKEY_CTX *ctx[2];
5055
#endif

0 commit comments

Comments
 (0)