Introduction to Certificates and OpenSSL

Alexandre Anriot <aanriot@bsdcow.org>
Updated: $BSDcow: openssl.html.en,v 1.2 2005/03/29 21:31:27 aanriot Exp $

Introduction

What is SSL?

This protocol provides mechanisms to send encrypted datas over a TCP connection, thanks to public-key technology. SSL, and also TLS are composed by several phases, like the negotiation for the algorithm to use, the authentication or the symmetric encrypted traffic.

Here is the kinematic of the SSL Handshake:

  1. -> Client Hello
  2. <- Server Hello
  3. <- Server Certificate
  4. <- Certificate Request
  5. -> Session key
  6. -> Client Certificate
  7. -> Client Done
  8. <- Server Done

What are Certificates?

To begin with, the goal of certificates is to trust a public key. That's why the creation of a certificate requires the intervention of a Certification Authority. Certification Authorities (CA) like CAcert, Thawte and others trust about 99% of Internet's traffic.

When a certification request is done, the CA checks informations provided by the client before validating the certificate. For this, it "hach" and signs the certificate with its private key. So, you just need to get its public key (largely distributed) to verify the validity of the certificate. Thus, when a user wants to communicate, he just need his certificate which contains his name and public key. It has been signed by the CA and the validity can be checked. Certificates can be used in a lot of applications/protocols, like SSL/TLS, IPSec etc. Several standards exist, like PKCS or X.509.

A certificate is defined in ASN1 (Abstract Syntax Notation 1), an OSI notation used to specify abstract datas. It can be accompanied by a CRR (Certificate Revocation Request, can be in a CRL, Certificate Revocation List), or a CSR (Certificate Signing Request). There are two encoding rules for certificates and keys, the BER (Basic Encoding Rules) and the DER (Distinguished Encoding Rules). PEM (Privacy Enhanced Mail) is an IETF standard, and is used by applications like email clients to encode certificates and keys in ASCII. It uses base-64 encryption.

X.509 encoding

The last version (the 3rd) contains extensions which improve the flexibility of them.

Each certificates contains information about the user and the certification authority:

PKCS encoding

PKCS#12 (Personal Information Exchange Syntax Standard) contains several things:

To export a certificate to PKCS:
$ sudo openssl pkcs12 -export -in cert.pem -inkey private.pem
  -certfile ca/cacert.pem -out pkcs_cert.p12

OpenSSL

Here is a few commands, needed if you want to play with OpenSSL and Certs, and/or construct a PKI.

Certificate creation

Here is a few commands to create a certificate.

To create a key:
$ sudo openssl genrsa -out ca.key 1024

To create a certification request:
$ sudo openssl req -new -key /etc/ssl/private/ca.key -out /etc/ssl/private/ca.csr

To create the certificate:
$ sudo openssl x509 -req -days 365 -in /etc/ssl/private/ca.csr
  -signkey /etc/ssl/private/ca.key -out /etc/ssl/ca.crt

Certificate display

x509 file: $ sudo openssl x509 -in mycert.pem -noout -text
DER file: $ sudo openssl x509 -inform DER -in file -noout -text
PEM file: $ sudo openssl x509 -inform PEM -in file -noout -text
PKCS#12 file: $ sudo openssl pkcs12 -in file

Certificate renew

$ sudo openssl ca -config /etc/openssl.cnf -policy policy
  -out newcert.pem -infiles new.pem
  -startdate [actual date] -enddate [actual date + 365]

Key signing

$ sudo openssl ca -infiles key.pem -out cert.pem

Certificate revocation with CRLs

To revoke a certificate, you have to do this properly and update the crl:
$ sudo openssl -revoke mycert.pem
$ sudo openssl ca -gencrl -config /etc/openssl.cnf -out crl/mycert.crl

You can look at the revocation list, wich is a list of revoked certificates serial numbers, generated by the Certification Authority, with:
$ sudo openssl crl -in crl.pem -text -noout -CApath /etc/ssl/trusted

Several ways exist to publish CRLs. The most commonly used way is to allow users to download the CRL, but it requires that the user often fetch it, like on a HTTP webserver. To solve this problem, the OCSP protocol has been created (RFC 2560).

Certificate revocation with OCSP

From the index.txt database, you can create an oscp server:
$ sudo openssl ocsp -index index.txt -port 8080 -CA cacert.pem   -text -rsigner new.pem -rkey new.key

You can use it with a web browser or with an OpenSSL client:
$ sudo openssl ocsp -issuer ca.pem -CAfile cacert.pem   -cert new.pem -url http://localhost:8080 -text

Fingerprints

MD5: $ sudo openssl x509 -fingerprint -in cert.pem -noout -sha1
SHA1: $ sudo openssl x509 -fingerprint -in cert.pem -noout -md5

Examples

Example 1, become a CA

In order to use some services like HTTPS, ISAMKPd etc. you need to use a Certification Authority, like Thawte or CAcert. You can also sign yourself and become your own authority...

To begin with let's generate our RSA key:
$ cd /etc/ssl/
$ sudo openssl genrsa -out ca.key 1024

To create a certification request corresponding to the key, you're going to be asked some information.

Be careful to answer with a correct Distinguished Name (hostname):
$ sudo openssl req -new -key /etc/ssl/private/ca.key -out /etc/ssl/private/ca.csr

Edit /etc/ssl/openssl.cnf and change countryName, stateOrProvinceName, localityName, organizationName, organizationalUnitName, commonName, emailAddress, challengePassword, unstructuredName, nsComment with your personal settings.

Eventually, create a X.509 certificate.
$ sudo openssl x509 -req -days 365 -in /etc/ssl/private/ca.csr
  -signkey /etc/ssl/private/ca.key -out /etc/ssl/ca.crt

Check your certificate with :
$ openssl x509 -in ca.crt -text -noout

You're now able to sign your proper keys.

Example 2, HTTPS setup on a webserver

Generate a 1024 bits RSA key :
$ sudo openssl genrsa -out /etc/ssl/private/httpd.key 1024

Create a signed certification request :
$ sudo openssl req -new -key /etc/ssl/private/httpd.key
  -out /etc/ssl/private/httpd.csr

If you want to sign it yourself :
$ sudo openssl x509 -req -days 365 -in /etc/ssl/private/httpd.csr
  -signkey /etc/ssl/private/ca.key -out /etc/ssl/httpd.crt

Example 3, TLS setup on a mailserver

Generate a 1024 bits RSA key :
$ sudo openssl dsaparam 1024 -out dsa1024.pem

Create a signed certification request and sign it:
$ sudo openssl req -x509 -nodes -days 365 -newkey dsa:dsa1024.pem
  -out /etc/mail/certs/mycert.pem -keyout /etc/mail/certs/mykey.pem

Note: the -nodes option is needed because MTAs deal with unciphered private keys.

Crypto devices

Personally, I have a vpn1401 device, working in my Soekris Net4501. If you have one of this kind of devices, you can look at hifn(4). You can test it with the following examples. Firstly, we'll try to work without the card.

$ sudo sysctl kern.usercrypto
kern.usercrypto=0

$ sudo openssl speed -evp aes-256-cbc
To get the most accurate results, try to run this
program when this computer is idle.
Doing aes-256-cbc for 3s on 16 size blocks: 118091 aes-256-cbc's in 2.99s
Doing aes-256-cbc for 3s on 64 size blocks: 31088 aes-256-cbc's in 2.95s
Doing aes-256-cbc for 3s on 256 size blocks: 7859 aes-256-cbc's in 2.98s
Doing aes-256-cbc for 3s on 1024 size blocks: 1959 aes-256-cbc's in 2.98s
Doing aes-256-cbc for 3s on 8192 size blocks: 242 aes-256-cbc's in 2.93s
OpenSSL 0.9.7d 17 Mar 2004
built on: date not available
options:bn(64,32) md2(int) rc4(idx,int) des(ptr,risc1,16,long) aes(partial) blowfish(idx)
compiler: information not available
available timing options: USE_TOD HZ=100 [sysconf value]
timing function used: getrusage
The 'numbers' are in 1000s of bytes per second processed.
type 16 bytes 64 bytes 256 bytes 1024 bytes 8192 bytes aes-256-cbc 631.46k 675.53k 674.15k 672.17k 676.68k

Afterwards, we activate kern.usercrypto :-)

$ sudo sysctl kern.usercrypto=1
kern.usercrypto: 0 -> 1

$ sudo openssl speed -evp aes-256-cbc
To get the most accurate results, try to run this
program when this computer is idle.
Doing aes-256-cbc for 3s on 16 size blocks: 6343 aes-256-cbc's in 0.16s
Doing aes-256-cbc for 3s on 64 size blocks: 6281 aes-256-cbc's in 0.09s
Doing aes-256-cbc for 3s on 256 size blocks: 6055 aes-256-cbc's in 0.17s
Doing aes-256-cbc for 3s on 1024 size blocks: 4919 aes-256-cbc's in 0.07s
Doing aes-256-cbc for 3s on 8192 size blocks: 1747 aes-256-cbc's in 0.05s
OpenSSL 0.9.7d 17 Mar 2004
built on: date not available
options:bn(64,32) md2(int) rc4(idx,int) des(ptr,risc1,16,long) aes(partial) blowfish(idx)
compiler: information not available
available timing options: USE_TOD HZ=100 [sysconf value]
timing function used: getrusage
The 'numbers' are in 1000s of bytes per second processed. type 16 bytes 64 bytes 256 bytes 1024 bytes 8192 bytes aes-256-cbc 649.52k 4287.83k 9018.65k 71637.62k 305310.38k

$BSDcow: openssl.html.en,v 1.2 2005/03/29 21:31:27 aanriot Exp $
Please email any comments, questions, corrections or suggestions to aanriot@bsdcow.org.
Valid XHTML