PROBLEM SOLVING ON UNIX/LINUX SYSTEMS
1/ CONVERT LETSENCRYPT [ .PEM] FILE TO [.CRT] and [.KEY]
# /etc/letsencrypt/live/
cert.pem 2,719 2018.06.22 11:51p -a—
chain.pem 1,647 2018.06.22 11:51p -a—
fullchain.pem 4,366 2018.06.22 11:51p -a—
fullchain.pem.crt 4,366 2018.06.22 11:51p -a—
privkey.pem 3,272 2018.06.22 11:51p -a—
privkey.pem.key 3,272 2018.06.22 11:51p -a—
README 968 2018.06.23 12:03a -a—
The are just different “extensions” but are all essentially text files.
+your key file (mysite.com.key) will be privkey.pem
+your cert file (mysite.com.crt) will be cert.pem
+your ca file ( AlphaSSL.crt in your example) will be chain.pem or fullchain.pem ( depending exactly what you need )
2/ LETSENCRYPT TO [.PEM] FILE FOR HAPROXY
2.1/ combined_file color: red;»>$< domain >.pem »
2.2/ cat /etc/letsencrypt/live/$ domain / fullchain.pem /etc/letsencrypt/live/$ domain / privkey.pem > $combined_file
3/ BONUS
cd /etc/haproxy/certs/
DOMAIN_NAME=mgiay.com
PEM_FILE=$DOMAIN_NAME.pem
CRT_FILE=$DOMAIN_NAME.crt
KEY_FILE=$DOMAIN_NAME.key
openssl x509 -outform der -in $PEM_FILE -out $CRT_FILE
openssl pkey -in $PEM_FILE -out $KEY_FILE
#Convert PEM to PFX:
openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.crt -certfile CACert.crt
How to get .crt and .key from cert.pem and key.pem
I need to get them into a crt and key format for use on an nginx server.
openssl rsa -outform der -in key.pem -out key.key
openssl x509 -outform der -in cert.pem -out cert.crt
but get the following error when starting up nginx:
# service nginx restart Performing sanity check on nginx configuration: nginx: [emerg] cannot load certificate "/etc/ssl/nginx/cert.crt": PEM_read_bio_X509_AUX() failed (SSL: error:0906D06C:PEM routines:PEM_read_bio:no start line:Expecting: TRUSTED CERTIFICATE) nginx: configuration file /usr/local/etc/nginx/nginx.conf test failed
Extensions in file names do not count. PEM is an encoding format, it can be either a key or one (or more) certificates. You can rename cert.pem to whatever.crt and key.pem to whatever.key and things will work, no need to convert just rename the files if you want. There is no crt and key format. You have either binary (called DER) or Base64-encoded (PEM). BTW your question is offtopic here as not related to programming.
That did it, thanks. I had assumed I would needed to have formatted the keys differently. If you make your comment an answer I will mark it accepted. There are many cert/ ssl questions on this site so I thought it would be fine.
2 Answers 2
The extension .pem indicates that the file format is PEM (Privacy-Enhanced Mail). However, the extension does not tell anything about the content of the file. The content may be a certificate, a private key, a public key, or something else.
The extension .crt indicates that the content of the file is a certificate. However, the extension does not tell anything about the file format. The file format may be PEM, DER (Distinguished Encoding Rules) or something else. If the file is text and contains ——BEGIN CERTIFICATE—— , the file format is PEM. On the other hand, if the file is binary, it is highly likely that the file format is DER.
The extension .key indicates that the content of the file is a private key. However, the extension does not tell anything about the file format. The file format may be PEM, DER or something else. If the file is text and contains ——BEGIN PRIVATE KEY—— (or something similar), the file format is PEM. On the other hand, if the file is binary, it is highly likely that the file format is DER.
Diagrams below from «Illustrated X.509 Certificate» illustrate relationship among ASN.1 (X.680), DER (X.690), BASE64 (RFC 4648) and PEM (RFC 7468).
Both ssl_certificate and ssl_certificate_key of ngx_http_ssl_module expect that the file format is PEM as the reference document says. Therefore, you don’t have to change the file format of your cert.pem and key.pem because their file extension .pem indicates that their file format is already PEM. Just write like below in your Nginx configuration file.
ssl_certificate /path/to/cert.pem; ssl_certificate_key /path/to/key.pem;
If you prefer .crt and .key extensions, just rename them like below.
$ mv cert.pem cert.crt $ mv key.pem key.key
Convert .pem to .crt and .key
Can anyone tell me the correct way/command to extract/convert the certificate .crt and private key .key files from a .pem file? I just read they are interchangable, but not how.
7 Answers 7
I was able to convert pem to crt using this:
openssl x509 -outform der -in your-cert.pem -out your-cert.crt
Using a text editor is not the best approach. To extract the key in PKCS8 form: openssl pkey -in mumble.pem -out mumble-key.pem If the OpenSSL version is older than 1.0.0, to extract the key as an RSA key: openssl rsa -in mumble.pem -out mumble-key.pem
I tried your command but I had: unable to load certificate 140584440387400:error:0906D06C:PEM routines:PEM_read_bio:no start line:pem_lib.c:703:Expecting: TRUSTED CERTIFICATE
the key command (openssl pkey -in mumble.pem -out mumble-key.pem) gives this: unable to load key 129051320116880:error:0906D06C:PEM routines:PEM_read_bio:no start line:pem_lib.c:703:Expecting: ANY PRIVATE KEY
Converting Using OpenSSL
These commands allow you to convert certificates and keys to different formats to make them compatible with specific types of servers or software.
- Convert a DER file (.crt .cer .der) to PEM
openssl x509 -inform der -in certificate.cer -out certificate.pem
openssl x509 -outform der -in certificate.pem -out certificate.der
openssl pkcs12 -in keyStore.pfx -out keyStore.pem -nodes You can add -nocerts to only output the private key or add -nokeys to only output the certificates.
openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.crt -certfile CACert.crt
openssl x509 -outform der -in certificate.pem -out certificate.crt
OpenSSL Convert PEM
openssl x509 -outform der -in certificate.pem -out certificate.der
openssl crl2pkcs7 -nocrl -certfile certificate.cer -out certificate.p7b -certfile CACert.cer
openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.crt -certfile CACert.crt
OpenSSL Convert DER
openssl x509 -inform der -in certificate.cer -out certificate.pem
OpenSSL Convert P7B
openssl pkcs7 -print_certs -in certificate.p7b -out certificate.cer
openssl pkcs7 -print_certs -in certificate.p7b -out certificate.cer openssl pkcs12 -export -in certificate.cer -inkey privateKey.key -out certificate.pfx -certfile CACert.cer
OpenSSL Convert PFX
openssl pkcs12 -in certificate.pfx -out certificate.cer -nodes
Generate rsa keys by OpenSSL
- Using OpenSSL on the command line you’d first need to generate a public and private key, you should password protect this file using the -passout argument, there are many different forms that this argument can take so consult the OpenSSL documentation about that.
openssl genrsa -out private.pem 1024
openssl rsa -in private.pem -out public.pem -outform PEM -pubout or openssl rsa -in private.pem -pubout > public.pem or openssl rsa -in private.pem -pubout -out public.pem
You’ll now have public.pem containing just your public key, you can freely share this with 3rd parties. You can test it all by just encrypting something yourself using your public key and then decrypting using your private key, first we need a bit of data to encrypt:
echo 'too many secrets' > file.txt
openssl rsautl -encrypt -inkey public.pem -pubin -in file.txt -out file.ssl
openssl rsautl -decrypt -inkey private.pem -in file.ssl -out decrypted.txt
cat decrypted.txt |output -> too many secrets
RSA TOOLS Options in OpenSSL
openssl rsa [-help] [-inform PEM|NET|DER] [-outform PEM|NET|DER] [-in filename] [-passin arg] [-out filename] [-passout arg] [-aes128] [-aes192] [-aes256] [-camellia128] [-camellia192] [-camellia256] [-des] [-des3] [-idea] [-text] [-noout] [-modulus] [-check] [-pubin] [-pubout] [-RSAPublicKey_in] [-RSAPublicKey_out] [-engine id]
The rsa command processes RSA keys. They can be converted between various forms and their components printed out. Note this command uses the traditional SSLeay compatible format for private key encryption: newer applications should use the more secure PKCS#8 format using the pkcs8 utility.
This specifies the input format. The DER option uses an ASN1 DER encoded form compatible with the PKCS#1 RSAPrivateKey or SubjectPublicKeyInfo format. The PEM form is the default format: it consists of the DER format base64 encoded with additional header and footer lines. On input PKCS#8 format private keys are also accepted. The NET form is a format is described in the NOTES section.
This specifies the input filename to read a key from or standard input if this option is not specified. If the key is encrypted a pass phrase will be prompted for.
the input file password source. For more information about the format of arg see the PASS PHRASE ARGUMENTS section in openssl.
This specifies the output filename to write a key to or standard output if this option is not specified. If any encryption options are set then a pass phrase will be prompted for. The output filename should not be the same as the input filename.
the output file password source. For more information about the format of arg see the PASS PHRASE ARGUMENTS section in openssl.
-aes128|-aes192|-aes256|-camellia128|-camellia192|-camellia256|-des|-des3|-idea
These options encrypt the private key with the specified cipher before outputting it. A pass phrase is prompted for. If none of these options is specified the key is written in plain text. This means that using the rsa utility to read in an encrypted key with no encryption option can be used to remove the pass phrase from a key, or by setting the encryption options it can be use to add or change the pass phrase. These options can only be used with PEM format output files.
prints out the various public or private key components in plain text in addition to the encoded version.
by default a private key is output: with this option a public key will be output instead. This option is automatically set if the input is a public key.
-RSAPublicKey_in, -RSAPublicKey_out
specifying an engine (by its unique id string) will cause rsa to attempt to obtain a functional reference to the specified engine, thus initialising it if needed. The engine will then be set as the default for all available algorithms.
-----BEGIN RSA PRIVATE KEY----- -----END RSA PRIVATE KEY-----
-----BEGIN PUBLIC KEY----- -----END PUBLIC KEY-----
-----BEGIN RSA PUBLIC KEY----- -----END RSA PUBLIC KEY-----
The NET form is a format compatible with older Netscape servers and Microsoft IIS .key files, this uses unsalted RC4 for its encryption. It is not very secure and so should only be used when necessary. Some newer version of IIS have additional data in the exported .key files. To use these with the utility, view the file with a binary editor and look for the string «private-key», then trace back to the byte sequence 0x30, 0x82 (this is an ASN1 SEQUENCE). Copy all the data from this point onwards to another file and use that as the input to the rsa utility with the -inform NET option.
openssl rsa -in key.pem -out keyout.pem
openssl rsa -in key.pem -des3 -out keyout.pem
openssl rsa -in key.pem -outform DER -out keyout.der
openssl rsa -in key.pem -text -noout
openssl rsa -in key.pem -pubout -out pubkey.pem
openssl rsa -in key.pem -RSAPublicKey_out -out pubkey.pem