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