Skip to content

Commit 600c37c

Browse files
sam-githubtargos
authored andcommitted
tls: cli option to enable TLS key logging to file
Debugging HTTPS or TLS connections from a Node.js app with (for example) Wireshark is unreasonably difficult without the ability to get the TLS key log. In theory, the application can be modified to use the `'keylog'` event directly, but for complex apps, or apps that define there own HTTPS Agent (like npm), this is unreasonably difficult. Use of the option triggers a warning to be emitted so the user is clearly notified of what is happening and its effect. PR-URL: #30055 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent dc521b0 commit 600c37c

6 files changed

Lines changed: 99 additions & 0 deletions

File tree

‎doc/api/cli.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,15 @@ added: v4.0.0
698698
Specify an alternative default TLS cipher list. Requires Node.js to be built
699699
with crypto support (default).
700700

701+
### `--tls-keylog=file`
702+
<!-- YAML
703+
added: REPLACEME
704+
-->
705+
706+
Log TLS key material to a file. The key material is in NSS `SSLKEYLOGFILE`
707+
format and can be used by software (such as Wireshark) to decrypt the TLS
708+
traffic.
709+
701710
### `--tls-max-v1.2`
702711
<!-- YAML
703712
added: v12.0.0
@@ -1108,6 +1117,7 @@ Node.js options that are allowed are:
11081117
*`--throw-deprecation`
11091118
*`--title`
11101119
*`--tls-cipher-list`
1120+
*`--tls-keylog`
11111121
*`--tls-max-v1.2`
11121122
*`--tls-max-v1.3`
11131123
*`--tls-min-v1.0`

‎doc/node.1‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,11 @@ Specify process.title on startup.
309309
Specify an alternative default TLS cipher list.
310310
Requires Node.js to be built with crypto support. (Default)
311311
.
312+
.ItFl-tls-keylogNs=NsArfile
313+
Log TLS key material to a file. The key material is in NSS SSLKEYLOGFILE
314+
format and can be used by software (such as Wireshark) to decrypt the TLS
315+
traffic.
316+
.
312317
.ItFl-tls-max-v1.2
313318
Set default maxVersion to 'TLSv1.2'. Use to disable support for TLSv1.3.
314319
.

‎lib/_tls_wrap.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ const {
6060
const{ getOptionValue }=require('internal/options');
6161
const{ validateString }=require('internal/validators');
6262
consttraceTls=getOptionValue('--trace-tls');
63+
consttlsKeylog=getOptionValue('--tls-keylog');
64+
const{ appendFile }=require('fs');
6365
constkConnectOptions=Symbol('connect-options');
6466
constkDisableRenegotiation=Symbol('disable-renegotiation');
6567
constkErrorEmitted=Symbol('error-emitted');
@@ -560,6 +562,8 @@ TLSSocket.prototype._destroySSL = function _destroySSL() {
560562
};
561563

562564
// Constructor guts, arbitrarily factored out.
565+
letwarnOnTlsKeylog=true;
566+
letwarnOnTlsKeylogError=true;
563567
TLSSocket.prototype._init=function(socket,wrap){
564568
constoptions=this._tlsOptions;
565569
constssl=this._handle;
@@ -643,6 +647,24 @@ TLSSocket.prototype._init = function(socket, wrap) {
643647
}
644648
}
645649

650+
if(tlsKeylog){
651+
if(warnOnTlsKeylog){
652+
warnOnTlsKeylog=false;
653+
process.emitWarning('Using --tls-keylog makes TLS connections insecure '+
654+
'by writing secret key material to file '+tlsKeylog);
655+
ssl.enableKeylogCallback();
656+
this.on('keylog',(line)=>{
657+
appendFile(tlsKeylog,line,{mode: 0o600},(err)=>{
658+
if(err&&warnOnTlsKeylogError){
659+
warnOnTlsKeylogError=false;
660+
process.emitWarning('Failed to write TLS keylog (this warning '+
661+
'will not be repeated): '+err);
662+
}
663+
});
664+
});
665+
}
666+
}
667+
646668
ssl.onerror=onerror;
647669

648670
// If custom SNICallback was given, or if

‎src/node_options.cc‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
567567

568568
AddOption("--napi-modules", "", NoOp{}, kAllowedInEnvironment);
569569

570+
AddOption("--tls-keylog",
571+
"log TLS decryption keys to named file for traffic analysis",
572+
&EnvironmentOptions::tls_keylog, kAllowedInEnvironment);
573+
570574
AddOption("--tls-min-v1.0",
571575
"set default TLS minimum to TLSv1.0 (default: TLSv1.2)",
572576
&EnvironmentOptions::tls_min_v1_0,

‎src/node_options.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ class EnvironmentOptions : public Options {
164164
bool tls_min_v1_3 = false;
165165
bool tls_max_v1_2 = false;
166166
bool tls_max_v1_3 = false;
167+
std::string tls_keylog;
167168

168169
std::vector<std::string> preload_modules;
169170

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
if(!common.hasCrypto)common.skip('missing crypto');
4+
constfixtures=require('../common/fixtures');
5+
6+
// Test --tls-keylog CLI flag.
7+
8+
constassert=require('assert');
9+
constpath=require('path');
10+
constfs=require('fs');
11+
const{ fork }=require('child_process');
12+
13+
if(process.argv[2]==='test')
14+
returntest();
15+
16+
consttmpdir=require('../common/tmpdir');
17+
tmpdir.refresh();
18+
constfile=path.resolve(tmpdir.path,'keylog.log');
19+
20+
constchild=fork(__filename,['test'],{
21+
execArgv: ['--tls-keylog='+file]
22+
});
23+
24+
child.on('close',common.mustCall((code,signal)=>{
25+
assert.strictEqual(code,0);
26+
assert.strictEqual(signal,null);
27+
constlog=fs.readFileSync(file,'utf8');
28+
assert(/SECRET/.test(log));
29+
}));
30+
31+
functiontest(){
32+
const{
33+
connect, keys
34+
}=require(fixtures.path('tls-connect'));
35+
36+
connect({
37+
client: {
38+
checkServerIdentity: (servername,cert)=>{},
39+
ca: `${keys.agent1.cert}\n${keys.agent6.ca}`,
40+
},
41+
server: {
42+
cert: keys.agent6.cert,
43+
key: keys.agent6.key
44+
},
45+
},common.mustCall((err,pair,cleanup)=>{
46+
if(pair.server.err){
47+
console.trace('server',pair.server.err);
48+
}
49+
if(pair.client.err){
50+
console.trace('client',pair.client.err);
51+
}
52+
assert.ifError(pair.server.err);
53+
assert.ifError(pair.client.err);
54+
55+
returncleanup();
56+
}));
57+
}

0 commit comments

Comments
 (0)