Share via


設定 TLS 1.3

適用於:SQL Server 2022 (16.x) 和更新版本

本篇文章說明如何︰

  1. 設定 SQL Server 2022 (16.x) 的實例以使用傳輸層安全性 (TLS) 1.3 和 TLS 1.2
  2. 確認通訊協定可運作
  3. 停用較舊的不安全通訊協定,包括 TLS 1.0 和 1.1

需求

SQL Server 2022 (16.x) 中的 TLS 1.3 支援需要:

  • Windows Server 2022
  • SQL Server 2022 (16.x) 含累積更新 1 或更新版本
  • SQL Server 實例使用 TCP/IP 作為網路協定
  • 隨私鑰一起安裝的有效 X.509 伺服器證書

重要

本文件假設您的需求在短期內同時包含 TLS 1.3 和 TLS 1.2,而長期只包含 TLS 1.3。

SQL Server 和 TLS

SQL Server 不會自行執行 TLS 作業,而是使用 Schannel SSP 由 Windows 執行這項工作。 安全通道是一種安全性支援提供者(SSP),其中包含並公開 Microsoft 的因特網標準安全性通訊協定實作,例如 TLS。 通道是 Windows OpenSSL 對 Linux 的內容。

設定 SQL Server 的 TLS 需要設定適用於 Windows 的 TLS。

在 Windows Server 2022 上使用 SQL Server 2022 (16.x),SQL Server 支援 TLS 1.0、1.1、1.2 和 1.3。 若要確認這一點,請使用 GitHub at TlsTest提供的 .NET 程式代碼。 工具的輸出如下所示:

Trying Ssl2
Authentication failed, see inner exception.
Exception: The client and server cannot communicate, because they do not possess a common algorithm.
Trying Ssl3
Authentication failed, see inner exception.
Exception: The client and server cannot communicate, because they do not possess a common algorithm.
Trying Tls
Tls using TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
Trying Tls11
Tls11 using TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
Trying Tls12
Tls12 using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
Trying Tls13
Tls13 using TLS_AES_256_GCM_SHA384

將 Windows 設定為只使用 TLS 1.2 和 TLS 1.3

Windows 有一組登錄機碼 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL ,可控制 TLS 通訊協定版本和加密套件。 在此案例中,只有影響伺服器的通訊協定版本很重要,因為 SQL Server 實例會做為伺服器。

下列 PowerShell 腳本 會更新登錄,以 在伺服器使用時啟用或停用 TLS 1.0 和 TLS 1.1:

警告

繼續之前, 請先備份登錄。 如有必要,這可讓您在未來還原登錄。

# Learn more at https://learn.microsoft.com/windows-server/security/tls/tls-registry-settings?tabs=diffie-hellman
Set-StrictMode -Version Latest

$base = 'HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\'
$protocols = [ordered]@{
    "SSL 2.0" = $false
    "SSL 3.0" = $false
    "TLS 1.0" = $false
    "TLS 1.1" = $false
    "TLS 1.2" = $true
    "TLS 1.3" = $true
}

foreach ($version in $protocols.Keys) {

    $enabledValue = $protocols[$version]
    $path = $base + $version + '\Server'

    New-Item $path -Force | Out-Null
    New-ItemProperty -Path $path `
                     -Name 'Enabled' `
                     -Value $enabledValue `
                     -PropertyType 'DWord' `
                     -Force | Out-Null
                     
    Write-Host "$version is $enabledValue."
}

此程式代碼可在 GitHub 中 於 set-reset-tls.ps1 取得。

執行此腳本之後,請重新啟動 SQL Server 進程,讓新的 TLS 設定生效。 如果您現在執行文章開頭所述的程式碼,它會傳回:

Trying Ssl2
Authentication failed, see inner exception.
Exception: The client and server cannot communicate, because they do not possess a common algorithm.
Trying Ssl3
Authentication failed, see inner exception.
Exception: The client and server cannot communicate, because they do not possess a common algorithm.
Trying Tls
Received an unexpected EOF    or 0 bytes from the transport stream.
Exception:
Trying Tls11
Received an unexpected EOF or 0 bytes from the transport stream.
Exception:
Trying Tls12
Tls12 using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
Trying Tls13
Tls13 using TLS_AES_256_GCM_SHA384

請注意,SSL 2.0、SSL 3.0、TLS 1.0 和 TLS 1.1 全都無法連線,但 TLS 1.2 和 TLS 1.3 都成功。

登錄更新之後,Windows 和 SQL Server 的這個實例只允許 TLS 1.2 和 TLS 1.3 連線。 稍後,當更多用戶端支援 TLS 1.3 時,您也可以停用 TLS 1.2。

設定 SQL Server 實例以強制嚴格加密

最後一個步驟是將 實體設定為使用 Force Strict Encryption。 使用 Force Strict Encryption時,SQL 實例會使用支援的表格式數據流版本(TDS 8.0 或更新版本)。

使用 SQL Server 組態管理員 來設定此設定。

  1. 展開 [SQL Server 網络組態]

  2. 以滑鼠右鍵按鍵按鍵按鍵的<instance name> [通訊協定],然後選取 [屬性]

    默認實例名稱為 MSSQLSERVER

  3. 在 [旗標] 索引標籤上,將 [強制嚴格加密] 設定為 [是]

    Screenshot of the UI control for SQL Server Configuration Manager, configure protocols dialog.

確認安全性

本節示範如何使用 Wireshark、OpenSSL 和 Nmap 來驗證加密。

Wireshark

您可以使用網路探查器來判斷 TLS 通訊協定版本和已同意的加密套件。 您可能會發現某些數據令人困惑。 如果您從Wireshark查看下面的螢幕快照,它會顯示封包是TLS v1.3 記錄層,但通訊協定版本是TLS 1.2,而交握通訊協定版本也是TLS 1.2。 這是 TLS 1.2 規格的所有部分,且正確且預期。 同意的通訊協定版本位於 [延伸模組] 區段中,如您所見, supported_versions 是 TLS 1.3。

Screenshot of the TLS extension section.

OpenSSL

您也可以使用 openssl 來探索已同意的 TLS 資訊。

使用下列命令:

openssl s_client 127.0.0.1:1433

此指令會傳回結果,例如:

Post-Handshake New Session Ticket arrived:
SSL-Session:
   Protocol   : TLSv1.3
   Cipher     : TLS_AES_256_GCM_SHA384
   Session-ID : 516D56D99088BCDE1 <snip> 098EDB1A
   Session-ID-ctx:
   Resumption PSD: B2B9CB92B59aa1 <snip> BD824CBA
   PSK identity: None

Nmap

目前版本的Nmap版本7.94,在使用下列專案時,似乎不會偵測到TLS 1.3:

nmap -sV --script ssl-enum-ciphers -p 1433 127.0.0.1.