#!/bin/sh
# This script generates a simple SAN CSR to be used with Let's Encrypt
# CA. Mostly intended for "auth --csr" testing, but, since it's easily
# auditable, feel free to adjust it and use it on your production web
# server.

if [ "$#" -lt 1 ]
then
  echo "Usage: $0 domain [domain...]" >&2
  exit 1
fi

domains="DNS:$1"
shift
for x in "$@"
do
  domains="$domains,DNS:$x"
done

_config=${OPENSSL_CNF:-openssl.cnf}
_conftemp=`mktemp /tmp/generate-csr.cnf.XXXXXXXX` || exit 1
sed "s%\${ENV::SAN}%$domains%" < $_config > $_conftemp

openssl req -config "$_conftemp" \
  -new -nodes -subj '/' -reqexts san \
  -out "${CSR_PATH:-csr.der}" \
  -keyout "${KEY_PATH:-key.pem}" \
  -newkey rsa:2048 \
  -outform DER
# 512 or 1024 too low for Boulder, 2048 is smallest for tests

echo rm $_conftemp
echo "You can now run: certbot auth --csr ${CSR_PATH:-csr.der}"
