Skip to content

Commit ad3bbae

Browse files
committed
TLS: Include subject name and SHA-256 fingerprint in handshake error log lines where possible
Signed-off-by: Neil Twigg <neil@nats.io>
1 parent 1da9471 commit ad3bbae

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

server/client.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ package server
1515

1616
import (
1717
"bytes"
18+
"crypto/sha256"
1819
"crypto/tls"
1920
"crypto/x509"
21+
"encoding/hex"
2022
"encoding/json"
2123
"errors"
2224
"fmt"
@@ -6066,10 +6068,22 @@ func (c *client) doTLSHandshake(typ string, solicit bool, url *url.URL, tlsConfi
60666068
}
60676069

60686070
if err != nil {
6071+
var detail string
6072+
var subjs []string
6073+
if ve, ok := err.(*tls.CertificateVerificationError); ok {
6074+
for _, cert := range ve.UnverifiedCertificates {
6075+
fp := sha256.Sum256(cert.Raw)
6076+
fph := hex.EncodeToString(fp[:])
6077+
subjs = append(subjs, fmt.Sprintf("%s SHA-256: %s", cert.Subject.String(), fph))
6078+
}
6079+
}
6080+
if len(subjs) > 0 {
6081+
detail = fmt.Sprintf(" (%s)", strings.Join(subjs, "; "))
6082+
}
60696083
if kind == CLIENT {
6070-
c.Errorf("TLS handshake error: %v", err)
6084+
c.Errorf("TLS handshake error: %v%s", err, detail)
60716085
} else {
6072-
c.Errorf("TLS %s handshake error: %v", typ, err)
6086+
c.Errorf("TLS %s handshake error: %v%s", typ, err, detail)
60736087
}
60746088
c.closeConnection(TLSHandshakeError)
60756089

0 commit comments

Comments
 (0)