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 /* 223812Shylee * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * digest.c 300Sstevel@tonic-gate * 310Sstevel@tonic-gate * Implements digest(1) and mac(1) commands 320Sstevel@tonic-gate * If command name is mac, performs mac operation 330Sstevel@tonic-gate * else perform digest operation 340Sstevel@tonic-gate * 350Sstevel@tonic-gate * See the man pages for digest and mac for details on 360Sstevel@tonic-gate * how these commands work. 370Sstevel@tonic-gate */ 380Sstevel@tonic-gate 390Sstevel@tonic-gate #include <stdio.h> 400Sstevel@tonic-gate #include <stdlib.h> 410Sstevel@tonic-gate #include <unistd.h> 420Sstevel@tonic-gate #include <fcntl.h> 430Sstevel@tonic-gate #include <ctype.h> 440Sstevel@tonic-gate #include <strings.h> 450Sstevel@tonic-gate #include <libintl.h> 460Sstevel@tonic-gate #include <libgen.h> 470Sstevel@tonic-gate #include <locale.h> 480Sstevel@tonic-gate #include <errno.h> 490Sstevel@tonic-gate #include <sys/types.h> 500Sstevel@tonic-gate #include <sys/stat.h> 510Sstevel@tonic-gate #include <security/cryptoki.h> 520Sstevel@tonic-gate #include <limits.h> 530Sstevel@tonic-gate #include <cryptoutil.h> 543812Shylee #include <kmfapi.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 */ 813812Shylee #define MAC_OPTIONS "lva:k:T:K:" /* for getopt */ 820Sstevel@tonic-gate #define DIGEST_NAME "digest" /* name of mac command */ 830Sstevel@tonic-gate #define DIGEST_OPTIONS "lva:" /* for getopt */ 843812Shylee #define DEFAULT_TOKEN_PROMPT "Enter PIN for %s: " 853812Shylee #define PK_DEFAULT_PK11TOKEN SOFT_TOKEN_LABEL 860Sstevel@tonic-gate 870Sstevel@tonic-gate static boolean_t vflag = B_FALSE; /* -v (verbose) flag, optional */ 880Sstevel@tonic-gate static boolean_t aflag = B_FALSE; /* -a <algorithm> flag, required */ 890Sstevel@tonic-gate static boolean_t lflag = B_FALSE; /* -l flag, for mac and digest */ 903812Shylee static boolean_t kflag = B_FALSE; 913812Shylee static boolean_t Tflag = B_FALSE; 923812Shylee static boolean_t Kflag = B_FALSE; 930Sstevel@tonic-gate 940Sstevel@tonic-gate static char *keyfile = NULL; /* name of keyfile */ 953812Shylee static char *token_label = NULL; 963812Shylee static char *key_label = NULL; 973812Shylee 980Sstevel@tonic-gate static CK_BYTE buf[BUFFERSIZE]; 990Sstevel@tonic-gate 1000Sstevel@tonic-gate struct mech_alias { 1010Sstevel@tonic-gate CK_MECHANISM_TYPE type; 1020Sstevel@tonic-gate char *alias; 1030Sstevel@tonic-gate CK_ULONG keysize_min; 1040Sstevel@tonic-gate CK_ULONG keysize_max; 1050Sstevel@tonic-gate int keysize_unit; 1060Sstevel@tonic-gate boolean_t available; 1070Sstevel@tonic-gate }; 1080Sstevel@tonic-gate 109676Sizick #define MECH_ALIASES_COUNT 11 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate static struct mech_alias mech_aliases[] = { 1120Sstevel@tonic-gate { CKM_SHA_1, "sha1", ULONG_MAX, 0L, 8, B_FALSE }, 1130Sstevel@tonic-gate { CKM_MD5, "md5", ULONG_MAX, 0L, 8, B_FALSE }, 1140Sstevel@tonic-gate { CKM_DES_MAC, "des_mac", ULONG_MAX, 0L, 8, B_FALSE }, 1150Sstevel@tonic-gate { CKM_SHA_1_HMAC, "sha1_hmac", ULONG_MAX, 0L, 8, B_FALSE }, 1160Sstevel@tonic-gate { CKM_MD5_HMAC, "md5_hmac", ULONG_MAX, 0L, 8, B_FALSE }, 117676Sizick { CKM_SHA256, "sha256", ULONG_MAX, 0L, 8, B_FALSE }, 118676Sizick { CKM_SHA384, "sha384", ULONG_MAX, 0L, 8, B_FALSE }, 119676Sizick { CKM_SHA512, "sha512", ULONG_MAX, 0L, 8, B_FALSE }, 120676Sizick { CKM_SHA256_HMAC, "sha256_hmac", ULONG_MAX, 0L, 8, B_FALSE }, 121676Sizick { CKM_SHA384_HMAC, "sha384_hmac", ULONG_MAX, 0L, 8, B_FALSE }, 122676Sizick { CKM_SHA512_HMAC, "sha512_hmac", ULONG_MAX, 0L, 8, B_FALSE } 1230Sstevel@tonic-gate }; 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate static CK_BBOOL true = TRUE; 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate static void usage(boolean_t mac_cmd); 1280Sstevel@tonic-gate static int execute_cmd(char *algo_str, int filecount, 1290Sstevel@tonic-gate char **filelist, boolean_t mac_cmd); 1300Sstevel@tonic-gate static CK_RV do_mac(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pmech, 1310Sstevel@tonic-gate int fd, CK_OBJECT_HANDLE key, CK_BYTE_PTR *psignature, 1320Sstevel@tonic-gate CK_ULONG_PTR psignaturelen); 1330Sstevel@tonic-gate static CK_RV do_digest(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pmech, 1340Sstevel@tonic-gate int fd, CK_BYTE_PTR *pdigest, CK_ULONG_PTR pdigestlen); 1350Sstevel@tonic-gate static int getkey(char *filename, CK_BYTE_PTR *pkeydata); 1363812Shylee static int getpasswd(char *token_spec, CK_BYTE_PTR *pdata, CK_ULONG_PTR psize); 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate int 1390Sstevel@tonic-gate main(int argc, char **argv) 1400Sstevel@tonic-gate { 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate extern char *optarg; 1430Sstevel@tonic-gate extern int optind; 1440Sstevel@tonic-gate int errflag = 0; /* We had an optstr parse error */ 1450Sstevel@tonic-gate char c; /* current getopts flag */ 1460Sstevel@tonic-gate char *algo_str; /* mechanism/algorithm string */ 1470Sstevel@tonic-gate int filecount; 1480Sstevel@tonic-gate boolean_t mac_cmd; /* if TRUE, do mac, else do digest */ 1490Sstevel@tonic-gate char *optstr; 1500Sstevel@tonic-gate char **filelist; /* list of files */ 1510Sstevel@tonic-gate char *cmdname = NULL; /* name of command */ 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1540Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defiend by cc -D */ 1550Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 1560Sstevel@tonic-gate #endif 1570Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate /* 1600Sstevel@tonic-gate * Based on command name, determine 1610Sstevel@tonic-gate * type of command. mac is mac 1620Sstevel@tonic-gate * everything else is digest. 1630Sstevel@tonic-gate */ 1640Sstevel@tonic-gate cmdname = basename(argv[0]); 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate cryptodebug_init(cmdname); 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate if (strcmp(cmdname, MAC_NAME) == 0) 1690Sstevel@tonic-gate mac_cmd = B_TRUE; 1700Sstevel@tonic-gate else if (strcmp(cmdname, DIGEST_NAME) == 0) 1710Sstevel@tonic-gate mac_cmd = B_FALSE; 1720Sstevel@tonic-gate else { 1730Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 174*5051Swyllys "command name must be either digest or mac\n")); 1750Sstevel@tonic-gate exit(EXIT_USAGE); 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate if (mac_cmd) { 1790Sstevel@tonic-gate optstr = MAC_OPTIONS; 1800Sstevel@tonic-gate } else { 1810Sstevel@tonic-gate optstr = DIGEST_OPTIONS; 1820Sstevel@tonic-gate } 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate /* Parse command line arguments */ 1850Sstevel@tonic-gate while (!errflag && (c = getopt(argc, argv, optstr)) != -1) { 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate switch (c) { 1880Sstevel@tonic-gate case 'v': 1890Sstevel@tonic-gate vflag = B_TRUE; 1900Sstevel@tonic-gate break; 1910Sstevel@tonic-gate case 'a': 1920Sstevel@tonic-gate aflag = B_TRUE; 1930Sstevel@tonic-gate algo_str = optarg; 1940Sstevel@tonic-gate break; 1950Sstevel@tonic-gate case 'k': 1963812Shylee kflag = B_TRUE; 1970Sstevel@tonic-gate keyfile = optarg; 1980Sstevel@tonic-gate break; 1990Sstevel@tonic-gate case 'l': 2000Sstevel@tonic-gate lflag = B_TRUE; 2010Sstevel@tonic-gate break; 2023812Shylee case 'T': 2033812Shylee Tflag = B_TRUE; 2043812Shylee token_label = optarg; 2053812Shylee break; 2063812Shylee case 'K': 2073812Shylee Kflag = B_TRUE; 2083812Shylee key_label = optarg; 2093812Shylee break; 2100Sstevel@tonic-gate default: 2110Sstevel@tonic-gate errflag++; 2120Sstevel@tonic-gate } 2130Sstevel@tonic-gate } 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate filecount = argc - optind; 2160Sstevel@tonic-gate if (errflag || (!aflag && !lflag) || (lflag && argc > 2) || 2173812Shylee (kflag && Kflag) || (Tflag && !Kflag) || filecount < 0) { 2180Sstevel@tonic-gate usage(mac_cmd); 2190Sstevel@tonic-gate exit(EXIT_USAGE); 2200Sstevel@tonic-gate } 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate if (filecount == 0) { 2230Sstevel@tonic-gate filelist = NULL; 2240Sstevel@tonic-gate } else { 2250Sstevel@tonic-gate filelist = &argv[optind]; 2260Sstevel@tonic-gate } 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate return (execute_cmd(algo_str, filecount, filelist, mac_cmd)); 2290Sstevel@tonic-gate } 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate /* 2320Sstevel@tonic-gate * usage message for digest/mac 2330Sstevel@tonic-gate */ 2340Sstevel@tonic-gate static void 2350Sstevel@tonic-gate usage(boolean_t mac_cmd) 2360Sstevel@tonic-gate { 2373812Shylee (void) fprintf(stderr, gettext("Usage:\n")); 2380Sstevel@tonic-gate if (mac_cmd) { 2393812Shylee (void) fprintf(stderr, gettext(" mac -l\n")); 2403812Shylee (void) fprintf(stderr, gettext(" mac [-v] -a <algorithm> " 2413812Shylee "[-k <keyfile> | -K <keylabel> [-T <tokenspec>]] " 2423812Shylee "[file...]\n")); 2430Sstevel@tonic-gate } else { 2443812Shylee (void) fprintf(stderr, gettext(" digest -l | [-v] " 2453812Shylee "-a <algorithm> [file...]\n")); 2460Sstevel@tonic-gate } 2470Sstevel@tonic-gate } 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate /* 2500Sstevel@tonic-gate * Print out list of available algorithms. 2510Sstevel@tonic-gate */ 2520Sstevel@tonic-gate static void 2530Sstevel@tonic-gate algorithm_list(boolean_t mac_cmd) 2540Sstevel@tonic-gate { 2550Sstevel@tonic-gate int mech; 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate if (mac_cmd) 2580Sstevel@tonic-gate (void) printf(gettext("Algorithm Keysize: Min " 259*5051Swyllys "Max (bits)\n" 2600Sstevel@tonic-gate "------------------------------------------\n")); 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate for (mech = 0; mech < MECH_ALIASES_COUNT; mech++) { 2630Sstevel@tonic-gate 2640Sstevel@tonic-gate if (mech_aliases[mech].available == B_FALSE) 2650Sstevel@tonic-gate continue; 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate if (mac_cmd) { 2680Sstevel@tonic-gate (void) printf("%-15s", mech_aliases[mech].alias); 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate if (mech_aliases[mech].keysize_min != ULONG_MAX && 2710Sstevel@tonic-gate mech_aliases[mech].keysize_max != 0) 2720Sstevel@tonic-gate (void) printf(" %5lu %5lu\n", 2730Sstevel@tonic-gate (mech_aliases[mech].keysize_min * 274*5051Swyllys mech_aliases[mech].keysize_unit), 2750Sstevel@tonic-gate (mech_aliases[mech].keysize_max * 276*5051Swyllys mech_aliases[mech].keysize_unit)); 2770Sstevel@tonic-gate else 2780Sstevel@tonic-gate (void) printf("\n"); 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate } else 2810Sstevel@tonic-gate (void) printf("%s\n", mech_aliases[mech].alias); 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate } 2840Sstevel@tonic-gate } 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate static CK_RV 2870Sstevel@tonic-gate generate_pkcs5_key(CK_SESSION_HANDLE hSession, 2880Sstevel@tonic-gate CK_BYTE_PTR pSaltData, 2890Sstevel@tonic-gate CK_ULONG saltLen, 2900Sstevel@tonic-gate CK_ULONG iterations, 2910Sstevel@tonic-gate CK_BYTE_PTR pkeydata, /* user entered passphrase */ 2920Sstevel@tonic-gate CK_KEY_TYPE keytype, 2930Sstevel@tonic-gate CK_ULONG passwd_size, 2940Sstevel@tonic-gate CK_ULONG keylen, /* desired length of generated key */ 2950Sstevel@tonic-gate CK_OBJECT_HANDLE *hKey) 2960Sstevel@tonic-gate { 2970Sstevel@tonic-gate CK_RV rv; 2980Sstevel@tonic-gate CK_PKCS5_PBKD2_PARAMS params; 2990Sstevel@tonic-gate CK_MECHANISM mechanism; 3000Sstevel@tonic-gate CK_OBJECT_CLASS class = CKO_SECRET_KEY; 3010Sstevel@tonic-gate CK_ATTRIBUTE tmpl[4]; 3020Sstevel@tonic-gate int attrs = 0; 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate tmpl[attrs].type = CKA_CLASS; 3050Sstevel@tonic-gate tmpl[attrs].pValue = &class; 3060Sstevel@tonic-gate tmpl[attrs].ulValueLen = sizeof (class); 3070Sstevel@tonic-gate attrs++; 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate tmpl[attrs].type = CKA_KEY_TYPE; 3100Sstevel@tonic-gate tmpl[attrs].pValue = &keytype; 3110Sstevel@tonic-gate tmpl[attrs].ulValueLen = sizeof (keytype); 3120Sstevel@tonic-gate attrs++; 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate tmpl[attrs].type = CKA_SIGN; 3150Sstevel@tonic-gate tmpl[attrs].pValue = &true; 3160Sstevel@tonic-gate tmpl[attrs].ulValueLen = sizeof (CK_BBOOL); 3170Sstevel@tonic-gate attrs++; 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate if (keylen > 0) { 3200Sstevel@tonic-gate tmpl[attrs].type = CKA_VALUE_LEN; 3210Sstevel@tonic-gate tmpl[attrs].pValue = &keylen; 3220Sstevel@tonic-gate tmpl[attrs].ulValueLen = sizeof (keylen); 3230Sstevel@tonic-gate attrs++; 3240Sstevel@tonic-gate } 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate params.saltSource = CKZ_SALT_SPECIFIED; 3270Sstevel@tonic-gate params.pSaltSourceData = (void *)pSaltData; 3280Sstevel@tonic-gate params.ulSaltSourceDataLen = saltLen; 3290Sstevel@tonic-gate params.iterations = iterations; 3300Sstevel@tonic-gate params.prf = CKP_PKCS5_PBKD2_HMAC_SHA1; 3310Sstevel@tonic-gate params.pPrfData = NULL; 3320Sstevel@tonic-gate params.ulPrfDataLen = 0; 3330Sstevel@tonic-gate params.pPassword = (CK_UTF8CHAR_PTR)pkeydata; 3340Sstevel@tonic-gate params.ulPasswordLen = &passwd_size; 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate mechanism.mechanism = CKM_PKCS5_PBKD2; 3370Sstevel@tonic-gate mechanism.pParameter = ¶ms; 3380Sstevel@tonic-gate mechanism.ulParameterLen = sizeof (params); 3390Sstevel@tonic-gate 340*5051Swyllys rv = C_GenerateKey(hSession, &mechanism, tmpl, attrs, hKey); 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate return (rv); 3430Sstevel@tonic-gate } 3440Sstevel@tonic-gate 3450Sstevel@tonic-gate 3463812Shylee static int 3473812Shylee get_token_key(CK_SESSION_HANDLE hSession, CK_KEY_TYPE keytype, 3483812Shylee char *keylabel, CK_BYTE *password, int password_len, 3493812Shylee CK_OBJECT_HANDLE *keyobj) 3503812Shylee { 3513812Shylee CK_RV rv; 3523812Shylee CK_ATTRIBUTE pTmpl[10]; 3533812Shylee CK_OBJECT_CLASS class = CKO_SECRET_KEY; 3543812Shylee CK_BBOOL true = 1; 3553812Shylee CK_BBOOL is_token = 1; 3563812Shylee CK_ULONG key_obj_count = 1; 3573812Shylee int i; 3583812Shylee CK_KEY_TYPE ckKeyType = keytype; 3593812Shylee 3603812Shylee 3613812Shylee rv = C_Login(hSession, CKU_USER, (CK_UTF8CHAR_PTR)password, 3623812Shylee password_len); 3633812Shylee if (rv != CKR_OK) { 3643812Shylee (void) fprintf(stderr, "Cannot login to the token." 3653812Shylee " error = %s\n", pkcs11_strerror(rv)); 3663812Shylee return (-1); 3673812Shylee } 3683812Shylee 3693812Shylee i = 0; 3703812Shylee pTmpl[i].type = CKA_TOKEN; 3713812Shylee pTmpl[i].pValue = &is_token; 3723812Shylee pTmpl[i].ulValueLen = sizeof (CK_BBOOL); 3733812Shylee i++; 3743812Shylee 3753812Shylee pTmpl[i].type = CKA_CLASS; 3763812Shylee pTmpl[i].pValue = &class; 3773812Shylee pTmpl[i].ulValueLen = sizeof (class); 3783812Shylee i++; 3793812Shylee 3803812Shylee pTmpl[i].type = CKA_LABEL; 3813812Shylee pTmpl[i].pValue = keylabel; 3823812Shylee pTmpl[i].ulValueLen = strlen(keylabel); 3833812Shylee i++; 3843812Shylee 3853812Shylee pTmpl[i].type = CKA_KEY_TYPE; 3863812Shylee pTmpl[i].pValue = &ckKeyType; 3873812Shylee pTmpl[i].ulValueLen = sizeof (ckKeyType); 3883812Shylee i++; 3893812Shylee 3903812Shylee pTmpl[i].type = CKA_PRIVATE; 3913812Shylee pTmpl[i].pValue = &true; 3923812Shylee pTmpl[i].ulValueLen = sizeof (true); 3933812Shylee i++; 3943812Shylee 3953812Shylee rv = C_FindObjectsInit(hSession, pTmpl, i); 3963812Shylee if (rv != CKR_OK) { 3973812Shylee goto out; 3983812Shylee } 3993812Shylee 4003812Shylee rv = C_FindObjects(hSession, keyobj, 1, &key_obj_count); 4013812Shylee (void) C_FindObjectsFinal(hSession); 4023812Shylee 4033812Shylee out: 4043812Shylee if (rv != CKR_OK) { 4053812Shylee (void) fprintf(stderr, 4063812Shylee "Cannot retrieve key object. error = %s\n", 4073812Shylee pkcs11_strerror(rv)); 4083812Shylee return (-1); 4093812Shylee } 4103812Shylee 4113812Shylee if (key_obj_count == 0) { 4123812Shylee (void) fprintf(stderr, "Cannot find the key object.\n"); 4133812Shylee return (-1); 4143812Shylee } 4153812Shylee 4163812Shylee return (0); 4173812Shylee } 4183812Shylee 4193812Shylee 4200Sstevel@tonic-gate /* 4210Sstevel@tonic-gate * Execute the command. 4220Sstevel@tonic-gate * algo_str - name of algorithm 4230Sstevel@tonic-gate * filecount - no. of files to process, if 0, use stdin 4240Sstevel@tonic-gate * filelist - list of files 4250Sstevel@tonic-gate * mac_cmd - if true do mac else do digest 4260Sstevel@tonic-gate */ 4270Sstevel@tonic-gate static int 4280Sstevel@tonic-gate execute_cmd(char *algo_str, int filecount, char **filelist, boolean_t mac_cmd) 4290Sstevel@tonic-gate { 4300Sstevel@tonic-gate int fd; 4310Sstevel@tonic-gate char *filename = NULL; 4320Sstevel@tonic-gate CK_RV rv; 4330Sstevel@tonic-gate CK_ULONG slotcount; 4340Sstevel@tonic-gate CK_SLOT_ID slotID; 4350Sstevel@tonic-gate CK_SLOT_ID_PTR pSlotList = NULL; 4360Sstevel@tonic-gate CK_MECHANISM_TYPE mech_type; 4370Sstevel@tonic-gate CK_MECHANISM_INFO info; 4380Sstevel@tonic-gate CK_MECHANISM mech; 4390Sstevel@tonic-gate CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE; 4400Sstevel@tonic-gate CK_BYTE_PTR resultbuf = NULL; 4410Sstevel@tonic-gate CK_ULONG resultlen; 4420Sstevel@tonic-gate CK_BYTE_PTR pkeydata = NULL; 4430Sstevel@tonic-gate CK_OBJECT_HANDLE key = (CK_OBJECT_HANDLE) 0; 4440Sstevel@tonic-gate int keylen = 0; /* key length */ 4450Sstevel@tonic-gate char *resultstr = NULL; /* result in hex string */ 4460Sstevel@tonic-gate int resultstrlen; /* result string length */ 4470Sstevel@tonic-gate int i; 4480Sstevel@tonic-gate int exitcode = EXIT_SUCCESS; /* return code */ 4490Sstevel@tonic-gate int slot, mek; /* index variables */ 4500Sstevel@tonic-gate int mech_match = 0; 4510Sstevel@tonic-gate CK_BYTE salt[PBKD2_SALT_SIZE]; 4520Sstevel@tonic-gate CK_ULONG keysize; 4530Sstevel@tonic-gate CK_ULONG iterations = PBKD2_ITERATIONS; 4543812Shylee CK_KEY_TYPE keytype; 4553812Shylee KMF_RETURN kmfrv; 4563812Shylee CK_SLOT_ID token_slot_id; 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate if (aflag) { 4590Sstevel@tonic-gate /* 4600Sstevel@tonic-gate * Determine if algorithm/mechanism is valid 4610Sstevel@tonic-gate */ 4620Sstevel@tonic-gate for (mech_match = 0; mech_match < MECH_ALIASES_COUNT; 463*5051Swyllys mech_match++) { 4640Sstevel@tonic-gate if (strcmp(algo_str, 4650Sstevel@tonic-gate mech_aliases[mech_match].alias) == 0) { 4660Sstevel@tonic-gate mech_type = mech_aliases[mech_match].type; 4670Sstevel@tonic-gate break; 4680Sstevel@tonic-gate } 4690Sstevel@tonic-gate 4700Sstevel@tonic-gate } 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate if (mech_match == MECH_ALIASES_COUNT) { 4730Sstevel@tonic-gate cryptoerror(LOG_STDERR, 4740Sstevel@tonic-gate gettext("unknown algorithm -- %s"), algo_str); 4750Sstevel@tonic-gate return (EXIT_FAILURE); 4760Sstevel@tonic-gate } 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate /* Get key to do a MAC operation */ 4790Sstevel@tonic-gate if (mac_cmd) { 4803812Shylee if (Kflag) { 4813812Shylee int status; 4823812Shylee 4833812Shylee if (token_label == NULL || 4843812Shylee !strlen(token_label)) { 4853812Shylee token_label = PK_DEFAULT_PK11TOKEN; 4863812Shylee } 4873812Shylee 4883812Shylee status = getpasswd(token_label, &pkeydata, 4893812Shylee (CK_ULONG *)&keylen); 4903812Shylee if (status == -1) { 4913812Shylee cryptoerror(LOG_STDERR, 4923812Shylee gettext("invalid passphrase.")); 4933812Shylee return (EXIT_FAILURE); 4943812Shylee } 4953812Shylee 4963812Shylee } else { 4973812Shylee keylen = getkey(keyfile, &pkeydata); 4983812Shylee if (keylen <= 0 || pkeydata == NULL) { 4993812Shylee cryptoerror(LOG_STDERR, 5003812Shylee gettext("invalid key.")); 5013812Shylee return (EXIT_FAILURE); 5023812Shylee } 5030Sstevel@tonic-gate } 5040Sstevel@tonic-gate } 5050Sstevel@tonic-gate } 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate /* Initialize, and get list of slots */ 5083812Shylee rv = C_Initialize(NULL); 5093812Shylee if (rv != CKR_OK && rv != CKR_CRYPTOKI_ALREADY_INITIALIZED) { 5100Sstevel@tonic-gate cryptoerror(LOG_STDERR, 5110Sstevel@tonic-gate gettext("failed to initialize PKCS #11 framework: %s"), 5120Sstevel@tonic-gate pkcs11_strerror(rv)); 5130Sstevel@tonic-gate return (EXIT_FAILURE); 5140Sstevel@tonic-gate } 5150Sstevel@tonic-gate 5160Sstevel@tonic-gate /* Get slot count */ 5170Sstevel@tonic-gate rv = C_GetSlotList(0, NULL_PTR, &slotcount); 5180Sstevel@tonic-gate if (rv != CKR_OK || slotcount == 0) { 5190Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 5200Sstevel@tonic-gate "failed to find any cryptographic provider," 5210Sstevel@tonic-gate "please check with your system administrator: %s"), 5220Sstevel@tonic-gate pkcs11_strerror(rv)); 5230Sstevel@tonic-gate exitcode = EXIT_FAILURE; 5240Sstevel@tonic-gate goto cleanup; 5250Sstevel@tonic-gate } 5260Sstevel@tonic-gate 5270Sstevel@tonic-gate /* Found at least one slot, allocate memory for slot list */ 5280Sstevel@tonic-gate pSlotList = malloc(slotcount * sizeof (CK_SLOT_ID)); 5290Sstevel@tonic-gate if (pSlotList == NULL_PTR) { 5300Sstevel@tonic-gate int err = errno; 5310Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s\n"), 5320Sstevel@tonic-gate strerror(err)); 5330Sstevel@tonic-gate exitcode = EXIT_FAILURE; 5340Sstevel@tonic-gate goto cleanup; 5350Sstevel@tonic-gate } 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate /* Get the list of slots */ 5380Sstevel@tonic-gate if ((rv = C_GetSlotList(0, pSlotList, &slotcount)) != CKR_OK) { 5390Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 5400Sstevel@tonic-gate "failed to find any cryptographic provider," 5410Sstevel@tonic-gate "please check with your system administrator: %s"), 5420Sstevel@tonic-gate pkcs11_strerror(rv)); 5430Sstevel@tonic-gate exitcode = EXIT_FAILURE; 5440Sstevel@tonic-gate goto cleanup; 5450Sstevel@tonic-gate } 5460Sstevel@tonic-gate 5470Sstevel@tonic-gate /* 5480Sstevel@tonic-gate * Obtain list of algorithms if -l option was given 5490Sstevel@tonic-gate */ 5500Sstevel@tonic-gate if (lflag) { 5510Sstevel@tonic-gate 5520Sstevel@tonic-gate for (slot = 0; slot < slotcount; slot++) { 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate /* Iterate through each mechanism */ 5550Sstevel@tonic-gate for (mek = 0; mek < MECH_ALIASES_COUNT; mek++) { 5560Sstevel@tonic-gate rv = C_GetMechanismInfo(pSlotList[slot], 5570Sstevel@tonic-gate mech_aliases[mek].type, &info); 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate /* Only check algorithms that can be used */ 5600Sstevel@tonic-gate if ((rv != CKR_OK) || 5610Sstevel@tonic-gate (!mac_cmd && (info.flags & CKF_SIGN)) || 5620Sstevel@tonic-gate (mac_cmd && (info.flags & CKF_DIGEST))) 5630Sstevel@tonic-gate continue; 5640Sstevel@tonic-gate 5650Sstevel@tonic-gate /* 5660Sstevel@tonic-gate * Set to minimum/maximum key sizes assuming 5670Sstevel@tonic-gate * the values available are not 0. 5680Sstevel@tonic-gate */ 5690Sstevel@tonic-gate if (info.ulMinKeySize && (info.ulMinKeySize < 5700Sstevel@tonic-gate mech_aliases[mek].keysize_min)) 5710Sstevel@tonic-gate mech_aliases[mek].keysize_min = 572*5051Swyllys info.ulMinKeySize; 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate if (info.ulMaxKeySize && (info.ulMaxKeySize > 5750Sstevel@tonic-gate mech_aliases[mek].keysize_max)) 5760Sstevel@tonic-gate mech_aliases[mek].keysize_max = 577*5051Swyllys info.ulMaxKeySize; 5780Sstevel@tonic-gate 5790Sstevel@tonic-gate mech_aliases[mek].available = B_TRUE; 5800Sstevel@tonic-gate } 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate } 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate algorithm_list(mac_cmd); 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate goto cleanup; 5870Sstevel@tonic-gate } 5880Sstevel@tonic-gate 5893812Shylee /* 5903812Shylee * Find a slot with matching mechanism 5913812Shylee * 5923812Shylee * If -K is specified, we find the slot id for the token first, then 5933812Shylee * check if the slot supports the algorithm. 5943812Shylee */ 5953812Shylee i = 0; 5963812Shylee if (Kflag) { 597*5051Swyllys kmfrv = kmf_pk11_token_lookup(NULL, token_label, 598*5051Swyllys &token_slot_id); 5993812Shylee if (kmfrv != KMF_OK) { 6003812Shylee cryptoerror(LOG_STDERR, 6013812Shylee gettext("no matching PKCS#11 token")); 6023812Shylee exitcode = EXIT_FAILURE; 6033812Shylee goto cleanup; 6043812Shylee } 6053812Shylee rv = C_GetMechanismInfo(token_slot_id, mech_type, &info); 6063812Shylee if (rv == CKR_OK && (info.flags & CKF_SIGN)) 6073812Shylee slotID = token_slot_id; 6083812Shylee else 6093812Shylee i = slotcount; 6103812Shylee 6113812Shylee } else { 6123812Shylee for (i = 0; i < slotcount; i++) { 6133812Shylee slotID = pSlotList[i]; 6143812Shylee rv = C_GetMechanismInfo(slotID, mech_type, &info); 6153812Shylee if (rv != CKR_OK) { 6163812Shylee continue; /* to the next slot */ 6173812Shylee } else { 6183812Shylee if (mac_cmd) { 6193812Shylee /* 6203812Shylee * Make sure the slot supports 6213812Shylee * PKCS5 key generation if we 6223812Shylee * will be using it later. 6233812Shylee * We use it whenever the key 6243812Shylee * is entered at command line. 6253812Shylee */ 6263812Shylee if ((info.flags & CKF_SIGN) && 6273812Shylee (keyfile == NULL)) { 6283812Shylee CK_MECHANISM_INFO kg_info; 6293812Shylee rv = C_GetMechanismInfo(slotID, 6303812Shylee CKM_PKCS5_PBKD2, &kg_info); 6313812Shylee if (rv == CKR_OK) 6323812Shylee break; 6333812Shylee } else if (info.flags & CKF_SIGN) { 6343812Shylee break; 6353812Shylee } 6363812Shylee } else { 6373812Shylee if (info.flags & CKF_DIGEST) 6383812Shylee break; 6390Sstevel@tonic-gate } 6400Sstevel@tonic-gate } 6410Sstevel@tonic-gate } 6420Sstevel@tonic-gate } 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate /* Show error if no matching mechanism found */ 6450Sstevel@tonic-gate if (i == slotcount) { 6460Sstevel@tonic-gate cryptoerror(LOG_STDERR, 6470Sstevel@tonic-gate gettext("no cryptographic provider was " 6480Sstevel@tonic-gate "found for this algorithm -- %s"), algo_str); 6490Sstevel@tonic-gate exitcode = EXIT_FAILURE; 6500Sstevel@tonic-gate goto cleanup; 6510Sstevel@tonic-gate } 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate /* Mechanism is supported. Go ahead & open a session */ 6540Sstevel@tonic-gate rv = C_OpenSession(slotID, CKF_SERIAL_SESSION, 655*5051Swyllys NULL_PTR, NULL, &hSession); 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate if (rv != CKR_OK) { 6580Sstevel@tonic-gate cryptoerror(LOG_STDERR, 6590Sstevel@tonic-gate gettext("can not open PKCS#11 session: %s"), 6600Sstevel@tonic-gate pkcs11_strerror(rv)); 6610Sstevel@tonic-gate exitcode = EXIT_FAILURE; 6620Sstevel@tonic-gate goto cleanup; 6630Sstevel@tonic-gate } 6640Sstevel@tonic-gate 6650Sstevel@tonic-gate /* Create a key object for mac operation */ 6660Sstevel@tonic-gate if (mac_cmd) { 6670Sstevel@tonic-gate /* 6680Sstevel@tonic-gate * If we read keybytes from a file, 6690Sstevel@tonic-gate * do NOT process them with C_GenerateKey, 6700Sstevel@tonic-gate * treat them as raw keydata bytes and 6710Sstevel@tonic-gate * create a key object for them. 6720Sstevel@tonic-gate */ 6730Sstevel@tonic-gate if (keyfile) { 6740Sstevel@tonic-gate CK_OBJECT_CLASS class = CKO_SECRET_KEY; 6750Sstevel@tonic-gate CK_KEY_TYPE tmpl_keytype = CKK_GENERIC_SECRET; 6760Sstevel@tonic-gate CK_BBOOL false = FALSE; 6770Sstevel@tonic-gate int nattr = 0; 6780Sstevel@tonic-gate CK_ATTRIBUTE template[5]; 6790Sstevel@tonic-gate 6800Sstevel@tonic-gate if (mech_type == CKM_DES_MAC) { 6810Sstevel@tonic-gate tmpl_keytype = CKK_DES; 6820Sstevel@tonic-gate } 6830Sstevel@tonic-gate template[nattr].type = CKA_CLASS; 6840Sstevel@tonic-gate template[nattr].pValue = &class; 6850Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (class); 6860Sstevel@tonic-gate nattr++; 6870Sstevel@tonic-gate 6880Sstevel@tonic-gate template[nattr].type = CKA_KEY_TYPE; 6890Sstevel@tonic-gate template[nattr].pValue = &tmpl_keytype; 6900Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (tmpl_keytype); 6910Sstevel@tonic-gate nattr++; 6920Sstevel@tonic-gate 6930Sstevel@tonic-gate template[nattr].type = CKA_SIGN; 6940Sstevel@tonic-gate template[nattr].pValue = &true; 6950Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (true); 6960Sstevel@tonic-gate nattr++; 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate template[nattr].type = CKA_TOKEN; 6990Sstevel@tonic-gate template[nattr].pValue = &false; 7000Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (false); 7010Sstevel@tonic-gate nattr++; 7020Sstevel@tonic-gate 7030Sstevel@tonic-gate template[nattr].type = CKA_VALUE; 7040Sstevel@tonic-gate template[nattr].pValue = pkeydata; 7050Sstevel@tonic-gate template[nattr].ulValueLen = keylen; 7060Sstevel@tonic-gate nattr++; 7070Sstevel@tonic-gate 708*5051Swyllys rv = C_CreateObject(hSession, template, nattr, &key); 7093812Shylee 7103812Shylee } else if (Kflag) { 7113812Shylee 7123812Shylee if (mech_type == CKM_DES_MAC) { 7133812Shylee keytype = CKK_DES; 7143812Shylee } else { 7153812Shylee keytype = CKK_GENERIC_SECRET; 7163812Shylee } 7173812Shylee 7183812Shylee rv = get_token_key(hSession, keytype, key_label, 7193812Shylee pkeydata, keylen, &key); 7203812Shylee if (rv != CKR_OK) { 7213812Shylee exitcode = EXIT_FAILURE; 7223812Shylee goto cleanup; 7233812Shylee } 7240Sstevel@tonic-gate } else { 7250Sstevel@tonic-gate CK_KEY_TYPE keytype; 7260Sstevel@tonic-gate if (mech_type == CKM_DES_MAC) { 7270Sstevel@tonic-gate keytype = CKK_DES; 7280Sstevel@tonic-gate keysize = 0; 7290Sstevel@tonic-gate } else { 7300Sstevel@tonic-gate keytype = CKK_GENERIC_SECRET; 7310Sstevel@tonic-gate keysize = 16; /* 128 bits */ 7320Sstevel@tonic-gate } 7330Sstevel@tonic-gate /* 7340Sstevel@tonic-gate * We use a fixed salt (0x0a, 0x0a, 0x0a ...) 7350Sstevel@tonic-gate * for creating the key so that the end user 7360Sstevel@tonic-gate * will be able to generate the same 'mac' 7370Sstevel@tonic-gate * using the same passphrase. 7380Sstevel@tonic-gate */ 7390Sstevel@tonic-gate (void) memset(salt, 0x0a, sizeof (salt)); 7400Sstevel@tonic-gate rv = generate_pkcs5_key(hSession, 741*5051Swyllys salt, sizeof (salt), iterations, pkeydata, 742*5051Swyllys keytype, keylen, keysize, &key); 7430Sstevel@tonic-gate } 7440Sstevel@tonic-gate 7450Sstevel@tonic-gate if (rv != CKR_OK) { 7460Sstevel@tonic-gate cryptoerror(LOG_STDERR, 7470Sstevel@tonic-gate gettext("unable to create key for crypto " 7480Sstevel@tonic-gate "operation: %s"), pkcs11_strerror(rv)); 7490Sstevel@tonic-gate exitcode = EXIT_FAILURE; 7500Sstevel@tonic-gate goto cleanup; 7510Sstevel@tonic-gate } 7520Sstevel@tonic-gate } 7530Sstevel@tonic-gate 7540Sstevel@tonic-gate /* Allocate a buffer to store result. */ 7550Sstevel@tonic-gate resultlen = RESULTLEN; 7560Sstevel@tonic-gate if ((resultbuf = malloc(resultlen)) == NULL) { 7570Sstevel@tonic-gate int err = errno; 7580Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s\n"), 7590Sstevel@tonic-gate strerror(err)); 7600Sstevel@tonic-gate exitcode = EXIT_FAILURE; 7610Sstevel@tonic-gate goto cleanup; 7620Sstevel@tonic-gate } 7630Sstevel@tonic-gate 7640Sstevel@tonic-gate /* Allocate a buffer to store result string */ 7650Sstevel@tonic-gate resultstrlen = RESULTLEN; 7660Sstevel@tonic-gate if ((resultstr = malloc(resultstrlen)) == NULL) { 7670Sstevel@tonic-gate int err = errno; 7680Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s\n"), 7690Sstevel@tonic-gate strerror(err)); 7700Sstevel@tonic-gate exitcode = EXIT_FAILURE; 7710Sstevel@tonic-gate goto cleanup; 7720Sstevel@tonic-gate } 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate mech.mechanism = mech_type; 7750Sstevel@tonic-gate mech.pParameter = NULL_PTR; 7760Sstevel@tonic-gate mech.ulParameterLen = 0; 7770Sstevel@tonic-gate exitcode = EXIT_SUCCESS; 7780Sstevel@tonic-gate i = 0; 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate do { 7810Sstevel@tonic-gate if (filecount > 0 && filelist != NULL) { 7820Sstevel@tonic-gate filename = filelist[i]; 783*5051Swyllys if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) == 784*5051Swyllys -1) { 7850Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 7860Sstevel@tonic-gate "can not open input file %s\n"), filename); 7870Sstevel@tonic-gate exitcode = EXIT_USAGE; 7880Sstevel@tonic-gate continue; 7890Sstevel@tonic-gate } 7900Sstevel@tonic-gate } else { 7910Sstevel@tonic-gate fd = 0; /* use stdin */ 7920Sstevel@tonic-gate } 7930Sstevel@tonic-gate 7940Sstevel@tonic-gate /* 7950Sstevel@tonic-gate * Perform the operation 7960Sstevel@tonic-gate */ 7970Sstevel@tonic-gate if (mac_cmd) { 7980Sstevel@tonic-gate rv = do_mac(hSession, &mech, fd, key, &resultbuf, 799*5051Swyllys &resultlen); 8000Sstevel@tonic-gate } else { 8010Sstevel@tonic-gate rv = do_digest(hSession, &mech, fd, &resultbuf, 802*5051Swyllys &resultlen); 8030Sstevel@tonic-gate } 8040Sstevel@tonic-gate 8050Sstevel@tonic-gate if (rv != CKR_OK) { 8060Sstevel@tonic-gate cryptoerror(LOG_STDERR, 8070Sstevel@tonic-gate gettext("crypto operation failed for " 808*5051Swyllys "file %s: %s\n"), 8090Sstevel@tonic-gate filename ? filename : "STDIN", 8100Sstevel@tonic-gate pkcs11_strerror(rv)); 8110Sstevel@tonic-gate exitcode = EXIT_FAILURE; 8120Sstevel@tonic-gate continue; 8130Sstevel@tonic-gate } 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate /* if result size has changed, allocate a bigger resulstr buf */ 8160Sstevel@tonic-gate if (resultlen != RESULTLEN) { 8170Sstevel@tonic-gate resultstrlen = 2 * resultlen + 1; 8180Sstevel@tonic-gate resultstr = realloc(resultstr, resultstrlen); 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate if (resultstr == NULL) { 8210Sstevel@tonic-gate int err = errno; 8220Sstevel@tonic-gate cryptoerror(LOG_STDERR, 8230Sstevel@tonic-gate gettext("realloc: %s\n"), strerror(err)); 8240Sstevel@tonic-gate exitcode = EXIT_FAILURE; 8250Sstevel@tonic-gate goto cleanup; 8260Sstevel@tonic-gate } 8270Sstevel@tonic-gate } 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate /* Output the result */ 8300Sstevel@tonic-gate tohexstr(resultbuf, resultlen, resultstr, resultstrlen); 8310Sstevel@tonic-gate 8320Sstevel@tonic-gate /* Include mechanism name for verbose */ 8330Sstevel@tonic-gate if (vflag) 8340Sstevel@tonic-gate (void) fprintf(stdout, "%s ", algo_str); 8350Sstevel@tonic-gate 8360Sstevel@tonic-gate /* Include file name for multiple files, or if verbose */ 8370Sstevel@tonic-gate if (filecount > 1 || (vflag && filecount > 0)) { 8380Sstevel@tonic-gate (void) fprintf(stdout, "(%s) = ", filename); 8390Sstevel@tonic-gate } 8400Sstevel@tonic-gate 8410Sstevel@tonic-gate (void) fprintf(stdout, "%s\n", resultstr); 8420Sstevel@tonic-gate (void) close(fd); 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate 8450Sstevel@tonic-gate } while (++i < filecount); 8460Sstevel@tonic-gate 8470Sstevel@tonic-gate 8480Sstevel@tonic-gate /* clear and free the key */ 8490Sstevel@tonic-gate if (mac_cmd) { 8500Sstevel@tonic-gate (void) memset(pkeydata, 0, keylen); 8510Sstevel@tonic-gate free(pkeydata); 8520Sstevel@tonic-gate pkeydata = NULL; 8530Sstevel@tonic-gate } 8540Sstevel@tonic-gate 8550Sstevel@tonic-gate cleanup: 8560Sstevel@tonic-gate if (resultbuf != NULL) { 8570Sstevel@tonic-gate free(resultbuf); 8580Sstevel@tonic-gate } 8590Sstevel@tonic-gate 8600Sstevel@tonic-gate if (resultstr != NULL) { 8610Sstevel@tonic-gate free(resultstr); 8620Sstevel@tonic-gate } 8630Sstevel@tonic-gate 8640Sstevel@tonic-gate if (pSlotList != NULL) { 8650Sstevel@tonic-gate free(pSlotList); 8660Sstevel@tonic-gate } 8670Sstevel@tonic-gate 8683812Shylee if (!Kflag && key != (CK_OBJECT_HANDLE) 0) { 8690Sstevel@tonic-gate (void) C_DestroyObject(hSession, key); 8700Sstevel@tonic-gate } 8710Sstevel@tonic-gate 8720Sstevel@tonic-gate if (hSession != CK_INVALID_HANDLE) 8730Sstevel@tonic-gate (void) C_CloseSession(hSession); 8740Sstevel@tonic-gate 8750Sstevel@tonic-gate (void) C_Finalize(NULL_PTR); 8760Sstevel@tonic-gate 8770Sstevel@tonic-gate return (exitcode); 8780Sstevel@tonic-gate } 8790Sstevel@tonic-gate 8800Sstevel@tonic-gate /* 8810Sstevel@tonic-gate * do_digest - Compute digest of a file 8820Sstevel@tonic-gate * 8830Sstevel@tonic-gate * hSession - session 8840Sstevel@tonic-gate * pmech - ptr to mechanism to be used for digest 8850Sstevel@tonic-gate * fd - file descriptor 8860Sstevel@tonic-gate * pdigest - buffer where digest result is returned 8870Sstevel@tonic-gate * pdigestlen - length of digest buffer on input, 8880Sstevel@tonic-gate * length of result on output 8890Sstevel@tonic-gate */ 8900Sstevel@tonic-gate static CK_RV 8910Sstevel@tonic-gate do_digest(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pmech, 8920Sstevel@tonic-gate int fd, CK_BYTE_PTR *pdigest, CK_ULONG_PTR pdigestlen) 8930Sstevel@tonic-gate { 8940Sstevel@tonic-gate CK_RV rv; 8950Sstevel@tonic-gate ssize_t nread; 8960Sstevel@tonic-gate int saved_errno; 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate if ((rv = C_DigestInit(hSession, pmech)) != CKR_OK) { 8990Sstevel@tonic-gate return (rv); 9000Sstevel@tonic-gate } 9010Sstevel@tonic-gate 9020Sstevel@tonic-gate while ((nread = read(fd, buf, sizeof (buf))) > 0) { 9030Sstevel@tonic-gate /* Get the digest */ 9040Sstevel@tonic-gate rv = C_DigestUpdate(hSession, buf, (CK_ULONG)nread); 9050Sstevel@tonic-gate if (rv != CKR_OK) 9060Sstevel@tonic-gate return (rv); 9070Sstevel@tonic-gate } 9080Sstevel@tonic-gate 9090Sstevel@tonic-gate saved_errno = errno; /* for later use */ 9100Sstevel@tonic-gate 9110Sstevel@tonic-gate /* 9120Sstevel@tonic-gate * Perform the C_DigestFinal, even if there is a read error. 9130Sstevel@tonic-gate * Otherwise C_DigestInit will return CKR_OPERATION_ACTIVE 9140Sstevel@tonic-gate * next time it is called (for another file) 9150Sstevel@tonic-gate */ 9160Sstevel@tonic-gate 9170Sstevel@tonic-gate rv = C_DigestFinal(hSession, *pdigest, pdigestlen); 9180Sstevel@tonic-gate 9190Sstevel@tonic-gate /* result too big to fit? Allocate a bigger buffer */ 9200Sstevel@tonic-gate if (rv == CKR_BUFFER_TOO_SMALL) { 9210Sstevel@tonic-gate *pdigest = realloc(*pdigest, *pdigestlen); 9220Sstevel@tonic-gate 9230Sstevel@tonic-gate if (*pdigest == NULL_PTR) { 9240Sstevel@tonic-gate int err = errno; 9250Sstevel@tonic-gate cryptoerror(LOG_STDERR, 9260Sstevel@tonic-gate gettext("realloc: %s\n"), strerror(err)); 9270Sstevel@tonic-gate return (CKR_HOST_MEMORY); 9280Sstevel@tonic-gate } 9290Sstevel@tonic-gate 9300Sstevel@tonic-gate rv = C_DigestFinal(hSession, *pdigest, pdigestlen); 9310Sstevel@tonic-gate } 9320Sstevel@tonic-gate 9330Sstevel@tonic-gate 9340Sstevel@tonic-gate /* There was a read error */ 9350Sstevel@tonic-gate if (nread == -1) { 9360Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 937*5051Swyllys "error reading file: %s"), strerror(saved_errno)); 9380Sstevel@tonic-gate return (CKR_GENERAL_ERROR); 9390Sstevel@tonic-gate } else { 9400Sstevel@tonic-gate return (rv); 9410Sstevel@tonic-gate } 9420Sstevel@tonic-gate } 9430Sstevel@tonic-gate 9440Sstevel@tonic-gate /* 9450Sstevel@tonic-gate * do_mac - Compute mac of a file 9460Sstevel@tonic-gate * 9470Sstevel@tonic-gate * hSession - session 9480Sstevel@tonic-gate * pmech - ptr to mechanism to be used 9490Sstevel@tonic-gate * fd - file descriptor 9500Sstevel@tonic-gate * key - key to be used 9510Sstevel@tonic-gate * psignature - ptr buffer where mac result is returned 9520Sstevel@tonic-gate * returns new buf if current buf is small 9530Sstevel@tonic-gate * psignaturelen - length of mac buffer on input, 9540Sstevel@tonic-gate * length of result on output 9550Sstevel@tonic-gate */ 9560Sstevel@tonic-gate static CK_RV 9570Sstevel@tonic-gate do_mac(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pmech, 9580Sstevel@tonic-gate int fd, CK_OBJECT_HANDLE key, CK_BYTE_PTR *psignature, 9590Sstevel@tonic-gate CK_ULONG_PTR psignaturelen) 9600Sstevel@tonic-gate { 9610Sstevel@tonic-gate CK_RV rv; 9620Sstevel@tonic-gate ssize_t nread; 9630Sstevel@tonic-gate int saved_errno; 9640Sstevel@tonic-gate 9650Sstevel@tonic-gate if ((rv = C_SignInit(hSession, pmech, key)) != CKR_OK) { 9660Sstevel@tonic-gate return (rv); 9670Sstevel@tonic-gate } 9680Sstevel@tonic-gate 9690Sstevel@tonic-gate while ((nread = read(fd, buf, sizeof (buf))) > 0) { 9700Sstevel@tonic-gate /* Get the MAC */ 9710Sstevel@tonic-gate rv = C_SignUpdate(hSession, buf, (CK_ULONG)nread); 9720Sstevel@tonic-gate if (rv != CKR_OK) 9730Sstevel@tonic-gate return (rv); 9740Sstevel@tonic-gate } 9750Sstevel@tonic-gate 9760Sstevel@tonic-gate saved_errno = errno; /* for later use */ 9770Sstevel@tonic-gate 9780Sstevel@tonic-gate /* 9790Sstevel@tonic-gate * Perform the C_SignFinal, even if there is a read error. 9800Sstevel@tonic-gate * Otherwise C_SignInit will return CKR_OPERATION_ACTIVE 9810Sstevel@tonic-gate * next time it is called (for another file) 9820Sstevel@tonic-gate */ 9830Sstevel@tonic-gate 9840Sstevel@tonic-gate rv = C_SignFinal(hSession, *psignature, psignaturelen); 9850Sstevel@tonic-gate 9860Sstevel@tonic-gate /* result too big to fit? Allocate a bigger buffer */ 9870Sstevel@tonic-gate if (rv == CKR_BUFFER_TOO_SMALL) { 9880Sstevel@tonic-gate *psignature = realloc(*psignature, *psignaturelen); 9890Sstevel@tonic-gate 9900Sstevel@tonic-gate if (*psignature == NULL_PTR) { 9910Sstevel@tonic-gate int err = errno; 9920Sstevel@tonic-gate cryptoerror(LOG_STDERR, 9930Sstevel@tonic-gate gettext("realloc: %s\n"), strerror(err)); 9940Sstevel@tonic-gate return (CKR_HOST_MEMORY); 9950Sstevel@tonic-gate } 9960Sstevel@tonic-gate 9970Sstevel@tonic-gate rv = C_SignFinal(hSession, *psignature, psignaturelen); 9980Sstevel@tonic-gate } 9990Sstevel@tonic-gate 10000Sstevel@tonic-gate /* There was a read error */ 10010Sstevel@tonic-gate if (nread == -1) { 10020Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("error reading file: %s"), 1003*5051Swyllys strerror(saved_errno)); 10040Sstevel@tonic-gate return (CKR_GENERAL_ERROR); 10050Sstevel@tonic-gate } else { 10060Sstevel@tonic-gate return (rv); 10070Sstevel@tonic-gate } 10080Sstevel@tonic-gate } 10090Sstevel@tonic-gate 10100Sstevel@tonic-gate 10110Sstevel@tonic-gate /* 10120Sstevel@tonic-gate * getkey - gets keydata from file specified 10130Sstevel@tonic-gate * 10140Sstevel@tonic-gate * filename - name of file, if null, prompt for pass phrase 10150Sstevel@tonic-gate * pkeydata - binary key data is returned in this buf 10160Sstevel@tonic-gate * 10170Sstevel@tonic-gate * returns length of key, or -1 if error 10180Sstevel@tonic-gate */ 10190Sstevel@tonic-gate static int 10200Sstevel@tonic-gate getkey(char *filename, CK_BYTE_PTR *pkeydata) 10210Sstevel@tonic-gate { 10220Sstevel@tonic-gate struct stat statbuf; 10230Sstevel@tonic-gate char *keybuf = NULL; 10240Sstevel@tonic-gate char *tmpbuf; 10250Sstevel@tonic-gate int keylen; 10260Sstevel@tonic-gate int fd; 10270Sstevel@tonic-gate 10280Sstevel@tonic-gate if (filename != NULL) { 10290Sstevel@tonic-gate 10300Sstevel@tonic-gate /* read the key file into a buffer */ 10310Sstevel@tonic-gate if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) == -1) { 10320Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 1033*5051Swyllys "can't open %s\n"), filename); 10340Sstevel@tonic-gate return (-1); 10350Sstevel@tonic-gate 10360Sstevel@tonic-gate } 10370Sstevel@tonic-gate 10380Sstevel@tonic-gate if (fstat(fd, &statbuf) == -1) { 10390Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 1040*5051Swyllys "can't stat %s\n"), filename); 10410Sstevel@tonic-gate (void) close(fd); 10420Sstevel@tonic-gate return (-1); 10430Sstevel@tonic-gate } 10440Sstevel@tonic-gate 1045871Scasper if (!S_ISREG(statbuf.st_mode)) { 10460Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 1047*5051Swyllys "%s not a regular file\n"), filename); 10480Sstevel@tonic-gate (void) close(fd); 10490Sstevel@tonic-gate return (-1); 10500Sstevel@tonic-gate } 10510Sstevel@tonic-gate 10520Sstevel@tonic-gate keylen = (size_t)statbuf.st_size; 10530Sstevel@tonic-gate 10540Sstevel@tonic-gate if (keylen > 0) { 10550Sstevel@tonic-gate /* allocate a buffer to hold the entire key */ 10560Sstevel@tonic-gate if ((keybuf = malloc(keylen)) == NULL) { 10570Sstevel@tonic-gate int err = errno; 10580Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s\n"), 10590Sstevel@tonic-gate strerror(err)); 10600Sstevel@tonic-gate (void) close(fd); 10610Sstevel@tonic-gate return (-1); 10620Sstevel@tonic-gate } 10630Sstevel@tonic-gate 10640Sstevel@tonic-gate if (read(fd, keybuf, keylen) != keylen) { 10650Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 1066*5051Swyllys "can't read %s\n"), filename); 10670Sstevel@tonic-gate (void) close(fd); 10680Sstevel@tonic-gate return (-1); 10690Sstevel@tonic-gate } 10700Sstevel@tonic-gate } 10710Sstevel@tonic-gate (void) close(fd); 10720Sstevel@tonic-gate 10730Sstevel@tonic-gate } else { 10740Sstevel@tonic-gate 10750Sstevel@tonic-gate /* No file, prompt for a pass phrase */ 10760Sstevel@tonic-gate tmpbuf = getpassphrase(gettext("Enter key:")); 10770Sstevel@tonic-gate 10780Sstevel@tonic-gate if (tmpbuf == NULL) { 10790Sstevel@tonic-gate return (-1); /* error */ 10800Sstevel@tonic-gate } else { 10810Sstevel@tonic-gate keybuf = strdup(tmpbuf); 10820Sstevel@tonic-gate (void) memset(tmpbuf, 0, strlen(tmpbuf)); 10830Sstevel@tonic-gate } 10840Sstevel@tonic-gate keylen = strlen(keybuf); 10850Sstevel@tonic-gate } 10860Sstevel@tonic-gate 10870Sstevel@tonic-gate *pkeydata = (CK_BYTE_PTR)keybuf; 10880Sstevel@tonic-gate 10890Sstevel@tonic-gate return (keylen); 10900Sstevel@tonic-gate } 10913812Shylee 10923812Shylee static int 10933812Shylee getpasswd(char *token_spec, CK_BYTE_PTR *pdata, CK_ULONG *psize) 10943812Shylee { 10953812Shylee char *databuf; 10963812Shylee char *tmpbuf; 10973812Shylee char prompt[1024]; 10983812Shylee 10993812Shylee if (token_spec == NULL) 11003812Shylee return (-1); 11013812Shylee 11023812Shylee (void) snprintf(prompt, sizeof (prompt), DEFAULT_TOKEN_PROMPT, 11033812Shylee token_spec); 11043812Shylee tmpbuf = getpassphrase(gettext(prompt)); 11053812Shylee 11063812Shylee if (tmpbuf == NULL) { 11073812Shylee return (-1); /* error */ 11083812Shylee } 11093812Shylee 11103812Shylee databuf = strdup(tmpbuf); 11113812Shylee (void) memset(tmpbuf, 0, strlen(tmpbuf)); 11123812Shylee if (databuf == NULL) 11133812Shylee return (-1); 11143812Shylee 11153812Shylee *pdata = (CK_BYTE_PTR)databuf; 11163812Shylee *psize = (CK_ULONG)strlen(databuf); 11173812Shylee 11183812Shylee return (0); 11193812Shylee } 1120