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