1 /* $OpenBSD: sshconnect1.c,v 1.70 2006/11/06 21:25:28 markus Exp $ */
2 /*
3  * Author: Tatu Ylonen <ylo@cs.hut.fi>
4  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5  *                    All rights reserved
6  * Code to connect to a remote host, and to perform the client side of the
7  * login (authentication) dialog.
8  *
9  * As far as I am concerned, the code I have written for this software
10  * can be used freely for any purpose.  Any derived versions of this
11  * software must be clearly marked as such, and if the derived work is
12  * incompatible with the protocol description in the RFC file, it must be
13  * called by a name other than "ssh" or "Secure Shell".
14  */
15 
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 
19 #include <openssl/bn.h>
20 #include <md5.h>
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <signal.h>
26 #include <pwd.h>
27 
28 #include "xmalloc.h"
29 #include "ssh.h"
30 #include "ssh1.h"
31 #include "rsa.h"
32 #include "buffer.h"
33 #include "packet.h"
34 #include "key.h"
35 #include "cipher.h"
36 #include "kex.h"
37 #include "uidswap.h"
38 #include "log.h"
39 #include "readconf.h"
40 #include "authfd.h"
41 #include "sshconnect.h"
42 #include "authfile.h"
43 #include "misc.h"
44 #include "canohost.h"
45 #include "hostfile.h"
46 #include "auth.h"
47 
48 __RCSID("$MirOS: src/usr.bin/ssh/sshconnect1.c,v 1.11 2014/03/12 23:35:12 tg Exp $");
49 
50 /* Session id for the current session. */
51 u_char session_id[16];
52 u_int supported_authentications = 0;
53 
54 extern Options options;
55 extern char *__progname;
56 
57 /*
58  * Checks if the user has an authentication agent, and if so, tries to
59  * authenticate using the agent.
60  */
61 static int
try_agent_authentication(void)62 try_agent_authentication(void)
63 {
64 	int type;
65 	char *comment;
66 	AuthenticationConnection *auth;
67 	u_char response[16];
68 	u_int i;
69 	Key *key;
70 	BIGNUM *challenge;
71 
72 	/* Get connection to the agent. */
73 	auth = ssh_get_authentication_connection();
74 	if (!auth)
75 		return 0;
76 
77 	if ((challenge = BN_new()) == NULL)
78 		fatal("try_agent_authentication: BN_new failed");
79 	/* Loop through identities served by the agent. */
80 	for (key = ssh_get_first_identity(auth, &comment, 1);
81 	    key != NULL;
82 	    key = ssh_get_next_identity(auth, &comment, 1)) {
83 
84 		/* Try this identity. */
85 		debug("Trying RSA authentication via agent with '%.100s'", comment);
86 		xfree(comment);
87 
88 		/* Tell the server that we are willing to authenticate using this key. */
89 		packet_start(SSH_CMSG_AUTH_RSA);
90 		packet_put_bignum(key->rsa->n);
91 		packet_send();
92 		packet_write_wait();
93 
94 		/* Wait for server's response. */
95 		type = packet_read();
96 
97 		/* The server sends failure if it doesn't like our key or
98 		   does not support RSA authentication. */
99 		if (type == SSH_SMSG_FAILURE) {
100 			debug("Server refused our key.");
101 			key_free(key);
102 			continue;
103 		}
104 		/* Otherwise it should have sent a challenge. */
105 		if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
106 			packet_disconnect("Protocol error during RSA authentication: %d",
107 					  type);
108 
109 		packet_get_bignum(challenge);
110 		packet_check_eom();
111 
112 		debug("Received RSA challenge from server.");
113 
114 		/* Ask the agent to decrypt the challenge. */
115 		if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
116 			/*
117 			 * The agent failed to authenticate this identifier
118 			 * although it advertised it supports this.  Just
119 			 * return a wrong value.
120 			 */
121 			logit("Authentication agent failed to decrypt challenge.");
122 			memset(response, 0, sizeof(response));
123 		}
124 		key_free(key);
125 		debug("Sending response to RSA challenge.");
126 
127 		/* Send the decrypted challenge back to the server. */
128 		packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
129 		for (i = 0; i < 16; i++)
130 			packet_put_char(response[i]);
131 		packet_send();
132 		packet_write_wait();
133 
134 		/* Wait for response from the server. */
135 		type = packet_read();
136 
137 		/* The server returns success if it accepted the authentication. */
138 		if (type == SSH_SMSG_SUCCESS) {
139 			ssh_close_authentication_connection(auth);
140 			BN_clear_free(challenge);
141 			debug("RSA authentication accepted by server.");
142 			return 1;
143 		}
144 		/* Otherwise it should return failure. */
145 		if (type != SSH_SMSG_FAILURE)
146 			packet_disconnect("Protocol error waiting RSA auth response: %d",
147 					  type);
148 	}
149 	ssh_close_authentication_connection(auth);
150 	BN_clear_free(challenge);
151 	debug("RSA authentication using agent refused.");
152 	return 0;
153 }
154 
155 /*
156  * Computes the proper response to a RSA challenge, and sends the response to
157  * the server.
158  */
159 static void
respond_to_rsa_challenge(BIGNUM * challenge,RSA * prv)160 respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
161 {
162 	u_char buf[32], response[16];
163 	MD5_CTX md;
164 	int i, len;
165 
166 	/* Decrypt the challenge using the private key. */
167 	/* XXX think about Bleichenbacher, too */
168 	if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
169 		packet_disconnect(
170 		    "respond_to_rsa_challenge: rsa_private_decrypt failed");
171 
172 	/* Compute the response. */
173 	/* The response is MD5 of decrypted challenge plus session id. */
174 	len = BN_num_bytes(challenge);
175 	if (len <= 0 || (u_int)len > sizeof(buf))
176 		packet_disconnect(
177 		    "respond_to_rsa_challenge: bad challenge length %d", len);
178 
179 	memset(buf, 0, sizeof(buf));
180 	BN_bn2bin(challenge, buf + sizeof(buf) - len);
181 	MD5Init(&md);
182 	MD5Update(&md, buf, 32);
183 	MD5Update(&md, session_id, 16);
184 	MD5Final(response, &md);
185 
186 	debug("Sending response to host key RSA challenge.");
187 
188 	/* Send the response back to the server. */
189 	packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
190 	for (i = 0; i < 16; i++)
191 		packet_put_char(response[i]);
192 	packet_send();
193 	packet_write_wait();
194 
195 	memset(buf, 0, sizeof(buf));
196 	memset(response, 0, sizeof(response));
197 	memset(&md, 0, sizeof(md));
198 }
199 
200 /*
201  * Checks if the user has authentication file, and if so, tries to authenticate
202  * the user using it.
203  */
204 static int
try_rsa_authentication(int idx)205 try_rsa_authentication(int idx)
206 {
207 	BIGNUM *challenge;
208 	Key *public, *private;
209 	char buf[300], *passphrase, *comment, *authfile;
210 	int i, perm_ok = 1, type, quit;
211 
212 	public = options.identity_keys[idx];
213 	authfile = options.identity_files[idx];
214 	comment = xstrdup(authfile);
215 
216 	debug("Trying RSA authentication with key '%.100s'", comment);
217 
218 	/* Tell the server that we are willing to authenticate using this key. */
219 	packet_start(SSH_CMSG_AUTH_RSA);
220 	packet_put_bignum(public->rsa->n);
221 	packet_send();
222 	packet_write_wait();
223 
224 	/* Wait for server's response. */
225 	type = packet_read();
226 
227 	/*
228 	 * The server responds with failure if it doesn't like our key or
229 	 * doesn't support RSA authentication.
230 	 */
231 	if (type == SSH_SMSG_FAILURE) {
232 		debug("Server refused our key.");
233 		xfree(comment);
234 		return 0;
235 	}
236 	/* Otherwise, the server should respond with a challenge. */
237 	if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
238 		packet_disconnect("Protocol error during RSA authentication: %d", type);
239 
240 	/* Get the challenge from the packet. */
241 	if ((challenge = BN_new()) == NULL)
242 		fatal("try_rsa_authentication: BN_new failed");
243 	packet_get_bignum(challenge);
244 	packet_check_eom();
245 
246 	debug("Received RSA challenge from server.");
247 
248 	/*
249 	 * If the key is not stored in external hardware, we have to
250 	 * load the private key.  Try first with empty passphrase; if it
251 	 * fails, ask for a passphrase.
252 	 */
253 	if (public->flags & KEY_FLAG_EXT)
254 		private = public;
255 	else
256 		private = key_load_private_type(KEY_RSA1, authfile, "", NULL,
257 		    &perm_ok);
258 	if (private == NULL && !options.batch_mode && perm_ok) {
259 		snprintf(buf, sizeof(buf),
260 		    "Enter passphrase for RSA key '%.100s': ", comment);
261 		for (i = 0; i < options.number_of_password_prompts; i++) {
262 			passphrase = read_passphrase(buf, 0);
263 			if (strcmp(passphrase, "") != 0) {
264 				private = key_load_private_type(KEY_RSA1,
265 				    authfile, passphrase, NULL, NULL);
266 				quit = 0;
267 			} else {
268 				debug2("no passphrase given, try next key");
269 				quit = 1;
270 			}
271 			memset(passphrase, 0, strlen(passphrase));
272 			xfree(passphrase);
273 			if (private != NULL || quit)
274 				break;
275 			debug2("bad passphrase given, try again...");
276 		}
277 	}
278 	/* We no longer need the comment. */
279 	xfree(comment);
280 
281 	if (private == NULL) {
282 		if (!options.batch_mode && perm_ok)
283 			error("Bad passphrase.");
284 
285 		/* Send a dummy response packet to avoid protocol error. */
286 		packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
287 		for (i = 0; i < 16; i++)
288 			packet_put_char(0);
289 		packet_send();
290 		packet_write_wait();
291 
292 		/* Expect the server to reject it... */
293 		packet_read_expect(SSH_SMSG_FAILURE);
294 		BN_clear_free(challenge);
295 		return 0;
296 	}
297 
298 	/* Compute and send a response to the challenge. */
299 	respond_to_rsa_challenge(challenge, private->rsa);
300 
301 	/* Destroy the private key unless it in external hardware. */
302 	if (!(private->flags & KEY_FLAG_EXT))
303 		key_free(private);
304 
305 	/* We no longer need the challenge. */
306 	BN_clear_free(challenge);
307 
308 	/* Wait for response from the server. */
309 	type = packet_read();
310 	if (type == SSH_SMSG_SUCCESS) {
311 		debug("RSA authentication accepted by server.");
312 		return 1;
313 	}
314 	if (type != SSH_SMSG_FAILURE)
315 		packet_disconnect("Protocol error waiting RSA auth response: %d", type);
316 	debug("RSA authentication refused.");
317 	return 0;
318 }
319 
320 /*
321  * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
322  * authentication and RSA host authentication.
323  */
324 static int
try_rhosts_rsa_authentication(const char * local_user,Key * host_key)325 try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
326 {
327 	int type;
328 	BIGNUM *challenge;
329 
330 	debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
331 
332 	/* Tell the server that we are willing to authenticate using this key. */
333 	packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
334 	packet_put_cstring(local_user);
335 	packet_put_int(BN_num_bits(host_key->rsa->n));
336 	packet_put_bignum(host_key->rsa->e);
337 	packet_put_bignum(host_key->rsa->n);
338 	packet_send();
339 	packet_write_wait();
340 
341 	/* Wait for server's response. */
342 	type = packet_read();
343 
344 	/* The server responds with failure if it doesn't admit our
345 	   .rhosts authentication or doesn't know our host key. */
346 	if (type == SSH_SMSG_FAILURE) {
347 		debug("Server refused our rhosts authentication or host key.");
348 		return 0;
349 	}
350 	/* Otherwise, the server should respond with a challenge. */
351 	if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
352 		packet_disconnect("Protocol error during RSA authentication: %d", type);
353 
354 	/* Get the challenge from the packet. */
355 	if ((challenge = BN_new()) == NULL)
356 		fatal("try_rhosts_rsa_authentication: BN_new failed");
357 	packet_get_bignum(challenge);
358 	packet_check_eom();
359 
360 	debug("Received RSA challenge for host key from server.");
361 
362 	/* Compute a response to the challenge. */
363 	respond_to_rsa_challenge(challenge, host_key->rsa);
364 
365 	/* We no longer need the challenge. */
366 	BN_clear_free(challenge);
367 
368 	/* Wait for response from the server. */
369 	type = packet_read();
370 	if (type == SSH_SMSG_SUCCESS) {
371 		debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
372 		return 1;
373 	}
374 	if (type != SSH_SMSG_FAILURE)
375 		packet_disconnect("Protocol error waiting RSA auth response: %d", type);
376 	debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
377 	return 0;
378 }
379 
380 /*
381  * Tries to authenticate with any string-based challenge/response system.
382  * Note that the client code is not tied to s/key or TIS.
383  */
384 static int
try_challenge_response_authentication(void)385 try_challenge_response_authentication(void)
386 {
387 	int type, i;
388 	u_int clen;
389 	char prompt[1024];
390 	char *challenge, *response;
391 
392 	debug("Doing challenge response authentication.");
393 
394 	for (i = 0; i < options.number_of_password_prompts; i++) {
395 		/* request a challenge */
396 		packet_start(SSH_CMSG_AUTH_TIS);
397 		packet_send();
398 		packet_write_wait();
399 
400 		type = packet_read();
401 		if (type != SSH_SMSG_FAILURE &&
402 		    type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
403 			packet_disconnect("Protocol error: got %d in response "
404 			    "to SSH_CMSG_AUTH_TIS", type);
405 		}
406 		if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
407 			debug("No challenge.");
408 			return 0;
409 		}
410 		challenge = packet_get_string(&clen);
411 		packet_check_eom();
412 		snprintf(prompt, sizeof prompt, "%s%s", challenge,
413 		    strchr(challenge, '\n') ? "" : "\nResponse: ");
414 		xfree(challenge);
415 		if (i != 0)
416 			error("Permission denied, please try again.");
417 		if (options.cipher == SSH_CIPHER_NONE)
418 			logit("WARNING: Encryption is disabled! "
419 			    "Response will be transmitted in clear text.");
420 		response = read_passphrase(prompt, 0);
421 		if (strcmp(response, "") == 0) {
422 			xfree(response);
423 			break;
424 		}
425 		packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
426 		ssh_put_password(response);
427 		memset(response, 0, strlen(response));
428 		xfree(response);
429 		packet_send();
430 		packet_write_wait();
431 		type = packet_read();
432 		if (type == SSH_SMSG_SUCCESS)
433 			return 1;
434 		if (type != SSH_SMSG_FAILURE)
435 			packet_disconnect("Protocol error: got %d in response "
436 			    "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
437 	}
438 	/* failure */
439 	return 0;
440 }
441 
442 /*
443  * Tries to authenticate with plain passwd authentication.
444  */
445 static int
try_password_authentication(char * prompt)446 try_password_authentication(char *prompt)
447 {
448 	int type, i;
449 	char *password;
450 
451 	debug("Doing password authentication.");
452 	if (options.cipher == SSH_CIPHER_NONE)
453 		logit("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
454 	for (i = 0; i < options.number_of_password_prompts; i++) {
455 		if (i != 0)
456 			error("Permission denied, please try again.");
457 		password = read_passphrase(prompt, 0);
458 		packet_start(SSH_CMSG_AUTH_PASSWORD);
459 		ssh_put_password(password);
460 		memset(password, 0, strlen(password));
461 		xfree(password);
462 		packet_send();
463 		packet_write_wait();
464 
465 		type = packet_read();
466 		if (type == SSH_SMSG_SUCCESS)
467 			return 1;
468 		if (type != SSH_SMSG_FAILURE)
469 			packet_disconnect("Protocol error: got %d in response to passwd auth", type);
470 	}
471 	/* failure */
472 	return 0;
473 }
474 
475 /*
476  * SSH1 key exchange
477  */
478 void
ssh_kex(char * host,struct sockaddr * hostaddr)479 ssh_kex(char *host, struct sockaddr *hostaddr)
480 {
481 	int i;
482 	BIGNUM *key;
483 	Key *host_key, *server_key;
484 	int bits, rbits;
485 	int ssh_cipher_default = SSH_CIPHER_BLOWFISH;
486 	u_char session_key[SSH_SESSION_KEY_LENGTH];
487 	u_char cookie[8];
488 	u_int supported_ciphers;
489 	u_int server_flags, client_flags;
490 
491 	debug("Waiting for server public key.");
492 
493 	/* Wait for a public key packet from the server. */
494 	packet_read_expect(SSH_SMSG_PUBLIC_KEY);
495 
496 	/* Get cookie from the packet. */
497 	for (i = 0; i < 8; i++)
498 		cookie[i] = packet_get_char();
499 
500 	/* Get the public key. */
501 	server_key = key_new(KEY_RSA1);
502 	bits = packet_get_int();
503 	packet_get_bignum(server_key->rsa->e);
504 	packet_get_bignum(server_key->rsa->n);
505 
506 	rbits = BN_num_bits(server_key->rsa->n);
507 	if (bits != rbits) {
508 		logit("Warning: Server lies about size of server public key: "
509 		    "actual size is %d bits vs. announced %d.", rbits, bits);
510 		logit("Warning: This may be due to an old implementation of ssh.");
511 	}
512 	/* Get the host key. */
513 	host_key = key_new(KEY_RSA1);
514 	bits = packet_get_int();
515 	packet_get_bignum(host_key->rsa->e);
516 	packet_get_bignum(host_key->rsa->n);
517 
518 	rbits = BN_num_bits(host_key->rsa->n);
519 	if (bits != rbits) {
520 		logit("Warning: Server lies about size of server host key: "
521 		    "actual size is %d bits vs. announced %d.", rbits, bits);
522 		logit("Warning: This may be due to an old implementation of ssh.");
523 	}
524 
525 	/* Get protocol flags. */
526 	server_flags = packet_get_int();
527 	packet_set_protocol_flags(server_flags);
528 
529 	supported_ciphers = packet_get_int();
530 	supported_authentications = packet_get_int();
531 	packet_check_eom();
532 
533 	debug("Received server public key (%d bits) and host key (%d bits).",
534 	    BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
535 
536 	if (verify_host_key(host, hostaddr, host_key) == -1)
537 		fatal("Host key verification failed.");
538 
539 	client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
540 
541 	derive_ssh1_session_id(host_key->rsa->n, server_key->rsa->n, cookie, session_id);
542 
543 	/* Generate a session key. */
544 
545 	/*
546 	 * Generate an encryption key for the session.   The key is a 256 bit
547 	 * random number, interpreted as a 32-byte key, with the least
548 	 * significant 8 bits being the first byte of the key.
549 	 */
550 	arc4random_buf(session_key, sizeof(session_key));
551 
552 	/*
553 	 * According to the protocol spec, the first byte of the session key
554 	 * is the highest byte of the integer.  The session key is xored with
555 	 * the first 16 bytes of the session id.
556 	 */
557 	if ((key = BN_new()) == NULL)
558 		fatal("ssh_kex: BN_new failed");
559 	if (BN_set_word(key, 0) == 0)
560 		fatal("ssh_kex: BN_set_word failed");
561 	for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
562 		if (BN_lshift(key, key, 8) == 0)
563 			fatal("ssh_kex: BN_lshift failed");
564 		if (i < 16) {
565 			if (BN_add_word(key, session_key[i] ^ session_id[i])
566 			    == 0)
567 				fatal("ssh_kex: BN_add_word failed");
568 		} else {
569 			if (BN_add_word(key, session_key[i]) == 0)
570 				fatal("ssh_kex: BN_add_word failed");
571 		}
572 	}
573 
574 	/*
575 	 * Encrypt the integer using the public key and host key of the
576 	 * server (key with smaller modulus first).
577 	 */
578 	if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
579 		/* Public key has smaller modulus. */
580 		if (BN_num_bits(host_key->rsa->n) <
581 		    BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
582 			fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
583 			    "SSH_KEY_BITS_RESERVED %d",
584 			    BN_num_bits(host_key->rsa->n),
585 			    BN_num_bits(server_key->rsa->n),
586 			    SSH_KEY_BITS_RESERVED);
587 		}
588 		rsa_public_encrypt(key, key, server_key->rsa);
589 		rsa_public_encrypt(key, key, host_key->rsa);
590 	} else {
591 		/* Host key has smaller modulus (or they are equal). */
592 		if (BN_num_bits(server_key->rsa->n) <
593 		    BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
594 			fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
595 			    "SSH_KEY_BITS_RESERVED %d",
596 			    BN_num_bits(server_key->rsa->n),
597 			    BN_num_bits(host_key->rsa->n),
598 			    SSH_KEY_BITS_RESERVED);
599 		}
600 		rsa_public_encrypt(key, key, host_key->rsa);
601 		rsa_public_encrypt(key, key, server_key->rsa);
602 	}
603 
604 	/* Destroy the public keys since we no longer need them. */
605 	key_free(server_key);
606 	key_free(host_key);
607 
608 	if (options.cipher == SSH_CIPHER_NOT_SET) {
609 		if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
610 			options.cipher = ssh_cipher_default;
611 	} else if (options.cipher == SSH_CIPHER_INVALID ||
612 	    !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
613 		logit("No valid SSH1 cipher, using %.100s instead.",
614 		    cipher_name(ssh_cipher_default));
615 		options.cipher = ssh_cipher_default;
616 	}
617 	/* Check that the selected cipher is supported. */
618 	if (!(supported_ciphers & (1 << options.cipher)))
619 		fatal("Selected cipher type %.100s not supported by server.",
620 		    cipher_name(options.cipher));
621 
622 	debug("Encryption type: %.100s", cipher_name(options.cipher));
623 
624 	/* Send the encrypted session key to the server. */
625 	packet_start(SSH_CMSG_SESSION_KEY);
626 	packet_put_char(options.cipher);
627 
628 	/* Send the cookie back to the server. */
629 	for (i = 0; i < 8; i++)
630 		packet_put_char(cookie[i]);
631 
632 	/* Send and destroy the encrypted encryption key integer. */
633 	packet_put_bignum(key);
634 	BN_clear_free(key);
635 
636 	/* Send protocol flags. */
637 	packet_put_int(client_flags);
638 
639 	/* Send the packet now. */
640 	packet_send();
641 	packet_write_wait();
642 
643 	debug("Sent encrypted session key.");
644 
645 	/* Set the encryption key. */
646 	packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
647 
648 	/* We will no longer need the session key here.  Destroy any extra copies. */
649 	memset(session_key, 0, sizeof(session_key));
650 
651 	/*
652 	 * Expect a success message from the server.  Note that this message
653 	 * will be received in encrypted form.
654 	 */
655 	packet_read_expect(SSH_SMSG_SUCCESS);
656 
657 	debug("Received encrypted confirmation.");
658 }
659 
660 /*
661  * Authenticate user
662  */
663 void
ssh_userauth1(const char * local_user,const char * server_user,char * host,Sensitive * sensitive)664 ssh_userauth1(const char *local_user, const char *server_user, char *host,
665     Sensitive *sensitive)
666 {
667 	int i, type;
668 
669 	if (supported_authentications == 0)
670 		fatal("ssh_userauth1: server supports no auth methods");
671 
672 	/* Send the name of the user to log in as on the server. */
673 	packet_start(SSH_CMSG_USER);
674 	packet_put_cstring(server_user);
675 	packet_send();
676 	packet_write_wait();
677 
678 	/*
679 	 * The server should respond with success if no authentication is
680 	 * needed (the user has no password).  Otherwise the server responds
681 	 * with failure.
682 	 */
683 	type = packet_read();
684 
685 	/* check whether the connection was accepted without authentication. */
686 	if (type == SSH_SMSG_SUCCESS)
687 		goto success;
688 	if (type != SSH_SMSG_FAILURE)
689 		packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
690 
691 	/*
692 	 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
693 	 * authentication.
694 	 */
695 	if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
696 	    options.rhosts_rsa_authentication) {
697 		for (i = 0; i < sensitive->nkeys; i++) {
698 			if (sensitive->keys[i] != NULL &&
699 			    sensitive->keys[i]->type == KEY_RSA1 &&
700 			    try_rhosts_rsa_authentication(local_user,
701 			    sensitive->keys[i]))
702 				goto success;
703 		}
704 	}
705 	/* Try RSA authentication if the server supports it. */
706 	if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
707 	    options.rsa_authentication) {
708 		/*
709 		 * Try RSA authentication using the authentication agent. The
710 		 * agent is tried first because no passphrase is needed for
711 		 * it, whereas identity files may require passphrases.
712 		 */
713 		if (try_agent_authentication())
714 			goto success;
715 
716 		/* Try RSA authentication for each identity. */
717 		for (i = 0; i < options.num_identity_files; i++)
718 			if (options.identity_keys[i] != NULL &&
719 			    options.identity_keys[i]->type == KEY_RSA1 &&
720 			    try_rsa_authentication(i))
721 				goto success;
722 	}
723 	/* Try challenge response authentication if the server supports it. */
724 	if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
725 	    options.challenge_response_authentication && !options.batch_mode) {
726 		if (try_challenge_response_authentication())
727 			goto success;
728 	}
729 	/* Try password authentication if the server supports it. */
730 	if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
731 	    options.password_authentication && !options.batch_mode) {
732 		char prompt[80];
733 
734 		snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
735 		    server_user, host);
736 		if (try_password_authentication(prompt))
737 			goto success;
738 	}
739 	/* All authentication methods have failed.  Exit with an error message. */
740 	fatal("Permission denied.");
741 	/* NOTREACHED */
742 
743  success:
744 	return;	/* need statement after label */
745 }
746