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