-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcert-manager
411 lines (378 loc) · 11.2 KB
/
cert-manager
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/env bash
typeset -i DAYS=398
typeset -i NDAYS=0
printf -v hr '%.s─' {1..60}
bo=$(tput bold)
rs=$(tput sgr0)
h6=$(tput setaf 6)
hr="$h6$hr$rs"
bold() {
echo -n $bo
echo "$@"
echo -n $rs
}
highlight() {
tput setaf $1
shift
echo "$@"
echo -n $rs
}
function savefile() {
[ -f "$1" ] && {
cp "$1" "${1}.$(date +%Y%m%d.%H%M)"
}
}
function die {
>&2 highlight 1 "$@"
exit 1
}
makeconf() {
[ -f "$CSRCONF" ] && return
cat > "$CSRCONF" << EOD
[ req ]
default_bits = 4096
default_keyfile = privkey.pem
distinguished_name = req_distinguished_name
prompt = yes
[ req_distinguished_name ]
countryName = Country Name
countryName_default = US
countryName_min = 2
countryName_max = 2
stateOrProvinceName = State or Province
stateOrProvinceName_default = FL
stateOrProvinceName_min = 2
stateOrProvinceName_max = 12
localityName = Locality Name
localityName_default = Locality
localityName_max = 32
organizationalUnitName = Organizational Unit Name
organizationalUnitName_default = Org Name
organizationalUnitName_max = 32
commonName = Common Name (server, Your name)
commonName_max = 64
emailAddress = Email Address
emailAddress_default = [email protected]
emailAddress_max = 32
EOD
}
makeext() {
cat > "$CAEXT" << EOD
#[certificate-name] <- this identifies the name of the crt you are creating
#the remaining lines are valid ssl cert extensions. Most common:
#subjectAltName=IP:192.168.1.1,DNS:hostname.example.com,DNS:hostname
EOD
highlight 3 "Created sample extensions database $CAEXT."
}
create_ca() {
typeset -l ans
highlight 3 "If you encrypt your CA Key you must provide a password and that password"
highlight 3 "will be required for every use of the CA for signing requests"
echo ""
read -p "${bo}Encrypt CA Key?${rs} " ans
if [ "${ans[0]}" = "y" ]; then
openssl genrsa -aes256 -out "$CAKEY" 4096
else
openssl genrsa -out "$CAKEY" 4096
fi
makeconf
openssl req -x509 -new -nodes -key "$CAKEY" -sha256 -days 1826 -config "$CSRCONF" -out "$CACRT"
makeext
settings=$(openssl x509 -in "$CACRT" -text -noout -certopt no_pubkey,no_sigdump|grep "Issuer:")
settings=${settings#*Issuer: }
set -- $(echo $settings|tr -d ',')
while [ -n "$1" ]; do
case "$1" in
C) sed -ie "s/countryName_default.*/countryName_default = $3/" "$CSRCONF"
shift 2
;;
L) sed -ie "s/localityName_default.*/localityName_default = $3/" "$CSRCONF"
shift 2
;;
ST) sed -ie "s/stateOrProvinceName_default.*/stateOrProvinceName_default = $3/" "$CSRCONF"
shift 2
;;
CN) sed -ie "s/commonName_default.*/commonName_default = $3/" "$CSRCONF"
shift 2
;;
OU) sed -ie "s/organizationalUnitName_default.*/organizationalUnitName_default = $3/" "$CSRCONF"
shift 2
;;
emailAddress)
sed -ie "s/emailAddress_default.*/emailAddress_default = $3/" "$CSRCONF"
shift 2
;;
esac
shift
done
}
set_ca() {
echo "$hr"
highlight 3 "Existing prefixes:"
find ${CADIR} -name '*.ext' -printf '%f\n' | sed -e 's/\.ext//'
echo "$hr"
typeset -l cap="$1"
if [ -z "$cap" ]; then
def_pre=${CAPREFIX:-$(whoami)}
read -p "${bo}What CA name do you want to use? ($def_pre)${rs} " cap
cap="${cap:-$def_pre}"
fi
CACRT="${CADIR}/${cap}-ca.crt"
CAKEY="${CADIR}/${cap}-ca.key"
CAEXT="${CADIR}/${cap}.ext"
CERTS="${TOPDIR}/certs-${cap}"
CSRCONF="${CADIR}/${cap}-csr.conf"
CAPREFIX="$cap"
[ ! -f "$CAKEY" ] && create_ca
[ ! -f "$CACRT" ] && create_ca
[ ! -f "$CAEXT" ] && create_ca
[ ! -f "$CSRCONF" ] && create_ca
[ ! -d "$CERTS" ] && mkdir -p "$CERTS"
cd "$CERTS"
echo "$CAPREFIX" > ${TOPDIR}/.certmgr
}
load_config() {
SAVES="${TOPDIR}/backup"
CADIR="${TOPDIR}/ca"
if [ -r "${TOPDIR}/.certmgr" ]; then
CAPREFIX="$(<${TOPDIR}/.certmgr)"
set_ca "$CAPREFIX"
else
printf "Certificate Directory (%s) not initialized\n" "$TOPDIR"
typeset -l ans
read -p "${bo}Initialize?${rs} " ans
[ "${ans[0]}" != "y" ] && {
echo -e "\n\n$(bold Options:)\n"
echo " Specify -d option to point to existing Certificate Directory"
echo " Change directory to the directory you want to initialize"
echo "Then rerun this tool"
echo ""
die "Directory not initialized"
}
mkdir -p ${SAVES}
mkdir -p ${CADIR}
set_ca
fi
[ -d "$CADIR" ] || die "Error: CA Directory Not found!"
[ -f "$CACRT" ] || die "Error: CA Certificate Not Found!"
[ -f "$CAKEY" ] || die "Error: CA Private Key Not Found!"
}
function signCSR {
printf '\n%s\n' $hr
bold "Enter CSR file name:"
read -ep "$PWD/" FN
FN=${FN%.}
FN="${FN%.csr}.csr"
if [ ! -r "$FN" ] ;then
>&2 highlight 1 "Error: CSR file not found or not readable"
return
fi
FNO="${FN%.csr}.crt"
FNE="${FN%.csr}"
FNE="${FNE##*/}"
echo "$hr"
highlight 3 "Processing CSR $FN, output is $FNO"
echo "$hr"
savefile "$FNO"
BN=${FN%.*}
if grep -q "^\[$BN\]" $CAEXT; then
openssl x509 -req -in "$FN" -CA "$CACRT" -CAkey "$CAKEY" \
-CAcreateserial -extfile "$CAEXT" -extensions "$FNE" -out "$FNO" -days $DAYS || \
die "Error signing certificate"
else
highlight 3 "Warning: entry for $BN not found in extensions database!"
openssl x509 -req -in "$FN" -CA "$CACRT" -CAkey "$CAKEY" \
-CAcreateserial -extfile "$CAEXT" -out "$FNO" -days $DAYS || \
die "Error signing certificate"
fi
}
showCert() {
printf '\n%s\n' $hr
highlight 3 "Enter Cert/Csr Filename:"
read -ep "$PWD/" FN
[ "$FN" == "ca" ] && FN="$CACRT"
FN=${FN%.}
[ ! -r "$FN" ] && FN="${FN}.crt"
[ ! -r "$FN" ] && FN="${FN#.crt}.csr"
[ ! -r "$FN" ] && FN="${FN#.crt}.pem"
if [ ! -r "$FN" ]; then
>&2 highlight 1 "Error: could not find matching cert/csr"
return
fi
echo "$hr"
case ${FN##*.} in
csr)
openssl req -in "$FN" -text -noout -reqopt no_pubkey,no_sigdump
;;
crt|pem)
openssl x509 -in "$FN" -text -noout -certopt no_pubkey,no_sigdump
;;
esac
}
genCSR() {
printf '\n%s\n' $hr
highlight 3 "Enter CSR Filename:"
read -ep "$PWD/" FN
FN=${FN%.}
FN="${FN%.csr}.csr"
FCSR="${FN%.csr}.csr"
FKEY="${FN%.csr}.key"
echo "$hr"
echo "Using $FCSR and $FKEY"
echo "$hr"
touch "$FCSR" || {
>&2 highglight 1 "Error: file could not be opened for writing"
return
}
openssl req -newkey rsa -keyout "$FKEY" -out "$FCSR" -nodes -config "$CSRCONF"
}
makeBundle() {
printf '\n%s\n' $hr
highlight 3 "Enter Certificate Filename:"
read -ep "$PWD/" FN
FN=${FN%.}
FCRT="${FN%.crt}.crt"
FKEY="${FN%.crt}.key"
[ -r "$FCRT" ] || {
>&2 highlight 1 "Error: cert not found or not readable: $FCRT"
return
}
[ -r "$FKEY" ] || {
>&2 highlight 1 "Error: private key not found or not readable: $FKEY"
return
}
FB="${FN%.*}.pem"
typeset -l ans
highlight 3 "Including the key can be unwise, especially if the key has not encrypted!"
read -p "${bo}Do you want to include the key?${rs} "
echo $hr
highlight 3 "Writing $FB"
if [ "${ans[0]}" = "y" ]; then
cat "$FCRT" "$CACRT" > "$FB"
else
cat "$FKEY" "$FCRT" "$CACRT" > "$FB"
fi
}
makeBackup() {
OF="${SAVES}/${CAPREFIX}-$(date +%Y%m%d-%H%M%S).tgz"
( cd $TOPDIR
if tar czf "$OF" ca/${CAPREFIX}* certs-${CAPREFIX}; then
echo $hr
highlight 3 "Successful backup of $CAPREFIX made to $OF"
else
echo $hr
highlight 1 "Backup of $CAPREFIX failed!"
fi
)
}
setDays() {
highlight 3 "Max days for most browsers now is 398 (just over 1Y)"
highlight 3 "Certificate tools allow up to 3650 (10 years)"
echo ""
read -p "${bo}Enter Number of Days:${rs} " NDAYS
[ $NDAYS -gt 3650 ] && NDAYS=3650
[ $NDAYS -gt 0 ] && {
DAYS=$NDAYS
return
}
highlight 1 "invalid number of days $NDAYS, using $DAYS"
}
regenCerts() {
printf '\n%s\n' $hr
read -p "${bo}Enter filter prefix or <enter> for all:${rs} " PF
if [ -r "$PF" ] ; then
FLIST=("$PF")
else
FLIST=$(find . -name "$PF*.csr" -printf '%f ' )
fi
echo $hr
highlight 3 "List of certs to regenerate:"
for FN in $FLIST;do
printf "...%s\n" "$FN"
done
typeset -l ANS
echo $hr
read -n1 -p "Is this correct (y/N)? " ANS
[ "$ANS" != "y" ] && return
for FN in $FLIST;do
highlight 4 $FN
FN="${FN%.csr}.csr"
if [ ! -r "$FN" ] ;then
>&2 highlight 1 "Error: CSR file not found or not readable: $FN"
continue
fi
FNO="${FN%.csr}.crt"
FNE="${FN%.csr}"
FNE="${FNE##*/}"
echo $hr
highlight 3 "Processing CSR $FN, output is $FNO"
savefile "$FNO"
openssl x509 -req -in "$FN" -CA "$CACRT" -CAkey "$CAKEY" -CAcreateserial -extfile "$CAEXT" -extensions "$FNE" -out "$FNO" -days $DAYS
done
}
function showMenu {
echo "$hr"
highlight 4 "Root Dir: $TOPDIR"
highlight 4 "Prefix: $CAPREFIX"
echo "$hr"
highlight 3 "Functions"
echo "$hr"
echo "b - make a backup of this cert tree"
echo "c - generate a certificate request (CSR)"
echo "e - edit the extensions database"
echo "m - make a PEM bundle (?key, crt, CA)"
echo "p - change CA prefix"
echo "r - regenerate certificates"
echo "s - sign a certificate request"
echo "t - change certificate expiration time ($DAYS days)"
echo "v - view a certificate or request"
echo "$hr"
echo "q - quit"
echo "$hr"
}
function menu {
showMenu
typeset -l mchoice=""
read -p "${bo}Choice?${rs} " mchoice
case "$mchoice" in
b) makeBackup ;;
m) makeBundle ;;
c) genCSR ;;
e) vi "$CAEXT" ;;
p) set_ca ;;
r) regenCerts ;;
s) signCSR ;;
v) showCert ;;
q) mexit="y" ;;
t) setDays ;;
*) showMenu ;;
esac
}
while [ -n "$1" ]; do
case "$1" in
-h) echo "Options:"
echo " -m <days> max cert days, default is 398"
echo " -d <dir> top level directory to store certs"
exit
;;
-m) NDAYS=$2
[ $NDAYS -gt 0 ] && DAYS=$NDAYS
[ $NDAYS -gt 3650 ] && DAYS=3650
shift
;;
-d) TOPDIR="$(readlink -f $2)"
shift
;;
*)
highlight 1 "Unknown argument $1"
exit
;;
esac
shift
done
TOPDIR=${TOPDIR:-$PWD}
load_config
typeset -l mexit="n"
while [ "$mexit" != "y" ]; do
menu
done