xref: /onnv-gate/usr/src/cmd/cmd-crypto/decrypt/decrypt.c (revision 871:f917b2ed7d21)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * decrypt.c
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  * Implements encrypt(1) and decrypt(1) commands
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * One binary performs both encrypt/decrypt operation.
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  * usage:
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  *  algorithm - mechanism name without CKM_ prefix. Case
390Sstevel@tonic-gate  *              does not matter
400Sstevel@tonic-gate  *  keyfile - file containing key data. If not specified user is
410Sstevel@tonic-gate  *            prompted to enter key. key length > 0 is required
420Sstevel@tonic-gate  *  infile  - input file to encrypt/decrypt. If omitted, stdin used.
430Sstevel@tonic-gate  *  outfile - output file to encrypt/decrypt. If omitted, stdout used.
440Sstevel@tonic-gate  *            if infile & outfile are same, a temp file is used for
450Sstevel@tonic-gate  *            output and infile is replaced with this file after
460Sstevel@tonic-gate  *            operation is complete.
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * Implementation notes:
490Sstevel@tonic-gate  *   iv data - It is generated by random bytes equal to one block size.
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  *   encrypted output format -
520Sstevel@tonic-gate  *   - Output format version number - 4 bytes in network byte order.
530Sstevel@tonic-gate  *   - Iterations used in key gen function, 4 bytes in  network byte order.
540Sstevel@tonic-gate  *   - IV ( 'ivlen' bytes)
550Sstevel@tonic-gate  *   - Salt data used in key gen (16 bytes)
560Sstevel@tonic-gate  *   - cipher text data.
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #include <stdio.h>
610Sstevel@tonic-gate #include <stdlib.h>
620Sstevel@tonic-gate #include <unistd.h>
630Sstevel@tonic-gate #include <errno.h>
640Sstevel@tonic-gate #include <fcntl.h>
650Sstevel@tonic-gate #include <ctype.h>
660Sstevel@tonic-gate #include <strings.h>
670Sstevel@tonic-gate #include <libintl.h>
680Sstevel@tonic-gate #include <libgen.h>
690Sstevel@tonic-gate #include <locale.h>
700Sstevel@tonic-gate #include <limits.h>
710Sstevel@tonic-gate #include <sys/types.h>
720Sstevel@tonic-gate #include <sys/stat.h>
730Sstevel@tonic-gate #include <netinet/in.h>
740Sstevel@tonic-gate #include <security/cryptoki.h>
750Sstevel@tonic-gate #include <cryptoutil.h>
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #define	BUFFERSIZE	(2048)		/* Buffer size for reading file */
780Sstevel@tonic-gate #define	BLOCKSIZE	(128)		/* Largest guess for block size */
790Sstevel@tonic-gate #define	PROGRESSSIZE	(BUFFERSIZE*20)	/* stdin progress indicator size */
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #define	PBKD2_ITERATIONS (1000)
820Sstevel@tonic-gate #define	PBKD2_SALT_SIZE	16
830Sstevel@tonic-gate 
840Sstevel@tonic-gate #define	SUNW_ENCRYPT_FILE_VERSION 1
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate  * Exit Status codes
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate #ifndef EXIT_SUCCESS
900Sstevel@tonic-gate #define	EXIT_SUCCESS	0	/* No errors */
910Sstevel@tonic-gate #define	EXIT_FAILURE	1	/* All errors except usage */
920Sstevel@tonic-gate #endif /* EXIT_SUCCESS */
930Sstevel@tonic-gate 
940Sstevel@tonic-gate #define	EXIT_USAGE	2	/* usage/syntax error */
950Sstevel@tonic-gate 
960Sstevel@tonic-gate #define	RANDOM_DEVICE	"/dev/urandom"	/* random device name */
970Sstevel@tonic-gate 
980Sstevel@tonic-gate #define	ENCRYPT_NAME	"encrypt"	/* name of encrypt command */
990Sstevel@tonic-gate #define	ENCRYPT_OPTIONS "a:k:i:o:lv"	/* options for encrypt */
1000Sstevel@tonic-gate #define	DECRYPT_NAME	"decrypt"	/* name of decrypt command */
1010Sstevel@tonic-gate #define	DECRYPT_OPTIONS "a:k:i:o:lv"	/* options for decrypt */
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * Structure containing info for encrypt/decrypt
1050Sstevel@tonic-gate  * command
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate struct CommandInfo {
1080Sstevel@tonic-gate 	char		*name;		/* name of the command */
1090Sstevel@tonic-gate 	char		*options;	/* command line options */
1100Sstevel@tonic-gate 	CK_FLAGS	flags;
1110Sstevel@tonic-gate 	CK_ATTRIBUTE_TYPE type;		/* type of command */
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	/* function pointers for various operations */
1140Sstevel@tonic-gate 	CK_RV	(*Init)(CK_SESSION_HANDLE, CK_MECHANISM_PTR, CK_OBJECT_HANDLE);
1150Sstevel@tonic-gate 	CK_RV	(*Update)(CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG, CK_BYTE_PTR,
1160Sstevel@tonic-gate 		CK_ULONG_PTR);
1170Sstevel@tonic-gate 	CK_RV	(*Crypt)(CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG, CK_BYTE_PTR,
1180Sstevel@tonic-gate 		CK_ULONG_PTR);
1190Sstevel@tonic-gate 	CK_RV	(*Final)(CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG_PTR);
1200Sstevel@tonic-gate };
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate static struct CommandInfo encrypt_cmd = {
1230Sstevel@tonic-gate 	ENCRYPT_NAME,
1240Sstevel@tonic-gate 	ENCRYPT_OPTIONS,
1250Sstevel@tonic-gate 	CKF_ENCRYPT,
1260Sstevel@tonic-gate 	CKA_ENCRYPT,
1270Sstevel@tonic-gate 	C_EncryptInit,
1280Sstevel@tonic-gate 	C_EncryptUpdate,
1290Sstevel@tonic-gate 	C_Encrypt,
1300Sstevel@tonic-gate 	C_EncryptFinal
1310Sstevel@tonic-gate };
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate static struct CommandInfo decrypt_cmd = {
1340Sstevel@tonic-gate 	DECRYPT_NAME,
1350Sstevel@tonic-gate 	DECRYPT_OPTIONS,
1360Sstevel@tonic-gate 	CKF_DECRYPT,
1370Sstevel@tonic-gate 	CKA_DECRYPT,
1380Sstevel@tonic-gate 	C_DecryptInit,
1390Sstevel@tonic-gate 	C_DecryptUpdate,
1400Sstevel@tonic-gate 	C_Decrypt,
1410Sstevel@tonic-gate 	C_DecryptFinal
1420Sstevel@tonic-gate };
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate struct mech_alias {
1450Sstevel@tonic-gate 	CK_MECHANISM_TYPE type;
1460Sstevel@tonic-gate 	char *alias;
1470Sstevel@tonic-gate 	CK_ULONG keysize_min;
1480Sstevel@tonic-gate 	CK_ULONG keysize_max;
1490Sstevel@tonic-gate 	int keysize_unit;
1500Sstevel@tonic-gate 	int ivlen;
1510Sstevel@tonic-gate 	boolean_t available;
1520Sstevel@tonic-gate };
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate #define	MECH_ALIASES_COUNT 4
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate static struct mech_alias mech_aliases[] = {
1570Sstevel@tonic-gate 	{ CKM_AES_CBC_PAD, "aes", ULONG_MAX, 0L, 8, 16, B_FALSE },
1580Sstevel@tonic-gate 	{ CKM_RC4, "arcfour", ULONG_MAX, 0L, 1, 0, B_FALSE },
1590Sstevel@tonic-gate 	{ CKM_DES_CBC_PAD, "des", 8, 8, 8, 8, B_FALSE },
1600Sstevel@tonic-gate 	{ CKM_DES3_CBC_PAD, "3des", 24, 24, 8, 8, B_FALSE },
1610Sstevel@tonic-gate };
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate static CK_BBOOL truevalue = TRUE;
1640Sstevel@tonic-gate static CK_BBOOL falsevalue = FALSE;
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate static boolean_t aflag = B_FALSE; /* -a <algorithm> flag, required */
1670Sstevel@tonic-gate static boolean_t kflag = B_FALSE; /* -k <keyfile> flag */
1680Sstevel@tonic-gate static boolean_t iflag = B_FALSE; /* -i <infile> flag, use stdin if absent */
1690Sstevel@tonic-gate static boolean_t oflag = B_FALSE; /* -o <outfile> flag, use stdout if absent */
1700Sstevel@tonic-gate static boolean_t lflag = B_FALSE; /* -l flag (list) */
1710Sstevel@tonic-gate static boolean_t vflag = B_FALSE; /* -v flag (verbose) */
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate static char *keyfile = NULL;	/* name of keyfile */
1740Sstevel@tonic-gate static char *inputfile = NULL;	/* name of input file */
1750Sstevel@tonic-gate static char *outputfile = NULL;	/* name of output file */
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate static int status_pos = 0; /* current position of progress bar element */
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate /*
1800Sstevel@tonic-gate  * function prototypes
1810Sstevel@tonic-gate  */
1820Sstevel@tonic-gate static void usage(struct CommandInfo *cmd);
1830Sstevel@tonic-gate static int execute_cmd(struct CommandInfo *cmd, char *algo_str);
1840Sstevel@tonic-gate static int cryptogetkey(CK_BYTE_PTR *pkeydata, CK_ULONG_PTR pkeysize);
1850Sstevel@tonic-gate static int cryptoreadfile(char *filename, CK_BYTE_PTR *pdata,
1860Sstevel@tonic-gate 	CK_ULONG_PTR pdatalen);
1870Sstevel@tonic-gate static int get_random_data(CK_BYTE_PTR pivbuf, int ivlen);
1880Sstevel@tonic-gate static int crypt_multipart(struct CommandInfo *cmd, CK_SESSION_HANDLE hSession,
1890Sstevel@tonic-gate 	int infd, int outfd, struct stat in);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate int
1920Sstevel@tonic-gate main(int argc, char **argv)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	extern char *optarg;
1960Sstevel@tonic-gate 	extern int optind;
1970Sstevel@tonic-gate 	char *optstr;
1980Sstevel@tonic-gate 	char c;			/* current getopts flag */
1990Sstevel@tonic-gate 	char *algo_str = NULL;	/* algorithm string */
2000Sstevel@tonic-gate 	struct CommandInfo *cmd;
2010Sstevel@tonic-gate 	char *cmdname;		/* name of command */
2020Sstevel@tonic-gate 	boolean_t errflag = B_FALSE;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2050Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defiend by cc -D */
2060Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
2070Sstevel@tonic-gate #endif
2080Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	/*
2110Sstevel@tonic-gate 	 * Based on command name, determine
2120Sstevel@tonic-gate 	 * type of command.
2130Sstevel@tonic-gate 	 */
2140Sstevel@tonic-gate 	cmdname = basename(argv[0]);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	cryptodebug_init(cmdname);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	if (strcmp(cmdname, encrypt_cmd.name) == 0) {
2190Sstevel@tonic-gate 		cmd = &encrypt_cmd;
2200Sstevel@tonic-gate 	} else if (strcmp(cmdname, decrypt_cmd.name) == 0) {
2210Sstevel@tonic-gate 		cmd = &decrypt_cmd;
2220Sstevel@tonic-gate 	} else {
2230Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
2240Sstevel@tonic-gate 		    "command name must be either encrypt or decrypt"));
2250Sstevel@tonic-gate 		exit(EXIT_USAGE);
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	optstr = cmd->options;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	/* Parse command line arguments */
2310Sstevel@tonic-gate 	while (!errflag && (c = getopt(argc, argv, optstr)) != -1) {
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 		switch (c) {
2340Sstevel@tonic-gate 		case 'a':
2350Sstevel@tonic-gate 			aflag = B_TRUE;
2360Sstevel@tonic-gate 			algo_str = optarg;
2370Sstevel@tonic-gate 			break;
2380Sstevel@tonic-gate 		case 'k':
2390Sstevel@tonic-gate 			kflag = B_TRUE;
2400Sstevel@tonic-gate 			keyfile = optarg;
2410Sstevel@tonic-gate 			break;
2420Sstevel@tonic-gate 		case 'i':
2430Sstevel@tonic-gate 			iflag = B_TRUE;
2440Sstevel@tonic-gate 			inputfile = optarg;
2450Sstevel@tonic-gate 			break;
2460Sstevel@tonic-gate 		case 'o':
2470Sstevel@tonic-gate 			oflag = B_TRUE;
2480Sstevel@tonic-gate 			outputfile = optarg;
2490Sstevel@tonic-gate 			break;
2500Sstevel@tonic-gate 		case 'l':
2510Sstevel@tonic-gate 			lflag = B_TRUE;
2520Sstevel@tonic-gate 			break;
2530Sstevel@tonic-gate 		case 'v':
2540Sstevel@tonic-gate 			vflag = B_TRUE;
2550Sstevel@tonic-gate 			break;
2560Sstevel@tonic-gate 		default:
2570Sstevel@tonic-gate 			errflag = B_TRUE;
2580Sstevel@tonic-gate 		}
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	if (errflag || (!aflag && !lflag) || (lflag && argc > 2) ||
2620Sstevel@tonic-gate 	    (optind < argc)) {
2630Sstevel@tonic-gate 		usage(cmd);
2640Sstevel@tonic-gate 		exit(EXIT_USAGE);
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	return (execute_cmd(cmd, algo_str));
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate /*
2710Sstevel@tonic-gate  * usage message
2720Sstevel@tonic-gate  */
2730Sstevel@tonic-gate static void
2740Sstevel@tonic-gate usage(struct CommandInfo *cmd)
2750Sstevel@tonic-gate {
2760Sstevel@tonic-gate 	if (cmd->type == CKA_ENCRYPT) {
2770Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("usage: encrypt -l | -a "
2780Sstevel@tonic-gate 		    "<algorithm> [-k <keyfile>] [-i <infile>]"
2790Sstevel@tonic-gate 		    "\n\t\t\t[-o <outfile>]"));
2800Sstevel@tonic-gate 	} else {
2810Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("usage: decrypt -l | -a "
2820Sstevel@tonic-gate 		    "<algorithm> [-k <keyfile>] [-i <infile>]"
2830Sstevel@tonic-gate 		    "\n\t\t\t[-o <outfile>]"));
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate /*
2880Sstevel@tonic-gate  * Print out list of algorithms in default and verbose mode
2890Sstevel@tonic-gate  */
2900Sstevel@tonic-gate static void
2910Sstevel@tonic-gate algorithm_list()
2920Sstevel@tonic-gate {
2930Sstevel@tonic-gate 	int mech;
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	(void) printf(gettext("Algorithm       Keysize:  Min   Max (bits)\n"
2960Sstevel@tonic-gate 	    "------------------------------------------\n"));
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	for (mech = 0; mech < MECH_ALIASES_COUNT; mech++) {
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 		if (mech_aliases[mech].available == B_FALSE)
3010Sstevel@tonic-gate 			continue;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 		(void) printf("%-15s", mech_aliases[mech].alias);
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 		if (mech_aliases[mech].keysize_min != UINT_MAX &&
3060Sstevel@tonic-gate 		    mech_aliases[mech].keysize_max != 0)
3070Sstevel@tonic-gate 			(void) printf("         %5lu %5lu\n",
3080Sstevel@tonic-gate 			    (mech_aliases[mech].keysize_min *
3090Sstevel@tonic-gate 				mech_aliases[mech].keysize_unit),
3100Sstevel@tonic-gate 			    (mech_aliases[mech].keysize_max *
3110Sstevel@tonic-gate 				mech_aliases[mech].keysize_unit));
3120Sstevel@tonic-gate 		else
3130Sstevel@tonic-gate 			(void) printf("\n");
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate static CK_RV
3190Sstevel@tonic-gate generate_pkcs5_key(CK_SESSION_HANDLE hSession,
3200Sstevel@tonic-gate 		CK_BYTE		*pSaltData,
3210Sstevel@tonic-gate 		CK_ULONG	saltLen,
3220Sstevel@tonic-gate 		CK_ULONG	iterations,
3230Sstevel@tonic-gate 		CK_BYTE		*pkeydata, /* user entered passphrase */
3240Sstevel@tonic-gate 		CK_KEY_TYPE	keytype,
3250Sstevel@tonic-gate 		CK_ULONG	passwd_size,
3260Sstevel@tonic-gate 		CK_ULONG	keylen,  /* desired length of generated key */
3270Sstevel@tonic-gate 		CK_ATTRIBUTE_TYPE operation,
3280Sstevel@tonic-gate 		CK_OBJECT_HANDLE *hKey)
3290Sstevel@tonic-gate {
3300Sstevel@tonic-gate 	CK_RV rv;
3310Sstevel@tonic-gate 	CK_PKCS5_PBKD2_PARAMS params;
3320Sstevel@tonic-gate 	CK_MECHANISM mechanism;
3330Sstevel@tonic-gate 	CK_OBJECT_CLASS class = CKO_SECRET_KEY;
3340Sstevel@tonic-gate 	CK_ATTRIBUTE tmpl[4];
3350Sstevel@tonic-gate 	int attrs = 0;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	mechanism.mechanism = CKM_PKCS5_PBKD2;
3380Sstevel@tonic-gate 	mechanism.pParameter = &params;
3390Sstevel@tonic-gate 	mechanism.ulParameterLen = sizeof (params);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	tmpl[attrs].type = CKA_CLASS;
3420Sstevel@tonic-gate 	tmpl[attrs].pValue = &class;
3430Sstevel@tonic-gate 	tmpl[attrs].ulValueLen = sizeof (class);
3440Sstevel@tonic-gate 	attrs++;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	tmpl[attrs].type = CKA_KEY_TYPE;
3470Sstevel@tonic-gate 	tmpl[attrs].pValue = &keytype;
3480Sstevel@tonic-gate 	tmpl[attrs].ulValueLen = sizeof (keytype);
3490Sstevel@tonic-gate 	attrs++;
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	tmpl[attrs].type = operation;
3520Sstevel@tonic-gate 	tmpl[attrs].pValue = &truevalue;
3530Sstevel@tonic-gate 	tmpl[attrs].ulValueLen = sizeof (CK_BBOOL);
3540Sstevel@tonic-gate 	attrs++;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	if (keylen > 0) {
3570Sstevel@tonic-gate 		tmpl[attrs].type = CKA_VALUE_LEN;
3580Sstevel@tonic-gate 		tmpl[attrs].pValue = &keylen;
3590Sstevel@tonic-gate 		tmpl[attrs].ulValueLen = sizeof (keylen);
3600Sstevel@tonic-gate 		attrs++;
3610Sstevel@tonic-gate 	}
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	params.saltSource = CKZ_SALT_SPECIFIED;
3640Sstevel@tonic-gate 	params.pSaltSourceData = (void *)pSaltData;
3650Sstevel@tonic-gate 	params.ulSaltSourceDataLen = saltLen;
3660Sstevel@tonic-gate 	params.iterations = iterations;
3670Sstevel@tonic-gate 	params.prf = CKP_PKCS5_PBKD2_HMAC_SHA1;
3680Sstevel@tonic-gate 	params.pPrfData = NULL;
3690Sstevel@tonic-gate 	params.ulPrfDataLen = 0;
3700Sstevel@tonic-gate 	params.pPassword = (CK_UTF8CHAR_PTR)pkeydata;
3710Sstevel@tonic-gate 	params.ulPasswordLen = &passwd_size;
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	mechanism.mechanism = CKM_PKCS5_PBKD2;
3740Sstevel@tonic-gate 	mechanism.pParameter = &params;
3750Sstevel@tonic-gate 	mechanism.ulParameterLen = sizeof (params);
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	rv = C_GenerateKey(hSession, &mechanism, tmpl,
3780Sstevel@tonic-gate 		attrs, hKey);
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	return (rv);
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate /*
3850Sstevel@tonic-gate  * Execute the command.
3860Sstevel@tonic-gate  *   cmd - command pointing to type of operation.
3870Sstevel@tonic-gate  *   algo_str - alias of the algorithm passed.
3880Sstevel@tonic-gate  */
3890Sstevel@tonic-gate static int
3900Sstevel@tonic-gate execute_cmd(struct CommandInfo *cmd, char *algo_str)
3910Sstevel@tonic-gate {
3920Sstevel@tonic-gate 	CK_RV rv;
3930Sstevel@tonic-gate 	CK_ULONG slotcount;
3940Sstevel@tonic-gate 	CK_SLOT_ID slotID;
3950Sstevel@tonic-gate 	CK_SLOT_ID_PTR pSlotList = NULL;
3960Sstevel@tonic-gate 	CK_MECHANISM_TYPE mech_type = 0;
3970Sstevel@tonic-gate 	CK_MECHANISM_INFO info, kg_info;
3980Sstevel@tonic-gate 	CK_MECHANISM mech;
3990Sstevel@tonic-gate 	CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE;
4000Sstevel@tonic-gate 	CK_BYTE_PTR	pkeydata = NULL;
4010Sstevel@tonic-gate 	CK_BYTE		salt[PBKD2_SALT_SIZE];
4020Sstevel@tonic-gate 	CK_ULONG	keysize = 0;
4030Sstevel@tonic-gate 	int i, slot, mek;		/* index variables */
4040Sstevel@tonic-gate 	int status;
4050Sstevel@tonic-gate 	struct stat	insbuf;		/* stat buf for infile */
4060Sstevel@tonic-gate 	struct stat	outsbuf;	/* stat buf for outfile */
4070Sstevel@tonic-gate 	char	tmpnam[PATH_MAX];	/* tmp file name */
4080Sstevel@tonic-gate 	CK_OBJECT_HANDLE key = (CK_OBJECT_HANDLE) 0;
4090Sstevel@tonic-gate 	int infd = 0;			/* input file, stdin default */
4100Sstevel@tonic-gate 	int outfd = 1;			/* output file, stdout default */
4110Sstevel@tonic-gate 	char *outfilename = NULL;
4120Sstevel@tonic-gate 	boolean_t errflag = B_TRUE;
4130Sstevel@tonic-gate 	boolean_t inoutsame = B_FALSE;	/* if both input & output are same */
4140Sstevel@tonic-gate 	CK_BYTE_PTR	pivbuf = NULL_PTR;
4150Sstevel@tonic-gate 	CK_ULONG	ivlen = 0L;
4160Sstevel@tonic-gate 	int mech_match = 0;
4170Sstevel@tonic-gate 	CK_ULONG	iterations = PBKD2_ITERATIONS;
4180Sstevel@tonic-gate 	CK_ULONG	keylen;
4190Sstevel@tonic-gate 	int version = SUNW_ENCRYPT_FILE_VERSION;
4200Sstevel@tonic-gate 	CK_KEY_TYPE keytype;
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	if (aflag) {
4230Sstevel@tonic-gate 		/* Determine if algorithm is valid */
4240Sstevel@tonic-gate 		for (mech_match = 0; mech_match < MECH_ALIASES_COUNT;
4250Sstevel@tonic-gate 			mech_match++) {
4260Sstevel@tonic-gate 			if (strcmp(algo_str,
4270Sstevel@tonic-gate 			    mech_aliases[mech_match].alias) == 0) {
4280Sstevel@tonic-gate 				mech_type = mech_aliases[mech_match].type;
4290Sstevel@tonic-gate 				break;
4300Sstevel@tonic-gate 			}
4310Sstevel@tonic-gate 		}
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 		if (mech_match == MECH_ALIASES_COUNT) {
4340Sstevel@tonic-gate 			cryptoerror(LOG_STDERR,
4350Sstevel@tonic-gate 			    gettext("unknown algorithm -- %s"), algo_str);
4360Sstevel@tonic-gate 			return (EXIT_FAILURE);
4370Sstevel@tonic-gate 		}
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 		/*
4400Sstevel@tonic-gate 		 * Process keyfile
4410Sstevel@tonic-gate 		 *
4420Sstevel@tonic-gate 		 * If a keyfile is provided, get the key data from
4430Sstevel@tonic-gate 		 * the file. Otherwise, prompt for a passphrase. The
4440Sstevel@tonic-gate 		 * passphrase is used as the key data.
4450Sstevel@tonic-gate 		 */
4460Sstevel@tonic-gate 		if (kflag) {
4470Sstevel@tonic-gate 			status = cryptoreadfile(keyfile, &pkeydata, &keysize);
4480Sstevel@tonic-gate 		} else {
4490Sstevel@tonic-gate 			status = cryptogetkey(&pkeydata, &keysize);
4500Sstevel@tonic-gate 		}
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 		if (status == -1 || keysize == 0L) {
4530Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext("invalid key."));
4540Sstevel@tonic-gate 			return (EXIT_FAILURE);
4550Sstevel@tonic-gate 		}
4560Sstevel@tonic-gate 	}
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	bzero(salt, sizeof (salt));
4590Sstevel@tonic-gate 	/* Initialize pkcs */
4600Sstevel@tonic-gate 	if ((rv = C_Initialize(NULL)) != CKR_OK) {
4610Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("failed to initialize "
4620Sstevel@tonic-gate 		    "PKCS #11 framework: %s"), pkcs11_strerror(rv));
4630Sstevel@tonic-gate 		goto cleanup;
4640Sstevel@tonic-gate 	}
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	/* Get slot count */
4670Sstevel@tonic-gate 	rv = C_GetSlotList(0, NULL_PTR, &slotcount);
4680Sstevel@tonic-gate 	if (rv != CKR_OK || slotcount == 0) {
4690Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
4700Sstevel@tonic-gate 		    "failed to find any cryptographic provider,"
4710Sstevel@tonic-gate 		    "please check with your system administrator: %s"),
4720Sstevel@tonic-gate 		    pkcs11_strerror(rv));
4730Sstevel@tonic-gate 		goto cleanup;
4740Sstevel@tonic-gate 	}
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	/* Found at least one slot, allocate memory for slot list */
4770Sstevel@tonic-gate 	pSlotList = malloc(slotcount * sizeof (CK_SLOT_ID));
4780Sstevel@tonic-gate 	if (pSlotList == NULL_PTR) {
4790Sstevel@tonic-gate 		int err = errno;
4800Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("malloc: %s"), strerror(err));
4810Sstevel@tonic-gate 		goto cleanup;
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	/* Get the list of slots */
4850Sstevel@tonic-gate 	if ((rv = C_GetSlotList(0, pSlotList, &slotcount)) != CKR_OK) {
4860Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
4870Sstevel@tonic-gate 		    "failed to find any cryptographic provider,"
4880Sstevel@tonic-gate 		    "please check with your system administrator: %s"),
4890Sstevel@tonic-gate 		    pkcs11_strerror(rv));
4900Sstevel@tonic-gate 		goto cleanup;
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	if (lflag) {
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 		/* Iterate through slots */
4960Sstevel@tonic-gate 		for (slot = 0; slot < slotcount; slot++) {
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 			/* Iterate through each mechanism */
4990Sstevel@tonic-gate 			for (mek = 0; mek < MECH_ALIASES_COUNT; mek++) {
5000Sstevel@tonic-gate 				rv = C_GetMechanismInfo(pSlotList[slot],
5010Sstevel@tonic-gate 				    mech_aliases[mek].type, &info);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 				if (rv != CKR_OK)
5040Sstevel@tonic-gate 					continue;
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 				/*
5070Sstevel@tonic-gate 				 * Set to minimum/maximum key sizes assuming
5080Sstevel@tonic-gate 				 * the values available are not 0.
5090Sstevel@tonic-gate 				 */
5100Sstevel@tonic-gate 				if (info.ulMinKeySize && (info.ulMinKeySize <
5110Sstevel@tonic-gate 				    mech_aliases[mek].keysize_min))
5120Sstevel@tonic-gate 					mech_aliases[mek].keysize_min =
5130Sstevel@tonic-gate 						    info.ulMinKeySize;
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 				if (info.ulMaxKeySize && (info.ulMaxKeySize >
5160Sstevel@tonic-gate 				    mech_aliases[mek].keysize_max))
5170Sstevel@tonic-gate 					mech_aliases[mek].keysize_max =
5180Sstevel@tonic-gate 						    info.ulMaxKeySize;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 				mech_aliases[mek].available = B_TRUE;
5210Sstevel@tonic-gate 			}
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 		}
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 		algorithm_list();
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 		errflag = B_FALSE;
5280Sstevel@tonic-gate 		goto cleanup;
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	/* Find a slot with matching mechanism */
5320Sstevel@tonic-gate 	for (i = 0; i < slotcount; i++) {
5330Sstevel@tonic-gate 		slotID = pSlotList[i];
5340Sstevel@tonic-gate 		rv = C_GetMechanismInfo(slotID, mech_type, &info);
5350Sstevel@tonic-gate 		if (rv != CKR_OK) {
5360Sstevel@tonic-gate 			continue; /* to the next slot */
5370Sstevel@tonic-gate 		} else {
5380Sstevel@tonic-gate 			/*
5390Sstevel@tonic-gate 			 * If the slot support the crypto, also
5400Sstevel@tonic-gate 			 * make sure it supports the correct
5410Sstevel@tonic-gate 			 * key generation mech if needed.
5420Sstevel@tonic-gate 			 *
5430Sstevel@tonic-gate 			 * We need PKCS5 when RC4 is used or
5440Sstevel@tonic-gate 			 * when the key is entered on cmd line.
5450Sstevel@tonic-gate 			 */
5460Sstevel@tonic-gate 			if ((info.flags & cmd->flags) &&
5470Sstevel@tonic-gate 			    (mech_type == CKM_RC4) || (keyfile == NULL)) {
5480Sstevel@tonic-gate 				rv = C_GetMechanismInfo(slotID,
5490Sstevel@tonic-gate 					CKM_PKCS5_PBKD2, &kg_info);
5500Sstevel@tonic-gate 				if (rv == CKR_OK)
5510Sstevel@tonic-gate 					break;
5520Sstevel@tonic-gate 			} else if (info.flags & cmd->flags) {
5530Sstevel@tonic-gate 				break;
5540Sstevel@tonic-gate 			}
5550Sstevel@tonic-gate 		}
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	/* Show error if no matching mechanism found */
5590Sstevel@tonic-gate 	if (i == slotcount) {
5600Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
5610Sstevel@tonic-gate 		    gettext("no cryptographic provider was "
5620Sstevel@tonic-gate 		    "found for this algorithm -- %s"), algo_str);
5630Sstevel@tonic-gate 		goto cleanup;
5640Sstevel@tonic-gate 	}
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	/* Open a session */
5680Sstevel@tonic-gate 	rv = C_OpenSession(slotID, CKF_SERIAL_SESSION,
5690Sstevel@tonic-gate 		NULL_PTR, NULL, &hSession);
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	if (rv != CKR_OK) {
5720Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
5730Sstevel@tonic-gate 		    gettext("can not open PKCS #11 session: %s"),
5740Sstevel@tonic-gate 		    pkcs11_strerror(rv));
5750Sstevel@tonic-gate 		goto cleanup;
5760Sstevel@tonic-gate 	}
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	/*
5790Sstevel@tonic-gate 	 * Generate IV data for encrypt.
5800Sstevel@tonic-gate 	 */
5810Sstevel@tonic-gate 	ivlen = mech_aliases[mech_match].ivlen;
5820Sstevel@tonic-gate 	if ((pivbuf = malloc((size_t)ivlen)) == NULL) {
5830Sstevel@tonic-gate 		int err = errno;
5840Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("malloc: %s"),
5850Sstevel@tonic-gate 		    strerror(err));
5860Sstevel@tonic-gate 		goto cleanup;
5870Sstevel@tonic-gate 	}
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	if (cmd->type == CKA_ENCRYPT) {
5900Sstevel@tonic-gate 		if ((get_random_data(pivbuf,
5910Sstevel@tonic-gate 		    mech_aliases[mech_match].ivlen)) != 0) {
5920Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
5930Sstevel@tonic-gate 				"Unable to generate random "
5940Sstevel@tonic-gate 				"data for initialization vector."));
5950Sstevel@tonic-gate 			goto cleanup;
5960Sstevel@tonic-gate 		}
5970Sstevel@tonic-gate 	}
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	/*
6000Sstevel@tonic-gate 	 * Create the key object
6010Sstevel@tonic-gate 	 */
6020Sstevel@tonic-gate 	rv = pkcs11_mech2keytype(mech_type, &keytype);
6030Sstevel@tonic-gate 	if (rv != CKR_OK) {
6040Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
6050Sstevel@tonic-gate 			gettext("unable to find key type for algorithm."));
6060Sstevel@tonic-gate 		goto cleanup;
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	/* Open input file */
6100Sstevel@tonic-gate 	if (iflag) {
6110Sstevel@tonic-gate 		if ((infd = open(inputfile, O_RDONLY | O_NONBLOCK)) == -1) {
6120Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
6130Sstevel@tonic-gate 				"can not open input file %s"), inputfile);
6140Sstevel@tonic-gate 			goto cleanup;
6150Sstevel@tonic-gate 		}
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 		/* Get info on input file */
6180Sstevel@tonic-gate 		if (fstat(infd, &insbuf) == -1) {
6190Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
6200Sstevel@tonic-gate 				"can not stat input file %s"), inputfile);
6210Sstevel@tonic-gate 			goto cleanup;
6220Sstevel@tonic-gate 		}
6230Sstevel@tonic-gate 	}
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	/*
6260Sstevel@tonic-gate 	 * Prepare output file
6270Sstevel@tonic-gate 	 * If the input & output file are same,
6280Sstevel@tonic-gate 	 * the output is written to a temp
6290Sstevel@tonic-gate 	 * file first, then renamed to the original file
6300Sstevel@tonic-gate 	 * after the crypt operation
6310Sstevel@tonic-gate 	 */
6320Sstevel@tonic-gate 	inoutsame = B_FALSE;
6330Sstevel@tonic-gate 	if (oflag) {
6340Sstevel@tonic-gate 		outfilename = outputfile;
6350Sstevel@tonic-gate 		if ((stat(outputfile, &outsbuf) != -1) &&
6360Sstevel@tonic-gate 			(insbuf.st_ino == outsbuf.st_ino)) {
6370Sstevel@tonic-gate 			char *dir;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 			/* create temp file on same dir */
6400Sstevel@tonic-gate 			dir = dirname(outputfile);
6410Sstevel@tonic-gate 			(void) snprintf(tmpnam, sizeof (tmpnam),
6420Sstevel@tonic-gate 				"%s/encrXXXXXX", dir);
6430Sstevel@tonic-gate 			outfilename = tmpnam;
6440Sstevel@tonic-gate 			if ((outfd = mkstemp(tmpnam)) == -1) {
6450Sstevel@tonic-gate 				cryptoerror(LOG_STDERR, gettext(
6460Sstevel@tonic-gate 				    "cannot create temp file"));
6470Sstevel@tonic-gate 				goto cleanup;
6480Sstevel@tonic-gate 			}
6490Sstevel@tonic-gate 			inoutsame = B_TRUE;
6500Sstevel@tonic-gate 		} else {
6510Sstevel@tonic-gate 			/* Create file for output */
6520Sstevel@tonic-gate 			if ((outfd = open(outfilename,
6530Sstevel@tonic-gate 			    O_CREAT|O_WRONLY|O_TRUNC,
6540Sstevel@tonic-gate 					0644)) == -1) {
6550Sstevel@tonic-gate 				cryptoerror(LOG_STDERR, gettext(
6560Sstevel@tonic-gate 				    "cannot open output file %s"),
6570Sstevel@tonic-gate 				    outfilename);
6580Sstevel@tonic-gate 				goto cleanup;
6590Sstevel@tonic-gate 			}
6600Sstevel@tonic-gate 		}
6610Sstevel@tonic-gate 	}
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	/*
6640Sstevel@tonic-gate 	 * Read the version number from the head of the file
6650Sstevel@tonic-gate 	 * to know how to interpret the data that follows.
6660Sstevel@tonic-gate 	 */
6670Sstevel@tonic-gate 	if (cmd->type == CKA_DECRYPT) {
6680Sstevel@tonic-gate 		if (read(infd, &version, sizeof (version)) !=
6690Sstevel@tonic-gate 			sizeof (version)) {
6700Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
6710Sstevel@tonic-gate 			    "failed to get format version from "
6720Sstevel@tonic-gate 			    "input file."));
6730Sstevel@tonic-gate 			goto cleanup;
6740Sstevel@tonic-gate 		}
6750Sstevel@tonic-gate 		/* convert to host byte order */
6760Sstevel@tonic-gate 		version = ntohl(version);
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 		switch (version) {
6790Sstevel@tonic-gate 		case 1:
6800Sstevel@tonic-gate 		/*
6810Sstevel@tonic-gate 		 * Version 1 output format:
6820Sstevel@tonic-gate 		 *  - Iterations used in key gen function (4 bytes)
6830Sstevel@tonic-gate 		 *  - IV ( 'ivlen' bytes)
6840Sstevel@tonic-gate 		 *  - Salt data used in key gen (16 bytes)
6850Sstevel@tonic-gate 		 *
6860Sstevel@tonic-gate 		 * An encrypted file has IV as first block (0 or
6870Sstevel@tonic-gate 		 * more bytes depending on mechanism) followed
6880Sstevel@tonic-gate 		 * by cipher text.  Get the IV from the encrypted
6890Sstevel@tonic-gate 		 * file.
6900Sstevel@tonic-gate 		 */
6910Sstevel@tonic-gate 			/*
6920Sstevel@tonic-gate 			 * Read iteration count and salt data.
6930Sstevel@tonic-gate 			 */
6940Sstevel@tonic-gate 			if (read(infd, &iterations,
6950Sstevel@tonic-gate 				sizeof (iterations)) !=
6960Sstevel@tonic-gate 				sizeof (iterations)) {
6970Sstevel@tonic-gate 				cryptoerror(LOG_STDERR, gettext(
6980Sstevel@tonic-gate 					"failed to get iterations from "
6990Sstevel@tonic-gate 					"input file."));
7000Sstevel@tonic-gate 				goto cleanup;
7010Sstevel@tonic-gate 			}
7020Sstevel@tonic-gate 			/* convert to host byte order */
7030Sstevel@tonic-gate 			iterations = ntohl(iterations);
7040Sstevel@tonic-gate 			if (ivlen > 0 &&
7050Sstevel@tonic-gate 			    read(infd, pivbuf, ivlen) != ivlen) {
7060Sstevel@tonic-gate 				cryptoerror(LOG_STDERR, gettext(
7070Sstevel@tonic-gate 				    "failed to get initialization "
7080Sstevel@tonic-gate 				    "vector from input file."));
7090Sstevel@tonic-gate 				goto cleanup;
7100Sstevel@tonic-gate 			}
7110Sstevel@tonic-gate 			if (read(infd, salt, sizeof (salt))
7120Sstevel@tonic-gate 				!= sizeof (salt)) {
7130Sstevel@tonic-gate 				cryptoerror(LOG_STDERR, gettext(
7140Sstevel@tonic-gate 					"failed to get salt data from "
7150Sstevel@tonic-gate 					"input file."));
7160Sstevel@tonic-gate 				goto cleanup;
7170Sstevel@tonic-gate 			}
7180Sstevel@tonic-gate 			break;
7190Sstevel@tonic-gate 		default:
7200Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
7210Sstevel@tonic-gate 			"Unrecognized format version read from "
7220Sstevel@tonic-gate 			"input file - expected %d, got %d."),
7230Sstevel@tonic-gate 			SUNW_ENCRYPT_FILE_VERSION, version);
7240Sstevel@tonic-gate 			goto cleanup;
7250Sstevel@tonic-gate 			break;
7260Sstevel@tonic-gate 		}
7270Sstevel@tonic-gate 	}
7280Sstevel@tonic-gate 	/*
7290Sstevel@tonic-gate 	 * If encrypting, we need some random
7300Sstevel@tonic-gate 	 * salt data to create the key.  If decrypting,
7310Sstevel@tonic-gate 	 * the salt should come from head of the file
7320Sstevel@tonic-gate 	 * to be decrypted.
7330Sstevel@tonic-gate 	 */
7340Sstevel@tonic-gate 	if (cmd->type == CKA_ENCRYPT) {
7350Sstevel@tonic-gate 		rv = get_random_data(salt, sizeof (salt));
7360Sstevel@tonic-gate 		if (rv != 0) {
7370Sstevel@tonic-gate 			cryptoerror(LOG_STDERR,
7380Sstevel@tonic-gate 			gettext("unable to generate random "
7390Sstevel@tonic-gate 				"data for key salt."));
7400Sstevel@tonic-gate 			goto cleanup;
7410Sstevel@tonic-gate 		}
7420Sstevel@tonic-gate 	}
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	/*
7450Sstevel@tonic-gate 	 * If key input is read from  a file, treat it as
7460Sstevel@tonic-gate 	 * raw key data, unless it is to be used with RC4,
7470Sstevel@tonic-gate 	 * in which case it must be used to generate a pkcs5
7480Sstevel@tonic-gate 	 * key to address security concerns with RC4 keys.
7490Sstevel@tonic-gate 	 */
7500Sstevel@tonic-gate 	if (kflag && keyfile != NULL && keytype != CKK_RC4) {
7510Sstevel@tonic-gate 		CK_OBJECT_CLASS objclass = CKO_SECRET_KEY;
7520Sstevel@tonic-gate 		CK_ATTRIBUTE template[5];
7530Sstevel@tonic-gate 		int nattr = 0;
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 		template[nattr].type = CKA_CLASS;
7560Sstevel@tonic-gate 		template[nattr].pValue = &objclass;
7570Sstevel@tonic-gate 		template[nattr].ulValueLen = sizeof (objclass);
7580Sstevel@tonic-gate 		nattr++;
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 		template[nattr].type = CKA_KEY_TYPE;
7610Sstevel@tonic-gate 		template[nattr].pValue = &keytype;
7620Sstevel@tonic-gate 		template[nattr].ulValueLen = sizeof (keytype);
7630Sstevel@tonic-gate 		nattr++;
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 		template[nattr].type = cmd->type;
7660Sstevel@tonic-gate 		template[nattr].pValue = &truevalue;
7670Sstevel@tonic-gate 		template[nattr].ulValueLen = sizeof (truevalue);
7680Sstevel@tonic-gate 		nattr++;
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 		template[nattr].type = CKA_TOKEN;
7710Sstevel@tonic-gate 		template[nattr].pValue = &falsevalue;
7720Sstevel@tonic-gate 		template[nattr].ulValueLen = sizeof (falsevalue);
7730Sstevel@tonic-gate 		nattr++;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 		template[nattr].type = CKA_VALUE;
7760Sstevel@tonic-gate 		template[nattr].pValue = pkeydata;
7770Sstevel@tonic-gate 		template[nattr].ulValueLen = keysize;
7780Sstevel@tonic-gate 		nattr++;
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 		rv = C_CreateObject(hSession, template,
7810Sstevel@tonic-gate 			nattr, &key);
7820Sstevel@tonic-gate 	} else {
7830Sstevel@tonic-gate 		/*
7840Sstevel@tonic-gate 		 * If the encryption type has a fixed key length,
7850Sstevel@tonic-gate 		 * then its not necessary to set the key length
7860Sstevel@tonic-gate 		 * parameter when generating the key.
7870Sstevel@tonic-gate 		 */
7880Sstevel@tonic-gate 		if (keytype == CKK_DES || keytype == CKK_DES3)
7890Sstevel@tonic-gate 			keylen = 0;
7900Sstevel@tonic-gate 		else
7910Sstevel@tonic-gate 			keylen = 16;
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 		/*
7940Sstevel@tonic-gate 		 * Generate a cryptographically secure key using
7950Sstevel@tonic-gate 		 * the key read from the file given (-k keyfile) or
7960Sstevel@tonic-gate 		 * the passphrase entered by the user.
7970Sstevel@tonic-gate 		 */
7980Sstevel@tonic-gate 		rv = generate_pkcs5_key(hSession,
7990Sstevel@tonic-gate 			salt, sizeof (salt),
8000Sstevel@tonic-gate 			iterations,
8010Sstevel@tonic-gate 			pkeydata, keytype, keysize,
8020Sstevel@tonic-gate 			keylen, cmd->type, &key);
8030Sstevel@tonic-gate 	}
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	if (rv != CKR_OK) {
8060Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
8070Sstevel@tonic-gate 		    "failed to generate a key: %s"),
8080Sstevel@tonic-gate 		    pkcs11_strerror(rv));
8090Sstevel@tonic-gate 		goto cleanup;
8100Sstevel@tonic-gate 	}
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	/* Setup up mechanism */
8130Sstevel@tonic-gate 	mech.mechanism = mech_type;
8140Sstevel@tonic-gate 	mech.pParameter = (CK_VOID_PTR)pivbuf;
8150Sstevel@tonic-gate 	mech.ulParameterLen = ivlen;
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	if ((rv = cmd->Init(hSession, &mech, key)) != CKR_OK) {
8180Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
8190Sstevel@tonic-gate 		    "failed to initialize crypto operation: %s"),
8200Sstevel@tonic-gate 		    pkcs11_strerror(rv));
8210Sstevel@tonic-gate 		goto cleanup;
8220Sstevel@tonic-gate 	}
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	/* Write the version header encrypt command */
8250Sstevel@tonic-gate 	if (cmd->type == CKA_ENCRYPT) {
8260Sstevel@tonic-gate 		/* convert to network order for storage */
8270Sstevel@tonic-gate 		int netversion = htonl(version);
8280Sstevel@tonic-gate 		CK_ULONG netiter;
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 		if (write(outfd, &netversion, sizeof (netversion))
8310Sstevel@tonic-gate 			!= sizeof (netversion)) {
8320Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
8330Sstevel@tonic-gate 			"failed to write version number "
8340Sstevel@tonic-gate 			"to output file."));
8350Sstevel@tonic-gate 			goto cleanup;
8360Sstevel@tonic-gate 		}
8370Sstevel@tonic-gate 		/*
8380Sstevel@tonic-gate 		 * Write the iteration and salt data, even if they
8390Sstevel@tonic-gate 		 * were not used to generate a key.
8400Sstevel@tonic-gate 		 */
8410Sstevel@tonic-gate 		netiter = htonl(iterations);
8420Sstevel@tonic-gate 		if (write(outfd, &netiter,
8430Sstevel@tonic-gate 			sizeof (netiter)) != sizeof (netiter)) {
8440Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
8450Sstevel@tonic-gate 			    "failed to write iterations to output"));
8460Sstevel@tonic-gate 			goto cleanup;
8470Sstevel@tonic-gate 		}
8480Sstevel@tonic-gate 		if (ivlen > 0 &&
8490Sstevel@tonic-gate 			write(outfd, pivbuf, ivlen) != ivlen) {
8500Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
8510Sstevel@tonic-gate 				"failed to write initialization vector "
8520Sstevel@tonic-gate 				"to output"));
8530Sstevel@tonic-gate 			goto cleanup;
8540Sstevel@tonic-gate 		}
8550Sstevel@tonic-gate 		if (write(outfd, salt, sizeof (salt)) != sizeof (salt)) {
8560Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
8570Sstevel@tonic-gate 			    "failed to write salt data to output"));
8580Sstevel@tonic-gate 			goto cleanup;
8590Sstevel@tonic-gate 		}
8600Sstevel@tonic-gate 	}
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	if (crypt_multipart(cmd, hSession, infd, outfd, insbuf) == -1) {
8630Sstevel@tonic-gate 		goto cleanup;
8640Sstevel@tonic-gate 	}
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	errflag = B_FALSE;
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	/*
8690Sstevel@tonic-gate 	 * Clean up
8700Sstevel@tonic-gate 	 */
8710Sstevel@tonic-gate cleanup:
8720Sstevel@tonic-gate 	/* Clear the key data, so others cannot snoop */
8730Sstevel@tonic-gate 	if (pkeydata != NULL) {
8740Sstevel@tonic-gate 		bzero(pkeydata, keysize);
8750Sstevel@tonic-gate 		free(pkeydata);
8760Sstevel@tonic-gate 		pkeydata = NULL;
8770Sstevel@tonic-gate 	}
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	/* Destroy key object */
8800Sstevel@tonic-gate 	if (key != (CK_OBJECT_HANDLE) 0) {
8810Sstevel@tonic-gate 		(void) C_DestroyObject(hSession, key);
8820Sstevel@tonic-gate 	}
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	/* free allocated memory */
8850Sstevel@tonic-gate 	if (pSlotList != NULL)
8860Sstevel@tonic-gate 		free(pSlotList);
8870Sstevel@tonic-gate 	if (pivbuf != NULL)
8880Sstevel@tonic-gate 		free(pivbuf);
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	/* close all the files */
8910Sstevel@tonic-gate 	if (infd != -1)
8920Sstevel@tonic-gate 		(void) close(infd);
8930Sstevel@tonic-gate 	if (outfd != -1)
8940Sstevel@tonic-gate 		(void) close(outfd);
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	/* rename tmp output to input file */
8970Sstevel@tonic-gate 	if (inoutsame) {
8980Sstevel@tonic-gate 		if (rename(outfilename, inputfile) == -1) {
8990Sstevel@tonic-gate 			(void) unlink(outfilename);
9000Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext("rename failed."));
9010Sstevel@tonic-gate 		}
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	/* If error occurred, remove the output file */
9050Sstevel@tonic-gate 	if (errflag && outfilename != NULL) {
9060Sstevel@tonic-gate 		(void) unlink(outfilename);
9070Sstevel@tonic-gate 	}
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	/* close pkcs11 session */
9100Sstevel@tonic-gate 	if (hSession != CK_INVALID_HANDLE)
9110Sstevel@tonic-gate 		(void) C_CloseSession(hSession);
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 	(void) C_Finalize(NULL);
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	return (errflag);
9160Sstevel@tonic-gate }
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate /*
9190Sstevel@tonic-gate  * Function for printing progress bar when the verbose flag
9200Sstevel@tonic-gate  * is set.
9210Sstevel@tonic-gate  *
9220Sstevel@tonic-gate  * The vertical bar is printed at 25, 50, and 75% complete.
9230Sstevel@tonic-gate  *
9240Sstevel@tonic-gate  * The function is passed the number of positions on the screen it needs to
9250Sstevel@tonic-gate  * advance and loops.
9260Sstevel@tonic-gate  */
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate static void
9290Sstevel@tonic-gate print_status(int pos_to_advance)
9300Sstevel@tonic-gate {
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	while (pos_to_advance > 0) {
9330Sstevel@tonic-gate 		switch (status_pos) {
9340Sstevel@tonic-gate 		case 0:
9350Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("["));
9360Sstevel@tonic-gate 			break;
9370Sstevel@tonic-gate 		case 19:
9380Sstevel@tonic-gate 		case 39:
9390Sstevel@tonic-gate 		case 59:
9400Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("|"));
9410Sstevel@tonic-gate 			break;
9420Sstevel@tonic-gate 		default:
9430Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("."));
9440Sstevel@tonic-gate 		}
9450Sstevel@tonic-gate 		pos_to_advance--;
9460Sstevel@tonic-gate 		status_pos++;
9470Sstevel@tonic-gate 	}
9480Sstevel@tonic-gate }
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate /*
9510Sstevel@tonic-gate  * Encrypt/Decrypt in multi part.
9520Sstevel@tonic-gate  *
9530Sstevel@tonic-gate  * This function reads the input file (infd) and writes the
9540Sstevel@tonic-gate  * encrypted/decrypted output to file (outfd).
9550Sstevel@tonic-gate  *
9560Sstevel@tonic-gate  * cmd - pointing  to commandinfo
9570Sstevel@tonic-gate  * hSession - pkcs session
9580Sstevel@tonic-gate  * infd - input file descriptor
9590Sstevel@tonic-gate  * outfd - output file descriptor
9600Sstevel@tonic-gate  *
9610Sstevel@tonic-gate  */
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate static int
9640Sstevel@tonic-gate crypt_multipart(struct CommandInfo *cmd, CK_SESSION_HANDLE hSession,
9650Sstevel@tonic-gate 	int infd, int outfd, struct stat in)
9660Sstevel@tonic-gate {
9670Sstevel@tonic-gate 	CK_RV		rv;
9680Sstevel@tonic-gate 	CK_ULONG	resultlen;
9690Sstevel@tonic-gate 	CK_ULONG	resultbuflen;
9700Sstevel@tonic-gate 	CK_BYTE_PTR	resultbuf;
9710Sstevel@tonic-gate 	CK_ULONG	datalen;
9720Sstevel@tonic-gate 	CK_BYTE		databuf[BUFFERSIZE];
9730Sstevel@tonic-gate 	CK_BYTE		outbuf[BUFFERSIZE+BLOCKSIZE];
9740Sstevel@tonic-gate 	CK_ULONG	status_index = 0; /* current total file size read */
9750Sstevel@tonic-gate 	float		status_last = 0.0; /* file size of last element used */
9760Sstevel@tonic-gate 	float		status_incr = 0.0; /* file size element increments */
9770Sstevel@tonic-gate 	int		pos; /* # of progress bar elements to be print */
9780Sstevel@tonic-gate 	ssize_t		nread;
9790Sstevel@tonic-gate 	boolean_t	errflag = B_FALSE;
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	datalen = sizeof (databuf);
9820Sstevel@tonic-gate 	resultbuflen = sizeof (outbuf);
9830Sstevel@tonic-gate 	resultbuf = outbuf;
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	/* Divide into 79 increments for progress bar element spacing */
9860Sstevel@tonic-gate 	if (vflag && iflag)
9870Sstevel@tonic-gate 		status_incr = (in.st_size / 79.0);
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	while ((nread = read(infd, databuf, datalen)) > 0) {
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 		/* Start with the initial buffer */
9920Sstevel@tonic-gate 		resultlen = resultbuflen;
9930Sstevel@tonic-gate 		rv = cmd->Update(hSession, databuf, (CK_ULONG)nread,
9940Sstevel@tonic-gate 			resultbuf, &resultlen);
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 		/* Need a bigger buffer? */
9970Sstevel@tonic-gate 		if (rv == CKR_BUFFER_TOO_SMALL) {
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 			/* free the old buffer */
10000Sstevel@tonic-gate 			if (resultbuf != NULL && resultbuf != outbuf) {
10010Sstevel@tonic-gate 				bzero(resultbuf, resultbuflen);
10020Sstevel@tonic-gate 				free(resultbuf);
10030Sstevel@tonic-gate 			}
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 			/* allocate a new big buffer */
10060Sstevel@tonic-gate 			if ((resultbuf = malloc((size_t)resultlen)) == NULL) {
10070Sstevel@tonic-gate 				int err = errno;
10080Sstevel@tonic-gate 				cryptoerror(LOG_STDERR, gettext("malloc: %s"),
10090Sstevel@tonic-gate 				    strerror(err));
10100Sstevel@tonic-gate 				return (-1);
10110Sstevel@tonic-gate 			}
10120Sstevel@tonic-gate 			resultbuflen = resultlen;
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 			/* Try again with bigger buffer */
10150Sstevel@tonic-gate 			rv = cmd->Update(hSession, databuf, (CK_ULONG)nread,
10160Sstevel@tonic-gate 				resultbuf, &resultlen);
10170Sstevel@tonic-gate 		}
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 		if (rv != CKR_OK) {
10200Sstevel@tonic-gate 			errflag = B_TRUE;
10210Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
10220Sstevel@tonic-gate 			    "crypto operation failed: %s"),
10230Sstevel@tonic-gate 			    pkcs11_strerror(rv));
10240Sstevel@tonic-gate 			break;
10250Sstevel@tonic-gate 		}
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 		/* write the output */
10280Sstevel@tonic-gate 		if (write(outfd, resultbuf, resultlen) != resultlen) {
10290Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
10300Sstevel@tonic-gate 			    "failed to write result to output file."));
10310Sstevel@tonic-gate 			errflag = B_TRUE;
10320Sstevel@tonic-gate 			break;
10330Sstevel@tonic-gate 		}
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 		if (vflag) {
10360Sstevel@tonic-gate 			status_index += resultlen;
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 			/*
10390Sstevel@tonic-gate 			 * If input is from stdin, do a our own progress bar
10400Sstevel@tonic-gate 			 * by printing periods at a pre-defined increment
10410Sstevel@tonic-gate 			 * until the file is done.
10420Sstevel@tonic-gate 			 */
10430Sstevel@tonic-gate 			if (!iflag) {
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 				/*
10460Sstevel@tonic-gate 				 * Print at least 1 element in case the file
10470Sstevel@tonic-gate 				 * is small, it looks better than nothing.
10480Sstevel@tonic-gate 				 */
10490Sstevel@tonic-gate 				if (status_pos == 0) {
10500Sstevel@tonic-gate 					(void) fprintf(stderr, gettext("."));
10510Sstevel@tonic-gate 					status_pos = 1;
10520Sstevel@tonic-gate 				}
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 				if ((status_index - status_last) >
10550Sstevel@tonic-gate 				    (PROGRESSSIZE)) {
10560Sstevel@tonic-gate 					(void) fprintf(stderr, gettext("."));
10570Sstevel@tonic-gate 					status_last = status_index;
10580Sstevel@tonic-gate 				}
10590Sstevel@tonic-gate 				continue;
10600Sstevel@tonic-gate 			}
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 			/* Calculate the number of elements need to be print */
10630Sstevel@tonic-gate 			if (in.st_size <= BUFFERSIZE)
10640Sstevel@tonic-gate 				pos = 78;
10650Sstevel@tonic-gate 			else
10660Sstevel@tonic-gate 				pos = (int)((status_index - status_last) /
10670Sstevel@tonic-gate 				    status_incr);
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 			/* Add progress bar elements, if needed */
10700Sstevel@tonic-gate 			if (pos > 0) {
10710Sstevel@tonic-gate 				print_status(pos);
10720Sstevel@tonic-gate 				status_last += (status_incr * pos);
10730Sstevel@tonic-gate 			}
10740Sstevel@tonic-gate 		}
10750Sstevel@tonic-gate 	}
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	/* Print verbose completion */
10780Sstevel@tonic-gate 	if (vflag) {
10790Sstevel@tonic-gate 		if (iflag)
10800Sstevel@tonic-gate 			(void) fprintf(stderr, "]");
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 		(void) fprintf(stderr, "\n%s\n", gettext("Done."));
10830Sstevel@tonic-gate 	}
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	/* Error in reading */
10860Sstevel@tonic-gate 	if (nread == -1) {
10870Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
10880Sstevel@tonic-gate 		    "error reading from input file"));
10890Sstevel@tonic-gate 		errflag = B_TRUE;
10900Sstevel@tonic-gate 	}
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 	if (!errflag) {
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 		/* Do the final part */
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 		rv = cmd->Final(hSession, resultbuf, &resultlen);
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 		if (rv == CKR_OK) {
10990Sstevel@tonic-gate 			/* write the output */
11000Sstevel@tonic-gate 			if (write(outfd, resultbuf, resultlen) != resultlen) {
11010Sstevel@tonic-gate 				cryptoerror(LOG_STDERR, gettext(
11020Sstevel@tonic-gate 				    "failed to write result to output file."));
11030Sstevel@tonic-gate 				errflag = B_TRUE;
11040Sstevel@tonic-gate 			}
11050Sstevel@tonic-gate 		} else {
11060Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
11070Sstevel@tonic-gate 			    "crypto operation failed: %s"),
11080Sstevel@tonic-gate 			    pkcs11_strerror(rv));
11090Sstevel@tonic-gate 			errflag = B_TRUE;
11100Sstevel@tonic-gate 		}
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	}
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 	if (resultbuf != NULL && resultbuf != outbuf) {
11150Sstevel@tonic-gate 		bzero(resultbuf, resultbuflen);
11160Sstevel@tonic-gate 		free(resultbuf);
11170Sstevel@tonic-gate 	}
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	if (errflag) {
11200Sstevel@tonic-gate 		return (-1);
11210Sstevel@tonic-gate 	} else {
11220Sstevel@tonic-gate 		return (0);
11230Sstevel@tonic-gate 	}
11240Sstevel@tonic-gate }
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate /*
11270Sstevel@tonic-gate  * cryptoreadfile - reads file into a buffer
11280Sstevel@tonic-gate  *  This function can be used for reading files
11290Sstevel@tonic-gate  *  containing key or initialization vector data.
11300Sstevel@tonic-gate  *
11310Sstevel@tonic-gate  *  filename - name of file
11320Sstevel@tonic-gate  *  pdata - entire file returned in this buffer
11330Sstevel@tonic-gate  *	must be freed by caller using free()
11340Sstevel@tonic-gate  *  pdatalen - length of data returned
11350Sstevel@tonic-gate  *
11360Sstevel@tonic-gate  * returns 0 if success, -1 if error
11370Sstevel@tonic-gate  */
11380Sstevel@tonic-gate static int
11390Sstevel@tonic-gate cryptoreadfile(char *filename, CK_BYTE_PTR *pdata, CK_ULONG_PTR pdatalen)
11400Sstevel@tonic-gate {
11410Sstevel@tonic-gate 	struct stat statbuf;
11420Sstevel@tonic-gate 	char *filebuf;
11430Sstevel@tonic-gate 	int filesize;
11440Sstevel@tonic-gate 	int fd;
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	if (filename == NULL)
11470Sstevel@tonic-gate 		return (-1);
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 	/* read the file into a buffer */
11500Sstevel@tonic-gate 	if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) == -1) {
11510Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
11520Sstevel@tonic-gate 			"cannot open %s"), filename);
11530Sstevel@tonic-gate 		return (-1);
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	}
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	if (fstat(fd, &statbuf) == -1) {
11580Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
11590Sstevel@tonic-gate 			"cannot stat %s"), filename);
11600Sstevel@tonic-gate 		(void) close(fd);
11610Sstevel@tonic-gate 		return (-1);
11620Sstevel@tonic-gate 	}
11630Sstevel@tonic-gate 
1164*871Scasper 	if (!S_ISREG(statbuf.st_mode)) {
11650Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
11660Sstevel@tonic-gate 			"%s not a regular file"), filename);
11670Sstevel@tonic-gate 		(void) close(fd);
11680Sstevel@tonic-gate 		return (-1);
11690Sstevel@tonic-gate 	}
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	filesize = (size_t)statbuf.st_size;
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 	if (filesize == 0) {
11740Sstevel@tonic-gate 		(void) close(fd);
11750Sstevel@tonic-gate 		return (-1);
11760Sstevel@tonic-gate 	}
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	/* allocate a buffer to hold the entire key */
11790Sstevel@tonic-gate 	if ((filebuf = malloc(filesize)) == NULL) {
11800Sstevel@tonic-gate 		int err = errno;
11810Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("malloc: %s"), strerror(err));
11820Sstevel@tonic-gate 		(void) close(fd);
11830Sstevel@tonic-gate 		return (-1);
11840Sstevel@tonic-gate 	}
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 	if (read(fd, filebuf, filesize) != filesize) {
11870Sstevel@tonic-gate 		int err = errno;
11880Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("error reading file: %s"),
11890Sstevel@tonic-gate 		    strerror(err));
11900Sstevel@tonic-gate 		(void) close(fd);
11910Sstevel@tonic-gate 		free(filebuf);
11920Sstevel@tonic-gate 		return (-1);
11930Sstevel@tonic-gate 	}
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	(void) close(fd);
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 	*pdata = (CK_BYTE_PTR)filebuf;
11980Sstevel@tonic-gate 	*pdatalen = (CK_ULONG)filesize;
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	return (0);
12010Sstevel@tonic-gate }
12020Sstevel@tonic-gate /*
12030Sstevel@tonic-gate  * cryptogetkey - prompt user for a key
12040Sstevel@tonic-gate  *
12050Sstevel@tonic-gate  *   pkeydata - buffer for returning key data
12060Sstevel@tonic-gate  *	must be freed by caller using free()
12070Sstevel@tonic-gate  *   pkeysize - size of buffer returned
12080Sstevel@tonic-gate  *
12090Sstevel@tonic-gate  * returns
12100Sstevel@tonic-gate  *   0 for success, -1 for failure
12110Sstevel@tonic-gate  */
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate static int
12140Sstevel@tonic-gate cryptogetkey(CK_BYTE_PTR *pkeydata, CK_ULONG_PTR pkeysize)
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate {
12170Sstevel@tonic-gate 	char *keybuf;
12180Sstevel@tonic-gate 	char *tmpbuf;
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 	tmpbuf = getpassphrase(gettext("Enter key:"));
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 	if (tmpbuf == NULL) {
12250Sstevel@tonic-gate 		return (-1);	/* error */
12260Sstevel@tonic-gate 	} else {
12270Sstevel@tonic-gate 		keybuf = strdup(tmpbuf);
12280Sstevel@tonic-gate 		(void) memset(tmpbuf, 0, strlen(tmpbuf));
12290Sstevel@tonic-gate 	}
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	*pkeydata = (CK_BYTE_PTR)keybuf;
12320Sstevel@tonic-gate 	*pkeysize = (CK_ULONG)strlen(keybuf);
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	return (0);
12360Sstevel@tonic-gate }
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate /*
12390Sstevel@tonic-gate  * get_random_data - generate initialization vector data
12400Sstevel@tonic-gate  *             iv data is random bytes
12410Sstevel@tonic-gate  *  hSession - a pkcs session
12420Sstevel@tonic-gate  *  pivbuf - buffer where data is returned
12430Sstevel@tonic-gate  *  ivlen - size of iv data
12440Sstevel@tonic-gate  */
12450Sstevel@tonic-gate static int
12460Sstevel@tonic-gate get_random_data(CK_BYTE_PTR pivbuf, int ivlen)
12470Sstevel@tonic-gate {
12480Sstevel@tonic-gate 	int fd;
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate 	if (ivlen == 0) {
12510Sstevel@tonic-gate 		/* nothing to generate */
12520Sstevel@tonic-gate 		return (0);
12530Sstevel@tonic-gate 	}
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 	/* Read random data directly from /dev/random */
12560Sstevel@tonic-gate 	if ((fd = open(RANDOM_DEVICE, O_RDONLY)) != -1) {
12570Sstevel@tonic-gate 		if (read(fd, pivbuf, (size_t)ivlen) == ivlen) {
12580Sstevel@tonic-gate 			(void) close(fd);
12590Sstevel@tonic-gate 			return (0);
12600Sstevel@tonic-gate 		}
12610Sstevel@tonic-gate 	}
12620Sstevel@tonic-gate 	(void) close(fd);
12630Sstevel@tonic-gate 	return (-1);
12640Sstevel@tonic-gate }
1265