]>
Commit | Line | Data |
---|---|---|
d8b0d145 | 1 | /* $OpenBSD: sshconnect2.c,v 1.173 2009/10/24 11:13:54 andreas Exp $ */ |
a306f2dd | 2 | /* |
3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. | |
5adf6b9a | 4 | * Copyright (c) 2008 Damien Miller. All rights reserved. |
a306f2dd | 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. | |
a306f2dd | 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" | |
c3ff311a | 28 | |
aa2eae64 | 29 | #include <sys/types.h> |
31652869 | 30 | #include <sys/socket.h> |
aa2eae64 | 31 | #include <sys/wait.h> |
4095f623 | 32 | #include <sys/stat.h> |
a306f2dd | 33 | |
028094f4 | 34 | #include <errno.h> |
b2349766 | 35 | #include <netdb.h> |
31652869 | 36 | #include <pwd.h> |
37 | #include <signal.h> | |
24436b92 | 38 | #include <stdarg.h> |
cf851879 | 39 | #include <stdio.h> |
00146caa | 40 | #include <string.h> |
67514848 | 41 | #include <unistd.h> |
3a69fb58 | 42 | #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) |
e5df5ff2 | 43 | #include <vis.h> |
3a69fb58 | 44 | #endif |
028094f4 | 45 | |
751092f9 | 46 | #include "openbsd-compat/sys-queue.h" |
47 | ||
31652869 | 48 | #include "xmalloc.h" |
a306f2dd | 49 | #include "ssh.h" |
42f11eb2 | 50 | #include "ssh2.h" |
a306f2dd | 51 | #include "buffer.h" |
52 | #include "packet.h" | |
a306f2dd | 53 | #include "compat.h" |
42f11eb2 | 54 | #include "cipher.h" |
31652869 | 55 | #include "key.h" |
a306f2dd | 56 | #include "kex.h" |
57 | #include "myproposal.h" | |
a306f2dd | 58 | #include "sshconnect.h" |
59 | #include "authfile.h" | |
db1cd2f3 | 60 | #include "dh.h" |
2e73a022 | 61 | #include "authfd.h" |
42f11eb2 | 62 | #include "log.h" |
63 | #include "readconf.h" | |
58ae9cb8 | 64 | #include "misc.h" |
cab80f75 | 65 | #include "match.h" |
a5c9ffdb | 66 | #include "dispatch.h" |
8002af61 | 67 | #include "canohost.h" |
39c00dc2 | 68 | #include "msg.h" |
69 | #include "pathnames.h" | |
82aeee5d | 70 | #include "uidswap.h" |
5b01421b | 71 | #include "schnorr.h" |
5adf6b9a | 72 | #include "jpake.h" |
94ec8c6b | 73 | |
7364bd04 | 74 | #ifdef GSSAPI |
75 | #include "ssh-gss.h" | |
76 | #endif | |
77 | ||
a306f2dd | 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 | ||
1e3b8b07 | 87 | u_char *session_id2 = NULL; |
a27002e5 | 88 | u_int session_id2_len = 0; |
a306f2dd | 89 | |
a5c9ffdb | 90 | char *xxx_host; |
91 | struct sockaddr *xxx_hostaddr; | |
92 | ||
e092ce67 | 93 | Kex *xxx_kex = NULL; |
94 | ||
396c147e | 95 | static int |
f49bc4f7 | 96 | verify_host_key_callback(Key *hostkey) |
a5c9ffdb | 97 | { |
f49bc4f7 | 98 | if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1) |
01e9ef57 | 99 | fatal("Host key verification failed."); |
a5c9ffdb | 100 | return 0; |
101 | } | |
102 | ||
a306f2dd | 103 | void |
94ec8c6b | 104 | ssh_kex2(char *host, struct sockaddr *hostaddr) |
105 | { | |
94ec8c6b | 106 | Kex *kex; |
a5c9ffdb | 107 | |
108 | xxx_host = host; | |
109 | xxx_hostaddr = hostaddr; | |
94ec8c6b | 110 | |
c523303b | 111 | if (options.ciphers == (char *)-1) { |
bbe88b6d | 112 | logit("No valid ciphers for protocol version 2 given, using defaults."); |
c523303b | 113 | options.ciphers = NULL; |
94ec8c6b | 114 | } |
115 | if (options.ciphers != NULL) { | |
116 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = | |
117 | myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers; | |
118 | } | |
1ad64a93 | 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]); | |
94ec8c6b | 123 | if (options.compression) { |
b2552997 | 124 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = |
07200973 | 125 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none"; |
94ec8c6b | 126 | } else { |
b2552997 | 127 | myproposal[PROPOSAL_COMP_ALGS_CTOS] = |
07200973 | 128 | myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib"; |
94ec8c6b | 129 | } |
b2552997 | 130 | if (options.macs != NULL) { |
131 | myproposal[PROPOSAL_MAC_ALGS_CTOS] = | |
132 | myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs; | |
133 | } | |
e961a8f9 | 134 | if (options.hostkeyalgorithms != NULL) |
184eed6a | 135 | myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = |
e961a8f9 | 136 | options.hostkeyalgorithms; |
94ec8c6b | 137 | |
ffd7b36b | 138 | if (options.rekey_limit) |
fe771396 | 139 | packet_set_rekey_limit((u_int32_t)options.rekey_limit); |
ffd7b36b | 140 | |
7a37c112 | 141 | /* start key exchange */ |
d8ee838b | 142 | kex = kex_setup(myproposal); |
98a58eda | 143 | kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client; |
41e5bd9a | 144 | kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client; |
98a58eda | 145 | kex->kex[KEX_DH_GEX_SHA1] = kexgex_client; |
2ff8003a | 146 | kex->kex[KEX_DH_GEX_SHA256] = kexgex_client; |
a5c9ffdb | 147 | kex->client_version_string=client_version_string; |
148 | kex->server_version_string=server_version_string; | |
f49bc4f7 | 149 | kex->verify_host_key=&verify_host_key_callback; |
94ec8c6b | 150 | |
e092ce67 | 151 | xxx_kex = kex; |
152 | ||
c422989b | 153 | dispatch_run(DISPATCH_BLOCK, &kex->done, kex); |
94ec8c6b | 154 | |
d8b0d145 | 155 | if (options.use_roaming && !kex->roaming) { |
156 | debug("Roaming not allowed by server"); | |
157 | options.use_roaming = 0; | |
158 | } | |
159 | ||
d1ac6175 | 160 | session_id2 = kex->session_id; |
161 | session_id2_len = kex->session_id_len; | |
162 | ||
94ec8c6b | 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 | |
a306f2dd | 170 | } |
71276795 | 171 | |
a306f2dd | 172 | /* |
173 | * Authenticate user | |
174 | */ | |
188adeb2 | 175 | |
176 | typedef struct Authctxt Authctxt; | |
177 | typedef struct Authmethod Authmethod; | |
43348518 | 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); | |
188adeb2 | 190 | |
191 | struct Authctxt { | |
192 | const char *server_user; | |
8002af61 | 193 | const char *local_user; |
188adeb2 | 194 | const char *host; |
195 | const char *service; | |
188adeb2 | 196 | Authmethod *method; |
94ec8c6b | 197 | int success; |
fee56204 | 198 | char *authlist; |
8002af61 | 199 | /* pubkey */ |
43348518 | 200 | Idlist keys; |
8002af61 | 201 | AuthenticationConnection *agent; |
202 | /* hostbased */ | |
39c00dc2 | 203 | Sensitive *sensitive; |
6b759005 | 204 | /* kbd-interactive */ |
205 | int info_req_seen; | |
7364bd04 | 206 | /* generic */ |
207 | void *methoddata; | |
188adeb2 | 208 | }; |
209 | struct Authmethod { | |
210 | char *name; /* string to compare against server's list */ | |
211 | int (*userauth)(Authctxt *authctxt); | |
5adf6b9a | 212 | void (*cleanup)(Authctxt *authctxt); |
188adeb2 | 213 | int *enabled; /* flag in option struct that enables method */ |
214 | int *batch_flag; /* flag in option struct that disables method */ | |
215 | }; | |
216 | ||
7819b5c3 | 217 | void input_userauth_success(int, u_int32_t, void *); |
fbba8bf6 | 218 | void input_userauth_success_unexpected(int, u_int32_t, void *); |
7819b5c3 | 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 *); | |
67ec167d | 224 | void input_userauth_passwd_changereq(int, u_int32_t, void *); |
5adf6b9a | 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 *); | |
94ec8c6b | 228 | |
d5bb9418 | 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 *); | |
5adf6b9a | 234 | int userauth_jpake(Authctxt *); |
235 | ||
236 | void userauth_jpake_cleanup(Authctxt *); | |
188adeb2 | 237 | |
7364bd04 | 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 | ||
d5bb9418 | 247 | void userauth(Authctxt *, char *); |
fee56204 | 248 | |
43348518 | 249 | static int sign_and_send_pubkey(Authctxt *, Identity *); |
43348518 | 250 | static void pubkey_prepare(Authctxt *); |
251 | static void pubkey_cleanup(Authctxt *); | |
252 | static Key *load_identity_file(char *); | |
fee56204 | 253 | |
396c147e | 254 | static Authmethod *authmethod_get(char *authlist); |
255 | static Authmethod *authmethod_lookup(const char *name); | |
256 | static char *authmethods_get(void); | |
188adeb2 | 257 | |
258 | Authmethod authmethods[] = { | |
7364bd04 | 259 | #ifdef GSSAPI |
0789992b | 260 | {"gssapi-with-mic", |
7364bd04 | 261 | userauth_gssapi, |
5adf6b9a | 262 | NULL, |
7364bd04 | 263 | &options.gss_authentication, |
264 | NULL}, | |
265 | #endif | |
3398dda9 | 266 | {"hostbased", |
267 | userauth_hostbased, | |
5adf6b9a | 268 | NULL, |
3398dda9 | 269 | &options.hostbased_authentication, |
270 | NULL}, | |
9f37c0af | 271 | {"publickey", |
272 | userauth_pubkey, | |
5adf6b9a | 273 | NULL, |
9f37c0af | 274 | &options.pubkey_authentication, |
275 | NULL}, | |
5adf6b9a | 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 | |
94ec8c6b | 283 | {"keyboard-interactive", |
284 | userauth_kbdint, | |
5adf6b9a | 285 | NULL, |
94ec8c6b | 286 | &options.kbd_interactive_authentication, |
287 | &options.batch_mode}, | |
9f37c0af | 288 | {"password", |
289 | userauth_passwd, | |
5adf6b9a | 290 | NULL, |
9f37c0af | 291 | &options.password_authentication, |
292 | &options.batch_mode}, | |
94ec8c6b | 293 | {"none", |
294 | userauth_none, | |
295 | NULL, | |
5adf6b9a | 296 | NULL, |
94ec8c6b | 297 | NULL}, |
5adf6b9a | 298 | {NULL, NULL, NULL, NULL, NULL} |
188adeb2 | 299 | }; |
300 | ||
301 | void | |
8002af61 | 302 | ssh_userauth2(const char *local_user, const char *server_user, char *host, |
39c00dc2 | 303 | Sensitive *sensitive) |
188adeb2 | 304 | { |
305 | Authctxt authctxt; | |
306 | int type; | |
188adeb2 | 307 | |
5ba55ada | 308 | if (options.challenge_response_authentication) |
d464095c | 309 | options.kbd_interactive_authentication = 1; |
310 | ||
188adeb2 | 311 | packet_start(SSH2_MSG_SERVICE_REQUEST); |
312 | packet_put_cstring("ssh-userauth"); | |
313 | packet_send(); | |
a77673cc | 314 | debug("SSH2_MSG_SERVICE_REQUEST sent"); |
188adeb2 | 315 | packet_write_wait(); |
54a5250f | 316 | type = packet_read(); |
a77673cc | 317 | if (type != SSH2_MSG_SERVICE_ACCEPT) |
318 | fatal("Server denied authentication request: %d", type); | |
188adeb2 | 319 | if (packet_remaining() > 0) { |
54a5250f | 320 | char *reply = packet_get_string(NULL); |
a77673cc | 321 | debug2("service_accept: %s", reply); |
188adeb2 | 322 | xfree(reply); |
188adeb2 | 323 | } else { |
6226a8f8 | 324 | debug2("buggy server: service_accept w/o service"); |
188adeb2 | 325 | } |
95500969 | 326 | packet_check_eom(); |
a77673cc | 327 | debug("SSH2_MSG_SERVICE_ACCEPT received"); |
188adeb2 | 328 | |
cab80f75 | 329 | if (options.preferred_authentications == NULL) |
330 | options.preferred_authentications = authmethods_get(); | |
331 | ||
188adeb2 | 332 | /* setup authentication context */ |
6b759005 | 333 | memset(&authctxt, 0, sizeof(authctxt)); |
43348518 | 334 | pubkey_prepare(&authctxt); |
188adeb2 | 335 | authctxt.server_user = server_user; |
8002af61 | 336 | authctxt.local_user = local_user; |
188adeb2 | 337 | authctxt.host = host; |
338 | authctxt.service = "ssh-connection"; /* service name */ | |
339 | authctxt.success = 0; | |
94ec8c6b | 340 | authctxt.method = authmethod_lookup("none"); |
fee56204 | 341 | authctxt.authlist = NULL; |
7364bd04 | 342 | authctxt.methoddata = NULL; |
39c00dc2 | 343 | authctxt.sensitive = sensitive; |
6b759005 | 344 | authctxt.info_req_seen = 0; |
94ec8c6b | 345 | if (authctxt.method == NULL) |
346 | fatal("ssh_userauth2: internal error: cannot send userauth none request"); | |
188adeb2 | 347 | |
348 | /* initial userauth request */ | |
94ec8c6b | 349 | userauth_none(&authctxt); |
188adeb2 | 350 | |
7a37c112 | 351 | dispatch_init(&input_userauth_error); |
188adeb2 | 352 | dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success); |
353 | dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure); | |
9616313f | 354 | dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner); |
188adeb2 | 355 | dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */ |
356 | ||
43348518 | 357 | pubkey_cleanup(&authctxt); |
b8c2031b | 358 | dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL); |
359 | ||
6226a8f8 | 360 | debug("Authentication succeeded (%s).", authctxt.method->name); |
188adeb2 | 361 | } |
b8c2031b | 362 | |
188adeb2 | 363 | void |
fee56204 | 364 | userauth(Authctxt *authctxt, char *authlist) |
365 | { | |
5adf6b9a | 366 | if (authctxt->method != NULL && authctxt->method->cleanup != NULL) |
367 | authctxt->method->cleanup(authctxt); | |
368 | ||
7364bd04 | 369 | if (authctxt->methoddata) { |
370 | xfree(authctxt->methoddata); | |
371 | authctxt->methoddata = NULL; | |
372 | } | |
fee56204 | 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; | |
b8c2031b | 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 */ | |
fee56204 | 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 | } | |
d6133f43 | 400 | |
e64399cc | 401 | /* ARGSUSED */ |
fee56204 | 402 | void |
7819b5c3 | 403 | input_userauth_error(int type, u_int32_t seq, void *ctxt) |
188adeb2 | 404 | { |
9616313f | 405 | fatal("input_userauth_error: bad message during authentication: " |
4e2e5cfd | 406 | "type %d", type); |
9616313f | 407 | } |
d6133f43 | 408 | |
e64399cc | 409 | /* ARGSUSED */ |
9616313f | 410 | void |
7819b5c3 | 411 | input_userauth_banner(int type, u_int32_t seq, void *ctxt) |
9616313f | 412 | { |
e5df5ff2 | 413 | char *msg, *raw, *lang; |
414 | u_int len; | |
c8f0cf13 | 415 | |
9616313f | 416 | debug3("input_userauth_banner"); |
e5df5ff2 | 417 | raw = packet_get_string(&len); |
9616313f | 418 | lang = packet_get_string(NULL); |
341cb46b | 419 | if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) { |
e5df5ff2 | 420 | if (len > 65536) |
421 | len = 65536; | |
90d5350e | 422 | msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */ |
423 | strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL); | |
19e633e7 | 424 | fprintf(stderr, "%s", msg); |
e5df5ff2 | 425 | xfree(msg); |
426 | } | |
427 | xfree(raw); | |
9616313f | 428 | xfree(lang); |
188adeb2 | 429 | } |
d6133f43 | 430 | |
e64399cc | 431 | /* ARGSUSED */ |
188adeb2 | 432 | void |
7819b5c3 | 433 | input_userauth_success(int type, u_int32_t seq, void *ctxt) |
188adeb2 | 434 | { |
435 | Authctxt *authctxt = ctxt; | |
fbba8bf6 | 436 | |
188adeb2 | 437 | if (authctxt == NULL) |
438 | fatal("input_userauth_success: no authentication context"); | |
c8f0cf13 | 439 | if (authctxt->authlist) { |
fee56204 | 440 | xfree(authctxt->authlist); |
c8f0cf13 | 441 | authctxt->authlist = NULL; |
442 | } | |
fbba8bf6 | 443 | if (authctxt->method != NULL && authctxt->method->cleanup != NULL) |
444 | authctxt->method->cleanup(authctxt); | |
c8f0cf13 | 445 | if (authctxt->methoddata) { |
7364bd04 | 446 | xfree(authctxt->methoddata); |
c8f0cf13 | 447 | authctxt->methoddata = NULL; |
448 | } | |
188adeb2 | 449 | authctxt->success = 1; /* break out */ |
450 | } | |
d6133f43 | 451 | |
fbba8bf6 | 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 | ||
e64399cc | 464 | /* ARGSUSED */ |
188adeb2 | 465 | void |
7819b5c3 | 466 | input_userauth_failure(int type, u_int32_t seq, void *ctxt) |
188adeb2 | 467 | { |
188adeb2 | 468 | Authctxt *authctxt = ctxt; |
469 | char *authlist = NULL; | |
470 | int partial; | |
188adeb2 | 471 | |
472 | if (authctxt == NULL) | |
473 | fatal("input_userauth_failure: no authentication context"); | |
474 | ||
94ec8c6b | 475 | authlist = packet_get_string(NULL); |
188adeb2 | 476 | partial = packet_get_char(); |
95500969 | 477 | packet_check_eom(); |
188adeb2 | 478 | |
479 | if (partial != 0) | |
bbe88b6d | 480 | logit("Authenticated with partial success."); |
6226a8f8 | 481 | debug("Authentications that can continue: %s", authlist); |
188adeb2 | 482 | |
fee56204 | 483 | userauth(authctxt, authlist); |
484 | } | |
e64399cc | 485 | |
486 | /* ARGSUSED */ | |
fee56204 | 487 | void |
7819b5c3 | 488 | input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt) |
fee56204 | 489 | { |
490 | Authctxt *authctxt = ctxt; | |
491 | Key *key = NULL; | |
43348518 | 492 | Identity *id = NULL; |
fee56204 | 493 | Buffer b; |
c66f9d0e | 494 | int pktype, sent = 0; |
495 | u_int alen, blen; | |
496 | char *pkalg, *fp; | |
497 | u_char *pkblob; | |
fee56204 | 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 | } | |
95500969 | 513 | packet_check_eom(); |
fee56204 | 514 | |
43348518 | 515 | debug("Server accepts key: pkalg %s blen %u", pkalg, blen); |
fee56204 | 516 | |
43348518 | 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 | ||
b56e99e2 | 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 | */ | |
484b2208 | 540 | TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) { |
43348518 | 541 | if (key_equal(key, id->key)) { |
542 | sent = sign_and_send_pubkey(authctxt, id); | |
fee56204 | 543 | break; |
544 | } | |
43348518 | 545 | } |
546 | done: | |
fee56204 | 547 | if (key != NULL) |
548 | key_free(key); | |
549 | xfree(pkalg); | |
550 | xfree(pkblob); | |
551 | ||
343288b8 | 552 | /* try another method if we did not send a packet */ |
fee56204 | 553 | if (sent == 0) |
554 | userauth(authctxt, NULL); | |
188adeb2 | 555 | } |
556 | ||
7364bd04 | 557 | #ifdef GSSAPI |
aff51935 | 558 | int |
7364bd04 | 559 | userauth_gssapi(Authctxt *authctxt) |
560 | { | |
561 | Gssctxt *gssctxt = NULL; | |
b0b30ca6 | 562 | static gss_OID_set gss_supported = NULL; |
2ceb8101 | 563 | static u_int mech = 0; |
7364bd04 | 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 | ||
b0b30ca6 | 570 | if (gss_supported == NULL) |
571 | gss_indicate_mechs(&min, &gss_supported); | |
7364bd04 | 572 | |
573 | /* Check to see if the mechanism is usable before we offer it */ | |
b0b30ca6 | 574 | while (mech < gss_supported->count && !ok) { |
7364bd04 | 575 | /* My DER encoding requires length<128 */ |
b0b30ca6 | 576 | if (gss_supported->elements[mech].length < 128 && |
592de384 | 577 | ssh_gssapi_check_mechanism(&gssctxt, |
578 | &gss_supported->elements[mech], authctxt->host)) { | |
7364bd04 | 579 | ok = 1; /* Mechanism works */ |
580 | } else { | |
581 | mech++; | |
582 | } | |
583 | } | |
584 | ||
592de384 | 585 | if (!ok) |
2ceb8101 | 586 | return 0; |
7364bd04 | 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 | ||
d8d9afd0 | 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); | |
7364bd04 | 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 | ||
b930668c | 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; | |
fe206de9 | 621 | gss_buffer_desc mic = GSS_C_EMPTY_BUFFER; |
622 | gss_buffer_desc gssbuf; | |
0789992b | 623 | OM_uint32 status, ms, flags; |
624 | Buffer b; | |
b6453d99 | 625 | |
b930668c | 626 | status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, |
0789992b | 627 | recv_tok, &send_tok, &flags); |
b930668c | 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); | |
b6453d99 | 634 | |
b930668c | 635 | packet_put_string(send_tok.value, send_tok.length); |
636 | packet_send(); | |
637 | gss_release_buffer(&ms, &send_tok); | |
638 | } | |
b6453d99 | 639 | |
b930668c | 640 | if (status == GSS_S_COMPLETE) { |
0789992b | 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); | |
b6453d99 | 651 | |
0789992b | 652 | status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic); |
b6453d99 | 653 | |
0789992b | 654 | if (!GSS_ERROR(status)) { |
655 | packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC); | |
656 | packet_put_string(mic.value, mic.length); | |
b6453d99 | 657 | |
0789992b | 658 | packet_send(); |
659 | } | |
b6453d99 | 660 | |
0789992b | 661 | buffer_free(&b); |
662 | gss_release_buffer(&ms, &mic); | |
b6453d99 | 663 | } |
b930668c | 664 | } |
b6453d99 | 665 | |
b930668c | 666 | return status; |
667 | } | |
668 | ||
e64399cc | 669 | /* ARGSUSED */ |
7364bd04 | 670 | void |
671 | input_gssapi_response(int type, u_int32_t plen, void *ctxt) | |
672 | { | |
673 | Authctxt *authctxt = ctxt; | |
674 | Gssctxt *gssctxt; | |
7364bd04 | 675 | int oidlen; |
676 | char *oidv; | |
7364bd04 | 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 | ||
d8d9afd0 | 685 | if (oidlen <= 2 || |
686 | oidv[0] != SSH_GSS_OIDTYPE || | |
687 | oidv[1] != oidlen - 2) { | |
b930668c | 688 | xfree(oidv); |
d8d9afd0 | 689 | debug("Badly encoded mechanism OID received"); |
690 | userauth(authctxt, NULL); | |
d8d9afd0 | 691 | return; |
7364bd04 | 692 | } |
693 | ||
d8d9afd0 | 694 | if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2)) |
695 | fatal("Server returned different OID than expected"); | |
696 | ||
7364bd04 | 697 | packet_check_eom(); |
698 | ||
699 | xfree(oidv); | |
700 | ||
b930668c | 701 | if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) { |
7364bd04 | 702 | /* Start again with next method on list */ |
703 | debug("Trying to start again"); | |
704 | userauth(authctxt, NULL); | |
705 | return; | |
706 | } | |
7364bd04 | 707 | } |
708 | ||
e64399cc | 709 | /* ARGSUSED */ |
7364bd04 | 710 | void |
711 | input_gssapi_token(int type, u_int32_t plen, void *ctxt) | |
712 | { | |
713 | Authctxt *authctxt = ctxt; | |
7364bd04 | 714 | gss_buffer_desc recv_tok; |
b930668c | 715 | OM_uint32 status; |
7364bd04 | 716 | u_int slen; |
717 | ||
718 | if (authctxt == NULL) | |
719 | fatal("input_gssapi_response: no authentication context"); | |
7364bd04 | 720 | |
721 | recv_tok.value = packet_get_string(&slen); | |
722 | recv_tok.length = slen; /* safe typecast */ | |
723 | ||
724 | packet_check_eom(); | |
725 | ||
b930668c | 726 | status = process_gssapi_token(ctxt, &recv_tok); |
7364bd04 | 727 | |
728 | xfree(recv_tok.value); | |
729 | ||
730 | if (GSS_ERROR(status)) { | |
7364bd04 | 731 | /* Start again with the next method in the list */ |
732 | userauth(authctxt, NULL); | |
733 | return; | |
734 | } | |
7364bd04 | 735 | } |
736 | ||
e64399cc | 737 | /* ARGSUSED */ |
7364bd04 | 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; | |
f99e1ca4 | 746 | u_int len; |
7364bd04 | 747 | |
748 | if (authctxt == NULL) | |
749 | fatal("input_gssapi_response: no authentication context"); | |
750 | gssctxt = authctxt->methoddata; | |
751 | ||
f99e1ca4 | 752 | recv_tok.value = packet_get_string(&len); |
753 | recv_tok.length = len; | |
7364bd04 | 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, | |
4e2e5cfd | 759 | &recv_tok, &send_tok, NULL); |
7364bd04 | 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 | ||
e64399cc | 767 | /* ARGSUSED */ |
7364bd04 | 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 | ||
17318dd6 | 782 | debug("Server GSSAPI Error:\n%s", msg); |
7364bd04 | 783 | xfree(msg); |
784 | xfree(lang); | |
785 | } | |
786 | #endif /* GSSAPI */ | |
787 | ||
94ec8c6b | 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(); | |
94ec8c6b | 797 | return 1; |
798 | } | |
799 | ||
a306f2dd | 800 | int |
188adeb2 | 801 | userauth_passwd(Authctxt *authctxt) |
a306f2dd | 802 | { |
1d1ffb87 | 803 | static int attempt = 0; |
67ec167d | 804 | char prompt[150]; |
a306f2dd | 805 | char *password; |
806 | ||
fa649821 | 807 | if (attempt++ >= options.number_of_password_prompts) |
1d1ffb87 | 808 | return 0; |
809 | ||
6aacefa7 | 810 | if (attempt != 1) |
fa649821 | 811 | error("Permission denied, please try again."); |
812 | ||
f72e01a5 | 813 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", |
188adeb2 | 814 | authctxt->server_user, authctxt->host); |
a306f2dd | 815 | password = read_passphrase(prompt, 0); |
816 | packet_start(SSH2_MSG_USERAUTH_REQUEST); | |
188adeb2 | 817 | packet_put_cstring(authctxt->server_user); |
818 | packet_put_cstring(authctxt->service); | |
94ec8c6b | 819 | packet_put_cstring(authctxt->method->name); |
a306f2dd | 820 | packet_put_char(0); |
a6215e53 | 821 | packet_put_cstring(password); |
a306f2dd | 822 | memset(password, 0, strlen(password)); |
823 | xfree(password); | |
b4b701be | 824 | packet_add_padding(64); |
a306f2dd | 825 | packet_send(); |
67ec167d | 826 | |
7203d6bb | 827 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, |
67ec167d | 828 | &input_userauth_passwd_changereq); |
829 | ||
a306f2dd | 830 | return 1; |
831 | } | |
e64399cc | 832 | |
67ec167d | 833 | /* |
834 | * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST | |
835 | */ | |
e64399cc | 836 | /* ARGSUSED */ |
67ec167d | 837 | void |
ef077e37 | 838 | input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt) |
67ec167d | 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) | |
bbe88b6d | 853 | logit("%s", info); |
67ec167d | 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 */ | |
7203d6bb | 861 | snprintf(prompt, sizeof(prompt), |
67ec167d | 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) { | |
7203d6bb | 870 | snprintf(prompt, sizeof(prompt), |
67ec167d | 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 | } | |
7203d6bb | 878 | snprintf(prompt, sizeof(prompt), |
67ec167d | 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); | |
bbe88b6d | 885 | logit("Mismatch; try again, EOF to quit."); |
67ec167d | 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(); | |
7203d6bb | 896 | |
897 | dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, | |
67ec167d | 898 | &input_userauth_passwd_changereq); |
899 | } | |
a306f2dd | 900 | |
5adf6b9a | 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 | ||
43348518 | 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); | |
fee56204 | 1127 | } |
1128 | ||
396c147e | 1129 | static int |
43348518 | 1130 | sign_and_send_pubkey(Authctxt *authctxt, Identity *id) |
a306f2dd | 1131 | { |
1132 | Buffer b; | |
1e3b8b07 | 1133 | u_char *blob, *signature; |
c66f9d0e | 1134 | u_int bloblen, slen; |
a27002e5 | 1135 | u_int skip = 0; |
2e73a022 | 1136 | int ret = -1; |
94ec8c6b | 1137 | int have_sig = 1; |
a306f2dd | 1138 | |
cbc5abf9 | 1139 | debug3("sign_and_send_pubkey"); |
fee56204 | 1140 | |
43348518 | 1141 | if (key_to_blob(id->key, &blob, &bloblen) == 0) { |
fa08c86b | 1142 | /* we cannot handle this key */ |
cbc5abf9 | 1143 | debug3("sign_and_send_pubkey: cannot handle key"); |
fa08c86b | 1144 | return 0; |
1145 | } | |
a306f2dd | 1146 | /* data to be signed */ |
1147 | buffer_init(&b); | |
33de75a3 | 1148 | if (datafellows & SSH_OLD_SESSIONID) { |
74fc9186 | 1149 | buffer_append(&b, session_id2, session_id2_len); |
2b87da3b | 1150 | skip = session_id2_len; |
33de75a3 | 1151 | } else { |
1152 | buffer_put_string(&b, session_id2, session_id2_len); | |
1153 | skip = buffer_len(&b); | |
74fc9186 | 1154 | } |
a306f2dd | 1155 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
188adeb2 | 1156 | buffer_put_cstring(&b, authctxt->server_user); |
d0c832f3 | 1157 | buffer_put_cstring(&b, |
cbc5abf9 | 1158 | datafellows & SSH_BUG_PKSERVICE ? |
d0c832f3 | 1159 | "ssh-userauth" : |
188adeb2 | 1160 | authctxt->service); |
cbc5abf9 | 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); | |
43348518 | 1166 | buffer_put_cstring(&b, key_ssh_name(id->key)); |
cbc5abf9 | 1167 | } |
a306f2dd | 1168 | buffer_put_string(&b, blob, bloblen); |
a306f2dd | 1169 | |
1170 | /* generate signature */ | |
43348518 | 1171 | ret = identity_sign(id, &signature, &slen, |
fee56204 | 1172 | buffer_ptr(&b), buffer_len(&b)); |
2e73a022 | 1173 | if (ret == -1) { |
1174 | xfree(blob); | |
1175 | buffer_free(&b); | |
1176 | return 0; | |
1177 | } | |
fa08c86b | 1178 | #ifdef DEBUG_PK |
a306f2dd | 1179 | buffer_dump(&b); |
1180 | #endif | |
cbc5abf9 | 1181 | if (datafellows & SSH_BUG_PKSERVICE) { |
d0c832f3 | 1182 | buffer_clear(&b); |
1183 | buffer_append(&b, session_id2, session_id2_len); | |
fee56204 | 1184 | skip = session_id2_len; |
d0c832f3 | 1185 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
188adeb2 | 1186 | buffer_put_cstring(&b, authctxt->server_user); |
1187 | buffer_put_cstring(&b, authctxt->service); | |
94ec8c6b | 1188 | buffer_put_cstring(&b, authctxt->method->name); |
1189 | buffer_put_char(&b, have_sig); | |
cbc5abf9 | 1190 | if (!(datafellows & SSH_BUG_PKAUTH)) |
43348518 | 1191 | buffer_put_cstring(&b, key_ssh_name(id->key)); |
d0c832f3 | 1192 | buffer_put_string(&b, blob, bloblen); |
1193 | } | |
1194 | xfree(blob); | |
fee56204 | 1195 | |
a306f2dd | 1196 | /* append signature */ |
1197 | buffer_put_string(&b, signature, slen); | |
1198 | xfree(signature); | |
1199 | ||
1200 | /* skip session id and packet type */ | |
74fc9186 | 1201 | if (buffer_len(&b) < skip + 1) |
188adeb2 | 1202 | fatal("userauth_pubkey: internal error"); |
74fc9186 | 1203 | buffer_consume(&b, skip + 1); |
a306f2dd | 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); | |
a306f2dd | 1209 | packet_send(); |
2e73a022 | 1210 | |
1211 | return 1; | |
4c8722d9 | 1212 | } |
1213 | ||
396c147e | 1214 | static int |
43348518 | 1215 | send_pubkey_test(Authctxt *authctxt, Identity *id) |
4c8722d9 | 1216 | { |
fee56204 | 1217 | u_char *blob; |
21b30f38 | 1218 | u_int bloblen, have_sig = 0; |
4c8722d9 | 1219 | |
fee56204 | 1220 | debug3("send_pubkey_test"); |
1221 | ||
43348518 | 1222 | if (key_to_blob(id->key, &blob, &bloblen) == 0) { |
fee56204 | 1223 | /* we cannot handle this key */ |
1224 | debug3("send_pubkey_test: cannot handle key"); | |
4c8722d9 | 1225 | return 0; |
1226 | } | |
fee56204 | 1227 | /* register callback for USERAUTH_PK_OK message */ |
fee56204 | 1228 | dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok); |
4c8722d9 | 1229 | |
fee56204 | 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)) | |
43348518 | 1236 | packet_put_cstring(key_ssh_name(id->key)); |
fee56204 | 1237 | packet_put_string(blob, bloblen); |
1238 | xfree(blob); | |
1239 | packet_send(); | |
1240 | return 1; | |
1241 | } | |
1242 | ||
396c147e | 1243 | static Key * |
fee56204 | 1244 | load_identity_file(char *filename) |
1245 | { | |
1246 | Key *private; | |
1247 | char prompt[300], *passphrase; | |
fc231518 | 1248 | int perm_ok, quit, i; |
d156519a | 1249 | struct stat st; |
fee56204 | 1250 | |
d156519a | 1251 | if (stat(filename, &st) < 0) { |
1252 | debug3("no such identity: %s", filename); | |
1253 | return NULL; | |
1254 | } | |
fc231518 | 1255 | private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok); |
1256 | if (!perm_ok) | |
1257 | return NULL; | |
aeaa3d9e | 1258 | if (private == NULL) { |
1259 | if (options.batch_mode) | |
fee56204 | 1260 | return NULL; |
4c8722d9 | 1261 | snprintf(prompt, sizeof prompt, |
184eed6a | 1262 | "Enter passphrase for key '%.100s': ", filename); |
188adeb2 | 1263 | for (i = 0; i < options.number_of_password_prompts; i++) { |
1264 | passphrase = read_passphrase(prompt, 0); | |
1265 | if (strcmp(passphrase, "") != 0) { | |
fc231518 | 1266 | private = key_load_private_type(KEY_UNSPEC, |
1267 | filename, passphrase, NULL, NULL); | |
fee56204 | 1268 | quit = 0; |
188adeb2 | 1269 | } else { |
1270 | debug2("no passphrase given, try next key"); | |
fee56204 | 1271 | quit = 1; |
188adeb2 | 1272 | } |
1273 | memset(passphrase, 0, strlen(passphrase)); | |
1274 | xfree(passphrase); | |
aeaa3d9e | 1275 | if (private != NULL || quit) |
188adeb2 | 1276 | break; |
1277 | debug2("bad passphrase given, try again..."); | |
1278 | } | |
4c8722d9 | 1279 | } |
fee56204 | 1280 | return private; |
1281 | } | |
1282 | ||
43348518 | 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) | |
fee56204 | 1291 | { |
43348518 | 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; | |
52e3daed | 1310 | id = xcalloc(1, sizeof(*id)); |
43348518 | 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) { | |
aff51935 | 1322 | /* agent keys from the config file are preferred */ |
43348518 | 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 | } | |
3a065ed0 | 1333 | if (!found && !options.identities_only) { |
52e3daed | 1334 | id = xcalloc(1, sizeof(*id)); |
43348518 | 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 | } | |
fee56204 | 1356 | } |
1357 | ||
43348518 | 1358 | static void |
1359 | pubkey_cleanup(Authctxt *authctxt) | |
2e73a022 | 1360 | { |
43348518 | 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); | |
2e73a022 | 1373 | } |
a306f2dd | 1374 | } |
1375 | ||
188adeb2 | 1376 | int |
1377 | userauth_pubkey(Authctxt *authctxt) | |
a306f2dd | 1378 | { |
43348518 | 1379 | Identity *id; |
188adeb2 | 1380 | int sent = 0; |
1381 | ||
43348518 | 1382 | while ((id = TAILQ_FIRST(&authctxt->keys))) { |
1383 | if (id->tried++) | |
1384 | return (0); | |
b56e99e2 | 1385 | /* move key to the end of the queue */ |
43348518 | 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; | |
fee56204 | 1404 | } |
fee56204 | 1405 | } |
43348518 | 1406 | if (sent) |
1407 | return (sent); | |
fa08c86b | 1408 | } |
43348518 | 1409 | return (0); |
188adeb2 | 1410 | } |
a306f2dd | 1411 | |
94ec8c6b | 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; | |
6b759005 | 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 | } | |
94ec8c6b | 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(); | |
94ec8c6b | 1438 | |
1439 | dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req); | |
1440 | return 1; | |
1441 | } | |
1442 | ||
1443 | /* | |
f72e01a5 | 1444 | * parse INFO_REQUEST, prompt user and send INFO_RESPONSE |
94ec8c6b | 1445 | */ |
1446 | void | |
7819b5c3 | 1447 | input_userauth_info_req(int type, u_int32_t seq, void *ctxt) |
94ec8c6b | 1448 | { |
1449 | Authctxt *authctxt = ctxt; | |
f72e01a5 | 1450 | char *name, *inst, *lang, *prompt, *response; |
1e3b8b07 | 1451 | u_int num_prompts, i; |
94ec8c6b | 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 | ||
6b759005 | 1459 | authctxt->info_req_seen = 1; |
1460 | ||
94ec8c6b | 1461 | name = packet_get_string(NULL); |
1462 | inst = packet_get_string(NULL); | |
1463 | lang = packet_get_string(NULL); | |
94ec8c6b | 1464 | if (strlen(name) > 0) |
bbe88b6d | 1465 | logit("%s", name); |
94ec8c6b | 1466 | if (strlen(inst) > 0) |
bbe88b6d | 1467 | logit("%s", inst); |
f72e01a5 | 1468 | xfree(name); |
94ec8c6b | 1469 | xfree(inst); |
f72e01a5 | 1470 | xfree(lang); |
94ec8c6b | 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 | ||
5ba55ada | 1482 | debug2("input_userauth_info_req: num_prompts %d", num_prompts); |
94ec8c6b | 1483 | for (i = 0; i < num_prompts; i++) { |
1484 | prompt = packet_get_string(NULL); | |
1485 | echo = packet_get_char(); | |
1486 | ||
1843a425 | 1487 | response = read_passphrase(prompt, echo ? RP_ECHO : 0); |
94ec8c6b | 1488 | |
a6215e53 | 1489 | packet_put_cstring(response); |
94ec8c6b | 1490 | memset(response, 0, strlen(response)); |
1491 | xfree(response); | |
1492 | xfree(prompt); | |
1493 | } | |
95500969 | 1494 | packet_check_eom(); /* done with parsing incoming message. */ |
94ec8c6b | 1495 | |
b4b701be | 1496 | packet_add_padding(64); |
94ec8c6b | 1497 | packet_send(); |
94ec8c6b | 1498 | } |
a306f2dd | 1499 | |
39c00dc2 | 1500 | static int |
d6133f43 | 1501 | ssh_keysign(Key *key, u_char **sigp, u_int *lenp, |
39c00dc2 | 1502 | u_char *data, u_int datalen) |
1503 | { | |
1504 | Buffer b; | |
7091a26b | 1505 | struct stat st; |
39c00dc2 | 1506 | pid_t pid; |
d4826734 | 1507 | int to[2], from[2], status, version = 2; |
39c00dc2 | 1508 | |
6226a8f8 | 1509 | debug2("ssh_keysign called"); |
39c00dc2 | 1510 | |
7091a26b | 1511 | if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) { |
1512 | error("ssh_keysign: no installed: %s", strerror(errno)); | |
1513 | return -1; | |
1514 | } | |
39c00dc2 | 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) { | |
3c33c1b6 | 1530 | permanently_drop_suid(getuid()); |
39c00dc2 | 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)); | |
d4826734 | 1537 | close(from[1]); |
1538 | close(to[0]); | |
a3f69458 | 1539 | execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0); |
39c00dc2 | 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); | |
d4826734 | 1547 | buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */ |
39c00dc2 | 1548 | buffer_put_string(&b, data, datalen); |
d3cbe6f8 | 1549 | if (ssh_msg_send(to[1], version, &b) == -1) |
1550 | fatal("ssh_keysign: couldn't send request"); | |
39c00dc2 | 1551 | |
e987fdbe | 1552 | if (ssh_msg_recv(from[0], &b) < 0) { |
7091a26b | 1553 | error("ssh_keysign: no reply"); |
43f7a4b8 | 1554 | buffer_free(&b); |
39c00dc2 | 1555 | return -1; |
1556 | } | |
39c00dc2 | 1557 | close(from[0]); |
1558 | close(to[1]); | |
1559 | ||
d4826734 | 1560 | while (waitpid(pid, &status, 0) < 0) |
1561 | if (errno != EINTR) | |
1562 | break; | |
39c00dc2 | 1563 | |
7091a26b | 1564 | if (buffer_get_char(&b) != version) { |
1565 | error("ssh_keysign: bad version"); | |
43f7a4b8 | 1566 | buffer_free(&b); |
7091a26b | 1567 | return -1; |
1568 | } | |
1569 | *sigp = buffer_get_string(&b, lenp); | |
43f7a4b8 | 1570 | buffer_free(&b); |
7091a26b | 1571 | |
39c00dc2 | 1572 | return 0; |
1573 | } | |
1574 | ||
8002af61 | 1575 | int |
1576 | userauth_hostbased(Authctxt *authctxt) | |
1577 | { | |
1578 | Key *private = NULL; | |
39c00dc2 | 1579 | Sensitive *sensitive = authctxt->sensitive; |
8002af61 | 1580 | Buffer b; |
1581 | u_char *signature, *blob; | |
7fd1deb2 | 1582 | char *chost, *pkalg, *p, myname[NI_MAXHOST]; |
8dddf799 | 1583 | const char *service; |
8002af61 | 1584 | u_int blen, slen; |
f98c3421 | 1585 | int ok, i, len, found = 0; |
8002af61 | 1586 | |
8002af61 | 1587 | /* check for a useful key */ |
39c00dc2 | 1588 | for (i = 0; i < sensitive->nkeys; i++) { |
1589 | private = sensitive->keys[i]; | |
8002af61 | 1590 | if (private && private->type != KEY_RSA1) { |
1591 | found = 1; | |
1592 | /* we take and free the key */ | |
39c00dc2 | 1593 | sensitive->keys[i] = NULL; |
8002af61 | 1594 | break; |
1595 | } | |
1596 | } | |
1597 | if (!found) { | |
6226a8f8 | 1598 | debug("No more client hostkeys for hostbased authentication."); |
8002af61 | 1599 | return 0; |
1600 | } | |
1601 | if (key_to_blob(private, &blob, &blen) == 0) { | |
1602 | key_free(private); | |
8002af61 | 1603 | return 0; |
1604 | } | |
e8d59b4d | 1605 | /* figure out a name for the client host */ |
7fd1deb2 | 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 | } | |
e8d59b4d | 1616 | if (p == NULL) { |
1617 | error("userauth_hostbased: cannot get local ipaddr/name"); | |
1618 | key_free(private); | |
18fc231c | 1619 | xfree(blob); |
e8d59b4d | 1620 | return 0; |
1621 | } | |
1622 | len = strlen(p) + 2; | |
52e3daed | 1623 | xasprintf(&chost, "%s.", p); |
e8d59b4d | 1624 | debug2("userauth_hostbased: chost %s", chost); |
3e2f2431 | 1625 | xfree(p); |
e8d59b4d | 1626 | |
8dddf799 | 1627 | service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" : |
1628 | authctxt->service; | |
8002af61 | 1629 | pkalg = xstrdup(key_ssh_name(private)); |
1630 | buffer_init(&b); | |
8002af61 | 1631 | /* construct data */ |
8dddf799 | 1632 | buffer_put_string(&b, session_id2, session_id2_len); |
8002af61 | 1633 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
1634 | buffer_put_cstring(&b, authctxt->server_user); | |
8dddf799 | 1635 | buffer_put_cstring(&b, service); |
8002af61 | 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 | |
39c00dc2 | 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)); | |
8002af61 | 1650 | key_free(private); |
1651 | buffer_free(&b); | |
1652 | if (ok != 0) { | |
1653 | error("key_sign failed"); | |
1654 | xfree(chost); | |
1655 | xfree(pkalg); | |
18fc231c | 1656 | xfree(blob); |
8002af61 | 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); | |
18fc231c | 1672 | xfree(blob); |
8002af61 | 1673 | |
1674 | packet_send(); | |
1675 | return 1; | |
1676 | } | |
1677 | ||
5adf6b9a | 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); | |
fbba8bf6 | 1733 | dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, |
1734 | &input_userauth_success_unexpected); | |
5adf6b9a | 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 | } | |
fbba8bf6 | 1747 | dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success); |
5adf6b9a | 1748 | } |
1749 | #endif /* JPAKE */ | |
1750 | ||
188adeb2 | 1751 | /* find auth method */ |
1752 | ||
188adeb2 | 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 | */ | |
396c147e | 1757 | static int |
188adeb2 | 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 | } | |
a306f2dd | 1770 | |
396c147e | 1771 | static Authmethod * |
188adeb2 | 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 | ||
cab80f75 | 1783 | /* XXX internal state */ |
1784 | static Authmethod *current = NULL; | |
1785 | static char *supported = NULL; | |
1786 | static char *preferred = NULL; | |
d6133f43 | 1787 | |
188adeb2 | 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, | |
cd332296 | 1791 | * use a built-in default list. |
2b87da3b | 1792 | */ |
396c147e | 1793 | static Authmethod * |
188adeb2 | 1794 | authmethod_get(char *authlist) |
1795 | { | |
cab80f75 | 1796 | char *name = NULL; |
21b30f38 | 1797 | u_int next; |
2b87da3b | 1798 | |
188adeb2 | 1799 | /* Use a suitable default if we're passed a nil list. */ |
1800 | if (authlist == NULL || strlen(authlist) == 0) | |
cab80f75 | 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; | |
188adeb2 | 1813 | |
cab80f75 | 1814 | for (;;) { |
1815 | if ((name = match_list(preferred, supported, &next)) == NULL) { | |
6226a8f8 | 1816 | debug("No more authentication methods to try."); |
cab80f75 | 1817 | current = NULL; |
1818 | return NULL; | |
1819 | } | |
1820 | preferred += next; | |
94ec8c6b | 1821 | debug3("authmethod_lookup %s", name); |
cab80f75 | 1822 | debug3("remaining preferred: %s", preferred); |
1823 | if ((current = authmethod_lookup(name)) != NULL && | |
1824 | authmethod_is_enabled(current)) { | |
94ec8c6b | 1825 | debug3("authmethod_is_enabled %s", name); |
6226a8f8 | 1826 | debug("Next authentication method: %s", name); |
cab80f75 | 1827 | return current; |
94ec8c6b | 1828 | } |
a306f2dd | 1829 | } |
cab80f75 | 1830 | } |
a22aff1f | 1831 | |
ffb215be | 1832 | static char * |
cab80f75 | 1833 | authmethods_get(void) |
1834 | { | |
1835 | Authmethod *method = NULL; | |
3469eac4 | 1836 | Buffer b; |
1837 | char *list; | |
cab80f75 | 1838 | |
3469eac4 | 1839 | buffer_init(&b); |
cab80f75 | 1840 | for (method = authmethods; method->name != NULL; method++) { |
1841 | if (authmethod_is_enabled(method)) { | |
3469eac4 | 1842 | if (buffer_len(&b) > 0) |
1843 | buffer_append(&b, ",", 1); | |
1844 | buffer_append(&b, method->name, strlen(method->name)); | |
cab80f75 | 1845 | } |
1846 | } | |
3469eac4 | 1847 | buffer_append(&b, "\0", 1); |
1848 | list = xstrdup(buffer_ptr(&b)); | |
1849 | buffer_free(&b); | |
1850 | return list; | |
a306f2dd | 1851 | } |
5adf6b9a | 1852 |