]>
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" | |
ce404659 | 16 | RCSID("$OpenBSD: sshconnect1.c,v 1.36 2001/06/23 22:37:46 markus Exp $"); |
a306f2dd | 17 | |
18 | #include <openssl/bn.h> | |
a306f2dd | 19 | #include <openssl/evp.h> |
20 | ||
42f11eb2 | 21 | #ifdef KRB4 |
22 | #include <krb.h> | |
42f11eb2 | 23 | #endif |
24 | #ifdef AFS | |
25 | #include <kafs.h> | |
53a24016 | 26 | #include "radix.h" |
42f11eb2 | 27 | #endif |
28 | ||
29 | #include "ssh.h" | |
30 | #include "ssh1.h" | |
a306f2dd | 31 | #include "xmalloc.h" |
32 | #include "rsa.h" | |
a306f2dd | 33 | #include "buffer.h" |
34 | #include "packet.h" | |
a306f2dd | 35 | #include "mpaux.h" |
36 | #include "uidswap.h" | |
42f11eb2 | 37 | #include "log.h" |
a306f2dd | 38 | #include "readconf.h" |
39 | #include "key.h" | |
4c8722d9 | 40 | #include "authfd.h" |
a306f2dd | 41 | #include "sshconnect.h" |
42 | #include "authfile.h" | |
42f11eb2 | 43 | #include "readpass.h" |
44 | #include "cipher.h" | |
45 | #include "canohost.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 | int plen, clen; |
67 | Key *key; | |
68 | BIGNUM *challenge; | |
a306f2dd | 69 | |
70 | /* Get connection to the agent. */ | |
71 | auth = ssh_get_authentication_connection(); | |
72 | if (!auth) | |
73 | return 0; | |
74 | ||
a306f2dd | 75 | challenge = BN_new(); |
76 | ||
77 | /* Loop through identities served by the agent. */ | |
2e73a022 | 78 | for (key = ssh_get_first_identity(auth, &comment, 1); |
79 | key != NULL; | |
80 | key = ssh_get_next_identity(auth, &comment, 1)) { | |
a306f2dd | 81 | |
82 | /* Try this identity. */ | |
83 | debug("Trying RSA authentication via agent with '%.100s'", comment); | |
84 | xfree(comment); | |
85 | ||
86 | /* Tell the server that we are willing to authenticate using this key. */ | |
87 | packet_start(SSH_CMSG_AUTH_RSA); | |
2e73a022 | 88 | packet_put_bignum(key->rsa->n); |
a306f2dd | 89 | packet_send(); |
90 | packet_write_wait(); | |
91 | ||
92 | /* Wait for server's response. */ | |
93 | type = packet_read(&plen); | |
94 | ||
95 | /* The server sends failure if it doesn\'t like our key or | |
96 | does not support RSA authentication. */ | |
97 | if (type == SSH_SMSG_FAILURE) { | |
98 | debug("Server refused our key."); | |
2e73a022 | 99 | key_free(key); |
a306f2dd | 100 | continue; |
101 | } | |
102 | /* Otherwise it should have sent a challenge. */ | |
103 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) | |
104 | packet_disconnect("Protocol error during RSA authentication: %d", | |
105 | type); | |
106 | ||
107 | packet_get_bignum(challenge, &clen); | |
108 | ||
109 | packet_integrity_check(plen, clen, type); | |
110 | ||
111 | debug("Received RSA challenge from server."); | |
112 | ||
113 | /* Ask the agent to decrypt the challenge. */ | |
2e73a022 | 114 | if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) { |
115 | /* | |
116 | * The agent failed to authenticate this identifier | |
117 | * although it advertised it supports this. Just | |
118 | * return a wrong value. | |
119 | */ | |
a306f2dd | 120 | log("Authentication agent failed to decrypt challenge."); |
121 | memset(response, 0, sizeof(response)); | |
122 | } | |
2e73a022 | 123 | key_free(key); |
a306f2dd | 124 | debug("Sending response to RSA challenge."); |
125 | ||
126 | /* Send the decrypted challenge back to the server. */ | |
127 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); | |
128 | for (i = 0; i < 16; i++) | |
129 | packet_put_char(response[i]); | |
130 | packet_send(); | |
131 | packet_write_wait(); | |
132 | ||
133 | /* Wait for response from the server. */ | |
134 | type = packet_read(&plen); | |
135 | ||
136 | /* The server returns success if it accepted the authentication. */ | |
137 | if (type == SSH_SMSG_SUCCESS) { | |
eea39c02 | 138 | ssh_close_authentication_connection(auth); |
a306f2dd | 139 | BN_clear_free(challenge); |
2e73a022 | 140 | debug("RSA authentication accepted by server."); |
a306f2dd | 141 | return 1; |
142 | } | |
143 | /* Otherwise it should return failure. */ | |
144 | if (type != SSH_SMSG_FAILURE) | |
145 | packet_disconnect("Protocol error waiting RSA auth response: %d", | |
146 | type); | |
147 | } | |
eea39c02 | 148 | ssh_close_authentication_connection(auth); |
a306f2dd | 149 | BN_clear_free(challenge); |
a306f2dd | 150 | debug("RSA authentication using agent refused."); |
151 | return 0; | |
152 | } | |
153 | ||
154 | /* | |
155 | * Computes the proper response to a RSA challenge, and sends the response to | |
156 | * the server. | |
157 | */ | |
396c147e | 158 | static void |
a306f2dd | 159 | respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv) |
160 | { | |
1e3b8b07 | 161 | u_char buf[32], response[16]; |
a306f2dd | 162 | MD5_CTX md; |
163 | int i, len; | |
164 | ||
165 | /* Decrypt the challenge using the private key. */ | |
46aa2d1f | 166 | /* XXX think about Bleichenbacher, too */ |
167 | if (rsa_private_decrypt(challenge, challenge, prv) <= 0) | |
168 | packet_disconnect( | |
169 | "respond_to_rsa_challenge: rsa_private_decrypt failed"); | |
a306f2dd | 170 | |
171 | /* Compute the response. */ | |
172 | /* The response is MD5 of decrypted challenge plus session id. */ | |
173 | len = BN_num_bytes(challenge); | |
174 | if (len <= 0 || len > sizeof(buf)) | |
46aa2d1f | 175 | packet_disconnect( |
176 | "respond_to_rsa_challenge: bad challenge length %d", len); | |
a306f2dd | 177 | |
178 | memset(buf, 0, sizeof(buf)); | |
179 | BN_bn2bin(challenge, buf + sizeof(buf) - len); | |
180 | MD5_Init(&md); | |
181 | MD5_Update(&md, buf, 32); | |
182 | MD5_Update(&md, session_id, 16); | |
183 | MD5_Final(response, &md); | |
184 | ||
185 | debug("Sending response to host key RSA challenge."); | |
186 | ||
187 | /* Send the response back to the server. */ | |
188 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); | |
189 | for (i = 0; i < 16; i++) | |
190 | packet_put_char(response[i]); | |
191 | packet_send(); | |
192 | packet_write_wait(); | |
193 | ||
194 | memset(buf, 0, sizeof(buf)); | |
195 | memset(response, 0, sizeof(response)); | |
196 | memset(&md, 0, sizeof(md)); | |
197 | } | |
198 | ||
199 | /* | |
200 | * Checks if the user has authentication file, and if so, tries to authenticate | |
201 | * the user using it. | |
202 | */ | |
396c147e | 203 | static int |
a306f2dd | 204 | try_rsa_authentication(const char *authfile) |
205 | { | |
206 | BIGNUM *challenge; | |
ce404659 | 207 | Key *public, *private; |
208 | char buf[300], *passphrase, *comment; | |
209 | int i, type, quit, plen, clen; | |
a306f2dd | 210 | |
211 | /* Try to load identification for the authentication key. */ | |
aeaa3d9e | 212 | /* XXKEYLOAD */ |
213 | public = key_load_public_type(KEY_RSA1, authfile, &comment); | |
214 | if (public == NULL) { | |
a306f2dd | 215 | /* Could not load it. Fail. */ |
216 | return 0; | |
217 | } | |
218 | debug("Trying RSA authentication with key '%.100s'", comment); | |
219 | ||
220 | /* Tell the server that we are willing to authenticate using this key. */ | |
221 | packet_start(SSH_CMSG_AUTH_RSA); | |
222 | packet_put_bignum(public->rsa->n); | |
223 | packet_send(); | |
224 | packet_write_wait(); | |
225 | ||
226 | /* We no longer need the public key. */ | |
227 | key_free(public); | |
228 | ||
229 | /* Wait for server's response. */ | |
230 | type = packet_read(&plen); | |
231 | ||
232 | /* | |
233 | * The server responds with failure if it doesn\'t like our key or | |
234 | * doesn\'t support RSA authentication. | |
235 | */ | |
236 | if (type == SSH_SMSG_FAILURE) { | |
237 | debug("Server refused our key."); | |
238 | xfree(comment); | |
239 | return 0; | |
240 | } | |
241 | /* Otherwise, the server should respond with a challenge. */ | |
242 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) | |
243 | packet_disconnect("Protocol error during RSA authentication: %d", type); | |
244 | ||
245 | /* Get the challenge from the packet. */ | |
246 | challenge = BN_new(); | |
247 | packet_get_bignum(challenge, &clen); | |
248 | ||
249 | packet_integrity_check(plen, clen, type); | |
250 | ||
251 | debug("Received RSA challenge from server."); | |
252 | ||
a306f2dd | 253 | /* |
254 | * Load the private key. Try first with empty passphrase; if it | |
255 | * fails, ask for a passphrase. | |
256 | */ | |
aeaa3d9e | 257 | private = key_load_private_type(KEY_RSA1, authfile, "", NULL); |
ce404659 | 258 | if (private == NULL && !options.batch_mode) { |
259 | snprintf(buf, sizeof(buf), | |
260 | "Enter passphrase for RSA key '%.100s': ", comment); | |
261 | for (i = 0; i < options.number_of_password_prompts; i++) { | |
a306f2dd | 262 | passphrase = read_passphrase(buf, 0); |
ce404659 | 263 | if (strcmp(passphrase, "") != 0) { |
264 | private = key_load_private_type(KEY_RSA1, | |
265 | authfile, passphrase, NULL); | |
266 | quit = 0; | |
267 | } else { | |
268 | debug2("no passphrase given, try next key"); | |
269 | quit = 1; | |
270 | } | |
a306f2dd | 271 | memset(passphrase, 0, strlen(passphrase)); |
272 | xfree(passphrase); | |
ce404659 | 273 | if (private != NULL || quit) |
274 | break; | |
275 | debug2("bad passphrase given, try again..."); | |
a306f2dd | 276 | } |
a306f2dd | 277 | } |
278 | /* We no longer need the comment. */ | |
279 | xfree(comment); | |
280 | ||
ce404659 | 281 | if (private == NULL) { |
282 | if (!options.batch_mode) | |
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(&plen, SSH_SMSG_FAILURE); | |
294 | BN_clear_free(challenge); | |
295 | return 0; | |
296 | } | |
297 | ||
a306f2dd | 298 | /* Compute and send a response to the challenge. */ |
299 | respond_to_rsa_challenge(challenge, private->rsa); | |
300 | ||
301 | /* Destroy the private key. */ | |
302 | key_free(private); | |
303 | ||
304 | /* We no longer need the challenge. */ | |
305 | BN_clear_free(challenge); | |
306 | ||
307 | /* Wait for response from the server. */ | |
308 | type = packet_read(&plen); | |
309 | if (type == SSH_SMSG_SUCCESS) { | |
310 | debug("RSA authentication accepted by server."); | |
311 | return 1; | |
312 | } | |
313 | if (type != SSH_SMSG_FAILURE) | |
314 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); | |
315 | debug("RSA authentication refused."); | |
316 | return 0; | |
317 | } | |
318 | ||
319 | /* | |
320 | * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv | |
321 | * authentication and RSA host authentication. | |
322 | */ | |
396c147e | 323 | static int |
aeaa3d9e | 324 | try_rhosts_rsa_authentication(const char *local_user, Key * host_key) |
a306f2dd | 325 | { |
326 | int type; | |
327 | BIGNUM *challenge; | |
328 | int plen, clen; | |
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); | |
449c5ba5 | 334 | packet_put_cstring(local_user); |
aeaa3d9e | 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); | |
a306f2dd | 338 | packet_send(); |
339 | packet_write_wait(); | |
340 | ||
341 | /* Wait for server's response. */ | |
342 | type = packet_read(&plen); | |
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 | challenge = BN_new(); | |
356 | packet_get_bignum(challenge, &clen); | |
357 | ||
358 | packet_integrity_check(plen, clen, type); | |
359 | ||
360 | debug("Received RSA challenge for host key from server."); | |
361 | ||
362 | /* Compute a response to the challenge. */ | |
aeaa3d9e | 363 | respond_to_rsa_challenge(challenge, host_key->rsa); |
a306f2dd | 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(&plen); | |
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 | #ifdef KRB4 | |
396c147e | 381 | static int |
5ca51e19 | 382 | try_kerberos_authentication(void) |
a306f2dd | 383 | { |
384 | KTEXT_ST auth; /* Kerberos data */ | |
385 | char *reply; | |
386 | char inst[INST_SZ]; | |
387 | char *realm; | |
388 | CREDENTIALS cred; | |
389 | int r, type, plen; | |
390 | socklen_t slen; | |
391 | Key_schedule schedule; | |
392 | u_long checksum, cksum; | |
393 | MSG_DAT msg_data; | |
394 | struct sockaddr_in local, foreign; | |
395 | struct stat st; | |
396 | ||
397 | /* Don't do anything if we don't have any tickets. */ | |
398 | if (stat(tkt_string(), &st) < 0) | |
399 | return 0; | |
400 | ||
61e96248 | 401 | strncpy(inst, (char *) krb_get_phost(get_canonical_hostname(1)), INST_SZ); |
a306f2dd | 402 | |
61e96248 | 403 | realm = (char *) krb_realmofhost(get_canonical_hostname(1)); |
a306f2dd | 404 | if (!realm) { |
61e96248 | 405 | debug("Kerberos V4: no realm for %s", get_canonical_hostname(1)); |
a306f2dd | 406 | return 0; |
407 | } | |
408 | /* This can really be anything. */ | |
409 | checksum = (u_long) getpid(); | |
410 | ||
411 | r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum); | |
412 | if (r != KSUCCESS) { | |
413 | debug("Kerberos V4 krb_mk_req failed: %s", krb_err_txt[r]); | |
414 | return 0; | |
415 | } | |
416 | /* Get session key to decrypt the server's reply with. */ | |
417 | r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred); | |
418 | if (r != KSUCCESS) { | |
419 | debug("get_cred failed: %s", krb_err_txt[r]); | |
420 | return 0; | |
421 | } | |
422 | des_key_sched((des_cblock *) cred.session, schedule); | |
423 | ||
424 | /* Send authentication info to server. */ | |
425 | packet_start(SSH_CMSG_AUTH_KERBEROS); | |
426 | packet_put_string((char *) auth.dat, auth.length); | |
427 | packet_send(); | |
428 | packet_write_wait(); | |
429 | ||
430 | /* Zero the buffer. */ | |
431 | (void) memset(auth.dat, 0, MAX_KTXT_LEN); | |
432 | ||
433 | slen = sizeof(local); | |
434 | memset(&local, 0, sizeof(local)); | |
435 | if (getsockname(packet_get_connection_in(), | |
436 | (struct sockaddr *) & local, &slen) < 0) | |
437 | debug("getsockname failed: %s", strerror(errno)); | |
438 | ||
439 | slen = sizeof(foreign); | |
440 | memset(&foreign, 0, sizeof(foreign)); | |
441 | if (getpeername(packet_get_connection_in(), | |
442 | (struct sockaddr *) & foreign, &slen) < 0) { | |
443 | debug("getpeername failed: %s", strerror(errno)); | |
444 | fatal_cleanup(); | |
445 | } | |
446 | /* Get server reply. */ | |
447 | type = packet_read(&plen); | |
448 | switch (type) { | |
449 | case SSH_SMSG_FAILURE: | |
450 | /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */ | |
451 | debug("Kerberos V4 authentication failed."); | |
452 | return 0; | |
453 | break; | |
454 | ||
455 | case SSH_SMSG_AUTH_KERBEROS_RESPONSE: | |
456 | /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */ | |
457 | debug("Kerberos V4 authentication accepted."); | |
458 | ||
459 | /* Get server's response. */ | |
1e3b8b07 | 460 | reply = packet_get_string((u_int *) &auth.length); |
a306f2dd | 461 | memcpy(auth.dat, reply, auth.length); |
462 | xfree(reply); | |
463 | ||
464 | packet_integrity_check(plen, 4 + auth.length, type); | |
465 | ||
466 | /* | |
467 | * If his response isn't properly encrypted with the session | |
468 | * key, and the decrypted checksum fails to match, he's | |
469 | * bogus. Bail out. | |
470 | */ | |
471 | r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session, | |
472 | &foreign, &local, &msg_data); | |
473 | if (r != KSUCCESS) { | |
474 | debug("Kerberos V4 krb_rd_priv failed: %s", krb_err_txt[r]); | |
475 | packet_disconnect("Kerberos V4 challenge failed!"); | |
476 | } | |
477 | /* Fetch the (incremented) checksum that we supplied in the request. */ | |
478 | (void) memcpy((char *) &cksum, (char *) msg_data.app_data, sizeof(cksum)); | |
479 | cksum = ntohl(cksum); | |
480 | ||
481 | /* If it matches, we're golden. */ | |
482 | if (cksum == checksum + 1) { | |
483 | debug("Kerberos V4 challenge successful."); | |
484 | return 1; | |
485 | } else | |
486 | packet_disconnect("Kerberos V4 challenge failed!"); | |
487 | break; | |
488 | ||
489 | default: | |
490 | packet_disconnect("Protocol error on Kerberos V4 response: %d", type); | |
491 | } | |
492 | return 0; | |
493 | } | |
494 | ||
495 | #endif /* KRB4 */ | |
496 | ||
497 | #ifdef AFS | |
396c147e | 498 | static int |
5ca51e19 | 499 | send_kerberos_tgt(void) |
a306f2dd | 500 | { |
501 | CREDENTIALS *creds; | |
502 | char pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ]; | |
503 | int r, type, plen; | |
504 | char buffer[8192]; | |
505 | struct stat st; | |
506 | ||
507 | /* Don't do anything if we don't have any tickets. */ | |
508 | if (stat(tkt_string(), &st) < 0) | |
509 | return 0; | |
510 | ||
511 | creds = xmalloc(sizeof(*creds)); | |
512 | ||
513 | if ((r = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm)) != KSUCCESS) { | |
514 | debug("Kerberos V4 tf_fullname failed: %s", krb_err_txt[r]); | |
515 | return 0; | |
516 | } | |
517 | if ((r = krb_get_cred("krbtgt", prealm, prealm, creds)) != GC_OK) { | |
518 | debug("Kerberos V4 get_cred failed: %s", krb_err_txt[r]); | |
519 | return 0; | |
520 | } | |
521 | if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) { | |
522 | debug("Kerberos V4 ticket expired: %s", TKT_FILE); | |
523 | return 0; | |
524 | } | |
1e3b8b07 | 525 | creds_to_radix(creds, (u_char *)buffer, sizeof buffer); |
a306f2dd | 526 | xfree(creds); |
527 | ||
528 | packet_start(SSH_CMSG_HAVE_KERBEROS_TGT); | |
449c5ba5 | 529 | packet_put_cstring(buffer); |
a306f2dd | 530 | packet_send(); |
531 | packet_write_wait(); | |
532 | ||
533 | type = packet_read(&plen); | |
534 | ||
535 | if (type == SSH_SMSG_FAILURE) | |
536 | debug("Kerberos TGT for realm %s rejected.", prealm); | |
537 | else if (type != SSH_SMSG_SUCCESS) | |
538 | packet_disconnect("Protocol error on Kerberos TGT response: %d", type); | |
539 | ||
540 | return 1; | |
541 | } | |
542 | ||
396c147e | 543 | static void |
a306f2dd | 544 | send_afs_tokens(void) |
545 | { | |
546 | CREDENTIALS creds; | |
547 | struct ViceIoctl parms; | |
548 | struct ClearToken ct; | |
549 | int i, type, len, plen; | |
550 | char buf[2048], *p, *server_cell; | |
551 | char buffer[8192]; | |
552 | ||
553 | /* Move over ktc_GetToken, here's something leaner. */ | |
554 | for (i = 0; i < 100; i++) { /* just in case */ | |
555 | parms.in = (char *) &i; | |
556 | parms.in_size = sizeof(i); | |
557 | parms.out = buf; | |
558 | parms.out_size = sizeof(buf); | |
559 | if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0) | |
560 | break; | |
561 | p = buf; | |
562 | ||
563 | /* Get secret token. */ | |
1e3b8b07 | 564 | memcpy(&creds.ticket_st.length, p, sizeof(u_int)); |
a306f2dd | 565 | if (creds.ticket_st.length > MAX_KTXT_LEN) |
566 | break; | |
1e3b8b07 | 567 | p += sizeof(u_int); |
a306f2dd | 568 | memcpy(creds.ticket_st.dat, p, creds.ticket_st.length); |
569 | p += creds.ticket_st.length; | |
570 | ||
571 | /* Get clear token. */ | |
572 | memcpy(&len, p, sizeof(len)); | |
573 | if (len != sizeof(struct ClearToken)) | |
574 | break; | |
575 | p += sizeof(len); | |
576 | memcpy(&ct, p, len); | |
577 | p += len; | |
578 | p += sizeof(len); /* primary flag */ | |
579 | server_cell = p; | |
580 | ||
581 | /* Flesh out our credentials. */ | |
582 | strlcpy(creds.service, "afs", sizeof creds.service); | |
583 | creds.instance[0] = '\0'; | |
584 | strlcpy(creds.realm, server_cell, REALM_SZ); | |
585 | memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ); | |
586 | creds.issue_date = ct.BeginTimestamp; | |
587 | creds.lifetime = krb_time_to_life(creds.issue_date, ct.EndTimestamp); | |
588 | creds.kvno = ct.AuthHandle; | |
589 | snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId); | |
590 | creds.pinst[0] = '\0'; | |
591 | ||
592 | /* Encode token, ship it off. */ | |
b5c334cc | 593 | if (creds_to_radix(&creds, (u_char *) buffer, sizeof buffer) <= 0) |
a306f2dd | 594 | break; |
595 | packet_start(SSH_CMSG_HAVE_AFS_TOKEN); | |
449c5ba5 | 596 | packet_put_cstring(buffer); |
a306f2dd | 597 | packet_send(); |
598 | packet_write_wait(); | |
599 | ||
600 | /* Roger, Roger. Clearance, Clarence. What's your vector, | |
601 | Victor? */ | |
602 | type = packet_read(&plen); | |
603 | ||
604 | if (type == SSH_SMSG_FAILURE) | |
605 | debug("AFS token for cell %s rejected.", server_cell); | |
606 | else if (type != SSH_SMSG_SUCCESS) | |
607 | packet_disconnect("Protocol error on AFS token response: %d", type); | |
608 | } | |
609 | } | |
610 | ||
611 | #endif /* AFS */ | |
612 | ||
613 | /* | |
614 | * Tries to authenticate with any string-based challenge/response system. | |
615 | * Note that the client code is not tied to s/key or TIS. | |
616 | */ | |
396c147e | 617 | static int |
5ba55ada | 618 | try_challenge_response_authentication(void) |
a306f2dd | 619 | { |
620 | int type, i; | |
621 | int payload_len; | |
1e3b8b07 | 622 | u_int clen; |
e3a70753 | 623 | char prompt[1024]; |
a306f2dd | 624 | char *challenge, *response; |
625 | ||
d464095c | 626 | debug("Doing challenge reponse authentication."); |
a306f2dd | 627 | |
a306f2dd | 628 | for (i = 0; i < options.number_of_password_prompts; i++) { |
e3a70753 | 629 | /* request a challenge */ |
630 | packet_start(SSH_CMSG_AUTH_TIS); | |
631 | packet_send(); | |
632 | packet_write_wait(); | |
633 | ||
634 | type = packet_read(&payload_len); | |
635 | if (type != SSH_SMSG_FAILURE && | |
636 | type != SSH_SMSG_AUTH_TIS_CHALLENGE) { | |
637 | packet_disconnect("Protocol error: got %d in response " | |
d464095c | 638 | "to SSH_CMSG_AUTH_TIS", type); |
e3a70753 | 639 | } |
640 | if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) { | |
d464095c | 641 | debug("No challenge."); |
e3a70753 | 642 | return 0; |
643 | } | |
644 | challenge = packet_get_string(&clen); | |
645 | packet_integrity_check(payload_len, (4 + clen), type); | |
59c97189 | 646 | snprintf(prompt, sizeof prompt, "%s%s", challenge, |
647 | strchr(challenge, '\n') ? "" : "\nResponse: "); | |
e3a70753 | 648 | xfree(challenge); |
a306f2dd | 649 | if (i != 0) |
650 | error("Permission denied, please try again."); | |
e3a70753 | 651 | if (options.cipher == SSH_CIPHER_NONE) |
652 | log("WARNING: Encryption is disabled! " | |
653 | "Reponse will be transmitted in clear text."); | |
654 | response = read_passphrase(prompt, 0); | |
655 | if (strcmp(response, "") == 0) { | |
656 | xfree(response); | |
657 | break; | |
658 | } | |
a306f2dd | 659 | packet_start(SSH_CMSG_AUTH_TIS_RESPONSE); |
0ae4fe1d | 660 | ssh_put_password(response); |
a306f2dd | 661 | memset(response, 0, strlen(response)); |
662 | xfree(response); | |
663 | packet_send(); | |
664 | packet_write_wait(); | |
665 | type = packet_read(&payload_len); | |
666 | if (type == SSH_SMSG_SUCCESS) | |
667 | return 1; | |
668 | if (type != SSH_SMSG_FAILURE) | |
669 | packet_disconnect("Protocol error: got %d in response " | |
d464095c | 670 | "to SSH_CMSG_AUTH_TIS_RESPONSE", type); |
a306f2dd | 671 | } |
672 | /* failure */ | |
673 | return 0; | |
674 | } | |
675 | ||
676 | /* | |
677 | * Tries to authenticate with plain passwd authentication. | |
678 | */ | |
396c147e | 679 | static int |
a306f2dd | 680 | try_password_authentication(char *prompt) |
681 | { | |
682 | int type, i, payload_len; | |
683 | char *password; | |
684 | ||
685 | debug("Doing password authentication."); | |
686 | if (options.cipher == SSH_CIPHER_NONE) | |
687 | log("WARNING: Encryption is disabled! Password will be transmitted in clear text."); | |
688 | for (i = 0; i < options.number_of_password_prompts; i++) { | |
689 | if (i != 0) | |
690 | error("Permission denied, please try again."); | |
691 | password = read_passphrase(prompt, 0); | |
692 | packet_start(SSH_CMSG_AUTH_PASSWORD); | |
0ae4fe1d | 693 | ssh_put_password(password); |
a306f2dd | 694 | memset(password, 0, strlen(password)); |
695 | xfree(password); | |
696 | packet_send(); | |
697 | packet_write_wait(); | |
698 | ||
699 | type = packet_read(&payload_len); | |
700 | if (type == SSH_SMSG_SUCCESS) | |
701 | return 1; | |
702 | if (type != SSH_SMSG_FAILURE) | |
703 | packet_disconnect("Protocol error: got %d in response to passwd auth", type); | |
704 | } | |
705 | /* failure */ | |
706 | return 0; | |
707 | } | |
708 | ||
709 | /* | |
710 | * SSH1 key exchange | |
711 | */ | |
712 | void | |
713 | ssh_kex(char *host, struct sockaddr *hostaddr) | |
714 | { | |
715 | int i; | |
716 | BIGNUM *key; | |
717 | RSA *host_key; | |
718 | RSA *public_key; | |
719 | Key k; | |
720 | int bits, rbits; | |
721 | int ssh_cipher_default = SSH_CIPHER_3DES; | |
1e3b8b07 | 722 | u_char session_key[SSH_SESSION_KEY_LENGTH]; |
723 | u_char cookie[8]; | |
724 | u_int supported_ciphers; | |
725 | u_int server_flags, client_flags; | |
a306f2dd | 726 | int payload_len, clen, sum_len = 0; |
727 | u_int32_t rand = 0; | |
728 | ||
729 | debug("Waiting for server public key."); | |
730 | ||
731 | /* Wait for a public key packet from the server. */ | |
732 | packet_read_expect(&payload_len, SSH_SMSG_PUBLIC_KEY); | |
733 | ||
734 | /* Get cookie from the packet. */ | |
735 | for (i = 0; i < 8; i++) | |
736 | cookie[i] = packet_get_char(); | |
737 | ||
738 | /* Get the public key. */ | |
739 | public_key = RSA_new(); | |
740 | bits = packet_get_int();/* bits */ | |
741 | public_key->e = BN_new(); | |
742 | packet_get_bignum(public_key->e, &clen); | |
743 | sum_len += clen; | |
744 | public_key->n = BN_new(); | |
745 | packet_get_bignum(public_key->n, &clen); | |
746 | sum_len += clen; | |
747 | ||
748 | rbits = BN_num_bits(public_key->n); | |
749 | if (bits != rbits) { | |
750 | log("Warning: Server lies about size of server public key: " | |
751 | "actual size is %d bits vs. announced %d.", rbits, bits); | |
752 | log("Warning: This may be due to an old implementation of ssh."); | |
753 | } | |
754 | /* Get the host key. */ | |
755 | host_key = RSA_new(); | |
756 | bits = packet_get_int();/* bits */ | |
757 | host_key->e = BN_new(); | |
758 | packet_get_bignum(host_key->e, &clen); | |
759 | sum_len += clen; | |
760 | host_key->n = BN_new(); | |
761 | packet_get_bignum(host_key->n, &clen); | |
762 | sum_len += clen; | |
763 | ||
764 | rbits = BN_num_bits(host_key->n); | |
765 | if (bits != rbits) { | |
766 | log("Warning: Server lies about size of server host key: " | |
767 | "actual size is %d bits vs. announced %d.", rbits, bits); | |
768 | log("Warning: This may be due to an old implementation of ssh."); | |
769 | } | |
770 | ||
771 | /* Get protocol flags. */ | |
772 | server_flags = packet_get_int(); | |
773 | packet_set_protocol_flags(server_flags); | |
774 | ||
775 | supported_ciphers = packet_get_int(); | |
776 | supported_authentications = packet_get_int(); | |
777 | ||
778 | debug("Received server public key (%d bits) and host key (%d bits).", | |
779 | BN_num_bits(public_key->n), BN_num_bits(host_key->n)); | |
780 | ||
781 | packet_integrity_check(payload_len, | |
782 | 8 + 4 + sum_len + 0 + 4 + 0 + 0 + 4 + 4 + 4, | |
783 | SSH_SMSG_PUBLIC_KEY); | |
fa08c86b | 784 | k.type = KEY_RSA1; |
a306f2dd | 785 | k.rsa = host_key; |
f49bc4f7 | 786 | if (verify_host_key(host, hostaddr, &k) == -1) |
787 | fatal("host_key verification failed"); | |
a306f2dd | 788 | |
789 | client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN; | |
790 | ||
791 | compute_session_id(session_id, cookie, host_key->n, public_key->n); | |
792 | ||
793 | /* Generate a session key. */ | |
794 | arc4random_stir(); | |
795 | ||
796 | /* | |
797 | * Generate an encryption key for the session. The key is a 256 bit | |
798 | * random number, interpreted as a 32-byte key, with the least | |
799 | * significant 8 bits being the first byte of the key. | |
800 | */ | |
801 | for (i = 0; i < 32; i++) { | |
802 | if (i % 4 == 0) | |
803 | rand = arc4random(); | |
804 | session_key[i] = rand & 0xff; | |
805 | rand >>= 8; | |
806 | } | |
807 | ||
808 | /* | |
809 | * According to the protocol spec, the first byte of the session key | |
810 | * is the highest byte of the integer. The session key is xored with | |
811 | * the first 16 bytes of the session id. | |
812 | */ | |
813 | key = BN_new(); | |
814 | BN_set_word(key, 0); | |
815 | for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) { | |
816 | BN_lshift(key, key, 8); | |
817 | if (i < 16) | |
818 | BN_add_word(key, session_key[i] ^ session_id[i]); | |
819 | else | |
820 | BN_add_word(key, session_key[i]); | |
821 | } | |
822 | ||
823 | /* | |
824 | * Encrypt the integer using the public key and host key of the | |
825 | * server (key with smaller modulus first). | |
826 | */ | |
827 | if (BN_cmp(public_key->n, host_key->n) < 0) { | |
828 | /* Public key has smaller modulus. */ | |
829 | if (BN_num_bits(host_key->n) < | |
830 | BN_num_bits(public_key->n) + SSH_KEY_BITS_RESERVED) { | |
831 | fatal("respond_to_rsa_challenge: host_key %d < public_key %d + " | |
832 | "SSH_KEY_BITS_RESERVED %d", | |
833 | BN_num_bits(host_key->n), | |
834 | BN_num_bits(public_key->n), | |
835 | SSH_KEY_BITS_RESERVED); | |
836 | } | |
837 | rsa_public_encrypt(key, key, public_key); | |
838 | rsa_public_encrypt(key, key, host_key); | |
839 | } else { | |
840 | /* Host key has smaller modulus (or they are equal). */ | |
841 | if (BN_num_bits(public_key->n) < | |
842 | BN_num_bits(host_key->n) + SSH_KEY_BITS_RESERVED) { | |
843 | fatal("respond_to_rsa_challenge: public_key %d < host_key %d + " | |
844 | "SSH_KEY_BITS_RESERVED %d", | |
845 | BN_num_bits(public_key->n), | |
846 | BN_num_bits(host_key->n), | |
847 | SSH_KEY_BITS_RESERVED); | |
848 | } | |
849 | rsa_public_encrypt(key, key, host_key); | |
850 | rsa_public_encrypt(key, key, public_key); | |
851 | } | |
852 | ||
853 | /* Destroy the public keys since we no longer need them. */ | |
854 | RSA_free(public_key); | |
855 | RSA_free(host_key); | |
856 | ||
c523303b | 857 | if (options.cipher == SSH_CIPHER_NOT_SET) { |
858 | if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default)) | |
859 | options.cipher = ssh_cipher_default; | |
860 | } else if (options.cipher == SSH_CIPHER_ILLEGAL || | |
861 | !(cipher_mask_ssh1(1) & (1 << options.cipher))) { | |
d0c832f3 | 862 | log("No valid SSH1 cipher, using %.100s instead.", |
94ec8c6b | 863 | cipher_name(ssh_cipher_default)); |
864 | options.cipher = ssh_cipher_default; | |
a306f2dd | 865 | } |
866 | /* Check that the selected cipher is supported. */ | |
867 | if (!(supported_ciphers & (1 << options.cipher))) | |
868 | fatal("Selected cipher type %.100s not supported by server.", | |
869 | cipher_name(options.cipher)); | |
870 | ||
871 | debug("Encryption type: %.100s", cipher_name(options.cipher)); | |
872 | ||
873 | /* Send the encrypted session key to the server. */ | |
874 | packet_start(SSH_CMSG_SESSION_KEY); | |
875 | packet_put_char(options.cipher); | |
876 | ||
877 | /* Send the cookie back to the server. */ | |
878 | for (i = 0; i < 8; i++) | |
879 | packet_put_char(cookie[i]); | |
880 | ||
881 | /* Send and destroy the encrypted encryption key integer. */ | |
882 | packet_put_bignum(key); | |
883 | BN_clear_free(key); | |
884 | ||
885 | /* Send protocol flags. */ | |
886 | packet_put_int(client_flags); | |
887 | ||
888 | /* Send the packet now. */ | |
889 | packet_send(); | |
890 | packet_write_wait(); | |
891 | ||
892 | debug("Sent encrypted session key."); | |
893 | ||
894 | /* Set the encryption key. */ | |
895 | packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher); | |
896 | ||
897 | /* We will no longer need the session key here. Destroy any extra copies. */ | |
898 | memset(session_key, 0, sizeof(session_key)); | |
899 | ||
900 | /* | |
901 | * Expect a success message from the server. Note that this message | |
902 | * will be received in encrypted form. | |
903 | */ | |
904 | packet_read_expect(&payload_len, SSH_SMSG_SUCCESS); | |
905 | ||
906 | debug("Received encrypted confirmation."); | |
907 | } | |
908 | ||
909 | /* | |
910 | * Authenticate user | |
911 | */ | |
912 | void | |
8002af61 | 913 | ssh_userauth1(const char *local_user, const char *server_user, char *host, |
914 | Key **keys, int nkeys) | |
a306f2dd | 915 | { |
916 | int i, type; | |
917 | int payload_len; | |
918 | ||
919 | if (supported_authentications == 0) | |
8002af61 | 920 | fatal("ssh_userauth1: server supports no auth methods"); |
a306f2dd | 921 | |
922 | /* Send the name of the user to log in as on the server. */ | |
923 | packet_start(SSH_CMSG_USER); | |
449c5ba5 | 924 | packet_put_cstring(server_user); |
a306f2dd | 925 | packet_send(); |
926 | packet_write_wait(); | |
927 | ||
928 | /* | |
929 | * The server should respond with success if no authentication is | |
930 | * needed (the user has no password). Otherwise the server responds | |
931 | * with failure. | |
932 | */ | |
933 | type = packet_read(&payload_len); | |
934 | ||
935 | /* check whether the connection was accepted without authentication. */ | |
936 | if (type == SSH_SMSG_SUCCESS) | |
937 | return; | |
938 | if (type != SSH_SMSG_FAILURE) | |
939 | packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", | |
940 | type); | |
941 | ||
942 | #ifdef AFS | |
943 | /* Try Kerberos tgt passing if the server supports it. */ | |
944 | if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) && | |
945 | options.kerberos_tgt_passing) { | |
946 | if (options.cipher == SSH_CIPHER_NONE) | |
947 | log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!"); | |
948 | (void) send_kerberos_tgt(); | |
949 | } | |
950 | /* Try AFS token passing if the server supports it. */ | |
951 | if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) && | |
952 | options.afs_token_passing && k_hasafs()) { | |
953 | if (options.cipher == SSH_CIPHER_NONE) | |
954 | log("WARNING: Encryption is disabled! Token will be transmitted in the clear!"); | |
955 | send_afs_tokens(); | |
956 | } | |
957 | #endif /* AFS */ | |
958 | ||
959 | #ifdef KRB4 | |
960 | if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) && | |
961 | options.kerberos_authentication) { | |
962 | debug("Trying Kerberos authentication."); | |
963 | if (try_kerberos_authentication()) { | |
964 | /* The server should respond with success or failure. */ | |
965 | type = packet_read(&payload_len); | |
966 | if (type == SSH_SMSG_SUCCESS) | |
967 | return; | |
968 | if (type != SSH_SMSG_FAILURE) | |
969 | packet_disconnect("Protocol error: got %d in response to Kerberos auth", type); | |
970 | } | |
971 | } | |
972 | #endif /* KRB4 */ | |
973 | ||
974 | /* | |
975 | * Use rhosts authentication if running in privileged socket and we | |
976 | * do not wish to remain anonymous. | |
977 | */ | |
978 | if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) && | |
979 | options.rhosts_authentication) { | |
980 | debug("Trying rhosts authentication."); | |
981 | packet_start(SSH_CMSG_AUTH_RHOSTS); | |
449c5ba5 | 982 | packet_put_cstring(local_user); |
a306f2dd | 983 | packet_send(); |
984 | packet_write_wait(); | |
985 | ||
986 | /* The server should respond with success or failure. */ | |
987 | type = packet_read(&payload_len); | |
988 | if (type == SSH_SMSG_SUCCESS) | |
989 | return; | |
990 | if (type != SSH_SMSG_FAILURE) | |
991 | packet_disconnect("Protocol error: got %d in response to rhosts auth", | |
992 | type); | |
993 | } | |
994 | /* | |
995 | * Try .rhosts or /etc/hosts.equiv authentication with RSA host | |
996 | * authentication. | |
997 | */ | |
998 | if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) && | |
8002af61 | 999 | options.rhosts_rsa_authentication) { |
1000 | for (i = 0; i < nkeys; i++) { | |
eba4bef7 | 1001 | if (keys[i] != NULL && keys[i]->type == KEY_RSA1 && |
8002af61 | 1002 | try_rhosts_rsa_authentication(local_user, keys[i])) |
1003 | return; | |
1004 | } | |
a306f2dd | 1005 | } |
1006 | /* Try RSA authentication if the server supports it. */ | |
1007 | if ((supported_authentications & (1 << SSH_AUTH_RSA)) && | |
1008 | options.rsa_authentication) { | |
1009 | /* | |
1010 | * Try RSA authentication using the authentication agent. The | |
1011 | * agent is tried first because no passphrase is needed for | |
1012 | * it, whereas identity files may require passphrases. | |
1013 | */ | |
1014 | if (try_agent_authentication()) | |
1015 | return; | |
1016 | ||
1017 | /* Try RSA authentication for each identity. */ | |
1018 | for (i = 0; i < options.num_identity_files; i++) | |
fee56204 | 1019 | if (options.identity_keys[i] != NULL && |
1020 | options.identity_keys[i]->type == KEY_RSA1 && | |
fa08c86b | 1021 | try_rsa_authentication(options.identity_files[i])) |
a306f2dd | 1022 | return; |
1023 | } | |
d464095c | 1024 | /* Try challenge response authentication if the server supports it. */ |
a306f2dd | 1025 | if ((supported_authentications & (1 << SSH_AUTH_TIS)) && |
5ba55ada | 1026 | options.challenge_response_authentication && !options.batch_mode) { |
1027 | if (try_challenge_response_authentication()) | |
a306f2dd | 1028 | return; |
1029 | } | |
1030 | /* Try password authentication if the server supports it. */ | |
1031 | if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) && | |
1032 | options.password_authentication && !options.batch_mode) { | |
1033 | char prompt[80]; | |
1034 | ||
b3b9ad8e | 1035 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", |
a306f2dd | 1036 | server_user, host); |
1037 | if (try_password_authentication(prompt)) | |
1038 | return; | |
1039 | } | |
1040 | /* All authentication methods have failed. Exit with an error message. */ | |
1041 | fatal("Permission denied."); | |
1042 | /* NOTREACHED */ | |
1043 | } |