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