@@ -51,13 +51,17 @@ void ssl_init(sslclient_context *ssl_client)
5151}
5252
5353
54- int start_ssl_client (sslclient_context *ssl_client, const char *host, uint32_t port, int timeout, const char *rootCABuff, const char *cli_cert, const char *cli_key, const char *pskIdent, const char *psKey)
54+ int start_ssl_client (sslclient_context *ssl_client, const char *host, uint32_t port, int timeout, const char *rootCABuff, const char *cli_cert, const char *cli_key, const char *pskIdent, const char *psKey, bool insecure )
5555{
5656 char buf[512 ];
5757 int ret, flags;
5858 int enable = 1 ;
5959 log_v (" Free internal heap before TLS %u" , ESP.getFreeHeap ());
6060
61+ if (rootCABuff == NULL && pskIdent == NULL && psKey == NULL && !insecure) {
62+ return -1 ;
63+ }
64+
6165 log_v (" Starting socket" );
6266 ssl_client->socket = -1 ;
6367
@@ -118,16 +122,19 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p
118122 // MBEDTLS_SSL_VERIFY_REQUIRED if a CA certificate is defined on Arduino IDE and
119123 // MBEDTLS_SSL_VERIFY_NONE if not.
120124
121- if (rootCABuff != NULL ) {
125+ if (insecure) {
126+ mbedtls_ssl_conf_authmode (&ssl_client->ssl_conf , MBEDTLS_SSL_VERIFY_NONE);
127+ log_i (" WARNING: Skipping SSL Verification. INSECURE!" );
128+ } else if (rootCABuff != NULL ) {
122129 log_v (" Loading CA cert" );
123130 mbedtls_x509_crt_init (&ssl_client->ca_cert );
124131 mbedtls_ssl_conf_authmode (&ssl_client->ssl_conf , MBEDTLS_SSL_VERIFY_REQUIRED);
125132 ret = mbedtls_x509_crt_parse (&ssl_client->ca_cert , (const unsigned char *)rootCABuff, strlen (rootCABuff) + 1 );
126133 mbedtls_ssl_conf_ca_chain (&ssl_client->ssl_conf , &ssl_client->ca_cert , NULL );
127134 // mbedtls_ssl_conf_verify(&ssl_client->ssl_ctx, my_verify, NULL );
128135 if (ret < 0 ) {
129- // free the ca_cert in the case parse failed, otherwise, the old ca_cert still in the heap memory, that lead to "out of memory" crash.
130- mbedtls_x509_crt_free (&ssl_client->ca_cert );
136+ // free the ca_cert in the case parse failed, otherwise, the old ca_cert still in the heap memory, that lead to "out of memory" crash.
137+ mbedtls_x509_crt_free (&ssl_client->ca_cert );
131138 return handle_error (ret);
132139 }
133140 } else if (pskIdent != NULL && psKey != NULL ) {
@@ -161,20 +168,19 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p
161168 return handle_error (ret);
162169 }
163170 } else {
164- mbedtls_ssl_conf_authmode (&ssl_client->ssl_conf , MBEDTLS_SSL_VERIFY_NONE);
165- log_i (" WARNING: Use certificates for a more secure communication!" );
171+ return -1 ;
166172 }
167173
168- if (cli_cert != NULL && cli_key != NULL ) {
174+ if (!insecure && cli_cert != NULL && cli_key != NULL ) {
169175 mbedtls_x509_crt_init (&ssl_client->client_cert );
170176 mbedtls_pk_init (&ssl_client->client_key );
171177
172178 log_v (" Loading CRT cert" );
173179
174180 ret = mbedtls_x509_crt_parse (&ssl_client->client_cert , (const unsigned char *)cli_cert, strlen (cli_cert) + 1 );
175181 if (ret < 0 ) {
176- // free the client_cert in the case parse failed, otherwise, the old client_cert still in the heap memory, that lead to "out of memory" crash.
177- mbedtls_x509_crt_free (&ssl_client->client_cert );
182+ // free the client_cert in the case parse failed, otherwise, the old client_cert still in the heap memory, that lead to "out of memory" crash.
183+ mbedtls_x509_crt_free (&ssl_client->client_cert );
178184 return handle_error (ret);
179185 }
180186
@@ -211,7 +217,7 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p
211217 }
212218 if ((millis ()-handshake_start_time)>ssl_client->handshake_timeout )
213219 return -1 ;
214- vTaskDelay (10 / portTICK_PERIOD_MS);
220+ vTaskDelay (2 ); // 2 ticks
215221 }
216222
217223
0 commit comments