From 019ad346e0a9f1669a1e81b0ae3eb2e0f7e4ddd7 Mon Sep 17 00:00:00 2001 From: Sergey Kholodilov Date: Sat, 10 Nov 2012 00:32:28 +0400 Subject: [PATCH] crypto: fix ssl error handling Make HandleSSLError() correctly process a zero status code: sometimes it indicates an error and sometimes it doesn't. --- src/node_crypto.cc | 24 ++++++++++++------------ src/node_crypto.h | 8 +++++++- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/node_crypto.cc b/src/node_crypto.cc index eedd3340f53..ac90259e10a 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -890,8 +890,9 @@ int Connection::HandleBIOError(BIO *bio, const char* func, int rv) { } -int Connection::HandleSSLError(const char* func, int rv) { - if (rv >= 0) return rv; +int Connection::HandleSSLError(const char* func, int rv, ZeroStatus zs) { + if (rv > 0) return rv; + if ((rv == 0) && (zs == kZeroIsNotAnError)) return rv; int err = SSL_get_error(ssl_, rv); @@ -1348,17 +1349,17 @@ Handle Connection::ClearOut(const Arguments& args) { if (ss->is_server_) { rv = SSL_accept(ss->ssl_); - ss->HandleSSLError("SSL_accept:ClearOut", rv); + ss->HandleSSLError("SSL_accept:ClearOut", rv, kZeroIsAnError); } else { rv = SSL_connect(ss->ssl_); - ss->HandleSSLError("SSL_connect:ClearOut", rv); + ss->HandleSSLError("SSL_connect:ClearOut", rv, kZeroIsAnError); } if (rv < 0) return scope.Close(Integer::New(rv)); } int bytes_read = SSL_read(ss->ssl_, buffer_data + off, len); - ss->HandleSSLError("SSL_read:ClearOut", bytes_read); + ss->HandleSSLError("SSL_read:ClearOut", bytes_read, kZeroIsNotAnError); ss->SetShutdownFlags(); return scope.Close(Integer::New(bytes_read)); @@ -1458,10 +1459,10 @@ Handle Connection::ClearIn(const Arguments& args) { int rv; if (ss->is_server_) { rv = SSL_accept(ss->ssl_); - ss->HandleSSLError("SSL_accept:ClearIn", rv); + ss->HandleSSLError("SSL_accept:ClearIn", rv, kZeroIsAnError); } else { rv = SSL_connect(ss->ssl_); - ss->HandleSSLError("SSL_connect:ClearIn", rv); + ss->HandleSSLError("SSL_connect:ClearIn", rv, kZeroIsAnError); } if (rv < 0) return scope.Close(Integer::New(rv)); @@ -1469,7 +1470,7 @@ Handle Connection::ClearIn(const Arguments& args) { int bytes_written = SSL_write(ss->ssl_, buffer_data + off, len); - ss->HandleSSLError("SSL_write:ClearIn", bytes_written); + ss->HandleSSLError("SSL_write:ClearIn", bytes_written, kZeroIsAnError); ss->SetShutdownFlags(); return scope.Close(Integer::New(bytes_written)); @@ -1697,10 +1698,10 @@ Handle Connection::Start(const Arguments& args) { int rv; if (ss->is_server_) { rv = SSL_accept(ss->ssl_); - ss->HandleSSLError("SSL_accept:Start", rv); + ss->HandleSSLError("SSL_accept:Start", rv, kZeroIsAnError); } else { rv = SSL_connect(ss->ssl_); - ss->HandleSSLError("SSL_connect:Start", rv); + ss->HandleSSLError("SSL_connect:Start", rv, kZeroIsAnError); } return scope.Close(Integer::New(rv)); @@ -1717,8 +1718,7 @@ Handle Connection::Shutdown(const Arguments& args) { if (ss->ssl_ == NULL) return False(); int rv = SSL_shutdown(ss->ssl_); - - ss->HandleSSLError("SSL_shutdown", rv); + ss->HandleSSLError("SSL_shutdown", rv, kZeroIsNotAnError); ss->SetShutdownFlags(); return scope.Close(Integer::New(rv)); diff --git a/src/node_crypto.h b/src/node_crypto.h index 91fbb2249b8..ee3cf93ba00 100644 --- a/src/node_crypto.h +++ b/src/node_crypto.h @@ -214,7 +214,13 @@ class Connection : ObjectWrap { #endif int HandleBIOError(BIO *bio, const char* func, int rv); - int HandleSSLError(const char* func, int rv); + + enum ZeroStatus { + kZeroIsNotAnError, + kZeroIsAnError + }; + + int HandleSSLError(const char* func, int rv, ZeroStatus zs); void ClearError(); void SetShutdownFlags();