10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 22*1142Sjk115741 /* Portions Copyright 2005 Richard Lowe */ 230Sstevel@tonic-gate /* 240Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 250Sstevel@tonic-gate * Use is subject to license terms. 260Sstevel@tonic-gate */ 270Sstevel@tonic-gate 280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* 310Sstevel@tonic-gate * decrypt.c 320Sstevel@tonic-gate * 330Sstevel@tonic-gate * Implements encrypt(1) and decrypt(1) commands 340Sstevel@tonic-gate * 350Sstevel@tonic-gate * One binary performs both encrypt/decrypt operation. 360Sstevel@tonic-gate * 370Sstevel@tonic-gate * usage: 380Sstevel@tonic-gate * 390Sstevel@tonic-gate * algorithm - mechanism name without CKM_ prefix. Case 400Sstevel@tonic-gate * does not matter 410Sstevel@tonic-gate * keyfile - file containing key data. If not specified user is 420Sstevel@tonic-gate * prompted to enter key. key length > 0 is required 430Sstevel@tonic-gate * infile - input file to encrypt/decrypt. If omitted, stdin used. 440Sstevel@tonic-gate * outfile - output file to encrypt/decrypt. If omitted, stdout used. 450Sstevel@tonic-gate * if infile & outfile are same, a temp file is used for 460Sstevel@tonic-gate * output and infile is replaced with this file after 470Sstevel@tonic-gate * operation is complete. 480Sstevel@tonic-gate * 490Sstevel@tonic-gate * Implementation notes: 500Sstevel@tonic-gate * iv data - It is generated by random bytes equal to one block size. 510Sstevel@tonic-gate * 520Sstevel@tonic-gate * encrypted output format - 530Sstevel@tonic-gate * - Output format version number - 4 bytes in network byte order. 540Sstevel@tonic-gate * - Iterations used in key gen function, 4 bytes in network byte order. 550Sstevel@tonic-gate * - IV ( 'ivlen' bytes) 560Sstevel@tonic-gate * - Salt data used in key gen (16 bytes) 570Sstevel@tonic-gate * - cipher text data. 580Sstevel@tonic-gate * 590Sstevel@tonic-gate */ 600Sstevel@tonic-gate 610Sstevel@tonic-gate #include <stdio.h> 620Sstevel@tonic-gate #include <stdlib.h> 630Sstevel@tonic-gate #include <unistd.h> 640Sstevel@tonic-gate #include <errno.h> 650Sstevel@tonic-gate #include <fcntl.h> 660Sstevel@tonic-gate #include <ctype.h> 670Sstevel@tonic-gate #include <strings.h> 680Sstevel@tonic-gate #include <libintl.h> 690Sstevel@tonic-gate #include <libgen.h> 700Sstevel@tonic-gate #include <locale.h> 710Sstevel@tonic-gate #include <limits.h> 720Sstevel@tonic-gate #include <sys/types.h> 730Sstevel@tonic-gate #include <sys/stat.h> 740Sstevel@tonic-gate #include <netinet/in.h> 750Sstevel@tonic-gate #include <security/cryptoki.h> 760Sstevel@tonic-gate #include <cryptoutil.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 */ 1000Sstevel@tonic-gate #define ENCRYPT_OPTIONS "a:k:i:o:lv" /* options for encrypt */ 1010Sstevel@tonic-gate #define DECRYPT_NAME "decrypt" /* name of decrypt command */ 1020Sstevel@tonic-gate #define DECRYPT_OPTIONS "a:k:i:o:lv" /* options for decrypt */ 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate /* 1050Sstevel@tonic-gate * Structure containing info for encrypt/decrypt 1060Sstevel@tonic-gate * command 1070Sstevel@tonic-gate */ 1080Sstevel@tonic-gate struct CommandInfo { 1090Sstevel@tonic-gate char *name; /* name of the command */ 1100Sstevel@tonic-gate char *options; /* command line options */ 1110Sstevel@tonic-gate CK_FLAGS flags; 1120Sstevel@tonic-gate CK_ATTRIBUTE_TYPE type; /* type of command */ 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate /* function pointers for various operations */ 1150Sstevel@tonic-gate CK_RV (*Init)(CK_SESSION_HANDLE, CK_MECHANISM_PTR, CK_OBJECT_HANDLE); 1160Sstevel@tonic-gate CK_RV (*Update)(CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG, CK_BYTE_PTR, 1170Sstevel@tonic-gate CK_ULONG_PTR); 1180Sstevel@tonic-gate CK_RV (*Crypt)(CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG, CK_BYTE_PTR, 1190Sstevel@tonic-gate CK_ULONG_PTR); 1200Sstevel@tonic-gate CK_RV (*Final)(CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG_PTR); 1210Sstevel@tonic-gate }; 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate static struct CommandInfo encrypt_cmd = { 1240Sstevel@tonic-gate ENCRYPT_NAME, 1250Sstevel@tonic-gate ENCRYPT_OPTIONS, 1260Sstevel@tonic-gate CKF_ENCRYPT, 1270Sstevel@tonic-gate CKA_ENCRYPT, 1280Sstevel@tonic-gate C_EncryptInit, 1290Sstevel@tonic-gate C_EncryptUpdate, 1300Sstevel@tonic-gate C_Encrypt, 1310Sstevel@tonic-gate C_EncryptFinal 1320Sstevel@tonic-gate }; 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate static struct CommandInfo decrypt_cmd = { 1350Sstevel@tonic-gate DECRYPT_NAME, 1360Sstevel@tonic-gate DECRYPT_OPTIONS, 1370Sstevel@tonic-gate CKF_DECRYPT, 1380Sstevel@tonic-gate CKA_DECRYPT, 1390Sstevel@tonic-gate C_DecryptInit, 1400Sstevel@tonic-gate C_DecryptUpdate, 1410Sstevel@tonic-gate C_Decrypt, 1420Sstevel@tonic-gate C_DecryptFinal 1430Sstevel@tonic-gate }; 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate struct mech_alias { 1460Sstevel@tonic-gate CK_MECHANISM_TYPE type; 1470Sstevel@tonic-gate char *alias; 1480Sstevel@tonic-gate CK_ULONG keysize_min; 1490Sstevel@tonic-gate CK_ULONG keysize_max; 1500Sstevel@tonic-gate int keysize_unit; 1510Sstevel@tonic-gate int ivlen; 1520Sstevel@tonic-gate boolean_t available; 1530Sstevel@tonic-gate }; 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate #define MECH_ALIASES_COUNT 4 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate static struct mech_alias mech_aliases[] = { 1580Sstevel@tonic-gate { CKM_AES_CBC_PAD, "aes", ULONG_MAX, 0L, 8, 16, B_FALSE }, 1590Sstevel@tonic-gate { CKM_RC4, "arcfour", ULONG_MAX, 0L, 1, 0, B_FALSE }, 1600Sstevel@tonic-gate { CKM_DES_CBC_PAD, "des", 8, 8, 8, 8, B_FALSE }, 1610Sstevel@tonic-gate { CKM_DES3_CBC_PAD, "3des", 24, 24, 8, 8, B_FALSE }, 1620Sstevel@tonic-gate }; 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate static CK_BBOOL truevalue = TRUE; 1650Sstevel@tonic-gate static CK_BBOOL falsevalue = FALSE; 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate static boolean_t aflag = B_FALSE; /* -a <algorithm> flag, required */ 1680Sstevel@tonic-gate static boolean_t kflag = B_FALSE; /* -k <keyfile> flag */ 1690Sstevel@tonic-gate static boolean_t iflag = B_FALSE; /* -i <infile> flag, use stdin if absent */ 1700Sstevel@tonic-gate static boolean_t oflag = B_FALSE; /* -o <outfile> flag, use stdout if absent */ 1710Sstevel@tonic-gate static boolean_t lflag = B_FALSE; /* -l flag (list) */ 1720Sstevel@tonic-gate static boolean_t vflag = B_FALSE; /* -v flag (verbose) */ 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate static char *keyfile = NULL; /* name of keyfile */ 1750Sstevel@tonic-gate static char *inputfile = NULL; /* name of input file */ 1760Sstevel@tonic-gate static char *outputfile = NULL; /* name of output file */ 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate static int status_pos = 0; /* current position of progress bar element */ 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate /* 1810Sstevel@tonic-gate * function prototypes 1820Sstevel@tonic-gate */ 1830Sstevel@tonic-gate static void usage(struct CommandInfo *cmd); 1840Sstevel@tonic-gate static int execute_cmd(struct CommandInfo *cmd, char *algo_str); 1850Sstevel@tonic-gate static int cryptogetkey(CK_BYTE_PTR *pkeydata, CK_ULONG_PTR pkeysize); 1860Sstevel@tonic-gate static int cryptoreadfile(char *filename, CK_BYTE_PTR *pdata, 1870Sstevel@tonic-gate CK_ULONG_PTR pdatalen); 1880Sstevel@tonic-gate static int get_random_data(CK_BYTE_PTR pivbuf, int ivlen); 1890Sstevel@tonic-gate static int crypt_multipart(struct CommandInfo *cmd, CK_SESSION_HANDLE hSession, 190*1142Sjk115741 int infd, int outfd, off_t insize); 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate int 1930Sstevel@tonic-gate main(int argc, char **argv) 1940Sstevel@tonic-gate { 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate extern char *optarg; 1970Sstevel@tonic-gate extern int optind; 1980Sstevel@tonic-gate char *optstr; 1990Sstevel@tonic-gate char c; /* current getopts flag */ 2000Sstevel@tonic-gate char *algo_str = NULL; /* algorithm string */ 2010Sstevel@tonic-gate struct CommandInfo *cmd; 2020Sstevel@tonic-gate char *cmdname; /* name of command */ 2030Sstevel@tonic-gate boolean_t errflag = B_FALSE; 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 2060Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defiend by cc -D */ 2070Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 2080Sstevel@tonic-gate #endif 2090Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate /* 2120Sstevel@tonic-gate * Based on command name, determine 2130Sstevel@tonic-gate * type of command. 2140Sstevel@tonic-gate */ 2150Sstevel@tonic-gate cmdname = basename(argv[0]); 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate cryptodebug_init(cmdname); 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate if (strcmp(cmdname, encrypt_cmd.name) == 0) { 2200Sstevel@tonic-gate cmd = &encrypt_cmd; 2210Sstevel@tonic-gate } else if (strcmp(cmdname, decrypt_cmd.name) == 0) { 2220Sstevel@tonic-gate cmd = &decrypt_cmd; 2230Sstevel@tonic-gate } else { 2240Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 2250Sstevel@tonic-gate "command name must be either encrypt or decrypt")); 2260Sstevel@tonic-gate exit(EXIT_USAGE); 2270Sstevel@tonic-gate } 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate optstr = cmd->options; 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate /* Parse command line arguments */ 2320Sstevel@tonic-gate while (!errflag && (c = getopt(argc, argv, optstr)) != -1) { 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate switch (c) { 2350Sstevel@tonic-gate case 'a': 2360Sstevel@tonic-gate aflag = B_TRUE; 2370Sstevel@tonic-gate algo_str = optarg; 2380Sstevel@tonic-gate break; 2390Sstevel@tonic-gate case 'k': 2400Sstevel@tonic-gate kflag = B_TRUE; 2410Sstevel@tonic-gate keyfile = optarg; 2420Sstevel@tonic-gate break; 2430Sstevel@tonic-gate case 'i': 2440Sstevel@tonic-gate iflag = B_TRUE; 2450Sstevel@tonic-gate inputfile = optarg; 2460Sstevel@tonic-gate break; 2470Sstevel@tonic-gate case 'o': 2480Sstevel@tonic-gate oflag = B_TRUE; 2490Sstevel@tonic-gate outputfile = optarg; 2500Sstevel@tonic-gate break; 2510Sstevel@tonic-gate case 'l': 2520Sstevel@tonic-gate lflag = B_TRUE; 2530Sstevel@tonic-gate break; 2540Sstevel@tonic-gate case 'v': 2550Sstevel@tonic-gate vflag = B_TRUE; 2560Sstevel@tonic-gate break; 2570Sstevel@tonic-gate default: 2580Sstevel@tonic-gate errflag = B_TRUE; 2590Sstevel@tonic-gate } 2600Sstevel@tonic-gate } 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate if (errflag || (!aflag && !lflag) || (lflag && argc > 2) || 2630Sstevel@tonic-gate (optind < argc)) { 2640Sstevel@tonic-gate usage(cmd); 2650Sstevel@tonic-gate exit(EXIT_USAGE); 2660Sstevel@tonic-gate } 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate return (execute_cmd(cmd, algo_str)); 2690Sstevel@tonic-gate } 2700Sstevel@tonic-gate 2710Sstevel@tonic-gate /* 2720Sstevel@tonic-gate * usage message 2730Sstevel@tonic-gate */ 2740Sstevel@tonic-gate static void 2750Sstevel@tonic-gate usage(struct CommandInfo *cmd) 2760Sstevel@tonic-gate { 2770Sstevel@tonic-gate if (cmd->type == CKA_ENCRYPT) { 2780Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("usage: encrypt -l | -a " 279*1142Sjk115741 "<algorithm> [-v] [-k <keyfile>] [-i <infile>]" 2800Sstevel@tonic-gate "\n\t\t\t[-o <outfile>]")); 2810Sstevel@tonic-gate } else { 2820Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("usage: decrypt -l | -a " 283*1142Sjk115741 "<algorithm> [-v] [-k <keyfile>] [-i <infile>]" 2840Sstevel@tonic-gate "\n\t\t\t[-o <outfile>]")); 2850Sstevel@tonic-gate } 2860Sstevel@tonic-gate } 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate /* 2890Sstevel@tonic-gate * Print out list of algorithms in default and verbose mode 2900Sstevel@tonic-gate */ 2910Sstevel@tonic-gate static void 2920Sstevel@tonic-gate algorithm_list() 2930Sstevel@tonic-gate { 2940Sstevel@tonic-gate int mech; 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate (void) printf(gettext("Algorithm Keysize: Min Max (bits)\n" 2970Sstevel@tonic-gate "------------------------------------------\n")); 2980Sstevel@tonic-gate 2990Sstevel@tonic-gate for (mech = 0; mech < MECH_ALIASES_COUNT; mech++) { 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate if (mech_aliases[mech].available == B_FALSE) 3020Sstevel@tonic-gate continue; 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate (void) printf("%-15s", mech_aliases[mech].alias); 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate if (mech_aliases[mech].keysize_min != UINT_MAX && 3070Sstevel@tonic-gate mech_aliases[mech].keysize_max != 0) 3080Sstevel@tonic-gate (void) printf(" %5lu %5lu\n", 3090Sstevel@tonic-gate (mech_aliases[mech].keysize_min * 3100Sstevel@tonic-gate mech_aliases[mech].keysize_unit), 3110Sstevel@tonic-gate (mech_aliases[mech].keysize_max * 3120Sstevel@tonic-gate mech_aliases[mech].keysize_unit)); 3130Sstevel@tonic-gate else 3140Sstevel@tonic-gate (void) printf("\n"); 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate } 3170Sstevel@tonic-gate } 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate static CK_RV 3200Sstevel@tonic-gate generate_pkcs5_key(CK_SESSION_HANDLE hSession, 3210Sstevel@tonic-gate CK_BYTE *pSaltData, 3220Sstevel@tonic-gate CK_ULONG saltLen, 3230Sstevel@tonic-gate CK_ULONG iterations, 3240Sstevel@tonic-gate CK_BYTE *pkeydata, /* user entered passphrase */ 3250Sstevel@tonic-gate CK_KEY_TYPE keytype, 3260Sstevel@tonic-gate CK_ULONG passwd_size, 3270Sstevel@tonic-gate CK_ULONG keylen, /* desired length of generated key */ 3280Sstevel@tonic-gate CK_ATTRIBUTE_TYPE operation, 3290Sstevel@tonic-gate CK_OBJECT_HANDLE *hKey) 3300Sstevel@tonic-gate { 3310Sstevel@tonic-gate CK_RV rv; 3320Sstevel@tonic-gate CK_PKCS5_PBKD2_PARAMS params; 3330Sstevel@tonic-gate CK_MECHANISM mechanism; 3340Sstevel@tonic-gate CK_OBJECT_CLASS class = CKO_SECRET_KEY; 3350Sstevel@tonic-gate CK_ATTRIBUTE tmpl[4]; 3360Sstevel@tonic-gate int attrs = 0; 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate mechanism.mechanism = CKM_PKCS5_PBKD2; 3390Sstevel@tonic-gate mechanism.pParameter = ¶ms; 3400Sstevel@tonic-gate mechanism.ulParameterLen = sizeof (params); 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate tmpl[attrs].type = CKA_CLASS; 3430Sstevel@tonic-gate tmpl[attrs].pValue = &class; 3440Sstevel@tonic-gate tmpl[attrs].ulValueLen = sizeof (class); 3450Sstevel@tonic-gate attrs++; 3460Sstevel@tonic-gate 3470Sstevel@tonic-gate tmpl[attrs].type = CKA_KEY_TYPE; 3480Sstevel@tonic-gate tmpl[attrs].pValue = &keytype; 3490Sstevel@tonic-gate tmpl[attrs].ulValueLen = sizeof (keytype); 3500Sstevel@tonic-gate attrs++; 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate tmpl[attrs].type = operation; 3530Sstevel@tonic-gate tmpl[attrs].pValue = &truevalue; 3540Sstevel@tonic-gate tmpl[attrs].ulValueLen = sizeof (CK_BBOOL); 3550Sstevel@tonic-gate attrs++; 3560Sstevel@tonic-gate 3570Sstevel@tonic-gate if (keylen > 0) { 3580Sstevel@tonic-gate tmpl[attrs].type = CKA_VALUE_LEN; 3590Sstevel@tonic-gate tmpl[attrs].pValue = &keylen; 3600Sstevel@tonic-gate tmpl[attrs].ulValueLen = sizeof (keylen); 3610Sstevel@tonic-gate attrs++; 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate params.saltSource = CKZ_SALT_SPECIFIED; 3650Sstevel@tonic-gate params.pSaltSourceData = (void *)pSaltData; 3660Sstevel@tonic-gate params.ulSaltSourceDataLen = saltLen; 3670Sstevel@tonic-gate params.iterations = iterations; 3680Sstevel@tonic-gate params.prf = CKP_PKCS5_PBKD2_HMAC_SHA1; 3690Sstevel@tonic-gate params.pPrfData = NULL; 3700Sstevel@tonic-gate params.ulPrfDataLen = 0; 3710Sstevel@tonic-gate params.pPassword = (CK_UTF8CHAR_PTR)pkeydata; 3720Sstevel@tonic-gate params.ulPasswordLen = &passwd_size; 3730Sstevel@tonic-gate 3740Sstevel@tonic-gate mechanism.mechanism = CKM_PKCS5_PBKD2; 3750Sstevel@tonic-gate mechanism.pParameter = ¶ms; 3760Sstevel@tonic-gate mechanism.ulParameterLen = sizeof (params); 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate rv = C_GenerateKey(hSession, &mechanism, tmpl, 3790Sstevel@tonic-gate attrs, hKey); 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate return (rv); 3820Sstevel@tonic-gate } 3830Sstevel@tonic-gate 3840Sstevel@tonic-gate 3850Sstevel@tonic-gate /* 3860Sstevel@tonic-gate * Execute the command. 3870Sstevel@tonic-gate * cmd - command pointing to type of operation. 3880Sstevel@tonic-gate * algo_str - alias of the algorithm passed. 3890Sstevel@tonic-gate */ 3900Sstevel@tonic-gate static int 3910Sstevel@tonic-gate execute_cmd(struct CommandInfo *cmd, char *algo_str) 3920Sstevel@tonic-gate { 3930Sstevel@tonic-gate CK_RV rv; 3940Sstevel@tonic-gate CK_ULONG slotcount; 3950Sstevel@tonic-gate CK_SLOT_ID slotID; 3960Sstevel@tonic-gate CK_SLOT_ID_PTR pSlotList = NULL; 3970Sstevel@tonic-gate CK_MECHANISM_TYPE mech_type = 0; 3980Sstevel@tonic-gate CK_MECHANISM_INFO info, kg_info; 3990Sstevel@tonic-gate CK_MECHANISM mech; 4000Sstevel@tonic-gate CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE; 4010Sstevel@tonic-gate CK_BYTE_PTR pkeydata = NULL; 4020Sstevel@tonic-gate CK_BYTE salt[PBKD2_SALT_SIZE]; 4030Sstevel@tonic-gate CK_ULONG keysize = 0; 4040Sstevel@tonic-gate int i, slot, mek; /* index variables */ 4050Sstevel@tonic-gate int status; 4060Sstevel@tonic-gate struct stat insbuf; /* stat buf for infile */ 4070Sstevel@tonic-gate struct stat outsbuf; /* stat buf for outfile */ 4080Sstevel@tonic-gate char tmpnam[PATH_MAX]; /* tmp file name */ 4090Sstevel@tonic-gate CK_OBJECT_HANDLE key = (CK_OBJECT_HANDLE) 0; 4100Sstevel@tonic-gate int infd = 0; /* input file, stdin default */ 4110Sstevel@tonic-gate int outfd = 1; /* output file, stdout default */ 4120Sstevel@tonic-gate char *outfilename = NULL; 4130Sstevel@tonic-gate boolean_t errflag = B_TRUE; 4140Sstevel@tonic-gate boolean_t inoutsame = B_FALSE; /* if both input & output are same */ 4150Sstevel@tonic-gate CK_BYTE_PTR pivbuf = NULL_PTR; 4160Sstevel@tonic-gate CK_ULONG ivlen = 0L; 4170Sstevel@tonic-gate int mech_match = 0; 4180Sstevel@tonic-gate CK_ULONG iterations = PBKD2_ITERATIONS; 4190Sstevel@tonic-gate CK_ULONG keylen; 4200Sstevel@tonic-gate int version = SUNW_ENCRYPT_FILE_VERSION; 4210Sstevel@tonic-gate CK_KEY_TYPE keytype; 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate if (aflag) { 4240Sstevel@tonic-gate /* Determine if algorithm is valid */ 4250Sstevel@tonic-gate for (mech_match = 0; mech_match < MECH_ALIASES_COUNT; 4260Sstevel@tonic-gate mech_match++) { 4270Sstevel@tonic-gate if (strcmp(algo_str, 4280Sstevel@tonic-gate mech_aliases[mech_match].alias) == 0) { 4290Sstevel@tonic-gate mech_type = mech_aliases[mech_match].type; 4300Sstevel@tonic-gate break; 4310Sstevel@tonic-gate } 4320Sstevel@tonic-gate } 4330Sstevel@tonic-gate 4340Sstevel@tonic-gate if (mech_match == MECH_ALIASES_COUNT) { 4350Sstevel@tonic-gate cryptoerror(LOG_STDERR, 4360Sstevel@tonic-gate gettext("unknown algorithm -- %s"), algo_str); 4370Sstevel@tonic-gate return (EXIT_FAILURE); 4380Sstevel@tonic-gate } 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate /* 4410Sstevel@tonic-gate * Process keyfile 4420Sstevel@tonic-gate * 4430Sstevel@tonic-gate * If a keyfile is provided, get the key data from 4440Sstevel@tonic-gate * the file. Otherwise, prompt for a passphrase. The 4450Sstevel@tonic-gate * passphrase is used as the key data. 4460Sstevel@tonic-gate */ 4470Sstevel@tonic-gate if (kflag) { 4480Sstevel@tonic-gate status = cryptoreadfile(keyfile, &pkeydata, &keysize); 4490Sstevel@tonic-gate } else { 4500Sstevel@tonic-gate status = cryptogetkey(&pkeydata, &keysize); 4510Sstevel@tonic-gate } 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate if (status == -1 || keysize == 0L) { 4540Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("invalid key.")); 4550Sstevel@tonic-gate return (EXIT_FAILURE); 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate } 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate bzero(salt, sizeof (salt)); 4600Sstevel@tonic-gate /* Initialize pkcs */ 4610Sstevel@tonic-gate if ((rv = C_Initialize(NULL)) != CKR_OK) { 4620Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("failed to initialize " 4630Sstevel@tonic-gate "PKCS #11 framework: %s"), pkcs11_strerror(rv)); 4640Sstevel@tonic-gate goto cleanup; 4650Sstevel@tonic-gate } 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate /* Get slot count */ 4680Sstevel@tonic-gate rv = C_GetSlotList(0, NULL_PTR, &slotcount); 4690Sstevel@tonic-gate if (rv != CKR_OK || slotcount == 0) { 4700Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 4710Sstevel@tonic-gate "failed to find any cryptographic provider," 4720Sstevel@tonic-gate "please check with your system administrator: %s"), 4730Sstevel@tonic-gate pkcs11_strerror(rv)); 4740Sstevel@tonic-gate goto cleanup; 4750Sstevel@tonic-gate } 4760Sstevel@tonic-gate 4770Sstevel@tonic-gate /* Found at least one slot, allocate memory for slot list */ 4780Sstevel@tonic-gate pSlotList = malloc(slotcount * sizeof (CK_SLOT_ID)); 4790Sstevel@tonic-gate if (pSlotList == NULL_PTR) { 4800Sstevel@tonic-gate int err = errno; 4810Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s"), strerror(err)); 4820Sstevel@tonic-gate goto cleanup; 4830Sstevel@tonic-gate } 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate /* Get the list of slots */ 4860Sstevel@tonic-gate if ((rv = C_GetSlotList(0, pSlotList, &slotcount)) != CKR_OK) { 4870Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 4880Sstevel@tonic-gate "failed to find any cryptographic provider," 4890Sstevel@tonic-gate "please check with your system administrator: %s"), 4900Sstevel@tonic-gate pkcs11_strerror(rv)); 4910Sstevel@tonic-gate goto cleanup; 4920Sstevel@tonic-gate } 4930Sstevel@tonic-gate 4940Sstevel@tonic-gate if (lflag) { 4950Sstevel@tonic-gate 4960Sstevel@tonic-gate /* Iterate through slots */ 4970Sstevel@tonic-gate for (slot = 0; slot < slotcount; slot++) { 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate /* Iterate through each mechanism */ 5000Sstevel@tonic-gate for (mek = 0; mek < MECH_ALIASES_COUNT; mek++) { 5010Sstevel@tonic-gate rv = C_GetMechanismInfo(pSlotList[slot], 5020Sstevel@tonic-gate mech_aliases[mek].type, &info); 5030Sstevel@tonic-gate 5040Sstevel@tonic-gate if (rv != CKR_OK) 5050Sstevel@tonic-gate continue; 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate /* 5080Sstevel@tonic-gate * Set to minimum/maximum key sizes assuming 5090Sstevel@tonic-gate * the values available are not 0. 5100Sstevel@tonic-gate */ 5110Sstevel@tonic-gate if (info.ulMinKeySize && (info.ulMinKeySize < 5120Sstevel@tonic-gate mech_aliases[mek].keysize_min)) 5130Sstevel@tonic-gate mech_aliases[mek].keysize_min = 5140Sstevel@tonic-gate info.ulMinKeySize; 5150Sstevel@tonic-gate 5160Sstevel@tonic-gate if (info.ulMaxKeySize && (info.ulMaxKeySize > 5170Sstevel@tonic-gate mech_aliases[mek].keysize_max)) 5180Sstevel@tonic-gate mech_aliases[mek].keysize_max = 5190Sstevel@tonic-gate info.ulMaxKeySize; 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate mech_aliases[mek].available = B_TRUE; 5220Sstevel@tonic-gate } 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate } 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate algorithm_list(); 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate errflag = B_FALSE; 5290Sstevel@tonic-gate goto cleanup; 5300Sstevel@tonic-gate } 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate /* Find a slot with matching mechanism */ 5330Sstevel@tonic-gate for (i = 0; i < slotcount; i++) { 5340Sstevel@tonic-gate slotID = pSlotList[i]; 5350Sstevel@tonic-gate rv = C_GetMechanismInfo(slotID, mech_type, &info); 5360Sstevel@tonic-gate if (rv != CKR_OK) { 5370Sstevel@tonic-gate continue; /* to the next slot */ 5380Sstevel@tonic-gate } else { 5390Sstevel@tonic-gate /* 5400Sstevel@tonic-gate * If the slot support the crypto, also 5410Sstevel@tonic-gate * make sure it supports the correct 5420Sstevel@tonic-gate * key generation mech if needed. 5430Sstevel@tonic-gate * 5440Sstevel@tonic-gate * We need PKCS5 when RC4 is used or 5450Sstevel@tonic-gate * when the key is entered on cmd line. 5460Sstevel@tonic-gate */ 5470Sstevel@tonic-gate if ((info.flags & cmd->flags) && 5480Sstevel@tonic-gate (mech_type == CKM_RC4) || (keyfile == NULL)) { 5490Sstevel@tonic-gate rv = C_GetMechanismInfo(slotID, 5500Sstevel@tonic-gate CKM_PKCS5_PBKD2, &kg_info); 5510Sstevel@tonic-gate if (rv == CKR_OK) 5520Sstevel@tonic-gate break; 5530Sstevel@tonic-gate } else if (info.flags & cmd->flags) { 5540Sstevel@tonic-gate break; 5550Sstevel@tonic-gate } 5560Sstevel@tonic-gate } 5570Sstevel@tonic-gate } 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate /* Show error if no matching mechanism found */ 5600Sstevel@tonic-gate if (i == slotcount) { 5610Sstevel@tonic-gate cryptoerror(LOG_STDERR, 5620Sstevel@tonic-gate gettext("no cryptographic provider was " 5630Sstevel@tonic-gate "found for this algorithm -- %s"), algo_str); 5640Sstevel@tonic-gate goto cleanup; 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate 5680Sstevel@tonic-gate /* Open a session */ 5690Sstevel@tonic-gate rv = C_OpenSession(slotID, CKF_SERIAL_SESSION, 5700Sstevel@tonic-gate NULL_PTR, NULL, &hSession); 5710Sstevel@tonic-gate 5720Sstevel@tonic-gate if (rv != CKR_OK) { 5730Sstevel@tonic-gate cryptoerror(LOG_STDERR, 5740Sstevel@tonic-gate gettext("can not open PKCS #11 session: %s"), 5750Sstevel@tonic-gate pkcs11_strerror(rv)); 5760Sstevel@tonic-gate goto cleanup; 5770Sstevel@tonic-gate } 5780Sstevel@tonic-gate 5790Sstevel@tonic-gate /* 5800Sstevel@tonic-gate * Generate IV data for encrypt. 5810Sstevel@tonic-gate */ 5820Sstevel@tonic-gate ivlen = mech_aliases[mech_match].ivlen; 5830Sstevel@tonic-gate if ((pivbuf = malloc((size_t)ivlen)) == NULL) { 5840Sstevel@tonic-gate int err = errno; 5850Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s"), 5860Sstevel@tonic-gate strerror(err)); 5870Sstevel@tonic-gate goto cleanup; 5880Sstevel@tonic-gate } 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate if (cmd->type == CKA_ENCRYPT) { 5910Sstevel@tonic-gate if ((get_random_data(pivbuf, 5920Sstevel@tonic-gate mech_aliases[mech_match].ivlen)) != 0) { 5930Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 5940Sstevel@tonic-gate "Unable to generate random " 5950Sstevel@tonic-gate "data for initialization vector.")); 5960Sstevel@tonic-gate goto cleanup; 5970Sstevel@tonic-gate } 5980Sstevel@tonic-gate } 5990Sstevel@tonic-gate 6000Sstevel@tonic-gate /* 6010Sstevel@tonic-gate * Create the key object 6020Sstevel@tonic-gate */ 6030Sstevel@tonic-gate rv = pkcs11_mech2keytype(mech_type, &keytype); 6040Sstevel@tonic-gate if (rv != CKR_OK) { 6050Sstevel@tonic-gate cryptoerror(LOG_STDERR, 6060Sstevel@tonic-gate gettext("unable to find key type for algorithm.")); 6070Sstevel@tonic-gate goto cleanup; 6080Sstevel@tonic-gate } 6090Sstevel@tonic-gate 6100Sstevel@tonic-gate /* Open input file */ 6110Sstevel@tonic-gate if (iflag) { 6120Sstevel@tonic-gate if ((infd = open(inputfile, O_RDONLY | O_NONBLOCK)) == -1) { 6130Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 6140Sstevel@tonic-gate "can not open input file %s"), inputfile); 6150Sstevel@tonic-gate goto cleanup; 6160Sstevel@tonic-gate } 6170Sstevel@tonic-gate 6180Sstevel@tonic-gate /* Get info on input file */ 6190Sstevel@tonic-gate if (fstat(infd, &insbuf) == -1) { 6200Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 6210Sstevel@tonic-gate "can not stat input file %s"), inputfile); 6220Sstevel@tonic-gate goto cleanup; 6230Sstevel@tonic-gate } 6240Sstevel@tonic-gate } 6250Sstevel@tonic-gate 6260Sstevel@tonic-gate /* 6270Sstevel@tonic-gate * Prepare output file 6280Sstevel@tonic-gate * If the input & output file are same, 6290Sstevel@tonic-gate * the output is written to a temp 6300Sstevel@tonic-gate * file first, then renamed to the original file 6310Sstevel@tonic-gate * after the crypt operation 6320Sstevel@tonic-gate */ 6330Sstevel@tonic-gate inoutsame = B_FALSE; 6340Sstevel@tonic-gate if (oflag) { 6350Sstevel@tonic-gate outfilename = outputfile; 6360Sstevel@tonic-gate if ((stat(outputfile, &outsbuf) != -1) && 6370Sstevel@tonic-gate (insbuf.st_ino == outsbuf.st_ino)) { 6380Sstevel@tonic-gate char *dir; 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate /* create temp file on same dir */ 6410Sstevel@tonic-gate dir = dirname(outputfile); 6420Sstevel@tonic-gate (void) snprintf(tmpnam, sizeof (tmpnam), 6430Sstevel@tonic-gate "%s/encrXXXXXX", dir); 6440Sstevel@tonic-gate outfilename = tmpnam; 6450Sstevel@tonic-gate if ((outfd = mkstemp(tmpnam)) == -1) { 6460Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 6470Sstevel@tonic-gate "cannot create temp file")); 6480Sstevel@tonic-gate goto cleanup; 6490Sstevel@tonic-gate } 6500Sstevel@tonic-gate inoutsame = B_TRUE; 6510Sstevel@tonic-gate } else { 6520Sstevel@tonic-gate /* Create file for output */ 6530Sstevel@tonic-gate if ((outfd = open(outfilename, 6540Sstevel@tonic-gate O_CREAT|O_WRONLY|O_TRUNC, 6550Sstevel@tonic-gate 0644)) == -1) { 6560Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 6570Sstevel@tonic-gate "cannot open output file %s"), 6580Sstevel@tonic-gate outfilename); 6590Sstevel@tonic-gate goto cleanup; 6600Sstevel@tonic-gate } 6610Sstevel@tonic-gate } 6620Sstevel@tonic-gate } 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate /* 6650Sstevel@tonic-gate * Read the version number from the head of the file 6660Sstevel@tonic-gate * to know how to interpret the data that follows. 6670Sstevel@tonic-gate */ 6680Sstevel@tonic-gate if (cmd->type == CKA_DECRYPT) { 6690Sstevel@tonic-gate if (read(infd, &version, sizeof (version)) != 6700Sstevel@tonic-gate sizeof (version)) { 6710Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 6720Sstevel@tonic-gate "failed to get format version from " 6730Sstevel@tonic-gate "input file.")); 6740Sstevel@tonic-gate goto cleanup; 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate /* convert to host byte order */ 6770Sstevel@tonic-gate version = ntohl(version); 6780Sstevel@tonic-gate 6790Sstevel@tonic-gate switch (version) { 6800Sstevel@tonic-gate case 1: 6810Sstevel@tonic-gate /* 6820Sstevel@tonic-gate * Version 1 output format: 6830Sstevel@tonic-gate * - Iterations used in key gen function (4 bytes) 6840Sstevel@tonic-gate * - IV ( 'ivlen' bytes) 6850Sstevel@tonic-gate * - Salt data used in key gen (16 bytes) 6860Sstevel@tonic-gate * 6870Sstevel@tonic-gate * An encrypted file has IV as first block (0 or 6880Sstevel@tonic-gate * more bytes depending on mechanism) followed 6890Sstevel@tonic-gate * by cipher text. Get the IV from the encrypted 6900Sstevel@tonic-gate * file. 6910Sstevel@tonic-gate */ 6920Sstevel@tonic-gate /* 6930Sstevel@tonic-gate * Read iteration count and salt data. 6940Sstevel@tonic-gate */ 6950Sstevel@tonic-gate if (read(infd, &iterations, 6960Sstevel@tonic-gate sizeof (iterations)) != 6970Sstevel@tonic-gate sizeof (iterations)) { 6980Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 6990Sstevel@tonic-gate "failed to get iterations from " 7000Sstevel@tonic-gate "input file.")); 7010Sstevel@tonic-gate goto cleanup; 7020Sstevel@tonic-gate } 7030Sstevel@tonic-gate /* convert to host byte order */ 7040Sstevel@tonic-gate iterations = ntohl(iterations); 7050Sstevel@tonic-gate if (ivlen > 0 && 7060Sstevel@tonic-gate read(infd, pivbuf, ivlen) != ivlen) { 7070Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 7080Sstevel@tonic-gate "failed to get initialization " 7090Sstevel@tonic-gate "vector from input file.")); 7100Sstevel@tonic-gate goto cleanup; 7110Sstevel@tonic-gate } 7120Sstevel@tonic-gate if (read(infd, salt, sizeof (salt)) 7130Sstevel@tonic-gate != sizeof (salt)) { 7140Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 7150Sstevel@tonic-gate "failed to get salt data from " 7160Sstevel@tonic-gate "input file.")); 7170Sstevel@tonic-gate goto cleanup; 7180Sstevel@tonic-gate } 7190Sstevel@tonic-gate break; 7200Sstevel@tonic-gate default: 7210Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 7220Sstevel@tonic-gate "Unrecognized format version read from " 7230Sstevel@tonic-gate "input file - expected %d, got %d."), 7240Sstevel@tonic-gate SUNW_ENCRYPT_FILE_VERSION, version); 7250Sstevel@tonic-gate goto cleanup; 7260Sstevel@tonic-gate break; 7270Sstevel@tonic-gate } 7280Sstevel@tonic-gate } 7290Sstevel@tonic-gate /* 7300Sstevel@tonic-gate * If encrypting, we need some random 7310Sstevel@tonic-gate * salt data to create the key. If decrypting, 7320Sstevel@tonic-gate * the salt should come from head of the file 7330Sstevel@tonic-gate * to be decrypted. 7340Sstevel@tonic-gate */ 7350Sstevel@tonic-gate if (cmd->type == CKA_ENCRYPT) { 7360Sstevel@tonic-gate rv = get_random_data(salt, sizeof (salt)); 7370Sstevel@tonic-gate if (rv != 0) { 7380Sstevel@tonic-gate cryptoerror(LOG_STDERR, 7390Sstevel@tonic-gate gettext("unable to generate random " 7400Sstevel@tonic-gate "data for key salt.")); 7410Sstevel@tonic-gate goto cleanup; 7420Sstevel@tonic-gate } 7430Sstevel@tonic-gate } 7440Sstevel@tonic-gate 7450Sstevel@tonic-gate /* 7460Sstevel@tonic-gate * If key input is read from a file, treat it as 7470Sstevel@tonic-gate * raw key data, unless it is to be used with RC4, 7480Sstevel@tonic-gate * in which case it must be used to generate a pkcs5 7490Sstevel@tonic-gate * key to address security concerns with RC4 keys. 7500Sstevel@tonic-gate */ 7510Sstevel@tonic-gate if (kflag && keyfile != NULL && keytype != CKK_RC4) { 7520Sstevel@tonic-gate CK_OBJECT_CLASS objclass = CKO_SECRET_KEY; 7530Sstevel@tonic-gate CK_ATTRIBUTE template[5]; 7540Sstevel@tonic-gate int nattr = 0; 7550Sstevel@tonic-gate 7560Sstevel@tonic-gate template[nattr].type = CKA_CLASS; 7570Sstevel@tonic-gate template[nattr].pValue = &objclass; 7580Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (objclass); 7590Sstevel@tonic-gate nattr++; 7600Sstevel@tonic-gate 7610Sstevel@tonic-gate template[nattr].type = CKA_KEY_TYPE; 7620Sstevel@tonic-gate template[nattr].pValue = &keytype; 7630Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (keytype); 7640Sstevel@tonic-gate nattr++; 7650Sstevel@tonic-gate 7660Sstevel@tonic-gate template[nattr].type = cmd->type; 7670Sstevel@tonic-gate template[nattr].pValue = &truevalue; 7680Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (truevalue); 7690Sstevel@tonic-gate nattr++; 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate template[nattr].type = CKA_TOKEN; 7720Sstevel@tonic-gate template[nattr].pValue = &falsevalue; 7730Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (falsevalue); 7740Sstevel@tonic-gate nattr++; 7750Sstevel@tonic-gate 7760Sstevel@tonic-gate template[nattr].type = CKA_VALUE; 7770Sstevel@tonic-gate template[nattr].pValue = pkeydata; 7780Sstevel@tonic-gate template[nattr].ulValueLen = keysize; 7790Sstevel@tonic-gate nattr++; 7800Sstevel@tonic-gate 7810Sstevel@tonic-gate rv = C_CreateObject(hSession, template, 7820Sstevel@tonic-gate nattr, &key); 7830Sstevel@tonic-gate } else { 7840Sstevel@tonic-gate /* 7850Sstevel@tonic-gate * If the encryption type has a fixed key length, 7860Sstevel@tonic-gate * then its not necessary to set the key length 7870Sstevel@tonic-gate * parameter when generating the key. 7880Sstevel@tonic-gate */ 7890Sstevel@tonic-gate if (keytype == CKK_DES || keytype == CKK_DES3) 7900Sstevel@tonic-gate keylen = 0; 7910Sstevel@tonic-gate else 7920Sstevel@tonic-gate keylen = 16; 7930Sstevel@tonic-gate 7940Sstevel@tonic-gate /* 7950Sstevel@tonic-gate * Generate a cryptographically secure key using 7960Sstevel@tonic-gate * the key read from the file given (-k keyfile) or 7970Sstevel@tonic-gate * the passphrase entered by the user. 7980Sstevel@tonic-gate */ 7990Sstevel@tonic-gate rv = generate_pkcs5_key(hSession, 8000Sstevel@tonic-gate salt, sizeof (salt), 8010Sstevel@tonic-gate iterations, 8020Sstevel@tonic-gate pkeydata, keytype, keysize, 8030Sstevel@tonic-gate keylen, cmd->type, &key); 8040Sstevel@tonic-gate } 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate if (rv != CKR_OK) { 8070Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 8080Sstevel@tonic-gate "failed to generate a key: %s"), 8090Sstevel@tonic-gate pkcs11_strerror(rv)); 8100Sstevel@tonic-gate goto cleanup; 8110Sstevel@tonic-gate } 8120Sstevel@tonic-gate 8130Sstevel@tonic-gate /* Setup up mechanism */ 8140Sstevel@tonic-gate mech.mechanism = mech_type; 8150Sstevel@tonic-gate mech.pParameter = (CK_VOID_PTR)pivbuf; 8160Sstevel@tonic-gate mech.ulParameterLen = ivlen; 8170Sstevel@tonic-gate 8180Sstevel@tonic-gate if ((rv = cmd->Init(hSession, &mech, key)) != CKR_OK) { 8190Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 8200Sstevel@tonic-gate "failed to initialize crypto operation: %s"), 8210Sstevel@tonic-gate pkcs11_strerror(rv)); 8220Sstevel@tonic-gate goto cleanup; 8230Sstevel@tonic-gate } 8240Sstevel@tonic-gate 8250Sstevel@tonic-gate /* Write the version header encrypt command */ 8260Sstevel@tonic-gate if (cmd->type == CKA_ENCRYPT) { 8270Sstevel@tonic-gate /* convert to network order for storage */ 8280Sstevel@tonic-gate int netversion = htonl(version); 8290Sstevel@tonic-gate CK_ULONG netiter; 8300Sstevel@tonic-gate 8310Sstevel@tonic-gate if (write(outfd, &netversion, sizeof (netversion)) 8320Sstevel@tonic-gate != sizeof (netversion)) { 8330Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 8340Sstevel@tonic-gate "failed to write version number " 8350Sstevel@tonic-gate "to output file.")); 8360Sstevel@tonic-gate goto cleanup; 8370Sstevel@tonic-gate } 8380Sstevel@tonic-gate /* 8390Sstevel@tonic-gate * Write the iteration and salt data, even if they 8400Sstevel@tonic-gate * were not used to generate a key. 8410Sstevel@tonic-gate */ 8420Sstevel@tonic-gate netiter = htonl(iterations); 8430Sstevel@tonic-gate if (write(outfd, &netiter, 8440Sstevel@tonic-gate sizeof (netiter)) != sizeof (netiter)) { 8450Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 8460Sstevel@tonic-gate "failed to write iterations to output")); 8470Sstevel@tonic-gate goto cleanup; 8480Sstevel@tonic-gate } 8490Sstevel@tonic-gate if (ivlen > 0 && 8500Sstevel@tonic-gate write(outfd, pivbuf, ivlen) != ivlen) { 8510Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 8520Sstevel@tonic-gate "failed to write initialization vector " 8530Sstevel@tonic-gate "to output")); 8540Sstevel@tonic-gate goto cleanup; 8550Sstevel@tonic-gate } 8560Sstevel@tonic-gate if (write(outfd, salt, sizeof (salt)) != sizeof (salt)) { 8570Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 8580Sstevel@tonic-gate "failed to write salt data to output")); 8590Sstevel@tonic-gate goto cleanup; 8600Sstevel@tonic-gate } 8610Sstevel@tonic-gate } 8620Sstevel@tonic-gate 863*1142Sjk115741 if (crypt_multipart(cmd, hSession, infd, outfd, insbuf.st_size) == -1) { 8640Sstevel@tonic-gate goto cleanup; 8650Sstevel@tonic-gate } 8660Sstevel@tonic-gate 8670Sstevel@tonic-gate errflag = B_FALSE; 8680Sstevel@tonic-gate 8690Sstevel@tonic-gate /* 8700Sstevel@tonic-gate * Clean up 8710Sstevel@tonic-gate */ 8720Sstevel@tonic-gate cleanup: 8730Sstevel@tonic-gate /* Clear the key data, so others cannot snoop */ 8740Sstevel@tonic-gate if (pkeydata != NULL) { 8750Sstevel@tonic-gate bzero(pkeydata, keysize); 8760Sstevel@tonic-gate free(pkeydata); 8770Sstevel@tonic-gate pkeydata = NULL; 8780Sstevel@tonic-gate } 8790Sstevel@tonic-gate 8800Sstevel@tonic-gate /* Destroy key object */ 8810Sstevel@tonic-gate if (key != (CK_OBJECT_HANDLE) 0) { 8820Sstevel@tonic-gate (void) C_DestroyObject(hSession, key); 8830Sstevel@tonic-gate } 8840Sstevel@tonic-gate 8850Sstevel@tonic-gate /* free allocated memory */ 8860Sstevel@tonic-gate if (pSlotList != NULL) 8870Sstevel@tonic-gate free(pSlotList); 8880Sstevel@tonic-gate if (pivbuf != NULL) 8890Sstevel@tonic-gate free(pivbuf); 8900Sstevel@tonic-gate 8910Sstevel@tonic-gate /* close all the files */ 892*1142Sjk115741 if (iflag && (infd != -1)) 8930Sstevel@tonic-gate (void) close(infd); 894*1142Sjk115741 if (oflag && (outfd != -1)) 8950Sstevel@tonic-gate (void) close(outfd); 8960Sstevel@tonic-gate 8970Sstevel@tonic-gate /* rename tmp output to input file */ 8980Sstevel@tonic-gate if (inoutsame) { 8990Sstevel@tonic-gate if (rename(outfilename, inputfile) == -1) { 9000Sstevel@tonic-gate (void) unlink(outfilename); 9010Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("rename failed.")); 9020Sstevel@tonic-gate } 9030Sstevel@tonic-gate } 9040Sstevel@tonic-gate 9050Sstevel@tonic-gate /* If error occurred, remove the output file */ 9060Sstevel@tonic-gate if (errflag && outfilename != NULL) { 9070Sstevel@tonic-gate (void) unlink(outfilename); 9080Sstevel@tonic-gate } 9090Sstevel@tonic-gate 9100Sstevel@tonic-gate /* close pkcs11 session */ 9110Sstevel@tonic-gate if (hSession != CK_INVALID_HANDLE) 9120Sstevel@tonic-gate (void) C_CloseSession(hSession); 9130Sstevel@tonic-gate 9140Sstevel@tonic-gate (void) C_Finalize(NULL); 9150Sstevel@tonic-gate 9160Sstevel@tonic-gate return (errflag); 9170Sstevel@tonic-gate } 9180Sstevel@tonic-gate 9190Sstevel@tonic-gate /* 9200Sstevel@tonic-gate * Function for printing progress bar when the verbose flag 9210Sstevel@tonic-gate * is set. 9220Sstevel@tonic-gate * 9230Sstevel@tonic-gate * The vertical bar is printed at 25, 50, and 75% complete. 9240Sstevel@tonic-gate * 9250Sstevel@tonic-gate * The function is passed the number of positions on the screen it needs to 9260Sstevel@tonic-gate * advance and loops. 9270Sstevel@tonic-gate */ 9280Sstevel@tonic-gate 9290Sstevel@tonic-gate static void 9300Sstevel@tonic-gate print_status(int pos_to_advance) 9310Sstevel@tonic-gate { 9320Sstevel@tonic-gate 9330Sstevel@tonic-gate while (pos_to_advance > 0) { 9340Sstevel@tonic-gate switch (status_pos) { 9350Sstevel@tonic-gate case 0: 9360Sstevel@tonic-gate (void) fprintf(stderr, gettext("[")); 9370Sstevel@tonic-gate break; 9380Sstevel@tonic-gate case 19: 9390Sstevel@tonic-gate case 39: 9400Sstevel@tonic-gate case 59: 9410Sstevel@tonic-gate (void) fprintf(stderr, gettext("|")); 9420Sstevel@tonic-gate break; 9430Sstevel@tonic-gate default: 9440Sstevel@tonic-gate (void) fprintf(stderr, gettext(".")); 9450Sstevel@tonic-gate } 9460Sstevel@tonic-gate pos_to_advance--; 9470Sstevel@tonic-gate status_pos++; 9480Sstevel@tonic-gate } 9490Sstevel@tonic-gate } 9500Sstevel@tonic-gate 9510Sstevel@tonic-gate /* 9520Sstevel@tonic-gate * Encrypt/Decrypt in multi part. 9530Sstevel@tonic-gate * 9540Sstevel@tonic-gate * This function reads the input file (infd) and writes the 9550Sstevel@tonic-gate * encrypted/decrypted output to file (outfd). 9560Sstevel@tonic-gate * 9570Sstevel@tonic-gate * cmd - pointing to commandinfo 9580Sstevel@tonic-gate * hSession - pkcs session 9590Sstevel@tonic-gate * infd - input file descriptor 9600Sstevel@tonic-gate * outfd - output file descriptor 9610Sstevel@tonic-gate * 9620Sstevel@tonic-gate */ 9630Sstevel@tonic-gate 9640Sstevel@tonic-gate static int 9650Sstevel@tonic-gate crypt_multipart(struct CommandInfo *cmd, CK_SESSION_HANDLE hSession, 966*1142Sjk115741 int infd, int outfd, off_t insize) 9670Sstevel@tonic-gate { 9680Sstevel@tonic-gate CK_RV rv; 9690Sstevel@tonic-gate CK_ULONG resultlen; 9700Sstevel@tonic-gate CK_ULONG resultbuflen; 9710Sstevel@tonic-gate CK_BYTE_PTR resultbuf; 9720Sstevel@tonic-gate CK_ULONG datalen; 9730Sstevel@tonic-gate CK_BYTE databuf[BUFFERSIZE]; 9740Sstevel@tonic-gate CK_BYTE outbuf[BUFFERSIZE+BLOCKSIZE]; 9750Sstevel@tonic-gate CK_ULONG status_index = 0; /* current total file size read */ 9760Sstevel@tonic-gate float status_last = 0.0; /* file size of last element used */ 9770Sstevel@tonic-gate float status_incr = 0.0; /* file size element increments */ 9780Sstevel@tonic-gate int pos; /* # of progress bar elements to be print */ 9790Sstevel@tonic-gate ssize_t nread; 9800Sstevel@tonic-gate boolean_t errflag = B_FALSE; 9810Sstevel@tonic-gate 9820Sstevel@tonic-gate datalen = sizeof (databuf); 9830Sstevel@tonic-gate resultbuflen = sizeof (outbuf); 9840Sstevel@tonic-gate resultbuf = outbuf; 9850Sstevel@tonic-gate 9860Sstevel@tonic-gate /* Divide into 79 increments for progress bar element spacing */ 9870Sstevel@tonic-gate if (vflag && iflag) 988*1142Sjk115741 status_incr = (insize / 79.0); 9890Sstevel@tonic-gate 9900Sstevel@tonic-gate while ((nread = read(infd, databuf, datalen)) > 0) { 9910Sstevel@tonic-gate 9920Sstevel@tonic-gate /* Start with the initial buffer */ 9930Sstevel@tonic-gate resultlen = resultbuflen; 9940Sstevel@tonic-gate rv = cmd->Update(hSession, databuf, (CK_ULONG)nread, 9950Sstevel@tonic-gate resultbuf, &resultlen); 9960Sstevel@tonic-gate 9970Sstevel@tonic-gate /* Need a bigger buffer? */ 9980Sstevel@tonic-gate if (rv == CKR_BUFFER_TOO_SMALL) { 9990Sstevel@tonic-gate 10000Sstevel@tonic-gate /* free the old buffer */ 10010Sstevel@tonic-gate if (resultbuf != NULL && resultbuf != outbuf) { 10020Sstevel@tonic-gate bzero(resultbuf, resultbuflen); 10030Sstevel@tonic-gate free(resultbuf); 10040Sstevel@tonic-gate } 10050Sstevel@tonic-gate 10060Sstevel@tonic-gate /* allocate a new big buffer */ 10070Sstevel@tonic-gate if ((resultbuf = malloc((size_t)resultlen)) == NULL) { 10080Sstevel@tonic-gate int err = errno; 10090Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s"), 10100Sstevel@tonic-gate strerror(err)); 10110Sstevel@tonic-gate return (-1); 10120Sstevel@tonic-gate } 10130Sstevel@tonic-gate resultbuflen = resultlen; 10140Sstevel@tonic-gate 10150Sstevel@tonic-gate /* Try again with bigger buffer */ 10160Sstevel@tonic-gate rv = cmd->Update(hSession, databuf, (CK_ULONG)nread, 10170Sstevel@tonic-gate resultbuf, &resultlen); 10180Sstevel@tonic-gate } 10190Sstevel@tonic-gate 10200Sstevel@tonic-gate if (rv != CKR_OK) { 10210Sstevel@tonic-gate errflag = B_TRUE; 10220Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 10230Sstevel@tonic-gate "crypto operation failed: %s"), 10240Sstevel@tonic-gate pkcs11_strerror(rv)); 10250Sstevel@tonic-gate break; 10260Sstevel@tonic-gate } 10270Sstevel@tonic-gate 10280Sstevel@tonic-gate /* write the output */ 10290Sstevel@tonic-gate if (write(outfd, resultbuf, resultlen) != resultlen) { 10300Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 10310Sstevel@tonic-gate "failed to write result to output file.")); 10320Sstevel@tonic-gate errflag = B_TRUE; 10330Sstevel@tonic-gate break; 10340Sstevel@tonic-gate } 10350Sstevel@tonic-gate 10360Sstevel@tonic-gate if (vflag) { 10370Sstevel@tonic-gate status_index += resultlen; 10380Sstevel@tonic-gate 10390Sstevel@tonic-gate /* 10400Sstevel@tonic-gate * If input is from stdin, do a our own progress bar 10410Sstevel@tonic-gate * by printing periods at a pre-defined increment 10420Sstevel@tonic-gate * until the file is done. 10430Sstevel@tonic-gate */ 10440Sstevel@tonic-gate if (!iflag) { 10450Sstevel@tonic-gate 10460Sstevel@tonic-gate /* 10470Sstevel@tonic-gate * Print at least 1 element in case the file 10480Sstevel@tonic-gate * is small, it looks better than nothing. 10490Sstevel@tonic-gate */ 10500Sstevel@tonic-gate if (status_pos == 0) { 10510Sstevel@tonic-gate (void) fprintf(stderr, gettext(".")); 10520Sstevel@tonic-gate status_pos = 1; 10530Sstevel@tonic-gate } 10540Sstevel@tonic-gate 10550Sstevel@tonic-gate if ((status_index - status_last) > 10560Sstevel@tonic-gate (PROGRESSSIZE)) { 10570Sstevel@tonic-gate (void) fprintf(stderr, gettext(".")); 10580Sstevel@tonic-gate status_last = status_index; 10590Sstevel@tonic-gate } 10600Sstevel@tonic-gate continue; 10610Sstevel@tonic-gate } 10620Sstevel@tonic-gate 10630Sstevel@tonic-gate /* Calculate the number of elements need to be print */ 1064*1142Sjk115741 if (insize <= BUFFERSIZE) 10650Sstevel@tonic-gate pos = 78; 10660Sstevel@tonic-gate else 10670Sstevel@tonic-gate pos = (int)((status_index - status_last) / 10680Sstevel@tonic-gate status_incr); 10690Sstevel@tonic-gate 10700Sstevel@tonic-gate /* Add progress bar elements, if needed */ 10710Sstevel@tonic-gate if (pos > 0) { 10720Sstevel@tonic-gate print_status(pos); 10730Sstevel@tonic-gate status_last += (status_incr * pos); 10740Sstevel@tonic-gate } 10750Sstevel@tonic-gate } 10760Sstevel@tonic-gate } 10770Sstevel@tonic-gate 10780Sstevel@tonic-gate /* Print verbose completion */ 10790Sstevel@tonic-gate if (vflag) { 10800Sstevel@tonic-gate if (iflag) 10810Sstevel@tonic-gate (void) fprintf(stderr, "]"); 10820Sstevel@tonic-gate 10830Sstevel@tonic-gate (void) fprintf(stderr, "\n%s\n", gettext("Done.")); 10840Sstevel@tonic-gate } 10850Sstevel@tonic-gate 10860Sstevel@tonic-gate /* Error in reading */ 10870Sstevel@tonic-gate if (nread == -1) { 10880Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 10890Sstevel@tonic-gate "error reading from input file")); 10900Sstevel@tonic-gate errflag = B_TRUE; 10910Sstevel@tonic-gate } 10920Sstevel@tonic-gate 10930Sstevel@tonic-gate if (!errflag) { 10940Sstevel@tonic-gate 10950Sstevel@tonic-gate /* Do the final part */ 10960Sstevel@tonic-gate 10970Sstevel@tonic-gate rv = cmd->Final(hSession, resultbuf, &resultlen); 10980Sstevel@tonic-gate 10990Sstevel@tonic-gate if (rv == CKR_OK) { 11000Sstevel@tonic-gate /* write the output */ 11010Sstevel@tonic-gate if (write(outfd, resultbuf, resultlen) != resultlen) { 11020Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 11030Sstevel@tonic-gate "failed to write result to output file.")); 11040Sstevel@tonic-gate errflag = B_TRUE; 11050Sstevel@tonic-gate } 11060Sstevel@tonic-gate } else { 11070Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 11080Sstevel@tonic-gate "crypto operation failed: %s"), 11090Sstevel@tonic-gate pkcs11_strerror(rv)); 11100Sstevel@tonic-gate errflag = B_TRUE; 11110Sstevel@tonic-gate } 11120Sstevel@tonic-gate 11130Sstevel@tonic-gate } 11140Sstevel@tonic-gate 11150Sstevel@tonic-gate if (resultbuf != NULL && resultbuf != outbuf) { 11160Sstevel@tonic-gate bzero(resultbuf, resultbuflen); 11170Sstevel@tonic-gate free(resultbuf); 11180Sstevel@tonic-gate } 11190Sstevel@tonic-gate 11200Sstevel@tonic-gate if (errflag) { 11210Sstevel@tonic-gate return (-1); 11220Sstevel@tonic-gate } else { 11230Sstevel@tonic-gate return (0); 11240Sstevel@tonic-gate } 11250Sstevel@tonic-gate } 11260Sstevel@tonic-gate 11270Sstevel@tonic-gate /* 11280Sstevel@tonic-gate * cryptoreadfile - reads file into a buffer 11290Sstevel@tonic-gate * This function can be used for reading files 11300Sstevel@tonic-gate * containing key or initialization vector data. 11310Sstevel@tonic-gate * 11320Sstevel@tonic-gate * filename - name of file 11330Sstevel@tonic-gate * pdata - entire file returned in this buffer 11340Sstevel@tonic-gate * must be freed by caller using free() 11350Sstevel@tonic-gate * pdatalen - length of data returned 11360Sstevel@tonic-gate * 11370Sstevel@tonic-gate * returns 0 if success, -1 if error 11380Sstevel@tonic-gate */ 11390Sstevel@tonic-gate static int 11400Sstevel@tonic-gate cryptoreadfile(char *filename, CK_BYTE_PTR *pdata, CK_ULONG_PTR pdatalen) 11410Sstevel@tonic-gate { 11420Sstevel@tonic-gate struct stat statbuf; 11430Sstevel@tonic-gate char *filebuf; 11440Sstevel@tonic-gate int filesize; 11450Sstevel@tonic-gate int fd; 11460Sstevel@tonic-gate 11470Sstevel@tonic-gate if (filename == NULL) 11480Sstevel@tonic-gate return (-1); 11490Sstevel@tonic-gate 11500Sstevel@tonic-gate /* read the file into a buffer */ 11510Sstevel@tonic-gate if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) == -1) { 11520Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 11530Sstevel@tonic-gate "cannot open %s"), filename); 11540Sstevel@tonic-gate return (-1); 11550Sstevel@tonic-gate 11560Sstevel@tonic-gate } 11570Sstevel@tonic-gate 11580Sstevel@tonic-gate if (fstat(fd, &statbuf) == -1) { 11590Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 11600Sstevel@tonic-gate "cannot stat %s"), filename); 11610Sstevel@tonic-gate (void) close(fd); 11620Sstevel@tonic-gate return (-1); 11630Sstevel@tonic-gate } 11640Sstevel@tonic-gate 1165871Scasper if (!S_ISREG(statbuf.st_mode)) { 11660Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 11670Sstevel@tonic-gate "%s not a regular file"), filename); 11680Sstevel@tonic-gate (void) close(fd); 11690Sstevel@tonic-gate return (-1); 11700Sstevel@tonic-gate } 11710Sstevel@tonic-gate 11720Sstevel@tonic-gate filesize = (size_t)statbuf.st_size; 11730Sstevel@tonic-gate 11740Sstevel@tonic-gate if (filesize == 0) { 11750Sstevel@tonic-gate (void) close(fd); 11760Sstevel@tonic-gate return (-1); 11770Sstevel@tonic-gate } 11780Sstevel@tonic-gate 11790Sstevel@tonic-gate /* allocate a buffer to hold the entire key */ 11800Sstevel@tonic-gate if ((filebuf = malloc(filesize)) == NULL) { 11810Sstevel@tonic-gate int err = errno; 11820Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s"), strerror(err)); 11830Sstevel@tonic-gate (void) close(fd); 11840Sstevel@tonic-gate return (-1); 11850Sstevel@tonic-gate } 11860Sstevel@tonic-gate 11870Sstevel@tonic-gate if (read(fd, filebuf, filesize) != filesize) { 11880Sstevel@tonic-gate int err = errno; 11890Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("error reading file: %s"), 11900Sstevel@tonic-gate strerror(err)); 11910Sstevel@tonic-gate (void) close(fd); 11920Sstevel@tonic-gate free(filebuf); 11930Sstevel@tonic-gate return (-1); 11940Sstevel@tonic-gate } 11950Sstevel@tonic-gate 11960Sstevel@tonic-gate (void) close(fd); 11970Sstevel@tonic-gate 11980Sstevel@tonic-gate *pdata = (CK_BYTE_PTR)filebuf; 11990Sstevel@tonic-gate *pdatalen = (CK_ULONG)filesize; 12000Sstevel@tonic-gate 12010Sstevel@tonic-gate return (0); 12020Sstevel@tonic-gate } 12030Sstevel@tonic-gate /* 12040Sstevel@tonic-gate * cryptogetkey - prompt user for a key 12050Sstevel@tonic-gate * 12060Sstevel@tonic-gate * pkeydata - buffer for returning key data 12070Sstevel@tonic-gate * must be freed by caller using free() 12080Sstevel@tonic-gate * pkeysize - size of buffer returned 12090Sstevel@tonic-gate * 12100Sstevel@tonic-gate * returns 12110Sstevel@tonic-gate * 0 for success, -1 for failure 12120Sstevel@tonic-gate */ 12130Sstevel@tonic-gate 12140Sstevel@tonic-gate static int 12150Sstevel@tonic-gate cryptogetkey(CK_BYTE_PTR *pkeydata, CK_ULONG_PTR pkeysize) 12160Sstevel@tonic-gate 12170Sstevel@tonic-gate { 12180Sstevel@tonic-gate char *keybuf; 12190Sstevel@tonic-gate char *tmpbuf; 12200Sstevel@tonic-gate 12210Sstevel@tonic-gate 12220Sstevel@tonic-gate 12230Sstevel@tonic-gate tmpbuf = getpassphrase(gettext("Enter key:")); 12240Sstevel@tonic-gate 12250Sstevel@tonic-gate if (tmpbuf == NULL) { 12260Sstevel@tonic-gate return (-1); /* error */ 12270Sstevel@tonic-gate } else { 12280Sstevel@tonic-gate keybuf = strdup(tmpbuf); 12290Sstevel@tonic-gate (void) memset(tmpbuf, 0, strlen(tmpbuf)); 12300Sstevel@tonic-gate } 12310Sstevel@tonic-gate 12320Sstevel@tonic-gate *pkeydata = (CK_BYTE_PTR)keybuf; 12330Sstevel@tonic-gate *pkeysize = (CK_ULONG)strlen(keybuf); 12340Sstevel@tonic-gate 12350Sstevel@tonic-gate 12360Sstevel@tonic-gate return (0); 12370Sstevel@tonic-gate } 12380Sstevel@tonic-gate 12390Sstevel@tonic-gate /* 12400Sstevel@tonic-gate * get_random_data - generate initialization vector data 12410Sstevel@tonic-gate * iv data is random bytes 12420Sstevel@tonic-gate * hSession - a pkcs session 12430Sstevel@tonic-gate * pivbuf - buffer where data is returned 12440Sstevel@tonic-gate * ivlen - size of iv data 12450Sstevel@tonic-gate */ 12460Sstevel@tonic-gate static int 12470Sstevel@tonic-gate get_random_data(CK_BYTE_PTR pivbuf, int ivlen) 12480Sstevel@tonic-gate { 12490Sstevel@tonic-gate int fd; 12500Sstevel@tonic-gate 12510Sstevel@tonic-gate if (ivlen == 0) { 12520Sstevel@tonic-gate /* nothing to generate */ 12530Sstevel@tonic-gate return (0); 12540Sstevel@tonic-gate } 12550Sstevel@tonic-gate 12560Sstevel@tonic-gate /* Read random data directly from /dev/random */ 12570Sstevel@tonic-gate if ((fd = open(RANDOM_DEVICE, O_RDONLY)) != -1) { 12580Sstevel@tonic-gate if (read(fd, pivbuf, (size_t)ivlen) == ivlen) { 12590Sstevel@tonic-gate (void) close(fd); 12600Sstevel@tonic-gate return (0); 12610Sstevel@tonic-gate } 12620Sstevel@tonic-gate } 12630Sstevel@tonic-gate (void) close(fd); 12640Sstevel@tonic-gate return (-1); 12650Sstevel@tonic-gate } 1266