Damien Miller | f54542a | 2013-12-07 16:32:44 +1100 | [diff] [blame^] | 1 | # $OpenBSD: cert-hostkey.sh,v 1.8 2013/12/06 13:52:46 markus 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 | |
| 6 | rm -f $OBJ/known_hosts-cert $OBJ/host_ca_key* $OBJ/cert_host_key* |
| 7 | cp $OBJ/sshd_proxy $OBJ/sshd_proxy_bak |
| 8 | |
| 9 | HOSTS='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 Tucker | 56347ef | 2013-05-17 13:28:36 +1000 | [diff] [blame] | 15 | printf '@cert-authority ' |
| 16 | printf "$HOSTS " |
Damien Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 17 | cat $OBJ/host_ca_key.pub |
| 18 | ) > $OBJ/known_hosts-cert |
| 19 | |
Damien Miller | f54542a | 2013-12-07 16:32:44 +1100 | [diff] [blame^] | 20 | PLAIN_TYPES=`$SSH -Q key-plain | sed 's/^ssh-dss/ssh-dsa/g;s/^ssh-//'` |
| 21 | |
| 22 | type_has_legacy() { |
| 23 | case $1 in |
| 24 | ed25519*|ecdsa*) return 1 ;; |
| 25 | esac |
| 26 | return 0 |
| 27 | } |
| 28 | |
Damien Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 29 | # Generate and sign host keys |
Damien Miller | f54542a | 2013-12-07 16:32:44 +1100 | [diff] [blame^] | 30 | for ktype in $PLAIN_TYPES ; do |
Damien Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 31 | 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 Miller | f54542a | 2013-12-07 16:32:44 +1100 | [diff] [blame^] | 40 | type_has_legacy $ktype || continue |
Damien Miller | 53f4bb6 | 2010-04-18 08:15:14 +1000 | [diff] [blame] | 41 | 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 Miller | f54542a | 2013-12-07 16:32:44 +1100 | [diff] [blame^] | 43 | verbose "$tid: sign host ${ktype}_v00 cert" |
Damien Miller | 53f4bb6 | 2010-04-18 08:15:14 +1000 | [diff] [blame] | 44 | ${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 Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 48 | done |
| 49 | |
| 50 | # Basic connect tests |
| 51 | for privsep in yes no ; do |
Damien Miller | f54542a | 2013-12-07 16:32:44 +1100 | [diff] [blame^] | 52 | for ktype in $PLAIN_TYPES rsa_v00 dsa_v00; do |
Damien Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 53 | 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 |
| 68 | done |
| 69 | |
Damien Miller | 700dcfa | 2010-03-04 21:58:01 +1100 | [diff] [blame] | 70 | # Revoked certificates with key present |
| 71 | ( |
Darren Tucker | 56347ef | 2013-05-17 13:28:36 +1000 | [diff] [blame] | 72 | printf '@cert-authority ' |
| 73 | printf "$HOSTS " |
Damien Miller | 700dcfa | 2010-03-04 21:58:01 +1100 | [diff] [blame] | 74 | cat $OBJ/host_ca_key.pub |
Darren Tucker | 56347ef | 2013-05-17 13:28:36 +1000 | [diff] [blame] | 75 | printf '@revoked ' |
| 76 | printf "* " |
Damien Miller | 700dcfa | 2010-03-04 21:58:01 +1100 | [diff] [blame] | 77 | cat $OBJ/cert_host_key_rsa.pub |
Tim Rice | c5c346b | 2011-01-13 22:36:14 -0800 | [diff] [blame] | 78 | if test "x$TEST_SSH_ECC" = "xyes"; then |
Damien Miller | f54542a | 2013-12-07 16:32:44 +1100 | [diff] [blame^] | 79 | cat $OBJ/cert_host_key_ecdsa-sha2-nistp256.pub |
Darren Tucker | 56347ef | 2013-05-17 13:28:36 +1000 | [diff] [blame] | 80 | printf '@revoked ' |
| 81 | printf "* " |
Damien Miller | f54542a | 2013-12-07 16:32:44 +1100 | [diff] [blame^] | 82 | 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 Rice | c5c346b | 2011-01-13 22:36:14 -0800 | [diff] [blame] | 89 | fi |
Darren Tucker | 56347ef | 2013-05-17 13:28:36 +1000 | [diff] [blame] | 90 | printf '@revoked ' |
| 91 | printf "* " |
Damien Miller | 700dcfa | 2010-03-04 21:58:01 +1100 | [diff] [blame] | 92 | cat $OBJ/cert_host_key_dsa.pub |
Darren Tucker | 56347ef | 2013-05-17 13:28:36 +1000 | [diff] [blame] | 93 | printf '@revoked ' |
| 94 | printf "* " |
Damien Miller | 53f4bb6 | 2010-04-18 08:15:14 +1000 | [diff] [blame] | 95 | cat $OBJ/cert_host_key_rsa_v00.pub |
Darren Tucker | 56347ef | 2013-05-17 13:28:36 +1000 | [diff] [blame] | 96 | printf '@revoked ' |
| 97 | printf "* " |
Damien Miller | 53f4bb6 | 2010-04-18 08:15:14 +1000 | [diff] [blame] | 98 | cat $OBJ/cert_host_key_dsa_v00.pub |
Damien Miller | 700dcfa | 2010-03-04 21:58:01 +1100 | [diff] [blame] | 99 | ) > $OBJ/known_hosts-cert |
| 100 | for privsep in yes no ; do |
Damien Miller | f54542a | 2013-12-07 16:32:44 +1100 | [diff] [blame^] | 101 | for ktype in $PLAIN_TYPES rsa_v00 dsa_v00; do |
Damien Miller | 700dcfa | 2010-03-04 21:58:01 +1100 | [diff] [blame] | 102 | 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 |
| 117 | done |
| 118 | |
| 119 | # Revoked CA |
| 120 | ( |
Darren Tucker | 56347ef | 2013-05-17 13:28:36 +1000 | [diff] [blame] | 121 | printf '@cert-authority ' |
| 122 | printf "$HOSTS " |
Damien Miller | 700dcfa | 2010-03-04 21:58:01 +1100 | [diff] [blame] | 123 | cat $OBJ/host_ca_key.pub |
Darren Tucker | 56347ef | 2013-05-17 13:28:36 +1000 | [diff] [blame] | 124 | printf '@revoked ' |
| 125 | printf "* " |
Damien Miller | 700dcfa | 2010-03-04 21:58:01 +1100 | [diff] [blame] | 126 | cat $OBJ/host_ca_key.pub |
| 127 | ) > $OBJ/known_hosts-cert |
Damien Miller | f54542a | 2013-12-07 16:32:44 +1100 | [diff] [blame^] | 128 | for ktype in $PLAIN_TYPES rsa_v00 dsa_v00 ; do |
Damien Miller | 700dcfa | 2010-03-04 21:58:01 +1100 | [diff] [blame] | 129 | 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 |
| 141 | done |
| 142 | |
| 143 | # Create a CA key and add it to known hosts |
| 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 |
| 148 | ) > $OBJ/known_hosts-cert |
| 149 | |
Damien Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 150 | test_one() { |
| 151 | ident=$1 |
| 152 | result=$2 |
| 153 | sign_opts=$3 |
Damien Miller | 53f4bb6 | 2010-04-18 08:15:14 +1000 | [diff] [blame] | 154 | |
| 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 Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 172 | |
Damien Miller | 53f4bb6 | 2010-04-18 08:15:14 +1000 | [diff] [blame] | 173 | ${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 Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 185 | fi |
Damien Miller | 53f4bb6 | 2010-04-18 08:15:14 +1000 | [diff] [blame] | 186 | done |
Damien Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | test_one "user-certificate" failure "-n $HOSTS" |
| 190 | test_one "empty principals" success "-h" |
| 191 | test_one "wrong principals" failure "-h -n foo" |
| 192 | test_one "cert not yet valid" failure "-h -V20200101:20300101" |
| 193 | test_one "cert expired" failure "-h -V19800101:19900101" |
| 194 | test_one "cert valid interval" success "-h -V-1w:+2w" |
| 195 | test_one "cert has constraints" failure "-h -Oforce-command=false" |
| 196 | |
| 197 | # Check downgrade of cert to raw key when no CA found |
Damien Miller | 53f4bb6 | 2010-04-18 08:15:14 +1000 | [diff] [blame] | 198 | for v in v01 v00 ; do |
Damien Miller | f54542a | 2013-12-07 16:32:44 +1100 | [diff] [blame^] | 199 | for ktype in $PLAIN_TYPES ; do |
| 200 | type_has_legacy $ktype || continue |
Damien Miller | 53f4bb6 | 2010-04-18 08:15:14 +1000 | [diff] [blame] | 201 | 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 Tucker | 56347ef | 2013-05-17 13:28:36 +1000 | [diff] [blame] | 212 | printf "$HOSTS " |
Damien Miller | 53f4bb6 | 2010-04-18 08:15:14 +1000 | [diff] [blame] | 213 | 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 Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 228 | done |
| 229 | |
Damien Miller | 017d1e7 | 2010-03-04 21:57:21 +1100 | [diff] [blame] | 230 | # Wrong certificate |
| 231 | ( |
Darren Tucker | 56347ef | 2013-05-17 13:28:36 +1000 | [diff] [blame] | 232 | printf '@cert-authority ' |
| 233 | printf "$HOSTS " |
Damien Miller | 017d1e7 | 2010-03-04 21:57:21 +1100 | [diff] [blame] | 234 | cat $OBJ/host_ca_key.pub |
| 235 | ) > $OBJ/known_hosts-cert |
Damien Miller | 53f4bb6 | 2010-04-18 08:15:14 +1000 | [diff] [blame] | 236 | for v in v01 v00 ; do |
Damien Miller | f54542a | 2013-12-07 16:32:44 +1100 | [diff] [blame^] | 237 | for kt in $PLAIN_TYPES ; do |
| 238 | type_has_legacy $kt || continue |
Damien Miller | 53f4bb6 | 2010-04-18 08:15:14 +1000 | [diff] [blame] | 239 | 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 Miller | 017d1e7 | 2010-03-04 21:57:21 +1100 | [diff] [blame] | 262 | done |
| 263 | |
Damien Miller | 58ac6de | 2010-02-27 07:57:12 +1100 | [diff] [blame] | 264 | rm -f $OBJ/known_hosts-cert $OBJ/host_ca_key* $OBJ/cert_host_key* |