If you have just received your certificate and struggle to find the private key that matches it, this article can help you get things sorted out.
First of all, let’s go through some basics. The whole concept of Public Key Infrastructure (PKI) is based on the idea of having two unique keys - the public key is encrypted within the SSL certificate, and the private key is generated on the server and kept secret. All the information sent from the website visitor to the server is encrypted with the public key and gets decrypted on the server side with the private key. To keep the message private and inaccessible for a third person, it is necessary to use a unique and unforgeable key pair. Basically, one key will not work without another.
Where did I get this key?
The private key gets generated along with your Certificate Signing Request (CSR). The CSR is submitted to the certificate authority right after you activate your certificate, while the private key must be kept safe and secret on your server or device. Later on this key is used for installation of your certificate.
Can I just generate a new private key for my certificate if I lose the old one?
You can certainly generate a new private key and CSR, however, the key will not match the certificate already issued. To make this work, you will need to use the new CSR code/private key pair to reissue the certificate.
What does the key look like?
It looks like an array of encoded data, starting and ending with headers, such as -----BEGIN RSA PRIVATE KEY----- and -----END RSA PRIVATE KEY-----.
You may not get to see this code when generating your CSR. Usually it gets generated in the background, simultaneously with the CSR, and is saved on your server. It also fills the corresponding field automatically during installation of the certificate in some control panels, such as cPanel.
However, there are numerous cases that vary depending on your web server, control panel, or any other tools used for CSR generation. See below for examples and hints of how to find your private key.
Finding your private key on different servers or control panels
Linux-based Operating Systems and web-servers (Apache, Nginx, LightHttpd)
Normally, the CSR/RSA private key pairs on Linux-based operating systems are generated using the OpenSSL cryptographic engine and saved as files with “.key” or “.pem” extensions on the server. Although, no specific extensions are mandatory for text files in Linux, so the key file may have any name and extension, or no extension at all.
If you remember the whole name of the key file, or at least a part of it, you can use the following command in your console to try and find the file and its directory:
sudo find / -name “filename.key”
Slash symbol in this command implies that the search begins from the root directory of the server - this way you have more chances to find the actual file. The name of the file in the parenthesis should be the part of the name that you remember. To search for the file by extension, you can enter the name in the following manner: “*.key” - this will show you all the files with .key extension on your server.
HINT: Often the name of the file corresponds to the domain name it was generated for, e.g. “domain_tld.key”, or “domain.tld.pem”, etc.
Another useful command lets you search files by their content:
grep -r --exclude-dir=log --exclude-dir=ssh --exclude=*history -I -l -e '-----BEGIN PRIVATE*' -e '-----BEGIN RSA*' -e ‘-----BEGIN EC*’ [search_start_folder] 2> /dev/null
This command will return the absolute path to the private key file, if it is present on your server.
Windows operating systems (IIS, Exchange, Small Business server)
Windows servers do not let you view the private key in plain text format. When you import your certificate via MMC or IIS, the corresponding private key is bound to it automatically, if the CSR/Key pair has been generated on the same server.
However, if it is necessary to get the private key out and install the certificate on a different server, you can export the key in a password protected PFX (PKCS#12) file. In order to do that, you should open MMC Certificates snap-in following these steps:
Win+R > mmc.exe > OK > File > Add/Remove Snap-in > Certificates > Add > Computer account > Next > Local computer > Finish > OK
Then, go to Certificate Enrollment Requests > Certificates (if you have not completed the certificate request yet) or Personal > Certificates (if you already did), right-click the certificate, then All Tasks > Export. Export wizard will open, and you will be able to export the certificate following the instructions in it. You can find more detailed instructions here.
Once done, you will have a .pfx file containing your certificate, CA-bundle and private key. To get the key out, you can use this tool. Choose “PKCS12 to PEM” option, upload the file and enter the password you set for it.
Mac OS X
The Keychain tool in the Server application of Mac OS X does not allow you to get the private key via the graphic user interface. You can do that using Terminal, by opening /etc/certificates/ directory and opening the file. Its name should be something like “*.key.pem”. The terminal commands would be the following: cd /etc/certificates/ , then ls , and sudo nano test.key.pem to open the file you find.
If your Tomcat SSL connector is configured in JSSE style, the private key must be in a password-protected keystore file, that has .jks or .keystore extension. This file, unlike in most other instances, is created before the CSR. To get the private key out of it, it is necessary to convert the keystore into a PFX file with the following command:
keytool -importkeystore -srckeystore keystore.jks -destkeystore keystore.p12 -deststoretype PKCS12 -srcalias <jkskeyalias> -srcstorepass <jkspassword> -srckeypass <keypassword> -deststorepass <newp12password> -destkeypass <newkeypassword>
“Keystore.jks” should be replaced with your actual keystore name, “keystore.p12” will be the name of the PKCS12 file you are going receive. <jkskeyalias>, <jkspassword> and <keypassword> are the alias, the key and keystore passwords that were entered during keystore generation. <jkskeyalias>, <jkspassword> and <keypassword> should be replaced with your JKS file alias, its password, and private key password correspondingly. <newp12password> and <newkeypassword> are to be replaced with the passwords you wish to set for your new PKCS12 file and the private key.
After the PKCS12 file is generated, you can convert it to a PEM file with separated CRT, CA-Bundle and KEY files using this tool or with the following command in terminal:
openssl pkcs12 -in keystore.p12 -nocerts -nodes -out private.key
“Private.key” can be replaced with any name you wish your key file to have.
In cPanel, your private key can be retrieved in two ways: through SSL/TLS Manager menu, or File manager.
- Using SSL/TLS Manager
On cPanel main page, click “SSL/TLS Manager”>>”Private keys”. There you will see all the private keys ever generated in your cPanel. To view the code of the key, you can click “View & Edit”. If there are several keys in that menu, you can copy each of them and match with your certificate code using this tool.
- Using File manager
Click “File manager” on the homepage of your cPanel, then find the folder named “ssl” in the folder tree to the left (see screenshot). That folder will have another one named “key” inside. “Key” folder also contains all the keys generated in your cPanel.
WHM stores your private keys and CSR codes in the SSL Storage manager menu. You can find it if you click “SSL/TLS” on the home page and then click “SSL Storage manager”. To view the private key, you can click the magnifier icon next to the key you need in the “Key” column.
If you follow “Domains”>> your domain >> SSL/TLS certificates, you should see a page like the one shown below. The key icon with the message “Private key part supplied” implies that there is a matching key on your server. To get it in plain text format, you can click the name of the entry and scroll down the page until you see the key code. Also, you can click the green arrow icon to the right. This will download a PEM file, containing your private key, certificate and CA-Bundle files, if they had been imported to the server previously. The files can be opened in any text editor, such as Notepad.
Synology NAS DSM
When generating a CSR in Synology DSM, the private key is provided to you in a ZIP file on the last step. The key code is contained within a server.key file, that can be opened with a text editor on your device.
Webmin works as a graphic user interface (GUI) on top of the command-line interface. There is also a file manager called Filemin, that you can use to browse the server file system and find your private key file. Alternatively, you can go to “Others”>>”Command shell” and run the find or grep command, which you can find in the “Linux operating systems” paragraph.
On VestaCP your private key must be saved during CSR generation, as it will not be available anywhere via the graphic user interface.
Although, there is a chance to retrieve your private key via SSH. It is saved as a temporary file in the “/tmp” folder. The path to the file may look the following way: “/tmp/tmp.npAnkmWFcu/domain.com.key” . Please keep in mind that the files in the /tmp directory are deleted every time your server is rebooted.
To get the exact path to your key file, you can try using the same old Linux command:
find /tmp -type f -name “domain.com.key”
“domain.com” should be replaced with the actual domain name you generated the CSR for.
The “grep” command would work as well in this case:
grep -r -I -l -e ‘-----BEGIN PRIVATE*’ -e ‘-----BEGIN RSA*’ /tmp 2> /dev/null
In the up-to-date versions of DirectAdmin, your private key is saved on the server and is fetched in the “Paste a pre-generated certificate and key” field during installation.
If that section is empty, that may mean that the CSR and key were generated elsewhere, or the key was not saved in DirectAdmin due to a glitch. If it was due to a glitch, you can try retrieving the key via SSH. Usually it is saved in this directory:
/usr/local/directadmin/data/users/<user>/domains/<domain>.key, where <user> and <domain> are your DirectAdmin username and the domain in question correspondingly.
Webuzo SSL management section can be found on the home page. If you click the “Private Keys” button, you will see the list of all the private keys generated in Webuzo. To see the key code, you can click the pencil icon to the right under the “Option” column as shown in the screenshot below:
To sum up - ways to find your private key fully depend on the interface of the web server where you generate the CSR. If the methods described above did not help you find the private key for your certificate, the only solution would be to generate a new CSR/private key pair and reissue your certificate, and to make sure that the key is saved on your server/local computer this time.