xref: /titanic_53/usr/src/cmd/cmd-crypto/digest/digest.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * digest.c
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * Implements digest(1) and mac(1) commands
33*7c478bd9Sstevel@tonic-gate  * If command name is mac, performs mac operation
34*7c478bd9Sstevel@tonic-gate  * else perform digest operation
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  * See the man pages for digest and mac for details on
37*7c478bd9Sstevel@tonic-gate  * how these commands work.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #include <stdio.h>
41*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
42*7c478bd9Sstevel@tonic-gate #include <unistd.h>
43*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
44*7c478bd9Sstevel@tonic-gate #include <ctype.h>
45*7c478bd9Sstevel@tonic-gate #include <strings.h>
46*7c478bd9Sstevel@tonic-gate #include <libintl.h>
47*7c478bd9Sstevel@tonic-gate #include <libgen.h>
48*7c478bd9Sstevel@tonic-gate #include <locale.h>
49*7c478bd9Sstevel@tonic-gate #include <errno.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
52*7c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
53*7c478bd9Sstevel@tonic-gate #include <limits.h>
54*7c478bd9Sstevel@tonic-gate #include <cryptoutil.h>
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate #define	BUFFERSIZE	(4096)		/* Buffer size for reading file */
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate /*
59*7c478bd9Sstevel@tonic-gate  * RESULTLEN - large enough size in bytes to hold result for
60*7c478bd9Sstevel@tonic-gate  * digest and mac results for all mechanisms
61*7c478bd9Sstevel@tonic-gate  */
62*7c478bd9Sstevel@tonic-gate #define	RESULTLEN	(512)
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate /*
65*7c478bd9Sstevel@tonic-gate  * Default parameters for PBKDF2 algorithm
66*7c478bd9Sstevel@tonic-gate  */
67*7c478bd9Sstevel@tonic-gate #define	PBKD2_ITERATIONS (1000)
68*7c478bd9Sstevel@tonic-gate #define	PBKD2_SALT_SIZE 16
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate /*
71*7c478bd9Sstevel@tonic-gate  * Exit Status codes
72*7c478bd9Sstevel@tonic-gate  */
73*7c478bd9Sstevel@tonic-gate #ifndef	EXIT_SUCCESS
74*7c478bd9Sstevel@tonic-gate #define	EXIT_SUCCESS	0	/* No errors */
75*7c478bd9Sstevel@tonic-gate #define	EXIT_FAILURE	1	/* All errors except usage */
76*7c478bd9Sstevel@tonic-gate #endif /* EXIT_SUCCESS */
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate #define	EXIT_USAGE	2	/* usage/syntax error */
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate #define	MAC_NAME	"mac"		/* name of mac command */
81*7c478bd9Sstevel@tonic-gate #define	MAC_OPTIONS	"lva:k:"		/* for getopt */
82*7c478bd9Sstevel@tonic-gate #define	DIGEST_NAME	"digest"	/* name of mac command */
83*7c478bd9Sstevel@tonic-gate #define	DIGEST_OPTIONS	"lva:"		/* for getopt */
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate static boolean_t vflag = B_FALSE;	/* -v (verbose) flag, optional */
86*7c478bd9Sstevel@tonic-gate static boolean_t aflag = B_FALSE;	/* -a <algorithm> flag, required */
87*7c478bd9Sstevel@tonic-gate static boolean_t lflag = B_FALSE;	/* -l flag, for mac and digest */
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate static char *keyfile = NULL;	/* name of keyfile */
90*7c478bd9Sstevel@tonic-gate static CK_BYTE buf[BUFFERSIZE];
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate struct mech_alias {
93*7c478bd9Sstevel@tonic-gate 	CK_MECHANISM_TYPE type;
94*7c478bd9Sstevel@tonic-gate 	char *alias;
95*7c478bd9Sstevel@tonic-gate 	CK_ULONG keysize_min;
96*7c478bd9Sstevel@tonic-gate 	CK_ULONG keysize_max;
97*7c478bd9Sstevel@tonic-gate 	int keysize_unit;
98*7c478bd9Sstevel@tonic-gate 	boolean_t available;
99*7c478bd9Sstevel@tonic-gate };
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate #define	MECH_ALIASES_COUNT 5
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate static struct mech_alias mech_aliases[] = {
104*7c478bd9Sstevel@tonic-gate 	{ CKM_SHA_1, "sha1", ULONG_MAX, 0L, 8, B_FALSE },
105*7c478bd9Sstevel@tonic-gate 	{ CKM_MD5, "md5", ULONG_MAX, 0L, 8, B_FALSE },
106*7c478bd9Sstevel@tonic-gate 	{ CKM_DES_MAC, "des_mac", ULONG_MAX, 0L, 8, B_FALSE },
107*7c478bd9Sstevel@tonic-gate 	{ CKM_SHA_1_HMAC, "sha1_hmac", ULONG_MAX, 0L, 8, B_FALSE },
108*7c478bd9Sstevel@tonic-gate 	{ CKM_MD5_HMAC, "md5_hmac", ULONG_MAX, 0L, 8, B_FALSE },
109*7c478bd9Sstevel@tonic-gate };
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate static CK_BBOOL true = TRUE;
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate static void usage(boolean_t mac_cmd);
114*7c478bd9Sstevel@tonic-gate static int execute_cmd(char *algo_str, int filecount,
115*7c478bd9Sstevel@tonic-gate 	char **filelist, boolean_t mac_cmd);
116*7c478bd9Sstevel@tonic-gate static CK_RV do_mac(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pmech,
117*7c478bd9Sstevel@tonic-gate 	int fd, CK_OBJECT_HANDLE key, CK_BYTE_PTR *psignature,
118*7c478bd9Sstevel@tonic-gate 	CK_ULONG_PTR psignaturelen);
119*7c478bd9Sstevel@tonic-gate static CK_RV do_digest(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pmech,
120*7c478bd9Sstevel@tonic-gate 	int fd, CK_BYTE_PTR *pdigest, CK_ULONG_PTR pdigestlen);
121*7c478bd9Sstevel@tonic-gate static int getkey(char *filename, CK_BYTE_PTR *pkeydata);
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate int
124*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
125*7c478bd9Sstevel@tonic-gate {
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	extern char *optarg;
128*7c478bd9Sstevel@tonic-gate 	extern int optind;
129*7c478bd9Sstevel@tonic-gate 	int errflag = 0;	/* We had an optstr parse error */
130*7c478bd9Sstevel@tonic-gate 	char c;			/* current getopts flag */
131*7c478bd9Sstevel@tonic-gate 	char *algo_str;		/* mechanism/algorithm string */
132*7c478bd9Sstevel@tonic-gate 	int filecount;
133*7c478bd9Sstevel@tonic-gate 	boolean_t mac_cmd;	/* if TRUE, do mac, else do digest */
134*7c478bd9Sstevel@tonic-gate 	char *optstr;
135*7c478bd9Sstevel@tonic-gate 	char **filelist;	/* list of files */
136*7c478bd9Sstevel@tonic-gate 	char *cmdname = NULL;	/* name of command */
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
139*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defiend by cc -D */
140*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
141*7c478bd9Sstevel@tonic-gate #endif
142*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	/*
145*7c478bd9Sstevel@tonic-gate 	 * Based on command name, determine
146*7c478bd9Sstevel@tonic-gate 	 * type of command. mac is mac
147*7c478bd9Sstevel@tonic-gate 	 * everything else is digest.
148*7c478bd9Sstevel@tonic-gate 	 */
149*7c478bd9Sstevel@tonic-gate 	cmdname = basename(argv[0]);
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	cryptodebug_init(cmdname);
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	if (strcmp(cmdname, MAC_NAME) == 0)
154*7c478bd9Sstevel@tonic-gate 		mac_cmd = B_TRUE;
155*7c478bd9Sstevel@tonic-gate 	else if (strcmp(cmdname, DIGEST_NAME) == 0)
156*7c478bd9Sstevel@tonic-gate 		mac_cmd = B_FALSE;
157*7c478bd9Sstevel@tonic-gate 	else {
158*7c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
159*7c478bd9Sstevel@tonic-gate 			"command name must be either digest or mac\n"));
160*7c478bd9Sstevel@tonic-gate 		exit(EXIT_USAGE);
161*7c478bd9Sstevel@tonic-gate 	}
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	if (mac_cmd) {
164*7c478bd9Sstevel@tonic-gate 		optstr = MAC_OPTIONS;
165*7c478bd9Sstevel@tonic-gate 	} else {
166*7c478bd9Sstevel@tonic-gate 		optstr = DIGEST_OPTIONS;
167*7c478bd9Sstevel@tonic-gate 	}
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	/* Parse command line arguments */
170*7c478bd9Sstevel@tonic-gate 	while (!errflag && (c = getopt(argc, argv, optstr)) != -1) {
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 		switch (c) {
173*7c478bd9Sstevel@tonic-gate 		case 'v':
174*7c478bd9Sstevel@tonic-gate 			vflag = B_TRUE;
175*7c478bd9Sstevel@tonic-gate 			break;
176*7c478bd9Sstevel@tonic-gate 		case 'a':
177*7c478bd9Sstevel@tonic-gate 			aflag = B_TRUE;
178*7c478bd9Sstevel@tonic-gate 			algo_str = optarg;
179*7c478bd9Sstevel@tonic-gate 			break;
180*7c478bd9Sstevel@tonic-gate 		case 'k':
181*7c478bd9Sstevel@tonic-gate 			keyfile = optarg;
182*7c478bd9Sstevel@tonic-gate 			break;
183*7c478bd9Sstevel@tonic-gate 		case 'l':
184*7c478bd9Sstevel@tonic-gate 			lflag = B_TRUE;
185*7c478bd9Sstevel@tonic-gate 			break;
186*7c478bd9Sstevel@tonic-gate 		default:
187*7c478bd9Sstevel@tonic-gate 			errflag++;
188*7c478bd9Sstevel@tonic-gate 		}
189*7c478bd9Sstevel@tonic-gate 	}
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	filecount = argc - optind;
192*7c478bd9Sstevel@tonic-gate 	if (errflag || (!aflag && !lflag) || (lflag && argc > 2) ||
193*7c478bd9Sstevel@tonic-gate 	    filecount < 0) {
194*7c478bd9Sstevel@tonic-gate 		usage(mac_cmd);
195*7c478bd9Sstevel@tonic-gate 		exit(EXIT_USAGE);
196*7c478bd9Sstevel@tonic-gate 	}
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 	if (filecount == 0) {
199*7c478bd9Sstevel@tonic-gate 		filelist = NULL;
200*7c478bd9Sstevel@tonic-gate 	} else {
201*7c478bd9Sstevel@tonic-gate 		filelist = &argv[optind];
202*7c478bd9Sstevel@tonic-gate 	}
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	return (execute_cmd(algo_str, filecount, filelist, mac_cmd));
205*7c478bd9Sstevel@tonic-gate }
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate /*
208*7c478bd9Sstevel@tonic-gate  * usage message for digest/mac
209*7c478bd9Sstevel@tonic-gate  */
210*7c478bd9Sstevel@tonic-gate static void
211*7c478bd9Sstevel@tonic-gate usage(boolean_t mac_cmd)
212*7c478bd9Sstevel@tonic-gate {
213*7c478bd9Sstevel@tonic-gate 	if (mac_cmd) {
214*7c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
215*7c478bd9Sstevel@tonic-gate 		"usage: mac -l | [-v] -a <algorithm> [-k <keyfile>] "
216*7c478bd9Sstevel@tonic-gate 		    "[file...]"));
217*7c478bd9Sstevel@tonic-gate 	} else {
218*7c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
219*7c478bd9Sstevel@tonic-gate 		    gettext("usage: digest -l | [-v] -a <algorithm> "
220*7c478bd9Sstevel@tonic-gate 			"[file...]"));
221*7c478bd9Sstevel@tonic-gate 	}
222*7c478bd9Sstevel@tonic-gate }
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate /*
225*7c478bd9Sstevel@tonic-gate  * Print out list of available algorithms.
226*7c478bd9Sstevel@tonic-gate  */
227*7c478bd9Sstevel@tonic-gate static void
228*7c478bd9Sstevel@tonic-gate algorithm_list(boolean_t mac_cmd)
229*7c478bd9Sstevel@tonic-gate {
230*7c478bd9Sstevel@tonic-gate 	int mech;
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	if (mac_cmd)
233*7c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Algorithm       Keysize:  Min   "
234*7c478bd9Sstevel@tonic-gate 				"Max (bits)\n"
235*7c478bd9Sstevel@tonic-gate 		    "------------------------------------------\n"));
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 	for (mech = 0; mech < MECH_ALIASES_COUNT; mech++) {
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 		if (mech_aliases[mech].available == B_FALSE)
240*7c478bd9Sstevel@tonic-gate 			continue;
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 		if (mac_cmd) {
243*7c478bd9Sstevel@tonic-gate 			(void) printf("%-15s", mech_aliases[mech].alias);
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 			if (mech_aliases[mech].keysize_min != ULONG_MAX &&
246*7c478bd9Sstevel@tonic-gate 			    mech_aliases[mech].keysize_max != 0)
247*7c478bd9Sstevel@tonic-gate 				(void) printf("         %5lu %5lu\n",
248*7c478bd9Sstevel@tonic-gate 				    (mech_aliases[mech].keysize_min *
249*7c478bd9Sstevel@tonic-gate 					mech_aliases[mech].keysize_unit),
250*7c478bd9Sstevel@tonic-gate 				    (mech_aliases[mech].keysize_max *
251*7c478bd9Sstevel@tonic-gate 					mech_aliases[mech].keysize_unit));
252*7c478bd9Sstevel@tonic-gate 			else
253*7c478bd9Sstevel@tonic-gate 				(void) printf("\n");
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 		} else
256*7c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", mech_aliases[mech].alias);
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 	}
259*7c478bd9Sstevel@tonic-gate }
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate static CK_RV
262*7c478bd9Sstevel@tonic-gate generate_pkcs5_key(CK_SESSION_HANDLE hSession,
263*7c478bd9Sstevel@tonic-gate 		CK_BYTE_PTR	pSaltData,
264*7c478bd9Sstevel@tonic-gate 		CK_ULONG	saltLen,
265*7c478bd9Sstevel@tonic-gate 		CK_ULONG	iterations,
266*7c478bd9Sstevel@tonic-gate 		CK_BYTE_PTR	pkeydata, /* user entered passphrase */
267*7c478bd9Sstevel@tonic-gate 		CK_KEY_TYPE	keytype,
268*7c478bd9Sstevel@tonic-gate 		CK_ULONG	passwd_size,
269*7c478bd9Sstevel@tonic-gate 		CK_ULONG	keylen,	 /* desired length of generated key */
270*7c478bd9Sstevel@tonic-gate 		CK_OBJECT_HANDLE *hKey)
271*7c478bd9Sstevel@tonic-gate {
272*7c478bd9Sstevel@tonic-gate 	CK_RV rv;
273*7c478bd9Sstevel@tonic-gate 	CK_PKCS5_PBKD2_PARAMS params;
274*7c478bd9Sstevel@tonic-gate 	CK_MECHANISM mechanism;
275*7c478bd9Sstevel@tonic-gate 	CK_OBJECT_CLASS class = CKO_SECRET_KEY;
276*7c478bd9Sstevel@tonic-gate 	CK_ATTRIBUTE tmpl[4];
277*7c478bd9Sstevel@tonic-gate 	int attrs = 0;
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 	tmpl[attrs].type = CKA_CLASS;
280*7c478bd9Sstevel@tonic-gate 	tmpl[attrs].pValue = &class;
281*7c478bd9Sstevel@tonic-gate 	tmpl[attrs].ulValueLen = sizeof (class);
282*7c478bd9Sstevel@tonic-gate 	attrs++;
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate 	tmpl[attrs].type = CKA_KEY_TYPE;
285*7c478bd9Sstevel@tonic-gate 	tmpl[attrs].pValue = &keytype;
286*7c478bd9Sstevel@tonic-gate 	tmpl[attrs].ulValueLen = sizeof (keytype);
287*7c478bd9Sstevel@tonic-gate 	attrs++;
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate 	tmpl[attrs].type = CKA_SIGN;
290*7c478bd9Sstevel@tonic-gate 	tmpl[attrs].pValue = &true;
291*7c478bd9Sstevel@tonic-gate 	tmpl[attrs].ulValueLen = sizeof (CK_BBOOL);
292*7c478bd9Sstevel@tonic-gate 	attrs++;
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 	if (keylen > 0) {
295*7c478bd9Sstevel@tonic-gate 		tmpl[attrs].type = CKA_VALUE_LEN;
296*7c478bd9Sstevel@tonic-gate 		tmpl[attrs].pValue = &keylen;
297*7c478bd9Sstevel@tonic-gate 		tmpl[attrs].ulValueLen = sizeof (keylen);
298*7c478bd9Sstevel@tonic-gate 		attrs++;
299*7c478bd9Sstevel@tonic-gate 	}
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 	params.saltSource = CKZ_SALT_SPECIFIED;
302*7c478bd9Sstevel@tonic-gate 	params.pSaltSourceData = (void *)pSaltData;
303*7c478bd9Sstevel@tonic-gate 	params.ulSaltSourceDataLen = saltLen;
304*7c478bd9Sstevel@tonic-gate 	params.iterations = iterations;
305*7c478bd9Sstevel@tonic-gate 	params.prf = CKP_PKCS5_PBKD2_HMAC_SHA1;
306*7c478bd9Sstevel@tonic-gate 	params.pPrfData = NULL;
307*7c478bd9Sstevel@tonic-gate 	params.ulPrfDataLen = 0;
308*7c478bd9Sstevel@tonic-gate 	params.pPassword = (CK_UTF8CHAR_PTR)pkeydata;
309*7c478bd9Sstevel@tonic-gate 	params.ulPasswordLen = &passwd_size;
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate 	mechanism.mechanism = CKM_PKCS5_PBKD2;
312*7c478bd9Sstevel@tonic-gate 	mechanism.pParameter = &params;
313*7c478bd9Sstevel@tonic-gate 	mechanism.ulParameterLen = sizeof (params);
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate 	rv = C_GenerateKey(hSession, &mechanism, tmpl,
316*7c478bd9Sstevel@tonic-gate 		attrs, hKey);
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 	return (rv);
319*7c478bd9Sstevel@tonic-gate }
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate /*
323*7c478bd9Sstevel@tonic-gate  * Execute the command.
324*7c478bd9Sstevel@tonic-gate  *   algo_str - name of algorithm
325*7c478bd9Sstevel@tonic-gate  *   filecount - no. of files to process, if 0, use stdin
326*7c478bd9Sstevel@tonic-gate  *   filelist - list of files
327*7c478bd9Sstevel@tonic-gate  *   mac_cmd - if true do mac else do digest
328*7c478bd9Sstevel@tonic-gate  */
329*7c478bd9Sstevel@tonic-gate static int
330*7c478bd9Sstevel@tonic-gate execute_cmd(char *algo_str, int filecount, char **filelist, boolean_t mac_cmd)
331*7c478bd9Sstevel@tonic-gate {
332*7c478bd9Sstevel@tonic-gate 	int fd;
333*7c478bd9Sstevel@tonic-gate 	char *filename = NULL;
334*7c478bd9Sstevel@tonic-gate 	CK_RV rv;
335*7c478bd9Sstevel@tonic-gate 	CK_ULONG slotcount;
336*7c478bd9Sstevel@tonic-gate 	CK_SLOT_ID slotID;
337*7c478bd9Sstevel@tonic-gate 	CK_SLOT_ID_PTR pSlotList = NULL;
338*7c478bd9Sstevel@tonic-gate 	CK_MECHANISM_TYPE mech_type;
339*7c478bd9Sstevel@tonic-gate 	CK_MECHANISM_INFO info;
340*7c478bd9Sstevel@tonic-gate 	CK_MECHANISM mech;
341*7c478bd9Sstevel@tonic-gate 	CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE;
342*7c478bd9Sstevel@tonic-gate 	CK_BYTE_PTR resultbuf = NULL;
343*7c478bd9Sstevel@tonic-gate 	CK_ULONG resultlen;
344*7c478bd9Sstevel@tonic-gate 	CK_BYTE_PTR	pkeydata = NULL;
345*7c478bd9Sstevel@tonic-gate 	CK_OBJECT_HANDLE key = (CK_OBJECT_HANDLE) 0;
346*7c478bd9Sstevel@tonic-gate 	int keylen = 0;		/* key length */
347*7c478bd9Sstevel@tonic-gate 	char *resultstr = NULL;	/* result in hex string */
348*7c478bd9Sstevel@tonic-gate 	int resultstrlen;	/* result string length */
349*7c478bd9Sstevel@tonic-gate 	int i;
350*7c478bd9Sstevel@tonic-gate 	int exitcode = EXIT_SUCCESS;		/* return code */
351*7c478bd9Sstevel@tonic-gate 	int slot, mek;			/* index variables */
352*7c478bd9Sstevel@tonic-gate 	int mech_match = 0;
353*7c478bd9Sstevel@tonic-gate 	CK_BYTE		salt[PBKD2_SALT_SIZE];
354*7c478bd9Sstevel@tonic-gate 	CK_ULONG	keysize;
355*7c478bd9Sstevel@tonic-gate 	CK_ULONG	iterations = PBKD2_ITERATIONS;
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate 	if (aflag) {
358*7c478bd9Sstevel@tonic-gate 		/*
359*7c478bd9Sstevel@tonic-gate 		 * Determine if algorithm/mechanism is valid
360*7c478bd9Sstevel@tonic-gate 		 */
361*7c478bd9Sstevel@tonic-gate 		for (mech_match = 0; mech_match < MECH_ALIASES_COUNT;
362*7c478bd9Sstevel@tonic-gate 			mech_match++) {
363*7c478bd9Sstevel@tonic-gate 			if (strcmp(algo_str,
364*7c478bd9Sstevel@tonic-gate 			    mech_aliases[mech_match].alias) == 0) {
365*7c478bd9Sstevel@tonic-gate 				mech_type = mech_aliases[mech_match].type;
366*7c478bd9Sstevel@tonic-gate 				break;
367*7c478bd9Sstevel@tonic-gate 			}
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate 		}
370*7c478bd9Sstevel@tonic-gate 
371*7c478bd9Sstevel@tonic-gate 		if (mech_match == MECH_ALIASES_COUNT) {
372*7c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR,
373*7c478bd9Sstevel@tonic-gate 			    gettext("unknown algorithm -- %s"), algo_str);
374*7c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
375*7c478bd9Sstevel@tonic-gate 		}
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate 		/* Get key to do a MAC operation */
378*7c478bd9Sstevel@tonic-gate 		if (mac_cmd) {
379*7c478bd9Sstevel@tonic-gate 			keylen = getkey(keyfile, &pkeydata);
380*7c478bd9Sstevel@tonic-gate 			if (keylen <= 0 || pkeydata == NULL) {
381*7c478bd9Sstevel@tonic-gate 				cryptoerror(LOG_STDERR,
382*7c478bd9Sstevel@tonic-gate 				    gettext("invalid key."));
383*7c478bd9Sstevel@tonic-gate 				return (EXIT_FAILURE);
384*7c478bd9Sstevel@tonic-gate 			}
385*7c478bd9Sstevel@tonic-gate 		}
386*7c478bd9Sstevel@tonic-gate 	}
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate 	/* Initialize, and get list of slots */
389*7c478bd9Sstevel@tonic-gate 	if ((rv = C_Initialize(NULL)) != CKR_OK) {
390*7c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
391*7c478bd9Sstevel@tonic-gate 		    gettext("failed to initialize PKCS #11 framework: %s"),
392*7c478bd9Sstevel@tonic-gate 		    pkcs11_strerror(rv));
393*7c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
394*7c478bd9Sstevel@tonic-gate 	}
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate 	/* Get slot count */
397*7c478bd9Sstevel@tonic-gate 	rv = C_GetSlotList(0, NULL_PTR, &slotcount);
398*7c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK || slotcount == 0) {
399*7c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
400*7c478bd9Sstevel@tonic-gate 		    "failed to find any cryptographic provider,"
401*7c478bd9Sstevel@tonic-gate 		    "please check with your system administrator: %s"),
402*7c478bd9Sstevel@tonic-gate 		    pkcs11_strerror(rv));
403*7c478bd9Sstevel@tonic-gate 		exitcode = EXIT_FAILURE;
404*7c478bd9Sstevel@tonic-gate 		goto cleanup;
405*7c478bd9Sstevel@tonic-gate 	}
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate 	/* Found at least one slot, allocate memory for slot list */
408*7c478bd9Sstevel@tonic-gate 	pSlotList = malloc(slotcount * sizeof (CK_SLOT_ID));
409*7c478bd9Sstevel@tonic-gate 	if (pSlotList == NULL_PTR) {
410*7c478bd9Sstevel@tonic-gate 		int err = errno;
411*7c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("malloc: %s\n"),
412*7c478bd9Sstevel@tonic-gate 		    strerror(err));
413*7c478bd9Sstevel@tonic-gate 		exitcode = EXIT_FAILURE;
414*7c478bd9Sstevel@tonic-gate 		goto cleanup;
415*7c478bd9Sstevel@tonic-gate 	}
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate 	/* Get the list of slots */
418*7c478bd9Sstevel@tonic-gate 	if ((rv = C_GetSlotList(0, pSlotList, &slotcount)) != CKR_OK) {
419*7c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
420*7c478bd9Sstevel@tonic-gate 		    "failed to find any cryptographic provider,"
421*7c478bd9Sstevel@tonic-gate 		    "please check with your system administrator: %s"),
422*7c478bd9Sstevel@tonic-gate 		    pkcs11_strerror(rv));
423*7c478bd9Sstevel@tonic-gate 		exitcode = EXIT_FAILURE;
424*7c478bd9Sstevel@tonic-gate 		goto cleanup;
425*7c478bd9Sstevel@tonic-gate 	}
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate 	/*
428*7c478bd9Sstevel@tonic-gate 	 * Obtain list of algorithms if -l option was given
429*7c478bd9Sstevel@tonic-gate 	 */
430*7c478bd9Sstevel@tonic-gate 	if (lflag) {
431*7c478bd9Sstevel@tonic-gate 
432*7c478bd9Sstevel@tonic-gate 		for (slot = 0; slot < slotcount; slot++) {
433*7c478bd9Sstevel@tonic-gate 
434*7c478bd9Sstevel@tonic-gate 			/* Iterate through each mechanism */
435*7c478bd9Sstevel@tonic-gate 			for (mek = 0; mek < MECH_ALIASES_COUNT; mek++) {
436*7c478bd9Sstevel@tonic-gate 				rv = C_GetMechanismInfo(pSlotList[slot],
437*7c478bd9Sstevel@tonic-gate 				    mech_aliases[mek].type, &info);
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 				/* Only check algorithms that can be used */
440*7c478bd9Sstevel@tonic-gate 				if ((rv != CKR_OK) ||
441*7c478bd9Sstevel@tonic-gate 				    (!mac_cmd && (info.flags & CKF_SIGN)) ||
442*7c478bd9Sstevel@tonic-gate 				    (mac_cmd && (info.flags & CKF_DIGEST)))
443*7c478bd9Sstevel@tonic-gate 					continue;
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 				/*
446*7c478bd9Sstevel@tonic-gate 				 * Set to minimum/maximum key sizes assuming
447*7c478bd9Sstevel@tonic-gate 				 * the values available are not 0.
448*7c478bd9Sstevel@tonic-gate 				 */
449*7c478bd9Sstevel@tonic-gate 				if (info.ulMinKeySize && (info.ulMinKeySize <
450*7c478bd9Sstevel@tonic-gate 				    mech_aliases[mek].keysize_min))
451*7c478bd9Sstevel@tonic-gate 					mech_aliases[mek].keysize_min =
452*7c478bd9Sstevel@tonic-gate 						    info.ulMinKeySize;
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate 				if (info.ulMaxKeySize && (info.ulMaxKeySize >
455*7c478bd9Sstevel@tonic-gate 				    mech_aliases[mek].keysize_max))
456*7c478bd9Sstevel@tonic-gate 					mech_aliases[mek].keysize_max =
457*7c478bd9Sstevel@tonic-gate 						    info.ulMaxKeySize;
458*7c478bd9Sstevel@tonic-gate 
459*7c478bd9Sstevel@tonic-gate 				mech_aliases[mek].available = B_TRUE;
460*7c478bd9Sstevel@tonic-gate 			}
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate 		}
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 		algorithm_list(mac_cmd);
465*7c478bd9Sstevel@tonic-gate 
466*7c478bd9Sstevel@tonic-gate 		goto cleanup;
467*7c478bd9Sstevel@tonic-gate 	}
468*7c478bd9Sstevel@tonic-gate 
469*7c478bd9Sstevel@tonic-gate 	/* Find a slot with matching mechanism */
470*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < slotcount; i++) {
471*7c478bd9Sstevel@tonic-gate 		slotID = pSlotList[i];
472*7c478bd9Sstevel@tonic-gate 		rv = C_GetMechanismInfo(slotID, mech_type, &info);
473*7c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK) {
474*7c478bd9Sstevel@tonic-gate 			continue; /* to the next slot */
475*7c478bd9Sstevel@tonic-gate 		} else {
476*7c478bd9Sstevel@tonic-gate 			if (mac_cmd) {
477*7c478bd9Sstevel@tonic-gate 				/*
478*7c478bd9Sstevel@tonic-gate 				 * Make sure the slot supports
479*7c478bd9Sstevel@tonic-gate 				 * PKCS5 key generation if we
480*7c478bd9Sstevel@tonic-gate 				 * will be using it later.
481*7c478bd9Sstevel@tonic-gate 				 * We use it whenever the key
482*7c478bd9Sstevel@tonic-gate 				 * is entered at command line.
483*7c478bd9Sstevel@tonic-gate 				 */
484*7c478bd9Sstevel@tonic-gate 				if ((info.flags & CKF_SIGN) &&
485*7c478bd9Sstevel@tonic-gate 				    (keyfile == NULL)) {
486*7c478bd9Sstevel@tonic-gate 					CK_MECHANISM_INFO kg_info;
487*7c478bd9Sstevel@tonic-gate 					rv = C_GetMechanismInfo(slotID,
488*7c478bd9Sstevel@tonic-gate 					    CKM_PKCS5_PBKD2, &kg_info);
489*7c478bd9Sstevel@tonic-gate 					if (rv == CKR_OK)
490*7c478bd9Sstevel@tonic-gate 					    break;
491*7c478bd9Sstevel@tonic-gate 				} else if (info.flags & CKF_SIGN) {
492*7c478bd9Sstevel@tonic-gate 					break;
493*7c478bd9Sstevel@tonic-gate 				}
494*7c478bd9Sstevel@tonic-gate 			} else {
495*7c478bd9Sstevel@tonic-gate 				if (info.flags & CKF_DIGEST)
496*7c478bd9Sstevel@tonic-gate 					break;
497*7c478bd9Sstevel@tonic-gate 			}
498*7c478bd9Sstevel@tonic-gate 		}
499*7c478bd9Sstevel@tonic-gate 	}
500*7c478bd9Sstevel@tonic-gate 
501*7c478bd9Sstevel@tonic-gate 	/* Show error if no matching mechanism found */
502*7c478bd9Sstevel@tonic-gate 	if (i == slotcount) {
503*7c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
504*7c478bd9Sstevel@tonic-gate 		    gettext("no cryptographic provider was "
505*7c478bd9Sstevel@tonic-gate 		    "found for this algorithm -- %s"), algo_str);
506*7c478bd9Sstevel@tonic-gate 		exitcode = EXIT_FAILURE;
507*7c478bd9Sstevel@tonic-gate 		goto cleanup;
508*7c478bd9Sstevel@tonic-gate 	}
509*7c478bd9Sstevel@tonic-gate 
510*7c478bd9Sstevel@tonic-gate 	/* Mechanism is supported. Go ahead & open a session */
511*7c478bd9Sstevel@tonic-gate 	rv = C_OpenSession(slotID, CKF_SERIAL_SESSION,
512*7c478bd9Sstevel@tonic-gate 		NULL_PTR, NULL, &hSession);
513*7c478bd9Sstevel@tonic-gate 
514*7c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
515*7c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
516*7c478bd9Sstevel@tonic-gate 		    gettext("can not open PKCS#11 session: %s"),
517*7c478bd9Sstevel@tonic-gate 		    pkcs11_strerror(rv));
518*7c478bd9Sstevel@tonic-gate 		exitcode = EXIT_FAILURE;
519*7c478bd9Sstevel@tonic-gate 		goto cleanup;
520*7c478bd9Sstevel@tonic-gate 	}
521*7c478bd9Sstevel@tonic-gate 
522*7c478bd9Sstevel@tonic-gate 	/* Create a key object for mac operation */
523*7c478bd9Sstevel@tonic-gate 	if (mac_cmd) {
524*7c478bd9Sstevel@tonic-gate 		/*
525*7c478bd9Sstevel@tonic-gate 		 * If we read keybytes from a file,
526*7c478bd9Sstevel@tonic-gate 		 * do NOT process them with C_GenerateKey,
527*7c478bd9Sstevel@tonic-gate 		 * treat them as raw keydata bytes and
528*7c478bd9Sstevel@tonic-gate 		 * create a key object for them.
529*7c478bd9Sstevel@tonic-gate 		 */
530*7c478bd9Sstevel@tonic-gate 		if (keyfile) {
531*7c478bd9Sstevel@tonic-gate 			CK_OBJECT_CLASS class = CKO_SECRET_KEY;
532*7c478bd9Sstevel@tonic-gate 			CK_KEY_TYPE tmpl_keytype = CKK_GENERIC_SECRET;
533*7c478bd9Sstevel@tonic-gate 			CK_BBOOL false = FALSE;
534*7c478bd9Sstevel@tonic-gate 			int nattr = 0;
535*7c478bd9Sstevel@tonic-gate 			CK_ATTRIBUTE template[5];
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate 			if (mech_type == CKM_DES_MAC) {
538*7c478bd9Sstevel@tonic-gate 				tmpl_keytype = CKK_DES;
539*7c478bd9Sstevel@tonic-gate 			}
540*7c478bd9Sstevel@tonic-gate 			template[nattr].type = CKA_CLASS;
541*7c478bd9Sstevel@tonic-gate 			template[nattr].pValue = &class;
542*7c478bd9Sstevel@tonic-gate 			template[nattr].ulValueLen = sizeof (class);
543*7c478bd9Sstevel@tonic-gate 			nattr++;
544*7c478bd9Sstevel@tonic-gate 
545*7c478bd9Sstevel@tonic-gate 			template[nattr].type = CKA_KEY_TYPE;
546*7c478bd9Sstevel@tonic-gate 			template[nattr].pValue = &tmpl_keytype;
547*7c478bd9Sstevel@tonic-gate 			template[nattr].ulValueLen = sizeof (tmpl_keytype);
548*7c478bd9Sstevel@tonic-gate 			nattr++;
549*7c478bd9Sstevel@tonic-gate 
550*7c478bd9Sstevel@tonic-gate 			template[nattr].type = CKA_SIGN;
551*7c478bd9Sstevel@tonic-gate 			template[nattr].pValue = &true;
552*7c478bd9Sstevel@tonic-gate 			template[nattr].ulValueLen = sizeof (true);
553*7c478bd9Sstevel@tonic-gate 			nattr++;
554*7c478bd9Sstevel@tonic-gate 
555*7c478bd9Sstevel@tonic-gate 			template[nattr].type = CKA_TOKEN;
556*7c478bd9Sstevel@tonic-gate 			template[nattr].pValue = &false;
557*7c478bd9Sstevel@tonic-gate 			template[nattr].ulValueLen = sizeof (false);
558*7c478bd9Sstevel@tonic-gate 			nattr++;
559*7c478bd9Sstevel@tonic-gate 
560*7c478bd9Sstevel@tonic-gate 			template[nattr].type = CKA_VALUE;
561*7c478bd9Sstevel@tonic-gate 			template[nattr].pValue = pkeydata;
562*7c478bd9Sstevel@tonic-gate 			template[nattr].ulValueLen = keylen;
563*7c478bd9Sstevel@tonic-gate 			nattr++;
564*7c478bd9Sstevel@tonic-gate 
565*7c478bd9Sstevel@tonic-gate 			rv = C_CreateObject(hSession, template,
566*7c478bd9Sstevel@tonic-gate 				nattr, &key);
567*7c478bd9Sstevel@tonic-gate 		} else {
568*7c478bd9Sstevel@tonic-gate 			CK_KEY_TYPE keytype;
569*7c478bd9Sstevel@tonic-gate 			if (mech_type == CKM_DES_MAC) {
570*7c478bd9Sstevel@tonic-gate 				keytype = CKK_DES;
571*7c478bd9Sstevel@tonic-gate 				keysize = 0;
572*7c478bd9Sstevel@tonic-gate 			} else {
573*7c478bd9Sstevel@tonic-gate 				keytype = CKK_GENERIC_SECRET;
574*7c478bd9Sstevel@tonic-gate 				keysize = 16; /* 128 bits */
575*7c478bd9Sstevel@tonic-gate 			}
576*7c478bd9Sstevel@tonic-gate 			/*
577*7c478bd9Sstevel@tonic-gate 			 * We use a fixed salt (0x0a, 0x0a, 0x0a ...)
578*7c478bd9Sstevel@tonic-gate 			 * for creating the key so that the end user
579*7c478bd9Sstevel@tonic-gate 			 * will be able to generate the same 'mac'
580*7c478bd9Sstevel@tonic-gate 			 * using the same passphrase.
581*7c478bd9Sstevel@tonic-gate 			 */
582*7c478bd9Sstevel@tonic-gate 			(void) memset(salt, 0x0a, sizeof (salt));
583*7c478bd9Sstevel@tonic-gate 			rv = generate_pkcs5_key(hSession,
584*7c478bd9Sstevel@tonic-gate 				salt, sizeof (salt),
585*7c478bd9Sstevel@tonic-gate 				iterations, pkeydata,
586*7c478bd9Sstevel@tonic-gate 				keytype, keylen, keysize,
587*7c478bd9Sstevel@tonic-gate 				&key);
588*7c478bd9Sstevel@tonic-gate 		}
589*7c478bd9Sstevel@tonic-gate 
590*7c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK) {
591*7c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR,
592*7c478bd9Sstevel@tonic-gate 			    gettext("unable to create key for crypto "
593*7c478bd9Sstevel@tonic-gate 			    "operation: %s"), pkcs11_strerror(rv));
594*7c478bd9Sstevel@tonic-gate 			exitcode = EXIT_FAILURE;
595*7c478bd9Sstevel@tonic-gate 			goto cleanup;
596*7c478bd9Sstevel@tonic-gate 		}
597*7c478bd9Sstevel@tonic-gate 	}
598*7c478bd9Sstevel@tonic-gate 
599*7c478bd9Sstevel@tonic-gate 	/* Allocate a buffer to store result. */
600*7c478bd9Sstevel@tonic-gate 	resultlen = RESULTLEN;
601*7c478bd9Sstevel@tonic-gate 	if ((resultbuf = malloc(resultlen)) == NULL) {
602*7c478bd9Sstevel@tonic-gate 		int err = errno;
603*7c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("malloc: %s\n"),
604*7c478bd9Sstevel@tonic-gate 		    strerror(err));
605*7c478bd9Sstevel@tonic-gate 		exitcode = EXIT_FAILURE;
606*7c478bd9Sstevel@tonic-gate 		goto cleanup;
607*7c478bd9Sstevel@tonic-gate 	}
608*7c478bd9Sstevel@tonic-gate 
609*7c478bd9Sstevel@tonic-gate 	/* Allocate a buffer to store result string */
610*7c478bd9Sstevel@tonic-gate 	resultstrlen = RESULTLEN;
611*7c478bd9Sstevel@tonic-gate 	if ((resultstr = malloc(resultstrlen)) == NULL) {
612*7c478bd9Sstevel@tonic-gate 		int err = errno;
613*7c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("malloc: %s\n"),
614*7c478bd9Sstevel@tonic-gate 		    strerror(err));
615*7c478bd9Sstevel@tonic-gate 		exitcode = EXIT_FAILURE;
616*7c478bd9Sstevel@tonic-gate 		goto cleanup;
617*7c478bd9Sstevel@tonic-gate 	}
618*7c478bd9Sstevel@tonic-gate 
619*7c478bd9Sstevel@tonic-gate 	mech.mechanism = mech_type;
620*7c478bd9Sstevel@tonic-gate 	mech.pParameter = NULL_PTR;
621*7c478bd9Sstevel@tonic-gate 	mech.ulParameterLen = 0;
622*7c478bd9Sstevel@tonic-gate 	exitcode = EXIT_SUCCESS;
623*7c478bd9Sstevel@tonic-gate 	i = 0;
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate 	do {
626*7c478bd9Sstevel@tonic-gate 		if (filecount > 0 && filelist != NULL) {
627*7c478bd9Sstevel@tonic-gate 			filename = filelist[i];
628*7c478bd9Sstevel@tonic-gate 			if ((fd = open(filename, O_RDONLY
629*7c478bd9Sstevel@tonic-gate 					| O_NONBLOCK)) == -1) {
630*7c478bd9Sstevel@tonic-gate 				cryptoerror(LOG_STDERR, gettext(
631*7c478bd9Sstevel@tonic-gate 				    "can not open input file %s\n"), filename);
632*7c478bd9Sstevel@tonic-gate 				exitcode = EXIT_USAGE;
633*7c478bd9Sstevel@tonic-gate 				continue;
634*7c478bd9Sstevel@tonic-gate 			}
635*7c478bd9Sstevel@tonic-gate 		} else {
636*7c478bd9Sstevel@tonic-gate 			fd = 0; /* use stdin */
637*7c478bd9Sstevel@tonic-gate 		}
638*7c478bd9Sstevel@tonic-gate 
639*7c478bd9Sstevel@tonic-gate 		/*
640*7c478bd9Sstevel@tonic-gate 		 * Perform the operation
641*7c478bd9Sstevel@tonic-gate 		 */
642*7c478bd9Sstevel@tonic-gate 		if (mac_cmd) {
643*7c478bd9Sstevel@tonic-gate 			rv = do_mac(hSession, &mech, fd, key, &resultbuf,
644*7c478bd9Sstevel@tonic-gate 				&resultlen);
645*7c478bd9Sstevel@tonic-gate 		} else {
646*7c478bd9Sstevel@tonic-gate 			rv = do_digest(hSession, &mech, fd, &resultbuf,
647*7c478bd9Sstevel@tonic-gate 				&resultlen);
648*7c478bd9Sstevel@tonic-gate 		}
649*7c478bd9Sstevel@tonic-gate 
650*7c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK) {
651*7c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR,
652*7c478bd9Sstevel@tonic-gate 			    gettext("crypto operation failed for "
653*7c478bd9Sstevel@tonic-gate 				"file %s: %s\n"),
654*7c478bd9Sstevel@tonic-gate 			    filename ? filename : "STDIN",
655*7c478bd9Sstevel@tonic-gate 			    pkcs11_strerror(rv));
656*7c478bd9Sstevel@tonic-gate 			exitcode = EXIT_FAILURE;
657*7c478bd9Sstevel@tonic-gate 			continue;
658*7c478bd9Sstevel@tonic-gate 		}
659*7c478bd9Sstevel@tonic-gate 
660*7c478bd9Sstevel@tonic-gate 		/* if result size has changed, allocate a bigger resulstr buf */
661*7c478bd9Sstevel@tonic-gate 		if (resultlen != RESULTLEN) {
662*7c478bd9Sstevel@tonic-gate 			resultstrlen = 2 * resultlen + 1;
663*7c478bd9Sstevel@tonic-gate 			resultstr = realloc(resultstr, resultstrlen);
664*7c478bd9Sstevel@tonic-gate 
665*7c478bd9Sstevel@tonic-gate 			if (resultstr == NULL) {
666*7c478bd9Sstevel@tonic-gate 				int err = errno;
667*7c478bd9Sstevel@tonic-gate 				cryptoerror(LOG_STDERR,
668*7c478bd9Sstevel@tonic-gate 				    gettext("realloc: %s\n"), strerror(err));
669*7c478bd9Sstevel@tonic-gate 				exitcode =  EXIT_FAILURE;
670*7c478bd9Sstevel@tonic-gate 				goto cleanup;
671*7c478bd9Sstevel@tonic-gate 			}
672*7c478bd9Sstevel@tonic-gate 		}
673*7c478bd9Sstevel@tonic-gate 
674*7c478bd9Sstevel@tonic-gate 		/* Output the result */
675*7c478bd9Sstevel@tonic-gate 		tohexstr(resultbuf, resultlen, resultstr, resultstrlen);
676*7c478bd9Sstevel@tonic-gate 
677*7c478bd9Sstevel@tonic-gate 		/* Include mechanism name for verbose */
678*7c478bd9Sstevel@tonic-gate 		if (vflag)
679*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "%s ", algo_str);
680*7c478bd9Sstevel@tonic-gate 
681*7c478bd9Sstevel@tonic-gate 		/* Include file name for multiple files, or if verbose */
682*7c478bd9Sstevel@tonic-gate 		if (filecount > 1 || (vflag && filecount > 0)) {
683*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "(%s) = ", filename);
684*7c478bd9Sstevel@tonic-gate 		}
685*7c478bd9Sstevel@tonic-gate 
686*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\n", resultstr);
687*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
688*7c478bd9Sstevel@tonic-gate 
689*7c478bd9Sstevel@tonic-gate 
690*7c478bd9Sstevel@tonic-gate 	} while (++i < filecount);
691*7c478bd9Sstevel@tonic-gate 
692*7c478bd9Sstevel@tonic-gate 
693*7c478bd9Sstevel@tonic-gate 	/* clear and free the key */
694*7c478bd9Sstevel@tonic-gate 	if (mac_cmd) {
695*7c478bd9Sstevel@tonic-gate 		(void) memset(pkeydata, 0, keylen);
696*7c478bd9Sstevel@tonic-gate 		free(pkeydata);
697*7c478bd9Sstevel@tonic-gate 		pkeydata = NULL;
698*7c478bd9Sstevel@tonic-gate 	}
699*7c478bd9Sstevel@tonic-gate 
700*7c478bd9Sstevel@tonic-gate cleanup:
701*7c478bd9Sstevel@tonic-gate 	if (resultbuf != NULL) {
702*7c478bd9Sstevel@tonic-gate 		free(resultbuf);
703*7c478bd9Sstevel@tonic-gate 	}
704*7c478bd9Sstevel@tonic-gate 
705*7c478bd9Sstevel@tonic-gate 	if (resultstr != NULL) {
706*7c478bd9Sstevel@tonic-gate 		free(resultstr);
707*7c478bd9Sstevel@tonic-gate 	}
708*7c478bd9Sstevel@tonic-gate 
709*7c478bd9Sstevel@tonic-gate 	if (pSlotList != NULL) {
710*7c478bd9Sstevel@tonic-gate 		free(pSlotList);
711*7c478bd9Sstevel@tonic-gate 	}
712*7c478bd9Sstevel@tonic-gate 
713*7c478bd9Sstevel@tonic-gate 	if (key != (CK_OBJECT_HANDLE) 0) {
714*7c478bd9Sstevel@tonic-gate 		(void) C_DestroyObject(hSession, key);
715*7c478bd9Sstevel@tonic-gate 	}
716*7c478bd9Sstevel@tonic-gate 
717*7c478bd9Sstevel@tonic-gate 	if (hSession != CK_INVALID_HANDLE)
718*7c478bd9Sstevel@tonic-gate 		(void) C_CloseSession(hSession);
719*7c478bd9Sstevel@tonic-gate 
720*7c478bd9Sstevel@tonic-gate 	(void) C_Finalize(NULL_PTR);
721*7c478bd9Sstevel@tonic-gate 
722*7c478bd9Sstevel@tonic-gate 	return (exitcode);
723*7c478bd9Sstevel@tonic-gate }
724*7c478bd9Sstevel@tonic-gate 
725*7c478bd9Sstevel@tonic-gate /*
726*7c478bd9Sstevel@tonic-gate  * do_digest - Compute digest of a file
727*7c478bd9Sstevel@tonic-gate  *
728*7c478bd9Sstevel@tonic-gate  *  hSession - session
729*7c478bd9Sstevel@tonic-gate  *  pmech - ptr to mechanism to be used for digest
730*7c478bd9Sstevel@tonic-gate  *  fd  - file descriptor
731*7c478bd9Sstevel@tonic-gate  *  pdigest - buffer  where digest result is returned
732*7c478bd9Sstevel@tonic-gate  *  pdigestlen - length of digest buffer on input,
733*7c478bd9Sstevel@tonic-gate  *               length of result on output
734*7c478bd9Sstevel@tonic-gate  */
735*7c478bd9Sstevel@tonic-gate static CK_RV
736*7c478bd9Sstevel@tonic-gate do_digest(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pmech,
737*7c478bd9Sstevel@tonic-gate 	int fd, CK_BYTE_PTR *pdigest, CK_ULONG_PTR pdigestlen)
738*7c478bd9Sstevel@tonic-gate {
739*7c478bd9Sstevel@tonic-gate 	CK_RV rv;
740*7c478bd9Sstevel@tonic-gate 	ssize_t nread;
741*7c478bd9Sstevel@tonic-gate 	int saved_errno;
742*7c478bd9Sstevel@tonic-gate 
743*7c478bd9Sstevel@tonic-gate 	if ((rv = C_DigestInit(hSession, pmech)) != CKR_OK) {
744*7c478bd9Sstevel@tonic-gate 		return (rv);
745*7c478bd9Sstevel@tonic-gate 	}
746*7c478bd9Sstevel@tonic-gate 
747*7c478bd9Sstevel@tonic-gate 	while ((nread = read(fd, buf, sizeof (buf))) > 0) {
748*7c478bd9Sstevel@tonic-gate 		/* Get the digest */
749*7c478bd9Sstevel@tonic-gate 		rv = C_DigestUpdate(hSession, buf, (CK_ULONG)nread);
750*7c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK)
751*7c478bd9Sstevel@tonic-gate 			return (rv);
752*7c478bd9Sstevel@tonic-gate 	}
753*7c478bd9Sstevel@tonic-gate 
754*7c478bd9Sstevel@tonic-gate 	saved_errno = errno; /* for later use */
755*7c478bd9Sstevel@tonic-gate 
756*7c478bd9Sstevel@tonic-gate 	/*
757*7c478bd9Sstevel@tonic-gate 	 * Perform the C_DigestFinal, even if there is a read error.
758*7c478bd9Sstevel@tonic-gate 	 * Otherwise C_DigestInit will return CKR_OPERATION_ACTIVE
759*7c478bd9Sstevel@tonic-gate 	 * next time it is called (for another file)
760*7c478bd9Sstevel@tonic-gate 	 */
761*7c478bd9Sstevel@tonic-gate 
762*7c478bd9Sstevel@tonic-gate 	rv = C_DigestFinal(hSession, *pdigest, pdigestlen);
763*7c478bd9Sstevel@tonic-gate 
764*7c478bd9Sstevel@tonic-gate 	/* result too big to fit? Allocate a bigger buffer */
765*7c478bd9Sstevel@tonic-gate 	if (rv == CKR_BUFFER_TOO_SMALL) {
766*7c478bd9Sstevel@tonic-gate 		*pdigest = realloc(*pdigest, *pdigestlen);
767*7c478bd9Sstevel@tonic-gate 
768*7c478bd9Sstevel@tonic-gate 		if (*pdigest == NULL_PTR) {
769*7c478bd9Sstevel@tonic-gate 			int err = errno;
770*7c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR,
771*7c478bd9Sstevel@tonic-gate 			    gettext("realloc: %s\n"), strerror(err));
772*7c478bd9Sstevel@tonic-gate 			return (CKR_HOST_MEMORY);
773*7c478bd9Sstevel@tonic-gate 		}
774*7c478bd9Sstevel@tonic-gate 
775*7c478bd9Sstevel@tonic-gate 		rv = C_DigestFinal(hSession, *pdigest, pdigestlen);
776*7c478bd9Sstevel@tonic-gate 	}
777*7c478bd9Sstevel@tonic-gate 
778*7c478bd9Sstevel@tonic-gate 
779*7c478bd9Sstevel@tonic-gate 	/* There was a read error */
780*7c478bd9Sstevel@tonic-gate 	if (nread == -1) {
781*7c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext(
782*7c478bd9Sstevel@tonic-gate 			"error reading file: %s"), strerror(saved_errno));
783*7c478bd9Sstevel@tonic-gate 		return (CKR_GENERAL_ERROR);
784*7c478bd9Sstevel@tonic-gate 	} else {
785*7c478bd9Sstevel@tonic-gate 		return (rv);
786*7c478bd9Sstevel@tonic-gate 	}
787*7c478bd9Sstevel@tonic-gate }
788*7c478bd9Sstevel@tonic-gate 
789*7c478bd9Sstevel@tonic-gate /*
790*7c478bd9Sstevel@tonic-gate  * do_mac - Compute mac of a file
791*7c478bd9Sstevel@tonic-gate  *
792*7c478bd9Sstevel@tonic-gate  *  hSession - session
793*7c478bd9Sstevel@tonic-gate  *  pmech - ptr to mechanism to be used
794*7c478bd9Sstevel@tonic-gate  *  fd  - file descriptor
795*7c478bd9Sstevel@tonic-gate  *  key - key to be used
796*7c478bd9Sstevel@tonic-gate  *  psignature - ptr buffer  where mac result is returned
797*7c478bd9Sstevel@tonic-gate  *		returns new buf if current buf is small
798*7c478bd9Sstevel@tonic-gate  *  psignaturelen - length of mac buffer on input,
799*7c478bd9Sstevel@tonic-gate  *               length of result on output
800*7c478bd9Sstevel@tonic-gate  */
801*7c478bd9Sstevel@tonic-gate static CK_RV
802*7c478bd9Sstevel@tonic-gate do_mac(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pmech,
803*7c478bd9Sstevel@tonic-gate 	int fd, CK_OBJECT_HANDLE key, CK_BYTE_PTR *psignature,
804*7c478bd9Sstevel@tonic-gate 	CK_ULONG_PTR psignaturelen)
805*7c478bd9Sstevel@tonic-gate {
806*7c478bd9Sstevel@tonic-gate 	CK_RV rv;
807*7c478bd9Sstevel@tonic-gate 	ssize_t nread;
808*7c478bd9Sstevel@tonic-gate 	int saved_errno;
809*7c478bd9Sstevel@tonic-gate 
810*7c478bd9Sstevel@tonic-gate 	if ((rv = C_SignInit(hSession, pmech, key)) != CKR_OK) {
811*7c478bd9Sstevel@tonic-gate 		return (rv);
812*7c478bd9Sstevel@tonic-gate 	}
813*7c478bd9Sstevel@tonic-gate 
814*7c478bd9Sstevel@tonic-gate 	while ((nread = read(fd, buf, sizeof (buf))) > 0) {
815*7c478bd9Sstevel@tonic-gate 		/* Get the MAC */
816*7c478bd9Sstevel@tonic-gate 		rv = C_SignUpdate(hSession, buf, (CK_ULONG)nread);
817*7c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK)
818*7c478bd9Sstevel@tonic-gate 			return (rv);
819*7c478bd9Sstevel@tonic-gate 	}
820*7c478bd9Sstevel@tonic-gate 
821*7c478bd9Sstevel@tonic-gate 	saved_errno = errno; /* for later use */
822*7c478bd9Sstevel@tonic-gate 
823*7c478bd9Sstevel@tonic-gate 	/*
824*7c478bd9Sstevel@tonic-gate 	 * Perform the C_SignFinal, even if there is a read error.
825*7c478bd9Sstevel@tonic-gate 	 * Otherwise C_SignInit will return CKR_OPERATION_ACTIVE
826*7c478bd9Sstevel@tonic-gate 	 * next time it is called (for another file)
827*7c478bd9Sstevel@tonic-gate 	 */
828*7c478bd9Sstevel@tonic-gate 
829*7c478bd9Sstevel@tonic-gate 	rv = C_SignFinal(hSession, *psignature, psignaturelen);
830*7c478bd9Sstevel@tonic-gate 
831*7c478bd9Sstevel@tonic-gate 	/* result too big to fit? Allocate a bigger buffer */
832*7c478bd9Sstevel@tonic-gate 	if (rv == CKR_BUFFER_TOO_SMALL) {
833*7c478bd9Sstevel@tonic-gate 		*psignature = realloc(*psignature, *psignaturelen);
834*7c478bd9Sstevel@tonic-gate 
835*7c478bd9Sstevel@tonic-gate 		if (*psignature == NULL_PTR) {
836*7c478bd9Sstevel@tonic-gate 			int err = errno;
837*7c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR,
838*7c478bd9Sstevel@tonic-gate 			    gettext("realloc: %s\n"), strerror(err));
839*7c478bd9Sstevel@tonic-gate 			return (CKR_HOST_MEMORY);
840*7c478bd9Sstevel@tonic-gate 		}
841*7c478bd9Sstevel@tonic-gate 
842*7c478bd9Sstevel@tonic-gate 		rv = C_SignFinal(hSession, *psignature, psignaturelen);
843*7c478bd9Sstevel@tonic-gate 	}
844*7c478bd9Sstevel@tonic-gate 
845*7c478bd9Sstevel@tonic-gate 	/* There was a read error */
846*7c478bd9Sstevel@tonic-gate 	if (nread == -1) {
847*7c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, gettext("error reading file: %s"),
848*7c478bd9Sstevel@tonic-gate 			strerror(saved_errno));
849*7c478bd9Sstevel@tonic-gate 		return (CKR_GENERAL_ERROR);
850*7c478bd9Sstevel@tonic-gate 	} else {
851*7c478bd9Sstevel@tonic-gate 		return (rv);
852*7c478bd9Sstevel@tonic-gate 	}
853*7c478bd9Sstevel@tonic-gate }
854*7c478bd9Sstevel@tonic-gate 
855*7c478bd9Sstevel@tonic-gate 
856*7c478bd9Sstevel@tonic-gate /*
857*7c478bd9Sstevel@tonic-gate  * getkey - gets keydata from file specified
858*7c478bd9Sstevel@tonic-gate  *
859*7c478bd9Sstevel@tonic-gate  *  filename - name of file, if null, prompt for pass phrase
860*7c478bd9Sstevel@tonic-gate  *  pkeydata - binary key data is returned in this buf
861*7c478bd9Sstevel@tonic-gate  *
862*7c478bd9Sstevel@tonic-gate  * returns length of key, or -1 if error
863*7c478bd9Sstevel@tonic-gate  */
864*7c478bd9Sstevel@tonic-gate static int
865*7c478bd9Sstevel@tonic-gate getkey(char *filename, CK_BYTE_PTR *pkeydata)
866*7c478bd9Sstevel@tonic-gate {
867*7c478bd9Sstevel@tonic-gate 	struct stat statbuf;
868*7c478bd9Sstevel@tonic-gate 	char *keybuf = NULL;
869*7c478bd9Sstevel@tonic-gate 	char *tmpbuf;
870*7c478bd9Sstevel@tonic-gate 	int keylen;
871*7c478bd9Sstevel@tonic-gate 	int fd;
872*7c478bd9Sstevel@tonic-gate 
873*7c478bd9Sstevel@tonic-gate 	if (filename != NULL) {
874*7c478bd9Sstevel@tonic-gate 
875*7c478bd9Sstevel@tonic-gate 		/* read the key file into a buffer */
876*7c478bd9Sstevel@tonic-gate 		if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) == -1) {
877*7c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
878*7c478bd9Sstevel@tonic-gate 				"can't open %s\n"), filename);
879*7c478bd9Sstevel@tonic-gate 			return (-1);
880*7c478bd9Sstevel@tonic-gate 
881*7c478bd9Sstevel@tonic-gate 		}
882*7c478bd9Sstevel@tonic-gate 
883*7c478bd9Sstevel@tonic-gate 		if (fstat(fd, &statbuf) == -1) {
884*7c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
885*7c478bd9Sstevel@tonic-gate 				"can't stat %s\n"), filename);
886*7c478bd9Sstevel@tonic-gate 			(void) close(fd);
887*7c478bd9Sstevel@tonic-gate 			return (-1);
888*7c478bd9Sstevel@tonic-gate 		}
889*7c478bd9Sstevel@tonic-gate 
890*7c478bd9Sstevel@tonic-gate 		if (!(statbuf.st_mode & S_IFREG)) {
891*7c478bd9Sstevel@tonic-gate 			cryptoerror(LOG_STDERR, gettext(
892*7c478bd9Sstevel@tonic-gate 				"%s not a regular file\n"), filename);
893*7c478bd9Sstevel@tonic-gate 			(void) close(fd);
894*7c478bd9Sstevel@tonic-gate 			return (-1);
895*7c478bd9Sstevel@tonic-gate 		}
896*7c478bd9Sstevel@tonic-gate 
897*7c478bd9Sstevel@tonic-gate 		keylen = (size_t)statbuf.st_size;
898*7c478bd9Sstevel@tonic-gate 
899*7c478bd9Sstevel@tonic-gate 		if (keylen > 0) {
900*7c478bd9Sstevel@tonic-gate 			/* allocate a buffer to hold the entire key */
901*7c478bd9Sstevel@tonic-gate 			if ((keybuf = malloc(keylen)) == NULL) {
902*7c478bd9Sstevel@tonic-gate 				int err = errno;
903*7c478bd9Sstevel@tonic-gate 				cryptoerror(LOG_STDERR, gettext("malloc: %s\n"),
904*7c478bd9Sstevel@tonic-gate 				    strerror(err));
905*7c478bd9Sstevel@tonic-gate 				(void) close(fd);
906*7c478bd9Sstevel@tonic-gate 				return (-1);
907*7c478bd9Sstevel@tonic-gate 			}
908*7c478bd9Sstevel@tonic-gate 
909*7c478bd9Sstevel@tonic-gate 			if (read(fd, keybuf, keylen) != keylen) {
910*7c478bd9Sstevel@tonic-gate 				cryptoerror(LOG_STDERR, gettext(
911*7c478bd9Sstevel@tonic-gate 					"can't read %s\n"), filename);
912*7c478bd9Sstevel@tonic-gate 				(void) close(fd);
913*7c478bd9Sstevel@tonic-gate 				return (-1);
914*7c478bd9Sstevel@tonic-gate 			}
915*7c478bd9Sstevel@tonic-gate 		}
916*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
917*7c478bd9Sstevel@tonic-gate 
918*7c478bd9Sstevel@tonic-gate 	} else {
919*7c478bd9Sstevel@tonic-gate 
920*7c478bd9Sstevel@tonic-gate 		/* No file, prompt for a pass phrase */
921*7c478bd9Sstevel@tonic-gate 		tmpbuf = getpassphrase(gettext("Enter key:"));
922*7c478bd9Sstevel@tonic-gate 
923*7c478bd9Sstevel@tonic-gate 		if (tmpbuf == NULL) {
924*7c478bd9Sstevel@tonic-gate 			return (-1);	/* error */
925*7c478bd9Sstevel@tonic-gate 		} else {
926*7c478bd9Sstevel@tonic-gate 			keybuf = strdup(tmpbuf);
927*7c478bd9Sstevel@tonic-gate 			(void) memset(tmpbuf, 0, strlen(tmpbuf));
928*7c478bd9Sstevel@tonic-gate 		}
929*7c478bd9Sstevel@tonic-gate 		keylen = strlen(keybuf);
930*7c478bd9Sstevel@tonic-gate 	}
931*7c478bd9Sstevel@tonic-gate 
932*7c478bd9Sstevel@tonic-gate 	*pkeydata = (CK_BYTE_PTR)keybuf;
933*7c478bd9Sstevel@tonic-gate 
934*7c478bd9Sstevel@tonic-gate 	return (keylen);
935*7c478bd9Sstevel@tonic-gate }
936