#!/usr/bin/env bash
# =================================================================
# # # # #
# ## # #### ##### ## # ###### #####
# # # # # # # # # # # # #
# # # # # # # # # # # ##### #
# # # # # # ##### # # # # #
# # ## # # # # # ## # #
# # # #### # # # # ###### #
#
# --- The NorNet Testbed for Multi-Homed Systems ---
# https://www.nntb.no
# =================================================================
#
# High-Performance Connectivity Tracer (HiPerConTracer)
# Copyright (C) 2015-2026 by Thomas Dreibholz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
#
# Contact: dreibh@simula.no
# Bash options:
set -o nounset
set -o errexit
if [ $# -lt 1 ] ; then
echo >&2 "ERROR:$0 server_name [ssl_directory]"
exit 1
fi
if [ $# -ge 2 ] ; then
SSL_DIRECTORY="$2"
else
SSL_DIRECTORY="/tmp/ssl"
fi
SERVER="$1"
SERVER_OPENSSL_CONF="${SSL_DIRECTORY}/${SERVER}.conf"
SERVER_KEY="${SSL_DIRECTORY}/${SERVER}.key"
SERVER_CSR="${SSL_DIRECTORY}/${SERVER}.csr"
SERVER_CRT="${SSL_DIRECTORY}/${SERVER}.crt"
SERVER_CHAIN="${SSL_DIRECTORY}/${SERVER}-chain.pem"
# Get all IP addresses:
ALT_NAMES="DNS:localhost,DNS:`hostname -f`"
ALT_ADDRESSES=`hostname -I | xargs -n1 | awk 'BEGIN { first=1 } { if(!first) { printf(","); } printf("IP:%s",$1); first=0 }'`
RSA_BITS=4096
CA1="TestLevel1"
CA1_DIR="${SSL_DIRECTORY}/${CA1}"
CA1_OPENSSL_CONF="${CA1}.conf"
CA1_KEY="private/${CA1}.key"
CA1_CSR="${CA1}.csr"
CA1_CRT="certs/${CA1}.crt"
CA1_PEM="chains/${CA1}-chain.pem"
CA1_CRL="crl/${CA1}.crl"
CA2="TestIntermediate"
CA2_DIR="${SSL_DIRECTORY}/${CA2}"
CA2_OPENSSL_CONF="${CA2}.conf"
CA2_KEY="private/${CA2}.key"
CA2_CSR="${CA2}.csr"
CA2_CRT="certs/${CA2}.crt"
CA2_PEM="${CA2}-chain.pem"
CA2_CRL="crl/${CA2}.crl"
CA3="TestLeaf"
CA3_DIR="${SSL_DIRECTORY}/${CA3}"
CA3_OPENSSL_CONF="${CA3}.conf"
CA3_KEY="private/${CA3}.key"
CA3_CSR="${CA3}.csr"
CA3_CRT="certs/${CA3}.crt"
CA3_PEM="${CA3}-chain.pem"
CA3_CRL="crl/${CA3}.crl"
if [ ! -e ${SSL_DIRECTORY} ]; then
sudo mkdir -p ${SSL_DIRECTORY}
fi
# ====== Make Level-1 CA ====================================================
if [ ! -d ${CA1_DIR}/private -o ! -e ${CA1_DIR}/${CA1_CRT} ] ; then
echo -e "\x1b[34mCreating Level-1 CA key and certificate ...\x1b[0m"
sudo mkdir -p \
${CA1_DIR}/certs \
${CA1_DIR}/crl \
${CA1_DIR}/newcerts \
${CA1_DIR}/chains
sudo mkdir -m700 -p ${CA1_DIR}/private
if [ ! -e ${CA1_DIR}/index.txt ] ; then
sudo touch ${CA1_DIR}/index.txt
fi
if [ ! -e ${CA1_DIR}/serial ] ; then
echo "1000" | sudo tee ${CA1_DIR}/serial >/dev/null
fi
if [ ! -e ${CA1_DIR}/crlnumber ] ; then
echo "1000" | sudo tee ${CA1_DIR}/crlnumber >/dev/null
fi
sudo tee ${CA1_DIR}/${CA1_OPENSSL_CONF} >/dev/null </dev/null
fi
if [ ! -e ${CA2_DIR}/crlnumber ] ; then
echo "1000" | sudo tee ${CA2_DIR}/crlnumber >/dev/null
fi
sudo tee ${CA2_DIR}/${CA2_OPENSSL_CONF} >/dev/null </dev/null
sudo openssl verify -CAfile ${CA2_DIR}/${CA2_PEM} ${CA2_DIR}/${CA2_CRT}
fi
# ====== Make Level-3 CA ====================================================
if [ ! -d ${CA3_DIR}/private -o ! -e ${CA3_DIR}/${CA3_CRT} ] ; then
echo -e "\x1b[34mCreating Level-3 CA key and certificate ...\x1b[0m"
sudo mkdir -p \
${CA3_DIR}/certs \
${CA3_DIR}/crl \
${CA3_DIR}/newcerts \
${CA3_DIR}/chains
sudo mkdir -m700 -p ${CA3_DIR}/private
if [ ! -e ${CA3_DIR}/index.txt ] ; then
sudo touch ${CA3_DIR}/index.txt
fi
if [ ! -e ${CA3_DIR}/serial ] ; then
echo "1000" | sudo tee ${CA3_DIR}/serial >/dev/null
fi
if [ ! -e ${CA3_DIR}/crlnumber ] ; then
echo "1000" | sudo tee ${CA3_DIR}/crlnumber >/dev/null
fi
sudo tee ${CA3_DIR}/${CA3_OPENSSL_CONF} >/dev/null </dev/null
sudo openssl verify -CAfile ${CA3_DIR}/${CA3_PEM} ${CA3_DIR}/${CA3_CRT}
fi
# ====== Make server certificate ============================================
if [ ! -e ${SERVER_KEY} -o ! -e ${SERVER_CRT} ] ; then
echo -e "\x1b[34mCreating server key and certificate ...\x1b[0m"
sudo tee ${SERVER_OPENSSL_CONF} >/dev/null </dev/null
fi
sudo openssl verify -CAfile ${CA3_DIR}/${CA3_PEM} ${SERVER_CRT}