blob: da2cec87dabf49621cc232c63c7b4b0c769ce6bc [file] [log] [blame]
Damien Millerf54542a2013-12-07 16:32:44 +11001# $OpenBSD: cert-hostkey.sh,v 1.8 2013/12/06 13:52:46 markus Exp $
Damien Miller58ac6de2010-02-27 07:57:12 +11002# Placed in the Public Domain.
3
4tid="certified host keys"
5
6rm -f $OBJ/known_hosts-cert $OBJ/host_ca_key* $OBJ/cert_host_key*
7cp $OBJ/sshd_proxy $OBJ/sshd_proxy_bak
8
9HOSTS='localhost-with-alias,127.0.0.1,::1'
10
11# Create a CA key and add it to known hosts
12${SSHKEYGEN} -q -N '' -t rsa -f $OBJ/host_ca_key ||\
13 fail "ssh-keygen of host_ca_key failed"
14(
Darren Tucker56347ef2013-05-17 13:28:36 +100015 printf '@cert-authority '
16 printf "$HOSTS "
Damien Miller58ac6de2010-02-27 07:57:12 +110017 cat $OBJ/host_ca_key.pub
18) > $OBJ/known_hosts-cert
19
Damien Millerf54542a2013-12-07 16:32:44 +110020PLAIN_TYPES=`$SSH -Q key-plain | sed 's/^ssh-dss/ssh-dsa/g;s/^ssh-//'`
21
22type_has_legacy() {
23 case $1 in
24 ed25519*|ecdsa*) return 1 ;;
25 esac
26 return 0
27}
28
Damien Miller58ac6de2010-02-27 07:57:12 +110029# Generate and sign host keys
Damien Millerf54542a2013-12-07 16:32:44 +110030for ktype in $PLAIN_TYPES ; do
Damien Miller58ac6de2010-02-27 07:57:12 +110031 verbose "$tid: sign host ${ktype} cert"
32 # Generate and sign a host key
33 ${SSHKEYGEN} -q -N '' -t ${ktype} \
34 -f $OBJ/cert_host_key_${ktype} || \
35 fail "ssh-keygen of cert_host_key_${ktype} failed"
36 ${SSHKEYGEN} -h -q -s $OBJ/host_ca_key \
37 -I "regress host key for $USER" \
38 -n $HOSTS $OBJ/cert_host_key_${ktype} ||
39 fail "couldn't sign cert_host_key_${ktype}"
Damien Millerf54542a2013-12-07 16:32:44 +110040 type_has_legacy $ktype || continue
Damien Miller53f4bb62010-04-18 08:15:14 +100041 cp $OBJ/cert_host_key_${ktype} $OBJ/cert_host_key_${ktype}_v00
42 cp $OBJ/cert_host_key_${ktype}.pub $OBJ/cert_host_key_${ktype}_v00.pub
Damien Millerf54542a2013-12-07 16:32:44 +110043 verbose "$tid: sign host ${ktype}_v00 cert"
Damien Miller53f4bb62010-04-18 08:15:14 +100044 ${SSHKEYGEN} -t v00 -h -q -s $OBJ/host_ca_key \
45 -I "regress host key for $USER" \
46 -n $HOSTS $OBJ/cert_host_key_${ktype}_v00 ||
47 fail "couldn't sign cert_host_key_${ktype}_v00"
Damien Miller58ac6de2010-02-27 07:57:12 +110048done
49
50# Basic connect tests
51for privsep in yes no ; do
Damien Millerf54542a2013-12-07 16:32:44 +110052 for ktype in $PLAIN_TYPES rsa_v00 dsa_v00; do
Damien Miller58ac6de2010-02-27 07:57:12 +110053 verbose "$tid: host ${ktype} cert connect privsep $privsep"
54 (
55 cat $OBJ/sshd_proxy_bak
56 echo HostKey $OBJ/cert_host_key_${ktype}
57 echo HostCertificate $OBJ/cert_host_key_${ktype}-cert.pub
58 echo UsePrivilegeSeparation $privsep
59 ) > $OBJ/sshd_proxy
60
61 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
62 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
63 -F $OBJ/ssh_proxy somehost true
64 if [ $? -ne 0 ]; then
65 fail "ssh cert connect failed"
66 fi
67 done
68done
69
Damien Miller700dcfa2010-03-04 21:58:01 +110070# Revoked certificates with key present
71(
Darren Tucker56347ef2013-05-17 13:28:36 +100072 printf '@cert-authority '
73 printf "$HOSTS "
Damien Miller700dcfa2010-03-04 21:58:01 +110074 cat $OBJ/host_ca_key.pub
Darren Tucker56347ef2013-05-17 13:28:36 +100075 printf '@revoked '
76 printf "* "
Damien Miller700dcfa2010-03-04 21:58:01 +110077 cat $OBJ/cert_host_key_rsa.pub
Tim Ricec5c346b2011-01-13 22:36:14 -080078 if test "x$TEST_SSH_ECC" = "xyes"; then
Damien Millerf54542a2013-12-07 16:32:44 +110079 cat $OBJ/cert_host_key_ecdsa-sha2-nistp256.pub
Darren Tucker56347ef2013-05-17 13:28:36 +100080 printf '@revoked '
81 printf "* "
Damien Millerf54542a2013-12-07 16:32:44 +110082 cat $OBJ/cert_host_key_ecdsa-sha2-nistp384.pub
83 printf '@revoked '
84 printf "* "
85 cat $OBJ/cert_host_key_ecdsa-sha2-nistp521.pub
86 printf '@revoked '
87 printf "* "
88 cat $OBJ/cert_host_key_ed25519.pub
Tim Ricec5c346b2011-01-13 22:36:14 -080089 fi
Darren Tucker56347ef2013-05-17 13:28:36 +100090 printf '@revoked '
91 printf "* "
Damien Miller700dcfa2010-03-04 21:58:01 +110092 cat $OBJ/cert_host_key_dsa.pub
Darren Tucker56347ef2013-05-17 13:28:36 +100093 printf '@revoked '
94 printf "* "
Damien Miller53f4bb62010-04-18 08:15:14 +100095 cat $OBJ/cert_host_key_rsa_v00.pub
Darren Tucker56347ef2013-05-17 13:28:36 +100096 printf '@revoked '
97 printf "* "
Damien Miller53f4bb62010-04-18 08:15:14 +100098 cat $OBJ/cert_host_key_dsa_v00.pub
Damien Miller700dcfa2010-03-04 21:58:01 +110099) > $OBJ/known_hosts-cert
100for privsep in yes no ; do
Damien Millerf54542a2013-12-07 16:32:44 +1100101 for ktype in $PLAIN_TYPES rsa_v00 dsa_v00; do
Damien Miller700dcfa2010-03-04 21:58:01 +1100102 verbose "$tid: host ${ktype} revoked cert privsep $privsep"
103 (
104 cat $OBJ/sshd_proxy_bak
105 echo HostKey $OBJ/cert_host_key_${ktype}
106 echo HostCertificate $OBJ/cert_host_key_${ktype}-cert.pub
107 echo UsePrivilegeSeparation $privsep
108 ) > $OBJ/sshd_proxy
109
110 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
111 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
112 -F $OBJ/ssh_proxy somehost true >/dev/null 2>&1
113 if [ $? -eq 0 ]; then
114 fail "ssh cert connect succeeded unexpectedly"
115 fi
116 done
117done
118
119# Revoked CA
120(
Darren Tucker56347ef2013-05-17 13:28:36 +1000121 printf '@cert-authority '
122 printf "$HOSTS "
Damien Miller700dcfa2010-03-04 21:58:01 +1100123 cat $OBJ/host_ca_key.pub
Darren Tucker56347ef2013-05-17 13:28:36 +1000124 printf '@revoked '
125 printf "* "
Damien Miller700dcfa2010-03-04 21:58:01 +1100126 cat $OBJ/host_ca_key.pub
127) > $OBJ/known_hosts-cert
Damien Millerf54542a2013-12-07 16:32:44 +1100128for ktype in $PLAIN_TYPES rsa_v00 dsa_v00 ; do
Damien Miller700dcfa2010-03-04 21:58:01 +1100129 verbose "$tid: host ${ktype} revoked cert"
130 (
131 cat $OBJ/sshd_proxy_bak
132 echo HostKey $OBJ/cert_host_key_${ktype}
133 echo HostCertificate $OBJ/cert_host_key_${ktype}-cert.pub
134 ) > $OBJ/sshd_proxy
135 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
136 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
137 -F $OBJ/ssh_proxy somehost true >/dev/null 2>&1
138 if [ $? -eq 0 ]; then
139 fail "ssh cert connect succeeded unexpectedly"
140 fi
141done
142
143# Create a CA key and add it to known hosts
144(
Darren Tucker56347ef2013-05-17 13:28:36 +1000145 printf '@cert-authority '
146 printf "$HOSTS "
Damien Miller700dcfa2010-03-04 21:58:01 +1100147 cat $OBJ/host_ca_key.pub
148) > $OBJ/known_hosts-cert
149
Damien Miller58ac6de2010-02-27 07:57:12 +1100150test_one() {
151 ident=$1
152 result=$2
153 sign_opts=$3
Damien Miller53f4bb62010-04-18 08:15:14 +1000154
155 for kt in rsa rsa_v00 ; do
156 case $kt in
157 *_v00) args="-t v00" ;;
158 *) args="" ;;
159 esac
160
161 verbose "$tid: host cert connect $ident $kt expect $result"
162 ${SSHKEYGEN} -q -s $OBJ/host_ca_key \
163 -I "regress host key for $USER" \
164 $sign_opts $args \
165 $OBJ/cert_host_key_${kt} ||
166 fail "couldn't sign cert_host_key_${kt}"
167 (
168 cat $OBJ/sshd_proxy_bak
169 echo HostKey $OBJ/cert_host_key_${kt}
170 echo HostCertificate $OBJ/cert_host_key_${kt}-cert.pub
171 ) > $OBJ/sshd_proxy
Damien Miller58ac6de2010-02-27 07:57:12 +1100172
Damien Miller53f4bb62010-04-18 08:15:14 +1000173 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
174 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
175 -F $OBJ/ssh_proxy somehost true >/dev/null 2>&1
176 rc=$?
177 if [ "x$result" = "xsuccess" ] ; then
178 if [ $rc -ne 0 ]; then
179 fail "ssh cert connect $ident failed unexpectedly"
180 fi
181 else
182 if [ $rc -eq 0 ]; then
183 fail "ssh cert connect $ident succeeded unexpectedly"
184 fi
Damien Miller58ac6de2010-02-27 07:57:12 +1100185 fi
Damien Miller53f4bb62010-04-18 08:15:14 +1000186 done
Damien Miller58ac6de2010-02-27 07:57:12 +1100187}
188
189test_one "user-certificate" failure "-n $HOSTS"
190test_one "empty principals" success "-h"
191test_one "wrong principals" failure "-h -n foo"
192test_one "cert not yet valid" failure "-h -V20200101:20300101"
193test_one "cert expired" failure "-h -V19800101:19900101"
194test_one "cert valid interval" success "-h -V-1w:+2w"
195test_one "cert has constraints" failure "-h -Oforce-command=false"
196
197# Check downgrade of cert to raw key when no CA found
Damien Miller53f4bb62010-04-18 08:15:14 +1000198for v in v01 v00 ; do
Damien Millerf54542a2013-12-07 16:32:44 +1100199 for ktype in $PLAIN_TYPES ; do
200 type_has_legacy $ktype || continue
Damien Miller53f4bb62010-04-18 08:15:14 +1000201 rm -f $OBJ/known_hosts-cert $OBJ/cert_host_key*
202 verbose "$tid: host ${ktype} ${v} cert downgrade to raw key"
203 # Generate and sign a host key
204 ${SSHKEYGEN} -q -N '' -t ${ktype} \
205 -f $OBJ/cert_host_key_${ktype} || \
206 fail "ssh-keygen of cert_host_key_${ktype} failed"
207 ${SSHKEYGEN} -t ${v} -h -q -s $OBJ/host_ca_key \
208 -I "regress host key for $USER" \
209 -n $HOSTS $OBJ/cert_host_key_${ktype} ||
210 fail "couldn't sign cert_host_key_${ktype}"
211 (
Darren Tucker56347ef2013-05-17 13:28:36 +1000212 printf "$HOSTS "
Damien Miller53f4bb62010-04-18 08:15:14 +1000213 cat $OBJ/cert_host_key_${ktype}.pub
214 ) > $OBJ/known_hosts-cert
215 (
216 cat $OBJ/sshd_proxy_bak
217 echo HostKey $OBJ/cert_host_key_${ktype}
218 echo HostCertificate $OBJ/cert_host_key_${ktype}-cert.pub
219 ) > $OBJ/sshd_proxy
220
221 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
222 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
223 -F $OBJ/ssh_proxy somehost true
224 if [ $? -ne 0 ]; then
225 fail "ssh cert connect failed"
226 fi
227 done
Damien Miller58ac6de2010-02-27 07:57:12 +1100228done
229
Damien Miller017d1e72010-03-04 21:57:21 +1100230# Wrong certificate
231(
Darren Tucker56347ef2013-05-17 13:28:36 +1000232 printf '@cert-authority '
233 printf "$HOSTS "
Damien Miller017d1e72010-03-04 21:57:21 +1100234 cat $OBJ/host_ca_key.pub
235) > $OBJ/known_hosts-cert
Damien Miller53f4bb62010-04-18 08:15:14 +1000236for v in v01 v00 ; do
Damien Millerf54542a2013-12-07 16:32:44 +1100237 for kt in $PLAIN_TYPES ; do
238 type_has_legacy $kt || continue
Damien Miller53f4bb62010-04-18 08:15:14 +1000239 rm -f $OBJ/cert_host_key*
240 # Self-sign key
241 ${SSHKEYGEN} -q -N '' -t ${kt} \
242 -f $OBJ/cert_host_key_${kt} || \
243 fail "ssh-keygen of cert_host_key_${kt} failed"
244 ${SSHKEYGEN} -t ${v} -h -q -s $OBJ/cert_host_key_${kt} \
245 -I "regress host key for $USER" \
246 -n $HOSTS $OBJ/cert_host_key_${kt} ||
247 fail "couldn't sign cert_host_key_${kt}"
248 verbose "$tid: host ${kt} connect wrong cert"
249 (
250 cat $OBJ/sshd_proxy_bak
251 echo HostKey $OBJ/cert_host_key_${kt}
252 echo HostCertificate $OBJ/cert_host_key_${kt}-cert.pub
253 ) > $OBJ/sshd_proxy
254
255 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
256 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
257 -F $OBJ/ssh_proxy -q somehost true >/dev/null 2>&1
258 if [ $? -eq 0 ]; then
259 fail "ssh cert connect $ident succeeded unexpectedly"
260 fi
261 done
Damien Miller017d1e72010-03-04 21:57:21 +1100262done
263
Damien Miller58ac6de2010-02-27 07:57:12 +1100264rm -f $OBJ/known_hosts-cert $OBJ/host_ca_key* $OBJ/cert_host_key*