Bei einer Servermigration ist das Problem aufgetreten, dass ein Zertifikat nicht exportiert werden konnte. Bei der Migration sollte nur das Betriebssystem gegen eine aktuelle Version ausgetauscht werden, das Zertifikat sollte, wenn möglich weiter genutzt werden. In dem konkreten Fall handelte es sich um ein Wildcard Zertifikat, bei dem allerdings beim Import die Einstellung „Privater Schlüssel als exportierbar markieren (Mark this key as exportable)“ nicht gesetzt wurde.
The certificate with the private key can therefore not simply be exported via MMC and imported on the new server. In order to be able to use the certificate on the new server anyway, you have to use a little trickery. I have simulated the procedure in a test environment.
In meiner Testumgebung habe ich ein Zertifikat, welches nicht mit dem Flag „Mark this key as exportable“ importiert wurde. In diesem Fall handelt es sich um das Zertifikat mit dem Common Name „DC1.ad.frankysweblab.de“ (als Beispiel).
Source server (export certificate and key)
In order for the certificate including the private key to be transferred to another server, some information must be collected. First of all, the thumbprint of the certificate that is to be transferred from the source server to the target server is required. The following command can be used to display the thumbprint and CNs on the source server:
Get-ChildItem -Path Cert:\LocalMachine\my
Wie bereits erwähnt, lässt sich der private Schlüssel des Zertifikats mit dem CN „DC1.ad.frankysweblab.de“ nicht exportieren.
In order to transfer the certificate to the target server, the name of the private key in the file system is required. The name can be found using the thumbprint of the certificate:
(Get-Item Cert:\LocalMachine\My\E8254B658C645C2B57DD0103CA65BE13CC9D5935).PrivateKey.CspKeyContainerInfo.UniqueKeyContainerName
Dies ist nun der Dateiname des privaten Schlüssels welcher unter dem Pfad „C:\Users\All Users\Microsoft\Crypto\RSA\MachineKeys“ gespeichert wird. Damit es etwas einfacher ist, wird der private Schlüssel nach C:\ kopiert:
Copy-Item "C:\Users\All Users\Microsoft\Crypto\RSA\MachineKeys\f97266d1ef98e6a0ceb271258b70163a_6f560699-8cc6-4aeb-bc83-42d42adf7863" c:\
Now the certificate must be exported without the private key, this can be done via MMC. The .CER file can also be saved under C:\:
There should now be 2 files under c:\, one with the exported private key and one containing the certificate:
Target server (import)
Both files must now be copied to the target server. The certificate (without the private key) can be imported directly into the computer's certificate store:
The certificate must be imported to the same storage location (Personal Certificates) so that the certificate and private key can be reunited on the target server. The certificate has now been imported, but the private key is still missing:
Damit der private Schlüssel auf dem Zielserver importiert werden kann, muss die Datei mit dem kryptischen Namen umbenannt werden. Der zweite Teil des Dateinamens ist die GUID des Quellservers (die Zeichenfolge nach dem Unterstrich „_“), diese GUID muss nun mit GUID des Zielservers ersetzt werden. Dazu wird jetzt die GUID des Zielservers benötigt, diese kann mit folgendem Befehl auf dem Zielserver abgefragt werden:
Get-ItemPropertyValue HKLM:\SOFTWARE\Microsoft\Cryptography\ -Name MachineGuid
To make it a little clearer, I have left the original file and the renamed file in the folder, as you can see, the character string after the underscore has been replaced with the GUID of the target server:
Die so umbenannte Datei muss nun wieder nach „C:\Users\All Users\Microsoft\Crypto\RSA\MachineKeys“ auf dem Zielserver kopiert werden, eine PowerShell mit Admin Rechten reicht hier aus:
Move-Item C:\f97266d1ef98e6a0ceb271258b70163a_39428449-e697-4b7f-b1b8-49ca15e421a4 'C:\Users\All Users\Microsoft\Crypto\RSA\MachineKeys\'
The thumbprint of the certificate is now required again. The certificate and private key can be reassembled on the target server using the thumbprint of the certificate:
certutil.exe -repairstore my E8254B658C645C2B57DD0103CA65BE13CC9D5935
Done. The MMC now also shows that the private key for the certificate is available:
Incidentally, the private key is protected against export on the target server.