xref: /titanic_53/usr/src/cmd/cmd-crypto/pktool/pktool.c (revision 49e212991a3065f7e499a4b29ae8d8eaf33f3135)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237711facfSdinak  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * This file comprises the main driver for this tool.
317711facfSdinak  * Upon parsing the command verbs from user input, it
327711facfSdinak  * branches to the appropriate modules to perform the
337711facfSdinak  * requested task.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <ctype.h>
397c478bd9Sstevel@tonic-gate #include <malloc.h>
407c478bd9Sstevel@tonic-gate #include <libgen.h>
417c478bd9Sstevel@tonic-gate #include <errno.h>
427c478bd9Sstevel@tonic-gate #include <cryptoutil.h>
437c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
447c478bd9Sstevel@tonic-gate #include "common.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * The verbcmd construct allows genericizing information about a verb so
487c478bd9Sstevel@tonic-gate  * that it is easier to manipulate.  Makes parsing code easier to read,
497c478bd9Sstevel@tonic-gate  * fix, and extend with new verbs.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate typedef struct verbcmd_s {
527c478bd9Sstevel@tonic-gate 	char	*verb;
537c478bd9Sstevel@tonic-gate 	int	(*action)(int, char *[]);
547711facfSdinak 	int	mode;
557711facfSdinak 	char	*synopsis;
567c478bd9Sstevel@tonic-gate } verbcmd;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /* External declarations for supported verb actions. */
597c478bd9Sstevel@tonic-gate extern int	pk_setpin(int argc, char *argv[]);
607711facfSdinak extern int	pk_list(int argc, char *argv[]);
617711facfSdinak extern int	pk_delete(int argc, char *argv[]);
627711facfSdinak extern int	pk_import(int argc, char *argv[]);
637711facfSdinak extern int	pk_export(int argc, char *argv[]);
647711facfSdinak extern int	pk_tokens(int argc, char *argv[]);
657711facfSdinak 
667711facfSdinak /* Forward declarations for "built-in" verb actions. */
677711facfSdinak static int	pk_help(int argc, char *argv[]);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /* Command structure for verbs and their actions.  Do NOT i18n/l10n. */
707c478bd9Sstevel@tonic-gate static verbcmd	cmds[] = {
717711facfSdinak 	{ "tokens",	pk_tokens,	0,	"tokens" },
72*49e21299Sdinak 	{ "setpin",	pk_setpin,	0,
73*49e21299Sdinak 	    "setpin [token=<token>[:<manuf>[:<serial>]]]" },
74*49e21299Sdinak 	{ "list",	pk_list,	0,
75*49e21299Sdinak 	    "list [token=<token>[:<manuf>[:<serial>]]] "
76*49e21299Sdinak 	    "[objtype=private|public|both] [label=<label>]" },
777711facfSdinak 	{ "delete",	pk_delete,	0,
78*49e21299Sdinak 	    "delete [token=<token>[:<manuf>[:<serial>]]] "
79*49e21299Sdinak 	    "{ [objtype=private|public|both] [label=<label>] }" },
80*49e21299Sdinak 	{ "import",	pk_import,	0,
81*49e21299Sdinak 	    "import [token=<token>[:<manuf>[:<serial>]]] infile=<file>" },
82*49e21299Sdinak 	{ "export",	pk_export,	0,
83*49e21299Sdinak 	    "export [token=<token>[:<manuf>[:<serial>]]] outfile=<file>" },
84*49e21299Sdinak 	{ "-?",		pk_help,	0,	"help\t(help and usage)" },
857c478bd9Sstevel@tonic-gate };
867c478bd9Sstevel@tonic-gate static int	num_cmds = sizeof (cmds) / sizeof (verbcmd);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static char	*prog;
897c478bd9Sstevel@tonic-gate static void	usage(void);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * Usage information.  This function must be updated when new verbs or
937c478bd9Sstevel@tonic-gate  * options are added.
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate static void
967c478bd9Sstevel@tonic-gate usage(void)
977c478bd9Sstevel@tonic-gate {
987711facfSdinak 	int	i;
997711facfSdinak 
1007711facfSdinak 	cryptodebug("inside usage");
1017711facfSdinak 
1027711facfSdinak 	/* Display this block only in command-line mode. */
1037711facfSdinak 	(void) fprintf(stdout, gettext("Usage:\n"));
1047711facfSdinak 	(void) fprintf(stdout, gettext("\t%s -?\t(help and usage)\n"), prog);
1057711facfSdinak 	(void) fprintf(stdout, gettext("\t%s subcommand [options...]\n"), prog);
1067711facfSdinak 	(void) fprintf(stdout, gettext("where subcommands may be:\n"));
1077711facfSdinak 
1087711facfSdinak 	/* Display only those verbs that match the current tool mode. */
1097711facfSdinak 	for (i = 0; i < num_cmds; i++) {
1107711facfSdinak 		/* Do NOT i18n/l10n. */
1117711facfSdinak 		(void) fprintf(stdout, "\t%s\n", cmds[i].synopsis);
1127711facfSdinak 	}
1137711facfSdinak }
1147711facfSdinak 
1157711facfSdinak /*
1167711facfSdinak  * Provide help, in the form of displaying the usage.
1177711facfSdinak  */
1187711facfSdinak static int
1197711facfSdinak pk_help(int argc, char *argv[])
1207711facfSdinak /* ARGSUSED */
1217711facfSdinak {
1227711facfSdinak 	cryptodebug("inside pk_help");
1237711facfSdinak 
1247711facfSdinak 	usage();
1257711facfSdinak 	return (0);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate  * MAIN() -- where all the action is
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate int
1327c478bd9Sstevel@tonic-gate main(int argc, char *argv[], char *envp[])
1337c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
1347c478bd9Sstevel@tonic-gate {
1357c478bd9Sstevel@tonic-gate 	int	i, found = -1;
1367c478bd9Sstevel@tonic-gate 	int	rv;
1377c478bd9Sstevel@tonic-gate 	int	pk_argc = 0;
1387c478bd9Sstevel@tonic-gate 	char	**pk_argv = NULL;
1397711facfSdinak 	int	save_errno = 0;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	/* Set up for i18n/l10n. */
1427c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1437c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D. */
1447c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it isn't. */
1457c478bd9Sstevel@tonic-gate #endif
1467c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	/* Get program base name and move pointer over 0th arg. */
1497c478bd9Sstevel@tonic-gate 	prog = basename(argv[0]);
1507c478bd9Sstevel@tonic-gate 	argv++, argc--;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	/* Set up for debug and error output. */
1537c478bd9Sstevel@tonic-gate 	cryptodebug_init(prog);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	if (argc == 0) {
1567c478bd9Sstevel@tonic-gate 		usage();
1577c478bd9Sstevel@tonic-gate 		return (1);
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607711facfSdinak 	/* Check for help options.  For CLIP-compliance. */
1617711facfSdinak 	if (argc == 1 && argv[0][0] == '-') {
1627711facfSdinak 		switch (argv[0][1]) {
1637711facfSdinak 		case '?':
1647711facfSdinak 			return (pk_help(argc, argv));
1657711facfSdinak 		default:
1667711facfSdinak 			usage();
1677711facfSdinak 			return (1);
1687711facfSdinak 		}
1697711facfSdinak 	}
1707711facfSdinak 
1717711facfSdinak 	/* Always turns off Metaslot so that we can see softtoken. */
1727711facfSdinak 	cryptodebug("disabling Metaslot");
1737c478bd9Sstevel@tonic-gate 	if (setenv("METASLOT_ENABLED", "false", 1) < 0) {
1747711facfSdinak 		save_errno = errno;
1757c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
1767711facfSdinak 		    gettext("Disabling Metaslot failed (%s)."),
1777711facfSdinak 		    strerror(save_errno));
1787c478bd9Sstevel@tonic-gate 		return (1);
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/* Begin parsing command line. */
1827711facfSdinak 	cryptodebug("begin parsing command line");
1837c478bd9Sstevel@tonic-gate 	pk_argc = argc;
1847c478bd9Sstevel@tonic-gate 	pk_argv = argv;
1857c478bd9Sstevel@tonic-gate 
1867711facfSdinak 	/* Check for valid verb (or an abbreviation of it). */
1877c478bd9Sstevel@tonic-gate 	found = -1;
1887c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_cmds; i++) {
1897c478bd9Sstevel@tonic-gate 		if (strcmp(cmds[i].verb, pk_argv[0]) == 0) {
1907c478bd9Sstevel@tonic-gate 			if (found < 0) {
1917711facfSdinak 				cryptodebug("found cmd %s", cmds[i].verb);
1927c478bd9Sstevel@tonic-gate 				found = i;
1937c478bd9Sstevel@tonic-gate 				break;
1947711facfSdinak 			} else {
1957711facfSdinak 				cryptodebug("also found cmd %s, skipping",
1967711facfSdinak 				    cmds[i].verb);
1977c478bd9Sstevel@tonic-gate 			}
1987c478bd9Sstevel@tonic-gate 		}
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 	/* Stop here if no valid verb found. */
2017c478bd9Sstevel@tonic-gate 	if (found < 0) {
2027711facfSdinak 		cryptoerror(LOG_STDERR, gettext("Invalid verb: %s"),
2037711facfSdinak 		    pk_argv[0]);
2047c478bd9Sstevel@tonic-gate 		return (1);
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	/* Get to work! */
2087711facfSdinak 	cryptodebug("begin executing cmd action");
2097c478bd9Sstevel@tonic-gate 	rv = (*cmds[found].action)(pk_argc, pk_argv);
2107711facfSdinak 	cryptodebug("end executing cmd action");
2117c478bd9Sstevel@tonic-gate 	switch (rv) {
2127c478bd9Sstevel@tonic-gate 	case PK_ERR_NONE:
2137711facfSdinak 		cryptodebug("subcommand succeeded");
2147c478bd9Sstevel@tonic-gate 		break;		/* Command succeeded, do nothing. */
2157c478bd9Sstevel@tonic-gate 	case PK_ERR_USAGE:
2167711facfSdinak 		cryptodebug("usage error detected");
2177c478bd9Sstevel@tonic-gate 		usage();
2187c478bd9Sstevel@tonic-gate 		break;
2197c478bd9Sstevel@tonic-gate 	case PK_ERR_QUIT:
2207711facfSdinak 		cryptodebug("quit command received");
2217c478bd9Sstevel@tonic-gate 		exit(0);
2227c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
2237711facfSdinak 	case PK_ERR_PK11:
2247c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, "%s",
2257711facfSdinak 		    gettext("Command failed due to PKCS#11 error."));
2267c478bd9Sstevel@tonic-gate 		break;
2277711facfSdinak 	case PK_ERR_SYSTEM:
2287c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, "%s",
2297711facfSdinak 		    gettext("Command failed due to system error."));
2307c478bd9Sstevel@tonic-gate 		break;
2317711facfSdinak 	case PK_ERR_OPENSSL:
2327711facfSdinak 		cryptoerror(LOG_STDERR, "%s",
2337711facfSdinak 		    gettext("Command failed due to OpenSSL error."));
2347c478bd9Sstevel@tonic-gate 		break;
2357c478bd9Sstevel@tonic-gate 	default:
2367711facfSdinak 		cryptoerror(LOG_STDERR, "%s (%d).",
2377c478bd9Sstevel@tonic-gate 		    gettext("Unknown error value"), rv);
2387c478bd9Sstevel@tonic-gate 		break;
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 	return (rv);
2417c478bd9Sstevel@tonic-gate }
242