-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCA.sh
executable file
·359 lines (341 loc) · 8.43 KB
/
CA.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
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
#!/bin/sh
#
# CA - wrapper around ca to make it easier to use ... basically ca requires
# some setup stuff to be done before you can use it and this makes
# things easier between now and when Eric is convinced to fix it :-)
#
# CA -newca ... will setup the right stuff
# CA -newreq ... will generate a certificate request
# CA -sign ... will sign the generated request and output
#
# At the end of that grab newreq.pem and newcert.pem (one has the key
# and the other the certificate) and cat them together and that is what
# you want/need ... I'll make even this a little cleaner later.
#
#
# 12-Jan-96 tjh Added more things ... including CA -signcert which
# converts a certificate to a request and then signs it.
# 10-Jan-96 eay Fixed a few more bugs and added the SSLEAY_CONFIG
# environment variable so this can be driven from
# a script.
# 25-Jul-96 eay Cleaned up filenames some more.
# 11-Jun-96 eay Fixed a few filename missmatches.
# 03-May-96 eay Modified to use 'ssleay cmd' instead of 'cmd'.
# 18-Apr-96 tjh Original hacking
# 05-Nov-11 clf Massive Overhaul
#
# Tim Hudson
#
# default openssl.cnf file has setup as per the following
# demoCA ... where everything is stored
cp_pem() {
infile=$1
outfile=$2
bound=$3
flag=0
exec <$infile;
while read line; do
if [ $flag -eq 1 ]; then
echo $line|grep "^-----END.*$bound" 2>/dev/null 1>/dev/null
if [ $? -eq 0 ] ; then
echo $line >>$outfile
break
else
echo $line >>$outfile
fi
fi
echo $line|grep "^-----BEGIN.*$bound" 2>/dev/null 1>/dev/null
if [ $? -eq 0 ]; then
echo $line >$outfile
flag=1
fi
done
}
is_mode() {
echo $1 >&2
case "$1" in
-newcert|-newreq|-newreq-nodes|-newca|-xsign|-pkcs11|-sign|-signreq|-signCA|-signcert|-verify|-exterminate)
return 0
;;
"") return 0
;;
*) return 1
;;
esac
}
usage() {
echo "usage: $0 -newcert|-newreq|-newreq-nodes|-newca|-sign|-verify" >&2
}
if [ -f ./openssl.cnf ]
then SSLEAY_CONFIG="-config ./openssl.cnf"
export SSLEAY_CONFIG
echo "local openssl.cnf file found"
fi
if [ -z "$OPENSSL" ]; then OPENSSL=openssl; fi
if [ -z "$DAYS" ] ; then DAYS="-days 365" ; fi # 1 year
CADAYS="-days 1095" # 3 years
REQ="$OPENSSL req $SSLEAY_CONFIG"
CA="$OPENSSL ca -verbose $SSLEAY_CONFIG"
VERIFY="$OPENSSL verify"
X509="$OPENSSL x509"
PKCS12="openssl pkcs12"
if [ -z "$CATOP" ] ; then CATOP=./ ; fi
CAKEY=./cakey.pem
CAREQ=./careq.pem
CACERT=./cacert.pem
RET=0
while [ "$1" != "" ] ; do
case $1 in
-\?|-h|-help)
usage
exit 0
;;
-newcert)
until (is_mode $2)
do
shift
case $1 in
-bits=*) bits="-newkey rsa:${1#-*=}"
;;
-days=*) days="-days ${1#-*=}"
;;
-extensions=*) reqext="-reqexts ${1#-*=}"
;;
-name=*) fileprefix="${1#-*=}"
;;
esac
done
# create a certificate
$REQ -new -x509 -keyout ${fileprefix:-new}key.pem -out ${fileprefix:-new}cert.pem $DAYS $days $bits $reqext
RET=$?
echo "Request is in ${fileprefix:-new}cert.pem, private key is in ${fileprefix:-new}key.pem"
unset bits days reqext fileprefix
;;
-newreq|-newreq-nodes)
if [ "$1" = "-newreq-nodes" ]
then
nodesset="-nodes"
fi
until (is_mode $2)
do
shift
case $1 in
-bits=*) bits="-newkey rsa:${1#-*=}"
;;
-days=*) days="-days ${1#-*=}"
;;
-extensions=*) reqext="-reqexts ${1#-*=}"
;;
-name=*) fileprefix="${1#-*=}"
;;
esac
done
# create a certificate request
$REQ -new $nodesset -keyout ${fileprefix:-new}key.pem -out ${fileprefix:-new}req.pem $DAYS $days $bits $reqext
RET=$?
echo "Request is in ${fileprefix:-new}req.pem, private key is in ${fileprefix:-new}key.pem"
unset bits days reqext nodesset fileprefix
;;
-newca)
until (is_mode $2)
do
shift
case $1 in
-bits=*) bits="-newkey rsa:${1#-*=}"
;;
-days=*) days="-days ${1#-*=}"
;;
-extensions=*) exten="${1#-*=}"
;;
esac
done
# if explicitly asked for or it doesn't exist then setup the directory
# structure that Eric likes to manage things
NEW="1"
if [ "$NEW" -o ! -f ${CATOP}/serial ]; then
# create the directory hierarchy
mkdir -p ${CATOP}
mkdir -p ${CATOP}/certs
mkdir -p ${CATOP}/crl
mkdir -p ${CATOP}/newcerts
mkdir -p ${CATOP}/private
touch ${CATOP}/index.txt
fi
if [ ! -f ${CATOP}/private/$CAKEY ]; then
echo "CA certificate filename (or enter to create)"
read FILE
# ask user for existing CA certificate
if [ "$FILE" -a -f "$FILE" -a -r "$FILE" ]; then
cp_pem $FILE ${CATOP}/private/$CAKEY PRIVATE
cp_pem $FILE ${CATOP}/$CACERT CERTIFICATE
RET=$?
if [ ! -f "${CATOP}/serial" ]; then
$X509 -in ${CATOP}/$CACERT -noout -next_serial \
-out ${CATOP}/serial
fi
else
echo "Making CA certificate ..."
$REQ -new -keyout ${CATOP}/private/$CAKEY \
-out ${CATOP}/$CAREQ $bits
RET=$?
if [ -s ${CATOP}/private/${CAKEY} ] ; then
$CA -create_serial -out ${CATOP}/$CACERT $CADAYS -batch \
-keyfile ${CATOP}/private/$CAKEY -selfsign \
-extensions ${exten:-v3_ca} \
-infiles ${CATOP}/$CAREQ
RET=$?
else
echo "CA key generation failed." 1>&2
fi
fi
fi
unset bits days exten
;;
-xsign)
until (is_mode $2)
do
shift
case $1 in
-policy=*) polset="${1#-*=}"
;;
-file=*) reqfiles="$reqfiles ${1#-*=}"
;;
esac
done
$CA -policy ${polset:-policy_anything} -infiles ${reqfiles:-newreq.pem}
RET=$?
unset polset reqfiles
;;
-pkcs12)
infile="newcert.pem"
inkey="newkey.pem"
CNAME="My Certificate"
until (is_mode $2)
do
case $1 in
-name=*) fileprefix="${1#-*=}"
if [ "$infile" != "newcert.pem" ] ; then
infile="${fileprefix}cert.pem"
fi
if [ "$inkey" != "newkey.pem" ] ; then
inkey="${fileprefix}key.pem"
fi
;;
-infile=*) infile="${1#-*=}"
;;
-inkey=*) inkey="${1#-*=}"
;;
*) CNAME="$1"
;;
esac
done
$PKCS12 -in "$infile" -inkey "$inkey" -certfile ${CATOP}/$CACERT \
-out ${fileprefix:-new}cert.p12 -export -name "$CNAME"
RET=$?
unset infile inkey CNAME fileprefix
echo "PKCS#12 certificate written to ${fileprefix:-new}cert.p12"
exit $RET
;;
-sign|-signreq)
until (is_mode $2)
do
shift
case $1 in
-policy=*) polset="${1#-*=}"
;;
-file=*) infile="${1#-*=}"
;;
-name=*) fileprefix="${1#-*=}"
;;
-out=*) outfile="${1#-*=}"
;;
-ext=*) v3ext="-extensions ${1#-*=}"
esac
done
$CA -policy ${polset:-policy_anything} $v3ext -out ${outfile:=${name:-new}cert.pem} -infiles ${infile:-${name:-new}req.pem}
RET=$?
cat $outfile
echo "Signed certificate is in $outfile"
unset polset infile fileprefix outfile ext
;;
-signCA)
until (is_mode $2)
do
shift
case $1 in
-policy=*) polset="${1#-*=}"
;;
-out=*) outfile="${1#-*=}"
;;
-file=*) infile="${1#-*=}"
;;
-name=*) fileprefix="${1#-*=}"
;;
esac
done
$CA -policy ${polset:-policy_anything} -out ${outfile:=${fileprefix:-new}cert.pem} -extensions v3_ca -infiles ${infile:-${fileprefix:-new}req.pem}
RET=$?
echo "Signed CA certificate is in $outfile"
unset polset outfile infile fileprefix
;;
-signcert)
until (is_mode $2)
do
shift
case $1 in
-name=*) fileprefix="${1#-*=}"
;;
-out=*) outfile="${1#-*=}"
;;
-file=*) infile="${1#-*=}"
;;
-key=*) keyfile="${1#-*=}"
;;
-policy=*) polset="${1#-*=}"
;;
-ext=*) extensions="-extensions ${1#-*=}"
;;
esac
done
echo "Cert passphrase will be requested twice - bug?"
$X509 -x509toreq -in ${infile:-${fileprefix:-new}req.pem} -signkey ${keyfile:-${fileprefix:-new}key.pem} -out tmp.pem
$CA -policy ${polset:-policy_anything} $extensions -out ${outfile:=${fileprefix:-new}cert.pem} -infiles tmp.pem
RET=$?
cat $outfile
echo "Signed certificate is in $outfile"
unset extensions fileprefix outfile infile polset keyfile
;;
-verify)
shift
if [ -z "$1" ]; then
$VERIFY -CAfile $CATOP/$CACERT newcert.pem
RET=$?
else
for j
do
$VERIFY -CAfile $CATOP/$CACERT $j
if [ $? != 0 ]; then
RET=$?
fi
done
fi
exit $RET
;;
-exterminate)
currdir=$(pwd)
cd ${CATOP}
ls -A | egrep -v '(CA\.sh|openssl\.cnf|\.git)' | xargs rm -rf
cd $currdir
unset currdir
;;
*)
echo "Command: $0 $*"
echo "Unknown arg $1" >&2
usage
exit 1
;;
esac
shift
done
exit $RET