Skip to content

Commit 1c8901e

Browse files
Copilotshueybubbles
andcommitted
Improve comments and optimize intermediate cert handling
Co-authored-by: shueybubbles <[email protected]>
1 parent edfd563 commit 1c8901e

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

msdsn/conn_str_go115.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ func setupTLSCommonName(config *tls.Config, pem []byte) error {
4343

4444
// Build intermediates pool from the peer certificates (excluding the first one which is the server cert)
4545
intermediates := x509.NewCertPool()
46-
for i := 1; i < len(rawCerts); i++ {
47-
intermediateCert, err := x509.ParseCertificate(rawCerts[i])
48-
if err != nil {
49-
return fmt.Errorf("failed to parse intermediate certificate: %w", err)
46+
if len(rawCerts) > 1 {
47+
for i := 1; i < len(rawCerts); i++ {
48+
intermediateCert, err := x509.ParseCertificate(rawCerts[i])
49+
if err != nil {
50+
return fmt.Errorf("failed to parse intermediate certificate: %w", err)
51+
}
52+
intermediates.AddCert(intermediateCert)
5053
}
51-
intermediates.AddCert(intermediateCert)
5254
}
5355

5456
// Verify the certificate chain against the provided root CA
@@ -64,9 +66,10 @@ func setupTLSCommonName(config *tls.Config, pem []byte) error {
6466

6567
// setupTLSCertificateOnly validates the certificate chain without checking the hostname
6668
func setupTLSCertificateOnly(config *tls.Config, pem []byte) error {
67-
// Skip hostname validation by setting ServerName to empty string
68-
// The certificate chain will still be verified against RootCAs (set later in SetupTLS)
69-
// This is the secure way to skip hostname validation without using InsecureSkipVerify
69+
// Skip hostname validation by setting ServerName to empty string.
70+
// When ServerName is empty, Go's TLS implementation will skip hostname verification
71+
// but still verify the certificate chain against the RootCAs (configured in SetupTLS after this function returns).
72+
// This is the secure way to skip hostname validation without using InsecureSkipVerify.
7073
config.ServerName = ""
7174
return nil
7275
}

msdsn/conn_str_go115pre.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ func setupTLSCommonName(config *tls.Config, pem []byte) error {
1313

1414
// setupTLSCertificateOnly validates the certificate chain without checking the hostname
1515
func setupTLSCertificateOnly(config *tls.Config, pem []byte) error {
16-
// Skip hostname validation by setting ServerName to empty string
17-
// The certificate chain will still be verified against RootCAs (set later in SetupTLS)
16+
// Skip hostname validation by setting ServerName to empty string.
17+
// When ServerName is empty, Go's TLS implementation will skip hostname verification
18+
// but still verify the certificate chain against the RootCAs (configured in SetupTLS after this function returns).
1819
config.ServerName = ""
1920
return nil
2021
}

0 commit comments

Comments
 (0)