]>
Commit | Line | Data |
---|---|---|
31652869 | 1 | /* $OpenBSD: sshconnect1.c,v 1.69 2006/08/03 03:34:42 deraadt Exp $ */ |
a306f2dd | 2 | /* |
3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> | |
4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
5 | * All rights reserved | |
a306f2dd | 6 | * Code to connect to a remote host, and to perform the client side of the |
7 | * login (authentication) dialog. | |
8 | * | |
bcbf86ec | 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". | |
a306f2dd | 14 | */ |
15 | ||
16 | #include "includes.h" | |
a306f2dd | 17 | |
31652869 | 18 | #include <sys/types.h> |
19 | #include <sys/socket.h> | |
20 | ||
a306f2dd | 21 | #include <openssl/bn.h> |
6b4b5e49 | 22 | #include <openssl/md5.h> |
a306f2dd | 23 | |
24436b92 | 24 | #include <stdarg.h> |
cf851879 | 25 | #include <stdio.h> |
ffa517a8 | 26 | #include <stdlib.h> |
00146caa | 27 | #include <string.h> |
31652869 | 28 | #include <signal.h> |
29 | #include <pwd.h> | |
00146caa | 30 | |
31652869 | 31 | #include "xmalloc.h" |
42f11eb2 | 32 | #include "ssh.h" |
33 | #include "ssh1.h" | |
a306f2dd | 34 | #include "rsa.h" |
a306f2dd | 35 | #include "buffer.h" |
36 | #include "packet.h" | |
31652869 | 37 | #include "key.h" |
38 | #include "cipher.h" | |
8bbf1fa6 | 39 | #include "kex.h" |
a306f2dd | 40 | #include "uidswap.h" |
42f11eb2 | 41 | #include "log.h" |
a306f2dd | 42 | #include "readconf.h" |
4c8722d9 | 43 | #include "authfd.h" |
a306f2dd | 44 | #include "sshconnect.h" |
45 | #include "authfile.h" | |
58ae9cb8 | 46 | #include "misc.h" |
42f11eb2 | 47 | #include "canohost.h" |
31652869 | 48 | #include "hostfile.h" |
ced49be2 | 49 | #include "auth.h" |
a306f2dd | 50 | |
51 | /* Session id for the current session. */ | |
1e3b8b07 | 52 | u_char session_id[16]; |
53 | u_int supported_authentications = 0; | |
a306f2dd | 54 | |
55 | extern Options options; | |
56 | extern char *__progname; | |
57 | ||
58 | /* | |
59 | * Checks if the user has an authentication agent, and if so, tries to | |
60 | * authenticate using the agent. | |
61 | */ | |
396c147e | 62 | static int |
5ca51e19 | 63 | try_agent_authentication(void) |
a306f2dd | 64 | { |
2e73a022 | 65 | int type; |
a306f2dd | 66 | char *comment; |
67 | AuthenticationConnection *auth; | |
1e3b8b07 | 68 | u_char response[16]; |
69 | u_int i; | |
2e73a022 | 70 | Key *key; |
71 | BIGNUM *challenge; | |
a306f2dd | 72 | |
73 | /* Get connection to the agent. */ | |
74 | auth = ssh_get_authentication_connection(); | |
75 | if (!auth) | |
76 | return 0; | |
77 | ||
b775c6f2 | 78 | if ((challenge = BN_new()) == NULL) |
79 | fatal("try_agent_authentication: BN_new failed"); | |
a306f2dd | 80 | /* Loop through identities served by the agent. */ |
2e73a022 | 81 | for (key = ssh_get_first_identity(auth, &comment, 1); |
184eed6a | 82 | key != NULL; |
83 | key = ssh_get_next_identity(auth, &comment, 1)) { | |
a306f2dd | 84 | |
85 | /* Try this identity. */ | |
86 | debug("Trying RSA authentication via agent with '%.100s'", comment); | |
87 | xfree(comment); | |
88 | ||
89 | /* Tell the server that we are willing to authenticate using this key. */ | |
90 | packet_start(SSH_CMSG_AUTH_RSA); | |
2e73a022 | 91 | packet_put_bignum(key->rsa->n); |
a306f2dd | 92 | packet_send(); |
93 | packet_write_wait(); | |
94 | ||
95 | /* Wait for server's response. */ | |
54a5250f | 96 | type = packet_read(); |
a306f2dd | 97 | |
7b9b0103 | 98 | /* The server sends failure if it doesn't like our key or |
a306f2dd | 99 | does not support RSA authentication. */ |
100 | if (type == SSH_SMSG_FAILURE) { | |
101 | debug("Server refused our key."); | |
2e73a022 | 102 | key_free(key); |
a306f2dd | 103 | continue; |
104 | } | |
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", | |
108 | type); | |
109 | ||
20b279e6 | 110 | packet_get_bignum(challenge); |
95500969 | 111 | packet_check_eom(); |
a306f2dd | 112 | |
113 | debug("Received RSA challenge from server."); | |
114 | ||
115 | /* Ask the agent to decrypt the challenge. */ | |
2e73a022 | 116 | if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) { |
117 | /* | |
118 | * The agent failed to authenticate this identifier | |
119 | * although it advertised it supports this. Just | |
120 | * return a wrong value. | |
121 | */ | |
bbe88b6d | 122 | logit("Authentication agent failed to decrypt challenge."); |
a306f2dd | 123 | memset(response, 0, sizeof(response)); |
124 | } | |
2e73a022 | 125 | key_free(key); |
a306f2dd | 126 | debug("Sending response to RSA challenge."); |
127 | ||
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]); | |
132 | packet_send(); | |
133 | packet_write_wait(); | |
134 | ||
135 | /* Wait for response from the server. */ | |
54a5250f | 136 | type = packet_read(); |
a306f2dd | 137 | |
138 | /* The server returns success if it accepted the authentication. */ | |
139 | if (type == SSH_SMSG_SUCCESS) { | |
eea39c02 | 140 | ssh_close_authentication_connection(auth); |
a306f2dd | 141 | BN_clear_free(challenge); |
2e73a022 | 142 | debug("RSA authentication accepted by server."); |
a306f2dd | 143 | return 1; |
144 | } | |
145 | /* Otherwise it should return failure. */ | |
146 | if (type != SSH_SMSG_FAILURE) | |
147 | packet_disconnect("Protocol error waiting RSA auth response: %d", | |
148 | type); | |
149 | } | |
eea39c02 | 150 | ssh_close_authentication_connection(auth); |
a306f2dd | 151 | BN_clear_free(challenge); |
a306f2dd | 152 | debug("RSA authentication using agent refused."); |
153 | return 0; | |
154 | } | |
155 | ||
156 | /* | |
157 | * Computes the proper response to a RSA challenge, and sends the response to | |
158 | * the server. | |
159 | */ | |
396c147e | 160 | static void |
a306f2dd | 161 | respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv) |
162 | { | |
1e3b8b07 | 163 | u_char buf[32], response[16]; |
a306f2dd | 164 | MD5_CTX md; |
165 | int i, len; | |
166 | ||
167 | /* Decrypt the challenge using the private key. */ | |
46aa2d1f | 168 | /* XXX think about Bleichenbacher, too */ |
169 | if (rsa_private_decrypt(challenge, challenge, prv) <= 0) | |
170 | packet_disconnect( | |
171 | "respond_to_rsa_challenge: rsa_private_decrypt failed"); | |
a306f2dd | 172 | |
173 | /* Compute the response. */ | |
174 | /* The response is MD5 of decrypted challenge plus session id. */ | |
175 | len = BN_num_bytes(challenge); | |
2ceb8101 | 176 | if (len <= 0 || (u_int)len > sizeof(buf)) |
46aa2d1f | 177 | packet_disconnect( |
178 | "respond_to_rsa_challenge: bad challenge length %d", len); | |
a306f2dd | 179 | |
180 | memset(buf, 0, sizeof(buf)); | |
181 | BN_bn2bin(challenge, buf + sizeof(buf) - len); | |
182 | MD5_Init(&md); | |
183 | MD5_Update(&md, buf, 32); | |
184 | MD5_Update(&md, session_id, 16); | |
185 | MD5_Final(response, &md); | |
186 | ||
187 | debug("Sending response to host key RSA challenge."); | |
188 | ||
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]); | |
193 | packet_send(); | |
194 | packet_write_wait(); | |
195 | ||
196 | memset(buf, 0, sizeof(buf)); | |
197 | memset(response, 0, sizeof(response)); | |
198 | memset(&md, 0, sizeof(md)); | |
199 | } | |
200 | ||
201 | /* | |
202 | * Checks if the user has authentication file, and if so, tries to authenticate | |
203 | * the user using it. | |
204 | */ | |
396c147e | 205 | static int |
383546c4 | 206 | try_rsa_authentication(int idx) |
a306f2dd | 207 | { |
208 | BIGNUM *challenge; | |
ce404659 | 209 | Key *public, *private; |
383546c4 | 210 | char buf[300], *passphrase, *comment, *authfile; |
fc231518 | 211 | int i, perm_ok = 1, type, quit; |
a306f2dd | 212 | |
383546c4 | 213 | public = options.identity_keys[idx]; |
214 | authfile = options.identity_files[idx]; | |
215 | comment = xstrdup(authfile); | |
216 | ||
a306f2dd | 217 | debug("Trying RSA authentication with key '%.100s'", comment); |
218 | ||
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); | |
222 | packet_send(); | |
223 | packet_write_wait(); | |
224 | ||
a306f2dd | 225 | /* Wait for server's response. */ |
54a5250f | 226 | type = packet_read(); |
a306f2dd | 227 | |
228 | /* | |
7b9b0103 | 229 | * The server responds with failure if it doesn't like our key or |
230 | * doesn't support RSA authentication. | |
a306f2dd | 231 | */ |
232 | if (type == SSH_SMSG_FAILURE) { | |
233 | debug("Server refused our key."); | |
234 | xfree(comment); | |
235 | return 0; | |
236 | } | |
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); | |
240 | ||
241 | /* Get the challenge from the packet. */ | |
b775c6f2 | 242 | if ((challenge = BN_new()) == NULL) |
243 | fatal("try_rsa_authentication: BN_new failed"); | |
20b279e6 | 244 | packet_get_bignum(challenge); |
95500969 | 245 | packet_check_eom(); |
a306f2dd | 246 | |
247 | debug("Received RSA challenge from server."); | |
248 | ||
a306f2dd | 249 | /* |
383546c4 | 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 | |
a306f2dd | 252 | * fails, ask for a passphrase. |
253 | */ | |
75cf7563 | 254 | if (public->flags & KEY_FLAG_EXT) |
383546c4 | 255 | private = public; |
256 | else | |
fc231518 | 257 | private = key_load_private_type(KEY_RSA1, authfile, "", NULL, |
258 | &perm_ok); | |
259 | if (private == NULL && !options.batch_mode && perm_ok) { | |
ce404659 | 260 | snprintf(buf, sizeof(buf), |
261 | "Enter passphrase for RSA key '%.100s': ", comment); | |
262 | for (i = 0; i < options.number_of_password_prompts; i++) { | |
a306f2dd | 263 | passphrase = read_passphrase(buf, 0); |
ce404659 | 264 | if (strcmp(passphrase, "") != 0) { |
265 | private = key_load_private_type(KEY_RSA1, | |
fc231518 | 266 | authfile, passphrase, NULL, NULL); |
ce404659 | 267 | quit = 0; |
268 | } else { | |
269 | debug2("no passphrase given, try next key"); | |
270 | quit = 1; | |
271 | } | |
a306f2dd | 272 | memset(passphrase, 0, strlen(passphrase)); |
273 | xfree(passphrase); | |
ce404659 | 274 | if (private != NULL || quit) |
275 | break; | |
276 | debug2("bad passphrase given, try again..."); | |
a306f2dd | 277 | } |
a306f2dd | 278 | } |
279 | /* We no longer need the comment. */ | |
280 | xfree(comment); | |
281 | ||
ce404659 | 282 | if (private == NULL) { |
fc231518 | 283 | if (!options.batch_mode && perm_ok) |
ce404659 | 284 | error("Bad passphrase."); |
285 | ||
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++) | |
289 | packet_put_char(0); | |
290 | packet_send(); | |
291 | packet_write_wait(); | |
292 | ||
293 | /* Expect the server to reject it... */ | |
54a5250f | 294 | packet_read_expect(SSH_SMSG_FAILURE); |
ce404659 | 295 | BN_clear_free(challenge); |
296 | return 0; | |
297 | } | |
298 | ||
a306f2dd | 299 | /* Compute and send a response to the challenge. */ |
300 | respond_to_rsa_challenge(challenge, private->rsa); | |
301 | ||
383546c4 | 302 | /* Destroy the private key unless it in external hardware. */ |
303 | if (!(private->flags & KEY_FLAG_EXT)) | |
304 | key_free(private); | |
a306f2dd | 305 | |
306 | /* We no longer need the challenge. */ | |
307 | BN_clear_free(challenge); | |
308 | ||
309 | /* Wait for response from the server. */ | |
54a5250f | 310 | type = packet_read(); |
a306f2dd | 311 | if (type == SSH_SMSG_SUCCESS) { |
312 | debug("RSA authentication accepted by server."); | |
313 | return 1; | |
314 | } | |
315 | if (type != SSH_SMSG_FAILURE) | |
316 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); | |
317 | debug("RSA authentication refused."); | |
318 | return 0; | |
319 | } | |
320 | ||
321 | /* | |
322 | * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv | |
323 | * authentication and RSA host authentication. | |
324 | */ | |
396c147e | 325 | static int |
aeaa3d9e | 326 | try_rhosts_rsa_authentication(const char *local_user, Key * host_key) |
a306f2dd | 327 | { |
328 | int type; | |
329 | BIGNUM *challenge; | |
a306f2dd | 330 | |
331 | debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication."); | |
332 | ||
333 | /* Tell the server that we are willing to authenticate using this key. */ | |
334 | packet_start(SSH_CMSG_AUTH_RHOSTS_RSA); | |
449c5ba5 | 335 | packet_put_cstring(local_user); |
aeaa3d9e | 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); | |
a306f2dd | 339 | packet_send(); |
340 | packet_write_wait(); | |
341 | ||
342 | /* Wait for server's response. */ | |
54a5250f | 343 | type = packet_read(); |
a306f2dd | 344 | |
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."); | |
349 | return 0; | |
350 | } | |
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); | |
354 | ||
355 | /* Get the challenge from the packet. */ | |
b775c6f2 | 356 | if ((challenge = BN_new()) == NULL) |
357 | fatal("try_rhosts_rsa_authentication: BN_new failed"); | |
20b279e6 | 358 | packet_get_bignum(challenge); |
95500969 | 359 | packet_check_eom(); |
a306f2dd | 360 | |
361 | debug("Received RSA challenge for host key from server."); | |
362 | ||
363 | /* Compute a response to the challenge. */ | |
aeaa3d9e | 364 | respond_to_rsa_challenge(challenge, host_key->rsa); |
a306f2dd | 365 | |
366 | /* We no longer need the challenge. */ | |
367 | BN_clear_free(challenge); | |
368 | ||
369 | /* Wait for response from the server. */ | |
54a5250f | 370 | type = packet_read(); |
a306f2dd | 371 | if (type == SSH_SMSG_SUCCESS) { |
372 | debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server."); | |
373 | return 1; | |
374 | } | |
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."); | |
378 | return 0; | |
379 | } | |
380 | ||
a306f2dd | 381 | /* |
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. | |
384 | */ | |
396c147e | 385 | static int |
5ba55ada | 386 | try_challenge_response_authentication(void) |
a306f2dd | 387 | { |
388 | int type, i; | |
1e3b8b07 | 389 | u_int clen; |
e3a70753 | 390 | char prompt[1024]; |
a306f2dd | 391 | char *challenge, *response; |
aa8003d6 | 392 | |
393 | debug("Doing challenge response authentication."); | |
394 | ||
a306f2dd | 395 | for (i = 0; i < options.number_of_password_prompts; i++) { |
e3a70753 | 396 | /* request a challenge */ |
397 | packet_start(SSH_CMSG_AUTH_TIS); | |
398 | packet_send(); | |
399 | packet_write_wait(); | |
400 | ||
54a5250f | 401 | type = packet_read(); |
e3a70753 | 402 | if (type != SSH_SMSG_FAILURE && |
403 | type != SSH_SMSG_AUTH_TIS_CHALLENGE) { | |
404 | packet_disconnect("Protocol error: got %d in response " | |
d464095c | 405 | "to SSH_CMSG_AUTH_TIS", type); |
e3a70753 | 406 | } |
407 | if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) { | |
d464095c | 408 | debug("No challenge."); |
e3a70753 | 409 | return 0; |
410 | } | |
411 | challenge = packet_get_string(&clen); | |
95500969 | 412 | packet_check_eom(); |
59c97189 | 413 | snprintf(prompt, sizeof prompt, "%s%s", challenge, |
184eed6a | 414 | strchr(challenge, '\n') ? "" : "\nResponse: "); |
e3a70753 | 415 | xfree(challenge); |
a306f2dd | 416 | if (i != 0) |
417 | error("Permission denied, please try again."); | |
e3a70753 | 418 | if (options.cipher == SSH_CIPHER_NONE) |
bbe88b6d | 419 | logit("WARNING: Encryption is disabled! " |
134c552b | 420 | "Response will be transmitted in clear text."); |
e3a70753 | 421 | response = read_passphrase(prompt, 0); |
422 | if (strcmp(response, "") == 0) { | |
423 | xfree(response); | |
424 | break; | |
425 | } | |
a306f2dd | 426 | packet_start(SSH_CMSG_AUTH_TIS_RESPONSE); |
0ae4fe1d | 427 | ssh_put_password(response); |
a306f2dd | 428 | memset(response, 0, strlen(response)); |
429 | xfree(response); | |
430 | packet_send(); | |
431 | packet_write_wait(); | |
54a5250f | 432 | type = packet_read(); |
a306f2dd | 433 | if (type == SSH_SMSG_SUCCESS) |
434 | return 1; | |
435 | if (type != SSH_SMSG_FAILURE) | |
436 | packet_disconnect("Protocol error: got %d in response " | |
d464095c | 437 | "to SSH_CMSG_AUTH_TIS_RESPONSE", type); |
a306f2dd | 438 | } |
439 | /* failure */ | |
440 | return 0; | |
441 | } | |
442 | ||
443 | /* | |
444 | * Tries to authenticate with plain passwd authentication. | |
445 | */ | |
396c147e | 446 | static int |
a306f2dd | 447 | try_password_authentication(char *prompt) |
448 | { | |
54a5250f | 449 | int type, i; |
a306f2dd | 450 | char *password; |
451 | ||
452 | debug("Doing password authentication."); | |
453 | if (options.cipher == SSH_CIPHER_NONE) | |
bbe88b6d | 454 | logit("WARNING: Encryption is disabled! Password will be transmitted in clear text."); |
a306f2dd | 455 | for (i = 0; i < options.number_of_password_prompts; i++) { |
456 | if (i != 0) | |
457 | error("Permission denied, please try again."); | |
458 | password = read_passphrase(prompt, 0); | |
459 | packet_start(SSH_CMSG_AUTH_PASSWORD); | |
0ae4fe1d | 460 | ssh_put_password(password); |
a306f2dd | 461 | memset(password, 0, strlen(password)); |
462 | xfree(password); | |
463 | packet_send(); | |
464 | packet_write_wait(); | |
465 | ||
54a5250f | 466 | type = packet_read(); |
a306f2dd | 467 | if (type == SSH_SMSG_SUCCESS) |
468 | return 1; | |
469 | if (type != SSH_SMSG_FAILURE) | |
470 | packet_disconnect("Protocol error: got %d in response to passwd auth", type); | |
471 | } | |
472 | /* failure */ | |
473 | return 0; | |
474 | } | |
475 | ||
476 | /* | |
477 | * SSH1 key exchange | |
478 | */ | |
479 | void | |
480 | ssh_kex(char *host, struct sockaddr *hostaddr) | |
481 | { | |
482 | int i; | |
483 | BIGNUM *key; | |
b775c6f2 | 484 | Key *host_key, *server_key; |
a306f2dd | 485 | int bits, rbits; |
486 | int ssh_cipher_default = SSH_CIPHER_3DES; | |
1e3b8b07 | 487 | u_char session_key[SSH_SESSION_KEY_LENGTH]; |
488 | u_char cookie[8]; | |
489 | u_int supported_ciphers; | |
490 | u_int server_flags, client_flags; | |
ca75d7de | 491 | u_int32_t rnd = 0; |
a306f2dd | 492 | |
493 | debug("Waiting for server public key."); | |
494 | ||
495 | /* Wait for a public key packet from the server. */ | |
54a5250f | 496 | packet_read_expect(SSH_SMSG_PUBLIC_KEY); |
a306f2dd | 497 | |
498 | /* Get cookie from the packet. */ | |
499 | for (i = 0; i < 8; i++) | |
500 | cookie[i] = packet_get_char(); | |
501 | ||
502 | /* Get the public key. */ | |
b775c6f2 | 503 | server_key = key_new(KEY_RSA1); |
504 | bits = packet_get_int(); | |
20b279e6 | 505 | packet_get_bignum(server_key->rsa->e); |
506 | packet_get_bignum(server_key->rsa->n); | |
a306f2dd | 507 | |
b775c6f2 | 508 | rbits = BN_num_bits(server_key->rsa->n); |
a306f2dd | 509 | if (bits != rbits) { |
bbe88b6d | 510 | logit("Warning: Server lies about size of server public key: " |
a306f2dd | 511 | "actual size is %d bits vs. announced %d.", rbits, bits); |
bbe88b6d | 512 | logit("Warning: This may be due to an old implementation of ssh."); |
a306f2dd | 513 | } |
514 | /* Get the host key. */ | |
b775c6f2 | 515 | host_key = key_new(KEY_RSA1); |
516 | bits = packet_get_int(); | |
20b279e6 | 517 | packet_get_bignum(host_key->rsa->e); |
518 | packet_get_bignum(host_key->rsa->n); | |
a306f2dd | 519 | |
b775c6f2 | 520 | rbits = BN_num_bits(host_key->rsa->n); |
a306f2dd | 521 | if (bits != rbits) { |
bbe88b6d | 522 | logit("Warning: Server lies about size of server host key: " |
a306f2dd | 523 | "actual size is %d bits vs. announced %d.", rbits, bits); |
bbe88b6d | 524 | logit("Warning: This may be due to an old implementation of ssh."); |
a306f2dd | 525 | } |
526 | ||
527 | /* Get protocol flags. */ | |
528 | server_flags = packet_get_int(); | |
529 | packet_set_protocol_flags(server_flags); | |
530 | ||
531 | supported_ciphers = packet_get_int(); | |
532 | supported_authentications = packet_get_int(); | |
95500969 | 533 | packet_check_eom(); |
a306f2dd | 534 | |
535 | debug("Received server public key (%d bits) and host key (%d bits).", | |
b775c6f2 | 536 | BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n)); |
a306f2dd | 537 | |
b775c6f2 | 538 | if (verify_host_key(host, hostaddr, host_key) == -1) |
01e9ef57 | 539 | fatal("Host key verification failed."); |
a306f2dd | 540 | |
541 | client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN; | |
542 | ||
8bbf1fa6 | 543 | derive_ssh1_session_id(host_key->rsa->n, server_key->rsa->n, cookie, session_id); |
a306f2dd | 544 | |
545 | /* Generate a session key. */ | |
546 | arc4random_stir(); | |
547 | ||
548 | /* | |
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. | |
552 | */ | |
553 | for (i = 0; i < 32; i++) { | |
554 | if (i % 4 == 0) | |
ca75d7de | 555 | rnd = arc4random(); |
556 | session_key[i] = rnd & 0xff; | |
557 | rnd >>= 8; | |
a306f2dd | 558 | } |
559 | ||
560 | /* | |
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. | |
564 | */ | |
b775c6f2 | 565 | if ((key = BN_new()) == NULL) |
566 | fatal("respond_to_rsa_challenge: BN_new failed"); | |
a306f2dd | 567 | BN_set_word(key, 0); |
568 | for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) { | |
569 | BN_lshift(key, key, 8); | |
570 | if (i < 16) | |
571 | BN_add_word(key, session_key[i] ^ session_id[i]); | |
572 | else | |
573 | BN_add_word(key, session_key[i]); | |
574 | } | |
575 | ||
576 | /* | |
577 | * Encrypt the integer using the public key and host key of the | |
578 | * server (key with smaller modulus first). | |
579 | */ | |
b775c6f2 | 580 | if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) { |
a306f2dd | 581 | /* Public key has smaller modulus. */ |
b775c6f2 | 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 + " | |
184eed6a | 585 | "SSH_KEY_BITS_RESERVED %d", |
b775c6f2 | 586 | BN_num_bits(host_key->rsa->n), |
587 | BN_num_bits(server_key->rsa->n), | |
184eed6a | 588 | SSH_KEY_BITS_RESERVED); |
a306f2dd | 589 | } |
b775c6f2 | 590 | rsa_public_encrypt(key, key, server_key->rsa); |
591 | rsa_public_encrypt(key, key, host_key->rsa); | |
a306f2dd | 592 | } else { |
593 | /* Host key has smaller modulus (or they are equal). */ | |
b775c6f2 | 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 + " | |
184eed6a | 597 | "SSH_KEY_BITS_RESERVED %d", |
b775c6f2 | 598 | BN_num_bits(server_key->rsa->n), |
599 | BN_num_bits(host_key->rsa->n), | |
184eed6a | 600 | SSH_KEY_BITS_RESERVED); |
a306f2dd | 601 | } |
b775c6f2 | 602 | rsa_public_encrypt(key, key, host_key->rsa); |
603 | rsa_public_encrypt(key, key, server_key->rsa); | |
a306f2dd | 604 | } |
605 | ||
606 | /* Destroy the public keys since we no longer need them. */ | |
b775c6f2 | 607 | key_free(server_key); |
608 | key_free(host_key); | |
a306f2dd | 609 | |
c523303b | 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; | |
d77347cc | 613 | } else if (options.cipher == SSH_CIPHER_INVALID || |
c523303b | 614 | !(cipher_mask_ssh1(1) & (1 << options.cipher))) { |
bbe88b6d | 615 | logit("No valid SSH1 cipher, using %.100s instead.", |
94ec8c6b | 616 | cipher_name(ssh_cipher_default)); |
617 | options.cipher = ssh_cipher_default; | |
a306f2dd | 618 | } |
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.", | |
184eed6a | 622 | cipher_name(options.cipher)); |
a306f2dd | 623 | |
624 | debug("Encryption type: %.100s", cipher_name(options.cipher)); | |
625 | ||
626 | /* Send the encrypted session key to the server. */ | |
627 | packet_start(SSH_CMSG_SESSION_KEY); | |
628 | packet_put_char(options.cipher); | |
629 | ||
630 | /* Send the cookie back to the server. */ | |
631 | for (i = 0; i < 8; i++) | |
632 | packet_put_char(cookie[i]); | |
633 | ||
634 | /* Send and destroy the encrypted encryption key integer. */ | |
635 | packet_put_bignum(key); | |
636 | BN_clear_free(key); | |
637 | ||
638 | /* Send protocol flags. */ | |
639 | packet_put_int(client_flags); | |
640 | ||
641 | /* Send the packet now. */ | |
642 | packet_send(); | |
643 | packet_write_wait(); | |
644 | ||
645 | debug("Sent encrypted session key."); | |
646 | ||
647 | /* Set the encryption key. */ | |
648 | packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher); | |
649 | ||
650 | /* We will no longer need the session key here. Destroy any extra copies. */ | |
651 | memset(session_key, 0, sizeof(session_key)); | |
652 | ||
653 | /* | |
654 | * Expect a success message from the server. Note that this message | |
655 | * will be received in encrypted form. | |
656 | */ | |
54a5250f | 657 | packet_read_expect(SSH_SMSG_SUCCESS); |
a306f2dd | 658 | |
659 | debug("Received encrypted confirmation."); | |
660 | } | |
661 | ||
662 | /* | |
663 | * Authenticate user | |
664 | */ | |
665 | void | |
8002af61 | 666 | ssh_userauth1(const char *local_user, const char *server_user, char *host, |
39c00dc2 | 667 | Sensitive *sensitive) |
a306f2dd | 668 | { |
669 | int i, type; | |
184eed6a | 670 | |
a306f2dd | 671 | if (supported_authentications == 0) |
8002af61 | 672 | fatal("ssh_userauth1: server supports no auth methods"); |
a306f2dd | 673 | |
674 | /* Send the name of the user to log in as on the server. */ | |
675 | packet_start(SSH_CMSG_USER); | |
449c5ba5 | 676 | packet_put_cstring(server_user); |
a306f2dd | 677 | packet_send(); |
678 | packet_write_wait(); | |
679 | ||
680 | /* | |
681 | * The server should respond with success if no authentication is | |
682 | * needed (the user has no password). Otherwise the server responds | |
683 | * with failure. | |
684 | */ | |
54a5250f | 685 | type = packet_read(); |
a306f2dd | 686 | |
687 | /* check whether the connection was accepted without authentication. */ | |
688 | if (type == SSH_SMSG_SUCCESS) | |
ced49be2 | 689 | goto success; |
a306f2dd | 690 | if (type != SSH_SMSG_FAILURE) |
ced49be2 | 691 | packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type); |
184eed6a | 692 | |
a306f2dd | 693 | /* |
694 | * Try .rhosts or /etc/hosts.equiv authentication with RSA host | |
695 | * authentication. | |
696 | */ | |
697 | if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) && | |
8002af61 | 698 | options.rhosts_rsa_authentication) { |
39c00dc2 | 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, | |
703 | sensitive->keys[i])) | |
ced49be2 | 704 | goto success; |
8002af61 | 705 | } |
a306f2dd | 706 | } |
707 | /* Try RSA authentication if the server supports it. */ | |
708 | if ((supported_authentications & (1 << SSH_AUTH_RSA)) && | |
709 | options.rsa_authentication) { | |
710 | /* | |
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. | |
714 | */ | |
715 | if (try_agent_authentication()) | |
ced49be2 | 716 | goto success; |
a306f2dd | 717 | |
718 | /* Try RSA authentication for each identity. */ | |
719 | for (i = 0; i < options.num_identity_files; i++) | |
fee56204 | 720 | if (options.identity_keys[i] != NULL && |
721 | options.identity_keys[i]->type == KEY_RSA1 && | |
383546c4 | 722 | try_rsa_authentication(i)) |
ced49be2 | 723 | goto success; |
a306f2dd | 724 | } |
d464095c | 725 | /* Try challenge response authentication if the server supports it. */ |
a306f2dd | 726 | if ((supported_authentications & (1 << SSH_AUTH_TIS)) && |
5ba55ada | 727 | options.challenge_response_authentication && !options.batch_mode) { |
728 | if (try_challenge_response_authentication()) | |
ced49be2 | 729 | goto success; |
a306f2dd | 730 | } |
731 | /* Try password authentication if the server supports it. */ | |
732 | if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) && | |
733 | options.password_authentication && !options.batch_mode) { | |
734 | char prompt[80]; | |
735 | ||
b3b9ad8e | 736 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", |
a306f2dd | 737 | server_user, host); |
738 | if (try_password_authentication(prompt)) | |
ced49be2 | 739 | goto success; |
a306f2dd | 740 | } |
741 | /* All authentication methods have failed. Exit with an error message. */ | |
742 | fatal("Permission denied."); | |
743 | /* NOTREACHED */ | |
ced49be2 | 744 | |
745 | success: | |
62bb2c8f | 746 | return; /* need statement after label */ |
a306f2dd | 747 | } |