Difference between revisions of "CSR Apache"

From Noah.org
Jump to: navigation, search
(Snake Oil cert script)
Line 35: Line 35:
openssl req -x509 -new -days 36500 -key server.key -out server.crt
openssl req -x509 -new -days 3650 -key server.key -out server.crt
=== Generate a PEM file ===
cat server.key server.crt > server.pem

Revision as of 11:23, 17 July 2008

Certificate Signing Request Overview

There are two types of SSL certificates: certificates signed by a Certificate Authority (CA) and self-signed certificates (Snakeoil Certs). Neither one is cryptographically stronger than the other. In other words, a snakeoil cert is not less secure or easier to break. A CA Signed certificate is simply an assurance by a neutral third party that you are who you say you are. This makes it difficult for you to impersonate anyone else. A self-signed cert means that no one is vouching for you. A browser will always display a warning to the user when connecting to your site. There is no way to get rid of this warning.

If you are simply want to secure your own private web server then a Snakeoil cert is fine.

There are two types of CA Signed Certificates. There are certs signed by Root CAs and certs signed by Intermediate CAs. A Root CA is implicitly trusted by most browsers. These are "better" and cost more. These are the signed certificates that you get from VeriSign and Thawte. In Intermediate CA signed certificate is cheaper and signed by a less well-known company. Your browser has to make two trips to verify your certificate. It first has to go to the Intermediate CA to get their certificate which is in turn signed by a Root CA. Your browser then goes to a Root CA to verify the certificate of the Intermediate CA. Intermediate CAs include companies like Go Daddy. Intermediate CA certs are sometimes called "chained root certificates".

For more information see:

   Apache2 SSL

Brief Examples

These examples assume that you have done this before -- this is just a refresher:

Generate Private Key

openssl genrsa -out server.key 1024

Generate CSR

openssl req -new -key server.key -out server.csr

Generate Self-Signed Certificate (no CSR)

openssl req -x509 -new -days 3650 -key server.key -out server.crt

Generate a PEM file

cat server.key server.crt > server.pem

Generate a private server key

Keep your server key private. You may also encrypt the server key, but then you will have to enter your key password every time you start your web server.

openssl genrsa -out server.key 1024

Make sure that no one can read your private server.key.

chmod 400 server.key

OPTIONAL -- Encrypt the server key

This will require that you enter your key password everytime you start your web server. This means that you cannot have the server start automatically when the machine boots. I almost never do this.

openssl rsa -des3 -in server.key -out server.key

View contents of a private server key

openssl rsa -noout -text -in server.key

Generate a CA-signed certificate

A Certificate Authority such as Thawte or Verisign verifies certificates. You have to subscribe to this service. It does not improve the crypto security. A CA merely lets your customers know that you are who you say you are.

First you have to generate a Certificate Signing Request (CSR) to give to a CA to obtain a CA-signed certificate:

Generate a Certificate Signing Request (CSR)

To get a signed certificate you need to request one by generating a Certificate Signing Request. Enter the following command:

openssl req -new -key server.key -out server.csr

You will be presented with a form to fill out. It's pretty simple. Just make sure that when you are asked for your "Common Name" that you enter your Fully Qualified Domain Name (FQDN) and that it exactly matches the domain name of your server. That includes the www in www.example.com. For example:

Common Name (eg, YOUR name) []: www.example.com

The certificate is generated and stored in the file server.crt.

View contents of a CSR

openssl req -noout -text -in server.csr

Submit the CSR to your Certificate Authority

The CA will provide instructions on how to submit the CSR. Usually you have to paste it into a form on a web page. Once the CSR is processed, the CA will mail you a signed certificate.

Receive your certificate from your CA

The CA will usually email you back a link to download your new certificate Store the CA-signed certificate in the file server.crt.

Make sure that everyone can read you server.crt:

chmod 444 server.crt

Generate a Self-Signed Certificate (Snakeoil)

You can skip the CSR step and directly generate your own CRT file. This is sometimes known as a Snakeoil certificate, because it is not signed by a trusted third party such as Thawte or Verisign. This is useful for testing or for small, personal web sites. The encryption is just as good, but your browser will popup a warning because it cannot vouch for the fact that you are who you say you are. The "-days 36500" option sets the certificate to expire in 100 years.

This will generate both a server key and a certificate with dummy values. This is the fastest way to make a cert.

openssl req -x509 -batch -newkey rsa:1024 -days 36500 -nodes -keyout server.key -out server.crt

Use this command if you already have a server key.

openssl req -x509 -new -days 36500 -key server.key -out server.crt

View contents of a CRT

openssl x509 -noout -text -in server.crt

Snake Oil cert script

You can't easily get `openssl` to take all certificate parameters from the command-line. It wants to read from stdin or from a config file.

#!/usr/bin/env python
config = """
prompt = no
distinguished_name = distinguished_name
[ distinguished_name ]
C                      = US
ST                     = California
L                      = San Francisco
O                      = %(cn)s
OU                     = Engineering
CN                     = %(cn)s
emailAddress           = postmaster@%(dn)s

import os,sys
from pexpect import run
dn = sys.argv[1]
cn = 'www.' + dn
fout = file(cn+'.config','w')
fout.write (config % locals())
run ('openssl genrsa -out %(cn)s.key 1024'%locals())
run ('openssl req -new -days 36500 -config %(cn)s.config -key %(cn)s.key -out %(cn)s.csr'%locals())

Cert Installation

Some systems such as security appliances (load balancers, hardware SSL) will want your SSL Cert as a single file. The Coyote Point Equalizer likes this. You can combine your server.key and server.crt files into a single file simply by concatenating them together. They are plain text files.

cat server.key server.crt > server.pem

This also works on Apache2.

Verify Server Certificate matches Server Key

You can verify that the PEM file is valid and that the CRT and KEY agree: This assumes that server.pem is a PEM format file with the Intermediate CA Cert, Server Cert, and Server Key all concatenated together.

openssl verify server.pem

Verify Intermediate Certification Authorities certificate bundle

You need to add the -CAfile option if you are using a budget certificate. In this case you should have also received an Intermediate CA Cert along with your Server Cert. You don't need this option if your Server Cert is verified against a Root Certificate Authority.

openssl verify -CAfile server.pem server.pem 

Encrypt your server key

If you want to require a password to start your web server then encrypt the key. This assumes that your key is not already encrypted. This will overwrite your key. To encrypt:

openssl rsa -des3 -in server.key -out server.key

Unencrypt your server key

You can remove the encryption from a server key. You may do this if you want to change the password. Just remove the old encryption then encrypt it again. To unencrypt:

openssl rsa -in server.key -out server.key

How to configure Apache2 with Intermediate CA Certificates

Use these instructions if you are using an SSL Certificate with an intermediate certificate authority (a third-party CA signed certificate). The following lines will usually be in a <VirtualHost> section in httpd.conf or included from a line such as 'Include conf/extra/httpd-ssl.conf' in httpd.conf. The important thing to set is the SSLCACertificateFile line. If you are using a PEM format you can set SSLCertificateFile and SSLCACertificateFile to point to the same file. PEM format is just a text file where keys and certificates are concatenated together in a human-readable Base64-encoded text file. The PEM name originated from Privacy Enhanced Mail, but don't let that throw you -- nobody uses Privacy Enhanced Mail these days. You could also have the server Key, server Cert, and CA Cert in separate files.

    SSLCertificateKeyFile /var/www/usr/local/apache2/certs/www.example.com/server.pem
    SSLCertificateFile /var/www/usr/local/apache2/certs/www.example.com/server.pem
    SSLCACertificateFile /var/www/usr/local/apache2/certs/www.example.com/server.pem

Certificate errors in the browser

If you installed a Verisign certificate on your server and it seems to work under Internet Explorer but not in Firefox or Safari then it's probably because you need to install an intermediate certificate. You may get a warning like this from the browser:

Website Certified by an Unknown Authority
- Your browser does not recognize the Certificate Authority that issued the site's certificate.

It used to be that Verisign certs did not need an intermediate cert, but this is no longer the case. It's a simple matter to download Verisign's Intermediate CA Certificates. You need to have some lines like the following in your Apache configuration (note the line with SSLCertificateChainFile):

SSLCertificateFile /var/www/conf/ssl/certs/www.example.com.crt
SSLCertificateKeyFile /var/www/conf/ssl/certs/www.example.com.key
SSLCertificateChainFile /var/www/conf/ssl/certs/verisign_intermediate_ca.crt