2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * Code to connect to a remote host, and to perform the client side of the
6 * login (authentication) dialog.
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
17 #include <openssl/bn.h>
18 #include <openssl/md5.h>
32 #include "sshconnect.h"
39 /* Session id for the current session. */
40 u_char session_id[16];
41 u_int supported_authentications = 0;
43 extern Options options;
44 extern char *__progname;
47 * Checks if the user has an authentication agent, and if so, tries to
48 * authenticate using the agent.
51 try_agent_authentication(void)
55 AuthenticationConnection *auth;
61 /* Get connection to the agent. */
62 auth = ssh_get_authentication_connection();
66 if ((challenge = BN_new()) == NULL)
67 fatal("try_agent_authentication: BN_new failed");
68 /* Loop through identities served by the agent. */
69 for (key = ssh_get_first_identity(auth, &comment, 1);
71 key = ssh_get_next_identity(auth, &comment, 1)) {
73 /* Try this identity. */
74 debug("Trying RSA authentication via agent with '%.100s'", comment);
77 /* Tell the server that we are willing to authenticate using this key. */
78 packet_start(SSH_CMSG_AUTH_RSA);
79 packet_put_bignum(key->rsa->n);
83 /* Wait for server's response. */
86 /* The server sends failure if it doesn't like our key or
87 does not support RSA authentication. */
88 if (type == SSH_SMSG_FAILURE) {
89 debug("Server refused our key.");
93 /* Otherwise it should have sent a challenge. */
94 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
95 packet_disconnect("Protocol error during RSA authentication: %d",
98 packet_get_bignum(challenge);
101 debug("Received RSA challenge from server.");
103 /* Ask the agent to decrypt the challenge. */
104 if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) {
106 * The agent failed to authenticate this identifier
107 * although it advertised it supports this. Just
108 * return a wrong value.
110 logit("Authentication agent failed to decrypt challenge.");
111 memset(response, 0, sizeof(response));
114 debug("Sending response to RSA challenge.");
116 /* Send the decrypted challenge back to the server. */
117 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
118 for (i = 0; i < 16; i++)
119 packet_put_char(response[i]);
123 /* Wait for response from the server. */
124 type = packet_read();
126 /* The server returns success if it accepted the authentication. */
127 if (type == SSH_SMSG_SUCCESS) {
128 ssh_close_authentication_connection(auth);
129 BN_clear_free(challenge);
130 debug("RSA authentication accepted by server.");
133 /* Otherwise it should return failure. */
134 if (type != SSH_SMSG_FAILURE)
135 packet_disconnect("Protocol error waiting RSA auth response: %d",
138 ssh_close_authentication_connection(auth);
139 BN_clear_free(challenge);
140 debug("RSA authentication using agent refused.");
145 * Computes the proper response to a RSA challenge, and sends the response to
149 respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv)
151 u_char buf[32], response[16];
155 /* Decrypt the challenge using the private key. */
156 /* XXX think about Bleichenbacher, too */
157 if (rsa_private_decrypt(challenge, challenge, prv) <= 0)
159 "respond_to_rsa_challenge: rsa_private_decrypt failed");
161 /* Compute the response. */
162 /* The response is MD5 of decrypted challenge plus session id. */
163 len = BN_num_bytes(challenge);
164 if (len <= 0 || (u_int)len > sizeof(buf))
166 "respond_to_rsa_challenge: bad challenge length %d", len);
168 memset(buf, 0, sizeof(buf));
169 BN_bn2bin(challenge, buf + sizeof(buf) - len);
171 MD5_Update(&md, buf, 32);
172 MD5_Update(&md, session_id, 16);
173 MD5_Final(response, &md);
175 debug("Sending response to host key RSA challenge.");
177 /* Send the response back to the server. */
178 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
179 for (i = 0; i < 16; i++)
180 packet_put_char(response[i]);
184 memset(buf, 0, sizeof(buf));
185 memset(response, 0, sizeof(response));
186 memset(&md, 0, sizeof(md));
190 * Checks if the user has authentication file, and if so, tries to authenticate
194 try_rsa_authentication(int idx)
197 Key *public, *private;
198 char buf[300], *passphrase, *comment, *authfile;
201 public = options.identity_keys[idx];
202 authfile = options.identity_files[idx];
203 comment = xstrdup(authfile);
205 debug("Trying RSA authentication with key '%.100s'", comment);
207 /* Tell the server that we are willing to authenticate using this key. */
208 packet_start(SSH_CMSG_AUTH_RSA);
209 packet_put_bignum(public->rsa->n);
213 /* Wait for server's response. */
214 type = packet_read();
217 * The server responds with failure if it doesn't like our key or
218 * doesn't support RSA authentication.
220 if (type == SSH_SMSG_FAILURE) {
221 debug("Server refused our key.");
225 /* Otherwise, the server should respond with a challenge. */
226 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
227 packet_disconnect("Protocol error during RSA authentication: %d", type);
229 /* Get the challenge from the packet. */
230 if ((challenge = BN_new()) == NULL)
231 fatal("try_rsa_authentication: BN_new failed");
232 packet_get_bignum(challenge);
235 debug("Received RSA challenge from server.");
238 * If the key is not stored in external hardware, we have to
239 * load the private key. Try first with empty passphrase; if it
240 * fails, ask for a passphrase.
242 if (public->flags & KEY_FLAG_EXT)
245 private = key_load_private_type(KEY_RSA1, authfile, "", NULL);
246 if (private == NULL && !options.batch_mode) {
247 snprintf(buf, sizeof(buf),
248 "Enter passphrase for RSA key '%.100s': ", comment);
249 for (i = 0; i < options.number_of_password_prompts; i++) {
250 passphrase = read_passphrase(buf, 0);
251 if (strcmp(passphrase, "") != 0) {
252 private = key_load_private_type(KEY_RSA1,
253 authfile, passphrase, NULL);
256 debug2("no passphrase given, try next key");
259 memset(passphrase, 0, strlen(passphrase));
261 if (private != NULL || quit)
263 debug2("bad passphrase given, try again...");
266 /* We no longer need the comment. */
269 if (private == NULL) {
270 if (!options.batch_mode)
271 error("Bad passphrase.");
273 /* Send a dummy response packet to avoid protocol error. */
274 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE);
275 for (i = 0; i < 16; i++)
280 /* Expect the server to reject it... */
281 packet_read_expect(SSH_SMSG_FAILURE);
282 BN_clear_free(challenge);
286 /* Compute and send a response to the challenge. */
287 respond_to_rsa_challenge(challenge, private->rsa);
289 /* Destroy the private key unless it in external hardware. */
290 if (!(private->flags & KEY_FLAG_EXT))
293 /* We no longer need the challenge. */
294 BN_clear_free(challenge);
296 /* Wait for response from the server. */
297 type = packet_read();
298 if (type == SSH_SMSG_SUCCESS) {
299 debug("RSA authentication accepted by server.");
302 if (type != SSH_SMSG_FAILURE)
303 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
304 debug("RSA authentication refused.");
309 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
310 * authentication and RSA host authentication.
313 try_rhosts_rsa_authentication(const char *local_user, Key * host_key)
318 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
320 /* Tell the server that we are willing to authenticate using this key. */
321 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
322 packet_put_cstring(local_user);
323 packet_put_int(BN_num_bits(host_key->rsa->n));
324 packet_put_bignum(host_key->rsa->e);
325 packet_put_bignum(host_key->rsa->n);
329 /* Wait for server's response. */
330 type = packet_read();
332 /* The server responds with failure if it doesn't admit our
333 .rhosts authentication or doesn't know our host key. */
334 if (type == SSH_SMSG_FAILURE) {
335 debug("Server refused our rhosts authentication or host key.");
338 /* Otherwise, the server should respond with a challenge. */
339 if (type != SSH_SMSG_AUTH_RSA_CHALLENGE)
340 packet_disconnect("Protocol error during RSA authentication: %d", type);
342 /* Get the challenge from the packet. */
343 if ((challenge = BN_new()) == NULL)
344 fatal("try_rhosts_rsa_authentication: BN_new failed");
345 packet_get_bignum(challenge);
348 debug("Received RSA challenge for host key from server.");
350 /* Compute a response to the challenge. */
351 respond_to_rsa_challenge(challenge, host_key->rsa);
353 /* We no longer need the challenge. */
354 BN_clear_free(challenge);
356 /* Wait for response from the server. */
357 type = packet_read();
358 if (type == SSH_SMSG_SUCCESS) {
359 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
362 if (type != SSH_SMSG_FAILURE)
363 packet_disconnect("Protocol error waiting RSA auth response: %d", type);
364 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
369 * Tries to authenticate with any string-based challenge/response system.
370 * Note that the client code is not tied to s/key or TIS.
373 try_challenge_response_authentication(void)
378 char *challenge, *response;
380 debug("Doing challenge response authentication.");
382 for (i = 0; i < options.number_of_password_prompts; i++) {
383 /* request a challenge */
384 packet_start(SSH_CMSG_AUTH_TIS);
388 type = packet_read();
389 if (type != SSH_SMSG_FAILURE &&
390 type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
391 packet_disconnect("Protocol error: got %d in response "
392 "to SSH_CMSG_AUTH_TIS", type);
394 if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) {
395 debug("No challenge.");
398 challenge = packet_get_string(&clen);
400 snprintf(prompt, sizeof prompt, "%s%s", challenge,
401 strchr(challenge, '\n') ? "" : "\nResponse: ");
404 error("Permission denied, please try again.");
405 if (options.cipher == SSH_CIPHER_NONE)
406 logit("WARNING: Encryption is disabled! "
407 "Response will be transmitted in clear text.");
408 response = read_passphrase(prompt, 0);
409 if (strcmp(response, "") == 0) {
413 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
414 ssh_put_password(response);
415 memset(response, 0, strlen(response));
419 type = packet_read();
420 if (type == SSH_SMSG_SUCCESS)
422 if (type != SSH_SMSG_FAILURE)
423 packet_disconnect("Protocol error: got %d in response "
424 "to SSH_CMSG_AUTH_TIS_RESPONSE", type);
431 * Tries to authenticate with plain passwd authentication.
434 try_password_authentication(char *prompt)
439 debug("Doing password authentication.");
440 if (options.cipher == SSH_CIPHER_NONE)
441 logit("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
442 for (i = 0; i < options.number_of_password_prompts; i++) {
444 error("Permission denied, please try again.");
445 password = read_passphrase(prompt, 0);
446 packet_start(SSH_CMSG_AUTH_PASSWORD);
447 ssh_put_password(password);
448 memset(password, 0, strlen(password));
453 type = packet_read();
454 if (type == SSH_SMSG_SUCCESS)
456 if (type != SSH_SMSG_FAILURE)
457 packet_disconnect("Protocol error: got %d in response to passwd auth", type);
467 ssh_kex(char *host, struct sockaddr *hostaddr)
471 Key *host_key, *server_key;
473 int ssh_cipher_default = SSH_CIPHER_3DES;
474 u_char session_key[SSH_SESSION_KEY_LENGTH];
476 u_int supported_ciphers;
477 u_int server_flags, client_flags;
480 debug("Waiting for server public key.");
482 /* Wait for a public key packet from the server. */
483 packet_read_expect(SSH_SMSG_PUBLIC_KEY);
485 /* Get cookie from the packet. */
486 for (i = 0; i < 8; i++)
487 cookie[i] = packet_get_char();
489 /* Get the public key. */
490 server_key = key_new(KEY_RSA1);
491 bits = packet_get_int();
492 packet_get_bignum(server_key->rsa->e);
493 packet_get_bignum(server_key->rsa->n);
495 rbits = BN_num_bits(server_key->rsa->n);
497 logit("Warning: Server lies about size of server public key: "
498 "actual size is %d bits vs. announced %d.", rbits, bits);
499 logit("Warning: This may be due to an old implementation of ssh.");
501 /* Get the host key. */
502 host_key = key_new(KEY_RSA1);
503 bits = packet_get_int();
504 packet_get_bignum(host_key->rsa->e);
505 packet_get_bignum(host_key->rsa->n);
507 rbits = BN_num_bits(host_key->rsa->n);
509 logit("Warning: Server lies about size of server host key: "
510 "actual size is %d bits vs. announced %d.", rbits, bits);
511 logit("Warning: This may be due to an old implementation of ssh.");
514 /* Get protocol flags. */
515 server_flags = packet_get_int();
516 packet_set_protocol_flags(server_flags);
518 supported_ciphers = packet_get_int();
519 supported_authentications = packet_get_int();
522 debug("Received server public key (%d bits) and host key (%d bits).",
523 BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n));
525 if (verify_host_key(host, hostaddr, host_key) == -1)
526 fatal("Host key verification failed.");
528 client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN;
530 derive_ssh1_session_id(host_key->rsa->n, server_key->rsa->n, cookie, session_id);
532 /* Generate a session key. */
536 * Generate an encryption key for the session. The key is a 256 bit
537 * random number, interpreted as a 32-byte key, with the least
538 * significant 8 bits being the first byte of the key.
540 for (i = 0; i < 32; i++) {
543 session_key[i] = rnd & 0xff;
548 * According to the protocol spec, the first byte of the session key
549 * is the highest byte of the integer. The session key is xored with
550 * the first 16 bytes of the session id.
552 if ((key = BN_new()) == NULL)
553 fatal("respond_to_rsa_challenge: BN_new failed");
555 for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
556 BN_lshift(key, key, 8);
558 BN_add_word(key, session_key[i] ^ session_id[i]);
560 BN_add_word(key, session_key[i]);
564 * Encrypt the integer using the public key and host key of the
565 * server (key with smaller modulus first).
567 if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) {
568 /* Public key has smaller modulus. */
569 if (BN_num_bits(host_key->rsa->n) <
570 BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
571 fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
572 "SSH_KEY_BITS_RESERVED %d",
573 BN_num_bits(host_key->rsa->n),
574 BN_num_bits(server_key->rsa->n),
575 SSH_KEY_BITS_RESERVED);
577 rsa_public_encrypt(key, key, server_key->rsa);
578 rsa_public_encrypt(key, key, host_key->rsa);
580 /* Host key has smaller modulus (or they are equal). */
581 if (BN_num_bits(server_key->rsa->n) <
582 BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
583 fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
584 "SSH_KEY_BITS_RESERVED %d",
585 BN_num_bits(server_key->rsa->n),
586 BN_num_bits(host_key->rsa->n),
587 SSH_KEY_BITS_RESERVED);
589 rsa_public_encrypt(key, key, host_key->rsa);
590 rsa_public_encrypt(key, key, server_key->rsa);
593 /* Destroy the public keys since we no longer need them. */
594 key_free(server_key);
597 if (options.cipher == SSH_CIPHER_NOT_SET) {
598 if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default))
599 options.cipher = ssh_cipher_default;
600 } else if (options.cipher == SSH_CIPHER_INVALID ||
601 !(cipher_mask_ssh1(1) & (1 << options.cipher))) {
602 logit("No valid SSH1 cipher, using %.100s instead.",
603 cipher_name(ssh_cipher_default));
604 options.cipher = ssh_cipher_default;
606 /* Check that the selected cipher is supported. */
607 if (!(supported_ciphers & (1 << options.cipher)))
608 fatal("Selected cipher type %.100s not supported by server.",
609 cipher_name(options.cipher));
611 debug("Encryption type: %.100s", cipher_name(options.cipher));
613 /* Send the encrypted session key to the server. */
614 packet_start(SSH_CMSG_SESSION_KEY);
615 packet_put_char(options.cipher);
617 /* Send the cookie back to the server. */
618 for (i = 0; i < 8; i++)
619 packet_put_char(cookie[i]);
621 /* Send and destroy the encrypted encryption key integer. */
622 packet_put_bignum(key);
625 /* Send protocol flags. */
626 packet_put_int(client_flags);
628 /* Send the packet now. */
632 debug("Sent encrypted session key.");
634 /* Set the encryption key. */
635 packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher);
637 /* We will no longer need the session key here. Destroy any extra copies. */
638 memset(session_key, 0, sizeof(session_key));
641 * Expect a success message from the server. Note that this message
642 * will be received in encrypted form.
644 packet_read_expect(SSH_SMSG_SUCCESS);
646 debug("Received encrypted confirmation.");
653 ssh_userauth1(const char *local_user, const char *server_user, char *host,
654 Sensitive *sensitive)
658 if (supported_authentications == 0)
659 fatal("ssh_userauth1: server supports no auth methods");
661 /* Send the name of the user to log in as on the server. */
662 packet_start(SSH_CMSG_USER);
663 packet_put_cstring(server_user);
668 * The server should respond with success if no authentication is
669 * needed (the user has no password). Otherwise the server responds
672 type = packet_read();
674 /* check whether the connection was accepted without authentication. */
675 if (type == SSH_SMSG_SUCCESS)
677 if (type != SSH_SMSG_FAILURE)
678 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type);
681 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
684 if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) &&
685 options.rhosts_rsa_authentication) {
686 for (i = 0; i < sensitive->nkeys; i++) {
687 if (sensitive->keys[i] != NULL &&
688 sensitive->keys[i]->type == KEY_RSA1 &&
689 try_rhosts_rsa_authentication(local_user,
694 /* Try RSA authentication if the server supports it. */
695 if ((supported_authentications & (1 << SSH_AUTH_RSA)) &&
696 options.rsa_authentication) {
698 * Try RSA authentication using the authentication agent. The
699 * agent is tried first because no passphrase is needed for
700 * it, whereas identity files may require passphrases.
702 if (try_agent_authentication())
705 /* Try RSA authentication for each identity. */
706 for (i = 0; i < options.num_identity_files; i++)
707 if (options.identity_keys[i] != NULL &&
708 options.identity_keys[i]->type == KEY_RSA1 &&
709 try_rsa_authentication(i))
712 /* Try challenge response authentication if the server supports it. */
713 if ((supported_authentications & (1 << SSH_AUTH_TIS)) &&
714 options.challenge_response_authentication && !options.batch_mode) {
715 if (try_challenge_response_authentication())
718 /* Try password authentication if the server supports it. */
719 if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) &&
720 options.password_authentication && !options.batch_mode) {
723 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
725 if (try_password_authentication(prompt))
728 /* All authentication methods have failed. Exit with an error message. */
729 fatal("Permission denied.");
733 return; /* need statement after label */