blob: c99c2b1c36de0ac83db42306540123da5ff40b94 [file] [log] [blame]
djm@openbsd.org6a977a42015-07-03 04:39:23 +00001# $OpenBSD: cert-hostkey.sh,v 1.12 2015/07/03 04:39:23 djm 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_*
Damien Miller58ac6de2010-02-27 07:57:12 +11008cp $OBJ/sshd_proxy $OBJ/sshd_proxy_bak
9
10HOSTS='localhost-with-alias,127.0.0.1,::1'
11
djm@openbsd.org3dfd8d92014-12-04 22:31:50 +000012# 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 ||\
Damien Miller58ac6de2010-02-27 07:57:12 +110014 fail "ssh-keygen of host_ca_key failed"
15(
Darren Tucker56347ef2013-05-17 13:28:36 +100016 printf '@cert-authority '
17 printf "$HOSTS "
Damien Miller58ac6de2010-02-27 07:57:12 +110018 cat $OBJ/host_ca_key.pub
djm@openbsd.orgd85e0622015-01-19 06:01:32 +000019) > $OBJ/known_hosts-cert.orig
20cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
Damien Miller58ac6de2010-02-27 07:57:12 +110021
djm@openbsd.org3dfd8d92014-12-04 22:31:50 +000022# Plain text revocation files
23touch $OBJ/host_revoked_empty
24touch $OBJ/host_revoked_plain
25touch $OBJ/host_revoked_cert
26cp $OBJ/host_ca_key.pub $OBJ/host_revoked_ca
27
Damien Millerf54542a2013-12-07 16:32:44 +110028PLAIN_TYPES=`$SSH -Q key-plain | sed 's/^ssh-dss/ssh-dsa/g;s/^ssh-//'`
29
djm@openbsd.org3dfd8d92014-12-04 22:31:50 +000030# Prepare certificate, plain key and CA KRLs
31${SSHKEYGEN} -kf $OBJ/host_krl_empty || fatal "KRL init failed"
32${SSHKEYGEN} -kf $OBJ/host_krl_plain || fatal "KRL init failed"
33${SSHKEYGEN} -kf $OBJ/host_krl_cert || fatal "KRL init failed"
34${SSHKEYGEN} -kf $OBJ/host_krl_ca $OBJ/host_ca_key.pub \
35 || fatal "KRL init failed"
36
Damien Miller58ac6de2010-02-27 07:57:12 +110037# Generate and sign host keys
djm@openbsd.org3dfd8d92014-12-04 22:31:50 +000038serial=1
Damien Millerf54542a2013-12-07 16:32:44 +110039for ktype in $PLAIN_TYPES ; do
Damien Miller58ac6de2010-02-27 07:57:12 +110040 verbose "$tid: sign host ${ktype} cert"
41 # Generate and sign a host key
42 ${SSHKEYGEN} -q -N '' -t ${ktype} \
43 -f $OBJ/cert_host_key_${ktype} || \
djm@openbsd.org3dfd8d92014-12-04 22:31:50 +000044 fatal "ssh-keygen of cert_host_key_${ktype} failed"
45 ${SSHKEYGEN} -ukf $OBJ/host_krl_plain \
46 $OBJ/cert_host_key_${ktype}.pub || fatal "KRL update failed"
47 cat $OBJ/cert_host_key_${ktype}.pub >> $OBJ/host_revoked_plain
48 ${SSHKEYGEN} -h -q -s $OBJ/host_ca_key -z $serial \
Damien Miller58ac6de2010-02-27 07:57:12 +110049 -I "regress host key for $USER" \
50 -n $HOSTS $OBJ/cert_host_key_${ktype} ||
djm@openbsd.org3dfd8d92014-12-04 22:31:50 +000051 fatal "couldn't sign cert_host_key_${ktype}"
52 ${SSHKEYGEN} -ukf $OBJ/host_krl_cert \
53 $OBJ/cert_host_key_${ktype}-cert.pub || \
54 fatal "KRL update failed"
55 cat $OBJ/cert_host_key_${ktype}-cert.pub >> $OBJ/host_revoked_cert
56 serial=`expr $serial + 1`
Damien Miller58ac6de2010-02-27 07:57:12 +110057done
58
djm@openbsd.org3dfd8d92014-12-04 22:31:50 +000059attempt_connect() {
60 _ident="$1"
61 _expect_success="$2"
62 shift; shift
63 verbose "$tid: $_ident expect success $_expect_success"
djm@openbsd.orgd85e0622015-01-19 06:01:32 +000064 cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
djm@openbsd.org3dfd8d92014-12-04 22:31:50 +000065 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
66 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
67 "$@" -F $OBJ/ssh_proxy somehost true
68 _r=$?
69 if [ "x$_expect_success" = "xyes" ] ; then
70 if [ $_r -ne 0 ]; then
71 fail "ssh cert connect $_ident failed"
72 fi
73 else
74 if [ $_r -eq 0 ]; then
75 fail "ssh cert connect $_ident succeeded unexpectedly"
76 fi
77 fi
78}
79
80# Basic connect and revocation tests.
Damien Miller58ac6de2010-02-27 07:57:12 +110081for privsep in yes no ; do
djm@openbsd.org6a977a42015-07-03 04:39:23 +000082 for ktype in $PLAIN_TYPES ; do
Damien Miller58ac6de2010-02-27 07:57:12 +110083 verbose "$tid: host ${ktype} cert connect privsep $privsep"
84 (
85 cat $OBJ/sshd_proxy_bak
86 echo HostKey $OBJ/cert_host_key_${ktype}
87 echo HostCertificate $OBJ/cert_host_key_${ktype}-cert.pub
88 echo UsePrivilegeSeparation $privsep
89 ) > $OBJ/sshd_proxy
90
djm@openbsd.org3dfd8d92014-12-04 22:31:50 +000091 # test name expect success
92 attempt_connect "$ktype basic connect" "yes"
93 attempt_connect "$ktype empty KRL" "yes" \
94 -oRevokedHostKeys=$OBJ/host_krl_empty
95 attempt_connect "$ktype KRL w/ plain key revoked" "no" \
96 -oRevokedHostKeys=$OBJ/host_krl_plain
97 attempt_connect "$ktype KRL w/ cert revoked" "no" \
98 -oRevokedHostKeys=$OBJ/host_krl_cert
99 attempt_connect "$ktype KRL w/ CA revoked" "no" \
100 -oRevokedHostKeys=$OBJ/host_krl_ca
101 attempt_connect "$ktype empty plaintext revocation" "yes" \
102 -oRevokedHostKeys=$OBJ/host_revoked_empty
103 attempt_connect "$ktype plain key plaintext revocation" "no" \
104 -oRevokedHostKeys=$OBJ/host_revoked_plain
105 attempt_connect "$ktype cert plaintext revocation" "no" \
106 -oRevokedHostKeys=$OBJ/host_revoked_cert
107 attempt_connect "$ktype CA plaintext revocation" "no" \
108 -oRevokedHostKeys=$OBJ/host_revoked_ca
Damien Miller58ac6de2010-02-27 07:57:12 +1100109 done
110done
111
Damien Miller700dcfa2010-03-04 21:58:01 +1100112# Revoked certificates with key present
113(
Darren Tucker56347ef2013-05-17 13:28:36 +1000114 printf '@cert-authority '
115 printf "$HOSTS "
Damien Miller700dcfa2010-03-04 21:58:01 +1100116 cat $OBJ/host_ca_key.pub
djm@openbsd.org6a977a42015-07-03 04:39:23 +0000117 for ktype in $PLAIN_TYPES ; do
Damien Millerf9df7f62014-01-20 20:07:15 +1100118 test -f "$OBJ/cert_host_key_${ktype}.pub" || fatal "no pubkey"
119 printf "@revoked * `cat $OBJ/cert_host_key_${ktype}.pub`\n"
120 done
djm@openbsd.orgd85e0622015-01-19 06:01:32 +0000121) > $OBJ/known_hosts-cert.orig
122cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
Damien Miller700dcfa2010-03-04 21:58:01 +1100123for privsep in yes no ; do
djm@openbsd.org6a977a42015-07-03 04:39:23 +0000124 for ktype in $PLAIN_TYPES ; do
Damien Miller700dcfa2010-03-04 21:58:01 +1100125 verbose "$tid: host ${ktype} revoked cert privsep $privsep"
126 (
127 cat $OBJ/sshd_proxy_bak
128 echo HostKey $OBJ/cert_host_key_${ktype}
129 echo HostCertificate $OBJ/cert_host_key_${ktype}-cert.pub
130 echo UsePrivilegeSeparation $privsep
131 ) > $OBJ/sshd_proxy
132
djm@openbsd.orgd85e0622015-01-19 06:01:32 +0000133 cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
Damien Miller700dcfa2010-03-04 21:58:01 +1100134 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
135 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
136 -F $OBJ/ssh_proxy somehost true >/dev/null 2>&1
137 if [ $? -eq 0 ]; then
138 fail "ssh cert connect succeeded unexpectedly"
139 fi
140 done
141done
142
143# Revoked CA
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
Darren Tucker56347ef2013-05-17 13:28:36 +1000148 printf '@revoked '
149 printf "* "
Damien Miller700dcfa2010-03-04 21:58:01 +1100150 cat $OBJ/host_ca_key.pub
djm@openbsd.orgd85e0622015-01-19 06:01:32 +0000151) > $OBJ/known_hosts-cert.orig
152cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
djm@openbsd.org6a977a42015-07-03 04:39:23 +0000153for ktype in $PLAIN_TYPES ; do
Damien Miller700dcfa2010-03-04 21:58:01 +1100154 verbose "$tid: host ${ktype} revoked cert"
155 (
156 cat $OBJ/sshd_proxy_bak
157 echo HostKey $OBJ/cert_host_key_${ktype}
158 echo HostCertificate $OBJ/cert_host_key_${ktype}-cert.pub
159 ) > $OBJ/sshd_proxy
djm@openbsd.orgd85e0622015-01-19 06:01:32 +0000160 cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
Damien Miller700dcfa2010-03-04 21:58:01 +1100161 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
162 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
163 -F $OBJ/ssh_proxy somehost true >/dev/null 2>&1
164 if [ $? -eq 0 ]; then
165 fail "ssh cert connect succeeded unexpectedly"
166 fi
167done
168
169# Create a CA key and add it to known hosts
170(
Darren Tucker56347ef2013-05-17 13:28:36 +1000171 printf '@cert-authority '
172 printf "$HOSTS "
Damien Miller700dcfa2010-03-04 21:58:01 +1100173 cat $OBJ/host_ca_key.pub
djm@openbsd.orgd85e0622015-01-19 06:01:32 +0000174) > $OBJ/known_hosts-cert.orig
175cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
Damien Miller700dcfa2010-03-04 21:58:01 +1100176
Damien Miller58ac6de2010-02-27 07:57:12 +1100177test_one() {
178 ident=$1
179 result=$2
180 sign_opts=$3
Damien Miller53f4bb62010-04-18 08:15:14 +1000181
djm@openbsd.org6a977a42015-07-03 04:39:23 +0000182 for kt in rsa ed25519 ; do
Damien Miller53f4bb62010-04-18 08:15:14 +1000183 ${SSHKEYGEN} -q -s $OBJ/host_ca_key \
184 -I "regress host key for $USER" \
djm@openbsd.org6a977a42015-07-03 04:39:23 +0000185 $sign_opts $OBJ/cert_host_key_${kt} ||
Damien Miller53f4bb62010-04-18 08:15:14 +1000186 fail "couldn't sign cert_host_key_${kt}"
187 (
188 cat $OBJ/sshd_proxy_bak
189 echo HostKey $OBJ/cert_host_key_${kt}
190 echo HostCertificate $OBJ/cert_host_key_${kt}-cert.pub
191 ) > $OBJ/sshd_proxy
Damien Miller58ac6de2010-02-27 07:57:12 +1100192
djm@openbsd.orgd85e0622015-01-19 06:01:32 +0000193 cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
Damien Miller53f4bb62010-04-18 08:15:14 +1000194 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
195 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
196 -F $OBJ/ssh_proxy somehost true >/dev/null 2>&1
197 rc=$?
198 if [ "x$result" = "xsuccess" ] ; then
199 if [ $rc -ne 0 ]; then
200 fail "ssh cert connect $ident failed unexpectedly"
201 fi
202 else
203 if [ $rc -eq 0 ]; then
204 fail "ssh cert connect $ident succeeded unexpectedly"
205 fi
Damien Miller58ac6de2010-02-27 07:57:12 +1100206 fi
Damien Miller53f4bb62010-04-18 08:15:14 +1000207 done
Damien Miller58ac6de2010-02-27 07:57:12 +1100208}
209
210test_one "user-certificate" failure "-n $HOSTS"
211test_one "empty principals" success "-h"
212test_one "wrong principals" failure "-h -n foo"
213test_one "cert not yet valid" failure "-h -V20200101:20300101"
214test_one "cert expired" failure "-h -V19800101:19900101"
215test_one "cert valid interval" success "-h -V-1w:+2w"
216test_one "cert has constraints" failure "-h -Oforce-command=false"
217
218# Check downgrade of cert to raw key when no CA found
djm@openbsd.org6a977a42015-07-03 04:39:23 +0000219for ktype in $PLAIN_TYPES ; do
220 rm -f $OBJ/known_hosts-cert $OBJ/cert_host_key*
221 verbose "$tid: host ${ktype} ${v} cert downgrade to raw key"
222 # Generate and sign a host key
223 ${SSHKEYGEN} -q -N '' -t ${ktype} \
224 -f $OBJ/cert_host_key_${ktype} || \
225 fail "ssh-keygen of cert_host_key_${ktype} failed"
226 ${SSHKEYGEN} -t ${v} -h -q -s $OBJ/host_ca_key \
227 -I "regress host key for $USER" \
228 -n $HOSTS $OBJ/cert_host_key_${ktype} ||
229 fail "couldn't sign cert_host_key_${ktype}"
230 (
231 printf "$HOSTS "
232 cat $OBJ/cert_host_key_${ktype}.pub
233 ) > $OBJ/known_hosts-cert
234 (
235 cat $OBJ/sshd_proxy_bak
236 echo HostKey $OBJ/cert_host_key_${ktype}
237 echo HostCertificate $OBJ/cert_host_key_${ktype}-cert.pub
238 ) > $OBJ/sshd_proxy
239
240 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
241 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
242 -F $OBJ/ssh_proxy somehost true
243 if [ $? -ne 0 ]; then
244 fail "ssh cert connect failed"
245 fi
Damien Miller58ac6de2010-02-27 07:57:12 +1100246done
247
Damien Miller017d1e72010-03-04 21:57:21 +1100248# Wrong certificate
249(
Darren Tucker56347ef2013-05-17 13:28:36 +1000250 printf '@cert-authority '
251 printf "$HOSTS "
Damien Miller017d1e72010-03-04 21:57:21 +1100252 cat $OBJ/host_ca_key.pub
djm@openbsd.orgd85e0622015-01-19 06:01:32 +0000253) > $OBJ/known_hosts-cert.orig
254cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
djm@openbsd.org6a977a42015-07-03 04:39:23 +0000255for kt in $PLAIN_TYPES ; do
256 rm -f $OBJ/cert_host_key*
257 # Self-sign key
258 ${SSHKEYGEN} -q -N '' -t ${kt} \
259 -f $OBJ/cert_host_key_${kt} || \
260 fail "ssh-keygen of cert_host_key_${kt} failed"
261 ${SSHKEYGEN} -t ${v} -h -q -s $OBJ/cert_host_key_${kt} \
262 -I "regress host key for $USER" \
263 -n $HOSTS $OBJ/cert_host_key_${kt} ||
264 fail "couldn't sign cert_host_key_${kt}"
265 verbose "$tid: host ${kt} connect wrong cert"
266 (
267 cat $OBJ/sshd_proxy_bak
268 echo HostKey $OBJ/cert_host_key_${kt}
269 echo HostCertificate $OBJ/cert_host_key_${kt}-cert.pub
270 ) > $OBJ/sshd_proxy
271
272 cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert
273 ${SSH} -2 -oUserKnownHostsFile=$OBJ/known_hosts-cert \
274 -oGlobalKnownHostsFile=$OBJ/known_hosts-cert \
275 -F $OBJ/ssh_proxy -q somehost true >/dev/null 2>&1
276 if [ $? -eq 0 ]; then
277 fail "ssh cert connect $ident succeeded unexpectedly"
278 fi
Damien Miller017d1e72010-03-04 21:57:21 +1100279done
280
djm@openbsd.orgd85e0622015-01-19 06:01:32 +0000281rm -f $OBJ/known_hosts-cert* $OBJ/host_ca_key* $OBJ/cert_host_key*