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