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