11cb0ef41Sopenharmony_ci# TLS (SSL) 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci<!--introduced_in=v0.10.0--> 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci> Stability: 2 - Stable 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci<!-- source_link=lib/tls.js --> 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciThe `node:tls` module provides an implementation of the Transport Layer Security 101cb0ef41Sopenharmony_ci(TLS) and Secure Socket Layer (SSL) protocols that is built on top of OpenSSL. 111cb0ef41Sopenharmony_ciThe module can be accessed using: 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci```js 141cb0ef41Sopenharmony_ciconst tls = require('node:tls'); 151cb0ef41Sopenharmony_ci``` 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci## Determining if crypto support is unavailable 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ciIt is possible for Node.js to be built without including support for the 201cb0ef41Sopenharmony_ci`node:crypto` module. In such cases, attempting to `import` from `tls` or 211cb0ef41Sopenharmony_cicalling `require('node:tls')` will result in an error being thrown. 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ciWhen using CommonJS, the error thrown can be caught using try/catch: 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci<!-- eslint-skip --> 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci```cjs 281cb0ef41Sopenharmony_cilet tls; 291cb0ef41Sopenharmony_citry { 301cb0ef41Sopenharmony_ci tls = require('node:tls'); 311cb0ef41Sopenharmony_ci} catch (err) { 321cb0ef41Sopenharmony_ci console.error('tls support is disabled!'); 331cb0ef41Sopenharmony_ci} 341cb0ef41Sopenharmony_ci``` 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ciWhen using the lexical ESM `import` keyword, the error can only be 371cb0ef41Sopenharmony_cicaught if a handler for `process.on('uncaughtException')` is registered 381cb0ef41Sopenharmony_ci_before_ any attempt to load the module is made (using, for instance, 391cb0ef41Sopenharmony_cia preload module). 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ciWhen using ESM, if there is a chance that the code may be run on a build 421cb0ef41Sopenharmony_ciof Node.js where crypto support is not enabled, consider using the 431cb0ef41Sopenharmony_ci[`import()`][] function instead of the lexical `import` keyword: 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci```mjs 461cb0ef41Sopenharmony_cilet tls; 471cb0ef41Sopenharmony_citry { 481cb0ef41Sopenharmony_ci tls = await import('node:tls'); 491cb0ef41Sopenharmony_ci} catch (err) { 501cb0ef41Sopenharmony_ci console.error('tls support is disabled!'); 511cb0ef41Sopenharmony_ci} 521cb0ef41Sopenharmony_ci``` 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci## TLS/SSL concepts 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ciTLS/SSL is a set of protocols that rely on a public key infrastructure (PKI) to 571cb0ef41Sopenharmony_cienable secure communication between a client and a server. For most common 581cb0ef41Sopenharmony_cicases, each server must have a private key. 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ciPrivate keys can be generated in multiple ways. The example below illustrates 611cb0ef41Sopenharmony_ciuse of the OpenSSL command-line interface to generate a 2048-bit RSA private 621cb0ef41Sopenharmony_cikey: 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci```bash 651cb0ef41Sopenharmony_ciopenssl genrsa -out ryans-key.pem 2048 661cb0ef41Sopenharmony_ci``` 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ciWith TLS/SSL, all servers (and some clients) must have a _certificate_. 691cb0ef41Sopenharmony_ciCertificates are _public keys_ that correspond to a private key, and that are 701cb0ef41Sopenharmony_cidigitally signed either by a Certificate Authority or by the owner of the 711cb0ef41Sopenharmony_ciprivate key (such certificates are referred to as "self-signed"). The first 721cb0ef41Sopenharmony_cistep to obtaining a certificate is to create a _Certificate Signing Request_ 731cb0ef41Sopenharmony_ci(CSR) file. 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ciThe OpenSSL command-line interface can be used to generate a CSR for a private 761cb0ef41Sopenharmony_cikey: 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci```bash 791cb0ef41Sopenharmony_ciopenssl req -new -sha256 -key ryans-key.pem -out ryans-csr.pem 801cb0ef41Sopenharmony_ci``` 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ciOnce the CSR file is generated, it can either be sent to a Certificate 831cb0ef41Sopenharmony_ciAuthority for signing or used to generate a self-signed certificate. 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ciCreating a self-signed certificate using the OpenSSL command-line interface 861cb0ef41Sopenharmony_ciis illustrated in the example below: 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci```bash 891cb0ef41Sopenharmony_ciopenssl x509 -req -in ryans-csr.pem -signkey ryans-key.pem -out ryans-cert.pem 901cb0ef41Sopenharmony_ci``` 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ciOnce the certificate is generated, it can be used to generate a `.pfx` or 931cb0ef41Sopenharmony_ci`.p12` file: 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci```bash 961cb0ef41Sopenharmony_ciopenssl pkcs12 -export -in ryans-cert.pem -inkey ryans-key.pem \ 971cb0ef41Sopenharmony_ci -certfile ca-cert.pem -out ryans.pfx 981cb0ef41Sopenharmony_ci``` 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ciWhere: 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci* `in`: is the signed certificate 1031cb0ef41Sopenharmony_ci* `inkey`: is the associated private key 1041cb0ef41Sopenharmony_ci* `certfile`: is a concatenation of all Certificate Authority (CA) certs into 1051cb0ef41Sopenharmony_ci a single file, e.g. `cat ca1-cert.pem ca2-cert.pem > ca-cert.pem` 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci### Perfect forward secrecy 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_ci<!-- type=misc --> 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ciThe term _[forward secrecy][]_ or _perfect forward secrecy_ describes a feature 1121cb0ef41Sopenharmony_ciof key-agreement (i.e., key-exchange) methods. That is, the server and client 1131cb0ef41Sopenharmony_cikeys are used to negotiate new temporary keys that are used specifically and 1141cb0ef41Sopenharmony_cionly for the current communication session. Practically, this means that even 1151cb0ef41Sopenharmony_ciif the server's private key is compromised, communication can only be decrypted 1161cb0ef41Sopenharmony_ciby eavesdroppers if the attacker manages to obtain the key-pair specifically 1171cb0ef41Sopenharmony_cigenerated for the session. 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_ciPerfect forward secrecy is achieved by randomly generating a key pair for 1201cb0ef41Sopenharmony_cikey-agreement on every TLS/SSL handshake (in contrast to using the same key for 1211cb0ef41Sopenharmony_ciall sessions). Methods implementing this technique are called "ephemeral". 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ciCurrently two methods are commonly used to achieve perfect forward secrecy (note 1241cb0ef41Sopenharmony_cithe character "E" appended to the traditional abbreviations): 1251cb0ef41Sopenharmony_ci 1261cb0ef41Sopenharmony_ci* [ECDHE][]: An ephemeral version of the Elliptic Curve Diffie-Hellman 1271cb0ef41Sopenharmony_ci key-agreement protocol. 1281cb0ef41Sopenharmony_ci* [DHE][]: An ephemeral version of the Diffie-Hellman key-agreement protocol. 1291cb0ef41Sopenharmony_ci 1301cb0ef41Sopenharmony_ciPerfect forward secrecy using ECDHE is enabled by default. The `ecdhCurve` 1311cb0ef41Sopenharmony_cioption can be used when creating a TLS server to customize the list of supported 1321cb0ef41Sopenharmony_ciECDH curves to use. See [`tls.createServer()`][] for more info. 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ciDHE is disabled by default but can be enabled alongside ECDHE by setting the 1351cb0ef41Sopenharmony_ci`dhparam` option to `'auto'`. Custom DHE parameters are also supported but 1361cb0ef41Sopenharmony_cidiscouraged in favor of automatically selected, well-known parameters. 1371cb0ef41Sopenharmony_ci 1381cb0ef41Sopenharmony_ciPerfect forward secrecy was optional up to TLSv1.2. As of TLSv1.3, (EC)DHE is 1391cb0ef41Sopenharmony_cialways used (with the exception of PSK-only connections). 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_ci### ALPN and SNI 1421cb0ef41Sopenharmony_ci 1431cb0ef41Sopenharmony_ci<!-- type=misc --> 1441cb0ef41Sopenharmony_ci 1451cb0ef41Sopenharmony_ciALPN (Application-Layer Protocol Negotiation Extension) and 1461cb0ef41Sopenharmony_ciSNI (Server Name Indication) are TLS handshake extensions: 1471cb0ef41Sopenharmony_ci 1481cb0ef41Sopenharmony_ci* ALPN: Allows the use of one TLS server for multiple protocols (HTTP, HTTP/2) 1491cb0ef41Sopenharmony_ci* SNI: Allows the use of one TLS server for multiple hostnames with different 1501cb0ef41Sopenharmony_ci certificates. 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_ci### Pre-shared keys 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ci<!-- type=misc --> 1551cb0ef41Sopenharmony_ci 1561cb0ef41Sopenharmony_ciTLS-PSK support is available as an alternative to normal certificate-based 1571cb0ef41Sopenharmony_ciauthentication. It uses a pre-shared key instead of certificates to 1581cb0ef41Sopenharmony_ciauthenticate a TLS connection, providing mutual authentication. 1591cb0ef41Sopenharmony_ciTLS-PSK and public key infrastructure are not mutually exclusive. Clients and 1601cb0ef41Sopenharmony_ciservers can accommodate both, choosing either of them during the normal cipher 1611cb0ef41Sopenharmony_cinegotiation step. 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_ciTLS-PSK is only a good choice where means exist to securely share a 1641cb0ef41Sopenharmony_cikey with every connecting machine, so it does not replace the public key 1651cb0ef41Sopenharmony_ciinfrastructure (PKI) for the majority of TLS uses. 1661cb0ef41Sopenharmony_ciThe TLS-PSK implementation in OpenSSL has seen many security flaws in 1671cb0ef41Sopenharmony_cirecent years, mostly because it is used only by a minority of applications. 1681cb0ef41Sopenharmony_ciPlease consider all alternative solutions before switching to PSK ciphers. 1691cb0ef41Sopenharmony_ciUpon generating PSK it is of critical importance to use sufficient entropy as 1701cb0ef41Sopenharmony_cidiscussed in [RFC 4086][]. Deriving a shared secret from a password or other 1711cb0ef41Sopenharmony_cilow-entropy sources is not secure. 1721cb0ef41Sopenharmony_ci 1731cb0ef41Sopenharmony_ciPSK ciphers are disabled by default, and using TLS-PSK thus requires explicitly 1741cb0ef41Sopenharmony_cispecifying a cipher suite with the `ciphers` option. The list of available 1751cb0ef41Sopenharmony_ciciphers can be retrieved via `openssl ciphers -v 'PSK'`. All TLS 1.3 1761cb0ef41Sopenharmony_ciciphers are eligible for PSK and can be retrieved via 1771cb0ef41Sopenharmony_ci`openssl ciphers -v -s -tls1_3 -psk`. 1781cb0ef41Sopenharmony_ci 1791cb0ef41Sopenharmony_ciAccording to the [RFC 4279][], PSK identities up to 128 bytes in length and 1801cb0ef41Sopenharmony_ciPSKs up to 64 bytes in length must be supported. As of OpenSSL 1.1.0 1811cb0ef41Sopenharmony_cimaximum identity size is 128 bytes, and maximum PSK length is 256 bytes. 1821cb0ef41Sopenharmony_ci 1831cb0ef41Sopenharmony_ciThe current implementation doesn't support asynchronous PSK callbacks due to the 1841cb0ef41Sopenharmony_cilimitations of the underlying OpenSSL API. 1851cb0ef41Sopenharmony_ci 1861cb0ef41Sopenharmony_ci### Client-initiated renegotiation attack mitigation 1871cb0ef41Sopenharmony_ci 1881cb0ef41Sopenharmony_ci<!-- type=misc --> 1891cb0ef41Sopenharmony_ci 1901cb0ef41Sopenharmony_ciThe TLS protocol allows clients to renegotiate certain aspects of the TLS 1911cb0ef41Sopenharmony_cisession. Unfortunately, session renegotiation requires a disproportionate amount 1921cb0ef41Sopenharmony_ciof server-side resources, making it a potential vector for denial-of-service 1931cb0ef41Sopenharmony_ciattacks. 1941cb0ef41Sopenharmony_ci 1951cb0ef41Sopenharmony_ciTo mitigate the risk, renegotiation is limited to three times every ten minutes. 1961cb0ef41Sopenharmony_ciAn `'error'` event is emitted on the [`tls.TLSSocket`][] instance when this 1971cb0ef41Sopenharmony_cithreshold is exceeded. The limits are configurable: 1981cb0ef41Sopenharmony_ci 1991cb0ef41Sopenharmony_ci* `tls.CLIENT_RENEG_LIMIT` {number} Specifies the number of renegotiation 2001cb0ef41Sopenharmony_ci requests. **Default:** `3`. 2011cb0ef41Sopenharmony_ci* `tls.CLIENT_RENEG_WINDOW` {number} Specifies the time renegotiation window 2021cb0ef41Sopenharmony_ci in seconds. **Default:** `600` (10 minutes). 2031cb0ef41Sopenharmony_ci 2041cb0ef41Sopenharmony_ciThe default renegotiation limits should not be modified without a full 2051cb0ef41Sopenharmony_ciunderstanding of the implications and risks. 2061cb0ef41Sopenharmony_ci 2071cb0ef41Sopenharmony_ciTLSv1.3 does not support renegotiation. 2081cb0ef41Sopenharmony_ci 2091cb0ef41Sopenharmony_ci### Session resumption 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ciEstablishing a TLS session can be relatively slow. The process can be sped 2121cb0ef41Sopenharmony_ciup by saving and later reusing the session state. There are several mechanisms 2131cb0ef41Sopenharmony_cito do so, discussed here from oldest to newest (and preferred). 2141cb0ef41Sopenharmony_ci 2151cb0ef41Sopenharmony_ci#### Session identifiers 2161cb0ef41Sopenharmony_ci 2171cb0ef41Sopenharmony_ciServers generate a unique ID for new connections and 2181cb0ef41Sopenharmony_cisend it to the client. Clients and servers save the session state. When 2191cb0ef41Sopenharmony_cireconnecting, clients send the ID of their saved session state and if the server 2201cb0ef41Sopenharmony_cialso has the state for that ID, it can agree to use it. Otherwise, the server 2211cb0ef41Sopenharmony_ciwill create a new session. See [RFC 2246][] for more information, page 23 and 2221cb0ef41Sopenharmony_ci30\. 2231cb0ef41Sopenharmony_ci 2241cb0ef41Sopenharmony_ciResumption using session identifiers is supported by most web browsers when 2251cb0ef41Sopenharmony_cimaking HTTPS requests. 2261cb0ef41Sopenharmony_ci 2271cb0ef41Sopenharmony_ciFor Node.js, clients wait for the [`'session'`][] event to get the session data, 2281cb0ef41Sopenharmony_ciand provide the data to the `session` option of a subsequent [`tls.connect()`][] 2291cb0ef41Sopenharmony_cito reuse the session. Servers must 2301cb0ef41Sopenharmony_ciimplement handlers for the [`'newSession'`][] and [`'resumeSession'`][] events 2311cb0ef41Sopenharmony_cito save and restore the session data using the session ID as the lookup key to 2321cb0ef41Sopenharmony_cireuse sessions. To reuse sessions across load balancers or cluster workers, 2331cb0ef41Sopenharmony_ciservers must use a shared session cache (such as Redis) in their session 2341cb0ef41Sopenharmony_cihandlers. 2351cb0ef41Sopenharmony_ci 2361cb0ef41Sopenharmony_ci#### Session tickets 2371cb0ef41Sopenharmony_ci 2381cb0ef41Sopenharmony_ciThe servers encrypt the entire session state and send it 2391cb0ef41Sopenharmony_cito the client as a "ticket". When reconnecting, the state is sent to the server 2401cb0ef41Sopenharmony_ciin the initial connection. This mechanism avoids the need for a server-side 2411cb0ef41Sopenharmony_cisession cache. If the server doesn't use the ticket, for any reason (failure 2421cb0ef41Sopenharmony_cito decrypt it, it's too old, etc.), it will create a new session and send a new 2431cb0ef41Sopenharmony_citicket. See [RFC 5077][] for more information. 2441cb0ef41Sopenharmony_ci 2451cb0ef41Sopenharmony_ciResumption using session tickets is becoming commonly supported by many web 2461cb0ef41Sopenharmony_cibrowsers when making HTTPS requests. 2471cb0ef41Sopenharmony_ci 2481cb0ef41Sopenharmony_ciFor Node.js, clients use the same APIs for resumption with session identifiers 2491cb0ef41Sopenharmony_cias for resumption with session tickets. For debugging, if 2501cb0ef41Sopenharmony_ci[`tls.TLSSocket.getTLSTicket()`][] returns a value, the session data contains a 2511cb0ef41Sopenharmony_citicket, otherwise it contains client-side session state. 2521cb0ef41Sopenharmony_ci 2531cb0ef41Sopenharmony_ciWith TLSv1.3, be aware that multiple tickets may be sent by the server, 2541cb0ef41Sopenharmony_ciresulting in multiple `'session'` events, see [`'session'`][] for more 2551cb0ef41Sopenharmony_ciinformation. 2561cb0ef41Sopenharmony_ci 2571cb0ef41Sopenharmony_ciSingle process servers need no specific implementation to use session tickets. 2581cb0ef41Sopenharmony_ciTo use session tickets across server restarts or load balancers, servers must 2591cb0ef41Sopenharmony_ciall have the same ticket keys. There are three 16-byte keys internally, but the 2601cb0ef41Sopenharmony_citls API exposes them as a single 48-byte buffer for convenience. 2611cb0ef41Sopenharmony_ci 2621cb0ef41Sopenharmony_ciIt's possible to get the ticket keys by calling [`server.getTicketKeys()`][] on 2631cb0ef41Sopenharmony_cione server instance and then distribute them, but it is more reasonable to 2641cb0ef41Sopenharmony_cisecurely generate 48 bytes of secure random data and set them with the 2651cb0ef41Sopenharmony_ci`ticketKeys` option of [`tls.createServer()`][]. The keys should be regularly 2661cb0ef41Sopenharmony_ciregenerated and server's keys can be reset with 2671cb0ef41Sopenharmony_ci[`server.setTicketKeys()`][]. 2681cb0ef41Sopenharmony_ci 2691cb0ef41Sopenharmony_ciSession ticket keys are cryptographic keys, and they _**must be stored 2701cb0ef41Sopenharmony_cisecurely**_. With TLS 1.2 and below, if they are compromised all sessions that 2711cb0ef41Sopenharmony_ciused tickets encrypted with them can be decrypted. They should not be stored 2721cb0ef41Sopenharmony_cion disk, and they should be regenerated regularly. 2731cb0ef41Sopenharmony_ci 2741cb0ef41Sopenharmony_ciIf clients advertise support for tickets, the server will send them. The 2751cb0ef41Sopenharmony_ciserver can disable tickets by supplying 2761cb0ef41Sopenharmony_ci`require('node:constants').SSL_OP_NO_TICKET` in `secureOptions`. 2771cb0ef41Sopenharmony_ci 2781cb0ef41Sopenharmony_ciBoth session identifiers and session tickets timeout, causing the server to 2791cb0ef41Sopenharmony_cicreate new sessions. The timeout can be configured with the `sessionTimeout` 2801cb0ef41Sopenharmony_cioption of [`tls.createServer()`][]. 2811cb0ef41Sopenharmony_ci 2821cb0ef41Sopenharmony_ciFor all the mechanisms, when resumption fails, servers will create new sessions. 2831cb0ef41Sopenharmony_ciSince failing to resume the session does not cause TLS/HTTPS connection 2841cb0ef41Sopenharmony_cifailures, it is easy to not notice unnecessarily poor TLS performance. The 2851cb0ef41Sopenharmony_ciOpenSSL CLI can be used to verify that servers are resuming sessions. Use the 2861cb0ef41Sopenharmony_ci`-reconnect` option to `openssl s_client`, for example: 2871cb0ef41Sopenharmony_ci 2881cb0ef41Sopenharmony_ci```console 2891cb0ef41Sopenharmony_ci$ openssl s_client -connect localhost:443 -reconnect 2901cb0ef41Sopenharmony_ci``` 2911cb0ef41Sopenharmony_ci 2921cb0ef41Sopenharmony_ciRead through the debug output. The first connection should say "New", for 2931cb0ef41Sopenharmony_ciexample: 2941cb0ef41Sopenharmony_ci 2951cb0ef41Sopenharmony_ci```text 2961cb0ef41Sopenharmony_ciNew, TLSv1.2, Cipher is ECDHE-RSA-AES128-GCM-SHA256 2971cb0ef41Sopenharmony_ci``` 2981cb0ef41Sopenharmony_ci 2991cb0ef41Sopenharmony_ciSubsequent connections should say "Reused", for example: 3001cb0ef41Sopenharmony_ci 3011cb0ef41Sopenharmony_ci```text 3021cb0ef41Sopenharmony_ciReused, TLSv1.2, Cipher is ECDHE-RSA-AES128-GCM-SHA256 3031cb0ef41Sopenharmony_ci``` 3041cb0ef41Sopenharmony_ci 3051cb0ef41Sopenharmony_ci## Modifying the default TLS cipher suite 3061cb0ef41Sopenharmony_ci 3071cb0ef41Sopenharmony_ciNode.js is built with a default suite of enabled and disabled TLS ciphers. This 3081cb0ef41Sopenharmony_cidefault cipher list can be configured when building Node.js to allow 3091cb0ef41Sopenharmony_cidistributions to provide their own default list. 3101cb0ef41Sopenharmony_ci 3111cb0ef41Sopenharmony_ciThe following command can be used to show the default cipher suite: 3121cb0ef41Sopenharmony_ci 3131cb0ef41Sopenharmony_ci```console 3141cb0ef41Sopenharmony_cinode -p crypto.constants.defaultCoreCipherList | tr ':' '\n' 3151cb0ef41Sopenharmony_ciTLS_AES_256_GCM_SHA384 3161cb0ef41Sopenharmony_ciTLS_CHACHA20_POLY1305_SHA256 3171cb0ef41Sopenharmony_ciTLS_AES_128_GCM_SHA256 3181cb0ef41Sopenharmony_ciECDHE-RSA-AES128-GCM-SHA256 3191cb0ef41Sopenharmony_ciECDHE-ECDSA-AES128-GCM-SHA256 3201cb0ef41Sopenharmony_ciECDHE-RSA-AES256-GCM-SHA384 3211cb0ef41Sopenharmony_ciECDHE-ECDSA-AES256-GCM-SHA384 3221cb0ef41Sopenharmony_ciDHE-RSA-AES128-GCM-SHA256 3231cb0ef41Sopenharmony_ciECDHE-RSA-AES128-SHA256 3241cb0ef41Sopenharmony_ciDHE-RSA-AES128-SHA256 3251cb0ef41Sopenharmony_ciECDHE-RSA-AES256-SHA384 3261cb0ef41Sopenharmony_ciDHE-RSA-AES256-SHA384 3271cb0ef41Sopenharmony_ciECDHE-RSA-AES256-SHA256 3281cb0ef41Sopenharmony_ciDHE-RSA-AES256-SHA256 3291cb0ef41Sopenharmony_ciHIGH 3301cb0ef41Sopenharmony_ci!aNULL 3311cb0ef41Sopenharmony_ci!eNULL 3321cb0ef41Sopenharmony_ci!EXPORT 3331cb0ef41Sopenharmony_ci!DES 3341cb0ef41Sopenharmony_ci!RC4 3351cb0ef41Sopenharmony_ci!MD5 3361cb0ef41Sopenharmony_ci!PSK 3371cb0ef41Sopenharmony_ci!SRP 3381cb0ef41Sopenharmony_ci!CAMELLIA 3391cb0ef41Sopenharmony_ci``` 3401cb0ef41Sopenharmony_ci 3411cb0ef41Sopenharmony_ciThis default can be replaced entirely using the [`--tls-cipher-list`][] 3421cb0ef41Sopenharmony_cicommand-line switch (directly, or via the [`NODE_OPTIONS`][] environment 3431cb0ef41Sopenharmony_civariable). For instance, the following makes `ECDHE-RSA-AES128-GCM-SHA256:!RC4` 3441cb0ef41Sopenharmony_cithe default TLS cipher suite: 3451cb0ef41Sopenharmony_ci 3461cb0ef41Sopenharmony_ci```bash 3471cb0ef41Sopenharmony_cinode --tls-cipher-list='ECDHE-RSA-AES128-GCM-SHA256:!RC4' server.js 3481cb0ef41Sopenharmony_ci 3491cb0ef41Sopenharmony_ciexport NODE_OPTIONS=--tls-cipher-list='ECDHE-RSA-AES128-GCM-SHA256:!RC4' 3501cb0ef41Sopenharmony_cinode server.js 3511cb0ef41Sopenharmony_ci``` 3521cb0ef41Sopenharmony_ci 3531cb0ef41Sopenharmony_ciTo verify, use the following command to show the set cipher list, note the 3541cb0ef41Sopenharmony_cidifference between `defaultCoreCipherList` and `defaultCipherList`: 3551cb0ef41Sopenharmony_ci 3561cb0ef41Sopenharmony_ci```bash 3571cb0ef41Sopenharmony_cinode --tls-cipher-list='ECDHE-RSA-AES128-GCM-SHA256:!RC4' -p crypto.constants.defaultCipherList | tr ':' '\n' 3581cb0ef41Sopenharmony_ciECDHE-RSA-AES128-GCM-SHA256 3591cb0ef41Sopenharmony_ci!RC4 3601cb0ef41Sopenharmony_ci``` 3611cb0ef41Sopenharmony_ci 3621cb0ef41Sopenharmony_cii.e. the `defaultCoreCipherList` list is set at compilation time and the 3631cb0ef41Sopenharmony_ci`defaultCipherList` is set at runtime. 3641cb0ef41Sopenharmony_ci 3651cb0ef41Sopenharmony_ciTo modify the default cipher suites from within the runtime, modify the 3661cb0ef41Sopenharmony_ci`tls.DEFAULT_CIPHERS` variable, this must be performed before listening on any 3671cb0ef41Sopenharmony_cisockets, it will not affect sockets already opened. For example: 3681cb0ef41Sopenharmony_ci 3691cb0ef41Sopenharmony_ci```js 3701cb0ef41Sopenharmony_ci// Remove Obsolete CBC Ciphers and RSA Key Exchange based Ciphers as they don't provide Forward Secrecy 3711cb0ef41Sopenharmony_citls.DEFAULT_CIPHERS += 3721cb0ef41Sopenharmony_ci ':!ECDHE-RSA-AES128-SHA:!ECDHE-RSA-AES128-SHA256:!ECDHE-RSA-AES256-SHA:!ECDHE-RSA-AES256-SHA384' + 3731cb0ef41Sopenharmony_ci ':!ECDHE-ECDSA-AES128-SHA:!ECDHE-ECDSA-AES128-SHA256:!ECDHE-ECDSA-AES256-SHA:!ECDHE-ECDSA-AES256-SHA384' + 3741cb0ef41Sopenharmony_ci ':!kRSA'; 3751cb0ef41Sopenharmony_ci``` 3761cb0ef41Sopenharmony_ci 3771cb0ef41Sopenharmony_ciThe default can also be replaced on a per client or server basis using the 3781cb0ef41Sopenharmony_ci`ciphers` option from [`tls.createSecureContext()`][], which is also available 3791cb0ef41Sopenharmony_ciin [`tls.createServer()`][], [`tls.connect()`][], and when creating new 3801cb0ef41Sopenharmony_ci[`tls.TLSSocket`][]s. 3811cb0ef41Sopenharmony_ci 3821cb0ef41Sopenharmony_ciThe ciphers list can contain a mixture of TLSv1.3 cipher suite names, the ones 3831cb0ef41Sopenharmony_cithat start with `'TLS_'`, and specifications for TLSv1.2 and below cipher 3841cb0ef41Sopenharmony_cisuites. The TLSv1.2 ciphers support a legacy specification format, consult 3851cb0ef41Sopenharmony_cithe OpenSSL [cipher list format][] documentation for details, but those 3861cb0ef41Sopenharmony_cispecifications do _not_ apply to TLSv1.3 ciphers. The TLSv1.3 suites can only 3871cb0ef41Sopenharmony_cibe enabled by including their full name in the cipher list. They cannot, for 3881cb0ef41Sopenharmony_ciexample, be enabled or disabled by using the legacy TLSv1.2 `'EECDH'` or 3891cb0ef41Sopenharmony_ci`'!EECDH'` specification. 3901cb0ef41Sopenharmony_ci 3911cb0ef41Sopenharmony_ciDespite the relative order of TLSv1.3 and TLSv1.2 cipher suites, the TLSv1.3 3921cb0ef41Sopenharmony_ciprotocol is significantly more secure than TLSv1.2, and will always be chosen 3931cb0ef41Sopenharmony_ciover TLSv1.2 if the handshake indicates it is supported, and if any TLSv1.3 3941cb0ef41Sopenharmony_cicipher suites are enabled. 3951cb0ef41Sopenharmony_ci 3961cb0ef41Sopenharmony_ciThe default cipher suite included within Node.js has been carefully 3971cb0ef41Sopenharmony_ciselected to reflect current security best practices and risk mitigation. 3981cb0ef41Sopenharmony_ciChanging the default cipher suite can have a significant impact on the security 3991cb0ef41Sopenharmony_ciof an application. The `--tls-cipher-list` switch and `ciphers` option should by 4001cb0ef41Sopenharmony_ciused only if absolutely necessary. 4011cb0ef41Sopenharmony_ci 4021cb0ef41Sopenharmony_ciThe default cipher suite prefers GCM ciphers for [Chrome's 'modern 4031cb0ef41Sopenharmony_cicryptography' setting][] and also prefers ECDHE and DHE ciphers for perfect 4041cb0ef41Sopenharmony_ciforward secrecy, while offering _some_ backward compatibility. 4051cb0ef41Sopenharmony_ci 4061cb0ef41Sopenharmony_ciOld clients that rely on insecure and deprecated RC4 or DES-based ciphers 4071cb0ef41Sopenharmony_ci(like Internet Explorer 6) cannot complete the handshaking process with 4081cb0ef41Sopenharmony_cithe default configuration. If these clients _must_ be supported, the 4091cb0ef41Sopenharmony_ci[TLS recommendations][] may offer a compatible cipher suite. For more details 4101cb0ef41Sopenharmony_cion the format, see the OpenSSL [cipher list format][] documentation. 4111cb0ef41Sopenharmony_ci 4121cb0ef41Sopenharmony_ciThere are only five TLSv1.3 cipher suites: 4131cb0ef41Sopenharmony_ci 4141cb0ef41Sopenharmony_ci* `'TLS_AES_256_GCM_SHA384'` 4151cb0ef41Sopenharmony_ci* `'TLS_CHACHA20_POLY1305_SHA256'` 4161cb0ef41Sopenharmony_ci* `'TLS_AES_128_GCM_SHA256'` 4171cb0ef41Sopenharmony_ci* `'TLS_AES_128_CCM_SHA256'` 4181cb0ef41Sopenharmony_ci* `'TLS_AES_128_CCM_8_SHA256'` 4191cb0ef41Sopenharmony_ci 4201cb0ef41Sopenharmony_ciThe first three are enabled by default. The two `CCM`-based suites are supported 4211cb0ef41Sopenharmony_ciby TLSv1.3 because they may be more performant on constrained systems, but they 4221cb0ef41Sopenharmony_ciare not enabled by default since they offer less security. 4231cb0ef41Sopenharmony_ci 4241cb0ef41Sopenharmony_ci## X509 certificate error codes 4251cb0ef41Sopenharmony_ci 4261cb0ef41Sopenharmony_ciMultiple functions can fail due to certificate errors that are reported by 4271cb0ef41Sopenharmony_ciOpenSSL. In such a case, the function provides an {Error} via its callback that 4281cb0ef41Sopenharmony_cihas the property `code` which can take one of the following values: 4291cb0ef41Sopenharmony_ci 4301cb0ef41Sopenharmony_ci<!-- 4311cb0ef41Sopenharmony_civalues are taken from src/crypto/crypto_common.cc 4321cb0ef41Sopenharmony_cidescription are taken from deps/openssl/openssl/crypto/x509/x509_txt.c 4331cb0ef41Sopenharmony_ci--> 4341cb0ef41Sopenharmony_ci 4351cb0ef41Sopenharmony_ci* `'UNABLE_TO_GET_ISSUER_CERT'`: Unable to get issuer certificate. 4361cb0ef41Sopenharmony_ci* `'UNABLE_TO_GET_CRL'`: Unable to get certificate CRL. 4371cb0ef41Sopenharmony_ci* `'UNABLE_TO_DECRYPT_CERT_SIGNATURE'`: Unable to decrypt certificate's 4381cb0ef41Sopenharmony_ci signature. 4391cb0ef41Sopenharmony_ci* `'UNABLE_TO_DECRYPT_CRL_SIGNATURE'`: Unable to decrypt CRL's signature. 4401cb0ef41Sopenharmony_ci* `'UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY'`: Unable to decode issuer public key. 4411cb0ef41Sopenharmony_ci* `'CERT_SIGNATURE_FAILURE'`: Certificate signature failure. 4421cb0ef41Sopenharmony_ci* `'CRL_SIGNATURE_FAILURE'`: CRL signature failure. 4431cb0ef41Sopenharmony_ci* `'CERT_NOT_YET_VALID'`: Certificate is not yet valid. 4441cb0ef41Sopenharmony_ci* `'CERT_HAS_EXPIRED'`: Certificate has expired. 4451cb0ef41Sopenharmony_ci* `'CRL_NOT_YET_VALID'`: CRL is not yet valid. 4461cb0ef41Sopenharmony_ci* `'CRL_HAS_EXPIRED'`: CRL has expired. 4471cb0ef41Sopenharmony_ci* `'ERROR_IN_CERT_NOT_BEFORE_FIELD'`: Format error in certificate's notBefore 4481cb0ef41Sopenharmony_ci field. 4491cb0ef41Sopenharmony_ci* `'ERROR_IN_CERT_NOT_AFTER_FIELD'`: Format error in certificate's notAfter 4501cb0ef41Sopenharmony_ci field. 4511cb0ef41Sopenharmony_ci* `'ERROR_IN_CRL_LAST_UPDATE_FIELD'`: Format error in CRL's lastUpdate field. 4521cb0ef41Sopenharmony_ci* `'ERROR_IN_CRL_NEXT_UPDATE_FIELD'`: Format error in CRL's nextUpdate field. 4531cb0ef41Sopenharmony_ci* `'OUT_OF_MEM'`: Out of memory. 4541cb0ef41Sopenharmony_ci* `'DEPTH_ZERO_SELF_SIGNED_CERT'`: Self signed certificate. 4551cb0ef41Sopenharmony_ci* `'SELF_SIGNED_CERT_IN_CHAIN'`: Self signed certificate in certificate chain. 4561cb0ef41Sopenharmony_ci* `'UNABLE_TO_GET_ISSUER_CERT_LOCALLY'`: Unable to get local issuer certificate. 4571cb0ef41Sopenharmony_ci* `'UNABLE_TO_VERIFY_LEAF_SIGNATURE'`: Unable to verify the first certificate. 4581cb0ef41Sopenharmony_ci* `'CERT_CHAIN_TOO_LONG'`: Certificate chain too long. 4591cb0ef41Sopenharmony_ci* `'CERT_REVOKED'`: Certificate revoked. 4601cb0ef41Sopenharmony_ci* `'INVALID_CA'`: Invalid CA certificate. 4611cb0ef41Sopenharmony_ci* `'PATH_LENGTH_EXCEEDED'`: Path length constraint exceeded. 4621cb0ef41Sopenharmony_ci* `'INVALID_PURPOSE'`: Unsupported certificate purpose. 4631cb0ef41Sopenharmony_ci* `'CERT_UNTRUSTED'`: Certificate not trusted. 4641cb0ef41Sopenharmony_ci* `'CERT_REJECTED'`: Certificate rejected. 4651cb0ef41Sopenharmony_ci* `'HOSTNAME_MISMATCH'`: Hostname mismatch. 4661cb0ef41Sopenharmony_ci 4671cb0ef41Sopenharmony_ci## Class: `tls.CryptoStream` 4681cb0ef41Sopenharmony_ci 4691cb0ef41Sopenharmony_ci<!-- YAML 4701cb0ef41Sopenharmony_ciadded: v0.3.4 4711cb0ef41Sopenharmony_cideprecated: v0.11.3 4721cb0ef41Sopenharmony_ci--> 4731cb0ef41Sopenharmony_ci 4741cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`tls.TLSSocket`][] instead. 4751cb0ef41Sopenharmony_ci 4761cb0ef41Sopenharmony_ciThe `tls.CryptoStream` class represents a stream of encrypted data. This class 4771cb0ef41Sopenharmony_ciis deprecated and should no longer be used. 4781cb0ef41Sopenharmony_ci 4791cb0ef41Sopenharmony_ci### `cryptoStream.bytesWritten` 4801cb0ef41Sopenharmony_ci 4811cb0ef41Sopenharmony_ci<!-- YAML 4821cb0ef41Sopenharmony_ciadded: v0.3.4 4831cb0ef41Sopenharmony_cideprecated: v0.11.3 4841cb0ef41Sopenharmony_ci--> 4851cb0ef41Sopenharmony_ci 4861cb0ef41Sopenharmony_ciThe `cryptoStream.bytesWritten` property returns the total number of bytes 4871cb0ef41Sopenharmony_ciwritten to the underlying socket _including_ the bytes required for the 4881cb0ef41Sopenharmony_ciimplementation of the TLS protocol. 4891cb0ef41Sopenharmony_ci 4901cb0ef41Sopenharmony_ci## Class: `tls.SecurePair` 4911cb0ef41Sopenharmony_ci 4921cb0ef41Sopenharmony_ci<!-- YAML 4931cb0ef41Sopenharmony_ciadded: v0.3.2 4941cb0ef41Sopenharmony_cideprecated: v0.11.3 4951cb0ef41Sopenharmony_ci--> 4961cb0ef41Sopenharmony_ci 4971cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`tls.TLSSocket`][] instead. 4981cb0ef41Sopenharmony_ci 4991cb0ef41Sopenharmony_ciReturned by [`tls.createSecurePair()`][]. 5001cb0ef41Sopenharmony_ci 5011cb0ef41Sopenharmony_ci### Event: `'secure'` 5021cb0ef41Sopenharmony_ci 5031cb0ef41Sopenharmony_ci<!-- YAML 5041cb0ef41Sopenharmony_ciadded: v0.3.2 5051cb0ef41Sopenharmony_cideprecated: v0.11.3 5061cb0ef41Sopenharmony_ci--> 5071cb0ef41Sopenharmony_ci 5081cb0ef41Sopenharmony_ciThe `'secure'` event is emitted by the `SecurePair` object once a secure 5091cb0ef41Sopenharmony_ciconnection has been established. 5101cb0ef41Sopenharmony_ci 5111cb0ef41Sopenharmony_ciAs with checking for the server 5121cb0ef41Sopenharmony_ci[`'secureConnection'`][] 5131cb0ef41Sopenharmony_cievent, `pair.cleartext.authorized` should be inspected to confirm whether the 5141cb0ef41Sopenharmony_cicertificate used is properly authorized. 5151cb0ef41Sopenharmony_ci 5161cb0ef41Sopenharmony_ci## Class: `tls.Server` 5171cb0ef41Sopenharmony_ci 5181cb0ef41Sopenharmony_ci<!-- YAML 5191cb0ef41Sopenharmony_ciadded: v0.3.2 5201cb0ef41Sopenharmony_ci--> 5211cb0ef41Sopenharmony_ci 5221cb0ef41Sopenharmony_ci* Extends: {net.Server} 5231cb0ef41Sopenharmony_ci 5241cb0ef41Sopenharmony_ciAccepts encrypted connections using TLS or SSL. 5251cb0ef41Sopenharmony_ci 5261cb0ef41Sopenharmony_ci### Event: `'connection'` 5271cb0ef41Sopenharmony_ci 5281cb0ef41Sopenharmony_ci<!-- YAML 5291cb0ef41Sopenharmony_ciadded: v0.3.2 5301cb0ef41Sopenharmony_ci--> 5311cb0ef41Sopenharmony_ci 5321cb0ef41Sopenharmony_ci* `socket` {stream.Duplex} 5331cb0ef41Sopenharmony_ci 5341cb0ef41Sopenharmony_ciThis event is emitted when a new TCP stream is established, before the TLS 5351cb0ef41Sopenharmony_cihandshake begins. `socket` is typically an object of type [`net.Socket`][] but 5361cb0ef41Sopenharmony_ciwill not receive events unlike the socket created from the [`net.Server`][] 5371cb0ef41Sopenharmony_ci`'connection'` event. Usually users will not want to access this event. 5381cb0ef41Sopenharmony_ci 5391cb0ef41Sopenharmony_ciThis event can also be explicitly emitted by users to inject connections 5401cb0ef41Sopenharmony_ciinto the TLS server. In that case, any [`Duplex`][] stream can be passed. 5411cb0ef41Sopenharmony_ci 5421cb0ef41Sopenharmony_ci### Event: `'keylog'` 5431cb0ef41Sopenharmony_ci 5441cb0ef41Sopenharmony_ci<!-- YAML 5451cb0ef41Sopenharmony_ciadded: 5461cb0ef41Sopenharmony_ci - v12.3.0 5471cb0ef41Sopenharmony_ci - v10.20.0 5481cb0ef41Sopenharmony_ci--> 5491cb0ef41Sopenharmony_ci 5501cb0ef41Sopenharmony_ci* `line` {Buffer} Line of ASCII text, in NSS `SSLKEYLOGFILE` format. 5511cb0ef41Sopenharmony_ci* `tlsSocket` {tls.TLSSocket} The `tls.TLSSocket` instance on which it was 5521cb0ef41Sopenharmony_ci generated. 5531cb0ef41Sopenharmony_ci 5541cb0ef41Sopenharmony_ciThe `keylog` event is emitted when key material is generated or received by 5551cb0ef41Sopenharmony_cia connection to this server (typically before handshake has completed, but not 5561cb0ef41Sopenharmony_cinecessarily). This keying material can be stored for debugging, as it allows 5571cb0ef41Sopenharmony_cicaptured TLS traffic to be decrypted. It may be emitted multiple times for 5581cb0ef41Sopenharmony_cieach socket. 5591cb0ef41Sopenharmony_ci 5601cb0ef41Sopenharmony_ciA typical use case is to append received lines to a common text file, which 5611cb0ef41Sopenharmony_ciis later used by software (such as Wireshark) to decrypt the traffic: 5621cb0ef41Sopenharmony_ci 5631cb0ef41Sopenharmony_ci```js 5641cb0ef41Sopenharmony_ciconst logFile = fs.createWriteStream('/tmp/ssl-keys.log', { flags: 'a' }); 5651cb0ef41Sopenharmony_ci// ... 5661cb0ef41Sopenharmony_ciserver.on('keylog', (line, tlsSocket) => { 5671cb0ef41Sopenharmony_ci if (tlsSocket.remoteAddress !== '...') 5681cb0ef41Sopenharmony_ci return; // Only log keys for a particular IP 5691cb0ef41Sopenharmony_ci logFile.write(line); 5701cb0ef41Sopenharmony_ci}); 5711cb0ef41Sopenharmony_ci``` 5721cb0ef41Sopenharmony_ci 5731cb0ef41Sopenharmony_ci### Event: `'newSession'` 5741cb0ef41Sopenharmony_ci 5751cb0ef41Sopenharmony_ci<!-- YAML 5761cb0ef41Sopenharmony_ciadded: v0.9.2 5771cb0ef41Sopenharmony_cichanges: 5781cb0ef41Sopenharmony_ci - version: v0.11.12 5791cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node-v0.x-archive/pull/7118 5801cb0ef41Sopenharmony_ci description: The `callback` argument is now supported. 5811cb0ef41Sopenharmony_ci--> 5821cb0ef41Sopenharmony_ci 5831cb0ef41Sopenharmony_ciThe `'newSession'` event is emitted upon creation of a new TLS session. This may 5841cb0ef41Sopenharmony_cibe used to store sessions in external storage. The data should be provided to 5851cb0ef41Sopenharmony_cithe [`'resumeSession'`][] callback. 5861cb0ef41Sopenharmony_ci 5871cb0ef41Sopenharmony_ciThe listener callback is passed three arguments when called: 5881cb0ef41Sopenharmony_ci 5891cb0ef41Sopenharmony_ci* `sessionId` {Buffer} The TLS session identifier 5901cb0ef41Sopenharmony_ci* `sessionData` {Buffer} The TLS session data 5911cb0ef41Sopenharmony_ci* `callback` {Function} A callback function taking no arguments that must be 5921cb0ef41Sopenharmony_ci invoked in order for data to be sent or received over the secure connection. 5931cb0ef41Sopenharmony_ci 5941cb0ef41Sopenharmony_ciListening for this event will have an effect only on connections established 5951cb0ef41Sopenharmony_ciafter the addition of the event listener. 5961cb0ef41Sopenharmony_ci 5971cb0ef41Sopenharmony_ci### Event: `'OCSPRequest'` 5981cb0ef41Sopenharmony_ci 5991cb0ef41Sopenharmony_ci<!-- YAML 6001cb0ef41Sopenharmony_ciadded: v0.11.13 6011cb0ef41Sopenharmony_ci--> 6021cb0ef41Sopenharmony_ci 6031cb0ef41Sopenharmony_ciThe `'OCSPRequest'` event is emitted when the client sends a certificate status 6041cb0ef41Sopenharmony_cirequest. The listener callback is passed three arguments when called: 6051cb0ef41Sopenharmony_ci 6061cb0ef41Sopenharmony_ci* `certificate` {Buffer} The server certificate 6071cb0ef41Sopenharmony_ci* `issuer` {Buffer} The issuer's certificate 6081cb0ef41Sopenharmony_ci* `callback` {Function} A callback function that must be invoked to provide 6091cb0ef41Sopenharmony_ci the results of the OCSP request. 6101cb0ef41Sopenharmony_ci 6111cb0ef41Sopenharmony_ciThe server's current certificate can be parsed to obtain the OCSP URL 6121cb0ef41Sopenharmony_ciand certificate ID; after obtaining an OCSP response, `callback(null, resp)` is 6131cb0ef41Sopenharmony_cithen invoked, where `resp` is a `Buffer` instance containing the OCSP response. 6141cb0ef41Sopenharmony_ciBoth `certificate` and `issuer` are `Buffer` DER-representations of the 6151cb0ef41Sopenharmony_ciprimary and issuer's certificates. These can be used to obtain the OCSP 6161cb0ef41Sopenharmony_cicertificate ID and OCSP endpoint URL. 6171cb0ef41Sopenharmony_ci 6181cb0ef41Sopenharmony_ciAlternatively, `callback(null, null)` may be called, indicating that there was 6191cb0ef41Sopenharmony_cino OCSP response. 6201cb0ef41Sopenharmony_ci 6211cb0ef41Sopenharmony_ciCalling `callback(err)` will result in a `socket.destroy(err)` call. 6221cb0ef41Sopenharmony_ci 6231cb0ef41Sopenharmony_ciThe typical flow of an OCSP request is as follows: 6241cb0ef41Sopenharmony_ci 6251cb0ef41Sopenharmony_ci1. Client connects to the server and sends an `'OCSPRequest'` (via the status 6261cb0ef41Sopenharmony_ci info extension in ClientHello). 6271cb0ef41Sopenharmony_ci2. Server receives the request and emits the `'OCSPRequest'` event, calling the 6281cb0ef41Sopenharmony_ci listener if registered. 6291cb0ef41Sopenharmony_ci3. Server extracts the OCSP URL from either the `certificate` or `issuer` and 6301cb0ef41Sopenharmony_ci performs an [OCSP request][] to the CA. 6311cb0ef41Sopenharmony_ci4. Server receives `'OCSPResponse'` from the CA and sends it back to the client 6321cb0ef41Sopenharmony_ci via the `callback` argument 6331cb0ef41Sopenharmony_ci5. Client validates the response and either destroys the socket or performs a 6341cb0ef41Sopenharmony_ci handshake. 6351cb0ef41Sopenharmony_ci 6361cb0ef41Sopenharmony_ciThe `issuer` can be `null` if the certificate is either self-signed or the 6371cb0ef41Sopenharmony_ciissuer is not in the root certificates list. (An issuer may be provided 6381cb0ef41Sopenharmony_civia the `ca` option when establishing the TLS connection.) 6391cb0ef41Sopenharmony_ci 6401cb0ef41Sopenharmony_ciListening for this event will have an effect only on connections established 6411cb0ef41Sopenharmony_ciafter the addition of the event listener. 6421cb0ef41Sopenharmony_ci 6431cb0ef41Sopenharmony_ciAn npm module like [asn1.js][] may be used to parse the certificates. 6441cb0ef41Sopenharmony_ci 6451cb0ef41Sopenharmony_ci### Event: `'resumeSession'` 6461cb0ef41Sopenharmony_ci 6471cb0ef41Sopenharmony_ci<!-- YAML 6481cb0ef41Sopenharmony_ciadded: v0.9.2 6491cb0ef41Sopenharmony_ci--> 6501cb0ef41Sopenharmony_ci 6511cb0ef41Sopenharmony_ciThe `'resumeSession'` event is emitted when the client requests to resume a 6521cb0ef41Sopenharmony_ciprevious TLS session. The listener callback is passed two arguments when 6531cb0ef41Sopenharmony_cicalled: 6541cb0ef41Sopenharmony_ci 6551cb0ef41Sopenharmony_ci* `sessionId` {Buffer} The TLS session identifier 6561cb0ef41Sopenharmony_ci* `callback` {Function} A callback function to be called when the prior session 6571cb0ef41Sopenharmony_ci has been recovered: `callback([err[, sessionData]])` 6581cb0ef41Sopenharmony_ci * `err` {Error} 6591cb0ef41Sopenharmony_ci * `sessionData` {Buffer} 6601cb0ef41Sopenharmony_ci 6611cb0ef41Sopenharmony_ciThe event listener should perform a lookup in external storage for the 6621cb0ef41Sopenharmony_ci`sessionData` saved by the [`'newSession'`][] event handler using the given 6631cb0ef41Sopenharmony_ci`sessionId`. If found, call `callback(null, sessionData)` to resume the session. 6641cb0ef41Sopenharmony_ciIf not found, the session cannot be resumed. `callback()` must be called 6651cb0ef41Sopenharmony_ciwithout `sessionData` so that the handshake can continue and a new session can 6661cb0ef41Sopenharmony_cibe created. It is possible to call `callback(err)` to terminate the incoming 6671cb0ef41Sopenharmony_ciconnection and destroy the socket. 6681cb0ef41Sopenharmony_ci 6691cb0ef41Sopenharmony_ciListening for this event will have an effect only on connections established 6701cb0ef41Sopenharmony_ciafter the addition of the event listener. 6711cb0ef41Sopenharmony_ci 6721cb0ef41Sopenharmony_ciThe following illustrates resuming a TLS session: 6731cb0ef41Sopenharmony_ci 6741cb0ef41Sopenharmony_ci```js 6751cb0ef41Sopenharmony_ciconst tlsSessionStore = {}; 6761cb0ef41Sopenharmony_ciserver.on('newSession', (id, data, cb) => { 6771cb0ef41Sopenharmony_ci tlsSessionStore[id.toString('hex')] = data; 6781cb0ef41Sopenharmony_ci cb(); 6791cb0ef41Sopenharmony_ci}); 6801cb0ef41Sopenharmony_ciserver.on('resumeSession', (id, cb) => { 6811cb0ef41Sopenharmony_ci cb(null, tlsSessionStore[id.toString('hex')] || null); 6821cb0ef41Sopenharmony_ci}); 6831cb0ef41Sopenharmony_ci``` 6841cb0ef41Sopenharmony_ci 6851cb0ef41Sopenharmony_ci### Event: `'secureConnection'` 6861cb0ef41Sopenharmony_ci 6871cb0ef41Sopenharmony_ci<!-- YAML 6881cb0ef41Sopenharmony_ciadded: v0.3.2 6891cb0ef41Sopenharmony_ci--> 6901cb0ef41Sopenharmony_ci 6911cb0ef41Sopenharmony_ciThe `'secureConnection'` event is emitted after the handshaking process for a 6921cb0ef41Sopenharmony_cinew connection has successfully completed. The listener callback is passed a 6931cb0ef41Sopenharmony_cisingle argument when called: 6941cb0ef41Sopenharmony_ci 6951cb0ef41Sopenharmony_ci* `tlsSocket` {tls.TLSSocket} The established TLS socket. 6961cb0ef41Sopenharmony_ci 6971cb0ef41Sopenharmony_ciThe `tlsSocket.authorized` property is a `boolean` indicating whether the 6981cb0ef41Sopenharmony_ciclient has been verified by one of the supplied Certificate Authorities for the 6991cb0ef41Sopenharmony_ciserver. If `tlsSocket.authorized` is `false`, then `socket.authorizationError` 7001cb0ef41Sopenharmony_ciis set to describe how authorization failed. Depending on the settings 7011cb0ef41Sopenharmony_ciof the TLS server, unauthorized connections may still be accepted. 7021cb0ef41Sopenharmony_ci 7031cb0ef41Sopenharmony_ciThe `tlsSocket.alpnProtocol` property is a string that contains the selected 7041cb0ef41Sopenharmony_ciALPN protocol. When ALPN has no selected protocol, `tlsSocket.alpnProtocol` 7051cb0ef41Sopenharmony_ciequals `false`. 7061cb0ef41Sopenharmony_ci 7071cb0ef41Sopenharmony_ciThe `tlsSocket.servername` property is a string containing the server name 7081cb0ef41Sopenharmony_cirequested via SNI. 7091cb0ef41Sopenharmony_ci 7101cb0ef41Sopenharmony_ci### Event: `'tlsClientError'` 7111cb0ef41Sopenharmony_ci 7121cb0ef41Sopenharmony_ci<!-- YAML 7131cb0ef41Sopenharmony_ciadded: v6.0.0 7141cb0ef41Sopenharmony_ci--> 7151cb0ef41Sopenharmony_ci 7161cb0ef41Sopenharmony_ciThe `'tlsClientError'` event is emitted when an error occurs before a secure 7171cb0ef41Sopenharmony_ciconnection is established. The listener callback is passed two arguments when 7181cb0ef41Sopenharmony_cicalled: 7191cb0ef41Sopenharmony_ci 7201cb0ef41Sopenharmony_ci* `exception` {Error} The `Error` object describing the error 7211cb0ef41Sopenharmony_ci* `tlsSocket` {tls.TLSSocket} The `tls.TLSSocket` instance from which the 7221cb0ef41Sopenharmony_ci error originated. 7231cb0ef41Sopenharmony_ci 7241cb0ef41Sopenharmony_ci### `server.addContext(hostname, context)` 7251cb0ef41Sopenharmony_ci 7261cb0ef41Sopenharmony_ci<!-- YAML 7271cb0ef41Sopenharmony_ciadded: v0.5.3 7281cb0ef41Sopenharmony_ci--> 7291cb0ef41Sopenharmony_ci 7301cb0ef41Sopenharmony_ci* `hostname` {string} A SNI host name or wildcard (e.g. `'*'`) 7311cb0ef41Sopenharmony_ci* `context` {Object|tls.SecureContext} An object containing any of the possible 7321cb0ef41Sopenharmony_ci properties from the [`tls.createSecureContext()`][] `options` arguments 7331cb0ef41Sopenharmony_ci (e.g. `key`, `cert`, `ca`, etc), or a TLS context object created with 7341cb0ef41Sopenharmony_ci [`tls.createSecureContext()`][] itself. 7351cb0ef41Sopenharmony_ci 7361cb0ef41Sopenharmony_ciThe `server.addContext()` method adds a secure context that will be used if 7371cb0ef41Sopenharmony_cithe client request's SNI name matches the supplied `hostname` (or wildcard). 7381cb0ef41Sopenharmony_ci 7391cb0ef41Sopenharmony_ciWhen there are multiple matching contexts, the most recently added one is 7401cb0ef41Sopenharmony_ciused. 7411cb0ef41Sopenharmony_ci 7421cb0ef41Sopenharmony_ci### `server.address()` 7431cb0ef41Sopenharmony_ci 7441cb0ef41Sopenharmony_ci<!-- YAML 7451cb0ef41Sopenharmony_ciadded: v0.6.0 7461cb0ef41Sopenharmony_ci--> 7471cb0ef41Sopenharmony_ci 7481cb0ef41Sopenharmony_ci* Returns: {Object} 7491cb0ef41Sopenharmony_ci 7501cb0ef41Sopenharmony_ciReturns the bound address, the address family name, and port of the 7511cb0ef41Sopenharmony_ciserver as reported by the operating system. See [`net.Server.address()`][] for 7521cb0ef41Sopenharmony_cimore information. 7531cb0ef41Sopenharmony_ci 7541cb0ef41Sopenharmony_ci### `server.close([callback])` 7551cb0ef41Sopenharmony_ci 7561cb0ef41Sopenharmony_ci<!-- YAML 7571cb0ef41Sopenharmony_ciadded: v0.3.2 7581cb0ef41Sopenharmony_ci--> 7591cb0ef41Sopenharmony_ci 7601cb0ef41Sopenharmony_ci* `callback` {Function} A listener callback that will be registered to listen 7611cb0ef41Sopenharmony_ci for the server instance's `'close'` event. 7621cb0ef41Sopenharmony_ci* Returns: {tls.Server} 7631cb0ef41Sopenharmony_ci 7641cb0ef41Sopenharmony_ciThe `server.close()` method stops the server from accepting new connections. 7651cb0ef41Sopenharmony_ci 7661cb0ef41Sopenharmony_ciThis function operates asynchronously. The `'close'` event will be emitted 7671cb0ef41Sopenharmony_ciwhen the server has no more open connections. 7681cb0ef41Sopenharmony_ci 7691cb0ef41Sopenharmony_ci### `server.getTicketKeys()` 7701cb0ef41Sopenharmony_ci 7711cb0ef41Sopenharmony_ci<!-- YAML 7721cb0ef41Sopenharmony_ciadded: v3.0.0 7731cb0ef41Sopenharmony_ci--> 7741cb0ef41Sopenharmony_ci 7751cb0ef41Sopenharmony_ci* Returns: {Buffer} A 48-byte buffer containing the session ticket keys. 7761cb0ef41Sopenharmony_ci 7771cb0ef41Sopenharmony_ciReturns the session ticket keys. 7781cb0ef41Sopenharmony_ci 7791cb0ef41Sopenharmony_ciSee [Session Resumption][] for more information. 7801cb0ef41Sopenharmony_ci 7811cb0ef41Sopenharmony_ci### `server.listen()` 7821cb0ef41Sopenharmony_ci 7831cb0ef41Sopenharmony_ciStarts the server listening for encrypted connections. 7841cb0ef41Sopenharmony_ciThis method is identical to [`server.listen()`][] from [`net.Server`][]. 7851cb0ef41Sopenharmony_ci 7861cb0ef41Sopenharmony_ci### `server.setSecureContext(options)` 7871cb0ef41Sopenharmony_ci 7881cb0ef41Sopenharmony_ci<!-- YAML 7891cb0ef41Sopenharmony_ciadded: v11.0.0 7901cb0ef41Sopenharmony_ci--> 7911cb0ef41Sopenharmony_ci 7921cb0ef41Sopenharmony_ci* `options` {Object} An object containing any of the possible properties from 7931cb0ef41Sopenharmony_ci the [`tls.createSecureContext()`][] `options` arguments (e.g. `key`, `cert`, 7941cb0ef41Sopenharmony_ci `ca`, etc). 7951cb0ef41Sopenharmony_ci 7961cb0ef41Sopenharmony_ciThe `server.setSecureContext()` method replaces the secure context of an 7971cb0ef41Sopenharmony_ciexisting server. Existing connections to the server are not interrupted. 7981cb0ef41Sopenharmony_ci 7991cb0ef41Sopenharmony_ci### `server.setTicketKeys(keys)` 8001cb0ef41Sopenharmony_ci 8011cb0ef41Sopenharmony_ci<!-- YAML 8021cb0ef41Sopenharmony_ciadded: v3.0.0 8031cb0ef41Sopenharmony_ci--> 8041cb0ef41Sopenharmony_ci 8051cb0ef41Sopenharmony_ci* `keys` {Buffer|TypedArray|DataView} A 48-byte buffer containing the session 8061cb0ef41Sopenharmony_ci ticket keys. 8071cb0ef41Sopenharmony_ci 8081cb0ef41Sopenharmony_ciSets the session ticket keys. 8091cb0ef41Sopenharmony_ci 8101cb0ef41Sopenharmony_ciChanges to the ticket keys are effective only for future server connections. 8111cb0ef41Sopenharmony_ciExisting or currently pending server connections will use the previous keys. 8121cb0ef41Sopenharmony_ci 8131cb0ef41Sopenharmony_ciSee [Session Resumption][] for more information. 8141cb0ef41Sopenharmony_ci 8151cb0ef41Sopenharmony_ci## Class: `tls.TLSSocket` 8161cb0ef41Sopenharmony_ci 8171cb0ef41Sopenharmony_ci<!-- YAML 8181cb0ef41Sopenharmony_ciadded: v0.11.4 8191cb0ef41Sopenharmony_ci--> 8201cb0ef41Sopenharmony_ci 8211cb0ef41Sopenharmony_ci* Extends: {net.Socket} 8221cb0ef41Sopenharmony_ci 8231cb0ef41Sopenharmony_ciPerforms transparent encryption of written data and all required TLS 8241cb0ef41Sopenharmony_cinegotiation. 8251cb0ef41Sopenharmony_ci 8261cb0ef41Sopenharmony_ciInstances of `tls.TLSSocket` implement the duplex [Stream][] interface. 8271cb0ef41Sopenharmony_ci 8281cb0ef41Sopenharmony_ciMethods that return TLS connection metadata (e.g. 8291cb0ef41Sopenharmony_ci[`tls.TLSSocket.getPeerCertificate()`][]) will only return data while the 8301cb0ef41Sopenharmony_ciconnection is open. 8311cb0ef41Sopenharmony_ci 8321cb0ef41Sopenharmony_ci### `new tls.TLSSocket(socket[, options])` 8331cb0ef41Sopenharmony_ci 8341cb0ef41Sopenharmony_ci<!-- YAML 8351cb0ef41Sopenharmony_ciadded: v0.11.4 8361cb0ef41Sopenharmony_cichanges: 8371cb0ef41Sopenharmony_ci - version: v12.2.0 8381cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/27497 8391cb0ef41Sopenharmony_ci description: The `enableTrace` option is now supported. 8401cb0ef41Sopenharmony_ci - version: v5.0.0 8411cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/2564 8421cb0ef41Sopenharmony_ci description: ALPN options are supported now. 8431cb0ef41Sopenharmony_ci--> 8441cb0ef41Sopenharmony_ci 8451cb0ef41Sopenharmony_ci* `socket` {net.Socket|stream.Duplex} 8461cb0ef41Sopenharmony_ci On the server side, any `Duplex` stream. On the client side, any 8471cb0ef41Sopenharmony_ci instance of [`net.Socket`][] (for generic `Duplex` stream support 8481cb0ef41Sopenharmony_ci on the client side, [`tls.connect()`][] must be used). 8491cb0ef41Sopenharmony_ci* `options` {Object} 8501cb0ef41Sopenharmony_ci * `enableTrace`: See [`tls.createServer()`][] 8511cb0ef41Sopenharmony_ci * `isServer`: The SSL/TLS protocol is asymmetrical, TLSSockets must know if 8521cb0ef41Sopenharmony_ci they are to behave as a server or a client. If `true` the TLS socket will be 8531cb0ef41Sopenharmony_ci instantiated as a server. **Default:** `false`. 8541cb0ef41Sopenharmony_ci * `server` {net.Server} A [`net.Server`][] instance. 8551cb0ef41Sopenharmony_ci * `requestCert`: Whether to authenticate the remote peer by requesting a 8561cb0ef41Sopenharmony_ci certificate. Clients always request a server certificate. Servers 8571cb0ef41Sopenharmony_ci (`isServer` is true) may set `requestCert` to true to request a client 8581cb0ef41Sopenharmony_ci certificate. 8591cb0ef41Sopenharmony_ci * `rejectUnauthorized`: See [`tls.createServer()`][] 8601cb0ef41Sopenharmony_ci * `ALPNProtocols`: See [`tls.createServer()`][] 8611cb0ef41Sopenharmony_ci * `SNICallback`: See [`tls.createServer()`][] 8621cb0ef41Sopenharmony_ci * `session` {Buffer} A `Buffer` instance containing a TLS session. 8631cb0ef41Sopenharmony_ci * `requestOCSP` {boolean} If `true`, specifies that the OCSP status request 8641cb0ef41Sopenharmony_ci extension will be added to the client hello and an `'OCSPResponse'` event 8651cb0ef41Sopenharmony_ci will be emitted on the socket before establishing a secure communication 8661cb0ef41Sopenharmony_ci * `secureContext`: TLS context object created with 8671cb0ef41Sopenharmony_ci [`tls.createSecureContext()`][]. If a `secureContext` is _not_ provided, one 8681cb0ef41Sopenharmony_ci will be created by passing the entire `options` object to 8691cb0ef41Sopenharmony_ci `tls.createSecureContext()`. 8701cb0ef41Sopenharmony_ci * ...: [`tls.createSecureContext()`][] options that are used if the 8711cb0ef41Sopenharmony_ci `secureContext` option is missing. Otherwise, they are ignored. 8721cb0ef41Sopenharmony_ci 8731cb0ef41Sopenharmony_ciConstruct a new `tls.TLSSocket` object from an existing TCP socket. 8741cb0ef41Sopenharmony_ci 8751cb0ef41Sopenharmony_ci### Event: `'keylog'` 8761cb0ef41Sopenharmony_ci 8771cb0ef41Sopenharmony_ci<!-- YAML 8781cb0ef41Sopenharmony_ciadded: 8791cb0ef41Sopenharmony_ci - v12.3.0 8801cb0ef41Sopenharmony_ci - v10.20.0 8811cb0ef41Sopenharmony_ci--> 8821cb0ef41Sopenharmony_ci 8831cb0ef41Sopenharmony_ci* `line` {Buffer} Line of ASCII text, in NSS `SSLKEYLOGFILE` format. 8841cb0ef41Sopenharmony_ci 8851cb0ef41Sopenharmony_ciThe `keylog` event is emitted on a `tls.TLSSocket` when key material 8861cb0ef41Sopenharmony_ciis generated or received by the socket. This keying material can be stored 8871cb0ef41Sopenharmony_cifor debugging, as it allows captured TLS traffic to be decrypted. It may 8881cb0ef41Sopenharmony_cibe emitted multiple times, before or after the handshake completes. 8891cb0ef41Sopenharmony_ci 8901cb0ef41Sopenharmony_ciA typical use case is to append received lines to a common text file, which 8911cb0ef41Sopenharmony_ciis later used by software (such as Wireshark) to decrypt the traffic: 8921cb0ef41Sopenharmony_ci 8931cb0ef41Sopenharmony_ci```js 8941cb0ef41Sopenharmony_ciconst logFile = fs.createWriteStream('/tmp/ssl-keys.log', { flags: 'a' }); 8951cb0ef41Sopenharmony_ci// ... 8961cb0ef41Sopenharmony_citlsSocket.on('keylog', (line) => logFile.write(line)); 8971cb0ef41Sopenharmony_ci``` 8981cb0ef41Sopenharmony_ci 8991cb0ef41Sopenharmony_ci### Event: `'OCSPResponse'` 9001cb0ef41Sopenharmony_ci 9011cb0ef41Sopenharmony_ci<!-- YAML 9021cb0ef41Sopenharmony_ciadded: v0.11.13 9031cb0ef41Sopenharmony_ci--> 9041cb0ef41Sopenharmony_ci 9051cb0ef41Sopenharmony_ciThe `'OCSPResponse'` event is emitted if the `requestOCSP` option was set 9061cb0ef41Sopenharmony_ciwhen the `tls.TLSSocket` was created and an OCSP response has been received. 9071cb0ef41Sopenharmony_ciThe listener callback is passed a single argument when called: 9081cb0ef41Sopenharmony_ci 9091cb0ef41Sopenharmony_ci* `response` {Buffer} The server's OCSP response 9101cb0ef41Sopenharmony_ci 9111cb0ef41Sopenharmony_ciTypically, the `response` is a digitally signed object from the server's CA that 9121cb0ef41Sopenharmony_cicontains information about server's certificate revocation status. 9131cb0ef41Sopenharmony_ci 9141cb0ef41Sopenharmony_ci### Event: `'secureConnect'` 9151cb0ef41Sopenharmony_ci 9161cb0ef41Sopenharmony_ci<!-- YAML 9171cb0ef41Sopenharmony_ciadded: v0.11.4 9181cb0ef41Sopenharmony_ci--> 9191cb0ef41Sopenharmony_ci 9201cb0ef41Sopenharmony_ciThe `'secureConnect'` event is emitted after the handshaking process for a new 9211cb0ef41Sopenharmony_ciconnection has successfully completed. The listener callback will be called 9221cb0ef41Sopenharmony_ciregardless of whether or not the server's certificate has been authorized. It 9231cb0ef41Sopenharmony_ciis the client's responsibility to check the `tlsSocket.authorized` property to 9241cb0ef41Sopenharmony_cidetermine if the server certificate was signed by one of the specified CAs. If 9251cb0ef41Sopenharmony_ci`tlsSocket.authorized === false`, then the error can be found by examining the 9261cb0ef41Sopenharmony_ci`tlsSocket.authorizationError` property. If ALPN was used, the 9271cb0ef41Sopenharmony_ci`tlsSocket.alpnProtocol` property can be checked to determine the negotiated 9281cb0ef41Sopenharmony_ciprotocol. 9291cb0ef41Sopenharmony_ci 9301cb0ef41Sopenharmony_ciThe `'secureConnect'` event is not emitted when a {tls.TLSSocket} is created 9311cb0ef41Sopenharmony_ciusing the `new tls.TLSSocket()` constructor. 9321cb0ef41Sopenharmony_ci 9331cb0ef41Sopenharmony_ci### Event: `'session'` 9341cb0ef41Sopenharmony_ci 9351cb0ef41Sopenharmony_ci<!-- YAML 9361cb0ef41Sopenharmony_ciadded: v11.10.0 9371cb0ef41Sopenharmony_ci--> 9381cb0ef41Sopenharmony_ci 9391cb0ef41Sopenharmony_ci* `session` {Buffer} 9401cb0ef41Sopenharmony_ci 9411cb0ef41Sopenharmony_ciThe `'session'` event is emitted on a client `tls.TLSSocket` when a new session 9421cb0ef41Sopenharmony_cior TLS ticket is available. This may or may not be before the handshake is 9431cb0ef41Sopenharmony_cicomplete, depending on the TLS protocol version that was negotiated. The event 9441cb0ef41Sopenharmony_ciis not emitted on the server, or if a new session was not created, for example, 9451cb0ef41Sopenharmony_ciwhen the connection was resumed. For some TLS protocol versions the event may be 9461cb0ef41Sopenharmony_ciemitted multiple times, in which case all the sessions can be used for 9471cb0ef41Sopenharmony_ciresumption. 9481cb0ef41Sopenharmony_ci 9491cb0ef41Sopenharmony_ciOn the client, the `session` can be provided to the `session` option of 9501cb0ef41Sopenharmony_ci[`tls.connect()`][] to resume the connection. 9511cb0ef41Sopenharmony_ci 9521cb0ef41Sopenharmony_ciSee [Session Resumption][] for more information. 9531cb0ef41Sopenharmony_ci 9541cb0ef41Sopenharmony_ciFor TLSv1.2 and below, [`tls.TLSSocket.getSession()`][] can be called once 9551cb0ef41Sopenharmony_cithe handshake is complete. For TLSv1.3, only ticket-based resumption is allowed 9561cb0ef41Sopenharmony_ciby the protocol, multiple tickets are sent, and the tickets aren't sent until 9571cb0ef41Sopenharmony_ciafter the handshake completes. So it is necessary to wait for the 9581cb0ef41Sopenharmony_ci`'session'` event to get a resumable session. Applications 9591cb0ef41Sopenharmony_cishould use the `'session'` event instead of `getSession()` to ensure 9601cb0ef41Sopenharmony_cithey will work for all TLS versions. Applications that only expect to 9611cb0ef41Sopenharmony_ciget or use one session should listen for this event only once: 9621cb0ef41Sopenharmony_ci 9631cb0ef41Sopenharmony_ci```js 9641cb0ef41Sopenharmony_citlsSocket.once('session', (session) => { 9651cb0ef41Sopenharmony_ci // The session can be used immediately or later. 9661cb0ef41Sopenharmony_ci tls.connect({ 9671cb0ef41Sopenharmony_ci session: session, 9681cb0ef41Sopenharmony_ci // Other connect options... 9691cb0ef41Sopenharmony_ci }); 9701cb0ef41Sopenharmony_ci}); 9711cb0ef41Sopenharmony_ci``` 9721cb0ef41Sopenharmony_ci 9731cb0ef41Sopenharmony_ci### `tlsSocket.address()` 9741cb0ef41Sopenharmony_ci 9751cb0ef41Sopenharmony_ci<!-- YAML 9761cb0ef41Sopenharmony_ciadded: v0.11.4 9771cb0ef41Sopenharmony_cichanges: 9781cb0ef41Sopenharmony_ci - version: v18.4.0 9791cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/43054 9801cb0ef41Sopenharmony_ci description: The `family` property now returns a string instead of a number. 9811cb0ef41Sopenharmony_ci - version: v18.0.0 9821cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41431 9831cb0ef41Sopenharmony_ci description: The `family` property now returns a number instead of a string. 9841cb0ef41Sopenharmony_ci--> 9851cb0ef41Sopenharmony_ci 9861cb0ef41Sopenharmony_ci* Returns: {Object} 9871cb0ef41Sopenharmony_ci 9881cb0ef41Sopenharmony_ciReturns the bound `address`, the address `family` name, and `port` of the 9891cb0ef41Sopenharmony_ciunderlying socket as reported by the operating system: 9901cb0ef41Sopenharmony_ci`{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`. 9911cb0ef41Sopenharmony_ci 9921cb0ef41Sopenharmony_ci### `tlsSocket.authorizationError` 9931cb0ef41Sopenharmony_ci 9941cb0ef41Sopenharmony_ci<!-- YAML 9951cb0ef41Sopenharmony_ciadded: v0.11.4 9961cb0ef41Sopenharmony_ci--> 9971cb0ef41Sopenharmony_ci 9981cb0ef41Sopenharmony_ciReturns the reason why the peer's certificate was not been verified. This 9991cb0ef41Sopenharmony_ciproperty is set only when `tlsSocket.authorized === false`. 10001cb0ef41Sopenharmony_ci 10011cb0ef41Sopenharmony_ci### `tlsSocket.authorized` 10021cb0ef41Sopenharmony_ci 10031cb0ef41Sopenharmony_ci<!-- YAML 10041cb0ef41Sopenharmony_ciadded: v0.11.4 10051cb0ef41Sopenharmony_ci--> 10061cb0ef41Sopenharmony_ci 10071cb0ef41Sopenharmony_ci* {boolean} 10081cb0ef41Sopenharmony_ci 10091cb0ef41Sopenharmony_ciThis property is `true` if the peer certificate was signed by one of the CAs 10101cb0ef41Sopenharmony_cispecified when creating the `tls.TLSSocket` instance, otherwise `false`. 10111cb0ef41Sopenharmony_ci 10121cb0ef41Sopenharmony_ci### `tlsSocket.disableRenegotiation()` 10131cb0ef41Sopenharmony_ci 10141cb0ef41Sopenharmony_ci<!-- YAML 10151cb0ef41Sopenharmony_ciadded: v8.4.0 10161cb0ef41Sopenharmony_ci--> 10171cb0ef41Sopenharmony_ci 10181cb0ef41Sopenharmony_ciDisables TLS renegotiation for this `TLSSocket` instance. Once called, attempts 10191cb0ef41Sopenharmony_cito renegotiate will trigger an `'error'` event on the `TLSSocket`. 10201cb0ef41Sopenharmony_ci 10211cb0ef41Sopenharmony_ci### `tlsSocket.enableTrace()` 10221cb0ef41Sopenharmony_ci 10231cb0ef41Sopenharmony_ci<!-- YAML 10241cb0ef41Sopenharmony_ciadded: v12.2.0 10251cb0ef41Sopenharmony_ci--> 10261cb0ef41Sopenharmony_ci 10271cb0ef41Sopenharmony_ciWhen enabled, TLS packet trace information is written to `stderr`. This can be 10281cb0ef41Sopenharmony_ciused to debug TLS connection problems. 10291cb0ef41Sopenharmony_ci 10301cb0ef41Sopenharmony_ciThe format of the output is identical to the output of 10311cb0ef41Sopenharmony_ci`openssl s_client -trace` or `openssl s_server -trace`. While it is produced by 10321cb0ef41Sopenharmony_ciOpenSSL's `SSL_trace()` function, the format is undocumented, can change 10331cb0ef41Sopenharmony_ciwithout notice, and should not be relied on. 10341cb0ef41Sopenharmony_ci 10351cb0ef41Sopenharmony_ci### `tlsSocket.encrypted` 10361cb0ef41Sopenharmony_ci 10371cb0ef41Sopenharmony_ci<!-- YAML 10381cb0ef41Sopenharmony_ciadded: v0.11.4 10391cb0ef41Sopenharmony_ci--> 10401cb0ef41Sopenharmony_ci 10411cb0ef41Sopenharmony_ciAlways returns `true`. This may be used to distinguish TLS sockets from regular 10421cb0ef41Sopenharmony_ci`net.Socket` instances. 10431cb0ef41Sopenharmony_ci 10441cb0ef41Sopenharmony_ci### `tlsSocket.exportKeyingMaterial(length, label[, context])` 10451cb0ef41Sopenharmony_ci 10461cb0ef41Sopenharmony_ci<!-- YAML 10471cb0ef41Sopenharmony_ciadded: 10481cb0ef41Sopenharmony_ci - v13.10.0 10491cb0ef41Sopenharmony_ci - v12.17.0 10501cb0ef41Sopenharmony_ci--> 10511cb0ef41Sopenharmony_ci 10521cb0ef41Sopenharmony_ci* `length` {number} number of bytes to retrieve from keying material 10531cb0ef41Sopenharmony_ci 10541cb0ef41Sopenharmony_ci* `label` {string} an application specific label, typically this will be a 10551cb0ef41Sopenharmony_ci value from the 10561cb0ef41Sopenharmony_ci [IANA Exporter Label Registry](https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#exporter-labels). 10571cb0ef41Sopenharmony_ci 10581cb0ef41Sopenharmony_ci* `context` {Buffer} Optionally provide a context. 10591cb0ef41Sopenharmony_ci 10601cb0ef41Sopenharmony_ci* Returns: {Buffer} requested bytes of the keying material 10611cb0ef41Sopenharmony_ci 10621cb0ef41Sopenharmony_ciKeying material is used for validations to prevent different kind of attacks in 10631cb0ef41Sopenharmony_cinetwork protocols, for example in the specifications of IEEE 802.1X. 10641cb0ef41Sopenharmony_ci 10651cb0ef41Sopenharmony_ciExample 10661cb0ef41Sopenharmony_ci 10671cb0ef41Sopenharmony_ci```js 10681cb0ef41Sopenharmony_ciconst keyingMaterial = tlsSocket.exportKeyingMaterial( 10691cb0ef41Sopenharmony_ci 128, 10701cb0ef41Sopenharmony_ci 'client finished'); 10711cb0ef41Sopenharmony_ci 10721cb0ef41Sopenharmony_ci/* 10731cb0ef41Sopenharmony_ci Example return value of keyingMaterial: 10741cb0ef41Sopenharmony_ci <Buffer 76 26 af 99 c5 56 8e 42 09 91 ef 9f 93 cb ad 6c 7b 65 f8 53 f1 d8 d9 10751cb0ef41Sopenharmony_ci 12 5a 33 b8 b5 25 df 7b 37 9f e0 e2 4f b8 67 83 a3 2f cd 5d 41 42 4c 91 10761cb0ef41Sopenharmony_ci 74 ef 2c ... 78 more bytes> 10771cb0ef41Sopenharmony_ci*/ 10781cb0ef41Sopenharmony_ci``` 10791cb0ef41Sopenharmony_ci 10801cb0ef41Sopenharmony_ciSee the OpenSSL [`SSL_export_keying_material`][] documentation for more 10811cb0ef41Sopenharmony_ciinformation. 10821cb0ef41Sopenharmony_ci 10831cb0ef41Sopenharmony_ci### `tlsSocket.getCertificate()` 10841cb0ef41Sopenharmony_ci 10851cb0ef41Sopenharmony_ci<!-- YAML 10861cb0ef41Sopenharmony_ciadded: v11.2.0 10871cb0ef41Sopenharmony_ci--> 10881cb0ef41Sopenharmony_ci 10891cb0ef41Sopenharmony_ci* Returns: {Object} 10901cb0ef41Sopenharmony_ci 10911cb0ef41Sopenharmony_ciReturns an object representing the local certificate. The returned object has 10921cb0ef41Sopenharmony_cisome properties corresponding to the fields of the certificate. 10931cb0ef41Sopenharmony_ci 10941cb0ef41Sopenharmony_ciSee [`tls.TLSSocket.getPeerCertificate()`][] for an example of the certificate 10951cb0ef41Sopenharmony_cistructure. 10961cb0ef41Sopenharmony_ci 10971cb0ef41Sopenharmony_ciIf there is no local certificate, an empty object will be returned. If the 10981cb0ef41Sopenharmony_cisocket has been destroyed, `null` will be returned. 10991cb0ef41Sopenharmony_ci 11001cb0ef41Sopenharmony_ci### `tlsSocket.getCipher()` 11011cb0ef41Sopenharmony_ci 11021cb0ef41Sopenharmony_ci<!-- YAML 11031cb0ef41Sopenharmony_ciadded: v0.11.4 11041cb0ef41Sopenharmony_cichanges: 11051cb0ef41Sopenharmony_ci - version: 11061cb0ef41Sopenharmony_ci - v13.4.0 11071cb0ef41Sopenharmony_ci - v12.16.0 11081cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/30637 11091cb0ef41Sopenharmony_ci description: Return the IETF cipher name as `standardName`. 11101cb0ef41Sopenharmony_ci - version: v12.0.0 11111cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/26625 11121cb0ef41Sopenharmony_ci description: Return the minimum cipher version, instead of a fixed string 11131cb0ef41Sopenharmony_ci (`'TLSv1/SSLv3'`). 11141cb0ef41Sopenharmony_ci--> 11151cb0ef41Sopenharmony_ci 11161cb0ef41Sopenharmony_ci* Returns: {Object} 11171cb0ef41Sopenharmony_ci * `name` {string} OpenSSL name for the cipher suite. 11181cb0ef41Sopenharmony_ci * `standardName` {string} IETF name for the cipher suite. 11191cb0ef41Sopenharmony_ci * `version` {string} The minimum TLS protocol version supported by this cipher 11201cb0ef41Sopenharmony_ci suite. For the actual negotiated protocol, see [`tls.TLSSocket.getProtocol()`][]. 11211cb0ef41Sopenharmony_ci 11221cb0ef41Sopenharmony_ciReturns an object containing information on the negotiated cipher suite. 11231cb0ef41Sopenharmony_ci 11241cb0ef41Sopenharmony_ciFor example, a TLSv1.2 protocol with AES256-SHA cipher: 11251cb0ef41Sopenharmony_ci 11261cb0ef41Sopenharmony_ci```json 11271cb0ef41Sopenharmony_ci{ 11281cb0ef41Sopenharmony_ci "name": "AES256-SHA", 11291cb0ef41Sopenharmony_ci "standardName": "TLS_RSA_WITH_AES_256_CBC_SHA", 11301cb0ef41Sopenharmony_ci "version": "SSLv3" 11311cb0ef41Sopenharmony_ci} 11321cb0ef41Sopenharmony_ci``` 11331cb0ef41Sopenharmony_ci 11341cb0ef41Sopenharmony_ciSee 11351cb0ef41Sopenharmony_ci[SSL\_CIPHER\_get\_name](https://www.openssl.org/docs/man1.1.1/man3/SSL_CIPHER_get_name.html) 11361cb0ef41Sopenharmony_cifor more information. 11371cb0ef41Sopenharmony_ci 11381cb0ef41Sopenharmony_ci### `tlsSocket.getEphemeralKeyInfo()` 11391cb0ef41Sopenharmony_ci 11401cb0ef41Sopenharmony_ci<!-- YAML 11411cb0ef41Sopenharmony_ciadded: v5.0.0 11421cb0ef41Sopenharmony_ci--> 11431cb0ef41Sopenharmony_ci 11441cb0ef41Sopenharmony_ci* Returns: {Object} 11451cb0ef41Sopenharmony_ci 11461cb0ef41Sopenharmony_ciReturns an object representing the type, name, and size of parameter of 11471cb0ef41Sopenharmony_cian ephemeral key exchange in [perfect forward secrecy][] on a client 11481cb0ef41Sopenharmony_ciconnection. It returns an empty object when the key exchange is not 11491cb0ef41Sopenharmony_ciephemeral. As this is only supported on a client socket; `null` is returned 11501cb0ef41Sopenharmony_ciif called on a server socket. The supported types are `'DH'` and `'ECDH'`. The 11511cb0ef41Sopenharmony_ci`name` property is available only when type is `'ECDH'`. 11521cb0ef41Sopenharmony_ci 11531cb0ef41Sopenharmony_ciFor example: `{ type: 'ECDH', name: 'prime256v1', size: 256 }`. 11541cb0ef41Sopenharmony_ci 11551cb0ef41Sopenharmony_ci### `tlsSocket.getFinished()` 11561cb0ef41Sopenharmony_ci 11571cb0ef41Sopenharmony_ci<!-- YAML 11581cb0ef41Sopenharmony_ciadded: v9.9.0 11591cb0ef41Sopenharmony_ci--> 11601cb0ef41Sopenharmony_ci 11611cb0ef41Sopenharmony_ci* Returns: {Buffer|undefined} The latest `Finished` message that has been 11621cb0ef41Sopenharmony_ci sent to the socket as part of a SSL/TLS handshake, or `undefined` if 11631cb0ef41Sopenharmony_ci no `Finished` message has been sent yet. 11641cb0ef41Sopenharmony_ci 11651cb0ef41Sopenharmony_ciAs the `Finished` messages are message digests of the complete handshake 11661cb0ef41Sopenharmony_ci(with a total of 192 bits for TLS 1.0 and more for SSL 3.0), they can 11671cb0ef41Sopenharmony_cibe used for external authentication procedures when the authentication 11681cb0ef41Sopenharmony_ciprovided by SSL/TLS is not desired or is not enough. 11691cb0ef41Sopenharmony_ci 11701cb0ef41Sopenharmony_ciCorresponds to the `SSL_get_finished` routine in OpenSSL and may be used 11711cb0ef41Sopenharmony_cito implement the `tls-unique` channel binding from [RFC 5929][]. 11721cb0ef41Sopenharmony_ci 11731cb0ef41Sopenharmony_ci### `tlsSocket.getPeerCertificate([detailed])` 11741cb0ef41Sopenharmony_ci 11751cb0ef41Sopenharmony_ci<!-- YAML 11761cb0ef41Sopenharmony_ciadded: v0.11.4 11771cb0ef41Sopenharmony_ci--> 11781cb0ef41Sopenharmony_ci 11791cb0ef41Sopenharmony_ci* `detailed` {boolean} Include the full certificate chain if `true`, otherwise 11801cb0ef41Sopenharmony_ci include just the peer's certificate. 11811cb0ef41Sopenharmony_ci* Returns: {Object} A certificate object. 11821cb0ef41Sopenharmony_ci 11831cb0ef41Sopenharmony_ciReturns an object representing the peer's certificate. If the peer does not 11841cb0ef41Sopenharmony_ciprovide a certificate, an empty object will be returned. If the socket has been 11851cb0ef41Sopenharmony_cidestroyed, `null` will be returned. 11861cb0ef41Sopenharmony_ci 11871cb0ef41Sopenharmony_ciIf the full certificate chain was requested, each certificate will include an 11881cb0ef41Sopenharmony_ci`issuerCertificate` property containing an object representing its issuer's 11891cb0ef41Sopenharmony_cicertificate. 11901cb0ef41Sopenharmony_ci 11911cb0ef41Sopenharmony_ci#### Certificate object 11921cb0ef41Sopenharmony_ci 11931cb0ef41Sopenharmony_ci<!-- YAML 11941cb0ef41Sopenharmony_cichanges: 11951cb0ef41Sopenharmony_ci - version: v18.13.0 11961cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/44935 11971cb0ef41Sopenharmony_ci description: Add "ca" property. 11981cb0ef41Sopenharmony_ci - version: 11991cb0ef41Sopenharmony_ci - v17.2.0 12001cb0ef41Sopenharmony_ci - v16.14.0 12011cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/39809 12021cb0ef41Sopenharmony_ci description: Add fingerprint512. 12031cb0ef41Sopenharmony_ci - version: v11.4.0 12041cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/24358 12051cb0ef41Sopenharmony_ci description: Support Elliptic Curve public key info. 12061cb0ef41Sopenharmony_ci--> 12071cb0ef41Sopenharmony_ci 12081cb0ef41Sopenharmony_ciA certificate object has properties corresponding to the fields of the 12091cb0ef41Sopenharmony_cicertificate. 12101cb0ef41Sopenharmony_ci 12111cb0ef41Sopenharmony_ci* `ca` {boolean} `true` if a Certificate Authority (CA), `false` otherwise. 12121cb0ef41Sopenharmony_ci* `raw` {Buffer} The DER encoded X.509 certificate data. 12131cb0ef41Sopenharmony_ci* `subject` {Object} The certificate subject, described in terms of 12141cb0ef41Sopenharmony_ci Country (`C`), StateOrProvince (`ST`), Locality (`L`), Organization (`O`), 12151cb0ef41Sopenharmony_ci OrganizationalUnit (`OU`), and CommonName (`CN`). The CommonName is typically 12161cb0ef41Sopenharmony_ci a DNS name with TLS certificates. Example: 12171cb0ef41Sopenharmony_ci `{C: 'UK', ST: 'BC', L: 'Metro', O: 'Node Fans', OU: 'Docs', CN: 'example.com'}`. 12181cb0ef41Sopenharmony_ci* `issuer` {Object} The certificate issuer, described in the same terms as the 12191cb0ef41Sopenharmony_ci `subject`. 12201cb0ef41Sopenharmony_ci* `valid_from` {string} The date-time the certificate is valid from. 12211cb0ef41Sopenharmony_ci* `valid_to` {string} The date-time the certificate is valid to. 12221cb0ef41Sopenharmony_ci* `serialNumber` {string} The certificate serial number, as a hex string. 12231cb0ef41Sopenharmony_ci Example: `'B9B0D332A1AA5635'`. 12241cb0ef41Sopenharmony_ci* `fingerprint` {string} The SHA-1 digest of the DER encoded certificate. It is 12251cb0ef41Sopenharmony_ci returned as a `:` separated hexadecimal string. Example: `'2A:7A:C2:DD:...'`. 12261cb0ef41Sopenharmony_ci* `fingerprint256` {string} The SHA-256 digest of the DER encoded certificate. 12271cb0ef41Sopenharmony_ci It is returned as a `:` separated hexadecimal string. Example: 12281cb0ef41Sopenharmony_ci `'2A:7A:C2:DD:...'`. 12291cb0ef41Sopenharmony_ci* `fingerprint512` {string} The SHA-512 digest of the DER encoded certificate. 12301cb0ef41Sopenharmony_ci It is returned as a `:` separated hexadecimal string. Example: 12311cb0ef41Sopenharmony_ci `'2A:7A:C2:DD:...'`. 12321cb0ef41Sopenharmony_ci* `ext_key_usage` {Array} (Optional) The extended key usage, a set of OIDs. 12331cb0ef41Sopenharmony_ci* `subjectaltname` {string} (Optional) A string containing concatenated names 12341cb0ef41Sopenharmony_ci for the subject, an alternative to the `subject` names. 12351cb0ef41Sopenharmony_ci* `infoAccess` {Array} (Optional) An array describing the AuthorityInfoAccess, 12361cb0ef41Sopenharmony_ci used with OCSP. 12371cb0ef41Sopenharmony_ci* `issuerCertificate` {Object} (Optional) The issuer certificate object. For 12381cb0ef41Sopenharmony_ci self-signed certificates, this may be a circular reference. 12391cb0ef41Sopenharmony_ci 12401cb0ef41Sopenharmony_ciThe certificate may contain information about the public key, depending on 12411cb0ef41Sopenharmony_cithe key type. 12421cb0ef41Sopenharmony_ci 12431cb0ef41Sopenharmony_ciFor RSA keys, the following properties may be defined: 12441cb0ef41Sopenharmony_ci 12451cb0ef41Sopenharmony_ci* `bits` {number} The RSA bit size. Example: `1024`. 12461cb0ef41Sopenharmony_ci* `exponent` {string} The RSA exponent, as a string in hexadecimal number 12471cb0ef41Sopenharmony_ci notation. Example: `'0x010001'`. 12481cb0ef41Sopenharmony_ci* `modulus` {string} The RSA modulus, as a hexadecimal string. Example: 12491cb0ef41Sopenharmony_ci `'B56CE45CB7...'`. 12501cb0ef41Sopenharmony_ci* `pubkey` {Buffer} The public key. 12511cb0ef41Sopenharmony_ci 12521cb0ef41Sopenharmony_ciFor EC keys, the following properties may be defined: 12531cb0ef41Sopenharmony_ci 12541cb0ef41Sopenharmony_ci* `pubkey` {Buffer} The public key. 12551cb0ef41Sopenharmony_ci* `bits` {number} The key size in bits. Example: `256`. 12561cb0ef41Sopenharmony_ci* `asn1Curve` {string} (Optional) The ASN.1 name of the OID of the elliptic 12571cb0ef41Sopenharmony_ci curve. Well-known curves are identified by an OID. While it is unusual, it is 12581cb0ef41Sopenharmony_ci possible that the curve is identified by its mathematical properties, in which 12591cb0ef41Sopenharmony_ci case it will not have an OID. Example: `'prime256v1'`. 12601cb0ef41Sopenharmony_ci* `nistCurve` {string} (Optional) The NIST name for the elliptic curve, if it 12611cb0ef41Sopenharmony_ci has one (not all well-known curves have been assigned names by NIST). Example: 12621cb0ef41Sopenharmony_ci `'P-256'`. 12631cb0ef41Sopenharmony_ci 12641cb0ef41Sopenharmony_ciExample certificate: 12651cb0ef41Sopenharmony_ci 12661cb0ef41Sopenharmony_ci<!-- eslint-skip --> 12671cb0ef41Sopenharmony_ci 12681cb0ef41Sopenharmony_ci```js 12691cb0ef41Sopenharmony_ci{ subject: 12701cb0ef41Sopenharmony_ci { OU: [ 'Domain Control Validated', 'PositiveSSL Wildcard' ], 12711cb0ef41Sopenharmony_ci CN: '*.nodejs.org' }, 12721cb0ef41Sopenharmony_ci issuer: 12731cb0ef41Sopenharmony_ci { C: 'GB', 12741cb0ef41Sopenharmony_ci ST: 'Greater Manchester', 12751cb0ef41Sopenharmony_ci L: 'Salford', 12761cb0ef41Sopenharmony_ci O: 'COMODO CA Limited', 12771cb0ef41Sopenharmony_ci CN: 'COMODO RSA Domain Validation Secure Server CA' }, 12781cb0ef41Sopenharmony_ci subjectaltname: 'DNS:*.nodejs.org, DNS:nodejs.org', 12791cb0ef41Sopenharmony_ci infoAccess: 12801cb0ef41Sopenharmony_ci { 'CA Issuers - URI': 12811cb0ef41Sopenharmony_ci [ 'http://crt.comodoca.com/COMODORSADomainValidationSecureServerCA.crt' ], 12821cb0ef41Sopenharmony_ci 'OCSP - URI': [ 'http://ocsp.comodoca.com' ] }, 12831cb0ef41Sopenharmony_ci modulus: 'B56CE45CB740B09A13F64AC543B712FF9EE8E4C284B542A1708A27E82A8D151CA178153E12E6DDA15BF70FFD96CB8A88618641BDFCCA03527E665B70D779C8A349A6F88FD4EF6557180BD4C98192872BCFE3AF56E863C09DDD8BC1EC58DF9D94F914F0369102B2870BECFA1348A0838C9C49BD1C20124B442477572347047506B1FCD658A80D0C44BCC16BC5C5496CFE6E4A8428EF654CD3D8972BF6E5BFAD59C93006830B5EB1056BBB38B53D1464FA6E02BFDF2FF66CD949486F0775EC43034EC2602AEFBF1703AD221DAA2A88353C3B6A688EFE8387811F645CEED7B3FE46E1F8B9F59FAD028F349B9BC14211D5830994D055EEA3D547911E07A0ADDEB8A82B9188E58720D95CD478EEC9AF1F17BE8141BE80906F1A339445A7EB5B285F68039B0F294598A7D1C0005FC22B5271B0752F58CCDEF8C8FD856FB7AE21C80B8A2CE983AE94046E53EDE4CB89F42502D31B5360771C01C80155918637490550E3F555E2EE75CC8C636DDE3633CFEDD62E91BF0F7688273694EEEBA20C2FC9F14A2A435517BC1D7373922463409AB603295CEB0BB53787A334C9CA3CA8B30005C5A62FC0715083462E00719A8FA3ED0A9828C3871360A73F8B04A4FC1E71302844E9BB9940B77E745C9D91F226D71AFCAD4B113AAF68D92B24DDB4A2136B55A1CD1ADF39605B63CB639038ED0F4C987689866743A68769CC55847E4A06D6E2E3F1', 12841cb0ef41Sopenharmony_ci exponent: '0x10001', 12851cb0ef41Sopenharmony_ci pubkey: <Buffer ... >, 12861cb0ef41Sopenharmony_ci valid_from: 'Aug 14 00:00:00 2017 GMT', 12871cb0ef41Sopenharmony_ci valid_to: 'Nov 20 23:59:59 2019 GMT', 12881cb0ef41Sopenharmony_ci fingerprint: '01:02:59:D9:C3:D2:0D:08:F7:82:4E:44:A4:B4:53:C5:E2:3A:87:4D', 12891cb0ef41Sopenharmony_ci 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', 12901cb0ef41Sopenharmony_ci 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', 12911cb0ef41Sopenharmony_ci ext_key_usage: [ '1.3.6.1.5.5.7.3.1', '1.3.6.1.5.5.7.3.2' ], 12921cb0ef41Sopenharmony_ci serialNumber: '66593D57F20CBC573E433381B5FEC280', 12931cb0ef41Sopenharmony_ci raw: <Buffer ... > } 12941cb0ef41Sopenharmony_ci``` 12951cb0ef41Sopenharmony_ci 12961cb0ef41Sopenharmony_ci### `tlsSocket.getPeerFinished()` 12971cb0ef41Sopenharmony_ci 12981cb0ef41Sopenharmony_ci<!-- YAML 12991cb0ef41Sopenharmony_ciadded: v9.9.0 13001cb0ef41Sopenharmony_ci--> 13011cb0ef41Sopenharmony_ci 13021cb0ef41Sopenharmony_ci* Returns: {Buffer|undefined} The latest `Finished` message that is expected 13031cb0ef41Sopenharmony_ci or has actually been received from the socket as part of a SSL/TLS handshake, 13041cb0ef41Sopenharmony_ci or `undefined` if there is no `Finished` message so far. 13051cb0ef41Sopenharmony_ci 13061cb0ef41Sopenharmony_ciAs the `Finished` messages are message digests of the complete handshake 13071cb0ef41Sopenharmony_ci(with a total of 192 bits for TLS 1.0 and more for SSL 3.0), they can 13081cb0ef41Sopenharmony_cibe used for external authentication procedures when the authentication 13091cb0ef41Sopenharmony_ciprovided by SSL/TLS is not desired or is not enough. 13101cb0ef41Sopenharmony_ci 13111cb0ef41Sopenharmony_ciCorresponds to the `SSL_get_peer_finished` routine in OpenSSL and may be used 13121cb0ef41Sopenharmony_cito implement the `tls-unique` channel binding from [RFC 5929][]. 13131cb0ef41Sopenharmony_ci 13141cb0ef41Sopenharmony_ci### `tlsSocket.getPeerX509Certificate()` 13151cb0ef41Sopenharmony_ci 13161cb0ef41Sopenharmony_ci<!-- YAML 13171cb0ef41Sopenharmony_ciadded: v15.9.0 13181cb0ef41Sopenharmony_ci--> 13191cb0ef41Sopenharmony_ci 13201cb0ef41Sopenharmony_ci* Returns: {X509Certificate} 13211cb0ef41Sopenharmony_ci 13221cb0ef41Sopenharmony_ciReturns the peer certificate as an {X509Certificate} object. 13231cb0ef41Sopenharmony_ci 13241cb0ef41Sopenharmony_ciIf there is no peer certificate, or the socket has been destroyed, 13251cb0ef41Sopenharmony_ci`undefined` will be returned. 13261cb0ef41Sopenharmony_ci 13271cb0ef41Sopenharmony_ci### `tlsSocket.getProtocol()` 13281cb0ef41Sopenharmony_ci 13291cb0ef41Sopenharmony_ci<!-- YAML 13301cb0ef41Sopenharmony_ciadded: v5.7.0 13311cb0ef41Sopenharmony_ci--> 13321cb0ef41Sopenharmony_ci 13331cb0ef41Sopenharmony_ci* Returns: {string|null} 13341cb0ef41Sopenharmony_ci 13351cb0ef41Sopenharmony_ciReturns a string containing the negotiated SSL/TLS protocol version of the 13361cb0ef41Sopenharmony_cicurrent connection. The value `'unknown'` will be returned for connected 13371cb0ef41Sopenharmony_cisockets that have not completed the handshaking process. The value `null` will 13381cb0ef41Sopenharmony_cibe returned for server sockets or disconnected client sockets. 13391cb0ef41Sopenharmony_ci 13401cb0ef41Sopenharmony_ciProtocol versions are: 13411cb0ef41Sopenharmony_ci 13421cb0ef41Sopenharmony_ci* `'SSLv3'` 13431cb0ef41Sopenharmony_ci* `'TLSv1'` 13441cb0ef41Sopenharmony_ci* `'TLSv1.1'` 13451cb0ef41Sopenharmony_ci* `'TLSv1.2'` 13461cb0ef41Sopenharmony_ci* `'TLSv1.3'` 13471cb0ef41Sopenharmony_ci 13481cb0ef41Sopenharmony_ciSee the OpenSSL [`SSL_get_version`][] documentation for more information. 13491cb0ef41Sopenharmony_ci 13501cb0ef41Sopenharmony_ci### `tlsSocket.getSession()` 13511cb0ef41Sopenharmony_ci 13521cb0ef41Sopenharmony_ci<!-- YAML 13531cb0ef41Sopenharmony_ciadded: v0.11.4 13541cb0ef41Sopenharmony_ci--> 13551cb0ef41Sopenharmony_ci 13561cb0ef41Sopenharmony_ci* {Buffer} 13571cb0ef41Sopenharmony_ci 13581cb0ef41Sopenharmony_ciReturns the TLS session data or `undefined` if no session was 13591cb0ef41Sopenharmony_cinegotiated. On the client, the data can be provided to the `session` option of 13601cb0ef41Sopenharmony_ci[`tls.connect()`][] to resume the connection. On the server, it may be useful 13611cb0ef41Sopenharmony_cifor debugging. 13621cb0ef41Sopenharmony_ci 13631cb0ef41Sopenharmony_ciSee [Session Resumption][] for more information. 13641cb0ef41Sopenharmony_ci 13651cb0ef41Sopenharmony_ciNote: `getSession()` works only for TLSv1.2 and below. For TLSv1.3, applications 13661cb0ef41Sopenharmony_cimust use the [`'session'`][] event (it also works for TLSv1.2 and below). 13671cb0ef41Sopenharmony_ci 13681cb0ef41Sopenharmony_ci### `tlsSocket.getSharedSigalgs()` 13691cb0ef41Sopenharmony_ci 13701cb0ef41Sopenharmony_ci<!-- YAML 13711cb0ef41Sopenharmony_ciadded: v12.11.0 13721cb0ef41Sopenharmony_ci--> 13731cb0ef41Sopenharmony_ci 13741cb0ef41Sopenharmony_ci* Returns: {Array} List of signature algorithms shared between the server and 13751cb0ef41Sopenharmony_ci the client in the order of decreasing preference. 13761cb0ef41Sopenharmony_ci 13771cb0ef41Sopenharmony_ciSee 13781cb0ef41Sopenharmony_ci[SSL\_get\_shared\_sigalgs](https://www.openssl.org/docs/man1.1.1/man3/SSL_get_shared_sigalgs.html) 13791cb0ef41Sopenharmony_cifor more information. 13801cb0ef41Sopenharmony_ci 13811cb0ef41Sopenharmony_ci### `tlsSocket.getTLSTicket()` 13821cb0ef41Sopenharmony_ci 13831cb0ef41Sopenharmony_ci<!-- YAML 13841cb0ef41Sopenharmony_ciadded: v0.11.4 13851cb0ef41Sopenharmony_ci--> 13861cb0ef41Sopenharmony_ci 13871cb0ef41Sopenharmony_ci* {Buffer} 13881cb0ef41Sopenharmony_ci 13891cb0ef41Sopenharmony_ciFor a client, returns the TLS session ticket if one is available, or 13901cb0ef41Sopenharmony_ci`undefined`. For a server, always returns `undefined`. 13911cb0ef41Sopenharmony_ci 13921cb0ef41Sopenharmony_ciIt may be useful for debugging. 13931cb0ef41Sopenharmony_ci 13941cb0ef41Sopenharmony_ciSee [Session Resumption][] for more information. 13951cb0ef41Sopenharmony_ci 13961cb0ef41Sopenharmony_ci### `tlsSocket.getX509Certificate()` 13971cb0ef41Sopenharmony_ci 13981cb0ef41Sopenharmony_ci<!-- YAML 13991cb0ef41Sopenharmony_ciadded: v15.9.0 14001cb0ef41Sopenharmony_ci--> 14011cb0ef41Sopenharmony_ci 14021cb0ef41Sopenharmony_ci* Returns: {X509Certificate} 14031cb0ef41Sopenharmony_ci 14041cb0ef41Sopenharmony_ciReturns the local certificate as an {X509Certificate} object. 14051cb0ef41Sopenharmony_ci 14061cb0ef41Sopenharmony_ciIf there is no local certificate, or the socket has been destroyed, 14071cb0ef41Sopenharmony_ci`undefined` will be returned. 14081cb0ef41Sopenharmony_ci 14091cb0ef41Sopenharmony_ci### `tlsSocket.isSessionReused()` 14101cb0ef41Sopenharmony_ci 14111cb0ef41Sopenharmony_ci<!-- YAML 14121cb0ef41Sopenharmony_ciadded: v0.5.6 14131cb0ef41Sopenharmony_ci--> 14141cb0ef41Sopenharmony_ci 14151cb0ef41Sopenharmony_ci* Returns: {boolean} `true` if the session was reused, `false` otherwise. 14161cb0ef41Sopenharmony_ci 14171cb0ef41Sopenharmony_ciSee [Session Resumption][] for more information. 14181cb0ef41Sopenharmony_ci 14191cb0ef41Sopenharmony_ci### `tlsSocket.localAddress` 14201cb0ef41Sopenharmony_ci 14211cb0ef41Sopenharmony_ci<!-- YAML 14221cb0ef41Sopenharmony_ciadded: v0.11.4 14231cb0ef41Sopenharmony_ci--> 14241cb0ef41Sopenharmony_ci 14251cb0ef41Sopenharmony_ci* {string} 14261cb0ef41Sopenharmony_ci 14271cb0ef41Sopenharmony_ciReturns the string representation of the local IP address. 14281cb0ef41Sopenharmony_ci 14291cb0ef41Sopenharmony_ci### `tlsSocket.localPort` 14301cb0ef41Sopenharmony_ci 14311cb0ef41Sopenharmony_ci<!-- YAML 14321cb0ef41Sopenharmony_ciadded: v0.11.4 14331cb0ef41Sopenharmony_ci--> 14341cb0ef41Sopenharmony_ci 14351cb0ef41Sopenharmony_ci* {integer} 14361cb0ef41Sopenharmony_ci 14371cb0ef41Sopenharmony_ciReturns the numeric representation of the local port. 14381cb0ef41Sopenharmony_ci 14391cb0ef41Sopenharmony_ci### `tlsSocket.remoteAddress` 14401cb0ef41Sopenharmony_ci 14411cb0ef41Sopenharmony_ci<!-- YAML 14421cb0ef41Sopenharmony_ciadded: v0.11.4 14431cb0ef41Sopenharmony_ci--> 14441cb0ef41Sopenharmony_ci 14451cb0ef41Sopenharmony_ci* {string} 14461cb0ef41Sopenharmony_ci 14471cb0ef41Sopenharmony_ciReturns the string representation of the remote IP address. For example, 14481cb0ef41Sopenharmony_ci`'74.125.127.100'` or `'2001:4860:a005::68'`. 14491cb0ef41Sopenharmony_ci 14501cb0ef41Sopenharmony_ci### `tlsSocket.remoteFamily` 14511cb0ef41Sopenharmony_ci 14521cb0ef41Sopenharmony_ci<!-- YAML 14531cb0ef41Sopenharmony_ciadded: v0.11.4 14541cb0ef41Sopenharmony_ci--> 14551cb0ef41Sopenharmony_ci 14561cb0ef41Sopenharmony_ci* {string} 14571cb0ef41Sopenharmony_ci 14581cb0ef41Sopenharmony_ciReturns the string representation of the remote IP family. `'IPv4'` or `'IPv6'`. 14591cb0ef41Sopenharmony_ci 14601cb0ef41Sopenharmony_ci### `tlsSocket.remotePort` 14611cb0ef41Sopenharmony_ci 14621cb0ef41Sopenharmony_ci<!-- YAML 14631cb0ef41Sopenharmony_ciadded: v0.11.4 14641cb0ef41Sopenharmony_ci--> 14651cb0ef41Sopenharmony_ci 14661cb0ef41Sopenharmony_ci* {integer} 14671cb0ef41Sopenharmony_ci 14681cb0ef41Sopenharmony_ciReturns the numeric representation of the remote port. For example, `443`. 14691cb0ef41Sopenharmony_ci 14701cb0ef41Sopenharmony_ci### `tlsSocket.renegotiate(options, callback)` 14711cb0ef41Sopenharmony_ci 14721cb0ef41Sopenharmony_ci<!-- YAML 14731cb0ef41Sopenharmony_ciadded: v0.11.8 14741cb0ef41Sopenharmony_cichanges: 14751cb0ef41Sopenharmony_ci - version: v18.0.0 14761cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 14771cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 14781cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 14791cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 14801cb0ef41Sopenharmony_ci--> 14811cb0ef41Sopenharmony_ci 14821cb0ef41Sopenharmony_ci* `options` {Object} 14831cb0ef41Sopenharmony_ci * `rejectUnauthorized` {boolean} If not `false`, the server certificate is 14841cb0ef41Sopenharmony_ci verified against the list of supplied CAs. An `'error'` event is emitted if 14851cb0ef41Sopenharmony_ci verification fails; `err.code` contains the OpenSSL error code. **Default:** 14861cb0ef41Sopenharmony_ci `true`. 14871cb0ef41Sopenharmony_ci * `requestCert` 14881cb0ef41Sopenharmony_ci 14891cb0ef41Sopenharmony_ci* `callback` {Function} If `renegotiate()` returned `true`, callback is 14901cb0ef41Sopenharmony_ci attached once to the `'secure'` event. If `renegotiate()` returned `false`, 14911cb0ef41Sopenharmony_ci `callback` will be called in the next tick with an error, unless the 14921cb0ef41Sopenharmony_ci `tlsSocket` has been destroyed, in which case `callback` will not be called 14931cb0ef41Sopenharmony_ci at all. 14941cb0ef41Sopenharmony_ci 14951cb0ef41Sopenharmony_ci* Returns: {boolean} `true` if renegotiation was initiated, `false` otherwise. 14961cb0ef41Sopenharmony_ci 14971cb0ef41Sopenharmony_ciThe `tlsSocket.renegotiate()` method initiates a TLS renegotiation process. 14981cb0ef41Sopenharmony_ciUpon completion, the `callback` function will be passed a single argument 14991cb0ef41Sopenharmony_cithat is either an `Error` (if the request failed) or `null`. 15001cb0ef41Sopenharmony_ci 15011cb0ef41Sopenharmony_ciThis method can be used to request a peer's certificate after the secure 15021cb0ef41Sopenharmony_ciconnection has been established. 15031cb0ef41Sopenharmony_ci 15041cb0ef41Sopenharmony_ciWhen running as the server, the socket will be destroyed with an error after 15051cb0ef41Sopenharmony_ci`handshakeTimeout` timeout. 15061cb0ef41Sopenharmony_ci 15071cb0ef41Sopenharmony_ciFor TLSv1.3, renegotiation cannot be initiated, it is not supported by the 15081cb0ef41Sopenharmony_ciprotocol. 15091cb0ef41Sopenharmony_ci 15101cb0ef41Sopenharmony_ci### `tlsSocket.setMaxSendFragment(size)` 15111cb0ef41Sopenharmony_ci 15121cb0ef41Sopenharmony_ci<!-- YAML 15131cb0ef41Sopenharmony_ciadded: v0.11.11 15141cb0ef41Sopenharmony_ci--> 15151cb0ef41Sopenharmony_ci 15161cb0ef41Sopenharmony_ci* `size` {number} The maximum TLS fragment size. The maximum value is `16384`. 15171cb0ef41Sopenharmony_ci **Default:** `16384`. 15181cb0ef41Sopenharmony_ci* Returns: {boolean} 15191cb0ef41Sopenharmony_ci 15201cb0ef41Sopenharmony_ciThe `tlsSocket.setMaxSendFragment()` method sets the maximum TLS fragment size. 15211cb0ef41Sopenharmony_ciReturns `true` if setting the limit succeeded; `false` otherwise. 15221cb0ef41Sopenharmony_ci 15231cb0ef41Sopenharmony_ciSmaller fragment sizes decrease the buffering latency on the client: larger 15241cb0ef41Sopenharmony_cifragments are buffered by the TLS layer until the entire fragment is received 15251cb0ef41Sopenharmony_ciand its integrity is verified; large fragments can span multiple roundtrips 15261cb0ef41Sopenharmony_ciand their processing can be delayed due to packet loss or reordering. However, 15271cb0ef41Sopenharmony_cismaller fragments add extra TLS framing bytes and CPU overhead, which may 15281cb0ef41Sopenharmony_cidecrease overall server throughput. 15291cb0ef41Sopenharmony_ci 15301cb0ef41Sopenharmony_ci## `tls.checkServerIdentity(hostname, cert)` 15311cb0ef41Sopenharmony_ci 15321cb0ef41Sopenharmony_ci<!-- YAML 15331cb0ef41Sopenharmony_ciadded: v0.8.4 15341cb0ef41Sopenharmony_cichanges: 15351cb0ef41Sopenharmony_ci - version: 15361cb0ef41Sopenharmony_ci - v17.3.1 15371cb0ef41Sopenharmony_ci - v16.13.2 15381cb0ef41Sopenharmony_ci - v14.18.3 15391cb0ef41Sopenharmony_ci - v12.22.9 15401cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs-private/node-private/pull/300 15411cb0ef41Sopenharmony_ci description: Support for `uniformResourceIdentifier` subject alternative 15421cb0ef41Sopenharmony_ci names has been disabled in response to CVE-2021-44531. 15431cb0ef41Sopenharmony_ci--> 15441cb0ef41Sopenharmony_ci 15451cb0ef41Sopenharmony_ci* `hostname` {string} The host name or IP address to verify the certificate 15461cb0ef41Sopenharmony_ci against. 15471cb0ef41Sopenharmony_ci* `cert` {Object} A [certificate object][] representing the peer's certificate. 15481cb0ef41Sopenharmony_ci* Returns: {Error|undefined} 15491cb0ef41Sopenharmony_ci 15501cb0ef41Sopenharmony_ciVerifies the certificate `cert` is issued to `hostname`. 15511cb0ef41Sopenharmony_ci 15521cb0ef41Sopenharmony_ciReturns {Error} object, populating it with `reason`, `host`, and `cert` on 15531cb0ef41Sopenharmony_cifailure. On success, returns {undefined}. 15541cb0ef41Sopenharmony_ci 15551cb0ef41Sopenharmony_ciThis function is intended to be used in combination with the 15561cb0ef41Sopenharmony_ci`checkServerIdentity` option that can be passed to [`tls.connect()`][] and as 15571cb0ef41Sopenharmony_cisuch operates on a [certificate object][]. For other purposes, consider using 15581cb0ef41Sopenharmony_ci[`x509.checkHost()`][] instead. 15591cb0ef41Sopenharmony_ci 15601cb0ef41Sopenharmony_ciThis function can be overwritten by providing an alternative function as the 15611cb0ef41Sopenharmony_ci`options.checkServerIdentity` option that is passed to `tls.connect()`. The 15621cb0ef41Sopenharmony_cioverwriting function can call `tls.checkServerIdentity()` of course, to augment 15631cb0ef41Sopenharmony_cithe checks done with additional verification. 15641cb0ef41Sopenharmony_ci 15651cb0ef41Sopenharmony_ciThis function is only called if the certificate passed all other checks, such as 15661cb0ef41Sopenharmony_cibeing issued by trusted CA (`options.ca`). 15671cb0ef41Sopenharmony_ci 15681cb0ef41Sopenharmony_ciEarlier versions of Node.js incorrectly accepted certificates for a given 15691cb0ef41Sopenharmony_ci`hostname` if a matching `uniformResourceIdentifier` subject alternative name 15701cb0ef41Sopenharmony_ciwas present (see [CVE-2021-44531][]). Applications that wish to accept 15711cb0ef41Sopenharmony_ci`uniformResourceIdentifier` subject alternative names can use a custom 15721cb0ef41Sopenharmony_ci`options.checkServerIdentity` function that implements the desired behavior. 15731cb0ef41Sopenharmony_ci 15741cb0ef41Sopenharmony_ci## `tls.connect(options[, callback])` 15751cb0ef41Sopenharmony_ci 15761cb0ef41Sopenharmony_ci<!-- YAML 15771cb0ef41Sopenharmony_ciadded: v0.11.3 15781cb0ef41Sopenharmony_cichanges: 15791cb0ef41Sopenharmony_ci - version: 15801cb0ef41Sopenharmony_ci - v15.1.0 15811cb0ef41Sopenharmony_ci - v14.18.0 15821cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35753 15831cb0ef41Sopenharmony_ci description: Added `onread` option. 15841cb0ef41Sopenharmony_ci - version: 15851cb0ef41Sopenharmony_ci - v14.1.0 15861cb0ef41Sopenharmony_ci - v13.14.0 15871cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/32786 15881cb0ef41Sopenharmony_ci description: The `highWaterMark` option is accepted now. 15891cb0ef41Sopenharmony_ci - version: 15901cb0ef41Sopenharmony_ci - v13.6.0 15911cb0ef41Sopenharmony_ci - v12.16.0 15921cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/23188 15931cb0ef41Sopenharmony_ci description: The `pskCallback` option is now supported. 15941cb0ef41Sopenharmony_ci - version: v12.9.0 15951cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/27836 15961cb0ef41Sopenharmony_ci description: Support the `allowHalfOpen` option. 15971cb0ef41Sopenharmony_ci - version: v12.4.0 15981cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/27816 15991cb0ef41Sopenharmony_ci description: The `hints` option is now supported. 16001cb0ef41Sopenharmony_ci - version: v12.2.0 16011cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/27497 16021cb0ef41Sopenharmony_ci description: The `enableTrace` option is now supported. 16031cb0ef41Sopenharmony_ci - version: 16041cb0ef41Sopenharmony_ci - v11.8.0 16051cb0ef41Sopenharmony_ci - v10.16.0 16061cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/25517 16071cb0ef41Sopenharmony_ci description: The `timeout` option is supported now. 16081cb0ef41Sopenharmony_ci - version: v8.0.0 16091cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/12839 16101cb0ef41Sopenharmony_ci description: The `lookup` option is supported now. 16111cb0ef41Sopenharmony_ci - version: v8.0.0 16121cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/11984 16131cb0ef41Sopenharmony_ci description: The `ALPNProtocols` option can be a `TypedArray` or 16141cb0ef41Sopenharmony_ci `DataView` now. 16151cb0ef41Sopenharmony_ci - version: 16161cb0ef41Sopenharmony_ci - v5.3.0 16171cb0ef41Sopenharmony_ci - v4.7.0 16181cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/4246 16191cb0ef41Sopenharmony_ci description: The `secureContext` option is supported now. 16201cb0ef41Sopenharmony_ci - version: v5.0.0 16211cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/2564 16221cb0ef41Sopenharmony_ci description: ALPN options are supported now. 16231cb0ef41Sopenharmony_ci--> 16241cb0ef41Sopenharmony_ci 16251cb0ef41Sopenharmony_ci* `options` {Object} 16261cb0ef41Sopenharmony_ci * `enableTrace`: See [`tls.createServer()`][] 16271cb0ef41Sopenharmony_ci * `host` {string} Host the client should connect to. **Default:** 16281cb0ef41Sopenharmony_ci `'localhost'`. 16291cb0ef41Sopenharmony_ci * `port` {number} Port the client should connect to. 16301cb0ef41Sopenharmony_ci * `path` {string} Creates Unix socket connection to path. If this option is 16311cb0ef41Sopenharmony_ci specified, `host` and `port` are ignored. 16321cb0ef41Sopenharmony_ci * `socket` {stream.Duplex} Establish secure connection on a given socket 16331cb0ef41Sopenharmony_ci rather than creating a new socket. Typically, this is an instance of 16341cb0ef41Sopenharmony_ci [`net.Socket`][], but any `Duplex` stream is allowed. 16351cb0ef41Sopenharmony_ci If this option is specified, `path`, `host`, and `port` are ignored, 16361cb0ef41Sopenharmony_ci except for certificate validation. Usually, a socket is already connected 16371cb0ef41Sopenharmony_ci when passed to `tls.connect()`, but it can be connected later. 16381cb0ef41Sopenharmony_ci Connection/disconnection/destruction of `socket` is the user's 16391cb0ef41Sopenharmony_ci responsibility; calling `tls.connect()` will not cause `net.connect()` to be 16401cb0ef41Sopenharmony_ci called. 16411cb0ef41Sopenharmony_ci * `allowHalfOpen` {boolean} If set to `false`, then the socket will 16421cb0ef41Sopenharmony_ci automatically end the writable side when the readable side ends. If the 16431cb0ef41Sopenharmony_ci `socket` option is set, this option has no effect. See the `allowHalfOpen` 16441cb0ef41Sopenharmony_ci option of [`net.Socket`][] for details. **Default:** `false`. 16451cb0ef41Sopenharmony_ci * `rejectUnauthorized` {boolean} If not `false`, the server certificate is 16461cb0ef41Sopenharmony_ci verified against the list of supplied CAs. An `'error'` event is emitted if 16471cb0ef41Sopenharmony_ci verification fails; `err.code` contains the OpenSSL error code. **Default:** 16481cb0ef41Sopenharmony_ci `true`. 16491cb0ef41Sopenharmony_ci * `pskCallback` {Function} 16501cb0ef41Sopenharmony_ci 16511cb0ef41Sopenharmony_ci * hint: {string} optional message sent from the server to help client 16521cb0ef41Sopenharmony_ci decide which identity to use during negotiation. 16531cb0ef41Sopenharmony_ci Always `null` if TLS 1.3 is used. 16541cb0ef41Sopenharmony_ci * Returns: {Object} in the form 16551cb0ef41Sopenharmony_ci `{ psk: <Buffer|TypedArray|DataView>, identity: <string> }` 16561cb0ef41Sopenharmony_ci or `null` to stop the negotiation process. `psk` must be 16571cb0ef41Sopenharmony_ci compatible with the selected cipher's digest. 16581cb0ef41Sopenharmony_ci `identity` must use UTF-8 encoding. 16591cb0ef41Sopenharmony_ci 16601cb0ef41Sopenharmony_ci When negotiating TLS-PSK (pre-shared keys), this function is called 16611cb0ef41Sopenharmony_ci with optional identity `hint` provided by the server or `null` 16621cb0ef41Sopenharmony_ci in case of TLS 1.3 where `hint` was removed. 16631cb0ef41Sopenharmony_ci It will be necessary to provide a custom `tls.checkServerIdentity()` 16641cb0ef41Sopenharmony_ci for the connection as the default one will try to check host name/IP 16651cb0ef41Sopenharmony_ci of the server against the certificate but that's not applicable for PSK 16661cb0ef41Sopenharmony_ci because there won't be a certificate present. 16671cb0ef41Sopenharmony_ci More information can be found in the [RFC 4279][]. 16681cb0ef41Sopenharmony_ci * `ALPNProtocols`: {string\[]|Buffer\[]|TypedArray\[]|DataView\[]|Buffer| 16691cb0ef41Sopenharmony_ci TypedArray|DataView} 16701cb0ef41Sopenharmony_ci An array of strings, `Buffer`s, `TypedArray`s, or `DataView`s, or a 16711cb0ef41Sopenharmony_ci single `Buffer`, `TypedArray`, or `DataView` containing the supported ALPN 16721cb0ef41Sopenharmony_ci protocols. `Buffer`s should have the format `[len][name][len][name]...` 16731cb0ef41Sopenharmony_ci e.g. `'\x08http/1.1\x08http/1.0'`, where the `len` byte is the length of the 16741cb0ef41Sopenharmony_ci next protocol name. Passing an array is usually much simpler, e.g. 16751cb0ef41Sopenharmony_ci `['http/1.1', 'http/1.0']`. Protocols earlier in the list have higher 16761cb0ef41Sopenharmony_ci preference than those later. 16771cb0ef41Sopenharmony_ci * `servername`: {string} Server name for the SNI (Server Name Indication) TLS 16781cb0ef41Sopenharmony_ci extension. It is the name of the host being connected to, and must be a host 16791cb0ef41Sopenharmony_ci name, and not an IP address. It can be used by a multi-homed server to 16801cb0ef41Sopenharmony_ci choose the correct certificate to present to the client, see the 16811cb0ef41Sopenharmony_ci `SNICallback` option to [`tls.createServer()`][]. 16821cb0ef41Sopenharmony_ci * `checkServerIdentity(servername, cert)` {Function} A callback function 16831cb0ef41Sopenharmony_ci to be used (instead of the builtin `tls.checkServerIdentity()` function) 16841cb0ef41Sopenharmony_ci when checking the server's host name (or the provided `servername` when 16851cb0ef41Sopenharmony_ci explicitly set) against the certificate. This should return an {Error} if 16861cb0ef41Sopenharmony_ci verification fails. The method should return `undefined` if the `servername` 16871cb0ef41Sopenharmony_ci and `cert` are verified. 16881cb0ef41Sopenharmony_ci * `session` {Buffer} A `Buffer` instance, containing TLS session. 16891cb0ef41Sopenharmony_ci * `minDHSize` {number} Minimum size of the DH parameter in bits to accept a 16901cb0ef41Sopenharmony_ci TLS connection. When a server offers a DH parameter with a size less 16911cb0ef41Sopenharmony_ci than `minDHSize`, the TLS connection is destroyed and an error is thrown. 16921cb0ef41Sopenharmony_ci **Default:** `1024`. 16931cb0ef41Sopenharmony_ci * `highWaterMark`: {number} Consistent with the readable stream `highWaterMark` parameter. 16941cb0ef41Sopenharmony_ci **Default:** `16 * 1024`. 16951cb0ef41Sopenharmony_ci * `secureContext`: TLS context object created with 16961cb0ef41Sopenharmony_ci [`tls.createSecureContext()`][]. If a `secureContext` is _not_ provided, one 16971cb0ef41Sopenharmony_ci will be created by passing the entire `options` object to 16981cb0ef41Sopenharmony_ci `tls.createSecureContext()`. 16991cb0ef41Sopenharmony_ci * `onread` {Object} If the `socket` option is missing, incoming data is 17001cb0ef41Sopenharmony_ci stored in a single `buffer` and passed to the supplied `callback` when 17011cb0ef41Sopenharmony_ci data arrives on the socket, otherwise the option is ignored. See the 17021cb0ef41Sopenharmony_ci `onread` option of [`net.Socket`][] for details. 17031cb0ef41Sopenharmony_ci * ...: [`tls.createSecureContext()`][] options that are used if the 17041cb0ef41Sopenharmony_ci `secureContext` option is missing, otherwise they are ignored. 17051cb0ef41Sopenharmony_ci * ...: Any [`socket.connect()`][] option not already listed. 17061cb0ef41Sopenharmony_ci* `callback` {Function} 17071cb0ef41Sopenharmony_ci* Returns: {tls.TLSSocket} 17081cb0ef41Sopenharmony_ci 17091cb0ef41Sopenharmony_ciThe `callback` function, if specified, will be added as a listener for the 17101cb0ef41Sopenharmony_ci[`'secureConnect'`][] event. 17111cb0ef41Sopenharmony_ci 17121cb0ef41Sopenharmony_ci`tls.connect()` returns a [`tls.TLSSocket`][] object. 17131cb0ef41Sopenharmony_ci 17141cb0ef41Sopenharmony_ciUnlike the `https` API, `tls.connect()` does not enable the 17151cb0ef41Sopenharmony_ciSNI (Server Name Indication) extension by default, which may cause some 17161cb0ef41Sopenharmony_ciservers to return an incorrect certificate or reject the connection 17171cb0ef41Sopenharmony_cialtogether. To enable SNI, set the `servername` option in addition 17181cb0ef41Sopenharmony_cito `host`. 17191cb0ef41Sopenharmony_ci 17201cb0ef41Sopenharmony_ciThe following illustrates a client for the echo server example from 17211cb0ef41Sopenharmony_ci[`tls.createServer()`][]: 17221cb0ef41Sopenharmony_ci 17231cb0ef41Sopenharmony_ci```js 17241cb0ef41Sopenharmony_ci// Assumes an echo server that is listening on port 8000. 17251cb0ef41Sopenharmony_ciconst tls = require('node:tls'); 17261cb0ef41Sopenharmony_ciconst fs = require('node:fs'); 17271cb0ef41Sopenharmony_ci 17281cb0ef41Sopenharmony_ciconst options = { 17291cb0ef41Sopenharmony_ci // Necessary only if the server requires client certificate authentication. 17301cb0ef41Sopenharmony_ci key: fs.readFileSync('client-key.pem'), 17311cb0ef41Sopenharmony_ci cert: fs.readFileSync('client-cert.pem'), 17321cb0ef41Sopenharmony_ci 17331cb0ef41Sopenharmony_ci // Necessary only if the server uses a self-signed certificate. 17341cb0ef41Sopenharmony_ci ca: [ fs.readFileSync('server-cert.pem') ], 17351cb0ef41Sopenharmony_ci 17361cb0ef41Sopenharmony_ci // Necessary only if the server's cert isn't for "localhost". 17371cb0ef41Sopenharmony_ci checkServerIdentity: () => { return null; }, 17381cb0ef41Sopenharmony_ci}; 17391cb0ef41Sopenharmony_ci 17401cb0ef41Sopenharmony_ciconst socket = tls.connect(8000, options, () => { 17411cb0ef41Sopenharmony_ci console.log('client connected', 17421cb0ef41Sopenharmony_ci socket.authorized ? 'authorized' : 'unauthorized'); 17431cb0ef41Sopenharmony_ci process.stdin.pipe(socket); 17441cb0ef41Sopenharmony_ci process.stdin.resume(); 17451cb0ef41Sopenharmony_ci}); 17461cb0ef41Sopenharmony_cisocket.setEncoding('utf8'); 17471cb0ef41Sopenharmony_cisocket.on('data', (data) => { 17481cb0ef41Sopenharmony_ci console.log(data); 17491cb0ef41Sopenharmony_ci}); 17501cb0ef41Sopenharmony_cisocket.on('end', () => { 17511cb0ef41Sopenharmony_ci console.log('server ends connection'); 17521cb0ef41Sopenharmony_ci}); 17531cb0ef41Sopenharmony_ci``` 17541cb0ef41Sopenharmony_ci 17551cb0ef41Sopenharmony_ci## `tls.connect(path[, options][, callback])` 17561cb0ef41Sopenharmony_ci 17571cb0ef41Sopenharmony_ci<!-- YAML 17581cb0ef41Sopenharmony_ciadded: v0.11.3 17591cb0ef41Sopenharmony_ci--> 17601cb0ef41Sopenharmony_ci 17611cb0ef41Sopenharmony_ci* `path` {string} Default value for `options.path`. 17621cb0ef41Sopenharmony_ci* `options` {Object} See [`tls.connect()`][]. 17631cb0ef41Sopenharmony_ci* `callback` {Function} See [`tls.connect()`][]. 17641cb0ef41Sopenharmony_ci* Returns: {tls.TLSSocket} 17651cb0ef41Sopenharmony_ci 17661cb0ef41Sopenharmony_ciSame as [`tls.connect()`][] except that `path` can be provided 17671cb0ef41Sopenharmony_cias an argument instead of an option. 17681cb0ef41Sopenharmony_ci 17691cb0ef41Sopenharmony_ciA path option, if specified, will take precedence over the path argument. 17701cb0ef41Sopenharmony_ci 17711cb0ef41Sopenharmony_ci## `tls.connect(port[, host][, options][, callback])` 17721cb0ef41Sopenharmony_ci 17731cb0ef41Sopenharmony_ci<!-- YAML 17741cb0ef41Sopenharmony_ciadded: v0.11.3 17751cb0ef41Sopenharmony_ci--> 17761cb0ef41Sopenharmony_ci 17771cb0ef41Sopenharmony_ci* `port` {number} Default value for `options.port`. 17781cb0ef41Sopenharmony_ci* `host` {string} Default value for `options.host`. 17791cb0ef41Sopenharmony_ci* `options` {Object} See [`tls.connect()`][]. 17801cb0ef41Sopenharmony_ci* `callback` {Function} See [`tls.connect()`][]. 17811cb0ef41Sopenharmony_ci* Returns: {tls.TLSSocket} 17821cb0ef41Sopenharmony_ci 17831cb0ef41Sopenharmony_ciSame as [`tls.connect()`][] except that `port` and `host` can be provided 17841cb0ef41Sopenharmony_cias arguments instead of options. 17851cb0ef41Sopenharmony_ci 17861cb0ef41Sopenharmony_ciA port or host option, if specified, will take precedence over any port or host 17871cb0ef41Sopenharmony_ciargument. 17881cb0ef41Sopenharmony_ci 17891cb0ef41Sopenharmony_ci## `tls.createSecureContext([options])` 17901cb0ef41Sopenharmony_ci 17911cb0ef41Sopenharmony_ci<!-- YAML 17921cb0ef41Sopenharmony_ciadded: v0.11.13 17931cb0ef41Sopenharmony_cichanges: 17941cb0ef41Sopenharmony_ci - version: v18.16.0 17951cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/46978 17961cb0ef41Sopenharmony_ci description: The `dhparam` option can now be set to `'auto'` to 17971cb0ef41Sopenharmony_ci enable DHE with appropriate well-known parameters. 17981cb0ef41Sopenharmony_ci - version: v12.12.0 17991cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/28973 18001cb0ef41Sopenharmony_ci description: Added `privateKeyIdentifier` and `privateKeyEngine` options 18011cb0ef41Sopenharmony_ci to get private key from an OpenSSL engine. 18021cb0ef41Sopenharmony_ci - version: v12.11.0 18031cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/29598 18041cb0ef41Sopenharmony_ci description: Added `sigalgs` option to override supported signature 18051cb0ef41Sopenharmony_ci algorithms. 18061cb0ef41Sopenharmony_ci - version: v12.0.0 18071cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/26209 18081cb0ef41Sopenharmony_ci description: TLSv1.3 support added. 18091cb0ef41Sopenharmony_ci - version: v11.5.0 18101cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/24733 18111cb0ef41Sopenharmony_ci description: The `ca:` option now supports `BEGIN TRUSTED CERTIFICATE`. 18121cb0ef41Sopenharmony_ci - version: 18131cb0ef41Sopenharmony_ci - v11.4.0 18141cb0ef41Sopenharmony_ci - v10.16.0 18151cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/24405 18161cb0ef41Sopenharmony_ci description: The `minVersion` and `maxVersion` can be used to restrict 18171cb0ef41Sopenharmony_ci the allowed TLS protocol versions. 18181cb0ef41Sopenharmony_ci - version: v10.0.0 18191cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/19794 18201cb0ef41Sopenharmony_ci description: The `ecdhCurve` cannot be set to `false` anymore due to a 18211cb0ef41Sopenharmony_ci change in OpenSSL. 18221cb0ef41Sopenharmony_ci - version: v9.3.0 18231cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/14903 18241cb0ef41Sopenharmony_ci description: The `options` parameter can now include `clientCertEngine`. 18251cb0ef41Sopenharmony_ci - version: v9.0.0 18261cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/15206 18271cb0ef41Sopenharmony_ci description: The `ecdhCurve` option can now be multiple `':'` separated 18281cb0ef41Sopenharmony_ci curve names or `'auto'`. 18291cb0ef41Sopenharmony_ci - version: v7.3.0 18301cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/10294 18311cb0ef41Sopenharmony_ci description: If the `key` option is an array, individual entries do not 18321cb0ef41Sopenharmony_ci need a `passphrase` property anymore. `Array` entries can also 18331cb0ef41Sopenharmony_ci just be `string`s or `Buffer`s now. 18341cb0ef41Sopenharmony_ci - version: v5.2.0 18351cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/4099 18361cb0ef41Sopenharmony_ci description: The `ca` option can now be a single string containing multiple 18371cb0ef41Sopenharmony_ci CA certificates. 18381cb0ef41Sopenharmony_ci--> 18391cb0ef41Sopenharmony_ci 18401cb0ef41Sopenharmony_ci* `options` {Object} 18411cb0ef41Sopenharmony_ci * `ca` {string|string\[]|Buffer|Buffer\[]} Optionally override the trusted CA 18421cb0ef41Sopenharmony_ci certificates. Default is to trust the well-known CAs curated by Mozilla. 18431cb0ef41Sopenharmony_ci Mozilla's CAs are completely replaced when CAs are explicitly specified 18441cb0ef41Sopenharmony_ci using this option. The value can be a string or `Buffer`, or an `Array` of 18451cb0ef41Sopenharmony_ci strings and/or `Buffer`s. Any string or `Buffer` can contain multiple PEM 18461cb0ef41Sopenharmony_ci CAs concatenated together. The peer's certificate must be chainable to a CA 18471cb0ef41Sopenharmony_ci trusted by the server for the connection to be authenticated. When using 18481cb0ef41Sopenharmony_ci certificates that are not chainable to a well-known CA, the certificate's CA 18491cb0ef41Sopenharmony_ci must be explicitly specified as a trusted or the connection will fail to 18501cb0ef41Sopenharmony_ci authenticate. 18511cb0ef41Sopenharmony_ci If the peer uses a certificate that doesn't match or chain to one of the 18521cb0ef41Sopenharmony_ci default CAs, use the `ca` option to provide a CA certificate that the peer's 18531cb0ef41Sopenharmony_ci certificate can match or chain to. 18541cb0ef41Sopenharmony_ci For self-signed certificates, the certificate is its own CA, and must be 18551cb0ef41Sopenharmony_ci provided. 18561cb0ef41Sopenharmony_ci For PEM encoded certificates, supported types are "TRUSTED CERTIFICATE", 18571cb0ef41Sopenharmony_ci "X509 CERTIFICATE", and "CERTIFICATE". 18581cb0ef41Sopenharmony_ci See also [`tls.rootCertificates`][]. 18591cb0ef41Sopenharmony_ci * `cert` {string|string\[]|Buffer|Buffer\[]} Cert chains in PEM format. One 18601cb0ef41Sopenharmony_ci cert chain should be provided per private key. Each cert chain should 18611cb0ef41Sopenharmony_ci consist of the PEM formatted certificate for a provided private `key`, 18621cb0ef41Sopenharmony_ci followed by the PEM formatted intermediate certificates (if any), in order, 18631cb0ef41Sopenharmony_ci and not including the root CA (the root CA must be pre-known to the peer, 18641cb0ef41Sopenharmony_ci see `ca`). When providing multiple cert chains, they do not have to be in 18651cb0ef41Sopenharmony_ci the same order as their private keys in `key`. If the intermediate 18661cb0ef41Sopenharmony_ci certificates are not provided, the peer will not be able to validate the 18671cb0ef41Sopenharmony_ci certificate, and the handshake will fail. 18681cb0ef41Sopenharmony_ci * `sigalgs` {string} Colon-separated list of supported signature algorithms. 18691cb0ef41Sopenharmony_ci The list can contain digest algorithms (`SHA256`, `MD5` etc.), public key 18701cb0ef41Sopenharmony_ci algorithms (`RSA-PSS`, `ECDSA` etc.), combination of both (e.g 18711cb0ef41Sopenharmony_ci 'RSA+SHA384') or TLS v1.3 scheme names (e.g. `rsa_pss_pss_sha512`). 18721cb0ef41Sopenharmony_ci See [OpenSSL man pages](https://www.openssl.org/docs/man1.1.1/man3/SSL_CTX_set1_sigalgs_list.html) 18731cb0ef41Sopenharmony_ci for more info. 18741cb0ef41Sopenharmony_ci * `ciphers` {string} Cipher suite specification, replacing the default. For 18751cb0ef41Sopenharmony_ci more information, see [Modifying the default TLS cipher suite][]. Permitted 18761cb0ef41Sopenharmony_ci ciphers can be obtained via [`tls.getCiphers()`][]. Cipher names must be 18771cb0ef41Sopenharmony_ci uppercased in order for OpenSSL to accept them. 18781cb0ef41Sopenharmony_ci * `clientCertEngine` {string} Name of an OpenSSL engine which can provide the 18791cb0ef41Sopenharmony_ci client certificate. 18801cb0ef41Sopenharmony_ci * `crl` {string|string\[]|Buffer|Buffer\[]} PEM formatted CRLs (Certificate 18811cb0ef41Sopenharmony_ci Revocation Lists). 18821cb0ef41Sopenharmony_ci * `dhparam` {string|Buffer} `'auto'` or custom Diffie-Hellman parameters, 18831cb0ef41Sopenharmony_ci required for non-ECDHE [perfect forward secrecy][]. If omitted or invalid, 18841cb0ef41Sopenharmony_ci the parameters are silently discarded and DHE ciphers will not be available. 18851cb0ef41Sopenharmony_ci [ECDHE][]-based [perfect forward secrecy][] will still be available. 18861cb0ef41Sopenharmony_ci * `ecdhCurve` {string} A string describing a named curve or a colon separated 18871cb0ef41Sopenharmony_ci list of curve NIDs or names, for example `P-521:P-384:P-256`, to use for 18881cb0ef41Sopenharmony_ci ECDH key agreement. Set to `auto` to select the 18891cb0ef41Sopenharmony_ci curve automatically. Use [`crypto.getCurves()`][] to obtain a list of 18901cb0ef41Sopenharmony_ci available curve names. On recent releases, `openssl ecparam -list_curves` 18911cb0ef41Sopenharmony_ci will also display the name and description of each available elliptic curve. 18921cb0ef41Sopenharmony_ci **Default:** [`tls.DEFAULT_ECDH_CURVE`][]. 18931cb0ef41Sopenharmony_ci * `honorCipherOrder` {boolean} Attempt to use the server's cipher suite 18941cb0ef41Sopenharmony_ci preferences instead of the client's. When `true`, causes 18951cb0ef41Sopenharmony_ci `SSL_OP_CIPHER_SERVER_PREFERENCE` to be set in `secureOptions`, see 18961cb0ef41Sopenharmony_ci [OpenSSL Options][] for more information. 18971cb0ef41Sopenharmony_ci * `key` {string|string\[]|Buffer|Buffer\[]|Object\[]} Private keys in PEM 18981cb0ef41Sopenharmony_ci format. PEM allows the option of private keys being encrypted. Encrypted 18991cb0ef41Sopenharmony_ci keys will be decrypted with `options.passphrase`. Multiple keys using 19001cb0ef41Sopenharmony_ci different algorithms can be provided either as an array of unencrypted key 19011cb0ef41Sopenharmony_ci strings or buffers, or an array of objects in the form 19021cb0ef41Sopenharmony_ci `{pem: <string|buffer>[, passphrase: <string>]}`. The object form can only 19031cb0ef41Sopenharmony_ci occur in an array. `object.passphrase` is optional. Encrypted keys will be 19041cb0ef41Sopenharmony_ci decrypted with `object.passphrase` if provided, or `options.passphrase` if 19051cb0ef41Sopenharmony_ci it is not. 19061cb0ef41Sopenharmony_ci * `privateKeyEngine` {string} Name of an OpenSSL engine to get private key 19071cb0ef41Sopenharmony_ci from. Should be used together with `privateKeyIdentifier`. 19081cb0ef41Sopenharmony_ci * `privateKeyIdentifier` {string} Identifier of a private key managed by 19091cb0ef41Sopenharmony_ci an OpenSSL engine. Should be used together with `privateKeyEngine`. 19101cb0ef41Sopenharmony_ci Should not be set together with `key`, because both options define a 19111cb0ef41Sopenharmony_ci private key in different ways. 19121cb0ef41Sopenharmony_ci * `maxVersion` {string} Optionally set the maximum TLS version to allow. One 19131cb0ef41Sopenharmony_ci of `'TLSv1.3'`, `'TLSv1.2'`, `'TLSv1.1'`, or `'TLSv1'`. Cannot be specified 19141cb0ef41Sopenharmony_ci along with the `secureProtocol` option; use one or the other. 19151cb0ef41Sopenharmony_ci **Default:** [`tls.DEFAULT_MAX_VERSION`][]. 19161cb0ef41Sopenharmony_ci * `minVersion` {string} Optionally set the minimum TLS version to allow. One 19171cb0ef41Sopenharmony_ci of `'TLSv1.3'`, `'TLSv1.2'`, `'TLSv1.1'`, or `'TLSv1'`. Cannot be specified 19181cb0ef41Sopenharmony_ci along with the `secureProtocol` option; use one or the other. Avoid 19191cb0ef41Sopenharmony_ci setting to less than TLSv1.2, but it may be required for 19201cb0ef41Sopenharmony_ci interoperability. 19211cb0ef41Sopenharmony_ci **Default:** [`tls.DEFAULT_MIN_VERSION`][]. 19221cb0ef41Sopenharmony_ci * `passphrase` {string} Shared passphrase used for a single private key and/or 19231cb0ef41Sopenharmony_ci a PFX. 19241cb0ef41Sopenharmony_ci * `pfx` {string|string\[]|Buffer|Buffer\[]|Object\[]} PFX or PKCS12 encoded 19251cb0ef41Sopenharmony_ci private key and certificate chain. `pfx` is an alternative to providing 19261cb0ef41Sopenharmony_ci `key` and `cert` individually. PFX is usually encrypted, if it is, 19271cb0ef41Sopenharmony_ci `passphrase` will be used to decrypt it. Multiple PFX can be provided either 19281cb0ef41Sopenharmony_ci as an array of unencrypted PFX buffers, or an array of objects in the form 19291cb0ef41Sopenharmony_ci `{buf: <string|buffer>[, passphrase: <string>]}`. The object form can only 19301cb0ef41Sopenharmony_ci occur in an array. `object.passphrase` is optional. Encrypted PFX will be 19311cb0ef41Sopenharmony_ci decrypted with `object.passphrase` if provided, or `options.passphrase` if 19321cb0ef41Sopenharmony_ci it is not. 19331cb0ef41Sopenharmony_ci * `secureOptions` {number} Optionally affect the OpenSSL protocol behavior, 19341cb0ef41Sopenharmony_ci which is not usually necessary. This should be used carefully if at all! 19351cb0ef41Sopenharmony_ci Value is a numeric bitmask of the `SSL_OP_*` options from 19361cb0ef41Sopenharmony_ci [OpenSSL Options][]. 19371cb0ef41Sopenharmony_ci * `secureProtocol` {string} Legacy mechanism to select the TLS protocol 19381cb0ef41Sopenharmony_ci version to use, it does not support independent control of the minimum and 19391cb0ef41Sopenharmony_ci maximum version, and does not support limiting the protocol to TLSv1.3. Use 19401cb0ef41Sopenharmony_ci `minVersion` and `maxVersion` instead. The possible values are listed as 19411cb0ef41Sopenharmony_ci [SSL\_METHODS][SSL_METHODS], use the function names as strings. For example, 19421cb0ef41Sopenharmony_ci use `'TLSv1_1_method'` to force TLS version 1.1, or `'TLS_method'` to allow 19431cb0ef41Sopenharmony_ci any TLS protocol version up to TLSv1.3. It is not recommended to use TLS 19441cb0ef41Sopenharmony_ci versions less than 1.2, but it may be required for interoperability. 19451cb0ef41Sopenharmony_ci **Default:** none, see `minVersion`. 19461cb0ef41Sopenharmony_ci * `sessionIdContext` {string} Opaque identifier used by servers to ensure 19471cb0ef41Sopenharmony_ci session state is not shared between applications. Unused by clients. 19481cb0ef41Sopenharmony_ci * `ticketKeys`: {Buffer} 48-bytes of cryptographically strong pseudorandom 19491cb0ef41Sopenharmony_ci data. See [Session Resumption][] for more information. 19501cb0ef41Sopenharmony_ci * `sessionTimeout` {number} The number of seconds after which a TLS session 19511cb0ef41Sopenharmony_ci created by the server will no longer be resumable. See 19521cb0ef41Sopenharmony_ci [Session Resumption][] for more information. **Default:** `300`. 19531cb0ef41Sopenharmony_ci 19541cb0ef41Sopenharmony_ci[`tls.createServer()`][] sets the default value of the `honorCipherOrder` option 19551cb0ef41Sopenharmony_cito `true`, other APIs that create secure contexts leave it unset. 19561cb0ef41Sopenharmony_ci 19571cb0ef41Sopenharmony_ci[`tls.createServer()`][] uses a 128 bit truncated SHA1 hash value generated 19581cb0ef41Sopenharmony_cifrom `process.argv` as the default value of the `sessionIdContext` option, other 19591cb0ef41Sopenharmony_ciAPIs that create secure contexts have no default value. 19601cb0ef41Sopenharmony_ci 19611cb0ef41Sopenharmony_ciThe `tls.createSecureContext()` method creates a `SecureContext` object. It is 19621cb0ef41Sopenharmony_ciusable as an argument to several `tls` APIs, such as [`server.addContext()`][], 19631cb0ef41Sopenharmony_cibut has no public methods. The [`tls.Server`][] constructor and the 19641cb0ef41Sopenharmony_ci[`tls.createServer()`][] method do not support the `secureContext` option. 19651cb0ef41Sopenharmony_ci 19661cb0ef41Sopenharmony_ciA key is _required_ for ciphers that use certificates. Either `key` or 19671cb0ef41Sopenharmony_ci`pfx` can be used to provide it. 19681cb0ef41Sopenharmony_ci 19691cb0ef41Sopenharmony_ciIf the `ca` option is not given, then Node.js will default to using 19701cb0ef41Sopenharmony_ci[Mozilla's publicly trusted list of CAs][]. 19711cb0ef41Sopenharmony_ci 19721cb0ef41Sopenharmony_ciCustom DHE parameters are discouraged in favor of the new `dhparam: 'auto'` 19731cb0ef41Sopenharmony_cioption. When set to `'auto'`, well-known DHE parameters of sufficient strength 19741cb0ef41Sopenharmony_ciwill be selected automatically. Otherwise, if necessary, `openssl dhparam` can 19751cb0ef41Sopenharmony_cibe used to create custom parameters. The key length must be greater than or 19761cb0ef41Sopenharmony_ciequal to 1024 bits or else an error will be thrown. Although 1024 bits is 19771cb0ef41Sopenharmony_cipermissible, use 2048 bits or larger for stronger security. 19781cb0ef41Sopenharmony_ci 19791cb0ef41Sopenharmony_ci## `tls.createSecurePair([context][, isServer][, requestCert][, rejectUnauthorized][, options])` 19801cb0ef41Sopenharmony_ci 19811cb0ef41Sopenharmony_ci<!-- YAML 19821cb0ef41Sopenharmony_ciadded: v0.3.2 19831cb0ef41Sopenharmony_cideprecated: v0.11.3 19841cb0ef41Sopenharmony_cichanges: 19851cb0ef41Sopenharmony_ci - version: v5.0.0 19861cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/2564 19871cb0ef41Sopenharmony_ci description: ALPN options are supported now. 19881cb0ef41Sopenharmony_ci--> 19891cb0ef41Sopenharmony_ci 19901cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`tls.TLSSocket`][] instead. 19911cb0ef41Sopenharmony_ci 19921cb0ef41Sopenharmony_ci* `context` {Object} A secure context object as returned by 19931cb0ef41Sopenharmony_ci `tls.createSecureContext()` 19941cb0ef41Sopenharmony_ci* `isServer` {boolean} `true` to specify that this TLS connection should be 19951cb0ef41Sopenharmony_ci opened as a server. 19961cb0ef41Sopenharmony_ci* `requestCert` {boolean} `true` to specify whether a server should request a 19971cb0ef41Sopenharmony_ci certificate from a connecting client. Only applies when `isServer` is `true`. 19981cb0ef41Sopenharmony_ci* `rejectUnauthorized` {boolean} If not `false` a server automatically reject 19991cb0ef41Sopenharmony_ci clients with invalid certificates. Only applies when `isServer` is `true`. 20001cb0ef41Sopenharmony_ci* `options` 20011cb0ef41Sopenharmony_ci * `enableTrace`: See [`tls.createServer()`][] 20021cb0ef41Sopenharmony_ci * `secureContext`: A TLS context object from [`tls.createSecureContext()`][] 20031cb0ef41Sopenharmony_ci * `isServer`: If `true` the TLS socket will be instantiated in server-mode. 20041cb0ef41Sopenharmony_ci **Default:** `false`. 20051cb0ef41Sopenharmony_ci * `server` {net.Server} A [`net.Server`][] instance 20061cb0ef41Sopenharmony_ci * `requestCert`: See [`tls.createServer()`][] 20071cb0ef41Sopenharmony_ci * `rejectUnauthorized`: See [`tls.createServer()`][] 20081cb0ef41Sopenharmony_ci * `ALPNProtocols`: See [`tls.createServer()`][] 20091cb0ef41Sopenharmony_ci * `SNICallback`: See [`tls.createServer()`][] 20101cb0ef41Sopenharmony_ci * `session` {Buffer} A `Buffer` instance containing a TLS session. 20111cb0ef41Sopenharmony_ci * `requestOCSP` {boolean} If `true`, specifies that the OCSP status request 20121cb0ef41Sopenharmony_ci extension will be added to the client hello and an `'OCSPResponse'` event 20131cb0ef41Sopenharmony_ci will be emitted on the socket before establishing a secure communication. 20141cb0ef41Sopenharmony_ci 20151cb0ef41Sopenharmony_ciCreates a new secure pair object with two streams, one of which reads and writes 20161cb0ef41Sopenharmony_cithe encrypted data and the other of which reads and writes the cleartext data. 20171cb0ef41Sopenharmony_ciGenerally, the encrypted stream is piped to/from an incoming encrypted data 20181cb0ef41Sopenharmony_cistream and the cleartext one is used as a replacement for the initial encrypted 20191cb0ef41Sopenharmony_cistream. 20201cb0ef41Sopenharmony_ci 20211cb0ef41Sopenharmony_ci`tls.createSecurePair()` returns a `tls.SecurePair` object with `cleartext` and 20221cb0ef41Sopenharmony_ci`encrypted` stream properties. 20231cb0ef41Sopenharmony_ci 20241cb0ef41Sopenharmony_ciUsing `cleartext` has the same API as [`tls.TLSSocket`][]. 20251cb0ef41Sopenharmony_ci 20261cb0ef41Sopenharmony_ciThe `tls.createSecurePair()` method is now deprecated in favor of 20271cb0ef41Sopenharmony_ci`tls.TLSSocket()`. For example, the code: 20281cb0ef41Sopenharmony_ci 20291cb0ef41Sopenharmony_ci```js 20301cb0ef41Sopenharmony_cipair = tls.createSecurePair(/* ... */); 20311cb0ef41Sopenharmony_cipair.encrypted.pipe(socket); 20321cb0ef41Sopenharmony_cisocket.pipe(pair.encrypted); 20331cb0ef41Sopenharmony_ci``` 20341cb0ef41Sopenharmony_ci 20351cb0ef41Sopenharmony_cican be replaced by: 20361cb0ef41Sopenharmony_ci 20371cb0ef41Sopenharmony_ci```js 20381cb0ef41Sopenharmony_cisecureSocket = tls.TLSSocket(socket, options); 20391cb0ef41Sopenharmony_ci``` 20401cb0ef41Sopenharmony_ci 20411cb0ef41Sopenharmony_ciwhere `secureSocket` has the same API as `pair.cleartext`. 20421cb0ef41Sopenharmony_ci 20431cb0ef41Sopenharmony_ci## `tls.createServer([options][, secureConnectionListener])` 20441cb0ef41Sopenharmony_ci 20451cb0ef41Sopenharmony_ci<!-- YAML 20461cb0ef41Sopenharmony_ciadded: v0.3.2 20471cb0ef41Sopenharmony_cichanges: 20481cb0ef41Sopenharmony_ci - version: v18.19.0 20491cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/45190 20501cb0ef41Sopenharmony_ci description: The `options` parameter can now include `ALPNCallback`. 20511cb0ef41Sopenharmony_ci - version: v12.3.0 20521cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/27665 20531cb0ef41Sopenharmony_ci description: The `options` parameter now supports `net.createServer()` 20541cb0ef41Sopenharmony_ci options. 20551cb0ef41Sopenharmony_ci - version: v9.3.0 20561cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/14903 20571cb0ef41Sopenharmony_ci description: The `options` parameter can now include `clientCertEngine`. 20581cb0ef41Sopenharmony_ci - version: v8.0.0 20591cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/11984 20601cb0ef41Sopenharmony_ci description: The `ALPNProtocols` option can be a `TypedArray` or 20611cb0ef41Sopenharmony_ci `DataView` now. 20621cb0ef41Sopenharmony_ci - version: v5.0.0 20631cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/2564 20641cb0ef41Sopenharmony_ci description: ALPN options are supported now. 20651cb0ef41Sopenharmony_ci--> 20661cb0ef41Sopenharmony_ci 20671cb0ef41Sopenharmony_ci* `options` {Object} 20681cb0ef41Sopenharmony_ci * `ALPNProtocols`: {string\[]|Buffer\[]|TypedArray\[]|DataView\[]|Buffer| 20691cb0ef41Sopenharmony_ci TypedArray|DataView} 20701cb0ef41Sopenharmony_ci An array of strings, `Buffer`s, `TypedArray`s, or `DataView`s, or a single 20711cb0ef41Sopenharmony_ci `Buffer`, `TypedArray`, or `DataView` containing the supported ALPN 20721cb0ef41Sopenharmony_ci protocols. `Buffer`s should have the format `[len][name][len][name]...` 20731cb0ef41Sopenharmony_ci e.g. `0x05hello0x05world`, where the first byte is the length of the next 20741cb0ef41Sopenharmony_ci protocol name. Passing an array is usually much simpler, e.g. 20751cb0ef41Sopenharmony_ci `['hello', 'world']`. (Protocols should be ordered by their priority.) 20761cb0ef41Sopenharmony_ci * `ALPNCallback`: {Function} If set, this will be called when a 20771cb0ef41Sopenharmony_ci client opens a connection using the ALPN extension. One argument will 20781cb0ef41Sopenharmony_ci be passed to the callback: an object containing `servername` and 20791cb0ef41Sopenharmony_ci `protocols` fields, respectively containing the server name from 20801cb0ef41Sopenharmony_ci the SNI extension (if any) and an array of ALPN protocol name strings. The 20811cb0ef41Sopenharmony_ci callback must return either one of the strings listed in 20821cb0ef41Sopenharmony_ci `protocols`, which will be returned to the client as the selected 20831cb0ef41Sopenharmony_ci ALPN protocol, or `undefined`, to reject the connection with a fatal alert. 20841cb0ef41Sopenharmony_ci If a string is returned that does not match one of the client's ALPN 20851cb0ef41Sopenharmony_ci protocols, an error will be thrown. This option cannot be used with the 20861cb0ef41Sopenharmony_ci `ALPNProtocols` option, and setting both options will throw an error. 20871cb0ef41Sopenharmony_ci * `clientCertEngine` {string} Name of an OpenSSL engine which can provide the 20881cb0ef41Sopenharmony_ci client certificate. 20891cb0ef41Sopenharmony_ci * `enableTrace` {boolean} If `true`, [`tls.TLSSocket.enableTrace()`][] will be 20901cb0ef41Sopenharmony_ci called on new connections. Tracing can be enabled after the secure 20911cb0ef41Sopenharmony_ci connection is established, but this option must be used to trace the secure 20921cb0ef41Sopenharmony_ci connection setup. **Default:** `false`. 20931cb0ef41Sopenharmony_ci * `handshakeTimeout` {number} Abort the connection if the SSL/TLS handshake 20941cb0ef41Sopenharmony_ci does not finish in the specified number of milliseconds. 20951cb0ef41Sopenharmony_ci A `'tlsClientError'` is emitted on the `tls.Server` object whenever 20961cb0ef41Sopenharmony_ci a handshake times out. **Default:** `120000` (120 seconds). 20971cb0ef41Sopenharmony_ci * `rejectUnauthorized` {boolean} If not `false` the server will reject any 20981cb0ef41Sopenharmony_ci connection which is not authorized with the list of supplied CAs. This 20991cb0ef41Sopenharmony_ci option only has an effect if `requestCert` is `true`. **Default:** `true`. 21001cb0ef41Sopenharmony_ci * `requestCert` {boolean} If `true` the server will request a certificate from 21011cb0ef41Sopenharmony_ci clients that connect and attempt to verify that certificate. **Default:** 21021cb0ef41Sopenharmony_ci `false`. 21031cb0ef41Sopenharmony_ci * `sessionTimeout` {number} The number of seconds after which a TLS session 21041cb0ef41Sopenharmony_ci created by the server will no longer be resumable. See 21051cb0ef41Sopenharmony_ci [Session Resumption][] for more information. **Default:** `300`. 21061cb0ef41Sopenharmony_ci * `SNICallback(servername, callback)` {Function} A function that will be 21071cb0ef41Sopenharmony_ci called if the client supports SNI TLS extension. Two arguments will be 21081cb0ef41Sopenharmony_ci passed when called: `servername` and `callback`. `callback` is an 21091cb0ef41Sopenharmony_ci error-first callback that takes two optional arguments: `error` and `ctx`. 21101cb0ef41Sopenharmony_ci `ctx`, if provided, is a `SecureContext` instance. 21111cb0ef41Sopenharmony_ci [`tls.createSecureContext()`][] can be used to get a proper `SecureContext`. 21121cb0ef41Sopenharmony_ci If `callback` is called with a falsy `ctx` argument, the default secure 21131cb0ef41Sopenharmony_ci context of the server will be used. If `SNICallback` wasn't provided the 21141cb0ef41Sopenharmony_ci default callback with high-level API will be used (see below). 21151cb0ef41Sopenharmony_ci * `ticketKeys`: {Buffer} 48-bytes of cryptographically strong pseudorandom 21161cb0ef41Sopenharmony_ci data. See [Session Resumption][] for more information. 21171cb0ef41Sopenharmony_ci * `pskCallback` {Function} 21181cb0ef41Sopenharmony_ci 21191cb0ef41Sopenharmony_ci * socket: {tls.TLSSocket} the server [`tls.TLSSocket`][] instance for 21201cb0ef41Sopenharmony_ci this connection. 21211cb0ef41Sopenharmony_ci * identity: {string} identity parameter sent from the client. 21221cb0ef41Sopenharmony_ci * Returns: {Buffer|TypedArray|DataView} pre-shared key that must either be 21231cb0ef41Sopenharmony_ci a buffer or `null` to stop the negotiation process. Returned PSK must be 21241cb0ef41Sopenharmony_ci compatible with the selected cipher's digest. 21251cb0ef41Sopenharmony_ci 21261cb0ef41Sopenharmony_ci When negotiating TLS-PSK (pre-shared keys), this function is called 21271cb0ef41Sopenharmony_ci with the identity provided by the client. 21281cb0ef41Sopenharmony_ci If the return value is `null` the negotiation process will stop and an 21291cb0ef41Sopenharmony_ci "unknown\_psk\_identity" alert message will be sent to the other party. 21301cb0ef41Sopenharmony_ci If the server wishes to hide the fact that the PSK identity was not known, 21311cb0ef41Sopenharmony_ci the callback must provide some random data as `psk` to make the connection 21321cb0ef41Sopenharmony_ci fail with "decrypt\_error" before negotiation is finished. 21331cb0ef41Sopenharmony_ci PSK ciphers are disabled by default, and using TLS-PSK thus 21341cb0ef41Sopenharmony_ci requires explicitly specifying a cipher suite with the `ciphers` option. 21351cb0ef41Sopenharmony_ci More information can be found in the [RFC 4279][]. 21361cb0ef41Sopenharmony_ci * `pskIdentityHint` {string} optional hint to send to a client to help 21371cb0ef41Sopenharmony_ci with selecting the identity during TLS-PSK negotiation. Will be ignored 21381cb0ef41Sopenharmony_ci in TLS 1.3. Upon failing to set pskIdentityHint `'tlsClientError'` will be 21391cb0ef41Sopenharmony_ci emitted with `'ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED'` code. 21401cb0ef41Sopenharmony_ci * ...: Any [`tls.createSecureContext()`][] option can be provided. For 21411cb0ef41Sopenharmony_ci servers, the identity options (`pfx`, `key`/`cert`, or `pskCallback`) 21421cb0ef41Sopenharmony_ci are usually required. 21431cb0ef41Sopenharmony_ci * ...: Any [`net.createServer()`][] option can be provided. 21441cb0ef41Sopenharmony_ci* `secureConnectionListener` {Function} 21451cb0ef41Sopenharmony_ci* Returns: {tls.Server} 21461cb0ef41Sopenharmony_ci 21471cb0ef41Sopenharmony_ciCreates a new [`tls.Server`][]. The `secureConnectionListener`, if provided, is 21481cb0ef41Sopenharmony_ciautomatically set as a listener for the [`'secureConnection'`][] event. 21491cb0ef41Sopenharmony_ci 21501cb0ef41Sopenharmony_ciThe `ticketKeys` options is automatically shared between `node:cluster` module 21511cb0ef41Sopenharmony_ciworkers. 21521cb0ef41Sopenharmony_ci 21531cb0ef41Sopenharmony_ciThe following illustrates a simple echo server: 21541cb0ef41Sopenharmony_ci 21551cb0ef41Sopenharmony_ci```js 21561cb0ef41Sopenharmony_ciconst tls = require('node:tls'); 21571cb0ef41Sopenharmony_ciconst fs = require('node:fs'); 21581cb0ef41Sopenharmony_ci 21591cb0ef41Sopenharmony_ciconst options = { 21601cb0ef41Sopenharmony_ci key: fs.readFileSync('server-key.pem'), 21611cb0ef41Sopenharmony_ci cert: fs.readFileSync('server-cert.pem'), 21621cb0ef41Sopenharmony_ci 21631cb0ef41Sopenharmony_ci // This is necessary only if using client certificate authentication. 21641cb0ef41Sopenharmony_ci requestCert: true, 21651cb0ef41Sopenharmony_ci 21661cb0ef41Sopenharmony_ci // This is necessary only if the client uses a self-signed certificate. 21671cb0ef41Sopenharmony_ci ca: [ fs.readFileSync('client-cert.pem') ], 21681cb0ef41Sopenharmony_ci}; 21691cb0ef41Sopenharmony_ci 21701cb0ef41Sopenharmony_ciconst server = tls.createServer(options, (socket) => { 21711cb0ef41Sopenharmony_ci console.log('server connected', 21721cb0ef41Sopenharmony_ci socket.authorized ? 'authorized' : 'unauthorized'); 21731cb0ef41Sopenharmony_ci socket.write('welcome!\n'); 21741cb0ef41Sopenharmony_ci socket.setEncoding('utf8'); 21751cb0ef41Sopenharmony_ci socket.pipe(socket); 21761cb0ef41Sopenharmony_ci}); 21771cb0ef41Sopenharmony_ciserver.listen(8000, () => { 21781cb0ef41Sopenharmony_ci console.log('server bound'); 21791cb0ef41Sopenharmony_ci}); 21801cb0ef41Sopenharmony_ci``` 21811cb0ef41Sopenharmony_ci 21821cb0ef41Sopenharmony_ciThe server can be tested by connecting to it using the example client from 21831cb0ef41Sopenharmony_ci[`tls.connect()`][]. 21841cb0ef41Sopenharmony_ci 21851cb0ef41Sopenharmony_ci## `tls.getCiphers()` 21861cb0ef41Sopenharmony_ci 21871cb0ef41Sopenharmony_ci<!-- YAML 21881cb0ef41Sopenharmony_ciadded: v0.10.2 21891cb0ef41Sopenharmony_ci--> 21901cb0ef41Sopenharmony_ci 21911cb0ef41Sopenharmony_ci* Returns: {string\[]} 21921cb0ef41Sopenharmony_ci 21931cb0ef41Sopenharmony_ciReturns an array with the names of the supported TLS ciphers. The names are 21941cb0ef41Sopenharmony_cilower-case for historical reasons, but must be uppercased to be used in 21951cb0ef41Sopenharmony_cithe `ciphers` option of [`tls.createSecureContext()`][]. 21961cb0ef41Sopenharmony_ci 21971cb0ef41Sopenharmony_ciNot all supported ciphers are enabled by default. See 21981cb0ef41Sopenharmony_ci[Modifying the default TLS cipher suite][]. 21991cb0ef41Sopenharmony_ci 22001cb0ef41Sopenharmony_ciCipher names that start with `'tls_'` are for TLSv1.3, all the others are for 22011cb0ef41Sopenharmony_ciTLSv1.2 and below. 22021cb0ef41Sopenharmony_ci 22031cb0ef41Sopenharmony_ci```js 22041cb0ef41Sopenharmony_ciconsole.log(tls.getCiphers()); // ['aes128-gcm-sha256', 'aes128-sha', ...] 22051cb0ef41Sopenharmony_ci``` 22061cb0ef41Sopenharmony_ci 22071cb0ef41Sopenharmony_ci## `tls.rootCertificates` 22081cb0ef41Sopenharmony_ci 22091cb0ef41Sopenharmony_ci<!-- YAML 22101cb0ef41Sopenharmony_ciadded: v12.3.0 22111cb0ef41Sopenharmony_ci--> 22121cb0ef41Sopenharmony_ci 22131cb0ef41Sopenharmony_ci* {string\[]} 22141cb0ef41Sopenharmony_ci 22151cb0ef41Sopenharmony_ciAn immutable array of strings representing the root certificates (in PEM format) 22161cb0ef41Sopenharmony_cifrom the bundled Mozilla CA store as supplied by the current Node.js version. 22171cb0ef41Sopenharmony_ci 22181cb0ef41Sopenharmony_ciThe bundled CA store, as supplied by Node.js, is a snapshot of Mozilla CA store 22191cb0ef41Sopenharmony_cithat is fixed at release time. It is identical on all supported platforms. 22201cb0ef41Sopenharmony_ci 22211cb0ef41Sopenharmony_ci## `tls.DEFAULT_ECDH_CURVE` 22221cb0ef41Sopenharmony_ci 22231cb0ef41Sopenharmony_ci<!-- YAML 22241cb0ef41Sopenharmony_ciadded: v0.11.13 22251cb0ef41Sopenharmony_cichanges: 22261cb0ef41Sopenharmony_ci - version: v10.0.0 22271cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/16853 22281cb0ef41Sopenharmony_ci description: Default value changed to `'auto'`. 22291cb0ef41Sopenharmony_ci--> 22301cb0ef41Sopenharmony_ci 22311cb0ef41Sopenharmony_ciThe default curve name to use for ECDH key agreement in a tls server. The 22321cb0ef41Sopenharmony_cidefault value is `'auto'`. See [`tls.createSecureContext()`][] for further 22331cb0ef41Sopenharmony_ciinformation. 22341cb0ef41Sopenharmony_ci 22351cb0ef41Sopenharmony_ci## `tls.DEFAULT_MAX_VERSION` 22361cb0ef41Sopenharmony_ci 22371cb0ef41Sopenharmony_ci<!-- YAML 22381cb0ef41Sopenharmony_ciadded: v11.4.0 22391cb0ef41Sopenharmony_ci--> 22401cb0ef41Sopenharmony_ci 22411cb0ef41Sopenharmony_ci* {string} The default value of the `maxVersion` option of 22421cb0ef41Sopenharmony_ci [`tls.createSecureContext()`][]. It can be assigned any of the supported TLS 22431cb0ef41Sopenharmony_ci protocol versions, `'TLSv1.3'`, `'TLSv1.2'`, `'TLSv1.1'`, or `'TLSv1'`. 22441cb0ef41Sopenharmony_ci **Default:** `'TLSv1.3'`, unless changed using CLI options. Using 22451cb0ef41Sopenharmony_ci `--tls-max-v1.2` sets the default to `'TLSv1.2'`. Using `--tls-max-v1.3` sets 22461cb0ef41Sopenharmony_ci the default to `'TLSv1.3'`. If multiple of the options are provided, the 22471cb0ef41Sopenharmony_ci highest maximum is used. 22481cb0ef41Sopenharmony_ci 22491cb0ef41Sopenharmony_ci## `tls.DEFAULT_MIN_VERSION` 22501cb0ef41Sopenharmony_ci 22511cb0ef41Sopenharmony_ci<!-- YAML 22521cb0ef41Sopenharmony_ciadded: v11.4.0 22531cb0ef41Sopenharmony_ci--> 22541cb0ef41Sopenharmony_ci 22551cb0ef41Sopenharmony_ci* {string} The default value of the `minVersion` option of 22561cb0ef41Sopenharmony_ci [`tls.createSecureContext()`][]. It can be assigned any of the supported TLS 22571cb0ef41Sopenharmony_ci protocol versions, `'TLSv1.3'`, `'TLSv1.2'`, `'TLSv1.1'`, or `'TLSv1'`. 22581cb0ef41Sopenharmony_ci **Default:** `'TLSv1.2'`, unless changed using CLI options. Using 22591cb0ef41Sopenharmony_ci `--tls-min-v1.0` sets the default to `'TLSv1'`. Using `--tls-min-v1.1` sets 22601cb0ef41Sopenharmony_ci the default to `'TLSv1.1'`. Using `--tls-min-v1.3` sets the default to 22611cb0ef41Sopenharmony_ci `'TLSv1.3'`. If multiple of the options are provided, the lowest minimum is 22621cb0ef41Sopenharmony_ci used. 22631cb0ef41Sopenharmony_ci 22641cb0ef41Sopenharmony_ci## `tls.DEFAULT_CIPHERS` 22651cb0ef41Sopenharmony_ci 22661cb0ef41Sopenharmony_ci<!-- YAML 22671cb0ef41Sopenharmony_ciadded: v18.16.0 22681cb0ef41Sopenharmony_ci--> 22691cb0ef41Sopenharmony_ci 22701cb0ef41Sopenharmony_ci* {string} The default value of the `ciphers` option of 22711cb0ef41Sopenharmony_ci [`tls.createSecureContext()`][]. It can be assigned any of the supported 22721cb0ef41Sopenharmony_ci OpenSSL ciphers. Defaults to the content of 22731cb0ef41Sopenharmony_ci `crypto.constants.defaultCoreCipherList`, unless changed using CLI options 22741cb0ef41Sopenharmony_ci using `--tls-default-ciphers`. 22751cb0ef41Sopenharmony_ci 22761cb0ef41Sopenharmony_ci[CVE-2021-44531]: https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-44531 22771cb0ef41Sopenharmony_ci[Chrome's 'modern cryptography' setting]: https://www.chromium.org/Home/chromium-security/education/tls#TOC-Cipher-Suites 22781cb0ef41Sopenharmony_ci[DHE]: https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange 22791cb0ef41Sopenharmony_ci[ECDHE]: https://en.wikipedia.org/wiki/Elliptic_curve_Diffie%E2%80%93Hellman 22801cb0ef41Sopenharmony_ci[Modifying the default TLS cipher suite]: #modifying-the-default-tls-cipher-suite 22811cb0ef41Sopenharmony_ci[Mozilla's publicly trusted list of CAs]: https://hg.mozilla.org/mozilla-central/raw-file/tip/security/nss/lib/ckfw/builtins/certdata.txt 22821cb0ef41Sopenharmony_ci[OCSP request]: https://en.wikipedia.org/wiki/OCSP_stapling 22831cb0ef41Sopenharmony_ci[OpenSSL Options]: crypto.md#openssl-options 22841cb0ef41Sopenharmony_ci[RFC 2246]: https://www.ietf.org/rfc/rfc2246.txt 22851cb0ef41Sopenharmony_ci[RFC 4086]: https://tools.ietf.org/html/rfc4086 22861cb0ef41Sopenharmony_ci[RFC 4279]: https://tools.ietf.org/html/rfc4279 22871cb0ef41Sopenharmony_ci[RFC 5077]: https://tools.ietf.org/html/rfc5077 22881cb0ef41Sopenharmony_ci[RFC 5929]: https://tools.ietf.org/html/rfc5929 22891cb0ef41Sopenharmony_ci[SSL_METHODS]: https://www.openssl.org/docs/man1.1.1/man7/ssl.html#Dealing-with-Protocol-Methods 22901cb0ef41Sopenharmony_ci[Session Resumption]: #session-resumption 22911cb0ef41Sopenharmony_ci[Stream]: stream.md#stream 22921cb0ef41Sopenharmony_ci[TLS recommendations]: https://wiki.mozilla.org/Security/Server_Side_TLS 22931cb0ef41Sopenharmony_ci[`'newSession'`]: #event-newsession 22941cb0ef41Sopenharmony_ci[`'resumeSession'`]: #event-resumesession 22951cb0ef41Sopenharmony_ci[`'secureConnect'`]: #event-secureconnect 22961cb0ef41Sopenharmony_ci[`'secureConnection'`]: #event-secureconnection 22971cb0ef41Sopenharmony_ci[`'session'`]: #event-session 22981cb0ef41Sopenharmony_ci[`--tls-cipher-list`]: cli.md#--tls-cipher-listlist 22991cb0ef41Sopenharmony_ci[`Duplex`]: stream.md#class-streamduplex 23001cb0ef41Sopenharmony_ci[`NODE_OPTIONS`]: cli.md#node_optionsoptions 23011cb0ef41Sopenharmony_ci[`SSL_export_keying_material`]: https://www.openssl.org/docs/man1.1.1/man3/SSL_export_keying_material.html 23021cb0ef41Sopenharmony_ci[`SSL_get_version`]: https://www.openssl.org/docs/man1.1.1/man3/SSL_get_version.html 23031cb0ef41Sopenharmony_ci[`crypto.getCurves()`]: crypto.md#cryptogetcurves 23041cb0ef41Sopenharmony_ci[`import()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import 23051cb0ef41Sopenharmony_ci[`net.Server.address()`]: net.md#serveraddress 23061cb0ef41Sopenharmony_ci[`net.Server`]: net.md#class-netserver 23071cb0ef41Sopenharmony_ci[`net.Socket`]: net.md#class-netsocket 23081cb0ef41Sopenharmony_ci[`net.createServer()`]: net.md#netcreateserveroptions-connectionlistener 23091cb0ef41Sopenharmony_ci[`server.addContext()`]: #serveraddcontexthostname-context 23101cb0ef41Sopenharmony_ci[`server.getTicketKeys()`]: #servergetticketkeys 23111cb0ef41Sopenharmony_ci[`server.listen()`]: net.md#serverlisten 23121cb0ef41Sopenharmony_ci[`server.setTicketKeys()`]: #serversetticketkeyskeys 23131cb0ef41Sopenharmony_ci[`socket.connect()`]: net.md#socketconnectoptions-connectlistener 23141cb0ef41Sopenharmony_ci[`tls.DEFAULT_ECDH_CURVE`]: #tlsdefault_ecdh_curve 23151cb0ef41Sopenharmony_ci[`tls.DEFAULT_MAX_VERSION`]: #tlsdefault_max_version 23161cb0ef41Sopenharmony_ci[`tls.DEFAULT_MIN_VERSION`]: #tlsdefault_min_version 23171cb0ef41Sopenharmony_ci[`tls.Server`]: #class-tlsserver 23181cb0ef41Sopenharmony_ci[`tls.TLSSocket.enableTrace()`]: #tlssocketenabletrace 23191cb0ef41Sopenharmony_ci[`tls.TLSSocket.getPeerCertificate()`]: #tlssocketgetpeercertificatedetailed 23201cb0ef41Sopenharmony_ci[`tls.TLSSocket.getProtocol()`]: #tlssocketgetprotocol 23211cb0ef41Sopenharmony_ci[`tls.TLSSocket.getSession()`]: #tlssocketgetsession 23221cb0ef41Sopenharmony_ci[`tls.TLSSocket.getTLSTicket()`]: #tlssocketgettlsticket 23231cb0ef41Sopenharmony_ci[`tls.TLSSocket`]: #class-tlstlssocket 23241cb0ef41Sopenharmony_ci[`tls.connect()`]: #tlsconnectoptions-callback 23251cb0ef41Sopenharmony_ci[`tls.createSecureContext()`]: #tlscreatesecurecontextoptions 23261cb0ef41Sopenharmony_ci[`tls.createSecurePair()`]: #tlscreatesecurepaircontext-isserver-requestcert-rejectunauthorized-options 23271cb0ef41Sopenharmony_ci[`tls.createServer()`]: #tlscreateserveroptions-secureconnectionlistener 23281cb0ef41Sopenharmony_ci[`tls.getCiphers()`]: #tlsgetciphers 23291cb0ef41Sopenharmony_ci[`tls.rootCertificates`]: #tlsrootcertificates 23301cb0ef41Sopenharmony_ci[`x509.checkHost()`]: crypto.md#x509checkhostname-options 23311cb0ef41Sopenharmony_ci[asn1.js]: https://www.npmjs.com/package/asn1.js 23321cb0ef41Sopenharmony_ci[certificate object]: #certificate-object 23331cb0ef41Sopenharmony_ci[cipher list format]: https://www.openssl.org/docs/man1.1.1/man1/ciphers.html#CIPHER-LIST-FORMAT 23341cb0ef41Sopenharmony_ci[forward secrecy]: https://en.wikipedia.org/wiki/Perfect_forward_secrecy 23351cb0ef41Sopenharmony_ci[perfect forward secrecy]: #perfect-forward-secrecy 2336