-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathpki.sh
executable file
·61 lines (49 loc) · 1.66 KB
/
pki.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
#!/usr/bin/env bash
# https://github.com/drduh/config/blob/master/scripts/pki.sh
# https://tools.ietf.org/html/rfc5280
#set -x # uncomment to debug
set -o errtrace
set -o nounset
set -o pipefail
umask 077
readonly OPENSSL="/usr/bin/openssl"
readonly OPENSSL_CONF="./openssl.cnf"
readonly CA_DAYS="3653"
readonly CERT_DAYS="90"
readonly DEFAULT_MD="sha512"
readonly KEYSIZE="4096"
if [[ ! -x ${OPENSSL} ]] ; then
printf "need ${OPENSSL}\n" ; exit 1
fi
if [[ ! -f ${OPENSSL_CONF} ]] ; then
printf "need ${OPENSSL_CONF}\n" ; exit 1
fi
for name in CN_AUTHORITY CN_SERVER CN_CLIENT ; do
readonly $name="$(tr -dc '[:xdigit:]' < /dev/urandom | fold -w 10 | head -n1)"
done
for key in ca client server ; do
${OPENSSL} genrsa -out $key.key ${KEYSIZE}
done
${OPENSSL} req -new -x509 -days ${CA_DAYS} -${DEFAULT_MD} \
-subj "/CN=${CN_AUTHORITY}" \
-config ${OPENSSL_CONF} -extensions v3_ca \
-set_serial "0x$(${OPENSSL} rand -hex 32)" \
-key ca.key -out ca.pem
${OPENSSL} req -new -${DEFAULT_MD} \
-subj "/CN=${CN_SERVER}" \
-key server.key -out server.csr
${OPENSSL} req -new -${DEFAULT_MD} \
-subj "/CN=${CN_CLIENT}" \
-key client.key -out client.csr
${OPENSSL} x509 -req -days ${CERT_DAYS} -${DEFAULT_MD} \
-extfile ${OPENSSL_CONF} -extensions tls_server \
-CA ca.pem -CAkey ca.key \
-set_serial "0x$(${OPENSSL} rand -hex 32)" \
-in server.csr -out server.pem
${OPENSSL} x509 -req -days ${CERT_DAYS} -${DEFAULT_MD} \
-extfile ${OPENSSL_CONF} -extensions tls_client \
-CA ca.pem -CAkey ca.key \
-set_serial "0x$(${OPENSSL} rand -hex 32)" \
-in client.csr -out client.pem
#${OPENSSL} pkcs12 -inkey client.key -in client.pem \
# -export -out client.pfx