10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
53812Shylee * Common Development and Distribution License (the "License").
63812Shylee * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*11458Sopensolaris@drydog.com * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * digest.c
280Sstevel@tonic-gate *
290Sstevel@tonic-gate * Implements digest(1) and mac(1) commands
300Sstevel@tonic-gate * If command name is mac, performs mac operation
310Sstevel@tonic-gate * else perform digest operation
320Sstevel@tonic-gate *
330Sstevel@tonic-gate * See the man pages for digest and mac for details on
340Sstevel@tonic-gate * how these commands work.
350Sstevel@tonic-gate */
360Sstevel@tonic-gate
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <stdlib.h>
390Sstevel@tonic-gate #include <unistd.h>
400Sstevel@tonic-gate #include <fcntl.h>
410Sstevel@tonic-gate #include <ctype.h>
420Sstevel@tonic-gate #include <strings.h>
430Sstevel@tonic-gate #include <libintl.h>
440Sstevel@tonic-gate #include <libgen.h>
450Sstevel@tonic-gate #include <locale.h>
460Sstevel@tonic-gate #include <errno.h>
470Sstevel@tonic-gate #include <sys/types.h>
480Sstevel@tonic-gate #include <sys/stat.h>
490Sstevel@tonic-gate #include <security/cryptoki.h>
500Sstevel@tonic-gate #include <limits.h>
510Sstevel@tonic-gate #include <cryptoutil.h>
523812Shylee #include <kmfapi.h>
530Sstevel@tonic-gate
540Sstevel@tonic-gate #define BUFFERSIZE (4096) /* Buffer size for reading file */
550Sstevel@tonic-gate
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate * RESULTLEN - large enough size in bytes to hold result for
580Sstevel@tonic-gate * digest and mac results for all mechanisms
590Sstevel@tonic-gate */
600Sstevel@tonic-gate #define RESULTLEN (512)
610Sstevel@tonic-gate
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate * Exit Status codes
640Sstevel@tonic-gate */
650Sstevel@tonic-gate #ifndef EXIT_SUCCESS
660Sstevel@tonic-gate #define EXIT_SUCCESS 0 /* No errors */
670Sstevel@tonic-gate #define EXIT_FAILURE 1 /* All errors except usage */
680Sstevel@tonic-gate #endif /* EXIT_SUCCESS */
690Sstevel@tonic-gate
700Sstevel@tonic-gate #define EXIT_USAGE 2 /* usage/syntax error */
710Sstevel@tonic-gate
720Sstevel@tonic-gate #define MAC_NAME "mac" /* name of mac command */
73*11458Sopensolaris@drydog.com #define MAC_OPTIONS "lva:k:T:K:" /* for getopt */
74*11458Sopensolaris@drydog.com #define DIGEST_NAME "digest" /* name of digest command */
750Sstevel@tonic-gate #define DIGEST_OPTIONS "lva:" /* for getopt */
760Sstevel@tonic-gate
77*11458Sopensolaris@drydog.com /* Saved command line options */
780Sstevel@tonic-gate static boolean_t vflag = B_FALSE; /* -v (verbose) flag, optional */
790Sstevel@tonic-gate static boolean_t aflag = B_FALSE; /* -a <algorithm> flag, required */
800Sstevel@tonic-gate static boolean_t lflag = B_FALSE; /* -l flag, for mac and digest */
81*11458Sopensolaris@drydog.com static boolean_t kflag = B_FALSE; /* -k keyfile */
82*11458Sopensolaris@drydog.com static boolean_t Tflag = B_FALSE; /* -T token_spec */
83*11458Sopensolaris@drydog.com static boolean_t Kflag = B_FALSE; /* -K key_label */
840Sstevel@tonic-gate
85*11458Sopensolaris@drydog.com static char *keyfile = NULL; /* name of file containing key value */
86*11458Sopensolaris@drydog.com static char *token_label = NULL; /* tokensSpec: tokenName[:manufId[:serial]] */
87*11458Sopensolaris@drydog.com static char *key_label = NULL; /* PKCS#11 symmetric token key label */
883812Shylee
890Sstevel@tonic-gate static CK_BYTE buf[BUFFERSIZE];
900Sstevel@tonic-gate
910Sstevel@tonic-gate struct mech_alias {
920Sstevel@tonic-gate CK_MECHANISM_TYPE type;
930Sstevel@tonic-gate char *alias;
940Sstevel@tonic-gate CK_ULONG keysize_min;
950Sstevel@tonic-gate CK_ULONG keysize_max;
960Sstevel@tonic-gate int keysize_unit;
970Sstevel@tonic-gate boolean_t available;
980Sstevel@tonic-gate };
990Sstevel@tonic-gate
100676Sizick #define MECH_ALIASES_COUNT 11
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate static struct mech_alias mech_aliases[] = {
1030Sstevel@tonic-gate { CKM_SHA_1, "sha1", ULONG_MAX, 0L, 8, B_FALSE },
1040Sstevel@tonic-gate { CKM_MD5, "md5", ULONG_MAX, 0L, 8, B_FALSE },
1050Sstevel@tonic-gate { CKM_DES_MAC, "des_mac", ULONG_MAX, 0L, 8, B_FALSE },
1060Sstevel@tonic-gate { CKM_SHA_1_HMAC, "sha1_hmac", ULONG_MAX, 0L, 8, B_FALSE },
1070Sstevel@tonic-gate { CKM_MD5_HMAC, "md5_hmac", ULONG_MAX, 0L, 8, B_FALSE },
108676Sizick { CKM_SHA256, "sha256", ULONG_MAX, 0L, 8, B_FALSE },
109676Sizick { CKM_SHA384, "sha384", ULONG_MAX, 0L, 8, B_FALSE },
110676Sizick { CKM_SHA512, "sha512", ULONG_MAX, 0L, 8, B_FALSE },
111676Sizick { CKM_SHA256_HMAC, "sha256_hmac", ULONG_MAX, 0L, 8, B_FALSE },
112676Sizick { CKM_SHA384_HMAC, "sha384_hmac", ULONG_MAX, 0L, 8, B_FALSE },
113676Sizick { CKM_SHA512_HMAC, "sha512_hmac", ULONG_MAX, 0L, 8, B_FALSE }
1140Sstevel@tonic-gate };
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate static CK_BBOOL true = TRUE;
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate static void usage(boolean_t mac_cmd);
1190Sstevel@tonic-gate static int execute_cmd(char *algo_str, int filecount,
1200Sstevel@tonic-gate char **filelist, boolean_t mac_cmd);
1210Sstevel@tonic-gate static CK_RV do_mac(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pmech,
1220Sstevel@tonic-gate int fd, CK_OBJECT_HANDLE key, CK_BYTE_PTR *psignature,
1230Sstevel@tonic-gate CK_ULONG_PTR psignaturelen);
1240Sstevel@tonic-gate static CK_RV do_digest(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pmech,
1250Sstevel@tonic-gate int fd, CK_BYTE_PTR *pdigest, CK_ULONG_PTR pdigestlen);
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate int
main(int argc,char ** argv)1280Sstevel@tonic-gate main(int argc, char **argv)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate extern char *optarg;
1310Sstevel@tonic-gate extern int optind;
1320Sstevel@tonic-gate int errflag = 0; /* We had an optstr parse error */
1330Sstevel@tonic-gate char c; /* current getopts flag */
1340Sstevel@tonic-gate char *algo_str; /* mechanism/algorithm string */
1350Sstevel@tonic-gate int filecount;
1360Sstevel@tonic-gate boolean_t mac_cmd; /* if TRUE, do mac, else do digest */
1370Sstevel@tonic-gate char *optstr;
1380Sstevel@tonic-gate char **filelist; /* list of files */
1390Sstevel@tonic-gate char *cmdname = NULL; /* name of command */
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1420Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defiend by cc -D */
1430Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
1440Sstevel@tonic-gate #endif
1450Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate /*
1480Sstevel@tonic-gate * Based on command name, determine
1490Sstevel@tonic-gate * type of command. mac is mac
1500Sstevel@tonic-gate * everything else is digest.
1510Sstevel@tonic-gate */
1520Sstevel@tonic-gate cmdname = basename(argv[0]);
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate cryptodebug_init(cmdname);
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate if (strcmp(cmdname, MAC_NAME) == 0)
1570Sstevel@tonic-gate mac_cmd = B_TRUE;
1580Sstevel@tonic-gate else if (strcmp(cmdname, DIGEST_NAME) == 0)
1590Sstevel@tonic-gate mac_cmd = B_FALSE;
1600Sstevel@tonic-gate else {
1610Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext(
1625051Swyllys "command name must be either digest or mac\n"));
1630Sstevel@tonic-gate exit(EXIT_USAGE);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate if (mac_cmd) {
1670Sstevel@tonic-gate optstr = MAC_OPTIONS;
1680Sstevel@tonic-gate } else {
1690Sstevel@tonic-gate optstr = DIGEST_OPTIONS;
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate /* Parse command line arguments */
1730Sstevel@tonic-gate while (!errflag && (c = getopt(argc, argv, optstr)) != -1) {
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate switch (c) {
1760Sstevel@tonic-gate case 'v':
1770Sstevel@tonic-gate vflag = B_TRUE;
1780Sstevel@tonic-gate break;
1790Sstevel@tonic-gate case 'a':
1800Sstevel@tonic-gate aflag = B_TRUE;
1810Sstevel@tonic-gate algo_str = optarg;
1820Sstevel@tonic-gate break;
1830Sstevel@tonic-gate case 'k':
1843812Shylee kflag = B_TRUE;
1850Sstevel@tonic-gate keyfile = optarg;
1860Sstevel@tonic-gate break;
1870Sstevel@tonic-gate case 'l':
1880Sstevel@tonic-gate lflag = B_TRUE;
1890Sstevel@tonic-gate break;
1903812Shylee case 'T':
1913812Shylee Tflag = B_TRUE;
1923812Shylee token_label = optarg;
1933812Shylee break;
1943812Shylee case 'K':
1953812Shylee Kflag = B_TRUE;
1963812Shylee key_label = optarg;
1973812Shylee break;
1980Sstevel@tonic-gate default:
1990Sstevel@tonic-gate errflag++;
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate filecount = argc - optind;
2040Sstevel@tonic-gate if (errflag || (!aflag && !lflag) || (lflag && argc > 2) ||
2053812Shylee (kflag && Kflag) || (Tflag && !Kflag) || filecount < 0) {
2060Sstevel@tonic-gate usage(mac_cmd);
2070Sstevel@tonic-gate exit(EXIT_USAGE);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate if (filecount == 0) {
2110Sstevel@tonic-gate filelist = NULL;
2120Sstevel@tonic-gate } else {
2130Sstevel@tonic-gate filelist = &argv[optind];
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate return (execute_cmd(algo_str, filecount, filelist, mac_cmd));
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate /*
2200Sstevel@tonic-gate * usage message for digest/mac
2210Sstevel@tonic-gate */
2220Sstevel@tonic-gate static void
usage(boolean_t mac_cmd)2230Sstevel@tonic-gate usage(boolean_t mac_cmd)
2240Sstevel@tonic-gate {
2253812Shylee (void) fprintf(stderr, gettext("Usage:\n"));
2260Sstevel@tonic-gate if (mac_cmd) {
2273812Shylee (void) fprintf(stderr, gettext(" mac -l\n"));
2283812Shylee (void) fprintf(stderr, gettext(" mac [-v] -a <algorithm> "
2293812Shylee "[-k <keyfile> | -K <keylabel> [-T <tokenspec>]] "
2303812Shylee "[file...]\n"));
2310Sstevel@tonic-gate } else {
2323812Shylee (void) fprintf(stderr, gettext(" digest -l | [-v] "
2333812Shylee "-a <algorithm> [file...]\n"));
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate /*
2380Sstevel@tonic-gate * Print out list of available algorithms.
2390Sstevel@tonic-gate */
2400Sstevel@tonic-gate static void
algorithm_list(boolean_t mac_cmd)2410Sstevel@tonic-gate algorithm_list(boolean_t mac_cmd)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate int mech;
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate if (mac_cmd)
2460Sstevel@tonic-gate (void) printf(gettext("Algorithm Keysize: Min "
2475051Swyllys "Max (bits)\n"
2480Sstevel@tonic-gate "------------------------------------------\n"));
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate for (mech = 0; mech < MECH_ALIASES_COUNT; mech++) {
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate if (mech_aliases[mech].available == B_FALSE)
2530Sstevel@tonic-gate continue;
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate if (mac_cmd) {
2560Sstevel@tonic-gate (void) printf("%-15s", mech_aliases[mech].alias);
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate if (mech_aliases[mech].keysize_min != ULONG_MAX &&
2590Sstevel@tonic-gate mech_aliases[mech].keysize_max != 0)
2600Sstevel@tonic-gate (void) printf(" %5lu %5lu\n",
2610Sstevel@tonic-gate (mech_aliases[mech].keysize_min *
2625051Swyllys mech_aliases[mech].keysize_unit),
2630Sstevel@tonic-gate (mech_aliases[mech].keysize_max *
2645051Swyllys mech_aliases[mech].keysize_unit));
2650Sstevel@tonic-gate else
2660Sstevel@tonic-gate (void) printf("\n");
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate } else
2690Sstevel@tonic-gate (void) printf("%s\n", mech_aliases[mech].alias);
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate
2743812Shylee static int
get_token_key(CK_SESSION_HANDLE hSession,CK_KEY_TYPE keytype,char * keylabel,CK_BYTE * password,int password_len,CK_OBJECT_HANDLE * keyobj)2753812Shylee get_token_key(CK_SESSION_HANDLE hSession, CK_KEY_TYPE keytype,
2763812Shylee char *keylabel, CK_BYTE *password, int password_len,
2773812Shylee CK_OBJECT_HANDLE *keyobj)
2783812Shylee {
2793812Shylee CK_RV rv;
2803812Shylee CK_ATTRIBUTE pTmpl[10];
2813812Shylee CK_OBJECT_CLASS class = CKO_SECRET_KEY;
2823812Shylee CK_BBOOL true = 1;
2833812Shylee CK_BBOOL is_token = 1;
2843812Shylee CK_ULONG key_obj_count = 1;
2853812Shylee int i;
2863812Shylee CK_KEY_TYPE ckKeyType = keytype;
2873812Shylee
2883812Shylee
2893812Shylee rv = C_Login(hSession, CKU_USER, (CK_UTF8CHAR_PTR)password,
2903812Shylee password_len);
2913812Shylee if (rv != CKR_OK) {
2923812Shylee (void) fprintf(stderr, "Cannot login to the token."
2933812Shylee " error = %s\n", pkcs11_strerror(rv));
2943812Shylee return (-1);
2953812Shylee }
2963812Shylee
2973812Shylee i = 0;
2983812Shylee pTmpl[i].type = CKA_TOKEN;
2993812Shylee pTmpl[i].pValue = &is_token;
3003812Shylee pTmpl[i].ulValueLen = sizeof (CK_BBOOL);
3013812Shylee i++;
3023812Shylee
3033812Shylee pTmpl[i].type = CKA_CLASS;
3043812Shylee pTmpl[i].pValue = &class;
3053812Shylee pTmpl[i].ulValueLen = sizeof (class);
3063812Shylee i++;
3073812Shylee
3083812Shylee pTmpl[i].type = CKA_LABEL;
3093812Shylee pTmpl[i].pValue = keylabel;
3103812Shylee pTmpl[i].ulValueLen = strlen(keylabel);
3113812Shylee i++;
3123812Shylee
3133812Shylee pTmpl[i].type = CKA_KEY_TYPE;
3143812Shylee pTmpl[i].pValue = &ckKeyType;
3153812Shylee pTmpl[i].ulValueLen = sizeof (ckKeyType);
3163812Shylee i++;
3173812Shylee
3183812Shylee pTmpl[i].type = CKA_PRIVATE;
3193812Shylee pTmpl[i].pValue = &true;
3203812Shylee pTmpl[i].ulValueLen = sizeof (true);
3213812Shylee i++;
3223812Shylee
3233812Shylee rv = C_FindObjectsInit(hSession, pTmpl, i);
3243812Shylee if (rv != CKR_OK) {
3253812Shylee goto out;
3263812Shylee }
3273812Shylee
3283812Shylee rv = C_FindObjects(hSession, keyobj, 1, &key_obj_count);
3293812Shylee (void) C_FindObjectsFinal(hSession);
3303812Shylee
3313812Shylee out:
3323812Shylee if (rv != CKR_OK) {
3333812Shylee (void) fprintf(stderr,
3343812Shylee "Cannot retrieve key object. error = %s\n",
3353812Shylee pkcs11_strerror(rv));
3363812Shylee return (-1);
3373812Shylee }
3383812Shylee
3393812Shylee if (key_obj_count == 0) {
3403812Shylee (void) fprintf(stderr, "Cannot find the key object.\n");
3413812Shylee return (-1);
3423812Shylee }
3433812Shylee
3443812Shylee return (0);
3453812Shylee }
3463812Shylee
3473812Shylee
3480Sstevel@tonic-gate /*
3490Sstevel@tonic-gate * Execute the command.
3500Sstevel@tonic-gate * algo_str - name of algorithm
3510Sstevel@tonic-gate * filecount - no. of files to process, if 0, use stdin
3520Sstevel@tonic-gate * filelist - list of files
3530Sstevel@tonic-gate * mac_cmd - if true do mac else do digest
3540Sstevel@tonic-gate */
3550Sstevel@tonic-gate static int
execute_cmd(char * algo_str,int filecount,char ** filelist,boolean_t mac_cmd)3560Sstevel@tonic-gate execute_cmd(char *algo_str, int filecount, char **filelist, boolean_t mac_cmd)
3570Sstevel@tonic-gate {
3580Sstevel@tonic-gate int fd;
3590Sstevel@tonic-gate char *filename = NULL;
3600Sstevel@tonic-gate CK_RV rv;
3610Sstevel@tonic-gate CK_ULONG slotcount;
3620Sstevel@tonic-gate CK_SLOT_ID slotID;
3630Sstevel@tonic-gate CK_SLOT_ID_PTR pSlotList = NULL;
3640Sstevel@tonic-gate CK_MECHANISM_TYPE mech_type;
3650Sstevel@tonic-gate CK_MECHANISM_INFO info;
3660Sstevel@tonic-gate CK_MECHANISM mech;
3670Sstevel@tonic-gate CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE;
3680Sstevel@tonic-gate CK_BYTE_PTR resultbuf = NULL;
3690Sstevel@tonic-gate CK_ULONG resultlen;
3700Sstevel@tonic-gate CK_BYTE_PTR pkeydata = NULL;
3710Sstevel@tonic-gate CK_OBJECT_HANDLE key = (CK_OBJECT_HANDLE) 0;
3726281Sda73024 size_t keylen = 0; /* key length */
3730Sstevel@tonic-gate char *resultstr = NULL; /* result in hex string */
3740Sstevel@tonic-gate int resultstrlen; /* result string length */
3750Sstevel@tonic-gate int i;
3760Sstevel@tonic-gate int exitcode = EXIT_SUCCESS; /* return code */
3770Sstevel@tonic-gate int slot, mek; /* index variables */
3780Sstevel@tonic-gate int mech_match = 0;
3795252Sdinak CK_BYTE salt[CK_PKCS5_PBKD2_SALT_SIZE];
3800Sstevel@tonic-gate CK_ULONG keysize;
3815252Sdinak CK_ULONG iterations = CK_PKCS5_PBKD2_ITERATIONS;
3823812Shylee CK_KEY_TYPE keytype;
3833812Shylee KMF_RETURN kmfrv;
3843812Shylee CK_SLOT_ID token_slot_id;
3850Sstevel@tonic-gate
3860Sstevel@tonic-gate if (aflag) {
3870Sstevel@tonic-gate /*
3880Sstevel@tonic-gate * Determine if algorithm/mechanism is valid
3890Sstevel@tonic-gate */
3900Sstevel@tonic-gate for (mech_match = 0; mech_match < MECH_ALIASES_COUNT;
3915051Swyllys mech_match++) {
3920Sstevel@tonic-gate if (strcmp(algo_str,
3930Sstevel@tonic-gate mech_aliases[mech_match].alias) == 0) {
3940Sstevel@tonic-gate mech_type = mech_aliases[mech_match].type;
3950Sstevel@tonic-gate break;
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate
4000Sstevel@tonic-gate if (mech_match == MECH_ALIASES_COUNT) {
4010Sstevel@tonic-gate cryptoerror(LOG_STDERR,
4020Sstevel@tonic-gate gettext("unknown algorithm -- %s"), algo_str);
4030Sstevel@tonic-gate return (EXIT_FAILURE);
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate /* Get key to do a MAC operation */
4070Sstevel@tonic-gate if (mac_cmd) {
4085252Sdinak int status;
4095252Sdinak
4103812Shylee if (Kflag) {
4115252Sdinak /* get the pin of the token */
4123812Shylee if (token_label == NULL ||
4133812Shylee !strlen(token_label)) {
4145252Sdinak token_label = pkcs11_default_token();
4153812Shylee }
4163812Shylee
4175252Sdinak status = pkcs11_get_pass(token_label,
4186281Sda73024 (char **)&pkeydata, &keylen,
4195252Sdinak 0, B_FALSE);
4205252Sdinak } else if (keyfile != NULL) {
4215252Sdinak /* get the key file */
4225252Sdinak status = pkcs11_read_data(keyfile,
4236281Sda73024 (void **)&pkeydata, &keylen);
4245252Sdinak } else {
4255252Sdinak /* get the key from input */
4265252Sdinak status = pkcs11_get_pass(NULL,
4276281Sda73024 (char **)&pkeydata, &keylen,
4285252Sdinak 0, B_FALSE);
4295252Sdinak }
4303812Shylee
4318309SAnthony.Scarpino@Sun.COM if (status != 0 || keylen == 0 || pkeydata == NULL) {
4325252Sdinak cryptoerror(LOG_STDERR,
433*11458Sopensolaris@drydog.com (Kflag || (keyfile == NULL)) ?
434*11458Sopensolaris@drydog.com gettext("invalid passphrase.") :
4355252Sdinak gettext("invalid key."));
4365252Sdinak return (EXIT_FAILURE);
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate }
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate
4410Sstevel@tonic-gate /* Initialize, and get list of slots */
4423812Shylee rv = C_Initialize(NULL);
4433812Shylee if (rv != CKR_OK && rv != CKR_CRYPTOKI_ALREADY_INITIALIZED) {
4440Sstevel@tonic-gate cryptoerror(LOG_STDERR,
4450Sstevel@tonic-gate gettext("failed to initialize PKCS #11 framework: %s"),
4460Sstevel@tonic-gate pkcs11_strerror(rv));
4470Sstevel@tonic-gate return (EXIT_FAILURE);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate
4500Sstevel@tonic-gate /* Get slot count */
4510Sstevel@tonic-gate rv = C_GetSlotList(0, NULL_PTR, &slotcount);
4520Sstevel@tonic-gate if (rv != CKR_OK || slotcount == 0) {
4530Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext(
454*11458Sopensolaris@drydog.com "failed to find any cryptographic provider; "
4550Sstevel@tonic-gate "please check with your system administrator: %s"),
4560Sstevel@tonic-gate pkcs11_strerror(rv));
4570Sstevel@tonic-gate exitcode = EXIT_FAILURE;
4580Sstevel@tonic-gate goto cleanup;
4590Sstevel@tonic-gate }
4600Sstevel@tonic-gate
4610Sstevel@tonic-gate /* Found at least one slot, allocate memory for slot list */
4620Sstevel@tonic-gate pSlotList = malloc(slotcount * sizeof (CK_SLOT_ID));
4630Sstevel@tonic-gate if (pSlotList == NULL_PTR) {
4640Sstevel@tonic-gate int err = errno;
4650Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s\n"),
4660Sstevel@tonic-gate strerror(err));
4670Sstevel@tonic-gate exitcode = EXIT_FAILURE;
4680Sstevel@tonic-gate goto cleanup;
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate
4710Sstevel@tonic-gate /* Get the list of slots */
4720Sstevel@tonic-gate if ((rv = C_GetSlotList(0, pSlotList, &slotcount)) != CKR_OK) {
4730Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext(
474*11458Sopensolaris@drydog.com "failed to find any cryptographic provider; "
4750Sstevel@tonic-gate "please check with your system administrator: %s"),
4760Sstevel@tonic-gate pkcs11_strerror(rv));
4770Sstevel@tonic-gate exitcode = EXIT_FAILURE;
4780Sstevel@tonic-gate goto cleanup;
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate
4810Sstevel@tonic-gate /*
4820Sstevel@tonic-gate * Obtain list of algorithms if -l option was given
4830Sstevel@tonic-gate */
4840Sstevel@tonic-gate if (lflag) {
4850Sstevel@tonic-gate
4860Sstevel@tonic-gate for (slot = 0; slot < slotcount; slot++) {
4870Sstevel@tonic-gate
4880Sstevel@tonic-gate /* Iterate through each mechanism */
4890Sstevel@tonic-gate for (mek = 0; mek < MECH_ALIASES_COUNT; mek++) {
4900Sstevel@tonic-gate rv = C_GetMechanismInfo(pSlotList[slot],
4910Sstevel@tonic-gate mech_aliases[mek].type, &info);
4920Sstevel@tonic-gate
4930Sstevel@tonic-gate /* Only check algorithms that can be used */
4940Sstevel@tonic-gate if ((rv != CKR_OK) ||
4950Sstevel@tonic-gate (!mac_cmd && (info.flags & CKF_SIGN)) ||
4960Sstevel@tonic-gate (mac_cmd && (info.flags & CKF_DIGEST)))
4970Sstevel@tonic-gate continue;
4980Sstevel@tonic-gate
4990Sstevel@tonic-gate /*
5000Sstevel@tonic-gate * Set to minimum/maximum key sizes assuming
5010Sstevel@tonic-gate * the values available are not 0.
5020Sstevel@tonic-gate */
5030Sstevel@tonic-gate if (info.ulMinKeySize && (info.ulMinKeySize <
5040Sstevel@tonic-gate mech_aliases[mek].keysize_min))
5050Sstevel@tonic-gate mech_aliases[mek].keysize_min =
5065051Swyllys info.ulMinKeySize;
5070Sstevel@tonic-gate
5080Sstevel@tonic-gate if (info.ulMaxKeySize && (info.ulMaxKeySize >
5090Sstevel@tonic-gate mech_aliases[mek].keysize_max))
5100Sstevel@tonic-gate mech_aliases[mek].keysize_max =
5115051Swyllys info.ulMaxKeySize;
5120Sstevel@tonic-gate
5130Sstevel@tonic-gate mech_aliases[mek].available = B_TRUE;
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate
5160Sstevel@tonic-gate }
5170Sstevel@tonic-gate
5180Sstevel@tonic-gate algorithm_list(mac_cmd);
5190Sstevel@tonic-gate
5200Sstevel@tonic-gate goto cleanup;
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate
5233812Shylee /*
5243812Shylee * Find a slot with matching mechanism
5253812Shylee *
5263812Shylee * If -K is specified, we find the slot id for the token first, then
5273812Shylee * check if the slot supports the algorithm.
5283812Shylee */
5293812Shylee i = 0;
5303812Shylee if (Kflag) {
5315051Swyllys kmfrv = kmf_pk11_token_lookup(NULL, token_label,
5325051Swyllys &token_slot_id);
5333812Shylee if (kmfrv != KMF_OK) {
5343812Shylee cryptoerror(LOG_STDERR,
5353812Shylee gettext("no matching PKCS#11 token"));
5363812Shylee exitcode = EXIT_FAILURE;
5373812Shylee goto cleanup;
5383812Shylee }
5393812Shylee rv = C_GetMechanismInfo(token_slot_id, mech_type, &info);
5403812Shylee if (rv == CKR_OK && (info.flags & CKF_SIGN))
5413812Shylee slotID = token_slot_id;
5423812Shylee else
5433812Shylee i = slotcount;
5443812Shylee
5453812Shylee } else {
5463812Shylee for (i = 0; i < slotcount; i++) {
5473812Shylee slotID = pSlotList[i];
5483812Shylee rv = C_GetMechanismInfo(slotID, mech_type, &info);
5493812Shylee if (rv != CKR_OK) {
5503812Shylee continue; /* to the next slot */
5513812Shylee } else {
5523812Shylee if (mac_cmd) {
5533812Shylee /*
5543812Shylee * Make sure the slot supports
5553812Shylee * PKCS5 key generation if we
5563812Shylee * will be using it later.
5573812Shylee * We use it whenever the key
5583812Shylee * is entered at command line.
5593812Shylee */
5603812Shylee if ((info.flags & CKF_SIGN) &&
5613812Shylee (keyfile == NULL)) {
5623812Shylee CK_MECHANISM_INFO kg_info;
5633812Shylee rv = C_GetMechanismInfo(slotID,
5643812Shylee CKM_PKCS5_PBKD2, &kg_info);
5653812Shylee if (rv == CKR_OK)
5663812Shylee break;
5673812Shylee } else if (info.flags & CKF_SIGN) {
5683812Shylee break;
5693812Shylee }
5703812Shylee } else {
5713812Shylee if (info.flags & CKF_DIGEST)
5723812Shylee break;
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate }
5770Sstevel@tonic-gate
5780Sstevel@tonic-gate /* Show error if no matching mechanism found */
5790Sstevel@tonic-gate if (i == slotcount) {
5800Sstevel@tonic-gate cryptoerror(LOG_STDERR,
5810Sstevel@tonic-gate gettext("no cryptographic provider was "
5820Sstevel@tonic-gate "found for this algorithm -- %s"), algo_str);
5830Sstevel@tonic-gate exitcode = EXIT_FAILURE;
5840Sstevel@tonic-gate goto cleanup;
5850Sstevel@tonic-gate }
5860Sstevel@tonic-gate
5870Sstevel@tonic-gate /* Mechanism is supported. Go ahead & open a session */
5880Sstevel@tonic-gate rv = C_OpenSession(slotID, CKF_SERIAL_SESSION,
5895051Swyllys NULL_PTR, NULL, &hSession);
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate if (rv != CKR_OK) {
5920Sstevel@tonic-gate cryptoerror(LOG_STDERR,
5930Sstevel@tonic-gate gettext("can not open PKCS#11 session: %s"),
5940Sstevel@tonic-gate pkcs11_strerror(rv));
5950Sstevel@tonic-gate exitcode = EXIT_FAILURE;
5960Sstevel@tonic-gate goto cleanup;
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate
5990Sstevel@tonic-gate /* Create a key object for mac operation */
6000Sstevel@tonic-gate if (mac_cmd) {
6010Sstevel@tonic-gate /*
6020Sstevel@tonic-gate * If we read keybytes from a file,
6030Sstevel@tonic-gate * do NOT process them with C_GenerateKey,
6040Sstevel@tonic-gate * treat them as raw keydata bytes and
6050Sstevel@tonic-gate * create a key object for them.
6060Sstevel@tonic-gate */
6070Sstevel@tonic-gate if (keyfile) {
6085252Sdinak /* XXX : why wasn't SUNW_C_KeyToObject used here? */
6090Sstevel@tonic-gate CK_OBJECT_CLASS class = CKO_SECRET_KEY;
6100Sstevel@tonic-gate CK_KEY_TYPE tmpl_keytype = CKK_GENERIC_SECRET;
6110Sstevel@tonic-gate CK_BBOOL false = FALSE;
6120Sstevel@tonic-gate int nattr = 0;
6130Sstevel@tonic-gate CK_ATTRIBUTE template[5];
6140Sstevel@tonic-gate
6150Sstevel@tonic-gate if (mech_type == CKM_DES_MAC) {
6160Sstevel@tonic-gate tmpl_keytype = CKK_DES;
6170Sstevel@tonic-gate }
6180Sstevel@tonic-gate template[nattr].type = CKA_CLASS;
6190Sstevel@tonic-gate template[nattr].pValue = &class;
6200Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (class);
6210Sstevel@tonic-gate nattr++;
6220Sstevel@tonic-gate
6230Sstevel@tonic-gate template[nattr].type = CKA_KEY_TYPE;
6240Sstevel@tonic-gate template[nattr].pValue = &tmpl_keytype;
6250Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (tmpl_keytype);
6260Sstevel@tonic-gate nattr++;
6270Sstevel@tonic-gate
6280Sstevel@tonic-gate template[nattr].type = CKA_SIGN;
6290Sstevel@tonic-gate template[nattr].pValue = &true;
6300Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (true);
6310Sstevel@tonic-gate nattr++;
6320Sstevel@tonic-gate
6330Sstevel@tonic-gate template[nattr].type = CKA_TOKEN;
6340Sstevel@tonic-gate template[nattr].pValue = &false;
6350Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (false);
6360Sstevel@tonic-gate nattr++;
6370Sstevel@tonic-gate
6380Sstevel@tonic-gate template[nattr].type = CKA_VALUE;
6390Sstevel@tonic-gate template[nattr].pValue = pkeydata;
6400Sstevel@tonic-gate template[nattr].ulValueLen = keylen;
6410Sstevel@tonic-gate nattr++;
6420Sstevel@tonic-gate
6435051Swyllys rv = C_CreateObject(hSession, template, nattr, &key);
6443812Shylee
6453812Shylee } else if (Kflag) {
6463812Shylee
6473812Shylee if (mech_type == CKM_DES_MAC) {
6483812Shylee keytype = CKK_DES;
6493812Shylee } else {
6503812Shylee keytype = CKK_GENERIC_SECRET;
6513812Shylee }
6523812Shylee
6533812Shylee rv = get_token_key(hSession, keytype, key_label,
6543812Shylee pkeydata, keylen, &key);
6553812Shylee if (rv != CKR_OK) {
6563812Shylee exitcode = EXIT_FAILURE;
6573812Shylee goto cleanup;
6583812Shylee }
6590Sstevel@tonic-gate } else {
6600Sstevel@tonic-gate CK_KEY_TYPE keytype;
6610Sstevel@tonic-gate if (mech_type == CKM_DES_MAC) {
6620Sstevel@tonic-gate keytype = CKK_DES;
6630Sstevel@tonic-gate keysize = 0;
6640Sstevel@tonic-gate } else {
6650Sstevel@tonic-gate keytype = CKK_GENERIC_SECRET;
6660Sstevel@tonic-gate keysize = 16; /* 128 bits */
6670Sstevel@tonic-gate }
6680Sstevel@tonic-gate /*
6690Sstevel@tonic-gate * We use a fixed salt (0x0a, 0x0a, 0x0a ...)
6700Sstevel@tonic-gate * for creating the key so that the end user
6710Sstevel@tonic-gate * will be able to generate the same 'mac'
6720Sstevel@tonic-gate * using the same passphrase.
6730Sstevel@tonic-gate */
6740Sstevel@tonic-gate (void) memset(salt, 0x0a, sizeof (salt));
6755252Sdinak rv = pkcs11_PasswdToPBKD2Object(hSession,
6765252Sdinak (char *)pkeydata, (size_t)keylen, (void *)salt,
6775252Sdinak sizeof (salt), iterations, keytype, keysize,
6785252Sdinak CKF_SIGN, &key);
6790Sstevel@tonic-gate }
6800Sstevel@tonic-gate
6810Sstevel@tonic-gate if (rv != CKR_OK) {
6820Sstevel@tonic-gate cryptoerror(LOG_STDERR,
6830Sstevel@tonic-gate gettext("unable to create key for crypto "
6840Sstevel@tonic-gate "operation: %s"), pkcs11_strerror(rv));
6850Sstevel@tonic-gate exitcode = EXIT_FAILURE;
6860Sstevel@tonic-gate goto cleanup;
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate }
6890Sstevel@tonic-gate
6900Sstevel@tonic-gate /* Allocate a buffer to store result. */
6910Sstevel@tonic-gate resultlen = RESULTLEN;
6920Sstevel@tonic-gate if ((resultbuf = malloc(resultlen)) == NULL) {
6930Sstevel@tonic-gate int err = errno;
6940Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s\n"),
6950Sstevel@tonic-gate strerror(err));
6960Sstevel@tonic-gate exitcode = EXIT_FAILURE;
6970Sstevel@tonic-gate goto cleanup;
6980Sstevel@tonic-gate }
6990Sstevel@tonic-gate
7000Sstevel@tonic-gate /* Allocate a buffer to store result string */
7010Sstevel@tonic-gate resultstrlen = RESULTLEN;
7020Sstevel@tonic-gate if ((resultstr = malloc(resultstrlen)) == NULL) {
7030Sstevel@tonic-gate int err = errno;
7040Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s\n"),
7050Sstevel@tonic-gate strerror(err));
7060Sstevel@tonic-gate exitcode = EXIT_FAILURE;
7070Sstevel@tonic-gate goto cleanup;
7080Sstevel@tonic-gate }
7090Sstevel@tonic-gate
7100Sstevel@tonic-gate mech.mechanism = mech_type;
7110Sstevel@tonic-gate mech.pParameter = NULL_PTR;
7120Sstevel@tonic-gate mech.ulParameterLen = 0;
7130Sstevel@tonic-gate exitcode = EXIT_SUCCESS;
7140Sstevel@tonic-gate i = 0;
7150Sstevel@tonic-gate
7160Sstevel@tonic-gate do {
7170Sstevel@tonic-gate if (filecount > 0 && filelist != NULL) {
7180Sstevel@tonic-gate filename = filelist[i];
7195051Swyllys if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) ==
7205051Swyllys -1) {
7210Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext(
7220Sstevel@tonic-gate "can not open input file %s\n"), filename);
7230Sstevel@tonic-gate exitcode = EXIT_USAGE;
7240Sstevel@tonic-gate continue;
7250Sstevel@tonic-gate }
7260Sstevel@tonic-gate } else {
7270Sstevel@tonic-gate fd = 0; /* use stdin */
7280Sstevel@tonic-gate }
7290Sstevel@tonic-gate
7300Sstevel@tonic-gate /*
7310Sstevel@tonic-gate * Perform the operation
7320Sstevel@tonic-gate */
7330Sstevel@tonic-gate if (mac_cmd) {
7340Sstevel@tonic-gate rv = do_mac(hSession, &mech, fd, key, &resultbuf,
7355051Swyllys &resultlen);
7360Sstevel@tonic-gate } else {
7370Sstevel@tonic-gate rv = do_digest(hSession, &mech, fd, &resultbuf,
7385051Swyllys &resultlen);
7390Sstevel@tonic-gate }
7400Sstevel@tonic-gate
7410Sstevel@tonic-gate if (rv != CKR_OK) {
7420Sstevel@tonic-gate cryptoerror(LOG_STDERR,
7430Sstevel@tonic-gate gettext("crypto operation failed for "
7445051Swyllys "file %s: %s\n"),
7450Sstevel@tonic-gate filename ? filename : "STDIN",
7460Sstevel@tonic-gate pkcs11_strerror(rv));
7470Sstevel@tonic-gate exitcode = EXIT_FAILURE;
7480Sstevel@tonic-gate continue;
7490Sstevel@tonic-gate }
7500Sstevel@tonic-gate
7510Sstevel@tonic-gate /* if result size has changed, allocate a bigger resulstr buf */
7520Sstevel@tonic-gate if (resultlen != RESULTLEN) {
7530Sstevel@tonic-gate resultstrlen = 2 * resultlen + 1;
7540Sstevel@tonic-gate resultstr = realloc(resultstr, resultstrlen);
7550Sstevel@tonic-gate
7560Sstevel@tonic-gate if (resultstr == NULL) {
7570Sstevel@tonic-gate int err = errno;
7580Sstevel@tonic-gate cryptoerror(LOG_STDERR,
7590Sstevel@tonic-gate gettext("realloc: %s\n"), strerror(err));
7600Sstevel@tonic-gate exitcode = EXIT_FAILURE;
7610Sstevel@tonic-gate goto cleanup;
7620Sstevel@tonic-gate }
7630Sstevel@tonic-gate }
7640Sstevel@tonic-gate
7650Sstevel@tonic-gate /* Output the result */
7660Sstevel@tonic-gate tohexstr(resultbuf, resultlen, resultstr, resultstrlen);
7670Sstevel@tonic-gate
7680Sstevel@tonic-gate /* Include mechanism name for verbose */
7690Sstevel@tonic-gate if (vflag)
7700Sstevel@tonic-gate (void) fprintf(stdout, "%s ", algo_str);
7710Sstevel@tonic-gate
7720Sstevel@tonic-gate /* Include file name for multiple files, or if verbose */
7730Sstevel@tonic-gate if (filecount > 1 || (vflag && filecount > 0)) {
7740Sstevel@tonic-gate (void) fprintf(stdout, "(%s) = ", filename);
7750Sstevel@tonic-gate }
7760Sstevel@tonic-gate
7770Sstevel@tonic-gate (void) fprintf(stdout, "%s\n", resultstr);
7780Sstevel@tonic-gate (void) close(fd);
7790Sstevel@tonic-gate
7800Sstevel@tonic-gate
7810Sstevel@tonic-gate } while (++i < filecount);
7820Sstevel@tonic-gate
7830Sstevel@tonic-gate
7840Sstevel@tonic-gate /* clear and free the key */
7850Sstevel@tonic-gate if (mac_cmd) {
7860Sstevel@tonic-gate (void) memset(pkeydata, 0, keylen);
7870Sstevel@tonic-gate free(pkeydata);
7880Sstevel@tonic-gate pkeydata = NULL;
7890Sstevel@tonic-gate }
7900Sstevel@tonic-gate
7910Sstevel@tonic-gate cleanup:
7920Sstevel@tonic-gate if (resultbuf != NULL) {
7930Sstevel@tonic-gate free(resultbuf);
7940Sstevel@tonic-gate }
7950Sstevel@tonic-gate
7960Sstevel@tonic-gate if (resultstr != NULL) {
7970Sstevel@tonic-gate free(resultstr);
7980Sstevel@tonic-gate }
7990Sstevel@tonic-gate
8000Sstevel@tonic-gate if (pSlotList != NULL) {
8010Sstevel@tonic-gate free(pSlotList);
8020Sstevel@tonic-gate }
8030Sstevel@tonic-gate
8043812Shylee if (!Kflag && key != (CK_OBJECT_HANDLE) 0) {
8050Sstevel@tonic-gate (void) C_DestroyObject(hSession, key);
8060Sstevel@tonic-gate }
8070Sstevel@tonic-gate
8080Sstevel@tonic-gate if (hSession != CK_INVALID_HANDLE)
8090Sstevel@tonic-gate (void) C_CloseSession(hSession);
8100Sstevel@tonic-gate
8110Sstevel@tonic-gate (void) C_Finalize(NULL_PTR);
8120Sstevel@tonic-gate
8130Sstevel@tonic-gate return (exitcode);
8140Sstevel@tonic-gate }
8150Sstevel@tonic-gate
8160Sstevel@tonic-gate /*
8170Sstevel@tonic-gate * do_digest - Compute digest of a file
8180Sstevel@tonic-gate *
8190Sstevel@tonic-gate * hSession - session
8200Sstevel@tonic-gate * pmech - ptr to mechanism to be used for digest
8210Sstevel@tonic-gate * fd - file descriptor
8220Sstevel@tonic-gate * pdigest - buffer where digest result is returned
8230Sstevel@tonic-gate * pdigestlen - length of digest buffer on input,
8240Sstevel@tonic-gate * length of result on output
8250Sstevel@tonic-gate */
8260Sstevel@tonic-gate static CK_RV
do_digest(CK_SESSION_HANDLE hSession,CK_MECHANISM_PTR pmech,int fd,CK_BYTE_PTR * pdigest,CK_ULONG_PTR pdigestlen)8270Sstevel@tonic-gate do_digest(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pmech,
8280Sstevel@tonic-gate int fd, CK_BYTE_PTR *pdigest, CK_ULONG_PTR pdigestlen)
8290Sstevel@tonic-gate {
8300Sstevel@tonic-gate CK_RV rv;
8310Sstevel@tonic-gate ssize_t nread;
8320Sstevel@tonic-gate int saved_errno;
8330Sstevel@tonic-gate
8340Sstevel@tonic-gate if ((rv = C_DigestInit(hSession, pmech)) != CKR_OK) {
8350Sstevel@tonic-gate return (rv);
8360Sstevel@tonic-gate }
8370Sstevel@tonic-gate
8380Sstevel@tonic-gate while ((nread = read(fd, buf, sizeof (buf))) > 0) {
8390Sstevel@tonic-gate /* Get the digest */
8400Sstevel@tonic-gate rv = C_DigestUpdate(hSession, buf, (CK_ULONG)nread);
8410Sstevel@tonic-gate if (rv != CKR_OK)
8420Sstevel@tonic-gate return (rv);
8430Sstevel@tonic-gate }
8440Sstevel@tonic-gate
8450Sstevel@tonic-gate saved_errno = errno; /* for later use */
8460Sstevel@tonic-gate
8470Sstevel@tonic-gate /*
8480Sstevel@tonic-gate * Perform the C_DigestFinal, even if there is a read error.
8490Sstevel@tonic-gate * Otherwise C_DigestInit will return CKR_OPERATION_ACTIVE
8500Sstevel@tonic-gate * next time it is called (for another file)
8510Sstevel@tonic-gate */
8520Sstevel@tonic-gate
8530Sstevel@tonic-gate rv = C_DigestFinal(hSession, *pdigest, pdigestlen);
8540Sstevel@tonic-gate
8550Sstevel@tonic-gate /* result too big to fit? Allocate a bigger buffer */
8560Sstevel@tonic-gate if (rv == CKR_BUFFER_TOO_SMALL) {
8570Sstevel@tonic-gate *pdigest = realloc(*pdigest, *pdigestlen);
8580Sstevel@tonic-gate
8590Sstevel@tonic-gate if (*pdigest == NULL_PTR) {
8600Sstevel@tonic-gate int err = errno;
8610Sstevel@tonic-gate cryptoerror(LOG_STDERR,
8620Sstevel@tonic-gate gettext("realloc: %s\n"), strerror(err));
8630Sstevel@tonic-gate return (CKR_HOST_MEMORY);
8640Sstevel@tonic-gate }
8650Sstevel@tonic-gate
8660Sstevel@tonic-gate rv = C_DigestFinal(hSession, *pdigest, pdigestlen);
8670Sstevel@tonic-gate }
8680Sstevel@tonic-gate
8690Sstevel@tonic-gate
8700Sstevel@tonic-gate /* There was a read error */
8710Sstevel@tonic-gate if (nread == -1) {
8720Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext(
8735051Swyllys "error reading file: %s"), strerror(saved_errno));
8740Sstevel@tonic-gate return (CKR_GENERAL_ERROR);
8750Sstevel@tonic-gate } else {
8760Sstevel@tonic-gate return (rv);
8770Sstevel@tonic-gate }
8780Sstevel@tonic-gate }
8790Sstevel@tonic-gate
8800Sstevel@tonic-gate /*
8810Sstevel@tonic-gate * do_mac - Compute mac of a file
8820Sstevel@tonic-gate *
8830Sstevel@tonic-gate * hSession - session
8840Sstevel@tonic-gate * pmech - ptr to mechanism to be used
8850Sstevel@tonic-gate * fd - file descriptor
8860Sstevel@tonic-gate * key - key to be used
8870Sstevel@tonic-gate * psignature - ptr buffer where mac result is returned
8880Sstevel@tonic-gate * returns new buf if current buf is small
8890Sstevel@tonic-gate * psignaturelen - length of mac buffer on input,
8900Sstevel@tonic-gate * length of result on output
8910Sstevel@tonic-gate */
8920Sstevel@tonic-gate static CK_RV
do_mac(CK_SESSION_HANDLE hSession,CK_MECHANISM_PTR pmech,int fd,CK_OBJECT_HANDLE key,CK_BYTE_PTR * psignature,CK_ULONG_PTR psignaturelen)8930Sstevel@tonic-gate do_mac(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pmech,
8940Sstevel@tonic-gate int fd, CK_OBJECT_HANDLE key, CK_BYTE_PTR *psignature,
8950Sstevel@tonic-gate CK_ULONG_PTR psignaturelen)
8960Sstevel@tonic-gate {
8970Sstevel@tonic-gate CK_RV rv;
8980Sstevel@tonic-gate ssize_t nread;
8990Sstevel@tonic-gate int saved_errno;
9000Sstevel@tonic-gate
9010Sstevel@tonic-gate if ((rv = C_SignInit(hSession, pmech, key)) != CKR_OK) {
9020Sstevel@tonic-gate return (rv);
9030Sstevel@tonic-gate }
9040Sstevel@tonic-gate
9050Sstevel@tonic-gate while ((nread = read(fd, buf, sizeof (buf))) > 0) {
9060Sstevel@tonic-gate /* Get the MAC */
9070Sstevel@tonic-gate rv = C_SignUpdate(hSession, buf, (CK_ULONG)nread);
9080Sstevel@tonic-gate if (rv != CKR_OK)
9090Sstevel@tonic-gate return (rv);
9100Sstevel@tonic-gate }
9110Sstevel@tonic-gate
9120Sstevel@tonic-gate saved_errno = errno; /* for later use */
9130Sstevel@tonic-gate
9140Sstevel@tonic-gate /*
9150Sstevel@tonic-gate * Perform the C_SignFinal, even if there is a read error.
9160Sstevel@tonic-gate * Otherwise C_SignInit will return CKR_OPERATION_ACTIVE
9170Sstevel@tonic-gate * next time it is called (for another file)
9180Sstevel@tonic-gate */
9190Sstevel@tonic-gate
9200Sstevel@tonic-gate rv = C_SignFinal(hSession, *psignature, psignaturelen);
9210Sstevel@tonic-gate
9220Sstevel@tonic-gate /* result too big to fit? Allocate a bigger buffer */
9230Sstevel@tonic-gate if (rv == CKR_BUFFER_TOO_SMALL) {
9240Sstevel@tonic-gate *psignature = realloc(*psignature, *psignaturelen);
9250Sstevel@tonic-gate
9260Sstevel@tonic-gate if (*psignature == NULL_PTR) {
9270Sstevel@tonic-gate int err = errno;
9280Sstevel@tonic-gate cryptoerror(LOG_STDERR,
9290Sstevel@tonic-gate gettext("realloc: %s\n"), strerror(err));
9300Sstevel@tonic-gate return (CKR_HOST_MEMORY);
9310Sstevel@tonic-gate }
9320Sstevel@tonic-gate
9330Sstevel@tonic-gate rv = C_SignFinal(hSession, *psignature, psignaturelen);
9340Sstevel@tonic-gate }
9350Sstevel@tonic-gate
9360Sstevel@tonic-gate /* There was a read error */
9370Sstevel@tonic-gate if (nread == -1) {
9380Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("error reading file: %s"),
9395051Swyllys strerror(saved_errno));
9400Sstevel@tonic-gate return (CKR_GENERAL_ERROR);
9410Sstevel@tonic-gate } else {
9420Sstevel@tonic-gate return (rv);
9430Sstevel@tonic-gate }
9440Sstevel@tonic-gate }
945