xref: /freebsd/crypto/openssh/regress/sshsig.sh (revision 069ac18495ad8fde2748bc94b0f80a50250bb01d)
1*069ac184SEd Maste#	$OpenBSD: sshsig.sh,v 1.15 2023/10/12 03:51:08 djm Exp $
219261079SEd Maste#	Placed in the Public Domain.
319261079SEd Maste
419261079SEd Mastetid="sshsig"
519261079SEd Maste
619261079SEd MasteDATA2=$OBJ/${DATANAME}.2
719261079SEd Mastecat ${DATA} ${DATA} > ${DATA2}
819261079SEd Maste
919261079SEd Masterm -f $OBJ/sshsig-*.sig $OBJ/wrong-key* $OBJ/sigca-key*
1019261079SEd Maste
1119261079SEd Mastesig_namespace="test-$$"
1219261079SEd Mastesig_principal="user-$$@example.com"
1319261079SEd Maste
1419261079SEd Maste# Make a "wrong key"
1519261079SEd Maste${SSHKEYGEN} -q -t ed25519 -f $OBJ/wrong-key \
1619261079SEd Maste	-C "wrong trousers, Grommit" -N '' \
1719261079SEd Maste	|| fatal "couldn't generate key"
1819261079SEd MasteWRONG=$OBJ/wrong-key.pub
1919261079SEd Maste
2019261079SEd Maste# Make a CA key.
2119261079SEd Maste${SSHKEYGEN} -q -t ed25519 -f $OBJ/sigca-key -C "CA" -N '' \
2219261079SEd Maste	|| fatal "couldn't generate key"
2319261079SEd MasteCA_PRIV=$OBJ/sigca-key
2419261079SEd MasteCA_PUB=$OBJ/sigca-key.pub
2519261079SEd Maste
2619261079SEd Mastetrace "start agent"
2719261079SEd Masteeval `${SSHAGENT} ${EXTRA_AGENT_ARGS} -s` > /dev/null
2819261079SEd Master=$?
2919261079SEd Masteif [ $r -ne 0 ]; then
3019261079SEd Maste	fatal "could not start ssh-agent: exit code $r"
3119261079SEd Mastefi
3219261079SEd Maste
3319261079SEd MasteSIGNKEYS="$SSH_KEYTYPES"
3419261079SEd Masteverbose "$tid: make certificates"
3519261079SEd Mastefor t in $SSH_KEYTYPES ; do
3619261079SEd Maste	${SSHKEYGEN} -q -s $CA_PRIV -z $$ \
3719261079SEd Maste	    -I "regress signature key for $USER" \
381323ec57SEd Maste		-V "19840101:19860101" \
3919261079SEd Maste	    -n $sig_principal $OBJ/${t} || \
4019261079SEd Maste		fatal "couldn't sign ${t}"
4119261079SEd Maste	SIGNKEYS="$SIGNKEYS ${t}-cert.pub"
4219261079SEd Mastedone
4319261079SEd Maste
4419261079SEd Mastefor t in $SIGNKEYS; do
4519261079SEd Maste	verbose "$tid: check signature for $t"
4619261079SEd Maste	keybase=`basename $t .pub`
4719261079SEd Maste	privkey=${OBJ}/`basename $t -cert.pub`
4819261079SEd Maste	sigfile=${OBJ}/sshsig-${keybase}.sig
4919261079SEd Maste	sigfile_agent=${OBJ}/sshsig-agent-${keybase}.sig
5019261079SEd Maste	pubkey=${OBJ}/${keybase}.pub
511323ec57SEd Maste	cert=${OBJ}/${keybase}-cert.pub
521323ec57SEd Maste	sigfile_cert=${OBJ}/sshsig-${keybase}-cert.sig
5319261079SEd Maste
54*069ac184SEd Maste	trace "$tid: key type $t check bad hashlg"
5519261079SEd Maste	${SSHKEYGEN} -vvv -Y sign -f ${OBJ}/$t -n $sig_namespace \
561323ec57SEd Maste	    -Ohashalg=sha1 < $DATA > $sigfile 2>/dev/null && \
571323ec57SEd Maste		fail "sign using $t with bad hash algorithm succeeded"
5819261079SEd Maste
591323ec57SEd Maste	for h in default sha256 sha512 ; do
601323ec57SEd Maste		case "$h" in
611323ec57SEd Maste		default) hashalg_arg="" ;;
621323ec57SEd Maste		*) hashalg_arg="-Ohashalg=$h" ;;
631323ec57SEd Maste		esac
64*069ac184SEd Maste		trace "$tid: key type $t sign with hash $h"
651323ec57SEd Maste		${SSHKEYGEN} -vvv -Y sign -f ${OBJ}/$t -n $sig_namespace \
661323ec57SEd Maste		    $hashalg_arg < $DATA > $sigfile 2>/dev/null || \
671323ec57SEd Maste			fail "sign using $t / $h failed"
6819261079SEd Maste		(printf "$sig_principal " ; cat $pubkey) > $OBJ/allowed_signers
69*069ac184SEd Maste		trace "$tid: key type $t verify with hash $h"
7019261079SEd Maste		${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
7119261079SEd Maste		    -I $sig_principal -f $OBJ/allowed_signers \
7219261079SEd Maste		    < $DATA >/dev/null 2>&1 || \
731323ec57SEd Maste			fail "failed signature for $t / $h key"
741323ec57SEd Maste	done
7519261079SEd Maste
76*069ac184SEd Maste	trace "$tid: key type $t verify with limited namespace"
7719261079SEd Maste	(printf "$sig_principal namespaces=\"$sig_namespace,whatever\" ";
7819261079SEd Maste	 cat $pubkey) > $OBJ/allowed_signers
7919261079SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
8019261079SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
8119261079SEd Maste		< $DATA >/dev/null 2>&1 || \
8219261079SEd Maste		fail "failed signature for $t key w/ limited namespace"
8319261079SEd Maste
84*069ac184SEd Maste	trace "$tid: key type $t print-pubkey"
8519261079SEd Maste	(printf "$sig_principal namespaces=\"$sig_namespace,whatever\" ";
8619261079SEd Maste	 cat $pubkey) > $OBJ/allowed_signers
8719261079SEd Maste	${SSHKEYGEN} -q -Y verify -s $sigfile -n $sig_namespace \
8819261079SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
8919261079SEd Maste		-O print-pubkey \
9019261079SEd Maste		< $DATA | cut -d' ' -f1-2 > ${OBJ}/${keybase}-fromsig.pub || \
9119261079SEd Maste		fail "failed signature for $t key w/ print-pubkey"
9219261079SEd Maste	cut -d' ' -f1-2 ${OBJ}/${keybase}.pub > ${OBJ}/${keybase}-strip.pub
9319261079SEd Maste	diff -r ${OBJ}/${keybase}-strip.pub ${OBJ}/${keybase}-fromsig.pub || \
9419261079SEd Maste		fail "print-pubkey differs from signature key"
9519261079SEd Maste
9619261079SEd Maste	# Invalid option
97*069ac184SEd Maste	trace "$tid: key type $t verify with bad signers"
9819261079SEd Maste	(printf "$sig_principal octopus " ; cat $pubkey) > $OBJ/allowed_signers
9919261079SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
10019261079SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
10119261079SEd Maste		< $DATA >/dev/null 2>&1 && \
10219261079SEd Maste		fail "accepted signature for $t key with bad signers option"
10319261079SEd Maste
10419261079SEd Maste	# Wrong key trusted.
105*069ac184SEd Maste	trace "$tid: key type $t verify with wrong key"
10619261079SEd Maste	(printf "$sig_principal " ; cat $WRONG) > $OBJ/allowed_signers
10719261079SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
10819261079SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
10919261079SEd Maste		< $DATA >/dev/null 2>&1 && \
11019261079SEd Maste		fail "accepted signature for $t key with wrong key trusted"
11119261079SEd Maste
11219261079SEd Maste	# incorrect data
113*069ac184SEd Maste	trace "$tid: key type $t verify with wrong data"
11419261079SEd Maste	(printf "$sig_principal " ; cat $pubkey) > $OBJ/allowed_signers
11519261079SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
11619261079SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
11719261079SEd Maste		< $DATA2 >/dev/null 2>&1 && \
11819261079SEd Maste		fail "passed signature for wrong data with $t key"
11919261079SEd Maste
12019261079SEd Maste	# wrong principal in signers
121*069ac184SEd Maste	trace "$tid: key type $t verify with wrong principal"
12219261079SEd Maste	(printf "josef.k@example.com " ; cat $pubkey) > $OBJ/allowed_signers
12319261079SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
12419261079SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
12519261079SEd Maste		< $DATA >/dev/null 2>&1 && \
12619261079SEd Maste		fail "accepted signature for $t key with wrong principal"
12719261079SEd Maste
12819261079SEd Maste	# wrong namespace
129*069ac184SEd Maste	trace "$tid: key type $t verify with wrong namespace"
13019261079SEd Maste	(printf "$sig_principal " ; cat $pubkey) > $OBJ/allowed_signers
13119261079SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n COWS_COWS_COWS \
13219261079SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
13319261079SEd Maste		< $DATA >/dev/null 2>&1 && \
13419261079SEd Maste		fail "accepted signature for $t key with wrong namespace"
13519261079SEd Maste
13619261079SEd Maste	# namespace excluded by option
137*069ac184SEd Maste	trace "$tid: key type $t verify with excluded namespace"
13819261079SEd Maste	(printf "$sig_principal namespaces=\"whatever\" " ;
13919261079SEd Maste	 cat $pubkey) > $OBJ/allowed_signers
14019261079SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
14119261079SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
14219261079SEd Maste		< $DATA >/dev/null 2>&1 && \
14319261079SEd Maste		fail "accepted signature for $t key with excluded namespace"
14419261079SEd Maste
14519261079SEd Maste	( printf "$sig_principal " ;
14619261079SEd Maste	  printf "valid-after=\"19800101\",valid-before=\"19900101\" " ;
14719261079SEd Maste	  cat $pubkey) > $OBJ/allowed_signers
14819261079SEd Maste
14919261079SEd Maste	# key lifespan valid
150*069ac184SEd Maste	trace "$tid: key type $t verify with valid lifespan"
15119261079SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
15219261079SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
15319261079SEd Maste		-Overify-time=19850101 \
15419261079SEd Maste		< $DATA >/dev/null 2>&1 || \
15519261079SEd Maste		fail "failed signature for $t key with valid expiry interval"
15619261079SEd Maste	# key not yet valid
157*069ac184SEd Maste	trace "$tid: key type $t verify with not-yet-valid lifespan"
15819261079SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
15919261079SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
16019261079SEd Maste		-Overify-time=19790101 \
16119261079SEd Maste		< $DATA >/dev/null 2>&1 && \
16219261079SEd Maste		fail "failed signature for $t not-yet-valid key"
16319261079SEd Maste	# key expired
164*069ac184SEd Maste	trace "$tid: key type $t verify with expired lifespan"
16519261079SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
16619261079SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
16719261079SEd Maste		-Overify-time=19910101 \
16819261079SEd Maste		< $DATA >/dev/null 2>&1 && \
16919261079SEd Maste		fail "failed signature for $t with expired key"
17019261079SEd Maste	# NB. assumes we're not running this test in the 1980s
171*069ac184SEd Maste	trace "$tid: key type $t verify with expired lifespan (now)"
17219261079SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
17319261079SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
17419261079SEd Maste		< $DATA >/dev/null 2>&1 && \
17519261079SEd Maste		fail "failed signature for $t with expired key"
17619261079SEd Maste
1771323ec57SEd Maste	# key lifespan valid
178*069ac184SEd Maste	trace "$tid: key type $t find-principals with valid lifespan"
1791323ec57SEd Maste	${SSHKEYGEN} -vvv -Y find-principals -s $sigfile \
1801323ec57SEd Maste		-Overify-time="19850101" \
1811323ec57SEd Maste		-f $OBJ/allowed_signers >/dev/null 2>&1 || \
1821323ec57SEd Maste		fail "failed find-principals for $t key with valid expiry interval"
1831323ec57SEd Maste	# key not yet valid
184*069ac184SEd Maste	trace "$tid: key type $t find principals with not-yet-valid lifespan"
1851323ec57SEd Maste	${SSHKEYGEN} -vvv -Y find-principals -s $sigfile \
1861323ec57SEd Maste		-Overify-time="19790101" \
1871323ec57SEd Maste		-f $OBJ/allowed_signers >/dev/null 2>&1 && \
1881323ec57SEd Maste		fail "failed find-principals for $t not-yet-valid key"
1891323ec57SEd Maste	# key expired
190*069ac184SEd Maste	trace "$tid: key type $t find-principals with expired lifespan"
1911323ec57SEd Maste	${SSHKEYGEN} -vvv -Y find-principals -s $sigfile \
1921323ec57SEd Maste		-Overify-time="19990101" \
1931323ec57SEd Maste		-f $OBJ/allowed_signers >/dev/null 2>&1 && \
1941323ec57SEd Maste		fail "failed find-principals for $t with expired key"
1951323ec57SEd Maste	# NB. assumes we're not running this test in the 1980s
196*069ac184SEd Maste	trace "$tid: key type $t find-principals with expired lifespan (now)"
1971323ec57SEd Maste	${SSHKEYGEN} -vvv -Y find-principals -s $sigfile \
1981323ec57SEd Maste		-f $OBJ/allowed_signers >/dev/null 2>&1 && \
1991323ec57SEd Maste		fail "failed find-principals for $t with expired key"
2001323ec57SEd Maste
20119261079SEd Maste	# public key in revoked keys file
202*069ac184SEd Maste	trace "$tid: key type $t verify with revoked key"
20319261079SEd Maste	cat $pubkey > $OBJ/revoked_keys
20419261079SEd Maste	(printf "$sig_principal namespaces=\"whatever\" " ;
20519261079SEd Maste	 cat $pubkey) > $OBJ/allowed_signers
20619261079SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
20719261079SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
20819261079SEd Maste		-r $OBJ/revoked_keys \
20919261079SEd Maste		< $DATA >/dev/null 2>&1 && \
21019261079SEd Maste		fail "accepted signature for $t key, but key is in revoked_keys"
21119261079SEd Maste
21219261079SEd Maste	# public key not revoked, but others are present in revoked_keysfile
213*069ac184SEd Maste	trace "$tid: key type $t verify with unrevoked key"
21419261079SEd Maste	cat $WRONG > $OBJ/revoked_keys
21519261079SEd Maste	(printf "$sig_principal " ; cat $pubkey) > $OBJ/allowed_signers
21619261079SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
21719261079SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
21819261079SEd Maste		-r $OBJ/revoked_keys \
21919261079SEd Maste		< $DATA >/dev/null 2>&1 || \
22019261079SEd Maste		fail "couldn't verify signature for $t key, but key not in revoked_keys"
22119261079SEd Maste
22219261079SEd Maste	# check-novalidate with valid data
223*069ac184SEd Maste	trace "$tid: key type $t check-novalidate with valid data"
22419261079SEd Maste	${SSHKEYGEN} -vvv -Y check-novalidate -s $sigfile -n $sig_namespace \
22519261079SEd Maste		< $DATA >/dev/null 2>&1 || \
22619261079SEd Maste		fail "failed to check valid signature for $t key"
22719261079SEd Maste
22819261079SEd Maste	# check-novalidate with invalid data
229*069ac184SEd Maste	trace "$tid: key type $t check-novalidate with invalid data"
23019261079SEd Maste	${SSHKEYGEN} -vvv -Y check-novalidate -s $sigfile -n $sig_namespace \
23119261079SEd Maste		< $DATA2 >/dev/null 2>&1 && \
23219261079SEd Maste		fail "succeeded checking signature for $t key with invalid data"
23319261079SEd Maste
2341323ec57SEd Maste	# find-principals with valid public key
235*069ac184SEd Maste	trace "$tid: key type $t find-principals with valid key"
2361323ec57SEd Maste	(printf "$sig_principal " ; cat $pubkey) > $OBJ/allowed_signers
2371323ec57SEd Maste	${SSHKEYGEN} -vvv -Y find-principals -s $sigfile -f $OBJ/allowed_signers >/dev/null 2>&1 || \
2381323ec57SEd Maste		fail "failed to find valid principals in allowed_signers"
2391323ec57SEd Maste
2401323ec57SEd Maste	# find-principals with wrong key not in allowed_signers
241*069ac184SEd Maste	trace "$tid: key type $t find-principals with wrong key"
2421323ec57SEd Maste	(printf "$sig_principal " ; cat $WRONG) > $OBJ/allowed_signers
2431323ec57SEd Maste	${SSHKEYGEN} -vvv -Y find-principals -s $sigfile -f $OBJ/allowed_signers >/dev/null 2>&1 && \
2441323ec57SEd Maste		fail "succeeded finding principal with invalid signers file"
2451323ec57SEd Maste
2461323ec57SEd Maste	# find-principals with a configured namespace but none on command-line
247*069ac184SEd Maste	trace "$tid: key type $t find-principals with missing namespace"
2481323ec57SEd Maste	(printf "$sig_principal " ;
2491323ec57SEd Maste	 printf "namespaces=\"test1,test2\" ";
2501323ec57SEd Maste	 cat $pubkey) > $OBJ/allowed_signers
2511323ec57SEd Maste	${SSHKEYGEN} -vvv -Y find-principals -s $sigfile \
2521323ec57SEd Maste	    -f $OBJ/allowed_signers >/dev/null 2>&1 || \
2531323ec57SEd Maste		fail "failed finding principal when namespaces are configured"
2541323ec57SEd Maste
25519261079SEd Maste	# Check signing keys using ssh-agent.
256*069ac184SEd Maste	trace "$tid: key type $t prepare agent"
25719261079SEd Maste	${SSHADD} -D >/dev/null 2>&1 # Remove all previously-loaded keys.
25819261079SEd Maste	${SSHADD} ${privkey} > /dev/null 2>&1 || fail "ssh-add failed"
25919261079SEd Maste
26019261079SEd Maste	# Move private key to ensure agent key is used
26119261079SEd Maste	mv ${privkey} ${privkey}.tmp
26219261079SEd Maste
263*069ac184SEd Maste	trace "$tid: key type $t sign with agent"
26419261079SEd Maste	${SSHKEYGEN} -vvv -Y sign -f $pubkey -n $sig_namespace \
26519261079SEd Maste		< $DATA > $sigfile_agent 2>/dev/null || \
26619261079SEd Maste		fail "ssh-agent based sign using $pubkey failed"
267*069ac184SEd Maste	trace "$tid: key type $t check signature w/ agent"
26819261079SEd Maste	${SSHKEYGEN} -vvv -Y check-novalidate -s $sigfile_agent \
26919261079SEd Maste		-n $sig_namespace < $DATA >/dev/null 2>&1 || \
27019261079SEd Maste		fail "failed to check valid signature for $t key"
271*069ac184SEd Maste	(printf "$sig_principal namespaces=\"$sig_namespace,whatever\" ";
272*069ac184SEd Maste	 cat $pubkey) > $OBJ/allowed_signers
273*069ac184SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile_agent -n $sig_namespace \
274*069ac184SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
275*069ac184SEd Maste		< $DATA >/dev/null 2>&1 || \
276*069ac184SEd Maste		fail "failed signature for $t key w/ limited namespace"
27719261079SEd Maste
27819261079SEd Maste	# Move private key back
27919261079SEd Maste	mv ${privkey}.tmp ${privkey}
28019261079SEd Maste
2811323ec57SEd Maste	# Duplicate principals & keys in allowed_signers but with different validities
2821323ec57SEd Maste	( printf "$sig_principal " ;
2831323ec57SEd Maste	  printf "valid-after=\"19800101\",valid-before=\"19900101\" " ;
2841323ec57SEd Maste	  cat $pubkey;
2851323ec57SEd Maste	  printf "${sig_principal} " ;
2861323ec57SEd Maste	  printf "valid-after=\"19850101\",valid-before=\"20000101\" " ;
2871323ec57SEd Maste	  cat $pubkey) > $OBJ/allowed_signers
2881323ec57SEd Maste
2891323ec57SEd Maste	# find-principals outside of any validity lifespan
290*069ac184SEd Maste	trace "$tid: key type $t find principals outside multiple validities"
2911323ec57SEd Maste	${SSHKEYGEN} -vvv -Y find-principals -s $sigfile \
2921323ec57SEd Maste		-Overify-time="20100101" \
2931323ec57SEd Maste		-f $OBJ/allowed_signers >/dev/null 2>&1 && \
2941323ec57SEd Maste		fail "succeeded find-principals for $t verify-time outside of validity"
2951323ec57SEd Maste	# find-principals matching only the first lifespan
296*069ac184SEd Maste	trace "$tid: key type $t find principals matching one validity (1st)"
2971323ec57SEd Maste	${SSHKEYGEN} -vvv -Y find-principals -s $sigfile \
2981323ec57SEd Maste		-Overify-time="19830101" \
2991323ec57SEd Maste		-f $OBJ/allowed_signers >/dev/null 2>&1 || \
3001323ec57SEd Maste		fail "failed find-principals for $t verify-time within first span"
3011323ec57SEd Maste	# find-principals matching both lifespans
302*069ac184SEd Maste	trace "$tid: key type $t find principals matching two validities"
3031323ec57SEd Maste	${SSHKEYGEN} -vvv -Y find-principals -s $sigfile \
3041323ec57SEd Maste		-Overify-time="19880101" \
3051323ec57SEd Maste		-f $OBJ/allowed_signers >/dev/null 2>&1 || \
3061323ec57SEd Maste		fail "failed find-principals for $t verify-time within both spans"
3071323ec57SEd Maste	# find-principals matching only the second lifespan
308*069ac184SEd Maste	trace "$tid: key type $t find principals matching one validity (2nd)"
3091323ec57SEd Maste	${SSHKEYGEN} -vvv -Y find-principals -s $sigfile \
3101323ec57SEd Maste		-Overify-time="19950101" \
3111323ec57SEd Maste		-f $OBJ/allowed_signers >/dev/null 2>&1 || \
3121323ec57SEd Maste		fail "failed find-principals for $t verify-time within second span"
3131323ec57SEd Maste
3141323ec57SEd Maste	# verify outside of any validity lifespan
315*069ac184SEd Maste	trace "$tid: key type $t verify outside multiple validities"
3161323ec57SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
3171323ec57SEd Maste		-Overify-time="20100101" -I $sig_principal \
3181323ec57SEd Maste		-r $OBJ/revoked_keys -f $OBJ/allowed_signers \
3191323ec57SEd Maste		< $DATA >/dev/null 2>&1 && \
3201323ec57SEd Maste		fail "succeeded verify for $t verify-time outside of validity"
3211323ec57SEd Maste	# verify matching only the first lifespan
322*069ac184SEd Maste	trace "$tid: key type $t verify matching one validity (1st)"
3231323ec57SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
3241323ec57SEd Maste		-Overify-time="19830101" -I $sig_principal \
3251323ec57SEd Maste		-r $OBJ/revoked_keys -f $OBJ/allowed_signers \
3261323ec57SEd Maste		< $DATA >/dev/null 2>&1 || \
3271323ec57SEd Maste		fail "failed verify for $t verify-time within first span"
3281323ec57SEd Maste	# verify matching both lifespans
329*069ac184SEd Maste	trace "$tid: key type $t verify matching two validities"
3301323ec57SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
3311323ec57SEd Maste		-Overify-time="19880101" -I $sig_principal \
3321323ec57SEd Maste		-r $OBJ/revoked_keys -f $OBJ/allowed_signers \
3331323ec57SEd Maste		< $DATA >/dev/null 2>&1 || \
3341323ec57SEd Maste		fail "failed verify for $t verify-time within both spans"
3351323ec57SEd Maste	# verify matching only the second lifespan
336*069ac184SEd Maste	trace "$tid: key type $t verify matching one validity (2nd)"
3371323ec57SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
3381323ec57SEd Maste		-Overify-time="19950101" -I $sig_principal \
3391323ec57SEd Maste		-r $OBJ/revoked_keys -f $OBJ/allowed_signers \
3401323ec57SEd Maste		< $DATA >/dev/null 2>&1 || \
3411323ec57SEd Maste		fail "failed verify for $t verify-time within second span"
3421323ec57SEd Maste
34319261079SEd Maste	# Remaining tests are for certificates only.
34419261079SEd Maste	case "$keybase" in
34519261079SEd Maste		*-cert) ;;
34619261079SEd Maste		*) continue ;;
34719261079SEd Maste	esac
34819261079SEd Maste
3491323ec57SEd Maste	# Check key lifespan on find-principals when using the CA
3501323ec57SEd Maste	( printf "$sig_principal " ;
3511323ec57SEd Maste	  printf "cert-authority,valid-after=\"19800101\",valid-before=\"19900101\" ";
3521323ec57SEd Maste	  cat $CA_PUB) > $OBJ/allowed_signers
3531323ec57SEd Maste	# key lifespan valid
354*069ac184SEd Maste	trace "$tid: key type $t find-principals cert lifetime valid"
3551323ec57SEd Maste	${SSHKEYGEN} -vvv -Y find-principals -s $sigfile \
3561323ec57SEd Maste		-Overify-time="19850101" \
3571323ec57SEd Maste		-f $OBJ/allowed_signers >/dev/null 2>&1 || \
3581323ec57SEd Maste		fail "failed find-principals for $t key with valid expiry interval"
3591323ec57SEd Maste	# key not yet valid
360*069ac184SEd Maste	trace "$tid: key type $t find-principals cert lifetime not-yet-valid"
3611323ec57SEd Maste	${SSHKEYGEN} -vvv -Y find-principals -s $sigfile \
3621323ec57SEd Maste		-Overify-time="19790101" \
3631323ec57SEd Maste		-f $OBJ/allowed_signers >/dev/null 2>&1 && \
3641323ec57SEd Maste		fail "failed find-principals for $t not-yet-valid key"
3651323ec57SEd Maste	# key expired
366*069ac184SEd Maste	trace "$tid: key type $t find-principals cert lifetime expired"
3671323ec57SEd Maste	${SSHKEYGEN} -vvv -Y find-principals -s $sigfile \
3681323ec57SEd Maste		-Overify-time="19990101" \
3691323ec57SEd Maste		-f $OBJ/allowed_signers >/dev/null 2>&1 && \
3701323ec57SEd Maste		fail "failed find-principals for $t with expired key"
3711323ec57SEd Maste	# NB. assumes we're not running this test in the 1980s
372*069ac184SEd Maste	trace "$tid: key type $t find-principals cert lifetime expired (now)"
3731323ec57SEd Maste	${SSHKEYGEN} -vvv -Y find-principals -s $sigfile \
3741323ec57SEd Maste		-f $OBJ/allowed_signers >/dev/null 2>&1 && \
3751323ec57SEd Maste		fail "failed find-principals for $t with expired key"
37619261079SEd Maste
37719261079SEd Maste	# correct CA key
378*069ac184SEd Maste	trace "$tid: key type $t verify cert good CA"
37919261079SEd Maste	(printf "$sig_principal cert-authority " ;
38019261079SEd Maste	 cat $CA_PUB) > $OBJ/allowed_signers
38119261079SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
38219261079SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
3831323ec57SEd Maste		-Overify-time=19850101 \
38419261079SEd Maste		< $DATA >/dev/null 2>&1 || \
38519261079SEd Maste		fail "failed signature for $t cert"
38619261079SEd Maste
3871323ec57SEd Maste	# find-principals
388*069ac184SEd Maste	trace "$tid: key type $t find-principals cert good CA"
3891323ec57SEd Maste	${SSHKEYGEN} -vvv -Y find-principals -s $sigfile \
3901323ec57SEd Maste		-Overify-time=19850101 \
3911323ec57SEd Maste		-f $OBJ/allowed_signers >/dev/null 2>&1 || \
3921323ec57SEd Maste		fail "failed find-principals for $t with ca key"
3931323ec57SEd Maste
3941323ec57SEd Maste	# CA with wildcard principal
395*069ac184SEd Maste	trace "$tid: key type $t find-principals cert good wildcard CA"
3961323ec57SEd Maste	(printf "*@example.com cert-authority " ;
3971323ec57SEd Maste	 cat $CA_PUB) > $OBJ/allowed_signers
3981323ec57SEd Maste	# find-principals CA with wildcard principal
3991323ec57SEd Maste	${SSHKEYGEN} -vvv -Y find-principals -s $sigfile \
4001323ec57SEd Maste		-Overify-time=19850101 \
4011323ec57SEd Maste		-f $OBJ/allowed_signers 2>/dev/null | \
4021323ec57SEd Maste		fgrep "$sig_principal" >/dev/null || \
4031323ec57SEd Maste		fail "failed find-principals for $t with ca key using wildcard principal"
4041323ec57SEd Maste
4051323ec57SEd Maste	# verify CA with wildcard principal
406*069ac184SEd Maste	trace "$tid: key type $t verify cert good wildcard CA"
4071323ec57SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
4081323ec57SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
4091323ec57SEd Maste		-Overify-time=19850101 \
4101323ec57SEd Maste		< $DATA >/dev/null 2>&1 || \
4111323ec57SEd Maste		fail "failed signature for $t cert using wildcard principal"
4121323ec57SEd Maste
41319261079SEd Maste	# signing key listed as cert-authority
414*069ac184SEd Maste	trace "$tid: key type $t verify signing key listed as CA"
41519261079SEd Maste	(printf "$sig_principal cert-authority " ;
41619261079SEd Maste	 cat $pubkey) > $OBJ/allowed_signers
41719261079SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
41819261079SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
41919261079SEd Maste		< $DATA >/dev/null 2>&1 && \
42019261079SEd Maste		fail "accepted signature with $t key listed as CA"
42119261079SEd Maste
42219261079SEd Maste	# CA key not flagged cert-authority
423*069ac184SEd Maste	trace "$tid: key type $t verify key not marked as CA"
42419261079SEd Maste	(printf "$sig_principal " ; cat $CA_PUB) > $OBJ/allowed_signers
42519261079SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
42619261079SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
42719261079SEd Maste		< $DATA >/dev/null 2>&1 && \
42819261079SEd Maste		fail "accepted signature for $t cert with CA not marked"
42919261079SEd Maste
43019261079SEd Maste	# mismatch between cert principal and file
431*069ac184SEd Maste	trace "$tid: key type $t verify cert with wrong principal"
43219261079SEd Maste	(printf "josef.k@example.com cert-authority " ;
43319261079SEd Maste	 cat $CA_PUB) > $OBJ/allowed_signers
43419261079SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
43519261079SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
43619261079SEd Maste		< $DATA >/dev/null 2>&1 && \
43719261079SEd Maste		fail "accepted signature for $t cert with wrong principal"
4381323ec57SEd Maste
4391323ec57SEd Maste	# Cert valid but CA revoked
440*069ac184SEd Maste	trace "$tid: key type $t verify cert with revoked CA"
4411323ec57SEd Maste	cat $CA_PUB > $OBJ/revoked_keys
4421323ec57SEd Maste	(printf "$sig_principal " ; cat $pubkey) > $OBJ/allowed_signers
4431323ec57SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
4441323ec57SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
4451323ec57SEd Maste		-r $OBJ/revoked_keys \
4461323ec57SEd Maste		< $DATA >/dev/null 2>&1 && \
4471323ec57SEd Maste		fail "accepted signature for $t key, but CA key in revoked_keys"
4481323ec57SEd Maste
4491323ec57SEd Maste	# Set lifespan of CA key and verify signed user certs behave accordingly
4501323ec57SEd Maste	( printf "$sig_principal " ;
4511323ec57SEd Maste	  printf "cert-authority,valid-after=\"19800101\",valid-before=\"19900101\" " ;
4521323ec57SEd Maste	  cat $CA_PUB) > $OBJ/allowed_signers
4531323ec57SEd Maste
4541323ec57SEd Maste	# CA key lifespan valid
455*069ac184SEd Maste	trace "$tid: key type $t verify cert valid CA lifespan"
4561323ec57SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
4571323ec57SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
4581323ec57SEd Maste		-Overify-time=19850101 \
4591323ec57SEd Maste		< $DATA >/dev/null 2>&1 >/dev/null 2>&1 || \
4601323ec57SEd Maste		fail "failed signature for $t key with valid CA expiry interval"
4611323ec57SEd Maste	# CA lifespan is valid but user key not yet valid
462*069ac184SEd Maste	trace "$tid: key type $t verify cert valid CA lifespan, not-yet-valid cert"
4631323ec57SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
4641323ec57SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
4651323ec57SEd Maste		-Overify-time=19810101 \
4661323ec57SEd Maste		< $DATA >/dev/null 2>&1 && \
4671323ec57SEd Maste		fail "accepted signature for $t key with valid CA expiry interval but not yet valid cert"
4681323ec57SEd Maste	# CA lifespan is valid but user key expired
469*069ac184SEd Maste	trace "$tid: key type $t verify cert valid CA lifespan, expired cert"
4701323ec57SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
4711323ec57SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
4721323ec57SEd Maste		-Overify-time=19890101 \
4731323ec57SEd Maste		< $DATA >/dev/null 2>&1 && \
4741323ec57SEd Maste		fail "accepted signature for $t key with valid CA expiry interval but expired cert"
4751323ec57SEd Maste	# CA key not yet valid
476*069ac184SEd Maste	trace "$tid: key type $t verify cert CA not-yet-valid"
4771323ec57SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
4781323ec57SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
4791323ec57SEd Maste		-Overify-time=19790101 \
4801323ec57SEd Maste		< $DATA >/dev/null 2>&1 && \
4811323ec57SEd Maste		fail "accepted signature for $t not-yet-valid CA key"
4821323ec57SEd Maste	# CA key expired
483*069ac184SEd Maste	trace "$tid: key type $t verify cert CA expired"
4841323ec57SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
4851323ec57SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
4861323ec57SEd Maste		-Overify-time=19910101 \
4871323ec57SEd Maste		< $DATA >/dev/null 2>&1 && \
4881323ec57SEd Maste		fail "accepted signature for $t with expired CA key"
4891323ec57SEd Maste	# NB. assumes we're not running this test in the 1980s
490*069ac184SEd Maste	trace "$tid: key type $t verify cert CA expired (now)"
4911323ec57SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
4921323ec57SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
4931323ec57SEd Maste		< $DATA >/dev/null 2>&1 && \
4941323ec57SEd Maste		fail "accepted signature for $t with expired CA key"
4951323ec57SEd Maste
4961323ec57SEd Maste	# Set lifespan of CA outside of the cert validity
497*069ac184SEd Maste	trace "$tid: key type $t verify CA/cert lifespan mismatch"
4981323ec57SEd Maste	( printf "$sig_principal " ;
4991323ec57SEd Maste	  printf "cert-authority,valid-after=\"19800101\",valid-before=\"19820101\" " ;
5001323ec57SEd Maste	  cat $CA_PUB) > $OBJ/allowed_signers
5011323ec57SEd Maste	# valid cert validity but expired CA
5021323ec57SEd Maste	${SSHKEYGEN} -vvv -Y verify -s $sigfile -n $sig_namespace \
5031323ec57SEd Maste		-I $sig_principal -f $OBJ/allowed_signers \
5041323ec57SEd Maste		-Overify-time=19840101 \
5051323ec57SEd Maste		< $DATA >/dev/null 2>&1 && \
5061323ec57SEd Maste		fail "accepted signature for $t key with expired CA but valid cert"
5071323ec57SEd Maste
5081323ec57SEd Mastedone
5091323ec57SEd Maste
5101323ec57SEd Maste# Test key independant match-principals
5111323ec57SEd Maste(
5121323ec57SEd Maste	printf "principal1 " ; cat $pubkey;
5131323ec57SEd Maste	printf "princi* " ; cat $pubkey;
5141323ec57SEd Maste	printf "unique " ; cat $pubkey;
5151323ec57SEd Maste) > $OBJ/allowed_signers
5161323ec57SEd Maste
5171323ec57SEd Masteverbose "$tid: match principals"
5181323ec57SEd Maste${SSHKEYGEN} -Y match-principals -f $OBJ/allowed_signers -I "unique" | \
5191323ec57SEd Maste    fgrep "unique" >/dev/null || \
520*069ac184SEd Maste	fail "failed to match static principal"
5211323ec57SEd Maste
522*069ac184SEd Mastetrace "$tid: match principals wildcard"
5231323ec57SEd Maste${SSHKEYGEN} -Y match-principals -f $OBJ/allowed_signers -I "princip" | \
5241323ec57SEd Maste    fgrep "princi*" >/dev/null || \
525*069ac184SEd Maste	fail "failed to match wildcard principal"
5261323ec57SEd Maste
527*069ac184SEd Mastetrace "$tid: match principals static/wildcard"
5281323ec57SEd Maste${SSHKEYGEN} -Y match-principals -f $OBJ/allowed_signers -I "principal1" | \
5291323ec57SEd Maste    fgrep -e "principal1" -e "princi*" >/dev/null || \
530*069ac184SEd Maste	fail "failed to match static and wildcard principal"
5311323ec57SEd Masteverbose "$tid: nomatch principals"
5321323ec57SEd Mastefor x in princ prince unknown ; do
5331323ec57SEd Maste	${SSHKEYGEN} -Y match-principals -f $OBJ/allowed_signers \
5341323ec57SEd Maste	    -I $x >/dev/null 2>&1 && \
5351323ec57SEd Maste		fail "succeeded to match unknown principal \"$x\""
53619261079SEd Mastedone
53719261079SEd Maste
53819261079SEd Mastetrace "kill agent"
53919261079SEd Maste${SSHAGENT} -k > /dev/null
54019261079SEd Maste
541