-
Notifications
You must be signed in to change notification settings - Fork 1
/
ovpn-ca.zsh
executable file
·255 lines (226 loc) · 5.82 KB
/
ovpn-ca.zsh
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
#!/bin/zsh -e
umask 'u=rwx,g=,o='
zmodload zsh/datetime
server=${server:-$s}
client=${client:-$c}
[[ -r config ]] && source config
[[ -r ${server}/config ]] && source ${server}/config
action=$1
y=${y:-1}
key=${key:-'ed448'}
subj=${subj:-'/CN=${cn}'}
[[ $key =~ '^rsa' ]] && digest=${digest:-'-sha512'}
pwgen=(pwgen -s -c -n 32 1)
caYears=10
serverYears=10
openssl='/usr/bin/openssl'
openvpn='/usr/sbin/openvpn'
export caPassword
typeset -A file
file=(
'ca_key' "${server}/ca/${server}_vpn_ca-key.pem"
'ca_cert' "${server}/ca/${server}_vpn_ca-cert.pem"
'dh' "${server}/${server}/dh.pem"
'server_csr' "${server}/${server}/${server}-csr.pem"
'server_key' "${server}/${server}/${server}-key.pem"
'server_cert' "${server}/${server}/${server}-cert.pem"
'tls_crypt' "${server}/${server}/${server}-tlscrypt-key.pem"
'config_template' "${server}/${type:+${type}_}client_template.ovpn"
'client_csr' "${server}/${client}/${client}-csr.pem"
'client_key' "${server}/${client}/${client}-key.pem"
'client_cert' "${server}/${client}/${client}-cert.pem"
'client_config' "${server}/${client}/${client}@${server}.ovpn"
)
function Subj
{
local cn=$1
print ${(e)subj}
}
# returns true number of days in $1 years
function Days
{
local years=$1
strftime -s now %s
strftime -s years -r '%F %T' "$(( $(strftime %Y)+$years ))-$(strftime '%m-%d %T')"
print $(( (years-now)/(24*60*60) ))
}
function MakeDHParams
{
print "Generating DH params."
$openssl dhparam -out ${file[dh]} ${key#rsa:}
}
function MakeTLSCrypt
{
print "Generating TLS crypt key."
$openvpn --genkey tls-crypt ${file[tls_crypt]}
}
function MakeCAPassword
{
print "Generated passphrase for CA private key:"
caPassword=$($pwgen)
print "Save it in the safe place!"
print $caPassword
}
function MakeCA
{
print "Creating CA for $caYears years."
$openssl req\
-utf8\
-new\
-x509\
-passout 'env:caPassword'\
$digest\
-newkey $key\
-days $(Days $caYears)\
-subj "$(Subj "$server vpn ca")"\
-out ${file[ca_cert]}\
-keyout ${file[ca_key]}
}
function MakeServerCertificate
{
print "Creating server for $serverYears years."
$openssl req\
-utf8\
-new\
-newkey $key\
-nodes\
-subj "$(Subj "$server vpn server")"\
-out ${file[server_csr]}\
-keyout ${file[server_key]}\
-addext 'basicConstraints = CA:FALSE'\
-addext 'keyUsage = digitalSignature, keyEncipherment'\
-addext 'extendedKeyUsage = serverAuth'
$openssl x509\
-req\
-passin 'env:caPassword'\
-in ${file[server_csr]}\
-CA ${file[ca_cert]}\
-CAkey ${file[ca_key]}\
-CAcreateserial\
-out ${file[server_cert]}\
-days $(Days $serverYears)\
$digest\
-copy_extensions copy
}
function MakeClientCertificate
{
print "Creating ${client}@${server} for $y years."
$openssl req\
-utf8\
-new\
-newkey $key\
-nodes\
-subj "$(Subj $client)"\
-out ${file[client_csr]}\
-keyout ${file[client_key]}\
-addext 'basicConstraints = CA:FALSE'\
-addext 'keyUsage = digitalSignature'\
-addext 'extendedKeyUsage = clientAuth'
$openssl x509\
-req\
-in ${file[client_csr]}\
-CA ${file[ca_cert]}\
-CAkey ${file[ca_key]}\
-out ${file[client_cert]}\
-days $(Days $y)\
-sha512\
-copy_extensions copy
}
function MakeClientConfig
{
[[ -r ${file[config_template]} ]] || {
print "no template file ${file[config_template]}"
return 4
}
local -a secrets
secrets=(
'ca' 'ca_cert'
'cert' 'client_cert'
'key' 'client_key' )
[[ -z $withoutTLSCrypt ]] && secrets+=('tls-crypt' 'tls_crypt')
integer config
exec {config} > ${file[client_config]}
>& $config < ${file[config_template]}
for k f in $secrets; do
>& $config <<< '' <<< "<$k>" < <(grep -v '^#' ${file[$f]}) <<< "</$k>"
done
exec {config} >&-
}
function ShowClientSerial
{
[[ -z $server || -z $client ]] && {
[[ -z $server ]] && print '$server is not specified'
[[ -z $client ]] && print '$client is not specified'
exit 1
}
print "ibase=16; ${$($openssl x509 -serial -noout -in ${file[client_cert]})##*=}" | bc
}
function ShowCertExpiration
{
local cert=$1
strftime -s now %s
strftime -s exp -r '%F %TZ' "${$(openssl x509 -dateopt iso_8601 -enddate -noout -in $cert)##*=}"
(( days = (exp-now)/(24*60*60) )) || true
strftime -s date '%F' $exp
if (( days > 0 )); then
(( days < 180 )) && print -nP "%F{143}"
(( days < 90 )) && print -nP "%F{208}"
(( days < 30 )) && print -nP "%F{203}"
print -n "expires $date (in ${(l:4:)days} days)"
else
print -nP "%F{95}"
print -n "expired $date (${(l:3:)$((-days))} days ago)"
fi
print -nP "%f"
}
function ShowAllCertsExpiration
{
[[ -z $server ]] && print '$server is not specified' && exit 1
local c
for c in $server/*/*-cert.pem; do
ShowCertExpiration $c
print " ${${c#*/}%/*}"
done
}
function AddVPN
{
[[ -z $server ]] && print '$server is not specified' && exit 1
[[ -d ${server} ]] && print 'VPN aleready exists.' && exit 2
mkdir ${server} ${server}/ca ${server}/${server}
[[ $key =~ '^rsa' ]] && MakeDHParams
[[ -z $withoutTLSCrypt ]] && MakeTLSCrypt
MakeCAPassword
MakeCA
chmod a-w ${server}/ca/*pem
MakeServerCertificate
chmod a-w ${server}/${server}/*pem
tree ${server}
print "Put ovpn template into vpn directory!"
}
function AddClient
{
[[ -z $server || -z $client ]] && {
[[ -z $server ]] && print '$server is not specified'
[[ -z $client ]] && print '$client is not specified'
exit 1
}
[[ -d ${server}/${client} ]] && 'Client aleready exists.' && exit 3
mkdir ${server}/${client}
MakeClientCertificate
chmod a-w ${server}/${client}/*pem
MakeClientConfig || true
tree ${server}/${client}
}
case $action in
'' | 'help' | '-h' | '--help')
print "Usage:"
print "s|server=<server> [c|client=<client> type={template_prefix} y=<years>] $0 {AddClient|AddVPN}"
print
print "All functions:"
# print ${(F)${(koi)functions}}
print -l ${(koi)functions}
;;
*)
$action
;;
esac