-
Notifications
You must be signed in to change notification settings - Fork 3
/
starttls.js
175 lines (136 loc) · 4.15 KB
/
starttls.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Original: https://gist.github.com/TooTallNate/848444
* Adapted: https://github.com/andris9/rai/blob/master/lib/starttls.js
*
* @overview
* @author Matthew Caruana Galizia <m@m.cg>
* @author Andris Reinman <andris.reinman@gmail.com>
* @author Nathan Rajlich <nathan@tootallnate.net>
* @copyright Copyright (c) 2012, Andris Reinman
* @copyright Copyright (c) 2011, Nathan Rajlich
* @license MIT
* @preserve
*/
'use strict';
/*jshint node:true*/
/*global exports:true*/
var net = require('net');
var tls = require('tls');
var crypto = require('crypto');
module.exports = exports = function(options, onSecure) {
var socket, credentials, securePair;
if (options instanceof net.Socket) {
socket = options;
options = {
socket: socket
};
} else if (options.socket) {
socket = options.socket;
} else {
socket = options.socket = net.createConnection(options);
}
if (options.pair) {
securePair = options.pair;
} else {
// Node v0.12.0 deprecated crypto.createCredentials.
if (tls.createSecureContext) {
credentials = tls.createSecureContext();
} else {
credentials = crypto.createCredentials();
}
securePair = tls.createSecurePair(credentials, false);
options.pair = securePair;
}
// In Node < 0.9.0, socket.readable is undefined.
if (socket.readable || undefined === socket.readable) {
return startTls(options, onSecure);
}
// In Node > 0.9.0, if the socket is still unconnected then wait for connect.
socket.once('connect', function() {
startTls(options, onSecure);
});
return securePair;
};
function startTls(options, onSecure) {
var socket, host, securePair, clearText;
socket = options.socket;
host = options.host;
securePair = options.pair;
socket.ondata = null;
socket.removeAllListeners('data');
clearText = pipe(securePair, socket);
securePair.once('secure', function() {
var err;
// A cleartext stream has the boolean property 'authorized' to determine if it was verified by the CA. If 'authorized' is false, a property 'authorizationError' is set on the stream.
err = securePair.ssl.verifyError();
if (err) {
clearText.authorized = false;
clearText.authorizationError = err;
} else {
clearText.authorized = true;
}
// The callback parameter is optional.
if (!onSecure) {
return;
}
if (host) {
err = tls.checkServerIdentity(host, clearText.getPeerCertificate());
// As of node v0.12.0, `tls.checkServerIdentity` returns an error object if there was an error, `undefined` otherwise.
// On previous versions it returned `false` if there was an error and `true` otherwise.
if (false === err) {
err = new Error('Server identity mismatch: invalid certificate for ' + host + '.');
} else if (true === err) {
err = null;
}
}
onSecure.call(securePair, err);
});
clearText._controlReleased = true;
return securePair;
}
function forwardEvents(events, emitterSource, emitterDestination) {
var i, l, event, handler, forwardEvent;
forwardEvent = function() {
this.emit.apply(this, arguments);
};
for (i = 0, l = events.length; i < l; i++) {
event = events[i];
handler = forwardEvent.bind(emitterDestination, event);
emitterSource.on(event, handler);
}
}
function removeEvents(events, emitterSource) {
var i, l;
for (i = 0, l = events.length; i < l; i++){
emitterSource.removeAllListeners(events[i]);
}
}
function pipe(securePair, socket) {
var clearText, onError, onClose, events;
events = ['timeout', 'end', 'drain'];
clearText = securePair.cleartext;
onError = function(err) {
if (clearText._controlReleased) {
clearText.emit('error', err);
}
};
onClose = function() {
socket.removeListener('error', onError);
socket.removeListener('close', onClose);
removeEvents(events, socket);
};
// Forward event emissions from the socket to the cleartext stream.
forwardEvents(events, socket, clearText);
socket.on('error', onError);
socket.on('close', onClose);
securePair.on('error', function(err) {
onError(err);
});
securePair.encrypted.pipe(socket);
socket.pipe(securePair.encrypted);
securePair.fd = socket.fd;
clearText.socket = socket;
clearText.encrypted = securePair.encrypted;
clearText.authorized = false;
return clearText;
}