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.
25 var common = require('../common');
26 var assert = require('assert');
27 var fs = require('fs');
28 var net = require('net');
32 var crypto = require('crypto');
36 console.log('Not compiled with OPENSSL support.');
40 var caPem = fs.readFileSync(common.fixturesDir + '/test_ca.pem', 'ascii');
41 var certPem = fs.readFileSync(common.fixturesDir + '/test_cert.pem', 'ascii');
42 var keyPem = fs.readFileSync(common.fixturesDir + '/test_key.pem', 'ascii');
45 var credentials = crypto.createCredentials(
51 console.log('Not compiled with OPENSSL support.');
55 var testData = 'TEST123';
58 var gotSecureServer = false;
59 var gotSecureClient = false;
61 var secureServer = net.createServer(function(connection) {
63 connection.setSecure(credentials);
64 connection.setEncoding('UTF8');
66 connection.on('secure', function() {
67 gotSecureServer = true;
68 var verified = connection.verifyPeer();
69 var peerDN = JSON.stringify(connection.getPeerCertificate());
70 assert.equal(verified, true);
72 '{"subject":"/C=UK/ST=Acknack Ltd/L=Rhys Jones' +
73 '/O=node.js/OU=Test TLS Certificate/CN=localhost",' +
74 '"issuer":"/C=UK/ST=Acknack Ltd/L=Rhys Jones/O=node.js' +
75 '/OU=Test TLS Certificate/CN=localhost",' +
76 '"valid_from":"Nov 11 09:52:22 2009 GMT",' +
77 '"valid_to":"Nov 6 09:52:22 2029 GMT",' +
78 '"fingerprint":"2A:7A:C2:DD:E5:F9:CC:53:72:35:99:7A:02:' +
79 '5A:71:38:52:EC:8A:DF"}');
83 connection.on('data', function(chunk) {
85 connection.write(chunk);
88 connection.on('end', function() {
89 assert.equal(serverData, testData);
94 secureServer.listen(common.PORT);
96 secureServer.on('listening', function() {
97 var secureClient = net.createConnection(common.PORT);
99 secureClient.setEncoding('UTF8');
100 secureClient.on('connect', function() {
101 secureClient.setSecure(credentials);
104 secureClient.on('secure', function() {
105 gotSecureClient = true;
106 var verified = secureClient.verifyPeer();
107 var peerDN = JSON.stringify(secureClient.getPeerCertificate());
108 assert.equal(verified, true);
110 '{"subject":"/C=UK/ST=Acknack Ltd/L=Rhys Jones' +
111 '/O=node.js/OU=Test TLS Certificate/CN=localhost",' +
112 '"issuer":"/C=UK/ST=Acknack Ltd/L=Rhys Jones/O=node.js' +
113 '/OU=Test TLS Certificate/CN=localhost",' +
114 '"valid_from":"Nov 11 09:52:22 2009 GMT",' +
115 '"valid_to":"Nov 6 09:52:22 2029 GMT",' +
116 '"fingerprint":"2A:7A:C2:DD:E5:F9:CC:53:72:35:99:7A:02:' +
117 '5A:71:38:52:EC:8A:DF"}');
119 secureClient.write(testData);
123 secureClient.on('data', function(chunk) {
127 secureClient.on('end', function() {
128 assert.equal(clientData, testData);
132 process.on('exit', function() {
133 assert.ok(gotSecureServer, 'Did not get secure event for server');
134 assert.ok(gotSecureClient, 'Did not get secure event for client');