xref: /titanic_50/usr/src/cmd/cmd-crypto/pktool/common.c (revision fa60c371cd00bdca17de2ff18fe3e64d051ae61b)
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 /*
22d00756ccSwyllys  * Copyright 2008 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 
1707711facfSdinak 	/* Prompt user for a PIN. */
1717711facfSdinak 	if (prompt1 == NULL) {
1727711facfSdinak 		return (CKR_ARGUMENTS_BAD);
1737711facfSdinak 	}
1747711facfSdinak 	if ((phrase1 = getpassphrase(prompt1)) == NULL) {
1757711facfSdinak 		return (CKR_FUNCTION_FAILED);
1767711facfSdinak 	}
1777711facfSdinak 
1787711facfSdinak 	/* Duplicate 1st PIN in separate chunk of memory. */
1797711facfSdinak 	if ((save_phrase = strdup(phrase1)) == NULL)
1807711facfSdinak 		return (CKR_HOST_MEMORY);
1817711facfSdinak 
1827711facfSdinak 	/* If second prompt given, PIN confirmation is requested. */
1837711facfSdinak 	if (prompt2 != NULL) {
1847711facfSdinak 		if ((phrase2 = getpassphrase(prompt2)) == NULL) {
1857711facfSdinak 			free(save_phrase);
1867711facfSdinak 			return (CKR_FUNCTION_FAILED);
1877711facfSdinak 		}
1887711facfSdinak 		if (strcmp(save_phrase, phrase2) != 0) {
1897711facfSdinak 			free(save_phrase);
1907711facfSdinak 			return (CKR_PIN_INCORRECT);
1917711facfSdinak 		}
1927711facfSdinak 	}
1937711facfSdinak 
1947711facfSdinak 	*pin = (CK_UTF8CHAR_PTR)save_phrase;
1957711facfSdinak 	*pinlen = strlen(save_phrase);
1967711facfSdinak 	return (CKR_OK);
1977711facfSdinak }
1987711facfSdinak 
199d00756ccSwyllys int
200d00756ccSwyllys yn_to_int(char *ynstr)
201d00756ccSwyllys {
202d00756ccSwyllys 	char *y = gettext("yes");
203d00756ccSwyllys 	char *n = gettext("no");
204d00756ccSwyllys 	if (ynstr == NULL)
205d00756ccSwyllys 		return (-1);
206d00756ccSwyllys 
207d00756ccSwyllys 	if (strncasecmp(ynstr, y, 1) == 0)
208d00756ccSwyllys 		return (1);
209d00756ccSwyllys 	else if (strncasecmp(ynstr, n, 1) == 0)
210d00756ccSwyllys 		return (0);
211d00756ccSwyllys 	else
212d00756ccSwyllys 		return (-1);
213d00756ccSwyllys }
214d00756ccSwyllys 
2157711facfSdinak /*
2167711facfSdinak  * Gets yes/no response from user.  If either no prompt is supplied, a
2177711facfSdinak  * default prompt is used.  If not message for invalid input is supplied,
2187711facfSdinak  * a default will not be provided.  If the user provides no response,
2197711facfSdinak  * the input default B_TRUE == yes, B_FALSE == no is returned.
2207711facfSdinak  * Otherwise, B_TRUE is returned for yes, and B_FALSE for no.
2217711facfSdinak  */
2227711facfSdinak boolean_t
2237711facfSdinak yesno(char *prompt, char *invalid, boolean_t dflt)
2247711facfSdinak {
2257711facfSdinak 	char	*response, buf[1024];
226d00756ccSwyllys 	int	ans;
2277711facfSdinak 
2287711facfSdinak 	if (prompt == NULL)
2297711facfSdinak 		prompt = gettext("Enter (y)es or (n)o? ");
2307711facfSdinak 
2317711facfSdinak 	for (;;) {
2327711facfSdinak 		/* Prompt user. */
2337711facfSdinak 		(void) printf("%s", prompt);
2347711facfSdinak 		(void) fflush(stdout);
2357711facfSdinak 
2367711facfSdinak 		/* Get the response. */
2377711facfSdinak 		if ((response = fgets(buf, sizeof (buf), stdin)) == NULL)
2387711facfSdinak 			break;		/* go to default response */
2397711facfSdinak 
2407711facfSdinak 		/* Skip any leading white space. */
2417711facfSdinak 		while (isspace(*response))
2427711facfSdinak 			response++;
2437711facfSdinak 		if (*response == '\0')
2447711facfSdinak 			break;		/* go to default response */
2457711facfSdinak 
246d00756ccSwyllys 		ans = yn_to_int(response);
247d00756ccSwyllys 		if (ans == 1)
2487711facfSdinak 			return (B_TRUE);
249d00756ccSwyllys 		else if (ans == 0)
2507711facfSdinak 			return (B_FALSE);
2517711facfSdinak 
2527711facfSdinak 		/* Indicate invalid input, and try again. */
2537711facfSdinak 		if (invalid != NULL)
2547711facfSdinak 			(void) printf("%s", invalid);
2557711facfSdinak 	}
2567711facfSdinak 	return (dflt);
2577711facfSdinak }
2587711facfSdinak 
2597711facfSdinak /*
2607711facfSdinak  * Gets the list of slots which have tokens in them.  Keeps adjusting
2617711facfSdinak  * the size of the slot list buffer until the call is successful or an
2627711facfSdinak  * irrecoverable error occurs.
2637711facfSdinak  */
2647711facfSdinak CK_RV
2657711facfSdinak get_token_slots(CK_SLOT_ID_PTR *slot_list, CK_ULONG *slot_count)
2667711facfSdinak {
2677711facfSdinak 	CK_ULONG	tmp_count = 0;
2687711facfSdinak 	CK_SLOT_ID_PTR	tmp_list = NULL_PTR, tmp2_list = NULL_PTR;
2697711facfSdinak 	int		rv = CKR_OK;
2707711facfSdinak 
2717711facfSdinak 	if (!initialized)
2727711facfSdinak 		if ((rv = init_pk11()) != CKR_OK)
2737711facfSdinak 			return (rv);
2747711facfSdinak 
2757711facfSdinak 	/*
2767711facfSdinak 	 * Get the slot count first because we don't know how many
2777711facfSdinak 	 * slots there are and how many of those slots even have tokens.
2787711facfSdinak 	 * Don't specify an arbitrary buffer size for the slot list;
2797711facfSdinak 	 * it may be too small (see section 11.5 of PKCS#11 spec).
2807711facfSdinak 	 * Also select only those slots that have tokens in them,
2817711facfSdinak 	 * because this tool has no need to know about empty slots.
2827711facfSdinak 	 */
2837711facfSdinak 	if ((rv = C_GetSlotList(1, NULL_PTR, &tmp_count)) != CKR_OK)
2847711facfSdinak 		return (rv);
2857711facfSdinak 
2867711facfSdinak 	if (tmp_count == 0) {
2877711facfSdinak 		*slot_list = NULL_PTR;
2887711facfSdinak 		*slot_count = 0;
2897711facfSdinak 		return (CKR_OK);
2907711facfSdinak 	}
2917711facfSdinak 
2927711facfSdinak 	/* Allocate initial space for the slot list. */
2937711facfSdinak 	if ((tmp_list = (CK_SLOT_ID_PTR) malloc(tmp_count *
2947711facfSdinak 	    sizeof (CK_SLOT_ID))) == NULL)
2957711facfSdinak 		return (CKR_HOST_MEMORY);
2967711facfSdinak 
2977711facfSdinak 	/* Then get the slot list itself. */
2987711facfSdinak 	for (;;) {
2997711facfSdinak 		if ((rv = C_GetSlotList(1, tmp_list, &tmp_count)) == CKR_OK) {
3007711facfSdinak 			*slot_list = tmp_list;
3017711facfSdinak 			*slot_count = tmp_count;
3027711facfSdinak 			break;
3037711facfSdinak 		}
3047711facfSdinak 
3057711facfSdinak 		if (rv != CKR_BUFFER_TOO_SMALL) {
3067711facfSdinak 			free(tmp_list);
3077711facfSdinak 			break;
3087711facfSdinak 		}
3097711facfSdinak 
3107711facfSdinak 		/* If the number of slots grew, try again. */
3117711facfSdinak 		if ((tmp2_list = (CK_SLOT_ID_PTR) realloc(tmp_list,
3127711facfSdinak 		    tmp_count * sizeof (CK_SLOT_ID))) == NULL) {
3137711facfSdinak 			free(tmp_list);
3147711facfSdinak 			rv = CKR_HOST_MEMORY;
3157711facfSdinak 			break;
3167711facfSdinak 		}
3177711facfSdinak 		tmp_list = tmp2_list;
3187711facfSdinak 	}
3197711facfSdinak 
3207711facfSdinak 	return (rv);
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate /*
32449e21299Sdinak  * Breaks out the getopt-style option string into a structure that can be
32549e21299Sdinak  * traversed later for calls to getopt_av().  Option string is NOT altered,
32649e21299Sdinak  * but the struct fields point to locations within option string.
32749e21299Sdinak  */
32849e21299Sdinak static int
32949e21299Sdinak populate_opts(char *optstring)
33049e21299Sdinak {
33149e21299Sdinak 	int		i;
33249e21299Sdinak 	av_opts		*temp;
33349e21299Sdinak 	char		*marker;
33449e21299Sdinak 
33549e21299Sdinak 	if (optstring == NULL || *optstring == '\0')
33649e21299Sdinak 		return (0);
33749e21299Sdinak 
33849e21299Sdinak 	/*
33949e21299Sdinak 	 * This tries to imitate getopt(3c) Each option must conform to:
34049e21299Sdinak 	 * <short name char> [ ':' ] [ '(' <long name string> ')' ]
34149e21299Sdinak 	 * If long name is missing, the short name is used for long name.
34249e21299Sdinak 	 */
34349e21299Sdinak 	for (i = 0; *optstring != '\0'; i++) {
34449e21299Sdinak 		if ((temp = (av_opts *)((i == 0) ? malloc(sizeof (av_opts)) :
34549e21299Sdinak 		    realloc(opts_av, (i+1) * sizeof (av_opts)))) == NULL) {
34699ebb4caSwyllys 			if (opts_av != NULL)
34749e21299Sdinak 				free(opts_av);
34849e21299Sdinak 			opts_av = NULL;
34949e21299Sdinak 			return (0);
35099ebb4caSwyllys 		} else {
35149e21299Sdinak 			opts_av = (av_opts *)temp;
35299ebb4caSwyllys 		}
35349e21299Sdinak 
35499ebb4caSwyllys 		(void) memset(&opts_av[i], 0, sizeof (av_opts));
35549e21299Sdinak 		marker = optstring;		/* may need optstring later */
35649e21299Sdinak 
35749e21299Sdinak 		opts_av[i].shortnm = *marker++;	/* set short name */
35849e21299Sdinak 
35949e21299Sdinak 		if (*marker == ':') {		/* check for opt arg */
36049e21299Sdinak 			marker++;
36149e21299Sdinak 			opts_av[i].has_arg = B_TRUE;
36249e21299Sdinak 		}
36349e21299Sdinak 
36449e21299Sdinak 		if (*marker == '(') {		/* check and set long name */
36549e21299Sdinak 			marker++;
36649e21299Sdinak 			opts_av[i].longnm = marker;
36749e21299Sdinak 			opts_av[i].longnm_len = strcspn(marker, ")");
36849e21299Sdinak 			optstring = marker + opts_av[i].longnm_len + 1;
36949e21299Sdinak 		} else {
37049e21299Sdinak 			/* use short name option character */
37149e21299Sdinak 			opts_av[i].longnm = optstring;
37249e21299Sdinak 			opts_av[i].longnm_len = 1;
37349e21299Sdinak 			optstring = marker;
37449e21299Sdinak 		}
37549e21299Sdinak 	}
37649e21299Sdinak 
37749e21299Sdinak 	return (i);
37849e21299Sdinak }
37949e21299Sdinak 
38049e21299Sdinak /*
38149e21299Sdinak  * getopt_av() is very similar to getopt(3c) in that the takes an option
38249e21299Sdinak  * string, compares command line arguments for matches, and returns a single
38349e21299Sdinak  * letter option when a match is found.  However, getopt_av() differs from
38449e21299Sdinak  * getopt(3c) by requiring that only longname options and values be found
38549e21299Sdinak  * on the command line and all leading dashes are omitted.  In other words,
38649e21299Sdinak  * it tries to enforce only longname "option=value" arguments on the command
38749e21299Sdinak  * line.  Boolean options are not allowed either.
38849e21299Sdinak  */
38949e21299Sdinak int
39049e21299Sdinak getopt_av(int argc, char * const *argv, const char *optstring)
39149e21299Sdinak {
39249e21299Sdinak 	int	i;
39349e21299Sdinak 	int	len;
39499ebb4caSwyllys 	char   *cur_option;
39549e21299Sdinak 
39649e21299Sdinak 	if (optind_av >= argc)
39749e21299Sdinak 		return (EOF);
39849e21299Sdinak 
39949e21299Sdinak 	/* First time or when optstring changes from previous one */
40049e21299Sdinak 	if (_save_optstr != optstring) {
40149e21299Sdinak 		if (opts_av != NULL)
40249e21299Sdinak 			free(opts_av);
40349e21299Sdinak 		opts_av = NULL;
40449e21299Sdinak 		_save_optstr = optstring;
40549e21299Sdinak 		_save_numopts = populate_opts((char *)optstring);
40649e21299Sdinak 	}
40749e21299Sdinak 
40849e21299Sdinak 	for (i = 0; i < _save_numopts; i++) {
40999ebb4caSwyllys 		cur_option = argv[optind_av];
41099ebb4caSwyllys 
41199ebb4caSwyllys 		if (strcmp(cur_option, "--") == 0) {
41249e21299Sdinak 			optind_av++;
41349e21299Sdinak 			break;
41449e21299Sdinak 		}
41549e21299Sdinak 
41699ebb4caSwyllys 		if (cur_option[0] == '-' && strlen(cur_option) == 2) {
41799ebb4caSwyllys 			len = 1;
41899ebb4caSwyllys 			cur_option++; /* remove "-" */
41999ebb4caSwyllys 		} else {
42099ebb4caSwyllys 			len = strcspn(cur_option, "=");
42199ebb4caSwyllys 		}
42249e21299Sdinak 
42399ebb4caSwyllys 		if (len == opts_av[i].longnm_len && strncmp(cur_option,
42449e21299Sdinak 		    opts_av[i].longnm, opts_av[i].longnm_len) == 0) {
42549e21299Sdinak 			/* matched */
42649e21299Sdinak 			if (!opts_av[i].has_arg) {
42749e21299Sdinak 				optind_av++;
42849e21299Sdinak 				return (opts_av[i].shortnm);
42949e21299Sdinak 			}
43049e21299Sdinak 
43149e21299Sdinak 			/* needs optarg */
43299ebb4caSwyllys 			if (cur_option[len] == '=') {
43399ebb4caSwyllys 				optarg_av = &(cur_option[len+1]);
43449e21299Sdinak 				optind_av++;
43549e21299Sdinak 				return (opts_av[i].shortnm);
43649e21299Sdinak 			}
43749e21299Sdinak 
43849e21299Sdinak 			optarg_av = NULL;
43949e21299Sdinak 			optind_av++;
44049e21299Sdinak 			return ((int)'?');
44149e21299Sdinak 		}
44249e21299Sdinak 	}
44349e21299Sdinak 
44449e21299Sdinak 	return (EOF);
44549e21299Sdinak }
44699ebb4caSwyllys 
44799ebb4caSwyllys KMF_KEYSTORE_TYPE
44899ebb4caSwyllys KS2Int(char *keystore_str)
44999ebb4caSwyllys {
45099ebb4caSwyllys 	if (keystore_str == NULL)
45199ebb4caSwyllys 		return (0);
452d00756ccSwyllys 	if (strcasecmp(keystore_str, "pkcs11") == 0)
45399ebb4caSwyllys 		return (KMF_KEYSTORE_PK11TOKEN);
454d00756ccSwyllys 	else if (strcasecmp(keystore_str, "nss") == 0)
45599ebb4caSwyllys 		return (KMF_KEYSTORE_NSS);
456d00756ccSwyllys 	else if (strcasecmp(keystore_str, "file") == 0)
45799ebb4caSwyllys 		return (KMF_KEYSTORE_OPENSSL);
45899ebb4caSwyllys 	else
45999ebb4caSwyllys 		return (0);
46099ebb4caSwyllys }
46199ebb4caSwyllys 
46299ebb4caSwyllys 
46399ebb4caSwyllys int
46499ebb4caSwyllys Str2KeyType(char *algm, KMF_KEY_ALG *ktype, KMF_ALGORITHM_INDEX *sigAlg)
46599ebb4caSwyllys {
46699ebb4caSwyllys 	if (algm == NULL) {
46799ebb4caSwyllys 		*sigAlg = KMF_ALGID_MD5WithRSA;
46899ebb4caSwyllys 		*ktype = KMF_RSA;
46999ebb4caSwyllys 	} else if (strcasecmp(algm, "DSA") == 0) {
47099ebb4caSwyllys 		*sigAlg = KMF_ALGID_SHA1WithDSA;
47199ebb4caSwyllys 		*ktype = KMF_DSA;
47299ebb4caSwyllys 	} else if (strcasecmp(algm, "RSA") == 0) {
47399ebb4caSwyllys 		*sigAlg = KMF_ALGID_MD5WithRSA;
47499ebb4caSwyllys 		*ktype = KMF_RSA;
47599ebb4caSwyllys 	} else {
47699ebb4caSwyllys 		return (-1);
47799ebb4caSwyllys 	}
47899ebb4caSwyllys 	return (0);
47999ebb4caSwyllys }
48099ebb4caSwyllys 
48199ebb4caSwyllys int
48299ebb4caSwyllys Str2SymKeyType(char *algm, KMF_KEY_ALG *ktype)
48399ebb4caSwyllys {
48499ebb4caSwyllys 	if (algm == NULL)
48599ebb4caSwyllys 		*ktype = KMF_AES;
48699ebb4caSwyllys 	else if (strcasecmp(algm, "aes") == 0)
48799ebb4caSwyllys 		*ktype = KMF_AES;
48899ebb4caSwyllys 	else if (strcasecmp(algm, "arcfour") == 0)
48999ebb4caSwyllys 		*ktype = KMF_RC4;
49099ebb4caSwyllys 	else if (strcasecmp(algm, "des") == 0)
49199ebb4caSwyllys 		*ktype = KMF_DES;
49299ebb4caSwyllys 	else if (strcasecmp(algm, "3des") == 0)
49399ebb4caSwyllys 		*ktype = KMF_DES3;
494c197cb9dShylee 	else if (strcasecmp(algm, "generic") == 0)
495c197cb9dShylee 		*ktype = KMF_GENERIC_SECRET;
49699ebb4caSwyllys 	else
49799ebb4caSwyllys 		return (-1);
49899ebb4caSwyllys 
49999ebb4caSwyllys 	return (0);
50099ebb4caSwyllys }
50199ebb4caSwyllys 
50299ebb4caSwyllys int
50399ebb4caSwyllys Str2Lifetime(char *ltimestr, uint32_t *ltime)
50499ebb4caSwyllys {
50599ebb4caSwyllys 	int num;
50699ebb4caSwyllys 	char timetok[6];
50799ebb4caSwyllys 
508d00756ccSwyllys 	if (ltimestr == NULL || strlen(ltimestr) == 0) {
50999ebb4caSwyllys 		/* default to 1 year lifetime */
51099ebb4caSwyllys 		*ltime = SECSPERDAY * DAYSPERNYEAR;
51199ebb4caSwyllys 		return (0);
51299ebb4caSwyllys 	}
51399ebb4caSwyllys 
51499ebb4caSwyllys 	(void) memset(timetok, 0, sizeof (timetok));
51599ebb4caSwyllys 	if (sscanf(ltimestr, "%d-%06s", &num, timetok) != 2)
51699ebb4caSwyllys 		return (-1);
51799ebb4caSwyllys 
518d00756ccSwyllys 	if (strcasecmp(timetok, "day") == 0||
519d00756ccSwyllys 	    strcasecmp(timetok, "days") == 0) {
52099ebb4caSwyllys 		*ltime = num * SECSPERDAY;
521d00756ccSwyllys 	} else if (strcasecmp(timetok, "hour") == 0||
522d00756ccSwyllys 	    strcasecmp(timetok, "hours") == 0) {
52399ebb4caSwyllys 		*ltime = num * SECSPERHOUR;
524d00756ccSwyllys 	} else if (strcasecmp(timetok, "year") == 0 ||
525d00756ccSwyllys 	    strcasecmp(timetok, "years") == 0) {
52699ebb4caSwyllys 		*ltime = num * SECSPERDAY * DAYSPERNYEAR;
52799ebb4caSwyllys 	} else {
52899ebb4caSwyllys 		*ltime = 0;
52999ebb4caSwyllys 		return (-1);
53099ebb4caSwyllys 	}
53199ebb4caSwyllys 
53299ebb4caSwyllys 	return (0);
53399ebb4caSwyllys }
53499ebb4caSwyllys 
53599ebb4caSwyllys int
53699ebb4caSwyllys OT2Int(char *objclass)
53799ebb4caSwyllys {
53899ebb4caSwyllys 	char *c = NULL;
53999ebb4caSwyllys 	int retval = 0;
54099ebb4caSwyllys 
54199ebb4caSwyllys 	if (objclass == NULL)
54299ebb4caSwyllys 		return (-1);
54399ebb4caSwyllys 
54499ebb4caSwyllys 	c = strchr(objclass, ':');
54599ebb4caSwyllys 	if (c != NULL) {
546d00756ccSwyllys 		if (strcasecmp(c, ":private") == 0)
54799ebb4caSwyllys 			retval = PK_PRIVATE_OBJ;
548d00756ccSwyllys 		else if (strcasecmp(c, ":public") == 0)
54999ebb4caSwyllys 			retval = PK_PUBLIC_OBJ;
550d00756ccSwyllys 		else if (strcasecmp(c, ":both") == 0)
55199ebb4caSwyllys 			retval = PK_PRIVATE_OBJ | PK_PUBLIC_OBJ;
55299ebb4caSwyllys 		else /* unrecognized option */
55399ebb4caSwyllys 			return (-1);
55499ebb4caSwyllys 
55599ebb4caSwyllys 		*c = '\0';
55699ebb4caSwyllys 	}
55799ebb4caSwyllys 
558d00756ccSwyllys 	if (strcasecmp(objclass, "public") == 0) {
55999ebb4caSwyllys 		if (retval)
56099ebb4caSwyllys 			return (-1);
56130a5e8faSwyllys 		return (retval | PK_PUBLIC_OBJ | PK_CERT_OBJ | PK_PUBKEY_OBJ);
562d00756ccSwyllys 	} else if (strcasecmp(objclass, "private") == 0) {
56399ebb4caSwyllys 		if (retval)
56499ebb4caSwyllys 			return (-1);
56599ebb4caSwyllys 		return (retval | PK_PRIKEY_OBJ | PK_PRIVATE_OBJ);
566d00756ccSwyllys 	} else if (strcasecmp(objclass, "both") == 0) {
56799ebb4caSwyllys 		if (retval)
56899ebb4caSwyllys 			return (-1);
56999ebb4caSwyllys 		return (PK_KEY_OBJ | PK_PUBLIC_OBJ | PK_PRIVATE_OBJ);
570d00756ccSwyllys 	} else if (strcasecmp(objclass, "cert") == 0) {
57199ebb4caSwyllys 		return (retval | PK_CERT_OBJ);
572d00756ccSwyllys 	} else if (strcasecmp(objclass, "key") == 0) {
57399ebb4caSwyllys 		if (retval == 0) /* return all keys */
57499ebb4caSwyllys 			return (retval | PK_KEY_OBJ);
57599ebb4caSwyllys 		else if (retval == (PK_PRIVATE_OBJ | PK_PUBLIC_OBJ))
57699ebb4caSwyllys 			/* return all keys */
57799ebb4caSwyllys 			return (retval | PK_KEY_OBJ);
57899ebb4caSwyllys 		else if (retval & PK_PUBLIC_OBJ)
57999ebb4caSwyllys 			/* Only return public keys */
58099ebb4caSwyllys 			return (retval | PK_PUBKEY_OBJ);
58199ebb4caSwyllys 		else if (retval & PK_PRIVATE_OBJ)
58299ebb4caSwyllys 			/* Only return private keys */
58399ebb4caSwyllys 			return (retval | PK_PRIKEY_OBJ);
584d00756ccSwyllys 	} else if (strcasecmp(objclass, "crl") == 0) {
58599ebb4caSwyllys 		if (retval)
58699ebb4caSwyllys 			return (-1);
58799ebb4caSwyllys 		return (retval | PK_CRL_OBJ);
58899ebb4caSwyllys 	}
58999ebb4caSwyllys 
59099ebb4caSwyllys 	if (retval == 0) /* No matches found */
59199ebb4caSwyllys 		retval = -1;
59299ebb4caSwyllys 	return (retval);
59399ebb4caSwyllys }
59499ebb4caSwyllys 
59599ebb4caSwyllys KMF_ENCODE_FORMAT
59699ebb4caSwyllys Str2Format(char *formstr)
59799ebb4caSwyllys {
598d00756ccSwyllys 	if (formstr == NULL || strcasecmp(formstr, "der") == 0)
59999ebb4caSwyllys 		return (KMF_FORMAT_ASN1);
600d00756ccSwyllys 	if (strcasecmp(formstr, "pem") == 0)
60199ebb4caSwyllys 		return (KMF_FORMAT_PEM);
602d00756ccSwyllys 	if (strcasecmp(formstr, "pkcs12") == 0)
60399ebb4caSwyllys 		return (KMF_FORMAT_PKCS12);
604d00756ccSwyllys 	if (strcasecmp(formstr, "raw") == 0)
60530a5e8faSwyllys 		return (KMF_FORMAT_RAWKEY);
60699ebb4caSwyllys 
60799ebb4caSwyllys 	return (KMF_FORMAT_UNDEF);
60899ebb4caSwyllys }
60999ebb4caSwyllys 
61099ebb4caSwyllys KMF_RETURN
611d00756ccSwyllys select_token(void *kmfhandle, char *token, int readonly)
61299ebb4caSwyllys {
61330a5e8faSwyllys 	KMF_ATTRIBUTE attlist[10];
61430a5e8faSwyllys 	int i = 0;
61530a5e8faSwyllys 	KMF_KEYSTORE_TYPE kstype = KMF_KEYSTORE_PK11TOKEN;
61699ebb4caSwyllys 	KMF_RETURN rv = KMF_OK;
61799ebb4caSwyllys 
61899ebb4caSwyllys 	if (token == NULL)
61999ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
62099ebb4caSwyllys 
62130a5e8faSwyllys 	kmf_set_attr_at_index(attlist, i,
62230a5e8faSwyllys 	    KMF_KEYSTORE_TYPE_ATTR, &kstype,
62330a5e8faSwyllys 	    sizeof (kstype));
62430a5e8faSwyllys 	i++;
62599ebb4caSwyllys 
62630a5e8faSwyllys 	if (token) {
62730a5e8faSwyllys 		kmf_set_attr_at_index(attlist, i,
62830a5e8faSwyllys 		    KMF_TOKEN_LABEL_ATTR, token,
62930a5e8faSwyllys 		    strlen(token));
63030a5e8faSwyllys 		i++;
63130a5e8faSwyllys 	}
63230a5e8faSwyllys 
63330a5e8faSwyllys 	kmf_set_attr_at_index(attlist, i,
63430a5e8faSwyllys 	    KMF_READONLY_ATTR, &readonly,
63530a5e8faSwyllys 	    sizeof (readonly));
63630a5e8faSwyllys 	i++;
63730a5e8faSwyllys 
63830a5e8faSwyllys 	rv = kmf_configure_keystore(kmfhandle, i, attlist);
63999ebb4caSwyllys 	if (rv == KMF_ERR_TOKEN_SELECTED)
64099ebb4caSwyllys 		rv = KMF_OK;
64199ebb4caSwyllys 	return (rv);
64299ebb4caSwyllys }
64399ebb4caSwyllys 
64499ebb4caSwyllys KMF_RETURN
64599ebb4caSwyllys configure_nss(void *kmfhandle, char *dir, char *prefix)
64699ebb4caSwyllys {
64730a5e8faSwyllys 	KMF_ATTRIBUTE attlist[10];
64830a5e8faSwyllys 	int i = 0;
64930a5e8faSwyllys 	KMF_KEYSTORE_TYPE kstype = KMF_KEYSTORE_NSS;
65099ebb4caSwyllys 	KMF_RETURN rv = KMF_OK;
65199ebb4caSwyllys 
65230a5e8faSwyllys 	kmf_set_attr_at_index(attlist, i,
65330a5e8faSwyllys 	    KMF_KEYSTORE_TYPE_ATTR, &kstype,
65430a5e8faSwyllys 	    sizeof (kstype));
65530a5e8faSwyllys 	i++;
65699ebb4caSwyllys 
65730a5e8faSwyllys 	if (dir) {
65830a5e8faSwyllys 		kmf_set_attr_at_index(attlist, i,
65930a5e8faSwyllys 		    KMF_DIRPATH_ATTR, dir,
66030a5e8faSwyllys 		    strlen(dir));
66130a5e8faSwyllys 		i++;
66230a5e8faSwyllys 	}
66330a5e8faSwyllys 
66430a5e8faSwyllys 	if (prefix) {
66530a5e8faSwyllys 		kmf_set_attr_at_index(attlist, i,
66630a5e8faSwyllys 		    KMF_CERTPREFIX_ATTR, prefix,
66730a5e8faSwyllys 		    strlen(prefix));
66830a5e8faSwyllys 		i++;
66930a5e8faSwyllys 
67030a5e8faSwyllys 		kmf_set_attr_at_index(attlist, i,
67130a5e8faSwyllys 		    KMF_KEYPREFIX_ATTR, prefix,
67230a5e8faSwyllys 		    strlen(prefix));
67330a5e8faSwyllys 		i++;
67430a5e8faSwyllys 	}
67530a5e8faSwyllys 
67630a5e8faSwyllys 	rv = kmf_configure_keystore(kmfhandle, i, attlist);
67799ebb4caSwyllys 	if (rv == KMF_KEYSTORE_ALREADY_INITIALIZED)
67899ebb4caSwyllys 		rv = KMF_OK;
67999ebb4caSwyllys 
68099ebb4caSwyllys 	return (rv);
68199ebb4caSwyllys }
68299ebb4caSwyllys 
68399ebb4caSwyllys KMF_RETURN
68499ebb4caSwyllys get_pk12_password(KMF_CREDENTIAL *cred)
68599ebb4caSwyllys {
68699ebb4caSwyllys 	KMF_RETURN rv = KMF_OK;
68799ebb4caSwyllys 	char prompt[1024];
68899ebb4caSwyllys 
68999ebb4caSwyllys 	/*
69099ebb4caSwyllys 	 * Get the password to use for the PK12 encryption.
69199ebb4caSwyllys 	 */
69299ebb4caSwyllys 	(void) strlcpy(prompt,
69399ebb4caSwyllys 	    gettext("Enter password to use for "
69430a5e8faSwyllys 	    "accessing the PKCS12 file: "), sizeof (prompt));
69599ebb4caSwyllys 
69699ebb4caSwyllys 	if (get_pin(prompt, NULL, (uchar_t **)&cred->cred,
69799ebb4caSwyllys 	    (ulong_t *)&cred->credlen) != CKR_OK) {
69899ebb4caSwyllys 		cred->cred = NULL;
69999ebb4caSwyllys 		cred->credlen = 0;
70099ebb4caSwyllys 	}
70199ebb4caSwyllys 
70299ebb4caSwyllys 	return (rv);
70399ebb4caSwyllys }
70499ebb4caSwyllys 
7052cbed729Swyllys #define	FILENAME_PROMPT gettext("Filename:")
7062cbed729Swyllys #define	FILENAME_MINLEN	1
7072cbed729Swyllys #define	FILENAME_MAXLEN MAXPATHLEN
70899ebb4caSwyllys 
7092cbed729Swyllys #define	COUNTRY_PROMPT	gettext("Country Name (2 letter code) [US]:")
7102cbed729Swyllys #define	STATE_PROMPT	gettext("State or Province Name (full name) " \
7112cbed729Swyllys 	"[Some-State]:")
7122cbed729Swyllys #define	LOCALITY_PROMPT	gettext("Locality Name (eg, city) []:")
7132cbed729Swyllys #define	ORG_PROMPT	gettext("Organization Name (eg, company) []:")
7142cbed729Swyllys #define	UNIT_PROMPT	gettext("Organizational Unit Name (eg, section) []:")
7152cbed729Swyllys #define	NAME_PROMPT	gettext("Common Name (eg, YOUR name) []:")
7162cbed729Swyllys #define	EMAIL_PROMPT	gettext("Email Address []:")
7172cbed729Swyllys 
7182cbed729Swyllys #define	SERNO_PROMPT	gettext("Serial Number (hex value, example: " \
7192cbed729Swyllys 	"0x01020304):")
7202cbed729Swyllys #define	SERNO_MINLEN	3
7212cbed729Swyllys #define	SERNO_MAXLEN	42
7222cbed729Swyllys 
7232cbed729Swyllys #define	LABEL_PROMPT	gettext("Enter a label for the certificate:")
7242cbed729Swyllys #define	LABEL_MINLEN	1
7252cbed729Swyllys #define	LABEL_MAXLEN	1024
72699ebb4caSwyllys 
72799ebb4caSwyllys #define	COUNTRY_DEFAULT "US"
7282cbed729Swyllys #define	STATE_DEFAULT	NULL
7292cbed729Swyllys #define	INVALID_INPUT 	gettext("Invalid input; please re-enter ...")
73099ebb4caSwyllys 
73199ebb4caSwyllys #define	SUBNAMESIZ	1024
73299ebb4caSwyllys #define	RDN_MIN		1
73399ebb4caSwyllys #define	RDN_MAX		64
73499ebb4caSwyllys #define	COUNTRYNAME_MIN	2
73599ebb4caSwyllys #define	COUNTRYNAME_MAX	2
73699ebb4caSwyllys 
73799ebb4caSwyllys static char *
73899ebb4caSwyllys get_input_string(char *prompt, char *default_str, int min_len, int max_len)
73999ebb4caSwyllys {
74099ebb4caSwyllys 	char buf[1024];
74199ebb4caSwyllys 	char *response = NULL;
74299ebb4caSwyllys 	char *ret = NULL;
74399ebb4caSwyllys 	int len;
74499ebb4caSwyllys 
74599ebb4caSwyllys 	for (;;) {
74699ebb4caSwyllys 		(void) printf("\t%s", prompt);
74799ebb4caSwyllys 		(void) fflush(stdout);
74899ebb4caSwyllys 
74999ebb4caSwyllys 		response = fgets(buf, sizeof (buf), stdin);
75099ebb4caSwyllys 		if (response == NULL) {
75199ebb4caSwyllys 			if (default_str != NULL) {
75299ebb4caSwyllys 				ret = strdup(default_str);
75399ebb4caSwyllys 			}
75499ebb4caSwyllys 			break;
75599ebb4caSwyllys 		}
75699ebb4caSwyllys 
75799ebb4caSwyllys 		/* Skip any leading white space. */
75899ebb4caSwyllys 		while (isspace(*response))
75999ebb4caSwyllys 			response++;
76099ebb4caSwyllys 		if (*response == '\0') {
76199ebb4caSwyllys 			if (default_str != NULL) {
76299ebb4caSwyllys 				ret = strdup(default_str);
76399ebb4caSwyllys 			}
76499ebb4caSwyllys 			break;
76599ebb4caSwyllys 		}
76699ebb4caSwyllys 
76799ebb4caSwyllys 		len = strlen(response);
76899ebb4caSwyllys 		response[len-1] = '\0'; /* get rid of "LF" */
76999ebb4caSwyllys 		len--;
77099ebb4caSwyllys 		if (len >= min_len && len <= max_len) {
77199ebb4caSwyllys 			ret = strdup(response);
77299ebb4caSwyllys 			break;
77399ebb4caSwyllys 		}
77499ebb4caSwyllys 
77599ebb4caSwyllys 		(void) printf("%s\n", INVALID_INPUT);
77699ebb4caSwyllys 
77799ebb4caSwyllys 	}
77899ebb4caSwyllys 
77999ebb4caSwyllys 	return (ret);
78099ebb4caSwyllys }
78199ebb4caSwyllys 
78299ebb4caSwyllys int
7832cbed729Swyllys get_filename(char *txt, char **result)
7842cbed729Swyllys {
7852cbed729Swyllys 	char prompt[1024];
7862cbed729Swyllys 	char *fname = NULL;
7872cbed729Swyllys 
7882cbed729Swyllys 	(void) snprintf(prompt, sizeof (prompt),
7892cbed729Swyllys 	    gettext("Enter filename for the %s: "),
7902cbed729Swyllys 	    txt);
7912cbed729Swyllys 	fname = get_input_string(prompt, NULL,
7922cbed729Swyllys 	    FILENAME_MINLEN, FILENAME_MAXLEN);
7932cbed729Swyllys 	*result = fname;
7942cbed729Swyllys 	return (0);
7952cbed729Swyllys }
7962cbed729Swyllys 
7972cbed729Swyllys int
7982cbed729Swyllys get_certlabel(char **result)
7992cbed729Swyllys {
8002cbed729Swyllys 	char *label = NULL;
8012cbed729Swyllys 
8022cbed729Swyllys 	label = get_input_string(LABEL_PROMPT, NULL,
8032cbed729Swyllys 	    LABEL_MINLEN, LABEL_MAXLEN);
8042cbed729Swyllys 	*result = label;
8052cbed729Swyllys 	return (0);
8062cbed729Swyllys }
8072cbed729Swyllys 
8082cbed729Swyllys int
8092cbed729Swyllys get_serial(char **result)
8102cbed729Swyllys {
8112cbed729Swyllys 	char *serial = NULL;
8122cbed729Swyllys 
8132cbed729Swyllys 	serial = get_input_string(SERNO_PROMPT, NULL, SERNO_MINLEN,
8142cbed729Swyllys 	    SERNO_MAXLEN);
8152cbed729Swyllys 
8162cbed729Swyllys 	*result = serial;
8172cbed729Swyllys 	return (0);
8182cbed729Swyllys }
8192cbed729Swyllys 
8202cbed729Swyllys int
82199ebb4caSwyllys get_subname(char **result)
82299ebb4caSwyllys {
82399ebb4caSwyllys 	char *country = NULL;
82499ebb4caSwyllys 	char *state = NULL;
82599ebb4caSwyllys 	char *locality = NULL;
82699ebb4caSwyllys 	char *org = NULL;
82799ebb4caSwyllys 	char *unit = NULL;
82899ebb4caSwyllys 	char *name = NULL;
82999ebb4caSwyllys 	char *email = NULL;
83099ebb4caSwyllys 	char *subname = NULL;
83199ebb4caSwyllys 
83299ebb4caSwyllys 	(void) printf("Entering following fields for subject (a DN) ...\n");
83399ebb4caSwyllys 	country = get_input_string(COUNTRY_PROMPT, COUNTRY_DEFAULT,
83499ebb4caSwyllys 	    COUNTRYNAME_MIN, COUNTRYNAME_MAX);
83599ebb4caSwyllys 	if (country == NULL)
83699ebb4caSwyllys 		return (-1);
83799ebb4caSwyllys 
83899ebb4caSwyllys 	state = get_input_string(STATE_PROMPT, STATE_DEFAULT,
83999ebb4caSwyllys 	    RDN_MIN, RDN_MAX);
84099ebb4caSwyllys 
84199ebb4caSwyllys 	locality = get_input_string(LOCALITY_PROMPT, NULL, RDN_MIN, RDN_MAX);
84299ebb4caSwyllys 	org = get_input_string(ORG_PROMPT, NULL, RDN_MIN, RDN_MAX);
84399ebb4caSwyllys 	unit = get_input_string(UNIT_PROMPT, NULL, RDN_MIN, RDN_MAX);
84499ebb4caSwyllys 	name = get_input_string(NAME_PROMPT, NULL, RDN_MIN, RDN_MAX);
84599ebb4caSwyllys 	email = get_input_string(EMAIL_PROMPT, NULL, RDN_MIN, RDN_MAX);
84699ebb4caSwyllys 
84799ebb4caSwyllys 	/* Now create a subject name from the input strings */
84899ebb4caSwyllys 	if ((subname = malloc(SUBNAMESIZ)) == NULL)
84999ebb4caSwyllys 		goto out;
85099ebb4caSwyllys 
85199ebb4caSwyllys 	(void) memset(subname, 0, SUBNAMESIZ);
85299ebb4caSwyllys 	(void) strlcpy(subname, "C=", SUBNAMESIZ);
85399ebb4caSwyllys 	(void) strlcat(subname, country, SUBNAMESIZ);
8542cbed729Swyllys 	if (state != NULL) {
8552cbed729Swyllys 		(void) strlcat(subname, ", ST=", SUBNAMESIZ);
85699ebb4caSwyllys 		(void) strlcat(subname, state, SUBNAMESIZ);
8572cbed729Swyllys 	}
85899ebb4caSwyllys 
8592cbed729Swyllys 	if (locality != NULL) {
8602cbed729Swyllys 		(void) strlcat(subname, ", L=", SUBNAMESIZ);
86199ebb4caSwyllys 		(void) strlcat(subname, locality, SUBNAMESIZ);
86299ebb4caSwyllys 	}
86399ebb4caSwyllys 
8642cbed729Swyllys 	if (org != NULL) {
8652cbed729Swyllys 		(void) strlcat(subname, ", O=", SUBNAMESIZ);
86699ebb4caSwyllys 		(void) strlcat(subname, org, SUBNAMESIZ);
86799ebb4caSwyllys 	}
86899ebb4caSwyllys 
8692cbed729Swyllys 	if (unit != NULL) {
8702cbed729Swyllys 		(void) strlcat(subname, ", OU=", SUBNAMESIZ);
87199ebb4caSwyllys 		(void) strlcat(subname, unit, SUBNAMESIZ);
87299ebb4caSwyllys 	}
87399ebb4caSwyllys 
8742cbed729Swyllys 	if (name != NULL) {
8752cbed729Swyllys 		(void) strlcat(subname, ", CN=", SUBNAMESIZ);
87699ebb4caSwyllys 		(void) strlcat(subname, name, SUBNAMESIZ);
87799ebb4caSwyllys 	}
87899ebb4caSwyllys 
8792cbed729Swyllys 	if (email != NULL) {
8802cbed729Swyllys 		(void) strlcat(subname, ", E=", SUBNAMESIZ);
88199ebb4caSwyllys 		(void) strlcat(subname, email, SUBNAMESIZ);
88299ebb4caSwyllys 	}
88399ebb4caSwyllys 
88499ebb4caSwyllys out:
88599ebb4caSwyllys 	if (country)
88699ebb4caSwyllys 		free(country);
88799ebb4caSwyllys 	if (state)
88899ebb4caSwyllys 		free(state);
88999ebb4caSwyllys 	if (locality)
89099ebb4caSwyllys 		free(locality);
89199ebb4caSwyllys 	if (org)
89299ebb4caSwyllys 		free(org);
89399ebb4caSwyllys 	if (unit)
89499ebb4caSwyllys 		free(unit);
89599ebb4caSwyllys 	if (name)
89699ebb4caSwyllys 		free(name);
89799ebb4caSwyllys 	if (email)
89899ebb4caSwyllys 		free(email);
89999ebb4caSwyllys 
90099ebb4caSwyllys 	if (subname == NULL)
90199ebb4caSwyllys 		return (-1);
90299ebb4caSwyllys 	else {
90399ebb4caSwyllys 		*result = subname;
90499ebb4caSwyllys 		return (0);
90599ebb4caSwyllys 	}
90699ebb4caSwyllys }
90799ebb4caSwyllys 
90899ebb4caSwyllys /*
90999ebb4caSwyllys  * Parse a string of KeyUsage values and convert
91099ebb4caSwyllys  * them to the correct KU Bits.
91199ebb4caSwyllys  * The field may be marked "critical" by prepending
91299ebb4caSwyllys  * "critical:" to the list.
91399ebb4caSwyllys  * EX:  critical:digitialSignature,keyEncipherment
91499ebb4caSwyllys  */
91599ebb4caSwyllys KMF_RETURN
91699ebb4caSwyllys verify_keyusage(char *kustr, uint16_t *kubits, int *critical)
91799ebb4caSwyllys {
91899ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
91999ebb4caSwyllys 	uint16_t kuval;
92099ebb4caSwyllys 	char *k;
92199ebb4caSwyllys 
92299ebb4caSwyllys 	*kubits = 0;
923d00756ccSwyllys 	if (kustr == NULL || strlen(kustr) == 0)
92499ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
92599ebb4caSwyllys 
92699ebb4caSwyllys 	/* Check to see if this is critical */
927d00756ccSwyllys 	if (strncasecmp(kustr, "critical:", strlen("critical:")) == 0) {
92899ebb4caSwyllys 		*critical = TRUE;
92999ebb4caSwyllys 		kustr += strlen("critical:");
93099ebb4caSwyllys 	} else {
93199ebb4caSwyllys 		*critical = FALSE;
93299ebb4caSwyllys 	}
93399ebb4caSwyllys 
93499ebb4caSwyllys 	k = strtok(kustr, ",");
93599ebb4caSwyllys 	while (k != NULL) {
93630a5e8faSwyllys 		kuval = kmf_string_to_ku(k);
93799ebb4caSwyllys 		if (kuval == 0) {
93899ebb4caSwyllys 			*kubits = 0;
93999ebb4caSwyllys 			return (KMF_ERR_BAD_PARAMETER);
94099ebb4caSwyllys 		}
94199ebb4caSwyllys 		*kubits |= kuval;
94299ebb4caSwyllys 		k = strtok(NULL, ",");
94399ebb4caSwyllys 	}
94499ebb4caSwyllys 
94599ebb4caSwyllys 	return (ret);
94699ebb4caSwyllys }
94799ebb4caSwyllys 
94899ebb4caSwyllys /*
94999ebb4caSwyllys  * Verify the alternate subject label is real or invalid.
95099ebb4caSwyllys  *
95199ebb4caSwyllys  * The field may be marked "critical" by prepending
95299ebb4caSwyllys  * "critical:" to the list.
95399ebb4caSwyllys  * EX:  "critical:IP=1.2.3.4"
95499ebb4caSwyllys  */
95599ebb4caSwyllys KMF_RETURN
95699ebb4caSwyllys verify_altname(char *arg, KMF_GENERALNAMECHOICES *type, int *critical)
95799ebb4caSwyllys {
95899ebb4caSwyllys 	char *p;
95999ebb4caSwyllys 	KMF_RETURN rv = KMF_OK;
96099ebb4caSwyllys 
96199ebb4caSwyllys 	/* Check to see if this is critical */
962d00756ccSwyllys 	if (strncasecmp(arg, "critical:", strlen("critical:")) == 0) {
96399ebb4caSwyllys 		*critical = TRUE;
96499ebb4caSwyllys 		arg += strlen("critical:");
96599ebb4caSwyllys 	} else {
96699ebb4caSwyllys 		*critical = FALSE;
96799ebb4caSwyllys 	}
96899ebb4caSwyllys 
96999ebb4caSwyllys 	/* Make sure there is an "=" sign */
97099ebb4caSwyllys 	p = strchr(arg, '=');
97199ebb4caSwyllys 	if (p == NULL)
97299ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
97399ebb4caSwyllys 
97499ebb4caSwyllys 	p[0] = '\0';
97599ebb4caSwyllys 
97699ebb4caSwyllys 	if (strcmp(arg, "IP") == 0)
97799ebb4caSwyllys 		*type = GENNAME_IPADDRESS;
97899ebb4caSwyllys 	else if (strcmp(arg, "DNS") == 0)
97999ebb4caSwyllys 		*type = GENNAME_DNSNAME;
98099ebb4caSwyllys 	else if (strcmp(arg, "EMAIL") == 0)
98199ebb4caSwyllys 		*type = GENNAME_RFC822NAME;
98299ebb4caSwyllys 	else if (strcmp(arg, "URI") == 0)
98399ebb4caSwyllys 		*type = GENNAME_URI;
98499ebb4caSwyllys 	else if (strcmp(arg, "DN") == 0)
98599ebb4caSwyllys 		*type = GENNAME_DIRECTORYNAME;
98699ebb4caSwyllys 	else if (strcmp(arg, "RID") == 0)
98799ebb4caSwyllys 		*type = GENNAME_REGISTEREDID;
988d00756ccSwyllys 	else if (strcmp(arg, "KRB") == 0)
989d00756ccSwyllys 		*type = GENNAME_KRB5PRINC;
990d00756ccSwyllys 	else if (strcmp(arg, "UPN") == 0)
991d00756ccSwyllys 		*type = GENNAME_SCLOGON_UPN;
99299ebb4caSwyllys 	else
99399ebb4caSwyllys 		rv = KMF_ERR_BAD_PARAMETER;
99499ebb4caSwyllys 
99599ebb4caSwyllys 	p[0] = '=';
99699ebb4caSwyllys 
99799ebb4caSwyllys 	return (rv);
99899ebb4caSwyllys }
99999ebb4caSwyllys 
100099ebb4caSwyllys int
100199ebb4caSwyllys get_token_password(KMF_KEYSTORE_TYPE kstype,
100299ebb4caSwyllys 	char *token_spec, KMF_CREDENTIAL *cred)
100399ebb4caSwyllys {
100499ebb4caSwyllys 	char	prompt[1024];
100599ebb4caSwyllys 	char	*p = NULL;
100699ebb4caSwyllys 
100799ebb4caSwyllys 	if (kstype == KMF_KEYSTORE_PK11TOKEN) {
100899ebb4caSwyllys 		p = strchr(token_spec, ':');
100999ebb4caSwyllys 		if (p != NULL)
101099ebb4caSwyllys 		*p = 0;
101199ebb4caSwyllys 	}
101299ebb4caSwyllys 	/*
101399ebb4caSwyllys 	 * Login to the token first.
101499ebb4caSwyllys 	 */
101599ebb4caSwyllys 	(void) snprintf(prompt, sizeof (prompt),
101630a5e8faSwyllys 	    gettext(DEFAULT_TOKEN_PROMPT), token_spec);
101799ebb4caSwyllys 
101899ebb4caSwyllys 	if (get_pin(prompt, NULL, (uchar_t **)&cred->cred,
101999ebb4caSwyllys 	    (ulong_t *)&cred->credlen) != CKR_OK) {
102099ebb4caSwyllys 		cred->cred = NULL;
102199ebb4caSwyllys 		cred->credlen = 0;
102299ebb4caSwyllys 	}
102399ebb4caSwyllys 
102499ebb4caSwyllys 	if (kstype == KMF_KEYSTORE_PK11TOKEN && p != NULL)
102599ebb4caSwyllys 		*p = ':';
102699ebb4caSwyllys 	return (KMF_OK);
102799ebb4caSwyllys }
102899ebb4caSwyllys 
102999ebb4caSwyllys KMF_RETURN
103099ebb4caSwyllys verify_file(char *filename)
103199ebb4caSwyllys {
103299ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
103399ebb4caSwyllys 	int fd;
103499ebb4caSwyllys 
103599ebb4caSwyllys 	/*
103699ebb4caSwyllys 	 * Attempt to open with  the EXCL flag so that if
103799ebb4caSwyllys 	 * it already exists, the open will fail.  It will
103899ebb4caSwyllys 	 * also fail if the file cannot be created due to
103999ebb4caSwyllys 	 * permissions on the parent directory, or if the
104099ebb4caSwyllys 	 * parent directory itself does not exist.
104199ebb4caSwyllys 	 */
104299ebb4caSwyllys 	fd = open(filename, O_CREAT | O_EXCL, 0600);
104399ebb4caSwyllys 	if (fd == -1)
104499ebb4caSwyllys 		return (KMF_ERR_OPEN_FILE);
104599ebb4caSwyllys 
104699ebb4caSwyllys 	/* If we were able to create it, delete it. */
104799ebb4caSwyllys 	(void) close(fd);
104899ebb4caSwyllys 	(void) unlink(filename);
104999ebb4caSwyllys 
105099ebb4caSwyllys 	return (ret);
105199ebb4caSwyllys }
105299ebb4caSwyllys 
105399ebb4caSwyllys void
105499ebb4caSwyllys display_error(void *handle, KMF_RETURN errcode, char *prefix)
105599ebb4caSwyllys {
105699ebb4caSwyllys 	KMF_RETURN rv1, rv2;
105799ebb4caSwyllys 	char *plugin_errmsg = NULL;
105899ebb4caSwyllys 	char *kmf_errmsg = NULL;
105999ebb4caSwyllys 
106030a5e8faSwyllys 	rv1 = kmf_get_plugin_error_str(handle, &plugin_errmsg);
106130a5e8faSwyllys 	rv2 = kmf_get_kmf_error_str(errcode, &kmf_errmsg);
106299ebb4caSwyllys 
106399ebb4caSwyllys 	cryptoerror(LOG_STDERR, "%s:", prefix);
106499ebb4caSwyllys 	if (rv1 == KMF_OK && plugin_errmsg) {
106530a5e8faSwyllys 		cryptoerror(LOG_STDERR, gettext("keystore error: %s"),
106699ebb4caSwyllys 		    plugin_errmsg);
106730a5e8faSwyllys 		kmf_free_str(plugin_errmsg);
106899ebb4caSwyllys 	}
106999ebb4caSwyllys 
107099ebb4caSwyllys 	if (rv2 == KMF_OK && kmf_errmsg) {
107130a5e8faSwyllys 		cryptoerror(LOG_STDERR, gettext("libkmf error: %s"),
107299ebb4caSwyllys 		    kmf_errmsg);
107330a5e8faSwyllys 		kmf_free_str(kmf_errmsg);
107499ebb4caSwyllys 	}
107599ebb4caSwyllys 
107699ebb4caSwyllys 	if (rv1 != KMF_OK && rv2 != KMF_OK)
107799ebb4caSwyllys 		cryptoerror(LOG_STDERR, gettext("<unknown error>\n"));
107899ebb4caSwyllys 
107999ebb4caSwyllys }
1080d00756ccSwyllys 
1081d00756ccSwyllys static KMF_RETURN
1082d00756ccSwyllys addToEKUList(EKU_LIST *ekus, int critical, KMF_OID *newoid)
1083d00756ccSwyllys {
1084d00756ccSwyllys 	if (newoid != NULL && ekus != NULL) {
1085d00756ccSwyllys 		ekus->eku_count++;
1086d00756ccSwyllys 
1087d00756ccSwyllys 		ekus->critlist = realloc(ekus->critlist,
1088d00756ccSwyllys 		    ekus->eku_count * sizeof (int));
1089d00756ccSwyllys 		if (ekus->critlist != NULL)
1090d00756ccSwyllys 			ekus->critlist[ekus->eku_count-1] = critical;
1091d00756ccSwyllys 		else
1092d00756ccSwyllys 			return (KMF_ERR_MEMORY);
1093d00756ccSwyllys 
1094d00756ccSwyllys 		ekus->ekulist = realloc(
1095d00756ccSwyllys 		    ekus->ekulist, ekus->eku_count * sizeof (KMF_OID));
1096d00756ccSwyllys 		if (ekus->ekulist != NULL)
1097d00756ccSwyllys 			ekus->ekulist[ekus->eku_count-1] = *newoid;
1098d00756ccSwyllys 		else
1099d00756ccSwyllys 			return (KMF_ERR_MEMORY);
1100d00756ccSwyllys 	}
1101d00756ccSwyllys 	return (KMF_OK);
1102d00756ccSwyllys }
1103d00756ccSwyllys 
1104d00756ccSwyllys void
1105d00756ccSwyllys free_eku_list(EKU_LIST *ekus)
1106d00756ccSwyllys {
1107d00756ccSwyllys 	if (ekus != NULL && ekus->eku_count > 0) {
1108d00756ccSwyllys 		int i;
1109d00756ccSwyllys 		for (i = 0; i < ekus->eku_count; i++) {
1110d00756ccSwyllys 			kmf_free_data(&ekus->ekulist[i]);
1111d00756ccSwyllys 		}
1112d00756ccSwyllys 		free(ekus->ekulist);
1113d00756ccSwyllys 		free(ekus->critlist);
1114d00756ccSwyllys 	}
1115d00756ccSwyllys }
1116d00756ccSwyllys 
1117d00756ccSwyllys static KMF_RETURN
1118d00756ccSwyllys parse_ekus(char *ekustr, EKU_LIST *ekus)
1119d00756ccSwyllys {
1120d00756ccSwyllys 	KMF_RETURN rv = KMF_OK;
1121d00756ccSwyllys 	KMF_OID *newoid;
1122d00756ccSwyllys 	int critical;
1123d00756ccSwyllys 
1124d00756ccSwyllys 	if (strncasecmp(ekustr, "critical:",
1125d00756ccSwyllys 	    strlen("critical:")) == 0) {
1126d00756ccSwyllys 		critical = TRUE;
1127d00756ccSwyllys 		ekustr += strlen("critical:");
1128d00756ccSwyllys 	} else {
1129d00756ccSwyllys 		critical = FALSE;
1130d00756ccSwyllys 	}
1131d00756ccSwyllys 	newoid = kmf_ekuname_to_oid(ekustr);
1132d00756ccSwyllys 	if (newoid != NULL) {
1133d00756ccSwyllys 		rv = addToEKUList(ekus, critical, newoid);
1134d00756ccSwyllys 		free(newoid);
1135d00756ccSwyllys 	} else {
1136d00756ccSwyllys 		rv = PK_ERR_USAGE;
1137d00756ccSwyllys 	}
1138d00756ccSwyllys 
1139d00756ccSwyllys 	return (rv);
1140d00756ccSwyllys }
1141d00756ccSwyllys 
1142d00756ccSwyllys KMF_RETURN
1143d00756ccSwyllys verify_ekunames(char *ekuliststr, EKU_LIST **ekulist)
1144d00756ccSwyllys {
1145d00756ccSwyllys 	KMF_RETURN rv = KMF_OK;
1146d00756ccSwyllys 	char *p;
1147d00756ccSwyllys 	EKU_LIST *ekus = NULL;
1148d00756ccSwyllys 
1149d00756ccSwyllys 	if (ekuliststr == NULL || strlen(ekuliststr) == 0)
1150d00756ccSwyllys 		return (0);
1151d00756ccSwyllys 
1152d00756ccSwyllys 	/*
1153d00756ccSwyllys 	 * The list should be comma separated list of EKU Names.
1154d00756ccSwyllys 	 */
1155d00756ccSwyllys 	p = strtok(ekuliststr, ",");
1156d00756ccSwyllys 
1157d00756ccSwyllys 	/* If no tokens found, then maybe it's just a single EKU value */
1158d00756ccSwyllys 	if (p == NULL) {
1159d00756ccSwyllys 		rv = parse_ekus(ekuliststr, ekus);
1160d00756ccSwyllys 	}
1161d00756ccSwyllys 
1162d00756ccSwyllys 	while (p != NULL) {
1163d00756ccSwyllys 		rv = parse_ekus(p, ekus);
1164d00756ccSwyllys 
1165d00756ccSwyllys 		if (rv != KMF_OK)
1166d00756ccSwyllys 			break;
1167d00756ccSwyllys 		p = strtok(NULL, ",");
1168d00756ccSwyllys 	}
1169d00756ccSwyllys 
1170d00756ccSwyllys 	if (rv != KMF_OK)
1171d00756ccSwyllys 		free_eku_list(ekus);
1172d00756ccSwyllys 	else
1173d00756ccSwyllys 		*ekulist = ekus;
1174d00756ccSwyllys 
1175d00756ccSwyllys 	return (rv);
1176d00756ccSwyllys }
1177*fa60c371Swyllys 
1178*fa60c371Swyllys KMF_RETURN
1179*fa60c371Swyllys token_auth_needed(KMF_HANDLE_T handle, char *tokenlabel, int *auth)
1180*fa60c371Swyllys {
1181*fa60c371Swyllys 	CK_TOKEN_INFO info;
1182*fa60c371Swyllys 	CK_SLOT_ID slot;
1183*fa60c371Swyllys 	CK_RV ckrv;
1184*fa60c371Swyllys 	KMF_RETURN rv;
1185*fa60c371Swyllys 
1186*fa60c371Swyllys 	*auth = 0;
1187*fa60c371Swyllys 	rv = kmf_pk11_token_lookup(handle, tokenlabel, &slot);
1188*fa60c371Swyllys 	if (rv != KMF_OK)
1189*fa60c371Swyllys 		return (rv);
1190*fa60c371Swyllys 
1191*fa60c371Swyllys 	ckrv = C_GetTokenInfo(slot, &info);
1192*fa60c371Swyllys 	if (ckrv != KMF_OK)
1193*fa60c371Swyllys 		return (KMF_ERR_INTERNAL);
1194*fa60c371Swyllys 
1195*fa60c371Swyllys 	*auth = (info.flags & CKF_LOGIN_REQUIRED);
1196*fa60c371Swyllys 
1197*fa60c371Swyllys 	return (KMF_OK);
1198*fa60c371Swyllys }
1199