xref: /illumos-gate/usr/src/lib/libkmf/libkmf/common/generalop.c (revision 9f0bc604621fbb9b9b038e6de7da8f9c46e28608)
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
20*9f0bc604SWyllys Ingersoll  *
21*9f0bc604SWyllys Ingersoll  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
2299ebb4caSwyllys  * Use is subject to license terms.
2399ebb4caSwyllys  *
2499ebb4caSwyllys  */
2599ebb4caSwyllys 
2699ebb4caSwyllys #include <stdio.h>
2799ebb4caSwyllys #include <dlfcn.h>
2899ebb4caSwyllys #include <link.h>
2999ebb4caSwyllys #include <fcntl.h>
3099ebb4caSwyllys #include <ctype.h>
3199ebb4caSwyllys #include <sys/param.h>
3299ebb4caSwyllys #include <sys/types.h>
3399ebb4caSwyllys #include <sys/stat.h>
3499ebb4caSwyllys #include <errno.h>
3599ebb4caSwyllys #include <sys/socket.h>
3699ebb4caSwyllys #include <netinet/in.h>
3799ebb4caSwyllys #include <arpa/inet.h>
3899ebb4caSwyllys #include <thread.h>
3999ebb4caSwyllys 
4099ebb4caSwyllys #include <ber_der.h>
4199ebb4caSwyllys #include <kmfapiP.h>
4299ebb4caSwyllys 
4399ebb4caSwyllys #include <pem_encode.h>
4499ebb4caSwyllys #include <rdn_parser.h>
4599ebb4caSwyllys #include <libxml2/libxml/uri.h>
4699ebb4caSwyllys #include <libgen.h>
4799ebb4caSwyllys #include <cryptoutil.h>
4899ebb4caSwyllys 
4999ebb4caSwyllys static uchar_t pkcs11_initialized = 0;
5099ebb4caSwyllys mutex_t init_lock = DEFAULTMUTEX;
5199ebb4caSwyllys extern int errno;
5299ebb4caSwyllys 
5399ebb4caSwyllys typedef struct {
5499ebb4caSwyllys 	KMF_RETURN code;
5599ebb4caSwyllys 	char	*message;
5699ebb4caSwyllys } kmf_error_map;
5799ebb4caSwyllys 
5899ebb4caSwyllys static kmf_error_map kmf_errcodes[] = {
5999ebb4caSwyllys 	{KMF_OK,	"KMF_OK"},
6099ebb4caSwyllys 	{KMF_ERR_BAD_PARAMETER,	"KMF_ERR_BAD_PARAMETER"},
6199ebb4caSwyllys 	{KMF_ERR_BAD_KEY_FORMAT,	"KMF_ERR_BAD_KEY_FORMAT"},
6299ebb4caSwyllys 	{KMF_ERR_BAD_ALGORITHM,	"KMF_ERR_BAD_ALGORITHM"},
6399ebb4caSwyllys 	{KMF_ERR_MEMORY,	"KMF_ERR_MEMORY"},
6499ebb4caSwyllys 	{KMF_ERR_ENCODING,	"KMF_ERR_ENCODING"},
6599ebb4caSwyllys 	{KMF_ERR_PLUGIN_INIT,	"KMF_ERR_PLUGIN_INIT"},
6699ebb4caSwyllys 	{KMF_ERR_PLUGIN_NOTFOUND,	"KMF_ERR_PLUGIN_NOTFOUND"},
6799ebb4caSwyllys 	{KMF_ERR_INTERNAL,	"KMF_ERR_INTERNAL"},
6899ebb4caSwyllys 	{KMF_ERR_BAD_CERT_FORMAT,	"KMF_ERR_BAD_CERT_FORMAT"},
6999ebb4caSwyllys 	{KMF_ERR_KEYGEN_FAILED,	"KMF_ERR_KEYGEN_FAILED"},
7099ebb4caSwyllys 	{KMF_ERR_UNINITIALIZED,	"KMF_ERR_UNINITIALIZED"},
7199ebb4caSwyllys 	{KMF_ERR_ISSUER,	"KMF_ERR_ISSUER"},
7299ebb4caSwyllys 	{KMF_ERR_NOT_REVOKED,	"KMF_ERR_NOT_REVOKED"},
7399ebb4caSwyllys 	{KMF_ERR_CERT_NOT_FOUND,	"KMF_ERR_CERT_NOT_FOUND"},
7499ebb4caSwyllys 	{KMF_ERR_CRL_NOT_FOUND,	"KMF_ERR_CRL_NOT_FOUND"},
7599ebb4caSwyllys 	{KMF_ERR_RDN_PARSER,	"KMF_ERR_RDN_PARSER"},
7699ebb4caSwyllys 	{KMF_ERR_RDN_ATTR,	"KMF_ERR_RDN_ATTR"},
7799ebb4caSwyllys 	{KMF_ERR_SLOTNAME,	"KMF_ERR_SLOTNAME"},
7899ebb4caSwyllys 	{KMF_ERR_EMPTY_CRL,	"KMF_ERR_EMPTY_CRL"},
7999ebb4caSwyllys 	{KMF_ERR_BUFFER_SIZE,	"KMF_ERR_BUFFER_SIZE"},
8099ebb4caSwyllys 	{KMF_ERR_AUTH_FAILED,	"KMF_ERR_AUTH_FAILED"},
8199ebb4caSwyllys 	{KMF_ERR_TOKEN_SELECTED,	"KMF_ERR_TOKEN_SELECTED"},
8299ebb4caSwyllys 	{KMF_ERR_NO_TOKEN_SELECTED,	"KMF_ERR_NO_TOKEN_SELECTED"},
8399ebb4caSwyllys 	{KMF_ERR_TOKEN_NOT_PRESENT,	"KMF_ERR_TOKEN_NOT_PRESENT"},
8499ebb4caSwyllys 	{KMF_ERR_EXTENSION_NOT_FOUND,	"KMF_ERR_EXTENSION_NOT_FOUND"},
8599ebb4caSwyllys 	{KMF_ERR_POLICY_ENGINE,	"KMF_ERR_POLICY_ENGINE"},
8699ebb4caSwyllys 	{KMF_ERR_POLICY_DB_FORMAT,	"KMF_ERR_POLICY_DB_FORMAT"},
8799ebb4caSwyllys 	{KMF_ERR_POLICY_NOT_FOUND,	"KMF_ERR_POLICY_NOT_FOUND"},
8899ebb4caSwyllys 	{KMF_ERR_POLICY_DB_FILE,	"KMF_ERR_POLICY_DB_FILE"},
8999ebb4caSwyllys 	{KMF_ERR_POLICY_NAME,	"KMF_ERR_POLICY_NAME"},
9099ebb4caSwyllys 	{KMF_ERR_OCSP_POLICY,	"KMF_ERR_OCSP_POLICY"},
9199ebb4caSwyllys 	{KMF_ERR_TA_POLICY,	"KMF_ERR_TA_POLICY"},
9299ebb4caSwyllys 	{KMF_ERR_KEY_NOT_FOUND,	"KMF_ERR_KEY_NOT_FOUND"},
9399ebb4caSwyllys 	{KMF_ERR_OPEN_FILE,	"KMF_ERR_OPEN_FILE"},
9499ebb4caSwyllys 	{KMF_ERR_OCSP_BAD_ISSUER,	"KMF_ERR_OCSP_BAD_ISSUER"},
9599ebb4caSwyllys 	{KMF_ERR_OCSP_BAD_CERT,	"KMF_ERR_OCSP_BAD_CERT"},
9699ebb4caSwyllys 	{KMF_ERR_OCSP_CREATE_REQUEST,	"KMF_ERR_OCSP_CREATE_REQUEST"},
9799ebb4caSwyllys 	{KMF_ERR_CONNECT_SERVER,	"KMF_ERR_CONNECT_SERVER"},
9899ebb4caSwyllys 	{KMF_ERR_SEND_REQUEST,	"KMF_ERR_SEND_REQUEST"},
9999ebb4caSwyllys 	{KMF_ERR_OCSP_CERTID,	"KMF_ERR_OCSP_CERTID"},
10099ebb4caSwyllys 	{KMF_ERR_OCSP_MALFORMED_RESPONSE, "KMF_ERR_OCSP_MALFORMED_RESPONSE"},
10199ebb4caSwyllys 	{KMF_ERR_OCSP_RESPONSE_STATUS,	"KMF_ERR_OCSP_RESPONSE_STATUS"},
10299ebb4caSwyllys 	{KMF_ERR_OCSP_NO_BASIC_RESPONSE, "KMF_ERR_OCSP_NO_BASIC_RESPONSE"},
10399ebb4caSwyllys 	{KMF_ERR_OCSP_BAD_SIGNER,	"KMF_ERR_OCSP_BAD_SIGNER"},
10499ebb4caSwyllys 	{KMF_ERR_OCSP_RESPONSE_SIGNATURE, "KMF_ERR_OCSP_RESPONSE_SIGNATURE"},
10599ebb4caSwyllys 	{KMF_ERR_OCSP_UNKNOWN_CERT,	"KMF_ERR_OCSP_UNKNOWN_CERT"},
10699ebb4caSwyllys 	{KMF_ERR_OCSP_STATUS_TIME_INVALID, "KMF_ERR_OCSP_STATUS_TIME_INVALID"},
10799ebb4caSwyllys 	{KMF_ERR_BAD_HTTP_RESPONSE,	"KMF_ERR_BAD_HTTP_RESPONSE"},
10899ebb4caSwyllys 	{KMF_ERR_RECV_RESPONSE,	"KMF_ERR_RECV_RESPONSE"},
10999ebb4caSwyllys 	{KMF_ERR_RECV_TIMEOUT,	"KMF_ERR_RECV_TIMEOUT"},
11099ebb4caSwyllys 	{KMF_ERR_DUPLICATE_KEYFILE,	"KMF_ERR_DUPLICATE_KEYFILE"},
11199ebb4caSwyllys 	{KMF_ERR_AMBIGUOUS_PATHNAME,	"KMF_ERR_AMBIGUOUS_PATHNAME"},
11299ebb4caSwyllys 	{KMF_ERR_FUNCTION_NOT_FOUND,	"KMF_ERR_FUNCTION_NOT_FOUND"},
11399ebb4caSwyllys 	{KMF_ERR_PKCS12_FORMAT,	"KMF_ERR_PKCS12_FORMAT"},
11499ebb4caSwyllys 	{KMF_ERR_BAD_KEY_TYPE,	"KMF_ERR_BAD_KEY_TYPE"},
11599ebb4caSwyllys 	{KMF_ERR_BAD_KEY_CLASS,	"KMF_ERR_BAD_KEY_CLASS"},
11699ebb4caSwyllys 	{KMF_ERR_BAD_KEY_SIZE,	"KMF_ERR_BAD_KEY_SIZE"},
11799ebb4caSwyllys 	{KMF_ERR_BAD_HEX_STRING,	"KMF_ERR_BAD_HEX_STRING"},
11899ebb4caSwyllys 	{KMF_ERR_KEYUSAGE,	"KMF_ERR_KEYUSAGE"},
11999ebb4caSwyllys 	{KMF_ERR_VALIDITY_PERIOD,	"KMF_ERR_VALIDITY_PERIOD"},
12099ebb4caSwyllys 	{KMF_ERR_OCSP_REVOKED,	"KMF_ERR_OCSP_REVOKED"},
12199ebb4caSwyllys 	{KMF_ERR_CERT_MULTIPLE_FOUND,	"KMF_ERR_CERT_MULTIPLE_FOUND"},
12299ebb4caSwyllys 	{KMF_ERR_WRITE_FILE,	"KMF_ERR_WRITE_FILE"},
12399ebb4caSwyllys 	{KMF_ERR_BAD_URI,	"KMF_ERR_BAD_URI"},
12499ebb4caSwyllys 	{KMF_ERR_BAD_CRLFILE,	"KMF_ERR_BAD_CRLFILE"},
12599ebb4caSwyllys 	{KMF_ERR_BAD_CERTFILE,	"KMF_ERR_BAD_CERTFILE"},
12699ebb4caSwyllys 	{KMF_ERR_GETKEYVALUE_FAILED,	"KMF_ERR_GETKEYVALUE_FAILED"},
12799ebb4caSwyllys 	{KMF_ERR_BAD_KEYHANDLE,	"KMF_ERR_BAD_KEYHANDLE"},
12899ebb4caSwyllys 	{KMF_ERR_BAD_OBJECT_TYPE,	"KMF_ERR_BAD_OBJECT_TYPE"},
12999ebb4caSwyllys 	{KMF_ERR_OCSP_RESPONSE_LIFETIME, "KMF_ERR_OCSP_RESPONSE_LIFETIME"},
13099ebb4caSwyllys 	{KMF_ERR_UNKNOWN_CSR_ATTRIBUTE,	"KMF_ERR_UNKNOWN_CSR_ATTRIBUTE"},
13199ebb4caSwyllys 	{KMF_ERR_UNINITIALIZED_TOKEN,	"KMF_ERR_UNINITIALIZED_TOKEN"},
13299ebb4caSwyllys 	{KMF_ERR_INCOMPLETE_TBS_CERT,	"KMF_ERR_INCOMPLETE_TBS_CERT"},
13399ebb4caSwyllys 	{KMF_ERR_MISSING_ERRCODE,	"KMF_ERR_MISSING_ERRCODE"},
13471593db2Swyllys 	{KMF_KEYSTORE_ALREADY_INITIALIZED, "KMF_KEYSTORE_ALREADY_INITIALIZED"},
13571593db2Swyllys 	{KMF_ERR_SENSITIVE_KEY,		"KMF_ERR_SENSITIVE_KEY"},
13671593db2Swyllys 	{KMF_ERR_UNEXTRACTABLE_KEY,	"KMF_ERR_UNEXTRACTABLE_KEY"},
137d00756ccSwyllys 	{KMF_ERR_KEY_MISMATCH,		"KMF_ERR_KEY_MISMATCH"},
138d00756ccSwyllys 	{KMF_ERR_ATTR_NOT_FOUND,	"KMF_ERR_ATTR_NOT_FOUND"},
139d00756ccSwyllys 	{KMF_ERR_KMF_CONF,		"KMF_ERR_KMF_CONF"}
14099ebb4caSwyllys };
14199ebb4caSwyllys 
14285b65b39Swyllys typedef struct {
14385b65b39Swyllys 	KMF_KEYSTORE_TYPE	kstype;
14485b65b39Swyllys 	char			*path;
14585b65b39Swyllys 	boolean_t		critical;
14685b65b39Swyllys } KMF_PLUGIN_ITEM;
14785b65b39Swyllys 
14885b65b39Swyllys KMF_PLUGIN_ITEM plugin_list[] = {
14985b65b39Swyllys 	{KMF_KEYSTORE_OPENSSL,	KMF_PLUGIN_PATH "kmf_openssl.so.1",  TRUE},
15085b65b39Swyllys 	{KMF_KEYSTORE_PK11TOKEN, KMF_PLUGIN_PATH "kmf_pkcs11.so.1",  TRUE},
15185b65b39Swyllys 	{KMF_KEYSTORE_NSS,	KMF_PLUGIN_PATH "kmf_nss.so.1",  FALSE}
15285b65b39Swyllys };
15399ebb4caSwyllys 
15430a5e8faSwyllys 
15530a5e8faSwyllys 
15690c85bf8Swyllys static KMF_RETURN InitializePlugin(KMF_KEYSTORE_TYPE, char *, KMF_PLUGIN **);
15790c85bf8Swyllys static KMF_RETURN AddPlugin(KMF_HANDLE_T, KMF_PLUGIN *);
15899ebb4caSwyllys static void free_extensions(KMF_X509_EXTENSIONS *extns);
15990c85bf8Swyllys static void DestroyPlugin(KMF_PLUGIN *);
16099ebb4caSwyllys 
161431deaa0Shylee #if defined(__sparcv9)
162431deaa0Shylee #define	ISA_PATH	"/sparcv9"
163431deaa0Shylee #elif defined(__sparc)
164431deaa0Shylee #define	ISA_PATH	"/"
165431deaa0Shylee #elif defined(__i386)
166431deaa0Shylee #define	ISA_PATH	"/"
167431deaa0Shylee #elif defined(__amd64)
168431deaa0Shylee #define	ISA_PATH	"/amd64"
169431deaa0Shylee #endif
170431deaa0Shylee 
171431deaa0Shylee #define	DEFAULT_KEYSTORE_NUM	3
172431deaa0Shylee static int kstore_num = DEFAULT_KEYSTORE_NUM;
173431deaa0Shylee conf_entrylist_t *extra_plugin_list = NULL;
174431deaa0Shylee static boolean_t check_extra_plugin = B_FALSE;
175431deaa0Shylee mutex_t extra_plugin_lock = DEFAULTMUTEX;
176431deaa0Shylee 
1779b37d296Swyllys KMF_RETURN
1789b37d296Swyllys init_pk11()
17999ebb4caSwyllys {
1809b37d296Swyllys 	(void) mutex_lock(&init_lock);
1819b37d296Swyllys 	if (!pkcs11_initialized) {
1829b37d296Swyllys 		CK_RV rv = C_Initialize(NULL);
1839b37d296Swyllys 		if ((rv != CKR_OK) &&
1849b37d296Swyllys 		    (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED)) {
1859b37d296Swyllys 			(void) mutex_unlock(&init_lock);
1869b37d296Swyllys 			return (KMF_ERR_UNINITIALIZED);
1879b37d296Swyllys 		} else {
1889b37d296Swyllys 			pkcs11_initialized = 1;
1899b37d296Swyllys 		}
1909b37d296Swyllys 	}
1919b37d296Swyllys 	(void) mutex_unlock(&init_lock);
1929b37d296Swyllys 	return (KMF_OK);
19399ebb4caSwyllys }
19499ebb4caSwyllys 
19599ebb4caSwyllys /*
19699ebb4caSwyllys  * Private method for searching the plugin list for the correct
19799ebb4caSwyllys  * Plugin to use.
19899ebb4caSwyllys  */
19999ebb4caSwyllys KMF_PLUGIN *
20099ebb4caSwyllys FindPlugin(KMF_HANDLE_T handle, KMF_KEYSTORE_TYPE kstype)
20199ebb4caSwyllys {
20299ebb4caSwyllys 	KMF_PLUGIN_LIST *node;
20390c85bf8Swyllys 	KMF_RETURN ret = KMF_OK;
204431deaa0Shylee 	KMF_PLUGIN *pluginrec = NULL;
20599ebb4caSwyllys 
20699ebb4caSwyllys 	if (handle == NULL)
20799ebb4caSwyllys 		return (NULL);
20899ebb4caSwyllys 
20999ebb4caSwyllys 	node = handle->plugins;
21099ebb4caSwyllys 
21190c85bf8Swyllys 	/* See if the desired plugin was already initialized. */
21299ebb4caSwyllys 	while (node != NULL && node->plugin->type != kstype)
21399ebb4caSwyllys 		node = node->next;
21499ebb4caSwyllys 
215431deaa0Shylee 	if (node != NULL)
216431deaa0Shylee 		return (node->plugin);
217431deaa0Shylee 
218431deaa0Shylee 	/* The plugin was not found, try to initialize it here. */
219431deaa0Shylee 	if (VALID_DEFAULT_KEYSTORE_TYPE(kstype)) {
22090c85bf8Swyllys 		int i;
22190c85bf8Swyllys 		int numitems = sizeof (plugin_list)/sizeof (KMF_PLUGIN_ITEM);
22290c85bf8Swyllys 		for (i = 0; i < numitems; i++) {
22390c85bf8Swyllys 			if (plugin_list[i].kstype == kstype) {
22490c85bf8Swyllys 				ret = InitializePlugin(plugin_list[i].kstype,
22590c85bf8Swyllys 				    plugin_list[i].path, &pluginrec);
22690c85bf8Swyllys 				break;
22790c85bf8Swyllys 			}
22890c85bf8Swyllys 		}
22990c85bf8Swyllys 
230431deaa0Shylee 		goto out;
231431deaa0Shylee 
232431deaa0Shylee 	} else {
233431deaa0Shylee 		/*
234431deaa0Shylee 		 * Not a built-in plugin. Check if it is in the
235431deaa0Shylee 		 * extra_plugin_list.  If it is, try to initialize it here.
236431deaa0Shylee 		 */
237431deaa0Shylee 		conf_entrylist_t *phead = extra_plugin_list;
238431deaa0Shylee 		char realpath[MAXPATHLEN];
239431deaa0Shylee 
240431deaa0Shylee 		while (phead != NULL) {
241431deaa0Shylee 			if (phead->entry->kstype == kstype)
242431deaa0Shylee 				break;
243431deaa0Shylee 			else
244431deaa0Shylee 				phead = phead->next;
245431deaa0Shylee 		}
246431deaa0Shylee 
247431deaa0Shylee 		if (phead == NULL)
24890c85bf8Swyllys 			return (NULL);
24990c85bf8Swyllys 
250431deaa0Shylee 		/*
251431deaa0Shylee 		 * Get the absolute path of the module.
252431deaa0Shylee 		 * - If modulepath is not a full path, then prepend it
253431deaa0Shylee 		 *   with KMF_PLUGIN_PATH.
254431deaa0Shylee 		 * - If modulepath is a full path and contain $ISA, then
255431deaa0Shylee 		 *   subsitute the architecture dependent path.
256431deaa0Shylee 		 */
257431deaa0Shylee 		(void) memset(realpath, 0, sizeof (realpath));
258431deaa0Shylee 		if (strncmp(phead->entry->modulepath, "/", 1) != 0) {
259431deaa0Shylee 			(void) snprintf(realpath, MAXPATHLEN, "%s%s",
260431deaa0Shylee 			    KMF_PLUGIN_PATH, phead->entry->modulepath);
261431deaa0Shylee 		} else {
262431deaa0Shylee 			char *buf = phead->entry->modulepath;
263431deaa0Shylee 			char *isa;
264431deaa0Shylee 
265431deaa0Shylee 			if ((isa = strstr(buf, PKCS11_ISA)) != NULL) {
266431deaa0Shylee 				char *isa_str;
267431deaa0Shylee 
268431deaa0Shylee 				(void) strncpy(realpath, buf, isa - buf);
269431deaa0Shylee 				isa_str = strdup(ISA_PATH);
270431deaa0Shylee 				if (isa_str == NULL) /* not enough memory */
271431deaa0Shylee 					return (NULL);
272431deaa0Shylee 
273431deaa0Shylee 				(void) strncat(realpath, isa_str,
274431deaa0Shylee 				    strlen(isa_str));
275431deaa0Shylee 				free(isa_str);
276431deaa0Shylee 
277431deaa0Shylee 				isa += strlen(PKCS11_ISA);
278431deaa0Shylee 				(void) strlcat(realpath, isa, MAXPATHLEN);
279431deaa0Shylee 			} else {
280431deaa0Shylee 				(void) snprintf(realpath, MAXPATHLEN, "%s",
281431deaa0Shylee 				    phead->entry->modulepath);
282431deaa0Shylee 			}
28390c85bf8Swyllys 		}
284431deaa0Shylee 
285431deaa0Shylee 		ret = InitializePlugin(phead->entry->kstype, realpath,
286431deaa0Shylee 		    &pluginrec);
287431deaa0Shylee 		goto out;
288431deaa0Shylee 	}
289431deaa0Shylee 
290431deaa0Shylee out:
291431deaa0Shylee 	if (ret != KMF_OK || pluginrec == NULL)
292431deaa0Shylee 		/* No matching plugins found in the built-in list */
293431deaa0Shylee 		return (NULL);
294431deaa0Shylee 
295431deaa0Shylee 	ret = AddPlugin(handle, pluginrec);
296431deaa0Shylee 	if (ret != KMF_OK) {
297431deaa0Shylee 		DestroyPlugin(pluginrec);
298431deaa0Shylee 		pluginrec = NULL;
29990c85bf8Swyllys 	}
300431deaa0Shylee 	return (pluginrec);
30199ebb4caSwyllys }
30299ebb4caSwyllys 
303431deaa0Shylee 
30499ebb4caSwyllys static KMF_RETURN
30599ebb4caSwyllys InitializePlugin(KMF_KEYSTORE_TYPE kstype, char *path, KMF_PLUGIN **plugin)
30699ebb4caSwyllys {
30799ebb4caSwyllys 	KMF_PLUGIN *p = NULL;
30899ebb4caSwyllys 	KMF_PLUGIN_FUNCLIST *(*sym)();
30999ebb4caSwyllys 
31099ebb4caSwyllys 	if (path == NULL || plugin == NULL)
31199ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
31299ebb4caSwyllys 
31399ebb4caSwyllys 	*plugin = NULL;
31499ebb4caSwyllys 
31599ebb4caSwyllys 	p = (KMF_PLUGIN *)malloc(sizeof (KMF_PLUGIN));
31699ebb4caSwyllys 	if (p == NULL)
31799ebb4caSwyllys 		return (KMF_ERR_MEMORY);
31899ebb4caSwyllys 
31999ebb4caSwyllys 	p->type = kstype;
32099ebb4caSwyllys 	p->path = strdup(path);
32199ebb4caSwyllys 	if (p->path == NULL) {
32299ebb4caSwyllys 		free(p);
32399ebb4caSwyllys 		return (KMF_ERR_MEMORY);
32499ebb4caSwyllys 	}
3252225707cSValerie Bubb Fenwick 	/*
3262225707cSValerie Bubb Fenwick 	 * Do not use RTLD_GROUP here, or this will cause a circular
3272225707cSValerie Bubb Fenwick 	 * dependency when kmf_pkcs11.so.1 gets its PKCS#11 functions
3282225707cSValerie Bubb Fenwick 	 * from libpkcs11.so.1 when kmf is used via libelfsign.so.1
3292225707cSValerie Bubb Fenwick 	 * called from kcfd.
3302225707cSValerie Bubb Fenwick 	 */
3312225707cSValerie Bubb Fenwick 	p->dldesc = dlopen(path, RTLD_LAZY | RTLD_PARENT);
33299ebb4caSwyllys 	if (p->dldesc == NULL) {
33399ebb4caSwyllys 		free(p->path);
33499ebb4caSwyllys 		free(p);
33599ebb4caSwyllys 		return (KMF_ERR_PLUGIN_INIT);
33699ebb4caSwyllys 	}
33799ebb4caSwyllys 
33899ebb4caSwyllys 	sym = (KMF_PLUGIN_FUNCLIST *(*)())dlsym(p->dldesc,
33930a5e8faSwyllys 	    KMF_PLUGIN_INIT_SYMBOL);
34099ebb4caSwyllys 	if (sym == NULL) {
34199ebb4caSwyllys 		(void) dlclose(p->dldesc);
34299ebb4caSwyllys 		free(p->path);
34399ebb4caSwyllys 		free(p);
34499ebb4caSwyllys 		return (KMF_ERR_PLUGIN_INIT);
34599ebb4caSwyllys 	}
34699ebb4caSwyllys 
34799ebb4caSwyllys 	/* Get the function list */
34899ebb4caSwyllys 	if ((p->funclist = (*sym)()) == NULL) {
34999ebb4caSwyllys 		(void) dlclose(p->dldesc);
35099ebb4caSwyllys 		free(p->path);
35199ebb4caSwyllys 		free(p);
35299ebb4caSwyllys 		return (KMF_ERR_PLUGIN_INIT);
35399ebb4caSwyllys 	}
35499ebb4caSwyllys 
35599ebb4caSwyllys 	*plugin = p;
35699ebb4caSwyllys 
35799ebb4caSwyllys 	return (KMF_OK);
35899ebb4caSwyllys }
35999ebb4caSwyllys 
36099ebb4caSwyllys static KMF_RETURN
36199ebb4caSwyllys AddPlugin(KMF_HANDLE_T handle, KMF_PLUGIN *plugin)
36299ebb4caSwyllys {
36399ebb4caSwyllys 	KMF_PLUGIN_LIST *n;
36499ebb4caSwyllys 
36599ebb4caSwyllys 	if (handle == NULL || plugin == NULL)
36699ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
36799ebb4caSwyllys 
36899ebb4caSwyllys 	/* If the head is NULL, create it */
36999ebb4caSwyllys 	if (handle->plugins == NULL) {
37099ebb4caSwyllys 		handle->plugins = (KMF_PLUGIN_LIST *)malloc(
37130a5e8faSwyllys 		    sizeof (KMF_PLUGIN_LIST));
37299ebb4caSwyllys 		if (handle->plugins == NULL)
37399ebb4caSwyllys 			return (KMF_ERR_MEMORY);
37499ebb4caSwyllys 		handle->plugins->plugin = plugin;
37599ebb4caSwyllys 		handle->plugins->next = NULL;
37699ebb4caSwyllys 	} else {
37799ebb4caSwyllys 		/* walk the list to find the tail */
37899ebb4caSwyllys 		n = handle->plugins;
37999ebb4caSwyllys 		while (n->next != NULL)
38099ebb4caSwyllys 			n = n->next;
38199ebb4caSwyllys 		n->next = (KMF_PLUGIN_LIST *)malloc(sizeof (KMF_PLUGIN_LIST));
38299ebb4caSwyllys 		if (n->next == NULL)
38399ebb4caSwyllys 			return (KMF_ERR_MEMORY);
38499ebb4caSwyllys 
38599ebb4caSwyllys 		n->next->plugin = plugin;
38699ebb4caSwyllys 		n->next->next = NULL;
38799ebb4caSwyllys 	}
38899ebb4caSwyllys 	return (0);
38999ebb4caSwyllys }
39099ebb4caSwyllys 
39199ebb4caSwyllys static void
39299ebb4caSwyllys DestroyPlugin(KMF_PLUGIN *plugin)
39399ebb4caSwyllys {
39499ebb4caSwyllys 	if (plugin) {
39599ebb4caSwyllys 		if (plugin->path)
39699ebb4caSwyllys 			free(plugin->path);
39799ebb4caSwyllys 		free(plugin);
39899ebb4caSwyllys 	}
39999ebb4caSwyllys }
40099ebb4caSwyllys 
40199ebb4caSwyllys static void
40299ebb4caSwyllys Cleanup_KMF_Handle(KMF_HANDLE_T handle)
40399ebb4caSwyllys {
40499ebb4caSwyllys 	if (handle != NULL) {
40599ebb4caSwyllys 		while (handle->plugins != NULL) {
40699ebb4caSwyllys 			KMF_PLUGIN_LIST *next = handle->plugins->next;
40799ebb4caSwyllys 
40899ebb4caSwyllys 			DestroyPlugin(handle->plugins->plugin);
40999ebb4caSwyllys 			free(handle->plugins);
41099ebb4caSwyllys 			handle->plugins = next;
41199ebb4caSwyllys 		}
41230a5e8faSwyllys 		kmf_free_policy_record(handle->policy);
41399ebb4caSwyllys 		free(handle->policy);
41499ebb4caSwyllys 	}
41599ebb4caSwyllys 	free(handle);
41699ebb4caSwyllys }
41799ebb4caSwyllys 
41899ebb4caSwyllys void
41999ebb4caSwyllys Cleanup_PK11_Session(KMF_HANDLE_T handle)
42099ebb4caSwyllys {
42199ebb4caSwyllys 	if (handle != NULL) {
42299ebb4caSwyllys 		/* Close active session on a pkcs11 token */
42399ebb4caSwyllys 		if (handle->pk11handle != NULL) {
42499ebb4caSwyllys 			(void) C_CloseSession(handle->pk11handle);
42599ebb4caSwyllys 			handle->pk11handle = NULL;
42699ebb4caSwyllys 		}
42799ebb4caSwyllys 	}
42899ebb4caSwyllys }
42999ebb4caSwyllys 
43099ebb4caSwyllys KMF_RETURN
43130a5e8faSwyllys kmf_initialize(KMF_HANDLE_T *outhandle, char *policyfile, char *policyname)
43299ebb4caSwyllys {
43399ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
43499ebb4caSwyllys 	KMF_HANDLE *handle = NULL;
43599ebb4caSwyllys 
43699ebb4caSwyllys 	if (outhandle == NULL)
43799ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
43899ebb4caSwyllys 
43999ebb4caSwyllys 	*outhandle = NULL;
44099ebb4caSwyllys 	handle = (KMF_HANDLE *)malloc(sizeof (KMF_HANDLE));
44199ebb4caSwyllys 	if (handle == NULL)
44299ebb4caSwyllys 		return (KMF_ERR_MEMORY);
44399ebb4caSwyllys 
44499ebb4caSwyllys 	(void) memset(handle, 0, sizeof (KMF_HANDLE));
44599ebb4caSwyllys 	handle->plugins = NULL;
44699ebb4caSwyllys 
447431deaa0Shylee 	/*
448431deaa0Shylee 	 * When this function is called the first time, get the additional
449431deaa0Shylee 	 * plugins from the config file.
450431deaa0Shylee 	 */
451431deaa0Shylee 	(void) mutex_lock(&extra_plugin_lock);
452431deaa0Shylee 	if (!check_extra_plugin) {
453431deaa0Shylee 
454431deaa0Shylee 		ret = get_entrylist(&extra_plugin_list);
455431deaa0Shylee 		check_extra_plugin = B_TRUE;
456431deaa0Shylee 
457431deaa0Shylee 		/*
458431deaa0Shylee 		 * Assign the kstype number to the additional plugins here.
459431deaa0Shylee 		 * The global kstore_num will be protected by the mutex lock.
460431deaa0Shylee 		 */
461431deaa0Shylee 		if (ret == KMF_OK) {
462431deaa0Shylee 			conf_entrylist_t *phead = extra_plugin_list;
463431deaa0Shylee 			while (phead != NULL) {
464431deaa0Shylee 				phead->entry->kstype = ++kstore_num;
465431deaa0Shylee 				phead = phead->next;
466431deaa0Shylee 			}
467431deaa0Shylee 		}
468431deaa0Shylee 
469431deaa0Shylee 		/*
470431deaa0Shylee 		 * If the KMF configuration file does not exist or cannot be
471431deaa0Shylee 		 * parsed correctly, we will give a warning in syslog and
472431deaa0Shylee 		 * continue on as there is no extra plugins in the system.
473431deaa0Shylee 		 */
474431deaa0Shylee 		if (ret == KMF_ERR_KMF_CONF) {
475431deaa0Shylee 			cryptoerror(LOG_WARNING, "KMF was unable to parse "
476431deaa0Shylee 			    "the private KMF config file.\n");
477431deaa0Shylee 			ret = KMF_OK;
478431deaa0Shylee 		}
479431deaa0Shylee 
480431deaa0Shylee 		if (ret != KMF_OK) {
481431deaa0Shylee 			(void) mutex_unlock(&extra_plugin_lock);
482431deaa0Shylee 			goto errout;
483431deaa0Shylee 		}
484431deaa0Shylee 	}
485431deaa0Shylee 	(void) mutex_unlock(&extra_plugin_lock);
486431deaa0Shylee 
48799ebb4caSwyllys 	/* Initialize the handle with the policy */
48830a5e8faSwyllys 	ret = kmf_set_policy((void *)handle,
48999ebb4caSwyllys 	    policyfile == NULL ? KMF_DEFAULT_POLICY_FILE : policyfile,
49099ebb4caSwyllys 	    policyname == NULL ? KMF_DEFAULT_POLICY_NAME : policyname);
49199ebb4caSwyllys 	if (ret != KMF_OK)
49299ebb4caSwyllys 		goto errout;
49399ebb4caSwyllys 
49499ebb4caSwyllys 	CLEAR_ERROR(handle, ret);
49599ebb4caSwyllys errout:
49699ebb4caSwyllys 	if (ret != KMF_OK) {
49799ebb4caSwyllys 		Cleanup_KMF_Handle(handle);
49899ebb4caSwyllys 		handle = NULL;
49999ebb4caSwyllys 	}
50099ebb4caSwyllys 
50199ebb4caSwyllys 	*outhandle = (KMF_HANDLE_T)handle;
50299ebb4caSwyllys 	return (ret);
50399ebb4caSwyllys }
50499ebb4caSwyllys 
50599ebb4caSwyllys KMF_RETURN
50630a5e8faSwyllys kmf_configure_keystore(KMF_HANDLE_T handle,
50730a5e8faSwyllys 	int	num_args,
50830a5e8faSwyllys 	KMF_ATTRIBUTE	*attrlist)
50999ebb4caSwyllys {
51030a5e8faSwyllys 	KMF_RETURN ret = KMF_OK;
51199ebb4caSwyllys 	KMF_PLUGIN *plugin;
51230a5e8faSwyllys 	KMF_KEYSTORE_TYPE kstype;
51330a5e8faSwyllys 	uint32_t len;
51430a5e8faSwyllys 
51530a5e8faSwyllys 	KMF_ATTRIBUTE_TESTER required_attrs[] = {
51630a5e8faSwyllys 		{KMF_KEYSTORE_TYPE_ATTR, FALSE, 1, sizeof (KMF_KEYSTORE_TYPE)},
51730a5e8faSwyllys 	};
51830a5e8faSwyllys 
51930a5e8faSwyllys 	int num_req_attrs = sizeof (required_attrs) /
52030a5e8faSwyllys 	    sizeof (KMF_ATTRIBUTE_TESTER);
52130a5e8faSwyllys 
52230a5e8faSwyllys 	if (handle == NULL)
52330a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
52499ebb4caSwyllys 
52599ebb4caSwyllys 	CLEAR_ERROR(handle, ret);
52630a5e8faSwyllys 
52730a5e8faSwyllys 	ret = test_attributes(num_req_attrs, required_attrs,
52830a5e8faSwyllys 	    0, NULL, num_args, attrlist);
52930a5e8faSwyllys 
53099ebb4caSwyllys 	if (ret != KMF_OK)
53199ebb4caSwyllys 		return (ret);
53299ebb4caSwyllys 
53330a5e8faSwyllys 	len = sizeof (kstype);
53430a5e8faSwyllys 	ret = kmf_get_attr(KMF_KEYSTORE_TYPE_ATTR, attrlist, num_args,
53530a5e8faSwyllys 	    &kstype, &len);
53630a5e8faSwyllys 	if (ret != KMF_OK)
53730a5e8faSwyllys 		return (ret);
53899ebb4caSwyllys 
53930a5e8faSwyllys 	plugin = FindPlugin(handle, kstype);
54030a5e8faSwyllys 	if (plugin != NULL && plugin->funclist->ConfigureKeystore != NULL) {
54130a5e8faSwyllys 		return (plugin->funclist->ConfigureKeystore(handle, num_args,
54230a5e8faSwyllys 		    attrlist));
54330a5e8faSwyllys 	} else {
54499ebb4caSwyllys 		/* return KMF_OK, if the plugin does not have an entry */
54599ebb4caSwyllys 		return (KMF_OK);
54630a5e8faSwyllys 	}
54799ebb4caSwyllys }
54899ebb4caSwyllys 
54999ebb4caSwyllys KMF_RETURN
55030a5e8faSwyllys kmf_finalize(KMF_HANDLE_T handle)
55199ebb4caSwyllys {
55299ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
55399ebb4caSwyllys 
55499ebb4caSwyllys 	CLEAR_ERROR(handle, ret);
55599ebb4caSwyllys 	if (ret != KMF_OK)
55699ebb4caSwyllys 		return (ret);
55799ebb4caSwyllys 
55899ebb4caSwyllys 	if (pkcs11_initialized) {
55999ebb4caSwyllys 		Cleanup_PK11_Session(handle);
56099ebb4caSwyllys 	}
56199ebb4caSwyllys 	Cleanup_KMF_Handle(handle);
56299ebb4caSwyllys 
56399ebb4caSwyllys 	return (ret);
56499ebb4caSwyllys }
56599ebb4caSwyllys 
56699ebb4caSwyllys KMF_RETURN
56730a5e8faSwyllys kmf_get_kmf_error_str(KMF_RETURN errcode, char **errmsg)
56899ebb4caSwyllys {
56999ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
57099ebb4caSwyllys 	int i, maxerr;
57199ebb4caSwyllys 
57299ebb4caSwyllys 	if (errmsg == NULL)
57399ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
57499ebb4caSwyllys 
57599ebb4caSwyllys 	*errmsg = NULL;
57699ebb4caSwyllys 	maxerr = sizeof (kmf_errcodes) / sizeof (kmf_error_map);
57799ebb4caSwyllys 
57830a5e8faSwyllys 	for (i = 0; i < maxerr && errcode != kmf_errcodes[i].code; i++)
57930a5e8faSwyllys 		/* empty body */
58030a5e8faSwyllys 		;
58199ebb4caSwyllys 
58299ebb4caSwyllys 	if (i == maxerr)
58399ebb4caSwyllys 		return (KMF_ERR_MISSING_ERRCODE);
58499ebb4caSwyllys 	else {
58599ebb4caSwyllys 		*errmsg = strdup(kmf_errcodes[i].message);
58699ebb4caSwyllys 		if ((*errmsg) == NULL)
58799ebb4caSwyllys 			return (KMF_ERR_MEMORY);
58899ebb4caSwyllys 	}
58999ebb4caSwyllys 	return (ret);
59099ebb4caSwyllys }
59199ebb4caSwyllys 
59299ebb4caSwyllys KMF_RETURN
59330a5e8faSwyllys kmf_get_plugin_error_str(KMF_HANDLE_T handle, char **msgstr)
59499ebb4caSwyllys {
59599ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
59699ebb4caSwyllys 	KMF_PLUGIN *plugin;
59799ebb4caSwyllys 
59899ebb4caSwyllys 	if (handle == NULL || msgstr == NULL)
59999ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
60099ebb4caSwyllys 
60199ebb4caSwyllys 	*msgstr = NULL;
60299ebb4caSwyllys 
60399ebb4caSwyllys 	if (handle->lasterr.errcode == 0) {
60499ebb4caSwyllys 		return (KMF_ERR_MISSING_ERRCODE);
60599ebb4caSwyllys 	}
60699ebb4caSwyllys 
60799ebb4caSwyllys 	if (handle->lasterr.kstype == -1) { /* System error */
60899ebb4caSwyllys 		char *str = strerror(handle->lasterr.errcode);
60999ebb4caSwyllys 		if (str != NULL) {
61099ebb4caSwyllys 			*msgstr = strdup(str);
61199ebb4caSwyllys 			if ((*msgstr) == NULL)
61299ebb4caSwyllys 				return (KMF_ERR_MEMORY);
61399ebb4caSwyllys 		}
61499ebb4caSwyllys 		return (KMF_OK);
61599ebb4caSwyllys 	}
61699ebb4caSwyllys 
61799ebb4caSwyllys 	plugin = FindPlugin(handle, handle->lasterr.kstype);
61899ebb4caSwyllys 	if (plugin == NULL)
61999ebb4caSwyllys 		return (KMF_ERR_PLUGIN_NOTFOUND);
62099ebb4caSwyllys 
62199ebb4caSwyllys 	if (plugin->funclist->GetErrorString != NULL) {
62299ebb4caSwyllys 		ret = plugin->funclist->GetErrorString(handle, msgstr);
62399ebb4caSwyllys 	} else {
62499ebb4caSwyllys 		return (KMF_ERR_FUNCTION_NOT_FOUND);
62599ebb4caSwyllys 	}
62699ebb4caSwyllys 
62799ebb4caSwyllys 	return (ret);
62899ebb4caSwyllys }
62999ebb4caSwyllys 
63099ebb4caSwyllys 
6319b37d296Swyllys #define	SET_SYS_ERROR(h, c) if (h) {\
6329b37d296Swyllys 	h->lasterr.kstype = -1;\
6339b37d296Swyllys 	h->lasterr.errcode = c;\
6349b37d296Swyllys }
63599ebb4caSwyllys 
63699ebb4caSwyllys KMF_RETURN
63730a5e8faSwyllys kmf_read_input_file(KMF_HANDLE_T handle, char *filename,  KMF_DATA *pdata)
63899ebb4caSwyllys {
63999ebb4caSwyllys 	struct stat s;
64099ebb4caSwyllys 	long nread, total = 0;
64199ebb4caSwyllys 	int fd;
64299ebb4caSwyllys 	unsigned char *buf = NULL;
64399ebb4caSwyllys 	KMF_RETURN ret;
64499ebb4caSwyllys 
6459b37d296Swyllys 	if (handle) {
6469b37d296Swyllys 		CLEAR_ERROR(handle, ret);
6479b37d296Swyllys 		if (ret != KMF_OK)
6489b37d296Swyllys 			return (ret);
6499b37d296Swyllys 	}
65099ebb4caSwyllys 
65199ebb4caSwyllys 	if (filename == NULL || pdata == NULL) {
65299ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
65399ebb4caSwyllys 	}
65499ebb4caSwyllys 
65599ebb4caSwyllys 	if ((fd = open(filename, O_RDONLY)) < 0) {
65699ebb4caSwyllys 		SET_SYS_ERROR(handle, errno);
65799ebb4caSwyllys 		return (KMF_ERR_OPEN_FILE);
65899ebb4caSwyllys 	}
65999ebb4caSwyllys 
66099ebb4caSwyllys 	if (fstat(fd, &s) < 0) {
66199ebb4caSwyllys 		SET_SYS_ERROR(handle, errno);
66299ebb4caSwyllys 		(void) close(fd);
66399ebb4caSwyllys 		return (KMF_ERR_OPEN_FILE);
66499ebb4caSwyllys 	}
66599ebb4caSwyllys 
66699ebb4caSwyllys 	if ((buf = (unsigned char *) malloc(s.st_size)) == NULL) {
66799ebb4caSwyllys 		(void) close(fd);
66899ebb4caSwyllys 		return (KMF_ERR_MEMORY);
66999ebb4caSwyllys 	}
67099ebb4caSwyllys 
67199ebb4caSwyllys 	do {
67299ebb4caSwyllys 		nread = read(fd, buf+total, s.st_size-total);
67399ebb4caSwyllys 		if (nread < 0) {
67499ebb4caSwyllys 			SET_SYS_ERROR(handle, errno);
67599ebb4caSwyllys 			(void) close(fd);
67699ebb4caSwyllys 			free(buf);
67799ebb4caSwyllys 			return (KMF_ERR_INTERNAL);
67899ebb4caSwyllys 		}
67999ebb4caSwyllys 		total += nread;
68099ebb4caSwyllys 	} while (total < s.st_size);
68199ebb4caSwyllys 
68299ebb4caSwyllys 	pdata->Data = buf;
68399ebb4caSwyllys 	pdata->Length = s.st_size;
68499ebb4caSwyllys 	(void) close(fd);
68599ebb4caSwyllys 	return (KMF_OK);
68699ebb4caSwyllys }
68799ebb4caSwyllys 
68899ebb4caSwyllys /*
68999ebb4caSwyllys  *
69030a5e8faSwyllys  * Name: kmf_der_to_pem
69199ebb4caSwyllys  *
69299ebb4caSwyllys  * Description:
69399ebb4caSwyllys  *   Function for converting DER encoded format to PEM encoded format
69499ebb4caSwyllys  *
69599ebb4caSwyllys  * Parameters:
69699ebb4caSwyllys  *   type(input) - CERTIFICATE or CSR
69799ebb4caSwyllys  *   data(input) - pointer to the DER encoded data
69899ebb4caSwyllys  *   len(input)  - length of input data
69999ebb4caSwyllys  *   out(output) - contains the output buffer address to be returned
70099ebb4caSwyllys  *   outlen(output) - pointer to the returned output length
70199ebb4caSwyllys  *
70299ebb4caSwyllys  * Returns:
70399ebb4caSwyllys  *   A KMF_RETURN value indicating success or specifying a particular
70499ebb4caSwyllys  * error condition.
70599ebb4caSwyllys  *   The value KMF_OK indicates success. All other values represent
70699ebb4caSwyllys  * an error condition.
70799ebb4caSwyllys  *
70899ebb4caSwyllys  */
70999ebb4caSwyllys KMF_RETURN
71030a5e8faSwyllys kmf_der_to_pem(KMF_OBJECT_TYPE type, unsigned char *data,
71199ebb4caSwyllys 	int len, unsigned char **out, int *outlen)
71299ebb4caSwyllys {
71399ebb4caSwyllys 
71499ebb4caSwyllys 	KMF_RETURN err;
71599ebb4caSwyllys 	if (data == NULL || out == NULL || outlen == NULL)
71699ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
71799ebb4caSwyllys 
71899ebb4caSwyllys 	err = Der2Pem(type, data, len, out, outlen);
71999ebb4caSwyllys 	return (err);
72099ebb4caSwyllys 
72199ebb4caSwyllys }
72299ebb4caSwyllys 
72399ebb4caSwyllys /*
72499ebb4caSwyllys  *
72530a5e8faSwyllys  * Name: kmf_pem_to_der
72699ebb4caSwyllys  *
72799ebb4caSwyllys  * Description:
72899ebb4caSwyllys  *   Function for converting PEM encoded format to DER encoded format
72999ebb4caSwyllys  *
73099ebb4caSwyllys  * Parameters:
73199ebb4caSwyllys  *   in(input) - pointer to the PEM encoded data
73299ebb4caSwyllys  *   inlen(input)  - length of input data
73399ebb4caSwyllys  *   out(output) - contains the output buffer address to be returned
73499ebb4caSwyllys  *   outlen(output) - pointer to the returned output length
73599ebb4caSwyllys  *
73699ebb4caSwyllys  * Returns:
73799ebb4caSwyllys  *   A KMF_RETURN value indicating success or specifying a particular
73899ebb4caSwyllys  *   error condition.
73999ebb4caSwyllys  *   The value KMF_OK indicates success. All other values represent
74099ebb4caSwyllys  *   an error condition.
74199ebb4caSwyllys  *
74299ebb4caSwyllys  */
74399ebb4caSwyllys KMF_RETURN
74430a5e8faSwyllys kmf_pem_to_der(unsigned char *in, int inlen,
74599ebb4caSwyllys 	unsigned char **out, int *outlen)
74699ebb4caSwyllys {
74799ebb4caSwyllys 	KMF_RETURN err;
74899ebb4caSwyllys 	if (in == NULL || out == NULL || outlen == NULL)
74999ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
75099ebb4caSwyllys 
75199ebb4caSwyllys 	err = Pem2Der(in, inlen, out, outlen);
75299ebb4caSwyllys 	return (err);
75399ebb4caSwyllys }
75499ebb4caSwyllys 
75599ebb4caSwyllys char *
75630a5e8faSwyllys kmf_oid_to_string(KMF_OID *oid)
75799ebb4caSwyllys {
75899ebb4caSwyllys 	char numstr[128];
75999ebb4caSwyllys 	uint32_t number;
76099ebb4caSwyllys 	int numshift;
76199ebb4caSwyllys 	uint32_t i, string_length;
76299ebb4caSwyllys 	uchar_t *cp;
76399ebb4caSwyllys 	char *bp;
76499ebb4caSwyllys 
76599ebb4caSwyllys 	/* First determine the size of the string */
76699ebb4caSwyllys 	string_length = 0;
76799ebb4caSwyllys 	number = 0;
76899ebb4caSwyllys 	numshift = 0;
76999ebb4caSwyllys 	cp = (unsigned char *)oid->Data;
77099ebb4caSwyllys 
77199ebb4caSwyllys 	number = (uint32_t)cp[0];
77299ebb4caSwyllys 	(void) sprintf(numstr, "%d ", number/40);
77399ebb4caSwyllys 
77499ebb4caSwyllys 	string_length += strlen(numstr);
77599ebb4caSwyllys 	(void) sprintf(numstr, "%d ", number%40);
77699ebb4caSwyllys 
77799ebb4caSwyllys 	string_length += strlen(numstr);
77899ebb4caSwyllys 
77999ebb4caSwyllys 	for (i = 1; i < oid->Length; i++) {
78099ebb4caSwyllys 		if ((uint32_t)(numshift+7) < (sizeof (uint32_t)*8)) {
78199ebb4caSwyllys 			number = (number << 7) | (cp[i] & 0x7f);
78299ebb4caSwyllys 			numshift += 7;
78399ebb4caSwyllys 		} else {
78499ebb4caSwyllys 			return (NULL);
78599ebb4caSwyllys 		}
78699ebb4caSwyllys 
78799ebb4caSwyllys 		if ((cp[i] & 0x80) == 0) {
78899ebb4caSwyllys 			(void) sprintf(numstr, "%d ", number);
78999ebb4caSwyllys 			string_length += strlen(numstr);
79099ebb4caSwyllys 			number = 0;
79199ebb4caSwyllys 			numshift = 0;
79299ebb4caSwyllys 		}
79399ebb4caSwyllys 	}
79499ebb4caSwyllys 	/*
79599ebb4caSwyllys 	 * If we get here, we've calculated the length of "n n n ... n ".  Add 4
79699ebb4caSwyllys 	 * here for "{ " and "}\0".
79799ebb4caSwyllys 	 */
79899ebb4caSwyllys 	string_length += 4;
79999ebb4caSwyllys 	if ((bp = (char *)malloc(string_length))) {
80099ebb4caSwyllys 		number = (uint32_t)cp[0];
80199ebb4caSwyllys 
80299ebb4caSwyllys 		(void) sprintf(numstr, "%d.", number/40);
80399ebb4caSwyllys 		(void) strcpy(bp, numstr);
80499ebb4caSwyllys 
80599ebb4caSwyllys 		(void) sprintf(numstr, "%d.", number%40);
80699ebb4caSwyllys 		(void) strcat(bp, numstr);
80799ebb4caSwyllys 
80899ebb4caSwyllys 		number = 0;
80999ebb4caSwyllys 		cp = (unsigned char *) oid->Data;
81099ebb4caSwyllys 		for (i = 1; i < oid->Length; i++) {
81199ebb4caSwyllys 			number = (number << 7) | (cp[i] & 0x7f);
81299ebb4caSwyllys 			if ((cp[i] & 0x80) == 0) {
81399ebb4caSwyllys 				(void) sprintf(numstr, "%d", number);
81499ebb4caSwyllys 				(void) strcat(bp, numstr);
81599ebb4caSwyllys 				number = 0;
81699ebb4caSwyllys 				if (i+1 < oid->Length)
81799ebb4caSwyllys 					(void) strcat(bp, ".");
81899ebb4caSwyllys 			}
81999ebb4caSwyllys 		}
82099ebb4caSwyllys 	}
82199ebb4caSwyllys 	return (bp);
82299ebb4caSwyllys }
82399ebb4caSwyllys 
82402744e81Swyllys static boolean_t
82508ec4bd3Shylee check_for_pem(uchar_t *buf, KMF_ENCODE_FORMAT *fmt)
82602744e81Swyllys {
8279b37d296Swyllys 	char *p;
828d00756ccSwyllys 	int i;
82902744e81Swyllys 
83008ec4bd3Shylee 	if (buf == NULL)
83102744e81Swyllys 		return (FALSE);
83202744e81Swyllys 
833d00756ccSwyllys 	for (i = 0; i < 8 && isascii(buf[i]); i++)
834d00756ccSwyllys 		/* loop to make sure this is ascii */;
835d00756ccSwyllys 	if (i != 8)
836d00756ccSwyllys 		return (FALSE);
837d00756ccSwyllys 
83808ec4bd3Shylee 	if (memcmp(buf, "Bag Attr", 8) == 0) {
83908ec4bd3Shylee 		*fmt = KMF_FORMAT_PEM_KEYPAIR;
84008ec4bd3Shylee 		return (TRUE);
84108ec4bd3Shylee 	}
84208ec4bd3Shylee 
8439b37d296Swyllys 	/* Look for "-----BEGIN" right after a newline */
84408ec4bd3Shylee 	p = strtok((char *)buf, "\n");
8459b37d296Swyllys 	while (p != NULL) {
8469b37d296Swyllys 		if (strstr(p, "-----BEGIN") != NULL) {
84708ec4bd3Shylee 			*fmt = KMF_FORMAT_PEM;
848d00756ccSwyllys 			/* Restore the buffer */
849d00756ccSwyllys 			buf[strlen(p)] = '\n';
8509b37d296Swyllys 			return (TRUE);
85102744e81Swyllys 		}
852d00756ccSwyllys 		buf[strlen(p)] = '\n';
8539b37d296Swyllys 		p = strtok(NULL, "\n");
85402744e81Swyllys 	}
85502744e81Swyllys 	return (FALSE);
85602744e81Swyllys }
85702744e81Swyllys 
85808ec4bd3Shylee 
85908ec4bd3Shylee static unsigned char pkcs12_version[3] = {0x02, 0x01, 0x03};
86008ec4bd3Shylee static unsigned char pkcs12_oid[11] =
86108ec4bd3Shylee {0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01};
86208ec4bd3Shylee 
86308ec4bd3Shylee /*
86408ec4bd3Shylee  * This function takes a BER encoded string as input and checks the version
86508ec4bd3Shylee  * and the oid in the the top-level ASN.1 structure to see if it complies to
86608ec4bd3Shylee  * the PKCS#12 Syntax.
86708ec4bd3Shylee  */
86808ec4bd3Shylee static boolean_t
86908ec4bd3Shylee check_for_pkcs12(uchar_t *buf, int buf_len)
87008ec4bd3Shylee {
87108ec4bd3Shylee 	int index = 0;
87208ec4bd3Shylee 	int length_octets;
87308ec4bd3Shylee 
87408ec4bd3Shylee 	if (buf == NULL || buf_len <= 0)
87508ec4bd3Shylee 		return (FALSE);
87608ec4bd3Shylee 
87708ec4bd3Shylee 	/*
87808ec4bd3Shylee 	 * The top level structure for a PKCS12 string:
87908ec4bd3Shylee 	 *
88008ec4bd3Shylee 	 * PFX ::= SEQUENCE {
88108ec4bd3Shylee 	 *	version		INTEGER {v3(3)}(v3,...)
88208ec4bd3Shylee 	 *	authSafe	ContentInfo
88308ec4bd3Shylee 	 *	macData		MacData OPTIONAL
88408ec4bd3Shylee 	 * }
88508ec4bd3Shylee 	 *
88608ec4bd3Shylee 	 * ContentInfo
88708ec4bd3Shylee 	 *	FROM PKCS-7 {iso(1) member-body(2) us(840) rsadsi(113549)
88808ec4bd3Shylee 	 *		pkcs(1) pkcs-7(7) modules(0) pkcs-7(1)}
88908ec4bd3Shylee 	 *
89008ec4bd3Shylee 	 * Therefore, the BER/DER dump of a PKCS#12 file for the first 2
89108ec4bd3Shylee 	 * sequences up to the oid part is as following:
89208ec4bd3Shylee 	 *
89308ec4bd3Shylee 	 *	SEQUENCE {
89408ec4bd3Shylee 	 *	    INTEGER 3
89508ec4bd3Shylee 	 *	    SEQUENCE {
89608ec4bd3Shylee 	 *		OBJECT IDENTIFIER data (1 2 840 113549 1 7 1)
89708ec4bd3Shylee 	 */
89808ec4bd3Shylee 
89908ec4bd3Shylee 	/*
90008ec4bd3Shylee 	 * Check the first sequence and calculate the number of bytes used
90108ec4bd3Shylee 	 * to store the length.
90208ec4bd3Shylee 	 */
90308ec4bd3Shylee 	if (buf[index++] != 0x30)
90408ec4bd3Shylee 		return (FALSE);
90508ec4bd3Shylee 
90608ec4bd3Shylee 	if (buf[index] & 0x80) {
90708ec4bd3Shylee 		length_octets = buf[index++] & 0x0F;  /* long form */
90808ec4bd3Shylee 	} else {
90908ec4bd3Shylee 		length_octets = 1; /* short form */
91008ec4bd3Shylee 	}
91108ec4bd3Shylee 
91208ec4bd3Shylee 	index += length_octets;
91308ec4bd3Shylee 	if (index  >= buf_len)
91408ec4bd3Shylee 		return (FALSE);
91508ec4bd3Shylee 
91608ec4bd3Shylee 	/* Skip the length octets and check the pkcs12 version */
91708ec4bd3Shylee 	if (memcmp(buf + index, pkcs12_version, sizeof (pkcs12_version)) != 0)
91808ec4bd3Shylee 		return (FALSE);
91908ec4bd3Shylee 
92008ec4bd3Shylee 	index += sizeof (pkcs12_version);
92108ec4bd3Shylee 	if (index  >= buf_len)
92208ec4bd3Shylee 		return (FALSE);
92308ec4bd3Shylee 
92408ec4bd3Shylee 	/*
92508ec4bd3Shylee 	 * Check the 2nd sequence and calculate the number of bytes used
92608ec4bd3Shylee 	 * to store the length.
92708ec4bd3Shylee 	 */
92808ec4bd3Shylee 	if ((buf[index++] & 0xFF) != 0x30)
92908ec4bd3Shylee 		return (FALSE);
93008ec4bd3Shylee 
93108ec4bd3Shylee 	if (buf[index] & 0x80) {
93208ec4bd3Shylee 		length_octets = buf[index++] & 0x0F;
93308ec4bd3Shylee 	} else {
93408ec4bd3Shylee 		length_octets = 1;
93508ec4bd3Shylee 	}
93608ec4bd3Shylee 
93708ec4bd3Shylee 	index += length_octets;
93808ec4bd3Shylee 	if (index + sizeof (pkcs12_oid) >= buf_len)
93908ec4bd3Shylee 		return (FALSE);
94008ec4bd3Shylee 
94108ec4bd3Shylee 	/* Skip the length octets and check the oid */
94208ec4bd3Shylee 	if (memcmp(buf + index, pkcs12_oid, sizeof (pkcs12_oid)) != 0)
94308ec4bd3Shylee 		return (FALSE);
94408ec4bd3Shylee 	else
94508ec4bd3Shylee 		return (TRUE);
94608ec4bd3Shylee }
94708ec4bd3Shylee 
948d00756ccSwyllys KMF_RETURN
949d00756ccSwyllys kmf_get_data_format(KMF_DATA *data, KMF_ENCODE_FORMAT *fmt)
950d00756ccSwyllys {
951d00756ccSwyllys 	uchar_t *buf = data->Data;
952d00756ccSwyllys 
953d00756ccSwyllys 	if (check_for_pkcs12(buf, data->Length) == TRUE) {
954d00756ccSwyllys 		*fmt = KMF_FORMAT_PKCS12;
955d00756ccSwyllys 	} else if (buf[0] == 0x30 && (buf[1] & 0x80)) {
956d00756ccSwyllys 		/* It is most likely a generic ASN.1 encoded file */
957d00756ccSwyllys 		*fmt = KMF_FORMAT_ASN1;
958d00756ccSwyllys 	} else if (check_for_pem(buf, fmt) != TRUE) {
959d00756ccSwyllys 		/* Cannot determine this file format */
960d00756ccSwyllys 		*fmt = KMF_FORMAT_UNDEF;
961d00756ccSwyllys 		return (KMF_ERR_ENCODING);
962d00756ccSwyllys 	}
963d00756ccSwyllys 	return (KMF_OK);
964d00756ccSwyllys }
965d00756ccSwyllys 
96699ebb4caSwyllys KMF_RETURN
96730a5e8faSwyllys kmf_get_file_format(char *filename, KMF_ENCODE_FORMAT *fmt)
96899ebb4caSwyllys {
96999ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
97008ec4bd3Shylee 	KMF_DATA filebuf = {NULL, 0};
97199ebb4caSwyllys 
97299ebb4caSwyllys 	if (filename == NULL || !strlen(filename) || fmt == NULL)
97399ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
97499ebb4caSwyllys 
97599ebb4caSwyllys 	*fmt = 0;
97630a5e8faSwyllys 	ret = kmf_read_input_file(NULL, filename, &filebuf);
97708ec4bd3Shylee 	if (ret != KMF_OK)
97808ec4bd3Shylee 		return (ret);
97999ebb4caSwyllys 
98008ec4bd3Shylee 	if (filebuf.Length < 8) {
98108ec4bd3Shylee 		ret = KMF_ERR_ENCODING; /* too small */
98299ebb4caSwyllys 		goto end;
98399ebb4caSwyllys 	}
98499ebb4caSwyllys 
985d00756ccSwyllys 	ret = kmf_get_data_format(&filebuf, fmt);
98699ebb4caSwyllys end:
98730a5e8faSwyllys 	kmf_free_data(&filebuf);
98899ebb4caSwyllys 	return (ret);
98999ebb4caSwyllys }
99099ebb4caSwyllys 
99199ebb4caSwyllys KMF_RETURN
99230a5e8faSwyllys kmf_hexstr_to_bytes(unsigned char *hexstr, unsigned char **bytes,
99399ebb4caSwyllys 	size_t *outlen)
99499ebb4caSwyllys {
99599ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
99699ebb4caSwyllys 	unsigned char *buf = NULL;
99799ebb4caSwyllys 	int len, stringlen;
99899ebb4caSwyllys 	int i;
99999ebb4caSwyllys 	unsigned char ch;
100099ebb4caSwyllys 
100199ebb4caSwyllys 	if (hexstr == NULL) {
100299ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
100399ebb4caSwyllys 	}
100499ebb4caSwyllys 
100530a5e8faSwyllys 	if (hexstr[0] == '0' && ((hexstr[1] == 'x') || (hexstr[1] == 'X')))
100699ebb4caSwyllys 		hexstr += 2;
100799ebb4caSwyllys 
100830a5e8faSwyllys 	for (i = 0; i < strlen((char *)hexstr) && isxdigit(hexstr[i]); i++)
100930a5e8faSwyllys 		/* empty body */
101030a5e8faSwyllys 		;
101199ebb4caSwyllys 	/*
101299ebb4caSwyllys 	 * If all the characters are not legitimate hex chars,
101399ebb4caSwyllys 	 * return an error.
101499ebb4caSwyllys 	 */
101599ebb4caSwyllys 	if (i != strlen((char *)hexstr))
101699ebb4caSwyllys 		return (KMF_ERR_BAD_HEX_STRING);
101799ebb4caSwyllys 	stringlen = i;
101899ebb4caSwyllys 	len = (i / 2) + (i % 2);
101999ebb4caSwyllys 
102099ebb4caSwyllys 	buf = malloc(len);
102199ebb4caSwyllys 	if (buf == NULL) {
102299ebb4caSwyllys 		return (KMF_ERR_MEMORY);
102399ebb4caSwyllys 	}
102499ebb4caSwyllys 	(void) memset(buf, 0, len);
102599ebb4caSwyllys 
102699ebb4caSwyllys 	for (i = 0; i < stringlen; i++) {
102799ebb4caSwyllys 		ch = (unsigned char) *hexstr;
102899ebb4caSwyllys 		hexstr++;
102999ebb4caSwyllys 		if ((ch >= '0') && (ch <= '9'))
103099ebb4caSwyllys 			ch -= '0';
103199ebb4caSwyllys 		else if ((ch >= 'A') && (ch <= 'F'))
103299ebb4caSwyllys 			ch = ch - 'A' + 10;
103399ebb4caSwyllys 		else if ((ch >= 'a') && (ch <= 'f'))
103499ebb4caSwyllys 			ch = ch - 'a' + 10;
103599ebb4caSwyllys 		else {
103699ebb4caSwyllys 			ret = KMF_ERR_BAD_HEX_STRING;
103799ebb4caSwyllys 			goto out;
103899ebb4caSwyllys 		}
103999ebb4caSwyllys 
104099ebb4caSwyllys 		if (i & 1) {
104199ebb4caSwyllys 			buf[i/2] |= ch;
104299ebb4caSwyllys 		} else {
104399ebb4caSwyllys 			buf[i/2] = (ch << 4);
104499ebb4caSwyllys 		}
104599ebb4caSwyllys 	}
104699ebb4caSwyllys 
104799ebb4caSwyllys 	*bytes = buf;
104899ebb4caSwyllys 	*outlen = len;
104999ebb4caSwyllys out:
105099ebb4caSwyllys 	if (buf != NULL && ret != KMF_OK) {
105199ebb4caSwyllys 		free(buf);
105299ebb4caSwyllys 	}
105399ebb4caSwyllys 	return (ret);
105499ebb4caSwyllys }
105599ebb4caSwyllys 
105699ebb4caSwyllys void
105730a5e8faSwyllys kmf_free_dn(KMF_X509_NAME *name)
105899ebb4caSwyllys {
105999ebb4caSwyllys 	KMF_X509_RDN 		*newrdn = NULL;
106099ebb4caSwyllys 	KMF_X509_TYPE_VALUE_PAIR *av = NULL;
106199ebb4caSwyllys 	int i, j;
106299ebb4caSwyllys 
106399ebb4caSwyllys 	if (name && name->numberOfRDNs) {
106499ebb4caSwyllys 		for (i = 0; i < name->numberOfRDNs; i++) {
106599ebb4caSwyllys 			newrdn = &name->RelativeDistinguishedName[i];
106699ebb4caSwyllys 			for (j = 0; j < newrdn->numberOfPairs; j++) {
106799ebb4caSwyllys 				av = &newrdn->AttributeTypeAndValue[j];
106830a5e8faSwyllys 				kmf_free_data(&av->type);
106930a5e8faSwyllys 				kmf_free_data(&av->value);
107099ebb4caSwyllys 			}
107199ebb4caSwyllys 			free(newrdn->AttributeTypeAndValue);
107299ebb4caSwyllys 			newrdn->numberOfPairs = 0;
107399ebb4caSwyllys 			newrdn->AttributeTypeAndValue = NULL;
107499ebb4caSwyllys 		}
107599ebb4caSwyllys 		free(name->RelativeDistinguishedName);
107699ebb4caSwyllys 		name->numberOfRDNs = 0;
107799ebb4caSwyllys 		name->RelativeDistinguishedName = NULL;
107899ebb4caSwyllys 	}
107999ebb4caSwyllys }
108099ebb4caSwyllys 
108199ebb4caSwyllys void
108230a5e8faSwyllys kmf_free_kmf_cert(KMF_HANDLE_T handle, KMF_X509_DER_CERT *kmf_cert)
108399ebb4caSwyllys {
108499ebb4caSwyllys 	KMF_PLUGIN *plugin;
108599ebb4caSwyllys 	KMF_RETURN ret;
108699ebb4caSwyllys 
108799ebb4caSwyllys 	CLEAR_ERROR(handle, ret);
108899ebb4caSwyllys 	if (ret != KMF_OK)
108999ebb4caSwyllys 		return;
109099ebb4caSwyllys 
109199ebb4caSwyllys 	if (kmf_cert == NULL)
109299ebb4caSwyllys 		return;
109399ebb4caSwyllys 
109499ebb4caSwyllys 	plugin = FindPlugin(handle, kmf_cert->kmf_private.keystore_type);
109599ebb4caSwyllys 
109699ebb4caSwyllys 	if (plugin != NULL && plugin->funclist->FreeKMFCert != NULL) {
109799ebb4caSwyllys 		plugin->funclist->FreeKMFCert(handle, kmf_cert);
109899ebb4caSwyllys 	}
109999ebb4caSwyllys }
110099ebb4caSwyllys 
110199ebb4caSwyllys void
110230a5e8faSwyllys kmf_free_data(KMF_DATA *datablock)
110399ebb4caSwyllys {
110499ebb4caSwyllys 	if (datablock != NULL && datablock->Data != NULL) {
110599ebb4caSwyllys 		free(datablock->Data);
110699ebb4caSwyllys 		datablock->Data = NULL;
110799ebb4caSwyllys 		datablock->Length = 0;
110899ebb4caSwyllys 	}
110999ebb4caSwyllys }
111099ebb4caSwyllys 
111199ebb4caSwyllys void
111230a5e8faSwyllys kmf_free_algoid(KMF_X509_ALGORITHM_IDENTIFIER *algoid)
111399ebb4caSwyllys {
111499ebb4caSwyllys 	if (algoid == NULL)
111599ebb4caSwyllys 		return;
111630a5e8faSwyllys 	kmf_free_data(&algoid->algorithm);
111730a5e8faSwyllys 	kmf_free_data(&algoid->parameters);
111899ebb4caSwyllys }
111999ebb4caSwyllys 
112099ebb4caSwyllys void
112130a5e8faSwyllys kmf_free_extn(KMF_X509_EXTENSION *exptr)
112299ebb4caSwyllys {
112399ebb4caSwyllys 	if (exptr == NULL)
112499ebb4caSwyllys 		return;
112599ebb4caSwyllys 
112630a5e8faSwyllys 	kmf_free_data((KMF_DATA *)&exptr->extnId);
112730a5e8faSwyllys 	kmf_free_data(&exptr->BERvalue);
112899ebb4caSwyllys 
112999ebb4caSwyllys 	if (exptr->value.tagAndValue) {
113030a5e8faSwyllys 		kmf_free_data(&exptr->value.tagAndValue->value);
113199ebb4caSwyllys 		free(exptr->value.tagAndValue);
113299ebb4caSwyllys 	}
113399ebb4caSwyllys }
113499ebb4caSwyllys 
113599ebb4caSwyllys void
113630a5e8faSwyllys kmf_free_tbs_csr(KMF_TBS_CSR *tbscsr)
113799ebb4caSwyllys {
113899ebb4caSwyllys 	if (tbscsr) {
113930a5e8faSwyllys 		kmf_free_data(&tbscsr->version);
114099ebb4caSwyllys 
114130a5e8faSwyllys 		kmf_free_dn(&tbscsr->subject);
114299ebb4caSwyllys 
114330a5e8faSwyllys 		kmf_free_algoid(&tbscsr->subjectPublicKeyInfo.algorithm);
114430a5e8faSwyllys 		kmf_free_data(&tbscsr->subjectPublicKeyInfo.subjectPublicKey);
114599ebb4caSwyllys 
114699ebb4caSwyllys 		free_extensions(&tbscsr->extensions);
114799ebb4caSwyllys 	}
114899ebb4caSwyllys }
114999ebb4caSwyllys 
115099ebb4caSwyllys void
115130a5e8faSwyllys kmf_free_signed_csr(KMF_CSR_DATA *csr)
115299ebb4caSwyllys {
115399ebb4caSwyllys 	if (csr) {
115430a5e8faSwyllys 		kmf_free_tbs_csr(&csr->csr);
115599ebb4caSwyllys 
115630a5e8faSwyllys 		kmf_free_algoid(&csr->signature.algorithmIdentifier);
115730a5e8faSwyllys 		kmf_free_data(&csr->signature.encrypted);
115899ebb4caSwyllys 	}
115999ebb4caSwyllys }
116099ebb4caSwyllys 
116199ebb4caSwyllys static void
116299ebb4caSwyllys free_validity(KMF_X509_VALIDITY *validity)
116399ebb4caSwyllys {
116499ebb4caSwyllys 	if (validity == NULL)
116599ebb4caSwyllys 		return;
116630a5e8faSwyllys 	kmf_free_data(&validity->notBefore.time);
116730a5e8faSwyllys 	kmf_free_data(&validity->notAfter.time);
116899ebb4caSwyllys }
116999ebb4caSwyllys 
117099ebb4caSwyllys static void
117199ebb4caSwyllys free_extensions(KMF_X509_EXTENSIONS *extns)
117299ebb4caSwyllys {
117399ebb4caSwyllys 	int i;
117499ebb4caSwyllys 	KMF_X509_EXTENSION *exptr;
117599ebb4caSwyllys 
117699ebb4caSwyllys 	if (extns && extns->numberOfExtensions > 0) {
117799ebb4caSwyllys 		for (i = 0; i < extns->numberOfExtensions; i++) {
117899ebb4caSwyllys 			exptr = &extns->extensions[i];
117930a5e8faSwyllys 			kmf_free_extn(exptr);
118099ebb4caSwyllys 		}
118199ebb4caSwyllys 		free(extns->extensions);
118299ebb4caSwyllys 		extns->numberOfExtensions = 0;
118399ebb4caSwyllys 		extns->extensions = NULL;
118499ebb4caSwyllys 	}
118599ebb4caSwyllys }
118699ebb4caSwyllys 
118799ebb4caSwyllys void
118830a5e8faSwyllys kmf_free_tbs_cert(KMF_X509_TBS_CERT *tbscert)
118999ebb4caSwyllys {
119099ebb4caSwyllys 	if (tbscert) {
119130a5e8faSwyllys 		kmf_free_data(&tbscert->version);
119230a5e8faSwyllys 		kmf_free_bigint(&tbscert->serialNumber);
119330a5e8faSwyllys 		kmf_free_algoid(&tbscert->signature);
119499ebb4caSwyllys 
119530a5e8faSwyllys 		kmf_free_dn(&tbscert->issuer);
119630a5e8faSwyllys 		kmf_free_dn(&tbscert->subject);
119799ebb4caSwyllys 
119899ebb4caSwyllys 		free_validity(&tbscert->validity);
119999ebb4caSwyllys 
120030a5e8faSwyllys 		kmf_free_data(&tbscert->issuerUniqueIdentifier);
120130a5e8faSwyllys 		kmf_free_data(&tbscert->subjectUniqueIdentifier);
120299ebb4caSwyllys 
120330a5e8faSwyllys 		kmf_free_algoid(&tbscert->subjectPublicKeyInfo.algorithm);
120430a5e8faSwyllys 		kmf_free_data(&tbscert->subjectPublicKeyInfo.subjectPublicKey);
120599ebb4caSwyllys 
120699ebb4caSwyllys 		free_extensions(&tbscert->extensions);
120799ebb4caSwyllys 
120830a5e8faSwyllys 		kmf_free_data(&tbscert->issuerUniqueIdentifier);
120930a5e8faSwyllys 		kmf_free_data(&tbscert->subjectUniqueIdentifier);
121099ebb4caSwyllys 	}
121199ebb4caSwyllys }
121299ebb4caSwyllys 
121399ebb4caSwyllys void
121430a5e8faSwyllys kmf_free_signed_cert(KMF_X509_CERTIFICATE *certptr)
121599ebb4caSwyllys {
121699ebb4caSwyllys 	if (!certptr)
121799ebb4caSwyllys 		return;
121899ebb4caSwyllys 
121930a5e8faSwyllys 	kmf_free_tbs_cert(&certptr->certificate);
122099ebb4caSwyllys 
122130a5e8faSwyllys 	kmf_free_algoid(&certptr->signature.algorithmIdentifier);
122230a5e8faSwyllys 	kmf_free_data(&certptr->signature.encrypted);
122399ebb4caSwyllys }
122499ebb4caSwyllys 
122599ebb4caSwyllys void
122630a5e8faSwyllys kmf_free_str(char *pstr)
122799ebb4caSwyllys {
122899ebb4caSwyllys 	if (pstr != NULL)
122999ebb4caSwyllys 		free(pstr);
123099ebb4caSwyllys }
123199ebb4caSwyllys 
123299ebb4caSwyllys void
123399ebb4caSwyllys free_keyidlist(KMF_OID *oidlist, int len)
123499ebb4caSwyllys {
123599ebb4caSwyllys 	int i;
123699ebb4caSwyllys 	for (i = 0; i < len; i++) {
123730a5e8faSwyllys 		kmf_free_data((KMF_DATA *)&oidlist[i]);
123899ebb4caSwyllys 	}
123999ebb4caSwyllys 	free(oidlist);
124099ebb4caSwyllys }
124199ebb4caSwyllys 
124299ebb4caSwyllys void
124330a5e8faSwyllys kmf_free_eku(KMF_X509EXT_EKU *eptr)
124499ebb4caSwyllys {
124530a5e8faSwyllys 	if (eptr && eptr->nEKUs > 0 && eptr->keyPurposeIdList != NULL)
124699ebb4caSwyllys 		free_keyidlist(eptr->keyPurposeIdList, eptr->nEKUs);
124799ebb4caSwyllys }
124899ebb4caSwyllys 
124999ebb4caSwyllys void
125030a5e8faSwyllys kmf_free_spki(KMF_X509_SPKI *spki)
125199ebb4caSwyllys {
125299ebb4caSwyllys 	if (spki != NULL) {
125330a5e8faSwyllys 		kmf_free_algoid(&spki->algorithm);
125430a5e8faSwyllys 		kmf_free_data(&spki->subjectPublicKey);
125599ebb4caSwyllys 	}
125699ebb4caSwyllys }
125799ebb4caSwyllys 
125899ebb4caSwyllys void
125930a5e8faSwyllys kmf_free_kmf_key(KMF_HANDLE_T handle, KMF_KEY_HANDLE *key)
126099ebb4caSwyllys {
126199ebb4caSwyllys 	KMF_PLUGIN *plugin;
126299ebb4caSwyllys 	KMF_RETURN ret;
126330a5e8faSwyllys 	KMF_ATTRIBUTE attlist[2]; /* only 2 attributes for DeleteKey op */
126430a5e8faSwyllys 	int i = 0;
126530a5e8faSwyllys 	boolean_t token_destroy = B_FALSE;
126630a5e8faSwyllys 
126730a5e8faSwyllys 	if (key == NULL)
126830a5e8faSwyllys 		return;
126999ebb4caSwyllys 
127099ebb4caSwyllys 	CLEAR_ERROR(handle, ret);
127199ebb4caSwyllys 	if (ret != KMF_OK)
127299ebb4caSwyllys 		return;
127399ebb4caSwyllys 
127430a5e8faSwyllys 	kmf_set_attr_at_index(attlist, i,
127530a5e8faSwyllys 	    KMF_KEY_HANDLE_ATTR, key, sizeof (KMF_KEY_HANDLE));
127630a5e8faSwyllys 	i++;
127730a5e8faSwyllys 
127830a5e8faSwyllys 	kmf_set_attr_at_index(attlist, i,
127930a5e8faSwyllys 	    KMF_DESTROY_BOOL_ATTR, &token_destroy, sizeof (boolean_t));
128030a5e8faSwyllys 	i++;
128199ebb4caSwyllys 
128299ebb4caSwyllys 	plugin = FindPlugin(handle, key->kstype);
128399ebb4caSwyllys 	if (plugin != NULL && plugin->funclist->DeleteKey != NULL) {
128430a5e8faSwyllys 		(void) plugin->funclist->DeleteKey(handle, i, attlist);
128599ebb4caSwyllys 	}
128699ebb4caSwyllys 
128799ebb4caSwyllys 	if (key->keylabel)
128899ebb4caSwyllys 		free(key->keylabel);
128999ebb4caSwyllys 
129099ebb4caSwyllys 	if (key->israw) {
129130a5e8faSwyllys 		kmf_free_raw_key(key->keyp);
129299ebb4caSwyllys 		free(key->keyp);
129399ebb4caSwyllys 	}
129499ebb4caSwyllys 
129599ebb4caSwyllys 	(void) memset(key, 0, sizeof (KMF_KEY_HANDLE));
129699ebb4caSwyllys }
129799ebb4caSwyllys 
129899ebb4caSwyllys void
129930a5e8faSwyllys kmf_free_bigint(KMF_BIGINT *big)
130099ebb4caSwyllys {
130199ebb4caSwyllys 	if (big != NULL && big->val != NULL) {
130202744e81Swyllys 		/* Clear it out before returning it to the pool */
130302744e81Swyllys 		(void) memset(big->val, 0x00, big->len);
130499ebb4caSwyllys 		free(big->val);
130599ebb4caSwyllys 		big->val = NULL;
130699ebb4caSwyllys 		big->len = 0;
130799ebb4caSwyllys 	}
130899ebb4caSwyllys }
130999ebb4caSwyllys 
131099ebb4caSwyllys static void
131199ebb4caSwyllys free_raw_rsa(KMF_RAW_RSA_KEY *key)
131299ebb4caSwyllys {
131399ebb4caSwyllys 	if (key == NULL)
131499ebb4caSwyllys 		return;
131530a5e8faSwyllys 	kmf_free_bigint(&key->mod);
131630a5e8faSwyllys 	kmf_free_bigint(&key->pubexp);
131730a5e8faSwyllys 	kmf_free_bigint(&key->priexp);
131830a5e8faSwyllys 	kmf_free_bigint(&key->prime1);
131930a5e8faSwyllys 	kmf_free_bigint(&key->prime2);
132030a5e8faSwyllys 	kmf_free_bigint(&key->exp1);
132130a5e8faSwyllys 	kmf_free_bigint(&key->exp2);
132230a5e8faSwyllys 	kmf_free_bigint(&key->coef);
132399ebb4caSwyllys }
132499ebb4caSwyllys 
132599ebb4caSwyllys static void
132699ebb4caSwyllys free_raw_dsa(KMF_RAW_DSA_KEY *key)
132799ebb4caSwyllys {
132899ebb4caSwyllys 	if (key == NULL)
132999ebb4caSwyllys 		return;
133030a5e8faSwyllys 	kmf_free_bigint(&key->prime);
133130a5e8faSwyllys 	kmf_free_bigint(&key->subprime);
133230a5e8faSwyllys 	kmf_free_bigint(&key->base);
133330a5e8faSwyllys 	kmf_free_bigint(&key->value);
133499ebb4caSwyllys }
133599ebb4caSwyllys 
133699ebb4caSwyllys static void
133799ebb4caSwyllys free_raw_sym(KMF_RAW_SYM_KEY *key)
133899ebb4caSwyllys {
133999ebb4caSwyllys 	if (key == NULL)
134099ebb4caSwyllys 		return;
134130a5e8faSwyllys 	kmf_free_bigint(&key->keydata);
134299ebb4caSwyllys }
134399ebb4caSwyllys 
134499ebb4caSwyllys void
134530a5e8faSwyllys kmf_free_raw_key(KMF_RAW_KEY_DATA *key)
134699ebb4caSwyllys {
134799ebb4caSwyllys 	if (key == NULL)
134899ebb4caSwyllys 		return;
134999ebb4caSwyllys 
135099ebb4caSwyllys 	switch (key->keytype) {
135199ebb4caSwyllys 	case KMF_RSA:
135299ebb4caSwyllys 		free_raw_rsa(&key->rawdata.rsa);
135399ebb4caSwyllys 		break;
135499ebb4caSwyllys 	case KMF_DSA:
135599ebb4caSwyllys 		free_raw_dsa(&key->rawdata.dsa);
135699ebb4caSwyllys 		break;
135799ebb4caSwyllys 	case KMF_AES:
135899ebb4caSwyllys 	case KMF_RC4:
135999ebb4caSwyllys 	case KMF_DES:
136099ebb4caSwyllys 	case KMF_DES3:
136199ebb4caSwyllys 		free_raw_sym(&key->rawdata.sym);
136299ebb4caSwyllys 		break;
136399ebb4caSwyllys 	}
13645b3e1433Swyllys 	if (key->label) {
13655b3e1433Swyllys 		free(key->label);
13665b3e1433Swyllys 		key->label = NULL;
13675b3e1433Swyllys 	}
13685b3e1433Swyllys 	kmf_free_data(&key->id);
136999ebb4caSwyllys }
137099ebb4caSwyllys 
137199ebb4caSwyllys void
137230a5e8faSwyllys kmf_free_raw_sym_key(KMF_RAW_SYM_KEY *key)
137399ebb4caSwyllys {
137499ebb4caSwyllys 	if (key == NULL)
137599ebb4caSwyllys 		return;
137630a5e8faSwyllys 	kmf_free_bigint(&key->keydata);
137799ebb4caSwyllys 	free(key);
137899ebb4caSwyllys }
137999ebb4caSwyllys 
138099ebb4caSwyllys /*
138199ebb4caSwyllys  * This function frees the space allocated for the name portion of a
138299ebb4caSwyllys  * KMF_CRL_DIST_POINT.
138399ebb4caSwyllys  */
138499ebb4caSwyllys void
138599ebb4caSwyllys free_dp_name(KMF_CRL_DIST_POINT *dp)
138699ebb4caSwyllys {
138799ebb4caSwyllys 	KMF_GENERALNAMES *fullname;
138899ebb4caSwyllys 	KMF_DATA *urldata;
138999ebb4caSwyllys 	int i;
139099ebb4caSwyllys 
139199ebb4caSwyllys 	if (dp == NULL)
139299ebb4caSwyllys 		return;
139399ebb4caSwyllys 
139499ebb4caSwyllys 	/* For phase 1, we only need to free the fullname space. */
139599ebb4caSwyllys 	fullname = &(dp->name.full_name);
139699ebb4caSwyllys 	if (fullname->number == 0)
139799ebb4caSwyllys 		return;
139899ebb4caSwyllys 
139999ebb4caSwyllys 	for (i = 0; i < fullname->number; i++) {
140099ebb4caSwyllys 		urldata = &(fullname->namelist[fullname->number - 1].name);
140130a5e8faSwyllys 		kmf_free_data(urldata);
140299ebb4caSwyllys 	}
140399ebb4caSwyllys 
140499ebb4caSwyllys 	free(fullname->namelist);
140599ebb4caSwyllys }
140699ebb4caSwyllys 
140799ebb4caSwyllys /*
140899ebb4caSwyllys  * This function frees the space allocated for a KMF_CRL_DIST_POINT.
140999ebb4caSwyllys  */
141099ebb4caSwyllys void
141199ebb4caSwyllys free_dp(KMF_CRL_DIST_POINT *dp)
141299ebb4caSwyllys {
141399ebb4caSwyllys 	if (dp == NULL)
141499ebb4caSwyllys 		return;
141599ebb4caSwyllys 
141699ebb4caSwyllys 	free_dp_name(dp);
141730a5e8faSwyllys 	kmf_free_data(&(dp->reasons));
141899ebb4caSwyllys 	/* Need not to free crl_issuer space at phase 1 */
141999ebb4caSwyllys }
142099ebb4caSwyllys 
142199ebb4caSwyllys /*
142299ebb4caSwyllys  * This function frees space for a KMF_X509EXT_CRLDISTPOINTS internally.
142399ebb4caSwyllys  */
142499ebb4caSwyllys void
142530a5e8faSwyllys kmf_free_crl_dist_pts(KMF_X509EXT_CRLDISTPOINTS *crl_dps)
142699ebb4caSwyllys {
142799ebb4caSwyllys 	int i;
142899ebb4caSwyllys 
142999ebb4caSwyllys 	if (crl_dps == NULL)
143099ebb4caSwyllys 		return;
143199ebb4caSwyllys 
143299ebb4caSwyllys 	for (i = 0; i < crl_dps->number; i++)
143399ebb4caSwyllys 		free_dp(&(crl_dps->dplist[i]));
143499ebb4caSwyllys 
143599ebb4caSwyllys 	free(crl_dps->dplist);
143699ebb4caSwyllys }
143799ebb4caSwyllys 
143899ebb4caSwyllys KMF_RETURN
143930a5e8faSwyllys kmf_create_ocsp_request(KMF_HANDLE_T handle,
144030a5e8faSwyllys 	int	num_args,
144130a5e8faSwyllys 	KMF_ATTRIBUTE	*attrlist)
144299ebb4caSwyllys {
144330a5e8faSwyllys 	KMF_RETURN ret = KMF_OK;
144499ebb4caSwyllys 	KMF_PLUGIN *plugin;
144530a5e8faSwyllys 	KMF_RETURN (*createReqFn)(void *, int num_args,
144630a5e8faSwyllys 	    KMF_ATTRIBUTE *attrlist);
144799ebb4caSwyllys 
144830a5e8faSwyllys 	KMF_ATTRIBUTE_TESTER required_attrs[] = {
144930a5e8faSwyllys 		{KMF_OCSP_REQUEST_FILENAME_ATTR, FALSE, 1, 0},
145030a5e8faSwyllys 		{KMF_USER_CERT_DATA_ATTR, FALSE, sizeof (KMF_DATA),
145130a5e8faSwyllys 			sizeof (KMF_DATA)},
145230a5e8faSwyllys 		{KMF_ISSUER_CERT_DATA_ATTR, FALSE, sizeof (KMF_DATA),
145330a5e8faSwyllys 			sizeof (KMF_DATA)},
145430a5e8faSwyllys 	};
145599ebb4caSwyllys 
145630a5e8faSwyllys 	int num_req_attrs = sizeof (required_attrs) /
145730a5e8faSwyllys 	    sizeof (KMF_ATTRIBUTE_TESTER);
145899ebb4caSwyllys 
145930a5e8faSwyllys 	if (handle == NULL)
146099ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
146199ebb4caSwyllys 
146230a5e8faSwyllys 	CLEAR_ERROR(handle, ret);
146330a5e8faSwyllys 
146430a5e8faSwyllys 	ret = test_attributes(num_req_attrs, required_attrs,
146530a5e8faSwyllys 	    0, NULL, num_args, attrlist);
146630a5e8faSwyllys 
146730a5e8faSwyllys 	if (ret != KMF_OK)
146830a5e8faSwyllys 		return (ret);
146930a5e8faSwyllys 
147099ebb4caSwyllys 	/*
147199ebb4caSwyllys 	 * This framework function is actually implemented in the openssl
147299ebb4caSwyllys 	 * plugin library, so we find the function address and call it.
147399ebb4caSwyllys 	 */
147499ebb4caSwyllys 	plugin = FindPlugin(handle, KMF_KEYSTORE_OPENSSL);
147599ebb4caSwyllys 	if (plugin == NULL || plugin->dldesc == NULL) {
147699ebb4caSwyllys 		return (KMF_ERR_PLUGIN_NOTFOUND);
147799ebb4caSwyllys 	}
147899ebb4caSwyllys 
147999ebb4caSwyllys 	createReqFn = (KMF_RETURN(*)())dlsym(plugin->dldesc,
148099ebb4caSwyllys 	    "OpenSSL_CreateOCSPRequest");
148199ebb4caSwyllys 	if (createReqFn == NULL) {
148299ebb4caSwyllys 		return (KMF_ERR_FUNCTION_NOT_FOUND);
148399ebb4caSwyllys 	}
148499ebb4caSwyllys 
148530a5e8faSwyllys 	return (createReqFn(handle, num_args, attrlist));
148630a5e8faSwyllys 
148799ebb4caSwyllys }
148899ebb4caSwyllys 
148999ebb4caSwyllys KMF_RETURN
149030a5e8faSwyllys kmf_get_ocsp_status_for_cert(KMF_HANDLE_T handle,
149130a5e8faSwyllys 	int	num_args,
149230a5e8faSwyllys 	KMF_ATTRIBUTE	*attrlist)
149399ebb4caSwyllys {
149430a5e8faSwyllys 	KMF_RETURN ret = KMF_OK;
149599ebb4caSwyllys 	KMF_PLUGIN *plugin;
149630a5e8faSwyllys 	KMF_RETURN (*getCertStatusFn)(void *, int num_args,
149730a5e8faSwyllys 	    KMF_ATTRIBUTE *attrlist);
149830a5e8faSwyllys 
149930a5e8faSwyllys 	KMF_ATTRIBUTE_TESTER required_attrs[] = {
150030a5e8faSwyllys 		{KMF_USER_CERT_DATA_ATTR, FALSE, sizeof (KMF_DATA),
150130a5e8faSwyllys 			sizeof (KMF_DATA)},
150230a5e8faSwyllys 		{KMF_ISSUER_CERT_DATA_ATTR, FALSE, sizeof (KMF_DATA),
150330a5e8faSwyllys 			sizeof (KMF_DATA)},
150430a5e8faSwyllys 		{KMF_OCSP_RESPONSE_DATA_ATTR, FALSE, sizeof (KMF_DATA),
150530a5e8faSwyllys 			sizeof (KMF_DATA)},
150630a5e8faSwyllys 		{KMF_OCSP_RESPONSE_STATUS_ATTR, FALSE, sizeof (int),
150730a5e8faSwyllys 			sizeof (uint32_t)},
150830a5e8faSwyllys 		{KMF_OCSP_RESPONSE_REASON_ATTR, FALSE, sizeof (int),
150930a5e8faSwyllys 			sizeof (uint32_t)},
151030a5e8faSwyllys 		{KMF_OCSP_RESPONSE_CERT_STATUS_ATTR, FALSE, sizeof (int),
151130a5e8faSwyllys 			sizeof (uint32_t)},
151230a5e8faSwyllys 	};
151330a5e8faSwyllys 
151430a5e8faSwyllys 	int num_req_attrs = sizeof (required_attrs) /
151530a5e8faSwyllys 	    sizeof (KMF_ATTRIBUTE_TESTER);
151630a5e8faSwyllys 
151730a5e8faSwyllys 	if (handle == NULL)
151830a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
151999ebb4caSwyllys 
152099ebb4caSwyllys 	CLEAR_ERROR(handle, ret);
152199ebb4caSwyllys 
152230a5e8faSwyllys 	ret = test_attributes(num_req_attrs, required_attrs,
152330a5e8faSwyllys 	    0, NULL, num_args, attrlist);
152499ebb4caSwyllys 
152530a5e8faSwyllys 	if (ret != KMF_OK)
152630a5e8faSwyllys 		return (ret);
152799ebb4caSwyllys 
152899ebb4caSwyllys 	/*
152999ebb4caSwyllys 	 * This framework function is actually implemented in the openssl
153099ebb4caSwyllys 	 * plugin library, so we find the function address and call it.
153199ebb4caSwyllys 	 */
153299ebb4caSwyllys 	plugin = FindPlugin(handle, KMF_KEYSTORE_OPENSSL);
153399ebb4caSwyllys 	if (plugin == NULL || plugin->dldesc == NULL) {
153499ebb4caSwyllys 		return (KMF_ERR_INTERNAL);
153599ebb4caSwyllys 	}
153699ebb4caSwyllys 
153799ebb4caSwyllys 	getCertStatusFn = (KMF_RETURN(*)())dlsym(plugin->dldesc,
153899ebb4caSwyllys 	    "OpenSSL_GetOCSPStatusForCert");
153999ebb4caSwyllys 	if (getCertStatusFn == NULL) {
154099ebb4caSwyllys 		return (KMF_ERR_INTERNAL);
154199ebb4caSwyllys 	}
154299ebb4caSwyllys 
154330a5e8faSwyllys 	return (getCertStatusFn(handle, num_args, attrlist));
154430a5e8faSwyllys 
154599ebb4caSwyllys }
154699ebb4caSwyllys 
154799ebb4caSwyllys KMF_RETURN
154830a5e8faSwyllys kmf_string_to_oid(char *oidstring, KMF_OID *oid)
154999ebb4caSwyllys {
155099ebb4caSwyllys 	KMF_RETURN rv = KMF_OK;
155199ebb4caSwyllys 	char *cp, *bp, *startp;
155299ebb4caSwyllys 	int numbuf;
155399ebb4caSwyllys 	int onumbuf;
155499ebb4caSwyllys 	int nbytes, index;
155599ebb4caSwyllys 	int len;
155699ebb4caSwyllys 	unsigned char *op;
155799ebb4caSwyllys 
155899ebb4caSwyllys 	if (oidstring == NULL || oid == NULL)
155999ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
156099ebb4caSwyllys 
156199ebb4caSwyllys 	len = strlen(oidstring);
156299ebb4caSwyllys 
156399ebb4caSwyllys 	bp = oidstring;
156499ebb4caSwyllys 	cp = bp;
156599ebb4caSwyllys 	/* Skip over leading space */
156699ebb4caSwyllys 	while ((bp < &cp[len]) && isspace(*bp))
156799ebb4caSwyllys 		bp++;
156899ebb4caSwyllys 
156999ebb4caSwyllys 	startp = bp;
157099ebb4caSwyllys 	nbytes = 0;
157199ebb4caSwyllys 
157299ebb4caSwyllys 	/*
157399ebb4caSwyllys 	 * The first two numbers are chewed up by the first octet.
157499ebb4caSwyllys 	 */
157599ebb4caSwyllys 	if (sscanf(bp, "%d", &numbuf) != 1)
157699ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
157799ebb4caSwyllys 	while ((bp < &cp[len]) && isdigit(*bp))
157899ebb4caSwyllys 		bp++;
157999ebb4caSwyllys 	while ((bp < &cp[len]) && (isspace(*bp) || *bp == '.'))
158099ebb4caSwyllys 		bp++;
158199ebb4caSwyllys 	if (sscanf(bp, "%d", &numbuf) != 1)
158299ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
158399ebb4caSwyllys 	while ((bp < &cp[len]) && isdigit(*bp))
158499ebb4caSwyllys 		bp++;
158599ebb4caSwyllys 	while ((bp < &cp[len]) && (isspace(*bp) || *bp == '.'))
158699ebb4caSwyllys 		bp++;
158799ebb4caSwyllys 	nbytes++;
158899ebb4caSwyllys 
158999ebb4caSwyllys 	while (isdigit(*bp)) {
159099ebb4caSwyllys 		if (sscanf(bp, "%d", &numbuf) != 1)
159199ebb4caSwyllys 			return (KMF_ERR_BAD_PARAMETER);
159299ebb4caSwyllys 		while (numbuf) {
159399ebb4caSwyllys 			nbytes++;
159499ebb4caSwyllys 			numbuf >>= 7;
159599ebb4caSwyllys 		}
159699ebb4caSwyllys 		while ((bp < &cp[len]) && isdigit(*bp))
159799ebb4caSwyllys 			bp++;
159899ebb4caSwyllys 		while ((bp < &cp[len]) && (isspace(*bp) || *bp == '.'))
159999ebb4caSwyllys 			bp++;
160099ebb4caSwyllys 	}
160199ebb4caSwyllys 
160299ebb4caSwyllys 	oid->Length = nbytes;
160399ebb4caSwyllys 	oid->Data = malloc(oid->Length);
160499ebb4caSwyllys 	if (oid->Data == NULL) {
160599ebb4caSwyllys 		return (KMF_ERR_MEMORY);
160699ebb4caSwyllys 	}
160799ebb4caSwyllys 	(void) memset(oid->Data, 0, oid->Length);
160899ebb4caSwyllys 
160999ebb4caSwyllys 	op = oid->Data;
161099ebb4caSwyllys 
161199ebb4caSwyllys 	bp = startp;
161299ebb4caSwyllys 	(void) sscanf(bp, "%d", &numbuf);
161399ebb4caSwyllys 
161499ebb4caSwyllys 	while (isdigit(*bp)) bp++;
161599ebb4caSwyllys 	while (isspace(*bp) || *bp == '.') bp++;
161699ebb4caSwyllys 
161799ebb4caSwyllys 	onumbuf = 40 * numbuf;
161899ebb4caSwyllys 	(void) sscanf(bp, "%d", &numbuf);
161999ebb4caSwyllys 	onumbuf += numbuf;
162099ebb4caSwyllys 	*op = (unsigned char) onumbuf;
162199ebb4caSwyllys 	op++;
162299ebb4caSwyllys 
162399ebb4caSwyllys 	while (isdigit(*bp)) bp++;
162499ebb4caSwyllys 	while (isspace(*bp) || *bp == '.') bp++;
162599ebb4caSwyllys 	while (isdigit(*bp)) {
162699ebb4caSwyllys 		(void) sscanf(bp, "%d", &numbuf);
162799ebb4caSwyllys 		nbytes = 0;
162899ebb4caSwyllys 		/* Have to fill in the bytes msb-first */
162999ebb4caSwyllys 		onumbuf = numbuf;
163099ebb4caSwyllys 		while (numbuf) {
163199ebb4caSwyllys 			nbytes++;
163299ebb4caSwyllys 			numbuf >>= 7;
163399ebb4caSwyllys 		}
163499ebb4caSwyllys 		numbuf = onumbuf;
163599ebb4caSwyllys 		op += nbytes;
163699ebb4caSwyllys 		index = -1;
163799ebb4caSwyllys 		while (numbuf) {
163899ebb4caSwyllys 			op[index] = (unsigned char)numbuf & 0x7f;
163999ebb4caSwyllys 			if (index != -1)
164099ebb4caSwyllys 				op[index] |= 0x80;
164199ebb4caSwyllys 			index--;
164299ebb4caSwyllys 			numbuf >>= 7;
164399ebb4caSwyllys 		}
164499ebb4caSwyllys 		while (isdigit(*bp)) bp++;
164599ebb4caSwyllys 		while (isspace(*bp) || *bp == '.') bp++;
164699ebb4caSwyllys 	}
164799ebb4caSwyllys 
164899ebb4caSwyllys 	return (rv);
164999ebb4caSwyllys }
165099ebb4caSwyllys 
165199ebb4caSwyllys static KMF_RETURN
165299ebb4caSwyllys encode_rid(char *name, KMF_DATA *derdata)
165399ebb4caSwyllys {
165499ebb4caSwyllys 	KMF_RETURN rv = KMF_OK;
165599ebb4caSwyllys 
165699ebb4caSwyllys 	if (name == NULL || derdata == NULL)
165799ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
165899ebb4caSwyllys 
165930a5e8faSwyllys 	rv = kmf_string_to_oid(name, (KMF_OID *)derdata);
166099ebb4caSwyllys 
166199ebb4caSwyllys 	return (rv);
166299ebb4caSwyllys }
166399ebb4caSwyllys 
166499ebb4caSwyllys static KMF_RETURN
166599ebb4caSwyllys encode_ipaddr(char *name, KMF_DATA *derdata)
166699ebb4caSwyllys {
166799ebb4caSwyllys 	KMF_RETURN rv = KMF_OK;
166899ebb4caSwyllys 	size_t len;
166999ebb4caSwyllys 	in_addr_t v4;
167099ebb4caSwyllys 	in6_addr_t v6;
167199ebb4caSwyllys 	uint8_t *ptr;
167299ebb4caSwyllys 
167399ebb4caSwyllys 	if (name == NULL || derdata == NULL)
167499ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
167599ebb4caSwyllys 
167699ebb4caSwyllys 	v4 = inet_addr(name);
167799ebb4caSwyllys 	if (v4 == (in_addr_t)-1) {
167899ebb4caSwyllys 		ptr = (uint8_t *)&v6;
167999ebb4caSwyllys 		if (inet_pton(AF_INET6, name, ptr) != 1)
168099ebb4caSwyllys 			return (KMF_ERR_ENCODING);
168199ebb4caSwyllys 		len = sizeof (v6);
168299ebb4caSwyllys 	} else {
168399ebb4caSwyllys 		ptr = (uint8_t *)&v4;
168499ebb4caSwyllys 		len = sizeof (v4);
168599ebb4caSwyllys 	}
168699ebb4caSwyllys 
168799ebb4caSwyllys 	derdata->Data = malloc(len);
168899ebb4caSwyllys 	if (derdata->Data == NULL)
168999ebb4caSwyllys 		return (KMF_ERR_MEMORY);
169099ebb4caSwyllys 	(void) memcpy(derdata->Data, ptr, len);
169199ebb4caSwyllys 	derdata->Length = len;
169299ebb4caSwyllys 
169399ebb4caSwyllys 	return (rv);
169499ebb4caSwyllys }
169599ebb4caSwyllys 
1696d00756ccSwyllys static KMF_RETURN
1697d00756ccSwyllys encode_krb5(char *name, KMF_DATA *derdata)
1698d00756ccSwyllys {
1699d00756ccSwyllys 	KMF_RETURN rv = KMF_OK;
1700d00756ccSwyllys 	char *at, *realm;
170156664548SWyllys Ingersoll 	char *slash, *inst = NULL;
1702d00756ccSwyllys 	BerElement *asn1 = NULL;
1703d00756ccSwyllys 	BerValue *extdata = NULL;
1704d00756ccSwyllys 
1705d00756ccSwyllys 	at = strchr(name, '@');
1706d00756ccSwyllys 	if (at == NULL)
1707d00756ccSwyllys 		return (KMF_ERR_ENCODING);
1708d00756ccSwyllys 
170956664548SWyllys Ingersoll 	realm = at + 1;
1710d00756ccSwyllys 	*at = 0;
1711d00756ccSwyllys 
171256664548SWyllys Ingersoll 	/*
171356664548SWyllys Ingersoll 	 * KRB5PrincipalName ::= SEQUENCE {
171456664548SWyllys Ingersoll 	 *	realm		[0] Realm,
171556664548SWyllys Ingersoll 	 *	principalName	[1] PrincipalName
171656664548SWyllys Ingersoll 	 * }
171756664548SWyllys Ingersoll 	 *
171856664548SWyllys Ingersoll 	 * KerberosString	::= GeneralString (IA5String)
171956664548SWyllys Ingersoll 	 * Realm	::= KerberosString
172056664548SWyllys Ingersoll 	 * PrincipalName	::= SEQUENCE {
172156664548SWyllys Ingersoll 	 *	name-type	[0] Int32,
172256664548SWyllys Ingersoll 	 *	name-string	[1] SEQUENCE OF KerberosString
172356664548SWyllys Ingersoll 	 * }
172456664548SWyllys Ingersoll 	 */
172556664548SWyllys Ingersoll 
172656664548SWyllys Ingersoll 	/*
172756664548SWyllys Ingersoll 	 * Construct the "principalName" first.
172856664548SWyllys Ingersoll 	 *
172956664548SWyllys Ingersoll 	 * The name may be split with a "/" to indicate a new instance.
173056664548SWyllys Ingersoll 	 * This must be separated in the ASN.1
173156664548SWyllys Ingersoll 	 */
173256664548SWyllys Ingersoll 	slash = strchr(name, '/');
173356664548SWyllys Ingersoll 	if (slash != NULL) {
173456664548SWyllys Ingersoll 		inst = name;
173556664548SWyllys Ingersoll 		name = slash + 1;
173656664548SWyllys Ingersoll 		*slash = 0;
173756664548SWyllys Ingersoll 	}
173856664548SWyllys Ingersoll 	if ((asn1 = kmfder_alloc()) == NULL) {
173956664548SWyllys Ingersoll 		rv = KMF_ERR_MEMORY;
174056664548SWyllys Ingersoll 		goto cleanup;
174156664548SWyllys Ingersoll 	}
174256664548SWyllys Ingersoll 	if (kmfber_printf(asn1, "{Tli", 0xa0, 3, 0x01) == -1)
174356664548SWyllys Ingersoll 		goto cleanup;
174456664548SWyllys Ingersoll 
174556664548SWyllys Ingersoll 	if (inst != NULL) {
174656664548SWyllys Ingersoll 		if (kmfber_printf(asn1, "Tl{Tl", 0xA1,
174756664548SWyllys Ingersoll 		    strlen(inst) + strlen(name) + 6,
174856664548SWyllys Ingersoll 		    BER_GENERALSTRING, strlen(inst)) == -1)
174956664548SWyllys Ingersoll 			goto cleanup;
175056664548SWyllys Ingersoll 		if (kmfber_write(asn1, inst, strlen(inst), 0) != strlen(inst))
175156664548SWyllys Ingersoll 			goto cleanup;
175256664548SWyllys Ingersoll 		if (kmfber_printf(asn1, "Tl", BER_GENERALSTRING,
175356664548SWyllys Ingersoll 		    strlen(name)) == -1)
175456664548SWyllys Ingersoll 			goto cleanup;
175556664548SWyllys Ingersoll 		if (kmfber_write(asn1, name, strlen(name), 0) != strlen(name))
175656664548SWyllys Ingersoll 			goto cleanup;
175756664548SWyllys Ingersoll 	} else {
175856664548SWyllys Ingersoll 		if (kmfber_printf(asn1, "Tl{Tl", 0xA1,
175956664548SWyllys Ingersoll 		    strlen(name) + 4, BER_GENERALSTRING, strlen(name)) == -1)
176056664548SWyllys Ingersoll 			goto cleanup;
176156664548SWyllys Ingersoll 		if (kmfber_write(asn1, name, strlen(name), 0) != strlen(name))
176256664548SWyllys Ingersoll 			goto cleanup;
176356664548SWyllys Ingersoll 	}
1764d00756ccSwyllys 
176556664548SWyllys Ingersoll 	if (kmfber_printf(asn1, "}}") == -1)
1766d00756ccSwyllys 		goto cleanup;
176756664548SWyllys Ingersoll 	if (kmfber_flatten(asn1, &extdata) == -1) {
176856664548SWyllys Ingersoll 		rv = KMF_ERR_ENCODING;
176956664548SWyllys Ingersoll 		goto cleanup;
177056664548SWyllys Ingersoll 	}
177156664548SWyllys Ingersoll 	kmfber_free(asn1, 1);
177256664548SWyllys Ingersoll 	asn1 = NULL;
1773d00756ccSwyllys 
177456664548SWyllys Ingersoll 	/* Next construct the KRB5PrincipalNameSeq */
177556664548SWyllys Ingersoll 	if ((asn1 = kmfder_alloc()) == NULL) {
177656664548SWyllys Ingersoll 		kmfber_bvfree(extdata);
177756664548SWyllys Ingersoll 		rv = KMF_ERR_MEMORY;
177856664548SWyllys Ingersoll 		goto cleanup;
177956664548SWyllys Ingersoll 	}
178056664548SWyllys Ingersoll 	if (kmfber_printf(asn1, "{TlTl", 0xA0, strlen(realm) + 2,
178156664548SWyllys Ingersoll 	    BER_GENERALSTRING, strlen(realm)) == -1)
1782d00756ccSwyllys 		goto cleanup;
1783d00756ccSwyllys 	if (kmfber_write(asn1, realm, strlen(realm), 0) != strlen(realm))
1784d00756ccSwyllys 		goto cleanup;
178556664548SWyllys Ingersoll 	if (kmfber_printf(asn1, "Tl", 0xA1, extdata->bv_len) == -1)
1786d00756ccSwyllys 		goto cleanup;
178756664548SWyllys Ingersoll 	if (kmfber_write(asn1, extdata->bv_val,
178856664548SWyllys Ingersoll 	    extdata->bv_len, 0) != extdata->bv_len)
1789d00756ccSwyllys 		goto cleanup;
179056664548SWyllys Ingersoll 	if (kmfber_printf(asn1, "}") == -1)
179156664548SWyllys Ingersoll 		goto cleanup;
179256664548SWyllys Ingersoll 	kmfber_bvfree(extdata);
179356664548SWyllys Ingersoll 	extdata = NULL;
179456664548SWyllys Ingersoll 	if (kmfber_flatten(asn1, &extdata) == -1) {
179556664548SWyllys Ingersoll 		rv = KMF_ERR_ENCODING;
1796d00756ccSwyllys 		goto cleanup;
179756664548SWyllys Ingersoll 	}
179856664548SWyllys Ingersoll 	kmfber_free(asn1, 1);
179956664548SWyllys Ingersoll 	asn1 = NULL;
180056664548SWyllys Ingersoll 
180156664548SWyllys Ingersoll 	/*
180256664548SWyllys Ingersoll 	 * GeneralName ::= CHOICE {
180356664548SWyllys Ingersoll 	 *	otherName	[0]	OtherName,
180456664548SWyllys Ingersoll 	 *	...
180556664548SWyllys Ingersoll 	 * }
180656664548SWyllys Ingersoll 	 *
180756664548SWyllys Ingersoll 	 * OtherName ::= SEQUENCE {
180856664548SWyllys Ingersoll 	 *	type-id	OBJECT IDENTIFIER,
180956664548SWyllys Ingersoll 	 *	value	[0] EXPLICIT ANY DEFINED BY type-id
181056664548SWyllys Ingersoll 	 * }
181156664548SWyllys Ingersoll 	 */
1812d00756ccSwyllys 
181356664548SWyllys Ingersoll 	/* Now construct the SAN: OID + typed data. */
181456664548SWyllys Ingersoll 	if ((asn1 = kmfder_alloc()) == NULL) {
181556664548SWyllys Ingersoll 		kmfber_bvfree(extdata);
181656664548SWyllys Ingersoll 		rv = KMF_ERR_MEMORY;
181756664548SWyllys Ingersoll 		goto cleanup;
181856664548SWyllys Ingersoll 	}
181956664548SWyllys Ingersoll 	if (kmfber_printf(asn1, "D", &KMFOID_PKINIT_san) == -1)
182056664548SWyllys Ingersoll 		goto cleanup;
182156664548SWyllys Ingersoll 	if (kmfber_printf(asn1, "Tl", 0xA0, extdata->bv_len) == -1)
182256664548SWyllys Ingersoll 		goto cleanup;
182356664548SWyllys Ingersoll 	if (kmfber_write(asn1, extdata->bv_val,
182456664548SWyllys Ingersoll 	    extdata->bv_len, 0) != extdata->bv_len)
182556664548SWyllys Ingersoll 		goto cleanup;
182656664548SWyllys Ingersoll 	kmfber_bvfree(extdata);
182756664548SWyllys Ingersoll 	extdata = NULL;
1828d00756ccSwyllys 	if (kmfber_flatten(asn1, &extdata) == -1) {
1829d00756ccSwyllys 		rv = KMF_ERR_ENCODING;
1830d00756ccSwyllys 		goto cleanup;
1831d00756ccSwyllys 	}
183256664548SWyllys Ingersoll 	kmfber_free(asn1, 1);
183356664548SWyllys Ingersoll 	asn1 = NULL;
1834d00756ccSwyllys 
1835d00756ccSwyllys 	derdata->Data = (uchar_t *)extdata->bv_val;
183656664548SWyllys Ingersoll 	extdata->bv_val = NULL; /* clear it so it is not freed later */
1837d00756ccSwyllys 	derdata->Length = extdata->bv_len;
1838d00756ccSwyllys 
1839d00756ccSwyllys cleanup:
1840d00756ccSwyllys 	if (asn1 != NULL)
1841d00756ccSwyllys 		kmfber_free(asn1, 1);
1842d00756ccSwyllys 
184356664548SWyllys Ingersoll 	if (extdata != NULL)
184456664548SWyllys Ingersoll 		kmfber_bvfree(extdata);
184556664548SWyllys Ingersoll 
1846d00756ccSwyllys 	if (*at == 0)
1847d00756ccSwyllys 		*at = '@';
1848d00756ccSwyllys 
184956664548SWyllys Ingersoll 	if (inst != NULL)
185056664548SWyllys Ingersoll 		*slash = '/';
185156664548SWyllys Ingersoll 
1852d00756ccSwyllys 	return (rv);
1853d00756ccSwyllys }
1854d00756ccSwyllys 
1855d00756ccSwyllys static KMF_RETURN
1856d00756ccSwyllys encode_sclogon(char *name, KMF_DATA *derdata)
1857d00756ccSwyllys {
1858d00756ccSwyllys 	KMF_RETURN rv = KMF_OK;
1859d00756ccSwyllys 	BerElement *asn1 = NULL;
1860d00756ccSwyllys 	BerValue *extdata = NULL;
1861d00756ccSwyllys 
1862d00756ccSwyllys 	if ((asn1 = kmfder_alloc()) == NULL)
1863d00756ccSwyllys 		return (KMF_ERR_MEMORY);
1864d00756ccSwyllys 
1865d00756ccSwyllys 	/* The name is encoded as a KerberosString (IA5STRING) */
1866d00756ccSwyllys 	if (kmfber_printf(asn1, "{Ds}",
1867d00756ccSwyllys 	    &KMFOID_MS_KP_SCLogon, name) == -1)
1868d00756ccSwyllys 		goto cleanup;
1869d00756ccSwyllys 
1870d00756ccSwyllys 	if (kmfber_flatten(asn1, &extdata) == -1) {
1871d00756ccSwyllys 		rv = KMF_ERR_ENCODING;
1872d00756ccSwyllys 		goto cleanup;
1873d00756ccSwyllys 	}
1874d00756ccSwyllys 
1875d00756ccSwyllys 	derdata->Data = (uchar_t *)extdata->bv_val;
1876d00756ccSwyllys 	derdata->Length = extdata->bv_len;
1877d00756ccSwyllys 
1878d00756ccSwyllys 	free(extdata);
1879d00756ccSwyllys cleanup:
1880d00756ccSwyllys 	if (asn1 != NULL)
1881d00756ccSwyllys 		kmfber_free(asn1, 1);
1882d00756ccSwyllys 
1883d00756ccSwyllys 	return (rv);
1884d00756ccSwyllys }
1885d00756ccSwyllys 
188699ebb4caSwyllys static KMF_RETURN
188799ebb4caSwyllys verify_uri_format(char *uristring)
188899ebb4caSwyllys {
188999ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
189099ebb4caSwyllys 	xmlURIPtr   uriptr = NULL;
189199ebb4caSwyllys 
189299ebb4caSwyllys 	/* Parse the URI string; get the hostname and port */
189399ebb4caSwyllys 	uriptr = xmlParseURI(uristring);
189499ebb4caSwyllys 	if (uriptr == NULL) {
189599ebb4caSwyllys 		ret = KMF_ERR_BAD_URI;
189699ebb4caSwyllys 		goto out;
189799ebb4caSwyllys 	}
189899ebb4caSwyllys 
189999ebb4caSwyllys 	if (uriptr->scheme == NULL || !strlen(uriptr->scheme)) {
190099ebb4caSwyllys 		ret = KMF_ERR_BAD_URI;
190199ebb4caSwyllys 		goto out;
190299ebb4caSwyllys 	}
190399ebb4caSwyllys 
190499ebb4caSwyllys 	if (uriptr->server == NULL || !strlen(uriptr->server)) {
190599ebb4caSwyllys 		ret = KMF_ERR_BAD_URI;
190699ebb4caSwyllys 		goto out;
190799ebb4caSwyllys 	}
190899ebb4caSwyllys out:
190999ebb4caSwyllys 	if (uriptr != NULL)
191099ebb4caSwyllys 		xmlFreeURI(uriptr);
191199ebb4caSwyllys 	return (ret);
191299ebb4caSwyllys }
191399ebb4caSwyllys 
191499ebb4caSwyllys static KMF_RETURN
191599ebb4caSwyllys encode_altname(char *namedata,
191699ebb4caSwyllys 	KMF_GENERALNAMECHOICES nametype, KMF_DATA *encodedname)
191799ebb4caSwyllys {
191899ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
191999ebb4caSwyllys 	KMF_X509_NAME dnname;
192099ebb4caSwyllys 	uchar_t tagval;
192199ebb4caSwyllys 	BerElement *asn1 = NULL;
192299ebb4caSwyllys 	BerValue *extdata;
192399ebb4caSwyllys 
192499ebb4caSwyllys 	if (namedata == NULL || encodedname == NULL)
192599ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
192699ebb4caSwyllys 
192799ebb4caSwyllys 	/*
192899ebb4caSwyllys 	 * Encode the namedata according to rules in RFC 3280 for GeneralName.
192999ebb4caSwyllys 	 * The input "namedata" is assumed to be an ASCII string representation
193099ebb4caSwyllys 	 * of the AltName, we need to convert it to correct ASN.1 here before
193199ebb4caSwyllys 	 * adding it to the cert.
193299ebb4caSwyllys 	 */
193399ebb4caSwyllys 	switch (nametype) {
193499ebb4caSwyllys 		case GENNAME_RFC822NAME: /* rfc 822 */
193599ebb4caSwyllys 			/* IA5String, no encoding needed */
193699ebb4caSwyllys 			encodedname->Data = (uchar_t *)strdup(namedata);
193799ebb4caSwyllys 			if (encodedname->Data == NULL)
193899ebb4caSwyllys 				return (KMF_ERR_MEMORY);
193999ebb4caSwyllys 			encodedname->Length = strlen(namedata);
194099ebb4caSwyllys 			tagval = (0x80 | nametype);
194199ebb4caSwyllys 			break;
194299ebb4caSwyllys 		case GENNAME_DNSNAME: /* rfc 1034 */
194399ebb4caSwyllys 			encodedname->Data = (uchar_t *)strdup(namedata);
194499ebb4caSwyllys 			if (encodedname->Data == NULL)
194599ebb4caSwyllys 				return (KMF_ERR_MEMORY);
194699ebb4caSwyllys 			encodedname->Length = strlen(namedata);
194799ebb4caSwyllys 			tagval = (0x80 | nametype);
194899ebb4caSwyllys 			break;
194999ebb4caSwyllys 		case GENNAME_URI: /* rfc 1738 */
195099ebb4caSwyllys 			ret = verify_uri_format(namedata);
195199ebb4caSwyllys 			if (ret != KMF_OK)
195299ebb4caSwyllys 				return (ret);
195399ebb4caSwyllys 			/* IA5String, no encoding needed */
195499ebb4caSwyllys 			encodedname->Data = (uchar_t *)strdup(namedata);
195599ebb4caSwyllys 			if (encodedname->Data == NULL)
195699ebb4caSwyllys 				return (KMF_ERR_MEMORY);
195799ebb4caSwyllys 			encodedname->Length = strlen(namedata);
195899ebb4caSwyllys 			tagval = (0x80 | nametype);
195999ebb4caSwyllys 			break;
196099ebb4caSwyllys 		case GENNAME_IPADDRESS:
196199ebb4caSwyllys 			ret =  encode_ipaddr(namedata, encodedname);
196299ebb4caSwyllys 			tagval = (0x80 | nametype);
196399ebb4caSwyllys 			break;
196499ebb4caSwyllys 		case GENNAME_REGISTEREDID:
196599ebb4caSwyllys 			ret = encode_rid(namedata, encodedname);
196699ebb4caSwyllys 			tagval = (0x80 | nametype);
196799ebb4caSwyllys 			break;
196899ebb4caSwyllys 		case GENNAME_DIRECTORYNAME:
196930a5e8faSwyllys 			ret = kmf_dn_parser(namedata, &dnname);
197099ebb4caSwyllys 			if (ret == KMF_OK) {
197130a5e8faSwyllys 				ret = DerEncodeName(&dnname, encodedname);
197299ebb4caSwyllys 			}
197330a5e8faSwyllys 			(void) kmf_free_dn(&dnname);
197456664548SWyllys Ingersoll 			tagval = (0x80 | nametype);
197599ebb4caSwyllys 			break;
1976d00756ccSwyllys 		case GENNAME_KRB5PRINC:
197756664548SWyllys Ingersoll 			tagval = (0xA0 | GENNAME_OTHERNAME);
1978d00756ccSwyllys 			ret = encode_krb5(namedata, encodedname);
1979d00756ccSwyllys 			break;
1980d00756ccSwyllys 		case GENNAME_SCLOGON_UPN:
198156664548SWyllys Ingersoll 			tagval = (0xA0 | GENNAME_OTHERNAME);
1982d00756ccSwyllys 			ret = encode_sclogon(namedata, encodedname);
1983d00756ccSwyllys 			break;
198499ebb4caSwyllys 		default:
198599ebb4caSwyllys 			/* unsupported */
198699ebb4caSwyllys 			return (KMF_ERR_BAD_PARAMETER);
198799ebb4caSwyllys 
198899ebb4caSwyllys 	}
198999ebb4caSwyllys 	if (ret != KMF_OK) {
199030a5e8faSwyllys 		kmf_free_data(encodedname);
199199ebb4caSwyllys 		return (ret);
199299ebb4caSwyllys 	}
199399ebb4caSwyllys 
199499ebb4caSwyllys 	if ((asn1 = kmfder_alloc()) == NULL)
199599ebb4caSwyllys 		return (KMF_ERR_MEMORY);
199699ebb4caSwyllys 
199730a5e8faSwyllys 	if (kmfber_printf(asn1, "Tl", tagval, encodedname->Length) == -1)
199899ebb4caSwyllys 		goto cleanup;
199999ebb4caSwyllys 
200099ebb4caSwyllys 	if (kmfber_write(asn1, (char *)encodedname->Data,
200130a5e8faSwyllys 	    encodedname->Length, 0) == -1) {
200299ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
200399ebb4caSwyllys 		goto cleanup;
200499ebb4caSwyllys 	}
200599ebb4caSwyllys 	if (kmfber_flatten(asn1, &extdata) == -1) {
200699ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
200799ebb4caSwyllys 		goto cleanup;
200899ebb4caSwyllys 	}
200999ebb4caSwyllys 
201030a5e8faSwyllys 	kmf_free_data(encodedname);
201199ebb4caSwyllys 	encodedname->Data = (uchar_t *)extdata->bv_val;
201299ebb4caSwyllys 	encodedname->Length = extdata->bv_len;
201399ebb4caSwyllys 
201499ebb4caSwyllys 	free(extdata);
201599ebb4caSwyllys 
201699ebb4caSwyllys cleanup:
201799ebb4caSwyllys 	if (asn1)
201899ebb4caSwyllys 		kmfber_free(asn1, 1);
201999ebb4caSwyllys 
202099ebb4caSwyllys 	if (ret != KMF_OK)
202130a5e8faSwyllys 		kmf_free_data(encodedname);
202299ebb4caSwyllys 
202399ebb4caSwyllys 	return (ret);
202499ebb4caSwyllys }
202599ebb4caSwyllys 
202699ebb4caSwyllys KMF_X509_EXTENSION *
202799ebb4caSwyllys FindExtn(KMF_X509_EXTENSIONS *exts, KMF_OID *oid)
202899ebb4caSwyllys {
202999ebb4caSwyllys 	KMF_X509_EXTENSION *foundextn = NULL;
203099ebb4caSwyllys 	int i;
203199ebb4caSwyllys 
203299ebb4caSwyllys 	if (exts == NULL || oid == NULL)
203399ebb4caSwyllys 		return (NULL);
203499ebb4caSwyllys 
203599ebb4caSwyllys 	for (i = 0; i < exts->numberOfExtensions; i++) {
203699ebb4caSwyllys 		if (IsEqualOid(oid, &exts->extensions[i].extnId))  {
203799ebb4caSwyllys 			foundextn = &exts->extensions[i];
203899ebb4caSwyllys 			break;
203999ebb4caSwyllys 		}
204099ebb4caSwyllys 	}
204199ebb4caSwyllys 	return (foundextn);
204299ebb4caSwyllys }
204399ebb4caSwyllys 
204499ebb4caSwyllys KMF_RETURN
204599ebb4caSwyllys GetSequenceContents(char *data, size_t len,
204699ebb4caSwyllys 	char **contents, size_t *outlen)
204799ebb4caSwyllys {
204899ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
204999ebb4caSwyllys 	BerElement *exasn1 = NULL;
205099ebb4caSwyllys 	BerValue oldextn;
205199ebb4caSwyllys 	int tag;
205299ebb4caSwyllys 	size_t oldsize;
205399ebb4caSwyllys 	char *olddata = NULL;
205499ebb4caSwyllys 
205599ebb4caSwyllys 	if (data == NULL || contents == NULL || outlen == NULL)
205699ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
205799ebb4caSwyllys 
205899ebb4caSwyllys 	/*
205999ebb4caSwyllys 	 * Decode the sequence of general names
206099ebb4caSwyllys 	 */
206199ebb4caSwyllys 	oldextn.bv_val = data;
206299ebb4caSwyllys 	oldextn.bv_len = len;
206399ebb4caSwyllys 
206499ebb4caSwyllys 	if ((exasn1 = kmfder_init(&oldextn)) == NULL) {
206599ebb4caSwyllys 		ret = KMF_ERR_MEMORY;
206699ebb4caSwyllys 		goto out;
206799ebb4caSwyllys 	}
206899ebb4caSwyllys 
206999ebb4caSwyllys 	/*
207099ebb4caSwyllys 	 * Unwrap the sequence to find the size of the block
207199ebb4caSwyllys 	 * of GeneralName items in the set.
207299ebb4caSwyllys 	 *
207399ebb4caSwyllys 	 * Peek at the tag and length ("tl"),
207499ebb4caSwyllys 	 * then consume them ("{").
207599ebb4caSwyllys 	 */
207699ebb4caSwyllys 	if (kmfber_scanf(exasn1, "tl{", &tag, &oldsize) == KMFBER_DEFAULT ||
207730a5e8faSwyllys 	    oldsize == 0) {
207899ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
207999ebb4caSwyllys 		goto out;
208099ebb4caSwyllys 	}
208199ebb4caSwyllys 
208299ebb4caSwyllys 	olddata = malloc(oldsize);
208399ebb4caSwyllys 	if (olddata == NULL) {
208499ebb4caSwyllys 		ret = KMF_ERR_MEMORY;
208599ebb4caSwyllys 		goto out;
208699ebb4caSwyllys 	}
208799ebb4caSwyllys 	(void) memset(olddata, 0, oldsize);
208899ebb4caSwyllys 	/*
208999ebb4caSwyllys 	 * Read the entire blob of GeneralNames, we don't
209099ebb4caSwyllys 	 * need to interpret them now.
209199ebb4caSwyllys 	 */
209299ebb4caSwyllys 	if (kmfber_read(exasn1, olddata, oldsize) != oldsize) {
209399ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
209499ebb4caSwyllys 		goto out;
209599ebb4caSwyllys 	}
209699ebb4caSwyllys out:
209799ebb4caSwyllys 	if (exasn1 != NULL)
209899ebb4caSwyllys 		kmfber_free(exasn1, 1);
209999ebb4caSwyllys 
210099ebb4caSwyllys 	if (ret != KMF_OK) {
210199ebb4caSwyllys 		*contents = NULL;
210299ebb4caSwyllys 		*outlen = 0;
210399ebb4caSwyllys 		if (olddata != NULL)
210499ebb4caSwyllys 			free(olddata);
210599ebb4caSwyllys 	} else {
210699ebb4caSwyllys 		*contents = olddata;
210799ebb4caSwyllys 		*outlen = oldsize;
210899ebb4caSwyllys 	}
210999ebb4caSwyllys 	return (ret);
211099ebb4caSwyllys }
211199ebb4caSwyllys 
211299ebb4caSwyllys KMF_RETURN
211399ebb4caSwyllys add_an_extension(KMF_X509_EXTENSIONS *exts, KMF_X509_EXTENSION *newextn)
211499ebb4caSwyllys {
211599ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
211699ebb4caSwyllys 	KMF_X509_EXTENSION *extlist;
211799ebb4caSwyllys 
211899ebb4caSwyllys 	if (exts == NULL || newextn == NULL)
211999ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
212099ebb4caSwyllys 
212199ebb4caSwyllys 	extlist = malloc(sizeof (KMF_X509_EXTENSION) *
212230a5e8faSwyllys 	    (exts->numberOfExtensions + 1));
212399ebb4caSwyllys 	if (extlist == NULL)
212499ebb4caSwyllys 		return (KMF_ERR_MEMORY);
212599ebb4caSwyllys 
212699ebb4caSwyllys 	(void) memcpy(extlist, exts->extensions,
212799ebb4caSwyllys 	    exts->numberOfExtensions * sizeof (KMF_X509_EXTENSION));
212899ebb4caSwyllys 
212999ebb4caSwyllys 	(void) memcpy(&extlist[exts->numberOfExtensions], newextn,
213030a5e8faSwyllys 	    sizeof (KMF_X509_EXTENSION));
213199ebb4caSwyllys 
213299ebb4caSwyllys 	free(exts->extensions);
213399ebb4caSwyllys 	exts->numberOfExtensions++;
213499ebb4caSwyllys 	exts->extensions = extlist;
213599ebb4caSwyllys 
213699ebb4caSwyllys 	return (ret);
213799ebb4caSwyllys }
213899ebb4caSwyllys 
213999ebb4caSwyllys KMF_RETURN
214030a5e8faSwyllys kmf_set_altname(KMF_X509_EXTENSIONS *extensions,
214199ebb4caSwyllys 	KMF_OID *oid,
214299ebb4caSwyllys 	int critical,
214399ebb4caSwyllys 	KMF_GENERALNAMECHOICES nametype,
214499ebb4caSwyllys 	char *namedata)
214599ebb4caSwyllys {
214699ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
214799ebb4caSwyllys 	KMF_X509_EXTENSION subjAltName;
214899ebb4caSwyllys 	KMF_DATA dername = { NULL, 0 };
214999ebb4caSwyllys 	BerElement *asn1 = NULL;
215099ebb4caSwyllys 	BerValue *extdata;
215199ebb4caSwyllys 	char *olddata = NULL;
215299ebb4caSwyllys 	KMF_X509_EXTENSION *foundextn = NULL;
215399ebb4caSwyllys 	size_t	oldsize = 0;
215499ebb4caSwyllys 
215599ebb4caSwyllys 	if (extensions == NULL || oid == NULL || namedata == NULL)
215699ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
215799ebb4caSwyllys 
215899ebb4caSwyllys 	ret = encode_altname(namedata, nametype, &dername);
215999ebb4caSwyllys 
216099ebb4caSwyllys 	if (ret != KMF_OK)
216199ebb4caSwyllys 		return (ret);
216299ebb4caSwyllys 
216399ebb4caSwyllys 	(void) memset(&subjAltName, 0, sizeof (subjAltName));
216499ebb4caSwyllys 
216599ebb4caSwyllys 	ret = copy_data(&subjAltName.extnId, oid);
216699ebb4caSwyllys 	if (ret != KMF_OK)
216799ebb4caSwyllys 		goto out;
216899ebb4caSwyllys 	/*
216999ebb4caSwyllys 	 * Check to see if this cert already has a subjectAltName.
217099ebb4caSwyllys 	 */
217199ebb4caSwyllys 	foundextn = FindExtn(extensions, oid);
217299ebb4caSwyllys 
217399ebb4caSwyllys 	if (foundextn != NULL) {
217499ebb4caSwyllys 		ret = GetSequenceContents(
217530a5e8faSwyllys 		    (char *)foundextn->BERvalue.Data,
217630a5e8faSwyllys 		    foundextn->BERvalue.Length,
217730a5e8faSwyllys 		    &olddata, &oldsize);
217899ebb4caSwyllys 		if (ret != KMF_OK)
217999ebb4caSwyllys 			goto out;
218099ebb4caSwyllys 	}
218199ebb4caSwyllys 
218299ebb4caSwyllys 	/*
218399ebb4caSwyllys 	 * Assume (!!) that the namedata given is already properly encoded.
218499ebb4caSwyllys 	 */
218599ebb4caSwyllys 	if ((asn1 = kmfder_alloc()) == NULL)
218699ebb4caSwyllys 		return (KMF_ERR_MEMORY);
218799ebb4caSwyllys 
218899ebb4caSwyllys 	if (kmfber_printf(asn1, "{") == -1) {
218999ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
219099ebb4caSwyllys 		goto out;
219199ebb4caSwyllys 	}
219299ebb4caSwyllys 
219399ebb4caSwyllys 	/* Write the old extension data first */
219499ebb4caSwyllys 	if (olddata != NULL && oldsize > 0) {
219599ebb4caSwyllys 		if (kmfber_write(asn1, olddata, oldsize, 0) == -1) {
219699ebb4caSwyllys 			ret = KMF_ERR_ENCODING;
219799ebb4caSwyllys 			goto out;
219899ebb4caSwyllys 		}
219999ebb4caSwyllys 	}
220099ebb4caSwyllys 
220199ebb4caSwyllys 	/* Now add the new name to the list */
220299ebb4caSwyllys 	if (kmfber_write(asn1, (char *)dername.Data, dername.Length, 0) == -1) {
220399ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
220499ebb4caSwyllys 		goto out;
220599ebb4caSwyllys 	}
220699ebb4caSwyllys 
220799ebb4caSwyllys 	/* Now close the sequence */
220899ebb4caSwyllys 	if (kmfber_printf(asn1, "}") == -1) {
220999ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
221099ebb4caSwyllys 		goto out;
221199ebb4caSwyllys 	}
221299ebb4caSwyllys 	if (kmfber_flatten(asn1, &extdata) == -1) {
221399ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
221499ebb4caSwyllys 		goto out;
221599ebb4caSwyllys 	}
221699ebb4caSwyllys 
221799ebb4caSwyllys 	/*
221899ebb4caSwyllys 	 * If we are just adding to an existing list of altNames,
221999ebb4caSwyllys 	 * just replace the BER data associated with the found extension.
222099ebb4caSwyllys 	 */
222199ebb4caSwyllys 	if (foundextn != NULL) {
222299ebb4caSwyllys 		free(foundextn->BERvalue.Data);
222399ebb4caSwyllys 		foundextn->critical = critical;
222499ebb4caSwyllys 		foundextn->BERvalue.Data = (uchar_t *)extdata->bv_val;
222599ebb4caSwyllys 		foundextn->BERvalue.Length = extdata->bv_len;
222699ebb4caSwyllys 	} else {
222799ebb4caSwyllys 		subjAltName.critical = critical;
222899ebb4caSwyllys 		subjAltName.format = KMF_X509_DATAFORMAT_ENCODED;
222999ebb4caSwyllys 		subjAltName.BERvalue.Data = (uchar_t *)extdata->bv_val;
223099ebb4caSwyllys 		subjAltName.BERvalue.Length = extdata->bv_len;
223199ebb4caSwyllys 		ret = add_an_extension(extensions, &subjAltName);
223299ebb4caSwyllys 		if (ret != KMF_OK)
223399ebb4caSwyllys 			free(subjAltName.BERvalue.Data);
223499ebb4caSwyllys 	}
223599ebb4caSwyllys 
223699ebb4caSwyllys 	free(extdata);
223799ebb4caSwyllys out:
223899ebb4caSwyllys 	if (olddata != NULL)
223999ebb4caSwyllys 		free(olddata);
224099ebb4caSwyllys 
224130a5e8faSwyllys 	kmf_free_data(&dername);
224299ebb4caSwyllys 	if (ret != KMF_OK)
224330a5e8faSwyllys 		kmf_free_data(&subjAltName.extnId);
224499ebb4caSwyllys 	if (asn1 != NULL)
224599ebb4caSwyllys 		kmfber_free(asn1, 1);
224699ebb4caSwyllys 	return (ret);
224799ebb4caSwyllys }
224830a5e8faSwyllys 
224930a5e8faSwyllys /*
225030a5e8faSwyllys  * Search a list of attributes for one that matches the given type.
225130a5e8faSwyllys  * Return a pointer into the attribute list.  This does not
225230a5e8faSwyllys  * return a copy of the value, it returns a reference into the
225330a5e8faSwyllys  * given list.
225430a5e8faSwyllys  */
225530a5e8faSwyllys int
225630a5e8faSwyllys kmf_find_attr(KMF_ATTR_TYPE type, KMF_ATTRIBUTE *attlist, int numattrs)
225730a5e8faSwyllys {
225830a5e8faSwyllys 	int i;
225930a5e8faSwyllys 	for (i = 0; i < numattrs; i++) {
226030a5e8faSwyllys 		if (attlist[i].type == type)
226130a5e8faSwyllys 			return (i);
226230a5e8faSwyllys 	}
226330a5e8faSwyllys 	return (-1);
226430a5e8faSwyllys }
226530a5e8faSwyllys 
226630a5e8faSwyllys /*
226730a5e8faSwyllys  * Verify that a given attribute is consistent with the
226830a5e8faSwyllys  * "test" attribute.
226930a5e8faSwyllys  */
227030a5e8faSwyllys static KMF_RETURN
227130a5e8faSwyllys verify_attribute(KMF_ATTRIBUTE *givenattr,
227230a5e8faSwyllys 	KMF_ATTRIBUTE_TESTER *testattr)
227330a5e8faSwyllys {
227430a5e8faSwyllys 	/* A NULL pValue was found where one is required */
227530a5e8faSwyllys 	if (testattr->null_value_ok == FALSE &&
227630a5e8faSwyllys 	    givenattr->pValue == NULL)
227730a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
227830a5e8faSwyllys 
227930a5e8faSwyllys 	/* If the given valueLen is too small, return error */
228030a5e8faSwyllys 	if (givenattr->pValue != NULL &&
228130a5e8faSwyllys 	    testattr->minlen > 0 &&
228230a5e8faSwyllys 	    (givenattr->valueLen < testattr->minlen))
228330a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
228430a5e8faSwyllys 
228530a5e8faSwyllys 	/* If the given valueLen is too big, return error */
228630a5e8faSwyllys 	if (givenattr->pValue != NULL &&
228730a5e8faSwyllys 	    testattr->maxlen > 0 &&
228830a5e8faSwyllys 	    (givenattr->valueLen > testattr->maxlen))
228930a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
229030a5e8faSwyllys 
229130a5e8faSwyllys 	return (KMF_OK);
229230a5e8faSwyllys }
229330a5e8faSwyllys 
229430a5e8faSwyllys /*
229530a5e8faSwyllys  * Given a set of required attribute tests and optional
229630a5e8faSwyllys  * attributes, make sure that the actual attributes
229730a5e8faSwyllys  * being tested (attrlist below) are allowed and are
229830a5e8faSwyllys  * properly specified.
229930a5e8faSwyllys  */
230030a5e8faSwyllys KMF_RETURN
230130a5e8faSwyllys test_attributes(int reqnum, KMF_ATTRIBUTE_TESTER *reqattrs,
230230a5e8faSwyllys 	int optnum, KMF_ATTRIBUTE_TESTER *optattrs,
230330a5e8faSwyllys 	int numattrs, KMF_ATTRIBUTE *attrlist)
230430a5e8faSwyllys {
230530a5e8faSwyllys 	KMF_RETURN ret = KMF_OK;
230630a5e8faSwyllys 	int i, idx;
230730a5e8faSwyllys 
230830a5e8faSwyllys 	/*
230930a5e8faSwyllys 	 * If the caller didn't supply enough attributes,
231030a5e8faSwyllys 	 * return an error.
231130a5e8faSwyllys 	 */
231230a5e8faSwyllys 	if (numattrs < reqnum || attrlist == NULL)
231330a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
231430a5e8faSwyllys 
231530a5e8faSwyllys 	/*
231630a5e8faSwyllys 	 * Make sure all required attrs are present and
231730a5e8faSwyllys 	 * correct.
231830a5e8faSwyllys 	 */
231930a5e8faSwyllys 	for (i = 0; i < reqnum && ret == KMF_OK; i++) {
232030a5e8faSwyllys 		idx = kmf_find_attr(reqattrs[i].type, attrlist, numattrs);
232130a5e8faSwyllys 		/* If a required attr is not found, return error */
232230a5e8faSwyllys 		if (idx == -1) {
232330a5e8faSwyllys 			return (KMF_ERR_BAD_PARAMETER);
232430a5e8faSwyllys 		}
232530a5e8faSwyllys 
232630a5e8faSwyllys 		ret = verify_attribute(&attrlist[idx], &reqattrs[i]);
232730a5e8faSwyllys 	}
232830a5e8faSwyllys 	/*
232930a5e8faSwyllys 	 * Now test the optional parameters.
233030a5e8faSwyllys 	 */
233130a5e8faSwyllys 	for (i = 0; i < optnum && ret == KMF_OK; i++) {
233230a5e8faSwyllys 		idx = kmf_find_attr(optattrs[i].type, attrlist, numattrs);
233330a5e8faSwyllys 		/* If a optional attr is not found, continue. */
233430a5e8faSwyllys 		if (idx == -1) {
233530a5e8faSwyllys 			continue;
233630a5e8faSwyllys 		}
233730a5e8faSwyllys 
233830a5e8faSwyllys 		ret = verify_attribute(&attrlist[idx], &optattrs[i]);
233930a5e8faSwyllys 	}
234030a5e8faSwyllys 
234130a5e8faSwyllys 	return (ret);
234230a5e8faSwyllys }
234330a5e8faSwyllys 
234430a5e8faSwyllys /*
234530a5e8faSwyllys  * Given an already allocated attribute list, insert
234630a5e8faSwyllys  * the given attribute information at a specific index
234730a5e8faSwyllys  * in the list.
234830a5e8faSwyllys  */
234930a5e8faSwyllys void
235030a5e8faSwyllys kmf_set_attr_at_index(KMF_ATTRIBUTE *attlist, int index,
235130a5e8faSwyllys 	KMF_ATTR_TYPE type,  void *pValue, uint32_t len)
235230a5e8faSwyllys {
235330a5e8faSwyllys 	if (attlist == NULL)
235430a5e8faSwyllys 		return;
235530a5e8faSwyllys 
235630a5e8faSwyllys 	attlist[index].type = type;
235730a5e8faSwyllys 	attlist[index].pValue = pValue;
235830a5e8faSwyllys 	attlist[index].valueLen = len;
235930a5e8faSwyllys }
236030a5e8faSwyllys 
236130a5e8faSwyllys /*
236230a5e8faSwyllys  * Find an attribute matching a particular type and set
236330a5e8faSwyllys  * the pValue and length fields to the given values.
236430a5e8faSwyllys  */
236530a5e8faSwyllys KMF_RETURN
236630a5e8faSwyllys kmf_set_attr(KMF_ATTRIBUTE *attlist, int numattr,
236730a5e8faSwyllys 	KMF_ATTR_TYPE type,  void *pValue, uint32_t len)
236830a5e8faSwyllys {
236930a5e8faSwyllys 	int idx;
237030a5e8faSwyllys 	if (attlist == NULL)
237130a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
237230a5e8faSwyllys 
237330a5e8faSwyllys 	idx = kmf_find_attr(type, attlist, numattr);
237430a5e8faSwyllys 	if (idx == -1)
237530a5e8faSwyllys 		return (KMF_ERR_ATTR_NOT_FOUND);
237630a5e8faSwyllys 
237730a5e8faSwyllys 	attlist[idx].type = type;
237830a5e8faSwyllys 	/* Assumes the attribute pValue can hold the result */
237930a5e8faSwyllys 	if (attlist[idx].pValue != NULL) {
238030a5e8faSwyllys 		if (attlist[idx].valueLen >= len)
238130a5e8faSwyllys 			(void) memcpy(attlist[idx].pValue, pValue, len);
238230a5e8faSwyllys 		else
238330a5e8faSwyllys 			return (KMF_ERR_BUFFER_SIZE);
238430a5e8faSwyllys 	}
238530a5e8faSwyllys 	attlist[idx].valueLen = len;
238630a5e8faSwyllys 	return (KMF_OK);
238730a5e8faSwyllys }
238830a5e8faSwyllys 
238930a5e8faSwyllys /*
239030a5e8faSwyllys  * Find a particular attribute in a list and return
239130a5e8faSwyllys  * a pointer to its value.
239230a5e8faSwyllys  */
239330a5e8faSwyllys void *
239430a5e8faSwyllys kmf_get_attr_ptr(KMF_ATTR_TYPE type, KMF_ATTRIBUTE *attlist,
239530a5e8faSwyllys 	int numattrs)
239630a5e8faSwyllys {
239730a5e8faSwyllys 	int i;
239830a5e8faSwyllys 
239930a5e8faSwyllys 	i = kmf_find_attr(type, attlist, numattrs);
240030a5e8faSwyllys 	if (i == -1)
240130a5e8faSwyllys 		return (NULL);
240230a5e8faSwyllys 
240330a5e8faSwyllys 	return (attlist[i].pValue);
240430a5e8faSwyllys }
240530a5e8faSwyllys 
240630a5e8faSwyllys /*
240730a5e8faSwyllys  * Find a particular attribute in a list and return
240830a5e8faSwyllys  * the value and length values.  Value and length
240930a5e8faSwyllys  * may be NULL if the caller doesn't want their values
241030a5e8faSwyllys  * to be filled in.
241130a5e8faSwyllys  */
241230a5e8faSwyllys KMF_RETURN
241330a5e8faSwyllys kmf_get_attr(KMF_ATTR_TYPE type, KMF_ATTRIBUTE *attlist,
241430a5e8faSwyllys 	int numattrs, void *outValue, uint32_t *outlen)
241530a5e8faSwyllys {
241630a5e8faSwyllys 	int i;
241730a5e8faSwyllys 	uint32_t len = 0;
241830a5e8faSwyllys 	uint32_t *lenptr = outlen;
241930a5e8faSwyllys 
242030a5e8faSwyllys 	if (lenptr == NULL)
242130a5e8faSwyllys 		lenptr = &len;
242230a5e8faSwyllys 
242330a5e8faSwyllys 	i = kmf_find_attr(type, attlist, numattrs);
242430a5e8faSwyllys 	if (i == -1)
242530a5e8faSwyllys 		return (KMF_ERR_ATTR_NOT_FOUND);
242630a5e8faSwyllys 
242730a5e8faSwyllys 	/* This assumes that the ptr passed in is pre-allocated space */
242830a5e8faSwyllys 	if (attlist[i].pValue != NULL && outValue != NULL) {
242930a5e8faSwyllys 		/*
243030a5e8faSwyllys 		 * If the caller did not specify a length,
243130a5e8faSwyllys 		 * assume "outValue" is big enough.
243230a5e8faSwyllys 		 */
243330a5e8faSwyllys 		if (outlen != NULL) {
243430a5e8faSwyllys 			if (*outlen >= attlist[i].valueLen)
243530a5e8faSwyllys 				(void) memcpy(outValue, attlist[i].pValue,
243630a5e8faSwyllys 				    attlist[i].valueLen);
243730a5e8faSwyllys 			else
243830a5e8faSwyllys 				return (KMF_ERR_BUFFER_SIZE);
243930a5e8faSwyllys 		} else {
244030a5e8faSwyllys 			(void) memcpy(outValue, attlist[i].pValue,
244130a5e8faSwyllys 			    attlist[i].valueLen);
244230a5e8faSwyllys 		}
244330a5e8faSwyllys 	}
244430a5e8faSwyllys 
244530a5e8faSwyllys 	if (outlen != NULL)
244630a5e8faSwyllys 		*outlen = attlist[i].valueLen;
244730a5e8faSwyllys 	return (KMF_OK);
244830a5e8faSwyllys }
244930a5e8faSwyllys 
245030a5e8faSwyllys /*
245130a5e8faSwyllys  * Utility routine to find a string type attribute, allocate it
245230a5e8faSwyllys  * and return the value to the caller.  This simplifies the
245330a5e8faSwyllys  * operation by doing both "kmf_get_attr" calls and avoids
245430a5e8faSwyllys  * duplicating this block of code in lots of places.
245530a5e8faSwyllys  */
245630a5e8faSwyllys KMF_RETURN
245730a5e8faSwyllys kmf_get_string_attr(KMF_ATTR_TYPE type, KMF_ATTRIBUTE *attrlist,
245830a5e8faSwyllys 	int numattrs, char **outstr)
245930a5e8faSwyllys {
246030a5e8faSwyllys 	KMF_RETURN rv;
246130a5e8faSwyllys 	uint32_t len;
246230a5e8faSwyllys 
246330a5e8faSwyllys 	if (outstr == NULL)
246430a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
246530a5e8faSwyllys 
246630a5e8faSwyllys 	if ((rv = kmf_get_attr(type, attrlist, numattrs, NULL, &len)) ==
246730a5e8faSwyllys 	    KMF_OK) {
246830a5e8faSwyllys 		*outstr = malloc(len + 1);
246930a5e8faSwyllys 		if ((*outstr) == NULL)
247030a5e8faSwyllys 			return (KMF_ERR_MEMORY);
247130a5e8faSwyllys 		(void) memset((*outstr), 0, len + 1);
247230a5e8faSwyllys 		rv = kmf_get_attr(type, attrlist, numattrs, (*outstr), &len);
247330a5e8faSwyllys 		if (rv != KMF_OK) {
247430a5e8faSwyllys 			free(*outstr);
247530a5e8faSwyllys 			*outstr = NULL;
247630a5e8faSwyllys 		}
247730a5e8faSwyllys 	}
247830a5e8faSwyllys 
247930a5e8faSwyllys 	return (rv);
248030a5e8faSwyllys }
248130a5e8faSwyllys 
2482431deaa0Shylee 
2483431deaa0Shylee void
2484431deaa0Shylee free_entry(conf_entry_t *entry)
2485431deaa0Shylee {
2486431deaa0Shylee 	if (entry == NULL)
2487431deaa0Shylee 		return;
2488431deaa0Shylee 	free(entry->keystore);
2489431deaa0Shylee 	free(entry->modulepath);
2490431deaa0Shylee 	free(entry->option);
2491431deaa0Shylee }
2492431deaa0Shylee 
2493431deaa0Shylee void
2494431deaa0Shylee free_entrylist(conf_entrylist_t *list)
2495431deaa0Shylee {
2496431deaa0Shylee 	conf_entrylist_t *next;
2497431deaa0Shylee 
2498431deaa0Shylee 	while (list != NULL) {
2499431deaa0Shylee 		next = list->next;
2500431deaa0Shylee 		free_entry(list->entry);
2501431deaa0Shylee 		free(list);
2502431deaa0Shylee 		list = next;
2503431deaa0Shylee 	}
2504431deaa0Shylee }
2505431deaa0Shylee 
2506431deaa0Shylee static KMF_RETURN
2507431deaa0Shylee parse_entry(char *buf, conf_entry_t **entry)
2508431deaa0Shylee {
2509431deaa0Shylee 	KMF_RETURN ret = KMF_OK;
2510431deaa0Shylee 	conf_entry_t *tmp = NULL;
2511431deaa0Shylee 	char *token1;
2512431deaa0Shylee 	char *token2;
2513431deaa0Shylee 	char *token3;
2514431deaa0Shylee 	char *lasts;
2515431deaa0Shylee 	char *value;
2516431deaa0Shylee 
2517431deaa0Shylee 	if ((token1 = strtok_r(buf, SEP_COLON, &lasts)) == NULL)
2518431deaa0Shylee 		return (KMF_ERR_KMF_CONF);
2519431deaa0Shylee 
2520431deaa0Shylee 	if ((tmp = calloc(sizeof (conf_entry_t), 1)) == NULL)
2521431deaa0Shylee 		return (KMF_ERR_MEMORY);
2522431deaa0Shylee 
2523431deaa0Shylee 	if ((tmp->keystore = strdup(token1)) == NULL) {
2524431deaa0Shylee 		ret = KMF_ERR_MEMORY;
2525431deaa0Shylee 		goto end;
2526431deaa0Shylee 	}
2527431deaa0Shylee 
2528431deaa0Shylee 	if ((token2 = strtok_r(NULL, SEP_SEMICOLON, &lasts)) == NULL) {
2529431deaa0Shylee 		ret = KMF_ERR_KMF_CONF;
2530431deaa0Shylee 		goto end;
2531431deaa0Shylee 	}
2532431deaa0Shylee 
2533431deaa0Shylee 	/* need to get token3 first to satisfy nested strtok invocations */
2534431deaa0Shylee 	token3 = strtok_r(NULL, SEP_SEMICOLON, &lasts);
2535431deaa0Shylee 
2536431deaa0Shylee 	/* parse token2 */
2537431deaa0Shylee 	if (strncmp(token2, CONF_MODULEPATH, strlen(CONF_MODULEPATH)) != 0) {
2538431deaa0Shylee 		ret = KMF_ERR_KMF_CONF;
2539431deaa0Shylee 		goto end;
2540431deaa0Shylee 	}
2541431deaa0Shylee 
2542431deaa0Shylee 	if (value = strpbrk(token2, SEP_EQUAL)) {
2543431deaa0Shylee 		value++; /* get rid of = */
2544431deaa0Shylee 	} else {
2545431deaa0Shylee 		ret = KMF_ERR_KMF_CONF;
2546431deaa0Shylee 		goto end;
2547431deaa0Shylee 	}
2548431deaa0Shylee 
2549431deaa0Shylee 	if ((tmp->modulepath = strdup(value)) == NULL) {
2550431deaa0Shylee 		ret = KMF_ERR_MEMORY;
2551431deaa0Shylee 		goto end;
2552431deaa0Shylee 	}
2553431deaa0Shylee 
2554431deaa0Shylee 	/* parse token3, if it exists */
2555431deaa0Shylee 	if (token3 != NULL) {
2556431deaa0Shylee 		if (strncmp(token3, CONF_OPTION, strlen(CONF_OPTION))
2557431deaa0Shylee 		    != 0) {
2558431deaa0Shylee 			ret = KMF_ERR_KMF_CONF;
2559431deaa0Shylee 			goto end;
2560431deaa0Shylee 		}
2561431deaa0Shylee 
2562431deaa0Shylee 		if (value = strpbrk(token3, SEP_EQUAL)) {
2563431deaa0Shylee 			value++; /* get rid of = */
2564431deaa0Shylee 		} else {
2565431deaa0Shylee 			ret = KMF_ERR_KMF_CONF;
2566431deaa0Shylee 			goto end;
2567431deaa0Shylee 		}
2568431deaa0Shylee 
2569431deaa0Shylee 		if ((tmp->option = strdup(value)) == NULL) {
2570431deaa0Shylee 			ret = KMF_ERR_MEMORY;
2571431deaa0Shylee 			goto end;
2572431deaa0Shylee 		}
2573431deaa0Shylee 	}
2574431deaa0Shylee 
2575431deaa0Shylee 	*entry = tmp;
2576431deaa0Shylee 
2577431deaa0Shylee end:
2578431deaa0Shylee 	if (ret != KMF_OK) {
2579431deaa0Shylee 		free_entry(tmp);
2580431deaa0Shylee 		free(tmp);
2581431deaa0Shylee 	}
2582431deaa0Shylee 	return (ret);
2583431deaa0Shylee }
2584431deaa0Shylee 
2585431deaa0Shylee 
2586431deaa0Shylee conf_entry_t *
2587431deaa0Shylee dup_entry(conf_entry_t *entry)
2588431deaa0Shylee {
2589431deaa0Shylee 	conf_entry_t *rtn_entry;
2590431deaa0Shylee 
2591431deaa0Shylee 	if (entry == NULL)
2592431deaa0Shylee 		return (NULL);
2593431deaa0Shylee 
2594431deaa0Shylee 	rtn_entry = malloc(sizeof (conf_entry_t));
2595431deaa0Shylee 	if (rtn_entry == NULL)
2596431deaa0Shylee 		return (NULL);
2597431deaa0Shylee 
2598431deaa0Shylee 	if ((rtn_entry->keystore = strdup(entry->keystore)) == NULL)
2599431deaa0Shylee 		goto out;
2600431deaa0Shylee 
2601431deaa0Shylee 	if ((rtn_entry->modulepath = strdup(entry->modulepath)) == NULL)
2602431deaa0Shylee 		goto out;
2603431deaa0Shylee 
2604431deaa0Shylee 	if (entry->option != NULL &&
2605431deaa0Shylee 	    (rtn_entry->option = strdup(entry->modulepath)) == NULL)
2606431deaa0Shylee 		goto out;
2607431deaa0Shylee 
2608431deaa0Shylee 	return (rtn_entry);
2609431deaa0Shylee 
2610431deaa0Shylee out:
2611431deaa0Shylee 	free_entry(rtn_entry);
2612431deaa0Shylee 	return (NULL);
2613431deaa0Shylee }
2614431deaa0Shylee 
2615431deaa0Shylee 
2616431deaa0Shylee /*
2617431deaa0Shylee  * This function takes a keystore_name as input and returns
2618431deaa0Shylee  * the KMF_KEYSTORE_TYPE value assigned to it.  If the "option"
2619431deaa0Shylee  * argument is not NULL, this function also returns the option string
2620431deaa0Shylee  * if there is an option string for the plugin module.
2621431deaa0Shylee  */
2622431deaa0Shylee KMF_RETURN
2623431deaa0Shylee kmf_get_plugin_info(KMF_HANDLE_T handle, char *keystore_name,
2624431deaa0Shylee     KMF_KEYSTORE_TYPE *kstype, char **option)
2625431deaa0Shylee {
2626431deaa0Shylee 	KMF_RETURN ret = KMF_OK;
2627431deaa0Shylee 	conf_entrylist_t  *phead = extra_plugin_list;
2628431deaa0Shylee 	boolean_t is_default = B_TRUE;
2629431deaa0Shylee 
2630431deaa0Shylee 	/*
2631431deaa0Shylee 	 * Although handle is not really used in the function, we will
2632431deaa0Shylee 	 * check the handle to make sure that kmf_intialize() is called
2633431deaa0Shylee 	 * before this function.
2634431deaa0Shylee 	 */
2635431deaa0Shylee 	if (handle == NULL || keystore_name == NULL || kstype == NULL)
2636431deaa0Shylee 		return (KMF_ERR_BAD_PARAMETER);
2637431deaa0Shylee 
2638431deaa0Shylee 	if (strcmp(keystore_name, "pkcs11") == 0) {
2639431deaa0Shylee 		*kstype = KMF_KEYSTORE_PK11TOKEN;
2640431deaa0Shylee 	} else if (strcmp(keystore_name, "file") == 0) {
2641431deaa0Shylee 		*kstype = KMF_KEYSTORE_OPENSSL;
2642431deaa0Shylee 	} else if (strcmp(keystore_name, "nss") == 0) {
2643431deaa0Shylee 		*kstype = KMF_KEYSTORE_NSS;
2644431deaa0Shylee 	} else {
2645431deaa0Shylee 		is_default = B_FALSE;
2646431deaa0Shylee 	}
2647431deaa0Shylee 
2648431deaa0Shylee 	if (is_default) {
2649431deaa0Shylee 		if (option != NULL)
2650431deaa0Shylee 			*option = NULL;
2651431deaa0Shylee 		goto out;
2652431deaa0Shylee 	}
2653431deaa0Shylee 
2654431deaa0Shylee 	/* Not a built-in plugin; check if it is in extra_plugin_list. */
2655431deaa0Shylee 	while (phead != NULL) {
2656431deaa0Shylee 		if (strcmp(phead->entry->keystore, keystore_name) == 0)
2657431deaa0Shylee 			break;
2658431deaa0Shylee 		phead = phead->next;
2659431deaa0Shylee 	}
2660431deaa0Shylee 
2661431deaa0Shylee 	if (phead == NULL) {
2662431deaa0Shylee 		ret = KMF_ERR_PLUGIN_NOTFOUND;
2663431deaa0Shylee 		goto out;
2664431deaa0Shylee 	}
2665431deaa0Shylee 
2666431deaa0Shylee 	/* found it */
2667431deaa0Shylee 	*kstype = phead->entry->kstype;
2668431deaa0Shylee 	if (option != NULL) {
2669431deaa0Shylee 		if (phead->entry->option == NULL)
2670431deaa0Shylee 			*option = NULL;
2671431deaa0Shylee 		else {
2672431deaa0Shylee 			*option = strdup(phead->entry->option);
2673431deaa0Shylee 			if (*option == NULL) {
2674431deaa0Shylee 				ret = KMF_ERR_MEMORY;
2675431deaa0Shylee 				goto out;
2676431deaa0Shylee 			}
2677431deaa0Shylee 		}
2678431deaa0Shylee 	}
2679431deaa0Shylee 
2680431deaa0Shylee out:
2681431deaa0Shylee 	return (ret);
2682431deaa0Shylee }
2683431deaa0Shylee 
2684431deaa0Shylee /*
2685431deaa0Shylee  * Retrieve the non-default plugin list from the kmf.conf file.
2686431deaa0Shylee  */
2687431deaa0Shylee KMF_RETURN
2688431deaa0Shylee get_entrylist(conf_entrylist_t **entlist)
2689431deaa0Shylee {
2690431deaa0Shylee 	KMF_RETURN rv = KMF_OK;
2691431deaa0Shylee 	FILE *pfile;
2692431deaa0Shylee 	conf_entry_t *entry;
2693431deaa0Shylee 	conf_entrylist_t *rtnlist = NULL;
2694431deaa0Shylee 	conf_entrylist_t *ptmp;
2695431deaa0Shylee 	conf_entrylist_t *pcur;
2696431deaa0Shylee 	char buffer[MAXPATHLEN];
2697431deaa0Shylee 	size_t len;
2698431deaa0Shylee 
2699431deaa0Shylee 	if ((pfile = fopen(_PATH_KMF_CONF, "rF")) == NULL) {
2700431deaa0Shylee 		cryptoerror(LOG_ERR, "failed to open %s.\n", _PATH_KMF_CONF);
2701431deaa0Shylee 		return (KMF_ERR_KMF_CONF);
2702431deaa0Shylee 	}
2703431deaa0Shylee 
2704431deaa0Shylee 	while (fgets(buffer, MAXPATHLEN, pfile) != NULL) {
2705431deaa0Shylee 		if (buffer[0] == '#' || buffer[0] == ' ' ||
2706431deaa0Shylee 		    buffer[0] == '\n'|| buffer[0] == '\t') {
2707431deaa0Shylee 			continue;   /* ignore comment lines */
2708431deaa0Shylee 		}
2709431deaa0Shylee 
2710431deaa0Shylee 		len = strlen(buffer);
2711431deaa0Shylee 		if (buffer[len-1] == '\n') { /* get rid of trailing '\n' */
2712431deaa0Shylee 			len--;
2713431deaa0Shylee 		}
2714431deaa0Shylee 		buffer[len] = '\0';
2715431deaa0Shylee 
2716431deaa0Shylee 		rv = parse_entry(buffer, &entry);
2717431deaa0Shylee 		if (rv != KMF_OK) {
2718431deaa0Shylee 			goto end;
2719431deaa0Shylee 		}
2720431deaa0Shylee 
2721431deaa0Shylee 		if ((ptmp = malloc(sizeof (conf_entrylist_t))) == NULL) {
2722431deaa0Shylee 			rv = KMF_ERR_MEMORY;
2723431deaa0Shylee 			goto end;
2724431deaa0Shylee 		}
2725431deaa0Shylee 		ptmp->entry = entry;
2726431deaa0Shylee 		ptmp->next = NULL;
2727431deaa0Shylee 
2728431deaa0Shylee 		if (rtnlist == NULL) {
2729431deaa0Shylee 			rtnlist = pcur = ptmp;
2730431deaa0Shylee 		} else {
2731431deaa0Shylee 			pcur->next = ptmp;
2732431deaa0Shylee 			pcur = ptmp;
2733431deaa0Shylee 		}
2734431deaa0Shylee 	}
2735431deaa0Shylee 
2736431deaa0Shylee end:
2737431deaa0Shylee 	(void) fclose(pfile);
2738431deaa0Shylee 
2739431deaa0Shylee 	if (rv == KMF_OK) {
2740431deaa0Shylee 		*entlist = rtnlist;
2741431deaa0Shylee 	} else if (rtnlist != NULL) {
2742431deaa0Shylee 		free_entrylist(rtnlist);
2743431deaa0Shylee 		*entlist = NULL;
2744431deaa0Shylee 		kstore_num = DEFAULT_KEYSTORE_NUM;
2745431deaa0Shylee 	}
2746431deaa0Shylee 
2747431deaa0Shylee 	return (rv);
2748431deaa0Shylee }
2749431deaa0Shylee 
2750431deaa0Shylee 
2751431deaa0Shylee boolean_t
2752431deaa0Shylee is_valid_keystore_type(KMF_KEYSTORE_TYPE kstype)
2753431deaa0Shylee {
2754431deaa0Shylee 
2755431deaa0Shylee 	if (kstype > 0 && kstype <= kstore_num)
2756431deaa0Shylee 		return (B_TRUE);
2757431deaa0Shylee 	else
2758431deaa0Shylee 		return (B_FALSE);
2759431deaa0Shylee }
2760