xref: /titanic_53/usr/src/cmd/cmd-crypto/decrypt/decrypt.c (revision 4bc0a2ef2b7ba50a7a717e7ddbf31472ad28e358)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * decrypt.c
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * Implements encrypt(1) and decrypt(1) commands
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * One binary performs both encrypt/decrypt operation.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * usage:
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  *  algorithm - mechanism name without CKM_ prefix. Case
397c478bd9Sstevel@tonic-gate  *              does not matter
407c478bd9Sstevel@tonic-gate  *  keyfile - file containing key data. If not specified user is
417c478bd9Sstevel@tonic-gate  *            prompted to enter key. key length > 0 is required
427c478bd9Sstevel@tonic-gate  *  infile  - input file to encrypt/decrypt. If omitted, stdin used.
437c478bd9Sstevel@tonic-gate  *  outfile - output file to encrypt/decrypt. If omitted, stdout used.
447c478bd9Sstevel@tonic-gate  *            if infile & outfile are same, a temp file is used for
457c478bd9Sstevel@tonic-gate  *            output and infile is replaced with this file after
467c478bd9Sstevel@tonic-gate  *            operation is complete.
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * Implementation notes:
497c478bd9Sstevel@tonic-gate  *   iv data - It is generated by random bytes equal to one block size.
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  *   encrypted output format -
527c478bd9Sstevel@tonic-gate  *   - Output format version number - 4 bytes in network byte order.
537c478bd9Sstevel@tonic-gate  *   - Iterations used in key gen function, 4 bytes in  network byte order.
547c478bd9Sstevel@tonic-gate  *   - IV ( 'ivlen' bytes)
557c478bd9Sstevel@tonic-gate  *   - Salt data used in key gen (16 bytes)
567c478bd9Sstevel@tonic-gate  *   - cipher text data.
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include <stdio.h>
617c478bd9Sstevel@tonic-gate #include <stdlib.h>
627c478bd9Sstevel@tonic-gate #include <unistd.h>
637c478bd9Sstevel@tonic-gate #include <errno.h>
647c478bd9Sstevel@tonic-gate #include <fcntl.h>
657c478bd9Sstevel@tonic-gate #include <ctype.h>
667c478bd9Sstevel@tonic-gate #include <strings.h>
677c478bd9Sstevel@tonic-gate #include <libintl.h>
687c478bd9Sstevel@tonic-gate #include <libgen.h>
697c478bd9Sstevel@tonic-gate #include <locale.h>
707c478bd9Sstevel@tonic-gate #include <limits.h>
717c478bd9Sstevel@tonic-gate #include <sys/types.h>
727c478bd9Sstevel@tonic-gate #include <sys/stat.h>
737c478bd9Sstevel@tonic-gate #include <netinet/in.h>
747c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
757c478bd9Sstevel@tonic-gate #include <cryptoutil.h>
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #define	BUFFERSIZE	(2048)		/* Buffer size for reading file */
787c478bd9Sstevel@tonic-gate #define	BLOCKSIZE	(128)		/* Largest guess for block size */
797c478bd9Sstevel@tonic-gate #define	PROGRESSSIZE	(BUFFERSIZE*20)	/* stdin progress indicator size */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #define	PBKD2_ITERATIONS (1000)
827c478bd9Sstevel@tonic-gate #define	PBKD2_SALT_SIZE	16
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #define	SUNW_ENCRYPT_FILE_VERSION 1
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * Exit Status codes
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate #ifndef EXIT_SUCCESS
907c478bd9Sstevel@tonic-gate #define	EXIT_SUCCESS	0	/* No errors */
917c478bd9Sstevel@tonic-gate #define	EXIT_FAILURE	1	/* All errors except usage */
927c478bd9Sstevel@tonic-gate #endif /* EXIT_SUCCESS */
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #define	EXIT_USAGE	2	/* usage/syntax error */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #define	RANDOM_DEVICE	"/dev/urandom"	/* random device name */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate #define	ENCRYPT_NAME	"encrypt"	/* name of encrypt command */
997c478bd9Sstevel@tonic-gate #define	ENCRYPT_OPTIONS "a:k:i:o:lv"	/* options for encrypt */
1007c478bd9Sstevel@tonic-gate #define	DECRYPT_NAME	"decrypt"	/* name of decrypt command */
1017c478bd9Sstevel@tonic-gate #define	DECRYPT_OPTIONS "a:k:i:o:lv"	/* options for decrypt */
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  * Structure containing info for encrypt/decrypt
1057c478bd9Sstevel@tonic-gate  * command
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate struct CommandInfo {
1087c478bd9Sstevel@tonic-gate 	char		*name;		/* name of the command */
1097c478bd9Sstevel@tonic-gate 	char		*options;	/* command line options */
1107c478bd9Sstevel@tonic-gate 	CK_FLAGS	flags;
1117c478bd9Sstevel@tonic-gate 	CK_ATTRIBUTE_TYPE type;		/* type of command */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	/* function pointers for various operations */
1147c478bd9Sstevel@tonic-gate 	CK_RV	(*Init)(CK_SESSION_HANDLE, CK_MECHANISM_PTR, CK_OBJECT_HANDLE);
1157c478bd9Sstevel@tonic-gate 	CK_RV	(*Update)(CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG, CK_BYTE_PTR,
1167c478bd9Sstevel@tonic-gate 		CK_ULONG_PTR);
1177c478bd9Sstevel@tonic-gate 	CK_RV	(*Crypt)(CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG, CK_BYTE_PTR,
1187c478bd9Sstevel@tonic-gate 		CK_ULONG_PTR);
1197c478bd9Sstevel@tonic-gate 	CK_RV	(*Final)(CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG_PTR);
1207c478bd9Sstevel@tonic-gate };
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate static struct CommandInfo encrypt_cmd = {
1237c478bd9Sstevel@tonic-gate 	ENCRYPT_NAME,
1247c478bd9Sstevel@tonic-gate 	ENCRYPT_OPTIONS,
1257c478bd9Sstevel@tonic-gate 	CKF_ENCRYPT,
1267c478bd9Sstevel@tonic-gate 	CKA_ENCRYPT,
1277c478bd9Sstevel@tonic-gate 	C_EncryptInit,
1287c478bd9Sstevel@tonic-gate 	C_EncryptUpdate,
1297c478bd9Sstevel@tonic-gate 	C_Encrypt,
1307c478bd9Sstevel@tonic-gate 	C_EncryptFinal
1317c478bd9Sstevel@tonic-gate };
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static struct CommandInfo decrypt_cmd = {
1347c478bd9Sstevel@tonic-gate 	DECRYPT_NAME,
1357c478bd9Sstevel@tonic-gate 	DECRYPT_OPTIONS,
1367c478bd9Sstevel@tonic-gate 	CKF_DECRYPT,
1377c478bd9Sstevel@tonic-gate 	CKA_DECRYPT,
1387c478bd9Sstevel@tonic-gate 	C_DecryptInit,
1397c478bd9Sstevel@tonic-gate 	C_DecryptUpdate,
1407c478bd9Sstevel@tonic-gate 	C_Decrypt,
1417c478bd9Sstevel@tonic-gate 	C_DecryptFinal
1427c478bd9Sstevel@tonic-gate };
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate struct mech_alias {
1457c478bd9Sstevel@tonic-gate 	CK_MECHANISM_TYPE type;
1467c478bd9Sstevel@tonic-gate 	char *alias;
1477c478bd9Sstevel@tonic-gate 	CK_ULONG keysize_min;
1487c478bd9Sstevel@tonic-gate 	CK_ULONG keysize_max;
1497c478bd9Sstevel@tonic-gate 	int keysize_unit;
1507c478bd9Sstevel@tonic-gate 	int ivlen;
1517c478bd9Sstevel@tonic-gate 	boolean_t available;
1527c478bd9Sstevel@tonic-gate };
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate #define	MECH_ALIASES_COUNT 4
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate static struct mech_alias mech_aliases[] = {
1577c478bd9Sstevel@tonic-gate 	{ CKM_AES_CBC_PAD, "aes", ULONG_MAX, 0L, 8, 16, B_FALSE },
1587c478bd9Sstevel@tonic-gate 	{ CKM_RC4, "arcfour", ULONG_MAX, 0L, 1, 0, B_FALSE },
1597c478bd9Sstevel@tonic-gate 	{ CKM_DES_CBC_PAD, "des", 8, 8, 8, 8, B_FALSE },
1607c478bd9Sstevel@tonic-gate 	{ CKM_DES3_CBC_PAD, "3des", 24, 24, 8, 8, B_FALSE },
1617c478bd9Sstevel@tonic-gate };
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate static CK_BBOOL truevalue = TRUE;
1647c478bd9Sstevel@tonic-gate static CK_BBOOL falsevalue = FALSE;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate static boolean_t aflag = B_FALSE; /* -a <algorithm> flag, required */
1677c478bd9Sstevel@tonic-gate static boolean_t kflag = B_FALSE; /* -k <keyfile> flag */
1687c478bd9Sstevel@tonic-gate static boolean_t iflag = B_FALSE; /* -i <infile> flag, use stdin if absent */
1697c478bd9Sstevel@tonic-gate static boolean_t oflag = B_FALSE; /* -o <outfile> flag, use stdout if absent */
1707c478bd9Sstevel@tonic-gate static boolean_t lflag = B_FALSE; /* -l flag (list) */
1717c478bd9Sstevel@tonic-gate static boolean_t vflag = B_FALSE; /* -v flag (verbose) */
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate static char *keyfile = NULL;	/* name of keyfile */
1747c478bd9Sstevel@tonic-gate static char *inputfile = NULL;	/* name of input file */
1757c478bd9Sstevel@tonic-gate static char *outputfile = NULL;	/* name of output file */
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate static int status_pos = 0; /* current position of progress bar element */
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate  * function prototypes
1817c478bd9Sstevel@tonic-gate  */
1827c478bd9Sstevel@tonic-gate static void usage(struct CommandInfo *cmd);
1837c478bd9Sstevel@tonic-gate static int execute_cmd(struct CommandInfo *cmd, char *algo_str);
1847c478bd9Sstevel@tonic-gate static int cryptogetkey(CK_BYTE_PTR *pkeydata, CK_ULONG_PTR pkeysize);
1857c478bd9Sstevel@tonic-gate static int cryptoreadfile(char *filename, CK_BYTE_PTR *pdata,
1867c478bd9Sstevel@tonic-gate 	CK_ULONG_PTR pdatalen);
1877c478bd9Sstevel@tonic-gate static int get_random_data(CK_BYTE_PTR pivbuf, int ivlen);
1887c478bd9Sstevel@tonic-gate static int crypt_multipart(struct CommandInfo *cmd, CK_SESSION_HANDLE hSession,
1897c478bd9Sstevel@tonic-gate 	int infd, int outfd, struct stat in);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate int
1927c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	extern char *optarg;
1967c478bd9Sstevel@tonic-gate 	extern int optind;
1977c478bd9Sstevel@tonic-gate 	char *optstr;
1987c478bd9Sstevel@tonic-gate 	char c;			/* current getopts flag */
1997c478bd9Sstevel@tonic-gate 	char *algo_str = NULL;	/* algorithm string */
2007c478bd9Sstevel@tonic-gate 	struct CommandInfo *cmd;
2017c478bd9Sstevel@tonic-gate 	char *cmdname;		/* name of command */
2027c478bd9Sstevel@tonic-gate 	boolean_t errflag = B_FALSE;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2057c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defiend by cc -D */
2067c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
2077c478bd9Sstevel@tonic-gate #endif
2087c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	/*
2117c478bd9Sstevel@tonic-gate 	 * Based on command name, determine
2127c478bd9Sstevel@tonic-gate 	 * type of command.
2137c478bd9Sstevel@tonic-gate 	 */
2147c478bd9Sstevel@tonic-gate 	cmdname = basename(argv[0]);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	cryptodebug_init(cmdname);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	if (strcmp(cmdname, encrypt_cmd.name) == 0) {
2197c478bd9Sstevel@tonic-gate 		cmd = &encrypt_cmd;
2207c478bd9Sstevel@tonic-gate 	} else if (strcmp(cmdname, decrypt_cmd.name) == 0) {
2217c478bd9Sstevel@tonic-gate 		cmd = &decrypt_cmd;
2227c478bd9Sstevel@tonic-gate 	} else {
2237c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
2247c478bd9Sstevel@tonic-gate 		    "command name must be either encrypt or decrypt"));
2257c478bd9Sstevel@tonic-gate 		exit(EXIT_USAGE);
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	optstr = cmd->options;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	/* Parse command line arguments */
2317c478bd9Sstevel@tonic-gate 	while (!errflag && (c = getopt(argc, argv, optstr)) != -1) {
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		switch (c) {
2347c478bd9Sstevel@tonic-gate 		case 'a':
2357c478bd9Sstevel@tonic-gate 			aflag = B_TRUE;
2367c478bd9Sstevel@tonic-gate 			algo_str = optarg;
2377c478bd9Sstevel@tonic-gate 			break;
2387c478bd9Sstevel@tonic-gate 		case 'k':
2397c478bd9Sstevel@tonic-gate 			kflag = B_TRUE;
2407c478bd9Sstevel@tonic-gate 			keyfile = optarg;
2417c478bd9Sstevel@tonic-gate 			break;
2427c478bd9Sstevel@tonic-gate 		case 'i':
2437c478bd9Sstevel@tonic-gate 			iflag = B_TRUE;
2447c478bd9Sstevel@tonic-gate 			inputfile = optarg;
2457c478bd9Sstevel@tonic-gate 			break;
2467c478bd9Sstevel@tonic-gate 		case 'o':
2477c478bd9Sstevel@tonic-gate 			oflag = B_TRUE;
2487c478bd9Sstevel@tonic-gate 			outputfile = optarg;
2497c478bd9Sstevel@tonic-gate 			break;
2507c478bd9Sstevel@tonic-gate 		case 'l':
2517c478bd9Sstevel@tonic-gate 			lflag = B_TRUE;
2527c478bd9Sstevel@tonic-gate 			break;
2537c478bd9Sstevel@tonic-gate 		case 'v':
2547c478bd9Sstevel@tonic-gate 			vflag = B_TRUE;
2557c478bd9Sstevel@tonic-gate 			break;
2567c478bd9Sstevel@tonic-gate 		default:
2577c478bd9Sstevel@tonic-gate 			errflag = B_TRUE;
2587c478bd9Sstevel@tonic-gate 		}
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if (errflag || (!aflag && !lflag) || (lflag && argc > 2) ||
2627c478bd9Sstevel@tonic-gate 	    (optind < argc)) {
2637c478bd9Sstevel@tonic-gate 		usage(cmd);
2647c478bd9Sstevel@tonic-gate 		exit(EXIT_USAGE);
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	return (execute_cmd(cmd, algo_str));
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate /*
2717c478bd9Sstevel@tonic-gate  * usage message
2727c478bd9Sstevel@tonic-gate  */
2737c478bd9Sstevel@tonic-gate static void
2747c478bd9Sstevel@tonic-gate usage(struct CommandInfo *cmd)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	if (cmd->type == CKA_ENCRYPT) {
2777c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("usage: encrypt -l | -a "
2787c478bd9Sstevel@tonic-gate 		    "<algorithm> [-k <keyfile>] [-i <infile>]"
2797c478bd9Sstevel@tonic-gate 		    "\n\t\t\t[-o <outfile>]"));
2807c478bd9Sstevel@tonic-gate 	} else {
2817c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("usage: decrypt -l | -a "
2827c478bd9Sstevel@tonic-gate 		    "<algorithm> [-k <keyfile>] [-i <infile>]"
2837c478bd9Sstevel@tonic-gate 		    "\n\t\t\t[-o <outfile>]"));
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate /*
2887c478bd9Sstevel@tonic-gate  * Print out list of algorithms in default and verbose mode
2897c478bd9Sstevel@tonic-gate  */
2907c478bd9Sstevel@tonic-gate static void
2917c478bd9Sstevel@tonic-gate algorithm_list()
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate 	int mech;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	(void) printf(gettext("Algorithm       Keysize:  Min   Max (bits)\n"
2967c478bd9Sstevel@tonic-gate 	    "------------------------------------------\n"));
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	for (mech = 0; mech < MECH_ALIASES_COUNT; mech++) {
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 		if (mech_aliases[mech].available == B_FALSE)
3017c478bd9Sstevel@tonic-gate 			continue;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 		(void) printf("%-15s", mech_aliases[mech].alias);
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 		if (mech_aliases[mech].keysize_min != UINT_MAX &&
3067c478bd9Sstevel@tonic-gate 		    mech_aliases[mech].keysize_max != 0)
3077c478bd9Sstevel@tonic-gate 			(void) printf("         %5lu %5lu\n",
3087c478bd9Sstevel@tonic-gate 			    (mech_aliases[mech].keysize_min *
3097c478bd9Sstevel@tonic-gate 				mech_aliases[mech].keysize_unit),
3107c478bd9Sstevel@tonic-gate 			    (mech_aliases[mech].keysize_max *
3117c478bd9Sstevel@tonic-gate 				mech_aliases[mech].keysize_unit));
3127c478bd9Sstevel@tonic-gate 		else
3137c478bd9Sstevel@tonic-gate 			(void) printf("\n");
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate static CK_RV
3197c478bd9Sstevel@tonic-gate generate_pkcs5_key(CK_SESSION_HANDLE hSession,
3207c478bd9Sstevel@tonic-gate 		CK_BYTE		*pSaltData,
3217c478bd9Sstevel@tonic-gate 		CK_ULONG	saltLen,
3227c478bd9Sstevel@tonic-gate 		CK_ULONG	iterations,
3237c478bd9Sstevel@tonic-gate 		CK_BYTE		*pkeydata, /* user entered passphrase */
3247c478bd9Sstevel@tonic-gate 		CK_KEY_TYPE	keytype,
3257c478bd9Sstevel@tonic-gate 		CK_ULONG	passwd_size,
3267c478bd9Sstevel@tonic-gate 		CK_ULONG	keylen,  /* desired length of generated key */
3277c478bd9Sstevel@tonic-gate 		CK_ATTRIBUTE_TYPE operation,
3287c478bd9Sstevel@tonic-gate 		CK_OBJECT_HANDLE *hKey)
3297c478bd9Sstevel@tonic-gate {
3307c478bd9Sstevel@tonic-gate 	CK_RV rv;
3317c478bd9Sstevel@tonic-gate 	CK_PKCS5_PBKD2_PARAMS params;
3327c478bd9Sstevel@tonic-gate 	CK_MECHANISM mechanism;
3337c478bd9Sstevel@tonic-gate 	CK_OBJECT_CLASS class = CKO_SECRET_KEY;
3347c478bd9Sstevel@tonic-gate 	CK_ATTRIBUTE tmpl[4];
3357c478bd9Sstevel@tonic-gate 	int attrs = 0;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	mechanism.mechanism = CKM_PKCS5_PBKD2;
3387c478bd9Sstevel@tonic-gate 	mechanism.pParameter = &params;
3397c478bd9Sstevel@tonic-gate 	mechanism.ulParameterLen = sizeof (params);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	tmpl[attrs].type = CKA_CLASS;
3427c478bd9Sstevel@tonic-gate 	tmpl[attrs].pValue = &class;
3437c478bd9Sstevel@tonic-gate 	tmpl[attrs].ulValueLen = sizeof (class);
3447c478bd9Sstevel@tonic-gate 	attrs++;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	tmpl[attrs].type = CKA_KEY_TYPE;
3477c478bd9Sstevel@tonic-gate 	tmpl[attrs].pValue = &keytype;
3487c478bd9Sstevel@tonic-gate 	tmpl[attrs].ulValueLen = sizeof (keytype);
3497c478bd9Sstevel@tonic-gate 	attrs++;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	tmpl[attrs].type = operation;
3527c478bd9Sstevel@tonic-gate 	tmpl[attrs].pValue = &truevalue;
3537c478bd9Sstevel@tonic-gate 	tmpl[attrs].ulValueLen = sizeof (CK_BBOOL);
3547c478bd9Sstevel@tonic-gate 	attrs++;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	if (keylen > 0) {
3577c478bd9Sstevel@tonic-gate 		tmpl[attrs].type = CKA_VALUE_LEN;
3587c478bd9Sstevel@tonic-gate 		tmpl[attrs].pValue = &keylen;
3597c478bd9Sstevel@tonic-gate 		tmpl[attrs].ulValueLen = sizeof (keylen);
3607c478bd9Sstevel@tonic-gate 		attrs++;
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	params.saltSource = CKZ_SALT_SPECIFIED;
3647c478bd9Sstevel@tonic-gate 	params.pSaltSourceData = (void *)pSaltData;
3657c478bd9Sstevel@tonic-gate 	params.ulSaltSourceDataLen = saltLen;
3667c478bd9Sstevel@tonic-gate 	params.iterations = iterations;
3677c478bd9Sstevel@tonic-gate 	params.prf = CKP_PKCS5_PBKD2_HMAC_SHA1;
3687c478bd9Sstevel@tonic-gate 	params.pPrfData = NULL;
3697c478bd9Sstevel@tonic-gate 	params.ulPrfDataLen = 0;
3707c478bd9Sstevel@tonic-gate 	params.pPassword = (CK_UTF8CHAR_PTR)pkeydata;
3717c478bd9Sstevel@tonic-gate 	params.ulPasswordLen = &passwd_size;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	mechanism.mechanism = CKM_PKCS5_PBKD2;
3747c478bd9Sstevel@tonic-gate 	mechanism.pParameter = &params;
3757c478bd9Sstevel@tonic-gate 	mechanism.ulParameterLen = sizeof (params);
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	rv = C_GenerateKey(hSession, &mechanism, tmpl,
3787c478bd9Sstevel@tonic-gate 		attrs, hKey);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	return (rv);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate /*
3857c478bd9Sstevel@tonic-gate  * Execute the command.
3867c478bd9Sstevel@tonic-gate  *   cmd - command pointing to type of operation.
3877c478bd9Sstevel@tonic-gate  *   algo_str - alias of the algorithm passed.
3887c478bd9Sstevel@tonic-gate  */
3897c478bd9Sstevel@tonic-gate static int
3907c478bd9Sstevel@tonic-gate execute_cmd(struct CommandInfo *cmd, char *algo_str)
3917c478bd9Sstevel@tonic-gate {
3927c478bd9Sstevel@tonic-gate 	CK_RV rv;
3937c478bd9Sstevel@tonic-gate 	CK_ULONG slotcount;
3947c478bd9Sstevel@tonic-gate 	CK_SLOT_ID slotID;
3957c478bd9Sstevel@tonic-gate 	CK_SLOT_ID_PTR pSlotList = NULL;
3967c478bd9Sstevel@tonic-gate 	CK_MECHANISM_TYPE mech_type = 0;
3977c478bd9Sstevel@tonic-gate 	CK_MECHANISM_INFO info, kg_info;
3987c478bd9Sstevel@tonic-gate 	CK_MECHANISM mech;
3997c478bd9Sstevel@tonic-gate 	CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE;
4007c478bd9Sstevel@tonic-gate 	CK_BYTE_PTR	pkeydata = NULL;
4017c478bd9Sstevel@tonic-gate 	CK_BYTE		salt[PBKD2_SALT_SIZE];
4027c478bd9Sstevel@tonic-gate 	CK_ULONG	keysize = 0;
4037c478bd9Sstevel@tonic-gate 	int i, slot, mek;		/* index variables */
4047c478bd9Sstevel@tonic-gate 	int status;
4057c478bd9Sstevel@tonic-gate 	struct stat	insbuf;		/* stat buf for infile */
4067c478bd9Sstevel@tonic-gate 	struct stat	outsbuf;	/* stat buf for outfile */
4077c478bd9Sstevel@tonic-gate 	char	tmpnam[PATH_MAX];	/* tmp file name */
4087c478bd9Sstevel@tonic-gate 	CK_OBJECT_HANDLE key = (CK_OBJECT_HANDLE) 0;
4097c478bd9Sstevel@tonic-gate 	int infd = 0;			/* input file, stdin default */
4107c478bd9Sstevel@tonic-gate 	int outfd = 1;			/* output file, stdout default */
4117c478bd9Sstevel@tonic-gate 	char *outfilename = NULL;
4127c478bd9Sstevel@tonic-gate 	boolean_t errflag = B_TRUE;
4137c478bd9Sstevel@tonic-gate 	boolean_t inoutsame = B_FALSE;	/* if both input & output are same */
4147c478bd9Sstevel@tonic-gate 	CK_BYTE_PTR	pivbuf = NULL_PTR;
4157c478bd9Sstevel@tonic-gate 	CK_ULONG	ivlen = 0L;
4167c478bd9Sstevel@tonic-gate 	int mech_match = 0;
4177c478bd9Sstevel@tonic-gate 	CK_ULONG	iterations = PBKD2_ITERATIONS;
4187c478bd9Sstevel@tonic-gate 	CK_ULONG	keylen;
4197c478bd9Sstevel@tonic-gate 	int version = SUNW_ENCRYPT_FILE_VERSION;
4207c478bd9Sstevel@tonic-gate 	CK_KEY_TYPE keytype;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	if (aflag) {
4237c478bd9Sstevel@tonic-gate 		/* Determine if algorithm is valid */
4247c478bd9Sstevel@tonic-gate 		for (mech_match = 0; mech_match < MECH_ALIASES_COUNT;
4257c478bd9Sstevel@tonic-gate 			mech_match++) {
4267c478bd9Sstevel@tonic-gate 			if (strcmp(algo_str,
4277c478bd9Sstevel@tonic-gate 			    mech_aliases[mech_match].alias) == 0) {
4287c478bd9Sstevel@tonic-gate 				mech_type = mech_aliases[mech_match].type;
4297c478bd9Sstevel@tonic-gate 				break;
4307c478bd9Sstevel@tonic-gate 			}
4317c478bd9Sstevel@tonic-gate 		}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 		if (mech_match == MECH_ALIASES_COUNT) {
4347c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR,
4357c478bd9Sstevel@tonic-gate 			    gettext("unknown algorithm -- %s"), algo_str);
4367c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
4377c478bd9Sstevel@tonic-gate 		}
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 		/*
4407c478bd9Sstevel@tonic-gate 		 * Process keyfile
4417c478bd9Sstevel@tonic-gate 		 *
4427c478bd9Sstevel@tonic-gate 		 * If a keyfile is provided, get the key data from
4437c478bd9Sstevel@tonic-gate 		 * the file. Otherwise, prompt for a passphrase. The
4447c478bd9Sstevel@tonic-gate 		 * passphrase is used as the key data.
4457c478bd9Sstevel@tonic-gate 		 */
4467c478bd9Sstevel@tonic-gate 		if (kflag) {
4477c478bd9Sstevel@tonic-gate 			status = cryptoreadfile(keyfile, &pkeydata, &keysize);
4487c478bd9Sstevel@tonic-gate 		} else {
4497c478bd9Sstevel@tonic-gate 			status = cryptogetkey(&pkeydata, &keysize);
4507c478bd9Sstevel@tonic-gate 		}
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 		if (status == -1 || keysize == 0L) {
4537c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext("invalid key."));
4547c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
4557c478bd9Sstevel@tonic-gate 		}
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	bzero(salt, sizeof (salt));
4597c478bd9Sstevel@tonic-gate 	/* Initialize pkcs */
4607c478bd9Sstevel@tonic-gate 	if ((rv = C_Initialize(NULL)) != CKR_OK) {
4617c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("failed to initialize "
4627c478bd9Sstevel@tonic-gate 		    "PKCS #11 framework: %s"), pkcs11_strerror(rv));
4637c478bd9Sstevel@tonic-gate 		goto cleanup;
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	/* Get slot count */
4677c478bd9Sstevel@tonic-gate 	rv = C_GetSlotList(0, NULL_PTR, &slotcount);
4687c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK || slotcount == 0) {
4697c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
4707c478bd9Sstevel@tonic-gate 		    "failed to find any cryptographic provider,"
4717c478bd9Sstevel@tonic-gate 		    "please check with your system administrator: %s"),
4727c478bd9Sstevel@tonic-gate 		    pkcs11_strerror(rv));
4737c478bd9Sstevel@tonic-gate 		goto cleanup;
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	/* Found at least one slot, allocate memory for slot list */
4777c478bd9Sstevel@tonic-gate 	pSlotList = malloc(slotcount * sizeof (CK_SLOT_ID));
4787c478bd9Sstevel@tonic-gate 	if (pSlotList == NULL_PTR) {
4797c478bd9Sstevel@tonic-gate 		int err = errno;
4807c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("malloc: %s"), strerror(err));
4817c478bd9Sstevel@tonic-gate 		goto cleanup;
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	/* Get the list of slots */
4857c478bd9Sstevel@tonic-gate 	if ((rv = C_GetSlotList(0, pSlotList, &slotcount)) != CKR_OK) {
4867c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
4877c478bd9Sstevel@tonic-gate 		    "failed to find any cryptographic provider,"
4887c478bd9Sstevel@tonic-gate 		    "please check with your system administrator: %s"),
4897c478bd9Sstevel@tonic-gate 		    pkcs11_strerror(rv));
4907c478bd9Sstevel@tonic-gate 		goto cleanup;
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	if (lflag) {
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 		/* Iterate through slots */
4967c478bd9Sstevel@tonic-gate 		for (slot = 0; slot < slotcount; slot++) {
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 			/* Iterate through each mechanism */
4997c478bd9Sstevel@tonic-gate 			for (mek = 0; mek < MECH_ALIASES_COUNT; mek++) {
5007c478bd9Sstevel@tonic-gate 				rv = C_GetMechanismInfo(pSlotList[slot],
5017c478bd9Sstevel@tonic-gate 				    mech_aliases[mek].type, &info);
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 				if (rv != CKR_OK)
5047c478bd9Sstevel@tonic-gate 					continue;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 				/*
5077c478bd9Sstevel@tonic-gate 				 * Set to minimum/maximum key sizes assuming
5087c478bd9Sstevel@tonic-gate 				 * the values available are not 0.
5097c478bd9Sstevel@tonic-gate 				 */
5107c478bd9Sstevel@tonic-gate 				if (info.ulMinKeySize && (info.ulMinKeySize <
5117c478bd9Sstevel@tonic-gate 				    mech_aliases[mek].keysize_min))
5127c478bd9Sstevel@tonic-gate 					mech_aliases[mek].keysize_min =
5137c478bd9Sstevel@tonic-gate 						    info.ulMinKeySize;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 				if (info.ulMaxKeySize && (info.ulMaxKeySize >
5167c478bd9Sstevel@tonic-gate 				    mech_aliases[mek].keysize_max))
5177c478bd9Sstevel@tonic-gate 					mech_aliases[mek].keysize_max =
5187c478bd9Sstevel@tonic-gate 						    info.ulMaxKeySize;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 				mech_aliases[mek].available = B_TRUE;
5217c478bd9Sstevel@tonic-gate 			}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 		}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 		algorithm_list();
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 		errflag = B_FALSE;
5287c478bd9Sstevel@tonic-gate 		goto cleanup;
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	/* Find a slot with matching mechanism */
5327c478bd9Sstevel@tonic-gate 	for (i = 0; i < slotcount; i++) {
5337c478bd9Sstevel@tonic-gate 		slotID = pSlotList[i];
5347c478bd9Sstevel@tonic-gate 		rv = C_GetMechanismInfo(slotID, mech_type, &info);
5357c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK) {
5367c478bd9Sstevel@tonic-gate 			continue; /* to the next slot */
5377c478bd9Sstevel@tonic-gate 		} else {
5387c478bd9Sstevel@tonic-gate 			/*
5397c478bd9Sstevel@tonic-gate 			 * If the slot support the crypto, also
5407c478bd9Sstevel@tonic-gate 			 * make sure it supports the correct
5417c478bd9Sstevel@tonic-gate 			 * key generation mech if needed.
5427c478bd9Sstevel@tonic-gate 			 *
5437c478bd9Sstevel@tonic-gate 			 * We need PKCS5 when RC4 is used or
5447c478bd9Sstevel@tonic-gate 			 * when the key is entered on cmd line.
5457c478bd9Sstevel@tonic-gate 			 */
5467c478bd9Sstevel@tonic-gate 			if ((info.flags & cmd->flags) &&
5477c478bd9Sstevel@tonic-gate 			    (mech_type == CKM_RC4) || (keyfile == NULL)) {
5487c478bd9Sstevel@tonic-gate 				rv = C_GetMechanismInfo(slotID,
5497c478bd9Sstevel@tonic-gate 					CKM_PKCS5_PBKD2, &kg_info);
5507c478bd9Sstevel@tonic-gate 				if (rv == CKR_OK)
5517c478bd9Sstevel@tonic-gate 					break;
5527c478bd9Sstevel@tonic-gate 			} else if (info.flags & cmd->flags) {
5537c478bd9Sstevel@tonic-gate 				break;
5547c478bd9Sstevel@tonic-gate 			}
5557c478bd9Sstevel@tonic-gate 		}
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	/* Show error if no matching mechanism found */
5597c478bd9Sstevel@tonic-gate 	if (i == slotcount) {
5607c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
5617c478bd9Sstevel@tonic-gate 		    gettext("no cryptographic provider was "
5627c478bd9Sstevel@tonic-gate 		    "found for this algorithm -- %s"), algo_str);
5637c478bd9Sstevel@tonic-gate 		goto cleanup;
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	/* Open a session */
5687c478bd9Sstevel@tonic-gate 	rv = C_OpenSession(slotID, CKF_SERIAL_SESSION,
5697c478bd9Sstevel@tonic-gate 		NULL_PTR, NULL, &hSession);
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
5727c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
5737c478bd9Sstevel@tonic-gate 		    gettext("can not open PKCS #11 session: %s"),
5747c478bd9Sstevel@tonic-gate 		    pkcs11_strerror(rv));
5757c478bd9Sstevel@tonic-gate 		goto cleanup;
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	/*
5797c478bd9Sstevel@tonic-gate 	 * Generate IV data for encrypt.
5807c478bd9Sstevel@tonic-gate 	 */
5817c478bd9Sstevel@tonic-gate 	ivlen = mech_aliases[mech_match].ivlen;
5827c478bd9Sstevel@tonic-gate 	if ((pivbuf = malloc((size_t)ivlen)) == NULL) {
5837c478bd9Sstevel@tonic-gate 		int err = errno;
5847c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("malloc: %s"),
5857c478bd9Sstevel@tonic-gate 		    strerror(err));
5867c478bd9Sstevel@tonic-gate 		goto cleanup;
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	if (cmd->type == CKA_ENCRYPT) {
5907c478bd9Sstevel@tonic-gate 		if ((get_random_data(pivbuf,
5917c478bd9Sstevel@tonic-gate 		    mech_aliases[mech_match].ivlen)) != 0) {
5927c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
5937c478bd9Sstevel@tonic-gate 				"Unable to generate random "
5947c478bd9Sstevel@tonic-gate 				"data for initialization vector."));
5957c478bd9Sstevel@tonic-gate 			goto cleanup;
5967c478bd9Sstevel@tonic-gate 		}
5977c478bd9Sstevel@tonic-gate 	}
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	/*
6007c478bd9Sstevel@tonic-gate 	 * Create the key object
6017c478bd9Sstevel@tonic-gate 	 */
6027c478bd9Sstevel@tonic-gate 	rv = pkcs11_mech2keytype(mech_type, &keytype);
6037c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
6047c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
6057c478bd9Sstevel@tonic-gate 			gettext("unable to find key type for algorithm."));
6067c478bd9Sstevel@tonic-gate 		goto cleanup;
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	/* Open input file */
6107c478bd9Sstevel@tonic-gate 	if (iflag) {
6117c478bd9Sstevel@tonic-gate 		if ((infd = open(inputfile, O_RDONLY | O_NONBLOCK)) == -1) {
6127c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
6137c478bd9Sstevel@tonic-gate 				"can not open input file %s"), inputfile);
6147c478bd9Sstevel@tonic-gate 			goto cleanup;
6157c478bd9Sstevel@tonic-gate 		}
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 		/* Get info on input file */
6187c478bd9Sstevel@tonic-gate 		if (fstat(infd, &insbuf) == -1) {
6197c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
6207c478bd9Sstevel@tonic-gate 				"can not stat input file %s"), inputfile);
6217c478bd9Sstevel@tonic-gate 			goto cleanup;
6227c478bd9Sstevel@tonic-gate 		}
6237c478bd9Sstevel@tonic-gate 	}
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	/*
6267c478bd9Sstevel@tonic-gate 	 * Prepare output file
6277c478bd9Sstevel@tonic-gate 	 * If the input & output file are same,
6287c478bd9Sstevel@tonic-gate 	 * the output is written to a temp
6297c478bd9Sstevel@tonic-gate 	 * file first, then renamed to the original file
6307c478bd9Sstevel@tonic-gate 	 * after the crypt operation
6317c478bd9Sstevel@tonic-gate 	 */
6327c478bd9Sstevel@tonic-gate 	inoutsame = B_FALSE;
6337c478bd9Sstevel@tonic-gate 	if (oflag) {
6347c478bd9Sstevel@tonic-gate 		outfilename = outputfile;
6357c478bd9Sstevel@tonic-gate 		if ((stat(outputfile, &outsbuf) != -1) &&
6367c478bd9Sstevel@tonic-gate 			(insbuf.st_ino == outsbuf.st_ino)) {
6377c478bd9Sstevel@tonic-gate 			char *dir;
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 			/* create temp file on same dir */
6407c478bd9Sstevel@tonic-gate 			dir = dirname(outputfile);
6417c478bd9Sstevel@tonic-gate 			(void) snprintf(tmpnam, sizeof (tmpnam),
6427c478bd9Sstevel@tonic-gate 				"%s/encrXXXXXX", dir);
6437c478bd9Sstevel@tonic-gate 			outfilename = tmpnam;
6447c478bd9Sstevel@tonic-gate 			if ((outfd = mkstemp(tmpnam)) == -1) {
6457c478bd9Sstevel@tonic-gate 				cryptoerror(LOG_STDERR, gettext(
6467c478bd9Sstevel@tonic-gate 				    "cannot create temp file"));
6477c478bd9Sstevel@tonic-gate 				goto cleanup;
6487c478bd9Sstevel@tonic-gate 			}
6497c478bd9Sstevel@tonic-gate 			inoutsame = B_TRUE;
6507c478bd9Sstevel@tonic-gate 		} else {
6517c478bd9Sstevel@tonic-gate 			/* Create file for output */
6527c478bd9Sstevel@tonic-gate 			if ((outfd = open(outfilename,
6537c478bd9Sstevel@tonic-gate 			    O_CREAT|O_WRONLY|O_TRUNC,
6547c478bd9Sstevel@tonic-gate 					0644)) == -1) {
6557c478bd9Sstevel@tonic-gate 				cryptoerror(LOG_STDERR, gettext(
6567c478bd9Sstevel@tonic-gate 				    "cannot open output file %s"),
6577c478bd9Sstevel@tonic-gate 				    outfilename);
6587c478bd9Sstevel@tonic-gate 				goto cleanup;
6597c478bd9Sstevel@tonic-gate 			}
6607c478bd9Sstevel@tonic-gate 		}
6617c478bd9Sstevel@tonic-gate 	}
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	/*
6647c478bd9Sstevel@tonic-gate 	 * Read the version number from the head of the file
6657c478bd9Sstevel@tonic-gate 	 * to know how to interpret the data that follows.
6667c478bd9Sstevel@tonic-gate 	 */
6677c478bd9Sstevel@tonic-gate 	if (cmd->type == CKA_DECRYPT) {
6687c478bd9Sstevel@tonic-gate 		if (read(infd, &version, sizeof (version)) !=
6697c478bd9Sstevel@tonic-gate 			sizeof (version)) {
6707c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
6717c478bd9Sstevel@tonic-gate 			    "failed to get format version from "
6727c478bd9Sstevel@tonic-gate 			    "input file."));
6737c478bd9Sstevel@tonic-gate 			goto cleanup;
6747c478bd9Sstevel@tonic-gate 		}
6757c478bd9Sstevel@tonic-gate 		/* convert to host byte order */
6767c478bd9Sstevel@tonic-gate 		version = ntohl(version);
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 		switch (version) {
6797c478bd9Sstevel@tonic-gate 		case 1:
6807c478bd9Sstevel@tonic-gate 		/*
6817c478bd9Sstevel@tonic-gate 		 * Version 1 output format:
6827c478bd9Sstevel@tonic-gate 		 *  - Iterations used in key gen function (4 bytes)
6837c478bd9Sstevel@tonic-gate 		 *  - IV ( 'ivlen' bytes)
6847c478bd9Sstevel@tonic-gate 		 *  - Salt data used in key gen (16 bytes)
6857c478bd9Sstevel@tonic-gate 		 *
6867c478bd9Sstevel@tonic-gate 		 * An encrypted file has IV as first block (0 or
6877c478bd9Sstevel@tonic-gate 		 * more bytes depending on mechanism) followed
6887c478bd9Sstevel@tonic-gate 		 * by cipher text.  Get the IV from the encrypted
6897c478bd9Sstevel@tonic-gate 		 * file.
6907c478bd9Sstevel@tonic-gate 		 */
6917c478bd9Sstevel@tonic-gate 			/*
6927c478bd9Sstevel@tonic-gate 			 * Read iteration count and salt data.
6937c478bd9Sstevel@tonic-gate 			 */
6947c478bd9Sstevel@tonic-gate 			if (read(infd, &iterations,
6957c478bd9Sstevel@tonic-gate 				sizeof (iterations)) !=
6967c478bd9Sstevel@tonic-gate 				sizeof (iterations)) {
6977c478bd9Sstevel@tonic-gate 				cryptoerror(LOG_STDERR, gettext(
6987c478bd9Sstevel@tonic-gate 					"failed to get iterations from "
6997c478bd9Sstevel@tonic-gate 					"input file."));
7007c478bd9Sstevel@tonic-gate 				goto cleanup;
7017c478bd9Sstevel@tonic-gate 			}
7027c478bd9Sstevel@tonic-gate 			/* convert to host byte order */
7037c478bd9Sstevel@tonic-gate 			iterations = ntohl(iterations);
7047c478bd9Sstevel@tonic-gate 			if (ivlen > 0 &&
7057c478bd9Sstevel@tonic-gate 			    read(infd, pivbuf, ivlen) != ivlen) {
7067c478bd9Sstevel@tonic-gate 				cryptoerror(LOG_STDERR, gettext(
7077c478bd9Sstevel@tonic-gate 				    "failed to get initialization "
7087c478bd9Sstevel@tonic-gate 				    "vector from input file."));
7097c478bd9Sstevel@tonic-gate 				goto cleanup;
7107c478bd9Sstevel@tonic-gate 			}
7117c478bd9Sstevel@tonic-gate 			if (read(infd, salt, sizeof (salt))
7127c478bd9Sstevel@tonic-gate 				!= sizeof (salt)) {
7137c478bd9Sstevel@tonic-gate 				cryptoerror(LOG_STDERR, gettext(
7147c478bd9Sstevel@tonic-gate 					"failed to get salt data from "
7157c478bd9Sstevel@tonic-gate 					"input file."));
7167c478bd9Sstevel@tonic-gate 				goto cleanup;
7177c478bd9Sstevel@tonic-gate 			}
7187c478bd9Sstevel@tonic-gate 			break;
7197c478bd9Sstevel@tonic-gate 		default:
7207c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
7217c478bd9Sstevel@tonic-gate 			"Unrecognized format version read from "
7227c478bd9Sstevel@tonic-gate 			"input file - expected %d, got %d."),
7237c478bd9Sstevel@tonic-gate 			SUNW_ENCRYPT_FILE_VERSION, version);
7247c478bd9Sstevel@tonic-gate 			goto cleanup;
7257c478bd9Sstevel@tonic-gate 			break;
7267c478bd9Sstevel@tonic-gate 		}
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 	/*
7297c478bd9Sstevel@tonic-gate 	 * If encrypting, we need some random
7307c478bd9Sstevel@tonic-gate 	 * salt data to create the key.  If decrypting,
7317c478bd9Sstevel@tonic-gate 	 * the salt should come from head of the file
7327c478bd9Sstevel@tonic-gate 	 * to be decrypted.
7337c478bd9Sstevel@tonic-gate 	 */
7347c478bd9Sstevel@tonic-gate 	if (cmd->type == CKA_ENCRYPT) {
7357c478bd9Sstevel@tonic-gate 		rv = get_random_data(salt, sizeof (salt));
7367c478bd9Sstevel@tonic-gate 		if (rv != 0) {
7377c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR,
7387c478bd9Sstevel@tonic-gate 			gettext("unable to generate random "
7397c478bd9Sstevel@tonic-gate 				"data for key salt."));
7407c478bd9Sstevel@tonic-gate 			goto cleanup;
7417c478bd9Sstevel@tonic-gate 		}
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	/*
7457c478bd9Sstevel@tonic-gate 	 * If key input is read from  a file, treat it as
7467c478bd9Sstevel@tonic-gate 	 * raw key data, unless it is to be used with RC4,
7477c478bd9Sstevel@tonic-gate 	 * in which case it must be used to generate a pkcs5
7487c478bd9Sstevel@tonic-gate 	 * key to address security concerns with RC4 keys.
7497c478bd9Sstevel@tonic-gate 	 */
7507c478bd9Sstevel@tonic-gate 	if (kflag && keyfile != NULL && keytype != CKK_RC4) {
7517c478bd9Sstevel@tonic-gate 		CK_OBJECT_CLASS objclass = CKO_SECRET_KEY;
7527c478bd9Sstevel@tonic-gate 		CK_ATTRIBUTE template[5];
7537c478bd9Sstevel@tonic-gate 		int nattr = 0;
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 		template[nattr].type = CKA_CLASS;
7567c478bd9Sstevel@tonic-gate 		template[nattr].pValue = &objclass;
7577c478bd9Sstevel@tonic-gate 		template[nattr].ulValueLen = sizeof (objclass);
7587c478bd9Sstevel@tonic-gate 		nattr++;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 		template[nattr].type = CKA_KEY_TYPE;
7617c478bd9Sstevel@tonic-gate 		template[nattr].pValue = &keytype;
7627c478bd9Sstevel@tonic-gate 		template[nattr].ulValueLen = sizeof (keytype);
7637c478bd9Sstevel@tonic-gate 		nattr++;
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 		template[nattr].type = cmd->type;
7667c478bd9Sstevel@tonic-gate 		template[nattr].pValue = &truevalue;
7677c478bd9Sstevel@tonic-gate 		template[nattr].ulValueLen = sizeof (truevalue);
7687c478bd9Sstevel@tonic-gate 		nattr++;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 		template[nattr].type = CKA_TOKEN;
7717c478bd9Sstevel@tonic-gate 		template[nattr].pValue = &falsevalue;
7727c478bd9Sstevel@tonic-gate 		template[nattr].ulValueLen = sizeof (falsevalue);
7737c478bd9Sstevel@tonic-gate 		nattr++;
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 		template[nattr].type = CKA_VALUE;
7767c478bd9Sstevel@tonic-gate 		template[nattr].pValue = pkeydata;
7777c478bd9Sstevel@tonic-gate 		template[nattr].ulValueLen = keysize;
7787c478bd9Sstevel@tonic-gate 		nattr++;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 		rv = C_CreateObject(hSession, template,
7817c478bd9Sstevel@tonic-gate 			nattr, &key);
7827c478bd9Sstevel@tonic-gate 	} else {
7837c478bd9Sstevel@tonic-gate 		/*
7847c478bd9Sstevel@tonic-gate 		 * If the encryption type has a fixed key length,
7857c478bd9Sstevel@tonic-gate 		 * then its not necessary to set the key length
7867c478bd9Sstevel@tonic-gate 		 * parameter when generating the key.
7877c478bd9Sstevel@tonic-gate 		 */
7887c478bd9Sstevel@tonic-gate 		if (keytype == CKK_DES || keytype == CKK_DES3)
7897c478bd9Sstevel@tonic-gate 			keylen = 0;
7907c478bd9Sstevel@tonic-gate 		else
7917c478bd9Sstevel@tonic-gate 			keylen = 16;
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 		/*
7947c478bd9Sstevel@tonic-gate 		 * Generate a cryptographically secure key using
7957c478bd9Sstevel@tonic-gate 		 * the key read from the file given (-k keyfile) or
7967c478bd9Sstevel@tonic-gate 		 * the passphrase entered by the user.
7977c478bd9Sstevel@tonic-gate 		 */
7987c478bd9Sstevel@tonic-gate 		rv = generate_pkcs5_key(hSession,
7997c478bd9Sstevel@tonic-gate 			salt, sizeof (salt),
8007c478bd9Sstevel@tonic-gate 			iterations,
8017c478bd9Sstevel@tonic-gate 			pkeydata, keytype, keysize,
8027c478bd9Sstevel@tonic-gate 			keylen, cmd->type, &key);
8037c478bd9Sstevel@tonic-gate 	}
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
8067c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
8077c478bd9Sstevel@tonic-gate 		    "failed to generate a key: %s"),
8087c478bd9Sstevel@tonic-gate 		    pkcs11_strerror(rv));
8097c478bd9Sstevel@tonic-gate 		goto cleanup;
8107c478bd9Sstevel@tonic-gate 	}
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	/* Setup up mechanism */
8137c478bd9Sstevel@tonic-gate 	mech.mechanism = mech_type;
8147c478bd9Sstevel@tonic-gate 	mech.pParameter = (CK_VOID_PTR)pivbuf;
8157c478bd9Sstevel@tonic-gate 	mech.ulParameterLen = ivlen;
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	if ((rv = cmd->Init(hSession, &mech, key)) != CKR_OK) {
8187c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
8197c478bd9Sstevel@tonic-gate 		    "failed to initialize crypto operation: %s"),
8207c478bd9Sstevel@tonic-gate 		    pkcs11_strerror(rv));
8217c478bd9Sstevel@tonic-gate 		goto cleanup;
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	/* Write the version header encrypt command */
8257c478bd9Sstevel@tonic-gate 	if (cmd->type == CKA_ENCRYPT) {
8267c478bd9Sstevel@tonic-gate 		/* convert to network order for storage */
8277c478bd9Sstevel@tonic-gate 		int netversion = htonl(version);
8287c478bd9Sstevel@tonic-gate 		CK_ULONG netiter;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 		if (write(outfd, &netversion, sizeof (netversion))
8317c478bd9Sstevel@tonic-gate 			!= sizeof (netversion)) {
8327c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
8337c478bd9Sstevel@tonic-gate 			"failed to write version number "
8347c478bd9Sstevel@tonic-gate 			"to output file."));
8357c478bd9Sstevel@tonic-gate 			goto cleanup;
8367c478bd9Sstevel@tonic-gate 		}
8377c478bd9Sstevel@tonic-gate 		/*
8387c478bd9Sstevel@tonic-gate 		 * Write the iteration and salt data, even if they
8397c478bd9Sstevel@tonic-gate 		 * were not used to generate a key.
8407c478bd9Sstevel@tonic-gate 		 */
8417c478bd9Sstevel@tonic-gate 		netiter = htonl(iterations);
8427c478bd9Sstevel@tonic-gate 		if (write(outfd, &netiter,
8437c478bd9Sstevel@tonic-gate 			sizeof (netiter)) != sizeof (netiter)) {
8447c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
8457c478bd9Sstevel@tonic-gate 			    "failed to write iterations to output"));
8467c478bd9Sstevel@tonic-gate 			goto cleanup;
8477c478bd9Sstevel@tonic-gate 		}
8487c478bd9Sstevel@tonic-gate 		if (ivlen > 0 &&
8497c478bd9Sstevel@tonic-gate 			write(outfd, pivbuf, ivlen) != ivlen) {
8507c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
8517c478bd9Sstevel@tonic-gate 				"failed to write initialization vector "
8527c478bd9Sstevel@tonic-gate 				"to output"));
8537c478bd9Sstevel@tonic-gate 			goto cleanup;
8547c478bd9Sstevel@tonic-gate 		}
8557c478bd9Sstevel@tonic-gate 		if (write(outfd, salt, sizeof (salt)) != sizeof (salt)) {
8567c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
8577c478bd9Sstevel@tonic-gate 			    "failed to write salt data to output"));
8587c478bd9Sstevel@tonic-gate 			goto cleanup;
8597c478bd9Sstevel@tonic-gate 		}
8607c478bd9Sstevel@tonic-gate 	}
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	if (crypt_multipart(cmd, hSession, infd, outfd, insbuf) == -1) {
8637c478bd9Sstevel@tonic-gate 		goto cleanup;
8647c478bd9Sstevel@tonic-gate 	}
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	errflag = B_FALSE;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	/*
8697c478bd9Sstevel@tonic-gate 	 * Clean up
8707c478bd9Sstevel@tonic-gate 	 */
8717c478bd9Sstevel@tonic-gate cleanup:
8727c478bd9Sstevel@tonic-gate 	/* Clear the key data, so others cannot snoop */
8737c478bd9Sstevel@tonic-gate 	if (pkeydata != NULL) {
8747c478bd9Sstevel@tonic-gate 		bzero(pkeydata, keysize);
8757c478bd9Sstevel@tonic-gate 		free(pkeydata);
8767c478bd9Sstevel@tonic-gate 		pkeydata = NULL;
8777c478bd9Sstevel@tonic-gate 	}
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	/* Destroy key object */
8807c478bd9Sstevel@tonic-gate 	if (key != (CK_OBJECT_HANDLE) 0) {
8817c478bd9Sstevel@tonic-gate 		(void) C_DestroyObject(hSession, key);
8827c478bd9Sstevel@tonic-gate 	}
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	/* free allocated memory */
8857c478bd9Sstevel@tonic-gate 	if (pSlotList != NULL)
8867c478bd9Sstevel@tonic-gate 		free(pSlotList);
8877c478bd9Sstevel@tonic-gate 	if (pivbuf != NULL)
8887c478bd9Sstevel@tonic-gate 		free(pivbuf);
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	/* close all the files */
8917c478bd9Sstevel@tonic-gate 	if (infd != -1)
8927c478bd9Sstevel@tonic-gate 		(void) close(infd);
8937c478bd9Sstevel@tonic-gate 	if (outfd != -1)
8947c478bd9Sstevel@tonic-gate 		(void) close(outfd);
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	/* rename tmp output to input file */
8977c478bd9Sstevel@tonic-gate 	if (inoutsame) {
8987c478bd9Sstevel@tonic-gate 		if (rename(outfilename, inputfile) == -1) {
8997c478bd9Sstevel@tonic-gate 			(void) unlink(outfilename);
9007c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext("rename failed."));
9017c478bd9Sstevel@tonic-gate 		}
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	/* If error occurred, remove the output file */
9057c478bd9Sstevel@tonic-gate 	if (errflag && outfilename != NULL) {
9067c478bd9Sstevel@tonic-gate 		(void) unlink(outfilename);
9077c478bd9Sstevel@tonic-gate 	}
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	/* close pkcs11 session */
9107c478bd9Sstevel@tonic-gate 	if (hSession != CK_INVALID_HANDLE)
9117c478bd9Sstevel@tonic-gate 		(void) C_CloseSession(hSession);
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	(void) C_Finalize(NULL);
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	return (errflag);
9167c478bd9Sstevel@tonic-gate }
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate /*
9197c478bd9Sstevel@tonic-gate  * Function for printing progress bar when the verbose flag
9207c478bd9Sstevel@tonic-gate  * is set.
9217c478bd9Sstevel@tonic-gate  *
9227c478bd9Sstevel@tonic-gate  * The vertical bar is printed at 25, 50, and 75% complete.
9237c478bd9Sstevel@tonic-gate  *
9247c478bd9Sstevel@tonic-gate  * The function is passed the number of positions on the screen it needs to
9257c478bd9Sstevel@tonic-gate  * advance and loops.
9267c478bd9Sstevel@tonic-gate  */
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate static void
9297c478bd9Sstevel@tonic-gate print_status(int pos_to_advance)
9307c478bd9Sstevel@tonic-gate {
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	while (pos_to_advance > 0) {
9337c478bd9Sstevel@tonic-gate 		switch (status_pos) {
9347c478bd9Sstevel@tonic-gate 		case 0:
9357c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("["));
9367c478bd9Sstevel@tonic-gate 			break;
9377c478bd9Sstevel@tonic-gate 		case 19:
9387c478bd9Sstevel@tonic-gate 		case 39:
9397c478bd9Sstevel@tonic-gate 		case 59:
9407c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("|"));
9417c478bd9Sstevel@tonic-gate 			break;
9427c478bd9Sstevel@tonic-gate 		default:
9437c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("."));
9447c478bd9Sstevel@tonic-gate 		}
9457c478bd9Sstevel@tonic-gate 		pos_to_advance--;
9467c478bd9Sstevel@tonic-gate 		status_pos++;
9477c478bd9Sstevel@tonic-gate 	}
9487c478bd9Sstevel@tonic-gate }
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate /*
9517c478bd9Sstevel@tonic-gate  * Encrypt/Decrypt in multi part.
9527c478bd9Sstevel@tonic-gate  *
9537c478bd9Sstevel@tonic-gate  * This function reads the input file (infd) and writes the
9547c478bd9Sstevel@tonic-gate  * encrypted/decrypted output to file (outfd).
9557c478bd9Sstevel@tonic-gate  *
9567c478bd9Sstevel@tonic-gate  * cmd - pointing  to commandinfo
9577c478bd9Sstevel@tonic-gate  * hSession - pkcs session
9587c478bd9Sstevel@tonic-gate  * infd - input file descriptor
9597c478bd9Sstevel@tonic-gate  * outfd - output file descriptor
9607c478bd9Sstevel@tonic-gate  *
9617c478bd9Sstevel@tonic-gate  */
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate static int
9647c478bd9Sstevel@tonic-gate crypt_multipart(struct CommandInfo *cmd, CK_SESSION_HANDLE hSession,
9657c478bd9Sstevel@tonic-gate 	int infd, int outfd, struct stat in)
9667c478bd9Sstevel@tonic-gate {
9677c478bd9Sstevel@tonic-gate 	CK_RV		rv;
9687c478bd9Sstevel@tonic-gate 	CK_ULONG	resultlen;
9697c478bd9Sstevel@tonic-gate 	CK_ULONG	resultbuflen;
9707c478bd9Sstevel@tonic-gate 	CK_BYTE_PTR	resultbuf;
9717c478bd9Sstevel@tonic-gate 	CK_ULONG	datalen;
9727c478bd9Sstevel@tonic-gate 	CK_BYTE		databuf[BUFFERSIZE];
9737c478bd9Sstevel@tonic-gate 	CK_BYTE		outbuf[BUFFERSIZE+BLOCKSIZE];
9747c478bd9Sstevel@tonic-gate 	CK_ULONG	status_index = 0; /* current total file size read */
9757c478bd9Sstevel@tonic-gate 	float		status_last = 0.0; /* file size of last element used */
9767c478bd9Sstevel@tonic-gate 	float		status_incr = 0.0; /* file size element increments */
9777c478bd9Sstevel@tonic-gate 	int		pos; /* # of progress bar elements to be print */
9787c478bd9Sstevel@tonic-gate 	ssize_t		nread;
9797c478bd9Sstevel@tonic-gate 	boolean_t	errflag = B_FALSE;
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	datalen = sizeof (databuf);
9827c478bd9Sstevel@tonic-gate 	resultbuflen = sizeof (outbuf);
9837c478bd9Sstevel@tonic-gate 	resultbuf = outbuf;
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	/* Divide into 79 increments for progress bar element spacing */
9867c478bd9Sstevel@tonic-gate 	if (vflag && iflag)
9877c478bd9Sstevel@tonic-gate 		status_incr = (in.st_size / 79.0);
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	while ((nread = read(infd, databuf, datalen)) > 0) {
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 		/* Start with the initial buffer */
9927c478bd9Sstevel@tonic-gate 		resultlen = resultbuflen;
9937c478bd9Sstevel@tonic-gate 		rv = cmd->Update(hSession, databuf, (CK_ULONG)nread,
9947c478bd9Sstevel@tonic-gate 			resultbuf, &resultlen);
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 		/* Need a bigger buffer? */
9977c478bd9Sstevel@tonic-gate 		if (rv == CKR_BUFFER_TOO_SMALL) {
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 			/* free the old buffer */
10007c478bd9Sstevel@tonic-gate 			if (resultbuf != NULL && resultbuf != outbuf) {
10017c478bd9Sstevel@tonic-gate 				bzero(resultbuf, resultbuflen);
10027c478bd9Sstevel@tonic-gate 				free(resultbuf);
10037c478bd9Sstevel@tonic-gate 			}
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 			/* allocate a new big buffer */
10067c478bd9Sstevel@tonic-gate 			if ((resultbuf = malloc((size_t)resultlen)) == NULL) {
10077c478bd9Sstevel@tonic-gate 				int err = errno;
10087c478bd9Sstevel@tonic-gate 				cryptoerror(LOG_STDERR, gettext("malloc: %s"),
10097c478bd9Sstevel@tonic-gate 				    strerror(err));
10107c478bd9Sstevel@tonic-gate 				return (-1);
10117c478bd9Sstevel@tonic-gate 			}
10127c478bd9Sstevel@tonic-gate 			resultbuflen = resultlen;
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 			/* Try again with bigger buffer */
10157c478bd9Sstevel@tonic-gate 			rv = cmd->Update(hSession, databuf, (CK_ULONG)nread,
10167c478bd9Sstevel@tonic-gate 				resultbuf, &resultlen);
10177c478bd9Sstevel@tonic-gate 		}
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK) {
10207c478bd9Sstevel@tonic-gate 			errflag = B_TRUE;
10217c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
10227c478bd9Sstevel@tonic-gate 			    "crypto operation failed: %s"),
10237c478bd9Sstevel@tonic-gate 			    pkcs11_strerror(rv));
10247c478bd9Sstevel@tonic-gate 			break;
10257c478bd9Sstevel@tonic-gate 		}
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 		/* write the output */
10287c478bd9Sstevel@tonic-gate 		if (write(outfd, resultbuf, resultlen) != resultlen) {
10297c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
10307c478bd9Sstevel@tonic-gate 			    "failed to write result to output file."));
10317c478bd9Sstevel@tonic-gate 			errflag = B_TRUE;
10327c478bd9Sstevel@tonic-gate 			break;
10337c478bd9Sstevel@tonic-gate 		}
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 		if (vflag) {
10367c478bd9Sstevel@tonic-gate 			status_index += resultlen;
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 			/*
10397c478bd9Sstevel@tonic-gate 			 * If input is from stdin, do a our own progress bar
10407c478bd9Sstevel@tonic-gate 			 * by printing periods at a pre-defined increment
10417c478bd9Sstevel@tonic-gate 			 * until the file is done.
10427c478bd9Sstevel@tonic-gate 			 */
10437c478bd9Sstevel@tonic-gate 			if (!iflag) {
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 				/*
10467c478bd9Sstevel@tonic-gate 				 * Print at least 1 element in case the file
10477c478bd9Sstevel@tonic-gate 				 * is small, it looks better than nothing.
10487c478bd9Sstevel@tonic-gate 				 */
10497c478bd9Sstevel@tonic-gate 				if (status_pos == 0) {
10507c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext("."));
10517c478bd9Sstevel@tonic-gate 					status_pos = 1;
10527c478bd9Sstevel@tonic-gate 				}
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 				if ((status_index - status_last) >
10557c478bd9Sstevel@tonic-gate 				    (PROGRESSSIZE)) {
10567c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext("."));
10577c478bd9Sstevel@tonic-gate 					status_last = status_index;
10587c478bd9Sstevel@tonic-gate 				}
10597c478bd9Sstevel@tonic-gate 				continue;
10607c478bd9Sstevel@tonic-gate 			}
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 			/* Calculate the number of elements need to be print */
10637c478bd9Sstevel@tonic-gate 			if (in.st_size <= BUFFERSIZE)
10647c478bd9Sstevel@tonic-gate 				pos = 78;
10657c478bd9Sstevel@tonic-gate 			else
10667c478bd9Sstevel@tonic-gate 				pos = (int)((status_index - status_last) /
10677c478bd9Sstevel@tonic-gate 				    status_incr);
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 			/* Add progress bar elements, if needed */
10707c478bd9Sstevel@tonic-gate 			if (pos > 0) {
10717c478bd9Sstevel@tonic-gate 				print_status(pos);
10727c478bd9Sstevel@tonic-gate 				status_last += (status_incr * pos);
10737c478bd9Sstevel@tonic-gate 			}
10747c478bd9Sstevel@tonic-gate 		}
10757c478bd9Sstevel@tonic-gate 	}
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	/* Print verbose completion */
10787c478bd9Sstevel@tonic-gate 	if (vflag) {
10797c478bd9Sstevel@tonic-gate 		if (iflag)
10807c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "]");
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n%s\n", gettext("Done."));
10837c478bd9Sstevel@tonic-gate 	}
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	/* Error in reading */
10867c478bd9Sstevel@tonic-gate 	if (nread == -1) {
10877c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
10887c478bd9Sstevel@tonic-gate 		    "error reading from input file"));
10897c478bd9Sstevel@tonic-gate 		errflag = B_TRUE;
10907c478bd9Sstevel@tonic-gate 	}
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	if (!errflag) {
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 		/* Do the final part */
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 		rv = cmd->Final(hSession, resultbuf, &resultlen);
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 		if (rv == CKR_OK) {
10997c478bd9Sstevel@tonic-gate 			/* write the output */
11007c478bd9Sstevel@tonic-gate 			if (write(outfd, resultbuf, resultlen) != resultlen) {
11017c478bd9Sstevel@tonic-gate 				cryptoerror(LOG_STDERR, gettext(
11027c478bd9Sstevel@tonic-gate 				    "failed to write result to output file."));
11037c478bd9Sstevel@tonic-gate 				errflag = B_TRUE;
11047c478bd9Sstevel@tonic-gate 			}
11057c478bd9Sstevel@tonic-gate 		} else {
11067c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
11077c478bd9Sstevel@tonic-gate 			    "crypto operation failed: %s"),
11087c478bd9Sstevel@tonic-gate 			    pkcs11_strerror(rv));
11097c478bd9Sstevel@tonic-gate 			errflag = B_TRUE;
11107c478bd9Sstevel@tonic-gate 		}
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	}
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	if (resultbuf != NULL && resultbuf != outbuf) {
11157c478bd9Sstevel@tonic-gate 		bzero(resultbuf, resultbuflen);
11167c478bd9Sstevel@tonic-gate 		free(resultbuf);
11177c478bd9Sstevel@tonic-gate 	}
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	if (errflag) {
11207c478bd9Sstevel@tonic-gate 		return (-1);
11217c478bd9Sstevel@tonic-gate 	} else {
11227c478bd9Sstevel@tonic-gate 		return (0);
11237c478bd9Sstevel@tonic-gate 	}
11247c478bd9Sstevel@tonic-gate }
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate /*
11277c478bd9Sstevel@tonic-gate  * cryptoreadfile - reads file into a buffer
11287c478bd9Sstevel@tonic-gate  *  This function can be used for reading files
11297c478bd9Sstevel@tonic-gate  *  containing key or initialization vector data.
11307c478bd9Sstevel@tonic-gate  *
11317c478bd9Sstevel@tonic-gate  *  filename - name of file
11327c478bd9Sstevel@tonic-gate  *  pdata - entire file returned in this buffer
11337c478bd9Sstevel@tonic-gate  *	must be freed by caller using free()
11347c478bd9Sstevel@tonic-gate  *  pdatalen - length of data returned
11357c478bd9Sstevel@tonic-gate  *
11367c478bd9Sstevel@tonic-gate  * returns 0 if success, -1 if error
11377c478bd9Sstevel@tonic-gate  */
11387c478bd9Sstevel@tonic-gate static int
11397c478bd9Sstevel@tonic-gate cryptoreadfile(char *filename, CK_BYTE_PTR *pdata, CK_ULONG_PTR pdatalen)
11407c478bd9Sstevel@tonic-gate {
11417c478bd9Sstevel@tonic-gate 	struct stat statbuf;
11427c478bd9Sstevel@tonic-gate 	char *filebuf;
11437c478bd9Sstevel@tonic-gate 	int filesize;
11447c478bd9Sstevel@tonic-gate 	int fd;
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	if (filename == NULL)
11477c478bd9Sstevel@tonic-gate 		return (-1);
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	/* read the file into a buffer */
11507c478bd9Sstevel@tonic-gate 	if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) == -1) {
11517c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
11527c478bd9Sstevel@tonic-gate 			"cannot open %s"), filename);
11537c478bd9Sstevel@tonic-gate 		return (-1);
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	}
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	if (fstat(fd, &statbuf) == -1) {
11587c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
11597c478bd9Sstevel@tonic-gate 			"cannot stat %s"), filename);
11607c478bd9Sstevel@tonic-gate 		(void) close(fd);
11617c478bd9Sstevel@tonic-gate 		return (-1);
11627c478bd9Sstevel@tonic-gate 	}
11637c478bd9Sstevel@tonic-gate 
1164*4bc0a2efScasper 	if (!S_ISREG(statbuf.st_mode)) {
11657c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
11667c478bd9Sstevel@tonic-gate 			"%s not a regular file"), filename);
11677c478bd9Sstevel@tonic-gate 		(void) close(fd);
11687c478bd9Sstevel@tonic-gate 		return (-1);
11697c478bd9Sstevel@tonic-gate 	}
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	filesize = (size_t)statbuf.st_size;
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	if (filesize == 0) {
11747c478bd9Sstevel@tonic-gate 		(void) close(fd);
11757c478bd9Sstevel@tonic-gate 		return (-1);
11767c478bd9Sstevel@tonic-gate 	}
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	/* allocate a buffer to hold the entire key */
11797c478bd9Sstevel@tonic-gate 	if ((filebuf = malloc(filesize)) == NULL) {
11807c478bd9Sstevel@tonic-gate 		int err = errno;
11817c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("malloc: %s"), strerror(err));
11827c478bd9Sstevel@tonic-gate 		(void) close(fd);
11837c478bd9Sstevel@tonic-gate 		return (-1);
11847c478bd9Sstevel@tonic-gate 	}
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	if (read(fd, filebuf, filesize) != filesize) {
11877c478bd9Sstevel@tonic-gate 		int err = errno;
11887c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("error reading file: %s"),
11897c478bd9Sstevel@tonic-gate 		    strerror(err));
11907c478bd9Sstevel@tonic-gate 		(void) close(fd);
11917c478bd9Sstevel@tonic-gate 		free(filebuf);
11927c478bd9Sstevel@tonic-gate 		return (-1);
11937c478bd9Sstevel@tonic-gate 	}
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	(void) close(fd);
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 	*pdata = (CK_BYTE_PTR)filebuf;
11987c478bd9Sstevel@tonic-gate 	*pdatalen = (CK_ULONG)filesize;
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	return (0);
12017c478bd9Sstevel@tonic-gate }
12027c478bd9Sstevel@tonic-gate /*
12037c478bd9Sstevel@tonic-gate  * cryptogetkey - prompt user for a key
12047c478bd9Sstevel@tonic-gate  *
12057c478bd9Sstevel@tonic-gate  *   pkeydata - buffer for returning key data
12067c478bd9Sstevel@tonic-gate  *	must be freed by caller using free()
12077c478bd9Sstevel@tonic-gate  *   pkeysize - size of buffer returned
12087c478bd9Sstevel@tonic-gate  *
12097c478bd9Sstevel@tonic-gate  * returns
12107c478bd9Sstevel@tonic-gate  *   0 for success, -1 for failure
12117c478bd9Sstevel@tonic-gate  */
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate static int
12147c478bd9Sstevel@tonic-gate cryptogetkey(CK_BYTE_PTR *pkeydata, CK_ULONG_PTR pkeysize)
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate {
12177c478bd9Sstevel@tonic-gate 	char *keybuf;
12187c478bd9Sstevel@tonic-gate 	char *tmpbuf;
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 	tmpbuf = getpassphrase(gettext("Enter key:"));
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 	if (tmpbuf == NULL) {
12257c478bd9Sstevel@tonic-gate 		return (-1);	/* error */
12267c478bd9Sstevel@tonic-gate 	} else {
12277c478bd9Sstevel@tonic-gate 		keybuf = strdup(tmpbuf);
12287c478bd9Sstevel@tonic-gate 		(void) memset(tmpbuf, 0, strlen(tmpbuf));
12297c478bd9Sstevel@tonic-gate 	}
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	*pkeydata = (CK_BYTE_PTR)keybuf;
12327c478bd9Sstevel@tonic-gate 	*pkeysize = (CK_ULONG)strlen(keybuf);
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	return (0);
12367c478bd9Sstevel@tonic-gate }
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate /*
12397c478bd9Sstevel@tonic-gate  * get_random_data - generate initialization vector data
12407c478bd9Sstevel@tonic-gate  *             iv data is random bytes
12417c478bd9Sstevel@tonic-gate  *  hSession - a pkcs session
12427c478bd9Sstevel@tonic-gate  *  pivbuf - buffer where data is returned
12437c478bd9Sstevel@tonic-gate  *  ivlen - size of iv data
12447c478bd9Sstevel@tonic-gate  */
12457c478bd9Sstevel@tonic-gate static int
12467c478bd9Sstevel@tonic-gate get_random_data(CK_BYTE_PTR pivbuf, int ivlen)
12477c478bd9Sstevel@tonic-gate {
12487c478bd9Sstevel@tonic-gate 	int fd;
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	if (ivlen == 0) {
12517c478bd9Sstevel@tonic-gate 		/* nothing to generate */
12527c478bd9Sstevel@tonic-gate 		return (0);
12537c478bd9Sstevel@tonic-gate 	}
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 	/* Read random data directly from /dev/random */
12567c478bd9Sstevel@tonic-gate 	if ((fd = open(RANDOM_DEVICE, O_RDONLY)) != -1) {
12577c478bd9Sstevel@tonic-gate 		if (read(fd, pivbuf, (size_t)ivlen) == ivlen) {
12587c478bd9Sstevel@tonic-gate 			(void) close(fd);
12597c478bd9Sstevel@tonic-gate 			return (0);
12607c478bd9Sstevel@tonic-gate 		}
12617c478bd9Sstevel@tonic-gate 	}
12627c478bd9Sstevel@tonic-gate 	(void) close(fd);
12637c478bd9Sstevel@tonic-gate 	return (-1);
12647c478bd9Sstevel@tonic-gate }
1265