Skip to content

Commit

Permalink
Add suport for TLS key logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jedisct1 committed May 24, 2023
1 parent 9f86ffd commit 0c26d16
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions dnscrypt-proxy/config.go
Expand Up @@ -92,6 +92,7 @@ type Config struct {
LogMaxBackups int `toml:"log_files_max_backups"`
TLSDisableSessionTickets bool `toml:"tls_disable_session_tickets"`
TLSCipherSuite []uint16 `toml:"tls_cipher_suite"`
TLSKeyLogFile string `toml:"tls_key_log_file"`
NetprobeAddress string `toml:"netprobe_address"`
NetprobeTimeout int `toml:"netprobe_timeout"`
OfflineMode bool `toml:"offline_mode"`
Expand Down Expand Up @@ -143,6 +144,7 @@ func newConfig() Config {
LogMaxBackups: 1,
TLSDisableSessionTickets: false,
TLSCipherSuite: nil,
TLSKeyLogFile: "",
NetprobeTimeout: 60,
OfflineMode: false,
RefusedCodeInResponses: false,
Expand Down Expand Up @@ -628,6 +630,16 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
proxy.skipAnonIncompatibleResolvers = config.AnonymizedDNS.SkipIncompatible
proxy.anonDirectCertFallback = config.AnonymizedDNS.DirectCertFallback

if len(config.TLSKeyLogFile) > 0 {
f, err := os.OpenFile(config.TLSKeyLogFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
dlog.Fatalf("Unable to create key log file [%s]: [%s]", config.TLSKeyLogFile, err)
}
dlog.Warnf("TLS key log file [%s] enabled", config.TLSKeyLogFile)
proxy.xTransport.keyLogWriter = f
proxy.xTransport.rebuildTransport()
}

if config.DoHClientX509AuthLegacy.Creds != nil {
return errors.New("[tls_client_auth] has been renamed to [doh_client_x509_auth] - Update your config file")
}
Expand Down
8 changes: 8 additions & 0 deletions dnscrypt-proxy/example-dnscrypt-proxy.toml
Expand Up @@ -223,6 +223,14 @@ cert_refresh_delay = 240
# tls_cipher_suite = [52392, 49199]


## Log TLS key material to a file, for debugging purposes only.
## This file will contain the TLS master key, which can be used to decrypt
## all TLS traffic to/from DoH servers.
## Never ever enable except for debugging purposes with a tool such as mitmproxy.

# tls_key_log_file = '/tmp/keylog.txt'


## Bootstrap resolvers
##
## These are normal, non-encrypted DNS resolvers, that will be only used
Expand Down
6 changes: 6 additions & 0 deletions dnscrypt-proxy/xtransport.go
Expand Up @@ -75,6 +75,7 @@ type XTransport struct {
proxyDialer *netproxy.Dialer
httpProxyFunction func(*http.Request) (*url.URL, error)
tlsClientCreds DOHClientCreds
keyLogWriter io.Writer
}

func NewXTransport() *XTransport {
Expand All @@ -93,6 +94,7 @@ func NewXTransport() *XTransport {
useIPv6: false,
tlsDisableSessionTickets: false,
tlsCipherSuite: nil,
keyLogWriter: nil,
}
return &xTransport
}
Expand Down Expand Up @@ -187,6 +189,10 @@ func (xTransport *XTransport) rebuildTransport() {
tlsClientConfig := tls.Config{}
certPool, certPoolErr := x509.SystemCertPool()

if xTransport.keyLogWriter != nil {
tlsClientConfig.KeyLogWriter = xTransport.keyLogWriter
}

if clientCreds.rootCA != "" {
if certPool == nil {
dlog.Fatalf("Additional CAs not supported on this platform: %v", certPoolErr)
Expand Down

0 comments on commit 0c26d16

Please sign in to comment.