OpenSSL Tutorial
OpenSSL Tutorial
The Protocol
So, given what we now know about asymmetric encryption, certificates and RSA, let’s put it
together in a single protocol:
Note: In real life, the protocols used are a little more complicated than this. You’ll notice that
both parties need to be using the same hashing and encryption algorithms, requiring more
initial communication (this is done in the TLS handshake for example).
9
6. OpenSSL Demo
Here we’ll implement all the steps of that protocol, using openssl terminal commands. In
practice you’re more likely to use openssl in the form of an API in another language- but
learning the terminal commands is still valuable as a transferable skill. Each command is
displayed with some explanations of its flags below.
If you want to follow along, you can make 3 folders, 1 for Alice, Bob and the CA respectively.
You need to repeat steps 1.a and 1.b for Bob and CA so they can have their own pair of keys.
And you need to generate a self-signed certificate for the CA (shown below).
The keys are saved in base64, and aren’t human readable if you open them in a text editor or
the terminal. Luckily, openssl provides us with a handy set of commands to convert them to
text. The (-noout) flag suppresses the command from printing out the base64 encoding as well.
10
Note the size difference in the private key and the public key (one is a subset of the other,
afterall). There are some additional values stored in the private key that you won’t recognize
(exponent1, exponent2 and coefficient). These are stored by openssl to speed up decryption.
openssl req -x509 -new -nodes -key rootkey.pem -sha256 -days 1024 -out
root.crt
● x509 ➝ an x509 certificate utility (displays, converts, edits and signs x509 certificates)
● -req ➝ a certificate request is taken as input (default is a certificate)
● -CA root.crt ➝ specifies the CA certificate to be used as the issuer of Alice’s certificate
● -CAkey rootkey.pem ➝ specifies the private key used in signing (rootkey.pem)
● -CAcreateserial ➝ creates a serial number file which contains a counter for how many
certificates were signed by this CA
● -days 500 ➝ sets Alice’s certificate to expire in 500 days
● -sha256 ➝ specifies the hashing algorithm to be used for the certificate’s signature
11
Aside: viewing the certificate as text
Step 2.c - Alice tries to encrypt her largefile.txt with Bob’s public key
12
Step 3.b - Alice encrypts symkey.pem using Bob’s public key
Step 3.c - Alice hashes symkey.pem and encrypts it using her private key
● dgst -sha1 ➝ hash the input file using the sha1 algorithm
● -sign privkey-A.pem ➝ sign the hash with the specified private key
● symkey.pem ➝ the input file to be hashed
Step 4.b - Bob gets and verifies Alice’s certificate and extracts her public key
Steps 4.c and 4.d in the protocol are combined in this step. Bob hashes symkey.pem, decrypts
signature.bin, and compares the two results in one command:
Step 5.a - Alice encrypts her largefile.txt with the symmetric key
13
openssl enc -aes-256-cbc -pass file:symkey.pem -p -md sha256 -in
largefile.txt -out ciphertext.bin
● enc -aes-256-cbc ➝ encrypt a file using the aes-256-cbc symmetric key algorithm
● -pass file:symkey.pem ➝ specified the file to get the symmetric key from
● -p ➝ prints the key, salt, initialization vector to the screen
● -md sha256 ➝ uses sha256 as part of the key derivation function (a f unction that
derives one or more secondary secret keys from a primary secret key)
Step 5.b - Bob decrypts ciphertext.bin with the same symmetric key
● -d ➝ decryption flag
7. References
https://www.openssl.org/
https://prefetch.net/articles/realworldssl.html
https://jamielinux.com/docs/openssl-certificate-authority/create-the-root-pair.html
https://www.oreilly.com/library/view/network-security-with/059600270X/
http://heartbleed.com/
14