class tls.Server extends net.Server
Accepts encrypted connections using TLS or SSL.
stream.DuplexThis event is emitted when a new TCP stream is established, before the TLS
handshake begins. socket is typically an object of type net.Socket but
will not receive events unlike the socket created from the net.Server
'connection' event. Usually users will not want to access this event.
This event can also be explicitly emitted by users to inject connections
into the TLS server. In that case, any Duplex stream can be passed.
BufferSSLKEYLOGFILE format.tls.TLSSockettls.TLSSocket instance on which it was
generated.The keylog event is emitted when key material is generated or received by
a connection to this server (typically before handshake has completed, but not
necessarily). This keying material can be stored for debugging, as it allows
captured TLS traffic to be decrypted. It may be emitted multiple times for
each socket.
A typical use case is to append received lines to a common text file, which is later used by software (such as Wireshark) to decrypt the traffic:
const logFile = fs.createWriteStream('/tmp/ssl-keys.log', { flags: 'a' }); // ... server.on('keylog', (line, tlsSocket) => { if (tlsSocket.remoteAddress !== '...') return; // Only log keys for a particular IP logFile.write(line); });
The 'newSession' event is emitted upon creation of a new TLS session. This may
be used to store sessions in external storage. The data should be provided to
the 'resumeSession' callback.
The listener callback is passed three arguments when called:
Listening for this event will have an effect only on connections established after the addition of the event listener.
The 'OCSPRequest' event is emitted when the client sends a certificate status
request. The listener callback is passed three arguments when called:
The server's current certificate can be parsed to obtain the OCSP URL
and certificate ID; after obtaining an OCSP response, callback(null, resp) is
then invoked, where resp is a Buffer instance containing the OCSP response.
Both certificate and issuer are Buffer DER-representations of the
primary and issuer's certificates. These can be used to obtain the OCSP
certificate ID and OCSP endpoint URL.
Alternatively, callback(null, null) may be called, indicating that there was
no OCSP response.
Calling callback(err) will result in a socket.destroy(err) call.
The typical flow of an OCSP request is as follows:
- Client connects to the server and sends an
'OCSPRequest'(via the status info extension in ClientHello). - Server receives the request and emits the
'OCSPRequest'event, calling the listener if registered. - Server extracts the OCSP URL from either the
certificateorissuerand performs an OCSP request to the CA. - Server receives
'OCSPResponse'from the CA and sends it back to the client via thecallbackargument - Client validates the response and either destroys the socket or performs a handshake.
The issuer can be null if the certificate is either self-signed or the
issuer is not in the root certificates list. (An issuer may be provided
via the ca option when establishing the TLS connection.)
Listening for this event will have an effect only on connections established after the addition of the event listener.
An npm module like asn1.js may be used to parse the certificates.
The 'resumeSession' event is emitted when the client requests to resume a
previous TLS session. The listener callback is passed two arguments when
called:
The event listener should perform a lookup in external storage for the
sessionData saved by the 'newSession' event handler using the given
sessionId. If found, call callback(null, sessionData) to resume the session.
If not found, the session cannot be resumed. callback() must be called
without sessionData so that the handshake can continue and a new session can
be created. It is possible to call callback(err) to terminate the incoming
connection and destroy the socket.
Listening for this event will have an effect only on connections established after the addition of the event listener.
The following illustrates resuming a TLS session:
const tlsSessionStore = {}; server.on('newSession', (id, data, cb) => { tlsSessionStore[id.toString('hex')] = data; cb(); }); server.on('resumeSession', (id, cb) => { cb(null, tlsSessionStore[id.toString('hex')] || null); });
The 'secureConnection' event is emitted after the handshaking process for a
new connection has successfully completed. The listener callback is passed a
single argument when called:
tls.TLSSocketThe tlsSocket.authorized property is a boolean indicating whether the
client has been verified by one of the supplied Certificate Authorities for the
server. If tlsSocket.authorized is false, then socket.authorizationError
is set to describe how authorization failed. Depending on the settings
of the TLS server, unauthorized connections may still be accepted.
The tls.TLSSocket.servername and tls.TLSSocket.alpnProtocol
properties can be used to check which server name was requested, and which
protocol was negotiated.
The 'tlsClientError' event is emitted when an error occurs before a secure
connection is established. The listener callback is passed two arguments when
called:
ErrorError object describing the errortls.TLSSockettls.TLSSocket instance from which the
error originated.server.addContext(hostname, context): void
string'*')Object | tls.SecureContexttls.createSecureContext() options arguments
(e.g. key, cert, ca, etc), or a TLS context object created with
tls.createSecureContext() itself.The server.addContext() method adds a secure context that will be used if
the client request's SNI name matches the supplied hostname (or wildcard).
When there are multiple matching contexts, the most recently added one is used.
server.address(): Object
ObjectReturns the bound address, the address family name, and port of the
server as reported by the operating system. See net.Server.address() for
more information.
server.close(callback?): tls.Server
Function'close' event.tls.ServerThe server.close() method stops the server from accepting new connections.
This function operates asynchronously. The 'close' event will be emitted
when the server has no more open connections.
server.getTicketKeys(): Buffer
BufferReturns the session ticket keys.
See Session Resumption for more information.
server.listen(): void
Starts the server listening for encrypted connections.
This method is identical to server.listen() from net.Server.
server.setSecureContext(options): void
Objecttls.createSecureContext() options arguments (e.g. key, cert,
ca, etc).The server.setSecureContext() method replaces the secure context of an
existing server. Existing connections to the server are not interrupted.
server.setTicketKeys(keys): void
Buffer | TypedArray | DataViewSets the session ticket keys.
Changes to the ticket keys are effective only for future server connections. Existing or currently pending server connections will use the previous keys.
See Session Resumption for more information.