2017-02-10 23:20:24 +01:00
|
|
|
/* Provide SSL/TLS functions to ESP32 with Arduino IDE
|
|
|
|
*
|
2017-03-31 00:01:09 +02:00
|
|
|
* Adapted from the ssl_client1 example of mbedtls.
|
2017-02-10 23:20:24 +01:00
|
|
|
*
|
|
|
|
* Original Copyright (C) 2006-2015, ARM Limited, All Rights Reserved, Apache 2.0 License.
|
|
|
|
* Additions Copyright (C) 2017 Evandro Luis Copercini, Apache 2.0 License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
2017-03-31 00:01:09 +02:00
|
|
|
#include <esp32-hal-log.h>
|
2017-02-10 23:20:24 +01:00
|
|
|
#include <lwip/err.h>
|
|
|
|
#include <lwip/sockets.h>
|
|
|
|
#include <lwip/sys.h>
|
|
|
|
#include <lwip/netdb.h>
|
|
|
|
#include "ssl_client.h"
|
|
|
|
|
2017-05-19 10:18:20 +02:00
|
|
|
|
2017-02-10 23:20:24 +01:00
|
|
|
const char *pers = "esp32-tls";
|
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
static int handle_error(int err)
|
|
|
|
{
|
|
|
|
#ifdef MBEDTLS_ERROR_C
|
|
|
|
char error_buf[100];
|
|
|
|
mbedtls_strerror(err, error_buf, 100);
|
2017-03-31 00:01:09 +02:00
|
|
|
log_e("%s", error_buf);
|
2017-03-10 15:52:50 +01:00
|
|
|
#endif
|
2017-03-31 00:01:09 +02:00
|
|
|
log_e("MbedTLS message code: %d", err);
|
2017-03-10 15:52:50 +01:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-02-10 23:20:24 +01:00
|
|
|
|
|
|
|
void ssl_init(sslclient_context *ssl_client)
|
|
|
|
{
|
|
|
|
mbedtls_ssl_init(&ssl_client->ssl_ctx);
|
|
|
|
mbedtls_ssl_config_init(&ssl_client->ssl_conf);
|
|
|
|
mbedtls_ctr_drbg_init(&ssl_client->drbg_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-24 02:33:26 +02:00
|
|
|
int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t port, const char *rootCABuff, const char *cli_cert, const char *cli_key)
|
2017-02-10 23:20:24 +01:00
|
|
|
{
|
|
|
|
char buf[512];
|
|
|
|
int ret, flags, len, timeout;
|
|
|
|
int enable = 1;
|
2017-03-31 00:01:09 +02:00
|
|
|
log_i("Free heap before TLS %u", xPortGetFreeHeapSize());
|
2017-02-10 23:20:24 +01:00
|
|
|
|
2017-03-31 00:01:09 +02:00
|
|
|
log_i("Starting socket");
|
2017-03-10 15:52:50 +01:00
|
|
|
ssl_client->socket = -1;
|
2017-03-31 00:01:09 +02:00
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
ssl_client->socket = lwip_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
if (ssl_client->socket < 0) {
|
2017-03-31 00:01:09 +02:00
|
|
|
log_e("ERROR opening socket");
|
2017-03-10 15:52:50 +01:00
|
|
|
return ssl_client->socket;
|
|
|
|
}
|
2017-02-10 23:20:24 +01:00
|
|
|
|
2017-08-24 02:33:26 +02:00
|
|
|
struct hostent *server;
|
|
|
|
server = gethostbyname(host);
|
|
|
|
if (server == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
IPAddress srv((const uint8_t *)(server->h_addr));
|
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
struct sockaddr_in serv_addr;
|
|
|
|
memset(&serv_addr, 0, sizeof(serv_addr));
|
|
|
|
serv_addr.sin_family = AF_INET;
|
2017-08-24 02:33:26 +02:00
|
|
|
serv_addr.sin_addr.s_addr = srv;
|
2017-03-10 15:52:50 +01:00
|
|
|
serv_addr.sin_port = htons(port);
|
|
|
|
|
|
|
|
if (lwip_connect(ssl_client->socket, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == 0) {
|
|
|
|
timeout = 30000;
|
|
|
|
lwip_setsockopt(ssl_client->socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
|
|
|
lwip_setsockopt(ssl_client->socket, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
|
|
|
|
lwip_setsockopt(ssl_client->socket, IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable));
|
|
|
|
lwip_setsockopt(ssl_client->socket, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable));
|
|
|
|
} else {
|
2017-03-31 00:01:09 +02:00
|
|
|
log_e("Connect to Server failed!");
|
2017-03-10 15:52:50 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2017-02-10 23:20:24 +01:00
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
fcntl( ssl_client->socket, F_SETFL, fcntl( ssl_client->socket, F_GETFL, 0 ) | O_NONBLOCK );
|
2017-02-10 23:20:24 +01:00
|
|
|
|
2017-03-31 00:01:09 +02:00
|
|
|
log_i("Seeding the random number generator");
|
2017-03-10 15:52:50 +01:00
|
|
|
mbedtls_entropy_init(&ssl_client->entropy_ctx);
|
2017-02-10 23:20:24 +01:00
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
ret = mbedtls_ctr_drbg_seed(&ssl_client->drbg_ctx, mbedtls_entropy_func,
|
|
|
|
&ssl_client->entropy_ctx, (const unsigned char *) pers, strlen(pers));
|
|
|
|
if (ret < 0) {
|
|
|
|
return handle_error(ret);
|
|
|
|
}
|
2017-02-10 23:20:24 +01:00
|
|
|
|
2017-03-31 00:01:09 +02:00
|
|
|
log_i("Setting up the SSL/TLS structure...");
|
|
|
|
|
|
|
|
if ((ret = mbedtls_ssl_config_defaults(&ssl_client->ssl_conf,
|
|
|
|
MBEDTLS_SSL_IS_CLIENT,
|
|
|
|
MBEDTLS_SSL_TRANSPORT_STREAM,
|
|
|
|
MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
|
|
|
|
return handle_error(ret);
|
|
|
|
}
|
2017-02-10 23:20:24 +01:00
|
|
|
|
2017-08-24 02:33:26 +02:00
|
|
|
// MBEDTLS_SSL_VERIFY_REQUIRED if a CA certificate is defined on Arduino IDE and
|
|
|
|
// MBEDTLS_SSL_VERIFY_NONE if not.
|
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
if (rootCABuff != NULL) {
|
2017-03-31 00:01:09 +02:00
|
|
|
log_i("Loading CA cert");
|
2017-03-10 15:52:50 +01:00
|
|
|
mbedtls_x509_crt_init(&ssl_client->ca_cert);
|
|
|
|
mbedtls_ssl_conf_authmode(&ssl_client->ssl_conf, MBEDTLS_SSL_VERIFY_REQUIRED);
|
|
|
|
ret = mbedtls_x509_crt_parse(&ssl_client->ca_cert, (const unsigned char *)rootCABuff, strlen(rootCABuff) + 1);
|
|
|
|
mbedtls_ssl_conf_ca_chain(&ssl_client->ssl_conf, &ssl_client->ca_cert, NULL);
|
|
|
|
//mbedtls_ssl_conf_verify(&ssl_client->ssl_ctx, my_verify, NULL );
|
|
|
|
if (ret < 0) {
|
|
|
|
return handle_error(ret);
|
2017-02-10 23:20:24 +01:00
|
|
|
}
|
2017-03-10 15:52:50 +01:00
|
|
|
} else {
|
|
|
|
mbedtls_ssl_conf_authmode(&ssl_client->ssl_conf, MBEDTLS_SSL_VERIFY_NONE);
|
2017-03-31 00:01:09 +02:00
|
|
|
log_i("WARNING: Use certificates for a more secure communication!");
|
2017-03-10 15:52:50 +01:00
|
|
|
}
|
2017-02-10 23:20:24 +01:00
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
if (cli_cert != NULL && cli_key != NULL) {
|
|
|
|
mbedtls_x509_crt_init(&ssl_client->client_cert);
|
|
|
|
mbedtls_pk_init(&ssl_client->client_key);
|
2017-02-10 23:20:24 +01:00
|
|
|
|
2017-03-31 00:01:09 +02:00
|
|
|
log_i("Loading CRT cert");
|
2017-02-10 23:20:24 +01:00
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
ret = mbedtls_x509_crt_parse(&ssl_client->client_cert, (const unsigned char *)cli_cert, strlen(cli_cert) + 1);
|
|
|
|
if (ret < 0) {
|
|
|
|
return handle_error(ret);
|
|
|
|
}
|
2017-02-10 23:20:24 +01:00
|
|
|
|
2017-03-31 00:01:09 +02:00
|
|
|
log_i("Loading private key");
|
2017-03-10 15:52:50 +01:00
|
|
|
ret = mbedtls_pk_parse_key(&ssl_client->client_key, (const unsigned char *)cli_key, strlen(cli_key) + 1, NULL, 0);
|
2017-02-10 23:20:24 +01:00
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return handle_error(ret);
|
2017-02-10 23:20:24 +01:00
|
|
|
}
|
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
mbedtls_ssl_conf_own_cert(&ssl_client->ssl_conf, &ssl_client->client_cert, &ssl_client->client_key);
|
|
|
|
}
|
|
|
|
|
2017-08-24 02:33:26 +02:00
|
|
|
log_i("Setting hostname for TLS session...");
|
2017-02-10 23:20:24 +01:00
|
|
|
|
2017-08-24 02:33:26 +02:00
|
|
|
// Hostname set here should match CN in server certificate
|
|
|
|
if((ret = mbedtls_ssl_set_hostname(&ssl_client->ssl_ctx, host)) != 0){
|
|
|
|
return handle_error(ret);
|
|
|
|
}
|
2017-02-10 23:20:24 +01:00
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
mbedtls_ssl_conf_rng(&ssl_client->ssl_conf, mbedtls_ctr_drbg_random, &ssl_client->drbg_ctx);
|
2017-02-10 23:20:24 +01:00
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
if ((ret = mbedtls_ssl_setup(&ssl_client->ssl_ctx, &ssl_client->ssl_conf)) != 0) {
|
|
|
|
return handle_error(ret);
|
|
|
|
}
|
2017-02-10 23:20:24 +01:00
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
mbedtls_ssl_set_bio(&ssl_client->ssl_ctx, &ssl_client->socket, mbedtls_net_send, mbedtls_net_recv, NULL );
|
2017-02-10 23:20:24 +01:00
|
|
|
|
2017-03-31 00:01:09 +02:00
|
|
|
log_i("Performing the SSL/TLS handshake...");
|
2017-02-10 23:20:24 +01:00
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
while ((ret = mbedtls_ssl_handshake(&ssl_client->ssl_ctx)) != 0) {
|
2017-05-19 10:18:20 +02:00
|
|
|
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
|
2017-03-10 15:52:50 +01:00
|
|
|
return handle_error(ret);
|
2017-02-10 23:20:24 +01:00
|
|
|
}
|
2017-03-10 15:52:50 +01:00
|
|
|
}
|
2017-02-10 23:20:24 +01:00
|
|
|
|
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
if (cli_cert != NULL && cli_key != NULL) {
|
2017-03-31 00:01:09 +02:00
|
|
|
log_i("Protocol is %s Ciphersuite is %s", mbedtls_ssl_get_version(&ssl_client->ssl_ctx), mbedtls_ssl_get_ciphersuite(&ssl_client->ssl_ctx));
|
2017-03-10 15:52:50 +01:00
|
|
|
if ((ret = mbedtls_ssl_get_record_expansion(&ssl_client->ssl_ctx)) >= 0) {
|
2017-03-31 00:01:09 +02:00
|
|
|
log_i("Record expansion is %d", ret);
|
2017-03-10 15:52:50 +01:00
|
|
|
} else {
|
2017-03-31 00:01:09 +02:00
|
|
|
log_i("Record expansion is unknown (compression)");
|
2017-02-10 23:20:24 +01:00
|
|
|
}
|
2017-03-10 15:52:50 +01:00
|
|
|
}
|
2017-02-10 23:20:24 +01:00
|
|
|
|
2017-03-31 00:01:09 +02:00
|
|
|
log_i("Verifying peer X.509 certificate...");
|
2017-02-10 23:20:24 +01:00
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
if ((flags = mbedtls_ssl_get_verify_result(&ssl_client->ssl_ctx)) != 0) {
|
2017-03-31 00:01:09 +02:00
|
|
|
log_e("Failed to verify peer certificate!");
|
2017-03-10 15:52:50 +01:00
|
|
|
bzero(buf, sizeof(buf));
|
|
|
|
mbedtls_x509_crt_verify_info(buf, sizeof(buf), " ! ", flags);
|
2017-03-31 00:01:09 +02:00
|
|
|
log_e("verification info: %s", buf);
|
2017-03-10 15:52:50 +01:00
|
|
|
stop_ssl_socket(ssl_client, rootCABuff, cli_cert, cli_key); //It's not safe continue.
|
|
|
|
return handle_error(ret);
|
|
|
|
} else {
|
2017-03-31 00:01:09 +02:00
|
|
|
log_i("Certificate verified.");
|
2017-03-10 15:52:50 +01:00
|
|
|
}
|
2017-06-29 09:30:59 +02:00
|
|
|
|
|
|
|
if (rootCABuff != NULL) {
|
|
|
|
mbedtls_x509_crt_free(&ssl_client->ca_cert);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cli_cert != NULL) {
|
|
|
|
mbedtls_x509_crt_free(&ssl_client->client_cert);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cli_key != NULL) {
|
|
|
|
mbedtls_pk_free(&ssl_client->client_key);
|
|
|
|
}
|
2017-02-10 23:20:24 +01:00
|
|
|
|
2017-03-31 00:01:09 +02:00
|
|
|
log_i("Free heap after TLS %u", xPortGetFreeHeapSize());
|
2017-02-10 23:20:24 +01:00
|
|
|
|
|
|
|
return ssl_client->socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
void stop_ssl_socket(sslclient_context *ssl_client, const char *rootCABuff, const char *cli_cert, const char *cli_key)
|
2017-02-10 23:20:24 +01:00
|
|
|
{
|
2017-03-31 00:01:09 +02:00
|
|
|
log_i("Cleaning SSL connection.");
|
2017-03-10 15:52:50 +01:00
|
|
|
|
|
|
|
if (ssl_client->socket >= 0) {
|
|
|
|
close(ssl_client->socket);
|
|
|
|
ssl_client->socket = -1;
|
|
|
|
}
|
|
|
|
|
2017-02-10 23:20:24 +01:00
|
|
|
mbedtls_ssl_free(&ssl_client->ssl_ctx);
|
|
|
|
mbedtls_ssl_config_free(&ssl_client->ssl_conf);
|
|
|
|
mbedtls_ctr_drbg_free(&ssl_client->drbg_ctx);
|
|
|
|
mbedtls_entropy_free(&ssl_client->entropy_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int data_to_read(sslclient_context *ssl_client)
|
|
|
|
{
|
|
|
|
int ret, res;
|
|
|
|
ret = mbedtls_ssl_read(&ssl_client->ssl_ctx, NULL, 0);
|
2017-03-31 00:01:09 +02:00
|
|
|
//log_e("RET: %i",ret); //for low level debug
|
2017-02-10 23:20:24 +01:00
|
|
|
res = mbedtls_ssl_get_bytes_avail(&ssl_client->ssl_ctx);
|
2017-08-24 02:33:26 +02:00
|
|
|
//log_e("RES: %i",res); //for low level debug
|
2017-05-19 10:18:20 +02:00
|
|
|
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE && ret < 0) {
|
2017-03-10 15:52:50 +01:00
|
|
|
return handle_error(ret);
|
2017-02-10 23:20:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int send_ssl_data(sslclient_context *ssl_client, const uint8_t *data, uint16_t len)
|
|
|
|
{
|
2017-03-31 00:01:09 +02:00
|
|
|
//log_i("Writing HTTP request..."); //for low level debug
|
2017-02-10 23:20:24 +01:00
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
while ((ret = mbedtls_ssl_write(&ssl_client->ssl_ctx, data, len)) <= 0) {
|
2017-05-19 10:18:20 +02:00
|
|
|
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
|
2017-03-10 15:52:50 +01:00
|
|
|
return handle_error(ret);
|
2017-02-10 23:20:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
len = ret;
|
2017-03-31 00:01:09 +02:00
|
|
|
//log_i("%d bytes written", len); //for low level debug
|
2017-02-10 23:20:24 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int get_ssl_receive(sslclient_context *ssl_client, uint8_t *data, int length)
|
|
|
|
{
|
2017-03-31 00:01:09 +02:00
|
|
|
//log_i( "Reading HTTP response..."); //for low level debug
|
2017-02-10 23:20:24 +01:00
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
ret = mbedtls_ssl_read(&ssl_client->ssl_ctx, data, length);
|
|
|
|
|
2017-03-31 00:01:09 +02:00
|
|
|
//log_i( "%d bytes readed", ret); //for low level debug
|
2017-02-10 23:20:24 +01:00
|
|
|
return ret;
|
|
|
|
}
|