forked from jlehen/minipki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.sh
119 lines (93 loc) · 1.59 KB
/
lib.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
# $Id: lib.sh,v 1.7 2009/09/23 21:23:57 jlh Exp $
error() {
echo "$@" >&2
exit 1
}
# Result in $PW global variable.
readpw() {
local verify pw1 pw2
if [ "x$1" = "x-v" ]; then
verify=1
shift
fi
while : ; do
echo "Warning: Double every backslash."
echo -n "$1? "
stty -echo
read pw1
stty echo
echo
if [ -z "$pw1" ]; then
echo "ERROR: Empty $1." >&2
continue
fi
[ -z "$verify" ] && break
echo -n "$1 again? "
stty -echo
read pw2
stty echo
echo
[ "x$pw1" = "x$pw2" ] && break
echo "ERROR: Passwords mismatch." >&2
done
PW="$pw1"
}
mkCAhierarchy() {
local d="$1"
mkdir -p "$d"
touch "$d/key.pem"
chmod 600 "$d/key.pem"
touch "$d/database.txt"
echo 01 > "$d/serial.txt"
}
dumpcert() {
local c="$1"
openssl x509 -in "$c" -noout -text
}
writeconf() {
case "$RDNSEP" in
,|/) : ;;
*) echo "ASSERTION FAILED: Invalid RDNSEP"; exit 127 ;;
esac
echo RDNSEP=\"$RDNSEP\"
echo
for var in "$@"; do
eval echo $var=\\\"\""\$$var"\"\\\"
done
}
furtherdir() {
local d i
case "$RDNSEP" in
,|/) : ;;
*) echo "ASSERTION FAILED: Invalid RDNSEP"; exit 127 ;;
esac
d="$1"
shift
for i in "$@"; do
d="${d}${RDNSEP}${i}"
done
echo "$d"
}
builddir() {
local d
d=$(furtherdir "" "$@")
echo "${d#$RDNSEP}"
}
deepOrFlat() {
case "$1" in
-f) RDNSEP=',' ;;
-d) RDNSEP='/' ;;
*) return 1 ;;
esac
return 0
}
genRandomSerial() {
local d s
local N=4
d="$1"
while : ; do
s=$(openssl rand $N | od -t x$N | awk 'NR == 1 {print $2}' | tr 'a-z' 'A-Z')
cat "$d/database.txt" | awk "\$3 ~ /^$s$/ { exit 1 }" && break
done
echo "$s" > "$d/serial.txt"
}