@@ -68,6 +68,12 @@ void CAtHandler::add_cmds_wifi_SSL() {
68
68
return chAT::CommandStatus::ERROR;
69
69
}
70
70
71
+ const int internal_sock = the_client.can_delete ;
72
+ if (internal_sock == -1 ) {
73
+ return chAT::CommandStatus::ERROR;
74
+ }
75
+
76
+ bool ca_root_custom = false ;
71
77
int ca_root_size = 0 ;
72
78
if (parser.args .size () >= 2 ){
73
79
auto &ca_root_size_str = parser.args [1 ];
@@ -79,17 +85,17 @@ void CAtHandler::add_cmds_wifi_SSL() {
79
85
}
80
86
81
87
if (ca_root_custom) {
82
- cert_buf = srv.inhibit_read (ca_root_size);
83
- size_t offset = cert_buf .size ();
88
+ clients_ca[internal_sock] = srv.inhibit_read (ca_root_size);
89
+ size_t offset = clients_ca[internal_sock] .size ();
84
90
85
91
if (offset < ca_root_size) {
86
92
87
- cert_buf .resize (ca_root_size);
93
+ clients_ca[internal_sock] .resize (ca_root_size);
88
94
do {
89
- offset += serial->read (cert_buf .data () + offset, ca_root_size - offset);
95
+ offset += serial->read (clients_ca[internal_sock] .data () + offset, ca_root_size - offset);
90
96
} while (offset < ca_root_size);
91
97
}
92
- the_client.sslclient ->setCACert ((const char *)cert_buf .data ());
98
+ the_client.sslclient ->setCACert ((const char *)clients_ca[internal_sock] .data ());
93
99
srv.continue_read ();
94
100
} else {
95
101
#ifdef BUNDLED_CA_ROOT_CRT
@@ -134,6 +140,11 @@ void CAtHandler::add_cmds_wifi_SSL() {
134
140
return chAT::CommandStatus::ERROR;
135
141
}
136
142
143
+ const int internal_sock = the_client.can_delete ;
144
+ if (internal_sock == -1 ) {
145
+ return chAT::CommandStatus::ERROR;
146
+ }
147
+
137
148
std::vector<unsigned char > client_cert_der;
138
149
client_cert_der = srv.inhibit_read (size);
139
150
size_t offset = client_cert_der.size ();
@@ -152,22 +163,22 @@ void CAtHandler::add_cmds_wifi_SSL() {
152
163
#endif
153
164
154
165
/* Convert client certificate DER buffer into PEM */
155
- client_cert_pem .resize (1024 );
166
+ clients_cert_pem[internal_sock] .resize (1024 );
156
167
size_t olen;
157
168
mbedtls_pem_write_buffer (" -----BEGIN CERTIFICATE-----\n " ,
158
169
" -----END CERTIFICATE-----\n " ,
159
170
client_cert_der.data (), size,
160
- client_cert_pem .data (), 1024 ,
171
+ clients_cert_pem[internal_sock] .data (), 1024 ,
161
172
&olen);
162
- client_cert_pem .resize (olen);
173
+ clients_cert_pem[internal_sock] .resize (olen);
163
174
164
175
#if ECC_DEBUG_ENABLED
165
176
log_v (" _SETECCSLOT: output cert" );
166
- log_v (" \n %s" , client_cert_pem .data ());
177
+ log_v (" \n %s" , clients_cert_pem[internal_sock] .data ());
167
178
#endif
168
179
169
180
/* Set client certificate */
170
- the_client.sslclient ->setCertificate ((const char *)client_cert_pem .data ());
181
+ the_client.sslclient ->setCertificate ((const char *)clients_cert_pem[internal_sock] .data ());
171
182
172
183
/* Read private key from non volatile storage in DER format */
173
184
std::vector<unsigned char > client_key_der;
@@ -185,23 +196,21 @@ void CAtHandler::add_cmds_wifi_SSL() {
185
196
#endif
186
197
187
198
/* Convert private key in PEM format */
188
- client_key_pem .resize (1024 );
199
+ clients_key_pem[internal_sock] .resize (1024 );
189
200
mbedtls_pem_write_buffer (" -----BEGIN EC PRIVATE KEY-----\n " ,
190
201
" -----END EC PRIVATE KEY-----\n " ,
191
202
client_key_der.data (), len,
192
- client_key_pem .data (), 1024 ,
203
+ clients_key_pem[internal_sock] .data (), 1024 ,
193
204
&olen);
194
- client_key_pem .resize (olen);
205
+ clients_key_pem[internal_sock] .resize (olen);
195
206
196
207
#if ECC_DEBUG_ENABLED
197
208
log_v (" _SETECCSLOT: output key" );
198
- log_v (" \n %s" , client_key_pem .data ());
209
+ log_v (" \n %s" , clients_key_pem[internal_sock] .data ());
199
210
#endif
200
211
201
212
/* Set client key */
202
- the_client.sslclient ->setPrivateKey ((const char *)client_key_pem.data ());
203
-
204
- client_cert = true ;
213
+ the_client.sslclient ->setPrivateKey ((const char *)clients_key_pem[internal_sock].data ());
205
214
206
215
return chAT::CommandStatus::OK;
207
216
}
@@ -266,6 +275,11 @@ void CAtHandler::add_cmds_wifi_SSL() {
266
275
return chAT::CommandStatus::ERROR;
267
276
}
268
277
278
+ const int internal_sock = the_client.can_delete ;
279
+ if (internal_sock == -1 ) {
280
+ return chAT::CommandStatus::ERROR;
281
+ }
282
+
269
283
auto &host = parser.args [1 ];
270
284
if (host.empty ()) {
271
285
return chAT::CommandStatus::ERROR;
@@ -277,16 +291,18 @@ void CAtHandler::add_cmds_wifi_SSL() {
277
291
}
278
292
279
293
/* Set custom root ca */
280
- if (ca_root_custom ) {
281
- the_client.sslclient ->setCACert ((const char *)cert_buf .data ());
294
+ if (clients_ca[internal_sock]. size () ) {
295
+ the_client.sslclient ->setCACert ((const char *)clients_ca[internal_sock] .data ());
282
296
}
283
297
/* Default ca bundle is configured automatically on connect by the WiFiSSLClient */
284
298
285
- if (client_cert ) {
299
+ if (clients_cert_pem[internal_sock]. size () ) {
286
300
/* Set client certificate */
287
- the_client.sslclient ->setCertificate ((const char *)client_cert_pem.data ());
301
+ the_client.sslclient ->setCertificate ((const char *)clients_cert_pem[internal_sock].data ());
302
+ }
303
+ if (clients_key_pem[internal_sock].size ()) {
288
304
/* Set client key */
289
- the_client.sslclient ->setPrivateKey ((const char *)client_key_pem .data ());
305
+ the_client.sslclient ->setPrivateKey ((const char *)clients_key_pem[internal_sock] .data ());
290
306
}
291
307
292
308
if (!the_client.sslclient ->connect (host.c_str (), atoi (port.c_str ()))) {
@@ -323,6 +339,11 @@ void CAtHandler::add_cmds_wifi_SSL() {
323
339
return chAT::CommandStatus::ERROR;
324
340
}
325
341
342
+ const int internal_sock = the_client.can_delete ;
343
+ if (internal_sock == -1 ) {
344
+ return chAT::CommandStatus::ERROR;
345
+ }
346
+
326
347
auto &hostip = parser.args [1 ];
327
348
if (hostip.empty ()) {
328
349
return chAT::CommandStatus::ERROR;
@@ -339,16 +360,18 @@ void CAtHandler::add_cmds_wifi_SSL() {
339
360
}
340
361
341
362
/* Set custom root ca */
342
- if (ca_root_custom ) {
343
- the_client.sslclient ->setCACert ((const char *)cert_buf .data ());
363
+ if (clients_ca[internal_sock]. size () ) {
364
+ the_client.sslclient ->setCACert ((const char *)clients_ca[internal_sock] .data ());
344
365
}
345
366
/* Default ca bundle is configured automatically on connect by the WiFiSSLClient */
346
367
347
- if (client_cert ) {
368
+ if (clients_cert_pem[internal_sock]. size () ) {
348
369
/* Set client certificate */
349
- the_client.sslclient ->setCertificate ((const char *)client_cert_pem.data ());
370
+ the_client.sslclient ->setCertificate ((const char *)clients_cert_pem[internal_sock].data ());
371
+ }
372
+ if (clients_key_pem[internal_sock].size ()) {
350
373
/* Set client key */
351
- the_client.sslclient ->setPrivateKey ((const char *)client_key_pem .data ());
374
+ the_client.sslclient ->setPrivateKey ((const char *)clients_key_pem[internal_sock] .data ());
352
375
}
353
376
354
377
if (!the_client.sslclient ->connect (address, atoi (hostport.c_str ()))) {
@@ -384,6 +407,11 @@ void CAtHandler::add_cmds_wifi_SSL() {
384
407
return chAT::CommandStatus::ERROR;
385
408
}
386
409
410
+ const int internal_sock = the_client.can_delete ;
411
+ if (internal_sock == -1 ) {
412
+ return chAT::CommandStatus::ERROR;
413
+ }
414
+
387
415
auto &host = parser.args [1 ];
388
416
if (host.empty ()) {
389
417
return chAT::CommandStatus::ERROR;
@@ -407,16 +435,18 @@ void CAtHandler::add_cmds_wifi_SSL() {
407
435
}
408
436
409
437
/* Set custom root ca */
410
- if (ca_root_custom ) {
411
- the_client.sslclient ->setCACert ((const char *)cert_buf .data ());
438
+ if (clients_ca[internal_sock]. size () ) {
439
+ the_client.sslclient ->setCACert ((const char *)clients_ca[internal_sock] .data ());
412
440
}
413
441
/* Default ca bundle is configured automatically on connect by the WiFiSSLClient */
414
442
415
- if (client_cert ) {
443
+ if (clients_cert_pem[internal_sock]. size () ) {
416
444
/* Set client certificate */
417
- the_client.sslclient ->setCertificate ((const char *)client_cert_pem.data ());
445
+ the_client.sslclient ->setCertificate ((const char *)clients_cert_pem[internal_sock].data ());
446
+ }
447
+ if (clients_key_pem[internal_sock].size ()) {
418
448
/* Set client key */
419
- the_client.sslclient ->setPrivateKey ((const char *)client_key_pem .data ());
449
+ the_client.sslclient ->setPrivateKey ((const char *)clients_key_pem[internal_sock] .data ());
420
450
}
421
451
422
452
if (!the_client.sslclient ->connect (host.c_str (), atoi (port.c_str ()), timeout)) {
@@ -536,6 +566,9 @@ void CAtHandler::add_cmds_wifi_SSL() {
536
566
if (the_client.can_delete >= 0 ) {
537
567
delete sslclients[the_client.can_delete ];
538
568
sslclients[the_client.can_delete ] = nullptr ;
569
+ clients_ca[the_client.can_delete ].clear ();
570
+ clients_cert_pem[the_client.can_delete ].clear ();
571
+ clients_key_pem[the_client.can_delete ].clear ();
539
572
sslclients_num--;
540
573
}
541
574
}
0 commit comments