jcs's openbsd hax
openbsd
at jcs 908 lines 24 kB view raw
1/* $OpenBSD: auth-options.c,v 1.102 2025/09/15 04:38:00 djm Exp $ */ 2/* 3 * Copyright (c) 2018 Damien Miller <djm@mindrot.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18#include <sys/types.h> 19#include <sys/queue.h> 20 21#include <stdlib.h> 22#include <netdb.h> 23#include <pwd.h> 24#include <string.h> 25#include <stdio.h> 26#include <stdint.h> 27#include <stdarg.h> 28#include <ctype.h> 29#include <limits.h> 30 31#include "xmalloc.h" 32#include "ssherr.h" 33#include "log.h" 34#include "sshbuf.h" 35#include "misc.h" 36#include "sshkey.h" 37#include "match.h" 38#include "ssh2.h" 39#include "auth-options.h" 40 41static int 42dup_strings(char ***dstp, size_t *ndstp, char **src, size_t nsrc) 43{ 44 char **dst; 45 size_t i, j; 46 47 *dstp = NULL; 48 *ndstp = 0; 49 50 if (nsrc == 0) 51 return 0; 52 if (nsrc >= SIZE_MAX / sizeof(*src) || 53 (dst = calloc(nsrc, sizeof(*src))) == NULL) 54 return -1; 55 for (i = 0; i < nsrc; i++) { 56 if ((dst[i] = strdup(src[i])) == NULL) { 57 for (j = 0; j < i; j++) 58 free(dst[j]); 59 free(dst); 60 return -1; 61 } 62 } 63 /* success */ 64 *dstp = dst; 65 *ndstp = nsrc; 66 return 0; 67} 68 69#define OPTIONS_CRITICAL 1 70#define OPTIONS_EXTENSIONS 2 71static int 72cert_option_list(struct sshauthopt *opts, struct sshbuf *oblob, 73 u_int which, int crit) 74{ 75 char *command, *allowed; 76 char *name = NULL; 77 struct sshbuf *c = NULL, *data = NULL; 78 int r, ret = -1, found; 79 80 if ((c = sshbuf_fromb(oblob)) == NULL) { 81 error_f("sshbuf_fromb failed"); 82 goto out; 83 } 84 85 while (sshbuf_len(c) > 0) { 86 sshbuf_free(data); 87 data = NULL; 88 if ((r = sshbuf_get_cstring(c, &name, NULL)) != 0 || 89 (r = sshbuf_froms(c, &data)) != 0) { 90 error_r(r, "Unable to parse certificate options"); 91 goto out; 92 } 93 debug3("found certificate option \"%.100s\" len %zu", 94 name, sshbuf_len(data)); 95 found = 0; 96 if ((which & OPTIONS_EXTENSIONS) != 0) { 97 if (strcmp(name, "no-touch-required") == 0) { 98 opts->no_require_user_presence = 1; 99 found = 1; 100 } else if (strcmp(name, "permit-X11-forwarding") == 0) { 101 opts->permit_x11_forwarding_flag = 1; 102 found = 1; 103 } else if (strcmp(name, 104 "permit-agent-forwarding") == 0) { 105 opts->permit_agent_forwarding_flag = 1; 106 found = 1; 107 } else if (strcmp(name, 108 "permit-port-forwarding") == 0) { 109 opts->permit_port_forwarding_flag = 1; 110 found = 1; 111 } else if (strcmp(name, "permit-pty") == 0) { 112 opts->permit_pty_flag = 1; 113 found = 1; 114 } else if (strcmp(name, "permit-user-rc") == 0) { 115 opts->permit_user_rc = 1; 116 found = 1; 117 } 118 } 119 if (!found && (which & OPTIONS_CRITICAL) != 0) { 120 if (strcmp(name, "verify-required") == 0) { 121 opts->require_verify = 1; 122 found = 1; 123 } else if (strcmp(name, "force-command") == 0) { 124 if ((r = sshbuf_get_cstring(data, &command, 125 NULL)) != 0) { 126 error_r(r, "Unable to parse \"%s\" " 127 "section", name); 128 goto out; 129 } 130 if (opts->force_command != NULL) { 131 error("Certificate has multiple " 132 "force-command options"); 133 free(command); 134 goto out; 135 } 136 opts->force_command = command; 137 found = 1; 138 } else if (strcmp(name, "source-address") == 0) { 139 if ((r = sshbuf_get_cstring(data, &allowed, 140 NULL)) != 0) { 141 error_r(r, "Unable to parse \"%s\" " 142 "section", name); 143 goto out; 144 } 145 if (opts->required_from_host_cert != NULL) { 146 error("Certificate has multiple " 147 "source-address options"); 148 free(allowed); 149 goto out; 150 } 151 /* Check syntax */ 152 if (addr_match_cidr_list(NULL, allowed) == -1) { 153 error("Certificate source-address " 154 "contents invalid"); 155 free(allowed); 156 goto out; 157 } 158 opts->required_from_host_cert = allowed; 159 found = 1; 160 } 161 } 162 163 if (!found) { 164 if (crit) { 165 error("Certificate critical option \"%s\" " 166 "is not supported", name); 167 goto out; 168 } else { 169 logit("Certificate extension \"%s\" " 170 "is not supported", name); 171 } 172 } else if (sshbuf_len(data) != 0) { 173 error("Certificate option \"%s\" corrupt " 174 "(extra data)", name); 175 goto out; 176 } 177 free(name); 178 name = NULL; 179 } 180 /* successfully parsed all options */ 181 ret = 0; 182 183 out: 184 free(name); 185 sshbuf_free(data); 186 sshbuf_free(c); 187 return ret; 188} 189 190struct sshauthopt * 191sshauthopt_new(void) 192{ 193 struct sshauthopt *ret; 194 195 if ((ret = calloc(1, sizeof(*ret))) == NULL) 196 return NULL; 197 ret->force_tun_device = -1; 198 return ret; 199} 200 201void 202sshauthopt_free(struct sshauthopt *opts) 203{ 204 size_t i; 205 206 if (opts == NULL) 207 return; 208 209 free(opts->cert_principals); 210 free(opts->force_command); 211 free(opts->required_from_host_cert); 212 free(opts->required_from_host_keys); 213 214 for (i = 0; i < opts->nenv; i++) 215 free(opts->env[i]); 216 free(opts->env); 217 218 for (i = 0; i < opts->npermitopen; i++) 219 free(opts->permitopen[i]); 220 free(opts->permitopen); 221 222 for (i = 0; i < opts->npermitlisten; i++) 223 free(opts->permitlisten[i]); 224 free(opts->permitlisten); 225 226 freezero(opts, sizeof(*opts)); 227} 228 229struct sshauthopt * 230sshauthopt_new_with_keys_defaults(void) 231{ 232 struct sshauthopt *ret = NULL; 233 234 if ((ret = sshauthopt_new()) == NULL) 235 return NULL; 236 237 /* Defaults for authorized_keys flags */ 238 ret->permit_port_forwarding_flag = 1; 239 ret->permit_agent_forwarding_flag = 1; 240 ret->permit_x11_forwarding_flag = 1; 241 ret->permit_pty_flag = 1; 242 ret->permit_user_rc = 1; 243 return ret; 244} 245 246/* 247 * Parse and record a permitopen/permitlisten directive. 248 * Return 0 on success. Return -1 on failure and sets *errstrp to error reason. 249 */ 250static int 251handle_permit(const char **optsp, int allow_bare_port, 252 char ***permitsp, size_t *npermitsp, const char **errstrp) 253{ 254 char *opt, *tmp, *cp, *host, **permits = *permitsp; 255 size_t npermits = *npermitsp; 256 const char *errstr = "unknown error"; 257 258 if (npermits > SSH_AUTHOPT_PERMIT_MAX) { 259 *errstrp = "too many permission directives"; 260 return -1; 261 } 262 if ((opt = opt_dequote(optsp, &errstr)) == NULL) { 263 return -1; 264 } 265 if (allow_bare_port && strchr(opt, ':') == NULL) { 266 /* 267 * Allow a bare port number in permitlisten to indicate a 268 * listen_host wildcard. 269 */ 270 if (asprintf(&tmp, "*:%s", opt) == -1) { 271 free(opt); 272 *errstrp = "memory allocation failed"; 273 return -1; 274 } 275 free(opt); 276 opt = tmp; 277 } 278 if ((tmp = strdup(opt)) == NULL) { 279 free(opt); 280 *errstrp = "memory allocation failed"; 281 return -1; 282 } 283 cp = tmp; 284 /* validate syntax before recording it. */ 285 host = hpdelim2(&cp, NULL); 286 if (host == NULL || strlen(host) >= NI_MAXHOST) { 287 free(tmp); 288 free(opt); 289 *errstrp = "invalid permission hostname"; 290 return -1; 291 } 292 /* 293 * don't want to use permitopen_port to avoid 294 * dependency on channels.[ch] here. 295 */ 296 if (cp == NULL || 297 (strcmp(cp, "*") != 0 && a2port(cp) <= 0)) { 298 free(tmp); 299 free(opt); 300 *errstrp = "invalid permission port"; 301 return -1; 302 } 303 /* XXX - add streamlocal support */ 304 free(tmp); 305 /* Record it */ 306 if ((permits = recallocarray(permits, npermits, npermits + 1, 307 sizeof(*permits))) == NULL) { 308 free(opt); 309 /* NB. don't update *permitsp if alloc fails */ 310 *errstrp = "memory allocation failed"; 311 return -1; 312 } 313 permits[npermits++] = opt; 314 *permitsp = permits; 315 *npermitsp = npermits; 316 return 0; 317} 318 319struct sshauthopt * 320sshauthopt_parse(const char *opts, const char **errstrp) 321{ 322 char **oarray, *opt, *cp, *tmp; 323 int r; 324 struct sshauthopt *ret = NULL; 325 const char *errstr = "unknown error"; 326 uint64_t valid_before; 327 size_t i, l; 328 329 if (errstrp != NULL) 330 *errstrp = NULL; 331 if ((ret = sshauthopt_new_with_keys_defaults()) == NULL) 332 goto alloc_fail; 333 334 if (opts == NULL) 335 return ret; 336 337 while (*opts && *opts != ' ' && *opts != '\t') { 338 /* flag options */ 339 if ((r = opt_flag("restrict", 0, &opts)) != -1) { 340 ret->restricted = 1; 341 ret->permit_port_forwarding_flag = 0; 342 ret->permit_agent_forwarding_flag = 0; 343 ret->permit_x11_forwarding_flag = 0; 344 ret->permit_pty_flag = 0; 345 ret->permit_user_rc = 0; 346 } else if ((r = opt_flag("cert-authority", 0, &opts)) != -1) { 347 ret->cert_authority = r; 348 } else if ((r = opt_flag("port-forwarding", 1, &opts)) != -1) { 349 ret->permit_port_forwarding_flag = r == 1; 350 } else if ((r = opt_flag("agent-forwarding", 1, &opts)) != -1) { 351 ret->permit_agent_forwarding_flag = r == 1; 352 } else if ((r = opt_flag("x11-forwarding", 1, &opts)) != -1) { 353 ret->permit_x11_forwarding_flag = r == 1; 354 } else if ((r = opt_flag("touch-required", 1, &opts)) != -1) { 355 ret->no_require_user_presence = r != 1; /* NB. flip */ 356 } else if ((r = opt_flag("verify-required", 1, &opts)) != -1) { 357 ret->require_verify = r == 1; 358 } else if ((r = opt_flag("pty", 1, &opts)) != -1) { 359 ret->permit_pty_flag = r == 1; 360 } else if ((r = opt_flag("user-rc", 1, &opts)) != -1) { 361 ret->permit_user_rc = r == 1; 362 } else if (opt_match(&opts, "command")) { 363 if (ret->force_command != NULL) { 364 errstr = "multiple \"command\" clauses"; 365 goto fail; 366 } 367 ret->force_command = opt_dequote(&opts, &errstr); 368 if (ret->force_command == NULL) 369 goto fail; 370 } else if (opt_match(&opts, "principals")) { 371 if (ret->cert_principals != NULL) { 372 errstr = "multiple \"principals\" clauses"; 373 goto fail; 374 } 375 ret->cert_principals = opt_dequote(&opts, &errstr); 376 if (ret->cert_principals == NULL) 377 goto fail; 378 } else if (opt_match(&opts, "from")) { 379 if (ret->required_from_host_keys != NULL) { 380 errstr = "multiple \"from\" clauses"; 381 goto fail; 382 } 383 ret->required_from_host_keys = opt_dequote(&opts, 384 &errstr); 385 if (ret->required_from_host_keys == NULL) 386 goto fail; 387 } else if (opt_match(&opts, "expiry-time")) { 388 if ((opt = opt_dequote(&opts, &errstr)) == NULL) 389 goto fail; 390 if (parse_absolute_time(opt, &valid_before) != 0 || 391 valid_before == 0) { 392 free(opt); 393 errstr = "invalid expires time"; 394 goto fail; 395 } 396 free(opt); 397 if (ret->valid_before == 0 || 398 valid_before < ret->valid_before) 399 ret->valid_before = valid_before; 400 } else if (opt_match(&opts, "environment")) { 401 if (ret->nenv > SSH_AUTHOPT_ENV_MAX) { 402 errstr = "too many environment strings"; 403 goto fail; 404 } 405 if ((opt = opt_dequote(&opts, &errstr)) == NULL) 406 goto fail; 407 /* env name must be alphanumeric and followed by '=' */ 408 if ((tmp = strchr(opt, '=')) == NULL) { 409 free(opt); 410 errstr = "invalid environment string"; 411 goto fail; 412 } 413 if ((cp = strdup(opt)) == NULL) { 414 free(opt); 415 goto alloc_fail; 416 } 417 l = (size_t)(tmp - opt); 418 cp[l] = '\0'; /* truncate at '=' */ 419 if (!valid_env_name(cp)) { 420 free(cp); 421 free(opt); 422 errstr = "invalid environment string"; 423 goto fail; 424 } 425 /* Check for duplicates; XXX O(n*log(n)) */ 426 for (i = 0; i < ret->nenv; i++) { 427 if (strncmp(ret->env[i], cp, l) == 0 && 428 ret->env[i][l] == '=') 429 break; 430 } 431 free(cp); 432 /* First match wins */ 433 if (i >= ret->nenv) { 434 /* Append it. */ 435 oarray = ret->env; 436 if ((ret->env = recallocarray(ret->env, 437 ret->nenv, ret->nenv + 1, 438 sizeof(*ret->env))) == NULL) { 439 free(opt); 440 /* put it back for cleanup */ 441 ret->env = oarray; 442 goto alloc_fail; 443 } 444 ret->env[ret->nenv++] = opt; 445 opt = NULL; /* transferred */ 446 } 447 free(opt); 448 } else if (opt_match(&opts, "permitopen")) { 449 if (handle_permit(&opts, 0, &ret->permitopen, 450 &ret->npermitopen, &errstr) != 0) 451 goto fail; 452 } else if (opt_match(&opts, "permitlisten")) { 453 if (handle_permit(&opts, 1, &ret->permitlisten, 454 &ret->npermitlisten, &errstr) != 0) 455 goto fail; 456 } else if (opt_match(&opts, "tunnel")) { 457 if ((opt = opt_dequote(&opts, &errstr)) == NULL) 458 goto fail; 459 ret->force_tun_device = a2tun(opt, NULL); 460 free(opt); 461 if (ret->force_tun_device == SSH_TUNID_ERR) { 462 errstr = "invalid tun device"; 463 goto fail; 464 } 465 } 466 /* 467 * Skip the comma, and move to the next option 468 * (or break out if there are no more). 469 */ 470 if (*opts == '\0' || *opts == ' ' || *opts == '\t') 471 break; /* End of options. */ 472 /* Anything other than a comma is an unknown option */ 473 if (*opts != ',') { 474 errstr = "unknown key option"; 475 goto fail; 476 } 477 opts++; 478 if (*opts == '\0') { 479 errstr = "unexpected end-of-options"; 480 goto fail; 481 } 482 } 483 484 /* success */ 485 if (errstrp != NULL) 486 *errstrp = NULL; 487 return ret; 488 489alloc_fail: 490 errstr = "memory allocation failed"; 491fail: 492 sshauthopt_free(ret); 493 if (errstrp != NULL) 494 *errstrp = errstr; 495 return NULL; 496} 497 498struct sshauthopt * 499sshauthopt_from_cert(struct sshkey *k) 500{ 501 struct sshauthopt *ret; 502 503 if (k == NULL || !sshkey_type_is_cert(k->type) || k->cert == NULL || 504 k->cert->type != SSH2_CERT_TYPE_USER) 505 return NULL; 506 507 if ((ret = sshauthopt_new()) == NULL) 508 return NULL; 509 510 /* Handle options and critical extensions separately */ 511 if (cert_option_list(ret, k->cert->critical, 512 OPTIONS_CRITICAL, 1) == -1) { 513 sshauthopt_free(ret); 514 return NULL; 515 } 516 if (cert_option_list(ret, k->cert->extensions, 517 OPTIONS_EXTENSIONS, 0) == -1) { 518 sshauthopt_free(ret); 519 return NULL; 520 } 521 /* success */ 522 return ret; 523} 524 525/* 526 * Merges "additional" options to "primary" and returns the result. 527 * NB. Some options from primary have primacy. 528 */ 529struct sshauthopt * 530sshauthopt_merge(const struct sshauthopt *primary, 531 const struct sshauthopt *additional, const char **errstrp) 532{ 533 struct sshauthopt *ret; 534 const char *errstr = "internal error"; 535 const char *tmp; 536 537 if (errstrp != NULL) 538 *errstrp = NULL; 539 540 if ((ret = sshauthopt_new()) == NULL) 541 goto alloc_fail; 542 543 /* cert_authority and cert_principals are cleared in result */ 544 545 /* Prefer access lists from primary. */ 546 /* XXX err is both set and mismatch? */ 547 tmp = primary->required_from_host_cert; 548 if (tmp == NULL) 549 tmp = additional->required_from_host_cert; 550 if (tmp != NULL && (ret->required_from_host_cert = strdup(tmp)) == NULL) 551 goto alloc_fail; 552 tmp = primary->required_from_host_keys; 553 if (tmp == NULL) 554 tmp = additional->required_from_host_keys; 555 if (tmp != NULL && (ret->required_from_host_keys = strdup(tmp)) == NULL) 556 goto alloc_fail; 557 558 /* 559 * force_tun_device, permitopen/permitlisten and environment all 560 * prefer the primary. 561 */ 562 ret->force_tun_device = primary->force_tun_device; 563 if (ret->force_tun_device == -1) 564 ret->force_tun_device = additional->force_tun_device; 565 if (primary->nenv > 0) { 566 if (dup_strings(&ret->env, &ret->nenv, 567 primary->env, primary->nenv) != 0) 568 goto alloc_fail; 569 } else if (additional->nenv) { 570 if (dup_strings(&ret->env, &ret->nenv, 571 additional->env, additional->nenv) != 0) 572 goto alloc_fail; 573 } 574 if (primary->npermitopen > 0) { 575 if (dup_strings(&ret->permitopen, &ret->npermitopen, 576 primary->permitopen, primary->npermitopen) != 0) 577 goto alloc_fail; 578 } else if (additional->npermitopen > 0) { 579 if (dup_strings(&ret->permitopen, &ret->npermitopen, 580 additional->permitopen, additional->npermitopen) != 0) 581 goto alloc_fail; 582 } 583 584 if (primary->npermitlisten > 0) { 585 if (dup_strings(&ret->permitlisten, &ret->npermitlisten, 586 primary->permitlisten, primary->npermitlisten) != 0) 587 goto alloc_fail; 588 } else if (additional->npermitlisten > 0) { 589 if (dup_strings(&ret->permitlisten, &ret->npermitlisten, 590 additional->permitlisten, additional->npermitlisten) != 0) 591 goto alloc_fail; 592 } 593 594#define OPTFLAG_AND(x) ret->x = (primary->x == 1) && (additional->x == 1) 595#define OPTFLAG_OR(x) ret->x = (primary->x == 1) || (additional->x == 1) 596 /* Permissive flags are logical-AND (i.e. must be set in both) */ 597 OPTFLAG_AND(permit_port_forwarding_flag); 598 OPTFLAG_AND(permit_agent_forwarding_flag); 599 OPTFLAG_AND(permit_x11_forwarding_flag); 600 OPTFLAG_AND(permit_pty_flag); 601 OPTFLAG_AND(permit_user_rc); 602 OPTFLAG_AND(no_require_user_presence); 603 /* Restrictive flags are logical-OR (i.e. must be set in either) */ 604 OPTFLAG_OR(require_verify); 605#undef OPTFLAG_AND 606 607 /* Earliest expiry time should win */ 608 if (primary->valid_before != 0) 609 ret->valid_before = primary->valid_before; 610 if (additional->valid_before != 0 && 611 additional->valid_before < ret->valid_before) 612 ret->valid_before = additional->valid_before; 613 614 /* 615 * When both multiple forced-command are specified, only 616 * proceed if they are identical, otherwise fail. 617 */ 618 if (primary->force_command != NULL && 619 additional->force_command != NULL) { 620 if (strcmp(primary->force_command, 621 additional->force_command) == 0) { 622 /* ok */ 623 ret->force_command = strdup(primary->force_command); 624 if (ret->force_command == NULL) 625 goto alloc_fail; 626 } else { 627 errstr = "forced command options do not match"; 628 goto fail; 629 } 630 } else if (primary->force_command != NULL) { 631 if ((ret->force_command = strdup( 632 primary->force_command)) == NULL) 633 goto alloc_fail; 634 } else if (additional->force_command != NULL) { 635 if ((ret->force_command = strdup( 636 additional->force_command)) == NULL) 637 goto alloc_fail; 638 } 639 /* success */ 640 if (errstrp != NULL) 641 *errstrp = NULL; 642 return ret; 643 644 alloc_fail: 645 errstr = "memory allocation failed"; 646 fail: 647 if (errstrp != NULL) 648 *errstrp = errstr; 649 sshauthopt_free(ret); 650 return NULL; 651} 652 653/* 654 * Copy options 655 */ 656struct sshauthopt * 657sshauthopt_copy(const struct sshauthopt *orig) 658{ 659 struct sshauthopt *ret; 660 661 if ((ret = sshauthopt_new()) == NULL) 662 return NULL; 663 664#define OPTSCALAR(x) ret->x = orig->x 665 OPTSCALAR(permit_port_forwarding_flag); 666 OPTSCALAR(permit_agent_forwarding_flag); 667 OPTSCALAR(permit_x11_forwarding_flag); 668 OPTSCALAR(permit_pty_flag); 669 OPTSCALAR(permit_user_rc); 670 OPTSCALAR(restricted); 671 OPTSCALAR(cert_authority); 672 OPTSCALAR(force_tun_device); 673 OPTSCALAR(valid_before); 674 OPTSCALAR(no_require_user_presence); 675 OPTSCALAR(require_verify); 676#undef OPTSCALAR 677#define OPTSTRING(x) \ 678 do { \ 679 if (orig->x != NULL && (ret->x = strdup(orig->x)) == NULL) { \ 680 sshauthopt_free(ret); \ 681 return NULL; \ 682 } \ 683 } while (0) 684 OPTSTRING(cert_principals); 685 OPTSTRING(force_command); 686 OPTSTRING(required_from_host_cert); 687 OPTSTRING(required_from_host_keys); 688#undef OPTSTRING 689 690 if (dup_strings(&ret->env, &ret->nenv, orig->env, orig->nenv) != 0 || 691 dup_strings(&ret->permitopen, &ret->npermitopen, 692 orig->permitopen, orig->npermitopen) != 0 || 693 dup_strings(&ret->permitlisten, &ret->npermitlisten, 694 orig->permitlisten, orig->npermitlisten) != 0) { 695 sshauthopt_free(ret); 696 return NULL; 697 } 698 return ret; 699} 700 701static int 702serialise_array(struct sshbuf *m, char **a, size_t n) 703{ 704 struct sshbuf *b; 705 size_t i; 706 int r = SSH_ERR_INTERNAL_ERROR; 707 708 if (n > INT_MAX) 709 return SSH_ERR_INTERNAL_ERROR; 710 711 if ((b = sshbuf_new()) == NULL) { 712 return SSH_ERR_ALLOC_FAIL; 713 } 714 for (i = 0; i < n; i++) { 715 if ((r = sshbuf_put_cstring(b, a[i])) != 0) 716 goto out; 717 } 718 if ((r = sshbuf_put_u32(m, n)) != 0 || 719 (r = sshbuf_put_stringb(m, b)) != 0) 720 goto out; 721 /* success */ 722 r = 0; 723 out: 724 sshbuf_free(b); 725 return r; 726} 727 728static int 729deserialise_array(struct sshbuf *m, char ***ap, size_t *np) 730{ 731 char **a = NULL; 732 size_t i, n = 0; 733 struct sshbuf *b = NULL; 734 u_int tmp; 735 int r = SSH_ERR_INTERNAL_ERROR; 736 737 if ((r = sshbuf_get_u32(m, &tmp)) != 0 || 738 (r = sshbuf_froms(m, &b)) != 0) 739 goto out; 740 if (tmp > INT_MAX) { 741 r = SSH_ERR_INVALID_FORMAT; 742 goto out; 743 } 744 n = tmp; 745 if (n > 0 && (a = calloc(n, sizeof(*a))) == NULL) { 746 r = SSH_ERR_ALLOC_FAIL; 747 goto out; 748 } 749 for (i = 0; i < n; i++) { 750 if ((r = sshbuf_get_cstring(b, &a[i], NULL)) != 0) 751 goto out; 752 } 753 /* success */ 754 r = 0; 755 *ap = a; 756 a = NULL; 757 *np = n; 758 n = 0; 759 out: 760 if (a != NULL) { 761 for (i = 0; i < n; i++) 762 free(a[i]); 763 free(a); 764 } 765 sshbuf_free(b); 766 return r; 767} 768 769static int 770serialise_nullable_string(struct sshbuf *m, const char *s) 771{ 772 int r; 773 774 if ((r = sshbuf_put_u8(m, s == NULL)) != 0 || 775 (r = sshbuf_put_cstring(m, s)) != 0) 776 return r; 777 return 0; 778} 779 780static int 781deserialise_nullable_string(struct sshbuf *m, char **sp) 782{ 783 int r; 784 u_char flag; 785 786 *sp = NULL; 787 if ((r = sshbuf_get_u8(m, &flag)) != 0 || 788 (r = sshbuf_get_cstring(m, flag ? NULL : sp, NULL)) != 0) 789 return r; 790 return 0; 791} 792 793int 794sshauthopt_serialise(const struct sshauthopt *opts, struct sshbuf *m, 795 int untrusted) 796{ 797 int r = SSH_ERR_INTERNAL_ERROR; 798 799 /* Flag options */ 800 if ((r = sshbuf_put_u8(m, opts->permit_port_forwarding_flag)) != 0 || 801 (r = sshbuf_put_u8(m, opts->permit_agent_forwarding_flag)) != 0 || 802 (r = sshbuf_put_u8(m, opts->permit_x11_forwarding_flag)) != 0 || 803 (r = sshbuf_put_u8(m, opts->permit_pty_flag)) != 0 || 804 (r = sshbuf_put_u8(m, opts->permit_user_rc)) != 0 || 805 (r = sshbuf_put_u8(m, opts->restricted)) != 0 || 806 (r = sshbuf_put_u8(m, opts->cert_authority)) != 0 || 807 (r = sshbuf_put_u8(m, opts->no_require_user_presence)) != 0 || 808 (r = sshbuf_put_u8(m, opts->require_verify)) != 0) 809 return r; 810 811 /* Simple integer options */ 812 if ((r = sshbuf_put_u64(m, opts->valid_before)) != 0) 813 return r; 814 815 /* tunnel number can be negative to indicate "unset" */ 816 if ((r = sshbuf_put_u8(m, opts->force_tun_device == -1)) != 0 || 817 (r = sshbuf_put_u32(m, (opts->force_tun_device < 0) ? 818 0 : (u_int)opts->force_tun_device)) != 0) 819 return r; 820 821 /* String options; these may be NULL */ 822 if ((r = serialise_nullable_string(m, 823 untrusted ? "yes" : opts->cert_principals)) != 0 || 824 (r = serialise_nullable_string(m, 825 untrusted ? "true" : opts->force_command)) != 0 || 826 (r = serialise_nullable_string(m, 827 untrusted ? NULL : opts->required_from_host_cert)) != 0 || 828 (r = serialise_nullable_string(m, 829 untrusted ? NULL : opts->required_from_host_keys)) != 0) 830 return r; 831 832 /* Array options */ 833 if ((r = serialise_array(m, opts->env, 834 untrusted ? 0 : opts->nenv)) != 0 || 835 (r = serialise_array(m, opts->permitopen, 836 untrusted ? 0 : opts->npermitopen)) != 0 || 837 (r = serialise_array(m, opts->permitlisten, 838 untrusted ? 0 : opts->npermitlisten)) != 0) 839 return r; 840 841 /* success */ 842 return 0; 843} 844 845int 846sshauthopt_deserialise(struct sshbuf *m, struct sshauthopt **optsp) 847{ 848 struct sshauthopt *opts = NULL; 849 int r = SSH_ERR_INTERNAL_ERROR; 850 u_char f; 851 u_int tmp; 852 853 if ((opts = calloc(1, sizeof(*opts))) == NULL) 854 return SSH_ERR_ALLOC_FAIL; 855 856 /* Flag options */ 857#define OPT_FLAG(x) \ 858 do { \ 859 if ((r = sshbuf_get_u8(m, &f)) != 0) \ 860 goto out; \ 861 opts->x = f; \ 862 } while (0) 863 OPT_FLAG(permit_port_forwarding_flag); 864 OPT_FLAG(permit_agent_forwarding_flag); 865 OPT_FLAG(permit_x11_forwarding_flag); 866 OPT_FLAG(permit_pty_flag); 867 OPT_FLAG(permit_user_rc); 868 OPT_FLAG(restricted); 869 OPT_FLAG(cert_authority); 870 OPT_FLAG(no_require_user_presence); 871 OPT_FLAG(require_verify); 872#undef OPT_FLAG 873 874 /* Simple integer options */ 875 if ((r = sshbuf_get_u64(m, &opts->valid_before)) != 0) 876 goto out; 877 878 /* tunnel number can be negative to indicate "unset" */ 879 if ((r = sshbuf_get_u8(m, &f)) != 0 || 880 (r = sshbuf_get_u32(m, &tmp)) != 0) 881 goto out; 882 opts->force_tun_device = f ? -1 : (int)tmp; 883 884 /* String options may be NULL */ 885 if ((r = deserialise_nullable_string(m, &opts->cert_principals)) != 0 || 886 (r = deserialise_nullable_string(m, &opts->force_command)) != 0 || 887 (r = deserialise_nullable_string(m, 888 &opts->required_from_host_cert)) != 0 || 889 (r = deserialise_nullable_string(m, 890 &opts->required_from_host_keys)) != 0) 891 goto out; 892 893 /* Array options */ 894 if ((r = deserialise_array(m, &opts->env, &opts->nenv)) != 0 || 895 (r = deserialise_array(m, 896 &opts->permitopen, &opts->npermitopen)) != 0 || 897 (r = deserialise_array(m, 898 &opts->permitlisten, &opts->npermitlisten)) != 0) 899 goto out; 900 901 /* success */ 902 r = 0; 903 *optsp = opts; 904 opts = NULL; 905 out: 906 sshauthopt_free(opts); 907 return r; 908}