Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple alternative domain names #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ To generate a certificate for `example.dev` and its subdomains, run:
create-certificate.sh example.dev
```

To generate a certificate for `example.dev` along with one or more [alternative domains](https://en.wikipedia.org/wiki/Subject_Alternative_Name), run:

```
create-certificate.sh example.dev alternative-domain.dev alternative-domain.test
```

![create-certificate.sh](https://raw.githubusercontent.com/BenMorel/dev-certificates/main/images/create-certificate.png)

You can now install the `.key` and `.crt` files in your web server, such as Apache or Nginx.
Expand Down
23 changes: 19 additions & 4 deletions create-certificate.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
#!/usr/bin/env bash

# Generates a wildcard certificate for a given domain name.
# Generates a wildcard certificate for a given domain name with optional alternative domain names.

set -e

if [ -z "$1" ]; then
echo -e "\e[43mMissing domain name!\e[49m"
echo
echo "Usage: $0 example.com"
echo "Usage: $0 example.dev"
echo
echo "This will generate a wildcard certificate for the given domain name and its subdomains."
echo
echo "Usage: $0 example.dev alternative-domain.dev"
echo
echo "This will generate a wildcard certificate for the given domain name, alternative domains and all subdomains for each of them."
exit
fi

DOMAIN=$1
ALT_NAME_INDEX=0

if [ ! -f "ca.key" ]; then
echo -e "\e[41mCertificate Authority private key does not exist!\e[49m"
Expand All @@ -36,10 +41,20 @@ keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth, clientAuth
subjectAltName = @alt_names
[alt_names]
DNS.1 = $DOMAIN
DNS.2 = *.$DOMAIN
EOF

function add_alt_name() {
ALT_NAME_INDEX=$((ALT_NAME_INDEX + 1))
>>"$DOMAIN.ext" cat <<-EOF
DNS.$ALT_NAME_INDEX = $1
EOF
}

for domain_alt in "${@}"; do
add_alt_name $domain_alt
add_alt_name "*.$domain_alt"
done

# Create the signed certificate
openssl x509 -req \
-in "$DOMAIN.csr" \
Expand Down