@@ -79,9 +79,8 @@ void WiFiClientSecure::_clear() {
79
79
_recvapp_buf = nullptr ;
80
80
_recvapp_len = 0 ;
81
81
_oom_err = false ;
82
- _deleteChainKeyTA = false ;
83
82
_session = nullptr ;
84
- _cipher_list = NULL ;
83
+ _cipher_list = nullptr ;
85
84
_cipher_cnt = 0 ;
86
85
}
87
86
@@ -92,6 +91,9 @@ void WiFiClientSecure::_clearAuthenticationSettings() {
92
91
_knownkey = nullptr ;
93
92
_sk = nullptr ;
94
93
_ta = nullptr ;
94
+ _axtls_ta = nullptr ;
95
+ _axtls_chain = nullptr ;
96
+ _axtls_sk = nullptr ;
95
97
}
96
98
97
99
@@ -102,20 +104,23 @@ WiFiClientSecure::WiFiClientSecure() : WiFiClient() {
102
104
stack_thunk_add_ref ();
103
105
}
104
106
107
+ WiFiClientSecure::WiFiClientSecure (const WiFiClientSecure &rhs) : WiFiClient(rhs) {
108
+ *this = rhs;
109
+ stack_thunk_add_ref ();
110
+ }
111
+
105
112
WiFiClientSecure::~WiFiClientSecure () {
106
113
if (_client) {
107
114
_client->unref ();
108
115
_client = nullptr ;
109
116
}
110
- free ( _cipher_list);
117
+ _cipher_list = nullptr ; // std::shared will free if last reference
111
118
_freeSSL ();
112
- // Serial.printf("Max stack usage: %d bytes\n", br_thunk_get_max_usage());
113
119
stack_thunk_del_ref ();
114
- if (_deleteChainKeyTA) {
115
- delete _ta;
116
- delete _chain;
117
- delete _sk;
118
- }
120
+ // Clean up any dangling axtls compat structures, if needed
121
+ _axtls_ta = nullptr ;
122
+ _axtls_chain = nullptr ;
123
+ _axtls_sk = nullptr ;
119
124
}
120
125
121
126
WiFiClientSecure::WiFiClientSecure (ClientContext* client,
@@ -808,12 +813,12 @@ extern "C" {
808
813
809
814
// Set custom list of ciphers
810
815
bool WiFiClientSecure::setCiphers (const uint16_t *cipherAry, int cipherCount) {
811
- free ( _cipher_list) ;
812
- _cipher_list = ( uint16_t *) malloc ( cipherCount * sizeof ( uint16_t ));
813
- if (!_cipher_list) {
816
+ _cipher_list = nullptr ;
817
+ _cipher_list = std::shared_ptr< uint16_t >( new uint16_t [ cipherCount], std::default_delete< uint16_t []>( ));
818
+ if (!_cipher_list. get () ) {
814
819
return false ;
815
820
}
816
- memcpy_P (_cipher_list, cipherAry, cipherCount * sizeof (uint16_t ));
821
+ memcpy_P (_cipher_list. get () , cipherAry, cipherCount * sizeof (uint16_t ));
817
822
_cipher_cnt = cipherCount;
818
823
return true ;
819
824
}
@@ -895,10 +900,10 @@ bool WiFiClientSecure::_connectSSL(const char* hostName) {
895
900
}
896
901
897
902
// If no cipher list yet set, use defaults
898
- if (_cipher_list == NULL ) {
903
+ if (_cipher_list. get () == nullptr ) {
899
904
br_ssl_client_base_init (_sc.get (), suites_P, sizeof (suites_P) / sizeof (suites_P[0 ]));
900
905
} else {
901
- br_ssl_client_base_init (_sc.get (), _cipher_list, _cipher_cnt);
906
+ br_ssl_client_base_init (_sc.get (), _cipher_list. get () , _cipher_cnt);
902
907
}
903
908
// Only failure possible in the installation is OOM
904
909
if (!_installClientX509Validator ()) {
@@ -1300,32 +1305,23 @@ bool WiFiClientSecure::probeMaxFragmentLength(IPAddress ip, uint16_t port, uint1
1300
1305
1301
1306
// AXTLS compatibility interfaces
1302
1307
bool WiFiClientSecure::setCACert (const uint8_t * pk, size_t size) {
1303
- if (_ta && _deleteChainKeyTA) {
1304
- delete _ta;
1305
- _ta = nullptr ;
1306
- }
1307
- _ta = new X509List (pk, size);
1308
- _deleteChainKeyTA = true ;
1308
+ _axtls_ta = nullptr ;
1309
+ _axtls_ta = std::shared_ptr<X509List>(new X509List (pk, size));
1310
+ _ta = _axtls_ta.get ();
1309
1311
return _ta ? true : false ;
1310
1312
}
1311
1313
1312
1314
bool WiFiClientSecure::setCertificate (const uint8_t * pk, size_t size) {
1313
- if (_chain && _deleteChainKeyTA) {
1314
- delete _chain;
1315
- _chain = nullptr ;
1316
- }
1317
- _chain = new X509List (pk, size);
1318
- _deleteChainKeyTA = true ;
1315
+ _axtls_chain = nullptr ;
1316
+ _axtls_chain = std::shared_ptr<X509List>(new X509List (pk, size));
1317
+ _chain = _axtls_chain.get ();
1319
1318
return _chain ? true : false ;
1320
1319
}
1321
1320
1322
1321
bool WiFiClientSecure::setPrivateKey (const uint8_t * pk, size_t size) {
1323
- if (_sk && _deleteChainKeyTA) {
1324
- delete _sk;
1325
- _sk = nullptr ;
1326
- }
1327
- _sk = new PrivateKey (pk, size);
1328
- _deleteChainKeyTA = true ;
1322
+ _axtls_sk = nullptr ;
1323
+ _axtls_sk = std::shared_ptr<PrivateKey>(new PrivateKey (pk, size));
1324
+ _sk = _axtls_sk.get ();
1329
1325
return _sk ? true : false ;
1330
1326
1331
1327
}
0 commit comments