From 9ad18531b75e19c3c5e872ba3bf1da9a914e74fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurenz=20Altenm=C3=BCller?= Date: Sat, 5 Apr 2025 14:22:58 +0200 Subject: [PATCH] Enable building with BoringSSL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Laurenz Altenmüller --- src/Base64.c | 57 ++++++++++++++++++++++++-------------------- src/MQTTAsyncUtils.c | 4 ++-- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/Base64.c b/src/Base64.c index 4634af2fe..c673ce2a8 100644 --- a/src/Base64.c +++ b/src/Base64.c @@ -48,40 +48,45 @@ static b64_size_t Base64_encodeDecode( b64_size_t ret = 0u; if ( in_len > 0u ) { - int rv; - BIO *bio, *b64, *b_in, *b_out; - - b64 = BIO_new(BIO_f_base64()); - bio = BIO_new(BIO_s_mem()); - b64 = BIO_push(b64, bio); - BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); /* ignore new-lines */ - if ( encode ) { - b_in = bio; - b_out = b64; + /* For encoding: output length is 4 * (input_length / 3) rounded up to nearest multiple of 4 */ + b64_size_t required_len = ((in_len + 2) / 3) * 4; + if (out_len >= required_len + 1) /* +1 for null terminator */ + { + int encoded_len = EVP_EncodeBlock((unsigned char*)out, (const unsigned char*)in, (int)in_len); + if (encoded_len > 0) + { + ret = (b64_size_t)encoded_len; + if (out_len > ret) + out[ret] = '\0'; + } + } } else { - b_in = b64; - b_out = bio; - } - - rv = BIO_write(b_out, in, (int)in_len); - BIO_flush(b_out); /* indicate end of encoding */ - - if ( rv > 0 ) - { - rv = BIO_read(b_in, out, (int)out_len); - if ( rv > 0 ) + /* For decoding: output length is at most 3 * (input_length / 4) */ + b64_size_t max_out_len = (in_len / 4) * 3; + if (out_len >= max_out_len + 1) /* +1 for null terminator */ { - ret = (b64_size_t)rv; - if ( out_len > ret ) - out[ret] = '\0'; + EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new(); + if (ctx) + { + int decoded_len = 0; + int final_len = 0; + unsigned char *temp_out = (unsigned char*)out; + + EVP_DecodeInit(ctx); + EVP_DecodeUpdate(ctx, temp_out, &decoded_len, (const unsigned char*)in, (int)in_len); + EVP_DecodeFinal(ctx, temp_out + decoded_len, &final_len); + EVP_ENCODE_CTX_free(ctx); + + ret = (b64_size_t)(decoded_len + final_len); + if (out_len > ret) + out[ret] = '\0'; + } } } - - BIO_free_all(b64); /* free all used memory */ } return ret; } diff --git a/src/MQTTAsyncUtils.c b/src/MQTTAsyncUtils.c index c084a11e3..208fa0917 100644 --- a/src/MQTTAsyncUtils.c +++ b/src/MQTTAsyncUtils.c @@ -1864,7 +1864,7 @@ thread_return_type WINAPI MQTTAsync_sendThread(void* n) MQTTAsync_unlock_mutex(mqttasync_mutex); #if defined(OPENSSL) -#if ((OPENSSL_VERSION_NUMBER < 0x1010000fL) || defined(LIBRESSL_VERSION_NUMBER)) +#if ((OPENSSL_VERSION_NUMBER < 0x1010000fL) || defined(LIBRESSL_VERSION_NUMBER)) || defined(OPENSSL_IS_BORINGSSL) ERR_remove_state(0); #else OPENSSL_thread_stop(); @@ -2397,7 +2397,7 @@ thread_return_type WINAPI MQTTAsync_receiveThread(void* n) #endif #if defined(OPENSSL) -#if ((OPENSSL_VERSION_NUMBER < 0x1010000fL) || defined(LIBRESSL_VERSION_NUMBER)) +#if ((OPENSSL_VERSION_NUMBER < 0x1010000fL) || defined(LIBRESSL_VERSION_NUMBER)) || defined(OPENSSL_IS_BORINGSSL) ERR_remove_state(0); #else OPENSSL_thread_stop();