@@ -386,7 +386,8 @@ bool TlsTransport::send(message_ptr message) {
386
386
int (message->size ()));
387
387
} while (ret == MBEDTLS_ERR_SSL_WANT_WRITE);
388
388
389
- mbedtls::check (ret);
389
+ if (!mbedtls::check (ret))
390
+ throw std::runtime_error (" TLS send failed" );
390
391
391
392
return mOutgoingResult ;
392
393
}
@@ -639,10 +640,15 @@ void TlsTransport::start() {
639
640
changeState (State::Connecting);
640
641
641
642
// Initiate the handshake
642
- std::lock_guard lock (mSslMutex );
643
- int ret = SSL_do_handshake (mSsl );
644
- openssl::check (mSsl , ret, " Handshake initiation failed" );
645
- flushOutput ();
643
+ int ret, err;
644
+ {
645
+ std::lock_guard lock (mSslMutex );
646
+ ret = SSL_do_handshake (mSsl );
647
+ err = SSL_get_error (mSsl , ret);
648
+ flushOutput ();
649
+ }
650
+
651
+ openssl::check_error (err, " Handshake failed" );
646
652
}
647
653
648
654
void TlsTransport::stop () {
@@ -661,12 +667,19 @@ bool TlsTransport::send(message_ptr message) {
661
667
662
668
PLOG_VERBOSE << " Send size=" << message->size ();
663
669
664
- std::lock_guard lock (mSslMutex );
665
- int ret = SSL_write (mSsl , message->data (), int (message->size ()));
666
- if (!openssl::check (mSsl , ret))
670
+ int err;
671
+ bool result;
672
+ {
673
+ std::lock_guard lock (mSslMutex );
674
+ int ret = SSL_write (mSsl , message->data (), int (message->size ()));
675
+ err = SSL_get_error (mSsl , ret);
676
+ result = flushOutput ();
677
+ }
678
+
679
+ if (!openssl::check_error (err))
667
680
throw std::runtime_error (" TLS send failed" );
668
681
669
- return flushOutput () ;
682
+ return result ;
670
683
}
671
684
672
685
void TlsTransport::incoming (message_ptr message) {
@@ -698,7 +711,7 @@ void TlsTransport::doRecv() {
698
711
const size_t bufferSize = 4096 ;
699
712
byte buffer[bufferSize];
700
713
701
- // Process incoming messages
714
+ // Read incoming messages
702
715
while (mIncomingQueue .running ()) {
703
716
auto next = mIncomingQueue .pop ();
704
717
if (!next)
@@ -712,44 +725,43 @@ void TlsTransport::doRecv() {
712
725
713
726
if (state () == State::Connecting) {
714
727
// Continue the handshake
715
- bool finished ;
728
+ int ret, err ;
716
729
{
717
730
std::lock_guard lock (mSslMutex );
718
- int ret = SSL_do_handshake (mSsl );
719
- if (!openssl::check (mSsl , ret, " Handshake failed" ))
720
- break ;
721
-
731
+ ret = SSL_do_handshake (mSsl );
732
+ err = SSL_get_error (mSsl , ret);
722
733
flushOutput ();
723
- finished = (SSL_is_init_finished (mSsl ) != 0 );
724
734
}
725
735
726
- if (finished ) {
736
+ if (openssl::check_error (err, " Handshake failed " ) ) {
727
737
PLOG_INFO << " TLS handshake finished" ;
728
738
changeState (State::Connected);
729
739
postHandshake ();
730
740
}
731
741
}
732
742
733
743
if (state () == State::Connected) {
734
- int ret;
744
+ int ret, err ;
735
745
while (true ) {
736
746
{
737
747
std::lock_guard lock (mSslMutex );
738
748
ret = SSL_read (mSsl , buffer, bufferSize);
749
+ err = SSL_get_error (mSsl , ret);
750
+ flushOutput (); // SSL_read() can also cause write operations
739
751
}
740
752
741
- if (ret > 0 )
753
+ if (err == SSL_ERROR_ZERO_RETURN)
754
+ break ;
755
+
756
+ if (openssl::check_error (err))
742
757
recv (make_message (buffer, buffer + ret));
743
758
else
744
759
break ;
745
760
}
746
761
747
- {
748
- std::lock_guard lock (mSslMutex );
749
- if (!openssl::check (mSsl , ret))
750
- break ;
751
-
752
- flushOutput (); // SSL_read() can also cause write operations
762
+ if (err == SSL_ERROR_ZERO_RETURN) {
763
+ PLOG_DEBUG << " TLS connection cleanly closed" ;
764
+ break ; // No more data can be read
753
765
}
754
766
}
755
767
}
0 commit comments