forked from nodejs/node-v0.x-archive
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tls: expose SSL_CTX_set_timeout via tls.createServer
Add the `sessionTimeout` integral value to the list of options recognized by `tls.createServer`. This option will be useful for applications which need frequently establish short-lived TLS connections to the same endpoint. The TLS tickets RFC is an ideal option to reduce the socket setup overhead for such scenarios, but the default ticket timeout value (5 minutes) is too low to be useful.
- Loading branch information
1 parent
1a65154
commit d205761
Showing
6 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
-----BEGIN SSL SESSION PARAMETERS----- | ||
MIID2wIBAQICAwEEAgA1BCAMjLe+70uBSPGvybkTnPVUMwdbdtVbkMIXf8L5M8Kl | ||
VAQwog+Afs00cnYUcgD1BQewJyxX1e561oRuDTpy7BHABC1hC7hxTaul+pwv+cBx | ||
8D72oQYCBFFQF3OiBAICASyjggNhMIIDXTCCAkWgAwIBAgIJAMUSOvlaeyQHMA0G | ||
CSqGSIb3DQEBBQUAMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRl | ||
MSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwHhcN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iSfzox6QCBAClAwIBEg== | ||
-----END SSL SESSION PARAMETERS----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// Copyright Joyent, Inc. and other Node contributors. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a | ||
// copy of this software and associated documentation files (the | ||
// "Software"), to deal in the Software without restriction, including | ||
// without limitation the rights to use, copy, modify, merge, publish, | ||
// distribute, sublicense, and/or sell copies of the Software, and to permit | ||
// persons to whom the Software is furnished to do so, subject to the | ||
// following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included | ||
// in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
// USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
if (!process.versions.openssl) { | ||
console.error('Skipping because node compiled without OpenSSL.'); | ||
process.exit(0); | ||
} | ||
require('child_process').exec('openssl version', function(err) { | ||
if (err !== null) { | ||
console.error('Skipping because openssl command is not available.'); | ||
process.exit(0); | ||
} | ||
doTest(); | ||
}); | ||
|
||
// This test consists of three TLS requests -- | ||
// * The first one should result in a new connection because we don't have | ||
// a valid session ticket. | ||
// * The second one should result in connection resumption because we used | ||
// the session ticket we saved from the first connection. | ||
// * The third one should result in a new connection because the ticket | ||
// that we used has expired by now. | ||
|
||
function doTest() { | ||
var common = require('../common'); | ||
var assert = require('assert'); | ||
var tls = require('tls'); | ||
var fs = require('fs'); | ||
var join = require('path').join; | ||
var spawn = require('child_process').spawn; | ||
|
||
var SESSION_TIMEOUT = 1; | ||
|
||
var keyFile = join(common.fixturesDir, 'agent.key'); | ||
var certFile = join(common.fixturesDir, 'agent.crt'); | ||
var key = fs.readFileSync(keyFile); | ||
var cert = fs.readFileSync(certFile); | ||
var options = { | ||
key: key, | ||
cert: cert, | ||
ca: [cert], | ||
sessionTimeout: SESSION_TIMEOUT | ||
}; | ||
|
||
// We need to store a sample session ticket in the fixtures directory because | ||
// `s_client` behaves incorrectly if we do not pass in both the `-sess_in` | ||
// and the `-sess_out` flags, and the `-sess_in` argument must point to a | ||
// file containing a proper serialization of a session ticket. | ||
// To avoid a source control diff, we copy the ticket to a temporary file. | ||
|
||
var sessionFileName = (function () { | ||
var ticketFileName = 'tls-session-ticket.txt'; | ||
var fixturesPath = join(common.fixturesDir, ticketFileName); | ||
var tmpPath = join(common.tmpDir, ticketFileName); | ||
fs.writeFileSync(tmpPath, fs.readFileSync(fixturesPath)); | ||
return tmpPath; | ||
}()); | ||
|
||
// Expects a callback -- cb(connectionType : enum ['New'|'Reused']) | ||
|
||
var Client = function (cb) { | ||
var flags = [ | ||
's_client', | ||
'-connect', 'localhost:' + common.PORT, | ||
'-sess_in', sessionFileName, | ||
'-sess_out', sessionFileName | ||
]; | ||
var client = spawn('openssl', flags, { | ||
stdio: ['ignore', 'pipe', 'ignore'] | ||
}); | ||
|
||
var clientOutput = ''; | ||
client.stdout.on('data', function(data) { | ||
clientOutput += data.toString(); | ||
}); | ||
client.on('exit', function(code) { | ||
var connectionType; | ||
var grepConnectionType = function (line) { | ||
var matches = line.match(/(New|Reused), /); | ||
if (matches) { | ||
connectionType = matches[1]; | ||
return true; | ||
} | ||
}; | ||
var lines = clientOutput.split('\n'); | ||
if (!lines.some(grepConnectionType)) { | ||
throw new Error('unexpected output from openssl client'); | ||
} | ||
cb(connectionType); | ||
}); | ||
}; | ||
|
||
var server = tls.createServer(options, function(cleartext) { | ||
cleartext.on('error', function(er) { | ||
if (er.code !== 'ECONNRESET') | ||
throw er; | ||
}); | ||
cleartext.end(); | ||
}); | ||
|
||
server.listen(common.PORT, function() { | ||
Client(function(connectionType) { | ||
assert(connectionType === 'New'); | ||
Client(function(connectionType) { | ||
assert(connectionType === 'Reused'); | ||
setTimeout(function () { | ||
Client(function(connectionType) { | ||
assert(connectionType === 'New'); | ||
server.close(); | ||
}); | ||
}, (SESSION_TIMEOUT + 1) * 1000); | ||
}); | ||
}); | ||
}); | ||
} |