1 # $OpenBSD: cert-hostkey.sh,v 1.11 2015/01/19 06:01:32 djm Exp $
2 # Placed in the Public Domain.
4 tid="certified host keys"
6 rm -f $OBJ/known_hosts-cert* $OBJ/host_ca_key* $OBJ/host_revoked_*
7 rm -f $OBJ/cert_host_key* $OBJ/host_krl_*
8 cp $OBJ/sshd_proxy $OBJ/sshd_proxy_bak
10 HOSTS='localhost-with-alias,127.0.0.1,::1'
12 # Create a CA key and add it to known hosts. Ed25519 chosed for speed.
13 ${SSHKEYGEN} -q -N '' -t ed25519 -f $OBJ/host_ca_key ||\
14 fail "ssh-keygen of host_ca_key failed"
16 printf '@cert-authority '
18 cat $OBJ/host_ca_key.pub
19 ) > $OBJ/known_hosts-cert.orig
20 cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
22 # Plain text revocation files
23 touch $OBJ/host_revoked_empty
24 touch $OBJ/host_revoked_plain
25 touch $OBJ/host_revoked_cert
26 cp $OBJ/host_ca_key.pub $OBJ/host_revoked_ca
28 PLAIN_TYPES=`$SSH -Q key-plain | sed 's/^ssh-dss/ssh-dsa/g;s/^ssh-//'`
32 ed25519*|ecdsa*) return 1 ;;
37 # Prepare certificate, plain key and CA KRLs
38 ${SSHKEYGEN} -kf $OBJ/host_krl_empty || fatal "KRL init failed"
39 ${SSHKEYGEN} -kf $OBJ/host_krl_plain || fatal "KRL init failed"
40 ${SSHKEYGEN} -kf $OBJ/host_krl_cert || fatal "KRL init failed"
41 ${SSHKEYGEN} -kf $OBJ/host_krl_ca $OBJ/host_ca_key.pub \
42 || fatal "KRL init failed"
44 # Generate and sign host keys
46 for ktype in $PLAIN_TYPES ; do
47 verbose "$tid: sign host ${ktype} cert"
48 # Generate and sign a host key
49 ${SSHKEYGEN} -q -N '' -t ${ktype} \
50 -f $OBJ/cert_host_key_${ktype} || \
51 fatal "ssh-keygen of cert_host_key_${ktype} failed"
52 ${SSHKEYGEN} -ukf $OBJ/host_krl_plain \
53 $OBJ/cert_host_key_${ktype}.pub || fatal "KRL update failed"
54 cat $OBJ/cert_host_key_${ktype}.pub >> $OBJ/host_revoked_plain
55 ${SSHKEYGEN} -h -q -s $OBJ/host_ca_key -z $serial \
56 -I "regress host key for $USER" \
57 -n $HOSTS $OBJ/cert_host_key_${ktype} ||
58 fatal "couldn't sign cert_host_key_${ktype}"
59 ${SSHKEYGEN} -ukf $OBJ/host_krl_cert \
60 $OBJ/cert_host_key_${ktype}-cert.pub || \
61 fatal "KRL update failed"
62 cat $OBJ/cert_host_key_${ktype}-cert.pub >> $OBJ/host_revoked_cert
63 serial=`expr $serial + 1`
64 type_has_legacy $ktype || continue
65 cp $OBJ/cert_host_key_${ktype} $OBJ/cert_host_key_${ktype}_v00
66 cp $OBJ/cert_host_key_${ktype}.pub $OBJ/cert_host_key_${ktype}_v00.pub
67 verbose "$tid: sign host ${ktype}_v00 cert"
68 ${SSHKEYGEN} -t v00 -h -q -s $OBJ/host_ca_key \
69 -I "regress host key for $USER" \
70 -n $HOSTS $OBJ/cert_host_key_${ktype}_v00 ||
71 fatal "couldn't sign cert_host_key_${ktype}_v00"
72 ${SSHKEYGEN} -ukf $OBJ/host_krl_cert \
73 $OBJ/cert_host_key_${ktype}_v00-cert.pub || \
74 fatal "KRL update failed"
75 cat $OBJ/cert_host_key_${ktype}_v00-cert.pub >> $OBJ/host_revoked_cert
82 verbose "$tid: $_ident expect success $_expect_success"
83 cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
84 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
85 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
86 "$@" -F $OBJ/ssh_proxy somehost true
88 if [ "x$_expect_success" = "xyes" ] ; then
89 if [ $_r -ne 0 ]; then
90 fail "ssh cert connect $_ident failed"
93 if [ $_r -eq 0 ]; then
94 fail "ssh cert connect $_ident succeeded unexpectedly"
99 # Basic connect and revocation tests.
100 for privsep in yes no ; do
101 for ktype in $PLAIN_TYPES rsa_v00 dsa_v00; do
102 verbose "$tid: host ${ktype} cert connect privsep $privsep"
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
110 # test name expect success
111 attempt_connect "$ktype basic connect" "yes"
112 attempt_connect "$ktype empty KRL" "yes" \
113 -oRevokedHostKeys=$OBJ/host_krl_empty
114 attempt_connect "$ktype KRL w/ plain key revoked" "no" \
115 -oRevokedHostKeys=$OBJ/host_krl_plain
116 attempt_connect "$ktype KRL w/ cert revoked" "no" \
117 -oRevokedHostKeys=$OBJ/host_krl_cert
118 attempt_connect "$ktype KRL w/ CA revoked" "no" \
119 -oRevokedHostKeys=$OBJ/host_krl_ca
120 attempt_connect "$ktype empty plaintext revocation" "yes" \
121 -oRevokedHostKeys=$OBJ/host_revoked_empty
122 attempt_connect "$ktype plain key plaintext revocation" "no" \
123 -oRevokedHostKeys=$OBJ/host_revoked_plain
124 attempt_connect "$ktype cert plaintext revocation" "no" \
125 -oRevokedHostKeys=$OBJ/host_revoked_cert
126 attempt_connect "$ktype CA plaintext revocation" "no" \
127 -oRevokedHostKeys=$OBJ/host_revoked_ca
131 # Revoked certificates with key present
133 printf '@cert-authority '
135 cat $OBJ/host_ca_key.pub
136 for ktype in $PLAIN_TYPES rsa_v00 dsa_v00; do
137 test -f "$OBJ/cert_host_key_${ktype}.pub" || fatal "no pubkey"
138 printf "@revoked * `cat $OBJ/cert_host_key_${ktype}.pub`\n"
140 ) > $OBJ/known_hosts-cert.orig
141 cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
142 for privsep in yes no ; do
143 for ktype in $PLAIN_TYPES rsa_v00 dsa_v00; do
144 verbose "$tid: host ${ktype} revoked cert privsep $privsep"
146 cat $OBJ/sshd_proxy_bak
147 echo HostKey $OBJ/cert_host_key_${ktype}
148 echo HostCertificate $OBJ/cert_host_key_${ktype}-cert.pub
149 echo UsePrivilegeSeparation $privsep
152 cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
153 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
154 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
155 -F $OBJ/ssh_proxy somehost true >/dev/null 2>&1
156 if [ $? -eq 0 ]; then
157 fail "ssh cert connect succeeded unexpectedly"
164 printf '@cert-authority '
166 cat $OBJ/host_ca_key.pub
169 cat $OBJ/host_ca_key.pub
170 ) > $OBJ/known_hosts-cert.orig
171 cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
172 for ktype in $PLAIN_TYPES rsa_v00 dsa_v00 ; do
173 verbose "$tid: host ${ktype} revoked cert"
175 cat $OBJ/sshd_proxy_bak
176 echo HostKey $OBJ/cert_host_key_${ktype}
177 echo HostCertificate $OBJ/cert_host_key_${ktype}-cert.pub
179 cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
180 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
181 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
182 -F $OBJ/ssh_proxy somehost true >/dev/null 2>&1
183 if [ $? -eq 0 ]; then
184 fail "ssh cert connect succeeded unexpectedly"
188 # Create a CA key and add it to known hosts
190 printf '@cert-authority '
192 cat $OBJ/host_ca_key.pub
193 ) > $OBJ/known_hosts-cert.orig
194 cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
201 for kt in rsa rsa_v00 ; do
203 *_v00) args="-t v00" ;;
207 verbose "$tid: host cert connect $ident $kt expect $result"
208 ${SSHKEYGEN} -q -s $OBJ/host_ca_key \
209 -I "regress host key for $USER" \
211 $OBJ/cert_host_key_${kt} ||
212 fail "couldn't sign cert_host_key_${kt}"
214 cat $OBJ/sshd_proxy_bak
215 echo HostKey $OBJ/cert_host_key_${kt}
216 echo HostCertificate $OBJ/cert_host_key_${kt}-cert.pub
219 cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
220 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
221 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
222 -F $OBJ/ssh_proxy somehost true >/dev/null 2>&1
224 if [ "x$result" = "xsuccess" ] ; then
225 if [ $rc -ne 0 ]; then
226 fail "ssh cert connect $ident failed unexpectedly"
229 if [ $rc -eq 0 ]; then
230 fail "ssh cert connect $ident succeeded unexpectedly"
236 test_one "user-certificate" failure "-n $HOSTS"
237 test_one "empty principals" success "-h"
238 test_one "wrong principals" failure "-h -n foo"
239 test_one "cert not yet valid" failure "-h -V20200101:20300101"
240 test_one "cert expired" failure "-h -V19800101:19900101"
241 test_one "cert valid interval" success "-h -V-1w:+2w"
242 test_one "cert has constraints" failure "-h -Oforce-command=false"
244 # Check downgrade of cert to raw key when no CA found
245 for v in v01 v00 ; do
246 for ktype in $PLAIN_TYPES ; do
247 type_has_legacy $ktype || continue
248 rm -f $OBJ/known_hosts-cert $OBJ/cert_host_key*
249 verbose "$tid: host ${ktype} ${v} cert downgrade to raw key"
250 # Generate and sign a host key
251 ${SSHKEYGEN} -q -N '' -t ${ktype} \
252 -f $OBJ/cert_host_key_${ktype} || \
253 fail "ssh-keygen of cert_host_key_${ktype} failed"
254 ${SSHKEYGEN} -t ${v} -h -q -s $OBJ/host_ca_key \
255 -I "regress host key for $USER" \
256 -n $HOSTS $OBJ/cert_host_key_${ktype} ||
257 fail "couldn't sign cert_host_key_${ktype}"
260 cat $OBJ/cert_host_key_${ktype}.pub
261 ) > $OBJ/known_hosts-cert
263 cat $OBJ/sshd_proxy_bak
264 echo HostKey $OBJ/cert_host_key_${ktype}
265 echo HostCertificate $OBJ/cert_host_key_${ktype}-cert.pub
268 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
269 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
270 -F $OBJ/ssh_proxy somehost true
271 if [ $? -ne 0 ]; then
272 fail "ssh cert connect failed"
279 printf '@cert-authority '
281 cat $OBJ/host_ca_key.pub
282 ) > $OBJ/known_hosts-cert.orig
283 cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
284 for v in v01 v00 ; do
285 for kt in $PLAIN_TYPES ; do
286 type_has_legacy $kt || continue
287 rm -f $OBJ/cert_host_key*
289 ${SSHKEYGEN} -q -N '' -t ${kt} \
290 -f $OBJ/cert_host_key_${kt} || \
291 fail "ssh-keygen of cert_host_key_${kt} failed"
292 ${SSHKEYGEN} -t ${v} -h -q -s $OBJ/cert_host_key_${kt} \
293 -I "regress host key for $USER" \
294 -n $HOSTS $OBJ/cert_host_key_${kt} ||
295 fail "couldn't sign cert_host_key_${kt}"
296 verbose "$tid: host ${kt} connect wrong cert"
298 cat $OBJ/sshd_proxy_bak
299 echo HostKey $OBJ/cert_host_key_${kt}
300 echo HostCertificate $OBJ/cert_host_key_${kt}-cert.pub
303 cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
304 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
305 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
306 -F $OBJ/ssh_proxy -q somehost true >/dev/null 2>&1
307 if [ $? -eq 0 ]; then
308 fail "ssh cert connect $ident succeeded unexpectedly"
313 rm -f $OBJ/known_hosts-cert* $OBJ/host_ca_key* $OBJ/cert_host_key*