-
Notifications
You must be signed in to change notification settings - Fork 8
/
pem-foreach
executable file
·50 lines (42 loc) · 1.05 KB
/
pem-foreach
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
#! /bin/sh
# pem-foreach (Bourne shell script) -- run openssl on each cert in a .pem file
OUTDIR=/tmp/pem-$$
set -e
mkdir $OUTDIR
echo $OUTDIR >&2
# open a given file (if supplied) as stdin
if [ $# -gt 0 ] ; then
if [ "x$1" != x- -a "x$1" != x-- ] ; then
# it's a filename
exec < "$1"
fi
shift
if [ "x$1" = x-- ] ; then
# this accounts for the case where a filename or - was present before the --
# (if not, the -- was already removed above)
shift
fi
fi
count=0
started=n
while read line
do
if [ "$line" = "-----BEGIN CERTIFICATE-----" ] ; then
# switch output filenames
count=$(expr $count + 1)
filename=$OUTDIR/$count.crt
exec 5> $filename
started=y
fi
if [ $started = y ] ; then
echo "$line" >&5
fi
# if the cert is finished and any command line arguments were present other
# than a filename, invoke OpenSSL on the cert
if [ $# -gt 0 -a "$line" = "-----END CERTIFICATE-----" ] ; then
# close the file
exec 5> /dev/null
started=n
openssl x509 "$@" -noout -in $filename
fi
done