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 */ 211142Sjk115741 /* Portions Copyright 2005 Richard Lowe */ 220Sstevel@tonic-gate /* 233812Shylee * Copyright 2007 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 * decrypt.c 310Sstevel@tonic-gate * 320Sstevel@tonic-gate * Implements encrypt(1) and decrypt(1) commands 330Sstevel@tonic-gate * 340Sstevel@tonic-gate * One binary performs both encrypt/decrypt operation. 350Sstevel@tonic-gate * 360Sstevel@tonic-gate * usage: 370Sstevel@tonic-gate * 380Sstevel@tonic-gate * algorithm - mechanism name without CKM_ prefix. Case 390Sstevel@tonic-gate * does not matter 400Sstevel@tonic-gate * keyfile - file containing key data. If not specified user is 410Sstevel@tonic-gate * prompted to enter key. key length > 0 is required 420Sstevel@tonic-gate * infile - input file to encrypt/decrypt. If omitted, stdin used. 430Sstevel@tonic-gate * outfile - output file to encrypt/decrypt. If omitted, stdout used. 440Sstevel@tonic-gate * if infile & outfile are same, a temp file is used for 450Sstevel@tonic-gate * output and infile is replaced with this file after 460Sstevel@tonic-gate * operation is complete. 470Sstevel@tonic-gate * 480Sstevel@tonic-gate * Implementation notes: 490Sstevel@tonic-gate * iv data - It is generated by random bytes equal to one block size. 500Sstevel@tonic-gate * 510Sstevel@tonic-gate * encrypted output format - 520Sstevel@tonic-gate * - Output format version number - 4 bytes in network byte order. 530Sstevel@tonic-gate * - Iterations used in key gen function, 4 bytes in network byte order. 540Sstevel@tonic-gate * - IV ( 'ivlen' bytes) 550Sstevel@tonic-gate * - Salt data used in key gen (16 bytes) 560Sstevel@tonic-gate * - cipher text data. 570Sstevel@tonic-gate * 580Sstevel@tonic-gate */ 590Sstevel@tonic-gate 600Sstevel@tonic-gate #include <stdio.h> 610Sstevel@tonic-gate #include <stdlib.h> 620Sstevel@tonic-gate #include <unistd.h> 630Sstevel@tonic-gate #include <errno.h> 640Sstevel@tonic-gate #include <fcntl.h> 650Sstevel@tonic-gate #include <ctype.h> 660Sstevel@tonic-gate #include <strings.h> 670Sstevel@tonic-gate #include <libintl.h> 680Sstevel@tonic-gate #include <libgen.h> 690Sstevel@tonic-gate #include <locale.h> 700Sstevel@tonic-gate #include <limits.h> 710Sstevel@tonic-gate #include <sys/types.h> 720Sstevel@tonic-gate #include <sys/stat.h> 730Sstevel@tonic-gate #include <netinet/in.h> 740Sstevel@tonic-gate #include <security/cryptoki.h> 750Sstevel@tonic-gate #include <cryptoutil.h> 763812Shylee #include <kmfapi.h> 770Sstevel@tonic-gate 780Sstevel@tonic-gate #define BUFFERSIZE (2048) /* Buffer size for reading file */ 790Sstevel@tonic-gate #define BLOCKSIZE (128) /* Largest guess for block size */ 800Sstevel@tonic-gate #define PROGRESSSIZE (BUFFERSIZE*20) /* stdin progress indicator size */ 810Sstevel@tonic-gate 820Sstevel@tonic-gate #define PBKD2_ITERATIONS (1000) 830Sstevel@tonic-gate #define PBKD2_SALT_SIZE 16 840Sstevel@tonic-gate 850Sstevel@tonic-gate #define SUNW_ENCRYPT_FILE_VERSION 1 860Sstevel@tonic-gate 870Sstevel@tonic-gate /* 880Sstevel@tonic-gate * Exit Status codes 890Sstevel@tonic-gate */ 900Sstevel@tonic-gate #ifndef EXIT_SUCCESS 910Sstevel@tonic-gate #define EXIT_SUCCESS 0 /* No errors */ 920Sstevel@tonic-gate #define EXIT_FAILURE 1 /* All errors except usage */ 930Sstevel@tonic-gate #endif /* EXIT_SUCCESS */ 940Sstevel@tonic-gate 950Sstevel@tonic-gate #define EXIT_USAGE 2 /* usage/syntax error */ 960Sstevel@tonic-gate 970Sstevel@tonic-gate #define RANDOM_DEVICE "/dev/urandom" /* random device name */ 980Sstevel@tonic-gate 990Sstevel@tonic-gate #define ENCRYPT_NAME "encrypt" /* name of encrypt command */ 1003812Shylee #define ENCRYPT_OPTIONS "a:T:K:k:i:o:lv" /* options for encrypt */ 1010Sstevel@tonic-gate #define DECRYPT_NAME "decrypt" /* name of decrypt command */ 1023812Shylee #define DECRYPT_OPTIONS "a:T:K:k:i:o:lv" /* options for decrypt */ 1033812Shylee #define DEFAULT_TOKEN_PROMPT "Enter PIN for %s: " 1043812Shylee #define PK_DEFAULT_PK11TOKEN SOFT_TOKEN_LABEL 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate /* 1070Sstevel@tonic-gate * Structure containing info for encrypt/decrypt 1080Sstevel@tonic-gate * command 1090Sstevel@tonic-gate */ 1100Sstevel@tonic-gate struct CommandInfo { 1110Sstevel@tonic-gate char *name; /* name of the command */ 1120Sstevel@tonic-gate char *options; /* command line options */ 1130Sstevel@tonic-gate CK_FLAGS flags; 1140Sstevel@tonic-gate CK_ATTRIBUTE_TYPE type; /* type of command */ 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate /* function pointers for various operations */ 1170Sstevel@tonic-gate CK_RV (*Init)(CK_SESSION_HANDLE, CK_MECHANISM_PTR, CK_OBJECT_HANDLE); 1180Sstevel@tonic-gate CK_RV (*Update)(CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG, CK_BYTE_PTR, 1190Sstevel@tonic-gate CK_ULONG_PTR); 1200Sstevel@tonic-gate CK_RV (*Crypt)(CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG, CK_BYTE_PTR, 1210Sstevel@tonic-gate CK_ULONG_PTR); 1220Sstevel@tonic-gate CK_RV (*Final)(CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG_PTR); 1230Sstevel@tonic-gate }; 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate static struct CommandInfo encrypt_cmd = { 1260Sstevel@tonic-gate ENCRYPT_NAME, 1270Sstevel@tonic-gate ENCRYPT_OPTIONS, 1280Sstevel@tonic-gate CKF_ENCRYPT, 1290Sstevel@tonic-gate CKA_ENCRYPT, 1300Sstevel@tonic-gate C_EncryptInit, 1310Sstevel@tonic-gate C_EncryptUpdate, 1320Sstevel@tonic-gate C_Encrypt, 1330Sstevel@tonic-gate C_EncryptFinal 1340Sstevel@tonic-gate }; 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate static struct CommandInfo decrypt_cmd = { 1370Sstevel@tonic-gate DECRYPT_NAME, 1380Sstevel@tonic-gate DECRYPT_OPTIONS, 1390Sstevel@tonic-gate CKF_DECRYPT, 1400Sstevel@tonic-gate CKA_DECRYPT, 1410Sstevel@tonic-gate C_DecryptInit, 1420Sstevel@tonic-gate C_DecryptUpdate, 1430Sstevel@tonic-gate C_Decrypt, 1440Sstevel@tonic-gate C_DecryptFinal 1450Sstevel@tonic-gate }; 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate struct mech_alias { 1480Sstevel@tonic-gate CK_MECHANISM_TYPE type; 1490Sstevel@tonic-gate char *alias; 1500Sstevel@tonic-gate CK_ULONG keysize_min; 1510Sstevel@tonic-gate CK_ULONG keysize_max; 1520Sstevel@tonic-gate int keysize_unit; 1530Sstevel@tonic-gate int ivlen; 1540Sstevel@tonic-gate boolean_t available; 1550Sstevel@tonic-gate }; 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate #define MECH_ALIASES_COUNT 4 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate static struct mech_alias mech_aliases[] = { 1600Sstevel@tonic-gate { CKM_AES_CBC_PAD, "aes", ULONG_MAX, 0L, 8, 16, B_FALSE }, 1610Sstevel@tonic-gate { CKM_RC4, "arcfour", ULONG_MAX, 0L, 1, 0, B_FALSE }, 1620Sstevel@tonic-gate { CKM_DES_CBC_PAD, "des", 8, 8, 8, 8, B_FALSE }, 1630Sstevel@tonic-gate { CKM_DES3_CBC_PAD, "3des", 24, 24, 8, 8, B_FALSE }, 1640Sstevel@tonic-gate }; 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate static CK_BBOOL truevalue = TRUE; 1670Sstevel@tonic-gate static CK_BBOOL falsevalue = FALSE; 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate static boolean_t aflag = B_FALSE; /* -a <algorithm> flag, required */ 1700Sstevel@tonic-gate static boolean_t kflag = B_FALSE; /* -k <keyfile> flag */ 1710Sstevel@tonic-gate static boolean_t iflag = B_FALSE; /* -i <infile> flag, use stdin if absent */ 1720Sstevel@tonic-gate static boolean_t oflag = B_FALSE; /* -o <outfile> flag, use stdout if absent */ 1730Sstevel@tonic-gate static boolean_t lflag = B_FALSE; /* -l flag (list) */ 1740Sstevel@tonic-gate static boolean_t vflag = B_FALSE; /* -v flag (verbose) */ 1753812Shylee static boolean_t Tflag = B_FALSE; 1763812Shylee static boolean_t Kflag = B_FALSE; 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate static char *keyfile = NULL; /* name of keyfile */ 1790Sstevel@tonic-gate static char *inputfile = NULL; /* name of input file */ 1800Sstevel@tonic-gate static char *outputfile = NULL; /* name of output file */ 1813812Shylee static char *token_label = NULL; 1823812Shylee static char *key_label = NULL; 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate static int status_pos = 0; /* current position of progress bar element */ 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate /* 1870Sstevel@tonic-gate * function prototypes 1880Sstevel@tonic-gate */ 1890Sstevel@tonic-gate static void usage(struct CommandInfo *cmd); 1900Sstevel@tonic-gate static int execute_cmd(struct CommandInfo *cmd, char *algo_str); 1913812Shylee static int cryptogetdata(char *, CK_BYTE_PTR *pkeydata, CK_ULONG_PTR pkeysize); 1920Sstevel@tonic-gate static int cryptoreadfile(char *filename, CK_BYTE_PTR *pdata, 1930Sstevel@tonic-gate CK_ULONG_PTR pdatalen); 1940Sstevel@tonic-gate static int get_random_data(CK_BYTE_PTR pivbuf, int ivlen); 1950Sstevel@tonic-gate static int crypt_multipart(struct CommandInfo *cmd, CK_SESSION_HANDLE hSession, 1961142Sjk115741 int infd, int outfd, off_t insize); 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate int 1990Sstevel@tonic-gate main(int argc, char **argv) 2000Sstevel@tonic-gate { 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate extern char *optarg; 2030Sstevel@tonic-gate extern int optind; 2040Sstevel@tonic-gate char *optstr; 2050Sstevel@tonic-gate char c; /* current getopts flag */ 2060Sstevel@tonic-gate char *algo_str = NULL; /* algorithm string */ 2070Sstevel@tonic-gate struct CommandInfo *cmd; 2080Sstevel@tonic-gate char *cmdname; /* name of command */ 2090Sstevel@tonic-gate boolean_t errflag = B_FALSE; 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 2120Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defiend by cc -D */ 2130Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 2140Sstevel@tonic-gate #endif 2150Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate /* 2180Sstevel@tonic-gate * Based on command name, determine 2190Sstevel@tonic-gate * type of command. 2200Sstevel@tonic-gate */ 2210Sstevel@tonic-gate cmdname = basename(argv[0]); 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate cryptodebug_init(cmdname); 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate if (strcmp(cmdname, encrypt_cmd.name) == 0) { 2260Sstevel@tonic-gate cmd = &encrypt_cmd; 2270Sstevel@tonic-gate } else if (strcmp(cmdname, decrypt_cmd.name) == 0) { 2280Sstevel@tonic-gate cmd = &decrypt_cmd; 2290Sstevel@tonic-gate } else { 2300Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 2310Sstevel@tonic-gate "command name must be either encrypt or decrypt")); 2320Sstevel@tonic-gate exit(EXIT_USAGE); 2330Sstevel@tonic-gate } 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate optstr = cmd->options; 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate /* Parse command line arguments */ 2380Sstevel@tonic-gate while (!errflag && (c = getopt(argc, argv, optstr)) != -1) { 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate switch (c) { 2410Sstevel@tonic-gate case 'a': 2420Sstevel@tonic-gate aflag = B_TRUE; 2430Sstevel@tonic-gate algo_str = optarg; 2440Sstevel@tonic-gate break; 2450Sstevel@tonic-gate case 'k': 2460Sstevel@tonic-gate kflag = B_TRUE; 2470Sstevel@tonic-gate keyfile = optarg; 2480Sstevel@tonic-gate break; 2493812Shylee case 'T': 2503812Shylee Tflag = B_TRUE; 2513812Shylee token_label = optarg; 2523812Shylee break; 2533812Shylee case 'K': 2543812Shylee Kflag = B_TRUE; 2553812Shylee key_label = optarg; 2563812Shylee break; 2570Sstevel@tonic-gate case 'i': 2580Sstevel@tonic-gate iflag = B_TRUE; 2590Sstevel@tonic-gate inputfile = optarg; 2600Sstevel@tonic-gate break; 2610Sstevel@tonic-gate case 'o': 2620Sstevel@tonic-gate oflag = B_TRUE; 2630Sstevel@tonic-gate outputfile = optarg; 2640Sstevel@tonic-gate break; 2650Sstevel@tonic-gate case 'l': 2660Sstevel@tonic-gate lflag = B_TRUE; 2670Sstevel@tonic-gate break; 2680Sstevel@tonic-gate case 'v': 2690Sstevel@tonic-gate vflag = B_TRUE; 2700Sstevel@tonic-gate break; 2710Sstevel@tonic-gate default: 2720Sstevel@tonic-gate errflag = B_TRUE; 2730Sstevel@tonic-gate } 2740Sstevel@tonic-gate } 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate if (errflag || (!aflag && !lflag) || (lflag && argc > 2) || 2773812Shylee (kflag && Kflag) || (Tflag && !Kflag) || 2780Sstevel@tonic-gate (optind < argc)) { 2790Sstevel@tonic-gate usage(cmd); 2800Sstevel@tonic-gate exit(EXIT_USAGE); 2810Sstevel@tonic-gate } 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate return (execute_cmd(cmd, algo_str)); 2840Sstevel@tonic-gate } 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate /* 2870Sstevel@tonic-gate * usage message 2880Sstevel@tonic-gate */ 2890Sstevel@tonic-gate static void 2900Sstevel@tonic-gate usage(struct CommandInfo *cmd) 2910Sstevel@tonic-gate { 2923812Shylee (void) fprintf(stderr, gettext("Usage:\n")); 2930Sstevel@tonic-gate if (cmd->type == CKA_ENCRYPT) { 2943812Shylee (void) fprintf(stderr, gettext(" encrypt -l\n")); 2953812Shylee (void) fprintf(stderr, gettext(" encrypt -a <algorithm> " 2963812Shylee "[-v] [-k <keyfile> | -K <keylabel> [-T <tokenspec>]] " 2973812Shylee "[-i <infile>] [-o <outfile>]\n")); 2983812Shylee 2990Sstevel@tonic-gate } else { 3003812Shylee (void) fprintf(stderr, gettext(" decrypt -l\n")); 3013812Shylee (void) fprintf(stderr, gettext(" decrypt -a <algorithm> " 3023812Shylee "[-v] [-k <keyfile> | -K <keylabel> [-T <tokenspec>]] " 3033812Shylee "[-i <infile>] [-o <outfile>]\n")); 3040Sstevel@tonic-gate } 3050Sstevel@tonic-gate } 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate /* 3080Sstevel@tonic-gate * Print out list of algorithms in default and verbose mode 3090Sstevel@tonic-gate */ 3100Sstevel@tonic-gate static void 3110Sstevel@tonic-gate algorithm_list() 3120Sstevel@tonic-gate { 3130Sstevel@tonic-gate int mech; 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate (void) printf(gettext("Algorithm Keysize: Min Max (bits)\n" 3160Sstevel@tonic-gate "------------------------------------------\n")); 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate for (mech = 0; mech < MECH_ALIASES_COUNT; mech++) { 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate if (mech_aliases[mech].available == B_FALSE) 3210Sstevel@tonic-gate continue; 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate (void) printf("%-15s", mech_aliases[mech].alias); 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate if (mech_aliases[mech].keysize_min != UINT_MAX && 3260Sstevel@tonic-gate mech_aliases[mech].keysize_max != 0) 3270Sstevel@tonic-gate (void) printf(" %5lu %5lu\n", 3280Sstevel@tonic-gate (mech_aliases[mech].keysize_min * 329*5051Swyllys mech_aliases[mech].keysize_unit), 3300Sstevel@tonic-gate (mech_aliases[mech].keysize_max * 331*5051Swyllys mech_aliases[mech].keysize_unit)); 3320Sstevel@tonic-gate else 3330Sstevel@tonic-gate (void) printf("\n"); 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate } 3360Sstevel@tonic-gate } 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate static CK_RV 3390Sstevel@tonic-gate generate_pkcs5_key(CK_SESSION_HANDLE hSession, 3400Sstevel@tonic-gate CK_BYTE *pSaltData, 3410Sstevel@tonic-gate CK_ULONG saltLen, 3420Sstevel@tonic-gate CK_ULONG iterations, 3430Sstevel@tonic-gate CK_BYTE *pkeydata, /* user entered passphrase */ 3440Sstevel@tonic-gate CK_KEY_TYPE keytype, 3450Sstevel@tonic-gate CK_ULONG passwd_size, 3460Sstevel@tonic-gate CK_ULONG keylen, /* desired length of generated key */ 3470Sstevel@tonic-gate CK_ATTRIBUTE_TYPE operation, 3480Sstevel@tonic-gate CK_OBJECT_HANDLE *hKey) 3490Sstevel@tonic-gate { 3500Sstevel@tonic-gate CK_RV rv; 3510Sstevel@tonic-gate CK_PKCS5_PBKD2_PARAMS params; 3520Sstevel@tonic-gate CK_MECHANISM mechanism; 3530Sstevel@tonic-gate CK_OBJECT_CLASS class = CKO_SECRET_KEY; 3540Sstevel@tonic-gate CK_ATTRIBUTE tmpl[4]; 3550Sstevel@tonic-gate int attrs = 0; 3560Sstevel@tonic-gate 3570Sstevel@tonic-gate mechanism.mechanism = CKM_PKCS5_PBKD2; 3580Sstevel@tonic-gate mechanism.pParameter = ¶ms; 3590Sstevel@tonic-gate mechanism.ulParameterLen = sizeof (params); 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate tmpl[attrs].type = CKA_CLASS; 3620Sstevel@tonic-gate tmpl[attrs].pValue = &class; 3630Sstevel@tonic-gate tmpl[attrs].ulValueLen = sizeof (class); 3640Sstevel@tonic-gate attrs++; 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate tmpl[attrs].type = CKA_KEY_TYPE; 3670Sstevel@tonic-gate tmpl[attrs].pValue = &keytype; 3680Sstevel@tonic-gate tmpl[attrs].ulValueLen = sizeof (keytype); 3690Sstevel@tonic-gate attrs++; 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate tmpl[attrs].type = operation; 3720Sstevel@tonic-gate tmpl[attrs].pValue = &truevalue; 3730Sstevel@tonic-gate tmpl[attrs].ulValueLen = sizeof (CK_BBOOL); 3740Sstevel@tonic-gate attrs++; 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate if (keylen > 0) { 3770Sstevel@tonic-gate tmpl[attrs].type = CKA_VALUE_LEN; 3780Sstevel@tonic-gate tmpl[attrs].pValue = &keylen; 3790Sstevel@tonic-gate tmpl[attrs].ulValueLen = sizeof (keylen); 3800Sstevel@tonic-gate attrs++; 3810Sstevel@tonic-gate } 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate params.saltSource = CKZ_SALT_SPECIFIED; 3840Sstevel@tonic-gate params.pSaltSourceData = (void *)pSaltData; 3850Sstevel@tonic-gate params.ulSaltSourceDataLen = saltLen; 3860Sstevel@tonic-gate params.iterations = iterations; 3870Sstevel@tonic-gate params.prf = CKP_PKCS5_PBKD2_HMAC_SHA1; 3880Sstevel@tonic-gate params.pPrfData = NULL; 3890Sstevel@tonic-gate params.ulPrfDataLen = 0; 3900Sstevel@tonic-gate params.pPassword = (CK_UTF8CHAR_PTR)pkeydata; 3910Sstevel@tonic-gate params.ulPasswordLen = &passwd_size; 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate mechanism.mechanism = CKM_PKCS5_PBKD2; 3940Sstevel@tonic-gate mechanism.pParameter = ¶ms; 3950Sstevel@tonic-gate mechanism.ulParameterLen = sizeof (params); 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate rv = C_GenerateKey(hSession, &mechanism, tmpl, 398*5051Swyllys attrs, hKey); 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate return (rv); 4010Sstevel@tonic-gate } 4020Sstevel@tonic-gate 4033812Shylee /* 4043812Shylee * This function will login into the token with the provided password and 4053812Shylee * find the token key object with the specified keytype and keylabel. 4063812Shylee */ 4073812Shylee static int 4083812Shylee get_token_key(CK_SESSION_HANDLE hSession, CK_KEY_TYPE keytype, 4093812Shylee char *keylabel, CK_BYTE *password, int password_len, 4103812Shylee CK_OBJECT_HANDLE *keyobj) 4113812Shylee { 4123812Shylee CK_RV rv; 4133812Shylee CK_ATTRIBUTE pTmpl[10]; 4143812Shylee CK_OBJECT_CLASS class = CKO_SECRET_KEY; 4153812Shylee CK_BBOOL true = 1; 4163812Shylee CK_BBOOL is_token = 1; 4173812Shylee CK_ULONG key_obj_count = 1; 4183812Shylee int i; 4193812Shylee CK_KEY_TYPE ckKeyType = keytype; 4203812Shylee 4213812Shylee 4223812Shylee rv = C_Login(hSession, CKU_USER, (CK_UTF8CHAR_PTR)password, 4233812Shylee (CK_ULONG)password_len); 4243812Shylee if (rv != CKR_OK) { 4253812Shylee (void) fprintf(stderr, "Cannot login to the token." 4263812Shylee " error = %s\n", pkcs11_strerror(rv)); 4273812Shylee return (-1); 4283812Shylee } 4293812Shylee 4303812Shylee i = 0; 4313812Shylee pTmpl[i].type = CKA_TOKEN; 4323812Shylee pTmpl[i].pValue = &is_token; 4333812Shylee pTmpl[i].ulValueLen = sizeof (CK_BBOOL); 4343812Shylee i++; 4353812Shylee 4363812Shylee pTmpl[i].type = CKA_CLASS; 4373812Shylee pTmpl[i].pValue = &class; 4383812Shylee pTmpl[i].ulValueLen = sizeof (class); 4393812Shylee i++; 4403812Shylee 4413812Shylee pTmpl[i].type = CKA_LABEL; 4423812Shylee pTmpl[i].pValue = keylabel; 4433812Shylee pTmpl[i].ulValueLen = strlen(keylabel); 4443812Shylee i++; 4453812Shylee 4463812Shylee pTmpl[i].type = CKA_KEY_TYPE; 4473812Shylee pTmpl[i].pValue = &ckKeyType; 4483812Shylee pTmpl[i].ulValueLen = sizeof (ckKeyType); 4493812Shylee i++; 4503812Shylee 4513812Shylee pTmpl[i].type = CKA_PRIVATE; 4523812Shylee pTmpl[i].pValue = &true; 4533812Shylee pTmpl[i].ulValueLen = sizeof (true); 4543812Shylee i++; 4553812Shylee 4563812Shylee rv = C_FindObjectsInit(hSession, pTmpl, i); 4573812Shylee if (rv != CKR_OK) { 4583812Shylee goto out; 4593812Shylee } 4603812Shylee 4613812Shylee rv = C_FindObjects(hSession, keyobj, 1, &key_obj_count); 4623812Shylee 4633812Shylee (void) C_FindObjectsFinal(hSession); 4643812Shylee 4653812Shylee out: 4663812Shylee if (rv != CKR_OK) { 4673812Shylee (void) fprintf(stderr, 4683812Shylee "Cannot retrieve key object. error = %s\n", 4693812Shylee pkcs11_strerror(rv)); 4703812Shylee return (-1); 4713812Shylee } 4723812Shylee 4733812Shylee if (key_obj_count == 0) { 4743812Shylee (void) fprintf(stderr, "Cannot find the key object.\n"); 4753812Shylee return (-1); 4763812Shylee } 4773812Shylee 4783812Shylee return (0); 4793812Shylee } 4803812Shylee 4810Sstevel@tonic-gate 4820Sstevel@tonic-gate /* 4830Sstevel@tonic-gate * Execute the command. 4840Sstevel@tonic-gate * cmd - command pointing to type of operation. 4850Sstevel@tonic-gate * algo_str - alias of the algorithm passed. 4860Sstevel@tonic-gate */ 4870Sstevel@tonic-gate static int 4880Sstevel@tonic-gate execute_cmd(struct CommandInfo *cmd, char *algo_str) 4890Sstevel@tonic-gate { 4900Sstevel@tonic-gate CK_RV rv; 4910Sstevel@tonic-gate CK_ULONG slotcount; 4920Sstevel@tonic-gate CK_SLOT_ID slotID; 4930Sstevel@tonic-gate CK_SLOT_ID_PTR pSlotList = NULL; 4940Sstevel@tonic-gate CK_MECHANISM_TYPE mech_type = 0; 4950Sstevel@tonic-gate CK_MECHANISM_INFO info, kg_info; 4960Sstevel@tonic-gate CK_MECHANISM mech; 4970Sstevel@tonic-gate CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE; 4980Sstevel@tonic-gate CK_BYTE_PTR pkeydata = NULL; 4990Sstevel@tonic-gate CK_BYTE salt[PBKD2_SALT_SIZE]; 5000Sstevel@tonic-gate CK_ULONG keysize = 0; 5010Sstevel@tonic-gate int i, slot, mek; /* index variables */ 5020Sstevel@tonic-gate int status; 5030Sstevel@tonic-gate struct stat insbuf; /* stat buf for infile */ 5040Sstevel@tonic-gate struct stat outsbuf; /* stat buf for outfile */ 5050Sstevel@tonic-gate char tmpnam[PATH_MAX]; /* tmp file name */ 5060Sstevel@tonic-gate CK_OBJECT_HANDLE key = (CK_OBJECT_HANDLE) 0; 5070Sstevel@tonic-gate int infd = 0; /* input file, stdin default */ 5080Sstevel@tonic-gate int outfd = 1; /* output file, stdout default */ 5090Sstevel@tonic-gate char *outfilename = NULL; 5100Sstevel@tonic-gate boolean_t errflag = B_TRUE; 5110Sstevel@tonic-gate boolean_t inoutsame = B_FALSE; /* if both input & output are same */ 5120Sstevel@tonic-gate CK_BYTE_PTR pivbuf = NULL_PTR; 5130Sstevel@tonic-gate CK_ULONG ivlen = 0L; 5140Sstevel@tonic-gate int mech_match = 0; 5150Sstevel@tonic-gate CK_ULONG iterations = PBKD2_ITERATIONS; 5160Sstevel@tonic-gate CK_ULONG keylen; 5170Sstevel@tonic-gate int version = SUNW_ENCRYPT_FILE_VERSION; 5180Sstevel@tonic-gate CK_KEY_TYPE keytype; 5193812Shylee KMF_RETURN kmfrv; 5203812Shylee CK_SLOT_ID token_slot_id; 5210Sstevel@tonic-gate 5220Sstevel@tonic-gate if (aflag) { 5230Sstevel@tonic-gate /* Determine if algorithm is valid */ 5240Sstevel@tonic-gate for (mech_match = 0; mech_match < MECH_ALIASES_COUNT; 525*5051Swyllys mech_match++) { 5260Sstevel@tonic-gate if (strcmp(algo_str, 5270Sstevel@tonic-gate mech_aliases[mech_match].alias) == 0) { 5280Sstevel@tonic-gate mech_type = mech_aliases[mech_match].type; 5290Sstevel@tonic-gate break; 5300Sstevel@tonic-gate } 5310Sstevel@tonic-gate } 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate if (mech_match == MECH_ALIASES_COUNT) { 5340Sstevel@tonic-gate cryptoerror(LOG_STDERR, 5350Sstevel@tonic-gate gettext("unknown algorithm -- %s"), algo_str); 5360Sstevel@tonic-gate return (EXIT_FAILURE); 5370Sstevel@tonic-gate } 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate /* 5403812Shylee * Process keyfile or get the token pin if -K is specified. 5410Sstevel@tonic-gate * 5420Sstevel@tonic-gate * If a keyfile is provided, get the key data from 5430Sstevel@tonic-gate * the file. Otherwise, prompt for a passphrase. The 5440Sstevel@tonic-gate * passphrase is used as the key data. 5450Sstevel@tonic-gate */ 5463812Shylee if (Kflag) { 5473812Shylee /* get the pin of the token */ 5483812Shylee if (token_label == NULL || !strlen(token_label)) { 5493812Shylee token_label = PK_DEFAULT_PK11TOKEN; 5503812Shylee } 5513812Shylee 5523812Shylee status = cryptogetdata(token_label, &pkeydata, 5533812Shylee &keysize); 5543812Shylee } else if (kflag) { 5553812Shylee /* get the key file */ 5560Sstevel@tonic-gate status = cryptoreadfile(keyfile, &pkeydata, &keysize); 5570Sstevel@tonic-gate } else { 5583812Shylee /* get the key from input */ 5593812Shylee status = cryptogetdata(NULL, &pkeydata, &keysize); 5600Sstevel@tonic-gate } 5610Sstevel@tonic-gate 5620Sstevel@tonic-gate if (status == -1 || keysize == 0L) { 5633812Shylee cryptoerror(LOG_STDERR, 5643812Shylee Kflag ? gettext("invalid password.") : 5653812Shylee gettext("invalid key.")); 5660Sstevel@tonic-gate return (EXIT_FAILURE); 5670Sstevel@tonic-gate } 5680Sstevel@tonic-gate } 5690Sstevel@tonic-gate 5700Sstevel@tonic-gate bzero(salt, sizeof (salt)); 5710Sstevel@tonic-gate /* Initialize pkcs */ 5723812Shylee rv = C_Initialize(NULL); 5733812Shylee if (rv != CKR_OK && rv != CKR_CRYPTOKI_ALREADY_INITIALIZED) { 5740Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("failed to initialize " 5750Sstevel@tonic-gate "PKCS #11 framework: %s"), pkcs11_strerror(rv)); 5760Sstevel@tonic-gate goto cleanup; 5770Sstevel@tonic-gate } 5780Sstevel@tonic-gate 5790Sstevel@tonic-gate /* Get slot count */ 5800Sstevel@tonic-gate rv = C_GetSlotList(0, NULL_PTR, &slotcount); 5810Sstevel@tonic-gate if (rv != CKR_OK || slotcount == 0) { 5820Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 5830Sstevel@tonic-gate "failed to find any cryptographic provider," 5840Sstevel@tonic-gate "please check with your system administrator: %s"), 5850Sstevel@tonic-gate pkcs11_strerror(rv)); 5860Sstevel@tonic-gate goto cleanup; 5870Sstevel@tonic-gate } 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate /* Found at least one slot, allocate memory for slot list */ 5900Sstevel@tonic-gate pSlotList = malloc(slotcount * sizeof (CK_SLOT_ID)); 5910Sstevel@tonic-gate if (pSlotList == NULL_PTR) { 5920Sstevel@tonic-gate int err = errno; 5930Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s"), strerror(err)); 5940Sstevel@tonic-gate goto cleanup; 5950Sstevel@tonic-gate } 5960Sstevel@tonic-gate 5970Sstevel@tonic-gate /* Get the list of slots */ 5980Sstevel@tonic-gate if ((rv = C_GetSlotList(0, pSlotList, &slotcount)) != CKR_OK) { 5990Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 6000Sstevel@tonic-gate "failed to find any cryptographic provider," 6010Sstevel@tonic-gate "please check with your system administrator: %s"), 6020Sstevel@tonic-gate pkcs11_strerror(rv)); 6030Sstevel@tonic-gate goto cleanup; 6040Sstevel@tonic-gate } 6050Sstevel@tonic-gate 6060Sstevel@tonic-gate if (lflag) { 6070Sstevel@tonic-gate 6080Sstevel@tonic-gate /* Iterate through slots */ 6090Sstevel@tonic-gate for (slot = 0; slot < slotcount; slot++) { 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate /* Iterate through each mechanism */ 6120Sstevel@tonic-gate for (mek = 0; mek < MECH_ALIASES_COUNT; mek++) { 6130Sstevel@tonic-gate rv = C_GetMechanismInfo(pSlotList[slot], 6140Sstevel@tonic-gate mech_aliases[mek].type, &info); 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate if (rv != CKR_OK) 6170Sstevel@tonic-gate continue; 6180Sstevel@tonic-gate 6190Sstevel@tonic-gate /* 6200Sstevel@tonic-gate * Set to minimum/maximum key sizes assuming 6210Sstevel@tonic-gate * the values available are not 0. 6220Sstevel@tonic-gate */ 6230Sstevel@tonic-gate if (info.ulMinKeySize && (info.ulMinKeySize < 6240Sstevel@tonic-gate mech_aliases[mek].keysize_min)) 6250Sstevel@tonic-gate mech_aliases[mek].keysize_min = 626*5051Swyllys info.ulMinKeySize; 6270Sstevel@tonic-gate 6280Sstevel@tonic-gate if (info.ulMaxKeySize && (info.ulMaxKeySize > 6290Sstevel@tonic-gate mech_aliases[mek].keysize_max)) 6300Sstevel@tonic-gate mech_aliases[mek].keysize_max = 631*5051Swyllys info.ulMaxKeySize; 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate mech_aliases[mek].available = B_TRUE; 6340Sstevel@tonic-gate } 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate } 6370Sstevel@tonic-gate 6380Sstevel@tonic-gate algorithm_list(); 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate errflag = B_FALSE; 6410Sstevel@tonic-gate goto cleanup; 6420Sstevel@tonic-gate } 6430Sstevel@tonic-gate 6443812Shylee 6453812Shylee /* 6463812Shylee * Find a slot with matching mechanism 6473812Shylee * 6483812Shylee * If -K is specified, we find the slot id for the token first, then 6493812Shylee * check if the slot supports the algorithm. 6503812Shylee */ 6513812Shylee i = 0; 6523812Shylee if (Kflag) { 653*5051Swyllys kmfrv = kmf_pk11_token_lookup(NULL, token_label, 654*5051Swyllys &token_slot_id); 6553812Shylee if (kmfrv != KMF_OK) { 6563812Shylee cryptoerror(LOG_STDERR, 6573812Shylee gettext("no matching PKCS#11 token")); 6583812Shylee errflag = B_TRUE; 6593812Shylee goto cleanup; 6603812Shylee } 6613812Shylee rv = C_GetMechanismInfo(token_slot_id, mech_type, &info); 6623812Shylee if (rv == CKR_OK && (info.flags & cmd->flags)) 6633812Shylee slotID = token_slot_id; 6643812Shylee else 6653812Shylee i = slotcount; 6663812Shylee } else { 6673812Shylee for (i = 0; i < slotcount; i++) { 6683812Shylee slotID = pSlotList[i]; 6693812Shylee rv = C_GetMechanismInfo(slotID, mech_type, &info); 6703812Shylee if (rv != CKR_OK) { 6713812Shylee continue; /* to the next slot */ 6723812Shylee } else { 6733812Shylee /* 6743812Shylee * If the slot support the crypto, also 6753812Shylee * make sure it supports the correct 6763812Shylee * key generation mech if needed. 6773812Shylee * 6783812Shylee * We need PKCS5 when RC4 is used or 6793812Shylee * when the key is entered on cmd line. 6803812Shylee */ 6813812Shylee if ((info.flags & cmd->flags) && 6823812Shylee (mech_type == CKM_RC4) || 6833812Shylee (keyfile == NULL)) { 6843812Shylee rv = C_GetMechanismInfo(slotID, 6853812Shylee CKM_PKCS5_PBKD2, &kg_info); 6863812Shylee if (rv == CKR_OK) 6873812Shylee break; 6883812Shylee } else if (info.flags & cmd->flags) { 6890Sstevel@tonic-gate break; 6903812Shylee } 6910Sstevel@tonic-gate } 6920Sstevel@tonic-gate } 6930Sstevel@tonic-gate } 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate /* Show error if no matching mechanism found */ 6960Sstevel@tonic-gate if (i == slotcount) { 6970Sstevel@tonic-gate cryptoerror(LOG_STDERR, 6980Sstevel@tonic-gate gettext("no cryptographic provider was " 6990Sstevel@tonic-gate "found for this algorithm -- %s"), algo_str); 7000Sstevel@tonic-gate goto cleanup; 7010Sstevel@tonic-gate } 7020Sstevel@tonic-gate 7030Sstevel@tonic-gate /* Open a session */ 7040Sstevel@tonic-gate rv = C_OpenSession(slotID, CKF_SERIAL_SESSION, 705*5051Swyllys NULL_PTR, NULL, &hSession); 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate if (rv != CKR_OK) { 7080Sstevel@tonic-gate cryptoerror(LOG_STDERR, 7090Sstevel@tonic-gate gettext("can not open PKCS #11 session: %s"), 7100Sstevel@tonic-gate pkcs11_strerror(rv)); 7110Sstevel@tonic-gate goto cleanup; 7120Sstevel@tonic-gate } 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate /* 7150Sstevel@tonic-gate * Generate IV data for encrypt. 7160Sstevel@tonic-gate */ 7170Sstevel@tonic-gate ivlen = mech_aliases[mech_match].ivlen; 7180Sstevel@tonic-gate if ((pivbuf = malloc((size_t)ivlen)) == NULL) { 7190Sstevel@tonic-gate int err = errno; 7200Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s"), 7210Sstevel@tonic-gate strerror(err)); 7220Sstevel@tonic-gate goto cleanup; 7230Sstevel@tonic-gate } 7240Sstevel@tonic-gate 7250Sstevel@tonic-gate if (cmd->type == CKA_ENCRYPT) { 7260Sstevel@tonic-gate if ((get_random_data(pivbuf, 7270Sstevel@tonic-gate mech_aliases[mech_match].ivlen)) != 0) { 7280Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 729*5051Swyllys "Unable to generate random " 730*5051Swyllys "data for initialization vector.")); 7310Sstevel@tonic-gate goto cleanup; 7320Sstevel@tonic-gate } 7330Sstevel@tonic-gate } 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate /* 7360Sstevel@tonic-gate * Create the key object 7370Sstevel@tonic-gate */ 7380Sstevel@tonic-gate rv = pkcs11_mech2keytype(mech_type, &keytype); 7390Sstevel@tonic-gate if (rv != CKR_OK) { 7400Sstevel@tonic-gate cryptoerror(LOG_STDERR, 741*5051Swyllys gettext("unable to find key type for algorithm.")); 7420Sstevel@tonic-gate goto cleanup; 7430Sstevel@tonic-gate } 7440Sstevel@tonic-gate 7450Sstevel@tonic-gate /* Open input file */ 7460Sstevel@tonic-gate if (iflag) { 7470Sstevel@tonic-gate if ((infd = open(inputfile, O_RDONLY | O_NONBLOCK)) == -1) { 7480Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 749*5051Swyllys "can not open input file %s"), inputfile); 7500Sstevel@tonic-gate goto cleanup; 7510Sstevel@tonic-gate } 7520Sstevel@tonic-gate 7530Sstevel@tonic-gate /* Get info on input file */ 7540Sstevel@tonic-gate if (fstat(infd, &insbuf) == -1) { 7550Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 756*5051Swyllys "can not stat input file %s"), inputfile); 7570Sstevel@tonic-gate goto cleanup; 7580Sstevel@tonic-gate } 7590Sstevel@tonic-gate } 7600Sstevel@tonic-gate 7610Sstevel@tonic-gate /* 7620Sstevel@tonic-gate * Prepare output file 7630Sstevel@tonic-gate * If the input & output file are same, 7640Sstevel@tonic-gate * the output is written to a temp 7650Sstevel@tonic-gate * file first, then renamed to the original file 7660Sstevel@tonic-gate * after the crypt operation 7670Sstevel@tonic-gate */ 7680Sstevel@tonic-gate inoutsame = B_FALSE; 7690Sstevel@tonic-gate if (oflag) { 7700Sstevel@tonic-gate outfilename = outputfile; 7710Sstevel@tonic-gate if ((stat(outputfile, &outsbuf) != -1) && 772*5051Swyllys (insbuf.st_ino == outsbuf.st_ino)) { 7730Sstevel@tonic-gate char *dir; 7740Sstevel@tonic-gate 7750Sstevel@tonic-gate /* create temp file on same dir */ 7760Sstevel@tonic-gate dir = dirname(outputfile); 7770Sstevel@tonic-gate (void) snprintf(tmpnam, sizeof (tmpnam), 778*5051Swyllys "%s/encrXXXXXX", dir); 7790Sstevel@tonic-gate outfilename = tmpnam; 7800Sstevel@tonic-gate if ((outfd = mkstemp(tmpnam)) == -1) { 7810Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 7820Sstevel@tonic-gate "cannot create temp file")); 7830Sstevel@tonic-gate goto cleanup; 7840Sstevel@tonic-gate } 7850Sstevel@tonic-gate inoutsame = B_TRUE; 7860Sstevel@tonic-gate } else { 7870Sstevel@tonic-gate /* Create file for output */ 7880Sstevel@tonic-gate if ((outfd = open(outfilename, 789*5051Swyllys O_CREAT|O_WRONLY|O_TRUNC, 0644)) == -1) { 7900Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 7910Sstevel@tonic-gate "cannot open output file %s"), 7920Sstevel@tonic-gate outfilename); 7930Sstevel@tonic-gate goto cleanup; 7940Sstevel@tonic-gate } 7950Sstevel@tonic-gate } 7960Sstevel@tonic-gate } 7970Sstevel@tonic-gate 7980Sstevel@tonic-gate /* 7990Sstevel@tonic-gate * Read the version number from the head of the file 8000Sstevel@tonic-gate * to know how to interpret the data that follows. 8010Sstevel@tonic-gate */ 8020Sstevel@tonic-gate if (cmd->type == CKA_DECRYPT) { 8030Sstevel@tonic-gate if (read(infd, &version, sizeof (version)) != 804*5051Swyllys sizeof (version)) { 8050Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 8060Sstevel@tonic-gate "failed to get format version from " 8070Sstevel@tonic-gate "input file.")); 8080Sstevel@tonic-gate goto cleanup; 8090Sstevel@tonic-gate } 8100Sstevel@tonic-gate /* convert to host byte order */ 8110Sstevel@tonic-gate version = ntohl(version); 8120Sstevel@tonic-gate 8130Sstevel@tonic-gate switch (version) { 8140Sstevel@tonic-gate case 1: 8150Sstevel@tonic-gate /* 8160Sstevel@tonic-gate * Version 1 output format: 8170Sstevel@tonic-gate * - Iterations used in key gen function (4 bytes) 8180Sstevel@tonic-gate * - IV ( 'ivlen' bytes) 8190Sstevel@tonic-gate * - Salt data used in key gen (16 bytes) 8200Sstevel@tonic-gate * 8210Sstevel@tonic-gate * An encrypted file has IV as first block (0 or 8220Sstevel@tonic-gate * more bytes depending on mechanism) followed 8230Sstevel@tonic-gate * by cipher text. Get the IV from the encrypted 8240Sstevel@tonic-gate * file. 8250Sstevel@tonic-gate */ 8260Sstevel@tonic-gate /* 8270Sstevel@tonic-gate * Read iteration count and salt data. 8280Sstevel@tonic-gate */ 8290Sstevel@tonic-gate if (read(infd, &iterations, 830*5051Swyllys sizeof (iterations)) != sizeof (iterations)) { 8310Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 832*5051Swyllys "failed to get iterations from " 833*5051Swyllys "input file.")); 8340Sstevel@tonic-gate goto cleanup; 8350Sstevel@tonic-gate } 8360Sstevel@tonic-gate /* convert to host byte order */ 8370Sstevel@tonic-gate iterations = ntohl(iterations); 8380Sstevel@tonic-gate if (ivlen > 0 && 8390Sstevel@tonic-gate read(infd, pivbuf, ivlen) != ivlen) { 8400Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 8410Sstevel@tonic-gate "failed to get initialization " 8420Sstevel@tonic-gate "vector from input file.")); 8430Sstevel@tonic-gate goto cleanup; 8440Sstevel@tonic-gate } 8450Sstevel@tonic-gate if (read(infd, salt, sizeof (salt)) 846*5051Swyllys != sizeof (salt)) { 8470Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 848*5051Swyllys "failed to get salt data from " 849*5051Swyllys "input file.")); 8500Sstevel@tonic-gate goto cleanup; 8510Sstevel@tonic-gate } 8520Sstevel@tonic-gate break; 8530Sstevel@tonic-gate default: 8540Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 855*5051Swyllys "Unrecognized format version read from " 856*5051Swyllys "input file - expected %d, got %d."), 857*5051Swyllys SUNW_ENCRYPT_FILE_VERSION, version); 8580Sstevel@tonic-gate goto cleanup; 8590Sstevel@tonic-gate break; 8600Sstevel@tonic-gate } 8610Sstevel@tonic-gate } 8623812Shylee 8630Sstevel@tonic-gate /* 8643812Shylee * If Kflag is set, let's find the token key now. 8653812Shylee * 8663812Shylee * If Kflag is not set and if encrypting, we need some random 8670Sstevel@tonic-gate * salt data to create the key. If decrypting, 8680Sstevel@tonic-gate * the salt should come from head of the file 8690Sstevel@tonic-gate * to be decrypted. 8700Sstevel@tonic-gate */ 8713812Shylee if (Kflag) { 8723812Shylee rv = get_token_key(hSession, keytype, key_label, pkeydata, 8733812Shylee keysize, &key); 8743812Shylee if (rv != CKR_OK) { 8753812Shylee cryptoerror(LOG_STDERR, gettext( 8763812Shylee "Can not find the token key")); 8773812Shylee goto cleanup; 8783812Shylee } else { 8793812Shylee goto do_crypto; 8803812Shylee } 8813812Shylee } else if (cmd->type == CKA_ENCRYPT) { 8820Sstevel@tonic-gate rv = get_random_data(salt, sizeof (salt)); 8830Sstevel@tonic-gate if (rv != 0) { 8840Sstevel@tonic-gate cryptoerror(LOG_STDERR, 8850Sstevel@tonic-gate gettext("unable to generate random " 886*5051Swyllys "data for key salt.")); 8870Sstevel@tonic-gate goto cleanup; 8880Sstevel@tonic-gate } 8890Sstevel@tonic-gate } 8900Sstevel@tonic-gate 8913812Shylee 8920Sstevel@tonic-gate /* 8930Sstevel@tonic-gate * If key input is read from a file, treat it as 8940Sstevel@tonic-gate * raw key data, unless it is to be used with RC4, 8950Sstevel@tonic-gate * in which case it must be used to generate a pkcs5 8960Sstevel@tonic-gate * key to address security concerns with RC4 keys. 8970Sstevel@tonic-gate */ 8980Sstevel@tonic-gate if (kflag && keyfile != NULL && keytype != CKK_RC4) { 8990Sstevel@tonic-gate CK_OBJECT_CLASS objclass = CKO_SECRET_KEY; 9000Sstevel@tonic-gate CK_ATTRIBUTE template[5]; 9010Sstevel@tonic-gate int nattr = 0; 9020Sstevel@tonic-gate 9030Sstevel@tonic-gate template[nattr].type = CKA_CLASS; 9040Sstevel@tonic-gate template[nattr].pValue = &objclass; 9050Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (objclass); 9060Sstevel@tonic-gate nattr++; 9070Sstevel@tonic-gate 9080Sstevel@tonic-gate template[nattr].type = CKA_KEY_TYPE; 9090Sstevel@tonic-gate template[nattr].pValue = &keytype; 9100Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (keytype); 9110Sstevel@tonic-gate nattr++; 9120Sstevel@tonic-gate 9130Sstevel@tonic-gate template[nattr].type = cmd->type; 9140Sstevel@tonic-gate template[nattr].pValue = &truevalue; 9150Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (truevalue); 9160Sstevel@tonic-gate nattr++; 9170Sstevel@tonic-gate 9180Sstevel@tonic-gate template[nattr].type = CKA_TOKEN; 9190Sstevel@tonic-gate template[nattr].pValue = &falsevalue; 9200Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (falsevalue); 9210Sstevel@tonic-gate nattr++; 9220Sstevel@tonic-gate 9230Sstevel@tonic-gate template[nattr].type = CKA_VALUE; 9240Sstevel@tonic-gate template[nattr].pValue = pkeydata; 9250Sstevel@tonic-gate template[nattr].ulValueLen = keysize; 9260Sstevel@tonic-gate nattr++; 9270Sstevel@tonic-gate 928*5051Swyllys rv = C_CreateObject(hSession, template, nattr, &key); 9290Sstevel@tonic-gate } else { 9300Sstevel@tonic-gate /* 9310Sstevel@tonic-gate * If the encryption type has a fixed key length, 9320Sstevel@tonic-gate * then its not necessary to set the key length 9330Sstevel@tonic-gate * parameter when generating the key. 9340Sstevel@tonic-gate */ 9350Sstevel@tonic-gate if (keytype == CKK_DES || keytype == CKK_DES3) 9360Sstevel@tonic-gate keylen = 0; 9370Sstevel@tonic-gate else 9380Sstevel@tonic-gate keylen = 16; 9390Sstevel@tonic-gate 9400Sstevel@tonic-gate /* 9410Sstevel@tonic-gate * Generate a cryptographically secure key using 9420Sstevel@tonic-gate * the key read from the file given (-k keyfile) or 9430Sstevel@tonic-gate * the passphrase entered by the user. 9440Sstevel@tonic-gate */ 9450Sstevel@tonic-gate rv = generate_pkcs5_key(hSession, 946*5051Swyllys salt, sizeof (salt), iterations, 947*5051Swyllys pkeydata, keytype, keysize, 948*5051Swyllys keylen, cmd->type, &key); 9490Sstevel@tonic-gate } 9500Sstevel@tonic-gate 9510Sstevel@tonic-gate if (rv != CKR_OK) { 9520Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 9530Sstevel@tonic-gate "failed to generate a key: %s"), 9540Sstevel@tonic-gate pkcs11_strerror(rv)); 9550Sstevel@tonic-gate goto cleanup; 9560Sstevel@tonic-gate } 9570Sstevel@tonic-gate 9583812Shylee 9593812Shylee do_crypto: 9600Sstevel@tonic-gate /* Setup up mechanism */ 9610Sstevel@tonic-gate mech.mechanism = mech_type; 9620Sstevel@tonic-gate mech.pParameter = (CK_VOID_PTR)pivbuf; 9630Sstevel@tonic-gate mech.ulParameterLen = ivlen; 9640Sstevel@tonic-gate 9650Sstevel@tonic-gate if ((rv = cmd->Init(hSession, &mech, key)) != CKR_OK) { 9660Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 9670Sstevel@tonic-gate "failed to initialize crypto operation: %s"), 9680Sstevel@tonic-gate pkcs11_strerror(rv)); 9690Sstevel@tonic-gate goto cleanup; 9700Sstevel@tonic-gate } 9710Sstevel@tonic-gate 9720Sstevel@tonic-gate /* Write the version header encrypt command */ 9730Sstevel@tonic-gate if (cmd->type == CKA_ENCRYPT) { 9740Sstevel@tonic-gate /* convert to network order for storage */ 9750Sstevel@tonic-gate int netversion = htonl(version); 9760Sstevel@tonic-gate CK_ULONG netiter; 9770Sstevel@tonic-gate 9780Sstevel@tonic-gate if (write(outfd, &netversion, sizeof (netversion)) 979*5051Swyllys != sizeof (netversion)) { 9800Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 981*5051Swyllys "failed to write version number " 982*5051Swyllys "to output file.")); 9830Sstevel@tonic-gate goto cleanup; 9840Sstevel@tonic-gate } 9850Sstevel@tonic-gate /* 9860Sstevel@tonic-gate * Write the iteration and salt data, even if they 9870Sstevel@tonic-gate * were not used to generate a key. 9880Sstevel@tonic-gate */ 9890Sstevel@tonic-gate netiter = htonl(iterations); 9900Sstevel@tonic-gate if (write(outfd, &netiter, 991*5051Swyllys sizeof (netiter)) != sizeof (netiter)) { 9920Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 9930Sstevel@tonic-gate "failed to write iterations to output")); 9940Sstevel@tonic-gate goto cleanup; 9950Sstevel@tonic-gate } 996*5051Swyllys if (ivlen > 0 && write(outfd, pivbuf, ivlen) != ivlen) { 9970Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 998*5051Swyllys "failed to write initialization vector " 999*5051Swyllys "to output")); 10000Sstevel@tonic-gate goto cleanup; 10010Sstevel@tonic-gate } 10020Sstevel@tonic-gate if (write(outfd, salt, sizeof (salt)) != sizeof (salt)) { 10030Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 10040Sstevel@tonic-gate "failed to write salt data to output")); 10050Sstevel@tonic-gate goto cleanup; 10060Sstevel@tonic-gate } 10070Sstevel@tonic-gate } 10080Sstevel@tonic-gate 10091142Sjk115741 if (crypt_multipart(cmd, hSession, infd, outfd, insbuf.st_size) == -1) { 10100Sstevel@tonic-gate goto cleanup; 10110Sstevel@tonic-gate } 10120Sstevel@tonic-gate 10130Sstevel@tonic-gate errflag = B_FALSE; 10140Sstevel@tonic-gate 10150Sstevel@tonic-gate /* 10160Sstevel@tonic-gate * Clean up 10170Sstevel@tonic-gate */ 10180Sstevel@tonic-gate cleanup: 10190Sstevel@tonic-gate /* Clear the key data, so others cannot snoop */ 10200Sstevel@tonic-gate if (pkeydata != NULL) { 10210Sstevel@tonic-gate bzero(pkeydata, keysize); 10220Sstevel@tonic-gate free(pkeydata); 10230Sstevel@tonic-gate pkeydata = NULL; 10240Sstevel@tonic-gate } 10250Sstevel@tonic-gate 10260Sstevel@tonic-gate /* Destroy key object */ 10273812Shylee if (Kflag != B_FALSE && key != (CK_OBJECT_HANDLE) 0) { 10280Sstevel@tonic-gate (void) C_DestroyObject(hSession, key); 10290Sstevel@tonic-gate } 10300Sstevel@tonic-gate 10310Sstevel@tonic-gate /* free allocated memory */ 10320Sstevel@tonic-gate if (pSlotList != NULL) 10330Sstevel@tonic-gate free(pSlotList); 10340Sstevel@tonic-gate if (pivbuf != NULL) 10350Sstevel@tonic-gate free(pivbuf); 10360Sstevel@tonic-gate 10370Sstevel@tonic-gate /* close all the files */ 10381142Sjk115741 if (iflag && (infd != -1)) 10390Sstevel@tonic-gate (void) close(infd); 10401142Sjk115741 if (oflag && (outfd != -1)) 10410Sstevel@tonic-gate (void) close(outfd); 10420Sstevel@tonic-gate 10430Sstevel@tonic-gate /* rename tmp output to input file */ 10440Sstevel@tonic-gate if (inoutsame) { 10450Sstevel@tonic-gate if (rename(outfilename, inputfile) == -1) { 10460Sstevel@tonic-gate (void) unlink(outfilename); 10470Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("rename failed.")); 10480Sstevel@tonic-gate } 10490Sstevel@tonic-gate } 10500Sstevel@tonic-gate 10510Sstevel@tonic-gate /* If error occurred, remove the output file */ 10520Sstevel@tonic-gate if (errflag && outfilename != NULL) { 10530Sstevel@tonic-gate (void) unlink(outfilename); 10540Sstevel@tonic-gate } 10550Sstevel@tonic-gate 10560Sstevel@tonic-gate /* close pkcs11 session */ 10570Sstevel@tonic-gate if (hSession != CK_INVALID_HANDLE) 10580Sstevel@tonic-gate (void) C_CloseSession(hSession); 10590Sstevel@tonic-gate 10600Sstevel@tonic-gate (void) C_Finalize(NULL); 10610Sstevel@tonic-gate 10620Sstevel@tonic-gate return (errflag); 10630Sstevel@tonic-gate } 10640Sstevel@tonic-gate 10650Sstevel@tonic-gate /* 10660Sstevel@tonic-gate * Function for printing progress bar when the verbose flag 10670Sstevel@tonic-gate * is set. 10680Sstevel@tonic-gate * 10690Sstevel@tonic-gate * The vertical bar is printed at 25, 50, and 75% complete. 10700Sstevel@tonic-gate * 10710Sstevel@tonic-gate * The function is passed the number of positions on the screen it needs to 10720Sstevel@tonic-gate * advance and loops. 10730Sstevel@tonic-gate */ 10740Sstevel@tonic-gate 10750Sstevel@tonic-gate static void 10760Sstevel@tonic-gate print_status(int pos_to_advance) 10770Sstevel@tonic-gate { 10780Sstevel@tonic-gate 10790Sstevel@tonic-gate while (pos_to_advance > 0) { 10800Sstevel@tonic-gate switch (status_pos) { 10810Sstevel@tonic-gate case 0: 10820Sstevel@tonic-gate (void) fprintf(stderr, gettext("[")); 10830Sstevel@tonic-gate break; 10840Sstevel@tonic-gate case 19: 10850Sstevel@tonic-gate case 39: 10860Sstevel@tonic-gate case 59: 10870Sstevel@tonic-gate (void) fprintf(stderr, gettext("|")); 10880Sstevel@tonic-gate break; 10890Sstevel@tonic-gate default: 10900Sstevel@tonic-gate (void) fprintf(stderr, gettext(".")); 10910Sstevel@tonic-gate } 10920Sstevel@tonic-gate pos_to_advance--; 10930Sstevel@tonic-gate status_pos++; 10940Sstevel@tonic-gate } 10950Sstevel@tonic-gate } 10960Sstevel@tonic-gate 10970Sstevel@tonic-gate /* 10980Sstevel@tonic-gate * Encrypt/Decrypt in multi part. 10990Sstevel@tonic-gate * 11000Sstevel@tonic-gate * This function reads the input file (infd) and writes the 11010Sstevel@tonic-gate * encrypted/decrypted output to file (outfd). 11020Sstevel@tonic-gate * 11030Sstevel@tonic-gate * cmd - pointing to commandinfo 11040Sstevel@tonic-gate * hSession - pkcs session 11050Sstevel@tonic-gate * infd - input file descriptor 11060Sstevel@tonic-gate * outfd - output file descriptor 11070Sstevel@tonic-gate * 11080Sstevel@tonic-gate */ 11090Sstevel@tonic-gate 11100Sstevel@tonic-gate static int 11110Sstevel@tonic-gate crypt_multipart(struct CommandInfo *cmd, CK_SESSION_HANDLE hSession, 11121142Sjk115741 int infd, int outfd, off_t insize) 11130Sstevel@tonic-gate { 11140Sstevel@tonic-gate CK_RV rv; 11150Sstevel@tonic-gate CK_ULONG resultlen; 11160Sstevel@tonic-gate CK_ULONG resultbuflen; 11170Sstevel@tonic-gate CK_BYTE_PTR resultbuf; 11180Sstevel@tonic-gate CK_ULONG datalen; 11190Sstevel@tonic-gate CK_BYTE databuf[BUFFERSIZE]; 11200Sstevel@tonic-gate CK_BYTE outbuf[BUFFERSIZE+BLOCKSIZE]; 11210Sstevel@tonic-gate CK_ULONG status_index = 0; /* current total file size read */ 11220Sstevel@tonic-gate float status_last = 0.0; /* file size of last element used */ 11230Sstevel@tonic-gate float status_incr = 0.0; /* file size element increments */ 11240Sstevel@tonic-gate int pos; /* # of progress bar elements to be print */ 11250Sstevel@tonic-gate ssize_t nread; 11260Sstevel@tonic-gate boolean_t errflag = B_FALSE; 11270Sstevel@tonic-gate 11280Sstevel@tonic-gate datalen = sizeof (databuf); 11290Sstevel@tonic-gate resultbuflen = sizeof (outbuf); 11300Sstevel@tonic-gate resultbuf = outbuf; 11310Sstevel@tonic-gate 11320Sstevel@tonic-gate /* Divide into 79 increments for progress bar element spacing */ 11330Sstevel@tonic-gate if (vflag && iflag) 11341142Sjk115741 status_incr = (insize / 79.0); 11350Sstevel@tonic-gate 11360Sstevel@tonic-gate while ((nread = read(infd, databuf, datalen)) > 0) { 11370Sstevel@tonic-gate 11380Sstevel@tonic-gate /* Start with the initial buffer */ 11390Sstevel@tonic-gate resultlen = resultbuflen; 11400Sstevel@tonic-gate rv = cmd->Update(hSession, databuf, (CK_ULONG)nread, 1141*5051Swyllys resultbuf, &resultlen); 11420Sstevel@tonic-gate 11430Sstevel@tonic-gate /* Need a bigger buffer? */ 11440Sstevel@tonic-gate if (rv == CKR_BUFFER_TOO_SMALL) { 11450Sstevel@tonic-gate 11460Sstevel@tonic-gate /* free the old buffer */ 11470Sstevel@tonic-gate if (resultbuf != NULL && resultbuf != outbuf) { 11480Sstevel@tonic-gate bzero(resultbuf, resultbuflen); 11490Sstevel@tonic-gate free(resultbuf); 11500Sstevel@tonic-gate } 11510Sstevel@tonic-gate 11520Sstevel@tonic-gate /* allocate a new big buffer */ 11530Sstevel@tonic-gate if ((resultbuf = malloc((size_t)resultlen)) == NULL) { 11540Sstevel@tonic-gate int err = errno; 11550Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s"), 11560Sstevel@tonic-gate strerror(err)); 11570Sstevel@tonic-gate return (-1); 11580Sstevel@tonic-gate } 11590Sstevel@tonic-gate resultbuflen = resultlen; 11600Sstevel@tonic-gate 11610Sstevel@tonic-gate /* Try again with bigger buffer */ 11620Sstevel@tonic-gate rv = cmd->Update(hSession, databuf, (CK_ULONG)nread, 1163*5051Swyllys resultbuf, &resultlen); 11640Sstevel@tonic-gate } 11650Sstevel@tonic-gate 11660Sstevel@tonic-gate if (rv != CKR_OK) { 11670Sstevel@tonic-gate errflag = B_TRUE; 11680Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 11690Sstevel@tonic-gate "crypto operation failed: %s"), 11700Sstevel@tonic-gate pkcs11_strerror(rv)); 11710Sstevel@tonic-gate break; 11720Sstevel@tonic-gate } 11730Sstevel@tonic-gate 11740Sstevel@tonic-gate /* write the output */ 11750Sstevel@tonic-gate if (write(outfd, resultbuf, resultlen) != resultlen) { 11760Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 11770Sstevel@tonic-gate "failed to write result to output file.")); 11780Sstevel@tonic-gate errflag = B_TRUE; 11790Sstevel@tonic-gate break; 11800Sstevel@tonic-gate } 11810Sstevel@tonic-gate 11820Sstevel@tonic-gate if (vflag) { 11830Sstevel@tonic-gate status_index += resultlen; 11840Sstevel@tonic-gate 11850Sstevel@tonic-gate /* 11860Sstevel@tonic-gate * If input is from stdin, do a our own progress bar 11870Sstevel@tonic-gate * by printing periods at a pre-defined increment 11880Sstevel@tonic-gate * until the file is done. 11890Sstevel@tonic-gate */ 11900Sstevel@tonic-gate if (!iflag) { 11910Sstevel@tonic-gate 11920Sstevel@tonic-gate /* 11930Sstevel@tonic-gate * Print at least 1 element in case the file 11940Sstevel@tonic-gate * is small, it looks better than nothing. 11950Sstevel@tonic-gate */ 11960Sstevel@tonic-gate if (status_pos == 0) { 11970Sstevel@tonic-gate (void) fprintf(stderr, gettext(".")); 11980Sstevel@tonic-gate status_pos = 1; 11990Sstevel@tonic-gate } 12000Sstevel@tonic-gate 12010Sstevel@tonic-gate if ((status_index - status_last) > 12020Sstevel@tonic-gate (PROGRESSSIZE)) { 12030Sstevel@tonic-gate (void) fprintf(stderr, gettext(".")); 12040Sstevel@tonic-gate status_last = status_index; 12050Sstevel@tonic-gate } 12060Sstevel@tonic-gate continue; 12070Sstevel@tonic-gate } 12080Sstevel@tonic-gate 12090Sstevel@tonic-gate /* Calculate the number of elements need to be print */ 12101142Sjk115741 if (insize <= BUFFERSIZE) 12110Sstevel@tonic-gate pos = 78; 12120Sstevel@tonic-gate else 12130Sstevel@tonic-gate pos = (int)((status_index - status_last) / 12140Sstevel@tonic-gate status_incr); 12150Sstevel@tonic-gate 12160Sstevel@tonic-gate /* Add progress bar elements, if needed */ 12170Sstevel@tonic-gate if (pos > 0) { 12180Sstevel@tonic-gate print_status(pos); 12190Sstevel@tonic-gate status_last += (status_incr * pos); 12200Sstevel@tonic-gate } 12210Sstevel@tonic-gate } 12220Sstevel@tonic-gate } 12230Sstevel@tonic-gate 12240Sstevel@tonic-gate /* Print verbose completion */ 12250Sstevel@tonic-gate if (vflag) { 12260Sstevel@tonic-gate if (iflag) 12270Sstevel@tonic-gate (void) fprintf(stderr, "]"); 12280Sstevel@tonic-gate 12290Sstevel@tonic-gate (void) fprintf(stderr, "\n%s\n", gettext("Done.")); 12300Sstevel@tonic-gate } 12310Sstevel@tonic-gate 12320Sstevel@tonic-gate /* Error in reading */ 12330Sstevel@tonic-gate if (nread == -1) { 12340Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 12350Sstevel@tonic-gate "error reading from input file")); 12360Sstevel@tonic-gate errflag = B_TRUE; 12370Sstevel@tonic-gate } 12380Sstevel@tonic-gate 12390Sstevel@tonic-gate if (!errflag) { 12400Sstevel@tonic-gate 12410Sstevel@tonic-gate /* Do the final part */ 12420Sstevel@tonic-gate 12430Sstevel@tonic-gate rv = cmd->Final(hSession, resultbuf, &resultlen); 12440Sstevel@tonic-gate 12450Sstevel@tonic-gate if (rv == CKR_OK) { 12460Sstevel@tonic-gate /* write the output */ 12470Sstevel@tonic-gate if (write(outfd, resultbuf, resultlen) != resultlen) { 12480Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 12490Sstevel@tonic-gate "failed to write result to output file.")); 12500Sstevel@tonic-gate errflag = B_TRUE; 12510Sstevel@tonic-gate } 12520Sstevel@tonic-gate } else { 12530Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 12540Sstevel@tonic-gate "crypto operation failed: %s"), 12550Sstevel@tonic-gate pkcs11_strerror(rv)); 12560Sstevel@tonic-gate errflag = B_TRUE; 12570Sstevel@tonic-gate } 12580Sstevel@tonic-gate 12590Sstevel@tonic-gate } 12600Sstevel@tonic-gate 12610Sstevel@tonic-gate if (resultbuf != NULL && resultbuf != outbuf) { 12620Sstevel@tonic-gate bzero(resultbuf, resultbuflen); 12630Sstevel@tonic-gate free(resultbuf); 12640Sstevel@tonic-gate } 12650Sstevel@tonic-gate 12660Sstevel@tonic-gate if (errflag) { 12670Sstevel@tonic-gate return (-1); 12680Sstevel@tonic-gate } else { 12690Sstevel@tonic-gate return (0); 12700Sstevel@tonic-gate } 12710Sstevel@tonic-gate } 12720Sstevel@tonic-gate 12730Sstevel@tonic-gate /* 12740Sstevel@tonic-gate * cryptoreadfile - reads file into a buffer 12750Sstevel@tonic-gate * This function can be used for reading files 12760Sstevel@tonic-gate * containing key or initialization vector data. 12770Sstevel@tonic-gate * 12780Sstevel@tonic-gate * filename - name of file 12790Sstevel@tonic-gate * pdata - entire file returned in this buffer 12800Sstevel@tonic-gate * must be freed by caller using free() 12810Sstevel@tonic-gate * pdatalen - length of data returned 12820Sstevel@tonic-gate * 12830Sstevel@tonic-gate * returns 0 if success, -1 if error 12840Sstevel@tonic-gate */ 12850Sstevel@tonic-gate static int 12860Sstevel@tonic-gate cryptoreadfile(char *filename, CK_BYTE_PTR *pdata, CK_ULONG_PTR pdatalen) 12870Sstevel@tonic-gate { 12880Sstevel@tonic-gate struct stat statbuf; 12890Sstevel@tonic-gate char *filebuf; 12900Sstevel@tonic-gate int filesize; 12910Sstevel@tonic-gate int fd; 12920Sstevel@tonic-gate 12930Sstevel@tonic-gate if (filename == NULL) 12940Sstevel@tonic-gate return (-1); 12950Sstevel@tonic-gate 12960Sstevel@tonic-gate /* read the file into a buffer */ 12970Sstevel@tonic-gate if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) == -1) { 12980Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 1299*5051Swyllys "cannot open %s"), filename); 13000Sstevel@tonic-gate return (-1); 13010Sstevel@tonic-gate 13020Sstevel@tonic-gate } 13030Sstevel@tonic-gate 13040Sstevel@tonic-gate if (fstat(fd, &statbuf) == -1) { 13050Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 1306*5051Swyllys "cannot stat %s"), filename); 13070Sstevel@tonic-gate (void) close(fd); 13080Sstevel@tonic-gate return (-1); 13090Sstevel@tonic-gate } 13100Sstevel@tonic-gate 1311871Scasper if (!S_ISREG(statbuf.st_mode)) { 13120Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 1313*5051Swyllys "%s not a regular file"), filename); 13140Sstevel@tonic-gate (void) close(fd); 13150Sstevel@tonic-gate return (-1); 13160Sstevel@tonic-gate } 13170Sstevel@tonic-gate 13180Sstevel@tonic-gate filesize = (size_t)statbuf.st_size; 13190Sstevel@tonic-gate 13200Sstevel@tonic-gate if (filesize == 0) { 13210Sstevel@tonic-gate (void) close(fd); 13220Sstevel@tonic-gate return (-1); 13230Sstevel@tonic-gate } 13240Sstevel@tonic-gate 13250Sstevel@tonic-gate /* allocate a buffer to hold the entire key */ 13260Sstevel@tonic-gate if ((filebuf = malloc(filesize)) == NULL) { 13270Sstevel@tonic-gate int err = errno; 13280Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s"), strerror(err)); 13290Sstevel@tonic-gate (void) close(fd); 13300Sstevel@tonic-gate return (-1); 13310Sstevel@tonic-gate } 13320Sstevel@tonic-gate 13330Sstevel@tonic-gate if (read(fd, filebuf, filesize) != filesize) { 13340Sstevel@tonic-gate int err = errno; 13350Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("error reading file: %s"), 13360Sstevel@tonic-gate strerror(err)); 13370Sstevel@tonic-gate (void) close(fd); 13380Sstevel@tonic-gate free(filebuf); 13390Sstevel@tonic-gate return (-1); 13400Sstevel@tonic-gate } 13410Sstevel@tonic-gate 13420Sstevel@tonic-gate (void) close(fd); 13430Sstevel@tonic-gate 13440Sstevel@tonic-gate *pdata = (CK_BYTE_PTR)filebuf; 13450Sstevel@tonic-gate *pdatalen = (CK_ULONG)filesize; 13460Sstevel@tonic-gate 13470Sstevel@tonic-gate return (0); 13480Sstevel@tonic-gate } 13493812Shylee 13500Sstevel@tonic-gate /* 13513812Shylee * cryptogetdata - prompt user for a key or the PIN for a token 13520Sstevel@tonic-gate * 13533812Shylee * pdata - buffer for returning key or pin data 13540Sstevel@tonic-gate * must be freed by caller using free() 13553812Shylee * psize - size of buffer returned 13560Sstevel@tonic-gate * 13570Sstevel@tonic-gate * returns 13580Sstevel@tonic-gate * 0 for success, -1 for failure 13590Sstevel@tonic-gate */ 13600Sstevel@tonic-gate 13610Sstevel@tonic-gate static int 13623812Shylee cryptogetdata(char *token_spec, CK_BYTE_PTR *pdata, CK_ULONG_PTR psize) 13630Sstevel@tonic-gate { 13643812Shylee char *databuf = NULL; 13653812Shylee char *tmpbuf = NULL; 13663812Shylee char prompt[1024]; 13670Sstevel@tonic-gate 13683812Shylee if (token_spec != NULL) { 13693812Shylee (void) snprintf(prompt, sizeof (prompt), 13703812Shylee DEFAULT_TOKEN_PROMPT, token_spec); 13713812Shylee tmpbuf = getpassphrase(gettext(prompt)); 13723812Shylee } else { 13733812Shylee tmpbuf = getpassphrase(gettext("Enter key:")); 13743812Shylee } 13750Sstevel@tonic-gate 13760Sstevel@tonic-gate if (tmpbuf == NULL) { 13770Sstevel@tonic-gate return (-1); /* error */ 13780Sstevel@tonic-gate } else { 13793812Shylee databuf = strdup(tmpbuf); 13803812Shylee (void) memset(tmpbuf, 0, strlen(tmpbuf)); /* clean up */ 13813812Shylee if (databuf == NULL) 13823812Shylee return (-1); 13830Sstevel@tonic-gate } 13840Sstevel@tonic-gate 13853812Shylee *pdata = (CK_BYTE_PTR)databuf; 13863812Shylee *psize = (CK_ULONG)strlen(databuf); 13870Sstevel@tonic-gate 13880Sstevel@tonic-gate return (0); 13890Sstevel@tonic-gate } 13900Sstevel@tonic-gate 13910Sstevel@tonic-gate /* 13920Sstevel@tonic-gate * get_random_data - generate initialization vector data 13930Sstevel@tonic-gate * iv data is random bytes 13940Sstevel@tonic-gate * hSession - a pkcs session 13950Sstevel@tonic-gate * pivbuf - buffer where data is returned 13960Sstevel@tonic-gate * ivlen - size of iv data 13970Sstevel@tonic-gate */ 13980Sstevel@tonic-gate static int 13990Sstevel@tonic-gate get_random_data(CK_BYTE_PTR pivbuf, int ivlen) 14000Sstevel@tonic-gate { 14010Sstevel@tonic-gate int fd; 14020Sstevel@tonic-gate 14030Sstevel@tonic-gate if (ivlen == 0) { 14040Sstevel@tonic-gate /* nothing to generate */ 14050Sstevel@tonic-gate return (0); 14060Sstevel@tonic-gate } 14070Sstevel@tonic-gate 14080Sstevel@tonic-gate /* Read random data directly from /dev/random */ 14090Sstevel@tonic-gate if ((fd = open(RANDOM_DEVICE, O_RDONLY)) != -1) { 14100Sstevel@tonic-gate if (read(fd, pivbuf, (size_t)ivlen) == ivlen) { 14110Sstevel@tonic-gate (void) close(fd); 14120Sstevel@tonic-gate return (0); 14130Sstevel@tonic-gate } 14140Sstevel@tonic-gate } 14150Sstevel@tonic-gate (void) close(fd); 14160Sstevel@tonic-gate return (-1); 14170Sstevel@tonic-gate } 1418