Moving TDE Certificates Without Files

How I Found This

I’ve been automating a lot of the tedious and routine migration tasks for our team for the past few months. Leveraging tools like PowerShell and Azure DevOps Pipelines to consistently deliver results and accomplish the majority of the heavy lifting relating to DBA toil allows us to focus on other tasks and initiatives. While improving my pipeline process, I noticed that the TDE cert migration portion was unreliable and failing often. The specific step backed up the .cer and .pvk file to a network share and attempted to assign file share permissions, and restore the cert with its pvk to the destination server. Connectivity to the share was unreliable, and trying to play permission assignments to a share in flight proved to be more challenging than I was hoping. With that, I sought out a new way to skin the cat. Turns out, you can leverage the certificate’s binary metadata to migrate the TDE cert to another server without having to worry about physical .cer and .pvk files to make that possible. You don’t need a pipeline for this to be useful. Here’s what it looks like by hand.

The Lab Setup

I’m going to cheat here a little bit. I have a two replica AG running SQL Server 2025 Developer. I’m just going to create a cert on the primary and move it to the secondary replica to demonstrate the process. I’ll start with the “old way” of migrating a TDE cert and then show the “new way”.

SQL Server Management Studio showing a query to list encrypted databases and a results table of database names.
Analyzing existing certificate inventory

As you can see, we have no TDE infrastructure set up yet. By the way, feel free to checkout sp_encryptdatabase stored proc and how to use it in this blog post. I’m going to create a certificate called “TheOldWay” on SQLLAB-SQL3.

SQL Server Management Studio displaying a query to encrypt the TheOldWay database and a list of unencrypted databases.
Creating the certificate

When I run the report we can see that we now have a TDE cert on SQL3. It’s not encrypting anything yet.

The Old Way: Backing Up To Files

Now, If I wanted to “migrate” this certificate to SQLLAB-SQL4, I would approach it by backing up the certificate on SQL3, moving its .cer and .pvk file to SQL4 and restoring it:

SQL Server code exporting the TheOldWay certificate and private key to files with password encryption; execution completed successfully.
Backing up cert on SQL3
Windows File Explorer showing two certificate files, TheOldWay and TheOldWay.pvk, in the C:\temp folder.
Showing the physical .cer and .pvk files on the C:\ drive on SQL3

Now, I don’t know about you, but I’m guilty of it so I’ll call myself out now. I have the best intentions when it comes to handling these files. Put them in a secure location, don’t keep them on the server, etc. Sometimes things happen and you are looking around one day and say “What the heck?! Why are these here?”.

Windows File Explorer showing TheOldWay security certificate and TheOldWay.pvk private key files in a network temp folder
Copying .cer and .pvk files to SQL4

From there, I’d copy the files to SQL4 so I can restore them on that server.

SQL Server query creating a certificate from a file with a private key protected by a password
Restoring TheOldWay certificate to SQL4
SQL Server Management Studio showing a query to list encrypted databases and results including TheOldWay database.
Validating that the certificate restored to SQL4 successfully.

Now remember, I still have the .cer and .pvk files I showed earlier sitting on the C:\ drive of SQL3 and SQL4. This is something that I’d have to handle by putting them in some shared, secure, and accessible location. But having to maintain physical files is another touchpoint that has to be taken into account. In my experience, depending on where you’re trying to do this, some environments won’t allow copy/ paste of these file types from one server to another due to permissions limitations. Not to mention issues with share connectivity and access depending on how your environment is set up.

The Alternative: Binary Values

In this approach, I’m going to migrate a cert called “TheNewWay” by use of the binary values.

SQL Server Management Studio showing sp_EncryptDatabase results and a list of unencrypted databases.
Creating TheNewWay certificate on SQL3
SQL Server query displaying certificate, private key, and thumbprint values for certificate 'TheNewWay'.
Reading and exporting certificate and private key binary data.

To clarify, this isn’t some new SQL Server feature. This has been around since SQL Server 2012. I just never needed it until the file share kept breaking my migrations. By the way, I’ll put all of the binary code snippets at the bottom of the post.

CERTENCODED() gives you the public cert as a binary value. THese are the same bytes that would end up in your .cer file but you’re getting them back as a query result.

CERTPRIVATEKEY() gives you the private key as a binary value which are the same bytes as your .pvk file. You pass it a password and it hands back a copy of the key locked with that password. The certificate on the server isn’t touched and only the copy you’re pulling out gets locked.

The thumbprint is for validation on SQL4 (or your target server) to make sure the thumbprint matches. Very important.

To dissect this a bit, the password for your private key goes in the CERTPRIVATEKEY() function. This is the equivalent to “encrypted by password”. The query shown above only reads out the certificate and its related objects. Nothing is backed up anywhere, just basically shown to you in plaintext. You’ll want to copy this information out to a secure location like a password vault.

Ok, now let’s see how the restore looks on SQL4:

SQL Server query editor showing a CREATE CERTIFICATE statement with binary certificate data and password encryption.
Restoring certificate and private key with binary values.

Breaking this down, that first binary value is going to be the value from the CertHex column and the second binary value is going to be from the KeyHex column. Notice no single tics around the binary values. You’re going to want to provide the encryption password you did in the prior step.

Proving It Worked

If I run the report proc again, we can see the cert existing on SQL4 now with the matching thumbprint. This is vital because if thumbprints don’t match you’re leveraging a useless cert:

SQL query results showing certificate details, encryption type, thumbprints, and issuer names in a database management tool
Thumbprint and certificate on SQL4
SQL Server query results showing two database master key certificates, with a red arrow highlighting a thumbprint value.
Thumbprint and certificate on SQL3

Old Way vs. The New Way

When using the traditional way to migrate certificates, you’re dependent on some sort of folder access, having to manage physical files on disk, and can’t complete the process over a SQL connection. With the Binary approach there is no need for folder access, no physical files to manage, and works over SQL connections end to end.

Is It More Secure?

Is it more secure? Well, kind of. In my opinion there’s tradeoffs to using this method. When using files as your backup / transfer method, you’re leveraging the existing standard way to complete this process. You’re left with physical files you have to manage and keep track of, or you have to take new backups of your certificates (which you should be doing anyways). With leveraging just the binaries and exporting the metadata via queries, you are exporting a “description” of what the certificate and private key looks like so SQL can “replay” the shape to create it properly. With files, the .pvk and its password usually live in different places. With this method, one vault entry holds both the key and the password needed to rebuild the certificate. You can store this export in a password vault and have the ability to recover the certificates used for TDE in a similar way. Two different approaches and you can decide if this is worth it for you or not.

Gotchas

Good news is that the two methods mix freely. So, a certificate restored from a binary can be backed up to files later and the other way round. Backing up to files is still fine and still preferred for a long term regular solution. This is an alternative and not intended to be a complete replacement.

If the private key has its own password, pass it as the third argument to CERTPRIVATEKEY or you’ll get NULL back. Also, no private key on the source means there’s nothing to move by any method. Lastly, the export password is not your master key password. That is something that is set up prior to any TDE cert work.

Why Not dbatools?

dbatools has a very useful cmdlet called Copy-DbaDbCertificate. I’ve used it many times. It requires a shared path for the cert to be written to and read from (source to destination) which is exactly what we’re trying to circumvent in this demo.

TL;DR

  • Backing up your certificates used for TDE to files works fine, but remembering and managing physical files can be a critical forgotten and neglected task.
  • CERTENCODED + CERTPRIVATEKEY out, FROM BINARY to restore it, then compare the thumbprints.
  • Make sure to save the strings safely and clean up anything with the key in it.

Code Snippets

Backup a certificate

BACKUP CERTIFICATE TDECertificate
TO FILE = 'C:\temp\TDECertificate.cer'
WITH PRIVATE KEY (file='C:\temp\TDECertificate.pvk',
ENCRYPTION BY PASSWORD='InsertStrongPasswordHere')

Restore a certificate

USE MASTER
GO
CREATE CERTIFICATE TDECertificate
FROM FILE = 'C:\Temp\TDECertificate.cer'
WITH PRIVATE KEY (FILE = 'C:\temp\TDECertificate.pvk',
DECRYPTION BY PASSWORD = 'InsertBackupCertPasswordHere' );

Binary Method Export

USE master;
DECLARE @Password nvarchar(128) = N'<export password>';
SELECT
CONVERT(varchar(max), CERTENCODED(CERT_ID('TDECertificate')), 1) AS CertHex,
CONVERT(varchar(max), CERTPRIVATEKEY(CERT_ID('TDECertificate'), @Password), 1) AS KeyHex,
CONVERT(varchar(max), thumbprint, 1) AS Thumbprint
FROM sys.certificates
WHERE name = 'TDECertificate';

Binary Method Import

USE master;
CREATE CERTIFICATE [TDECertificate]
FROM BINARY = <CertHex>
WITH PRIVATE KEY (
BINARY = <KeyHex>,
DECRYPTION BY PASSWORD = '<export password>'
);

Leave a Reply