-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-certificates.sh
60 lines (49 loc) · 1.53 KB
/
create-certificates.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env bash
# Generates a wildcard certificate for a given domain name.
set -e
if [ -z "$1" ]; then
echo -e "\e[43mMissing domain name!\e[49m"
echo
echo "Usage: $0 example.com"
echo
echo "This will generate a wildcard certificate for the given domain name and its subdomains."
exit
fi
DOMAIN=$1
if [ ! -f "ca.key" ]; then
echo -e "\e[41mCertificate Authority private key does not exist!\e[49m"
echo
echo -e "Please run \e[93mcreate-ca.sh\e[39m first."
exit
fi
# Generate a private key
openssl genrsa -out "$DOMAIN.key" 2048
# Create a certificate signing request
openssl req -new -subj "/C=US/O=Local Development/CN=$DOMAIN" -key "$DOMAIN.key" -out "$DOMAIN.csr"
# Create a config file for the extensions
>"$DOMAIN.ext" cat <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth, clientAuth
subjectAltName = @alt_names
[alt_names]
DNS.1 = $DOMAIN
DNS.2 = *.$DOMAIN
EOF
# Create the signed certificate
openssl x509 -req \
-in "$DOMAIN.csr" \
-extfile "$DOMAIN.ext" \
-CA ca.crt \
-CAkey ca.key \
-CAcreateserial \
-out "$DOMAIN.crt" \
-days 365 \
-sha256
rm "$DOMAIN.csr"
rm "$DOMAIN.ext"
echo -e "\e[42mSuccess!\e[49m"
echo
echo -e "You can now use \e[93m$DOMAIN.key\e[39m and \e[93m$DOMAIN.crt\e[39m in your web server."
echo -e "Don't forget that \e[1myou must have imported \e[93mca.crt\e[39m in your browser\e[0m to make it accept the certificate."