-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprovision-certificate.sh
80 lines (75 loc) · 2.31 KB
/
provision-certificate.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
#!/bin/bash
source /vagrant/lib.sh
domain=$1
ca_file_name='example-ca'
ca_common_name='Example CA'
mkdir -p /vagrant/shared/tls/$ca_file_name
cd /vagrant/shared/tls/$ca_file_name
# create the CA certificate.
if [ ! -f $ca_file_name-crt.pem ]; then
openssl genrsa \
-out $ca_file_name-key.pem \
2048 \
2>/dev/null
chmod 400 $ca_file_name-key.pem
openssl req -new \
-sha256 \
-subj "/CN=$ca_common_name" \
-key $ca_file_name-key.pem \
-out $ca_file_name-csr.pem
openssl x509 -req -sha256 \
-signkey $ca_file_name-key.pem \
-extensions a \
-extfile <(echo "[a]
basicConstraints=critical,CA:TRUE,pathlen:0
keyUsage=critical,digitalSignature,keyCertSign,cRLSign
") \
-days 365 \
-in $ca_file_name-csr.pem \
-out $ca_file_name-crt.pem
openssl x509 \
-in $ca_file_name-crt.pem \
-outform der \
-out $ca_file_name-crt.der
# dump the certificate contents (for logging purposes).
#openssl x509 -noout -text -in $ca_file_name-crt.pem
fi
# trust the CA.
if [ ! -f /usr/local/share/ca-certificates/example-ca.crt ]; then
cp example-ca-crt.pem /usr/local/share/ca-certificates/example-ca.crt
update-ca-certificates -v
fi
if [ "$domain" != '' ] && [ ! -f $domain-crt.pem ]; then
openssl genrsa \
-out $domain-key.pem \
2048 \
2>/dev/null
chmod 400 $domain-key.pem
openssl req -new \
-sha256 \
-subj "/CN=$domain" \
-key $domain-key.pem \
-out $domain-csr.pem
openssl x509 -req -sha256 \
-CA $ca_file_name-crt.pem \
-CAkey $ca_file_name-key.pem \
-CAcreateserial \
-extensions a \
-extfile <(echo "[a]
subjectAltName=DNS:$domain
extendedKeyUsage=critical,serverAuth
") \
-days 365 \
-in $domain-csr.pem \
-out $domain-crt.pem
openssl pkcs12 -export \
-keyex \
-inkey $domain-key.pem \
-in $domain-crt.pem \
-certfile $domain-crt.pem \
-passout pass: \
-out $domain-key.p12
# dump the certificate contents (for logging purposes).
#openssl x509 -noout -text -in $domain-crt.pem
#openssl pkcs12 -info -nodes -passin pass: -in $domain-key.p12
fi