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 /* 23*6535Sda73024 * Copyright 2008 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 * 36*6535Sda73024 * Usage: 37*6535Sda73024 * -a algorithm mechanism name without CKM_ prefix. Case 38*6535Sda73024 * does not matter 39*6535Sda73024 * -k keyfile file containing key data. If not specified user is 40*6535Sda73024 * prompted to enter key. key length > 0 is required 41*6535Sda73024 * -i infile input file to encrypt/decrypt. If omitted, stdin used. 42*6535Sda73024 * -o outfile output file to encrypt/decrypt. If omitted, stdout used. 43*6535Sda73024 * if infile & outfile are same, a temp file is used for 44*6535Sda73024 * output and infile is replaced with this file after 45*6535Sda73024 * operation is complete 46*6535Sda73024 * -l Display the list of algorithms 47*6535Sda73024 * -v Display verbose information 48*6535Sda73024 * -T tokenspec Specify a PKCS#11 token (optionally used with -K) 49*6535Sda73024 * -K keylabel Specify the symmetric PKCS#11 token key label 500Sstevel@tonic-gate * 510Sstevel@tonic-gate * Implementation notes: 52*6535Sda73024 * IV data - It is generated by random bytes equal to one block size. 530Sstevel@tonic-gate * 54*6535Sda73024 * Encrypted output format - 55*6535Sda73024 * - Output format version number (1) - 4 bytes in network byte order. 56*6535Sda73024 * - Iterations used in key gen function, 4 bytes in network byte order. 57*6535Sda73024 * - IV ('ivlen' bytes). Length is algorithm-dependent (see mech_aliases) 580Sstevel@tonic-gate * - Salt data used in key gen (16 bytes) 59*6535Sda73024 * - Cipher text data (remainder of the file) 600Sstevel@tonic-gate */ 610Sstevel@tonic-gate 620Sstevel@tonic-gate #include <stdio.h> 630Sstevel@tonic-gate #include <stdlib.h> 640Sstevel@tonic-gate #include <unistd.h> 650Sstevel@tonic-gate #include <errno.h> 660Sstevel@tonic-gate #include <fcntl.h> 670Sstevel@tonic-gate #include <ctype.h> 680Sstevel@tonic-gate #include <strings.h> 690Sstevel@tonic-gate #include <libintl.h> 700Sstevel@tonic-gate #include <libgen.h> 710Sstevel@tonic-gate #include <locale.h> 720Sstevel@tonic-gate #include <limits.h> 730Sstevel@tonic-gate #include <sys/types.h> 740Sstevel@tonic-gate #include <sys/stat.h> 750Sstevel@tonic-gate #include <netinet/in.h> 760Sstevel@tonic-gate #include <security/cryptoki.h> 770Sstevel@tonic-gate #include <cryptoutil.h> 783812Shylee #include <kmfapi.h> 790Sstevel@tonic-gate 800Sstevel@tonic-gate #define BUFFERSIZE (2048) /* Buffer size for reading file */ 810Sstevel@tonic-gate #define BLOCKSIZE (128) /* Largest guess for block size */ 820Sstevel@tonic-gate #define PROGRESSSIZE (BUFFERSIZE*20) /* stdin progress indicator size */ 830Sstevel@tonic-gate 840Sstevel@tonic-gate #define SUNW_ENCRYPT_FILE_VERSION 1 850Sstevel@tonic-gate 860Sstevel@tonic-gate /* 870Sstevel@tonic-gate * Exit Status codes 880Sstevel@tonic-gate */ 890Sstevel@tonic-gate #ifndef EXIT_SUCCESS 900Sstevel@tonic-gate #define EXIT_SUCCESS 0 /* No errors */ 910Sstevel@tonic-gate #define EXIT_FAILURE 1 /* All errors except usage */ 920Sstevel@tonic-gate #endif /* EXIT_SUCCESS */ 930Sstevel@tonic-gate 940Sstevel@tonic-gate #define EXIT_USAGE 2 /* usage/syntax error */ 950Sstevel@tonic-gate 960Sstevel@tonic-gate #define ENCRYPT_NAME "encrypt" /* name of encrypt command */ 973812Shylee #define ENCRYPT_OPTIONS "a:T:K:k:i:o:lv" /* options for encrypt */ 980Sstevel@tonic-gate #define DECRYPT_NAME "decrypt" /* name of decrypt command */ 993812Shylee #define DECRYPT_OPTIONS "a:T:K:k:i:o:lv" /* options for decrypt */ 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate /* 1020Sstevel@tonic-gate * Structure containing info for encrypt/decrypt 1030Sstevel@tonic-gate * command 1040Sstevel@tonic-gate */ 1050Sstevel@tonic-gate struct CommandInfo { 1060Sstevel@tonic-gate char *name; /* name of the command */ 1070Sstevel@tonic-gate char *options; /* command line options */ 1080Sstevel@tonic-gate CK_FLAGS flags; 1090Sstevel@tonic-gate CK_ATTRIBUTE_TYPE type; /* type of command */ 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate /* function pointers for various operations */ 1120Sstevel@tonic-gate CK_RV (*Init)(CK_SESSION_HANDLE, CK_MECHANISM_PTR, CK_OBJECT_HANDLE); 1130Sstevel@tonic-gate CK_RV (*Update)(CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG, CK_BYTE_PTR, 1140Sstevel@tonic-gate CK_ULONG_PTR); 1150Sstevel@tonic-gate CK_RV (*Crypt)(CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG, CK_BYTE_PTR, 1160Sstevel@tonic-gate CK_ULONG_PTR); 1170Sstevel@tonic-gate CK_RV (*Final)(CK_SESSION_HANDLE, CK_BYTE_PTR, CK_ULONG_PTR); 1180Sstevel@tonic-gate }; 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate static struct CommandInfo encrypt_cmd = { 1210Sstevel@tonic-gate ENCRYPT_NAME, 1220Sstevel@tonic-gate ENCRYPT_OPTIONS, 1230Sstevel@tonic-gate CKF_ENCRYPT, 1240Sstevel@tonic-gate CKA_ENCRYPT, 1250Sstevel@tonic-gate C_EncryptInit, 1260Sstevel@tonic-gate C_EncryptUpdate, 1270Sstevel@tonic-gate C_Encrypt, 1280Sstevel@tonic-gate C_EncryptFinal 1290Sstevel@tonic-gate }; 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate static struct CommandInfo decrypt_cmd = { 1320Sstevel@tonic-gate DECRYPT_NAME, 1330Sstevel@tonic-gate DECRYPT_OPTIONS, 1340Sstevel@tonic-gate CKF_DECRYPT, 1350Sstevel@tonic-gate CKA_DECRYPT, 1360Sstevel@tonic-gate C_DecryptInit, 1370Sstevel@tonic-gate C_DecryptUpdate, 1380Sstevel@tonic-gate C_Decrypt, 1390Sstevel@tonic-gate C_DecryptFinal 1400Sstevel@tonic-gate }; 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate struct mech_alias { 1430Sstevel@tonic-gate CK_MECHANISM_TYPE type; 1440Sstevel@tonic-gate char *alias; 1450Sstevel@tonic-gate CK_ULONG keysize_min; 1460Sstevel@tonic-gate CK_ULONG keysize_max; 1470Sstevel@tonic-gate int keysize_unit; 1480Sstevel@tonic-gate int ivlen; 1490Sstevel@tonic-gate boolean_t available; 1500Sstevel@tonic-gate }; 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate #define MECH_ALIASES_COUNT 4 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate static struct mech_alias mech_aliases[] = { 1550Sstevel@tonic-gate { CKM_AES_CBC_PAD, "aes", ULONG_MAX, 0L, 8, 16, B_FALSE }, 1560Sstevel@tonic-gate { CKM_RC4, "arcfour", ULONG_MAX, 0L, 1, 0, B_FALSE }, 1570Sstevel@tonic-gate { CKM_DES_CBC_PAD, "des", 8, 8, 8, 8, B_FALSE }, 1580Sstevel@tonic-gate { CKM_DES3_CBC_PAD, "3des", 24, 24, 8, 8, B_FALSE }, 1590Sstevel@tonic-gate }; 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate static CK_BBOOL truevalue = TRUE; 1620Sstevel@tonic-gate static CK_BBOOL falsevalue = FALSE; 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate static boolean_t aflag = B_FALSE; /* -a <algorithm> flag, required */ 1650Sstevel@tonic-gate static boolean_t kflag = B_FALSE; /* -k <keyfile> flag */ 1660Sstevel@tonic-gate static boolean_t iflag = B_FALSE; /* -i <infile> flag, use stdin if absent */ 1670Sstevel@tonic-gate static boolean_t oflag = B_FALSE; /* -o <outfile> flag, use stdout if absent */ 1680Sstevel@tonic-gate static boolean_t lflag = B_FALSE; /* -l flag (list) */ 1690Sstevel@tonic-gate static boolean_t vflag = B_FALSE; /* -v flag (verbose) */ 170*6535Sda73024 static boolean_t Tflag = B_FALSE; /* -T flag (tokenspec) */ 171*6535Sda73024 static boolean_t Kflag = B_FALSE; /* -K flag (keylabel) */ 1720Sstevel@tonic-gate 173*6535Sda73024 static char *keyfile = NULL; /* name of keyfile */ 174*6535Sda73024 static char *inputfile = NULL; /* name of input file */ 175*6535Sda73024 static char *outputfile = NULL; /* name of output file */ 176*6535Sda73024 static char *token_label = NULL; /* name of PKCS#11 token */ 177*6535Sda73024 static char *key_label = NULL; /* name of PKCS#11 token key label */ 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate static int status_pos = 0; /* current position of progress bar element */ 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate /* 1820Sstevel@tonic-gate * function prototypes 1830Sstevel@tonic-gate */ 1840Sstevel@tonic-gate static void usage(struct CommandInfo *cmd); 1850Sstevel@tonic-gate static int execute_cmd(struct CommandInfo *cmd, char *algo_str); 1860Sstevel@tonic-gate static int crypt_multipart(struct CommandInfo *cmd, CK_SESSION_HANDLE hSession, 1871142Sjk115741 int infd, int outfd, off_t insize); 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate int 1900Sstevel@tonic-gate main(int argc, char **argv) 1910Sstevel@tonic-gate { 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate extern char *optarg; 1940Sstevel@tonic-gate extern int optind; 1950Sstevel@tonic-gate char *optstr; 1960Sstevel@tonic-gate char c; /* current getopts flag */ 1970Sstevel@tonic-gate char *algo_str = NULL; /* algorithm string */ 1980Sstevel@tonic-gate struct CommandInfo *cmd; 1990Sstevel@tonic-gate char *cmdname; /* name of command */ 2000Sstevel@tonic-gate boolean_t errflag = B_FALSE; 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 203*6535Sda73024 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 2040Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 2050Sstevel@tonic-gate #endif 2060Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate /* 2090Sstevel@tonic-gate * Based on command name, determine 2100Sstevel@tonic-gate * type of command. 2110Sstevel@tonic-gate */ 2120Sstevel@tonic-gate cmdname = basename(argv[0]); 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate cryptodebug_init(cmdname); 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate if (strcmp(cmdname, encrypt_cmd.name) == 0) { 2170Sstevel@tonic-gate cmd = &encrypt_cmd; 2180Sstevel@tonic-gate } else if (strcmp(cmdname, decrypt_cmd.name) == 0) { 2190Sstevel@tonic-gate cmd = &decrypt_cmd; 2200Sstevel@tonic-gate } else { 2210Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 2220Sstevel@tonic-gate "command name must be either encrypt or decrypt")); 2230Sstevel@tonic-gate exit(EXIT_USAGE); 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate optstr = cmd->options; 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate /* Parse command line arguments */ 2290Sstevel@tonic-gate while (!errflag && (c = getopt(argc, argv, optstr)) != -1) { 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate switch (c) { 2320Sstevel@tonic-gate case 'a': 2330Sstevel@tonic-gate aflag = B_TRUE; 2340Sstevel@tonic-gate algo_str = optarg; 2350Sstevel@tonic-gate break; 2360Sstevel@tonic-gate case 'k': 2370Sstevel@tonic-gate kflag = B_TRUE; 2380Sstevel@tonic-gate keyfile = optarg; 2390Sstevel@tonic-gate break; 2403812Shylee case 'T': 2413812Shylee Tflag = B_TRUE; 2423812Shylee token_label = optarg; 2433812Shylee break; 2443812Shylee case 'K': 2453812Shylee Kflag = B_TRUE; 2463812Shylee key_label = optarg; 2473812Shylee break; 2480Sstevel@tonic-gate case 'i': 2490Sstevel@tonic-gate iflag = B_TRUE; 2500Sstevel@tonic-gate inputfile = optarg; 2510Sstevel@tonic-gate break; 2520Sstevel@tonic-gate case 'o': 2530Sstevel@tonic-gate oflag = B_TRUE; 2540Sstevel@tonic-gate outputfile = optarg; 2550Sstevel@tonic-gate break; 2560Sstevel@tonic-gate case 'l': 2570Sstevel@tonic-gate lflag = B_TRUE; 2580Sstevel@tonic-gate break; 2590Sstevel@tonic-gate case 'v': 2600Sstevel@tonic-gate vflag = B_TRUE; 2610Sstevel@tonic-gate break; 2620Sstevel@tonic-gate default: 2630Sstevel@tonic-gate errflag = B_TRUE; 2640Sstevel@tonic-gate } 2650Sstevel@tonic-gate } 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate if (errflag || (!aflag && !lflag) || (lflag && argc > 2) || 2683812Shylee (kflag && Kflag) || (Tflag && !Kflag) || 2690Sstevel@tonic-gate (optind < argc)) { 2700Sstevel@tonic-gate usage(cmd); 2710Sstevel@tonic-gate exit(EXIT_USAGE); 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate return (execute_cmd(cmd, algo_str)); 2750Sstevel@tonic-gate } 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate /* 2780Sstevel@tonic-gate * usage message 2790Sstevel@tonic-gate */ 2800Sstevel@tonic-gate static void 2810Sstevel@tonic-gate usage(struct CommandInfo *cmd) 2820Sstevel@tonic-gate { 2833812Shylee (void) fprintf(stderr, gettext("Usage:\n")); 2840Sstevel@tonic-gate if (cmd->type == CKA_ENCRYPT) { 2853812Shylee (void) fprintf(stderr, gettext(" encrypt -l\n")); 2863812Shylee (void) fprintf(stderr, gettext(" encrypt -a <algorithm> " 2873812Shylee "[-v] [-k <keyfile> | -K <keylabel> [-T <tokenspec>]] " 2883812Shylee "[-i <infile>] [-o <outfile>]\n")); 2893812Shylee 2900Sstevel@tonic-gate } else { 2913812Shylee (void) fprintf(stderr, gettext(" decrypt -l\n")); 2923812Shylee (void) fprintf(stderr, gettext(" decrypt -a <algorithm> " 2933812Shylee "[-v] [-k <keyfile> | -K <keylabel> [-T <tokenspec>]] " 2943812Shylee "[-i <infile>] [-o <outfile>]\n")); 2950Sstevel@tonic-gate } 2960Sstevel@tonic-gate } 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate /* 2990Sstevel@tonic-gate * Print out list of algorithms in default and verbose mode 3000Sstevel@tonic-gate */ 3010Sstevel@tonic-gate static void 3020Sstevel@tonic-gate algorithm_list() 3030Sstevel@tonic-gate { 3040Sstevel@tonic-gate int mech; 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate (void) printf(gettext("Algorithm Keysize: Min Max (bits)\n" 3070Sstevel@tonic-gate "------------------------------------------\n")); 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate for (mech = 0; mech < MECH_ALIASES_COUNT; mech++) { 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate if (mech_aliases[mech].available == B_FALSE) 3120Sstevel@tonic-gate continue; 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate (void) printf("%-15s", mech_aliases[mech].alias); 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate if (mech_aliases[mech].keysize_min != UINT_MAX && 3170Sstevel@tonic-gate mech_aliases[mech].keysize_max != 0) 3180Sstevel@tonic-gate (void) printf(" %5lu %5lu\n", 3190Sstevel@tonic-gate (mech_aliases[mech].keysize_min * 3205051Swyllys mech_aliases[mech].keysize_unit), 3210Sstevel@tonic-gate (mech_aliases[mech].keysize_max * 3225051Swyllys mech_aliases[mech].keysize_unit)); 3230Sstevel@tonic-gate else 3240Sstevel@tonic-gate (void) printf("\n"); 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate } 3270Sstevel@tonic-gate } 3280Sstevel@tonic-gate 3293812Shylee /* 3303812Shylee * This function will login into the token with the provided password and 3313812Shylee * find the token key object with the specified keytype and keylabel. 3323812Shylee */ 3333812Shylee static int 3343812Shylee get_token_key(CK_SESSION_HANDLE hSession, CK_KEY_TYPE keytype, 3353812Shylee char *keylabel, CK_BYTE *password, int password_len, 3363812Shylee CK_OBJECT_HANDLE *keyobj) 3373812Shylee { 3383812Shylee CK_RV rv; 3393812Shylee CK_ATTRIBUTE pTmpl[10]; 3403812Shylee CK_OBJECT_CLASS class = CKO_SECRET_KEY; 3413812Shylee CK_BBOOL true = 1; 3423812Shylee CK_BBOOL is_token = 1; 3433812Shylee CK_ULONG key_obj_count = 1; 3443812Shylee int i; 3453812Shylee CK_KEY_TYPE ckKeyType = keytype; 3463812Shylee 3473812Shylee 3483812Shylee rv = C_Login(hSession, CKU_USER, (CK_UTF8CHAR_PTR)password, 3493812Shylee (CK_ULONG)password_len); 3503812Shylee if (rv != CKR_OK) { 3513812Shylee (void) fprintf(stderr, "Cannot login to the token." 3523812Shylee " error = %s\n", pkcs11_strerror(rv)); 3533812Shylee return (-1); 3543812Shylee } 3553812Shylee 3563812Shylee i = 0; 3573812Shylee pTmpl[i].type = CKA_TOKEN; 3583812Shylee pTmpl[i].pValue = &is_token; 3593812Shylee pTmpl[i].ulValueLen = sizeof (CK_BBOOL); 3603812Shylee i++; 3613812Shylee 3623812Shylee pTmpl[i].type = CKA_CLASS; 3633812Shylee pTmpl[i].pValue = &class; 3643812Shylee pTmpl[i].ulValueLen = sizeof (class); 3653812Shylee i++; 3663812Shylee 3673812Shylee pTmpl[i].type = CKA_LABEL; 3683812Shylee pTmpl[i].pValue = keylabel; 3693812Shylee pTmpl[i].ulValueLen = strlen(keylabel); 3703812Shylee i++; 3713812Shylee 3723812Shylee pTmpl[i].type = CKA_KEY_TYPE; 3733812Shylee pTmpl[i].pValue = &ckKeyType; 3743812Shylee pTmpl[i].ulValueLen = sizeof (ckKeyType); 3753812Shylee i++; 3763812Shylee 3773812Shylee pTmpl[i].type = CKA_PRIVATE; 3783812Shylee pTmpl[i].pValue = &true; 3793812Shylee pTmpl[i].ulValueLen = sizeof (true); 3803812Shylee i++; 3813812Shylee 3823812Shylee rv = C_FindObjectsInit(hSession, pTmpl, i); 3833812Shylee if (rv != CKR_OK) { 3843812Shylee goto out; 3853812Shylee } 3863812Shylee 3873812Shylee rv = C_FindObjects(hSession, keyobj, 1, &key_obj_count); 3883812Shylee 3893812Shylee (void) C_FindObjectsFinal(hSession); 3903812Shylee 3913812Shylee out: 3923812Shylee if (rv != CKR_OK) { 3933812Shylee (void) fprintf(stderr, 3943812Shylee "Cannot retrieve key object. error = %s\n", 3953812Shylee pkcs11_strerror(rv)); 3963812Shylee return (-1); 3973812Shylee } 3983812Shylee 3993812Shylee if (key_obj_count == 0) { 4003812Shylee (void) fprintf(stderr, "Cannot find the key object.\n"); 4013812Shylee return (-1); 4023812Shylee } 4033812Shylee 4043812Shylee return (0); 4053812Shylee } 4063812Shylee 4070Sstevel@tonic-gate 4080Sstevel@tonic-gate /* 4090Sstevel@tonic-gate * Execute the command. 4100Sstevel@tonic-gate * cmd - command pointing to type of operation. 4110Sstevel@tonic-gate * algo_str - alias of the algorithm passed. 4120Sstevel@tonic-gate */ 4130Sstevel@tonic-gate static int 4140Sstevel@tonic-gate execute_cmd(struct CommandInfo *cmd, char *algo_str) 4150Sstevel@tonic-gate { 4160Sstevel@tonic-gate CK_RV rv; 4170Sstevel@tonic-gate CK_ULONG slotcount; 4180Sstevel@tonic-gate CK_SLOT_ID slotID; 4190Sstevel@tonic-gate CK_SLOT_ID_PTR pSlotList = NULL; 4200Sstevel@tonic-gate CK_MECHANISM_TYPE mech_type = 0; 4210Sstevel@tonic-gate CK_MECHANISM_INFO info, kg_info; 4220Sstevel@tonic-gate CK_MECHANISM mech; 4230Sstevel@tonic-gate CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE; 4240Sstevel@tonic-gate CK_BYTE_PTR pkeydata = NULL; 4255252Sdinak CK_BYTE salt[CK_PKCS5_PBKD2_SALT_SIZE]; 4260Sstevel@tonic-gate CK_ULONG keysize = 0; 4270Sstevel@tonic-gate int i, slot, mek; /* index variables */ 4280Sstevel@tonic-gate int status; 4290Sstevel@tonic-gate struct stat insbuf; /* stat buf for infile */ 4300Sstevel@tonic-gate struct stat outsbuf; /* stat buf for outfile */ 4310Sstevel@tonic-gate char tmpnam[PATH_MAX]; /* tmp file name */ 4320Sstevel@tonic-gate CK_OBJECT_HANDLE key = (CK_OBJECT_HANDLE) 0; 4330Sstevel@tonic-gate int infd = 0; /* input file, stdin default */ 4340Sstevel@tonic-gate int outfd = 1; /* output file, stdout default */ 4350Sstevel@tonic-gate char *outfilename = NULL; 4360Sstevel@tonic-gate boolean_t errflag = B_TRUE; 4370Sstevel@tonic-gate boolean_t inoutsame = B_FALSE; /* if both input & output are same */ 4380Sstevel@tonic-gate CK_BYTE_PTR pivbuf = NULL_PTR; 4390Sstevel@tonic-gate CK_ULONG ivlen = 0L; 440*6535Sda73024 int mech_match = 0; 441*6535Sda73024 uint32_t iterations = CK_PKCS5_PBKD2_ITERATIONS; 4420Sstevel@tonic-gate CK_ULONG keylen; 443*6535Sda73024 uint32_t version = SUNW_ENCRYPT_FILE_VERSION; 4440Sstevel@tonic-gate CK_KEY_TYPE keytype; 4453812Shylee KMF_RETURN kmfrv; 4463812Shylee CK_SLOT_ID token_slot_id; 4470Sstevel@tonic-gate 4480Sstevel@tonic-gate if (aflag) { 4490Sstevel@tonic-gate /* Determine if algorithm is valid */ 4500Sstevel@tonic-gate for (mech_match = 0; mech_match < MECH_ALIASES_COUNT; 4515051Swyllys mech_match++) { 4520Sstevel@tonic-gate if (strcmp(algo_str, 4530Sstevel@tonic-gate mech_aliases[mech_match].alias) == 0) { 4540Sstevel@tonic-gate mech_type = mech_aliases[mech_match].type; 4550Sstevel@tonic-gate break; 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate } 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate if (mech_match == MECH_ALIASES_COUNT) { 4600Sstevel@tonic-gate cryptoerror(LOG_STDERR, 4610Sstevel@tonic-gate gettext("unknown algorithm -- %s"), algo_str); 4620Sstevel@tonic-gate return (EXIT_FAILURE); 4630Sstevel@tonic-gate } 4640Sstevel@tonic-gate 4650Sstevel@tonic-gate /* 4663812Shylee * Process keyfile or get the token pin if -K is specified. 4670Sstevel@tonic-gate * 4680Sstevel@tonic-gate * If a keyfile is provided, get the key data from 4690Sstevel@tonic-gate * the file. Otherwise, prompt for a passphrase. The 4700Sstevel@tonic-gate * passphrase is used as the key data. 4710Sstevel@tonic-gate */ 4723812Shylee if (Kflag) { 4733812Shylee /* get the pin of the token */ 4743812Shylee if (token_label == NULL || !strlen(token_label)) { 4755252Sdinak token_label = pkcs11_default_token(); 4763812Shylee } 4773812Shylee 4785252Sdinak status = pkcs11_get_pass(token_label, 4795252Sdinak (char **)&pkeydata, (size_t *)&keysize, 0, B_FALSE); 4803812Shylee } else if (kflag) { 4813812Shylee /* get the key file */ 4825252Sdinak status = pkcs11_read_data(keyfile, (void **)&pkeydata, 4835252Sdinak (size_t *)&keysize); 4840Sstevel@tonic-gate } else { 4853812Shylee /* get the key from input */ 4865252Sdinak status = pkcs11_get_pass(NULL, (char **)&pkeydata, 4875252Sdinak (size_t *)&keysize, 0, B_FALSE); 4880Sstevel@tonic-gate } 4890Sstevel@tonic-gate 4900Sstevel@tonic-gate if (status == -1 || keysize == 0L) { 4913812Shylee cryptoerror(LOG_STDERR, 4923812Shylee Kflag ? gettext("invalid password.") : 4933812Shylee gettext("invalid key.")); 4940Sstevel@tonic-gate return (EXIT_FAILURE); 4950Sstevel@tonic-gate } 4960Sstevel@tonic-gate } 4970Sstevel@tonic-gate 4980Sstevel@tonic-gate bzero(salt, sizeof (salt)); 4990Sstevel@tonic-gate /* Initialize pkcs */ 5003812Shylee rv = C_Initialize(NULL); 5013812Shylee if (rv != CKR_OK && rv != CKR_CRYPTOKI_ALREADY_INITIALIZED) { 5020Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("failed to initialize " 5030Sstevel@tonic-gate "PKCS #11 framework: %s"), pkcs11_strerror(rv)); 5040Sstevel@tonic-gate goto cleanup; 5050Sstevel@tonic-gate } 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate /* Get slot count */ 5080Sstevel@tonic-gate rv = C_GetSlotList(0, NULL_PTR, &slotcount); 5090Sstevel@tonic-gate if (rv != CKR_OK || slotcount == 0) { 5100Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 5110Sstevel@tonic-gate "failed to find any cryptographic provider," 5120Sstevel@tonic-gate "please check with your system administrator: %s"), 5130Sstevel@tonic-gate pkcs11_strerror(rv)); 5140Sstevel@tonic-gate goto cleanup; 5150Sstevel@tonic-gate } 5160Sstevel@tonic-gate 5170Sstevel@tonic-gate /* Found at least one slot, allocate memory for slot list */ 5180Sstevel@tonic-gate pSlotList = malloc(slotcount * sizeof (CK_SLOT_ID)); 5190Sstevel@tonic-gate if (pSlotList == NULL_PTR) { 5200Sstevel@tonic-gate int err = errno; 5210Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s"), strerror(err)); 5220Sstevel@tonic-gate goto cleanup; 5230Sstevel@tonic-gate } 5240Sstevel@tonic-gate 5250Sstevel@tonic-gate /* Get the list of slots */ 5260Sstevel@tonic-gate if ((rv = C_GetSlotList(0, pSlotList, &slotcount)) != CKR_OK) { 5270Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 5280Sstevel@tonic-gate "failed to find any cryptographic provider," 5290Sstevel@tonic-gate "please check with your system administrator: %s"), 5300Sstevel@tonic-gate pkcs11_strerror(rv)); 5310Sstevel@tonic-gate goto cleanup; 5320Sstevel@tonic-gate } 5330Sstevel@tonic-gate 5340Sstevel@tonic-gate if (lflag) { 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate /* Iterate through slots */ 5370Sstevel@tonic-gate for (slot = 0; slot < slotcount; slot++) { 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate /* Iterate through each mechanism */ 5400Sstevel@tonic-gate for (mek = 0; mek < MECH_ALIASES_COUNT; mek++) { 5410Sstevel@tonic-gate rv = C_GetMechanismInfo(pSlotList[slot], 5420Sstevel@tonic-gate mech_aliases[mek].type, &info); 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate if (rv != CKR_OK) 5450Sstevel@tonic-gate continue; 5460Sstevel@tonic-gate 5470Sstevel@tonic-gate /* 5480Sstevel@tonic-gate * Set to minimum/maximum key sizes assuming 5490Sstevel@tonic-gate * the values available are not 0. 5500Sstevel@tonic-gate */ 5510Sstevel@tonic-gate if (info.ulMinKeySize && (info.ulMinKeySize < 5520Sstevel@tonic-gate mech_aliases[mek].keysize_min)) 5530Sstevel@tonic-gate mech_aliases[mek].keysize_min = 5545051Swyllys info.ulMinKeySize; 5550Sstevel@tonic-gate 5560Sstevel@tonic-gate if (info.ulMaxKeySize && (info.ulMaxKeySize > 5570Sstevel@tonic-gate mech_aliases[mek].keysize_max)) 5580Sstevel@tonic-gate mech_aliases[mek].keysize_max = 5595051Swyllys info.ulMaxKeySize; 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate mech_aliases[mek].available = B_TRUE; 5620Sstevel@tonic-gate } 5630Sstevel@tonic-gate 5640Sstevel@tonic-gate } 5650Sstevel@tonic-gate 5660Sstevel@tonic-gate algorithm_list(); 5670Sstevel@tonic-gate 5680Sstevel@tonic-gate errflag = B_FALSE; 5690Sstevel@tonic-gate goto cleanup; 5700Sstevel@tonic-gate } 5710Sstevel@tonic-gate 5723812Shylee 5733812Shylee /* 5743812Shylee * Find a slot with matching mechanism 5753812Shylee * 5763812Shylee * If -K is specified, we find the slot id for the token first, then 5773812Shylee * check if the slot supports the algorithm. 5783812Shylee */ 5793812Shylee i = 0; 5803812Shylee if (Kflag) { 5815051Swyllys kmfrv = kmf_pk11_token_lookup(NULL, token_label, 5825051Swyllys &token_slot_id); 5833812Shylee if (kmfrv != KMF_OK) { 5843812Shylee cryptoerror(LOG_STDERR, 5853812Shylee gettext("no matching PKCS#11 token")); 5863812Shylee errflag = B_TRUE; 5873812Shylee goto cleanup; 5883812Shylee } 5893812Shylee rv = C_GetMechanismInfo(token_slot_id, mech_type, &info); 5903812Shylee if (rv == CKR_OK && (info.flags & cmd->flags)) 5913812Shylee slotID = token_slot_id; 5923812Shylee else 5933812Shylee i = slotcount; 5943812Shylee } else { 5953812Shylee for (i = 0; i < slotcount; i++) { 5963812Shylee slotID = pSlotList[i]; 5973812Shylee rv = C_GetMechanismInfo(slotID, mech_type, &info); 5983812Shylee if (rv != CKR_OK) { 5993812Shylee continue; /* to the next slot */ 6003812Shylee } else { 6013812Shylee /* 6023812Shylee * If the slot support the crypto, also 6033812Shylee * make sure it supports the correct 6043812Shylee * key generation mech if needed. 6053812Shylee * 6063812Shylee * We need PKCS5 when RC4 is used or 6073812Shylee * when the key is entered on cmd line. 6083812Shylee */ 6093812Shylee if ((info.flags & cmd->flags) && 6103812Shylee (mech_type == CKM_RC4) || 6113812Shylee (keyfile == NULL)) { 6123812Shylee rv = C_GetMechanismInfo(slotID, 6133812Shylee CKM_PKCS5_PBKD2, &kg_info); 6143812Shylee if (rv == CKR_OK) 6153812Shylee break; 6163812Shylee } else if (info.flags & cmd->flags) { 6170Sstevel@tonic-gate break; 6183812Shylee } 6190Sstevel@tonic-gate } 6200Sstevel@tonic-gate } 6210Sstevel@tonic-gate } 6220Sstevel@tonic-gate 6230Sstevel@tonic-gate /* Show error if no matching mechanism found */ 6240Sstevel@tonic-gate if (i == slotcount) { 6250Sstevel@tonic-gate cryptoerror(LOG_STDERR, 6260Sstevel@tonic-gate gettext("no cryptographic provider was " 6270Sstevel@tonic-gate "found for this algorithm -- %s"), algo_str); 6280Sstevel@tonic-gate goto cleanup; 6290Sstevel@tonic-gate } 6300Sstevel@tonic-gate 6310Sstevel@tonic-gate /* Open a session */ 6320Sstevel@tonic-gate rv = C_OpenSession(slotID, CKF_SERIAL_SESSION, 6335051Swyllys NULL_PTR, NULL, &hSession); 6340Sstevel@tonic-gate 6350Sstevel@tonic-gate if (rv != CKR_OK) { 6360Sstevel@tonic-gate cryptoerror(LOG_STDERR, 6370Sstevel@tonic-gate gettext("can not open PKCS #11 session: %s"), 6380Sstevel@tonic-gate pkcs11_strerror(rv)); 6390Sstevel@tonic-gate goto cleanup; 6400Sstevel@tonic-gate } 6410Sstevel@tonic-gate 6420Sstevel@tonic-gate /* 6430Sstevel@tonic-gate * Generate IV data for encrypt. 6440Sstevel@tonic-gate */ 6450Sstevel@tonic-gate ivlen = mech_aliases[mech_match].ivlen; 6460Sstevel@tonic-gate if ((pivbuf = malloc((size_t)ivlen)) == NULL) { 6470Sstevel@tonic-gate int err = errno; 6480Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s"), 6490Sstevel@tonic-gate strerror(err)); 6500Sstevel@tonic-gate goto cleanup; 6510Sstevel@tonic-gate } 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate if (cmd->type == CKA_ENCRYPT) { 6545252Sdinak if ((pkcs11_random_data((void *)pivbuf, 6550Sstevel@tonic-gate mech_aliases[mech_match].ivlen)) != 0) { 6560Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 6575051Swyllys "Unable to generate random " 6585051Swyllys "data for initialization vector.")); 6590Sstevel@tonic-gate goto cleanup; 6600Sstevel@tonic-gate } 6610Sstevel@tonic-gate } 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate /* 6640Sstevel@tonic-gate * Create the key object 6650Sstevel@tonic-gate */ 6660Sstevel@tonic-gate rv = pkcs11_mech2keytype(mech_type, &keytype); 6670Sstevel@tonic-gate if (rv != CKR_OK) { 6680Sstevel@tonic-gate cryptoerror(LOG_STDERR, 6695051Swyllys gettext("unable to find key type for algorithm.")); 6700Sstevel@tonic-gate goto cleanup; 6710Sstevel@tonic-gate } 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate /* Open input file */ 6740Sstevel@tonic-gate if (iflag) { 6750Sstevel@tonic-gate if ((infd = open(inputfile, O_RDONLY | O_NONBLOCK)) == -1) { 6760Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 6775051Swyllys "can not open input file %s"), inputfile); 6780Sstevel@tonic-gate goto cleanup; 6790Sstevel@tonic-gate } 6800Sstevel@tonic-gate 6810Sstevel@tonic-gate /* Get info on input file */ 6820Sstevel@tonic-gate if (fstat(infd, &insbuf) == -1) { 6830Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 6845051Swyllys "can not stat input file %s"), inputfile); 6850Sstevel@tonic-gate goto cleanup; 6860Sstevel@tonic-gate } 6870Sstevel@tonic-gate } 6880Sstevel@tonic-gate 6890Sstevel@tonic-gate /* 6900Sstevel@tonic-gate * Prepare output file 6910Sstevel@tonic-gate * If the input & output file are same, 6920Sstevel@tonic-gate * the output is written to a temp 6930Sstevel@tonic-gate * file first, then renamed to the original file 6940Sstevel@tonic-gate * after the crypt operation 6950Sstevel@tonic-gate */ 6960Sstevel@tonic-gate inoutsame = B_FALSE; 6970Sstevel@tonic-gate if (oflag) { 6980Sstevel@tonic-gate outfilename = outputfile; 6990Sstevel@tonic-gate if ((stat(outputfile, &outsbuf) != -1) && 7005051Swyllys (insbuf.st_ino == outsbuf.st_ino)) { 7010Sstevel@tonic-gate char *dir; 7020Sstevel@tonic-gate 7030Sstevel@tonic-gate /* create temp file on same dir */ 7040Sstevel@tonic-gate dir = dirname(outputfile); 7050Sstevel@tonic-gate (void) snprintf(tmpnam, sizeof (tmpnam), 7065051Swyllys "%s/encrXXXXXX", dir); 7070Sstevel@tonic-gate outfilename = tmpnam; 7080Sstevel@tonic-gate if ((outfd = mkstemp(tmpnam)) == -1) { 7090Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 7100Sstevel@tonic-gate "cannot create temp file")); 7110Sstevel@tonic-gate goto cleanup; 7120Sstevel@tonic-gate } 7130Sstevel@tonic-gate inoutsame = B_TRUE; 7140Sstevel@tonic-gate } else { 7150Sstevel@tonic-gate /* Create file for output */ 7160Sstevel@tonic-gate if ((outfd = open(outfilename, 7175051Swyllys O_CREAT|O_WRONLY|O_TRUNC, 0644)) == -1) { 7180Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 7190Sstevel@tonic-gate "cannot open output file %s"), 7200Sstevel@tonic-gate outfilename); 7210Sstevel@tonic-gate goto cleanup; 7220Sstevel@tonic-gate } 7230Sstevel@tonic-gate } 7240Sstevel@tonic-gate } 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate /* 7270Sstevel@tonic-gate * Read the version number from the head of the file 7280Sstevel@tonic-gate * to know how to interpret the data that follows. 7290Sstevel@tonic-gate */ 7300Sstevel@tonic-gate if (cmd->type == CKA_DECRYPT) { 7310Sstevel@tonic-gate if (read(infd, &version, sizeof (version)) != 7325051Swyllys sizeof (version)) { 7330Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 7340Sstevel@tonic-gate "failed to get format version from " 7350Sstevel@tonic-gate "input file.")); 7360Sstevel@tonic-gate goto cleanup; 7370Sstevel@tonic-gate } 7380Sstevel@tonic-gate /* convert to host byte order */ 7390Sstevel@tonic-gate version = ntohl(version); 7400Sstevel@tonic-gate 7410Sstevel@tonic-gate switch (version) { 7420Sstevel@tonic-gate case 1: 7430Sstevel@tonic-gate /* 7440Sstevel@tonic-gate * Version 1 output format: 745*6535Sda73024 * - Output format version 1 (4 bytes) 7460Sstevel@tonic-gate * - Iterations used in key gen function (4 bytes) 747*6535Sda73024 * - IV ('ivlen' bytes). The length algorithm-dependent 7480Sstevel@tonic-gate * - Salt data used in key gen (16 bytes) 749*6535Sda73024 * - Cipher text data (remainder of the file) 7500Sstevel@tonic-gate * 7510Sstevel@tonic-gate * An encrypted file has IV as first block (0 or 7520Sstevel@tonic-gate * more bytes depending on mechanism) followed 7530Sstevel@tonic-gate * by cipher text. Get the IV from the encrypted 7540Sstevel@tonic-gate * file. 7550Sstevel@tonic-gate */ 7560Sstevel@tonic-gate /* 7570Sstevel@tonic-gate * Read iteration count and salt data. 7580Sstevel@tonic-gate */ 7590Sstevel@tonic-gate if (read(infd, &iterations, 7605051Swyllys sizeof (iterations)) != sizeof (iterations)) { 7610Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 7625051Swyllys "failed to get iterations from " 7635051Swyllys "input file.")); 7640Sstevel@tonic-gate goto cleanup; 7650Sstevel@tonic-gate } 7660Sstevel@tonic-gate /* convert to host byte order */ 7670Sstevel@tonic-gate iterations = ntohl(iterations); 7680Sstevel@tonic-gate if (ivlen > 0 && 7690Sstevel@tonic-gate read(infd, pivbuf, ivlen) != ivlen) { 7700Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 7710Sstevel@tonic-gate "failed to get initialization " 7720Sstevel@tonic-gate "vector from input file.")); 7730Sstevel@tonic-gate goto cleanup; 7740Sstevel@tonic-gate } 7750Sstevel@tonic-gate if (read(infd, salt, sizeof (salt)) 7765051Swyllys != sizeof (salt)) { 7770Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 7785051Swyllys "failed to get salt data from " 7795051Swyllys "input file.")); 7800Sstevel@tonic-gate goto cleanup; 7810Sstevel@tonic-gate } 7820Sstevel@tonic-gate break; 7830Sstevel@tonic-gate default: 7840Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 7855051Swyllys "Unrecognized format version read from " 7865051Swyllys "input file - expected %d, got %d."), 7875051Swyllys SUNW_ENCRYPT_FILE_VERSION, version); 7880Sstevel@tonic-gate goto cleanup; 7890Sstevel@tonic-gate break; 7900Sstevel@tonic-gate } 7910Sstevel@tonic-gate } 7923812Shylee 7930Sstevel@tonic-gate /* 7943812Shylee * If Kflag is set, let's find the token key now. 7953812Shylee * 7963812Shylee * If Kflag is not set and if encrypting, we need some random 7970Sstevel@tonic-gate * salt data to create the key. If decrypting, 7980Sstevel@tonic-gate * the salt should come from head of the file 7990Sstevel@tonic-gate * to be decrypted. 8000Sstevel@tonic-gate */ 8013812Shylee if (Kflag) { 8023812Shylee rv = get_token_key(hSession, keytype, key_label, pkeydata, 8033812Shylee keysize, &key); 8043812Shylee if (rv != CKR_OK) { 8053812Shylee cryptoerror(LOG_STDERR, gettext( 8063812Shylee "Can not find the token key")); 8073812Shylee goto cleanup; 8083812Shylee } else { 8093812Shylee goto do_crypto; 8103812Shylee } 8113812Shylee } else if (cmd->type == CKA_ENCRYPT) { 8125252Sdinak rv = pkcs11_random_data((void *)salt, sizeof (salt)); 8130Sstevel@tonic-gate if (rv != 0) { 8140Sstevel@tonic-gate cryptoerror(LOG_STDERR, 8150Sstevel@tonic-gate gettext("unable to generate random " 8165051Swyllys "data for key salt.")); 8170Sstevel@tonic-gate goto cleanup; 8180Sstevel@tonic-gate } 8190Sstevel@tonic-gate } 8200Sstevel@tonic-gate 8213812Shylee 8220Sstevel@tonic-gate /* 8230Sstevel@tonic-gate * If key input is read from a file, treat it as 8240Sstevel@tonic-gate * raw key data, unless it is to be used with RC4, 8250Sstevel@tonic-gate * in which case it must be used to generate a pkcs5 8260Sstevel@tonic-gate * key to address security concerns with RC4 keys. 8270Sstevel@tonic-gate */ 8280Sstevel@tonic-gate if (kflag && keyfile != NULL && keytype != CKK_RC4) { 8295252Sdinak /* XXX : why wasn't SUNW_C_KeyToObject used here? */ 8300Sstevel@tonic-gate CK_OBJECT_CLASS objclass = CKO_SECRET_KEY; 8310Sstevel@tonic-gate CK_ATTRIBUTE template[5]; 8320Sstevel@tonic-gate int nattr = 0; 8330Sstevel@tonic-gate 8340Sstevel@tonic-gate template[nattr].type = CKA_CLASS; 8350Sstevel@tonic-gate template[nattr].pValue = &objclass; 8360Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (objclass); 8370Sstevel@tonic-gate nattr++; 8380Sstevel@tonic-gate 8390Sstevel@tonic-gate template[nattr].type = CKA_KEY_TYPE; 8400Sstevel@tonic-gate template[nattr].pValue = &keytype; 8410Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (keytype); 8420Sstevel@tonic-gate nattr++; 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate template[nattr].type = cmd->type; 8450Sstevel@tonic-gate template[nattr].pValue = &truevalue; 8460Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (truevalue); 8470Sstevel@tonic-gate nattr++; 8480Sstevel@tonic-gate 8490Sstevel@tonic-gate template[nattr].type = CKA_TOKEN; 8500Sstevel@tonic-gate template[nattr].pValue = &falsevalue; 8510Sstevel@tonic-gate template[nattr].ulValueLen = sizeof (falsevalue); 8520Sstevel@tonic-gate nattr++; 8530Sstevel@tonic-gate 8540Sstevel@tonic-gate template[nattr].type = CKA_VALUE; 8550Sstevel@tonic-gate template[nattr].pValue = pkeydata; 8560Sstevel@tonic-gate template[nattr].ulValueLen = keysize; 8570Sstevel@tonic-gate nattr++; 8580Sstevel@tonic-gate 8595051Swyllys rv = C_CreateObject(hSession, template, nattr, &key); 8600Sstevel@tonic-gate } else { 8610Sstevel@tonic-gate /* 8620Sstevel@tonic-gate * If the encryption type has a fixed key length, 8630Sstevel@tonic-gate * then its not necessary to set the key length 8640Sstevel@tonic-gate * parameter when generating the key. 8650Sstevel@tonic-gate */ 8660Sstevel@tonic-gate if (keytype == CKK_DES || keytype == CKK_DES3) 8670Sstevel@tonic-gate keylen = 0; 8680Sstevel@tonic-gate else 8690Sstevel@tonic-gate keylen = 16; 8700Sstevel@tonic-gate 8710Sstevel@tonic-gate /* 8720Sstevel@tonic-gate * Generate a cryptographically secure key using 8730Sstevel@tonic-gate * the key read from the file given (-k keyfile) or 8740Sstevel@tonic-gate * the passphrase entered by the user. 8750Sstevel@tonic-gate */ 8765252Sdinak rv = pkcs11_PasswdToPBKD2Object(hSession, (char *)pkeydata, 8775252Sdinak (size_t)keysize, (void *)salt, sizeof (salt), iterations, 8785252Sdinak keytype, keylen, cmd->flags, &key); 8790Sstevel@tonic-gate } 8800Sstevel@tonic-gate 8810Sstevel@tonic-gate if (rv != CKR_OK) { 8820Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 8830Sstevel@tonic-gate "failed to generate a key: %s"), 8840Sstevel@tonic-gate pkcs11_strerror(rv)); 8850Sstevel@tonic-gate goto cleanup; 8860Sstevel@tonic-gate } 8870Sstevel@tonic-gate 8883812Shylee 8893812Shylee do_crypto: 8900Sstevel@tonic-gate /* Setup up mechanism */ 8910Sstevel@tonic-gate mech.mechanism = mech_type; 8920Sstevel@tonic-gate mech.pParameter = (CK_VOID_PTR)pivbuf; 8930Sstevel@tonic-gate mech.ulParameterLen = ivlen; 8940Sstevel@tonic-gate 8950Sstevel@tonic-gate if ((rv = cmd->Init(hSession, &mech, key)) != CKR_OK) { 8960Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 8970Sstevel@tonic-gate "failed to initialize crypto operation: %s"), 8980Sstevel@tonic-gate pkcs11_strerror(rv)); 8990Sstevel@tonic-gate goto cleanup; 9000Sstevel@tonic-gate } 9010Sstevel@tonic-gate 9020Sstevel@tonic-gate /* Write the version header encrypt command */ 9030Sstevel@tonic-gate if (cmd->type == CKA_ENCRYPT) { 9040Sstevel@tonic-gate /* convert to network order for storage */ 905*6535Sda73024 uint32_t netversion = htonl(version); 906*6535Sda73024 uint32_t netiter; 9070Sstevel@tonic-gate 9080Sstevel@tonic-gate if (write(outfd, &netversion, sizeof (netversion)) 9095051Swyllys != sizeof (netversion)) { 9100Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 9115051Swyllys "failed to write version number " 9125051Swyllys "to output file.")); 9130Sstevel@tonic-gate goto cleanup; 9140Sstevel@tonic-gate } 9150Sstevel@tonic-gate /* 9160Sstevel@tonic-gate * Write the iteration and salt data, even if they 9170Sstevel@tonic-gate * were not used to generate a key. 9180Sstevel@tonic-gate */ 9190Sstevel@tonic-gate netiter = htonl(iterations); 9200Sstevel@tonic-gate if (write(outfd, &netiter, 9215051Swyllys sizeof (netiter)) != sizeof (netiter)) { 9220Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 9230Sstevel@tonic-gate "failed to write iterations to output")); 9240Sstevel@tonic-gate goto cleanup; 9250Sstevel@tonic-gate } 9265051Swyllys if (ivlen > 0 && write(outfd, pivbuf, ivlen) != ivlen) { 9270Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 9285051Swyllys "failed to write initialization vector " 9295051Swyllys "to output")); 9300Sstevel@tonic-gate goto cleanup; 9310Sstevel@tonic-gate } 9320Sstevel@tonic-gate if (write(outfd, salt, sizeof (salt)) != sizeof (salt)) { 9330Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 9340Sstevel@tonic-gate "failed to write salt data to output")); 9350Sstevel@tonic-gate goto cleanup; 9360Sstevel@tonic-gate } 9370Sstevel@tonic-gate } 9380Sstevel@tonic-gate 9391142Sjk115741 if (crypt_multipart(cmd, hSession, infd, outfd, insbuf.st_size) == -1) { 9400Sstevel@tonic-gate goto cleanup; 9410Sstevel@tonic-gate } 9420Sstevel@tonic-gate 9430Sstevel@tonic-gate errflag = B_FALSE; 9440Sstevel@tonic-gate 9450Sstevel@tonic-gate /* 9460Sstevel@tonic-gate * Clean up 9470Sstevel@tonic-gate */ 9480Sstevel@tonic-gate cleanup: 9490Sstevel@tonic-gate /* Clear the key data, so others cannot snoop */ 9500Sstevel@tonic-gate if (pkeydata != NULL) { 9510Sstevel@tonic-gate bzero(pkeydata, keysize); 9520Sstevel@tonic-gate free(pkeydata); 9530Sstevel@tonic-gate pkeydata = NULL; 9540Sstevel@tonic-gate } 9550Sstevel@tonic-gate 9560Sstevel@tonic-gate /* Destroy key object */ 9573812Shylee if (Kflag != B_FALSE && key != (CK_OBJECT_HANDLE) 0) { 9580Sstevel@tonic-gate (void) C_DestroyObject(hSession, key); 9590Sstevel@tonic-gate } 9600Sstevel@tonic-gate 9610Sstevel@tonic-gate /* free allocated memory */ 9620Sstevel@tonic-gate if (pSlotList != NULL) 9630Sstevel@tonic-gate free(pSlotList); 9640Sstevel@tonic-gate if (pivbuf != NULL) 9650Sstevel@tonic-gate free(pivbuf); 9660Sstevel@tonic-gate 9670Sstevel@tonic-gate /* close all the files */ 9681142Sjk115741 if (iflag && (infd != -1)) 9690Sstevel@tonic-gate (void) close(infd); 9701142Sjk115741 if (oflag && (outfd != -1)) 9710Sstevel@tonic-gate (void) close(outfd); 9720Sstevel@tonic-gate 9730Sstevel@tonic-gate /* rename tmp output to input file */ 9740Sstevel@tonic-gate if (inoutsame) { 9750Sstevel@tonic-gate if (rename(outfilename, inputfile) == -1) { 9760Sstevel@tonic-gate (void) unlink(outfilename); 9770Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("rename failed.")); 9780Sstevel@tonic-gate } 9790Sstevel@tonic-gate } 9800Sstevel@tonic-gate 9810Sstevel@tonic-gate /* If error occurred, remove the output file */ 9820Sstevel@tonic-gate if (errflag && outfilename != NULL) { 9830Sstevel@tonic-gate (void) unlink(outfilename); 9840Sstevel@tonic-gate } 9850Sstevel@tonic-gate 9860Sstevel@tonic-gate /* close pkcs11 session */ 9870Sstevel@tonic-gate if (hSession != CK_INVALID_HANDLE) 9880Sstevel@tonic-gate (void) C_CloseSession(hSession); 9890Sstevel@tonic-gate 9900Sstevel@tonic-gate (void) C_Finalize(NULL); 9910Sstevel@tonic-gate 9920Sstevel@tonic-gate return (errflag); 9930Sstevel@tonic-gate } 9940Sstevel@tonic-gate 9950Sstevel@tonic-gate /* 9960Sstevel@tonic-gate * Function for printing progress bar when the verbose flag 9970Sstevel@tonic-gate * is set. 9980Sstevel@tonic-gate * 9990Sstevel@tonic-gate * The vertical bar is printed at 25, 50, and 75% complete. 10000Sstevel@tonic-gate * 10010Sstevel@tonic-gate * The function is passed the number of positions on the screen it needs to 10020Sstevel@tonic-gate * advance and loops. 10030Sstevel@tonic-gate */ 10040Sstevel@tonic-gate 10050Sstevel@tonic-gate static void 10060Sstevel@tonic-gate print_status(int pos_to_advance) 10070Sstevel@tonic-gate { 10080Sstevel@tonic-gate 10090Sstevel@tonic-gate while (pos_to_advance > 0) { 10100Sstevel@tonic-gate switch (status_pos) { 10110Sstevel@tonic-gate case 0: 10120Sstevel@tonic-gate (void) fprintf(stderr, gettext("[")); 10130Sstevel@tonic-gate break; 10140Sstevel@tonic-gate case 19: 10150Sstevel@tonic-gate case 39: 10160Sstevel@tonic-gate case 59: 10170Sstevel@tonic-gate (void) fprintf(stderr, gettext("|")); 10180Sstevel@tonic-gate break; 10190Sstevel@tonic-gate default: 10200Sstevel@tonic-gate (void) fprintf(stderr, gettext(".")); 10210Sstevel@tonic-gate } 10220Sstevel@tonic-gate pos_to_advance--; 10230Sstevel@tonic-gate status_pos++; 10240Sstevel@tonic-gate } 10250Sstevel@tonic-gate } 10260Sstevel@tonic-gate 10270Sstevel@tonic-gate /* 10280Sstevel@tonic-gate * Encrypt/Decrypt in multi part. 10290Sstevel@tonic-gate * 10300Sstevel@tonic-gate * This function reads the input file (infd) and writes the 10310Sstevel@tonic-gate * encrypted/decrypted output to file (outfd). 10320Sstevel@tonic-gate * 10330Sstevel@tonic-gate * cmd - pointing to commandinfo 10340Sstevel@tonic-gate * hSession - pkcs session 10350Sstevel@tonic-gate * infd - input file descriptor 10360Sstevel@tonic-gate * outfd - output file descriptor 10370Sstevel@tonic-gate * 10380Sstevel@tonic-gate */ 10390Sstevel@tonic-gate 10400Sstevel@tonic-gate static int 10410Sstevel@tonic-gate crypt_multipart(struct CommandInfo *cmd, CK_SESSION_HANDLE hSession, 10421142Sjk115741 int infd, int outfd, off_t insize) 10430Sstevel@tonic-gate { 10440Sstevel@tonic-gate CK_RV rv; 10450Sstevel@tonic-gate CK_ULONG resultlen; 10460Sstevel@tonic-gate CK_ULONG resultbuflen; 10470Sstevel@tonic-gate CK_BYTE_PTR resultbuf; 10480Sstevel@tonic-gate CK_ULONG datalen; 10490Sstevel@tonic-gate CK_BYTE databuf[BUFFERSIZE]; 10500Sstevel@tonic-gate CK_BYTE outbuf[BUFFERSIZE+BLOCKSIZE]; 10510Sstevel@tonic-gate CK_ULONG status_index = 0; /* current total file size read */ 10520Sstevel@tonic-gate float status_last = 0.0; /* file size of last element used */ 10530Sstevel@tonic-gate float status_incr = 0.0; /* file size element increments */ 10540Sstevel@tonic-gate int pos; /* # of progress bar elements to be print */ 10550Sstevel@tonic-gate ssize_t nread; 10560Sstevel@tonic-gate boolean_t errflag = B_FALSE; 10570Sstevel@tonic-gate 10580Sstevel@tonic-gate datalen = sizeof (databuf); 10590Sstevel@tonic-gate resultbuflen = sizeof (outbuf); 10600Sstevel@tonic-gate resultbuf = outbuf; 10610Sstevel@tonic-gate 10620Sstevel@tonic-gate /* Divide into 79 increments for progress bar element spacing */ 10630Sstevel@tonic-gate if (vflag && iflag) 10641142Sjk115741 status_incr = (insize / 79.0); 10650Sstevel@tonic-gate 10660Sstevel@tonic-gate while ((nread = read(infd, databuf, datalen)) > 0) { 10670Sstevel@tonic-gate 10680Sstevel@tonic-gate /* Start with the initial buffer */ 10690Sstevel@tonic-gate resultlen = resultbuflen; 10700Sstevel@tonic-gate rv = cmd->Update(hSession, databuf, (CK_ULONG)nread, 10715051Swyllys resultbuf, &resultlen); 10720Sstevel@tonic-gate 10730Sstevel@tonic-gate /* Need a bigger buffer? */ 10740Sstevel@tonic-gate if (rv == CKR_BUFFER_TOO_SMALL) { 10750Sstevel@tonic-gate 10760Sstevel@tonic-gate /* free the old buffer */ 10770Sstevel@tonic-gate if (resultbuf != NULL && resultbuf != outbuf) { 10780Sstevel@tonic-gate bzero(resultbuf, resultbuflen); 10790Sstevel@tonic-gate free(resultbuf); 10800Sstevel@tonic-gate } 10810Sstevel@tonic-gate 10820Sstevel@tonic-gate /* allocate a new big buffer */ 10830Sstevel@tonic-gate if ((resultbuf = malloc((size_t)resultlen)) == NULL) { 10840Sstevel@tonic-gate int err = errno; 10850Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("malloc: %s"), 10860Sstevel@tonic-gate strerror(err)); 10870Sstevel@tonic-gate return (-1); 10880Sstevel@tonic-gate } 10890Sstevel@tonic-gate resultbuflen = resultlen; 10900Sstevel@tonic-gate 10910Sstevel@tonic-gate /* Try again with bigger buffer */ 10920Sstevel@tonic-gate rv = cmd->Update(hSession, databuf, (CK_ULONG)nread, 10935051Swyllys resultbuf, &resultlen); 10940Sstevel@tonic-gate } 10950Sstevel@tonic-gate 10960Sstevel@tonic-gate if (rv != CKR_OK) { 10970Sstevel@tonic-gate errflag = B_TRUE; 10980Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 10990Sstevel@tonic-gate "crypto operation failed: %s"), 11000Sstevel@tonic-gate pkcs11_strerror(rv)); 11010Sstevel@tonic-gate break; 11020Sstevel@tonic-gate } 11030Sstevel@tonic-gate 11040Sstevel@tonic-gate /* write the output */ 11050Sstevel@tonic-gate if (write(outfd, resultbuf, resultlen) != resultlen) { 11060Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 11070Sstevel@tonic-gate "failed to write result to output file.")); 11080Sstevel@tonic-gate errflag = B_TRUE; 11090Sstevel@tonic-gate break; 11100Sstevel@tonic-gate } 11110Sstevel@tonic-gate 11120Sstevel@tonic-gate if (vflag) { 11130Sstevel@tonic-gate status_index += resultlen; 11140Sstevel@tonic-gate 11150Sstevel@tonic-gate /* 11160Sstevel@tonic-gate * If input is from stdin, do a our own progress bar 11170Sstevel@tonic-gate * by printing periods at a pre-defined increment 11180Sstevel@tonic-gate * until the file is done. 11190Sstevel@tonic-gate */ 11200Sstevel@tonic-gate if (!iflag) { 11210Sstevel@tonic-gate 11220Sstevel@tonic-gate /* 11230Sstevel@tonic-gate * Print at least 1 element in case the file 11240Sstevel@tonic-gate * is small, it looks better than nothing. 11250Sstevel@tonic-gate */ 11260Sstevel@tonic-gate if (status_pos == 0) { 11270Sstevel@tonic-gate (void) fprintf(stderr, gettext(".")); 11280Sstevel@tonic-gate status_pos = 1; 11290Sstevel@tonic-gate } 11300Sstevel@tonic-gate 11310Sstevel@tonic-gate if ((status_index - status_last) > 11320Sstevel@tonic-gate (PROGRESSSIZE)) { 11330Sstevel@tonic-gate (void) fprintf(stderr, gettext(".")); 11340Sstevel@tonic-gate status_last = status_index; 11350Sstevel@tonic-gate } 11360Sstevel@tonic-gate continue; 11370Sstevel@tonic-gate } 11380Sstevel@tonic-gate 11390Sstevel@tonic-gate /* Calculate the number of elements need to be print */ 11401142Sjk115741 if (insize <= BUFFERSIZE) 11410Sstevel@tonic-gate pos = 78; 11420Sstevel@tonic-gate else 11430Sstevel@tonic-gate pos = (int)((status_index - status_last) / 11440Sstevel@tonic-gate status_incr); 11450Sstevel@tonic-gate 11460Sstevel@tonic-gate /* Add progress bar elements, if needed */ 11470Sstevel@tonic-gate if (pos > 0) { 11480Sstevel@tonic-gate print_status(pos); 11490Sstevel@tonic-gate status_last += (status_incr * pos); 11500Sstevel@tonic-gate } 11510Sstevel@tonic-gate } 11520Sstevel@tonic-gate } 11530Sstevel@tonic-gate 11540Sstevel@tonic-gate /* Print verbose completion */ 11550Sstevel@tonic-gate if (vflag) { 11560Sstevel@tonic-gate if (iflag) 11570Sstevel@tonic-gate (void) fprintf(stderr, "]"); 11580Sstevel@tonic-gate 11590Sstevel@tonic-gate (void) fprintf(stderr, "\n%s\n", gettext("Done.")); 11600Sstevel@tonic-gate } 11610Sstevel@tonic-gate 11620Sstevel@tonic-gate /* Error in reading */ 11630Sstevel@tonic-gate if (nread == -1) { 11640Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 11650Sstevel@tonic-gate "error reading from input file")); 11660Sstevel@tonic-gate errflag = B_TRUE; 11670Sstevel@tonic-gate } 11680Sstevel@tonic-gate 11690Sstevel@tonic-gate if (!errflag) { 11700Sstevel@tonic-gate 11710Sstevel@tonic-gate /* Do the final part */ 11720Sstevel@tonic-gate 11730Sstevel@tonic-gate rv = cmd->Final(hSession, resultbuf, &resultlen); 11740Sstevel@tonic-gate 11750Sstevel@tonic-gate if (rv == CKR_OK) { 11760Sstevel@tonic-gate /* write the output */ 11770Sstevel@tonic-gate if (write(outfd, resultbuf, resultlen) != resultlen) { 11780Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 11790Sstevel@tonic-gate "failed to write result to output file.")); 11800Sstevel@tonic-gate errflag = B_TRUE; 11810Sstevel@tonic-gate } 11820Sstevel@tonic-gate } else { 11830Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext( 11840Sstevel@tonic-gate "crypto operation failed: %s"), 11850Sstevel@tonic-gate pkcs11_strerror(rv)); 11860Sstevel@tonic-gate errflag = B_TRUE; 11870Sstevel@tonic-gate } 11880Sstevel@tonic-gate 11890Sstevel@tonic-gate } 11900Sstevel@tonic-gate 11910Sstevel@tonic-gate if (resultbuf != NULL && resultbuf != outbuf) { 11920Sstevel@tonic-gate bzero(resultbuf, resultbuflen); 11930Sstevel@tonic-gate free(resultbuf); 11940Sstevel@tonic-gate } 11950Sstevel@tonic-gate 11960Sstevel@tonic-gate if (errflag) { 11970Sstevel@tonic-gate return (-1); 11980Sstevel@tonic-gate } else { 11990Sstevel@tonic-gate return (0); 12000Sstevel@tonic-gate } 12010Sstevel@tonic-gate } 1202