]>
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" | |
150a5466 | 16 | RCSID("$OpenBSD: sshconnect1.c,v 1.49 2002/03/14 15:24:27 markus Exp $"); |
a306f2dd | 17 | |
18 | #include <openssl/bn.h> | |
6b4b5e49 | 19 | #include <openssl/md5.h> |
a306f2dd | 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; | |
2e73a022 | 70 | Key *key; |
71 | BIGNUM *challenge; | |
a306f2dd | 72 | |
73 | /* Get connection to the agent. */ | |
74 | auth = ssh_get_authentication_connection(); | |
75 | if (!auth) | |
76 | return 0; | |
77 | ||
b775c6f2 | 78 | if ((challenge = BN_new()) == NULL) |
79 | fatal("try_agent_authentication: BN_new failed"); | |
a306f2dd | 80 | /* Loop through identities served by the agent. */ |
2e73a022 | 81 | for (key = ssh_get_first_identity(auth, &comment, 1); |
184eed6a | 82 | key != NULL; |
83 | key = ssh_get_next_identity(auth, &comment, 1)) { | |
a306f2dd | 84 | |
85 | /* Try this identity. */ | |
86 | debug("Trying RSA authentication via agent with '%.100s'", comment); | |
87 | xfree(comment); | |
88 | ||
89 | /* Tell the server that we are willing to authenticate using this key. */ | |
90 | packet_start(SSH_CMSG_AUTH_RSA); | |
2e73a022 | 91 | packet_put_bignum(key->rsa->n); |
a306f2dd | 92 | packet_send(); |
93 | packet_write_wait(); | |
94 | ||
95 | /* Wait for server's response. */ | |
54a5250f | 96 | type = packet_read(); |
a306f2dd | 97 | |
98 | /* The server sends failure if it doesn\'t like our key or | |
99 | does not support RSA authentication. */ | |
100 | if (type == SSH_SMSG_FAILURE) { | |
101 | debug("Server refused our key."); | |
2e73a022 | 102 | key_free(key); |
a306f2dd | 103 | continue; |
104 | } | |
105 | /* Otherwise it should have sent a challenge. */ | |
106 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) | |
107 | packet_disconnect("Protocol error during RSA authentication: %d", | |
108 | type); | |
109 | ||
20b279e6 | 110 | packet_get_bignum(challenge); |
95500969 | 111 | packet_check_eom(); |
a306f2dd | 112 | |
113 | debug("Received RSA challenge from server."); | |
114 | ||
115 | /* Ask the agent to decrypt the challenge. */ | |
2e73a022 | 116 | if (!ssh_decrypt_challenge(auth, key, challenge, session_id, 1, response)) { |
117 | /* | |
118 | * The agent failed to authenticate this identifier | |
119 | * although it advertised it supports this. Just | |
120 | * return a wrong value. | |
121 | */ | |
a306f2dd | 122 | log("Authentication agent failed to decrypt challenge."); |
123 | memset(response, 0, sizeof(response)); | |
124 | } | |
2e73a022 | 125 | key_free(key); |
a306f2dd | 126 | debug("Sending response to RSA challenge."); |
127 | ||
128 | /* Send the decrypted challenge back to the server. */ | |
129 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); | |
130 | for (i = 0; i < 16; i++) | |
131 | packet_put_char(response[i]); | |
132 | packet_send(); | |
133 | packet_write_wait(); | |
134 | ||
135 | /* Wait for response from the server. */ | |
54a5250f | 136 | type = packet_read(); |
a306f2dd | 137 | |
138 | /* The server returns success if it accepted the authentication. */ | |
139 | if (type == SSH_SMSG_SUCCESS) { | |
eea39c02 | 140 | ssh_close_authentication_connection(auth); |
a306f2dd | 141 | BN_clear_free(challenge); |
2e73a022 | 142 | debug("RSA authentication accepted by server."); |
a306f2dd | 143 | return 1; |
144 | } | |
145 | /* Otherwise it should return failure. */ | |
146 | if (type != SSH_SMSG_FAILURE) | |
147 | packet_disconnect("Protocol error waiting RSA auth response: %d", | |
148 | type); | |
149 | } | |
eea39c02 | 150 | ssh_close_authentication_connection(auth); |
a306f2dd | 151 | BN_clear_free(challenge); |
a306f2dd | 152 | debug("RSA authentication using agent refused."); |
153 | return 0; | |
154 | } | |
155 | ||
156 | /* | |
157 | * Computes the proper response to a RSA challenge, and sends the response to | |
158 | * the server. | |
159 | */ | |
396c147e | 160 | static void |
a306f2dd | 161 | respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv) |
162 | { | |
1e3b8b07 | 163 | u_char buf[32], response[16]; |
a306f2dd | 164 | MD5_CTX md; |
165 | int i, len; | |
166 | ||
167 | /* Decrypt the challenge using the private key. */ | |
46aa2d1f | 168 | /* XXX think about Bleichenbacher, too */ |
169 | if (rsa_private_decrypt(challenge, challenge, prv) <= 0) | |
170 | packet_disconnect( | |
171 | "respond_to_rsa_challenge: rsa_private_decrypt failed"); | |
a306f2dd | 172 | |
173 | /* Compute the response. */ | |
174 | /* The response is MD5 of decrypted challenge plus session id. */ | |
175 | len = BN_num_bytes(challenge); | |
176 | if (len <= 0 || len > sizeof(buf)) | |
46aa2d1f | 177 | packet_disconnect( |
178 | "respond_to_rsa_challenge: bad challenge length %d", len); | |
a306f2dd | 179 | |
180 | memset(buf, 0, sizeof(buf)); | |
181 | BN_bn2bin(challenge, buf + sizeof(buf) - len); | |
182 | MD5_Init(&md); | |
183 | MD5_Update(&md, buf, 32); | |
184 | MD5_Update(&md, session_id, 16); | |
185 | MD5_Final(response, &md); | |
186 | ||
187 | debug("Sending response to host key RSA challenge."); | |
188 | ||
189 | /* Send the response back to the server. */ | |
190 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); | |
191 | for (i = 0; i < 16; i++) | |
192 | packet_put_char(response[i]); | |
193 | packet_send(); | |
194 | packet_write_wait(); | |
195 | ||
196 | memset(buf, 0, sizeof(buf)); | |
197 | memset(response, 0, sizeof(response)); | |
198 | memset(&md, 0, sizeof(md)); | |
199 | } | |
200 | ||
201 | /* | |
202 | * Checks if the user has authentication file, and if so, tries to authenticate | |
203 | * the user using it. | |
204 | */ | |
396c147e | 205 | static int |
383546c4 | 206 | try_rsa_authentication(int idx) |
a306f2dd | 207 | { |
208 | BIGNUM *challenge; | |
ce404659 | 209 | Key *public, *private; |
383546c4 | 210 | char buf[300], *passphrase, *comment, *authfile; |
54a5250f | 211 | int i, type, quit; |
a306f2dd | 212 | |
383546c4 | 213 | public = options.identity_keys[idx]; |
214 | authfile = options.identity_files[idx]; | |
215 | comment = xstrdup(authfile); | |
216 | ||
a306f2dd | 217 | debug("Trying RSA authentication with key '%.100s'", comment); |
218 | ||
219 | /* Tell the server that we are willing to authenticate using this key. */ | |
220 | packet_start(SSH_CMSG_AUTH_RSA); | |
221 | packet_put_bignum(public->rsa->n); | |
222 | packet_send(); | |
223 | packet_write_wait(); | |
224 | ||
a306f2dd | 225 | /* Wait for server's response. */ |
54a5250f | 226 | type = packet_read(); |
a306f2dd | 227 | |
228 | /* | |
229 | * The server responds with failure if it doesn\'t like our key or | |
230 | * doesn\'t support RSA authentication. | |
231 | */ | |
232 | if (type == SSH_SMSG_FAILURE) { | |
233 | debug("Server refused our key."); | |
234 | xfree(comment); | |
235 | return 0; | |
236 | } | |
237 | /* Otherwise, the server should respond with a challenge. */ | |
238 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) | |
239 | packet_disconnect("Protocol error during RSA authentication: %d", type); | |
240 | ||
241 | /* Get the challenge from the packet. */ | |
b775c6f2 | 242 | if ((challenge = BN_new()) == NULL) |
243 | fatal("try_rsa_authentication: BN_new failed"); | |
20b279e6 | 244 | packet_get_bignum(challenge); |
95500969 | 245 | packet_check_eom(); |
a306f2dd | 246 | |
247 | debug("Received RSA challenge from server."); | |
248 | ||
a306f2dd | 249 | /* |
383546c4 | 250 | * If the key is not stored in external hardware, we have to |
251 | * load the private key. Try first with empty passphrase; if it | |
a306f2dd | 252 | * fails, ask for a passphrase. |
253 | */ | |
383546c4 | 254 | if (public->flags && KEY_FLAG_EXT) |
255 | private = public; | |
256 | else | |
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... */ | |
54a5250f | 293 | packet_read_expect(SSH_SMSG_FAILURE); |
ce404659 | 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 | ||
383546c4 | 301 | /* Destroy the private key unless it in external hardware. */ |
302 | if (!(private->flags & KEY_FLAG_EXT)) | |
303 | key_free(private); | |
a306f2dd | 304 | |
305 | /* We no longer need the challenge. */ | |
306 | BN_clear_free(challenge); | |
307 | ||
308 | /* Wait for response from the server. */ | |
54a5250f | 309 | type = packet_read(); |
a306f2dd | 310 | if (type == SSH_SMSG_SUCCESS) { |
311 | debug("RSA authentication accepted by server."); | |
312 | return 1; | |
313 | } | |
314 | if (type != SSH_SMSG_FAILURE) | |
315 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); | |
316 | debug("RSA authentication refused."); | |
317 | return 0; | |
318 | } | |
319 | ||
320 | /* | |
321 | * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv | |
322 | * authentication and RSA host authentication. | |
323 | */ | |
396c147e | 324 | static int |
aeaa3d9e | 325 | try_rhosts_rsa_authentication(const char *local_user, Key * host_key) |
a306f2dd | 326 | { |
327 | int type; | |
328 | BIGNUM *challenge; | |
a306f2dd | 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. */ | |
54a5250f | 342 | type = packet_read(); |
a306f2dd | 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. */ | |
b775c6f2 | 355 | if ((challenge = BN_new()) == NULL) |
356 | fatal("try_rhosts_rsa_authentication: BN_new failed"); | |
20b279e6 | 357 | packet_get_bignum(challenge); |
95500969 | 358 | packet_check_eom(); |
a306f2dd | 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. */ | |
54a5250f | 369 | type = packet_read(); |
a306f2dd | 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 |
ced49be2 | 382 | try_krb4_authentication(void) |
a306f2dd | 383 | { |
384 | KTEXT_ST auth; /* Kerberos data */ | |
385 | char *reply; | |
386 | char inst[INST_SZ]; | |
387 | char *realm; | |
388 | CREDENTIALS cred; | |
54a5250f | 389 | int r, type; |
a306f2dd | 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; | |
184eed6a | 400 | |
ced49be2 | 401 | strlcpy(inst, (char *)krb_get_phost(get_canonical_hostname(1)), |
402 | INST_SZ); | |
184eed6a | 403 | |
ced49be2 | 404 | realm = (char *)krb_realmofhost(get_canonical_hostname(1)); |
a306f2dd | 405 | if (!realm) { |
ced49be2 | 406 | debug("Kerberos v4: no realm for %s", get_canonical_hostname(1)); |
a306f2dd | 407 | return 0; |
408 | } | |
409 | /* This can really be anything. */ | |
ced49be2 | 410 | checksum = (u_long)getpid(); |
184eed6a | 411 | |
a306f2dd | 412 | r = krb_mk_req(&auth, KRB4_SERVICE_NAME, inst, realm, checksum); |
413 | if (r != KSUCCESS) { | |
ced49be2 | 414 | debug("Kerberos v4 krb_mk_req failed: %s", krb_err_txt[r]); |
a306f2dd | 415 | return 0; |
416 | } | |
417 | /* Get session key to decrypt the server's reply with. */ | |
418 | r = krb_get_cred(KRB4_SERVICE_NAME, inst, realm, &cred); | |
419 | if (r != KSUCCESS) { | |
420 | debug("get_cred failed: %s", krb_err_txt[r]); | |
421 | return 0; | |
422 | } | |
423 | des_key_sched((des_cblock *) cred.session, schedule); | |
184eed6a | 424 | |
a306f2dd | 425 | /* Send authentication info to server. */ |
426 | packet_start(SSH_CMSG_AUTH_KERBEROS); | |
427 | packet_put_string((char *) auth.dat, auth.length); | |
428 | packet_send(); | |
429 | packet_write_wait(); | |
184eed6a | 430 | |
a306f2dd | 431 | /* Zero the buffer. */ |
432 | (void) memset(auth.dat, 0, MAX_KTXT_LEN); | |
184eed6a | 433 | |
a306f2dd | 434 | slen = sizeof(local); |
435 | memset(&local, 0, sizeof(local)); | |
436 | if (getsockname(packet_get_connection_in(), | |
ced49be2 | 437 | (struct sockaddr *)&local, &slen) < 0) |
a306f2dd | 438 | debug("getsockname failed: %s", strerror(errno)); |
184eed6a | 439 | |
a306f2dd | 440 | slen = sizeof(foreign); |
441 | memset(&foreign, 0, sizeof(foreign)); | |
442 | if (getpeername(packet_get_connection_in(), | |
ced49be2 | 443 | (struct sockaddr *)&foreign, &slen) < 0) { |
a306f2dd | 444 | debug("getpeername failed: %s", strerror(errno)); |
445 | fatal_cleanup(); | |
446 | } | |
447 | /* Get server reply. */ | |
54a5250f | 448 | type = packet_read(); |
a306f2dd | 449 | switch (type) { |
450 | case SSH_SMSG_FAILURE: | |
451 | /* Should really be SSH_SMSG_AUTH_KERBEROS_FAILURE */ | |
ced49be2 | 452 | debug("Kerberos v4 authentication failed."); |
a306f2dd | 453 | return 0; |
454 | break; | |
184eed6a | 455 | |
a306f2dd | 456 | case SSH_SMSG_AUTH_KERBEROS_RESPONSE: |
457 | /* SSH_SMSG_AUTH_KERBEROS_SUCCESS */ | |
ced49be2 | 458 | debug("Kerberos v4 authentication accepted."); |
184eed6a | 459 | |
a306f2dd | 460 | /* Get server's response. */ |
1e3b8b07 | 461 | reply = packet_get_string((u_int *) &auth.length); |
150a5466 | 462 | if (auth.length >= MAX_KTXT_LEN) |
463 | fatal("Kerberos v4: Malformed response from server"); | |
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; | |
54a5250f | 512 | int type; |
ced49be2 | 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 | |
54a5250f | 562 | type = packet_read(); |
ced49be2 | 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 | { | |
54a5250f | 608 | int fd, type; |
ced49be2 | 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 | |
54a5250f | 658 | type = packet_read(); |
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]; |
54a5250f | 693 | int problem, type; |
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 | |
54a5250f | 720 | type = packet_read(); |
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? */ | |
54a5250f | 798 | type = packet_read(); |
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; | |
1e3b8b07 | 817 | u_int clen; |
e3a70753 | 818 | char prompt[1024]; |
a306f2dd | 819 | char *challenge, *response; |
aa8003d6 | 820 | |
821 | debug("Doing challenge response authentication."); | |
822 | ||
a306f2dd | 823 | for (i = 0; i < options.number_of_password_prompts; i++) { |
e3a70753 | 824 | /* request a challenge */ |
825 | packet_start(SSH_CMSG_AUTH_TIS); | |
826 | packet_send(); | |
827 | packet_write_wait(); | |
828 | ||
54a5250f | 829 | type = packet_read(); |
e3a70753 | 830 | if (type != SSH_SMSG_FAILURE && |
831 | type != SSH_SMSG_AUTH_TIS_CHALLENGE) { | |
832 | packet_disconnect("Protocol error: got %d in response " | |
d464095c | 833 | "to SSH_CMSG_AUTH_TIS", type); |
e3a70753 | 834 | } |
835 | if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) { | |
d464095c | 836 | debug("No challenge."); |
e3a70753 | 837 | return 0; |
838 | } | |
839 | challenge = packet_get_string(&clen); | |
95500969 | 840 | packet_check_eom(); |
59c97189 | 841 | snprintf(prompt, sizeof prompt, "%s%s", challenge, |
184eed6a | 842 | strchr(challenge, '\n') ? "" : "\nResponse: "); |
e3a70753 | 843 | xfree(challenge); |
a306f2dd | 844 | if (i != 0) |
845 | error("Permission denied, please try again."); | |
e3a70753 | 846 | if (options.cipher == SSH_CIPHER_NONE) |
847 | log("WARNING: Encryption is disabled! " | |
848 | "Reponse will be transmitted in clear text."); | |
849 | response = read_passphrase(prompt, 0); | |
850 | if (strcmp(response, "") == 0) { | |
851 | xfree(response); | |
852 | break; | |
853 | } | |
a306f2dd | 854 | packet_start(SSH_CMSG_AUTH_TIS_RESPONSE); |
0ae4fe1d | 855 | ssh_put_password(response); |
a306f2dd | 856 | memset(response, 0, strlen(response)); |
857 | xfree(response); | |
858 | packet_send(); | |
859 | packet_write_wait(); | |
54a5250f | 860 | type = packet_read(); |
a306f2dd | 861 | if (type == SSH_SMSG_SUCCESS) |
862 | return 1; | |
863 | if (type != SSH_SMSG_FAILURE) | |
864 | packet_disconnect("Protocol error: got %d in response " | |
d464095c | 865 | "to SSH_CMSG_AUTH_TIS_RESPONSE", type); |
a306f2dd | 866 | } |
867 | /* failure */ | |
868 | return 0; | |
869 | } | |
870 | ||
871 | /* | |
872 | * Tries to authenticate with plain passwd authentication. | |
873 | */ | |
396c147e | 874 | static int |
a306f2dd | 875 | try_password_authentication(char *prompt) |
876 | { | |
54a5250f | 877 | int type, i; |
a306f2dd | 878 | char *password; |
879 | ||
880 | debug("Doing password authentication."); | |
881 | if (options.cipher == SSH_CIPHER_NONE) | |
882 | log("WARNING: Encryption is disabled! Password will be transmitted in clear text."); | |
883 | for (i = 0; i < options.number_of_password_prompts; i++) { | |
884 | if (i != 0) | |
885 | error("Permission denied, please try again."); | |
886 | password = read_passphrase(prompt, 0); | |
887 | packet_start(SSH_CMSG_AUTH_PASSWORD); | |
0ae4fe1d | 888 | ssh_put_password(password); |
a306f2dd | 889 | memset(password, 0, strlen(password)); |
890 | xfree(password); | |
891 | packet_send(); | |
892 | packet_write_wait(); | |
893 | ||
54a5250f | 894 | type = packet_read(); |
a306f2dd | 895 | if (type == SSH_SMSG_SUCCESS) |
896 | return 1; | |
897 | if (type != SSH_SMSG_FAILURE) | |
898 | packet_disconnect("Protocol error: got %d in response to passwd auth", type); | |
899 | } | |
900 | /* failure */ | |
901 | return 0; | |
902 | } | |
903 | ||
904 | /* | |
905 | * SSH1 key exchange | |
906 | */ | |
907 | void | |
908 | ssh_kex(char *host, struct sockaddr *hostaddr) | |
909 | { | |
910 | int i; | |
911 | BIGNUM *key; | |
b775c6f2 | 912 | Key *host_key, *server_key; |
a306f2dd | 913 | int bits, rbits; |
914 | int ssh_cipher_default = SSH_CIPHER_3DES; | |
1e3b8b07 | 915 | u_char session_key[SSH_SESSION_KEY_LENGTH]; |
916 | u_char cookie[8]; | |
917 | u_int supported_ciphers; | |
918 | u_int server_flags, client_flags; | |
a306f2dd | 919 | u_int32_t rand = 0; |
920 | ||
921 | debug("Waiting for server public key."); | |
922 | ||
923 | /* Wait for a public key packet from the server. */ | |
54a5250f | 924 | packet_read_expect(SSH_SMSG_PUBLIC_KEY); |
a306f2dd | 925 | |
926 | /* Get cookie from the packet. */ | |
927 | for (i = 0; i < 8; i++) | |
928 | cookie[i] = packet_get_char(); | |
929 | ||
930 | /* Get the public key. */ | |
b775c6f2 | 931 | server_key = key_new(KEY_RSA1); |
932 | bits = packet_get_int(); | |
20b279e6 | 933 | packet_get_bignum(server_key->rsa->e); |
934 | packet_get_bignum(server_key->rsa->n); | |
a306f2dd | 935 | |
b775c6f2 | 936 | rbits = BN_num_bits(server_key->rsa->n); |
a306f2dd | 937 | if (bits != rbits) { |
938 | log("Warning: Server lies about size of server public key: " | |
939 | "actual size is %d bits vs. announced %d.", rbits, bits); | |
940 | log("Warning: This may be due to an old implementation of ssh."); | |
941 | } | |
942 | /* Get the host key. */ | |
b775c6f2 | 943 | host_key = key_new(KEY_RSA1); |
944 | bits = packet_get_int(); | |
20b279e6 | 945 | packet_get_bignum(host_key->rsa->e); |
946 | packet_get_bignum(host_key->rsa->n); | |
a306f2dd | 947 | |
b775c6f2 | 948 | rbits = BN_num_bits(host_key->rsa->n); |
a306f2dd | 949 | if (bits != rbits) { |
950 | log("Warning: Server lies about size of server host key: " | |
951 | "actual size is %d bits vs. announced %d.", rbits, bits); | |
952 | log("Warning: This may be due to an old implementation of ssh."); | |
953 | } | |
954 | ||
955 | /* Get protocol flags. */ | |
956 | server_flags = packet_get_int(); | |
957 | packet_set_protocol_flags(server_flags); | |
958 | ||
959 | supported_ciphers = packet_get_int(); | |
960 | supported_authentications = packet_get_int(); | |
95500969 | 961 | packet_check_eom(); |
a306f2dd | 962 | |
963 | debug("Received server public key (%d bits) and host key (%d bits).", | |
b775c6f2 | 964 | BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n)); |
a306f2dd | 965 | |
b775c6f2 | 966 | if (verify_host_key(host, hostaddr, host_key) == -1) |
01e9ef57 | 967 | fatal("Host key verification failed."); |
a306f2dd | 968 | |
969 | client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN; | |
970 | ||
b775c6f2 | 971 | compute_session_id(session_id, cookie, host_key->rsa->n, server_key->rsa->n); |
a306f2dd | 972 | |
973 | /* Generate a session key. */ | |
974 | arc4random_stir(); | |
975 | ||
976 | /* | |
977 | * Generate an encryption key for the session. The key is a 256 bit | |
978 | * random number, interpreted as a 32-byte key, with the least | |
979 | * significant 8 bits being the first byte of the key. | |
980 | */ | |
981 | for (i = 0; i < 32; i++) { | |
982 | if (i % 4 == 0) | |
983 | rand = arc4random(); | |
984 | session_key[i] = rand & 0xff; | |
985 | rand >>= 8; | |
986 | } | |
987 | ||
988 | /* | |
989 | * According to the protocol spec, the first byte of the session key | |
990 | * is the highest byte of the integer. The session key is xored with | |
991 | * the first 16 bytes of the session id. | |
992 | */ | |
b775c6f2 | 993 | if ((key = BN_new()) == NULL) |
994 | fatal("respond_to_rsa_challenge: BN_new failed"); | |
a306f2dd | 995 | BN_set_word(key, 0); |
996 | for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) { | |
997 | BN_lshift(key, key, 8); | |
998 | if (i < 16) | |
999 | BN_add_word(key, session_key[i] ^ session_id[i]); | |
1000 | else | |
1001 | BN_add_word(key, session_key[i]); | |
1002 | } | |
1003 | ||
1004 | /* | |
1005 | * Encrypt the integer using the public key and host key of the | |
1006 | * server (key with smaller modulus first). | |
1007 | */ | |
b775c6f2 | 1008 | if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) { |
a306f2dd | 1009 | /* Public key has smaller modulus. */ |
b775c6f2 | 1010 | if (BN_num_bits(host_key->rsa->n) < |
1011 | BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) { | |
1012 | fatal("respond_to_rsa_challenge: host_key %d < server_key %d + " | |
184eed6a | 1013 | "SSH_KEY_BITS_RESERVED %d", |
b775c6f2 | 1014 | BN_num_bits(host_key->rsa->n), |
1015 | BN_num_bits(server_key->rsa->n), | |
184eed6a | 1016 | SSH_KEY_BITS_RESERVED); |
a306f2dd | 1017 | } |
b775c6f2 | 1018 | rsa_public_encrypt(key, key, server_key->rsa); |
1019 | rsa_public_encrypt(key, key, host_key->rsa); | |
a306f2dd | 1020 | } else { |
1021 | /* Host key has smaller modulus (or they are equal). */ | |
b775c6f2 | 1022 | if (BN_num_bits(server_key->rsa->n) < |
1023 | BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) { | |
1024 | fatal("respond_to_rsa_challenge: server_key %d < host_key %d + " | |
184eed6a | 1025 | "SSH_KEY_BITS_RESERVED %d", |
b775c6f2 | 1026 | BN_num_bits(server_key->rsa->n), |
1027 | BN_num_bits(host_key->rsa->n), | |
184eed6a | 1028 | SSH_KEY_BITS_RESERVED); |
a306f2dd | 1029 | } |
b775c6f2 | 1030 | rsa_public_encrypt(key, key, host_key->rsa); |
1031 | rsa_public_encrypt(key, key, server_key->rsa); | |
a306f2dd | 1032 | } |
1033 | ||
1034 | /* Destroy the public keys since we no longer need them. */ | |
b775c6f2 | 1035 | key_free(server_key); |
1036 | key_free(host_key); | |
a306f2dd | 1037 | |
c523303b | 1038 | if (options.cipher == SSH_CIPHER_NOT_SET) { |
1039 | if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default)) | |
1040 | options.cipher = ssh_cipher_default; | |
1041 | } else if (options.cipher == SSH_CIPHER_ILLEGAL || | |
1042 | !(cipher_mask_ssh1(1) & (1 << options.cipher))) { | |
d0c832f3 | 1043 | log("No valid SSH1 cipher, using %.100s instead.", |
94ec8c6b | 1044 | cipher_name(ssh_cipher_default)); |
1045 | options.cipher = ssh_cipher_default; | |
a306f2dd | 1046 | } |
1047 | /* Check that the selected cipher is supported. */ | |
1048 | if (!(supported_ciphers & (1 << options.cipher))) | |
1049 | fatal("Selected cipher type %.100s not supported by server.", | |
184eed6a | 1050 | cipher_name(options.cipher)); |
a306f2dd | 1051 | |
1052 | debug("Encryption type: %.100s", cipher_name(options.cipher)); | |
1053 | ||
1054 | /* Send the encrypted session key to the server. */ | |
1055 | packet_start(SSH_CMSG_SESSION_KEY); | |
1056 | packet_put_char(options.cipher); | |
1057 | ||
1058 | /* Send the cookie back to the server. */ | |
1059 | for (i = 0; i < 8; i++) | |
1060 | packet_put_char(cookie[i]); | |
1061 | ||
1062 | /* Send and destroy the encrypted encryption key integer. */ | |
1063 | packet_put_bignum(key); | |
1064 | BN_clear_free(key); | |
1065 | ||
1066 | /* Send protocol flags. */ | |
1067 | packet_put_int(client_flags); | |
1068 | ||
1069 | /* Send the packet now. */ | |
1070 | packet_send(); | |
1071 | packet_write_wait(); | |
1072 | ||
1073 | debug("Sent encrypted session key."); | |
1074 | ||
1075 | /* Set the encryption key. */ | |
1076 | packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher); | |
1077 | ||
1078 | /* We will no longer need the session key here. Destroy any extra copies. */ | |
1079 | memset(session_key, 0, sizeof(session_key)); | |
1080 | ||
1081 | /* | |
1082 | * Expect a success message from the server. Note that this message | |
1083 | * will be received in encrypted form. | |
1084 | */ | |
54a5250f | 1085 | packet_read_expect(SSH_SMSG_SUCCESS); |
a306f2dd | 1086 | |
1087 | debug("Received encrypted confirmation."); | |
1088 | } | |
1089 | ||
1090 | /* | |
1091 | * Authenticate user | |
1092 | */ | |
1093 | void | |
8002af61 | 1094 | ssh_userauth1(const char *local_user, const char *server_user, char *host, |
1095 | Key **keys, int nkeys) | |
a306f2dd | 1096 | { |
ced49be2 | 1097 | #ifdef KRB5 |
1098 | krb5_context context = NULL; | |
1099 | krb5_auth_context auth_context = NULL; | |
1100 | #endif | |
a306f2dd | 1101 | int i, type; |
184eed6a | 1102 | |
a306f2dd | 1103 | if (supported_authentications == 0) |
8002af61 | 1104 | fatal("ssh_userauth1: server supports no auth methods"); |
a306f2dd | 1105 | |
1106 | /* Send the name of the user to log in as on the server. */ | |
1107 | packet_start(SSH_CMSG_USER); | |
449c5ba5 | 1108 | packet_put_cstring(server_user); |
a306f2dd | 1109 | packet_send(); |
1110 | packet_write_wait(); | |
1111 | ||
1112 | /* | |
1113 | * The server should respond with success if no authentication is | |
1114 | * needed (the user has no password). Otherwise the server responds | |
1115 | * with failure. | |
1116 | */ | |
54a5250f | 1117 | type = packet_read(); |
a306f2dd | 1118 | |
1119 | /* check whether the connection was accepted without authentication. */ | |
1120 | if (type == SSH_SMSG_SUCCESS) | |
ced49be2 | 1121 | goto success; |
a306f2dd | 1122 | if (type != SSH_SMSG_FAILURE) |
ced49be2 | 1123 | packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type); |
184eed6a | 1124 | |
ced49be2 | 1125 | #ifdef KRB5 |
1126 | if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) && | |
184eed6a | 1127 | options.kerberos_authentication) { |
ced49be2 | 1128 | debug("Trying Kerberos v5 authentication."); |
184eed6a | 1129 | |
ced49be2 | 1130 | if (try_krb5_authentication(&context, &auth_context)) { |
54a5250f | 1131 | type = packet_read(); |
ced49be2 | 1132 | if (type == SSH_SMSG_SUCCESS) |
1133 | goto success; | |
1134 | if (type != SSH_SMSG_FAILURE) | |
1135 | packet_disconnect("Protocol error: got %d in response to Kerberos v5 auth", type); | |
1136 | } | |
a306f2dd | 1137 | } |
ced49be2 | 1138 | #endif /* KRB5 */ |
184eed6a | 1139 | |
a306f2dd | 1140 | #ifdef KRB4 |
1141 | if ((supported_authentications & (1 << SSH_AUTH_KERBEROS)) && | |
1142 | options.kerberos_authentication) { | |
ced49be2 | 1143 | debug("Trying Kerberos v4 authentication."); |
184eed6a | 1144 | |
ced49be2 | 1145 | if (try_krb4_authentication()) { |
54a5250f | 1146 | type = packet_read(); |
a306f2dd | 1147 | if (type == SSH_SMSG_SUCCESS) |
ced49be2 | 1148 | goto success; |
a306f2dd | 1149 | if (type != SSH_SMSG_FAILURE) |
ced49be2 | 1150 | packet_disconnect("Protocol error: got %d in response to Kerberos v4 auth", type); |
a306f2dd | 1151 | } |
1152 | } | |
1153 | #endif /* KRB4 */ | |
184eed6a | 1154 | |
a306f2dd | 1155 | /* |
1156 | * Use rhosts authentication if running in privileged socket and we | |
1157 | * do not wish to remain anonymous. | |
1158 | */ | |
1159 | if ((supported_authentications & (1 << SSH_AUTH_RHOSTS)) && | |
1160 | options.rhosts_authentication) { | |
1161 | debug("Trying rhosts authentication."); | |
1162 | packet_start(SSH_CMSG_AUTH_RHOSTS); | |
449c5ba5 | 1163 | packet_put_cstring(local_user); |
a306f2dd | 1164 | packet_send(); |
1165 | packet_write_wait(); | |
1166 | ||
1167 | /* The server should respond with success or failure. */ | |
54a5250f | 1168 | type = packet_read(); |
a306f2dd | 1169 | if (type == SSH_SMSG_SUCCESS) |
ced49be2 | 1170 | goto success; |
a306f2dd | 1171 | if (type != SSH_SMSG_FAILURE) |
1172 | packet_disconnect("Protocol error: got %d in response to rhosts auth", | |
1173 | type); | |
1174 | } | |
1175 | /* | |
1176 | * Try .rhosts or /etc/hosts.equiv authentication with RSA host | |
1177 | * authentication. | |
1178 | */ | |
1179 | if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) && | |
8002af61 | 1180 | options.rhosts_rsa_authentication) { |
1181 | for (i = 0; i < nkeys; i++) { | |
eba4bef7 | 1182 | if (keys[i] != NULL && keys[i]->type == KEY_RSA1 && |
8002af61 | 1183 | try_rhosts_rsa_authentication(local_user, keys[i])) |
ced49be2 | 1184 | goto success; |
8002af61 | 1185 | } |
a306f2dd | 1186 | } |
1187 | /* Try RSA authentication if the server supports it. */ | |
1188 | if ((supported_authentications & (1 << SSH_AUTH_RSA)) && | |
1189 | options.rsa_authentication) { | |
1190 | /* | |
1191 | * Try RSA authentication using the authentication agent. The | |
1192 | * agent is tried first because no passphrase is needed for | |
1193 | * it, whereas identity files may require passphrases. | |
1194 | */ | |
1195 | if (try_agent_authentication()) | |
ced49be2 | 1196 | goto success; |
a306f2dd | 1197 | |
1198 | /* Try RSA authentication for each identity. */ | |
1199 | for (i = 0; i < options.num_identity_files; i++) | |
fee56204 | 1200 | if (options.identity_keys[i] != NULL && |
1201 | options.identity_keys[i]->type == KEY_RSA1 && | |
383546c4 | 1202 | try_rsa_authentication(i)) |
ced49be2 | 1203 | goto success; |
a306f2dd | 1204 | } |
d464095c | 1205 | /* Try challenge response authentication if the server supports it. */ |
a306f2dd | 1206 | if ((supported_authentications & (1 << SSH_AUTH_TIS)) && |
5ba55ada | 1207 | options.challenge_response_authentication && !options.batch_mode) { |
1208 | if (try_challenge_response_authentication()) | |
ced49be2 | 1209 | goto success; |
a306f2dd | 1210 | } |
1211 | /* Try password authentication if the server supports it. */ | |
1212 | if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) && | |
1213 | options.password_authentication && !options.batch_mode) { | |
1214 | char prompt[80]; | |
1215 | ||
b3b9ad8e | 1216 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", |
a306f2dd | 1217 | server_user, host); |
1218 | if (try_password_authentication(prompt)) | |
ced49be2 | 1219 | goto success; |
a306f2dd | 1220 | } |
1221 | /* All authentication methods have failed. Exit with an error message. */ | |
1222 | fatal("Permission denied."); | |
1223 | /* NOTREACHED */ | |
ced49be2 | 1224 | |
1225 | success: | |
1226 | #ifdef KRB5 | |
1227 | /* Try Kerberos v5 TGT passing. */ | |
1228 | if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) && | |
1229 | options.kerberos_tgt_passing && context && auth_context) { | |
1230 | if (options.cipher == SSH_CIPHER_NONE) | |
1231 | log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!"); | |
1232 | send_krb5_tgt(context, auth_context); | |
1233 | } | |
1234 | if (auth_context) | |
1235 | krb5_auth_con_free(context, auth_context); | |
1236 | if (context) | |
1237 | krb5_free_context(context); | |
1238 | #endif | |
184eed6a | 1239 | |
ced49be2 | 1240 | #ifdef AFS |
1241 | /* Try Kerberos v4 TGT passing if the server supports it. */ | |
1242 | if ((supported_authentications & (1 << SSH_PASS_KERBEROS_TGT)) && | |
1243 | options.kerberos_tgt_passing) { | |
1244 | if (options.cipher == SSH_CIPHER_NONE) | |
1245 | log("WARNING: Encryption is disabled! Ticket will be transmitted in the clear!"); | |
1246 | send_krb4_tgt(); | |
1247 | } | |
1248 | /* Try AFS token passing if the server supports it. */ | |
1249 | if ((supported_authentications & (1 << SSH_PASS_AFS_TOKEN)) && | |
1250 | options.afs_token_passing && k_hasafs()) { | |
1251 | if (options.cipher == SSH_CIPHER_NONE) | |
1252 | log("WARNING: Encryption is disabled! Token will be transmitted in the clear!"); | |
1253 | send_afs_tokens(); | |
1254 | } | |
1255 | #endif /* AFS */ | |
d9e3d19f | 1256 | |
62bb2c8f | 1257 | return; /* need statement after label */ |
a306f2dd | 1258 | } |