xref: /freebsd/crypto/openssh/regress/krl.sh (revision ab0b9f6b3073e6c4d1dfbf07444d7db67a189a96)
1#	$OpenBSD: krl.sh,v 1.1 2013/01/18 00:45:29 djm Exp $
2#	Placed in the Public Domain.
3
4tid="key revocation lists"
5
6# If we don't support ecdsa keys then this tell will be much slower.
7ECDSA=ecdsa
8if test "x$TEST_SSH_ECC" != "xyes"; then
9	ECDSA=rsa
10fi
11
12# Do most testing with ssh-keygen; it uses the same verification code as sshd.
13
14# Old keys will interfere with ssh-keygen.
15rm -f $OBJ/revoked-* $OBJ/krl-*
16
17# Generate a CA key
18$SSHKEYGEN -t $ECDSA -f $OBJ/revoked-ca  -C "" -N "" > /dev/null ||
19	fatal "$SSHKEYGEN CA failed"
20
21# A specification that revokes some certificates by serial numbers
22# The serial pattern is chosen to ensure the KRL includes list, range and
23# bitmap sections.
24cat << EOF >> $OBJ/revoked-serials
25serial: 1-4
26serial: 10
27serial: 15
28serial: 30
29serial: 50
30serial: 999
31# The following sum to 500-799
32serial: 500
33serial: 501
34serial: 502
35serial: 503-600
36serial: 700-797
37serial: 798
38serial: 799
39serial: 599-701
40EOF
41
42# A specification that revokes some certificated by key ID.
43touch $OBJ/revoked-keyid
44for n in 1 2 3 4 10 15 30 50 `jot 500 300` 999 1000 1001 1002; do
45	# Fill in by-ID revocation spec.
46	echo "id: revoked $n" >> $OBJ/revoked-keyid
47done
48
49keygen() {
50	N=$1
51	f=$OBJ/revoked-`printf "%04d" $N`
52	# Vary the keytype. We use mostly ECDSA since this is fastest by far.
53	keytype=$ECDSA
54	case $N in
55	2 | 10 | 510 | 1001)	keytype=rsa;;
56	4 | 30 | 520 | 1002)	keytype=dsa;;
57	esac
58	$SSHKEYGEN -t $keytype -f $f -C "" -N "" > /dev/null \
59		|| fatal "$SSHKEYGEN failed"
60	# Sign cert
61	$SSHKEYGEN -s $OBJ/revoked-ca -z $n -I "revoked $N" $f >/dev/null 2>&1 \
62		|| fatal "$SSHKEYGEN sign failed"
63	echo $f
64}
65
66# Generate some keys.
67verbose "$tid: generating test keys"
68REVOKED_SERIALS="1 4 10 50 500 510 520 799 999"
69for n in $REVOKED_SERIALS ; do
70	f=`keygen $n`
71	REVOKED_KEYS="$REVOKED_KEYS ${f}.pub"
72	REVOKED_CERTS="$REVOKED_CERTS ${f}-cert.pub"
73done
74NOTREVOKED_SERIALS="5 9 14 16 29 30 49 51 499 800 1000 1001"
75NOTREVOKED=""
76for n in $NOTREVOKED_SERIALS ; do
77	NOTREVOKED_KEYS="$NOTREVOKED_KEYS ${f}.pub"
78	NOTREVOKED_CERTS="$NOTREVOKED_CERTS ${f}-cert.pub"
79done
80
81genkrls() {
82	OPTS=$1
83$SSHKEYGEN $OPTS -kf $OBJ/krl-empty - </dev/null \
84	>/dev/null || fatal "$SSHKEYGEN KRL failed"
85$SSHKEYGEN $OPTS -kf $OBJ/krl-keys $REVOKED_KEYS \
86	>/dev/null || fatal "$SSHKEYGEN KRL failed"
87$SSHKEYGEN $OPTS -kf $OBJ/krl-cert $REVOKED_CERTS \
88	>/dev/null || fatal "$SSHKEYGEN KRL failed"
89$SSHKEYGEN $OPTS -kf $OBJ/krl-all $REVOKED_KEYS $REVOKED_CERTS \
90	>/dev/null || fatal "$SSHKEYGEN KRL failed"
91$SSHKEYGEN $OPTS -kf $OBJ/krl-ca $OBJ/revoked-ca.pub \
92	>/dev/null || fatal "$SSHKEYGEN KRL failed"
93# KRLs from serial/key-id spec need the CA specified.
94$SSHKEYGEN $OPTS -kf $OBJ/krl-serial $OBJ/revoked-serials \
95	>/dev/null 2>&1 && fatal "$SSHKEYGEN KRL succeeded unexpectedly"
96$SSHKEYGEN $OPTS -kf $OBJ/krl-keyid $OBJ/revoked-keyid \
97	>/dev/null 2>&1 && fatal "$SSHKEYGEN KRL succeeded unexpectedly"
98$SSHKEYGEN $OPTS -kf $OBJ/krl-serial -s $OBJ/revoked-ca $OBJ/revoked-serials \
99	>/dev/null || fatal "$SSHKEYGEN KRL failed"
100$SSHKEYGEN $OPTS -kf $OBJ/krl-keyid -s $OBJ/revoked-ca.pub $OBJ/revoked-keyid \
101	>/dev/null || fatal "$SSHKEYGEN KRL failed"
102}
103
104verbose "$tid: generating KRLs"
105genkrls
106
107check_krl() {
108	KEY=$1
109	KRL=$2
110	EXPECT_REVOKED=$3
111	TAG=$4
112	$SSHKEYGEN -Qf $KRL $KEY >/dev/null
113	result=$?
114	if test "x$EXPECT_REVOKED" = "xyes" -a $result -eq 0 ; then
115		fatal "key $KEY not revoked by KRL $KRL: $TAG"
116	elif test "x$EXPECT_REVOKED" = "xno" -a $result -ne 0 ; then
117		fatal "key $KEY unexpectedly revoked by KRL $KRL: $TAG"
118	fi
119}
120test_all() {
121	FILES=$1
122	TAG=$2
123	KEYS_RESULT=$3
124	ALL_RESULT=$4
125	SERIAL_RESULT=$5
126	KEYID_RESULT=$6
127	CERTS_RESULT=$7
128	CA_RESULT=$8
129	verbose "$tid: checking revocations for $TAG"
130	for f in $FILES ; do
131		check_krl $f $OBJ/krl-empty  no             "$TAG"
132		check_krl $f $OBJ/krl-keys   $KEYS_RESULT   "$TAG"
133		check_krl $f $OBJ/krl-all    $ALL_RESULT    "$TAG"
134		check_krl $f $OBJ/krl-serial $SERIAL_RESULT "$TAG"
135		check_krl $f $OBJ/krl-keyid  $KEYID_RESULT  "$TAG"
136		check_krl $f $OBJ/krl-cert  $CERTS_RESULT   "$TAG"
137		check_krl $f $OBJ/krl-ca     $CA_RESULT     "$TAG"
138	done
139}
140#                                            keys  all serial  keyid  certs   CA
141test_all    "$REVOKED_KEYS"    "revoked keys" yes  yes     no     no     no   no
142test_all  "$UNREVOKED_KEYS"  "unrevoked keys"  no   no     no     no     no   no
143test_all   "$REVOKED_CERTS"   "revoked certs" yes  yes    yes    yes    yes  yes
144test_all "$UNREVOKED_CERTS" "unrevoked certs"  no   no     no     no     no  yes
145
146# Check update. Results should be identical.
147verbose "$tid: testing KRL update"
148for f in $OBJ/krl-keys $OBJ/krl-cert $OBJ/krl-all \
149    $OBJ/krl-ca $OBJ/krl-serial $OBJ/krl-keyid ; do
150	cp -f $OBJ/krl-empty $f
151	genkrls -u
152done
153#                                            keys  all serial  keyid  certs   CA
154test_all    "$REVOKED_KEYS"    "revoked keys" yes  yes     no     no     no   no
155test_all  "$UNREVOKED_KEYS"  "unrevoked keys"  no   no     no     no     no   no
156test_all   "$REVOKED_CERTS"   "revoked certs" yes  yes    yes    yes    yes  yes
157test_all "$UNREVOKED_CERTS" "unrevoked certs"  no   no     no     no     no  yes
158