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