Creating and Using SSL Certificates
This document describes how to establish yourself as a root certificate authority (root CA) using the OpenSSL toolset. As a root CA, you are able to sign and install certificates for use in your Internet server applications, such as Apache and Stunnel. Taken from www.eclectica.ca/howto/ssl-cert-howto.php Copyright © 1996, 2003 Marcus Redivo. All rights reserved. Researched and written by Marcus Redivo.Permission to use this document for any purpose is hereby granted, providing that the copyright information and this disclaimer is retained. Author accepts no responsibility for any consequences arising from the use of this information.
- Creating and Using SSL Certificates
- Scope
- Quick Start
- Background
- Prerequisites
- Initial Setup
- Creating a Root Certificate
- Creating a Certificate Signing Request (CSR)
- Signing a Certificate
- Installing the Certificate and Key
- Distributing the CA Certificate
- Renewing Certificates
- Getting a Commercially Signed Certificate
- Publishing Your CA Certificate
- Summary
- References
Scope
This document covers a very specific, limited purpose, but one that meets a common need: preventing browser, mail, and other clients from complaining about the certificates installed on your server. Not covered is dealing with a commercial root certificate authority (CA). Instead, we will become our own root CA, and sign our own certificates. These procedures were developed using OpenSSL 0.9.6, 24 Sep 2000, on Linux.Quick Start
Those who want to start creating certificates right away without reading this whole document should skip to the summary at the end.note: a crappy self signed cert can be created with the simple command¬
mod-ssl-makecert, part of debian package libapache-mod-ssl
Background
Why be our own root CA? So that we can take advantage of SSL encryption without spending unnecessary money on having our certificates signed. A drawback is that browsers will still complain about our site not being trusted until our root certificate is imported. However, once this is done, we are no different from the commercial root CAs. Clients will only import our root certificate if they trust us. This is where the commercial CAs come in: they purport to do extensive research into the people and organizations for whom they sign certificates. By importing (actually, by the browser vendors incorporating) their trusted root certificates, we are saying that we trust them when they guarantee that someone else is who they say they are. We can trust additional root CAs (like ourselves) by importing their CA certificates. Note: If you are in the business of running a commercial secure site, obtaining a commercially signed certificate is the only realistic choice.Prerequisites
You will need an installed copy of OpenSSL for this, which is available from www.openssl.org. Chances are it is already installed on your machine. This document will not cover the installation procedure.> apt-get install openssl
Initial Setup
First, we will create a directory where we can work. It does not matter where this is; I am arbitrarily going to create it in my home directory.> mkdir CA
> cd CA
> mkdir newcerts private
- Our Certificate Authority (CA) certificate
- The database of the certificates that we have signed
- The keys, requests, and certificates we generate
- A copy of each certificate we sign
- Our CA private key
- Do not lose this key. Without it, you will not be able to sign or renew any certificates.
- Do not disclose this key to anyone. If it is compromised, others will be able to impersonate you.
> echo '01' > serial
> touch index.txt
#
# OpenSSL configuration file.
#
# Establish working directory.
dir = .
Creating a Root Certificate
With OpenSSL, a large part of what goes into a certificate depends on the contents of the configuration file, rather than the command line. This is a good thing, because there is a lot to specify. The configuration file is divided into sections, which are selectively read and processed according to openssl command line arguments. Sections can include one or more other sections by referring to them, which helps to make the configuration file more modular. A name in square brackets (e.g. " req ") starts each section. We now need to add the section that controls how certificates are created, and a section to define the type of certificate to create. The first thing we need to specify is the Distinguished Name. This is the text that identifies the owner of the certificate when it is viewed. It is not directly referenced in the configuration file, but is included into the section processed when certificate requests are created. The command is "openssl req[ req ]
default_bits = 1024 # Size of keys
default_keyfile = key.pem # name of generated keys
default_md = md5 # message digest algorithm
string_mask = nombstr # permitted characters
distinguished_name = req_distinguished_name
[ req_distinguished_name ]
# Variable name Prompt string
#---------------------- ----------------------------------
0.organizationName = Organization Name (company)
organizationalUnitName = Organizational Unit Name (department, division)
emailAddress = Email Address
emailAddress_max = 40
localityName = Locality Name (city, district)
stateOrProvinceName = State or Province Name (full name)
countryName = Country Name (2 letter code)
countryName_min = 2
countryName_max = 2
commonName = Common Name (hostname, IP, or your name)
commonName_max = 64
# Default values for the above, for consistency and less typing.
# Variable name Value
#------------------------------ ------------------------------
0.organizationName_default = The Sample Company
localityName_default = Metropolis
stateOrProvinceName_default = New York
countryName_default = US
[ v3_ca ]
basicConstraints = CA:TRUE
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer:always
- Create a new self-signed certificate: -new -x509
- Create a CA certificate: -extensions v3_ca
- Make it valid for more than 30 days: -days 3650
- Write output to specific locations: -keyout, -out
- Use our configuration file: -config ./openssl.cnf
# openssl req -new -x509 -extensions v3_ca -keyout private/cakey.pem \
-out cacert.pem -days 3650 -config ./openssl.cnf
Using configuration from ./openssl.cnf
Generating a 1024 bit RSA private key
.......++++++
..........................++++++
writing new private key to 'private/cakey.pem'
Enter PEM pass phrase:demo
Verifying password - Enter PEM pass phrase:demo
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Organization Name (company) [The Sample Company]:<enter>
Organizational Unit Name (department, division) []:CA Division
Email Address []:ca@sample.com
Locality Name (city, district) [Metropolis]:<enter>
State or Province Name (full name) [New York]:<enter>
Country Name (2 letter code) [US]:<enter>
Common Name (hostname, IP, or your name) []:TSC Root CA
- A private key in private/cakey.pem
- A root CA certificate in cacert.pem
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,0947F49BB28FE5F4
jlQvt9WdR9Vpg3WQT5+C3HU17bUOwvhp/r0+viMcBUCRW85UqI2BJJKTi1IwQQ4c
tyTrhYJYOP+A6JXt5BzDzZy/B7tjEMDBosPiwH2m4MaP+6wTbi1qR1pFDL3fXYDr
ZsuN08dkbw9ML6LOX5Rl6bIBL3i5hnGiqm338Fl52gNstThv0C/OZhXT3B4qsJn8
qZb3mC6U2nRaP/NpZPcEx4lv2vH7OzHTu1TZ7t0asSpgpuH58dfHPw775kZDep2F
LXA3Oeavg0TLFHkaFBUx2xaeEG6Txpt9I74aAsw1T6UbTSjqgtsK0PHdjPNfPGlY
5U3Do1pnU9hfoem/4RAOe0cCovP/xf6YPBraSFPs4XFfnWwgEtL09ReFqO9T0aSp
5ajLyBOYOBKQ3PCSu1HQDw/OzphInhKxdYg81WBBEfELzSdMFQZgmfGrt5DyyWmq
TADwWtGVvO3pEhO1STmCaNqZQSpSwEGPGo5RFkyFvyvyozWX2SZg4g1o1X40qSg9
0FMHTEB5HQebEkKBoRQMCJN/uyKXTLjNB7ibtVbZmf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QFMAMBAf8wHQYDVR0OBBYEFG/v
yytrBtEquMX2dreysix/MlPMMIHJBgNVHSMEgcEwgb6AFG/vyytrBtEquMX2drey
six/MlPMoYGipIGfMIGcMRswGQYDVQQKExJUaGUgU2FtcGxlIENvbXBhbnkxFDAS
BgNVBAsTC0NBIERpdmlzaW9uMRwwGgYJKoZIhvcNAQkBFg1jYUBzYW1wbGUuY29t
MRMwEQYDVQQHEwpNZXRyb3BvbGlzMREwDwYDVQQIEwhOZXcgWW9yazELMAkGA1UE
BhMCVVMxFDASBgNVBAMTC1RTQyBSb290IENBggEAMA0GCSqGSIb3DQEBBAUAA4GB
ABclymJfsPOUazNQO8aIaxwVbXWS+8AFEkMMRx6O68ICAMubQBvs8Buz3ALXhqYe
FS5G13pW2ZnAlSdTkSTKkE5wGZ1RYSfyiEKXb+uOKhDN9LnajDzaMPkNDU2NDXDz
SqHk9ZiE1boQaMzjNLu+KabTLpmL9uXvFA/i+gdenFHv
-----END CERTIFICATE-----
> openssl x509 -in cacert.pem -noout -text
> openssl x509 -in cacert.pem -noout -dates
> openssl x509 -in cacert.pem -noout -purpose
Creating a Certificate Signing Request (CSR)
Now that we have a root certificate, we can create any number of certificates for installation into our SSL applications such as https, spop, or simap. The procedure involves creating a private key and certificate request, and then signing the request to generate the certificate. Our configuration file needs some more definitions for creating non-CA certificates. Add the following at the end of the file:[ v3_req ]
basicConstraints = CA:FALSE
subjectKeyIdentifier = hash
distinguished_name = req_distinguished_name
req_extensions = v3_req
- Organizational Unit: a reminder of what the certificate is for
- Email Address: the postmaster
- Common Name: the server hostname
> openssl req -new -nodes -out req.pem -config ./openssl.cnf
Organizational Unit Name (department, division) :Mail Server Email Address :postmaster@sample.comThis process produces two files as output:
Common Name (hostname, IP, or your name) :mail.sample.com
- A private key in key.pem
- A certificate signing request in req.pem
-----BEGIN CERTIFICATE REQUEST-----
MIICJDCCAY0CAQAwgagxGzAZBgNVBAoTElRoZSBTYW1wbGUgQ29tcGFueTEUMBIG
A1UECxMLTWFpbCBTZXJ2ZXIxJDAiBgkqhkiG9w0BCQEWFXBvc3RtYXN0ZXJAc2Ft
cGxlLmNvbTETMBEGA1UEBxMKTWV0cm9wb2xpczERMA8GA1UECBMITmV3IFlvcmsx
CzAJBgNVBAYTAlVTMRgwFgYDVQQDEw9tYWlsLnNhbXBsZS5jb20wgZ8wDQYJKoZI
hvcNAQEBBQADgY0AMIGJAoGBAPJhc++WxcBaoDbJpzFbDg42NcOz/ELVFMU4FlPa
yUzUO+xXkdFRMPKo54d4Pf1w575Jhlu9lE+kJ8QN2st6JFySbc9QjPwVwl9D2+I3
SSf2kVTu+2Ur5izCPbVAfU0rPZxxK8ELoOkA1uwwjFz6EFuVvnHwlguonWKDtmYW
u7KTAgMBAAGgOzA5BgkqhkiG9w0BCQ4xLDAqMAkGA1UdEwQCMAAwHQYDVR0OBBYE
FLWaQsUVIQzWr58HtDinH1JfeCheMA0GCSqGSIb3DQEBBAUAA4GBAAbe0jrGEQ3i
tyVfy5Lg4/f69rKvDGs+uhZJ9ZRx7Dl92Qq2osE7XrLB1bANmcoEv/ORLZOjWZEY
NjMvuz60O7R8GKBrvb/YhAwWhIIt2LJqPkpAEWS0kY0AkoQcfZ7h6oC35+eJ7okg
Uu3WuE57RgcNt7/ftr0sG1jUyRwMLvhv
-----END CERTIFICATE REQUEST-----
> openssl req -in req.pem -text -verify -noout
Signing a Certificate
Now we need to add the configuration file section that deals with being a Certificate Authority. This section will identify the paths to the various pieces, such as the database, the CA certificate, and the private key. It also provides some basic default values. Insert the following into openssl.cnf just before the req section:[ ca ]
default_ca = CA_default
[ CA_default ]
serial = $dir/serial
database = $dir/index.txt
new_certs_dir = $dir/newcerts
certificate = $dir/cacert.pem
private_key = $dir/private/cakey.pem
default_days = 365
default_md = md5
preserve = no
email_in_dn = no
nameopt = default_ca
certopt = default_ca
policy = policy_match
[ policy_match ]
countryName = match
stateOrProvinceName = match
organizationName = match
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
> openssl ca -out cert.pem -config ./openssl.cnf -infiles req.pem
Using configuration from ./openssl.cnf
Enter PEM pass phrase:demo
Check that the request matches the signature
Signature ok
The Subjects Distinguished Name is as follows
organizationName :PRINTABLE:'The Sample Company'
organizationalUnitName:PRINTABLE:'Mail Server'
emailAddress :IA5STRING:'postmaster@sample.com'
localityName :PRINTABLE:'Metropolis'
stateOrProvinceName :PRINTABLE:'New York'
countryName :PRINTABLE:'US'
commonName :PRINTABLE:'mail.sample.com'
Certificate is to be certified until Dec 8 04:37:38 2002 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
- A certificate in cert.pem
- A copy of the certificate in newcerts/
.pem
> openssl x509 -in cert.pem -noout -text -purpose | more
> mv cert.pem tmp.pem
> openssl x509 -in tmp.pem -out cert.pem
Installing the Certificate and Key
This depends on the application. Some want the key and the certificate in the same file, and others want them separately. Combining them is easily done with:> cat key.pem cert.pem >key-cert.pem
- A private key in key.pem
- A certificate in cert.pem
- A combined private key and certificate in key-cert.pem
Apache has separate configuration directives for the key and the certificate, so we keep each in its own file. These files should be kept outside of the DocumentRoot subtree, so a reasonable directory structure might be:
File Comment
/home/httpd/html Apache DocumentRoot
/home/httpd/ssl SSL-related files
/home/httpd/ssl/cert.pem Site certificate
/home/httpd/ssl/key.pem Site private key
<VirtualHost 192.168.1.1:443>
ServerName mail.sample.com
DocumentRoot /home/httpd/html
... other directives for this site ...
SSLEngine on
SSLLog /var/log/ssl_engine_log
SSLCertificateFile /home/httpd/ssl/cert.pem
SSLCertificateKeyFile /home/httpd/ssl/key.pem
</VirtualHost>
stunnel is used as an SSL wrapper for normal non-secure services such as IMAP and POP. It accepts as arguments (among other things) the service to execute, and the location of the certificate and private key. The key and the certificate are provided in the same file. These can go anywhere, but a good location might be /etc/ssl/certs. Specify it on the stunnel command line as follows:
> stunnel -p /etc/ssl/certs/key-cert.pem <other stunnel args...>
Distributing the CA Certificate
This, finally, is the step that stops the clients from complaining about untrusted certificates. Send cacert.pem to anyone who is going to use your secure servers, so they can install it in their browsers, mail clients, et cetera as a root certificate.Renewing Certificates
Your certificate chain can break due to certificate expiry in two ways:- The certificates you signed with your root certificate have expired.
- Your root certificate itself has expired.
> openssl ca -revoke newcerts/02.pem -config ./openssl.cnf
Using configuration from ./openssl.cnf
Enter PEM pass phrase: demo
Revoking Certificate 02.
Data Base Updated
Getting a Commercially Signed Certificate
The process is basically the same as the one just demonstrated, but the CA does most of it. You need to generate a Certificate Signing Request as shown above, and then submit it for signing. You will receive a signed certificate for installation. This certificate will automatically be trusted by your client's browser, as the browser has the commercial CA's certificate built in. There is no need to distribute anything. The configuration described here may be inadequate for this purpose, as there is much more that can go into a request. Different certificate authorities require different features in the certificate signing request, none of which we have gone into here. This additional material is beyond the current scope of this document.Publishing Your CA Certificate
You can post the certificate on your web site for download. If you do this, you should also post a Certificate Revocation List (CRL), and a means of displaying a certificate given its serial number. This is outside the current scope of this document. Apache will serve your certificate in a form recognizable to browsers if you specify its MIME type. For example, you can use the filename extension ".crt" for downloadable certificates, and put the following into the general section of your Apache configuration:AddType application/x-x509-ca-cert .crt
<a href="www.sample.com/ourrootcert.crt">Our Root Certificate</a>
> openssl ca -gencrl -crldays 31 -config ./openssl.cnf -out rootca.crl
Summary
You now have enough information to create and sign certificates on your own behalf. While this is a fairly long document, the procedure can be summarized easily.One-Time Setup
Set up, and create a root CA certificate. Commands# mkdir CA
# cd CA
# mkdir newcerts private
# echo '01' >serial
# touch index.txt
# (IMPORTANT: Install and edit the configuration file shown below.)
# openssl req -new -x509 -extensions v3_ca -keyout private/cakey.pem \
-out cacert.pem -days 365 -config ./openssl.cnf
File Purpose
cacert.pem CA certificate
private/cakey.pem CA private key
Per Certificate
Create certificate signing requests and sign them, supplying appropriate values for the Common Name and the Organizational Unit. Commands> openssl req -new -nodes -out req.pem -config ./openssl.cnf
> openssl ca -out cert.pem -config ./openssl.cnf -infiles req.pem
> cat key.pem cert.pem >key-cert.pem
File Purpose
key.pem Private key
req.pem Certificate signing request
cert.pem Certificate
key-cert.pem Combined private key and certificate
Per Certificate - Renewal
Revoke the expired certificate, and re-sign the original request. Commands> openssl ca -revoke newcerts/<serial>.pem -config ./openssl.cnf
> openssl ca -out cert.pem -config ./openssl.cnf -infiles req.pem
#
# OpenSSL configuration file.
#
# Establish working directory.
dir = .
[ ca ]
default_ca = CA_default
[ CA_default ]
serial = $dir/serial
database = $dir/index.txt
new_certs_dir = $dir/newcerts
certificate = $dir/cacert.pem
private_key = $dir/private/cakey.pem
default_days = 365
default_md = md5
preserve = no
email_in_dn = no
nameopt = default_ca
certopt = default_ca
policy = policy_match
[ policy_match ]
countryName = match
stateOrProvinceName = match
organizationName = match
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
[ req ]
default_bits = 1024 # Size of keys
default_keyfile = key.pem # name of generated keys
default_md = md5 # message digest algorithm
string_mask = nombstr # permitted characters
distinguished_name = req_distinguished_name
req_extensions = v3_req
[ req_distinguished_name ]
# Variable name Prompt string
#---------------------- ----------------------------------
0.organizationName = Organization Name (company)
organizationalUnitName = Organizational Unit Name (department, division)
emailAddress = Email Address
emailAddress_max = 40
localityName = Locality Name (city, district)
stateOrProvinceName = State or Province Name (full name)
countryName = Country Name (2 letter code)
countryName_min = 2
countryName_max = 2
commonName = Common Name (hostname, IP, or your name)
commonName_max = 64
# Default values for the above, for consistency and less typing.
# Variable name Value
#------------------------------ ------------------------------
0.organizationName_default = The Sample Company
localityName_default = Metropolis
stateOrProvinceName_default = New York
countryName_default = US
[ v3_ca ]
basicConstraints = CA:TRUE
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer:always
[ v3_req ]
basicConstraints = CA:FALSE
subjectKeyIdentifier = hash
References
More information is available at the following sites (opens in new window):- OpenSSL Home Page
- OpenSSL Documentation
- OpenSSL FAQ
- Nick Burch's Certificate Management and Installation with OpenSSL
tirian.magd.ox.ac.uk/~nick/openssl-certs/
- Franck Martin's SSL Certificates HOWTO
www.tldp.org/HOWTO/SSL-Certificates-HOWTO/