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