xref: /illumos-gate/usr/src/cmd/cmd-crypto/kmfcfg/list.c (revision 269e59f9a28bf47e0f463e64fc5af4a408b73b21)
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  *
21*269e59f9SJan 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
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"));
6099ebb4caSwyllys 	} else {
6199ebb4caSwyllys 		(void) printf(gettext("Trust Anchor Certificate:\n"));
6299ebb4caSwyllys 		(void) printf(gettext("\tName: %s\n"),
6330a5e8faSwyllys 		    plc->ta_name ? plc->ta_name : "<null>");
6499ebb4caSwyllys 		(void) printf(gettext("\tSerial Number: %s\n"),
6530a5e8faSwyllys 		    plc->ta_serial ? plc->ta_serial : "<null>");
6699ebb4caSwyllys 	}
6799ebb4caSwyllys 
6899ebb4caSwyllys 	if (plc->ku_bits != 0) {
6999ebb4caSwyllys 		(void) printf(gettext("Key Usage Bits: "));
7099ebb4caSwyllys 		for (i = KULOWBIT; i <= KUHIGHBIT; i++) {
7130a5e8faSwyllys 			char *s = kmf_ku_to_string(
7230a5e8faSwyllys 			    (plc->ku_bits & (1<<i)));
7399ebb4caSwyllys 			if (s != NULL) {
7499ebb4caSwyllys 				(void) printf("%s ", s);
7599ebb4caSwyllys 			}
7699ebb4caSwyllys 		}
7799ebb4caSwyllys 		(void) printf("\n");
7899ebb4caSwyllys 	} else {
7999ebb4caSwyllys 		(void) printf(gettext("Key Usage Bits: 0\n"));
8099ebb4caSwyllys 	}
8199ebb4caSwyllys 
8299ebb4caSwyllys 	if (plc->eku_set.eku_count > 0) {
8399ebb4caSwyllys 		(void) printf(gettext("Extended Key Usage Values:\n"));
8499ebb4caSwyllys 		for (i = 0; i < plc->eku_set.eku_count; i++) {
85d00756ccSwyllys 			char *s = kmf_oid_to_ekuname(
8630a5e8faSwyllys 			    &plc->eku_set.ekulist[i]);
8799ebb4caSwyllys 			(void) printf("\t%s\t(%s)\n",
8830a5e8faSwyllys 			    kmf_oid_to_string(&plc->eku_set.ekulist[i]),
8930a5e8faSwyllys 			    s ? s : "unknown");
9099ebb4caSwyllys 		}
9199ebb4caSwyllys 	} else {
9299ebb4caSwyllys 		(void) printf(gettext("Extended Key Usage Values: <null>\n"));
9399ebb4caSwyllys 	}
9499ebb4caSwyllys 
9599ebb4caSwyllys 	(void) printf(gettext("Validation Policy Information:\n"));
9699ebb4caSwyllys 
9799ebb4caSwyllys 	if (plc->revocation & KMF_REVOCATION_METHOD_OCSP) {
9899ebb4caSwyllys 		(void) printf(gettext("    OCSP:\n"));
9999ebb4caSwyllys 
10099ebb4caSwyllys 		(void) printf(gettext("\tResponder URI: %s\n"),
10199ebb4caSwyllys 		    plc->VAL_OCSP_BASIC.responderURI ?
10299ebb4caSwyllys 		    plc->VAL_OCSP_BASIC.responderURI : "<null>");
10399ebb4caSwyllys 
10499ebb4caSwyllys 		(void) printf(gettext("\tProxy: %s\n"),
10599ebb4caSwyllys 		    plc->VAL_OCSP_BASIC.proxy ?
10699ebb4caSwyllys 		    plc->VAL_OCSP_BASIC.proxy : "<null>");
10799ebb4caSwyllys 
10899ebb4caSwyllys 		(void) printf(gettext("\tUse ResponderURI from Certificate: "
10999ebb4caSwyllys 		    "%s\n"), plc->VAL_OCSP_BASIC.uri_from_cert ?
11099ebb4caSwyllys 		    gettext("true") : gettext("false"));
11199ebb4caSwyllys 
11299ebb4caSwyllys 		(void) printf(gettext("\tResponse lifetime: %s\n"),
11399ebb4caSwyllys 		    plc->VAL_OCSP_BASIC.response_lifetime ?
11499ebb4caSwyllys 		    plc->VAL_OCSP_BASIC.response_lifetime : "<null>");
11599ebb4caSwyllys 
11699ebb4caSwyllys 		(void) printf(gettext("\tIgnore Response signature: %s\n"),
11799ebb4caSwyllys 		    plc->VAL_OCSP_BASIC.ignore_response_sign ?
11899ebb4caSwyllys 		    gettext("true") : gettext("false"));
11999ebb4caSwyllys 
12099ebb4caSwyllys 		if (!plc->VAL_OCSP.has_resp_cert) {
12199ebb4caSwyllys 			(void) printf(gettext("\tResponder Certificate:"
12299ebb4caSwyllys 			    " <null>\n"));
12399ebb4caSwyllys 		} else {
12499ebb4caSwyllys 			(void) printf(gettext("\tResponder Certificate:\n"));
12599ebb4caSwyllys 			(void) printf(gettext("\t\tName: %s\n"),
12699ebb4caSwyllys 			    plc->VAL_OCSP_RESP_CERT.name ?
12799ebb4caSwyllys 			    plc->VAL_OCSP_RESP_CERT.name : "<null>");
12899ebb4caSwyllys 			(void) printf(gettext("\t\tSerial: %s\n"),
12999ebb4caSwyllys 			    plc->VAL_OCSP_RESP_CERT.serial ?
13099ebb4caSwyllys 			    plc->VAL_OCSP_RESP_CERT.serial : "<null>");
13199ebb4caSwyllys 		}
13299ebb4caSwyllys 	}
13399ebb4caSwyllys 
13499ebb4caSwyllys 	if (plc->revocation & KMF_REVOCATION_METHOD_CRL) {
13599ebb4caSwyllys 		(void) printf(gettext("    CRL:\n"));
13699ebb4caSwyllys 
13799ebb4caSwyllys 		(void) printf(gettext("\tBase filename: %s\n"),
13899ebb4caSwyllys 		    plc->validation_info.crl_info.basefilename ?
13999ebb4caSwyllys 		    plc->validation_info.crl_info.basefilename : "<null>");
14099ebb4caSwyllys 
14199ebb4caSwyllys 		(void) printf(gettext("\tDirectory: %s\n"),
14299ebb4caSwyllys 		    plc->validation_info.crl_info.directory ?
14399ebb4caSwyllys 		    plc->validation_info.crl_info.directory : "<null>");
14499ebb4caSwyllys 
14599ebb4caSwyllys 		(void) printf(gettext("\tDownload and cache CRL: %s\n"),
14630a5e8faSwyllys 		    plc->validation_info.crl_info.get_crl_uri ?
14730a5e8faSwyllys 		    gettext("true") : gettext("false"));
14899ebb4caSwyllys 
14999ebb4caSwyllys 		(void) printf(gettext("\tProxy: %s\n"),
15099ebb4caSwyllys 		    plc->validation_info.crl_info.proxy ?
15199ebb4caSwyllys 		    plc->validation_info.crl_info.proxy : "<null>");
15299ebb4caSwyllys 
15399ebb4caSwyllys 		(void) printf(gettext("\tIgnore CRL signature: %s\n"),
15430a5e8faSwyllys 		    plc->validation_info.crl_info.ignore_crl_sign ?
15530a5e8faSwyllys 		    gettext("true") : gettext("false"));
15699ebb4caSwyllys 
15799ebb4caSwyllys 		(void) printf(gettext("\tIgnore CRL validity date: %s\n"),
15830a5e8faSwyllys 		    plc->validation_info.crl_info.ignore_crl_date ?
15930a5e8faSwyllys 		    gettext("true") : gettext("false"));
16099ebb4caSwyllys 	}
161*269e59f9SJan Pechanec 	(void) printf(gettext("Mapper name: %s\n"),
162*269e59f9SJan Pechanec 	    plc->mapper.mapname ? plc->mapper.mapname : "<null>");
163*269e59f9SJan Pechanec 	(void) printf(gettext("Mapper pathname: %s\n"),
164*269e59f9SJan Pechanec 	    plc->mapper.pathname ? plc->mapper.pathname : "<null>");
165*269e59f9SJan Pechanec 	(void) printf(gettext("Mapper directory: %s\n"),
166*269e59f9SJan Pechanec 	    plc->mapper.dir ? plc->mapper.dir : "<null>");
167*269e59f9SJan Pechanec 	(void) printf(gettext("Mapper options: %s\n"),
168*269e59f9SJan Pechanec 	    plc->mapper.options ? plc->mapper.options : "<null>");
16999ebb4caSwyllys 
17099ebb4caSwyllys 	(void) printf("\n");
17199ebb4caSwyllys }
17299ebb4caSwyllys 
173431deaa0Shylee void
174431deaa0Shylee show_plugin(void)
175431deaa0Shylee {
176431deaa0Shylee 	conf_entrylist_t *phead = NULL;
177431deaa0Shylee 	struct stat 	statbuf;
178431deaa0Shylee 
179431deaa0Shylee 	(void) printf(gettext("KMF plugin information:\n"));
180431deaa0Shylee 	(void) printf(gettext("-----------------------\n"));
181431deaa0Shylee 
182431deaa0Shylee 	/* List the built-in plugins */
183431deaa0Shylee 	(void) printf("pkcs11:kmf_pkcs11.so.1 (built-in)\n");
184431deaa0Shylee 	(void) printf("file:kmf_openssl.so.1 (built-in)\n");
185431deaa0Shylee 
186431deaa0Shylee 	/*
187431deaa0Shylee 	 * If the NSS libraries are not installed in the system,
188431deaa0Shylee 	 * then we will not show the nss plugin either.
189431deaa0Shylee 	 */
190431deaa0Shylee 	if (stat(LIB_NSS_PATH, &statbuf) == 0 &&
191431deaa0Shylee 	    stat(LIB_NSPR_PATH, &statbuf) == 0) {
192431deaa0Shylee 		(void) printf("nss:kmf_nss.so.1 (built-in)\n");
193431deaa0Shylee 	}
194431deaa0Shylee 
195431deaa0Shylee 	/* List non-default plugins, if there is any. */
196431deaa0Shylee 	if (get_entrylist(&phead) == KMF_OK) {
197431deaa0Shylee 		while (phead != NULL) {
198431deaa0Shylee 			(void) printf("%s:%s", phead->entry->keystore,
199431deaa0Shylee 			    phead->entry->modulepath);
200431deaa0Shylee 
201431deaa0Shylee 			if (phead->entry->option == NULL)
202431deaa0Shylee 				(void) printf("\n");
203431deaa0Shylee 			else
204431deaa0Shylee 				(void) printf(";option=%s\n",
205431deaa0Shylee 				    phead->entry->option);
206431deaa0Shylee 			phead = phead->next;
207431deaa0Shylee 		}
208431deaa0Shylee 		free_entrylist(phead);
209431deaa0Shylee 	}
210431deaa0Shylee }
211431deaa0Shylee 
212431deaa0Shylee 
21399ebb4caSwyllys int
21499ebb4caSwyllys kc_list(int argc, char *argv[])
21599ebb4caSwyllys {
21699ebb4caSwyllys 	int 		rv = KC_OK;
21799ebb4caSwyllys 	int		opt, found = 0;
21899ebb4caSwyllys 	extern int	optind_av;
21999ebb4caSwyllys 	extern char	*optarg_av;
22099ebb4caSwyllys 	char		*filename = NULL;
22199ebb4caSwyllys 	char		*policyname = NULL;
22299ebb4caSwyllys 	POLICY_LIST	*plclist = NULL, *pnode;
22399ebb4caSwyllys 	int		sanity_err = 0;
224431deaa0Shylee 	boolean_t	list_plugin = B_FALSE;
22599ebb4caSwyllys 
226431deaa0Shylee 	while ((opt = getopt_av(argc, argv, "i:(dbfile)p:(policy)m(plugin)"))
227431deaa0Shylee 	    != EOF) {
22899ebb4caSwyllys 		switch (opt) {
229431deaa0Shylee 		case 'i':
230431deaa0Shylee 			if (list_plugin)
231431deaa0Shylee 				rv = KC_ERR_USAGE;
232431deaa0Shylee 			else {
23399ebb4caSwyllys 				filename = get_string(optarg_av, &rv);
23499ebb4caSwyllys 				if (filename == NULL) {
23599ebb4caSwyllys 					(void) fprintf(stderr,
23699ebb4caSwyllys 					    gettext("Error dbfile input.\n"));
23799ebb4caSwyllys 				}
238431deaa0Shylee 			}
239431deaa0Shylee 			break;
240431deaa0Shylee 		case 'p':
241431deaa0Shylee 			if (list_plugin)
242431deaa0Shylee 				rv = KC_ERR_USAGE;
243431deaa0Shylee 			else {
24499ebb4caSwyllys 				policyname = get_string(optarg_av, &rv);
24599ebb4caSwyllys 				if (policyname == NULL) {
24699ebb4caSwyllys 					(void) fprintf(stderr,
24799ebb4caSwyllys 					    gettext("Error policy name.\n"));
24899ebb4caSwyllys 				}
249431deaa0Shylee 			}
250431deaa0Shylee 			break;
251431deaa0Shylee 		case 'm':
252431deaa0Shylee 			list_plugin = B_TRUE;
253431deaa0Shylee 			break;
254431deaa0Shylee 		default:
255431deaa0Shylee 			(void) fprintf(stderr,
256431deaa0Shylee 			    gettext("Error input option.\n"));
257431deaa0Shylee 			rv = KC_ERR_USAGE;
258431deaa0Shylee 			break;
25999ebb4caSwyllys 		}
26099ebb4caSwyllys 		if (rv != KC_OK)
26199ebb4caSwyllys 			goto out;
26299ebb4caSwyllys 	}
26399ebb4caSwyllys 
26499ebb4caSwyllys 	/* No additional args allowed. */
26599ebb4caSwyllys 	argc -= optind_av;
26699ebb4caSwyllys 	if (argc) {
26799ebb4caSwyllys 		(void) fprintf(stderr,
26899ebb4caSwyllys 		    gettext("Error input option\n"));
26999ebb4caSwyllys 		rv = KC_ERR_USAGE;
27099ebb4caSwyllys 		goto out;
27199ebb4caSwyllys 	}
27299ebb4caSwyllys 
273431deaa0Shylee 	if (list_plugin) {
274431deaa0Shylee 		show_plugin();
275431deaa0Shylee 		goto out;
276431deaa0Shylee 	}
277431deaa0Shylee 
27899ebb4caSwyllys 	if (filename == NULL) {
27999ebb4caSwyllys 		filename = strdup(KMF_DEFAULT_POLICY_FILE);
28099ebb4caSwyllys 		if (filename == NULL) {
28199ebb4caSwyllys 			rv = KC_ERR_MEMORY;
28299ebb4caSwyllys 			goto out;
28399ebb4caSwyllys 		}
28499ebb4caSwyllys 	}
28599ebb4caSwyllys 
28699ebb4caSwyllys 	/* Check the access permission of the policy DB */
28799ebb4caSwyllys 	if (access(filename, R_OK) < 0) {
28899ebb4caSwyllys 		int err = errno;
28999ebb4caSwyllys 		(void) fprintf(stderr,
29099ebb4caSwyllys 		    gettext("Cannot access \"%s\" for list - %s\n"), filename,
29199ebb4caSwyllys 		    strerror(err));
29299ebb4caSwyllys 		rv = KC_ERR_ACCESS;
29399ebb4caSwyllys 		goto out;
29499ebb4caSwyllys 	}
29599ebb4caSwyllys 
29699ebb4caSwyllys 	rv = load_policies(filename, &plclist);
29799ebb4caSwyllys 	if (rv != KMF_OK) {
29899ebb4caSwyllys 		goto out;
29999ebb4caSwyllys 	}
30099ebb4caSwyllys 
30199ebb4caSwyllys 	pnode = plclist;
30299ebb4caSwyllys 	while (pnode != NULL) {
30399ebb4caSwyllys 		if (policyname == NULL ||
30430a5e8faSwyllys 		    strcmp(policyname, pnode->plc.name) == 0) {
30599ebb4caSwyllys 			KMF_POLICY_RECORD *plc = &pnode->plc;
30699ebb4caSwyllys 
30799ebb4caSwyllys 			found++;
30830a5e8faSwyllys 			rv = kmf_verify_policy(plc);
30999ebb4caSwyllys 			if (rv != KMF_OK) {
31099ebb4caSwyllys 				(void) fprintf(stderr, gettext(
31199ebb4caSwyllys 				    "Policy Name: '%s' is invalid\n"),
31299ebb4caSwyllys 				    plc->name);
31399ebb4caSwyllys 				sanity_err++;
31499ebb4caSwyllys 			} else {
31599ebb4caSwyllys 				show_policy(&pnode->plc);
31699ebb4caSwyllys 			}
31799ebb4caSwyllys 		}
31899ebb4caSwyllys 		pnode = pnode->next;
31999ebb4caSwyllys 	}
32099ebb4caSwyllys 
32199ebb4caSwyllys 	free_policy_list(plclist);
32299ebb4caSwyllys 
32399ebb4caSwyllys 	if (!found) {
32499ebb4caSwyllys 		if (policyname)
32599ebb4caSwyllys 			(void) fprintf(stderr, gettext(
32699ebb4caSwyllys 			    "Cannot find policy '%s'\n"), policyname);
32799ebb4caSwyllys 		else
32899ebb4caSwyllys 			(void) fprintf(stderr, gettext("Cannot find "
32999ebb4caSwyllys 			    "any policies to display\n"));
33099ebb4caSwyllys 		rv = KC_ERR_FIND_POLICY;
33199ebb4caSwyllys 	} else if (sanity_err) {
33299ebb4caSwyllys 		rv = KC_ERR_VERIFY_POLICY;
33399ebb4caSwyllys 	}
33499ebb4caSwyllys 
33599ebb4caSwyllys out:
33699ebb4caSwyllys 
33799ebb4caSwyllys 	if (filename != NULL)
33899ebb4caSwyllys 		free(filename);
33999ebb4caSwyllys 
34099ebb4caSwyllys 	if (policyname != NULL)
34199ebb4caSwyllys 		free(policyname);
34299ebb4caSwyllys 
34399ebb4caSwyllys 	return (rv);
34499ebb4caSwyllys }
345