199ebb4caSwyllys /*
299ebb4caSwyllys  * CDDL HEADER START
399ebb4caSwyllys  *
499ebb4caSwyllys  * 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.
799ebb4caSwyllys  *
899ebb4caSwyllys  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
999ebb4caSwyllys  * or http://www.opensolaris.org/os/licensing.
1099ebb4caSwyllys  * See the License for the specific language governing permissions
1199ebb4caSwyllys  * and limitations under the License.
1299ebb4caSwyllys  *
1399ebb4caSwyllys  * When distributing Covered Code, include this CDDL HEADER in each
1499ebb4caSwyllys  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1599ebb4caSwyllys  * If applicable, add the following below this CDDL HEADER, with the
1699ebb4caSwyllys  * fields enclosed by brackets "[]" replaced with your own identifying
1799ebb4caSwyllys  * information: Portions Copyright [yyyy] [name of copyright owner]
1899ebb4caSwyllys  *
1999ebb4caSwyllys  * CDDL HEADER END
2099ebb4caSwyllys  *
21269e59f9SJan Pechanec  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
2299ebb4caSwyllys  */
2399ebb4caSwyllys 
2499ebb4caSwyllys #include <stdio.h>
2599ebb4caSwyllys #include <strings.h>
2699ebb4caSwyllys #include <ctype.h>
2799ebb4caSwyllys #include <libgen.h>
2899ebb4caSwyllys #include <libintl.h>
2999ebb4caSwyllys #include <errno.h>
30431deaa0Shylee #include <sys/stat.h>
3199ebb4caSwyllys #include <kmfapiP.h>
3299ebb4caSwyllys #include "util.h"
3399ebb4caSwyllys 
34431deaa0Shylee #define	LIB_NSS_PATH	"/usr/lib/mps/libnss3.so"
35431deaa0Shylee #define	LIB_NSPR_PATH	"/usr/lib/mps/libnspr4.so"
36431deaa0Shylee 
3799ebb4caSwyllys static void
show_policy(KMF_POLICY_RECORD * plc)3899ebb4caSwyllys show_policy(KMF_POLICY_RECORD *plc)
3999ebb4caSwyllys {
4099ebb4caSwyllys 	int i;
4199ebb4caSwyllys 	if (plc == NULL)
4299ebb4caSwyllys 		return;
4399ebb4caSwyllys 
4499ebb4caSwyllys 	(void) printf("Name: %s\n", plc->name);
4599ebb4caSwyllys 
4699ebb4caSwyllys 	(void) printf(gettext("Ignore Date: %s\n"),
4730a5e8faSwyllys 	    plc->ignore_date ? gettext("true") : gettext("false"));
4899ebb4caSwyllys 
4999ebb4caSwyllys 	(void) printf(gettext("Ignore Unknown EKUs: %s\n"),
5030a5e8faSwyllys 	    plc->ignore_unknown_ekus ? gettext("true") : gettext("false"));
5199ebb4caSwyllys 
5299ebb4caSwyllys 	(void) printf(gettext("Ignore TA: %s\n"),
5330a5e8faSwyllys 	    plc->ignore_trust_anchor ? gettext("true") : gettext("false"));
5499ebb4caSwyllys 
5599ebb4caSwyllys 	(void) printf(gettext("Validity Adjusted Time: %s\n"),
5630a5e8faSwyllys 	    plc->validity_adjusttime ? plc->validity_adjusttime : "<null>");
5799ebb4caSwyllys 
5899ebb4caSwyllys 	if (plc->ta_name == NULL && plc->ta_serial == NULL) {
5999ebb4caSwyllys 		(void) printf(gettext("Trust Anchor Certificate: <null>\n"));
60*fc2613b0SWyllys Ingersoll 	} else if (strcasecmp(plc->ta_name, "search") == 0) {
61*fc2613b0SWyllys Ingersoll 		(void) printf(gettext("Trust Anchor Certificate: "
62*fc2613b0SWyllys Ingersoll 		    "Search by Issuer\n"));
6399ebb4caSwyllys 	} else {
6499ebb4caSwyllys 		(void) printf(gettext("Trust Anchor Certificate:\n"));
6599ebb4caSwyllys 		(void) printf(gettext("\tName: %s\n"),
6630a5e8faSwyllys 		    plc->ta_name ? plc->ta_name : "<null>");
6799ebb4caSwyllys 		(void) printf(gettext("\tSerial Number: %s\n"),
6830a5e8faSwyllys 		    plc->ta_serial ? plc->ta_serial : "<null>");
6999ebb4caSwyllys 	}
7099ebb4caSwyllys 
7199ebb4caSwyllys 	if (plc->ku_bits != 0) {
7299ebb4caSwyllys 		(void) printf(gettext("Key Usage Bits: "));
7399ebb4caSwyllys 		for (i = KULOWBIT; i <= KUHIGHBIT; i++) {
7430a5e8faSwyllys 			char *s = kmf_ku_to_string(
7530a5e8faSwyllys 			    (plc->ku_bits & (1<<i)));
7699ebb4caSwyllys 			if (s != NULL) {
7799ebb4caSwyllys 				(void) printf("%s ", s);
7899ebb4caSwyllys 			}
7999ebb4caSwyllys 		}
8099ebb4caSwyllys 		(void) printf("\n");
8199ebb4caSwyllys 	} else {
8299ebb4caSwyllys 		(void) printf(gettext("Key Usage Bits: 0\n"));
8399ebb4caSwyllys 	}
8499ebb4caSwyllys 
8599ebb4caSwyllys 	if (plc->eku_set.eku_count > 0) {
8699ebb4caSwyllys 		(void) printf(gettext("Extended Key Usage Values:\n"));
8799ebb4caSwyllys 		for (i = 0; i < plc->eku_set.eku_count; i++) {
88d00756ccSwyllys 			char *s = kmf_oid_to_ekuname(
8930a5e8faSwyllys 			    &plc->eku_set.ekulist[i]);
9099ebb4caSwyllys 			(void) printf("\t%s\t(%s)\n",
9130a5e8faSwyllys 			    kmf_oid_to_string(&plc->eku_set.ekulist[i]),
9230a5e8faSwyllys 			    s ? s : "unknown");
9399ebb4caSwyllys 		}
9499ebb4caSwyllys 	} else {
9599ebb4caSwyllys 		(void) printf(gettext("Extended Key Usage Values: <null>\n"));
9699ebb4caSwyllys 	}
9799ebb4caSwyllys 
9899ebb4caSwyllys 	(void) printf(gettext("Validation Policy Information:\n"));
9999ebb4caSwyllys 
10099ebb4caSwyllys 	if (plc->revocation & KMF_REVOCATION_METHOD_OCSP) {
10199ebb4caSwyllys 		(void) printf(gettext("    OCSP:\n"));
10299ebb4caSwyllys 
10399ebb4caSwyllys 		(void) printf(gettext("\tResponder URI: %s\n"),
10499ebb4caSwyllys 		    plc->VAL_OCSP_BASIC.responderURI ?
10599ebb4caSwyllys 		    plc->VAL_OCSP_BASIC.responderURI : "<null>");
10699ebb4caSwyllys 
10799ebb4caSwyllys 		(void) printf(gettext("\tProxy: %s\n"),
10899ebb4caSwyllys 		    plc->VAL_OCSP_BASIC.proxy ?
10999ebb4caSwyllys 		    plc->VAL_OCSP_BASIC.proxy : "<null>");
11099ebb4caSwyllys 
11199ebb4caSwyllys 		(void) printf(gettext("\tUse ResponderURI from Certificate: "
11299ebb4caSwyllys 		    "%s\n"), plc->VAL_OCSP_BASIC.uri_from_cert ?
11399ebb4caSwyllys 		    gettext("true") : gettext("false"));
11499ebb4caSwyllys 
11599ebb4caSwyllys 		(void) printf(gettext("\tResponse lifetime: %s\n"),
11699ebb4caSwyllys 		    plc->VAL_OCSP_BASIC.response_lifetime ?
11799ebb4caSwyllys 		    plc->VAL_OCSP_BASIC.response_lifetime : "<null>");
11899ebb4caSwyllys 
11999ebb4caSwyllys 		(void) printf(gettext("\tIgnore Response signature: %s\n"),
12099ebb4caSwyllys 		    plc->VAL_OCSP_BASIC.ignore_response_sign ?
12199ebb4caSwyllys 		    gettext("true") : gettext("false"));
12299ebb4caSwyllys 
12399ebb4caSwyllys 		if (!plc->VAL_OCSP.has_resp_cert) {
12499ebb4caSwyllys 			(void) printf(gettext("\tResponder Certificate:"
12599ebb4caSwyllys 			    " <null>\n"));
12699ebb4caSwyllys 		} else {
12799ebb4caSwyllys 			(void) printf(gettext("\tResponder Certificate:\n"));
12899ebb4caSwyllys 			(void) printf(gettext("\t\tName: %s\n"),
12999ebb4caSwyllys 			    plc->VAL_OCSP_RESP_CERT.name ?
13099ebb4caSwyllys 			    plc->VAL_OCSP_RESP_CERT.name : "<null>");
13199ebb4caSwyllys 			(void) printf(gettext("\t\tSerial: %s\n"),
13299ebb4caSwyllys 			    plc->VAL_OCSP_RESP_CERT.serial ?
13399ebb4caSwyllys 			    plc->VAL_OCSP_RESP_CERT.serial : "<null>");
13499ebb4caSwyllys 		}
13599ebb4caSwyllys 	}
13699ebb4caSwyllys 
13799ebb4caSwyllys 	if (plc->revocation & KMF_REVOCATION_METHOD_CRL) {
13899ebb4caSwyllys 		(void) printf(gettext("    CRL:\n"));
13999ebb4caSwyllys 
14099ebb4caSwyllys 		(void) printf(gettext("\tBase filename: %s\n"),
14199ebb4caSwyllys 		    plc->validation_info.crl_info.basefilename ?
14299ebb4caSwyllys 		    plc->validation_info.crl_info.basefilename : "<null>");
14399ebb4caSwyllys 
14499ebb4caSwyllys 		(void) printf(gettext("\tDirectory: %s\n"),
14599ebb4caSwyllys 		    plc->validation_info.crl_info.directory ?
14699ebb4caSwyllys 		    plc->validation_info.crl_info.directory : "<null>");
14799ebb4caSwyllys 
14899ebb4caSwyllys 		(void) printf(gettext("\tDownload and cache CRL: %s\n"),
14930a5e8faSwyllys 		    plc->validation_info.crl_info.get_crl_uri ?
15030a5e8faSwyllys 		    gettext("true") : gettext("false"));
15199ebb4caSwyllys 
15299ebb4caSwyllys 		(void) printf(gettext("\tProxy: %s\n"),
15399ebb4caSwyllys 		    plc->validation_info.crl_info.proxy ?
15499ebb4caSwyllys 		    plc->validation_info.crl_info.proxy : "<null>");
15599ebb4caSwyllys 
15699ebb4caSwyllys 		(void) printf(gettext("\tIgnore CRL signature: %s\n"),
15730a5e8faSwyllys 		    plc->validation_info.crl_info.ignore_crl_sign ?
15830a5e8faSwyllys 		    gettext("true") : gettext("false"));
15999ebb4caSwyllys 
16099ebb4caSwyllys 		(void) printf(gettext("\tIgnore CRL validity date: %s\n"),
16130a5e8faSwyllys 		    plc->validation_info.crl_info.ignore_crl_date ?
16230a5e8faSwyllys 		    gettext("true") : gettext("false"));
16399ebb4caSwyllys 	}
164269e59f9SJan Pechanec 	(void) printf(gettext("Mapper name: %s\n"),
165269e59f9SJan Pechanec 	    plc->mapper.mapname ? plc->mapper.mapname : "<null>");
166269e59f9SJan Pechanec 	(void) printf(gettext("Mapper pathname: %s\n"),
167269e59f9SJan Pechanec 	    plc->mapper.pathname ? plc->mapper.pathname : "<null>");
168269e59f9SJan Pechanec 	(void) printf(gettext("Mapper directory: %s\n"),
169269e59f9SJan Pechanec 	    plc->mapper.dir ? plc->mapper.dir : "<null>");
170269e59f9SJan Pechanec 	(void) printf(gettext("Mapper options: %s\n"),
171269e59f9SJan Pechanec 	    plc->mapper.options ? plc->mapper.options : "<null>");
17299ebb4caSwyllys 
17399ebb4caSwyllys 	(void) printf("\n");
17499ebb4caSwyllys }
17599ebb4caSwyllys 
176431deaa0Shylee void
show_plugin(void)177431deaa0Shylee show_plugin(void)
178431deaa0Shylee {
179431deaa0Shylee 	conf_entrylist_t *phead = NULL;
180431deaa0Shylee 	struct stat 	statbuf;
181431deaa0Shylee 
182431deaa0Shylee 	(void) printf(gettext("KMF plugin information:\n"));
183431deaa0Shylee 	(void) printf(gettext("-----------------------\n"));
184431deaa0Shylee 
185431deaa0Shylee 	/* List the built-in plugins */
186431deaa0Shylee 	(void) printf("pkcs11:kmf_pkcs11.so.1 (built-in)\n");
187431deaa0Shylee 	(void) printf("file:kmf_openssl.so.1 (built-in)\n");
188431deaa0Shylee 
189431deaa0Shylee 	/*
190431deaa0Shylee 	 * If the NSS libraries are not installed in the system,
191431deaa0Shylee 	 * then we will not show the nss plugin either.
192431deaa0Shylee 	 */
193431deaa0Shylee 	if (stat(LIB_NSS_PATH, &statbuf) == 0 &&
194431deaa0Shylee 	    stat(LIB_NSPR_PATH, &statbuf) == 0) {
195431deaa0Shylee 		(void) printf("nss:kmf_nss.so.1 (built-in)\n");
196431deaa0Shylee 	}
197431deaa0Shylee 
198431deaa0Shylee 	/* List non-default plugins, if there is any. */
199431deaa0Shylee 	if (get_entrylist(&phead) == KMF_OK) {
200431deaa0Shylee 		while (phead != NULL) {
201431deaa0Shylee 			(void) printf("%s:%s", phead->entry->keystore,
202431deaa0Shylee 			    phead->entry->modulepath);
203431deaa0Shylee 
204431deaa0Shylee 			if (phead->entry->option == NULL)
205431deaa0Shylee 				(void) printf("\n");
206431deaa0Shylee 			else
207431deaa0Shylee 				(void) printf(";option=%s\n",
208431deaa0Shylee 				    phead->entry->option);
209431deaa0Shylee 			phead = phead->next;
210431deaa0Shylee 		}
211431deaa0Shylee 		free_entrylist(phead);
212431deaa0Shylee 	}
213431deaa0Shylee }
214431deaa0Shylee 
215431deaa0Shylee 
21699ebb4caSwyllys int
kc_list(int argc,char * argv[])21799ebb4caSwyllys kc_list(int argc, char *argv[])
21899ebb4caSwyllys {
21999ebb4caSwyllys 	int 		rv = KC_OK;
22099ebb4caSwyllys 	int		opt, found = 0;
22199ebb4caSwyllys 	extern int	optind_av;
22299ebb4caSwyllys 	extern char	*optarg_av;
22399ebb4caSwyllys 	char		*filename = NULL;
22499ebb4caSwyllys 	char		*policyname = NULL;
22599ebb4caSwyllys 	POLICY_LIST	*plclist = NULL, *pnode;
22699ebb4caSwyllys 	int		sanity_err = 0;
227431deaa0Shylee 	boolean_t	list_plugin = B_FALSE;
22899ebb4caSwyllys 
229431deaa0Shylee 	while ((opt = getopt_av(argc, argv, "i:(dbfile)p:(policy)m(plugin)"))
230431deaa0Shylee 	    != EOF) {
23199ebb4caSwyllys 		switch (opt) {
232431deaa0Shylee 		case 'i':
233431deaa0Shylee 			if (list_plugin)
234431deaa0Shylee 				rv = KC_ERR_USAGE;
235431deaa0Shylee 			else {
23699ebb4caSwyllys 				filename = get_string(optarg_av, &rv);
23799ebb4caSwyllys 				if (filename == NULL) {
23899ebb4caSwyllys 					(void) fprintf(stderr,
23999ebb4caSwyllys 					    gettext("Error dbfile input.\n"));
24099ebb4caSwyllys 				}
241431deaa0Shylee 			}
242431deaa0Shylee 			break;
243431deaa0Shylee 		case 'p':
244431deaa0Shylee 			if (list_plugin)
245431deaa0Shylee 				rv = KC_ERR_USAGE;
246431deaa0Shylee 			else {
24799ebb4caSwyllys 				policyname = get_string(optarg_av, &rv);
24899ebb4caSwyllys 				if (policyname == NULL) {
24999ebb4caSwyllys 					(void) fprintf(stderr,
25099ebb4caSwyllys 					    gettext("Error policy name.\n"));
25199ebb4caSwyllys 				}
252431deaa0Shylee 			}
253431deaa0Shylee 			break;
254431deaa0Shylee 		case 'm':
255431deaa0Shylee 			list_plugin = B_TRUE;
256431deaa0Shylee 			break;
257431deaa0Shylee 		default:
258431deaa0Shylee 			(void) fprintf(stderr,
259431deaa0Shylee 			    gettext("Error input option.\n"));
260431deaa0Shylee 			rv = KC_ERR_USAGE;
261431deaa0Shylee 			break;
26299ebb4caSwyllys 		}
26399ebb4caSwyllys 		if (rv != KC_OK)
26499ebb4caSwyllys 			goto out;
26599ebb4caSwyllys 	}
26699ebb4caSwyllys 
26799ebb4caSwyllys 	/* No additional args allowed. */
26899ebb4caSwyllys 	argc -= optind_av;
26999ebb4caSwyllys 	if (argc) {
27099ebb4caSwyllys 		(void) fprintf(stderr,
27199ebb4caSwyllys 		    gettext("Error input option\n"));
27299ebb4caSwyllys 		rv = KC_ERR_USAGE;
27399ebb4caSwyllys 		goto out;
27499ebb4caSwyllys 	}
27599ebb4caSwyllys 
276431deaa0Shylee 	if (list_plugin) {
277431deaa0Shylee 		show_plugin();
278431deaa0Shylee 		goto out;
279431deaa0Shylee 	}
280431deaa0Shylee 
28199ebb4caSwyllys 	if (filename == NULL) {
28299ebb4caSwyllys 		filename = strdup(KMF_DEFAULT_POLICY_FILE);
28399ebb4caSwyllys 		if (filename == NULL) {
28499ebb4caSwyllys 			rv = KC_ERR_MEMORY;
28599ebb4caSwyllys 			goto out;
28699ebb4caSwyllys 		}
28799ebb4caSwyllys 	}
28899ebb4caSwyllys 
28999ebb4caSwyllys 	/* Check the access permission of the policy DB */
29099ebb4caSwyllys 	if (access(filename, R_OK) < 0) {
29199ebb4caSwyllys 		int err = errno;
29299ebb4caSwyllys 		(void) fprintf(stderr,
29399ebb4caSwyllys 		    gettext("Cannot access \"%s\" for list - %s\n"), filename,
29499ebb4caSwyllys 		    strerror(err));
29599ebb4caSwyllys 		rv = KC_ERR_ACCESS;
29699ebb4caSwyllys 		goto out;
29799ebb4caSwyllys 	}
29899ebb4caSwyllys 
29999ebb4caSwyllys 	rv = load_policies(filename, &plclist);
30099ebb4caSwyllys 	if (rv != KMF_OK) {
30199ebb4caSwyllys 		goto out;
30299ebb4caSwyllys 	}
30399ebb4caSwyllys 
30499ebb4caSwyllys 	pnode = plclist;
30599ebb4caSwyllys 	while (pnode != NULL) {
30699ebb4caSwyllys 		if (policyname == NULL ||
30730a5e8faSwyllys 		    strcmp(policyname, pnode->plc.name) == 0) {
30899ebb4caSwyllys 			KMF_POLICY_RECORD *plc = &pnode->plc;
30999ebb4caSwyllys 
31099ebb4caSwyllys 			found++;
31130a5e8faSwyllys 			rv = kmf_verify_policy(plc);
31299ebb4caSwyllys 			if (rv != KMF_OK) {
31399ebb4caSwyllys 				(void) fprintf(stderr, gettext(
31499ebb4caSwyllys 				    "Policy Name: '%s' is invalid\n"),
31599ebb4caSwyllys 				    plc->name);
31699ebb4caSwyllys 				sanity_err++;
31799ebb4caSwyllys 			} else {
31899ebb4caSwyllys 				show_policy(&pnode->plc);
31999ebb4caSwyllys 			}
32099ebb4caSwyllys 		}
32199ebb4caSwyllys 		pnode = pnode->next;
32299ebb4caSwyllys 	}
32399ebb4caSwyllys 
32499ebb4caSwyllys 	free_policy_list(plclist);
32599ebb4caSwyllys 
32699ebb4caSwyllys 	if (!found) {
32799ebb4caSwyllys 		if (policyname)
32899ebb4caSwyllys 			(void) fprintf(stderr, gettext(
32999ebb4caSwyllys 			    "Cannot find policy '%s'\n"), policyname);
33099ebb4caSwyllys 		else
33199ebb4caSwyllys 			(void) fprintf(stderr, gettext("Cannot find "
33299ebb4caSwyllys 			    "any policies to display\n"));
33399ebb4caSwyllys 		rv = KC_ERR_FIND_POLICY;
33499ebb4caSwyllys 	} else if (sanity_err) {
33599ebb4caSwyllys 		rv = KC_ERR_VERIFY_POLICY;
33699ebb4caSwyllys 	}
33799ebb4caSwyllys 
33899ebb4caSwyllys out:
33999ebb4caSwyllys 
34099ebb4caSwyllys 	if (filename != NULL)
34199ebb4caSwyllys 		free(filename);
34299ebb4caSwyllys 
34399ebb4caSwyllys 	if (policyname != NULL)
34499ebb4caSwyllys 		free(policyname);
34599ebb4caSwyllys 
34699ebb4caSwyllys 	return (rv);
34799ebb4caSwyllys }
348