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