1 /* $OpenBSD: sshconnect1.c,v 1.76 2014/07/15 15:54:14 millert 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>
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 explicit_bzero(response
, 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];
164 struct ssh_digest_ctx
*md
;
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
);
182 if ((md
= ssh_digest_start(SSH_DIGEST_MD5
)) == NULL
||
183 ssh_digest_update(md
, buf
, 32) < 0 ||
184 ssh_digest_update(md
, session_id
, 16) < 0 ||
185 ssh_digest_final(md
, response
, sizeof(response
)) < 0)
186 fatal("%s: md5 failed", __func__
);
189 debug("Sending response to host key RSA challenge.");
191 /* Send the response back to the server. */
192 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE
);
193 for (i
= 0; i
< 16; i
++)
194 packet_put_char(response
[i
]);
198 explicit_bzero(buf
, sizeof(buf
));
199 explicit_bzero(response
, sizeof(response
));
200 explicit_bzero(&md
, sizeof(md
));
204 * Checks if the user has authentication file, and if so, tries to authenticate
208 try_rsa_authentication(int idx
)
211 Key
*public, *private;
212 char buf
[300], *passphrase
, *comment
, *authfile
;
213 int i
, perm_ok
= 1, type
, quit
;
215 public = options
.identity_keys
[idx
];
216 authfile
= options
.identity_files
[idx
];
217 comment
= xstrdup(authfile
);
219 debug("Trying RSA authentication with key '%.100s'", comment
);
221 /* Tell the server that we are willing to authenticate using this key. */
222 packet_start(SSH_CMSG_AUTH_RSA
);
223 packet_put_bignum(public->rsa
->n
);
227 /* Wait for server's response. */
228 type
= packet_read();
231 * The server responds with failure if it doesn't like our key or
232 * doesn't support RSA authentication.
234 if (type
== SSH_SMSG_FAILURE
) {
235 debug("Server refused our key.");
239 /* Otherwise, the server should respond with a challenge. */
240 if (type
!= SSH_SMSG_AUTH_RSA_CHALLENGE
)
241 packet_disconnect("Protocol error during RSA authentication: %d", type
);
243 /* Get the challenge from the packet. */
244 if ((challenge
= BN_new()) == NULL
)
245 fatal("try_rsa_authentication: BN_new failed");
246 packet_get_bignum(challenge
);
249 debug("Received RSA challenge from server.");
252 * If the key is not stored in external hardware, we have to
253 * load the private key. Try first with empty passphrase; if it
254 * fails, ask for a passphrase.
256 if (public->flags
& SSHKEY_FLAG_EXT
)
259 private = key_load_private_type(KEY_RSA1
, authfile
, "", NULL
,
261 if (private == NULL
&& !options
.batch_mode
&& perm_ok
) {
262 snprintf(buf
, sizeof(buf
),
263 "Enter passphrase for RSA key '%.100s': ", comment
);
264 for (i
= 0; i
< options
.number_of_password_prompts
; i
++) {
265 passphrase
= read_passphrase(buf
, 0);
266 if (strcmp(passphrase
, "") != 0) {
267 private = key_load_private_type(KEY_RSA1
,
268 authfile
, passphrase
, NULL
, NULL
);
271 debug2("no passphrase given, try next key");
274 explicit_bzero(passphrase
, strlen(passphrase
));
276 if (private != NULL
|| quit
)
278 debug2("bad passphrase given, try again...");
281 /* We no longer need the comment. */
284 if (private == NULL
) {
285 if (!options
.batch_mode
&& perm_ok
)
286 error("Bad passphrase.");
288 /* Send a dummy response packet to avoid protocol error. */
289 packet_start(SSH_CMSG_AUTH_RSA_RESPONSE
);
290 for (i
= 0; i
< 16; i
++)
295 /* Expect the server to reject it... */
296 packet_read_expect(SSH_SMSG_FAILURE
);
297 BN_clear_free(challenge
);
301 /* Compute and send a response to the challenge. */
302 respond_to_rsa_challenge(challenge
, private->rsa
);
304 /* Destroy the private key unless it in external hardware. */
305 if (!(private->flags
& SSHKEY_FLAG_EXT
))
308 /* We no longer need the challenge. */
309 BN_clear_free(challenge
);
311 /* Wait for response from the server. */
312 type
= packet_read();
313 if (type
== SSH_SMSG_SUCCESS
) {
314 debug("RSA authentication accepted by server.");
317 if (type
!= SSH_SMSG_FAILURE
)
318 packet_disconnect("Protocol error waiting RSA auth response: %d", type
);
319 debug("RSA authentication refused.");
324 * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv
325 * authentication and RSA host authentication.
328 try_rhosts_rsa_authentication(const char *local_user
, Key
* host_key
)
333 debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication.");
335 /* Tell the server that we are willing to authenticate using this key. */
336 packet_start(SSH_CMSG_AUTH_RHOSTS_RSA
);
337 packet_put_cstring(local_user
);
338 packet_put_int(BN_num_bits(host_key
->rsa
->n
));
339 packet_put_bignum(host_key
->rsa
->e
);
340 packet_put_bignum(host_key
->rsa
->n
);
344 /* Wait for server's response. */
345 type
= packet_read();
347 /* The server responds with failure if it doesn't admit our
348 .rhosts authentication or doesn't know our host key. */
349 if (type
== SSH_SMSG_FAILURE
) {
350 debug("Server refused our rhosts authentication or host key.");
353 /* Otherwise, the server should respond with a challenge. */
354 if (type
!= SSH_SMSG_AUTH_RSA_CHALLENGE
)
355 packet_disconnect("Protocol error during RSA authentication: %d", type
);
357 /* Get the challenge from the packet. */
358 if ((challenge
= BN_new()) == NULL
)
359 fatal("try_rhosts_rsa_authentication: BN_new failed");
360 packet_get_bignum(challenge
);
363 debug("Received RSA challenge for host key from server.");
365 /* Compute a response to the challenge. */
366 respond_to_rsa_challenge(challenge
, host_key
->rsa
);
368 /* We no longer need the challenge. */
369 BN_clear_free(challenge
);
371 /* Wait for response from the server. */
372 type
= packet_read();
373 if (type
== SSH_SMSG_SUCCESS
) {
374 debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
377 if (type
!= SSH_SMSG_FAILURE
)
378 packet_disconnect("Protocol error waiting RSA auth response: %d", type
);
379 debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused.");
384 * Tries to authenticate with any string-based challenge/response system.
385 * Note that the client code is not tied to s/key or TIS.
388 try_challenge_response_authentication(void)
393 char *challenge
, *response
;
395 debug("Doing challenge response authentication.");
397 for (i
= 0; i
< options
.number_of_password_prompts
; i
++) {
398 /* request a challenge */
399 packet_start(SSH_CMSG_AUTH_TIS
);
403 type
= packet_read();
404 if (type
!= SSH_SMSG_FAILURE
&&
405 type
!= SSH_SMSG_AUTH_TIS_CHALLENGE
) {
406 packet_disconnect("Protocol error: got %d in response "
407 "to SSH_CMSG_AUTH_TIS", type
);
409 if (type
!= SSH_SMSG_AUTH_TIS_CHALLENGE
) {
410 debug("No challenge.");
413 challenge
= packet_get_string(&clen
);
415 snprintf(prompt
, sizeof prompt
, "%s%s", challenge
,
416 strchr(challenge
, '\n') ? "" : "\nResponse: ");
419 error("Permission denied, please try again.");
420 if (options
.cipher
== SSH_CIPHER_NONE
)
421 logit("WARNING: Encryption is disabled! "
422 "Response will be transmitted in clear text.");
423 response
= read_passphrase(prompt
, 0);
424 if (strcmp(response
, "") == 0) {
428 packet_start(SSH_CMSG_AUTH_TIS_RESPONSE
);
429 ssh_put_password(response
);
430 explicit_bzero(response
, strlen(response
));
434 type
= packet_read();
435 if (type
== SSH_SMSG_SUCCESS
)
437 if (type
!= SSH_SMSG_FAILURE
)
438 packet_disconnect("Protocol error: got %d in response "
439 "to SSH_CMSG_AUTH_TIS_RESPONSE", type
);
446 * Tries to authenticate with plain passwd authentication.
449 try_password_authentication(char *prompt
)
454 debug("Doing password authentication.");
455 if (options
.cipher
== SSH_CIPHER_NONE
)
456 logit("WARNING: Encryption is disabled! Password will be transmitted in clear text.");
457 for (i
= 0; i
< options
.number_of_password_prompts
; i
++) {
459 error("Permission denied, please try again.");
460 password
= read_passphrase(prompt
, 0);
461 packet_start(SSH_CMSG_AUTH_PASSWORD
);
462 ssh_put_password(password
);
463 explicit_bzero(password
, strlen(password
));
468 type
= packet_read();
469 if (type
== SSH_SMSG_SUCCESS
)
471 if (type
!= SSH_SMSG_FAILURE
)
472 packet_disconnect("Protocol error: got %d in response to passwd auth", type
);
482 ssh_kex(char *host
, struct sockaddr
*hostaddr
)
486 Key
*host_key
, *server_key
;
488 int ssh_cipher_default
= SSH_CIPHER_3DES
;
489 u_char session_key
[SSH_SESSION_KEY_LENGTH
];
491 u_int supported_ciphers
;
492 u_int server_flags
, client_flags
;
495 debug("Waiting for server public key.");
497 /* Wait for a public key packet from the server. */
498 packet_read_expect(SSH_SMSG_PUBLIC_KEY
);
500 /* Get cookie from the packet. */
501 for (i
= 0; i
< 8; i
++)
502 cookie
[i
] = packet_get_char();
504 /* Get the public key. */
505 server_key
= key_new(KEY_RSA1
);
506 bits
= packet_get_int();
507 packet_get_bignum(server_key
->rsa
->e
);
508 packet_get_bignum(server_key
->rsa
->n
);
510 rbits
= BN_num_bits(server_key
->rsa
->n
);
512 logit("Warning: Server lies about size of server public key: "
513 "actual size is %d bits vs. announced %d.", rbits
, bits
);
514 logit("Warning: This may be due to an old implementation of ssh.");
516 /* Get the host key. */
517 host_key
= key_new(KEY_RSA1
);
518 bits
= packet_get_int();
519 packet_get_bignum(host_key
->rsa
->e
);
520 packet_get_bignum(host_key
->rsa
->n
);
522 rbits
= BN_num_bits(host_key
->rsa
->n
);
524 logit("Warning: Server lies about size of server host key: "
525 "actual size is %d bits vs. announced %d.", rbits
, bits
);
526 logit("Warning: This may be due to an old implementation of ssh.");
529 /* Get protocol flags. */
530 server_flags
= packet_get_int();
531 packet_set_protocol_flags(server_flags
);
533 supported_ciphers
= packet_get_int();
534 supported_authentications
= packet_get_int();
537 debug("Received server public key (%d bits) and host key (%d bits).",
538 BN_num_bits(server_key
->rsa
->n
), BN_num_bits(host_key
->rsa
->n
));
540 if (verify_host_key(host
, hostaddr
, host_key
) == -1)
541 fatal("Host key verification failed.");
543 client_flags
= SSH_PROTOFLAG_SCREEN_NUMBER
| SSH_PROTOFLAG_HOST_IN_FWD_OPEN
;
545 derive_ssh1_session_id(host_key
->rsa
->n
, server_key
->rsa
->n
, cookie
, session_id
);
548 * Generate an encryption key for the session. The key is a 256 bit
549 * random number, interpreted as a 32-byte key, with the least
550 * significant 8 bits being the first byte of the key.
552 for (i
= 0; i
< 32; i
++) {
555 session_key
[i
] = rnd
& 0xff;
560 * According to the protocol spec, the first byte of the session key
561 * is the highest byte of the integer. The session key is xored with
562 * the first 16 bytes of the session id.
564 if ((key
= BN_new()) == NULL
)
565 fatal("ssh_kex: BN_new failed");
566 if (BN_set_word(key
, 0) == 0)
567 fatal("ssh_kex: BN_set_word failed");
568 for (i
= 0; i
< SSH_SESSION_KEY_LENGTH
; i
++) {
569 if (BN_lshift(key
, key
, 8) == 0)
570 fatal("ssh_kex: BN_lshift failed");
572 if (BN_add_word(key
, session_key
[i
] ^ session_id
[i
])
574 fatal("ssh_kex: BN_add_word failed");
576 if (BN_add_word(key
, session_key
[i
]) == 0)
577 fatal("ssh_kex: BN_add_word failed");
582 * Encrypt the integer using the public key and host key of the
583 * server (key with smaller modulus first).
585 if (BN_cmp(server_key
->rsa
->n
, host_key
->rsa
->n
) < 0) {
586 /* Public key has smaller modulus. */
587 if (BN_num_bits(host_key
->rsa
->n
) <
588 BN_num_bits(server_key
->rsa
->n
) + SSH_KEY_BITS_RESERVED
) {
589 fatal("respond_to_rsa_challenge: host_key %d < server_key %d + "
590 "SSH_KEY_BITS_RESERVED %d",
591 BN_num_bits(host_key
->rsa
->n
),
592 BN_num_bits(server_key
->rsa
->n
),
593 SSH_KEY_BITS_RESERVED
);
595 if (rsa_public_encrypt(key
, key
, server_key
->rsa
) != 0 ||
596 rsa_public_encrypt(key
, key
, host_key
->rsa
) != 0)
597 fatal("%s: rsa_public_encrypt failed", __func__
);
599 /* Host key has smaller modulus (or they are equal). */
600 if (BN_num_bits(server_key
->rsa
->n
) <
601 BN_num_bits(host_key
->rsa
->n
) + SSH_KEY_BITS_RESERVED
) {
602 fatal("respond_to_rsa_challenge: server_key %d < host_key %d + "
603 "SSH_KEY_BITS_RESERVED %d",
604 BN_num_bits(server_key
->rsa
->n
),
605 BN_num_bits(host_key
->rsa
->n
),
606 SSH_KEY_BITS_RESERVED
);
608 if (rsa_public_encrypt(key
, key
, host_key
->rsa
) != 0 ||
609 rsa_public_encrypt(key
, key
, server_key
->rsa
) != 0)
610 fatal("%s: rsa_public_encrypt failed", __func__
);
613 /* Destroy the public keys since we no longer need them. */
614 key_free(server_key
);
617 if (options
.cipher
== SSH_CIPHER_NOT_SET
) {
618 if (cipher_mask_ssh1(1) & supported_ciphers
& (1 << ssh_cipher_default
))
619 options
.cipher
= ssh_cipher_default
;
620 } else if (options
.cipher
== SSH_CIPHER_INVALID
||
621 !(cipher_mask_ssh1(1) & (1 << options
.cipher
))) {
622 logit("No valid SSH1 cipher, using %.100s instead.",
623 cipher_name(ssh_cipher_default
));
624 options
.cipher
= ssh_cipher_default
;
626 /* Check that the selected cipher is supported. */
627 if (!(supported_ciphers
& (1 << options
.cipher
)))
628 fatal("Selected cipher type %.100s not supported by server.",
629 cipher_name(options
.cipher
));
631 debug("Encryption type: %.100s", cipher_name(options
.cipher
));
633 /* Send the encrypted session key to the server. */
634 packet_start(SSH_CMSG_SESSION_KEY
);
635 packet_put_char(options
.cipher
);
637 /* Send the cookie back to the server. */
638 for (i
= 0; i
< 8; i
++)
639 packet_put_char(cookie
[i
]);
641 /* Send and destroy the encrypted encryption key integer. */
642 packet_put_bignum(key
);
645 /* Send protocol flags. */
646 packet_put_int(client_flags
);
648 /* Send the packet now. */
652 debug("Sent encrypted session key.");
654 /* Set the encryption key. */
655 packet_set_encryption_key(session_key
, SSH_SESSION_KEY_LENGTH
, options
.cipher
);
658 * We will no longer need the session key here.
659 * Destroy any extra copies.
661 explicit_bzero(session_key
, sizeof(session_key
));
664 * Expect a success message from the server. Note that this message
665 * will be received in encrypted form.
667 packet_read_expect(SSH_SMSG_SUCCESS
);
669 debug("Received encrypted confirmation.");
676 ssh_userauth1(const char *local_user
, const char *server_user
, char *host
,
677 Sensitive
*sensitive
)
681 if (supported_authentications
== 0)
682 fatal("ssh_userauth1: server supports no auth methods");
684 /* Send the name of the user to log in as on the server. */
685 packet_start(SSH_CMSG_USER
);
686 packet_put_cstring(server_user
);
691 * The server should respond with success if no authentication is
692 * needed (the user has no password). Otherwise the server responds
695 type
= packet_read();
697 /* check whether the connection was accepted without authentication. */
698 if (type
== SSH_SMSG_SUCCESS
)
700 if (type
!= SSH_SMSG_FAILURE
)
701 packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type
);
704 * Try .rhosts or /etc/hosts.equiv authentication with RSA host
707 if ((supported_authentications
& (1 << SSH_AUTH_RHOSTS_RSA
)) &&
708 options
.rhosts_rsa_authentication
) {
709 for (i
= 0; i
< sensitive
->nkeys
; i
++) {
710 if (sensitive
->keys
[i
] != NULL
&&
711 sensitive
->keys
[i
]->type
== KEY_RSA1
&&
712 try_rhosts_rsa_authentication(local_user
,
717 /* Try RSA authentication if the server supports it. */
718 if ((supported_authentications
& (1 << SSH_AUTH_RSA
)) &&
719 options
.rsa_authentication
) {
721 * Try RSA authentication using the authentication agent. The
722 * agent is tried first because no passphrase is needed for
723 * it, whereas identity files may require passphrases.
725 if (try_agent_authentication())
728 /* Try RSA authentication for each identity. */
729 for (i
= 0; i
< options
.num_identity_files
; i
++)
730 if (options
.identity_keys
[i
] != NULL
&&
731 options
.identity_keys
[i
]->type
== KEY_RSA1
&&
732 try_rsa_authentication(i
))
735 /* Try challenge response authentication if the server supports it. */
736 if ((supported_authentications
& (1 << SSH_AUTH_TIS
)) &&
737 options
.challenge_response_authentication
&& !options
.batch_mode
) {
738 if (try_challenge_response_authentication())
741 /* Try password authentication if the server supports it. */
742 if ((supported_authentications
& (1 << SSH_AUTH_PASSWORD
)) &&
743 options
.password_authentication
&& !options
.batch_mode
) {
746 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password: ",
748 if (try_password_authentication(prompt
))
751 /* All authentication methods have failed. Exit with an error message. */
752 fatal("Permission denied.");
756 return; /* need statement after label */