blob: 8fd62c7730d97d43be926c02d250d39df7864755 [file] [log] [blame]
djm@openbsd.orgdd369322017-04-30 23:34:55 +00001# $OpenBSD: cert-file.sh,v 1.6 2017/04/30 23:34:55 djm Exp $
djm@openbsd.orge14ac432015-09-24 06:16:53 +00002# Placed in the Public Domain.
3
4tid="ssh with certificates"
5
6rm -f $OBJ/user_ca_key* $OBJ/user_key*
7rm -f $OBJ/cert_user_key*
8
9# Create a CA key
10${SSHKEYGEN} -q -N '' -t ed25519 -f $OBJ/user_ca_key1 ||\
11 fatal "ssh-keygen failed"
12${SSHKEYGEN} -q -N '' -t ed25519 -f $OBJ/user_ca_key2 ||\
13 fatal "ssh-keygen failed"
14
15# Make some keys and certificates.
16${SSHKEYGEN} -q -N '' -t ed25519 -f $OBJ/user_key1 || \
17 fatal "ssh-keygen failed"
18${SSHKEYGEN} -q -N '' -t ed25519 -f $OBJ/user_key2 || \
19 fatal "ssh-keygen failed"
djm@openbsd.org8ff3fc32017-03-11 23:44:16 +000020${SSHKEYGEN} -q -N '' -t ed25519 -f $OBJ/user_key3 || \
21 fatal "ssh-keygen failed"
22${SSHKEYGEN} -q -N '' -t ed25519 -f $OBJ/user_key4 || \
23 fatal "ssh-keygen failed"
24${SSHKEYGEN} -q -N '' -t ed25519 -f $OBJ/user_key5 || \
25 fatal "ssh-keygen failed"
26
djm@openbsd.orge14ac432015-09-24 06:16:53 +000027# Move the certificate to a different address to better control
28# when it is offered.
29${SSHKEYGEN} -q -s $OBJ/user_ca_key1 -I "regress user key for $USER" \
30 -z $$ -n ${USER} $OBJ/user_key1 ||
djm@openbsd.org8ff3fc32017-03-11 23:44:16 +000031 fatal "couldn't sign user_key1 with user_ca_key1"
djm@openbsd.orge14ac432015-09-24 06:16:53 +000032mv $OBJ/user_key1-cert.pub $OBJ/cert_user_key1_1.pub
33${SSHKEYGEN} -q -s $OBJ/user_ca_key2 -I "regress user key for $USER" \
34 -z $$ -n ${USER} $OBJ/user_key1 ||
djm@openbsd.org8ff3fc32017-03-11 23:44:16 +000035 fatal "couldn't sign user_key1 with user_ca_key2"
djm@openbsd.orge14ac432015-09-24 06:16:53 +000036mv $OBJ/user_key1-cert.pub $OBJ/cert_user_key1_2.pub
djm@openbsd.org8ff3fc32017-03-11 23:44:16 +000037${SSHKEYGEN} -q -s $OBJ/user_ca_key1 -I "regress user key for $USER" \
38 -z $$ -n ${USER} $OBJ/user_key3 ||
39 fatal "couldn't sign user_key3 with user_ca_key1"
40rm $OBJ/user_key3.pub # to test use of private key w/o public half.
41${SSHKEYGEN} -q -s $OBJ/user_ca_key1 -I "regress user key for $USER" \
42 -z $$ -n ${USER} $OBJ/user_key4 ||
43 fatal "couldn't sign user_key4 with user_ca_key1"
44rm $OBJ/user_key4 $OBJ/user_key4.pub # to test no matching pub/private key case.
djm@openbsd.orge14ac432015-09-24 06:16:53 +000045
46trace 'try with identity files'
47opts="-F $OBJ/ssh_proxy -oIdentitiesOnly=yes"
48opts2="$opts -i $OBJ/user_key1 -i $OBJ/user_key2"
49echo "cert-authority $(cat $OBJ/user_ca_key1.pub)" > $OBJ/authorized_keys_$USER
50
djm@openbsd.org8ff3fc32017-03-11 23:44:16 +000051# Make a clean config that doesn't have any pre-added identities.
52cat $OBJ/ssh_proxy | grep -v IdentityFile > $OBJ/no_identity_config
53
54# XXX: verify that certificate used was what we expect. Needs exposure of
55# keys via enviornment variable or similar.
56
djm@openbsd.org8ff3fc32017-03-11 23:44:16 +000057 # Key with no .pub should work - finding the equivalent *-cert.pub.
djm@openbsd.orgdd369322017-04-30 23:34:55 +000058verbose "identity cert with no plain public file"
59${SSH} -F $OBJ/no_identity_config -oIdentitiesOnly=yes \
60 -i $OBJ/user_key3 somehost exit 52
61[ $? -ne 52 ] && fail "ssh failed"
djm@openbsd.org8ff3fc32017-03-11 23:44:16 +000062
djm@openbsd.orgdd369322017-04-30 23:34:55 +000063# CertificateFile matching private key with no .pub file should work.
64verbose "CertificateFile with no plain public file"
65${SSH} -F $OBJ/no_identity_config -oIdentitiesOnly=yes \
66 -oCertificateFile=$OBJ/user_key3-cert.pub \
67 -i $OBJ/user_key3 somehost exit 52
68[ $? -ne 52 ] && fail "ssh failed"
djm@openbsd.org8ff3fc32017-03-11 23:44:16 +000069
djm@openbsd.orgdd369322017-04-30 23:34:55 +000070# Just keys should fail
71verbose "plain keys"
72${SSH} $opts2 somehost exit 52
73r=$?
74if [ $r -eq 52 ]; then
75 fail "ssh succeeded with no certs"
76fi
djm@openbsd.orge14ac432015-09-24 06:16:53 +000077
djm@openbsd.orgdd369322017-04-30 23:34:55 +000078# Keys with untrusted cert should fail.
79verbose "untrusted cert"
80opts3="$opts2 -oCertificateFile=$OBJ/cert_user_key1_2.pub"
81${SSH} $opts3 somehost exit 52
82r=$?
83if [ $r -eq 52 ]; then
84 fail "ssh succeeded with bad cert"
85fi
djm@openbsd.orge14ac432015-09-24 06:16:53 +000086
djm@openbsd.orgdd369322017-04-30 23:34:55 +000087# Good cert with bad key should fail.
88verbose "good cert, bad key"
89opts3="$opts -i $OBJ/user_key2"
90opts3="$opts3 -oCertificateFile=$OBJ/cert_user_key1_1.pub"
91${SSH} $opts3 somehost exit 52
92r=$?
93if [ $r -eq 52 ]; then
94 fail "ssh succeeded with no matching key"
95fi
djm@openbsd.orge14ac432015-09-24 06:16:53 +000096
djm@openbsd.orgdd369322017-04-30 23:34:55 +000097# Keys with one trusted cert, should succeed.
98verbose "single trusted"
99opts3="$opts2 -oCertificateFile=$OBJ/cert_user_key1_1.pub"
100${SSH} $opts3 somehost exit 52
101r=$?
102if [ $r -ne 52 ]; then
103 fail "ssh failed with trusted cert and key"
104fi
djm@openbsd.orge14ac432015-09-24 06:16:53 +0000105
djm@openbsd.orgdd369322017-04-30 23:34:55 +0000106# Multiple certs and keys, with one trusted cert, should succeed.
107verbose "multiple trusted"
108opts3="$opts2 -oCertificateFile=$OBJ/cert_user_key1_2.pub"
109opts3="$opts3 -oCertificateFile=$OBJ/cert_user_key1_1.pub"
110${SSH} $opts3 somehost exit 52
111r=$?
112if [ $r -ne 52 ]; then
113 fail "ssh failed with multiple certs"
114fi
djm@openbsd.orge14ac432015-09-24 06:16:53 +0000115
116#next, using an agent in combination with the keys
117SSH_AUTH_SOCK=/nonexistent ${SSHADD} -l > /dev/null 2>&1
118if [ $? -ne 2 ]; then
119 fatal "ssh-add -l did not fail with exit code 2"
120fi
121
122trace "start agent"
123eval `${SSHAGENT} -s` > /dev/null
124r=$?
125if [ $r -ne 0 ]; then
126 fatal "could not start ssh-agent: exit code $r"
127fi
128
129# add private keys to agent
130${SSHADD} -k $OBJ/user_key2 > /dev/null 2>&1
131if [ $? -ne 0 ]; then
132 fatal "ssh-add did not succeed with exit code 0"
133fi
134${SSHADD} -k $OBJ/user_key1 > /dev/null 2>&1
135if [ $? -ne 0 ]; then
136 fatal "ssh-add did not succeed with exit code 0"
137fi
138
139# try ssh with the agent and certificates
djm@openbsd.orge14ac432015-09-24 06:16:53 +0000140opts="-F $OBJ/ssh_proxy"
141# with no certificates, shoud fail
djm@openbsd.orgdd369322017-04-30 23:34:55 +0000142${SSH} $opts somehost exit 52
djm@openbsd.orge14ac432015-09-24 06:16:53 +0000143if [ $? -eq 52 ]; then
djm@openbsd.orgdd369322017-04-30 23:34:55 +0000144 fail "ssh connect with agent in succeeded with no cert"
djm@openbsd.orge14ac432015-09-24 06:16:53 +0000145fi
146
147#with an untrusted certificate, should fail
djm@openbsd.org21ae8ee2015-09-24 07:15:39 +0000148opts="$opts -oCertificateFile=$OBJ/cert_user_key1_2.pub"
djm@openbsd.orgdd369322017-04-30 23:34:55 +0000149${SSH} $opts somehost exit 52
djm@openbsd.orge14ac432015-09-24 06:16:53 +0000150if [ $? -eq 52 ]; then
djm@openbsd.orgdd369322017-04-30 23:34:55 +0000151 fail "ssh connect with agent in succeeded with bad cert"
djm@openbsd.orge14ac432015-09-24 06:16:53 +0000152fi
153
154#with an additional trusted certificate, should succeed
djm@openbsd.org21ae8ee2015-09-24 07:15:39 +0000155opts="$opts -oCertificateFile=$OBJ/cert_user_key1_1.pub"
djm@openbsd.orgdd369322017-04-30 23:34:55 +0000156${SSH} $opts somehost exit 52
djm@openbsd.orge14ac432015-09-24 06:16:53 +0000157if [ $? -ne 52 ]; then
djm@openbsd.orgdd369322017-04-30 23:34:55 +0000158 fail "ssh connect with agent in failed with good cert"
djm@openbsd.orge14ac432015-09-24 06:16:53 +0000159fi
160
161trace "kill agent"
162${SSHAGENT} -k > /dev/null
163
164#cleanup
165rm -f $OBJ/user_ca_key* $OBJ/user_key*
166rm -f $OBJ/cert_user_key*