Home | History | Annotate | Line # | Download | only in cgdconfig
cgdconfig.c revision 1.60
      1  1.60  riastrad /* $NetBSD: cgdconfig.c,v 1.60 2022/09/13 10:14:32 riastradh Exp $ */
      2   1.1     elric 
      3   1.1     elric /*-
      4   1.5     elric  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
      5   1.1     elric  * All rights reserved.
      6   1.1     elric  *
      7   1.1     elric  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1     elric  * by Roland C. Dowdeswell.
      9   1.1     elric  *
     10   1.1     elric  * Redistribution and use in source and binary forms, with or without
     11   1.1     elric  * modification, are permitted provided that the following conditions
     12   1.1     elric  * are met:
     13   1.1     elric  * 1. Redistributions of source code must retain the above copyright
     14   1.1     elric  *    notice, this list of conditions and the following disclaimer.
     15   1.1     elric  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1     elric  *    notice, this list of conditions and the following disclaimer in the
     17   1.1     elric  *    documentation and/or other materials provided with the distribution.
     18   1.1     elric  *
     19   1.1     elric  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1     elric  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1     elric  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1     elric  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1     elric  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1     elric  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1     elric  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1     elric  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1     elric  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1     elric  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1     elric  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1     elric  */
     31   1.1     elric 
     32   1.1     elric #include <sys/cdefs.h>
     33   1.1     elric #ifndef lint
     34  1.24     lukem __COPYRIGHT("@(#) Copyright (c) 2002, 2003\
     35  1.24     lukem  The NetBSD Foundation, Inc.  All rights reserved.");
     36  1.60  riastrad __RCSID("$NetBSD: cgdconfig.c,v 1.60 2022/09/13 10:14:32 riastradh Exp $");
     37   1.1     elric #endif
     38   1.1     elric 
     39  1.53       nia #ifdef HAVE_ARGON2
     40  1.53       nia #include <argon2.h>
     41  1.53       nia #endif
     42  1.56  riastrad #include <assert.h>
     43   1.5     elric #include <err.h>
     44   1.1     elric #include <errno.h>
     45   1.1     elric #include <fcntl.h>
     46   1.1     elric #include <libgen.h>
     47   1.1     elric #include <stdio.h>
     48   1.1     elric #include <stdlib.h>
     49   1.1     elric #include <string.h>
     50   1.1     elric #include <unistd.h>
     51   1.1     elric #include <util.h>
     52  1.35  christos #include <paths.h>
     53  1.35  christos #include <dirent.h>
     54   1.1     elric 
     55  1.54  riastrad /* base64 gunk */
     56  1.54  riastrad #include <netinet/in.h>
     57  1.54  riastrad #include <arpa/nameser.h>
     58  1.54  riastrad #include <resolv.h>
     59  1.54  riastrad 
     60   1.1     elric #include <sys/ioctl.h>
     61  1.41  christos #include <sys/stat.h>
     62  1.37   mlelstv #include <sys/bootblock.h>
     63   1.3     elric #include <sys/disklabel.h>
     64  1.37   mlelstv #include <sys/disklabel_gpt.h>
     65  1.14     elric #include <sys/mman.h>
     66   1.1     elric #include <sys/param.h>
     67  1.13     elric #include <sys/resource.h>
     68  1.34  christos #include <sys/statvfs.h>
     69  1.35  christos #include <sys/bitops.h>
     70  1.56  riastrad #include <sys/queue.h>
     71   1.1     elric 
     72   1.1     elric #include <dev/cgdvar.h>
     73   1.1     elric 
     74   1.5     elric #include <ufs/ffs/fs.h>
     75   1.5     elric 
     76   1.1     elric #include "params.h"
     77   1.1     elric #include "pkcs5_pbkdf2.h"
     78   1.1     elric #include "utils.h"
     79  1.28     pooka #include "cgdconfig.h"
     80  1.32     pooka #include "prog_ops.h"
     81  1.56  riastrad #include "hkdf_hmac_sha256.h"
     82   1.1     elric 
     83   1.1     elric #define CGDCONFIG_CFILE		CGDCONFIG_DIR "/cgd.conf"
     84   1.1     elric 
     85  1.17    cbiere enum action {
     86  1.17    cbiere 	 ACTION_DEFAULT,		/* default -> configure */
     87  1.17    cbiere 	 ACTION_CONFIGURE,		/* configure, with paramsfile */
     88  1.17    cbiere 	 ACTION_UNCONFIGURE,		/* unconfigure */
     89  1.17    cbiere 	 ACTION_GENERATE,		/* generate a paramsfile */
     90  1.17    cbiere 	 ACTION_GENERATE_CONVERT,	/* generate a ``dup'' paramsfile */
     91  1.17    cbiere 	 ACTION_CONFIGALL,		/* configure all from config file */
     92  1.17    cbiere 	 ACTION_UNCONFIGALL,		/* unconfigure all from config file */
     93  1.34  christos 	 ACTION_CONFIGSTDIN,		/* configure, key from stdin */
     94  1.54  riastrad 	 ACTION_LIST,			/* list configured devices */
     95  1.54  riastrad 	 ACTION_PRINTKEY,		/* print key to stdout */
     96  1.55  riastrad 	 ACTION_PRINTALLKEYS,		/* print all keys to stdout */
     97  1.17    cbiere };
     98   1.1     elric 
     99   1.1     elric /* if nflag is set, do not configure/unconfigure the cgd's */
    100   1.1     elric 
    101   1.1     elric int	nflag = 0;
    102   1.1     elric 
    103  1.57  riastrad /* if Sflag is set, generate shared keys */
    104  1.57  riastrad 
    105  1.57  riastrad int	Sflag = 0;
    106  1.57  riastrad 
    107  1.22     elric /* if pflag is set to PFLAG_STDIN read from stdin rather than getpass(3) */
    108  1.22     elric 
    109  1.46     alnsn #define	PFLAG_GETPASS		0x01
    110  1.46     alnsn #define	PFLAG_GETPASS_ECHO	0x02
    111  1.46     alnsn #define	PFLAG_GETPASS_MASK	0x03
    112  1.46     alnsn #define	PFLAG_STDIN		0x04
    113  1.22     elric int	pflag = PFLAG_GETPASS;
    114  1.22     elric 
    115  1.56  riastrad /*
    116  1.56  riastrad  * When configuring all cgds, save a cache of shared keys for key
    117  1.59  riastrad  * derivation.  If the _first_ verification with a shared key fails, we
    118  1.59  riastrad  * chuck it and start over; if _subsequent_ verifications fail, we
    119  1.59  riastrad  * assume the disk is wrong and give up on it immediately.
    120  1.56  riastrad  */
    121  1.56  riastrad 
    122  1.56  riastrad struct sharedkey {
    123  1.56  riastrad 	int			 alg;
    124  1.56  riastrad 	string_t		*id;
    125  1.56  riastrad 	bits_t			*key;
    126  1.56  riastrad 	LIST_ENTRY(sharedkey)	 list;
    127  1.59  riastrad 	SLIST_ENTRY(sharedkey)	 used;
    128  1.59  riastrad 	int			 verified;
    129  1.56  riastrad };
    130  1.56  riastrad LIST_HEAD(, sharedkey) sharedkeys;
    131  1.59  riastrad SLIST_HEAD(sharedkeyhits, sharedkey);
    132  1.56  riastrad 
    133   1.3     elric static int	configure(int, char **, struct params *, int);
    134   1.1     elric static int	configure_stdin(struct params *, int argc, char **);
    135  1.57  riastrad static int	generate(struct params *, int, char **, const char *,
    136  1.57  riastrad 		    const char *);
    137  1.58  riastrad static int	generate_convert(struct params *, int, char **, const char *,
    138  1.58  riastrad 		    const char *);
    139   1.3     elric static int	unconfigure(int, char **, struct params *, int);
    140   1.3     elric static int	do_all(const char *, int, char **,
    141   1.3     elric 		       int (*)(int, char **, struct params *, int));
    142  1.34  christos static int	do_list(int, char **);
    143  1.55  riastrad static int	printkey(const char *, const char *, const char *, ...)
    144  1.55  riastrad 		    __printflike(3,4);
    145  1.55  riastrad static int	printkey1(int, char **, struct params *, int);
    146  1.54  riastrad static int	do_printkey(int, char **);
    147   1.1     elric 
    148   1.1     elric #define CONFIG_FLAGS_FROMALL	1	/* called from configure_all() */
    149   1.1     elric #define CONFIG_FLAGS_FROMMAIN	2	/* called from main() */
    150   1.1     elric 
    151   1.2     elric static int	 configure_params(int, const char *, const char *,
    152   1.2     elric 				  struct params *);
    153  1.13     elric static void	 eliminate_cores(void);
    154  1.59  riastrad static bits_t	*getkey(const char *, struct keygen *, size_t,
    155  1.59  riastrad 		     struct sharedkeyhits *);
    156  1.19  christos static bits_t	*getkey_storedkey(const char *, struct keygen *, size_t);
    157  1.19  christos static bits_t	*getkey_randomkey(const char *, struct keygen *, size_t, int);
    158  1.53       nia #ifdef HAVE_ARGON2
    159  1.53       nia static bits_t	*getkey_argon2id(const char *, struct keygen *, size_t);
    160  1.53       nia #endif
    161  1.23     elric static bits_t	*getkey_pkcs5_pbkdf2(const char *, struct keygen *, size_t,
    162  1.23     elric 				     int);
    163  1.23     elric static bits_t	*getkey_shell_cmd(const char *, struct keygen *, size_t);
    164  1.22     elric static char	*maybe_getpass(char *);
    165  1.17    cbiere static int	 opendisk_werror(const char *, char *, size_t);
    166   1.3     elric static int	 unconfigure_fd(int);
    167   1.3     elric static int	 verify(struct params *, int);
    168   1.5     elric static int	 verify_disklabel(int);
    169   1.5     elric static int	 verify_ffs(int);
    170   1.9        cb static int	 verify_reenter(struct params *);
    171  1.37   mlelstv static int	 verify_mbr(int);
    172  1.37   mlelstv static int	 verify_gpt(int);
    173   1.1     elric 
    174  1.33     joerg __dead static void	 usage(void);
    175   1.1     elric 
    176   1.1     elric /* Verbose Framework */
    177  1.17    cbiere unsigned	verbose = 0;
    178   1.1     elric 
    179   1.1     elric #define VERBOSE(x,y)	if (verbose >= x) y
    180  1.19  christos #define VPRINTF(x,y)	if (verbose >= x) (void)printf y
    181   1.1     elric 
    182   1.1     elric static void
    183   1.1     elric usage(void)
    184   1.1     elric {
    185   1.1     elric 
    186  1.46     alnsn 	(void)fprintf(stderr, "usage: %s [-enpv] [-V vmeth] cgd dev "
    187  1.45       kre 	    "[paramsfile]\n", getprogname());
    188  1.46     alnsn 	(void)fprintf(stderr, "       %s -C [-enpv] [-f configfile]\n",
    189   1.1     elric 	    getprogname());
    190  1.58  riastrad 	(void)fprintf(stderr, "       %s -G [-enpSv] [-i ivmeth] [-k kgmeth] "
    191  1.58  riastrad 	    "[-P paramsfile] [-o outfile] paramsfile\n", getprogname());
    192  1.57  riastrad 	(void)fprintf(stderr, "       %s -g [-Sv] [-i ivmeth] [-k kgmeth] "
    193  1.57  riastrad 	    "[-P paramsfile] [-o outfile] alg [keylen]\n", getprogname());
    194  1.45       kre 	(void)fprintf(stderr, "       %s -l [-v[v]] [cgd]\n", getprogname());
    195  1.19  christos 	(void)fprintf(stderr, "       %s -s [-nv] [-i ivmeth] cgd dev alg "
    196   1.1     elric 	    "[keylen]\n", getprogname());
    197  1.54  riastrad 	(void)fprintf(stderr, "       %s -t paramsfile\n", getprogname());
    198  1.55  riastrad 	(void)fprintf(stderr, "       %s -T [-f configfile]\n", getprogname());
    199  1.45       kre 	(void)fprintf(stderr, "       %s -U [-nv] [-f configfile]\n",
    200  1.45       kre 	    getprogname());
    201  1.19  christos 	(void)fprintf(stderr, "       %s -u [-nv] cgd\n", getprogname());
    202  1.17    cbiere 	exit(EXIT_FAILURE);
    203  1.17    cbiere }
    204  1.17    cbiere 
    205  1.17    cbiere static int
    206  1.19  christos parse_size_t(const char *s, size_t *l)
    207  1.17    cbiere {
    208  1.17    cbiere 	char *endptr;
    209  1.17    cbiere 	long v;
    210  1.17    cbiere 
    211  1.17    cbiere 	errno = 0;
    212  1.17    cbiere 	v = strtol(s, &endptr, 10);
    213  1.17    cbiere 	if ((v == LONG_MIN || v == LONG_MAX) && errno)
    214  1.17    cbiere 		return -1;
    215  1.17    cbiere 	if (v < INT_MIN || v > INT_MAX) {
    216  1.17    cbiere 		errno = ERANGE;
    217  1.17    cbiere 		return -1;
    218  1.17    cbiere 	}
    219  1.17    cbiere 	if (endptr == s) {
    220  1.17    cbiere 		errno = EINVAL;
    221  1.17    cbiere 		return -1;
    222  1.17    cbiere 	}
    223  1.19  christos 	*l = (size_t)v;
    224  1.19  christos 	return 0;
    225  1.17    cbiere }
    226  1.17    cbiere 
    227  1.17    cbiere static void
    228  1.17    cbiere set_action(enum action *action, enum action value)
    229  1.17    cbiere {
    230  1.17    cbiere 	if (*action != ACTION_DEFAULT)
    231  1.17    cbiere 		usage();
    232  1.17    cbiere 	*action = value;
    233   1.1     elric }
    234   1.1     elric 
    235   1.1     elric int
    236   1.1     elric main(int argc, char **argv)
    237   1.1     elric {
    238   1.5     elric 	struct params *p;
    239   1.5     elric 	struct params *tp;
    240   1.5     elric 	struct keygen *kg;
    241  1.17    cbiere 	enum action action = ACTION_DEFAULT;
    242   1.1     elric 	int	ch;
    243  1.17    cbiere 	const char	*cfile = NULL;
    244  1.17    cbiere 	const char	*outfile = NULL;
    245  1.57  riastrad 	const char	*Pfile = NULL;
    246   1.1     elric 
    247  1.15     elric 	setprogname(*argv);
    248  1.56  riastrad 	if (hkdf_hmac_sha256_selftest())
    249  1.56  riastrad 		err(EXIT_FAILURE, "Crypto self-test failed");
    250  1.13     elric 	eliminate_cores();
    251  1.14     elric 	if (mlockall(MCL_FUTURE))
    252  1.14     elric 		err(EXIT_FAILURE, "Can't lock memory");
    253   1.5     elric 	p = params_new();
    254   1.5     elric 	kg = NULL;
    255   1.1     elric 
    256  1.57  riastrad 	while ((ch = getopt(argc, argv, "CGP:STUV:b:ef:gi:k:lno:sptuv")) != -1)
    257   1.1     elric 		switch (ch) {
    258   1.1     elric 		case 'C':
    259  1.17    cbiere 			set_action(&action, ACTION_CONFIGALL);
    260   1.1     elric 			break;
    261   1.5     elric 		case 'G':
    262  1.17    cbiere 			set_action(&action, ACTION_GENERATE_CONVERT);
    263   1.5     elric 			break;
    264  1.57  riastrad 		case 'P':
    265  1.57  riastrad 			if (Pfile)
    266  1.57  riastrad 				usage();
    267  1.57  riastrad 			Pfile = estrdup(optarg);
    268  1.57  riastrad 			break;
    269  1.57  riastrad 		case 'S':
    270  1.57  riastrad 			Sflag = 1;
    271  1.57  riastrad 			break;
    272  1.55  riastrad 		case 'T':
    273  1.55  riastrad 			set_action(&action, ACTION_PRINTALLKEYS);
    274  1.55  riastrad 			break;
    275   1.1     elric 		case 'U':
    276  1.17    cbiere 			set_action(&action, ACTION_UNCONFIGALL);
    277   1.1     elric 			break;
    278   1.3     elric 		case 'V':
    279   1.5     elric 			tp = params_verify_method(string_fromcharstar(optarg));
    280   1.5     elric 			if (!tp)
    281   1.3     elric 				usage();
    282   1.5     elric 			p = params_combine(p, tp);
    283   1.3     elric 			break;
    284   1.1     elric 		case 'b':
    285  1.17    cbiere 			{
    286  1.19  christos 				size_t size;
    287  1.17    cbiere 
    288  1.19  christos 				if (parse_size_t(optarg, &size) == -1)
    289  1.17    cbiere 					usage();
    290  1.17    cbiere 				tp = params_bsize(size);
    291  1.17    cbiere 				if (!tp)
    292  1.17    cbiere 					usage();
    293  1.17    cbiere 				p = params_combine(p, tp);
    294  1.17    cbiere 			}
    295   1.1     elric 			break;
    296  1.46     alnsn 		case 'e':
    297  1.46     alnsn 			pflag = PFLAG_GETPASS_ECHO;
    298  1.46     alnsn 			break;
    299   1.1     elric 		case 'f':
    300  1.17    cbiere 			if (cfile)
    301  1.17    cbiere 				usage();
    302  1.17    cbiere 			cfile = estrdup(optarg);
    303   1.1     elric 			break;
    304   1.1     elric 		case 'g':
    305  1.17    cbiere 			set_action(&action, ACTION_GENERATE);
    306   1.1     elric 			break;
    307   1.1     elric 		case 'i':
    308   1.5     elric 			tp = params_ivmeth(string_fromcharstar(optarg));
    309   1.5     elric 			p = params_combine(p, tp);
    310   1.1     elric 			break;
    311   1.1     elric 		case 'k':
    312   1.5     elric 			kg = keygen_method(string_fromcharstar(optarg));
    313   1.5     elric 			if (!kg)
    314   1.1     elric 				usage();
    315   1.5     elric 			keygen_addlist(&p->keygen, kg);
    316   1.1     elric 			break;
    317  1.34  christos 		case 'l':
    318  1.34  christos 			set_action(&action, ACTION_LIST);
    319  1.34  christos 			break;
    320   1.1     elric 		case 'n':
    321   1.1     elric 			nflag = 1;
    322   1.1     elric 			break;
    323   1.1     elric 		case 'o':
    324  1.17    cbiere 			if (outfile)
    325  1.17    cbiere 				usage();
    326  1.17    cbiere 			outfile = estrdup(optarg);
    327   1.1     elric 			break;
    328  1.22     elric 		case 'p':
    329  1.22     elric 			pflag = PFLAG_STDIN;
    330  1.22     elric 			break;
    331   1.1     elric 		case 's':
    332  1.17    cbiere 			set_action(&action, ACTION_CONFIGSTDIN);
    333   1.1     elric 			break;
    334  1.54  riastrad 		case 't':
    335  1.54  riastrad 			set_action(&action, ACTION_PRINTKEY);
    336  1.54  riastrad 			break;
    337   1.1     elric 		case 'u':
    338  1.17    cbiere 			set_action(&action, ACTION_UNCONFIGURE);
    339   1.1     elric 			break;
    340   1.1     elric 		case 'v':
    341   1.1     elric 			verbose++;
    342   1.1     elric 			break;
    343   1.1     elric 		default:
    344   1.1     elric 			usage();
    345   1.1     elric 			/* NOTREACHED */
    346   1.1     elric 		}
    347   1.1     elric 
    348   1.1     elric 	argc -= optind;
    349   1.1     elric 	argv += optind;
    350   1.1     elric 
    351  1.17    cbiere 	if (!outfile)
    352  1.17    cbiere 		outfile = "";
    353  1.17    cbiere 	if (!cfile)
    354  1.17    cbiere 		cfile = "";
    355  1.17    cbiere 
    356  1.32     pooka 	if (prog_init && prog_init() == -1)
    357  1.32     pooka 		err(1, "init failed");
    358  1.32     pooka 
    359   1.1     elric 	/* validate the consistency of the arguments */
    360  1.58  riastrad 	if (Pfile != NULL &&
    361  1.58  riastrad 	    action != ACTION_GENERATE &&
    362  1.58  riastrad 	    action != ACTION_GENERATE_CONVERT) {
    363  1.58  riastrad 		warnx("-P is only for use with -g/-G action");
    364  1.57  riastrad 		usage();
    365  1.57  riastrad 	}
    366  1.57  riastrad 	if (Pfile != NULL && !Sflag) {
    367  1.57  riastrad 		warnx("-P only makes sense with -S flag");
    368  1.57  riastrad 	}
    369  1.58  riastrad 	if (Sflag &&
    370  1.58  riastrad 	    action != ACTION_GENERATE &&
    371  1.58  riastrad 	    action != ACTION_GENERATE_CONVERT) {
    372  1.58  riastrad 		warnx("-S is only for use with -g/-G action");
    373  1.57  riastrad 		usage();
    374  1.57  riastrad 	}
    375   1.1     elric 
    376   1.1     elric 	switch (action) {
    377  1.17    cbiere 	case ACTION_DEFAULT:	/* ACTION_CONFIGURE is the default */
    378   1.1     elric 	case ACTION_CONFIGURE:
    379   1.5     elric 		return configure(argc, argv, p, CONFIG_FLAGS_FROMMAIN);
    380   1.1     elric 	case ACTION_UNCONFIGURE:
    381   1.3     elric 		return unconfigure(argc, argv, NULL, CONFIG_FLAGS_FROMMAIN);
    382   1.1     elric 	case ACTION_GENERATE:
    383  1.57  riastrad 		return generate(p, argc, argv, outfile, Pfile);
    384   1.5     elric 	case ACTION_GENERATE_CONVERT:
    385  1.58  riastrad 		return generate_convert(p, argc, argv, outfile, Pfile);
    386   1.1     elric 	case ACTION_CONFIGALL:
    387   1.1     elric 		return do_all(cfile, argc, argv, configure);
    388   1.1     elric 	case ACTION_UNCONFIGALL:
    389   1.1     elric 		return do_all(cfile, argc, argv, unconfigure);
    390   1.1     elric 	case ACTION_CONFIGSTDIN:
    391   1.5     elric 		return configure_stdin(p, argc, argv);
    392  1.34  christos 	case ACTION_LIST:
    393  1.34  christos 		return do_list(argc, argv);
    394  1.54  riastrad 	case ACTION_PRINTKEY:
    395  1.54  riastrad 		return do_printkey(argc, argv);
    396  1.55  riastrad 	case ACTION_PRINTALLKEYS:
    397  1.55  riastrad 		return do_all(cfile, argc, argv, printkey1);
    398  1.19  christos 	default:
    399  1.19  christos 		errx(EXIT_FAILURE, "undefined action");
    400  1.19  christos 		/* NOTREACHED */
    401   1.1     elric 	}
    402   1.1     elric }
    403   1.1     elric 
    404   1.5     elric static bits_t *
    405  1.56  riastrad getsubkey_hkdf_hmac_sha256(bits_t *key, bits_t *info, size_t subkeylen)
    406  1.56  riastrad {
    407  1.56  riastrad 	bits_t		*ret = NULL;
    408  1.56  riastrad 	uint8_t		*tmp;
    409  1.56  riastrad 
    410  1.56  riastrad 	tmp = emalloc(BITS2BYTES(subkeylen));
    411  1.56  riastrad 	if (hkdf_hmac_sha256(tmp, BITS2BYTES(subkeylen),
    412  1.56  riastrad 		bits_getbuf(key), BITS2BYTES(bits_len(key)),
    413  1.56  riastrad 		bits_getbuf(info), BITS2BYTES(bits_len(info)))) {
    414  1.56  riastrad 		warnx("failed to derive HKDF-HMAC-SHA256 subkey");
    415  1.56  riastrad 		goto out;
    416  1.56  riastrad 	}
    417  1.56  riastrad 
    418  1.56  riastrad 	ret = bits_new(tmp, subkeylen);
    419  1.56  riastrad 
    420  1.56  riastrad out:	free(tmp);
    421  1.56  riastrad 	return ret;
    422  1.56  riastrad }
    423  1.56  riastrad 
    424  1.56  riastrad static bits_t *
    425  1.56  riastrad getsubkey(int alg, bits_t *key, bits_t *info, size_t subkeylen)
    426  1.56  riastrad {
    427  1.56  riastrad 
    428  1.56  riastrad 	switch (alg) {
    429  1.56  riastrad 	case SHARED_ALG_HKDF_HMAC_SHA256:
    430  1.56  riastrad 		return getsubkey_hkdf_hmac_sha256(key, info, subkeylen);
    431  1.56  riastrad 	default:
    432  1.56  riastrad 		warnx("unrecognised shared key derivation method %d", alg);
    433  1.56  riastrad 		return NULL;
    434  1.56  riastrad 	}
    435  1.56  riastrad }
    436  1.56  riastrad 
    437  1.56  riastrad static bits_t *
    438  1.59  riastrad getkey(const char *dev, struct keygen *kg, size_t len0,
    439  1.59  riastrad     struct sharedkeyhits *skh)
    440   1.1     elric {
    441   1.5     elric 	bits_t	*ret = NULL;
    442   1.5     elric 	bits_t	*tmp;
    443   1.1     elric 
    444  1.56  riastrad 	VPRINTF(3, ("getkey(\"%s\", %p, %zu) called\n", dev, kg, len0));
    445   1.5     elric 	for (; kg; kg=kg->next) {
    446  1.56  riastrad 		struct sharedkey *sk = NULL;
    447  1.56  riastrad 		size_t len = len0;
    448  1.56  riastrad 
    449  1.56  riastrad 		/*
    450  1.56  riastrad 		 * If shared, determine the shared key's length and
    451  1.56  riastrad 		 * probe the cache of shared keys.
    452  1.56  riastrad 		 */
    453  1.56  riastrad 		if (kg->kg_sharedid) {
    454  1.56  riastrad 			const char *id = string_tocharstar(kg->kg_sharedid);
    455  1.56  riastrad 
    456  1.56  riastrad 			len = kg->kg_sharedlen;
    457  1.56  riastrad 			LIST_FOREACH(sk, &sharedkeys, list) {
    458  1.56  riastrad 				if (kg->kg_sharedalg == sk->alg &&
    459  1.56  riastrad 				    kg->kg_sharedlen == bits_len(sk->key) &&
    460  1.56  riastrad 				    strcmp(id, string_tocharstar(sk->id)) == 0)
    461  1.56  riastrad 					break;
    462  1.56  riastrad 			}
    463  1.56  riastrad 			if (sk) {
    464  1.56  riastrad 				tmp = sk->key;
    465  1.56  riastrad 				goto derive;
    466  1.56  riastrad 			}
    467  1.56  riastrad 		}
    468  1.56  riastrad 
    469   1.5     elric 		switch (kg->kg_method) {
    470   1.5     elric 		case KEYGEN_STOREDKEY:
    471   1.5     elric 			tmp = getkey_storedkey(dev, kg, len);
    472   1.5     elric 			break;
    473   1.5     elric 		case KEYGEN_RANDOMKEY:
    474  1.12        tv 			tmp = getkey_randomkey(dev, kg, len, 1);
    475  1.12        tv 			break;
    476  1.12        tv 		case KEYGEN_URANDOMKEY:
    477  1.12        tv 			tmp = getkey_randomkey(dev, kg, len, 0);
    478   1.5     elric 			break;
    479  1.53       nia #ifdef HAVE_ARGON2
    480  1.53       nia 		case KEYGEN_ARGON2ID:
    481  1.53       nia 			tmp = getkey_argon2id(dev, kg, len);
    482  1.53       nia 			break;
    483  1.53       nia #endif
    484  1.10       dan 		case KEYGEN_PKCS5_PBKDF2_SHA1:
    485  1.10       dan 			tmp = getkey_pkcs5_pbkdf2(dev, kg, len, 0);
    486  1.10       dan 			break;
    487  1.10       dan 		/* provide backwards compatibility for old config files */
    488  1.10       dan 		case KEYGEN_PKCS5_PBKDF2_OLD:
    489  1.10       dan 			tmp = getkey_pkcs5_pbkdf2(dev, kg, len, 1);
    490   1.5     elric 			break;
    491  1.23     elric 		case KEYGEN_SHELL_CMD:
    492  1.23     elric 			tmp = getkey_shell_cmd(dev, kg, len);
    493  1.23     elric 			break;
    494   1.5     elric 		default:
    495   1.5     elric 			warnx("unrecognised keygen method %d in getkey()",
    496   1.5     elric 			    kg->kg_method);
    497   1.5     elric 			if (ret)
    498   1.5     elric 				bits_free(ret);
    499   1.5     elric 			return NULL;
    500   1.5     elric 		}
    501   1.5     elric 
    502  1.56  riastrad 		/*
    503  1.56  riastrad 		 * If shared, cache the key.
    504  1.56  riastrad 		 */
    505  1.56  riastrad 		if (kg->kg_sharedid) {
    506  1.56  riastrad 			assert(sk == NULL);
    507  1.56  riastrad 			sk = ecalloc(1, sizeof(*sk));
    508  1.56  riastrad 			sk->alg = kg->kg_sharedalg;
    509  1.56  riastrad 			sk->id = string_dup(kg->kg_sharedid);
    510  1.56  riastrad 			sk->key = tmp;
    511  1.56  riastrad 			LIST_INSERT_HEAD(&sharedkeys, sk, list);
    512  1.59  riastrad 			sk->verified = 0;
    513  1.56  riastrad 		}
    514  1.56  riastrad 
    515  1.56  riastrad derive:		if (kg->kg_sharedid) {
    516  1.59  riastrad 			assert(sk != NULL);
    517  1.56  riastrad 			/*
    518  1.56  riastrad 			 * tmp holds the master key, owned by the
    519  1.56  riastrad 			 * struct sharedkey record; replace it by the
    520  1.56  riastrad 			 * derived subkey.
    521  1.56  riastrad 			 */
    522  1.56  riastrad 			tmp = getsubkey(kg->kg_sharedalg, tmp,
    523  1.56  riastrad 			    kg->kg_sharedinfo, len0);
    524  1.56  riastrad 			if (tmp == NULL) {
    525  1.56  riastrad 				if (ret)
    526  1.56  riastrad 					bits_free(ret);
    527  1.56  riastrad 				return NULL;
    528  1.56  riastrad 			}
    529  1.59  riastrad 			if (skh)
    530  1.59  riastrad 				SLIST_INSERT_HEAD(skh, sk, used);
    531  1.56  riastrad 		}
    532   1.5     elric 		if (ret)
    533   1.5     elric 			ret = bits_xor_d(tmp, ret);
    534   1.5     elric 		else
    535   1.5     elric 			ret = tmp;
    536   1.1     elric 	}
    537   1.5     elric 
    538   1.5     elric 	return ret;
    539   1.5     elric }
    540   1.5     elric 
    541   1.5     elric /*ARGSUSED*/
    542   1.5     elric static bits_t *
    543  1.19  christos getkey_storedkey(const char *target, struct keygen *kg, size_t keylen)
    544   1.5     elric {
    545   1.5     elric 	return bits_dup(kg->kg_key);
    546   1.1     elric }
    547   1.1     elric 
    548   1.5     elric /*ARGSUSED*/
    549   1.5     elric static bits_t *
    550  1.19  christos getkey_randomkey(const char *target, struct keygen *kg, size_t keylen, int hard)
    551   1.1     elric {
    552  1.12        tv 	return bits_getrandombits(keylen, hard);
    553   1.1     elric }
    554   1.1     elric 
    555  1.22     elric static char *
    556  1.22     elric maybe_getpass(char *prompt)
    557  1.22     elric {
    558  1.22     elric 	char	 buf[1024];
    559  1.46     alnsn 	char	*p = NULL;
    560  1.46     alnsn 	char	*tmp, *pass;
    561  1.22     elric 
    562  1.22     elric 	switch (pflag) {
    563  1.22     elric 	case PFLAG_GETPASS:
    564  1.46     alnsn 		p = getpass_r(prompt, buf, sizeof(buf));
    565  1.46     alnsn 		break;
    566  1.46     alnsn 
    567  1.46     alnsn 	case PFLAG_GETPASS_ECHO:
    568  1.46     alnsn 		p = getpassfd(prompt, buf, sizeof(buf), NULL,
    569  1.46     alnsn 		    GETPASS_ECHO|GETPASS_ECHO_NL|GETPASS_NEED_TTY, 0);
    570  1.22     elric 		break;
    571  1.22     elric 
    572  1.22     elric 	case PFLAG_STDIN:
    573  1.22     elric 		p = fgets(buf, sizeof(buf), stdin);
    574  1.22     elric 		if (p) {
    575  1.22     elric 			tmp = strchr(p, '\n');
    576  1.22     elric 			if (tmp)
    577  1.22     elric 				*tmp = '\0';
    578  1.22     elric 		}
    579  1.22     elric 		break;
    580  1.22     elric 
    581  1.22     elric 	default:
    582  1.22     elric 		errx(EXIT_FAILURE, "pflag set inappropriately?");
    583  1.22     elric 	}
    584  1.22     elric 
    585  1.22     elric 	if (!p)
    586  1.22     elric 		err(EXIT_FAILURE, "failed to read passphrase");
    587  1.22     elric 
    588  1.46     alnsn 	pass = estrdup(p);
    589  1.48     alnsn 	explicit_memset(buf, 0, sizeof(buf));
    590  1.46     alnsn 
    591  1.46     alnsn 	return pass;
    592  1.22     elric }
    593  1.22     elric 
    594   1.5     elric /*ARGSUSED*/
    595  1.29     elric /*
    596  1.10       dan  * XXX take, and pass through, a compat flag that indicates whether we
    597  1.10       dan  * provide backwards compatibility with a previous bug.  The previous
    598  1.10       dan  * behaviour is indicated by the keygen method pkcs5_pbkdf2, and a
    599  1.10       dan  * non-zero compat flag. The new default, and correct keygen method is
    600  1.10       dan  * called pcks5_pbkdf2/sha1.  When the old method is removed, so will
    601  1.10       dan  * be the compat argument.
    602  1.10       dan  */
    603   1.5     elric static bits_t *
    604  1.19  christos getkey_pkcs5_pbkdf2(const char *target, struct keygen *kg, size_t keylen,
    605  1.19  christos     int compat)
    606   1.1     elric {
    607   1.5     elric 	bits_t		*ret;
    608  1.25  dholland 	char		*passp;
    609   1.5     elric 	char		 buf[1024];
    610   1.5     elric 	u_int8_t	*tmp;
    611   1.1     elric 
    612  1.46     alnsn 	snprintf(buf, sizeof(buf), "%s's passphrase%s:", target,
    613  1.46     alnsn 	    pflag & PFLAG_GETPASS_ECHO ? " (echo)" : "");
    614  1.25  dholland 	passp = maybe_getpass(buf);
    615  1.25  dholland 	if (pkcs5_pbkdf2(&tmp, BITS2BYTES(keylen), (uint8_t *)passp,
    616  1.25  dholland 	    strlen(passp),
    617   1.5     elric 	    bits_getbuf(kg->kg_salt), BITS2BYTES(bits_len(kg->kg_salt)),
    618  1.10       dan 	    kg->kg_iterations, compat)) {
    619   1.5     elric 		warnx("failed to generate PKCS#5 PBKDF2 key");
    620   1.5     elric 		return NULL;
    621   1.5     elric 	}
    622   1.5     elric 
    623   1.5     elric 	ret = bits_new(tmp, keylen);
    624   1.9        cb 	kg->kg_key = bits_dup(ret);
    625  1.48     alnsn 	explicit_memset(passp, 0, strlen(passp));
    626  1.22     elric 	free(passp);
    627   1.5     elric 	free(tmp);
    628   1.1     elric 	return ret;
    629   1.1     elric }
    630   1.1     elric 
    631  1.53       nia #ifdef HAVE_ARGON2
    632  1.53       nia static bits_t *
    633  1.53       nia getkey_argon2id(const char *target, struct keygen *kg, size_t keylen)
    634  1.53       nia {
    635  1.53       nia 	bits_t *ret;
    636  1.53       nia 	char *passp;
    637  1.53       nia 	char buf[1024];
    638  1.53       nia 	uint8_t	raw[256];
    639  1.53       nia 	int err;
    640  1.53       nia 
    641  1.53       nia 	snprintf(buf, sizeof(buf), "%s's passphrase%s:", target,
    642  1.53       nia 	    pflag & PFLAG_GETPASS_ECHO ? " (echo)" : "");
    643  1.53       nia 	passp = maybe_getpass(buf);
    644  1.53       nia 	if ((err = argon2_hash(kg->kg_iterations, kg->kg_memory,
    645  1.53       nia 	    kg->kg_parallelism,
    646  1.53       nia 	    passp, strlen(passp),
    647  1.53       nia 	    bits_getbuf(kg->kg_salt),
    648  1.53       nia 	    BITS2BYTES(bits_len(kg->kg_salt)),
    649  1.53       nia 	    raw, sizeof(raw),
    650  1.53       nia 	    NULL, 0,
    651  1.53       nia 	    Argon2_id, kg->kg_version)) != ARGON2_OK) {
    652  1.53       nia 		warnx("failed to generate Argon2id key, error code %d", err);
    653  1.53       nia 		return NULL;
    654  1.53       nia 	}
    655  1.53       nia 
    656  1.53       nia 	ret = bits_new(raw, keylen);
    657  1.53       nia 	kg->kg_key = bits_dup(ret);
    658  1.53       nia 	explicit_memset(passp, 0, strlen(passp));
    659  1.53       nia 	explicit_memset(raw, 0, sizeof(raw));
    660  1.53       nia 	free(passp);
    661  1.53       nia 	return ret;
    662  1.53       nia }
    663  1.53       nia #endif
    664  1.53       nia 
    665   1.5     elric /*ARGSUSED*/
    666  1.23     elric static bits_t *
    667  1.23     elric getkey_shell_cmd(const char *target, struct keygen *kg, size_t keylen)
    668  1.23     elric {
    669  1.23     elric 	FILE	*f;
    670  1.23     elric 	bits_t	*ret;
    671  1.52  riastrad 	int	status;
    672  1.23     elric 
    673  1.52  riastrad 	if ((f = popen(string_tocharstar(kg->kg_cmd), "r")) == NULL)
    674  1.52  riastrad 		errx(1, "command failed");
    675  1.52  riastrad 	if ((ret = bits_fget(f, keylen)) == NULL)
    676  1.52  riastrad 		errx(1, "command output too short");
    677  1.52  riastrad 	if ((status = pclose(f)) != 0)
    678  1.52  riastrad 		err(1, "command failed with status %d", status);
    679  1.23     elric 
    680  1.23     elric 	return ret;
    681  1.23     elric }
    682  1.23     elric 
    683  1.23     elric /*ARGSUSED*/
    684   1.1     elric static int
    685   1.3     elric unconfigure(int argc, char **argv, struct params *inparams, int flags)
    686   1.1     elric {
    687   1.1     elric 	int	fd;
    688   1.1     elric 	int	ret;
    689   1.1     elric 	char	buf[MAXPATHLEN] = "";
    690   1.1     elric 
    691   1.1     elric 	/* only complain about additional arguments, if called from main() */
    692   1.1     elric 	if (flags == CONFIG_FLAGS_FROMMAIN && argc != 1)
    693   1.1     elric 		usage();
    694   1.1     elric 
    695   1.1     elric 	/* if called from do_all(), then ensure that 2 or 3 args exist */
    696   1.1     elric 	if (flags == CONFIG_FLAGS_FROMALL && (argc < 2 || argc > 3))
    697   1.1     elric 		return -1;
    698   1.1     elric 
    699  1.32     pooka 	fd = opendisk1(*argv, O_RDWR, buf, sizeof(buf), 1, prog_open);
    700   1.1     elric 	if (fd == -1) {
    701  1.17    cbiere 		int saved_errno = errno;
    702  1.17    cbiere 
    703   1.5     elric 		warn("can't open cgd \"%s\", \"%s\"", *argv, buf);
    704   1.1     elric 
    705   1.1     elric 		/* this isn't fatal with nflag != 0 */
    706   1.1     elric 		if (!nflag)
    707  1.17    cbiere 			return saved_errno;
    708   1.1     elric 	}
    709   1.1     elric 
    710   1.1     elric 	VPRINTF(1, ("%s (%s): clearing\n", *argv, buf));
    711   1.1     elric 
    712   1.1     elric 	if (nflag)
    713   1.1     elric 		return 0;
    714   1.1     elric 
    715   1.3     elric 	ret = unconfigure_fd(fd);
    716  1.32     pooka 	(void)prog_close(fd);
    717   1.3     elric 	return ret;
    718   1.3     elric }
    719   1.3     elric 
    720   1.3     elric static int
    721   1.3     elric unconfigure_fd(int fd)
    722   1.3     elric {
    723   1.3     elric 	struct	cgd_ioctl ci;
    724   1.3     elric 
    725  1.32     pooka 	if (prog_ioctl(fd, CGDIOCCLR, &ci) == -1) {
    726  1.17    cbiere 		warn("ioctl");
    727   1.3     elric 		return -1;
    728   1.1     elric 	}
    729   1.1     elric 
    730   1.1     elric 	return 0;
    731   1.1     elric }
    732   1.1     elric 
    733   1.5     elric /*ARGSUSED*/
    734   1.1     elric static int
    735   1.3     elric configure(int argc, char **argv, struct params *inparams, int flags)
    736   1.1     elric {
    737   1.5     elric 	struct params	*p;
    738  1.22     elric 	struct keygen	*kg;
    739   1.5     elric 	int		 fd;
    740  1.22     elric 	int		 loop = 0;
    741   1.5     elric 	int		 ret;
    742   1.5     elric 	char		 cgdname[PATH_MAX];
    743  1.38  christos 	char		 devicename[PATH_MAX];
    744  1.39  christos 	const char	*dev = NULL;	/* XXX: gcc */
    745  1.36   mlelstv 
    746  1.44       kre 	if (argc < 2 || argc > 3) {
    747  1.44       kre 		/* print usage and exit, only if called from main() */
    748  1.44       kre 		if (flags == CONFIG_FLAGS_FROMMAIN) {
    749  1.44       kre 			warnx("wrong number of args");
    750  1.44       kre 			usage();
    751  1.44       kre 		}
    752  1.44       kre 		return -1;
    753  1.44       kre 	}
    754  1.44       kre 
    755  1.42       kre 	if ((
    756  1.42       kre 	  fd = opendisk1(*argv, O_RDWR, cgdname, sizeof(cgdname), 1, prog_open)
    757  1.42       kre 	    ) != -1) {
    758  1.42       kre 		struct cgd_user cgu;
    759  1.42       kre 
    760  1.42       kre 		cgu.cgu_unit = -1;
    761  1.42       kre 		if (prog_ioctl(fd, CGDIOCGET, &cgu) != -1 && cgu.cgu_dev != 0) {
    762  1.42       kre 			warnx("device %s already in use", *argv);
    763  1.43       kre 			prog_close(fd);
    764  1.42       kre 			return -1;
    765  1.42       kre 		}
    766  1.43       kre 		prog_close(fd);
    767  1.42       kre 	}
    768  1.42       kre 
    769  1.44       kre 	dev = getfsspecname(devicename, sizeof(devicename), argv[1]);
    770  1.44       kre 	if (dev == NULL) {
    771  1.44       kre 		warnx("getfsspecname failed: %s", devicename);
    772  1.44       kre 		return -1;
    773  1.36   mlelstv 	}
    774   1.1     elric 
    775  1.34  christos 	if (argc == 2) {
    776  1.40  christos 		char pfile[MAXPATHLEN];
    777  1.36   mlelstv 
    778  1.36   mlelstv 		/* make string writable for basename */
    779  1.40  christos 		strlcpy(pfile, dev, sizeof(pfile));
    780  1.40  christos 		p = params_cget(basename(pfile));
    781  1.44       kre 	} else
    782  1.17    cbiere 		p = params_cget(argv[2]);
    783   1.1     elric 
    784   1.5     elric 	if (!p)
    785   1.5     elric 		return -1;
    786   1.2     elric 
    787   1.3     elric 	/*
    788   1.5     elric 	 * over-ride with command line specifications and fill in default
    789   1.5     elric 	 * values.
    790   1.3     elric 	 */
    791   1.3     elric 
    792   1.5     elric 	p = params_combine(p, inparams);
    793   1.5     elric 	ret = params_filldefaults(p);
    794   1.5     elric 	if (ret) {
    795   1.5     elric 		params_free(p);
    796   1.5     elric 		return ret;
    797   1.5     elric 	}
    798   1.5     elric 
    799   1.5     elric 	if (!params_verify(p)) {
    800   1.5     elric 		warnx("params invalid");
    801   1.5     elric 		return -1;
    802   1.5     elric 	}
    803   1.3     elric 
    804   1.3     elric 	/*
    805   1.3     elric 	 * loop over configuring the disk and checking to see if it
    806   1.3     elric 	 * verifies properly.  We open and close the disk device each
    807   1.3     elric 	 * time, because if the user passes us the block device we
    808   1.3     elric 	 * need to flush the buffer cache.
    809  1.22     elric 	 *
    810  1.22     elric 	 * We only loop if one of the verification methods prompts for
    811  1.22     elric 	 * a password.
    812   1.3     elric 	 */
    813   1.3     elric 
    814  1.47     alnsn 	for (kg = p->keygen;
    815  1.47     alnsn 	    (pflag & PFLAG_GETPASS_MASK) && kg;
    816  1.47     alnsn 	    kg = kg->next)
    817  1.53       nia 		if (kg->kg_method == KEYGEN_ARGON2ID ||
    818  1.53       nia 		    kg->kg_method == KEYGEN_PKCS5_PBKDF2_SHA1 ||
    819  1.53       nia 		    kg->kg_method == KEYGEN_PKCS5_PBKDF2_OLD) {
    820  1.22     elric 			loop = 1;
    821  1.22     elric 			break;
    822  1.22     elric 		}
    823  1.22     elric 
    824   1.3     elric 	for (;;) {
    825  1.59  riastrad 		struct sharedkeyhits skh;
    826  1.59  riastrad 		struct sharedkey *sk, *sk1;
    827  1.59  riastrad 		int all_verified;
    828  1.59  riastrad 
    829  1.59  riastrad 		SLIST_INIT(&skh);
    830  1.59  riastrad 
    831   1.3     elric 		fd = opendisk_werror(argv[0], cgdname, sizeof(cgdname));
    832   1.3     elric 		if (fd == -1)
    833   1.3     elric 			return -1;
    834   1.3     elric 
    835   1.5     elric 		if (p->key)
    836   1.5     elric 			bits_free(p->key);
    837   1.5     elric 
    838  1.59  riastrad 		p->key = getkey(argv[1], p->keygen, p->keylen, &skh);
    839   1.5     elric 		if (!p->key)
    840   1.3     elric 			goto bail_err;
    841   1.3     elric 
    842  1.36   mlelstv 		ret = configure_params(fd, cgdname, dev, p);
    843   1.3     elric 		if (ret)
    844   1.3     elric 			goto bail_err;
    845   1.3     elric 
    846   1.5     elric 		ret = verify(p, fd);
    847  1.49       kre 		if (ret == -1) {
    848  1.49       kre 			(void)unconfigure_fd(fd);
    849   1.3     elric 			goto bail_err;
    850  1.49       kre 		}
    851  1.59  riastrad 		if (ret == 0) {		/* success */
    852  1.59  riastrad 			SLIST_FOREACH(sk, &skh, used)
    853  1.59  riastrad 				sk->verified = 1;
    854   1.3     elric 			break;
    855  1.59  riastrad 		}
    856   1.2     elric 
    857  1.19  christos 		(void)unconfigure_fd(fd);
    858  1.32     pooka 		(void)prog_close(fd);
    859  1.22     elric 
    860  1.59  riastrad 		/*
    861  1.60  riastrad 		 * For shared keys: If the shared keys were all
    862  1.60  riastrad 		 * verified already, assume something is wrong with the
    863  1.60  riastrad 		 * disk and give up.  If not, flush the cache of the
    864  1.60  riastrad 		 * ones that have not been verified in case we can try
    865  1.60  riastrad 		 * again with passphrase re-entry.
    866  1.59  riastrad 		 */
    867  1.60  riastrad 		if (!SLIST_EMPTY(&skh)) {
    868  1.60  riastrad 			all_verified = 1;
    869  1.60  riastrad 			SLIST_FOREACH_SAFE(sk, &skh, used, sk1) {
    870  1.60  riastrad 				all_verified &= sk->verified;
    871  1.60  riastrad 				if (!sk->verified) {
    872  1.60  riastrad 					LIST_REMOVE(sk, list);
    873  1.60  riastrad 					free(sk);
    874  1.60  riastrad 				}
    875  1.59  riastrad 			}
    876  1.60  riastrad 			if (all_verified)
    877  1.60  riastrad 				loop = 0;
    878  1.59  riastrad 		}
    879  1.59  riastrad 
    880  1.22     elric 		if (!loop) {
    881  1.22     elric 			warnx("verification failed permanently");
    882  1.22     elric 			goto bail_err;
    883  1.22     elric 		}
    884  1.22     elric 
    885  1.22     elric 		warnx("verification failed, please reenter passphrase");
    886   1.3     elric 	}
    887   1.2     elric 
    888   1.5     elric 	params_free(p);
    889  1.32     pooka 	(void)prog_close(fd);
    890   1.3     elric 	return 0;
    891  1.50       kre 
    892  1.50       kre  bail_err:;
    893   1.5     elric 	params_free(p);
    894  1.32     pooka 	(void)prog_close(fd);
    895   1.3     elric 	return -1;
    896   1.1     elric }
    897   1.1     elric 
    898   1.1     elric static int
    899   1.1     elric configure_stdin(struct params *p, int argc, char **argv)
    900   1.1     elric {
    901  1.36   mlelstv 	int		 fd;
    902  1.36   mlelstv 	int		 ret;
    903  1.36   mlelstv 	char		 cgdname[PATH_MAX];
    904  1.38  christos 	char		 devicename[PATH_MAX];
    905  1.36   mlelstv 	const char	*dev;
    906   1.1     elric 
    907   1.1     elric 	if (argc < 3 || argc > 4)
    908   1.1     elric 		usage();
    909   1.1     elric 
    910  1.38  christos 	dev = getfsspecname(devicename, sizeof(devicename), argv[1]);
    911  1.36   mlelstv 	if (dev == NULL) {
    912  1.38  christos 		warnx("getfsspecname failed: %s", devicename);
    913  1.36   mlelstv 		return -1;
    914  1.36   mlelstv 	}
    915  1.36   mlelstv 
    916   1.5     elric 	p->algorithm = string_fromcharstar(argv[2]);
    917  1.17    cbiere 	if (argc > 3) {
    918  1.19  christos 		size_t keylen;
    919  1.17    cbiere 
    920  1.19  christos 		if (parse_size_t(argv[3], &keylen) == -1) {
    921  1.17    cbiere 			warn("failed to parse key length");
    922  1.17    cbiere 			return -1;
    923  1.17    cbiere 		}
    924  1.17    cbiere 		p->keylen = keylen;
    925  1.17    cbiere 	}
    926   1.1     elric 
    927   1.1     elric 	ret = params_filldefaults(p);
    928   1.1     elric 	if (ret)
    929   1.1     elric 		return ret;
    930   1.1     elric 
    931   1.2     elric 	fd = opendisk_werror(argv[0], cgdname, sizeof(cgdname));
    932   1.2     elric 	if (fd == -1)
    933   1.2     elric 		return -1;
    934   1.2     elric 
    935   1.5     elric 	p->key = bits_fget(stdin, p->keylen);
    936   1.5     elric 	if (!p->key) {
    937   1.5     elric 		warnx("failed to read key from stdin");
    938   1.1     elric 		return -1;
    939   1.5     elric 	}
    940   1.1     elric 
    941  1.36   mlelstv 	return configure_params(fd, cgdname, dev, p);
    942   1.1     elric }
    943   1.1     elric 
    944   1.1     elric static int
    945  1.17    cbiere opendisk_werror(const char *cgd, char *buf, size_t buflen)
    946   1.2     elric {
    947   1.2     elric 	int	fd;
    948   1.2     elric 
    949  1.50       kre 	VPRINTF(3, ("opendisk_werror(%s, %s, %zu) called.\n", cgd,buf,buflen));
    950   1.5     elric 
    951   1.2     elric 	/* sanity */
    952   1.2     elric 	if (!cgd || !buf)
    953   1.2     elric 		return -1;
    954   1.2     elric 
    955   1.2     elric 	if (nflag) {
    956  1.17    cbiere 		if (strlcpy(buf, cgd, buflen) >= buflen)
    957  1.17    cbiere 			return -1;
    958   1.2     elric 		return 0;
    959   1.2     elric 	}
    960   1.2     elric 
    961  1.32     pooka 	fd = opendisk1(cgd, O_RDWR, buf, buflen, 0, prog_open);
    962   1.2     elric 	if (fd == -1)
    963   1.5     elric 		warnx("can't open cgd \"%s\", \"%s\"", cgd, buf);
    964   1.5     elric 
    965   1.2     elric 	return fd;
    966   1.2     elric }
    967   1.2     elric 
    968   1.2     elric static int
    969   1.2     elric configure_params(int fd, const char *cgd, const char *dev, struct params *p)
    970   1.1     elric {
    971   1.1     elric 	struct cgd_ioctl ci;
    972   1.1     elric 
    973   1.1     elric 	/* sanity */
    974   1.1     elric 	if (!cgd || !dev)
    975   1.1     elric 		return -1;
    976   1.1     elric 
    977  1.19  christos 	(void)memset(&ci, 0x0, sizeof(ci));
    978  1.16  christos 	ci.ci_disk = dev;
    979  1.16  christos 	ci.ci_alg = string_tocharstar(p->algorithm);
    980  1.16  christos 	ci.ci_ivmethod = string_tocharstar(p->ivmeth);
    981  1.16  christos 	ci.ci_key = bits_getbuf(p->key);
    982   1.1     elric 	ci.ci_keylen = p->keylen;
    983   1.1     elric 	ci.ci_blocksize = p->bsize;
    984   1.1     elric 
    985  1.20    martin 	VPRINTF(1, ("    with alg %s keylen %zu blocksize %zu ivmethod %s\n",
    986   1.5     elric 	    string_tocharstar(p->algorithm), p->keylen, p->bsize,
    987   1.5     elric 	    string_tocharstar(p->ivmeth)));
    988   1.5     elric 	VPRINTF(2, ("key: "));
    989   1.5     elric 	VERBOSE(2, bits_fprint(stdout, p->key));
    990   1.5     elric 	VPRINTF(2, ("\n"));
    991   1.1     elric 
    992   1.1     elric 	if (nflag)
    993   1.1     elric 		return 0;
    994   1.1     elric 
    995  1.32     pooka 	if (prog_ioctl(fd, CGDIOCSET, &ci) == -1) {
    996  1.17    cbiere 		int saved_errno = errno;
    997  1.17    cbiere 		warn("ioctl");
    998  1.17    cbiere 		return saved_errno;
    999   1.1     elric 	}
   1000   1.1     elric 
   1001   1.1     elric 	return 0;
   1002   1.1     elric }
   1003   1.1     elric 
   1004   1.3     elric /*
   1005   1.3     elric  * verify returns 0 for success, -1 for unrecoverable error, or 1 for retry.
   1006   1.3     elric  */
   1007   1.3     elric 
   1008   1.3     elric #define SCANSIZE	8192
   1009   1.3     elric 
   1010   1.3     elric static int
   1011   1.3     elric verify(struct params *p, int fd)
   1012   1.3     elric {
   1013   1.3     elric 
   1014   1.3     elric 	switch (p->verify_method) {
   1015   1.3     elric 	case VERIFY_NONE:
   1016   1.3     elric 		return 0;
   1017   1.3     elric 	case VERIFY_DISKLABEL:
   1018   1.5     elric 		return verify_disklabel(fd);
   1019   1.5     elric 	case VERIFY_FFS:
   1020   1.5     elric 		return verify_ffs(fd);
   1021   1.9        cb 	case VERIFY_REENTER:
   1022   1.9        cb 		return verify_reenter(p);
   1023  1.37   mlelstv 	case VERIFY_MBR:
   1024  1.37   mlelstv 		return verify_mbr(fd);
   1025  1.37   mlelstv 	case VERIFY_GPT:
   1026  1.37   mlelstv 		return verify_gpt(fd);
   1027   1.3     elric 	default:
   1028   1.5     elric 		warnx("unimplemented verification method");
   1029   1.3     elric 		return -1;
   1030   1.3     elric 	}
   1031   1.5     elric }
   1032   1.5     elric 
   1033   1.5     elric static int
   1034   1.5     elric verify_disklabel(int fd)
   1035   1.5     elric {
   1036   1.5     elric 	struct	disklabel l;
   1037  1.17    cbiere 	ssize_t	ret;
   1038   1.5     elric 	char	buf[SCANSIZE];
   1039   1.3     elric 
   1040   1.3     elric 	/*
   1041   1.3     elric 	 * we simply scan the first few blocks for a disklabel, ignoring
   1042   1.3     elric 	 * any MBR/filecore sorts of logic.  MSDOS and RiscOS can't read
   1043   1.3     elric 	 * a cgd, anyway, so it is unlikely that there will be non-native
   1044   1.3     elric 	 * partition information.
   1045   1.3     elric 	 */
   1046   1.3     elric 
   1047  1.37   mlelstv 	ret = prog_pread(fd, buf, SCANSIZE, 0);
   1048  1.17    cbiere 	if (ret < 0) {
   1049   1.5     elric 		warn("can't read disklabel area");
   1050   1.3     elric 		return -1;
   1051   1.3     elric 	}
   1052   1.3     elric 
   1053   1.3     elric 	/* now scan for the disklabel */
   1054   1.3     elric 
   1055  1.17    cbiere 	return disklabel_scan(&l, buf, (size_t)ret);
   1056   1.3     elric }
   1057   1.3     elric 
   1058  1.37   mlelstv static int
   1059  1.37   mlelstv verify_mbr(int fd)
   1060  1.37   mlelstv {
   1061  1.37   mlelstv 	struct mbr_sector mbr;
   1062  1.37   mlelstv 	ssize_t	ret;
   1063  1.37   mlelstv 	char	buf[SCANSIZE];
   1064  1.37   mlelstv 
   1065  1.37   mlelstv 	/*
   1066  1.37   mlelstv 	 * we read the first blocks to avoid sector size issues and
   1067  1.37   mlelstv 	 * verify the MBR in the beginning
   1068  1.37   mlelstv 	 */
   1069  1.37   mlelstv 
   1070  1.37   mlelstv 	ret = prog_pread(fd, buf, SCANSIZE, 0);
   1071  1.37   mlelstv 	if (ret < 0) {
   1072  1.37   mlelstv 		warn("can't read mbr area");
   1073  1.37   mlelstv 		return -1;
   1074  1.37   mlelstv 	}
   1075  1.37   mlelstv 
   1076  1.37   mlelstv 	memcpy(&mbr, buf, sizeof(mbr));
   1077  1.37   mlelstv 	if (le16toh(mbr.mbr_magic) != MBR_MAGIC)
   1078  1.49       kre 		return 1;
   1079  1.37   mlelstv 
   1080  1.37   mlelstv 	return 0;
   1081  1.37   mlelstv }
   1082  1.37   mlelstv 
   1083  1.37   mlelstv static uint32_t crc32_tab[] = {
   1084  1.37   mlelstv 	0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
   1085  1.37   mlelstv 	0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
   1086  1.37   mlelstv 	0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
   1087  1.37   mlelstv 	0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
   1088  1.37   mlelstv 	0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
   1089  1.37   mlelstv 	0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
   1090  1.37   mlelstv 	0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
   1091  1.37   mlelstv 	0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
   1092  1.37   mlelstv 	0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
   1093  1.37   mlelstv 	0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
   1094  1.37   mlelstv 	0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
   1095  1.37   mlelstv 	0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
   1096  1.37   mlelstv 	0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
   1097  1.37   mlelstv 	0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
   1098  1.37   mlelstv 	0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
   1099  1.37   mlelstv 	0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
   1100  1.37   mlelstv 	0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
   1101  1.37   mlelstv 	0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
   1102  1.37   mlelstv 	0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
   1103  1.37   mlelstv 	0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
   1104  1.37   mlelstv 	0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
   1105  1.37   mlelstv 	0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
   1106  1.37   mlelstv 	0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
   1107  1.37   mlelstv 	0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
   1108  1.37   mlelstv 	0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
   1109  1.37   mlelstv 	0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
   1110  1.37   mlelstv 	0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
   1111  1.37   mlelstv 	0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
   1112  1.37   mlelstv 	0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
   1113  1.37   mlelstv 	0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
   1114  1.37   mlelstv 	0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
   1115  1.37   mlelstv 	0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
   1116  1.37   mlelstv 	0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
   1117  1.37   mlelstv 	0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
   1118  1.37   mlelstv 	0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
   1119  1.37   mlelstv 	0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
   1120  1.37   mlelstv 	0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
   1121  1.37   mlelstv 	0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
   1122  1.37   mlelstv 	0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
   1123  1.37   mlelstv 	0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
   1124  1.37   mlelstv 	0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
   1125  1.37   mlelstv 	0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
   1126  1.37   mlelstv 	0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
   1127  1.37   mlelstv };
   1128  1.37   mlelstv 
   1129  1.37   mlelstv static uint32_t
   1130  1.37   mlelstv crc32(const void *buf, size_t size)
   1131  1.37   mlelstv {
   1132  1.37   mlelstv 	const uint8_t *p;
   1133  1.37   mlelstv 	uint32_t crc;
   1134  1.37   mlelstv 
   1135  1.37   mlelstv 	p = buf;
   1136  1.37   mlelstv 	crc = ~0U;
   1137  1.37   mlelstv 
   1138  1.37   mlelstv 	while (size--)
   1139  1.37   mlelstv 		crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
   1140  1.37   mlelstv 
   1141  1.37   mlelstv 	return crc ^ ~0U;
   1142  1.37   mlelstv }
   1143  1.37   mlelstv 
   1144  1.37   mlelstv static int
   1145  1.37   mlelstv verify_gpt(int fd)
   1146  1.37   mlelstv {
   1147  1.37   mlelstv 	struct	 gpt_hdr hdr;
   1148  1.37   mlelstv 	ssize_t	 ret;
   1149  1.37   mlelstv 	char	 buf[SCANSIZE];
   1150  1.37   mlelstv 	unsigned blksize;
   1151  1.37   mlelstv 	size_t	 off;
   1152  1.37   mlelstv 
   1153  1.37   mlelstv 	/*
   1154  1.37   mlelstv 	 * we read the first blocks to avoid sector size issues and
   1155  1.37   mlelstv 	 * verify the GPT header.
   1156  1.37   mlelstv 	 */
   1157  1.37   mlelstv 
   1158  1.37   mlelstv 	ret = prog_pread(fd, buf, SCANSIZE, 0);
   1159  1.37   mlelstv 	if (ret < 0) {
   1160  1.37   mlelstv 		warn("can't read gpt area");
   1161  1.37   mlelstv 		return -1;
   1162  1.37   mlelstv 	}
   1163  1.37   mlelstv 
   1164  1.49       kre 	ret = 1;
   1165  1.50       kre 	for (blksize = DEV_BSIZE;
   1166  1.50       kre              (off = (blksize * GPT_HDR_BLKNO)) <= SCANSIZE - sizeof(hdr);
   1167  1.37   mlelstv              blksize <<= 1) {
   1168  1.37   mlelstv 
   1169  1.37   mlelstv 		memcpy(&hdr, &buf[off], sizeof(hdr));
   1170  1.50       kre 		if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) == 0
   1171  1.50       kre 		    && le32toh(hdr.hdr_revision) == GPT_HDR_REVISION
   1172  1.50       kre 		    && le32toh(hdr.hdr_size) == GPT_HDR_SIZE) {
   1173  1.37   mlelstv 
   1174  1.37   mlelstv 			hdr.hdr_crc_self = 0;
   1175  1.37   mlelstv 			if (crc32(&hdr, sizeof(hdr))) {
   1176  1.37   mlelstv 				ret = 0;
   1177  1.37   mlelstv 				break;
   1178  1.37   mlelstv 			}
   1179  1.37   mlelstv 		}
   1180  1.37   mlelstv 	}
   1181  1.37   mlelstv 
   1182  1.37   mlelstv 	return ret;
   1183  1.37   mlelstv }
   1184  1.37   mlelstv 
   1185   1.7      fvdl static off_t sblock_try[] = SBLOCKSEARCH;
   1186   1.7      fvdl 
   1187   1.1     elric static int
   1188   1.5     elric verify_ffs(int fd)
   1189   1.5     elric {
   1190  1.19  christos 	size_t	i;
   1191   1.5     elric 
   1192   1.7      fvdl 	for (i = 0; sblock_try[i] != -1; i++) {
   1193  1.19  christos 		union {
   1194  1.19  christos 		    char	buf[SBLOCKSIZE];
   1195  1.19  christos 		    struct	fs fs;
   1196  1.19  christos 		} u;
   1197  1.17    cbiere 		ssize_t ret;
   1198  1.17    cbiere 
   1199  1.32     pooka 		ret = prog_pread(fd, &u, sizeof(u), sblock_try[i]);
   1200  1.17    cbiere 		if (ret < 0) {
   1201   1.7      fvdl 			warn("pread");
   1202  1.17    cbiere 			break;
   1203  1.19  christos 		} else if ((size_t)ret < sizeof(u)) {
   1204  1.17    cbiere 			warnx("pread: incomplete block");
   1205  1.17    cbiere 			break;
   1206   1.7      fvdl 		}
   1207  1.19  christos 		switch (u.fs.fs_magic) {
   1208   1.7      fvdl 		case FS_UFS1_MAGIC:
   1209   1.7      fvdl 		case FS_UFS2_MAGIC:
   1210   1.7      fvdl 		case FS_UFS1_MAGIC_SWAPPED:
   1211   1.7      fvdl 		case FS_UFS2_MAGIC_SWAPPED:
   1212   1.7      fvdl 			return 0;
   1213   1.7      fvdl 		default:
   1214   1.7      fvdl 			continue;
   1215   1.7      fvdl 		}
   1216   1.5     elric 	}
   1217  1.17    cbiere 
   1218  1.17    cbiere 	return 1;	/* failure */
   1219   1.9        cb }
   1220   1.9        cb 
   1221   1.9        cb static int
   1222   1.9        cb verify_reenter(struct params *p)
   1223   1.9        cb {
   1224   1.9        cb 	struct keygen *kg;
   1225  1.53       nia 	bits_t *orig_key, *key = NULL;
   1226   1.9        cb 	int ret;
   1227   1.9        cb 
   1228   1.9        cb 	ret = 0;
   1229   1.9        cb 	for (kg = p->keygen; kg && !ret; kg = kg->next) {
   1230  1.53       nia 		if (kg->kg_method != KEYGEN_ARGON2ID &&
   1231  1.53       nia 		    kg->kg_method != KEYGEN_PKCS5_PBKDF2_SHA1 &&
   1232  1.53       nia 		    kg->kg_method != KEYGEN_PKCS5_PBKDF2_OLD)
   1233   1.9        cb 			continue;
   1234   1.9        cb 
   1235   1.9        cb 		orig_key = kg->kg_key;
   1236   1.9        cb 		kg->kg_key = NULL;
   1237   1.9        cb 
   1238  1.53       nia 		switch (kg->kg_method) {
   1239  1.53       nia #ifdef HAVE_ARGON2
   1240  1.53       nia 		case KEYGEN_ARGON2ID:
   1241  1.53       nia 			key = getkey_argon2id("re-enter device", kg,
   1242  1.53       nia 				bits_len(orig_key));
   1243  1.53       nia 			break;
   1244  1.53       nia #endif
   1245  1.53       nia 		case KEYGEN_PKCS5_PBKDF2_SHA1:
   1246  1.53       nia 			key = getkey_pkcs5_pbkdf2("re-enter device", kg,
   1247  1.53       nia 				bits_len(orig_key), 0);
   1248  1.53       nia 			break;
   1249  1.53       nia 		case KEYGEN_PKCS5_PBKDF2_OLD:
   1250  1.53       nia 			key = getkey_pkcs5_pbkdf2("re-enter device", kg,
   1251  1.53       nia 				bits_len(orig_key), 1);
   1252  1.53       nia 			break;
   1253  1.53       nia 		default:
   1254  1.53       nia 			warnx("unsupported keygen method");
   1255  1.53       nia 			kg->kg_key = orig_key;
   1256  1.53       nia 			return -1;
   1257  1.53       nia 		}
   1258  1.50       kre 
   1259   1.9        cb 		ret = !bits_match(key, orig_key);
   1260   1.9        cb 
   1261   1.9        cb 		bits_free(key);
   1262   1.9        cb 		bits_free(kg->kg_key);
   1263   1.9        cb 		kg->kg_key = orig_key;
   1264   1.9        cb 	}
   1265   1.9        cb 
   1266   1.9        cb 	return ret;
   1267   1.5     elric }
   1268   1.5     elric 
   1269   1.5     elric static int
   1270  1.57  riastrad generate(struct params *p, int argc, char **argv, const char *outfile,
   1271  1.57  riastrad     const char *Pfile)
   1272   1.1     elric {
   1273   1.1     elric 	int	 ret;
   1274   1.1     elric 
   1275   1.1     elric 	if (argc < 1 || argc > 2)
   1276   1.1     elric 		usage();
   1277   1.1     elric 
   1278   1.5     elric 	p->algorithm = string_fromcharstar(argv[0]);
   1279  1.17    cbiere 	if (argc > 1) {
   1280  1.19  christos 		size_t keylen;
   1281  1.17    cbiere 
   1282  1.19  christos 		if (parse_size_t(argv[1], &keylen) == -1) {
   1283  1.17    cbiere 			warn("Failed to parse key length");
   1284  1.17    cbiere 			return -1;
   1285  1.17    cbiere 		}
   1286  1.17    cbiere 		p->keylen = keylen;
   1287  1.17    cbiere 	}
   1288   1.5     elric 
   1289   1.5     elric 	ret = params_filldefaults(p);
   1290   1.1     elric 	if (ret)
   1291   1.1     elric 		return ret;
   1292   1.5     elric 
   1293  1.57  riastrad 	if (Pfile) {
   1294  1.57  riastrad 		struct params *pp;
   1295  1.57  riastrad 
   1296  1.57  riastrad 		pp = params_cget(Pfile);
   1297  1.57  riastrad 		if (pp == NULL)
   1298  1.57  riastrad 			return -1;
   1299  1.57  riastrad 		if (!params_verify(pp)) {
   1300  1.57  riastrad 			params_free(pp);
   1301  1.57  riastrad 			warnx("invalid parameters file \"%s\"", Pfile);
   1302  1.57  riastrad 			return -1;
   1303  1.57  riastrad 		}
   1304  1.57  riastrad 		p = params_combine(pp, p);
   1305  1.57  riastrad 		keygen_stripstored(&p->keygen);
   1306  1.57  riastrad 		if (!p->keygen) {
   1307  1.57  riastrad 			warnx("no keygen in parameters file \"%s\"", Pfile);
   1308  1.57  riastrad 			return -1;
   1309  1.57  riastrad 		}
   1310  1.57  riastrad 	} else {
   1311  1.57  riastrad 		if (!p->keygen) {
   1312  1.57  riastrad 			p->keygen = keygen_generate(KEYGEN_PKCS5_PBKDF2_SHA1);
   1313  1.57  riastrad 			if (!p->keygen)
   1314  1.57  riastrad 				return -1;
   1315  1.57  riastrad 		}
   1316  1.57  riastrad 
   1317  1.57  riastrad 		if (keygen_filldefaults(p->keygen, p->keylen)) {
   1318  1.57  riastrad 			warnx("Failed to generate defaults for keygen");
   1319   1.5     elric 			return -1;
   1320  1.57  riastrad 		}
   1321   1.5     elric 	}
   1322   1.5     elric 
   1323  1.57  riastrad 	if (Sflag) {
   1324  1.57  riastrad 		if (Pfile)
   1325  1.57  riastrad 			ret = keygen_tweakshared(p->keygen);
   1326  1.57  riastrad 		else
   1327  1.57  riastrad 			ret = keygen_makeshared(p->keygen);
   1328  1.57  riastrad 		if (ret)
   1329  1.57  riastrad 			return ret;
   1330   1.5     elric 	}
   1331   1.5     elric 
   1332   1.5     elric 	if (!params_verify(p)) {
   1333   1.5     elric 		warnx("invalid parameters generated");
   1334   1.5     elric 		return -1;
   1335   1.5     elric 	}
   1336   1.5     elric 
   1337   1.5     elric 	return params_cput(p, outfile);
   1338   1.5     elric }
   1339   1.5     elric 
   1340   1.5     elric static int
   1341  1.58  riastrad generate_convert(struct params *p, int argc, char **argv, const char *outfile,
   1342  1.58  riastrad     const char *Pfile)
   1343   1.5     elric {
   1344   1.5     elric 	struct params	*oldp;
   1345   1.5     elric 	struct keygen	*kg;
   1346  1.58  riastrad 	int		 ret;
   1347   1.5     elric 
   1348   1.5     elric 	if (argc != 1)
   1349   1.5     elric 		usage();
   1350   1.5     elric 
   1351   1.5     elric 	oldp = params_cget(*argv);
   1352   1.5     elric 	if (!oldp)
   1353   1.5     elric 		return -1;
   1354   1.5     elric 
   1355   1.5     elric 	/* for sanity, we ensure that none of the keygens are randomkey */
   1356   1.5     elric 	for (kg=p->keygen; kg; kg=kg->next)
   1357  1.30     elric 		if ((kg->kg_method == KEYGEN_RANDOMKEY) ||
   1358  1.30     elric 		    (kg->kg_method == KEYGEN_URANDOMKEY)) {
   1359  1.30     elric 			warnx("can't preserve randomly generated key");
   1360   1.5     elric 			goto bail;
   1361  1.30     elric 		}
   1362   1.5     elric 	for (kg=oldp->keygen; kg; kg=kg->next)
   1363  1.30     elric 		if ((kg->kg_method == KEYGEN_RANDOMKEY) ||
   1364  1.30     elric 		    (kg->kg_method == KEYGEN_URANDOMKEY)) {
   1365  1.30     elric 			warnx("can't preserve randomly generated key");
   1366   1.5     elric 			goto bail;
   1367  1.30     elric 		}
   1368   1.5     elric 
   1369   1.5     elric 	if (!params_verify(oldp)) {
   1370   1.5     elric 		warnx("invalid old parameters file \"%s\"", *argv);
   1371   1.5     elric 		return -1;
   1372   1.1     elric 	}
   1373   1.1     elric 
   1374  1.59  riastrad 	oldp->key = getkey("old file", oldp->keygen, oldp->keylen, NULL);
   1375   1.5     elric 
   1376   1.5     elric 	/* we copy across the non-keygen info, here. */
   1377   1.5     elric 
   1378   1.5     elric 	string_free(p->algorithm);
   1379   1.5     elric 	string_free(p->ivmeth);
   1380   1.5     elric 
   1381   1.5     elric 	p->algorithm = string_dup(oldp->algorithm);
   1382   1.5     elric 	p->ivmeth = string_dup(oldp->ivmeth);
   1383   1.5     elric 	p->keylen = oldp->keylen;
   1384   1.5     elric 	p->bsize = oldp->bsize;
   1385   1.5     elric 	if (p->verify_method == VERIFY_UNKNOWN)
   1386   1.5     elric 		p->verify_method = oldp->verify_method;
   1387   1.5     elric 
   1388   1.5     elric 	params_free(oldp);
   1389   1.1     elric 
   1390  1.58  riastrad 	if (Pfile) {
   1391  1.58  riastrad 		struct params *pp;
   1392  1.58  riastrad 
   1393  1.58  riastrad 		pp = params_cget(Pfile);
   1394  1.58  riastrad 		if (pp == NULL)
   1395  1.58  riastrad 			return -1;
   1396  1.58  riastrad 		if (!params_verify(pp)) {
   1397  1.58  riastrad 			params_free(pp);
   1398  1.58  riastrad 			warnx("invalid parameters file \"%s\"", Pfile);
   1399  1.58  riastrad 			return -1;
   1400  1.58  riastrad 		}
   1401  1.58  riastrad 		p = params_combine(pp, p);
   1402  1.58  riastrad 		keygen_stripstored(&p->keygen);
   1403  1.58  riastrad 		if (!p->keygen) {
   1404  1.58  riastrad 			warnx("no keygen in parameters file \"%s\"", Pfile);
   1405   1.1     elric 			return -1;
   1406  1.58  riastrad 		}
   1407  1.58  riastrad 	} else {
   1408  1.58  riastrad 		if (!p->keygen) {
   1409  1.58  riastrad 			p->keygen = keygen_generate(KEYGEN_PKCS5_PBKDF2_SHA1);
   1410  1.58  riastrad 			if (!p->keygen)
   1411  1.58  riastrad 				return -1;
   1412  1.58  riastrad 		}
   1413  1.58  riastrad 		(void)params_filldefaults(p);
   1414  1.58  riastrad 		(void)keygen_filldefaults(p->keygen, p->keylen);
   1415   1.5     elric 	}
   1416  1.58  riastrad 
   1417  1.58  riastrad 	if (Sflag) {
   1418  1.58  riastrad 		if (Pfile)
   1419  1.58  riastrad 			ret = keygen_tweakshared(p->keygen);
   1420  1.58  riastrad 		else
   1421  1.58  riastrad 			ret = keygen_makeshared(p->keygen);
   1422  1.58  riastrad 		if (ret)
   1423  1.58  riastrad 			return ret;
   1424  1.58  riastrad 	}
   1425  1.58  riastrad 
   1426  1.59  riastrad 	p->key = getkey("new file", p->keygen, p->keylen, NULL);
   1427   1.5     elric 
   1428   1.5     elric 	kg = keygen_generate(KEYGEN_STOREDKEY);
   1429   1.5     elric 	kg->kg_key = bits_xor(p->key, oldp->key);
   1430   1.5     elric 	keygen_addlist(&p->keygen, kg);
   1431   1.5     elric 
   1432   1.5     elric 	if (!params_verify(p)) {
   1433   1.5     elric 		warnx("can't generate new parameters file");
   1434   1.5     elric 		return -1;
   1435   1.1     elric 	}
   1436   1.1     elric 
   1437   1.5     elric 	return params_cput(p, outfile);
   1438  1.50       kre  bail:;
   1439   1.5     elric 	params_free(oldp);
   1440   1.5     elric 	return -1;
   1441   1.1     elric }
   1442   1.1     elric 
   1443   1.1     elric static int
   1444  1.19  christos /*ARGSUSED*/
   1445   1.1     elric do_all(const char *cfile, int argc, char **argv,
   1446   1.3     elric        int (*conf)(int, char **, struct params *, int))
   1447   1.1     elric {
   1448   1.1     elric 	FILE		 *f;
   1449   1.1     elric 	size_t		  len;
   1450   1.1     elric 	size_t		  lineno;
   1451   1.1     elric 	int		  my_argc;
   1452   1.1     elric 	int		  ret;
   1453   1.1     elric 	const char	 *fn;
   1454   1.1     elric 	char		 *line;
   1455   1.1     elric 	char		**my_argv;
   1456   1.1     elric 
   1457   1.1     elric 	if (argc > 0)
   1458   1.1     elric 		usage();
   1459   1.1     elric 
   1460   1.1     elric 	if (!cfile[0])
   1461   1.1     elric 		fn = CGDCONFIG_CFILE;
   1462   1.1     elric 	else
   1463   1.1     elric 		fn = cfile;
   1464   1.1     elric 
   1465   1.1     elric 	f = fopen(fn, "r");
   1466  1.50       kre 	if (f == NULL) {
   1467   1.5     elric 		warn("could not open config file \"%s\"", fn);
   1468   1.1     elric 		return -1;
   1469   1.1     elric 	}
   1470   1.1     elric 
   1471   1.1     elric 	ret = 0;
   1472   1.1     elric 	lineno = 0;
   1473   1.1     elric 	for (;;) {
   1474   1.1     elric 		line = fparseln(f, &len, &lineno, "\\\\#", FPARSELN_UNESCALL);
   1475   1.1     elric 		if (!line)
   1476   1.1     elric 			break;
   1477   1.1     elric 		if (!*line)
   1478   1.1     elric 			continue;
   1479   1.1     elric 
   1480   1.1     elric 		my_argv = words(line, &my_argc);
   1481   1.3     elric 		ret = conf(my_argc, my_argv, NULL, CONFIG_FLAGS_FROMALL);
   1482   1.1     elric 		if (ret) {
   1483   1.5     elric 			warnx("action failed on \"%s\" line %lu", fn,
   1484   1.1     elric 			    (u_long)lineno);
   1485   1.1     elric 			break;
   1486   1.1     elric 		}
   1487   1.1     elric 		words_free(my_argv, my_argc);
   1488   1.1     elric 	}
   1489   1.1     elric 	return ret;
   1490   1.1     elric }
   1491  1.13     elric 
   1492  1.34  christos static const char *
   1493  1.34  christos iv_method(int mode)
   1494  1.34  christos {
   1495  1.34  christos 
   1496  1.34  christos 	switch (mode) {
   1497  1.34  christos 	case CGD_CIPHER_CBC_ENCBLKNO8:
   1498  1.34  christos 		return "encblkno8";
   1499  1.34  christos 	case CGD_CIPHER_CBC_ENCBLKNO1:
   1500  1.34  christos 		return "encblkno1";
   1501  1.34  christos 	default:
   1502  1.34  christos 		return "unknown";
   1503  1.34  christos 	}
   1504  1.34  christos }
   1505  1.34  christos 
   1506  1.35  christos 
   1507  1.35  christos static void
   1508  1.35  christos show(const char *dev) {
   1509  1.35  christos 	char path[64];
   1510  1.35  christos 	struct cgd_user cgu;
   1511  1.35  christos 	int fd;
   1512  1.35  christos 
   1513  1.35  christos 	fd = opendisk(dev, O_RDONLY, path, sizeof(path), 0);
   1514  1.35  christos 	if (fd == -1) {
   1515  1.35  christos 		warn("open: %s", dev);
   1516  1.35  christos 		return;
   1517  1.35  christos 	}
   1518  1.35  christos 
   1519  1.35  christos 	cgu.cgu_unit = -1;
   1520  1.35  christos 	if (prog_ioctl(fd, CGDIOCGET, &cgu) == -1) {
   1521  1.35  christos 		close(fd);
   1522  1.35  christos 		err(1, "CGDIOCGET");
   1523  1.35  christos 	}
   1524  1.35  christos 
   1525  1.35  christos 	printf("%s: ", dev);
   1526  1.35  christos 
   1527  1.35  christos 	if (cgu.cgu_dev == 0) {
   1528  1.35  christos 		printf("not in use");
   1529  1.35  christos 		goto out;
   1530  1.35  christos 	}
   1531  1.35  christos 
   1532  1.35  christos 	dev = devname(cgu.cgu_dev, S_IFBLK);
   1533  1.35  christos 	if (dev != NULL)
   1534  1.35  christos 		printf("%s ", dev);
   1535  1.35  christos 	else
   1536  1.35  christos 		printf("dev %llu,%llu ", (unsigned long long)major(cgu.cgu_dev),
   1537  1.35  christos 		    (unsigned long long)minor(cgu.cgu_dev));
   1538  1.35  christos 
   1539  1.35  christos 	if (verbose)
   1540  1.35  christos 		printf("%s ", cgu.cgu_alg);
   1541  1.35  christos 	if (verbose > 1) {
   1542  1.35  christos 		printf("keylen %d ", cgu.cgu_keylen);
   1543  1.35  christos 		printf("blksize %zd ", cgu.cgu_blocksize);
   1544  1.35  christos 		printf("%s ", iv_method(cgu.cgu_mode));
   1545  1.35  christos 	}
   1546  1.35  christos 
   1547  1.50       kre  out:;
   1548  1.35  christos 	putchar('\n');
   1549  1.35  christos 	close(fd);
   1550  1.35  christos }
   1551  1.35  christos 
   1552  1.34  christos static int
   1553  1.34  christos do_list(int argc, char **argv)
   1554  1.34  christos {
   1555  1.34  christos 
   1556  1.34  christos 	if (argc != 0 && argc != 1)
   1557  1.34  christos 		usage();
   1558  1.34  christos 
   1559  1.35  christos 	if (argc) {
   1560  1.35  christos 		show(argv[0]);
   1561  1.35  christos 		return 0;
   1562  1.35  christos 	}
   1563  1.34  christos 
   1564  1.35  christos 	DIR *dirp;
   1565  1.35  christos 	struct dirent *dp;
   1566  1.35  christos 	__BITMAP_TYPE(, uint32_t, 65536) bm;
   1567  1.35  christos 
   1568  1.35  christos 	__BITMAP_ZERO(&bm);
   1569  1.35  christos 
   1570  1.35  christos 	if ((dirp = opendir(_PATH_DEV)) == NULL)
   1571  1.35  christos 		err(1, "opendir: %s", _PATH_DEV);
   1572  1.35  christos 
   1573  1.35  christos 	while ((dp = readdir(dirp)) != NULL) {
   1574  1.35  christos 		char *ep;
   1575  1.35  christos 		if (strncmp(dp->d_name, "rcgd", 4) != 0)
   1576  1.35  christos 			continue;
   1577  1.35  christos 		errno = 0;
   1578  1.35  christos 		int n = (int)strtol(dp->d_name + 4, &ep, 0);
   1579  1.35  christos 		if (ep == dp->d_name + 4 || errno != 0) {
   1580  1.35  christos 			warnx("bad name %s", dp->d_name);
   1581  1.35  christos 			continue;
   1582  1.34  christos 		}
   1583  1.35  christos 		*ep = '\0';
   1584  1.35  christos 		if (__BITMAP_ISSET(n, &bm))
   1585  1.35  christos 			continue;
   1586  1.35  christos 		__BITMAP_SET(n, &bm);
   1587  1.35  christos 		show(dp->d_name + 1);
   1588  1.34  christos 	}
   1589  1.34  christos 
   1590  1.35  christos 	closedir(dirp);
   1591  1.34  christos 	return 0;
   1592  1.34  christos }
   1593  1.34  christos 
   1594  1.54  riastrad static int
   1595  1.55  riastrad printkey(const char *dev, const char *paramsfile, const char *fmt, ...)
   1596  1.54  riastrad {
   1597  1.55  riastrad 	va_list va;
   1598  1.54  riastrad 	struct params *p;
   1599  1.54  riastrad 	const uint8_t *raw;
   1600  1.54  riastrad 	size_t nbits, nbytes;
   1601  1.54  riastrad 	size_t nb64;
   1602  1.54  riastrad 	char *b64;
   1603  1.54  riastrad 	int ret;
   1604  1.54  riastrad 
   1605  1.55  riastrad 	p = params_cget(paramsfile);
   1606  1.54  riastrad 	if (p == NULL)
   1607  1.54  riastrad 		return -1;
   1608  1.54  riastrad 	if (!params_verify(p)) {
   1609  1.55  riastrad 		warnx("invalid parameters file \"%s\"", paramsfile);
   1610  1.54  riastrad 		return -1;
   1611  1.54  riastrad 	}
   1612  1.59  riastrad 	p->key = getkey(dev, p->keygen, p->keylen, NULL);
   1613  1.54  riastrad 	raw = bits_getbuf(p->key);
   1614  1.54  riastrad 	nbits = bits_len(p->key);
   1615  1.54  riastrad 	assert(nbits <= INT_MAX - 7);
   1616  1.54  riastrad 	nbytes = BITS2BYTES(nbits);
   1617  1.54  riastrad 	assert(nbytes <= 3*(INT_MAX/4) - 2);
   1618  1.54  riastrad 
   1619  1.54  riastrad 	nb64 = 4*((nbytes + 2)/3);
   1620  1.54  riastrad 	b64 = emalloc(nb64 + 2);
   1621  1.54  riastrad 	ret = __b64_ntop(raw, nbytes, b64, nb64 + 1);
   1622  1.54  riastrad 	assert(ret == (int)nb64);
   1623  1.54  riastrad 	b64[nb64] = '\n';
   1624  1.54  riastrad 	b64[nb64 + 1] = '\0';
   1625  1.54  riastrad 
   1626  1.55  riastrad 	va_start(va, fmt);
   1627  1.55  riastrad 	vprintf(fmt, va);
   1628  1.55  riastrad 	va_end(va);
   1629  1.54  riastrad 	if (fwrite(b64, nb64 + 1, 1, stdout) != 1)
   1630  1.54  riastrad 		err(1, "fwrite");
   1631  1.54  riastrad 	fflush(stdout);
   1632  1.54  riastrad 	return ferror(stdout);
   1633  1.54  riastrad }
   1634  1.54  riastrad 
   1635  1.55  riastrad static int
   1636  1.55  riastrad printkey1(int argc, char **argv, struct params *inparams, int flags)
   1637  1.55  riastrad {
   1638  1.55  riastrad 	char devicename[PATH_MAX], paramsfilebuf[PATH_MAX];
   1639  1.55  riastrad 	const char *dev, *paramsfile;
   1640  1.55  riastrad 
   1641  1.55  riastrad 	assert(flags == CONFIG_FLAGS_FROMALL);
   1642  1.55  riastrad 
   1643  1.55  riastrad 	if (argc < 2 || argc > 3)
   1644  1.55  riastrad 		return -1;
   1645  1.55  riastrad 
   1646  1.55  riastrad 	dev = getfsspecname(devicename, sizeof(devicename), argv[1]);
   1647  1.55  riastrad 	if (dev == NULL) {
   1648  1.55  riastrad 		warnx("getfsspecname failed: %s", devicename);
   1649  1.55  riastrad 		return -1;
   1650  1.55  riastrad 	}
   1651  1.55  riastrad 
   1652  1.55  riastrad 	if (argc == 2) {
   1653  1.55  riastrad 		strlcpy(paramsfilebuf, dev, sizeof(paramsfilebuf));
   1654  1.55  riastrad 		paramsfile = basename(paramsfilebuf);
   1655  1.55  riastrad 	} else {
   1656  1.55  riastrad 		paramsfile = argv[2];
   1657  1.55  riastrad 	}
   1658  1.55  riastrad 
   1659  1.55  riastrad 	return printkey(dev, paramsfile, "%s: ", dev);
   1660  1.55  riastrad }
   1661  1.55  riastrad 
   1662  1.55  riastrad static int
   1663  1.55  riastrad do_printkey(int argc, char **argv)
   1664  1.55  riastrad {
   1665  1.55  riastrad 
   1666  1.55  riastrad 	if (argc != 1)
   1667  1.55  riastrad 		usage();
   1668  1.55  riastrad 	return printkey("key", argv[0], "");
   1669  1.55  riastrad }
   1670  1.55  riastrad 
   1671  1.13     elric static void
   1672  1.13     elric eliminate_cores(void)
   1673  1.13     elric {
   1674  1.13     elric 	struct rlimit	rlp;
   1675  1.13     elric 
   1676  1.13     elric 	rlp.rlim_cur = 0;
   1677  1.13     elric 	rlp.rlim_max = 0;
   1678  1.17    cbiere 	if (setrlimit(RLIMIT_CORE, &rlp) == -1)
   1679  1.13     elric 		err(EXIT_FAILURE, "Can't disable cores");
   1680  1.13     elric }
   1681