diff --git a/doc/api/tls.md b/doc/api/tls.md index f98bb297672..34f8c3b99e9 100644 --- a/doc/api/tls.md +++ b/doc/api/tls.md @@ -468,35 +468,64 @@ to set the security level to 0 while using the default OpenSSL cipher list, you ```mjs import { createServer, connect } from 'node:tls'; -const port = 443; +import { readFileSync } from 'node:fs'; +const port = 8000; -createServer({ ciphers: 'DEFAULT@SECLEVEL=0', minVersion: 'TLSv1' }, function(socket) { +createServer({ + key: readFileSync('server-key.pem'), + cert: readFileSync('server-cert.pem'), + ciphers: 'DEFAULT@SECLEVEL=0', + minVersion: 'TLSv1', +}, function(socket) { console.log('Client connected with protocol:', socket.getProtocol()); socket.end(); this.close(); }) .listen(port, () => { - connect(port, { ciphers: 'DEFAULT@SECLEVEL=0', maxVersion: 'TLSv1' }); + connect(port, { + ciphers: 'DEFAULT@SECLEVEL=0', + minVersion: 'TLSv1', + maxVersion: 'TLSv1', + ca: [ readFileSync('server-cert.pem') ], + }); }); ``` ```cjs const { createServer, connect } = require('node:tls'); -const port = 443; +const { readFileSync } = require('node:fs'); +const port = 8000; -createServer({ ciphers: 'DEFAULT@SECLEVEL=0', minVersion: 'TLSv1' }, function(socket) { +createServer({ + key: readFileSync('server-key.pem'), + cert: readFileSync('server-cert.pem'), + ciphers: 'DEFAULT@SECLEVEL=0', + minVersion: 'TLSv1', +}, function(socket) { console.log('Client connected with protocol:', socket.getProtocol()); socket.end(); this.close(); }) .listen(port, () => { - connect(port, { ciphers: 'DEFAULT@SECLEVEL=0', maxVersion: 'TLSv1' }); + connect(port, { + ciphers: 'DEFAULT@SECLEVEL=0', + minVersion: 'TLSv1', + maxVersion: 'TLSv1', + ca: [ readFileSync('server-cert.pem') ], + }); }); ``` This approach sets the security level to 0, allowing the use of legacy features while still leveraging the default OpenSSL ciphers. +To generate the certificate and key for this example, run: + +```bash +openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \ + -keyout server-key.pem -out server-cert.pem +``` + ### Using [`--tls-cipher-list`][] You can also set the security level and ciphers from the command line using the