@@ -42,14 +42,15 @@ WebSocketsClient::~WebSocketsClient() {
42
42
void WebSocketsClient::begin (const char *host, uint16_t port, const char * url, const char * protocol) {
43
43
_host = host;
44
44
_port = port;
45
- #if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32 )
45
+ #if defined(HAS_SSL )
46
46
_fingerprint = " " ;
47
+ _CA_cert = NULL ;
47
48
#endif
48
49
49
50
_client.num = 0 ;
50
51
_client.status = WSC_NOT_CONNECTED;
51
52
_client.tcp = NULL ;
52
- #if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32 )
53
+ #if defined(HAS_SSL )
53
54
_client.isSSL = false ;
54
55
_client.ssl = NULL ;
55
56
#endif
@@ -92,16 +93,24 @@ void WebSocketsClient::begin(IPAddress host, uint16_t port, const char * url, co
92
93
return begin (host.toString ().c_str (), port, url, protocol);
93
94
}
94
95
95
- #if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32 )
96
+ #if defined(HAS_SSL )
96
97
void WebSocketsClient::beginSSL (const char *host, uint16_t port, const char * url, const char * fingerprint, const char * protocol) {
97
98
begin (host, port, url, protocol);
98
99
_client.isSSL = true ;
99
100
_fingerprint = fingerprint;
101
+ _CA_cert = NULL ;
100
102
}
101
103
102
104
void WebSocketsClient::beginSSL (String host, uint16_t port, String url, String fingerprint, String protocol) {
103
105
beginSSL (host.c_str (), port, url.c_str (), fingerprint.c_str (), protocol.c_str ());
104
106
}
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
+ }
105
114
#endif
106
115
107
116
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
113
122
beginSocketIO (host.c_str (), port, url.c_str (), protocol.c_str ());
114
123
}
115
124
116
- #if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32 )
125
+ #if defined(HAS_SSL )
117
126
void WebSocketsClient::beginSocketIOSSL (const char *host, uint16_t port, const char * url, const char * protocol) {
118
127
begin (host, port, url, protocol);
119
128
_client.isSocketIO = true ;
@@ -124,6 +133,14 @@ void WebSocketsClient::beginSocketIOSSL(const char *host, uint16_t port, const c
124
133
void WebSocketsClient::beginSocketIOSSL (String host, uint16_t port, String url, String protocol) {
125
134
beginSocketIOSSL (host.c_str (), port, url.c_str (), protocol.c_str ());
126
135
}
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
+ }
127
144
#endif
128
145
129
146
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
@@ -147,6 +164,16 @@ void WebSocketsClient::loop(void) {
147
164
}
148
165
_client.ssl = new WiFiClientSecure ();
149
166
_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
+ }
150
177
} else {
151
178
DEBUG_WEBSOCKETS (" [WS-Client] connect ws...\n " );
152
179
if (_client.tcp ) {
@@ -710,9 +737,11 @@ void WebSocketsClient::connectedCb() {
710
737
_client.tcp ->setTimeout (WEBSOCKETS_TCP_TIMEOUT);
711
738
#endif
712
739
713
- #if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
740
+ #if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) || WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32
714
741
_client.tcp ->setNoDelay (true );
742
+ #endif
715
743
744
+ #if defined(HAS_SSL)
716
745
if (_client.isSSL && _fingerprint.length ()) {
717
746
if (!_client.ssl ->verify (_fingerprint.c_str (), _host.c_str ())) {
718
747
DEBUG_WEBSOCKETS (" [WS-Client] certificate mismatch\n " );
@@ -806,4 +835,4 @@ void WebSocketsClient::enableHeartbeat(uint32_t pingInterval, uint32_t pongTimeo
806
835
*/
807
836
void WebSocketsClient::disableHeartbeat (){
808
837
_client.pingInterval = 0 ;
809
- }
838
+ }
0 commit comments