xref: /onnv-gate/usr/src/cmd/lofiadm/main.c (revision 9127:39de79f2e5d5)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53180Svikram  * Common Development and Distribution License (the "License").
63180Svikram  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
228996SAlok.Aggarwal@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
233180Svikram  * Use is subject to license terms.
244451Seschrock  */
254451Seschrock 
264451Seschrock /*
270Sstevel@tonic-gate  * lofiadm - administer lofi(7d). Very simple, add and remove file<->device
280Sstevel@tonic-gate  * associations, and display status. All the ioctls are private between
290Sstevel@tonic-gate  * lofi and lofiadm, and so are very simple - device information is
300Sstevel@tonic-gate  * communicated via a minor number.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include <sys/lofi.h>
360Sstevel@tonic-gate #include <sys/stat.h>
379048Sjrgn.keil@googlemail.com #include <sys/sysmacros.h>
385643Saalok #include <netinet/in.h>
390Sstevel@tonic-gate #include <stdio.h>
400Sstevel@tonic-gate #include <fcntl.h>
410Sstevel@tonic-gate #include <locale.h>
420Sstevel@tonic-gate #include <string.h>
435643Saalok #include <strings.h>
440Sstevel@tonic-gate #include <errno.h>
450Sstevel@tonic-gate #include <stdlib.h>
460Sstevel@tonic-gate #include <unistd.h>
470Sstevel@tonic-gate #include <stropts.h>
483180Svikram #include <libdevinfo.h>
495643Saalok #include <libgen.h>
505643Saalok #include <ctype.h>
515719Saalok #include <dlfcn.h>
528313SDina.Nimeh@Sun.Com #include <limits.h>
538313SDina.Nimeh@Sun.Com #include <security/cryptoki.h>
548313SDina.Nimeh@Sun.Com #include <cryptoutil.h>
558313SDina.Nimeh@Sun.Com #include <sys/crypto/ioctl.h>
568313SDina.Nimeh@Sun.Com #include <sys/crypto/ioctladmin.h>
570Sstevel@tonic-gate #include "utils.h"
588996SAlok.Aggarwal@Sun.COM #include <LzmaEnc.h>
590Sstevel@tonic-gate 
608313SDina.Nimeh@Sun.Com /* Only need the IV len #defines out of these files, nothing else. */
618313SDina.Nimeh@Sun.Com #include <aes/aes_impl.h>
628313SDina.Nimeh@Sun.Com #include <des/des_impl.h>
638313SDina.Nimeh@Sun.Com #include <blowfish/blowfish_impl.h>
648313SDina.Nimeh@Sun.Com 
650Sstevel@tonic-gate static const char USAGE[] =
668313SDina.Nimeh@Sun.Com 	"Usage: %s -a file [ device ] "
678313SDina.Nimeh@Sun.Com 	" [-c aes-128-cbc|aes-192-cbc|aes-256-cbc|des3-cbc|blowfish-cbc]"
688313SDina.Nimeh@Sun.Com 	" [-e] [-k keyfile] [-T [token]:[manuf]:[serial]:key]\n"
698069SDina.Nimeh@Sun.Com 	"       %s -d file | device\n"
708996SAlok.Aggarwal@Sun.COM 	"       %s -C [gzip|gzip-6|gzip-9|lzma] [-s segment_size] file\n"
718069SDina.Nimeh@Sun.Com 	"       %s -U file\n"
728069SDina.Nimeh@Sun.Com 	"       %s [ file | device ]\n";
730Sstevel@tonic-gate 
748313SDina.Nimeh@Sun.Com typedef struct token_spec {
758313SDina.Nimeh@Sun.Com 	char	*name;
768313SDina.Nimeh@Sun.Com 	char	*mfr;
778313SDina.Nimeh@Sun.Com 	char	*serno;
788313SDina.Nimeh@Sun.Com 	char	*key;
798313SDina.Nimeh@Sun.Com } token_spec_t;
808313SDina.Nimeh@Sun.Com 
818313SDina.Nimeh@Sun.Com typedef struct mech_alias {
828313SDina.Nimeh@Sun.Com 	char	*alias;
838313SDina.Nimeh@Sun.Com 	CK_MECHANISM_TYPE type;
848313SDina.Nimeh@Sun.Com 	char	*name;		/* for ioctl */
858313SDina.Nimeh@Sun.Com 	char	*iv_name;	/* for ioctl */
868313SDina.Nimeh@Sun.Com 	size_t	iv_len;		/* for ioctl */
878313SDina.Nimeh@Sun.Com 	iv_method_t iv_type;	/* for ioctl */
888313SDina.Nimeh@Sun.Com 	size_t	min_keysize;	/* in bytes */
898313SDina.Nimeh@Sun.Com 	size_t	max_keysize;	/* in bytes */
908313SDina.Nimeh@Sun.Com 	token_spec_t *token;
918313SDina.Nimeh@Sun.Com 	CK_SLOT_ID slot;
928313SDina.Nimeh@Sun.Com } mech_alias_t;
938313SDina.Nimeh@Sun.Com 
948313SDina.Nimeh@Sun.Com static mech_alias_t mech_aliases[] = {
958313SDina.Nimeh@Sun.Com 	/* Preferred one should always be listed first. */
968313SDina.Nimeh@Sun.Com 	{ "aes-256-cbc", CKM_AES_CBC, "CKM_AES_CBC", "CKM_AES_ECB", AES_IV_LEN,
978313SDina.Nimeh@Sun.Com 	    IVM_ENC_BLKNO, ULONG_MAX, 0L, NULL, (CK_SLOT_ID) -1 },
988313SDina.Nimeh@Sun.Com 	{ "aes-192-cbc", CKM_AES_CBC, "CKM_AES_CBC", "CKM_AES_ECB", AES_IV_LEN,
998313SDina.Nimeh@Sun.Com 	    IVM_ENC_BLKNO, ULONG_MAX, 0L, NULL, (CK_SLOT_ID) -1 },
1008313SDina.Nimeh@Sun.Com 	{ "aes-128-cbc", CKM_AES_CBC, "CKM_AES_CBC", "CKM_AES_ECB", AES_IV_LEN,
1018313SDina.Nimeh@Sun.Com 	    IVM_ENC_BLKNO, ULONG_MAX, 0L, NULL, (CK_SLOT_ID) -1 },
1028313SDina.Nimeh@Sun.Com 	{ "des3-cbc", CKM_DES3_CBC, "CKM_DES3_CBC", "CKM_DES3_ECB", DES_IV_LEN,
1038313SDina.Nimeh@Sun.Com 	    IVM_ENC_BLKNO, ULONG_MAX, 0L, NULL, (CK_SLOT_ID)-1 },
1048313SDina.Nimeh@Sun.Com 	{ "blowfish-cbc", CKM_BLOWFISH_CBC, "CKM_BLOWFISH_CBC",
1058313SDina.Nimeh@Sun.Com 	    "CKM_BLOWFISH_ECB", BLOWFISH_IV_LEN, IVM_ENC_BLKNO, ULONG_MAX,
1068313SDina.Nimeh@Sun.Com 	    0L, NULL, (CK_SLOT_ID)-1 }
1078313SDina.Nimeh@Sun.Com 	/*
1088313SDina.Nimeh@Sun.Com 	 * A cipher without an iv requirement would look like this:
1098313SDina.Nimeh@Sun.Com 	 * { "aes-xex", CKM_AES_XEX, "CKM_AES_XEX", NULL, 0,
1108313SDina.Nimeh@Sun.Com 	 *    IVM_NONE, ULONG_MAX, 0L, NULL, (CK_SLOT_ID)-1 }
1118313SDina.Nimeh@Sun.Com 	 */
1128313SDina.Nimeh@Sun.Com };
1138313SDina.Nimeh@Sun.Com 
1148313SDina.Nimeh@Sun.Com int	mech_aliases_count = (sizeof (mech_aliases) / sizeof (mech_alias_t));
1158313SDina.Nimeh@Sun.Com 
1168313SDina.Nimeh@Sun.Com /* Preferred cipher, if one isn't specified on command line. */
1178313SDina.Nimeh@Sun.Com #define	DEFAULT_CIPHER	(&mech_aliases[0])
1188313SDina.Nimeh@Sun.Com 
1198313SDina.Nimeh@Sun.Com #define	DEFAULT_CIPHER_NUM	64	/* guess # kernel ciphers available */
1208313SDina.Nimeh@Sun.Com #define	DEFAULT_MECHINFO_NUM	16	/* guess # kernel mechs available */
1218313SDina.Nimeh@Sun.Com #define	MIN_PASSLEN		8	/* min acceptable passphrase size */
1220Sstevel@tonic-gate 
1235643Saalok static int gzip_compress(void *src, size_t srclen, void *dst,
1245643Saalok 	size_t *destlen, int level);
1258996SAlok.Aggarwal@Sun.COM static int lzma_compress(void *src, size_t srclen, void *dst,
1268996SAlok.Aggarwal@Sun.COM 	size_t *destlen, int level);
1275643Saalok 
1285643Saalok lofi_compress_info_t lofi_compress_table[LOFI_COMPRESS_FUNCTIONS] = {
1298996SAlok.Aggarwal@Sun.COM 	{NULL,  		gzip_compress,  6,	"gzip"}, /* default */
1308996SAlok.Aggarwal@Sun.COM 	{NULL,			gzip_compress,	6,	"gzip-6"},
1318996SAlok.Aggarwal@Sun.COM 	{NULL,			gzip_compress,	9, 	"gzip-9"},
1328996SAlok.Aggarwal@Sun.COM 	{NULL,  		lzma_compress, 	0, 	"lzma"}
1335643Saalok };
1345643Saalok 
1358313SDina.Nimeh@Sun.Com /* For displaying lofi mappings */
1365643Saalok #define	FORMAT 			"%-20s     %-30s	%s\n"
1378313SDina.Nimeh@Sun.Com 
1385643Saalok #define	COMPRESS_ALGORITHM	"gzip"
1395643Saalok #define	COMPRESS_THRESHOLD	2048
1405643Saalok #define	SEGSIZE			131072
1415643Saalok #define	BLOCK_SIZE		512
1425643Saalok #define	KILOBYTE		1024
1435643Saalok #define	MEGABYTE		(KILOBYTE * KILOBYTE)
1445643Saalok #define	GIGABYTE		(KILOBYTE * MEGABYTE)
1455719Saalok #define	LIBZ			"libz.so"
1465719Saalok 
1478996SAlok.Aggarwal@Sun.COM static void
usage(const char * pname)1488996SAlok.Aggarwal@Sun.COM usage(const char *pname)
1498996SAlok.Aggarwal@Sun.COM {
1508996SAlok.Aggarwal@Sun.COM 	(void) fprintf(stderr, gettext(USAGE), pname, pname, pname,
1518996SAlok.Aggarwal@Sun.COM 	    pname, pname);
1528996SAlok.Aggarwal@Sun.COM 	exit(E_USAGE);
1538996SAlok.Aggarwal@Sun.COM }
1545643Saalok 
1558996SAlok.Aggarwal@Sun.COM static int
gzip_compress(void * src,size_t srclen,void * dst,size_t * dstlen,int level)1568996SAlok.Aggarwal@Sun.COM gzip_compress(void *src, size_t srclen, void *dst, size_t *dstlen, int level)
1575643Saalok {
1588996SAlok.Aggarwal@Sun.COM 	static int (*compress2p)(void *, ulong_t *, void *, size_t, int) = NULL;
1595719Saalok 	void *libz_hdl = NULL;
1605719Saalok 
1615719Saalok 	/*
1625719Saalok 	 * The first time we are called, attempt to dlopen()
1635719Saalok 	 * libz.so and get a pointer to the compress2() function
1645719Saalok 	 */
1655719Saalok 	if (compress2p == NULL) {
1665719Saalok 		if ((libz_hdl = openlib(LIBZ)) == NULL)
1675719Saalok 			die(gettext("could not find %s. "
1685719Saalok 			    "gzip compression unavailable\n"), LIBZ);
1695719Saalok 
1705719Saalok 		if ((compress2p =
1715719Saalok 		    (int (*)(void *, ulong_t *, void *, size_t, int))
1725719Saalok 		    dlsym(libz_hdl, "compress2")) == NULL) {
1735719Saalok 			closelib();
1745719Saalok 			die(gettext("could not find the correct %s. "
1755719Saalok 			    "gzip compression unavailable\n"), LIBZ);
1765719Saalok 		}
1775719Saalok 	}
1785719Saalok 
1795719Saalok 	if ((*compress2p)(dst, (ulong_t *)dstlen, src, srclen, level) != 0)
1805643Saalok 		return (-1);
1815643Saalok 	return (0);
1825643Saalok }
1830Sstevel@tonic-gate 
1848996SAlok.Aggarwal@Sun.COM /*ARGSUSED*/
1850Sstevel@tonic-gate static void
SzAlloc(void * p,size_t size)1868996SAlok.Aggarwal@Sun.COM *SzAlloc(void *p, size_t size)
1878996SAlok.Aggarwal@Sun.COM {
1888996SAlok.Aggarwal@Sun.COM 	return (malloc(size));
1898996SAlok.Aggarwal@Sun.COM }
1908996SAlok.Aggarwal@Sun.COM 
1918996SAlok.Aggarwal@Sun.COM /*ARGSUSED*/
1928996SAlok.Aggarwal@Sun.COM static void
SzFree(void * p,void * address,size_t size)1938996SAlok.Aggarwal@Sun.COM SzFree(void *p, void *address, size_t size)
1948996SAlok.Aggarwal@Sun.COM {
1958996SAlok.Aggarwal@Sun.COM 	free(address);
1968996SAlok.Aggarwal@Sun.COM }
1978996SAlok.Aggarwal@Sun.COM 
1988996SAlok.Aggarwal@Sun.COM static ISzAlloc g_Alloc = {
1998996SAlok.Aggarwal@Sun.COM 	SzAlloc,
2008996SAlok.Aggarwal@Sun.COM 	SzFree
2018996SAlok.Aggarwal@Sun.COM };
2028996SAlok.Aggarwal@Sun.COM 
2038996SAlok.Aggarwal@Sun.COM #define	LZMA_UNCOMPRESSED_SIZE	8
2048996SAlok.Aggarwal@Sun.COM #define	LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + LZMA_UNCOMPRESSED_SIZE)
2058996SAlok.Aggarwal@Sun.COM 
2068996SAlok.Aggarwal@Sun.COM /*ARGSUSED*/
2078996SAlok.Aggarwal@Sun.COM static int
lzma_compress(void * src,size_t srclen,void * dst,size_t * dstlen,int level)2088996SAlok.Aggarwal@Sun.COM lzma_compress(void *src, size_t srclen, void *dst,
2098996SAlok.Aggarwal@Sun.COM 	size_t *dstlen, int level)
2100Sstevel@tonic-gate {
2118996SAlok.Aggarwal@Sun.COM 	CLzmaEncProps props;
2128996SAlok.Aggarwal@Sun.COM 	size_t outsize2;
2138996SAlok.Aggarwal@Sun.COM 	size_t outsizeprocessed;
2148996SAlok.Aggarwal@Sun.COM 	size_t outpropssize = LZMA_PROPS_SIZE;
2158996SAlok.Aggarwal@Sun.COM 	uint64_t t = 0;
2168996SAlok.Aggarwal@Sun.COM 	SRes res;
2178996SAlok.Aggarwal@Sun.COM 	Byte *dstp;
2188996SAlok.Aggarwal@Sun.COM 	int i;
2198996SAlok.Aggarwal@Sun.COM 
2208996SAlok.Aggarwal@Sun.COM 	outsize2 = *dstlen;
2218996SAlok.Aggarwal@Sun.COM 
2228996SAlok.Aggarwal@Sun.COM 	LzmaEncProps_Init(&props);
2238996SAlok.Aggarwal@Sun.COM 
2248996SAlok.Aggarwal@Sun.COM 	/*
2258996SAlok.Aggarwal@Sun.COM 	 * The LZMA compressed file format is as follows -
2268996SAlok.Aggarwal@Sun.COM 	 *
2278996SAlok.Aggarwal@Sun.COM 	 * Offset Size(bytes) Description
2288996SAlok.Aggarwal@Sun.COM 	 * 0		1	LZMA properties (lc, lp, lp (encoded))
2298996SAlok.Aggarwal@Sun.COM 	 * 1		4	Dictionary size (little endian)
2308996SAlok.Aggarwal@Sun.COM 	 * 5		8	Uncompressed size (little endian)
2318996SAlok.Aggarwal@Sun.COM 	 * 13			Compressed data
2328996SAlok.Aggarwal@Sun.COM 	 */
2338996SAlok.Aggarwal@Sun.COM 
2348996SAlok.Aggarwal@Sun.COM 	/* set the dictionary size to be 8MB */
2358996SAlok.Aggarwal@Sun.COM 	props.dictSize = 1 << 23;
2368996SAlok.Aggarwal@Sun.COM 
2378996SAlok.Aggarwal@Sun.COM 	if (*dstlen < LZMA_HEADER_SIZE)
2388996SAlok.Aggarwal@Sun.COM 		return (SZ_ERROR_OUTPUT_EOF);
2398996SAlok.Aggarwal@Sun.COM 
2408996SAlok.Aggarwal@Sun.COM 	dstp = (Byte *)dst;
2418996SAlok.Aggarwal@Sun.COM 	t = srclen;
2428996SAlok.Aggarwal@Sun.COM 	/*
2438996SAlok.Aggarwal@Sun.COM 	 * Set the uncompressed size in the LZMA header
2448996SAlok.Aggarwal@Sun.COM 	 * The LZMA properties (specified in 'props')
2458996SAlok.Aggarwal@Sun.COM 	 * will be set by the call to LzmaEncode()
2468996SAlok.Aggarwal@Sun.COM 	 */
2478996SAlok.Aggarwal@Sun.COM 	for (i = 0; i < LZMA_UNCOMPRESSED_SIZE; i++, t >>= 8) {
2488996SAlok.Aggarwal@Sun.COM 		dstp[LZMA_PROPS_SIZE + i] = (Byte)t;
2498996SAlok.Aggarwal@Sun.COM 	}
2508996SAlok.Aggarwal@Sun.COM 
2518996SAlok.Aggarwal@Sun.COM 	outsizeprocessed = outsize2 - LZMA_HEADER_SIZE;
2528996SAlok.Aggarwal@Sun.COM 	res = LzmaEncode(dstp + LZMA_HEADER_SIZE, &outsizeprocessed,
2538996SAlok.Aggarwal@Sun.COM 	    src, srclen, &props, dstp, &outpropssize, 0, NULL,
2548996SAlok.Aggarwal@Sun.COM 	    &g_Alloc, &g_Alloc);
2558996SAlok.Aggarwal@Sun.COM 
2568996SAlok.Aggarwal@Sun.COM 	if (res != 0)
2578996SAlok.Aggarwal@Sun.COM 		return (-1);
2588996SAlok.Aggarwal@Sun.COM 
2598996SAlok.Aggarwal@Sun.COM 	*dstlen = outsizeprocessed + LZMA_HEADER_SIZE;
2608996SAlok.Aggarwal@Sun.COM 	return (0);
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate /*
2640Sstevel@tonic-gate  * Translate a lofi device name to a minor number. We might be asked
2650Sstevel@tonic-gate  * to do this when there is no association (such as when the user specifies
2660Sstevel@tonic-gate  * a particular device), so we can only look at the string.
2670Sstevel@tonic-gate  */
2680Sstevel@tonic-gate static int
name_to_minor(const char * devicename)2690Sstevel@tonic-gate name_to_minor(const char *devicename)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate 	int	minor;
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	if (sscanf(devicename, "/dev/" LOFI_BLOCK_NAME "/%d", &minor) == 1) {
2740Sstevel@tonic-gate 		return (minor);
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate 	if (sscanf(devicename, "/dev/" LOFI_CHAR_NAME "/%d", &minor) == 1) {
2770Sstevel@tonic-gate 		return (minor);
2780Sstevel@tonic-gate 	}
2790Sstevel@tonic-gate 	return (0);
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate /*
2830Sstevel@tonic-gate  * This might be the first time we've used this minor number. If so,
2840Sstevel@tonic-gate  * it might also be that the /dev links are in the process of being created
2850Sstevel@tonic-gate  * by devfsadmd (or that they'll be created "soon"). We cannot return
2860Sstevel@tonic-gate  * until they're there or the invoker of lofiadm might try to use them
2870Sstevel@tonic-gate  * and not find them. This can happen if a shell script is running on
2880Sstevel@tonic-gate  * an MP.
2890Sstevel@tonic-gate  */
2900Sstevel@tonic-gate static int sleeptime = 2;	/* number of seconds to sleep between stat's */
2910Sstevel@tonic-gate static int maxsleep = 120;	/* maximum number of seconds to sleep */
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate static void
wait_until_dev_complete(int minor)2940Sstevel@tonic-gate wait_until_dev_complete(int minor)
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate 	struct stat64 buf;
2970Sstevel@tonic-gate 	int	cursleep;
2985643Saalok 	char	blkpath[MAXPATHLEN];
2995643Saalok 	char	charpath[MAXPATHLEN];
3003180Svikram 	di_devlink_handle_t hdl;
3013180Svikram 
3020Sstevel@tonic-gate 	(void) snprintf(blkpath, sizeof (blkpath), "/dev/%s/%d",
3030Sstevel@tonic-gate 	    LOFI_BLOCK_NAME, minor);
3040Sstevel@tonic-gate 	(void) snprintf(charpath, sizeof (charpath), "/dev/%s/%d",
3050Sstevel@tonic-gate 	    LOFI_CHAR_NAME, minor);
3063180Svikram 
3073180Svikram 	/* Check if links already present */
3083180Svikram 	if (stat64(blkpath, &buf) == 0 && stat64(charpath, &buf) == 0)
3093180Svikram 		return;
3103180Svikram 
3113180Svikram 	/* First use di_devlink_init() */
3123180Svikram 	if (hdl = di_devlink_init("lofi", DI_MAKE_LINK)) {
3133180Svikram 		(void) di_devlink_fini(&hdl);
3143180Svikram 		goto out;
3153180Svikram 	}
3163180Svikram 
3173180Svikram 	/*
3183180Svikram 	 * Under normal conditions, di_devlink_init(DI_MAKE_LINK) above will
3193180Svikram 	 * only fail if the caller is non-root. In that case, wait for
3203180Svikram 	 * link creation via sysevents.
3213180Svikram 	 */
3228313SDina.Nimeh@Sun.Com 	for (cursleep = 0; cursleep < maxsleep; cursleep += sleeptime) {
3238313SDina.Nimeh@Sun.Com 		if (stat64(blkpath, &buf) == 0 && stat64(charpath, &buf) == 0)
3248313SDina.Nimeh@Sun.Com 			return;
3258313SDina.Nimeh@Sun.Com 		(void) sleep(sleeptime);
3260Sstevel@tonic-gate 	}
3273180Svikram 
3280Sstevel@tonic-gate 	/* one last try */
3293180Svikram out:
3300Sstevel@tonic-gate 	if (stat64(blkpath, &buf) == -1) {
3310Sstevel@tonic-gate 		die(gettext("%s was not created"), blkpath);
3320Sstevel@tonic-gate 	}
3330Sstevel@tonic-gate 	if (stat64(charpath, &buf) == -1) {
3340Sstevel@tonic-gate 		die(gettext("%s was not created"), charpath);
3350Sstevel@tonic-gate 	}
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate /*
3398313SDina.Nimeh@Sun.Com  * Map the file and return the minor number the driver picked for the file
3408313SDina.Nimeh@Sun.Com  * DO NOT use this function if the filename is actually the device name.
3418313SDina.Nimeh@Sun.Com  */
3428313SDina.Nimeh@Sun.Com static int
lofi_map_file(int lfd,struct lofi_ioctl li,const char * filename)3438313SDina.Nimeh@Sun.Com lofi_map_file(int lfd, struct lofi_ioctl li, const char *filename)
3448313SDina.Nimeh@Sun.Com {
3458313SDina.Nimeh@Sun.Com 	int	minor;
3468313SDina.Nimeh@Sun.Com 
3478313SDina.Nimeh@Sun.Com 	li.li_minor = 0;
3488313SDina.Nimeh@Sun.Com 	(void) strlcpy(li.li_filename, filename, sizeof (li.li_filename));
3498313SDina.Nimeh@Sun.Com 	minor = ioctl(lfd, LOFI_MAP_FILE, &li);
3508313SDina.Nimeh@Sun.Com 	if (minor == -1) {
3518313SDina.Nimeh@Sun.Com 		if (errno == ENOTSUP)
3528313SDina.Nimeh@Sun.Com 			warn(gettext("encrypting compressed files is "
3538313SDina.Nimeh@Sun.Com 			    "unsupported"));
3548313SDina.Nimeh@Sun.Com 		die(gettext("could not map file %s"), filename);
3558313SDina.Nimeh@Sun.Com 	}
3568313SDina.Nimeh@Sun.Com 	wait_until_dev_complete(minor);
3578313SDina.Nimeh@Sun.Com 	return (minor);
3588313SDina.Nimeh@Sun.Com }
3598313SDina.Nimeh@Sun.Com 
3608313SDina.Nimeh@Sun.Com /*
3610Sstevel@tonic-gate  * Add a device association. If devicename is NULL, let the driver
3620Sstevel@tonic-gate  * pick a device.
3630Sstevel@tonic-gate  */
3640Sstevel@tonic-gate static void
add_mapping(int lfd,const char * devicename,const char * filename,mech_alias_t * cipher,const char * rkey,size_t rksz)3655643Saalok add_mapping(int lfd, const char *devicename, const char *filename,
3668313SDina.Nimeh@Sun.Com     mech_alias_t *cipher, const char *rkey, size_t rksz)
3670Sstevel@tonic-gate {
3680Sstevel@tonic-gate 	struct lofi_ioctl li;
3698313SDina.Nimeh@Sun.Com 
3708313SDina.Nimeh@Sun.Com 	li.li_crypto_enabled = B_FALSE;
3718313SDina.Nimeh@Sun.Com 	if (cipher != NULL) {
3728313SDina.Nimeh@Sun.Com 		/* set up encryption for mapped file */
3738313SDina.Nimeh@Sun.Com 		li.li_crypto_enabled = B_TRUE;
3748313SDina.Nimeh@Sun.Com 		(void) strlcpy(li.li_cipher, cipher->name,
3758313SDina.Nimeh@Sun.Com 		    sizeof (li.li_cipher));
3768313SDina.Nimeh@Sun.Com 		if (rksz > sizeof (li.li_key)) {
3778313SDina.Nimeh@Sun.Com 			die(gettext("key too large"));
3788313SDina.Nimeh@Sun.Com 		}
3798313SDina.Nimeh@Sun.Com 		bcopy(rkey, li.li_key, rksz);
3808313SDina.Nimeh@Sun.Com 		li.li_key_len = rksz << 3;	/* convert to bits */
3818313SDina.Nimeh@Sun.Com 
3828313SDina.Nimeh@Sun.Com 		li.li_iv_type = cipher->iv_type;
3838313SDina.Nimeh@Sun.Com 		li.li_iv_len = cipher->iv_len;	/* 0 when no iv needed */
3848313SDina.Nimeh@Sun.Com 		switch (cipher->iv_type) {
3858313SDina.Nimeh@Sun.Com 		case IVM_ENC_BLKNO:
3868313SDina.Nimeh@Sun.Com 			(void) strlcpy(li.li_iv_cipher, cipher->iv_name,
3878313SDina.Nimeh@Sun.Com 			    sizeof (li.li_iv_cipher));
3888313SDina.Nimeh@Sun.Com 			break;
3898313SDina.Nimeh@Sun.Com 		case IVM_NONE:
3908313SDina.Nimeh@Sun.Com 			/* FALLTHROUGH */
3918313SDina.Nimeh@Sun.Com 		default:
3928313SDina.Nimeh@Sun.Com 			break;
3938313SDina.Nimeh@Sun.Com 		}
3948313SDina.Nimeh@Sun.Com 	}
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	if (devicename == NULL) {
3978313SDina.Nimeh@Sun.Com 		int	minor;
3985643Saalok 
3998313SDina.Nimeh@Sun.Com 		/* pick one via the driver */
4008313SDina.Nimeh@Sun.Com 		minor = lofi_map_file(lfd, li, filename);
4018313SDina.Nimeh@Sun.Com 		/* if mapping succeeds, print the one picked */
4028313SDina.Nimeh@Sun.Com 		(void) printf("/dev/%s/%d\n", LOFI_BLOCK_NAME, minor);
4030Sstevel@tonic-gate 		return;
4040Sstevel@tonic-gate 	}
4058313SDina.Nimeh@Sun.Com 
4060Sstevel@tonic-gate 	/* use device we were given */
4078313SDina.Nimeh@Sun.Com 	li.li_minor = name_to_minor(devicename);
4088313SDina.Nimeh@Sun.Com 	if (li.li_minor == 0) {
4090Sstevel@tonic-gate 		die(gettext("malformed device name %s\n"), devicename);
4100Sstevel@tonic-gate 	}
4115643Saalok 	(void) strlcpy(li.li_filename, filename, sizeof (li.li_filename));
4128313SDina.Nimeh@Sun.Com 
4138313SDina.Nimeh@Sun.Com 	/* if device is already in use li.li_minor won't change */
4140Sstevel@tonic-gate 	if (ioctl(lfd, LOFI_MAP_FILE_MINOR, &li) == -1) {
4158313SDina.Nimeh@Sun.Com 		if (errno == ENOTSUP)
4168313SDina.Nimeh@Sun.Com 			warn(gettext("encrypting compressed files is "
4178313SDina.Nimeh@Sun.Com 			    "unsupported"));
4180Sstevel@tonic-gate 		die(gettext("could not map file %s to %s"), filename,
4190Sstevel@tonic-gate 		    devicename);
4200Sstevel@tonic-gate 	}
4218313SDina.Nimeh@Sun.Com 	wait_until_dev_complete(li.li_minor);
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate /*
4250Sstevel@tonic-gate  * Remove an association. Delete by device name if non-NULL, or by
4260Sstevel@tonic-gate  * filename otherwise.
4270Sstevel@tonic-gate  */
4280Sstevel@tonic-gate static void
delete_mapping(int lfd,const char * devicename,const char * filename,boolean_t force)4294451Seschrock delete_mapping(int lfd, const char *devicename, const char *filename,
4304451Seschrock     boolean_t force)
4310Sstevel@tonic-gate {
4320Sstevel@tonic-gate 	struct lofi_ioctl li;
4330Sstevel@tonic-gate 
4344451Seschrock 	li.li_force = force;
4356734Sjohnlev 	li.li_cleanup = B_FALSE;
4366734Sjohnlev 
4370Sstevel@tonic-gate 	if (devicename == NULL) {
4380Sstevel@tonic-gate 		/* delete by filename */
4395643Saalok 		(void) strlcpy(li.li_filename, filename,
4405643Saalok 		    sizeof (li.li_filename));
4410Sstevel@tonic-gate 		li.li_minor = 0;
4420Sstevel@tonic-gate 		if (ioctl(lfd, LOFI_UNMAP_FILE, &li) == -1) {
4430Sstevel@tonic-gate 			die(gettext("could not unmap file %s"), filename);
4440Sstevel@tonic-gate 		}
4450Sstevel@tonic-gate 		return;
4460Sstevel@tonic-gate 	}
4478069SDina.Nimeh@Sun.Com 
4480Sstevel@tonic-gate 	/* delete by device */
4490Sstevel@tonic-gate 	li.li_minor = name_to_minor(devicename);
4500Sstevel@tonic-gate 	if (li.li_minor == 0) {
4510Sstevel@tonic-gate 		die(gettext("malformed device name %s\n"), devicename);
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate 	if (ioctl(lfd, LOFI_UNMAP_FILE_MINOR, &li) == -1) {
4540Sstevel@tonic-gate 		die(gettext("could not unmap device %s"), devicename);
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate }
4570Sstevel@tonic-gate 
4588313SDina.Nimeh@Sun.Com /*
4598313SDina.Nimeh@Sun.Com  * Show filename given devicename, or devicename given filename.
4608313SDina.Nimeh@Sun.Com  */
4610Sstevel@tonic-gate static void
print_one_mapping(int lfd,const char * devicename,const char * filename)4620Sstevel@tonic-gate print_one_mapping(int lfd, const char *devicename, const char *filename)
4630Sstevel@tonic-gate {
4640Sstevel@tonic-gate 	struct lofi_ioctl li;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	if (devicename == NULL) {
4670Sstevel@tonic-gate 		/* given filename, print devicename */
4680Sstevel@tonic-gate 		li.li_minor = 0;
4695643Saalok 		(void) strlcpy(li.li_filename, filename,
4705643Saalok 		    sizeof (li.li_filename));
4710Sstevel@tonic-gate 		if (ioctl(lfd, LOFI_GET_MINOR, &li) == -1) {
4720Sstevel@tonic-gate 			die(gettext("could not find device for %s"), filename);
4730Sstevel@tonic-gate 		}
4740Sstevel@tonic-gate 		(void) printf("/dev/%s/%d\n", LOFI_BLOCK_NAME, li.li_minor);
4750Sstevel@tonic-gate 		return;
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	/* given devicename, print filename */
4790Sstevel@tonic-gate 	li.li_minor = name_to_minor(devicename);
4800Sstevel@tonic-gate 	if (li.li_minor == 0) {
4810Sstevel@tonic-gate 		die(gettext("malformed device name %s\n"), devicename);
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate 	if (ioctl(lfd, LOFI_GET_FILENAME, &li) == -1) {
4840Sstevel@tonic-gate 		die(gettext("could not find filename for %s"), devicename);
4850Sstevel@tonic-gate 	}
4860Sstevel@tonic-gate 	(void) printf("%s\n", li.li_filename);
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate 
4895643Saalok /*
4908313SDina.Nimeh@Sun.Com  * Print the list of all the mappings, including a header.
4918313SDina.Nimeh@Sun.Com  */
4928313SDina.Nimeh@Sun.Com static void
print_mappings(int fd)4938313SDina.Nimeh@Sun.Com print_mappings(int fd)
4948313SDina.Nimeh@Sun.Com {
4958313SDina.Nimeh@Sun.Com 	struct lofi_ioctl li;
4968313SDina.Nimeh@Sun.Com 	int	minor;
4978313SDina.Nimeh@Sun.Com 	int	maxminor;
4988313SDina.Nimeh@Sun.Com 	char	path[MAXPATHLEN];
4998313SDina.Nimeh@Sun.Com 	char	options[MAXPATHLEN];
5008313SDina.Nimeh@Sun.Com 
5018313SDina.Nimeh@Sun.Com 	li.li_minor = 0;
5028313SDina.Nimeh@Sun.Com 	if (ioctl(fd, LOFI_GET_MAXMINOR, &li) == -1) {
5038313SDina.Nimeh@Sun.Com 		die("ioctl");
5048313SDina.Nimeh@Sun.Com 	}
5058313SDina.Nimeh@Sun.Com 	maxminor = li.li_minor;
5068313SDina.Nimeh@Sun.Com 
5078313SDina.Nimeh@Sun.Com 	(void) printf(FORMAT, gettext("Block Device"), gettext("File"),
5088313SDina.Nimeh@Sun.Com 	    gettext("Options"));
5098313SDina.Nimeh@Sun.Com 	for (minor = 1; minor <= maxminor; minor++) {
5108313SDina.Nimeh@Sun.Com 		li.li_minor = minor;
5118313SDina.Nimeh@Sun.Com 		if (ioctl(fd, LOFI_GET_FILENAME, &li) == -1) {
5128313SDina.Nimeh@Sun.Com 			if (errno == ENXIO)
5138313SDina.Nimeh@Sun.Com 				continue;
5148313SDina.Nimeh@Sun.Com 			warn("ioctl");
5158313SDina.Nimeh@Sun.Com 			break;
5168313SDina.Nimeh@Sun.Com 		}
5178313SDina.Nimeh@Sun.Com 		(void) snprintf(path, sizeof (path), "/dev/%s/%d",
5188313SDina.Nimeh@Sun.Com 		    LOFI_BLOCK_NAME, minor);
5198313SDina.Nimeh@Sun.Com 		/*
5208313SDina.Nimeh@Sun.Com 		 * Encrypted lofi and compressed lofi are mutually exclusive.
5218313SDina.Nimeh@Sun.Com 		 */
5228313SDina.Nimeh@Sun.Com 		if (li.li_crypto_enabled)
5238313SDina.Nimeh@Sun.Com 			(void) snprintf(options, sizeof (options),
5248313SDina.Nimeh@Sun.Com 			    gettext("Encrypted"));
5258313SDina.Nimeh@Sun.Com 		else if (li.li_algorithm[0] != '\0')
5268313SDina.Nimeh@Sun.Com 			(void) snprintf(options, sizeof (options),
5278313SDina.Nimeh@Sun.Com 			    gettext("Compressed(%s)"), li.li_algorithm);
5288313SDina.Nimeh@Sun.Com 		else
5298313SDina.Nimeh@Sun.Com 			(void) snprintf(options, sizeof (options), "-");
5308313SDina.Nimeh@Sun.Com 
5318313SDina.Nimeh@Sun.Com 		(void) printf(FORMAT, path, li.li_filename, options);
5328313SDina.Nimeh@Sun.Com 	}
5338313SDina.Nimeh@Sun.Com }
5348313SDina.Nimeh@Sun.Com 
5358313SDina.Nimeh@Sun.Com /*
5368313SDina.Nimeh@Sun.Com  * Verify the cipher selected by user.
5378313SDina.Nimeh@Sun.Com  */
5388313SDina.Nimeh@Sun.Com static mech_alias_t *
ciph2mech(const char * alias)5398313SDina.Nimeh@Sun.Com ciph2mech(const char *alias)
5408313SDina.Nimeh@Sun.Com {
5418313SDina.Nimeh@Sun.Com 	int	i;
5428313SDina.Nimeh@Sun.Com 
5438313SDina.Nimeh@Sun.Com 	for (i = 0; i < mech_aliases_count; i++) {
5448313SDina.Nimeh@Sun.Com 		if (strcasecmp(alias, mech_aliases[i].alias) == 0)
5458313SDina.Nimeh@Sun.Com 			return (&mech_aliases[i]);
5468313SDina.Nimeh@Sun.Com 	}
5478313SDina.Nimeh@Sun.Com 	return (NULL);
5488313SDina.Nimeh@Sun.Com }
5498313SDina.Nimeh@Sun.Com 
5508313SDina.Nimeh@Sun.Com /*
5518313SDina.Nimeh@Sun.Com  * Verify user selected cipher is also available in kernel.
5528313SDina.Nimeh@Sun.Com  *
5538313SDina.Nimeh@Sun.Com  * While traversing kernel list of mechs, if the cipher is supported in the
5548313SDina.Nimeh@Sun.Com  * kernel for both encryption and decryption, it also picks up the min/max
5558313SDina.Nimeh@Sun.Com  * key size.
5568313SDina.Nimeh@Sun.Com  */
5578313SDina.Nimeh@Sun.Com static boolean_t
kernel_cipher_check(mech_alias_t * cipher)5588313SDina.Nimeh@Sun.Com kernel_cipher_check(mech_alias_t *cipher)
5598313SDina.Nimeh@Sun.Com {
5608313SDina.Nimeh@Sun.Com 	boolean_t ciph_ok = B_FALSE;
5618313SDina.Nimeh@Sun.Com 	boolean_t iv_ok = B_FALSE;
5628313SDina.Nimeh@Sun.Com 	int	i;
5638313SDina.Nimeh@Sun.Com 	int	count;
5648313SDina.Nimeh@Sun.Com 	crypto_get_mechanism_list_t *kciphers = NULL;
5658313SDina.Nimeh@Sun.Com 	crypto_get_all_mechanism_info_t *kinfo = NULL;
5668313SDina.Nimeh@Sun.Com 	int	fd = -1;
5678313SDina.Nimeh@Sun.Com 	size_t	keymin;
5688313SDina.Nimeh@Sun.Com 	size_t	keymax;
5698313SDina.Nimeh@Sun.Com 
5708313SDina.Nimeh@Sun.Com 	/* if cipher doesn't need iv generating mech, bypass that check now */
5718313SDina.Nimeh@Sun.Com 	if (cipher->iv_name == NULL)
5728313SDina.Nimeh@Sun.Com 		iv_ok = B_TRUE;
5738313SDina.Nimeh@Sun.Com 
5748313SDina.Nimeh@Sun.Com 	/* allocate some space for the list of kernel ciphers */
5758313SDina.Nimeh@Sun.Com 	count = DEFAULT_CIPHER_NUM;
5768313SDina.Nimeh@Sun.Com 	kciphers = malloc(sizeof (crypto_get_mechanism_list_t) +
5778313SDina.Nimeh@Sun.Com 	    sizeof (crypto_mech_name_t) * (count - 1));
5788313SDina.Nimeh@Sun.Com 	if (kciphers == NULL)
5798313SDina.Nimeh@Sun.Com 		die(gettext("failed to allocate memory for list of "
5808313SDina.Nimeh@Sun.Com 		    "kernel mechanisms"));
5818313SDina.Nimeh@Sun.Com 	kciphers->ml_count = count;
5828313SDina.Nimeh@Sun.Com 
5838313SDina.Nimeh@Sun.Com 	/* query crypto device to get list of kernel ciphers */
5848313SDina.Nimeh@Sun.Com 	if ((fd = open("/dev/crypto", O_RDWR)) == -1) {
5858313SDina.Nimeh@Sun.Com 		warn(gettext("failed to open %s"), "/dev/crypto");
5868313SDina.Nimeh@Sun.Com 		goto kcc_out;
5878313SDina.Nimeh@Sun.Com 	}
5888313SDina.Nimeh@Sun.Com 
5898313SDina.Nimeh@Sun.Com 	if (ioctl(fd, CRYPTO_GET_MECHANISM_LIST, kciphers) == -1) {
5908313SDina.Nimeh@Sun.Com 		warn(gettext("CRYPTO_GET_MECHANISM_LIST ioctl failed"));
5918313SDina.Nimeh@Sun.Com 		goto kcc_out;
5928313SDina.Nimeh@Sun.Com 	}
5938313SDina.Nimeh@Sun.Com 
5948313SDina.Nimeh@Sun.Com 	if (kciphers->ml_return_value == CRYPTO_BUFFER_TOO_SMALL) {
5958313SDina.Nimeh@Sun.Com 		count = kciphers->ml_count;
5968313SDina.Nimeh@Sun.Com 		free(kciphers);
5978313SDina.Nimeh@Sun.Com 		kciphers = malloc(sizeof (crypto_get_mechanism_list_t) +
5988313SDina.Nimeh@Sun.Com 		    sizeof (crypto_mech_name_t) * (count - 1));
5998313SDina.Nimeh@Sun.Com 		if (kciphers == NULL) {
6008313SDina.Nimeh@Sun.Com 			warn(gettext("failed to allocate memory for list of "
6018313SDina.Nimeh@Sun.Com 			    "kernel mechanisms"));
6028313SDina.Nimeh@Sun.Com 			goto kcc_out;
6038313SDina.Nimeh@Sun.Com 		}
6048313SDina.Nimeh@Sun.Com 		kciphers->ml_count = count;
6058313SDina.Nimeh@Sun.Com 
6068313SDina.Nimeh@Sun.Com 		if (ioctl(fd, CRYPTO_GET_MECHANISM_LIST, kciphers) == -1) {
6078313SDina.Nimeh@Sun.Com 			warn(gettext("CRYPTO_GET_MECHANISM_LIST ioctl failed"));
6088313SDina.Nimeh@Sun.Com 			goto kcc_out;
6098313SDina.Nimeh@Sun.Com 		}
6108313SDina.Nimeh@Sun.Com 	}
6118313SDina.Nimeh@Sun.Com 
6128313SDina.Nimeh@Sun.Com 	if (kciphers->ml_return_value != CRYPTO_SUCCESS) {
6138313SDina.Nimeh@Sun.Com 		warn(gettext(
6148313SDina.Nimeh@Sun.Com 		    "CRYPTO_GET_MECHANISM_LIST ioctl return value = %d\n"),
6158313SDina.Nimeh@Sun.Com 		    kciphers->ml_return_value);
6168313SDina.Nimeh@Sun.Com 		goto kcc_out;
6178313SDina.Nimeh@Sun.Com 	}
6188313SDina.Nimeh@Sun.Com 
6198313SDina.Nimeh@Sun.Com 	/*
6208313SDina.Nimeh@Sun.Com 	 * scan list of kernel ciphers looking for the selected one and if
6218313SDina.Nimeh@Sun.Com 	 * it needs an iv generated using another cipher, also look for that
6228313SDina.Nimeh@Sun.Com 	 * additional cipher to be used for generating the iv
6238313SDina.Nimeh@Sun.Com 	 */
6248313SDina.Nimeh@Sun.Com 	count = kciphers->ml_count;
6258313SDina.Nimeh@Sun.Com 	for (i = 0; i < count && !(ciph_ok && iv_ok); i++) {
6268313SDina.Nimeh@Sun.Com 		if (!ciph_ok &&
6278313SDina.Nimeh@Sun.Com 		    strcasecmp(cipher->name, kciphers->ml_list[i]) == 0)
6288313SDina.Nimeh@Sun.Com 			ciph_ok = B_TRUE;
6298313SDina.Nimeh@Sun.Com 		if (!iv_ok &&
6308313SDina.Nimeh@Sun.Com 		    strcasecmp(cipher->iv_name, kciphers->ml_list[i]) == 0)
6318313SDina.Nimeh@Sun.Com 			iv_ok = B_TRUE;
6328313SDina.Nimeh@Sun.Com 	}
6338313SDina.Nimeh@Sun.Com 	free(kciphers);
6348313SDina.Nimeh@Sun.Com 	kciphers = NULL;
6358313SDina.Nimeh@Sun.Com 
6368313SDina.Nimeh@Sun.Com 	if (!ciph_ok)
6378313SDina.Nimeh@Sun.Com 		warn(gettext("%s mechanism not supported in kernel\n"),
6388313SDina.Nimeh@Sun.Com 		    cipher->name);
6398313SDina.Nimeh@Sun.Com 	if (!iv_ok)
6408313SDina.Nimeh@Sun.Com 		warn(gettext("%s mechanism not supported in kernel\n"),
6418313SDina.Nimeh@Sun.Com 		    cipher->iv_name);
6428313SDina.Nimeh@Sun.Com 
6438313SDina.Nimeh@Sun.Com 	if (ciph_ok) {
6448313SDina.Nimeh@Sun.Com 		/* Get the details about the user selected cipher */
6458313SDina.Nimeh@Sun.Com 		count = DEFAULT_MECHINFO_NUM;
6468313SDina.Nimeh@Sun.Com 		kinfo = malloc(sizeof (crypto_get_all_mechanism_info_t) +
6478313SDina.Nimeh@Sun.Com 		    sizeof (crypto_mechanism_info_t) * (count - 1));
6488313SDina.Nimeh@Sun.Com 		if (kinfo == NULL) {
6498313SDina.Nimeh@Sun.Com 			warn(gettext("failed to allocate memory for "
6508313SDina.Nimeh@Sun.Com 			    "kernel mechanism info"));
6518313SDina.Nimeh@Sun.Com 			goto kcc_out;
6528313SDina.Nimeh@Sun.Com 		}
6538313SDina.Nimeh@Sun.Com 		kinfo->mi_count = count;
6548313SDina.Nimeh@Sun.Com 		(void) strlcpy(kinfo->mi_mechanism_name, cipher->name,
6558313SDina.Nimeh@Sun.Com 		    CRYPTO_MAX_MECH_NAME);
6568313SDina.Nimeh@Sun.Com 
6578313SDina.Nimeh@Sun.Com 		if (ioctl(fd, CRYPTO_GET_ALL_MECHANISM_INFO, kinfo) == -1) {
6588313SDina.Nimeh@Sun.Com 			warn(gettext(
6598313SDina.Nimeh@Sun.Com 			    "CRYPTO_GET_ALL_MECHANISM_INFO ioctl failed"));
6608313SDina.Nimeh@Sun.Com 			goto kcc_out;
6618313SDina.Nimeh@Sun.Com 		}
6628313SDina.Nimeh@Sun.Com 
6638313SDina.Nimeh@Sun.Com 		if (kinfo->mi_return_value == CRYPTO_BUFFER_TOO_SMALL) {
6648313SDina.Nimeh@Sun.Com 			count = kinfo->mi_count;
6658313SDina.Nimeh@Sun.Com 			free(kinfo);
6668313SDina.Nimeh@Sun.Com 			kinfo = malloc(
6678313SDina.Nimeh@Sun.Com 			    sizeof (crypto_get_all_mechanism_info_t) +
6688313SDina.Nimeh@Sun.Com 			    sizeof (crypto_mechanism_info_t) * (count - 1));
6698313SDina.Nimeh@Sun.Com 			if (kinfo == NULL) {
6708313SDina.Nimeh@Sun.Com 				warn(gettext("failed to allocate memory for "
6718313SDina.Nimeh@Sun.Com 				    "kernel mechanism info"));
6728313SDina.Nimeh@Sun.Com 				goto kcc_out;
6738313SDina.Nimeh@Sun.Com 			}
6748313SDina.Nimeh@Sun.Com 			kinfo->mi_count = count;
6758313SDina.Nimeh@Sun.Com 			(void) strlcpy(kinfo->mi_mechanism_name, cipher->name,
6768313SDina.Nimeh@Sun.Com 			    CRYPTO_MAX_MECH_NAME);
6778313SDina.Nimeh@Sun.Com 
6788313SDina.Nimeh@Sun.Com 			if (ioctl(fd, CRYPTO_GET_ALL_MECHANISM_INFO, kinfo) ==
6798313SDina.Nimeh@Sun.Com 			    -1) {
6808313SDina.Nimeh@Sun.Com 				warn(gettext("CRYPTO_GET_ALL_MECHANISM_INFO "
6818313SDina.Nimeh@Sun.Com 				    "ioctl failed"));
6828313SDina.Nimeh@Sun.Com 				goto kcc_out;
6838313SDina.Nimeh@Sun.Com 			}
6848313SDina.Nimeh@Sun.Com 		}
6858313SDina.Nimeh@Sun.Com 
6868313SDina.Nimeh@Sun.Com 		if (kinfo->mi_return_value != CRYPTO_SUCCESS) {
6878313SDina.Nimeh@Sun.Com 			warn(gettext("CRYPTO_GET_ALL_MECHANISM_INFO ioctl "
6888313SDina.Nimeh@Sun.Com 			    "return value = %d\n"), kinfo->mi_return_value);
6898313SDina.Nimeh@Sun.Com 			goto kcc_out;
6908313SDina.Nimeh@Sun.Com 		}
6918313SDina.Nimeh@Sun.Com 
6928313SDina.Nimeh@Sun.Com 		/* Set key min and max size */
6938313SDina.Nimeh@Sun.Com 		count = kinfo->mi_count;
6948313SDina.Nimeh@Sun.Com 		i = 0;
6958313SDina.Nimeh@Sun.Com 		if (i < count) {
6968313SDina.Nimeh@Sun.Com 			keymin = kinfo->mi_list[i].mi_min_key_size;
6978313SDina.Nimeh@Sun.Com 			keymax = kinfo->mi_list[i].mi_max_key_size;
6988313SDina.Nimeh@Sun.Com 			if (kinfo->mi_list[i].mi_keysize_unit &
6998313SDina.Nimeh@Sun.Com 			    CRYPTO_KEYSIZE_UNIT_IN_BITS) {
7008313SDina.Nimeh@Sun.Com 				keymin = CRYPTO_BITS2BYTES(keymin);
7018313SDina.Nimeh@Sun.Com 				keymax = CRYPTO_BITS2BYTES(keymax);
7028313SDina.Nimeh@Sun.Com 
7038313SDina.Nimeh@Sun.Com 			}
7048313SDina.Nimeh@Sun.Com 			cipher->min_keysize = keymin;
7058313SDina.Nimeh@Sun.Com 			cipher->max_keysize = keymax;
7068313SDina.Nimeh@Sun.Com 		}
7078313SDina.Nimeh@Sun.Com 		free(kinfo);
7088313SDina.Nimeh@Sun.Com 		kinfo = NULL;
7098313SDina.Nimeh@Sun.Com 
7108313SDina.Nimeh@Sun.Com 		if (i == count) {
7118313SDina.Nimeh@Sun.Com 			(void) close(fd);
7128313SDina.Nimeh@Sun.Com 			die(gettext(
7138313SDina.Nimeh@Sun.Com 			    "failed to find usable %s kernel mechanism, "
7148313SDina.Nimeh@Sun.Com 			    "use \"cryptoadm list -m\" to find available "
7158313SDina.Nimeh@Sun.Com 			    "mechanisms\n"),
7168313SDina.Nimeh@Sun.Com 			    cipher->name);
7178313SDina.Nimeh@Sun.Com 		}
7188313SDina.Nimeh@Sun.Com 	}
7198313SDina.Nimeh@Sun.Com 
7208313SDina.Nimeh@Sun.Com 	/* Note: key min/max, unit size, usage for iv cipher are not checked. */
7218313SDina.Nimeh@Sun.Com 
7228313SDina.Nimeh@Sun.Com 	return (ciph_ok && iv_ok);
7238313SDina.Nimeh@Sun.Com 
7248313SDina.Nimeh@Sun.Com kcc_out:
7258313SDina.Nimeh@Sun.Com 	if (kinfo != NULL)
7268313SDina.Nimeh@Sun.Com 		free(kinfo);
7278313SDina.Nimeh@Sun.Com 	if (kciphers != NULL)
7288313SDina.Nimeh@Sun.Com 		free(kciphers);
7298313SDina.Nimeh@Sun.Com 	if (fd != -1)
7308313SDina.Nimeh@Sun.Com 		(void) close(fd);
7318313SDina.Nimeh@Sun.Com 	return (B_FALSE);
7328313SDina.Nimeh@Sun.Com }
7338313SDina.Nimeh@Sun.Com 
7348313SDina.Nimeh@Sun.Com /*
7358313SDina.Nimeh@Sun.Com  * Break up token spec into its components (non-destructive)
7368313SDina.Nimeh@Sun.Com  */
7378313SDina.Nimeh@Sun.Com static token_spec_t *
parsetoken(char * spec)7388313SDina.Nimeh@Sun.Com parsetoken(char *spec)
7398313SDina.Nimeh@Sun.Com {
7408313SDina.Nimeh@Sun.Com #define	FLD_NAME	0
7418313SDina.Nimeh@Sun.Com #define	FLD_MANUF	1
7428313SDina.Nimeh@Sun.Com #define	FLD_SERIAL	2
7438313SDina.Nimeh@Sun.Com #define	FLD_LABEL	3
7448313SDina.Nimeh@Sun.Com #define	NFIELDS		4
7458313SDina.Nimeh@Sun.Com #define	nullfield(i)	((field[(i)+1] - field[(i)]) <= 1)
7468313SDina.Nimeh@Sun.Com #define	copyfield(fld, i)	\
7478313SDina.Nimeh@Sun.Com 		{							\
7488313SDina.Nimeh@Sun.Com 			int	n;					\
7498313SDina.Nimeh@Sun.Com 			(fld) = NULL;					\
7508313SDina.Nimeh@Sun.Com 			if ((n = (field[(i)+1] - field[(i)])) > 1) {	\
7518313SDina.Nimeh@Sun.Com 				if (((fld) = malloc(n)) != NULL) {	\
7528313SDina.Nimeh@Sun.Com 					(void) strncpy((fld), field[(i)], n); \
7538313SDina.Nimeh@Sun.Com 					((fld))[n - 1] = '\0';		\
7548313SDina.Nimeh@Sun.Com 				}					\
7558313SDina.Nimeh@Sun.Com 			}						\
7568313SDina.Nimeh@Sun.Com 		}
7578313SDina.Nimeh@Sun.Com 
7588313SDina.Nimeh@Sun.Com 	int	i;
7598313SDina.Nimeh@Sun.Com 	char	*field[NFIELDS + 1];	/* +1 to catch extra delimiters */
7608313SDina.Nimeh@Sun.Com 	token_spec_t *ti = NULL;
7618313SDina.Nimeh@Sun.Com 
7628313SDina.Nimeh@Sun.Com 	if (spec == NULL)
7638313SDina.Nimeh@Sun.Com 		return (NULL);
7648313SDina.Nimeh@Sun.Com 
7658313SDina.Nimeh@Sun.Com 	/*
7668313SDina.Nimeh@Sun.Com 	 * Correct format is "[name]:[manuf]:[serial]:key". Can't use
7678313SDina.Nimeh@Sun.Com 	 * strtok because it treats ":::key" and "key:::" and "key" all
7688313SDina.Nimeh@Sun.Com 	 * as the same thing, and we can't have the :s compressed away.
7698313SDina.Nimeh@Sun.Com 	 */
7708313SDina.Nimeh@Sun.Com 	field[0] = spec;
7718313SDina.Nimeh@Sun.Com 	for (i = 1; i < NFIELDS + 1; i++) {
7728313SDina.Nimeh@Sun.Com 		field[i] = strchr(field[i-1], ':');
7738313SDina.Nimeh@Sun.Com 		if (field[i] == NULL)
7748313SDina.Nimeh@Sun.Com 			break;
7758313SDina.Nimeh@Sun.Com 		field[i]++;
7768313SDina.Nimeh@Sun.Com 	}
7778313SDina.Nimeh@Sun.Com 	if (i < NFIELDS)		/* not enough fields */
7788313SDina.Nimeh@Sun.Com 		return (NULL);
7798313SDina.Nimeh@Sun.Com 	if (field[NFIELDS] != NULL)	/* too many fields */
7808313SDina.Nimeh@Sun.Com 		return (NULL);
7818313SDina.Nimeh@Sun.Com 	field[NFIELDS] = strchr(field[NFIELDS-1], '\0') + 1;
7828313SDina.Nimeh@Sun.Com 
7838313SDina.Nimeh@Sun.Com 	/* key label can't be empty */
7848313SDina.Nimeh@Sun.Com 	if (nullfield(FLD_LABEL))
7858313SDina.Nimeh@Sun.Com 		return (NULL);
7868313SDina.Nimeh@Sun.Com 
7878313SDina.Nimeh@Sun.Com 	ti = malloc(sizeof (token_spec_t));
7888313SDina.Nimeh@Sun.Com 	if (ti == NULL)
7898313SDina.Nimeh@Sun.Com 		return (NULL);
7908313SDina.Nimeh@Sun.Com 
7918313SDina.Nimeh@Sun.Com 	copyfield(ti->name, FLD_NAME);
7928313SDina.Nimeh@Sun.Com 	copyfield(ti->mfr, FLD_MANUF);
7938313SDina.Nimeh@Sun.Com 	copyfield(ti->serno, FLD_SERIAL);
7948313SDina.Nimeh@Sun.Com 	copyfield(ti->key, FLD_LABEL);
7958313SDina.Nimeh@Sun.Com 
7968313SDina.Nimeh@Sun.Com 	/*
7978313SDina.Nimeh@Sun.Com 	 * If token specified and it only contains a key label, then
7988313SDina.Nimeh@Sun.Com 	 * search all tokens for the key, otherwise only those with
7998313SDina.Nimeh@Sun.Com 	 * matching name, mfr, and serno are used.
8008313SDina.Nimeh@Sun.Com 	 */
8018313SDina.Nimeh@Sun.Com 	/*
8028313SDina.Nimeh@Sun.Com 	 * That's how we'd like it to be, however, if only the key label
8038313SDina.Nimeh@Sun.Com 	 * is specified, default to using softtoken.  It's easier.
8048313SDina.Nimeh@Sun.Com 	 */
8058313SDina.Nimeh@Sun.Com 	if (ti->name == NULL && ti->mfr == NULL && ti->serno == NULL)
8068313SDina.Nimeh@Sun.Com 		ti->name = strdup(pkcs11_default_token());
8078313SDina.Nimeh@Sun.Com 	return (ti);
8088313SDina.Nimeh@Sun.Com }
8098313SDina.Nimeh@Sun.Com 
8108313SDina.Nimeh@Sun.Com /*
8118313SDina.Nimeh@Sun.Com  * PBE the passphrase into a raw key
8128313SDina.Nimeh@Sun.Com  */
8138313SDina.Nimeh@Sun.Com static void
getkeyfromuser(mech_alias_t * cipher,char ** raw_key,size_t * raw_key_sz)8148313SDina.Nimeh@Sun.Com getkeyfromuser(mech_alias_t *cipher, char **raw_key, size_t *raw_key_sz)
8158313SDina.Nimeh@Sun.Com {
8168313SDina.Nimeh@Sun.Com 	CK_SESSION_HANDLE sess;
8178313SDina.Nimeh@Sun.Com 	CK_RV	rv;
8188313SDina.Nimeh@Sun.Com 	char	*pass = NULL;
8198313SDina.Nimeh@Sun.Com 	size_t	passlen = 0;
8208313SDina.Nimeh@Sun.Com 	void	*salt = NULL;	/* don't use NULL, see note on salt below */
8218313SDina.Nimeh@Sun.Com 	size_t	saltlen = 0;
8228313SDina.Nimeh@Sun.Com 	CK_KEY_TYPE ktype;
8238313SDina.Nimeh@Sun.Com 	void	*kvalue;
8248313SDina.Nimeh@Sun.Com 	size_t	klen;
8258313SDina.Nimeh@Sun.Com 
8268313SDina.Nimeh@Sun.Com 	/* did init_crypto find a slot that supports this cipher? */
8278313SDina.Nimeh@Sun.Com 	if (cipher->slot == (CK_SLOT_ID)-1 || cipher->max_keysize == 0) {
8288313SDina.Nimeh@Sun.Com 		rv = CKR_MECHANISM_INVALID;
8298313SDina.Nimeh@Sun.Com 		goto cleanup;
8308313SDina.Nimeh@Sun.Com 	}
8318313SDina.Nimeh@Sun.Com 
8328313SDina.Nimeh@Sun.Com 	rv = pkcs11_mech2keytype(cipher->type, &ktype);
8338313SDina.Nimeh@Sun.Com 	if (rv != CKR_OK)
8348313SDina.Nimeh@Sun.Com 		goto cleanup;
8358313SDina.Nimeh@Sun.Com 
8368313SDina.Nimeh@Sun.Com 	/*
8378313SDina.Nimeh@Sun.Com 	 * use the passphrase to generate a PBE PKCS#5 secret key and
8388313SDina.Nimeh@Sun.Com 	 * retrieve the raw key data to eventually pass it to the kernel;
8398313SDina.Nimeh@Sun.Com 	 */
8408313SDina.Nimeh@Sun.Com 	rv = C_OpenSession(cipher->slot, CKF_SERIAL_SESSION, NULL, NULL, &sess);
8418313SDina.Nimeh@Sun.Com 	if (rv != CKR_OK)
8428313SDina.Nimeh@Sun.Com 		goto cleanup;
8438313SDina.Nimeh@Sun.Com 
8448313SDina.Nimeh@Sun.Com 	/* get user passphrase with 8 byte minimum */
8458313SDina.Nimeh@Sun.Com 	if (pkcs11_get_pass(NULL, &pass, &passlen, MIN_PASSLEN, B_TRUE) < 0) {
8468313SDina.Nimeh@Sun.Com 		die(gettext("passphrases do not match\n"));
8478313SDina.Nimeh@Sun.Com 	}
8488313SDina.Nimeh@Sun.Com 
8498313SDina.Nimeh@Sun.Com 	/*
8508313SDina.Nimeh@Sun.Com 	 * salt should not be NULL, or else pkcs11_PasswdToKey() will
8518313SDina.Nimeh@Sun.Com 	 * complain about CKR_MECHANISM_PARAM_INVALID; the following is
8528313SDina.Nimeh@Sun.Com 	 * to make up for not having a salt until a proper one is used
8538313SDina.Nimeh@Sun.Com 	 */
8548313SDina.Nimeh@Sun.Com 	salt = pass;
8558313SDina.Nimeh@Sun.Com 	saltlen = passlen;
8568313SDina.Nimeh@Sun.Com 
8578313SDina.Nimeh@Sun.Com 	klen = cipher->max_keysize;
8588313SDina.Nimeh@Sun.Com 	rv = pkcs11_PasswdToKey(sess, pass, passlen, salt, saltlen, ktype,
8598313SDina.Nimeh@Sun.Com 	    cipher->max_keysize, &kvalue, &klen);
8608313SDina.Nimeh@Sun.Com 
8618313SDina.Nimeh@Sun.Com 	(void) C_CloseSession(sess);
8628313SDina.Nimeh@Sun.Com 
8638313SDina.Nimeh@Sun.Com 	if (rv != CKR_OK) {
8648313SDina.Nimeh@Sun.Com 		goto cleanup;
8658313SDina.Nimeh@Sun.Com 	}
8668313SDina.Nimeh@Sun.Com 
8678313SDina.Nimeh@Sun.Com 	/* assert(klen == cipher->max_keysize); */
8688313SDina.Nimeh@Sun.Com 	*raw_key_sz = klen;
8698313SDina.Nimeh@Sun.Com 	*raw_key = (char *)kvalue;
8708313SDina.Nimeh@Sun.Com 	return;
8718313SDina.Nimeh@Sun.Com 
8728313SDina.Nimeh@Sun.Com cleanup:
8738313SDina.Nimeh@Sun.Com 	die(gettext("failed to generate %s key from passphrase: %s"),
8748313SDina.Nimeh@Sun.Com 	    cipher->alias, pkcs11_strerror(rv));
8758313SDina.Nimeh@Sun.Com }
8768313SDina.Nimeh@Sun.Com 
8778313SDina.Nimeh@Sun.Com /*
8788313SDina.Nimeh@Sun.Com  * Read raw key from file; also handles ephemeral keys.
8798313SDina.Nimeh@Sun.Com  */
8808313SDina.Nimeh@Sun.Com void
getkeyfromfile(const char * pathname,mech_alias_t * cipher,char ** key,size_t * ksz)8818313SDina.Nimeh@Sun.Com getkeyfromfile(const char *pathname, mech_alias_t *cipher, char **key,
8828313SDina.Nimeh@Sun.Com     size_t *ksz)
8838313SDina.Nimeh@Sun.Com {
8848313SDina.Nimeh@Sun.Com 	int	fd;
8858313SDina.Nimeh@Sun.Com 	struct stat sbuf;
8868313SDina.Nimeh@Sun.Com 	boolean_t notplain = B_FALSE;
8878313SDina.Nimeh@Sun.Com 	ssize_t	cursz;
8888313SDina.Nimeh@Sun.Com 	ssize_t	nread;
8898313SDina.Nimeh@Sun.Com 
8908313SDina.Nimeh@Sun.Com 	/* ephemeral keys are just random data */
8918313SDina.Nimeh@Sun.Com 	if (pathname == NULL) {
8928313SDina.Nimeh@Sun.Com 		*ksz = cipher->max_keysize;
8938313SDina.Nimeh@Sun.Com 		*key = malloc(*ksz);
8948313SDina.Nimeh@Sun.Com 		if (*key == NULL)
8958313SDina.Nimeh@Sun.Com 			die(gettext("failed to allocate memory for"
8968313SDina.Nimeh@Sun.Com 			    " ephemeral key"));
897*9127SDina.Nimeh@Sun.COM 		if (pkcs11_get_urandom(*key, *ksz) < 0) {
8988313SDina.Nimeh@Sun.Com 			free(*key);
8998313SDina.Nimeh@Sun.Com 			die(gettext("failed to get enough random data"));
9008313SDina.Nimeh@Sun.Com 		}
9018313SDina.Nimeh@Sun.Com 		return;
9028313SDina.Nimeh@Sun.Com 	}
9038313SDina.Nimeh@Sun.Com 
9048313SDina.Nimeh@Sun.Com 	/*
9058313SDina.Nimeh@Sun.Com 	 * If the remaining section of code didn't also check for secure keyfile
9068313SDina.Nimeh@Sun.Com 	 * permissions and whether the key is within cipher min and max lengths,
9078313SDina.Nimeh@Sun.Com 	 * (or, if those things moved out of this block), we could have had:
9088313SDina.Nimeh@Sun.Com 	 *	if (pkcs11_read_data(pathname, key, ksz) < 0)
9098313SDina.Nimeh@Sun.Com 	 *		handle_error();
9108313SDina.Nimeh@Sun.Com 	 */
9118313SDina.Nimeh@Sun.Com 
9128313SDina.Nimeh@Sun.Com 	if ((fd = open(pathname, O_RDONLY, 0)) == -1)
9138313SDina.Nimeh@Sun.Com 		die(gettext("open of keyfile (%s) failed"), pathname);
9148313SDina.Nimeh@Sun.Com 
9158313SDina.Nimeh@Sun.Com 	if (fstat(fd, &sbuf) == -1)
9168313SDina.Nimeh@Sun.Com 		die(gettext("fstat of keyfile (%s) failed"), pathname);
9178313SDina.Nimeh@Sun.Com 
9188313SDina.Nimeh@Sun.Com 	if (S_ISREG(sbuf.st_mode)) {
9198313SDina.Nimeh@Sun.Com 		if ((sbuf.st_mode & (S_IWGRP | S_IWOTH)) != 0)
9208313SDina.Nimeh@Sun.Com 			die(gettext("insecure permissions on keyfile %s\n"),
9218313SDina.Nimeh@Sun.Com 			    pathname);
9228313SDina.Nimeh@Sun.Com 
9238313SDina.Nimeh@Sun.Com 		*ksz = sbuf.st_size;
9248313SDina.Nimeh@Sun.Com 		if (*ksz < cipher->min_keysize || cipher->max_keysize < *ksz) {
9258313SDina.Nimeh@Sun.Com 			warn(gettext("%s: invalid keysize: %d\n"),
9268313SDina.Nimeh@Sun.Com 			    pathname, (int)*ksz);
9278313SDina.Nimeh@Sun.Com 			die(gettext("\t%d <= keysize <= %d\n"),
9288313SDina.Nimeh@Sun.Com 			    cipher->min_keysize, cipher->max_keysize);
9298313SDina.Nimeh@Sun.Com 		}
9308313SDina.Nimeh@Sun.Com 	} else {
9318313SDina.Nimeh@Sun.Com 		*ksz = cipher->max_keysize;
9328313SDina.Nimeh@Sun.Com 		notplain = B_TRUE;
9338313SDina.Nimeh@Sun.Com 	}
9348313SDina.Nimeh@Sun.Com 
9358313SDina.Nimeh@Sun.Com 	*key = malloc(*ksz);
9368313SDina.Nimeh@Sun.Com 	if (*key == NULL)
9378313SDina.Nimeh@Sun.Com 		die(gettext("failed to allocate memory for key from file"));
9388313SDina.Nimeh@Sun.Com 
9398313SDina.Nimeh@Sun.Com 	for (cursz = 0, nread = 0; cursz < *ksz; cursz += nread) {
9408313SDina.Nimeh@Sun.Com 		nread = read(fd, *key, *ksz);
9418313SDina.Nimeh@Sun.Com 		if (nread > 0)
9428313SDina.Nimeh@Sun.Com 			continue;
9438313SDina.Nimeh@Sun.Com 		/*
9448313SDina.Nimeh@Sun.Com 		 * nread == 0.  If it's not a regular file we were trying to
9458313SDina.Nimeh@Sun.Com 		 * get the maximum keysize of data possible for this cipher.
9468313SDina.Nimeh@Sun.Com 		 * But if we've got at least the minimum keysize of data,
9478313SDina.Nimeh@Sun.Com 		 * round down to the nearest keysize unit and call it good.
9488313SDina.Nimeh@Sun.Com 		 * If we haven't met the minimum keysize, that's an error.
9498313SDina.Nimeh@Sun.Com 		 * If it's a regular file, nread = 0 is also an error.
9508313SDina.Nimeh@Sun.Com 		 */
9518313SDina.Nimeh@Sun.Com 		if (nread == 0 && notplain && cursz >= cipher->min_keysize) {
9528313SDina.Nimeh@Sun.Com 			*ksz = (cursz / cipher->min_keysize) *
9538313SDina.Nimeh@Sun.Com 			    cipher->min_keysize;
9548313SDina.Nimeh@Sun.Com 			break;
9558313SDina.Nimeh@Sun.Com 		}
9568313SDina.Nimeh@Sun.Com 		die(gettext("%s: can't read all keybytes"), pathname);
9578313SDina.Nimeh@Sun.Com 	}
9588313SDina.Nimeh@Sun.Com 	(void) close(fd);
9598313SDina.Nimeh@Sun.Com }
9608313SDina.Nimeh@Sun.Com 
9618313SDina.Nimeh@Sun.Com /*
9628313SDina.Nimeh@Sun.Com  * Read the raw key from token, or from a file that was wrapped with a
9638313SDina.Nimeh@Sun.Com  * key from token
9648313SDina.Nimeh@Sun.Com  */
9658313SDina.Nimeh@Sun.Com void
getkeyfromtoken(CK_SESSION_HANDLE sess,token_spec_t * token,const char * keyfile,mech_alias_t * cipher,char ** raw_key,size_t * raw_key_sz)9668313SDina.Nimeh@Sun.Com getkeyfromtoken(CK_SESSION_HANDLE sess,
9678313SDina.Nimeh@Sun.Com     token_spec_t *token, const char *keyfile, mech_alias_t *cipher,
9688313SDina.Nimeh@Sun.Com     char **raw_key, size_t *raw_key_sz)
9698313SDina.Nimeh@Sun.Com {
9708313SDina.Nimeh@Sun.Com 	CK_RV	rv = CKR_OK;
9718313SDina.Nimeh@Sun.Com 	CK_BBOOL trueval = B_TRUE;
9728313SDina.Nimeh@Sun.Com 	CK_OBJECT_CLASS kclass;		/* secret key or RSA private key */
9738313SDina.Nimeh@Sun.Com 	CK_KEY_TYPE ktype;		/* from selected cipher or CKK_RSA */
9748313SDina.Nimeh@Sun.Com 	CK_KEY_TYPE raw_ktype;		/* from selected cipher */
9758313SDina.Nimeh@Sun.Com 	CK_ATTRIBUTE	key_tmpl[] = {
9768313SDina.Nimeh@Sun.Com 		{ CKA_CLASS, NULL, 0 },	/* re-used for token key and unwrap */
9778313SDina.Nimeh@Sun.Com 		{ CKA_KEY_TYPE, NULL, 0 },	/* ditto */
9788313SDina.Nimeh@Sun.Com 		{ CKA_LABEL, NULL, 0 },
9798313SDina.Nimeh@Sun.Com 		{ CKA_TOKEN, NULL, 0 },
9808313SDina.Nimeh@Sun.Com 		{ CKA_PRIVATE, NULL, 0 }
9818313SDina.Nimeh@Sun.Com 	    };
9828313SDina.Nimeh@Sun.Com 	CK_ULONG attrs = sizeof (key_tmpl) / sizeof (CK_ATTRIBUTE);
9838313SDina.Nimeh@Sun.Com 	int	i;
9848313SDina.Nimeh@Sun.Com 	char	*pass = NULL;
9858313SDina.Nimeh@Sun.Com 	size_t	passlen = 0;
9868313SDina.Nimeh@Sun.Com 	CK_OBJECT_HANDLE obj, rawobj;
9878313SDina.Nimeh@Sun.Com 	CK_ULONG num_objs = 1;		/* just want to find 1 token key */
9888313SDina.Nimeh@Sun.Com 	CK_MECHANISM unwrap = { CKM_RSA_PKCS, NULL, 0 };
9898313SDina.Nimeh@Sun.Com 	char	*rkey;
9908313SDina.Nimeh@Sun.Com 	size_t	rksz;
9918313SDina.Nimeh@Sun.Com 
9928313SDina.Nimeh@Sun.Com 	if (token == NULL || token->key == NULL)
9938313SDina.Nimeh@Sun.Com 		return;
9948313SDina.Nimeh@Sun.Com 
9958313SDina.Nimeh@Sun.Com 	/* did init_crypto find a slot that supports this cipher? */
9968313SDina.Nimeh@Sun.Com 	if (cipher->slot == (CK_SLOT_ID)-1 || cipher->max_keysize == 0) {
9978313SDina.Nimeh@Sun.Com 		die(gettext("failed to find any cryptographic provider, "
9988313SDina.Nimeh@Sun.Com 		    "use \"cryptoadm list -p\" to find providers: %s\n"),
9998313SDina.Nimeh@Sun.Com 		    pkcs11_strerror(CKR_MECHANISM_INVALID));
10008313SDina.Nimeh@Sun.Com 	}
10018313SDina.Nimeh@Sun.Com 
10028313SDina.Nimeh@Sun.Com 	if (pkcs11_get_pass(token->name, &pass, &passlen, 0, B_FALSE) < 0)
10038313SDina.Nimeh@Sun.Com 		die(gettext("unable to get passphrase"));
10048313SDina.Nimeh@Sun.Com 
10058313SDina.Nimeh@Sun.Com 	/* use passphrase to login to token */
10068313SDina.Nimeh@Sun.Com 	if (pass != NULL && passlen > 0) {
10078313SDina.Nimeh@Sun.Com 		rv = C_Login(sess, CKU_USER, (CK_UTF8CHAR_PTR)pass, passlen);
10088313SDina.Nimeh@Sun.Com 		if (rv != CKR_OK) {
10098313SDina.Nimeh@Sun.Com 			die(gettext("cannot login to the token %s: %s\n"),
10108313SDina.Nimeh@Sun.Com 			    token->name, pkcs11_strerror(rv));
10118313SDina.Nimeh@Sun.Com 		}
10128313SDina.Nimeh@Sun.Com 	}
10138313SDina.Nimeh@Sun.Com 
10148313SDina.Nimeh@Sun.Com 	rv = pkcs11_mech2keytype(cipher->type, &raw_ktype);
10158313SDina.Nimeh@Sun.Com 	if (rv != CKR_OK) {
10168313SDina.Nimeh@Sun.Com 		die(gettext("failed to get key type for cipher %s: %s\n"),
10178313SDina.Nimeh@Sun.Com 		    cipher->name, pkcs11_strerror(rv));
10188313SDina.Nimeh@Sun.Com 	}
10198313SDina.Nimeh@Sun.Com 
10208313SDina.Nimeh@Sun.Com 	/*
10218313SDina.Nimeh@Sun.Com 	 * If no keyfile was given, then the token key is secret key to
10228313SDina.Nimeh@Sun.Com 	 * be used for encryption/decryption.  Otherwise, the keyfile
10238313SDina.Nimeh@Sun.Com 	 * contains a wrapped secret key, and the token is actually the
10248313SDina.Nimeh@Sun.Com 	 * unwrapping RSA private key.
10258313SDina.Nimeh@Sun.Com 	 */
10268313SDina.Nimeh@Sun.Com 	if (keyfile == NULL) {
10278313SDina.Nimeh@Sun.Com 		kclass = CKO_SECRET_KEY;
10288313SDina.Nimeh@Sun.Com 		ktype = raw_ktype;
10298313SDina.Nimeh@Sun.Com 	} else {
10308313SDina.Nimeh@Sun.Com 		kclass = CKO_PRIVATE_KEY;
10318313SDina.Nimeh@Sun.Com 		ktype = CKK_RSA;
10328313SDina.Nimeh@Sun.Com 	}
10338313SDina.Nimeh@Sun.Com 
10348313SDina.Nimeh@Sun.Com 	/* Find the key in the token first */
10358313SDina.Nimeh@Sun.Com 	for (i = 0; i < attrs; i++) {
10368313SDina.Nimeh@Sun.Com 		switch (key_tmpl[i].type) {
10378313SDina.Nimeh@Sun.Com 		case CKA_CLASS:
10388313SDina.Nimeh@Sun.Com 			key_tmpl[i].pValue = &kclass;
10398313SDina.Nimeh@Sun.Com 			key_tmpl[i].ulValueLen = sizeof (kclass);
10408313SDina.Nimeh@Sun.Com 			break;
10418313SDina.Nimeh@Sun.Com 		case CKA_KEY_TYPE:
10428313SDina.Nimeh@Sun.Com 			key_tmpl[i].pValue = &ktype;
10438313SDina.Nimeh@Sun.Com 			key_tmpl[i].ulValueLen = sizeof (ktype);
10448313SDina.Nimeh@Sun.Com 			break;
10458313SDina.Nimeh@Sun.Com 		case CKA_LABEL:
10468313SDina.Nimeh@Sun.Com 			key_tmpl[i].pValue = token->key;
10478313SDina.Nimeh@Sun.Com 			key_tmpl[i].ulValueLen = strlen(token->key);
10488313SDina.Nimeh@Sun.Com 			break;
10498313SDina.Nimeh@Sun.Com 		case CKA_TOKEN:
10508313SDina.Nimeh@Sun.Com 			key_tmpl[i].pValue = &trueval;
10518313SDina.Nimeh@Sun.Com 			key_tmpl[i].ulValueLen = sizeof (trueval);
10528313SDina.Nimeh@Sun.Com 			break;
10538313SDina.Nimeh@Sun.Com 		case CKA_PRIVATE:
10548313SDina.Nimeh@Sun.Com 			key_tmpl[i].pValue = &trueval;
10558313SDina.Nimeh@Sun.Com 			key_tmpl[i].ulValueLen = sizeof (trueval);
10568313SDina.Nimeh@Sun.Com 			break;
10578313SDina.Nimeh@Sun.Com 		default:
10588313SDina.Nimeh@Sun.Com 			break;
10598313SDina.Nimeh@Sun.Com 		}
10608313SDina.Nimeh@Sun.Com 	}
10618313SDina.Nimeh@Sun.Com 	rv = C_FindObjectsInit(sess, key_tmpl, attrs);
10628313SDina.Nimeh@Sun.Com 	if (rv != CKR_OK)
10638313SDina.Nimeh@Sun.Com 		die(gettext("cannot find key %s: %s\n"), token->key,
10648313SDina.Nimeh@Sun.Com 		    pkcs11_strerror(rv));
10658313SDina.Nimeh@Sun.Com 	rv = C_FindObjects(sess, &obj, 1, &num_objs);
10668313SDina.Nimeh@Sun.Com 	(void) C_FindObjectsFinal(sess);
10678313SDina.Nimeh@Sun.Com 
10688313SDina.Nimeh@Sun.Com 	if (num_objs == 0) {
10698313SDina.Nimeh@Sun.Com 		die(gettext("cannot find key %s\n"), token->key);
10708313SDina.Nimeh@Sun.Com 	} else if (rv != CKR_OK) {
10718313SDina.Nimeh@Sun.Com 		die(gettext("cannot find key %s: %s\n"), token->key,
10728313SDina.Nimeh@Sun.Com 		    pkcs11_strerror(rv));
10738313SDina.Nimeh@Sun.Com 	}
10748313SDina.Nimeh@Sun.Com 
10758313SDina.Nimeh@Sun.Com 	/*
10768313SDina.Nimeh@Sun.Com 	 * No keyfile means when token key is found, convert it to raw key,
10778313SDina.Nimeh@Sun.Com 	 * and done.  Otherwise still need do an unwrap to create yet another
10788313SDina.Nimeh@Sun.Com 	 * obj and that needs to be converted to raw key before we're done.
10798313SDina.Nimeh@Sun.Com 	 */
10808313SDina.Nimeh@Sun.Com 	if (keyfile == NULL) {
10818313SDina.Nimeh@Sun.Com 		/* obj contains raw key, extract it */
10828313SDina.Nimeh@Sun.Com 		rv = pkcs11_ObjectToKey(sess, obj, (void **)&rkey, &rksz,
10838313SDina.Nimeh@Sun.Com 		    B_FALSE);
10848313SDina.Nimeh@Sun.Com 		if (rv != CKR_OK) {
10858313SDina.Nimeh@Sun.Com 			die(gettext("failed to get key value for %s"
10868313SDina.Nimeh@Sun.Com 			    " from token %s, %s\n"), token->key,
10878313SDina.Nimeh@Sun.Com 			    token->name, pkcs11_strerror(rv));
10888313SDina.Nimeh@Sun.Com 		}
10898313SDina.Nimeh@Sun.Com 	} else {
10908313SDina.Nimeh@Sun.Com 		getkeyfromfile(keyfile, cipher, &rkey, &rksz);
10918313SDina.Nimeh@Sun.Com 
10928313SDina.Nimeh@Sun.Com 		/*
10938313SDina.Nimeh@Sun.Com 		 * Got the wrapping RSA obj and the wrapped key from file.
10948313SDina.Nimeh@Sun.Com 		 * Unwrap the key from file with RSA obj to get rawkey obj.
10958313SDina.Nimeh@Sun.Com 		 */
10968313SDina.Nimeh@Sun.Com 
10978313SDina.Nimeh@Sun.Com 		/* re-use the first two attributes of key_tmpl */
10988313SDina.Nimeh@Sun.Com 		kclass = CKO_SECRET_KEY;
10998313SDina.Nimeh@Sun.Com 		ktype = raw_ktype;
11008313SDina.Nimeh@Sun.Com 
11018313SDina.Nimeh@Sun.Com 		rv = C_UnwrapKey(sess, &unwrap, obj, (CK_BYTE_PTR)rkey,
11028313SDina.Nimeh@Sun.Com 		    rksz, key_tmpl, 2, &rawobj);
11038313SDina.Nimeh@Sun.Com 		if (rv != CKR_OK) {
11048313SDina.Nimeh@Sun.Com 			die(gettext("failed to unwrap key in keyfile %s,"
11058313SDina.Nimeh@Sun.Com 			    " %s\n"), keyfile, pkcs11_strerror(rv));
11068313SDina.Nimeh@Sun.Com 		}
11078313SDina.Nimeh@Sun.Com 		/* rawobj contains raw key, extract it */
11088313SDina.Nimeh@Sun.Com 		rv = pkcs11_ObjectToKey(sess, rawobj, (void **)&rkey, &rksz,
11098313SDina.Nimeh@Sun.Com 		    B_TRUE);
11108313SDina.Nimeh@Sun.Com 		if (rv != CKR_OK) {
11118313SDina.Nimeh@Sun.Com 			die(gettext("failed to get unwrapped key value for"
11128313SDina.Nimeh@Sun.Com 			    " key in keyfile %s, %s\n"), keyfile,
11138313SDina.Nimeh@Sun.Com 			    pkcs11_strerror(rv));
11148313SDina.Nimeh@Sun.Com 		}
11158313SDina.Nimeh@Sun.Com 	}
11168313SDina.Nimeh@Sun.Com 
11178313SDina.Nimeh@Sun.Com 	/* validate raw key size */
11188313SDina.Nimeh@Sun.Com 	if (rksz < cipher->min_keysize || cipher->max_keysize < rksz) {
11198313SDina.Nimeh@Sun.Com 		warn(gettext("%s: invalid keysize: %d\n"), keyfile, (int)rksz);
11208313SDina.Nimeh@Sun.Com 		die(gettext("\t%d <= keysize <= %d\n"), cipher->min_keysize,
11218313SDina.Nimeh@Sun.Com 		    cipher->max_keysize);
11228313SDina.Nimeh@Sun.Com 	}
11238313SDina.Nimeh@Sun.Com 
11248313SDina.Nimeh@Sun.Com 	*raw_key_sz = rksz;
11258313SDina.Nimeh@Sun.Com 	*raw_key = (char *)rkey;
11268313SDina.Nimeh@Sun.Com }
11278313SDina.Nimeh@Sun.Com 
11288313SDina.Nimeh@Sun.Com /*
11298313SDina.Nimeh@Sun.Com  * Set up cipher key limits and verify PKCS#11 can be done
11308313SDina.Nimeh@Sun.Com  * match_token_cipher is the function pointer used by
11318313SDina.Nimeh@Sun.Com  * pkcs11_GetCriteriaSession() init_crypto.
11328313SDina.Nimeh@Sun.Com  */
11338313SDina.Nimeh@Sun.Com boolean_t
match_token_cipher(CK_SLOT_ID slot_id,void * args,CK_RV * rv)11348313SDina.Nimeh@Sun.Com match_token_cipher(CK_SLOT_ID slot_id, void *args, CK_RV *rv)
11358313SDina.Nimeh@Sun.Com {
11368313SDina.Nimeh@Sun.Com 	token_spec_t *token;
11378313SDina.Nimeh@Sun.Com 	mech_alias_t *cipher;
11388313SDina.Nimeh@Sun.Com 	CK_TOKEN_INFO tokinfo;
11398313SDina.Nimeh@Sun.Com 	CK_MECHANISM_INFO mechinfo;
11408313SDina.Nimeh@Sun.Com 	boolean_t token_match;
11418313SDina.Nimeh@Sun.Com 
11428313SDina.Nimeh@Sun.Com 	/*
11438313SDina.Nimeh@Sun.Com 	 * While traversing slot list, pick up the following info per slot:
11448313SDina.Nimeh@Sun.Com 	 * - if token specified, whether it matches this slot's token info
11458313SDina.Nimeh@Sun.Com 	 * - if the slot supports the PKCS#5 PBKD2 cipher
11468313SDina.Nimeh@Sun.Com 	 *
11478313SDina.Nimeh@Sun.Com 	 * If the user said on the command line
11488313SDina.Nimeh@Sun.Com 	 *	-T tok:mfr:ser:lab -k keyfile
11498313SDina.Nimeh@Sun.Com 	 *	-c cipher -T tok:mfr:ser:lab -k keyfile
11508313SDina.Nimeh@Sun.Com 	 * the given cipher or the default cipher apply to keyfile,
11518313SDina.Nimeh@Sun.Com 	 * If the user said instead
11528313SDina.Nimeh@Sun.Com 	 *	-T tok:mfr:ser:lab
11538313SDina.Nimeh@Sun.Com 	 *	-c cipher -T tok:mfr:ser:lab
11548313SDina.Nimeh@Sun.Com 	 * the key named "lab" may or may not agree with the given
11558313SDina.Nimeh@Sun.Com 	 * cipher or the default cipher.  In those cases, cipher will
11568313SDina.Nimeh@Sun.Com 	 * be overridden with the actual cipher type of the key "lab".
11578313SDina.Nimeh@Sun.Com 	 */
11588313SDina.Nimeh@Sun.Com 	*rv = CKR_FUNCTION_FAILED;
11598313SDina.Nimeh@Sun.Com 
11608313SDina.Nimeh@Sun.Com 	if (args == NULL) {
11618313SDina.Nimeh@Sun.Com 		return (B_FALSE);
11628313SDina.Nimeh@Sun.Com 	}
11638313SDina.Nimeh@Sun.Com 
11648313SDina.Nimeh@Sun.Com 	cipher = (mech_alias_t *)args;
11658313SDina.Nimeh@Sun.Com 	token = cipher->token;
11668313SDina.Nimeh@Sun.Com 
11678313SDina.Nimeh@Sun.Com 	if (C_GetMechanismInfo(slot_id, cipher->type, &mechinfo) != CKR_OK) {
11688313SDina.Nimeh@Sun.Com 		return (B_FALSE);
11698313SDina.Nimeh@Sun.Com 	}
11708313SDina.Nimeh@Sun.Com 
11718313SDina.Nimeh@Sun.Com 	if (token == NULL) {
11728313SDina.Nimeh@Sun.Com 		if (C_GetMechanismInfo(slot_id, CKM_PKCS5_PBKD2, &mechinfo) !=
11738313SDina.Nimeh@Sun.Com 		    CKR_OK) {
11748313SDina.Nimeh@Sun.Com 			return (B_FALSE);
11758313SDina.Nimeh@Sun.Com 		}
11768313SDina.Nimeh@Sun.Com 		goto foundit;
11778313SDina.Nimeh@Sun.Com 	}
11788313SDina.Nimeh@Sun.Com 
11798313SDina.Nimeh@Sun.Com 	/* does the token match the token spec? */
11808313SDina.Nimeh@Sun.Com 	if (token->key == NULL || (C_GetTokenInfo(slot_id, &tokinfo) != CKR_OK))
11818313SDina.Nimeh@Sun.Com 		return (B_FALSE);
11828313SDina.Nimeh@Sun.Com 
11838313SDina.Nimeh@Sun.Com 	token_match = B_TRUE;
11848313SDina.Nimeh@Sun.Com 
11858313SDina.Nimeh@Sun.Com 	if (token->name != NULL && (token->name)[0] != '\0' &&
11868313SDina.Nimeh@Sun.Com 	    strncmp((char *)token->name, (char *)tokinfo.label,
11878313SDina.Nimeh@Sun.Com 	    TOKEN_LABEL_SIZE) != 0)
11888313SDina.Nimeh@Sun.Com 		token_match = B_FALSE;
11898313SDina.Nimeh@Sun.Com 	if (token->mfr != NULL && (token->mfr)[0] != '\0' &&
11908313SDina.Nimeh@Sun.Com 	    strncmp((char *)token->mfr, (char *)tokinfo.manufacturerID,
11918313SDina.Nimeh@Sun.Com 	    TOKEN_MANUFACTURER_SIZE) != 0)
11928313SDina.Nimeh@Sun.Com 		token_match = B_FALSE;
11938313SDina.Nimeh@Sun.Com 	if (token->serno != NULL && (token->serno)[0] != '\0' &&
11948313SDina.Nimeh@Sun.Com 	    strncmp((char *)token->serno, (char *)tokinfo.serialNumber,
11958313SDina.Nimeh@Sun.Com 	    TOKEN_SERIAL_SIZE) != 0)
11968313SDina.Nimeh@Sun.Com 		token_match = B_FALSE;
11978313SDina.Nimeh@Sun.Com 
11988313SDina.Nimeh@Sun.Com 	if (!token_match)
11998313SDina.Nimeh@Sun.Com 		return (B_FALSE);
12008313SDina.Nimeh@Sun.Com 
12018313SDina.Nimeh@Sun.Com foundit:
12028313SDina.Nimeh@Sun.Com 	cipher->slot = slot_id;
12038313SDina.Nimeh@Sun.Com 	return (B_TRUE);
12048313SDina.Nimeh@Sun.Com }
12058313SDina.Nimeh@Sun.Com 
12068313SDina.Nimeh@Sun.Com /*
12078313SDina.Nimeh@Sun.Com  * Clean up crypto loose ends
12088313SDina.Nimeh@Sun.Com  */
12098313SDina.Nimeh@Sun.Com static void
end_crypto(CK_SESSION_HANDLE sess)12108313SDina.Nimeh@Sun.Com end_crypto(CK_SESSION_HANDLE sess)
12118313SDina.Nimeh@Sun.Com {
12128313SDina.Nimeh@Sun.Com 	(void) C_CloseSession(sess);
12138313SDina.Nimeh@Sun.Com 	(void) C_Finalize(NULL);
12148313SDina.Nimeh@Sun.Com }
12158313SDina.Nimeh@Sun.Com 
12168313SDina.Nimeh@Sun.Com /*
12178313SDina.Nimeh@Sun.Com  * Set up crypto, opening session on slot that matches token and cipher
12188313SDina.Nimeh@Sun.Com  */
12198313SDina.Nimeh@Sun.Com static void
init_crypto(token_spec_t * token,mech_alias_t * cipher,CK_SESSION_HANDLE_PTR sess)12208313SDina.Nimeh@Sun.Com init_crypto(token_spec_t *token, mech_alias_t *cipher,
12218313SDina.Nimeh@Sun.Com     CK_SESSION_HANDLE_PTR sess)
12228313SDina.Nimeh@Sun.Com {
12238313SDina.Nimeh@Sun.Com 	CK_RV	rv;
12248313SDina.Nimeh@Sun.Com 
12258313SDina.Nimeh@Sun.Com 	cipher->token = token;
12268313SDina.Nimeh@Sun.Com 
12278313SDina.Nimeh@Sun.Com 	/* Turn off Metaslot so that we can see actual tokens */
12288313SDina.Nimeh@Sun.Com 	if (setenv("METASLOT_ENABLED", "false", 1) < 0) {
12298313SDina.Nimeh@Sun.Com 		die(gettext("could not disable Metaslot"));
12308313SDina.Nimeh@Sun.Com 	}
12318313SDina.Nimeh@Sun.Com 
12328313SDina.Nimeh@Sun.Com 	rv = pkcs11_GetCriteriaSession(match_token_cipher, (void *)cipher,
12338313SDina.Nimeh@Sun.Com 	    sess);
12348313SDina.Nimeh@Sun.Com 	if (rv != CKR_OK) {
12358313SDina.Nimeh@Sun.Com 		end_crypto(*sess);
12368313SDina.Nimeh@Sun.Com 		if (rv == CKR_HOST_MEMORY) {
12378313SDina.Nimeh@Sun.Com 			die("malloc");
12388313SDina.Nimeh@Sun.Com 		}
12398313SDina.Nimeh@Sun.Com 		die(gettext("failed to find any cryptographic provider, "
12408313SDina.Nimeh@Sun.Com 		    "use \"cryptoadm list -p\" to find providers: %s\n"),
12418313SDina.Nimeh@Sun.Com 		    pkcs11_strerror(rv));
12428313SDina.Nimeh@Sun.Com 	}
12438313SDina.Nimeh@Sun.Com }
12448313SDina.Nimeh@Sun.Com 
12458313SDina.Nimeh@Sun.Com /*
12465643Saalok  * Uncompress a file.
12475643Saalok  *
12485643Saalok  * First map the file in to establish a device
12495643Saalok  * association, then read from it. On-the-fly
12505643Saalok  * decompression will automatically uncompress
12515643Saalok  * the file if it's compressed
12525643Saalok  *
12535643Saalok  * If the file is mapped and a device association
12545643Saalok  * has been established, disallow uncompressing
12555643Saalok  * the file until it is unmapped.
12565643Saalok  */
12575643Saalok static void
lofi_uncompress(int lfd,const char * filename)12585643Saalok lofi_uncompress(int lfd, const char *filename)
12595643Saalok {
12605643Saalok 	struct lofi_ioctl li;
12615643Saalok 	char buf[MAXBSIZE];
12625643Saalok 	char devicename[32];
12635643Saalok 	char tmpfilename[MAXPATHLEN];
12648313SDina.Nimeh@Sun.Com 	char *x;
12655643Saalok 	char *dir = NULL;
12665643Saalok 	char *file = NULL;
12675643Saalok 	int minor = 0;
12685643Saalok 	struct stat64 statbuf;
12695643Saalok 	int compfd = -1;
12705643Saalok 	int uncompfd = -1;
12715643Saalok 	ssize_t rbytes;
12725643Saalok 
12735643Saalok 	/*
12745643Saalok 	 * Disallow uncompressing the file if it is
12755643Saalok 	 * already mapped.
12765643Saalok 	 */
12775643Saalok 	li.li_minor = 0;
12785643Saalok 	(void) strlcpy(li.li_filename, filename, sizeof (li.li_filename));
12795643Saalok 	if (ioctl(lfd, LOFI_GET_MINOR, &li) != -1)
12805643Saalok 		die(gettext("%s must be unmapped before uncompressing"),
12815643Saalok 		    filename);
12825643Saalok 
12835643Saalok 	/* Zero length files don't need to be uncompressed */
12845643Saalok 	if (stat64(filename, &statbuf) == -1)
12855643Saalok 		die(gettext("stat: %s"), filename);
12865643Saalok 	if (statbuf.st_size == 0)
12875643Saalok 		return;
12885643Saalok 
12898313SDina.Nimeh@Sun.Com 	minor = lofi_map_file(lfd, li, filename);
12905643Saalok 	(void) snprintf(devicename, sizeof (devicename), "/dev/%s/%d",
12915643Saalok 	    LOFI_BLOCK_NAME, minor);
12925643Saalok 
12935643Saalok 	/* If the file isn't compressed, we just return */
12945643Saalok 	if ((ioctl(lfd, LOFI_CHECK_COMPRESSED, &li) == -1) ||
12956791Saalok 	    (li.li_algorithm[0] == '\0')) {
12965643Saalok 		delete_mapping(lfd, devicename, filename, B_TRUE);
12976791Saalok 		die("%s is not compressed\n", filename);
12985643Saalok 	}
12995643Saalok 
13005643Saalok 	if ((compfd = open64(devicename, O_RDONLY | O_NONBLOCK)) == -1) {
13015643Saalok 		delete_mapping(lfd, devicename, filename, B_TRUE);
13025643Saalok 		die(gettext("open: %s"), filename);
13035643Saalok 	}
13045643Saalok 	/* Create a temp file in the same directory */
13058313SDina.Nimeh@Sun.Com 	x = strdup(filename);
13068313SDina.Nimeh@Sun.Com 	dir = strdup(dirname(x));
13078313SDina.Nimeh@Sun.Com 	free(x);
13088313SDina.Nimeh@Sun.Com 	x = strdup(filename);
13098313SDina.Nimeh@Sun.Com 	file = strdup(basename(x));
13108313SDina.Nimeh@Sun.Com 	free(x);
13115643Saalok 	(void) snprintf(tmpfilename, sizeof (tmpfilename),
13125643Saalok 	    "%s/.%sXXXXXX", dir, file);
13138313SDina.Nimeh@Sun.Com 	free(dir);
13148313SDina.Nimeh@Sun.Com 	free(file);
13155643Saalok 
13165643Saalok 	if ((uncompfd = mkstemp64(tmpfilename)) == -1) {
13175643Saalok 		(void) close(compfd);
13185643Saalok 		delete_mapping(lfd, devicename, filename, B_TRUE);
13196791Saalok 		die("%s could not be uncompressed\n", filename);
13205643Saalok 	}
13215643Saalok 
13225643Saalok 	/*
13235643Saalok 	 * Set the mode bits and the owner of this temporary
13245643Saalok 	 * file to be that of the original uncompressed file
13255643Saalok 	 */
13265643Saalok 	(void) fchmod(uncompfd, statbuf.st_mode);
13275643Saalok 
13285643Saalok 	if (fchown(uncompfd, statbuf.st_uid, statbuf.st_gid) == -1) {
13295643Saalok 		(void) close(compfd);
13305643Saalok 		(void) close(uncompfd);
13315643Saalok 		delete_mapping(lfd, devicename, filename, B_TRUE);
13326791Saalok 		die("%s could not be uncompressed\n", filename);
13335643Saalok 	}
13345643Saalok 
13355643Saalok 	/* Now read from the device in MAXBSIZE-sized chunks */
13365643Saalok 	for (;;) {
13375643Saalok 		rbytes = read(compfd, buf, sizeof (buf));
13385643Saalok 
13395643Saalok 		if (rbytes <= 0)
13405643Saalok 			break;
13415643Saalok 
13425643Saalok 		if (write(uncompfd, buf, rbytes) != rbytes) {
13435643Saalok 			rbytes = -1;
13445643Saalok 			break;
13455643Saalok 		}
13465643Saalok 	}
13475643Saalok 
13485643Saalok 	(void) close(compfd);
13495643Saalok 	(void) close(uncompfd);
13505643Saalok 
13515643Saalok 	/* Delete the mapping */
13525643Saalok 	delete_mapping(lfd, devicename, filename, B_TRUE);
13535643Saalok 
13545643Saalok 	/*
13555643Saalok 	 * If an error occured while reading or writing, rbytes will
13565643Saalok 	 * be negative
13575643Saalok 	 */
13585643Saalok 	if (rbytes < 0) {
13595643Saalok 		(void) unlink(tmpfilename);
13605643Saalok 		die(gettext("could not read from %s"), filename);
13615643Saalok 	}
13625643Saalok 
13635643Saalok 	/* Rename the temp file to the actual file */
13645643Saalok 	if (rename(tmpfilename, filename) == -1)
13655643Saalok 		(void) unlink(tmpfilename);
13665643Saalok }
13675643Saalok 
13685643Saalok /*
13695643Saalok  * Compress a file
13705643Saalok  */
13715643Saalok static void
lofi_compress(int * lfd,const char * filename,int compress_index,uint32_t segsize)13726926Saalok lofi_compress(int *lfd, const char *filename, int compress_index,
13735643Saalok     uint32_t segsize)
13745643Saalok {
13755643Saalok 	struct lofi_ioctl lic;
13765643Saalok 	lofi_compress_info_t *li;
13776926Saalok 	struct flock lock;
13785643Saalok 	char tmpfilename[MAXPATHLEN];
13795643Saalok 	char comp_filename[MAXPATHLEN];
13805643Saalok 	char algorithm[MAXALGLEN];
13818313SDina.Nimeh@Sun.Com 	char *x;
13825643Saalok 	char *dir = NULL, *file = NULL;
13835643Saalok 	uchar_t *uncompressed_seg = NULL;
13845643Saalok 	uchar_t *compressed_seg = NULL;
13855643Saalok 	uint32_t compressed_segsize;
13865643Saalok 	uint32_t len_compressed, count;
13875643Saalok 	uint32_t index_entries, index_sz;
13885643Saalok 	uint64_t *index = NULL;
13895643Saalok 	uint64_t offset;
13905643Saalok 	size_t real_segsize;
13915643Saalok 	struct stat64 statbuf;
13925643Saalok 	int compfd = -1, uncompfd = -1;
13935643Saalok 	int tfd = -1;
13945643Saalok 	ssize_t rbytes, wbytes, lastread;
13955643Saalok 	int i, type;
13965643Saalok 
13975643Saalok 	/*
13985643Saalok 	 * Disallow compressing the file if it is
13995643Saalok 	 * already mapped
14005643Saalok 	 */
14015643Saalok 	lic.li_minor = 0;
14025643Saalok 	(void) strlcpy(lic.li_filename, filename, sizeof (lic.li_filename));
14036926Saalok 	if (ioctl(*lfd, LOFI_GET_MINOR, &lic) != -1)
14045643Saalok 		die(gettext("%s must be unmapped before compressing"),
14055643Saalok 		    filename);
14065643Saalok 
14076926Saalok 	/*
14086926Saalok 	 * Close the control device so other operations
14096926Saalok 	 * can use it
14106926Saalok 	 */
14116926Saalok 	(void) close(*lfd);
14126926Saalok 	*lfd = -1;
14136926Saalok 
14145643Saalok 	li = &lofi_compress_table[compress_index];
14155643Saalok 
14165643Saalok 	/*
14175643Saalok 	 * The size of the buffer to hold compressed data must
14185643Saalok 	 * be slightly larger than the compressed segment size.
14195643Saalok 	 *
14205643Saalok 	 * The compress functions use part of the buffer as
14215643Saalok 	 * scratch space to do calculations.
14225643Saalok 	 * Ref: http://www.zlib.net/manual.html#compress2
14235643Saalok 	 */
14245643Saalok 	compressed_segsize = segsize + (segsize >> 6);
14255643Saalok 	compressed_seg = (uchar_t *)malloc(compressed_segsize + SEGHDR);
14265643Saalok 	uncompressed_seg = (uchar_t *)malloc(segsize);
14275643Saalok 
14285643Saalok 	if (compressed_seg == NULL || uncompressed_seg == NULL)
14295643Saalok 		die(gettext("No memory"));
14305643Saalok 
14316926Saalok 	if ((uncompfd = open64(filename, O_RDWR|O_LARGEFILE, 0)) == -1)
14325643Saalok 		die(gettext("open: %s"), filename);
14335643Saalok 
14346926Saalok 	lock.l_type = F_WRLCK;
14356926Saalok 	lock.l_whence = SEEK_SET;
14366926Saalok 	lock.l_start = 0;
14376926Saalok 	lock.l_len = 0;
14386926Saalok 
14396926Saalok 	/*
14406926Saalok 	 * Use an advisory lock to ensure that only a
14416926Saalok 	 * single lofiadm process compresses a given
14426926Saalok 	 * file at any given time
14436926Saalok 	 *
14446926Saalok 	 * A close on the file descriptor automatically
14456926Saalok 	 * closes all lock state on the file
14466926Saalok 	 */
14476926Saalok 	if (fcntl(uncompfd, F_SETLKW, &lock) == -1)
14486926Saalok 		die(gettext("fcntl: %s"), filename);
14496926Saalok 
14505643Saalok 	if (fstat64(uncompfd, &statbuf) == -1) {
14515643Saalok 		(void) close(uncompfd);
14525643Saalok 		die(gettext("fstat: %s"), filename);
14535643Saalok 	}
14545643Saalok 
14555643Saalok 	/* Zero length files don't need to be compressed */
14565643Saalok 	if (statbuf.st_size == 0) {
14575643Saalok 		(void) close(uncompfd);
14585643Saalok 		return;
14595643Saalok 	}
14605643Saalok 
14615643Saalok 	/*
14625643Saalok 	 * Create temporary files in the same directory that
14635643Saalok 	 * will hold the intermediate data
14645643Saalok 	 */
14658313SDina.Nimeh@Sun.Com 	x = strdup(filename);
14668313SDina.Nimeh@Sun.Com 	dir = strdup(dirname(x));
14678313SDina.Nimeh@Sun.Com 	free(x);
14688313SDina.Nimeh@Sun.Com 	x = strdup(filename);
14698313SDina.Nimeh@Sun.Com 	file = strdup(basename(x));
14708313SDina.Nimeh@Sun.Com 	free(x);
14715643Saalok 	(void) snprintf(tmpfilename, sizeof (tmpfilename),
14725643Saalok 	    "%s/.%sXXXXXX", dir, file);
14735643Saalok 	(void) snprintf(comp_filename, sizeof (comp_filename),
14745643Saalok 	    "%s/.%sXXXXXX", dir, file);
14758313SDina.Nimeh@Sun.Com 	free(dir);
14768313SDina.Nimeh@Sun.Com 	free(file);
14775643Saalok 
14785643Saalok 	if ((tfd = mkstemp64(tmpfilename)) == -1)
14795643Saalok 		goto cleanup;
14805643Saalok 
14815643Saalok 	if ((compfd = mkstemp64(comp_filename)) == -1)
14825643Saalok 		goto cleanup;
14835643Saalok 
14845643Saalok 	/*
14855643Saalok 	 * Set the mode bits and owner of the compressed
14865643Saalok 	 * file to be that of the original uncompressed file
14875643Saalok 	 */
14885643Saalok 	(void) fchmod(compfd, statbuf.st_mode);
14895643Saalok 
14905643Saalok 	if (fchown(compfd, statbuf.st_uid, statbuf.st_gid) == -1)
14915643Saalok 		goto cleanup;
14925643Saalok 
14935643Saalok 	/*
14945643Saalok 	 * Calculate the number of index entries required.
14955643Saalok 	 * index entries are stored as an array. adding
14965643Saalok 	 * a '2' here accounts for the fact that the last
14975643Saalok 	 * segment may not be a multiple of the segment size
14985643Saalok 	 */
14995643Saalok 	index_sz = (statbuf.st_size / segsize) + 2;
15005643Saalok 	index = malloc(sizeof (*index) * index_sz);
15015643Saalok 
15025643Saalok 	if (index == NULL)
15035643Saalok 		goto cleanup;
15045643Saalok 
15055643Saalok 	offset = 0;
15065643Saalok 	lastread = segsize;
15075643Saalok 	count = 0;
15085643Saalok 
15095643Saalok 	/*
15105643Saalok 	 * Now read from the uncompressed file in 'segsize'
15115643Saalok 	 * sized chunks, compress what was read in and
15125643Saalok 	 * write it out to a temporary file
15135643Saalok 	 */
15145643Saalok 	for (;;) {
15155643Saalok 		rbytes = read(uncompfd, uncompressed_seg, segsize);
15165643Saalok 
15175643Saalok 		if (rbytes <= 0)
15185643Saalok 			break;
15195643Saalok 
15205643Saalok 		if (lastread < segsize)
15215643Saalok 			goto cleanup;
15225643Saalok 
15235643Saalok 		/*
15245643Saalok 		 * Account for the first byte that
15255643Saalok 		 * indicates whether a segment is
15265643Saalok 		 * compressed or not
15275643Saalok 		 */
15285643Saalok 		real_segsize = segsize - 1;
15295643Saalok 		(void) li->l_compress(uncompressed_seg, rbytes,
15305643Saalok 		    compressed_seg + SEGHDR, &real_segsize, li->l_level);
15315643Saalok 
15325643Saalok 		/*
15335643Saalok 		 * If the length of the compressed data is more
15345643Saalok 		 * than a threshold then there isn't any benefit
15355643Saalok 		 * to be had from compressing this segment - leave
15365643Saalok 		 * it uncompressed.
15375643Saalok 		 *
15385643Saalok 		 * NB. In case an error occurs during compression (above)
15395643Saalok 		 * the 'real_segsize' isn't changed. The logic below
15405643Saalok 		 * ensures that that segment is left uncompressed.
15415643Saalok 		 */
15425643Saalok 		len_compressed = real_segsize;
15439048Sjrgn.keil@googlemail.com 		if (segsize <= COMPRESS_THRESHOLD ||
15449048Sjrgn.keil@googlemail.com 		    real_segsize > (segsize - COMPRESS_THRESHOLD)) {
15455643Saalok 			(void) memcpy(compressed_seg + SEGHDR, uncompressed_seg,
15465643Saalok 			    rbytes);
15475643Saalok 			type = UNCOMPRESSED;
15485643Saalok 			len_compressed = rbytes;
15495643Saalok 		} else {
15505643Saalok 			type = COMPRESSED;
15515643Saalok 		}
15525643Saalok 
15535643Saalok 		/*
15545643Saalok 		 * Set the first byte or the SEGHDR to
15555643Saalok 		 * indicate if it's compressed or not
15565643Saalok 		 */
15575643Saalok 		*compressed_seg = type;
15585643Saalok 		wbytes = write(tfd, compressed_seg, len_compressed + SEGHDR);
15595643Saalok 		if (wbytes != (len_compressed + SEGHDR)) {
15605643Saalok 			rbytes = -1;
15615643Saalok 			break;
15625643Saalok 		}
15635643Saalok 
15645643Saalok 		index[count] = BE_64(offset);
15655643Saalok 		offset += wbytes;
15665643Saalok 		lastread = rbytes;
15675643Saalok 		count++;
15685643Saalok 	}
15695643Saalok 
15705643Saalok 	(void) close(uncompfd);
15715643Saalok 
15725643Saalok 	if (rbytes < 0)
15735643Saalok 		goto cleanup;
15745643Saalok 	/*
15755643Saalok 	 * The last index entry is a sentinel entry. It does not point to
15765643Saalok 	 * an actual compressed segment but helps in computing the size of
15775643Saalok 	 * the compressed segment. The size of each compressed segment is
15785643Saalok 	 * computed by subtracting the current index value from the next
15795643Saalok 	 * one (the compressed blocks are stored sequentially)
15805643Saalok 	 */
15815643Saalok 	index[count++] = BE_64(offset);
15825643Saalok 
15835643Saalok 	/*
15845643Saalok 	 * Now write the compressed data along with the
15855643Saalok 	 * header information to this file which will
15865643Saalok 	 * later be renamed to the original uncompressed
15875643Saalok 	 * file name
15885643Saalok 	 *
15895643Saalok 	 * The header is as follows -
15905643Saalok 	 *
15915643Saalok 	 * Signature (name of the compression algorithm)
15925643Saalok 	 * Compression segment size (a multiple of 512)
15935643Saalok 	 * Number of index entries
15945643Saalok 	 * Size of the last block
15955643Saalok 	 * The array containing the index entries
15965643Saalok 	 *
15975643Saalok 	 * the header is always stored in network byte
15985643Saalok 	 * order
15995643Saalok 	 */
16005643Saalok 	(void) bzero(algorithm, sizeof (algorithm));
16015643Saalok 	(void) strlcpy(algorithm, li->l_name, sizeof (algorithm));
16025643Saalok 	if (write(compfd, algorithm, sizeof (algorithm))
16035643Saalok 	    != sizeof (algorithm))
16045643Saalok 		goto cleanup;
16055643Saalok 
16065643Saalok 	segsize = htonl(segsize);
16075643Saalok 	if (write(compfd, &segsize, sizeof (segsize)) != sizeof (segsize))
16085643Saalok 		goto cleanup;
16095643Saalok 
16105643Saalok 	index_entries = htonl(count);
16115643Saalok 	if (write(compfd, &index_entries, sizeof (index_entries)) !=
16125643Saalok 	    sizeof (index_entries))
16135643Saalok 		goto cleanup;
16145643Saalok 
16155643Saalok 	lastread = htonl(lastread);
16165643Saalok 	if (write(compfd, &lastread, sizeof (lastread)) != sizeof (lastread))
16175643Saalok 		goto cleanup;
16185643Saalok 
16195643Saalok 	for (i = 0; i < count; i++) {
16205643Saalok 		if (write(compfd, index + i, sizeof (*index)) !=
16215643Saalok 		    sizeof (*index))
16225643Saalok 			goto cleanup;
16235643Saalok 	}
16245643Saalok 
16255643Saalok 	/* Header is written, now write the compressed data */
16265643Saalok 	if (lseek(tfd, 0, SEEK_SET) != 0)
16275643Saalok 		goto cleanup;
16285643Saalok 
16295643Saalok 	rbytes = wbytes = 0;
16305643Saalok 
16315643Saalok 	for (;;) {
16325643Saalok 		rbytes = read(tfd, compressed_seg, compressed_segsize + SEGHDR);
16335643Saalok 
16345643Saalok 		if (rbytes <= 0)
16355643Saalok 			break;
16365643Saalok 
16375643Saalok 		if (write(compfd, compressed_seg, rbytes) != rbytes)
16385643Saalok 			goto cleanup;
16395643Saalok 	}
16405643Saalok 
16415643Saalok 	if (fstat64(compfd, &statbuf) == -1)
16425643Saalok 		goto cleanup;
16435643Saalok 
16445643Saalok 	/*
16455643Saalok 	 * Round up the compressed file size to be a multiple of
16465643Saalok 	 * DEV_BSIZE. lofi(7D) likes it that way.
16475643Saalok 	 */
16485643Saalok 	if ((offset = statbuf.st_size % DEV_BSIZE) > 0) {
16495643Saalok 
16505643Saalok 		offset = DEV_BSIZE - offset;
16515643Saalok 
16525643Saalok 		for (i = 0; i < offset; i++)
16535643Saalok 			uncompressed_seg[i] = '\0';
16545643Saalok 		if (write(compfd, uncompressed_seg, offset) != offset)
16555643Saalok 			goto cleanup;
16565643Saalok 	}
16575643Saalok 	(void) close(compfd);
16585643Saalok 	(void) close(tfd);
16595643Saalok 	(void) unlink(tmpfilename);
16605643Saalok cleanup:
16615643Saalok 	if (rbytes < 0) {
16625643Saalok 		if (tfd != -1)
16635643Saalok 			(void) unlink(tmpfilename);
16645643Saalok 		if (compfd != -1)
16655643Saalok 			(void) unlink(comp_filename);
16665643Saalok 		die(gettext("error compressing file %s"), filename);
16675643Saalok 	} else {
16685643Saalok 		/* Rename the compressed file to the actual file */
16695643Saalok 		if (rename(comp_filename, filename) == -1) {
16705643Saalok 			(void) unlink(comp_filename);
16715643Saalok 			die(gettext("error compressing file %s"), filename);
16725643Saalok 		}
16735643Saalok 	}
16745643Saalok 	if (compressed_seg != NULL)
16755643Saalok 		free(compressed_seg);
16765643Saalok 	if (uncompressed_seg != NULL)
16775643Saalok 		free(uncompressed_seg);
16785643Saalok 	if (index != NULL)
16795643Saalok 		free(index);
16805643Saalok 	if (compfd != -1)
16815643Saalok 		(void) close(compfd);
16825643Saalok 	if (uncompfd != -1)
16835643Saalok 		(void) close(uncompfd);
16845643Saalok 	if (tfd != -1)
16855643Saalok 		(void) close(tfd);
16865643Saalok }
16875643Saalok 
16885643Saalok static int
lofi_compress_select(const char * algname)16895643Saalok lofi_compress_select(const char *algname)
16905643Saalok {
16915643Saalok 	int i;
16925643Saalok 
16935643Saalok 	for (i = 0; i < LOFI_COMPRESS_FUNCTIONS; i++) {
16945643Saalok 		if (strcmp(lofi_compress_table[i].l_name, algname) == 0)
16955643Saalok 			return (i);
16965643Saalok 	}
16975643Saalok 	return (-1);
16985643Saalok }
16995643Saalok 
17005643Saalok static void
check_algorithm_validity(const char * algname,int * compress_index)17015643Saalok check_algorithm_validity(const char *algname, int *compress_index)
17025643Saalok {
17035643Saalok 	*compress_index = lofi_compress_select(algname);
17045643Saalok 	if (*compress_index < 0)
17055643Saalok 		die(gettext("invalid algorithm name: %s\n"), algname);
17065643Saalok }
17075643Saalok 
17085643Saalok static void
check_file_validity(const char * filename)17095643Saalok check_file_validity(const char *filename)
17105643Saalok {
17115643Saalok 	struct stat64 buf;
17125643Saalok 	int 	error;
17138313SDina.Nimeh@Sun.Com 	int	fd;
17145643Saalok 
17155643Saalok 	fd = open64(filename, O_RDONLY);
17165643Saalok 	if (fd == -1) {
17175643Saalok 		die(gettext("open: %s"), filename);
17185643Saalok 	}
17195643Saalok 	error = fstat64(fd, &buf);
17205643Saalok 	if (error == -1) {
17215643Saalok 		die(gettext("fstat: %s"), filename);
17225643Saalok 	} else if (!S_ISLOFIABLE(buf.st_mode)) {
17235643Saalok 		die(gettext("%s is not a regular file, "
17245643Saalok 		    "block, or character device\n"),
17255643Saalok 		    filename);
17265643Saalok 	} else if ((buf.st_size % DEV_BSIZE) != 0) {
17278069SDina.Nimeh@Sun.Com 		die(gettext("size of %s is not a multiple of %d\n"),
17285643Saalok 		    filename, DEV_BSIZE);
17295643Saalok 	}
17305643Saalok 	(void) close(fd);
17315643Saalok 
17325643Saalok 	if (name_to_minor(filename) != 0) {
17338069SDina.Nimeh@Sun.Com 		die(gettext("cannot use %s on itself\n"), LOFI_DRIVER_NAME);
17345643Saalok 	}
17355643Saalok }
17365643Saalok 
17375643Saalok static uint32_t
convert_to_num(const char * str)17385643Saalok convert_to_num(const char *str)
17395643Saalok {
17405643Saalok 	int len;
17415643Saalok 	uint32_t segsize, mult = 1;
17425643Saalok 
17435643Saalok 	len = strlen(str);
17445643Saalok 	if (len && isalpha(str[len - 1])) {
17455643Saalok 		switch (str[len - 1]) {
17465643Saalok 		case 'k':
17475643Saalok 		case 'K':
17485643Saalok 			mult = KILOBYTE;
17495643Saalok 			break;
17505643Saalok 		case 'b':
17515643Saalok 		case 'B':
17525643Saalok 			mult = BLOCK_SIZE;
17535643Saalok 			break;
17545643Saalok 		case 'm':
17555643Saalok 		case 'M':
17565643Saalok 			mult = MEGABYTE;
17575643Saalok 			break;
17585643Saalok 		case 'g':
17595643Saalok 		case 'G':
17605643Saalok 			mult = GIGABYTE;
17615643Saalok 			break;
17625643Saalok 		default:
17635643Saalok 			die(gettext("invalid segment size %s\n"), str);
17645643Saalok 		}
17655643Saalok 	}
17665643Saalok 
17675643Saalok 	segsize = atol(str);
17685643Saalok 	segsize *= mult;
17695643Saalok 
17705643Saalok 	return (segsize);
17715643Saalok }
17725643Saalok 
17730Sstevel@tonic-gate int
main(int argc,char * argv[])17740Sstevel@tonic-gate main(int argc, char *argv[])
17750Sstevel@tonic-gate {
17760Sstevel@tonic-gate 	int	lfd;
17770Sstevel@tonic-gate 	int	c;
17780Sstevel@tonic-gate 	const char *devicename = NULL;
17790Sstevel@tonic-gate 	const char *filename = NULL;
17805643Saalok 	const char *algname = COMPRESS_ALGORITHM;
17810Sstevel@tonic-gate 	int	openflag;
17820Sstevel@tonic-gate 	int	minor;
17835643Saalok 	int 	compress_index;
17845643Saalok 	uint32_t segsize = SEGSIZE;
17850Sstevel@tonic-gate 	static char *lofictl = "/dev/" LOFI_CTL_NAME;
17864451Seschrock 	boolean_t force = B_FALSE;
17878313SDina.Nimeh@Sun.Com 	const char *pname;
17888313SDina.Nimeh@Sun.Com 	boolean_t errflag = B_FALSE;
17898313SDina.Nimeh@Sun.Com 	boolean_t addflag = B_FALSE;
17908313SDina.Nimeh@Sun.Com 	boolean_t deleteflag = B_FALSE;
17918313SDina.Nimeh@Sun.Com 	boolean_t ephflag = B_FALSE;
17928313SDina.Nimeh@Sun.Com 	boolean_t compressflag = B_FALSE;
17938313SDina.Nimeh@Sun.Com 	boolean_t uncompressflag = B_FALSE;
17948313SDina.Nimeh@Sun.Com 	/* the next two work together for -c, -k, -T, -e options only */
17958313SDina.Nimeh@Sun.Com 	boolean_t need_crypto = B_FALSE;	/* if any -c, -k, -T, -e */
17968313SDina.Nimeh@Sun.Com 	boolean_t cipher_only = B_TRUE;		/* if -c only */
17978313SDina.Nimeh@Sun.Com 	const char *keyfile = NULL;
17988313SDina.Nimeh@Sun.Com 	mech_alias_t *cipher = NULL;
17998313SDina.Nimeh@Sun.Com 	token_spec_t *token = NULL;
18008313SDina.Nimeh@Sun.Com 	char	*rkey = NULL;
18018313SDina.Nimeh@Sun.Com 	size_t	rksz = 0;
18028313SDina.Nimeh@Sun.Com 	char realfilename[MAXPATHLEN];
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate 	pname = getpname(argv[0]);
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
18070Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
18080Sstevel@tonic-gate 
18098313SDina.Nimeh@Sun.Com 	while ((c = getopt(argc, argv, "a:c:Cd:efk:o:s:T:U")) != EOF) {
18100Sstevel@tonic-gate 		switch (c) {
18110Sstevel@tonic-gate 		case 'a':
18128313SDina.Nimeh@Sun.Com 			addflag = B_TRUE;
18138069SDina.Nimeh@Sun.Com 			if ((filename = realpath(optarg, realfilename)) == NULL)
18148069SDina.Nimeh@Sun.Com 				die("%s", optarg);
18150Sstevel@tonic-gate 			if (((argc - optind) > 0) && (*argv[optind] != '-')) {
18160Sstevel@tonic-gate 				/* optional device */
18170Sstevel@tonic-gate 				devicename = argv[optind];
18180Sstevel@tonic-gate 				optind++;
18190Sstevel@tonic-gate 			}
18200Sstevel@tonic-gate 			break;
18215643Saalok 		case 'C':
18228313SDina.Nimeh@Sun.Com 			compressflag = B_TRUE;
18238313SDina.Nimeh@Sun.Com 			if (((argc - optind) > 1) && (*argv[optind] != '-')) {
18248313SDina.Nimeh@Sun.Com 				/* optional algorithm */
18258313SDina.Nimeh@Sun.Com 				algname = argv[optind];
18265643Saalok 				optind++;
18275643Saalok 			}
18285643Saalok 			check_algorithm_validity(algname, &compress_index);
18295643Saalok 			break;
18308313SDina.Nimeh@Sun.Com 		case 'c':
18318313SDina.Nimeh@Sun.Com 			/* is the chosen cipher allowed? */
18328313SDina.Nimeh@Sun.Com 			if ((cipher = ciph2mech(optarg)) == NULL) {
18338313SDina.Nimeh@Sun.Com 				errflag = B_TRUE;
18348313SDina.Nimeh@Sun.Com 				warn(gettext("cipher %s not allowed\n"),
18358313SDina.Nimeh@Sun.Com 				    optarg);
18368313SDina.Nimeh@Sun.Com 			}
18378313SDina.Nimeh@Sun.Com 			need_crypto = B_TRUE;
18388313SDina.Nimeh@Sun.Com 			/* cipher_only is already set */
18398313SDina.Nimeh@Sun.Com 			break;
18400Sstevel@tonic-gate 		case 'd':
18418313SDina.Nimeh@Sun.Com 			deleteflag = B_TRUE;
18420Sstevel@tonic-gate 			minor = name_to_minor(optarg);
18430Sstevel@tonic-gate 			if (minor != 0)
18440Sstevel@tonic-gate 				devicename = optarg;
18458069SDina.Nimeh@Sun.Com 			else {
18468069SDina.Nimeh@Sun.Com 				if ((filename = realpath(optarg,
18478069SDina.Nimeh@Sun.Com 				    realfilename)) == NULL)
18488069SDina.Nimeh@Sun.Com 					die("%s", optarg);
18498069SDina.Nimeh@Sun.Com 			}
18500Sstevel@tonic-gate 			break;
18518313SDina.Nimeh@Sun.Com 		case 'e':
18528313SDina.Nimeh@Sun.Com 			ephflag = B_TRUE;
18538313SDina.Nimeh@Sun.Com 			need_crypto = B_TRUE;
18548313SDina.Nimeh@Sun.Com 			cipher_only = B_FALSE;	/* need to unset cipher_only */
18558313SDina.Nimeh@Sun.Com 			break;
18564451Seschrock 		case 'f':
18574451Seschrock 			force = B_TRUE;
18584451Seschrock 			break;
18598313SDina.Nimeh@Sun.Com 		case 'k':
18608313SDina.Nimeh@Sun.Com 			keyfile = optarg;
18618313SDina.Nimeh@Sun.Com 			need_crypto = B_TRUE;
18628313SDina.Nimeh@Sun.Com 			cipher_only = B_FALSE;	/* need to unset cipher_only */
18638313SDina.Nimeh@Sun.Com 			break;
18645643Saalok 		case 's':
18655643Saalok 			segsize = convert_to_num(optarg);
18669048Sjrgn.keil@googlemail.com 			if (segsize < DEV_BSIZE || !ISP2(segsize))
18675643Saalok 				die(gettext("segment size %s is invalid "
18685643Saalok 				    "or not a multiple of minimum block "
18695643Saalok 				    "size %ld\n"), optarg, DEV_BSIZE);
18708313SDina.Nimeh@Sun.Com 			break;
18718313SDina.Nimeh@Sun.Com 		case 'T':
18728313SDina.Nimeh@Sun.Com 			if ((token = parsetoken(optarg)) == NULL) {
18738313SDina.Nimeh@Sun.Com 				errflag = B_TRUE;
18748313SDina.Nimeh@Sun.Com 				warn(
18758313SDina.Nimeh@Sun.Com 				    gettext("invalid token key specifier %s\n"),
18768313SDina.Nimeh@Sun.Com 				    optarg);
18778313SDina.Nimeh@Sun.Com 			}
18788313SDina.Nimeh@Sun.Com 			need_crypto = B_TRUE;
18798313SDina.Nimeh@Sun.Com 			cipher_only = B_FALSE;	/* need to unset cipher_only */
18805643Saalok 			break;
18815643Saalok 		case 'U':
18828313SDina.Nimeh@Sun.Com 			uncompressflag = B_TRUE;
18835643Saalok 			break;
18840Sstevel@tonic-gate 		case '?':
18850Sstevel@tonic-gate 		default:
18868313SDina.Nimeh@Sun.Com 			errflag = B_TRUE;
18870Sstevel@tonic-gate 			break;
18880Sstevel@tonic-gate 		}
18890Sstevel@tonic-gate 	}
18908313SDina.Nimeh@Sun.Com 
18918313SDina.Nimeh@Sun.Com 	/* Check for mutually exclusive combinations of options */
18925643Saalok 	if (errflag ||
18935643Saalok 	    (addflag && deleteflag) ||
18948313SDina.Nimeh@Sun.Com 	    (!addflag && need_crypto) ||
18955643Saalok 	    ((compressflag || uncompressflag) && (addflag || deleteflag)))
18968313SDina.Nimeh@Sun.Com 		usage(pname);
18978313SDina.Nimeh@Sun.Com 
18988313SDina.Nimeh@Sun.Com 	/* ephemeral key, and key from either file or token are incompatible */
18998313SDina.Nimeh@Sun.Com 	if (ephflag && (keyfile != NULL || token != NULL)) {
19008313SDina.Nimeh@Sun.Com 		die(gettext("ephemeral key cannot be used with keyfile"
19018313SDina.Nimeh@Sun.Com 		    " or token key\n"));
19028313SDina.Nimeh@Sun.Com 	}
19038313SDina.Nimeh@Sun.Com 
19048313SDina.Nimeh@Sun.Com 	/*
19058313SDina.Nimeh@Sun.Com 	 * "-c" but no "-k", "-T", "-e", or "-T -k" means derive key from
19068313SDina.Nimeh@Sun.Com 	 * command line passphrase
19078313SDina.Nimeh@Sun.Com 	 */
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 	switch (argc - optind) {
19100Sstevel@tonic-gate 	case 0: /* no more args */
19118313SDina.Nimeh@Sun.Com 		if (compressflag || uncompressflag)	/* needs filename */
19128313SDina.Nimeh@Sun.Com 			usage(pname);
19130Sstevel@tonic-gate 		break;
19148313SDina.Nimeh@Sun.Com 	case 1:
19150Sstevel@tonic-gate 		if (addflag || deleteflag)
19168313SDina.Nimeh@Sun.Com 			usage(pname);
19178313SDina.Nimeh@Sun.Com 		/* one arg means compress/uncompress the file ... */
19188313SDina.Nimeh@Sun.Com 		if (compressflag || uncompressflag) {
19198069SDina.Nimeh@Sun.Com 			if ((filename = realpath(argv[optind],
19208069SDina.Nimeh@Sun.Com 			    realfilename)) == NULL)
19218069SDina.Nimeh@Sun.Com 				die("%s", argv[optind]);
19228313SDina.Nimeh@Sun.Com 		/* ... or without options means print the association */
19238313SDina.Nimeh@Sun.Com 		} else {
19248313SDina.Nimeh@Sun.Com 			minor = name_to_minor(argv[optind]);
19258313SDina.Nimeh@Sun.Com 			if (minor != 0)
19268313SDina.Nimeh@Sun.Com 				devicename = argv[optind];
19278313SDina.Nimeh@Sun.Com 			else {
19288313SDina.Nimeh@Sun.Com 				if ((filename = realpath(argv[optind],
19298313SDina.Nimeh@Sun.Com 				    realfilename)) == NULL)
19308313SDina.Nimeh@Sun.Com 					die("%s", argv[optind]);
19318313SDina.Nimeh@Sun.Com 			}
19328069SDina.Nimeh@Sun.Com 		}
19330Sstevel@tonic-gate 		break;
19340Sstevel@tonic-gate 	default:
19358313SDina.Nimeh@Sun.Com 		usage(pname);
19360Sstevel@tonic-gate 		break;
19370Sstevel@tonic-gate 	}
19380Sstevel@tonic-gate 
19398313SDina.Nimeh@Sun.Com 	if (addflag || compressflag || uncompressflag)
19408313SDina.Nimeh@Sun.Com 		check_file_validity(filename);
19418313SDina.Nimeh@Sun.Com 
19420Sstevel@tonic-gate 	if (filename && !valid_abspath(filename))
19430Sstevel@tonic-gate 		exit(E_ERROR);
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate 	/*
19460Sstevel@tonic-gate 	 * Here, we know the arguments are correct, the filename is an
19470Sstevel@tonic-gate 	 * absolute path, it exists and is a regular file. We don't yet
19480Sstevel@tonic-gate 	 * know that the device name is ok or not.
19490Sstevel@tonic-gate 	 */
19508313SDina.Nimeh@Sun.Com 
19510Sstevel@tonic-gate 	openflag = O_EXCL;
19525643Saalok 	if (addflag || deleteflag || compressflag || uncompressflag)
19530Sstevel@tonic-gate 		openflag |= O_RDWR;
19540Sstevel@tonic-gate 	else
19550Sstevel@tonic-gate 		openflag |= O_RDONLY;
19560Sstevel@tonic-gate 	lfd = open(lofictl, openflag);
19570Sstevel@tonic-gate 	if (lfd == -1) {
19580Sstevel@tonic-gate 		if ((errno == EPERM) || (errno == EACCES)) {
19598069SDina.Nimeh@Sun.Com 			die(gettext("you do not have permission to perform "
19608069SDina.Nimeh@Sun.Com 			    "that operation.\n"));
19610Sstevel@tonic-gate 		} else {
19628069SDina.Nimeh@Sun.Com 			die(gettext("open: %s"), lofictl);
19630Sstevel@tonic-gate 		}
19640Sstevel@tonic-gate 		/*NOTREACHED*/
19650Sstevel@tonic-gate 	}
19668313SDina.Nimeh@Sun.Com 
19678313SDina.Nimeh@Sun.Com 	/*
19688313SDina.Nimeh@Sun.Com 	 * No passphrase is needed for ephemeral key, or when key is
19698313SDina.Nimeh@Sun.Com 	 * in a file and not wrapped by another key from a token.
19708313SDina.Nimeh@Sun.Com 	 * However, a passphrase is needed in these cases:
19718313SDina.Nimeh@Sun.Com 	 * 1. cipher with no ephemeral key, key file, or token,
19728313SDina.Nimeh@Sun.Com 	 *    in which case the passphrase is used to build the key
19738313SDina.Nimeh@Sun.Com 	 * 2. token with an optional cipher or optional key file,
19748313SDina.Nimeh@Sun.Com 	 *    in which case the passphrase unlocks the token
19758313SDina.Nimeh@Sun.Com 	 * If only the cipher is specified, reconfirm the passphrase
19768313SDina.Nimeh@Sun.Com 	 * to ensure the user hasn't mis-entered it.  Otherwise, the
19778313SDina.Nimeh@Sun.Com 	 * token will enforce the token passphrase.
19788313SDina.Nimeh@Sun.Com 	 */
19798313SDina.Nimeh@Sun.Com 	if (need_crypto) {
19808313SDina.Nimeh@Sun.Com 		CK_SESSION_HANDLE	sess;
19818313SDina.Nimeh@Sun.Com 
19828313SDina.Nimeh@Sun.Com 		/* pick a cipher if none specified */
19838313SDina.Nimeh@Sun.Com 		if (cipher == NULL)
19848313SDina.Nimeh@Sun.Com 			cipher = DEFAULT_CIPHER;
19858313SDina.Nimeh@Sun.Com 
19868313SDina.Nimeh@Sun.Com 		if (!kernel_cipher_check(cipher))
19878313SDina.Nimeh@Sun.Com 			die(gettext(
19888313SDina.Nimeh@Sun.Com 			    "use \"cryptoadm list -m\" to find available "
19898313SDina.Nimeh@Sun.Com 			    "mechanisms\n"));
19908313SDina.Nimeh@Sun.Com 
19918313SDina.Nimeh@Sun.Com 		init_crypto(token, cipher, &sess);
19928313SDina.Nimeh@Sun.Com 
19938313SDina.Nimeh@Sun.Com 		if (cipher_only) {
19948313SDina.Nimeh@Sun.Com 			getkeyfromuser(cipher, &rkey, &rksz);
19958313SDina.Nimeh@Sun.Com 		} else if (token != NULL) {
19968313SDina.Nimeh@Sun.Com 			getkeyfromtoken(sess, token, keyfile, cipher,
19978313SDina.Nimeh@Sun.Com 			    &rkey, &rksz);
19988313SDina.Nimeh@Sun.Com 		} else {
19998313SDina.Nimeh@Sun.Com 			/* this also handles ephemeral keys */
20008313SDina.Nimeh@Sun.Com 			getkeyfromfile(keyfile, cipher, &rkey, &rksz);
20018313SDina.Nimeh@Sun.Com 		}
20028313SDina.Nimeh@Sun.Com 
20038313SDina.Nimeh@Sun.Com 		end_crypto(sess);
20048313SDina.Nimeh@Sun.Com 	}
20058313SDina.Nimeh@Sun.Com 
20068313SDina.Nimeh@Sun.Com 	/*
20078313SDina.Nimeh@Sun.Com 	 * Now to the real work.
20088313SDina.Nimeh@Sun.Com 	 */
20090Sstevel@tonic-gate 	if (addflag)
20108313SDina.Nimeh@Sun.Com 		add_mapping(lfd, devicename, filename, cipher, rkey, rksz);
20115643Saalok 	else if (compressflag)
20126926Saalok 		lofi_compress(&lfd, filename, compress_index, segsize);
20135643Saalok 	else if (uncompressflag)
20145643Saalok 		lofi_uncompress(lfd, filename);
20150Sstevel@tonic-gate 	else if (deleteflag)
20164451Seschrock 		delete_mapping(lfd, devicename, filename, force);
20170Sstevel@tonic-gate 	else if (filename || devicename)
20180Sstevel@tonic-gate 		print_one_mapping(lfd, devicename, filename);
20190Sstevel@tonic-gate 	else
20200Sstevel@tonic-gate 		print_mappings(lfd);
20215719Saalok 
20226926Saalok 	if (lfd != -1)
20236926Saalok 		(void) close(lfd);
20245719Saalok 	closelib();
20250Sstevel@tonic-gate 	return (E_SUCCESS);
20260Sstevel@tonic-gate }
2027