1+ #include <stdarg.h>
12#include <stdio.h>
23#include <stdlib.h>
34#include <string.h>
45
5- #include "mbedtls/debug.h"
66#include "socket.h"
77#include "socket_mbedtls.h"
88#if defined(LSS_HAS_BUNDLED_ROOT_CERTIFICATES )
1111#include <errno.h>
1212#include "mbedtls/platform.h"
1313#include "mbedtls/ssl.h"
14+ #if defined(MBEDTLS_DEBUG_C )
15+ #include "mbedtls/debug.h"
16+ #endif
17+ #include "psa/crypto.h"
1418
1519void
1620free_connection (lss_tls_connection_context * context ) {
@@ -30,6 +34,23 @@ mbedtlsDebugPrint(void* ctx, int level, const char* pFile, int line, const char*
3034 printf ("mbedtlsDebugPrint: |%d| %s\n" , level , pStr );
3135}
3236
37+ void
38+ lssDebugPrint (lss_open_tls_connection_options * options , const char * format , ...) {
39+ if (options == NULL || options -> debugLevel <= 1 ) {
40+ return ;
41+ }
42+
43+ va_list args ;
44+ va_start (args , format ); // Initialize the va_list variable with the last fixed parameter
45+
46+ // Print the formatted string
47+ printf ("lssDebugPrint: " );
48+ vprintf (format , args );
49+ printf ("\n" );
50+
51+ va_end (args ); // Clean up the va_list variable
52+ }
53+
3354lss_tls_connection_result
3455lss_open_tls_connection (const char * hostname , int portno , lss_open_tls_connection_options * options ) {
3556 lss_open_tls_connection_options defaultOptions = {0 , 0 , 0 , 1 , NULL , 0 , 1 , NULL , NULL };
@@ -48,6 +69,7 @@ lss_open_tls_connection(const char* hostname, int portno, lss_open_tls_connectio
4869 options = & defaultOptions ;
4970 }
5071
72+ psa_crypto_init (); // mbedtls 3.6.0+
5173 mbedtls_net_init (& result .context -> socket );
5274 mbedtls_ssl_init (& result .context -> ssl );
5375 mbedtls_ssl_config_init (& result .context -> conf );
@@ -70,7 +92,7 @@ lss_open_tls_connection(const char* hostname, int portno, lss_open_tls_connectio
7092 if ((err = mbedtls_ssl_config_defaults (& result .context -> conf , MBEDTLS_SSL_IS_CLIENT , MBEDTLS_SSL_TRANSPORT_STREAM ,
7193 MBEDTLS_SSL_PRESET_DEFAULT ))
7294 != 0 ) {
73- MBEDTLS_SSL_DEBUG_MSG ( 1 , ( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n " , err ) );
95+ lssDebugPrint ( options , "mbedtls_ssl_config_defaults failed with %d\n" , err );
7496 result .error_num = err ;
7597 result .error_source = ERR_SRC_MBEDTLS ;
7698 goto exit ;
@@ -95,7 +117,7 @@ lss_open_tls_connection(const char* hostname, int portno, lss_open_tls_connectio
95117 for (int i = 0 ; i < lss_cacertsCount ; i ++ ) {
96118 err = mbedtls_x509_crt_parse_der_nocopy (& result .context -> cacert , lss_cacerts + shift , lss_cacertSizes [i ]);
97119 if (err != 0 ) {
98- fprintf ( stderr , "Mbedtls_Connect: mbedtls_x509_crt_parse_file Failed. mbedtlsError = %d\n" , err );
120+ lssDebugPrint ( options , "mbedtls_x509_crt_parse_file Failed. mbedtlsError = %d\n" , err );
99121 result .error_num = err ;
100122 result .error_source = ERR_SRC_MBEDTLS ;
101123 goto exit ;
@@ -109,7 +131,7 @@ lss_open_tls_connection(const char* hostname, int portno, lss_open_tls_connectio
109131 err = mbedtls_x509_crt_parse_der (& result .context -> cacert , options -> caCertificates -> certificates [i ],
110132 options -> caCertificates -> sizes [i ]);
111133 if (err != 0 ) {
112- fprintf ( stderr , "Mbedtls_Connect: mbedtls_x509_crt_parse_file Failed. mbedtlsError = %d\n" , err );
134+ lssDebugPrint ( options , "mbedtls_x509_crt_parse_file Failed. mbedtlsError = %d\n" , err );
113135 result .error_num = err ;
114136 result .error_source = ERR_SRC_MBEDTLS ;
115137 goto exit ;
@@ -121,7 +143,7 @@ lss_open_tls_connection(const char* hostname, int portno, lss_open_tls_connectio
121143 err = mbedtls_x509_crt_parse_der (& result .context -> cacert , options -> clientCertificate -> certificate ,
122144 options -> clientCertificate -> certificateSize );
123145 if (err != 0 ) {
124- fprintf ( stderr , "Mbedtls_Connect: mbedtls_x509_crt_parse_file Failed. mbedtlsError = %d\n" , err );
146+ lssDebugPrint ( options , "mbedtls_x509_crt_parse_file Failed. mbedtlsError = %d\n" , err );
125147 result .error_num = err ;
126148 result .error_source = ERR_SRC_MBEDTLS ;
127149 goto exit ;
@@ -131,14 +153,14 @@ lss_open_tls_connection(const char* hostname, int portno, lss_open_tls_connectio
131153 options -> clientCertificate -> passwordSize , mbedtls_ctr_drbg_random ,
132154 & result .context -> ctr_drbg );
133155 if (err != 0 ) {
134- fprintf ( stderr , "Mbedtls_Connect: mbedtls_x509_crt_parse_file Failed. mbedtlsError = %d\n" , err );
156+ lssDebugPrint ( options , "mbedtls_x509_crt_parse_file Failed. mbedtlsError = %d\n" , err );
135157 result .error_num = err ;
136158 result .error_source = ERR_SRC_MBEDTLS ;
137159 goto exit ;
138160 }
139161 err = mbedtls_ssl_conf_own_cert (& result .context -> conf , & result .context -> clicert , & result .context -> pkey );
140162 if (err != 0 ) {
141- fprintf ( stderr , "Mbedtls_Connect: mbedtls_x509_crt_parse_file Failed. mbedtlsError = %d\n" , err );
163+ lssDebugPrint ( options , "mbedtls_x509_crt_parse_file Failed. mbedtlsError = %d\n" , err );
142164 result .error_num = err ;
143165 result .error_source = ERR_SRC_MBEDTLS ;
144166 goto exit ;
@@ -150,7 +172,7 @@ lss_open_tls_connection(const char* hostname, int portno, lss_open_tls_connectio
150172 mbedtls_ssl_set_hostname (& result .context -> ssl , hostname );
151173
152174 if ((err = mbedtls_ssl_setup (& result .context -> ssl , & result .context -> conf )) != 0 ) {
153- MBEDTLS_SSL_DEBUG_MSG ( 1 , ( " failed\n ! mbedtls_ssl_setup returned %d\n\n " , err ) );
175+ lssDebugPrint ( options , "mbedtls_ssl_setup failed with %d\n" , err );
154176 result .error_num = err ;
155177 result .error_source = ERR_SRC_MBEDTLS ;
156178 goto exit ;
@@ -162,7 +184,7 @@ lss_open_tls_connection(const char* hostname, int portno, lss_open_tls_connectio
162184 _options .write_timeout = options -> write_timeout ;
163185 lss_connection_result connResult = lss_open_connection (hostname , portno , & _options );
164186 if (connResult .error_num != 0 ) {
165- MBEDTLS_SSL_DEBUG_MSG ( 1 , ( " failed\n ! mbedtls_ssl_setup returned %d\n\n " , connResult .error_num ) );
187+ lssDebugPrint ( options , "lss_open_connection failed with %d\n" , connResult .error_num );
166188 result .error_num = connResult .error_num ;
167189 result .error_source = connResult .error_source ;
168190 goto exit ;
@@ -173,26 +195,27 @@ lss_open_tls_connection(const char* hostname, int portno, lss_open_tls_connectio
173195 mbedtls_ssl_set_bio (& result .context -> ssl , (void * )& result .context -> socket , mbedtls_net_send , mbedtls_net_recv ,
174196 mbedtls_net_recv_timeout );
175197
176- if ((err = mbedtls_ssl_conf_max_frag_len (& result .context -> conf , MBEDTLS_SSL_MAX_FRAG_LEN_4096 )) != 0 ) {
177- MBEDTLS_SSL_DEBUG_MSG (1 , (" failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n" , err ));
178- result .error_num = err ;
179- result .error_source = ERR_SRC_MBEDTLS ;
180- goto exit ;
181- }
198+ // fails in 3.6.0 with tls1.3 - unsupported extension
199+ // if ((err = mbedtls_ssl_conf_max_frag_len(&result.context->conf, MBEDTLS_SSL_MAX_FRAG_LEN_4096)) != 0) {
200+ // lssDebugPrint(options, "mbedtls_ssl_conf_max_frag_len failed with %d\n", err);
201+ // result.error_num = err;
202+ // result.error_source = ERR_SRC_MBEDTLS;
203+ // goto exit;
204+ // }
182205
183206 do {
184207 err = mbedtls_ssl_handshake (& result .context -> ssl );
185208 } while ((err == MBEDTLS_ERR_SSL_WANT_READ ) || (err == MBEDTLS_ERR_SSL_WANT_WRITE ));
186209
187210 if (err != 0 ) {
188- MBEDTLS_SSL_DEBUG_MSG ( 1 , ( " failed\n ! mbedtls_ssl_handshake returned %d\n\n " , err ) );
211+ lssDebugPrint ( options , "mbedtls_ssl_handshake failed with %d\n" , err );
189212 result .error_num = err ;
190213 result .error_source = ERR_SRC_MBEDTLS ;
191214 goto exit ;
192215 }
193216
194217 if ((err = mbedtls_ssl_get_verify_result (& result .context -> ssl )) != 0 ) {
195- MBEDTLS_SSL_DEBUG_MSG ( 1 , ( " failed\n ! mbedtls_ssl_get_verify_result returned %d\n\n " , err ) );
218+ lssDebugPrint ( options , "mbedtls_ssl_get_verify_result failed with %d\n" , err );
196219 result .error_num = err ;
197220 result .error_source = ERR_SRC_MBEDTLS ;
198221 goto exit ;
@@ -217,7 +240,7 @@ lss_close_tls_connection(lss_tls_connection_context* context) {
217240 } while ((err == MBEDTLS_ERR_SSL_WANT_READ ) || (err == MBEDTLS_ERR_SSL_WANT_WRITE ));
218241
219242 if (err != 0 ) {
220- MBEDTLS_SSL_DEBUG_MSG ( 1 , ( " failed\n ! mbedtls_ssl_close_notify returned %d\n\n " , err ) );
243+ lssDebugPrint ( NULL , "mbedtls_ssl_close_notify failed with %d\n" , err );
221244 result .error_num = err ;
222245 result .error_source = ERR_SRC_MBEDTLS ;
223246 }
0 commit comments