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