1 /* $OpenBSD: sshconnect1.c,v 1.66 2006/07/22 20:48:23 stevesk Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * Code to connect to a remote host, and to perform the client side of the
7 * login (authentication) dialog.
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".
18 #include <openssl/bn.h>
19 #include <openssl/md5.h>
35 #include "sshconnect.h"
42 /* Session id for the current session. */
43 u_char session_id[16];
44 u_int supported_authentications = 0;
46 extern Options options;
47 extern char *__progname;
50 * Checks if the user has an authentication agent, and if so, tries to
51 * authenticate using the agent.
54 try_agent_authentication(void)
58 AuthenticationConnection *auth;
64 /* Get connection to the agent. */
65 auth = ssh_get_authentication_connection();
69 if ((challenge = BN_new()) == NULL)
70 fatal("try_agent_authentication: BN_new failed");
71 /* Loop through identities served by the agent. */
72 for (key = ssh_get_first_identity(auth, &comment, 1);
74 key = ssh_get_next_identity(auth, &comment, 1)) {
76 /* Try this identity. */
77 debug("Trying RSA authentication via agent with '%.100s'", comment);
80 /* Tell the server that we are willing to authenticate using this key. */
81 packet_start(SSH_CMSG_AUTH_RSA);
82 packet_put_bignum(key->rsa->n);
86 /* Wait for server's response. */
89 /* The server sends failure if it doesn't like our key or
90 does not support RSA authentication. */
91 if (type == SSH_SMSG_FAILURE) {
92 debug("Server refused our key.");
96 /* Otherwise it should have sent a challenge. */
97 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
98 packet_disconnect("Protocol error during RSA authentication: %d",
101 packet_get_bignum(challenge);
104 debug("Received RSA challenge from server.");
106 /* Ask the agent to decrypt the challenge. */
107 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
109 * The agent failed to authenticate this identifier
110 * although it advertised it supports this. Just
111 * return a wrong value.
113 logit("Authentication agent failed to decrypt challenge.");
114 memset(response, 0, sizeof(response));
117 debug("Sending response to RSA challenge.");
119 /* Send the decrypted challenge back to the server. */
120 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
121 for (i = 0; i < 16; i++)
122 packet_put_char(response[i]);
126 /* Wait for response from the server. */
127 type = packet_read();
129 /* The server returns success if it accepted the authentication. */
130 if (type == SSH_SMSG_SUCCESS) {
131 ssh_close_authentication_connection(auth);
132 BN_clear_free(challenge);
133 debug("RSA authentication accepted by server.");
136 /* Otherwise it should return failure. */
137 if (type != SSH_SMSG_FAILURE)
138 packet_disconnect("Protocol error waiting RSA auth response: %d",
141 ssh_close_authentication_connection(auth);
142 BN_clear_free(challenge);
143 debug("RSA authentication using agent refused.");
148 * Computes the proper response to a RSA challenge, and sends the response to
152 respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
154 u_char buf[32], response[16];
158 /* Decrypt the challenge using the private key. */
159 /* XXX think about Bleichenbacher, too */
160 if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
162 "respond_to_rsa_challenge: rsa_private_decrypt failed");
164 /* Compute the response. */
165 /* The response is MD5 of decrypted challenge plus session id. */
166 len = BN_num_bytes(challenge);
167 if (len <= 0 || (u_int)len > sizeof(buf))
169 "respond_to_rsa_challenge: bad challenge length %d", len);
171 memset(buf, 0, sizeof(buf));
172 BN_bn2bin(challenge, buf + sizeof(buf) - len);
174 MD5_Update(&md, buf, 32);
175 MD5_Update(&md, session_id, 16);
176 MD5_Final(response, &md);
178 debug("Sending response to host key RSA challenge.");
180 /* Send the response back to the server. */
181 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
182 for (i = 0; i < 16; i++)
183 packet_put_char(response[i]);
187 memset(buf, 0, sizeof(buf));
188 memset(response, 0, sizeof(response));
189 memset(&md, 0, sizeof(md));
193 * Checks if the user has authentication file, and if so, tries to authenticate
197 try_rsa_authentication(int idx)
200 Key *public, *private;
201 char buf[300], *passphrase, *comment, *authfile;
202 int i, perm_ok = 1, type, quit;
204 public = options.identity_keys[idx];
205 authfile = options.identity_files[idx];
206 comment = xstrdup(authfile);
208 debug("Trying RSA authentication with key '%.100s'", comment);
210 /* Tell the server that we are willing to authenticate using this key. */
211 packet_start(SSH_CMSG_AUTH_RSA);
212 packet_put_bignum(public->rsa->n);
216 /* Wait for server's response. */
217 type = packet_read();
220 * The server responds with failure if it doesn't like our key or
221 * doesn't support RSA authentication.
223 if (type == SSH_SMSG_FAILURE) {
224 debug("Server refused our key.");
228 /* Otherwise, the server should respond with a challenge. */
229 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
230 packet_disconnect("Protocol error during RSA authentication: %d", type);
232 /* Get the challenge from the packet. */
233 if ((challenge = BN_new()) == NULL)
234 fatal("try_rsa_authentication: BN_new failed");
235 packet_get_bignum(challenge);
238 debug("Received RSA challenge from server.");
241 * If the key is not stored in external hardware, we have to
242 * load the private key. Try first with empty passphrase; if it
243 * fails, ask for a passphrase.
245 if (public->flags & KEY_FLAG_EXT)
248 private = key_load_private_type(KEY_RSA1, authfile, "", NULL,
250 if (private == NULL && !options.batch_mode && perm_ok) {
251 snprintf(buf, sizeof(buf),
252 "Enter passphrase for RSA key '%.100s': ", comment);
253 for (i = 0; i < options.number_of_password_prompts; i++) {
254 passphrase = read_passphrase(buf, 0);
255 if (strcmp(passphrase, "") != 0) {
256 private = key_load_private_type(KEY_RSA1,
257 authfile, passphrase, NULL, NULL);
260 debug2("no passphrase given, try next key");
263 memset(passphrase, 0, strlen(passphrase));
265 if (private != NULL || quit)
267 debug2("bad passphrase given, try again...");
270 /* We no longer need the comment. */
273 if (private == NULL) {
274 if (!options.batch_mode && perm_ok)
275 error("Bad passphrase.");
277 /* Send a dummy response packet to avoid protocol error. */
278 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
279 for (i = 0; i < 16; i++)
284 /* Expect the server to reject it... */
285 packet_read_expect(SSH_SMSG_FAILURE);
286 BN_clear_free(challenge);
290 /* Compute and send a response to the challenge. */
291 respond_to_rsa_challenge(challenge, private->rsa);
293 /* Destroy the private key unless it in external hardware. */
294 if (!(private->flags & KEY_FLAG_EXT))
297 /* We no longer need the challenge. */
298 BN_clear_free(challenge);
300 /* Wait for response from the server. */
301 type = packet_read();
302 if (type == SSH_SMSG_SUCCESS) {
303 debug("RSA authentication accepted by server.");
306 if (type != SSH_SMSG_FAILURE)
307 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
308 debug("RSA authentication refused.");
313 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
314 * authentication and RSA host authentication.
317 try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
322 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
324 /* Tell the server that we are willing to authenticate using this key. */
325 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
326 packet_put_cstring(local_user);
327 packet_put_int(BN_num_bits(host_key->rsa->n));
328 packet_put_bignum(host_key->rsa->e);
329 packet_put_bignum(host_key->rsa->n);
333 /* Wait for server's response. */
334 type = packet_read();
336 /* The server responds with failure if it doesn't admit our
337 .rhosts authentication or doesn't know our host key. */
338 if (type == SSH_SMSG_FAILURE) {
339 debug("Server refused our rhosts authentication or host key.");
342 /* Otherwise, the server should respond with a challenge. */
343 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
344 packet_disconnect("Protocol error during RSA authentication: %d", type);
346 /* Get the challenge from the packet. */
347 if ((challenge = BN_new()) == NULL)
348 fatal("try_rhosts_rsa_authentication: BN_new failed");
349 packet_get_bignum(challenge);
352 debug("Received RSA challenge for host key from server.");
354 /* Compute a response to the challenge. */
355 respond_to_rsa_challenge(challenge, host_key->rsa);
357 /* We no longer need the challenge. */
358 BN_clear_free(challenge);
360 /* Wait for response from the server. */
361 type = packet_read();
362 if (type == SSH_SMSG_SUCCESS) {
363 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
366 if (type != SSH_SMSG_FAILURE)
367 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
368 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
373 * Tries to authenticate with any string-based challenge/response system.
374 * Note that the client code is not tied to s/key or TIS.
377 try_challenge_response_authentication(void)
382 char *challenge, *response;
384 debug("Doing challenge response authentication.");
386 for (i = 0; i < options.number_of_password_prompts; i++) {
387 /* request a challenge */
388 packet_start(SSH_CMSG_AUTH_TIS);
392 type = packet_read();
393 if (type != SSH_SMSG_FAILURE &&
394 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
395 packet_disconnect("Protocol error: got %d in response "
396 "to SSH_CMSG_AUTH_TIS", type);
398 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
399 debug("No challenge.");
402 challenge = packet_get_string(&clen);
404 snprintf(prompt, sizeof prompt, "%s%s", challenge,
405 strchr(challenge, '\n') ? "" : "\nResponse: ");
408 error("Permission denied, please try again.");
409 if (options.cipher == SSH_CIPHER_NONE)
410 logit("WARNING: Encryption is disabled! "
411 "Response will be transmitted in clear text.");
412 response = read_passphrase(prompt, 0);
413 if (strcmp(response, "") == 0) {
417 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
418 ssh_put_password(response);
419 memset(response, 0, strlen(response));
423 type = packet_read();
424 if (type == SSH_SMSG_SUCCESS)
426 if (type != SSH_SMSG_FAILURE)
427 packet_disconnect("Protocol error: got %d in response "
428 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
435 * Tries to authenticate with plain passwd authentication.
438 try_password_authentication(char *prompt)
443 debug("Doing password authentication.");
444 if (options.cipher == SSH_CIPHER_NONE)
445 logit("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
446 for (i = 0; i < options.number_of_password_prompts; i++) {
448 error("Permission denied, please try again.");
449 password = read_passphrase(prompt, 0);
450 packet_start(SSH_CMSG_AUTH_PASSWORD);
451 ssh_put_password(password);
452 memset(password, 0, strlen(password));
457 type = packet_read();
458 if (type == SSH_SMSG_SUCCESS)
460 if (type != SSH_SMSG_FAILURE)
461 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
471 ssh_kex(char *host, struct sockaddr *hostaddr)
475 Key *host_key, *server_key;
477 int ssh_cipher_default = SSH_CIPHER_3DES;
478 u_char session_key[SSH_SESSION_KEY_LENGTH];
480 u_int supported_ciphers;
481 u_int server_flags, client_flags;
484 debug("Waiting for server public key.");
486 /* Wait for a public key packet from the server. */
487 packet_read_expect(SSH_SMSG_PUBLIC_KEY);
489 /* Get cookie from the packet. */
490 for (i = 0; i < 8; i++)
491 cookie[i] = packet_get_char();
493 /* Get the public key. */
494 server_key = key_new(KEY_RSA1);
495 bits = packet_get_int();
496 packet_get_bignum(server_key->rsa->e);
497 packet_get_bignum(server_key->rsa->n);
499 rbits = BN_num_bits(server_key->rsa->n);
501 logit("Warning: Server lies about size of server public key: "
502 "actual size is %d bits vs. announced %d.", rbits, bits);
503 logit("Warning: This may be due to an old implementation of ssh.");
505 /* Get the host key. */
506 host_key = key_new(KEY_RSA1);
507 bits = packet_get_int();
508 packet_get_bignum(host_key->rsa->e);
509 packet_get_bignum(host_key->rsa->n);
511 rbits = BN_num_bits(host_key->rsa->n);
513 logit("Warning: Server lies about size of server host key: "
514 "actual size is %d bits vs. announced %d.", rbits, bits);
515 logit("Warning: This may be due to an old implementation of ssh.");
518 /* Get protocol flags. */
519 server_flags = packet_get_int();
520 packet_set_protocol_flags(server_flags);
522 supported_ciphers = packet_get_int();
523 supported_authentications = packet_get_int();
526 debug("Received server public key (%d bits) and host key (%d bits).",
527 BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
529 if (verify_host_key(host, hostaddr, host_key) == -1)
530 fatal("Host key verification failed.");
532 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
534 derive_ssh1_session_id(host_key->rsa->n, server_key->rsa->n, cookie, session_id);
536 /* Generate a session key. */
540 * Generate an encryption key for the session. The key is a 256 bit
541 * random number, interpreted as a 32-byte key, with the least
542 * significant 8 bits being the first byte of the key.
544 for (i = 0; i < 32; i++) {
547 session_key[i] = rnd & 0xff;
552 * According to the protocol spec, the first byte of the session key
553 * is the highest byte of the integer. The session key is xored with
554 * the first 16 bytes of the session id.
556 if ((key = BN_new()) == NULL)
557 fatal("respond_to_rsa_challenge: BN_new failed");
559 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
560 BN_lshift(key, key, 8);
562 BN_add_word(key, session_key[i] ^ session_id[i]);
564 BN_add_word(key, session_key[i]);
568 * Encrypt the integer using the public key and host key of the
569 * server (key with smaller modulus first).
571 if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
572 /* Public key has smaller modulus. */
573 if (BN_num_bits(host_key->rsa->n) <
574 BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
575 fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
576 "SSH_KEY_BITS_RESERVED %d",
577 BN_num_bits(host_key->rsa->n),
578 BN_num_bits(server_key->rsa->n),
579 SSH_KEY_BITS_RESERVED);
581 rsa_public_encrypt(key, key, server_key->rsa);
582 rsa_public_encrypt(key, key, host_key->rsa);
584 /* Host key has smaller modulus (or they are equal). */
585 if (BN_num_bits(server_key->rsa->n) <
586 BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
587 fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
588 "SSH_KEY_BITS_RESERVED %d",
589 BN_num_bits(server_key->rsa->n),
590 BN_num_bits(host_key->rsa->n),
591 SSH_KEY_BITS_RESERVED);
593 rsa_public_encrypt(key, key, host_key->rsa);
594 rsa_public_encrypt(key, key, server_key->rsa);
597 /* Destroy the public keys since we no longer need them. */
598 key_free(server_key);
601 if (options.cipher == SSH_CIPHER_NOT_SET) {
602 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
603 options.cipher = ssh_cipher_default;
604 } else if (options.cipher == SSH_CIPHER_INVALID ||
605 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
606 logit("No valid SSH1 cipher, using %.100s instead.",
607 cipher_name(ssh_cipher_default));
608 options.cipher = ssh_cipher_default;
610 /* Check that the selected cipher is supported. */
611 if (!(supported_ciphers & (1 << options.cipher)))
612 fatal("Selected cipher type %.100s not supported by server.",
613 cipher_name(options.cipher));
615 debug("Encryption type: %.100s", cipher_name(options.cipher));
617 /* Send the encrypted session key to the server. */
618 packet_start(SSH_CMSG_SESSION_KEY);
619 packet_put_char(options.cipher);
621 /* Send the cookie back to the server. */
622 for (i = 0; i < 8; i++)
623 packet_put_char(cookie[i]);
625 /* Send and destroy the encrypted encryption key integer. */
626 packet_put_bignum(key);
629 /* Send protocol flags. */
630 packet_put_int(client_flags);
632 /* Send the packet now. */
636 debug("Sent encrypted session key.");
638 /* Set the encryption key. */
639 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
641 /* We will no longer need the session key here. Destroy any extra copies. */
642 memset(session_key, 0, sizeof(session_key));
645 * Expect a success message from the server. Note that this message
646 * will be received in encrypted form.
648 packet_read_expect(SSH_SMSG_SUCCESS);
650 debug("Received encrypted confirmation.");
657 ssh_userauth1(const char *local_user, const char *server_user, char *host,
658 Sensitive *sensitive)
662 if (supported_authentications == 0)
663 fatal("ssh_userauth1: server supports no auth methods");
665 /* Send the name of the user to log in as on the server. */
666 packet_start(SSH_CMSG_USER);
667 packet_put_cstring(server_user);
672 * The server should respond with success if no authentication is
673 * needed (the user has no password). Otherwise the server responds
676 type = packet_read();
678 /* check whether the connection was accepted without authentication. */
679 if (type == SSH_SMSG_SUCCESS)
681 if (type != SSH_SMSG_FAILURE)
682 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
685 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
688 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
689 options.rhosts_rsa_authentication) {
690 for (i = 0; i < sensitive->nkeys; i++) {
691 if (sensitive->keys[i] != NULL &&
692 sensitive->keys[i]->type == KEY_RSA1 &&
693 try_rhosts_rsa_authentication(local_user,
698 /* Try RSA authentication if the server supports it. */
699 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
700 options.rsa_authentication) {
702 * Try RSA authentication using the authentication agent. The
703 * agent is tried first because no passphrase is needed for
704 * it, whereas identity files may require passphrases.
706 if (try_agent_authentication())
709 /* Try RSA authentication for each identity. */
710 for (i = 0; i < options.num_identity_files; i++)
711 if (options.identity_keys[i] != NULL &&
712 options.identity_keys[i]->type == KEY_RSA1 &&
713 try_rsa_authentication(i))
716 /* Try challenge response authentication if the server supports it. */
717 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
718 options.challenge_response_authentication && !options.batch_mode) {
719 if (try_challenge_response_authentication())
722 /* Try password authentication if the server supports it. */
723 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
724 options.password_authentication && !options.batch_mode) {
727 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
729 if (try_password_authentication(prompt))
732 /* All authentication methods have failed. Exit with an error message. */
733 fatal("Permission denied.");
737 return; /* need statement after label */