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