@@ -50,6 +50,17 @@ extern "C"
50
50
#define SSL_DEBUG_OPTS 0
51
51
#endif
52
52
53
+ uint8_t * default_private_key = 0 ;
54
+ uint32_t default_private_key_len = 0 ;
55
+ static bool default_private_key_dynamic = false ;
56
+ //
57
+ uint8_t * default_certificate = 0 ;
58
+ uint32_t default_certificate_len = 0 ;
59
+ static bool default_certificate_dynamic = false ;
60
+
61
+ static void clear_private_key ();
62
+ static void clear_certificate ();
63
+
53
64
54
65
class SSLContext {
55
66
public:
@@ -70,6 +81,9 @@ class SSLContext {
70
81
if (_ssl_ctx_refcnt == 0 ) {
71
82
ssl_ctx_free (_ssl_ctx);
72
83
}
84
+
85
+ clear_private_key ();
86
+ clear_certificate ();
73
87
}
74
88
75
89
void ref () {
@@ -337,6 +351,66 @@ bool WiFiClientSecure::verify(const char* fp, const char* url) {
337
351
return true ;
338
352
}
339
353
354
+ void WiFiClientSecure::setCertificate (const uint8_t * cert_data, size_t size) {
355
+ clear_certificate ();
356
+ default_certificate = (uint8_t *) cert_data;
357
+ default_certificate_len = size;
358
+ }
359
+
360
+ void WiFiClientSecure::setPrivateKey (const uint8_t * pk, size_t size) {
361
+ clear_private_key ();
362
+ default_private_key = (uint8_t *) pk;
363
+ default_private_key_len = size;
364
+ }
365
+
366
+ bool WiFiClientSecure::loadCertificate (Stream& stream, size_t size) {
367
+ clear_certificate ();
368
+ default_certificate = new uint8_t [size];
369
+ if (!default_certificate) {
370
+ return false ;
371
+ }
372
+ if (stream.readBytes (default_certificate, size) != size) {
373
+ delete[] default_certificate;
374
+ return false ;
375
+ }
376
+ default_certificate_dynamic = true ;
377
+ default_certificate_len = size;
378
+ return true ;
379
+ }
380
+
381
+ bool WiFiClientSecure::loadPrivateKey (Stream& stream, size_t size) {
382
+ clear_private_key ();
383
+ default_private_key = new uint8_t [size];
384
+ if (!default_private_key) {
385
+ return false ;
386
+ }
387
+ if (stream.readBytes (default_private_key, size) != size) {
388
+ delete[] default_private_key;
389
+ return false ;
390
+ }
391
+ default_private_key_dynamic = true ;
392
+ default_private_key_len = size;
393
+ return true ;
394
+ }
395
+
396
+ static void clear_private_key () {
397
+ if (default_private_key && default_private_key_dynamic) {
398
+ delete[] default_private_key;
399
+ default_private_key_dynamic = false ;
400
+ }
401
+ default_private_key = 0 ;
402
+ default_private_key_len = 0 ;
403
+ }
404
+
405
+ static void clear_certificate () {
406
+ if (default_certificate && default_certificate_dynamic) {
407
+ delete[] default_certificate;
408
+ default_certificate_dynamic = false ;
409
+ }
410
+ default_certificate = 0 ;
411
+ default_certificate_len = 0 ;
412
+ }
413
+
340
414
extern " C" int ax_port_read (int fd, uint8_t * buffer, size_t count) {
341
415
ClientContext* _client = reinterpret_cast <ClientContext*>(fd);
342
416
if (_client->state () != ESTABLISHED && !_client->getSize ()) {
0 commit comments