OpenSSL -- Advanced PKI

Aus RZ-Amper Wiki
Version vom 31. Oktober 2020, 12:08 Uhr von WikiSysop (Diskussion | Beiträge)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
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 -enddate 20491231235959Z
openssl ca -config etc/root-ca.conf -in ca/tls-ca.csr -out ca/tls-ca.crt -extensions signing_ca_ext -enddate 20491231235959Z

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 -enddate 20491231235959Z

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

The Most Common OpenSSL Commands

Checking Using OpenSSL

If you need to check the information within a Certificate, CSR or Private Key, use these commands. You can also check CSRs and check certificates using our online tools.

Check a Certificate Signing Request (CSR)

openssl req -text -noout -verify -in CSR.csr

Check a private key

openssl rsa -in privateKey.key -check

Check a certificate

openssl x509 -in certificate.crt -text -noout

Check a PKCS#12 file (.pfx or .p12)

openssl pkcs12 -info -in keyStore.p12

Connect to TCP port using openssl

openssl s_client -connect server.tld:443

Debugging Using OpenSSL

If you are receiving an error that the private doesn't match the certificate or that a certificate that you installed to a site is not trusted, try one of these commands. If you are trying to verify that an SSL certificate is installed correctly, be sure to check out the SSL Checker.

Check an MD5 hash of the public key to ensure that it matches with what is in a CSR or private key

openssl x509 -noout -modulus -in certificate.crt | openssl md5
openssl rsa -noout -modulus -in privateKey.key | openssl md5
openssl req -noout -modulus -in CSR.csr | openssl md5

Check an SSL connection. All the certificates (including Intermediates) should be displayed

openssl s_client -connect www.paypal.com:443

Converting Using OpenSSL

These commands allow you to convert certificates and keys to different formats to make them compatible with specific types of servers or software. For example, you can convert a normal PEM file that would work with Apache to a PFX (PKCS#12) file and use it with Tomcat or IIS. Use our SSL Converter to convert certificates without messing with OpenSSL.

Convert a DER file (.crt .cer .der) to PEM

openssl x509 -inform der -in certificate.cer -out certificate.pem

Convert a PEM file to DER

openssl x509 -outform der -in certificate.pem -out certificate.der

Convert a PKCS#12 file (.pfx .p12) containing a private key and certificates to PEM

openssl pkcs12 -in keyStore.pfx -out keyStore.pem -nodes

You can add -nocerts to only output the private key or add -nokeys to only output the certificates.

Convert a PEM certificate file and a private key to PKCS#12 (.pfx .p12)

openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.crt -certfile CACert.crt