@@ -118,6 +118,43 @@ static void throw_openssl_error()
118
118
throw_openssl_error_msg (buffer);
119
119
}
120
120
121
+ /*
122
+ Function should be called after SSL_read/SSL_write returns error (<=0).
123
+ It will get ssl error and throw it if needed.
124
+ Will return normally if the error can be continued.
125
+ */
126
+ static void throw_ssl_error (SSL* tls, int err)
127
+ {
128
+ switch (SSL_get_error (tls, err))
129
+ {
130
+ case SSL_ERROR_WANT_READ:
131
+ case SSL_ERROR_WANT_WRITE:
132
+ #ifndef WITH_SSL_YASSL
133
+ case SSL_ERROR_WANT_CONNECT:
134
+ case SSL_ERROR_WANT_ACCEPT:
135
+ case SSL_ERROR_WANT_X509_LOOKUP:
136
+ # if OPENSSL_VERSION_NUMBER >= 0x10100000L
137
+ case SSL_ERROR_WANT_ASYNC:
138
+ case SSL_ERROR_WANT_ASYNC_JOB:
139
+ # endif
140
+ #endif
141
+ // Will not throw anything, so function that calls this, will continue.
142
+ break ;
143
+ case SSL_ERROR_ZERO_RETURN:
144
+ throw cdk::foundation::connection::Error_eos ();
145
+ case SSL_ERROR_SYSCALL:
146
+ cdk::foundation::throw_posix_error ();
147
+ case SSL_ERROR_SSL:
148
+ throw_openssl_error ();
149
+ default :
150
+ {
151
+ char buffer[512 ];
152
+ ERR_error_string_n (static_cast <unsigned long >(SSL_get_error (tls, err)), buffer, sizeof (buffer));
153
+ throw_openssl_error_msg (buffer);
154
+ }
155
+ }
156
+ }
157
+
121
158
122
159
/*
123
160
Implementation of TLS connection class.
@@ -467,8 +504,10 @@ bool TLS::Read_op::common_read()
467
504
468
505
int result = SSL_read (impl.m_tls , data, buffer_size);
469
506
470
- if (result == -1 )
471
- throw IO_error (SSL_get_error (impl.m_tls ,0 ));
507
+ if (result <= 0 )
508
+ {
509
+ throw_ssl_error (impl.m_tls , result);
510
+ }
472
511
473
512
if (result > 0 )
474
513
{
@@ -525,6 +564,11 @@ bool TLS::Read_some_op::common_read()
525
564
526
565
int result = SSL_read (impl.m_tls , buffer.begin (), (int )buffer.size ());
527
566
567
+ if (result <= 0 )
568
+ {
569
+ throw_ssl_error (impl.m_tls , result);
570
+ }
571
+
528
572
if (result > 0 )
529
573
{
530
574
set_completed (static_cast <size_t >(result));
@@ -574,6 +618,11 @@ bool TLS::Write_op::common_write()
574
618
575
619
int result = SSL_write (impl.m_tls , data, buffer_size);
576
620
621
+ if (result <= 0 )
622
+ {
623
+ throw_ssl_error (impl.m_tls , result);
624
+ }
625
+
577
626
if (result > 0 )
578
627
{
579
628
m_currentBufferOffset += result;
@@ -629,6 +678,11 @@ bool TLS::Write_some_op::common_write()
629
678
630
679
int result = SSL_write (impl.m_tls , buffer.begin (), (int )buffer.size ());
631
680
681
+ if (result <= 0 )
682
+ {
683
+ throw_ssl_error (impl.m_tls , result);
684
+ }
685
+
632
686
if (result > 0 )
633
687
{
634
688
set_completed (static_cast <size_t >(result));
0 commit comments