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