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