#!/bin/bash
#
# Example-script to show, how to set up a self-signed CA
#
# Usage:
# ./create-ca.sh PASSWORD VALIDITY

. settings.conf

if [ -z "$1" ]
then
  PASSWORD=$DEFAULT_PASSWORD
else
  PASSWORD="$1"
fi

if [ -z "$2" ]
then
  VALIDITY=$DEFAULT_VALIDITY
else
  VALIDITY="$2"
fi

if [ -a ca-cert ]
then
  echo "The root-certificate for the CA already exists..."
else
  echo "Creating a x509-certificate for the CA..."
  openssl req -new -x509 -subj "/C=$C/ST=$ST/L=$L/O=$O/OU=$OU/CN=Root-CA" -keyout ca-key -out ca-cert -days $VALIDITY -passout pass:$PASSWORD
fi
#

if [ -a truststore.p12 ]
then
  echo "The keystore truststore.jks already exists!"
else
  echo "Importing the root-certificate of the CA into truststore.jks..."
  keytool -keystore truststore.p12 -storetype pkcs12 -storepass $PASSWORD -alias CARoot -import -file ca-cert -noprompt
fi
