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