Certificate types X.509 certificates: X.509 certificates are the most common type of digital certificate. They are often used for website security and for email encryption. X.509 certificates can be issued by either a trusted third party or by a company or organization include many types :-
- PEM (Privacy Enhanced Mail ): the most common type of X.509 certificate format and is most likely used in Unix/Linux-based operating systems. It is a Base64-encoded DER certificate. This is the format most of the Certificate Authorities (CA) issue certificates with .pem, .crt, .cer, or .key extensions. PEM certificates can be used with Apache, Microsoft IIS , and other web servers. It is possible to bundle the server certificate, the intermediate certificate, and the private key within a single PEM certificate. Or you can have all of them in a separate file.
- DER (Distinguished Encoding Rule) : is a binary form of a PEM certificate. Often used in Windows and with Java-based web servers. This format is barely seen used in Linux-based operating systems. So, you can choose this if you need the certificate for your Windows servers.
- P7B (PKCS#7) : This is a format that can be used to store multiple chain certificates. However, unlike PEM, it doesn’t store a private key. PKCS#7 certificates can be used with Apache, Microsoft IIS , and other web servers.
- PFX (PKCS#12/P12) : Personal Information Exchange is a format that can be used to store multiple private keys and certificates like PEM. However, the difference is that PFX is a password-protected container. PKCS#12 certificates can be used with Apache, Microsoft IIS , and other web servers.
[!warning] Using
winpty
beforeopenssl
on windows through GIT bash If you are using OpenSSL commands on windows through the GIT bash you may need to prefix eachopenssl
command withwinpty
Note : the (text in between), references a file name, like (filename).xml can be any .xml with name of your choice
openssl pkcs12 -export -out (OUTPUTNAME).pfx -inkey (KEY).key -in (FILENAME).cer -certfile (CACert).crt
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 crl2pkcs7 -nocrl -certfile (certificate).cer -out (certificate).p7b -certfile (CACert).cer
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 pkcs12 -in (certificate).pfx -out (certificate).cer -nodes
The following command can be used to test connectivity to an https service. Basically s_client
attribute is used to debug SSL Servers.
This will open an SSL connection to example.com with port 443 and print the SSL certificate used by the service. After connecting you can manually send http requests. This is similar to using telnet to connect to an http service and manually sending an http, i.e GET, OPTIONS or TRACE request. Press Enter Twice:
openssl s_client -connect example.com:443
One commonly used and secure encryption scheme is PBKDF2, which stands for Password-Based Key Derivation Function 2 :
openssl enc -pbkdf2 -aes-256-cbc -in {file to encrypt} -out {output file}
- Using AES256
openssl aes-256-cbc -e -in {file to encrypt} -out {encrypted_message file}
to decrypt that file :-
openssl aes-256-cbc -d -in {encrypted_message file} -out {decrypted file}
- To make the encryption more secure and resilient against brute-force attacks, we can add
-pbkdf2
to use the Password-Based Key Derivation Function 2 (PBKDF2); moreover, we can specify the number of iterations on the password to derive the encryption key using-iter NUMBER
.
openssl enc -pbkdf2 -aes-256-cbc -d -in {file to encrypt} > {encrypted file}
# OR to iterate 10,000 times, the command would become:
openssl aes-256-cbc -pbkdf2 -iter 10000 -e -in {file to encrypt} -out {encrypted_message_file}
decryption :
openssl aes-256-cbc -pbkdf2 -iter 10000 -d -in {encrypted_message} -out {original_message_file}
- To use RSA, first generate a new 1024-bit private key stored in the file
openssl genrsa -out {private key file}.pem 1024
• Next, use that private key to generate a public key
openssl rsa -in {private key file}.pem -out {public key file}.pem -outform PEM -pubout
# OR
openssl rsa -in {private key file}.pem -pubout -out {public key file}.pem
• Display public key and private keys (example)
cat {public key file}.pem
# display private key
openssl rsa -in {private key file}.pem -text -noout
• Now you (or anyone!) can encrypt a file using this public key:
openssl rsautl -encrypt -inkey {pem-file}.pem -pubin -in {file-text}.txt -out {rsa-encrypt-file}.txt
• Finally, only you can decrypt this file, so long as only you know your private key
openssl rsautl -decrypt -inkey {pem-file}.pem -in {rsa-encrypt}.txt -out {rsa-decrypted file}.txt
Diffie-Hellman is an asymmetric encryption algorithm. It allows the exchange of a secret over a public channel.
openssl dhparam -out dhparams.pem 2048
we can view the prime number P
and the generator G
using the command
openssl dhparam -in dhparams.pem -text -noout
Hash-based message authentication code (HMAC) is a message authentication code (MAC) that uses a cryptographic key in addition to a hash function.
Example:
openssl dgst -sha256 -hmac "secret key in quotes" {file to hash}
OpenSSL on the command line you’d first need to generate a public and private key
Should password protect this file using the -passout
argument
-
Create a key file called private.pem that uses 1024 bits, file actually has both the private and public keys
openssl genrsa -out (private).pem 1024
-
Extract the public key from this file with either one of the these commands
openssl rsa -in (private).pem -out (public).pem -outform PEM -pubout
openssl rsa -in (private).pem -pubout > (public).pem
openssl rsa -in (private).pem -pubout -out (public).pem
Create a dummy data file : echo 'too many secrets' > (file).txt
-
encrypt the file with public key
openssl rsautl -encrypt -inkey (public).pem -pubin -in (file).txt -out (file).ssl
-
decrypt using private key
openssl rsautl -decrypt -inkey (private).pem -in file.ssl -out (decrypted).txt
- Extract all the certs, including the CA Chain
openssl crl2pkcs7 -nocrl -certfile (foo).pem | openssl pkcs7 -print_certs -out (foo).cert
- Extract the textually first cert as DER
openssl x509 -in foo.pem -outform DER -out first-cert.der
-
remove the pass phrase on an RSA private key
openssl rsa -in key.pem -out keyout.pem
-
encrypt a private key using triple DES
openssl rsa -in key.pem -des3 -out keyout.pem
-
private key from PEM to DER
openssl rsa -in key.pem -outform DER -out keyout.der
-
print out the components of a private key
openssl rsa -in key.pem -text -noout
-
output the public part of a private key
openssl rsa -in key.pem -pubout -out pubkey.pem
-
Output the public part of a private key in RSAPublicKey
openssl rsa -in key.pem -RSAPublicKey_out -out pubkey.pem
To make SSL work between your server and client
a. Generate keystore :
keytool -genkey -alias {alias name} -keyalg RSA -keystore (KeyStore).jks -keysize 2048
Optional: export the certificate for that key to PEM format
_keytool -export -rfc -alias {alias name} -file (certificate).pem -keystore (KeyStore).jks
b. Generate new ca-cert and ca-key
openssl req -new -x509 -keyout ca-key -out ca-cert
c. Extracting cert/creating cert sign
keytool -keystore (KeyStore).jks -alias bmc -certreq -file cert-file
d. Sign the “cert-file” and cert-signed will be the new cert
openssl x509 -req -CA ca-cert -CAkey ca-key -in cert-file -out cert-signed -days 365 -CAcreateserial -passin pass:yourpass
e. importing the ca-cert to keystore file
keytool -keystore KeyStore.jks -alias CARoot -import -file ca-cert
f. import cert-signed to keystore
keytool -keystore KeyStore.jks -alias bmc -import -file cert-signed
g. Copy ca-cert into client machine and generate trust-store
keytool -keystore truststore.jks -alias bmc -import -file ca-cert-s
Repeat the steps ( a-f ) at client side
h. Copy ca-cert into client machine and generate trust-store
keytool -keystore truststore.jks -alias bmc -import -file ca-cert-c
generate trust-store at server side by importing ca-cert of client(step h)
Renamed ca-cert after step f. Ex: ca-cert-s generated at server side and ca-cert-c at client and exchanged each other for generating trust-store