xref: /titanic_44/usr/src/cmd/cmd-crypto/pktool/common.c (revision 30a5e8fa1253cb33980ee4514743cf683f584b4e)
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
599ebb4caSwyllys  * Common Development and Distribution License (the "License").
699ebb4caSwyllys  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22c197cb9dShylee  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * This file contains the functions that are shared among
307c478bd9Sstevel@tonic-gate  * the various services this tool will ultimately provide.
317711facfSdinak  * The functions in this file return PKCS#11 CK_RV errors.
327711facfSdinak  * Only one session and one login per token is supported
337711facfSdinak  * at this time.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <ctype.h>
4099ebb4caSwyllys #include <sys/types.h>
4199ebb4caSwyllys #include <sys/stat.h>
4299ebb4caSwyllys #include <fcntl.h>
4399ebb4caSwyllys #include <tzfile.h>
447c478bd9Sstevel@tonic-gate #include <cryptoutil.h>
457c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
4699ebb4caSwyllys #include <kmfapi.h>
477c478bd9Sstevel@tonic-gate 
4899ebb4caSwyllys #include "common.h"
497711facfSdinak 
507711facfSdinak /* Local status variables. */
517711facfSdinak static boolean_t	initialized = B_FALSE;
527711facfSdinak static boolean_t	session_opened = B_FALSE;
537711facfSdinak static boolean_t	logged_in = B_FALSE;
547c478bd9Sstevel@tonic-gate 
5549e21299Sdinak /* Supporting structures and global variables for getopt_av(). */
5649e21299Sdinak typedef struct	av_opts_s {
5749e21299Sdinak 	int		shortnm;	/* short name character */
5849e21299Sdinak 	char		*longnm;	/* long name string, NOT terminated */
5949e21299Sdinak 	int		longnm_len;	/* length of long name string */
6049e21299Sdinak 	boolean_t	has_arg;	/* takes optional argument */
6149e21299Sdinak } av_opts;
6249e21299Sdinak static av_opts		*opts_av = NULL;
6349e21299Sdinak static const char	*_save_optstr = NULL;
6449e21299Sdinak static int		_save_numopts = 0;
6549e21299Sdinak 
6649e21299Sdinak int			optind_av = 1;
6749e21299Sdinak char			*optarg_av = NULL;
6849e21299Sdinak 
6999ebb4caSwyllys static void close_sess(CK_SESSION_HANDLE);
7099ebb4caSwyllys static void logout_token(CK_SESSION_HANDLE);
7199ebb4caSwyllys 
727c478bd9Sstevel@tonic-gate /*
737711facfSdinak  * Perform PKCS#11 setup here.  Currently only C_Initialize is required,
747711facfSdinak  * along with setting/resetting state variables.
757c478bd9Sstevel@tonic-gate  */
767711facfSdinak CK_RV
777c478bd9Sstevel@tonic-gate init_pk11(void)
787c478bd9Sstevel@tonic-gate {
797711facfSdinak 	CK_RV		rv = CKR_OK;
807c478bd9Sstevel@tonic-gate 
817711facfSdinak 	/* If C_Initialize() already called, nothing to do here. */
827711facfSdinak 	if (initialized == B_TRUE)
837711facfSdinak 		return (CKR_OK);
847711facfSdinak 
857711facfSdinak 	/* Reset state variables because C_Initialize() not yet done. */
867711facfSdinak 	session_opened = B_FALSE;
877711facfSdinak 	logged_in = B_FALSE;
887711facfSdinak 
897c478bd9Sstevel@tonic-gate 	/* Initialize PKCS#11 library. */
907c478bd9Sstevel@tonic-gate 	if ((rv = C_Initialize(NULL_PTR)) != CKR_OK &&
917c478bd9Sstevel@tonic-gate 	    rv != CKR_CRYPTOKI_ALREADY_INITIALIZED) {
927711facfSdinak 		return (rv);
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate 
957711facfSdinak 	initialized = B_TRUE;
967711facfSdinak 	return (CKR_OK);
977711facfSdinak }
987711facfSdinak 
997711facfSdinak /*
1007711facfSdinak  * Finalize PKCS#11 library and reset state variables.  Open sessions,
1017711facfSdinak  * if any, are closed, and thereby any logins are logged out also.
1027711facfSdinak  */
1037711facfSdinak void
1047711facfSdinak final_pk11(CK_SESSION_HANDLE sess)
1057711facfSdinak {
1067711facfSdinak 
1077711facfSdinak 	/* If the library wasn't initialized, nothing to do here. */
1087711facfSdinak 	if (!initialized)
1097711facfSdinak 		return;
1107711facfSdinak 
1117711facfSdinak 	/* Make sure the sesion is closed first. */
1127711facfSdinak 	close_sess(sess);
1137711facfSdinak 
1147711facfSdinak 	(void) C_Finalize(NULL);
1157711facfSdinak 	initialized = B_FALSE;
1167711facfSdinak }
1177711facfSdinak 
1187711facfSdinak /*
1197711facfSdinak  * Close PKCS#11 session and reset state variables.  Any logins are
1207711facfSdinak  * logged out.
1217711facfSdinak  */
12299ebb4caSwyllys static void
1237711facfSdinak close_sess(CK_SESSION_HANDLE sess)
1247711facfSdinak {
1257711facfSdinak 
1267711facfSdinak 	if (sess == NULL) {
1277711facfSdinak 		return;
1287711facfSdinak 	}
1297711facfSdinak 
1307711facfSdinak 	/* If session is already closed, nothing to do here. */
1317711facfSdinak 	if (!session_opened)
1327711facfSdinak 		return;
1337711facfSdinak 
1347711facfSdinak 	/* Make sure user is logged out of token. */
1357711facfSdinak 	logout_token(sess);
1367711facfSdinak 
1377711facfSdinak 	(void) C_CloseSession(sess);
1387711facfSdinak 	session_opened = B_FALSE;
1397711facfSdinak }
1407711facfSdinak 
1417711facfSdinak /*
1427711facfSdinak  * Log user out of token and reset status variable.
1437711facfSdinak  */
14499ebb4caSwyllys static void
1457711facfSdinak logout_token(CK_SESSION_HANDLE sess)
1467711facfSdinak {
1477711facfSdinak 
1487711facfSdinak 	if (sess == NULL) {
1497711facfSdinak 		return;
1507711facfSdinak 	}
1517711facfSdinak 
1527711facfSdinak 	/* If already logged out, nothing to do here. */
1537711facfSdinak 	if (!logged_in)
1547711facfSdinak 		return;
1557711facfSdinak 
1567711facfSdinak 	(void) C_Logout(sess);
1577711facfSdinak 	logged_in = B_FALSE;
1587711facfSdinak }
1597711facfSdinak 
1607711facfSdinak /*
1617711facfSdinak  * Gets PIN from user.  Caller needs to free the returned PIN when done.
1627711facfSdinak  * If two prompts are given, the PIN is confirmed with second prompt.
1637711facfSdinak  * Note that getphassphrase() may return data in static memory area.
1647711facfSdinak  */
1657711facfSdinak CK_RV
1667711facfSdinak get_pin(char *prompt1, char *prompt2, CK_UTF8CHAR_PTR *pin, CK_ULONG *pinlen)
1677711facfSdinak {
1687711facfSdinak 	char		*save_phrase, *phrase1, *phrase2;
1697711facfSdinak 
17099ebb4caSwyllys 
17199ebb4caSwyllys #ifdef DEBUG
17299ebb4caSwyllys 	if (getenv("TOKENPIN") != NULL) {
17399ebb4caSwyllys 		*pin = (CK_UTF8CHAR_PTR)strdup(getenv("TOKENPIN"));
17499ebb4caSwyllys 		*pinlen = strlen((char *)(*pin));
17599ebb4caSwyllys 		return (CKR_OK);
17699ebb4caSwyllys 	}
17799ebb4caSwyllys #endif /* DEBUG */
1787711facfSdinak 
1797711facfSdinak 	/* Prompt user for a PIN. */
1807711facfSdinak 	if (prompt1 == NULL) {
1817711facfSdinak 		return (CKR_ARGUMENTS_BAD);
1827711facfSdinak 	}
1837711facfSdinak 	if ((phrase1 = getpassphrase(prompt1)) == NULL) {
1847711facfSdinak 		return (CKR_FUNCTION_FAILED);
1857711facfSdinak 	}
1867711facfSdinak 
1877711facfSdinak 	/* Duplicate 1st PIN in separate chunk of memory. */
1887711facfSdinak 	if ((save_phrase = strdup(phrase1)) == NULL)
1897711facfSdinak 		return (CKR_HOST_MEMORY);
1907711facfSdinak 
1917711facfSdinak 	/* If second prompt given, PIN confirmation is requested. */
1927711facfSdinak 	if (prompt2 != NULL) {
1937711facfSdinak 		if ((phrase2 = getpassphrase(prompt2)) == NULL) {
1947711facfSdinak 			free(save_phrase);
1957711facfSdinak 			return (CKR_FUNCTION_FAILED);
1967711facfSdinak 		}
1977711facfSdinak 		if (strcmp(save_phrase, phrase2) != 0) {
1987711facfSdinak 			free(save_phrase);
1997711facfSdinak 			return (CKR_PIN_INCORRECT);
2007711facfSdinak 		}
2017711facfSdinak 	}
2027711facfSdinak 
2037711facfSdinak 	*pin = (CK_UTF8CHAR_PTR)save_phrase;
2047711facfSdinak 	*pinlen = strlen(save_phrase);
2057711facfSdinak 	return (CKR_OK);
2067711facfSdinak }
2077711facfSdinak 
2087711facfSdinak /*
2097711facfSdinak  * Gets yes/no response from user.  If either no prompt is supplied, a
2107711facfSdinak  * default prompt is used.  If not message for invalid input is supplied,
2117711facfSdinak  * a default will not be provided.  If the user provides no response,
2127711facfSdinak  * the input default B_TRUE == yes, B_FALSE == no is returned.
2137711facfSdinak  * Otherwise, B_TRUE is returned for yes, and B_FALSE for no.
2147711facfSdinak  */
2157711facfSdinak boolean_t
2167711facfSdinak yesno(char *prompt, char *invalid, boolean_t dflt)
2177711facfSdinak {
2187711facfSdinak 	char		*response, buf[1024];
2197711facfSdinak 	char		*yes = gettext("yes");
2207711facfSdinak 	char		*no = gettext("no");
2217711facfSdinak 
22299ebb4caSwyllys 
22399ebb4caSwyllys #ifdef DEBUG
22499ebb4caSwyllys 	/* If debugging or testing, return TRUE and avoid prompting */
22599ebb4caSwyllys 	if (getenv("TOKENPIN") != NULL) {
22699ebb4caSwyllys 		return (B_TRUE);
22799ebb4caSwyllys 	}
22899ebb4caSwyllys #endif /* DEBUG */
2297711facfSdinak 
2307711facfSdinak 	if (prompt == NULL)
2317711facfSdinak 		prompt = gettext("Enter (y)es or (n)o? ");
2327711facfSdinak 
2337711facfSdinak 	for (;;) {
2347711facfSdinak 		/* Prompt user. */
2357711facfSdinak 		(void) printf("%s", prompt);
2367711facfSdinak 		(void) fflush(stdout);
2377711facfSdinak 
2387711facfSdinak 		/* Get the response. */
2397711facfSdinak 		if ((response = fgets(buf, sizeof (buf), stdin)) == NULL)
2407711facfSdinak 			break;		/* go to default response */
2417711facfSdinak 
2427711facfSdinak 		/* Skip any leading white space. */
2437711facfSdinak 		while (isspace(*response))
2447711facfSdinak 			response++;
2457711facfSdinak 		if (*response == '\0')
2467711facfSdinak 			break;		/* go to default response */
2477711facfSdinak 
2487711facfSdinak 		/* Is it valid input?  Return appropriately. */
2497711facfSdinak 		if (strncasecmp(response, yes, 1) == 0)
2507711facfSdinak 			return (B_TRUE);
2517711facfSdinak 		if (strncasecmp(response, no, 1) == 0)
2527711facfSdinak 			return (B_FALSE);
2537711facfSdinak 
2547711facfSdinak 		/* Indicate invalid input, and try again. */
2557711facfSdinak 		if (invalid != NULL)
2567711facfSdinak 			(void) printf("%s", invalid);
2577711facfSdinak 	}
2587711facfSdinak 	return (dflt);
2597711facfSdinak }
2607711facfSdinak 
2617711facfSdinak /*
2627711facfSdinak  * Gets the list of slots which have tokens in them.  Keeps adjusting
2637711facfSdinak  * the size of the slot list buffer until the call is successful or an
2647711facfSdinak  * irrecoverable error occurs.
2657711facfSdinak  */
2667711facfSdinak CK_RV
2677711facfSdinak get_token_slots(CK_SLOT_ID_PTR *slot_list, CK_ULONG *slot_count)
2687711facfSdinak {
2697711facfSdinak 	CK_ULONG	tmp_count = 0;
2707711facfSdinak 	CK_SLOT_ID_PTR	tmp_list = NULL_PTR, tmp2_list = NULL_PTR;
2717711facfSdinak 	int		rv = CKR_OK;
2727711facfSdinak 
2737711facfSdinak 	if (!initialized)
2747711facfSdinak 		if ((rv = init_pk11()) != CKR_OK)
2757711facfSdinak 			return (rv);
2767711facfSdinak 
2777711facfSdinak 	/*
2787711facfSdinak 	 * Get the slot count first because we don't know how many
2797711facfSdinak 	 * slots there are and how many of those slots even have tokens.
2807711facfSdinak 	 * Don't specify an arbitrary buffer size for the slot list;
2817711facfSdinak 	 * it may be too small (see section 11.5 of PKCS#11 spec).
2827711facfSdinak 	 * Also select only those slots that have tokens in them,
2837711facfSdinak 	 * because this tool has no need to know about empty slots.
2847711facfSdinak 	 */
2857711facfSdinak 	if ((rv = C_GetSlotList(1, NULL_PTR, &tmp_count)) != CKR_OK)
2867711facfSdinak 		return (rv);
2877711facfSdinak 
2887711facfSdinak 	if (tmp_count == 0) {
2897711facfSdinak 		*slot_list = NULL_PTR;
2907711facfSdinak 		*slot_count = 0;
2917711facfSdinak 		return (CKR_OK);
2927711facfSdinak 	}
2937711facfSdinak 
2947711facfSdinak 	/* Allocate initial space for the slot list. */
2957711facfSdinak 	if ((tmp_list = (CK_SLOT_ID_PTR) malloc(tmp_count *
2967711facfSdinak 	    sizeof (CK_SLOT_ID))) == NULL)
2977711facfSdinak 		return (CKR_HOST_MEMORY);
2987711facfSdinak 
2997711facfSdinak 	/* Then get the slot list itself. */
3007711facfSdinak 	for (;;) {
3017711facfSdinak 		if ((rv = C_GetSlotList(1, tmp_list, &tmp_count)) == CKR_OK) {
3027711facfSdinak 			*slot_list = tmp_list;
3037711facfSdinak 			*slot_count = tmp_count;
3047711facfSdinak 			break;
3057711facfSdinak 		}
3067711facfSdinak 
3077711facfSdinak 		if (rv != CKR_BUFFER_TOO_SMALL) {
3087711facfSdinak 			free(tmp_list);
3097711facfSdinak 			break;
3107711facfSdinak 		}
3117711facfSdinak 
3127711facfSdinak 		/* If the number of slots grew, try again. */
3137711facfSdinak 		if ((tmp2_list = (CK_SLOT_ID_PTR) realloc(tmp_list,
3147711facfSdinak 		    tmp_count * sizeof (CK_SLOT_ID))) == NULL) {
3157711facfSdinak 			free(tmp_list);
3167711facfSdinak 			rv = CKR_HOST_MEMORY;
3177711facfSdinak 			break;
3187711facfSdinak 		}
3197711facfSdinak 		tmp_list = tmp2_list;
3207711facfSdinak 	}
3217711facfSdinak 
3227711facfSdinak 	return (rv);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate /*
32649e21299Sdinak  * Breaks out the getopt-style option string into a structure that can be
32749e21299Sdinak  * traversed later for calls to getopt_av().  Option string is NOT altered,
32849e21299Sdinak  * but the struct fields point to locations within option string.
32949e21299Sdinak  */
33049e21299Sdinak static int
33149e21299Sdinak populate_opts(char *optstring)
33249e21299Sdinak {
33349e21299Sdinak 	int		i;
33449e21299Sdinak 	av_opts		*temp;
33549e21299Sdinak 	char		*marker;
33649e21299Sdinak 
33749e21299Sdinak 	if (optstring == NULL || *optstring == '\0')
33849e21299Sdinak 		return (0);
33949e21299Sdinak 
34049e21299Sdinak 	/*
34149e21299Sdinak 	 * This tries to imitate getopt(3c) Each option must conform to:
34249e21299Sdinak 	 * <short name char> [ ':' ] [ '(' <long name string> ')' ]
34349e21299Sdinak 	 * If long name is missing, the short name is used for long name.
34449e21299Sdinak 	 */
34549e21299Sdinak 	for (i = 0; *optstring != '\0'; i++) {
34649e21299Sdinak 		if ((temp = (av_opts *)((i == 0) ? malloc(sizeof (av_opts)) :
34749e21299Sdinak 		    realloc(opts_av, (i+1) * sizeof (av_opts)))) == NULL) {
34899ebb4caSwyllys 			if (opts_av != NULL)
34949e21299Sdinak 				free(opts_av);
35049e21299Sdinak 			opts_av = NULL;
35149e21299Sdinak 			return (0);
35299ebb4caSwyllys 		} else {
35349e21299Sdinak 			opts_av = (av_opts *)temp;
35499ebb4caSwyllys 		}
35549e21299Sdinak 
35699ebb4caSwyllys 		(void) memset(&opts_av[i], 0, sizeof (av_opts));
35749e21299Sdinak 		marker = optstring;		/* may need optstring later */
35849e21299Sdinak 
35949e21299Sdinak 		opts_av[i].shortnm = *marker++;	/* set short name */
36049e21299Sdinak 
36149e21299Sdinak 		if (*marker == ':') {		/* check for opt arg */
36249e21299Sdinak 			marker++;
36349e21299Sdinak 			opts_av[i].has_arg = B_TRUE;
36449e21299Sdinak 		}
36549e21299Sdinak 
36649e21299Sdinak 		if (*marker == '(') {		/* check and set long name */
36749e21299Sdinak 			marker++;
36849e21299Sdinak 			opts_av[i].longnm = marker;
36949e21299Sdinak 			opts_av[i].longnm_len = strcspn(marker, ")");
37049e21299Sdinak 			optstring = marker + opts_av[i].longnm_len + 1;
37149e21299Sdinak 		} else {
37249e21299Sdinak 			/* use short name option character */
37349e21299Sdinak 			opts_av[i].longnm = optstring;
37449e21299Sdinak 			opts_av[i].longnm_len = 1;
37549e21299Sdinak 			optstring = marker;
37649e21299Sdinak 		}
37749e21299Sdinak 	}
37849e21299Sdinak 
37949e21299Sdinak 	return (i);
38049e21299Sdinak }
38149e21299Sdinak 
38249e21299Sdinak /*
38349e21299Sdinak  * getopt_av() is very similar to getopt(3c) in that the takes an option
38449e21299Sdinak  * string, compares command line arguments for matches, and returns a single
38549e21299Sdinak  * letter option when a match is found.  However, getopt_av() differs from
38649e21299Sdinak  * getopt(3c) by requiring that only longname options and values be found
38749e21299Sdinak  * on the command line and all leading dashes are omitted.  In other words,
38849e21299Sdinak  * it tries to enforce only longname "option=value" arguments on the command
38949e21299Sdinak  * line.  Boolean options are not allowed either.
39049e21299Sdinak  */
39149e21299Sdinak int
39249e21299Sdinak getopt_av(int argc, char * const *argv, const char *optstring)
39349e21299Sdinak {
39449e21299Sdinak 	int	i;
39549e21299Sdinak 	int	len;
39699ebb4caSwyllys 	char   *cur_option;
39749e21299Sdinak 
39849e21299Sdinak 	if (optind_av >= argc)
39949e21299Sdinak 		return (EOF);
40049e21299Sdinak 
40149e21299Sdinak 	/* First time or when optstring changes from previous one */
40249e21299Sdinak 	if (_save_optstr != optstring) {
40349e21299Sdinak 		if (opts_av != NULL)
40449e21299Sdinak 			free(opts_av);
40549e21299Sdinak 		opts_av = NULL;
40649e21299Sdinak 		_save_optstr = optstring;
40749e21299Sdinak 		_save_numopts = populate_opts((char *)optstring);
40849e21299Sdinak 	}
40949e21299Sdinak 
41049e21299Sdinak 	for (i = 0; i < _save_numopts; i++) {
41199ebb4caSwyllys 		cur_option = argv[optind_av];
41299ebb4caSwyllys 
41399ebb4caSwyllys 		if (strcmp(cur_option, "--") == 0) {
41449e21299Sdinak 			optind_av++;
41549e21299Sdinak 			break;
41649e21299Sdinak 		}
41749e21299Sdinak 
41899ebb4caSwyllys 		if (cur_option[0] == '-' && strlen(cur_option) == 2) {
41999ebb4caSwyllys 			len = 1;
42099ebb4caSwyllys 			cur_option++; /* remove "-" */
42199ebb4caSwyllys 		} else {
42299ebb4caSwyllys 			len = strcspn(cur_option, "=");
42399ebb4caSwyllys 		}
42449e21299Sdinak 
42599ebb4caSwyllys 		if (len == opts_av[i].longnm_len && strncmp(cur_option,
42649e21299Sdinak 		    opts_av[i].longnm, opts_av[i].longnm_len) == 0) {
42749e21299Sdinak 			/* matched */
42849e21299Sdinak 			if (!opts_av[i].has_arg) {
42949e21299Sdinak 				optind_av++;
43049e21299Sdinak 				return (opts_av[i].shortnm);
43149e21299Sdinak 			}
43249e21299Sdinak 
43349e21299Sdinak 			/* needs optarg */
43499ebb4caSwyllys 			if (cur_option[len] == '=') {
43599ebb4caSwyllys 				optarg_av = &(cur_option[len+1]);
43649e21299Sdinak 				optind_av++;
43749e21299Sdinak 				return (opts_av[i].shortnm);
43849e21299Sdinak 			}
43949e21299Sdinak 
44049e21299Sdinak 			optarg_av = NULL;
44149e21299Sdinak 			optind_av++;
44249e21299Sdinak 			return ((int)'?');
44349e21299Sdinak 		}
44449e21299Sdinak 	}
44549e21299Sdinak 
44649e21299Sdinak 	return (EOF);
44749e21299Sdinak }
44899ebb4caSwyllys 
44999ebb4caSwyllys KMF_KEYSTORE_TYPE
45099ebb4caSwyllys KS2Int(char *keystore_str)
45199ebb4caSwyllys {
45299ebb4caSwyllys 	if (keystore_str == NULL)
45399ebb4caSwyllys 		return (0);
45499ebb4caSwyllys 	if (!strcasecmp(keystore_str, "pkcs11"))
45599ebb4caSwyllys 		return (KMF_KEYSTORE_PK11TOKEN);
45699ebb4caSwyllys 	else if (!strcasecmp(keystore_str, "nss"))
45799ebb4caSwyllys 		return (KMF_KEYSTORE_NSS);
45899ebb4caSwyllys 	else if (!strcasecmp(keystore_str, "file"))
45999ebb4caSwyllys 		return (KMF_KEYSTORE_OPENSSL);
46099ebb4caSwyllys 	else
46199ebb4caSwyllys 		return (0);
46299ebb4caSwyllys }
46399ebb4caSwyllys 
46499ebb4caSwyllys 
46599ebb4caSwyllys int
46699ebb4caSwyllys Str2KeyType(char *algm, KMF_KEY_ALG *ktype, KMF_ALGORITHM_INDEX *sigAlg)
46799ebb4caSwyllys {
46899ebb4caSwyllys 	if (algm == NULL) {
46999ebb4caSwyllys 		*sigAlg = KMF_ALGID_MD5WithRSA;
47099ebb4caSwyllys 		*ktype = KMF_RSA;
47199ebb4caSwyllys 	} else if (strcasecmp(algm, "DSA") == 0) {
47299ebb4caSwyllys 		*sigAlg = KMF_ALGID_SHA1WithDSA;
47399ebb4caSwyllys 		*ktype = KMF_DSA;
47499ebb4caSwyllys 	} else if (strcasecmp(algm, "RSA") == 0) {
47599ebb4caSwyllys 		*sigAlg = KMF_ALGID_MD5WithRSA;
47699ebb4caSwyllys 		*ktype = KMF_RSA;
47799ebb4caSwyllys 	} else {
47899ebb4caSwyllys 		return (-1);
47999ebb4caSwyllys 	}
48099ebb4caSwyllys 	return (0);
48199ebb4caSwyllys }
48299ebb4caSwyllys 
48399ebb4caSwyllys int
48499ebb4caSwyllys Str2SymKeyType(char *algm, KMF_KEY_ALG *ktype)
48599ebb4caSwyllys {
48699ebb4caSwyllys 	if (algm == NULL)
48799ebb4caSwyllys 		*ktype = KMF_AES;
48899ebb4caSwyllys 	else if (strcasecmp(algm, "aes") == 0)
48999ebb4caSwyllys 		*ktype = KMF_AES;
49099ebb4caSwyllys 	else if (strcasecmp(algm, "arcfour") == 0)
49199ebb4caSwyllys 		*ktype = KMF_RC4;
49299ebb4caSwyllys 	else if (strcasecmp(algm, "des") == 0)
49399ebb4caSwyllys 		*ktype = KMF_DES;
49499ebb4caSwyllys 	else if (strcasecmp(algm, "3des") == 0)
49599ebb4caSwyllys 		*ktype = KMF_DES3;
496c197cb9dShylee 	else if (strcasecmp(algm, "generic") == 0)
497c197cb9dShylee 		*ktype = KMF_GENERIC_SECRET;
49899ebb4caSwyllys 	else
49999ebb4caSwyllys 		return (-1);
50099ebb4caSwyllys 
50199ebb4caSwyllys 	return (0);
50299ebb4caSwyllys }
50399ebb4caSwyllys 
50499ebb4caSwyllys int
50599ebb4caSwyllys Str2Lifetime(char *ltimestr, uint32_t *ltime)
50699ebb4caSwyllys {
50799ebb4caSwyllys 	int num;
50899ebb4caSwyllys 	char timetok[6];
50999ebb4caSwyllys 
51099ebb4caSwyllys 	if (ltimestr == NULL || !strlen(ltimestr)) {
51199ebb4caSwyllys 		/* default to 1 year lifetime */
51299ebb4caSwyllys 		*ltime = SECSPERDAY * DAYSPERNYEAR;
51399ebb4caSwyllys 		return (0);
51499ebb4caSwyllys 	}
51599ebb4caSwyllys 
51699ebb4caSwyllys 	(void) memset(timetok, 0, sizeof (timetok));
51799ebb4caSwyllys 	if (sscanf(ltimestr, "%d-%06s", &num, timetok) != 2)
51899ebb4caSwyllys 		return (-1);
51999ebb4caSwyllys 
52099ebb4caSwyllys 	if (!strcasecmp(timetok, "day") ||
52199ebb4caSwyllys 	    !strcasecmp(timetok, "days")) {
52299ebb4caSwyllys 		*ltime = num * SECSPERDAY;
52399ebb4caSwyllys 	} else if (!strcasecmp(timetok, "hour") ||
52499ebb4caSwyllys 	    !strcasecmp(timetok, "hours")) {
52599ebb4caSwyllys 		*ltime = num * SECSPERHOUR;
52699ebb4caSwyllys 	} else if (!strcasecmp(timetok, "year") ||
52799ebb4caSwyllys 	    !strcasecmp(timetok, "years")) {
52899ebb4caSwyllys 		*ltime = num * SECSPERDAY * DAYSPERNYEAR;
52999ebb4caSwyllys 	} else {
53099ebb4caSwyllys 		*ltime = 0;
53199ebb4caSwyllys 		return (-1);
53299ebb4caSwyllys 	}
53399ebb4caSwyllys 
53499ebb4caSwyllys 	return (0);
53599ebb4caSwyllys }
53699ebb4caSwyllys 
53799ebb4caSwyllys int
53899ebb4caSwyllys OT2Int(char *objclass)
53999ebb4caSwyllys {
54099ebb4caSwyllys 	char *c = NULL;
54199ebb4caSwyllys 	int retval = 0;
54299ebb4caSwyllys 
54399ebb4caSwyllys 	if (objclass == NULL)
54499ebb4caSwyllys 		return (-1);
54599ebb4caSwyllys 
54699ebb4caSwyllys 	c = strchr(objclass, ':');
54799ebb4caSwyllys 	if (c != NULL) {
54899ebb4caSwyllys 		if (!strcasecmp(c, ":private"))
54999ebb4caSwyllys 			retval = PK_PRIVATE_OBJ;
55099ebb4caSwyllys 		else if (!strcasecmp(c, ":public"))
55199ebb4caSwyllys 			retval = PK_PUBLIC_OBJ;
55299ebb4caSwyllys 		else if (!strcasecmp(c, ":both"))
55399ebb4caSwyllys 			retval = PK_PRIVATE_OBJ | PK_PUBLIC_OBJ;
55499ebb4caSwyllys 		else /* unrecognized option */
55599ebb4caSwyllys 			return (-1);
55699ebb4caSwyllys 
55799ebb4caSwyllys 		*c = '\0';
55899ebb4caSwyllys 	}
55999ebb4caSwyllys 
56099ebb4caSwyllys 	if (!strcasecmp(objclass, "public")) {
56199ebb4caSwyllys 		if (retval)
56299ebb4caSwyllys 			return (-1);
563*30a5e8faSwyllys 		return (retval | PK_PUBLIC_OBJ | PK_CERT_OBJ | PK_PUBKEY_OBJ);
56499ebb4caSwyllys 	} else if (!strcasecmp(objclass, "private")) {
56599ebb4caSwyllys 		if (retval)
56699ebb4caSwyllys 			return (-1);
56799ebb4caSwyllys 		return (retval | PK_PRIKEY_OBJ | PK_PRIVATE_OBJ);
56899ebb4caSwyllys 	} else if (!strcasecmp(objclass, "both")) {
56999ebb4caSwyllys 		if (retval)
57099ebb4caSwyllys 			return (-1);
57199ebb4caSwyllys 		return (PK_KEY_OBJ | PK_PUBLIC_OBJ | PK_PRIVATE_OBJ);
57299ebb4caSwyllys 	} else if (!strcasecmp(objclass, "cert")) {
57399ebb4caSwyllys 		return (retval | PK_CERT_OBJ);
57499ebb4caSwyllys 	} else if (!strcasecmp(objclass, "key")) {
57599ebb4caSwyllys 		if (retval == 0) /* return all keys */
57699ebb4caSwyllys 			return (retval | PK_KEY_OBJ);
57799ebb4caSwyllys 		else if (retval == (PK_PRIVATE_OBJ | PK_PUBLIC_OBJ))
57899ebb4caSwyllys 			/* return all keys */
57999ebb4caSwyllys 			return (retval | PK_KEY_OBJ);
58099ebb4caSwyllys 		else if (retval & PK_PUBLIC_OBJ)
58199ebb4caSwyllys 			/* Only return public keys */
58299ebb4caSwyllys 			return (retval | PK_PUBKEY_OBJ);
58399ebb4caSwyllys 		else if (retval & PK_PRIVATE_OBJ)
58499ebb4caSwyllys 			/* Only return private keys */
58599ebb4caSwyllys 			return (retval | PK_PRIKEY_OBJ);
58699ebb4caSwyllys 	} else if (!strcasecmp(objclass, "crl")) {
58799ebb4caSwyllys 		if (retval)
58899ebb4caSwyllys 			return (-1);
58999ebb4caSwyllys 		return (retval | PK_CRL_OBJ);
59099ebb4caSwyllys 	}
59199ebb4caSwyllys 
59299ebb4caSwyllys 	if (retval == 0) /* No matches found */
59399ebb4caSwyllys 		retval = -1;
59499ebb4caSwyllys 	return (retval);
59599ebb4caSwyllys }
59699ebb4caSwyllys 
59799ebb4caSwyllys KMF_ENCODE_FORMAT
59899ebb4caSwyllys Str2Format(char *formstr)
59999ebb4caSwyllys {
60099ebb4caSwyllys 	if (formstr == NULL || !strcasecmp(formstr, "der"))
60199ebb4caSwyllys 		return (KMF_FORMAT_ASN1);
60299ebb4caSwyllys 	if (!strcasecmp(formstr, "pem"))
60399ebb4caSwyllys 		return (KMF_FORMAT_PEM);
60499ebb4caSwyllys 	if (!strcasecmp(formstr, "pkcs12"))
60599ebb4caSwyllys 		return (KMF_FORMAT_PKCS12);
606*30a5e8faSwyllys 	if (!strcasecmp(formstr, "raw"))
607*30a5e8faSwyllys 		return (KMF_FORMAT_RAWKEY);
60899ebb4caSwyllys 
60999ebb4caSwyllys 	return (KMF_FORMAT_UNDEF);
61099ebb4caSwyllys }
61199ebb4caSwyllys 
61299ebb4caSwyllys 
61399ebb4caSwyllys KMF_RETURN
61499ebb4caSwyllys select_token(void *kmfhandle, char *token,
61599ebb4caSwyllys 	int readonly)
61699ebb4caSwyllys {
617*30a5e8faSwyllys 	KMF_ATTRIBUTE attlist[10];
618*30a5e8faSwyllys 	int i = 0;
619*30a5e8faSwyllys 	KMF_KEYSTORE_TYPE kstype = KMF_KEYSTORE_PK11TOKEN;
62099ebb4caSwyllys 	KMF_RETURN rv = KMF_OK;
62199ebb4caSwyllys 
62299ebb4caSwyllys 	if (token == NULL)
62399ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
62499ebb4caSwyllys 
625*30a5e8faSwyllys 	kmf_set_attr_at_index(attlist, i,
626*30a5e8faSwyllys 	    KMF_KEYSTORE_TYPE_ATTR, &kstype,
627*30a5e8faSwyllys 	    sizeof (kstype));
628*30a5e8faSwyllys 	i++;
62999ebb4caSwyllys 
630*30a5e8faSwyllys 	if (token) {
631*30a5e8faSwyllys 		kmf_set_attr_at_index(attlist, i,
632*30a5e8faSwyllys 		    KMF_TOKEN_LABEL_ATTR, token,
633*30a5e8faSwyllys 		    strlen(token));
634*30a5e8faSwyllys 		i++;
635*30a5e8faSwyllys 	}
636*30a5e8faSwyllys 
637*30a5e8faSwyllys 	kmf_set_attr_at_index(attlist, i,
638*30a5e8faSwyllys 	    KMF_READONLY_ATTR, &readonly,
639*30a5e8faSwyllys 	    sizeof (readonly));
640*30a5e8faSwyllys 	i++;
641*30a5e8faSwyllys 
642*30a5e8faSwyllys 	rv = kmf_configure_keystore(kmfhandle, i, attlist);
64399ebb4caSwyllys 	if (rv == KMF_ERR_TOKEN_SELECTED)
64499ebb4caSwyllys 		rv = KMF_OK;
64599ebb4caSwyllys 	return (rv);
64699ebb4caSwyllys }
64799ebb4caSwyllys 
64899ebb4caSwyllys 
64999ebb4caSwyllys KMF_RETURN
65099ebb4caSwyllys configure_nss(void *kmfhandle, char *dir, char *prefix)
65199ebb4caSwyllys {
652*30a5e8faSwyllys 
653*30a5e8faSwyllys 	KMF_ATTRIBUTE attlist[10];
654*30a5e8faSwyllys 	int i = 0;
655*30a5e8faSwyllys 	KMF_KEYSTORE_TYPE kstype = KMF_KEYSTORE_NSS;
65699ebb4caSwyllys 	KMF_RETURN rv = KMF_OK;
65799ebb4caSwyllys 
658*30a5e8faSwyllys 	kmf_set_attr_at_index(attlist, i,
659*30a5e8faSwyllys 	    KMF_KEYSTORE_TYPE_ATTR, &kstype,
660*30a5e8faSwyllys 	    sizeof (kstype));
661*30a5e8faSwyllys 	i++;
66299ebb4caSwyllys 
663*30a5e8faSwyllys 	if (dir) {
664*30a5e8faSwyllys 		kmf_set_attr_at_index(attlist, i,
665*30a5e8faSwyllys 		    KMF_DIRPATH_ATTR, dir,
666*30a5e8faSwyllys 		    strlen(dir));
667*30a5e8faSwyllys 		i++;
668*30a5e8faSwyllys 	}
669*30a5e8faSwyllys 
670*30a5e8faSwyllys 	if (prefix) {
671*30a5e8faSwyllys 		kmf_set_attr_at_index(attlist, i,
672*30a5e8faSwyllys 		    KMF_CERTPREFIX_ATTR, prefix,
673*30a5e8faSwyllys 		    strlen(prefix));
674*30a5e8faSwyllys 		i++;
675*30a5e8faSwyllys 
676*30a5e8faSwyllys 		kmf_set_attr_at_index(attlist, i,
677*30a5e8faSwyllys 		    KMF_KEYPREFIX_ATTR, prefix,
678*30a5e8faSwyllys 		    strlen(prefix));
679*30a5e8faSwyllys 		i++;
680*30a5e8faSwyllys 	}
681*30a5e8faSwyllys 
682*30a5e8faSwyllys 	rv = kmf_configure_keystore(kmfhandle, i, attlist);
68399ebb4caSwyllys 	if (rv == KMF_KEYSTORE_ALREADY_INITIALIZED)
68499ebb4caSwyllys 		rv = KMF_OK;
68599ebb4caSwyllys 
68699ebb4caSwyllys 	return (rv);
68799ebb4caSwyllys }
68899ebb4caSwyllys 
68999ebb4caSwyllys 
69099ebb4caSwyllys KMF_RETURN
69199ebb4caSwyllys get_pk12_password(KMF_CREDENTIAL *cred)
69299ebb4caSwyllys {
69399ebb4caSwyllys 	KMF_RETURN rv = KMF_OK;
69499ebb4caSwyllys 	char prompt[1024];
69599ebb4caSwyllys 
69699ebb4caSwyllys 	/*
69799ebb4caSwyllys 	 * Get the password to use for the PK12 encryption.
69899ebb4caSwyllys 	 */
69999ebb4caSwyllys 	(void) strlcpy(prompt,
70099ebb4caSwyllys 	    gettext("Enter password to use for "
701*30a5e8faSwyllys 	    "accessing the PKCS12 file: "), sizeof (prompt));
70299ebb4caSwyllys 
70399ebb4caSwyllys 	if (get_pin(prompt, NULL, (uchar_t **)&cred->cred,
70499ebb4caSwyllys 	    (ulong_t *)&cred->credlen) != CKR_OK) {
70599ebb4caSwyllys 		cred->cred = NULL;
70699ebb4caSwyllys 		cred->credlen = 0;
70799ebb4caSwyllys 	}
70899ebb4caSwyllys 
70999ebb4caSwyllys 	return (rv);
71099ebb4caSwyllys }
71199ebb4caSwyllys 
71299ebb4caSwyllys 
71399ebb4caSwyllys #define	COUNTRY_PROMPT	"Country Name (2 letter code) [US]:"
71499ebb4caSwyllys #define	STATE_PROMPT	"State or Province Name (full name) [Some-State]:"
71599ebb4caSwyllys #define	LOCALITY_PROMPT	"Locality Name (eg, city) []:"
71699ebb4caSwyllys #define	ORG_PROMPT	"Organization Name (eg, company) []:"
71799ebb4caSwyllys #define	UNIT_PROMPT	"Organizational Unit Name (eg, section) []:"
71899ebb4caSwyllys #define	NAME_PROMPT	"Common Name (eg, YOUR name) []:"
71999ebb4caSwyllys #define	EMAIL_PROMPT	"Email Address []:"
72099ebb4caSwyllys 
72199ebb4caSwyllys #define	COUNTRY_DEFAULT "US"
72299ebb4caSwyllys #define	STATE_DEFAULT	"Some-State"
72399ebb4caSwyllys #define	INVALID_INPUT 	"Invalid input; please re-enter ..."
72499ebb4caSwyllys 
72599ebb4caSwyllys #define	SUBNAMESIZ	1024
72699ebb4caSwyllys #define	RDN_MIN		1
72799ebb4caSwyllys #define	RDN_MAX		64
72899ebb4caSwyllys #define	COUNTRYNAME_MIN	2
72999ebb4caSwyllys #define	COUNTRYNAME_MAX	2
73099ebb4caSwyllys 
73199ebb4caSwyllys static char *
73299ebb4caSwyllys get_input_string(char *prompt, char *default_str, int min_len, int max_len)
73399ebb4caSwyllys {
73499ebb4caSwyllys 	char buf[1024];
73599ebb4caSwyllys 	char *response = NULL;
73699ebb4caSwyllys 	char *ret = NULL;
73799ebb4caSwyllys 	int len;
73899ebb4caSwyllys 
73999ebb4caSwyllys 	for (;;) {
74099ebb4caSwyllys 		(void) printf("\t%s", prompt);
74199ebb4caSwyllys 		(void) fflush(stdout);
74299ebb4caSwyllys 
74399ebb4caSwyllys 		response = fgets(buf, sizeof (buf), stdin);
74499ebb4caSwyllys 		if (response == NULL) {
74599ebb4caSwyllys 			if (default_str != NULL) {
74699ebb4caSwyllys 				ret = strdup(default_str);
74799ebb4caSwyllys 			}
74899ebb4caSwyllys 			break;
74999ebb4caSwyllys 		}
75099ebb4caSwyllys 
75199ebb4caSwyllys 		/* Skip any leading white space. */
75299ebb4caSwyllys 		while (isspace(*response))
75399ebb4caSwyllys 			response++;
75499ebb4caSwyllys 		if (*response == '\0') {
75599ebb4caSwyllys 			if (default_str != NULL) {
75699ebb4caSwyllys 				ret = strdup(default_str);
75799ebb4caSwyllys 			}
75899ebb4caSwyllys 			break;
75999ebb4caSwyllys 		}
76099ebb4caSwyllys 
76199ebb4caSwyllys 		len = strlen(response);
76299ebb4caSwyllys 		response[len-1] = '\0'; /* get rid of "LF" */
76399ebb4caSwyllys 		len--;
76499ebb4caSwyllys 		if (len >= min_len && len <= max_len) {
76599ebb4caSwyllys 			ret = strdup(response);
76699ebb4caSwyllys 			break;
76799ebb4caSwyllys 		}
76899ebb4caSwyllys 
76999ebb4caSwyllys 		(void) printf("%s\n", INVALID_INPUT);
77099ebb4caSwyllys 
77199ebb4caSwyllys 	}
77299ebb4caSwyllys 
77399ebb4caSwyllys 	return (ret);
77499ebb4caSwyllys }
77599ebb4caSwyllys 
77699ebb4caSwyllys int
77799ebb4caSwyllys get_subname(char **result)
77899ebb4caSwyllys {
77999ebb4caSwyllys 	char *country = NULL;
78099ebb4caSwyllys 	char *state = NULL;
78199ebb4caSwyllys 	char *locality = NULL;
78299ebb4caSwyllys 	char *org = NULL;
78399ebb4caSwyllys 	char *unit = NULL;
78499ebb4caSwyllys 	char *name = NULL;
78599ebb4caSwyllys 	char *email = NULL;
78699ebb4caSwyllys 	char *subname = NULL;
78799ebb4caSwyllys 
78899ebb4caSwyllys 	(void) printf("Entering following fields for subject (a DN) ...\n");
78999ebb4caSwyllys 	country = get_input_string(COUNTRY_PROMPT, COUNTRY_DEFAULT,
79099ebb4caSwyllys 	    COUNTRYNAME_MIN, COUNTRYNAME_MAX);
79199ebb4caSwyllys 	if (country == NULL)
79299ebb4caSwyllys 		return (-1);
79399ebb4caSwyllys 
79499ebb4caSwyllys 	state = get_input_string(STATE_PROMPT, STATE_DEFAULT,
79599ebb4caSwyllys 	    RDN_MIN, RDN_MAX);
79699ebb4caSwyllys 	if (state == NULL) {
79799ebb4caSwyllys 		goto out;
79899ebb4caSwyllys 	}
79999ebb4caSwyllys 
80099ebb4caSwyllys 	locality = get_input_string(LOCALITY_PROMPT, NULL, RDN_MIN, RDN_MAX);
80199ebb4caSwyllys 	org = get_input_string(ORG_PROMPT, NULL, RDN_MIN, RDN_MAX);
80299ebb4caSwyllys 	unit = get_input_string(UNIT_PROMPT, NULL, RDN_MIN, RDN_MAX);
80399ebb4caSwyllys 	name = get_input_string(NAME_PROMPT, NULL, RDN_MIN, RDN_MAX);
80499ebb4caSwyllys 	email = get_input_string(EMAIL_PROMPT, NULL, RDN_MIN, RDN_MAX);
80599ebb4caSwyllys 
80699ebb4caSwyllys 	/* Now create a subject name from the input strings */
80799ebb4caSwyllys 	if ((subname = malloc(SUBNAMESIZ)) == NULL)
80899ebb4caSwyllys 		goto out;
80999ebb4caSwyllys 
81099ebb4caSwyllys 	(void) memset(subname, 0, SUBNAMESIZ);
81199ebb4caSwyllys 	(void) strlcpy(subname, "C=", SUBNAMESIZ);
81299ebb4caSwyllys 	(void) strlcat(subname, country, SUBNAMESIZ);
81399ebb4caSwyllys 	(void) strlcat(subname, ", ", SUBNAMESIZ);
81499ebb4caSwyllys 	(void) strlcat(subname, "ST=", SUBNAMESIZ);
81599ebb4caSwyllys 	(void) strlcat(subname, state, SUBNAMESIZ);
81699ebb4caSwyllys 
81799ebb4caSwyllys 	if (locality) {
81899ebb4caSwyllys 		(void) strlcat(subname, ", ", SUBNAMESIZ);
81999ebb4caSwyllys 		(void) strlcat(subname, "L=", SUBNAMESIZ);
82099ebb4caSwyllys 		(void) strlcat(subname, locality, SUBNAMESIZ);
82199ebb4caSwyllys 	}
82299ebb4caSwyllys 
82399ebb4caSwyllys 	if (org) {
82499ebb4caSwyllys 		(void) strlcat(subname, ", ", SUBNAMESIZ);
82599ebb4caSwyllys 		(void) strlcat(subname, "O=", SUBNAMESIZ);
82699ebb4caSwyllys 		(void) strlcat(subname, org, SUBNAMESIZ);
82799ebb4caSwyllys 	}
82899ebb4caSwyllys 
82999ebb4caSwyllys 	if (unit) {
83099ebb4caSwyllys 		(void) strlcat(subname, ", ", SUBNAMESIZ);
83199ebb4caSwyllys 		(void) strlcat(subname, "OU=", SUBNAMESIZ);
83299ebb4caSwyllys 		(void) strlcat(subname, unit, SUBNAMESIZ);
83399ebb4caSwyllys 	}
83499ebb4caSwyllys 
83599ebb4caSwyllys 	if (name) {
83699ebb4caSwyllys 		(void) strlcat(subname, ", ", SUBNAMESIZ);
83799ebb4caSwyllys 		(void) strlcat(subname, "CN=", SUBNAMESIZ);
83899ebb4caSwyllys 		(void) strlcat(subname, name, SUBNAMESIZ);
83999ebb4caSwyllys 	}
84099ebb4caSwyllys 
84199ebb4caSwyllys 	if (email) {
84299ebb4caSwyllys 		(void) strlcat(subname, ", ", SUBNAMESIZ);
84399ebb4caSwyllys 		(void) strlcat(subname, "E=", SUBNAMESIZ);
84499ebb4caSwyllys 		(void) strlcat(subname, email, SUBNAMESIZ);
84599ebb4caSwyllys 	}
84699ebb4caSwyllys 
84799ebb4caSwyllys out:
84899ebb4caSwyllys 	if (country)
84999ebb4caSwyllys 		free(country);
85099ebb4caSwyllys 	if (state)
85199ebb4caSwyllys 		free(state);
85299ebb4caSwyllys 	if (locality)
85399ebb4caSwyllys 		free(locality);
85499ebb4caSwyllys 	if (org)
85599ebb4caSwyllys 		free(org);
85699ebb4caSwyllys 	if (unit)
85799ebb4caSwyllys 		free(unit);
85899ebb4caSwyllys 	if (name)
85999ebb4caSwyllys 		free(name);
86099ebb4caSwyllys 	if (email)
86199ebb4caSwyllys 		free(email);
86299ebb4caSwyllys 
86399ebb4caSwyllys 	if (subname == NULL)
86499ebb4caSwyllys 		return (-1);
86599ebb4caSwyllys 	else {
86699ebb4caSwyllys 		*result = subname;
86799ebb4caSwyllys 		return (0);
86899ebb4caSwyllys 	}
86999ebb4caSwyllys }
87099ebb4caSwyllys 
87199ebb4caSwyllys /*
87299ebb4caSwyllys  * Parse a string of KeyUsage values and convert
87399ebb4caSwyllys  * them to the correct KU Bits.
87499ebb4caSwyllys  * The field may be marked "critical" by prepending
87599ebb4caSwyllys  * "critical:" to the list.
87699ebb4caSwyllys  * EX:  critical:digitialSignature,keyEncipherment
87799ebb4caSwyllys  */
87899ebb4caSwyllys KMF_RETURN
87999ebb4caSwyllys verify_keyusage(char *kustr, uint16_t *kubits, int *critical)
88099ebb4caSwyllys {
88199ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
88299ebb4caSwyllys 	uint16_t kuval;
88399ebb4caSwyllys 	char *k;
88499ebb4caSwyllys 
88599ebb4caSwyllys 	*kubits = 0;
88699ebb4caSwyllys 	if (kustr == NULL || !strlen(kustr))
88799ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
88899ebb4caSwyllys 
88999ebb4caSwyllys 	/* Check to see if this is critical */
89099ebb4caSwyllys 	if (!strncasecmp(kustr, "critical:", strlen("critical:"))) {
89199ebb4caSwyllys 		*critical = TRUE;
89299ebb4caSwyllys 		kustr += strlen("critical:");
89399ebb4caSwyllys 	} else {
89499ebb4caSwyllys 		*critical = FALSE;
89599ebb4caSwyllys 	}
89699ebb4caSwyllys 
89799ebb4caSwyllys 	k = strtok(kustr, ",");
89899ebb4caSwyllys 	while (k != NULL) {
899*30a5e8faSwyllys 		kuval = kmf_string_to_ku(k);
90099ebb4caSwyllys 		if (kuval == 0) {
90199ebb4caSwyllys 			*kubits = 0;
90299ebb4caSwyllys 			return (KMF_ERR_BAD_PARAMETER);
90399ebb4caSwyllys 		}
90499ebb4caSwyllys 		*kubits |= kuval;
90599ebb4caSwyllys 		k = strtok(NULL, ",");
90699ebb4caSwyllys 	}
90799ebb4caSwyllys 
90899ebb4caSwyllys 	return (ret);
90999ebb4caSwyllys }
91099ebb4caSwyllys 
91199ebb4caSwyllys /*
91299ebb4caSwyllys  * Verify the alternate subject label is real or invalid.
91399ebb4caSwyllys  *
91499ebb4caSwyllys  * The field may be marked "critical" by prepending
91599ebb4caSwyllys  * "critical:" to the list.
91699ebb4caSwyllys  * EX:  "critical:IP=1.2.3.4"
91799ebb4caSwyllys  */
91899ebb4caSwyllys KMF_RETURN
91999ebb4caSwyllys verify_altname(char *arg, KMF_GENERALNAMECHOICES *type, int *critical)
92099ebb4caSwyllys {
92199ebb4caSwyllys 	char *p;
92299ebb4caSwyllys 	KMF_RETURN rv = KMF_OK;
92399ebb4caSwyllys 
92499ebb4caSwyllys 	/* Check to see if this is critical */
92599ebb4caSwyllys 	if (!strncasecmp(arg, "critical:", strlen("critical:"))) {
92699ebb4caSwyllys 		*critical = TRUE;
92799ebb4caSwyllys 		arg += strlen("critical:");
92899ebb4caSwyllys 	} else {
92999ebb4caSwyllys 		*critical = FALSE;
93099ebb4caSwyllys 	}
93199ebb4caSwyllys 
93299ebb4caSwyllys 	/* Make sure there is an "=" sign */
93399ebb4caSwyllys 	p = strchr(arg, '=');
93499ebb4caSwyllys 	if (p == NULL)
93599ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
93699ebb4caSwyllys 
93799ebb4caSwyllys 	p[0] = '\0';
93899ebb4caSwyllys 
93999ebb4caSwyllys 	if (strcmp(arg, "IP") == 0)
94099ebb4caSwyllys 		*type = GENNAME_IPADDRESS;
94199ebb4caSwyllys 	else if (strcmp(arg, "DNS") == 0)
94299ebb4caSwyllys 		*type = GENNAME_DNSNAME;
94399ebb4caSwyllys 	else if (strcmp(arg, "EMAIL") == 0)
94499ebb4caSwyllys 		*type = GENNAME_RFC822NAME;
94599ebb4caSwyllys 	else if (strcmp(arg, "URI") == 0)
94699ebb4caSwyllys 		*type = GENNAME_URI;
94799ebb4caSwyllys 	else if (strcmp(arg, "DN") == 0)
94899ebb4caSwyllys 		*type = GENNAME_DIRECTORYNAME;
94999ebb4caSwyllys 	else if (strcmp(arg, "RID") == 0)
95099ebb4caSwyllys 		*type = GENNAME_REGISTEREDID;
95199ebb4caSwyllys 	else
95299ebb4caSwyllys 		rv = KMF_ERR_BAD_PARAMETER;
95399ebb4caSwyllys 
95499ebb4caSwyllys 	p[0] = '=';
95599ebb4caSwyllys 
95699ebb4caSwyllys 	return (rv);
95799ebb4caSwyllys }
95899ebb4caSwyllys 
95999ebb4caSwyllys int
96099ebb4caSwyllys get_token_password(KMF_KEYSTORE_TYPE kstype,
96199ebb4caSwyllys 	char *token_spec, KMF_CREDENTIAL *cred)
96299ebb4caSwyllys {
96399ebb4caSwyllys 	char	prompt[1024];
96499ebb4caSwyllys 	char	*p = NULL;
96599ebb4caSwyllys 
96699ebb4caSwyllys 	if (kstype == KMF_KEYSTORE_PK11TOKEN) {
96799ebb4caSwyllys 		p = strchr(token_spec, ':');
96899ebb4caSwyllys 		if (p != NULL)
96999ebb4caSwyllys 		*p = 0;
97099ebb4caSwyllys 	}
97199ebb4caSwyllys 	/*
97299ebb4caSwyllys 	 * Login to the token first.
97399ebb4caSwyllys 	 */
97499ebb4caSwyllys 	(void) snprintf(prompt, sizeof (prompt),
975*30a5e8faSwyllys 	    gettext(DEFAULT_TOKEN_PROMPT), token_spec);
97699ebb4caSwyllys 
97799ebb4caSwyllys 	if (get_pin(prompt, NULL, (uchar_t **)&cred->cred,
97899ebb4caSwyllys 	    (ulong_t *)&cred->credlen) != CKR_OK) {
97999ebb4caSwyllys 		cred->cred = NULL;
98099ebb4caSwyllys 		cred->credlen = 0;
98199ebb4caSwyllys 	}
98299ebb4caSwyllys 
98399ebb4caSwyllys 	if (kstype == KMF_KEYSTORE_PK11TOKEN && p != NULL)
98499ebb4caSwyllys 		*p = ':';
98599ebb4caSwyllys 	return (KMF_OK);
98699ebb4caSwyllys }
98799ebb4caSwyllys 
98899ebb4caSwyllys KMF_RETURN
98999ebb4caSwyllys verify_file(char *filename)
99099ebb4caSwyllys {
99199ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
99299ebb4caSwyllys 	int fd;
99399ebb4caSwyllys 
99499ebb4caSwyllys 	/*
99599ebb4caSwyllys 	 * Attempt to open with  the EXCL flag so that if
99699ebb4caSwyllys 	 * it already exists, the open will fail.  It will
99799ebb4caSwyllys 	 * also fail if the file cannot be created due to
99899ebb4caSwyllys 	 * permissions on the parent directory, or if the
99999ebb4caSwyllys 	 * parent directory itself does not exist.
100099ebb4caSwyllys 	 */
100199ebb4caSwyllys 	fd = open(filename, O_CREAT | O_EXCL, 0600);
100299ebb4caSwyllys 	if (fd == -1)
100399ebb4caSwyllys 		return (KMF_ERR_OPEN_FILE);
100499ebb4caSwyllys 
100599ebb4caSwyllys 	/* If we were able to create it, delete it. */
100699ebb4caSwyllys 	(void) close(fd);
100799ebb4caSwyllys 	(void) unlink(filename);
100899ebb4caSwyllys 
100999ebb4caSwyllys 	return (ret);
101099ebb4caSwyllys }
101199ebb4caSwyllys 
101299ebb4caSwyllys void
101399ebb4caSwyllys display_error(void *handle, KMF_RETURN errcode, char *prefix)
101499ebb4caSwyllys {
101599ebb4caSwyllys 	KMF_RETURN rv1, rv2;
101699ebb4caSwyllys 	char *plugin_errmsg = NULL;
101799ebb4caSwyllys 	char *kmf_errmsg = NULL;
101899ebb4caSwyllys 
1019*30a5e8faSwyllys 	rv1 = kmf_get_plugin_error_str(handle, &plugin_errmsg);
1020*30a5e8faSwyllys 	rv2 = kmf_get_kmf_error_str(errcode, &kmf_errmsg);
102199ebb4caSwyllys 
102299ebb4caSwyllys 	cryptoerror(LOG_STDERR, "%s:", prefix);
102399ebb4caSwyllys 	if (rv1 == KMF_OK && plugin_errmsg) {
1024*30a5e8faSwyllys 		cryptoerror(LOG_STDERR, gettext("keystore error: %s"),
102599ebb4caSwyllys 		    plugin_errmsg);
1026*30a5e8faSwyllys 		kmf_free_str(plugin_errmsg);
102799ebb4caSwyllys 	}
102899ebb4caSwyllys 
102999ebb4caSwyllys 	if (rv2 == KMF_OK && kmf_errmsg) {
1030*30a5e8faSwyllys 		cryptoerror(LOG_STDERR, gettext("libkmf error: %s"),
103199ebb4caSwyllys 		    kmf_errmsg);
1032*30a5e8faSwyllys 		kmf_free_str(kmf_errmsg);
103399ebb4caSwyllys 	}
103499ebb4caSwyllys 
103599ebb4caSwyllys 	if (rv1 != KMF_OK && rv2 != KMF_OK)
103699ebb4caSwyllys 		cryptoerror(LOG_STDERR, gettext("<unknown error>\n"));
103799ebb4caSwyllys 
103899ebb4caSwyllys }
1039