-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_me_https.sh
executable file
·97 lines (77 loc) · 2.89 KB
/
make_me_https.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env bash
# Generates your own Certificate Authority (CA) for development.
# This section of script should be executed just once.
set -e
if [ -f "ca.crt" ] || [ -f "ca.key" ]; then
echo -e "\e[41mCertificate Authority files already exist!\e[49m"
echo
echo -e "You only need a single CA even if you need to create multiple certificates."
echo -e "This way, you only ever have to import the certificate in your browser once."
echo
echo -e "If you want to restart from scratch, delete the \e[93mca.crt\e[39m and \e[93mca.key\e[39m files."
else
# Generate private key
openssl genrsa -out ca.key 2048
# Generate root certificate
openssl req -x509 -new -nodes -subj "/C=US/O=_Development CA/CN=Development certificates" -key ca.key -sha256 -days 3650 -out ca.crt
echo -e "\e[42mSuccess!\e[49m"
echo
echo "The following files have been written:"
echo -e " - \e[93mca.crt\e[39m is the public certificate that should be imported in your browser"
echo -e " - \e[93mca.key\e[39m is the private key that will be used by \e[93mcreate-certificate.sh\e[39m"
echo
echo "Next steps:"
echo -e " - Import \e[93mca.crt\e[39m in your browser"
echo -e " - run \e[93mcreate-certificate.sh example.com\e[39m"
fi
# 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 3650 \
-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."
clear
echo -e " - ca.crt is the public certificate that should be imported in your browser"
echo -e "- use $DOMAIN.key.key and $DOMAIN.key.crt in your web server."