8000 add setCACert based on #434 · FEmbed/arduinoWebSockets@c361895 · GitHub
[go: up one dir, main page]

Skip to content 65F5

Commit c361895

Browse files
committed
add setCACert based on Links2004#434
1 parent 31bade1 commit c361895

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

src/WebSockets.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757

5858
#if defined(ESP8266) || defined(ESP32)
5959

60+
#define HAS_SSL
6061
#define WEBSOCKETS_MAX_DATA_SIZE (15*1024)
6162
#define WEBSOCKETS_USE_BIG_MEM
6263
#define GET_FREE_HEAP ESP.getFreeHeap()

src/WebSocketsClient.cpp

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ WebSocketsClient::~WebSocketsClient() {
4242
void WebSocketsClient::begin(const char *host, uint16_t port, const char * url, const char * protocol) {
4343
_host = host;
4444
_port = port;
45-
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
45+
#if defined(HAS_SSL)
4646
_fingerprint = "";
47+
_CA_cert = NULL;
4748
#endif
4849

4950
_client.num = 0;
5051
_client.status = WSC_NOT_CONNECTED;
5152
_client.tcp = NULL;
52-
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
53+
#if defined(HAS_SSL)
5354
_client.isSSL = false;
5455
_client.ssl = NULL;
5556
#endif
@@ -92,16 +93,24 @@ void WebSocketsClient::begin(IPAddress host, uint16_t port, const char * url, co
9293
return begin(host.toString().c_str(), port, url, protocol);
9394
}
9495

95-
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
96+
#if defined(HAS_SSL)
9697
void WebSocketsClient::beginSSL(const char *host, uint16_t port, const char * url, const char * fingerprint, const char * protocol) {
9798
begin(host, port, url, protocol);
9899
_client.isSSL = true;
99100
_fingerprint = fingerprint;
101+
_CA_cert = NULL;
100102
}
101103

102104
void WebSocketsClient::beginSSL(String host, uint16_t port, String url, String fingerprint, String protocol) {
103105
beginSSL(host.c_str(), port, url.c_str(), fingerprint.c_str(), protocol.c_str());
104106
}
107+
108+
void WebSocketsClient::beginSslWithCA(const char *host, uint16_t port, const char * url, const char * CA_cert, const char * protocol) {
109+
begin(host, port, url, protocol);
110+
_client.isSSL = true;
111+
_fingerprint = "";
112+
_CA_cert = CA_cert;
113+
}
105114
#endif
106115

107116
void WebSocketsClient::beginSocketIO(const char *host, uint16_t port, const char * url, const char * protocol) {
@@ -113,7 +122,7 @@ void WebSocketsClient::beginSocketIO(String host, uint16_t port, String url, Str
113122
beginSocketIO(host.c_str(), port, url.c_str(), protocol.c_str());
114123
}
115124

116-
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
125+
#if defined(HAS_SSL)
117126
void WebSocketsClient::beginSocketIOSSL(const char *host, uint16_t port, const char * url, const char * protocol) {
118127
begin(host, port, url, protocol);
119128
_client.isSocketIO = true;
@@ -124,6 +133,14 @@ void WebSocketsClient::beginSocketIOSSL(const char *host, uint16_t port, const c
124133
void WebSocketsClient::beginSocketIOSSL(String host, uint16_t port, String url, String protocol) {
125134
beginSocketIOSSL(host.c_str(), port, url.c_str(), protocol.c_str());
126135
}
136+
137+
void WebSocketsClient::beginSocketIOSSLWithCA(const char *host, uint16_t port, const char * url, const char * CA_cert, const char * protocol) {
138+
begin(host, port, url, protocol);
139+
_client.isSocketIO = true;
140+
_client.isSSL = true;
141+
_fingerprint = "";
142+
_CA_cert = CA_cert;
143+
}
127144
#endif
128145

129146
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
@@ -147,6 +164,16 @@ void WebSocketsClient::loop(void) {
147164
}
148165
_client.ssl = new WiFiClientSecure();
149166
_client.tcp = _client.ssl;
167+
if(_CA_cert) {
168+
DEBUG_WEBSOCKETS("[WS-Client] setting CA certificate");
169+
#if defined(ESP32)
170+
_client.ssl->setCACert(_CA_cert);
171+
#elif defined(ESP8266)
172+
_client.ssl->setCACert((const uint8_t *)_CA_cert, strlen(_CA_cert) + 1);
173+
#else
174+
#error setCACert not implemented
175+
#endif
176+
}
150177
} else {
151178
DEBUG_WEBSOCKETS("[WS-Client] connect ws...\n");
152179
if(_client.tcp) {
@@ -710,9 +737,11 @@ void WebSocketsClient::connectedCb() {
710737
_client.tcp->setTimeout(WEBSOCKETS_TCP_TIMEOUT);
711738
#endif
712739

713-
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
740+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32
714741
_client.tcp->setNoDelay(true);
742+
#endif
715743

744+
#if defined(HAS_SSL)
716745
if(_client.isSSL && _fingerprint.length()) {
717746
if(!_client.ssl->verify(_fingerprint.c_str(), _host.c_str())) {
718747
DEBUG_WEBSOCKETS("[WS-Client] certificate mismatch\n");
@@ -806,4 +835,4 @@ void WebSocketsClient::enableHeartbeat(uint32_t pingInterval, uint32_t pongTimeo
806835
*/
807836
void WebSocketsClient::disableHeartbeat(){
808837
_client.pingInterval = 0;
809-
}
838+
}

src/WebSocketsClient.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,19 @@ class WebSocketsClient: private WebSockets {
4343
void begin(String host, uint16_t port, String url = "/", String protocol = "arduino");
4444
void begin(IPAddress host, uint16_t port, const char * url = "/", const char * protocol = "arduino");
4545

46-
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
46+
#if defined(HAS_SSL)
4747
void beginSSL(const char *host, uint16_t port, const char * url = "/", const char * = "", const char * protocol = "arduino");
4848
void beginSSL(String host, uint16_t port, String url = "/", String fingerprint = "", String protocol = "arduino");
49+
void beginSslWithCA(const char *host, uint16_t port, const char * url = "/", const char * CA_cert = NULL, const char * protocol = "arduino");
4950
#endif
5051

5152
void beginSocketIO(const char *host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
5253
void beginSocketIO(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
5354

54-
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
55+
#if defined(HAS_SSL)
5556
void beginSocketIOSSL(const char *host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
5657
void beginSocketIOSSL(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
58+
void beginSocketIOSSLWithCA(const char *host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * CA_cert = NULL, const char * protocol = "arduino");
5759
#endif
5860

5961
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
@@ -93,8 +95,9 @@ class WebSocketsClient: private WebSockets {
9395
String _host;
9496
uint16_t _port;
9597

96-
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32)
98+
#if defined(HAS_SSL)
9799
String _fingerprint;
100+
const char *_CA_cert;
98101
#endif
99102
WSclient_t _client;
100103

0 commit comments

Comments
 (0)
0