1 /* $OpenBSD: sshconnect1.c,v 1.69 2006/08/03 03:34:42 deraadt 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 <sys/types.h>
19 #include <sys/socket.h>
21 #include <openssl/bn.h>
22 #include <openssl/md5.h>
44 #include "sshconnect.h"
51 /* Session id for the current session. */
52 u_char session_id
[16];
53 u_int supported_authentications
= 0;
55 extern Options options
;
56 extern char *__progname
;
59 * Checks if the user has an authentication agent, and if so, tries to
60 * authenticate using the agent.
63 try_agent_authentication(void)
67 AuthenticationConnection
*auth
;
73 /* Get connection to the agent. */
74 auth
= ssh_get_authentication_connection();
78 if ((challenge
= BN_new()) == NULL
)
79 fatal("try_agent_authentication: BN_new failed");
80 /* Loop through identities served by the agent. */
81 for (key
= ssh_get_first_identity(auth
, &comment
, 1);
83 key
= ssh_get_next_identity(auth
, &comment
, 1)) {
85 /* Try this identity. */
86 debug("Trying RSA authentication via agent with '%.100s'", comment
);
89 /* Tell the server that we are willing to authenticate using this key. */
90 packet_start(SSH_CMSG_AUTH_RSA
);
91 packet_put_bignum(key
->rsa
->n
);
95 /* Wait for server's response. */
98 /* The server sends failure if it doesn't like our key or
99 does not support RSA authentication. */
100 if (type
== SSH_SMSG_FAILURE
) {
101 debug("Server refused our key.");
105 /* Otherwise it should have sent a challenge. */
106 if (type
!= SSH_SMSG_AUTH_RSA_CHALLENGE
)
107 packet_disconnect("Protocol error during RSA authentication: %d",
110 packet_get_bignum(challenge
);
113 debug("Received RSA challenge from server.");
115 /* Ask the agent to decrypt the challenge. */
116 if (!ssh_decrypt_challenge(auth
, key
, challenge
, session_id
, 1, response
)) {
118 * The agent failed to authenticate this identifier
119 * although it advertised it supports this. Just
120 * return a wrong value.
122 logit("Authentication agent failed to decrypt challenge.");
123 memset(response
, 0, sizeof(response
));
126 debug("Sending response to RSA challenge.");
128 /* Send the decrypted challenge back to the server. */
129 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE
);
130 for (i
= 0; i
< 16; i
++)
131 packet_put_char(response
[i
]);
135 /* Wait for response from the server. */
136 type
= packet_read();
138 /* The server returns success if it accepted the authentication. */
139 if (type
== SSH_SMSG_SUCCESS
) {
140 ssh_close_authentication_connection(auth
);
141 BN_clear_free(challenge
);
142 debug("RSA authentication accepted by server.");
145 /* Otherwise it should return failure. */
146 if (type
!= SSH_SMSG_FAILURE
)
147 packet_disconnect("Protocol error waiting RSA auth response: %d",
150 ssh_close_authentication_connection(auth
);
151 BN_clear_free(challenge
);
152 debug("RSA authentication using agent refused.");
157 * Computes the proper response to a RSA challenge, and sends the response to
161 respond_to_rsa_challenge(BIGNUM
* challenge
, RSA
* prv
)
163 u_char buf
[32], response
[16];
167 /* Decrypt the challenge using the private key. */
168 /* XXX think about Bleichenbacher, too */
169 if (rsa_private_decrypt(challenge
, challenge
, prv
) <= 0)
171 "respond_to_rsa_challenge: rsa_private_decrypt failed");
173 /* Compute the response. */
174 /* The response is MD5 of decrypted challenge plus session id. */
175 len
= BN_num_bytes(challenge
);
176 if (len
<= 0 || (u_int
)len
> sizeof(buf
))
178 "respond_to_rsa_challenge: bad challenge length %d", len
);
180 memset(buf
, 0, sizeof(buf
));
181 BN_bn2bin(challenge
, buf
+ sizeof(buf
) - len
);
183 MD5_Update(&md
, buf
, 32);
184 MD5_Update(&md
, session_id
, 16);
185 MD5_Final(response
, &md
);
187 debug("Sending response to host key RSA challenge.");
189 /* Send the response back to the server. */
190 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE
);
191 for (i
= 0; i
< 16; i
++)
192 packet_put_char(response
[i
]);
196 memset(buf
, 0, sizeof(buf
));
197 memset(response
, 0, sizeof(response
));
198 memset(&md
, 0, sizeof(md
));
202 * Checks if the user has authentication file, and if so, tries to authenticate
206 try_rsa_authentication(int idx
)
209 Key
*public, *private;
210 char buf
[300], *passphrase
, *comment
, *authfile
;
211 int i
, perm_ok
= 1, type
, quit
;
213 public = options
.identity_keys
[idx
];
214 authfile
= options
.identity_files
[idx
];
215 comment
= xstrdup(authfile
);
217 debug("Trying RSA authentication with key '%.100s'", comment
);
219 /* Tell the server that we are willing to authenticate using this key. */
220 packet_start(SSH_CMSG_AUTH_RSA
);
221 packet_put_bignum(public->rsa
->n
);
225 /* Wait for server's response. */
226 type
= packet_read();
229 * The server responds with failure if it doesn't like our key or
230 * doesn't support RSA authentication.
232 if (type
== SSH_SMSG_FAILURE
) {
233 debug("Server refused our key.");
237 /* Otherwise, the server should respond with a challenge. */
238 if (type
!= SSH_SMSG_AUTH_RSA_CHALLENGE
)
239 packet_disconnect("Protocol error during RSA authentication: %d", type
);
241 /* Get the challenge from the packet. */
242 if ((challenge
= BN_new()) == NULL
)
243 fatal("try_rsa_authentication: BN_new failed");
244 packet_get_bignum(challenge
);
247 debug("Received RSA challenge from server.");
250 * If the key is not stored in external hardware, we have to
251 * load the private key. Try first with empty passphrase; if it
252 * fails, ask for a passphrase.
254 if (public->flags
& KEY_FLAG_EXT
)
257 private = key_load_private_type(KEY_RSA1
, authfile
, "", NULL
,
259 if (private == NULL
&& !options
.batch_mode
&& perm_ok
) {
260 snprintf(buf
, sizeof(buf
),
261 "Enter passphrase for RSA key '%.100s': ", comment
);
262 for (i
= 0; i
< options
.number_of_password_prompts
; i
++) {
263 passphrase
= read_passphrase(buf
, 0);
264 if (strcmp(passphrase
, "") != 0) {
265 private = key_load_private_type(KEY_RSA1
,
266 authfile
, passphrase
, NULL
, NULL
);
269 debug2("no passphrase given, try next key");
272 memset(passphrase
, 0, strlen(passphrase
));
274 if (private != NULL
|| quit
)
276 debug2("bad passphrase given, try again...");
279 /* We no longer need the comment. */
282 if (private == NULL
) {
283 if (!options
.batch_mode
&& perm_ok
)
284 error("Bad passphrase.");
286 /* Send a dummy response packet to avoid protocol error. */
287 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE
);
288 for (i
= 0; i
< 16; i
++)
293 /* Expect the server to reject it... */
294 packet_read_expect(SSH_SMSG_FAILURE
);
295 BN_clear_free(challenge
);
299 /* Compute and send a response to the challenge. */
300 respond_to_rsa_challenge(challenge
, private->rsa
);
302 /* Destroy the private key unless it in external hardware. */
303 if (!(private->flags
& KEY_FLAG_EXT
))
306 /* We no longer need the challenge. */
307 BN_clear_free(challenge
);
309 /* Wait for response from the server. */
310 type
= packet_read();
311 if (type
== SSH_SMSG_SUCCESS
) {
312 debug("RSA authentication accepted by server.");
315 if (type
!= SSH_SMSG_FAILURE
)
316 packet_disconnect("Protocol error waiting RSA auth response: %d", type
);
317 debug("RSA authentication refused.");
322 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
323 * authentication and RSA host authentication.
326 try_rhosts_rsa_authentication(const char *local_user
, Key
* host_key
)
331 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
333 /* Tell the server that we are willing to authenticate using this key. */
334 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA
);
335 packet_put_cstring(local_user
);
336 packet_put_int(BN_num_bits(host_key
->rsa
->n
));
337 packet_put_bignum(host_key
->rsa
->e
);
338 packet_put_bignum(host_key
->rsa
->n
);
342 /* Wait for server's response. */
343 type
= packet_read();
345 /* The server responds with failure if it doesn't admit our
346 .rhosts authentication or doesn't know our host key. */
347 if (type
== SSH_SMSG_FAILURE
) {
348 debug("Server refused our rhosts authentication or host key.");
351 /* Otherwise, the server should respond with a challenge. */
352 if (type
!= SSH_SMSG_AUTH_RSA_CHALLENGE
)
353 packet_disconnect("Protocol error during RSA authentication: %d", type
);
355 /* Get the challenge from the packet. */
356 if ((challenge
= BN_new()) == NULL
)
357 fatal("try_rhosts_rsa_authentication: BN_new failed");
358 packet_get_bignum(challenge
);
361 debug("Received RSA challenge for host key from server.");
363 /* Compute a response to the challenge. */
364 respond_to_rsa_challenge(challenge
, host_key
->rsa
);
366 /* We no longer need the challenge. */
367 BN_clear_free(challenge
);
369 /* Wait for response from the server. */
370 type
= packet_read();
371 if (type
== SSH_SMSG_SUCCESS
) {
372 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
375 if (type
!= SSH_SMSG_FAILURE
)
376 packet_disconnect("Protocol error waiting RSA auth response: %d", type
);
377 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
382 * Tries to authenticate with any string-based challenge/response system.
383 * Note that the client code is not tied to s/key or TIS.
386 try_challenge_response_authentication(void)
391 char *challenge
, *response
;
393 debug("Doing challenge response authentication.");
395 for (i
= 0; i
< options
.number_of_password_prompts
; i
++) {
396 /* request a challenge */
397 packet_start(SSH_CMSG_AUTH_TIS
);
401 type
= packet_read();
402 if (type
!= SSH_SMSG_FAILURE
&&
403 type
!= SSH_SMSG_AUTH_TIS_CHALLENGE
) {
404 packet_disconnect("Protocol error: got %d in response "
405 "to SSH_CMSG_AUTH_TIS", type
);
407 if (type
!= SSH_SMSG_AUTH_TIS_CHALLENGE
) {
408 debug("No challenge.");
411 challenge
= packet_get_string(&clen
);
413 snprintf(prompt
, sizeof prompt
, "%s%s", challenge
,
414 strchr(challenge
, '\n') ? "" : "\nResponse: ");
417 error("Permission denied, please try again.");
418 if (options
.cipher
== SSH_CIPHER_NONE
)
419 logit("WARNING: Encryption is disabled! "
420 "Response will be transmitted in clear text.");
421 response
= read_passphrase(prompt
, 0);
422 if (strcmp(response
, "") == 0) {
426 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE
);
427 ssh_put_password(response
);
428 memset(response
, 0, strlen(response
));
432 type
= packet_read();
433 if (type
== SSH_SMSG_SUCCESS
)
435 if (type
!= SSH_SMSG_FAILURE
)
436 packet_disconnect("Protocol error: got %d in response "
437 "to SSH_CMSG_AUTH_TIS_RESPONSE", type
);
444 * Tries to authenticate with plain passwd authentication.
447 try_password_authentication(char *prompt
)
452 debug("Doing password authentication.");
453 if (options
.cipher
== SSH_CIPHER_NONE
)
454 logit("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
455 for (i
= 0; i
< options
.number_of_password_prompts
; i
++) {
457 error("Permission denied, please try again.");
458 password
= read_passphrase(prompt
, 0);
459 packet_start(SSH_CMSG_AUTH_PASSWORD
);
460 ssh_put_password(password
);
461 memset(password
, 0, strlen(password
));
466 type
= packet_read();
467 if (type
== SSH_SMSG_SUCCESS
)
469 if (type
!= SSH_SMSG_FAILURE
)
470 packet_disconnect("Protocol error: got %d in response to passwd auth", type
);
480 ssh_kex(char *host
, struct sockaddr
*hostaddr
)
484 Key
*host_key
, *server_key
;
486 int ssh_cipher_default
= SSH_CIPHER_3DES
;
487 u_char session_key
[SSH_SESSION_KEY_LENGTH
];
489 u_int supported_ciphers
;
490 u_int server_flags
, client_flags
;
493 debug("Waiting for server public key.");
495 /* Wait for a public key packet from the server. */
496 packet_read_expect(SSH_SMSG_PUBLIC_KEY
);
498 /* Get cookie from the packet. */
499 for (i
= 0; i
< 8; i
++)
500 cookie
[i
] = packet_get_char();
502 /* Get the public key. */
503 server_key
= key_new(KEY_RSA1
);
504 bits
= packet_get_int();
505 packet_get_bignum(server_key
->rsa
->e
);
506 packet_get_bignum(server_key
->rsa
->n
);
508 rbits
= BN_num_bits(server_key
->rsa
->n
);
510 logit("Warning: Server lies about size of server public key: "
511 "actual size is %d bits vs. announced %d.", rbits
, bits
);
512 logit("Warning: This may be due to an old implementation of ssh.");
514 /* Get the host key. */
515 host_key
= key_new(KEY_RSA1
);
516 bits
= packet_get_int();
517 packet_get_bignum(host_key
->rsa
->e
);
518 packet_get_bignum(host_key
->rsa
->n
);
520 rbits
= BN_num_bits(host_key
->rsa
->n
);
522 logit("Warning: Server lies about size of server host key: "
523 "actual size is %d bits vs. announced %d.", rbits
, bits
);
524 logit("Warning: This may be due to an old implementation of ssh.");
527 /* Get protocol flags. */
528 server_flags
= packet_get_int();
529 packet_set_protocol_flags(server_flags
);
531 supported_ciphers
= packet_get_int();
532 supported_authentications
= packet_get_int();
535 debug("Received server public key (%d bits) and host key (%d bits).",
536 BN_num_bits(server_key
->rsa
->n
), BN_num_bits(host_key
->rsa
->n
));
538 if (verify_host_key(host
, hostaddr
, host_key
) == -1)
539 fatal("Host key verification failed.");
541 client_flags
= SSH_PROTOFLAG_SCREEN_NUMBER
| SSH_PROTOFLAG_HOST_IN_FWD_OPEN
;
543 derive_ssh1_session_id(host_key
->rsa
->n
, server_key
->rsa
->n
, cookie
, session_id
);
545 /* Generate a session key. */
549 * Generate an encryption key for the session. The key is a 256 bit
550 * random number, interpreted as a 32-byte key, with the least
551 * significant 8 bits being the first byte of the key.
553 for (i
= 0; i
< 32; i
++) {
556 session_key
[i
] = rnd
& 0xff;
561 * According to the protocol spec, the first byte of the session key
562 * is the highest byte of the integer. The session key is xored with
563 * the first 16 bytes of the session id.
565 if ((key
= BN_new()) == NULL
)
566 fatal("respond_to_rsa_challenge: BN_new failed");
568 for (i
= 0; i
< SSH_SESSION_KEY_LENGTH
; i
++) {
569 BN_lshift(key
, key
, 8);
571 BN_add_word(key
, session_key
[i
] ^ session_id
[i
]);
573 BN_add_word(key
, session_key
[i
]);
577 * Encrypt the integer using the public key and host key of the
578 * server (key with smaller modulus first).
580 if (BN_cmp(server_key
->rsa
->n
, host_key
->rsa
->n
) < 0) {
581 /* Public key has smaller modulus. */
582 if (BN_num_bits(host_key
->rsa
->n
) <
583 BN_num_bits(server_key
->rsa
->n
) + SSH_KEY_BITS_RESERVED
) {
584 fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
585 "SSH_KEY_BITS_RESERVED %d",
586 BN_num_bits(host_key
->rsa
->n
),
587 BN_num_bits(server_key
->rsa
->n
),
588 SSH_KEY_BITS_RESERVED
);
590 rsa_public_encrypt(key
, key
, server_key
->rsa
);
591 rsa_public_encrypt(key
, key
, host_key
->rsa
);
593 /* Host key has smaller modulus (or they are equal). */
594 if (BN_num_bits(server_key
->rsa
->n
) <
595 BN_num_bits(host_key
->rsa
->n
) + SSH_KEY_BITS_RESERVED
) {
596 fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
597 "SSH_KEY_BITS_RESERVED %d",
598 BN_num_bits(server_key
->rsa
->n
),
599 BN_num_bits(host_key
->rsa
->n
),
600 SSH_KEY_BITS_RESERVED
);
602 rsa_public_encrypt(key
, key
, host_key
->rsa
);
603 rsa_public_encrypt(key
, key
, server_key
->rsa
);
606 /* Destroy the public keys since we no longer need them. */
607 key_free(server_key
);
610 if (options
.cipher
== SSH_CIPHER_NOT_SET
) {
611 if (cipher_mask_ssh1(1) & supported_ciphers
& (1 << ssh_cipher_default
))
612 options
.cipher
= ssh_cipher_default
;
613 } else if (options
.cipher
== SSH_CIPHER_INVALID
||
614 !(cipher_mask_ssh1(1) & (1 << options
.cipher
))) {
615 logit("No valid SSH1 cipher, using %.100s instead.",
616 cipher_name(ssh_cipher_default
));
617 options
.cipher
= ssh_cipher_default
;
619 /* Check that the selected cipher is supported. */
620 if (!(supported_ciphers
& (1 << options
.cipher
)))
621 fatal("Selected cipher type %.100s not supported by server.",
622 cipher_name(options
.cipher
));
624 debug("Encryption type: %.100s", cipher_name(options
.cipher
));
626 /* Send the encrypted session key to the server. */
627 packet_start(SSH_CMSG_SESSION_KEY
);
628 packet_put_char(options
.cipher
);
630 /* Send the cookie back to the server. */
631 for (i
= 0; i
< 8; i
++)
632 packet_put_char(cookie
[i
]);
634 /* Send and destroy the encrypted encryption key integer. */
635 packet_put_bignum(key
);
638 /* Send protocol flags. */
639 packet_put_int(client_flags
);
641 /* Send the packet now. */
645 debug("Sent encrypted session key.");
647 /* Set the encryption key. */
648 packet_set_encryption_key(session_key
, SSH_SESSION_KEY_LENGTH
, options
.cipher
);
650 /* We will no longer need the session key here. Destroy any extra copies. */
651 memset(session_key
, 0, sizeof(session_key
));
654 * Expect a success message from the server. Note that this message
655 * will be received in encrypted form.
657 packet_read_expect(SSH_SMSG_SUCCESS
);
659 debug("Received encrypted confirmation.");
666 ssh_userauth1(const char *local_user
, const char *server_user
, char *host
,
667 Sensitive
*sensitive
)
671 if (supported_authentications
== 0)
672 fatal("ssh_userauth1: server supports no auth methods");
674 /* Send the name of the user to log in as on the server. */
675 packet_start(SSH_CMSG_USER
);
676 packet_put_cstring(server_user
);
681 * The server should respond with success if no authentication is
682 * needed (the user has no password). Otherwise the server responds
685 type
= packet_read();
687 /* check whether the connection was accepted without authentication. */
688 if (type
== SSH_SMSG_SUCCESS
)
690 if (type
!= SSH_SMSG_FAILURE
)
691 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type
);
694 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
697 if ((supported_authentications
& (1 << SSH_AUTH_RHOSTS_RSA
)) &&
698 options
.rhosts_rsa_authentication
) {
699 for (i
= 0; i
< sensitive
->nkeys
; i
++) {
700 if (sensitive
->keys
[i
] != NULL
&&
701 sensitive
->keys
[i
]->type
== KEY_RSA1
&&
702 try_rhosts_rsa_authentication(local_user
,
707 /* Try RSA authentication if the server supports it. */
708 if ((supported_authentications
& (1 << SSH_AUTH_RSA
)) &&
709 options
.rsa_authentication
) {
711 * Try RSA authentication using the authentication agent. The
712 * agent is tried first because no passphrase is needed for
713 * it, whereas identity files may require passphrases.
715 if (try_agent_authentication())
718 /* Try RSA authentication for each identity. */
719 for (i
= 0; i
< options
.num_identity_files
; i
++)
720 if (options
.identity_keys
[i
] != NULL
&&
721 options
.identity_keys
[i
]->type
== KEY_RSA1
&&
722 try_rsa_authentication(i
))
725 /* Try challenge response authentication if the server supports it. */
726 if ((supported_authentications
& (1 << SSH_AUTH_TIS
)) &&
727 options
.challenge_response_authentication
&& !options
.batch_mode
) {
728 if (try_challenge_response_authentication())
731 /* Try password authentication if the server supports it. */
732 if ((supported_authentications
& (1 << SSH_AUTH_PASSWORD
)) &&
733 options
.password_authentication
&& !options
.batch_mode
) {
736 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password: ",
738 if (try_password_authentication(prompt
))
741 /* All authentication methods have failed. Exit with an error message. */
742 fatal("Permission denied.");
746 return; /* need statement after label */