199ebb4caSwyllys /*
299ebb4caSwyllys  * CDDL HEADER START
399ebb4caSwyllys  *
499ebb4caSwyllys  * The contents of this file are subject to the terms of the
599ebb4caSwyllys  * Common Development and Distribution License (the "License").
699ebb4caSwyllys  * You may not use this file except in compliance with the License.
799ebb4caSwyllys  *
899ebb4caSwyllys  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
999ebb4caSwyllys  * or http://www.opensolaris.org/os/licensing.
1099ebb4caSwyllys  * See the License for the specific language governing permissions
1199ebb4caSwyllys  * and limitations under the License.
1299ebb4caSwyllys  *
1399ebb4caSwyllys  * When distributing Covered Code, include this CDDL HEADER in each
1499ebb4caSwyllys  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1599ebb4caSwyllys  * If applicable, add the following below this CDDL HEADER, with the
1699ebb4caSwyllys  * fields enclosed by brackets "[]" replaced with your own identifying
1799ebb4caSwyllys  * information: Portions Copyright [yyyy] [name of copyright owner]
1899ebb4caSwyllys  *
1999ebb4caSwyllys  * CDDL HEADER END
2099ebb4caSwyllys  */
2199ebb4caSwyllys /*
22*2225707cSValerie Bubb Fenwick  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
2399ebb4caSwyllys  * Use is subject to license terms.
2499ebb4caSwyllys  *
2599ebb4caSwyllys  */
2699ebb4caSwyllys 
2799ebb4caSwyllys #include <stdio.h>
2899ebb4caSwyllys #include <dlfcn.h>
2999ebb4caSwyllys #include <link.h>
3099ebb4caSwyllys #include <fcntl.h>
3199ebb4caSwyllys #include <ctype.h>
3299ebb4caSwyllys #include <sys/param.h>
3399ebb4caSwyllys #include <sys/types.h>
3499ebb4caSwyllys #include <sys/stat.h>
3599ebb4caSwyllys #include <errno.h>
3699ebb4caSwyllys #include <sys/socket.h>
3799ebb4caSwyllys #include <netinet/in.h>
3899ebb4caSwyllys #include <arpa/inet.h>
3999ebb4caSwyllys #include <thread.h>
4099ebb4caSwyllys 
4199ebb4caSwyllys #include <ber_der.h>
4299ebb4caSwyllys #include <kmfapiP.h>
4399ebb4caSwyllys 
4499ebb4caSwyllys #include <pem_encode.h>
4599ebb4caSwyllys #include <rdn_parser.h>
4699ebb4caSwyllys #include <libxml2/libxml/uri.h>
4799ebb4caSwyllys #include <libgen.h>
4899ebb4caSwyllys #include <cryptoutil.h>
4999ebb4caSwyllys 
5099ebb4caSwyllys static uchar_t pkcs11_initialized = 0;
5199ebb4caSwyllys mutex_t init_lock = DEFAULTMUTEX;
5299ebb4caSwyllys extern int errno;
5399ebb4caSwyllys 
5499ebb4caSwyllys typedef struct {
5599ebb4caSwyllys 	KMF_RETURN code;
5699ebb4caSwyllys 	char	*message;
5799ebb4caSwyllys } kmf_error_map;
5899ebb4caSwyllys 
5999ebb4caSwyllys static kmf_error_map kmf_errcodes[] = {
6099ebb4caSwyllys 	{KMF_OK,	"KMF_OK"},
6199ebb4caSwyllys 	{KMF_ERR_BAD_PARAMETER,	"KMF_ERR_BAD_PARAMETER"},
6299ebb4caSwyllys 	{KMF_ERR_BAD_KEY_FORMAT,	"KMF_ERR_BAD_KEY_FORMAT"},
6399ebb4caSwyllys 	{KMF_ERR_BAD_ALGORITHM,	"KMF_ERR_BAD_ALGORITHM"},
6499ebb4caSwyllys 	{KMF_ERR_MEMORY,	"KMF_ERR_MEMORY"},
6599ebb4caSwyllys 	{KMF_ERR_ENCODING,	"KMF_ERR_ENCODING"},
6699ebb4caSwyllys 	{KMF_ERR_PLUGIN_INIT,	"KMF_ERR_PLUGIN_INIT"},
6799ebb4caSwyllys 	{KMF_ERR_PLUGIN_NOTFOUND,	"KMF_ERR_PLUGIN_NOTFOUND"},
6899ebb4caSwyllys 	{KMF_ERR_INTERNAL,	"KMF_ERR_INTERNAL"},
6999ebb4caSwyllys 	{KMF_ERR_BAD_CERT_FORMAT,	"KMF_ERR_BAD_CERT_FORMAT"},
7099ebb4caSwyllys 	{KMF_ERR_KEYGEN_FAILED,	"KMF_ERR_KEYGEN_FAILED"},
7199ebb4caSwyllys 	{KMF_ERR_UNINITIALIZED,	"KMF_ERR_UNINITIALIZED"},
7299ebb4caSwyllys 	{KMF_ERR_ISSUER,	"KMF_ERR_ISSUER"},
7399ebb4caSwyllys 	{KMF_ERR_NOT_REVOKED,	"KMF_ERR_NOT_REVOKED"},
7499ebb4caSwyllys 	{KMF_ERR_CERT_NOT_FOUND,	"KMF_ERR_CERT_NOT_FOUND"},
7599ebb4caSwyllys 	{KMF_ERR_CRL_NOT_FOUND,	"KMF_ERR_CRL_NOT_FOUND"},
7699ebb4caSwyllys 	{KMF_ERR_RDN_PARSER,	"KMF_ERR_RDN_PARSER"},
7799ebb4caSwyllys 	{KMF_ERR_RDN_ATTR,	"KMF_ERR_RDN_ATTR"},
7899ebb4caSwyllys 	{KMF_ERR_SLOTNAME,	"KMF_ERR_SLOTNAME"},
7999ebb4caSwyllys 	{KMF_ERR_EMPTY_CRL,	"KMF_ERR_EMPTY_CRL"},
8099ebb4caSwyllys 	{KMF_ERR_BUFFER_SIZE,	"KMF_ERR_BUFFER_SIZE"},
8199ebb4caSwyllys 	{KMF_ERR_AUTH_FAILED,	"KMF_ERR_AUTH_FAILED"},
8299ebb4caSwyllys 	{KMF_ERR_TOKEN_SELECTED,	"KMF_ERR_TOKEN_SELECTED"},
8399ebb4caSwyllys 	{KMF_ERR_NO_TOKEN_SELECTED,	"KMF_ERR_NO_TOKEN_SELECTED"},
8499ebb4caSwyllys 	{KMF_ERR_TOKEN_NOT_PRESENT,	"KMF_ERR_TOKEN_NOT_PRESENT"},
8599ebb4caSwyllys 	{KMF_ERR_EXTENSION_NOT_FOUND,	"KMF_ERR_EXTENSION_NOT_FOUND"},
8699ebb4caSwyllys 	{KMF_ERR_POLICY_ENGINE,	"KMF_ERR_POLICY_ENGINE"},
8799ebb4caSwyllys 	{KMF_ERR_POLICY_DB_FORMAT,	"KMF_ERR_POLICY_DB_FORMAT"},
8899ebb4caSwyllys 	{KMF_ERR_POLICY_NOT_FOUND,	"KMF_ERR_POLICY_NOT_FOUND"},
8999ebb4caSwyllys 	{KMF_ERR_POLICY_DB_FILE,	"KMF_ERR_POLICY_DB_FILE"},
9099ebb4caSwyllys 	{KMF_ERR_POLICY_NAME,	"KMF_ERR_POLICY_NAME"},
9199ebb4caSwyllys 	{KMF_ERR_OCSP_POLICY,	"KMF_ERR_OCSP_POLICY"},
9299ebb4caSwyllys 	{KMF_ERR_TA_POLICY,	"KMF_ERR_TA_POLICY"},
9399ebb4caSwyllys 	{KMF_ERR_KEY_NOT_FOUND,	"KMF_ERR_KEY_NOT_FOUND"},
9499ebb4caSwyllys 	{KMF_ERR_OPEN_FILE,	"KMF_ERR_OPEN_FILE"},
9599ebb4caSwyllys 	{KMF_ERR_OCSP_BAD_ISSUER,	"KMF_ERR_OCSP_BAD_ISSUER"},
9699ebb4caSwyllys 	{KMF_ERR_OCSP_BAD_CERT,	"KMF_ERR_OCSP_BAD_CERT"},
9799ebb4caSwyllys 	{KMF_ERR_OCSP_CREATE_REQUEST,	"KMF_ERR_OCSP_CREATE_REQUEST"},
9899ebb4caSwyllys 	{KMF_ERR_CONNECT_SERVER,	"KMF_ERR_CONNECT_SERVER"},
9999ebb4caSwyllys 	{KMF_ERR_SEND_REQUEST,	"KMF_ERR_SEND_REQUEST"},
10099ebb4caSwyllys 	{KMF_ERR_OCSP_CERTID,	"KMF_ERR_OCSP_CERTID"},
10199ebb4caSwyllys 	{KMF_ERR_OCSP_MALFORMED_RESPONSE, "KMF_ERR_OCSP_MALFORMED_RESPONSE"},
10299ebb4caSwyllys 	{KMF_ERR_OCSP_RESPONSE_STATUS,	"KMF_ERR_OCSP_RESPONSE_STATUS"},
10399ebb4caSwyllys 	{KMF_ERR_OCSP_NO_BASIC_RESPONSE, "KMF_ERR_OCSP_NO_BASIC_RESPONSE"},
10499ebb4caSwyllys 	{KMF_ERR_OCSP_BAD_SIGNER,	"KMF_ERR_OCSP_BAD_SIGNER"},
10599ebb4caSwyllys 	{KMF_ERR_OCSP_RESPONSE_SIGNATURE, "KMF_ERR_OCSP_RESPONSE_SIGNATURE"},
10699ebb4caSwyllys 	{KMF_ERR_OCSP_UNKNOWN_CERT,	"KMF_ERR_OCSP_UNKNOWN_CERT"},
10799ebb4caSwyllys 	{KMF_ERR_OCSP_STATUS_TIME_INVALID, "KMF_ERR_OCSP_STATUS_TIME_INVALID"},
10899ebb4caSwyllys 	{KMF_ERR_BAD_HTTP_RESPONSE,	"KMF_ERR_BAD_HTTP_RESPONSE"},
10999ebb4caSwyllys 	{KMF_ERR_RECV_RESPONSE,	"KMF_ERR_RECV_RESPONSE"},
11099ebb4caSwyllys 	{KMF_ERR_RECV_TIMEOUT,	"KMF_ERR_RECV_TIMEOUT"},
11199ebb4caSwyllys 	{KMF_ERR_DUPLICATE_KEYFILE,	"KMF_ERR_DUPLICATE_KEYFILE"},
11299ebb4caSwyllys 	{KMF_ERR_AMBIGUOUS_PATHNAME,	"KMF_ERR_AMBIGUOUS_PATHNAME"},
11399ebb4caSwyllys 	{KMF_ERR_FUNCTION_NOT_FOUND,	"KMF_ERR_FUNCTION_NOT_FOUND"},
11499ebb4caSwyllys 	{KMF_ERR_PKCS12_FORMAT,	"KMF_ERR_PKCS12_FORMAT"},
11599ebb4caSwyllys 	{KMF_ERR_BAD_KEY_TYPE,	"KMF_ERR_BAD_KEY_TYPE"},
11699ebb4caSwyllys 	{KMF_ERR_BAD_KEY_CLASS,	"KMF_ERR_BAD_KEY_CLASS"},
11799ebb4caSwyllys 	{KMF_ERR_BAD_KEY_SIZE,	"KMF_ERR_BAD_KEY_SIZE"},
11899ebb4caSwyllys 	{KMF_ERR_BAD_HEX_STRING,	"KMF_ERR_BAD_HEX_STRING"},
11999ebb4caSwyllys 	{KMF_ERR_KEYUSAGE,	"KMF_ERR_KEYUSAGE"},
12099ebb4caSwyllys 	{KMF_ERR_VALIDITY_PERIOD,	"KMF_ERR_VALIDITY_PERIOD"},
12199ebb4caSwyllys 	{KMF_ERR_OCSP_REVOKED,	"KMF_ERR_OCSP_REVOKED"},
12299ebb4caSwyllys 	{KMF_ERR_CERT_MULTIPLE_FOUND,	"KMF_ERR_CERT_MULTIPLE_FOUND"},
12399ebb4caSwyllys 	{KMF_ERR_WRITE_FILE,	"KMF_ERR_WRITE_FILE"},
12499ebb4caSwyllys 	{KMF_ERR_BAD_URI,	"KMF_ERR_BAD_URI"},
12599ebb4caSwyllys 	{KMF_ERR_BAD_CRLFILE,	"KMF_ERR_BAD_CRLFILE"},
12699ebb4caSwyllys 	{KMF_ERR_BAD_CERTFILE,	"KMF_ERR_BAD_CERTFILE"},
12799ebb4caSwyllys 	{KMF_ERR_GETKEYVALUE_FAILED,	"KMF_ERR_GETKEYVALUE_FAILED"},
12899ebb4caSwyllys 	{KMF_ERR_BAD_KEYHANDLE,	"KMF_ERR_BAD_KEYHANDLE"},
12999ebb4caSwyllys 	{KMF_ERR_BAD_OBJECT_TYPE,	"KMF_ERR_BAD_OBJECT_TYPE"},
13099ebb4caSwyllys 	{KMF_ERR_OCSP_RESPONSE_LIFETIME, "KMF_ERR_OCSP_RESPONSE_LIFETIME"},
13199ebb4caSwyllys 	{KMF_ERR_UNKNOWN_CSR_ATTRIBUTE,	"KMF_ERR_UNKNOWN_CSR_ATTRIBUTE"},
13299ebb4caSwyllys 	{KMF_ERR_UNINITIALIZED_TOKEN,	"KMF_ERR_UNINITIALIZED_TOKEN"},
13399ebb4caSwyllys 	{KMF_ERR_INCOMPLETE_TBS_CERT,	"KMF_ERR_INCOMPLETE_TBS_CERT"},
13499ebb4caSwyllys 	{KMF_ERR_MISSING_ERRCODE,	"KMF_ERR_MISSING_ERRCODE"},
13571593db2Swyllys 	{KMF_KEYSTORE_ALREADY_INITIALIZED, "KMF_KEYSTORE_ALREADY_INITIALIZED"},
13671593db2Swyllys 	{KMF_ERR_SENSITIVE_KEY,		"KMF_ERR_SENSITIVE_KEY"},
13771593db2Swyllys 	{KMF_ERR_UNEXTRACTABLE_KEY,	"KMF_ERR_UNEXTRACTABLE_KEY"},
138d00756ccSwyllys 	{KMF_ERR_KEY_MISMATCH,		"KMF_ERR_KEY_MISMATCH"},
139d00756ccSwyllys 	{KMF_ERR_ATTR_NOT_FOUND,	"KMF_ERR_ATTR_NOT_FOUND"},
140d00756ccSwyllys 	{KMF_ERR_KMF_CONF,		"KMF_ERR_KMF_CONF"}
14199ebb4caSwyllys };
14299ebb4caSwyllys 
14385b65b39Swyllys typedef struct {
14485b65b39Swyllys 	KMF_KEYSTORE_TYPE	kstype;
14585b65b39Swyllys 	char			*path;
14685b65b39Swyllys 	boolean_t		critical;
14785b65b39Swyllys } KMF_PLUGIN_ITEM;
14885b65b39Swyllys 
14985b65b39Swyllys KMF_PLUGIN_ITEM plugin_list[] = {
15085b65b39Swyllys 	{KMF_KEYSTORE_OPENSSL,	KMF_PLUGIN_PATH "kmf_openssl.so.1",  TRUE},
15185b65b39Swyllys 	{KMF_KEYSTORE_PK11TOKEN, KMF_PLUGIN_PATH "kmf_pkcs11.so.1",  TRUE},
15285b65b39Swyllys 	{KMF_KEYSTORE_NSS,	KMF_PLUGIN_PATH "kmf_nss.so.1",  FALSE}
15385b65b39Swyllys };
15499ebb4caSwyllys 
15530a5e8faSwyllys 
15630a5e8faSwyllys 
15790c85bf8Swyllys static KMF_RETURN InitializePlugin(KMF_KEYSTORE_TYPE, char *, KMF_PLUGIN **);
15890c85bf8Swyllys static KMF_RETURN AddPlugin(KMF_HANDLE_T, KMF_PLUGIN *);
15999ebb4caSwyllys static void free_extensions(KMF_X509_EXTENSIONS *extns);
16090c85bf8Swyllys static void DestroyPlugin(KMF_PLUGIN *);
16199ebb4caSwyllys 
162431deaa0Shylee #if defined(__sparcv9)
163431deaa0Shylee #define	ISA_PATH	"/sparcv9"
164431deaa0Shylee #elif defined(__sparc)
165431deaa0Shylee #define	ISA_PATH	"/"
166431deaa0Shylee #elif defined(__i386)
167431deaa0Shylee #define	ISA_PATH	"/"
168431deaa0Shylee #elif defined(__amd64)
169431deaa0Shylee #define	ISA_PATH	"/amd64"
170431deaa0Shylee #endif
171431deaa0Shylee 
172431deaa0Shylee #define	DEFAULT_KEYSTORE_NUM	3
173431deaa0Shylee static int kstore_num = DEFAULT_KEYSTORE_NUM;
174431deaa0Shylee conf_entrylist_t *extra_plugin_list = NULL;
175431deaa0Shylee static boolean_t check_extra_plugin = B_FALSE;
176431deaa0Shylee mutex_t extra_plugin_lock = DEFAULTMUTEX;
177431deaa0Shylee 
1789b37d296Swyllys KMF_RETURN
1799b37d296Swyllys init_pk11()
18099ebb4caSwyllys {
1819b37d296Swyllys 	(void) mutex_lock(&init_lock);
1829b37d296Swyllys 	if (!pkcs11_initialized) {
1839b37d296Swyllys 		CK_RV rv = C_Initialize(NULL);
1849b37d296Swyllys 		if ((rv != CKR_OK) &&
1859b37d296Swyllys 		    (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED)) {
1869b37d296Swyllys 			(void) mutex_unlock(&init_lock);
1879b37d296Swyllys 			return (KMF_ERR_UNINITIALIZED);
1889b37d296Swyllys 		} else {
1899b37d296Swyllys 			pkcs11_initialized = 1;
1909b37d296Swyllys 		}
1919b37d296Swyllys 	}
1929b37d296Swyllys 	(void) mutex_unlock(&init_lock);
1939b37d296Swyllys 	return (KMF_OK);
19499ebb4caSwyllys }
19599ebb4caSwyllys 
19699ebb4caSwyllys /*
19799ebb4caSwyllys  * Private method for searching the plugin list for the correct
19899ebb4caSwyllys  * Plugin to use.
19999ebb4caSwyllys  */
20099ebb4caSwyllys KMF_PLUGIN *
20199ebb4caSwyllys FindPlugin(KMF_HANDLE_T handle, KMF_KEYSTORE_TYPE kstype)
20299ebb4caSwyllys {
20399ebb4caSwyllys 	KMF_PLUGIN_LIST *node;
20490c85bf8Swyllys 	KMF_RETURN ret = KMF_OK;
205431deaa0Shylee 	KMF_PLUGIN *pluginrec = NULL;
20699ebb4caSwyllys 
20799ebb4caSwyllys 	if (handle == NULL)
20899ebb4caSwyllys 		return (NULL);
20999ebb4caSwyllys 
21099ebb4caSwyllys 	node = handle->plugins;
21199ebb4caSwyllys 
21290c85bf8Swyllys 	/* See if the desired plugin was already initialized. */
21399ebb4caSwyllys 	while (node != NULL && node->plugin->type != kstype)
21499ebb4caSwyllys 		node = node->next;
21599ebb4caSwyllys 
216431deaa0Shylee 	if (node != NULL)
217431deaa0Shylee 		return (node->plugin);
218431deaa0Shylee 
219431deaa0Shylee 	/* The plugin was not found, try to initialize it here. */
220431deaa0Shylee 	if (VALID_DEFAULT_KEYSTORE_TYPE(kstype)) {
22190c85bf8Swyllys 		int i;
22290c85bf8Swyllys 		int numitems = sizeof (plugin_list)/sizeof (KMF_PLUGIN_ITEM);
22390c85bf8Swyllys 		for (i = 0; i < numitems; i++) {
22490c85bf8Swyllys 			if (plugin_list[i].kstype == kstype) {
22590c85bf8Swyllys 				ret = InitializePlugin(plugin_list[i].kstype,
22690c85bf8Swyllys 				    plugin_list[i].path, &pluginrec);
22790c85bf8Swyllys 				break;
22890c85bf8Swyllys 			}
22990c85bf8Swyllys 		}
23090c85bf8Swyllys 
231431deaa0Shylee 		goto out;
232431deaa0Shylee 
233431deaa0Shylee 	} else {
234431deaa0Shylee 		/*
235431deaa0Shylee 		 * Not a built-in plugin. Check if it is in the
236431deaa0Shylee 		 * extra_plugin_list.  If it is, try to initialize it here.
237431deaa0Shylee 		 */
238431deaa0Shylee 		conf_entrylist_t *phead = extra_plugin_list;
239431deaa0Shylee 		char realpath[MAXPATHLEN];
240431deaa0Shylee 
241431deaa0Shylee 		while (phead != NULL) {
242431deaa0Shylee 			if (phead->entry->kstype == kstype)
243431deaa0Shylee 				break;
244431deaa0Shylee 			else
245431deaa0Shylee 				phead = phead->next;
246431deaa0Shylee 		}
247431deaa0Shylee 
248431deaa0Shylee 		if (phead == NULL)
24990c85bf8Swyllys 			return (NULL);
25090c85bf8Swyllys 
251431deaa0Shylee 		/*
252431deaa0Shylee 		 * Get the absolute path of the module.
253431deaa0Shylee 		 * - If modulepath is not a full path, then prepend it
254431deaa0Shylee 		 *   with KMF_PLUGIN_PATH.
255431deaa0Shylee 		 * - If modulepath is a full path and contain $ISA, then
256431deaa0Shylee 		 *   subsitute the architecture dependent path.
257431deaa0Shylee 		 */
258431deaa0Shylee 		(void) memset(realpath, 0, sizeof (realpath));
259431deaa0Shylee 		if (strncmp(phead->entry->modulepath, "/", 1) != 0) {
260431deaa0Shylee 			(void) snprintf(realpath, MAXPATHLEN, "%s%s",
261431deaa0Shylee 			    KMF_PLUGIN_PATH, phead->entry->modulepath);
262431deaa0Shylee 		} else {
263431deaa0Shylee 			char *buf = phead->entry->modulepath;
264431deaa0Shylee 			char *isa;
265431deaa0Shylee 
266431deaa0Shylee 			if ((isa = strstr(buf, PKCS11_ISA)) != NULL) {
267431deaa0Shylee 				char *isa_str;
268431deaa0Shylee 
269431deaa0Shylee 				(void) strncpy(realpath, buf, isa - buf);
270431deaa0Shylee 				isa_str = strdup(ISA_PATH);
271431deaa0Shylee 				if (isa_str == NULL) /* not enough memory */
272431deaa0Shylee 					return (NULL);
273431deaa0Shylee 
274431deaa0Shylee 				(void) strncat(realpath, isa_str,
275431deaa0Shylee 				    strlen(isa_str));
276431deaa0Shylee 				free(isa_str);
277431deaa0Shylee 
278431deaa0Shylee 				isa += strlen(PKCS11_ISA);
279431deaa0Shylee 				(void) strlcat(realpath, isa, MAXPATHLEN);
280431deaa0Shylee 			} else {
281431deaa0Shylee 				(void) snprintf(realpath, MAXPATHLEN, "%s",
282431deaa0Shylee 				    phead->entry->modulepath);
283431deaa0Shylee 			}
28490c85bf8Swyllys 		}
285431deaa0Shylee 
286431deaa0Shylee 		ret = InitializePlugin(phead->entry->kstype, realpath,
287431deaa0Shylee 		    &pluginrec);
288431deaa0Shylee 		goto out;
289431deaa0Shylee 	}
290431deaa0Shylee 
291431deaa0Shylee out:
292431deaa0Shylee 	if (ret != KMF_OK || pluginrec == NULL)
293431deaa0Shylee 		/* No matching plugins found in the built-in list */
294431deaa0Shylee 		return (NULL);
295431deaa0Shylee 
296431deaa0Shylee 	ret = AddPlugin(handle, pluginrec);
297431deaa0Shylee 	if (ret != KMF_OK) {
298431deaa0Shylee 		DestroyPlugin(pluginrec);
299431deaa0Shylee 		pluginrec = NULL;
30090c85bf8Swyllys 	}
301431deaa0Shylee 	return (pluginrec);
30299ebb4caSwyllys }
30399ebb4caSwyllys 
304431deaa0Shylee 
30599ebb4caSwyllys static KMF_RETURN
30699ebb4caSwyllys InitializePlugin(KMF_KEYSTORE_TYPE kstype, char *path, KMF_PLUGIN **plugin)
30799ebb4caSwyllys {
30899ebb4caSwyllys 	KMF_PLUGIN *p = NULL;
30999ebb4caSwyllys 	KMF_PLUGIN_FUNCLIST *(*sym)();
31099ebb4caSwyllys 
31199ebb4caSwyllys 	if (path == NULL || plugin == NULL)
31299ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
31399ebb4caSwyllys 
31499ebb4caSwyllys 	*plugin = NULL;
31599ebb4caSwyllys 
31699ebb4caSwyllys 	p = (KMF_PLUGIN *)malloc(sizeof (KMF_PLUGIN));
31799ebb4caSwyllys 	if (p == NULL)
31899ebb4caSwyllys 		return (KMF_ERR_MEMORY);
31999ebb4caSwyllys 
32099ebb4caSwyllys 	p->type = kstype;
32199ebb4caSwyllys 	p->path = strdup(path);
32299ebb4caSwyllys 	if (p->path == NULL) {
32399ebb4caSwyllys 		free(p);
32499ebb4caSwyllys 		return (KMF_ERR_MEMORY);
32599ebb4caSwyllys 	}
326*2225707cSValerie Bubb Fenwick 	/*
327*2225707cSValerie Bubb Fenwick 	 * Do not use RTLD_GROUP here, or this will cause a circular
328*2225707cSValerie Bubb Fenwick 	 * dependency when kmf_pkcs11.so.1 gets its PKCS#11 functions
329*2225707cSValerie Bubb Fenwick 	 * from libpkcs11.so.1 when kmf is used via libelfsign.so.1
330*2225707cSValerie Bubb Fenwick 	 * called from kcfd.
331*2225707cSValerie Bubb Fenwick 	 */
332*2225707cSValerie Bubb Fenwick 	p->dldesc = dlopen(path, RTLD_LAZY | RTLD_PARENT);
33399ebb4caSwyllys 	if (p->dldesc == NULL) {
33499ebb4caSwyllys 		free(p->path);
33599ebb4caSwyllys 		free(p);
33699ebb4caSwyllys 		return (KMF_ERR_PLUGIN_INIT);
33799ebb4caSwyllys 	}
33899ebb4caSwyllys 
33999ebb4caSwyllys 	sym = (KMF_PLUGIN_FUNCLIST *(*)())dlsym(p->dldesc,
34030a5e8faSwyllys 	    KMF_PLUGIN_INIT_SYMBOL);
34199ebb4caSwyllys 	if (sym == NULL) {
34299ebb4caSwyllys 		(void) dlclose(p->dldesc);
34399ebb4caSwyllys 		free(p->path);
34499ebb4caSwyllys 		free(p);
34599ebb4caSwyllys 		return (KMF_ERR_PLUGIN_INIT);
34699ebb4caSwyllys 	}
34799ebb4caSwyllys 
34899ebb4caSwyllys 	/* Get the function list */
34999ebb4caSwyllys 	if ((p->funclist = (*sym)()) == NULL) {
35099ebb4caSwyllys 		(void) dlclose(p->dldesc);
35199ebb4caSwyllys 		free(p->path);
35299ebb4caSwyllys 		free(p);
35399ebb4caSwyllys 		return (KMF_ERR_PLUGIN_INIT);
35499ebb4caSwyllys 	}
35599ebb4caSwyllys 
35699ebb4caSwyllys 	*plugin = p;
35799ebb4caSwyllys 
35899ebb4caSwyllys 	return (KMF_OK);
35999ebb4caSwyllys }
36099ebb4caSwyllys 
36199ebb4caSwyllys static KMF_RETURN
36299ebb4caSwyllys AddPlugin(KMF_HANDLE_T handle, KMF_PLUGIN *plugin)
36399ebb4caSwyllys {
36499ebb4caSwyllys 	KMF_PLUGIN_LIST *n;
36599ebb4caSwyllys 
36699ebb4caSwyllys 	if (handle == NULL || plugin == NULL)
36799ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
36899ebb4caSwyllys 
36999ebb4caSwyllys 	/* If the head is NULL, create it */
37099ebb4caSwyllys 	if (handle->plugins == NULL) {
37199ebb4caSwyllys 		handle->plugins = (KMF_PLUGIN_LIST *)malloc(
37230a5e8faSwyllys 		    sizeof (KMF_PLUGIN_LIST));
37399ebb4caSwyllys 		if (handle->plugins == NULL)
37499ebb4caSwyllys 			return (KMF_ERR_MEMORY);
37599ebb4caSwyllys 		handle->plugins->plugin = plugin;
37699ebb4caSwyllys 		handle->plugins->next = NULL;
37799ebb4caSwyllys 	} else {
37899ebb4caSwyllys 		/* walk the list to find the tail */
37999ebb4caSwyllys 		n = handle->plugins;
38099ebb4caSwyllys 		while (n->next != NULL)
38199ebb4caSwyllys 			n = n->next;
38299ebb4caSwyllys 		n->next = (KMF_PLUGIN_LIST *)malloc(sizeof (KMF_PLUGIN_LIST));
38399ebb4caSwyllys 		if (n->next == NULL)
38499ebb4caSwyllys 			return (KMF_ERR_MEMORY);
38599ebb4caSwyllys 
38699ebb4caSwyllys 		n->next->plugin = plugin;
38799ebb4caSwyllys 		n->next->next = NULL;
38899ebb4caSwyllys 	}
38999ebb4caSwyllys 	return (0);
39099ebb4caSwyllys }
39199ebb4caSwyllys 
39299ebb4caSwyllys static void
39399ebb4caSwyllys DestroyPlugin(KMF_PLUGIN *plugin)
39499ebb4caSwyllys {
39599ebb4caSwyllys 	if (plugin) {
39699ebb4caSwyllys 		if (plugin->path)
39799ebb4caSwyllys 			free(plugin->path);
39899ebb4caSwyllys 		free(plugin);
39999ebb4caSwyllys 	}
40099ebb4caSwyllys }
40199ebb4caSwyllys 
40299ebb4caSwyllys static void
40399ebb4caSwyllys Cleanup_KMF_Handle(KMF_HANDLE_T handle)
40499ebb4caSwyllys {
40599ebb4caSwyllys 	if (handle != NULL) {
40699ebb4caSwyllys 		while (handle->plugins != NULL) {
40799ebb4caSwyllys 			KMF_PLUGIN_LIST *next = handle->plugins->next;
40899ebb4caSwyllys 
40999ebb4caSwyllys 			DestroyPlugin(handle->plugins->plugin);
41099ebb4caSwyllys 			free(handle->plugins);
41199ebb4caSwyllys 			handle->plugins = next;
41299ebb4caSwyllys 		}
41330a5e8faSwyllys 		kmf_free_policy_record(handle->policy);
41499ebb4caSwyllys 		free(handle->policy);
41599ebb4caSwyllys 	}
41699ebb4caSwyllys 	free(handle);
41799ebb4caSwyllys }
41899ebb4caSwyllys 
41999ebb4caSwyllys void
42099ebb4caSwyllys Cleanup_PK11_Session(KMF_HANDLE_T handle)
42199ebb4caSwyllys {
42299ebb4caSwyllys 	if (handle != NULL) {
42399ebb4caSwyllys 		/* Close active session on a pkcs11 token */
42499ebb4caSwyllys 		if (handle->pk11handle != NULL) {
42599ebb4caSwyllys 			(void) C_CloseSession(handle->pk11handle);
42699ebb4caSwyllys 			handle->pk11handle = NULL;
42799ebb4caSwyllys 		}
42899ebb4caSwyllys 	}
42999ebb4caSwyllys }
43099ebb4caSwyllys 
43199ebb4caSwyllys KMF_RETURN
43230a5e8faSwyllys kmf_initialize(KMF_HANDLE_T *outhandle, char *policyfile, char *policyname)
43399ebb4caSwyllys {
43499ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
43599ebb4caSwyllys 	KMF_HANDLE *handle = NULL;
43699ebb4caSwyllys 
43799ebb4caSwyllys 	if (outhandle == NULL)
43899ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
43999ebb4caSwyllys 
44099ebb4caSwyllys 	*outhandle = NULL;
44199ebb4caSwyllys 	handle = (KMF_HANDLE *)malloc(sizeof (KMF_HANDLE));
44299ebb4caSwyllys 	if (handle == NULL)
44399ebb4caSwyllys 		return (KMF_ERR_MEMORY);
44499ebb4caSwyllys 
44599ebb4caSwyllys 	(void) memset(handle, 0, sizeof (KMF_HANDLE));
44699ebb4caSwyllys 	handle->plugins = NULL;
44799ebb4caSwyllys 
448431deaa0Shylee 	/*
449431deaa0Shylee 	 * When this function is called the first time, get the additional
450431deaa0Shylee 	 * plugins from the config file.
451431deaa0Shylee 	 */
452431deaa0Shylee 	(void) mutex_lock(&extra_plugin_lock);
453431deaa0Shylee 	if (!check_extra_plugin) {
454431deaa0Shylee 
455431deaa0Shylee 		ret = get_entrylist(&extra_plugin_list);
456431deaa0Shylee 		check_extra_plugin = B_TRUE;
457431deaa0Shylee 
458431deaa0Shylee 		/*
459431deaa0Shylee 		 * Assign the kstype number to the additional plugins here.
460431deaa0Shylee 		 * The global kstore_num will be protected by the mutex lock.
461431deaa0Shylee 		 */
462431deaa0Shylee 		if (ret == KMF_OK) {
463431deaa0Shylee 			conf_entrylist_t *phead = extra_plugin_list;
464431deaa0Shylee 			while (phead != NULL) {
465431deaa0Shylee 				phead->entry->kstype = ++kstore_num;
466431deaa0Shylee 				phead = phead->next;
467431deaa0Shylee 			}
468431deaa0Shylee 		}
469431deaa0Shylee 
470431deaa0Shylee 		/*
471431deaa0Shylee 		 * If the KMF configuration file does not exist or cannot be
472431deaa0Shylee 		 * parsed correctly, we will give a warning in syslog and
473431deaa0Shylee 		 * continue on as there is no extra plugins in the system.
474431deaa0Shylee 		 */
475431deaa0Shylee 		if (ret == KMF_ERR_KMF_CONF) {
476431deaa0Shylee 			cryptoerror(LOG_WARNING, "KMF was unable to parse "
477431deaa0Shylee 			    "the private KMF config file.\n");
478431deaa0Shylee 			ret = KMF_OK;
479431deaa0Shylee 		}
480431deaa0Shylee 
481431deaa0Shylee 		if (ret != KMF_OK) {
482431deaa0Shylee 			(void) mutex_unlock(&extra_plugin_lock);
483431deaa0Shylee 			goto errout;
484431deaa0Shylee 		}
485431deaa0Shylee 	}
486431deaa0Shylee 	(void) mutex_unlock(&extra_plugin_lock);
487431deaa0Shylee 
48899ebb4caSwyllys 	/* Initialize the handle with the policy */
48930a5e8faSwyllys 	ret = kmf_set_policy((void *)handle,
49099ebb4caSwyllys 	    policyfile == NULL ? KMF_DEFAULT_POLICY_FILE : policyfile,
49199ebb4caSwyllys 	    policyname == NULL ? KMF_DEFAULT_POLICY_NAME : policyname);
49299ebb4caSwyllys 	if (ret != KMF_OK)
49399ebb4caSwyllys 		goto errout;
49499ebb4caSwyllys 
49599ebb4caSwyllys 	CLEAR_ERROR(handle, ret);
49699ebb4caSwyllys errout:
49799ebb4caSwyllys 	if (ret != KMF_OK) {
49899ebb4caSwyllys 		Cleanup_KMF_Handle(handle);
49999ebb4caSwyllys 		handle = NULL;
50099ebb4caSwyllys 	}
50199ebb4caSwyllys 
50299ebb4caSwyllys 	*outhandle = (KMF_HANDLE_T)handle;
50399ebb4caSwyllys 	return (ret);
50499ebb4caSwyllys }
50599ebb4caSwyllys 
50699ebb4caSwyllys KMF_RETURN
50730a5e8faSwyllys kmf_configure_keystore(KMF_HANDLE_T handle,
50830a5e8faSwyllys 	int	num_args,
50930a5e8faSwyllys 	KMF_ATTRIBUTE	*attrlist)
51099ebb4caSwyllys {
51130a5e8faSwyllys 	KMF_RETURN ret = KMF_OK;
51299ebb4caSwyllys 	KMF_PLUGIN *plugin;
51330a5e8faSwyllys 	KMF_KEYSTORE_TYPE kstype;
51430a5e8faSwyllys 	uint32_t len;
51530a5e8faSwyllys 
51630a5e8faSwyllys 	KMF_ATTRIBUTE_TESTER required_attrs[] = {
51730a5e8faSwyllys 		{KMF_KEYSTORE_TYPE_ATTR, FALSE, 1, sizeof (KMF_KEYSTORE_TYPE)},
51830a5e8faSwyllys 	};
51930a5e8faSwyllys 
52030a5e8faSwyllys 	int num_req_attrs = sizeof (required_attrs) /
52130a5e8faSwyllys 	    sizeof (KMF_ATTRIBUTE_TESTER);
52230a5e8faSwyllys 
52330a5e8faSwyllys 	if (handle == NULL)
52430a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
52599ebb4caSwyllys 
52699ebb4caSwyllys 	CLEAR_ERROR(handle, ret);
52730a5e8faSwyllys 
52830a5e8faSwyllys 	ret = test_attributes(num_req_attrs, required_attrs,
52930a5e8faSwyllys 	    0, NULL, num_args, attrlist);
53030a5e8faSwyllys 
53199ebb4caSwyllys 	if (ret != KMF_OK)
53299ebb4caSwyllys 		return (ret);
53399ebb4caSwyllys 
53430a5e8faSwyllys 	len = sizeof (kstype);
53530a5e8faSwyllys 	ret = kmf_get_attr(KMF_KEYSTORE_TYPE_ATTR, attrlist, num_args,
53630a5e8faSwyllys 	    &kstype, &len);
53730a5e8faSwyllys 	if (ret != KMF_OK)
53830a5e8faSwyllys 		return (ret);
53999ebb4caSwyllys 
54030a5e8faSwyllys 	plugin = FindPlugin(handle, kstype);
54130a5e8faSwyllys 	if (plugin != NULL && plugin->funclist->ConfigureKeystore != NULL) {
54230a5e8faSwyllys 		return (plugin->funclist->ConfigureKeystore(handle, num_args,
54330a5e8faSwyllys 		    attrlist));
54430a5e8faSwyllys 	} else {
54599ebb4caSwyllys 		/* return KMF_OK, if the plugin does not have an entry */
54699ebb4caSwyllys 		return (KMF_OK);
54730a5e8faSwyllys 	}
54899ebb4caSwyllys }
54999ebb4caSwyllys 
55099ebb4caSwyllys KMF_RETURN
55130a5e8faSwyllys kmf_finalize(KMF_HANDLE_T handle)
55299ebb4caSwyllys {
55399ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
55499ebb4caSwyllys 
55599ebb4caSwyllys 	CLEAR_ERROR(handle, ret);
55699ebb4caSwyllys 	if (ret != KMF_OK)
55799ebb4caSwyllys 		return (ret);
55899ebb4caSwyllys 
55999ebb4caSwyllys 	if (pkcs11_initialized) {
56099ebb4caSwyllys 		Cleanup_PK11_Session(handle);
56199ebb4caSwyllys 	}
56299ebb4caSwyllys 	Cleanup_KMF_Handle(handle);
56399ebb4caSwyllys 
56499ebb4caSwyllys 	return (ret);
56599ebb4caSwyllys }
56699ebb4caSwyllys 
56799ebb4caSwyllys KMF_RETURN
56830a5e8faSwyllys kmf_get_kmf_error_str(KMF_RETURN errcode, char **errmsg)
56999ebb4caSwyllys {
57099ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
57199ebb4caSwyllys 	int i, maxerr;
57299ebb4caSwyllys 
57399ebb4caSwyllys 	if (errmsg == NULL)
57499ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
57599ebb4caSwyllys 
57699ebb4caSwyllys 	*errmsg = NULL;
57799ebb4caSwyllys 	maxerr = sizeof (kmf_errcodes) / sizeof (kmf_error_map);
57899ebb4caSwyllys 
57930a5e8faSwyllys 	for (i = 0; i < maxerr && errcode != kmf_errcodes[i].code; i++)
58030a5e8faSwyllys 		/* empty body */
58130a5e8faSwyllys 		;
58299ebb4caSwyllys 
58399ebb4caSwyllys 	if (i == maxerr)
58499ebb4caSwyllys 		return (KMF_ERR_MISSING_ERRCODE);
58599ebb4caSwyllys 	else {
58699ebb4caSwyllys 		*errmsg = strdup(kmf_errcodes[i].message);
58799ebb4caSwyllys 		if ((*errmsg) == NULL)
58899ebb4caSwyllys 			return (KMF_ERR_MEMORY);
58999ebb4caSwyllys 	}
59099ebb4caSwyllys 	return (ret);
59199ebb4caSwyllys }
59299ebb4caSwyllys 
59399ebb4caSwyllys KMF_RETURN
59430a5e8faSwyllys kmf_get_plugin_error_str(KMF_HANDLE_T handle, char **msgstr)
59599ebb4caSwyllys {
59699ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
59799ebb4caSwyllys 	KMF_PLUGIN *plugin;
59899ebb4caSwyllys 
59999ebb4caSwyllys 	if (handle == NULL || msgstr == NULL)
60099ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
60199ebb4caSwyllys 
60299ebb4caSwyllys 	*msgstr = NULL;
60399ebb4caSwyllys 
60499ebb4caSwyllys 	if (handle->lasterr.errcode == 0) {
60599ebb4caSwyllys 		return (KMF_ERR_MISSING_ERRCODE);
60699ebb4caSwyllys 	}
60799ebb4caSwyllys 
60899ebb4caSwyllys 	if (handle->lasterr.kstype == -1) { /* System error */
60999ebb4caSwyllys 		char *str = strerror(handle->lasterr.errcode);
61099ebb4caSwyllys 		if (str != NULL) {
61199ebb4caSwyllys 			*msgstr = strdup(str);
61299ebb4caSwyllys 			if ((*msgstr) == NULL)
61399ebb4caSwyllys 				return (KMF_ERR_MEMORY);
61499ebb4caSwyllys 		}
61599ebb4caSwyllys 		return (KMF_OK);
61699ebb4caSwyllys 	}
61799ebb4caSwyllys 
61899ebb4caSwyllys 	plugin = FindPlugin(handle, handle->lasterr.kstype);
61999ebb4caSwyllys 	if (plugin == NULL)
62099ebb4caSwyllys 		return (KMF_ERR_PLUGIN_NOTFOUND);
62199ebb4caSwyllys 
62299ebb4caSwyllys 	if (plugin->funclist->GetErrorString != NULL) {
62399ebb4caSwyllys 		ret = plugin->funclist->GetErrorString(handle, msgstr);
62499ebb4caSwyllys 	} else {
62599ebb4caSwyllys 		return (KMF_ERR_FUNCTION_NOT_FOUND);
62699ebb4caSwyllys 	}
62799ebb4caSwyllys 
62899ebb4caSwyllys 	return (ret);
62999ebb4caSwyllys }
63099ebb4caSwyllys 
63199ebb4caSwyllys 
6329b37d296Swyllys #define	SET_SYS_ERROR(h, c) if (h) {\
6339b37d296Swyllys 	h->lasterr.kstype = -1;\
6349b37d296Swyllys 	h->lasterr.errcode = c;\
6359b37d296Swyllys }
63699ebb4caSwyllys 
63799ebb4caSwyllys KMF_RETURN
63830a5e8faSwyllys kmf_read_input_file(KMF_HANDLE_T handle, char *filename,  KMF_DATA *pdata)
63999ebb4caSwyllys {
64099ebb4caSwyllys 	struct stat s;
64199ebb4caSwyllys 	long nread, total = 0;
64299ebb4caSwyllys 	int fd;
64399ebb4caSwyllys 	unsigned char *buf = NULL;
64499ebb4caSwyllys 	KMF_RETURN ret;
64599ebb4caSwyllys 
6469b37d296Swyllys 	if (handle) {
6479b37d296Swyllys 		CLEAR_ERROR(handle, ret);
6489b37d296Swyllys 		if (ret != KMF_OK)
6499b37d296Swyllys 			return (ret);
6509b37d296Swyllys 	}
65199ebb4caSwyllys 
65299ebb4caSwyllys 	if (filename == NULL || pdata == NULL) {
65399ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
65499ebb4caSwyllys 	}
65599ebb4caSwyllys 
65699ebb4caSwyllys 	if ((fd = open(filename, O_RDONLY)) < 0) {
65799ebb4caSwyllys 		SET_SYS_ERROR(handle, errno);
65899ebb4caSwyllys 		return (KMF_ERR_OPEN_FILE);
65999ebb4caSwyllys 	}
66099ebb4caSwyllys 
66199ebb4caSwyllys 	if (fstat(fd, &s) < 0) {
66299ebb4caSwyllys 		SET_SYS_ERROR(handle, errno);
66399ebb4caSwyllys 		(void) close(fd);
66499ebb4caSwyllys 		return (KMF_ERR_OPEN_FILE);
66599ebb4caSwyllys 	}
66699ebb4caSwyllys 
66799ebb4caSwyllys 	if ((buf = (unsigned char *) malloc(s.st_size)) == NULL) {
66899ebb4caSwyllys 		(void) close(fd);
66999ebb4caSwyllys 		return (KMF_ERR_MEMORY);
67099ebb4caSwyllys 	}
67199ebb4caSwyllys 
67299ebb4caSwyllys 	do {
67399ebb4caSwyllys 		nread = read(fd, buf+total, s.st_size-total);
67499ebb4caSwyllys 		if (nread < 0) {
67599ebb4caSwyllys 			SET_SYS_ERROR(handle, errno);
67699ebb4caSwyllys 			(void) close(fd);
67799ebb4caSwyllys 			free(buf);
67899ebb4caSwyllys 			return (KMF_ERR_INTERNAL);
67999ebb4caSwyllys 		}
68099ebb4caSwyllys 		total += nread;
68199ebb4caSwyllys 	} while (total < s.st_size);
68299ebb4caSwyllys 
68399ebb4caSwyllys 	pdata->Data = buf;
68499ebb4caSwyllys 	pdata->Length = s.st_size;
68599ebb4caSwyllys 	(void) close(fd);
68699ebb4caSwyllys 	return (KMF_OK);
68799ebb4caSwyllys }
68899ebb4caSwyllys 
68999ebb4caSwyllys /*
69099ebb4caSwyllys  *
69130a5e8faSwyllys  * Name: kmf_der_to_pem
69299ebb4caSwyllys  *
69399ebb4caSwyllys  * Description:
69499ebb4caSwyllys  *   Function for converting DER encoded format to PEM encoded format
69599ebb4caSwyllys  *
69699ebb4caSwyllys  * Parameters:
69799ebb4caSwyllys  *   type(input) - CERTIFICATE or CSR
69899ebb4caSwyllys  *   data(input) - pointer to the DER encoded data
69999ebb4caSwyllys  *   len(input)  - length of input data
70099ebb4caSwyllys  *   out(output) - contains the output buffer address to be returned
70199ebb4caSwyllys  *   outlen(output) - pointer to the returned output length
70299ebb4caSwyllys  *
70399ebb4caSwyllys  * Returns:
70499ebb4caSwyllys  *   A KMF_RETURN value indicating success or specifying a particular
70599ebb4caSwyllys  * error condition.
70699ebb4caSwyllys  *   The value KMF_OK indicates success. All other values represent
70799ebb4caSwyllys  * an error condition.
70899ebb4caSwyllys  *
70999ebb4caSwyllys  */
71099ebb4caSwyllys KMF_RETURN
71130a5e8faSwyllys kmf_der_to_pem(KMF_OBJECT_TYPE type, unsigned char *data,
71299ebb4caSwyllys 	int len, unsigned char **out, int *outlen)
71399ebb4caSwyllys {
71499ebb4caSwyllys 
71599ebb4caSwyllys 	KMF_RETURN err;
71699ebb4caSwyllys 	if (data == NULL || out == NULL || outlen == NULL)
71799ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
71899ebb4caSwyllys 
71999ebb4caSwyllys 	err = Der2Pem(type, data, len, out, outlen);
72099ebb4caSwyllys 	return (err);
72199ebb4caSwyllys 
72299ebb4caSwyllys }
72399ebb4caSwyllys 
72499ebb4caSwyllys /*
72599ebb4caSwyllys  *
72630a5e8faSwyllys  * Name: kmf_pem_to_der
72799ebb4caSwyllys  *
72899ebb4caSwyllys  * Description:
72999ebb4caSwyllys  *   Function for converting PEM encoded format to DER encoded format
73099ebb4caSwyllys  *
73199ebb4caSwyllys  * Parameters:
73299ebb4caSwyllys  *   in(input) - pointer to the PEM encoded data
73399ebb4caSwyllys  *   inlen(input)  - length of input data
73499ebb4caSwyllys  *   out(output) - contains the output buffer address to be returned
73599ebb4caSwyllys  *   outlen(output) - pointer to the returned output length
73699ebb4caSwyllys  *
73799ebb4caSwyllys  * Returns:
73899ebb4caSwyllys  *   A KMF_RETURN value indicating success or specifying a particular
73999ebb4caSwyllys  *   error condition.
74099ebb4caSwyllys  *   The value KMF_OK indicates success. All other values represent
74199ebb4caSwyllys  *   an error condition.
74299ebb4caSwyllys  *
74399ebb4caSwyllys  */
74499ebb4caSwyllys KMF_RETURN
74530a5e8faSwyllys kmf_pem_to_der(unsigned char *in, int inlen,
74699ebb4caSwyllys 	unsigned char **out, int *outlen)
74799ebb4caSwyllys {
74899ebb4caSwyllys 	KMF_RETURN err;
74999ebb4caSwyllys 	if (in == NULL || out == NULL || outlen == NULL)
75099ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
75199ebb4caSwyllys 
75299ebb4caSwyllys 	err = Pem2Der(in, inlen, out, outlen);
75399ebb4caSwyllys 	return (err);
75499ebb4caSwyllys }
75599ebb4caSwyllys 
75699ebb4caSwyllys char *
75730a5e8faSwyllys kmf_oid_to_string(KMF_OID *oid)
75899ebb4caSwyllys {
75999ebb4caSwyllys 	char numstr[128];
76099ebb4caSwyllys 	uint32_t number;
76199ebb4caSwyllys 	int numshift;
76299ebb4caSwyllys 	uint32_t i, string_length;
76399ebb4caSwyllys 	uchar_t *cp;
76499ebb4caSwyllys 	char *bp;
76599ebb4caSwyllys 
76699ebb4caSwyllys 	/* First determine the size of the string */
76799ebb4caSwyllys 	string_length = 0;
76899ebb4caSwyllys 	number = 0;
76999ebb4caSwyllys 	numshift = 0;
77099ebb4caSwyllys 	cp = (unsigned char *)oid->Data;
77199ebb4caSwyllys 
77299ebb4caSwyllys 	number = (uint32_t)cp[0];
77399ebb4caSwyllys 	(void) sprintf(numstr, "%d ", number/40);
77499ebb4caSwyllys 
77599ebb4caSwyllys 	string_length += strlen(numstr);
77699ebb4caSwyllys 	(void) sprintf(numstr, "%d ", number%40);
77799ebb4caSwyllys 
77899ebb4caSwyllys 	string_length += strlen(numstr);
77999ebb4caSwyllys 
78099ebb4caSwyllys 	for (i = 1; i < oid->Length; i++) {
78199ebb4caSwyllys 		if ((uint32_t)(numshift+7) < (sizeof (uint32_t)*8)) {
78299ebb4caSwyllys 			number = (number << 7) | (cp[i] & 0x7f);
78399ebb4caSwyllys 			numshift += 7;
78499ebb4caSwyllys 		} else {
78599ebb4caSwyllys 			return (NULL);
78699ebb4caSwyllys 		}
78799ebb4caSwyllys 
78899ebb4caSwyllys 		if ((cp[i] & 0x80) == 0) {
78999ebb4caSwyllys 			(void) sprintf(numstr, "%d ", number);
79099ebb4caSwyllys 			string_length += strlen(numstr);
79199ebb4caSwyllys 			number = 0;
79299ebb4caSwyllys 			numshift = 0;
79399ebb4caSwyllys 		}
79499ebb4caSwyllys 	}
79599ebb4caSwyllys 	/*
79699ebb4caSwyllys 	 * If we get here, we've calculated the length of "n n n ... n ".  Add 4
79799ebb4caSwyllys 	 * here for "{ " and "}\0".
79899ebb4caSwyllys 	 */
79999ebb4caSwyllys 	string_length += 4;
80099ebb4caSwyllys 	if ((bp = (char *)malloc(string_length))) {
80199ebb4caSwyllys 		number = (uint32_t)cp[0];
80299ebb4caSwyllys 
80399ebb4caSwyllys 		(void) sprintf(numstr, "%d.", number/40);
80499ebb4caSwyllys 		(void) strcpy(bp, numstr);
80599ebb4caSwyllys 
80699ebb4caSwyllys 		(void) sprintf(numstr, "%d.", number%40);
80799ebb4caSwyllys 		(void) strcat(bp, numstr);
80899ebb4caSwyllys 
80999ebb4caSwyllys 		number = 0;
81099ebb4caSwyllys 		cp = (unsigned char *) oid->Data;
81199ebb4caSwyllys 		for (i = 1; i < oid->Length; i++) {
81299ebb4caSwyllys 			number = (number << 7) | (cp[i] & 0x7f);
81399ebb4caSwyllys 			if ((cp[i] & 0x80) == 0) {
81499ebb4caSwyllys 				(void) sprintf(numstr, "%d", number);
81599ebb4caSwyllys 				(void) strcat(bp, numstr);
81699ebb4caSwyllys 				number = 0;
81799ebb4caSwyllys 				if (i+1 < oid->Length)
81899ebb4caSwyllys 					(void) strcat(bp, ".");
81999ebb4caSwyllys 			}
82099ebb4caSwyllys 		}
82199ebb4caSwyllys 	}
82299ebb4caSwyllys 	return (bp);
82399ebb4caSwyllys }
82499ebb4caSwyllys 
82502744e81Swyllys static boolean_t
82608ec4bd3Shylee check_for_pem(uchar_t *buf, KMF_ENCODE_FORMAT *fmt)
82702744e81Swyllys {
8289b37d296Swyllys 	char *p;
829d00756ccSwyllys 	int i;
83002744e81Swyllys 
83108ec4bd3Shylee 	if (buf == NULL)
83202744e81Swyllys 		return (FALSE);
83302744e81Swyllys 
834d00756ccSwyllys 	for (i = 0; i < 8 && isascii(buf[i]); i++)
835d00756ccSwyllys 		/* loop to make sure this is ascii */;
836d00756ccSwyllys 	if (i != 8)
837d00756ccSwyllys 		return (FALSE);
838d00756ccSwyllys 
83908ec4bd3Shylee 	if (memcmp(buf, "Bag Attr", 8) == 0) {
84008ec4bd3Shylee 		*fmt = KMF_FORMAT_PEM_KEYPAIR;
84108ec4bd3Shylee 		return (TRUE);
84208ec4bd3Shylee 	}
84308ec4bd3Shylee 
8449b37d296Swyllys 	/* Look for "-----BEGIN" right after a newline */
84508ec4bd3Shylee 	p = strtok((char *)buf, "\n");
8469b37d296Swyllys 	while (p != NULL) {
8479b37d296Swyllys 		if (strstr(p, "-----BEGIN") != NULL) {
84808ec4bd3Shylee 			*fmt = KMF_FORMAT_PEM;
849d00756ccSwyllys 			/* Restore the buffer */
850d00756ccSwyllys 			buf[strlen(p)] = '\n';
8519b37d296Swyllys 			return (TRUE);
85202744e81Swyllys 		}
853d00756ccSwyllys 		buf[strlen(p)] = '\n';
8549b37d296Swyllys 		p = strtok(NULL, "\n");
85502744e81Swyllys 	}
85602744e81Swyllys 	return (FALSE);
85702744e81Swyllys }
85802744e81Swyllys 
85908ec4bd3Shylee 
86008ec4bd3Shylee static unsigned char pkcs12_version[3] = {0x02, 0x01, 0x03};
86108ec4bd3Shylee static unsigned char pkcs12_oid[11] =
86208ec4bd3Shylee {0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01};
86308ec4bd3Shylee 
86408ec4bd3Shylee /*
86508ec4bd3Shylee  * This function takes a BER encoded string as input and checks the version
86608ec4bd3Shylee  * and the oid in the the top-level ASN.1 structure to see if it complies to
86708ec4bd3Shylee  * the PKCS#12 Syntax.
86808ec4bd3Shylee  */
86908ec4bd3Shylee static boolean_t
87008ec4bd3Shylee check_for_pkcs12(uchar_t *buf, int buf_len)
87108ec4bd3Shylee {
87208ec4bd3Shylee 	int index = 0;
87308ec4bd3Shylee 	int length_octets;
87408ec4bd3Shylee 
87508ec4bd3Shylee 	if (buf == NULL || buf_len <= 0)
87608ec4bd3Shylee 		return (FALSE);
87708ec4bd3Shylee 
87808ec4bd3Shylee 	/*
87908ec4bd3Shylee 	 * The top level structure for a PKCS12 string:
88008ec4bd3Shylee 	 *
88108ec4bd3Shylee 	 * PFX ::= SEQUENCE {
88208ec4bd3Shylee 	 *	version		INTEGER {v3(3)}(v3,...)
88308ec4bd3Shylee 	 *	authSafe	ContentInfo
88408ec4bd3Shylee 	 *	macData		MacData OPTIONAL
88508ec4bd3Shylee 	 * }
88608ec4bd3Shylee 	 *
88708ec4bd3Shylee 	 * ContentInfo
88808ec4bd3Shylee 	 *	FROM PKCS-7 {iso(1) member-body(2) us(840) rsadsi(113549)
88908ec4bd3Shylee 	 *		pkcs(1) pkcs-7(7) modules(0) pkcs-7(1)}
89008ec4bd3Shylee 	 *
89108ec4bd3Shylee 	 * Therefore, the BER/DER dump of a PKCS#12 file for the first 2
89208ec4bd3Shylee 	 * sequences up to the oid part is as following:
89308ec4bd3Shylee 	 *
89408ec4bd3Shylee 	 *	SEQUENCE {
89508ec4bd3Shylee 	 *	    INTEGER 3
89608ec4bd3Shylee 	 *	    SEQUENCE {
89708ec4bd3Shylee 	 *		OBJECT IDENTIFIER data (1 2 840 113549 1 7 1)
89808ec4bd3Shylee 	 */
89908ec4bd3Shylee 
90008ec4bd3Shylee 	/*
90108ec4bd3Shylee 	 * Check the first sequence and calculate the number of bytes used
90208ec4bd3Shylee 	 * to store the length.
90308ec4bd3Shylee 	 */
90408ec4bd3Shylee 	if (buf[index++] != 0x30)
90508ec4bd3Shylee 		return (FALSE);
90608ec4bd3Shylee 
90708ec4bd3Shylee 	if (buf[index] & 0x80) {
90808ec4bd3Shylee 		length_octets = buf[index++] & 0x0F;  /* long form */
90908ec4bd3Shylee 	} else {
91008ec4bd3Shylee 		length_octets = 1; /* short form */
91108ec4bd3Shylee 	}
91208ec4bd3Shylee 
91308ec4bd3Shylee 	index += length_octets;
91408ec4bd3Shylee 	if (index  >= buf_len)
91508ec4bd3Shylee 		return (FALSE);
91608ec4bd3Shylee 
91708ec4bd3Shylee 	/* Skip the length octets and check the pkcs12 version */
91808ec4bd3Shylee 	if (memcmp(buf + index, pkcs12_version, sizeof (pkcs12_version)) != 0)
91908ec4bd3Shylee 		return (FALSE);
92008ec4bd3Shylee 
92108ec4bd3Shylee 	index += sizeof (pkcs12_version);
92208ec4bd3Shylee 	if (index  >= buf_len)
92308ec4bd3Shylee 		return (FALSE);
92408ec4bd3Shylee 
92508ec4bd3Shylee 	/*
92608ec4bd3Shylee 	 * Check the 2nd sequence and calculate the number of bytes used
92708ec4bd3Shylee 	 * to store the length.
92808ec4bd3Shylee 	 */
92908ec4bd3Shylee 	if ((buf[index++] & 0xFF) != 0x30)
93008ec4bd3Shylee 		return (FALSE);
93108ec4bd3Shylee 
93208ec4bd3Shylee 	if (buf[index] & 0x80) {
93308ec4bd3Shylee 		length_octets = buf[index++] & 0x0F;
93408ec4bd3Shylee 	} else {
93508ec4bd3Shylee 		length_octets = 1;
93608ec4bd3Shylee 	}
93708ec4bd3Shylee 
93808ec4bd3Shylee 	index += length_octets;
93908ec4bd3Shylee 	if (index + sizeof (pkcs12_oid) >= buf_len)
94008ec4bd3Shylee 		return (FALSE);
94108ec4bd3Shylee 
94208ec4bd3Shylee 	/* Skip the length octets and check the oid */
94308ec4bd3Shylee 	if (memcmp(buf + index, pkcs12_oid, sizeof (pkcs12_oid)) != 0)
94408ec4bd3Shylee 		return (FALSE);
94508ec4bd3Shylee 	else
94608ec4bd3Shylee 		return (TRUE);
94708ec4bd3Shylee }
94808ec4bd3Shylee 
949d00756ccSwyllys KMF_RETURN
950d00756ccSwyllys kmf_get_data_format(KMF_DATA *data, KMF_ENCODE_FORMAT *fmt)
951d00756ccSwyllys {
952d00756ccSwyllys 	uchar_t *buf = data->Data;
953d00756ccSwyllys 
954d00756ccSwyllys 	if (check_for_pkcs12(buf, data->Length) == TRUE) {
955d00756ccSwyllys 		*fmt = KMF_FORMAT_PKCS12;
956d00756ccSwyllys 	} else if (buf[0] == 0x30 && (buf[1] & 0x80)) {
957d00756ccSwyllys 		/* It is most likely a generic ASN.1 encoded file */
958d00756ccSwyllys 		*fmt = KMF_FORMAT_ASN1;
959d00756ccSwyllys 	} else if (check_for_pem(buf, fmt) != TRUE) {
960d00756ccSwyllys 		/* Cannot determine this file format */
961d00756ccSwyllys 		*fmt = KMF_FORMAT_UNDEF;
962d00756ccSwyllys 		return (KMF_ERR_ENCODING);
963d00756ccSwyllys 	}
964d00756ccSwyllys 	return (KMF_OK);
965d00756ccSwyllys }
966d00756ccSwyllys 
96799ebb4caSwyllys KMF_RETURN
96830a5e8faSwyllys kmf_get_file_format(char *filename, KMF_ENCODE_FORMAT *fmt)
96999ebb4caSwyllys {
97099ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
97108ec4bd3Shylee 	KMF_DATA filebuf = {NULL, 0};
97299ebb4caSwyllys 
97399ebb4caSwyllys 	if (filename == NULL || !strlen(filename) || fmt == NULL)
97499ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
97599ebb4caSwyllys 
97699ebb4caSwyllys 	*fmt = 0;
97730a5e8faSwyllys 	ret = kmf_read_input_file(NULL, filename, &filebuf);
97808ec4bd3Shylee 	if (ret != KMF_OK)
97908ec4bd3Shylee 		return (ret);
98099ebb4caSwyllys 
98108ec4bd3Shylee 	if (filebuf.Length < 8) {
98208ec4bd3Shylee 		ret = KMF_ERR_ENCODING; /* too small */
98399ebb4caSwyllys 		goto end;
98499ebb4caSwyllys 	}
98599ebb4caSwyllys 
986d00756ccSwyllys 	ret = kmf_get_data_format(&filebuf, fmt);
98799ebb4caSwyllys end:
98830a5e8faSwyllys 	kmf_free_data(&filebuf);
98999ebb4caSwyllys 	return (ret);
99099ebb4caSwyllys }
99199ebb4caSwyllys 
99299ebb4caSwyllys KMF_RETURN
99330a5e8faSwyllys kmf_hexstr_to_bytes(unsigned char *hexstr, unsigned char **bytes,
99499ebb4caSwyllys 	size_t *outlen)
99599ebb4caSwyllys {
99699ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
99799ebb4caSwyllys 	unsigned char *buf = NULL;
99899ebb4caSwyllys 	int len, stringlen;
99999ebb4caSwyllys 	int i;
100099ebb4caSwyllys 	unsigned char ch;
100199ebb4caSwyllys 
100299ebb4caSwyllys 	if (hexstr == NULL) {
100399ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
100499ebb4caSwyllys 	}
100599ebb4caSwyllys 
100630a5e8faSwyllys 	if (hexstr[0] == '0' && ((hexstr[1] == 'x') || (hexstr[1] == 'X')))
100799ebb4caSwyllys 		hexstr += 2;
100899ebb4caSwyllys 
100930a5e8faSwyllys 	for (i = 0; i < strlen((char *)hexstr) && isxdigit(hexstr[i]); i++)
101030a5e8faSwyllys 		/* empty body */
101130a5e8faSwyllys 		;
101299ebb4caSwyllys 	/*
101399ebb4caSwyllys 	 * If all the characters are not legitimate hex chars,
101499ebb4caSwyllys 	 * return an error.
101599ebb4caSwyllys 	 */
101699ebb4caSwyllys 	if (i != strlen((char *)hexstr))
101799ebb4caSwyllys 		return (KMF_ERR_BAD_HEX_STRING);
101899ebb4caSwyllys 	stringlen = i;
101999ebb4caSwyllys 	len = (i / 2) + (i % 2);
102099ebb4caSwyllys 
102199ebb4caSwyllys 	buf = malloc(len);
102299ebb4caSwyllys 	if (buf == NULL) {
102399ebb4caSwyllys 		return (KMF_ERR_MEMORY);
102499ebb4caSwyllys 	}
102599ebb4caSwyllys 	(void) memset(buf, 0, len);
102699ebb4caSwyllys 
102799ebb4caSwyllys 	for (i = 0; i < stringlen; i++) {
102899ebb4caSwyllys 		ch = (unsigned char) *hexstr;
102999ebb4caSwyllys 		hexstr++;
103099ebb4caSwyllys 		if ((ch >= '0') && (ch <= '9'))
103199ebb4caSwyllys 			ch -= '0';
103299ebb4caSwyllys 		else if ((ch >= 'A') && (ch <= 'F'))
103399ebb4caSwyllys 			ch = ch - 'A' + 10;
103499ebb4caSwyllys 		else if ((ch >= 'a') && (ch <= 'f'))
103599ebb4caSwyllys 			ch = ch - 'a' + 10;
103699ebb4caSwyllys 		else {
103799ebb4caSwyllys 			ret = KMF_ERR_BAD_HEX_STRING;
103899ebb4caSwyllys 			goto out;
103999ebb4caSwyllys 		}
104099ebb4caSwyllys 
104199ebb4caSwyllys 		if (i & 1) {
104299ebb4caSwyllys 			buf[i/2] |= ch;
104399ebb4caSwyllys 		} else {
104499ebb4caSwyllys 			buf[i/2] = (ch << 4);
104599ebb4caSwyllys 		}
104699ebb4caSwyllys 	}
104799ebb4caSwyllys 
104899ebb4caSwyllys 	*bytes = buf;
104999ebb4caSwyllys 	*outlen = len;
105099ebb4caSwyllys out:
105199ebb4caSwyllys 	if (buf != NULL && ret != KMF_OK) {
105299ebb4caSwyllys 		free(buf);
105399ebb4caSwyllys 	}
105499ebb4caSwyllys 	return (ret);
105599ebb4caSwyllys }
105699ebb4caSwyllys 
105799ebb4caSwyllys void
105830a5e8faSwyllys kmf_free_dn(KMF_X509_NAME *name)
105999ebb4caSwyllys {
106099ebb4caSwyllys 	KMF_X509_RDN 		*newrdn = NULL;
106199ebb4caSwyllys 	KMF_X509_TYPE_VALUE_PAIR *av = NULL;
106299ebb4caSwyllys 	int i, j;
106399ebb4caSwyllys 
106499ebb4caSwyllys 	if (name && name->numberOfRDNs) {
106599ebb4caSwyllys 		for (i = 0; i < name->numberOfRDNs; i++) {
106699ebb4caSwyllys 			newrdn = &name->RelativeDistinguishedName[i];
106799ebb4caSwyllys 			for (j = 0; j < newrdn->numberOfPairs; j++) {
106899ebb4caSwyllys 				av = &newrdn->AttributeTypeAndValue[j];
106930a5e8faSwyllys 				kmf_free_data(&av->type);
107030a5e8faSwyllys 				kmf_free_data(&av->value);
107199ebb4caSwyllys 			}
107299ebb4caSwyllys 			free(newrdn->AttributeTypeAndValue);
107399ebb4caSwyllys 			newrdn->numberOfPairs = 0;
107499ebb4caSwyllys 			newrdn->AttributeTypeAndValue = NULL;
107599ebb4caSwyllys 		}
107699ebb4caSwyllys 		free(name->RelativeDistinguishedName);
107799ebb4caSwyllys 		name->numberOfRDNs = 0;
107899ebb4caSwyllys 		name->RelativeDistinguishedName = NULL;
107999ebb4caSwyllys 	}
108099ebb4caSwyllys }
108199ebb4caSwyllys 
108299ebb4caSwyllys void
108330a5e8faSwyllys kmf_free_kmf_cert(KMF_HANDLE_T handle, KMF_X509_DER_CERT *kmf_cert)
108499ebb4caSwyllys {
108599ebb4caSwyllys 	KMF_PLUGIN *plugin;
108699ebb4caSwyllys 	KMF_RETURN ret;
108799ebb4caSwyllys 
108899ebb4caSwyllys 	CLEAR_ERROR(handle, ret);
108999ebb4caSwyllys 	if (ret != KMF_OK)
109099ebb4caSwyllys 		return;
109199ebb4caSwyllys 
109299ebb4caSwyllys 	if (kmf_cert == NULL)
109399ebb4caSwyllys 		return;
109499ebb4caSwyllys 
109599ebb4caSwyllys 	plugin = FindPlugin(handle, kmf_cert->kmf_private.keystore_type);
109699ebb4caSwyllys 
109799ebb4caSwyllys 	if (plugin != NULL && plugin->funclist->FreeKMFCert != NULL) {
109899ebb4caSwyllys 		plugin->funclist->FreeKMFCert(handle, kmf_cert);
109999ebb4caSwyllys 	}
110099ebb4caSwyllys }
110199ebb4caSwyllys 
110299ebb4caSwyllys void
110330a5e8faSwyllys kmf_free_data(KMF_DATA *datablock)
110499ebb4caSwyllys {
110599ebb4caSwyllys 	if (datablock != NULL && datablock->Data != NULL) {
110699ebb4caSwyllys 		free(datablock->Data);
110799ebb4caSwyllys 		datablock->Data = NULL;
110899ebb4caSwyllys 		datablock->Length = 0;
110999ebb4caSwyllys 	}
111099ebb4caSwyllys }
111199ebb4caSwyllys 
111299ebb4caSwyllys void
111330a5e8faSwyllys kmf_free_algoid(KMF_X509_ALGORITHM_IDENTIFIER *algoid)
111499ebb4caSwyllys {
111599ebb4caSwyllys 	if (algoid == NULL)
111699ebb4caSwyllys 		return;
111730a5e8faSwyllys 	kmf_free_data(&algoid->algorithm);
111830a5e8faSwyllys 	kmf_free_data(&algoid->parameters);
111999ebb4caSwyllys }
112099ebb4caSwyllys 
112199ebb4caSwyllys void
112230a5e8faSwyllys kmf_free_extn(KMF_X509_EXTENSION *exptr)
112399ebb4caSwyllys {
112499ebb4caSwyllys 	if (exptr == NULL)
112599ebb4caSwyllys 		return;
112699ebb4caSwyllys 
112730a5e8faSwyllys 	kmf_free_data((KMF_DATA *)&exptr->extnId);
112830a5e8faSwyllys 	kmf_free_data(&exptr->BERvalue);
112999ebb4caSwyllys 
113099ebb4caSwyllys 	if (exptr->value.tagAndValue) {
113130a5e8faSwyllys 		kmf_free_data(&exptr->value.tagAndValue->value);
113299ebb4caSwyllys 		free(exptr->value.tagAndValue);
113399ebb4caSwyllys 	}
113499ebb4caSwyllys }
113599ebb4caSwyllys 
113699ebb4caSwyllys void
113730a5e8faSwyllys kmf_free_tbs_csr(KMF_TBS_CSR *tbscsr)
113899ebb4caSwyllys {
113999ebb4caSwyllys 	if (tbscsr) {
114030a5e8faSwyllys 		kmf_free_data(&tbscsr->version);
114199ebb4caSwyllys 
114230a5e8faSwyllys 		kmf_free_dn(&tbscsr->subject);
114399ebb4caSwyllys 
114430a5e8faSwyllys 		kmf_free_algoid(&tbscsr->subjectPublicKeyInfo.algorithm);
114530a5e8faSwyllys 		kmf_free_data(&tbscsr->subjectPublicKeyInfo.subjectPublicKey);
114699ebb4caSwyllys 
114799ebb4caSwyllys 		free_extensions(&tbscsr->extensions);
114899ebb4caSwyllys 	}
114999ebb4caSwyllys }
115099ebb4caSwyllys 
115199ebb4caSwyllys void
115230a5e8faSwyllys kmf_free_signed_csr(KMF_CSR_DATA *csr)
115399ebb4caSwyllys {
115499ebb4caSwyllys 	if (csr) {
115530a5e8faSwyllys 		kmf_free_tbs_csr(&csr->csr);
115699ebb4caSwyllys 
115730a5e8faSwyllys 		kmf_free_algoid(&csr->signature.algorithmIdentifier);
115830a5e8faSwyllys 		kmf_free_data(&csr->signature.encrypted);
115999ebb4caSwyllys 	}
116099ebb4caSwyllys }
116199ebb4caSwyllys 
116299ebb4caSwyllys static void
116399ebb4caSwyllys free_validity(KMF_X509_VALIDITY *validity)
116499ebb4caSwyllys {
116599ebb4caSwyllys 	if (validity == NULL)
116699ebb4caSwyllys 		return;
116730a5e8faSwyllys 	kmf_free_data(&validity->notBefore.time);
116830a5e8faSwyllys 	kmf_free_data(&validity->notAfter.time);
116999ebb4caSwyllys }
117099ebb4caSwyllys 
117199ebb4caSwyllys static void
117299ebb4caSwyllys free_extensions(KMF_X509_EXTENSIONS *extns)
117399ebb4caSwyllys {
117499ebb4caSwyllys 	int i;
117599ebb4caSwyllys 	KMF_X509_EXTENSION *exptr;
117699ebb4caSwyllys 
117799ebb4caSwyllys 	if (extns && extns->numberOfExtensions > 0) {
117899ebb4caSwyllys 		for (i = 0; i < extns->numberOfExtensions; i++) {
117999ebb4caSwyllys 			exptr = &extns->extensions[i];
118030a5e8faSwyllys 			kmf_free_extn(exptr);
118199ebb4caSwyllys 		}
118299ebb4caSwyllys 		free(extns->extensions);
118399ebb4caSwyllys 		extns->numberOfExtensions = 0;
118499ebb4caSwyllys 		extns->extensions = NULL;
118599ebb4caSwyllys 	}
118699ebb4caSwyllys }
118799ebb4caSwyllys 
118899ebb4caSwyllys void
118930a5e8faSwyllys kmf_free_tbs_cert(KMF_X509_TBS_CERT *tbscert)
119099ebb4caSwyllys {
119199ebb4caSwyllys 	if (tbscert) {
119230a5e8faSwyllys 		kmf_free_data(&tbscert->version);
119330a5e8faSwyllys 		kmf_free_bigint(&tbscert->serialNumber);
119430a5e8faSwyllys 		kmf_free_algoid(&tbscert->signature);
119599ebb4caSwyllys 
119630a5e8faSwyllys 		kmf_free_dn(&tbscert->issuer);
119730a5e8faSwyllys 		kmf_free_dn(&tbscert->subject);
119899ebb4caSwyllys 
119999ebb4caSwyllys 		free_validity(&tbscert->validity);
120099ebb4caSwyllys 
120130a5e8faSwyllys 		kmf_free_data(&tbscert->issuerUniqueIdentifier);
120230a5e8faSwyllys 		kmf_free_data(&tbscert->subjectUniqueIdentifier);
120399ebb4caSwyllys 
120430a5e8faSwyllys 		kmf_free_algoid(&tbscert->subjectPublicKeyInfo.algorithm);
120530a5e8faSwyllys 		kmf_free_data(&tbscert->subjectPublicKeyInfo.subjectPublicKey);
120699ebb4caSwyllys 
120799ebb4caSwyllys 		free_extensions(&tbscert->extensions);
120899ebb4caSwyllys 
120930a5e8faSwyllys 		kmf_free_data(&tbscert->issuerUniqueIdentifier);
121030a5e8faSwyllys 		kmf_free_data(&tbscert->subjectUniqueIdentifier);
121199ebb4caSwyllys 	}
121299ebb4caSwyllys }
121399ebb4caSwyllys 
121499ebb4caSwyllys void
121530a5e8faSwyllys kmf_free_signed_cert(KMF_X509_CERTIFICATE *certptr)
121699ebb4caSwyllys {
121799ebb4caSwyllys 	if (!certptr)
121899ebb4caSwyllys 		return;
121999ebb4caSwyllys 
122030a5e8faSwyllys 	kmf_free_tbs_cert(&certptr->certificate);
122199ebb4caSwyllys 
122230a5e8faSwyllys 	kmf_free_algoid(&certptr->signature.algorithmIdentifier);
122330a5e8faSwyllys 	kmf_free_data(&certptr->signature.encrypted);
122499ebb4caSwyllys }
122599ebb4caSwyllys 
122699ebb4caSwyllys void
122730a5e8faSwyllys kmf_free_str(char *pstr)
122899ebb4caSwyllys {
122999ebb4caSwyllys 	if (pstr != NULL)
123099ebb4caSwyllys 		free(pstr);
123199ebb4caSwyllys }
123299ebb4caSwyllys 
123399ebb4caSwyllys void
123499ebb4caSwyllys free_keyidlist(KMF_OID *oidlist, int len)
123599ebb4caSwyllys {
123699ebb4caSwyllys 	int i;
123799ebb4caSwyllys 	for (i = 0; i < len; i++) {
123830a5e8faSwyllys 		kmf_free_data((KMF_DATA *)&oidlist[i]);
123999ebb4caSwyllys 	}
124099ebb4caSwyllys 	free(oidlist);
124199ebb4caSwyllys }
124299ebb4caSwyllys 
124399ebb4caSwyllys void
124430a5e8faSwyllys kmf_free_eku(KMF_X509EXT_EKU *eptr)
124599ebb4caSwyllys {
124630a5e8faSwyllys 	if (eptr && eptr->nEKUs > 0 && eptr->keyPurposeIdList != NULL)
124799ebb4caSwyllys 		free_keyidlist(eptr->keyPurposeIdList, eptr->nEKUs);
124899ebb4caSwyllys }
124999ebb4caSwyllys 
125099ebb4caSwyllys void
125130a5e8faSwyllys kmf_free_spki(KMF_X509_SPKI *spki)
125299ebb4caSwyllys {
125399ebb4caSwyllys 	if (spki != NULL) {
125430a5e8faSwyllys 		kmf_free_algoid(&spki->algorithm);
125530a5e8faSwyllys 		kmf_free_data(&spki->subjectPublicKey);
125699ebb4caSwyllys 	}
125799ebb4caSwyllys }
125899ebb4caSwyllys 
125999ebb4caSwyllys void
126030a5e8faSwyllys kmf_free_kmf_key(KMF_HANDLE_T handle, KMF_KEY_HANDLE *key)
126199ebb4caSwyllys {
126299ebb4caSwyllys 	KMF_PLUGIN *plugin;
126399ebb4caSwyllys 	KMF_RETURN ret;
126430a5e8faSwyllys 	KMF_ATTRIBUTE attlist[2]; /* only 2 attributes for DeleteKey op */
126530a5e8faSwyllys 	int i = 0;
126630a5e8faSwyllys 	boolean_t token_destroy = B_FALSE;
126730a5e8faSwyllys 
126830a5e8faSwyllys 	if (key == NULL)
126930a5e8faSwyllys 		return;
127099ebb4caSwyllys 
127199ebb4caSwyllys 	CLEAR_ERROR(handle, ret);
127299ebb4caSwyllys 	if (ret != KMF_OK)
127399ebb4caSwyllys 		return;
127499ebb4caSwyllys 
127530a5e8faSwyllys 	kmf_set_attr_at_index(attlist, i,
127630a5e8faSwyllys 	    KMF_KEY_HANDLE_ATTR, key, sizeof (KMF_KEY_HANDLE));
127730a5e8faSwyllys 	i++;
127830a5e8faSwyllys 
127930a5e8faSwyllys 	kmf_set_attr_at_index(attlist, i,
128030a5e8faSwyllys 	    KMF_DESTROY_BOOL_ATTR, &token_destroy, sizeof (boolean_t));
128130a5e8faSwyllys 	i++;
128299ebb4caSwyllys 
128399ebb4caSwyllys 	plugin = FindPlugin(handle, key->kstype);
128499ebb4caSwyllys 	if (plugin != NULL && plugin->funclist->DeleteKey != NULL) {
128530a5e8faSwyllys 		(void) plugin->funclist->DeleteKey(handle, i, attlist);
128699ebb4caSwyllys 	}
128799ebb4caSwyllys 
128899ebb4caSwyllys 	if (key->keylabel)
128999ebb4caSwyllys 		free(key->keylabel);
129099ebb4caSwyllys 
129199ebb4caSwyllys 	if (key->israw) {
129230a5e8faSwyllys 		kmf_free_raw_key(key->keyp);
129399ebb4caSwyllys 		free(key->keyp);
129499ebb4caSwyllys 	}
129599ebb4caSwyllys 
129699ebb4caSwyllys 	(void) memset(key, 0, sizeof (KMF_KEY_HANDLE));
129799ebb4caSwyllys }
129899ebb4caSwyllys 
129999ebb4caSwyllys void
130030a5e8faSwyllys kmf_free_bigint(KMF_BIGINT *big)
130199ebb4caSwyllys {
130299ebb4caSwyllys 	if (big != NULL && big->val != NULL) {
130302744e81Swyllys 		/* Clear it out before returning it to the pool */
130402744e81Swyllys 		(void) memset(big->val, 0x00, big->len);
130599ebb4caSwyllys 		free(big->val);
130699ebb4caSwyllys 		big->val = NULL;
130799ebb4caSwyllys 		big->len = 0;
130899ebb4caSwyllys 	}
130999ebb4caSwyllys }
131099ebb4caSwyllys 
131199ebb4caSwyllys static void
131299ebb4caSwyllys free_raw_rsa(KMF_RAW_RSA_KEY *key)
131399ebb4caSwyllys {
131499ebb4caSwyllys 	if (key == NULL)
131599ebb4caSwyllys 		return;
131630a5e8faSwyllys 	kmf_free_bigint(&key->mod);
131730a5e8faSwyllys 	kmf_free_bigint(&key->pubexp);
131830a5e8faSwyllys 	kmf_free_bigint(&key->priexp);
131930a5e8faSwyllys 	kmf_free_bigint(&key->prime1);
132030a5e8faSwyllys 	kmf_free_bigint(&key->prime2);
132130a5e8faSwyllys 	kmf_free_bigint(&key->exp1);
132230a5e8faSwyllys 	kmf_free_bigint(&key->exp2);
132330a5e8faSwyllys 	kmf_free_bigint(&key->coef);
132499ebb4caSwyllys }
132599ebb4caSwyllys 
132699ebb4caSwyllys static void
132799ebb4caSwyllys free_raw_dsa(KMF_RAW_DSA_KEY *key)
132899ebb4caSwyllys {
132999ebb4caSwyllys 	if (key == NULL)
133099ebb4caSwyllys 		return;
133130a5e8faSwyllys 	kmf_free_bigint(&key->prime);
133230a5e8faSwyllys 	kmf_free_bigint(&key->subprime);
133330a5e8faSwyllys 	kmf_free_bigint(&key->base);
133430a5e8faSwyllys 	kmf_free_bigint(&key->value);
133599ebb4caSwyllys }
133699ebb4caSwyllys 
133799ebb4caSwyllys static void
133899ebb4caSwyllys free_raw_sym(KMF_RAW_SYM_KEY *key)
133999ebb4caSwyllys {
134099ebb4caSwyllys 	if (key == NULL)
134199ebb4caSwyllys 		return;
134230a5e8faSwyllys 	kmf_free_bigint(&key->keydata);
134399ebb4caSwyllys }
134499ebb4caSwyllys 
134599ebb4caSwyllys void
134630a5e8faSwyllys kmf_free_raw_key(KMF_RAW_KEY_DATA *key)
134799ebb4caSwyllys {
134899ebb4caSwyllys 	if (key == NULL)
134999ebb4caSwyllys 		return;
135099ebb4caSwyllys 
135199ebb4caSwyllys 	switch (key->keytype) {
135299ebb4caSwyllys 	case KMF_RSA:
135399ebb4caSwyllys 		free_raw_rsa(&key->rawdata.rsa);
135499ebb4caSwyllys 		break;
135599ebb4caSwyllys 	case KMF_DSA:
135699ebb4caSwyllys 		free_raw_dsa(&key->rawdata.dsa);
135799ebb4caSwyllys 		break;
135899ebb4caSwyllys 	case KMF_AES:
135999ebb4caSwyllys 	case KMF_RC4:
136099ebb4caSwyllys 	case KMF_DES:
136199ebb4caSwyllys 	case KMF_DES3:
136299ebb4caSwyllys 		free_raw_sym(&key->rawdata.sym);
136399ebb4caSwyllys 		break;
136499ebb4caSwyllys 	}
13655b3e1433Swyllys 	if (key->label) {
13665b3e1433Swyllys 		free(key->label);
13675b3e1433Swyllys 		key->label = NULL;
13685b3e1433Swyllys 	}
13695b3e1433Swyllys 	kmf_free_data(&key->id);
137099ebb4caSwyllys }
137199ebb4caSwyllys 
137299ebb4caSwyllys void
137330a5e8faSwyllys kmf_free_raw_sym_key(KMF_RAW_SYM_KEY *key)
137499ebb4caSwyllys {
137599ebb4caSwyllys 	if (key == NULL)
137699ebb4caSwyllys 		return;
137730a5e8faSwyllys 	kmf_free_bigint(&key->keydata);
137899ebb4caSwyllys 	free(key);
137999ebb4caSwyllys }
138099ebb4caSwyllys 
138199ebb4caSwyllys /*
138299ebb4caSwyllys  * This function frees the space allocated for the name portion of a
138399ebb4caSwyllys  * KMF_CRL_DIST_POINT.
138499ebb4caSwyllys  */
138599ebb4caSwyllys void
138699ebb4caSwyllys free_dp_name(KMF_CRL_DIST_POINT *dp)
138799ebb4caSwyllys {
138899ebb4caSwyllys 	KMF_GENERALNAMES *fullname;
138999ebb4caSwyllys 	KMF_DATA *urldata;
139099ebb4caSwyllys 	int i;
139199ebb4caSwyllys 
139299ebb4caSwyllys 	if (dp == NULL)
139399ebb4caSwyllys 		return;
139499ebb4caSwyllys 
139599ebb4caSwyllys 	/* For phase 1, we only need to free the fullname space. */
139699ebb4caSwyllys 	fullname = &(dp->name.full_name);
139799ebb4caSwyllys 	if (fullname->number == 0)
139899ebb4caSwyllys 		return;
139999ebb4caSwyllys 
140099ebb4caSwyllys 	for (i = 0; i < fullname->number; i++) {
140199ebb4caSwyllys 		urldata = &(fullname->namelist[fullname->number - 1].name);
140230a5e8faSwyllys 		kmf_free_data(urldata);
140399ebb4caSwyllys 	}
140499ebb4caSwyllys 
140599ebb4caSwyllys 	free(fullname->namelist);
140699ebb4caSwyllys }
140799ebb4caSwyllys 
140899ebb4caSwyllys /*
140999ebb4caSwyllys  * This function frees the space allocated for a KMF_CRL_DIST_POINT.
141099ebb4caSwyllys  */
141199ebb4caSwyllys void
141299ebb4caSwyllys free_dp(KMF_CRL_DIST_POINT *dp)
141399ebb4caSwyllys {
141499ebb4caSwyllys 	if (dp == NULL)
141599ebb4caSwyllys 		return;
141699ebb4caSwyllys 
141799ebb4caSwyllys 	free_dp_name(dp);
141830a5e8faSwyllys 	kmf_free_data(&(dp->reasons));
141999ebb4caSwyllys 	/* Need not to free crl_issuer space at phase 1 */
142099ebb4caSwyllys }
142199ebb4caSwyllys 
142299ebb4caSwyllys /*
142399ebb4caSwyllys  * This function frees space for a KMF_X509EXT_CRLDISTPOINTS internally.
142499ebb4caSwyllys  */
142599ebb4caSwyllys void
142630a5e8faSwyllys kmf_free_crl_dist_pts(KMF_X509EXT_CRLDISTPOINTS *crl_dps)
142799ebb4caSwyllys {
142899ebb4caSwyllys 	int i;
142999ebb4caSwyllys 
143099ebb4caSwyllys 	if (crl_dps == NULL)
143199ebb4caSwyllys 		return;
143299ebb4caSwyllys 
143399ebb4caSwyllys 	for (i = 0; i < crl_dps->number; i++)
143499ebb4caSwyllys 		free_dp(&(crl_dps->dplist[i]));
143599ebb4caSwyllys 
143699ebb4caSwyllys 	free(crl_dps->dplist);
143799ebb4caSwyllys }
143899ebb4caSwyllys 
143999ebb4caSwyllys KMF_RETURN
144030a5e8faSwyllys kmf_create_ocsp_request(KMF_HANDLE_T handle,
144130a5e8faSwyllys 	int	num_args,
144230a5e8faSwyllys 	KMF_ATTRIBUTE	*attrlist)
144399ebb4caSwyllys {
144430a5e8faSwyllys 	KMF_RETURN ret = KMF_OK;
144599ebb4caSwyllys 	KMF_PLUGIN *plugin;
144630a5e8faSwyllys 	KMF_RETURN (*createReqFn)(void *, int num_args,
144730a5e8faSwyllys 	    KMF_ATTRIBUTE *attrlist);
144899ebb4caSwyllys 
144930a5e8faSwyllys 	KMF_ATTRIBUTE_TESTER required_attrs[] = {
145030a5e8faSwyllys 		{KMF_OCSP_REQUEST_FILENAME_ATTR, FALSE, 1, 0},
145130a5e8faSwyllys 		{KMF_USER_CERT_DATA_ATTR, FALSE, sizeof (KMF_DATA),
145230a5e8faSwyllys 			sizeof (KMF_DATA)},
145330a5e8faSwyllys 		{KMF_ISSUER_CERT_DATA_ATTR, FALSE, sizeof (KMF_DATA),
145430a5e8faSwyllys 			sizeof (KMF_DATA)},
145530a5e8faSwyllys 	};
145699ebb4caSwyllys 
145730a5e8faSwyllys 	int num_req_attrs = sizeof (required_attrs) /
145830a5e8faSwyllys 	    sizeof (KMF_ATTRIBUTE_TESTER);
145999ebb4caSwyllys 
146030a5e8faSwyllys 	if (handle == NULL)
146199ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
146299ebb4caSwyllys 
146330a5e8faSwyllys 	CLEAR_ERROR(handle, ret);
146430a5e8faSwyllys 
146530a5e8faSwyllys 	ret = test_attributes(num_req_attrs, required_attrs,
146630a5e8faSwyllys 	    0, NULL, num_args, attrlist);
146730a5e8faSwyllys 
146830a5e8faSwyllys 	if (ret != KMF_OK)
146930a5e8faSwyllys 		return (ret);
147030a5e8faSwyllys 
147199ebb4caSwyllys 	/*
147299ebb4caSwyllys 	 * This framework function is actually implemented in the openssl
147399ebb4caSwyllys 	 * plugin library, so we find the function address and call it.
147499ebb4caSwyllys 	 */
147599ebb4caSwyllys 	plugin = FindPlugin(handle, KMF_KEYSTORE_OPENSSL);
147699ebb4caSwyllys 	if (plugin == NULL || plugin->dldesc == NULL) {
147799ebb4caSwyllys 		return (KMF_ERR_PLUGIN_NOTFOUND);
147899ebb4caSwyllys 	}
147999ebb4caSwyllys 
148099ebb4caSwyllys 	createReqFn = (KMF_RETURN(*)())dlsym(plugin->dldesc,
148199ebb4caSwyllys 	    "OpenSSL_CreateOCSPRequest");
148299ebb4caSwyllys 	if (createReqFn == NULL) {
148399ebb4caSwyllys 		return (KMF_ERR_FUNCTION_NOT_FOUND);
148499ebb4caSwyllys 	}
148599ebb4caSwyllys 
148630a5e8faSwyllys 	return (createReqFn(handle, num_args, attrlist));
148730a5e8faSwyllys 
148899ebb4caSwyllys }
148999ebb4caSwyllys 
149099ebb4caSwyllys KMF_RETURN
149130a5e8faSwyllys kmf_get_ocsp_status_for_cert(KMF_HANDLE_T handle,
149230a5e8faSwyllys 	int	num_args,
149330a5e8faSwyllys 	KMF_ATTRIBUTE	*attrlist)
149499ebb4caSwyllys {
149530a5e8faSwyllys 	KMF_RETURN ret = KMF_OK;
149699ebb4caSwyllys 	KMF_PLUGIN *plugin;
149730a5e8faSwyllys 	KMF_RETURN (*getCertStatusFn)(void *, int num_args,
149830a5e8faSwyllys 	    KMF_ATTRIBUTE *attrlist);
149930a5e8faSwyllys 
150030a5e8faSwyllys 	KMF_ATTRIBUTE_TESTER required_attrs[] = {
150130a5e8faSwyllys 		{KMF_USER_CERT_DATA_ATTR, FALSE, sizeof (KMF_DATA),
150230a5e8faSwyllys 			sizeof (KMF_DATA)},
150330a5e8faSwyllys 		{KMF_ISSUER_CERT_DATA_ATTR, FALSE, sizeof (KMF_DATA),
150430a5e8faSwyllys 			sizeof (KMF_DATA)},
150530a5e8faSwyllys 		{KMF_OCSP_RESPONSE_DATA_ATTR, FALSE, sizeof (KMF_DATA),
150630a5e8faSwyllys 			sizeof (KMF_DATA)},
150730a5e8faSwyllys 		{KMF_OCSP_RESPONSE_STATUS_ATTR, FALSE, sizeof (int),
150830a5e8faSwyllys 			sizeof (uint32_t)},
150930a5e8faSwyllys 		{KMF_OCSP_RESPONSE_REASON_ATTR, FALSE, sizeof (int),
151030a5e8faSwyllys 			sizeof (uint32_t)},
151130a5e8faSwyllys 		{KMF_OCSP_RESPONSE_CERT_STATUS_ATTR, FALSE, sizeof (int),
151230a5e8faSwyllys 			sizeof (uint32_t)},
151330a5e8faSwyllys 	};
151430a5e8faSwyllys 
151530a5e8faSwyllys 	int num_req_attrs = sizeof (required_attrs) /
151630a5e8faSwyllys 	    sizeof (KMF_ATTRIBUTE_TESTER);
151730a5e8faSwyllys 
151830a5e8faSwyllys 	if (handle == NULL)
151930a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
152099ebb4caSwyllys 
152199ebb4caSwyllys 	CLEAR_ERROR(handle, ret);
152299ebb4caSwyllys 
152330a5e8faSwyllys 	ret = test_attributes(num_req_attrs, required_attrs,
152430a5e8faSwyllys 	    0, NULL, num_args, attrlist);
152599ebb4caSwyllys 
152630a5e8faSwyllys 	if (ret != KMF_OK)
152730a5e8faSwyllys 		return (ret);
152899ebb4caSwyllys 
152999ebb4caSwyllys 	/*
153099ebb4caSwyllys 	 * This framework function is actually implemented in the openssl
153199ebb4caSwyllys 	 * plugin library, so we find the function address and call it.
153299ebb4caSwyllys 	 */
153399ebb4caSwyllys 	plugin = FindPlugin(handle, KMF_KEYSTORE_OPENSSL);
153499ebb4caSwyllys 	if (plugin == NULL || plugin->dldesc == NULL) {
153599ebb4caSwyllys 		return (KMF_ERR_INTERNAL);
153699ebb4caSwyllys 	}
153799ebb4caSwyllys 
153899ebb4caSwyllys 	getCertStatusFn = (KMF_RETURN(*)())dlsym(plugin->dldesc,
153999ebb4caSwyllys 	    "OpenSSL_GetOCSPStatusForCert");
154099ebb4caSwyllys 	if (getCertStatusFn == NULL) {
154199ebb4caSwyllys 		return (KMF_ERR_INTERNAL);
154299ebb4caSwyllys 	}
154399ebb4caSwyllys 
154430a5e8faSwyllys 	return (getCertStatusFn(handle, num_args, attrlist));
154530a5e8faSwyllys 
154699ebb4caSwyllys }
154799ebb4caSwyllys 
154899ebb4caSwyllys KMF_RETURN
154930a5e8faSwyllys kmf_string_to_oid(char *oidstring, KMF_OID *oid)
155099ebb4caSwyllys {
155199ebb4caSwyllys 	KMF_RETURN rv = KMF_OK;
155299ebb4caSwyllys 	char *cp, *bp, *startp;
155399ebb4caSwyllys 	int numbuf;
155499ebb4caSwyllys 	int onumbuf;
155599ebb4caSwyllys 	int nbytes, index;
155699ebb4caSwyllys 	int len;
155799ebb4caSwyllys 	unsigned char *op;
155899ebb4caSwyllys 
155999ebb4caSwyllys 	if (oidstring == NULL || oid == NULL)
156099ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
156199ebb4caSwyllys 
156299ebb4caSwyllys 	len = strlen(oidstring);
156399ebb4caSwyllys 
156499ebb4caSwyllys 	bp = oidstring;
156599ebb4caSwyllys 	cp = bp;
156699ebb4caSwyllys 	/* Skip over leading space */
156799ebb4caSwyllys 	while ((bp < &cp[len]) && isspace(*bp))
156899ebb4caSwyllys 		bp++;
156999ebb4caSwyllys 
157099ebb4caSwyllys 	startp = bp;
157199ebb4caSwyllys 	nbytes = 0;
157299ebb4caSwyllys 
157399ebb4caSwyllys 	/*
157499ebb4caSwyllys 	 * The first two numbers are chewed up by the first octet.
157599ebb4caSwyllys 	 */
157699ebb4caSwyllys 	if (sscanf(bp, "%d", &numbuf) != 1)
157799ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
157899ebb4caSwyllys 	while ((bp < &cp[len]) && isdigit(*bp))
157999ebb4caSwyllys 		bp++;
158099ebb4caSwyllys 	while ((bp < &cp[len]) && (isspace(*bp) || *bp == '.'))
158199ebb4caSwyllys 		bp++;
158299ebb4caSwyllys 	if (sscanf(bp, "%d", &numbuf) != 1)
158399ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
158499ebb4caSwyllys 	while ((bp < &cp[len]) && isdigit(*bp))
158599ebb4caSwyllys 		bp++;
158699ebb4caSwyllys 	while ((bp < &cp[len]) && (isspace(*bp) || *bp == '.'))
158799ebb4caSwyllys 		bp++;
158899ebb4caSwyllys 	nbytes++;
158999ebb4caSwyllys 
159099ebb4caSwyllys 	while (isdigit(*bp)) {
159199ebb4caSwyllys 		if (sscanf(bp, "%d", &numbuf) != 1)
159299ebb4caSwyllys 			return (KMF_ERR_BAD_PARAMETER);
159399ebb4caSwyllys 		while (numbuf) {
159499ebb4caSwyllys 			nbytes++;
159599ebb4caSwyllys 			numbuf >>= 7;
159699ebb4caSwyllys 		}
159799ebb4caSwyllys 		while ((bp < &cp[len]) && isdigit(*bp))
159899ebb4caSwyllys 			bp++;
159999ebb4caSwyllys 		while ((bp < &cp[len]) && (isspace(*bp) || *bp == '.'))
160099ebb4caSwyllys 			bp++;
160199ebb4caSwyllys 	}
160299ebb4caSwyllys 
160399ebb4caSwyllys 	oid->Length = nbytes;
160499ebb4caSwyllys 	oid->Data = malloc(oid->Length);
160599ebb4caSwyllys 	if (oid->Data == NULL) {
160699ebb4caSwyllys 		return (KMF_ERR_MEMORY);
160799ebb4caSwyllys 	}
160899ebb4caSwyllys 	(void) memset(oid->Data, 0, oid->Length);
160999ebb4caSwyllys 
161099ebb4caSwyllys 	op = oid->Data;
161199ebb4caSwyllys 
161299ebb4caSwyllys 	bp = startp;
161399ebb4caSwyllys 	(void) sscanf(bp, "%d", &numbuf);
161499ebb4caSwyllys 
161599ebb4caSwyllys 	while (isdigit(*bp)) bp++;
161699ebb4caSwyllys 	while (isspace(*bp) || *bp == '.') bp++;
161799ebb4caSwyllys 
161899ebb4caSwyllys 	onumbuf = 40 * numbuf;
161999ebb4caSwyllys 	(void) sscanf(bp, "%d", &numbuf);
162099ebb4caSwyllys 	onumbuf += numbuf;
162199ebb4caSwyllys 	*op = (unsigned char) onumbuf;
162299ebb4caSwyllys 	op++;
162399ebb4caSwyllys 
162499ebb4caSwyllys 	while (isdigit(*bp)) bp++;
162599ebb4caSwyllys 	while (isspace(*bp) || *bp == '.') bp++;
162699ebb4caSwyllys 	while (isdigit(*bp)) {
162799ebb4caSwyllys 		(void) sscanf(bp, "%d", &numbuf);
162899ebb4caSwyllys 		nbytes = 0;
162999ebb4caSwyllys 		/* Have to fill in the bytes msb-first */
163099ebb4caSwyllys 		onumbuf = numbuf;
163199ebb4caSwyllys 		while (numbuf) {
163299ebb4caSwyllys 			nbytes++;
163399ebb4caSwyllys 			numbuf >>= 7;
163499ebb4caSwyllys 		}
163599ebb4caSwyllys 		numbuf = onumbuf;
163699ebb4caSwyllys 		op += nbytes;
163799ebb4caSwyllys 		index = -1;
163899ebb4caSwyllys 		while (numbuf) {
163999ebb4caSwyllys 			op[index] = (unsigned char)numbuf & 0x7f;
164099ebb4caSwyllys 			if (index != -1)
164199ebb4caSwyllys 				op[index] |= 0x80;
164299ebb4caSwyllys 			index--;
164399ebb4caSwyllys 			numbuf >>= 7;
164499ebb4caSwyllys 		}
164599ebb4caSwyllys 		while (isdigit(*bp)) bp++;
164699ebb4caSwyllys 		while (isspace(*bp) || *bp == '.') bp++;
164799ebb4caSwyllys 	}
164899ebb4caSwyllys 
164999ebb4caSwyllys 	return (rv);
165099ebb4caSwyllys }
165199ebb4caSwyllys 
165299ebb4caSwyllys static KMF_RETURN
165399ebb4caSwyllys encode_rid(char *name, KMF_DATA *derdata)
165499ebb4caSwyllys {
165599ebb4caSwyllys 	KMF_RETURN rv = KMF_OK;
165699ebb4caSwyllys 
165799ebb4caSwyllys 	if (name == NULL || derdata == NULL)
165899ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
165999ebb4caSwyllys 
166030a5e8faSwyllys 	rv = kmf_string_to_oid(name, (KMF_OID *)derdata);
166199ebb4caSwyllys 
166299ebb4caSwyllys 	return (rv);
166399ebb4caSwyllys }
166499ebb4caSwyllys 
166599ebb4caSwyllys static KMF_RETURN
166699ebb4caSwyllys encode_ipaddr(char *name, KMF_DATA *derdata)
166799ebb4caSwyllys {
166899ebb4caSwyllys 	KMF_RETURN rv = KMF_OK;
166999ebb4caSwyllys 	size_t len;
167099ebb4caSwyllys 	in_addr_t v4;
167199ebb4caSwyllys 	in6_addr_t v6;
167299ebb4caSwyllys 	uint8_t *ptr;
167399ebb4caSwyllys 
167499ebb4caSwyllys 	if (name == NULL || derdata == NULL)
167599ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
167699ebb4caSwyllys 
167799ebb4caSwyllys 	v4 = inet_addr(name);
167899ebb4caSwyllys 	if (v4 == (in_addr_t)-1) {
167999ebb4caSwyllys 		ptr = (uint8_t *)&v6;
168099ebb4caSwyllys 		if (inet_pton(AF_INET6, name, ptr) != 1)
168199ebb4caSwyllys 			return (KMF_ERR_ENCODING);
168299ebb4caSwyllys 		len = sizeof (v6);
168399ebb4caSwyllys 	} else {
168499ebb4caSwyllys 		ptr = (uint8_t *)&v4;
168599ebb4caSwyllys 		len = sizeof (v4);
168699ebb4caSwyllys 	}
168799ebb4caSwyllys 
168899ebb4caSwyllys 	derdata->Data = malloc(len);
168999ebb4caSwyllys 	if (derdata->Data == NULL)
169099ebb4caSwyllys 		return (KMF_ERR_MEMORY);
169199ebb4caSwyllys 	(void) memcpy(derdata->Data, ptr, len);
169299ebb4caSwyllys 	derdata->Length = len;
169399ebb4caSwyllys 
169499ebb4caSwyllys 	return (rv);
169599ebb4caSwyllys }
169699ebb4caSwyllys 
1697d00756ccSwyllys static KMF_RETURN
1698d00756ccSwyllys encode_krb5(char *name, KMF_DATA *derdata)
1699d00756ccSwyllys {
1700d00756ccSwyllys 	KMF_RETURN rv = KMF_OK;
1701d00756ccSwyllys 	char *at, *realm;
1702d00756ccSwyllys 	BerElement *asn1 = NULL;
1703d00756ccSwyllys 	BerValue *extdata = NULL;
1704d00756ccSwyllys 
1705d00756ccSwyllys 	at = strchr(name, '@');
1706d00756ccSwyllys 	if (at == NULL)
1707d00756ccSwyllys 		return (KMF_ERR_ENCODING);
1708d00756ccSwyllys 
1709d00756ccSwyllys 	realm = at+1;
1710d00756ccSwyllys 	*at = 0;
1711d00756ccSwyllys 
1712d00756ccSwyllys 	if ((asn1 = kmfder_alloc()) == NULL)
1713d00756ccSwyllys 		return (KMF_ERR_MEMORY);
1714d00756ccSwyllys 
1715d00756ccSwyllys 	if (kmfber_printf(asn1, "{D{", &KMFOID_PKINIT_san) == -1)
1716d00756ccSwyllys 		goto cleanup;
1717d00756ccSwyllys 
1718d00756ccSwyllys 	if (kmfber_printf(asn1, "l", strlen(realm)) == -1)
1719d00756ccSwyllys 		goto cleanup;
1720d00756ccSwyllys 	if (kmfber_write(asn1, realm, strlen(realm), 0) != strlen(realm))
1721d00756ccSwyllys 		goto cleanup;
1722d00756ccSwyllys 	if (kmfber_printf(asn1, "l", strlen(name)) == -1)
1723d00756ccSwyllys 		goto cleanup;
1724d00756ccSwyllys 	if (kmfber_write(asn1, name, strlen(name), 0) != strlen(name))
1725d00756ccSwyllys 		goto cleanup;
1726d00756ccSwyllys 	if (kmfber_printf(asn1, "}}") == -1)
1727d00756ccSwyllys 		goto cleanup;
1728d00756ccSwyllys 
1729d00756ccSwyllys 	if (kmfber_flatten(asn1, &extdata) == -1) {
1730d00756ccSwyllys 		rv = KMF_ERR_ENCODING;
1731d00756ccSwyllys 		goto cleanup;
1732d00756ccSwyllys 	}
1733d00756ccSwyllys 
1734d00756ccSwyllys 	derdata->Data = (uchar_t *)extdata->bv_val;
1735d00756ccSwyllys 	derdata->Length = extdata->bv_len;
1736d00756ccSwyllys 
1737d00756ccSwyllys 	free(extdata);
1738d00756ccSwyllys cleanup:
1739d00756ccSwyllys 	if (asn1 != NULL)
1740d00756ccSwyllys 		kmfber_free(asn1, 1);
1741d00756ccSwyllys 
1742d00756ccSwyllys 	if (*at == 0)
1743d00756ccSwyllys 		*at = '@';
1744d00756ccSwyllys 
1745d00756ccSwyllys 	return (rv);
1746d00756ccSwyllys }
1747d00756ccSwyllys 
1748d00756ccSwyllys static KMF_RETURN
1749d00756ccSwyllys encode_sclogon(char *name, KMF_DATA *derdata)
1750d00756ccSwyllys {
1751d00756ccSwyllys 	KMF_RETURN rv = KMF_OK;
1752d00756ccSwyllys 	BerElement *asn1 = NULL;
1753d00756ccSwyllys 	BerValue *extdata = NULL;
1754d00756ccSwyllys 
1755d00756ccSwyllys 	if ((asn1 = kmfder_alloc()) == NULL)
1756d00756ccSwyllys 		return (KMF_ERR_MEMORY);
1757d00756ccSwyllys 
1758d00756ccSwyllys 	/* The name is encoded as a KerberosString (IA5STRING) */
1759d00756ccSwyllys 	if (kmfber_printf(asn1, "{Ds}",
1760d00756ccSwyllys 	    &KMFOID_MS_KP_SCLogon, name) == -1)
1761d00756ccSwyllys 		goto cleanup;
1762d00756ccSwyllys 
1763d00756ccSwyllys 	if (kmfber_flatten(asn1, &extdata) == -1) {
1764d00756ccSwyllys 		rv = KMF_ERR_ENCODING;
1765d00756ccSwyllys 		goto cleanup;
1766d00756ccSwyllys 	}
1767d00756ccSwyllys 
1768d00756ccSwyllys 	derdata->Data = (uchar_t *)extdata->bv_val;
1769d00756ccSwyllys 	derdata->Length = extdata->bv_len;
1770d00756ccSwyllys 
1771d00756ccSwyllys 	free(extdata);
1772d00756ccSwyllys cleanup:
1773d00756ccSwyllys 	if (asn1 != NULL)
1774d00756ccSwyllys 		kmfber_free(asn1, 1);
1775d00756ccSwyllys 
1776d00756ccSwyllys 	return (rv);
1777d00756ccSwyllys }
1778d00756ccSwyllys 
177999ebb4caSwyllys static KMF_RETURN
178099ebb4caSwyllys verify_uri_format(char *uristring)
178199ebb4caSwyllys {
178299ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
178399ebb4caSwyllys 	xmlURIPtr   uriptr = NULL;
178499ebb4caSwyllys 
178599ebb4caSwyllys 	/* Parse the URI string; get the hostname and port */
178699ebb4caSwyllys 	uriptr = xmlParseURI(uristring);
178799ebb4caSwyllys 	if (uriptr == NULL) {
178899ebb4caSwyllys 		ret = KMF_ERR_BAD_URI;
178999ebb4caSwyllys 		goto out;
179099ebb4caSwyllys 	}
179199ebb4caSwyllys 
179299ebb4caSwyllys 	if (uriptr->scheme == NULL || !strlen(uriptr->scheme)) {
179399ebb4caSwyllys 		ret = KMF_ERR_BAD_URI;
179499ebb4caSwyllys 		goto out;
179599ebb4caSwyllys 	}
179699ebb4caSwyllys 
179799ebb4caSwyllys 	if (uriptr->server == NULL || !strlen(uriptr->server)) {
179899ebb4caSwyllys 		ret = KMF_ERR_BAD_URI;
179999ebb4caSwyllys 		goto out;
180099ebb4caSwyllys 	}
180199ebb4caSwyllys out:
180299ebb4caSwyllys 	if (uriptr != NULL)
180399ebb4caSwyllys 		xmlFreeURI(uriptr);
180499ebb4caSwyllys 	return (ret);
180599ebb4caSwyllys }
180699ebb4caSwyllys 
180799ebb4caSwyllys static KMF_RETURN
180899ebb4caSwyllys encode_altname(char *namedata,
180999ebb4caSwyllys 	KMF_GENERALNAMECHOICES nametype, KMF_DATA *encodedname)
181099ebb4caSwyllys {
181199ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
181299ebb4caSwyllys 	KMF_X509_NAME dnname;
181399ebb4caSwyllys 	uchar_t tagval;
181499ebb4caSwyllys 	BerElement *asn1 = NULL;
181599ebb4caSwyllys 	BerValue *extdata;
181699ebb4caSwyllys 
181799ebb4caSwyllys 	if (namedata == NULL || encodedname == NULL)
181899ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
181999ebb4caSwyllys 
182099ebb4caSwyllys 	/*
182199ebb4caSwyllys 	 * Encode the namedata according to rules in RFC 3280 for GeneralName.
182299ebb4caSwyllys 	 * The input "namedata" is assumed to be an ASCII string representation
182399ebb4caSwyllys 	 * of the AltName, we need to convert it to correct ASN.1 here before
182499ebb4caSwyllys 	 * adding it to the cert.
182599ebb4caSwyllys 	 */
182699ebb4caSwyllys 	switch (nametype) {
182799ebb4caSwyllys 		case GENNAME_RFC822NAME: /* rfc 822 */
182899ebb4caSwyllys 			/* IA5String, no encoding needed */
182999ebb4caSwyllys 			encodedname->Data = (uchar_t *)strdup(namedata);
183099ebb4caSwyllys 			if (encodedname->Data == NULL)
183199ebb4caSwyllys 				return (KMF_ERR_MEMORY);
183299ebb4caSwyllys 			encodedname->Length = strlen(namedata);
183399ebb4caSwyllys 			tagval = (0x80 | nametype);
183499ebb4caSwyllys 			break;
183599ebb4caSwyllys 		case GENNAME_DNSNAME: /* rfc 1034 */
183699ebb4caSwyllys 			encodedname->Data = (uchar_t *)strdup(namedata);
183799ebb4caSwyllys 			if (encodedname->Data == NULL)
183899ebb4caSwyllys 				return (KMF_ERR_MEMORY);
183999ebb4caSwyllys 			encodedname->Length = strlen(namedata);
184099ebb4caSwyllys 			tagval = (0x80 | nametype);
184199ebb4caSwyllys 			break;
184299ebb4caSwyllys 		case GENNAME_URI: /* rfc 1738 */
184399ebb4caSwyllys 			ret = verify_uri_format(namedata);
184499ebb4caSwyllys 			if (ret != KMF_OK)
184599ebb4caSwyllys 				return (ret);
184699ebb4caSwyllys 			/* IA5String, no encoding needed */
184799ebb4caSwyllys 			encodedname->Data = (uchar_t *)strdup(namedata);
184899ebb4caSwyllys 			if (encodedname->Data == NULL)
184999ebb4caSwyllys 				return (KMF_ERR_MEMORY);
185099ebb4caSwyllys 			encodedname->Length = strlen(namedata);
185199ebb4caSwyllys 			tagval = (0x80 | nametype);
185299ebb4caSwyllys 			break;
185399ebb4caSwyllys 		case GENNAME_IPADDRESS:
185499ebb4caSwyllys 			ret =  encode_ipaddr(namedata, encodedname);
185599ebb4caSwyllys 			tagval = (0x80 | nametype);
185699ebb4caSwyllys 			break;
185799ebb4caSwyllys 		case GENNAME_REGISTEREDID:
185899ebb4caSwyllys 			ret = encode_rid(namedata, encodedname);
185999ebb4caSwyllys 			tagval = (0x80 | nametype);
186099ebb4caSwyllys 			break;
186199ebb4caSwyllys 		case GENNAME_DIRECTORYNAME:
186230a5e8faSwyllys 			ret = kmf_dn_parser(namedata, &dnname);
186399ebb4caSwyllys 			if (ret == KMF_OK) {
186430a5e8faSwyllys 				ret = DerEncodeName(&dnname, encodedname);
186599ebb4caSwyllys 			}
186630a5e8faSwyllys 			(void) kmf_free_dn(&dnname);
186799ebb4caSwyllys 			tagval = (0xA0 | nametype);
186899ebb4caSwyllys 			break;
1869d00756ccSwyllys 		case GENNAME_KRB5PRINC:
1870d00756ccSwyllys 			tagval = (0x80 | GENNAME_OTHERNAME);
1871d00756ccSwyllys 			ret = encode_krb5(namedata, encodedname);
1872d00756ccSwyllys 			break;
1873d00756ccSwyllys 		case GENNAME_SCLOGON_UPN:
1874d00756ccSwyllys 			tagval = (0x80 | GENNAME_OTHERNAME);
1875d00756ccSwyllys 			ret = encode_sclogon(namedata, encodedname);
1876d00756ccSwyllys 			break;
187799ebb4caSwyllys 		default:
187899ebb4caSwyllys 			/* unsupported */
187999ebb4caSwyllys 			return (KMF_ERR_BAD_PARAMETER);
188099ebb4caSwyllys 
188199ebb4caSwyllys 	}
188299ebb4caSwyllys 	if (ret != KMF_OK) {
188330a5e8faSwyllys 		kmf_free_data(encodedname);
188499ebb4caSwyllys 		return (ret);
188599ebb4caSwyllys 	}
188699ebb4caSwyllys 
188799ebb4caSwyllys 	if ((asn1 = kmfder_alloc()) == NULL)
188899ebb4caSwyllys 		return (KMF_ERR_MEMORY);
188999ebb4caSwyllys 
189030a5e8faSwyllys 	if (kmfber_printf(asn1, "Tl", tagval, encodedname->Length) == -1)
189199ebb4caSwyllys 		goto cleanup;
189299ebb4caSwyllys 
189399ebb4caSwyllys 	if (kmfber_write(asn1, (char *)encodedname->Data,
189430a5e8faSwyllys 	    encodedname->Length, 0) == -1) {
189599ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
189699ebb4caSwyllys 		goto cleanup;
189799ebb4caSwyllys 	}
189899ebb4caSwyllys 	if (kmfber_flatten(asn1, &extdata) == -1) {
189999ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
190099ebb4caSwyllys 		goto cleanup;
190199ebb4caSwyllys 	}
190299ebb4caSwyllys 
190330a5e8faSwyllys 	kmf_free_data(encodedname);
190499ebb4caSwyllys 	encodedname->Data = (uchar_t *)extdata->bv_val;
190599ebb4caSwyllys 	encodedname->Length = extdata->bv_len;
190699ebb4caSwyllys 
190799ebb4caSwyllys 	free(extdata);
190899ebb4caSwyllys 
190999ebb4caSwyllys cleanup:
191099ebb4caSwyllys 	if (asn1)
191199ebb4caSwyllys 		kmfber_free(asn1, 1);
191299ebb4caSwyllys 
191399ebb4caSwyllys 	if (ret != KMF_OK)
191430a5e8faSwyllys 		kmf_free_data(encodedname);
191599ebb4caSwyllys 
191699ebb4caSwyllys 	return (ret);
191799ebb4caSwyllys }
191899ebb4caSwyllys 
191999ebb4caSwyllys KMF_X509_EXTENSION *
192099ebb4caSwyllys FindExtn(KMF_X509_EXTENSIONS *exts, KMF_OID *oid)
192199ebb4caSwyllys {
192299ebb4caSwyllys 	KMF_X509_EXTENSION *foundextn = NULL;
192399ebb4caSwyllys 	int i;
192499ebb4caSwyllys 
192599ebb4caSwyllys 	if (exts == NULL || oid == NULL)
192699ebb4caSwyllys 		return (NULL);
192799ebb4caSwyllys 
192899ebb4caSwyllys 	for (i = 0; i < exts->numberOfExtensions; i++) {
192999ebb4caSwyllys 		if (IsEqualOid(oid, &exts->extensions[i].extnId))  {
193099ebb4caSwyllys 			foundextn = &exts->extensions[i];
193199ebb4caSwyllys 			break;
193299ebb4caSwyllys 		}
193399ebb4caSwyllys 	}
193499ebb4caSwyllys 	return (foundextn);
193599ebb4caSwyllys }
193699ebb4caSwyllys 
193799ebb4caSwyllys KMF_RETURN
193899ebb4caSwyllys GetSequenceContents(char *data, size_t len,
193999ebb4caSwyllys 	char **contents, size_t *outlen)
194099ebb4caSwyllys {
194199ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
194299ebb4caSwyllys 	BerElement *exasn1 = NULL;
194399ebb4caSwyllys 	BerValue oldextn;
194499ebb4caSwyllys 	int tag;
194599ebb4caSwyllys 	size_t oldsize;
194699ebb4caSwyllys 	char *olddata = NULL;
194799ebb4caSwyllys 
194899ebb4caSwyllys 	if (data == NULL || contents == NULL || outlen == NULL)
194999ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
195099ebb4caSwyllys 
195199ebb4caSwyllys 	/*
195299ebb4caSwyllys 	 * Decode the sequence of general names
195399ebb4caSwyllys 	 */
195499ebb4caSwyllys 	oldextn.bv_val = data;
195599ebb4caSwyllys 	oldextn.bv_len = len;
195699ebb4caSwyllys 
195799ebb4caSwyllys 	if ((exasn1 = kmfder_init(&oldextn)) == NULL) {
195899ebb4caSwyllys 		ret = KMF_ERR_MEMORY;
195999ebb4caSwyllys 		goto out;
196099ebb4caSwyllys 	}
196199ebb4caSwyllys 
196299ebb4caSwyllys 	/*
196399ebb4caSwyllys 	 * Unwrap the sequence to find the size of the block
196499ebb4caSwyllys 	 * of GeneralName items in the set.
196599ebb4caSwyllys 	 *
196699ebb4caSwyllys 	 * Peek at the tag and length ("tl"),
196799ebb4caSwyllys 	 * then consume them ("{").
196899ebb4caSwyllys 	 */
196999ebb4caSwyllys 	if (kmfber_scanf(exasn1, "tl{", &tag, &oldsize) == KMFBER_DEFAULT ||
197030a5e8faSwyllys 	    oldsize == 0) {
197199ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
197299ebb4caSwyllys 		goto out;
197399ebb4caSwyllys 	}
197499ebb4caSwyllys 
197599ebb4caSwyllys 	olddata = malloc(oldsize);
197699ebb4caSwyllys 	if (olddata == NULL) {
197799ebb4caSwyllys 		ret = KMF_ERR_MEMORY;
197899ebb4caSwyllys 		goto out;
197999ebb4caSwyllys 	}
198099ebb4caSwyllys 	(void) memset(olddata, 0, oldsize);
198199ebb4caSwyllys 	/*
198299ebb4caSwyllys 	 * Read the entire blob of GeneralNames, we don't
198399ebb4caSwyllys 	 * need to interpret them now.
198499ebb4caSwyllys 	 */
198599ebb4caSwyllys 	if (kmfber_read(exasn1, olddata, oldsize) != oldsize) {
198699ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
198799ebb4caSwyllys 		goto out;
198899ebb4caSwyllys 	}
198999ebb4caSwyllys out:
199099ebb4caSwyllys 	if (exasn1 != NULL)
199199ebb4caSwyllys 		kmfber_free(exasn1, 1);
199299ebb4caSwyllys 
199399ebb4caSwyllys 	if (ret != KMF_OK) {
199499ebb4caSwyllys 		*contents = NULL;
199599ebb4caSwyllys 		*outlen = 0;
199699ebb4caSwyllys 		if (olddata != NULL)
199799ebb4caSwyllys 			free(olddata);
199899ebb4caSwyllys 	} else {
199999ebb4caSwyllys 		*contents = olddata;
200099ebb4caSwyllys 		*outlen = oldsize;
200199ebb4caSwyllys 	}
200299ebb4caSwyllys 	return (ret);
200399ebb4caSwyllys }
200499ebb4caSwyllys 
200599ebb4caSwyllys KMF_RETURN
200699ebb4caSwyllys add_an_extension(KMF_X509_EXTENSIONS *exts, KMF_X509_EXTENSION *newextn)
200799ebb4caSwyllys {
200899ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
200999ebb4caSwyllys 	KMF_X509_EXTENSION *extlist;
201099ebb4caSwyllys 
201199ebb4caSwyllys 	if (exts == NULL || newextn == NULL)
201299ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
201399ebb4caSwyllys 
201499ebb4caSwyllys 	extlist = malloc(sizeof (KMF_X509_EXTENSION) *
201530a5e8faSwyllys 	    (exts->numberOfExtensions + 1));
201699ebb4caSwyllys 	if (extlist == NULL)
201799ebb4caSwyllys 		return (KMF_ERR_MEMORY);
201899ebb4caSwyllys 
201999ebb4caSwyllys 	(void) memcpy(extlist, exts->extensions,
202099ebb4caSwyllys 	    exts->numberOfExtensions * sizeof (KMF_X509_EXTENSION));
202199ebb4caSwyllys 
202299ebb4caSwyllys 	(void) memcpy(&extlist[exts->numberOfExtensions], newextn,
202330a5e8faSwyllys 	    sizeof (KMF_X509_EXTENSION));
202499ebb4caSwyllys 
202599ebb4caSwyllys 	free(exts->extensions);
202699ebb4caSwyllys 	exts->numberOfExtensions++;
202799ebb4caSwyllys 	exts->extensions = extlist;
202899ebb4caSwyllys 
202999ebb4caSwyllys 	return (ret);
203099ebb4caSwyllys }
203199ebb4caSwyllys 
203299ebb4caSwyllys KMF_RETURN
203330a5e8faSwyllys kmf_set_altname(KMF_X509_EXTENSIONS *extensions,
203499ebb4caSwyllys 	KMF_OID *oid,
203599ebb4caSwyllys 	int critical,
203699ebb4caSwyllys 	KMF_GENERALNAMECHOICES nametype,
203799ebb4caSwyllys 	char *namedata)
203899ebb4caSwyllys {
203999ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
204099ebb4caSwyllys 	KMF_X509_EXTENSION subjAltName;
204199ebb4caSwyllys 	KMF_DATA dername = { NULL, 0 };
204299ebb4caSwyllys 	BerElement *asn1 = NULL;
204399ebb4caSwyllys 	BerValue *extdata;
204499ebb4caSwyllys 	char *olddata = NULL;
204599ebb4caSwyllys 	KMF_X509_EXTENSION *foundextn = NULL;
204699ebb4caSwyllys 	size_t	oldsize = 0;
204799ebb4caSwyllys 
204899ebb4caSwyllys 	if (extensions == NULL || oid == NULL || namedata == NULL)
204999ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
205099ebb4caSwyllys 
205199ebb4caSwyllys 	ret = encode_altname(namedata, nametype, &dername);
205299ebb4caSwyllys 
205399ebb4caSwyllys 	if (ret != KMF_OK)
205499ebb4caSwyllys 		return (ret);
205599ebb4caSwyllys 
205699ebb4caSwyllys 	(void) memset(&subjAltName, 0, sizeof (subjAltName));
205799ebb4caSwyllys 
205899ebb4caSwyllys 	ret = copy_data(&subjAltName.extnId, oid);
205999ebb4caSwyllys 	if (ret != KMF_OK)
206099ebb4caSwyllys 		goto out;
206199ebb4caSwyllys 	/*
206299ebb4caSwyllys 	 * Check to see if this cert already has a subjectAltName.
206399ebb4caSwyllys 	 */
206499ebb4caSwyllys 	foundextn = FindExtn(extensions, oid);
206599ebb4caSwyllys 
206699ebb4caSwyllys 	if (foundextn != NULL) {
206799ebb4caSwyllys 		ret = GetSequenceContents(
206830a5e8faSwyllys 		    (char *)foundextn->BERvalue.Data,
206930a5e8faSwyllys 		    foundextn->BERvalue.Length,
207030a5e8faSwyllys 		    &olddata, &oldsize);
207199ebb4caSwyllys 		if (ret != KMF_OK)
207299ebb4caSwyllys 			goto out;
207399ebb4caSwyllys 	}
207499ebb4caSwyllys 
207599ebb4caSwyllys 	/*
207699ebb4caSwyllys 	 * Assume (!!) that the namedata given is already properly encoded.
207799ebb4caSwyllys 	 */
207899ebb4caSwyllys 	if ((asn1 = kmfder_alloc()) == NULL)
207999ebb4caSwyllys 		return (KMF_ERR_MEMORY);
208099ebb4caSwyllys 
208199ebb4caSwyllys 	if (kmfber_printf(asn1, "{") == -1) {
208299ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
208399ebb4caSwyllys 		goto out;
208499ebb4caSwyllys 	}
208599ebb4caSwyllys 
208699ebb4caSwyllys 	/* Write the old extension data first */
208799ebb4caSwyllys 	if (olddata != NULL && oldsize > 0) {
208899ebb4caSwyllys 		if (kmfber_write(asn1, olddata, oldsize, 0) == -1) {
208999ebb4caSwyllys 			ret = KMF_ERR_ENCODING;
209099ebb4caSwyllys 			goto out;
209199ebb4caSwyllys 		}
209299ebb4caSwyllys 	}
209399ebb4caSwyllys 
209499ebb4caSwyllys 	/* Now add the new name to the list */
209599ebb4caSwyllys 	if (kmfber_write(asn1, (char *)dername.Data, dername.Length, 0) == -1) {
209699ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
209799ebb4caSwyllys 		goto out;
209899ebb4caSwyllys 	}
209999ebb4caSwyllys 
210099ebb4caSwyllys 	/* Now close the sequence */
210199ebb4caSwyllys 	if (kmfber_printf(asn1, "}") == -1) {
210299ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
210399ebb4caSwyllys 		goto out;
210499ebb4caSwyllys 	}
210599ebb4caSwyllys 	if (kmfber_flatten(asn1, &extdata) == -1) {
210699ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
210799ebb4caSwyllys 		goto out;
210899ebb4caSwyllys 	}
210999ebb4caSwyllys 
211099ebb4caSwyllys 	/*
211199ebb4caSwyllys 	 * If we are just adding to an existing list of altNames,
211299ebb4caSwyllys 	 * just replace the BER data associated with the found extension.
211399ebb4caSwyllys 	 */
211499ebb4caSwyllys 	if (foundextn != NULL) {
211599ebb4caSwyllys 		free(foundextn->BERvalue.Data);
211699ebb4caSwyllys 		foundextn->critical = critical;
211799ebb4caSwyllys 		foundextn->BERvalue.Data = (uchar_t *)extdata->bv_val;
211899ebb4caSwyllys 		foundextn->BERvalue.Length = extdata->bv_len;
211999ebb4caSwyllys 	} else {
212099ebb4caSwyllys 		subjAltName.critical = critical;
212199ebb4caSwyllys 		subjAltName.format = KMF_X509_DATAFORMAT_ENCODED;
212299ebb4caSwyllys 		subjAltName.BERvalue.Data = (uchar_t *)extdata->bv_val;
212399ebb4caSwyllys 		subjAltName.BERvalue.Length = extdata->bv_len;
212499ebb4caSwyllys 		ret = add_an_extension(extensions, &subjAltName);
212599ebb4caSwyllys 		if (ret != KMF_OK)
212699ebb4caSwyllys 			free(subjAltName.BERvalue.Data);
212799ebb4caSwyllys 	}
212899ebb4caSwyllys 
212999ebb4caSwyllys 	free(extdata);
213099ebb4caSwyllys out:
213199ebb4caSwyllys 	if (olddata != NULL)
213299ebb4caSwyllys 		free(olddata);
213399ebb4caSwyllys 
213430a5e8faSwyllys 	kmf_free_data(&dername);
213599ebb4caSwyllys 	if (ret != KMF_OK)
213630a5e8faSwyllys 		kmf_free_data(&subjAltName.extnId);
213799ebb4caSwyllys 	if (asn1 != NULL)
213899ebb4caSwyllys 		kmfber_free(asn1, 1);
213999ebb4caSwyllys 	return (ret);
214099ebb4caSwyllys }
214130a5e8faSwyllys 
214230a5e8faSwyllys /*
214330a5e8faSwyllys  * Search a list of attributes for one that matches the given type.
214430a5e8faSwyllys  * Return a pointer into the attribute list.  This does not
214530a5e8faSwyllys  * return a copy of the value, it returns a reference into the
214630a5e8faSwyllys  * given list.
214730a5e8faSwyllys  */
214830a5e8faSwyllys int
214930a5e8faSwyllys kmf_find_attr(KMF_ATTR_TYPE type, KMF_ATTRIBUTE *attlist, int numattrs)
215030a5e8faSwyllys {
215130a5e8faSwyllys 	int i;
215230a5e8faSwyllys 	for (i = 0; i < numattrs; i++) {
215330a5e8faSwyllys 		if (attlist[i].type == type)
215430a5e8faSwyllys 			return (i);
215530a5e8faSwyllys 	}
215630a5e8faSwyllys 	return (-1);
215730a5e8faSwyllys }
215830a5e8faSwyllys 
215930a5e8faSwyllys /*
216030a5e8faSwyllys  * Verify that a given attribute is consistent with the
216130a5e8faSwyllys  * "test" attribute.
216230a5e8faSwyllys  */
216330a5e8faSwyllys static KMF_RETURN
216430a5e8faSwyllys verify_attribute(KMF_ATTRIBUTE *givenattr,
216530a5e8faSwyllys 	KMF_ATTRIBUTE_TESTER *testattr)
216630a5e8faSwyllys {
216730a5e8faSwyllys 	/* A NULL pValue was found where one is required */
216830a5e8faSwyllys 	if (testattr->null_value_ok == FALSE &&
216930a5e8faSwyllys 	    givenattr->pValue == NULL)
217030a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
217130a5e8faSwyllys 
217230a5e8faSwyllys 	/* If the given valueLen is too small, return error */
217330a5e8faSwyllys 	if (givenattr->pValue != NULL &&
217430a5e8faSwyllys 	    testattr->minlen > 0 &&
217530a5e8faSwyllys 	    (givenattr->valueLen < testattr->minlen))
217630a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
217730a5e8faSwyllys 
217830a5e8faSwyllys 	/* If the given valueLen is too big, return error */
217930a5e8faSwyllys 	if (givenattr->pValue != NULL &&
218030a5e8faSwyllys 	    testattr->maxlen > 0 &&
218130a5e8faSwyllys 	    (givenattr->valueLen > testattr->maxlen))
218230a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
218330a5e8faSwyllys 
218430a5e8faSwyllys 	return (KMF_OK);
218530a5e8faSwyllys }
218630a5e8faSwyllys 
218730a5e8faSwyllys /*
218830a5e8faSwyllys  * Given a set of required attribute tests and optional
218930a5e8faSwyllys  * attributes, make sure that the actual attributes
219030a5e8faSwyllys  * being tested (attrlist below) are allowed and are
219130a5e8faSwyllys  * properly specified.
219230a5e8faSwyllys  */
219330a5e8faSwyllys KMF_RETURN
219430a5e8faSwyllys test_attributes(int reqnum, KMF_ATTRIBUTE_TESTER *reqattrs,
219530a5e8faSwyllys 	int optnum, KMF_ATTRIBUTE_TESTER *optattrs,
219630a5e8faSwyllys 	int numattrs, KMF_ATTRIBUTE *attrlist)
219730a5e8faSwyllys {
219830a5e8faSwyllys 	KMF_RETURN ret = KMF_OK;
219930a5e8faSwyllys 	int i, idx;
220030a5e8faSwyllys 
220130a5e8faSwyllys 	/*
220230a5e8faSwyllys 	 * If the caller didn't supply enough attributes,
220330a5e8faSwyllys 	 * return an error.
220430a5e8faSwyllys 	 */
220530a5e8faSwyllys 	if (numattrs < reqnum || attrlist == NULL)
220630a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
220730a5e8faSwyllys 
220830a5e8faSwyllys 	/*
220930a5e8faSwyllys 	 * Make sure all required attrs are present and
221030a5e8faSwyllys 	 * correct.
221130a5e8faSwyllys 	 */
221230a5e8faSwyllys 	for (i = 0; i < reqnum && ret == KMF_OK; i++) {
221330a5e8faSwyllys 		idx = kmf_find_attr(reqattrs[i].type, attrlist, numattrs);
221430a5e8faSwyllys 		/* If a required attr is not found, return error */
221530a5e8faSwyllys 		if (idx == -1) {
221630a5e8faSwyllys 			return (KMF_ERR_BAD_PARAMETER);
221730a5e8faSwyllys 		}
221830a5e8faSwyllys 
221930a5e8faSwyllys 		ret = verify_attribute(&attrlist[idx], &reqattrs[i]);
222030a5e8faSwyllys 	}
222130a5e8faSwyllys 	/*
222230a5e8faSwyllys 	 * Now test the optional parameters.
222330a5e8faSwyllys 	 */
222430a5e8faSwyllys 	for (i = 0; i < optnum && ret == KMF_OK; i++) {
222530a5e8faSwyllys 		idx = kmf_find_attr(optattrs[i].type, attrlist, numattrs);
222630a5e8faSwyllys 		/* If a optional attr is not found, continue. */
222730a5e8faSwyllys 		if (idx == -1) {
222830a5e8faSwyllys 			continue;
222930a5e8faSwyllys 		}
223030a5e8faSwyllys 
223130a5e8faSwyllys 		ret = verify_attribute(&attrlist[idx], &optattrs[i]);
223230a5e8faSwyllys 	}
223330a5e8faSwyllys 
223430a5e8faSwyllys 	return (ret);
223530a5e8faSwyllys }
223630a5e8faSwyllys 
223730a5e8faSwyllys /*
223830a5e8faSwyllys  * Given an already allocated attribute list, insert
223930a5e8faSwyllys  * the given attribute information at a specific index
224030a5e8faSwyllys  * in the list.
224130a5e8faSwyllys  */
224230a5e8faSwyllys void
224330a5e8faSwyllys kmf_set_attr_at_index(KMF_ATTRIBUTE *attlist, int index,
224430a5e8faSwyllys 	KMF_ATTR_TYPE type,  void *pValue, uint32_t len)
224530a5e8faSwyllys {
224630a5e8faSwyllys 	if (attlist == NULL)
224730a5e8faSwyllys 		return;
224830a5e8faSwyllys 
224930a5e8faSwyllys 	attlist[index].type = type;
225030a5e8faSwyllys 	attlist[index].pValue = pValue;
225130a5e8faSwyllys 	attlist[index].valueLen = len;
225230a5e8faSwyllys }
225330a5e8faSwyllys 
225430a5e8faSwyllys /*
225530a5e8faSwyllys  * Find an attribute matching a particular type and set
225630a5e8faSwyllys  * the pValue and length fields to the given values.
225730a5e8faSwyllys  */
225830a5e8faSwyllys KMF_RETURN
225930a5e8faSwyllys kmf_set_attr(KMF_ATTRIBUTE *attlist, int numattr,
226030a5e8faSwyllys 	KMF_ATTR_TYPE type,  void *pValue, uint32_t len)
226130a5e8faSwyllys {
226230a5e8faSwyllys 	int idx;
226330a5e8faSwyllys 	if (attlist == NULL)
226430a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
226530a5e8faSwyllys 
226630a5e8faSwyllys 	idx = kmf_find_attr(type, attlist, numattr);
226730a5e8faSwyllys 	if (idx == -1)
226830a5e8faSwyllys 		return (KMF_ERR_ATTR_NOT_FOUND);
226930a5e8faSwyllys 
227030a5e8faSwyllys 	attlist[idx].type = type;
227130a5e8faSwyllys 	/* Assumes the attribute pValue can hold the result */
227230a5e8faSwyllys 	if (attlist[idx].pValue != NULL) {
227330a5e8faSwyllys 		if (attlist[idx].valueLen >= len)
227430a5e8faSwyllys 			(void) memcpy(attlist[idx].pValue, pValue, len);
227530a5e8faSwyllys 		else
227630a5e8faSwyllys 			return (KMF_ERR_BUFFER_SIZE);
227730a5e8faSwyllys 	}
227830a5e8faSwyllys 	attlist[idx].valueLen = len;
227930a5e8faSwyllys 	return (KMF_OK);
228030a5e8faSwyllys }
228130a5e8faSwyllys 
228230a5e8faSwyllys /*
228330a5e8faSwyllys  * Find a particular attribute in a list and return
228430a5e8faSwyllys  * a pointer to its value.
228530a5e8faSwyllys  */
228630a5e8faSwyllys void *
228730a5e8faSwyllys kmf_get_attr_ptr(KMF_ATTR_TYPE type, KMF_ATTRIBUTE *attlist,
228830a5e8faSwyllys 	int numattrs)
228930a5e8faSwyllys {
229030a5e8faSwyllys 	int i;
229130a5e8faSwyllys 
229230a5e8faSwyllys 	i = kmf_find_attr(type, attlist, numattrs);
229330a5e8faSwyllys 	if (i == -1)
229430a5e8faSwyllys 		return (NULL);
229530a5e8faSwyllys 
229630a5e8faSwyllys 	return (attlist[i].pValue);
229730a5e8faSwyllys }
229830a5e8faSwyllys 
229930a5e8faSwyllys /*
230030a5e8faSwyllys  * Find a particular attribute in a list and return
230130a5e8faSwyllys  * the value and length values.  Value and length
230230a5e8faSwyllys  * may be NULL if the caller doesn't want their values
230330a5e8faSwyllys  * to be filled in.
230430a5e8faSwyllys  */
230530a5e8faSwyllys KMF_RETURN
230630a5e8faSwyllys kmf_get_attr(KMF_ATTR_TYPE type, KMF_ATTRIBUTE *attlist,
230730a5e8faSwyllys 	int numattrs, void *outValue, uint32_t *outlen)
230830a5e8faSwyllys {
230930a5e8faSwyllys 	int i;
231030a5e8faSwyllys 	uint32_t len = 0;
231130a5e8faSwyllys 	uint32_t *lenptr = outlen;
231230a5e8faSwyllys 
231330a5e8faSwyllys 	if (lenptr == NULL)
231430a5e8faSwyllys 		lenptr = &len;
231530a5e8faSwyllys 
231630a5e8faSwyllys 	i = kmf_find_attr(type, attlist, numattrs);
231730a5e8faSwyllys 	if (i == -1)
231830a5e8faSwyllys 		return (KMF_ERR_ATTR_NOT_FOUND);
231930a5e8faSwyllys 
232030a5e8faSwyllys 	/* This assumes that the ptr passed in is pre-allocated space */
232130a5e8faSwyllys 	if (attlist[i].pValue != NULL && outValue != NULL) {
232230a5e8faSwyllys 		/*
232330a5e8faSwyllys 		 * If the caller did not specify a length,
232430a5e8faSwyllys 		 * assume "outValue" is big enough.
232530a5e8faSwyllys 		 */
232630a5e8faSwyllys 		if (outlen != NULL) {
232730a5e8faSwyllys 			if (*outlen >= attlist[i].valueLen)
232830a5e8faSwyllys 				(void) memcpy(outValue, attlist[i].pValue,
232930a5e8faSwyllys 				    attlist[i].valueLen);
233030a5e8faSwyllys 			else
233130a5e8faSwyllys 				return (KMF_ERR_BUFFER_SIZE);
233230a5e8faSwyllys 		} else {
233330a5e8faSwyllys 			(void) memcpy(outValue, attlist[i].pValue,
233430a5e8faSwyllys 			    attlist[i].valueLen);
233530a5e8faSwyllys 		}
233630a5e8faSwyllys 	}
233730a5e8faSwyllys 
233830a5e8faSwyllys 	if (outlen != NULL)
233930a5e8faSwyllys 		*outlen = attlist[i].valueLen;
234030a5e8faSwyllys 	return (KMF_OK);
234130a5e8faSwyllys }
234230a5e8faSwyllys 
234330a5e8faSwyllys /*
234430a5e8faSwyllys  * Utility routine to find a string type attribute, allocate it
234530a5e8faSwyllys  * and return the value to the caller.  This simplifies the
234630a5e8faSwyllys  * operation by doing both "kmf_get_attr" calls and avoids
234730a5e8faSwyllys  * duplicating this block of code in lots of places.
234830a5e8faSwyllys  */
234930a5e8faSwyllys KMF_RETURN
235030a5e8faSwyllys kmf_get_string_attr(KMF_ATTR_TYPE type, KMF_ATTRIBUTE *attrlist,
235130a5e8faSwyllys 	int numattrs, char **outstr)
235230a5e8faSwyllys {
235330a5e8faSwyllys 	KMF_RETURN rv;
235430a5e8faSwyllys 	uint32_t len;
235530a5e8faSwyllys 
235630a5e8faSwyllys 	if (outstr == NULL)
235730a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
235830a5e8faSwyllys 
235930a5e8faSwyllys 	if ((rv = kmf_get_attr(type, attrlist, numattrs, NULL, &len)) ==
236030a5e8faSwyllys 	    KMF_OK) {
236130a5e8faSwyllys 		*outstr = malloc(len + 1);
236230a5e8faSwyllys 		if ((*outstr) == NULL)
236330a5e8faSwyllys 			return (KMF_ERR_MEMORY);
236430a5e8faSwyllys 		(void) memset((*outstr), 0, len + 1);
236530a5e8faSwyllys 		rv = kmf_get_attr(type, attrlist, numattrs, (*outstr), &len);
236630a5e8faSwyllys 		if (rv != KMF_OK) {
236730a5e8faSwyllys 			free(*outstr);
236830a5e8faSwyllys 			*outstr = NULL;
236930a5e8faSwyllys 		}
237030a5e8faSwyllys 	}
237130a5e8faSwyllys 
237230a5e8faSwyllys 	return (rv);
237330a5e8faSwyllys }
237430a5e8faSwyllys 
2375431deaa0Shylee 
2376431deaa0Shylee void
2377431deaa0Shylee free_entry(conf_entry_t *entry)
2378431deaa0Shylee {
2379431deaa0Shylee 	if (entry == NULL)
2380431deaa0Shylee 		return;
2381431deaa0Shylee 	free(entry->keystore);
2382431deaa0Shylee 	free(entry->modulepath);
2383431deaa0Shylee 	free(entry->option);
2384431deaa0Shylee }
2385431deaa0Shylee 
2386431deaa0Shylee void
2387431deaa0Shylee free_entrylist(conf_entrylist_t *list)
2388431deaa0Shylee {
2389431deaa0Shylee 	conf_entrylist_t *next;
2390431deaa0Shylee 
2391431deaa0Shylee 	while (list != NULL) {
2392431deaa0Shylee 		next = list->next;
2393431deaa0Shylee 		free_entry(list->entry);
2394431deaa0Shylee 		free(list);
2395431deaa0Shylee 		list = next;
2396431deaa0Shylee 	}
2397431deaa0Shylee }
2398431deaa0Shylee 
2399431deaa0Shylee static KMF_RETURN
2400431deaa0Shylee parse_entry(char *buf, conf_entry_t **entry)
2401431deaa0Shylee {
2402431deaa0Shylee 	KMF_RETURN ret = KMF_OK;
2403431deaa0Shylee 	conf_entry_t *tmp = NULL;
2404431deaa0Shylee 	char *token1;
2405431deaa0Shylee 	char *token2;
2406431deaa0Shylee 	char *token3;
2407431deaa0Shylee 	char *lasts;
2408431deaa0Shylee 	char *value;
2409431deaa0Shylee 
2410431deaa0Shylee 	if ((token1 = strtok_r(buf, SEP_COLON, &lasts)) == NULL)
2411431deaa0Shylee 		return (KMF_ERR_KMF_CONF);
2412431deaa0Shylee 
2413431deaa0Shylee 	if ((tmp = calloc(sizeof (conf_entry_t), 1)) == NULL)
2414431deaa0Shylee 		return (KMF_ERR_MEMORY);
2415431deaa0Shylee 
2416431deaa0Shylee 	if ((tmp->keystore = strdup(token1)) == NULL) {
2417431deaa0Shylee 		ret = KMF_ERR_MEMORY;
2418431deaa0Shylee 		goto end;
2419431deaa0Shylee 	}
2420431deaa0Shylee 
2421431deaa0Shylee 	if ((token2 = strtok_r(NULL, SEP_SEMICOLON, &lasts)) == NULL) {
2422431deaa0Shylee 		ret = KMF_ERR_KMF_CONF;
2423431deaa0Shylee 		goto end;
2424431deaa0Shylee 	}
2425431deaa0Shylee 
2426431deaa0Shylee 	/* need to get token3 first to satisfy nested strtok invocations */
2427431deaa0Shylee 	token3 = strtok_r(NULL, SEP_SEMICOLON, &lasts);
2428431deaa0Shylee 
2429431deaa0Shylee 	/* parse token2 */
2430431deaa0Shylee 	if (strncmp(token2, CONF_MODULEPATH, strlen(CONF_MODULEPATH)) != 0) {
2431431deaa0Shylee 		ret = KMF_ERR_KMF_CONF;
2432431deaa0Shylee 		goto end;
2433431deaa0Shylee 	}
2434431deaa0Shylee 
2435431deaa0Shylee 	if (value = strpbrk(token2, SEP_EQUAL)) {
2436431deaa0Shylee 		value++; /* get rid of = */
2437431deaa0Shylee 	} else {
2438431deaa0Shylee 		ret = KMF_ERR_KMF_CONF;
2439431deaa0Shylee 		goto end;
2440431deaa0Shylee 	}
2441431deaa0Shylee 
2442431deaa0Shylee 	if ((tmp->modulepath = strdup(value)) == NULL) {
2443431deaa0Shylee 		ret = KMF_ERR_MEMORY;
2444431deaa0Shylee 		goto end;
2445431deaa0Shylee 	}
2446431deaa0Shylee 
2447431deaa0Shylee 	/* parse token3, if it exists */
2448431deaa0Shylee 	if (token3 != NULL) {
2449431deaa0Shylee 		if (strncmp(token3, CONF_OPTION, strlen(CONF_OPTION))
2450431deaa0Shylee 		    != 0) {
2451431deaa0Shylee 			ret = KMF_ERR_KMF_CONF;
2452431deaa0Shylee 			goto end;
2453431deaa0Shylee 		}
2454431deaa0Shylee 
2455431deaa0Shylee 		if (value = strpbrk(token3, SEP_EQUAL)) {
2456431deaa0Shylee 			value++; /* get rid of = */
2457431deaa0Shylee 		} else {
2458431deaa0Shylee 			ret = KMF_ERR_KMF_CONF;
2459431deaa0Shylee 			goto end;
2460431deaa0Shylee 		}
2461431deaa0Shylee 
2462431deaa0Shylee 		if ((tmp->option = strdup(value)) == NULL) {
2463431deaa0Shylee 			ret = KMF_ERR_MEMORY;
2464431deaa0Shylee 			goto end;
2465431deaa0Shylee 		}
2466431deaa0Shylee 	}
2467431deaa0Shylee 
2468431deaa0Shylee 	*entry = tmp;
2469431deaa0Shylee 
2470431deaa0Shylee end:
2471431deaa0Shylee 	if (ret != KMF_OK) {
2472431deaa0Shylee 		free_entry(tmp);
2473431deaa0Shylee 		free(tmp);
2474431deaa0Shylee 	}
2475431deaa0Shylee 	return (ret);
2476431deaa0Shylee }
2477431deaa0Shylee 
2478431deaa0Shylee 
2479431deaa0Shylee conf_entry_t *
2480431deaa0Shylee dup_entry(conf_entry_t *entry)
2481431deaa0Shylee {
2482431deaa0Shylee 	conf_entry_t *rtn_entry;
2483431deaa0Shylee 
2484431deaa0Shylee 	if (entry == NULL)
2485431deaa0Shylee 		return (NULL);
2486431deaa0Shylee 
2487431deaa0Shylee 	rtn_entry = malloc(sizeof (conf_entry_t));
2488431deaa0Shylee 	if (rtn_entry == NULL)
2489431deaa0Shylee 		return (NULL);
2490431deaa0Shylee 
2491431deaa0Shylee 	if ((rtn_entry->keystore = strdup(entry->keystore)) == NULL)
2492431deaa0Shylee 		goto out;
2493431deaa0Shylee 
2494431deaa0Shylee 	if ((rtn_entry->modulepath = strdup(entry->modulepath)) == NULL)
2495431deaa0Shylee 		goto out;
2496431deaa0Shylee 
2497431deaa0Shylee 	if (entry->option != NULL &&
2498431deaa0Shylee 	    (rtn_entry->option = strdup(entry->modulepath)) == NULL)
2499431deaa0Shylee 		goto out;
2500431deaa0Shylee 
2501431deaa0Shylee 	return (rtn_entry);
2502431deaa0Shylee 
2503431deaa0Shylee out:
2504431deaa0Shylee 	free_entry(rtn_entry);
2505431deaa0Shylee 	return (NULL);
2506431deaa0Shylee }
2507431deaa0Shylee 
2508431deaa0Shylee 
2509431deaa0Shylee /*
2510431deaa0Shylee  * This function takes a keystore_name as input and returns
2511431deaa0Shylee  * the KMF_KEYSTORE_TYPE value assigned to it.  If the "option"
2512431deaa0Shylee  * argument is not NULL, this function also returns the option string
2513431deaa0Shylee  * if there is an option string for the plugin module.
2514431deaa0Shylee  */
2515431deaa0Shylee KMF_RETURN
2516431deaa0Shylee kmf_get_plugin_info(KMF_HANDLE_T handle, char *keystore_name,
2517431deaa0Shylee     KMF_KEYSTORE_TYPE *kstype, char **option)
2518431deaa0Shylee {
2519431deaa0Shylee 	KMF_RETURN ret = KMF_OK;
2520431deaa0Shylee 	conf_entrylist_t  *phead = extra_plugin_list;
2521431deaa0Shylee 	boolean_t is_default = B_TRUE;
2522431deaa0Shylee 
2523431deaa0Shylee 	/*
2524431deaa0Shylee 	 * Although handle is not really used in the function, we will
2525431deaa0Shylee 	 * check the handle to make sure that kmf_intialize() is called
2526431deaa0Shylee 	 * before this function.
2527431deaa0Shylee 	 */
2528431deaa0Shylee 	if (handle == NULL || keystore_name == NULL || kstype == NULL)
2529431deaa0Shylee 		return (KMF_ERR_BAD_PARAMETER);
2530431deaa0Shylee 
2531431deaa0Shylee 	if (strcmp(keystore_name, "pkcs11") == 0) {
2532431deaa0Shylee 		*kstype = KMF_KEYSTORE_PK11TOKEN;
2533431deaa0Shylee 	} else if (strcmp(keystore_name, "file") == 0) {
2534431deaa0Shylee 		*kstype = KMF_KEYSTORE_OPENSSL;
2535431deaa0Shylee 	} else if (strcmp(keystore_name, "nss") == 0) {
2536431deaa0Shylee 		*kstype = KMF_KEYSTORE_NSS;
2537431deaa0Shylee 	} else {
2538431deaa0Shylee 		is_default = B_FALSE;
2539431deaa0Shylee 	}
2540431deaa0Shylee 
2541431deaa0Shylee 	if (is_default) {
2542431deaa0Shylee 		if (option != NULL)
2543431deaa0Shylee 			*option = NULL;
2544431deaa0Shylee 		goto out;
2545431deaa0Shylee 	}
2546431deaa0Shylee 
2547431deaa0Shylee 	/* Not a built-in plugin; check if it is in extra_plugin_list. */
2548431deaa0Shylee 	while (phead != NULL) {
2549431deaa0Shylee 		if (strcmp(phead->entry->keystore, keystore_name) == 0)
2550431deaa0Shylee 			break;
2551431deaa0Shylee 		phead = phead->next;
2552431deaa0Shylee 	}
2553431deaa0Shylee 
2554431deaa0Shylee 	if (phead == NULL) {
2555431deaa0Shylee 		ret = KMF_ERR_PLUGIN_NOTFOUND;
2556431deaa0Shylee 		goto out;
2557431deaa0Shylee 	}
2558431deaa0Shylee 
2559431deaa0Shylee 	/* found it */
2560431deaa0Shylee 	*kstype = phead->entry->kstype;
2561431deaa0Shylee 	if (option != NULL) {
2562431deaa0Shylee 		if (phead->entry->option == NULL)
2563431deaa0Shylee 			*option = NULL;
2564431deaa0Shylee 		else {
2565431deaa0Shylee 			*option = strdup(phead->entry->option);
2566431deaa0Shylee 			if (*option == NULL) {
2567431deaa0Shylee 				ret = KMF_ERR_MEMORY;
2568431deaa0Shylee 				goto out;
2569431deaa0Shylee 			}
2570431deaa0Shylee 		}
2571431deaa0Shylee 	}
2572431deaa0Shylee 
2573431deaa0Shylee out:
2574431deaa0Shylee 	return (ret);
2575431deaa0Shylee }
2576431deaa0Shylee 
2577431deaa0Shylee /*
2578431deaa0Shylee  * Retrieve the non-default plugin list from the kmf.conf file.
2579431deaa0Shylee  */
2580431deaa0Shylee KMF_RETURN
2581431deaa0Shylee get_entrylist(conf_entrylist_t **entlist)
2582431deaa0Shylee {
2583431deaa0Shylee 	KMF_RETURN rv = KMF_OK;
2584431deaa0Shylee 	FILE *pfile;
2585431deaa0Shylee 	conf_entry_t *entry;
2586431deaa0Shylee 	conf_entrylist_t *rtnlist = NULL;
2587431deaa0Shylee 	conf_entrylist_t *ptmp;
2588431deaa0Shylee 	conf_entrylist_t *pcur;
2589431deaa0Shylee 	char buffer[MAXPATHLEN];
2590431deaa0Shylee 	size_t len;
2591431deaa0Shylee 
2592431deaa0Shylee 	if ((pfile = fopen(_PATH_KMF_CONF, "rF")) == NULL) {
2593431deaa0Shylee 		cryptoerror(LOG_ERR, "failed to open %s.\n", _PATH_KMF_CONF);
2594431deaa0Shylee 		return (KMF_ERR_KMF_CONF);
2595431deaa0Shylee 	}
2596431deaa0Shylee 
2597431deaa0Shylee 	while (fgets(buffer, MAXPATHLEN, pfile) != NULL) {
2598431deaa0Shylee 		if (buffer[0] == '#' || buffer[0] == ' ' ||
2599431deaa0Shylee 		    buffer[0] == '\n'|| buffer[0] == '\t') {
2600431deaa0Shylee 			continue;   /* ignore comment lines */
2601431deaa0Shylee 		}
2602431deaa0Shylee 
2603431deaa0Shylee 		len = strlen(buffer);
2604431deaa0Shylee 		if (buffer[len-1] == '\n') { /* get rid of trailing '\n' */
2605431deaa0Shylee 			len--;
2606431deaa0Shylee 		}
2607431deaa0Shylee 		buffer[len] = '\0';
2608431deaa0Shylee 
2609431deaa0Shylee 		rv = parse_entry(buffer, &entry);
2610431deaa0Shylee 		if (rv != KMF_OK) {
2611431deaa0Shylee 			goto end;
2612431deaa0Shylee 		}
2613431deaa0Shylee 
2614431deaa0Shylee 		if ((ptmp = malloc(sizeof (conf_entrylist_t))) == NULL) {
2615431deaa0Shylee 			rv = KMF_ERR_MEMORY;
2616431deaa0Shylee 			goto end;
2617431deaa0Shylee 		}
2618431deaa0Shylee 		ptmp->entry = entry;
2619431deaa0Shylee 		ptmp->next = NULL;
2620431deaa0Shylee 
2621431deaa0Shylee 		if (rtnlist == NULL) {
2622431deaa0Shylee 			rtnlist = pcur = ptmp;
2623431deaa0Shylee 		} else {
2624431deaa0Shylee 			pcur->next = ptmp;
2625431deaa0Shylee 			pcur = ptmp;
2626431deaa0Shylee 		}
2627431deaa0Shylee 	}
2628431deaa0Shylee 
2629431deaa0Shylee end:
2630431deaa0Shylee 	(void) fclose(pfile);
2631431deaa0Shylee 
2632431deaa0Shylee 	if (rv == KMF_OK) {
2633431deaa0Shylee 		*entlist = rtnlist;
2634431deaa0Shylee 	} else if (rtnlist != NULL) {
2635431deaa0Shylee 		free_entrylist(rtnlist);
2636431deaa0Shylee 		*entlist = NULL;
2637431deaa0Shylee 		kstore_num = DEFAULT_KEYSTORE_NUM;
2638431deaa0Shylee 	}
2639431deaa0Shylee 
2640431deaa0Shylee 	return (rv);
2641431deaa0Shylee }
2642431deaa0Shylee 
2643431deaa0Shylee 
2644431deaa0Shylee boolean_t
2645431deaa0Shylee is_valid_keystore_type(KMF_KEYSTORE_TYPE kstype)
2646431deaa0Shylee {
2647431deaa0Shylee 
2648431deaa0Shylee 	if (kstype > 0 && kstype <= kstore_num)
2649431deaa0Shylee 		return (B_TRUE);
2650431deaa0Shylee 	else
2651431deaa0Shylee 		return (B_FALSE);
2652431deaa0Shylee }
2653431deaa0Shylee 
2654431deaa0Shylee 
265530a5e8faSwyllys /*
265630a5e8faSwyllys  * This API is used by elfsign. We must keep it in old API form.
265730a5e8faSwyllys  */
265830a5e8faSwyllys KMF_RETURN
265930a5e8faSwyllys KMF_ConfigureKeystore(KMF_HANDLE_T handle, KMF_CONFIG_PARAMS *params)
266030a5e8faSwyllys {
266130a5e8faSwyllys 
266230a5e8faSwyllys 	KMF_ATTRIBUTE attlist[32];
266330a5e8faSwyllys 	int i = 0;
266430a5e8faSwyllys 
266530a5e8faSwyllys 	if (params == NULL)
266630a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
266730a5e8faSwyllys 
266830a5e8faSwyllys 	kmf_set_attr_at_index(attlist, i,
266930a5e8faSwyllys 	    KMF_KEYSTORE_TYPE_ATTR, &params->kstype, sizeof (params->kstype));
267030a5e8faSwyllys 	i++;
267130a5e8faSwyllys 
267230a5e8faSwyllys 	if (params->kstype == KMF_KEYSTORE_NSS) {
267330a5e8faSwyllys 		if (params->nssconfig.configdir != NULL) {
267430a5e8faSwyllys 			kmf_set_attr_at_index(attlist, i,
267530a5e8faSwyllys 			    KMF_DIRPATH_ATTR,
267630a5e8faSwyllys 			    params->nssconfig.configdir,
267730a5e8faSwyllys 			    strlen(params->nssconfig.configdir));
267830a5e8faSwyllys 			i++;
267930a5e8faSwyllys 		}
268030a5e8faSwyllys 		if (params->nssconfig.certPrefix != NULL) {
268130a5e8faSwyllys 			kmf_set_attr_at_index(attlist, i,
268230a5e8faSwyllys 			    KMF_CERTPREFIX_ATTR,
268330a5e8faSwyllys 			    params->nssconfig.certPrefix,
268430a5e8faSwyllys 			    strlen(params->nssconfig.certPrefix));
268530a5e8faSwyllys 			i++;
268630a5e8faSwyllys 		}
268730a5e8faSwyllys 		if (params->nssconfig.keyPrefix != NULL) {
268830a5e8faSwyllys 			kmf_set_attr_at_index(attlist, i,
268930a5e8faSwyllys 			    KMF_KEYPREFIX_ATTR,
269030a5e8faSwyllys 			    params->nssconfig.keyPrefix,
269130a5e8faSwyllys 			    strlen(params->nssconfig.keyPrefix));
269230a5e8faSwyllys 			i++;
269330a5e8faSwyllys 		}
269430a5e8faSwyllys 		if (params->nssconfig.secModName != NULL) {
269530a5e8faSwyllys 			kmf_set_attr_at_index(attlist, i,
269630a5e8faSwyllys 			    KMF_SECMODNAME_ATTR,
269730a5e8faSwyllys 			    params->nssconfig.secModName,
269830a5e8faSwyllys 			    strlen(params->nssconfig.secModName));
269930a5e8faSwyllys 			i++;
270030a5e8faSwyllys 		}
270130a5e8faSwyllys 	} else if (params->kstype == KMF_KEYSTORE_PK11TOKEN) {
270230a5e8faSwyllys 		if (params->pkcs11config.label != NULL) {
270330a5e8faSwyllys 			kmf_set_attr_at_index(attlist, i,
270430a5e8faSwyllys 			    KMF_TOKEN_LABEL_ATTR,
270530a5e8faSwyllys 			    params->pkcs11config.label,
270630a5e8faSwyllys 			    strlen(params->pkcs11config.label));
270730a5e8faSwyllys 			i++;
270830a5e8faSwyllys 		}
270930a5e8faSwyllys 		kmf_set_attr_at_index(attlist, i,
271030a5e8faSwyllys 		    KMF_READONLY_ATTR,
271130a5e8faSwyllys 		    &params->pkcs11config.readonly,
271230a5e8faSwyllys 		    sizeof (params->pkcs11config.readonly));
271330a5e8faSwyllys 		i++;
271430a5e8faSwyllys 	}
271530a5e8faSwyllys 
271630a5e8faSwyllys 	return (kmf_configure_keystore(handle, i, attlist));
271730a5e8faSwyllys }
271830a5e8faSwyllys 
271930a5e8faSwyllys /*
272030a5e8faSwyllys  * This API is used by elfsign. We must keep it in old API form.
272130a5e8faSwyllys  */
272230a5e8faSwyllys KMF_RETURN
272330a5e8faSwyllys KMF_Initialize(KMF_HANDLE_T *outhandle, char *policyfile, char *policyname)
272430a5e8faSwyllys {
272530a5e8faSwyllys 	return (kmf_initialize(outhandle, policyfile, policyname));
272630a5e8faSwyllys }
272730a5e8faSwyllys 
272830a5e8faSwyllys /*
272930a5e8faSwyllys  * This API is used by elfsign. We must keep it in old API form.
273030a5e8faSwyllys  */
273130a5e8faSwyllys KMF_RETURN
273230a5e8faSwyllys KMF_Finalize(KMF_HANDLE_T handle)
273330a5e8faSwyllys {
273430a5e8faSwyllys 	return (kmf_finalize(handle));
273530a5e8faSwyllys }
273630a5e8faSwyllys 
273730a5e8faSwyllys /*
273830a5e8faSwyllys  * This API is used by elfsign. We must keep it in old API form.
273930a5e8faSwyllys  */
274030a5e8faSwyllys KMF_RETURN
274130a5e8faSwyllys KMF_GetKMFErrorString(KMF_RETURN errcode, char **errmsg)
274230a5e8faSwyllys {
274330a5e8faSwyllys 	return (kmf_get_kmf_error_str(errcode, errmsg));
274430a5e8faSwyllys }
274530a5e8faSwyllys 
274630a5e8faSwyllys /*
274730a5e8faSwyllys  * This API is used by elfsign. We must keep it in old API form.
274830a5e8faSwyllys  */
274930a5e8faSwyllys KMF_RETURN
275030a5e8faSwyllys KMF_ReadInputFile(KMF_HANDLE_T handle, char *filename,  KMF_DATA *pdata)
275130a5e8faSwyllys {
275230a5e8faSwyllys 	return (kmf_read_input_file(handle, filename, pdata));
275330a5e8faSwyllys }
275430a5e8faSwyllys 
275530a5e8faSwyllys 
275630a5e8faSwyllys /*
275730a5e8faSwyllys  * This API is used by elfsign. We must keep it in old API form.
275830a5e8faSwyllys  */
275930a5e8faSwyllys void
276030a5e8faSwyllys KMF_FreeKMFCert(KMF_HANDLE_T handle, KMF_X509_DER_CERT *kmf_cert)
276130a5e8faSwyllys {
276230a5e8faSwyllys 	kmf_free_kmf_cert(handle, kmf_cert);
276330a5e8faSwyllys }
276430a5e8faSwyllys 
276530a5e8faSwyllys /*
276630a5e8faSwyllys  * This API is used by elfsign. We must keep it in old API form.
276730a5e8faSwyllys  */
276830a5e8faSwyllys void
276930a5e8faSwyllys KMF_FreeData(KMF_DATA *datablock)
277030a5e8faSwyllys {
277130a5e8faSwyllys 	kmf_free_data(datablock);
277230a5e8faSwyllys }
277330a5e8faSwyllys 
277430a5e8faSwyllys /*
277530a5e8faSwyllys  * This API is used by elfsign. We must keep it in old API form.
277630a5e8faSwyllys  */
277730a5e8faSwyllys void
277830a5e8faSwyllys KMF_FreeKMFKey(KMF_HANDLE_T handle, KMF_KEY_HANDLE *key)
277930a5e8faSwyllys {
278030a5e8faSwyllys 	kmf_free_kmf_key(handle, key);
278130a5e8faSwyllys }
278230a5e8faSwyllys 
278330a5e8faSwyllys /*
278430a5e8faSwyllys  * This API is used by elfsign. We must keep it in old API form.
278530a5e8faSwyllys  */
278630a5e8faSwyllys void
278730a5e8faSwyllys KMF_FreeSignedCSR(KMF_CSR_DATA *csr)
278830a5e8faSwyllys {
278930a5e8faSwyllys 	kmf_free_signed_csr(csr);
279030a5e8faSwyllys }
2791