@@ -68,6 +68,11 @@ 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
+
71
76
bool ca_root_custom = false ;
72
77
int ca_root_size = 0 ;
73
78
if (parser.args .size () >= 2 ){
@@ -80,19 +85,17 @@ void CAtHandler::add_cmds_wifi_SSL() {
80
85
}
81
86
82
87
if (ca_root_custom) {
83
-
84
-
85
- cert_buf = srv.inhibit_read (ca_root_size);
86
- 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 ();
87
90
88
91
if (offset < ca_root_size) {
89
92
90
- cert_buf .resize (ca_root_size);
93
+ clients_ca[internal_sock] .resize (ca_root_size);
91
94
do {
92
- 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);
93
96
} while (offset < ca_root_size);
94
97
}
95
- the_client.sslclient ->setCACert ((const char *)cert_buf .data ());
98
+ the_client.sslclient ->setCACert ((const char *)clients_ca[internal_sock] .data ());
96
99
srv.continue_read ();
97
100
} else {
98
101
#ifdef BUNDLED_CA_ROOT_CRT
@@ -137,6 +140,11 @@ void CAtHandler::add_cmds_wifi_SSL() {
137
140
return chAT::CommandStatus::ERROR;
138
141
}
139
142
143
+ const int internal_sock = the_client.can_delete ;
144
+ if (internal_sock == -1 ) {
145
+ return chAT::CommandStatus::ERROR;
146
+ }
147
+
140
148
std::vector<unsigned char > client_cert_der;
141
149
client_cert_der = srv.inhibit_read (size);
142
150
size_t offset = client_cert_der.size ();
@@ -155,22 +163,22 @@ void CAtHandler::add_cmds_wifi_SSL() {
155
163
#endif
156
164
157
165
/* Convert client certificate DER buffer into PEM */
158
- client_cert_pem .resize (1024 );
166
+ clients_cert_pem[internal_sock] .resize (1024 );
159
167
size_t olen;
160
168
mbedtls_pem_write_buffer (" -----BEGIN CERTIFICATE-----\n " ,
161
169
" -----END CERTIFICATE-----\n " ,
162
170
client_cert_der.data (), size,
163
- client_cert_pem .data (), 1024 ,
171
+ clients_cert_pem[internal_sock] .data (), 1024 ,
164
172
&olen);
165
- client_cert_pem .resize (olen);
173
+ clients_cert_pem[internal_sock] .resize (olen);
166
174
167
175
#if ECC_DEBUG_ENABLED
168
176
log_v (" _SETECCSLOT: output cert" );
169
- log_v (" \n %s" , client_cert_pem .data ());
177
+ log_v (" \n %s" , clients_cert_pem[internal_sock] .data ());
170
178
#endif
171
179
172
180
/* Set client certificate */
173
- the_client.sslclient ->setCertificate ((const char *)client_cert_pem .data ());
181
+ the_client.sslclient ->setCertificate ((const char *)clients_cert_pem[internal_sock] .data ());
174
182
175
183
/* Read private key from non volatile storage in DER format */
176
184
std::vector<unsigned char > client_key_der;
@@ -188,21 +196,21 @@ void CAtHandler::add_cmds_wifi_SSL() {
188
196
#endif
189
197
190
198
/* Convert private key in PEM format */
191
- client_key_pem .resize (1024 );
199
+ clients_key_pem[internal_sock] .resize (1024 );
192
200
mbedtls_pem_write_buffer (" -----BEGIN EC PRIVATE KEY-----\n " ,
193
201
" -----END EC PRIVATE KEY-----\n " ,
194
202
client_key_der.data (), len,
195
- client_key_pem .data (), 1024 ,
203
+ clients_key_pem[internal_sock] .data (), 1024 ,
196
204
&olen);
197
- client_key_pem .resize (olen);
205
+ clients_key_pem[internal_sock] .resize (olen);
198
206
199
207
#if ECC_DEBUG_ENABLED
200
208
log_v (" _SETECCSLOT: output key" );
201
- log_v (" \n %s" , client_key_pem .data ());
209
+ log_v (" \n %s" , clients_key_pem[internal_sock] .data ());
202
210
#endif
203
211
204
212
/* Set client key */
205
- the_client.sslclient ->setPrivateKey ((const char *)client_key_pem .data ());
213
+ the_client.sslclient ->setPrivateKey ((const char *)clients_key_pem[internal_sock] .data ());
206
214
207
215
return chAT::CommandStatus::OK;
208
216
}
@@ -267,6 +275,11 @@ void CAtHandler::add_cmds_wifi_SSL() {
267
275
return chAT::CommandStatus::ERROR;
268
276
}
269
277
278
+ const int internal_sock = the_client.can_delete ;
279
+ if (internal_sock == -1 ) {
280
+ return chAT::CommandStatus::ERROR;
281
+ }
282
+
270
283
auto &host = parser.args [1 ];
271
284
if (host.empty ()) {
272
285
return chAT::CommandStatus::ERROR;
@@ -277,6 +290,21 @@ void CAtHandler::add_cmds_wifi_SSL() {
277
290
return chAT::CommandStatus::ERROR;
278
291
}
279
292
293
+ /* Set custom root ca */
294
+ if (clients_ca[internal_sock].size ()) {
295
+ the_client.sslclient ->setCACert ((const char *)clients_ca[internal_sock].data ());
296
+ }
297
+ /* Default ca bundle is configured automatically on connect by the WiFiSSLClient */
298
+
299
+ if (clients_cert_pem[internal_sock].size ()) {
300
+ /* Set client certificate */
301
+ the_client.sslclient ->setCertificate ((const char *)clients_cert_pem[internal_sock].data ());
302
+ }
303
+ if (clients_key_pem[internal_sock].size ()) {
304
+ /* Set client key */
305
+ the_client.sslclient ->setPrivateKey ((const char *)clients_key_pem[internal_sock].data ());
306
+ }
307
+
280
308
if (!the_client.sslclient ->connect (host.c_str (), atoi (port.c_str ()))) {
281
309
return chAT::CommandStatus::ERROR;
282
310
}
@@ -311,6 +339,11 @@ void CAtHandler::add_cmds_wifi_SSL() {
311
339
return chAT::CommandStatus::ERROR;
312
340
}
313
341
342
+ const int internal_sock = the_client.can_delete ;
343
+ if (internal_sock == -1 ) {
344
+ return chAT::CommandStatus::ERROR;
345
+ }
346
+
314
347
auto &hostip = parser.args [1 ];
315
348
if (hostip.empty ()) {
316
349
return chAT::CommandStatus::ERROR;
@@ -326,6 +359,21 @@ void CAtHandler::add_cmds_wifi_SSL() {
326
359
return chAT::CommandStatus::ERROR;
327
360
}
328
361
362
+ /* Set custom root ca */
363
+ if (clients_ca[internal_sock].size ()) {
364
+ the_client.sslclient ->setCACert ((const char *)clients_ca[internal_sock].data ());
365
+ }
366
+ /* Default ca bundle is configured automatically on connect by the WiFiSSLClient */
367
+
368
+ if (clients_cert_pem[internal_sock].size ()) {
369
+ /* Set client certificate */
370
+ the_client.sslclient ->setCertificate ((const char *)clients_cert_pem[internal_sock].data ());
371
+ }
372
+ if (clients_key_pem[internal_sock].size ()) {
373
+ /* Set client key */
374
+ the_client.sslclient ->setPrivateKey ((const char *)clients_key_pem[internal_sock].data ());
375
+ }
376
+
329
377
if (!the_client.sslclient ->connect (address, atoi (hostport.c_str ()))) {
330
378
return chAT::CommandStatus::ERROR;
331
379
}
@@ -359,6 +407,11 @@ void CAtHandler::add_cmds_wifi_SSL() {
359
407
return chAT::CommandStatus::ERROR;
360
408
}
361
409
410
+ const int internal_sock = the_client.can_delete ;
411
+ if (internal_sock == -1 ) {
412
+ return chAT::CommandStatus::ERROR;
413
+ }
414
+
362
415
auto &host = parser.args [1 ];
363
416
if (host.empty ()) {
364
417
return chAT::CommandStatus::ERROR;
@@ -381,6 +434,21 @@ void CAtHandler::add_cmds_wifi_SSL() {
381
434
}
382
435
}
383
436
437
+ /* Set custom root ca */
438
+ if (clients_ca[internal_sock].size ()) {
439
+ the_client.sslclient ->setCACert ((const char *)clients_ca[internal_sock].data ());
440
+ }
441
+ /* Default ca bundle is configured automatically on connect by the WiFiSSLClient */
442
+
443
+ if (clients_cert_pem[internal_sock].size ()) {
444
+ /* Set client certificate */
445
+ the_client.sslclient ->setCertificate ((const char *)clients_cert_pem[internal_sock].data ());
446
+ }
447
+ if (clients_key_pem[internal_sock].size ()) {
448
+ /* Set client key */
449
+ the_client.sslclient ->setPrivateKey ((const char *)clients_key_pem[internal_sock].data ());
450
+ }
451
+
384
452
if (!the_client.sslclient ->connect (host.c_str (), atoi (port.c_str ()), timeout)) {
385
453
return chAT::CommandStatus::ERROR;
386
454
}
@@ -498,6 +566,9 @@ void CAtHandler::add_cmds_wifi_SSL() {
498
566
if (the_client.can_delete >= 0 ) {
499
567
delete sslclients[the_client.can_delete ];
500
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 ();
501
572
sslclients_num--;
502
573
}
503
574
}
0 commit comments