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