Microsoft SQL Server 2019 Always Encrypted: nShield HSM Integration Guide
Table of Contents
Introduction
Always Encrypted is a feature in Windows SQL Server 2019 designed to protect sensitive data both at rest and in flight between an on-premises client application server and Azure or SQL Server database(s).
Data protected by Always Encrypted remains in an encrypted state until it has reached the on–premises client application server. This effectively mitigates man-in-the-middle attacks and provides assurances against unauthorized activity from rogue DBAs or admins with access to Azure or SQL server Databases.
The nshield HSM secures the key used to protect the Column Master Key, stored in an encrypted state on the on-premises client application server.
Product configurations
Entrust successfully tested nshield HSM integration with Windows SQL Server 2019 and the Always Encrypted feature in the following configurations:
Remote server
Product | Version |
---|---|
SQL Server |
Microsoft SQL Server 2019 |
Base OS |
Windows Server 2019 Datacenter |
On-premises client
Product | Version |
---|---|
SQL Server GUI |
Microsoft SQL Server Management Studio V18.8 |
Base OS |
Windows 10 Enterprise |
Supported nshield features
Entrust successfully tested nshield HSM integration with the following features:
Feature | Support |
---|---|
Module Only Key |
Yes |
OCS cards |
Yes |
Supported nshield hardware and software versions
Entrust successfully tested with the following nshield hardware and software versions:
Connect XC
Security World Software | Firmware | Image | OCS | Module |
---|---|---|---|---|
12.80.4 |
FIPS 12.50.11 |
12.60.10 |
✓ |
✓ |
12.80.4 |
CC 12.50.7 |
12.50.7 |
✓ |
✓ |
Connect +
Security World Software | Firmware | Image | OCS | Module |
---|---|---|---|---|
12.80.4 |
FIPS 12.50.8 |
12.60.10 |
✓ |
✓ |
12.40 Compatibility Package |
CC 2.55.4 |
12.45.1 |
✓ |
✓ |
Role separation
The generation of keys, and the application of these keys for encryption or decryption are separate processes. The processes can be assigned to users with various access permissions, or Duty Roles. The table below shows the processes and duty roles with reference to the Security Administrator and the Database Administrator.
Process | Duty Role |
---|---|
Generating the Column Master Key (CMK) and Column Encryption Key (CEK) |
Security Administrator |
Applying the CMK and CEK in the database |
Database Administrator |
Four database permissions are required for Always Encrypted.
Operation | Description |
---|---|
|
Required to generate and delete a column master key |
|
Required to generate and delete a column encryption key |
|
Required to access and read the metadata of the column master keys to manage keys or query encrypted columns |
|
Required to access and read the metadata of the column encryption key to manage keys or query encrypted columns |
Using multiple on-premises client servers
Each client server wanting access to the contents of data encrypted with a given CEK must have access to an HSM in the same Security World and have a copy of the CMK key token stored on its local drive.
Always Encrypted and TDE
The same Security World can be used for Always Encrypted and TDE.
Install and configure
This installation must be performed on the on-premises client computer.
The nshield Security World software will be installed on this computer. This computer will also be made a client of the HSM.
Install the Security World software and create a Security World
-
Install the Security World software. For instructions, see the Installation Guide and the User Guide for the HSM.
-
Add the Security World utilities path C:\Program Files\nCipher\nfast\bin to the Windows system path.
-
Open the firewall port 9004 for the HSM connections.
-
Install the nshield Connect HSM locally, remotely, or remotely via the serial console. See the following nshield Support articles, and the Installation Guide for the HSM:
-
Open a command window and run the following to confirm that the HSM is
operational
.C:\Users\dbuser>enquiry Server: enquiry reply flags none enquiry reply level Six serial number 530E-02E0-D947 7724-8509-81E3 09AF-0BE9-53AA 9E10-03E0-D947 mode operational ... Module #1: enquiry reply flags none enquiry reply level Six serial number 530E-02E0-D947 mode operational ...
-
Create your Security World if one does not already exist, or copy an existing one. Follow your organization’s security policy for this.
-
Confirm that the Security World is
usable
.C:\Users\dbuser>nfkminfo World generation 2 state 0x37270008 Initialised Usable ... ... Module #1 generation 2 state 0x2 Usable ...
Install and register the CNG provider
-
Open a command window as administrator and type the following to put the HSM in
pre-initialization
mode. This operation takes about a minute to complete.C:\Windows\system32>enquiry -m 1 Module #1: enquiry reply flags none enquiry reply level Six serial number 530E-02E0-D947 mode operational ... C:\Windows\system32>nopclearfail -I -m 1 Module 1, command ClearUnitEx: OK C:\Windows\system32>enquiry -m 1 Module #1: enquiry reply flags none enquiry reply level Six serial number 530E-02E0-D947 mode pre-initialization ...
-
Select the Start button to access all applications. Look for the recently installed nshield utilities.
-
Double-click the CNG configuration wizard and run it as Administrator.
-
Select Next on the CNG Install welcome screen.
-
Select Next on the Enable HSM Pool Mode screen. Leave the Enable HSM Pool Mode for CNG Providers check box un-checked.
-
At the Security World screen, select:
-
Use the existing security world if you already have a Security World that you intend to use for Always Encrypted. The corresponding
world
andmodule_xxxx-xxxx-xxxx
files most be present in the%NFAST_KMDATA%\local
folder. Be prepared to present the quorum of Administrator cards. -
Create a new Security World if you do not currently have a Security World or would like to create a new Security World.
NoteFor the purposes of this integration guide we have chosen to use an existing Security World. For instructions on how to create and configure a new Security World, see the Installation Guide and User Guide for your HSM. Select Next .
-
-
The Set Module States pop-up shows the available HSM(s). Select the desired HSM. The state of the selected HMS should be
(pre-)initialisation
. Select Next . -
At the Module Programming Options screen, clear Enable this module as a remote target and select Next . It will take about a minute before the screen changes.
NotePlease be aware that this is not to be confused with the nshield Remote Administration utility. -
Insert the first Administrator Card in the HSM, enter the passphrase and select Next . Repeat this step for the other Administrator Cards as required.
Loading or creating the Security World takes about a minute.
-
Return the HSM to
Operational
mode. This operation takes about a minute to complete.C:\Windows\system32>enquiry -m 1 Module #1: enquiry reply flags none enquiry reply level Six serial number 530E-02E0-D947 mode initialization ... C:\Windows\system32>nopclearfail -O -m 1 Module 1, command ClearUnitEx: OK C:\Windows\system32>enquiry -m 1 Module #1: enquiry reply flags none enquiry reply level Six serial number 530E-02E0-D947 mode operational ...
The module state will change to
Usable
.Select Next .
-
Select Operator Card Set in the Key Protection Setup . Select Next .
Select Next .
-
Enter the OCS name, K of N values, and check Persistent and Usable remotely as show.
Select Next .
-
Insert a blank Operator Card in the HSM. On the Insert Next Card screen enter a name to for the OCS card and passphrase.
Select Next .
-
Select Next and Finish . The nshield CNG providers will now be installed and the key Storage Provider will be registered.
-
Open a command window as administrator and type the following to confirm the KSP has been successfully registered. Look for nCipher Security World Key Storage Provider .
C:\Windows\system32>cnglist.exe --list-providers Microsoft Key Protection Provider Microsoft Passport Key Storage Provider Microsoft Platform Crypto Provider Microsoft Primitive Provider Microsoft Smart Card Key Storage Provider Microsoft Software Key Storage Provider Microsoft SSL Protocol Provider Windows Client Key Protection Provider nCipher Primitive Provider nCipher Security World Key Storage Provider
-
Check the registry in
CNGRegistry
:HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Cryptography\Providers\nCipherSecurityWorldKeyStorageProvider
Install and configure SqlServer PowerShell module
-
Open a PowerShell session as Administrator and run:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 Install-PackageProvider Nuget –force –verbose
-
Update PowerShellGet:
Install-Module –Name PowerShellGet –force –verbose
-
Download and install the SqlServer module to configure Always Encrypted using Power Shell:
Install-Module -Name SqlServer -force -verbose -AllowClobber
NoteThe -AllowClobber
parameter allows you to import the specified commands if it exists in the current session. -
Once installed (if you are using PowerShell ISE refresh the Commands pane if you are using PowerShell open a new session), confirm the install by running:
Get-Module -list -Name SqlServer
-
You should see something similar to the output below:
Directory: C:\Program Files\WindowsPowerShell\Modules ModuleType Version Name ExportedCommands ---------- ------- ---- ---------------- Manifest 21.0.17152 SqlServer {Add-SqlColumnEncryptionKeyValue, Complete-SqlColumnMasterKeyRotatio…
Generate the encryption keys
Generate the Always Encrypted Column Master Key (CMK) protected by the nshield HMS
-
Launch PowerShell on the on-premises client computer as Administrator, and run the
Generate_CMK.ps1
script.$cngProviderName = "nCipher Security World Key Storage Provider" $cngAlgorithmName = "RSA" $cngKeySize = 2048 $cngKeyName = "AECMK" $cngProvider = New-Object System.Security.Cryptography.CngProvider($cngProviderName) $cngKeyParams = New-Object System.Security.Cryptography.CngKeyCreationParameters $cngKeyParams.provider = $cngProvider $cngKeyParams.KeyCreationOptions = [System.Security.Cryptography.CngKeyCreationOptions]::OverwriteExistingKey $keySizeProperty = New-Object System.Security.Cryptography.CngProperty("Length", [System.BitConverter]::GetBytes($cngKeySize), [System.Security.Cryptography.CngPropertyOptions]::None); $cngKeyParams.Parameters.Add($keySizeProperty) $cngAlgorithm = New-Object System.Security.Cryptography.CngAlgorithm($cngAlgorithmName) $cngKey = [System.Security.Cryptography.CngKey]::Create($cngAlgorithm, $cngKeyName, $cngKeyParams)
The command line is
> PowerShell -ExecutionPolicy Bypass -File Generate_CMK.ps1
-
The following pop-window should appear. Select Next .
-
Select the Operator Card Set Protection . Insert the OCS card in the HSM and select Next .
-
Select Next .
-
Select the HSM and select Finish .
-
Enter the OCS passphrase and select Next .
-
Select Finish .
A 2048-bit RSA key pair, called
AECMK
, has been generated. The key is encrypted in the HSM, and then pushed to the requesting On-Premise Client server, where it is stored as an Application Key Token in the%NFAST_KMDATA%\local folder
(:\ProgramData\nCipher\Key Management Data\local
). -
-
Verify the new key as follows on a command window.
C:\Users\dbuser>nfkminfo -k Key list - 1 keys AppName caping Ident s-1-5-21-2556418611-2173580918-1658130183-1001--7b7eb65c095c556e5da059480e6ca2ed512dacc1
-
Display the information about the key by copy-pasting the key name above as follows.
C:\Users\dbuser>nfkminfo -k caping s-1-5-21-2556418611-2173580918-1658130183-1001--7b7eb65c095c556e5da059480e6ca2ed512dacc1 Key AppName caping Ident s-1-5-21-2556418611-2173580918-1658130183-1001--7b7eb65c095c556e5da059480e6ca2ed512dacc1 BlobKA length 1128 BlobPubKA length 484 BlobRecoveryKA length 1496 name "AECMK" hash 76071834044810539e7354f468cc2cae61a448da recovery Enabled protection CardSet other flags PublicKey !SEEAppKey !NVMemBlob +0x0 cardset 0e8d19801b25d774c3b2bab5a643ec7c20a5255d gentime 2021-03-30 19:18:14 SEE integrity key NONE BlobKA format 6 Token other flags 0x0 hkm 2a2e6b22ad6a72673473511d91304efd2f76e197 hkt 0e8d19801b25d774c3b2bab5a643ec7c20a5255d hkr none BlobRecoveryKA format 9 UserKey other flags 0x0 hkm none hkt none hkr fc4cbd1a6e88c08dd35912d0aecabf47ff1e0c2a BlobPubKA format 5 Module other flags 0x0 hkm c2be99fe1c77f1b75d48e2fd2df8dffc0c969bcb hkt none hkr none Extra entry #1 typecode 0x10000 65536 length 60 Not a blob
Generate My Column Master Key (MyCMK) and My Column Encryption Key (MyCEK) with SSMS
This key will encrypt all subsequent Column Encryption keys (CEKs) in your database.
-
Launch Microsoft SQL Server Management Studio on the on-premises client computer.
-
As the
dbuser
user, connect to the database on the SQL server on the hosting site. -
Using Object Explorer , select the Security directory under the desired Database. Select Always Encrypted Keys to expand it, then select New Column Master Key .
-
Enter the following information on the Column Master Keys pop-up window, then select Next
-
Enter a name, for example
MyCMK
. -
Select Key Storage Provider (CNG) from the Key store drop-down list. This will then present the option to Select a provider .
-
Select nCipher Security World Key Storage Provider from the drop-down list.
-
The
AECMK
key created in an earlier step appears in Name . Select OK to create a new key using the nshield HSM and CNG KSP.
-
-
Notice the newly created MyCMK in the database Security\Always Encrypted Keys\Column Master Keys .
-
Using Object Explorer , select the Security directory under the desired Database. Select Always Encrypted Keys to expand it, then select New Column MEncryption Key .
-
Enter Name and select OK .
-
Present the OCS and select Next .
-
Select the HSM and select Finish .
-
Enter the passphrase and select Next .
-
Select Finish after the OCS card reading completes.
-
Notice the newly created MyCEK in the database Security\Always Encrypted Keys\Column Encryption Keys .
Generate My Column Master Key (MyCMK) and My Column Encryption Key (MyCEK) with PowerShell
-
Delete MyCEK and MyCMK created above by right-clicking each key and selecting Delete .
-
Launch PowerShell on the on-premises client computer and run the
Generate_MyCMK_and_MyCEK.ps1
script.# Import the SqlServer module. Import-Module SqlServer # Connect to database. $ConnectionString = "Data Source=<DB_Server_IP>,49170;Initial Catalog=TestDatabase;User ID=dbuser;Password=<dbuser_Password>;MultipleActiveResultSets=False;Connect Timeout=30;Encrypt=True;TrustServerCertificate=True;Packet Size=4096;Application Name=`"Microsoft SQL Server Management Studio`"" $Database = Get-SqlDatabase -ConnectionString $ConnectionString # Create a SqlColumnMasterKeySettings object for your column master key. $cmkSettings = New-SqlCngColumnMasterKeySettings -CngProviderName "nCipher Security World Key Storage Provider" -KeyName "AECMK" # Create column master key metadata in the database. New-SqlColumnMasterKey -Name "MyCMK" -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. New-SqlColumnEncryptionKey -Name "MyCKE" -InputObject $Database -ColumnMasterKey "MyCMK"
The command line is
> PowerShell -ExecutionPolicy Bypass -File Generate_MyCMK_and_MyCEK.ps1 Name ----- MyCMK MyCEK
-
Present the OCS, select the HSM, and enter the passphrase.
-
Notice the newly created MyCMK in the database Security\Always Encrypted Keys\Column Master Keys .
Encrypt or decrypt a column with SSMS
Encrypt a column
-
Launch Microsoft SQL Server Management Studio on the on-premises client. Connect with the dbuser account to the database on the SQL server.
-
Right-click the database, TestDatabase , and select Tasks > Encrypt Columns .
-
Select Next on the Introduction screen.
-
Select the column and encryption type on the Column Selection screen and select Next .
-
Select MyCMK on the Master key Configuration window. Select Next .
-
Select Proceed to finish now radio button and select Next .
-
Verify the configuration choices on the Summary screen. Select Next .
-
Present the OCS that is protecting the CMK and select Finish .
-
Select the HSM and select Next .
-
Enter the passphrase, and select Next .
-
Select Finish .
-
Select Close .
The column has been encrypted in the SQL server, but it shows as clear text on the Microsoft SQL Server Management Studio GUI on the on-premises client. This is because Always Encrypted is performing the decryption at the on-premises client site.
View an encrypted column
Reconnect to the SQL server with Always Encrypted disabled to view the encrypted data stored in the SQL server.
-
Connect to the SQL server from the on-premises client, but with the Enable Always Encrypted unchecked.
-
Right-click dbo.Table , and select Select Top 1000 Rows . The column that was chosen for encryption now appears as ciphertext, that is, as an encrypted value.
-
Reconnect to the SQL server from the on-premises client, but with the Enable Always Encrypted checked. Be prepared to provide the OCS. Select Next .
-
Select the HSM. Select Finish .
-
Enter the passphrase. Select Next .
-
Select Finish .
-
Right-click dbo.Table , and select Select Top 1000 Rows . The column that was chosen for encryption is now being decrypted by Always Encrypted with the key protected by the nCipher HSM.
-
Select Finish .
Remove column encryption
-
Right-click the required database and in the Tasks menu and select Encrypt Columns .
-
Select Next on the Introduction screen.
-
Select Plaintext from the drop down list in the Encryption Type and select Next .
-
Select Next on the Master Key Configuration window.
-
Select the Proceed to finish now radio button and select Next .
-
Select Finish on the Summary window.
-
Present the OCS and select Next .
-
Select the HSM and select Finish .
-
Enter the passphrase and select Next .
-
Select Finish upon Card reading complete .
The column has been decrypted in the SQL server. To view the plain text data stored SQL server, reconnect to the server with Always Encrypted disabled, see View an encrypted column .
Encrypt or decrypt a column with PowerShell
Encrypt a column
-
Launch PowerShell on the on-premises client computer and run the following script named Encrypt_Column_Named_Password.ps1.
# Import the SqlServer module. Import-Module SqlServer # Connect to database. $ConnectionString = "Data Source=<DB_Server_IP>,49170;Initial Catalog=TestDatabase;User ID=dbuser;Password=<dbuser_Password>;MultipleActiveResultSets=False;Connect Timeout=30;Encrypt=True;TrustServerCertificate=True;Packet Size=4096;Application Name=`"Microsoft SQL Server Management Studio`"" $Database = Get-SqlDatabase -ConnectionString $ConnectionString # Change encryption schema. $encryptionChanges = @() # Add changes for table [dbo].[TestTable] $encryptionChanges += New-SqlColumnEncryptionSettings -ColumnName dbo.TestTable.Password -EncryptionType Randomized -EncryptionKey "MyCEK" Set-SqlColumnEncryption -ColumnEncryptionSettings $encryptionChanges -InputObject $Database
The command line is
> PowerShell -ExecutionPolicy Bypass -File Encrypt_Column_Named_Password.ps1
-
Present the OCS, select the HSM, and enter the passphrase.
The column has been encrypted in the SQL server, but it shows as clear text on the Microsoft SQL Server Management Studio screen on the on-premises client. This is because Always Encrypted is performing the decryption at the on-premises client site.
View an encrypted column
Reconnect to the SQL server with Always Encrypted disabled to view the encrypted data stored in the SQL server. See encrypt-decrypt-column-with-ssms.html .
Remove column encryption
-
Launch PowerShell on the on-premises client computer and run the following script named Decrypt_Column_Named_Password.ps1.
# Import the SqlServer module. Import-Module SqlServer # Connect to database. $ConnectionString = "Data Source=<DB_Server_IP>,49170;Initial Catalog=TestDatabase;User ID=dbuser;Password=<dbuser_Password>;MultipleActiveResultSets=False;Connect Timeout=30;Encrypt=True;TrustServerCertificate=True;Packet Size=4096;Application Name=`"Microsoft SQL Server Management Studio`"" $Database = Get-SqlDatabase -ConnectionString $ConnectionString # Change encryption schema $encryptionChanges = @() # Add changes for table [dbo].[TestTable] $encryptionChanges += New-SqlColumnEncryptionSettings -ColumnName dbo.TestTable.Password -EncryptionType Plaintext Set-SqlColumnEncryption -ColumnEncryptionSettings $encryptionChanges -InputObject $Database
The command line is
> PowerShell -ExecutionPolicy Bypass -File Decrypt_Column_Named_Password.ps1
-
Present the OCS, select the HSM, and enter the passphrase.
The column has been decrypted in the SQL server. To view the plain text data stored SQL server, reconnect to the server with Always Encrypted disabled, see encrypt-decrypt-column-with-ssms.html .
Supported PowerShell SqlServer cmdlets
PowerShell cmdlet | Description |
---|---|
|
Adds a new encrypted value for an existing column encryption key object in the database. |
|
Completes the rotation of a column master key. |
|
Returns all column encryption key objects defined in the database, or returns one column encryption key object with the specified name. |
|
Returns the column master key objects defined in the database, or returns one column master key object with the specified name. |
|
Initiates the rotation of a column master key. |
|
Creates a
|
|
Creates a
|
|
Creates a new column encryption key object in the database. |
|
Produces an encrypted value of a column encryption key. |
|
Creates a new
|
|
Creates a new column master key object in the database. |
|
Creates a
|
|
Removes the column encryption key object from the database. |
|
Removes an encrypted value from an existing column encryption key object in the database. |
|
Removes the column master key object from the database. |
|
Encrypts, decrypts or re-encrypts specified columns in the database. |
The full list of cmdlets and additions to the
SqlServer
module can be found at
https://docs.microsoft.com/en-us/powershell/module/sqlserver/?view=sqlserver-ps
.
-
Integration GuideMicrosoft SQL Server 2019 Always Encrypted nShield HSM Integration Guide
-
ProductsnShield Connect
-
ProductsnShield as a Service