#!/bin/sh

# Take the correct binary to create the certificates
CERTTOOL=$(which gnutls-certtool || which certtool)
if [ -z "$CERTTOOL" ]
then
  echo "ERROR: No certtool found" >&2
  exit 1
fi

. ./vars

# Create a CA key.
$CERTTOOL \
  --generate-privkey \
  --bits $BITS \
  --outfile ca.key.pem

chmod 600 ca.key.pem

# Sign a CA cert.
cat <<EOF >ca.info
organization = $ORGANIZATION
cn = $CN
country = $COUNTRY
state = $STATE
locality = $LOCALITY
ca
cert_signing_key
EOF

$CERTTOOL \
  --generate-self-signed \
  --load-privkey ca.key.pem \
  --template ca.info \
  --outfile ca.cert.pem

chmod 600 ca.cert.pem
rm ca.info
exit 0
