1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 var common = require('../common');
24 if (!common.opensslCli) {
25 console.error('Skipping because node compiled without OpenSSL CLI.');
29 var assert = require('assert');
31 var join = require('path').join;
32 var net = require('net');
33 var fs = require('fs');
34 var tls = require('tls');
35 var spawn = require('child_process').spawn;
38 var key = fs.readFileSync(join(common.fixturesDir, 'agent.key')).toString();
39 var cert = fs.readFileSync(join(common.fixturesDir, 'agent.crt')).toString();
42 console.error('***server*** ' + a);
45 var server = net.createServer(function(socket) {
47 log('connection fd=' + socket.fd);
48 var sslcontext = tls.createSecureContext({key: key, cert: cert});
49 sslcontext.context.setCiphers('RC4-SHA:AES128-SHA:AES256-SHA');
51 var pair = tls.createSecurePair(sslcontext, true);
53 assert.ok(pair.encrypted.writable);
54 assert.ok(pair.cleartext.writable);
56 pair.encrypted.pipe(socket);
57 socket.pipe(pair.encrypted);
59 log('i set it secure');
61 pair.on('secure', function() {
62 log('connected+secure!');
63 pair.cleartext.write('hello\r\n');
64 log(pair.cleartext.getPeerCertificate());
65 log(pair.cleartext.getCipher());
68 pair.cleartext.on('data', function(data) {
69 log('read bytes ' + data.length);
70 pair.cleartext.write(data);
73 socket.on('end', function() {
77 pair.cleartext.on('error', function(err) {
84 pair.encrypted.on('error', function(err) {
85 log('encrypted error: ');
91 socket.on('error', function(err) {
92 log('socket error: ');
98 socket.on('close', function(err) {
102 pair.on('error', function(err) {
103 log('secure error: ');
110 var gotHello = false;
111 var sentWorld = false;
112 var gotWorld = false;
113 var opensslExitCode = -1;
115 server.listen(common.PORT, function() {
116 // To test use: openssl s_client -connect localhost:8000
117 var client = spawn(common.opensslCli, ['s_client', '-connect', '127.0.0.1:' +
123 client.stdout.setEncoding('utf8');
124 client.stdout.on('data', function(d) {
127 if (!gotHello && /hello/.test(out)) {
129 client.stdin.write('world\r\n');
133 if (!gotWorld && /world/.test(out)) {
139 client.stdout.pipe(process.stdout, { end: false });
141 client.on('exit', function(code) {
142 opensslExitCode = code;
147 process.on('exit', function() {
148 assert.equal(1, connections);
150 assert.ok(sentWorld);
152 assert.equal(0, opensslExitCode);