Holistic usage guide for OpenSSL
We aggregate and tag open source projects. We have collections of more than one million projects. Check out the projects section.
OpenSSL is a general purpose cryptographty toolkit that provides an open source implementation of Transport Layer Security(TLS) and Secure Socket Layer(SSL) protocols. It is written in C,assembly and Perl language but wrappers are available in all languages. This article explains about OpenSSL commands.
For the 3.0.0 release, and later releases derived from that, the Apache License v2 applies. Before that it is licensed under OpenSSL license.
It provides various cryptographic functions
- RSA & AES keys
- Certificate Signing Requests(CSR), X509 certificates
- Message digest/checksums
- Encryption / Decryption with ciphers and encoding commands.
Encoding and decoding schemes are used to convert the binary to text and vice versa respectively. Base64 is one of the encoding scheme where a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. This base64 encoding used in the encrypted bytes to convert into textual format and transmit the data securely.
Below commands will base64 encode the text and then decode back to the text format.
$ echo "secret password" | openssl enc -base64
$ echo "c2VjcmV0IHBhc3N3b3JkZAo=" | openssl enc -base64 -d
Advanced Encryption Standard(AES)
AES is symmetric encryption algorithm, where text will be encrypted by secret key, then it will be decrypted by same secret key. Openssl provides AES encryption/decryption facilities of different block sizes.
Cipher algorithm (AES cyber block chain of 256 bit) which will encrypt and then does base64 encoding. To encrypt the data, provide encryption password (secret key). During decryption, same password / secret has to be provided. Cyber block chain will split the message text into block size, xor with initialiation vector and encrypt with key, then output will be xor with next block of message text which will again encrypt with key, it will continue until all blocks encrypted.
$ echo "confidential-data" | openssl enc -aes-256-cbc -base64
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
$ echo "U2FsdGVkX1/2T5aTQE9K/PCJlXCqtAC9RIxGoQIdrFc=" | openssl enc -aes-256-cbc -base64 -d
enter aes-256-cbc decryption password:
Rivest-Shamir-Adleman Encryption Algorithm
RSA algorithm works with public (known to everyone) and private key (secret key) combinations based on factorization difficulty of large prime numbers. Message will be encrypted with public key and transmitted to recipient who will decrypt with his secret private key. It is slow algorithm so it is used to share the symmetric secret key, less commonly used for encryption of user data.
Alice and Bob want to transfer secure information.
- Bob has securely stored his private key and Bob's public key is available with Alice.
- Alice encrypts the data using Bob's public key and sends the encrypted data to Bob
- Bob decrypts the data using his private key
Below OpenSSL commands used to generate private key and then public key from private key.
$ openssl genrsa -out rsaprivatekey.pem 2048
$ openssl rsa -in rsaprivatekey.pem -pubout -outform PEM -out rsapublickey.pem
rsautl command option will encrypt the message with public key which produce secure message. Redirect the output to a file. Decrypt the secure message with generated private key.
$ echo "confidential-data" | openssl rsautl -encrypt -pubin -inkey rsapublickey.pem > encryptedmsg
$ openssl rsautl -decrypt -inkey rsaprivatekey.pem -in encryptedmsg -out decryptedmsg
$ cat decryptedmsg
Message Digest/Hashing Message
Message Digest or Hash Function takes any arbitrary message (with any content or length) as an input and provides a fixed size hash value as a result.
- It is to used to verify the message was transmitted without any loss or tampering by hashing the message matches with message checksums.
- It is also used to store password by hashing the password. (Hashing is one-way function which will not be reversed).
OpenSSL has dgst command option to hash the password. If same command executed again, it will give the same hash value. Below command generates hash using sha256 algorithm.
$ echo 'password' | openssl dgst -sha256
$ echo 'password' | openssl dgst -sha256
Certificate Signing Requests
SSL Certificate is valid certificate only after authorized by Certificate Authority(CA). So to get the authoriztion, certificate signing request has to be sent to CA.
OpenSSL is to generate the certificate signing request, which will prompt for the details like location, organization details and finally generatedcertificate.csr is generated.
$ openssl req -new -key rsaprivatekey.pem -out generatedcertificate.csr -sha256
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
Country Name (2 letter code) [AU]:IN
State or Province Name (full name) [Some-State]:TAMILNADU
Locality Name (eg, city) :COIMBATORE
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Findbestopensource
Organizational Unit Name (eg, section) :TechnicalBlogs
Common Name (e.g. server FQDN or YOUR name) :
Email Address :email@example.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password :password
An optional company name :TechnicalBlogs
Print the generated certificate request file.
$ cat generatedcertificate.csr
-----BEGIN CERTIFICATE REQUEST-----
-----END CERTIFICATE REQUEST-----
X509 Certificate Generation
Similar to CA generating the certificate, we can use openssl to create certificate for local development purposes. X509 certificate is a standard defining public key certificate. It contains public key, identity, location and validity period..
OpenSSL with X509 tool to generate the certificate from certificate signing request, validity days and root CA certificate with private key. Root CA certificate and private key can be generated if it is not already using mkcert certificate authority tool.
$ openssl x509 -req -in generatedcertificate.csr -signkey rsaprivatekey.pem -CA rootCA.pem -CAkey rootCA-key.pem -CAcreateserial -out technicalblogcertificate.crt -days 365
subject=C = IN, ST = TAMILNADU, L = COIMBATORE, O = Findbestopensource, OU = TechnicalBlogs, emailAddress = firstname.lastname@example.org
Getting Private key
Getting CA Private Key
Print the certificate and could find the root CA as mkcert with given identity.
$ openssl x509 -in technicalblogcertificate.crt -text -noout | head -20
Version: 1 (0x0)
Signature Algorithm: sha256WithRSAEncryption
Issuer: O = mkcert development CA, OU = ubuntu@ip-172-31-40-103, CN = mkcert ubuntu@ip-172-31-40-103
Not Before: Jan 24 07:25:42 2019 GMT
Not After : Jan 24 07:25:42 2020 GMT
Subject: C = IN, ST = TAMILNADU, L = COIMBATORE, O = Findbestopensource, OU = TechnicalBlogs, emailAddress = email@example.com
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Certificate can also verified by verify command options provided by OpenSSL.
$ openssl verify technicalblogcertificate.crt
mkcert - no config certificate authority tool