2021-03-11 11:11:53 +01:00
|
|
|
/*
|
|
|
|
WiFiClientSecure.cpp - Client Secure class for ESP32
|
|
|
|
Copyright (c) 2016 Hristo Gochkov All right reserved.
|
|
|
|
Additions Copyright (C) 2017 Evandro Luis Copercini.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2018-04-16 16:34:39 +02:00
|
|
|
|
|
|
|
#include "WiFiClientSecure.h"
|
|
|
|
#include <lwip/sockets.h>
|
|
|
|
#include <lwip/netdb.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#undef connect
|
|
|
|
#undef write
|
|
|
|
#undef read
|
|
|
|
|
|
|
|
|
|
|
|
WiFiClientSecure::WiFiClientSecure()
|
|
|
|
{
|
|
|
|
_connected = false;
|
|
|
|
|
|
|
|
sslclient = new sslclient_context;
|
|
|
|
ssl_init(sslclient);
|
|
|
|
sslclient->socket = -1;
|
2018-11-26 23:25:08 +01:00
|
|
|
sslclient->handshake_timeout = 120000;
|
2020-12-21 00:09:37 +01:00
|
|
|
_use_insecure = false;
|
2018-04-16 16:34:39 +02:00
|
|
|
_CA_cert = NULL;
|
|
|
|
_cert = NULL;
|
|
|
|
_private_key = NULL;
|
2018-12-03 16:17:55 +01:00
|
|
|
_pskIdent = NULL;
|
|
|
|
_psKey = NULL;
|
2018-04-16 16:34:39 +02:00
|
|
|
next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
WiFiClientSecure::WiFiClientSecure(int sock)
|
|
|
|
{
|
|
|
|
_connected = false;
|
2019-04-15 17:19:49 +02:00
|
|
|
_timeout = 0;
|
2018-04-16 16:34:39 +02:00
|
|
|
|
|
|
|
sslclient = new sslclient_context;
|
|
|
|
ssl_init(sslclient);
|
|
|
|
sslclient->socket = sock;
|
2018-11-26 23:25:08 +01:00
|
|
|
sslclient->handshake_timeout = 120000;
|
2018-04-16 16:34:39 +02:00
|
|
|
|
|
|
|
if (sock >= 0) {
|
|
|
|
_connected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_CA_cert = NULL;
|
|
|
|
_cert = NULL;
|
|
|
|
_private_key = NULL;
|
2018-12-03 16:17:55 +01:00
|
|
|
_pskIdent = NULL;
|
|
|
|
_psKey = NULL;
|
2018-04-16 16:34:39 +02:00
|
|
|
next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
WiFiClientSecure::~WiFiClientSecure()
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
delete sslclient;
|
|
|
|
}
|
|
|
|
|
|
|
|
WiFiClientSecure &WiFiClientSecure::operator=(const WiFiClientSecure &other)
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
sslclient->socket = other.sslclient->socket;
|
|
|
|
_connected = other._connected;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiClientSecure::stop()
|
|
|
|
{
|
|
|
|
if (sslclient->socket >= 0) {
|
|
|
|
close(sslclient->socket);
|
|
|
|
sslclient->socket = -1;
|
|
|
|
_connected = false;
|
2018-06-27 22:09:21 +02:00
|
|
|
_peek = -1;
|
2018-04-16 16:34:39 +02:00
|
|
|
}
|
|
|
|
stop_ssl_socket(sslclient, _CA_cert, _cert, _private_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClientSecure::connect(IPAddress ip, uint16_t port)
|
|
|
|
{
|
2018-12-03 16:17:55 +01:00
|
|
|
if (_pskIdent && _psKey)
|
|
|
|
return connect(ip, port, _pskIdent, _psKey);
|
2018-04-16 16:34:39 +02:00
|
|
|
return connect(ip, port, _CA_cert, _cert, _private_key);
|
|
|
|
}
|
|
|
|
|
2019-04-15 17:19:49 +02:00
|
|
|
int WiFiClientSecure::connect(IPAddress ip, uint16_t port, int32_t timeout){
|
|
|
|
_timeout = timeout;
|
|
|
|
return connect(ip, port);
|
|
|
|
}
|
|
|
|
|
2018-04-16 16:34:39 +02:00
|
|
|
int WiFiClientSecure::connect(const char *host, uint16_t port)
|
|
|
|
{
|
2018-12-03 16:17:55 +01:00
|
|
|
if (_pskIdent && _psKey)
|
|
|
|
return connect(host, port, _pskIdent, _psKey);
|
2018-04-16 16:34:39 +02:00
|
|
|
return connect(host, port, _CA_cert, _cert, _private_key);
|
|
|
|
}
|
|
|
|
|
2019-04-15 17:19:49 +02:00
|
|
|
int WiFiClientSecure::connect(const char *host, uint16_t port, int32_t timeout){
|
|
|
|
_timeout = timeout;
|
|
|
|
return connect(host, port);
|
|
|
|
}
|
|
|
|
|
2020-12-21 00:09:37 +01:00
|
|
|
int WiFiClientSecure::connect(IPAddress ip, uint16_t port, const char *CA_cert, const char *cert, const char *private_key)
|
2018-04-16 16:34:39 +02:00
|
|
|
{
|
2020-12-21 00:09:37 +01:00
|
|
|
return connect(ip.toString().c_str(), port, CA_cert, cert, private_key);
|
2018-04-16 16:34:39 +02:00
|
|
|
}
|
|
|
|
|
2020-12-21 00:09:37 +01:00
|
|
|
int WiFiClientSecure::connect(const char *host, uint16_t port, const char *CA_cert, const char *cert, const char *private_key)
|
2018-04-16 16:34:39 +02:00
|
|
|
{
|
2019-04-15 17:19:49 +02:00
|
|
|
if(_timeout > 0){
|
2019-04-15 17:27:54 +02:00
|
|
|
sslclient->handshake_timeout = _timeout;
|
2019-04-15 17:19:49 +02:00
|
|
|
}
|
2020-12-21 00:09:37 +01:00
|
|
|
int ret = start_ssl_client(sslclient, host, port, _timeout, CA_cert, cert, private_key, NULL, NULL, _use_insecure);
|
2018-12-03 16:17:55 +01:00
|
|
|
_lastError = ret;
|
|
|
|
if (ret < 0) {
|
|
|
|
log_e("start_ssl_client: %d", ret);
|
|
|
|
stop();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
_connected = true;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClientSecure::connect(IPAddress ip, uint16_t port, const char *pskIdent, const char *psKey) {
|
2020-12-21 00:09:37 +01:00
|
|
|
return connect(ip.toString().c_str(), port, pskIdent, psKey);
|
2018-12-03 16:17:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClientSecure::connect(const char *host, uint16_t port, const char *pskIdent, const char *psKey) {
|
|
|
|
log_v("start_ssl_client with PSK");
|
2019-04-15 17:19:49 +02:00
|
|
|
if(_timeout > 0){
|
2019-04-15 17:27:54 +02:00
|
|
|
sslclient->handshake_timeout = _timeout;
|
2019-04-15 17:19:49 +02:00
|
|
|
}
|
2020-12-21 00:09:37 +01:00
|
|
|
int ret = start_ssl_client(sslclient, host, port, _timeout, NULL, NULL, NULL, pskIdent, psKey, _use_insecure);
|
2018-04-16 16:34:39 +02:00
|
|
|
_lastError = ret;
|
|
|
|
if (ret < 0) {
|
2018-06-27 22:09:21 +02:00
|
|
|
log_e("start_ssl_client: %d", ret);
|
2018-04-16 16:34:39 +02:00
|
|
|
stop();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
_connected = true;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClientSecure::peek(){
|
|
|
|
if(_peek >= 0){
|
|
|
|
return _peek;
|
|
|
|
}
|
2018-06-27 22:09:21 +02:00
|
|
|
_peek = timedRead();
|
2018-04-16 16:34:39 +02:00
|
|
|
return _peek;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t WiFiClientSecure::write(uint8_t data)
|
|
|
|
{
|
|
|
|
return write(&data, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClientSecure::read()
|
|
|
|
{
|
|
|
|
uint8_t data = -1;
|
|
|
|
int res = read(&data, 1);
|
|
|
|
if (res < 0) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t WiFiClientSecure::write(const uint8_t *buf, size_t size)
|
|
|
|
{
|
|
|
|
if (!_connected) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int res = send_ssl_data(sslclient, buf, size);
|
|
|
|
if (res < 0) {
|
|
|
|
stop();
|
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClientSecure::read(uint8_t *buf, size_t size)
|
|
|
|
{
|
2018-06-27 22:09:21 +02:00
|
|
|
int peeked = 0;
|
2018-12-15 18:14:38 +01:00
|
|
|
int avail = available();
|
|
|
|
if ((!buf && size) || avail <= 0) {
|
2018-06-27 22:09:21 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(!size){
|
|
|
|
return 0;
|
|
|
|
}
|
2018-04-16 16:34:39 +02:00
|
|
|
if(_peek >= 0){
|
2018-06-27 22:09:21 +02:00
|
|
|
buf[0] = _peek;
|
2018-04-16 16:34:39 +02:00
|
|
|
_peek = -1;
|
2018-06-27 22:09:21 +02:00
|
|
|
size--;
|
2018-12-15 18:14:38 +01:00
|
|
|
avail--;
|
|
|
|
if(!size || !avail){
|
2018-06-27 22:09:21 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
buf++;
|
|
|
|
peeked = 1;
|
2018-04-16 16:34:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int res = get_ssl_receive(sslclient, buf, size);
|
|
|
|
if (res < 0) {
|
|
|
|
stop();
|
2018-12-15 18:14:38 +01:00
|
|
|
return peeked?peeked:res;
|
2018-04-16 16:34:39 +02:00
|
|
|
}
|
2018-06-27 22:09:21 +02:00
|
|
|
return res + peeked;
|
2018-04-16 16:34:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClientSecure::available()
|
|
|
|
{
|
2018-12-15 18:14:38 +01:00
|
|
|
int peeked = (_peek >= 0);
|
2018-04-16 16:34:39 +02:00
|
|
|
if (!_connected) {
|
2018-12-15 18:14:38 +01:00
|
|
|
return peeked;
|
2018-04-16 16:34:39 +02:00
|
|
|
}
|
|
|
|
int res = data_to_read(sslclient);
|
2018-12-15 18:14:38 +01:00
|
|
|
if (res < 0) {
|
2018-04-16 16:34:39 +02:00
|
|
|
stop();
|
2018-12-15 18:14:38 +01:00
|
|
|
return peeked?peeked:res;
|
2018-06-27 22:09:21 +02:00
|
|
|
}
|
2018-12-15 18:14:38 +01:00
|
|
|
return res+peeked;
|
2018-04-16 16:34:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t WiFiClientSecure::connected()
|
|
|
|
{
|
|
|
|
uint8_t dummy = 0;
|
|
|
|
read(&dummy, 0);
|
|
|
|
|
|
|
|
return _connected;
|
|
|
|
}
|
|
|
|
|
2020-12-21 00:09:37 +01:00
|
|
|
void WiFiClientSecure::setInsecure()
|
|
|
|
{
|
|
|
|
_CA_cert = NULL;
|
|
|
|
_cert = NULL;
|
|
|
|
_private_key = NULL;
|
|
|
|
_pskIdent = NULL;
|
|
|
|
_psKey = NULL;
|
|
|
|
_use_insecure = true;
|
|
|
|
}
|
|
|
|
|
2018-04-16 16:34:39 +02:00
|
|
|
void WiFiClientSecure::setCACert (const char *rootCA)
|
|
|
|
{
|
|
|
|
_CA_cert = rootCA;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiClientSecure::setCertificate (const char *client_ca)
|
|
|
|
{
|
|
|
|
_cert = client_ca;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiClientSecure::setPrivateKey (const char *private_key)
|
|
|
|
{
|
|
|
|
_private_key = private_key;
|
|
|
|
}
|
|
|
|
|
2018-12-03 16:17:55 +01:00
|
|
|
void WiFiClientSecure::setPreSharedKey(const char *pskIdent, const char *psKey) {
|
|
|
|
_pskIdent = pskIdent;
|
|
|
|
_psKey = psKey;
|
|
|
|
}
|
|
|
|
|
2018-05-14 13:00:40 +02:00
|
|
|
bool WiFiClientSecure::verify(const char* fp, const char* domain_name)
|
|
|
|
{
|
|
|
|
if (!sslclient)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return verify_ssl_fingerprint(sslclient, fp, domain_name);
|
|
|
|
}
|
|
|
|
|
2018-11-19 16:50:08 +01:00
|
|
|
char *WiFiClientSecure::_streamLoad(Stream& stream, size_t size) {
|
2020-09-30 14:19:41 +02:00
|
|
|
char *dest = (char*)malloc(size+1);
|
2018-11-19 16:50:08 +01:00
|
|
|
if (!dest) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (size != stream.readBytes(dest, size)) {
|
|
|
|
free(dest);
|
2018-11-29 11:34:55 +01:00
|
|
|
dest = nullptr;
|
2020-09-30 14:19:41 +02:00
|
|
|
return nullptr;
|
2018-11-19 16:50:08 +01:00
|
|
|
}
|
2020-09-30 14:19:41 +02:00
|
|
|
dest[size] = '\0';
|
2018-11-29 11:34:55 +01:00
|
|
|
return dest;
|
2018-11-19 16:50:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WiFiClientSecure::loadCACert(Stream& stream, size_t size) {
|
|
|
|
char *dest = _streamLoad(stream, size);
|
|
|
|
bool ret = false;
|
|
|
|
if (dest) {
|
|
|
|
setCACert(dest);
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WiFiClientSecure::loadCertificate(Stream& stream, size_t size) {
|
|
|
|
char *dest = _streamLoad(stream, size);
|
|
|
|
bool ret = false;
|
|
|
|
if (dest) {
|
|
|
|
setCertificate(dest);
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WiFiClientSecure::loadPrivateKey(Stream& stream, size_t size) {
|
|
|
|
char *dest = _streamLoad(stream, size);
|
|
|
|
bool ret = false;
|
|
|
|
if (dest) {
|
|
|
|
setPrivateKey(dest);
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-04-16 16:34:39 +02:00
|
|
|
int WiFiClientSecure::lastError(char *buf, const size_t size)
|
|
|
|
{
|
|
|
|
if (!_lastError) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-02-18 10:44:44 +01:00
|
|
|
mbedtls_strerror(_lastError, buf, size);
|
2018-04-16 16:34:39 +02:00
|
|
|
return _lastError;
|
|
|
|
}
|
2018-11-26 23:25:08 +01:00
|
|
|
|
|
|
|
void WiFiClientSecure::setHandshakeTimeout(unsigned long handshake_timeout)
|
|
|
|
{
|
|
|
|
sslclient->handshake_timeout = handshake_timeout * 1000;
|
2018-11-29 11:34:55 +01:00
|
|
|
}
|