-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-root-ca.sh
executable file
·55 lines (44 loc) · 1.26 KB
/
generate-root-ca.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
#!/usr/bin/env bash
set -e
DIR=$(pwd $(dirname $(dirname ${BASH_SOURCE[0]})))
DIR_CERT=$DIR/.cert
[[ -f $DIR/.env ]] && echo "sourcing $DIR/.env" && source $DIR/.env
HOST=${HTTP_HOST:-localhost}
echo "> configuration:"
echo " \$DIR_CERT: $DIR_CERT"
echo " \$HOST: $HOST"
echo
echo "> Generating Root CA key"
openssl genrsa -des3 -out $DIR_CERT/rootCA.key 2048
echo
echo "> Generating Root CA certificate"
openssl req -x509 -new -nodes -key $DIR_CERT/rootCA.key -sha256 -days 1024 -out $DIR_CERT/rootCA.pem
echo
echo "> Generating certificate key"
openssl genrsa -des3 -out $DIR_CERT/$HOST.key 1024
echo
echo "> Generating Certificate signing request"
openssl req -nodes -sha256 -newkey rsa:2048 -keyout $DIR_CERT/$HOST.key -out $DIR_CERT/$HOST.csr -config <( cat<<EOF
[req]
prompt = no
distinguished_name = dn
req_extensions = req_ext
x509_extensions = usr_cert
[dn]
C=BG
ST=Sofia
L=Sofia
O=publicvoidltd
OU=IT
CN=$HOST
[ req_ext ]
subjectAltName=DNS:$HOST
[ usr_cert ]
subjectAltName=DNS:$HOST
EOF
)
echo
echo "> Signing CSR with root key"
openssl x509 -req -in $DIR_CERT/$HOST.csr -CA $DIR_CERT/rootCA.pem -CAkey $DIR_CERT/rootCA.key -CAcreateserial -out $DIR_CERT/$HOST.crt -days 500 -sha256 -extfile <(printf "subjectAltName=DNS:$HOST")
echo