SSL Manual

 

Table of contents

1Preface
1.1Certificates explained
1.2Signing explained
1.3Single Sign On
1.4SSL explained
2Install
3Create root certificate
4Certificate request
5Sign a certificate
6Revoke a certificate
7Renew a certificate
8View a certificate
9Apache 2.0 howto
9.1Certificate request
9.2Setup SSL
10Outlook email howto
10.1Sign an email
10.2Encrypt an email
11Converting certificates
12Combined client certificate and Basic authentication with Apache and PHP

1 - Preface

This guide explains how to setup a root certification authority (CA) on Linux, and how to implement apache ssl on Linux.

A Certification Authority (CA) holds a selfsigned key that is able to sign other keys. Large commercial CA's like Verisign use this exact same procedure as described in this document. Only these commercial CA's have installed there root certificate within the most browsers, so you don't have to manualy install the root certificate of Verisign. Microsoft and Netscape did already do this for us.

You can use a Verisign certificate for your own HTTPS server, but this is very expensive. But we can create our own CA, the only thing your visitors have to do is install your root certificate. We can use our CA to sign any certificate, like for HTTPS, SSH, VPN, E-Mail and any other application that can use a certificate.

1.6 - Certificates explained

Certificates are a way to encrypt and decrypt information without first discussing the encryption key and authenticating the identity of the sender. In the past when someone wanted to send an encrypted message to another person, both the sender and the receiver had to agree on a secret encryption key. They had to find a way to secretly pass the encryption key to each other.

E.g. Bob wants to send Alice a message. Bob and Alice agree to send the words in the message backwards (ok, this is easy to hack, but it's just an example). Bob send "olleh" to Alice. Alice knows that the message must be reversed to decrypt it, so Alice reads "hello". Mallod is listening to the messages between Bob and Alice does not understand the message since he reads only the "olleh" message, and he does not know the secret encryption key Alice and Bob have agreed on. (Ok, Mallod must be a complete idiot not to guest the key...).

The big disadvantage of this method is that both the sender and receiver must know the exact encryption key before they can start communicating. And how can you be sure that nobody else will 'hear' what you agree on.

Modern methods of encrypting data is based on key pairs. A key pair consists of a private key and a public key. Messages encrypted by a private key can only be decrypted by its public key, and visa versa. The way this is done is an extreem complex mathematical algoritm which is based on prime numbers.
So know if Bob wants to send Alice a message, Bob has to encrypt the message with the public key of Alice. Now only Alice can decrypt the message since she is the only one who knows the private key. if Mallod was not as stupid as we thought before, he could try to hack the encrypted message by trying a random key. The most keys used on the internet contain 1024 bytes key. So Mallod has a chance of 1 to 1,208,925,819,614,629,174,706,176.
Ofcource Alice has to publish her public key so Bob can use it. But Alice doesnot have to be afraid that anyone can misuse he public key. Without the private key, the public key is useless for any hacker.
if Alice wants to send a message back to Bob, she has to use Bob's public key to encrypt the message. if Alice uses her private key to encrypt the message, Bob can also decrypt the message using Alice's public key, but also can everyone else!
Certificates

1.7 - Signing explained

With only a certificate we can encrypt and decrypt information, but sometimes you only want to know is the person you are talking with is realy the person that is tells you he is. The person can encrypt data and send it to you. if you have his public key, you know only he could encrypt data. But where did you get his public key from? E.g. on HTTPS the server itself sends you its public key when you make your initial contact.

To make sure that a person is really the person he says he is, you would like to ask someone who you can trust. This is exact what signing is about. A certificate can be signed by a Certification Authority (CA). A CA is nothing more' than an orginazion that gives out a signature saying that the holder of the signature is checked by this organization. When you connect to a bank using HTTPS, the bank server will send you its public key at the initial connection. But this public key is signed by a CA. So this public key holds an encrypted message from the CA, which is encrypted using the CA's private key. Your browser uses the CA's public key to decrypt this CA's message in the banks certificate. The CA's message contains some information about the banks server, e.g. the domain name. Your browser checks this information with the banks server. E.g. the CA's message tells you that the banks domain name is www.bank.com, so if the banks server suddenly uses www.bogus.com, you will know that the 'misconceived' bank server is now the server it tells you it is.

1.8 - Single Sign On

Signing can not only be used to authenticate servers, but can also be used to authenticate a client or user. When a number of servers are used for 1 single company or task. It would be very unpleasant to login on all servers individually. You can create your own personal certificate pair and sign them at a CA. Now you can store your personal public key in all the servers and tell those servers that this key belongs to your login code. Now when you connect to one of these servers, you send your public key encrypted using your private key to the server instead of sending your username and password. The server decrypts your encrypted public key using your private key and checks this key against the stored key. if the keys match, you are logged in.
An other way to handle a larger group of users is to configure the server so that it will accept all users with a personal key that is signed by a specific CA. Normally this is a special CA and not a public one. So only the CA has to check that a users is valid to use a specific application. The server checks the CA to see if a user is permitted to connect.
This is where directory services (like Microsofts Active Directory) come in handy.
Single Sign On

1.9 - SSL explained

The basic functionality of SSL (Secured socket layer) is to:
  • Encrypt confidential information to ensure that only the right receiver can read the information.
  • Verify the data integrity so that the receiver is sure the data is not modified.
  • Authenticate the sender so the receiver is certain the data came from the sender.
A certificate can also be used to sign an other certificate. The first certificate in the chain is called the root certificate. This root certificate is a selfsigned certificate. Other certificates are signed using this root certificate.

The process for generating a certificate that is signed by a root certificate, you will have to follow the next procedure:
  • Generate a private key
  • From the private key, generate a certification request for the CA
  • The CA signs the public key in the certification request
  • Install the signed public key
Certificates are only valid for a period of time, mostly 1 year. After this period the certificate has to be renewed. It is also possible that the CA revokes the certificate. This means that the certificate is no longer valid althought it is not passed the expiring date.

2 - Install

3 - Create root certificate

The root certificate is used to authenticate all other certificates that are generated using the root certificate. This way only the root certificate must be installed in e.g. the Internet browser and all other certificates are than authenticated.
The root certificate is made up by 2 keys, just like any other certificate. The root certificate is a self signing key, exept that the DN (common name) is not the website but an describing name (e.g. Schaake root Certificate).
The cakey.key is the private key and must be kept save, since this will be the server key to sign other keys. And you don't want others to be able to sign your keys!
The cacert.crt is the public key, this key must be installed in a visitors browser. So publish this key on your website.
openssl req -new -x509 \
	-keyout /var/ssl/private/cakey.key \
	-out /var/ssl/certs/cacert.crt -days 3650
cp /var/ssl/certs/cacert.crt /var/www/html/ssl
The first command generates the selfsiging key request and will generate the server and public keys. The copy command copies the public key to the website (apache 2.0 on Redhat). Create a hyperlink to the key at your web pages.

The option days will set the number of days the certificate will remain valid. Normally we would like to set it to only 1 year. But we don't want our visitors to install a new root certificate every year. And we also don't want to create new certificates for all services every year. So only for the root certificate, set the expiring period to 10 years.

4 - Certificate request

Most applications can create there own certification request, but we can also do this using the openssl command. On the common name (CN) enter the hostname, email address or application name that is going to use this certificate. You have to supply the exact name in the DN field, since this is the only field that is checked to see if the certificate belongs to this source.
openssl req -new -keyout /var/ssl/private/www-schaake-nu.key \
	-out /var/ssl/requests/www-schaake-nu.csr -days 365
The .key file is the private key (keep this save). The .csr file is the certification request. This includes the public key and all other information about the request.
Normally you would send this file to the CA.

5 - Sign a certificate

openssl ca -out /var/ssl/certs/www-schaake-nu.crt \
	-infiles /var/ssl/requests/www-schaake-nu.csr

6 - Revoke a certificate

openssl ca -revoke /var/ssl/certs/www-schaake-nu.crt
openssl ca -gencrl -out /var/ssl/crl/schaake-ca.crl
cp /var/ssl/crl/schaake-ca.crl /var/www/html/ssl
cp /var/ssl/crl/schaake-ca.crl /etc/httpd/conf/ssl.crl

7 - Renew a certificate

8 - View a certificate

openssl x509 -in /var/ssl/certs/www-schaake-nu.crt -noout -text

9 - Apache 2.0 howto

9.1 - Certificate request

openssl genrsa 1024 > /etc/httpd/conf/ssl.key/www-schaake-nu.key
openssl req -new -key /etc/httpd/conf/ssl.key/www-schaake-nu.key \
	-out /etc/httpd/conf/ssl.csr/www-schaake-nu.csr
cp /etc/httpd/conf/ssl.csr/www-schaake-nu.csr /var/ssl/requests

9.2 - Setup SSL

10 - Outlook email howto

10.1 - Sign an email

openssl pkcs12 -export -in /var/ssl/certs/postmaster@schaake-nu.crt \
	-inkey /var/ssl/private/postmaster@schaake-nu.pem \
	-out /var/ssl/certs/postmaster@schaake-nu.p12 \
	-name "Schaake Postmaster"

10.2 - Encrypt an email

11 - Converting certificates

To convert a private key from PEM (default openssl format) to DER (default MS format)
openssl rsa -in /var/ssl/private/www-schaake-nu.key \
		-out /var/ssl/private/www-schaake-nu.der \
		-outform DER

To convert a certificate from PEM (default openssl format) to DER (default MS format)
openssl x509 -in /var/ssl/certs/www-schaake-nu.crt 
		-out /var/ssl/certs/www-schaake-nu.der 
		-outform DER

12 - Combined client certificate and Basic authentication with Apache and PHP

This chapter describes how to setup client certificate authentication combined with Basic Authentication when no valid client certificate was given.
First setup a Root CA and Apache SSL before you can continue.

Now create a client certificate. First create a certificate request (as shown in a previous chapter). Only set the CN (or Common Name) to a user name you want the certificate to be used for. Sign the certificate, and now you have a private and public pair for your client certificate.
for some reason all webbrowsers only accept pk12 certificates, so you have to convert the certificates using the next command.
openssl pkcs12 -export -in /var/ssl/certs/cc-test.crt \
	-inkey /var/ssl/private/cc-test.pem \
	-out /var/ssl/certs/cc-test.p12 \
	-name "Test Client Certificate"
Now you can import this client certificate in any webbrowser.

Next we have to configure is the web server. for our test we will use a specific directory for testing purposes. So make add the next location in you httpd.conf, we are going to use https://www.webserver.com/cert as testing area.
<Location /cert>
        # Client authentication stuff
        SSLVerifyClient optional	# Can be optional or required
        SSLVerifyDepth 2	
        SSLOptions +FakeBasicAuth	# I'm not sure about this option
</Location>
	
Now we can connect to this directory with our certificate. if things won't work, check your server logfiles. And make sure you have configure you server for SSL. (TIP: Check your servers DNS name, this must be the same as in the CA certificate use for signing your client certificate).

Now we only have to build a script to check if the certificate is valid, and if no certificate was provided, do basis authentication.
<? 
// Check if and how we are authenticated
if ($_SERVER['SSL_CLIENT_VERIFY'] != "SUCCESS") { // Not using a client certificate
	if ((!$_SERVER['PHP_AUTH_USER']) && (!$_SERVER['PHP_AUTH_PW'])) { // Not logged in using basic authentication
		authenticate(); // Send basic authentication headers
	}
}


if ($_SERVER['SSL_CLIENT_S_DN_CN'] != "chris") { // Check CN name of cert

	if (!(($_SERVER['PHP_AUTH_USER'] == "test") && ($_SERVER['PHP_AUTH_PW'] == "123"))) { // Check username and password
		authenticate(); // Send basic authentication headers because username and/or password didnot match
	} 
} 

phpinfo();

// ------------------------------------------------------------------------
// authenticate()
//
// Call authentication display
// ------------------------------------------------------------------------

function authenticate() {
	Header("WWW-Authenticate: Basic realm=Website");
    	Header("HTTP/1.0 401 Unauthorized");
    	error401();
    	exit;
}
?>
What you will have to include yourself is:
  • Certificate to user mapping
  • Username/password checking
A good solution would be an LDAP directory for both tasks. And you have to make sure your users can easilly revoke there certificate when it has being stolen. Apache will take care of the certificate checking including the check if the certificate was revoked, if configured correctly...
A number of browsers can have an additional password on the certificate, so a user will have to enter a password when the server requists the client certificate. But this is an optional service, so don't depend on it.
You could also do a client certification check and a username password check, just mixup the script...