]>
Commit | Line | Data |
---|---|---|
1 | /* $OpenBSD: sshconnect2.c,v 1.173 2009/10/24 11:13:54 andreas Exp $ */ | |
2 | /* | |
3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. | |
4 | * Copyright (c) 2008 Damien Miller. All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions | |
8 | * are met: | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * | |
15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
25 | */ | |
26 | ||
27 | #include "includes.h" | |
28 | ||
29 | #include <sys/types.h> | |
30 | #include <sys/socket.h> | |
31 | #include <sys/wait.h> | |
32 | #include <sys/stat.h> | |
33 | ||
34 | #include <errno.h> | |
35 | #include <netdb.h> | |
36 | #include <pwd.h> | |
37 | #include <signal.h> | |
38 | #include <stdarg.h> | |
39 | #include <stdio.h> | |
40 | #include <string.h> | |
41 | #include <unistd.h> | |
42 | #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) | |
43 | #include <vis.h> | |
44 | #endif | |
45 | ||
46 | #include "openbsd-compat/sys-queue.h" | |
47 | ||
48 | #include "xmalloc.h" | |
49 | #include "ssh.h" | |
50 | #include "ssh2.h" | |
51 | #include "buffer.h" | |
52 | #include "packet.h" | |
53 | #include "compat.h" | |
54 | #include "cipher.h" | |
55 | #include "key.h" | |
56 | #include "kex.h" | |
57 | #include "myproposal.h" | |
58 | #include "sshconnect.h" | |
59 | #include "authfile.h" | |
60 | #include "dh.h" | |
61 | #include "authfd.h" | |
62 | #include "log.h" | |
63 | #include "readconf.h" | |
64 | #include "misc.h" | |
65 | #include "match.h" | |
66 | #include "dispatch.h" | |
67 | #include "canohost.h" | |
68 | #include "msg.h" | |
69 | #include "pathnames.h" | |
70 | #include "uidswap.h" | |
71 | #include "schnorr.h" | |
72 | #include "jpake.h" | |
73 | ||
74 | #ifdef GSSAPI | |
75 | #include "ssh-gss.h" | |
76 | #endif | |
77 | ||
78 | /* import */ | |
79 | extern char *client_version_string; | |
80 | extern char *server_version_string; | |
81 | extern Options options; | |
82 | ||
83 | /* | |
84 | * SSH2 key exchange | |
85 | */ | |
86 | ||
87 | u_char *session_id2 = NULL; | |
88 | u_int session_id2_len = 0; | |
89 | ||
90 | char *xxx_host; | |
91 | struct sockaddr *xxx_hostaddr; | |
92 | ||
93 | Kex *xxx_kex = NULL; | |
94 | ||
95 | static int | |
96 | verify_host_key_callback(Key *hostkey) | |
97 | { | |
98 | if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1) | |
99 | fatal("Host key verification failed."); | |
100 | return 0; | |
101 | } | |
102 | ||
103 | void | |
104 | ssh_kex2(char *host, struct sockaddr *hostaddr) | |
105 | { | |
106 | Kex *kex; | |
107 | ||
108 | xxx_host = host; | |
109 | xxx_hostaddr = hostaddr; | |
110 | ||
111 | if (options.ciphers == (char *)-1) { | |
112 | logit("No valid ciphers for protocol version 2 given, using defaults."); | |
113 | options.ciphers = NULL; | |
114 | } | |
115 | if (options.ciphers != NULL) { | |
116 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = | |
117 | myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers; | |
118 | } | |
119 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = | |
120 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]); | |
121 | myproposal[PROPOSAL_ENC_ALGS_STOC] = | |
122 | compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]); | |
123 | if (options.compression) { | |
124 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = | |
125 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none"; | |
126 | } else { | |
127 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = | |
128 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib"; | |
129 | } | |
130 | if (options.macs != NULL) { | |
131 | myproposal[PROPOSAL_MAC_ALGS_CTOS] = | |
132 | myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs; | |
133 | } | |
134 | if (options.hostkeyalgorithms != NULL) | |
135 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = | |
136 | options.hostkeyalgorithms; | |
137 | ||
138 | if (options.rekey_limit) | |
139 | packet_set_rekey_limit((u_int32_t)options.rekey_limit); | |
140 | ||
141 | /* start key exchange */ | |
142 | kex = kex_setup(myproposal); | |
143 | kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client; | |
144 | kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client; | |
145 | kex->kex[KEX_DH_GEX_SHA1] = kexgex_client; | |
146 | kex->kex[KEX_DH_GEX_SHA256] = kexgex_client; | |
147 | kex->client_version_string=client_version_string; | |
148 | kex->server_version_string=server_version_string; | |
149 | kex->verify_host_key=&verify_host_key_callback; | |
150 | ||
151 | xxx_kex = kex; | |
152 | ||
153 | dispatch_run(DISPATCH_BLOCK, &kex->done, kex); | |
154 | ||
155 | if (options.use_roaming && !kex->roaming) { | |
156 | debug("Roaming not allowed by server"); | |
157 | options.use_roaming = 0; | |
158 | } | |
159 | ||
160 | session_id2 = kex->session_id; | |
161 | session_id2_len = kex->session_id_len; | |
162 | ||
163 | #ifdef DEBUG_KEXDH | |
164 | /* send 1st encrypted/maced/compressed message */ | |
165 | packet_start(SSH2_MSG_IGNORE); | |
166 | packet_put_cstring("markus"); | |
167 | packet_send(); | |
168 | packet_write_wait(); | |
169 | #endif | |
170 | } | |
171 | ||
172 | /* | |
173 | * Authenticate user | |
174 | */ | |
175 | ||
176 | typedef struct Authctxt Authctxt; | |
177 | typedef struct Authmethod Authmethod; | |
178 | typedef struct identity Identity; | |
179 | typedef struct idlist Idlist; | |
180 | ||
181 | struct identity { | |
182 | TAILQ_ENTRY(identity) next; | |
183 | AuthenticationConnection *ac; /* set if agent supports key */ | |
184 | Key *key; /* public/private key */ | |
185 | char *filename; /* comment for agent-only keys */ | |
186 | int tried; | |
187 | int isprivate; /* key points to the private key */ | |
188 | }; | |
189 | TAILQ_HEAD(idlist, identity); | |
190 | ||
191 | struct Authctxt { | |
192 | const char *server_user; | |
193 | const char *local_user; | |
194 | const char *host; | |
195 | const char *service; | |
196 | Authmethod *method; | |
197 | int success; | |
198 | char *authlist; | |
199 | /* pubkey */ | |
200 | Idlist keys; | |
201 | AuthenticationConnection *agent; | |
202 | /* hostbased */ | |
203 | Sensitive *sensitive; | |
204 | /* kbd-interactive */ | |
205 | int info_req_seen; | |
206 | /* generic */ | |
207 | void *methoddata; | |
208 | }; | |
209 | struct Authmethod { | |
210 | char *name; /* string to compare against server's list */ | |
211 | int (*userauth)(Authctxt *authctxt); | |
212 | void (*cleanup)(Authctxt *authctxt); | |
213 | int *enabled; /* flag in option struct that enables method */ | |
214 | int *batch_flag; /* flag in option struct that disables method */ | |
215 | }; | |
216 | ||
217 | void input_userauth_success(int, u_int32_t, void *); | |
218 | void input_userauth_success_unexpected(int, u_int32_t, void *); | |
219 | void input_userauth_failure(int, u_int32_t, void *); | |
220 | void input_userauth_banner(int, u_int32_t, void *); | |
221 | void input_userauth_error(int, u_int32_t, void *); | |
222 | void input_userauth_info_req(int, u_int32_t, void *); | |
223 | void input_userauth_pk_ok(int, u_int32_t, void *); | |
224 | void input_userauth_passwd_changereq(int, u_int32_t, void *); | |
225 | void input_userauth_jpake_server_step1(int, u_int32_t, void *); | |
226 | void input_userauth_jpake_server_step2(int, u_int32_t, void *); | |
227 | void input_userauth_jpake_server_confirm(int, u_int32_t, void *); | |
228 | ||
229 | int userauth_none(Authctxt *); | |
230 | int userauth_pubkey(Authctxt *); | |
231 | int userauth_passwd(Authctxt *); | |
232 | int userauth_kbdint(Authctxt *); | |
233 | int userauth_hostbased(Authctxt *); | |
234 | int userauth_jpake(Authctxt *); | |
235 | ||
236 | void userauth_jpake_cleanup(Authctxt *); | |
237 | ||
238 | #ifdef GSSAPI | |
239 | int userauth_gssapi(Authctxt *authctxt); | |
240 | void input_gssapi_response(int type, u_int32_t, void *); | |
241 | void input_gssapi_token(int type, u_int32_t, void *); | |
242 | void input_gssapi_hash(int type, u_int32_t, void *); | |
243 | void input_gssapi_error(int, u_int32_t, void *); | |
244 | void input_gssapi_errtok(int, u_int32_t, void *); | |
245 | #endif | |
246 | ||
247 | void userauth(Authctxt *, char *); | |
248 | ||
249 | static int sign_and_send_pubkey(Authctxt *, Identity *); | |
250 | static void pubkey_prepare(Authctxt *); | |
251 | static void pubkey_cleanup(Authctxt *); | |
252 | static Key *load_identity_file(char *); | |
253 | ||
254 | static Authmethod *authmethod_get(char *authlist); | |
255 | static Authmethod *authmethod_lookup(const char *name); | |
256 | static char *authmethods_get(void); | |
257 | ||
258 | Authmethod authmethods[] = { | |
259 | #ifdef GSSAPI | |
260 | {"gssapi-with-mic", | |
261 | userauth_gssapi, | |
262 | NULL, | |
263 | &options.gss_authentication, | |
264 | NULL}, | |
265 | #endif | |
266 | {"hostbased", | |
267 | userauth_hostbased, | |
268 | NULL, | |
269 | &options.hostbased_authentication, | |
270 | NULL}, | |
271 | {"publickey", | |
272 | userauth_pubkey, | |
273 | NULL, | |
274 | &options.pubkey_authentication, | |
275 | NULL}, | |
276 | #ifdef JPAKE | |
277 | {"jpake-01@openssh.com", | |
278 | userauth_jpake, | |
279 | userauth_jpake_cleanup, | |
280 | &options.zero_knowledge_password_authentication, | |
281 | &options.batch_mode}, | |
282 | #endif | |
283 | {"keyboard-interactive", | |
284 | userauth_kbdint, | |
285 | NULL, | |
286 | &options.kbd_interactive_authentication, | |
287 | &options.batch_mode}, | |
288 | {"password", | |
289 | userauth_passwd, | |
290 | NULL, | |
291 | &options.password_authentication, | |
292 | &options.batch_mode}, | |
293 | {"none", | |
294 | userauth_none, | |
295 | NULL, | |
296 | NULL, | |
297 | NULL}, | |
298 | {NULL, NULL, NULL, NULL, NULL} | |
299 | }; | |
300 | ||
301 | void | |
302 | ssh_userauth2(const char *local_user, const char *server_user, char *host, | |
303 | Sensitive *sensitive) | |
304 | { | |
305 | Authctxt authctxt; | |
306 | int type; | |
307 | ||
308 | if (options.challenge_response_authentication) | |
309 | options.kbd_interactive_authentication = 1; | |
310 | ||
311 | packet_start(SSH2_MSG_SERVICE_REQUEST); | |
312 | packet_put_cstring("ssh-userauth"); | |
313 | packet_send(); | |
314 | debug("SSH2_MSG_SERVICE_REQUEST sent"); | |
315 | packet_write_wait(); | |
316 | type = packet_read(); | |
317 | if (type != SSH2_MSG_SERVICE_ACCEPT) | |
318 | fatal("Server denied authentication request: %d", type); | |
319 | if (packet_remaining() > 0) { | |
320 | char *reply = packet_get_string(NULL); | |
321 | debug2("service_accept: %s", reply); | |
322 | xfree(reply); | |
323 | } else { | |
324 | debug2("buggy server: service_accept w/o service"); | |
325 | } | |
326 | packet_check_eom(); | |
327 | debug("SSH2_MSG_SERVICE_ACCEPT received"); | |
328 | ||
329 | if (options.preferred_authentications == NULL) | |
330 | options.preferred_authentications = authmethods_get(); | |
331 | ||
332 | /* setup authentication context */ | |
333 | memset(&authctxt, 0, sizeof(authctxt)); | |
334 | pubkey_prepare(&authctxt); | |
335 | authctxt.server_user = server_user; | |
336 | authctxt.local_user = local_user; | |
337 | authctxt.host = host; | |
338 | authctxt.service = "ssh-connection"; /* service name */ | |
339 | authctxt.success = 0; | |
340 | authctxt.method = authmethod_lookup("none"); | |
341 | authctxt.authlist = NULL; | |
342 | authctxt.methoddata = NULL; | |
343 | authctxt.sensitive = sensitive; | |
344 | authctxt.info_req_seen = 0; | |
345 | if (authctxt.method == NULL) | |
346 | fatal("ssh_userauth2: internal error: cannot send userauth none request"); | |
347 | ||
348 | /* initial userauth request */ | |
349 | userauth_none(&authctxt); | |
350 | ||
351 | dispatch_init(&input_userauth_error); | |
352 | dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success); | |
353 | dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure); | |
354 | dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner); | |
355 | dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */ | |
356 | ||
357 | pubkey_cleanup(&authctxt); | |
358 | dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL); | |
359 | ||
360 | debug("Authentication succeeded (%s).", authctxt.method->name); | |
361 | } | |
362 | ||
363 | void | |
364 | userauth(Authctxt *authctxt, char *authlist) | |
365 | { | |
366 | if (authctxt->method != NULL && authctxt->method->cleanup != NULL) | |
367 | authctxt->method->cleanup(authctxt); | |
368 | ||
369 | if (authctxt->methoddata) { | |
370 | xfree(authctxt->methoddata); | |
371 | authctxt->methoddata = NULL; | |
372 | } | |
373 | if (authlist == NULL) { | |
374 | authlist = authctxt->authlist; | |
375 | } else { | |
376 | if (authctxt->authlist) | |
377 | xfree(authctxt->authlist); | |
378 | authctxt->authlist = authlist; | |
379 | } | |
380 | for (;;) { | |
381 | Authmethod *method = authmethod_get(authlist); | |
382 | if (method == NULL) | |
383 | fatal("Permission denied (%s).", authlist); | |
384 | authctxt->method = method; | |
385 | ||
386 | /* reset the per method handler */ | |
387 | dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN, | |
388 | SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL); | |
389 | ||
390 | /* and try new method */ | |
391 | if (method->userauth(authctxt) != 0) { | |
392 | debug2("we sent a %s packet, wait for reply", method->name); | |
393 | break; | |
394 | } else { | |
395 | debug2("we did not send a packet, disable method"); | |
396 | method->enabled = NULL; | |
397 | } | |
398 | } | |
399 | } | |
400 | ||
401 | /* ARGSUSED */ | |
402 | void | |
403 | input_userauth_error(int type, u_int32_t seq, void *ctxt) | |
404 | { | |
405 | fatal("input_userauth_error: bad message during authentication: " | |
406 | "type %d", type); | |
407 | } | |
408 | ||
409 | /* ARGSUSED */ | |
410 | void | |
411 | input_userauth_banner(int type, u_int32_t seq, void *ctxt) | |
412 | { | |
413 | char *msg, *raw, *lang; | |
414 | u_int len; | |
415 | ||
416 | debug3("input_userauth_banner"); | |
417 | raw = packet_get_string(&len); | |
418 | lang = packet_get_string(NULL); | |
419 | if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) { | |
420 | if (len > 65536) | |
421 | len = 65536; | |
422 | msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */ | |
423 | strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL); | |
424 | fprintf(stderr, "%s", msg); | |
425 | xfree(msg); | |
426 | } | |
427 | xfree(raw); | |
428 | xfree(lang); | |
429 | } | |
430 | ||
431 | /* ARGSUSED */ | |
432 | void | |
433 | input_userauth_success(int type, u_int32_t seq, void *ctxt) | |
434 | { | |
435 | Authctxt *authctxt = ctxt; | |
436 | ||
437 | if (authctxt == NULL) | |
438 | fatal("input_userauth_success: no authentication context"); | |
439 | if (authctxt->authlist) { | |
440 | xfree(authctxt->authlist); | |
441 | authctxt->authlist = NULL; | |
442 | } | |
443 | if (authctxt->method != NULL && authctxt->method->cleanup != NULL) | |
444 | authctxt->method->cleanup(authctxt); | |
445 | if (authctxt->methoddata) { | |
446 | xfree(authctxt->methoddata); | |
447 | authctxt->methoddata = NULL; | |
448 | } | |
449 | authctxt->success = 1; /* break out */ | |
450 | } | |
451 | ||
452 | void | |
453 | input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt) | |
454 | { | |
455 | Authctxt *authctxt = ctxt; | |
456 | ||
457 | if (authctxt == NULL) | |
458 | fatal("%s: no authentication context", __func__); | |
459 | ||
460 | fatal("Unexpected authentication success during %s.", | |
461 | authctxt->method->name); | |
462 | } | |
463 | ||
464 | /* ARGSUSED */ | |
465 | void | |
466 | input_userauth_failure(int type, u_int32_t seq, void *ctxt) | |
467 | { | |
468 | Authctxt *authctxt = ctxt; | |
469 | char *authlist = NULL; | |
470 | int partial; | |
471 | ||
472 | if (authctxt == NULL) | |
473 | fatal("input_userauth_failure: no authentication context"); | |
474 | ||
475 | authlist = packet_get_string(NULL); | |
476 | partial = packet_get_char(); | |
477 | packet_check_eom(); | |
478 | ||
479 | if (partial != 0) | |
480 | logit("Authenticated with partial success."); | |
481 | debug("Authentications that can continue: %s", authlist); | |
482 | ||
483 | userauth(authctxt, authlist); | |
484 | } | |
485 | ||
486 | /* ARGSUSED */ | |
487 | void | |
488 | input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt) | |
489 | { | |
490 | Authctxt *authctxt = ctxt; | |
491 | Key *key = NULL; | |
492 | Identity *id = NULL; | |
493 | Buffer b; | |
494 | int pktype, sent = 0; | |
495 | u_int alen, blen; | |
496 | char *pkalg, *fp; | |
497 | u_char *pkblob; | |
498 | ||
499 | if (authctxt == NULL) | |
500 | fatal("input_userauth_pk_ok: no authentication context"); | |
501 | if (datafellows & SSH_BUG_PKOK) { | |
502 | /* this is similar to SSH_BUG_PKAUTH */ | |
503 | debug2("input_userauth_pk_ok: SSH_BUG_PKOK"); | |
504 | pkblob = packet_get_string(&blen); | |
505 | buffer_init(&b); | |
506 | buffer_append(&b, pkblob, blen); | |
507 | pkalg = buffer_get_string(&b, &alen); | |
508 | buffer_free(&b); | |
509 | } else { | |
510 | pkalg = packet_get_string(&alen); | |
511 | pkblob = packet_get_string(&blen); | |
512 | } | |
513 | packet_check_eom(); | |
514 | ||
515 | debug("Server accepts key: pkalg %s blen %u", pkalg, blen); | |
516 | ||
517 | if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) { | |
518 | debug("unknown pkalg %s", pkalg); | |
519 | goto done; | |
520 | } | |
521 | if ((key = key_from_blob(pkblob, blen)) == NULL) { | |
522 | debug("no key from blob. pkalg %s", pkalg); | |
523 | goto done; | |
524 | } | |
525 | if (key->type != pktype) { | |
526 | error("input_userauth_pk_ok: type mismatch " | |
527 | "for decoded key (received %d, expected %d)", | |
528 | key->type, pktype); | |
529 | goto done; | |
530 | } | |
531 | fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX); | |
532 | debug2("input_userauth_pk_ok: fp %s", fp); | |
533 | xfree(fp); | |
534 | ||
535 | /* | |
536 | * search keys in the reverse order, because last candidate has been | |
537 | * moved to the end of the queue. this also avoids confusion by | |
538 | * duplicate keys | |
539 | */ | |
540 | TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) { | |
541 | if (key_equal(key, id->key)) { | |
542 | sent = sign_and_send_pubkey(authctxt, id); | |
543 | break; | |
544 | } | |
545 | } | |
546 | done: | |
547 | if (key != NULL) | |
548 | key_free(key); | |
549 | xfree(pkalg); | |
550 | xfree(pkblob); | |
551 | ||
552 | /* try another method if we did not send a packet */ | |
553 | if (sent == 0) | |
554 | userauth(authctxt, NULL); | |
555 | } | |
556 | ||
557 | #ifdef GSSAPI | |
558 | int | |
559 | userauth_gssapi(Authctxt *authctxt) | |
560 | { | |
561 | Gssctxt *gssctxt = NULL; | |
562 | static gss_OID_set gss_supported = NULL; | |
563 | static u_int mech = 0; | |
564 | OM_uint32 min; | |
565 | int ok = 0; | |
566 | ||
567 | /* Try one GSSAPI method at a time, rather than sending them all at | |
568 | * once. */ | |
569 | ||
570 | if (gss_supported == NULL) | |
571 | gss_indicate_mechs(&min, &gss_supported); | |
572 | ||
573 | /* Check to see if the mechanism is usable before we offer it */ | |
574 | while (mech < gss_supported->count && !ok) { | |
575 | /* My DER encoding requires length<128 */ | |
576 | if (gss_supported->elements[mech].length < 128 && | |
577 | ssh_gssapi_check_mechanism(&gssctxt, | |
578 | &gss_supported->elements[mech], authctxt->host)) { | |
579 | ok = 1; /* Mechanism works */ | |
580 | } else { | |
581 | mech++; | |
582 | } | |
583 | } | |
584 | ||
585 | if (!ok) | |
586 | return 0; | |
587 | ||
588 | authctxt->methoddata=(void *)gssctxt; | |
589 | ||
590 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
591 | packet_put_cstring(authctxt->server_user); | |
592 | packet_put_cstring(authctxt->service); | |
593 | packet_put_cstring(authctxt->method->name); | |
594 | ||
595 | packet_put_int(1); | |
596 | ||
597 | packet_put_int((gss_supported->elements[mech].length) + 2); | |
598 | packet_put_char(SSH_GSS_OIDTYPE); | |
599 | packet_put_char(gss_supported->elements[mech].length); | |
600 | packet_put_raw(gss_supported->elements[mech].elements, | |
601 | gss_supported->elements[mech].length); | |
602 | ||
603 | packet_send(); | |
604 | ||
605 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response); | |
606 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token); | |
607 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error); | |
608 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok); | |
609 | ||
610 | mech++; /* Move along to next candidate */ | |
611 | ||
612 | return 1; | |
613 | } | |
614 | ||
615 | static OM_uint32 | |
616 | process_gssapi_token(void *ctxt, gss_buffer_t recv_tok) | |
617 | { | |
618 | Authctxt *authctxt = ctxt; | |
619 | Gssctxt *gssctxt = authctxt->methoddata; | |
620 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; | |
621 | gss_buffer_desc mic = GSS_C_EMPTY_BUFFER; | |
622 | gss_buffer_desc gssbuf; | |
623 | OM_uint32 status, ms, flags; | |
624 | Buffer b; | |
625 | ||
626 | status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, | |
627 | recv_tok, &send_tok, &flags); | |
628 | ||
629 | if (send_tok.length > 0) { | |
630 | if (GSS_ERROR(status)) | |
631 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK); | |
632 | else | |
633 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN); | |
634 | ||
635 | packet_put_string(send_tok.value, send_tok.length); | |
636 | packet_send(); | |
637 | gss_release_buffer(&ms, &send_tok); | |
638 | } | |
639 | ||
640 | if (status == GSS_S_COMPLETE) { | |
641 | /* send either complete or MIC, depending on mechanism */ | |
642 | if (!(flags & GSS_C_INTEG_FLAG)) { | |
643 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE); | |
644 | packet_send(); | |
645 | } else { | |
646 | ssh_gssapi_buildmic(&b, authctxt->server_user, | |
647 | authctxt->service, "gssapi-with-mic"); | |
648 | ||
649 | gssbuf.value = buffer_ptr(&b); | |
650 | gssbuf.length = buffer_len(&b); | |
651 | ||
652 | status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic); | |
653 | ||
654 | if (!GSS_ERROR(status)) { | |
655 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC); | |
656 | packet_put_string(mic.value, mic.length); | |
657 | ||
658 | packet_send(); | |
659 | } | |
660 | ||
661 | buffer_free(&b); | |
662 | gss_release_buffer(&ms, &mic); | |
663 | } | |
664 | } | |
665 | ||
666 | return status; | |
667 | } | |
668 | ||
669 | /* ARGSUSED */ | |
670 | void | |
671 | input_gssapi_response(int type, u_int32_t plen, void *ctxt) | |
672 | { | |
673 | Authctxt *authctxt = ctxt; | |
674 | Gssctxt *gssctxt; | |
675 | int oidlen; | |
676 | char *oidv; | |
677 | ||
678 | if (authctxt == NULL) | |
679 | fatal("input_gssapi_response: no authentication context"); | |
680 | gssctxt = authctxt->methoddata; | |
681 | ||
682 | /* Setup our OID */ | |
683 | oidv = packet_get_string(&oidlen); | |
684 | ||
685 | if (oidlen <= 2 || | |
686 | oidv[0] != SSH_GSS_OIDTYPE || | |
687 | oidv[1] != oidlen - 2) { | |
688 | xfree(oidv); | |
689 | debug("Badly encoded mechanism OID received"); | |
690 | userauth(authctxt, NULL); | |
691 | return; | |
692 | } | |
693 | ||
694 | if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2)) | |
695 | fatal("Server returned different OID than expected"); | |
696 | ||
697 | packet_check_eom(); | |
698 | ||
699 | xfree(oidv); | |
700 | ||
701 | if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) { | |
702 | /* Start again with next method on list */ | |
703 | debug("Trying to start again"); | |
704 | userauth(authctxt, NULL); | |
705 | return; | |
706 | } | |
707 | } | |
708 | ||
709 | /* ARGSUSED */ | |
710 | void | |
711 | input_gssapi_token(int type, u_int32_t plen, void *ctxt) | |
712 | { | |
713 | Authctxt *authctxt = ctxt; | |
714 | gss_buffer_desc recv_tok; | |
715 | OM_uint32 status; | |
716 | u_int slen; | |
717 | ||
718 | if (authctxt == NULL) | |
719 | fatal("input_gssapi_response: no authentication context"); | |
720 | ||
721 | recv_tok.value = packet_get_string(&slen); | |
722 | recv_tok.length = slen; /* safe typecast */ | |
723 | ||
724 | packet_check_eom(); | |
725 | ||
726 | status = process_gssapi_token(ctxt, &recv_tok); | |
727 | ||
728 | xfree(recv_tok.value); | |
729 | ||
730 | if (GSS_ERROR(status)) { | |
731 | /* Start again with the next method in the list */ | |
732 | userauth(authctxt, NULL); | |
733 | return; | |
734 | } | |
735 | } | |
736 | ||
737 | /* ARGSUSED */ | |
738 | void | |
739 | input_gssapi_errtok(int type, u_int32_t plen, void *ctxt) | |
740 | { | |
741 | Authctxt *authctxt = ctxt; | |
742 | Gssctxt *gssctxt; | |
743 | gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; | |
744 | gss_buffer_desc recv_tok; | |
745 | OM_uint32 status, ms; | |
746 | u_int len; | |
747 | ||
748 | if (authctxt == NULL) | |
749 | fatal("input_gssapi_response: no authentication context"); | |
750 | gssctxt = authctxt->methoddata; | |
751 | ||
752 | recv_tok.value = packet_get_string(&len); | |
753 | recv_tok.length = len; | |
754 | ||
755 | packet_check_eom(); | |
756 | ||
757 | /* Stick it into GSSAPI and see what it says */ | |
758 | status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, | |
759 | &recv_tok, &send_tok, NULL); | |
760 | ||
761 | xfree(recv_tok.value); | |
762 | gss_release_buffer(&ms, &send_tok); | |
763 | ||
764 | /* Server will be returning a failed packet after this one */ | |
765 | } | |
766 | ||
767 | /* ARGSUSED */ | |
768 | void | |
769 | input_gssapi_error(int type, u_int32_t plen, void *ctxt) | |
770 | { | |
771 | OM_uint32 maj, min; | |
772 | char *msg; | |
773 | char *lang; | |
774 | ||
775 | maj=packet_get_int(); | |
776 | min=packet_get_int(); | |
777 | msg=packet_get_string(NULL); | |
778 | lang=packet_get_string(NULL); | |
779 | ||
780 | packet_check_eom(); | |
781 | ||
782 | debug("Server GSSAPI Error:\n%s", msg); | |
783 | xfree(msg); | |
784 | xfree(lang); | |
785 | } | |
786 | #endif /* GSSAPI */ | |
787 | ||
788 | int | |
789 | userauth_none(Authctxt *authctxt) | |
790 | { | |
791 | /* initial userauth request */ | |
792 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
793 | packet_put_cstring(authctxt->server_user); | |
794 | packet_put_cstring(authctxt->service); | |
795 | packet_put_cstring(authctxt->method->name); | |
796 | packet_send(); | |
797 | return 1; | |
798 | } | |
799 | ||
800 | int | |
801 | userauth_passwd(Authctxt *authctxt) | |
802 | { | |
803 | static int attempt = 0; | |
804 | char prompt[150]; | |
805 | char *password; | |
806 | ||
807 | if (attempt++ >= options.number_of_password_prompts) | |
808 | return 0; | |
809 | ||
810 | if (attempt != 1) | |
811 | error("Permission denied, please try again."); | |
812 | ||
813 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", | |
814 | authctxt->server_user, authctxt->host); | |
815 | password = read_passphrase(prompt, 0); | |
816 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
817 | packet_put_cstring(authctxt->server_user); | |
818 | packet_put_cstring(authctxt->service); | |
819 | packet_put_cstring(authctxt->method->name); | |
820 | packet_put_char(0); | |
821 | packet_put_cstring(password); | |
822 | memset(password, 0, strlen(password)); | |
823 | xfree(password); | |
824 | packet_add_padding(64); | |
825 | packet_send(); | |
826 | ||
827 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, | |
828 | &input_userauth_passwd_changereq); | |
829 | ||
830 | return 1; | |
831 | } | |
832 | ||
833 | /* | |
834 | * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST | |
835 | */ | |
836 | /* ARGSUSED */ | |
837 | void | |
838 | input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt) | |
839 | { | |
840 | Authctxt *authctxt = ctxt; | |
841 | char *info, *lang, *password = NULL, *retype = NULL; | |
842 | char prompt[150]; | |
843 | ||
844 | debug2("input_userauth_passwd_changereq"); | |
845 | ||
846 | if (authctxt == NULL) | |
847 | fatal("input_userauth_passwd_changereq: " | |
848 | "no authentication context"); | |
849 | ||
850 | info = packet_get_string(NULL); | |
851 | lang = packet_get_string(NULL); | |
852 | if (strlen(info) > 0) | |
853 | logit("%s", info); | |
854 | xfree(info); | |
855 | xfree(lang); | |
856 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
857 | packet_put_cstring(authctxt->server_user); | |
858 | packet_put_cstring(authctxt->service); | |
859 | packet_put_cstring(authctxt->method->name); | |
860 | packet_put_char(1); /* additional info */ | |
861 | snprintf(prompt, sizeof(prompt), | |
862 | "Enter %.30s@%.128s's old password: ", | |
863 | authctxt->server_user, authctxt->host); | |
864 | password = read_passphrase(prompt, 0); | |
865 | packet_put_cstring(password); | |
866 | memset(password, 0, strlen(password)); | |
867 | xfree(password); | |
868 | password = NULL; | |
869 | while (password == NULL) { | |
870 | snprintf(prompt, sizeof(prompt), | |
871 | "Enter %.30s@%.128s's new password: ", | |
872 | authctxt->server_user, authctxt->host); | |
873 | password = read_passphrase(prompt, RP_ALLOW_EOF); | |
874 | if (password == NULL) { | |
875 | /* bail out */ | |
876 | return; | |
877 | } | |
878 | snprintf(prompt, sizeof(prompt), | |
879 | "Retype %.30s@%.128s's new password: ", | |
880 | authctxt->server_user, authctxt->host); | |
881 | retype = read_passphrase(prompt, 0); | |
882 | if (strcmp(password, retype) != 0) { | |
883 | memset(password, 0, strlen(password)); | |
884 | xfree(password); | |
885 | logit("Mismatch; try again, EOF to quit."); | |
886 | password = NULL; | |
887 | } | |
888 | memset(retype, 0, strlen(retype)); | |
889 | xfree(retype); | |
890 | } | |
891 | packet_put_cstring(password); | |
892 | memset(password, 0, strlen(password)); | |
893 | xfree(password); | |
894 | packet_add_padding(64); | |
895 | packet_send(); | |
896 | ||
897 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, | |
898 | &input_userauth_passwd_changereq); | |
899 | } | |
900 | ||
901 | #ifdef JPAKE | |
902 | static char * | |
903 | pw_encrypt(const char *password, const char *crypt_scheme, const char *salt) | |
904 | { | |
905 | /* OpenBSD crypt(3) handles all of these */ | |
906 | if (strcmp(crypt_scheme, "crypt") == 0 || | |
907 | strcmp(crypt_scheme, "bcrypt") == 0 || | |
908 | strcmp(crypt_scheme, "md5crypt") == 0 || | |
909 | strcmp(crypt_scheme, "crypt-extended") == 0) | |
910 | return xstrdup(crypt(password, salt)); | |
911 | error("%s: unsupported password encryption scheme \"%.100s\"", | |
912 | __func__, crypt_scheme); | |
913 | return NULL; | |
914 | } | |
915 | ||
916 | static BIGNUM * | |
917 | jpake_password_to_secret(Authctxt *authctxt, const char *crypt_scheme, | |
918 | const char *salt) | |
919 | { | |
920 | char prompt[256], *password, *crypted; | |
921 | u_char *secret; | |
922 | u_int secret_len; | |
923 | BIGNUM *ret; | |
924 | ||
925 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password (JPAKE): ", | |
926 | authctxt->server_user, authctxt->host); | |
927 | password = read_passphrase(prompt, 0); | |
928 | ||
929 | if ((crypted = pw_encrypt(password, crypt_scheme, salt)) == NULL) { | |
930 | logit("Disabling %s authentication", authctxt->method->name); | |
931 | authctxt->method->enabled = NULL; | |
932 | /* Continue with an empty password to fail gracefully */ | |
933 | crypted = xstrdup(""); | |
934 | } | |
935 | ||
936 | #ifdef JPAKE_DEBUG | |
937 | debug3("%s: salt = %s", __func__, salt); | |
938 | debug3("%s: scheme = %s", __func__, crypt_scheme); | |
939 | debug3("%s: crypted = %s", __func__, crypted); | |
940 | #endif | |
941 | ||
942 | if (hash_buffer(crypted, strlen(crypted), EVP_sha256(), | |
943 | &secret, &secret_len) != 0) | |
944 | fatal("%s: hash_buffer", __func__); | |
945 | ||
946 | bzero(password, strlen(password)); | |
947 | bzero(crypted, strlen(crypted)); | |
948 | xfree(password); | |
949 | xfree(crypted); | |
950 | ||
951 | if ((ret = BN_bin2bn(secret, secret_len, NULL)) == NULL) | |
952 | fatal("%s: BN_bin2bn (secret)", __func__); | |
953 | bzero(secret, secret_len); | |
954 | xfree(secret); | |
955 | ||
956 | return ret; | |
957 | } | |
958 | ||
959 | /* ARGSUSED */ | |
960 | void | |
961 | input_userauth_jpake_server_step1(int type, u_int32_t seq, void *ctxt) | |
962 | { | |
963 | Authctxt *authctxt = ctxt; | |
964 | struct jpake_ctx *pctx = authctxt->methoddata; | |
965 | u_char *x3_proof, *x4_proof, *x2_s_proof; | |
966 | u_int x3_proof_len, x4_proof_len, x2_s_proof_len; | |
967 | char *crypt_scheme, *salt; | |
968 | ||
969 | /* Disable this message */ | |
970 | dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, NULL); | |
971 | ||
972 | if ((pctx->g_x3 = BN_new()) == NULL || | |
973 | (pctx->g_x4 = BN_new()) == NULL) | |
974 | fatal("%s: BN_new", __func__); | |
975 | ||
976 | /* Fetch step 1 values */ | |
977 | crypt_scheme = packet_get_string(NULL); | |
978 | salt = packet_get_string(NULL); | |
979 | pctx->server_id = packet_get_string(&pctx->server_id_len); | |
980 | packet_get_bignum2(pctx->g_x3); | |
981 | packet_get_bignum2(pctx->g_x4); | |
982 | x3_proof = packet_get_string(&x3_proof_len); | |
983 | x4_proof = packet_get_string(&x4_proof_len); | |
984 | packet_check_eom(); | |
985 | ||
986 | JPAKE_DEBUG_CTX((pctx, "step 1 received in %s", __func__)); | |
987 | ||
988 | /* Obtain password and derive secret */ | |
989 | pctx->s = jpake_password_to_secret(authctxt, crypt_scheme, salt); | |
990 | bzero(crypt_scheme, strlen(crypt_scheme)); | |
991 | bzero(salt, strlen(salt)); | |
992 | xfree(crypt_scheme); | |
993 | xfree(salt); | |
994 | JPAKE_DEBUG_BN((pctx->s, "%s: s = ", __func__)); | |
995 | ||
996 | /* Calculate step 2 values */ | |
997 | jpake_step2(pctx->grp, pctx->s, pctx->g_x1, | |
998 | pctx->g_x3, pctx->g_x4, pctx->x2, | |
999 | pctx->server_id, pctx->server_id_len, | |
1000 | pctx->client_id, pctx->client_id_len, | |
1001 | x3_proof, x3_proof_len, | |
1002 | x4_proof, x4_proof_len, | |
1003 | &pctx->a, | |
1004 | &x2_s_proof, &x2_s_proof_len); | |
1005 | ||
1006 | bzero(x3_proof, x3_proof_len); | |
1007 | bzero(x4_proof, x4_proof_len); | |
1008 | xfree(x3_proof); | |
1009 | xfree(x4_proof); | |
1010 | ||
1011 | JPAKE_DEBUG_CTX((pctx, "step 2 sending in %s", __func__)); | |
1012 | ||
1013 | /* Send values for step 2 */ | |
1014 | packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2); | |
1015 | packet_put_bignum2(pctx->a); | |
1016 | packet_put_string(x2_s_proof, x2_s_proof_len); | |
1017 | packet_send(); | |
1018 | ||
1019 | bzero(x2_s_proof, x2_s_proof_len); | |
1020 | xfree(x2_s_proof); | |
1021 | ||
1022 | /* Expect step 2 packet from peer */ | |
1023 | dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, | |
1024 | input_userauth_jpake_server_step2); | |
1025 | } | |
1026 | ||
1027 | /* ARGSUSED */ | |
1028 | void | |
1029 | input_userauth_jpake_server_step2(int type, u_int32_t seq, void *ctxt) | |
1030 | { | |
1031 | Authctxt *authctxt = ctxt; | |
1032 | struct jpake_ctx *pctx = authctxt->methoddata; | |
1033 | u_char *x4_s_proof; | |
1034 | u_int x4_s_proof_len; | |
1035 | ||
1036 | /* Disable this message */ | |
1037 | dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, NULL); | |
1038 | ||
1039 | if ((pctx->b = BN_new()) == NULL) | |
1040 | fatal("%s: BN_new", __func__); | |
1041 | ||
1042 | /* Fetch step 2 values */ | |
1043 | packet_get_bignum2(pctx->b); | |
1044 | x4_s_proof = packet_get_string(&x4_s_proof_len); | |
1045 | packet_check_eom(); | |
1046 | ||
1047 | JPAKE_DEBUG_CTX((pctx, "step 2 received in %s", __func__)); | |
1048 | ||
1049 | /* Derive shared key and calculate confirmation hash */ | |
1050 | jpake_key_confirm(pctx->grp, pctx->s, pctx->b, | |
1051 | pctx->x2, pctx->g_x1, pctx->g_x2, pctx->g_x3, pctx->g_x4, | |
1052 | pctx->client_id, pctx->client_id_len, | |
1053 | pctx->server_id, pctx->server_id_len, | |
1054 | session_id2, session_id2_len, | |
1055 | x4_s_proof, x4_s_proof_len, | |
1056 | &pctx->k, | |
1057 | &pctx->h_k_cid_sessid, &pctx->h_k_cid_sessid_len); | |
1058 | ||
1059 | bzero(x4_s_proof, x4_s_proof_len); | |
1060 | xfree(x4_s_proof); | |
1061 | ||
1062 | JPAKE_DEBUG_CTX((pctx, "confirm sending in %s", __func__)); | |
1063 | ||
1064 | /* Send key confirmation proof */ | |
1065 | packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM); | |
1066 | packet_put_string(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len); | |
1067 | packet_send(); | |
1068 | ||
1069 | /* Expect confirmation from peer */ | |
1070 | dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, | |
1071 | input_userauth_jpake_server_confirm); | |
1072 | } | |
1073 | ||
1074 | /* ARGSUSED */ | |
1075 | void | |
1076 | input_userauth_jpake_server_confirm(int type, u_int32_t seq, void *ctxt) | |
1077 | { | |
1078 | Authctxt *authctxt = ctxt; | |
1079 | struct jpake_ctx *pctx = authctxt->methoddata; | |
1080 | ||
1081 | /* Disable this message */ | |
1082 | dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, NULL); | |
1083 | ||
1084 | pctx->h_k_sid_sessid = packet_get_string(&pctx->h_k_sid_sessid_len); | |
1085 | packet_check_eom(); | |
1086 | ||
1087 | JPAKE_DEBUG_CTX((pctx, "confirm received in %s", __func__)); | |
1088 | ||
1089 | /* Verify expected confirmation hash */ | |
1090 | if (jpake_check_confirm(pctx->k, | |
1091 | pctx->server_id, pctx->server_id_len, | |
1092 | session_id2, session_id2_len, | |
1093 | pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len) == 1) | |
1094 | debug("%s: %s success", __func__, authctxt->method->name); | |
1095 | else { | |
1096 | debug("%s: confirmation mismatch", __func__); | |
1097 | /* XXX stash this so if auth succeeds then we can warn/kill */ | |
1098 | } | |
1099 | ||
1100 | userauth_jpake_cleanup(authctxt); | |
1101 | } | |
1102 | #endif /* JPAKE */ | |
1103 | ||
1104 | static int | |
1105 | identity_sign(Identity *id, u_char **sigp, u_int *lenp, | |
1106 | u_char *data, u_int datalen) | |
1107 | { | |
1108 | Key *prv; | |
1109 | int ret; | |
1110 | ||
1111 | /* the agent supports this key */ | |
1112 | if (id->ac) | |
1113 | return (ssh_agent_sign(id->ac, id->key, sigp, lenp, | |
1114 | data, datalen)); | |
1115 | /* | |
1116 | * we have already loaded the private key or | |
1117 | * the private key is stored in external hardware | |
1118 | */ | |
1119 | if (id->isprivate || (id->key->flags & KEY_FLAG_EXT)) | |
1120 | return (key_sign(id->key, sigp, lenp, data, datalen)); | |
1121 | /* load the private key from the file */ | |
1122 | if ((prv = load_identity_file(id->filename)) == NULL) | |
1123 | return (-1); | |
1124 | ret = key_sign(prv, sigp, lenp, data, datalen); | |
1125 | key_free(prv); | |
1126 | return (ret); | |
1127 | } | |
1128 | ||
1129 | static int | |
1130 | sign_and_send_pubkey(Authctxt *authctxt, Identity *id) | |
1131 | { | |
1132 | Buffer b; | |
1133 | u_char *blob, *signature; | |
1134 | u_int bloblen, slen; | |
1135 | u_int skip = 0; | |
1136 | int ret = -1; | |
1137 | int have_sig = 1; | |
1138 | ||
1139 | debug3("sign_and_send_pubkey"); | |
1140 | ||
1141 | if (key_to_blob(id->key, &blob, &bloblen) == 0) { | |
1142 | /* we cannot handle this key */ | |
1143 | debug3("sign_and_send_pubkey: cannot handle key"); | |
1144 | return 0; | |
1145 | } | |
1146 | /* data to be signed */ | |
1147 | buffer_init(&b); | |
1148 | if (datafellows & SSH_OLD_SESSIONID) { | |
1149 | buffer_append(&b, session_id2, session_id2_len); | |
1150 | skip = session_id2_len; | |
1151 | } else { | |
1152 | buffer_put_string(&b, session_id2, session_id2_len); | |
1153 | skip = buffer_len(&b); | |
1154 | } | |
1155 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
1156 | buffer_put_cstring(&b, authctxt->server_user); | |
1157 | buffer_put_cstring(&b, | |
1158 | datafellows & SSH_BUG_PKSERVICE ? | |
1159 | "ssh-userauth" : | |
1160 | authctxt->service); | |
1161 | if (datafellows & SSH_BUG_PKAUTH) { | |
1162 | buffer_put_char(&b, have_sig); | |
1163 | } else { | |
1164 | buffer_put_cstring(&b, authctxt->method->name); | |
1165 | buffer_put_char(&b, have_sig); | |
1166 | buffer_put_cstring(&b, key_ssh_name(id->key)); | |
1167 | } | |
1168 | buffer_put_string(&b, blob, bloblen); | |
1169 | ||
1170 | /* generate signature */ | |
1171 | ret = identity_sign(id, &signature, &slen, | |
1172 | buffer_ptr(&b), buffer_len(&b)); | |
1173 | if (ret == -1) { | |
1174 | xfree(blob); | |
1175 | buffer_free(&b); | |
1176 | return 0; | |
1177 | } | |
1178 | #ifdef DEBUG_PK | |
1179 | buffer_dump(&b); | |
1180 | #endif | |
1181 | if (datafellows & SSH_BUG_PKSERVICE) { | |
1182 | buffer_clear(&b); | |
1183 | buffer_append(&b, session_id2, session_id2_len); | |
1184 | skip = session_id2_len; | |
1185 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
1186 | buffer_put_cstring(&b, authctxt->server_user); | |
1187 | buffer_put_cstring(&b, authctxt->service); | |
1188 | buffer_put_cstring(&b, authctxt->method->name); | |
1189 | buffer_put_char(&b, have_sig); | |
1190 | if (!(datafellows & SSH_BUG_PKAUTH)) | |
1191 | buffer_put_cstring(&b, key_ssh_name(id->key)); | |
1192 | buffer_put_string(&b, blob, bloblen); | |
1193 | } | |
1194 | xfree(blob); | |
1195 | ||
1196 | /* append signature */ | |
1197 | buffer_put_string(&b, signature, slen); | |
1198 | xfree(signature); | |
1199 | ||
1200 | /* skip session id and packet type */ | |
1201 | if (buffer_len(&b) < skip + 1) | |
1202 | fatal("userauth_pubkey: internal error"); | |
1203 | buffer_consume(&b, skip + 1); | |
1204 | ||
1205 | /* put remaining data from buffer into packet */ | |
1206 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
1207 | packet_put_raw(buffer_ptr(&b), buffer_len(&b)); | |
1208 | buffer_free(&b); | |
1209 | packet_send(); | |
1210 | ||
1211 | return 1; | |
1212 | } | |
1213 | ||
1214 | static int | |
1215 | send_pubkey_test(Authctxt *authctxt, Identity *id) | |
1216 | { | |
1217 | u_char *blob; | |
1218 | u_int bloblen, have_sig = 0; | |
1219 | ||
1220 | debug3("send_pubkey_test"); | |
1221 | ||
1222 | if (key_to_blob(id->key, &blob, &bloblen) == 0) { | |
1223 | /* we cannot handle this key */ | |
1224 | debug3("send_pubkey_test: cannot handle key"); | |
1225 | return 0; | |
1226 | } | |
1227 | /* register callback for USERAUTH_PK_OK message */ | |
1228 | dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok); | |
1229 | ||
1230 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
1231 | packet_put_cstring(authctxt->server_user); | |
1232 | packet_put_cstring(authctxt->service); | |
1233 | packet_put_cstring(authctxt->method->name); | |
1234 | packet_put_char(have_sig); | |
1235 | if (!(datafellows & SSH_BUG_PKAUTH)) | |
1236 | packet_put_cstring(key_ssh_name(id->key)); | |
1237 | packet_put_string(blob, bloblen); | |
1238 | xfree(blob); | |
1239 | packet_send(); | |
1240 | return 1; | |
1241 | } | |
1242 | ||
1243 | static Key * | |
1244 | load_identity_file(char *filename) | |
1245 | { | |
1246 | Key *private; | |
1247 | char prompt[300], *passphrase; | |
1248 | int perm_ok, quit, i; | |
1249 | struct stat st; | |
1250 | ||
1251 | if (stat(filename, &st) < 0) { | |
1252 | debug3("no such identity: %s", filename); | |
1253 | return NULL; | |
1254 | } | |
1255 | private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok); | |
1256 | if (!perm_ok) | |
1257 | return NULL; | |
1258 | if (private == NULL) { | |
1259 | if (options.batch_mode) | |
1260 | return NULL; | |
1261 | snprintf(prompt, sizeof prompt, | |
1262 | "Enter passphrase for key '%.100s': ", filename); | |
1263 | for (i = 0; i < options.number_of_password_prompts; i++) { | |
1264 | passphrase = read_passphrase(prompt, 0); | |
1265 | if (strcmp(passphrase, "") != 0) { | |
1266 | private = key_load_private_type(KEY_UNSPEC, | |
1267 | filename, passphrase, NULL, NULL); | |
1268 | quit = 0; | |
1269 | } else { | |
1270 | debug2("no passphrase given, try next key"); | |
1271 | quit = 1; | |
1272 | } | |
1273 | memset(passphrase, 0, strlen(passphrase)); | |
1274 | xfree(passphrase); | |
1275 | if (private != NULL || quit) | |
1276 | break; | |
1277 | debug2("bad passphrase given, try again..."); | |
1278 | } | |
1279 | } | |
1280 | return private; | |
1281 | } | |
1282 | ||
1283 | /* | |
1284 | * try keys in the following order: | |
1285 | * 1. agent keys that are found in the config file | |
1286 | * 2. other agent keys | |
1287 | * 3. keys that are only listed in the config file | |
1288 | */ | |
1289 | static void | |
1290 | pubkey_prepare(Authctxt *authctxt) | |
1291 | { | |
1292 | Identity *id; | |
1293 | Idlist agent, files, *preferred; | |
1294 | Key *key; | |
1295 | AuthenticationConnection *ac; | |
1296 | char *comment; | |
1297 | int i, found; | |
1298 | ||
1299 | TAILQ_INIT(&agent); /* keys from the agent */ | |
1300 | TAILQ_INIT(&files); /* keys from the config file */ | |
1301 | preferred = &authctxt->keys; | |
1302 | TAILQ_INIT(preferred); /* preferred order of keys */ | |
1303 | ||
1304 | /* list of keys stored in the filesystem */ | |
1305 | for (i = 0; i < options.num_identity_files; i++) { | |
1306 | key = options.identity_keys[i]; | |
1307 | if (key && key->type == KEY_RSA1) | |
1308 | continue; | |
1309 | options.identity_keys[i] = NULL; | |
1310 | id = xcalloc(1, sizeof(*id)); | |
1311 | id->key = key; | |
1312 | id->filename = xstrdup(options.identity_files[i]); | |
1313 | TAILQ_INSERT_TAIL(&files, id, next); | |
1314 | } | |
1315 | /* list of keys supported by the agent */ | |
1316 | if ((ac = ssh_get_authentication_connection())) { | |
1317 | for (key = ssh_get_first_identity(ac, &comment, 2); | |
1318 | key != NULL; | |
1319 | key = ssh_get_next_identity(ac, &comment, 2)) { | |
1320 | found = 0; | |
1321 | TAILQ_FOREACH(id, &files, next) { | |
1322 | /* agent keys from the config file are preferred */ | |
1323 | if (key_equal(key, id->key)) { | |
1324 | key_free(key); | |
1325 | xfree(comment); | |
1326 | TAILQ_REMOVE(&files, id, next); | |
1327 | TAILQ_INSERT_TAIL(preferred, id, next); | |
1328 | id->ac = ac; | |
1329 | found = 1; | |
1330 | break; | |
1331 | } | |
1332 | } | |
1333 | if (!found && !options.identities_only) { | |
1334 | id = xcalloc(1, sizeof(*id)); | |
1335 | id->key = key; | |
1336 | id->filename = comment; | |
1337 | id->ac = ac; | |
1338 | TAILQ_INSERT_TAIL(&agent, id, next); | |
1339 | } | |
1340 | } | |
1341 | /* append remaining agent keys */ | |
1342 | for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) { | |
1343 | TAILQ_REMOVE(&agent, id, next); | |
1344 | TAILQ_INSERT_TAIL(preferred, id, next); | |
1345 | } | |
1346 | authctxt->agent = ac; | |
1347 | } | |
1348 | /* append remaining keys from the config file */ | |
1349 | for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) { | |
1350 | TAILQ_REMOVE(&files, id, next); | |
1351 | TAILQ_INSERT_TAIL(preferred, id, next); | |
1352 | } | |
1353 | TAILQ_FOREACH(id, preferred, next) { | |
1354 | debug2("key: %s (%p)", id->filename, id->key); | |
1355 | } | |
1356 | } | |
1357 | ||
1358 | static void | |
1359 | pubkey_cleanup(Authctxt *authctxt) | |
1360 | { | |
1361 | Identity *id; | |
1362 | ||
1363 | if (authctxt->agent != NULL) | |
1364 | ssh_close_authentication_connection(authctxt->agent); | |
1365 | for (id = TAILQ_FIRST(&authctxt->keys); id; | |
1366 | id = TAILQ_FIRST(&authctxt->keys)) { | |
1367 | TAILQ_REMOVE(&authctxt->keys, id, next); | |
1368 | if (id->key) | |
1369 | key_free(id->key); | |
1370 | if (id->filename) | |
1371 | xfree(id->filename); | |
1372 | xfree(id); | |
1373 | } | |
1374 | } | |
1375 | ||
1376 | int | |
1377 | userauth_pubkey(Authctxt *authctxt) | |
1378 | { | |
1379 | Identity *id; | |
1380 | int sent = 0; | |
1381 | ||
1382 | while ((id = TAILQ_FIRST(&authctxt->keys))) { | |
1383 | if (id->tried++) | |
1384 | return (0); | |
1385 | /* move key to the end of the queue */ | |
1386 | TAILQ_REMOVE(&authctxt->keys, id, next); | |
1387 | TAILQ_INSERT_TAIL(&authctxt->keys, id, next); | |
1388 | /* | |
1389 | * send a test message if we have the public key. for | |
1390 | * encrypted keys we cannot do this and have to load the | |
1391 | * private key instead | |
1392 | */ | |
1393 | if (id->key && id->key->type != KEY_RSA1) { | |
1394 | debug("Offering public key: %s", id->filename); | |
1395 | sent = send_pubkey_test(authctxt, id); | |
1396 | } else if (id->key == NULL) { | |
1397 | debug("Trying private key: %s", id->filename); | |
1398 | id->key = load_identity_file(id->filename); | |
1399 | if (id->key != NULL) { | |
1400 | id->isprivate = 1; | |
1401 | sent = sign_and_send_pubkey(authctxt, id); | |
1402 | key_free(id->key); | |
1403 | id->key = NULL; | |
1404 | } | |
1405 | } | |
1406 | if (sent) | |
1407 | return (sent); | |
1408 | } | |
1409 | return (0); | |
1410 | } | |
1411 | ||
1412 | /* | |
1413 | * Send userauth request message specifying keyboard-interactive method. | |
1414 | */ | |
1415 | int | |
1416 | userauth_kbdint(Authctxt *authctxt) | |
1417 | { | |
1418 | static int attempt = 0; | |
1419 | ||
1420 | if (attempt++ >= options.number_of_password_prompts) | |
1421 | return 0; | |
1422 | /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */ | |
1423 | if (attempt > 1 && !authctxt->info_req_seen) { | |
1424 | debug3("userauth_kbdint: disable: no info_req_seen"); | |
1425 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL); | |
1426 | return 0; | |
1427 | } | |
1428 | ||
1429 | debug2("userauth_kbdint"); | |
1430 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
1431 | packet_put_cstring(authctxt->server_user); | |
1432 | packet_put_cstring(authctxt->service); | |
1433 | packet_put_cstring(authctxt->method->name); | |
1434 | packet_put_cstring(""); /* lang */ | |
1435 | packet_put_cstring(options.kbd_interactive_devices ? | |
1436 | options.kbd_interactive_devices : ""); | |
1437 | packet_send(); | |
1438 | ||
1439 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req); | |
1440 | return 1; | |
1441 | } | |
1442 | ||
1443 | /* | |
1444 | * parse INFO_REQUEST, prompt user and send INFO_RESPONSE | |
1445 | */ | |
1446 | void | |
1447 | input_userauth_info_req(int type, u_int32_t seq, void *ctxt) | |
1448 | { | |
1449 | Authctxt *authctxt = ctxt; | |
1450 | char *name, *inst, *lang, *prompt, *response; | |
1451 | u_int num_prompts, i; | |
1452 | int echo = 0; | |
1453 | ||
1454 | debug2("input_userauth_info_req"); | |
1455 | ||
1456 | if (authctxt == NULL) | |
1457 | fatal("input_userauth_info_req: no authentication context"); | |
1458 | ||
1459 | authctxt->info_req_seen = 1; | |
1460 | ||
1461 | name = packet_get_string(NULL); | |
1462 | inst = packet_get_string(NULL); | |
1463 | lang = packet_get_string(NULL); | |
1464 | if (strlen(name) > 0) | |
1465 | logit("%s", name); | |
1466 | if (strlen(inst) > 0) | |
1467 | logit("%s", inst); | |
1468 | xfree(name); | |
1469 | xfree(inst); | |
1470 | xfree(lang); | |
1471 | ||
1472 | num_prompts = packet_get_int(); | |
1473 | /* | |
1474 | * Begin to build info response packet based on prompts requested. | |
1475 | * We commit to providing the correct number of responses, so if | |
1476 | * further on we run into a problem that prevents this, we have to | |
1477 | * be sure and clean this up and send a correct error response. | |
1478 | */ | |
1479 | packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE); | |
1480 | packet_put_int(num_prompts); | |
1481 | ||
1482 | debug2("input_userauth_info_req: num_prompts %d", num_prompts); | |
1483 | for (i = 0; i < num_prompts; i++) { | |
1484 | prompt = packet_get_string(NULL); | |
1485 | echo = packet_get_char(); | |
1486 | ||
1487 | response = read_passphrase(prompt, echo ? RP_ECHO : 0); | |
1488 | ||
1489 | packet_put_cstring(response); | |
1490 | memset(response, 0, strlen(response)); | |
1491 | xfree(response); | |
1492 | xfree(prompt); | |
1493 | } | |
1494 | packet_check_eom(); /* done with parsing incoming message. */ | |
1495 | ||
1496 | packet_add_padding(64); | |
1497 | packet_send(); | |
1498 | } | |
1499 | ||
1500 | static int | |
1501 | ssh_keysign(Key *key, u_char **sigp, u_int *lenp, | |
1502 | u_char *data, u_int datalen) | |
1503 | { | |
1504 | Buffer b; | |
1505 | struct stat st; | |
1506 | pid_t pid; | |
1507 | int to[2], from[2], status, version = 2; | |
1508 | ||
1509 | debug2("ssh_keysign called"); | |
1510 | ||
1511 | if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) { | |
1512 | error("ssh_keysign: no installed: %s", strerror(errno)); | |
1513 | return -1; | |
1514 | } | |
1515 | if (fflush(stdout) != 0) | |
1516 | error("ssh_keysign: fflush: %s", strerror(errno)); | |
1517 | if (pipe(to) < 0) { | |
1518 | error("ssh_keysign: pipe: %s", strerror(errno)); | |
1519 | return -1; | |
1520 | } | |
1521 | if (pipe(from) < 0) { | |
1522 | error("ssh_keysign: pipe: %s", strerror(errno)); | |
1523 | return -1; | |
1524 | } | |
1525 | if ((pid = fork()) < 0) { | |
1526 | error("ssh_keysign: fork: %s", strerror(errno)); | |
1527 | return -1; | |
1528 | } | |
1529 | if (pid == 0) { | |
1530 | permanently_drop_suid(getuid()); | |
1531 | close(from[0]); | |
1532 | if (dup2(from[1], STDOUT_FILENO) < 0) | |
1533 | fatal("ssh_keysign: dup2: %s", strerror(errno)); | |
1534 | close(to[1]); | |
1535 | if (dup2(to[0], STDIN_FILENO) < 0) | |
1536 | fatal("ssh_keysign: dup2: %s", strerror(errno)); | |
1537 | close(from[1]); | |
1538 | close(to[0]); | |
1539 | execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0); | |
1540 | fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN, | |
1541 | strerror(errno)); | |
1542 | } | |
1543 | close(from[1]); | |
1544 | close(to[0]); | |
1545 | ||
1546 | buffer_init(&b); | |
1547 | buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */ | |
1548 | buffer_put_string(&b, data, datalen); | |
1549 | if (ssh_msg_send(to[1], version, &b) == -1) | |
1550 | fatal("ssh_keysign: couldn't send request"); | |
1551 | ||
1552 | if (ssh_msg_recv(from[0], &b) < 0) { | |
1553 | error("ssh_keysign: no reply"); | |
1554 | buffer_free(&b); | |
1555 | return -1; | |
1556 | } | |
1557 | close(from[0]); | |
1558 | close(to[1]); | |
1559 | ||
1560 | while (waitpid(pid, &status, 0) < 0) | |
1561 | if (errno != EINTR) | |
1562 | break; | |
1563 | ||
1564 | if (buffer_get_char(&b) != version) { | |
1565 | error("ssh_keysign: bad version"); | |
1566 | buffer_free(&b); | |
1567 | return -1; | |
1568 | } | |
1569 | *sigp = buffer_get_string(&b, lenp); | |
1570 | buffer_free(&b); | |
1571 | ||
1572 | return 0; | |
1573 | } | |
1574 | ||
1575 | int | |
1576 | userauth_hostbased(Authctxt *authctxt) | |
1577 | { | |
1578 | Key *private = NULL; | |
1579 | Sensitive *sensitive = authctxt->sensitive; | |
1580 | Buffer b; | |
1581 | u_char *signature, *blob; | |
1582 | char *chost, *pkalg, *p, myname[NI_MAXHOST]; | |
1583 | const char *service; | |
1584 | u_int blen, slen; | |
1585 | int ok, i, len, found = 0; | |
1586 | ||
1587 | /* check for a useful key */ | |
1588 | for (i = 0; i < sensitive->nkeys; i++) { | |
1589 | private = sensitive->keys[i]; | |
1590 | if (private && private->type != KEY_RSA1) { | |
1591 | found = 1; | |
1592 | /* we take and free the key */ | |
1593 | sensitive->keys[i] = NULL; | |
1594 | break; | |
1595 | } | |
1596 | } | |
1597 | if (!found) { | |
1598 | debug("No more client hostkeys for hostbased authentication."); | |
1599 | return 0; | |
1600 | } | |
1601 | if (key_to_blob(private, &blob, &blen) == 0) { | |
1602 | key_free(private); | |
1603 | return 0; | |
1604 | } | |
1605 | /* figure out a name for the client host */ | |
1606 | p = NULL; | |
1607 | if (packet_connection_is_on_socket()) | |
1608 | p = get_local_name(packet_get_connection_in()); | |
1609 | if (p == NULL) { | |
1610 | if (gethostname(myname, sizeof(myname)) == -1) { | |
1611 | verbose("userauth_hostbased: gethostname: %s", | |
1612 | strerror(errno)); | |
1613 | } else | |
1614 | p = xstrdup(myname); | |
1615 | } | |
1616 | if (p == NULL) { | |
1617 | error("userauth_hostbased: cannot get local ipaddr/name"); | |
1618 | key_free(private); | |
1619 | xfree(blob); | |
1620 | return 0; | |
1621 | } | |
1622 | len = strlen(p) + 2; | |
1623 | xasprintf(&chost, "%s.", p); | |
1624 | debug2("userauth_hostbased: chost %s", chost); | |
1625 | xfree(p); | |
1626 | ||
1627 | service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : | |
1628 | authctxt->service; | |
1629 | pkalg = xstrdup(key_ssh_name(private)); | |
1630 | buffer_init(&b); | |
1631 | /* construct data */ | |
1632 | buffer_put_string(&b, session_id2, session_id2_len); | |
1633 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); | |
1634 | buffer_put_cstring(&b, authctxt->server_user); | |
1635 | buffer_put_cstring(&b, service); | |
1636 | buffer_put_cstring(&b, authctxt->method->name); | |
1637 | buffer_put_cstring(&b, pkalg); | |
1638 | buffer_put_string(&b, blob, blen); | |
1639 | buffer_put_cstring(&b, chost); | |
1640 | buffer_put_cstring(&b, authctxt->local_user); | |
1641 | #ifdef DEBUG_PK | |
1642 | buffer_dump(&b); | |
1643 | #endif | |
1644 | if (sensitive->external_keysign) | |
1645 | ok = ssh_keysign(private, &signature, &slen, | |
1646 | buffer_ptr(&b), buffer_len(&b)); | |
1647 | else | |
1648 | ok = key_sign(private, &signature, &slen, | |
1649 | buffer_ptr(&b), buffer_len(&b)); | |
1650 | key_free(private); | |
1651 | buffer_free(&b); | |
1652 | if (ok != 0) { | |
1653 | error("key_sign failed"); | |
1654 | xfree(chost); | |
1655 | xfree(pkalg); | |
1656 | xfree(blob); | |
1657 | return 0; | |
1658 | } | |
1659 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
1660 | packet_put_cstring(authctxt->server_user); | |
1661 | packet_put_cstring(authctxt->service); | |
1662 | packet_put_cstring(authctxt->method->name); | |
1663 | packet_put_cstring(pkalg); | |
1664 | packet_put_string(blob, blen); | |
1665 | packet_put_cstring(chost); | |
1666 | packet_put_cstring(authctxt->local_user); | |
1667 | packet_put_string(signature, slen); | |
1668 | memset(signature, 's', slen); | |
1669 | xfree(signature); | |
1670 | xfree(chost); | |
1671 | xfree(pkalg); | |
1672 | xfree(blob); | |
1673 | ||
1674 | packet_send(); | |
1675 | return 1; | |
1676 | } | |
1677 | ||
1678 | #ifdef JPAKE | |
1679 | int | |
1680 | userauth_jpake(Authctxt *authctxt) | |
1681 | { | |
1682 | struct jpake_ctx *pctx; | |
1683 | u_char *x1_proof, *x2_proof; | |
1684 | u_int x1_proof_len, x2_proof_len; | |
1685 | static int attempt = 0; /* XXX share with userauth_password's? */ | |
1686 | ||
1687 | if (attempt++ >= options.number_of_password_prompts) | |
1688 | return 0; | |
1689 | if (attempt != 1) | |
1690 | error("Permission denied, please try again."); | |
1691 | ||
1692 | if (authctxt->methoddata != NULL) | |
1693 | fatal("%s: authctxt->methoddata already set (%p)", | |
1694 | __func__, authctxt->methoddata); | |
1695 | ||
1696 | authctxt->methoddata = pctx = jpake_new(); | |
1697 | ||
1698 | /* | |
1699 | * Send request immediately, to get the protocol going while | |
1700 | * we do the initial computations. | |
1701 | */ | |
1702 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
1703 | packet_put_cstring(authctxt->server_user); | |
1704 | packet_put_cstring(authctxt->service); | |
1705 | packet_put_cstring(authctxt->method->name); | |
1706 | packet_send(); | |
1707 | packet_write_wait(); | |
1708 | ||
1709 | jpake_step1(pctx->grp, | |
1710 | &pctx->client_id, &pctx->client_id_len, | |
1711 | &pctx->x1, &pctx->x2, &pctx->g_x1, &pctx->g_x2, | |
1712 | &x1_proof, &x1_proof_len, | |
1713 | &x2_proof, &x2_proof_len); | |
1714 | ||
1715 | JPAKE_DEBUG_CTX((pctx, "step 1 sending in %s", __func__)); | |
1716 | ||
1717 | packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1); | |
1718 | packet_put_string(pctx->client_id, pctx->client_id_len); | |
1719 | packet_put_bignum2(pctx->g_x1); | |
1720 | packet_put_bignum2(pctx->g_x2); | |
1721 | packet_put_string(x1_proof, x1_proof_len); | |
1722 | packet_put_string(x2_proof, x2_proof_len); | |
1723 | packet_send(); | |
1724 | ||
1725 | bzero(x1_proof, x1_proof_len); | |
1726 | bzero(x2_proof, x2_proof_len); | |
1727 | xfree(x1_proof); | |
1728 | xfree(x2_proof); | |
1729 | ||
1730 | /* Expect step 1 packet from peer */ | |
1731 | dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, | |
1732 | input_userauth_jpake_server_step1); | |
1733 | dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, | |
1734 | &input_userauth_success_unexpected); | |
1735 | ||
1736 | return 1; | |
1737 | } | |
1738 | ||
1739 | void | |
1740 | userauth_jpake_cleanup(Authctxt *authctxt) | |
1741 | { | |
1742 | debug3("%s: clean up", __func__); | |
1743 | if (authctxt->methoddata != NULL) { | |
1744 | jpake_free(authctxt->methoddata); | |
1745 | authctxt->methoddata = NULL; | |
1746 | } | |
1747 | dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success); | |
1748 | } | |
1749 | #endif /* JPAKE */ | |
1750 | ||
1751 | /* find auth method */ | |
1752 | ||
1753 | /* | |
1754 | * given auth method name, if configurable options permit this method fill | |
1755 | * in auth_ident field and return true, otherwise return false. | |
1756 | */ | |
1757 | static int | |
1758 | authmethod_is_enabled(Authmethod *method) | |
1759 | { | |
1760 | if (method == NULL) | |
1761 | return 0; | |
1762 | /* return false if options indicate this method is disabled */ | |
1763 | if (method->enabled == NULL || *method->enabled == 0) | |
1764 | return 0; | |
1765 | /* return false if batch mode is enabled but method needs interactive mode */ | |
1766 | if (method->batch_flag != NULL && *method->batch_flag != 0) | |
1767 | return 0; | |
1768 | return 1; | |
1769 | } | |
1770 | ||
1771 | static Authmethod * | |
1772 | authmethod_lookup(const char *name) | |
1773 | { | |
1774 | Authmethod *method = NULL; | |
1775 | if (name != NULL) | |
1776 | for (method = authmethods; method->name != NULL; method++) | |
1777 | if (strcmp(name, method->name) == 0) | |
1778 | return method; | |
1779 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); | |
1780 | return NULL; | |
1781 | } | |
1782 | ||
1783 | /* XXX internal state */ | |
1784 | static Authmethod *current = NULL; | |
1785 | static char *supported = NULL; | |
1786 | static char *preferred = NULL; | |
1787 | ||
1788 | /* | |
1789 | * Given the authentication method list sent by the server, return the | |
1790 | * next method we should try. If the server initially sends a nil list, | |
1791 | * use a built-in default list. | |
1792 | */ | |
1793 | static Authmethod * | |
1794 | authmethod_get(char *authlist) | |
1795 | { | |
1796 | char *name = NULL; | |
1797 | u_int next; | |
1798 | ||
1799 | /* Use a suitable default if we're passed a nil list. */ | |
1800 | if (authlist == NULL || strlen(authlist) == 0) | |
1801 | authlist = options.preferred_authentications; | |
1802 | ||
1803 | if (supported == NULL || strcmp(authlist, supported) != 0) { | |
1804 | debug3("start over, passed a different list %s", authlist); | |
1805 | if (supported != NULL) | |
1806 | xfree(supported); | |
1807 | supported = xstrdup(authlist); | |
1808 | preferred = options.preferred_authentications; | |
1809 | debug3("preferred %s", preferred); | |
1810 | current = NULL; | |
1811 | } else if (current != NULL && authmethod_is_enabled(current)) | |
1812 | return current; | |
1813 | ||
1814 | for (;;) { | |
1815 | if ((name = match_list(preferred, supported, &next)) == NULL) { | |
1816 | debug("No more authentication methods to try."); | |
1817 | current = NULL; | |
1818 | return NULL; | |
1819 | } | |
1820 | preferred += next; | |
1821 | debug3("authmethod_lookup %s", name); | |
1822 | debug3("remaining preferred: %s", preferred); | |
1823 | if ((current = authmethod_lookup(name)) != NULL && | |
1824 | authmethod_is_enabled(current)) { | |
1825 | debug3("authmethod_is_enabled %s", name); | |
1826 | debug("Next authentication method: %s", name); | |
1827 | return current; | |
1828 | } | |
1829 | } | |
1830 | } | |
1831 | ||
1832 | static char * | |
1833 | authmethods_get(void) | |
1834 | { | |
1835 | Authmethod *method = NULL; | |
1836 | Buffer b; | |
1837 | char *list; | |
1838 | ||
1839 | buffer_init(&b); | |
1840 | for (method = authmethods; method->name != NULL; method++) { | |
1841 | if (authmethod_is_enabled(method)) { | |
1842 | if (buffer_len(&b) > 0) | |
1843 | buffer_append(&b, ",", 1); | |
1844 | buffer_append(&b, method->name, strlen(method->name)); | |
1845 | } | |
1846 | } | |
1847 | buffer_append(&b, "\0", 1); | |
1848 | list = xstrdup(buffer_ptr(&b)); | |
1849 | buffer_free(&b); | |
1850 | return list; | |
1851 | } | |
1852 |