Home | History | Annotate | Line # | Download | only in dist
auth2-chall.c revision 1.21
      1 /*	$NetBSD: auth2-chall.c,v 1.21 2026/04/08 18:58:40 christos Exp $	*/
      2 /* $OpenBSD: auth2-chall.c,v 1.60 2026/03/03 09:57:25 dtucker Exp $ */
      3 
      4 /*
      5  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
      6  * Copyright (c) 2001 Per Allansson.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include "includes.h"
     30 __RCSID("$NetBSD: auth2-chall.c,v 1.21 2026/04/08 18:58:40 christos Exp $");
     31 #include <sys/types.h>
     32 
     33 #include <stdlib.h>
     34 #include <stdio.h>
     35 #include <string.h>
     36 #include <stdarg.h>
     37 
     38 #include "xmalloc.h"
     39 #include "ssh2.h"
     40 #include "sshkey.h"
     41 #include "hostfile.h"
     42 #include "auth.h"
     43 #include "sshbuf.h"
     44 #include "packet.h"
     45 #include "dispatch.h"
     46 #include "ssherr.h"
     47 #include "log.h"
     48 #include "misc.h"
     49 #include "servconf.h"
     50 
     51 /* import */
     52 extern ServerOptions options;
     53 
     54 static int auth2_challenge_start(struct ssh *);
     55 static int send_userauth_info_request(struct ssh *);
     56 static int input_userauth_info_response(int, uint32_t, struct ssh *);
     57 
     58 #ifdef BSD_AUTH
     59 extern KbdintDevice mm_bsdauth_device;
     60 #else
     61 #ifdef USE_PAM
     62 extern KbdintDevice sshpam_device;
     63 #endif
     64 #ifdef SKEY
     65 extern KbdintDevice skey_device;
     66 #endif
     67 #endif
     68 
     69 KbdintDevice *devices[] = {
     70 #ifdef BSD_AUTH
     71 	&bsdauth_device,
     72 #else
     73 #ifdef USE_PAM
     74 	&sshpam_device,
     75 #endif
     76 #ifdef SKEY
     77 	&skey_device,
     78 #endif
     79 #endif
     80 	NULL
     81 };
     82 
     83 typedef struct KbdintAuthctxt KbdintAuthctxt;
     84 struct KbdintAuthctxt
     85 {
     86 	char *devices;
     87 	void *ctxt;
     88 	KbdintDevice *device;
     89 	u_int nreq;
     90 	u_int devices_done;
     91 };
     92 
     93 #ifdef USE_PAM
     94 void remove_kbdint_device(const char *);
     95 void
     96 remove_kbdint_device(const char *xdevname)
     97 {
     98 	int i, j;
     99 
    100 	for (i = 0; devices[i] != NULL; i++)
    101 		if (strcmp(devices[i]->name, xdevname) == 0) {
    102 			for (j = i; devices[j] != NULL; j++)
    103 				devices[j] = devices[j+1];
    104 			i--;
    105 		}
    106 }
    107 #endif
    108 
    109 static KbdintAuthctxt *
    110 kbdint_alloc(const char *devs)
    111 {
    112 	KbdintAuthctxt *kbdintctxt;
    113 	struct sshbuf *b;
    114 	int i, r;
    115 
    116 #ifdef USE_PAM
    117 	if (!options.use_pam)
    118 		remove_kbdint_device("pam");
    119 #endif
    120 
    121 	kbdintctxt = xcalloc(1, sizeof(KbdintAuthctxt));
    122 	if (strcmp(devs, "") == 0) {
    123 		if ((b = sshbuf_new()) == NULL)
    124 			fatal_f("sshbuf_new failed");
    125 		for (i = 0; devices[i]; i++) {
    126 			if ((r = sshbuf_putf(b, "%s%s",
    127 			    sshbuf_len(b) ? "," : "", devices[i]->name)) != 0)
    128 				fatal_fr(r, "buffer error");
    129 		}
    130 		if ((kbdintctxt->devices = sshbuf_dup_string(b)) == NULL)
    131 			fatal_f("sshbuf_dup_string failed");
    132 		sshbuf_free(b);
    133 	} else {
    134 		kbdintctxt->devices = xstrdup(devs);
    135 	}
    136 	debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
    137 	kbdintctxt->ctxt = NULL;
    138 	kbdintctxt->device = NULL;
    139 	kbdintctxt->nreq = 0;
    140 
    141 	return kbdintctxt;
    142 }
    143 static void
    144 kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
    145 {
    146 	if (kbdintctxt->ctxt) {
    147 		kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
    148 		kbdintctxt->ctxt = NULL;
    149 	}
    150 	kbdintctxt->device = NULL;
    151 }
    152 static void
    153 kbdint_free(KbdintAuthctxt *kbdintctxt)
    154 {
    155 	if (kbdintctxt->device)
    156 		kbdint_reset_device(kbdintctxt);
    157 	free(kbdintctxt->devices);
    158 	freezero(kbdintctxt, sizeof(*kbdintctxt));
    159 }
    160 /* get next device */
    161 static int
    162 kbdint_next_device(Authctxt *authctxt, KbdintAuthctxt *kbdintctxt)
    163 {
    164 	size_t len;
    165 	char *t;
    166 	size_t i;
    167 
    168 	if (kbdintctxt->device)
    169 		kbdint_reset_device(kbdintctxt);
    170 	do {
    171 		len = kbdintctxt->devices ?
    172 		    strcspn(kbdintctxt->devices, ",") : 0;
    173 
    174 		if (len == 0)
    175 			break;
    176 		for (i = 0; devices[i]; i++) {
    177 			if (i >= sizeof(kbdintctxt->devices_done) * 8 ||
    178 			    i >= sizeof(devices) / sizeof(devices[0]))
    179 				fatal_f("internal error: too many devices");
    180 			if ((kbdintctxt->devices_done & (1 << i)) != 0 ||
    181 			    !auth2_method_allowed(authctxt,
    182 			    "keyboard-interactive", devices[i]->name))
    183 				continue;
    184 			if (strlen(devices[i]->name) == len &&
    185 			    memcmp(kbdintctxt->devices, devices[i]->name,
    186 			    len) == 0) {
    187 				kbdintctxt->device = devices[i];
    188 				kbdintctxt->devices_done |= 1 << i;
    189 			}
    190 		}
    191 		t = kbdintctxt->devices;
    192 		kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
    193 		free(t);
    194 		debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
    195 		    kbdintctxt->devices : "<empty>");
    196 	} while (kbdintctxt->devices && !kbdintctxt->device);
    197 
    198 	return kbdintctxt->device ? 1 : 0;
    199 }
    200 
    201 /*
    202  * try challenge-response, set authctxt->postponed if we have to
    203  * wait for the response.
    204  */
    205 int
    206 auth2_challenge(struct ssh *ssh, char *devs)
    207 {
    208 	Authctxt *authctxt = ssh->authctxt;
    209 	debug("auth2_challenge: user=%s devs=%s",
    210 	    authctxt->user ? authctxt->user : "<nouser>",
    211 	    devs ? devs : "<no devs>");
    212 
    213 	if (authctxt->user == NULL || !devs)
    214 		return 0;
    215 	if (authctxt->kbdintctxt == NULL)
    216 		authctxt->kbdintctxt = kbdint_alloc(devs);
    217 	return auth2_challenge_start(ssh);
    218 }
    219 
    220 /* unregister kbd-int callbacks and context */
    221 void
    222 auth2_challenge_stop(struct ssh *ssh)
    223 {
    224 	Authctxt *authctxt = ssh->authctxt;
    225 	/* unregister callback */
    226 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
    227 	if (authctxt->kbdintctxt != NULL) {
    228 		kbdint_free(authctxt->kbdintctxt);
    229 		authctxt->kbdintctxt = NULL;
    230 	}
    231 }
    232 
    233 /* side effect: sets authctxt->postponed if a reply was sent*/
    234 static int
    235 auth2_challenge_start(struct ssh *ssh)
    236 {
    237 	Authctxt *authctxt = ssh->authctxt;
    238 	KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
    239 
    240 	debug2("auth2_challenge_start: devices %s",
    241 	    kbdintctxt->devices ?  kbdintctxt->devices : "<empty>");
    242 
    243 	if (kbdint_next_device(authctxt, kbdintctxt) == 0) {
    244 		auth2_challenge_stop(ssh);
    245 		return 0;
    246 	}
    247 	debug("auth2_challenge_start: trying authentication method '%s'",
    248 	    kbdintctxt->device->name);
    249 
    250 	if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
    251 		auth2_challenge_stop(ssh);
    252 		return 0;
    253 	}
    254 	if (send_userauth_info_request(ssh) == 0) {
    255 		auth2_challenge_stop(ssh);
    256 		return 0;
    257 	}
    258 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE,
    259 	    &input_userauth_info_response);
    260 
    261 	authctxt->postponed = 1;
    262 	return 0;
    263 }
    264 
    265 static int
    266 send_userauth_info_request(struct ssh *ssh)
    267 {
    268 	Authctxt *authctxt = ssh->authctxt;
    269 	KbdintAuthctxt *kbdintctxt;
    270 	char *name, *instr, **prompts;
    271 	int r;
    272 	u_int i, *echo_on;
    273 
    274 	kbdintctxt = authctxt->kbdintctxt;
    275 	if (kbdintctxt->device->query(kbdintctxt->ctxt,
    276 	    &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
    277 		return 0;
    278 
    279 	if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST)) != 0 ||
    280 	    (r = sshpkt_put_cstring(ssh, name)) != 0 ||
    281 	    (r = sshpkt_put_cstring(ssh, instr)) != 0 ||
    282 	    (r = sshpkt_put_cstring(ssh, "")) != 0 ||	/* language not used */
    283 	    (r = sshpkt_put_u32(ssh, kbdintctxt->nreq)) != 0)
    284 		fatal_fr(r, "start packet");
    285 	for (i = 0; i < kbdintctxt->nreq; i++) {
    286 		if ((r = sshpkt_put_cstring(ssh, prompts[i])) != 0 ||
    287 		    (r = sshpkt_put_u8(ssh, echo_on[i])) != 0)
    288 			fatal_fr(r, "assemble packet");
    289 	}
    290 	if ((r = sshpkt_send(ssh)) != 0 ||
    291 	    (r = ssh_packet_write_wait(ssh)) < 0)
    292 		fatal_fr(r, "send packet");
    293 
    294 	for (i = 0; i < kbdintctxt->nreq; i++)
    295 		free(prompts[i]);
    296 	free(prompts);
    297 	free(echo_on);
    298 	free(name);
    299 	free(instr);
    300 	return 1;
    301 }
    302 
    303 static int
    304 input_userauth_info_response(int type, uint32_t seq, struct ssh *ssh)
    305 {
    306 	Authctxt *authctxt = ssh->authctxt;
    307 	KbdintAuthctxt *kbdintctxt;
    308 	int authenticated = 0, res;
    309 	int r;
    310 	u_int i, nresp;
    311 	const char *devicename = NULL;
    312 	char **response = NULL;
    313 
    314 	if (authctxt == NULL)
    315 		fatal_f("no authctxt");
    316 	kbdintctxt = authctxt->kbdintctxt;
    317 	if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
    318 		fatal_f("no kbdintctxt");
    319 	if (kbdintctxt->device == NULL)
    320 		fatal_f("no device");
    321 
    322 	authctxt->postponed = 0;	/* reset */
    323 	if ((r = sshpkt_get_u32(ssh, &nresp)) != 0)
    324 		fatal_fr(r, "parse packet");
    325 	if (nresp != kbdintctxt->nreq)
    326 		fatal_f("wrong number of replies");
    327 	if (nresp > 100)
    328 		fatal_f("too many replies");
    329 	if (nresp > 0) {
    330 		response = xcalloc(nresp, sizeof(char *));
    331 		for (i = 0; i < nresp; i++) {
    332 			if ((r = sshpkt_get_cstring(ssh, &response[i], NULL)) != 0)
    333 				fatal_fr(r, "parse response");
    334 		}
    335 	}
    336 	if ((r = sshpkt_get_end(ssh)) != 0)
    337 		fatal_fr(r, "parse packet");
    338 
    339 	res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
    340 
    341 	for (i = 0; i < nresp; i++) {
    342 		explicit_bzero(response[i], strlen(response[i]));
    343 		free(response[i]);
    344 	}
    345 	free(response);
    346 
    347 	switch (res) {
    348 	case 0:
    349 		/* Success! */
    350 		authenticated = authctxt->valid ? 1 : 0;
    351 		break;
    352 	case 1:
    353 		/* Authentication needs further interaction */
    354 		if (send_userauth_info_request(ssh) == 1)
    355 			authctxt->postponed = 1;
    356 		break;
    357 	default:
    358 		/* Failure! */
    359 		break;
    360 	}
    361 	devicename = kbdintctxt->device->name;
    362 	if (!authctxt->postponed) {
    363 		if (authenticated) {
    364 			auth2_challenge_stop(ssh);
    365 		} else {
    366 			/* start next device */
    367 			/* may set authctxt->postponed */
    368 			auth2_challenge_start(ssh);
    369 		}
    370 	}
    371 	userauth_finish(ssh, authenticated, "keyboard-interactive",
    372 	    devicename);
    373 	return 0;
    374 }
    375