blob: 4ed39a23ee641eab3990a1748ff334c54708b62f [file] [log] [blame]
Damien Millereba71ba2000-04-29 23:57:08 +10001/*
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.
Damien Millereba71ba2000-04-29 23:57:08 +100012 *
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"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000026RCSID("$OpenBSD: sshconnect2.c,v 1.61 2001/04/03 19:53:29 markus Exp $");
Damien Millereba71ba2000-04-29 23:57:08 +100027
28#include <openssl/bn.h>
Damien Millereba71ba2000-04-29 23:57:08 +100029#include <openssl/md5.h>
30#include <openssl/dh.h>
31#include <openssl/hmac.h>
32
33#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000034#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100035#include "xmalloc.h"
36#include "rsa.h"
37#include "buffer.h"
38#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100039#include "uidswap.h"
40#include "compat.h"
Damien Millereba71ba2000-04-29 23:57:08 +100041#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000042#include "cipher.h"
Damien Millereba71ba2000-04-29 23:57:08 +100043#include "kex.h"
44#include "myproposal.h"
45#include "key.h"
Damien Millereba71ba2000-04-29 23:57:08 +100046#include "sshconnect.h"
47#include "authfile.h"
Damien Miller874d77b2000-10-14 16:23:11 +110048#include "cli.h"
Ben Lindstromdf221392001-03-29 00:36:16 +000049#include "dh.h"
Damien Millerad833b32000-08-23 10:46:23 +100050#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "log.h"
52#include "readconf.h"
53#include "readpass.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000054#include "match.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000055#include "dispatch.h"
Damien Miller874d77b2000-10-14 16:23:11 +110056
Damien Millereba71ba2000-04-29 23:57:08 +100057/* import */
58extern char *client_version_string;
59extern char *server_version_string;
60extern Options options;
61
62/*
63 * SSH2 key exchange
64 */
65
Ben Lindstrom46c16222000-12-22 01:43:59 +000066u_char *session_id2 = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +100067int session_id2_len = 0;
68
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000069char *xxx_host;
70struct sockaddr *xxx_hostaddr;
71
72int
73check_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
Damien Millereba71ba2000-04-29 23:57:08 +100080void
Damien Miller874d77b2000-10-14 16:23:11 +110081ssh_kex2(char *host, struct sockaddr *hostaddr)
82{
Damien Miller874d77b2000-10-14 16:23:11 +110083 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000084
85 xxx_host = host;
86 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +110087
Damien Millere39cacc2000-11-29 12:18:44 +110088 if (options.ciphers == (char *)-1) {
89 log("No valid ciphers for protocol version 2 given, using defaults.");
90 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +110091 }
92 if (options.ciphers != NULL) {
93 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
94 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
95 }
Damien Millera0ff4662001-03-30 10:49:35 +100096 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]);
Damien Miller874d77b2000-10-14 16:23:11 +1100100 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000101 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller874d77b2000-10-14 16:23:11 +1100102 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
103 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000104 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller874d77b2000-10-14 16:23:11 +1100105 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
106 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000107 if (options.macs != NULL) {
108 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
109 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
110 }
Damien Miller874d77b2000-10-14 16:23:11 +1100111
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000112 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;
Damien Miller874d77b2000-10-14 16:23:11 +1100116
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000117 /* start key exchange */
118 dispatch_run(DISPATCH_BLOCK, &kex->newkeys, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100119
120#ifdef DEBUG_KEXDH
121 /* send 1st encrypted/maced/compressed message */
122 packet_start(SSH2_MSG_IGNORE);
123 packet_put_cstring("markus");
124 packet_send();
125 packet_write_wait();
126#endif
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000127 debug("done: ssh_kex2.");
Damien Millereba71ba2000-04-29 23:57:08 +1000128}
Damien Millerb1715dc2000-05-30 13:44:51 +1000129
Damien Millereba71ba2000-04-29 23:57:08 +1000130/*
131 * Authenticate user
132 */
Damien Miller62cee002000-09-23 17:15:56 +1100133
134typedef struct Authctxt Authctxt;
135typedef struct Authmethod Authmethod;
136
137typedef int sign_cb_fn(
138 Authctxt *authctxt, Key *key,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000139 u_char **sigp, int *lenp, u_char *data, int datalen);
Damien Miller62cee002000-09-23 17:15:56 +1100140
141struct Authctxt {
142 const char *server_user;
143 const char *host;
144 const char *service;
145 AuthenticationConnection *agent;
Damien Miller62cee002000-09-23 17:15:56 +1100146 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100147 int success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000148 char *authlist;
149 Key *last_key;
150 sign_cb_fn *last_key_sign;
151 int last_key_hint;
Damien Miller62cee002000-09-23 17:15:56 +1100152};
153struct Authmethod {
154 char *name; /* string to compare against server's list */
155 int (*userauth)(Authctxt *authctxt);
156 int *enabled; /* flag in option struct that enables method */
157 int *batch_flag; /* flag in option struct that disables method */
158};
159
160void input_userauth_success(int type, int plen, void *ctxt);
161void input_userauth_failure(int type, int plen, void *ctxt);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000162void input_userauth_banner(int type, int plen, void *ctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100163void input_userauth_error(int type, int plen, void *ctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100164void input_userauth_info_req(int type, int plen, void *ctxt);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000165void input_userauth_pk_ok(int type, int plen, void *ctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100166
167int userauth_none(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100168int userauth_pubkey(Authctxt *authctxt);
169int userauth_passwd(Authctxt *authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100170int userauth_kbdint(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100171
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000172void userauth(Authctxt *authctxt, char *authlist);
173
174int
175sign_and_send_pubkey(Authctxt *authctxt, Key *k,
176 sign_cb_fn *sign_callback);
177void clear_auth_state(Authctxt *authctxt);
178
Damien Miller874d77b2000-10-14 16:23:11 +1100179Authmethod *authmethod_get(char *authlist);
180Authmethod *authmethod_lookup(const char *name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000181char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100182
183Authmethod authmethods[] = {
184 {"publickey",
185 userauth_pubkey,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100186 &options.pubkey_authentication,
Damien Miller62cee002000-09-23 17:15:56 +1100187 NULL},
188 {"password",
189 userauth_passwd,
190 &options.password_authentication,
191 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100192 {"keyboard-interactive",
193 userauth_kbdint,
194 &options.kbd_interactive_authentication,
195 &options.batch_mode},
196 {"none",
197 userauth_none,
198 NULL,
199 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100200 {NULL, NULL, NULL, NULL}
201};
202
203void
204ssh_userauth2(const char *server_user, char *host)
205{
206 Authctxt authctxt;
207 int type;
208 int plen;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000209 int i;
Damien Miller62cee002000-09-23 17:15:56 +1100210
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000211 if (options.challenge_reponse_authentication)
212 options.kbd_interactive_authentication = 1;
213
Damien Miller62cee002000-09-23 17:15:56 +1100214 debug("send SSH2_MSG_SERVICE_REQUEST");
215 packet_start(SSH2_MSG_SERVICE_REQUEST);
216 packet_put_cstring("ssh-userauth");
217 packet_send();
218 packet_write_wait();
219 type = packet_read(&plen);
220 if (type != SSH2_MSG_SERVICE_ACCEPT) {
221 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
222 }
223 if (packet_remaining() > 0) {
224 char *reply = packet_get_string(&plen);
225 debug("service_accept: %s", reply);
226 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100227 } else {
228 debug("buggy server: service_accept w/o service");
229 }
230 packet_done();
231 debug("got SSH2_MSG_SERVICE_ACCEPT");
232
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000233 if (options.preferred_authentications == NULL)
234 options.preferred_authentications = authmethods_get();
235
Damien Miller62cee002000-09-23 17:15:56 +1100236 /* setup authentication context */
237 authctxt.agent = ssh_get_authentication_connection();
238 authctxt.server_user = server_user;
239 authctxt.host = host;
240 authctxt.service = "ssh-connection"; /* service name */
241 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100242 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000243 authctxt.authlist = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100244 if (authctxt.method == NULL)
245 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100246
247 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100248 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100249
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000250 //dispatch_init(&input_userauth_error);
251 for (i = 50; i <= 254; i++) {
252 dispatch_set(i, &input_userauth_error);
253 }
Damien Miller62cee002000-09-23 17:15:56 +1100254 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
255 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000256 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100257 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
258
259 if (authctxt.agent != NULL)
260 ssh_close_authentication_connection(authctxt.agent);
261
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000262 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100263}
264void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000265userauth(Authctxt *authctxt, char *authlist)
266{
267 if (authlist == NULL) {
268 authlist = authctxt->authlist;
269 } else {
270 if (authctxt->authlist)
271 xfree(authctxt->authlist);
272 authctxt->authlist = authlist;
273 }
274 for (;;) {
275 Authmethod *method = authmethod_get(authlist);
276 if (method == NULL)
277 fatal("Permission denied (%s).", authlist);
278 authctxt->method = method;
279 if (method->userauth(authctxt) != 0) {
280 debug2("we sent a %s packet, wait for reply", method->name);
281 break;
282 } else {
283 debug2("we did not send a packet, disable method");
284 method->enabled = NULL;
285 }
286 }
287}
288void
Damien Miller62cee002000-09-23 17:15:56 +1100289input_userauth_error(int type, int plen, void *ctxt)
290{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000291 fatal("input_userauth_error: bad message during authentication: "
292 "type %d", type);
293}
294void
295input_userauth_banner(int type, int plen, void *ctxt)
296{
297 char *msg, *lang;
298 debug3("input_userauth_banner");
299 msg = packet_get_string(NULL);
300 lang = packet_get_string(NULL);
301 fprintf(stderr, "%s", msg);
302 xfree(msg);
303 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100304}
305void
306input_userauth_success(int type, int plen, void *ctxt)
307{
308 Authctxt *authctxt = ctxt;
309 if (authctxt == NULL)
310 fatal("input_userauth_success: no authentication context");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000311 if (authctxt->authlist)
312 xfree(authctxt->authlist);
313 clear_auth_state(authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100314 authctxt->success = 1; /* break out */
315}
316void
317input_userauth_failure(int type, int plen, void *ctxt)
318{
Damien Miller62cee002000-09-23 17:15:56 +1100319 Authctxt *authctxt = ctxt;
320 char *authlist = NULL;
321 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100322
323 if (authctxt == NULL)
324 fatal("input_userauth_failure: no authentication context");
325
Damien Miller874d77b2000-10-14 16:23:11 +1100326 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100327 partial = packet_get_char();
328 packet_done();
329
330 if (partial != 0)
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000331 log("Authenticated with partial success.");
Damien Miller62cee002000-09-23 17:15:56 +1100332 debug("authentications that can continue: %s", authlist);
333
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000334 clear_auth_state(authctxt);
335 userauth(authctxt, authlist);
336}
337void
338input_userauth_pk_ok(int type, int plen, void *ctxt)
339{
340 Authctxt *authctxt = ctxt;
341 Key *key = NULL;
342 Buffer b;
343 int alen, blen, pktype, sent = 0;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000344 char *pkalg, *pkblob, *fp;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000345
346 if (authctxt == NULL)
347 fatal("input_userauth_pk_ok: no authentication context");
348 if (datafellows & SSH_BUG_PKOK) {
349 /* this is similar to SSH_BUG_PKAUTH */
350 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
351 pkblob = packet_get_string(&blen);
352 buffer_init(&b);
353 buffer_append(&b, pkblob, blen);
354 pkalg = buffer_get_string(&b, &alen);
355 buffer_free(&b);
356 } else {
357 pkalg = packet_get_string(&alen);
358 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000359 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000360 packet_done();
361
362 debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d",
363 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
364
365 do {
366 if (authctxt->last_key == NULL ||
367 authctxt->last_key_sign == NULL) {
368 debug("no last key or no sign cb");
369 break;
370 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000371 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
372 debug("unknown pkalg %s", pkalg);
373 break;
374 }
375 if ((key = key_from_blob(pkblob, blen)) == NULL) {
376 debug("no key from blob. pkalg %s", pkalg);
377 break;
378 }
Ben Lindstromcfccef92001-03-13 04:57:58 +0000379 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
380 debug2("input_userauth_pk_ok: fp %s", fp);
381 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000382 if (!key_equal(key, authctxt->last_key)) {
383 debug("key != last_key");
384 break;
385 }
386 sent = sign_and_send_pubkey(authctxt, key,
387 authctxt->last_key_sign);
388 } while(0);
389
390 if (key != NULL)
391 key_free(key);
392 xfree(pkalg);
393 xfree(pkblob);
394
395 /* unregister */
396 clear_auth_state(authctxt);
397 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
398
399 /* try another method if we did not send a packet*/
400 if (sent == 0)
401 userauth(authctxt, NULL);
402
Damien Miller62cee002000-09-23 17:15:56 +1100403}
404
Damien Millereba71ba2000-04-29 23:57:08 +1000405int
Damien Miller874d77b2000-10-14 16:23:11 +1100406userauth_none(Authctxt *authctxt)
407{
408 /* initial userauth request */
409 packet_start(SSH2_MSG_USERAUTH_REQUEST);
410 packet_put_cstring(authctxt->server_user);
411 packet_put_cstring(authctxt->service);
412 packet_put_cstring(authctxt->method->name);
413 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100414 return 1;
415}
416
417int
Damien Miller62cee002000-09-23 17:15:56 +1100418userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000419{
Damien Millere247cc42000-05-07 12:03:14 +1000420 static int attempt = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000421 char prompt[80];
422 char *password;
423
Damien Millerd3a18572000-06-07 19:55:44 +1000424 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000425 return 0;
426
Damien Millerd3a18572000-06-07 19:55:44 +1000427 if(attempt != 1)
428 error("Permission denied, please try again.");
429
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000430 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100431 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000432 password = read_passphrase(prompt, 0);
433 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100434 packet_put_cstring(authctxt->server_user);
435 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100436 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000437 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000438 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000439 memset(password, 0, strlen(password));
440 xfree(password);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000441 packet_inject_ignore(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000442 packet_send();
Damien Millereba71ba2000-04-29 23:57:08 +1000443 return 1;
444}
445
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000446void
447clear_auth_state(Authctxt *authctxt)
448{
449 /* XXX clear authentication state */
450 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
451 debug3("clear_auth_state: key_free %p", authctxt->last_key);
452 key_free(authctxt->last_key);
453 }
454 authctxt->last_key = NULL;
455 authctxt->last_key_hint = -2;
456 authctxt->last_key_sign = NULL;
457}
458
Damien Millerad833b32000-08-23 10:46:23 +1000459int
Damien Miller62cee002000-09-23 17:15:56 +1100460sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
Damien Millereba71ba2000-04-29 23:57:08 +1000461{
462 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000463 u_char *blob, *signature;
Damien Millereba71ba2000-04-29 23:57:08 +1000464 int bloblen, slen;
Damien Miller6536c7d2000-06-22 21:32:31 +1000465 int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000466 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100467 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000468
Ben Lindstromd121f612000-12-03 17:00:47 +0000469 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000470
Damien Miller0bc1bd82000-11-13 22:57:25 +1100471 if (key_to_blob(k, &blob, &bloblen) == 0) {
472 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000473 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100474 return 0;
475 }
Damien Millereba71ba2000-04-29 23:57:08 +1000476 /* data to be signed */
477 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100478 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000479 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000480 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100481 } else {
482 buffer_put_string(&b, session_id2, session_id2_len);
483 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000484 }
Damien Millereba71ba2000-04-29 23:57:08 +1000485 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100486 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000487 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000488 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000489 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100490 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000491 if (datafellows & SSH_BUG_PKAUTH) {
492 buffer_put_char(&b, have_sig);
493 } else {
494 buffer_put_cstring(&b, authctxt->method->name);
495 buffer_put_char(&b, have_sig);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000496 buffer_put_cstring(&b, key_ssh_name(k));
Ben Lindstromd121f612000-12-03 17:00:47 +0000497 }
Damien Millereba71ba2000-04-29 23:57:08 +1000498 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000499
500 /* generate signature */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000501 ret = (*sign_callback)(authctxt, k, &signature, &slen,
502 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000503 if (ret == -1) {
504 xfree(blob);
505 buffer_free(&b);
506 return 0;
507 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100508#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000509 buffer_dump(&b);
510#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000511 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000512 buffer_clear(&b);
513 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000514 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +1000515 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100516 buffer_put_cstring(&b, authctxt->server_user);
517 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100518 buffer_put_cstring(&b, authctxt->method->name);
519 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000520 if (!(datafellows & SSH_BUG_PKAUTH))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000521 buffer_put_cstring(&b, key_ssh_name(k));
Damien Miller30c3d422000-05-09 11:02:59 +1000522 buffer_put_string(&b, blob, bloblen);
523 }
524 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000525
Damien Millereba71ba2000-04-29 23:57:08 +1000526 /* append signature */
527 buffer_put_string(&b, signature, slen);
528 xfree(signature);
529
530 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000531 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100532 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000533 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000534
535 /* put remaining data from buffer into packet */
536 packet_start(SSH2_MSG_USERAUTH_REQUEST);
537 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
538 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +1000539 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +1000540
541 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000542}
543
544int
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000545send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
546 int hint)
Damien Miller994cf142000-07-21 10:19:44 +1000547{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000548 u_char *blob;
549 int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +1000550
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000551 debug3("send_pubkey_test");
552
553 if (key_to_blob(k, &blob, &bloblen) == 0) {
554 /* we cannot handle this key */
555 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +1000556 return 0;
557 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000558 /* register callback for USERAUTH_PK_OK message */
559 authctxt->last_key_sign = sign_callback;
560 authctxt->last_key_hint = hint;
561 authctxt->last_key = k;
562 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +1000563
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000564 packet_start(SSH2_MSG_USERAUTH_REQUEST);
565 packet_put_cstring(authctxt->server_user);
566 packet_put_cstring(authctxt->service);
567 packet_put_cstring(authctxt->method->name);
568 packet_put_char(have_sig);
569 if (!(datafellows & SSH_BUG_PKAUTH))
570 packet_put_cstring(key_ssh_name(k));
571 packet_put_string(blob, bloblen);
572 xfree(blob);
573 packet_send();
574 return 1;
575}
576
577Key *
578load_identity_file(char *filename)
579{
580 Key *private;
581 char prompt[300], *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000582 int quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +0000583 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000584
Ben Lindstrom329782e2001-03-10 17:08:59 +0000585 if (stat(filename, &st) < 0) {
586 debug3("no such identity: %s", filename);
587 return NULL;
588 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000589 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
590 if (private == NULL) {
591 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000592 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +1000593 snprintf(prompt, sizeof prompt,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100594 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +1100595 for (i = 0; i < options.number_of_password_prompts; i++) {
596 passphrase = read_passphrase(prompt, 0);
597 if (strcmp(passphrase, "") != 0) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000598 private = key_load_private_type(KEY_UNSPEC, filename,
599 passphrase, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000600 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100601 } else {
602 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000603 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +1100604 }
605 memset(passphrase, 0, strlen(passphrase));
606 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000607 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +1100608 break;
609 debug2("bad passphrase given, try again...");
610 }
Damien Miller994cf142000-07-21 10:19:44 +1000611 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000612 return private;
613}
614
615int
616identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
617 u_char *data, int datalen)
618{
619 Key *private;
620 int idx, ret;
621
622 idx = authctxt->last_key_hint;
623 if (idx < 0)
624 return -1;
625 private = load_identity_file(options.identity_files[idx]);
626 if (private == NULL)
627 return -1;
628 ret = key_sign(private, sigp, lenp, data, datalen);
629 key_free(private);
Damien Millerad833b32000-08-23 10:46:23 +1000630 return ret;
631}
632
Ben Lindstrom46c16222000-12-22 01:43:59 +0000633int agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
634 u_char *data, int datalen)
Damien Millerad833b32000-08-23 10:46:23 +1000635{
Damien Miller62cee002000-09-23 17:15:56 +1100636 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
Damien Millerad833b32000-08-23 10:46:23 +1000637}
638
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000639int key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
640 u_char *data, int datalen)
641{
642 return key_sign(key, sigp, lenp, data, datalen);
643}
644
Damien Millerad833b32000-08-23 10:46:23 +1000645int
Damien Miller62cee002000-09-23 17:15:56 +1100646userauth_pubkey_agent(Authctxt *authctxt)
Damien Millerad833b32000-08-23 10:46:23 +1000647{
648 static int called = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100649 int ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000650 char *comment;
651 Key *k;
Damien Millerad833b32000-08-23 10:46:23 +1000652
653 if (called == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100654 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
655 debug2("userauth_pubkey_agent: no keys at all");
Damien Miller62cee002000-09-23 17:15:56 +1100656 called = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000657 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100658 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
Damien Miller62cee002000-09-23 17:15:56 +1100659 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100660 debug2("userauth_pubkey_agent: no more keys");
661 } else {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000662 debug("userauth_pubkey_agent: testing agent key %s", comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100663 xfree(comment);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000664 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
665 if (ret == 0)
666 key_free(k);
Damien Miller62cee002000-09-23 17:15:56 +1100667 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100668 if (ret == 0)
669 debug2("userauth_pubkey_agent: no message sent");
Damien Millerad833b32000-08-23 10:46:23 +1000670 return ret;
Damien Millereba71ba2000-04-29 23:57:08 +1000671}
672
Damien Miller62cee002000-09-23 17:15:56 +1100673int
674userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000675{
Damien Miller62cee002000-09-23 17:15:56 +1100676 static int idx = 0;
677 int sent = 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000678 Key *key;
679 char *filename;
Damien Millereba71ba2000-04-29 23:57:08 +1000680
Damien Miller0bc1bd82000-11-13 22:57:25 +1100681 if (authctxt->agent != NULL) {
682 do {
683 sent = userauth_pubkey_agent(authctxt);
684 } while(!sent && authctxt->agent->howmany > 0);
685 }
686 while (!sent && idx < options.num_identity_files) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000687 key = options.identity_keys[idx];
688 filename = options.identity_files[idx];
689 if (key == NULL) {
690 debug("try privkey: %s", filename);
691 key = load_identity_file(filename);
692 if (key != NULL) {
693 sent = sign_and_send_pubkey(authctxt, key,
694 key_sign_cb);
695 key_free(key);
696 }
697 } else if (key->type != KEY_RSA1) {
698 debug("try pubkey: %s", filename);
699 sent = send_pubkey_test(authctxt, key,
700 identity_sign_cb, idx);
701 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100702 idx++;
703 }
Damien Miller62cee002000-09-23 17:15:56 +1100704 return sent;
705}
Damien Millereba71ba2000-04-29 23:57:08 +1000706
Damien Miller874d77b2000-10-14 16:23:11 +1100707/*
708 * Send userauth request message specifying keyboard-interactive method.
709 */
710int
711userauth_kbdint(Authctxt *authctxt)
712{
713 static int attempt = 0;
714
715 if (attempt++ >= options.number_of_password_prompts)
716 return 0;
717
718 debug2("userauth_kbdint");
719 packet_start(SSH2_MSG_USERAUTH_REQUEST);
720 packet_put_cstring(authctxt->server_user);
721 packet_put_cstring(authctxt->service);
722 packet_put_cstring(authctxt->method->name);
723 packet_put_cstring(""); /* lang */
724 packet_put_cstring(options.kbd_interactive_devices ?
725 options.kbd_interactive_devices : "");
726 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100727
728 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
729 return 1;
730}
731
732/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000733 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +1100734 */
735void
736input_userauth_info_req(int type, int plen, void *ctxt)
737{
738 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000739 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000740 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +1100741 int echo = 0;
742
743 debug2("input_userauth_info_req");
744
745 if (authctxt == NULL)
746 fatal("input_userauth_info_req: no authentication context");
747
748 name = packet_get_string(NULL);
749 inst = packet_get_string(NULL);
750 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +1100751 if (strlen(name) > 0)
752 cli_mesg(name);
Damien Miller874d77b2000-10-14 16:23:11 +1100753 if (strlen(inst) > 0)
754 cli_mesg(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000755 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +1100756 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000757 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +1100758
759 num_prompts = packet_get_int();
760 /*
761 * Begin to build info response packet based on prompts requested.
762 * We commit to providing the correct number of responses, so if
763 * further on we run into a problem that prevents this, we have to
764 * be sure and clean this up and send a correct error response.
765 */
766 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
767 packet_put_int(num_prompts);
768
769 for (i = 0; i < num_prompts; i++) {
770 prompt = packet_get_string(NULL);
771 echo = packet_get_char();
772
773 response = cli_prompt(prompt, echo);
774
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000775 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +1100776 memset(response, 0, strlen(response));
777 xfree(response);
778 xfree(prompt);
779 }
780 packet_done(); /* done with parsing incoming message. */
781
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000782 packet_inject_ignore(64);
Damien Miller874d77b2000-10-14 16:23:11 +1100783 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100784}
Damien Miller62cee002000-09-23 17:15:56 +1100785
786/* find auth method */
787
Damien Miller62cee002000-09-23 17:15:56 +1100788/*
789 * given auth method name, if configurable options permit this method fill
790 * in auth_ident field and return true, otherwise return false.
791 */
792int
793authmethod_is_enabled(Authmethod *method)
794{
795 if (method == NULL)
796 return 0;
797 /* return false if options indicate this method is disabled */
798 if (method->enabled == NULL || *method->enabled == 0)
799 return 0;
800 /* return false if batch mode is enabled but method needs interactive mode */
801 if (method->batch_flag != NULL && *method->batch_flag != 0)
802 return 0;
803 return 1;
804}
805
806Authmethod *
807authmethod_lookup(const char *name)
808{
809 Authmethod *method = NULL;
810 if (name != NULL)
811 for (method = authmethods; method->name != NULL; method++)
812 if (strcmp(name, method->name) == 0)
813 return method;
814 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
815 return NULL;
816}
817
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000818/* XXX internal state */
819static Authmethod *current = NULL;
820static char *supported = NULL;
821static char *preferred = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100822/*
823 * Given the authentication method list sent by the server, return the
824 * next method we should try. If the server initially sends a nil list,
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000825 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +0000826 */
Damien Miller62cee002000-09-23 17:15:56 +1100827Authmethod *
828authmethod_get(char *authlist)
829{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000830
831 char *name = NULL;
832 int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000833
Damien Miller62cee002000-09-23 17:15:56 +1100834 /* Use a suitable default if we're passed a nil list. */
835 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000836 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +1100837
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000838 if (supported == NULL || strcmp(authlist, supported) != 0) {
839 debug3("start over, passed a different list %s", authlist);
840 if (supported != NULL)
841 xfree(supported);
842 supported = xstrdup(authlist);
843 preferred = options.preferred_authentications;
844 debug3("preferred %s", preferred);
845 current = NULL;
846 } else if (current != NULL && authmethod_is_enabled(current))
847 return current;
Damien Millereba71ba2000-04-29 23:57:08 +1000848
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000849 for (;;) {
850 if ((name = match_list(preferred, supported, &next)) == NULL) {
851 debug("no more auth methods to try");
852 current = NULL;
853 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100854 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000855 preferred += next;
856 debug3("authmethod_lookup %s", name);
857 debug3("remaining preferred: %s", preferred);
858 if ((current = authmethod_lookup(name)) != NULL &&
859 authmethod_is_enabled(current)) {
860 debug3("authmethod_is_enabled %s", name);
861 debug("next auth method to try is %s", name);
862 return current;
863 }
Damien Millereba71ba2000-04-29 23:57:08 +1000864 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000865}
Damien Miller62cee002000-09-23 17:15:56 +1100866
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000867
868#define DELIM ","
869char *
870authmethods_get(void)
871{
872 Authmethod *method = NULL;
873 char buf[1024];
874
875 buf[0] = '\0';
876 for (method = authmethods; method->name != NULL; method++) {
877 if (authmethod_is_enabled(method)) {
878 if (buf[0] != '\0')
879 strlcat(buf, DELIM, sizeof buf);
880 strlcat(buf, method->name, sizeof buf);
881 }
Damien Miller62cee002000-09-23 17:15:56 +1100882 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000883 return xstrdup(buf);
Damien Millereba71ba2000-04-29 23:57:08 +1000884}