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