Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | */ |
| 24 | |
| 25 | #include "includes.h" |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame^] | 26 | RCSID("$OpenBSD: sshconnect2.c,v 1.54 2001/03/12 22:02:02 markus Exp $"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 27 | |
| 28 | #include <openssl/bn.h> |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 29 | #include <openssl/md5.h> |
| 30 | #include <openssl/dh.h> |
| 31 | #include <openssl/hmac.h> |
| 32 | |
| 33 | #include "ssh.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 34 | #include "ssh2.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 35 | #include "xmalloc.h" |
| 36 | #include "rsa.h" |
| 37 | #include "buffer.h" |
| 38 | #include "packet.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 39 | #include "uidswap.h" |
| 40 | #include "compat.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 41 | #include "bufaux.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 42 | #include "cipher.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 43 | #include "kex.h" |
| 44 | #include "myproposal.h" |
| 45 | #include "key.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 46 | #include "sshconnect.h" |
| 47 | #include "authfile.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 48 | #include "cli.h" |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 49 | #include "dispatch.h" |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 50 | #include "authfd.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 51 | #include "log.h" |
| 52 | #include "readconf.h" |
| 53 | #include "readpass.h" |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 54 | #include "match.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 55 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 56 | void ssh_dh1_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *); |
| 57 | void ssh_dhgex_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *); |
| 58 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 59 | /* import */ |
| 60 | extern char *client_version_string; |
| 61 | extern char *server_version_string; |
| 62 | extern Options options; |
| 63 | |
| 64 | /* |
| 65 | * SSH2 key exchange |
| 66 | */ |
| 67 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 68 | u_char *session_id2 = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 69 | int session_id2_len = 0; |
| 70 | |
| 71 | void |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 72 | ssh_kex2(char *host, struct sockaddr *hostaddr) |
| 73 | { |
| 74 | int i, plen; |
| 75 | Kex *kex; |
| 76 | Buffer *client_kexinit, *server_kexinit; |
| 77 | char *sprop[PROPOSAL_MAX]; |
| 78 | |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 79 | if (options.ciphers == (char *)-1) { |
| 80 | log("No valid ciphers for protocol version 2 given, using defaults."); |
| 81 | options.ciphers = NULL; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 82 | } |
| 83 | if (options.ciphers != NULL) { |
| 84 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = |
| 85 | myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers; |
| 86 | } |
| 87 | if (options.compression) { |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 88 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 89 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib"; |
| 90 | } else { |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 91 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 92 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "none"; |
| 93 | } |
Ben Lindstrom | 06b33aa | 2001-02-15 03:01:59 +0000 | [diff] [blame] | 94 | if (options.macs != NULL) { |
| 95 | myproposal[PROPOSAL_MAC_ALGS_CTOS] = |
| 96 | myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs; |
| 97 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 98 | |
| 99 | /* buffers with raw kexinit messages */ |
| 100 | server_kexinit = xmalloc(sizeof(*server_kexinit)); |
| 101 | buffer_init(server_kexinit); |
| 102 | client_kexinit = kex_init(myproposal); |
| 103 | |
| 104 | /* algorithm negotiation */ |
| 105 | kex_exchange_kexinit(client_kexinit, server_kexinit, sprop); |
| 106 | kex = kex_choose_conf(myproposal, sprop, 0); |
| 107 | for (i = 0; i < PROPOSAL_MAX; i++) |
| 108 | xfree(sprop[i]); |
| 109 | |
| 110 | /* server authentication and session key agreement */ |
| 111 | switch(kex->kex_type) { |
| 112 | case DH_GRP1_SHA1: |
| 113 | ssh_dh1_client(kex, host, hostaddr, |
| 114 | client_kexinit, server_kexinit); |
| 115 | break; |
| 116 | case DH_GEX_SHA1: |
| 117 | ssh_dhgex_client(kex, host, hostaddr, client_kexinit, |
| 118 | server_kexinit); |
| 119 | break; |
| 120 | default: |
| 121 | fatal("Unsupported key exchange %d", kex->kex_type); |
| 122 | } |
| 123 | |
| 124 | buffer_free(client_kexinit); |
| 125 | buffer_free(server_kexinit); |
| 126 | xfree(client_kexinit); |
| 127 | xfree(server_kexinit); |
| 128 | |
| 129 | debug("Wait SSH2_MSG_NEWKEYS."); |
| 130 | packet_read_expect(&plen, SSH2_MSG_NEWKEYS); |
| 131 | packet_done(); |
| 132 | debug("GOT SSH2_MSG_NEWKEYS."); |
| 133 | |
| 134 | debug("send SSH2_MSG_NEWKEYS."); |
| 135 | packet_start(SSH2_MSG_NEWKEYS); |
| 136 | packet_send(); |
| 137 | packet_write_wait(); |
| 138 | debug("done: send SSH2_MSG_NEWKEYS."); |
| 139 | |
| 140 | #ifdef DEBUG_KEXDH |
| 141 | /* send 1st encrypted/maced/compressed message */ |
| 142 | packet_start(SSH2_MSG_IGNORE); |
| 143 | packet_put_cstring("markus"); |
| 144 | packet_send(); |
| 145 | packet_write_wait(); |
| 146 | #endif |
| 147 | debug("done: KEX2."); |
| 148 | } |
| 149 | |
| 150 | /* diffie-hellman-group1-sha1 */ |
| 151 | |
| 152 | void |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 153 | ssh_dh1_client(Kex *kex, char *host, struct sockaddr *hostaddr, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 154 | Buffer *client_kexinit, Buffer *server_kexinit) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 155 | { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 156 | #ifdef DEBUG_KEXDH |
| 157 | int i; |
| 158 | #endif |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 159 | int plen, dlen; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 160 | u_int klen, kout; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 161 | char *signature = NULL; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 162 | u_int slen; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 163 | char *server_host_key_blob = NULL; |
| 164 | Key *server_host_key; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 165 | u_int sbloblen; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 166 | DH *dh; |
| 167 | BIGNUM *dh_server_pub = 0; |
| 168 | BIGNUM *shared_secret = 0; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 169 | u_char *kbuf; |
| 170 | u_char *hash; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 171 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 172 | debug("Sending SSH2_MSG_KEXDH_INIT."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 173 | /* generate and send 'e', client DH public key */ |
| 174 | dh = dh_new_group1(); |
Ben Lindstrom | 4c4f05e | 2001-03-06 01:09:20 +0000 | [diff] [blame] | 175 | dh_gen_key(dh, kex->we_need * 8); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 176 | packet_start(SSH2_MSG_KEXDH_INIT); |
| 177 | packet_put_bignum2(dh->pub_key); |
| 178 | packet_send(); |
| 179 | packet_write_wait(); |
| 180 | |
| 181 | #ifdef DEBUG_KEXDH |
| 182 | fprintf(stderr, "\np= "); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 183 | BN_print_fp(stderr, dh->p); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 184 | fprintf(stderr, "\ng= "); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 185 | BN_print_fp(stderr, dh->g); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 186 | fprintf(stderr, "\npub= "); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 187 | BN_print_fp(stderr, dh->pub_key); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 188 | fprintf(stderr, "\n"); |
| 189 | DHparams_print_fp(stderr, dh); |
| 190 | #endif |
| 191 | |
| 192 | debug("Wait SSH2_MSG_KEXDH_REPLY."); |
| 193 | |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 194 | packet_read_expect(&plen, SSH2_MSG_KEXDH_REPLY); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 195 | |
| 196 | debug("Got SSH2_MSG_KEXDH_REPLY."); |
| 197 | |
| 198 | /* key, cert */ |
| 199 | server_host_key_blob = packet_get_string(&sbloblen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 200 | server_host_key = key_from_blob(server_host_key_blob, sbloblen); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 201 | if (server_host_key == NULL) |
| 202 | fatal("cannot decode server_host_key_blob"); |
| 203 | |
| 204 | check_host_key(host, hostaddr, server_host_key, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 205 | options.user_hostfile2, options.system_hostfile2); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 206 | |
| 207 | /* DH paramter f, server public DH key */ |
| 208 | dh_server_pub = BN_new(); |
| 209 | if (dh_server_pub == NULL) |
| 210 | fatal("dh_server_pub == NULL"); |
| 211 | packet_get_bignum2(dh_server_pub, &dlen); |
| 212 | |
| 213 | #ifdef DEBUG_KEXDH |
| 214 | fprintf(stderr, "\ndh_server_pub= "); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 215 | BN_print_fp(stderr, dh_server_pub); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 216 | fprintf(stderr, "\n"); |
| 217 | debug("bits %d", BN_num_bits(dh_server_pub)); |
| 218 | #endif |
| 219 | |
| 220 | /* signed H */ |
| 221 | signature = packet_get_string(&slen); |
| 222 | packet_done(); |
| 223 | |
| 224 | if (!dh_pub_is_valid(dh, dh_server_pub)) |
| 225 | packet_disconnect("bad server public DH value"); |
| 226 | |
| 227 | klen = DH_size(dh); |
| 228 | kbuf = xmalloc(klen); |
| 229 | kout = DH_compute_key(kbuf, dh_server_pub, dh); |
| 230 | #ifdef DEBUG_KEXDH |
| 231 | debug("shared secret: len %d/%d", klen, kout); |
| 232 | fprintf(stderr, "shared secret == "); |
| 233 | for (i = 0; i< kout; i++) |
| 234 | fprintf(stderr, "%02x", (kbuf[i])&0xff); |
| 235 | fprintf(stderr, "\n"); |
| 236 | #endif |
| 237 | shared_secret = BN_new(); |
| 238 | |
| 239 | BN_bin2bn(kbuf, kout, shared_secret); |
| 240 | memset(kbuf, 0, klen); |
| 241 | xfree(kbuf); |
| 242 | |
| 243 | /* calc and verify H */ |
| 244 | hash = kex_hash( |
| 245 | client_version_string, |
| 246 | server_version_string, |
| 247 | buffer_ptr(client_kexinit), buffer_len(client_kexinit), |
| 248 | buffer_ptr(server_kexinit), buffer_len(server_kexinit), |
| 249 | server_host_key_blob, sbloblen, |
| 250 | dh->pub_key, |
| 251 | dh_server_pub, |
| 252 | shared_secret |
| 253 | ); |
| 254 | xfree(server_host_key_blob); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 255 | DH_free(dh); |
Ben Lindstrom | b1985f7 | 2001-01-23 00:19:15 +0000 | [diff] [blame] | 256 | BN_free(dh_server_pub); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 257 | #ifdef DEBUG_KEXDH |
| 258 | fprintf(stderr, "hash == "); |
| 259 | for (i = 0; i< 20; i++) |
| 260 | fprintf(stderr, "%02x", (hash[i])&0xff); |
| 261 | fprintf(stderr, "\n"); |
| 262 | #endif |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 263 | if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 264 | fatal("key_verify failed for server_host_key"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 265 | key_free(server_host_key); |
Ben Lindstrom | b1985f7 | 2001-01-23 00:19:15 +0000 | [diff] [blame] | 266 | xfree(signature); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 267 | |
| 268 | kex_derive_keys(kex, hash, shared_secret); |
Ben Lindstrom | b1985f7 | 2001-01-23 00:19:15 +0000 | [diff] [blame] | 269 | BN_clear_free(shared_secret); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 270 | packet_set_kex(kex); |
| 271 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 272 | /* save session id */ |
| 273 | session_id2_len = 20; |
| 274 | session_id2 = xmalloc(session_id2_len); |
| 275 | memcpy(session_id2, hash, session_id2_len); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 276 | } |
| 277 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 278 | /* diffie-hellman-group-exchange-sha1 */ |
| 279 | |
| 280 | /* |
| 281 | * Estimates the group order for a Diffie-Hellman group that has an |
| 282 | * attack complexity approximately the same as O(2**bits). Estimate |
| 283 | * with: O(exp(1.9223 * (ln q)^(1/3) (ln ln q)^(2/3))) |
| 284 | */ |
| 285 | |
| 286 | int |
| 287 | dh_estimate(int bits) |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 288 | { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 289 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 290 | if (bits < 64) |
| 291 | return (512); /* O(2**63) */ |
| 292 | if (bits < 128) |
| 293 | return (1024); /* O(2**86) */ |
| 294 | if (bits < 192) |
| 295 | return (2048); /* O(2**116) */ |
| 296 | return (4096); /* O(2**156) */ |
| 297 | } |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 298 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 299 | void |
| 300 | ssh_dhgex_client(Kex *kex, char *host, struct sockaddr *hostaddr, |
| 301 | Buffer *client_kexinit, Buffer *server_kexinit) |
| 302 | { |
| 303 | #ifdef DEBUG_KEXDH |
| 304 | int i; |
| 305 | #endif |
| 306 | int plen, dlen; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 307 | u_int klen, kout; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 308 | char *signature = NULL; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 309 | u_int slen, nbits; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 310 | char *server_host_key_blob = NULL; |
| 311 | Key *server_host_key; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 312 | u_int sbloblen; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 313 | DH *dh; |
| 314 | BIGNUM *dh_server_pub = 0; |
| 315 | BIGNUM *shared_secret = 0; |
| 316 | BIGNUM *p = 0, *g = 0; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 317 | u_char *kbuf; |
| 318 | u_char *hash; |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 319 | |
Ben Lindstrom | 4c4f05e | 2001-03-06 01:09:20 +0000 | [diff] [blame] | 320 | nbits = dh_estimate(kex->we_need * 8); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 321 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 322 | debug("Sending SSH2_MSG_KEX_DH_GEX_REQUEST."); |
| 323 | packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST); |
| 324 | packet_put_int(nbits); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 325 | packet_send(); |
| 326 | packet_write_wait(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 327 | |
| 328 | #ifdef DEBUG_KEXDH |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 329 | fprintf(stderr, "\nnbits = %d", nbits); |
| 330 | #endif |
| 331 | |
| 332 | debug("Wait SSH2_MSG_KEX_DH_GEX_GROUP."); |
| 333 | |
| 334 | packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_GROUP); |
| 335 | |
| 336 | debug("Got SSH2_MSG_KEX_DH_GEX_GROUP."); |
| 337 | |
| 338 | if ((p = BN_new()) == NULL) |
| 339 | fatal("BN_new"); |
| 340 | packet_get_bignum2(p, &dlen); |
| 341 | if ((g = BN_new()) == NULL) |
| 342 | fatal("BN_new"); |
| 343 | packet_get_bignum2(g, &dlen); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 344 | dh = dh_new_group(g, p); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 345 | |
Ben Lindstrom | 4c4f05e | 2001-03-06 01:09:20 +0000 | [diff] [blame] | 346 | dh_gen_key(dh, kex->we_need * 8); |
Kevin Steves | 6b87586 | 2000-12-15 23:31:01 +0000 | [diff] [blame] | 347 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 348 | #ifdef DEBUG_KEXDH |
| 349 | fprintf(stderr, "\np= "); |
| 350 | BN_print_fp(stderr, dh->p); |
| 351 | fprintf(stderr, "\ng= "); |
| 352 | BN_print_fp(stderr, dh->g); |
| 353 | fprintf(stderr, "\npub= "); |
| 354 | BN_print_fp(stderr, dh->pub_key); |
| 355 | fprintf(stderr, "\n"); |
| 356 | DHparams_print_fp(stderr, dh); |
| 357 | #endif |
| 358 | |
| 359 | debug("Sending SSH2_MSG_KEX_DH_GEX_INIT."); |
| 360 | /* generate and send 'e', client DH public key */ |
| 361 | packet_start(SSH2_MSG_KEX_DH_GEX_INIT); |
| 362 | packet_put_bignum2(dh->pub_key); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 363 | packet_send(); |
| 364 | packet_write_wait(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 365 | |
| 366 | debug("Wait SSH2_MSG_KEX_DH_GEX_REPLY."); |
| 367 | |
| 368 | packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_REPLY); |
| 369 | |
| 370 | debug("Got SSH2_MSG_KEXDH_REPLY."); |
| 371 | |
| 372 | /* key, cert */ |
| 373 | server_host_key_blob = packet_get_string(&sbloblen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 374 | server_host_key = key_from_blob(server_host_key_blob, sbloblen); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 375 | if (server_host_key == NULL) |
| 376 | fatal("cannot decode server_host_key_blob"); |
| 377 | |
| 378 | check_host_key(host, hostaddr, server_host_key, |
| 379 | options.user_hostfile2, options.system_hostfile2); |
| 380 | |
| 381 | /* DH paramter f, server public DH key */ |
| 382 | dh_server_pub = BN_new(); |
| 383 | if (dh_server_pub == NULL) |
| 384 | fatal("dh_server_pub == NULL"); |
| 385 | packet_get_bignum2(dh_server_pub, &dlen); |
| 386 | |
| 387 | #ifdef DEBUG_KEXDH |
| 388 | fprintf(stderr, "\ndh_server_pub= "); |
| 389 | BN_print_fp(stderr, dh_server_pub); |
| 390 | fprintf(stderr, "\n"); |
| 391 | debug("bits %d", BN_num_bits(dh_server_pub)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 392 | #endif |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 393 | |
| 394 | /* signed H */ |
| 395 | signature = packet_get_string(&slen); |
| 396 | packet_done(); |
| 397 | |
| 398 | if (!dh_pub_is_valid(dh, dh_server_pub)) |
| 399 | packet_disconnect("bad server public DH value"); |
| 400 | |
| 401 | klen = DH_size(dh); |
| 402 | kbuf = xmalloc(klen); |
| 403 | kout = DH_compute_key(kbuf, dh_server_pub, dh); |
| 404 | #ifdef DEBUG_KEXDH |
| 405 | debug("shared secret: len %d/%d", klen, kout); |
| 406 | fprintf(stderr, "shared secret == "); |
| 407 | for (i = 0; i< kout; i++) |
| 408 | fprintf(stderr, "%02x", (kbuf[i])&0xff); |
| 409 | fprintf(stderr, "\n"); |
| 410 | #endif |
| 411 | shared_secret = BN_new(); |
| 412 | |
| 413 | BN_bin2bn(kbuf, kout, shared_secret); |
| 414 | memset(kbuf, 0, klen); |
| 415 | xfree(kbuf); |
| 416 | |
| 417 | /* calc and verify H */ |
| 418 | hash = kex_hash_gex( |
| 419 | client_version_string, |
| 420 | server_version_string, |
| 421 | buffer_ptr(client_kexinit), buffer_len(client_kexinit), |
| 422 | buffer_ptr(server_kexinit), buffer_len(server_kexinit), |
| 423 | server_host_key_blob, sbloblen, |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 424 | nbits, dh->p, dh->g, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 425 | dh->pub_key, |
| 426 | dh_server_pub, |
| 427 | shared_secret |
| 428 | ); |
| 429 | xfree(server_host_key_blob); |
| 430 | DH_free(dh); |
Ben Lindstrom | b1985f7 | 2001-01-23 00:19:15 +0000 | [diff] [blame] | 431 | BN_free(dh_server_pub); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 432 | #ifdef DEBUG_KEXDH |
| 433 | fprintf(stderr, "hash == "); |
| 434 | for (i = 0; i< 20; i++) |
| 435 | fprintf(stderr, "%02x", (hash[i])&0xff); |
| 436 | fprintf(stderr, "\n"); |
| 437 | #endif |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 438 | if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 439 | fatal("key_verify failed for server_host_key"); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 440 | key_free(server_host_key); |
Ben Lindstrom | b1985f7 | 2001-01-23 00:19:15 +0000 | [diff] [blame] | 441 | xfree(signature); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 442 | |
| 443 | kex_derive_keys(kex, hash, shared_secret); |
Ben Lindstrom | b1985f7 | 2001-01-23 00:19:15 +0000 | [diff] [blame] | 444 | BN_clear_free(shared_secret); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 445 | packet_set_kex(kex); |
| 446 | |
| 447 | /* save session id */ |
| 448 | session_id2_len = 20; |
| 449 | session_id2 = xmalloc(session_id2_len); |
| 450 | memcpy(session_id2, hash, session_id2_len); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 451 | } |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 452 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 453 | /* |
| 454 | * Authenticate user |
| 455 | */ |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 456 | |
| 457 | typedef struct Authctxt Authctxt; |
| 458 | typedef struct Authmethod Authmethod; |
| 459 | |
| 460 | typedef int sign_cb_fn( |
| 461 | Authctxt *authctxt, Key *key, |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 462 | u_char **sigp, int *lenp, u_char *data, int datalen); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 463 | |
| 464 | struct Authctxt { |
| 465 | const char *server_user; |
| 466 | const char *host; |
| 467 | const char *service; |
| 468 | AuthenticationConnection *agent; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 469 | Authmethod *method; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 470 | int success; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 471 | char *authlist; |
| 472 | Key *last_key; |
| 473 | sign_cb_fn *last_key_sign; |
| 474 | int last_key_hint; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 475 | }; |
| 476 | struct Authmethod { |
| 477 | char *name; /* string to compare against server's list */ |
| 478 | int (*userauth)(Authctxt *authctxt); |
| 479 | int *enabled; /* flag in option struct that enables method */ |
| 480 | int *batch_flag; /* flag in option struct that disables method */ |
| 481 | }; |
| 482 | |
| 483 | void input_userauth_success(int type, int plen, void *ctxt); |
| 484 | void input_userauth_failure(int type, int plen, void *ctxt); |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 485 | void input_userauth_banner(int type, int plen, void *ctxt); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 486 | void input_userauth_error(int type, int plen, void *ctxt); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 487 | void input_userauth_info_req(int type, int plen, void *ctxt); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 488 | void input_userauth_pk_ok(int type, int plen, void *ctxt); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 489 | |
| 490 | int userauth_none(Authctxt *authctxt); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 491 | int userauth_pubkey(Authctxt *authctxt); |
| 492 | int userauth_passwd(Authctxt *authctxt); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 493 | int userauth_kbdint(Authctxt *authctxt); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 494 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 495 | void userauth(Authctxt *authctxt, char *authlist); |
| 496 | |
| 497 | int |
| 498 | sign_and_send_pubkey(Authctxt *authctxt, Key *k, |
| 499 | sign_cb_fn *sign_callback); |
| 500 | void clear_auth_state(Authctxt *authctxt); |
| 501 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 502 | Authmethod *authmethod_get(char *authlist); |
| 503 | Authmethod *authmethod_lookup(const char *name); |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 504 | char *authmethods_get(void); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 505 | |
| 506 | Authmethod authmethods[] = { |
| 507 | {"publickey", |
| 508 | userauth_pubkey, |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 509 | &options.pubkey_authentication, |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 510 | NULL}, |
| 511 | {"password", |
| 512 | userauth_passwd, |
| 513 | &options.password_authentication, |
| 514 | &options.batch_mode}, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 515 | {"keyboard-interactive", |
| 516 | userauth_kbdint, |
| 517 | &options.kbd_interactive_authentication, |
| 518 | &options.batch_mode}, |
| 519 | {"none", |
| 520 | userauth_none, |
| 521 | NULL, |
| 522 | NULL}, |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 523 | {NULL, NULL, NULL, NULL} |
| 524 | }; |
| 525 | |
| 526 | void |
| 527 | ssh_userauth2(const char *server_user, char *host) |
| 528 | { |
| 529 | Authctxt authctxt; |
| 530 | int type; |
| 531 | int plen; |
| 532 | |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 533 | if (options.challenge_reponse_authentication) |
| 534 | options.kbd_interactive_authentication = 1; |
| 535 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 536 | debug("send SSH2_MSG_SERVICE_REQUEST"); |
| 537 | packet_start(SSH2_MSG_SERVICE_REQUEST); |
| 538 | packet_put_cstring("ssh-userauth"); |
| 539 | packet_send(); |
| 540 | packet_write_wait(); |
| 541 | type = packet_read(&plen); |
| 542 | if (type != SSH2_MSG_SERVICE_ACCEPT) { |
| 543 | fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type); |
| 544 | } |
| 545 | if (packet_remaining() > 0) { |
| 546 | char *reply = packet_get_string(&plen); |
| 547 | debug("service_accept: %s", reply); |
| 548 | xfree(reply); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 549 | } else { |
| 550 | debug("buggy server: service_accept w/o service"); |
| 551 | } |
| 552 | packet_done(); |
| 553 | debug("got SSH2_MSG_SERVICE_ACCEPT"); |
| 554 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 555 | if (options.preferred_authentications == NULL) |
| 556 | options.preferred_authentications = authmethods_get(); |
| 557 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 558 | /* setup authentication context */ |
| 559 | authctxt.agent = ssh_get_authentication_connection(); |
| 560 | authctxt.server_user = server_user; |
| 561 | authctxt.host = host; |
| 562 | authctxt.service = "ssh-connection"; /* service name */ |
| 563 | authctxt.success = 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 564 | authctxt.method = authmethod_lookup("none"); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 565 | authctxt.authlist = NULL; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 566 | if (authctxt.method == NULL) |
| 567 | fatal("ssh_userauth2: internal error: cannot send userauth none request"); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 568 | |
| 569 | /* initial userauth request */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 570 | userauth_none(&authctxt); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 571 | |
| 572 | dispatch_init(&input_userauth_error); |
| 573 | dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success); |
| 574 | dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure); |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 575 | dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 576 | dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */ |
| 577 | |
| 578 | if (authctxt.agent != NULL) |
| 579 | ssh_close_authentication_connection(authctxt.agent); |
| 580 | |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 581 | debug("ssh-userauth2 successful: method %s", authctxt.method->name); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 582 | } |
| 583 | void |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 584 | userauth(Authctxt *authctxt, char *authlist) |
| 585 | { |
| 586 | if (authlist == NULL) { |
| 587 | authlist = authctxt->authlist; |
| 588 | } else { |
| 589 | if (authctxt->authlist) |
| 590 | xfree(authctxt->authlist); |
| 591 | authctxt->authlist = authlist; |
| 592 | } |
| 593 | for (;;) { |
| 594 | Authmethod *method = authmethod_get(authlist); |
| 595 | if (method == NULL) |
| 596 | fatal("Permission denied (%s).", authlist); |
| 597 | authctxt->method = method; |
| 598 | if (method->userauth(authctxt) != 0) { |
| 599 | debug2("we sent a %s packet, wait for reply", method->name); |
| 600 | break; |
| 601 | } else { |
| 602 | debug2("we did not send a packet, disable method"); |
| 603 | method->enabled = NULL; |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 608 | input_userauth_error(int type, int plen, void *ctxt) |
| 609 | { |
Ben Lindstrom | d26dcf3 | 2001-01-06 15:18:16 +0000 | [diff] [blame] | 610 | fatal("input_userauth_error: bad message during authentication: " |
| 611 | "type %d", type); |
| 612 | } |
| 613 | void |
| 614 | input_userauth_banner(int type, int plen, void *ctxt) |
| 615 | { |
| 616 | char *msg, *lang; |
| 617 | debug3("input_userauth_banner"); |
| 618 | msg = packet_get_string(NULL); |
| 619 | lang = packet_get_string(NULL); |
| 620 | fprintf(stderr, "%s", msg); |
| 621 | xfree(msg); |
| 622 | xfree(lang); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 623 | } |
| 624 | void |
| 625 | input_userauth_success(int type, int plen, void *ctxt) |
| 626 | { |
| 627 | Authctxt *authctxt = ctxt; |
| 628 | if (authctxt == NULL) |
| 629 | fatal("input_userauth_success: no authentication context"); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 630 | if (authctxt->authlist) |
| 631 | xfree(authctxt->authlist); |
| 632 | clear_auth_state(authctxt); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 633 | authctxt->success = 1; /* break out */ |
| 634 | } |
| 635 | void |
| 636 | input_userauth_failure(int type, int plen, void *ctxt) |
| 637 | { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 638 | Authctxt *authctxt = ctxt; |
| 639 | char *authlist = NULL; |
| 640 | int partial; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 641 | |
| 642 | if (authctxt == NULL) |
| 643 | fatal("input_userauth_failure: no authentication context"); |
| 644 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 645 | authlist = packet_get_string(NULL); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 646 | partial = packet_get_char(); |
| 647 | packet_done(); |
| 648 | |
| 649 | if (partial != 0) |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 650 | log("Authenticated with partial success."); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 651 | debug("authentications that can continue: %s", authlist); |
| 652 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 653 | clear_auth_state(authctxt); |
| 654 | userauth(authctxt, authlist); |
| 655 | } |
| 656 | void |
| 657 | input_userauth_pk_ok(int type, int plen, void *ctxt) |
| 658 | { |
| 659 | Authctxt *authctxt = ctxt; |
| 660 | Key *key = NULL; |
| 661 | Buffer b; |
| 662 | int alen, blen, pktype, sent = 0; |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame^] | 663 | char *pkalg, *pkblob, *fp; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 664 | |
| 665 | if (authctxt == NULL) |
| 666 | fatal("input_userauth_pk_ok: no authentication context"); |
| 667 | if (datafellows & SSH_BUG_PKOK) { |
| 668 | /* this is similar to SSH_BUG_PKAUTH */ |
| 669 | debug2("input_userauth_pk_ok: SSH_BUG_PKOK"); |
| 670 | pkblob = packet_get_string(&blen); |
| 671 | buffer_init(&b); |
| 672 | buffer_append(&b, pkblob, blen); |
| 673 | pkalg = buffer_get_string(&b, &alen); |
| 674 | buffer_free(&b); |
| 675 | } else { |
| 676 | pkalg = packet_get_string(&alen); |
| 677 | pkblob = packet_get_string(&blen); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 678 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 679 | packet_done(); |
| 680 | |
| 681 | debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d", |
| 682 | pkalg, blen, authctxt->last_key, authctxt->last_key_hint); |
| 683 | |
| 684 | do { |
| 685 | if (authctxt->last_key == NULL || |
| 686 | authctxt->last_key_sign == NULL) { |
| 687 | debug("no last key or no sign cb"); |
| 688 | break; |
| 689 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 690 | if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) { |
| 691 | debug("unknown pkalg %s", pkalg); |
| 692 | break; |
| 693 | } |
| 694 | if ((key = key_from_blob(pkblob, blen)) == NULL) { |
| 695 | debug("no key from blob. pkalg %s", pkalg); |
| 696 | break; |
| 697 | } |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame^] | 698 | fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX); |
| 699 | debug2("input_userauth_pk_ok: fp %s", fp); |
| 700 | xfree(fp); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 701 | if (!key_equal(key, authctxt->last_key)) { |
| 702 | debug("key != last_key"); |
| 703 | break; |
| 704 | } |
| 705 | sent = sign_and_send_pubkey(authctxt, key, |
| 706 | authctxt->last_key_sign); |
| 707 | } while(0); |
| 708 | |
| 709 | if (key != NULL) |
| 710 | key_free(key); |
| 711 | xfree(pkalg); |
| 712 | xfree(pkblob); |
| 713 | |
| 714 | /* unregister */ |
| 715 | clear_auth_state(authctxt); |
| 716 | dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL); |
| 717 | |
| 718 | /* try another method if we did not send a packet*/ |
| 719 | if (sent == 0) |
| 720 | userauth(authctxt, NULL); |
| 721 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 722 | } |
| 723 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 724 | int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 725 | userauth_none(Authctxt *authctxt) |
| 726 | { |
| 727 | /* initial userauth request */ |
| 728 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 729 | packet_put_cstring(authctxt->server_user); |
| 730 | packet_put_cstring(authctxt->service); |
| 731 | packet_put_cstring(authctxt->method->name); |
| 732 | packet_send(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 733 | return 1; |
| 734 | } |
| 735 | |
| 736 | int |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 737 | userauth_passwd(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 738 | { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 739 | static int attempt = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 740 | char prompt[80]; |
| 741 | char *password; |
| 742 | |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 743 | if (attempt++ >= options.number_of_password_prompts) |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 744 | return 0; |
| 745 | |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 746 | if(attempt != 1) |
| 747 | error("Permission denied, please try again."); |
| 748 | |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 749 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 750 | authctxt->server_user, authctxt->host); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 751 | password = read_passphrase(prompt, 0); |
| 752 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 753 | packet_put_cstring(authctxt->server_user); |
| 754 | packet_put_cstring(authctxt->service); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 755 | packet_put_cstring(authctxt->method->name); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 756 | packet_put_char(0); |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 757 | packet_put_cstring(password); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 758 | memset(password, 0, strlen(password)); |
| 759 | xfree(password); |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 760 | packet_inject_ignore(64); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 761 | packet_send(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 762 | return 1; |
| 763 | } |
| 764 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 765 | void |
| 766 | clear_auth_state(Authctxt *authctxt) |
| 767 | { |
| 768 | /* XXX clear authentication state */ |
| 769 | if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) { |
| 770 | debug3("clear_auth_state: key_free %p", authctxt->last_key); |
| 771 | key_free(authctxt->last_key); |
| 772 | } |
| 773 | authctxt->last_key = NULL; |
| 774 | authctxt->last_key_hint = -2; |
| 775 | authctxt->last_key_sign = NULL; |
| 776 | } |
| 777 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 778 | int |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 779 | sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 780 | { |
| 781 | Buffer b; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 782 | u_char *blob, *signature; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 783 | int bloblen, slen; |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 784 | int skip = 0; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 785 | int ret = -1; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 786 | int have_sig = 1; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 787 | |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 788 | debug3("sign_and_send_pubkey"); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 789 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 790 | if (key_to_blob(k, &blob, &bloblen) == 0) { |
| 791 | /* we cannot handle this key */ |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 792 | debug3("sign_and_send_pubkey: cannot handle key"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 793 | return 0; |
| 794 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 795 | /* data to be signed */ |
| 796 | buffer_init(&b); |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 797 | if (datafellows & SSH_OLD_SESSIONID) { |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 798 | buffer_append(&b, session_id2, session_id2_len); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 799 | skip = session_id2_len; |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 800 | } else { |
| 801 | buffer_put_string(&b, session_id2, session_id2_len); |
| 802 | skip = buffer_len(&b); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 803 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 804 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 805 | buffer_put_cstring(&b, authctxt->server_user); |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 806 | buffer_put_cstring(&b, |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 807 | datafellows & SSH_BUG_PKSERVICE ? |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 808 | "ssh-userauth" : |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 809 | authctxt->service); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 810 | if (datafellows & SSH_BUG_PKAUTH) { |
| 811 | buffer_put_char(&b, have_sig); |
| 812 | } else { |
| 813 | buffer_put_cstring(&b, authctxt->method->name); |
| 814 | buffer_put_char(&b, have_sig); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 815 | buffer_put_cstring(&b, key_ssh_name(k)); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 816 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 817 | buffer_put_string(&b, blob, bloblen); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 818 | |
| 819 | /* generate signature */ |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 820 | ret = (*sign_callback)(authctxt, k, &signature, &slen, |
| 821 | buffer_ptr(&b), buffer_len(&b)); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 822 | if (ret == -1) { |
| 823 | xfree(blob); |
| 824 | buffer_free(&b); |
| 825 | return 0; |
| 826 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 827 | #ifdef DEBUG_PK |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 828 | buffer_dump(&b); |
| 829 | #endif |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 830 | if (datafellows & SSH_BUG_PKSERVICE) { |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 831 | buffer_clear(&b); |
| 832 | buffer_append(&b, session_id2, session_id2_len); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 833 | skip = session_id2_len; |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 834 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 835 | buffer_put_cstring(&b, authctxt->server_user); |
| 836 | buffer_put_cstring(&b, authctxt->service); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 837 | buffer_put_cstring(&b, authctxt->method->name); |
| 838 | buffer_put_char(&b, have_sig); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 839 | if (!(datafellows & SSH_BUG_PKAUTH)) |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 840 | buffer_put_cstring(&b, key_ssh_name(k)); |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 841 | buffer_put_string(&b, blob, bloblen); |
| 842 | } |
| 843 | xfree(blob); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 844 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 845 | /* append signature */ |
| 846 | buffer_put_string(&b, signature, slen); |
| 847 | xfree(signature); |
| 848 | |
| 849 | /* skip session id and packet type */ |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 850 | if (buffer_len(&b) < skip + 1) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 851 | fatal("userauth_pubkey: internal error"); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 852 | buffer_consume(&b, skip + 1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 853 | |
| 854 | /* put remaining data from buffer into packet */ |
| 855 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 856 | packet_put_raw(buffer_ptr(&b), buffer_len(&b)); |
| 857 | buffer_free(&b); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 858 | packet_send(); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 859 | |
| 860 | return 1; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | int |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 864 | send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback, |
| 865 | int hint) |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 866 | { |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 867 | u_char *blob; |
| 868 | int bloblen, have_sig = 0; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 869 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 870 | debug3("send_pubkey_test"); |
| 871 | |
| 872 | if (key_to_blob(k, &blob, &bloblen) == 0) { |
| 873 | /* we cannot handle this key */ |
| 874 | debug3("send_pubkey_test: cannot handle key"); |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 875 | return 0; |
| 876 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 877 | /* register callback for USERAUTH_PK_OK message */ |
| 878 | authctxt->last_key_sign = sign_callback; |
| 879 | authctxt->last_key_hint = hint; |
| 880 | authctxt->last_key = k; |
| 881 | dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok); |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 882 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 883 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 884 | packet_put_cstring(authctxt->server_user); |
| 885 | packet_put_cstring(authctxt->service); |
| 886 | packet_put_cstring(authctxt->method->name); |
| 887 | packet_put_char(have_sig); |
| 888 | if (!(datafellows & SSH_BUG_PKAUTH)) |
| 889 | packet_put_cstring(key_ssh_name(k)); |
| 890 | packet_put_string(blob, bloblen); |
| 891 | xfree(blob); |
| 892 | packet_send(); |
| 893 | return 1; |
| 894 | } |
| 895 | |
| 896 | Key * |
| 897 | load_identity_file(char *filename) |
| 898 | { |
| 899 | Key *private; |
| 900 | char prompt[300], *passphrase; |
| 901 | int success = 0, quit, i; |
Ben Lindstrom | 329782e | 2001-03-10 17:08:59 +0000 | [diff] [blame] | 902 | struct stat st; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 903 | |
Ben Lindstrom | 329782e | 2001-03-10 17:08:59 +0000 | [diff] [blame] | 904 | if (stat(filename, &st) < 0) { |
| 905 | debug3("no such identity: %s", filename); |
| 906 | return NULL; |
| 907 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 908 | private = key_new(KEY_UNSPEC); |
| 909 | if (!load_private_key(filename, "", private, NULL)) { |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 910 | if (options.batch_mode) { |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 911 | key_free(private); |
| 912 | return NULL; |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 913 | } |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 914 | snprintf(prompt, sizeof prompt, |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 915 | "Enter passphrase for key '%.100s': ", filename); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 916 | for (i = 0; i < options.number_of_password_prompts; i++) { |
| 917 | passphrase = read_passphrase(prompt, 0); |
| 918 | if (strcmp(passphrase, "") != 0) { |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 919 | success = load_private_key(filename, |
| 920 | passphrase, private, NULL); |
| 921 | quit = 0; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 922 | } else { |
| 923 | debug2("no passphrase given, try next key"); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 924 | quit = 1; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 925 | } |
| 926 | memset(passphrase, 0, strlen(passphrase)); |
| 927 | xfree(passphrase); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 928 | if (success || quit) |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 929 | break; |
| 930 | debug2("bad passphrase given, try again..."); |
| 931 | } |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 932 | if (!success) { |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 933 | key_free(private); |
| 934 | return NULL; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 935 | } |
| 936 | } |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 937 | return private; |
| 938 | } |
| 939 | |
| 940 | int |
| 941 | identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp, |
| 942 | u_char *data, int datalen) |
| 943 | { |
| 944 | Key *private; |
| 945 | int idx, ret; |
| 946 | |
| 947 | idx = authctxt->last_key_hint; |
| 948 | if (idx < 0) |
| 949 | return -1; |
| 950 | private = load_identity_file(options.identity_files[idx]); |
| 951 | if (private == NULL) |
| 952 | return -1; |
| 953 | ret = key_sign(private, sigp, lenp, data, datalen); |
| 954 | key_free(private); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 955 | return ret; |
| 956 | } |
| 957 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 958 | int agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp, |
| 959 | u_char *data, int datalen) |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 960 | { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 961 | return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 962 | } |
| 963 | |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 964 | int key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp, |
| 965 | u_char *data, int datalen) |
| 966 | { |
| 967 | return key_sign(key, sigp, lenp, data, datalen); |
| 968 | } |
| 969 | |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 970 | int |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 971 | userauth_pubkey_agent(Authctxt *authctxt) |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 972 | { |
| 973 | static int called = 0; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 974 | int ret = 0; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 975 | char *comment; |
| 976 | Key *k; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 977 | |
| 978 | if (called == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 979 | if (ssh_get_num_identities(authctxt->agent, 2) == 0) |
| 980 | debug2("userauth_pubkey_agent: no keys at all"); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 981 | called = 1; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 982 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 983 | k = ssh_get_next_identity(authctxt->agent, &comment, 2); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 984 | if (k == NULL) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 985 | debug2("userauth_pubkey_agent: no more keys"); |
| 986 | } else { |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 987 | debug("userauth_pubkey_agent: testing agent key %s", comment); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 988 | xfree(comment); |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 989 | ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1); |
| 990 | if (ret == 0) |
| 991 | key_free(k); |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 992 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 993 | if (ret == 0) |
| 994 | debug2("userauth_pubkey_agent: no message sent"); |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 995 | return ret; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 996 | } |
| 997 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 998 | int |
| 999 | userauth_pubkey(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1000 | { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1001 | static int idx = 0; |
| 1002 | int sent = 0; |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1003 | Key *key; |
| 1004 | char *filename; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1005 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1006 | if (authctxt->agent != NULL) { |
| 1007 | do { |
| 1008 | sent = userauth_pubkey_agent(authctxt); |
| 1009 | } while(!sent && authctxt->agent->howmany > 0); |
| 1010 | } |
| 1011 | while (!sent && idx < options.num_identity_files) { |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 1012 | key = options.identity_keys[idx]; |
| 1013 | filename = options.identity_files[idx]; |
| 1014 | if (key == NULL) { |
| 1015 | debug("try privkey: %s", filename); |
| 1016 | key = load_identity_file(filename); |
| 1017 | if (key != NULL) { |
| 1018 | sent = sign_and_send_pubkey(authctxt, key, |
| 1019 | key_sign_cb); |
| 1020 | key_free(key); |
| 1021 | } |
| 1022 | } else if (key->type != KEY_RSA1) { |
| 1023 | debug("try pubkey: %s", filename); |
| 1024 | sent = send_pubkey_test(authctxt, key, |
| 1025 | identity_sign_cb, idx); |
| 1026 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1027 | idx++; |
| 1028 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1029 | return sent; |
| 1030 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1031 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1032 | /* |
| 1033 | * Send userauth request message specifying keyboard-interactive method. |
| 1034 | */ |
| 1035 | int |
| 1036 | userauth_kbdint(Authctxt *authctxt) |
| 1037 | { |
| 1038 | static int attempt = 0; |
| 1039 | |
| 1040 | if (attempt++ >= options.number_of_password_prompts) |
| 1041 | return 0; |
| 1042 | |
| 1043 | debug2("userauth_kbdint"); |
| 1044 | packet_start(SSH2_MSG_USERAUTH_REQUEST); |
| 1045 | packet_put_cstring(authctxt->server_user); |
| 1046 | packet_put_cstring(authctxt->service); |
| 1047 | packet_put_cstring(authctxt->method->name); |
| 1048 | packet_put_cstring(""); /* lang */ |
| 1049 | packet_put_cstring(options.kbd_interactive_devices ? |
| 1050 | options.kbd_interactive_devices : ""); |
| 1051 | packet_send(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1052 | |
| 1053 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req); |
| 1054 | return 1; |
| 1055 | } |
| 1056 | |
| 1057 | /* |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 1058 | * parse INFO_REQUEST, prompt user and send INFO_RESPONSE |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1059 | */ |
| 1060 | void |
| 1061 | input_userauth_info_req(int type, int plen, void *ctxt) |
| 1062 | { |
| 1063 | Authctxt *authctxt = ctxt; |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 1064 | char *name, *inst, *lang, *prompt, *response; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 1065 | u_int num_prompts, i; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1066 | int echo = 0; |
| 1067 | |
| 1068 | debug2("input_userauth_info_req"); |
| 1069 | |
| 1070 | if (authctxt == NULL) |
| 1071 | fatal("input_userauth_info_req: no authentication context"); |
| 1072 | |
| 1073 | name = packet_get_string(NULL); |
| 1074 | inst = packet_get_string(NULL); |
| 1075 | lang = packet_get_string(NULL); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1076 | if (strlen(name) > 0) |
| 1077 | cli_mesg(name); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1078 | if (strlen(inst) > 0) |
| 1079 | cli_mesg(inst); |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 1080 | xfree(name); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1081 | xfree(inst); |
Ben Lindstrom | 03df5bd | 2001-02-10 22:16:41 +0000 | [diff] [blame] | 1082 | xfree(lang); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1083 | |
| 1084 | num_prompts = packet_get_int(); |
| 1085 | /* |
| 1086 | * Begin to build info response packet based on prompts requested. |
| 1087 | * We commit to providing the correct number of responses, so if |
| 1088 | * further on we run into a problem that prevents this, we have to |
| 1089 | * be sure and clean this up and send a correct error response. |
| 1090 | */ |
| 1091 | packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE); |
| 1092 | packet_put_int(num_prompts); |
| 1093 | |
| 1094 | for (i = 0; i < num_prompts; i++) { |
| 1095 | prompt = packet_get_string(NULL); |
| 1096 | echo = packet_get_char(); |
| 1097 | |
| 1098 | response = cli_prompt(prompt, echo); |
| 1099 | |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1100 | packet_put_cstring(response); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1101 | memset(response, 0, strlen(response)); |
| 1102 | xfree(response); |
| 1103 | xfree(prompt); |
| 1104 | } |
| 1105 | packet_done(); /* done with parsing incoming message. */ |
| 1106 | |
Ben Lindstrom | 5699c5f | 2001-03-05 06:17:49 +0000 | [diff] [blame] | 1107 | packet_inject_ignore(64); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1108 | packet_send(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1109 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1110 | |
| 1111 | /* find auth method */ |
| 1112 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1113 | /* |
| 1114 | * given auth method name, if configurable options permit this method fill |
| 1115 | * in auth_ident field and return true, otherwise return false. |
| 1116 | */ |
| 1117 | int |
| 1118 | authmethod_is_enabled(Authmethod *method) |
| 1119 | { |
| 1120 | if (method == NULL) |
| 1121 | return 0; |
| 1122 | /* return false if options indicate this method is disabled */ |
| 1123 | if (method->enabled == NULL || *method->enabled == 0) |
| 1124 | return 0; |
| 1125 | /* return false if batch mode is enabled but method needs interactive mode */ |
| 1126 | if (method->batch_flag != NULL && *method->batch_flag != 0) |
| 1127 | return 0; |
| 1128 | return 1; |
| 1129 | } |
| 1130 | |
| 1131 | Authmethod * |
| 1132 | authmethod_lookup(const char *name) |
| 1133 | { |
| 1134 | Authmethod *method = NULL; |
| 1135 | if (name != NULL) |
| 1136 | for (method = authmethods; method->name != NULL; method++) |
| 1137 | if (strcmp(name, method->name) == 0) |
| 1138 | return method; |
| 1139 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); |
| 1140 | return NULL; |
| 1141 | } |
| 1142 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1143 | /* XXX internal state */ |
| 1144 | static Authmethod *current = NULL; |
| 1145 | static char *supported = NULL; |
| 1146 | static char *preferred = NULL; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1147 | /* |
| 1148 | * Given the authentication method list sent by the server, return the |
| 1149 | * next method we should try. If the server initially sends a nil list, |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1150 | * use a built-in default list. |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1151 | */ |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1152 | Authmethod * |
| 1153 | authmethod_get(char *authlist) |
| 1154 | { |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1155 | |
| 1156 | char *name = NULL; |
| 1157 | int next; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 1158 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1159 | /* Use a suitable default if we're passed a nil list. */ |
| 1160 | if (authlist == NULL || strlen(authlist) == 0) |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1161 | authlist = options.preferred_authentications; |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1162 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1163 | if (supported == NULL || strcmp(authlist, supported) != 0) { |
| 1164 | debug3("start over, passed a different list %s", authlist); |
| 1165 | if (supported != NULL) |
| 1166 | xfree(supported); |
| 1167 | supported = xstrdup(authlist); |
| 1168 | preferred = options.preferred_authentications; |
| 1169 | debug3("preferred %s", preferred); |
| 1170 | current = NULL; |
| 1171 | } else if (current != NULL && authmethod_is_enabled(current)) |
| 1172 | return current; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1173 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1174 | for (;;) { |
| 1175 | if ((name = match_list(preferred, supported, &next)) == NULL) { |
| 1176 | debug("no more auth methods to try"); |
| 1177 | current = NULL; |
| 1178 | return NULL; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 1179 | } |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1180 | preferred += next; |
| 1181 | debug3("authmethod_lookup %s", name); |
| 1182 | debug3("remaining preferred: %s", preferred); |
| 1183 | if ((current = authmethod_lookup(name)) != NULL && |
| 1184 | authmethod_is_enabled(current)) { |
| 1185 | debug3("authmethod_is_enabled %s", name); |
| 1186 | debug("next auth method to try is %s", name); |
| 1187 | return current; |
| 1188 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1189 | } |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1190 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1191 | |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1192 | |
| 1193 | #define DELIM "," |
| 1194 | char * |
| 1195 | authmethods_get(void) |
| 1196 | { |
| 1197 | Authmethod *method = NULL; |
| 1198 | char buf[1024]; |
| 1199 | |
| 1200 | buf[0] = '\0'; |
| 1201 | for (method = authmethods; method->name != NULL; method++) { |
| 1202 | if (authmethod_is_enabled(method)) { |
| 1203 | if (buf[0] != '\0') |
| 1204 | strlcat(buf, DELIM, sizeof buf); |
| 1205 | strlcat(buf, method->name, sizeof buf); |
| 1206 | } |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 1207 | } |
Ben Lindstrom | b9be60a | 2001-03-11 01:49:19 +0000 | [diff] [blame] | 1208 | return xstrdup(buf); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1209 | } |