#!/bin/sh
# Create and install a CA certificate along with two regular certificates, for use with iked
# Create a temporary working directory under /root to ensure 700 permissions:
rm -rf /root/iked_certs
mkdir /root/iked_certs
cd /root/iked_certs
# Write elliptic curve parameters for key generation to a temporary file:
openssl ecparam -out ec-secp384r1.pem -name secp384r1
# Create public and private keypair for CA:
openssl genpkey -paramfile ec-secp384r1.pem -out ca.key
openssl ec -in ca.key -pubout -out ca.pub
# Interactively prompt for Distinguished Name parameters to create CA certificate signing request:
echo CSR for CA:
openssl req -key ca.key -new -out ca.csr
# Write extension options to a temporary file:
echo "basicConstraints=critical,CA:true" > ca.ext
echo "keyUsage=digitalSignature,keyCertSign,cRLSign" >> ca.ext
# Create the CA certificate in ca.ssc:
openssl x509 -sha256 -req -days 365 -in ca.csr -signkey ca.key -extfile ca.ext -out ca.ssc
# Display the generated certificate in text format:
openssl x509 -text -noout -in ca.ssc
# Create public and private keypair for one.lan, (reusing the EC parameter file):
openssl genpkey -paramfile ec-secp384r1.pem -out one.lan.key
openssl ec -in one.lan.key -pubout -out one.lan.pub
# Interactively prompt for DN parameters to create CSR for one.lan:
echo CSR for one.lan
openssl req -key one.lan.key -new -out one.lan.csr
# Write extension options to a temporary file:
echo "subjectAltName=DNS:one.lan" > one.lan.ext
echo "basicConstraints=CA:false" >> one.lan.ext
echo "keyUsage=digitalSignature,keyCertSign,cRLSign" >> one.lan.ext
echo "extendedKeyUsage=serverAuth,clientAuth" >> one.lan.ext
# Create public and private keypair for two.lan, (reusing the EC parameter file):
openssl genpkey -paramfile ec-secp384r1.pem -out two.lan.key
openssl ec -in two.lan.key -pubout -out two.lan.pub
# Interactively prompt for DN parameters to create CSR for one.lan:
echo CSR for two.lan
openssl req -key two.lan.key -new -out two.lan.csr
# Write extension options to a temporary file:
echo "subjectAltName=DNS:two.lan" > two.lan.ext
echo "basicConstraints=CA:false" >> two.lan.ext
echo "keyUsage=digitalSignature,keyCertSign,cRLSign" >> two.lan.ext
echo "extendedKeyUsage=serverAuth,clientAuth" >> two.lan.ext
# Initialize a file containing the certificate serial number to zero:
echo "00" > ca.srl
# Generate CA signed certificates for one.lan and two.lan from the corresponding CSRs:
openssl x509 -CA ca.ssc -CAkey ca.key -CAserial ca.srl -req -in one.lan.csr -extfile one.lan.ext -out one.lan.crt
openssl x509 -CA ca.ssc -CAkey ca.key -CAserial ca.srl -req -in two.lan.csr -extfile two.lan.ext -out two.lan.crt
# Copy the CA certificate to /etc/iked/ca/:
cp ca.ssc /etc/iked/ca/ca.crt
scp ca.ssc root@two.lan:/etc/iked/ca/ca.crt
# Install each machine's local certificate to /etc/iked/certs/:
cp one.lan.crt /etc/iked/certs/
scp two.lan.crt root@two.lan:/etc/iked/certs/
# Install each machine's local private key to /etc/iked/private/local.key:
cp one.lan.key /etc/iked/private/local.key
scp two.lan.key root@two.lan:/etc/iked/private/local.key
# Install each machine's public key to /etc/iked/local.pub:
cp one.lan.pub /etc/iked/local.pub
scp two.lan.pub root@two.lan:/etc/iked/local.pub