Skip to content

Commit 4a2792c

Browse files
committed
tls: emit 'end' on .receivedShutdown
NOTE: Also removed `.receivedShutdown` method of `Connection` it wasn't documented anywhere, and was rewritten with `true` after receiving `close_notify`. fix #6638
1 parent 92bbd60 commit 4a2792c

File tree

4 files changed

+67
-20
lines changed

4 files changed

+67
-20
lines changed

lib/tls.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -486,16 +486,21 @@ CryptoStream.prototype._read = function read(size) {
486486

487487
if (bytesRead === 0) {
488488
// EOF when cleartext has finished and we have nothing to read
489-
if (this._opposite._finished && this._internallyPendingBytes() === 0) {
489+
if (this._opposite._finished && this._internallyPendingBytes() === 0 ||
490+
this.pair.ssl && this.pair.ssl.receivedShutdown) {
490491
// Perform graceful shutdown
491492
this._done();
492493

493494
// No half-open, sorry!
494-
if (this === this.pair.cleartext)
495+
if (this === this.pair.cleartext) {
495496
this._opposite._done();
496497

497-
// EOF
498-
this.push(null);
498+
// EOF
499+
this.push(null);
500+
} else if (!this.pair.ssl || !this.pair.ssl.receivedShutdown) {
501+
// EOF
502+
this.push(null);
503+
}
499504
} else {
500505
// Bail out
501506
this.push('');

src/node_crypto.cc

-15
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,6 @@ void Connection::Initialize(Handle<Object> target) {
10221022
NODE_SET_PROTOTYPE_METHOD(t, "getCurrentCipher", Connection::GetCurrentCipher);
10231023
NODE_SET_PROTOTYPE_METHOD(t, "start", Connection::Start);
10241024
NODE_SET_PROTOTYPE_METHOD(t, "shutdown", Connection::Shutdown);
1025-
NODE_SET_PROTOTYPE_METHOD(t, "receivedShutdown", Connection::ReceivedShutdown);
10261025
NODE_SET_PROTOTYPE_METHOD(t, "close", Connection::Close);
10271026

10281027
#ifdef OPENSSL_NPN_NEGOTIATED
@@ -1766,20 +1765,6 @@ Handle<Value> Connection::Shutdown(const Arguments& args) {
17661765
}
17671766

17681767

1769-
Handle<Value> Connection::ReceivedShutdown(const Arguments& args) {
1770-
HandleScope scope;
1771-
1772-
Connection *ss = Connection::Unwrap(args);
1773-
1774-
if (ss->ssl_ == NULL) return False();
1775-
int r = SSL_get_shutdown(ss->ssl_);
1776-
1777-
if (r & SSL_RECEIVED_SHUTDOWN) return True();
1778-
1779-
return False();
1780-
}
1781-
1782-
17831768
Handle<Value> Connection::IsInitFinished(const Arguments& args) {
17841769
HandleScope scope;
17851770

src/node_crypto.h

-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ class Connection : ObjectWrap {
186186
static v8::Handle<v8::Value> VerifyError(const v8::Arguments& args);
187187
static v8::Handle<v8::Value> GetCurrentCipher(const v8::Arguments& args);
188188
static v8::Handle<v8::Value> Shutdown(const v8::Arguments& args);
189-
static v8::Handle<v8::Value> ReceivedShutdown(const v8::Arguments& args);
190189
static v8::Handle<v8::Value> Start(const v8::Arguments& args);
191190
static v8::Handle<v8::Value> Close(const v8::Arguments& args);
192191

test/simple/test-tls-close-notify.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
if (!process.versions.openssl) {
23+
console.error('Skipping because node compiled without OpenSSL.');
24+
process.exit(0);
25+
}
26+
27+
var assert = require('assert');
28+
var fs = require('fs');
29+
var net = require('net');
30+
var tls = require('tls');
31+
32+
var common = require('../common');
33+
34+
var ended = 0;
35+
36+
var server = tls.createServer({
37+
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
38+
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
39+
}, function(c) {
40+
// Send close-notify without shutting down TCP socket
41+
if (c.pair.ssl.shutdown() !== 1)
42+
c.pair.ssl.shutdown();
43+
}).listen(common.PORT, function() {
44+
var c = tls.connect(common.PORT, {
45+
rejectUnauthorized: false
46+
}, function() {
47+
// Ensure that we receive 'end' event anyway
48+
c.on('end', function() {
49+
ended++;
50+
c.destroy();
51+
server.close();
52+
});
53+
});
54+
});
55+
56+
process.on('exit', function() {
57+
assert.equal(ended, 1);
58+
});

0 commit comments

Comments
 (0)