@@ -37,6 +37,10 @@ public final class MSSQLConnection: SQLDatabase, @unchecked Sendable {
3737 /// SQL Server password. Not required when using Windows/NTLM authentication (`domain` is set).
3838 public var password : String = " "
3939 public var tls : SQLTLSConfiguration = . prefer
40+ /// When `true`, the server's TLS certificate is accepted without verification
41+ /// (equivalent to `TrustServerCertificate=true` in a SQL Server connection string).
42+ /// Set this to `true` when connecting to servers with self-signed certificates (e.g. dev/test).
43+ public var trustServerCertificate : Bool = false
4044 public var logger : Logger = Logger ( label: " MSSQLNio " )
4145 /// Timeout for establishing the TCP + TLS + Login7 handshake (seconds). nil = no limit.
4246 public var connectTimeout : TimeInterval ? = 30
@@ -52,18 +56,20 @@ public final class MSSQLConnection: SQLDatabase, @unchecked Sendable {
5256 public init ( host: String , port: Int = 1433 ,
5357 database: String , username: String , password: String ,
5458 tls: SQLTLSConfiguration = . prefer,
59+ trustServerCertificate: Bool = false ,
5560 connectTimeout: TimeInterval ? = 30 ,
5661 queryTimeout: TimeInterval ? = nil ,
5762 readOnly: Bool = false ) {
58- self . host = host
59- self . port = port
60- self . database = database
61- self . username = username
62- self . password = password
63- self . tls = tls
64- self . connectTimeout = connectTimeout
65- self . queryTimeout = queryTimeout
66- self . readOnly = readOnly
63+ self . host = host
64+ self . port = port
65+ self . database = database
66+ self . username = username
67+ self . password = password
68+ self . tls = tls
69+ self . trustServerCertificate = trustServerCertificate
70+ self . connectTimeout = connectTimeout
71+ self . queryTimeout = queryTimeout
72+ self . readOnly = readOnly
6773 }
6874
6975 /// Windows/NTLM authentication. Username and password are optional;
@@ -75,19 +81,21 @@ public final class MSSQLConnection: SQLDatabase, @unchecked Sendable {
7581 username: String = " " ,
7682 password: String = " " ,
7783 tls: SQLTLSConfiguration = . prefer,
84+ trustServerCertificate: Bool = false ,
7885 connectTimeout: TimeInterval ? = 30 ,
7986 queryTimeout: TimeInterval ? = nil ,
8087 readOnly: Bool = false ) {
81- self . host = host
82- self . port = port
83- self . database = database
84- self . domain = domain
85- self . username = username
86- self . password = password
87- self . tls = tls
88- self . connectTimeout = connectTimeout
89- self . queryTimeout = queryTimeout
90- self . readOnly = readOnly
88+ self . host = host
89+ self . port = port
90+ self . database = database
91+ self . domain = domain
92+ self . username = username
93+ self . password = password
94+ self . tls = tls
95+ self . trustServerCertificate = trustServerCertificate
96+ self . connectTimeout = connectTimeout
97+ self . queryTimeout = queryTimeout
98+ self . readOnly = readOnly
9199 }
92100 }
93101
@@ -201,7 +209,9 @@ public final class MSSQLConnection: SQLDatabase, @unchecked Sendable {
201209
202210 private func upgradeTLS( ) async throws {
203211 var tlsConfig = TLSConfiguration . makeClientConfiguration ( )
204- tlsConfig. certificateVerification = . none // dev/test; in production supply a CA cert
212+ if config. trustServerCertificate {
213+ tlsConfig. certificateVerification = . none
214+ }
205215 let sslContext = try NIOSSLContext ( configuration: tlsConfig)
206216 // IP addresses cannot be used for SNI — pass nil to disable SNI for IP hosts
207217 let sniHostname : String ? = {
0 commit comments