This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkca.sh
executable file
·143 lines (138 loc) · 5.04 KB
/
mkca.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/bash
##
## mkca.sh -- Create Certificate Authority
## Copyright (c) 2011-2015 Michael Bekaert, All Rights Reserved.
##
## This script is derived from mkcert.sh from the mod_ssl distribution.
## gid-mkcert.sh -- Create Certificates for Global Server ID facility
## Copyright (c) 1998-2001 Ralf S. Engelschall, All Rights Reserved.
##
## It requires OpenSSL 0.9.4.
##
# parameters
openssl="openssl"
sslcrtdir="."
sslcsrdir="."
sslkeydir="."
sslbit="4096"
# some optional terminal sequences
case $TERM in
xterm|xterm*|vt220|vt220*)
T_MD=$(echo dummy | awk '{ printf("%c%c%c%c", 27, 91, 49, 109); }')
T_ME=$(echo dummy | awk '{ printf("%c%c%c", 27, 91, 109); }')
;;
vt100|vt100*)
T_MD=$(echo dummy | awk '{ printf("%c%c%c%c%c%c", 27, 91, 49, 109, 0, 0); }')
T_ME=$(echo dummy | awk '{ printf("%c%c%c%c%c", 27, 91, 109, 0, 0); }')
;;
default)
T_MD=''
T_ME=''
;;
esac
# find some random files
# (do not use /dev/random here, because this device
# doesn't work as expected on all platforms)
randfiles=''
for file in /var/log/messages /var/adm/messages \
/kernel /vmunix /vmlinuz \
/etc/hosts /etc/resolv.conf; do
if [ -f $file ]; then
if [ ".$randfiles" = . ]; then
randfiles="$file"
else
randfiles="${randfiles}:$file"
fi
fi
done
echo "${T_MD}mkca.sh -- Create Certificate Authority${T_ME}"
if [ ! -f $sslcrtdir/ca.crt ]; then
echo ""
echo "${T_MD}Generating custom Certificate Authority (CA)${T_ME}"
echo "______________________________________________________________________"
echo ""
echo "${T_MD}STEP 1: Generating RSA private key for CA (${sslbit} bit)${T_ME}"
if [ ! -f "$HOME/.rnd" ]; then
touch "$HOME/.rnd"
fi
if [ ".$randfiles" != . ]; then
$openssl genrsa -aes256 -rand $randfiles \
-out $sslkeydir/ca.key \
$sslbit
else
$openssl genrsa -aes256 -out $sslkeydir/ca.key \
$sslbit
fi
if [ $? -ne 0 ]; then
echo "Error: Failed to generate RSA private key" 1>&2
exit 1
fi
echo "______________________________________________________________________"
echo ""
echo "${T_MD}STEP 2: Generating X.509 certificate signing request for CA${T_ME}"
cat >.mkcert.cfg <<EOT
[ req ]
default_bits = ${sslbit}
distinguished_name = req_DN
[ req_DN ]
countryName = "1. Country Name (2 letter code)"
countryName_default = WF
countryName_min = 2
countryName_max = 2
0.organizationName = "4. Organization Name (eg, company) "
0.organizationName_default = Certificate Authority World Dominators
organizationalUnitName = "5. Organizational Unit Name (eg, section) "
organizationalUnitName_default = Certificate Authority
commonName = "6. Common Name (eg, CA name) "
commonName_max = 64
commonName_default = Certificate Authority World Dominators CA
emailAddress = "7. Email Address (eg, name@FQDN)"
emailAddress_max = 40
emailAddress_default = ca@cawd.wf
EOT
$openssl req -nodes -sha512 -newkey rsa:$sslbit \
-config .mkcert.cfg \
-new \
-key $sslkeydir/ca.key \
-out $sslcsrdir/ca.csr
if [ $? -ne 0 ]; then
echo "Error: Failed to generate certificate signing request" 1>&2
exit 1
fi
echo "______________________________________________________________________"
echo ""
echo "${T_MD}STEP 3: Generating X.509 certificate for CA signed by itself${T_ME}"
cat >.mkcert.cfg <<EOT
extensions = x509v3
[ x509v3 ]
subjectAltName = email:copy
basicConstraints = CA:true,pathlen:0
nsComment = "Certificate Authority World Dominators"
nsCertType = sslCA
EOT
$openssl x509 -extfile .mkcert.cfg \
-sha512 \
-days 3650 \
-signkey $sslkeydir/ca.key \
-in $sslcsrdir/ca.csr -req \
-out $sslcrtdir/ca.crt
if [ $? -ne 0 ]; then
echo "Error: Failed to generate self-signed CA certificate" 1>&2
exit 1
fi
echo "______________________________________________________________________"
echo ""
echo "${T_MD}RESULT:${T_ME}"
echo '01' >.mkca.serial
$openssl verify $sslcrtdir/ca.crt
if [ $? -ne 0 ]; then
echo "Error: Failed to verify resulting X.509 certificate" 1>&2
exit 1
fi
rm -f .mkcert.cfg .$randfiles
chmod 400 $sslkeydir/ca.key $sslcsrdir/ca.csr $sslcrtdir/ca.crt
echo -e "${T_MD}.mkca.serial${T_ME}\t\tCA serial number(local shared)"
echo -e "${T_MD}$sslkeydir/ca.key${T_ME}\t\tPrivate Key (private)"
echo -e "${T_MD}$sslcsrdir/ca.csr${T_ME}\t\tCertificate Signing Request (public)"
echo -e "${T_MD}$sslcrtdir/ca.crt${T_ME}\t\tCertificate (public)"
fi