@@ -58,6 +58,12 @@ extern "C" {
58
58
59
59
#endif
60
60
61
+ #ifdef DEBUG_ESP_SSL
62
+ #define DEBUG_BSSL (fmt, ...) DEBUG_ESP_PORT.printf_P((PGM_P)PSTR( " BSSL:" fmt), ## __VA_ARGS__)
63
+ #else
64
+ #define DEBUG_BSSL (...)
65
+ #endif
66
+
61
67
namespace BearSSL {
62
68
63
69
void WiFiClientSecure::_clear () {
@@ -214,9 +220,11 @@ int WiFiClientSecure::connect(CONST IPAddress& ip, uint16_t port) {
214
220
int WiFiClientSecure::connect (const char * name, uint16_t port) {
215
221
IPAddress remote_addr;
216
222
if (!WiFi.hostByName (name, remote_addr)) {
223
+ DEBUG_BSSL (" connect: Name loopup failure\n " );
217
224
return 0 ;
218
225
}
219
226
if (!WiFiClient::connect (remote_addr, port)) {
227
+ DEBUG_BSSL (" connect: Unable to connect TCP socket\n " );
220
228
return 0 ;
221
229
}
222
230
return _connectSSL (name);
@@ -309,6 +317,7 @@ size_t WiFiClientSecure::write(Stream& stream) {
309
317
size_t countSent;
310
318
311
319
if (!connected () || !_handshake_done) {
320
+ DEBUG_BSSL (" write: Connect/handshake not completed yet\n " );
312
321
return 0 ;
313
322
}
314
323
@@ -336,6 +345,7 @@ int WiFiClientSecure::read(uint8_t *buf, size_t size) {
336
345
return 0 ; // We're still connected, but nothing to read
337
346
}
338
347
if (!avail && !conn) {
348
+ DEBUG_BSSL (" read: Not connected, none left available\n " );
339
349
return -1 ;
340
350
}
341
351
@@ -349,14 +359,19 @@ int WiFiClientSecure::read(uint8_t *buf, size_t size) {
349
359
return to_copy;
350
360
}
351
361
352
- return conn ? 0 : -1 ; // If we're connected, no error but no read. OTW error
362
+ if (!conn) {
363
+ DEBUG_BSSL (" read: Not connected\n " );
364
+ return -1 ;
365
+ }
366
+ return 0 ; // If we're connected, no error but no read.
353
367
}
354
368
355
369
int WiFiClientSecure::read () {
356
370
uint8_t c;
357
371
if (1 == read (&c, 1 )) {
358
372
return c;
359
373
}
374
+ DEBUG_BSSL (" read: failed\n " );
360
375
return -1 ;
361
376
}
362
377
@@ -383,17 +398,20 @@ int WiFiClientSecure::available() {
383
398
384
399
int WiFiClientSecure::peek () {
385
400
if (!ctx_present () || !available ()) {
401
+ DEBUG_BSSL (" peek: Not connected, none left available\n " );
386
402
return -1 ;
387
403
}
388
404
if (_recvapp_buf && _recvapp_len) {
389
405
return _recvapp_buf[0 ];
390
406
}
407
+ DEBUG_BSSL (" peek: No data left\n " );
391
408
return -1 ;
392
409
}
393
410
394
411
size_t WiFiClientSecure::peekBytes (uint8_t *buffer, size_t length) {
395
412
size_t to_copy = 0 ;
396
413
if (!ctx_present ()) {
414
+ DEBUG_BSSL (" peekBytes: Not connected\n " );
397
415
return 0 ;
398
416
}
399
417
@@ -415,6 +433,7 @@ size_t WiFiClientSecure::peekBytes(uint8_t *buffer, size_t length) {
415
433
*/
416
434
int WiFiClientSecure::_run_until (unsigned target, bool blocking) {
417
435
if (!ctx_present ()) {
436
+ DEBUG_BSSL (" _run_until: Not connected\n " );
418
437
return -1 ;
419
438
}
420
439
for (int no_work = 0 ; blocking || no_work < 2 ;) {
@@ -477,6 +496,7 @@ int WiFiClientSecure::_run_until(unsigned target, bool blocking) {
477
496
This is unrecoverable here, so we report an error.
478
497
*/
479
498
if (state & BR_SSL_RECVAPP) {
499
+ DEBUG_BSSL (" _run_until: Fatal protocol state\n " );
480
500
return -1 ;
481
501
}
482
502
@@ -524,6 +544,7 @@ bool WiFiClientSecure::_wait_for_handshake() {
524
544
while (!_handshake_done && _clientConnected ()) {
525
545
int ret = _run_until (BR_SSL_SENDAPP);
526
546
if (ret < 0 ) {
547
+ DEBUG_BSSL (" _wait_for_handshake: failed\n " );
527
548
break ;
528
549
}
529
550
if (br_ssl_engine_current_state (_eng) & BR_SSL_SENDAPP) {
@@ -552,10 +573,14 @@ bool WiFiClientSecure::setFingerprint(const char *fpStr) {
552
573
c = pgm_read_byte (fpStr++);
553
574
if (!c) break ; // String ended, done processing
554
575
d = pgm_read_byte (fpStr++);
555
- if (!d) return false ; // Only half of the last hex digit, error
576
+ if (!d) {
577
+ DEBUG_BSSL (" setFingerprint: FP too short\n " );
578
+ return false ; // Only half of the last hex digit, error
579
+ }
556
580
c = htoi (c);
557
581
d = htoi (d);
558
582
if ((c>15 ) || (d>15 )) {
583
+ DEBUG_BSSL (" setFingerprint: Invalid char\n " );
559
584
return false ; // Error in one of the hex characters
560
585
}
561
586
fp[idx++] = (c<<4 )|d;
@@ -566,6 +591,7 @@ bool WiFiClientSecure::setFingerprint(const char *fpStr) {
566
591
}
567
592
}
568
593
if ((idx != 20 ) || pgm_read_byte (fpStr)) {
594
+ DEBUG_BSSL (" setFingerprint: Garbage at end of fp\n " );
569
595
return false ; // Garbage at EOL or we didn't have enough hex digits
570
596
}
571
597
return setFingerprint (fp);
@@ -641,13 +667,15 @@ extern "C" {
641
667
static unsigned insecure_end_chain (const br_x509_class **ctx) {
642
668
const br_x509_insecure_context *xc = (const br_x509_insecure_context *)ctx;
643
669
if (!xc->done_cert ) {
670
+ DEBUG_BSSL (" insecure_end_chain: No cert seen\n " );
644
671
return 1 ; // error
645
672
}
646
673
647
674
// Handle SHA1 fingerprint matching
648
675
char res[20 ];
649
676
br_sha1_out (&xc->sha1_cert , res);
650
677
if (xc->match_fingerprint && memcmp (res, xc->match_fingerprint , sizeof (res))) {
678
+ DEBUG_BSSL (" insecure_end_chain: Received cert FP doesn't match\n " );
651
679
return BR_ERR_X509_NOT_TRUSTED;
652
680
}
653
681
@@ -657,6 +685,7 @@ extern "C" {
657
685
br_sha256_out (&xc->sha256_issuer , res_issuer);
658
686
br_sha256_out (&xc->sha256_subject , res_subject);
659
687
if (xc->allow_self_signed && memcmp (res_subject, res_issuer, sizeof (res_issuer))) {
688
+ DEBUG_BSSL (" insecure_end_chain: Didn't get self-signed cert\n " );
660
689
return BR_ERR_X509_NOT_TRUSTED;
661
690
}
662
691
@@ -816,6 +845,7 @@ bool WiFiClientSecure::setCiphers(const uint16_t *cipherAry, int cipherCount) {
816
845
_cipher_list = nullptr ;
817
846
_cipher_list = std::shared_ptr<uint16_t >(new uint16_t [cipherCount], std::default_delete<uint16_t []>());
818
847
if (!_cipher_list.get ()) {
848
+ DEBUG_BSSL (" setCiphers: list empty\n " );
819
849
return false ;
820
850
}
821
851
memcpy_P (_cipher_list.get (), cipherAry, cipherCount * sizeof (uint16_t ));
@@ -837,6 +867,7 @@ bool WiFiClientSecure::_installClientX509Validator() {
837
867
// Use common insecure x509 authenticator
838
868
_x509_insecure = std::make_shared<struct br_x509_insecure_context >();
839
869
if (!_x509_insecure) {
870
+ DEBUG_BSSL (" _installClientX509Validator: OOM for _x509_insecure\n " );
840
871
return false ;
841
872
}
842
873
br_x509_insecure_init (_x509_insecure.get (), _use_fingerprint, _fingerprint, _use_self_signed);
@@ -845,6 +876,7 @@ bool WiFiClientSecure::_installClientX509Validator() {
845
876
// Simple, pre-known public key authenticator, ignores cert completely.
846
877
_x509_knownkey = std::make_shared<br_x509_knownkey_context>();
847
878
if (!_x509_knownkey) {
879
+ DEBUG_BSSL (" _installClientX509Validator: OOM for _x509_knownkey\n " );
848
880
return false ;
849
881
}
850
882
if (_knownkey->isRSA ()) {
@@ -857,6 +889,7 @@ bool WiFiClientSecure::_installClientX509Validator() {
857
889
// X509 minimal validator. Checks dates, cert chain for trusted CA, etc.
858
890
_x509_minimal = std::make_shared<br_x509_minimal_context>();
859
891
if (!_x509_minimal) {
892
+ DEBUG_BSSL (" _installClientX509Validator: OOM for _x509_minimal\n " );
860
893
return false ;
861
894
}
862
895
br_x509_minimal_init (_x509_minimal.get (), &br_sha256_vtable, _ta ? _ta->getTrustAnchors () : nullptr , _ta ? _ta->getCount () : 0 );
@@ -878,13 +911,14 @@ bool WiFiClientSecure::_installClientX509Validator() {
878
911
// Called by connect() to do the actual SSL setup and handshake.
879
912
// Returns if the SSL handshake succeeded.
880
913
bool WiFiClientSecure::_connectSSL (const char * hostName) {
914
+ DEBUG_BSSL (" _connectSSL: start connection\n " );
881
915
_freeSSL ();
882
916
_oom_err = false ;
883
917
884
918
#ifdef DEBUG_ESP_SSL
885
919
// BearSSL will reject all connections unless an authentication option is set, warn in DEBUG builds
886
920
if (!_use_insecure && !_use_fingerprint && !_use_self_signed && !_knownkey && !_certStore && !_ta) {
887
- DEBUGV ( " BSSL: Connection *will* fail, no authentication method is setup" );
921
+ DEBUG_BSSL ( " Connection *will* fail, no authentication method is setup\n " );
888
922
}
889
923
#endif
890
924
@@ -896,6 +930,7 @@ bool WiFiClientSecure::_connectSSL(const char* hostName) {
896
930
if (!_sc || !_iobuf_in || !_iobuf_out) {
897
931
_freeSSL (); // Frees _sc, _iobuf*
898
932
_oom_err = true ;
933
+ DEBUG_BSSL (" _connectSSL: OOM error\n " );
899
934
return false ;
900
935
}
901
936
@@ -909,6 +944,7 @@ bool WiFiClientSecure::_connectSSL(const char* hostName) {
909
944
if (!_installClientX509Validator ()) {
910
945
_freeSSL ();
911
946
_oom_err = true ;
947
+ DEBUG_BSSL (" _connectSSL: Can't install x509 validator\n " );
912
948
return false ;
913
949
}
914
950
br_ssl_engine_set_buffers_bidi (_eng, _iobuf_in.get (), _iobuf_in_size, _iobuf_out.get (), _iobuf_out_size);
@@ -929,10 +965,21 @@ bool WiFiClientSecure::_connectSSL(const char* hostName) {
929
965
930
966
if (!br_ssl_client_reset (_sc.get (), hostName, _session?1 :0 )) {
931
967
_freeSSL ();
968
+ DEBUG_BSSL (" _connectSSL: Can't reset client\n " );
932
969
return false ;
933
970
}
934
971
935
- return _wait_for_handshake ();
972
+ auto ret = _wait_for_handshake ();
973
+ #ifdef DEBUG_ESP_SSL
974
+ if (!ret) {
975
+ char err[256 ];
976
+ getLastSSLError (err, sizeof (err));
977
+ DEBUG_BSSL (" Couldn't connect. Error = '%s'\n " , err);
978
+ } else {
979
+ DEBUG_BSSL (" Connected!\n " );
980
+ }
981
+ #endif
982
+ return ret;
936
983
}
937
984
938
985
// Slightly different X509 setup for servers who want to validate client
@@ -945,6 +992,7 @@ bool WiFiClientSecure::_installServerX509Validator(const X509List *client_CA_ta)
945
992
if (!_x509_minimal) {
946
993
_freeSSL ();
947
994
_oom_err = true ;
995
+ DEBUG_BSSL (" _installServerX509Validator: OOM for _x509_minimal\n " );
948
996
return false ;
949
997
}
950
998
br_x509_minimal_init (_x509_minimal.get (), &br_sha256_vtable, _ta->getTrustAnchors (), _ta->getCount ());
@@ -977,16 +1025,19 @@ bool WiFiClientSecure::_connectSSLServerRSA(const X509List *chain,
977
1025
if (!_sc_svr || !_iobuf_in || !_iobuf_out) {
978
1026
_freeSSL ();
979
1027
_oom_err = true ;
1028
+ DEBUG_BSSL (" _connectSSLServerRSA: OOM error\n " );
980
1029
return false ;
981
1030
}
982
1031
983
1032
br_ssl_server_init_full_rsa (_sc_svr.get (), chain ? chain->getX509Certs () : nullptr , chain ? chain->getCount () : 0 , sk ? sk->getRSA () : nullptr );
984
1033
br_ssl_engine_set_buffers_bidi (_eng, _iobuf_in.get (), _iobuf_in_size, _iobuf_out.get (), _iobuf_out_size);
985
1034
if (client_CA_ta && !_installServerX509Validator (client_CA_ta)) {
1035
+ DEBUG_BSSL (" _connectSSLServerRSA: Can't install serverX509check\n " );
986
1036
return false ;
987
1037
}
988
1038
if (!br_ssl_server_reset (_sc_svr.get ())) {
989
1039
_freeSSL ();
1040
+ DEBUG_BSSL (" _connectSSLServerRSA: Can't reset server ctx\n " );
990
1041
return false ;
991
1042
}
992
1043
@@ -1007,17 +1058,20 @@ bool WiFiClientSecure::_connectSSLServerEC(const X509List *chain,
1007
1058
if (!_sc_svr || !_iobuf_in || !_iobuf_out) {
1008
1059
_freeSSL ();
1009
1060
_oom_err = true ;
1061
+ DEBUG_BSSL (" _connectSSLServerEC: OOM error\n " );
1010
1062
return false ;
1011
1063
}
1012
1064
1013
1065
br_ssl_server_init_full_ec (_sc_svr.get (), chain ? chain->getX509Certs () : nullptr , chain ? chain->getCount () : 0 ,
1014
1066
cert_issuer_key_type, sk ? sk->getEC () : nullptr );
1015
1067
br_ssl_engine_set_buffers_bidi (_eng, _iobuf_in.get (), _iobuf_in_size, _iobuf_out.get (), _iobuf_out_size);
1016
1068
if (client_CA_ta && !_installServerX509Validator (client_CA_ta)) {
1069
+ DEBUG_BSSL (" _connectSSLServerEC: Can't install serverX509check\n " );
1017
1070
return false ;
1018
1071
}
1019
1072
if (!br_ssl_server_reset (_sc_svr.get ())) {
1020
1073
_freeSSL ();
1074
+ DEBUG_BSSL (" _connectSSLServerEC: Can't reset server ctx\n " );
1021
1075
return false ;
1022
1076
}
1023
1077
@@ -1107,6 +1161,7 @@ int WiFiClientSecure::getLastSSLError(char *dest, size_t len) {
1107
1161
bool WiFiClientSecure::probeMaxFragmentLength (const char * name, uint16_t port, uint16_t len) {
1108
1162
IPAddress remote_addr;
1109
1163
if (!WiFi.hostByName (name, remote_addr)) {
1164
+ DEBUG_BSSL (" probeMaxFragmentLength: Can't resolve host\n " );
1110
1165
return false ;
1111
1166
}
1112
1167
return WiFiClientSecure::probeMaxFragmentLength (remote_addr, port, len);
@@ -1184,6 +1239,7 @@ bool WiFiClientSecure::probeMaxFragmentLength(IPAddress ip, uint16_t port, uint1
1184
1239
int ttlLen = sizeof (clientHelloHead_P) + (2 + sizeof (suites_P)) + (sizeof (clientHelloTail_P) + 1 );
1185
1240
uint8_t *clientHello = new uint8_t [ttlLen];
1186
1241
if (!clientHello) {
1242
+ DEBUG_BSSL (" probeMaxFragmentLength: OOM\n " );
1187
1243
return false ;
1188
1244
}
1189
1245
memcpy_P (clientHello, clientHelloHead_P, sizeof (clientHelloHead_P));
@@ -1209,12 +1265,14 @@ bool WiFiClientSecure::probeMaxFragmentLength(IPAddress ip, uint16_t port, uint1
1209
1265
probe.connect (ip, port);
1210
1266
if (!probe.connected ()) {
1211
1267
delete[] clientHello;
1268
+ DEBUG_BSSL (" probeMaxFragmentLength: Can't connect\n " );
1212
1269
return false ;
1213
1270
}
1214
1271
1215
1272
int ret = probe.write (clientHello, ttlLen);
1216
1273
delete[] clientHello; // We're done w/the hello message
1217
1274
if (!probe.connected () || (ret != ttlLen)) {
1275
+ DEBUG_BSSL (" probeMaxFragmentLength: Protocol error\n " );
1218
1276
return false ;
1219
1277
}
1220
1278
0 commit comments