blob: da5b71478470657e28be3cba67702774602c6114 [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 Lindstroma3700052001-04-05 23:26:32 +000026RCSID("$OpenBSD: sshconnect2.c,v 1.67 2001/04/05 10:42:56 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
Ben Lindstromf28f6342001-04-04 02:03:04 +000072Kex *xxx_kex = NULL;
73
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000074int
75check_host_key_callback(Key *hostkey)
76{
77 check_host_key(xxx_host, xxx_hostaddr, hostkey,
78 options.user_hostfile2, options.system_hostfile2);
79 return 0;
80}
81
Damien Millereba71ba2000-04-29 23:57:08 +100082void
Damien Miller874d77b2000-10-14 16:23:11 +110083ssh_kex2(char *host, struct sockaddr *hostaddr)
84{
Damien Miller874d77b2000-10-14 16:23:11 +110085 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000086
87 xxx_host = host;
88 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +110089
Damien Millere39cacc2000-11-29 12:18:44 +110090 if (options.ciphers == (char *)-1) {
91 log("No valid ciphers for protocol version 2 given, using defaults.");
92 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +110093 }
94 if (options.ciphers != NULL) {
95 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
96 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
97 }
Damien Millera0ff4662001-03-30 10:49:35 +100098 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
99 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
100 myproposal[PROPOSAL_ENC_ALGS_STOC] =
101 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +1100102 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000103 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller874d77b2000-10-14 16:23:11 +1100104 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
105 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000106 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller874d77b2000-10-14 16:23:11 +1100107 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
108 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000109 if (options.macs != NULL) {
110 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
111 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
112 }
Damien Miller874d77b2000-10-14 16:23:11 +1100113
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000114 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000115 kex = kex_setup(myproposal);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000116 kex->client_version_string=client_version_string;
117 kex->server_version_string=server_version_string;
118 kex->check_host_key=&check_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100119
Ben Lindstromf28f6342001-04-04 02:03:04 +0000120 xxx_kex = kex;
121
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000122 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100123
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000124 session_id2 = kex->session_id;
125 session_id2_len = kex->session_id_len;
126
Damien Miller874d77b2000-10-14 16:23:11 +1100127#ifdef DEBUG_KEXDH
128 /* send 1st encrypted/maced/compressed message */
129 packet_start(SSH2_MSG_IGNORE);
130 packet_put_cstring("markus");
131 packet_send();
132 packet_write_wait();
133#endif
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000134 debug("done: ssh_kex2.");
Damien Millereba71ba2000-04-29 23:57:08 +1000135}
Damien Millerb1715dc2000-05-30 13:44:51 +1000136
Damien Millereba71ba2000-04-29 23:57:08 +1000137/*
138 * Authenticate user
139 */
Damien Miller62cee002000-09-23 17:15:56 +1100140
141typedef struct Authctxt Authctxt;
142typedef struct Authmethod Authmethod;
143
144typedef int sign_cb_fn(
145 Authctxt *authctxt, Key *key,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000146 u_char **sigp, int *lenp, u_char *data, int datalen);
Damien Miller62cee002000-09-23 17:15:56 +1100147
148struct Authctxt {
149 const char *server_user;
150 const char *host;
151 const char *service;
152 AuthenticationConnection *agent;
Damien Miller62cee002000-09-23 17:15:56 +1100153 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100154 int success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000155 char *authlist;
156 Key *last_key;
157 sign_cb_fn *last_key_sign;
158 int last_key_hint;
Damien Miller62cee002000-09-23 17:15:56 +1100159};
160struct Authmethod {
161 char *name; /* string to compare against server's list */
162 int (*userauth)(Authctxt *authctxt);
163 int *enabled; /* flag in option struct that enables method */
164 int *batch_flag; /* flag in option struct that disables method */
165};
166
167void input_userauth_success(int type, int plen, void *ctxt);
168void input_userauth_failure(int type, int plen, void *ctxt);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000169void input_userauth_banner(int type, int plen, void *ctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100170void input_userauth_error(int type, int plen, void *ctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100171void input_userauth_info_req(int type, int plen, void *ctxt);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000172void input_userauth_pk_ok(int type, int plen, void *ctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100173
174int userauth_none(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100175int userauth_pubkey(Authctxt *authctxt);
176int userauth_passwd(Authctxt *authctxt);
Damien Miller874d77b2000-10-14 16:23:11 +1100177int userauth_kbdint(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100178
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000179void userauth(Authctxt *authctxt, char *authlist);
180
181int
182sign_and_send_pubkey(Authctxt *authctxt, Key *k,
183 sign_cb_fn *sign_callback);
184void clear_auth_state(Authctxt *authctxt);
185
Damien Miller874d77b2000-10-14 16:23:11 +1100186Authmethod *authmethod_get(char *authlist);
187Authmethod *authmethod_lookup(const char *name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000188char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100189
190Authmethod authmethods[] = {
191 {"publickey",
192 userauth_pubkey,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100193 &options.pubkey_authentication,
Damien Miller62cee002000-09-23 17:15:56 +1100194 NULL},
195 {"password",
196 userauth_passwd,
197 &options.password_authentication,
198 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100199 {"keyboard-interactive",
200 userauth_kbdint,
201 &options.kbd_interactive_authentication,
202 &options.batch_mode},
203 {"none",
204 userauth_none,
205 NULL,
206 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100207 {NULL, NULL, NULL, NULL}
208};
209
210void
211ssh_userauth2(const char *server_user, char *host)
212{
213 Authctxt authctxt;
214 int type;
215 int plen;
216
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000217 if (options.challenge_reponse_authentication)
218 options.kbd_interactive_authentication = 1;
219
Damien Miller62cee002000-09-23 17:15:56 +1100220 debug("send SSH2_MSG_SERVICE_REQUEST");
221 packet_start(SSH2_MSG_SERVICE_REQUEST);
222 packet_put_cstring("ssh-userauth");
223 packet_send();
224 packet_write_wait();
225 type = packet_read(&plen);
226 if (type != SSH2_MSG_SERVICE_ACCEPT) {
227 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
228 }
229 if (packet_remaining() > 0) {
230 char *reply = packet_get_string(&plen);
231 debug("service_accept: %s", reply);
232 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100233 } else {
234 debug("buggy server: service_accept w/o service");
235 }
236 packet_done();
237 debug("got SSH2_MSG_SERVICE_ACCEPT");
238
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000239 if (options.preferred_authentications == NULL)
240 options.preferred_authentications = authmethods_get();
241
Damien Miller62cee002000-09-23 17:15:56 +1100242 /* setup authentication context */
243 authctxt.agent = ssh_get_authentication_connection();
244 authctxt.server_user = server_user;
245 authctxt.host = host;
246 authctxt.service = "ssh-connection"; /* service name */
247 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100248 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000249 authctxt.authlist = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100250 if (authctxt.method == NULL)
251 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100252
253 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100254 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100255
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000256 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100257 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
258 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000259 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100260 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
261
262 if (authctxt.agent != NULL)
263 ssh_close_authentication_connection(authctxt.agent);
264
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000265 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100266}
267void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000268userauth(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}
291void
Damien Miller62cee002000-09-23 17:15:56 +1100292input_userauth_error(int type, int plen, void *ctxt)
293{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000294 fatal("input_userauth_error: bad message during authentication: "
295 "type %d", type);
296}
297void
298input_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);
Damien Miller62cee002000-09-23 17:15:56 +1100307}
308void
309input_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");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000314 if (authctxt->authlist)
315 xfree(authctxt->authlist);
316 clear_auth_state(authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100317 authctxt->success = 1; /* break out */
318}
319void
320input_userauth_failure(int type, int plen, void *ctxt)
321{
Damien Miller62cee002000-09-23 17:15:56 +1100322 Authctxt *authctxt = ctxt;
323 char *authlist = NULL;
324 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100325
326 if (authctxt == NULL)
327 fatal("input_userauth_failure: no authentication context");
328
Damien Miller874d77b2000-10-14 16:23:11 +1100329 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100330 partial = packet_get_char();
331 packet_done();
332
333 if (partial != 0)
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000334 log("Authenticated with partial success.");
Damien Miller62cee002000-09-23 17:15:56 +1100335 debug("authentications that can continue: %s", authlist);
336
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000337 clear_auth_state(authctxt);
338 userauth(authctxt, authlist);
339}
340void
341input_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;
Ben Lindstromcfccef92001-03-13 04:57:58 +0000347 char *pkalg, *pkblob, *fp;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000348
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);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000362 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000363 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");
372 break;
373 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000374 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 }
Ben Lindstromcfccef92001-03-13 04:57:58 +0000382 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
383 debug2("input_userauth_pk_ok: fp %s", fp);
384 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000385 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
Damien Miller62cee002000-09-23 17:15:56 +1100406}
407
Damien Millereba71ba2000-04-29 23:57:08 +1000408int
Damien Miller874d77b2000-10-14 16:23:11 +1100409userauth_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();
Damien Miller874d77b2000-10-14 16:23:11 +1100417 return 1;
418}
419
420int
Damien Miller62cee002000-09-23 17:15:56 +1100421userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000422{
Damien Millere247cc42000-05-07 12:03:14 +1000423 static int attempt = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000424 char prompt[80];
425 char *password;
426
Damien Millerd3a18572000-06-07 19:55:44 +1000427 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000428 return 0;
429
Damien Millerd3a18572000-06-07 19:55:44 +1000430 if(attempt != 1)
431 error("Permission denied, please try again.");
432
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000433 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100434 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000435 password = read_passphrase(prompt, 0);
436 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100437 packet_put_cstring(authctxt->server_user);
438 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100439 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000440 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000441 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000442 memset(password, 0, strlen(password));
443 xfree(password);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000444 packet_inject_ignore(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000445 packet_send();
Damien Millereba71ba2000-04-29 23:57:08 +1000446 return 1;
447}
448
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000449void
450clear_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
Damien Millerad833b32000-08-23 10:46:23 +1000462int
Damien Miller62cee002000-09-23 17:15:56 +1100463sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
Damien Millereba71ba2000-04-29 23:57:08 +1000464{
465 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000466 u_char *blob, *signature;
Damien Millereba71ba2000-04-29 23:57:08 +1000467 int bloblen, slen;
Damien Miller6536c7d2000-06-22 21:32:31 +1000468 int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000469 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100470 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000471
Ben Lindstromd121f612000-12-03 17:00:47 +0000472 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000473
Damien Miller0bc1bd82000-11-13 22:57:25 +1100474 if (key_to_blob(k, &blob, &bloblen) == 0) {
475 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000476 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100477 return 0;
478 }
Damien Millereba71ba2000-04-29 23:57:08 +1000479 /* data to be signed */
480 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100481 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000482 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000483 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100484 } else {
485 buffer_put_string(&b, session_id2, session_id2_len);
486 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000487 }
Damien Millereba71ba2000-04-29 23:57:08 +1000488 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100489 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000490 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000491 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000492 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100493 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000494 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);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000499 buffer_put_cstring(&b, key_ssh_name(k));
Ben Lindstromd121f612000-12-03 17:00:47 +0000500 }
Damien Millereba71ba2000-04-29 23:57:08 +1000501 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000502
503 /* generate signature */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000504 ret = (*sign_callback)(authctxt, k, &signature, &slen,
505 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000506 if (ret == -1) {
507 xfree(blob);
508 buffer_free(&b);
509 return 0;
510 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100511#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000512 buffer_dump(&b);
513#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000514 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000515 buffer_clear(&b);
516 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000517 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +1000518 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100519 buffer_put_cstring(&b, authctxt->server_user);
520 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100521 buffer_put_cstring(&b, authctxt->method->name);
522 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000523 if (!(datafellows & SSH_BUG_PKAUTH))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000524 buffer_put_cstring(&b, key_ssh_name(k));
Damien Miller30c3d422000-05-09 11:02:59 +1000525 buffer_put_string(&b, blob, bloblen);
526 }
527 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000528
Damien Millereba71ba2000-04-29 23:57:08 +1000529 /* append signature */
530 buffer_put_string(&b, signature, slen);
531 xfree(signature);
532
533 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000534 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100535 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000536 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000537
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);
Damien Millereba71ba2000-04-29 23:57:08 +1000542 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +1000543
544 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000545}
546
547int
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000548send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
549 int hint)
Damien Miller994cf142000-07-21 10:19:44 +1000550{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000551 u_char *blob;
552 int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +1000553
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000554 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");
Damien Miller994cf142000-07-21 10:19:44 +1000559 return 0;
560 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000561 /* 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);
Damien Miller994cf142000-07-21 10:19:44 +1000566
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000567 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
580Key *
581load_identity_file(char *filename)
582{
583 Key *private;
584 char prompt[300], *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000585 int quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +0000586 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000587
Ben Lindstrom329782e2001-03-10 17:08:59 +0000588 if (stat(filename, &st) < 0) {
589 debug3("no such identity: %s", filename);
590 return NULL;
591 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000592 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
593 if (private == NULL) {
594 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000595 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +1000596 snprintf(prompt, sizeof prompt,
Damien Miller0bc1bd82000-11-13 22:57:25 +1100597 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +1100598 for (i = 0; i < options.number_of_password_prompts; i++) {
599 passphrase = read_passphrase(prompt, 0);
600 if (strcmp(passphrase, "") != 0) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000601 private = key_load_private_type(KEY_UNSPEC, filename,
602 passphrase, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000603 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100604 } else {
605 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000606 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +1100607 }
608 memset(passphrase, 0, strlen(passphrase));
609 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000610 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +1100611 break;
612 debug2("bad passphrase given, try again...");
613 }
Damien Miller994cf142000-07-21 10:19:44 +1000614 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000615 return private;
616}
617
618int
619identity_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);
Damien Millerad833b32000-08-23 10:46:23 +1000633 return ret;
634}
635
Ben Lindstrom46c16222000-12-22 01:43:59 +0000636int agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
637 u_char *data, int datalen)
Damien Millerad833b32000-08-23 10:46:23 +1000638{
Damien Miller62cee002000-09-23 17:15:56 +1100639 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
Damien Millerad833b32000-08-23 10:46:23 +1000640}
641
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000642int key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
643 u_char *data, int datalen)
644{
Ben Lindstroma3700052001-04-05 23:26:32 +0000645 return key_sign(key, sigp, lenp, data, datalen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000646}
647
Damien Millerad833b32000-08-23 10:46:23 +1000648int
Damien Miller62cee002000-09-23 17:15:56 +1100649userauth_pubkey_agent(Authctxt *authctxt)
Damien Millerad833b32000-08-23 10:46:23 +1000650{
651 static int called = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100652 int ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000653 char *comment;
654 Key *k;
Damien Millerad833b32000-08-23 10:46:23 +1000655
656 if (called == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100657 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
658 debug2("userauth_pubkey_agent: no keys at all");
Damien Miller62cee002000-09-23 17:15:56 +1100659 called = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000660 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100661 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
Damien Miller62cee002000-09-23 17:15:56 +1100662 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100663 debug2("userauth_pubkey_agent: no more keys");
664 } else {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000665 debug("userauth_pubkey_agent: testing agent key %s", comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100666 xfree(comment);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000667 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
668 if (ret == 0)
669 key_free(k);
Damien Miller62cee002000-09-23 17:15:56 +1100670 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100671 if (ret == 0)
672 debug2("userauth_pubkey_agent: no message sent");
Damien Millerad833b32000-08-23 10:46:23 +1000673 return ret;
Damien Millereba71ba2000-04-29 23:57:08 +1000674}
675
Damien Miller62cee002000-09-23 17:15:56 +1100676int
677userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000678{
Damien Miller62cee002000-09-23 17:15:56 +1100679 static int idx = 0;
680 int sent = 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000681 Key *key;
682 char *filename;
Damien Millereba71ba2000-04-29 23:57:08 +1000683
Damien Miller0bc1bd82000-11-13 22:57:25 +1100684 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) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000690 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 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100705 idx++;
706 }
Damien Miller62cee002000-09-23 17:15:56 +1100707 return sent;
708}
Damien Millereba71ba2000-04-29 23:57:08 +1000709
Damien Miller874d77b2000-10-14 16:23:11 +1100710/*
711 * Send userauth request message specifying keyboard-interactive method.
712 */
713int
714userauth_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();
Damien Miller874d77b2000-10-14 16:23:11 +1100730
731 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
732 return 1;
733}
734
735/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000736 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +1100737 */
738void
739input_userauth_info_req(int type, int plen, void *ctxt)
740{
741 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000742 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000743 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +1100744 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);
Damien Miller874d77b2000-10-14 16:23:11 +1100754 if (strlen(name) > 0)
755 cli_mesg(name);
Damien Miller874d77b2000-10-14 16:23:11 +1100756 if (strlen(inst) > 0)
757 cli_mesg(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000758 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +1100759 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000760 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +1100761
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
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000778 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +1100779 memset(response, 0, strlen(response));
780 xfree(response);
781 xfree(prompt);
782 }
783 packet_done(); /* done with parsing incoming message. */
784
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000785 packet_inject_ignore(64);
Damien Miller874d77b2000-10-14 16:23:11 +1100786 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100787}
Damien Miller62cee002000-09-23 17:15:56 +1100788
789/* find auth method */
790
Damien Miller62cee002000-09-23 17:15:56 +1100791/*
792 * given auth method name, if configurable options permit this method fill
793 * in auth_ident field and return true, otherwise return false.
794 */
795int
796authmethod_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}
808
809Authmethod *
810authmethod_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
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000821/* XXX internal state */
822static Authmethod *current = NULL;
823static char *supported = NULL;
824static char *preferred = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100825/*
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,
Ben Lindstroma3700052001-04-05 23:26:32 +0000828 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +0000829 */
Damien Miller62cee002000-09-23 17:15:56 +1100830Authmethod *
831authmethod_get(char *authlist)
832{
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000833
834 char *name = NULL;
835 int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000836
Damien Miller62cee002000-09-23 17:15:56 +1100837 /* Use a suitable default if we're passed a nil list. */
838 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000839 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +1100840
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000841 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;
Damien Millereba71ba2000-04-29 23:57:08 +1000851
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000852 for (;;) {
853 if ((name = match_list(preferred, supported, &next)) == NULL) {
854 debug("no more auth methods to try");
855 current = NULL;
856 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100857 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000858 preferred += next;
859 debug3("authmethod_lookup %s", name);
860 debug3("remaining preferred: %s", preferred);
861 if ((current = authmethod_lookup(name)) != NULL &&
862 authmethod_is_enabled(current)) {
863 debug3("authmethod_is_enabled %s", name);
864 debug("next auth method to try is %s", name);
865 return current;
866 }
Damien Millereba71ba2000-04-29 23:57:08 +1000867 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000868}
Damien Miller62cee002000-09-23 17:15:56 +1100869
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000870
871#define DELIM ","
872char *
873authmethods_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 }
Damien Miller62cee002000-09-23 17:15:56 +1100885 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000886 return xstrdup(buf);
Damien Millereba71ba2000-04-29 23:57:08 +1000887}