OpenSSL -- PKI

Aus RZ-Amper Wiki
Zur Navigation springen Zur Suche springen

The Advanced PKI consists of a root CA and a layer of subordinate CAs. We assume a company named Rechenzentrum Amper, controlling the domain rz-amper.de. The company runs a two-pronged PKI to serve its security needs. To implement the PKI, we first create the Rechenzentrum Amper Root CA Authority and its CA certificate. We then use the root CA to create the three signing CAs: Rechenzentrum Amper Email CA and Rechenzentrum Amper TLS CA. The CAs in place we proceed to show them in operation, issuing user certificates for email-protection and TLS-authentication.

Root Certification Authority

All commands are ready to be copy/pasted into a terminal session. When you have reached the end of this page, you will have built a PKI with multiple signing CAs and issued 4 different types of user certificates.

Directories

Just follow these commands to setup needed directories.

mkdir -p /usr/local/etc/PKI
cd /usr/local/etc/PKI 
mkdir -p ca/root-ca/private ca/root-ca/db
mkdir -p ca/email-ca/private ca/email-ca/db
mkdir -p ca/tls-ca/private ca/tls-ca/db
mkdir crl certs etc
chown -R root:root /usr/local/etc/PKI
chmod -R 0700 /usr/local/etc/PKI

Serial Database

Create files to keep track of serial numbers. The files must exist before the openssl ca command can be used.

cp /dev/null ca/root-ca/db/root-ca.db
cp /dev/null ca/root-ca/db/root-ca.db.attr
cp /dev/null ca/email-ca/db/email-ca.db
cp /dev/null ca/email-ca/db/email-ca.db.attr
cp /dev/null ca/tls-ca/db/tls-ca.db
cp /dev/null ca/tls-ca/db/tls-ca.db.attr
echo '100001' > ca/root-ca/db/root-ca.crt.srl
echo '100001' > ca/root-ca/db/root-ca.crl.srl
echo '100001' > ca/email-ca/db/email-ca.crt.srl
echo '100001' > ca/email-ca/db/email-ca.crl.srl
echo '100001' > ca/tls-ca/db/tls-ca.crt.srl
echo '100001' > ca/tls-ca/db/tls-ca.crl.srl 
touch certindex.txt

Config File for SSL

Create the files using vi text editor and call them described. Here are the basics needed for this exercise (edit as needed):
We use one configuration file per CA:

And one configuration file per CSR type:

Create CA Request

With the openssl req -new command we create a private key and a CSR for the root CA. The configuration is taken from the [req] section of the Root CA configuration file.

openssl req -new -config etc/root-ca.conf -out ca/root-ca.csr -keyout ca/root-ca/private/root-ca.key
openssl req -new -config etc/email-ca.conf -out ca/email-ca.csr -keyout ca/email-ca/private/email-ca.key
openssl req -new -config etc/tls-ca.conf -out ca/tls-ca.csr -keyout ca/tls-ca/private/tls-ca.key

Create CA Certificate

With the openssl ca command we create a self-signed root certificate from the CSR. The configuration is taken from the [ca] section of the root CA configuration file. Note that we specify an end date based on the key length. 2048-bit RSA keys are deemed safe until 2030 (RSA Labs).

openssl ca -selfsign -config etc/root-ca.conf -in ca/root-ca.csr -out ca/root-ca.crt -extensions root_ca_ext -enddate 20491231235959Z
openssl ca -config etc/root-ca.conf -in ca/email-ca.csr -out ca/email-ca.crt -extensions signing_ca_ext
openssl ca -config etc/root-ca.conf -in ca/tls-ca.csr -out ca/tls-ca.crt -extensions signing_ca_ext

Create initial CRL

With the openssl ca -gencrl command we generate an initial (empty) CRL.

openssl ca -gencrl -config etc/root-ca.conf -out crl/root-ca.crl
openssl ca -gencrl -config etc/email-ca.conf -out crl/email-ca.crl
openssl ca -gencrl -config etc/tls-ca.conf -out crl/tls-ca.crl

Create PEM bundle

We create a certificate chain file from the email CA and root CA certificates. It will come handly later as input for the openssl pkcs12 command.

cat ca/email-ca.crt ca/root-ca.crt > ca/email-ca-chain.pem
cat ca/tls-ca.crt ca/root-ca.crt > ca/tls-ca-chain.pem

Operate Email CA

Create email request

We create the private key and CSR for an email-protection certificate using a request configuration file. When prompted enter these DN components: C=NO, O=Green AS, CN=Fred Flintstone, emailAddress=fred@green.no. Leave other fields empty.

openssl req -new -config etc/email.conf -out certs/fred.csr -keyout certs/fred.key

Create email certificate

We use the email CA to issue Fred’s email-protection certificate. A copy of the certificate is saved in the certificate archive under the name ca/email-ca/01.pem (01 being the certificate serial number in hex.)

openssl ca -config etc/email-ca.conf -in certs/fred.csr -out certs/fred.crt -extensions email_ext

Create PKCS#12 bundle

We pack the private key, the certificate, and the CA chain into a PKCS#12 bundle. This format (often with a .pfx extension) is used to distribute keys and certificates to end users. The friendly names help identify individual certificates within the bundle.

openssl pkcs12 -export -name "Fred Flintstone (Email Security)" -caname "Green Email CA" -caname "Green Root CA" -inkey certs/fred.key -in certs/fred.crt -certfile ca/email-ca-chain.pem -out certs/fred.p12

Revoke certificate

When Fred’s laptop goes missing, we revoke his certificate.

openssl ca -config etc/email-ca.conf -revoke ca/email-ca/01.pem -crl_reason keyCompromise

Create CRL

The next CRL contains the revoked certificate.

openssl ca -gencrl -config etc/email-ca.conf -out crl/email-ca.crl

Operate TLS CA

Create TLS server request

We create the private key and CSR for a TLS-server certificate using the appropriate request configuration file. When prompted enter these DN components: C=NO, O=Green AS, CN=www.green.no. The subjectAltName cannot be prompted for and must be specified as environment variable.

SAN=DNS:rz-amper.de,DNS:www.rz-amper.de \
openssl req -new -config etc/server.conf -out certs/rz-amper.de.csr -keyout certs/rz-amper.de.key

Create TLS server certificate

We use the TLS CA to issue the server certificate.

openssl ca -config etc/tls-ca.conf -in certs/rz-amper.de.csr -out certs/rz-amper.de.crt -extensions server_ext

Create PKCS#12 bundle

We pack the private key, the certificate, and the CA chain into a PKCS#12 bundle for distribution.

openssl pkcs12 -export -name "RZ-Amper (Network Component)" -caname "RZ-Amper TLS CA" -caname "RZ-Amper Root CA" \
-inkey certs/rz-amper.de.key -in certs/rz-amper.de.crt -certfile ca/tls-ca-chain.pem -out certs/rz-amper.de.p12

Links

http://pki-tutorial.readthedocs.org/en/latest/advanced/ Probably the best documentation to get.
https://www.madboa.com/geek/openssl/
https://www.seccommerce.de/en/faqs-en/28-secpki/36-certificate-generation-using-openssl.html
http://www.akadia.com/services/ssh_test_certificate.html
http://www.sans.org/reading-room/whitepapers/certificates/building-managing-pki-solution-small-medium-size-business-34445
http://www.pki.iam.metu.edu.tr/yazi-makale/ospki.pdf
http://hexeract.wordpress.com/2009/04/17/useful-openssl-one-liners/