1 /* $OpenBSD: sshconnect1.c,v 1.65 2006/04/25 08:02:27 dtucker 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>
33 #include "sshconnect.h"
40 /* Session id for the current session. */
41 u_char session_id
[16];
42 u_int supported_authentications
= 0;
44 extern Options options
;
45 extern char *__progname
;
48 * Checks if the user has an authentication agent, and if so, tries to
49 * authenticate using the agent.
52 try_agent_authentication(void)
56 AuthenticationConnection
*auth
;
62 /* Get connection to the agent. */
63 auth
= ssh_get_authentication_connection();
67 if ((challenge
= BN_new()) == NULL
)
68 fatal("try_agent_authentication: BN_new failed");
69 /* Loop through identities served by the agent. */
70 for (key
= ssh_get_first_identity(auth
, &comment
, 1);
72 key
= ssh_get_next_identity(auth
, &comment
, 1)) {
74 /* Try this identity. */
75 debug("Trying RSA authentication via agent with '%.100s'", comment
);
78 /* Tell the server that we are willing to authenticate using this key. */
79 packet_start(SSH_CMSG_AUTH_RSA
);
80 packet_put_bignum(key
->rsa
->n
);
84 /* Wait for server's response. */
87 /* The server sends failure if it doesn't like our key or
88 does not support RSA authentication. */
89 if (type
== SSH_SMSG_FAILURE
) {
90 debug("Server refused our key.");
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",
99 packet_get_bignum(challenge
);
102 debug("Received RSA challenge from server.");
104 /* Ask the agent to decrypt the challenge. */
105 if (!ssh_decrypt_challenge(auth
, key
, challenge
, session_id
, 1, response
)) {
107 * The agent failed to authenticate this identifier
108 * although it advertised it supports this. Just
109 * return a wrong value.
111 logit("Authentication agent failed to decrypt challenge.");
112 memset(response
, 0, sizeof(response
));
115 debug("Sending response to RSA challenge.");
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
]);
124 /* Wait for response from the server. */
125 type
= packet_read();
127 /* The server returns success if it accepted the authentication. */
128 if (type
== SSH_SMSG_SUCCESS
) {
129 ssh_close_authentication_connection(auth
);
130 BN_clear_free(challenge
);
131 debug("RSA authentication accepted by server.");
134 /* Otherwise it should return failure. */
135 if (type
!= SSH_SMSG_FAILURE
)
136 packet_disconnect("Protocol error waiting RSA auth response: %d",
139 ssh_close_authentication_connection(auth
);
140 BN_clear_free(challenge
);
141 debug("RSA authentication using agent refused.");
146 * Computes the proper response to a RSA challenge, and sends the response to
150 respond_to_rsa_challenge(BIGNUM
* challenge
, RSA
* prv
)
152 u_char buf
[32], response
[16];
156 /* Decrypt the challenge using the private key. */
157 /* XXX think about Bleichenbacher, too */
158 if (rsa_private_decrypt(challenge
, challenge
, prv
) <= 0)
160 "respond_to_rsa_challenge: rsa_private_decrypt failed");
162 /* Compute the response. */
163 /* The response is MD5 of decrypted challenge plus session id. */
164 len
= BN_num_bytes(challenge
);
165 if (len
<= 0 || (u_int
)len
> sizeof(buf
))
167 "respond_to_rsa_challenge: bad challenge length %d", len
);
169 memset(buf
, 0, sizeof(buf
));
170 BN_bn2bin(challenge
, buf
+ sizeof(buf
) - len
);
172 MD5_Update(&md
, buf
, 32);
173 MD5_Update(&md
, session_id
, 16);
174 MD5_Final(response
, &md
);
176 debug("Sending response to host key RSA challenge.");
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
]);
185 memset(buf
, 0, sizeof(buf
));
186 memset(response
, 0, sizeof(response
));
187 memset(&md
, 0, sizeof(md
));
191 * Checks if the user has authentication file, and if so, tries to authenticate
195 try_rsa_authentication(int idx
)
198 Key
*public, *private;
199 char buf
[300], *passphrase
, *comment
, *authfile
;
200 int i
, perm_ok
= 1, type
, quit
;
202 public = options
.identity_keys
[idx
];
203 authfile
= options
.identity_files
[idx
];
204 comment
= xstrdup(authfile
);
206 debug("Trying RSA authentication with key '%.100s'", comment
);
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
);
214 /* Wait for server's response. */
215 type
= packet_read();
218 * The server responds with failure if it doesn't like our key or
219 * doesn't support RSA authentication.
221 if (type
== SSH_SMSG_FAILURE
) {
222 debug("Server refused our key.");
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
);
230 /* Get the challenge from the packet. */
231 if ((challenge
= BN_new()) == NULL
)
232 fatal("try_rsa_authentication: BN_new failed");
233 packet_get_bignum(challenge
);
236 debug("Received RSA challenge from server.");
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
241 * fails, ask for a passphrase.
243 if (public->flags
& KEY_FLAG_EXT
)
246 private = key_load_private_type(KEY_RSA1
, authfile
, "", NULL
,
248 if (private == NULL
&& !options
.batch_mode
&& perm_ok
) {
249 snprintf(buf
, sizeof(buf
),
250 "Enter passphrase for RSA key '%.100s': ", comment
);
251 for (i
= 0; i
< options
.number_of_password_prompts
; i
++) {
252 passphrase
= read_passphrase(buf
, 0);
253 if (strcmp(passphrase
, "") != 0) {
254 private = key_load_private_type(KEY_RSA1
,
255 authfile
, passphrase
, NULL
, NULL
);
258 debug2("no passphrase given, try next key");
261 memset(passphrase
, 0, strlen(passphrase
));
263 if (private != NULL
|| quit
)
265 debug2("bad passphrase given, try again...");
268 /* We no longer need the comment. */
271 if (private == NULL
) {
272 if (!options
.batch_mode
&& perm_ok
)
273 error("Bad passphrase.");
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
++)
282 /* Expect the server to reject it... */
283 packet_read_expect(SSH_SMSG_FAILURE
);
284 BN_clear_free(challenge
);
288 /* Compute and send a response to the challenge. */
289 respond_to_rsa_challenge(challenge
, private->rsa
);
291 /* Destroy the private key unless it in external hardware. */
292 if (!(private->flags
& KEY_FLAG_EXT
))
295 /* We no longer need the challenge. */
296 BN_clear_free(challenge
);
298 /* Wait for response from the server. */
299 type
= packet_read();
300 if (type
== SSH_SMSG_SUCCESS
) {
301 debug("RSA authentication accepted by server.");
304 if (type
!= SSH_SMSG_FAILURE
)
305 packet_disconnect("Protocol error waiting RSA auth response: %d", type
);
306 debug("RSA authentication refused.");
311 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
312 * authentication and RSA host authentication.
315 try_rhosts_rsa_authentication(const char *local_user
, Key
* host_key
)
320 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
322 /* Tell the server that we are willing to authenticate using this key. */
323 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA
);
324 packet_put_cstring(local_user
);
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
);
331 /* Wait for server's response. */
332 type
= packet_read();
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.");
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
);
344 /* Get the challenge from the packet. */
345 if ((challenge
= BN_new()) == NULL
)
346 fatal("try_rhosts_rsa_authentication: BN_new failed");
347 packet_get_bignum(challenge
);
350 debug("Received RSA challenge for host key from server.");
352 /* Compute a response to the challenge. */
353 respond_to_rsa_challenge(challenge
, host_key
->rsa
);
355 /* We no longer need the challenge. */
356 BN_clear_free(challenge
);
358 /* Wait for response from the server. */
359 type
= packet_read();
360 if (type
== SSH_SMSG_SUCCESS
) {
361 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
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.");
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.
375 try_challenge_response_authentication(void)
380 char *challenge
, *response
;
382 debug("Doing challenge response authentication.");
384 for (i
= 0; i
< options
.number_of_password_prompts
; i
++) {
385 /* request a challenge */
386 packet_start(SSH_CMSG_AUTH_TIS
);
390 type
= packet_read();
391 if (type
!= SSH_SMSG_FAILURE
&&
392 type
!= SSH_SMSG_AUTH_TIS_CHALLENGE
) {
393 packet_disconnect("Protocol error: got %d in response "
394 "to SSH_CMSG_AUTH_TIS", type
);
396 if (type
!= SSH_SMSG_AUTH_TIS_CHALLENGE
) {
397 debug("No challenge.");
400 challenge
= packet_get_string(&clen
);
402 snprintf(prompt
, sizeof prompt
, "%s%s", challenge
,
403 strchr(challenge
, '\n') ? "" : "\nResponse: ");
406 error("Permission denied, please try again.");
407 if (options
.cipher
== SSH_CIPHER_NONE
)
408 logit("WARNING: Encryption is disabled! "
409 "Response will be transmitted in clear text.");
410 response
= read_passphrase(prompt
, 0);
411 if (strcmp(response
, "") == 0) {
415 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE
);
416 ssh_put_password(response
);
417 memset(response
, 0, strlen(response
));
421 type
= packet_read();
422 if (type
== SSH_SMSG_SUCCESS
)
424 if (type
!= SSH_SMSG_FAILURE
)
425 packet_disconnect("Protocol error: got %d in response "
426 "to SSH_CMSG_AUTH_TIS_RESPONSE", type
);
433 * Tries to authenticate with plain passwd authentication.
436 try_password_authentication(char *prompt
)
441 debug("Doing password authentication.");
442 if (options
.cipher
== SSH_CIPHER_NONE
)
443 logit("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
444 for (i
= 0; i
< options
.number_of_password_prompts
; i
++) {
446 error("Permission denied, please try again.");
447 password
= read_passphrase(prompt
, 0);
448 packet_start(SSH_CMSG_AUTH_PASSWORD
);
449 ssh_put_password(password
);
450 memset(password
, 0, strlen(password
));
455 type
= packet_read();
456 if (type
== SSH_SMSG_SUCCESS
)
458 if (type
!= SSH_SMSG_FAILURE
)
459 packet_disconnect("Protocol error: got %d in response to passwd auth", type
);
469 ssh_kex(char *host
, struct sockaddr
*hostaddr
)
473 Key
*host_key
, *server_key
;
475 int ssh_cipher_default
= SSH_CIPHER_3DES
;
476 u_char session_key
[SSH_SESSION_KEY_LENGTH
];
478 u_int supported_ciphers
;
479 u_int server_flags
, client_flags
;
482 debug("Waiting for server public key.");
484 /* Wait for a public key packet from the server. */
485 packet_read_expect(SSH_SMSG_PUBLIC_KEY
);
487 /* Get cookie from the packet. */
488 for (i
= 0; i
< 8; i
++)
489 cookie
[i
] = packet_get_char();
491 /* Get the public key. */
492 server_key
= key_new(KEY_RSA1
);
493 bits
= packet_get_int();
494 packet_get_bignum(server_key
->rsa
->e
);
495 packet_get_bignum(server_key
->rsa
->n
);
497 rbits
= BN_num_bits(server_key
->rsa
->n
);
499 logit("Warning: Server lies about size of server public key: "
500 "actual size is %d bits vs. announced %d.", rbits
, bits
);
501 logit("Warning: This may be due to an old implementation of ssh.");
503 /* Get the host key. */
504 host_key
= key_new(KEY_RSA1
);
505 bits
= packet_get_int();
506 packet_get_bignum(host_key
->rsa
->e
);
507 packet_get_bignum(host_key
->rsa
->n
);
509 rbits
= BN_num_bits(host_key
->rsa
->n
);
511 logit("Warning: Server lies about size of server host key: "
512 "actual size is %d bits vs. announced %d.", rbits
, bits
);
513 logit("Warning: This may be due to an old implementation of ssh.");
516 /* Get protocol flags. */
517 server_flags
= packet_get_int();
518 packet_set_protocol_flags(server_flags
);
520 supported_ciphers
= packet_get_int();
521 supported_authentications
= packet_get_int();
524 debug("Received server public key (%d bits) and host key (%d bits).",
525 BN_num_bits(server_key
->rsa
->n
), BN_num_bits(host_key
->rsa
->n
));
527 if (verify_host_key(host
, hostaddr
, host_key
) == -1)
528 fatal("Host key verification failed.");
530 client_flags
= SSH_PROTOFLAG_SCREEN_NUMBER
| SSH_PROTOFLAG_HOST_IN_FWD_OPEN
;
532 derive_ssh1_session_id(host_key
->rsa
->n
, server_key
->rsa
->n
, cookie
, session_id
);
534 /* Generate a session key. */
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.
542 for (i
= 0; i
< 32; i
++) {
545 session_key
[i
] = rnd
& 0xff;
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.
554 if ((key
= BN_new()) == NULL
)
555 fatal("respond_to_rsa_challenge: BN_new failed");
557 for (i
= 0; i
< SSH_SESSION_KEY_LENGTH
; i
++) {
558 BN_lshift(key
, key
, 8);
560 BN_add_word(key
, session_key
[i
] ^ session_id
[i
]);
562 BN_add_word(key
, session_key
[i
]);
566 * Encrypt the integer using the public key and host key of the
567 * server (key with smaller modulus first).
569 if (BN_cmp(server_key
->rsa
->n
, host_key
->rsa
->n
) < 0) {
570 /* Public key has smaller modulus. */
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 + "
574 "SSH_KEY_BITS_RESERVED %d",
575 BN_num_bits(host_key
->rsa
->n
),
576 BN_num_bits(server_key
->rsa
->n
),
577 SSH_KEY_BITS_RESERVED
);
579 rsa_public_encrypt(key
, key
, server_key
->rsa
);
580 rsa_public_encrypt(key
, key
, host_key
->rsa
);
582 /* Host key has smaller modulus (or they are equal). */
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 + "
586 "SSH_KEY_BITS_RESERVED %d",
587 BN_num_bits(server_key
->rsa
->n
),
588 BN_num_bits(host_key
->rsa
->n
),
589 SSH_KEY_BITS_RESERVED
);
591 rsa_public_encrypt(key
, key
, host_key
->rsa
);
592 rsa_public_encrypt(key
, key
, server_key
->rsa
);
595 /* Destroy the public keys since we no longer need them. */
596 key_free(server_key
);
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
;
602 } else if (options
.cipher
== SSH_CIPHER_INVALID
||
603 !(cipher_mask_ssh1(1) & (1 << options
.cipher
))) {
604 logit("No valid SSH1 cipher, using %.100s instead.",
605 cipher_name(ssh_cipher_default
));
606 options
.cipher
= ssh_cipher_default
;
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.",
611 cipher_name(options
.cipher
));
613 debug("Encryption type: %.100s", cipher_name(options
.cipher
));
615 /* Send the encrypted session key to the server. */
616 packet_start(SSH_CMSG_SESSION_KEY
);
617 packet_put_char(options
.cipher
);
619 /* Send the cookie back to the server. */
620 for (i
= 0; i
< 8; i
++)
621 packet_put_char(cookie
[i
]);
623 /* Send and destroy the encrypted encryption key integer. */
624 packet_put_bignum(key
);
627 /* Send protocol flags. */
628 packet_put_int(client_flags
);
630 /* Send the packet now. */
634 debug("Sent encrypted session key.");
636 /* Set the encryption key. */
637 packet_set_encryption_key(session_key
, SSH_SESSION_KEY_LENGTH
, options
.cipher
);
639 /* We will no longer need the session key here. Destroy any extra copies. */
640 memset(session_key
, 0, sizeof(session_key
));
643 * Expect a success message from the server. Note that this message
644 * will be received in encrypted form.
646 packet_read_expect(SSH_SMSG_SUCCESS
);
648 debug("Received encrypted confirmation.");
655 ssh_userauth1(const char *local_user
, const char *server_user
, char *host
,
656 Sensitive
*sensitive
)
660 if (supported_authentications
== 0)
661 fatal("ssh_userauth1: server supports no auth methods");
663 /* Send the name of the user to log in as on the server. */
664 packet_start(SSH_CMSG_USER
);
665 packet_put_cstring(server_user
);
670 * The server should respond with success if no authentication is
671 * needed (the user has no password). Otherwise the server responds
674 type
= packet_read();
676 /* check whether the connection was accepted without authentication. */
677 if (type
== SSH_SMSG_SUCCESS
)
679 if (type
!= SSH_SMSG_FAILURE
)
680 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type
);
683 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
686 if ((supported_authentications
& (1 << SSH_AUTH_RHOSTS_RSA
)) &&
687 options
.rhosts_rsa_authentication
) {
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
,
696 /* Try RSA authentication if the server supports it. */
697 if ((supported_authentications
& (1 << SSH_AUTH_RSA
)) &&
698 options
.rsa_authentication
) {
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.
704 if (try_agent_authentication())
707 /* Try RSA authentication for each identity. */
708 for (i
= 0; i
< options
.num_identity_files
; i
++)
709 if (options
.identity_keys
[i
] != NULL
&&
710 options
.identity_keys
[i
]->type
== KEY_RSA1
&&
711 try_rsa_authentication(i
))
714 /* Try challenge response authentication if the server supports it. */
715 if ((supported_authentications
& (1 << SSH_AUTH_TIS
)) &&
716 options
.challenge_response_authentication
&& !options
.batch_mode
) {
717 if (try_challenge_response_authentication())
720 /* Try password authentication if the server supports it. */
721 if ((supported_authentications
& (1 << SSH_AUTH_PASSWORD
)) &&
722 options
.password_authentication
&& !options
.batch_mode
) {
725 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password: ",
727 if (try_password_authentication(prompt
))
730 /* All authentication methods have failed. Exit with an error message. */
731 fatal("Permission denied.");
735 return; /* need statement after label */