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