notes
notes
Shayan Ghazi
OpenSSL Tutorial
1. Introduction
OpenSSL is an open source software library useful for encryption and secure network
communication. SSL stands for Secure Sockets Layer, a cryptographic communications
protocol. This tutorial will demonstrate one way to use openssl to exchange a file between two
parties. There are 3 sections on asymmetric encryption, certificates and RSA respectively-
please feel free to skip straight to section 5 if you already have a good understanding of them.
2. Asymmetric Encryption
Let’s say you have two parties (Alice and Bob) communicating over an insecure network, one on
which an attacker (Eve) could intercept all communications. They want to communicate with
confidentiality, meaning Eve shouldn’t be able to understand what they’re talking about, even
if she can listen to everything they are sending each other. They want authenticity- Eve
shouldn’t be able to defraud Alice by sending messages to her claiming to be from Bob, and vice
versa.
You might think to yourself- “Easy! Alice can simply encrypt her message with a key before
sending it to Bob. And then Bob can decrypt the message with that key. Eve won’t be able to
read anything!”. But that implies Alice and Bob must both possess the same key, and that begs
the question- how did they securely exchange a symmetric key?
1
Alice and Bob can meet up in person to exchange the key- but this isn’t always practical in
today’s interconnected age. Think of Alice as an internet user and Bob as a webserver- imagine
how inconvenient it would be if you had to visit a server farm everytime you wanted to access a
new website!
One solution here is to use asymmetric encryption. Alice can generate a pair of keys- a private
key that she keeps to herself, and a public key that is freely available to anyone who wants it.
If someone encrypts some data with her public key, it can be decrypted with her private key.
And she can “sign” data with her private key- a signature that can then be verified with her
public key. Bob must have his own set of public and private keys that behave in the same
fashion.
So if Alice wants to send Bob a message in a confidential manner- she can encrypt it with Bob’s
public key. Only Bob’s private key can be used to decrypt it, hence only Bob can read the
message. And if Alice wants to authenticate herself to Bob, she can generate a signature by
signing the message with her private key.
Note: In the RSA cryptosystem, signing a message involves hashing it and then encrypting the
hash using a private key. Verifying that signature consists of decrypting the ciphertext using
the corresponding public key and checking it against a hash of the message.
2
Figure 2.3: Signing a document using a private key (src)
1. Alice generates ciphertext by encrypting her message with Bob’s public key
2. Alice generates a signature by hashing her message and then encrypting it with her
private key
○ Note: If she didn’t hash the message, Eve could simply decrypt this signature
using Alice’s freely available public key, and thus read the message
You might be wondering- how do Alice and Bob exchange public keys? Can’t they be tampered
with or falsified by Eve, just like all their communications? The answer is in the next section…
3
3. Certificates
Alice and Bob can use certificates to ensure that their public keys are authentic. They first
generate a certificate signing request (CSR) that contains their public keys, some important
information about themselves (name, address, organization, etc..) and is signed with their
private keys. These CSR’s are then signed by a trusted third party- a Certificate Authority
(CA), with a private key of its own, and turned into certificates.
The CA also has a self-signed certificate, and Alice can verify Bob’s certificate by checking it
against the CA’s certificate. She doesn’t have to worry about the authenticity of the CA’s
certificate because it isn’t sent to her over a network. Instead, she’ll already have the CA’s
certificate on her computer (most browsers come with a list of pre-installed certificates from
trusted CA organizations, as does Windows).
Each CA is an established organization with a reputation. It does all the legwork in determining
that Bob is who he says he is, and in making sure that its private key is not compromised. It
charges Bob for this service (although there are some free options like Let’s Encrypt). In this
way Alice can trust the CA certificates that are on her computer- she doesn’t have to worry
about the specifics behind how Bob and the CA are communicating.
You can check out wikipedia for a list of the largest certificate authorities by market share.
4
Let’s take a look at the structure of an actual certificate. We’re going to see what ssl2buy.com’s
certificate looks like- you can follow along yourself if you’d like by visiting this page.
Above we can see that www.ssl2buy.com has been issued a certificate by COMODO RSA
Extended Validation Secure Server CA. Note the validity period- the CA also inputs an expiry
date after which the certificate is no longer valid. On the right we can see that the signature
algorithm is sha256RSA - this means that the CA produced a signature by hashing the rest of
the certificate with sha256, then encrypting that hash using RSA and its private key.
Chain of trust
The Certificate Authority’s job involves signing many different signing requests with its private
key. A single key also means a single point of failure- if it’s compromised, then all certificates
signed by that CA cannot be trusted. And if the CA wants to use one private key for all it’s
servers, it must copy the key onto every one of them, increasing it’s vulnerability.
Instead, CA’s use a hierarchical structure of certificates and private keys- a “root” key signs a
number of “intermediate” certificates with their own intermediate private keys. The
intermediate keys can be used to sign lower-level certificates, and so on, for as many levels as
required. The intermediate or low level keys are actually used for signing end-entity CSRs, not
the root key. The root key is kept in some secure, encrypted, offline environment, never to be
used unless the CA wants to generate more intermediate certificates.
5
Figure 3.3: An example of a certificate hierarchy (src)
So using the hierarchy above, if Alice wants to verify Bob’s certificate, she’ll first check it
against the Intermediate Certificate. If she already has the intermediate certificate in her
computer’s store of trusted certificates, she’s done. If not, she has to check the intermediate
certificate against the root certificate. This is why it’s called a chain of trust -if at any point
any of these verification ‘links’ are broken, Bob’s certificate can not be considered valid.
6
We can take a look at ssl2buy.com’s certificate and see that it also relies on a chain of trust:
CA’s can also maintain certification revocation lists- certificates that have been revoked before
their expiration date and should no longer be trusted. All this is part of a public-key
infrastructure that Alice and Bob can use to communicate.
Finally, let’s take a look at ssl2buy’s public key (as contained in their certificate):
What does modulus or exponent mean? Find out in the next section…
7
4. RSA
Then the encryption and decryption functions are (where M = message, C = ciphertext):
● C = M e mod n
● M = C d mod n
It’s important to note that in practice M should not be a plaintext message. Rather a plaintext
message should be processed into M using a padding algorithm. You can read more here.
Note: RSA cannot encrypt data larger than the modulus length. It’s not meant to encrypt
arbitrarily large files. You could of course break the data into small chunks and encrypt those
chunks separately, even using techniques to combat frequency analysis. But RSA is still
relatively computationally expensive- you’re better off using it to exchange a symmetric key
and go from there.
This was a very brief intro to RSA. If you’re interested in learning more, see this tutorial.
8
5. 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