Skip to content

Instantly share code, notes, and snippets.

@wouterds
Last active May 2, 2017 12:35
Show Gist options
  • Select an option

  • Save wouterds/b9bb126e26dbf3bdba2ee9cec24a1c02 to your computer and use it in GitHub Desktop.

Select an option

Save wouterds/b9bb126e26dbf3bdba2ee9cec24a1c02 to your computer and use it in GitHub Desktop.
Script to generate self signed certificate working with Chrome 58+
#!/bin/bash
if [ ! -d "./ssl" ]; then
mkdir ./ssl
fi
# Hostname
HOSTNAME=$1
# Fallback to "server" if no hostname provided
if [ -z "$HOSTNAME" ]; then
HOSTNAME=server
fi
# Get valid filename from hostname
FILENAME=${HOSTNAME/\*\./}
# OpenSSL Dir
OPENSSL_DIR=$(openssl version -d)
OPENSSL_DIR=${OPENSSL_DIR/OPENSSLDIR\: /}
OPENSSL_DIR=${OPENSSL_DIR/\"/}
OPENSSL_DIR=${OPENSSL_DIR/\"/}
# Generate key
openssl genrsa -out ./ssl/$FILENAME.key 2048
# Generate certificate
openssl req \
-new \
-x509 \
-sha256 \
-days 3650 \
-key ./ssl/$FILENAME.key \
-out ./ssl/$FILENAME.crt \
-subj /CN=$HOSTNAME \
-reqexts SAN \
-extensions SAN \
-config <(cat $OPENSSL_DIR/openssl.cnf <(printf '[SAN]\nsubjectAltName=DNS:'$HOSTNAME))
# Show info message to user to trust certificate
echo ""
echo "To trust this certificate, run the following: "
echo " sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ./ssl/$FILENAME.crt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment