Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit b3a0189

Browse files
committed
API skeleton for SSL/TLS client support
Completely non-functional. API sketch only.
1 parent 8ee20ad commit b3a0189

3 files changed

Lines changed: 86 additions & 0 deletions

File tree

src/AsyncTCP.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,18 @@ AsyncClient::AsyncClient(int sockfd)
283283
, _rx_since_timeout(0)
284284
, _ack_timeout(ASYNC_MAX_ACK_TIME)
285285
, _connect_port(0)
286+
#if ASYNC_TCP_SSL_ENABLED
287+
, _root_ca_len(0)
288+
, _root_ca(NULL)
289+
, _cli_cert_len(0)
290+
, _cli_cert(NULL)
291+
, _cli_key_len(0)
292+
, _cli_key(NULL)
293+
, _secure(false)
294+
, _handshake_done(true)
295+
, _psk_ident(0)
296+
, _psk(0)
297+
#endif // ASYNC_TCP_SSL_ENABLED
286298
, _writeSpaceRemaining(TCP_SND_BUF)
287299
, _conn_state(0)
288300
{
@@ -468,7 +480,11 @@ uint16_t AsyncClient::localPort() {
468480
}
469481

470482

483+
#if ASYNC_TCP_SSL_ENABLED
484+
bool AsyncClient::connect(IPAddress ip, uint16_t port, bool secure)
485+
#else
471486
bool AsyncClient::connect(IPAddress ip, uint16_t port)
487+
#endif // ASYNC_TCP_SSL_ENABLED
472488
{
473489
if (_socket != -1) {
474490
log_w("already connected, state %d", _conn_state);
@@ -480,6 +496,11 @@ bool AsyncClient::connect(IPAddress ip, uint16_t port)
480496
return false;
481497
}
482498

499+
#if ASYNC_TCP_SSL_ENABLED
500+
_secure = secure;
501+
_handshake_done = !secure;
502+
#endif // ASYNC_TCP_SSL_ENABLED
503+
483504
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
484505
if (sockfd < 0) {
485506
log_e("socket: %d", errno);
@@ -523,7 +544,11 @@ bool AsyncClient::connect(IPAddress ip, uint16_t port)
523544
}
524545

525546
void _tcpsock_dns_found(const char * name, struct ip_addr * ipaddr, void * arg);
547+
#if ASYNC_TCP_SSL_ENABLED
548+
bool AsyncClient::connect(const char* host, uint16_t port, bool secure){
549+
#else
526550
bool AsyncClient::connect(const char* host, uint16_t port){
551+
#endif // ASYNC_TCP_SSL_ENABLED
527552
ip_addr_t addr;
528553

529554
if(!_start_asyncsock_task()){
@@ -535,11 +560,19 @@ bool AsyncClient::connect(const char* host, uint16_t port){
535560
err_t err = dns_gethostbyname(host, &addr, (dns_found_callback)&_tcpsock_dns_found, this);
536561
if(err == ERR_OK) {
537562
log_v("\taddr resolved as %08x, connecting...", addr.u_addr.ip4.addr);
563+
#if ASYNC_TCP_SSL_ENABLED
564+
return connect(IPAddress(addr.u_addr.ip4.addr), port, secure);
565+
#else
538566
return connect(IPAddress(addr.u_addr.ip4.addr), port);
567+
#endif // ASYNC_TCP_SSL_ENABLED
539568
} else if(err == ERR_INPROGRESS) {
540569
log_v("\twaiting for DNS resolution");
541570
_conn_state = 1;
542571
_connect_port = port;
572+
#if ASYNC_TCP_SSL_ENABLED
573+
_secure = secure;
574+
_handshake_done = !secure;
575+
#endif // ASYNC_TCP_SSL_ENABLED
543576
return true;
544577
}
545578
log_e("error: %d", err);
@@ -952,6 +985,28 @@ int8_t AsyncClient::abort(){
952985
return ERR_ABRT;
953986
}
954987

988+
#if ASYNC_TCP_SSL_ENABLED
989+
void AsyncClient::setRootCa(const char* rootca, const size_t len) {
990+
_root_ca = (char*)rootca;
991+
_root_ca_len = len;
992+
}
993+
994+
void AsyncClient::setClientCert(const char* cli_cert, const size_t len) {
995+
_cli_cert = (char*)cli_cert;
996+
_cli_cert_len = len;
997+
}
998+
999+
void AsyncClient::setClientKey(const char* cli_key, const size_t len) {
1000+
_cli_key = (char*)cli_key;
1001+
_cli_key_len = len;
1002+
}
1003+
1004+
void AsyncClient::setPsk(const char* psk_ident, const char* psk) {
1005+
_psk_ident = psk_ident;
1006+
_psk = psk;
1007+
}
1008+
#endif // ASYNC_TCP_SSL_ENABLED
1009+
9551010
const char * AsyncClient::errorToString(int8_t error){
9561011
switch(error){
9571012
case ERR_OK: return "OK";

src/AsyncTCP.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,19 @@
2424
#ifndef ASYNCTCP_H_
2525
#define ASYNCTCP_H_
2626

27+
// TODO: se debe quitar este #define para que pueda habilitarse a voluntad el SSL
28+
// según el proyecto. Se coloca aquí para probar el desarrollo
29+
#define ASYNC_TCP_SSL_ENABLED 1
30+
2731
#include "IPAddress.h"
2832
#include "sdkconfig.h"
2933
#include <functional>
3034
#include <deque>
3135
#include <list>
36+
#if ASYNC_TCP_SSL_ENABLED
37+
#include <ssl_client.h>
38+
#include "tcp_mbedtls.h"
39+
#endif
3240

3341
extern "C" {
3442
#include "lwip/err.h"
@@ -46,6 +54,7 @@ class AsyncClient;
4654
#define ASYNC_MAX_ACK_TIME 5000
4755
#define ASYNC_WRITE_FLAG_COPY 0x01 //will allocate new buffer to hold the data while sending (else will hold reference to the data given)
4856
#define ASYNC_WRITE_FLAG_MORE 0x02 //will not send PSH flag, meaning that there should be more data to be sent before the application should react.
57+
#define SSL_HANDSHAKE_TIMEOUT 5000 // timeout to complete SSL handshake
4958

5059
typedef std::function<void(void*, AsyncClient*)> AcConnectHandler;
5160
typedef std::function<void(void*, AsyncClient*, size_t len, uint32_t time)> AcAckHandler;
@@ -86,8 +95,17 @@ class AsyncClient : public AsyncSocketBase
8695
AsyncClient(int sockfd = -1);
8796
~AsyncClient();
8897

98+
#if ASYNC_TCP_SSL_ENABLED
99+
bool connect(IPAddress ip, uint16_t port, bool secure = false);
100+
bool connect(const char* host, uint16_t port, bool secure = false);
101+
void setRootCa(const char* rootca, const size_t len);
102+
void setClientCert(const char* cli_cert, const size_t len);
103+
void setClientKey(const char* cli_key, const size_t len);
104+
void setPsk(const char* psk_ident, const char* psk);
105+
#else
89106
bool connect(IPAddress ip, uint16_t port);
90107
bool connect(const char* host, uint16_t port);
108+
#endif // ASYNC_TCP_SSL_ENABLED
91109
void close(bool now = false);
92110

93111
int8_t abort();
@@ -174,6 +192,19 @@ class AsyncClient : public AsyncSocketBase
174192
uint16_t _connect_port = 0;
175193
//const char * _connect_dnsname = NULL;
176194

195+
#if ASYNC_TCP_SSL_ENABLED
196+
size_t _root_ca_len;
197+
char* _root_ca;
198+
size_t _cli_cert_len;
199+
char* _cli_cert;
200+
size_t _cli_key_len;
201+
char* _cli_key;
202+
bool _secure;
203+
bool _handshake_done;
204+
const char* _psk_ident;
205+
const char* _psk;
206+
#endif // ASYNC_TCP_SSL_ENABLED
207+
177208
// The following private struct represents a buffer enqueued with the add()
178209
// method. Each of these buffers are flushed whenever the socket becomes
179210
// writable

src/tcp_mbedtls.h

Whitespace-only changes.

0 commit comments

Comments
 (0)