-
Notifications
You must be signed in to change notification settings - Fork 4
/
mk-ovpn2
executable file
·356 lines (287 loc) · 9.75 KB
/
mk-ovpn2
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/bin/bash
if [ -z "$BASH_VERSION" ];then exec bash "$0" "$@";else set +o posix;fi
set -e
umask 077
export OPENSSL_CONF=/dev/null
[ "$CERT_DIR" = "" ] && [ "$1" != "" ] && [ -d "$1" ] && CERT_DIR="$1"
CERT_DIR="${CERT_DIR:-certs}"
CONF_DIR="$CERT_DIR-conf"
TMP=/tmp/ca_tmp.$$
trap 'rm -rf "$TMP"' 0 1 2
[ -d "$CERT_DIR" ] || { echo >&2 ERROR: Certificate directory missing ; exit 1;}
build_files() {
rm -rf "$TMP"
mkdir "$TMP"
mkdir -p "$CONF_DIR"
SERVERCOUNT=0
SERVERNAME=
SERVEROU=
rm -f "$TMP"/clients.CA-list.pem "$TMP"/servers.CA-list.pem ||:
# Add all the server CA certificates.
for cert in "$CERT_DIR"/*.crt
do
CN="$(basename "$cert" .crt)"
echo >&2 Checking "$CN"
fetch_pems "$CN" >&2
[ -s "$TMP"/client.ca ] || continue
save_certs "$CN"
restore_certs "$CN"
SUSAGE=$(openssl x509 -purpose -noout -in "$TMP"/client.crt 2>/dev/null |
grep -q 'SSL server *: *Yes' &&
echo yes || echo no)
CUSAGE=$(openssl x509 -purpose -noout -in "$TMP"/client.crt 2>/dev/null |
grep -q 'SSL client *: *Yes' &&
echo yes || echo no)
[ "$SUSAGE" = yes ] &&
SUSAGE=$(openssl verify -check_ss_sig -purpose sslserver \
-CAfile "$TMP"/client.ca "$TMP"/client.crt 2>&1 | grep -iq ^error &&
echo no || echo yes)
[ "$CUSAGE" = yes ] &&
CUSAGE=$(openssl verify -check_ss_sig -purpose sslclient \
-CAfile "$TMP"/client.ca "$TMP"/client.crt 2>&1 | grep -iq ^error &&
echo no || echo yes)
DESC="$(openssl x509 -subject -issuer -in "$TMP"/client.crt)"
case "$CUSAGE$SUSAGE" in
yesyes )
case "$DESC" in
*lient* ) touch "$TMP"/type.client."$CN".flg ;;
*erver* ) touch "$TMP"/type.server."$CN".flg ;;
* ) touch "$TMP"/type.client."$CN".flg ;;
esac
;;
yesno ) touch "$TMP"/type.client."$CN".flg ;;
noyes ) touch "$TMP"/type.server."$CN".flg ;;
* )
openssl verify -check_ss_sig -purpose sslserver \
-CAfile "$TMP"/client.ca "$TMP"/client.crt >&2 ||:
echo >&2 "ERROR: Skipped $CN, unusable certificate."
continue
;;
esac
# Dup check
ISSU="$(openssl x509 -issuer_hash -noout < "$TMP"/client.crt 2>/dev/null)"
CAPUB="$(openssl x509 -pubkey -noout < "$TMP"/client.ca 2>/dev/null |
openssl md5 | awk '{print $NF;}' )"
[ -f "$TMP"/type.server."$CN".flg ] && {
SERVERCOUNT=$((SERVERCOUNT+1))
SERVERNAME="$(openssl x509 -noout -in "$TMP"/client.crt \
-subject -nameopt multiline | sed -n 's/ *commonName *= //p')"
OU="$(openssl x509 -noout -in "$TMP"/client.crt -subject \
-nameopt multiline | sed -n 's/ *organizationalUnitName *= //p')"
# If they have a common (simple) OU
if [ "$SERVERCOUNT" = 1 ]
then SERVEROU="$OU"
else [ "$OU" != "$SERVEROU" ] && SERVEROU=""
fi
[ "$(echo "$OU" | wc -l)" != 1 ] && SERVEROU=""
[ -f "$TMP/dup.server.$ISSU.$CAPUB.flg" ] || {
touch "$TMP/dup.server.$ISSU.flg"
touch "$TMP/dup.server.$ISSU.$CAPUB.flg"
openssl x509 -subject -serial -dates -in "$TMP"/client.ca >> "$TMP"/servers.CA-list.pem
}
}
[ -f "$TMP"/type.client."$CN".flg ] && {
[ -f "$TMP/dup.client.$ISSU.$CAPUB.flg" ] || {
touch "$TMP/dup.client.$ISSU.flg"
touch "$TMP/dup.client.$ISSU.$CAPUB.flg"
{
openssl x509 -subject -serial -dates -in "$TMP"/client.ca
echo
} >> "$TMP"/clients.CA-list.pem
}
}
done
[ -s "$TMP"/servers.CA-list.pem ] || {
echo >&2 ERROR: No servers have been defined.
exit 1
}
[ -s "$TMP"/clients.CA-list.pem ] || {
echo >&2 ERROR: No clients have been defined.
exit 1
}
FLG=1
for file in "$CERT_DIR"/*pattern*.ovpn
do
if [ -f "$file" ]
then tfile="$(basename "$file")"
tfile="${tfile/pattern/$CN}"
else continue
fi
FLG=0
done
[ "$FLG" = 0 ] || {
echo >&2 ERROR: No ovpn pattern files defined.
exit 1
}
# Protect the TLS handshake from s-kiddies.
[ -f "$CERT_DIR"/tls-crypt.pvk ] ||
openvpn --genkey --secret "$CERT_DIR"/tls-crypt.pvk
# For all the client CA certificates
for cert in "$CERT_DIR"/*.crt
do
CN="$(basename "$cert" .crt)"
[ "$CN" = "*" ] && continue
[ -f "$TMP"/type.client."$CN".flg ] || continue
echo Client "$CN"
restore_certs "$CN"
[ -s "$TMP"/client.ca ] || continue
CLIENTNAME="$(openssl x509 -noout -in "$TMP"/client.crt \
-subject -nameopt multiline | sed -n 's/ *commonName *= //p')"
[ "$CLIENTNAME" != "$CN" ] &&
echo "WARNING: Certificate common name is '$CLIENTNAME'"
ISSU="$(openssl x509 -issuer_hash -noout < "$TMP"/client.crt 2>/dev/null)"
[ -f "$TMP/dup.server.$ISSU.flg" ] &&
echo WARNING: This client has the same CA as a server.
for file in "$CERT_DIR"/*pattern*.ovpn
do
if [ -f "$file" ]
then tfile="$(basename "$file")"
tfile="${tfile/pattern/$CN}"
else continue
fi
{
echo "# Name: $CN"
cat "$file"
echo
# Stop the deamon whinging.
# This has NO security effect as the CAs are unique.
# (Even if the clients are the same OU)
if [ "$SERVERCOUNT" -eq 1 ] && [ "$SERVERNAME" != "" ]
then
# Not on 2.4.1 Windows, ENABLE_X509ALTUSERNAME missing
# echo x509-username-field CN
echo "verify-x509-name '$SERVERNAME' name"
elif [ "$SERVEROU" != "" ]
then
echo x509-username-field OU
echo "verify-x509-name '$SERVEROU' name"
fi
echo
[ -s "$CERT_DIR"/tls-crypt.pvk ] && {
if ! grep -q tls-auth "$file"
then
echo '<tls-crypt>'
cat "$CERT_DIR"/tls-crypt.pvk
echo '</tls-crypt>'
fi
}
echo '<cert>'
openssl x509 -subject -serial -dates -in "$TMP"/client.crt
echo '</cert>'
echo '<key>'
openssl pkey -in "$TMP"/client.key
echo '</key>'
echo '<ca>'
cat "$TMP"/servers.CA-list.pem
echo '</ca>'
} | sed 's/$//' > "$TMP"/"$tfile"
try_file "$CONF_DIR" "$tfile"
done
rm -f "$TMP"/client.key "$TMP"/client.crt ||:
done
# Copy all the certificates and keys for the servers.
for cert in "$CERT_DIR"/*.crt
do
CN="$(basename "$cert" .crt)"
[ -f "$TMP"/type.server."$CN".flg ] || continue
echo "Server $CN"
restore_certs "$CN"
[ -s "$TMP"/client.ca ] || continue
ISSU="$(openssl x509 -issuer_hash -noout < "$TMP"/client.crt 2>/dev/null)"
[ -f "$TMP/dup.client.$ISSU.flg" ] &&
echo WARNING: This server has the same CA as a client.
openssl x509 -subject -serial -dates -in "$TMP"/client.crt > "$TMP"/"$CN".crt
openssl pkey -in "$TMP"/client.key > "$TMP"/"$CN".key
cp -p "$TMP"/clients.CA-list.pem "$TMP"/"$CN".ca-list.pem
cp -p "$CERT_DIR"/tls-crypt.pvk "$TMP"/"$CN".tls-crypt.pvk
try_file "$CONF_DIR" "$CN".crt
try_file "$CONF_DIR" "$CN".key
try_file "$CONF_DIR" "$CN".ca-list.pem
try_file "$CONF_DIR" "$CN".tls-crypt.pvk
[ -f "$CERT_DIR"/dhparam.pem ] && {
cp -p "$CERT_DIR"/dhparam.pem "$TMP"/"$CN".dhparam.pem
try_file "$CONF_DIR" "$CN".dhparam.pem
}
done
}
fetch_pems() {
CN="$1"
# This checks the certificates and allows us to put them anywhere
rm -f "$TMP"/client.key "$TMP"/client.crt "$TMP"/client.ca \
"$TMP"/client.pfx "$TMP"/client.pem "$TMP"/client.ca.pem
touch "$TMP"/client.crt "$TMP"/client.key
for i in "$CERT_DIR"/"$CN".key "$CERT_DIR"/"$CN".crt "$CERT_DIR"/"$CN".pem "$CERT_DIR"/"$CN".ca.crt "$CERT_DIR"/"$CN".ca.cer
do
[ -s "$i" ] || continue
[ -s "$TMP"/client.crt ] || {
openssl x509 -in "$i" > "$TMP"/client.crt 2>/dev/null ||:
}
[ -s "$TMP"/client.key ] || {
openssl pkey -in "$i" > "$TMP"/client.key 2>/dev/null ||:
}
cat "$i" >> "$TMP"/client.ca.pem
done
[ -s "$CERT_DIR"/ca.crt ] && cat "$CERT_DIR"/ca.crt >> "$TMP"/client.ca.pem
[ -s "$CERT_DIR"/ca.pem ] && cat "$CERT_DIR"/ca.pem >> "$TMP"/client.ca.pem
for i in "$CERT_DIR"/*[-.]ca.*
do [ -s "$i" ] && cat "$i" >> "$TMP"/client.ca.pem
done
CERTPUB="$(openssl x509 -pubkey -noout -in "$TMP"/client.crt 2>/dev/null ||:)"
if [ "$CERTPUB" = '' ]
then echo "ERROR: Skipped $CN, no certificate."
rm -f "$TMP"/client.pfx "$TMP"/client.pem "$TMP"/client.ca.pem
rm -f "$TMP"/client.crt "$TMP"/client.ca "$TMP"/client.key
return
fi
PKEYPUB="$(openssl pkey -pubout -in "$TMP"/client.key 2>/dev/null ||:)"
if [ "$CERTPUB" != "$PKEYPUB" ]
then echo "ERROR: Skipped $CN, public keys don't match"
rm -f "$TMP"/client.pfx "$TMP"/client.pem "$TMP"/client.ca.pem
rm -f "$TMP"/client.crt "$TMP"/client.ca "$TMP"/client.key
return
fi
ISSU="$(openssl x509 -issuer_hash -noout -in "$TMP"/client.crt 2>/dev/null ||:)"
SUBJ="#"
k=0
while :
do ((k+=1))
awk -v k=$k '/^-----BEGIN/ && k>1 {k--;next;} /^-----BEGIN/&& k {disp=1;} disp{print;} /^-----END/&&disp{disp=0;k=0;}' \
< "$TMP"/client.ca.pem > "$TMP"/client.ca
[ -s "$TMP"/client.ca ] || break
SUBJ="$(openssl x509 -subject_hash -noout < "$TMP"/client.ca 2>/dev/null ||:)"
[ "$SUBJ" == "$ISSU" ] && break
done
rm -f "$TMP"/client.pem "$TMP"/client.ca.pem
# Check them.
SUBJ="$(openssl x509 -subject_hash -noout < "$TMP"/client.ca 2>/dev/null ||:)"
# CASER="$(openssl x509 -serial -noout < "$TMP"/client.ca 2>/dev/null ||:)"
if [ "$SUBJ" != "$ISSU" ]
then echo "ERROR: Skipped $CN, issuer not found"
rm -f "$TMP"/client.crt "$TMP"/client.ca "$TMP"/client.key
return 0
fi
return 0
}
save_certs() {
CN="$1"
mv "$TMP"/client.crt "$TMP"/saved."$CN".crt
mv "$TMP"/client.key "$TMP"/saved."$CN".key
mv "$TMP"/client.ca "$TMP"/saved."$CN".ca
}
restore_certs() {
CN="$1"
rm -f "$TMP"/client.key "$TMP"/client.crt "$TMP"/client.ca "$TMP"/client.pfx "$TMP"/client.pem "$TMP"/client.ca.pem
cp "$TMP"/saved."$CN".crt "$TMP"/client.crt
cp "$TMP"/saved."$CN".key "$TMP"/client.key
cp "$TMP"/saved."$CN".ca "$TMP"/client.ca
}
try_file() {
[ ! -s "$1"/"$2" ] &&
cp -p "$TMP"/"$2" "$1"/"$2"
if ! cmp -s "$TMP"/"$2" "$1"/"$2"
then cp -p "$TMP"/"$2" "$1"/"$2"
fi
[ -f "$TMP"/"$2" ] && rm "$TMP"/"$2"
}
build_files