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