Skip to content

Commit af80e7b

Browse files
committed
tls: introduce TLSSocket based on tls_wrap binding
Split `tls.js` into `_tls_legacy.js`, containing legacy `createSecurePair` API, and `_tls_wrap.js` containing new code based on `tls_wrap` binding. Remove tests that are no longer useful/valid.
1 parent 03e008d commit af80e7b

15 files changed

+1455
-1764
lines changed

doc/api/tls.markdown

+59-83
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ server-side resources, which makes it a potential vector for denial-of-service
4949
attacks.
5050

5151
To mitigate this, renegotiations are limited to three times every 10 minutes. An
52-
error is emitted on the [CleartextStream][] instance when the threshold is
52+
error is emitted on the [tls.TLSSocket][] instance when the threshold is
5353
exceeded. The limits are configurable:
5454

5555
- `tls.CLIENT_RENEG_LIMIT`: renegotiation limit, default is 3.
@@ -188,12 +188,12 @@ Here is a simple example echo server:
188188
ca: [ fs.readFileSync('client-cert.pem') ]
189189
};
190190

191-
var server = tls.createServer(options, function(cleartextStream) {
191+
var server = tls.createServer(options, function(socket) {
192192
console.log('server connected',
193-
cleartextStream.authorized ? 'authorized' : 'unauthorized');
194-
cleartextStream.write("welcome!\n");
195-
cleartextStream.setEncoding('utf8');
196-
cleartextStream.pipe(cleartextStream);
193+
socket.authorized ? 'authorized' : 'unauthorized');
194+
socket.write("welcome!\n");
195+
socket.setEncoding('utf8');
196+
socket.pipe(socket);
197197
});
198198
server.listen(8000, function() {
199199
console.log('server bound');
@@ -212,12 +212,12 @@ Or
212212

213213
};
214214

215-
var server = tls.createServer(options, function(cleartextStream) {
215+
var server = tls.createServer(options, function(socket) {
216216
console.log('server connected',
217-
cleartextStream.authorized ? 'authorized' : 'unauthorized');
218-
cleartextStream.write("welcome!\n");
219-
cleartextStream.setEncoding('utf8');
220-
cleartextStream.pipe(cleartextStream);
217+
socket.authorized ? 'authorized' : 'unauthorized');
218+
socket.write("welcome!\n");
219+
socket.setEncoding('utf8');
220+
socket.pipe(socket);
221221
});
222222
server.listen(8000, function() {
223223
console.log('server bound');
@@ -228,15 +228,6 @@ You can test this server by connecting to it with `openssl s_client`:
228228
openssl s_client -connect 127.0.0.1:8000
229229

230230

231-
## tls.SLAB_BUFFER_SIZE
232-
233-
Size of slab buffer used by all tls servers and clients.
234-
Default: `10 * 1024 * 1024`.
235-
236-
237-
Don't change the defaults unless you know what you are doing.
238-
239-
240231
## tls.connect(options, [callback])
241232
## tls.connect(port, [host], [options], [callback])
242233

@@ -285,7 +276,7 @@ Creates a new client connection to the given `port` and `host` (old API) or
285276
The `callback` parameter will be added as a listener for the
286277
['secureConnect'][] event.
287278

288-
`tls.connect()` returns a [CleartextStream][] object.
279+
`tls.connect()` returns a [tls.TLSSocket][] object.
289280

290281
Here is an example of a client of echo server as described previously:
291282

@@ -301,17 +292,17 @@ Here is an example of a client of echo server as described previously:
301292
ca: [ fs.readFileSync('server-cert.pem') ]
302293
};
303294

304-
var cleartextStream = tls.connect(8000, options, function() {
295+
var socket = tls.connect(8000, options, function() {
305296
console.log('client connected',
306-
cleartextStream.authorized ? 'authorized' : 'unauthorized');
307-
process.stdin.pipe(cleartextStream);
297+
socket.authorized ? 'authorized' : 'unauthorized');
298+
process.stdin.pipe(socket);
308299
process.stdin.resume();
309300
});
310-
cleartextStream.setEncoding('utf8');
311-
cleartextStream.on('data', function(data) {
301+
socket.setEncoding('utf8');
302+
socket.on('data', function(data) {
312303
console.log(data);
313304
});
314-
cleartextStream.on('end', function() {
305+
socket.on('end', function() {
315306
server.close();
316307
});
317308

@@ -324,22 +315,24 @@ Or
324315
pfx: fs.readFileSync('client.pfx')
325316
};
326317

327-
var cleartextStream = tls.connect(8000, options, function() {
318+
var socket = tls.connect(8000, options, function() {
328319
console.log('client connected',
329-
cleartextStream.authorized ? 'authorized' : 'unauthorized');
330-
process.stdin.pipe(cleartextStream);
320+
socket.authorized ? 'authorized' : 'unauthorized');
321+
process.stdin.pipe(socket);
331322
process.stdin.resume();
332323
});
333-
cleartextStream.setEncoding('utf8');
334-
cleartextStream.on('data', function(data) {
324+
socket.setEncoding('utf8');
325+
socket.on('data', function(data) {
335326
console.log(data);
336327
});
337-
cleartextStream.on('end', function() {
328+
socket.on('end', function() {
338329
server.close();
339330
});
340331

341332
## tls.createSecurePair([credentials], [isServer], [requestCert], [rejectUnauthorized])
342333

334+
** Deprecated **
335+
343336
Creates a new secure pair object with two streams, one of which reads/writes
344337
encrypted data, and one reads/writes cleartext data.
345338
Generally the encrypted one is piped to/from an incoming encrypted data stream,
@@ -357,9 +350,11 @@ and the cleartext one is used as a replacement for the initial encrypted stream.
357350
automatically reject clients with invalid certificates. Only applies to
358351
servers with `requestCert` enabled.
359352

360-
`tls.createSecurePair()` returns a SecurePair object with [cleartext][] and
353+
`tls.createSecurePair()` returns a SecurePair object with `cleartext` and
361354
`encrypted` stream properties.
362355

356+
NOTE: `cleartext` has the same APIs as [tls.TLSSocket][]
357+
363358
## Class: SecurePair
364359

365360
Returned by tls.createSecurePair.
@@ -381,50 +376,31 @@ connections using TLS or SSL.
381376

382377
### Event: 'secureConnection'
383378

384-
`function (cleartextStream) {}`
379+
`function (tlsSocket) {}`
385380

386381
This event is emitted after a new connection has been successfully
387-
handshaked. The argument is a instance of [CleartextStream][]. It has all the
382+
handshaked. The argument is a instance of [tls.TLSSocket][]. It has all the
388383
common stream methods and events.
389384

390-
`cleartextStream.authorized` is a boolean value which indicates if the
385+
`socket.authorized` is a boolean value which indicates if the
391386
client has verified by one of the supplied certificate authorities for the
392-
server. If `cleartextStream.authorized` is false, then
393-
`cleartextStream.authorizationError` is set to describe how authorization
387+
server. If `socket.authorized` is false, then
388+
`socket.authorizationError` is set to describe how authorization
394389
failed. Implied but worth mentioning: depending on the settings of the TLS
395390
server, you unauthorized connections may be accepted.
396-
`cleartextStream.npnProtocol` is a string containing selected NPN protocol.
397-
`cleartextStream.servername` is a string containing servername requested with
391+
`socket.npnProtocol` is a string containing selected NPN protocol.
392+
`socket.servername` is a string containing servername requested with
398393
SNI.
399394

400395

401396
### Event: 'clientError'
402397

403-
`function (exception, securePair) { }`
398+
`function (exception, tlsSocket) { }`
404399

405400
When a client connection emits an 'error' event before secure connection is
406401
established - it will be forwarded here.
407402

408-
`securePair` is the `tls.SecurePair` that the error originated from.
409-
410-
411-
### Event: 'newSession'
412-
413-
`function (sessionId, sessionData) { }`
414-
415-
Emitted on creation of TLS session. May be used to store sessions in external
416-
storage.
417-
418-
419-
### Event: 'resumeSession'
420-
421-
`function (sessionId, callback) { }`
422-
423-
Emitted when client wants to resume previous TLS session. Event listener may
424-
perform lookup in external storage using given `sessionId`, and invoke
425-
`callback(null, sessionData)` once finished. If session can't be resumed
426-
(i.e. doesn't exist in storage) one may call `callback(null, null)`. Calling
427-
`callback(err)` will terminate incoming connection and destroy socket.
403+
`tlsSocket` is the [tls.TLSSocket][] that the error originated from.
428404

429405

430406
### server.listen(port, [host], [callback])
@@ -469,44 +445,44 @@ The number of concurrent connections on the server.
469445

470446
## Class: CryptoStream
471447

448+
** Deprecated **
449+
472450
This is an encrypted stream.
473451

474452
### cryptoStream.bytesWritten
475453

476454
A proxy to the underlying socket's bytesWritten accessor, this will return
477455
the total bytes written to the socket, *including the TLS overhead*.
478456

479-
## Class: tls.CleartextStream
457+
## Class: tls.TLSSocket
480458

481-
This is a stream on top of the *Encrypted* stream that makes it possible to
482-
read/write an encrypted data as a cleartext data.
459+
This is a wrapped version of [net.Socket][] that does transparent encryption
460+
of written data and all required TLS negotiation.
483461

484462
This instance implements a duplex [Stream][] interfaces. It has all the
485463
common stream methods and events.
486464

487-
A ClearTextStream is the `clear` member of a SecurePair object.
488-
489465
### Event: 'secureConnect'
490466

491-
This event is emitted after a new connection has been successfully handshaked.
467+
This event is emitted after a new connection has been successfully handshaked.
492468
The listener will be called no matter if the server's certificate was
493-
authorized or not. It is up to the user to test `cleartextStream.authorized`
469+
authorized or not. It is up to the user to test `tlsSocket.authorized`
494470
to see if the server certificate was signed by one of the specified CAs.
495-
If `cleartextStream.authorized === false` then the error can be found in
496-
`cleartextStream.authorizationError`. Also if NPN was used - you can check
497-
`cleartextStream.npnProtocol` for negotiated protocol.
471+
If `tlsSocket.authorized === false` then the error can be found in
472+
`tlsSocket.authorizationError`. Also if NPN was used - you can check
473+
`tlsSocket.npnProtocol` for negotiated protocol.
498474

499-
### cleartextStream.authorized
475+
### tlsSocket.authorized
500476

501477
A boolean that is `true` if the peer certificate was signed by one of the
502478
specified CAs, otherwise `false`
503479

504-
### cleartextStream.authorizationError
480+
### tlsSocket.authorizationError
505481

506482
The reason why the peer's certificate has not been verified. This property
507-
becomes available only when `cleartextStream.authorized === false`.
483+
becomes available only when `tlsSocket.authorized === false`.
508484

509-
### cleartextStream.getPeerCertificate()
485+
### tlsSocket.getPeerCertificate()
510486

511487
Returns an object representing the peer's certificate. The returned object has
512488
some properties corresponding to the field of the certificate.
@@ -534,7 +510,7 @@ Example:
534510
If the peer does not provide a certificate, it returns `null` or an empty
535511
object.
536512

537-
### cleartextStream.getCipher()
513+
### tlsSocket.getCipher()
538514
Returns an object representing the cipher name and the SSL/TLS
539515
protocol version of the current connection.
540516

@@ -545,33 +521,33 @@ See SSL_CIPHER_get_name() and SSL_CIPHER_get_version() in
545521
http://www.openssl.org/docs/ssl/ssl.html#DEALING_WITH_CIPHERS for more
546522
information.
547523

548-
### cleartextStream.address()
524+
### tlsSocket.address()
549525

550526
Returns the bound address, the address family name and port of the
551527
underlying socket as reported by the operating system. Returns an
552528
object with three properties, e.g.
553529
`{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`
554530

555-
### cleartextStream.remoteAddress
531+
### tlsSocket.remoteAddress
556532

557533
The string representation of the remote IP address. For example,
558534
`'74.125.127.100'` or `'2001:4860:a005::68'`.
559535

560-
### cleartextStream.remotePort
536+
### tlsSocket.remotePort
561537

562538
The numeric representation of the remote port. For example, `443`.
563539

564-
### cleartextStream.localAddress
540+
### tlsSocket.localAddress
565541

566542
The string representation of the local IP address.
567543

568-
### cleartextStream.localPort
544+
### tlsSocket.localPort
569545

570546
The numeric representation of the local port.
571547

572548
[OpenSSL cipher list format documentation]: http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT
573549
[BEAST attacks]: http://blog.ivanristic.com/2011/10/mitigating-the-beast-attack-on-tls.html
574-
[CleartextStream]: #tls_class_tls_cleartextstream
550+
[tls.TLSSocket]: #tls_class_tls_tlssocket
575551
[net.Server.address()]: net.html#net_server_address
576552
['secureConnect']: #tls_event_secureconnect
577553
[secureConnection]: #tls_event_secureconnection

0 commit comments

Comments
 (0)