1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * This file comprises the main driver for this tool. 31*7c478bd9Sstevel@tonic-gate */ 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate #include <stdio.h> 34*7c478bd9Sstevel@tonic-gate #include <string.h> 35*7c478bd9Sstevel@tonic-gate #include <ctype.h> 36*7c478bd9Sstevel@tonic-gate #include <malloc.h> 37*7c478bd9Sstevel@tonic-gate #include <libgen.h> 38*7c478bd9Sstevel@tonic-gate #include <errno.h> 39*7c478bd9Sstevel@tonic-gate #include <cryptoutil.h> 40*7c478bd9Sstevel@tonic-gate #include <security/cryptoki.h> 41*7c478bd9Sstevel@tonic-gate #include "common.h" 42*7c478bd9Sstevel@tonic-gate 43*7c478bd9Sstevel@tonic-gate /* 44*7c478bd9Sstevel@tonic-gate * The verbcmd construct allows genericizing information about a verb so 45*7c478bd9Sstevel@tonic-gate * that it is easier to manipulate. Makes parsing code easier to read, 46*7c478bd9Sstevel@tonic-gate * fix, and extend with new verbs. 47*7c478bd9Sstevel@tonic-gate */ 48*7c478bd9Sstevel@tonic-gate typedef struct verbcmd_s { 49*7c478bd9Sstevel@tonic-gate char *verb; 50*7c478bd9Sstevel@tonic-gate int (*action)(int, char *[]); 51*7c478bd9Sstevel@tonic-gate int mode; /* reserved */ 52*7c478bd9Sstevel@tonic-gate char *synopsis; /* reserved */ 53*7c478bd9Sstevel@tonic-gate } verbcmd; 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate /* External declarations for supported verb actions. */ 56*7c478bd9Sstevel@tonic-gate extern int pk_setpin(int argc, char *argv[]); 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate /* Command structure for verbs and their actions. Do NOT i18n/l10n. */ 59*7c478bd9Sstevel@tonic-gate static verbcmd cmds[] = { 60*7c478bd9Sstevel@tonic-gate { "setpin", pk_setpin, 0, "" }, 61*7c478bd9Sstevel@tonic-gate }; 62*7c478bd9Sstevel@tonic-gate static int num_cmds = sizeof (cmds) / sizeof (verbcmd); 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate static char *prog; 65*7c478bd9Sstevel@tonic-gate static void usage(void); 66*7c478bd9Sstevel@tonic-gate 67*7c478bd9Sstevel@tonic-gate /* 68*7c478bd9Sstevel@tonic-gate * Usage information. This function must be updated when new verbs or 69*7c478bd9Sstevel@tonic-gate * options are added. 70*7c478bd9Sstevel@tonic-gate */ 71*7c478bd9Sstevel@tonic-gate static void 72*7c478bd9Sstevel@tonic-gate usage(void) 73*7c478bd9Sstevel@tonic-gate { 74*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Usage:\n")); 75*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("\t%s setpin\n"), prog); 76*7c478bd9Sstevel@tonic-gate } 77*7c478bd9Sstevel@tonic-gate 78*7c478bd9Sstevel@tonic-gate /* 79*7c478bd9Sstevel@tonic-gate * MAIN() -- where all the action is 80*7c478bd9Sstevel@tonic-gate */ 81*7c478bd9Sstevel@tonic-gate int 82*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[], char *envp[]) 83*7c478bd9Sstevel@tonic-gate /* ARGSUSED2 */ 84*7c478bd9Sstevel@tonic-gate { 85*7c478bd9Sstevel@tonic-gate int i, found = -1; 86*7c478bd9Sstevel@tonic-gate int rv; 87*7c478bd9Sstevel@tonic-gate int pk_argc = 0; 88*7c478bd9Sstevel@tonic-gate char **pk_argv = NULL; 89*7c478bd9Sstevel@tonic-gate 90*7c478bd9Sstevel@tonic-gate /* Set up for i18n/l10n. */ 91*7c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 92*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D. */ 93*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it isn't. */ 94*7c478bd9Sstevel@tonic-gate #endif 95*7c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 96*7c478bd9Sstevel@tonic-gate 97*7c478bd9Sstevel@tonic-gate /* Get program base name and move pointer over 0th arg. */ 98*7c478bd9Sstevel@tonic-gate prog = basename(argv[0]); 99*7c478bd9Sstevel@tonic-gate argv++, argc--; 100*7c478bd9Sstevel@tonic-gate 101*7c478bd9Sstevel@tonic-gate /* Set up for debug and error output. */ 102*7c478bd9Sstevel@tonic-gate cryptodebug_init(prog); 103*7c478bd9Sstevel@tonic-gate 104*7c478bd9Sstevel@tonic-gate /* There must be one remaining arg at this point */ 105*7c478bd9Sstevel@tonic-gate if (argc == 0) { 106*7c478bd9Sstevel@tonic-gate usage(); 107*7c478bd9Sstevel@tonic-gate return (1); 108*7c478bd9Sstevel@tonic-gate } 109*7c478bd9Sstevel@tonic-gate 110*7c478bd9Sstevel@tonic-gate /* 111*7c478bd9Sstevel@tonic-gate * By default, metaslot is enabled, and pkcs11_softtoken is 112*7c478bd9Sstevel@tonic-gate * the keystore, so, pkcs11_softtoken is hidden. 113*7c478bd9Sstevel@tonic-gate * Always turns off Metaslot so that we can see pkcs11_softtoken. 114*7c478bd9Sstevel@tonic-gate */ 115*7c478bd9Sstevel@tonic-gate if (setenv("METASLOT_ENABLED", "false", 1) < 0) { 116*7c478bd9Sstevel@tonic-gate pk11_errno = errno; 117*7c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, 118*7c478bd9Sstevel@tonic-gate gettext("Disabling metaslot failed: %s"), 119*7c478bd9Sstevel@tonic-gate strerror(pk11_errno)); 120*7c478bd9Sstevel@tonic-gate return (1); 121*7c478bd9Sstevel@tonic-gate } 122*7c478bd9Sstevel@tonic-gate 123*7c478bd9Sstevel@tonic-gate /* Begin parsing command line. */ 124*7c478bd9Sstevel@tonic-gate pk_argc = argc; 125*7c478bd9Sstevel@tonic-gate pk_argv = argv; 126*7c478bd9Sstevel@tonic-gate 127*7c478bd9Sstevel@tonic-gate /* Check for valid verb */ 128*7c478bd9Sstevel@tonic-gate found = -1; 129*7c478bd9Sstevel@tonic-gate for (i = 0; i < num_cmds; i++) { 130*7c478bd9Sstevel@tonic-gate if (strcmp(cmds[i].verb, pk_argv[0]) == 0) { 131*7c478bd9Sstevel@tonic-gate if (found < 0) { 132*7c478bd9Sstevel@tonic-gate found = i; 133*7c478bd9Sstevel@tonic-gate break; 134*7c478bd9Sstevel@tonic-gate } 135*7c478bd9Sstevel@tonic-gate } 136*7c478bd9Sstevel@tonic-gate } 137*7c478bd9Sstevel@tonic-gate 138*7c478bd9Sstevel@tonic-gate /* Stop here if no valid verb found. */ 139*7c478bd9Sstevel@tonic-gate if (found < 0) { 140*7c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, 141*7c478bd9Sstevel@tonic-gate gettext("Invalid verb: %s"), pk_argv[0]); 142*7c478bd9Sstevel@tonic-gate return (1); 143*7c478bd9Sstevel@tonic-gate } 144*7c478bd9Sstevel@tonic-gate 145*7c478bd9Sstevel@tonic-gate /* Get to work! */ 146*7c478bd9Sstevel@tonic-gate rv = (*cmds[found].action)(pk_argc, pk_argv); 147*7c478bd9Sstevel@tonic-gate switch (rv) { 148*7c478bd9Sstevel@tonic-gate case PK_ERR_NONE: 149*7c478bd9Sstevel@tonic-gate break; /* Command succeeded, do nothing. */ 150*7c478bd9Sstevel@tonic-gate case PK_ERR_USAGE: 151*7c478bd9Sstevel@tonic-gate usage(); 152*7c478bd9Sstevel@tonic-gate break; 153*7c478bd9Sstevel@tonic-gate case PK_ERR_QUIT: 154*7c478bd9Sstevel@tonic-gate exit(0); 155*7c478bd9Sstevel@tonic-gate /* NOTREACHED */ 156*7c478bd9Sstevel@tonic-gate case PK_ERR_PK11INIT: 157*7c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, "%s (%s)", 158*7c478bd9Sstevel@tonic-gate gettext("Unable to initialize PKCS#11"), 159*7c478bd9Sstevel@tonic-gate pkcs11_strerror(pk11_errno)); 160*7c478bd9Sstevel@tonic-gate cryptodebug("C_Initialize failed (%s)", 161*7c478bd9Sstevel@tonic-gate pkcs11_strerror(pk11_errno)); 162*7c478bd9Sstevel@tonic-gate break; 163*7c478bd9Sstevel@tonic-gate case PK_ERR_PK11SLOTS: 164*7c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, "%s (%s)", 165*7c478bd9Sstevel@tonic-gate gettext("Failed to find PKCS#11 slots"), 166*7c478bd9Sstevel@tonic-gate pkcs11_strerror(pk11_errno)); 167*7c478bd9Sstevel@tonic-gate cryptodebug("C_GetSlotList failed (%s)", 168*7c478bd9Sstevel@tonic-gate pkcs11_strerror(pk11_errno)); 169*7c478bd9Sstevel@tonic-gate break; 170*7c478bd9Sstevel@tonic-gate case PK_ERR_PK11SESSION: 171*7c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, "%s (%s)", 172*7c478bd9Sstevel@tonic-gate gettext("Unable to open PKCS#11 session"), 173*7c478bd9Sstevel@tonic-gate pkcs11_strerror(pk11_errno)); 174*7c478bd9Sstevel@tonic-gate cryptodebug("C_OpenSession failed (%s)", 175*7c478bd9Sstevel@tonic-gate pkcs11_strerror(pk11_errno)); 176*7c478bd9Sstevel@tonic-gate break; 177*7c478bd9Sstevel@tonic-gate case PK_ERR_PK11LOGIN: 178*7c478bd9Sstevel@tonic-gate if (pk11_errno == CKR_PIN_INCORRECT) 179*7c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, "%s", gettext("Incorrect PIN")); 180*7c478bd9Sstevel@tonic-gate else { 181*7c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, "%s (%s)", 182*7c478bd9Sstevel@tonic-gate gettext("PKCS#11 authentication failed"), 183*7c478bd9Sstevel@tonic-gate pkcs11_strerror(pk11_errno)); 184*7c478bd9Sstevel@tonic-gate cryptodebug("C_Login failed (%s)", 185*7c478bd9Sstevel@tonic-gate pkcs11_strerror(pk11_errno)); 186*7c478bd9Sstevel@tonic-gate } 187*7c478bd9Sstevel@tonic-gate break; 188*7c478bd9Sstevel@tonic-gate case PK_ERR_PK11SETPIN: 189*7c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, "%s (%s)", 190*7c478bd9Sstevel@tonic-gate gettext("Set PIN failed"), pkcs11_strerror(pk11_errno)); 191*7c478bd9Sstevel@tonic-gate break; 192*7c478bd9Sstevel@tonic-gate case PK_ERR_NOSLOTS: 193*7c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, "%s", gettext("No slots were found")); 194*7c478bd9Sstevel@tonic-gate break; 195*7c478bd9Sstevel@tonic-gate case PK_ERR_NOMEMORY: 196*7c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, "%s", gettext("Out of memory")); 197*7c478bd9Sstevel@tonic-gate break; 198*7c478bd9Sstevel@tonic-gate case PK_ERR_NOTFOUND: 199*7c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, "%s", gettext("Token name not found")); 200*7c478bd9Sstevel@tonic-gate break; 201*7c478bd9Sstevel@tonic-gate case PK_ERR_PASSPHRASE: 202*7c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, "%s", 203*7c478bd9Sstevel@tonic-gate gettext("Unable to get token PIN")); 204*7c478bd9Sstevel@tonic-gate break; 205*7c478bd9Sstevel@tonic-gate case PK_ERR_NEWPIN: 206*7c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, "%s", gettext("Failed to get new PIN")); 207*7c478bd9Sstevel@tonic-gate break; 208*7c478bd9Sstevel@tonic-gate case PK_ERR_PINCONFIRM: 209*7c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, "%s", 210*7c478bd9Sstevel@tonic-gate gettext("Failed to confirm new PIN")); 211*7c478bd9Sstevel@tonic-gate break; 212*7c478bd9Sstevel@tonic-gate case PK_ERR_PINMATCH: 213*7c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, "%s", gettext("PINs do not match")); 214*7c478bd9Sstevel@tonic-gate break; 215*7c478bd9Sstevel@tonic-gate case PK_ERR_CHANGEPIN: 216*7c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, "%s", gettext("PIN must be changed")); 217*7c478bd9Sstevel@tonic-gate break; 218*7c478bd9Sstevel@tonic-gate default: 219*7c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, "%s (%d)", 220*7c478bd9Sstevel@tonic-gate gettext("Unknown error value"), rv); 221*7c478bd9Sstevel@tonic-gate break; 222*7c478bd9Sstevel@tonic-gate } 223*7c478bd9Sstevel@tonic-gate return (rv); 224*7c478bd9Sstevel@tonic-gate } 225