]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000 Markus Friedl. All rights reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * 1. Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * 2. Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * | |
13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
23 | */ | |
24 | ||
25 | #include "includes.h" | |
26 | RCSID("$OpenBSD: sshconnect2.c,v 1.76 2001/06/23 15:12:21 itojun Exp $"); | |
27 | ||
28 | #include <openssl/bn.h> | |
29 | #include <openssl/md5.h> | |
30 | #include <openssl/dh.h> | |
31 | #include <openssl/hmac.h> | |
32 | ||
33 | #include "ssh.h" | |
34 | #include "ssh2.h" | |
35 | #include "xmalloc.h" | |
36 | #include "rsa.h" | |
37 | #include "buffer.h" | |
38 | #include "packet.h" | |
39 | #include "uidswap.h" | |
40 | #include "compat.h" | |
41 | #include "bufaux.h" | |
42 | #include "cipher.h" | |
43 | #include "kex.h" | |
44 | #include "myproposal.h" | |
45 | #include "key.h" | |
46 | #include "sshconnect.h" | |
47 | #include "authfile.h" | |
48 | #include "cli.h" | |
49 | #include "dh.h" | |
50 | #include "authfd.h" | |
51 | #include "log.h" | |
52 | #include "readconf.h" | |
53 | #include "readpass.h" | |
54 | #include "match.h" | |
55 | #include "dispatch.h" | |
56 | #include "canohost.h" | |
57 | ||
58 | /* import */ | |
59 | extern char *client_version_string; | |
60 | extern char *server_version_string; | |
61 | extern Options options; | |
62 | ||
63 | /* | |
64 | * SSH2 key exchange | |
65 | */ | |
66 | ||
67 | u_char *session_id2 = NULL; | |
68 | int session_id2_len = 0; | |
69 | ||
70 | char *xxx_host; | |
71 | struct sockaddr *xxx_hostaddr; | |
72 | ||
73 | Kex *xxx_kex = NULL; | |
74 | ||
75 | static int | |
76 | verify_host_key_callback(Key *hostkey) | |
77 | { | |
78 | if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1) | |
79 | fatal("verify_host_key failed"); | |
80 | return 0; | |
81 | } | |
82 | ||
83 | void | |
84 | ssh_kex2(char *host, struct sockaddr *hostaddr) | |
85 | { | |
86 | Kex *kex; | |
87 | ||
88 | xxx_host = host; | |
89 | xxx_hostaddr = hostaddr; | |
90 | ||
91 | if (options.ciphers == (char *)-1) { | |
92 | log("No valid ciphers for protocol version 2 given, using defaults."); | |
93 | options.ciphers = NULL; | |
94 | } | |
95 | if (options.ciphers != NULL) { | |
96 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = | |
97 | myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers; | |
98 | } | |
99 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = | |
100 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]); | |
101 | myproposal[PROPOSAL_ENC_ALGS_STOC] = | |
102 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]); | |
103 | if (options.compression) { | |
104 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = | |
105 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib"; | |
106 | } else { | |
107 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = | |
108 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "none"; | |
109 | } | |
110 | if (options.macs != NULL) { | |
111 | myproposal[PROPOSAL_MAC_ALGS_CTOS] = | |
112 | myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs; | |
113 | } | |
114 | if (options.hostkeyalgorithms != NULL) | |
115 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = | |
116 | options.hostkeyalgorithms; | |
117 | ||
118 | /* start key exchange */ | |
119 | kex = kex_setup(myproposal); | |
120 | kex->client_version_string=client_version_string; | |
121 | kex->server_version_string=server_version_string; | |
122 | kex->verify_host_key=&verify_host_key_callback; | |
123 | ||
124 | xxx_kex = kex; | |
125 | ||
126 | dispatch_run(DISPATCH_BLOCK, &kex->done, kex); | |
127 | ||
128 | session_id2 = kex->session_id; | |
129 | session_id2_len = kex->session_id_len; | |
130 | ||
131 | #ifdef DEBUG_KEXDH | |
132 | /* send 1st encrypted/maced/compressed message */ | |
133 | packet_start(SSH2_MSG_IGNORE); | |
134 | packet_put_cstring("markus"); | |
135 | packet_send(); | |
136 | packet_write_wait(); | |
137 | #endif | |
138 | debug("done: ssh_kex2."); | |
139 | } | |
140 | ||
141 | /* | |
142 | * Authenticate user | |
143 | */ | |
144 | ||
145 | typedef struct Authctxt Authctxt; | |
146 | typedef struct Authmethod Authmethod; | |
147 | ||
148 | typedef int sign_cb_fn( | |
149 | Authctxt *authctxt, Key *key, | |
150 | u_char **sigp, int *lenp, u_char *data, int datalen); | |
151 | ||
152 | struct Authctxt { | |
153 | const char *server_user; | |
154 | const char *local_user; | |
155 | const char *host; | |
156 | const char *service; | |
157 | Authmethod *method; | |
158 | int success; | |
159 | char *authlist; | |
160 | /* pubkey */ | |
161 | Key *last_key; | |
162 | sign_cb_fn *last_key_sign; | |
163 | int last_key_hint; | |
164 | AuthenticationConnection *agent; | |
165 | /* hostbased */ | |
166 | Key **keys; | |
167 | int nkeys; | |
168 | }; | |
169 | struct Authmethod { | |
170 | char *name; /* string to compare against server's list */ | |
171 | int (*userauth)(Authctxt *authctxt); | |
172 | int *enabled; /* flag in option struct that enables method */ | |
173 | int *batch_flag; /* flag in option struct that disables method */ | |
174 | }; | |
175 | ||
176 | void input_userauth_success(int type, int plen, void *ctxt); | |
177 | void input_userauth_failure(int type, int plen, void *ctxt); | |
178 | void input_userauth_banner(int type, int plen, void *ctxt); | |
179 | void input_userauth_error(int type, int plen, void *ctxt); | |
180 | void input_userauth_info_req(int type, int plen, void *ctxt); | |
181 | void input_userauth_pk_ok(int type, int plen, void *ctxt); | |
182 | ||
183 | int userauth_none(Authctxt *authctxt); | |
184 | int userauth_pubkey(Authctxt *authctxt); | |
185 | int userauth_passwd(Authctxt *authctxt); | |
186 | int userauth_kbdint(Authctxt *authctxt); | |
187 | int userauth_hostbased(Authctxt *authctxt); | |
188 | ||
189 | void userauth(Authctxt *authctxt, char *authlist); | |
190 | ||
191 | static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *); | |
192 | static void clear_auth_state(Authctxt *); | |
193 | ||
194 | static Authmethod *authmethod_get(char *authlist); | |
195 | static Authmethod *authmethod_lookup(const char *name); | |
196 | static char *authmethods_get(void); | |
197 | ||
198 | Authmethod authmethods[] = { | |
199 | {"publickey", | |
200 | userauth_pubkey, | |
201 | &options.pubkey_authentication, | |
202 | NULL}, | |
203 | {"hostbased", | |
204 | userauth_hostbased, | |
205 | &options.hostbased_authentication, | |
206 | NULL}, | |
207 | {"password", | |
208 | userauth_passwd, | |
209 | &options.password_authentication, | |
210 | &options.batch_mode}, | |
211 | {"keyboard-interactive", | |
212 | userauth_kbdint, | |
213 | &options.kbd_interactive_authentication, | |
214 | &options.batch_mode}, | |
215 | {"none", | |
216 | userauth_none, | |
217 | NULL, | |
218 | NULL}, | |
219 | {NULL, NULL, NULL, NULL} | |
220 | }; | |
221 | ||
222 | void | |
223 | ssh_userauth2(const char *local_user, const char *server_user, char *host, | |
224 | Key **keys, int nkeys) | |
225 | { | |
226 | Authctxt authctxt; | |
227 | int type; | |
228 | int plen; | |
229 | ||
230 | if (options.challenge_response_authentication) | |
231 | options.kbd_interactive_authentication = 1; | |
232 | ||
233 | debug("send SSH2_MSG_SERVICE_REQUEST"); | |
234 | packet_start(SSH2_MSG_SERVICE_REQUEST); | |
235 | packet_put_cstring("ssh-userauth"); | |
236 | packet_send(); | |
237 | packet_write_wait(); | |
238 | type = packet_read(&plen); | |
239 | if (type != SSH2_MSG_SERVICE_ACCEPT) { | |
240 | fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type); | |
241 | } | |
242 | if (packet_remaining() > 0) { | |
243 | char *reply = packet_get_string(&plen); | |
244 | debug("service_accept: %s", reply); | |
245 | xfree(reply); | |
246 | } else { | |
247 | debug("buggy server: service_accept w/o service"); | |
248 | } | |
249 | packet_done(); | |
250 | debug("got SSH2_MSG_SERVICE_ACCEPT"); | |
251 | ||
252 | if (options.preferred_authentications == NULL) | |
253 | options.preferred_authentications = authmethods_get(); | |
254 | ||
255 | /* setup authentication context */ | |
256 | authctxt.agent = ssh_get_authentication_connection(); | |
257 | authctxt.server_user = server_user; | |
258 | authctxt.local_user = local_user; | |
259 | authctxt.host = host; | |
260 | authctxt.service = "ssh-connection"; /* service name */ | |
261 | authctxt.success = 0; | |
262 | authctxt.method = authmethod_lookup("none"); | |
263 | authctxt.authlist = NULL; | |
264 | authctxt.keys = keys; | |
265 | authctxt.nkeys = nkeys; | |
266 | if (authctxt.method == NULL) | |
267 | fatal("ssh_userauth2: internal error: cannot send userauth none request"); | |
268 | ||
269 | /* initial userauth request */ | |
270 | userauth_none(&authctxt); | |
271 | ||
272 | dispatch_init(&input_userauth_error); | |
273 | dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success); | |
274 | dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure); | |
275 | dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner); | |
276 | dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */ | |
277 | ||
278 | if (authctxt.agent != NULL) | |
279 | ssh_close_authentication_connection(authctxt.agent); | |
280 | ||
281 | debug("ssh-userauth2 successful: method %s", authctxt.method->name); | |
282 | } | |
283 | void | |
284 | userauth(Authctxt *authctxt, char *authlist) | |
285 | { | |
286 | if (authlist == NULL) { | |
287 | authlist = authctxt->authlist; | |
288 | } else { | |
289 | if (authctxt->authlist) | |
290 | xfree(authctxt->authlist); | |
291 | authctxt->authlist = authlist; | |
292 | } | |
293 | for (;;) { | |
294 | Authmethod *method = authmethod_get(authlist); | |
295 | if (method == NULL) | |
296 | fatal("Permission denied (%s).", authlist); | |
297 | authctxt->method = method; | |
298 | if (method->userauth(authctxt) != 0) { | |
299 | debug2("we sent a %s packet, wait for reply", method->name); | |
300 | break; | |
301 | } else { | |
302 | debug2("we did not send a packet, disable method"); | |
303 | method->enabled = NULL; | |
304 | } | |
305 | } | |
306 | } | |
307 | void | |
308 | input_userauth_error(int type, int plen, void *ctxt) | |
309 | { | |
310 | fatal("input_userauth_error: bad message during authentication: " | |
311 | "type %d", type); | |
312 | } | |
313 | void | |
314 | input_userauth_banner(int type, int plen, void *ctxt) | |
315 | { | |
316 | char *msg, *lang; | |
317 | debug3("input_userauth_banner"); | |
318 | msg = packet_get_string(NULL); | |
319 | lang = packet_get_string(NULL); | |
320 | fprintf(stderr, "%s", msg); | |
321 | xfree(msg); | |
322 | xfree(lang); | |
323 | } | |
324 | void | |
325 | input_userauth_success(int type, int plen, void *ctxt) | |
326 | { | |
327 | Authctxt *authctxt = ctxt; | |
328 | if (authctxt == NULL) | |
329 | fatal("input_userauth_success: no authentication context"); | |
330 | if (authctxt->authlist) | |
331 | xfree(authctxt->authlist); | |
332 | clear_auth_state(authctxt); | |
333 | authctxt->success = 1; /* break out */ | |
334 | } | |
335 | void | |
336 | input_userauth_failure(int type, int plen, void *ctxt) | |
337 | { | |
338 | Authctxt *authctxt = ctxt; | |
339 | char *authlist = NULL; | |
340 | int partial; | |
341 | ||
342 | if (authctxt == NULL) | |
343 | fatal("input_userauth_failure: no authentication context"); | |
344 | ||
345 | authlist = packet_get_string(NULL); | |
346 | partial = packet_get_char(); | |
347 | packet_done(); | |
348 | ||
349 | if (partial != 0) | |
350 | log("Authenticated with partial success."); | |
351 | debug("authentications that can continue: %s", authlist); | |
352 | ||
353 | clear_auth_state(authctxt); | |
354 | userauth(authctxt, authlist); | |
355 | } | |
356 | void | |
357 | input_userauth_pk_ok(int type, int plen, void *ctxt) | |
358 | { | |
359 | Authctxt *authctxt = ctxt; | |
360 | Key *key = NULL; | |
361 | Buffer b; | |
362 | int alen, blen, sent = 0; | |
363 | char *pkalg, *pkblob, *fp; | |
364 | ||
365 | if (authctxt == NULL) | |
366 | fatal("input_userauth_pk_ok: no authentication context"); | |
367 | if (datafellows & SSH_BUG_PKOK) { | |
368 | /* this is similar to SSH_BUG_PKAUTH */ | |
369 | debug2("input_userauth_pk_ok: SSH_BUG_PKOK"); | |
370 | pkblob = packet_get_string(&blen); | |
371 | buffer_init(&b); | |
372 | buffer_append(&b, pkblob, blen); | |
373 | pkalg = buffer_get_string(&b, &alen); | |
374 | buffer_free(&b); | |
375 | } else { | |
376 | pkalg = packet_get_string(&alen); | |
377 | pkblob = packet_get_string(&blen); | |
378 | } | |
379 | packet_done(); | |
380 | ||
381 | debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d", | |
382 | pkalg, blen, authctxt->last_key, authctxt->last_key_hint); | |
383 | ||
384 | do { | |
385 | if (authctxt->last_key == NULL || | |
386 | authctxt->last_key_sign == NULL) { | |
387 | debug("no last key or no sign cb"); | |
388 | break; | |
389 | } | |
390 | if (key_type_from_name(pkalg) == KEY_UNSPEC) { | |
391 | debug("unknown pkalg %s", pkalg); | |
392 | break; | |
393 | } | |
394 | if ((key = key_from_blob(pkblob, blen)) == NULL) { | |
395 | debug("no key from blob. pkalg %s", pkalg); | |
396 | break; | |
397 | } | |
398 | fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX); | |
399 | debug2("input_userauth_pk_ok: fp %s", fp); | |
400 | xfree(fp); | |
401 | if (!key_equal(key, authctxt->last_key)) { | |
402 | debug("key != last_key"); | |
403 | break; | |
404 | } | |
405 | sent = sign_and_send_pubkey(authctxt, key, | |
406 | authctxt->last_key_sign); | |
407 | } while(0); | |
408 | ||
409 | if (key != NULL) | |
410 | key_free(key); | |
411 | xfree(pkalg); | |
412 | xfree(pkblob); | |
413 | ||
414 | /* unregister */ | |
415 | clear_auth_state(authctxt); | |
416 | dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL); | |
417 | ||
418 | /* try another method if we did not send a packet*/ | |
419 | if (sent == 0) | |
420 | userauth(authctxt, NULL); | |
421 | ||
422 | } | |
423 | ||
424 | int | |
425 | userauth_none(Authctxt *authctxt) | |
426 | { | |
427 | /* initial userauth request */ | |
428 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
429 | packet_put_cstring(authctxt->server_user); | |
430 | packet_put_cstring(authctxt->service); | |
431 | packet_put_cstring(authctxt->method->name); | |
432 | packet_send(); | |
433 | return 1; | |
434 | } | |
435 | ||
436 | int | |
437 | userauth_passwd(Authctxt *authctxt) | |
438 | { | |
439 | static int attempt = 0; | |
440 | char prompt[80]; | |
441 | char *password; | |
442 | ||
443 | if (attempt++ >= options.number_of_password_prompts) | |
444 | return 0; | |
445 | ||
446 | if(attempt != 1) | |
447 | error("Permission denied, please try again."); | |
448 | ||
449 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", | |
450 | authctxt->server_user, authctxt->host); | |
451 | password = read_passphrase(prompt, 0); | |
452 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
453 | packet_put_cstring(authctxt->server_user); | |
454 | packet_put_cstring(authctxt->service); | |
455 | packet_put_cstring(authctxt->method->name); | |
456 | packet_put_char(0); | |
457 | packet_put_cstring(password); | |
458 | memset(password, 0, strlen(password)); | |
459 | xfree(password); | |
460 | packet_inject_ignore(64); | |
461 | packet_send(); | |
462 | return 1; | |
463 | } | |
464 | ||
465 | void | |
466 | clear_auth_state(Authctxt *authctxt) | |
467 | { | |
468 | /* XXX clear authentication state */ | |
469 | if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) { | |
470 | debug3("clear_auth_state: key_free %p", authctxt->last_key); | |
471 | key_free(authctxt->last_key); | |
472 | } | |
473 | authctxt->last_key = NULL; | |
474 | authctxt->last_key_hint = -2; | |
475 | authctxt->last_key_sign = NULL; | |
476 | } | |
477 | ||
478 | static int | |
479 | sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback) | |
480 | { | |
481 | Buffer b; | |
482 | u_char *blob, *signature; | |
483 | int bloblen, slen; | |
484 | int skip = 0; | |
485 | int ret = -1; | |
486 | int have_sig = 1; | |
487 | ||
488 | debug3("sign_and_send_pubkey"); | |
489 | ||
490 | if (key_to_blob(k, &blob, &bloblen) == 0) { | |
491 | /* we cannot handle this key */ | |
492 | debug3("sign_and_send_pubkey: cannot handle key"); | |
493 | return 0; | |
494 | } | |
495 | /* data to be signed */ | |
496 | buffer_init(&b); | |
497 | if (datafellows & SSH_OLD_SESSIONID) { | |
498 | buffer_append(&b, session_id2, session_id2_len); | |
499 | skip = session_id2_len; | |
500 | } else { | |
501 | buffer_put_string(&b, session_id2, session_id2_len); | |
502 | skip = buffer_len(&b); | |
503 | } | |
504 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
505 | buffer_put_cstring(&b, authctxt->server_user); | |
506 | buffer_put_cstring(&b, | |
507 | datafellows & SSH_BUG_PKSERVICE ? | |
508 | "ssh-userauth" : | |
509 | authctxt->service); | |
510 | if (datafellows & SSH_BUG_PKAUTH) { | |
511 | buffer_put_char(&b, have_sig); | |
512 | } else { | |
513 | buffer_put_cstring(&b, authctxt->method->name); | |
514 | buffer_put_char(&b, have_sig); | |
515 | buffer_put_cstring(&b, key_ssh_name(k)); | |
516 | } | |
517 | buffer_put_string(&b, blob, bloblen); | |
518 | ||
519 | /* generate signature */ | |
520 | ret = (*sign_callback)(authctxt, k, &signature, &slen, | |
521 | buffer_ptr(&b), buffer_len(&b)); | |
522 | if (ret == -1) { | |
523 | xfree(blob); | |
524 | buffer_free(&b); | |
525 | return 0; | |
526 | } | |
527 | #ifdef DEBUG_PK | |
528 | buffer_dump(&b); | |
529 | #endif | |
530 | if (datafellows & SSH_BUG_PKSERVICE) { | |
531 | buffer_clear(&b); | |
532 | buffer_append(&b, session_id2, session_id2_len); | |
533 | skip = session_id2_len; | |
534 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
535 | buffer_put_cstring(&b, authctxt->server_user); | |
536 | buffer_put_cstring(&b, authctxt->service); | |
537 | buffer_put_cstring(&b, authctxt->method->name); | |
538 | buffer_put_char(&b, have_sig); | |
539 | if (!(datafellows & SSH_BUG_PKAUTH)) | |
540 | buffer_put_cstring(&b, key_ssh_name(k)); | |
541 | buffer_put_string(&b, blob, bloblen); | |
542 | } | |
543 | xfree(blob); | |
544 | ||
545 | /* append signature */ | |
546 | buffer_put_string(&b, signature, slen); | |
547 | xfree(signature); | |
548 | ||
549 | /* skip session id and packet type */ | |
550 | if (buffer_len(&b) < skip + 1) | |
551 | fatal("userauth_pubkey: internal error"); | |
552 | buffer_consume(&b, skip + 1); | |
553 | ||
554 | /* put remaining data from buffer into packet */ | |
555 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
556 | packet_put_raw(buffer_ptr(&b), buffer_len(&b)); | |
557 | buffer_free(&b); | |
558 | packet_send(); | |
559 | ||
560 | return 1; | |
561 | } | |
562 | ||
563 | static int | |
564 | send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback, | |
565 | int hint) | |
566 | { | |
567 | u_char *blob; | |
568 | int bloblen, have_sig = 0; | |
569 | ||
570 | debug3("send_pubkey_test"); | |
571 | ||
572 | if (key_to_blob(k, &blob, &bloblen) == 0) { | |
573 | /* we cannot handle this key */ | |
574 | debug3("send_pubkey_test: cannot handle key"); | |
575 | return 0; | |
576 | } | |
577 | /* register callback for USERAUTH_PK_OK message */ | |
578 | authctxt->last_key_sign = sign_callback; | |
579 | authctxt->last_key_hint = hint; | |
580 | authctxt->last_key = k; | |
581 | dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok); | |
582 | ||
583 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
584 | packet_put_cstring(authctxt->server_user); | |
585 | packet_put_cstring(authctxt->service); | |
586 | packet_put_cstring(authctxt->method->name); | |
587 | packet_put_char(have_sig); | |
588 | if (!(datafellows & SSH_BUG_PKAUTH)) | |
589 | packet_put_cstring(key_ssh_name(k)); | |
590 | packet_put_string(blob, bloblen); | |
591 | xfree(blob); | |
592 | packet_send(); | |
593 | return 1; | |
594 | } | |
595 | ||
596 | static Key * | |
597 | load_identity_file(char *filename) | |
598 | { | |
599 | Key *private; | |
600 | char prompt[300], *passphrase; | |
601 | int quit, i; | |
602 | struct stat st; | |
603 | ||
604 | if (stat(filename, &st) < 0) { | |
605 | debug3("no such identity: %s", filename); | |
606 | return NULL; | |
607 | } | |
608 | private = key_load_private_type(KEY_UNSPEC, filename, "", NULL); | |
609 | if (private == NULL) { | |
610 | if (options.batch_mode) | |
611 | return NULL; | |
612 | snprintf(prompt, sizeof prompt, | |
613 | "Enter passphrase for key '%.100s': ", filename); | |
614 | for (i = 0; i < options.number_of_password_prompts; i++) { | |
615 | passphrase = read_passphrase(prompt, 0); | |
616 | if (strcmp(passphrase, "") != 0) { | |
617 | private = key_load_private_type(KEY_UNSPEC, filename, | |
618 | passphrase, NULL); | |
619 | quit = 0; | |
620 | } else { | |
621 | debug2("no passphrase given, try next key"); | |
622 | quit = 1; | |
623 | } | |
624 | memset(passphrase, 0, strlen(passphrase)); | |
625 | xfree(passphrase); | |
626 | if (private != NULL || quit) | |
627 | break; | |
628 | debug2("bad passphrase given, try again..."); | |
629 | } | |
630 | } | |
631 | return private; | |
632 | } | |
633 | ||
634 | static int | |
635 | identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp, | |
636 | u_char *data, int datalen) | |
637 | { | |
638 | Key *private; | |
639 | int idx, ret; | |
640 | ||
641 | idx = authctxt->last_key_hint; | |
642 | if (idx < 0) | |
643 | return -1; | |
644 | private = load_identity_file(options.identity_files[idx]); | |
645 | if (private == NULL) | |
646 | return -1; | |
647 | ret = key_sign(private, sigp, lenp, data, datalen); | |
648 | key_free(private); | |
649 | return ret; | |
650 | } | |
651 | ||
652 | static int | |
653 | agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp, | |
654 | u_char *data, int datalen) | |
655 | { | |
656 | return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen); | |
657 | } | |
658 | ||
659 | static int | |
660 | key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp, | |
661 | u_char *data, int datalen) | |
662 | { | |
663 | return key_sign(key, sigp, lenp, data, datalen); | |
664 | } | |
665 | ||
666 | static int | |
667 | userauth_pubkey_agent(Authctxt *authctxt) | |
668 | { | |
669 | static int called = 0; | |
670 | int ret = 0; | |
671 | char *comment; | |
672 | Key *k; | |
673 | ||
674 | if (called == 0) { | |
675 | if (ssh_get_num_identities(authctxt->agent, 2) == 0) | |
676 | debug2("userauth_pubkey_agent: no keys at all"); | |
677 | called = 1; | |
678 | } | |
679 | k = ssh_get_next_identity(authctxt->agent, &comment, 2); | |
680 | if (k == NULL) { | |
681 | debug2("userauth_pubkey_agent: no more keys"); | |
682 | } else { | |
683 | debug("userauth_pubkey_agent: testing agent key %s", comment); | |
684 | xfree(comment); | |
685 | ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1); | |
686 | if (ret == 0) | |
687 | key_free(k); | |
688 | } | |
689 | if (ret == 0) | |
690 | debug2("userauth_pubkey_agent: no message sent"); | |
691 | return ret; | |
692 | } | |
693 | ||
694 | int | |
695 | userauth_pubkey(Authctxt *authctxt) | |
696 | { | |
697 | static int idx = 0; | |
698 | int sent = 0; | |
699 | Key *key; | |
700 | char *filename; | |
701 | ||
702 | if (authctxt->agent != NULL) { | |
703 | do { | |
704 | sent = userauth_pubkey_agent(authctxt); | |
705 | } while(!sent && authctxt->agent->howmany > 0); | |
706 | } | |
707 | while (!sent && idx < options.num_identity_files) { | |
708 | key = options.identity_keys[idx]; | |
709 | filename = options.identity_files[idx]; | |
710 | if (key == NULL) { | |
711 | debug("try privkey: %s", filename); | |
712 | key = load_identity_file(filename); | |
713 | if (key != NULL) { | |
714 | sent = sign_and_send_pubkey(authctxt, key, | |
715 | key_sign_cb); | |
716 | key_free(key); | |
717 | } | |
718 | } else if (key->type != KEY_RSA1) { | |
719 | debug("try pubkey: %s", filename); | |
720 | sent = send_pubkey_test(authctxt, key, | |
721 | identity_sign_cb, idx); | |
722 | } | |
723 | idx++; | |
724 | } | |
725 | return sent; | |
726 | } | |
727 | ||
728 | /* | |
729 | * Send userauth request message specifying keyboard-interactive method. | |
730 | */ | |
731 | int | |
732 | userauth_kbdint(Authctxt *authctxt) | |
733 | { | |
734 | static int attempt = 0; | |
735 | ||
736 | if (attempt++ >= options.number_of_password_prompts) | |
737 | return 0; | |
738 | ||
739 | debug2("userauth_kbdint"); | |
740 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
741 | packet_put_cstring(authctxt->server_user); | |
742 | packet_put_cstring(authctxt->service); | |
743 | packet_put_cstring(authctxt->method->name); | |
744 | packet_put_cstring(""); /* lang */ | |
745 | packet_put_cstring(options.kbd_interactive_devices ? | |
746 | options.kbd_interactive_devices : ""); | |
747 | packet_send(); | |
748 | ||
749 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req); | |
750 | return 1; | |
751 | } | |
752 | ||
753 | /* | |
754 | * parse INFO_REQUEST, prompt user and send INFO_RESPONSE | |
755 | */ | |
756 | void | |
757 | input_userauth_info_req(int type, int plen, void *ctxt) | |
758 | { | |
759 | Authctxt *authctxt = ctxt; | |
760 | char *name, *inst, *lang, *prompt, *response; | |
761 | u_int num_prompts, i; | |
762 | int echo = 0; | |
763 | ||
764 | debug2("input_userauth_info_req"); | |
765 | ||
766 | if (authctxt == NULL) | |
767 | fatal("input_userauth_info_req: no authentication context"); | |
768 | ||
769 | name = packet_get_string(NULL); | |
770 | inst = packet_get_string(NULL); | |
771 | lang = packet_get_string(NULL); | |
772 | if (strlen(name) > 0) | |
773 | cli_mesg(name); | |
774 | if (strlen(inst) > 0) | |
775 | cli_mesg(inst); | |
776 | xfree(name); | |
777 | xfree(inst); | |
778 | xfree(lang); | |
779 | ||
780 | num_prompts = packet_get_int(); | |
781 | /* | |
782 | * Begin to build info response packet based on prompts requested. | |
783 | * We commit to providing the correct number of responses, so if | |
784 | * further on we run into a problem that prevents this, we have to | |
785 | * be sure and clean this up and send a correct error response. | |
786 | */ | |
787 | packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE); | |
788 | packet_put_int(num_prompts); | |
789 | ||
790 | debug2("input_userauth_info_req: num_prompts %d", num_prompts); | |
791 | for (i = 0; i < num_prompts; i++) { | |
792 | prompt = packet_get_string(NULL); | |
793 | echo = packet_get_char(); | |
794 | ||
795 | response = cli_prompt(prompt, echo); | |
796 | ||
797 | packet_put_cstring(response); | |
798 | memset(response, 0, strlen(response)); | |
799 | xfree(response); | |
800 | xfree(prompt); | |
801 | } | |
802 | packet_done(); /* done with parsing incoming message. */ | |
803 | ||
804 | packet_inject_ignore(64); | |
805 | packet_send(); | |
806 | } | |
807 | ||
808 | /* | |
809 | * this will be move to an external program (ssh-keysign) ASAP. ssh-keysign | |
810 | * will be setuid-root and the sbit can be removed from /usr/bin/ssh. | |
811 | */ | |
812 | int | |
813 | userauth_hostbased(Authctxt *authctxt) | |
814 | { | |
815 | Key *private = NULL; | |
816 | Buffer b; | |
817 | u_char *signature, *blob; | |
818 | char *chost, *pkalg, *p; | |
819 | const char *service; | |
820 | u_int blen, slen; | |
821 | int ok, i, len, found = 0; | |
822 | ||
823 | p = get_local_name(packet_get_connection_in()); | |
824 | if (p == NULL) { | |
825 | error("userauth_hostbased: cannot get local ipaddr/name"); | |
826 | return 0; | |
827 | } | |
828 | len = strlen(p) + 2; | |
829 | chost = xmalloc(len); | |
830 | strlcpy(chost, p, len); | |
831 | strlcat(chost, ".", len); | |
832 | debug2("userauth_hostbased: chost %s", chost); | |
833 | /* check for a useful key */ | |
834 | for (i = 0; i < authctxt->nkeys; i++) { | |
835 | private = authctxt->keys[i]; | |
836 | if (private && private->type != KEY_RSA1) { | |
837 | found = 1; | |
838 | /* we take and free the key */ | |
839 | authctxt->keys[i] = NULL; | |
840 | break; | |
841 | } | |
842 | } | |
843 | if (!found) { | |
844 | xfree(chost); | |
845 | return 0; | |
846 | } | |
847 | if (key_to_blob(private, &blob, &blen) == 0) { | |
848 | key_free(private); | |
849 | xfree(chost); | |
850 | return 0; | |
851 | } | |
852 | service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : | |
853 | authctxt->service; | |
854 | pkalg = xstrdup(key_ssh_name(private)); | |
855 | buffer_init(&b); | |
856 | /* construct data */ | |
857 | buffer_put_string(&b, session_id2, session_id2_len); | |
858 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
859 | buffer_put_cstring(&b, authctxt->server_user); | |
860 | buffer_put_cstring(&b, service); | |
861 | buffer_put_cstring(&b, authctxt->method->name); | |
862 | buffer_put_cstring(&b, pkalg); | |
863 | buffer_put_string(&b, blob, blen); | |
864 | buffer_put_cstring(&b, chost); | |
865 | buffer_put_cstring(&b, authctxt->local_user); | |
866 | #ifdef DEBUG_PK | |
867 | buffer_dump(&b); | |
868 | #endif | |
869 | debug2("xxx: chost %s", chost); | |
870 | ok = key_sign(private, &signature, &slen, buffer_ptr(&b), buffer_len(&b)); | |
871 | key_free(private); | |
872 | buffer_free(&b); | |
873 | if (ok != 0) { | |
874 | error("key_sign failed"); | |
875 | xfree(chost); | |
876 | xfree(pkalg); | |
877 | return 0; | |
878 | } | |
879 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
880 | packet_put_cstring(authctxt->server_user); | |
881 | packet_put_cstring(authctxt->service); | |
882 | packet_put_cstring(authctxt->method->name); | |
883 | packet_put_cstring(pkalg); | |
884 | packet_put_string(blob, blen); | |
885 | packet_put_cstring(chost); | |
886 | packet_put_cstring(authctxt->local_user); | |
887 | packet_put_string(signature, slen); | |
888 | memset(signature, 's', slen); | |
889 | xfree(signature); | |
890 | xfree(chost); | |
891 | xfree(pkalg); | |
892 | ||
893 | packet_send(); | |
894 | return 1; | |
895 | } | |
896 | ||
897 | /* find auth method */ | |
898 | ||
899 | /* | |
900 | * given auth method name, if configurable options permit this method fill | |
901 | * in auth_ident field and return true, otherwise return false. | |
902 | */ | |
903 | static int | |
904 | authmethod_is_enabled(Authmethod *method) | |
905 | { | |
906 | if (method == NULL) | |
907 | return 0; | |
908 | /* return false if options indicate this method is disabled */ | |
909 | if (method->enabled == NULL || *method->enabled == 0) | |
910 | return 0; | |
911 | /* return false if batch mode is enabled but method needs interactive mode */ | |
912 | if (method->batch_flag != NULL && *method->batch_flag != 0) | |
913 | return 0; | |
914 | return 1; | |
915 | } | |
916 | ||
917 | static Authmethod * | |
918 | authmethod_lookup(const char *name) | |
919 | { | |
920 | Authmethod *method = NULL; | |
921 | if (name != NULL) | |
922 | for (method = authmethods; method->name != NULL; method++) | |
923 | if (strcmp(name, method->name) == 0) | |
924 | return method; | |
925 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); | |
926 | return NULL; | |
927 | } | |
928 | ||
929 | /* XXX internal state */ | |
930 | static Authmethod *current = NULL; | |
931 | static char *supported = NULL; | |
932 | static char *preferred = NULL; | |
933 | /* | |
934 | * Given the authentication method list sent by the server, return the | |
935 | * next method we should try. If the server initially sends a nil list, | |
936 | * use a built-in default list. | |
937 | */ | |
938 | static Authmethod * | |
939 | authmethod_get(char *authlist) | |
940 | { | |
941 | ||
942 | char *name = NULL; | |
943 | int next; | |
944 | ||
945 | /* Use a suitable default if we're passed a nil list. */ | |
946 | if (authlist == NULL || strlen(authlist) == 0) | |
947 | authlist = options.preferred_authentications; | |
948 | ||
949 | if (supported == NULL || strcmp(authlist, supported) != 0) { | |
950 | debug3("start over, passed a different list %s", authlist); | |
951 | if (supported != NULL) | |
952 | xfree(supported); | |
953 | supported = xstrdup(authlist); | |
954 | preferred = options.preferred_authentications; | |
955 | debug3("preferred %s", preferred); | |
956 | current = NULL; | |
957 | } else if (current != NULL && authmethod_is_enabled(current)) | |
958 | return current; | |
959 | ||
960 | for (;;) { | |
961 | if ((name = match_list(preferred, supported, &next)) == NULL) { | |
962 | debug("no more auth methods to try"); | |
963 | current = NULL; | |
964 | return NULL; | |
965 | } | |
966 | preferred += next; | |
967 | debug3("authmethod_lookup %s", name); | |
968 | debug3("remaining preferred: %s", preferred); | |
969 | if ((current = authmethod_lookup(name)) != NULL && | |
970 | authmethod_is_enabled(current)) { | |
971 | debug3("authmethod_is_enabled %s", name); | |
972 | debug("next auth method to try is %s", name); | |
973 | return current; | |
974 | } | |
975 | } | |
976 | } | |
977 | ||
978 | ||
979 | #define DELIM "," | |
980 | char * | |
981 | authmethods_get(void) | |
982 | { | |
983 | Authmethod *method = NULL; | |
984 | char buf[1024]; | |
985 | ||
986 | buf[0] = '\0'; | |
987 | for (method = authmethods; method->name != NULL; method++) { | |
988 | if (authmethod_is_enabled(method)) { | |
989 | if (buf[0] != '\0') | |
990 | strlcat(buf, DELIM, sizeof buf); | |
991 | strlcat(buf, method->name, sizeof buf); | |
992 | } | |
993 | } | |
994 | return xstrdup(buf); | |
995 | } |