@@ -51,13 +51,17 @@ void ssl_init(sslclient_context *ssl_client)
51
51
}
52
52
53
53
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 )
55
55
{
56
56
char buf[512 ];
57
57
int ret, flags;
58
58
int enable = 1 ;
59
59
log_v (" Free internal heap before TLS %u" , ESP.getFreeHeap ());
60
60
61
+ if (rootCABuff == NULL && pskIdent == NULL && psKey == NULL && !insecure) {
62
+ return -1 ;
63
+ }
64
+
61
65
log_v (" Starting socket" );
62
66
ssl_client->socket = -1 ;
63
67
@@ -118,16 +122,19 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p
118
122
// MBEDTLS_SSL_VERIFY_REQUIRED if a CA certificate is defined on Arduino IDE and
119
123
// MBEDTLS_SSL_VERIFY_NONE if not.
120
124
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 ) {
122
129
log_v (" Loading CA cert" );
123
130
mbedtls_x509_crt_init (&ssl_client->ca_cert );
124
131
mbedtls_ssl_conf_authmode (&ssl_client->ssl_conf , MBEDTLS_SSL_VERIFY_REQUIRED);
125
132
ret = mbedtls_x509_crt_parse (&ssl_client->ca_cert , (const unsigned char *)rootCABuff, strlen (rootCABuff) + 1 );
126
133
mbedtls_ssl_conf_ca_chain (&ssl_client->ssl_conf , &ssl_client->ca_cert , NULL );
127
134
// mbedtls_ssl_conf_verify(&ssl_client->ssl_ctx, my_verify, NULL );
128
135
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 );
131
138
return handle_error (ret);
132
139
}
133
140
} else if (pskIdent != NULL && psKey != NULL ) {
@@ -161,20 +168,19 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p
161
168
return handle_error (ret);
162
169
}
163
170
} 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 ;
166
172
}
167
173
168
- if (cli_cert != NULL && cli_key != NULL ) {
174
+ if (!insecure && cli_cert != NULL && cli_key != NULL ) {
169
175
mbedtls_x509_crt_init (&ssl_client->client_cert );
170
176
mbedtls_pk_init (&ssl_client->client_key );
171
177
172
178
log_v (" Loading CRT cert" );
173
179
174
180
ret = mbedtls_x509_crt_parse (&ssl_client->client_cert , (const unsigned char *)cli_cert, strlen (cli_cert) + 1 );
175
181
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 );
178
184
return handle_error (ret);
179
185
}
180
186
@@ -211,7 +217,7 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p
211
217
}
212
218
if ((millis ()-handshake_start_time)>ssl_client->handshake_timeout )
213
219
return -1 ;
214
- vTaskDelay (10 / portTICK_PERIOD_MS);
220
+ vTaskDelay (2 ); // 2 ticks
215
221
}
216
222
217
223
0 commit comments