0% found this document useful (0 votes)
15 views26 pages

Open ssl1

c2

Uploaded by

yasmine chiter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views26 pages

Open ssl1

c2

Uploaded by

yasmine chiter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

By

Kaouther hamani
Chams Nadine Saadallah
OpenSSL
OpenSSL is an open source software
library useful for encryption and secure
network communication. SSL stands for
Secure Sockets Layer, a cryptographic
communications protocol.
OpenSSL
Protocole SSL Le protocole TLS
Le protocole SSL estpour
est un acronyme entre la couche
Secure Socket TCP/IP
Layer quiet une (Transport Layer Security) est une évolution de
application
est un utilisant
protocoleTCP.(en Lefait principe
un ensemblegénérale
de d’un SSL réalisé par l’IETF et qui sert de base à
protocole de typequi
protocoles) SSL est qu’il
a été se passe
développé par en
la deux temps : HTTPS par exemple.
société
1. Une poignée Communication
Netscape de mains : c’est une étape durant
Corporation laquelle
pour
le client et le serveur
permettre de la s’identifient,
communication se mettent d’accord
sécurisée en sur
le type du système
mode de chiffrement
client/serveur pour des et lesapplication
clefs qui seront
utilisés lors du
réseaux reste de
utilisant la communication. 2. La phase de
TCP/IP.
communication : les données sont alors échangées en
format compressées et chiffrées et signées.
OpenSSL
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
OpenSSL
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.
OpenSSL
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.
OpenSSL
RSA:
The setup behind RSA is as follows:
1. Select two large primes p and q
2. Compute n = pq and Ф(n) = (p - 1)(q - 1)
3. Choose an encryption key e that is relatively prime to Ф(n)
4. Calculate a decryption key d such that ed = 1 mod Ф(n)
5. Your private key is all of { e, n, p, q, d }
6. Your public key is { e, n } i.e the public exponent and modulus
Then the encryption and decryption functions are (where M = message, C = ciphertext):
● C = Me mod n
● M = Cd 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.
OpenSSL
1.Download the OpenSSL for Windows installation package.
2.Double-click the installation file.
3.If the following error message appears, you should install Microsoft Visual C++
2008 Redistributables.
4.Double-click the installation file and click on Next

Note: The following error message might appear when


running OpenSSL:
WARNING: can't open config file: /usr/local/ssl/openssl.cnf
This can be solved as following:
1.Close OpenSSL.
2.Open a Command Prompt (CMD) as Administrator
3.Run the following command:SET
OPENSSL_CONF=C:\OpenSSL-Win32\bin\openssl.cfg
4.Reboot the computer.
OpenSSL
The Protocol:
So, given what we now know about asymmetric encryption, certificates and RSA, let’s put it
together in a single protocol:
1. Alice needs a certificate: 3. Alice picks a symmetric key:
a. She chooses a public exponent e and generates a a. She picks a strong symmetric key using a pseudo-
private key random number generator
b. She generates a public key from that private key b. She encrypts it with Bob’s public key ➝ symkey.enc
c. She generate a certificate signing request ➝ sends that c. She hashes it and then encrypts it with her private key
to the CA ➝ signature.bin
d. The CA generates a certificate for Alice and signs it ➝ d. She sends both symkey.enc and signature.bin to Bob
sends it to Alice
4. Bob deciphers and verifies the symmetric key:
2. Alice gets Bob’s certificate from him: a. He decrypts symkey.enc using his private key
a. She verifies it using the CA’s certificate (already pre- b. He gets and verifies Alice’s certificate and extracts her
installed on her computer) public key
b. She extracts Bob’s public key from it c. He decrypts signature.bin using Alice’s public key
c. She attempts to encrypt her large message using Bob’s d. He compares a hashed symkey with the decrypted
public key ➝ error! signature, they must match
OpenSSL
The Protocol:
So, given what we now know about asymmetric encryption, certificates and RSA, let’s put it
together in a single protocol:
5. Alice encrypts her large message with that symmetric
key:
a. She needs to use choose a symmetric key encryption
Note : In real life, the protocols used are a little more
algorithm complicated than this. You’ll notice that
b. Bob decrypts using the symmetric key and that same both parties need to be using the same hashing and encryption
algorithm algorithms, requiring more
c. She can also use something like HMAC now for initial communication (this is done in the TLS handshake for
authentication (this won’t be example).
covered in this document but it’s similar to how she
creates her signature
OpenSSL
OpenSSL Demo:
Here we’ll implement all the steps of that protocol, using openssl terminal commands.

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).
OpenSSL
Step 1.a - Alice generates a private key

openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 –pkeyopt rsa_keygen_pubexp:3 -out privkey-A.pem

genpkey ➝ generate a private key


● -algorithm RSA ➝ use the RSA algorithm (can also
take “EC” for elliptic-curve)
● -pkeyopt opt:value ➝ set opt to value (see items
below)
● rsa_keygen_bits:2048 ➝ sets the size of the key to
2048 bits (the default is 1024)
● rsa_keygen_pubexp:3 ➝ sets the public exponent e to
3 (default is 65, 537)
● -out privkey-A.pem ➝ outputs to the file privkey-
A.pem
OpenSSL
Step 1.b - Alice generates a public key
openssl pkey - in privkey-A.pem -pubout -out pubkey-A.pem

● pkey ➝ processes public or private keys


● -in privkey-A.pem ➝ read the key from filename
privkey-A.pem
● -pubout ➝ output a public key (by default, a private
key is output)
OpenSSL
Step 1.b - Alice generates a public key
openssl pkey - in privkey-A.pem -pubout -out pubkey-A.pem

● pkey ➝ processes public or private keys


● -in privkey-A.pem ➝ read the key from filename
privkey-A.pem
● -pubout ➝ output a public key (by default, a private
key is output) Aside: viewing the keys in plain text
The keys are saved in base64, and aren’t human readable
if you open them in a text editor or
openssl pkey - in privkey-A.pem -text -noout the terminal. Luckily, openssl provides us with a handy set
openssl pkey -pubin - in pubkey-A.pem -text -noout of commands to convert them to
text. The ( -noout ) flag suppresses the command from
printing out the base64 encoding as well.
OpenSSL
Step 1.c - Alice generates a certificate signing request
openssl req -new -key privkey-A.pem -out A-req.csr

The command will prompt Alice with these


questions:
● req ➝ creates and processes signing requests
● C ountry code [ C ]: {Alice fills in her country
● -new ➝ generates a new certificate request, will code}
prompt Alice for some information ● Province/ ST ate name [ ST ]: {Alice fills in her
● -key privkey-A.pem ➝ signs the request with Alice’s province name fully}
private key ● City/ L ocation [ L ]: {The city Alice’s business is
registered in, for example}
● O rganization Name [ O ]: {Alice’s business
name, for example}
● O rganizational U nit Name [ OU ]: (Optional)
{What part of the company is she?}
● C ommon N ame [ CN ]: the hostname+domain,
i.e. “www.alice.com”
● A challenge password []: {this can be used as a
secret nonce between Alice and CA}
OpenSSL
Aside: generating a self-signed certificate for the CA
openssl req -x509 -new -nodes -key rootkey.pem -sha256 -days 1024 –out root.crt
OpenSSL
Step 1.d - CA generates and signs a certificate for Alice openssl
openssl x509 -req - in A-req.csr -CA root.crt -CAkey rootkey.pem -CAcreateserial -out A.crt -days 500 -sha256

● 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
OpenSSL
Step 2.a - Alice verifies Bob’s public certificate
openssl verify -CAfile root.crt Bob.crt

● verify ➝ a utility that verifies certificate chains


● -CAfile root.crt ➝ specified the trusted certificate (root.crt)
● Bob.crt ➝ the certificate to verify
● If you get an OK, you know the certificate can be trusted
OpenSSL
Step 2.b - Alice extracts Bob’s public key
openssl x509 -pubkey - in Bob.crt -noout > pubkey-B.pem

● -pubkey ➝ outputs the certificate’s public key (in PEM format )


OpenSSL
Step 2.c - Alice tries to encrypt her largefile.txt with Bob’s public key
openssl pkeyutl -encrypt - in largefile.txt -pubin -inkey pubkey-B.pem –out ciphertext.bin

● pkeyutl ➝ utility to perform public key operations


● -encrypt ➝ encrypt the input data
● error! (recall: RSA is not meant for encrypting arbitrary large files- Alice needs to use
symmetric key encryption for that)
OpenSSL
Step 3.a - Alice generates a symmetric key
openssl rand -base64 32 -out symkey.pem

● rand ➝ generates pseudo-random bytes (seeded by default by $HOME/.rnd)


● -base64 32 ➝ outputs 32 random bytes and encodes it in base64
OpenSSL
Step 3.b - Alice encrypts symkey.pem using Bob’s public key
openssl pkeyutl -encrypt - in symkey.pem -pubin -inkey pubkey-B.pem –out symkey.enc

Step 3.c - Alice hashes symkey.pem and encrypts it using her private key

openssl dgst -sha1 -sign privkey-A.pem -out signature.bin symkey.pem

● 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
OpenSSL
Step 4.a - Bob decrypts symkey.enc using his private key
openssl pkeyutl -decrypt - in symkey.enc -inkey privkey-B.pem –out symkey.pem

● -decrypt ➝ decrypt the input file


Step 4.b - Bob gets and verifies Alice’s certificate and extracts her public key
(This is simply a retread of what Alice did in step 2)
OpenSSL
Step 4.c - Bob verifies the message is from Alice
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:

openssl dgst -sha1 -verify pubkey-A.pem -signature signature.bin symkey.pem

● -verify pubkey-A.pem ➝ verify the signature using the


specified filename
● -signature signature.bin ➝ specifies the signature to
be verified
● symkey.pem ➝ the file to be hashed
OpenSSL
Step 5.a - Alice encrypts her largefile.txt with the symmetric key

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 function
that
derives one or more secondary secret keys from a primary secret key)
OpenSSL
Step 5.b - Bob decrypts ciphertext.bin with the same symmetric key

openssl enc -aes-256-cbc -d -pass file:symkey.pem -p -md sha256 – in ciphertext.bin -out largefile.txt

● -d ➝ decryption flag

You might also like