djm@openbsd.org | 6a977a4 | 2015-07-03 04:39:23 +0000 | [diff] [blame^] | 1 | # $OpenBSD: cert-hostkey.sh,v 1.12 2015/07/03 04:39:23 djm Exp $ |
Damien Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 2 | # Placed in the Public Domain. |
| 3 | |
| 4 | tid="certified host keys" |
| 5 | |
djm@openbsd.org | d85e062 | 2015-01-19 06:01:32 +0000 | [diff] [blame] | 6 | rm -f $OBJ/known_hosts-cert* $OBJ/host_ca_key* $OBJ/host_revoked_* |
djm@openbsd.org | 3dfd8d9 | 2014-12-04 22:31:50 +0000 | [diff] [blame] | 7 | rm -f $OBJ/cert_host_key* $OBJ/host_krl_* |
Damien Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 8 | cp $OBJ/sshd_proxy $OBJ/sshd_proxy_bak |
| 9 | |
| 10 | HOSTS='localhost-with-alias,127.0.0.1,::1' |
| 11 | |
djm@openbsd.org | 3dfd8d9 | 2014-12-04 22:31:50 +0000 | [diff] [blame] | 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 ||\ |
Damien Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 14 | fail "ssh-keygen of host_ca_key failed" |
| 15 | ( |
Darren Tucker | 56347ef | 2013-05-17 13:28:36 +1000 | [diff] [blame] | 16 | printf '@cert-authority ' |
| 17 | printf "$HOSTS " |
Damien Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 18 | cat $OBJ/host_ca_key.pub |
djm@openbsd.org | d85e062 | 2015-01-19 06:01:32 +0000 | [diff] [blame] | 19 | ) > $OBJ/known_hosts-cert.orig |
| 20 | cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert |
Damien Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 21 | |
djm@openbsd.org | 3dfd8d9 | 2014-12-04 22:31:50 +0000 | [diff] [blame] | 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 |
| 27 | |
Damien Miller | f54542a | 2013-12-07 16:32:44 +1100 | [diff] [blame] | 28 | PLAIN_TYPES=`$SSH -Q key-plain | sed 's/^ssh-dss/ssh-dsa/g;s/^ssh-//'` |
| 29 | |
djm@openbsd.org | 3dfd8d9 | 2014-12-04 22:31:50 +0000 | [diff] [blame] | 30 | # 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 Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 37 | # Generate and sign host keys |
djm@openbsd.org | 3dfd8d9 | 2014-12-04 22:31:50 +0000 | [diff] [blame] | 38 | serial=1 |
Damien Miller | f54542a | 2013-12-07 16:32:44 +1100 | [diff] [blame] | 39 | for ktype in $PLAIN_TYPES ; do |
Damien Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 40 | 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.org | 3dfd8d9 | 2014-12-04 22:31:50 +0000 | [diff] [blame] | 44 | 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 Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 49 | -I "regress host key for $USER" \ |
| 50 | -n $HOSTS $OBJ/cert_host_key_${ktype} || |
djm@openbsd.org | 3dfd8d9 | 2014-12-04 22:31:50 +0000 | [diff] [blame] | 51 | 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 Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 57 | done |
| 58 | |
djm@openbsd.org | 3dfd8d9 | 2014-12-04 22:31:50 +0000 | [diff] [blame] | 59 | attempt_connect() { |
| 60 | _ident="$1" |
| 61 | _expect_success="$2" |
| 62 | shift; shift |
| 63 | verbose "$tid: $_ident expect success $_expect_success" |
djm@openbsd.org | d85e062 | 2015-01-19 06:01:32 +0000 | [diff] [blame] | 64 | cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert |
djm@openbsd.org | 3dfd8d9 | 2014-12-04 22:31:50 +0000 | [diff] [blame] | 65 | ${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 Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 81 | for privsep in yes no ; do |
djm@openbsd.org | 6a977a4 | 2015-07-03 04:39:23 +0000 | [diff] [blame^] | 82 | for ktype in $PLAIN_TYPES ; do |
Damien Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 83 | 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.org | 3dfd8d9 | 2014-12-04 22:31:50 +0000 | [diff] [blame] | 91 | # 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 Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 109 | done |
| 110 | done |
| 111 | |
Damien Miller | 700dcfa | 2010-03-04 21:58:01 +1100 | [diff] [blame] | 112 | # Revoked certificates with key present |
| 113 | ( |
Darren Tucker | 56347ef | 2013-05-17 13:28:36 +1000 | [diff] [blame] | 114 | printf '@cert-authority ' |
| 115 | printf "$HOSTS " |
Damien Miller | 700dcfa | 2010-03-04 21:58:01 +1100 | [diff] [blame] | 116 | cat $OBJ/host_ca_key.pub |
djm@openbsd.org | 6a977a4 | 2015-07-03 04:39:23 +0000 | [diff] [blame^] | 117 | for ktype in $PLAIN_TYPES ; do |
Damien Miller | f9df7f6 | 2014-01-20 20:07:15 +1100 | [diff] [blame] | 118 | 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.org | d85e062 | 2015-01-19 06:01:32 +0000 | [diff] [blame] | 121 | ) > $OBJ/known_hosts-cert.orig |
| 122 | cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert |
Damien Miller | 700dcfa | 2010-03-04 21:58:01 +1100 | [diff] [blame] | 123 | for privsep in yes no ; do |
djm@openbsd.org | 6a977a4 | 2015-07-03 04:39:23 +0000 | [diff] [blame^] | 124 | for ktype in $PLAIN_TYPES ; do |
Damien Miller | 700dcfa | 2010-03-04 21:58:01 +1100 | [diff] [blame] | 125 | 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.org | d85e062 | 2015-01-19 06:01:32 +0000 | [diff] [blame] | 133 | cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert |
Damien Miller | 700dcfa | 2010-03-04 21:58:01 +1100 | [diff] [blame] | 134 | ${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 |
| 141 | done |
| 142 | |
| 143 | # Revoked CA |
| 144 | ( |
Darren Tucker | 56347ef | 2013-05-17 13:28:36 +1000 | [diff] [blame] | 145 | printf '@cert-authority ' |
| 146 | printf "$HOSTS " |
Damien Miller | 700dcfa | 2010-03-04 21:58:01 +1100 | [diff] [blame] | 147 | cat $OBJ/host_ca_key.pub |
Darren Tucker | 56347ef | 2013-05-17 13:28:36 +1000 | [diff] [blame] | 148 | printf '@revoked ' |
| 149 | printf "* " |
Damien Miller | 700dcfa | 2010-03-04 21:58:01 +1100 | [diff] [blame] | 150 | cat $OBJ/host_ca_key.pub |
djm@openbsd.org | d85e062 | 2015-01-19 06:01:32 +0000 | [diff] [blame] | 151 | ) > $OBJ/known_hosts-cert.orig |
| 152 | cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert |
djm@openbsd.org | 6a977a4 | 2015-07-03 04:39:23 +0000 | [diff] [blame^] | 153 | for ktype in $PLAIN_TYPES ; do |
Damien Miller | 700dcfa | 2010-03-04 21:58:01 +1100 | [diff] [blame] | 154 | 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.org | d85e062 | 2015-01-19 06:01:32 +0000 | [diff] [blame] | 160 | cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert |
Damien Miller | 700dcfa | 2010-03-04 21:58:01 +1100 | [diff] [blame] | 161 | ${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 |
| 167 | done |
| 168 | |
| 169 | # Create a CA key and add it to known hosts |
| 170 | ( |
Darren Tucker | 56347ef | 2013-05-17 13:28:36 +1000 | [diff] [blame] | 171 | printf '@cert-authority ' |
| 172 | printf "$HOSTS " |
Damien Miller | 700dcfa | 2010-03-04 21:58:01 +1100 | [diff] [blame] | 173 | cat $OBJ/host_ca_key.pub |
djm@openbsd.org | d85e062 | 2015-01-19 06:01:32 +0000 | [diff] [blame] | 174 | ) > $OBJ/known_hosts-cert.orig |
| 175 | cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert |
Damien Miller | 700dcfa | 2010-03-04 21:58:01 +1100 | [diff] [blame] | 176 | |
Damien Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 177 | test_one() { |
| 178 | ident=$1 |
| 179 | result=$2 |
| 180 | sign_opts=$3 |
Damien Miller | 53f4bb6 | 2010-04-18 08:15:14 +1000 | [diff] [blame] | 181 | |
djm@openbsd.org | 6a977a4 | 2015-07-03 04:39:23 +0000 | [diff] [blame^] | 182 | for kt in rsa ed25519 ; do |
Damien Miller | 53f4bb6 | 2010-04-18 08:15:14 +1000 | [diff] [blame] | 183 | ${SSHKEYGEN} -q -s $OBJ/host_ca_key \ |
| 184 | -I "regress host key for $USER" \ |
djm@openbsd.org | 6a977a4 | 2015-07-03 04:39:23 +0000 | [diff] [blame^] | 185 | $sign_opts $OBJ/cert_host_key_${kt} || |
Damien Miller | 53f4bb6 | 2010-04-18 08:15:14 +1000 | [diff] [blame] | 186 | 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 Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 192 | |
djm@openbsd.org | d85e062 | 2015-01-19 06:01:32 +0000 | [diff] [blame] | 193 | cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert |
Damien Miller | 53f4bb6 | 2010-04-18 08:15:14 +1000 | [diff] [blame] | 194 | ${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 Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 206 | fi |
Damien Miller | 53f4bb6 | 2010-04-18 08:15:14 +1000 | [diff] [blame] | 207 | done |
Damien Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | test_one "user-certificate" failure "-n $HOSTS" |
| 211 | test_one "empty principals" success "-h" |
| 212 | test_one "wrong principals" failure "-h -n foo" |
| 213 | test_one "cert not yet valid" failure "-h -V20200101:20300101" |
| 214 | test_one "cert expired" failure "-h -V19800101:19900101" |
| 215 | test_one "cert valid interval" success "-h -V-1w:+2w" |
| 216 | test_one "cert has constraints" failure "-h -Oforce-command=false" |
| 217 | |
| 218 | # Check downgrade of cert to raw key when no CA found |
djm@openbsd.org | 6a977a4 | 2015-07-03 04:39:23 +0000 | [diff] [blame^] | 219 | for 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 Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 246 | done |
| 247 | |
Damien Miller | 017d1e7 | 2010-03-04 21:57:21 +1100 | [diff] [blame] | 248 | # Wrong certificate |
| 249 | ( |
Darren Tucker | 56347ef | 2013-05-17 13:28:36 +1000 | [diff] [blame] | 250 | printf '@cert-authority ' |
| 251 | printf "$HOSTS " |
Damien Miller | 017d1e7 | 2010-03-04 21:57:21 +1100 | [diff] [blame] | 252 | cat $OBJ/host_ca_key.pub |
djm@openbsd.org | d85e062 | 2015-01-19 06:01:32 +0000 | [diff] [blame] | 253 | ) > $OBJ/known_hosts-cert.orig |
| 254 | cp $OBJ/known_hosts-cert.orig $OBJ/known_hosts-cert |
djm@openbsd.org | 6a977a4 | 2015-07-03 04:39:23 +0000 | [diff] [blame^] | 255 | for 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 Miller | 017d1e7 | 2010-03-04 21:57:21 +1100 | [diff] [blame] | 279 | done |
| 280 | |
djm@openbsd.org | d85e062 | 2015-01-19 06:01:32 +0000 | [diff] [blame] | 281 | rm -f $OBJ/known_hosts-cert* $OBJ/host_ca_key* $OBJ/cert_host_key* |