]>
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" | |
0598d99d | 16 | RCSID("$OpenBSD: sshconnect1.c,v 1.55 2003/08/13 08:46:31 markus Exp $"); |
a306f2dd | 17 | |
18 | #include <openssl/bn.h> | |
6b4b5e49 | 19 | #include <openssl/md5.h> |
a306f2dd | 20 | |
ced49be2 | 21 | #ifdef KRB5 |
22 | #include <krb5.h> | |
12928e80 | 23 | #ifndef HEIMDAL |
24 | #define krb5_get_err_text(context,code) error_message(code) | |
25 | #endif /* !HEIMDAL */ | |
ced49be2 | 26 | #endif |
42f11eb2 | 27 | |
28 | #include "ssh.h" | |
29 | #include "ssh1.h" | |
a306f2dd | 30 | #include "xmalloc.h" |
31 | #include "rsa.h" | |
a306f2dd | 32 | #include "buffer.h" |
33 | #include "packet.h" | |
a306f2dd | 34 | #include "mpaux.h" |
35 | #include "uidswap.h" | |
42f11eb2 | 36 | #include "log.h" |
a306f2dd | 37 | #include "readconf.h" |
38 | #include "key.h" | |
4c8722d9 | 39 | #include "authfd.h" |
a306f2dd | 40 | #include "sshconnect.h" |
41 | #include "authfile.h" | |
42f11eb2 | 42 | #include "readpass.h" |
43 | #include "cipher.h" | |
44 | #include "canohost.h" | |
ced49be2 | 45 | #include "auth.h" |
a306f2dd | 46 | |
47 | /* Session id for the current session. */ | |
1e3b8b07 | 48 | u_char session_id[16]; |
49 | u_int supported_authentications = 0; | |
a306f2dd | 50 | |
51 | extern Options options; | |
52 | extern char *__progname; | |
53 | ||
54 | /* | |
55 | * Checks if the user has an authentication agent, and if so, tries to | |
56 | * authenticate using the agent. | |
57 | */ | |
396c147e | 58 | static int |
5ca51e19 | 59 | try_agent_authentication(void) |
a306f2dd | 60 | { |
2e73a022 | 61 | int type; |
a306f2dd | 62 | char *comment; |
63 | AuthenticationConnection *auth; | |
1e3b8b07 | 64 | u_char response[16]; |
65 | u_int i; | |
2e73a022 | 66 | Key *key; |
67 | BIGNUM *challenge; | |
a306f2dd | 68 | |
69 | /* Get connection to the agent. */ | |
70 | auth = ssh_get_authentication_connection(); | |
71 | if (!auth) | |
72 | return 0; | |
73 | ||
b775c6f2 | 74 | if ((challenge = BN_new()) == NULL) |
75 | fatal("try_agent_authentication: BN_new failed"); | |
a306f2dd | 76 | /* Loop through identities served by the agent. */ |
2e73a022 | 77 | for (key = ssh_get_first_identity(auth, &comment, 1); |
184eed6a | 78 | key != NULL; |
79 | key = ssh_get_next_identity(auth, &comment, 1)) { | |
a306f2dd | 80 | |
81 | /* Try this identity. */ | |
82 | debug("Trying RSA authentication via agent with '%.100s'", comment); | |
83 | xfree(comment); | |
84 | ||
85 | /* Tell the server that we are willing to authenticate using this key. */ | |
86 | packet_start(SSH_CMSG_AUTH_RSA); | |
2e73a022 | 87 | packet_put_bignum(key->rsa->n); |
a306f2dd | 88 | packet_send(); |
89 | packet_write_wait(); | |
90 | ||
91 | /* Wait for server's response. */ | |
54a5250f | 92 | type = packet_read(); |
a306f2dd | 93 | |
94 | /* The server sends failure if it doesn\'t like our key or | |
95 | does not support RSA authentication. */ | |
96 | if (type == SSH_SMSG_FAILURE) { | |
97 | debug("Server refused our key."); | |
2e73a022 | 98 | key_free(key); |
a306f2dd | 99 | continue; |
100 | } | |
101 | /* Otherwise it should have sent a challenge. */ | |
102 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) | |
103 | packet_disconnect("Protocol error during RSA authentication: %d", | |
104 | type); | |
105 | ||
20b279e6 | 106 | packet_get_bignum(challenge); |
95500969 | 107 | packet_check_eom(); |
a306f2dd | 108 | |
109 | debug("Received RSA challenge from server."); | |
110 | ||
111 | /* Ask the agent to decrypt the challenge. */ | |
2e73a022 | 112 | if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) { |
113 | /* | |
114 | * The agent failed to authenticate this identifier | |
115 | * although it advertised it supports this. Just | |
116 | * return a wrong value. | |
117 | */ | |
bbe88b6d | 118 | logit("Authentication agent failed to decrypt challenge."); |
a306f2dd | 119 | memset(response, 0, sizeof(response)); |
120 | } | |
2e73a022 | 121 | key_free(key); |
a306f2dd | 122 | debug("Sending response to RSA challenge."); |
123 | ||
124 | /* Send the decrypted challenge back to the server. */ | |
125 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); | |
126 | for (i = 0; i < 16; i++) | |
127 | packet_put_char(response[i]); | |
128 | packet_send(); | |
129 | packet_write_wait(); | |
130 | ||
131 | /* Wait for response from the server. */ | |
54a5250f | 132 | type = packet_read(); |
a306f2dd | 133 | |
134 | /* The server returns success if it accepted the authentication. */ | |
135 | if (type == SSH_SMSG_SUCCESS) { | |
eea39c02 | 136 | ssh_close_authentication_connection(auth); |
a306f2dd | 137 | BN_clear_free(challenge); |
2e73a022 | 138 | debug("RSA authentication accepted by server."); |
a306f2dd | 139 | return 1; |
140 | } | |
141 | /* Otherwise it should return failure. */ | |
142 | if (type != SSH_SMSG_FAILURE) | |
143 | packet_disconnect("Protocol error waiting RSA auth response: %d", | |
144 | type); | |
145 | } | |
eea39c02 | 146 | ssh_close_authentication_connection(auth); |
a306f2dd | 147 | BN_clear_free(challenge); |
a306f2dd | 148 | debug("RSA authentication using agent refused."); |
149 | return 0; | |
150 | } | |
151 | ||
152 | /* | |
153 | * Computes the proper response to a RSA challenge, and sends the response to | |
154 | * the server. | |
155 | */ | |
396c147e | 156 | static void |
a306f2dd | 157 | respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv) |
158 | { | |
1e3b8b07 | 159 | u_char buf[32], response[16]; |
a306f2dd | 160 | MD5_CTX md; |
161 | int i, len; | |
162 | ||
163 | /* Decrypt the challenge using the private key. */ | |
46aa2d1f | 164 | /* XXX think about Bleichenbacher, too */ |
165 | if (rsa_private_decrypt(challenge, challenge, prv) <= 0) | |
166 | packet_disconnect( | |
167 | "respond_to_rsa_challenge: rsa_private_decrypt failed"); | |
a306f2dd | 168 | |
169 | /* Compute the response. */ | |
170 | /* The response is MD5 of decrypted challenge plus session id. */ | |
171 | len = BN_num_bytes(challenge); | |
172 | if (len <= 0 || len > sizeof(buf)) | |
46aa2d1f | 173 | packet_disconnect( |
174 | "respond_to_rsa_challenge: bad challenge length %d", len); | |
a306f2dd | 175 | |
176 | memset(buf, 0, sizeof(buf)); | |
177 | BN_bn2bin(challenge, buf + sizeof(buf) - len); | |
178 | MD5_Init(&md); | |
179 | MD5_Update(&md, buf, 32); | |
180 | MD5_Update(&md, session_id, 16); | |
181 | MD5_Final(response, &md); | |
182 | ||
183 | debug("Sending response to host key RSA challenge."); | |
184 | ||
185 | /* Send the response back to the server. */ | |
186 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); | |
187 | for (i = 0; i < 16; i++) | |
188 | packet_put_char(response[i]); | |
189 | packet_send(); | |
190 | packet_write_wait(); | |
191 | ||
192 | memset(buf, 0, sizeof(buf)); | |
193 | memset(response, 0, sizeof(response)); | |
194 | memset(&md, 0, sizeof(md)); | |
195 | } | |
196 | ||
197 | /* | |
198 | * Checks if the user has authentication file, and if so, tries to authenticate | |
199 | * the user using it. | |
200 | */ | |
396c147e | 201 | static int |
383546c4 | 202 | try_rsa_authentication(int idx) |
a306f2dd | 203 | { |
204 | BIGNUM *challenge; | |
ce404659 | 205 | Key *public, *private; |
383546c4 | 206 | char buf[300], *passphrase, *comment, *authfile; |
54a5250f | 207 | int i, type, quit; |
a306f2dd | 208 | |
383546c4 | 209 | public = options.identity_keys[idx]; |
210 | authfile = options.identity_files[idx]; | |
211 | comment = xstrdup(authfile); | |
212 | ||
a306f2dd | 213 | debug("Trying RSA authentication with key '%.100s'", comment); |
214 | ||
215 | /* Tell the server that we are willing to authenticate using this key. */ | |
216 | packet_start(SSH_CMSG_AUTH_RSA); | |
217 | packet_put_bignum(public->rsa->n); | |
218 | packet_send(); | |
219 | packet_write_wait(); | |
220 | ||
a306f2dd | 221 | /* Wait for server's response. */ |
54a5250f | 222 | type = packet_read(); |
a306f2dd | 223 | |
224 | /* | |
225 | * The server responds with failure if it doesn\'t like our key or | |
226 | * doesn\'t support RSA authentication. | |
227 | */ | |
228 | if (type == SSH_SMSG_FAILURE) { | |
229 | debug("Server refused our key."); | |
230 | xfree(comment); | |
231 | return 0; | |
232 | } | |
233 | /* Otherwise, the server should respond with a challenge. */ | |
234 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) | |
235 | packet_disconnect("Protocol error during RSA authentication: %d", type); | |
236 | ||
237 | /* Get the challenge from the packet. */ | |
b775c6f2 | 238 | if ((challenge = BN_new()) == NULL) |
239 | fatal("try_rsa_authentication: BN_new failed"); | |
20b279e6 | 240 | packet_get_bignum(challenge); |
95500969 | 241 | packet_check_eom(); |
a306f2dd | 242 | |
243 | debug("Received RSA challenge from server."); | |
244 | ||
a306f2dd | 245 | /* |
383546c4 | 246 | * If the key is not stored in external hardware, we have to |
247 | * load the private key. Try first with empty passphrase; if it | |
a306f2dd | 248 | * fails, ask for a passphrase. |
249 | */ | |
75cf7563 | 250 | if (public->flags & KEY_FLAG_EXT) |
383546c4 | 251 | private = public; |
252 | else | |
253 | private = key_load_private_type(KEY_RSA1, authfile, "", NULL); | |
ce404659 | 254 | if (private == NULL && !options.batch_mode) { |
255 | snprintf(buf, sizeof(buf), | |
256 | "Enter passphrase for RSA key '%.100s': ", comment); | |
257 | for (i = 0; i < options.number_of_password_prompts; i++) { | |
a306f2dd | 258 | passphrase = read_passphrase(buf, 0); |
ce404659 | 259 | if (strcmp(passphrase, "") != 0) { |
260 | private = key_load_private_type(KEY_RSA1, | |
261 | authfile, passphrase, NULL); | |
262 | quit = 0; | |
263 | } else { | |
264 | debug2("no passphrase given, try next key"); | |
265 | quit = 1; | |
266 | } | |
a306f2dd | 267 | memset(passphrase, 0, strlen(passphrase)); |
268 | xfree(passphrase); | |
ce404659 | 269 | if (private != NULL || quit) |
270 | break; | |
271 | debug2("bad passphrase given, try again..."); | |
a306f2dd | 272 | } |
a306f2dd | 273 | } |
274 | /* We no longer need the comment. */ | |
275 | xfree(comment); | |
276 | ||
ce404659 | 277 | if (private == NULL) { |
278 | if (!options.batch_mode) | |
279 | error("Bad passphrase."); | |
280 | ||
281 | /* Send a dummy response packet to avoid protocol error. */ | |
282 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); | |
283 | for (i = 0; i < 16; i++) | |
284 | packet_put_char(0); | |
285 | packet_send(); | |
286 | packet_write_wait(); | |
287 | ||
288 | /* Expect the server to reject it... */ | |
54a5250f | 289 | packet_read_expect(SSH_SMSG_FAILURE); |
ce404659 | 290 | BN_clear_free(challenge); |
291 | return 0; | |
292 | } | |
293 | ||
a306f2dd | 294 | /* Compute and send a response to the challenge. */ |
295 | respond_to_rsa_challenge(challenge, private->rsa); | |
296 | ||
383546c4 | 297 | /* Destroy the private key unless it in external hardware. */ |
298 | if (!(private->flags & KEY_FLAG_EXT)) | |
299 | key_free(private); | |
a306f2dd | 300 | |
301 | /* We no longer need the challenge. */ | |
302 | BN_clear_free(challenge); | |
303 | ||
304 | /* Wait for response from the server. */ | |
54a5250f | 305 | type = packet_read(); |
a306f2dd | 306 | if (type == SSH_SMSG_SUCCESS) { |
307 | debug("RSA authentication accepted by server."); | |
308 | return 1; | |
309 | } | |
310 | if (type != SSH_SMSG_FAILURE) | |
311 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); | |
312 | debug("RSA authentication refused."); | |
313 | return 0; | |
314 | } | |
315 | ||
316 | /* | |
317 | * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv | |
318 | * authentication and RSA host authentication. | |
319 | */ | |
396c147e | 320 | static int |
aeaa3d9e | 321 | try_rhosts_rsa_authentication(const char *local_user, Key * host_key) |
a306f2dd | 322 | { |
323 | int type; | |
324 | BIGNUM *challenge; | |
a306f2dd | 325 | |
326 | debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication."); | |
327 | ||
328 | /* Tell the server that we are willing to authenticate using this key. */ | |
329 | packet_start(SSH_CMSG_AUTH_RHOSTS_RSA); | |
449c5ba5 | 330 | packet_put_cstring(local_user); |
aeaa3d9e | 331 | packet_put_int(BN_num_bits(host_key->rsa->n)); |
332 | packet_put_bignum(host_key->rsa->e); | |
333 | packet_put_bignum(host_key->rsa->n); | |
a306f2dd | 334 | packet_send(); |
335 | packet_write_wait(); | |
336 | ||
337 | /* Wait for server's response. */ | |
54a5250f | 338 | type = packet_read(); |
a306f2dd | 339 | |
340 | /* The server responds with failure if it doesn't admit our | |
341 | .rhosts authentication or doesn't know our host key. */ | |
342 | if (type == SSH_SMSG_FAILURE) { | |
343 | debug("Server refused our rhosts authentication or host key."); | |
344 | return 0; | |
345 | } | |
346 | /* Otherwise, the server should respond with a challenge. */ | |
347 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) | |
348 | packet_disconnect("Protocol error during RSA authentication: %d", type); | |
349 | ||
350 | /* Get the challenge from the packet. */ | |
b775c6f2 | 351 | if ((challenge = BN_new()) == NULL) |
352 | fatal("try_rhosts_rsa_authentication: BN_new failed"); | |
20b279e6 | 353 | packet_get_bignum(challenge); |
95500969 | 354 | packet_check_eom(); |
a306f2dd | 355 | |
356 | debug("Received RSA challenge for host key from server."); | |
357 | ||
358 | /* Compute a response to the challenge. */ | |
aeaa3d9e | 359 | respond_to_rsa_challenge(challenge, host_key->rsa); |
a306f2dd | 360 | |
361 | /* We no longer need the challenge. */ | |
362 | BN_clear_free(challenge); | |
363 | ||
364 | /* Wait for response from the server. */ | |
54a5250f | 365 | type = packet_read(); |
a306f2dd | 366 | if (type == SSH_SMSG_SUCCESS) { |
367 | debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server."); | |
368 | return 1; | |
369 | } | |
370 | if (type != SSH_SMSG_FAILURE) | |
371 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); | |
372 | debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused."); | |
373 | return 0; | |
374 | } | |
375 | ||
ced49be2 | 376 | #ifdef KRB5 |
396c147e | 377 | static int |
ced49be2 | 378 | try_krb5_authentication(krb5_context *context, krb5_auth_context *auth_context) |
379 | { | |
380 | krb5_error_code problem; | |
381 | const char *tkfile; | |
382 | struct stat buf; | |
383 | krb5_ccache ccache = NULL; | |
384 | const char *remotehost; | |
385 | krb5_data ap; | |
54a5250f | 386 | int type; |
ced49be2 | 387 | krb5_ap_rep_enc_part *reply = NULL; |
388 | int ret; | |
184eed6a | 389 | |
ced49be2 | 390 | memset(&ap, 0, sizeof(ap)); |
184eed6a | 391 | |
ced49be2 | 392 | problem = krb5_init_context(context); |
393 | if (problem) { | |
394 | debug("Kerberos v5: krb5_init_context failed"); | |
395 | ret = 0; | |
396 | goto out; | |
397 | } | |
12928e80 | 398 | |
399 | problem = krb5_auth_con_init(*context, auth_context); | |
400 | if (problem) { | |
401 | debug("Kerberos v5: krb5_auth_con_init failed"); | |
402 | ret = 0; | |
403 | goto out; | |
404 | } | |
405 | ||
406 | #ifndef HEIMDAL | |
407 | problem = krb5_auth_con_setflags(*context, *auth_context, | |
408 | KRB5_AUTH_CONTEXT_RET_TIME); | |
409 | if (problem) { | |
410 | debug("Keberos v5: krb5_auth_con_setflags failed"); | |
411 | ret = 0; | |
412 | goto out; | |
413 | } | |
414 | #endif | |
184eed6a | 415 | |
ced49be2 | 416 | tkfile = krb5_cc_default_name(*context); |
417 | if (strncmp(tkfile, "FILE:", 5) == 0) | |
418 | tkfile += 5; | |
184eed6a | 419 | |
ced49be2 | 420 | if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) { |
421 | debug("Kerberos v5: could not get default ccache (permission denied)."); | |
422 | ret = 0; | |
423 | goto out; | |
424 | } | |
184eed6a | 425 | |
ced49be2 | 426 | problem = krb5_cc_default(*context, &ccache); |
427 | if (problem) { | |
428 | debug("Kerberos v5: krb5_cc_default failed: %s", | |
429 | krb5_get_err_text(*context, problem)); | |
430 | ret = 0; | |
431 | goto out; | |
432 | } | |
184eed6a | 433 | |
ced49be2 | 434 | remotehost = get_canonical_hostname(1); |
184eed6a | 435 | |
ced49be2 | 436 | problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED, |
437 | "host", remotehost, NULL, ccache, &ap); | |
438 | if (problem) { | |
439 | debug("Kerberos v5: krb5_mk_req failed: %s", | |
440 | krb5_get_err_text(*context, problem)); | |
441 | ret = 0; | |
442 | goto out; | |
443 | } | |
184eed6a | 444 | |
ced49be2 | 445 | packet_start(SSH_CMSG_AUTH_KERBEROS); |
446 | packet_put_string((char *) ap.data, ap.length); | |
447 | packet_send(); | |
448 | packet_write_wait(); | |
184eed6a | 449 | |
ced49be2 | 450 | xfree(ap.data); |
451 | ap.length = 0; | |
184eed6a | 452 | |
54a5250f | 453 | type = packet_read(); |
ced49be2 | 454 | switch (type) { |
184eed6a | 455 | case SSH_SMSG_FAILURE: |
456 | /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */ | |
457 | debug("Kerberos v5 authentication failed."); | |
458 | ret = 0; | |
459 | break; | |
460 | ||
ced49be2 | 461 | case SSH_SMSG_AUTH_KERBEROS_RESPONSE: |
184eed6a | 462 | /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */ |
463 | debug("Kerberos v5 authentication accepted."); | |
464 | ||
465 | /* Get server's response. */ | |
466 | ap.data = packet_get_string((unsigned int *) &ap.length); | |
95500969 | 467 | packet_check_eom(); |
184eed6a | 468 | /* XXX je to dobre? */ |
469 | ||
470 | problem = krb5_rd_rep(*context, *auth_context, &ap, &reply); | |
471 | if (problem) { | |
ced49be2 | 472 | ret = 0; |
473 | } | |
474 | ret = 1; | |
475 | break; | |
184eed6a | 476 | |
ced49be2 | 477 | default: |
478 | packet_disconnect("Protocol error on Kerberos v5 response: %d", | |
479 | type); | |
480 | ret = 0; | |
481 | break; | |
184eed6a | 482 | |
ced49be2 | 483 | } |
184eed6a | 484 | |
ced49be2 | 485 | out: |
486 | if (ccache != NULL) | |
487 | krb5_cc_close(*context, ccache); | |
488 | if (reply != NULL) | |
489 | krb5_free_ap_rep_enc_part(*context, reply); | |
490 | if (ap.length > 0) | |
12928e80 | 491 | #ifdef HEIMDAL |
ced49be2 | 492 | krb5_data_free(&ap); |
12928e80 | 493 | #else |
494 | krb5_free_data_contents(*context, &ap); | |
495 | #endif | |
184eed6a | 496 | |
ced49be2 | 497 | return (ret); |
498 | } | |
499 | ||
500 | static void | |
501 | send_krb5_tgt(krb5_context context, krb5_auth_context auth_context) | |
502 | { | |
54a5250f | 503 | int fd, type; |
ced49be2 | 504 | krb5_error_code problem; |
505 | krb5_data outbuf; | |
506 | krb5_ccache ccache = NULL; | |
507 | krb5_creds creds; | |
12928e80 | 508 | #ifdef HEIMDAL |
ced49be2 | 509 | krb5_kdc_flags flags; |
12928e80 | 510 | #else |
511 | int forwardable; | |
512 | #endif | |
ced49be2 | 513 | const char *remotehost; |
184eed6a | 514 | |
ced49be2 | 515 | memset(&creds, 0, sizeof(creds)); |
516 | memset(&outbuf, 0, sizeof(outbuf)); | |
184eed6a | 517 | |
ced49be2 | 518 | fd = packet_get_connection_in(); |
184eed6a | 519 | |
12928e80 | 520 | #ifdef HEIMDAL |
ced49be2 | 521 | problem = krb5_auth_con_setaddrs_from_fd(context, auth_context, &fd); |
12928e80 | 522 | #else |
523 | problem = krb5_auth_con_genaddrs(context, auth_context, fd, | |
524 | KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR | | |
525 | KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR); | |
526 | #endif | |
ced49be2 | 527 | if (problem) |
528 | goto out; | |
184eed6a | 529 | |
ced49be2 | 530 | problem = krb5_cc_default(context, &ccache); |
531 | if (problem) | |
532 | goto out; | |
184eed6a | 533 | |
ced49be2 | 534 | problem = krb5_cc_get_principal(context, ccache, &creds.client); |
535 | if (problem) | |
536 | goto out; | |
184eed6a | 537 | |
12928e80 | 538 | remotehost = get_canonical_hostname(1); |
539 | ||
540 | #ifdef HEIMDAL | |
ced49be2 | 541 | problem = krb5_build_principal(context, &creds.server, |
542 | strlen(creds.client->realm), creds.client->realm, | |
543 | "krbtgt", creds.client->realm, NULL); | |
12928e80 | 544 | #else |
545 | problem = krb5_build_principal(context, &creds.server, | |
546 | creds.client->realm.length, creds.client->realm.data, | |
547 | "host", remotehost, NULL); | |
548 | #endif | |
ced49be2 | 549 | if (problem) |
550 | goto out; | |
184eed6a | 551 | |
ced49be2 | 552 | creds.times.endtime = 0; |
184eed6a | 553 | |
12928e80 | 554 | #ifdef HEIMDAL |
ced49be2 | 555 | flags.i = 0; |
556 | flags.b.forwarded = 1; | |
557 | flags.b.forwardable = krb5_config_get_bool(context, NULL, | |
558 | "libdefaults", "forwardable", NULL); | |
ced49be2 | 559 | problem = krb5_get_forwarded_creds(context, auth_context, |
560 | ccache, flags.i, remotehost, &creds, &outbuf); | |
12928e80 | 561 | #else |
562 | forwardable = 1; | |
563 | problem = krb5_fwd_tgt_creds(context, auth_context, remotehost, | |
564 | creds.client, creds.server, ccache, forwardable, &outbuf); | |
565 | #endif | |
566 | ||
ced49be2 | 567 | if (problem) |
568 | goto out; | |
184eed6a | 569 | |
ced49be2 | 570 | packet_start(SSH_CMSG_HAVE_KERBEROS_TGT); |
571 | packet_put_string((char *)outbuf.data, outbuf.length); | |
572 | packet_send(); | |
573 | packet_write_wait(); | |
184eed6a | 574 | |
54a5250f | 575 | type = packet_read(); |
184eed6a | 576 | |
ced49be2 | 577 | if (type == SSH_SMSG_SUCCESS) { |
578 | char *pname; | |
184eed6a | 579 | |
ced49be2 | 580 | krb5_unparse_name(context, creds.client, &pname); |
581 | debug("Kerberos v5 TGT forwarded (%s).", pname); | |
582 | xfree(pname); | |
583 | } else | |
584 | debug("Kerberos v5 TGT forwarding failed."); | |
184eed6a | 585 | |
ced49be2 | 586 | return; |
184eed6a | 587 | |
ced49be2 | 588 | out: |
589 | if (problem) | |
590 | debug("Kerberos v5 TGT forwarding failed: %s", | |
591 | krb5_get_err_text(context, problem)); | |
592 | if (creds.client) | |
593 | krb5_free_principal(context, creds.client); | |
594 | if (creds.server) | |
595 | krb5_free_principal(context, creds.server); | |
596 | if (ccache) | |
597 | krb5_cc_close(context, ccache); | |
598 | if (outbuf.data) | |
599 | xfree(outbuf.data); | |
600 | } | |
601 | #endif /* KRB5 */ | |
602 | ||
a306f2dd | 603 | /* |
604 | * Tries to authenticate with any string-based challenge/response system. | |
605 | * Note that the client code is not tied to s/key or TIS. | |
606 | */ | |
396c147e | 607 | static int |
5ba55ada | 608 | try_challenge_response_authentication(void) |
a306f2dd | 609 | { |
610 | int type, i; | |
1e3b8b07 | 611 | u_int clen; |
e3a70753 | 612 | char prompt[1024]; |
a306f2dd | 613 | char *challenge, *response; |
aa8003d6 | 614 | |
615 | debug("Doing challenge response authentication."); | |
616 | ||
a306f2dd | 617 | for (i = 0; i < options.number_of_password_prompts; i++) { |
e3a70753 | 618 | /* request a challenge */ |
619 | packet_start(SSH_CMSG_AUTH_TIS); | |
620 | packet_send(); | |
621 | packet_write_wait(); | |
622 | ||
54a5250f | 623 | type = packet_read(); |
e3a70753 | 624 | if (type != SSH_SMSG_FAILURE && |
625 | type != SSH_SMSG_AUTH_TIS_CHALLENGE) { | |
626 | packet_disconnect("Protocol error: got %d in response " | |
d464095c | 627 | "to SSH_CMSG_AUTH_TIS", type); |
e3a70753 | 628 | } |
629 | if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) { | |
d464095c | 630 | debug("No challenge."); |
e3a70753 | 631 | return 0; |
632 | } | |
633 | challenge = packet_get_string(&clen); | |
95500969 | 634 | packet_check_eom(); |
59c97189 | 635 | snprintf(prompt, sizeof prompt, "%s%s", challenge, |
184eed6a | 636 | strchr(challenge, '\n') ? "" : "\nResponse: "); |
e3a70753 | 637 | xfree(challenge); |
a306f2dd | 638 | if (i != 0) |
639 | error("Permission denied, please try again."); | |
e3a70753 | 640 | if (options.cipher == SSH_CIPHER_NONE) |
bbe88b6d | 641 | logit("WARNING: Encryption is disabled! " |
134c552b | 642 | "Response will be transmitted in clear text."); |
e3a70753 | 643 | response = read_passphrase(prompt, 0); |
644 | if (strcmp(response, "") == 0) { | |
645 | xfree(response); | |
646 | break; | |
647 | } | |
a306f2dd | 648 | packet_start(SSH_CMSG_AUTH_TIS_RESPONSE); |
0ae4fe1d | 649 | ssh_put_password(response); |
a306f2dd | 650 | memset(response, 0, strlen(response)); |
651 | xfree(response); | |
652 | packet_send(); | |
653 | packet_write_wait(); | |
54a5250f | 654 | type = packet_read(); |
a306f2dd | 655 | if (type == SSH_SMSG_SUCCESS) |
656 | return 1; | |
657 | if (type != SSH_SMSG_FAILURE) | |
658 | packet_disconnect("Protocol error: got %d in response " | |
d464095c | 659 | "to SSH_CMSG_AUTH_TIS_RESPONSE", type); |
a306f2dd | 660 | } |
661 | /* failure */ | |
662 | return 0; | |
663 | } | |
664 | ||
665 | /* | |
666 | * Tries to authenticate with plain passwd authentication. | |
667 | */ | |
396c147e | 668 | static int |
a306f2dd | 669 | try_password_authentication(char *prompt) |
670 | { | |
54a5250f | 671 | int type, i; |
a306f2dd | 672 | char *password; |
673 | ||
674 | debug("Doing password authentication."); | |
675 | if (options.cipher == SSH_CIPHER_NONE) | |
bbe88b6d | 676 | logit("WARNING: Encryption is disabled! Password will be transmitted in clear text."); |
a306f2dd | 677 | for (i = 0; i < options.number_of_password_prompts; i++) { |
678 | if (i != 0) | |
679 | error("Permission denied, please try again."); | |
680 | password = read_passphrase(prompt, 0); | |
681 | packet_start(SSH_CMSG_AUTH_PASSWORD); | |
0ae4fe1d | 682 | ssh_put_password(password); |
a306f2dd | 683 | memset(password, 0, strlen(password)); |
684 | xfree(password); | |
685 | packet_send(); | |
686 | packet_write_wait(); | |
687 | ||
54a5250f | 688 | type = packet_read(); |
a306f2dd | 689 | if (type == SSH_SMSG_SUCCESS) |
690 | return 1; | |
691 | if (type != SSH_SMSG_FAILURE) | |
692 | packet_disconnect("Protocol error: got %d in response to passwd auth", type); | |
693 | } | |
694 | /* failure */ | |
695 | return 0; | |
696 | } | |
697 | ||
698 | /* | |
699 | * SSH1 key exchange | |
700 | */ | |
701 | void | |
702 | ssh_kex(char *host, struct sockaddr *hostaddr) | |
703 | { | |
704 | int i; | |
705 | BIGNUM *key; | |
b775c6f2 | 706 | Key *host_key, *server_key; |
a306f2dd | 707 | int bits, rbits; |
708 | int ssh_cipher_default = SSH_CIPHER_3DES; | |
1e3b8b07 | 709 | u_char session_key[SSH_SESSION_KEY_LENGTH]; |
710 | u_char cookie[8]; | |
711 | u_int supported_ciphers; | |
712 | u_int server_flags, client_flags; | |
a306f2dd | 713 | u_int32_t rand = 0; |
714 | ||
715 | debug("Waiting for server public key."); | |
716 | ||
717 | /* Wait for a public key packet from the server. */ | |
54a5250f | 718 | packet_read_expect(SSH_SMSG_PUBLIC_KEY); |
a306f2dd | 719 | |
720 | /* Get cookie from the packet. */ | |
721 | for (i = 0; i < 8; i++) | |
722 | cookie[i] = packet_get_char(); | |
723 | ||
724 | /* Get the public key. */ | |
b775c6f2 | 725 | server_key = key_new(KEY_RSA1); |
726 | bits = packet_get_int(); | |
20b279e6 | 727 | packet_get_bignum(server_key->rsa->e); |
728 | packet_get_bignum(server_key->rsa->n); | |
a306f2dd | 729 | |
b775c6f2 | 730 | rbits = BN_num_bits(server_key->rsa->n); |
a306f2dd | 731 | if (bits != rbits) { |
bbe88b6d | 732 | logit("Warning: Server lies about size of server public key: " |
a306f2dd | 733 | "actual size is %d bits vs. announced %d.", rbits, bits); |
bbe88b6d | 734 | logit("Warning: This may be due to an old implementation of ssh."); |
a306f2dd | 735 | } |
736 | /* Get the host key. */ | |
b775c6f2 | 737 | host_key = key_new(KEY_RSA1); |
738 | bits = packet_get_int(); | |
20b279e6 | 739 | packet_get_bignum(host_key->rsa->e); |
740 | packet_get_bignum(host_key->rsa->n); | |
a306f2dd | 741 | |
b775c6f2 | 742 | rbits = BN_num_bits(host_key->rsa->n); |
a306f2dd | 743 | if (bits != rbits) { |
bbe88b6d | 744 | logit("Warning: Server lies about size of server host key: " |
a306f2dd | 745 | "actual size is %d bits vs. announced %d.", rbits, bits); |
bbe88b6d | 746 | logit("Warning: This may be due to an old implementation of ssh."); |
a306f2dd | 747 | } |
748 | ||
749 | /* Get protocol flags. */ | |
750 | server_flags = packet_get_int(); | |
751 | packet_set_protocol_flags(server_flags); | |
752 | ||
753 | supported_ciphers = packet_get_int(); | |
754 | supported_authentications = packet_get_int(); | |
95500969 | 755 | packet_check_eom(); |
a306f2dd | 756 | |
757 | debug("Received server public key (%d bits) and host key (%d bits).", | |
b775c6f2 | 758 | BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n)); |
a306f2dd | 759 | |
b775c6f2 | 760 | if (verify_host_key(host, hostaddr, host_key) == -1) |
01e9ef57 | 761 | fatal("Host key verification failed."); |
a306f2dd | 762 | |
763 | client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN; | |
764 | ||
b775c6f2 | 765 | compute_session_id(session_id, cookie, host_key->rsa->n, server_key->rsa->n); |
a306f2dd | 766 | |
767 | /* Generate a session key. */ | |
768 | arc4random_stir(); | |
769 | ||
770 | /* | |
771 | * Generate an encryption key for the session. The key is a 256 bit | |
772 | * random number, interpreted as a 32-byte key, with the least | |
773 | * significant 8 bits being the first byte of the key. | |
774 | */ | |
775 | for (i = 0; i < 32; i++) { | |
776 | if (i % 4 == 0) | |
777 | rand = arc4random(); | |
778 | session_key[i] = rand & 0xff; | |
779 | rand >>= 8; | |
780 | } | |
781 | ||
782 | /* | |
783 | * According to the protocol spec, the first byte of the session key | |
784 | * is the highest byte of the integer. The session key is xored with | |
785 | * the first 16 bytes of the session id. | |
786 | */ | |
b775c6f2 | 787 | if ((key = BN_new()) == NULL) |
788 | fatal("respond_to_rsa_challenge: BN_new failed"); | |
a306f2dd | 789 | BN_set_word(key, 0); |
790 | for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) { | |
791 | BN_lshift(key, key, 8); | |
792 | if (i < 16) | |
793 | BN_add_word(key, session_key[i] ^ session_id[i]); | |
794 | else | |
795 | BN_add_word(key, session_key[i]); | |
796 | } | |
797 | ||
798 | /* | |
799 | * Encrypt the integer using the public key and host key of the | |
800 | * server (key with smaller modulus first). | |
801 | */ | |
b775c6f2 | 802 | if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) { |
a306f2dd | 803 | /* Public key has smaller modulus. */ |
b775c6f2 | 804 | if (BN_num_bits(host_key->rsa->n) < |
805 | BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) { | |
806 | fatal("respond_to_rsa_challenge: host_key %d < server_key %d + " | |
184eed6a | 807 | "SSH_KEY_BITS_RESERVED %d", |
b775c6f2 | 808 | BN_num_bits(host_key->rsa->n), |
809 | BN_num_bits(server_key->rsa->n), | |
184eed6a | 810 | SSH_KEY_BITS_RESERVED); |
a306f2dd | 811 | } |
b775c6f2 | 812 | rsa_public_encrypt(key, key, server_key->rsa); |
813 | rsa_public_encrypt(key, key, host_key->rsa); | |
a306f2dd | 814 | } else { |
815 | /* Host key has smaller modulus (or they are equal). */ | |
b775c6f2 | 816 | if (BN_num_bits(server_key->rsa->n) < |
817 | BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) { | |
818 | fatal("respond_to_rsa_challenge: server_key %d < host_key %d + " | |
184eed6a | 819 | "SSH_KEY_BITS_RESERVED %d", |
b775c6f2 | 820 | BN_num_bits(server_key->rsa->n), |
821 | BN_num_bits(host_key->rsa->n), | |
184eed6a | 822 | SSH_KEY_BITS_RESERVED); |
a306f2dd | 823 | } |
b775c6f2 | 824 | rsa_public_encrypt(key, key, host_key->rsa); |
825 | rsa_public_encrypt(key, key, server_key->rsa); | |
a306f2dd | 826 | } |
827 | ||
828 | /* Destroy the public keys since we no longer need them. */ | |
b775c6f2 | 829 | key_free(server_key); |
830 | key_free(host_key); | |
a306f2dd | 831 | |
c523303b | 832 | if (options.cipher == SSH_CIPHER_NOT_SET) { |
833 | if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default)) | |
834 | options.cipher = ssh_cipher_default; | |
835 | } else if (options.cipher == SSH_CIPHER_ILLEGAL || | |
836 | !(cipher_mask_ssh1(1) & (1 << options.cipher))) { | |
bbe88b6d | 837 | logit("No valid SSH1 cipher, using %.100s instead.", |
94ec8c6b | 838 | cipher_name(ssh_cipher_default)); |
839 | options.cipher = ssh_cipher_default; | |
a306f2dd | 840 | } |
841 | /* Check that the selected cipher is supported. */ | |
842 | if (!(supported_ciphers & (1 << options.cipher))) | |
843 | fatal("Selected cipher type %.100s not supported by server.", | |
184eed6a | 844 | cipher_name(options.cipher)); |
a306f2dd | 845 | |
846 | debug("Encryption type: %.100s", cipher_name(options.cipher)); | |
847 | ||
848 | /* Send the encrypted session key to the server. */ | |
849 | packet_start(SSH_CMSG_SESSION_KEY); | |
850 | packet_put_char(options.cipher); | |
851 | ||
852 | /* Send the cookie back to the server. */ | |
853 | for (i = 0; i < 8; i++) | |
854 | packet_put_char(cookie[i]); | |
855 | ||
856 | /* Send and destroy the encrypted encryption key integer. */ | |
857 | packet_put_bignum(key); | |
858 | BN_clear_free(key); | |
859 | ||
860 | /* Send protocol flags. */ | |
861 | packet_put_int(client_flags); | |
862 | ||
863 | /* Send the packet now. */ | |
864 | packet_send(); | |
865 | packet_write_wait(); | |
866 | ||
867 | debug("Sent encrypted session key."); | |
868 | ||
869 | /* Set the encryption key. */ | |
870 | packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher); | |
871 | ||
872 | /* We will no longer need the session key here. Destroy any extra copies. */ | |
873 | memset(session_key, 0, sizeof(session_key)); | |
874 | ||
875 | /* | |
876 | * Expect a success message from the server. Note that this message | |
877 | * will be received in encrypted form. | |
878 | */ | |
54a5250f | 879 | packet_read_expect(SSH_SMSG_SUCCESS); |
a306f2dd | 880 | |
881 | debug("Received encrypted confirmation."); | |
882 | } | |
883 | ||
884 | /* | |
885 | * Authenticate user | |
886 | */ | |
887 | void | |
8002af61 | 888 | ssh_userauth1(const char *local_user, const char *server_user, char *host, |
39c00dc2 | 889 | Sensitive *sensitive) |
a306f2dd | 890 | { |
ced49be2 | 891 | #ifdef KRB5 |
892 | krb5_context context = NULL; | |
893 | krb5_auth_context auth_context = NULL; | |
894 | #endif | |
a306f2dd | 895 | int i, type; |
184eed6a | 896 | |
a306f2dd | 897 | if (supported_authentications == 0) |
8002af61 | 898 | fatal("ssh_userauth1: server supports no auth methods"); |
a306f2dd | 899 | |
900 | /* Send the name of the user to log in as on the server. */ | |
901 | packet_start(SSH_CMSG_USER); | |
449c5ba5 | 902 | packet_put_cstring(server_user); |
a306f2dd | 903 | packet_send(); |
904 | packet_write_wait(); | |
905 | ||
906 | /* | |
907 | * The server should respond with success if no authentication is | |
908 | * needed (the user has no password). Otherwise the server responds | |
909 | * with failure. | |
910 | */ | |
54a5250f | 911 | type = packet_read(); |
a306f2dd | 912 | |
913 | /* check whether the connection was accepted without authentication. */ | |
914 | if (type == SSH_SMSG_SUCCESS) | |
ced49be2 | 915 | goto success; |
a306f2dd | 916 | if (type != SSH_SMSG_FAILURE) |
ced49be2 | 917 | packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type); |
184eed6a | 918 | |
ced49be2 | 919 | #ifdef KRB5 |
920 | if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) && | |
184eed6a | 921 | options.kerberos_authentication) { |
ced49be2 | 922 | debug("Trying Kerberos v5 authentication."); |
184eed6a | 923 | |
ced49be2 | 924 | if (try_krb5_authentication(&context, &auth_context)) { |
54a5250f | 925 | type = packet_read(); |
ced49be2 | 926 | if (type == SSH_SMSG_SUCCESS) |
927 | goto success; | |
928 | if (type != SSH_SMSG_FAILURE) | |
929 | packet_disconnect("Protocol error: got %d in response to Kerberos v5 auth", type); | |
930 | } | |
a306f2dd | 931 | } |
ced49be2 | 932 | #endif /* KRB5 */ |
184eed6a | 933 | |
a306f2dd | 934 | /* |
935 | * Try .rhosts or /etc/hosts.equiv authentication with RSA host | |
936 | * authentication. | |
937 | */ | |
938 | if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) && | |
8002af61 | 939 | options.rhosts_rsa_authentication) { |
39c00dc2 | 940 | for (i = 0; i < sensitive->nkeys; i++) { |
941 | if (sensitive->keys[i] != NULL && | |
942 | sensitive->keys[i]->type == KEY_RSA1 && | |
943 | try_rhosts_rsa_authentication(local_user, | |
944 | sensitive->keys[i])) | |
ced49be2 | 945 | goto success; |
8002af61 | 946 | } |
a306f2dd | 947 | } |
948 | /* Try RSA authentication if the server supports it. */ | |
949 | if ((supported_authentications & (1 << SSH_AUTH_RSA)) && | |
950 | options.rsa_authentication) { | |
951 | /* | |
952 | * Try RSA authentication using the authentication agent. The | |
953 | * agent is tried first because no passphrase is needed for | |
954 | * it, whereas identity files may require passphrases. | |
955 | */ | |
956 | if (try_agent_authentication()) | |
ced49be2 | 957 | goto success; |
a306f2dd | 958 | |
959 | /* Try RSA authentication for each identity. */ | |
960 | for (i = 0; i < options.num_identity_files; i++) | |
fee56204 | 961 | if (options.identity_keys[i] != NULL && |
962 | options.identity_keys[i]->type == KEY_RSA1 && | |
383546c4 | 963 | try_rsa_authentication(i)) |
ced49be2 | 964 | goto success; |
a306f2dd | 965 | } |
d464095c | 966 | /* Try challenge response authentication if the server supports it. */ |
a306f2dd | 967 | if ((supported_authentications & (1 << SSH_AUTH_TIS)) && |
5ba55ada | 968 | options.challenge_response_authentication && !options.batch_mode) { |
969 | if (try_challenge_response_authentication()) | |
ced49be2 | 970 | goto success; |
a306f2dd | 971 | } |
972 | /* Try password authentication if the server supports it. */ | |
973 | if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) && | |
974 | options.password_authentication && !options.batch_mode) { | |
975 | char prompt[80]; | |
976 | ||
b3b9ad8e | 977 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", |
a306f2dd | 978 | server_user, host); |
979 | if (try_password_authentication(prompt)) | |
ced49be2 | 980 | goto success; |
a306f2dd | 981 | } |
982 | /* All authentication methods have failed. Exit with an error message. */ | |
983 | fatal("Permission denied."); | |
984 | /* NOTREACHED */ | |
ced49be2 | 985 | |
986 | success: | |
987 | #ifdef KRB5 | |
988 | /* Try Kerberos v5 TGT passing. */ | |
989 | if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) && | |
990 | options.kerberos_tgt_passing && context && auth_context) { | |
991 | if (options.cipher == SSH_CIPHER_NONE) | |
bbe88b6d | 992 | logit("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!"); |
ced49be2 | 993 | send_krb5_tgt(context, auth_context); |
994 | } | |
995 | if (auth_context) | |
996 | krb5_auth_con_free(context, auth_context); | |
997 | if (context) | |
998 | krb5_free_context(context); | |
999 | #endif | |
62bb2c8f | 1000 | return; /* need statement after label */ |
a306f2dd | 1001 | } |