Skip to content

Commit dc6fd04

Browse files
committed
fix #896 ESP32 Arduino changed setCACertBundle args
1 parent 7da1dc5 commit dc6fd04

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

examples/esp32/WebSocketClientSSLBundle/WebSocketClientSSLBundle.ino

+6
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,13 @@ void setup() {
112112
// server address, port and URL. This server can be flakey.
113113
// Expected response: Request served by 0123456789abcdef
114114
// webSocket.beginSslWithBundle("echo.websocket.org", 443, "/", rootca_crt_bundle_start, "");
115+
// ESP32 3.0.4 or higher needs the size of the bundle
116+
// webSocket.beginSslWithBundle("echo.websocket.org", 443, "/", rootca_crt_bundle_start, sizeof(rootca_crt_bundle_start), "");
117+
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 4)
118+
webSocket.beginSslWithBundle("echo.websocket.org", 443, "/", NULL, 0, "");
119+
#else
115120
webSocket.beginSslWithBundle("echo.websocket.org", 443, "/", NULL, "");
121+
#endif
116122

117123
// event handler
118124
webSocket.onEvent(webSocketEvent);

src/WebSocketsClient.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ void WebSocketsClient::begin(const char * host, uint16_t port, const char * url,
5050
_CA_cert = NULL;
5151
#ifdef ESP32
5252
_CA_bundle = NULL;
53+
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 4)
54+
_CA_bundle_size = 0;
55+
#endif
5356
#endif
5457
#endif
5558

@@ -124,13 +127,25 @@ void WebSocketsClient::beginSslWithCA(const char * host, uint16_t port, const ch
124127
_CA_cert = CA_cert;
125128
_CA_bundle = NULL;
126129
}
130+
131+
#if defined(ESP32) && ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 4)
132+
void WebSocketsClient::beginSslWithBundle(const char * host, uint16_t port, const char * url, const uint8_t * CA_bundle, size_t CA_bundle_size, const char * protocol) {
133+
begin(host, port, url, protocol);
134+
_client.isSSL = true;
135+
_fingerprint = SSL_FINGERPRINT_NULL;
136+
_CA_cert = NULL;
137+
_CA_bundle = CA_bundle;
138+
_CA_bundle_size = CA_bundle_size;
139+
}
140+
#else
127141
void WebSocketsClient::beginSslWithBundle(const char * host, uint16_t port, const char * url, const uint8_t * CA_bundle, const char * protocol) {
128142
begin(host, port, url, protocol);
129143
_client.isSSL = true;
130144
_fingerprint = SSL_FINGERPRINT_NULL;
131145
_CA_cert = NULL;
132146
_CA_bundle = CA_bundle;
133147
}
148+
#endif
134149

135150
#else
136151
void WebSocketsClient::beginSSL(const char * host, uint16_t port, const char * url, const uint8_t * fingerprint, const char * protocol) {
@@ -247,9 +262,11 @@ void WebSocketsClient::loop(void) {
247262
#if defined(ESP32)
248263
} else if(_CA_bundle) {
249264
DEBUG_WEBSOCKETS("[WS-Client] setting CA bundle");
265+
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 4)
266+
_client.ssl->setCACertBundle(_CA_bundle, _CA_bundle_size);
267+
#else
250268
_client.ssl->setCACertBundle(_CA_bundle);
251269
#endif
252-
#if defined(ESP32)
253270
} else if(!SSL_FINGERPRINT_IS_SET) {
254271
_client.ssl->setInsecure();
255272
#elif defined(SSL_BARESSL)

src/WebSocketsClient.h

+9
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ class WebSocketsClient : protected WebSockets {
5454
#endif
5555
void beginSslWithCA(const char * host, uint16_t port, const char * url = "/", const char * CA_cert = NULL, const char * protocol = "arduino");
5656
#ifdef ESP32
57+
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 4)
58+
void beginSslWithBundle(const char * host, uint16_t port, const char * url = "/", const uint8_t * CA_bundle = NULL, size_t CA_bundle_size = 0, const char * protocol = "arduino");
59+
#else
5760
void beginSslWithBundle(const char * host, uint16_t port, const char * url = "/", const uint8_t * CA_bundle = NULL, const char * protocol = "arduino");
5861
#endif
62+
#endif
5963
#endif
6064

6165
void beginSocketIO(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
@@ -116,6 +120,11 @@ class WebSocketsClient : protected WebSockets {
116120
String _fingerprint;
117121
const char * _CA_cert;
118122
const uint8_t * _CA_bundle;
123+
#if defined(ESP32)
124+
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 4)
125+
size_t _CA_bundle_size;
126+
#endif
127+
#endif
119128
#define SSL_FINGERPRINT_IS_SET (_fingerprint.length())
120129
#define SSL_FINGERPRINT_NULL ""
121130
#else

0 commit comments

Comments
 (0)