On this page

C

DTLSSession

History

Represents a DTLS association with a single remote peer.

session.send(data): number
Attributes
The data to send. At most 16384 bytes. A view sends the bytes it covers, so an offset or a subarray is sent as given rather than as the whole buffer behind it.
Returns:number
The number of bytes written to the DTLS layer.

Send application data to the peer. The data is encrypted by DTLS before being sent over UDP. Can only be called after the handshake completes (session.opened has resolved).

DTLS carries application data in a single record per datagram and does not fragment it, so data must fit in one record. Sending more throws ERR_OUT_OF_RANGE. This limit is independent of the mtu option: a record larger than the path MTU is still sent, and is fragmented by IP.

Throws ERR_INVALID_STATE if the handshake has not completed, or if the session is closed or destroyed.

A successful return means the data was handed to the DTLS layer and written to the socket, not that the peer received it. DTLS runs over UDP, so application data may still be lost in transit.

session.close(): Promise
Returns:Promise
Resolves when the session is closed.

Initiates a graceful DTLS shutdown by sending a close_notify alert.

session.destroy(error?): void

Immediately destroys the session without sending close_notify.

Attributes
True once the session has been destroyed, whether by session.destroy(), by a close, or by its endpoint going away.
Attributes
DTLSEndpoint
The endpoint carrying this session. For a session from dtls.listen() this is the listening endpoint, shared with every other session on it; for one from dtls.connect() it is the endpoint created to carry that session alone.
Attributes
The server name for this session: the name the client sent in the SNI extension, read on either side of the connection. undefined when no name was sent. See Server name indication.
Attributes
Resolves with { protocol } when the DTLS handshake completes.

Rejects if the handshake fails, and also if the session is closed or destroyed before the handshake completes -- in that case with ERR_INVALID_STATE, or with the error passed to session.destroy() if one was given. The promise always settles, so awaiting it cannot hang.

Attributes
Settles when the session is fully closed. Resolves when the close was graceful, and rejects with the error when the session was destroyed with one, or when its endpoint was. The promise always settles, so awaiting it cannot hang.
Returns:Object
{ address, family, port }
Returns:string
The negotiated DTLS protocol version (e.g., 'DTLSv1.2').
Returns:Object
{ name, standardName, version }
Returns:string | undefined
The peer's certificate in PEM format, or undefined if the peer sent none.

This is the leaf certificate as PEM text and nothing else. For the issuer chain and the parsed fields, use session.peerX509Certificate, whose toString() returns this same PEM. Use session.authorized and session.authorizationError for the verification result rather than parsing either.

P

session.peerX509Certificate

History
The peer's certificate, or undefined if the peer sent none.

An X509Certificate for the peer's leaf certificate. The issuer chain is reachable through its issuerCertificate property, and the parsed fields -- subject, issuer, validFrom, validTo, fingerprint256, serialNumber and the rest -- are properties of that object.

Where tls.TLSSocket.getPeerCertificate() returns a plain dictionary with valid_from, valid_to and a chain walked through issuerCertificate, this returns the same X509Certificate class that tls.TLSSocket.getPeerX509Certificate() does. Call toLegacyObject() on it to get the dictionary form.

The same object is returned on every access once the peer's certificate is available.

P

session

History
Returns:Buffer | undefined
An opaque session for resuming this connection later, or undefined on a server session or before the handshake completes.

Pass it as the session option to a later dtls.connect(). It is bound to the host this connection authenticated against and is refused elsewhere; see Session resumption.

Server sessions return undefined: a server has no identity to bind the value to, and it is the client that carries a session between connections.

P

session.reused

History
Returns:boolean
true if this connection resumed an earlier session rather than performing a full handshake.

Like session.authorized, this reads false once the session is closed.

P

session.authorized

History
Returns:boolean
true if the peer presented a certificate chain that verified against the configured certificate authorities, and, for a client, matched the requested identity. false before the handshake completes.
P

session.authorizationError

History
Returns:string | undefined
The short X509 verification error code, for example 'CERT_HAS_EXPIRED' or 'HOSTNAME_MISMATCH', or undefined if the peer's chain verified.

A peer that presented no certificate at all reports 'UNABLE_TO_GET_ISSUER_CERT', so this can be used to distinguish "no certificate" from "a certificate that failed to verify".

The chain is verified even when rejectUnauthorized is false; the result is simply not enforced. That makes these two properties the way to apply a custom authorization policy:

import { connect } from 'node:dtls';

const session = connect('192.0.2.1', 4433, {
  ca: [caCert],
  servername: 'example.com',
  rejectUnauthorized: false,
});

await session.opened;

if (!session.authorized && session.authorizationError !== 'CERT_HAS_EXPIRED') {
  await session.close();
}
Returns:string | undefined
The negotiated ALPN protocol, or undefined if ALPN was not used.

If a server has alpn configured and a client offers only protocols the server does not support, the server sends a fatal no_application_protocol alert and the handshake fails, as required by RFC 7301 section 3.2. A server with no alpn configured declines the extension instead, and the handshake completes with no protocol negotiated.

Returns:string | undefined
The negotiated SRTP protection profile name.
P

session.stats

History

The statistics collected for this session. Read only. The stats object is live and updated as data flows through the session.

session.exportKeyingMaterial(length, label, context?): Buffer
Attributes
length:number
Number of bytes to export. Must be an integer between 1 and 65536.
label:string
The label for the exported keying material.
context:Buffer
Optional context value.
Returns:Buffer

Exports keying material from the DTLS session, as defined in RFC 5705. This is commonly used with DTLS-SRTP to derive encryption keys for media streams.

Throws ERR_OUT_OF_RANGE if length is outside the accepted range. The upper bound is not imposed by RFC 5705; it exists so that a caller cannot request an arbitrarily large allocation, and is far above what any defined exporter needs (DTLS-SRTP uses 60 bytes).

Attributes

Set to receive application data from the peer.

Attributes

Set to receive error notifications.

Attributes

Set to receive handshake completion notifications.

Attributes

Set to receive TLS key log lines (for debugging with Wireshark).

session[Symbol.asyncDispose](): void

Equivalent to calling session.close().