佈建已啟用記憶體保護區的金鑰

適用於:SQL Server 2019 (15.x) 和更新版本 - 僅限 WindowsAzure SQL Database

本文描述如何佈建已啟用記憶體保護區的金鑰,其支援位於伺服器端安全記憶體保護區內用於具備安全記憶體保護區 Always Encrypted 的計算。

在您佈建已啟用記憶體保護區的金鑰時,適用管理 Always Encrypted 金鑰的一般方針和流程。 本文說明適用於具備安全記憶體保護區 Always Encrypted 的詳細資料。

若要使用 SQL Server Management Studio 或 PowerShell 佈建已啟用安全記憶體保護區的資料行主要金鑰,請確認新的金鑰支援記憶體保護區計算。 這會使工具 (SSMS 或 PowerShell) 產生 CREATE COLUMN MASTER KEY 陳述式,在資料庫中的資料行主要金鑰中繼資料內設定 ENCLAVE_COMPUTATIONS。 如需詳細資訊,請參閱 CREATE COLUMN MASTER KEY (Transact-SQL)

工具也會使用資料行主要金鑰對資料行主要屬性進行數位簽署,並會將簽章儲存在資料庫中繼資料內。 簽章能防止 ENCLAVE_COMPUTATIONS 被惡意竄改。 SQL 用戶端驅動程式會先驗證簽章,才允許使用記憶體保護區。 這可讓安全性系統管理員控制在記憶體保護區內計算哪些資料行資料。

ENCLAVE_COMPUTATIONS 是固定的,這表示您在中繼資料中定義資料行主要金鑰後,便無法進行變更。 若要使用指定資料行主要金鑰所加密資料行來加密金鑰啟用記憶體保護區計算,您需要輪替資料行主要金鑰,並以已啟用記憶體保護區的資料行主要金鑰來加以取代。 請參閱輪替已啟用記憶體保護區的金鑰

注意

目前,SSMS 和 PowerShell 都支援儲存在 Azure Key Vault 或 Windows 憑證存放區中已啟用記憶體保護區的資料行主要金鑰。 不支援硬體安全性模組 (使用 CNG 或 CAPI)。

若要建立已啟用記憶體保護區的資料行加密金鑰,您需要確認您選取已啟用記憶體保護區的資料行主要金鑰來加密新金鑰。

下列各節提供詳細資料,說明如何使用 SSMS 和 PowerShell 來佈建已啟用記憶體保護區的金鑰。

使用 SQL Server Management Studio 佈建已啟用記憶體保護區的金鑰

您可在 SQL Server Management Studio 中佈建:

  • 使用 [新增資料行主要金鑰] 對話方塊來佈建已啟用記憶體保護區的資料行主要金鑰。
  • 使用 [新增資料行加密金鑰] 對話方塊來佈建已啟用記憶體保護區的資料行加密金鑰。

Always Encrypted 精靈也可讓您建立已啟用記憶體保護區的資料行主要金鑰和已啟用記憶體保護區的資料行加密金鑰。

確定您已安裝最新正式發行 (GA) 版的 SQL Server Management Studio (SSMS)

使用 [新增資料行主要金鑰] 對話方塊來佈建已啟用記憶體保護區的資料行主要金鑰

請遵循使用 [新增資料行主要金鑰] 對話方塊佈建資料行主要金鑰中步驟來佈建已啟用記憶體保護區的資料行主要金鑰。 請確認選取 [允許記憶體保護區計算]。 請參閱以下的螢幕擷取畫面:

Allow enclave computations

注意

只有當資料庫設定有安全記憶體保護區時,才會出現 [允許記憶體保護區計算] 核取方塊。 如果您正在使用 SQL Server,請參閱在 SQL Server 中設定安全記憶體保護區。 如果您正在使用 Azure SQL Database,請參閱為 Azure SQL Database 啟用具有安全記憶體保護區的 Always Encrypted

提示

若要檢查特定資料行主要金鑰是否已啟用記憶體保護區,請在 [物件總管] 中以滑鼠右鍵按一下該資料行主要金鑰,並選取 [屬性]。 若金鑰已啟用記憶體保護區,視窗中便會出現 [記憶體保護區計算: 允許],其顯示金鑰的屬性。 或者,您可以使用 sys.column_master_keys (Transact-SQL) 檢視。

使用 [新增資料行加密金鑰] 對話方塊來佈建已啟用記憶體保護區的資料行加密金鑰

請遵循使用 [新增資料行加密金鑰] 對話方塊佈建資料行加密金鑰中步驟來佈建已啟用記憶體保護區的資料行加密金鑰。 選取資料行主要金鑰時,請確認該資料行主要金鑰已啟用記憶體保護區。

提示

若要檢查特定資料行加密金鑰是否已啟用記憶體保護區,請在 [物件總管] 中以滑鼠右鍵按一下該資料行加密金鑰,並選取 [屬性]。 若金鑰已啟用記憶體保護區,視窗中便會出現 [記憶體保護區計算: 允許],其顯示金鑰的屬性。

使用 PowerShell 佈建已啟用記憶體保護區的金鑰

若要使用 PowerShell 佈建啟用記憶體保護區的金鑰,則需要有 SqlServer PowerShell 模組版本 22 或更高版本。

一般而言,使用 PowerShell 佈建 Always Encrypted 金鑰中所描述 PowerShell 的 Always Encrypted 金鑰佈建工作流程 (搭配或不搭配角色隔離),其也適用於已啟用記憶體保護區的金鑰。 本節描述已啟用記憶體保護區的金鑰特定詳細資料。

SqlServer PowerShell 模組會使用 -AllowEnclaveComputations 參數來延伸 New-SqlCertificateStoreColumnMasterKeySettingsNew-SqlAzureKeyVaultColumnMasterKeySettings Cmdlet,以允許您在佈建流程期間指定已啟用記憶體保護區的資料行主要金鑰。 其中任何一個 Cmdlet 都會建立本機物件,包含資料行主要金鑰的屬性 (儲存在 Azure Key Vault 或 Windows 憑證存放區中)。 若進行指定,-AllowEnclaveComputations 屬性便會在本機物件中將金鑰標記為已啟用記憶體保護區。 這也會使 Cmdlet 存取參考的資料行主要金鑰 (位於 Azure Key Vault 或 Windows 憑證存放區中),以對金鑰屬性進行數位簽署。 在您為已啟用記憶體保護區資料行的新主要金鑰建立設定物件後,您便可以在後續引動 New-SqlColumnMasterKey Cmdlet 時使用該金鑰,來建立描述資料庫中新金鑰的中繼資料物件。

佈建已啟用記憶體保護區資料行加密金鑰的過程,與佈建未啟用記憶體保護區資料行加密金鑰的過程沒有任何不同。 您只需要確認用來加密新資料行加密金鑰的資料行主要金鑰已啟用記憶體保護區。

注意

SqlServer PowerShell 模組目前不支援佈建儲存在硬體安全性模組 (使用 CNG 或 CAPI) 中已啟用記憶體保護區的金鑰。

範例 - 使用 Windows 憑證存放區佈建已啟用記憶體保護區的金鑰

以下端對端範例會示範如何佈建已啟用記憶體保護區的金鑰,並儲存 Windows 憑證存放區中的資料行主要金鑰。 指令碼是以不使用角色隔離的 Windows 憑證存放區 (範例) 中範例為基礎。 值得注意的是在 New-SqlCertificateStoreColumnMasterKeySettings Cmdlet 中使用了 -AllowEnclaveComputations 參數;此為兩個範例中工作流程間的唯一差異。

# Create a column master key in Windows Certificate Store.
$cert = New-SelfSignedCertificate -Subject "AlwaysEncryptedCert" -CertStoreLocation Cert:CurrentUser\My -KeyExportPolicy Exportable -Type DocumentEncryptionCert -KeyUsage DataEncipherment -KeySpec KeyExchange

# Import the SqlServer module.
Import-Module "SqlServer" -MinimumVersion 22.0.50

# Connect to your database.
$serverName = "<server name>"
$databaseName = "<database name>"
# Change the authentication method in the connection string, if needed.
$connStr = "Server = " + $serverName + "; Database = " + $databaseName + "; Integrated Security = True"
$database = Get-SqlDatabase -ConnectionString $connStr

# Create a SqlColumnMasterKeySettings object for your column master key
# using the -AllowEnclaveComputations parameter.
$cmkSettings = New-SqlCertificateStoreColumnMasterKeySettings -CertificateStoreLocation "CurrentUser" -Thumbprint $cert.Thumbprint -AllowEnclaveComputations

# Create column master key metadata in the database.
$cmkName = "CMK1"
New-SqlColumnMasterKey -Name $cmkName -InputObject $database -ColumnMasterKeySettings $cmkSettings

# Generate a column encryption key, encrypt it with the column master key and create column encryption key metadata in the database. 
$cekName = "CEK1"
New-SqlColumnEncryptionKey -Name $cekName  -InputObject $database -ColumnMasterKey $cmkName

範例 - 使用 Azure Key Vault 佈建已啟用記憶體保護區的金鑰

以下端對端範例會示範如何佈建啟用記憶體保護區的金鑰,以將資料行主要金鑰儲存在 Azure Key Vault 中的金鑰保存庫。 指令碼是以不使用角色隔離的 Azure Key Vault (範例) 中範例為基礎。 請務必注意已啟用記憶體保護區金鑰與未啟用記憶體保護區金鑰工作流程之間的兩項差異。

  • 在以下指令碼中,New-SqlCertificateStoreColumnMasterKeySettings 使用 -AllowEnclaveComputations 參數來讓新資料行主要金鑰啟用記憶體保護區。
  • 以下指令碼使用 Get-AzAccessToken Cmdlet 來取得金鑰保存庫的存取權杖。 這是必要的,因為 New-SqlAzureKeyVaultColumnMasterKeySettings 需要有權存取 Azure Key Vault 才能簽署資料行主要金鑰的屬性。
# Create a column master key in Azure Key Vault.
Import-Module "SqlServer" -MinimumVersion 22.0.50
Import-Module Az.Accounts -MinimumVersion 2.2.0
Connect-AzAccount
$SubscriptionId = "<Azure SubscriptionId>"
$resourceGroup = "<resource group name>"
$azureLocation = "<datacenter location>"
$akvName = "<key vault name>"
$akvKeyName = "<key name>"
$azureCtx = Set-AzConteXt -SubscriptionId $SubscriptionId # Sets the context for the below cmdlets to the specified subscription.
New-AzResourceGroup -Name $resourceGroup -Location $azureLocation # Creates a new resource group - skip, if your desired group already exists.
New-AzKeyVault -VaultName $akvName -ResourceGroupName $resourceGroup -Location $azureLocation # Creates a new key vault - skip if your vault already exists.
Set-AzKeyVaultAccessPolicy -VaultName $akvName -ResourceGroupName $resourceGroup -PermissionsToKeys get, create, delete, list, wrapKey,unwrapKey, sign, verify -UserPrincipalName $azureCtx.Account
$akvKey = Add-AzKeyVaultKey -VaultName $akvName -Name $akvKeyName -Destination "Software"

# Connect to your database.
$serverName = "<server name>"
$databaseName = "<database name>"
# Change the authentication method in the connection string, if needed.
$connStr = "Server = " + $serverName + "; Database = " + $databaseName + "; Integrated Security = True"
$database = Get-SqlDatabase -ConnectionString $connStr

# Obtain an access token for key vaults.
$keyVaultAccessToken = (Get-AzAccessToken -ResourceUrl https://vault.azure.net).Token 

# Create a SqlColumnMasterKeySettings object for your column master key. 
$cmkSettings = New-SqlAzureKeyVaultColumnMasterKeySettings -KeyURL $akvKey.ID -AllowEnclaveComputations -KeyVaultAccessToken $keyVaultAccessToken

# Create column master key metadata in the database.
$cmkName = "CMK1"
New-SqlColumnMasterKey -Name $cmkName -InputObject $database -ColumnMasterKeySettings $cmkSettings

# Generate a column encryption key, encrypt it with the column master key and create column encryption key metadata in the database. 
$cekName = "CEK1"
New-SqlColumnEncryptionKey -Name $cekName -InputObject $database -ColumnMasterKey $cmkName -KeyVaultAccessToken $keyVaultAccessToken

下一步

另請參閱