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