You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 8acf068
Browse filesBrowse the repository at this point in the historyBrowse files
Fix | Perform ServerCertificate pin validation to verify with server provided cert (#4445)
* Server Certificate validation changes
* Address comments
* Address review feedback: reorder pin check after policy checks
- Perform ServerCertificate pin validation after chain/name policy error
handling, making the additive semantics explicit in control flow.
- Extract ValidateCertificatePin/LoadValidationCertificate helpers so the
loaded pin certificate lives in a narrow 'using' scope (no try/finally).
- Guard against a null server certificate in the pin path.
- Clarify method docs on the policyErrors relationship and reword the
no-pin fast-path trace message.
- Replace ambiguous 'platform trust' wording in docs with chain-and-name
validation, and note that pinning does not itself confer trust.
- Add TempCertFile disposable test helper and a null-server-cert regression
test for the policyErrors == None case.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
* Revert to minimal design: pin always consulted, fail closed on load error
Restores the original semantic where an exact ServerCertificate match
satisfies certificate validation. The only behavior changes remain:
- The policyErrors == None fast path now also requires that no
ServerCertificate was supplied, so a configured certificate is always
compared against the one presented by the server.
- A ServerCertificate that cannot be loaded/parsed now fails the
connection instead of being silently ignored.
Also keeps the non-design fixes: the loaded certificate is disposed via a
narrow using scope, and a null server certificate throws
AuthenticationException instead of NullReferenceException.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
* Address review: dedicated string, RemoteCertificateNotAvailable guard, safe Dispose
- Add SQL_ServerCertificateNotAvailable so the failure names the option in
play rather than reusing the generic policy-error text.
- A matching ServerCertificate no longer satisfies validation when
RemoteCertificateNotAvailable is reported. The flag and a null serverCert
are expected to agree, but both are now checked so the contradictory
combination cannot be accepted via comparison. Documented the
relationship on the method.
- Guard TempCertFile.Dispose so a cleanup failure cannot mask a real
assertion failure.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
---------
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copy file name to clipboardExpand all lines: doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1015,7 +1015,7 @@ The following table lists the valid names for keyword values within the <xref:Mi
1015
1015
|Failover Partner|N/A|The name of the failover partner server where database mirroring is configured.<br /><br /> If the value of this key is "", then **Initial Catalog** must be present, and its value must not be "".<br /><br /> The server name can be 128 characters or less.<br /><br /> If you specify a failover partner but the failover partner server is not configured for database mirroring and the primary server (specified with the Server keyword) is not available, then the connection will fail.<br /><br /> If you specify a failover partner and the primary server is not configured for database mirroring, the connection to the primary server (specified with the Server keyword) will succeed if the primary server is available.|
1016
1016
|Failover Partner SPN<br /><br /> -or-<br /><br /> FailoverPartnerSPN|N/A|The SPN for the failover partner. The default value is an empty string, which causes SqlClient to use the default, driver-generated SPN.<br /><br /> (Only available in v5.0+)|
1017
1017
|Host Name In Certificate<br /><br /> -or-<br /><br />HostNameInCertificate|N/A|The host name to use when validating the server certificate. When not specified, the server name from the Data Source is used for certificate validation.<br /><br /> (Only available in v5.0+)|
1018
-
|Server Certificate<br /><br /> -or-<br /><br />ServerCertificate|N/A|The path to a certificate file to match against the SQL Server TLS/SSL certificate. The accepted certificate formats are PEM, DER, and CER. If specified, the SQL Server certificate is checked by verifying if the ServerCertificate provided is an exact match.<br /><br /> (Only available in v5.1+)|
1018
+
|Server Certificate<br /><br /> -or-<br /><br />ServerCertificate|N/A|The path to a certificate file to match against the SQL Server TLS/SSL certificate. The accepted certificate formats are PEM, DER, and CER. If specified, the SQL Server certificate is checked by verifying if the ServerCertificate provided is an exact match.<br /><br />When specified, this comparison is always performed, including when the certificate already passes the usual chain-and-name validation. If the presented certificate does not match, or the file cannot be loaded or parsed, the TLS handshake fails; a configured `ServerCertificate` is never silently ignored.<br /><br />Certificate validation itself can be disabled by `TrustServerCertificate=true` (except with `Encrypt=strict`, where validation is always performed). When validation is disabled, `ServerCertificate` is not consulted.<br /><br /> (Only available in v5.1+)|
1019
1019
|Initial Catalog<br /><br /> -or-<br /><br /> Database|N/A|The name of the database.<br /><br /> The database name can be 128 characters or less.|
1020
1020
|Integrated Security<br /><br /> -or-<br /><br /> Trusted_Connection|'false'|When `false`, User ID and Password are specified in the connection. When `true`, the current Windows account credentials are used for authentication.<br /><br /> Recognized values are `true`, `false`, `yes`, `no`, and `sspi` (strongly recommended), which is equivalent to `true`.<br /><br /> If User ID and Password are specified and Integrated Security is set to true, the User ID and Password will be ignored and Integrated Security will be used.<br /><br /> <xref:Microsoft.Data.SqlClient.SqlCredential> is a more secure way to specify credentials for a connection that uses SQL Server Authentication (`Integrated Security=false`).|
1021
1021
|IP Address Preference<br /><br /> -or-<br /><br /> IPAddressPreference|IPv4First|The IP address family preference when establishing TCP connections. If `Transparent Network IP Resolution` (in .NET Framework) or `Multi Subnet Failover` is set to true, this setting has no effect. Supported values include:<br /><br /> `IPAddressPreference=IPv4First`<br /><br />`IPAddressPreference=IPv6First`<br /><br />`IPAddressPreference=UsePlatformDefault`|
Copy file name to clipboardExpand all lines: doc/snippets/Microsoft.Data.SqlClient/SqlConnectionStringBuilder.xml
+7Lines changed: 7 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1332,6 +1332,13 @@ Database = AdventureWorks
1332
1332
<![CDATA[
1333
1333
This property corresponds to the "Server Certificate" and "ServerCertificate" keys within the connection string.
1334
1334
1335
+
> [!NOTE]
1336
+
> When `ServerCertificate` is specified, the certificate presented by the server is always compared against the certificate loaded from this path, including when the presented certificate already passes chain-and-name validation. An exact match satisfies certificate validation.
1337
+
>
1338
+
> If the presented certificate does not match, or the file cannot be loaded or parsed, the TLS handshake fails; a configured `ServerCertificate` is never silently ignored.
1339
+
>
1340
+
> Certificate validation itself can be disabled by `TrustServerCertificate=true` (except with `Encrypt=strict`, where validation is always performed). When validation is disabled, `ServerCertificate` is not consulted.
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniCommon),EventType.INFO,"Connection Id {0}, targetServerName {1}, SSL Server certificate not validated as PolicyErrors set to None.",args0:connectionId,args1:targetServerName);
79
+
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniCommon),EventType.INFO,"Connection Id {0}, targetServerName {1}, SSL Server certificate has no policy errors and no ServerCertificate was supplied; skipping certificate comparison.",args0:connectionId,args1:targetServerName);
// if this fails, then fall back to the HostNameInCertificate or TargetServer validation.
92
-
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniTcpHandle),EventType.INFO,"Connection Id {0}, Exception occurred loading specified ServerCertificate: {1}, treating it as if ServerCertificate has not been specified.",args0:connectionId,args1:e.Message);
102
+
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniCommon),EventType.ERR,"Connection Id {0}, ServerCertificate was specified but the server presented no certificate. Certificate validation failed.",args0:connectionId);
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniCommon),EventType.INFO,"Connection Id {0}, ServerCertificate matches the certificate provided by the server. Certificate validation passed.",args0:connectionId);
101
111
returntrue;
102
112
}
103
113
else
104
114
{
105
-
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniCommon),EventType.INFO,"Connection Id {0}, ServerCertificate doesn't match the certificate provided by the server. Certificate validation failed.",args0:connectionId);
115
+
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniCommon),EventType.ERR,"Connection Id {0}, ServerCertificate doesn't match the certificate provided by the server. Certificate validation failed.",args0:connectionId);
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniCommon),EventType.ERR,"Connection Id {0}, targetServerName {1}, SSL Server certificate not validated as PolicyErrors set to RemoteCertificateNotAvailable.",args0:connectionId,args1:targetServerName);
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniCommon),EventType.ERR,"Connection Id {0}, targetServerName {1}, SSL Server certificate not validated as PolicyErrors set to RemoteCertificateNotAvailable.",args0:connectionId,args1:targetServerName);
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniCommon),EventType.ERR,"Connection Id {0}, serverNameToValidate {1}, Target Server name or HNIC does not match the Subject/SAN in Certificate.",args0:connectionId,args1:serverNameToValidate);
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniCommon),EventType.ERR,"Connection Id {0}, serverNameToValidate {1}, Target Server name or HNIC does not match the Subject/SAN in Certificate.",args0:connectionId,args1:serverNameToValidate);
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniCommon),EventType.INFO,"Connection Id {0}, certificate with subject: {1}, validated successfully.",args0:connectionId,args1:serverCert.Subject);
160
168
returntrue;
161
169
}
162
170
}
163
171
172
+
/// <summary>
173
+
/// Loads the certificate specified by the <c>ServerCertificate</c> option, failing closed
174
+
/// when it cannot be loaded or parsed.
175
+
/// </summary>
176
+
/// <param name="connectionId">Connection ID/GUID for tracing</param>
177
+
/// <param name="validationCertFileName">Path to the X.509 certificate file</param>
0 commit comments