2017-02-10 23:20:24 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
|
|
|
_CA_cert = NULL;
|
|
|
|
_cert = NULL;
|
|
|
|
_private_key = NULL;
|
2017-03-10 15:52:50 +01:00
|
|
|
next = NULL;
|
2017-02-10 23:20:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
WiFiClientSecure::WiFiClientSecure(int sock)
|
|
|
|
{
|
|
|
|
_connected = false;
|
|
|
|
|
|
|
|
sslclient = new sslclient_context;
|
|
|
|
ssl_init(sslclient);
|
|
|
|
sslclient->socket = sock;
|
|
|
|
|
|
|
|
if (sock >= 0) {
|
|
|
|
_connected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_CA_cert = NULL;
|
|
|
|
_cert = NULL;
|
|
|
|
_private_key = NULL;
|
2017-03-10 15:52:50 +01:00
|
|
|
next = NULL;
|
2017-02-10 23:20:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WiFiClientSecure::~WiFiClientSecure()
|
|
|
|
{
|
|
|
|
stop();
|
2017-05-19 10:18:20 +02:00
|
|
|
delete sslclient;
|
2017-02-10 23:20:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WiFiClientSecure &WiFiClientSecure::operator=(const WiFiClientSecure &other)
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
sslclient->socket = other.sslclient->socket;
|
|
|
|
_connected = other._connected;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiClientSecure::stop()
|
|
|
|
{
|
2017-03-10 15:52:50 +01:00
|
|
|
if (sslclient->socket >= 0) {
|
|
|
|
close(sslclient->socket);
|
2017-02-10 23:20:24 +01:00
|
|
|
sslclient->socket = -1;
|
|
|
|
_connected = false;
|
|
|
|
}
|
2017-03-10 15:52:50 +01:00
|
|
|
stop_ssl_socket(sslclient, _CA_cert, _cert, _private_key);
|
2017-02-10 23:20:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClientSecure::connect(IPAddress ip, uint16_t port)
|
|
|
|
{
|
2017-03-10 15:52:50 +01:00
|
|
|
return connect(ip, port, _CA_cert, _cert, _private_key);
|
2017-02-10 23:20:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClientSecure::connect(const char *host, uint16_t port)
|
|
|
|
{
|
2017-03-10 15:52:50 +01:00
|
|
|
return connect(host, port, _CA_cert, _cert, _private_key);
|
2017-02-10 23:20:24 +01:00
|
|
|
}
|
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
int WiFiClientSecure::connect(IPAddress ip, uint16_t port, const char *_CA_cert, const char *_cert, const char *_private_key)
|
2017-02-10 23:20:24 +01:00
|
|
|
{
|
|
|
|
int ret = start_ssl_client(sslclient, ip, port, _CA_cert, _cert, _private_key);
|
|
|
|
if (ret < 0) {
|
|
|
|
log_e("lwip_connect_r: %d", errno);
|
2017-03-10 15:52:50 +01:00
|
|
|
stop();
|
|
|
|
return 0;
|
2017-02-10 23:20:24 +01:00
|
|
|
}
|
|
|
|
_connected = true;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
int WiFiClientSecure::connect(const char *host, uint16_t port, const char *_CA_cert, const char *_cert, const char *_private_key)
|
2017-02-10 23:20:24 +01:00
|
|
|
{
|
|
|
|
struct hostent *server;
|
|
|
|
server = gethostbyname(host);
|
|
|
|
if (server == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
IPAddress srv((const uint8_t *)(server->h_addr));
|
|
|
|
return connect(srv, port, _CA_cert, _cert, _private_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t WiFiClientSecure::write(uint8_t data)
|
|
|
|
{
|
|
|
|
return write(&data, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClientSecure::read()
|
|
|
|
{
|
|
|
|
uint8_t data = 0;
|
|
|
|
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) {
|
2017-03-10 15:52:50 +01:00
|
|
|
|
2017-02-10 23:20:24 +01:00
|
|
|
stop();
|
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClientSecure::read(uint8_t *buf, size_t size)
|
|
|
|
{
|
|
|
|
if (!available()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int res = get_ssl_receive(sslclient, buf, size);
|
2017-03-10 15:52:50 +01:00
|
|
|
if (res < 0) {
|
|
|
|
|
2017-02-10 23:20:24 +01:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClientSecure::available()
|
|
|
|
{
|
|
|
|
if (!_connected) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int res = data_to_read(sslclient);
|
2017-03-10 15:52:50 +01:00
|
|
|
if (res < 0 ) {
|
|
|
|
stop();
|
|
|
|
}
|
2017-02-10 23:20:24 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t WiFiClientSecure::connected()
|
|
|
|
{
|
|
|
|
uint8_t dummy = 0;
|
|
|
|
read(&dummy, 0);
|
|
|
|
|
|
|
|
return _connected;
|
|
|
|
}
|
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
void WiFiClientSecure::setCACert (const char *rootCA)
|
2017-02-10 23:20:24 +01:00
|
|
|
{
|
|
|
|
_CA_cert = rootCA;
|
|
|
|
}
|
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
void WiFiClientSecure::setCertificate (const char *client_ca)
|
2017-02-10 23:20:24 +01:00
|
|
|
{
|
|
|
|
_cert = client_ca;
|
|
|
|
}
|
|
|
|
|
2017-03-10 15:52:50 +01:00
|
|
|
void WiFiClientSecure::setPrivateKey (const char *private_key)
|
2017-02-10 23:20:24 +01:00
|
|
|
{
|
|
|
|
_private_key = private_key;
|
|
|
|
}
|
|
|
|
|