@@ -465,7 +465,8 @@ changes:
465
465
* ` socket ` {net.Socket|stream.Duplex}
466
466
On the server side, any ` Duplex ` stream. On the client side, any
467
467
instance of [ ` net.Socket ` ] [ ] (for generic ` Duplex ` stream support
468
- on the client side, [ ` tls.connect() ` ] [ ] must be used).
468
+ on the client side, [ ` tls.connect() ` ] [ ] must be used). If the ` socket `
469
+ is not provided, a new unconnected TCP socket or named pipe will be created.
469
470
* ` options ` {Object}
470
471
* ` isServer ` : The SSL/TLS protocol is asymmetrical, TLSSockets must know if
471
472
they are to behave as a server or a client. If ` true ` the TLS socket will be
@@ -489,7 +490,24 @@ changes:
489
490
* ...: [ ` tls.createSecureContext() ` ] [ ] options that are used if the
490
491
` secureContext ` option is missing. Otherwise, they are ignored.
491
492
492
- Construct a new ` tls.TLSSocket ` object from an existing TCP socket.
493
+ Construct a new ` tls.TLSSocket ` object from an existing ` net.Socket ` instance.
494
+ Using the ` tls.connect() ` method is preferred when creating a new TLS session
495
+ on top of a new ` net.Socket ` instance.
496
+
497
+ Using the ` tls.TLSSocket() ` constructor directly is helpful when implementing
498
+ protocols that can start off insecure (such as SMTP), then initiating a secure
499
+ session after the socket is connected. It is important to remember, however,
500
+ that it is the caller's responsibility to manage the lifecycle of the provided
501
+ ` net.Socket ` , including establishing the connection and validating peer
502
+ certificates and identity. See the [ ` 'secure' ` ] [ ] event.
503
+
504
+ ### Event: 'connect'
505
+ <!-- YAML
506
+ added: v0.11.4
507
+ -->
508
+
509
+ The ` 'connect' ` event is emitted once the underlying ` net.Socket ` has been
510
+ connected.
493
511
494
512
### Event: 'OCSPResponse'
495
513
<!-- YAML
@@ -505,6 +523,30 @@ The listener callback is passed a single argument when called:
505
523
Typically, the ` response ` is a digitally signed object from the server's CA that
506
524
contains information about server's certificate revocation status.
507
525
526
+ ### Event: 'secure'
527
+ <!-- YAML
528
+ added: v0.11.3
529
+ -->
530
+
531
+ The ` 'secure' ` event is emitted after the TLS handshake has been completed.
532
+
533
+ Before using the connection, the user must verify that the peer certificate
534
+ is valid (see [ ` tls.TLSSocket.verifyError() ` ] [ ] ) and that the peer certificate
535
+ is for the expected host (see [ ` tls.checkServerIdentity() ` ] [ ] and
536
+ [ ` tls.TLSSocket.getPeerCertificate() ` ] [ ] ). If these checks are not performed,
537
+ the connection should be considered insecure. When using the ` tls.connect() `
538
+ method to create a new ` tls.TLSSocket ` , these checks are performed
539
+ automatically.
540
+
541
+ ``` js
542
+ tlsSocket .on (' secure' , function () {
543
+ const err = this .verifyError () ||
544
+ tls .checkServerIdentity (hostname, this .getPeerCertificate ());
545
+ if (err)
546
+ this .destroy (err);
547
+ });
548
+ ```
549
+
508
550
### Event: 'secureConnect'
509
551
<!-- YAML
510
552
added: v0.11.4
@@ -520,6 +562,9 @@ determine if the server certificate was signed by one of the specified CAs. If
520
562
` tlsSocket.alpnProtocol ` property can be checked to determine the negotiated
521
563
protocol.
522
564
565
+ The ` 'secureConnect' ` event is only emitted on ` tls.TLSSocket ` connections
566
+ created using the ` tls.connect() ` method.
567
+
523
568
### tlsSocket.address()
524
569
<!-- YAML
525
570
added: v0.11.4
@@ -539,6 +584,9 @@ added: v0.11.4
539
584
Returns the reason why the peer's certificate was not been verified. This
540
585
property is set only when ` tlsSocket.authorized === false ` .
541
586
587
+ The ` tlsSocket.authorizationError ` property is only set for ` tls.TLSSocket `
588
+ instances created using the ` tls.connect() ` method.
589
+
542
590
### tlsSocket.authorized
543
591
<!-- YAML
544
592
added: v0.11.4
@@ -549,6 +597,9 @@ added: v0.11.4
549
597
Returns ` true ` if the peer certificate was signed by one of the CAs specified
550
598
when creating the ` tls.TLSSocket ` instance, otherwise ` false ` .
551
599
600
+ The ` tlsSocket.authorized ` property is only set for ` tls.TLSSocket ` instances
601
+ created using the ` tls.connect() ` method.
602
+
552
603
### tlsSocket.disableRenegotiation()
553
604
<!-- YAML
554
605
added: v8.4.0
@@ -805,6 +856,21 @@ and their processing can be delayed due to packet loss or reordering. However,
805
856
smaller fragments add extra TLS framing bytes and CPU overhead, which may
806
857
decrease overall server throughput.
807
858
859
+ ### tlsSocket.verifyError()
860
+ <!-- YAML
861
+ added: v0.11.3
862
+ -->
863
+
864
+ * Returns: {Error} object if the peer's certificate fails validation.
865
+
866
+ Validation contains many checks, including verification that the certificate
867
+ is either trusted or can be chained to a trusted certificate authority
868
+ (see the ` ca ` option of [ ` tls.createSecureContext() ` ] [ ] for more information).
869
+
870
+ Validation explicitly does * not* include any authentication of the identity.
871
+ The [ ` tls.checkServerIdentity() ` ] [ ] method can be used to authenticate the
872
+ identity of the peer.
873
+
808
874
## tls.checkServerIdentity(hostname, cert)
809
875
<!-- YAML
810
876
added: v0.8.4
@@ -1389,6 +1455,7 @@ secureSocket = tls.TLSSocket(socket, options);
1389
1455
1390
1456
where ` secureSocket ` has the same API as ` pair.cleartext ` .
1391
1457
1458
+ [ `'secure'` ] : #tls_event_secure
1392
1459
[ `'secureConnect'` ] : #tls_event_secureconnect
1393
1460
[ `'secureConnection'` ] : #tls_event_secureconnection
1394
1461
[ `SSL_CTX_set_timeout` ] : https://www.openssl.org/docs/man1.1.0/ssl/SSL_CTX_set_timeout.html
@@ -1403,6 +1470,8 @@ where `secureSocket` has the same API as `pair.cleartext`.
1403
1470
[ `tls.Server` ] : #tls_class_tls_server
1404
1471
[ `tls.TLSSocket.getPeerCertificate()` ] : #tls_tlssocket_getpeercertificate_detailed
1405
1472
[ `tls.TLSSocket` ] : #tls_class_tls_tlssocket
1473
+ [ `tls.TLSSocket.verifyError()` ] : #tls_tlssocket_verifyerror
1474
+ [ `tls.checkServerIdentity()` ] : #tls_tls_checkserveridentity_hostname_cert
1406
1475
[ `tls.connect()` ] : #tls_tls_connect_options_callback
1407
1476
[ `tls.createSecureContext()` ] : #tls_tls_createsecurecontext_options
1408
1477
[ `tls.createSecurePair()` ] : #tls_tls_createsecurepair_context_isserver_requestcert_rejectunauthorized_options
0 commit comments