blob: 3f53922c89e034fffb47d7ab1993f094c41ca223 [file] [log] [blame]
markus@openbsd.org5bf09332015-07-10 06:23:25 +00001# $OpenBSD: cert-hostkey.sh,v 1.13 2015/07/10 06:23:25 markus Exp $
Damien Miller58ac6de2010-02-27 07:57:12 +11002# Placed in the Public Domain.
3
4tid="certified host keys"
5
djm@openbsd.orgd85e0622015-01-19 06:01:32 +00006rm -f $OBJ/known_hosts-cert* $OBJ/host_ca_key* $OBJ/host_revoked_*
djm@openbsd.org3dfd8d92014-12-04 22:31:50 +00007rm -f $OBJ/cert_host_key* $OBJ/host_krl_*
markus@openbsd.org5bf09332015-07-10 06:23:25 +00008
9# Allow all hostkey/pubkey types, prefer certs for the client
10types=""
11for i in `$SSH -Q key`; do
12 if [ -z "$types" ]; then
13 types="$i"
14 continue
15 fi
16 case "$i" in
17 *cert*) types="$i,$types";;
18 *) types="$types,$i";;
19 esac
20done
21(
22 echo "HostKeyAlgorithms ${types}"
23 echo "PubkeyAcceptedKeyTypes *"
24) >> $OBJ/ssh_proxy
Damien Miller58ac6de2010-02-27 07:57:12 +110025cp $OBJ/sshd_proxy $OBJ/sshd_proxy_bak
markus@openbsd.org5bf09332015-07-10 06:23:25 +000026(
27 echo "HostKeyAlgorithms *"
28 echo "PubkeyAcceptedKeyTypes *"
29) >> $OBJ/sshd_proxy_bak
Damien Miller58ac6de2010-02-27 07:57:12 +110030
31HOSTS='localhost-with-alias,127.0.0.1,::1'
32
djm@openbsd.org3dfd8d92014-12-04 22:31:50 +000033# Create a CA key and add it to known hosts. Ed25519 chosed for speed.
34${SSHKEYGEN} -q -N '' -t ed25519 -f $OBJ/host_ca_key ||\
Damien Miller58ac6de2010-02-27 07:57:12 +110035 fail "ssh-keygen of host_ca_key failed"
36(
Darren Tucker56347ef2013-05-17 13:28:36 +100037 printf '@cert-authority '
38 printf "$HOSTS "
Damien Miller58ac6de2010-02-27 07:57:12 +110039 cat $OBJ/host_ca_key.pub
djm@openbsd.orgd85e0622015-01-19 06:01:32 +000040) > $OBJ/known_hosts-cert.orig
41cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
Damien Miller58ac6de2010-02-27 07:57:12 +110042
djm@openbsd.org3dfd8d92014-12-04 22:31:50 +000043# Plain text revocation files
44touch $OBJ/host_revoked_empty
45touch $OBJ/host_revoked_plain
46touch $OBJ/host_revoked_cert
47cp $OBJ/host_ca_key.pub $OBJ/host_revoked_ca
48
Damien Millerf54542a2013-12-07 16:32:44 +110049PLAIN_TYPES=`$SSH -Q key-plain | sed 's/^ssh-dss/ssh-dsa/g;s/^ssh-//'`
50
djm@openbsd.org3dfd8d92014-12-04 22:31:50 +000051# Prepare certificate, plain key and CA KRLs
52${SSHKEYGEN} -kf $OBJ/host_krl_empty || fatal "KRL init failed"
53${SSHKEYGEN} -kf $OBJ/host_krl_plain || fatal "KRL init failed"
54${SSHKEYGEN} -kf $OBJ/host_krl_cert || fatal "KRL init failed"
55${SSHKEYGEN} -kf $OBJ/host_krl_ca $OBJ/host_ca_key.pub \
56 || fatal "KRL init failed"
57
Damien Miller58ac6de2010-02-27 07:57:12 +110058# Generate and sign host keys
djm@openbsd.org3dfd8d92014-12-04 22:31:50 +000059serial=1
Damien Millerf54542a2013-12-07 16:32:44 +110060for ktype in $PLAIN_TYPES ; do
Damien Miller58ac6de2010-02-27 07:57:12 +110061 verbose "$tid: sign host ${ktype} cert"
62 # Generate and sign a host key
63 ${SSHKEYGEN} -q -N '' -t ${ktype} \
64 -f $OBJ/cert_host_key_${ktype} || \
djm@openbsd.org3dfd8d92014-12-04 22:31:50 +000065 fatal "ssh-keygen of cert_host_key_${ktype} failed"
66 ${SSHKEYGEN} -ukf $OBJ/host_krl_plain \
67 $OBJ/cert_host_key_${ktype}.pub || fatal "KRL update failed"
68 cat $OBJ/cert_host_key_${ktype}.pub >> $OBJ/host_revoked_plain
69 ${SSHKEYGEN} -h -q -s $OBJ/host_ca_key -z $serial \
Damien Miller58ac6de2010-02-27 07:57:12 +110070 -I "regress host key for $USER" \
71 -n $HOSTS $OBJ/cert_host_key_${ktype} ||
djm@openbsd.org3dfd8d92014-12-04 22:31:50 +000072 fatal "couldn't sign cert_host_key_${ktype}"
73 ${SSHKEYGEN} -ukf $OBJ/host_krl_cert \
74 $OBJ/cert_host_key_${ktype}-cert.pub || \
75 fatal "KRL update failed"
76 cat $OBJ/cert_host_key_${ktype}-cert.pub >> $OBJ/host_revoked_cert
77 serial=`expr $serial + 1`
Damien Miller58ac6de2010-02-27 07:57:12 +110078done
79
djm@openbsd.org3dfd8d92014-12-04 22:31:50 +000080attempt_connect() {
81 _ident="$1"
82 _expect_success="$2"
83 shift; shift
84 verbose "$tid: $_ident expect success $_expect_success"
djm@openbsd.orgd85e0622015-01-19 06:01:32 +000085 cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
djm@openbsd.org3dfd8d92014-12-04 22:31:50 +000086 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
87 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
88 "$@" -F $OBJ/ssh_proxy somehost true
89 _r=$?
90 if [ "x$_expect_success" = "xyes" ] ; then
91 if [ $_r -ne 0 ]; then
92 fail "ssh cert connect $_ident failed"
93 fi
94 else
95 if [ $_r -eq 0 ]; then
96 fail "ssh cert connect $_ident succeeded unexpectedly"
97 fi
98 fi
99}
100
101# Basic connect and revocation tests.
Damien Miller58ac6de2010-02-27 07:57:12 +1100102for privsep in yes no ; do
djm@openbsd.org6a977a42015-07-03 04:39:23 +0000103 for ktype in $PLAIN_TYPES ; do
Damien Miller58ac6de2010-02-27 07:57:12 +1100104 verbose "$tid: host ${ktype} cert connect privsep $privsep"
105 (
106 cat $OBJ/sshd_proxy_bak
107 echo HostKey $OBJ/cert_host_key_${ktype}
108 echo HostCertificate $OBJ/cert_host_key_${ktype}-cert.pub
109 echo UsePrivilegeSeparation $privsep
110 ) > $OBJ/sshd_proxy
111
djm@openbsd.org3dfd8d92014-12-04 22:31:50 +0000112 # test name expect success
113 attempt_connect "$ktype basic connect" "yes"
114 attempt_connect "$ktype empty KRL" "yes" \
115 -oRevokedHostKeys=$OBJ/host_krl_empty
116 attempt_connect "$ktype KRL w/ plain key revoked" "no" \
117 -oRevokedHostKeys=$OBJ/host_krl_plain
118 attempt_connect "$ktype KRL w/ cert revoked" "no" \
119 -oRevokedHostKeys=$OBJ/host_krl_cert
120 attempt_connect "$ktype KRL w/ CA revoked" "no" \
121 -oRevokedHostKeys=$OBJ/host_krl_ca
122 attempt_connect "$ktype empty plaintext revocation" "yes" \
123 -oRevokedHostKeys=$OBJ/host_revoked_empty
124 attempt_connect "$ktype plain key plaintext revocation" "no" \
125 -oRevokedHostKeys=$OBJ/host_revoked_plain
126 attempt_connect "$ktype cert plaintext revocation" "no" \
127 -oRevokedHostKeys=$OBJ/host_revoked_cert
128 attempt_connect "$ktype CA plaintext revocation" "no" \
129 -oRevokedHostKeys=$OBJ/host_revoked_ca
Damien Miller58ac6de2010-02-27 07:57:12 +1100130 done
131done
132
Damien Miller700dcfa2010-03-04 21:58:01 +1100133# Revoked certificates with key present
134(
Darren Tucker56347ef2013-05-17 13:28:36 +1000135 printf '@cert-authority '
136 printf "$HOSTS "
Damien Miller700dcfa2010-03-04 21:58:01 +1100137 cat $OBJ/host_ca_key.pub
djm@openbsd.org6a977a42015-07-03 04:39:23 +0000138 for ktype in $PLAIN_TYPES ; do
Damien Millerf9df7f62014-01-20 20:07:15 +1100139 test -f "$OBJ/cert_host_key_${ktype}.pub" || fatal "no pubkey"
140 printf "@revoked * `cat $OBJ/cert_host_key_${ktype}.pub`\n"
141 done
djm@openbsd.orgd85e0622015-01-19 06:01:32 +0000142) > $OBJ/known_hosts-cert.orig
143cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
Damien Miller700dcfa2010-03-04 21:58:01 +1100144for privsep in yes no ; do
djm@openbsd.org6a977a42015-07-03 04:39:23 +0000145 for ktype in $PLAIN_TYPES ; do
Damien Miller700dcfa2010-03-04 21:58:01 +1100146 verbose "$tid: host ${ktype} revoked cert privsep $privsep"
147 (
148 cat $OBJ/sshd_proxy_bak
149 echo HostKey $OBJ/cert_host_key_${ktype}
150 echo HostCertificate $OBJ/cert_host_key_${ktype}-cert.pub
151 echo UsePrivilegeSeparation $privsep
152 ) > $OBJ/sshd_proxy
153
djm@openbsd.orgd85e0622015-01-19 06:01:32 +0000154 cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
Damien Miller700dcfa2010-03-04 21:58:01 +1100155 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
156 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
157 -F $OBJ/ssh_proxy somehost true >/dev/null 2>&1
158 if [ $? -eq 0 ]; then
159 fail "ssh cert connect succeeded unexpectedly"
160 fi
161 done
162done
163
164# Revoked CA
165(
Darren Tucker56347ef2013-05-17 13:28:36 +1000166 printf '@cert-authority '
167 printf "$HOSTS "
Damien Miller700dcfa2010-03-04 21:58:01 +1100168 cat $OBJ/host_ca_key.pub
Darren Tucker56347ef2013-05-17 13:28:36 +1000169 printf '@revoked '
170 printf "* "
Damien Miller700dcfa2010-03-04 21:58:01 +1100171 cat $OBJ/host_ca_key.pub
djm@openbsd.orgd85e0622015-01-19 06:01:32 +0000172) > $OBJ/known_hosts-cert.orig
173cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
djm@openbsd.org6a977a42015-07-03 04:39:23 +0000174for ktype in $PLAIN_TYPES ; do
Damien Miller700dcfa2010-03-04 21:58:01 +1100175 verbose "$tid: host ${ktype} revoked cert"
176 (
177 cat $OBJ/sshd_proxy_bak
178 echo HostKey $OBJ/cert_host_key_${ktype}
179 echo HostCertificate $OBJ/cert_host_key_${ktype}-cert.pub
180 ) > $OBJ/sshd_proxy
djm@openbsd.orgd85e0622015-01-19 06:01:32 +0000181 cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
Damien Miller700dcfa2010-03-04 21:58:01 +1100182 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
183 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
184 -F $OBJ/ssh_proxy somehost true >/dev/null 2>&1
185 if [ $? -eq 0 ]; then
186 fail "ssh cert connect succeeded unexpectedly"
187 fi
188done
189
190# Create a CA key and add it to known hosts
191(
Darren Tucker56347ef2013-05-17 13:28:36 +1000192 printf '@cert-authority '
193 printf "$HOSTS "
Damien Miller700dcfa2010-03-04 21:58:01 +1100194 cat $OBJ/host_ca_key.pub
djm@openbsd.orgd85e0622015-01-19 06:01:32 +0000195) > $OBJ/known_hosts-cert.orig
196cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
Damien Miller700dcfa2010-03-04 21:58:01 +1100197
Damien Miller58ac6de2010-02-27 07:57:12 +1100198test_one() {
199 ident=$1
200 result=$2
201 sign_opts=$3
Damien Miller53f4bb62010-04-18 08:15:14 +1000202
djm@openbsd.org6a977a42015-07-03 04:39:23 +0000203 for kt in rsa ed25519 ; do
Damien Miller53f4bb62010-04-18 08:15:14 +1000204 ${SSHKEYGEN} -q -s $OBJ/host_ca_key \
205 -I "regress host key for $USER" \
djm@openbsd.org6a977a42015-07-03 04:39:23 +0000206 $sign_opts $OBJ/cert_host_key_${kt} ||
Damien Miller53f4bb62010-04-18 08:15:14 +1000207 fail "couldn't sign cert_host_key_${kt}"
208 (
209 cat $OBJ/sshd_proxy_bak
210 echo HostKey $OBJ/cert_host_key_${kt}
211 echo HostCertificate $OBJ/cert_host_key_${kt}-cert.pub
212 ) > $OBJ/sshd_proxy
Damien Miller58ac6de2010-02-27 07:57:12 +1100213
djm@openbsd.orgd85e0622015-01-19 06:01:32 +0000214 cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
Damien Miller53f4bb62010-04-18 08:15:14 +1000215 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
216 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
217 -F $OBJ/ssh_proxy somehost true >/dev/null 2>&1
218 rc=$?
219 if [ "x$result" = "xsuccess" ] ; then
220 if [ $rc -ne 0 ]; then
221 fail "ssh cert connect $ident failed unexpectedly"
222 fi
223 else
224 if [ $rc -eq 0 ]; then
225 fail "ssh cert connect $ident succeeded unexpectedly"
226 fi
Damien Miller58ac6de2010-02-27 07:57:12 +1100227 fi
Damien Miller53f4bb62010-04-18 08:15:14 +1000228 done
Damien Miller58ac6de2010-02-27 07:57:12 +1100229}
230
231test_one "user-certificate" failure "-n $HOSTS"
232test_one "empty principals" success "-h"
233test_one "wrong principals" failure "-h -n foo"
234test_one "cert not yet valid" failure "-h -V20200101:20300101"
235test_one "cert expired" failure "-h -V19800101:19900101"
236test_one "cert valid interval" success "-h -V-1w:+2w"
237test_one "cert has constraints" failure "-h -Oforce-command=false"
238
239# Check downgrade of cert to raw key when no CA found
djm@openbsd.org6a977a42015-07-03 04:39:23 +0000240for ktype in $PLAIN_TYPES ; do
241 rm -f $OBJ/known_hosts-cert $OBJ/cert_host_key*
242 verbose "$tid: host ${ktype} ${v} cert downgrade to raw key"
243 # Generate and sign a host key
244 ${SSHKEYGEN} -q -N '' -t ${ktype} \
245 -f $OBJ/cert_host_key_${ktype} || \
246 fail "ssh-keygen of cert_host_key_${ktype} failed"
247 ${SSHKEYGEN} -t ${v} -h -q -s $OBJ/host_ca_key \
248 -I "regress host key for $USER" \
249 -n $HOSTS $OBJ/cert_host_key_${ktype} ||
250 fail "couldn't sign cert_host_key_${ktype}"
251 (
252 printf "$HOSTS "
253 cat $OBJ/cert_host_key_${ktype}.pub
254 ) > $OBJ/known_hosts-cert
255 (
256 cat $OBJ/sshd_proxy_bak
257 echo HostKey $OBJ/cert_host_key_${ktype}
258 echo HostCertificate $OBJ/cert_host_key_${ktype}-cert.pub
259 ) > $OBJ/sshd_proxy
260
261 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
262 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
263 -F $OBJ/ssh_proxy somehost true
264 if [ $? -ne 0 ]; then
265 fail "ssh cert connect failed"
266 fi
Damien Miller58ac6de2010-02-27 07:57:12 +1100267done
268
Damien Miller017d1e72010-03-04 21:57:21 +1100269# Wrong certificate
270(
Darren Tucker56347ef2013-05-17 13:28:36 +1000271 printf '@cert-authority '
272 printf "$HOSTS "
Damien Miller017d1e72010-03-04 21:57:21 +1100273 cat $OBJ/host_ca_key.pub
djm@openbsd.orgd85e0622015-01-19 06:01:32 +0000274) > $OBJ/known_hosts-cert.orig
275cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
djm@openbsd.org6a977a42015-07-03 04:39:23 +0000276for kt in $PLAIN_TYPES ; do
277 rm -f $OBJ/cert_host_key*
278 # Self-sign key
279 ${SSHKEYGEN} -q -N '' -t ${kt} \
280 -f $OBJ/cert_host_key_${kt} || \
281 fail "ssh-keygen of cert_host_key_${kt} failed"
282 ${SSHKEYGEN} -t ${v} -h -q -s $OBJ/cert_host_key_${kt} \
283 -I "regress host key for $USER" \
284 -n $HOSTS $OBJ/cert_host_key_${kt} ||
285 fail "couldn't sign cert_host_key_${kt}"
286 verbose "$tid: host ${kt} connect wrong cert"
287 (
288 cat $OBJ/sshd_proxy_bak
289 echo HostKey $OBJ/cert_host_key_${kt}
290 echo HostCertificate $OBJ/cert_host_key_${kt}-cert.pub
291 ) > $OBJ/sshd_proxy
292
293 cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
294 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
295 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
296 -F $OBJ/ssh_proxy -q somehost true >/dev/null 2>&1
297 if [ $? -eq 0 ]; then
298 fail "ssh cert connect $ident succeeded unexpectedly"
299 fi
Damien Miller017d1e72010-03-04 21:57:21 +1100300done
301
djm@openbsd.orgd85e0622015-01-19 06:01:32 +0000302rm -f $OBJ/known_hosts-cert* $OBJ/host_ca_key* $OBJ/cert_host_key*