Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the tls min version to dsn #736

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 22 additions & 3 deletions msdsn/conn_str.go
Expand Up @@ -75,11 +75,11 @@ type Config struct {
PacketSize uint16
}

func SetupTLS(certificate string, insecureSkipVerify bool, hostInCertificate string) (*tls.Config, error) {
func SetupTLS(certificate string, insecureSkipVerify bool, hostInCertificate string, tlsMinVer uint16) (*tls.Config, error) {
config := tls.Config{
ServerName: hostInCertificate,
InsecureSkipVerify: insecureSkipVerify,

MinVersion: tlsMinVer,
// fix for https://github.com/denisenkom/go-mssqldb/issues/166
// Go implementation of TLS payload size heuristic algorithm splits single TDS package to multiple TCP segments,
// while SQL Server seems to expect one TCP segment per encrypted TDS package.
Expand Down Expand Up @@ -254,10 +254,29 @@ func Parse(dsn string) (Config, map[string]string, error) {
hostInCertificate = p.Host
p.HostInCertificateProvided = false
}
tlsversion, ok := params["tlsminversion"]
tlsMinVer := uint16(0)
if ok {
tlsversion = strings.ToUpper(tlsversion)
switch tlsversion {
case "TLS1.0":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just "1.0" and "1.2" etc since we already know the parameter name is about TLS?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhiyunliu we'd consider taking this change in the microsoft fork
github.com/microsoft/go-mssqldb

tlsMinVer = tls.VersionTLS10
case "TLS1.1":
tlsMinVer = tls.VersionTLS11
case "TLS1.2":
tlsMinVer = tls.VersionTLS12
/*comment by go1.8 ~ go1.11 has no tls.VersionTLS13
case "TLS1.3":
tlsMinVer = tls.VersionTLS13
*/
default:
tlsMinVer = 0
}
}

if p.Encryption != EncryptionDisabled {
var err error
p.TLSConfig, err = SetupTLS(certificate, trustServerCert, hostInCertificate)
p.TLSConfig, err = SetupTLS(certificate, trustServerCert, hostInCertificate, tlsMinVer)
if err != nil {
return p, params, fmt.Errorf("failed to setup TLS: %w", err)
}
Expand Down
29 changes: 29 additions & 0 deletions msdsn/conn_str_test.go
@@ -1,6 +1,7 @@
package msdsn

import (
"crypto/tls"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -196,3 +197,31 @@ func TestConnParseRoundTripFixed(t *testing.T) {
t.Fatal("Parameters do not match after roundtrip", params, rtParams)
}
}

func TestConnParseWithTlsVersion(t *testing.T) {
tests := []struct {
name string
connStr string
wantCfg *Config
}{
{name: "1.TLS1.0", connStr: "sqlserver://someuser@somehost?tlsminversion=tls1.0", wantCfg: &Config{TLSConfig: &tls.Config{MinVersion: tls.VersionTLS10}}},
{name: "2.TLS1.1", connStr: "sqlserver://someuser@somehost?tlsminversion=tls1.1", wantCfg: &Config{TLSConfig: &tls.Config{MinVersion: tls.VersionTLS11}}},
{name: "3.TLS1.2", connStr: "sqlserver://someuser@somehost?tlsminversion=tls1.2", wantCfg: &Config{TLSConfig: &tls.Config{MinVersion: tls.VersionTLS12}}},
{name: "4.no tlsminversion parameter", connStr: "sqlserver://someuser@somehost", wantCfg: &Config{TLSConfig: &tls.Config{MinVersion: 0}}},
{name: "5.wrong tlsminversion parameter", connStr: "sqlserver://someuser@somehost?tlsminversion=wrongtlsversion", wantCfg: &Config{TLSConfig: &tls.Config{MinVersion: 0}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _, err := Parse(tt.connStr)
if err != nil {
t.Errorf("%s Parse Error:%+v", tt.name, err)
return
}
if got.TLSConfig.MinVersion != tt.wantCfg.TLSConfig.MinVersion {
t.Errorf("%s Parse MinVersion not match. want:%d, got:%d", tt.name, tt.wantCfg.TLSConfig.MinVersion, got.TLSConfig.MinVersion)
return
}
})
}

}
2 changes: 1 addition & 1 deletion tds.go
Expand Up @@ -1150,7 +1150,7 @@ initiate_connection:
}
}
if config == nil {
config, err = msdsn.SetupTLS("", false, p.Host)
config, err = msdsn.SetupTLS("", false, p.Host, 0)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try to many way add the test case . but no one can cover this line.

if err != nil {
return nil, err
}
Expand Down