xref: /titanic_44/usr/src/cmd/cmd-crypto/pktool/pktool.c (revision 7711facfe58561dd91d6ece0f5f41150c3956c83)
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 /*
23*7711facfSdinak  * 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.
31*7711facfSdinak  * Upon parsing the command verbs from user input, it
32*7711facfSdinak  * branches to the appropriate modules to perform the
33*7711facfSdinak  * 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 *[]);
54*7711facfSdinak 	int	mode;
55*7711facfSdinak 	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[]);
60*7711facfSdinak extern int	pk_list(int argc, char *argv[]);
61*7711facfSdinak extern int	pk_delete(int argc, char *argv[]);
62*7711facfSdinak extern int	pk_import(int argc, char *argv[]);
63*7711facfSdinak extern int	pk_export(int argc, char *argv[]);
64*7711facfSdinak extern int	pk_tokens(int argc, char *argv[]);
65*7711facfSdinak 
66*7711facfSdinak /* Forward declarations for "built-in" verb actions. */
67*7711facfSdinak 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[] = {
71*7711facfSdinak 	{ "tokens",	pk_tokens,	0,	"tokens" },
72*7711facfSdinak 	{ "setpin",	pk_setpin,	0,	"setpin" },
73*7711facfSdinak 	{ "list",	pk_list,	0,	"list [-p] [-P] [-l <label>]"
74*7711facfSdinak 	    "\n\t\tor list [--public] [--private] [--label[=]<label>]" },
75*7711facfSdinak 	{ "delete",	pk_delete,	0,
76*7711facfSdinak 	    "delete { [-p] [-P] [-l <label>] }"
77*7711facfSdinak 	    "\n\t\tor delete { [--public] [--private] [--label[=]<label>] }" },
78*7711facfSdinak 	{ "import",	pk_import,	0,	"import <file>" },
79*7711facfSdinak 	{ "export",	pk_export,	0,	"export <file>" },
80*7711facfSdinak 	{ "-?",		pk_help,	0,	"--help\t(help and usage)" },
817c478bd9Sstevel@tonic-gate };
827c478bd9Sstevel@tonic-gate static int	num_cmds = sizeof (cmds) / sizeof (verbcmd);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static char	*prog;
857c478bd9Sstevel@tonic-gate static void	usage(void);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  * Usage information.  This function must be updated when new verbs or
897c478bd9Sstevel@tonic-gate  * options are added.
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate static void
927c478bd9Sstevel@tonic-gate usage(void)
937c478bd9Sstevel@tonic-gate {
94*7711facfSdinak 	int	i;
95*7711facfSdinak 
96*7711facfSdinak 	cryptodebug("inside usage");
97*7711facfSdinak 
98*7711facfSdinak 	/* Display this block only in command-line mode. */
99*7711facfSdinak 	(void) fprintf(stdout, gettext("Usage:\n"));
100*7711facfSdinak 	(void) fprintf(stdout, gettext("\t%s -?\t(help and usage)\n"), prog);
101*7711facfSdinak 	(void) fprintf(stdout, gettext("\t%s subcommand [options...]\n"), prog);
102*7711facfSdinak 	(void) fprintf(stdout, gettext("where subcommands may be:\n"));
103*7711facfSdinak 
104*7711facfSdinak 	/* Display only those verbs that match the current tool mode. */
105*7711facfSdinak 	for (i = 0; i < num_cmds; i++) {
106*7711facfSdinak 		/* Do NOT i18n/l10n. */
107*7711facfSdinak 		(void) fprintf(stdout, "\t%s\n", cmds[i].synopsis);
108*7711facfSdinak 	}
109*7711facfSdinak }
110*7711facfSdinak 
111*7711facfSdinak /*
112*7711facfSdinak  * Provide help, in the form of displaying the usage.
113*7711facfSdinak  */
114*7711facfSdinak static int
115*7711facfSdinak pk_help(int argc, char *argv[])
116*7711facfSdinak /* ARGSUSED */
117*7711facfSdinak {
118*7711facfSdinak 	cryptodebug("inside pk_help");
119*7711facfSdinak 
120*7711facfSdinak 	usage();
121*7711facfSdinak 	return (0);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * MAIN() -- where all the action is
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate int
1287c478bd9Sstevel@tonic-gate main(int argc, char *argv[], char *envp[])
1297c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	int	i, found = -1;
1327c478bd9Sstevel@tonic-gate 	int	rv;
1337c478bd9Sstevel@tonic-gate 	int	pk_argc = 0;
1347c478bd9Sstevel@tonic-gate 	char	**pk_argv = NULL;
135*7711facfSdinak 	int	save_errno = 0;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	/* Set up for i18n/l10n. */
1387c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1397c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D. */
1407c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it isn't. */
1417c478bd9Sstevel@tonic-gate #endif
1427c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	/* Get program base name and move pointer over 0th arg. */
1457c478bd9Sstevel@tonic-gate 	prog = basename(argv[0]);
1467c478bd9Sstevel@tonic-gate 	argv++, argc--;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	/* Set up for debug and error output. */
1497c478bd9Sstevel@tonic-gate 	cryptodebug_init(prog);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (argc == 0) {
1527c478bd9Sstevel@tonic-gate 		usage();
1537c478bd9Sstevel@tonic-gate 		return (1);
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
156*7711facfSdinak 	/* Check for help options.  For CLIP-compliance. */
157*7711facfSdinak 	if (argc == 1 && argv[0][0] == '-') {
158*7711facfSdinak 		switch (argv[0][1]) {
159*7711facfSdinak 		case '?':
160*7711facfSdinak 			return (pk_help(argc, argv));
161*7711facfSdinak 		default:
162*7711facfSdinak 			usage();
163*7711facfSdinak 			return (1);
164*7711facfSdinak 		}
165*7711facfSdinak 	}
166*7711facfSdinak 
167*7711facfSdinak 	/* Always turns off Metaslot so that we can see softtoken. */
168*7711facfSdinak 	cryptodebug("disabling Metaslot");
1697c478bd9Sstevel@tonic-gate 	if (setenv("METASLOT_ENABLED", "false", 1) < 0) {
170*7711facfSdinak 		save_errno = errno;
1717c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR,
172*7711facfSdinak 		    gettext("Disabling Metaslot failed (%s)."),
173*7711facfSdinak 		    strerror(save_errno));
1747c478bd9Sstevel@tonic-gate 		return (1);
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	/* Begin parsing command line. */
178*7711facfSdinak 	cryptodebug("begin parsing command line");
1797c478bd9Sstevel@tonic-gate 	pk_argc = argc;
1807c478bd9Sstevel@tonic-gate 	pk_argv = argv;
1817c478bd9Sstevel@tonic-gate 
182*7711facfSdinak 	/* Check for valid verb (or an abbreviation of it). */
1837c478bd9Sstevel@tonic-gate 	found = -1;
1847c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_cmds; i++) {
1857c478bd9Sstevel@tonic-gate 		if (strcmp(cmds[i].verb, pk_argv[0]) == 0) {
1867c478bd9Sstevel@tonic-gate 			if (found < 0) {
187*7711facfSdinak 				cryptodebug("found cmd %s", cmds[i].verb);
1887c478bd9Sstevel@tonic-gate 				found = i;
1897c478bd9Sstevel@tonic-gate 				break;
190*7711facfSdinak 			} else {
191*7711facfSdinak 				cryptodebug("also found cmd %s, skipping",
192*7711facfSdinak 				    cmds[i].verb);
1937c478bd9Sstevel@tonic-gate 			}
1947c478bd9Sstevel@tonic-gate 		}
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 	/* Stop here if no valid verb found. */
1977c478bd9Sstevel@tonic-gate 	if (found < 0) {
198*7711facfSdinak 		cryptoerror(LOG_STDERR, gettext("Invalid verb: %s"),
199*7711facfSdinak 		    pk_argv[0]);
2007c478bd9Sstevel@tonic-gate 		return (1);
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/* Get to work! */
204*7711facfSdinak 	cryptodebug("begin executing cmd action");
2057c478bd9Sstevel@tonic-gate 	rv = (*cmds[found].action)(pk_argc, pk_argv);
206*7711facfSdinak 	cryptodebug("end executing cmd action");
2077c478bd9Sstevel@tonic-gate 	switch (rv) {
2087c478bd9Sstevel@tonic-gate 	case PK_ERR_NONE:
209*7711facfSdinak 		cryptodebug("subcommand succeeded");
2107c478bd9Sstevel@tonic-gate 		break;		/* Command succeeded, do nothing. */
2117c478bd9Sstevel@tonic-gate 	case PK_ERR_USAGE:
212*7711facfSdinak 		cryptodebug("usage error detected");
2137c478bd9Sstevel@tonic-gate 		usage();
2147c478bd9Sstevel@tonic-gate 		break;
2157c478bd9Sstevel@tonic-gate 	case PK_ERR_QUIT:
216*7711facfSdinak 		cryptodebug("quit command received");
2177c478bd9Sstevel@tonic-gate 		exit(0);
2187c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
219*7711facfSdinak 	case PK_ERR_PK11:
2207c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, "%s",
221*7711facfSdinak 		    gettext("Command failed due to PKCS#11 error."));
2227c478bd9Sstevel@tonic-gate 		break;
223*7711facfSdinak 	case PK_ERR_SYSTEM:
2247c478bd9Sstevel@tonic-gate 		cryptoerror(LOG_STDERR, "%s",
225*7711facfSdinak 		    gettext("Command failed due to system error."));
2267c478bd9Sstevel@tonic-gate 		break;
227*7711facfSdinak 	case PK_ERR_OPENSSL:
228*7711facfSdinak 		cryptoerror(LOG_STDERR, "%s",
229*7711facfSdinak 		    gettext("Command failed due to OpenSSL error."));
2307c478bd9Sstevel@tonic-gate 		break;
2317c478bd9Sstevel@tonic-gate 	default:
232*7711facfSdinak 		cryptoerror(LOG_STDERR, "%s (%d).",
2337c478bd9Sstevel@tonic-gate 		    gettext("Unknown error value"), rv);
2347c478bd9Sstevel@tonic-gate 		break;
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 	return (rv);
2377c478bd9Sstevel@tonic-gate }
238