10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*676Sizick * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * digest.c 310Sstevel@tonic-gate * 320Sstevel@tonic-gate * Implements digest(1) and mac(1) commands 330Sstevel@tonic-gate * If command name is mac, performs mac operation 340Sstevel@tonic-gate * else perform digest operation 350Sstevel@tonic-gate * 360Sstevel@tonic-gate * See the man pages for digest and mac for details on 370Sstevel@tonic-gate * how these commands work. 380Sstevel@tonic-gate */ 390Sstevel@tonic-gate 400Sstevel@tonic-gate #include <stdio.h> 410Sstevel@tonic-gate #include <stdlib.h> 420Sstevel@tonic-gate #include <unistd.h> 430Sstevel@tonic-gate #include <fcntl.h> 440Sstevel@tonic-gate #include <ctype.h> 450Sstevel@tonic-gate #include <strings.h> 460Sstevel@tonic-gate #include <libintl.h> 470Sstevel@tonic-gate #include <libgen.h> 480Sstevel@tonic-gate #include <locale.h> 490Sstevel@tonic-gate #include <errno.h> 500Sstevel@tonic-gate #include <sys/types.h> 510Sstevel@tonic-gate #include <sys/stat.h> 520Sstevel@tonic-gate #include <security/cryptoki.h> 530Sstevel@tonic-gate #include <limits.h> 540Sstevel@tonic-gate #include <cryptoutil.h> 550Sstevel@tonic-gate 560Sstevel@tonic-gate #define BUFFERSIZE (4096) /* Buffer size for reading file */ 570Sstevel@tonic-gate 580Sstevel@tonic-gate /* 590Sstevel@tonic-gate * RESULTLEN - large enough size in bytes to hold result for 600Sstevel@tonic-gate * digest and mac results for all mechanisms 610Sstevel@tonic-gate */ 620Sstevel@tonic-gate #define RESULTLEN (512) 630Sstevel@tonic-gate 640Sstevel@tonic-gate /* 650Sstevel@tonic-gate * Default parameters for PBKDF2 algorithm 660Sstevel@tonic-gate */ 670Sstevel@tonic-gate #define PBKD2_ITERATIONS (1000) 680Sstevel@tonic-gate #define PBKD2_SALT_SIZE 16 690Sstevel@tonic-gate 700Sstevel@tonic-gate /* 710Sstevel@tonic-gate * Exit Status codes 720Sstevel@tonic-gate */ 730Sstevel@tonic-gate #ifndef EXIT_SUCCESS 740Sstevel@tonic-gate #define EXIT_SUCCESS 0 /* No errors */ 750Sstevel@tonic-gate #define EXIT_FAILURE 1 /* All errors except usage */ 760Sstevel@tonic-gate #endif /* EXIT_SUCCESS */ 770Sstevel@tonic-gate 780Sstevel@tonic-gate #define EXIT_USAGE 2 /* usage/syntax error */ 790Sstevel@tonic-gate 800Sstevel@tonic-gate #define MAC_NAME "mac" /* name of mac command */ 810Sstevel@tonic-gate #define MAC_OPTIONS "lva:k:" /* for getopt */ 820Sstevel@tonic-gate #define DIGEST_NAME "digest" /* name of mac command */ 830Sstevel@tonic-gate #define DIGEST_OPTIONS "lva:" /* for getopt */ 840Sstevel@tonic-gate 850Sstevel@tonic-gate static boolean_t vflag = B_FALSE; /* -v (verbose) flag, optional */ 860Sstevel@tonic-gate static boolean_t aflag = B_FALSE; /* -a <algorithm> flag, required */ 870Sstevel@tonic-gate static boolean_t lflag = B_FALSE; /* -l flag, for mac and digest */ 880Sstevel@tonic-gate 890Sstevel@tonic-gate static char *keyfile = NULL; /* name of keyfile */ 900Sstevel@tonic-gate static CK_BYTE buf[BUFFERSIZE]; 910Sstevel@tonic-gate 920Sstevel@tonic-gate struct mech_alias { 930Sstevel@tonic-gate CK_MECHANISM_TYPE type; 940Sstevel@tonic-gate char *alias; 950Sstevel@tonic-gate CK_ULONG keysize_min; 960Sstevel@tonic-gate CK_ULONG keysize_max; 970Sstevel@tonic-gate int keysize_unit; 980Sstevel@tonic-gate boolean_t available; 990Sstevel@tonic-gate }; 1000Sstevel@tonic-gate 101*676Sizick #define MECH_ALIASES_COUNT 11 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate static struct mech_alias mech_aliases[] = { 1040Sstevel@tonic-gate { CKM_SHA_1, "sha1", ULONG_MAX, 0L, 8, B_FALSE }, 1050Sstevel@tonic-gate { CKM_MD5, "md5", ULONG_MAX, 0L, 8, B_FALSE }, 1060Sstevel@tonic-gate { CKM_DES_MAC, "des_mac", ULONG_MAX, 0L, 8, B_FALSE }, 1070Sstevel@tonic-gate { CKM_SHA_1_HMAC, "sha1_hmac", ULONG_MAX, 0L, 8, B_FALSE }, 1080Sstevel@tonic-gate { CKM_MD5_HMAC, "md5_hmac", ULONG_MAX, 0L, 8, B_FALSE }, 109*676Sizick { CKM_SHA256, "sha256", ULONG_MAX, 0L, 8, B_FALSE }, 110*676Sizick { CKM_SHA384, "sha384", ULONG_MAX, 0L, 8, B_FALSE }, 111*676Sizick { CKM_SHA512, "sha512", ULONG_MAX, 0L, 8, B_FALSE }, 112*676Sizick { CKM_SHA256_HMAC, "sha256_hmac", ULONG_MAX, 0L, 8, B_FALSE }, 113*676Sizick { CKM_SHA384_HMAC, "sha384_hmac", ULONG_MAX, 0L, 8, B_FALSE }, 114*676Sizick { CKM_SHA512_HMAC, "sha512_hmac", ULONG_MAX, 0L, 8, B_FALSE } 1150Sstevel@tonic-gate }; 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate static CK_BBOOL true = TRUE; 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate static void usage(boolean_t mac_cmd); 1200Sstevel@tonic-gate static int execute_cmd(char *algo_str, int filecount, 1210Sstevel@tonic-gate char **filelist, boolean_t mac_cmd); 1220Sstevel@tonic-gate static CK_RV do_mac(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pmech, 1230Sstevel@tonic-gate int fd, CK_OBJECT_HANDLE key, CK_BYTE_PTR *psignature, 1240Sstevel@tonic-gate CK_ULONG_PTR psignaturelen); 1250Sstevel@tonic-gate static CK_RV do_digest(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pmech, 1260Sstevel@tonic-gate int fd, CK_BYTE_PTR *pdigest, CK_ULONG_PTR pdigestlen); 1270Sstevel@tonic-gate static int getkey(char *filename, CK_BYTE_PTR *pkeydata); 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate int 1300Sstevel@tonic-gate main(int argc, char **argv) 1310Sstevel@tonic-gate { 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate extern char *optarg; 1340Sstevel@tonic-gate extern int optind; 1350Sstevel@tonic-gate int errflag = 0; /* We had an optstr parse error */ 1360Sstevel@tonic-gate char c; /* current getopts flag */ 1370Sstevel@tonic-gate char *algo_str; /* mechanism/algorithm string */ 1380Sstevel@tonic-gate int filecount; 1390Sstevel@tonic-gate boolean_t mac_cmd; /* if TRUE, do mac, else do digest */ 1400Sstevel@tonic-gate char *optstr; 1410Sstevel@tonic-gate char **filelist; /* list of files */ 1420Sstevel@tonic-gate char *cmdname = NULL; /* name of command */ 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1450Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defiend by cc -D */ 1460Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 1470Sstevel@tonic-gate #endif 1480Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate /* 1510Sstevel@tonic-gate * Based on command name, determine 1520Sstevel@tonic-gate * type of command. mac is mac 1530Sstevel@tonic-gate * everything else is digest. 1540Sstevel@tonic-gate */ 1550Sstevel@tonic-gate cmdname = basename(argv[0]); 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate cryptodebug_init(cmdname); 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate if (strcmp(cmdname, MAC_NAME) == 0) 1600Sstevel@tonic-gate mac_cmd = B_TRUE; 1610Sstevel@tonic-gate else if (strcmp(cmdname, DIGEST_NAME) == 0) 1620Sstevel@tonic-gate mac_cmd = B_FALSE; 1630Sstevel@tonic-gate else { 1640Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 1650Sstevel@tonic-gate "command name must be either digest or mac\n")); 1660Sstevel@tonic-gate exit(EXIT_USAGE); 1670Sstevel@tonic-gate } 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate if (mac_cmd) { 1700Sstevel@tonic-gate optstr = MAC_OPTIONS; 1710Sstevel@tonic-gate } else { 1720Sstevel@tonic-gate optstr = DIGEST_OPTIONS; 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate /* Parse command line arguments */ 1760Sstevel@tonic-gate while (!errflag && (c = getopt(argc, argv, optstr)) != -1) { 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate switch (c) { 1790Sstevel@tonic-gate case 'v': 1800Sstevel@tonic-gate vflag = B_TRUE; 1810Sstevel@tonic-gate break; 1820Sstevel@tonic-gate case 'a': 1830Sstevel@tonic-gate aflag = B_TRUE; 1840Sstevel@tonic-gate algo_str = optarg; 1850Sstevel@tonic-gate break; 1860Sstevel@tonic-gate case 'k': 1870Sstevel@tonic-gate keyfile = optarg; 1880Sstevel@tonic-gate break; 1890Sstevel@tonic-gate case 'l': 1900Sstevel@tonic-gate lflag = B_TRUE; 1910Sstevel@tonic-gate break; 1920Sstevel@tonic-gate default: 1930Sstevel@tonic-gate errflag++; 1940Sstevel@tonic-gate } 1950Sstevel@tonic-gate } 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate filecount = argc - optind; 1980Sstevel@tonic-gate if (errflag || (!aflag && !lflag) || (lflag && argc > 2) || 1990Sstevel@tonic-gate filecount < 0) { 2000Sstevel@tonic-gate usage(mac_cmd); 2010Sstevel@tonic-gate exit(EXIT_USAGE); 2020Sstevel@tonic-gate } 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate if (filecount == 0) { 2050Sstevel@tonic-gate filelist = NULL; 2060Sstevel@tonic-gate } else { 2070Sstevel@tonic-gate filelist = &argv[optind]; 2080Sstevel@tonic-gate } 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate return (execute_cmd(algo_str, filecount, filelist, mac_cmd)); 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate /* 2140Sstevel@tonic-gate * usage message for digest/mac 2150Sstevel@tonic-gate */ 2160Sstevel@tonic-gate static void 2170Sstevel@tonic-gate usage(boolean_t mac_cmd) 2180Sstevel@tonic-gate { 2190Sstevel@tonic-gate if (mac_cmd) { 2200Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 2210Sstevel@tonic-gate "usage: mac -l | [-v] -a <algorithm> [-k <keyfile>] " 2220Sstevel@tonic-gate "[file...]")); 2230Sstevel@tonic-gate } else { 2240Sstevel@tonic-gate cryptoerror(LOG_STDERR, 2250Sstevel@tonic-gate gettext("usage: digest -l | [-v] -a <algorithm> " 2260Sstevel@tonic-gate "[file...]")); 2270Sstevel@tonic-gate } 2280Sstevel@tonic-gate } 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate /* 2310Sstevel@tonic-gate * Print out list of available algorithms. 2320Sstevel@tonic-gate */ 2330Sstevel@tonic-gate static void 2340Sstevel@tonic-gate algorithm_list(boolean_t mac_cmd) 2350Sstevel@tonic-gate { 2360Sstevel@tonic-gate int mech; 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate if (mac_cmd) 2390Sstevel@tonic-gate (void) printf(gettext("Algorithm Keysize: Min " 2400Sstevel@tonic-gate "Max (bits)\n" 2410Sstevel@tonic-gate "------------------------------------------\n")); 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate for (mech = 0; mech < MECH_ALIASES_COUNT; mech++) { 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate if (mech_aliases[mech].available == B_FALSE) 2460Sstevel@tonic-gate continue; 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate if (mac_cmd) { 2490Sstevel@tonic-gate (void) printf("%-15s", mech_aliases[mech].alias); 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate if (mech_aliases[mech].keysize_min != ULONG_MAX && 2520Sstevel@tonic-gate mech_aliases[mech].keysize_max != 0) 2530Sstevel@tonic-gate (void) printf(" %5lu %5lu\n", 2540Sstevel@tonic-gate (mech_aliases[mech].keysize_min * 2550Sstevel@tonic-gate mech_aliases[mech].keysize_unit), 2560Sstevel@tonic-gate (mech_aliases[mech].keysize_max * 2570Sstevel@tonic-gate mech_aliases[mech].keysize_unit)); 2580Sstevel@tonic-gate else 2590Sstevel@tonic-gate (void) printf("\n"); 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate } else 2620Sstevel@tonic-gate (void) printf("%s\n", mech_aliases[mech].alias); 2630Sstevel@tonic-gate 2640Sstevel@tonic-gate } 2650Sstevel@tonic-gate } 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate static CK_RV 2680Sstevel@tonic-gate generate_pkcs5_key(CK_SESSION_HANDLE hSession, 2690Sstevel@tonic-gate CK_BYTE_PTR pSaltData, 2700Sstevel@tonic-gate CK_ULONG saltLen, 2710Sstevel@tonic-gate CK_ULONG iterations, 2720Sstevel@tonic-gate CK_BYTE_PTR pkeydata, /* user entered passphrase */ 2730Sstevel@tonic-gate CK_KEY_TYPE keytype, 2740Sstevel@tonic-gate CK_ULONG passwd_size, 2750Sstevel@tonic-gate CK_ULONG keylen, /* desired length of generated key */ 2760Sstevel@tonic-gate CK_OBJECT_HANDLE *hKey) 2770Sstevel@tonic-gate { 2780Sstevel@tonic-gate CK_RV rv; 2790Sstevel@tonic-gate CK_PKCS5_PBKD2_PARAMS params; 2800Sstevel@tonic-gate CK_MECHANISM mechanism; 2810Sstevel@tonic-gate CK_OBJECT_CLASS class = CKO_SECRET_KEY; 2820Sstevel@tonic-gate CK_ATTRIBUTE tmpl[4]; 2830Sstevel@tonic-gate int attrs = 0; 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate tmpl[attrs].type = CKA_CLASS; 2860Sstevel@tonic-gate tmpl[attrs].pValue = &class; 2870Sstevel@tonic-gate tmpl[attrs].ulValueLen = sizeof (class); 2880Sstevel@tonic-gate attrs++; 2890Sstevel@tonic-gate 2900Sstevel@tonic-gate tmpl[attrs].type = CKA_KEY_TYPE; 2910Sstevel@tonic-gate tmpl[attrs].pValue = &keytype; 2920Sstevel@tonic-gate tmpl[attrs].ulValueLen = sizeof (keytype); 2930Sstevel@tonic-gate attrs++; 2940Sstevel@tonic-gate 2950Sstevel@tonic-gate tmpl[attrs].type = CKA_SIGN; 2960Sstevel@tonic-gate tmpl[attrs].pValue = &true; 2970Sstevel@tonic-gate tmpl[attrs].ulValueLen = sizeof (CK_BBOOL); 2980Sstevel@tonic-gate attrs++; 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate if (keylen > 0) { 3010Sstevel@tonic-gate tmpl[attrs].type = CKA_VALUE_LEN; 3020Sstevel@tonic-gate tmpl[attrs].pValue = &keylen; 3030Sstevel@tonic-gate tmpl[attrs].ulValueLen = sizeof (keylen); 3040Sstevel@tonic-gate attrs++; 3050Sstevel@tonic-gate } 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate params.saltSource = CKZ_SALT_SPECIFIED; 3080Sstevel@tonic-gate params.pSaltSourceData = (void *)pSaltData; 3090Sstevel@tonic-gate params.ulSaltSourceDataLen = saltLen; 3100Sstevel@tonic-gate params.iterations = iterations; 3110Sstevel@tonic-gate params.prf = CKP_PKCS5_PBKD2_HMAC_SHA1; 3120Sstevel@tonic-gate params.pPrfData = NULL; 3130Sstevel@tonic-gate params.ulPrfDataLen = 0; 3140Sstevel@tonic-gate params.pPassword = (CK_UTF8CHAR_PTR)pkeydata; 3150Sstevel@tonic-gate params.ulPasswordLen = &passwd_size; 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate mechanism.mechanism = CKM_PKCS5_PBKD2; 3180Sstevel@tonic-gate mechanism.pParameter = ¶ms; 3190Sstevel@tonic-gate mechanism.ulParameterLen = sizeof (params); 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate rv = C_GenerateKey(hSession, &mechanism, tmpl, 3220Sstevel@tonic-gate attrs, hKey); 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate return (rv); 3250Sstevel@tonic-gate } 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate /* 3290Sstevel@tonic-gate * Execute the command. 3300Sstevel@tonic-gate * algo_str - name of algorithm 3310Sstevel@tonic-gate * filecount - no. of files to process, if 0, use stdin 3320Sstevel@tonic-gate * filelist - list of files 3330Sstevel@tonic-gate * mac_cmd - if true do mac else do digest 3340Sstevel@tonic-gate */ 3350Sstevel@tonic-gate static int 3360Sstevel@tonic-gate execute_cmd(char *algo_str, int filecount, char **filelist, boolean_t mac_cmd) 3370Sstevel@tonic-gate { 3380Sstevel@tonic-gate int fd; 3390Sstevel@tonic-gate char *filename = NULL; 3400Sstevel@tonic-gate CK_RV rv; 3410Sstevel@tonic-gate CK_ULONG slotcount; 3420Sstevel@tonic-gate CK_SLOT_ID slotID; 3430Sstevel@tonic-gate CK_SLOT_ID_PTR pSlotList = NULL; 3440Sstevel@tonic-gate CK_MECHANISM_TYPE mech_type; 3450Sstevel@tonic-gate CK_MECHANISM_INFO info; 3460Sstevel@tonic-gate CK_MECHANISM mech; 3470Sstevel@tonic-gate CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE; 3480Sstevel@tonic-gate CK_BYTE_PTR resultbuf = NULL; 3490Sstevel@tonic-gate CK_ULONG resultlen; 3500Sstevel@tonic-gate CK_BYTE_PTR pkeydata = NULL; 3510Sstevel@tonic-gate CK_OBJECT_HANDLE key = (CK_OBJECT_HANDLE) 0; 3520Sstevel@tonic-gate int keylen = 0; /* key length */ 3530Sstevel@tonic-gate char *resultstr = NULL; /* result in hex string */ 3540Sstevel@tonic-gate int resultstrlen; /* result string length */ 3550Sstevel@tonic-gate int i; 3560Sstevel@tonic-gate int exitcode = EXIT_SUCCESS; /* return code */ 3570Sstevel@tonic-gate int slot, mek; /* index variables */ 3580Sstevel@tonic-gate int mech_match = 0; 3590Sstevel@tonic-gate CK_BYTE salt[PBKD2_SALT_SIZE]; 3600Sstevel@tonic-gate CK_ULONG keysize; 3610Sstevel@tonic-gate CK_ULONG iterations = PBKD2_ITERATIONS; 3620Sstevel@tonic-gate 3630Sstevel@tonic-gate if (aflag) { 3640Sstevel@tonic-gate /* 3650Sstevel@tonic-gate * Determine if algorithm/mechanism is valid 3660Sstevel@tonic-gate */ 3670Sstevel@tonic-gate for (mech_match = 0; mech_match < MECH_ALIASES_COUNT; 3680Sstevel@tonic-gate mech_match++) { 3690Sstevel@tonic-gate if (strcmp(algo_str, 3700Sstevel@tonic-gate mech_aliases[mech_match].alias) == 0) { 3710Sstevel@tonic-gate mech_type = mech_aliases[mech_match].type; 3720Sstevel@tonic-gate break; 3730Sstevel@tonic-gate } 3740Sstevel@tonic-gate 3750Sstevel@tonic-gate } 3760Sstevel@tonic-gate 3770Sstevel@tonic-gate if (mech_match == MECH_ALIASES_COUNT) { 3780Sstevel@tonic-gate cryptoerror(LOG_STDERR, 3790Sstevel@tonic-gate gettext("unknown algorithm -- %s"), algo_str); 3800Sstevel@tonic-gate return (EXIT_FAILURE); 3810Sstevel@tonic-gate } 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate /* Get key to do a MAC operation */ 3840Sstevel@tonic-gate if (mac_cmd) { 3850Sstevel@tonic-gate keylen = getkey(keyfile, &pkeydata); 3860Sstevel@tonic-gate if (keylen <= 0 || pkeydata == NULL) { 3870Sstevel@tonic-gate cryptoerror(LOG_STDERR, 3880Sstevel@tonic-gate gettext("invalid key.")); 3890Sstevel@tonic-gate return (EXIT_FAILURE); 3900Sstevel@tonic-gate } 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate } 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate /* Initialize, and get list of slots */ 3950Sstevel@tonic-gate if ((rv = C_Initialize(NULL)) != CKR_OK) { 3960Sstevel@tonic-gate cryptoerror(LOG_STDERR, 3970Sstevel@tonic-gate gettext("failed to initialize PKCS #11 framework: %s"), 3980Sstevel@tonic-gate pkcs11_strerror(rv)); 3990Sstevel@tonic-gate return (EXIT_FAILURE); 4000Sstevel@tonic-gate } 4010Sstevel@tonic-gate 4020Sstevel@tonic-gate /* Get slot count */ 4030Sstevel@tonic-gate rv = C_GetSlotList(0, NULL_PTR, &slotcount); 4040Sstevel@tonic-gate if (rv != CKR_OK || slotcount == 0) { 4050Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 4060Sstevel@tonic-gate "failed to find any cryptographic provider," 4070Sstevel@tonic-gate "please check with your system administrator: %s"), 4080Sstevel@tonic-gate pkcs11_strerror(rv)); 4090Sstevel@tonic-gate exitcode = EXIT_FAILURE; 4100Sstevel@tonic-gate goto cleanup; 4110Sstevel@tonic-gate } 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate /* Found at least one slot, allocate memory for slot list */ 4140Sstevel@tonic-gate pSlotList = malloc(slotcount * sizeof (CK_SLOT_ID)); 4150Sstevel@tonic-gate if (pSlotList == NULL_PTR) { 4160Sstevel@tonic-gate int err = errno; 4170Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s\n"), 4180Sstevel@tonic-gate strerror(err)); 4190Sstevel@tonic-gate exitcode = EXIT_FAILURE; 4200Sstevel@tonic-gate goto cleanup; 4210Sstevel@tonic-gate } 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate /* Get the list of slots */ 4240Sstevel@tonic-gate if ((rv = C_GetSlotList(0, pSlotList, &slotcount)) != CKR_OK) { 4250Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 4260Sstevel@tonic-gate "failed to find any cryptographic provider," 4270Sstevel@tonic-gate "please check with your system administrator: %s"), 4280Sstevel@tonic-gate pkcs11_strerror(rv)); 4290Sstevel@tonic-gate exitcode = EXIT_FAILURE; 4300Sstevel@tonic-gate goto cleanup; 4310Sstevel@tonic-gate } 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate /* 4340Sstevel@tonic-gate * Obtain list of algorithms if -l option was given 4350Sstevel@tonic-gate */ 4360Sstevel@tonic-gate if (lflag) { 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate for (slot = 0; slot < slotcount; slot++) { 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate /* Iterate through each mechanism */ 4410Sstevel@tonic-gate for (mek = 0; mek < MECH_ALIASES_COUNT; mek++) { 4420Sstevel@tonic-gate rv = C_GetMechanismInfo(pSlotList[slot], 4430Sstevel@tonic-gate mech_aliases[mek].type, &info); 4440Sstevel@tonic-gate 4450Sstevel@tonic-gate /* Only check algorithms that can be used */ 4460Sstevel@tonic-gate if ((rv != CKR_OK) || 4470Sstevel@tonic-gate (!mac_cmd && (info.flags & CKF_SIGN)) || 4480Sstevel@tonic-gate (mac_cmd && (info.flags & CKF_DIGEST))) 4490Sstevel@tonic-gate continue; 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate /* 4520Sstevel@tonic-gate * Set to minimum/maximum key sizes assuming 4530Sstevel@tonic-gate * the values available are not 0. 4540Sstevel@tonic-gate */ 4550Sstevel@tonic-gate if (info.ulMinKeySize && (info.ulMinKeySize < 4560Sstevel@tonic-gate mech_aliases[mek].keysize_min)) 4570Sstevel@tonic-gate mech_aliases[mek].keysize_min = 4580Sstevel@tonic-gate info.ulMinKeySize; 4590Sstevel@tonic-gate 4600Sstevel@tonic-gate if (info.ulMaxKeySize && (info.ulMaxKeySize > 4610Sstevel@tonic-gate mech_aliases[mek].keysize_max)) 4620Sstevel@tonic-gate mech_aliases[mek].keysize_max = 4630Sstevel@tonic-gate info.ulMaxKeySize; 4640Sstevel@tonic-gate 4650Sstevel@tonic-gate mech_aliases[mek].available = B_TRUE; 4660Sstevel@tonic-gate } 4670Sstevel@tonic-gate 4680Sstevel@tonic-gate } 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate algorithm_list(mac_cmd); 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate goto cleanup; 4730Sstevel@tonic-gate } 4740Sstevel@tonic-gate 4750Sstevel@tonic-gate /* Find a slot with matching mechanism */ 4760Sstevel@tonic-gate for (i = 0; i < slotcount; i++) { 4770Sstevel@tonic-gate slotID = pSlotList[i]; 4780Sstevel@tonic-gate rv = C_GetMechanismInfo(slotID, mech_type, &info); 4790Sstevel@tonic-gate if (rv != CKR_OK) { 4800Sstevel@tonic-gate continue; /* to the next slot */ 4810Sstevel@tonic-gate } else { 4820Sstevel@tonic-gate if (mac_cmd) { 4830Sstevel@tonic-gate /* 4840Sstevel@tonic-gate * Make sure the slot supports 4850Sstevel@tonic-gate * PKCS5 key generation if we 4860Sstevel@tonic-gate * will be using it later. 4870Sstevel@tonic-gate * We use it whenever the key 4880Sstevel@tonic-gate * is entered at command line. 4890Sstevel@tonic-gate */ 4900Sstevel@tonic-gate if ((info.flags & CKF_SIGN) && 4910Sstevel@tonic-gate (keyfile == NULL)) { 4920Sstevel@tonic-gate CK_MECHANISM_INFO kg_info; 4930Sstevel@tonic-gate rv = C_GetMechanismInfo(slotID, 4940Sstevel@tonic-gate CKM_PKCS5_PBKD2, &kg_info); 4950Sstevel@tonic-gate if (rv == CKR_OK) 4960Sstevel@tonic-gate break; 4970Sstevel@tonic-gate } else if (info.flags & CKF_SIGN) { 4980Sstevel@tonic-gate break; 4990Sstevel@tonic-gate } 5000Sstevel@tonic-gate } else { 5010Sstevel@tonic-gate if (info.flags & CKF_DIGEST) 5020Sstevel@tonic-gate break; 5030Sstevel@tonic-gate } 5040Sstevel@tonic-gate } 5050Sstevel@tonic-gate } 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate /* Show error if no matching mechanism found */ 5080Sstevel@tonic-gate if (i == slotcount) { 5090Sstevel@tonic-gate cryptoerror(LOG_STDERR, 5100Sstevel@tonic-gate gettext("no cryptographic provider was " 5110Sstevel@tonic-gate "found for this algorithm -- %s"), algo_str); 5120Sstevel@tonic-gate exitcode = EXIT_FAILURE; 5130Sstevel@tonic-gate goto cleanup; 5140Sstevel@tonic-gate } 5150Sstevel@tonic-gate 5160Sstevel@tonic-gate /* Mechanism is supported. Go ahead & open a session */ 5170Sstevel@tonic-gate rv = C_OpenSession(slotID, CKF_SERIAL_SESSION, 5180Sstevel@tonic-gate NULL_PTR, NULL, &hSession); 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate if (rv != CKR_OK) { 5210Sstevel@tonic-gate cryptoerror(LOG_STDERR, 5220Sstevel@tonic-gate gettext("can not open PKCS#11 session: %s"), 5230Sstevel@tonic-gate pkcs11_strerror(rv)); 5240Sstevel@tonic-gate exitcode = EXIT_FAILURE; 5250Sstevel@tonic-gate goto cleanup; 5260Sstevel@tonic-gate } 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate /* Create a key object for mac operation */ 5290Sstevel@tonic-gate if (mac_cmd) { 5300Sstevel@tonic-gate /* 5310Sstevel@tonic-gate * If we read keybytes from a file, 5320Sstevel@tonic-gate * do NOT process them with C_GenerateKey, 5330Sstevel@tonic-gate * treat them as raw keydata bytes and 5340Sstevel@tonic-gate * create a key object for them. 5350Sstevel@tonic-gate */ 5360Sstevel@tonic-gate if (keyfile) { 5370Sstevel@tonic-gate CK_OBJECT_CLASS class = CKO_SECRET_KEY; 5380Sstevel@tonic-gate CK_KEY_TYPE tmpl_keytype = CKK_GENERIC_SECRET; 5390Sstevel@tonic-gate CK_BBOOL false = FALSE; 5400Sstevel@tonic-gate int nattr = 0; 5410Sstevel@tonic-gate CK_ATTRIBUTE template[5]; 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate if (mech_type == CKM_DES_MAC) { 5440Sstevel@tonic-gate tmpl_keytype = CKK_DES; 5450Sstevel@tonic-gate } 5460Sstevel@tonic-gate template[nattr].type = CKA_CLASS; 5470Sstevel@tonic-gate template[nattr].pValue = &class; 5480Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (class); 5490Sstevel@tonic-gate nattr++; 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate template[nattr].type = CKA_KEY_TYPE; 5520Sstevel@tonic-gate template[nattr].pValue = &tmpl_keytype; 5530Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (tmpl_keytype); 5540Sstevel@tonic-gate nattr++; 5550Sstevel@tonic-gate 5560Sstevel@tonic-gate template[nattr].type = CKA_SIGN; 5570Sstevel@tonic-gate template[nattr].pValue = &true; 5580Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (true); 5590Sstevel@tonic-gate nattr++; 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate template[nattr].type = CKA_TOKEN; 5620Sstevel@tonic-gate template[nattr].pValue = &false; 5630Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (false); 5640Sstevel@tonic-gate nattr++; 5650Sstevel@tonic-gate 5660Sstevel@tonic-gate template[nattr].type = CKA_VALUE; 5670Sstevel@tonic-gate template[nattr].pValue = pkeydata; 5680Sstevel@tonic-gate template[nattr].ulValueLen = keylen; 5690Sstevel@tonic-gate nattr++; 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate rv = C_CreateObject(hSession, template, 5720Sstevel@tonic-gate nattr, &key); 5730Sstevel@tonic-gate } else { 5740Sstevel@tonic-gate CK_KEY_TYPE keytype; 5750Sstevel@tonic-gate if (mech_type == CKM_DES_MAC) { 5760Sstevel@tonic-gate keytype = CKK_DES; 5770Sstevel@tonic-gate keysize = 0; 5780Sstevel@tonic-gate } else { 5790Sstevel@tonic-gate keytype = CKK_GENERIC_SECRET; 5800Sstevel@tonic-gate keysize = 16; /* 128 bits */ 5810Sstevel@tonic-gate } 5820Sstevel@tonic-gate /* 5830Sstevel@tonic-gate * We use a fixed salt (0x0a, 0x0a, 0x0a ...) 5840Sstevel@tonic-gate * for creating the key so that the end user 5850Sstevel@tonic-gate * will be able to generate the same 'mac' 5860Sstevel@tonic-gate * using the same passphrase. 5870Sstevel@tonic-gate */ 5880Sstevel@tonic-gate (void) memset(salt, 0x0a, sizeof (salt)); 5890Sstevel@tonic-gate rv = generate_pkcs5_key(hSession, 5900Sstevel@tonic-gate salt, sizeof (salt), 5910Sstevel@tonic-gate iterations, pkeydata, 5920Sstevel@tonic-gate keytype, keylen, keysize, 5930Sstevel@tonic-gate &key); 5940Sstevel@tonic-gate } 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate if (rv != CKR_OK) { 5970Sstevel@tonic-gate cryptoerror(LOG_STDERR, 5980Sstevel@tonic-gate gettext("unable to create key for crypto " 5990Sstevel@tonic-gate "operation: %s"), pkcs11_strerror(rv)); 6000Sstevel@tonic-gate exitcode = EXIT_FAILURE; 6010Sstevel@tonic-gate goto cleanup; 6020Sstevel@tonic-gate } 6030Sstevel@tonic-gate } 6040Sstevel@tonic-gate 6050Sstevel@tonic-gate /* Allocate a buffer to store result. */ 6060Sstevel@tonic-gate resultlen = RESULTLEN; 6070Sstevel@tonic-gate if ((resultbuf = malloc(resultlen)) == NULL) { 6080Sstevel@tonic-gate int err = errno; 6090Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s\n"), 6100Sstevel@tonic-gate strerror(err)); 6110Sstevel@tonic-gate exitcode = EXIT_FAILURE; 6120Sstevel@tonic-gate goto cleanup; 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate /* Allocate a buffer to store result string */ 6160Sstevel@tonic-gate resultstrlen = RESULTLEN; 6170Sstevel@tonic-gate if ((resultstr = malloc(resultstrlen)) == NULL) { 6180Sstevel@tonic-gate int err = errno; 6190Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s\n"), 6200Sstevel@tonic-gate strerror(err)); 6210Sstevel@tonic-gate exitcode = EXIT_FAILURE; 6220Sstevel@tonic-gate goto cleanup; 6230Sstevel@tonic-gate } 6240Sstevel@tonic-gate 6250Sstevel@tonic-gate mech.mechanism = mech_type; 6260Sstevel@tonic-gate mech.pParameter = NULL_PTR; 6270Sstevel@tonic-gate mech.ulParameterLen = 0; 6280Sstevel@tonic-gate exitcode = EXIT_SUCCESS; 6290Sstevel@tonic-gate i = 0; 6300Sstevel@tonic-gate 6310Sstevel@tonic-gate do { 6320Sstevel@tonic-gate if (filecount > 0 && filelist != NULL) { 6330Sstevel@tonic-gate filename = filelist[i]; 6340Sstevel@tonic-gate if ((fd = open(filename, O_RDONLY 6350Sstevel@tonic-gate | O_NONBLOCK)) == -1) { 6360Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 6370Sstevel@tonic-gate "can not open input file %s\n"), filename); 6380Sstevel@tonic-gate exitcode = EXIT_USAGE; 6390Sstevel@tonic-gate continue; 6400Sstevel@tonic-gate } 6410Sstevel@tonic-gate } else { 6420Sstevel@tonic-gate fd = 0; /* use stdin */ 6430Sstevel@tonic-gate } 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate /* 6460Sstevel@tonic-gate * Perform the operation 6470Sstevel@tonic-gate */ 6480Sstevel@tonic-gate if (mac_cmd) { 6490Sstevel@tonic-gate rv = do_mac(hSession, &mech, fd, key, &resultbuf, 6500Sstevel@tonic-gate &resultlen); 6510Sstevel@tonic-gate } else { 6520Sstevel@tonic-gate rv = do_digest(hSession, &mech, fd, &resultbuf, 6530Sstevel@tonic-gate &resultlen); 6540Sstevel@tonic-gate } 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate if (rv != CKR_OK) { 6570Sstevel@tonic-gate cryptoerror(LOG_STDERR, 6580Sstevel@tonic-gate gettext("crypto operation failed for " 6590Sstevel@tonic-gate "file %s: %s\n"), 6600Sstevel@tonic-gate filename ? filename : "STDIN", 6610Sstevel@tonic-gate pkcs11_strerror(rv)); 6620Sstevel@tonic-gate exitcode = EXIT_FAILURE; 6630Sstevel@tonic-gate continue; 6640Sstevel@tonic-gate } 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate /* if result size has changed, allocate a bigger resulstr buf */ 6670Sstevel@tonic-gate if (resultlen != RESULTLEN) { 6680Sstevel@tonic-gate resultstrlen = 2 * resultlen + 1; 6690Sstevel@tonic-gate resultstr = realloc(resultstr, resultstrlen); 6700Sstevel@tonic-gate 6710Sstevel@tonic-gate if (resultstr == NULL) { 6720Sstevel@tonic-gate int err = errno; 6730Sstevel@tonic-gate cryptoerror(LOG_STDERR, 6740Sstevel@tonic-gate gettext("realloc: %s\n"), strerror(err)); 6750Sstevel@tonic-gate exitcode = EXIT_FAILURE; 6760Sstevel@tonic-gate goto cleanup; 6770Sstevel@tonic-gate } 6780Sstevel@tonic-gate } 6790Sstevel@tonic-gate 6800Sstevel@tonic-gate /* Output the result */ 6810Sstevel@tonic-gate tohexstr(resultbuf, resultlen, resultstr, resultstrlen); 6820Sstevel@tonic-gate 6830Sstevel@tonic-gate /* Include mechanism name for verbose */ 6840Sstevel@tonic-gate if (vflag) 6850Sstevel@tonic-gate (void) fprintf(stdout, "%s ", algo_str); 6860Sstevel@tonic-gate 6870Sstevel@tonic-gate /* Include file name for multiple files, or if verbose */ 6880Sstevel@tonic-gate if (filecount > 1 || (vflag && filecount > 0)) { 6890Sstevel@tonic-gate (void) fprintf(stdout, "(%s) = ", filename); 6900Sstevel@tonic-gate } 6910Sstevel@tonic-gate 6920Sstevel@tonic-gate (void) fprintf(stdout, "%s\n", resultstr); 6930Sstevel@tonic-gate (void) close(fd); 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate } while (++i < filecount); 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate 6990Sstevel@tonic-gate /* clear and free the key */ 7000Sstevel@tonic-gate if (mac_cmd) { 7010Sstevel@tonic-gate (void) memset(pkeydata, 0, keylen); 7020Sstevel@tonic-gate free(pkeydata); 7030Sstevel@tonic-gate pkeydata = NULL; 7040Sstevel@tonic-gate } 7050Sstevel@tonic-gate 7060Sstevel@tonic-gate cleanup: 7070Sstevel@tonic-gate if (resultbuf != NULL) { 7080Sstevel@tonic-gate free(resultbuf); 7090Sstevel@tonic-gate } 7100Sstevel@tonic-gate 7110Sstevel@tonic-gate if (resultstr != NULL) { 7120Sstevel@tonic-gate free(resultstr); 7130Sstevel@tonic-gate } 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate if (pSlotList != NULL) { 7160Sstevel@tonic-gate free(pSlotList); 7170Sstevel@tonic-gate } 7180Sstevel@tonic-gate 7190Sstevel@tonic-gate if (key != (CK_OBJECT_HANDLE) 0) { 7200Sstevel@tonic-gate (void) C_DestroyObject(hSession, key); 7210Sstevel@tonic-gate } 7220Sstevel@tonic-gate 7230Sstevel@tonic-gate if (hSession != CK_INVALID_HANDLE) 7240Sstevel@tonic-gate (void) C_CloseSession(hSession); 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate (void) C_Finalize(NULL_PTR); 7270Sstevel@tonic-gate 7280Sstevel@tonic-gate return (exitcode); 7290Sstevel@tonic-gate } 7300Sstevel@tonic-gate 7310Sstevel@tonic-gate /* 7320Sstevel@tonic-gate * do_digest - Compute digest of a file 7330Sstevel@tonic-gate * 7340Sstevel@tonic-gate * hSession - session 7350Sstevel@tonic-gate * pmech - ptr to mechanism to be used for digest 7360Sstevel@tonic-gate * fd - file descriptor 7370Sstevel@tonic-gate * pdigest - buffer where digest result is returned 7380Sstevel@tonic-gate * pdigestlen - length of digest buffer on input, 7390Sstevel@tonic-gate * length of result on output 7400Sstevel@tonic-gate */ 7410Sstevel@tonic-gate static CK_RV 7420Sstevel@tonic-gate do_digest(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pmech, 7430Sstevel@tonic-gate int fd, CK_BYTE_PTR *pdigest, CK_ULONG_PTR pdigestlen) 7440Sstevel@tonic-gate { 7450Sstevel@tonic-gate CK_RV rv; 7460Sstevel@tonic-gate ssize_t nread; 7470Sstevel@tonic-gate int saved_errno; 7480Sstevel@tonic-gate 7490Sstevel@tonic-gate if ((rv = C_DigestInit(hSession, pmech)) != CKR_OK) { 7500Sstevel@tonic-gate return (rv); 7510Sstevel@tonic-gate } 7520Sstevel@tonic-gate 7530Sstevel@tonic-gate while ((nread = read(fd, buf, sizeof (buf))) > 0) { 7540Sstevel@tonic-gate /* Get the digest */ 7550Sstevel@tonic-gate rv = C_DigestUpdate(hSession, buf, (CK_ULONG)nread); 7560Sstevel@tonic-gate if (rv != CKR_OK) 7570Sstevel@tonic-gate return (rv); 7580Sstevel@tonic-gate } 7590Sstevel@tonic-gate 7600Sstevel@tonic-gate saved_errno = errno; /* for later use */ 7610Sstevel@tonic-gate 7620Sstevel@tonic-gate /* 7630Sstevel@tonic-gate * Perform the C_DigestFinal, even if there is a read error. 7640Sstevel@tonic-gate * Otherwise C_DigestInit will return CKR_OPERATION_ACTIVE 7650Sstevel@tonic-gate * next time it is called (for another file) 7660Sstevel@tonic-gate */ 7670Sstevel@tonic-gate 7680Sstevel@tonic-gate rv = C_DigestFinal(hSession, *pdigest, pdigestlen); 7690Sstevel@tonic-gate 7700Sstevel@tonic-gate /* result too big to fit? Allocate a bigger buffer */ 7710Sstevel@tonic-gate if (rv == CKR_BUFFER_TOO_SMALL) { 7720Sstevel@tonic-gate *pdigest = realloc(*pdigest, *pdigestlen); 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate if (*pdigest == NULL_PTR) { 7750Sstevel@tonic-gate int err = errno; 7760Sstevel@tonic-gate cryptoerror(LOG_STDERR, 7770Sstevel@tonic-gate gettext("realloc: %s\n"), strerror(err)); 7780Sstevel@tonic-gate return (CKR_HOST_MEMORY); 7790Sstevel@tonic-gate } 7800Sstevel@tonic-gate 7810Sstevel@tonic-gate rv = C_DigestFinal(hSession, *pdigest, pdigestlen); 7820Sstevel@tonic-gate } 7830Sstevel@tonic-gate 7840Sstevel@tonic-gate 7850Sstevel@tonic-gate /* There was a read error */ 7860Sstevel@tonic-gate if (nread == -1) { 7870Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 7880Sstevel@tonic-gate "error reading file: %s"), strerror(saved_errno)); 7890Sstevel@tonic-gate return (CKR_GENERAL_ERROR); 7900Sstevel@tonic-gate } else { 7910Sstevel@tonic-gate return (rv); 7920Sstevel@tonic-gate } 7930Sstevel@tonic-gate } 7940Sstevel@tonic-gate 7950Sstevel@tonic-gate /* 7960Sstevel@tonic-gate * do_mac - Compute mac of a file 7970Sstevel@tonic-gate * 7980Sstevel@tonic-gate * hSession - session 7990Sstevel@tonic-gate * pmech - ptr to mechanism to be used 8000Sstevel@tonic-gate * fd - file descriptor 8010Sstevel@tonic-gate * key - key to be used 8020Sstevel@tonic-gate * psignature - ptr buffer where mac result is returned 8030Sstevel@tonic-gate * returns new buf if current buf is small 8040Sstevel@tonic-gate * psignaturelen - length of mac buffer on input, 8050Sstevel@tonic-gate * length of result on output 8060Sstevel@tonic-gate */ 8070Sstevel@tonic-gate static CK_RV 8080Sstevel@tonic-gate do_mac(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pmech, 8090Sstevel@tonic-gate int fd, CK_OBJECT_HANDLE key, CK_BYTE_PTR *psignature, 8100Sstevel@tonic-gate CK_ULONG_PTR psignaturelen) 8110Sstevel@tonic-gate { 8120Sstevel@tonic-gate CK_RV rv; 8130Sstevel@tonic-gate ssize_t nread; 8140Sstevel@tonic-gate int saved_errno; 8150Sstevel@tonic-gate 8160Sstevel@tonic-gate if ((rv = C_SignInit(hSession, pmech, key)) != CKR_OK) { 8170Sstevel@tonic-gate return (rv); 8180Sstevel@tonic-gate } 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate while ((nread = read(fd, buf, sizeof (buf))) > 0) { 8210Sstevel@tonic-gate /* Get the MAC */ 8220Sstevel@tonic-gate rv = C_SignUpdate(hSession, buf, (CK_ULONG)nread); 8230Sstevel@tonic-gate if (rv != CKR_OK) 8240Sstevel@tonic-gate return (rv); 8250Sstevel@tonic-gate } 8260Sstevel@tonic-gate 8270Sstevel@tonic-gate saved_errno = errno; /* for later use */ 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate /* 8300Sstevel@tonic-gate * Perform the C_SignFinal, even if there is a read error. 8310Sstevel@tonic-gate * Otherwise C_SignInit will return CKR_OPERATION_ACTIVE 8320Sstevel@tonic-gate * next time it is called (for another file) 8330Sstevel@tonic-gate */ 8340Sstevel@tonic-gate 8350Sstevel@tonic-gate rv = C_SignFinal(hSession, *psignature, psignaturelen); 8360Sstevel@tonic-gate 8370Sstevel@tonic-gate /* result too big to fit? Allocate a bigger buffer */ 8380Sstevel@tonic-gate if (rv == CKR_BUFFER_TOO_SMALL) { 8390Sstevel@tonic-gate *psignature = realloc(*psignature, *psignaturelen); 8400Sstevel@tonic-gate 8410Sstevel@tonic-gate if (*psignature == NULL_PTR) { 8420Sstevel@tonic-gate int err = errno; 8430Sstevel@tonic-gate cryptoerror(LOG_STDERR, 8440Sstevel@tonic-gate gettext("realloc: %s\n"), strerror(err)); 8450Sstevel@tonic-gate return (CKR_HOST_MEMORY); 8460Sstevel@tonic-gate } 8470Sstevel@tonic-gate 8480Sstevel@tonic-gate rv = C_SignFinal(hSession, *psignature, psignaturelen); 8490Sstevel@tonic-gate } 8500Sstevel@tonic-gate 8510Sstevel@tonic-gate /* There was a read error */ 8520Sstevel@tonic-gate if (nread == -1) { 8530Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("error reading file: %s"), 8540Sstevel@tonic-gate strerror(saved_errno)); 8550Sstevel@tonic-gate return (CKR_GENERAL_ERROR); 8560Sstevel@tonic-gate } else { 8570Sstevel@tonic-gate return (rv); 8580Sstevel@tonic-gate } 8590Sstevel@tonic-gate } 8600Sstevel@tonic-gate 8610Sstevel@tonic-gate 8620Sstevel@tonic-gate /* 8630Sstevel@tonic-gate * getkey - gets keydata from file specified 8640Sstevel@tonic-gate * 8650Sstevel@tonic-gate * filename - name of file, if null, prompt for pass phrase 8660Sstevel@tonic-gate * pkeydata - binary key data is returned in this buf 8670Sstevel@tonic-gate * 8680Sstevel@tonic-gate * returns length of key, or -1 if error 8690Sstevel@tonic-gate */ 8700Sstevel@tonic-gate static int 8710Sstevel@tonic-gate getkey(char *filename, CK_BYTE_PTR *pkeydata) 8720Sstevel@tonic-gate { 8730Sstevel@tonic-gate struct stat statbuf; 8740Sstevel@tonic-gate char *keybuf = NULL; 8750Sstevel@tonic-gate char *tmpbuf; 8760Sstevel@tonic-gate int keylen; 8770Sstevel@tonic-gate int fd; 8780Sstevel@tonic-gate 8790Sstevel@tonic-gate if (filename != NULL) { 8800Sstevel@tonic-gate 8810Sstevel@tonic-gate /* read the key file into a buffer */ 8820Sstevel@tonic-gate if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) == -1) { 8830Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 8840Sstevel@tonic-gate "can't open %s\n"), filename); 8850Sstevel@tonic-gate return (-1); 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate } 8880Sstevel@tonic-gate 8890Sstevel@tonic-gate if (fstat(fd, &statbuf) == -1) { 8900Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 8910Sstevel@tonic-gate "can't stat %s\n"), filename); 8920Sstevel@tonic-gate (void) close(fd); 8930Sstevel@tonic-gate return (-1); 8940Sstevel@tonic-gate } 8950Sstevel@tonic-gate 8960Sstevel@tonic-gate if (!(statbuf.st_mode & S_IFREG)) { 8970Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 8980Sstevel@tonic-gate "%s not a regular file\n"), filename); 8990Sstevel@tonic-gate (void) close(fd); 9000Sstevel@tonic-gate return (-1); 9010Sstevel@tonic-gate } 9020Sstevel@tonic-gate 9030Sstevel@tonic-gate keylen = (size_t)statbuf.st_size; 9040Sstevel@tonic-gate 9050Sstevel@tonic-gate if (keylen > 0) { 9060Sstevel@tonic-gate /* allocate a buffer to hold the entire key */ 9070Sstevel@tonic-gate if ((keybuf = malloc(keylen)) == NULL) { 9080Sstevel@tonic-gate int err = errno; 9090Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s\n"), 9100Sstevel@tonic-gate strerror(err)); 9110Sstevel@tonic-gate (void) close(fd); 9120Sstevel@tonic-gate return (-1); 9130Sstevel@tonic-gate } 9140Sstevel@tonic-gate 9150Sstevel@tonic-gate if (read(fd, keybuf, keylen) != keylen) { 9160Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 9170Sstevel@tonic-gate "can't read %s\n"), filename); 9180Sstevel@tonic-gate (void) close(fd); 9190Sstevel@tonic-gate return (-1); 9200Sstevel@tonic-gate } 9210Sstevel@tonic-gate } 9220Sstevel@tonic-gate (void) close(fd); 9230Sstevel@tonic-gate 9240Sstevel@tonic-gate } else { 9250Sstevel@tonic-gate 9260Sstevel@tonic-gate /* No file, prompt for a pass phrase */ 9270Sstevel@tonic-gate tmpbuf = getpassphrase(gettext("Enter key:")); 9280Sstevel@tonic-gate 9290Sstevel@tonic-gate if (tmpbuf == NULL) { 9300Sstevel@tonic-gate return (-1); /* error */ 9310Sstevel@tonic-gate } else { 9320Sstevel@tonic-gate keybuf = strdup(tmpbuf); 9330Sstevel@tonic-gate (void) memset(tmpbuf, 0, strlen(tmpbuf)); 9340Sstevel@tonic-gate } 9350Sstevel@tonic-gate keylen = strlen(keybuf); 9360Sstevel@tonic-gate } 9370Sstevel@tonic-gate 9380Sstevel@tonic-gate *pkeydata = (CK_BYTE_PTR)keybuf; 9390Sstevel@tonic-gate 9400Sstevel@tonic-gate return (keylen); 9410Sstevel@tonic-gate } 942