Skip to content

Commit 2c9f348

Browse files
committed
Fix handshake issue for connection upgrade.
1 parent 6637b60 commit 2c9f348

10 files changed

+224
-20
lines changed

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ESP_SSLClient",
3-
"version": "2.1.5",
3+
"version": "2.1.6",
44
"keywords": "communication, REST, esp32, esp8266, arduino",
55
"description": "This library provided the Secure Layer Networking (SSL/TLS) TCP Client for ESP8266, ESP32 and Raspberry Pi RP2040, Teensy, SAMD, AVR and other Arduino devices (except for avr) that support external networking interfaces e.g., WiFiClient, EthernetClient and GSMClient.",
66
"repository": {

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name=ESP_SSLClient
22

3-
version=2.1.5
3+
version=2.1.6
44

55
author=Mobizt
66

src/ESP_SSLClient.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
*
3-
* The ESP SSL Client Class, ESP_SSLClient.h v2.1.5
3+
* The ESP SSL Client Class, ESP_SSLClient.h v2.1.6
44
*
5-
* Created August 22, 2023
5+
* Created August 27, 2023
66
*
77
* The MIT License (MIT)
88
* Copyright (c) 2023 K. Suwatchai (Mobizt)

src/client/BSSL_SSL_Client.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* BSSL_SSL_Client library v1.0.10 for Arduino devices.
2+
* BSSL_SSL_Client library v1.0.11 for Arduino devices.
33
*
4-
* Created August 22, 2003
4+
* Created August 27, 2003
55
*
66
* This work contains codes based on WiFiClientSecure from Earle F. Philhower and SSLClient from OSU OPEnS Lab.
77
*
@@ -435,8 +435,6 @@ int BSSL_SSL_Client::connectSSL(IPAddress ip, uint16_t port)
435435
if (!mIsClientInitialized(true))
436436
return 0;
437437

438-
validate(ip, port);
439-
440438
if (!_basic_client->connected() && !mConnectBasicClient(nullptr, ip, port))
441439
return 0;
442440

@@ -452,8 +450,6 @@ int BSSL_SSL_Client::connectSSL(const char *host, uint16_t port)
452450
if (!mIsClientInitialized(true))
453451
return 0;
454452

455-
validate(host, port);
456-
457453
if (!_basic_client->connected() && !mConnectBasicClient(host, IPAddress(), port))
458454
return 0;
459455

src/client/BSSL_SSL_Client.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* BSSL_SSL_Client library v1.0.10 for Arduino devices.
2+
* BSSL_SSL_Client library v1.0.11 for Arduino devices.
33
*
4-
* Created August 22, 2003
4+
* Created August 27, 2003
55
*
66
* This work contains codes based on WiFiClientSecure from Earle F. Philhower and SSLClient from OSU OPEnS Lab.
77
*
@@ -98,7 +98,7 @@ class BSSL_SSL_Client : public Client
9898

9999
uint8_t connected() override;
100100

101-
void validate(const char* host, uint16_t port);
101+
void validate(const char *host, uint16_t port);
102102

103103
void validate(IPAddress ip, uint16_t port);
104104

@@ -128,8 +128,6 @@ class BSSL_SSL_Client : public Client
128128

129129
int connectSSL(const char *host, uint16_t port);
130130

131-
132-
133131
void stop() override;
134132

135133
void setTimeout(unsigned int timeoutMs);
@@ -342,7 +340,7 @@ class BSSL_SSL_Client : public Client
342340
unsigned long _handshake_timeout = 60000;
343341
bool _isSSLEnabled = false;
344342
String _host;
345-
uint16_t _port;
343+
uint16_t _port = 0;
346344
IPAddress _ip;
347345
};
348346

src/client/BSSL_TCP_Client.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* BSSL_TCP_Client v2.0.11 for Arduino devices.
2+
* BSSL_TCP_Client v2.0.12 for Arduino devices.
33
*
4-
* Created August 22, 2023
4+
* Created August 27, 2023
55
*
66
* The MIT License (MIT)
77
* Copyright (c) 2023 K. Suwatchai (Mobizt)

src/client/BSSL_TCP_Client.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* BSSL_TCP_Client v2.0.11 for Arduino devices.
2+
* BSSL_TCP_Client v2.0.12 for Arduino devices.
33
*
4-
* Created August 22, 2023
4+
* Created August 27, 2023
55
*
66
* The MIT License (MIT)
77
* Copyright (c) 2023 K. Suwatchai (Mobizt)

src/client/ESP_SSLClient.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
*
3+
* The ESP SSL Client Class, ESP_SSLClient.h v2.0.6
4+
*
5+
* Created August 6, 2023
6+
*
7+
* The MIT License (MIT)
8+
* Copyright (c) 2023 K. Suwatchai (Mobizt)
9+
*
10+
*
11+
* Permission is hereby granted, free of charge, to any person returning a copy of
12+
* this software and associated documentation files (the "Software"), to deal in
13+
* the Software without restriction, including without limitation the rights to
14+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
15+
* the Software, and to permit persons to whom the Software is furnished to do so,
16+
* subject to the following conditions:
17+
*
18+
* The above copyright notice and this permission notice shall be included in all
19+
* copies or substantial portions of the Software.
20+
*
21+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
23+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
24+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
25+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27+
*/
28+
29+
#ifndef ESP_SSLCLIENT_H
30+
#define ESP_SSLCLIENT_H
31+
32+
#include "ESP_SSLClient_FS.h"
33+
34+
#ifndef SSLCLIENT_CONNECTION_UPGRADABLE
35+
#define SSLCLIENT_CONNECTION_UPGRADABLE
36+
#endif
37+
38+
#include "client/BSSL_TCP_Client.h"
39+
40+
class ESP_SSLClient : public BSSL_TCP_Client
41+
{
42+
public:
43+
ESP_SSLClient(){};
44+
~ESP_SSLClient(){};
45+
};
46+
47+
#endif

src/client/ESP_SSLClient_Const.h

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#ifndef ESP_SSLCLIENT_CONST_H
2+
#define ESP_SSLCLIENT_CONST_H
3+
4+
#pragma GCC diagnostic ignored "-Wunused-function"
5+
#pragma GCC diagnostic ignored "-Wvla"
6+
7+
#include "ESP_SSLClient_FS.h"
8+
9+
#include <Arduino.h>
10+
#include <Client.h>
11+
12+
#define ESP_SSLCLIENT_VALID_TIMESTAMP 1690979919
13+
14+
#ifndef SSLCLIENT_CONNECTION_UPGRADABLE
15+
#define SSLCLIENT_CONNECTION_UPGRADABLE
16+
#endif
17+
18+
#ifdef ESP_SSLCLIENT_ENABLE_DEBUG
19+
#if !defined(ESP_SSLCLIENT_DEBUG_PORT)
20+
#define ESP_SSLCLIENT_DEBUG_PORT Serial
21+
#endif
22+
#define ESP_SSLCLIENT_DEBUG_PRINT ESP_SSLCLIENT_DEBUG_PORT.print
23+
#else
24+
#define ESP_SSLCLIENT_DEBUG_PRINT(...)
25+
#endif
26+
27+
enum esp_ssl_client_debug_level
28+
{
29+
esp_ssl_debug_none = 0,
30+
esp_ssl_debug_error = 1,
31+
esp_ssl_debug_warn = 2,
32+
esp_ssl_debug_info = 3,
33+
esp_ssl_debug_dump = 4
34+
};
35+
36+
enum esp_ssl_client_error_types
37+
{
38+
esp_ssl_ok,
39+
esp_ssl_connection_fail,
40+
esp_ssl_write_error,
41+
esp_ssl_read_error,
42+
esp_ssl_out_of_memory,
43+
esp_ssl_internal_error
44+
};
45+
46+
#if defined(ESP_SSLCLIENT_ENABLE_DEBUG)
47+
48+
static void esp_ssl_debug_print_prefix(const char *func_name, int level)
49+
{
50+
ESP_SSLCLIENT_DEBUG_PRINT(PSTR("> "));
51+
// print the debug level
52+
switch (level)
53+
{
54+
case esp_ssl_debug_info:
55+
ESP_SSLCLIENT_DEBUG_PRINT(PSTR("INFO."));
56+
break;
57+
case esp_ssl_debug_warn:
58+
ESP_SSLCLIENT_DEBUG_PRINT(PSTR("WARN."));
59+
break;
60+
case esp_ssl_debug_error:
61+
ESP_SSLCLIENT_DEBUG_PRINT(PSTR("ERROR."));
62+
break;
63+
default:
64+
break;
65+
}
66+
67+
// print the function name
68+
ESP_SSLCLIENT_DEBUG_PRINT(PSTR(""));
69+
ESP_SSLCLIENT_DEBUG_PRINT(func_name);
70+
ESP_SSLCLIENT_DEBUG_PRINT(PSTR(": "));
71+
}
72+
73+
static void esp_ssl_debug_print(PGM_P msg, int debug_level, int level, const char *func_name)
74+
{
75+
if (debug_level >= level)
76+
{
77+
esp_ssl_debug_print_prefix(func_name, level);
78+
ESP_SSLCLIENT_DEBUG_PRINT(msg);
79+
ESP_SSLCLIENT_DEBUG_PRINT("\r\n");
80+
}
81+
}
82+
83+
#endif
84+
85+
static uint8_t htoi(unsigned char c)
86+
{
87+
if (c >= '0' && c <= '9')
88+
return c - '0';
89+
else if (c >= 'A' && c <= 'F')
90+
return 10 + c - 'A';
91+
else if (c >= 'a' && c <= 'f')
92+
return 10 + c - 'a';
93+
else
94+
return 255;
95+
}
96+
97+
// Helper function which aborts a TLS handshake by sending TLS
98+
// ClientAbort and ClientClose messages.
99+
static bool send_abort(Client *probe, bool supportsLen)
100+
{
101+
// If we're still connected, send the appropriate notice that
102+
// we're aborting the handshake per RFCs.
103+
static const uint8_t clientAbort_P[] PROGMEM = {
104+
0x15 /*alert*/, 0x03, 0x03 /*TLS 1.2*/, 0x00, 0x02,
105+
1, 90 /* warning: user_cancelled */
106+
};
107+
static const uint8_t clientClose_P[] PROGMEM = {
108+
0x15 /*alert*/, 0x03, 0x03 /*TLS 1.2*/, 0x00, 0x02,
109+
1, 0 /* warning: close_notify */
110+
};
111+
if (probe->connected())
112+
{
113+
uint8_t msg[sizeof(clientAbort_P)];
114+
memcpy_P(msg, clientAbort_P, sizeof(clientAbort_P));
115+
probe->write(msg, sizeof(clientAbort_P));
116+
memcpy_P(msg, clientClose_P, sizeof(clientClose_P));
117+
probe->write(msg, sizeof(clientClose_P));
118+
}
119+
return supportsLen;
120+
}
121+
122+
const uint16_t _secure_ports[26] = {443 /* HTTPS */, 465 /* SMTP */, 563 /* NNTP */, 636 /* LDAPS */, 695 /* IEEE-MMS-SSL */, 832 /* NETCONF */, 853 /* DNS */, 989 /* FTPS */, 990 /* FTPS */, 992 /* Telnet */, 993 /* IMAP */, 995 /* POP3 */, 4116 /* Smartcard */, 4843 /* OPC */, 5061 /* SIP */, 5085 /* LLIP */, 5349 /* NAT */, 5671 /* AMQP */, 5986 /* WinRM-HTTPS */, 6513 /* NETCONF */, 6514 /* Syslog */, 6515 /* Elipse RPC */, 6619 /* OFTP */, 8243 /* Apache Synapse */, 8403 /* GxFWD */, 8883 /* MQTT */};
123+
124+
#endif

src/client/ESP_SSLClient_FS.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#ifndef ESP_SSLClient_FS_H
2+
#define ESP_SSLClient_FS_H
3+
4+
#if (defined(ESP8266) || defined(ARDUINO_ARCH_RP2040)) && !defined(ARDUINO_NANO_RP2040_CONNECT)
5+
// for ESP8266 and Raspberry Pi Pico (RP2040) only except for Arduino Nano RP2040 Connect
6+
#define USE_EMBED_SSL_ENGINE
7+
#else
8+
#define USE_LIB_SSL_ENGINE
9+
#endif
10+
11+
#pragma once
12+
13+
// for enable debugging
14+
#define ESP_SSLCLIENT_ENABLE_DEBUG
15+
16+
/** Call ssl_client.setDebugLevel(x) to set the debug
17+
* esp_ssl_debug_none = 0
18+
* esp_ssl_debug_error = 1
19+
* esp_ssl_debug_warn = 2
20+
* esp_ssl_debug_info = 3
21+
* esp_ssl_debug_dump = 4
22+
*/
23+
24+
// for debug port
25+
#define ESP_SSLCLIENT_DEBUG_PORT Serial
26+
27+
// for SSL Error String
28+
#define ESP_SSLCLIENT_ENABLE_SSL_ERROR_STRING
29+
30+
// for Filesystem support that required for CertStore
31+
#define ESP_SSLCLIENT_USE_FILESYSTEM
32+
33+
#if defined __has_include
34+
#if __has_include(<Custom_ESP_SSLClient_FS.h>)
35+
#include "Custom_ESP_SSLClient_FS"
36+
#endif
37+
#endif
38+
39+
#endif

0 commit comments

Comments
 (0)