-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-self-signed-cert.sh
58 lines (50 loc) · 1.81 KB
/
generate-self-signed-cert.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
#!/bin/sh
if [ ! -d "/out" ]; then
echo "Volume not set correctly, generated certficate will not be populated to host"
mkdir /out
fi
if [ -z "$DOMAIN" ]
then
echo "DOMAIN environment variable is not assigned, setting localhost";
DOMAIN=localhost;
fi
if [ -z "$DAYS" ]
then
echo "DAYS environment variable is not assigned, setting 365 days";
DAYS=365;
fi
if [ -n "$PASSWORD" ]; then
privateKeyEncryption=" -passout pass:${PASSWORD}"
else
echo "PASSWORD environment variable is not assigned, pfx file will contain NOT encrypted private key"
privateKeyEncryption=" -nodes -passout pass:"
fi
if [ -n "$ALT_DOMAINS" ]; then
echo "Alternative names provided: ${ALT_DOMAINS}"
req_additional=" -extensions v3_req -config /var/www/example.com/cert/custom-openssl.cnf"
set_main="DNS.1 = ${DOMAIN}"
echo "$set_main"
echo "$set_main" >> /var/www/example.com/cert/custom-openssl.cnf
domains=$(echo $ALT_DOMAINS | tr ";" "\n")
i=2
for domain in $domains
do
if [ "$domain" = "$DOMAIN" ]; then
continue
fi
set="DNS.${i} = ${domain}"
echo "$set"
echo "$set" >> /var/www/example.com/cert/custom-openssl.cnf
i=$(expr $i + 1)
done
set="DNS.${i} = ${DOMAIN}"
echo "$set"
echo "$set" >> /var/www/example.com/cert/custom-openssl.cnf
else
req_additional=""
fi
openssl genrsa -out /out/${DOMAIN}.key 2048
openssl req -x509 -new -key /out/${DOMAIN}.key -out /out/${DOMAIN}.crt${privateKeyEncryption} -days ${DAYS} -subj "/C=${C}/ST=${ST}/L=${L}/O=${O}/OU=${OU}/CN=${DOMAIN}/emailAddress=${EMAIL}"${req_additional}
openssl pkcs12 -export -in /out/${DOMAIN}.crt -inkey /out/${DOMAIN}.key -out /out/${DOMAIN}.pfx${privateKeyEncryption}
openssl x509 -in /out/${DOMAIN}.crt -out /out/${DOMAIN}.pem -outform PEM
openssl x509 -text -in /out/${DOMAIN}.crt > /out/${DOMAIN}.crt.txt