]> andersk Git - openssh.git/blame - sshconnect2.c
- markus@cvs.openbsd.org 2001/02/28 08:54:55
[openssh.git] / sshconnect2.c
CommitLineData
a306f2dd 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.
a306f2dd 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"
0ae4fe1d 26RCSID("$OpenBSD: sshconnect2.c,v 1.48 2001/02/15 23:19:59 markus Exp $");
a306f2dd 27
28#include <openssl/bn.h>
a306f2dd 29#include <openssl/md5.h>
30#include <openssl/dh.h>
31#include <openssl/hmac.h>
32
33#include "ssh.h"
42f11eb2 34#include "ssh2.h"
a306f2dd 35#include "xmalloc.h"
36#include "rsa.h"
37#include "buffer.h"
38#include "packet.h"
a306f2dd 39#include "uidswap.h"
40#include "compat.h"
a306f2dd 41#include "bufaux.h"
42f11eb2 42#include "cipher.h"
a306f2dd 43#include "kex.h"
44#include "myproposal.h"
45#include "key.h"
a306f2dd 46#include "sshconnect.h"
47#include "authfile.h"
94ec8c6b 48#include "cli.h"
188adeb2 49#include "dispatch.h"
2e73a022 50#include "authfd.h"
42f11eb2 51#include "log.h"
52#include "readconf.h"
53#include "readpass.h"
a306f2dd 54
94ec8c6b 55void ssh_dh1_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *);
56void ssh_dhgex_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *);
57
a306f2dd 58/* import */
59extern char *client_version_string;
60extern char *server_version_string;
61extern Options options;
62
63/*
64 * SSH2 key exchange
65 */
66
1e3b8b07 67u_char *session_id2 = NULL;
a306f2dd 68int session_id2_len = 0;
69
70void
94ec8c6b 71ssh_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
c523303b 78 if (options.ciphers == (char *)-1) {
79 log("No valid ciphers for protocol version 2 given, using defaults.");
80 options.ciphers = NULL;
94ec8c6b 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) {
b2552997 87 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
94ec8c6b 88 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
89 } else {
b2552997 90 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
94ec8c6b 91 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
92 }
b2552997 93 if (options.macs != NULL) {
94 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
95 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
96 }
94ec8c6b 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
151void
2b87da3b 152ssh_dh1_client(Kex *kex, char *host, struct sockaddr *hostaddr,
94ec8c6b 153 Buffer *client_kexinit, Buffer *server_kexinit)
a306f2dd 154{
188adeb2 155#ifdef DEBUG_KEXDH
156 int i;
157#endif
71276795 158 int plen, dlen;
1e3b8b07 159 u_int klen, kout;
a306f2dd 160 char *signature = NULL;
1e3b8b07 161 u_int slen;
a306f2dd 162 char *server_host_key_blob = NULL;
163 Key *server_host_key;
1e3b8b07 164 u_int sbloblen;
a306f2dd 165 DH *dh;
166 BIGNUM *dh_server_pub = 0;
167 BIGNUM *shared_secret = 0;
1e3b8b07 168 u_char *kbuf;
169 u_char *hash;
a306f2dd 170
a306f2dd 171 debug("Sending SSH2_MSG_KEXDH_INIT.");
a306f2dd 172 /* generate and send 'e', client DH public key */
173 dh = dh_new_group1();
3e1caa83 174 dh_gen_key(dh);
a306f2dd 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= ");
188adeb2 182 BN_print_fp(stderr, dh->p);
a306f2dd 183 fprintf(stderr, "\ng= ");
188adeb2 184 BN_print_fp(stderr, dh->g);
a306f2dd 185 fprintf(stderr, "\npub= ");
188adeb2 186 BN_print_fp(stderr, dh->pub_key);
a306f2dd 187 fprintf(stderr, "\n");
188 DHparams_print_fp(stderr, dh);
189#endif
190
191 debug("Wait SSH2_MSG_KEXDH_REPLY.");
192
71276795 193 packet_read_expect(&plen, SSH2_MSG_KEXDH_REPLY);
a306f2dd 194
195 debug("Got SSH2_MSG_KEXDH_REPLY.");
196
197 /* key, cert */
198 server_host_key_blob = packet_get_string(&sbloblen);
fa08c86b 199 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
a306f2dd 200 if (server_host_key == NULL)
201 fatal("cannot decode server_host_key_blob");
202
203 check_host_key(host, hostaddr, server_host_key,
94ec8c6b 204 options.user_hostfile2, options.system_hostfile2);
a306f2dd 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= ");
188adeb2 214 BN_print_fp(stderr, dh_server_pub);
a306f2dd 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);
71276795 254 DH_free(dh);
53a24016 255 BN_free(dh_server_pub);
a306f2dd 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
1e3b8b07 262 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
fa08c86b 263 fatal("key_verify failed for server_host_key");
a306f2dd 264 key_free(server_host_key);
53a24016 265 xfree(signature);
a306f2dd 266
267 kex_derive_keys(kex, hash, shared_secret);
53a24016 268 BN_clear_free(shared_secret);
a306f2dd 269 packet_set_kex(kex);
270
a306f2dd 271 /* save session id */
272 session_id2_len = 20;
273 session_id2 = xmalloc(session_id2_len);
274 memcpy(session_id2, hash, session_id2_len);
71276795 275}
276
94ec8c6b 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
285int
286dh_estimate(int bits)
287{
2b87da3b 288
94ec8c6b 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}
297
71276795 298void
94ec8c6b 299ssh_dhgex_client(Kex *kex, char *host, struct sockaddr *hostaddr,
300 Buffer *client_kexinit, Buffer *server_kexinit)
71276795 301{
94ec8c6b 302#ifdef DEBUG_KEXDH
303 int i;
304#endif
305 int plen, dlen;
1e3b8b07 306 u_int klen, kout;
94ec8c6b 307 char *signature = NULL;
1e3b8b07 308 u_int slen, nbits;
94ec8c6b 309 char *server_host_key_blob = NULL;
310 Key *server_host_key;
1e3b8b07 311 u_int sbloblen;
94ec8c6b 312 DH *dh;
313 BIGNUM *dh_server_pub = 0;
314 BIGNUM *shared_secret = 0;
315 BIGNUM *p = 0, *g = 0;
1e3b8b07 316 u_char *kbuf;
317 u_char *hash;
71276795 318
94ec8c6b 319 nbits = dh_estimate(kex->enc[MODE_OUT].cipher->key_len * 8);
71276795 320
94ec8c6b 321 debug("Sending SSH2_MSG_KEX_DH_GEX_REQUEST.");
322 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
323 packet_put_int(nbits);
324 packet_send();
325 packet_write_wait();
71276795 326
94ec8c6b 327#ifdef DEBUG_KEXDH
328 fprintf(stderr, "\nnbits = %d", nbits);
329#endif
71276795 330
94ec8c6b 331 debug("Wait SSH2_MSG_KEX_DH_GEX_GROUP.");
71276795 332
94ec8c6b 333 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_GROUP);
a306f2dd 334
94ec8c6b 335 debug("Got SSH2_MSG_KEX_DH_GEX_GROUP.");
a306f2dd 336
94ec8c6b 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);
42f11eb2 343 dh = dh_new_group(g, p);
a306f2dd 344
3e1caa83 345 dh_gen_key(dh);
346
a306f2dd 347#ifdef DEBUG_KEXDH
94ec8c6b 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);
a306f2dd 362 packet_send();
363 packet_write_wait();
94ec8c6b 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);
fa08c86b 373 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
94ec8c6b 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));
a306f2dd 391#endif
94ec8c6b 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,
2b87da3b 423 nbits, dh->p, dh->g,
94ec8c6b 424 dh->pub_key,
425 dh_server_pub,
426 shared_secret
427 );
428 xfree(server_host_key_blob);
429 DH_free(dh);
53a24016 430 BN_free(dh_server_pub);
94ec8c6b 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
1e3b8b07 437 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
fa08c86b 438 fatal("key_verify failed for server_host_key");
94ec8c6b 439 key_free(server_host_key);
53a24016 440 xfree(signature);
94ec8c6b 441
442 kex_derive_keys(kex, hash, shared_secret);
53a24016 443 BN_clear_free(shared_secret);
94ec8c6b 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);
a306f2dd 450}
71276795 451
a306f2dd 452/*
453 * Authenticate user
454 */
188adeb2 455
456typedef struct Authctxt Authctxt;
457typedef struct Authmethod Authmethod;
458
459typedef int sign_cb_fn(
460 Authctxt *authctxt, Key *key,
1e3b8b07 461 u_char **sigp, int *lenp, u_char *data, int datalen);
188adeb2 462
463struct Authctxt {
464 const char *server_user;
465 const char *host;
466 const char *service;
467 AuthenticationConnection *agent;
188adeb2 468 Authmethod *method;
94ec8c6b 469 int success;
188adeb2 470};
471struct 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
478void input_userauth_success(int type, int plen, void *ctxt);
479void input_userauth_failure(int type, int plen, void *ctxt);
9616313f 480void input_userauth_banner(int type, int plen, void *ctxt);
188adeb2 481void input_userauth_error(int type, int plen, void *ctxt);
94ec8c6b 482void input_userauth_info_req(int type, int plen, void *ctxt);
483
484int userauth_none(Authctxt *authctxt);
188adeb2 485int userauth_pubkey(Authctxt *authctxt);
486int userauth_passwd(Authctxt *authctxt);
94ec8c6b 487int userauth_kbdint(Authctxt *authctxt);
188adeb2 488
1e3b8b07 489void authmethod_clear(void);
94ec8c6b 490Authmethod *authmethod_get(char *authlist);
491Authmethod *authmethod_lookup(const char *name);
188adeb2 492
493Authmethod authmethods[] = {
494 {"publickey",
495 userauth_pubkey,
fa08c86b 496 &options.pubkey_authentication,
188adeb2 497 NULL},
498 {"password",
499 userauth_passwd,
500 &options.password_authentication,
501 &options.batch_mode},
94ec8c6b 502 {"keyboard-interactive",
503 userauth_kbdint,
504 &options.kbd_interactive_authentication,
505 &options.batch_mode},
506 {"none",
507 userauth_none,
508 NULL,
509 NULL},
188adeb2 510 {NULL, NULL, NULL, NULL}
511};
512
513void
514ssh_userauth2(const char *server_user, char *host)
515{
516 Authctxt authctxt;
517 int type;
518 int plen;
519
d464095c 520 if (options.challenge_reponse_authentication)
521 options.kbd_interactive_authentication = 1;
522
188adeb2 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);
188adeb2 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;
94ec8c6b 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();
188adeb2 552
553 /* initial userauth request */
94ec8c6b 554 userauth_none(&authctxt);
188adeb2 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);
9616313f 559 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
188adeb2 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
8abcdba4 565 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
188adeb2 566}
567void
568input_userauth_error(int type, int plen, void *ctxt)
569{
9616313f 570 fatal("input_userauth_error: bad message during authentication: "
571 "type %d", type);
572}
573void
574input_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);
188adeb2 583}
584void
585input_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}
592void
593input_userauth_failure(int type, int plen, void *ctxt)
594{
595 Authmethod *method = NULL;
596 Authctxt *authctxt = ctxt;
597 char *authlist = NULL;
598 int partial;
188adeb2 599
600 if (authctxt == NULL)
601 fatal("input_userauth_failure: no authentication context");
602
94ec8c6b 603 authlist = packet_get_string(NULL);
188adeb2 604 partial = packet_get_char();
605 packet_done();
606
607 if (partial != 0)
f72e01a5 608 log("Authenticated with partial success.");
188adeb2 609 debug("authentications that can continue: %s", authlist);
610
611 for (;;) {
188adeb2 612 method = authmethod_get(authlist);
613 if (method == NULL)
f72e01a5 614 fatal("Permission denied (%s).", authlist);
94ec8c6b 615 authctxt->method = method;
188adeb2 616 if (method->userauth(authctxt) != 0) {
94ec8c6b 617 debug2("we sent a %s packet, wait for reply", method->name);
188adeb2 618 break;
619 } else {
620 debug2("we did not send a packet, disable method");
621 method->enabled = NULL;
622 }
2b87da3b 623 }
188adeb2 624 xfree(authlist);
625}
626
94ec8c6b 627int
628userauth_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
a306f2dd 640int
188adeb2 641userauth_passwd(Authctxt *authctxt)
a306f2dd 642{
1d1ffb87 643 static int attempt = 0;
a306f2dd 644 char prompt[80];
645 char *password;
646
fa649821 647 if (attempt++ >= options.number_of_password_prompts)
1d1ffb87 648 return 0;
649
fa649821 650 if(attempt != 1)
651 error("Permission denied, please try again.");
652
f72e01a5 653 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
188adeb2 654 authctxt->server_user, authctxt->host);
a306f2dd 655 password = read_passphrase(prompt, 0);
656 packet_start(SSH2_MSG_USERAUTH_REQUEST);
188adeb2 657 packet_put_cstring(authctxt->server_user);
658 packet_put_cstring(authctxt->service);
94ec8c6b 659 packet_put_cstring(authctxt->method->name);
a306f2dd 660 packet_put_char(0);
0ae4fe1d 661 ssh_put_password(password);
a306f2dd 662 memset(password, 0, strlen(password));
663 xfree(password);
664 packet_send();
665 packet_write_wait();
666 return 1;
667}
668
2e73a022 669int
188adeb2 670sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
a306f2dd 671{
672 Buffer b;
1e3b8b07 673 u_char *blob, *signature;
a306f2dd 674 int bloblen, slen;
74fc9186 675 int skip = 0;
2e73a022 676 int ret = -1;
94ec8c6b 677 int have_sig = 1;
a306f2dd 678
cbc5abf9 679 debug3("sign_and_send_pubkey");
fa08c86b 680 if (key_to_blob(k, &blob, &bloblen) == 0) {
681 /* we cannot handle this key */
cbc5abf9 682 debug3("sign_and_send_pubkey: cannot handle key");
fa08c86b 683 return 0;
684 }
a306f2dd 685 /* data to be signed */
686 buffer_init(&b);
33de75a3 687 if (datafellows & SSH_OLD_SESSIONID) {
74fc9186 688 buffer_append(&b, session_id2, session_id2_len);
2b87da3b 689 skip = session_id2_len;
33de75a3 690 } else {
691 buffer_put_string(&b, session_id2, session_id2_len);
692 skip = buffer_len(&b);
74fc9186 693 }
a306f2dd 694 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
188adeb2 695 buffer_put_cstring(&b, authctxt->server_user);
d0c832f3 696 buffer_put_cstring(&b,
cbc5abf9 697 datafellows & SSH_BUG_PKSERVICE ?
d0c832f3 698 "ssh-userauth" :
188adeb2 699 authctxt->service);
cbc5abf9 700 if (datafellows & SSH_BUG_PKAUTH) {
701 buffer_put_char(&b, have_sig);
702 } else {
703 buffer_put_cstring(&b, authctxt->method->name);
704 buffer_put_char(&b, have_sig);
2b87da3b 705 buffer_put_cstring(&b, key_ssh_name(k));
cbc5abf9 706 }
a306f2dd 707 buffer_put_string(&b, blob, bloblen);
a306f2dd 708
709 /* generate signature */
188adeb2 710 ret = (*sign_callback)(authctxt, k, &signature, &slen, buffer_ptr(&b), buffer_len(&b));
2e73a022 711 if (ret == -1) {
712 xfree(blob);
713 buffer_free(&b);
714 return 0;
715 }
fa08c86b 716#ifdef DEBUG_PK
a306f2dd 717 buffer_dump(&b);
718#endif
cbc5abf9 719 if (datafellows & SSH_BUG_PKSERVICE) {
d0c832f3 720 buffer_clear(&b);
721 buffer_append(&b, session_id2, session_id2_len);
722 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
188adeb2 723 buffer_put_cstring(&b, authctxt->server_user);
724 buffer_put_cstring(&b, authctxt->service);
94ec8c6b 725 buffer_put_cstring(&b, authctxt->method->name);
726 buffer_put_char(&b, have_sig);
cbc5abf9 727 if (!(datafellows & SSH_BUG_PKAUTH))
2b87da3b 728 buffer_put_cstring(&b, key_ssh_name(k));
d0c832f3 729 buffer_put_string(&b, blob, bloblen);
730 }
731 xfree(blob);
a306f2dd 732 /* append signature */
733 buffer_put_string(&b, signature, slen);
734 xfree(signature);
735
736 /* skip session id and packet type */
74fc9186 737 if (buffer_len(&b) < skip + 1)
188adeb2 738 fatal("userauth_pubkey: internal error");
74fc9186 739 buffer_consume(&b, skip + 1);
a306f2dd 740
741 /* put remaining data from buffer into packet */
742 packet_start(SSH2_MSG_USERAUTH_REQUEST);
743 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
744 buffer_free(&b);
745
746 /* send */
747 packet_send();
748 packet_write_wait();
2e73a022 749
750 return 1;
4c8722d9 751}
752
188adeb2 753/* sign callback */
1e3b8b07 754int key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
755 u_char *data, int datalen)
188adeb2 756{
fa08c86b 757 return key_sign(key, sigp, lenp, data, datalen);
188adeb2 758}
759
4c8722d9 760int
188adeb2 761userauth_pubkey_identity(Authctxt *authctxt, char *filename)
4c8722d9 762{
763 Key *k;
f72e01a5 764 int i, ret, try_next, success = 0;
4c8722d9 765 struct stat st;
f72e01a5 766 char *passphrase;
767 char prompt[300];
4c8722d9 768
769 if (stat(filename, &st) != 0) {
770 debug("key does not exist: %s", filename);
771 return 0;
772 }
773 debug("try pubkey: %s", filename);
774
fa08c86b 775 k = key_new(KEY_UNSPEC);
4c8722d9 776 if (!load_private_key(filename, "", k, NULL)) {
f72e01a5 777 if (options.batch_mode) {
778 key_free(k);
779 return 0;
780 }
4c8722d9 781 snprintf(prompt, sizeof prompt,
fa08c86b 782 "Enter passphrase for key '%.100s': ", filename);
188adeb2 783 for (i = 0; i < options.number_of_password_prompts; i++) {
784 passphrase = read_passphrase(prompt, 0);
785 if (strcmp(passphrase, "") != 0) {
786 success = load_private_key(filename, passphrase, k, NULL);
787 try_next = 0;
788 } else {
789 debug2("no passphrase given, try next key");
790 try_next = 1;
791 }
792 memset(passphrase, 0, strlen(passphrase));
793 xfree(passphrase);
794 if (success || try_next)
795 break;
796 debug2("bad passphrase given, try again...");
797 }
4c8722d9 798 if (!success) {
799 key_free(k);
800 return 0;
801 }
802 }
fa08c86b 803 ret = sign_and_send_pubkey(authctxt, k, key_sign_cb);
2e73a022 804 key_free(k);
805 return ret;
806}
807
188adeb2 808/* sign callback */
1e3b8b07 809int agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
810 u_char *data, int datalen)
2e73a022 811{
188adeb2 812 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
2e73a022 813}
814
815int
188adeb2 816userauth_pubkey_agent(Authctxt *authctxt)
2e73a022 817{
818 static int called = 0;
fa08c86b 819 int ret = 0;
2e73a022 820 char *comment;
821 Key *k;
2e73a022 822
823 if (called == 0) {
fa08c86b 824 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
825 debug2("userauth_pubkey_agent: no keys at all");
188adeb2 826 called = 1;
2e73a022 827 }
fa08c86b 828 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
188adeb2 829 if (k == NULL) {
fa08c86b 830 debug2("userauth_pubkey_agent: no more keys");
831 } else {
832 debug("userauth_pubkey_agent: trying agent key %s", comment);
833 xfree(comment);
834 ret = sign_and_send_pubkey(authctxt, k, agent_sign_cb);
835 key_free(k);
188adeb2 836 }
fa08c86b 837 if (ret == 0)
838 debug2("userauth_pubkey_agent: no message sent");
2e73a022 839 return ret;
a306f2dd 840}
841
188adeb2 842int
843userauth_pubkey(Authctxt *authctxt)
a306f2dd 844{
188adeb2 845 static int idx = 0;
846 int sent = 0;
847
fa08c86b 848 if (authctxt->agent != NULL) {
849 do {
850 sent = userauth_pubkey_agent(authctxt);
851 } while(!sent && authctxt->agent->howmany > 0);
852 }
853 while (!sent && idx < options.num_identity_files) {
854 if (options.identity_files_type[idx] != KEY_RSA1)
855 sent = userauth_pubkey_identity(authctxt,
856 options.identity_files[idx]);
857 idx++;
858 }
188adeb2 859 return sent;
860}
a306f2dd 861
94ec8c6b 862/*
863 * Send userauth request message specifying keyboard-interactive method.
864 */
865int
866userauth_kbdint(Authctxt *authctxt)
867{
868 static int attempt = 0;
869
870 if (attempt++ >= options.number_of_password_prompts)
871 return 0;
872
873 debug2("userauth_kbdint");
874 packet_start(SSH2_MSG_USERAUTH_REQUEST);
875 packet_put_cstring(authctxt->server_user);
876 packet_put_cstring(authctxt->service);
877 packet_put_cstring(authctxt->method->name);
878 packet_put_cstring(""); /* lang */
879 packet_put_cstring(options.kbd_interactive_devices ?
880 options.kbd_interactive_devices : "");
881 packet_send();
882 packet_write_wait();
883
884 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
885 return 1;
886}
887
888/*
f72e01a5 889 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
94ec8c6b 890 */
891void
892input_userauth_info_req(int type, int plen, void *ctxt)
893{
894 Authctxt *authctxt = ctxt;
f72e01a5 895 char *name, *inst, *lang, *prompt, *response;
1e3b8b07 896 u_int num_prompts, i;
94ec8c6b 897 int echo = 0;
898
899 debug2("input_userauth_info_req");
900
901 if (authctxt == NULL)
902 fatal("input_userauth_info_req: no authentication context");
903
904 name = packet_get_string(NULL);
905 inst = packet_get_string(NULL);
906 lang = packet_get_string(NULL);
94ec8c6b 907 if (strlen(name) > 0)
908 cli_mesg(name);
94ec8c6b 909 if (strlen(inst) > 0)
910 cli_mesg(inst);
f72e01a5 911 xfree(name);
94ec8c6b 912 xfree(inst);
f72e01a5 913 xfree(lang);
94ec8c6b 914
915 num_prompts = packet_get_int();
916 /*
917 * Begin to build info response packet based on prompts requested.
918 * We commit to providing the correct number of responses, so if
919 * further on we run into a problem that prevents this, we have to
920 * be sure and clean this up and send a correct error response.
921 */
922 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
923 packet_put_int(num_prompts);
924
925 for (i = 0; i < num_prompts; i++) {
926 prompt = packet_get_string(NULL);
927 echo = packet_get_char();
928
929 response = cli_prompt(prompt, echo);
930
0ae4fe1d 931 ssh_put_password(response);
94ec8c6b 932 memset(response, 0, strlen(response));
933 xfree(response);
934 xfree(prompt);
935 }
936 packet_done(); /* done with parsing incoming message. */
937
938 packet_send();
939 packet_write_wait();
940}
a306f2dd 941
188adeb2 942/* find auth method */
943
944#define DELIM ","
945
946static char *def_authlist = "publickey,password";
947static char *authlist_current = NULL; /* clean copy used for comparison */
948static char *authname_current = NULL; /* last used auth method */
949static char *authlist_working = NULL; /* copy that gets modified by strtok_r() */
950static char *authlist_state = NULL; /* state variable for strtok_r() */
951
952/*
953 * Before starting to use a new authentication method list sent by the
954 * server, reset internal variables. This should also be called when
955 * finished processing server list to free resources.
956 */
957void
1e3b8b07 958authmethod_clear(void)
188adeb2 959{
960 if (authlist_current != NULL) {
961 xfree(authlist_current);
962 authlist_current = NULL;
a306f2dd 963 }
188adeb2 964 if (authlist_working != NULL) {
965 xfree(authlist_working);
966 authlist_working = NULL;
a306f2dd 967 }
188adeb2 968 if (authname_current != NULL) {
969 xfree(authname_current);
f72e01a5 970 authname_current = NULL;
188adeb2 971 }
972 if (authlist_state != NULL)
973 authlist_state = NULL;
974 return;
975}
a306f2dd 976
188adeb2 977/*
978 * given auth method name, if configurable options permit this method fill
979 * in auth_ident field and return true, otherwise return false.
980 */
981int
982authmethod_is_enabled(Authmethod *method)
983{
984 if (method == NULL)
985 return 0;
986 /* return false if options indicate this method is disabled */
987 if (method->enabled == NULL || *method->enabled == 0)
988 return 0;
989 /* return false if batch mode is enabled but method needs interactive mode */
990 if (method->batch_flag != NULL && *method->batch_flag != 0)
991 return 0;
992 return 1;
993}
a306f2dd 994
188adeb2 995Authmethod *
996authmethod_lookup(const char *name)
997{
998 Authmethod *method = NULL;
999 if (name != NULL)
1000 for (method = authmethods; method->name != NULL; method++)
1001 if (strcmp(name, method->name) == 0)
1002 return method;
1003 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1004 return NULL;
1005}
1006
1007/*
1008 * Given the authentication method list sent by the server, return the
1009 * next method we should try. If the server initially sends a nil list,
1010 * use a built-in default list. If the server sends a nil list after
1011 * previously sending a valid list, continue using the list originally
1012 * sent.
2b87da3b 1013 */
188adeb2 1014
1015Authmethod *
1016authmethod_get(char *authlist)
1017{
a22aff1f 1018 char *name = NULL, *authname_old;
188adeb2 1019 Authmethod *method = NULL;
2b87da3b 1020
188adeb2 1021 /* Use a suitable default if we're passed a nil list. */
1022 if (authlist == NULL || strlen(authlist) == 0)
1023 authlist = def_authlist;
1024
1025 if (authlist_current == NULL || strcmp(authlist, authlist_current) != 0) {
1026 /* start over if passed a different list */
94ec8c6b 1027 debug3("start over, passed a different list");
188adeb2 1028 authmethod_clear();
1029 authlist_current = xstrdup(authlist);
1030 authlist_working = xstrdup(authlist);
1031 name = strtok_r(authlist_working, DELIM, &authlist_state);
1032 } else {
1033 /*
1034 * try to use previously used authentication method
1035 * or continue to use previously passed list
1036 */
1037 name = (authname_current != NULL) ?
1038 authname_current : strtok_r(NULL, DELIM, &authlist_state);
1039 }
1040
1041 while (name != NULL) {
94ec8c6b 1042 debug3("authmethod_lookup %s", name);
188adeb2 1043 method = authmethod_lookup(name);
94ec8c6b 1044 if (method != NULL && authmethod_is_enabled(method)) {
1045 debug3("authmethod_is_enabled %s", name);
a306f2dd 1046 break;
94ec8c6b 1047 }
188adeb2 1048 name = strtok_r(NULL, DELIM, &authlist_state);
94ec8c6b 1049 method = NULL;
188adeb2 1050 }
1051
a22aff1f 1052 authname_old = authname_current;
94ec8c6b 1053 if (method != NULL) {
188adeb2 1054 debug("next auth method to try is %s", name);
1055 authname_current = xstrdup(name);
188adeb2 1056 } else {
1057 debug("no more auth methods to try");
1058 authname_current = NULL;
a306f2dd 1059 }
a22aff1f 1060
1061 if (authname_old != NULL)
1062 xfree(authname_old);
1063
1064 return (method);
a306f2dd 1065}
This page took 0.292513 seconds and 5 git commands to generate.