#!/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

NAME=client
if [ $# -gt 0 ] ; then
  NAME=$1
fi

# Create a client key.
$CERTTOOL \
  --generate-privkey \
  --bits $BITS \
  --outfile ${NAME}.key.pem

# Sign a client cert with the key.
chmod 600 ${NAME}.key.pem
cat <<EOF >client.info
organization = $ORGANIZATION
cn = $CN
tls_www_client
encryption_key
signing_key
EOF

$CERTTOOL \
  --generate-certificate \
  --load-privkey ${NAME}.key.pem \
  --load-ca-certificate ca.cert.pem \
  --load-ca-privkey ca.key.pem \
  --template client.info \
  --outfile ${NAME}.cert.pem

chmod 600 ${NAME}.cert.pem
rm client.info
exit 0

