Home | History | Annotate | Line # | Download | only in dist
sshconnect2.c revision 1.1.1.24
      1  1.1.1.24  christos /* $OpenBSD: sshconnect2.c,v 1.326 2020/09/18 05:23:03 djm Exp $ */
      2       1.1  christos /*
      3       1.1  christos  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
      4       1.1  christos  * Copyright (c) 2008 Damien Miller.  All rights reserved.
      5       1.1  christos  *
      6       1.1  christos  * Redistribution and use in source and binary forms, with or without
      7       1.1  christos  * modification, are permitted provided that the following conditions
      8       1.1  christos  * are met:
      9       1.1  christos  * 1. Redistributions of source code must retain the above copyright
     10       1.1  christos  *    notice, this list of conditions and the following disclaimer.
     11       1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     13       1.1  christos  *    documentation and/or other materials provided with the distribution.
     14       1.1  christos  *
     15       1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16       1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17       1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18       1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19       1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20       1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21       1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22       1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23       1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24       1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25       1.1  christos  */
     26       1.1  christos 
     27       1.1  christos #include <sys/types.h>
     28       1.1  christos #include <sys/socket.h>
     29       1.1  christos #include <sys/wait.h>
     30       1.1  christos #include <sys/queue.h>
     31       1.1  christos #include <sys/stat.h>
     32       1.1  christos 
     33       1.1  christos #include <errno.h>
     34   1.1.1.3      adam #include <fcntl.h>
     35       1.1  christos #include <netdb.h>
     36       1.1  christos #include <stdio.h>
     37       1.1  christos #include <string.h>
     38  1.1.1.22  christos #include <stdarg.h>
     39       1.1  christos #include <signal.h>
     40       1.1  christos #include <pwd.h>
     41       1.1  christos #include <unistd.h>
     42       1.1  christos #include <vis.h>
     43       1.1  christos 
     44       1.1  christos #include "xmalloc.h"
     45       1.1  christos #include "ssh.h"
     46       1.1  christos #include "ssh2.h"
     47  1.1.1.19  christos #include "sshbuf.h"
     48       1.1  christos #include "packet.h"
     49       1.1  christos #include "compat.h"
     50       1.1  christos #include "cipher.h"
     51  1.1.1.19  christos #include "sshkey.h"
     52       1.1  christos #include "kex.h"
     53       1.1  christos #include "myproposal.h"
     54       1.1  christos #include "sshconnect.h"
     55       1.1  christos #include "authfile.h"
     56       1.1  christos #include "dh.h"
     57       1.1  christos #include "authfd.h"
     58       1.1  christos #include "log.h"
     59       1.1  christos #include "misc.h"
     60   1.1.1.9  christos #include "readconf.h"
     61       1.1  christos #include "match.h"
     62       1.1  christos #include "dispatch.h"
     63       1.1  christos #include "canohost.h"
     64       1.1  christos #include "msg.h"
     65       1.1  christos #include "pathnames.h"
     66       1.1  christos #include "uidswap.h"
     67   1.1.1.4  christos #include "hostfile.h"
     68  1.1.1.10  christos #include "ssherr.h"
     69  1.1.1.14  christos #include "utf8.h"
     70  1.1.1.22  christos #include "ssh-sk.h"
     71  1.1.1.22  christos #include "sk-api.h"
     72       1.1  christos 
     73       1.1  christos #ifdef GSSAPI
     74       1.1  christos #include "ssh-gss.h"
     75       1.1  christos #endif
     76       1.1  christos 
     77       1.1  christos /* import */
     78       1.1  christos extern char *client_version_string;
     79       1.1  christos extern char *server_version_string;
     80       1.1  christos extern Options options;
     81       1.1  christos 
     82       1.1  christos /*
     83       1.1  christos  * SSH2 key exchange
     84       1.1  christos  */
     85       1.1  christos 
     86       1.1  christos u_char *session_id2 = NULL;
     87       1.1  christos u_int session_id2_len = 0;
     88       1.1  christos 
     89       1.1  christos char *xxx_host;
     90       1.1  christos struct sockaddr *xxx_hostaddr;
     91       1.1  christos 
     92       1.1  christos static int
     93  1.1.1.17  christos verify_host_key_callback(struct sshkey *hostkey, struct ssh *ssh)
     94       1.1  christos {
     95       1.1  christos 	if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
     96       1.1  christos 		fatal("Host key verification failed.");
     97       1.1  christos 	return 0;
     98       1.1  christos }
     99       1.1  christos 
    100  1.1.1.24  christos /* Returns the first item from a comma-separated algorithm list */
    101  1.1.1.24  christos static char *
    102  1.1.1.24  christos first_alg(const char *algs)
    103  1.1.1.24  christos {
    104  1.1.1.24  christos 	char *ret, *cp;
    105  1.1.1.24  christos 
    106  1.1.1.24  christos 	ret = xstrdup(algs);
    107  1.1.1.24  christos 	if ((cp = strchr(ret, ',')) != NULL)
    108  1.1.1.24  christos 		*cp = '\0';
    109  1.1.1.24  christos 	return ret;
    110  1.1.1.24  christos }
    111  1.1.1.24  christos 
    112   1.1.1.4  christos static char *
    113   1.1.1.4  christos order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
    114   1.1.1.4  christos {
    115  1.1.1.24  christos 	char *oavail = NULL, *avail = NULL, *first = NULL, *last = NULL;
    116  1.1.1.24  christos 	char *alg = NULL, *hostname = NULL, *ret = NULL, *best = NULL;
    117   1.1.1.4  christos 	size_t maxlen;
    118  1.1.1.24  christos 	struct hostkeys *hostkeys = NULL;
    119   1.1.1.4  christos 	int ktype;
    120   1.1.1.5  christos 	u_int i;
    121   1.1.1.4  christos 
    122   1.1.1.4  christos 	/* Find all hostkeys for this hostname */
    123   1.1.1.4  christos 	get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
    124   1.1.1.4  christos 	hostkeys = init_hostkeys();
    125   1.1.1.5  christos 	for (i = 0; i < options.num_user_hostfiles; i++)
    126   1.1.1.5  christos 		load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
    127   1.1.1.5  christos 	for (i = 0; i < options.num_system_hostfiles; i++)
    128   1.1.1.5  christos 		load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
    129   1.1.1.4  christos 
    130  1.1.1.24  christos 	/*
    131  1.1.1.24  christos 	 * If a plain public key exists that matches the type of the best
    132  1.1.1.24  christos 	 * preference HostkeyAlgorithms, then use the whole list as is.
    133  1.1.1.24  christos 	 * Note that we ignore whether the best preference algorithm is a
    134  1.1.1.24  christos 	 * certificate type, as sshconnect.c will downgrade certs to
    135  1.1.1.24  christos 	 * plain keys if necessary.
    136  1.1.1.24  christos 	 */
    137  1.1.1.24  christos 	best = first_alg(options.hostkeyalgorithms);
    138  1.1.1.24  christos 	if (lookup_key_in_hostkeys_by_type(hostkeys,
    139  1.1.1.24  christos 	    sshkey_type_plain(sshkey_type_from_name(best)), NULL)) {
    140  1.1.1.24  christos 		debug3("%s: have matching best-preference key type %s, "
    141  1.1.1.24  christos 		    "using HostkeyAlgorithms verbatim", __func__, best);
    142  1.1.1.24  christos 		ret = xstrdup(options.hostkeyalgorithms);
    143  1.1.1.24  christos 		goto out;
    144  1.1.1.24  christos 	}
    145  1.1.1.24  christos 
    146  1.1.1.24  christos 	/*
    147  1.1.1.24  christos 	 * Otherwise, prefer the host key algorithms that match known keys
    148  1.1.1.24  christos 	 * while keeping the ordering of HostkeyAlgorithms as much as possible.
    149  1.1.1.24  christos 	 */
    150  1.1.1.22  christos 	oavail = avail = xstrdup(options.hostkeyalgorithms);
    151   1.1.1.4  christos 	maxlen = strlen(avail) + 1;
    152   1.1.1.4  christos 	first = xmalloc(maxlen);
    153   1.1.1.4  christos 	last = xmalloc(maxlen);
    154   1.1.1.4  christos 	*first = *last = '\0';
    155   1.1.1.4  christos 
    156   1.1.1.4  christos #define ALG_APPEND(to, from) \
    157   1.1.1.4  christos 	do { \
    158   1.1.1.4  christos 		if (*to != '\0') \
    159   1.1.1.4  christos 			strlcat(to, ",", maxlen); \
    160   1.1.1.4  christos 		strlcat(to, from, maxlen); \
    161   1.1.1.4  christos 	} while (0)
    162   1.1.1.4  christos 
    163   1.1.1.4  christos 	while ((alg = strsep(&avail, ",")) && *alg != '\0') {
    164  1.1.1.10  christos 		if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
    165   1.1.1.4  christos 			fatal("%s: unknown alg %s", __func__, alg);
    166  1.1.1.24  christos 		/*
    167  1.1.1.24  christos 		 * If we have a @cert-authority marker in known_hosts then
    168  1.1.1.24  christos 		 * prefer all certificate algorithms.
    169  1.1.1.24  christos 		 */
    170  1.1.1.24  christos 		if (sshkey_type_is_cert(ktype) &&
    171  1.1.1.24  christos 		    lookup_marker_in_hostkeys(hostkeys, MRK_CA)) {
    172  1.1.1.24  christos 			ALG_APPEND(first, alg);
    173  1.1.1.24  christos 			continue;
    174  1.1.1.24  christos 		}
    175  1.1.1.24  christos 		/* If the key appears in known_hosts then prefer it */
    176   1.1.1.4  christos 		if (lookup_key_in_hostkeys_by_type(hostkeys,
    177  1.1.1.24  christos 		    sshkey_type_plain(ktype), NULL)) {
    178   1.1.1.4  christos 			ALG_APPEND(first, alg);
    179  1.1.1.24  christos 			continue;
    180  1.1.1.24  christos 		}
    181  1.1.1.24  christos 		/* Otherwise, put it last */
    182  1.1.1.24  christos 		ALG_APPEND(last, alg);
    183   1.1.1.4  christos 	}
    184   1.1.1.4  christos #undef ALG_APPEND
    185  1.1.1.10  christos 	xasprintf(&ret, "%s%s%s", first,
    186  1.1.1.10  christos 	    (*first == '\0' || *last == '\0') ? "" : ",", last);
    187   1.1.1.4  christos 	if (*first != '\0')
    188   1.1.1.4  christos 		debug3("%s: prefer hostkeyalgs: %s", __func__, first);
    189   1.1.1.4  christos 
    190  1.1.1.24  christos  out:
    191  1.1.1.24  christos 	free(best);
    192   1.1.1.8  christos 	free(first);
    193   1.1.1.8  christos 	free(last);
    194   1.1.1.8  christos 	free(hostname);
    195   1.1.1.8  christos 	free(oavail);
    196   1.1.1.4  christos 	free_hostkeys(hostkeys);
    197   1.1.1.4  christos 
    198   1.1.1.4  christos 	return ret;
    199   1.1.1.4  christos }
    200   1.1.1.4  christos 
    201       1.1  christos void
    202  1.1.1.20  christos ssh_kex2(struct ssh *ssh, char *host, struct sockaddr *hostaddr, u_short port)
    203       1.1  christos {
    204   1.1.1.9  christos 	char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
    205  1.1.1.19  christos 	char *s, *all_key;
    206  1.1.1.22  christos 	int r, use_known_hosts_order = 0;
    207       1.1  christos 
    208       1.1  christos 	xxx_host = host;
    209       1.1  christos 	xxx_hostaddr = hostaddr;
    210       1.1  christos 
    211  1.1.1.22  christos 	/*
    212  1.1.1.22  christos 	 * If the user has not specified HostkeyAlgorithms, or has only
    213  1.1.1.22  christos 	 * appended or removed algorithms from that list then prefer algorithms
    214  1.1.1.22  christos 	 * that are in the list that are supported by known_hosts keys.
    215  1.1.1.22  christos 	 */
    216  1.1.1.22  christos 	if (options.hostkeyalgorithms == NULL ||
    217  1.1.1.22  christos 	    options.hostkeyalgorithms[0] == '-' ||
    218  1.1.1.22  christos 	    options.hostkeyalgorithms[0] == '+')
    219  1.1.1.22  christos 		use_known_hosts_order = 1;
    220  1.1.1.22  christos 
    221  1.1.1.22  christos 	/* Expand or fill in HostkeyAlgorithms */
    222  1.1.1.22  christos 	all_key = sshkey_alg_list(0, 0, 1, ',');
    223  1.1.1.22  christos 	if (kex_assemble_names(&options.hostkeyalgorithms,
    224  1.1.1.22  christos 	    kex_default_pk_alg(), all_key) != 0)
    225  1.1.1.22  christos 		fatal("%s: kex_assemble_namelist", __func__);
    226  1.1.1.22  christos 	free(all_key);
    227  1.1.1.22  christos 
    228  1.1.1.13  christos 	if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL)
    229  1.1.1.13  christos 		fatal("%s: kex_names_cat", __func__);
    230  1.1.1.13  christos 	myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(s);
    231       1.1  christos 	myproposal[PROPOSAL_ENC_ALGS_CTOS] =
    232  1.1.1.12  christos 	    compat_cipher_proposal(options.ciphers);
    233       1.1  christos 	myproposal[PROPOSAL_ENC_ALGS_STOC] =
    234  1.1.1.12  christos 	    compat_cipher_proposal(options.ciphers);
    235  1.1.1.14  christos 	myproposal[PROPOSAL_COMP_ALGS_CTOS] =
    236  1.1.1.22  christos 	    myproposal[PROPOSAL_COMP_ALGS_STOC] =
    237  1.1.1.22  christos 	    (char *)compression_alg_list(options.compression);
    238  1.1.1.12  christos 	myproposal[PROPOSAL_MAC_ALGS_CTOS] =
    239  1.1.1.12  christos 	    myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
    240  1.1.1.22  christos 	if (use_known_hosts_order) {
    241  1.1.1.22  christos 		/* Query known_hosts and prefer algorithms that appear there */
    242   1.1.1.4  christos 		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
    243   1.1.1.9  christos 		    compat_pkalg_proposal(
    244   1.1.1.9  christos 		    order_hostkeyalgs(host, hostaddr, port));
    245  1.1.1.22  christos 	} else {
    246  1.1.1.22  christos 		/* Use specified HostkeyAlgorithms exactly */
    247  1.1.1.22  christos 		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
    248  1.1.1.22  christos 		    compat_pkalg_proposal(options.hostkeyalgorithms);
    249   1.1.1.4  christos 	}
    250       1.1  christos 
    251   1.1.1.8  christos 	if (options.rekey_limit || options.rekey_interval)
    252  1.1.1.20  christos 		ssh_packet_set_rekey_limits(ssh, options.rekey_limit,
    253  1.1.1.16  christos 		    options.rekey_interval);
    254       1.1  christos 
    255       1.1  christos 	/* start key exchange */
    256  1.1.1.20  christos 	if ((r = kex_setup(ssh, myproposal)) != 0)
    257  1.1.1.10  christos 		fatal("kex_setup: %s", ssh_err(r));
    258   1.1.1.9  christos #ifdef WITH_OPENSSL
    259  1.1.1.20  christos 	ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_client;
    260  1.1.1.20  christos 	ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_client;
    261  1.1.1.20  christos 	ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_client;
    262  1.1.1.20  christos 	ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_client;
    263  1.1.1.20  christos 	ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_client;
    264  1.1.1.20  christos 	ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
    265  1.1.1.20  christos 	ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
    266  1.1.1.20  christos 	ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client;
    267   1.1.1.9  christos #endif
    268  1.1.1.20  christos 	ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client;
    269  1.1.1.20  christos 	ssh->kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_client;
    270  1.1.1.20  christos 	ssh->kex->verify_host_key=&verify_host_key_callback;
    271       1.1  christos 
    272  1.1.1.20  christos 	ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &ssh->kex->done);
    273       1.1  christos 
    274  1.1.1.13  christos 	/* remove ext-info from the KEX proposals for rekeying */
    275  1.1.1.13  christos 	myproposal[PROPOSAL_KEX_ALGS] =
    276  1.1.1.13  christos 	    compat_kex_proposal(options.kex_algorithms);
    277  1.1.1.20  christos 	if ((r = kex_prop2buf(ssh->kex->my, myproposal)) != 0)
    278  1.1.1.13  christos 		fatal("kex_prop2buf: %s", ssh_err(r));
    279   1.1.1.3      adam 
    280  1.1.1.20  christos 	session_id2 = ssh->kex->session_id;
    281  1.1.1.20  christos 	session_id2_len = ssh->kex->session_id_len;
    282       1.1  christos 
    283       1.1  christos #ifdef DEBUG_KEXDH
    284       1.1  christos 	/* send 1st encrypted/maced/compressed message */
    285  1.1.1.19  christos 	if ((r = sshpkt_start(ssh, SSH2_MSG_IGNORE)) != 0 ||
    286  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, "markus")) != 0 ||
    287  1.1.1.19  christos 	    (r = sshpkt_send(ssh)) != 0 ||
    288  1.1.1.19  christos 	    (r = ssh_packet_write_wait(ssh)) != 0)
    289  1.1.1.19  christos 		fatal("%s: %s", __func__, ssh_err(r));
    290       1.1  christos #endif
    291       1.1  christos }
    292       1.1  christos 
    293       1.1  christos /*
    294       1.1  christos  * Authenticate user
    295       1.1  christos  */
    296       1.1  christos 
    297  1.1.1.10  christos typedef struct cauthctxt Authctxt;
    298  1.1.1.10  christos typedef struct cauthmethod Authmethod;
    299       1.1  christos typedef struct identity Identity;
    300       1.1  christos typedef struct idlist Idlist;
    301       1.1  christos 
    302       1.1  christos struct identity {
    303       1.1  christos 	TAILQ_ENTRY(identity) next;
    304  1.1.1.10  christos 	int	agent_fd;		/* >=0 if agent supports key */
    305  1.1.1.10  christos 	struct sshkey	*key;		/* public/private key */
    306       1.1  christos 	char	*filename;		/* comment for agent-only keys */
    307       1.1  christos 	int	tried;
    308       1.1  christos 	int	isprivate;		/* key points to the private key */
    309   1.1.1.7  christos 	int	userprovided;
    310       1.1  christos };
    311       1.1  christos TAILQ_HEAD(idlist, identity);
    312       1.1  christos 
    313  1.1.1.10  christos struct cauthctxt {
    314       1.1  christos 	const char *server_user;
    315       1.1  christos 	const char *local_user;
    316       1.1  christos 	const char *host;
    317       1.1  christos 	const char *service;
    318  1.1.1.10  christos 	struct cauthmethod *method;
    319   1.1.1.3      adam 	sig_atomic_t success;
    320       1.1  christos 	char *authlist;
    321  1.1.1.20  christos #ifdef GSSAPI
    322  1.1.1.20  christos 	/* gssapi */
    323  1.1.1.20  christos 	gss_OID_set gss_supported_mechs;
    324  1.1.1.20  christos 	u_int mech_tried;
    325  1.1.1.20  christos #endif
    326       1.1  christos 	/* pubkey */
    327  1.1.1.10  christos 	struct idlist keys;
    328  1.1.1.10  christos 	int agent_fd;
    329       1.1  christos 	/* hostbased */
    330       1.1  christos 	Sensitive *sensitive;
    331  1.1.1.10  christos 	char *oktypes, *ktypes;
    332  1.1.1.10  christos 	const char *active_ktype;
    333       1.1  christos 	/* kbd-interactive */
    334       1.1  christos 	int info_req_seen;
    335  1.1.1.20  christos 	int attempt_kbdint;
    336  1.1.1.20  christos 	/* password */
    337  1.1.1.20  christos 	int attempt_passwd;
    338       1.1  christos 	/* generic */
    339       1.1  christos 	void *methoddata;
    340       1.1  christos };
    341  1.1.1.10  christos 
    342  1.1.1.10  christos struct cauthmethod {
    343       1.1  christos 	char	*name;		/* string to compare against server's list */
    344  1.1.1.20  christos 	int	(*userauth)(struct ssh *ssh);
    345  1.1.1.20  christos 	void	(*cleanup)(struct ssh *ssh);
    346       1.1  christos 	int	*enabled;	/* flag in option struct that enables method */
    347       1.1  christos 	int	*batch_flag;	/* flag in option struct that disables method */
    348       1.1  christos };
    349       1.1  christos 
    350  1.1.1.20  christos static int input_userauth_service_accept(int, u_int32_t, struct ssh *);
    351  1.1.1.20  christos static int input_userauth_ext_info(int, u_int32_t, struct ssh *);
    352  1.1.1.20  christos static int input_userauth_success(int, u_int32_t, struct ssh *);
    353  1.1.1.20  christos static int input_userauth_failure(int, u_int32_t, struct ssh *);
    354  1.1.1.20  christos static int input_userauth_banner(int, u_int32_t, struct ssh *);
    355  1.1.1.20  christos static int input_userauth_error(int, u_int32_t, struct ssh *);
    356  1.1.1.20  christos static int input_userauth_info_req(int, u_int32_t, struct ssh *);
    357  1.1.1.20  christos static int input_userauth_pk_ok(int, u_int32_t, struct ssh *);
    358  1.1.1.20  christos static int input_userauth_passwd_changereq(int, u_int32_t, struct ssh *);
    359  1.1.1.20  christos 
    360  1.1.1.20  christos static int userauth_none(struct ssh *);
    361  1.1.1.20  christos static int userauth_pubkey(struct ssh *);
    362  1.1.1.20  christos static int userauth_passwd(struct ssh *);
    363  1.1.1.20  christos static int userauth_kbdint(struct ssh *);
    364  1.1.1.20  christos static int userauth_hostbased(struct ssh *);
    365       1.1  christos 
    366       1.1  christos #ifdef GSSAPI
    367  1.1.1.20  christos static int userauth_gssapi(struct ssh *);
    368  1.1.1.20  christos static void userauth_gssapi_cleanup(struct ssh *);
    369  1.1.1.20  christos static int input_gssapi_response(int type, u_int32_t, struct ssh *);
    370  1.1.1.20  christos static int input_gssapi_token(int type, u_int32_t, struct ssh *);
    371  1.1.1.20  christos static int input_gssapi_error(int, u_int32_t, struct ssh *);
    372  1.1.1.20  christos static int input_gssapi_errtok(int, u_int32_t, struct ssh *);
    373       1.1  christos #endif
    374       1.1  christos 
    375  1.1.1.20  christos void	userauth(struct ssh *, char *);
    376       1.1  christos 
    377  1.1.1.20  christos static void pubkey_cleanup(struct ssh *);
    378  1.1.1.20  christos static int sign_and_send_pubkey(struct ssh *ssh, Identity *);
    379       1.1  christos static void pubkey_prepare(Authctxt *);
    380  1.1.1.15  christos static void pubkey_reset(Authctxt *);
    381  1.1.1.17  christos static struct sshkey *load_identity_file(Identity *);
    382       1.1  christos 
    383       1.1  christos static Authmethod *authmethod_get(char *authlist);
    384       1.1  christos static Authmethod *authmethod_lookup(const char *name);
    385       1.1  christos static char *authmethods_get(void);
    386       1.1  christos 
    387       1.1  christos Authmethod authmethods[] = {
    388       1.1  christos #ifdef GSSAPI
    389       1.1  christos 	{"gssapi-with-mic",
    390       1.1  christos 		userauth_gssapi,
    391  1.1.1.20  christos 		userauth_gssapi_cleanup,
    392       1.1  christos 		&options.gss_authentication,
    393       1.1  christos 		NULL},
    394       1.1  christos #endif
    395       1.1  christos 	{"hostbased",
    396       1.1  christos 		userauth_hostbased,
    397       1.1  christos 		NULL,
    398       1.1  christos 		&options.hostbased_authentication,
    399       1.1  christos 		NULL},
    400       1.1  christos 	{"publickey",
    401       1.1  christos 		userauth_pubkey,
    402       1.1  christos 		NULL,
    403       1.1  christos 		&options.pubkey_authentication,
    404       1.1  christos 		NULL},
    405       1.1  christos 	{"keyboard-interactive",
    406       1.1  christos 		userauth_kbdint,
    407       1.1  christos 		NULL,
    408       1.1  christos 		&options.kbd_interactive_authentication,
    409       1.1  christos 		&options.batch_mode},
    410       1.1  christos 	{"password",
    411       1.1  christos 		userauth_passwd,
    412       1.1  christos 		NULL,
    413       1.1  christos 		&options.password_authentication,
    414       1.1  christos 		&options.batch_mode},
    415       1.1  christos 	{"none",
    416       1.1  christos 		userauth_none,
    417       1.1  christos 		NULL,
    418       1.1  christos 		NULL,
    419       1.1  christos 		NULL},
    420       1.1  christos 	{NULL, NULL, NULL, NULL, NULL}
    421       1.1  christos };
    422       1.1  christos 
    423       1.1  christos void
    424  1.1.1.20  christos ssh_userauth2(struct ssh *ssh, const char *local_user,
    425  1.1.1.20  christos     const char *server_user, char *host, Sensitive *sensitive)
    426       1.1  christos {
    427       1.1  christos 	Authctxt authctxt;
    428  1.1.1.13  christos 	int r;
    429       1.1  christos 
    430       1.1  christos 	if (options.challenge_response_authentication)
    431       1.1  christos 		options.kbd_interactive_authentication = 1;
    432       1.1  christos 	if (options.preferred_authentications == NULL)
    433       1.1  christos 		options.preferred_authentications = authmethods_get();
    434       1.1  christos 
    435       1.1  christos 	/* setup authentication context */
    436       1.1  christos 	memset(&authctxt, 0, sizeof(authctxt));
    437       1.1  christos 	authctxt.server_user = server_user;
    438       1.1  christos 	authctxt.local_user = local_user;
    439       1.1  christos 	authctxt.host = host;
    440       1.1  christos 	authctxt.service = "ssh-connection";		/* service name */
    441       1.1  christos 	authctxt.success = 0;
    442       1.1  christos 	authctxt.method = authmethod_lookup("none");
    443       1.1  christos 	authctxt.authlist = NULL;
    444       1.1  christos 	authctxt.methoddata = NULL;
    445       1.1  christos 	authctxt.sensitive = sensitive;
    446  1.1.1.10  christos 	authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
    447       1.1  christos 	authctxt.info_req_seen = 0;
    448  1.1.1.20  christos 	authctxt.attempt_kbdint = 0;
    449  1.1.1.20  christos 	authctxt.attempt_passwd = 0;
    450  1.1.1.20  christos #if GSSAPI
    451  1.1.1.20  christos 	authctxt.gss_supported_mechs = NULL;
    452  1.1.1.20  christos 	authctxt.mech_tried = 0;
    453  1.1.1.20  christos #endif
    454  1.1.1.10  christos 	authctxt.agent_fd = -1;
    455  1.1.1.20  christos 	pubkey_prepare(&authctxt);
    456  1.1.1.20  christos 	if (authctxt.method == NULL) {
    457  1.1.1.20  christos 		fatal("%s: internal error: cannot send userauth none request",
    458  1.1.1.20  christos 		    __func__);
    459  1.1.1.20  christos 	}
    460       1.1  christos 
    461  1.1.1.13  christos 	if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
    462  1.1.1.13  christos 	    (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 ||
    463  1.1.1.13  christos 	    (r = sshpkt_send(ssh)) != 0)
    464  1.1.1.13  christos 		fatal("%s: %s", __func__, ssh_err(r));
    465  1.1.1.13  christos 
    466  1.1.1.17  christos 	ssh->authctxt = &authctxt;
    467  1.1.1.13  christos 	ssh_dispatch_init(ssh, &input_userauth_error);
    468  1.1.1.13  christos 	ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info);
    469  1.1.1.13  christos 	ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept);
    470  1.1.1.17  christos 	ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt.success);	/* loop until success */
    471  1.1.1.20  christos 	pubkey_cleanup(ssh);
    472  1.1.1.17  christos 	ssh->authctxt = NULL;
    473       1.1  christos 
    474  1.1.1.13  christos 	ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
    475       1.1  christos 
    476  1.1.1.15  christos 	if (!authctxt.success)
    477  1.1.1.15  christos 		fatal("Authentication failed.");
    478       1.1  christos 	debug("Authentication succeeded (%s).", authctxt.method->name);
    479       1.1  christos }
    480       1.1  christos 
    481  1.1.1.13  christos /* ARGSUSED */
    482  1.1.1.20  christos static int
    483  1.1.1.17  christos input_userauth_service_accept(int type, u_int32_t seq, struct ssh *ssh)
    484  1.1.1.13  christos {
    485  1.1.1.13  christos 	int r;
    486  1.1.1.13  christos 
    487  1.1.1.13  christos 	if (ssh_packet_remaining(ssh) > 0) {
    488  1.1.1.13  christos 		char *reply;
    489  1.1.1.13  christos 
    490  1.1.1.13  christos 		if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0)
    491  1.1.1.13  christos 			goto out;
    492  1.1.1.13  christos 		debug2("service_accept: %s", reply);
    493  1.1.1.13  christos 		free(reply);
    494  1.1.1.13  christos 	} else {
    495  1.1.1.13  christos 		debug2("buggy server: service_accept w/o service");
    496  1.1.1.13  christos 	}
    497  1.1.1.13  christos 	if ((r = sshpkt_get_end(ssh)) != 0)
    498  1.1.1.13  christos 		goto out;
    499  1.1.1.13  christos 	debug("SSH2_MSG_SERVICE_ACCEPT received");
    500  1.1.1.13  christos 
    501  1.1.1.13  christos 	/* initial userauth request */
    502  1.1.1.20  christos 	userauth_none(ssh);
    503  1.1.1.13  christos 
    504  1.1.1.13  christos 	ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_error);
    505  1.1.1.13  christos 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
    506  1.1.1.13  christos 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
    507  1.1.1.13  christos 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
    508  1.1.1.13  christos 	r = 0;
    509  1.1.1.13  christos  out:
    510  1.1.1.13  christos 	return r;
    511  1.1.1.13  christos }
    512  1.1.1.13  christos 
    513  1.1.1.13  christos /* ARGSUSED */
    514  1.1.1.20  christos static int
    515  1.1.1.17  christos input_userauth_ext_info(int type, u_int32_t seqnr, struct ssh *ssh)
    516  1.1.1.13  christos {
    517  1.1.1.17  christos 	return kex_input_ext_info(type, seqnr, ssh);
    518  1.1.1.13  christos }
    519  1.1.1.13  christos 
    520       1.1  christos void
    521  1.1.1.20  christos userauth(struct ssh *ssh, char *authlist)
    522       1.1  christos {
    523  1.1.1.20  christos 	Authctxt *authctxt = (Authctxt *)ssh->authctxt;
    524  1.1.1.19  christos 
    525       1.1  christos 	if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
    526  1.1.1.20  christos 		authctxt->method->cleanup(ssh);
    527       1.1  christos 
    528   1.1.1.8  christos 	free(authctxt->methoddata);
    529   1.1.1.8  christos 	authctxt->methoddata = NULL;
    530       1.1  christos 	if (authlist == NULL) {
    531       1.1  christos 		authlist = authctxt->authlist;
    532       1.1  christos 	} else {
    533   1.1.1.8  christos 		free(authctxt->authlist);
    534       1.1  christos 		authctxt->authlist = authlist;
    535       1.1  christos 	}
    536       1.1  christos 	for (;;) {
    537       1.1  christos 		Authmethod *method = authmethod_get(authlist);
    538       1.1  christos 		if (method == NULL)
    539  1.1.1.17  christos 			fatal("%s@%s: Permission denied (%s).",
    540  1.1.1.17  christos 			    authctxt->server_user, authctxt->host, authlist);
    541       1.1  christos 		authctxt->method = method;
    542       1.1  christos 
    543       1.1  christos 		/* reset the per method handler */
    544  1.1.1.19  christos 		ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_PER_METHOD_MIN,
    545       1.1  christos 		    SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
    546       1.1  christos 
    547       1.1  christos 		/* and try new method */
    548  1.1.1.20  christos 		if (method->userauth(ssh) != 0) {
    549       1.1  christos 			debug2("we sent a %s packet, wait for reply", method->name);
    550       1.1  christos 			break;
    551       1.1  christos 		} else {
    552       1.1  christos 			debug2("we did not send a packet, disable method");
    553       1.1  christos 			method->enabled = NULL;
    554       1.1  christos 		}
    555       1.1  christos 	}
    556       1.1  christos }
    557       1.1  christos 
    558       1.1  christos /* ARGSUSED */
    559  1.1.1.20  christos static int
    560  1.1.1.17  christos input_userauth_error(int type, u_int32_t seq, struct ssh *ssh)
    561       1.1  christos {
    562  1.1.1.20  christos 	fatal("%s: bad message during authentication: type %d", __func__, type);
    563  1.1.1.10  christos 	return 0;
    564       1.1  christos }
    565       1.1  christos 
    566       1.1  christos /* ARGSUSED */
    567  1.1.1.20  christos static int
    568  1.1.1.17  christos input_userauth_banner(int type, u_int32_t seq, struct ssh *ssh)
    569       1.1  christos {
    570  1.1.1.20  christos 	char *msg = NULL;
    571  1.1.1.20  christos 	size_t len;
    572  1.1.1.20  christos 	int r;
    573       1.1  christos 
    574  1.1.1.14  christos 	debug3("%s", __func__);
    575  1.1.1.20  christos 	if ((r = sshpkt_get_cstring(ssh, &msg, &len)) != 0 ||
    576  1.1.1.20  christos 	    (r = sshpkt_get_cstring(ssh, NULL, NULL)) != 0)
    577  1.1.1.20  christos 		goto out;
    578  1.1.1.14  christos 	if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO)
    579  1.1.1.14  christos 		fmprintf(stderr, "%s", msg);
    580  1.1.1.20  christos 	r = 0;
    581  1.1.1.20  christos  out:
    582  1.1.1.14  christos 	free(msg);
    583  1.1.1.20  christos 	return r;
    584       1.1  christos }
    585       1.1  christos 
    586       1.1  christos /* ARGSUSED */
    587  1.1.1.20  christos static int
    588  1.1.1.17  christos input_userauth_success(int type, u_int32_t seq, struct ssh *ssh)
    589       1.1  christos {
    590  1.1.1.17  christos 	Authctxt *authctxt = ssh->authctxt;
    591   1.1.1.3      adam 
    592       1.1  christos 	if (authctxt == NULL)
    593  1.1.1.20  christos 		fatal("%s: no authentication context", __func__);
    594   1.1.1.8  christos 	free(authctxt->authlist);
    595   1.1.1.8  christos 	authctxt->authlist = NULL;
    596   1.1.1.3      adam 	if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
    597  1.1.1.20  christos 		authctxt->method->cleanup(ssh);
    598   1.1.1.8  christos 	free(authctxt->methoddata);
    599   1.1.1.8  christos 	authctxt->methoddata = NULL;
    600       1.1  christos 	authctxt->success = 1;			/* break out */
    601  1.1.1.10  christos 	return 0;
    602       1.1  christos }
    603       1.1  christos 
    604  1.1.1.20  christos #if 0
    605  1.1.1.20  christos static int
    606  1.1.1.17  christos input_userauth_success_unexpected(int type, u_int32_t seq, struct ssh *ssh)
    607   1.1.1.3      adam {
    608  1.1.1.17  christos 	Authctxt *authctxt = ssh->authctxt;
    609   1.1.1.3      adam 
    610   1.1.1.3      adam 	if (authctxt == NULL)
    611   1.1.1.3      adam 		fatal("%s: no authentication context", __func__);
    612   1.1.1.3      adam 
    613   1.1.1.3      adam 	fatal("Unexpected authentication success during %s.",
    614   1.1.1.3      adam 	    authctxt->method->name);
    615  1.1.1.10  christos 	return 0;
    616   1.1.1.3      adam }
    617  1.1.1.20  christos #endif
    618   1.1.1.3      adam 
    619       1.1  christos /* ARGSUSED */
    620  1.1.1.20  christos static int
    621  1.1.1.17  christos input_userauth_failure(int type, u_int32_t seq, struct ssh *ssh)
    622       1.1  christos {
    623  1.1.1.17  christos 	Authctxt *authctxt = ssh->authctxt;
    624       1.1  christos 	char *authlist = NULL;
    625  1.1.1.19  christos 	u_char partial;
    626       1.1  christos 
    627       1.1  christos 	if (authctxt == NULL)
    628       1.1  christos 		fatal("input_userauth_failure: no authentication context");
    629       1.1  christos 
    630  1.1.1.21  christos 	if (sshpkt_get_cstring(ssh, &authlist, NULL) != 0 ||
    631  1.1.1.21  christos 	    sshpkt_get_u8(ssh, &partial) != 0 ||
    632  1.1.1.21  christos 	    sshpkt_get_end(ssh) != 0)
    633  1.1.1.19  christos 		goto out;
    634       1.1  christos 
    635   1.1.1.8  christos 	if (partial != 0) {
    636  1.1.1.14  christos 		verbose("Authenticated with partial success.");
    637   1.1.1.8  christos 		/* reset state */
    638  1.1.1.15  christos 		pubkey_reset(authctxt);
    639   1.1.1.8  christos 	}
    640       1.1  christos 	debug("Authentications that can continue: %s", authlist);
    641       1.1  christos 
    642  1.1.1.20  christos 	userauth(ssh, authlist);
    643  1.1.1.19  christos 	authlist = NULL;
    644  1.1.1.19  christos  out:
    645  1.1.1.19  christos 	free(authlist);
    646  1.1.1.10  christos 	return 0;
    647       1.1  christos }
    648       1.1  christos 
    649  1.1.1.20  christos /*
    650  1.1.1.20  christos  * Format an identity for logging including filename, key type, fingerprint
    651  1.1.1.20  christos  * and location (agent, etc.). Caller must free.
    652  1.1.1.20  christos  */
    653  1.1.1.20  christos static char *
    654  1.1.1.20  christos format_identity(Identity *id)
    655  1.1.1.20  christos {
    656  1.1.1.20  christos 	char *fp = NULL, *ret = NULL;
    657  1.1.1.22  christos 	const char *note = "";
    658  1.1.1.20  christos 
    659  1.1.1.20  christos 	if (id->key != NULL) {
    660  1.1.1.20  christos 	     fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
    661  1.1.1.20  christos 		    SSH_FP_DEFAULT);
    662  1.1.1.20  christos 	}
    663  1.1.1.22  christos 	if (id->key) {
    664  1.1.1.22  christos 		if ((id->key->flags & SSHKEY_FLAG_EXT) != 0)
    665  1.1.1.22  christos 			note = " token";
    666  1.1.1.22  christos 		else if (sshkey_is_sk(id->key))
    667  1.1.1.22  christos 			note = " authenticator";
    668  1.1.1.22  christos 	}
    669  1.1.1.20  christos 	xasprintf(&ret, "%s %s%s%s%s%s%s",
    670  1.1.1.20  christos 	    id->filename,
    671  1.1.1.20  christos 	    id->key ? sshkey_type(id->key) : "", id->key ? " " : "",
    672  1.1.1.20  christos 	    fp ? fp : "",
    673  1.1.1.22  christos 	    id->userprovided ? " explicit" : "", note,
    674  1.1.1.20  christos 	    id->agent_fd != -1 ? " agent" : "");
    675  1.1.1.20  christos 	free(fp);
    676  1.1.1.20  christos 	return ret;
    677  1.1.1.20  christos }
    678  1.1.1.20  christos 
    679       1.1  christos /* ARGSUSED */
    680  1.1.1.20  christos static int
    681  1.1.1.17  christos input_userauth_pk_ok(int type, u_int32_t seq, struct ssh *ssh)
    682       1.1  christos {
    683  1.1.1.17  christos 	Authctxt *authctxt = ssh->authctxt;
    684  1.1.1.17  christos 	struct sshkey *key = NULL;
    685       1.1  christos 	Identity *id = NULL;
    686  1.1.1.20  christos 	int pktype, found = 0, sent = 0;
    687  1.1.1.19  christos 	size_t blen;
    688  1.1.1.20  christos 	char *pkalg = NULL, *fp = NULL, *ident = NULL;
    689  1.1.1.19  christos 	u_char *pkblob = NULL;
    690  1.1.1.19  christos 	int r;
    691       1.1  christos 
    692       1.1  christos 	if (authctxt == NULL)
    693       1.1  christos 		fatal("input_userauth_pk_ok: no authentication context");
    694  1.1.1.18  christos 
    695  1.1.1.19  christos 	if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
    696  1.1.1.19  christos 	    (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
    697  1.1.1.19  christos 	    (r = sshpkt_get_end(ssh)) != 0)
    698  1.1.1.19  christos 		goto done;
    699       1.1  christos 
    700  1.1.1.19  christos 	if ((pktype = sshkey_type_from_name(pkalg)) == KEY_UNSPEC) {
    701  1.1.1.20  christos 		debug("%s: server sent unknown pkalg %s", __func__, pkalg);
    702       1.1  christos 		goto done;
    703       1.1  christos 	}
    704  1.1.1.19  christos 	if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
    705  1.1.1.19  christos 		debug("no key from blob. pkalg %s: %s", pkalg, ssh_err(r));
    706       1.1  christos 		goto done;
    707       1.1  christos 	}
    708       1.1  christos 	if (key->type != pktype) {
    709       1.1  christos 		error("input_userauth_pk_ok: type mismatch "
    710       1.1  christos 		    "for decoded key (received %d, expected %d)",
    711       1.1  christos 		    key->type, pktype);
    712       1.1  christos 		goto done;
    713       1.1  christos 	}
    714       1.1  christos 
    715       1.1  christos 	/*
    716       1.1  christos 	 * search keys in the reverse order, because last candidate has been
    717       1.1  christos 	 * moved to the end of the queue.  this also avoids confusion by
    718       1.1  christos 	 * duplicate keys
    719       1.1  christos 	 */
    720       1.1  christos 	TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
    721  1.1.1.19  christos 		if (sshkey_equal(key, id->key)) {
    722  1.1.1.20  christos 			found = 1;
    723       1.1  christos 			break;
    724       1.1  christos 		}
    725       1.1  christos 	}
    726  1.1.1.20  christos 	if (!found || id == NULL) {
    727  1.1.1.20  christos 		fp = sshkey_fingerprint(key, options.fingerprint_hash,
    728  1.1.1.20  christos 		    SSH_FP_DEFAULT);
    729  1.1.1.20  christos 		error("%s: server replied with unknown key: %s %s", __func__,
    730  1.1.1.20  christos 		    sshkey_type(key), fp == NULL ? "<ERROR>" : fp);
    731  1.1.1.20  christos 		goto done;
    732  1.1.1.20  christos 	}
    733  1.1.1.20  christos 	ident = format_identity(id);
    734  1.1.1.20  christos 	debug("Server accepts key: %s", ident);
    735  1.1.1.20  christos 	sent = sign_and_send_pubkey(ssh, id);
    736  1.1.1.19  christos 	r = 0;
    737  1.1.1.19  christos  done:
    738  1.1.1.19  christos 	sshkey_free(key);
    739  1.1.1.20  christos 	free(ident);
    740  1.1.1.20  christos 	free(fp);
    741   1.1.1.8  christos 	free(pkalg);
    742   1.1.1.8  christos 	free(pkblob);
    743       1.1  christos 
    744       1.1  christos 	/* try another method if we did not send a packet */
    745  1.1.1.19  christos 	if (r == 0 && sent == 0)
    746  1.1.1.20  christos 		userauth(ssh, NULL);
    747  1.1.1.19  christos 	return r;
    748       1.1  christos }
    749       1.1  christos 
    750       1.1  christos #ifdef GSSAPI
    751  1.1.1.20  christos static int
    752  1.1.1.20  christos userauth_gssapi(struct ssh *ssh)
    753       1.1  christos {
    754  1.1.1.20  christos 	Authctxt *authctxt = (Authctxt *)ssh->authctxt;
    755       1.1  christos 	Gssctxt *gssctxt = NULL;
    756       1.1  christos 	OM_uint32 min;
    757  1.1.1.19  christos 	int r, ok = 0;
    758  1.1.1.20  christos 	gss_OID mech = NULL;
    759       1.1  christos 
    760       1.1  christos 	/* Try one GSSAPI method at a time, rather than sending them all at
    761       1.1  christos 	 * once. */
    762       1.1  christos 
    763  1.1.1.20  christos 	if (authctxt->gss_supported_mechs == NULL)
    764  1.1.1.20  christos 		gss_indicate_mechs(&min, &authctxt->gss_supported_mechs);
    765       1.1  christos 
    766  1.1.1.20  christos 	/* Check to see whether the mechanism is usable before we offer it */
    767  1.1.1.20  christos 	while (authctxt->mech_tried < authctxt->gss_supported_mechs->count &&
    768  1.1.1.20  christos 	    !ok) {
    769  1.1.1.20  christos 		mech = &authctxt->gss_supported_mechs->
    770  1.1.1.20  christos 		    elements[authctxt->mech_tried];
    771       1.1  christos 		/* My DER encoding requires length<128 */
    772  1.1.1.20  christos 		if (mech->length < 128 && ssh_gssapi_check_mechanism(&gssctxt,
    773  1.1.1.20  christos 		    mech, authctxt->host)) {
    774       1.1  christos 			ok = 1; /* Mechanism works */
    775       1.1  christos 		} else {
    776  1.1.1.20  christos 			authctxt->mech_tried++;
    777       1.1  christos 		}
    778       1.1  christos 	}
    779       1.1  christos 
    780  1.1.1.20  christos 	if (!ok || mech == NULL)
    781       1.1  christos 		return 0;
    782       1.1  christos 
    783       1.1  christos 	authctxt->methoddata=(void *)gssctxt;
    784       1.1  christos 
    785  1.1.1.19  christos 	if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
    786  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
    787  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
    788  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
    789  1.1.1.19  christos 	    (r = sshpkt_put_u32(ssh, 1)) != 0 ||
    790  1.1.1.20  christos 	    (r = sshpkt_put_u32(ssh, (mech->length) + 2)) != 0 ||
    791  1.1.1.19  christos 	    (r = sshpkt_put_u8(ssh, SSH_GSS_OIDTYPE)) != 0 ||
    792  1.1.1.20  christos 	    (r = sshpkt_put_u8(ssh, mech->length)) != 0 ||
    793  1.1.1.20  christos 	    (r = sshpkt_put(ssh, mech->elements, mech->length)) != 0 ||
    794  1.1.1.19  christos 	    (r = sshpkt_send(ssh)) != 0)
    795  1.1.1.19  christos 		fatal("%s: %s", __func__, ssh_err(r));
    796  1.1.1.19  christos 
    797  1.1.1.19  christos 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
    798  1.1.1.19  christos 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
    799  1.1.1.19  christos 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
    800  1.1.1.19  christos 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
    801       1.1  christos 
    802  1.1.1.20  christos 	authctxt->mech_tried++; /* Move along to next candidate */
    803       1.1  christos 
    804       1.1  christos 	return 1;
    805       1.1  christos }
    806       1.1  christos 
    807  1.1.1.20  christos static void
    808  1.1.1.20  christos userauth_gssapi_cleanup(struct ssh *ssh)
    809  1.1.1.20  christos {
    810  1.1.1.20  christos 	Authctxt *authctxt = (Authctxt *)ssh->authctxt;
    811  1.1.1.20  christos 	Gssctxt *gssctxt = (Gssctxt *)authctxt->methoddata;
    812  1.1.1.20  christos 
    813  1.1.1.20  christos 	ssh_gssapi_delete_ctx(&gssctxt);
    814  1.1.1.20  christos 	authctxt->methoddata = NULL;
    815  1.1.1.20  christos 
    816  1.1.1.20  christos 	free(authctxt->gss_supported_mechs);
    817  1.1.1.20  christos 	authctxt->gss_supported_mechs = NULL;
    818  1.1.1.20  christos }
    819  1.1.1.20  christos 
    820       1.1  christos static OM_uint32
    821  1.1.1.17  christos process_gssapi_token(struct ssh *ssh, gss_buffer_t recv_tok)
    822       1.1  christos {
    823  1.1.1.17  christos 	Authctxt *authctxt = ssh->authctxt;
    824       1.1  christos 	Gssctxt *gssctxt = authctxt->methoddata;
    825       1.1  christos 	gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
    826       1.1  christos 	gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
    827       1.1  christos 	gss_buffer_desc gssbuf;
    828       1.1  christos 	OM_uint32 status, ms, flags;
    829  1.1.1.19  christos 	int r;
    830       1.1  christos 
    831       1.1  christos 	status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
    832       1.1  christos 	    recv_tok, &send_tok, &flags);
    833       1.1  christos 
    834       1.1  christos 	if (send_tok.length > 0) {
    835  1.1.1.19  christos 		u_char type = GSS_ERROR(status) ?
    836  1.1.1.19  christos 		    SSH2_MSG_USERAUTH_GSSAPI_ERRTOK :
    837  1.1.1.19  christos 		    SSH2_MSG_USERAUTH_GSSAPI_TOKEN;
    838  1.1.1.19  christos 
    839  1.1.1.19  christos 		if ((r = sshpkt_start(ssh, type)) != 0 ||
    840  1.1.1.19  christos 		    (r = sshpkt_put_string(ssh, send_tok.value,
    841  1.1.1.19  christos 		    send_tok.length)) != 0 ||
    842  1.1.1.19  christos 		    (r = sshpkt_send(ssh)) != 0)
    843  1.1.1.19  christos 			fatal("%s: %s", __func__, ssh_err(r));
    844       1.1  christos 
    845       1.1  christos 		gss_release_buffer(&ms, &send_tok);
    846       1.1  christos 	}
    847       1.1  christos 
    848       1.1  christos 	if (status == GSS_S_COMPLETE) {
    849       1.1  christos 		/* send either complete or MIC, depending on mechanism */
    850       1.1  christos 		if (!(flags & GSS_C_INTEG_FLAG)) {
    851  1.1.1.19  christos 			if ((r = sshpkt_start(ssh,
    852  1.1.1.19  christos 			    SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE)) != 0 ||
    853  1.1.1.19  christos 			    (r = sshpkt_send(ssh)) != 0)
    854  1.1.1.19  christos 				fatal("%s: %s", __func__, ssh_err(r));
    855       1.1  christos 		} else {
    856  1.1.1.19  christos 			struct sshbuf *b;
    857  1.1.1.19  christos 
    858  1.1.1.19  christos 			if ((b = sshbuf_new()) == NULL)
    859  1.1.1.19  christos 				fatal("%s: sshbuf_new failed", __func__);
    860  1.1.1.19  christos 			ssh_gssapi_buildmic(b, authctxt->server_user,
    861       1.1  christos 			    authctxt->service, "gssapi-with-mic");
    862       1.1  christos 
    863  1.1.1.19  christos 			if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
    864  1.1.1.19  christos 				fatal("%s: sshbuf_mutable_ptr failed", __func__);
    865  1.1.1.19  christos 			gssbuf.length = sshbuf_len(b);
    866       1.1  christos 
    867       1.1  christos 			status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
    868       1.1  christos 
    869       1.1  christos 			if (!GSS_ERROR(status)) {
    870  1.1.1.19  christos 				if ((r = sshpkt_start(ssh,
    871  1.1.1.19  christos 				    SSH2_MSG_USERAUTH_GSSAPI_MIC)) != 0 ||
    872  1.1.1.19  christos 				    (r = sshpkt_put_string(ssh, mic.value,
    873  1.1.1.19  christos 				    mic.length)) != 0 ||
    874  1.1.1.19  christos 				    (r = sshpkt_send(ssh)) != 0)
    875  1.1.1.19  christos 					fatal("%s: %s", __func__, ssh_err(r));
    876       1.1  christos 			}
    877       1.1  christos 
    878  1.1.1.19  christos 			sshbuf_free(b);
    879       1.1  christos 			gss_release_buffer(&ms, &mic);
    880       1.1  christos 		}
    881       1.1  christos 	}
    882       1.1  christos 
    883       1.1  christos 	return status;
    884       1.1  christos }
    885       1.1  christos 
    886       1.1  christos /* ARGSUSED */
    887  1.1.1.20  christos static int
    888  1.1.1.17  christos input_gssapi_response(int type, u_int32_t plen, struct ssh *ssh)
    889       1.1  christos {
    890  1.1.1.17  christos 	Authctxt *authctxt = ssh->authctxt;
    891       1.1  christos 	Gssctxt *gssctxt;
    892  1.1.1.19  christos 	size_t oidlen;
    893  1.1.1.19  christos 	u_char *oidv = NULL;
    894  1.1.1.19  christos 	int r;
    895       1.1  christos 
    896       1.1  christos 	if (authctxt == NULL)
    897       1.1  christos 		fatal("input_gssapi_response: no authentication context");
    898       1.1  christos 	gssctxt = authctxt->methoddata;
    899       1.1  christos 
    900       1.1  christos 	/* Setup our OID */
    901  1.1.1.19  christos 	if ((r = sshpkt_get_string(ssh, &oidv, &oidlen)) != 0)
    902  1.1.1.19  christos 		goto done;
    903       1.1  christos 
    904       1.1  christos 	if (oidlen <= 2 ||
    905       1.1  christos 	    oidv[0] != SSH_GSS_OIDTYPE ||
    906       1.1  christos 	    oidv[1] != oidlen - 2) {
    907       1.1  christos 		debug("Badly encoded mechanism OID received");
    908  1.1.1.20  christos 		userauth(ssh, NULL);
    909  1.1.1.19  christos 		goto ok;
    910       1.1  christos 	}
    911       1.1  christos 
    912       1.1  christos 	if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
    913       1.1  christos 		fatal("Server returned different OID than expected");
    914       1.1  christos 
    915  1.1.1.19  christos 	if ((r = sshpkt_get_end(ssh)) != 0)
    916  1.1.1.19  christos 		goto done;
    917       1.1  christos 
    918  1.1.1.17  christos 	if (GSS_ERROR(process_gssapi_token(ssh, GSS_C_NO_BUFFER))) {
    919       1.1  christos 		/* Start again with next method on list */
    920       1.1  christos 		debug("Trying to start again");
    921  1.1.1.20  christos 		userauth(ssh, NULL);
    922  1.1.1.19  christos 		goto ok;
    923       1.1  christos 	}
    924  1.1.1.19  christos  ok:
    925  1.1.1.19  christos 	r = 0;
    926  1.1.1.19  christos  done:
    927  1.1.1.19  christos 	free(oidv);
    928  1.1.1.19  christos 	return r;
    929       1.1  christos }
    930       1.1  christos 
    931       1.1  christos /* ARGSUSED */
    932  1.1.1.20  christos static int
    933  1.1.1.17  christos input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh)
    934       1.1  christos {
    935  1.1.1.17  christos 	Authctxt *authctxt = ssh->authctxt;
    936       1.1  christos 	gss_buffer_desc recv_tok;
    937  1.1.1.19  christos 	u_char *p = NULL;
    938  1.1.1.19  christos 	size_t len;
    939       1.1  christos 	OM_uint32 status;
    940  1.1.1.19  christos 	int r;
    941       1.1  christos 
    942       1.1  christos 	if (authctxt == NULL)
    943       1.1  christos 		fatal("input_gssapi_response: no authentication context");
    944       1.1  christos 
    945  1.1.1.19  christos 	if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
    946  1.1.1.19  christos 	    (r = sshpkt_get_end(ssh)) != 0)
    947  1.1.1.19  christos 		goto out;
    948       1.1  christos 
    949  1.1.1.19  christos 	recv_tok.value = p;
    950  1.1.1.19  christos 	recv_tok.length = len;
    951  1.1.1.17  christos 	status = process_gssapi_token(ssh, &recv_tok);
    952       1.1  christos 
    953  1.1.1.19  christos 	/* Start again with the next method in the list */
    954       1.1  christos 	if (GSS_ERROR(status)) {
    955  1.1.1.20  christos 		userauth(ssh, NULL);
    956  1.1.1.19  christos 		/* ok */
    957       1.1  christos 	}
    958  1.1.1.19  christos 	r = 0;
    959  1.1.1.19  christos  out:
    960  1.1.1.19  christos 	free(p);
    961  1.1.1.19  christos 	return r;
    962       1.1  christos }
    963       1.1  christos 
    964       1.1  christos /* ARGSUSED */
    965  1.1.1.20  christos static int
    966  1.1.1.17  christos input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh)
    967       1.1  christos {
    968  1.1.1.17  christos 	Authctxt *authctxt = ssh->authctxt;
    969       1.1  christos 	Gssctxt *gssctxt;
    970       1.1  christos 	gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
    971       1.1  christos 	gss_buffer_desc recv_tok;
    972   1.1.1.8  christos 	OM_uint32 ms;
    973  1.1.1.19  christos 	u_char *p = NULL;
    974  1.1.1.19  christos 	size_t len;
    975  1.1.1.19  christos 	int r;
    976       1.1  christos 
    977       1.1  christos 	if (authctxt == NULL)
    978       1.1  christos 		fatal("input_gssapi_response: no authentication context");
    979       1.1  christos 	gssctxt = authctxt->methoddata;
    980       1.1  christos 
    981  1.1.1.19  christos 	if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
    982  1.1.1.19  christos 	    (r = sshpkt_get_end(ssh)) != 0) {
    983  1.1.1.19  christos 		free(p);
    984  1.1.1.19  christos 		return r;
    985  1.1.1.19  christos 	}
    986       1.1  christos 
    987       1.1  christos 	/* Stick it into GSSAPI and see what it says */
    988  1.1.1.19  christos 	recv_tok.value = p;
    989  1.1.1.19  christos 	recv_tok.length = len;
    990   1.1.1.8  christos 	(void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
    991       1.1  christos 	    &recv_tok, &send_tok, NULL);
    992  1.1.1.19  christos 	free(p);
    993       1.1  christos 	gss_release_buffer(&ms, &send_tok);
    994       1.1  christos 
    995       1.1  christos 	/* Server will be returning a failed packet after this one */
    996  1.1.1.10  christos 	return 0;
    997       1.1  christos }
    998       1.1  christos 
    999       1.1  christos /* ARGSUSED */
   1000  1.1.1.20  christos static int
   1001  1.1.1.17  christos input_gssapi_error(int type, u_int32_t plen, struct ssh *ssh)
   1002       1.1  christos {
   1003  1.1.1.19  christos 	char *msg = NULL;
   1004  1.1.1.19  christos 	char *lang = NULL;
   1005  1.1.1.19  christos 	int r;
   1006       1.1  christos 
   1007  1.1.1.19  christos 	if ((r = sshpkt_get_u32(ssh, NULL)) != 0 ||	/* maj */
   1008  1.1.1.19  christos 	    (r = sshpkt_get_u32(ssh, NULL)) != 0 ||	/* min */
   1009  1.1.1.19  christos 	    (r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
   1010  1.1.1.19  christos 	    (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
   1011  1.1.1.19  christos 		goto out;
   1012  1.1.1.19  christos 	r = sshpkt_get_end(ssh);
   1013       1.1  christos 	debug("Server GSSAPI Error:\n%s", msg);
   1014  1.1.1.19  christos  out:
   1015   1.1.1.8  christos 	free(msg);
   1016   1.1.1.8  christos 	free(lang);
   1017  1.1.1.19  christos 	return r;
   1018       1.1  christos }
   1019       1.1  christos #endif /* GSSAPI */
   1020       1.1  christos 
   1021  1.1.1.20  christos static int
   1022  1.1.1.20  christos userauth_none(struct ssh *ssh)
   1023       1.1  christos {
   1024  1.1.1.20  christos 	Authctxt *authctxt = (Authctxt *)ssh->authctxt;
   1025  1.1.1.19  christos 	int r;
   1026  1.1.1.19  christos 
   1027       1.1  christos 	/* initial userauth request */
   1028  1.1.1.19  christos 	if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
   1029  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
   1030  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
   1031  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
   1032  1.1.1.19  christos 	    (r = sshpkt_send(ssh)) != 0)
   1033  1.1.1.19  christos 		fatal("%s: %s", __func__, ssh_err(r));
   1034       1.1  christos 	return 1;
   1035       1.1  christos }
   1036       1.1  christos 
   1037  1.1.1.20  christos static int
   1038  1.1.1.20  christos userauth_passwd(struct ssh *ssh)
   1039       1.1  christos {
   1040  1.1.1.20  christos 	Authctxt *authctxt = (Authctxt *)ssh->authctxt;
   1041  1.1.1.20  christos 	char *password, *prompt = NULL;
   1042   1.1.1.3      adam 	const char *host = options.host_key_alias ?  options.host_key_alias :
   1043   1.1.1.3      adam 	    authctxt->host;
   1044  1.1.1.19  christos 	int r;
   1045       1.1  christos 
   1046  1.1.1.20  christos 	if (authctxt->attempt_passwd++ >= options.number_of_password_prompts)
   1047       1.1  christos 		return 0;
   1048       1.1  christos 
   1049  1.1.1.20  christos 	if (authctxt->attempt_passwd != 1)
   1050       1.1  christos 		error("Permission denied, please try again.");
   1051       1.1  christos 
   1052  1.1.1.20  christos 	xasprintf(&prompt, "%s@%s's password: ", authctxt->server_user, host);
   1053       1.1  christos 	password = read_passphrase(prompt, 0);
   1054  1.1.1.19  christos 	if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
   1055  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
   1056  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
   1057  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
   1058  1.1.1.19  christos 	    (r = sshpkt_put_u8(ssh, 0)) != 0 ||
   1059  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, password)) != 0 ||
   1060  1.1.1.19  christos 	    (r = sshpkt_add_padding(ssh, 64)) != 0 ||
   1061  1.1.1.19  christos 	    (r = sshpkt_send(ssh)) != 0)
   1062  1.1.1.19  christos 		fatal("%s: %s", __func__, ssh_err(r));
   1063  1.1.1.19  christos 
   1064  1.1.1.20  christos 	free(prompt);
   1065  1.1.1.20  christos 	if (password != NULL)
   1066  1.1.1.19  christos 		freezero(password, strlen(password));
   1067       1.1  christos 
   1068  1.1.1.19  christos 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
   1069       1.1  christos 	    &input_userauth_passwd_changereq);
   1070       1.1  christos 
   1071       1.1  christos 	return 1;
   1072       1.1  christos }
   1073       1.1  christos 
   1074       1.1  christos /*
   1075       1.1  christos  * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
   1076       1.1  christos  */
   1077       1.1  christos /* ARGSUSED */
   1078  1.1.1.20  christos static int
   1079  1.1.1.17  christos input_userauth_passwd_changereq(int type, u_int32_t seqnr, struct ssh *ssh)
   1080       1.1  christos {
   1081  1.1.1.17  christos 	Authctxt *authctxt = ssh->authctxt;
   1082  1.1.1.19  christos 	char *info = NULL, *lang = NULL, *password = NULL, *retype = NULL;
   1083  1.1.1.17  christos 	char prompt[256];
   1084  1.1.1.16  christos 	const char *host;
   1085  1.1.1.19  christos 	int r;
   1086       1.1  christos 
   1087       1.1  christos 	debug2("input_userauth_passwd_changereq");
   1088       1.1  christos 
   1089       1.1  christos 	if (authctxt == NULL)
   1090       1.1  christos 		fatal("input_userauth_passwd_changereq: "
   1091       1.1  christos 		    "no authentication context");
   1092  1.1.1.16  christos 	host = options.host_key_alias ? options.host_key_alias : authctxt->host;
   1093       1.1  christos 
   1094  1.1.1.19  christos 	if ((r = sshpkt_get_cstring(ssh, &info, NULL)) != 0 ||
   1095  1.1.1.19  christos 	    (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
   1096  1.1.1.19  christos 		goto out;
   1097       1.1  christos 	if (strlen(info) > 0)
   1098       1.1  christos 		logit("%s", info);
   1099  1.1.1.19  christos 	if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
   1100  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
   1101  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
   1102  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
   1103  1.1.1.19  christos 	    (r = sshpkt_put_u8(ssh, 1)) != 0)	/* additional info */
   1104  1.1.1.19  christos 		goto out;
   1105  1.1.1.19  christos 
   1106       1.1  christos 	snprintf(prompt, sizeof(prompt),
   1107       1.1  christos 	    "Enter %.30s@%.128s's old password: ",
   1108   1.1.1.3      adam 	    authctxt->server_user, host);
   1109       1.1  christos 	password = read_passphrase(prompt, 0);
   1110  1.1.1.19  christos 	if ((r = sshpkt_put_cstring(ssh, password)) != 0)
   1111  1.1.1.19  christos 		goto out;
   1112  1.1.1.19  christos 
   1113  1.1.1.19  christos 	freezero(password, strlen(password));
   1114       1.1  christos 	password = NULL;
   1115       1.1  christos 	while (password == NULL) {
   1116       1.1  christos 		snprintf(prompt, sizeof(prompt),
   1117       1.1  christos 		    "Enter %.30s@%.128s's new password: ",
   1118   1.1.1.3      adam 		    authctxt->server_user, host);
   1119       1.1  christos 		password = read_passphrase(prompt, RP_ALLOW_EOF);
   1120       1.1  christos 		if (password == NULL) {
   1121       1.1  christos 			/* bail out */
   1122  1.1.1.19  christos 			r = 0;
   1123  1.1.1.19  christos 			goto out;
   1124       1.1  christos 		}
   1125       1.1  christos 		snprintf(prompt, sizeof(prompt),
   1126       1.1  christos 		    "Retype %.30s@%.128s's new password: ",
   1127   1.1.1.3      adam 		    authctxt->server_user, host);
   1128       1.1  christos 		retype = read_passphrase(prompt, 0);
   1129       1.1  christos 		if (strcmp(password, retype) != 0) {
   1130  1.1.1.19  christos 			freezero(password, strlen(password));
   1131       1.1  christos 			logit("Mismatch; try again, EOF to quit.");
   1132       1.1  christos 			password = NULL;
   1133       1.1  christos 		}
   1134  1.1.1.19  christos 		freezero(retype, strlen(retype));
   1135       1.1  christos 	}
   1136  1.1.1.19  christos 	if ((r = sshpkt_put_cstring(ssh, password)) != 0 ||
   1137  1.1.1.19  christos 	    (r = sshpkt_add_padding(ssh, 64)) != 0 ||
   1138  1.1.1.19  christos 	    (r = sshpkt_send(ssh)) != 0)
   1139  1.1.1.19  christos 		goto out;
   1140       1.1  christos 
   1141  1.1.1.19  christos 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
   1142       1.1  christos 	    &input_userauth_passwd_changereq);
   1143  1.1.1.19  christos 	r = 0;
   1144  1.1.1.19  christos  out:
   1145  1.1.1.19  christos 	if (password)
   1146  1.1.1.19  christos 		freezero(password, strlen(password));
   1147  1.1.1.19  christos 	free(info);
   1148  1.1.1.19  christos 	free(lang);
   1149  1.1.1.19  christos 	return r;
   1150  1.1.1.13  christos }
   1151  1.1.1.13  christos 
   1152  1.1.1.18  christos /*
   1153  1.1.1.19  christos  * Select an algorithm for publickey signatures.
   1154  1.1.1.19  christos  * Returns algorithm (caller must free) or NULL if no mutual algorithm found.
   1155  1.1.1.19  christos  *
   1156  1.1.1.19  christos  * Call with ssh==NULL to ignore server-sig-algs extension list and
   1157  1.1.1.19  christos  * only attempt with the key's base signature type.
   1158  1.1.1.18  christos  */
   1159  1.1.1.19  christos static char *
   1160  1.1.1.19  christos key_sig_algorithm(struct ssh *ssh, const struct sshkey *key)
   1161  1.1.1.18  christos {
   1162  1.1.1.19  christos 	char *allowed, *oallowed, *cp, *tmp, *alg = NULL;
   1163  1.1.1.18  christos 
   1164  1.1.1.19  christos 	/*
   1165  1.1.1.19  christos 	 * The signature algorithm will only differ from the key algorithm
   1166  1.1.1.19  christos 	 * for RSA keys/certs and when the server advertises support for
   1167  1.1.1.19  christos 	 * newer (SHA2) algorithms.
   1168  1.1.1.19  christos 	 */
   1169  1.1.1.19  christos 	if (ssh == NULL || ssh->kex->server_sig_algs == NULL ||
   1170  1.1.1.20  christos 	    (key->type != KEY_RSA && key->type != KEY_RSA_CERT) ||
   1171  1.1.1.20  christos 	    (key->type == KEY_RSA_CERT && (datafellows & SSH_BUG_SIGTYPE))) {
   1172  1.1.1.19  christos 		/* Filter base key signature alg against our configuration */
   1173  1.1.1.19  christos 		return match_list(sshkey_ssh_name(key),
   1174  1.1.1.19  christos 		    options.pubkey_key_types, NULL);
   1175  1.1.1.18  christos 	}
   1176  1.1.1.19  christos 
   1177  1.1.1.19  christos 	/*
   1178  1.1.1.19  christos 	 * For RSA keys/certs, since these might have a different sig type:
   1179  1.1.1.19  christos 	 * find the first entry in PubkeyAcceptedKeyTypes of the right type
   1180  1.1.1.19  christos 	 * that also appears in the supported signature algorithms list from
   1181  1.1.1.19  christos 	 * the server.
   1182  1.1.1.19  christos 	 */
   1183  1.1.1.19  christos 	oallowed = allowed = xstrdup(options.pubkey_key_types);
   1184  1.1.1.19  christos 	while ((cp = strsep(&allowed, ",")) != NULL) {
   1185  1.1.1.19  christos 		if (sshkey_type_from_name(cp) != key->type)
   1186  1.1.1.19  christos 			continue;
   1187  1.1.1.24  christos 		tmp = match_list(sshkey_sigalg_by_name(cp),
   1188  1.1.1.24  christos 		    ssh->kex->server_sig_algs, NULL);
   1189  1.1.1.19  christos 		if (tmp != NULL)
   1190  1.1.1.19  christos 			alg = xstrdup(cp);
   1191  1.1.1.19  christos 		free(tmp);
   1192  1.1.1.19  christos 		if (alg != NULL)
   1193  1.1.1.19  christos 			break;
   1194  1.1.1.19  christos 	}
   1195  1.1.1.19  christos 	free(oallowed);
   1196  1.1.1.19  christos 	return alg;
   1197  1.1.1.18  christos }
   1198  1.1.1.18  christos 
   1199       1.1  christos static int
   1200  1.1.1.10  christos identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
   1201  1.1.1.19  christos     const u_char *data, size_t datalen, u_int compat, const char *alg)
   1202       1.1  christos {
   1203  1.1.1.22  christos 	struct sshkey *sign_key = NULL, *prv = NULL;
   1204  1.1.1.22  christos 	int r = SSH_ERR_INTERNAL_ERROR;
   1205  1.1.1.22  christos 	struct notifier_ctx *notifier = NULL;
   1206  1.1.1.24  christos 	char *fp = NULL, *pin = NULL, *prompt = NULL;
   1207  1.1.1.22  christos 
   1208  1.1.1.22  christos 	*sigp = NULL;
   1209  1.1.1.22  christos 	*lenp = 0;
   1210       1.1  christos 
   1211  1.1.1.19  christos 	/* The agent supports this key. */
   1212  1.1.1.18  christos 	if (id->key != NULL && id->agent_fd != -1) {
   1213  1.1.1.19  christos 		return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
   1214  1.1.1.19  christos 		    data, datalen, alg, compat);
   1215  1.1.1.18  christos 	}
   1216  1.1.1.10  christos 
   1217       1.1  christos 	/*
   1218  1.1.1.19  christos 	 * We have already loaded the private key or the private key is
   1219  1.1.1.19  christos 	 * stored in external hardware.
   1220       1.1  christos 	 */
   1221  1.1.1.16  christos 	if (id->key != NULL &&
   1222  1.1.1.19  christos 	    (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))) {
   1223  1.1.1.22  christos 		sign_key = id->key;
   1224  1.1.1.22  christos 	} else {
   1225  1.1.1.22  christos 		/* Load the private key from the file. */
   1226  1.1.1.22  christos 		if ((prv = load_identity_file(id)) == NULL)
   1227  1.1.1.22  christos 			return SSH_ERR_KEY_NOT_FOUND;
   1228  1.1.1.22  christos 		if (id->key != NULL && !sshkey_equal_public(prv, id->key)) {
   1229  1.1.1.22  christos 			error("%s: private key %s contents do not match public",
   1230  1.1.1.22  christos 			   __func__, id->filename);
   1231  1.1.1.22  christos 			r = SSH_ERR_KEY_NOT_FOUND;
   1232  1.1.1.22  christos 			goto out;
   1233  1.1.1.22  christos 		}
   1234  1.1.1.22  christos 		sign_key = prv;
   1235  1.1.1.24  christos 		if (sshkey_is_sk(sign_key)) {
   1236  1.1.1.24  christos 			if ((sign_key->sk_flags &
   1237  1.1.1.24  christos 			    SSH_SK_USER_VERIFICATION_REQD)) {
   1238  1.1.1.24  christos 				xasprintf(&prompt, "Enter PIN for %s key %s: ",
   1239  1.1.1.24  christos 				    sshkey_type(sign_key), id->filename);
   1240  1.1.1.24  christos 				pin = read_passphrase(prompt, 0);
   1241  1.1.1.24  christos 			}
   1242  1.1.1.24  christos 			if ((sign_key->sk_flags & SSH_SK_USER_PRESENCE_REQD)) {
   1243  1.1.1.24  christos 				/* XXX should batch mode just skip these? */
   1244  1.1.1.24  christos 				if ((fp = sshkey_fingerprint(sign_key,
   1245  1.1.1.24  christos 				    options.fingerprint_hash,
   1246  1.1.1.24  christos 				    SSH_FP_DEFAULT)) == NULL)
   1247  1.1.1.24  christos 					fatal("%s: fingerprint", __func__);
   1248  1.1.1.24  christos 				notifier = notify_start(options.batch_mode,
   1249  1.1.1.24  christos 				    "Confirm user presence for key %s %s",
   1250  1.1.1.24  christos 				    sshkey_type(sign_key), fp);
   1251  1.1.1.24  christos 				free(fp);
   1252  1.1.1.24  christos 			}
   1253  1.1.1.22  christos 		}
   1254  1.1.1.22  christos 	}
   1255  1.1.1.22  christos 	if ((r = sshkey_sign(sign_key, sigp, lenp, data, datalen,
   1256  1.1.1.24  christos 	    alg, options.sk_provider, pin, compat)) != 0) {
   1257  1.1.1.22  christos 		debug("%s: sshkey_sign: %s", __func__, ssh_err(r));
   1258  1.1.1.22  christos 		goto out;
   1259  1.1.1.19  christos 	}
   1260  1.1.1.22  christos 	/*
   1261  1.1.1.22  christos 	 * PKCS#11 tokens may not support all signature algorithms,
   1262  1.1.1.22  christos 	 * so check what we get back.
   1263  1.1.1.22  christos 	 */
   1264  1.1.1.22  christos 	if ((r = sshkey_check_sigtype(*sigp, *lenp, alg)) != 0) {
   1265  1.1.1.22  christos 		debug("%s: sshkey_check_sigtype: %s", __func__, ssh_err(r));
   1266  1.1.1.22  christos 		goto out;
   1267  1.1.1.17  christos 	}
   1268  1.1.1.22  christos 	/* success */
   1269  1.1.1.22  christos 	r = 0;
   1270  1.1.1.22  christos  out:
   1271  1.1.1.24  christos 	free(prompt);
   1272  1.1.1.24  christos 	if (pin != NULL)
   1273  1.1.1.24  christos 		freezero(pin, strlen(pin));
   1274  1.1.1.22  christos 	notify_complete(notifier);
   1275  1.1.1.10  christos 	sshkey_free(prv);
   1276  1.1.1.18  christos 	return r;
   1277       1.1  christos }
   1278       1.1  christos 
   1279       1.1  christos static int
   1280  1.1.1.16  christos id_filename_matches(Identity *id, Identity *private_id)
   1281  1.1.1.16  christos {
   1282  1.1.1.16  christos 	const char *suffixes[] = { ".pub", "-cert.pub", NULL };
   1283  1.1.1.16  christos 	size_t len = strlen(id->filename), plen = strlen(private_id->filename);
   1284  1.1.1.16  christos 	size_t i, slen;
   1285  1.1.1.16  christos 
   1286  1.1.1.16  christos 	if (strcmp(id->filename, private_id->filename) == 0)
   1287  1.1.1.16  christos 		return 1;
   1288  1.1.1.16  christos 	for (i = 0; suffixes[i]; i++) {
   1289  1.1.1.16  christos 		slen = strlen(suffixes[i]);
   1290  1.1.1.16  christos 		if (len > slen && plen == len - slen &&
   1291  1.1.1.16  christos 		    strcmp(id->filename + (len - slen), suffixes[i]) == 0 &&
   1292  1.1.1.16  christos 		    memcmp(id->filename, private_id->filename, plen) == 0)
   1293  1.1.1.16  christos 			return 1;
   1294  1.1.1.16  christos 	}
   1295  1.1.1.16  christos 	return 0;
   1296  1.1.1.16  christos }
   1297  1.1.1.16  christos 
   1298  1.1.1.16  christos static int
   1299  1.1.1.20  christos sign_and_send_pubkey(struct ssh *ssh, Identity *id)
   1300       1.1  christos {
   1301  1.1.1.20  christos 	Authctxt *authctxt = (Authctxt *)ssh->authctxt;
   1302  1.1.1.19  christos 	struct sshbuf *b = NULL;
   1303  1.1.1.19  christos 	Identity *private_id, *sign_id = NULL;
   1304  1.1.1.19  christos 	u_char *signature = NULL;
   1305  1.1.1.19  christos 	size_t slen = 0, skip = 0;
   1306  1.1.1.19  christos 	int r, fallback_sigtype, sent = 0;
   1307  1.1.1.19  christos 	char *alg = NULL, *fp = NULL;
   1308  1.1.1.19  christos 	const char *loc = "";
   1309       1.1  christos 
   1310  1.1.1.10  christos 	if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
   1311  1.1.1.10  christos 	    SSH_FP_DEFAULT)) == NULL)
   1312  1.1.1.10  christos 		return 0;
   1313       1.1  christos 
   1314  1.1.1.19  christos 	debug3("%s: %s %s", __func__, sshkey_type(id->key), fp);
   1315       1.1  christos 
   1316  1.1.1.13  christos 	/*
   1317  1.1.1.13  christos 	 * If the key is an certificate, try to find a matching private key
   1318  1.1.1.13  christos 	 * and use it to complete the signature.
   1319  1.1.1.14  christos 	 * If no such private key exists, fall back to trying the certificate
   1320  1.1.1.14  christos 	 * key itself in case it has a private half already loaded.
   1321  1.1.1.19  christos 	 * This will try to set sign_id to the private key that will perform
   1322  1.1.1.19  christos 	 * the signature.
   1323  1.1.1.13  christos 	 */
   1324  1.1.1.19  christos 	if (sshkey_is_cert(id->key)) {
   1325  1.1.1.13  christos 		TAILQ_FOREACH(private_id, &authctxt->keys, next) {
   1326  1.1.1.13  christos 			if (sshkey_equal_public(id->key, private_id->key) &&
   1327  1.1.1.13  christos 			    id->key->type != private_id->key->type) {
   1328  1.1.1.19  christos 				sign_id = private_id;
   1329  1.1.1.13  christos 				break;
   1330  1.1.1.13  christos 			}
   1331  1.1.1.13  christos 		}
   1332  1.1.1.16  christos 		/*
   1333  1.1.1.16  christos 		 * Exact key matches are preferred, but also allow
   1334  1.1.1.16  christos 		 * filename matches for non-PKCS#11/agent keys that
   1335  1.1.1.16  christos 		 * didn't load public keys. This supports the case
   1336  1.1.1.16  christos 		 * of keeping just a private key file and public
   1337  1.1.1.16  christos 		 * certificate on disk.
   1338  1.1.1.16  christos 		 */
   1339  1.1.1.19  christos 		if (sign_id == NULL &&
   1340  1.1.1.19  christos 		    !id->isprivate && id->agent_fd == -1 &&
   1341  1.1.1.16  christos 		    (id->key->flags & SSHKEY_FLAG_EXT) == 0) {
   1342  1.1.1.16  christos 			TAILQ_FOREACH(private_id, &authctxt->keys, next) {
   1343  1.1.1.16  christos 				if (private_id->key == NULL &&
   1344  1.1.1.16  christos 				    id_filename_matches(id, private_id)) {
   1345  1.1.1.19  christos 					sign_id = private_id;
   1346  1.1.1.16  christos 					break;
   1347  1.1.1.16  christos 				}
   1348  1.1.1.16  christos 			}
   1349  1.1.1.16  christos 		}
   1350  1.1.1.19  christos 		if (sign_id != NULL) {
   1351  1.1.1.13  christos 			debug2("%s: using private key \"%s\"%s for "
   1352  1.1.1.13  christos 			    "certificate", __func__, id->filename,
   1353  1.1.1.13  christos 			    id->agent_fd != -1 ? " from agent" : "");
   1354  1.1.1.13  christos 		} else {
   1355  1.1.1.14  christos 			debug("%s: no separate private key for certificate "
   1356  1.1.1.13  christos 			    "\"%s\"", __func__, id->filename);
   1357  1.1.1.13  christos 		}
   1358  1.1.1.13  christos 	}
   1359  1.1.1.13  christos 
   1360  1.1.1.19  christos 	/*
   1361  1.1.1.19  christos 	 * If the above didn't select another identity to do the signing
   1362  1.1.1.19  christos 	 * then default to the one we started with.
   1363  1.1.1.19  christos 	 */
   1364  1.1.1.19  christos 	if (sign_id == NULL)
   1365  1.1.1.19  christos 		sign_id = id;
   1366  1.1.1.19  christos 
   1367  1.1.1.19  christos 	/* assemble and sign data */
   1368  1.1.1.19  christos 	for (fallback_sigtype = 0; fallback_sigtype <= 1; fallback_sigtype++) {
   1369  1.1.1.19  christos 		free(alg);
   1370  1.1.1.19  christos 		slen = 0;
   1371  1.1.1.19  christos 		signature = NULL;
   1372  1.1.1.19  christos 		if ((alg = key_sig_algorithm(fallback_sigtype ? NULL : ssh,
   1373  1.1.1.19  christos 		    id->key)) == NULL) {
   1374  1.1.1.19  christos 			error("%s: no mutual signature supported", __func__);
   1375  1.1.1.19  christos 			goto out;
   1376  1.1.1.19  christos 		}
   1377  1.1.1.22  christos 		debug3("%s: signing using %s %s", __func__, alg, fp);
   1378  1.1.1.19  christos 
   1379  1.1.1.19  christos 		sshbuf_free(b);
   1380  1.1.1.19  christos 		if ((b = sshbuf_new()) == NULL)
   1381  1.1.1.19  christos 			fatal("%s: sshbuf_new failed", __func__);
   1382  1.1.1.19  christos 		if (datafellows & SSH_OLD_SESSIONID) {
   1383  1.1.1.19  christos 			if ((r = sshbuf_put(b, session_id2,
   1384  1.1.1.19  christos 			    session_id2_len)) != 0) {
   1385  1.1.1.19  christos 				fatal("%s: sshbuf_put: %s",
   1386  1.1.1.19  christos 				    __func__, ssh_err(r));
   1387  1.1.1.19  christos 			}
   1388  1.1.1.19  christos 		} else {
   1389  1.1.1.19  christos 			if ((r = sshbuf_put_string(b, session_id2,
   1390  1.1.1.19  christos 			    session_id2_len)) != 0) {
   1391  1.1.1.19  christos 				fatal("%s: sshbuf_put_string: %s",
   1392  1.1.1.19  christos 				    __func__, ssh_err(r));
   1393  1.1.1.19  christos 			}
   1394  1.1.1.19  christos 		}
   1395  1.1.1.19  christos 		skip = sshbuf_len(b);
   1396  1.1.1.19  christos 		if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
   1397  1.1.1.19  christos 		    (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
   1398  1.1.1.19  christos 		    (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
   1399  1.1.1.19  christos 		    (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
   1400  1.1.1.19  christos 		    (r = sshbuf_put_u8(b, 1)) != 0 ||
   1401  1.1.1.19  christos 		    (r = sshbuf_put_cstring(b, alg)) != 0 ||
   1402  1.1.1.19  christos 		    (r = sshkey_puts(id->key, b)) != 0) {
   1403  1.1.1.19  christos 			fatal("%s: assemble signed data: %s",
   1404  1.1.1.19  christos 			    __func__, ssh_err(r));
   1405  1.1.1.19  christos 		}
   1406  1.1.1.19  christos 
   1407  1.1.1.19  christos 		/* generate signature */
   1408  1.1.1.19  christos 		r = identity_sign(sign_id, &signature, &slen,
   1409  1.1.1.19  christos 		    sshbuf_ptr(b), sshbuf_len(b), datafellows, alg);
   1410  1.1.1.19  christos 		if (r == 0)
   1411  1.1.1.19  christos 			break;
   1412  1.1.1.19  christos 		else if (r == SSH_ERR_KEY_NOT_FOUND)
   1413  1.1.1.19  christos 			goto out; /* soft failure */
   1414  1.1.1.19  christos 		else if (r == SSH_ERR_SIGN_ALG_UNSUPPORTED &&
   1415  1.1.1.19  christos 		    !fallback_sigtype) {
   1416  1.1.1.19  christos 			if (sign_id->agent_fd != -1)
   1417  1.1.1.19  christos 				loc = "agent ";
   1418  1.1.1.19  christos 			else if ((sign_id->key->flags & SSHKEY_FLAG_EXT) != 0)
   1419  1.1.1.19  christos 				loc = "token ";
   1420  1.1.1.19  christos 			logit("%skey %s %s returned incorrect signature type",
   1421  1.1.1.19  christos 			    loc, sshkey_type(id->key), fp);
   1422  1.1.1.19  christos 			continue;
   1423  1.1.1.19  christos 		}
   1424  1.1.1.22  christos 		error("%s: signing failed for %s \"%s\"%s: %s", __func__,
   1425  1.1.1.22  christos 		    sshkey_type(sign_id->key), sign_id->filename,
   1426  1.1.1.22  christos 		    id->agent_fd != -1 ? " from agent" : "", ssh_err(r));
   1427  1.1.1.19  christos 		goto out;
   1428       1.1  christos 	}
   1429  1.1.1.19  christos 	if (slen == 0 || signature == NULL) /* shouldn't happen */
   1430  1.1.1.19  christos 		fatal("%s: no signature", __func__);
   1431       1.1  christos 
   1432       1.1  christos 	/* append signature */
   1433  1.1.1.19  christos 	if ((r = sshbuf_put_string(b, signature, slen)) != 0)
   1434  1.1.1.19  christos 		fatal("%s: append signature: %s", __func__, ssh_err(r));
   1435       1.1  christos 
   1436  1.1.1.19  christos #ifdef DEBUG_PK
   1437  1.1.1.19  christos 	sshbuf_dump(b, stderr);
   1438  1.1.1.19  christos #endif
   1439       1.1  christos 	/* skip session id and packet type */
   1440  1.1.1.19  christos 	if ((r = sshbuf_consume(b, skip + 1)) != 0)
   1441  1.1.1.19  christos 		fatal("%s: consume: %s", __func__, ssh_err(r));
   1442       1.1  christos 
   1443       1.1  christos 	/* put remaining data from buffer into packet */
   1444  1.1.1.19  christos 	if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
   1445  1.1.1.19  christos 	    (r = sshpkt_putb(ssh, b)) != 0 ||
   1446  1.1.1.19  christos 	    (r = sshpkt_send(ssh)) != 0)
   1447  1.1.1.19  christos 		fatal("%s: enqueue request: %s", __func__, ssh_err(r));
   1448       1.1  christos 
   1449  1.1.1.19  christos 	/* success */
   1450  1.1.1.19  christos 	sent = 1;
   1451  1.1.1.19  christos 
   1452  1.1.1.19  christos  out:
   1453  1.1.1.19  christos 	free(fp);
   1454  1.1.1.19  christos 	free(alg);
   1455  1.1.1.19  christos 	sshbuf_free(b);
   1456  1.1.1.19  christos 	freezero(signature, slen);
   1457  1.1.1.19  christos 	return sent;
   1458       1.1  christos }
   1459       1.1  christos 
   1460       1.1  christos static int
   1461  1.1.1.20  christos send_pubkey_test(struct ssh *ssh, Identity *id)
   1462       1.1  christos {
   1463  1.1.1.20  christos 	Authctxt *authctxt = (Authctxt *)ssh->authctxt;
   1464  1.1.1.19  christos 	u_char *blob = NULL;
   1465  1.1.1.19  christos 	char *alg = NULL;
   1466  1.1.1.19  christos 	size_t bloblen;
   1467  1.1.1.19  christos 	u_int have_sig = 0;
   1468  1.1.1.19  christos 	int sent = 0, r;
   1469       1.1  christos 
   1470  1.1.1.19  christos 	if ((alg = key_sig_algorithm(ssh, id->key)) == NULL) {
   1471  1.1.1.19  christos 		debug("%s: no mutual signature algorithm", __func__);
   1472  1.1.1.19  christos 		goto out;
   1473  1.1.1.19  christos 	}
   1474       1.1  christos 
   1475  1.1.1.19  christos 	if ((r = sshkey_to_blob(id->key, &blob, &bloblen)) != 0) {
   1476       1.1  christos 		/* we cannot handle this key */
   1477  1.1.1.19  christos 		debug3("%s: cannot handle key", __func__);
   1478  1.1.1.19  christos 		goto out;
   1479       1.1  christos 	}
   1480       1.1  christos 	/* register callback for USERAUTH_PK_OK message */
   1481  1.1.1.19  christos 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
   1482  1.1.1.19  christos 
   1483  1.1.1.19  christos 	if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
   1484  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
   1485  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
   1486  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
   1487  1.1.1.19  christos 	    (r = sshpkt_put_u8(ssh, have_sig)) != 0 ||
   1488  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, alg)) != 0 ||
   1489  1.1.1.19  christos 	    (r = sshpkt_put_string(ssh, blob, bloblen)) != 0 ||
   1490  1.1.1.19  christos 	    (r = sshpkt_send(ssh)) != 0)
   1491  1.1.1.19  christos 		fatal("%s: %s", __func__, ssh_err(r));
   1492  1.1.1.19  christos 	sent = 1;
   1493       1.1  christos 
   1494  1.1.1.19  christos  out:
   1495  1.1.1.19  christos 	free(alg);
   1496   1.1.1.8  christos 	free(blob);
   1497  1.1.1.19  christos 	return sent;
   1498       1.1  christos }
   1499       1.1  christos 
   1500  1.1.1.17  christos static struct sshkey *
   1501  1.1.1.13  christos load_identity_file(Identity *id)
   1502       1.1  christos {
   1503  1.1.1.17  christos 	struct sshkey *private = NULL;
   1504  1.1.1.13  christos 	char prompt[300], *passphrase, *comment;
   1505  1.1.1.21  christos 	int r, quit = 0, i;
   1506       1.1  christos 	struct stat st;
   1507       1.1  christos 
   1508  1.1.1.21  christos 	if (stat(id->filename, &st) == -1) {
   1509  1.1.1.13  christos 		(id->userprovided ? logit : debug3)("no such identity: %s: %s",
   1510  1.1.1.13  christos 		    id->filename, strerror(errno));
   1511       1.1  christos 		return NULL;
   1512       1.1  christos 	}
   1513  1.1.1.10  christos 	snprintf(prompt, sizeof prompt,
   1514  1.1.1.13  christos 	    "Enter passphrase for key '%.100s': ", id->filename);
   1515  1.1.1.10  christos 	for (i = 0; i <= options.number_of_password_prompts; i++) {
   1516  1.1.1.10  christos 		if (i == 0)
   1517  1.1.1.10  christos 			passphrase = "";
   1518  1.1.1.10  christos 		else {
   1519       1.1  christos 			passphrase = read_passphrase(prompt, 0);
   1520  1.1.1.10  christos 			if (*passphrase == '\0') {
   1521       1.1  christos 				debug2("no passphrase given, try next key");
   1522  1.1.1.10  christos 				free(passphrase);
   1523  1.1.1.10  christos 				break;
   1524  1.1.1.10  christos 			}
   1525  1.1.1.10  christos 		}
   1526  1.1.1.13  christos 		switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename,
   1527  1.1.1.21  christos 		    passphrase, &private, &comment))) {
   1528  1.1.1.10  christos 		case 0:
   1529  1.1.1.10  christos 			break;
   1530  1.1.1.10  christos 		case SSH_ERR_KEY_WRONG_PASSPHRASE:
   1531  1.1.1.10  christos 			if (options.batch_mode) {
   1532  1.1.1.10  christos 				quit = 1;
   1533  1.1.1.10  christos 				break;
   1534  1.1.1.10  christos 			}
   1535  1.1.1.10  christos 			if (i != 0)
   1536  1.1.1.10  christos 				debug2("bad passphrase given, try again...");
   1537  1.1.1.10  christos 			break;
   1538  1.1.1.10  christos 		case SSH_ERR_SYSTEM_ERROR:
   1539  1.1.1.10  christos 			if (errno == ENOENT) {
   1540  1.1.1.10  christos 				debug2("Load key \"%s\": %s",
   1541  1.1.1.13  christos 				    id->filename, ssh_err(r));
   1542       1.1  christos 				quit = 1;
   1543  1.1.1.10  christos 				break;
   1544       1.1  christos 			}
   1545  1.1.1.10  christos 			/* FALLTHROUGH */
   1546  1.1.1.10  christos 		default:
   1547  1.1.1.13  christos 			error("Load key \"%s\": %s", id->filename, ssh_err(r));
   1548  1.1.1.10  christos 			quit = 1;
   1549  1.1.1.10  christos 			break;
   1550  1.1.1.10  christos 		}
   1551  1.1.1.22  christos 		if (private != NULL && sshkey_is_sk(private) &&
   1552  1.1.1.22  christos 		    options.sk_provider == NULL) {
   1553  1.1.1.22  christos 			debug("key \"%s\" is an authenticator-hosted key, "
   1554  1.1.1.22  christos 			    "but no provider specified", id->filename);
   1555  1.1.1.22  christos 			sshkey_free(private);
   1556  1.1.1.22  christos 			private = NULL;
   1557  1.1.1.22  christos 			quit = 1;
   1558  1.1.1.22  christos 		}
   1559  1.1.1.13  christos 		if (!quit && private != NULL && id->agent_fd == -1 &&
   1560  1.1.1.13  christos 		    !(id->key && id->isprivate))
   1561  1.1.1.13  christos 			maybe_add_key_to_agent(id->filename, private, comment,
   1562  1.1.1.13  christos 			    passphrase);
   1563  1.1.1.19  christos 		if (i > 0)
   1564  1.1.1.19  christos 			freezero(passphrase, strlen(passphrase));
   1565  1.1.1.13  christos 		free(comment);
   1566  1.1.1.10  christos 		if (private != NULL || quit)
   1567  1.1.1.10  christos 			break;
   1568       1.1  christos 	}
   1569       1.1  christos 	return private;
   1570       1.1  christos }
   1571       1.1  christos 
   1572  1.1.1.19  christos static int
   1573  1.1.1.19  christos key_type_allowed_by_config(struct sshkey *key)
   1574  1.1.1.19  christos {
   1575  1.1.1.19  christos 	if (match_pattern_list(sshkey_ssh_name(key),
   1576  1.1.1.19  christos 	    options.pubkey_key_types, 0) == 1)
   1577  1.1.1.19  christos 		return 1;
   1578  1.1.1.19  christos 
   1579  1.1.1.19  christos 	/* RSA keys/certs might be allowed by alternate signature types */
   1580  1.1.1.19  christos 	switch (key->type) {
   1581  1.1.1.19  christos 	case KEY_RSA:
   1582  1.1.1.19  christos 		if (match_pattern_list("rsa-sha2-512",
   1583  1.1.1.19  christos 		    options.pubkey_key_types, 0) == 1)
   1584  1.1.1.19  christos 			return 1;
   1585  1.1.1.19  christos 		if (match_pattern_list("rsa-sha2-256",
   1586  1.1.1.19  christos 		    options.pubkey_key_types, 0) == 1)
   1587  1.1.1.19  christos 			return 1;
   1588  1.1.1.19  christos 		break;
   1589  1.1.1.19  christos 	case KEY_RSA_CERT:
   1590  1.1.1.19  christos 		if (match_pattern_list("rsa-sha2-512-cert-v01 (at) openssh.com",
   1591  1.1.1.19  christos 		    options.pubkey_key_types, 0) == 1)
   1592  1.1.1.19  christos 			return 1;
   1593  1.1.1.19  christos 		if (match_pattern_list("rsa-sha2-256-cert-v01 (at) openssh.com",
   1594  1.1.1.19  christos 		    options.pubkey_key_types, 0) == 1)
   1595  1.1.1.19  christos 			return 1;
   1596  1.1.1.19  christos 		break;
   1597  1.1.1.19  christos 	}
   1598  1.1.1.19  christos 	return 0;
   1599  1.1.1.19  christos }
   1600  1.1.1.19  christos 
   1601  1.1.1.19  christos 
   1602       1.1  christos /*
   1603       1.1  christos  * try keys in the following order:
   1604  1.1.1.13  christos  * 	1. certificates listed in the config file
   1605  1.1.1.13  christos  * 	2. other input certificates
   1606  1.1.1.13  christos  *	3. agent keys that are found in the config file
   1607  1.1.1.13  christos  *	4. other agent keys
   1608  1.1.1.13  christos  *	5. keys that are only listed in the config file
   1609       1.1  christos  */
   1610       1.1  christos static void
   1611       1.1  christos pubkey_prepare(Authctxt *authctxt)
   1612       1.1  christos {
   1613  1.1.1.10  christos 	struct identity *id, *id2, *tmp;
   1614  1.1.1.10  christos 	struct idlist agent, files, *preferred;
   1615  1.1.1.10  christos 	struct sshkey *key;
   1616  1.1.1.13  christos 	int agent_fd = -1, i, r, found;
   1617  1.1.1.10  christos 	size_t j;
   1618  1.1.1.10  christos 	struct ssh_identitylist *idlist;
   1619  1.1.1.20  christos 	char *ident;
   1620       1.1  christos 
   1621       1.1  christos 	TAILQ_INIT(&agent);	/* keys from the agent */
   1622       1.1  christos 	TAILQ_INIT(&files);	/* keys from the config file */
   1623       1.1  christos 	preferred = &authctxt->keys;
   1624       1.1  christos 	TAILQ_INIT(preferred);	/* preferred order of keys */
   1625       1.1  christos 
   1626   1.1.1.7  christos 	/* list of keys stored in the filesystem and PKCS#11 */
   1627       1.1  christos 	for (i = 0; i < options.num_identity_files; i++) {
   1628       1.1  christos 		key = options.identity_keys[i];
   1629  1.1.1.22  christos 		if (key && key->cert &&
   1630  1.1.1.22  christos 		    key->cert->type != SSH2_CERT_TYPE_USER) {
   1631  1.1.1.22  christos 			debug("%s: ignoring certificate %s: not a user "
   1632  1.1.1.22  christos 			    "certificate",  __func__,
   1633  1.1.1.22  christos 			    options.identity_files[i]);
   1634  1.1.1.22  christos 			continue;
   1635  1.1.1.22  christos 		}
   1636  1.1.1.22  christos 		if (key && sshkey_is_sk(key) && options.sk_provider == NULL) {
   1637  1.1.1.22  christos 			debug("%s: ignoring authenticator-hosted key %s as no "
   1638  1.1.1.22  christos 			    "SecurityKeyProvider has been specified",
   1639  1.1.1.22  christos 			    __func__, options.identity_files[i]);
   1640   1.1.1.3      adam 			continue;
   1641  1.1.1.22  christos 		}
   1642       1.1  christos 		options.identity_keys[i] = NULL;
   1643       1.1  christos 		id = xcalloc(1, sizeof(*id));
   1644  1.1.1.13  christos 		id->agent_fd = -1;
   1645       1.1  christos 		id->key = key;
   1646       1.1  christos 		id->filename = xstrdup(options.identity_files[i]);
   1647   1.1.1.8  christos 		id->userprovided = options.identity_file_userprovided[i];
   1648       1.1  christos 		TAILQ_INSERT_TAIL(&files, id, next);
   1649       1.1  christos 	}
   1650  1.1.1.13  christos 	/* list of certificates specified by user */
   1651  1.1.1.13  christos 	for (i = 0; i < options.num_certificate_files; i++) {
   1652  1.1.1.13  christos 		key = options.certificates[i];
   1653  1.1.1.19  christos 		if (!sshkey_is_cert(key) || key->cert == NULL ||
   1654  1.1.1.22  christos 		    key->cert->type != SSH2_CERT_TYPE_USER) {
   1655  1.1.1.22  christos 			debug("%s: ignoring certificate %s: not a user "
   1656  1.1.1.22  christos 			    "certificate",  __func__,
   1657  1.1.1.22  christos 			    options.identity_files[i]);
   1658  1.1.1.22  christos 			continue;
   1659  1.1.1.22  christos 		}
   1660  1.1.1.22  christos 		if (key && sshkey_is_sk(key) && options.sk_provider == NULL) {
   1661  1.1.1.22  christos 			debug("%s: ignoring authenticator-hosted key "
   1662  1.1.1.22  christos 			    "certificate %s as no "
   1663  1.1.1.22  christos 			    "SecurityKeyProvider has been specified",
   1664  1.1.1.22  christos 			    __func__, options.identity_files[i]);
   1665  1.1.1.13  christos 			continue;
   1666  1.1.1.22  christos 		}
   1667  1.1.1.13  christos 		id = xcalloc(1, sizeof(*id));
   1668  1.1.1.13  christos 		id->agent_fd = -1;
   1669  1.1.1.13  christos 		id->key = key;
   1670  1.1.1.13  christos 		id->filename = xstrdup(options.certificate_files[i]);
   1671  1.1.1.13  christos 		id->userprovided = options.certificate_file_userprovided[i];
   1672  1.1.1.13  christos 		TAILQ_INSERT_TAIL(preferred, id, next);
   1673  1.1.1.13  christos 	}
   1674       1.1  christos 	/* list of keys supported by the agent */
   1675  1.1.1.10  christos 	if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
   1676  1.1.1.10  christos 		if (r != SSH_ERR_AGENT_NOT_PRESENT)
   1677  1.1.1.10  christos 			debug("%s: ssh_get_authentication_socket: %s",
   1678  1.1.1.10  christos 			    __func__, ssh_err(r));
   1679  1.1.1.17  christos 	} else if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
   1680  1.1.1.10  christos 		if (r != SSH_ERR_AGENT_NO_IDENTITIES)
   1681  1.1.1.10  christos 			debug("%s: ssh_fetch_identitylist: %s",
   1682  1.1.1.10  christos 			    __func__, ssh_err(r));
   1683  1.1.1.13  christos 		close(agent_fd);
   1684  1.1.1.10  christos 	} else {
   1685  1.1.1.10  christos 		for (j = 0; j < idlist->nkeys; j++) {
   1686       1.1  christos 			found = 0;
   1687       1.1  christos 			TAILQ_FOREACH(id, &files, next) {
   1688  1.1.1.10  christos 				/*
   1689  1.1.1.10  christos 				 * agent keys from the config file are
   1690  1.1.1.10  christos 				 * preferred
   1691  1.1.1.10  christos 				 */
   1692  1.1.1.10  christos 				if (sshkey_equal(idlist->keys[j], id->key)) {
   1693       1.1  christos 					TAILQ_REMOVE(&files, id, next);
   1694       1.1  christos 					TAILQ_INSERT_TAIL(preferred, id, next);
   1695  1.1.1.10  christos 					id->agent_fd = agent_fd;
   1696       1.1  christos 					found = 1;
   1697       1.1  christos 					break;
   1698       1.1  christos 				}
   1699       1.1  christos 			}
   1700       1.1  christos 			if (!found && !options.identities_only) {
   1701       1.1  christos 				id = xcalloc(1, sizeof(*id));
   1702  1.1.1.10  christos 				/* XXX "steals" key/comment from idlist */
   1703  1.1.1.10  christos 				id->key = idlist->keys[j];
   1704  1.1.1.10  christos 				id->filename = idlist->comments[j];
   1705  1.1.1.10  christos 				idlist->keys[j] = NULL;
   1706  1.1.1.10  christos 				idlist->comments[j] = NULL;
   1707  1.1.1.10  christos 				id->agent_fd = agent_fd;
   1708       1.1  christos 				TAILQ_INSERT_TAIL(&agent, id, next);
   1709       1.1  christos 			}
   1710       1.1  christos 		}
   1711  1.1.1.10  christos 		ssh_free_identitylist(idlist);
   1712       1.1  christos 		/* append remaining agent keys */
   1713  1.1.1.24  christos 		TAILQ_CONCAT(preferred, &agent, next);
   1714  1.1.1.10  christos 		authctxt->agent_fd = agent_fd;
   1715       1.1  christos 	}
   1716  1.1.1.14  christos 	/* Prefer PKCS11 keys that are explicitly listed */
   1717  1.1.1.14  christos 	TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
   1718  1.1.1.14  christos 		if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
   1719  1.1.1.14  christos 			continue;
   1720  1.1.1.14  christos 		found = 0;
   1721  1.1.1.14  christos 		TAILQ_FOREACH(id2, &files, next) {
   1722  1.1.1.14  christos 			if (id2->key == NULL ||
   1723  1.1.1.23  christos 			    (id2->key->flags & SSHKEY_FLAG_EXT) != 0)
   1724  1.1.1.14  christos 				continue;
   1725  1.1.1.14  christos 			if (sshkey_equal(id->key, id2->key)) {
   1726  1.1.1.14  christos 				TAILQ_REMOVE(&files, id, next);
   1727  1.1.1.14  christos 				TAILQ_INSERT_TAIL(preferred, id, next);
   1728  1.1.1.14  christos 				found = 1;
   1729  1.1.1.14  christos 				break;
   1730  1.1.1.14  christos 			}
   1731  1.1.1.14  christos 		}
   1732  1.1.1.14  christos 		/* If IdentitiesOnly set and key not found then don't use it */
   1733  1.1.1.14  christos 		if (!found && options.identities_only) {
   1734  1.1.1.14  christos 			TAILQ_REMOVE(&files, id, next);
   1735  1.1.1.19  christos 			freezero(id, sizeof(*id));
   1736  1.1.1.14  christos 		}
   1737  1.1.1.14  christos 	}
   1738       1.1  christos 	/* append remaining keys from the config file */
   1739  1.1.1.24  christos 	TAILQ_CONCAT(preferred, &files, next);
   1740  1.1.1.13  christos 	/* finally, filter by PubkeyAcceptedKeyTypes */
   1741  1.1.1.13  christos 	TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
   1742  1.1.1.19  christos 		if (id->key != NULL && !key_type_allowed_by_config(id->key)) {
   1743  1.1.1.13  christos 			debug("Skipping %s key %s - "
   1744  1.1.1.13  christos 			    "not in PubkeyAcceptedKeyTypes",
   1745  1.1.1.13  christos 			    sshkey_ssh_name(id->key), id->filename);
   1746  1.1.1.13  christos 			TAILQ_REMOVE(preferred, id, next);
   1747  1.1.1.13  christos 			sshkey_free(id->key);
   1748  1.1.1.13  christos 			free(id->filename);
   1749  1.1.1.13  christos 			memset(id, 0, sizeof(*id));
   1750  1.1.1.13  christos 			continue;
   1751  1.1.1.13  christos 		}
   1752       1.1  christos 	}
   1753  1.1.1.20  christos 	/* List the keys we plan on using */
   1754  1.1.1.20  christos 	TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
   1755  1.1.1.20  christos 		ident = format_identity(id);
   1756  1.1.1.20  christos 		debug("Will attempt key: %s", ident);
   1757  1.1.1.20  christos 		free(ident);
   1758  1.1.1.20  christos 	}
   1759  1.1.1.20  christos 	debug2("%s: done", __func__);
   1760       1.1  christos }
   1761       1.1  christos 
   1762       1.1  christos static void
   1763  1.1.1.20  christos pubkey_cleanup(struct ssh *ssh)
   1764       1.1  christos {
   1765  1.1.1.20  christos 	Authctxt *authctxt = (Authctxt *)ssh->authctxt;
   1766       1.1  christos 	Identity *id;
   1767       1.1  christos 
   1768  1.1.1.20  christos 	if (authctxt->agent_fd != -1) {
   1769  1.1.1.10  christos 		ssh_close_authentication_socket(authctxt->agent_fd);
   1770  1.1.1.20  christos 		authctxt->agent_fd = -1;
   1771  1.1.1.20  christos 	}
   1772       1.1  christos 	for (id = TAILQ_FIRST(&authctxt->keys); id;
   1773       1.1  christos 	    id = TAILQ_FIRST(&authctxt->keys)) {
   1774       1.1  christos 		TAILQ_REMOVE(&authctxt->keys, id, next);
   1775  1.1.1.13  christos 		sshkey_free(id->key);
   1776   1.1.1.8  christos 		free(id->filename);
   1777   1.1.1.8  christos 		free(id);
   1778       1.1  christos 	}
   1779       1.1  christos }
   1780       1.1  christos 
   1781  1.1.1.15  christos static void
   1782  1.1.1.15  christos pubkey_reset(Authctxt *authctxt)
   1783  1.1.1.15  christos {
   1784  1.1.1.15  christos 	Identity *id;
   1785  1.1.1.15  christos 
   1786  1.1.1.15  christos 	TAILQ_FOREACH(id, &authctxt->keys, next)
   1787  1.1.1.15  christos 		id->tried = 0;
   1788  1.1.1.15  christos }
   1789  1.1.1.15  christos 
   1790  1.1.1.12  christos static int
   1791  1.1.1.12  christos try_identity(Identity *id)
   1792  1.1.1.12  christos {
   1793  1.1.1.12  christos 	if (!id->key)
   1794  1.1.1.12  christos 		return (0);
   1795  1.1.1.19  christos 	if (sshkey_type_plain(id->key->type) == KEY_RSA &&
   1796  1.1.1.12  christos 	    (datafellows & SSH_BUG_RSASIGMD5) != 0) {
   1797  1.1.1.12  christos 		debug("Skipped %s key %s for RSA/MD5 server",
   1798  1.1.1.19  christos 		    sshkey_type(id->key), id->filename);
   1799  1.1.1.12  christos 		return (0);
   1800  1.1.1.12  christos 	}
   1801  1.1.1.17  christos 	return 1;
   1802  1.1.1.12  christos }
   1803  1.1.1.12  christos 
   1804  1.1.1.20  christos static int
   1805  1.1.1.20  christos userauth_pubkey(struct ssh *ssh)
   1806       1.1  christos {
   1807  1.1.1.20  christos 	Authctxt *authctxt = (Authctxt *)ssh->authctxt;
   1808       1.1  christos 	Identity *id;
   1809       1.1  christos 	int sent = 0;
   1810  1.1.1.20  christos 	char *ident;
   1811       1.1  christos 
   1812       1.1  christos 	while ((id = TAILQ_FIRST(&authctxt->keys))) {
   1813       1.1  christos 		if (id->tried++)
   1814       1.1  christos 			return (0);
   1815       1.1  christos 		/* move key to the end of the queue */
   1816       1.1  christos 		TAILQ_REMOVE(&authctxt->keys, id, next);
   1817       1.1  christos 		TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
   1818       1.1  christos 		/*
   1819       1.1  christos 		 * send a test message if we have the public key. for
   1820       1.1  christos 		 * encrypted keys we cannot do this and have to load the
   1821       1.1  christos 		 * private key instead
   1822       1.1  christos 		 */
   1823   1.1.1.9  christos 		if (id->key != NULL) {
   1824  1.1.1.12  christos 			if (try_identity(id)) {
   1825  1.1.1.20  christos 				ident = format_identity(id);
   1826  1.1.1.20  christos 				debug("Offering public key: %s", ident);
   1827  1.1.1.20  christos 				free(ident);
   1828  1.1.1.20  christos 				sent = send_pubkey_test(ssh, id);
   1829   1.1.1.9  christos 			}
   1830   1.1.1.9  christos 		} else {
   1831       1.1  christos 			debug("Trying private key: %s", id->filename);
   1832  1.1.1.13  christos 			id->key = load_identity_file(id);
   1833       1.1  christos 			if (id->key != NULL) {
   1834  1.1.1.12  christos 				if (try_identity(id)) {
   1835  1.1.1.12  christos 					id->isprivate = 1;
   1836  1.1.1.20  christos 					sent = sign_and_send_pubkey(ssh, id);
   1837   1.1.1.9  christos 				}
   1838  1.1.1.19  christos 				sshkey_free(id->key);
   1839       1.1  christos 				id->key = NULL;
   1840  1.1.1.15  christos 				id->isprivate = 0;
   1841       1.1  christos 			}
   1842       1.1  christos 		}
   1843       1.1  christos 		if (sent)
   1844       1.1  christos 			return (sent);
   1845       1.1  christos 	}
   1846       1.1  christos 	return (0);
   1847       1.1  christos }
   1848       1.1  christos 
   1849       1.1  christos /*
   1850       1.1  christos  * Send userauth request message specifying keyboard-interactive method.
   1851       1.1  christos  */
   1852  1.1.1.20  christos static int
   1853  1.1.1.20  christos userauth_kbdint(struct ssh *ssh)
   1854       1.1  christos {
   1855  1.1.1.20  christos 	Authctxt *authctxt = (Authctxt *)ssh->authctxt;
   1856  1.1.1.19  christos 	int r;
   1857       1.1  christos 
   1858  1.1.1.20  christos 	if (authctxt->attempt_kbdint++ >= options.number_of_password_prompts)
   1859       1.1  christos 		return 0;
   1860       1.1  christos 	/* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
   1861  1.1.1.20  christos 	if (authctxt->attempt_kbdint > 1 && !authctxt->info_req_seen) {
   1862       1.1  christos 		debug3("userauth_kbdint: disable: no info_req_seen");
   1863  1.1.1.19  christos 		ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
   1864       1.1  christos 		return 0;
   1865       1.1  christos 	}
   1866       1.1  christos 
   1867       1.1  christos 	debug2("userauth_kbdint");
   1868  1.1.1.19  christos 	if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
   1869  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
   1870  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
   1871  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
   1872  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, "")) != 0 ||		/* lang */
   1873  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, options.kbd_interactive_devices ?
   1874  1.1.1.19  christos 	    options.kbd_interactive_devices : "")) != 0 ||
   1875  1.1.1.19  christos 	    (r = sshpkt_send(ssh)) != 0)
   1876  1.1.1.19  christos 		fatal("%s: %s", __func__, ssh_err(r));
   1877       1.1  christos 
   1878  1.1.1.19  christos 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
   1879       1.1  christos 	return 1;
   1880       1.1  christos }
   1881       1.1  christos 
   1882       1.1  christos /*
   1883       1.1  christos  * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
   1884       1.1  christos  */
   1885  1.1.1.20  christos static int
   1886  1.1.1.17  christos input_userauth_info_req(int type, u_int32_t seq, struct ssh *ssh)
   1887       1.1  christos {
   1888  1.1.1.17  christos 	Authctxt *authctxt = ssh->authctxt;
   1889  1.1.1.19  christos 	char *name = NULL, *inst = NULL, *lang = NULL, *prompt = NULL;
   1890  1.1.1.19  christos 	char *response = NULL;
   1891  1.1.1.19  christos 	u_char echo = 0;
   1892       1.1  christos 	u_int num_prompts, i;
   1893  1.1.1.19  christos 	int r;
   1894       1.1  christos 
   1895       1.1  christos 	debug2("input_userauth_info_req");
   1896       1.1  christos 
   1897       1.1  christos 	if (authctxt == NULL)
   1898       1.1  christos 		fatal("input_userauth_info_req: no authentication context");
   1899       1.1  christos 
   1900       1.1  christos 	authctxt->info_req_seen = 1;
   1901       1.1  christos 
   1902  1.1.1.19  christos 	if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 ||
   1903  1.1.1.19  christos 	    (r = sshpkt_get_cstring(ssh, &inst, NULL)) != 0 ||
   1904  1.1.1.19  christos 	    (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
   1905  1.1.1.19  christos 		goto out;
   1906       1.1  christos 	if (strlen(name) > 0)
   1907       1.1  christos 		logit("%s", name);
   1908       1.1  christos 	if (strlen(inst) > 0)
   1909       1.1  christos 		logit("%s", inst);
   1910       1.1  christos 
   1911  1.1.1.19  christos 	if ((r = sshpkt_get_u32(ssh, &num_prompts)) != 0)
   1912  1.1.1.19  christos 		goto out;
   1913       1.1  christos 	/*
   1914       1.1  christos 	 * Begin to build info response packet based on prompts requested.
   1915       1.1  christos 	 * We commit to providing the correct number of responses, so if
   1916       1.1  christos 	 * further on we run into a problem that prevents this, we have to
   1917       1.1  christos 	 * be sure and clean this up and send a correct error response.
   1918       1.1  christos 	 */
   1919  1.1.1.19  christos 	if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE)) != 0 ||
   1920  1.1.1.19  christos 	    (r = sshpkt_put_u32(ssh, num_prompts)) != 0)
   1921  1.1.1.19  christos 		goto out;
   1922       1.1  christos 
   1923       1.1  christos 	debug2("input_userauth_info_req: num_prompts %d", num_prompts);
   1924       1.1  christos 	for (i = 0; i < num_prompts; i++) {
   1925  1.1.1.19  christos 		if ((r = sshpkt_get_cstring(ssh, &prompt, NULL)) != 0 ||
   1926  1.1.1.19  christos 		    (r = sshpkt_get_u8(ssh, &echo)) != 0)
   1927  1.1.1.19  christos 			goto out;
   1928       1.1  christos 		response = read_passphrase(prompt, echo ? RP_ECHO : 0);
   1929  1.1.1.19  christos 		if ((r = sshpkt_put_cstring(ssh, response)) != 0)
   1930  1.1.1.19  christos 			goto out;
   1931  1.1.1.19  christos 		freezero(response, strlen(response));
   1932   1.1.1.8  christos 		free(prompt);
   1933  1.1.1.19  christos 		response = prompt = NULL;
   1934       1.1  christos 	}
   1935  1.1.1.19  christos 	/* done with parsing incoming message. */
   1936  1.1.1.19  christos 	if ((r = sshpkt_get_end(ssh)) != 0 ||
   1937  1.1.1.19  christos 	    (r = sshpkt_add_padding(ssh, 64)) != 0)
   1938  1.1.1.19  christos 		goto out;
   1939  1.1.1.19  christos 	r = sshpkt_send(ssh);
   1940  1.1.1.19  christos  out:
   1941  1.1.1.19  christos 	if (response)
   1942  1.1.1.19  christos 		freezero(response, strlen(response));
   1943  1.1.1.19  christos 	free(prompt);
   1944  1.1.1.19  christos 	free(name);
   1945  1.1.1.19  christos 	free(inst);
   1946  1.1.1.19  christos 	free(lang);
   1947  1.1.1.19  christos 	return r;
   1948       1.1  christos }
   1949       1.1  christos 
   1950       1.1  christos static int
   1951  1.1.1.20  christos ssh_keysign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp,
   1952  1.1.1.10  christos     const u_char *data, size_t datalen)
   1953       1.1  christos {
   1954  1.1.1.10  christos 	struct sshbuf *b;
   1955       1.1  christos 	struct stat st;
   1956       1.1  christos 	pid_t pid;
   1957  1.1.1.21  christos 	int r, to[2], from[2], status;
   1958  1.1.1.20  christos 	int sock = ssh_packet_get_connection_in(ssh);
   1959  1.1.1.10  christos 	u_char rversion = 0, version = 2;
   1960  1.1.1.10  christos 	void (*osigchld)(int);
   1961       1.1  christos 
   1962  1.1.1.10  christos 	*sigp = NULL;
   1963  1.1.1.10  christos 	*lenp = 0;
   1964       1.1  christos 
   1965  1.1.1.21  christos 	if (stat(_PATH_SSH_KEY_SIGN, &st) == -1) {
   1966  1.1.1.10  christos 		error("%s: not installed: %s", __func__, strerror(errno));
   1967  1.1.1.10  christos 		return -1;
   1968  1.1.1.10  christos 	}
   1969  1.1.1.10  christos 	if (fflush(stdout) != 0) {
   1970  1.1.1.10  christos 		error("%s: fflush: %s", __func__, strerror(errno));
   1971       1.1  christos 		return -1;
   1972       1.1  christos 	}
   1973  1.1.1.21  christos 	if (pipe(to) == -1) {
   1974  1.1.1.10  christos 		error("%s: pipe: %s", __func__, strerror(errno));
   1975       1.1  christos 		return -1;
   1976       1.1  christos 	}
   1977  1.1.1.21  christos 	if (pipe(from) == -1) {
   1978  1.1.1.10  christos 		error("%s: pipe: %s", __func__, strerror(errno));
   1979       1.1  christos 		return -1;
   1980       1.1  christos 	}
   1981  1.1.1.21  christos 	if ((pid = fork()) == -1) {
   1982  1.1.1.10  christos 		error("%s: fork: %s", __func__, strerror(errno));
   1983       1.1  christos 		return -1;
   1984       1.1  christos 	}
   1985  1.1.1.22  christos 	osigchld = ssh_signal(SIGCHLD, SIG_DFL);
   1986       1.1  christos 	if (pid == 0) {
   1987       1.1  christos 		close(from[0]);
   1988  1.1.1.21  christos 		if (dup2(from[1], STDOUT_FILENO) == -1)
   1989  1.1.1.10  christos 			fatal("%s: dup2: %s", __func__, strerror(errno));
   1990       1.1  christos 		close(to[1]);
   1991  1.1.1.21  christos 		if (dup2(to[0], STDIN_FILENO) == -1)
   1992  1.1.1.10  christos 			fatal("%s: dup2: %s", __func__, strerror(errno));
   1993       1.1  christos 		close(from[1]);
   1994       1.1  christos 		close(to[0]);
   1995  1.1.1.21  christos 
   1996  1.1.1.21  christos 		if (dup2(sock, STDERR_FILENO + 1) == -1)
   1997  1.1.1.21  christos 			fatal("%s: dup2: %s", __func__, strerror(errno));
   1998  1.1.1.21  christos 		sock = STDERR_FILENO + 1;
   1999  1.1.1.21  christos 		fcntl(sock, F_SETFD, 0);	/* keep the socket on exec */
   2000  1.1.1.10  christos 		closefrom(sock + 1);
   2001  1.1.1.21  christos 
   2002  1.1.1.10  christos 		debug3("%s: [child] pid=%ld, exec %s",
   2003  1.1.1.10  christos 		    __func__, (long)getpid(), _PATH_SSH_KEY_SIGN);
   2004  1.1.1.13  christos 		execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL);
   2005  1.1.1.10  christos 		fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN,
   2006       1.1  christos 		    strerror(errno));
   2007       1.1  christos 	}
   2008       1.1  christos 	close(from[1]);
   2009       1.1  christos 	close(to[0]);
   2010  1.1.1.21  christos 	sock = STDERR_FILENO + 1;
   2011       1.1  christos 
   2012  1.1.1.10  christos 	if ((b = sshbuf_new()) == NULL)
   2013  1.1.1.10  christos 		fatal("%s: sshbuf_new failed", __func__);
   2014  1.1.1.10  christos 	/* send # of sock, data to be signed */
   2015  1.1.1.16  christos 	if ((r = sshbuf_put_u32(b, sock)) != 0 ||
   2016  1.1.1.10  christos 	    (r = sshbuf_put_string(b, data, datalen)) != 0)
   2017  1.1.1.10  christos 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
   2018  1.1.1.10  christos 	if (ssh_msg_send(to[1], version, b) == -1)
   2019  1.1.1.10  christos 		fatal("%s: couldn't send request", __func__);
   2020  1.1.1.10  christos 	sshbuf_reset(b);
   2021  1.1.1.10  christos 	r = ssh_msg_recv(from[0], b);
   2022       1.1  christos 	close(from[0]);
   2023       1.1  christos 	close(to[1]);
   2024  1.1.1.10  christos 	if (r < 0) {
   2025  1.1.1.10  christos 		error("%s: no reply", __func__);
   2026  1.1.1.10  christos 		goto fail;
   2027  1.1.1.10  christos 	}
   2028       1.1  christos 
   2029  1.1.1.10  christos 	errno = 0;
   2030  1.1.1.21  christos 	while (waitpid(pid, &status, 0) == -1) {
   2031  1.1.1.10  christos 		if (errno != EINTR) {
   2032  1.1.1.10  christos 			error("%s: waitpid %ld: %s",
   2033  1.1.1.10  christos 			    __func__, (long)pid, strerror(errno));
   2034  1.1.1.10  christos 			goto fail;
   2035  1.1.1.10  christos 		}
   2036  1.1.1.10  christos 	}
   2037  1.1.1.10  christos 	if (!WIFEXITED(status)) {
   2038  1.1.1.10  christos 		error("%s: exited abnormally", __func__);
   2039  1.1.1.10  christos 		goto fail;
   2040  1.1.1.10  christos 	}
   2041  1.1.1.10  christos 	if (WEXITSTATUS(status) != 0) {
   2042  1.1.1.10  christos 		error("%s: exited with status %d",
   2043  1.1.1.10  christos 		    __func__, WEXITSTATUS(status));
   2044  1.1.1.10  christos 		goto fail;
   2045  1.1.1.10  christos 	}
   2046  1.1.1.10  christos 	if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
   2047  1.1.1.10  christos 		error("%s: buffer error: %s", __func__, ssh_err(r));
   2048  1.1.1.10  christos 		goto fail;
   2049  1.1.1.10  christos 	}
   2050  1.1.1.10  christos 	if (rversion != version) {
   2051  1.1.1.10  christos 		error("%s: bad version", __func__);
   2052  1.1.1.10  christos 		goto fail;
   2053  1.1.1.10  christos 	}
   2054  1.1.1.10  christos 	if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
   2055  1.1.1.10  christos 		error("%s: buffer error: %s", __func__, ssh_err(r));
   2056  1.1.1.10  christos  fail:
   2057  1.1.1.22  christos 		ssh_signal(SIGCHLD, osigchld);
   2058  1.1.1.10  christos 		sshbuf_free(b);
   2059       1.1  christos 		return -1;
   2060       1.1  christos 	}
   2061  1.1.1.22  christos 	ssh_signal(SIGCHLD, osigchld);
   2062  1.1.1.10  christos 	sshbuf_free(b);
   2063       1.1  christos 
   2064       1.1  christos 	return 0;
   2065       1.1  christos }
   2066       1.1  christos 
   2067  1.1.1.20  christos static int
   2068  1.1.1.20  christos userauth_hostbased(struct ssh *ssh)
   2069       1.1  christos {
   2070  1.1.1.20  christos 	Authctxt *authctxt = (Authctxt *)ssh->authctxt;
   2071  1.1.1.10  christos 	struct sshkey *private = NULL;
   2072  1.1.1.10  christos 	struct sshbuf *b = NULL;
   2073  1.1.1.10  christos 	u_char *sig = NULL, *keyblob = NULL;
   2074  1.1.1.10  christos 	char *fp = NULL, *chost = NULL, *lname = NULL;
   2075  1.1.1.10  christos 	size_t siglen = 0, keylen = 0;
   2076  1.1.1.10  christos 	int i, r, success = 0;
   2077  1.1.1.10  christos 
   2078  1.1.1.10  christos 	if (authctxt->ktypes == NULL) {
   2079  1.1.1.10  christos 		authctxt->oktypes = xstrdup(options.hostbased_key_types);
   2080  1.1.1.10  christos 		authctxt->ktypes = authctxt->oktypes;
   2081  1.1.1.10  christos 	}
   2082       1.1  christos 
   2083  1.1.1.10  christos 	/*
   2084  1.1.1.10  christos 	 * Work through each listed type pattern in HostbasedKeyTypes,
   2085  1.1.1.10  christos 	 * trying each hostkey that matches the type in turn.
   2086  1.1.1.10  christos 	 */
   2087  1.1.1.10  christos 	for (;;) {
   2088  1.1.1.10  christos 		if (authctxt->active_ktype == NULL)
   2089  1.1.1.10  christos 			authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
   2090  1.1.1.10  christos 		if (authctxt->active_ktype == NULL ||
   2091  1.1.1.10  christos 		    *authctxt->active_ktype == '\0')
   2092  1.1.1.10  christos 			break;
   2093  1.1.1.10  christos 		debug3("%s: trying key type %s", __func__,
   2094  1.1.1.10  christos 		    authctxt->active_ktype);
   2095  1.1.1.10  christos 
   2096  1.1.1.10  christos 		/* check for a useful key */
   2097  1.1.1.10  christos 		private = NULL;
   2098  1.1.1.10  christos 		for (i = 0; i < authctxt->sensitive->nkeys; i++) {
   2099  1.1.1.10  christos 			if (authctxt->sensitive->keys[i] == NULL ||
   2100  1.1.1.10  christos 			    authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
   2101  1.1.1.10  christos 				continue;
   2102  1.1.1.10  christos 			if (match_pattern_list(
   2103  1.1.1.10  christos 			    sshkey_ssh_name(authctxt->sensitive->keys[i]),
   2104  1.1.1.11  christos 			    authctxt->active_ktype, 0) != 1)
   2105  1.1.1.10  christos 				continue;
   2106       1.1  christos 			/* we take and free the key */
   2107  1.1.1.10  christos 			private = authctxt->sensitive->keys[i];
   2108  1.1.1.10  christos 			authctxt->sensitive->keys[i] = NULL;
   2109       1.1  christos 			break;
   2110       1.1  christos 		}
   2111  1.1.1.10  christos 		/* Found one */
   2112  1.1.1.10  christos 		if (private != NULL)
   2113  1.1.1.10  christos 			break;
   2114  1.1.1.10  christos 		/* No more keys of this type; advance */
   2115  1.1.1.10  christos 		authctxt->active_ktype = NULL;
   2116       1.1  christos 	}
   2117  1.1.1.10  christos 	if (private == NULL) {
   2118  1.1.1.10  christos 		free(authctxt->oktypes);
   2119  1.1.1.10  christos 		authctxt->oktypes = authctxt->ktypes = NULL;
   2120  1.1.1.10  christos 		authctxt->active_ktype = NULL;
   2121       1.1  christos 		debug("No more client hostkeys for hostbased authentication.");
   2122  1.1.1.10  christos 		goto out;
   2123       1.1  christos 	}
   2124  1.1.1.10  christos 
   2125  1.1.1.10  christos 	if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
   2126  1.1.1.10  christos 	    SSH_FP_DEFAULT)) == NULL) {
   2127  1.1.1.10  christos 		error("%s: sshkey_fingerprint failed", __func__);
   2128  1.1.1.10  christos 		goto out;
   2129       1.1  christos 	}
   2130  1.1.1.10  christos 	debug("%s: trying hostkey %s %s",
   2131  1.1.1.10  christos 	    __func__, sshkey_ssh_name(private), fp);
   2132  1.1.1.10  christos 
   2133       1.1  christos 	/* figure out a name for the client host */
   2134  1.1.1.20  christos 	lname = get_local_name(ssh_packet_get_connection_in(ssh));
   2135  1.1.1.20  christos 	if (lname == NULL) {
   2136  1.1.1.10  christos 		error("%s: cannot get local ipaddr/name", __func__);
   2137  1.1.1.10  christos 		goto out;
   2138       1.1  christos 	}
   2139  1.1.1.10  christos 
   2140  1.1.1.10  christos 	/* XXX sshbuf_put_stringf? */
   2141  1.1.1.10  christos 	xasprintf(&chost, "%s.", lname);
   2142  1.1.1.10  christos 	debug2("%s: chost %s", __func__, chost);
   2143       1.1  christos 
   2144       1.1  christos 	/* construct data */
   2145  1.1.1.10  christos 	if ((b = sshbuf_new()) == NULL) {
   2146  1.1.1.10  christos 		error("%s: sshbuf_new failed", __func__);
   2147  1.1.1.10  christos 		goto out;
   2148  1.1.1.10  christos 	}
   2149  1.1.1.10  christos 	if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
   2150  1.1.1.10  christos 		error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
   2151  1.1.1.10  christos 		goto out;
   2152  1.1.1.10  christos 	}
   2153  1.1.1.10  christos 	if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
   2154  1.1.1.10  christos 	    (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
   2155  1.1.1.10  christos 	    (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
   2156  1.1.1.18  christos 	    (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
   2157  1.1.1.10  christos 	    (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
   2158  1.1.1.19  christos 	    (r = sshbuf_put_cstring(b, sshkey_ssh_name(private))) != 0 ||
   2159  1.1.1.10  christos 	    (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
   2160  1.1.1.10  christos 	    (r = sshbuf_put_cstring(b, chost)) != 0 ||
   2161  1.1.1.10  christos 	    (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
   2162  1.1.1.10  christos 		error("%s: buffer error: %s", __func__, ssh_err(r));
   2163  1.1.1.10  christos 		goto out;
   2164  1.1.1.10  christos 	}
   2165  1.1.1.10  christos 
   2166       1.1  christos #ifdef DEBUG_PK
   2167  1.1.1.10  christos 	sshbuf_dump(b, stderr);
   2168       1.1  christos #endif
   2169  1.1.1.20  christos 	if ((r = ssh_keysign(ssh, private, &sig, &siglen,
   2170  1.1.1.20  christos 	    sshbuf_ptr(b), sshbuf_len(b))) != 0) {
   2171  1.1.1.10  christos 		error("sign using hostkey %s %s failed",
   2172  1.1.1.10  christos 		    sshkey_ssh_name(private), fp);
   2173  1.1.1.10  christos 		goto out;
   2174  1.1.1.10  christos 	}
   2175  1.1.1.10  christos 	if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
   2176  1.1.1.10  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
   2177  1.1.1.10  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
   2178  1.1.1.10  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
   2179  1.1.1.19  christos 	    (r = sshpkt_put_cstring(ssh, sshkey_ssh_name(private))) != 0 ||
   2180  1.1.1.10  christos 	    (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
   2181  1.1.1.10  christos 	    (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
   2182  1.1.1.10  christos 	    (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
   2183  1.1.1.10  christos 	    (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
   2184  1.1.1.10  christos 	    (r = sshpkt_send(ssh)) != 0) {
   2185  1.1.1.10  christos 		error("%s: packet error: %s", __func__, ssh_err(r));
   2186  1.1.1.10  christos 		goto out;
   2187  1.1.1.10  christos 	}
   2188  1.1.1.10  christos 	success = 1;
   2189  1.1.1.10  christos 
   2190  1.1.1.10  christos  out:
   2191  1.1.1.19  christos 	if (sig != NULL)
   2192  1.1.1.19  christos 		freezero(sig, siglen);
   2193  1.1.1.10  christos 	free(keyblob);
   2194  1.1.1.10  christos 	free(lname);
   2195  1.1.1.10  christos 	free(fp);
   2196   1.1.1.8  christos 	free(chost);
   2197  1.1.1.10  christos 	sshkey_free(private);
   2198  1.1.1.10  christos 	sshbuf_free(b);
   2199       1.1  christos 
   2200  1.1.1.10  christos 	return success;
   2201       1.1  christos }
   2202       1.1  christos 
   2203       1.1  christos /* find auth method */
   2204       1.1  christos 
   2205       1.1  christos /*
   2206       1.1  christos  * given auth method name, if configurable options permit this method fill
   2207       1.1  christos  * in auth_ident field and return true, otherwise return false.
   2208       1.1  christos  */
   2209       1.1  christos static int
   2210       1.1  christos authmethod_is_enabled(Authmethod *method)
   2211       1.1  christos {
   2212       1.1  christos 	if (method == NULL)
   2213       1.1  christos 		return 0;
   2214       1.1  christos 	/* return false if options indicate this method is disabled */
   2215       1.1  christos 	if  (method->enabled == NULL || *method->enabled == 0)
   2216       1.1  christos 		return 0;
   2217       1.1  christos 	/* return false if batch mode is enabled but method needs interactive mode */
   2218       1.1  christos 	if  (method->batch_flag != NULL && *method->batch_flag != 0)
   2219       1.1  christos 		return 0;
   2220       1.1  christos 	return 1;
   2221       1.1  christos }
   2222       1.1  christos 
   2223       1.1  christos static Authmethod *
   2224       1.1  christos authmethod_lookup(const char *name)
   2225       1.1  christos {
   2226       1.1  christos 	Authmethod *method = NULL;
   2227       1.1  christos 	if (name != NULL)
   2228       1.1  christos 		for (method = authmethods; method->name != NULL; method++)
   2229       1.1  christos 			if (strcmp(name, method->name) == 0)
   2230       1.1  christos 				return method;
   2231       1.1  christos 	debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
   2232       1.1  christos 	return NULL;
   2233       1.1  christos }
   2234       1.1  christos 
   2235       1.1  christos /* XXX internal state */
   2236       1.1  christos static Authmethod *current = NULL;
   2237       1.1  christos static char *supported = NULL;
   2238       1.1  christos static char *preferred = NULL;
   2239       1.1  christos 
   2240       1.1  christos /*
   2241       1.1  christos  * Given the authentication method list sent by the server, return the
   2242       1.1  christos  * next method we should try.  If the server initially sends a nil list,
   2243       1.1  christos  * use a built-in default list.
   2244       1.1  christos  */
   2245       1.1  christos static Authmethod *
   2246       1.1  christos authmethod_get(char *authlist)
   2247       1.1  christos {
   2248       1.1  christos 	char *name = NULL;
   2249       1.1  christos 	u_int next;
   2250       1.1  christos 
   2251       1.1  christos 	/* Use a suitable default if we're passed a nil list.  */
   2252       1.1  christos 	if (authlist == NULL || strlen(authlist) == 0)
   2253       1.1  christos 		authlist = options.preferred_authentications;
   2254       1.1  christos 
   2255       1.1  christos 	if (supported == NULL || strcmp(authlist, supported) != 0) {
   2256       1.1  christos 		debug3("start over, passed a different list %s", authlist);
   2257   1.1.1.8  christos 		free(supported);
   2258       1.1  christos 		supported = xstrdup(authlist);
   2259       1.1  christos 		preferred = options.preferred_authentications;
   2260       1.1  christos 		debug3("preferred %s", preferred);
   2261       1.1  christos 		current = NULL;
   2262       1.1  christos 	} else if (current != NULL && authmethod_is_enabled(current))
   2263       1.1  christos 		return current;
   2264       1.1  christos 
   2265       1.1  christos 	for (;;) {
   2266       1.1  christos 		if ((name = match_list(preferred, supported, &next)) == NULL) {
   2267       1.1  christos 			debug("No more authentication methods to try.");
   2268       1.1  christos 			current = NULL;
   2269       1.1  christos 			return NULL;
   2270       1.1  christos 		}
   2271       1.1  christos 		preferred += next;
   2272       1.1  christos 		debug3("authmethod_lookup %s", name);
   2273       1.1  christos 		debug3("remaining preferred: %s", preferred);
   2274       1.1  christos 		if ((current = authmethod_lookup(name)) != NULL &&
   2275       1.1  christos 		    authmethod_is_enabled(current)) {
   2276       1.1  christos 			debug3("authmethod_is_enabled %s", name);
   2277       1.1  christos 			debug("Next authentication method: %s", name);
   2278   1.1.1.8  christos 			free(name);
   2279       1.1  christos 			return current;
   2280       1.1  christos 		}
   2281   1.1.1.8  christos 		free(name);
   2282       1.1  christos 	}
   2283       1.1  christos }
   2284       1.1  christos 
   2285       1.1  christos static char *
   2286       1.1  christos authmethods_get(void)
   2287       1.1  christos {
   2288       1.1  christos 	Authmethod *method = NULL;
   2289  1.1.1.19  christos 	struct sshbuf *b;
   2290       1.1  christos 	char *list;
   2291  1.1.1.19  christos 	int r;
   2292       1.1  christos 
   2293  1.1.1.19  christos 	if ((b = sshbuf_new()) == NULL)
   2294  1.1.1.19  christos 		fatal("%s: sshbuf_new failed", __func__);
   2295       1.1  christos 	for (method = authmethods; method->name != NULL; method++) {
   2296       1.1  christos 		if (authmethod_is_enabled(method)) {
   2297  1.1.1.19  christos 			if ((r = sshbuf_putf(b, "%s%s",
   2298  1.1.1.19  christos 			    sshbuf_len(b) ? "," : "", method->name)) != 0)
   2299  1.1.1.19  christos 				fatal("%s: buffer error: %s",
   2300  1.1.1.19  christos 				    __func__, ssh_err(r));
   2301       1.1  christos 		}
   2302       1.1  christos 	}
   2303  1.1.1.19  christos 	if ((list = sshbuf_dup_string(b)) == NULL)
   2304  1.1.1.14  christos 		fatal("%s: sshbuf_dup_string failed", __func__);
   2305  1.1.1.19  christos 	sshbuf_free(b);
   2306       1.1  christos 	return list;
   2307       1.1  christos }
   2308