Daily Hack #day58 - Shell Script for Creating [self] CA-Signed Certificates for Your Dev Sites

Daily Hack #day58 - Shell Script for Creating [self] CA-Signed Certificates for Your Dev Sites

By using self-signed certificates, developers can ensure their development sites are secured with HTTPS, facilitating the testing of secure connections and encryption-related functionality. While not suitable for production, self-signed certificates are a practical solution for development and testing environments.

Here’s a handy shell script you can modify for your own purposes. It should work on macOS, Linux, or Windows via Git Bash:

#!/bin/sh

if [ "$#" -ne 1 ]
then
  echo "Usage: Must supply a domain"
  exit 1
fi

DOMAIN=$1

cd ~/certs

openssl genrsa -out $DOMAIN.key 2048
openssl req -new -key $DOMAIN.key -out $DOMAIN.csr

cat > $DOMAIN.ext << EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names
[alt_names]
DNS.1 = $DOMAIN
EOF

openssl x509 -req -in $DOMAIN.csr -CA ../myCA.pem -CAkey ../myCA.key -CAcreateserial \
-out $DOMAIN.crt -days 365 -sha256 -extfile $DOMAIN.ext