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