]>
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" | |
20b279e6 | 16 | RCSID("$OpenBSD: sshconnect1.c,v 1.46 2001/12/28 13:57:33 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 |
ced49be2 | 24 | #ifdef KRB5 |
25 | #include <krb5.h> | |
26 | #endif | |
42f11eb2 | 27 | #ifdef AFS |
28 | #include <kafs.h> | |
53a24016 | 29 | #include "radix.h" |
42f11eb2 | 30 | #endif |
31 | ||
32 | #include "ssh.h" | |
33 | #include "ssh1.h" | |
a306f2dd | 34 | #include "xmalloc.h" |
35 | #include "rsa.h" | |
a306f2dd | 36 | #include "buffer.h" |
37 | #include "packet.h" | |
a306f2dd | 38 | #include "mpaux.h" |
39 | #include "uidswap.h" | |
42f11eb2 | 40 | #include "log.h" |
a306f2dd | 41 | #include "readconf.h" |
42 | #include "key.h" | |
4c8722d9 | 43 | #include "authfd.h" |
a306f2dd | 44 | #include "sshconnect.h" |
45 | #include "authfile.h" | |
42f11eb2 | 46 | #include "readpass.h" |
47 | #include "cipher.h" | |
48 | #include "canohost.h" | |
ced49be2 | 49 | #include "auth.h" |
a306f2dd | 50 | |
51 | /* Session id for the current session. */ | |
1e3b8b07 | 52 | u_char session_id[16]; |
53 | u_int supported_authentications = 0; | |
a306f2dd | 54 | |
55 | extern Options options; | |
56 | extern char *__progname; | |
57 | ||
58 | /* | |
59 | * Checks if the user has an authentication agent, and if so, tries to | |
60 | * authenticate using the agent. | |
61 | */ | |
396c147e | 62 | static int |
5ca51e19 | 63 | try_agent_authentication(void) |
a306f2dd | 64 | { |
2e73a022 | 65 | int type; |
a306f2dd | 66 | char *comment; |
67 | AuthenticationConnection *auth; | |
1e3b8b07 | 68 | u_char response[16]; |
69 | u_int i; | |
20b279e6 | 70 | int plen; |
2e73a022 | 71 | Key *key; |
72 | BIGNUM *challenge; | |
a306f2dd | 73 | |
74 | /* Get connection to the agent. */ | |
75 | auth = ssh_get_authentication_connection(); | |
76 | if (!auth) | |
77 | return 0; | |
78 | ||
b775c6f2 | 79 | if ((challenge = BN_new()) == NULL) |
80 | fatal("try_agent_authentication: BN_new failed"); | |
a306f2dd | 81 | /* Loop through identities served by the agent. */ |
2e73a022 | 82 | for (key = ssh_get_first_identity(auth, &comment, 1); |
184eed6a | 83 | key != NULL; |
84 | key = ssh_get_next_identity(auth, &comment, 1)) { | |
a306f2dd | 85 | |
86 | /* Try this identity. */ | |
87 | debug("Trying RSA authentication via agent with '%.100s'", comment); | |
88 | xfree(comment); | |
89 | ||
90 | /* Tell the server that we are willing to authenticate using this key. */ | |
91 | packet_start(SSH_CMSG_AUTH_RSA); | |
2e73a022 | 92 | packet_put_bignum(key->rsa->n); |
a306f2dd | 93 | packet_send(); |
94 | packet_write_wait(); | |
95 | ||
96 | /* Wait for server's response. */ | |
97 | type = packet_read(&plen); | |
98 | ||
99 | /* The server sends failure if it doesn\'t like our key or | |
100 | does not support RSA authentication. */ | |
101 | if (type == SSH_SMSG_FAILURE) { | |
102 | debug("Server refused our key."); | |
2e73a022 | 103 | key_free(key); |
a306f2dd | 104 | continue; |
105 | } | |
106 | /* Otherwise it should have sent a challenge. */ | |
107 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) | |
108 | packet_disconnect("Protocol error during RSA authentication: %d", | |
109 | type); | |
110 | ||
20b279e6 | 111 | packet_get_bignum(challenge); |
95500969 | 112 | packet_check_eom(); |
a306f2dd | 113 | |
114 | debug("Received RSA challenge from server."); | |
115 | ||
116 | /* Ask the agent to decrypt the challenge. */ | |
2e73a022 | 117 | if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) { |
118 | /* | |
119 | * The agent failed to authenticate this identifier | |
120 | * although it advertised it supports this. Just | |
121 | * return a wrong value. | |
122 | */ | |
a306f2dd | 123 | log("Authentication agent failed to decrypt challenge."); |
124 | memset(response, 0, sizeof(response)); | |
125 | } | |
2e73a022 | 126 | key_free(key); |
a306f2dd | 127 | debug("Sending response to RSA challenge."); |
128 | ||
129 | /* Send the decrypted challenge back to the server. */ | |
130 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); | |
131 | for (i = 0; i < 16; i++) | |
132 | packet_put_char(response[i]); | |
133 | packet_send(); | |
134 | packet_write_wait(); | |
135 | ||
136 | /* Wait for response from the server. */ | |
137 | type = packet_read(&plen); | |
138 | ||
139 | /* The server returns success if it accepted the authentication. */ | |
140 | if (type == SSH_SMSG_SUCCESS) { | |
eea39c02 | 141 | ssh_close_authentication_connection(auth); |
a306f2dd | 142 | BN_clear_free(challenge); |
2e73a022 | 143 | debug("RSA authentication accepted by server."); |
a306f2dd | 144 | return 1; |
145 | } | |
146 | /* Otherwise it should return failure. */ | |
147 | if (type != SSH_SMSG_FAILURE) | |
148 | packet_disconnect("Protocol error waiting RSA auth response: %d", | |
149 | type); | |
150 | } | |
eea39c02 | 151 | ssh_close_authentication_connection(auth); |
a306f2dd | 152 | BN_clear_free(challenge); |
a306f2dd | 153 | debug("RSA authentication using agent refused."); |
154 | return 0; | |
155 | } | |
156 | ||
157 | /* | |
158 | * Computes the proper response to a RSA challenge, and sends the response to | |
159 | * the server. | |
160 | */ | |
396c147e | 161 | static void |
a306f2dd | 162 | respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv) |
163 | { | |
1e3b8b07 | 164 | u_char buf[32], response[16]; |
a306f2dd | 165 | MD5_CTX md; |
166 | int i, len; | |
167 | ||
168 | /* Decrypt the challenge using the private key. */ | |
46aa2d1f | 169 | /* XXX think about Bleichenbacher, too */ |
170 | if (rsa_private_decrypt(challenge, challenge, prv) <= 0) | |
171 | packet_disconnect( | |
172 | "respond_to_rsa_challenge: rsa_private_decrypt failed"); | |
a306f2dd | 173 | |
174 | /* Compute the response. */ | |
175 | /* The response is MD5 of decrypted challenge plus session id. */ | |
176 | len = BN_num_bytes(challenge); | |
177 | if (len <= 0 || len > sizeof(buf)) | |
46aa2d1f | 178 | packet_disconnect( |
179 | "respond_to_rsa_challenge: bad challenge length %d", len); | |
a306f2dd | 180 | |
181 | memset(buf, 0, sizeof(buf)); | |
182 | BN_bn2bin(challenge, buf + sizeof(buf) - len); | |
183 | MD5_Init(&md); | |
184 | MD5_Update(&md, buf, 32); | |
185 | MD5_Update(&md, session_id, 16); | |
186 | MD5_Final(response, &md); | |
187 | ||
188 | debug("Sending response to host key RSA challenge."); | |
189 | ||
190 | /* Send the response back to the server. */ | |
191 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); | |
192 | for (i = 0; i < 16; i++) | |
193 | packet_put_char(response[i]); | |
194 | packet_send(); | |
195 | packet_write_wait(); | |
196 | ||
197 | memset(buf, 0, sizeof(buf)); | |
198 | memset(response, 0, sizeof(response)); | |
199 | memset(&md, 0, sizeof(md)); | |
200 | } | |
201 | ||
202 | /* | |
203 | * Checks if the user has authentication file, and if so, tries to authenticate | |
204 | * the user using it. | |
205 | */ | |
396c147e | 206 | static int |
383546c4 | 207 | try_rsa_authentication(int idx) |
a306f2dd | 208 | { |
209 | BIGNUM *challenge; | |
ce404659 | 210 | Key *public, *private; |
383546c4 | 211 | char buf[300], *passphrase, *comment, *authfile; |
20b279e6 | 212 | int i, type, quit, plen; |
a306f2dd | 213 | |
383546c4 | 214 | public = options.identity_keys[idx]; |
215 | authfile = options.identity_files[idx]; | |
216 | comment = xstrdup(authfile); | |
217 | ||
a306f2dd | 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 | ||
a306f2dd | 226 | /* Wait for server's response. */ |
227 | type = packet_read(&plen); | |
228 | ||
229 | /* | |
230 | * The server responds with failure if it doesn\'t like our key or | |
231 | * doesn\'t support RSA authentication. | |
232 | */ | |
233 | if (type == SSH_SMSG_FAILURE) { | |
234 | debug("Server refused our key."); | |
235 | xfree(comment); | |
236 | return 0; | |
237 | } | |
238 | /* Otherwise, the server should respond with a challenge. */ | |
239 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) | |
240 | packet_disconnect("Protocol error during RSA authentication: %d", type); | |
241 | ||
242 | /* Get the challenge from the packet. */ | |
b775c6f2 | 243 | if ((challenge = BN_new()) == NULL) |
244 | fatal("try_rsa_authentication: BN_new failed"); | |
20b279e6 | 245 | packet_get_bignum(challenge); |
95500969 | 246 | packet_check_eom(); |
a306f2dd | 247 | |
248 | debug("Received RSA challenge from server."); | |
249 | ||
a306f2dd | 250 | /* |
383546c4 | 251 | * If the key is not stored in external hardware, we have to |
252 | * load the private key. Try first with empty passphrase; if it | |
a306f2dd | 253 | * fails, ask for a passphrase. |
254 | */ | |
383546c4 | 255 | if (public->flags && KEY_FLAG_EXT) |
256 | private = public; | |
257 | else | |
258 | private = key_load_private_type(KEY_RSA1, authfile, "", NULL); | |
ce404659 | 259 | if (private == NULL && !options.batch_mode) { |
260 | snprintf(buf, sizeof(buf), | |
261 | "Enter passphrase for RSA key '%.100s': ", comment); | |
262 | for (i = 0; i < options.number_of_password_prompts; i++) { | |
a306f2dd | 263 | passphrase = read_passphrase(buf, 0); |
ce404659 | 264 | if (strcmp(passphrase, "") != 0) { |
265 | private = key_load_private_type(KEY_RSA1, | |
266 | authfile, passphrase, NULL); | |
267 | quit = 0; | |
268 | } else { | |
269 | debug2("no passphrase given, try next key"); | |
270 | quit = 1; | |
271 | } | |
a306f2dd | 272 | memset(passphrase, 0, strlen(passphrase)); |
273 | xfree(passphrase); | |
ce404659 | 274 | if (private != NULL || quit) |
275 | break; | |
276 | debug2("bad passphrase given, try again..."); | |
a306f2dd | 277 | } |
a306f2dd | 278 | } |
279 | /* We no longer need the comment. */ | |
280 | xfree(comment); | |
281 | ||
ce404659 | 282 | if (private == NULL) { |
283 | if (!options.batch_mode) | |
284 | error("Bad passphrase."); | |
285 | ||
286 | /* Send a dummy response packet to avoid protocol error. */ | |
287 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); | |
288 | for (i = 0; i < 16; i++) | |
289 | packet_put_char(0); | |
290 | packet_send(); | |
291 | packet_write_wait(); | |
292 | ||
293 | /* Expect the server to reject it... */ | |
294 | packet_read_expect(&plen, SSH_SMSG_FAILURE); | |
295 | BN_clear_free(challenge); | |
296 | return 0; | |
297 | } | |
298 | ||
a306f2dd | 299 | /* Compute and send a response to the challenge. */ |
300 | respond_to_rsa_challenge(challenge, private->rsa); | |
301 | ||
383546c4 | 302 | /* Destroy the private key unless it in external hardware. */ |
303 | if (!(private->flags & KEY_FLAG_EXT)) | |
304 | key_free(private); | |
a306f2dd | 305 | |
306 | /* We no longer need the challenge. */ | |
307 | BN_clear_free(challenge); | |
308 | ||
309 | /* Wait for response from the server. */ | |
310 | type = packet_read(&plen); | |
311 | if (type == SSH_SMSG_SUCCESS) { | |
312 | debug("RSA authentication accepted by server."); | |
313 | return 1; | |
314 | } | |
315 | if (type != SSH_SMSG_FAILURE) | |
316 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); | |
317 | debug("RSA authentication refused."); | |
318 | return 0; | |
319 | } | |
320 | ||
321 | /* | |
322 | * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv | |
323 | * authentication and RSA host authentication. | |
324 | */ | |
396c147e | 325 | static int |
aeaa3d9e | 326 | try_rhosts_rsa_authentication(const char *local_user, Key * host_key) |
a306f2dd | 327 | { |
328 | int type; | |
329 | BIGNUM *challenge; | |
20b279e6 | 330 | int plen; |
a306f2dd | 331 | |
332 | debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication."); | |
333 | ||
334 | /* Tell the server that we are willing to authenticate using this key. */ | |
335 | packet_start(SSH_CMSG_AUTH_RHOSTS_RSA); | |
449c5ba5 | 336 | packet_put_cstring(local_user); |
aeaa3d9e | 337 | packet_put_int(BN_num_bits(host_key->rsa->n)); |
338 | packet_put_bignum(host_key->rsa->e); | |
339 | packet_put_bignum(host_key->rsa->n); | |
a306f2dd | 340 | packet_send(); |
341 | packet_write_wait(); | |
342 | ||
343 | /* Wait for server's response. */ | |
344 | type = packet_read(&plen); | |
345 | ||
346 | /* The server responds with failure if it doesn't admit our | |
347 | .rhosts authentication or doesn't know our host key. */ | |
348 | if (type == SSH_SMSG_FAILURE) { | |
349 | debug("Server refused our rhosts authentication or host key."); | |
350 | return 0; | |
351 | } | |
352 | /* Otherwise, the server should respond with a challenge. */ | |
353 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) | |
354 | packet_disconnect("Protocol error during RSA authentication: %d", type); | |
355 | ||
356 | /* Get the challenge from the packet. */ | |
b775c6f2 | 357 | if ((challenge = BN_new()) == NULL) |
358 | fatal("try_rhosts_rsa_authentication: BN_new failed"); | |
20b279e6 | 359 | packet_get_bignum(challenge); |
95500969 | 360 | packet_check_eom(); |
a306f2dd | 361 | |
362 | debug("Received RSA challenge for host key from server."); | |
363 | ||
364 | /* Compute a response to the challenge. */ | |
aeaa3d9e | 365 | respond_to_rsa_challenge(challenge, host_key->rsa); |
a306f2dd | 366 | |
367 | /* We no longer need the challenge. */ | |
368 | BN_clear_free(challenge); | |
369 | ||
370 | /* Wait for response from the server. */ | |
371 | type = packet_read(&plen); | |
372 | if (type == SSH_SMSG_SUCCESS) { | |
373 | debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server."); | |
374 | return 1; | |
375 | } | |
376 | if (type != SSH_SMSG_FAILURE) | |
377 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); | |
378 | debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused."); | |
379 | return 0; | |
380 | } | |
381 | ||
382 | #ifdef KRB4 | |
396c147e | 383 | static int |
ced49be2 | 384 | try_krb4_authentication(void) |
a306f2dd | 385 | { |
386 | KTEXT_ST auth; /* Kerberos data */ | |
387 | char *reply; | |
388 | char inst[INST_SZ]; | |
389 | char *realm; | |
390 | CREDENTIALS cred; | |
391 | int r, type, plen; | |
392 | socklen_t slen; | |
393 | Key_schedule schedule; | |
394 | u_long checksum, cksum; | |
395 | MSG_DAT msg_data; | |
396 | struct sockaddr_in local, foreign; | |
397 | struct stat st; | |
398 | ||
399 | /* Don't do anything if we don't have any tickets. */ | |
400 | if (stat(tkt_string(), &st) < 0) | |
401 | return 0; | |
184eed6a | 402 | |
ced49be2 | 403 | strlcpy(inst, (char *)krb_get_phost(get_canonical_hostname(1)), |
404 | INST_SZ); | |
184eed6a | 405 | |
ced49be2 | 406 | realm = (char *)krb_realmofhost(get_canonical_hostname(1)); |
a306f2dd | 407 | if (!realm) { |
ced49be2 | 408 | debug("Kerberos v4: no realm for %s", get_canonical_hostname(1)); |
a306f2dd | 409 | return 0; |
410 | } | |
411 | /* This can really be anything. */ | |
ced49be2 | 412 | checksum = (u_long)getpid(); |
184eed6a | 413 | |
a306f2dd | 414 | r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum); |
415 | if (r != KSUCCESS) { | |
ced49be2 | 416 | debug("Kerberos v4 krb_mk_req failed: %s", krb_err_txt[r]); |
a306f2dd | 417 | return 0; |
418 | } | |
419 | /* Get session key to decrypt the server's reply with. */ | |
420 | r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred); | |
421 | if (r != KSUCCESS) { | |
422 | debug("get_cred failed: %s", krb_err_txt[r]); | |
423 | return 0; | |
424 | } | |
425 | des_key_sched((des_cblock *) cred.session, schedule); | |
184eed6a | 426 | |
a306f2dd | 427 | /* Send authentication info to server. */ |
428 | packet_start(SSH_CMSG_AUTH_KERBEROS); | |
429 | packet_put_string((char *) auth.dat, auth.length); | |
430 | packet_send(); | |
431 | packet_write_wait(); | |
184eed6a | 432 | |
a306f2dd | 433 | /* Zero the buffer. */ |
434 | (void) memset(auth.dat, 0, MAX_KTXT_LEN); | |
184eed6a | 435 | |
a306f2dd | 436 | slen = sizeof(local); |
437 | memset(&local, 0, sizeof(local)); | |
438 | if (getsockname(packet_get_connection_in(), | |
ced49be2 | 439 | (struct sockaddr *)&local, &slen) < 0) |
a306f2dd | 440 | debug("getsockname failed: %s", strerror(errno)); |
184eed6a | 441 | |
a306f2dd | 442 | slen = sizeof(foreign); |
443 | memset(&foreign, 0, sizeof(foreign)); | |
444 | if (getpeername(packet_get_connection_in(), | |
ced49be2 | 445 | (struct sockaddr *)&foreign, &slen) < 0) { |
a306f2dd | 446 | debug("getpeername failed: %s", strerror(errno)); |
447 | fatal_cleanup(); | |
448 | } | |
449 | /* Get server reply. */ | |
450 | type = packet_read(&plen); | |
451 | switch (type) { | |
452 | case SSH_SMSG_FAILURE: | |
453 | /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */ | |
ced49be2 | 454 | debug("Kerberos v4 authentication failed."); |
a306f2dd | 455 | return 0; |
456 | break; | |
184eed6a | 457 | |
a306f2dd | 458 | case SSH_SMSG_AUTH_KERBEROS_RESPONSE: |
459 | /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */ | |
ced49be2 | 460 | debug("Kerberos v4 authentication accepted."); |
184eed6a | 461 | |
a306f2dd | 462 | /* Get server's response. */ |
1e3b8b07 | 463 | reply = packet_get_string((u_int *) &auth.length); |
a306f2dd | 464 | memcpy(auth.dat, reply, auth.length); |
465 | xfree(reply); | |
184eed6a | 466 | |
95500969 | 467 | packet_check_eom(); |
184eed6a | 468 | |
a306f2dd | 469 | /* |
470 | * If his response isn't properly encrypted with the session | |
471 | * key, and the decrypted checksum fails to match, he's | |
472 | * bogus. Bail out. | |
473 | */ | |
474 | r = krb_rd_priv(auth.dat, auth.length, schedule, &cred.session, | |
ced49be2 | 475 | &foreign, &local, &msg_data); |
a306f2dd | 476 | if (r != KSUCCESS) { |
ced49be2 | 477 | debug("Kerberos v4 krb_rd_priv failed: %s", |
478 | krb_err_txt[r]); | |
479 | packet_disconnect("Kerberos v4 challenge failed!"); | |
a306f2dd | 480 | } |
481 | /* Fetch the (incremented) checksum that we supplied in the request. */ | |
ced49be2 | 482 | memcpy((char *)&cksum, (char *)msg_data.app_data, |
483 | sizeof(cksum)); | |
a306f2dd | 484 | cksum = ntohl(cksum); |
184eed6a | 485 | |
a306f2dd | 486 | /* If it matches, we're golden. */ |
487 | if (cksum == checksum + 1) { | |
ced49be2 | 488 | debug("Kerberos v4 challenge successful."); |
a306f2dd | 489 | return 1; |
490 | } else | |
ced49be2 | 491 | packet_disconnect("Kerberos v4 challenge failed!"); |
a306f2dd | 492 | break; |
184eed6a | 493 | |
a306f2dd | 494 | default: |
ced49be2 | 495 | packet_disconnect("Protocol error on Kerberos v4 response: %d", type); |
a306f2dd | 496 | } |
497 | return 0; | |
498 | } | |
499 | ||
500 | #endif /* KRB4 */ | |
501 | ||
ced49be2 | 502 | #ifdef KRB5 |
396c147e | 503 | static int |
ced49be2 | 504 | try_krb5_authentication(krb5_context *context, krb5_auth_context *auth_context) |
505 | { | |
506 | krb5_error_code problem; | |
507 | const char *tkfile; | |
508 | struct stat buf; | |
509 | krb5_ccache ccache = NULL; | |
510 | const char *remotehost; | |
511 | krb5_data ap; | |
512 | int type, payload_len; | |
513 | krb5_ap_rep_enc_part *reply = NULL; | |
514 | int ret; | |
184eed6a | 515 | |
ced49be2 | 516 | memset(&ap, 0, sizeof(ap)); |
184eed6a | 517 | |
ced49be2 | 518 | problem = krb5_init_context(context); |
519 | if (problem) { | |
520 | debug("Kerberos v5: krb5_init_context failed"); | |
521 | ret = 0; | |
522 | goto out; | |
523 | } | |
184eed6a | 524 | |
ced49be2 | 525 | tkfile = krb5_cc_default_name(*context); |
526 | if (strncmp(tkfile, "FILE:", 5) == 0) | |
527 | tkfile += 5; | |
184eed6a | 528 | |
ced49be2 | 529 | if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) { |
530 | debug("Kerberos v5: could not get default ccache (permission denied)."); | |
531 | ret = 0; | |
532 | goto out; | |
533 | } | |
184eed6a | 534 | |
ced49be2 | 535 | problem = krb5_cc_default(*context, &ccache); |
536 | if (problem) { | |
537 | debug("Kerberos v5: krb5_cc_default failed: %s", | |
538 | krb5_get_err_text(*context, problem)); | |
539 | ret = 0; | |
540 | goto out; | |
541 | } | |
184eed6a | 542 | |
ced49be2 | 543 | remotehost = get_canonical_hostname(1); |
184eed6a | 544 | |
ced49be2 | 545 | problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED, |
546 | "host", remotehost, NULL, ccache, &ap); | |
547 | if (problem) { | |
548 | debug("Kerberos v5: krb5_mk_req failed: %s", | |
549 | krb5_get_err_text(*context, problem)); | |
550 | ret = 0; | |
551 | goto out; | |
552 | } | |
184eed6a | 553 | |
ced49be2 | 554 | packet_start(SSH_CMSG_AUTH_KERBEROS); |
555 | packet_put_string((char *) ap.data, ap.length); | |
556 | packet_send(); | |
557 | packet_write_wait(); | |
184eed6a | 558 | |
ced49be2 | 559 | xfree(ap.data); |
560 | ap.length = 0; | |
184eed6a | 561 | |
ced49be2 | 562 | type = packet_read(&payload_len); |
563 | switch (type) { | |
184eed6a | 564 | case SSH_SMSG_FAILURE: |
565 | /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */ | |
566 | debug("Kerberos v5 authentication failed."); | |
567 | ret = 0; | |
568 | break; | |
569 | ||
ced49be2 | 570 | case SSH_SMSG_AUTH_KERBEROS_RESPONSE: |
184eed6a | 571 | /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */ |
572 | debug("Kerberos v5 authentication accepted."); | |
573 | ||
574 | /* Get server's response. */ | |
575 | ap.data = packet_get_string((unsigned int *) &ap.length); | |
95500969 | 576 | packet_check_eom(); |
184eed6a | 577 | /* XXX je to dobre? */ |
578 | ||
579 | problem = krb5_rd_rep(*context, *auth_context, &ap, &reply); | |
580 | if (problem) { | |
ced49be2 | 581 | ret = 0; |
582 | } | |
583 | ret = 1; | |
584 | break; | |
184eed6a | 585 | |
ced49be2 | 586 | default: |
587 | packet_disconnect("Protocol error on Kerberos v5 response: %d", | |
588 | type); | |
589 | ret = 0; | |
590 | break; | |
184eed6a | 591 | |
ced49be2 | 592 | } |
184eed6a | 593 | |
ced49be2 | 594 | out: |
595 | if (ccache != NULL) | |
596 | krb5_cc_close(*context, ccache); | |
597 | if (reply != NULL) | |
598 | krb5_free_ap_rep_enc_part(*context, reply); | |
599 | if (ap.length > 0) | |
600 | krb5_data_free(&ap); | |
184eed6a | 601 | |
ced49be2 | 602 | return (ret); |
603 | } | |
604 | ||
605 | static void | |
606 | send_krb5_tgt(krb5_context context, krb5_auth_context auth_context) | |
607 | { | |
608 | int fd, type, payload_len; | |
609 | krb5_error_code problem; | |
610 | krb5_data outbuf; | |
611 | krb5_ccache ccache = NULL; | |
612 | krb5_creds creds; | |
613 | krb5_kdc_flags flags; | |
614 | const char *remotehost; | |
184eed6a | 615 | |
ced49be2 | 616 | memset(&creds, 0, sizeof(creds)); |
617 | memset(&outbuf, 0, sizeof(outbuf)); | |
184eed6a | 618 | |
ced49be2 | 619 | fd = packet_get_connection_in(); |
184eed6a | 620 | |
ced49be2 | 621 | problem = krb5_auth_con_setaddrs_from_fd(context, auth_context, &fd); |
622 | if (problem) | |
623 | goto out; | |
184eed6a | 624 | |
ced49be2 | 625 | problem = krb5_cc_default(context, &ccache); |
626 | if (problem) | |
627 | goto out; | |
184eed6a | 628 | |
ced49be2 | 629 | problem = krb5_cc_get_principal(context, ccache, &creds.client); |
630 | if (problem) | |
631 | goto out; | |
184eed6a | 632 | |
ced49be2 | 633 | problem = krb5_build_principal(context, &creds.server, |
634 | strlen(creds.client->realm), creds.client->realm, | |
635 | "krbtgt", creds.client->realm, NULL); | |
636 | if (problem) | |
637 | goto out; | |
184eed6a | 638 | |
ced49be2 | 639 | creds.times.endtime = 0; |
184eed6a | 640 | |
ced49be2 | 641 | flags.i = 0; |
642 | flags.b.forwarded = 1; | |
643 | flags.b.forwardable = krb5_config_get_bool(context, NULL, | |
644 | "libdefaults", "forwardable", NULL); | |
184eed6a | 645 | |
ced49be2 | 646 | remotehost = get_canonical_hostname(1); |
184eed6a | 647 | |
ced49be2 | 648 | problem = krb5_get_forwarded_creds(context, auth_context, |
649 | ccache, flags.i, remotehost, &creds, &outbuf); | |
650 | if (problem) | |
651 | goto out; | |
184eed6a | 652 | |
ced49be2 | 653 | packet_start(SSH_CMSG_HAVE_KERBEROS_TGT); |
654 | packet_put_string((char *)outbuf.data, outbuf.length); | |
655 | packet_send(); | |
656 | packet_write_wait(); | |
184eed6a | 657 | |
ced49be2 | 658 | type = packet_read(&payload_len); |
184eed6a | 659 | |
ced49be2 | 660 | if (type == SSH_SMSG_SUCCESS) { |
661 | char *pname; | |
184eed6a | 662 | |
ced49be2 | 663 | krb5_unparse_name(context, creds.client, &pname); |
664 | debug("Kerberos v5 TGT forwarded (%s).", pname); | |
665 | xfree(pname); | |
666 | } else | |
667 | debug("Kerberos v5 TGT forwarding failed."); | |
184eed6a | 668 | |
ced49be2 | 669 | return; |
184eed6a | 670 | |
ced49be2 | 671 | out: |
672 | if (problem) | |
673 | debug("Kerberos v5 TGT forwarding failed: %s", | |
674 | krb5_get_err_text(context, problem)); | |
675 | if (creds.client) | |
676 | krb5_free_principal(context, creds.client); | |
677 | if (creds.server) | |
678 | krb5_free_principal(context, creds.server); | |
679 | if (ccache) | |
680 | krb5_cc_close(context, ccache); | |
681 | if (outbuf.data) | |
682 | xfree(outbuf.data); | |
683 | } | |
684 | #endif /* KRB5 */ | |
685 | ||
686 | #ifdef AFS | |
687 | static void | |
688 | send_krb4_tgt(void) | |
a306f2dd | 689 | { |
690 | CREDENTIALS *creds; | |
a306f2dd | 691 | struct stat st; |
ced49be2 | 692 | char buffer[4096], pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ]; |
693 | int problem, type, len; | |
184eed6a | 694 | |
a306f2dd | 695 | /* Don't do anything if we don't have any tickets. */ |
696 | if (stat(tkt_string(), &st) < 0) | |
ced49be2 | 697 | return; |
184eed6a | 698 | |
a306f2dd | 699 | creds = xmalloc(sizeof(*creds)); |
184eed6a | 700 | |
ced49be2 | 701 | problem = krb_get_tf_fullname(TKT_FILE, pname, pinst, prealm); |
702 | if (problem) | |
703 | goto out; | |
184eed6a | 704 | |
ced49be2 | 705 | problem = krb_get_cred("krbtgt", prealm, prealm, creds); |
706 | if (problem) | |
707 | goto out; | |
184eed6a | 708 | |
a306f2dd | 709 | if (time(0) > krb_life_to_time(creds->issue_date, creds->lifetime)) { |
ced49be2 | 710 | problem = RD_AP_EXP; |
711 | goto out; | |
a306f2dd | 712 | } |
ced49be2 | 713 | creds_to_radix(creds, (u_char *)buffer, sizeof(buffer)); |
184eed6a | 714 | |
a306f2dd | 715 | packet_start(SSH_CMSG_HAVE_KERBEROS_TGT); |
449c5ba5 | 716 | packet_put_cstring(buffer); |
a306f2dd | 717 | packet_send(); |
718 | packet_write_wait(); | |
184eed6a | 719 | |
ced49be2 | 720 | type = packet_read(&len); |
184eed6a | 721 | |
ced49be2 | 722 | if (type == SSH_SMSG_SUCCESS) |
723 | debug("Kerberos v4 TGT forwarded (%s%s%s@%s).", | |
724 | creds->pname, creds->pinst[0] ? "." : "", | |
725 | creds->pinst, creds->realm); | |
726 | else | |
727 | debug("Kerberos v4 TGT rejected."); | |
184eed6a | 728 | |
ced49be2 | 729 | xfree(creds); |
730 | return; | |
184eed6a | 731 | |
ced49be2 | 732 | out: |
733 | debug("Kerberos v4 TGT passing failed: %s", krb_err_txt[problem]); | |
734 | xfree(creds); | |
a306f2dd | 735 | } |
736 | ||
396c147e | 737 | static void |
a306f2dd | 738 | send_afs_tokens(void) |
739 | { | |
740 | CREDENTIALS creds; | |
741 | struct ViceIoctl parms; | |
742 | struct ClearToken ct; | |
ced49be2 | 743 | int i, type, len; |
a306f2dd | 744 | char buf[2048], *p, *server_cell; |
745 | char buffer[8192]; | |
184eed6a | 746 | |
a306f2dd | 747 | /* Move over ktc_GetToken, here's something leaner. */ |
748 | for (i = 0; i < 100; i++) { /* just in case */ | |
749 | parms.in = (char *) &i; | |
750 | parms.in_size = sizeof(i); | |
751 | parms.out = buf; | |
752 | parms.out_size = sizeof(buf); | |
753 | if (k_pioctl(0, VIOCGETTOK, &parms, 0) != 0) | |
754 | break; | |
755 | p = buf; | |
184eed6a | 756 | |
a306f2dd | 757 | /* Get secret token. */ |
1e3b8b07 | 758 | memcpy(&creds.ticket_st.length, p, sizeof(u_int)); |
a306f2dd | 759 | if (creds.ticket_st.length > MAX_KTXT_LEN) |
760 | break; | |
1e3b8b07 | 761 | p += sizeof(u_int); |
a306f2dd | 762 | memcpy(creds.ticket_st.dat, p, creds.ticket_st.length); |
763 | p += creds.ticket_st.length; | |
184eed6a | 764 | |
a306f2dd | 765 | /* Get clear token. */ |
766 | memcpy(&len, p, sizeof(len)); | |
767 | if (len != sizeof(struct ClearToken)) | |
768 | break; | |
769 | p += sizeof(len); | |
770 | memcpy(&ct, p, len); | |
771 | p += len; | |
772 | p += sizeof(len); /* primary flag */ | |
773 | server_cell = p; | |
184eed6a | 774 | |
a306f2dd | 775 | /* Flesh out our credentials. */ |
ced49be2 | 776 | strlcpy(creds.service, "afs", sizeof(creds.service)); |
a306f2dd | 777 | creds.instance[0] = '\0'; |
778 | strlcpy(creds.realm, server_cell, REALM_SZ); | |
779 | memcpy(creds.session, ct.HandShakeKey, DES_KEY_SZ); | |
780 | creds.issue_date = ct.BeginTimestamp; | |
ced49be2 | 781 | creds.lifetime = krb_time_to_life(creds.issue_date, |
782 | ct.EndTimestamp); | |
a306f2dd | 783 | creds.kvno = ct.AuthHandle; |
784 | snprintf(creds.pname, sizeof(creds.pname), "AFS ID %d", ct.ViceId); | |
785 | creds.pinst[0] = '\0'; | |
184eed6a | 786 | |
a306f2dd | 787 | /* Encode token, ship it off. */ |
ced49be2 | 788 | if (creds_to_radix(&creds, (u_char *)buffer, |
789 | sizeof(buffer)) <= 0) | |
a306f2dd | 790 | break; |
791 | packet_start(SSH_CMSG_HAVE_AFS_TOKEN); | |
449c5ba5 | 792 | packet_put_cstring(buffer); |
a306f2dd | 793 | packet_send(); |
794 | packet_write_wait(); | |
795 | ||
796 | /* Roger, Roger. Clearance, Clarence. What's your vector, | |
797 | Victor? */ | |
ced49be2 | 798 | type = packet_read(&len); |
184eed6a | 799 | |
a306f2dd | 800 | if (type == SSH_SMSG_FAILURE) |
801 | debug("AFS token for cell %s rejected.", server_cell); | |
802 | else if (type != SSH_SMSG_SUCCESS) | |
803 | packet_disconnect("Protocol error on AFS token response: %d", type); | |
804 | } | |
805 | } | |
806 | ||
807 | #endif /* AFS */ | |
808 | ||
809 | /* | |
810 | * Tries to authenticate with any string-based challenge/response system. | |
811 | * Note that the client code is not tied to s/key or TIS. | |
812 | */ | |
396c147e | 813 | static int |
5ba55ada | 814 | try_challenge_response_authentication(void) |
a306f2dd | 815 | { |
816 | int type, i; | |
817 | int payload_len; | |
1e3b8b07 | 818 | u_int clen; |
e3a70753 | 819 | char prompt[1024]; |
a306f2dd | 820 | char *challenge, *response; |
aa8003d6 | 821 | |
822 | debug("Doing challenge response authentication."); | |
823 | ||
a306f2dd | 824 | for (i = 0; i < options.number_of_password_prompts; i++) { |
e3a70753 | 825 | /* request a challenge */ |
826 | packet_start(SSH_CMSG_AUTH_TIS); | |
827 | packet_send(); | |
828 | packet_write_wait(); | |
829 | ||
830 | type = packet_read(&payload_len); | |
831 | if (type != SSH_SMSG_FAILURE && | |
832 | type != SSH_SMSG_AUTH_TIS_CHALLENGE) { | |
833 | packet_disconnect("Protocol error: got %d in response " | |
d464095c | 834 | "to SSH_CMSG_AUTH_TIS", type); |
e3a70753 | 835 | } |
836 | if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) { | |
d464095c | 837 | debug("No challenge."); |
e3a70753 | 838 | return 0; |
839 | } | |
840 | challenge = packet_get_string(&clen); | |
95500969 | 841 | packet_check_eom(); |
59c97189 | 842 | snprintf(prompt, sizeof prompt, "%s%s", challenge, |
184eed6a | 843 | strchr(challenge, '\n') ? "" : "\nResponse: "); |
e3a70753 | 844 | xfree(challenge); |
a306f2dd | 845 | if (i != 0) |
846 | error("Permission denied, please try again."); | |
e3a70753 | 847 | if (options.cipher == SSH_CIPHER_NONE) |
848 | log("WARNING: Encryption is disabled! " | |
849 | "Reponse will be transmitted in clear text."); | |
850 | response = read_passphrase(prompt, 0); | |
851 | if (strcmp(response, "") == 0) { | |
852 | xfree(response); | |
853 | break; | |
854 | } | |
a306f2dd | 855 | packet_start(SSH_CMSG_AUTH_TIS_RESPONSE); |
0ae4fe1d | 856 | ssh_put_password(response); |
a306f2dd | 857 | memset(response, 0, strlen(response)); |
858 | xfree(response); | |
859 | packet_send(); | |
860 | packet_write_wait(); | |
861 | type = packet_read(&payload_len); | |
862 | if (type == SSH_SMSG_SUCCESS) | |
863 | return 1; | |
864 | if (type != SSH_SMSG_FAILURE) | |
865 | packet_disconnect("Protocol error: got %d in response " | |
d464095c | 866 | "to SSH_CMSG_AUTH_TIS_RESPONSE", type); |
a306f2dd | 867 | } |
868 | /* failure */ | |
869 | return 0; | |
870 | } | |
871 | ||
872 | /* | |
873 | * Tries to authenticate with plain passwd authentication. | |
874 | */ | |
396c147e | 875 | static int |
a306f2dd | 876 | try_password_authentication(char *prompt) |
877 | { | |
878 | int type, i, payload_len; | |
879 | char *password; | |
880 | ||
881 | debug("Doing password authentication."); | |
882 | if (options.cipher == SSH_CIPHER_NONE) | |
883 | log("WARNING: Encryption is disabled! Password will be transmitted in clear text."); | |
884 | for (i = 0; i < options.number_of_password_prompts; i++) { | |
885 | if (i != 0) | |
886 | error("Permission denied, please try again."); | |
887 | password = read_passphrase(prompt, 0); | |
888 | packet_start(SSH_CMSG_AUTH_PASSWORD); | |
0ae4fe1d | 889 | ssh_put_password(password); |
a306f2dd | 890 | memset(password, 0, strlen(password)); |
891 | xfree(password); | |
892 | packet_send(); | |
893 | packet_write_wait(); | |
894 | ||
895 | type = packet_read(&payload_len); | |
896 | if (type == SSH_SMSG_SUCCESS) | |
897 | return 1; | |
898 | if (type != SSH_SMSG_FAILURE) | |
899 | packet_disconnect("Protocol error: got %d in response to passwd auth", type); | |
900 | } | |
901 | /* failure */ | |
902 | return 0; | |
903 | } | |
904 | ||
905 | /* | |
906 | * SSH1 key exchange | |
907 | */ | |
908 | void | |
909 | ssh_kex(char *host, struct sockaddr *hostaddr) | |
910 | { | |
911 | int i; | |
912 | BIGNUM *key; | |
b775c6f2 | 913 | Key *host_key, *server_key; |
a306f2dd | 914 | int bits, rbits; |
915 | int ssh_cipher_default = SSH_CIPHER_3DES; | |
1e3b8b07 | 916 | u_char session_key[SSH_SESSION_KEY_LENGTH]; |
917 | u_char cookie[8]; | |
918 | u_int supported_ciphers; | |
919 | u_int server_flags, client_flags; | |
20b279e6 | 920 | int payload_len; |
a306f2dd | 921 | u_int32_t rand = 0; |
922 | ||
923 | debug("Waiting for server public key."); | |
924 | ||
925 | /* Wait for a public key packet from the server. */ | |
926 | packet_read_expect(&payload_len, SSH_SMSG_PUBLIC_KEY); | |
927 | ||
928 | /* Get cookie from the packet. */ | |
929 | for (i = 0; i < 8; i++) | |
930 | cookie[i] = packet_get_char(); | |
931 | ||
932 | /* Get the public key. */ | |
b775c6f2 | 933 | server_key = key_new(KEY_RSA1); |
934 | bits = packet_get_int(); | |
20b279e6 | 935 | packet_get_bignum(server_key->rsa->e); |
936 | packet_get_bignum(server_key->rsa->n); | |
a306f2dd | 937 | |
b775c6f2 | 938 | rbits = BN_num_bits(server_key->rsa->n); |
a306f2dd | 939 | if (bits != rbits) { |
940 | log("Warning: Server lies about size of server public key: " | |
941 | "actual size is %d bits vs. announced %d.", rbits, bits); | |
942 | log("Warning: This may be due to an old implementation of ssh."); | |
943 | } | |
944 | /* Get the host key. */ | |
b775c6f2 | 945 | host_key = key_new(KEY_RSA1); |
946 | bits = packet_get_int(); | |
20b279e6 | 947 | packet_get_bignum(host_key->rsa->e); |
948 | packet_get_bignum(host_key->rsa->n); | |
a306f2dd | 949 | |
b775c6f2 | 950 | rbits = BN_num_bits(host_key->rsa->n); |
a306f2dd | 951 | if (bits != rbits) { |
952 | log("Warning: Server lies about size of server host key: " | |
953 | "actual size is %d bits vs. announced %d.", rbits, bits); | |
954 | log("Warning: This may be due to an old implementation of ssh."); | |
955 | } | |
956 | ||
957 | /* Get protocol flags. */ | |
958 | server_flags = packet_get_int(); | |
959 | packet_set_protocol_flags(server_flags); | |
960 | ||
961 | supported_ciphers = packet_get_int(); | |
962 | supported_authentications = packet_get_int(); | |
95500969 | 963 | packet_check_eom(); |
a306f2dd | 964 | |
965 | debug("Received server public key (%d bits) and host key (%d bits).", | |
b775c6f2 | 966 | BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n)); |
a306f2dd | 967 | |
b775c6f2 | 968 | if (verify_host_key(host, hostaddr, host_key) == -1) |
01e9ef57 | 969 | fatal("Host key verification failed."); |
a306f2dd | 970 | |
971 | client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN; | |
972 | ||
b775c6f2 | 973 | compute_session_id(session_id, cookie, host_key->rsa->n, server_key->rsa->n); |
a306f2dd | 974 | |
975 | /* Generate a session key. */ | |
976 | arc4random_stir(); | |
977 | ||
978 | /* | |
979 | * Generate an encryption key for the session. The key is a 256 bit | |
980 | * random number, interpreted as a 32-byte key, with the least | |
981 | * significant 8 bits being the first byte of the key. | |
982 | */ | |
983 | for (i = 0; i < 32; i++) { | |
984 | if (i % 4 == 0) | |
985 | rand = arc4random(); | |
986 | session_key[i] = rand & 0xff; | |
987 | rand >>= 8; | |
988 | } | |
989 | ||
990 | /* | |
991 | * According to the protocol spec, the first byte of the session key | |
992 | * is the highest byte of the integer. The session key is xored with | |
993 | * the first 16 bytes of the session id. | |
994 | */ | |
b775c6f2 | 995 | if ((key = BN_new()) == NULL) |
996 | fatal("respond_to_rsa_challenge: BN_new failed"); | |
a306f2dd | 997 | BN_set_word(key, 0); |
998 | for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) { | |
999 | BN_lshift(key, key, 8); | |
1000 | if (i < 16) | |
1001 | BN_add_word(key, session_key[i] ^ session_id[i]); | |
1002 | else | |
1003 | BN_add_word(key, session_key[i]); | |
1004 | } | |
1005 | ||
1006 | /* | |
1007 | * Encrypt the integer using the public key and host key of the | |
1008 | * server (key with smaller modulus first). | |
1009 | */ | |
b775c6f2 | 1010 | if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) { |
a306f2dd | 1011 | /* Public key has smaller modulus. */ |
b775c6f2 | 1012 | if (BN_num_bits(host_key->rsa->n) < |
1013 | BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) { | |
1014 | fatal("respond_to_rsa_challenge: host_key %d < server_key %d + " | |
184eed6a | 1015 | "SSH_KEY_BITS_RESERVED %d", |
b775c6f2 | 1016 | BN_num_bits(host_key->rsa->n), |
1017 | BN_num_bits(server_key->rsa->n), | |
184eed6a | 1018 | SSH_KEY_BITS_RESERVED); |
a306f2dd | 1019 | } |
b775c6f2 | 1020 | rsa_public_encrypt(key, key, server_key->rsa); |
1021 | rsa_public_encrypt(key, key, host_key->rsa); | |
a306f2dd | 1022 | } else { |
1023 | /* Host key has smaller modulus (or they are equal). */ | |
b775c6f2 | 1024 | if (BN_num_bits(server_key->rsa->n) < |
1025 | BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) { | |
1026 | fatal("respond_to_rsa_challenge: server_key %d < host_key %d + " | |
184eed6a | 1027 | "SSH_KEY_BITS_RESERVED %d", |
b775c6f2 | 1028 | BN_num_bits(server_key->rsa->n), |
1029 | BN_num_bits(host_key->rsa->n), | |
184eed6a | 1030 | SSH_KEY_BITS_RESERVED); |
a306f2dd | 1031 | } |
b775c6f2 | 1032 | rsa_public_encrypt(key, key, host_key->rsa); |
1033 | rsa_public_encrypt(key, key, server_key->rsa); | |
a306f2dd | 1034 | } |
1035 | ||
1036 | /* Destroy the public keys since we no longer need them. */ | |
b775c6f2 | 1037 | key_free(server_key); |
1038 | key_free(host_key); | |
a306f2dd | 1039 | |
c523303b | 1040 | if (options.cipher == SSH_CIPHER_NOT_SET) { |
1041 | if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default)) | |
1042 | options.cipher = ssh_cipher_default; | |
1043 | } else if (options.cipher == SSH_CIPHER_ILLEGAL || | |
1044 | !(cipher_mask_ssh1(1) & (1 << options.cipher))) { | |
d0c832f3 | 1045 | log("No valid SSH1 cipher, using %.100s instead.", |
94ec8c6b | 1046 | cipher_name(ssh_cipher_default)); |
1047 | options.cipher = ssh_cipher_default; | |
a306f2dd | 1048 | } |
1049 | /* Check that the selected cipher is supported. */ | |
1050 | if (!(supported_ciphers & (1 << options.cipher))) | |
1051 | fatal("Selected cipher type %.100s not supported by server.", | |
184eed6a | 1052 | cipher_name(options.cipher)); |
a306f2dd | 1053 | |
1054 | debug("Encryption type: %.100s", cipher_name(options.cipher)); | |
1055 | ||
1056 | /* Send the encrypted session key to the server. */ | |
1057 | packet_start(SSH_CMSG_SESSION_KEY); | |
1058 | packet_put_char(options.cipher); | |
1059 | ||
1060 | /* Send the cookie back to the server. */ | |
1061 | for (i = 0; i < 8; i++) | |
1062 | packet_put_char(cookie[i]); | |
1063 | ||
1064 | /* Send and destroy the encrypted encryption key integer. */ | |
1065 | packet_put_bignum(key); | |
1066 | BN_clear_free(key); | |
1067 | ||
1068 | /* Send protocol flags. */ | |
1069 | packet_put_int(client_flags); | |
1070 | ||
1071 | /* Send the packet now. */ | |
1072 | packet_send(); | |
1073 | packet_write_wait(); | |
1074 | ||
1075 | debug("Sent encrypted session key."); | |
1076 | ||
1077 | /* Set the encryption key. */ | |
1078 | packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher); | |
1079 | ||
1080 | /* We will no longer need the session key here. Destroy any extra copies. */ | |
1081 | memset(session_key, 0, sizeof(session_key)); | |
1082 | ||
1083 | /* | |
1084 | * Expect a success message from the server. Note that this message | |
1085 | * will be received in encrypted form. | |
1086 | */ | |
1087 | packet_read_expect(&payload_len, SSH_SMSG_SUCCESS); | |
1088 | ||
1089 | debug("Received encrypted confirmation."); | |
1090 | } | |
1091 | ||
1092 | /* | |
1093 | * Authenticate user | |
1094 | */ | |
1095 | void | |
8002af61 | 1096 | ssh_userauth1(const char *local_user, const char *server_user, char *host, |
1097 | Key **keys, int nkeys) | |
a306f2dd | 1098 | { |
ced49be2 | 1099 | #ifdef KRB5 |
1100 | krb5_context context = NULL; | |
1101 | krb5_auth_context auth_context = NULL; | |
1102 | #endif | |
a306f2dd | 1103 | int i, type; |
1104 | int payload_len; | |
184eed6a | 1105 | |
a306f2dd | 1106 | if (supported_authentications == 0) |
8002af61 | 1107 | fatal("ssh_userauth1: server supports no auth methods"); |
a306f2dd | 1108 | |
1109 | /* Send the name of the user to log in as on the server. */ | |
1110 | packet_start(SSH_CMSG_USER); | |
449c5ba5 | 1111 | packet_put_cstring(server_user); |
a306f2dd | 1112 | packet_send(); |
1113 | packet_write_wait(); | |
1114 | ||
1115 | /* | |
1116 | * The server should respond with success if no authentication is | |
1117 | * needed (the user has no password). Otherwise the server responds | |
1118 | * with failure. | |
1119 | */ | |
1120 | type = packet_read(&payload_len); | |
1121 | ||
1122 | /* check whether the connection was accepted without authentication. */ | |
1123 | if (type == SSH_SMSG_SUCCESS) | |
ced49be2 | 1124 | goto success; |
a306f2dd | 1125 | if (type != SSH_SMSG_FAILURE) |
ced49be2 | 1126 | packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type); |
184eed6a | 1127 | |
ced49be2 | 1128 | #ifdef KRB5 |
1129 | if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) && | |
184eed6a | 1130 | options.kerberos_authentication) { |
ced49be2 | 1131 | debug("Trying Kerberos v5 authentication."); |
184eed6a | 1132 | |
ced49be2 | 1133 | if (try_krb5_authentication(&context, &auth_context)) { |
1134 | type = packet_read(&payload_len); | |
1135 | if (type == SSH_SMSG_SUCCESS) | |
1136 | goto success; | |
1137 | if (type != SSH_SMSG_FAILURE) | |
1138 | packet_disconnect("Protocol error: got %d in response to Kerberos v5 auth", type); | |
1139 | } | |
a306f2dd | 1140 | } |
ced49be2 | 1141 | #endif /* KRB5 */ |
184eed6a | 1142 | |
a306f2dd | 1143 | #ifdef KRB4 |
1144 | if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) && | |
1145 | options.kerberos_authentication) { | |
ced49be2 | 1146 | debug("Trying Kerberos v4 authentication."); |
184eed6a | 1147 | |
ced49be2 | 1148 | if (try_krb4_authentication()) { |
a306f2dd | 1149 | type = packet_read(&payload_len); |
1150 | if (type == SSH_SMSG_SUCCESS) | |
ced49be2 | 1151 | goto success; |
a306f2dd | 1152 | if (type != SSH_SMSG_FAILURE) |
ced49be2 | 1153 | packet_disconnect("Protocol error: got %d in response to Kerberos v4 auth", type); |
a306f2dd | 1154 | } |
1155 | } | |
1156 | #endif /* KRB4 */ | |
184eed6a | 1157 | |
a306f2dd | 1158 | /* |
1159 | * Use rhosts authentication if running in privileged socket and we | |
1160 | * do not wish to remain anonymous. | |
1161 | */ | |
1162 | if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) && | |
1163 | options.rhosts_authentication) { | |
1164 | debug("Trying rhosts authentication."); | |
1165 | packet_start(SSH_CMSG_AUTH_RHOSTS); | |
449c5ba5 | 1166 | packet_put_cstring(local_user); |
a306f2dd | 1167 | packet_send(); |
1168 | packet_write_wait(); | |
1169 | ||
1170 | /* The server should respond with success or failure. */ | |
1171 | type = packet_read(&payload_len); | |
1172 | if (type == SSH_SMSG_SUCCESS) | |
ced49be2 | 1173 | goto success; |
a306f2dd | 1174 | if (type != SSH_SMSG_FAILURE) |
1175 | packet_disconnect("Protocol error: got %d in response to rhosts auth", | |
1176 | type); | |
1177 | } | |
1178 | /* | |
1179 | * Try .rhosts or /etc/hosts.equiv authentication with RSA host | |
1180 | * authentication. | |
1181 | */ | |
1182 | if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) && | |
8002af61 | 1183 | options.rhosts_rsa_authentication) { |
1184 | for (i = 0; i < nkeys; i++) { | |
eba4bef7 | 1185 | if (keys[i] != NULL && keys[i]->type == KEY_RSA1 && |
8002af61 | 1186 | try_rhosts_rsa_authentication(local_user, keys[i])) |
ced49be2 | 1187 | goto success; |
8002af61 | 1188 | } |
a306f2dd | 1189 | } |
1190 | /* Try RSA authentication if the server supports it. */ | |
1191 | if ((supported_authentications & (1 << SSH_AUTH_RSA)) && | |
1192 | options.rsa_authentication) { | |
1193 | /* | |
1194 | * Try RSA authentication using the authentication agent. The | |
1195 | * agent is tried first because no passphrase is needed for | |
1196 | * it, whereas identity files may require passphrases. | |
1197 | */ | |
1198 | if (try_agent_authentication()) | |
ced49be2 | 1199 | goto success; |
a306f2dd | 1200 | |
1201 | /* Try RSA authentication for each identity. */ | |
1202 | for (i = 0; i < options.num_identity_files; i++) | |
fee56204 | 1203 | if (options.identity_keys[i] != NULL && |
1204 | options.identity_keys[i]->type == KEY_RSA1 && | |
383546c4 | 1205 | try_rsa_authentication(i)) |
ced49be2 | 1206 | goto success; |
a306f2dd | 1207 | } |
d464095c | 1208 | /* Try challenge response authentication if the server supports it. */ |
a306f2dd | 1209 | if ((supported_authentications & (1 << SSH_AUTH_TIS)) && |
5ba55ada | 1210 | options.challenge_response_authentication && !options.batch_mode) { |
1211 | if (try_challenge_response_authentication()) | |
ced49be2 | 1212 | goto success; |
a306f2dd | 1213 | } |
1214 | /* Try password authentication if the server supports it. */ | |
1215 | if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) && | |
1216 | options.password_authentication && !options.batch_mode) { | |
1217 | char prompt[80]; | |
1218 | ||
b3b9ad8e | 1219 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", |
a306f2dd | 1220 | server_user, host); |
1221 | if (try_password_authentication(prompt)) | |
ced49be2 | 1222 | goto success; |
a306f2dd | 1223 | } |
1224 | /* All authentication methods have failed. Exit with an error message. */ | |
1225 | fatal("Permission denied."); | |
1226 | /* NOTREACHED */ | |
ced49be2 | 1227 | |
1228 | success: | |
1229 | #ifdef KRB5 | |
1230 | /* Try Kerberos v5 TGT passing. */ | |
1231 | if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) && | |
1232 | options.kerberos_tgt_passing && context && auth_context) { | |
1233 | if (options.cipher == SSH_CIPHER_NONE) | |
1234 | log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!"); | |
1235 | send_krb5_tgt(context, auth_context); | |
1236 | } | |
1237 | if (auth_context) | |
1238 | krb5_auth_con_free(context, auth_context); | |
1239 | if (context) | |
1240 | krb5_free_context(context); | |
1241 | #endif | |
184eed6a | 1242 | |
ced49be2 | 1243 | #ifdef AFS |
1244 | /* Try Kerberos v4 TGT passing if the server supports it. */ | |
1245 | if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) && | |
1246 | options.kerberos_tgt_passing) { | |
1247 | if (options.cipher == SSH_CIPHER_NONE) | |
1248 | log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!"); | |
1249 | send_krb4_tgt(); | |
1250 | } | |
1251 | /* Try AFS token passing if the server supports it. */ | |
1252 | if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) && | |
1253 | options.afs_token_passing && k_hasafs()) { | |
1254 | if (options.cipher == SSH_CIPHER_NONE) | |
1255 | log("WARNING: Encryption is disabled! Token will be transmitted in the clear!"); | |
1256 | send_afs_tokens(); | |
1257 | } | |
1258 | #endif /* AFS */ | |
d9e3d19f | 1259 | |
62bb2c8f | 1260 | return; /* need statement after label */ |
a306f2dd | 1261 | } |