• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <meta name="viewport" content="width=device-width">
6  <meta name="nodejs.org:node-version" content="v14.21.2">
7  <title>TLS (SSL) | Node.js v14.21.2 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/tls.html">
12</head>
13<body class="alt apidoc" id="api-section-tls">
14  <div id="content" class="clearfix">
15    <div id="column2" class="interior">
16      <div id="intro" class="interior">
17        <a href="/" title="Go back to the home page">
18          Node.js
19        </a>
20      </div>
21      <ul>
22<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
23<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
24</ul>
25<hr class="line">
26<ul>
27<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
28<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
29<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
30<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
31<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
32<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
33<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
34<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
35<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
36<li><a href="console.html" class="nav-console">Console</a></li>
37<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
38<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
39<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
40<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
41<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
42<li><a href="dns.html" class="nav-dns">DNS</a></li>
43<li><a href="domain.html" class="nav-domain">Domain</a></li>
44<li><a href="errors.html" class="nav-errors">Errors</a></li>
45<li><a href="events.html" class="nav-events">Events</a></li>
46<li><a href="fs.html" class="nav-fs">File system</a></li>
47<li><a href="globals.html" class="nav-globals">Globals</a></li>
48<li><a href="http.html" class="nav-http">HTTP</a></li>
49<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
50<li><a href="https.html" class="nav-https">HTTPS</a></li>
51<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
52<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
53<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
54<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
55<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li>
56<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
57<li><a href="net.html" class="nav-net">Net</a></li>
58<li><a href="os.html" class="nav-os">OS</a></li>
59<li><a href="path.html" class="nav-path">Path</a></li>
60<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
61<li><a href="policy.html" class="nav-policy">Policies</a></li>
62<li><a href="process.html" class="nav-process">Process</a></li>
63<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
64<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
65<li><a href="readline.html" class="nav-readline">Readline</a></li>
66<li><a href="repl.html" class="nav-repl">REPL</a></li>
67<li><a href="report.html" class="nav-report">Report</a></li>
68<li><a href="stream.html" class="nav-stream">Stream</a></li>
69<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
70<li><a href="timers.html" class="nav-timers">Timers</a></li>
71<li><a href="tls.html" class="nav-tls active">TLS/SSL</a></li>
72<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
73<li><a href="tty.html" class="nav-tty">TTY</a></li>
74<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
75<li><a href="url.html" class="nav-url">URL</a></li>
76<li><a href="util.html" class="nav-util">Utilities</a></li>
77<li><a href="v8.html" class="nav-v8">V8</a></li>
78<li><a href="vm.html" class="nav-vm">VM</a></li>
79<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
80<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
81<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
82</ul>
83<hr class="line">
84<ul>
85<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
86</ul>
87    </div>
88
89    <div id="column1" data-id="tls" class="interior">
90      <header>
91        <div class="header-container">
92          <h1>Node.js v14.21.2 documentation</h1>
93          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
94            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
95              <path fill="none" d="M0 0h24v24H0z" />
96              <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
97              <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
98            </svg>
99            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
100              <path d="M0 0h24v24H0z" fill="none" />
101              <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
102            </svg>
103          </button>
104        </div>
105        <div id="gtoc">
106          <ul>
107            <li>
108              <a href="index.html">Index</a>
109            </li>
110            <li>
111              <a href="all.html">View on single page</a>
112            </li>
113            <li>
114              <a href="tls.json">View as JSON</a>
115            </li>
116
117    <li class="version-picker">
118      <a href="#">View another version <span>&#x25bc;</span></a>
119      <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v19.x/api/tls.html">19.x</a></li>
120<li><a href="https://nodejs.org/docs/latest-v18.x/api/tls.html">18.x <b>LTS</b></a></li>
121<li><a href="https://nodejs.org/docs/latest-v17.x/api/tls.html">17.x</a></li>
122<li><a href="https://nodejs.org/docs/latest-v16.x/api/tls.html">16.x <b>LTS</b></a></li>
123<li><a href="https://nodejs.org/docs/latest-v15.x/api/tls.html">15.x</a></li>
124<li><a href="https://nodejs.org/docs/latest-v14.x/api/tls.html">14.x <b>LTS</b></a></li>
125<li><a href="https://nodejs.org/docs/latest-v13.x/api/tls.html">13.x</a></li>
126<li><a href="https://nodejs.org/docs/latest-v12.x/api/tls.html">12.x</a></li>
127<li><a href="https://nodejs.org/docs/latest-v11.x/api/tls.html">11.x</a></li>
128<li><a href="https://nodejs.org/docs/latest-v10.x/api/tls.html">10.x</a></li>
129<li><a href="https://nodejs.org/docs/latest-v9.x/api/tls.html">9.x</a></li>
130<li><a href="https://nodejs.org/docs/latest-v8.x/api/tls.html">8.x</a></li>
131<li><a href="https://nodejs.org/docs/latest-v7.x/api/tls.html">7.x</a></li>
132<li><a href="https://nodejs.org/docs/latest-v6.x/api/tls.html">6.x</a></li>
133<li><a href="https://nodejs.org/docs/latest-v5.x/api/tls.html">5.x</a></li>
134<li><a href="https://nodejs.org/docs/latest-v4.x/api/tls.html">4.x</a></li>
135<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/tls.html">0.12.x</a></li>
136<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/tls.html">0.10.x</a></li></ol>
137    </li>
138
139            <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/tls.md">Edit on GitHub</a></li>
140          </ul>
141        </div>
142        <hr>
143      </header>
144
145      <details id="toc" open><summary>Table of contents</summary><ul>
146<li><span class="stability_2"><a href="#tls_tls_ssl">TLS (SSL)</a></span>
147<ul>
148<li><a href="#tls_tls_ssl_concepts">TLS/SSL concepts</a>
149<ul>
150<li><a href="#tls_perfect_forward_secrecy">Perfect forward secrecy</a></li>
151<li><a href="#tls_alpn_and_sni">ALPN and SNI</a></li>
152<li><a href="#tls_pre_shared_keys">Pre-shared keys</a></li>
153<li><a href="#tls_client_initiated_renegotiation_attack_mitigation">Client-initiated renegotiation attack mitigation</a></li>
154<li><a href="#tls_session_resumption">Session resumption</a>
155<ul>
156<li><a href="#tls_session_identifiers">Session identifiers</a></li>
157<li><a href="#tls_session_tickets">Session tickets</a></li>
158</ul>
159</li>
160</ul>
161</li>
162<li><a href="#tls_modifying_the_default_tls_cipher_suite">Modifying the default TLS cipher suite</a></li>
163<li><span class="stability_0"><a href="#tls_class_tls_cryptostream">Class: <code>tls.CryptoStream</code></a></span>
164<ul>
165<li><a href="#tls_cryptostream_byteswritten"><code>cryptoStream.bytesWritten</code></a></li>
166</ul>
167</li>
168<li><span class="stability_0"><a href="#tls_class_tls_securepair">Class: <code>tls.SecurePair</code></a></span>
169<ul>
170<li><a href="#tls_event_secure">Event: <code>'secure'</code></a></li>
171</ul>
172</li>
173<li><a href="#tls_class_tls_server">Class: <code>tls.Server</code></a>
174<ul>
175<li><a href="#tls_event_connection">Event: <code>'connection'</code></a></li>
176<li><a href="#tls_event_keylog">Event: <code>'keylog'</code></a></li>
177<li><a href="#tls_event_newsession">Event: <code>'newSession'</code></a></li>
178<li><a href="#tls_event_ocsprequest">Event: <code>'OCSPRequest'</code></a></li>
179<li><a href="#tls_event_resumesession">Event: <code>'resumeSession'</code></a></li>
180<li><a href="#tls_event_secureconnection">Event: <code>'secureConnection'</code></a></li>
181<li><a href="#tls_event_tlsclienterror">Event: <code>'tlsClientError'</code></a></li>
182<li><a href="#tls_server_addcontext_hostname_context"><code>server.addContext(hostname, context)</code></a></li>
183<li><a href="#tls_server_address"><code>server.address()</code></a></li>
184<li><a href="#tls_server_close_callback"><code>server.close([callback])</code></a></li>
185<li><span class="stability_0"><a href="#tls_server_connections"><code>server.connections</code></a></span></li>
186<li><a href="#tls_server_getticketkeys"><code>server.getTicketKeys()</code></a></li>
187<li><a href="#tls_server_listen"><code>server.listen()</code></a></li>
188<li><a href="#tls_server_setsecurecontext_options"><code>server.setSecureContext(options)</code></a></li>
189<li><a href="#tls_server_setticketkeys_keys"><code>server.setTicketKeys(keys)</code></a></li>
190</ul>
191</li>
192<li><a href="#tls_class_tls_tlssocket">Class: <code>tls.TLSSocket</code></a>
193<ul>
194<li><a href="#tls_new_tls_tlssocket_socket_options"><code>new tls.TLSSocket(socket[, options])</code></a></li>
195<li><a href="#tls_event_keylog_1">Event: <code>'keylog'</code></a></li>
196<li><a href="#tls_event_ocspresponse">Event: <code>'OCSPResponse'</code></a></li>
197<li><a href="#tls_event_secureconnect">Event: <code>'secureConnect'</code></a></li>
198<li><a href="#tls_event_session">Event: <code>'session'</code></a></li>
199<li><a href="#tls_tlssocket_address"><code>tlsSocket.address()</code></a></li>
200<li><a href="#tls_tlssocket_authorizationerror"><code>tlsSocket.authorizationError</code></a></li>
201<li><a href="#tls_tlssocket_authorized"><code>tlsSocket.authorized</code></a></li>
202<li><a href="#tls_tlssocket_disablerenegotiation"><code>tlsSocket.disableRenegotiation()</code></a></li>
203<li><a href="#tls_tlssocket_enabletrace"><code>tlsSocket.enableTrace()</code></a></li>
204<li><a href="#tls_tlssocket_encrypted"><code>tlsSocket.encrypted</code></a></li>
205<li><a href="#tls_tlssocket_getcertificate"><code>tlsSocket.getCertificate()</code></a></li>
206<li><a href="#tls_tlssocket_getcipher"><code>tlsSocket.getCipher()</code></a></li>
207<li><a href="#tls_tlssocket_getephemeralkeyinfo"><code>tlsSocket.getEphemeralKeyInfo()</code></a></li>
208<li><a href="#tls_tlssocket_getfinished"><code>tlsSocket.getFinished()</code></a></li>
209<li><a href="#tls_tlssocket_getpeercertificate_detailed"><code>tlsSocket.getPeerCertificate([detailed])</code></a>
210<ul>
211<li><a href="#tls_certificate_object">Certificate object</a></li>
212</ul>
213</li>
214<li><a href="#tls_tlssocket_getpeerfinished"><code>tlsSocket.getPeerFinished()</code></a></li>
215<li><a href="#tls_tlssocket_getprotocol"><code>tlsSocket.getProtocol()</code></a></li>
216<li><a href="#tls_tlssocket_getsession"><code>tlsSocket.getSession()</code></a></li>
217<li><a href="#tls_tlssocket_getsharedsigalgs"><code>tlsSocket.getSharedSigalgs()</code></a></li>
218<li><a href="#tls_tlssocket_exportkeyingmaterial_length_label_context"><code>tlsSocket.exportKeyingMaterial(length, label[, context])</code></a></li>
219<li><a href="#tls_tlssocket_gettlsticket"><code>tlsSocket.getTLSTicket()</code></a></li>
220<li><a href="#tls_tlssocket_issessionreused"><code>tlsSocket.isSessionReused()</code></a></li>
221<li><a href="#tls_tlssocket_localaddress"><code>tlsSocket.localAddress</code></a></li>
222<li><a href="#tls_tlssocket_localport"><code>tlsSocket.localPort</code></a></li>
223<li><a href="#tls_tlssocket_remoteaddress"><code>tlsSocket.remoteAddress</code></a></li>
224<li><a href="#tls_tlssocket_remotefamily"><code>tlsSocket.remoteFamily</code></a></li>
225<li><a href="#tls_tlssocket_remoteport"><code>tlsSocket.remotePort</code></a></li>
226<li><a href="#tls_tlssocket_renegotiate_options_callback"><code>tlsSocket.renegotiate(options, callback)</code></a></li>
227<li><a href="#tls_tlssocket_setmaxsendfragment_size"><code>tlsSocket.setMaxSendFragment(size)</code></a></li>
228</ul>
229</li>
230<li><a href="#tls_tls_checkserveridentity_hostname_cert"><code>tls.checkServerIdentity(hostname, cert)</code></a></li>
231<li><a href="#tls_tls_connect_options_callback"><code>tls.connect(options[, callback])</code></a></li>
232<li><a href="#tls_tls_connect_path_options_callback"><code>tls.connect(path[, options][, callback])</code></a></li>
233<li><a href="#tls_tls_connect_port_host_options_callback"><code>tls.connect(port[, host][, options][, callback])</code></a></li>
234<li><a href="#tls_tls_createsecurecontext_options"><code>tls.createSecureContext([options])</code></a></li>
235<li><span class="stability_0"><a href="#tls_tls_createsecurepair_context_isserver_requestcert_rejectunauthorized_options"><code>tls.createSecurePair([context][, isServer][, requestCert][, rejectUnauthorized][, options])</code></a></span></li>
236<li><a href="#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer([options][, secureConnectionListener])</code></a></li>
237<li><a href="#tls_tls_getciphers"><code>tls.getCiphers()</code></a></li>
238<li><a href="#tls_tls_rootcertificates"><code>tls.rootCertificates</code></a></li>
239<li><a href="#tls_tls_default_ecdh_curve"><code>tls.DEFAULT_ECDH_CURVE</code></a></li>
240<li><a href="#tls_tls_default_max_version"><code>tls.DEFAULT_MAX_VERSION</code></a></li>
241<li><a href="#tls_tls_default_min_version"><code>tls.DEFAULT_MIN_VERSION</code></a></li>
242</ul>
243</li>
244</ul></details>
245
246      <div id="apicontent">
247        <h2>TLS (SSL)<span><a class="mark" href="#tls_tls_ssl" id="tls_tls_ssl">#</a></span></h2>
248
249<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
250<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.21.2/lib/tls.js">lib/tls.js</a></p>
251<p>The <code>tls</code> module provides an implementation of the Transport Layer Security
252(TLS) and Secure Socket Layer (SSL) protocols that is built on top of OpenSSL.
253The module can be accessed using:</p>
254<pre><code class="language-js"><span class="hljs-keyword">const</span> tls = <span class="hljs-built_in">require</span>(<span class="hljs-string">'tls'</span>);</code></pre>
255<section><h3>TLS/SSL concepts<span><a class="mark" href="#tls_tls_ssl_concepts" id="tls_tls_ssl_concepts">#</a></span></h3>
256<p>The TLS/SSL is a public/private key infrastructure (PKI). For most common
257cases, each client and server must have a <em>private key</em>.</p>
258<p>Private keys can be generated in multiple ways. The example below illustrates
259use of the OpenSSL command-line interface to generate a 2048-bit RSA private
260key:</p>
261<pre><code class="language-bash">openssl genrsa -out ryans-key.pem 2048</code></pre>
262<p>With TLS/SSL, all servers (and some clients) must have a <em>certificate</em>.
263Certificates are <em>public keys</em> that correspond to a private key, and that are
264digitally signed either by a Certificate Authority or by the owner of the
265private key (such certificates are referred to as "self-signed"). The first
266step to obtaining a certificate is to create a <em>Certificate Signing Request</em>
267(CSR) file.</p>
268<p>The OpenSSL command-line interface can be used to generate a CSR for a private
269key:</p>
270<pre><code class="language-bash">openssl req -new -sha256 -key ryans-key.pem -out ryans-csr.pem</code></pre>
271<p>Once the CSR file is generated, it can either be sent to a Certificate
272Authority for signing or used to generate a self-signed certificate.</p>
273<p>Creating a self-signed certificate using the OpenSSL command-line interface
274is illustrated in the example below:</p>
275<pre><code class="language-bash">openssl x509 -req -<span class="hljs-keyword">in</span> ryans-csr.pem -signkey ryans-key.pem -out ryans-cert.pem</code></pre>
276<p>Once the certificate is generated, it can be used to generate a <code>.pfx</code> or
277<code>.p12</code> file:</p>
278<pre><code class="language-bash">openssl pkcs12 -<span class="hljs-built_in">export</span> -<span class="hljs-keyword">in</span> ryans-cert.pem -inkey ryans-key.pem \
279      -certfile ca-cert.pem -out ryans.pfx</code></pre>
280<p>Where:</p>
281<ul>
282<li><code>in</code>: is the signed certificate</li>
283<li><code>inkey</code>: is the associated private key</li>
284<li><code>certfile</code>: is a concatenation of all Certificate Authority (CA) certs into
285a single file, e.g. <code>cat ca1-cert.pem ca2-cert.pem > ca-cert.pem</code></li>
286</ul>
287<h4>Perfect forward secrecy<span><a class="mark" href="#tls_perfect_forward_secrecy" id="tls_perfect_forward_secrecy">#</a></span></h4>
288
289<p>The term <em><a href="https://en.wikipedia.org/wiki/Perfect_forward_secrecy">forward secrecy</a></em> or <em>perfect forward secrecy</em> describes a feature
290of key-agreement (i.e., key-exchange) methods. That is, the server and client
291keys are used to negotiate new temporary keys that are used specifically and
292only for the current communication session. Practically, this means that even
293if the server's private key is compromised, communication can only be decrypted
294by eavesdroppers if the attacker manages to obtain the key-pair specifically
295generated for the session.</p>
296<p>Perfect forward secrecy is achieved by randomly generating a key pair for
297key-agreement on every TLS/SSL handshake (in contrast to using the same key for
298all sessions). Methods implementing this technique are called "ephemeral".</p>
299<p>Currently two methods are commonly used to achieve perfect forward secrecy (note
300the character "E" appended to the traditional abbreviations):</p>
301<ul>
302<li><a href="https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange">DHE</a>: An ephemeral version of the Diffie-Hellman key-agreement protocol.</li>
303<li><a href="https://en.wikipedia.org/wiki/Elliptic_curve_Diffie%E2%80%93Hellman">ECDHE</a>: An ephemeral version of the Elliptic Curve Diffie-Hellman
304key-agreement protocol.</li>
305</ul>
306<p>Ephemeral methods may have some performance drawbacks, because key generation
307is expensive.</p>
308<p>To use perfect forward secrecy using <code>DHE</code> with the <code>tls</code> module, it is required
309to generate Diffie-Hellman parameters and specify them with the <code>dhparam</code>
310option to <a href="#tls_tls_createsecurecontext_options"><code>tls.createSecureContext()</code></a>. The following illustrates the use of
311the OpenSSL command-line interface to generate such parameters:</p>
312<pre><code class="language-bash">openssl dhparam -outform PEM -out dhparam.pem 2048</code></pre>
313<p>If using perfect forward secrecy using <code>ECDHE</code>, Diffie-Hellman parameters are
314not required and a default ECDHE curve will be used. The <code>ecdhCurve</code> property
315can be used when creating a TLS Server to specify the list of names of supported
316curves to use, see <a href="#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer()</code></a> for more info.</p>
317<p>Perfect forward secrecy was optional up to TLSv1.2, but it is not optional for
318TLSv1.3, because all TLSv1.3 cipher suites use ECDHE.</p>
319<h4>ALPN and SNI<span><a class="mark" href="#tls_alpn_and_sni" id="tls_alpn_and_sni">#</a></span></h4>
320
321<p>ALPN (Application-Layer Protocol Negotiation Extension) and
322SNI (Server Name Indication) are TLS handshake extensions:</p>
323<ul>
324<li>ALPN: Allows the use of one TLS server for multiple protocols (HTTP, HTTP/2)</li>
325<li>SNI: Allows the use of one TLS server for multiple hostnames with different
326SSL certificates.</li>
327</ul>
328<h4>Pre-shared keys<span><a class="mark" href="#tls_pre_shared_keys" id="tls_pre_shared_keys">#</a></span></h4>
329
330<p>TLS-PSK support is available as an alternative to normal certificate-based
331authentication. It uses a pre-shared key instead of certificates to
332authenticate a TLS connection, providing mutual authentication.
333TLS-PSK and public key infrastructure are not mutually exclusive. Clients and
334servers can accommodate both, choosing either of them during the normal cipher
335negotiation step.</p>
336<p>TLS-PSK is only a good choice where means exist to securely share a
337key with every connecting machine, so it does not replace PKI
338(Public Key Infrastructure) for the majority of TLS uses.
339The TLS-PSK implementation in OpenSSL has seen many security flaws in
340recent years, mostly because it is used only by a minority of applications.
341Please consider all alternative solutions before switching to PSK ciphers.
342Upon generating PSK it is of critical importance to use sufficient entropy as
343discussed in <a href="https://tools.ietf.org/html/rfc4086">RFC 4086</a>. Deriving a shared secret from a password or other
344low-entropy sources is not secure.</p>
345<p>PSK ciphers are disabled by default, and using TLS-PSK thus requires explicitly
346specifying a cipher suite with the <code>ciphers</code> option. The list of available
347ciphers can be retrieved via <code>openssl ciphers -v 'PSK'</code>. All TLS 1.3
348ciphers are eligible for PSK but currently only those that use SHA256 digest are
349supported they can be retrieved via <code>openssl ciphers -v -s -tls1_3 -psk</code>.</p>
350<p>According to the <a href="https://tools.ietf.org/html/rfc4279">RFC 4279</a>, PSK identities up to 128 bytes in length and
351PSKs up to 64 bytes in length must be supported. As of OpenSSL 1.1.0
352maximum identity size is 128 bytes, and maximum PSK length is 256 bytes.</p>
353<p>The current implementation doesn't support asynchronous PSK callbacks due to the
354limitations of the underlying OpenSSL API.</p>
355<h4>Client-initiated renegotiation attack mitigation<span><a class="mark" href="#tls_client_initiated_renegotiation_attack_mitigation" id="tls_client_initiated_renegotiation_attack_mitigation">#</a></span></h4>
356
357<p>The TLS protocol allows clients to renegotiate certain aspects of the TLS
358session. Unfortunately, session renegotiation requires a disproportionate amount
359of server-side resources, making it a potential vector for denial-of-service
360attacks.</p>
361<p>To mitigate the risk, renegotiation is limited to three times every ten minutes.
362An <code>'error'</code> event is emitted on the <a href="#tls_class_tls_tlssocket"><code>tls.TLSSocket</code></a> instance when this
363threshold is exceeded. The limits are configurable:</p>
364<ul>
365<li><code>tls.CLIENT_RENEG_LIMIT</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the number of renegotiation
366requests. <strong>Default:</strong> <code>3</code>.</li>
367<li><code>tls.CLIENT_RENEG_WINDOW</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the time renegotiation window
368in seconds. <strong>Default:</strong> <code>600</code> (10 minutes).</li>
369</ul>
370<p>The default renegotiation limits should not be modified without a full
371understanding of the implications and risks.</p>
372<p>TLSv1.3 does not support renegotiation.</p>
373<h4>Session resumption<span><a class="mark" href="#tls_session_resumption" id="tls_session_resumption">#</a></span></h4>
374<p>Establishing a TLS session can be relatively slow. The process can be sped
375up by saving and later reusing the session state. There are several mechanisms
376to do so, discussed here from oldest to newest (and preferred).</p>
377<h5>Session identifiers<span><a class="mark" href="#tls_session_identifiers" id="tls_session_identifiers">#</a></span></h5>
378<p>Servers generate a unique ID for new connections and
379send it to the client. Clients and servers save the session state. When
380reconnecting, clients send the ID of their saved session state and if the server
381also has the state for that ID, it can agree to use it. Otherwise, the server
382will create a new session. See <a href="https://www.ietf.org/rfc/rfc2246.txt">RFC 2246</a> for more information, page 23 and
38330.</p>
384<p>Resumption using session identifiers is supported by most web browsers when
385making HTTPS requests.</p>
386<p>For Node.js, clients wait for the <a href="#tls_event_session"><code>'session'</code></a> event to get the session data,
387and provide the data to the <code>session</code> option of a subsequent <a href="#tls_tls_connect_options_callback"><code>tls.connect()</code></a>
388to reuse the session. Servers must
389implement handlers for the <a href="#tls_event_newsession"><code>'newSession'</code></a> and <a href="#tls_event_resumesession"><code>'resumeSession'</code></a> events
390to save and restore the session data using the session ID as the lookup key to
391reuse sessions. To reuse sessions across load balancers or cluster workers,
392servers must use a shared session cache (such as Redis) in their session
393handlers.</p>
394<h5>Session tickets<span><a class="mark" href="#tls_session_tickets" id="tls_session_tickets">#</a></span></h5>
395<p>The servers encrypt the entire session state and send it
396to the client as a "ticket". When reconnecting, the state is sent to the server
397in the initial connection. This mechanism avoids the need for server-side
398session cache. If the server doesn't use the ticket, for any reason (failure
399to decrypt it, it's too old, etc.), it will create a new session and send a new
400ticket. See <a href="https://tools.ietf.org/html/rfc5077">RFC 5077</a> for more information.</p>
401<p>Resumption using session tickets is becoming commonly supported by many web
402browsers when making HTTPS requests.</p>
403<p>For Node.js, clients use the same APIs for resumption with session identifiers
404as for resumption with session tickets. For debugging, if
405<a href="#tls_tlssocket_gettlsticket"><code>tls.TLSSocket.getTLSTicket()</code></a> returns a value, the session data contains a
406ticket, otherwise it contains client-side session state.</p>
407<p>With TLSv1.3, be aware that multiple tickets may be sent by the server,
408resulting in multiple <code>'session'</code> events, see <a href="#tls_event_session"><code>'session'</code></a> for more
409information.</p>
410<p>Single process servers need no specific implementation to use session tickets.
411To use session tickets across server restarts or load balancers, servers must
412all have the same ticket keys. There are three 16-byte keys internally, but the
413tls API exposes them as a single 48-byte buffer for convenience.</p>
414<p>Its possible to get the ticket keys by calling <a href="#tls_server_getticketkeys"><code>server.getTicketKeys()</code></a> on
415one server instance and then distribute them, but it is more reasonable to
416securely generate 48 bytes of secure random data and set them with the
417<code>ticketKeys</code> option of <a href="#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer()</code></a>. The keys should be regularly
418regenerated and server's keys can be reset with
419<a href="#tls_server_setticketkeys_keys"><code>server.setTicketKeys()</code></a>.</p>
420<p>Session ticket keys are cryptographic keys, and they <em><strong>must be stored
421securely</strong></em>. With TLS 1.2 and below, if they are compromised all sessions that
422used tickets encrypted with them can be decrypted. They should not be stored
423on disk, and they should be regenerated regularly.</p>
424<p>If clients advertise support for tickets, the server will send them. The
425server can disable tickets by supplying
426<code>require('constants').SSL_OP_NO_TICKET</code> in <code>secureOptions</code>.</p>
427<p>Both session identifiers and session tickets timeout, causing the server to
428create new sessions. The timeout can be configured with the <code>sessionTimeout</code>
429option of <a href="#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer()</code></a>.</p>
430<p>For all the mechanisms, when resumption fails, servers will create new sessions.
431Since failing to resume the session does not cause TLS/HTTPS connection
432failures, it is easy to not notice unnecessarily poor TLS performance. The
433OpenSSL CLI can be used to verify that servers are resuming sessions. Use the
434<code>-reconnect</code> option to <code>openssl s_client</code>, for example:</p>
435<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">openssl s_client -connect localhost:443 -reconnect</span></code></pre>
436<p>Read through the debug output. The first connection should say "New", for
437example:</p>
438<pre><code class="language-text">New, TLSv1.2, Cipher is ECDHE-RSA-AES128-GCM-SHA256</code></pre>
439<p>Subsequent connections should say "Reused", for example:</p>
440<pre><code class="language-text">Reused, TLSv1.2, Cipher is ECDHE-RSA-AES128-GCM-SHA256</code></pre>
441</section><section><h3>Modifying the default TLS cipher suite<span><a class="mark" href="#tls_modifying_the_default_tls_cipher_suite" id="tls_modifying_the_default_tls_cipher_suite">#</a></span></h3>
442<p>Node.js is built with a default suite of enabled and disabled TLS ciphers. This
443default cipher list can be configured when building Node.js to allow
444distributions to provide their own default list.</p>
445<p>The following command can be used to show the default cipher suite:</p>
446<pre><code class="language-console">node -p crypto.constants.defaultCoreCipherList | tr ':' '\n'
447TLS_AES_256_GCM_SHA384
448TLS_CHACHA20_POLY1305_SHA256
449TLS_AES_128_GCM_SHA256
450ECDHE-RSA-AES128-GCM-SHA256
451ECDHE-ECDSA-AES128-GCM-SHA256
452ECDHE-RSA-AES256-GCM-SHA384
453ECDHE-ECDSA-AES256-GCM-SHA384
454DHE-RSA-AES128-GCM-SHA256
455ECDHE-RSA-AES128-SHA256
456DHE-RSA-AES128-SHA256
457ECDHE-RSA-AES256-SHA384
458DHE-RSA-AES256-SHA384
459ECDHE-RSA-AES256-SHA256
460DHE-RSA-AES256-SHA256
461HIGH
462!aNULL
463!eNULL
464!EXPORT
465!DES
466!RC4
467!MD5
468!PSK
469!SRP
470!CAMELLIA</code></pre>
471<p>This default can be replaced entirely using the <a href="cli.html#cli_tls_cipher_list_list"><code>--tls-cipher-list</code></a>
472command-line switch (directly, or via the <a href="cli.html#cli_node_options_options"><code>NODE_OPTIONS</code></a> environment
473variable). For instance, the following makes <code>ECDHE-RSA-AES128-GCM-SHA256:!RC4</code>
474the default TLS cipher suite:</p>
475<pre><code class="language-bash">node --tls-cipher-list=<span class="hljs-string">'ECDHE-RSA-AES128-GCM-SHA256:!RC4'</span> server.js
476
477<span class="hljs-built_in">export</span> NODE_OPTIONS=--tls-cipher-list=<span class="hljs-string">'ECDHE-RSA-AES128-GCM-SHA256:!RC4'</span>
478node server.js</code></pre>
479<p>The default can also be replaced on a per client or server basis using the
480<code>ciphers</code> option from <a href="#tls_tls_createsecurecontext_options"><code>tls.createSecureContext()</code></a>, which is also available
481in <a href="#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer()</code></a>, <a href="#tls_tls_connect_options_callback"><code>tls.connect()</code></a>, and when creating new
482<a href="#tls_class_tls_tlssocket"><code>tls.TLSSocket</code></a>s.</p>
483<p>The ciphers list can contain a mixture of TLSv1.3 cipher suite names, the ones
484that start with <code>'TLS_'</code>, and specifications for TLSv1.2 and below cipher
485suites. The TLSv1.2 ciphers support a legacy specification format, consult
486the OpenSSL <a href="https://www.openssl.org/docs/man1.1.1/man1/ciphers.html#CIPHER-LIST-FORMAT">cipher list format</a> documentation for details, but those
487specifications do <em>not</em> apply to TLSv1.3 ciphers. The TLSv1.3 suites can only
488be enabled by including their full name in the cipher list. They cannot, for
489example, be enabled or disabled by using the legacy TLSv1.2 <code>'EECDH'</code> or
490<code>'!EECDH'</code> specification.</p>
491<p>Despite the relative order of TLSv1.3 and TLSv1.2 cipher suites, the TLSv1.3
492protocol is significantly more secure than TLSv1.2, and will always be chosen
493over TLSv1.2 if the handshake indicates it is supported, and if any TLSv1.3
494cipher suites are enabled.</p>
495<p>The default cipher suite included within Node.js has been carefully
496selected to reflect current security best practices and risk mitigation.
497Changing the default cipher suite can have a significant impact on the security
498of an application. The <code>--tls-cipher-list</code> switch and <code>ciphers</code> option should by
499used only if absolutely necessary.</p>
500<p>The default cipher suite prefers GCM ciphers for <a href="https://www.chromium.org/Home/chromium-security/education/tls#TOC-Cipher-Suites">Chrome's 'modern
501cryptography' setting</a> and also prefers ECDHE and DHE ciphers for perfect
502forward secrecy, while offering <em>some</em> backward compatibility.</p>
503<p>128 bit AES is preferred over 192 and 256 bit AES in light of <a href="https://www.schneier.com/blog/archives/2009/07/another_new_aes.html">specific
504attacks affecting larger AES key sizes</a>.</p>
505<p>Old clients that rely on insecure and deprecated RC4 or DES-based ciphers
506(like Internet Explorer 6) cannot complete the handshaking process with
507the default configuration. If these clients <em>must</em> be supported, the
508<a href="https://wiki.mozilla.org/Security/Server_Side_TLS">TLS recommendations</a> may offer a compatible cipher suite. For more details
509on the format, see the OpenSSL <a href="https://www.openssl.org/docs/man1.1.1/man1/ciphers.html#CIPHER-LIST-FORMAT">cipher list format</a> documentation.</p>
510<p>There are only 5 TLSv1.3 cipher suites:</p>
511<ul>
512<li><code>'TLS_AES_256_GCM_SHA384'</code></li>
513<li><code>'TLS_CHACHA20_POLY1305_SHA256'</code></li>
514<li><code>'TLS_AES_128_GCM_SHA256'</code></li>
515<li><code>'TLS_AES_128_CCM_SHA256'</code></li>
516<li><code>'TLS_AES_128_CCM_8_SHA256'</code></li>
517</ul>
518<p>The first 3 are enabled by default. The last 2 <code>CCM</code>-based suites are supported
519by TLSv1.3 because they may be more performant on constrained systems, but they
520are not enabled by default since they offer less security.</p>
521</section><section><h3>Class: <code>tls.CryptoStream</code><span><a class="mark" href="#tls_class_tls_cryptostream" id="tls_class_tls_cryptostream">#</a></span></h3>
522<div class="api_metadata">
523<span>Added in: v0.3.4</span><span>Deprecated since: v0.11.3</span>
524</div>
525<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#tls_class_tls_tlssocket"><code>tls.TLSSocket</code></a> instead.</div><p></p>
526<p>The <code>tls.CryptoStream</code> class represents a stream of encrypted data. This class
527is deprecated and should no longer be used.</p>
528<h4><code>cryptoStream.bytesWritten</code><span><a class="mark" href="#tls_cryptostream_byteswritten" id="tls_cryptostream_byteswritten">#</a></span></h4>
529<div class="api_metadata">
530<span>Added in: v0.3.4</span><span>Deprecated since: v0.11.3</span>
531</div>
532<p>The <code>cryptoStream.bytesWritten</code> property returns the total number of bytes
533written to the underlying socket <em>including</em> the bytes required for the
534implementation of the TLS protocol.</p>
535</section><section><h3>Class: <code>tls.SecurePair</code><span><a class="mark" href="#tls_class_tls_securepair" id="tls_class_tls_securepair">#</a></span></h3>
536<div class="api_metadata">
537<span>Added in: v0.3.2</span><span>Deprecated since: v0.11.3</span>
538</div>
539<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#tls_class_tls_tlssocket"><code>tls.TLSSocket</code></a> instead.</div><p></p>
540<p>Returned by <a href="#tls_tls_createsecurepair_context_isserver_requestcert_rejectunauthorized_options"><code>tls.createSecurePair()</code></a>.</p>
541<h4>Event: <code>'secure'</code><span><a class="mark" href="#tls_event_secure" id="tls_event_secure">#</a></span></h4>
542<div class="api_metadata">
543<span>Added in: v0.3.2</span><span>Deprecated since: v0.11.3</span>
544</div>
545<p>The <code>'secure'</code> event is emitted by the <code>SecurePair</code> object once a secure
546connection has been established.</p>
547<p>As with checking for the server
548<a href="#tls_event_secureconnection"><code>'secureConnection'</code></a>
549event, <code>pair.cleartext.authorized</code> should be inspected to confirm whether the
550certificate used is properly authorized.</p>
551</section><section><h3>Class: <code>tls.Server</code><span><a class="mark" href="#tls_class_tls_server" id="tls_class_tls_server">#</a></span></h3>
552<div class="api_metadata">
553<span>Added in: v0.3.2</span>
554</div>
555<ul>
556<li>Extends: <a href="net.html#net_class_net_server" class="type">&#x3C;net.Server></a></li>
557</ul>
558<p>Accepts encrypted connections using TLS or SSL.</p>
559<h4>Event: <code>'connection'</code><span><a class="mark" href="#tls_event_connection" id="tls_event_connection">#</a></span></h4>
560<div class="api_metadata">
561<span>Added in: v0.3.2</span>
562</div>
563<ul>
564<li><code>socket</code> <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a></li>
565</ul>
566<p>This event is emitted when a new TCP stream is established, before the TLS
567handshake begins. <code>socket</code> is typically an object of type <a href="net.html#net_class_net_socket"><code>net.Socket</code></a>.
568Usually users will not want to access this event.</p>
569<p>This event can also be explicitly emitted by users to inject connections
570into the TLS server. In that case, any <a href="stream.html#stream_class_stream_duplex"><code>Duplex</code></a> stream can be passed.</p>
571<h4>Event: <code>'keylog'</code><span><a class="mark" href="#tls_event_keylog" id="tls_event_keylog">#</a></span></h4>
572<div class="api_metadata">
573<span>Added in: v12.3.0, v10.20.0</span>
574</div>
575<ul>
576<li><code>line</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> Line of ASCII text, in NSS <code>SSLKEYLOGFILE</code> format.</li>
577<li><code>tlsSocket</code> <a href="tls.html#tls_class_tls_tlssocket" class="type">&#x3C;tls.TLSSocket></a> The <code>tls.TLSSocket</code> instance on which it was
578generated.</li>
579</ul>
580<p>The <code>keylog</code> event is emitted when key material is generated or received by
581a connection to this server (typically before handshake has completed, but not
582necessarily). This keying material can be stored for debugging, as it allows
583captured TLS traffic to be decrypted. It may be emitted multiple times for
584each socket.</p>
585<p>A typical use case is to append received lines to a common text file, which
586is later used by software (such as Wireshark) to decrypt the traffic:</p>
587<pre><code class="language-js"><span class="hljs-keyword">const</span> logFile = fs.<span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'/tmp/ssl-keys.log'</span>, { <span class="hljs-attr">flags</span>: <span class="hljs-string">'a'</span> });
588<span class="hljs-comment">// ...</span>
589server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'keylog'</span>, <span class="hljs-function">(<span class="hljs-params">line, tlsSocket</span>) =></span> {
590  <span class="hljs-keyword">if</span> (tlsSocket.<span class="hljs-property">remoteAddress</span> !== <span class="hljs-string">'...'</span>)
591    <span class="hljs-keyword">return</span>; <span class="hljs-comment">// Only log keys for a particular IP</span>
592  logFile.<span class="hljs-title function_">write</span>(line);
593});</code></pre>
594<h4>Event: <code>'newSession'</code><span><a class="mark" href="#tls_event_newsession" id="tls_event_newsession">#</a></span></h4>
595<div class="api_metadata">
596<details class="changelog"><summary>History</summary>
597<table>
598<tbody><tr><th>Version</th><th>Changes</th></tr>
599<tr><td>v0.11.12</td>
600<td><p>The <code>callback</code> argument is now supported.</p></td></tr>
601<tr><td>v0.9.2</td>
602<td><p><span>Added in: v0.9.2</span></p></td></tr>
603</tbody></table>
604</details>
605</div>
606<p>The <code>'newSession'</code> event is emitted upon creation of a new TLS session. This may
607be used to store sessions in external storage. The data should be provided to
608the <a href="#tls_event_resumesession"><code>'resumeSession'</code></a> callback.</p>
609<p>The listener callback is passed three arguments when called:</p>
610<ul>
611<li><code>sessionId</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> The TLS session identifier</li>
612<li><code>sessionData</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> The TLS session data</li>
613<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A callback function taking no arguments that must be
614invoked in order for data to be sent or received over the secure connection.</li>
615</ul>
616<p>Listening for this event will have an effect only on connections established
617after the addition of the event listener.</p>
618<h4>Event: <code>'OCSPRequest'</code><span><a class="mark" href="#tls_event_ocsprequest" id="tls_event_ocsprequest">#</a></span></h4>
619<div class="api_metadata">
620<span>Added in: v0.11.13</span>
621</div>
622<p>The <code>'OCSPRequest'</code> event is emitted when the client sends a certificate status
623request. The listener callback is passed three arguments when called:</p>
624<ul>
625<li><code>certificate</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> The server certificate</li>
626<li><code>issuer</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> The issuer's certificate</li>
627<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A callback function that must be invoked to provide
628the results of the OCSP request.</li>
629</ul>
630<p>The server's current certificate can be parsed to obtain the OCSP URL
631and certificate ID; after obtaining an OCSP response, <code>callback(null, resp)</code> is
632then invoked, where <code>resp</code> is a <code>Buffer</code> instance containing the OCSP response.
633Both <code>certificate</code> and <code>issuer</code> are <code>Buffer</code> DER-representations of the
634primary and issuer's certificates. These can be used to obtain the OCSP
635certificate ID and OCSP endpoint URL.</p>
636<p>Alternatively, <code>callback(null, null)</code> may be called, indicating that there was
637no OCSP response.</p>
638<p>Calling <code>callback(err)</code> will result in a <code>socket.destroy(err)</code> call.</p>
639<p>The typical flow of an OCSP Request is as follows:</p>
640<ol>
641<li>Client connects to the server and sends an <code>'OCSPRequest'</code> (via the status
642info extension in ClientHello).</li>
643<li>Server receives the request and emits the <code>'OCSPRequest'</code> event, calling the
644listener if registered.</li>
645<li>Server extracts the OCSP URL from either the <code>certificate</code> or <code>issuer</code> and
646performs an <a href="https://en.wikipedia.org/wiki/OCSP_stapling">OCSP request</a> to the CA.</li>
647<li>Server receives <code>'OCSPResponse'</code> from the CA and sends it back to the client
648via the <code>callback</code> argument</li>
649<li>Client validates the response and either destroys the socket or performs a
650handshake.</li>
651</ol>
652<p>The <code>issuer</code> can be <code>null</code> if the certificate is either self-signed or the
653issuer is not in the root certificates list. (An issuer may be provided
654via the <code>ca</code> option when establishing the TLS connection.)</p>
655<p>Listening for this event will have an effect only on connections established
656after the addition of the event listener.</p>
657<p>An npm module like <a href="https://www.npmjs.com/package/asn1.js">asn1.js</a> may be used to parse the certificates.</p>
658<h4>Event: <code>'resumeSession'</code><span><a class="mark" href="#tls_event_resumesession" id="tls_event_resumesession">#</a></span></h4>
659<div class="api_metadata">
660<span>Added in: v0.9.2</span>
661</div>
662<p>The <code>'resumeSession'</code> event is emitted when the client requests to resume a
663previous TLS session. The listener callback is passed two arguments when
664called:</p>
665<ul>
666<li><code>sessionId</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> The TLS session identifier</li>
667<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A callback function to be called when the prior session
668has been recovered: <code>callback([err[, sessionData]])</code>
669<ul>
670<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
671<li><code>sessionData</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
672</ul>
673</li>
674</ul>
675<p>The event listener should perform a lookup in external storage for the
676<code>sessionData</code> saved by the <a href="#tls_event_newsession"><code>'newSession'</code></a> event handler using the given
677<code>sessionId</code>. If found, call <code>callback(null, sessionData)</code> to resume the session.
678If not found, the session cannot be resumed. <code>callback()</code> must be called
679without <code>sessionData</code> so that the handshake can continue and a new session can
680be created. It is possible to call <code>callback(err)</code> to terminate the incoming
681connection and destroy the socket.</p>
682<p>Listening for this event will have an effect only on connections established
683after the addition of the event listener.</p>
684<p>The following illustrates resuming a TLS session:</p>
685<pre><code class="language-js"><span class="hljs-keyword">const</span> tlsSessionStore = {};
686server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'newSession'</span>, <span class="hljs-function">(<span class="hljs-params">id, data, cb</span>) =></span> {
687  tlsSessionStore[id.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)] = data;
688  <span class="hljs-title function_">cb</span>();
689});
690server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'resumeSession'</span>, <span class="hljs-function">(<span class="hljs-params">id, cb</span>) =></span> {
691  <span class="hljs-title function_">cb</span>(<span class="hljs-literal">null</span>, tlsSessionStore[id.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>)] || <span class="hljs-literal">null</span>);
692});</code></pre>
693<h4>Event: <code>'secureConnection'</code><span><a class="mark" href="#tls_event_secureconnection" id="tls_event_secureconnection">#</a></span></h4>
694<div class="api_metadata">
695<span>Added in: v0.3.2</span>
696</div>
697<p>The <code>'secureConnection'</code> event is emitted after the handshaking process for a
698new connection has successfully completed. The listener callback is passed a
699single argument when called:</p>
700<ul>
701<li><code>tlsSocket</code> <a href="tls.html#tls_class_tls_tlssocket" class="type">&#x3C;tls.TLSSocket></a> The established TLS socket.</li>
702</ul>
703<p>The <code>tlsSocket.authorized</code> property is a <code>boolean</code> indicating whether the
704client has been verified by one of the supplied Certificate Authorities for the
705server. If <code>tlsSocket.authorized</code> is <code>false</code>, then <code>socket.authorizationError</code>
706is set to describe how authorization failed. Depending on the settings
707of the TLS server, unauthorized connections may still be accepted.</p>
708<p>The <code>tlsSocket.alpnProtocol</code> property is a string that contains the selected
709ALPN protocol. When ALPN has no selected protocol, <code>tlsSocket.alpnProtocol</code>
710equals <code>false</code>.</p>
711<p>The <code>tlsSocket.servername</code> property is a string containing the server name
712requested via SNI.</p>
713<h4>Event: <code>'tlsClientError'</code><span><a class="mark" href="#tls_event_tlsclienterror" id="tls_event_tlsclienterror">#</a></span></h4>
714<div class="api_metadata">
715<span>Added in: v6.0.0</span>
716</div>
717<p>The <code>'tlsClientError'</code> event is emitted when an error occurs before a secure
718connection is established. The listener callback is passed two arguments when
719called:</p>
720<ul>
721<li><code>exception</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> The <code>Error</code> object describing the error</li>
722<li><code>tlsSocket</code> <a href="tls.html#tls_class_tls_tlssocket" class="type">&#x3C;tls.TLSSocket></a> The <code>tls.TLSSocket</code> instance from which the
723error originated.</li>
724</ul>
725<h4><code>server.addContext(hostname, context)</code><span><a class="mark" href="#tls_server_addcontext_hostname_context" id="tls_server_addcontext_hostname_context">#</a></span></h4>
726<div class="api_metadata">
727<span>Added in: v0.5.3</span>
728</div>
729<ul>
730<li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> A SNI host name or wildcard (e.g. <code>'*'</code>)</li>
731<li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> An object containing any of the possible properties
732from the <a href="#tls_tls_createsecurecontext_options"><code>tls.createSecureContext()</code></a> <code>options</code> arguments (e.g. <code>key</code>,
733<code>cert</code>, <code>ca</code>, etc).</li>
734</ul>
735<p>The <code>server.addContext()</code> method adds a secure context that will be used if
736the client request's SNI name matches the supplied <code>hostname</code> (or wildcard).</p>
737<h4><code>server.address()</code><span><a class="mark" href="#tls_server_address" id="tls_server_address">#</a></span></h4>
738<div class="api_metadata">
739<span>Added in: v0.6.0</span>
740</div>
741<ul>
742<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
743</ul>
744<p>Returns the bound address, the address family name, and port of the
745server as reported by the operating system. See <a href="net.html#net_server_address"><code>net.Server.address()</code></a> for
746more information.</p>
747<h4><code>server.close([callback])</code><span><a class="mark" href="#tls_server_close_callback" id="tls_server_close_callback">#</a></span></h4>
748<div class="api_metadata">
749<span>Added in: v0.3.2</span>
750</div>
751<ul>
752<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A listener callback that will be registered to listen
753for the server instance's <code>'close'</code> event.</li>
754<li>Returns: <a href="tls.html#tls_class_tls_server" class="type">&#x3C;tls.Server></a></li>
755</ul>
756<p>The <code>server.close()</code> method stops the server from accepting new connections.</p>
757<p>This function operates asynchronously. The <code>'close'</code> event will be emitted
758when the server has no more open connections.</p>
759<h4><code>server.connections</code><span><a class="mark" href="#tls_server_connections" id="tls_server_connections">#</a></span></h4>
760<div class="api_metadata">
761<span>Added in: v0.3.2</span><span>Deprecated since: v0.9.7</span>
762</div>
763<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="net.html#net_server_getconnections_callback"><code>server.getConnections()</code></a> instead.</div><p></p>
764<ul>
765<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
766</ul>
767<p>Returns the current number of concurrent connections on the server.</p>
768<h4><code>server.getTicketKeys()</code><span><a class="mark" href="#tls_server_getticketkeys" id="tls_server_getticketkeys">#</a></span></h4>
769<div class="api_metadata">
770<span>Added in: v3.0.0</span>
771</div>
772<ul>
773<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> A 48-byte buffer containing the session ticket keys.</li>
774</ul>
775<p>Returns the session ticket keys.</p>
776<p>See <a href="#tls_session_resumption">Session Resumption</a> for more information.</p>
777<h4><code>server.listen()</code><span><a class="mark" href="#tls_server_listen" id="tls_server_listen">#</a></span></h4>
778<p>Starts the server listening for encrypted connections.
779This method is identical to <a href="net.html#net_server_listen"><code>server.listen()</code></a> from <a href="net.html#net_class_net_server"><code>net.Server</code></a>.</p>
780<h4><code>server.setSecureContext(options)</code><span><a class="mark" href="#tls_server_setsecurecontext_options" id="tls_server_setsecurecontext_options">#</a></span></h4>
781<div class="api_metadata">
782<span>Added in: v11.0.0</span>
783</div>
784<ul>
785<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> An object containing any of the possible properties from
786the <a href="#tls_tls_createsecurecontext_options"><code>tls.createSecureContext()</code></a> <code>options</code> arguments (e.g. <code>key</code>, <code>cert</code>,
787<code>ca</code>, etc).</li>
788</ul>
789<p>The <code>server.setSecureContext()</code> method replaces the secure context of an
790existing server. Existing connections to the server are not interrupted.</p>
791<h4><code>server.setTicketKeys(keys)</code><span><a class="mark" href="#tls_server_setticketkeys_keys" id="tls_server_setticketkeys_keys">#</a></span></h4>
792<div class="api_metadata">
793<span>Added in: v3.0.0</span>
794</div>
795<ul>
796<li><code>keys</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> A 48-byte buffer containing the session
797ticket keys.</li>
798</ul>
799<p>Sets the session ticket keys.</p>
800<p>Changes to the ticket keys are effective only for future server connections.
801Existing or currently pending server connections will use the previous keys.</p>
802<p>See <a href="#tls_session_resumption">Session Resumption</a> for more information.</p>
803</section><section><h3>Class: <code>tls.TLSSocket</code><span><a class="mark" href="#tls_class_tls_tlssocket" id="tls_class_tls_tlssocket">#</a></span></h3>
804<div class="api_metadata">
805<span>Added in: v0.11.4</span>
806</div>
807<ul>
808<li>Extends: <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a></li>
809</ul>
810<p>Performs transparent encryption of written data and all required TLS
811negotiation.</p>
812<p>Instances of <code>tls.TLSSocket</code> implement the duplex <a href="stream.html#stream_stream">Stream</a> interface.</p>
813<p>Methods that return TLS connection metadata (e.g.
814<a href="#tls_tlssocket_getpeercertificate_detailed"><code>tls.TLSSocket.getPeerCertificate()</code></a> will only return data while the
815connection is open.</p>
816<h4><code>new tls.TLSSocket(socket[, options])</code><span><a class="mark" href="#tls_new_tls_tlssocket_socket_options" id="tls_new_tls_tlssocket_socket_options">#</a></span></h4>
817<div class="api_metadata">
818<details class="changelog"><summary>History</summary>
819<table>
820<tbody><tr><th>Version</th><th>Changes</th></tr>
821<tr><td>v12.2.0</td>
822<td><p>The <code>enableTrace</code> option is now supported.</p></td></tr>
823<tr><td>v5.0.0</td>
824<td><p>ALPN options are supported now.</p></td></tr>
825<tr><td>v0.11.4</td>
826<td><p><span>Added in: v0.11.4</span></p></td></tr>
827</tbody></table>
828</details>
829</div>
830<ul>
831<li><code>socket</code> <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a> | <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a>
832On the server side, any <code>Duplex</code> stream. On the client side, any
833instance of <a href="net.html#net_class_net_socket"><code>net.Socket</code></a> (for generic <code>Duplex</code> stream support
834on the client side, <a href="#tls_tls_connect_options_callback"><code>tls.connect()</code></a> must be used).</li>
835<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
836<ul>
837<li><code>enableTrace</code>: See <a href="#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer()</code></a></li>
838<li><code>isServer</code>: The SSL/TLS protocol is asymmetrical, TLSSockets must know if
839they are to behave as a server or a client. If <code>true</code> the TLS socket will be
840instantiated as a server. <strong>Default:</strong> <code>false</code>.</li>
841<li><code>server</code> <a href="net.html#net_class_net_server" class="type">&#x3C;net.Server></a> A <a href="net.html#net_class_net_server"><code>net.Server</code></a> instance.</li>
842<li><code>requestCert</code>: Whether to authenticate the remote peer by requesting a
843certificate. Clients always request a server certificate. Servers
844(<code>isServer</code> is true) may set <code>requestCert</code> to true to request a client
845certificate.</li>
846<li><code>rejectUnauthorized</code>: See <a href="#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer()</code></a></li>
847<li><code>ALPNProtocols</code>: See <a href="#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer()</code></a></li>
848<li><code>SNICallback</code>: See <a href="#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer()</code></a></li>
849<li><code>session</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> A <code>Buffer</code> instance containing a TLS session.</li>
850<li><code>requestOCSP</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, specifies that the OCSP status request
851extension will be added to the client hello and an <code>'OCSPResponse'</code> event
852will be emitted on the socket before establishing a secure communication</li>
853<li><code>secureContext</code>: TLS context object created with
854<a href="#tls_tls_createsecurecontext_options"><code>tls.createSecureContext()</code></a>. If a <code>secureContext</code> is <em>not</em> provided, one
855will be created by passing the entire <code>options</code> object to
856<code>tls.createSecureContext()</code>.</li>
857<li>...: <a href="#tls_tls_createsecurecontext_options"><code>tls.createSecureContext()</code></a> options that are used if the
858<code>secureContext</code> option is missing. Otherwise, they are ignored.</li>
859</ul>
860</li>
861</ul>
862<p>Construct a new <code>tls.TLSSocket</code> object from an existing TCP socket.</p>
863<h4>Event: <code>'keylog'</code><span><a class="mark" href="#tls_event_keylog_1" id="tls_event_keylog_1">#</a></span></h4>
864<div class="api_metadata">
865<span>Added in: v12.3.0, v10.20.0</span>
866</div>
867<ul>
868<li><code>line</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> Line of ASCII text, in NSS <code>SSLKEYLOGFILE</code> format.</li>
869</ul>
870<p>The <code>keylog</code> event is emitted on a <code>tls.TLSSocket</code> when key material
871is generated or received by the socket. This keying material can be stored
872for debugging, as it allows captured TLS traffic to be decrypted. It may
873be emitted multiple times, before or after the handshake completes.</p>
874<p>A typical use case is to append received lines to a common text file, which
875is later used by software (such as Wireshark) to decrypt the traffic:</p>
876<pre><code class="language-js"><span class="hljs-keyword">const</span> logFile = fs.<span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'/tmp/ssl-keys.log'</span>, { <span class="hljs-attr">flags</span>: <span class="hljs-string">'a'</span> });
877<span class="hljs-comment">// ...</span>
878tlsSocket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'keylog'</span>, <span class="hljs-function">(<span class="hljs-params">line</span>) =></span> logFile.<span class="hljs-title function_">write</span>(line));</code></pre>
879<h4>Event: <code>'OCSPResponse'</code><span><a class="mark" href="#tls_event_ocspresponse" id="tls_event_ocspresponse">#</a></span></h4>
880<div class="api_metadata">
881<span>Added in: v0.11.13</span>
882</div>
883<p>The <code>'OCSPResponse'</code> event is emitted if the <code>requestOCSP</code> option was set
884when the <code>tls.TLSSocket</code> was created and an OCSP response has been received.
885The listener callback is passed a single argument when called:</p>
886<ul>
887<li><code>response</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> The server's OCSP response</li>
888</ul>
889<p>Typically, the <code>response</code> is a digitally signed object from the server's CA that
890contains information about server's certificate revocation status.</p>
891<h4>Event: <code>'secureConnect'</code><span><a class="mark" href="#tls_event_secureconnect" id="tls_event_secureconnect">#</a></span></h4>
892<div class="api_metadata">
893<span>Added in: v0.11.4</span>
894</div>
895<p>The <code>'secureConnect'</code> event is emitted after the handshaking process for a new
896connection has successfully completed. The listener callback will be called
897regardless of whether or not the server's certificate has been authorized. It
898is the client's responsibility to check the <code>tlsSocket.authorized</code> property to
899determine if the server certificate was signed by one of the specified CAs. If
900<code>tlsSocket.authorized === false</code>, then the error can be found by examining the
901<code>tlsSocket.authorizationError</code> property. If ALPN was used, the
902<code>tlsSocket.alpnProtocol</code> property can be checked to determine the negotiated
903protocol.</p>
904<p>The <code>'secureConnect'</code> event is not emitted when a <a href="tls.html#tls_class_tls_tlssocket" class="type">&#x3C;tls.TLSSocket></a> is created
905using the <code>new tls.TLSSocket()</code> constructor.</p>
906<h4>Event: <code>'session'</code><span><a class="mark" href="#tls_event_session" id="tls_event_session">#</a></span></h4>
907<div class="api_metadata">
908<span>Added in: v11.10.0</span>
909</div>
910<ul>
911<li><code>session</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
912</ul>
913<p>The <code>'session'</code> event is emitted on a client <code>tls.TLSSocket</code> when a new session
914or TLS ticket is available. This may or may not be before the handshake is
915complete, depending on the TLS protocol version that was negotiated. The event
916is not emitted on the server, or if a new session was not created, for example,
917when the connection was resumed. For some TLS protocol versions the event may be
918emitted multiple times, in which case all the sessions can be used for
919resumption.</p>
920<p>On the client, the <code>session</code> can be provided to the <code>session</code> option of
921<a href="#tls_tls_connect_options_callback"><code>tls.connect()</code></a> to resume the connection.</p>
922<p>See <a href="#tls_session_resumption">Session Resumption</a> for more information.</p>
923<p>For TLSv1.2 and below, <a href="#tls_tlssocket_getsession"><code>tls.TLSSocket.getSession()</code></a> can be called once
924the handshake is complete. For TLSv1.3, only ticket-based resumption is allowed
925by the protocol, multiple tickets are sent, and the tickets aren't sent until
926after the handshake completes. So it is necessary to wait for the
927<code>'session'</code> event to get a resumable session. Applications
928should use the <code>'session'</code> event instead of <code>getSession()</code> to ensure
929they will work for all TLS versions. Applications that only expect to
930get or use one session should listen for this event only once:</p>
931<pre><code class="language-js">tlsSocket.<span class="hljs-title function_">once</span>(<span class="hljs-string">'session'</span>, <span class="hljs-function">(<span class="hljs-params">session</span>) =></span> {
932  <span class="hljs-comment">// The session can be used immediately or later.</span>
933  tls.<span class="hljs-title function_">connect</span>({
934    <span class="hljs-attr">session</span>: session,
935    <span class="hljs-comment">// Other connect options...</span>
936  });
937});</code></pre>
938<h4><code>tlsSocket.address()</code><span><a class="mark" href="#tls_tlssocket_address" id="tls_tlssocket_address">#</a></span></h4>
939<div class="api_metadata">
940<span>Added in: v0.11.4</span>
941</div>
942<ul>
943<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
944</ul>
945<p>Returns the bound <code>address</code>, the address <code>family</code> name, and <code>port</code> of the
946underlying socket as reported by the operating system:
947<code>{ port: 12346, family: 'IPv4', address: '127.0.0.1' }</code>.</p>
948<h4><code>tlsSocket.authorizationError</code><span><a class="mark" href="#tls_tlssocket_authorizationerror" id="tls_tlssocket_authorizationerror">#</a></span></h4>
949<div class="api_metadata">
950<span>Added in: v0.11.4</span>
951</div>
952<p>Returns the reason why the peer's certificate was not been verified. This
953property is set only when <code>tlsSocket.authorized === false</code>.</p>
954<h4><code>tlsSocket.authorized</code><span><a class="mark" href="#tls_tlssocket_authorized" id="tls_tlssocket_authorized">#</a></span></h4>
955<div class="api_metadata">
956<span>Added in: v0.11.4</span>
957</div>
958<ul>
959<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
960</ul>
961<p>Returns <code>true</code> if the peer certificate was signed by one of the CAs specified
962when creating the <code>tls.TLSSocket</code> instance, otherwise <code>false</code>.</p>
963<h4><code>tlsSocket.disableRenegotiation()</code><span><a class="mark" href="#tls_tlssocket_disablerenegotiation" id="tls_tlssocket_disablerenegotiation">#</a></span></h4>
964<div class="api_metadata">
965<span>Added in: v8.4.0</span>
966</div>
967<p>Disables TLS renegotiation for this <code>TLSSocket</code> instance. Once called, attempts
968to renegotiate will trigger an <code>'error'</code> event on the <code>TLSSocket</code>.</p>
969<h4><code>tlsSocket.enableTrace()</code><span><a class="mark" href="#tls_tlssocket_enabletrace" id="tls_tlssocket_enabletrace">#</a></span></h4>
970<div class="api_metadata">
971<span>Added in: v12.2.0</span>
972</div>
973<p>When enabled, TLS packet trace information is written to <code>stderr</code>. This can be
974used to debug TLS connection problems.</p>
975<p>Note: The format of the output is identical to the output of <code>openssl s_client -trace</code> or <code>openssl s_server -trace</code>. While it is produced by OpenSSL's
976<code>SSL_trace()</code> function, the format is undocumented, can change without notice,
977and should not be relied on.</p>
978<h4><code>tlsSocket.encrypted</code><span><a class="mark" href="#tls_tlssocket_encrypted" id="tls_tlssocket_encrypted">#</a></span></h4>
979<div class="api_metadata">
980<span>Added in: v0.11.4</span>
981</div>
982<p>Always returns <code>true</code>. This may be used to distinguish TLS sockets from regular
983<code>net.Socket</code> instances.</p>
984<h4><code>tlsSocket.getCertificate()</code><span><a class="mark" href="#tls_tlssocket_getcertificate" id="tls_tlssocket_getcertificate">#</a></span></h4>
985<div class="api_metadata">
986<span>Added in: v11.2.0</span>
987</div>
988<ul>
989<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
990</ul>
991<p>Returns an object representing the local certificate. The returned object has
992some properties corresponding to the fields of the certificate.</p>
993<p>See <a href="#tls_tlssocket_getpeercertificate_detailed"><code>tls.TLSSocket.getPeerCertificate()</code></a> for an example of the certificate
994structure.</p>
995<p>If there is no local certificate, an empty object will be returned. If the
996socket has been destroyed, <code>null</code> will be returned.</p>
997<h4><code>tlsSocket.getCipher()</code><span><a class="mark" href="#tls_tlssocket_getcipher" id="tls_tlssocket_getcipher">#</a></span></h4>
998<div class="api_metadata">
999<details class="changelog"><summary>History</summary>
1000<table>
1001<tbody><tr><th>Version</th><th>Changes</th></tr>
1002<tr><td>v13.4.0, v12.16.0</td>
1003<td><p>Return the IETF cipher name as <code>standardName</code>.</p></td></tr>
1004<tr><td>v12.0.0</td>
1005<td><p>Return the minimum cipher version, instead of a fixed string (<code>'TLSv1/SSLv3'</code>).</p></td></tr>
1006<tr><td>v0.11.4</td>
1007<td><p><span>Added in: v0.11.4</span></p></td></tr>
1008</tbody></table>
1009</details>
1010</div>
1011<ul>
1012<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1013<ul>
1014<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> OpenSSL name for the cipher suite.</li>
1015<li><code>standardName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> IETF name for the cipher suite.</li>
1016<li><code>version</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The minimum TLS protocol version supported by this cipher
1017suite.</li>
1018</ul>
1019</li>
1020</ul>
1021<p>Returns an object containing information on the negotiated cipher suite.</p>
1022<p>For example:</p>
1023<pre><code class="language-json"><span class="hljs-punctuation">{</span>
1024    <span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"AES128-SHA256"</span><span class="hljs-punctuation">,</span>
1025    <span class="hljs-attr">"standardName"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"TLS_RSA_WITH_AES_128_CBC_SHA256"</span><span class="hljs-punctuation">,</span>
1026    <span class="hljs-attr">"version"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"TLSv1.2"</span>
1027<span class="hljs-punctuation">}</span></code></pre>
1028<p>See
1029<a href="https://www.openssl.org/docs/man1.1.1/man3/SSL_CIPHER_get_name.html">SSL_CIPHER_get_name</a>
1030for more information.</p>
1031<h4><code>tlsSocket.getEphemeralKeyInfo()</code><span><a class="mark" href="#tls_tlssocket_getephemeralkeyinfo" id="tls_tlssocket_getephemeralkeyinfo">#</a></span></h4>
1032<div class="api_metadata">
1033<span>Added in: v5.0.0</span>
1034</div>
1035<ul>
1036<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
1037</ul>
1038<p>Returns an object representing the type, name, and size of parameter of
1039an ephemeral key exchange in <a href="#tls_perfect_forward_secrecy">perfect forward secrecy</a> on a client
1040connection. It returns an empty object when the key exchange is not
1041ephemeral. As this is only supported on a client socket; <code>null</code> is returned
1042if called on a server socket. The supported types are <code>'DH'</code> and <code>'ECDH'</code>. The
1043<code>name</code> property is available only when type is <code>'ECDH'</code>.</p>
1044<p>For example: <code>{ type: 'ECDH', name: 'prime256v1', size: 256 }</code>.</p>
1045<h4><code>tlsSocket.getFinished()</code><span><a class="mark" href="#tls_tlssocket_getfinished" id="tls_tlssocket_getfinished">#</a></span></h4>
1046<div class="api_metadata">
1047<span>Added in: v9.9.0</span>
1048</div>
1049<ul>
1050<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a> The latest <code>Finished</code> message that has been
1051sent to the socket as part of a SSL/TLS handshake, or <code>undefined</code> if
1052no <code>Finished</code> message has been sent yet.</li>
1053</ul>
1054<p>As the <code>Finished</code> messages are message digests of the complete handshake
1055(with a total of 192 bits for TLS 1.0 and more for SSL 3.0), they can
1056be used for external authentication procedures when the authentication
1057provided by SSL/TLS is not desired or is not enough.</p>
1058<p>Corresponds to the <code>SSL_get_finished</code> routine in OpenSSL and may be used
1059to implement the <code>tls-unique</code> channel binding from <a href="https://tools.ietf.org/html/rfc5929">RFC 5929</a>.</p>
1060<h4><code>tlsSocket.getPeerCertificate([detailed])</code><span><a class="mark" href="#tls_tlssocket_getpeercertificate_detailed" id="tls_tlssocket_getpeercertificate_detailed">#</a></span></h4>
1061<div class="api_metadata">
1062<span>Added in: v0.11.4</span>
1063</div>
1064<ul>
1065<li><code>detailed</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Include the full certificate chain if <code>true</code>, otherwise
1066include just the peer's certificate.</li>
1067<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> A certificate object.</li>
1068</ul>
1069<p>Returns an object representing the peer's certificate. If the peer does not
1070provide a certificate, an empty object will be returned. If the socket has been
1071destroyed, <code>null</code> will be returned.</p>
1072<p>If the full certificate chain was requested, each certificate will include an
1073<code>issuerCertificate</code> property containing an object representing its issuer's
1074certificate.</p>
1075<h5>Certificate object<span><a class="mark" href="#tls_certificate_object" id="tls_certificate_object">#</a></span></h5>
1076<div class="api_metadata">
1077<details class="changelog"><summary>History</summary>
1078<table>
1079<tbody><tr><th>Version</th><th>Changes</th></tr>
1080<tr><td>v11.4.0</td>
1081<td><p>Support Elliptic Curve public key info.</p></td></tr>
1082</tbody></table>
1083</details>
1084</div>
1085<p>A certificate object has properties corresponding to the fields of the
1086certificate.</p>
1087<ul>
1088<li><code>raw</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> The DER encoded X.509 certificate data.</li>
1089<li><code>subject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The certificate subject, described in terms of
1090Country (<code>C:</code>), StateOrProvince (<code>ST</code>), Locality (<code>L</code>), Organization (<code>O</code>),
1091OrganizationalUnit (<code>OU</code>), and CommonName (<code>CN</code>). The CommonName is typically
1092a DNS name with TLS certificates. Example:
1093<code>{C: 'UK', ST: 'BC', L: 'Metro', O: 'Node Fans', OU: 'Docs', CN: 'example.com'}</code>.</li>
1094<li><code>issuer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The certificate issuer, described in the same terms as the
1095<code>subject</code>.</li>
1096<li><code>valid_from</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The date-time the certificate is valid from.</li>
1097<li><code>valid_to</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The date-time the certificate is valid to.</li>
1098<li><code>serialNumber</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The certificate serial number, as a hex string.
1099Example: <code>'B9B0D332A1AA5635'</code>.</li>
1100<li><code>fingerprint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The SHA-1 digest of the DER encoded certificate. It is
1101returned as a <code>:</code> separated hexadecimal string. Example: <code>'2A:7A:C2:DD:...'</code>.</li>
1102<li><code>fingerprint256</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The SHA-256 digest of the DER encoded certificate.
1103It is returned as a <code>:</code> separated hexadecimal string. Example:
1104<code>'2A:7A:C2:DD:...'</code>.</li>
1105<li><code>ext_key_usage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> (Optional) The extended key usage, a set of OIDs.</li>
1106<li><code>subjectaltname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> (Optional) A string containing concatenated names
1107for the subject, an alternative to the <code>subject</code> names.</li>
1108<li><code>infoAccess</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> (Optional) An array describing the AuthorityInfoAccess,
1109used with OCSP.</li>
1110<li><code>issuerCertificate</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> (Optional) The issuer certificate object. For
1111self-signed certificates, this may be a circular reference.</li>
1112</ul>
1113<p>The certificate may contain information about the public key, depending on
1114the key type.</p>
1115<p>For RSA keys, the following properties may be defined:</p>
1116<ul>
1117<li><code>bits</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The RSA bit size. Example: <code>1024</code>.</li>
1118<li><code>exponent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The RSA exponent, as a string in hexadecimal number
1119notation. Example: <code>'0x010001'</code>.</li>
1120<li><code>modulus</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The RSA modulus, as a hexadecimal string. Example:
1121<code>'B56CE45CB7...'</code>.</li>
1122<li><code>pubkey</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> The public key.</li>
1123</ul>
1124<p>For EC keys, the following properties may be defined:</p>
1125<ul>
1126<li><code>pubkey</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> The public key.</li>
1127<li><code>bits</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The key size in bits. Example: <code>256</code>.</li>
1128<li><code>asn1Curve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> (Optional) The ASN.1 name of the OID of the elliptic
1129curve. Well-known curves are identified by an OID. While it is unusual, it is
1130possible that the curve is identified by its mathematical properties, in which
1131case it will not have an OID. Example: <code>'prime256v1'</code>.</li>
1132<li><code>nistCurve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> (Optional) The NIST name for the elliptic curve, if it
1133has one (not all well-known curves have been assigned names by NIST). Example:
1134<code>'P-256'</code>.</li>
1135</ul>
1136<p>Example certificate:</p>
1137<!-- eslint-skip -->
1138<pre><code class="language-js">{ <span class="hljs-attr">subject</span>:
1139   { <span class="hljs-attr">OU</span>: [ <span class="hljs-string">'Domain Control Validated'</span>, <span class="hljs-string">'PositiveSSL Wildcard'</span> ],
1140     <span class="hljs-attr">CN</span>: <span class="hljs-string">'*.nodejs.org'</span> },
1141  <span class="hljs-attr">issuer</span>:
1142   { <span class="hljs-attr">C</span>: <span class="hljs-string">'GB'</span>,
1143     <span class="hljs-attr">ST</span>: <span class="hljs-string">'Greater Manchester'</span>,
1144     <span class="hljs-attr">L</span>: <span class="hljs-string">'Salford'</span>,
1145     <span class="hljs-attr">O</span>: <span class="hljs-string">'COMODO CA Limited'</span>,
1146     <span class="hljs-attr">CN</span>: <span class="hljs-string">'COMODO RSA Domain Validation Secure Server CA'</span> },
1147  <span class="hljs-attr">subjectaltname</span>: <span class="hljs-string">'DNS:*.nodejs.org, DNS:nodejs.org'</span>,
1148  <span class="hljs-attr">infoAccess</span>:
1149   { <span class="hljs-string">'CA Issuers - URI'</span>:
1150      [ <span class="hljs-string">'http://crt.comodoca.com/COMODORSADomainValidationSecureServerCA.crt'</span> ],
1151     <span class="hljs-string">'OCSP - URI'</span>: [ <span class="hljs-string">'http://ocsp.comodoca.com'</span> ] },
1152  <span class="hljs-attr">modulus</span>: <span class="hljs-string">'B56CE45CB740B09A13F64AC543B712FF9EE8E4C284B542A1708A27E82A8D151CA178153E12E6DDA15BF70FFD96CB8A88618641BDFCCA03527E665B70D779C8A349A6F88FD4EF6557180BD4C98192872BCFE3AF56E863C09DDD8BC1EC58DF9D94F914F0369102B2870BECFA1348A0838C9C49BD1C20124B442477572347047506B1FCD658A80D0C44BCC16BC5C5496CFE6E4A8428EF654CD3D8972BF6E5BFAD59C93006830B5EB1056BBB38B53D1464FA6E02BFDF2FF66CD949486F0775EC43034EC2602AEFBF1703AD221DAA2A88353C3B6A688EFE8387811F645CEED7B3FE46E1F8B9F59FAD028F349B9BC14211D5830994D055EEA3D547911E07A0ADDEB8A82B9188E58720D95CD478EEC9AF1F17BE8141BE80906F1A339445A7EB5B285F68039B0F294598A7D1C0005FC22B5271B0752F58CCDEF8C8FD856FB7AE21C80B8A2CE983AE94046E53EDE4CB89F42502D31B5360771C01C80155918637490550E3F555E2EE75CC8C636DDE3633CFEDD62E91BF0F7688273694EEEBA20C2FC9F14A2A435517BC1D7373922463409AB603295CEB0BB53787A334C9CA3CA8B30005C5A62FC0715083462E00719A8FA3ED0A9828C3871360A73F8B04A4FC1E71302844E9BB9940B77E745C9D91F226D71AFCAD4B113AAF68D92B24DDB4A2136B55A1CD1ADF39605B63CB639038ED0F4C987689866743A68769CC55847E4A06D6E2E3F1'</span>,
1153  <span class="hljs-attr">exponent</span>: <span class="hljs-string">'0x10001'</span>,
1154  <span class="hljs-attr">pubkey</span>: &#x3C;Buffer ... >,
1155  valid_from: 'Aug 14 00:00:00 2017 GMT',
1156  valid_to: 'Nov 20 23:59:59 2019 GMT',
1157  fingerprint: '01:02:59:D9:C3:D2:0D:08:F7:82:4E:44:A4:B4:53:C5:E2:3A:87:4D',
1158  fingerprint256: '69:AE:1A:6A:D4:3D:C6:C1:1B:EA:C6:23:DE:BA:2A:14:62:62:93:5C:7A:EA:06:41:9B:0B:BC:87:CE:48:4E:02',
1159  ext_key_usage: [ '1.3.6.1.5.5.7.3.1', '1.3.6.1.5.5.7.3.2' ],
1160  serialNumber: '66593D57F20CBC573E433381B5FEC280',
1161  raw: &#x3C;Buffer ... > }</code></pre>
1162<h4><code>tlsSocket.getPeerFinished()</code><span><a class="mark" href="#tls_tlssocket_getpeerfinished" id="tls_tlssocket_getpeerfinished">#</a></span></h4>
1163<div class="api_metadata">
1164<span>Added in: v9.9.0</span>
1165</div>
1166<ul>
1167<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a> The latest <code>Finished</code> message that is expected
1168or has actually been received from the socket as part of a SSL/TLS handshake,
1169or <code>undefined</code> if there is no <code>Finished</code> message so far.</li>
1170</ul>
1171<p>As the <code>Finished</code> messages are message digests of the complete handshake
1172(with a total of 192 bits for TLS 1.0 and more for SSL 3.0), they can
1173be used for external authentication procedures when the authentication
1174provided by SSL/TLS is not desired or is not enough.</p>
1175<p>Corresponds to the <code>SSL_get_peer_finished</code> routine in OpenSSL and may be used
1176to implement the <code>tls-unique</code> channel binding from <a href="https://tools.ietf.org/html/rfc5929">RFC 5929</a>.</p>
1177<h4><code>tlsSocket.getProtocol()</code><span><a class="mark" href="#tls_tlssocket_getprotocol" id="tls_tlssocket_getprotocol">#</a></span></h4>
1178<div class="api_metadata">
1179<span>Added in: v5.7.0</span>
1180</div>
1181<ul>
1182<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a></li>
1183</ul>
1184<p>Returns a string containing the negotiated SSL/TLS protocol version of the
1185current connection. The value <code>'unknown'</code> will be returned for connected
1186sockets that have not completed the handshaking process. The value <code>null</code> will
1187be returned for server sockets or disconnected client sockets.</p>
1188<p>Protocol versions are:</p>
1189<ul>
1190<li><code>'SSLv3'</code></li>
1191<li><code>'TLSv1'</code></li>
1192<li><code>'TLSv1.1'</code></li>
1193<li><code>'TLSv1.2'</code></li>
1194<li><code>'TLSv1.3'</code></li>
1195</ul>
1196<p>See the OpenSSL <a href="https://www.openssl.org/docs/man1.1.1/man3/SSL_get_version.html"><code>SSL_get_version</code></a> documentation for more information.</p>
1197<h4><code>tlsSocket.getSession()</code><span><a class="mark" href="#tls_tlssocket_getsession" id="tls_tlssocket_getsession">#</a></span></h4>
1198<div class="api_metadata">
1199<span>Added in: v0.11.4</span>
1200</div>
1201<ul>
1202<li><a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
1203</ul>
1204<p>Returns the TLS session data or <code>undefined</code> if no session was
1205negotiated. On the client, the data can be provided to the <code>session</code> option of
1206<a href="#tls_tls_connect_options_callback"><code>tls.connect()</code></a> to resume the connection. On the server, it may be useful
1207for debugging.</p>
1208<p>See <a href="#tls_session_resumption">Session Resumption</a> for more information.</p>
1209<p>Note: <code>getSession()</code> works only for TLSv1.2 and below. For TLSv1.3, applications
1210must use the <a href="#tls_event_session"><code>'session'</code></a> event (it also works for TLSv1.2 and below).</p>
1211<h4><code>tlsSocket.getSharedSigalgs()</code><span><a class="mark" href="#tls_tlssocket_getsharedsigalgs" id="tls_tlssocket_getsharedsigalgs">#</a></span></h4>
1212<div class="api_metadata">
1213<span>Added in: v12.11.0</span>
1214</div>
1215<ul>
1216<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> List of signature algorithms shared between the server and
1217the client in the order of decreasing preference.</li>
1218</ul>
1219<p>See
1220<a href="https://www.openssl.org/docs/man1.1.1/man3/SSL_get_shared_sigalgs.html">SSL_get_shared_sigalgs</a>
1221for more information.</p>
1222<h4><code>tlsSocket.exportKeyingMaterial(length, label[, context])</code><span><a class="mark" href="#tls_tlssocket_exportkeyingmaterial_length_label_context" id="tls_tlssocket_exportkeyingmaterial_length_label_context">#</a></span></h4>
1223<div class="api_metadata">
1224<span>Added in: v13.10.0</span>
1225</div>
1226<ul>
1227<li>
1228<p><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> number of bytes to retrieve from keying material</p>
1229</li>
1230<li>
1231<p><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> an application specific label, typically this will be a
1232value from the
1233<a href="https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#exporter-labels">IANA Exporter Label Registry</a>.</p>
1234</li>
1235<li>
1236<p><code>context</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> Optionally provide a context.</p>
1237</li>
1238<li>
1239<p>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> requested bytes of the keying material</p>
1240</li>
1241</ul>
1242<p>Keying material is used for validations to prevent different kind of attacks in
1243network protocols, for example in the specifications of IEEE 802.1X.</p>
1244<p>Example</p>
1245<pre><code class="language-js"><span class="hljs-keyword">const</span> keyingMaterial = tlsSocket.<span class="hljs-title function_">exportKeyingMaterial</span>(
1246  <span class="hljs-number">128</span>,
1247  <span class="hljs-string">'client finished'</span>);
1248
1249<span class="hljs-comment">/**
1250 Example return value of keyingMaterial:
1251 &#x3C;Buffer 76 26 af 99 c5 56 8e 42 09 91 ef 9f 93 cb ad 6c 7b 65 f8 53 f1 d8 d9
1252    12 5a 33 b8 b5 25 df 7b 37 9f e0 e2 4f b8 67 83 a3 2f cd 5d 41 42 4c 91
1253    74 ef 2c ... 78 more bytes>
1254*/</span></code></pre>
1255<p>See the OpenSSL <a href="https://www.openssl.org/docs/man1.1.1/man3/SSL_export_keying_material.html"><code>SSL_export_keying_material</code></a> documentation for more
1256information.</p>
1257<h4><code>tlsSocket.getTLSTicket()</code><span><a class="mark" href="#tls_tlssocket_gettlsticket" id="tls_tlssocket_gettlsticket">#</a></span></h4>
1258<div class="api_metadata">
1259<span>Added in: v0.11.4</span>
1260</div>
1261<ul>
1262<li><a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
1263</ul>
1264<p>For a client, returns the TLS session ticket if one is available, or
1265<code>undefined</code>. For a server, always returns <code>undefined</code>.</p>
1266<p>It may be useful for debugging.</p>
1267<p>See <a href="#tls_session_resumption">Session Resumption</a> for more information.</p>
1268<h4><code>tlsSocket.isSessionReused()</code><span><a class="mark" href="#tls_tlssocket_issessionreused" id="tls_tlssocket_issessionreused">#</a></span></h4>
1269<div class="api_metadata">
1270<span>Added in: v0.5.6</span>
1271</div>
1272<ul>
1273<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> if the session was reused, <code>false</code> otherwise.</li>
1274</ul>
1275<p>See <a href="#tls_session_resumption">Session Resumption</a> for more information.</p>
1276<h4><code>tlsSocket.localAddress</code><span><a class="mark" href="#tls_tlssocket_localaddress" id="tls_tlssocket_localaddress">#</a></span></h4>
1277<div class="api_metadata">
1278<span>Added in: v0.11.4</span>
1279</div>
1280<ul>
1281<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1282</ul>
1283<p>Returns the string representation of the local IP address.</p>
1284<h4><code>tlsSocket.localPort</code><span><a class="mark" href="#tls_tlssocket_localport" id="tls_tlssocket_localport">#</a></span></h4>
1285<div class="api_metadata">
1286<span>Added in: v0.11.4</span>
1287</div>
1288<ul>
1289<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1290</ul>
1291<p>Returns the numeric representation of the local port.</p>
1292<h4><code>tlsSocket.remoteAddress</code><span><a class="mark" href="#tls_tlssocket_remoteaddress" id="tls_tlssocket_remoteaddress">#</a></span></h4>
1293<div class="api_metadata">
1294<span>Added in: v0.11.4</span>
1295</div>
1296<ul>
1297<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1298</ul>
1299<p>Returns the string representation of the remote IP address. For example,
1300<code>'74.125.127.100'</code> or <code>'2001:4860:a005::68'</code>.</p>
1301<h4><code>tlsSocket.remoteFamily</code><span><a class="mark" href="#tls_tlssocket_remotefamily" id="tls_tlssocket_remotefamily">#</a></span></h4>
1302<div class="api_metadata">
1303<span>Added in: v0.11.4</span>
1304</div>
1305<ul>
1306<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1307</ul>
1308<p>Returns the string representation of the remote IP family. <code>'IPv4'</code> or <code>'IPv6'</code>.</p>
1309<h4><code>tlsSocket.remotePort</code><span><a class="mark" href="#tls_tlssocket_remoteport" id="tls_tlssocket_remoteport">#</a></span></h4>
1310<div class="api_metadata">
1311<span>Added in: v0.11.4</span>
1312</div>
1313<ul>
1314<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1315</ul>
1316<p>Returns the numeric representation of the remote port. For example, <code>443</code>.</p>
1317<h4><code>tlsSocket.renegotiate(options, callback)</code><span><a class="mark" href="#tls_tlssocket_renegotiate_options_callback" id="tls_tlssocket_renegotiate_options_callback">#</a></span></h4>
1318<div class="api_metadata">
1319<span>Added in: v0.11.8</span>
1320</div>
1321<ul>
1322<li>
1323<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></p>
1324<ul>
1325<li><code>rejectUnauthorized</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If not <code>false</code>, the server certificate is
1326verified against the list of supplied CAs. An <code>'error'</code> event is emitted if
1327verification fails; <code>err.code</code> contains the OpenSSL error code. <strong>Default:</strong>
1328<code>true</code>.</li>
1329<li><code>requestCert</code></li>
1330</ul>
1331</li>
1332<li>
1333<p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> If <code>renegotiate()</code> returned <code>true</code>, callback is
1334attached once to the <code>'secure'</code> event. If <code>renegotiate()</code> returned <code>false</code>,
1335<code>callback</code> will be called in the next tick with an error, unless the
1336<code>tlsSocket</code> has been destroyed, in which case <code>callback</code> will not be called
1337at all.</p>
1338</li>
1339<li>
1340<p>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> if renegotiation was initiated, <code>false</code> otherwise.</p>
1341</li>
1342</ul>
1343<p>The <code>tlsSocket.renegotiate()</code> method initiates a TLS renegotiation process.
1344Upon completion, the <code>callback</code> function will be passed a single argument
1345that is either an <code>Error</code> (if the request failed) or <code>null</code>.</p>
1346<p>This method can be used to request a peer's certificate after the secure
1347connection has been established.</p>
1348<p>When running as the server, the socket will be destroyed with an error after
1349<code>handshakeTimeout</code> timeout.</p>
1350<p>For TLSv1.3, renegotiation cannot be initiated, it is not supported by the
1351protocol.</p>
1352<h4><code>tlsSocket.setMaxSendFragment(size)</code><span><a class="mark" href="#tls_tlssocket_setmaxsendfragment_size" id="tls_tlssocket_setmaxsendfragment_size">#</a></span></h4>
1353<div class="api_metadata">
1354<span>Added in: v0.11.11</span>
1355</div>
1356<ul>
1357<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The maximum TLS fragment size. The maximum value is <code>16384</code>.
1358<strong>Default:</strong> <code>16384</code>.</li>
1359<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1360</ul>
1361<p>The <code>tlsSocket.setMaxSendFragment()</code> method sets the maximum TLS fragment size.
1362Returns <code>true</code> if setting the limit succeeded; <code>false</code> otherwise.</p>
1363<p>Smaller fragment sizes decrease the buffering latency on the client: larger
1364fragments are buffered by the TLS layer until the entire fragment is received
1365and its integrity is verified; large fragments can span multiple roundtrips
1366and their processing can be delayed due to packet loss or reordering. However,
1367smaller fragments add extra TLS framing bytes and CPU overhead, which may
1368decrease overall server throughput.</p>
1369</section><section><h3><code>tls.checkServerIdentity(hostname, cert)</code><span><a class="mark" href="#tls_tls_checkserveridentity_hostname_cert" id="tls_tls_checkserveridentity_hostname_cert">#</a></span></h3>
1370<div class="api_metadata">
1371<details class="changelog"><summary>History</summary>
1372<table>
1373<tbody><tr><th>Version</th><th>Changes</th></tr>
1374<tr><td>v14.18.3</td>
1375<td><p>Support for <code>uniformResourceIdentifier</code> subject alternative names has been disabled in response to CVE-2021-44531.</p></td></tr>
1376<tr><td>v0.8.4</td>
1377<td><p><span>Added in: v0.8.4</span></p></td></tr>
1378</tbody></table>
1379</details>
1380</div>
1381<ul>
1382<li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The host name or IP address to verify the certificate
1383against.</li>
1384<li><code>cert</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> A <a href="#tls_certificate_object">certificate object</a> representing the peer's certificate.</li>
1385<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
1386</ul>
1387<p>Verifies the certificate <code>cert</code> is issued to <code>hostname</code>.</p>
1388<p>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> object, populating it with <code>reason</code>, <code>host</code>, and <code>cert</code> on
1389failure. On success, returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a>.</p>
1390<p>This function can be overwritten by providing alternative function as part of
1391the <code>options.checkServerIdentity</code> option passed to <code>tls.connect()</code>. The
1392overwriting function can call <code>tls.checkServerIdentity()</code> of course, to augment
1393the checks done with additional verification.</p>
1394<p>This function is only called if the certificate passed all other checks, such as
1395being issued by trusted CA (<code>options.ca</code>).</p>
1396<p>Earlier versions of Node.js incorrectly accepted certificates for a given
1397<code>hostname</code> if a matching <code>uniformResourceIdentifier</code> subject alternative name
1398was present (see <a href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-44531">CVE-2021-44531</a>). Applications that wish to accept
1399<code>uniformResourceIdentifier</code> subject alternative names can use a custom
1400<code>options.checkServerIdentity</code> function that implements the desired behavior.</p>
1401</section><section><h3><code>tls.connect(options[, callback])</code><span><a class="mark" href="#tls_tls_connect_options_callback" id="tls_tls_connect_options_callback">#</a></span></h3>
1402<div class="api_metadata">
1403<details class="changelog"><summary>History</summary>
1404<table>
1405<tbody><tr><th>Version</th><th>Changes</th></tr>
1406<tr><td>v14.18.0</td>
1407<td><p>Added <code>onread</code> option.</p></td></tr>
1408<tr><td>v14.1.0</td>
1409<td><p>The <code>highWaterMark</code> option is accepted now.</p></td></tr>
1410<tr><td>v13.6.0, v12.16.0</td>
1411<td><p>The <code>pskCallback</code> option is now supported.</p></td></tr>
1412<tr><td>v12.9.0</td>
1413<td><p>Support the <code>allowHalfOpen</code> option.</p></td></tr>
1414<tr><td>v12.4.0</td>
1415<td><p>The <code>hints</code> option is now supported.</p></td></tr>
1416<tr><td>v12.2.0</td>
1417<td><p>The <code>enableTrace</code> option is now supported.</p></td></tr>
1418<tr><td>v11.8.0, v10.16.0</td>
1419<td><p>The <code>timeout</code> option is supported now.</p></td></tr>
1420<tr><td>v8.0.0</td>
1421<td><p>The <code>lookup</code> option is supported now.</p></td></tr>
1422<tr><td>v8.0.0</td>
1423<td><p>The <code>ALPNProtocols</code> option can be a <code>TypedArray</code> or <code>DataView</code> now.</p></td></tr>
1424<tr><td>v5.0.0</td>
1425<td><p>ALPN options are supported now.</p></td></tr>
1426<tr><td>v5.3.0, v4.7.0</td>
1427<td><p>The <code>secureContext</code> option is supported now.</p></td></tr>
1428<tr><td>v0.11.3</td>
1429<td><p><span>Added in: v0.11.3</span></p></td></tr>
1430</tbody></table>
1431</details>
1432</div>
1433<ul>
1434<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1435<ul>
1436<li><code>enableTrace</code>: See <a href="#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer()</code></a></li>
1437<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Host the client should connect to. <strong>Default:</strong>
1438<code>'localhost'</code>.</li>
1439<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Port the client should connect to.</li>
1440<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Creates Unix socket connection to path. If this option is
1441specified, <code>host</code> and <code>port</code> are ignored.</li>
1442<li><code>socket</code> <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a> Establish secure connection on a given socket
1443rather than creating a new socket. Typically, this is an instance of
1444<a href="net.html#net_class_net_socket"><code>net.Socket</code></a>, but any <code>Duplex</code> stream is allowed.
1445If this option is specified, <code>path</code>, <code>host</code> and <code>port</code> are ignored,
1446except for certificate validation. Usually, a socket is already connected
1447when passed to <code>tls.connect()</code>, but it can be connected later.
1448Connection/disconnection/destruction of <code>socket</code> is the user's
1449responsibility; calling <code>tls.connect()</code> will not cause <code>net.connect()</code> to be
1450called.</li>
1451<li><code>allowHalfOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If set to <code>false</code>, then the socket will
1452automatically end the writable side when the readable side ends. If the
1453<code>socket</code> option is set, this option has no effect. See the <code>allowHalfOpen</code>
1454option of <a href="net.html#net_class_net_socket"><code>net.Socket</code></a> for details. <strong>Default:</strong> <code>false</code>.</li>
1455<li><code>rejectUnauthorized</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If not <code>false</code>, the server certificate is
1456verified against the list of supplied CAs. An <code>'error'</code> event is emitted if
1457verification fails; <code>err.code</code> contains the OpenSSL error code. <strong>Default:</strong>
1458<code>true</code>.</li>
1459<li><code>pskCallback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
1460<ul>
1461<li>hint: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> optional message sent from the server to help client
1462decide which identity to use during negotiation.
1463Always <code>null</code> if TLS 1.3 is used.</li>
1464<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> in the form
1465<code>{ psk: &#x3C;Buffer|TypedArray|DataView>, identity: &#x3C;string> }</code>
1466or <code>null</code> to stop the negotiation process. <code>psk</code> must be
1467compatible with the selected cipher's digest.
1468<code>identity</code> must use UTF-8 encoding.</li>
1469</ul>
1470When negotiating TLS-PSK (pre-shared keys), this function is called
1471with optional identity <code>hint</code> provided by the server or <code>null</code>
1472in case of TLS 1.3 where <code>hint</code> was removed.
1473It will be necessary to provide a custom <code>tls.checkServerIdentity()</code>
1474for the connection as the default one will try to check host name/IP
1475of the server against the certificate but that's not applicable for PSK
1476because there won't be a certificate present.
1477More information can be found in the <a href="https://tools.ietf.org/html/rfc4279">RFC 4279</a>.</li>
1478<li><code>ALPNProtocols</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView[]></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a>
1479An array of strings, <code>Buffer</code>s or <code>TypedArray</code>s or <code>DataView</code>s, or a
1480single <code>Buffer</code> or <code>TypedArray</code> or <code>DataView</code> containing the supported ALPN
1481protocols. <code>Buffer</code>s should have the format <code>[len][name][len][name]...</code>
1482e.g. <code>'\x08http/1.1\x08http/1.0'</code>, where the <code>len</code> byte is the length of the
1483next protocol name. Passing an array is usually much simpler, e.g.
1484<code>['http/1.1', 'http/1.0']</code>. Protocols earlier in the list have higher
1485preference than those later.</li>
1486<li><code>servername</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Server name for the SNI (Server Name Indication) TLS
1487extension. It is the name of the host being connected to, and must be a host
1488name, and not an IP address. It can be used by a multi-homed server to
1489choose the correct certificate to present to the client, see the
1490<code>SNICallback</code> option to <a href="#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer()</code></a>.</li>
1491<li><code>checkServerIdentity(servername, cert)</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A callback function
1492to be used (instead of the builtin <code>tls.checkServerIdentity()</code> function)
1493when checking the server's host name (or the provided <code>servername</code> when
1494explicitly set) against the certificate. This should return an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> if
1495verification fails. The method should return <code>undefined</code> if the <code>servername</code>
1496and <code>cert</code> are verified.</li>
1497<li><code>session</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> A <code>Buffer</code> instance, containing TLS session.</li>
1498<li><code>minDHSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Minimum size of the DH parameter in bits to accept a
1499TLS connection. When a server offers a DH parameter with a size less
1500than <code>minDHSize</code>, the TLS connection is destroyed and an error is thrown.
1501<strong>Default:</strong> <code>1024</code>.</li>
1502<li><code>highWaterMark</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Consistent with the readable stream <code>highWaterMark</code> parameter.
1503<strong>Default:</strong> <code>16 * 1024</code>.</li>
1504<li><code>secureContext</code>: TLS context object created with
1505<a href="#tls_tls_createsecurecontext_options"><code>tls.createSecureContext()</code></a>. If a <code>secureContext</code> is <em>not</em> provided, one
1506will be created by passing the entire <code>options</code> object to
1507<code>tls.createSecureContext()</code>.</li>
1508<li><code>onread</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> If the <code>socket</code> option is missing, incoming data is
1509stored in a single <code>buffer</code> and passed to the supplied <code>callback</code> when
1510data arrives on the socket, otherwise the option is ignored. See the
1511<code>onread</code> option of <a href="net.html#net_class_net_socket"><code>net.Socket</code></a> for details.</li>
1512<li>...: <a href="#tls_tls_createsecurecontext_options"><code>tls.createSecureContext()</code></a> options that are used if the
1513<code>secureContext</code> option is missing, otherwise they are ignored.</li>
1514<li>...: Any <a href="net.html#net_socket_connect_options_connectlistener"><code>socket.connect()</code></a> option not already listed.</li>
1515</ul>
1516</li>
1517<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
1518<li>Returns: <a href="tls.html#tls_class_tls_tlssocket" class="type">&#x3C;tls.TLSSocket></a></li>
1519</ul>
1520<p>The <code>callback</code> function, if specified, will be added as a listener for the
1521<a href="#tls_event_secureconnect"><code>'secureConnect'</code></a> event.</p>
1522<p><code>tls.connect()</code> returns a <a href="#tls_class_tls_tlssocket"><code>tls.TLSSocket</code></a> object.</p>
1523<p>Unlike the <code>https</code> API, <code>tls.connect()</code> does not enable the
1524SNI (Server Name Indication) extension by default, which may cause some
1525servers to return an incorrect certificate or reject the connection
1526altogether. To enable SNI, set the <code>servername</code> option in addition
1527to <code>host</code>.</p>
1528<p>The following illustrates a client for the echo server example from
1529<a href="#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer()</code></a>:</p>
1530<pre><code class="language-js"><span class="hljs-comment">// Assumes an echo server that is listening on port 8000.</span>
1531<span class="hljs-keyword">const</span> tls = <span class="hljs-built_in">require</span>(<span class="hljs-string">'tls'</span>);
1532<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
1533
1534<span class="hljs-keyword">const</span> options = {
1535  <span class="hljs-comment">// Necessary only if the server requires client certificate authentication.</span>
1536  <span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'client-key.pem'</span>),
1537  <span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'client-cert.pem'</span>),
1538
1539  <span class="hljs-comment">// Necessary only if the server uses a self-signed certificate.</span>
1540  <span class="hljs-attr">ca</span>: [ fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'server-cert.pem'</span>) ],
1541
1542  <span class="hljs-comment">// Necessary only if the server's cert isn't for "localhost".</span>
1543  <span class="hljs-attr">checkServerIdentity</span>: <span class="hljs-function">() =></span> { <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>; },
1544};
1545
1546<span class="hljs-keyword">const</span> socket = tls.<span class="hljs-title function_">connect</span>(<span class="hljs-number">8000</span>, options, <span class="hljs-function">() =></span> {
1547  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'client connected'</span>,
1548              socket.<span class="hljs-property">authorized</span> ? <span class="hljs-string">'authorized'</span> : <span class="hljs-string">'unauthorized'</span>);
1549  process.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">pipe</span>(socket);
1550  process.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">resume</span>();
1551});
1552socket.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
1553socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> {
1554  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data);
1555});
1556socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
1557  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'server ends connection'</span>);
1558});</code></pre>
1559</section><section><h3><code>tls.connect(path[, options][, callback])</code><span><a class="mark" href="#tls_tls_connect_path_options_callback" id="tls_tls_connect_path_options_callback">#</a></span></h3>
1560<div class="api_metadata">
1561<span>Added in: v0.11.3</span>
1562</div>
1563<ul>
1564<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Default value for <code>options.path</code>.</li>
1565<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> See <a href="#tls_tls_connect_options_callback"><code>tls.connect()</code></a>.</li>
1566<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> See <a href="#tls_tls_connect_options_callback"><code>tls.connect()</code></a>.</li>
1567<li>Returns: <a href="tls.html#tls_class_tls_tlssocket" class="type">&#x3C;tls.TLSSocket></a></li>
1568</ul>
1569<p>Same as <a href="#tls_tls_connect_options_callback"><code>tls.connect()</code></a> except that <code>path</code> can be provided
1570as an argument instead of an option.</p>
1571<p>A path option, if specified, will take precedence over the path argument.</p>
1572</section><section><h3><code>tls.connect(port[, host][, options][, callback])</code><span><a class="mark" href="#tls_tls_connect_port_host_options_callback" id="tls_tls_connect_port_host_options_callback">#</a></span></h3>
1573<div class="api_metadata">
1574<span>Added in: v0.11.3</span>
1575</div>
1576<ul>
1577<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Default value for <code>options.port</code>.</li>
1578<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Default value for <code>options.host</code>.</li>
1579<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> See <a href="#tls_tls_connect_options_callback"><code>tls.connect()</code></a>.</li>
1580<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> See <a href="#tls_tls_connect_options_callback"><code>tls.connect()</code></a>.</li>
1581<li>Returns: <a href="tls.html#tls_class_tls_tlssocket" class="type">&#x3C;tls.TLSSocket></a></li>
1582</ul>
1583<p>Same as <a href="#tls_tls_connect_options_callback"><code>tls.connect()</code></a> except that <code>port</code> and <code>host</code> can be provided
1584as arguments instead of options.</p>
1585<p>A port or host option, if specified, will take precedence over any port or host
1586argument.</p>
1587</section><section><h3><code>tls.createSecureContext([options])</code><span><a class="mark" href="#tls_tls_createsecurecontext_options" id="tls_tls_createsecurecontext_options">#</a></span></h3>
1588<div class="api_metadata">
1589<details class="changelog"><summary>History</summary>
1590<table>
1591<tbody><tr><th>Version</th><th>Changes</th></tr>
1592<tr><td>v12.12.0</td>
1593<td><p>Added <code>privateKeyIdentifier</code> and <code>privateKeyEngine</code> options to get private key from an OpenSSL engine.</p></td></tr>
1594<tr><td>v12.11.0</td>
1595<td><p>Added <code>sigalgs</code> option to override supported signature algorithms.</p></td></tr>
1596<tr><td>v12.0.0</td>
1597<td><p>TLSv1.3 support added.</p></td></tr>
1598<tr><td>v11.5.0</td>
1599<td><p>The <code>ca:</code> option now supports <code>BEGIN TRUSTED CERTIFICATE</code>.</p></td></tr>
1600<tr><td>v11.4.0, v10.16.0</td>
1601<td><p>The <code>minVersion</code> and <code>maxVersion</code> can be used to restrict the allowed TLS protocol versions.</p></td></tr>
1602<tr><td>v10.0.0</td>
1603<td><p>The <code>ecdhCurve</code> cannot be set to <code>false</code> anymore due to a change in OpenSSL.</p></td></tr>
1604<tr><td>v9.3.0</td>
1605<td><p>The <code>options</code> parameter can now include <code>clientCertEngine</code>.</p></td></tr>
1606<tr><td>v9.0.0</td>
1607<td><p>The <code>ecdhCurve</code> option can now be multiple <code>':'</code> separated curve names or <code>'auto'</code>.</p></td></tr>
1608<tr><td>v7.3.0</td>
1609<td><p>If the <code>key</code> option is an array, individual entries do not need a <code>passphrase</code> property anymore. <code>Array</code> entries can also just be <code>string</code>s or <code>Buffer</code>s now.</p></td></tr>
1610<tr><td>v5.2.0</td>
1611<td><p>The <code>ca</code> option can now be a single string containing multiple CA certificates.</p></td></tr>
1612<tr><td>v0.11.13</td>
1613<td><p><span>Added in: v0.11.13</span></p></td></tr>
1614</tbody></table>
1615</details>
1616</div>
1617<ul>
1618<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1619<ul>
1620<li><code>ca</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer[]></a> Optionally override the trusted CA
1621certificates. Default is to trust the well-known CAs curated by Mozilla.
1622Mozilla's CAs are completely replaced when CAs are explicitly specified
1623using this option. The value can be a string or <code>Buffer</code>, or an <code>Array</code> of
1624strings and/or <code>Buffer</code>s. Any string or <code>Buffer</code> can contain multiple PEM
1625CAs concatenated together. The peer's certificate must be chainable to a CA
1626trusted by the server for the connection to be authenticated. When using
1627certificates that are not chainable to a well-known CA, the certificate's CA
1628must be explicitly specified as a trusted or the connection will fail to
1629authenticate.
1630If the peer uses a certificate that doesn't match or chain to one of the
1631default CAs, use the <code>ca</code> option to provide a CA certificate that the peer's
1632certificate can match or chain to.
1633For self-signed certificates, the certificate is its own CA, and must be
1634provided.
1635For PEM encoded certificates, supported types are "TRUSTED CERTIFICATE",
1636"X509 CERTIFICATE", and "CERTIFICATE".
1637See also <a href="#tls_tls_rootcertificates"><code>tls.rootCertificates</code></a>.</li>
1638<li><code>cert</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer[]></a> Cert chains in PEM format. One cert
1639chain should be provided per private key. Each cert chain should consist of
1640the PEM formatted certificate for a provided private <code>key</code>, followed by the
1641PEM formatted intermediate certificates (if any), in order, and not
1642including the root CA (the root CA must be pre-known to the peer, see <code>ca</code>).
1643When providing multiple cert chains, they do not have to be in the same
1644order as their private keys in <code>key</code>. If the intermediate certificates are
1645not provided, the peer will not be able to validate the certificate, and the
1646handshake will fail.</li>
1647<li><code>sigalgs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Colon-separated list of supported signature algorithms.
1648The list can contain digest algorithms (<code>SHA256</code>, <code>MD5</code> etc.), public key
1649algorithms (<code>RSA-PSS</code>, <code>ECDSA</code> etc.), combination of both (e.g
1650'RSA+SHA384') or TLS v1.3 scheme names (e.g. <code>rsa_pss_pss_sha512</code>).
1651See <a href="https://www.openssl.org/docs/man1.1.1/man3/SSL_CTX_set1_sigalgs_list.html">OpenSSL man pages</a>
1652for more info.</li>
1653<li><code>ciphers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Cipher suite specification, replacing the default. For
1654more information, see <a href="#tls_modifying_the_default_tls_cipher_suite">modifying the default cipher suite</a>. Permitted
1655ciphers can be obtained via <a href="#tls_tls_getciphers"><code>tls.getCiphers()</code></a>. Cipher names must be
1656uppercased in order for OpenSSL to accept them.</li>
1657<li><code>clientCertEngine</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Name of an OpenSSL engine which can provide the
1658client certificate.</li>
1659<li><code>crl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer[]></a> PEM formatted CRLs (Certificate
1660Revocation Lists).</li>
1661<li><code>dhparam</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> Diffie-Hellman parameters, required for
1662<a href="#tls_perfect_forward_secrecy">perfect forward secrecy</a>. Use <code>openssl dhparam</code> to create the parameters.
1663The key length must be greater than or equal to 1024 bits or else an error
1664will be thrown. Although 1024 bits is permissible, use 2048 bits or larger
1665for stronger security. If omitted or invalid, the parameters are silently
1666discarded and DHE ciphers will not be available.</li>
1667<li><code>ecdhCurve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> A string describing a named curve or a colon separated
1668list of curve NIDs or names, for example <code>P-521:P-384:P-256</code>, to use for
1669ECDH key agreement. Set to <code>auto</code> to select the
1670curve automatically. Use <a href="crypto.html#crypto_crypto_getcurves"><code>crypto.getCurves()</code></a> to obtain a list of
1671available curve names. On recent releases, <code>openssl ecparam -list_curves</code>
1672will also display the name and description of each available elliptic curve.
1673<strong>Default:</strong> <a href="#tls_tls_default_ecdh_curve"><code>tls.DEFAULT_ECDH_CURVE</code></a>.</li>
1674<li><code>honorCipherOrder</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Attempt to use the server's cipher suite
1675preferences instead of the client's. When <code>true</code>, causes
1676<code>SSL_OP_CIPHER_SERVER_PREFERENCE</code> to be set in <code>secureOptions</code>, see
1677<a href="crypto.html#crypto_openssl_options">OpenSSL Options</a> for more information.</li>
1678<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object[]></a> Private keys in PEM format.
1679PEM allows the option of private keys being encrypted. Encrypted keys will
1680be decrypted with <code>options.passphrase</code>. Multiple keys using different
1681algorithms can be provided either as an array of unencrypted key strings or
1682buffers, or an array of objects in the form
1683<code>{pem: &#x3C;string|buffer>[, passphrase: &#x3C;string>]}</code>. The object form can only
1684occur in an array. <code>object.passphrase</code> is optional. Encrypted keys will be
1685decrypted with <code>object.passphrase</code> if provided, or <code>options.passphrase</code> if
1686it is not.</li>
1687<li><code>privateKeyEngine</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Name of an OpenSSL engine to get private key
1688from. Should be used together with <code>privateKeyIdentifier</code>.</li>
1689<li><code>privateKeyIdentifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Identifier of a private key managed by
1690an OpenSSL engine. Should be used together with <code>privateKeyEngine</code>.
1691Should not be set together with <code>key</code>, because both options define a
1692private key in different ways.</li>
1693<li><code>maxVersion</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Optionally set the maximum TLS version to allow. One
1694of <code>'TLSv1.3'</code>, <code>'TLSv1.2'</code>, <code>'TLSv1.1'</code>, or <code>'TLSv1'</code>. Cannot be specified
1695along with the <code>secureProtocol</code> option; use one or the other.
1696<strong>Default:</strong> <a href="#tls_tls_default_max_version"><code>tls.DEFAULT_MAX_VERSION</code></a>.</li>
1697<li><code>minVersion</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Optionally set the minimum TLS version to allow. One
1698of <code>'TLSv1.3'</code>, <code>'TLSv1.2'</code>, <code>'TLSv1.1'</code>, or <code>'TLSv1'</code>. Cannot be specified
1699along with the <code>secureProtocol</code> option; use one or the other. Avoid
1700setting to less than TLSv1.2, but it may be required for
1701interoperability.
1702<strong>Default:</strong> <a href="#tls_tls_default_min_version"><code>tls.DEFAULT_MIN_VERSION</code></a>.</li>
1703<li><code>passphrase</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Shared passphrase used for a single private key and/or
1704a PFX.</li>
1705<li><code>pfx</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object[]></a> PFX or PKCS12 encoded
1706private key and certificate chain. <code>pfx</code> is an alternative to providing
1707<code>key</code> and <code>cert</code> individually. PFX is usually encrypted, if it is,
1708<code>passphrase</code> will be used to decrypt it. Multiple PFX can be provided either
1709as an array of unencrypted PFX buffers, or an array of objects in the form
1710<code>{buf: &#x3C;string|buffer>[, passphrase: &#x3C;string>]}</code>. The object form can only
1711occur in an array. <code>object.passphrase</code> is optional. Encrypted PFX will be
1712decrypted with <code>object.passphrase</code> if provided, or <code>options.passphrase</code> if
1713it is not.</li>
1714<li><code>secureOptions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Optionally affect the OpenSSL protocol behavior,
1715which is not usually necessary. This should be used carefully if at all!
1716Value is a numeric bitmask of the <code>SSL_OP_*</code> options from
1717<a href="crypto.html#crypto_openssl_options">OpenSSL Options</a>.</li>
1718<li><code>secureProtocol</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Legacy mechanism to select the TLS protocol
1719version to use, it does not support independent control of the minimum and
1720maximum version, and does not support limiting the protocol to TLSv1.3. Use
1721<code>minVersion</code> and <code>maxVersion</code> instead. The possible values are listed as
1722<a href="https://www.openssl.org/docs/man1.1.1/man7/ssl.html#Dealing-with-Protocol-Methods">SSL_METHODS</a>, use the function names as strings. For example, use
1723<code>'TLSv1_1_method'</code> to force TLS version 1.1, or <code>'TLS_method'</code> to allow any
1724TLS protocol version up to TLSv1.3. It is not recommended to use TLS
1725versions less than 1.2, but it may be required for interoperability.
1726<strong>Default:</strong> none, see <code>minVersion</code>.</li>
1727<li><code>sessionIdContext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Opaque identifier used by servers to ensure
1728session state is not shared between applications. Unused by clients.</li>
1729<li><code>ticketKeys</code>: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> 48-bytes of cryptographically strong pseudo-random
1730data. See <a href="#tls_session_resumption">Session Resumption</a> for more information.</li>
1731<li><code>sessionTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of seconds after which a TLS session
1732created by the server will no longer be resumable. See
1733<a href="#tls_session_resumption">Session Resumption</a> for more information. <strong>Default:</strong> <code>300</code>.</li>
1734</ul>
1735</li>
1736</ul>
1737<p><a href="#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer()</code></a> sets the default value of the <code>honorCipherOrder</code> option
1738to <code>true</code>, other APIs that create secure contexts leave it unset.</p>
1739<p><a href="#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer()</code></a> uses a 128 bit truncated SHA1 hash value generated
1740from <code>process.argv</code> as the default value of the <code>sessionIdContext</code> option, other
1741APIs that create secure contexts have no default value.</p>
1742<p>The <code>tls.createSecureContext()</code> method creates a <code>SecureContext</code> object. It is
1743usable as an argument to several <code>tls</code> APIs, such as <a href="#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer()</code></a>
1744and <a href="#tls_server_addcontext_hostname_context"><code>server.addContext()</code></a>, but has no public methods.</p>
1745<p>A key is <em>required</em> for ciphers that use certificates. Either <code>key</code> or
1746<code>pfx</code> can be used to provide it.</p>
1747<p>If the <code>ca</code> option is not given, then Node.js will default to using
1748<a href="https://hg.mozilla.org/mozilla-central/raw-file/tip/security/nss/lib/ckfw/builtins/certdata.txt">Mozilla's publicly trusted list of CAs</a>.</p>
1749</section><section><h3><code>tls.createSecurePair([context][, isServer][, requestCert][, rejectUnauthorized][, options])</code><span><a class="mark" href="#tls_tls_createsecurepair_context_isserver_requestcert_rejectunauthorized_options" id="tls_tls_createsecurepair_context_isserver_requestcert_rejectunauthorized_options">#</a></span></h3>
1750<div class="api_metadata">
1751<details class="changelog"><summary>History</summary>
1752<table>
1753<tbody><tr><th>Version</th><th>Changes</th></tr>
1754<tr><td>v5.0.0</td>
1755<td><p>ALPN options are supported now.</p></td></tr>
1756<tr><td>v0.11.3</td>
1757<td><p><span>Deprecated since: v0.11.3</span></p></td></tr>
1758<tr><td>v0.3.2</td>
1759<td><p><span>Added in: v0.3.2</span></p></td></tr>
1760</tbody></table>
1761</details>
1762</div>
1763<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#tls_class_tls_tlssocket"><code>tls.TLSSocket</code></a> instead.</div><p></p>
1764<ul>
1765<li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> A secure context object as returned by
1766<code>tls.createSecureContext()</code></li>
1767<li><code>isServer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> to specify that this TLS connection should be
1768opened as a server.</li>
1769<li><code>requestCert</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> to specify whether a server should request a
1770certificate from a connecting client. Only applies when <code>isServer</code> is <code>true</code>.</li>
1771<li><code>rejectUnauthorized</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If not <code>false</code> a server automatically reject
1772clients with invalid certificates. Only applies when <code>isServer</code> is <code>true</code>.</li>
1773<li><code>options</code>
1774<ul>
1775<li><code>enableTrace</code>: See <a href="#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer()</code></a></li>
1776<li><code>secureContext</code>: A TLS context object from <a href="#tls_tls_createsecurecontext_options"><code>tls.createSecureContext()</code></a></li>
1777<li><code>isServer</code>: If <code>true</code> the TLS socket will be instantiated in server-mode.
1778<strong>Default:</strong> <code>false</code>.</li>
1779<li><code>server</code> <a href="net.html#net_class_net_server" class="type">&#x3C;net.Server></a> A <a href="net.html#net_class_net_server"><code>net.Server</code></a> instance</li>
1780<li><code>requestCert</code>: See <a href="#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer()</code></a></li>
1781<li><code>rejectUnauthorized</code>: See <a href="#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer()</code></a></li>
1782<li><code>ALPNProtocols</code>: See <a href="#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer()</code></a></li>
1783<li><code>SNICallback</code>: See <a href="#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer()</code></a></li>
1784<li><code>session</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> A <code>Buffer</code> instance containing a TLS session.</li>
1785<li><code>requestOCSP</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, specifies that the OCSP status request
1786extension will be added to the client hello and an <code>'OCSPResponse'</code> event
1787will be emitted on the socket before establishing a secure communication.</li>
1788</ul>
1789</li>
1790</ul>
1791<p>Creates a new secure pair object with two streams, one of which reads and writes
1792the encrypted data and the other of which reads and writes the cleartext data.
1793Generally, the encrypted stream is piped to/from an incoming encrypted data
1794stream and the cleartext one is used as a replacement for the initial encrypted
1795stream.</p>
1796<p><code>tls.createSecurePair()</code> returns a <code>tls.SecurePair</code> object with <code>cleartext</code> and
1797<code>encrypted</code> stream properties.</p>
1798<p>Using <code>cleartext</code> has the same API as <a href="#tls_class_tls_tlssocket"><code>tls.TLSSocket</code></a>.</p>
1799<p>The <code>tls.createSecurePair()</code> method is now deprecated in favor of
1800<code>tls.TLSSocket()</code>. For example, the code:</p>
1801<pre><code class="language-js">pair = tls.<span class="hljs-title function_">createSecurePair</span>(<span class="hljs-comment">/* ... */</span>);
1802pair.<span class="hljs-property">encrypted</span>.<span class="hljs-title function_">pipe</span>(socket);
1803socket.<span class="hljs-title function_">pipe</span>(pair.<span class="hljs-property">encrypted</span>);</code></pre>
1804<p>can be replaced by:</p>
1805<pre><code class="language-js">secureSocket = tls.<span class="hljs-title function_">TLSSocket</span>(socket, options);</code></pre>
1806<p>where <code>secureSocket</code> has the same API as <code>pair.cleartext</code>.</p>
1807</section><section><h3><code>tls.createServer([options][, secureConnectionListener])</code><span><a class="mark" href="#tls_tls_createserver_options_secureconnectionlistener" id="tls_tls_createserver_options_secureconnectionlistener">#</a></span></h3>
1808<div class="api_metadata">
1809<details class="changelog"><summary>History</summary>
1810<table>
1811<tbody><tr><th>Version</th><th>Changes</th></tr>
1812<tr><td>v12.3.0</td>
1813<td><p>The <code>options</code> parameter now supports <code>net.createServer()</code> options.</p></td></tr>
1814<tr><td>v9.3.0</td>
1815<td><p>The <code>options</code> parameter can now include <code>clientCertEngine</code>.</p></td></tr>
1816<tr><td>v8.0.0</td>
1817<td><p>The <code>ALPNProtocols</code> option can be a <code>TypedArray</code> or <code>DataView</code> now.</p></td></tr>
1818<tr><td>v5.0.0</td>
1819<td><p>ALPN options are supported now.</p></td></tr>
1820<tr><td>v0.3.2</td>
1821<td><p><span>Added in: v0.3.2</span></p></td></tr>
1822</tbody></table>
1823</details>
1824</div>
1825<ul>
1826<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1827<ul>
1828<li><code>ALPNProtocols</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView[]></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a>
1829An array of strings, <code>Buffer</code>s or <code>TypedArray</code>s or <code>DataView</code>s, or a single
1830<code>Buffer</code> or <code>TypedArray</code> or <code>DataView</code> containing the supported ALPN
1831protocols. <code>Buffer</code>s should have the format <code>[len][name][len][name]...</code>
1832e.g. <code>0x05hello0x05world</code>, where the first byte is the length of the next
1833protocol name. Passing an array is usually much simpler, e.g.
1834<code>['hello', 'world']</code>. (Protocols should be ordered by their priority.)</li>
1835<li><code>clientCertEngine</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Name of an OpenSSL engine which can provide the
1836client certificate.</li>
1837<li><code>enableTrace</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, <a href="#tls_tlssocket_enabletrace"><code>tls.TLSSocket.enableTrace()</code></a> will be
1838called on new connections. Tracing can be enabled after the secure
1839connection is established, but this option must be used to trace the secure
1840connection setup. <strong>Default:</strong> <code>false</code>.</li>
1841<li><code>handshakeTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Abort the connection if the SSL/TLS handshake
1842does not finish in the specified number of milliseconds.
1843A <code>'tlsClientError'</code> is emitted on the <code>tls.Server</code> object whenever
1844a handshake times out. <strong>Default:</strong> <code>120000</code> (120 seconds).</li>
1845<li><code>rejectUnauthorized</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If not <code>false</code> the server will reject any
1846connection which is not authorized with the list of supplied CAs. This
1847option only has an effect if <code>requestCert</code> is <code>true</code>. <strong>Default:</strong> <code>true</code>.</li>
1848<li><code>requestCert</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code> the server will request a certificate from
1849clients that connect and attempt to verify that certificate. <strong>Default:</strong>
1850<code>false</code>.</li>
1851<li><code>sessionTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of seconds after which a TLS session
1852created by the server will no longer be resumable. See
1853<a href="#tls_session_resumption">Session Resumption</a> for more information. <strong>Default:</strong> <code>300</code>.</li>
1854<li><code>SNICallback(servername, callback)</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A function that will be
1855called if the client supports SNI TLS extension. Two arguments will be
1856passed when called: <code>servername</code> and <code>callback</code>. <code>callback</code> is an
1857error-first callback that takes two optional arguments: <code>error</code> and <code>ctx</code>.
1858<code>ctx</code>, if provided, is a <code>SecureContext</code> instance.
1859<a href="#tls_tls_createsecurecontext_options"><code>tls.createSecureContext()</code></a> can be used to get a proper <code>SecureContext</code>.
1860If <code>callback</code> is called with a falsy <code>ctx</code> argument, the default secure
1861context of the server will be used. If <code>SNICallback</code> wasn't provided the
1862default callback with high-level API will be used (see below).</li>
1863<li><code>ticketKeys</code>: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> 48-bytes of cryptographically strong pseudo-random
1864data. See <a href="#tls_session_resumption">Session Resumption</a> for more information.</li>
1865<li><code>pskCallback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a>
1866<ul>
1867<li>socket: <a href="tls.html#tls_class_tls_tlssocket" class="type">&#x3C;tls.TLSSocket></a> the server <a href="#tls_class_tls_tlssocket"><code>tls.TLSSocket</code></a> instance for
1868this connection.</li>
1869<li>identity: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> identity parameter sent from the client.</li>
1870<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> pre-shared key that must either be
1871a buffer or <code>null</code> to stop the negotiation process. Returned PSK must be
1872compatible with the selected cipher's digest.</li>
1873</ul>
1874When negotiating TLS-PSK (pre-shared keys), this function is called
1875with the identity provided by the client.
1876If the return value is <code>null</code> the negotiation process will stop and an
1877"unknown_psk_identity" alert message will be sent to the other party.
1878If the server wishes to hide the fact that the PSK identity was not known,
1879the callback must provide some random data as <code>psk</code> to make the connection
1880fail with "decrypt_error" before negotiation is finished.
1881PSK ciphers are disabled by default, and using TLS-PSK thus
1882requires explicitly specifying a cipher suite with the <code>ciphers</code> option.
1883More information can be found in the <a href="https://tools.ietf.org/html/rfc4279">RFC 4279</a>.</li>
1884<li><code>pskIdentityHint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> optional hint to send to a client to help
1885with selecting the identity during TLS-PSK negotiation. Will be ignored
1886in TLS 1.3. Upon failing to set pskIdentityHint <code>'tlsClientError'</code> will be
1887emitted with <code>'ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED'</code> code.</li>
1888<li>...: Any <a href="#tls_tls_createsecurecontext_options"><code>tls.createSecureContext()</code></a> option can be provided. For
1889servers, the identity options (<code>pfx</code>, <code>key</code>/<code>cert</code> or <code>pskCallback</code>)
1890are usually required.</li>
1891<li>...: Any <a href="net.html#net_net_createserver_options_connectionlistener"><code>net.createServer()</code></a> option can be provided.</li>
1892</ul>
1893</li>
1894<li><code>secureConnectionListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
1895<li>Returns: <a href="tls.html#tls_class_tls_server" class="type">&#x3C;tls.Server></a></li>
1896</ul>
1897<p>Creates a new <a href="#tls_class_tls_server"><code>tls.Server</code></a>. The <code>secureConnectionListener</code>, if provided, is
1898automatically set as a listener for the <a href="#tls_event_secureconnection"><code>'secureConnection'</code></a> event.</p>
1899<p>The <code>ticketKeys</code> options is automatically shared between <code>cluster</code> module
1900workers.</p>
1901<p>The following illustrates a simple echo server:</p>
1902<pre><code class="language-js"><span class="hljs-keyword">const</span> tls = <span class="hljs-built_in">require</span>(<span class="hljs-string">'tls'</span>);
1903<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
1904
1905<span class="hljs-keyword">const</span> options = {
1906  <span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'server-key.pem'</span>),
1907  <span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'server-cert.pem'</span>),
1908
1909  <span class="hljs-comment">// This is necessary only if using client certificate authentication.</span>
1910  <span class="hljs-attr">requestCert</span>: <span class="hljs-literal">true</span>,
1911
1912  <span class="hljs-comment">// This is necessary only if the client uses a self-signed certificate.</span>
1913  <span class="hljs-attr">ca</span>: [ fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'client-cert.pem'</span>) ]
1914};
1915
1916<span class="hljs-keyword">const</span> server = tls.<span class="hljs-title function_">createServer</span>(options, <span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> {
1917  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'server connected'</span>,
1918              socket.<span class="hljs-property">authorized</span> ? <span class="hljs-string">'authorized'</span> : <span class="hljs-string">'unauthorized'</span>);
1919  socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'welcome!\n'</span>);
1920  socket.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
1921  socket.<span class="hljs-title function_">pipe</span>(socket);
1922});
1923server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>, <span class="hljs-function">() =></span> {
1924  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'server bound'</span>);
1925});</code></pre>
1926<p>The server can be tested by connecting to it using the example client from
1927<a href="#tls_tls_connect_options_callback"><code>tls.connect()</code></a>.</p>
1928</section><section><h3><code>tls.getCiphers()</code><span><a class="mark" href="#tls_tls_getciphers" id="tls_tls_getciphers">#</a></span></h3>
1929<div class="api_metadata">
1930<span>Added in: v0.10.2</span>
1931</div>
1932<ul>
1933<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
1934</ul>
1935<p>Returns an array with the names of the supported TLS ciphers. The names are
1936lower-case for historical reasons, but must be uppercased to be used in
1937the <code>ciphers</code> option of <a href="#tls_tls_createsecurecontext_options"><code>tls.createSecureContext()</code></a>.</p>
1938<p>Cipher names that start with <code>'tls_'</code> are for TLSv1.3, all the others are for
1939TLSv1.2 and below.</p>
1940<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(tls.<span class="hljs-title function_">getCiphers</span>()); <span class="hljs-comment">// ['aes128-gcm-sha256', 'aes128-sha', ...]</span></code></pre>
1941</section><section><h3><code>tls.rootCertificates</code><span><a class="mark" href="#tls_tls_rootcertificates" id="tls_tls_rootcertificates">#</a></span></h3>
1942<div class="api_metadata">
1943<span>Added in: v12.3.0</span>
1944</div>
1945<ul>
1946<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
1947</ul>
1948<p>An immutable array of strings representing the root certificates (in PEM format)
1949from the bundled Mozilla CA store as supplied by current Node.js version.</p>
1950<p>The bundled CA store, as supplied by Node.js, is a snapshot of Mozilla CA store
1951that is fixed at release time. It is identical on all supported platforms.</p>
1952</section><section><h3><code>tls.DEFAULT_ECDH_CURVE</code><span><a class="mark" href="#tls_tls_default_ecdh_curve" id="tls_tls_default_ecdh_curve">#</a></span></h3>
1953<div class="api_metadata">
1954<details class="changelog"><summary>History</summary>
1955<table>
1956<tbody><tr><th>Version</th><th>Changes</th></tr>
1957<tr><td>v10.0.0</td>
1958<td><p>Default value changed to <code>'auto'</code>.</p></td></tr>
1959<tr><td>v0.11.13</td>
1960<td><p><span>Added in: v0.11.13</span></p></td></tr>
1961</tbody></table>
1962</details>
1963</div>
1964<p>The default curve name to use for ECDH key agreement in a tls server. The
1965default value is <code>'auto'</code>. See <a href="#tls_tls_createsecurecontext_options"><code>tls.createSecureContext()</code></a> for further
1966information.</p>
1967</section><section><h3><code>tls.DEFAULT_MAX_VERSION</code><span><a class="mark" href="#tls_tls_default_max_version" id="tls_tls_default_max_version">#</a></span></h3>
1968<div class="api_metadata">
1969<span>Added in: v11.4.0</span>
1970</div>
1971<ul>
1972<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The default value of the <code>maxVersion</code> option of
1973<a href="#tls_tls_createsecurecontext_options"><code>tls.createSecureContext()</code></a>. It can be assigned any of the supported TLS
1974protocol versions, <code>'TLSv1.3'</code>, <code>'TLSv1.2'</code>, <code>'TLSv1.1'</code>, or <code>'TLSv1'</code>.
1975<strong>Default:</strong> <code>'TLSv1.3'</code>, unless changed using CLI options. Using
1976<code>--tls-max-v1.2</code> sets the default to <code>'TLSv1.2'</code>. Using <code>--tls-max-v1.3</code> sets
1977the default to <code>'TLSv1.3'</code>. If multiple of the options are provided, the
1978highest maximum is used.</li>
1979</ul>
1980</section><section><h3><code>tls.DEFAULT_MIN_VERSION</code><span><a class="mark" href="#tls_tls_default_min_version" id="tls_tls_default_min_version">#</a></span></h3>
1981<div class="api_metadata">
1982<span>Added in: v11.4.0</span>
1983</div>
1984<ul>
1985<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The default value of the <code>minVersion</code> option of
1986<a href="#tls_tls_createsecurecontext_options"><code>tls.createSecureContext()</code></a>. It can be assigned any of the supported TLS
1987protocol versions, <code>'TLSv1.3'</code>, <code>'TLSv1.2'</code>, <code>'TLSv1.1'</code>, or <code>'TLSv1'</code>.
1988<strong>Default:</strong> <code>'TLSv1.2'</code>, unless changed using CLI options. Using
1989<code>--tls-min-v1.0</code> sets the default to <code>'TLSv1'</code>. Using <code>--tls-min-v1.1</code> sets
1990the default to <code>'TLSv1.1'</code>. Using <code>--tls-min-v1.3</code> sets the default to
1991<code>'TLSv1.3'</code>. If multiple of the options are provided, the lowest minimum is
1992used.</li>
1993</ul></section>
1994        <!-- API END -->
1995      </div>
1996    </div>
1997  </div>
1998  <script>
1999    'use strict';
2000    {
2001      const kCustomPreference = 'customDarkTheme';
2002      const userSettings = sessionStorage.getItem(kCustomPreference);
2003      const themeToggleButton = document.getElementById('theme-toggle-btn');
2004      if (userSettings === null && window.matchMedia) {
2005        const mq = window.matchMedia('(prefers-color-scheme: dark)');
2006        if ('onchange' in mq) {
2007          function mqChangeListener(e) {
2008            document.body.classList.toggle('dark-mode', e.matches);
2009          }
2010          mq.addEventListener('change', mqChangeListener);
2011          if (themeToggleButton) {
2012            themeToggleButton.addEventListener('click', function() {
2013              mq.removeEventListener('change', mqChangeListener);
2014            }, { once: true });
2015          }
2016        }
2017        if (mq.matches) {
2018          document.body.classList.add('dark-mode');
2019        }
2020      } else if (userSettings === 'true') {
2021        document.body.classList.add('dark-mode');
2022      }
2023      if (themeToggleButton) {
2024        themeToggleButton.hidden = false;
2025        themeToggleButton.addEventListener('click', function() {
2026          sessionStorage.setItem(
2027            kCustomPreference,
2028            document.body.classList.toggle('dark-mode')
2029          );
2030        });
2031      }
2032    }
2033  </script>
2034</body>
2035</html>
2036