@@ -159,6 +159,101 @@ class WiFiClientSecureCtx : public WiFiClient {
159
159
// consume bytes after use (see peekBuffer)
160
160
virtual void peekConsume(size_t consume) override;
161
161
#endif
162
+
163
+ // ESP32 compatibility
164
+ void setCACert (const char *rootCA) {
165
+ if (_esp32_ta) {
166
+ delete _esp32_ta;
167
+ }
168
+ _esp32_ta = new X509List (rootCA);
169
+ }
170
+ void setCertificate (const char *client_ca) {
171
+ if (_esp32_chain) {
172
+ delete _esp32_chain;
173
+ }
174
+ _esp32_chain = new X509List (client_ca);
175
+ }
176
+ void setPrivateKey (const char *private_key) {
177
+ if (_esp32_sk) {
178
+ delete _esp32_sk;
179
+ }
180
+ _esp32_sk = new PrivateKey (private_key);
181
+ }
182
+ bool loadCACert (Stream& stream, size_t size) {
183
+ bool ret = false ;
184
+ auto buff = new char [size];
185
+ if (size == stream.readBytes (buff, size)) {
186
+ setCACert (buff);
187
+ ret = true ;
188
+ }
189
+ delete[] buff;
190
+ return ret;
191
+ }
192
+ bool loadCertificate (Stream& stream, size_t size) {
193
+ bool ret = false ;
194
+ auto buff = new char [size];
195
+ if (size == stream.readBytes (buff, size)) {
196
+ setCertificate (buff);
197
+ ret = true ;
198
+ }
199
+ delete[] buff;
200
+ return ret;
201
+ }
202
+ bool loadPrivateKey (Stream& stream, size_t size) {
203
+ bool ret = false ;
204
+ auto buff = new char [size];
205
+ if (size == stream.readBytes (buff, size)) {
206
+ setPrivateKey (buff);
207
+ ret = true ;
208
+ }
209
+ delete[] buff;
210
+ return ret;
211
+ }
212
+ int connect (IPAddress ip, uint16_t port, int32_t timeout) {
213
+ auto save = _timeout;
214
+ _timeout = timeout * 1000 ; // timeout is in secs, _timeout in milliseconds
215
+ auto ret = connect (ip, port);
216
+ _timeout = save;
217
+ return ret;
218
+ }
219
+ int connect (const char *host, uint16_t port, int32_t timeout) {
220
+ auto save = _timeout;
221
+ _timeout = timeout * 1000 ; // timeout is in secs, _timeout in milliseconds
222
+ auto ret = connect (host, port);
223
+ _timeout = save;
224
+ return ret;
225
+ }
226
+ int connect (IPAddress ip, uint16_t port, const char *rootCABuff, const char *cli_cert, const char *cli_key) {
227
+ if (_esp32_ta) {
228
+ delete _esp32_ta;
229
+ _esp32_ta = nullptr ;
230
+ }
231
+ if (_esp32_chain) {
232
+ delete _esp32_chain;
233
+ _esp32_chain = nullptr ;
234
+ }
235
+ if (_esp32_sk) {
236
+ delete _esp32_sk;
237
+ _esp32_sk = nullptr ;
238
+ }
239
+ if (rootCABuff) {
240
+ setCertificate (rootCABuff);
241
+ }
242
+ if (cli_cert && cli_key) {
243
+ setCertificate (cli_cert);
244
+ setPrivateKey (cli_key);
245
+ }
246
+ return connect (ip, port);
247
+ }
248
+ int connect (const char *host, uint16_t port, const char *rootCABuff, const char *cli_cert, const char *cli_key) {
249
+ IPAddress ip;
250
+ if (WiFi.hostByName (host, ip, _timeout)) {
251
+ return connect (ip, port, rootCABuff, cli_cert, cli_key);
252
+ } else {
253
+ return 0 ;
254
+ }
255
+ }
256
+
162
257
protected:
163
258
bool _connectSSL (const char *hostName); // Do initial SSL handshake
164
259
@@ -241,6 +336,11 @@ class WiFiClientSecureCtx : public WiFiClient {
241
336
bool _installServerX509Validator (const X509List *client_CA_ta); // Setup X509 client cert validation, if supplied
242
337
243
338
uint8_t *_streamLoad (Stream& stream, size_t size);
339
+
340
+ // ESP32 compatibility
341
+ X509List *_esp32_ta = nullptr ;
342
+ X509List *_esp32_chain = nullptr ;
343
+ PrivateKey *_esp32_sk = nullptr ;
244
344
}; // class WiFiClientSecureCtx
245
345
246
346
@@ -443,6 +543,40 @@ class WiFiClientSecure : public WiFiClient {
443
543
return _ctx->peekConsume(consume);
444
544
}
445
545
#endif
546
+
547
+ // ESP32 compatibility
548
+ void setCACert (const char *rootCA) {
549
+ return _ctx->setCACert (rootCA);
550
+ }
551
+ void setCertificate (const char *client_ca) {
552
+ return _ctx->setCertificate (client_ca);
553
+ }
554
+ void setPrivateKey (const char *private_key) {
555
+ return _ctx->setPrivateKey (private_key);
556
+ }
557
+ bool loadCACert (Stream& stream, size_t size) {
558
+ return _ctx->loadCACert (stream, size);
559
+ }
560
+ bool loadCertificate (Stream& stream, size_t size) {
561
+ return _ctx->loadCertificate (stream, size);
562
+ }
563
+ bool loadPrivateKey (Stream& stream, size_t size) {
564
+ return _ctx->loadPrivateKey (stream, size);
565
+ }
566
+
567
+ int connect (IPAddress ip, uint16_t port, int32_t timeout) {
568
+ return _ctx->connect (ip, port, timeout);
569
+ }
570
+ int connect (const char *host, uint16_t port, int32_t timeout) {
571
+ return _ctx->connect (host, port, timeout);
572
+ }
573
+ int connect (IPAddress ip, uint16_t port, const char *rootCABuff, const char *cli_cert, const char *cli_key) {
574
+ return _ctx->connect (ip, port, rootCABuff, cli_cert, cli_key);
575
+ }
576
+ int connect (const char *host, uint16_t port, const char *rootCABuff, const char *cli_cert, const char *cli_key) {
577
+ return _ctx->connect (host, port, rootCABuff, cli_cert, cli_key);
578
+ }
579
+
446
580
private:
447
581
std::shared_ptr<WiFiClientSecureCtx> _ctx;
448
582
0 commit comments