Computer and Network Security: Practical Session
Computer and Network Security: Practical Session
SECURITY
Practical session
1
Outline
• Encryption using DES
• Encryption using RSA
• Combination of DES and RSA
• Digital Signature
• Digital Certificate
2
DES, RSA and Certificates with openSSL
1. Presentation of OpenSSL
Protocol SSL
• The SSL protocol (Secure Socket Layer) was developed by
Netscape to allow client/server applications to
communicate safely.
4
DES, RSA and Certificates with openSSL
1. Presentation of OpenSSL
openSSL
• openSSL is a toolbox for cryptographic material
implementing SSL and TLS. It gives:
1. A library to program in C allowing to construct client/server
applications using SSL/TLS
2. A command line (openssl) allowing
– Creation of RSA, DSA keys
– Creation of X509 certificates
– Digest computation (MD5, SHA, …)
– Ciphering and Deciphering (DES, IDEA, RC2, RC4, Blowfish …)
– Tests of client/server SSL/TLS
– Signature and ciphering of mails (S/MIME) Secure Multi- 5
Purpose Internet Mail Extension
DES, RSA and Certificates with openSSL
1. Presentation of OpenSSL
openSSL
• To know everything about OpenSSL: man openssl
• The general syntax of openssl is:
openssl> <command> <options>
6
DES, RSA and Certificates with openSSL
2. Symmetric encryption with openSSL
Basic commands
• To encrypt a file with openssl using a DES encryption:
openssl> enc –des3 –in file –out file2
• The result is in the file file2.
• To decrypt the same file:
openssl> enc –des3 –d –in file2 –out filedecrypted
7
DES, RSA and Certificates with openSSL
2. Symmetric encryption with openSSL
8
DES, RSA and Certificates with openSSL
2. Symmetric encryption with openSSL
Create and print keys
• To create a symmetric key:
openssl> enc –des3 –P
• This command asks for the password.
• It generates a key, starting from a password and a random
“salt”.
– This salt is there to scramble the password.
• This command prints the used salt, the generated key and an
initialization vector (iv) to be used with the key for
encryption.
9
DES, RSA and Certificates with openSSL
2. Symmetric encryption with openSSL
Create and print keys
• This command encrypt with DES3, the file file1 to the file
file2, using the key key and the initialization vector
vector.
Openssl> enc –des3 –in file1 –out file2 –k key –
iv vector
Remarks:
• You can use directly openssl des3 (instead of openssl enc
–des3)
• You can use base64 instead of des3.
– The file is then not ciphered, since base64 is a coding system (clear text)
allowing data to be independent of any architecture (useful when data are
10
sent between different computers, OSs, networks,…).
DES, RSA and Certificates with openSSL
3. RSA with openSSL
Generating key pairs
• To create a pair of keys, the genrsa command is used:
openssl> genrsa size
• Here, size is the size of the key.
• To save this key in keyfile.pem, use the option: -out
keyfile.pem
openssl> genrsa -out keyfile.pem size
11
DES, RSA and Certificates with openSSL
3. RSA with openSSL
Visualizing RSA keys
• The command rsa allows to visualize the content of a file
(PEM format) containing a RSA key pairs.
openssl> rsa –in keyfile.pem –text –noout
12
DES, RSA and Certificates with openSSL
3. RSA with openSSL
Visualizing RSA keys
13
DES, RSA and Certificates with openSSL
3. RSA with openSSL
Ciphering the key file
• In the file, the private key is in clear text and could be
extracted.
• It is necessary to encrypt it.
– It can be done at the generation of the key (genrsa command),
or
– at any time with the rsa command.
15
DES, RSA and Certificates with openSSL
3. RSA with openSSL
Exporting the public key
• The public key should be extracted from the file (encrypted
file or not), since this public key should be transmitted to
anyone.
16
DES, RSA and Certificates with openSSL
3. RSA with openSSL
Visualizing the public part
17
DES, RSA and Certificates with openSSL
3. RSA with openSSL
Ciphering data with RSA
• To cipher data with RSA key, use the command rsautl (RSA
utile functions):
Openssl> rsautl –encrypt –in inputfile –inkey
keyfile.pem –out outputfile
• The inputfile is the file to encrypt (-encrypt).
• Caution: The file should not be too large for the key (116 bits
for a 1024 bits key).
• The keyfile.pem contains the RSA key.
• If only the public key is in the file, the option –pubin must
be used.
18
DES, RSA and Certificates with openSSL
3. RSA with openSSL
Ciphering data with RSA
• To decrypt, replace the option -encrypt with -decrypt.
• Then the keyfile must contain the private key.
19
Practical session 1
Encrypting a file using a DES
20
Encrypting a file using a DES algorithm
• Step 1: create a file name abe using:
[hen@localhost ~] cat >abe
Welcome to Jimma
• Then, change the command line into openssl using:
[hen@localhost ~] openssl
• Step 2: encrypt the file name abe into kebe using DES:
openSSL> enc –des3 -in abe -out kebe
• It will then request you to enter password:
enter des-ede3-cbc encryption password: 123456
verifying - enter des-ede3-cbc encryption
password: 123456
• Now, the decrypted file named with selam has been created.
• To see the decrypted file selam, change the command line:
[hen@localhost ~] cat selam
Welcome to Jimma
22
Creating and printing keys (DES)
• Step 1: To create a symmetric key, use:
openssl> enc –des3 -P
enter des-ede3-cbc encryption password: 123456
verifying - enter des-ede3-cbc encryption
password: 123456
salt=CB832CAA53360439
key=065BD1FC9A761790B53F1410B3372176D99F06FBEC3FB
7F3
iv=5DA7C1A98C9908DB
Step 2: create a new file named hen
[hen@localhost ~] cat >hen
Hello Security World
• Then, change the command line into openssl using:
[hen@localhost ~] openssl
23
Creating and printing keys (DES)
• Step 3: use the key(k) and initialization vector(iv)
to encrypt filename hen to the file name mar with DES3:
openssl>enc –des3 –in hen –out mar –k
065BD1FC9A761790B53F1410B3372176D99F06FBEC3FB7F3
–iv 5DA7C1A98C9908DB
• To decrypt the encrypted file mar into other file name mesr:
openssl> enc –des3 –d –in mar –out mesr –K
065BD1FC9A761790B53F1410B3372176D99F06FBEC3FB7F3
–iv 5DA7C1A98C9908DB
• To see the content of mesr
[hen@localhost ~] cat mesr
Hello Security World
24
Practical session 2
Encrypting files using RSA
25
Encrypting file using RSA
• Step 1: Create a pair of keys:
openssl>genrsa 512
Generating RSA private key, 512 bit long modulus
………..++++++++
……………++++++++
e is 65537(0x10001)
------BEGIN RSA PRIVATE KEY------
MIIB0QI….
------END RSA PRIVATE KEY------
26
Encrypting file using RSA
• Step 2: save the key in file name privatekey.pem
openssl>genrsa -out privatekey.pem 512
Generating RSA private key, 512 bit long
modulus
..+++++++++++++++
………………….++++++++++
e is 65537 (0x10001)
27
Encrypting file using RSA
• Step 3: To visualize the content of privatekey.pem
containing RSA key pairs,
openssl>rsa –in privatekey.pem -text -noout
Private-Key: (512 bit)
Modulus:
00:d9:e0:58: ...
publicExponent: 65537 (0x10001)
privateExponent:
6b:11:72: …
prime1:
00:f5: …
prime2:
00:e2: …
exponent1:
00:cd: …
exponent2: 28
Encrypting file using RSA
• Step 4: Encrypt the private key using the following command:
openssl>rsa -in privatekey.pem -des3 -out
privatekey_encrypted.pem
writing RSA key
Enter PEM passphrase: 123456
Verifying – Enter PEM pass phrase: 123456
29
Encrypting file using RSA
• Step 5: Extract the public key from the encrypted file
privatekey_encrypted.pem,
openssl>rsa -in privatekey_encrypted.pem –
pubout –out publickey.pem
Enter pass phrase for
privatekey_encrypted.pem:123456
writing RSA key
31
DES, RSA and Certificates with openSSL
Exercise 1:
• Cipher a file with a symmetric algorithm of your choice
(DES). Cipher the key with public key of your left/right
neighbor (ask him on USB key if you don’t have it, RSA).
Send him the encrypted file and the encrypted
symmetric key.
32
Signature of files
• Only small document can be signed. Therefore, we have to
compute the hash of the documents, using the dgst
command:
openssl> dgst hashfunction –out fingerprint
inputfile
• where hashfunction is the hash function to use.
– It can be MD5 (-md5 option) for 128 bits fingerprint, SHA1 (-sha1) for
160 bits fingerprint,
• To sign a document, we sign the fingerprint with the
command rsautl and the option –sign:
openssl> rsautl –sign –in fingerprint –inkey
privatekey.pem –out signature
33
Signature of files…
• To verify a signature, we use the option –verify:
openssl> rsautl –verify –in signature –pubin –
inkey publickey.pem –out fingerprint
• Note: the –pubin option is used to use the public key.
34
Signature of files
• Step 2: compute the hash function of the file named as
signdemo using dgst command:
openssl> dgst -md5 -out fingerprint signdemo
• Step 3: write the following command to see the fingerprint
[hen@localhost ~] cat fingerprint
MD5(signdemo) =7b36eeb717bc9b8b19b4c35cbc4ed0d5
• Step 4: To sign the document signdemo, sign the
fingerprint, by using rsautl command and the option –
sign:
openssl> rsautl –sign –in fingerprint –inkey
privatekey.pem –out signature
35
Signature of files
• Step 5: verify the signature, use the option –verify:
Openssl> rsautl –verify –in signature –pubin –
inkey publickey.pem –out fingerprint
Exercise 1
• Create three messages. Sign all of them. Slightly
modify one or two of them, and send them to your lab
partner, together with the signatures. Ask him/her to
determine which messages were modified.
36
Signature of files
Exercise 2
1. Create a text file
2. Compute message digest functions with MD5
3. Change the text
4. Compute message digest functions again with
MD5
5. Compute message digest functions with SHA-1
37
Digital Certificates
• Secure Socket Layer (SSL) protocol was created by Netscape to
ensure secure transactions between web servers and browsers.
• The protocol uses a third party, a Certificate Authority (CA), to
identify one end or both end of the transactions. This is in short
how it works.
1. A browser requests a secure page (usually https://).
2. The web server sends its public key with its certificate.
3. The browser checks that the certificate was issued by a trusted party
(usually a trusted root CA), that the certificate is still valid and that
the certificate is related to the site contacted.
4. The browser then uses the public key, to encrypt a random symmetric
encryption key and sends it to the server with the encrypted URL
required as well as other encrypted http data.
38
Digital Certificates
• Secure Socket Layer (SSL) protocol was created by Netscape to
ensure secure transactions between web servers and browsers.
• The protocol uses a third party, a Certificate Authority (CA), to
identify one end or both end of the transactions. This is in short
how it works.
5. The web server decrypts the symmetric encryption key using its
private key and uses the symmetric key to decrypt the URL and http
data.
6. The web server sends back the requested html document and http
data encrypted with the symmetric key.
7. The browser decrypts the http data and html document using the
symmetric key and displays the information.
39
Certificates
• Preparation: Prepare a key pair (length 1024 bits), protected
by a password.
• In the following, the file name keypair.pem will be supposed
to hold your key pair.
40
Certificates
• The command req allows to build a request for a certificate:
openssl> req –new –key keypair.pem –out
myrequest.pem
• The result is a request, also in the PEM format (check it !).
• The private key is used to build the signature of this request, in
order to authenticate the issuer of the request with its public
key that is in the request.
• It is possible to see the content of the request with the –text
–noout options (like before!):
openssl> req –in myrequest.pem –text –noout
• Note: This request is NOT a certificate.
41
Certificates
• Request for certificates demo:
• Step 1: Create a keypair.pem using asymmetric RSA.
openssl> genrsa -out keypair.pem 1024
• Step 2 (optional): protect keypair.pem using DES3 into
kpair.pem
openssl> rsa –in keypair.pem –des3 –out
kpair.pem
Writing RSA key
Enter PEM pass phrase: 123456
Verifying - Enter PEM pass phrase: 123456
• Step 3: extract the public key from the encrypted file
kpair.pem
openssl> rsa -in kpair.pem –pubout –out
publickeynew.pem 42
Certificates
• Request for certificates demo:
• Step 4: create request for certification:
openssl> req –new –key keypair.pem –out
myrequest.pem
Enter pass phrase for keypair.pem: 123456
……
Country Name (2 letter code) [GB]: ET
State or province name (full name): Adddis Ababa
Locality name: Kolfe Keranio
Organization name: AAU
Organizational unit name: AAIT
Common name: Henock
E-mail address: [email protected]
Challenge password: 123456
Optional company name: NGUC 43
Certificates
• Request for certificates demo:
• Step 5: see the content of the request
openssl> req –in myrequest.pem –text –noout
Certificate request:
Data:
Version: 0 (0x0)
Subject: C=et, ST=Addis Ababa, L=Kolfe Keranio,
O=AAU, OU=AAIT,
CN=Henock, e-mail address:
[email protected]
Subject: public key info:
Public key algorithm = rsaEncryption
RSA public key: (1024 bit):
44
Certificates
• Request for certificates demo:
Modulus (1024 bit):
00:d2: …
Exponent: 65537 (0x1024)
Attributes:
• unstructuredName: aau
• challengedpassword: 123456
• Signature Algorithm: shalWithRSAEncryption
• 9f:65: …
45
Creation of a certificate
• This one is done by the CA. You are the CA for this work.
• To create and sign a certificate from a user request, the CA is
invoking the x509 command:
openssl> x509 –days 356 –CAserial Addis.srl –CA
certificatAddis.pem –CAkey pkca.pem –in
userrequest.pem –req –out
CertificateSignedByCA.pem
• The options:
– -days 365: gives the TTL of the certificate
– -CAserial Addis.srl: gives the serial number of the
certificate to create (in a file)
– -CA certificateAddis.pem specifies the CA certificate
– -CAkey specifies the private key of the CA
– -in userrequest.pem: gives the file containing the request
for a certificate, sent by a user to this CA. 46
Creation of a certificate…
• Exercise 5: Create and sign a certificate from a user request.
• Step 1: create addis.srl
[hen@localhost ~]cat>addis.srl
123456
openssl> x509 -days 1000 -req –signkey
privatekeyCA.pem –in myrequest.pem –out
henockcertificate.pem
Signature ok
Subject=/C=et/ST=Addis Ababa/L=Kolfe
Keranio/O=AAU/OU=AAIT/CN=Henock/emailaddress=heno
[email protected]
Getting Private Key
CertificateSignedByCAH.pem:/C=ET/ST=AddisAbaba/L=K
olfeKeranio/O=AAU/OU=AAIT/CN=Henock/emailaddress=h
[email protected]
Self signed certificate
OK
Exercise
50
Self-Signed Certificate: Example 2
//Generate a 2048 bit RSA private key
Openssl> genrsa -out key.pem 2048
//Generate a Certificate Signing Request
openssl> req -new -sha256 -key key.pem -out csr.csr
//visualize the certificate signing request
Openssl> req -in csr.csr -text -noout
//Generate a self-signed x509 certificate
Openssl> req -x509 -sha256 -days 365 -key key.pem -
in csr.csr -out certificate.pem
//visualize the self-signed certificate
Openssl>x509 –in certificate.pem –text -noout
51
Self-Signed Certificate: Example 3
Step - 1: Generate RSA private key (4096- bit) for root CA and store
it in rootca.key
openssl> genrsa –out rootca.key 4096
Step - 2: Create Self-signed root CA certificate rootCA.crt
openssl> req –new –x509 –days 365 –key rootca.key –
out rootCA.crt
Step – 3: Create intermediate certificate CA, which will be used for
actual signing. First generate the key
openssl> genrsa –out imca.key 2048
Step – 4: Request a certificate for this intermediate CA
openssl> req –new –key imca.key –out imCA.csr
52
Self-Signed Certificate: Example 3…
Step - 5: Process the request for the intermediate CA certificate and
get it signed by the root CA.
openssl> x509 –req –days 365 –in imCA.csr –CA
rootCA.crt –CAkey rootca.key –set_serial 01 –out
imCA.crt
53