xref: /onnv-gate/usr/src/cmd/cmd-crypto/pktool/pktool.c (revision 17:003a85e6a25c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*17Sdinak  * Copyright 2005 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  * This file comprises the main driver for this tool.
31*17Sdinak  * Upon parsing the command verbs from user input, it
32*17Sdinak  * branches to the appropriate modules to perform the
33*17Sdinak  * requested task.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <string.h>
380Sstevel@tonic-gate #include <ctype.h>
390Sstevel@tonic-gate #include <malloc.h>
400Sstevel@tonic-gate #include <libgen.h>
410Sstevel@tonic-gate #include <errno.h>
420Sstevel@tonic-gate #include <cryptoutil.h>
430Sstevel@tonic-gate #include <security/cryptoki.h>
440Sstevel@tonic-gate #include "common.h"
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate  * The verbcmd construct allows genericizing information about a verb so
480Sstevel@tonic-gate  * that it is easier to manipulate.  Makes parsing code easier to read,
490Sstevel@tonic-gate  * fix, and extend with new verbs.
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate typedef struct verbcmd_s {
520Sstevel@tonic-gate 	char	*verb;
530Sstevel@tonic-gate 	int	(*action)(int, char *[]);
54*17Sdinak 	int	mode;
55*17Sdinak 	char	*synopsis;
560Sstevel@tonic-gate } verbcmd;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /* External declarations for supported verb actions. */
590Sstevel@tonic-gate extern int	pk_setpin(int argc, char *argv[]);
60*17Sdinak extern int	pk_list(int argc, char *argv[]);
61*17Sdinak extern int	pk_delete(int argc, char *argv[]);
62*17Sdinak extern int	pk_import(int argc, char *argv[]);
63*17Sdinak extern int	pk_export(int argc, char *argv[]);
64*17Sdinak extern int	pk_tokens(int argc, char *argv[]);
65*17Sdinak 
66*17Sdinak /* Forward declarations for "built-in" verb actions. */
67*17Sdinak static int	pk_help(int argc, char *argv[]);
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /* Command structure for verbs and their actions.  Do NOT i18n/l10n. */
700Sstevel@tonic-gate static verbcmd	cmds[] = {
71*17Sdinak 	{ "tokens",	pk_tokens,	0,	"tokens" },
72*17Sdinak 	{ "setpin",	pk_setpin,	0,	"setpin" },
73*17Sdinak 	{ "list",	pk_list,	0,	"list [-p] [-P] [-l <label>]"
74*17Sdinak 	    "\n\t\tor list [--public] [--private] [--label[=]<label>]" },
75*17Sdinak 	{ "delete",	pk_delete,	0,
76*17Sdinak 	    "delete { [-p] [-P] [-l <label>] }"
77*17Sdinak 	    "\n\t\tor delete { [--public] [--private] [--label[=]<label>] }" },
78*17Sdinak 	{ "import",	pk_import,	0,	"import <file>" },
79*17Sdinak 	{ "export",	pk_export,	0,	"export <file>" },
80*17Sdinak 	{ "-?",		pk_help,	0,	"--help\t(help and usage)" },
810Sstevel@tonic-gate };
820Sstevel@tonic-gate static int	num_cmds = sizeof (cmds) / sizeof (verbcmd);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate static char	*prog;
850Sstevel@tonic-gate static void	usage(void);
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /*
880Sstevel@tonic-gate  * Usage information.  This function must be updated when new verbs or
890Sstevel@tonic-gate  * options are added.
900Sstevel@tonic-gate  */
910Sstevel@tonic-gate static void
920Sstevel@tonic-gate usage(void)
930Sstevel@tonic-gate {
94*17Sdinak 	int	i;
95*17Sdinak 
96*17Sdinak 	cryptodebug("inside usage");
97*17Sdinak 
98*17Sdinak 	/* Display this block only in command-line mode. */
99*17Sdinak 	(void) fprintf(stdout, gettext("Usage:\n"));
100*17Sdinak 	(void) fprintf(stdout, gettext("\t%s -?\t(help and usage)\n"), prog);
101*17Sdinak 	(void) fprintf(stdout, gettext("\t%s subcommand [options...]\n"), prog);
102*17Sdinak 	(void) fprintf(stdout, gettext("where subcommands may be:\n"));
103*17Sdinak 
104*17Sdinak 	/* Display only those verbs that match the current tool mode. */
105*17Sdinak 	for (i = 0; i < num_cmds; i++) {
106*17Sdinak 		/* Do NOT i18n/l10n. */
107*17Sdinak 		(void) fprintf(stdout, "\t%s\n", cmds[i].synopsis);
108*17Sdinak 	}
109*17Sdinak }
110*17Sdinak 
111*17Sdinak /*
112*17Sdinak  * Provide help, in the form of displaying the usage.
113*17Sdinak  */
114*17Sdinak static int
115*17Sdinak pk_help(int argc, char *argv[])
116*17Sdinak /* ARGSUSED */
117*17Sdinak {
118*17Sdinak 	cryptodebug("inside pk_help");
119*17Sdinak 
120*17Sdinak 	usage();
121*17Sdinak 	return (0);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate  * MAIN() -- where all the action is
1260Sstevel@tonic-gate  */
1270Sstevel@tonic-gate int
1280Sstevel@tonic-gate main(int argc, char *argv[], char *envp[])
1290Sstevel@tonic-gate /* ARGSUSED2 */
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate 	int	i, found = -1;
1320Sstevel@tonic-gate 	int	rv;
1330Sstevel@tonic-gate 	int	pk_argc = 0;
1340Sstevel@tonic-gate 	char	**pk_argv = NULL;
135*17Sdinak 	int	save_errno = 0;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	/* Set up for i18n/l10n. */
1380Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1390Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D. */
1400Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it isn't. */
1410Sstevel@tonic-gate #endif
1420Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	/* Get program base name and move pointer over 0th arg. */
1450Sstevel@tonic-gate 	prog = basename(argv[0]);
1460Sstevel@tonic-gate 	argv++, argc--;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	/* Set up for debug and error output. */
1490Sstevel@tonic-gate 	cryptodebug_init(prog);
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	if (argc == 0) {
1520Sstevel@tonic-gate 		usage();
1530Sstevel@tonic-gate 		return (1);
1540Sstevel@tonic-gate 	}
1550Sstevel@tonic-gate 
156*17Sdinak 	/* Check for help options.  For CLIP-compliance. */
157*17Sdinak 	if (argc == 1 && argv[0][0] == '-') {
158*17Sdinak 		switch (argv[0][1]) {
159*17Sdinak 		case '?':
160*17Sdinak 			return (pk_help(argc, argv));
161*17Sdinak 		default:
162*17Sdinak 			usage();
163*17Sdinak 			return (1);
164*17Sdinak 		}
165*17Sdinak 	}
166*17Sdinak 
167*17Sdinak 	/* Always turns off Metaslot so that we can see softtoken. */
168*17Sdinak 	cryptodebug("disabling Metaslot");
1690Sstevel@tonic-gate 	if (setenv("METASLOT_ENABLED", "false", 1) < 0) {
170*17Sdinak 		save_errno = errno;
1710Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
172*17Sdinak 		    gettext("Disabling Metaslot failed (%s)."),
173*17Sdinak 		    strerror(save_errno));
1740Sstevel@tonic-gate 		return (1);
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	/* Begin parsing command line. */
178*17Sdinak 	cryptodebug("begin parsing command line");
1790Sstevel@tonic-gate 	pk_argc = argc;
1800Sstevel@tonic-gate 	pk_argv = argv;
1810Sstevel@tonic-gate 
182*17Sdinak 	/* Check for valid verb (or an abbreviation of it). */
1830Sstevel@tonic-gate 	found = -1;
1840Sstevel@tonic-gate 	for (i = 0; i < num_cmds; i++) {
1850Sstevel@tonic-gate 		if (strcmp(cmds[i].verb, pk_argv[0]) == 0) {
1860Sstevel@tonic-gate 			if (found < 0) {
187*17Sdinak 				cryptodebug("found cmd %s", cmds[i].verb);
1880Sstevel@tonic-gate 				found = i;
1890Sstevel@tonic-gate 				break;
190*17Sdinak 			} else {
191*17Sdinak 				cryptodebug("also found cmd %s, skipping",
192*17Sdinak 				    cmds[i].verb);
1930Sstevel@tonic-gate 			}
1940Sstevel@tonic-gate 		}
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 	/* Stop here if no valid verb found. */
1970Sstevel@tonic-gate 	if (found < 0) {
198*17Sdinak 		cryptoerror(LOG_STDERR, gettext("Invalid verb: %s"),
199*17Sdinak 		    pk_argv[0]);
2000Sstevel@tonic-gate 		return (1);
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	/* Get to work! */
204*17Sdinak 	cryptodebug("begin executing cmd action");
2050Sstevel@tonic-gate 	rv = (*cmds[found].action)(pk_argc, pk_argv);
206*17Sdinak 	cryptodebug("end executing cmd action");
2070Sstevel@tonic-gate 	switch (rv) {
2080Sstevel@tonic-gate 	case PK_ERR_NONE:
209*17Sdinak 		cryptodebug("subcommand succeeded");
2100Sstevel@tonic-gate 		break;		/* Command succeeded, do nothing. */
2110Sstevel@tonic-gate 	case PK_ERR_USAGE:
212*17Sdinak 		cryptodebug("usage error detected");
2130Sstevel@tonic-gate 		usage();
2140Sstevel@tonic-gate 		break;
2150Sstevel@tonic-gate 	case PK_ERR_QUIT:
216*17Sdinak 		cryptodebug("quit command received");
2170Sstevel@tonic-gate 		exit(0);
2180Sstevel@tonic-gate 		/* NOTREACHED */
219*17Sdinak 	case PK_ERR_PK11:
220*17Sdinak 		cryptoerror(LOG_STDERR, "%s",
221*17Sdinak 		    gettext("Command failed due to PKCS#11 error."));
2220Sstevel@tonic-gate 		break;
223*17Sdinak 	case PK_ERR_SYSTEM:
224*17Sdinak 		cryptoerror(LOG_STDERR, "%s",
225*17Sdinak 		    gettext("Command failed due to system error."));
2260Sstevel@tonic-gate 		break;
227*17Sdinak 	case PK_ERR_OPENSSL:
2280Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, "%s",
229*17Sdinak 		    gettext("Command failed due to OpenSSL error."));
2300Sstevel@tonic-gate 		break;
2310Sstevel@tonic-gate 	default:
232*17Sdinak 		cryptoerror(LOG_STDERR, "%s (%d).",
2330Sstevel@tonic-gate 		    gettext("Unknown error value"), rv);
2340Sstevel@tonic-gate 		break;
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate 	return (rv);
2370Sstevel@tonic-gate }
238