MARWAN docs NOC knowledge base
DocsCertificates

Setup a CA and a self-signed certificate

Create a local certificate authority and use it to sign a server certificate with the extensions applications need.

Published Redouane Merrouch

1. Create Certification Authority#

CANAME=YOUR-CA
DOMAIN=domain.ma
mkdir $CANAME
cd $CANAME
CONFIG="
[req]
distinguished_name=dn
[ dn ]
[ ext ]
basicConstraints=CA:TRUE,pathlen:0
"
openssl req -config <(echo "$CONFIG") -new -newkey rsa:4096 -nodes -subj "/CN=$CANAME/C=MA/ST=Rabat/L=Rabat/O=YOUR-ORG" -x509 -days 3000 -extensions ext -keyout $CANAME.key -out YOUR-CA.crt

Let's install the CA locally:

cp $CANAME.crt /etc/pki/ca-trust/source/anchors/$CANAME.crt
update-ca-trust

2. Create the CSR and EXT files for our server certificate#

Let's create a server SSL certificate that we will sign with the root CA.

The EXT file will contain some certificate extension that are required by some applications. Modify according to your needs.

$DOMAIN.v3.ext
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names

[alt_names]
DNS.1 = mail.domain.ma   ## add domains that the certificate
DNS.2 = radius.domain.ma ## covers here

Now we genarate the server key and CSR, you can leave the email and challenge password emply.

openssl genrsa  -out $DOMAIN.key 4096
openssl req -new -key $DOMAIN.key -out $DOMAIN.csr
$DOMAIN.key unprotected

A passwordless key is convenient. But, make sure your key is well protected, has 600 permission, and only authorized personnel has access to the server.

3. Sign the server certificate with our CA.#

openssl x509 -req -days 390 -in $DOMAIN.csr -CA $CANAME.crt -CAkey $CANAME.key -out $DOMAIN.crt -CAcreateserial -sha256 -extfile $DOMAIN.v3.ext

To check that the server certificate is correct, let's run the command:

openssl x509 -in $DOMAIN.crt -noout -text | head

If you need Diffie-Hellman parameters, generate with the following command:

openssl dhparam -check -text -5 -out /etc/raddb/certs/dh 2048

4. Configure auto-renewal for the certificate#

Let's configure a CRON to auto-renew the certificate. First, let's write a script for the auto-renew command, and name it renew-$DOMAIN-cert.sh

#!/bin/bash
CANAME=YOUR-CA       ## change to your org's name
DOMAIN=domain.ma     ## change to your org's domain
cd /root/$CANAME
openssl x509 -req -days 390 -in $DOMAIN.csr -CA $CANAME.crt -CAkey $CANAME.key -out $DOMAIN.crt -CAcreateserial -sha256 -extfile $DOMAIN.v3.ext

Make it executable:

chmod +x renew-$DOMAIN-cert.sh

Then we configure a CRON to execute the script, the CRON below runs it every 3 months, but you can tune to your liking. Run crontab -e, and paste the following content:

46 13 * */3 * /root/$CANAME/renew-radius-cert.sh