From 4ca0e4c949307d7e93e797b1a2cb4c54e6af0dc5 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Fri, 26 Oct 2018 15:46:07 -0700 Subject: [PATCH] tls: deprecate undocumented .ssl property, add docs deprecate the legacy undocumented `.ssl` alias for the `TLSSocket._handle` and document alternatives. Document how to properly use the `TLSSocket` constructor directly. Updated take on https://github.com/nodejs/node/pull/10846 Fixes: https://github.com/nodejs/node/issues/10555 --- doc/api/deprecations.md | 10 +++ doc/api/tls.md | 73 ++++++++++++++++++- lib/_tls_wrap.js | 35 ++++++--- .../test-tls-socket-default-options.js | 2 +- .../test-tls-socket-ssl-deprecated.js | 31 ++++++++ test/parallel/test-tls-tlswrap-segfault.js | 4 +- 6 files changed, 141 insertions(+), 14 deletions(-) create mode 100644 test/parallel/test-tls-socket-ssl-deprecated.js diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 681fac92deae99..7ea3139b181d9b 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -2292,6 +2292,16 @@ Type: Runtime Please use `Server.prototype.setSecureContext()` instead. + +### DEP00XX: TLSSocket.prototype.ssl + + +The undocumented `tlsSocket.ssl` property has been deprecated. [`--pending-deprecation`]: cli.html#cli_pending_deprecation [`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size diff --git a/doc/api/tls.md b/doc/api/tls.md index 8700839c400b38..c318d753661834 100644 --- a/doc/api/tls.md +++ b/doc/api/tls.md @@ -465,7 +465,8 @@ changes: * `socket` {net.Socket|stream.Duplex} On the server side, any `Duplex` stream. On the client side, any instance of [`net.Socket`][] (for generic `Duplex` stream support - on the client side, [`tls.connect()`][] must be used). + on the client side, [`tls.connect()`][] must be used). If the `socket` + is not provided, a new unconnected TCP socket or named pipe will be created. * `options` {Object} * `isServer`: The SSL/TLS protocol is asymmetrical, TLSSockets must know if they are to behave as a server or a client. If `true` the TLS socket will be @@ -489,7 +490,24 @@ changes: * ...: [`tls.createSecureContext()`][] options that are used if the `secureContext` option is missing. Otherwise, they are ignored. -Construct a new `tls.TLSSocket` object from an existing TCP socket. +Construct a new `tls.TLSSocket` object from an existing `net.Socket` instance. +Using the `tls.connect()` method is preferred when creating a new TLS session +on top of a new `net.Socket` instance. + +Using the `tls.TLSSocket()` constructor directly is helpful when implementing +protocols that can start off insecure (such as SMTP), then initiating a secure +session after the socket is connected. It is important to remember, however, +that it is the caller's responsibility to manage the lifecycle of the provided +`net.Socket`, including establishing the connection and validating peer +certificates and identity. See the [`'secure'`][] event. + +### Event: 'connect' + + +The `'connect'` event is emitted once the underlying `net.Socket` has been +connected. ### Event: 'OCSPResponse' + +The `'secure'` event is emitted after the TLS handshake has been completed. + +Before using the connection, the user must verify that the peer certificate +is valid (see [`tls.TLSSocket.verifyError()`][]) and that the peer certificate +is for the expected host (see [`tls.checkServerIdentity()`][] and +[`tls.TLSSocket.getPeerCertificate()`][]). If these checks are not performed, +the connection should be considered insecure. When using the `tls.connect()` +method to create a new `tls.TLSSocket`, these checks are performed +automatically. + +```js +tlsSocket.on('secure', function() { + const err = this.verifyError() || + tls.checkServerIdentity(hostname, this.getPeerCertificate()); + if (err) + this.destroy(err); +}); +``` + ### Event: 'secureConnect' + +* Returns: {Error} object if the peer's certificate fails validation. + +Validation contains many checks, including verification that the certificate +is either trusted or can be chained to a trusted certificate authority +(see the `ca` option of [`tls.createSecureContext()`][] for more information). + +Validation explicitly does *not* include any authentication of the identity. +The [`tls.checkServerIdentity()`][] method can be used to authenticate the +identity of the peer. + ## tls.checkServerIdentity(hostname, cert)