Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
crypto: fix ssl error handling
Browse files Browse the repository at this point in the history
Make HandleSSLError() correctly process a zero status code: sometimes it
indicates an error and sometimes it doesn't.
  • Loading branch information
fat-crocodile authored and bnoordhuis committed Nov 16, 2012
1 parent 71ba7bc commit 019ad34
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
24 changes: 12 additions & 12 deletions src/node_crypto.cc
Expand Up @@ -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);

Expand Down Expand Up @@ -1348,17 +1349,17 @@ Handle<Value> 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));
Expand Down Expand Up @@ -1458,18 +1459,18 @@ Handle<Value> 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));
}

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));
Expand Down Expand Up @@ -1697,10 +1698,10 @@ Handle<Value> 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));
Expand All @@ -1717,8 +1718,7 @@ Handle<Value> 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));
Expand Down
8 changes: 7 additions & 1 deletion src/node_crypto.h
Expand Up @@ -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();
Expand Down

0 comments on commit 019ad34

Please sign in to comment.