Skip to content

Commit 86f06c2

Browse files
committed
IO library: fix ssl filter error handling / reporting by clearing openssl current thread's error queue prior to invoking library methods followed by error retrieval. Do not invoke SSL shutdown in filter close in the case if ssl handshake has not finished.
1 parent 25666a7 commit 86f06c2

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/cc/kfsio/SslFilter.cc

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,12 @@ class SslFilter::Impl : private IOBuffer::Reader
494494
}
495495
if (inMaxRead == 0) {
496496
// Don't want to read, just complete handshake.
497-
theRet = mSslEofFlag ? 0 :
498-
SSL_peek(mSslPtr, &theByte, sizeof(theByte));
497+
if (mSslEofFlag) {
498+
theRet = 0;
499+
} else {
500+
ERR_clear_error();
501+
theRet = SSL_peek(mSslPtr, &theByte, sizeof(theByte));
502+
}
499503
mReadPendingFlag = 0 < theRet;
500504
if (theRet < 0) {
501505
theRet = SslRetToErr(theRet);
@@ -532,6 +536,7 @@ class SslFilter::Impl : private IOBuffer::Reader
532536
if (inIoBuffer.IsEmpty()) {
533537
return 0;
534538
}
539+
ERR_clear_error();
535540
int theWrCnt = 0;
536541
for (IOBuffer::iterator theIt = inIoBuffer.begin();
537542
theIt != inIoBuffer.end();
@@ -560,8 +565,14 @@ class SslFilter::Impl : private IOBuffer::Reader
560565
SslFilter& inOuter)
561566
{
562567
if (mSslPtr && inSocketPtr &&
563-
SSL_get_fd(mSslPtr) == inSocketPtr->GetFd()) {
564-
SSL_shutdown(mSslPtr);
568+
SSL_get_fd(mSslPtr) == inSocketPtr->GetFd() &&
569+
SSL_is_init_finished(mSslPtr)) {
570+
ERR_clear_error();
571+
const int theRet = SSL_shutdown(mSslPtr);
572+
if (theRet <= 0) {
573+
// Drain error queue.
574+
SslRetToErr(theRet);
575+
}
565576
}
566577
inConnection.SetFilter(0, 0);
567578
if (mDeleteOnCloseFlag) {
@@ -606,6 +617,7 @@ class SslFilter::Impl : private IOBuffer::Reader
606617
mPeerPskId.clear();
607618
mServerFlag = ! SSL_in_connect_init(mSslPtr);
608619
SetStoredClientSession();
620+
ERR_clear_error();
609621
const int theSslRet = mServerFlag ?
610622
SSL_accept(mSslPtr) : SSL_connect(mSslPtr);
611623
if (theSslRet <= 0) {
@@ -655,6 +667,7 @@ class SslFilter::Impl : private IOBuffer::Reader
655667
if (! inBufPtr || ! mSslPtr) {
656668
return -EINVAL;
657669
}
670+
ERR_clear_error();
658671
char* thePtr = reinterpret_cast<char*>(inBufPtr);
659672
char* const theStartPtr = thePtr;
660673
char* const theEndPtr = thePtr + inNumRead;
@@ -1042,6 +1055,7 @@ class SslFilter::Impl : private IOBuffer::Reader
10421055
mVerifyOrGetPskInvokedFlag = false;
10431056
}
10441057
mRenegotiationPendingFlag = false;
1058+
ERR_clear_error();
10451059
const int theRet = SSL_do_handshake(mSslPtr);
10461060
if (0 < theRet) {
10471061
if (! VerifyPeerIfNeeded()) {
@@ -1197,6 +1211,7 @@ class SslFilter::Impl : private IOBuffer::Reader
11971211
// Wait for handshake to complete, then issue shutdown.
11981212
return 0;
11991213
}
1214+
ERR_clear_error();
12001215
int theRet = SSL_shutdown(mSslPtr);
12011216
if (theRet == 0) {
12021217
// Call shutdown again to initiate read state, if the shutdown call

0 commit comments

Comments
 (0)