xref: /illumos-gate/usr/src/lib/libkmf/libkmf/common/generalop.c (revision d00756ccb34596a328f8a15d1965da5412d366d0)
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*d00756ccSwyllys  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
2399ebb4caSwyllys  * Use is subject to license terms.
2499ebb4caSwyllys  *
2599ebb4caSwyllys  */
2699ebb4caSwyllys 
2799ebb4caSwyllys #pragma ident	"%Z%%M%	%I%	%E% SMI"
2899ebb4caSwyllys 
2999ebb4caSwyllys #include <stdio.h>
3099ebb4caSwyllys #include <dlfcn.h>
3199ebb4caSwyllys #include <link.h>
3299ebb4caSwyllys #include <fcntl.h>
3399ebb4caSwyllys #include <ctype.h>
3499ebb4caSwyllys #include <sys/param.h>
3599ebb4caSwyllys #include <sys/types.h>
3699ebb4caSwyllys #include <sys/stat.h>
3799ebb4caSwyllys #include <errno.h>
3899ebb4caSwyllys #include <sys/socket.h>
3999ebb4caSwyllys #include <netinet/in.h>
4099ebb4caSwyllys #include <arpa/inet.h>
4199ebb4caSwyllys #include <thread.h>
4299ebb4caSwyllys 
4399ebb4caSwyllys #include <ber_der.h>
4499ebb4caSwyllys #include <kmfapiP.h>
4599ebb4caSwyllys 
4699ebb4caSwyllys #include <pem_encode.h>
4799ebb4caSwyllys #include <rdn_parser.h>
4899ebb4caSwyllys #include <libxml2/libxml/uri.h>
4999ebb4caSwyllys #include <libgen.h>
5099ebb4caSwyllys #include <cryptoutil.h>
5199ebb4caSwyllys 
5299ebb4caSwyllys static uchar_t pkcs11_initialized = 0;
5399ebb4caSwyllys mutex_t init_lock = DEFAULTMUTEX;
5499ebb4caSwyllys extern int errno;
5599ebb4caSwyllys 
5699ebb4caSwyllys typedef struct {
5799ebb4caSwyllys 	KMF_RETURN code;
5899ebb4caSwyllys 	char	*message;
5999ebb4caSwyllys } kmf_error_map;
6099ebb4caSwyllys 
6199ebb4caSwyllys static kmf_error_map kmf_errcodes[] = {
6299ebb4caSwyllys 	{KMF_OK,	"KMF_OK"},
6399ebb4caSwyllys 	{KMF_ERR_BAD_PARAMETER,	"KMF_ERR_BAD_PARAMETER"},
6499ebb4caSwyllys 	{KMF_ERR_BAD_KEY_FORMAT,	"KMF_ERR_BAD_KEY_FORMAT"},
6599ebb4caSwyllys 	{KMF_ERR_BAD_ALGORITHM,	"KMF_ERR_BAD_ALGORITHM"},
6699ebb4caSwyllys 	{KMF_ERR_MEMORY,	"KMF_ERR_MEMORY"},
6799ebb4caSwyllys 	{KMF_ERR_ENCODING,	"KMF_ERR_ENCODING"},
6899ebb4caSwyllys 	{KMF_ERR_PLUGIN_INIT,	"KMF_ERR_PLUGIN_INIT"},
6999ebb4caSwyllys 	{KMF_ERR_PLUGIN_NOTFOUND,	"KMF_ERR_PLUGIN_NOTFOUND"},
7099ebb4caSwyllys 	{KMF_ERR_INTERNAL,	"KMF_ERR_INTERNAL"},
7199ebb4caSwyllys 	{KMF_ERR_BAD_CERT_FORMAT,	"KMF_ERR_BAD_CERT_FORMAT"},
7299ebb4caSwyllys 	{KMF_ERR_KEYGEN_FAILED,	"KMF_ERR_KEYGEN_FAILED"},
7399ebb4caSwyllys 	{KMF_ERR_UNINITIALIZED,	"KMF_ERR_UNINITIALIZED"},
7499ebb4caSwyllys 	{KMF_ERR_ISSUER,	"KMF_ERR_ISSUER"},
7599ebb4caSwyllys 	{KMF_ERR_NOT_REVOKED,	"KMF_ERR_NOT_REVOKED"},
7699ebb4caSwyllys 	{KMF_ERR_CERT_NOT_FOUND,	"KMF_ERR_CERT_NOT_FOUND"},
7799ebb4caSwyllys 	{KMF_ERR_CRL_NOT_FOUND,	"KMF_ERR_CRL_NOT_FOUND"},
7899ebb4caSwyllys 	{KMF_ERR_RDN_PARSER,	"KMF_ERR_RDN_PARSER"},
7999ebb4caSwyllys 	{KMF_ERR_RDN_ATTR,	"KMF_ERR_RDN_ATTR"},
8099ebb4caSwyllys 	{KMF_ERR_SLOTNAME,	"KMF_ERR_SLOTNAME"},
8199ebb4caSwyllys 	{KMF_ERR_EMPTY_CRL,	"KMF_ERR_EMPTY_CRL"},
8299ebb4caSwyllys 	{KMF_ERR_BUFFER_SIZE,	"KMF_ERR_BUFFER_SIZE"},
8399ebb4caSwyllys 	{KMF_ERR_AUTH_FAILED,	"KMF_ERR_AUTH_FAILED"},
8499ebb4caSwyllys 	{KMF_ERR_TOKEN_SELECTED,	"KMF_ERR_TOKEN_SELECTED"},
8599ebb4caSwyllys 	{KMF_ERR_NO_TOKEN_SELECTED,	"KMF_ERR_NO_TOKEN_SELECTED"},
8699ebb4caSwyllys 	{KMF_ERR_TOKEN_NOT_PRESENT,	"KMF_ERR_TOKEN_NOT_PRESENT"},
8799ebb4caSwyllys 	{KMF_ERR_EXTENSION_NOT_FOUND,	"KMF_ERR_EXTENSION_NOT_FOUND"},
8899ebb4caSwyllys 	{KMF_ERR_POLICY_ENGINE,	"KMF_ERR_POLICY_ENGINE"},
8999ebb4caSwyllys 	{KMF_ERR_POLICY_DB_FORMAT,	"KMF_ERR_POLICY_DB_FORMAT"},
9099ebb4caSwyllys 	{KMF_ERR_POLICY_NOT_FOUND,	"KMF_ERR_POLICY_NOT_FOUND"},
9199ebb4caSwyllys 	{KMF_ERR_POLICY_DB_FILE,	"KMF_ERR_POLICY_DB_FILE"},
9299ebb4caSwyllys 	{KMF_ERR_POLICY_NAME,	"KMF_ERR_POLICY_NAME"},
9399ebb4caSwyllys 	{KMF_ERR_OCSP_POLICY,	"KMF_ERR_OCSP_POLICY"},
9499ebb4caSwyllys 	{KMF_ERR_TA_POLICY,	"KMF_ERR_TA_POLICY"},
9599ebb4caSwyllys 	{KMF_ERR_KEY_NOT_FOUND,	"KMF_ERR_KEY_NOT_FOUND"},
9699ebb4caSwyllys 	{KMF_ERR_OPEN_FILE,	"KMF_ERR_OPEN_FILE"},
9799ebb4caSwyllys 	{KMF_ERR_OCSP_BAD_ISSUER,	"KMF_ERR_OCSP_BAD_ISSUER"},
9899ebb4caSwyllys 	{KMF_ERR_OCSP_BAD_CERT,	"KMF_ERR_OCSP_BAD_CERT"},
9999ebb4caSwyllys 	{KMF_ERR_OCSP_CREATE_REQUEST,	"KMF_ERR_OCSP_CREATE_REQUEST"},
10099ebb4caSwyllys 	{KMF_ERR_CONNECT_SERVER,	"KMF_ERR_CONNECT_SERVER"},
10199ebb4caSwyllys 	{KMF_ERR_SEND_REQUEST,	"KMF_ERR_SEND_REQUEST"},
10299ebb4caSwyllys 	{KMF_ERR_OCSP_CERTID,	"KMF_ERR_OCSP_CERTID"},
10399ebb4caSwyllys 	{KMF_ERR_OCSP_MALFORMED_RESPONSE, "KMF_ERR_OCSP_MALFORMED_RESPONSE"},
10499ebb4caSwyllys 	{KMF_ERR_OCSP_RESPONSE_STATUS,	"KMF_ERR_OCSP_RESPONSE_STATUS"},
10599ebb4caSwyllys 	{KMF_ERR_OCSP_NO_BASIC_RESPONSE, "KMF_ERR_OCSP_NO_BASIC_RESPONSE"},
10699ebb4caSwyllys 	{KMF_ERR_OCSP_BAD_SIGNER,	"KMF_ERR_OCSP_BAD_SIGNER"},
10799ebb4caSwyllys 	{KMF_ERR_OCSP_RESPONSE_SIGNATURE, "KMF_ERR_OCSP_RESPONSE_SIGNATURE"},
10899ebb4caSwyllys 	{KMF_ERR_OCSP_UNKNOWN_CERT,	"KMF_ERR_OCSP_UNKNOWN_CERT"},
10999ebb4caSwyllys 	{KMF_ERR_OCSP_STATUS_TIME_INVALID, "KMF_ERR_OCSP_STATUS_TIME_INVALID"},
11099ebb4caSwyllys 	{KMF_ERR_BAD_HTTP_RESPONSE,	"KMF_ERR_BAD_HTTP_RESPONSE"},
11199ebb4caSwyllys 	{KMF_ERR_RECV_RESPONSE,	"KMF_ERR_RECV_RESPONSE"},
11299ebb4caSwyllys 	{KMF_ERR_RECV_TIMEOUT,	"KMF_ERR_RECV_TIMEOUT"},
11399ebb4caSwyllys 	{KMF_ERR_DUPLICATE_KEYFILE,	"KMF_ERR_DUPLICATE_KEYFILE"},
11499ebb4caSwyllys 	{KMF_ERR_AMBIGUOUS_PATHNAME,	"KMF_ERR_AMBIGUOUS_PATHNAME"},
11599ebb4caSwyllys 	{KMF_ERR_FUNCTION_NOT_FOUND,	"KMF_ERR_FUNCTION_NOT_FOUND"},
11699ebb4caSwyllys 	{KMF_ERR_PKCS12_FORMAT,	"KMF_ERR_PKCS12_FORMAT"},
11799ebb4caSwyllys 	{KMF_ERR_BAD_KEY_TYPE,	"KMF_ERR_BAD_KEY_TYPE"},
11899ebb4caSwyllys 	{KMF_ERR_BAD_KEY_CLASS,	"KMF_ERR_BAD_KEY_CLASS"},
11999ebb4caSwyllys 	{KMF_ERR_BAD_KEY_SIZE,	"KMF_ERR_BAD_KEY_SIZE"},
12099ebb4caSwyllys 	{KMF_ERR_BAD_HEX_STRING,	"KMF_ERR_BAD_HEX_STRING"},
12199ebb4caSwyllys 	{KMF_ERR_KEYUSAGE,	"KMF_ERR_KEYUSAGE"},
12299ebb4caSwyllys 	{KMF_ERR_VALIDITY_PERIOD,	"KMF_ERR_VALIDITY_PERIOD"},
12399ebb4caSwyllys 	{KMF_ERR_OCSP_REVOKED,	"KMF_ERR_OCSP_REVOKED"},
12499ebb4caSwyllys 	{KMF_ERR_CERT_MULTIPLE_FOUND,	"KMF_ERR_CERT_MULTIPLE_FOUND"},
12599ebb4caSwyllys 	{KMF_ERR_WRITE_FILE,	"KMF_ERR_WRITE_FILE"},
12699ebb4caSwyllys 	{KMF_ERR_BAD_URI,	"KMF_ERR_BAD_URI"},
12799ebb4caSwyllys 	{KMF_ERR_BAD_CRLFILE,	"KMF_ERR_BAD_CRLFILE"},
12899ebb4caSwyllys 	{KMF_ERR_BAD_CERTFILE,	"KMF_ERR_BAD_CERTFILE"},
12999ebb4caSwyllys 	{KMF_ERR_GETKEYVALUE_FAILED,	"KMF_ERR_GETKEYVALUE_FAILED"},
13099ebb4caSwyllys 	{KMF_ERR_BAD_KEYHANDLE,	"KMF_ERR_BAD_KEYHANDLE"},
13199ebb4caSwyllys 	{KMF_ERR_BAD_OBJECT_TYPE,	"KMF_ERR_BAD_OBJECT_TYPE"},
13299ebb4caSwyllys 	{KMF_ERR_OCSP_RESPONSE_LIFETIME, "KMF_ERR_OCSP_RESPONSE_LIFETIME"},
13399ebb4caSwyllys 	{KMF_ERR_UNKNOWN_CSR_ATTRIBUTE,	"KMF_ERR_UNKNOWN_CSR_ATTRIBUTE"},
13499ebb4caSwyllys 	{KMF_ERR_UNINITIALIZED_TOKEN,	"KMF_ERR_UNINITIALIZED_TOKEN"},
13599ebb4caSwyllys 	{KMF_ERR_INCOMPLETE_TBS_CERT,	"KMF_ERR_INCOMPLETE_TBS_CERT"},
13699ebb4caSwyllys 	{KMF_ERR_MISSING_ERRCODE,	"KMF_ERR_MISSING_ERRCODE"},
13771593db2Swyllys 	{KMF_KEYSTORE_ALREADY_INITIALIZED, "KMF_KEYSTORE_ALREADY_INITIALIZED"},
13871593db2Swyllys 	{KMF_ERR_SENSITIVE_KEY,		"KMF_ERR_SENSITIVE_KEY"},
13971593db2Swyllys 	{KMF_ERR_UNEXTRACTABLE_KEY,	"KMF_ERR_UNEXTRACTABLE_KEY"},
140*d00756ccSwyllys 	{KMF_ERR_KEY_MISMATCH,		"KMF_ERR_KEY_MISMATCH"},
141*d00756ccSwyllys 	{KMF_ERR_ATTR_NOT_FOUND,	"KMF_ERR_ATTR_NOT_FOUND"},
142*d00756ccSwyllys 	{KMF_ERR_KMF_CONF,		"KMF_ERR_KMF_CONF"}
14399ebb4caSwyllys };
14499ebb4caSwyllys 
14585b65b39Swyllys typedef struct {
14685b65b39Swyllys 	KMF_KEYSTORE_TYPE	kstype;
14785b65b39Swyllys 	char			*path;
14885b65b39Swyllys 	boolean_t		critical;
14985b65b39Swyllys } KMF_PLUGIN_ITEM;
15085b65b39Swyllys 
15185b65b39Swyllys KMF_PLUGIN_ITEM plugin_list[] = {
15285b65b39Swyllys 	{KMF_KEYSTORE_OPENSSL,	KMF_PLUGIN_PATH "kmf_openssl.so.1",  TRUE},
15385b65b39Swyllys 	{KMF_KEYSTORE_PK11TOKEN, KMF_PLUGIN_PATH "kmf_pkcs11.so.1",  TRUE},
15485b65b39Swyllys 	{KMF_KEYSTORE_NSS,	KMF_PLUGIN_PATH "kmf_nss.so.1",  FALSE}
15585b65b39Swyllys };
15699ebb4caSwyllys 
15730a5e8faSwyllys 
15830a5e8faSwyllys 
15990c85bf8Swyllys static KMF_RETURN InitializePlugin(KMF_KEYSTORE_TYPE, char *, KMF_PLUGIN **);
16090c85bf8Swyllys static KMF_RETURN AddPlugin(KMF_HANDLE_T, KMF_PLUGIN *);
16199ebb4caSwyllys static void free_extensions(KMF_X509_EXTENSIONS *extns);
16290c85bf8Swyllys static void DestroyPlugin(KMF_PLUGIN *);
16399ebb4caSwyllys 
164431deaa0Shylee #if defined(__sparcv9)
165431deaa0Shylee #define	ISA_PATH	"/sparcv9"
166431deaa0Shylee #elif defined(__sparc)
167431deaa0Shylee #define	ISA_PATH	"/"
168431deaa0Shylee #elif defined(__i386)
169431deaa0Shylee #define	ISA_PATH	"/"
170431deaa0Shylee #elif defined(__amd64)
171431deaa0Shylee #define	ISA_PATH	"/amd64"
172431deaa0Shylee #endif
173431deaa0Shylee 
174431deaa0Shylee #define	DEFAULT_KEYSTORE_NUM	3
175431deaa0Shylee static int kstore_num = DEFAULT_KEYSTORE_NUM;
176431deaa0Shylee conf_entrylist_t *extra_plugin_list = NULL;
177431deaa0Shylee static boolean_t check_extra_plugin = B_FALSE;
178431deaa0Shylee mutex_t extra_plugin_lock = DEFAULTMUTEX;
179431deaa0Shylee 
1809b37d296Swyllys KMF_RETURN
1819b37d296Swyllys init_pk11()
18299ebb4caSwyllys {
1839b37d296Swyllys 	(void) mutex_lock(&init_lock);
1849b37d296Swyllys 	if (!pkcs11_initialized) {
1859b37d296Swyllys 		CK_RV rv = C_Initialize(NULL);
1869b37d296Swyllys 		if ((rv != CKR_OK) &&
1879b37d296Swyllys 		    (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED)) {
1889b37d296Swyllys 			(void) mutex_unlock(&init_lock);
1899b37d296Swyllys 			return (KMF_ERR_UNINITIALIZED);
1909b37d296Swyllys 		} else {
1919b37d296Swyllys 			pkcs11_initialized = 1;
1929b37d296Swyllys 		}
1939b37d296Swyllys 	}
1949b37d296Swyllys 	(void) mutex_unlock(&init_lock);
1959b37d296Swyllys 	return (KMF_OK);
19699ebb4caSwyllys }
19799ebb4caSwyllys 
19899ebb4caSwyllys /*
19999ebb4caSwyllys  * Private method for searching the plugin list for the correct
20099ebb4caSwyllys  * Plugin to use.
20199ebb4caSwyllys  */
20299ebb4caSwyllys KMF_PLUGIN *
20399ebb4caSwyllys FindPlugin(KMF_HANDLE_T handle, KMF_KEYSTORE_TYPE kstype)
20499ebb4caSwyllys {
20599ebb4caSwyllys 	KMF_PLUGIN_LIST *node;
20690c85bf8Swyllys 	KMF_RETURN ret = KMF_OK;
207431deaa0Shylee 	KMF_PLUGIN *pluginrec = NULL;
20899ebb4caSwyllys 
20999ebb4caSwyllys 	if (handle == NULL)
21099ebb4caSwyllys 		return (NULL);
21199ebb4caSwyllys 
21299ebb4caSwyllys 	node = handle->plugins;
21399ebb4caSwyllys 
21490c85bf8Swyllys 	/* See if the desired plugin was already initialized. */
21599ebb4caSwyllys 	while (node != NULL && node->plugin->type != kstype)
21699ebb4caSwyllys 		node = node->next;
21799ebb4caSwyllys 
218431deaa0Shylee 	if (node != NULL)
219431deaa0Shylee 		return (node->plugin);
220431deaa0Shylee 
221431deaa0Shylee 	/* The plugin was not found, try to initialize it here. */
222431deaa0Shylee 	if (VALID_DEFAULT_KEYSTORE_TYPE(kstype)) {
22390c85bf8Swyllys 		int i;
22490c85bf8Swyllys 		int numitems = sizeof (plugin_list)/sizeof (KMF_PLUGIN_ITEM);
22590c85bf8Swyllys 		for (i = 0; i < numitems; i++) {
22690c85bf8Swyllys 			if (plugin_list[i].kstype == kstype) {
22790c85bf8Swyllys 				ret = InitializePlugin(plugin_list[i].kstype,
22890c85bf8Swyllys 				    plugin_list[i].path, &pluginrec);
22990c85bf8Swyllys 				break;
23090c85bf8Swyllys 			}
23190c85bf8Swyllys 		}
23290c85bf8Swyllys 
233431deaa0Shylee 		goto out;
234431deaa0Shylee 
235431deaa0Shylee 	} else {
236431deaa0Shylee 		/*
237431deaa0Shylee 		 * Not a built-in plugin. Check if it is in the
238431deaa0Shylee 		 * extra_plugin_list.  If it is, try to initialize it here.
239431deaa0Shylee 		 */
240431deaa0Shylee 		conf_entrylist_t *phead = extra_plugin_list;
241431deaa0Shylee 		char realpath[MAXPATHLEN];
242431deaa0Shylee 
243431deaa0Shylee 		while (phead != NULL) {
244431deaa0Shylee 			if (phead->entry->kstype == kstype)
245431deaa0Shylee 				break;
246431deaa0Shylee 			else
247431deaa0Shylee 				phead = phead->next;
248431deaa0Shylee 		}
249431deaa0Shylee 
250431deaa0Shylee 		if (phead == NULL)
25190c85bf8Swyllys 			return (NULL);
25290c85bf8Swyllys 
253431deaa0Shylee 		/*
254431deaa0Shylee 		 * Get the absolute path of the module.
255431deaa0Shylee 		 * - If modulepath is not a full path, then prepend it
256431deaa0Shylee 		 *   with KMF_PLUGIN_PATH.
257431deaa0Shylee 		 * - If modulepath is a full path and contain $ISA, then
258431deaa0Shylee 		 *   subsitute the architecture dependent path.
259431deaa0Shylee 		 */
260431deaa0Shylee 		(void) memset(realpath, 0, sizeof (realpath));
261431deaa0Shylee 		if (strncmp(phead->entry->modulepath, "/", 1) != 0) {
262431deaa0Shylee 			(void) snprintf(realpath, MAXPATHLEN, "%s%s",
263431deaa0Shylee 			    KMF_PLUGIN_PATH, phead->entry->modulepath);
264431deaa0Shylee 		} else {
265431deaa0Shylee 			char *buf = phead->entry->modulepath;
266431deaa0Shylee 			char *isa;
267431deaa0Shylee 
268431deaa0Shylee 			if ((isa = strstr(buf, PKCS11_ISA)) != NULL) {
269431deaa0Shylee 				char *isa_str;
270431deaa0Shylee 
271431deaa0Shylee 				(void) strncpy(realpath, buf, isa - buf);
272431deaa0Shylee 				isa_str = strdup(ISA_PATH);
273431deaa0Shylee 				if (isa_str == NULL) /* not enough memory */
274431deaa0Shylee 					return (NULL);
275431deaa0Shylee 
276431deaa0Shylee 				(void) strncat(realpath, isa_str,
277431deaa0Shylee 				    strlen(isa_str));
278431deaa0Shylee 				free(isa_str);
279431deaa0Shylee 
280431deaa0Shylee 				isa += strlen(PKCS11_ISA);
281431deaa0Shylee 				(void) strlcat(realpath, isa, MAXPATHLEN);
282431deaa0Shylee 			} else {
283431deaa0Shylee 				(void) snprintf(realpath, MAXPATHLEN, "%s",
284431deaa0Shylee 				    phead->entry->modulepath);
285431deaa0Shylee 			}
28690c85bf8Swyllys 		}
287431deaa0Shylee 
288431deaa0Shylee 		ret = InitializePlugin(phead->entry->kstype, realpath,
289431deaa0Shylee 		    &pluginrec);
290431deaa0Shylee 		goto out;
291431deaa0Shylee 	}
292431deaa0Shylee 
293431deaa0Shylee out:
294431deaa0Shylee 	if (ret != KMF_OK || pluginrec == NULL)
295431deaa0Shylee 		/* No matching plugins found in the built-in list */
296431deaa0Shylee 		return (NULL);
297431deaa0Shylee 
298431deaa0Shylee 	ret = AddPlugin(handle, pluginrec);
299431deaa0Shylee 	if (ret != KMF_OK) {
300431deaa0Shylee 		DestroyPlugin(pluginrec);
301431deaa0Shylee 		pluginrec = NULL;
30290c85bf8Swyllys 	}
303431deaa0Shylee 	return (pluginrec);
30499ebb4caSwyllys }
30599ebb4caSwyllys 
306431deaa0Shylee 
30799ebb4caSwyllys static KMF_RETURN
30899ebb4caSwyllys InitializePlugin(KMF_KEYSTORE_TYPE kstype, char *path, KMF_PLUGIN **plugin)
30999ebb4caSwyllys {
31099ebb4caSwyllys 	KMF_PLUGIN *p = NULL;
31199ebb4caSwyllys 	KMF_PLUGIN_FUNCLIST *(*sym)();
31299ebb4caSwyllys 
31399ebb4caSwyllys 	if (path == NULL || plugin == NULL)
31499ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
31599ebb4caSwyllys 
31699ebb4caSwyllys 	*plugin = NULL;
31799ebb4caSwyllys 
31899ebb4caSwyllys 	p = (KMF_PLUGIN *)malloc(sizeof (KMF_PLUGIN));
31999ebb4caSwyllys 	if (p == NULL)
32099ebb4caSwyllys 		return (KMF_ERR_MEMORY);
32199ebb4caSwyllys 
32299ebb4caSwyllys 	p->type = kstype;
32399ebb4caSwyllys 	p->path = strdup(path);
32499ebb4caSwyllys 	if (p->path == NULL) {
32599ebb4caSwyllys 		free(p);
32699ebb4caSwyllys 		return (KMF_ERR_MEMORY);
32799ebb4caSwyllys 	}
32890c85bf8Swyllys 	p->dldesc = dlopen(path, RTLD_LAZY | RTLD_GROUP | RTLD_PARENT);
32999ebb4caSwyllys 	if (p->dldesc == NULL) {
33099ebb4caSwyllys 		free(p->path);
33199ebb4caSwyllys 		free(p);
33299ebb4caSwyllys 		return (KMF_ERR_PLUGIN_INIT);
33399ebb4caSwyllys 	}
33499ebb4caSwyllys 
33599ebb4caSwyllys 	sym = (KMF_PLUGIN_FUNCLIST *(*)())dlsym(p->dldesc,
33630a5e8faSwyllys 	    KMF_PLUGIN_INIT_SYMBOL);
33799ebb4caSwyllys 	if (sym == NULL) {
33899ebb4caSwyllys 		(void) dlclose(p->dldesc);
33999ebb4caSwyllys 		free(p->path);
34099ebb4caSwyllys 		free(p);
34199ebb4caSwyllys 		return (KMF_ERR_PLUGIN_INIT);
34299ebb4caSwyllys 	}
34399ebb4caSwyllys 
34499ebb4caSwyllys 	/* Get the function list */
34599ebb4caSwyllys 	if ((p->funclist = (*sym)()) == NULL) {
34699ebb4caSwyllys 		(void) dlclose(p->dldesc);
34799ebb4caSwyllys 		free(p->path);
34899ebb4caSwyllys 		free(p);
34999ebb4caSwyllys 		return (KMF_ERR_PLUGIN_INIT);
35099ebb4caSwyllys 	}
35199ebb4caSwyllys 
35299ebb4caSwyllys 	*plugin = p;
35399ebb4caSwyllys 
35499ebb4caSwyllys 	return (KMF_OK);
35599ebb4caSwyllys }
35699ebb4caSwyllys 
35799ebb4caSwyllys static KMF_RETURN
35899ebb4caSwyllys AddPlugin(KMF_HANDLE_T handle, KMF_PLUGIN *plugin)
35999ebb4caSwyllys {
36099ebb4caSwyllys 	KMF_PLUGIN_LIST *n;
36199ebb4caSwyllys 
36299ebb4caSwyllys 	if (handle == NULL || plugin == NULL)
36399ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
36499ebb4caSwyllys 
36599ebb4caSwyllys 	/* If the head is NULL, create it */
36699ebb4caSwyllys 	if (handle->plugins == NULL) {
36799ebb4caSwyllys 		handle->plugins = (KMF_PLUGIN_LIST *)malloc(
36830a5e8faSwyllys 		    sizeof (KMF_PLUGIN_LIST));
36999ebb4caSwyllys 		if (handle->plugins == NULL)
37099ebb4caSwyllys 			return (KMF_ERR_MEMORY);
37199ebb4caSwyllys 		handle->plugins->plugin = plugin;
37299ebb4caSwyllys 		handle->plugins->next = NULL;
37399ebb4caSwyllys 	} else {
37499ebb4caSwyllys 		/* walk the list to find the tail */
37599ebb4caSwyllys 		n = handle->plugins;
37699ebb4caSwyllys 		while (n->next != NULL)
37799ebb4caSwyllys 			n = n->next;
37899ebb4caSwyllys 		n->next = (KMF_PLUGIN_LIST *)malloc(sizeof (KMF_PLUGIN_LIST));
37999ebb4caSwyllys 		if (n->next == NULL)
38099ebb4caSwyllys 			return (KMF_ERR_MEMORY);
38199ebb4caSwyllys 
38299ebb4caSwyllys 		n->next->plugin = plugin;
38399ebb4caSwyllys 		n->next->next = NULL;
38499ebb4caSwyllys 	}
38599ebb4caSwyllys 	return (0);
38699ebb4caSwyllys }
38799ebb4caSwyllys 
38899ebb4caSwyllys static void
38999ebb4caSwyllys DestroyPlugin(KMF_PLUGIN *plugin)
39099ebb4caSwyllys {
39199ebb4caSwyllys 	if (plugin) {
39299ebb4caSwyllys 		if (plugin->path)
39399ebb4caSwyllys 			free(plugin->path);
39499ebb4caSwyllys 		free(plugin);
39599ebb4caSwyllys 	}
39699ebb4caSwyllys }
39799ebb4caSwyllys 
39899ebb4caSwyllys static void
39999ebb4caSwyllys Cleanup_KMF_Handle(KMF_HANDLE_T handle)
40099ebb4caSwyllys {
40199ebb4caSwyllys 	if (handle != NULL) {
40299ebb4caSwyllys 		while (handle->plugins != NULL) {
40399ebb4caSwyllys 			KMF_PLUGIN_LIST *next = handle->plugins->next;
40499ebb4caSwyllys 
40599ebb4caSwyllys 			DestroyPlugin(handle->plugins->plugin);
40699ebb4caSwyllys 			free(handle->plugins);
40799ebb4caSwyllys 			handle->plugins = next;
40899ebb4caSwyllys 		}
40930a5e8faSwyllys 		kmf_free_policy_record(handle->policy);
41099ebb4caSwyllys 		free(handle->policy);
41199ebb4caSwyllys 	}
41299ebb4caSwyllys 	free(handle);
41399ebb4caSwyllys }
41499ebb4caSwyllys 
41599ebb4caSwyllys void
41699ebb4caSwyllys Cleanup_PK11_Session(KMF_HANDLE_T handle)
41799ebb4caSwyllys {
41899ebb4caSwyllys 	if (handle != NULL) {
41999ebb4caSwyllys 		/* Close active session on a pkcs11 token */
42099ebb4caSwyllys 		if (handle->pk11handle != NULL) {
42199ebb4caSwyllys 			(void) C_CloseSession(handle->pk11handle);
42299ebb4caSwyllys 			handle->pk11handle = NULL;
42399ebb4caSwyllys 		}
42499ebb4caSwyllys 	}
42599ebb4caSwyllys }
42699ebb4caSwyllys 
42799ebb4caSwyllys KMF_RETURN
42830a5e8faSwyllys kmf_initialize(KMF_HANDLE_T *outhandle, char *policyfile, char *policyname)
42999ebb4caSwyllys {
43099ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
43199ebb4caSwyllys 	KMF_HANDLE *handle = NULL;
43299ebb4caSwyllys 
43399ebb4caSwyllys 	if (outhandle == NULL)
43499ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
43599ebb4caSwyllys 
43699ebb4caSwyllys 	*outhandle = NULL;
43799ebb4caSwyllys 	handle = (KMF_HANDLE *)malloc(sizeof (KMF_HANDLE));
43899ebb4caSwyllys 	if (handle == NULL)
43999ebb4caSwyllys 		return (KMF_ERR_MEMORY);
44099ebb4caSwyllys 
44199ebb4caSwyllys 	(void) memset(handle, 0, sizeof (KMF_HANDLE));
44299ebb4caSwyllys 	handle->plugins = NULL;
44399ebb4caSwyllys 
444431deaa0Shylee 	/*
445431deaa0Shylee 	 * When this function is called the first time, get the additional
446431deaa0Shylee 	 * plugins from the config file.
447431deaa0Shylee 	 */
448431deaa0Shylee 	(void) mutex_lock(&extra_plugin_lock);
449431deaa0Shylee 	if (!check_extra_plugin) {
450431deaa0Shylee 
451431deaa0Shylee 		ret = get_entrylist(&extra_plugin_list);
452431deaa0Shylee 		check_extra_plugin = B_TRUE;
453431deaa0Shylee 
454431deaa0Shylee 		/*
455431deaa0Shylee 		 * Assign the kstype number to the additional plugins here.
456431deaa0Shylee 		 * The global kstore_num will be protected by the mutex lock.
457431deaa0Shylee 		 */
458431deaa0Shylee 		if (ret == KMF_OK) {
459431deaa0Shylee 			conf_entrylist_t *phead = extra_plugin_list;
460431deaa0Shylee 			while (phead != NULL) {
461431deaa0Shylee 				phead->entry->kstype = ++kstore_num;
462431deaa0Shylee 				phead = phead->next;
463431deaa0Shylee 			}
464431deaa0Shylee 		}
465431deaa0Shylee 
466431deaa0Shylee 		/*
467431deaa0Shylee 		 * If the KMF configuration file does not exist or cannot be
468431deaa0Shylee 		 * parsed correctly, we will give a warning in syslog and
469431deaa0Shylee 		 * continue on as there is no extra plugins in the system.
470431deaa0Shylee 		 */
471431deaa0Shylee 		if (ret == KMF_ERR_KMF_CONF) {
472431deaa0Shylee 			cryptoerror(LOG_WARNING, "KMF was unable to parse "
473431deaa0Shylee 			    "the private KMF config file.\n");
474431deaa0Shylee 			ret = KMF_OK;
475431deaa0Shylee 		}
476431deaa0Shylee 
477431deaa0Shylee 		if (ret != KMF_OK) {
478431deaa0Shylee 			(void) mutex_unlock(&extra_plugin_lock);
479431deaa0Shylee 			goto errout;
480431deaa0Shylee 		}
481431deaa0Shylee 	}
482431deaa0Shylee 	(void) mutex_unlock(&extra_plugin_lock);
483431deaa0Shylee 
48499ebb4caSwyllys 	/* Initialize the handle with the policy */
48530a5e8faSwyllys 	ret = kmf_set_policy((void *)handle,
48699ebb4caSwyllys 	    policyfile == NULL ? KMF_DEFAULT_POLICY_FILE : policyfile,
48799ebb4caSwyllys 	    policyname == NULL ? KMF_DEFAULT_POLICY_NAME : policyname);
48899ebb4caSwyllys 	if (ret != KMF_OK)
48999ebb4caSwyllys 		goto errout;
49099ebb4caSwyllys 
49199ebb4caSwyllys 	CLEAR_ERROR(handle, ret);
49299ebb4caSwyllys errout:
49399ebb4caSwyllys 	if (ret != KMF_OK) {
49499ebb4caSwyllys 		Cleanup_KMF_Handle(handle);
49599ebb4caSwyllys 		handle = NULL;
49699ebb4caSwyllys 	}
49799ebb4caSwyllys 
49899ebb4caSwyllys 	*outhandle = (KMF_HANDLE_T)handle;
49999ebb4caSwyllys 	return (ret);
50099ebb4caSwyllys }
50199ebb4caSwyllys 
50299ebb4caSwyllys KMF_RETURN
50330a5e8faSwyllys kmf_configure_keystore(KMF_HANDLE_T handle,
50430a5e8faSwyllys 	int	num_args,
50530a5e8faSwyllys 	KMF_ATTRIBUTE	*attrlist)
50699ebb4caSwyllys {
50730a5e8faSwyllys 	KMF_RETURN ret = KMF_OK;
50899ebb4caSwyllys 	KMF_PLUGIN *plugin;
50930a5e8faSwyllys 	KMF_KEYSTORE_TYPE kstype;
51030a5e8faSwyllys 	uint32_t len;
51130a5e8faSwyllys 
51230a5e8faSwyllys 	KMF_ATTRIBUTE_TESTER required_attrs[] = {
51330a5e8faSwyllys 		{KMF_KEYSTORE_TYPE_ATTR, FALSE, 1, sizeof (KMF_KEYSTORE_TYPE)},
51430a5e8faSwyllys 	};
51530a5e8faSwyllys 
51630a5e8faSwyllys 	int num_req_attrs = sizeof (required_attrs) /
51730a5e8faSwyllys 	    sizeof (KMF_ATTRIBUTE_TESTER);
51830a5e8faSwyllys 
51930a5e8faSwyllys 	if (handle == NULL)
52030a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
52199ebb4caSwyllys 
52299ebb4caSwyllys 	CLEAR_ERROR(handle, ret);
52330a5e8faSwyllys 
52430a5e8faSwyllys 	ret = test_attributes(num_req_attrs, required_attrs,
52530a5e8faSwyllys 	    0, NULL, num_args, attrlist);
52630a5e8faSwyllys 
52799ebb4caSwyllys 	if (ret != KMF_OK)
52899ebb4caSwyllys 		return (ret);
52999ebb4caSwyllys 
53030a5e8faSwyllys 	len = sizeof (kstype);
53130a5e8faSwyllys 	ret = kmf_get_attr(KMF_KEYSTORE_TYPE_ATTR, attrlist, num_args,
53230a5e8faSwyllys 	    &kstype, &len);
53330a5e8faSwyllys 	if (ret != KMF_OK)
53430a5e8faSwyllys 		return (ret);
53599ebb4caSwyllys 
53630a5e8faSwyllys 	plugin = FindPlugin(handle, kstype);
53730a5e8faSwyllys 	if (plugin != NULL && plugin->funclist->ConfigureKeystore != NULL) {
53830a5e8faSwyllys 		return (plugin->funclist->ConfigureKeystore(handle, num_args,
53930a5e8faSwyllys 		    attrlist));
54030a5e8faSwyllys 	} else {
54199ebb4caSwyllys 		/* return KMF_OK, if the plugin does not have an entry */
54299ebb4caSwyllys 		return (KMF_OK);
54330a5e8faSwyllys 	}
54499ebb4caSwyllys }
54599ebb4caSwyllys 
54699ebb4caSwyllys KMF_RETURN
54730a5e8faSwyllys kmf_finalize(KMF_HANDLE_T handle)
54899ebb4caSwyllys {
54999ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
55099ebb4caSwyllys 
55199ebb4caSwyllys 	CLEAR_ERROR(handle, ret);
55299ebb4caSwyllys 	if (ret != KMF_OK)
55399ebb4caSwyllys 		return (ret);
55499ebb4caSwyllys 
55599ebb4caSwyllys 	if (pkcs11_initialized) {
55699ebb4caSwyllys 		Cleanup_PK11_Session(handle);
55799ebb4caSwyllys 	}
55899ebb4caSwyllys 	Cleanup_KMF_Handle(handle);
55999ebb4caSwyllys 
56099ebb4caSwyllys 	return (ret);
56199ebb4caSwyllys }
56299ebb4caSwyllys 
56399ebb4caSwyllys KMF_RETURN
56430a5e8faSwyllys kmf_get_kmf_error_str(KMF_RETURN errcode, char **errmsg)
56599ebb4caSwyllys {
56699ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
56799ebb4caSwyllys 	int i, maxerr;
56899ebb4caSwyllys 
56999ebb4caSwyllys 	if (errmsg == NULL)
57099ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
57199ebb4caSwyllys 
57299ebb4caSwyllys 	*errmsg = NULL;
57399ebb4caSwyllys 	maxerr = sizeof (kmf_errcodes) / sizeof (kmf_error_map);
57499ebb4caSwyllys 
57530a5e8faSwyllys 	for (i = 0; i < maxerr && errcode != kmf_errcodes[i].code; i++)
57630a5e8faSwyllys 		/* empty body */
57730a5e8faSwyllys 		;
57899ebb4caSwyllys 
57999ebb4caSwyllys 	if (i == maxerr)
58099ebb4caSwyllys 		return (KMF_ERR_MISSING_ERRCODE);
58199ebb4caSwyllys 	else {
58299ebb4caSwyllys 		*errmsg = strdup(kmf_errcodes[i].message);
58399ebb4caSwyllys 		if ((*errmsg) == NULL)
58499ebb4caSwyllys 			return (KMF_ERR_MEMORY);
58599ebb4caSwyllys 	}
58699ebb4caSwyllys 	return (ret);
58799ebb4caSwyllys }
58899ebb4caSwyllys 
58999ebb4caSwyllys KMF_RETURN
59030a5e8faSwyllys kmf_get_plugin_error_str(KMF_HANDLE_T handle, char **msgstr)
59199ebb4caSwyllys {
59299ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
59399ebb4caSwyllys 	KMF_PLUGIN *plugin;
59499ebb4caSwyllys 
59599ebb4caSwyllys 	if (handle == NULL || msgstr == NULL)
59699ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
59799ebb4caSwyllys 
59899ebb4caSwyllys 	*msgstr = NULL;
59999ebb4caSwyllys 
60099ebb4caSwyllys 	if (handle->lasterr.errcode == 0) {
60199ebb4caSwyllys 		return (KMF_ERR_MISSING_ERRCODE);
60299ebb4caSwyllys 	}
60399ebb4caSwyllys 
60499ebb4caSwyllys 	if (handle->lasterr.kstype == -1) { /* System error */
60599ebb4caSwyllys 		char *str = strerror(handle->lasterr.errcode);
60699ebb4caSwyllys 		if (str != NULL) {
60799ebb4caSwyllys 			*msgstr = strdup(str);
60899ebb4caSwyllys 			if ((*msgstr) == NULL)
60999ebb4caSwyllys 				return (KMF_ERR_MEMORY);
61099ebb4caSwyllys 		}
61199ebb4caSwyllys 		return (KMF_OK);
61299ebb4caSwyllys 	}
61399ebb4caSwyllys 
61499ebb4caSwyllys 	plugin = FindPlugin(handle, handle->lasterr.kstype);
61599ebb4caSwyllys 	if (plugin == NULL)
61699ebb4caSwyllys 		return (KMF_ERR_PLUGIN_NOTFOUND);
61799ebb4caSwyllys 
61899ebb4caSwyllys 	if (plugin->funclist->GetErrorString != NULL) {
61999ebb4caSwyllys 		ret = plugin->funclist->GetErrorString(handle, msgstr);
62099ebb4caSwyllys 	} else {
62199ebb4caSwyllys 		return (KMF_ERR_FUNCTION_NOT_FOUND);
62299ebb4caSwyllys 	}
62399ebb4caSwyllys 
62499ebb4caSwyllys 	return (ret);
62599ebb4caSwyllys }
62699ebb4caSwyllys 
62799ebb4caSwyllys 
6289b37d296Swyllys #define	SET_SYS_ERROR(h, c) if (h) {\
6299b37d296Swyllys 	h->lasterr.kstype = -1;\
6309b37d296Swyllys 	h->lasterr.errcode = c;\
6319b37d296Swyllys }
63299ebb4caSwyllys 
63399ebb4caSwyllys KMF_RETURN
63430a5e8faSwyllys kmf_read_input_file(KMF_HANDLE_T handle, char *filename,  KMF_DATA *pdata)
63599ebb4caSwyllys {
63699ebb4caSwyllys 	struct stat s;
63799ebb4caSwyllys 	long nread, total = 0;
63899ebb4caSwyllys 	int fd;
63999ebb4caSwyllys 	unsigned char *buf = NULL;
64099ebb4caSwyllys 	KMF_RETURN ret;
64199ebb4caSwyllys 
6429b37d296Swyllys 	if (handle) {
6439b37d296Swyllys 		CLEAR_ERROR(handle, ret);
6449b37d296Swyllys 		if (ret != KMF_OK)
6459b37d296Swyllys 			return (ret);
6469b37d296Swyllys 	}
64799ebb4caSwyllys 
64899ebb4caSwyllys 	if (filename == NULL || pdata == NULL) {
64999ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
65099ebb4caSwyllys 	}
65199ebb4caSwyllys 
65299ebb4caSwyllys 	if ((fd = open(filename, O_RDONLY)) < 0) {
65399ebb4caSwyllys 		SET_SYS_ERROR(handle, errno);
65499ebb4caSwyllys 		return (KMF_ERR_OPEN_FILE);
65599ebb4caSwyllys 	}
65699ebb4caSwyllys 
65799ebb4caSwyllys 	if (fstat(fd, &s) < 0) {
65899ebb4caSwyllys 		SET_SYS_ERROR(handle, errno);
65999ebb4caSwyllys 		(void) close(fd);
66099ebb4caSwyllys 		return (KMF_ERR_OPEN_FILE);
66199ebb4caSwyllys 	}
66299ebb4caSwyllys 
66399ebb4caSwyllys 	if ((buf = (unsigned char *) malloc(s.st_size)) == NULL) {
66499ebb4caSwyllys 		(void) close(fd);
66599ebb4caSwyllys 		return (KMF_ERR_MEMORY);
66699ebb4caSwyllys 	}
66799ebb4caSwyllys 
66899ebb4caSwyllys 	do {
66999ebb4caSwyllys 		nread = read(fd, buf+total, s.st_size-total);
67099ebb4caSwyllys 		if (nread < 0) {
67199ebb4caSwyllys 			SET_SYS_ERROR(handle, errno);
67299ebb4caSwyllys 			(void) close(fd);
67399ebb4caSwyllys 			free(buf);
67499ebb4caSwyllys 			return (KMF_ERR_INTERNAL);
67599ebb4caSwyllys 		}
67699ebb4caSwyllys 		total += nread;
67799ebb4caSwyllys 	} while (total < s.st_size);
67899ebb4caSwyllys 
67999ebb4caSwyllys 	pdata->Data = buf;
68099ebb4caSwyllys 	pdata->Length = s.st_size;
68199ebb4caSwyllys 	(void) close(fd);
68299ebb4caSwyllys 	return (KMF_OK);
68399ebb4caSwyllys }
68499ebb4caSwyllys 
68599ebb4caSwyllys /*
68699ebb4caSwyllys  *
68730a5e8faSwyllys  * Name: kmf_der_to_pem
68899ebb4caSwyllys  *
68999ebb4caSwyllys  * Description:
69099ebb4caSwyllys  *   Function for converting DER encoded format to PEM encoded format
69199ebb4caSwyllys  *
69299ebb4caSwyllys  * Parameters:
69399ebb4caSwyllys  *   type(input) - CERTIFICATE or CSR
69499ebb4caSwyllys  *   data(input) - pointer to the DER encoded data
69599ebb4caSwyllys  *   len(input)  - length of input data
69699ebb4caSwyllys  *   out(output) - contains the output buffer address to be returned
69799ebb4caSwyllys  *   outlen(output) - pointer to the returned output length
69899ebb4caSwyllys  *
69999ebb4caSwyllys  * Returns:
70099ebb4caSwyllys  *   A KMF_RETURN value indicating success or specifying a particular
70199ebb4caSwyllys  * error condition.
70299ebb4caSwyllys  *   The value KMF_OK indicates success. All other values represent
70399ebb4caSwyllys  * an error condition.
70499ebb4caSwyllys  *
70599ebb4caSwyllys  */
70699ebb4caSwyllys KMF_RETURN
70730a5e8faSwyllys kmf_der_to_pem(KMF_OBJECT_TYPE type, unsigned char *data,
70899ebb4caSwyllys 	int len, unsigned char **out, int *outlen)
70999ebb4caSwyllys {
71099ebb4caSwyllys 
71199ebb4caSwyllys 	KMF_RETURN err;
71299ebb4caSwyllys 	if (data == NULL || out == NULL || outlen == NULL)
71399ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
71499ebb4caSwyllys 
71599ebb4caSwyllys 	err = Der2Pem(type, data, len, out, outlen);
71699ebb4caSwyllys 	return (err);
71799ebb4caSwyllys 
71899ebb4caSwyllys }
71999ebb4caSwyllys 
72099ebb4caSwyllys /*
72199ebb4caSwyllys  *
72230a5e8faSwyllys  * Name: kmf_pem_to_der
72399ebb4caSwyllys  *
72499ebb4caSwyllys  * Description:
72599ebb4caSwyllys  *   Function for converting PEM encoded format to DER encoded format
72699ebb4caSwyllys  *
72799ebb4caSwyllys  * Parameters:
72899ebb4caSwyllys  *   in(input) - pointer to the PEM encoded data
72999ebb4caSwyllys  *   inlen(input)  - length of input data
73099ebb4caSwyllys  *   out(output) - contains the output buffer address to be returned
73199ebb4caSwyllys  *   outlen(output) - pointer to the returned output length
73299ebb4caSwyllys  *
73399ebb4caSwyllys  * Returns:
73499ebb4caSwyllys  *   A KMF_RETURN value indicating success or specifying a particular
73599ebb4caSwyllys  *   error condition.
73699ebb4caSwyllys  *   The value KMF_OK indicates success. All other values represent
73799ebb4caSwyllys  *   an error condition.
73899ebb4caSwyllys  *
73999ebb4caSwyllys  */
74099ebb4caSwyllys KMF_RETURN
74130a5e8faSwyllys kmf_pem_to_der(unsigned char *in, int inlen,
74299ebb4caSwyllys 	unsigned char **out, int *outlen)
74399ebb4caSwyllys {
74499ebb4caSwyllys 	KMF_RETURN err;
74599ebb4caSwyllys 	if (in == NULL || out == NULL || outlen == NULL)
74699ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
74799ebb4caSwyllys 
74899ebb4caSwyllys 	err = Pem2Der(in, inlen, out, outlen);
74999ebb4caSwyllys 	return (err);
75099ebb4caSwyllys }
75199ebb4caSwyllys 
75299ebb4caSwyllys char *
75330a5e8faSwyllys kmf_oid_to_string(KMF_OID *oid)
75499ebb4caSwyllys {
75599ebb4caSwyllys 	char numstr[128];
75699ebb4caSwyllys 	uint32_t number;
75799ebb4caSwyllys 	int numshift;
75899ebb4caSwyllys 	uint32_t i, string_length;
75999ebb4caSwyllys 	uchar_t *cp;
76099ebb4caSwyllys 	char *bp;
76199ebb4caSwyllys 
76299ebb4caSwyllys 	/* First determine the size of the string */
76399ebb4caSwyllys 	string_length = 0;
76499ebb4caSwyllys 	number = 0;
76599ebb4caSwyllys 	numshift = 0;
76699ebb4caSwyllys 	cp = (unsigned char *)oid->Data;
76799ebb4caSwyllys 
76899ebb4caSwyllys 	number = (uint32_t)cp[0];
76999ebb4caSwyllys 	(void) sprintf(numstr, "%d ", number/40);
77099ebb4caSwyllys 
77199ebb4caSwyllys 	string_length += strlen(numstr);
77299ebb4caSwyllys 	(void) sprintf(numstr, "%d ", number%40);
77399ebb4caSwyllys 
77499ebb4caSwyllys 	string_length += strlen(numstr);
77599ebb4caSwyllys 
77699ebb4caSwyllys 	for (i = 1; i < oid->Length; i++) {
77799ebb4caSwyllys 		if ((uint32_t)(numshift+7) < (sizeof (uint32_t)*8)) {
77899ebb4caSwyllys 			number = (number << 7) | (cp[i] & 0x7f);
77999ebb4caSwyllys 			numshift += 7;
78099ebb4caSwyllys 		} else {
78199ebb4caSwyllys 			return (NULL);
78299ebb4caSwyllys 		}
78399ebb4caSwyllys 
78499ebb4caSwyllys 		if ((cp[i] & 0x80) == 0) {
78599ebb4caSwyllys 			(void) sprintf(numstr, "%d ", number);
78699ebb4caSwyllys 			string_length += strlen(numstr);
78799ebb4caSwyllys 			number = 0;
78899ebb4caSwyllys 			numshift = 0;
78999ebb4caSwyllys 		}
79099ebb4caSwyllys 	}
79199ebb4caSwyllys 	/*
79299ebb4caSwyllys 	 * If we get here, we've calculated the length of "n n n ... n ".  Add 4
79399ebb4caSwyllys 	 * here for "{ " and "}\0".
79499ebb4caSwyllys 	 */
79599ebb4caSwyllys 	string_length += 4;
79699ebb4caSwyllys 	if ((bp = (char *)malloc(string_length))) {
79799ebb4caSwyllys 		number = (uint32_t)cp[0];
79899ebb4caSwyllys 
79999ebb4caSwyllys 		(void) sprintf(numstr, "%d.", number/40);
80099ebb4caSwyllys 		(void) strcpy(bp, numstr);
80199ebb4caSwyllys 
80299ebb4caSwyllys 		(void) sprintf(numstr, "%d.", number%40);
80399ebb4caSwyllys 		(void) strcat(bp, numstr);
80499ebb4caSwyllys 
80599ebb4caSwyllys 		number = 0;
80699ebb4caSwyllys 		cp = (unsigned char *) oid->Data;
80799ebb4caSwyllys 		for (i = 1; i < oid->Length; i++) {
80899ebb4caSwyllys 			number = (number << 7) | (cp[i] & 0x7f);
80999ebb4caSwyllys 			if ((cp[i] & 0x80) == 0) {
81099ebb4caSwyllys 				(void) sprintf(numstr, "%d", number);
81199ebb4caSwyllys 				(void) strcat(bp, numstr);
81299ebb4caSwyllys 				number = 0;
81399ebb4caSwyllys 				if (i+1 < oid->Length)
81499ebb4caSwyllys 					(void) strcat(bp, ".");
81599ebb4caSwyllys 			}
81699ebb4caSwyllys 		}
81799ebb4caSwyllys 	}
81899ebb4caSwyllys 	return (bp);
81999ebb4caSwyllys }
82099ebb4caSwyllys 
82102744e81Swyllys static boolean_t
82208ec4bd3Shylee check_for_pem(uchar_t *buf, KMF_ENCODE_FORMAT *fmt)
82302744e81Swyllys {
8249b37d296Swyllys 	char *p;
825*d00756ccSwyllys 	int i;
82602744e81Swyllys 
82708ec4bd3Shylee 	if (buf == NULL)
82802744e81Swyllys 		return (FALSE);
82902744e81Swyllys 
830*d00756ccSwyllys 	for (i = 0; i < 8 && isascii(buf[i]); i++)
831*d00756ccSwyllys 		/* loop to make sure this is ascii */;
832*d00756ccSwyllys 	if (i != 8)
833*d00756ccSwyllys 		return (FALSE);
834*d00756ccSwyllys 
83508ec4bd3Shylee 	if (memcmp(buf, "Bag Attr", 8) == 0) {
83608ec4bd3Shylee 		*fmt = KMF_FORMAT_PEM_KEYPAIR;
83708ec4bd3Shylee 		return (TRUE);
83808ec4bd3Shylee 	}
83908ec4bd3Shylee 
8409b37d296Swyllys 	/* Look for "-----BEGIN" right after a newline */
84108ec4bd3Shylee 	p = strtok((char *)buf, "\n");
8429b37d296Swyllys 	while (p != NULL) {
8439b37d296Swyllys 		if (strstr(p, "-----BEGIN") != NULL) {
84408ec4bd3Shylee 			*fmt = KMF_FORMAT_PEM;
845*d00756ccSwyllys 			/* Restore the buffer */
846*d00756ccSwyllys 			buf[strlen(p)] = '\n';
8479b37d296Swyllys 			return (TRUE);
84802744e81Swyllys 		}
849*d00756ccSwyllys 		buf[strlen(p)] = '\n';
8509b37d296Swyllys 		p = strtok(NULL, "\n");
85102744e81Swyllys 	}
85202744e81Swyllys 	return (FALSE);
85302744e81Swyllys }
85402744e81Swyllys 
85508ec4bd3Shylee 
85608ec4bd3Shylee static unsigned char pkcs12_version[3] = {0x02, 0x01, 0x03};
85708ec4bd3Shylee static unsigned char pkcs12_oid[11] =
85808ec4bd3Shylee {0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01};
85908ec4bd3Shylee 
86008ec4bd3Shylee /*
86108ec4bd3Shylee  * This function takes a BER encoded string as input and checks the version
86208ec4bd3Shylee  * and the oid in the the top-level ASN.1 structure to see if it complies to
86308ec4bd3Shylee  * the PKCS#12 Syntax.
86408ec4bd3Shylee  */
86508ec4bd3Shylee static boolean_t
86608ec4bd3Shylee check_for_pkcs12(uchar_t *buf, int buf_len)
86708ec4bd3Shylee {
86808ec4bd3Shylee 	int index = 0;
86908ec4bd3Shylee 	int length_octets;
87008ec4bd3Shylee 
87108ec4bd3Shylee 	if (buf == NULL || buf_len <= 0)
87208ec4bd3Shylee 		return (FALSE);
87308ec4bd3Shylee 
87408ec4bd3Shylee 	/*
87508ec4bd3Shylee 	 * The top level structure for a PKCS12 string:
87608ec4bd3Shylee 	 *
87708ec4bd3Shylee 	 * PFX ::= SEQUENCE {
87808ec4bd3Shylee 	 *	version		INTEGER {v3(3)}(v3,...)
87908ec4bd3Shylee 	 *	authSafe	ContentInfo
88008ec4bd3Shylee 	 *	macData		MacData OPTIONAL
88108ec4bd3Shylee 	 * }
88208ec4bd3Shylee 	 *
88308ec4bd3Shylee 	 * ContentInfo
88408ec4bd3Shylee 	 *	FROM PKCS-7 {iso(1) member-body(2) us(840) rsadsi(113549)
88508ec4bd3Shylee 	 *		pkcs(1) pkcs-7(7) modules(0) pkcs-7(1)}
88608ec4bd3Shylee 	 *
88708ec4bd3Shylee 	 * Therefore, the BER/DER dump of a PKCS#12 file for the first 2
88808ec4bd3Shylee 	 * sequences up to the oid part is as following:
88908ec4bd3Shylee 	 *
89008ec4bd3Shylee 	 *	SEQUENCE {
89108ec4bd3Shylee 	 *	    INTEGER 3
89208ec4bd3Shylee 	 *	    SEQUENCE {
89308ec4bd3Shylee 	 *		OBJECT IDENTIFIER data (1 2 840 113549 1 7 1)
89408ec4bd3Shylee 	 */
89508ec4bd3Shylee 
89608ec4bd3Shylee 	/*
89708ec4bd3Shylee 	 * Check the first sequence and calculate the number of bytes used
89808ec4bd3Shylee 	 * to store the length.
89908ec4bd3Shylee 	 */
90008ec4bd3Shylee 	if (buf[index++] != 0x30)
90108ec4bd3Shylee 		return (FALSE);
90208ec4bd3Shylee 
90308ec4bd3Shylee 	if (buf[index] & 0x80) {
90408ec4bd3Shylee 		length_octets = buf[index++] & 0x0F;  /* long form */
90508ec4bd3Shylee 	} else {
90608ec4bd3Shylee 		length_octets = 1; /* short form */
90708ec4bd3Shylee 	}
90808ec4bd3Shylee 
90908ec4bd3Shylee 	index += length_octets;
91008ec4bd3Shylee 	if (index  >= buf_len)
91108ec4bd3Shylee 		return (FALSE);
91208ec4bd3Shylee 
91308ec4bd3Shylee 	/* Skip the length octets and check the pkcs12 version */
91408ec4bd3Shylee 	if (memcmp(buf + index, pkcs12_version, sizeof (pkcs12_version)) != 0)
91508ec4bd3Shylee 		return (FALSE);
91608ec4bd3Shylee 
91708ec4bd3Shylee 	index += sizeof (pkcs12_version);
91808ec4bd3Shylee 	if (index  >= buf_len)
91908ec4bd3Shylee 		return (FALSE);
92008ec4bd3Shylee 
92108ec4bd3Shylee 	/*
92208ec4bd3Shylee 	 * Check the 2nd sequence and calculate the number of bytes used
92308ec4bd3Shylee 	 * to store the length.
92408ec4bd3Shylee 	 */
92508ec4bd3Shylee 	if ((buf[index++] & 0xFF) != 0x30)
92608ec4bd3Shylee 		return (FALSE);
92708ec4bd3Shylee 
92808ec4bd3Shylee 	if (buf[index] & 0x80) {
92908ec4bd3Shylee 		length_octets = buf[index++] & 0x0F;
93008ec4bd3Shylee 	} else {
93108ec4bd3Shylee 		length_octets = 1;
93208ec4bd3Shylee 	}
93308ec4bd3Shylee 
93408ec4bd3Shylee 	index += length_octets;
93508ec4bd3Shylee 	if (index + sizeof (pkcs12_oid) >= buf_len)
93608ec4bd3Shylee 		return (FALSE);
93708ec4bd3Shylee 
93808ec4bd3Shylee 	/* Skip the length octets and check the oid */
93908ec4bd3Shylee 	if (memcmp(buf + index, pkcs12_oid, sizeof (pkcs12_oid)) != 0)
94008ec4bd3Shylee 		return (FALSE);
94108ec4bd3Shylee 	else
94208ec4bd3Shylee 		return (TRUE);
94308ec4bd3Shylee }
94408ec4bd3Shylee 
945*d00756ccSwyllys KMF_RETURN
946*d00756ccSwyllys kmf_get_data_format(KMF_DATA *data, KMF_ENCODE_FORMAT *fmt)
947*d00756ccSwyllys {
948*d00756ccSwyllys 	uchar_t *buf = data->Data;
949*d00756ccSwyllys 
950*d00756ccSwyllys 	if (check_for_pkcs12(buf, data->Length) == TRUE) {
951*d00756ccSwyllys 		*fmt = KMF_FORMAT_PKCS12;
952*d00756ccSwyllys 	} else if (buf[0] == 0x30 && (buf[1] & 0x80)) {
953*d00756ccSwyllys 		/* It is most likely a generic ASN.1 encoded file */
954*d00756ccSwyllys 		*fmt = KMF_FORMAT_ASN1;
955*d00756ccSwyllys 	} else if (check_for_pem(buf, fmt) != TRUE) {
956*d00756ccSwyllys 		/* Cannot determine this file format */
957*d00756ccSwyllys 		*fmt = KMF_FORMAT_UNDEF;
958*d00756ccSwyllys 		return (KMF_ERR_ENCODING);
959*d00756ccSwyllys 	}
960*d00756ccSwyllys 	return (KMF_OK);
961*d00756ccSwyllys }
962*d00756ccSwyllys 
96399ebb4caSwyllys KMF_RETURN
96430a5e8faSwyllys kmf_get_file_format(char *filename, KMF_ENCODE_FORMAT *fmt)
96599ebb4caSwyllys {
96699ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
96708ec4bd3Shylee 	KMF_DATA filebuf = {NULL, 0};
96899ebb4caSwyllys 
96999ebb4caSwyllys 	if (filename == NULL || !strlen(filename) || fmt == NULL)
97099ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
97199ebb4caSwyllys 
97299ebb4caSwyllys 	*fmt = 0;
97330a5e8faSwyllys 	ret = kmf_read_input_file(NULL, filename, &filebuf);
97408ec4bd3Shylee 	if (ret != KMF_OK)
97508ec4bd3Shylee 		return (ret);
97699ebb4caSwyllys 
97708ec4bd3Shylee 	if (filebuf.Length < 8) {
97808ec4bd3Shylee 		ret = KMF_ERR_ENCODING; /* too small */
97999ebb4caSwyllys 		goto end;
98099ebb4caSwyllys 	}
98199ebb4caSwyllys 
982*d00756ccSwyllys 	ret = kmf_get_data_format(&filebuf, fmt);
98399ebb4caSwyllys end:
98430a5e8faSwyllys 	kmf_free_data(&filebuf);
98599ebb4caSwyllys 	return (ret);
98699ebb4caSwyllys }
98799ebb4caSwyllys 
98899ebb4caSwyllys KMF_RETURN
98930a5e8faSwyllys kmf_hexstr_to_bytes(unsigned char *hexstr, unsigned char **bytes,
99099ebb4caSwyllys 	size_t *outlen)
99199ebb4caSwyllys {
99299ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
99399ebb4caSwyllys 	unsigned char *buf = NULL;
99499ebb4caSwyllys 	int len, stringlen;
99599ebb4caSwyllys 	int i;
99699ebb4caSwyllys 	unsigned char ch;
99799ebb4caSwyllys 
99899ebb4caSwyllys 	if (hexstr == NULL) {
99999ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
100099ebb4caSwyllys 	}
100199ebb4caSwyllys 
100230a5e8faSwyllys 	if (hexstr[0] == '0' && ((hexstr[1] == 'x') || (hexstr[1] == 'X')))
100399ebb4caSwyllys 		hexstr += 2;
100499ebb4caSwyllys 
100530a5e8faSwyllys 	for (i = 0; i < strlen((char *)hexstr) && isxdigit(hexstr[i]); i++)
100630a5e8faSwyllys 		/* empty body */
100730a5e8faSwyllys 		;
100899ebb4caSwyllys 	/*
100999ebb4caSwyllys 	 * If all the characters are not legitimate hex chars,
101099ebb4caSwyllys 	 * return an error.
101199ebb4caSwyllys 	 */
101299ebb4caSwyllys 	if (i != strlen((char *)hexstr))
101399ebb4caSwyllys 		return (KMF_ERR_BAD_HEX_STRING);
101499ebb4caSwyllys 	stringlen = i;
101599ebb4caSwyllys 	len = (i / 2) + (i % 2);
101699ebb4caSwyllys 
101799ebb4caSwyllys 	buf = malloc(len);
101899ebb4caSwyllys 	if (buf == NULL) {
101999ebb4caSwyllys 		return (KMF_ERR_MEMORY);
102099ebb4caSwyllys 	}
102199ebb4caSwyllys 	(void) memset(buf, 0, len);
102299ebb4caSwyllys 
102399ebb4caSwyllys 	for (i = 0; i < stringlen; i++) {
102499ebb4caSwyllys 		ch = (unsigned char) *hexstr;
102599ebb4caSwyllys 		hexstr++;
102699ebb4caSwyllys 		if ((ch >= '0') && (ch <= '9'))
102799ebb4caSwyllys 			ch -= '0';
102899ebb4caSwyllys 		else if ((ch >= 'A') && (ch <= 'F'))
102999ebb4caSwyllys 			ch = ch - 'A' + 10;
103099ebb4caSwyllys 		else if ((ch >= 'a') && (ch <= 'f'))
103199ebb4caSwyllys 			ch = ch - 'a' + 10;
103299ebb4caSwyllys 		else {
103399ebb4caSwyllys 			ret = KMF_ERR_BAD_HEX_STRING;
103499ebb4caSwyllys 			goto out;
103599ebb4caSwyllys 		}
103699ebb4caSwyllys 
103799ebb4caSwyllys 		if (i & 1) {
103899ebb4caSwyllys 			buf[i/2] |= ch;
103999ebb4caSwyllys 		} else {
104099ebb4caSwyllys 			buf[i/2] = (ch << 4);
104199ebb4caSwyllys 		}
104299ebb4caSwyllys 	}
104399ebb4caSwyllys 
104499ebb4caSwyllys 	*bytes = buf;
104599ebb4caSwyllys 	*outlen = len;
104699ebb4caSwyllys out:
104799ebb4caSwyllys 	if (buf != NULL && ret != KMF_OK) {
104899ebb4caSwyllys 		free(buf);
104999ebb4caSwyllys 	}
105099ebb4caSwyllys 	return (ret);
105199ebb4caSwyllys }
105299ebb4caSwyllys 
105399ebb4caSwyllys void
105430a5e8faSwyllys kmf_free_dn(KMF_X509_NAME *name)
105599ebb4caSwyllys {
105699ebb4caSwyllys 	KMF_X509_RDN 		*newrdn = NULL;
105799ebb4caSwyllys 	KMF_X509_TYPE_VALUE_PAIR *av = NULL;
105899ebb4caSwyllys 	int i, j;
105999ebb4caSwyllys 
106099ebb4caSwyllys 	if (name && name->numberOfRDNs) {
106199ebb4caSwyllys 		for (i = 0; i < name->numberOfRDNs; i++) {
106299ebb4caSwyllys 			newrdn = &name->RelativeDistinguishedName[i];
106399ebb4caSwyllys 			for (j = 0; j < newrdn->numberOfPairs; j++) {
106499ebb4caSwyllys 				av = &newrdn->AttributeTypeAndValue[j];
106530a5e8faSwyllys 				kmf_free_data(&av->type);
106630a5e8faSwyllys 				kmf_free_data(&av->value);
106799ebb4caSwyllys 			}
106899ebb4caSwyllys 			free(newrdn->AttributeTypeAndValue);
106999ebb4caSwyllys 			newrdn->numberOfPairs = 0;
107099ebb4caSwyllys 			newrdn->AttributeTypeAndValue = NULL;
107199ebb4caSwyllys 		}
107299ebb4caSwyllys 		free(name->RelativeDistinguishedName);
107399ebb4caSwyllys 		name->numberOfRDNs = 0;
107499ebb4caSwyllys 		name->RelativeDistinguishedName = NULL;
107599ebb4caSwyllys 	}
107699ebb4caSwyllys }
107799ebb4caSwyllys 
107899ebb4caSwyllys void
107930a5e8faSwyllys kmf_free_kmf_cert(KMF_HANDLE_T handle, KMF_X509_DER_CERT *kmf_cert)
108099ebb4caSwyllys {
108199ebb4caSwyllys 	KMF_PLUGIN *plugin;
108299ebb4caSwyllys 	KMF_RETURN ret;
108399ebb4caSwyllys 
108499ebb4caSwyllys 	CLEAR_ERROR(handle, ret);
108599ebb4caSwyllys 	if (ret != KMF_OK)
108699ebb4caSwyllys 		return;
108799ebb4caSwyllys 
108899ebb4caSwyllys 	if (kmf_cert == NULL)
108999ebb4caSwyllys 		return;
109099ebb4caSwyllys 
109199ebb4caSwyllys 	plugin = FindPlugin(handle, kmf_cert->kmf_private.keystore_type);
109299ebb4caSwyllys 
109399ebb4caSwyllys 	if (plugin != NULL && plugin->funclist->FreeKMFCert != NULL) {
109499ebb4caSwyllys 		plugin->funclist->FreeKMFCert(handle, kmf_cert);
109599ebb4caSwyllys 	}
109699ebb4caSwyllys }
109799ebb4caSwyllys 
109899ebb4caSwyllys void
109930a5e8faSwyllys kmf_free_data(KMF_DATA *datablock)
110099ebb4caSwyllys {
110199ebb4caSwyllys 	if (datablock != NULL && datablock->Data != NULL) {
110299ebb4caSwyllys 		free(datablock->Data);
110399ebb4caSwyllys 		datablock->Data = NULL;
110499ebb4caSwyllys 		datablock->Length = 0;
110599ebb4caSwyllys 	}
110699ebb4caSwyllys }
110799ebb4caSwyllys 
110899ebb4caSwyllys void
110930a5e8faSwyllys kmf_free_algoid(KMF_X509_ALGORITHM_IDENTIFIER *algoid)
111099ebb4caSwyllys {
111199ebb4caSwyllys 	if (algoid == NULL)
111299ebb4caSwyllys 		return;
111330a5e8faSwyllys 	kmf_free_data(&algoid->algorithm);
111430a5e8faSwyllys 	kmf_free_data(&algoid->parameters);
111599ebb4caSwyllys }
111699ebb4caSwyllys 
111799ebb4caSwyllys void
111830a5e8faSwyllys kmf_free_extn(KMF_X509_EXTENSION *exptr)
111999ebb4caSwyllys {
112099ebb4caSwyllys 	if (exptr == NULL)
112199ebb4caSwyllys 		return;
112299ebb4caSwyllys 
112330a5e8faSwyllys 	kmf_free_data((KMF_DATA *)&exptr->extnId);
112430a5e8faSwyllys 	kmf_free_data(&exptr->BERvalue);
112599ebb4caSwyllys 
112699ebb4caSwyllys 	if (exptr->value.tagAndValue) {
112730a5e8faSwyllys 		kmf_free_data(&exptr->value.tagAndValue->value);
112899ebb4caSwyllys 		free(exptr->value.tagAndValue);
112999ebb4caSwyllys 	}
113099ebb4caSwyllys }
113199ebb4caSwyllys 
113299ebb4caSwyllys void
113330a5e8faSwyllys kmf_free_tbs_csr(KMF_TBS_CSR *tbscsr)
113499ebb4caSwyllys {
113599ebb4caSwyllys 	if (tbscsr) {
113630a5e8faSwyllys 		kmf_free_data(&tbscsr->version);
113799ebb4caSwyllys 
113830a5e8faSwyllys 		kmf_free_dn(&tbscsr->subject);
113999ebb4caSwyllys 
114030a5e8faSwyllys 		kmf_free_algoid(&tbscsr->subjectPublicKeyInfo.algorithm);
114130a5e8faSwyllys 		kmf_free_data(&tbscsr->subjectPublicKeyInfo.subjectPublicKey);
114299ebb4caSwyllys 
114399ebb4caSwyllys 		free_extensions(&tbscsr->extensions);
114499ebb4caSwyllys 	}
114599ebb4caSwyllys }
114699ebb4caSwyllys 
114799ebb4caSwyllys void
114830a5e8faSwyllys kmf_free_signed_csr(KMF_CSR_DATA *csr)
114999ebb4caSwyllys {
115099ebb4caSwyllys 	if (csr) {
115130a5e8faSwyllys 		kmf_free_tbs_csr(&csr->csr);
115299ebb4caSwyllys 
115330a5e8faSwyllys 		kmf_free_algoid(&csr->signature.algorithmIdentifier);
115430a5e8faSwyllys 		kmf_free_data(&csr->signature.encrypted);
115599ebb4caSwyllys 	}
115699ebb4caSwyllys }
115799ebb4caSwyllys 
115899ebb4caSwyllys static void
115999ebb4caSwyllys free_validity(KMF_X509_VALIDITY *validity)
116099ebb4caSwyllys {
116199ebb4caSwyllys 	if (validity == NULL)
116299ebb4caSwyllys 		return;
116330a5e8faSwyllys 	kmf_free_data(&validity->notBefore.time);
116430a5e8faSwyllys 	kmf_free_data(&validity->notAfter.time);
116599ebb4caSwyllys }
116699ebb4caSwyllys 
116799ebb4caSwyllys static void
116899ebb4caSwyllys free_extensions(KMF_X509_EXTENSIONS *extns)
116999ebb4caSwyllys {
117099ebb4caSwyllys 	int i;
117199ebb4caSwyllys 	KMF_X509_EXTENSION *exptr;
117299ebb4caSwyllys 
117399ebb4caSwyllys 	if (extns && extns->numberOfExtensions > 0) {
117499ebb4caSwyllys 		for (i = 0; i < extns->numberOfExtensions; i++) {
117599ebb4caSwyllys 			exptr = &extns->extensions[i];
117630a5e8faSwyllys 			kmf_free_extn(exptr);
117799ebb4caSwyllys 		}
117899ebb4caSwyllys 		free(extns->extensions);
117999ebb4caSwyllys 		extns->numberOfExtensions = 0;
118099ebb4caSwyllys 		extns->extensions = NULL;
118199ebb4caSwyllys 	}
118299ebb4caSwyllys }
118399ebb4caSwyllys 
118499ebb4caSwyllys void
118530a5e8faSwyllys kmf_free_tbs_cert(KMF_X509_TBS_CERT *tbscert)
118699ebb4caSwyllys {
118799ebb4caSwyllys 	if (tbscert) {
118830a5e8faSwyllys 		kmf_free_data(&tbscert->version);
118930a5e8faSwyllys 		kmf_free_bigint(&tbscert->serialNumber);
119030a5e8faSwyllys 		kmf_free_algoid(&tbscert->signature);
119199ebb4caSwyllys 
119230a5e8faSwyllys 		kmf_free_dn(&tbscert->issuer);
119330a5e8faSwyllys 		kmf_free_dn(&tbscert->subject);
119499ebb4caSwyllys 
119599ebb4caSwyllys 		free_validity(&tbscert->validity);
119699ebb4caSwyllys 
119730a5e8faSwyllys 		kmf_free_data(&tbscert->issuerUniqueIdentifier);
119830a5e8faSwyllys 		kmf_free_data(&tbscert->subjectUniqueIdentifier);
119999ebb4caSwyllys 
120030a5e8faSwyllys 		kmf_free_algoid(&tbscert->subjectPublicKeyInfo.algorithm);
120130a5e8faSwyllys 		kmf_free_data(&tbscert->subjectPublicKeyInfo.subjectPublicKey);
120299ebb4caSwyllys 
120399ebb4caSwyllys 		free_extensions(&tbscert->extensions);
120499ebb4caSwyllys 
120530a5e8faSwyllys 		kmf_free_data(&tbscert->issuerUniqueIdentifier);
120630a5e8faSwyllys 		kmf_free_data(&tbscert->subjectUniqueIdentifier);
120799ebb4caSwyllys 	}
120899ebb4caSwyllys }
120999ebb4caSwyllys 
121099ebb4caSwyllys void
121130a5e8faSwyllys kmf_free_signed_cert(KMF_X509_CERTIFICATE *certptr)
121299ebb4caSwyllys {
121399ebb4caSwyllys 	if (!certptr)
121499ebb4caSwyllys 		return;
121599ebb4caSwyllys 
121630a5e8faSwyllys 	kmf_free_tbs_cert(&certptr->certificate);
121799ebb4caSwyllys 
121830a5e8faSwyllys 	kmf_free_algoid(&certptr->signature.algorithmIdentifier);
121930a5e8faSwyllys 	kmf_free_data(&certptr->signature.encrypted);
122099ebb4caSwyllys }
122199ebb4caSwyllys 
122299ebb4caSwyllys void
122330a5e8faSwyllys kmf_free_str(char *pstr)
122499ebb4caSwyllys {
122599ebb4caSwyllys 	if (pstr != NULL)
122699ebb4caSwyllys 		free(pstr);
122799ebb4caSwyllys }
122899ebb4caSwyllys 
122999ebb4caSwyllys void
123099ebb4caSwyllys free_keyidlist(KMF_OID *oidlist, int len)
123199ebb4caSwyllys {
123299ebb4caSwyllys 	int i;
123399ebb4caSwyllys 	for (i = 0; i < len; i++) {
123430a5e8faSwyllys 		kmf_free_data((KMF_DATA *)&oidlist[i]);
123599ebb4caSwyllys 	}
123699ebb4caSwyllys 	free(oidlist);
123799ebb4caSwyllys }
123899ebb4caSwyllys 
123999ebb4caSwyllys void
124030a5e8faSwyllys kmf_free_eku(KMF_X509EXT_EKU *eptr)
124199ebb4caSwyllys {
124230a5e8faSwyllys 	if (eptr && eptr->nEKUs > 0 && eptr->keyPurposeIdList != NULL)
124399ebb4caSwyllys 		free_keyidlist(eptr->keyPurposeIdList, eptr->nEKUs);
124499ebb4caSwyllys }
124599ebb4caSwyllys 
124699ebb4caSwyllys void
124730a5e8faSwyllys kmf_free_spki(KMF_X509_SPKI *spki)
124899ebb4caSwyllys {
124999ebb4caSwyllys 	if (spki != NULL) {
125030a5e8faSwyllys 		kmf_free_algoid(&spki->algorithm);
125130a5e8faSwyllys 		kmf_free_data(&spki->subjectPublicKey);
125299ebb4caSwyllys 	}
125399ebb4caSwyllys }
125499ebb4caSwyllys 
125599ebb4caSwyllys void
125630a5e8faSwyllys kmf_free_kmf_key(KMF_HANDLE_T handle, KMF_KEY_HANDLE *key)
125799ebb4caSwyllys {
125899ebb4caSwyllys 	KMF_PLUGIN *plugin;
125999ebb4caSwyllys 	KMF_RETURN ret;
126030a5e8faSwyllys 	KMF_ATTRIBUTE attlist[2]; /* only 2 attributes for DeleteKey op */
126130a5e8faSwyllys 	int i = 0;
126230a5e8faSwyllys 	boolean_t token_destroy = B_FALSE;
126330a5e8faSwyllys 
126430a5e8faSwyllys 	if (key == NULL)
126530a5e8faSwyllys 		return;
126699ebb4caSwyllys 
126799ebb4caSwyllys 	CLEAR_ERROR(handle, ret);
126899ebb4caSwyllys 	if (ret != KMF_OK)
126999ebb4caSwyllys 		return;
127099ebb4caSwyllys 
127130a5e8faSwyllys 	kmf_set_attr_at_index(attlist, i,
127230a5e8faSwyllys 	    KMF_KEY_HANDLE_ATTR, key, sizeof (KMF_KEY_HANDLE));
127330a5e8faSwyllys 	i++;
127430a5e8faSwyllys 
127530a5e8faSwyllys 	kmf_set_attr_at_index(attlist, i,
127630a5e8faSwyllys 	    KMF_DESTROY_BOOL_ATTR, &token_destroy, sizeof (boolean_t));
127730a5e8faSwyllys 	i++;
127899ebb4caSwyllys 
127999ebb4caSwyllys 	plugin = FindPlugin(handle, key->kstype);
128099ebb4caSwyllys 	if (plugin != NULL && plugin->funclist->DeleteKey != NULL) {
128130a5e8faSwyllys 		(void) plugin->funclist->DeleteKey(handle, i, attlist);
128299ebb4caSwyllys 	}
128399ebb4caSwyllys 
128499ebb4caSwyllys 	if (key->keylabel)
128599ebb4caSwyllys 		free(key->keylabel);
128699ebb4caSwyllys 
128799ebb4caSwyllys 	if (key->israw) {
128830a5e8faSwyllys 		kmf_free_raw_key(key->keyp);
128999ebb4caSwyllys 		free(key->keyp);
129099ebb4caSwyllys 	}
129199ebb4caSwyllys 
129299ebb4caSwyllys 	(void) memset(key, 0, sizeof (KMF_KEY_HANDLE));
129399ebb4caSwyllys }
129499ebb4caSwyllys 
129599ebb4caSwyllys void
129630a5e8faSwyllys kmf_free_bigint(KMF_BIGINT *big)
129799ebb4caSwyllys {
129899ebb4caSwyllys 	if (big != NULL && big->val != NULL) {
129902744e81Swyllys 		/* Clear it out before returning it to the pool */
130002744e81Swyllys 		(void) memset(big->val, 0x00, big->len);
130199ebb4caSwyllys 		free(big->val);
130299ebb4caSwyllys 		big->val = NULL;
130399ebb4caSwyllys 		big->len = 0;
130499ebb4caSwyllys 	}
130599ebb4caSwyllys }
130699ebb4caSwyllys 
130799ebb4caSwyllys static void
130899ebb4caSwyllys free_raw_rsa(KMF_RAW_RSA_KEY *key)
130999ebb4caSwyllys {
131099ebb4caSwyllys 	if (key == NULL)
131199ebb4caSwyllys 		return;
131230a5e8faSwyllys 	kmf_free_bigint(&key->mod);
131330a5e8faSwyllys 	kmf_free_bigint(&key->pubexp);
131430a5e8faSwyllys 	kmf_free_bigint(&key->priexp);
131530a5e8faSwyllys 	kmf_free_bigint(&key->prime1);
131630a5e8faSwyllys 	kmf_free_bigint(&key->prime2);
131730a5e8faSwyllys 	kmf_free_bigint(&key->exp1);
131830a5e8faSwyllys 	kmf_free_bigint(&key->exp2);
131930a5e8faSwyllys 	kmf_free_bigint(&key->coef);
132099ebb4caSwyllys }
132199ebb4caSwyllys 
132299ebb4caSwyllys static void
132399ebb4caSwyllys free_raw_dsa(KMF_RAW_DSA_KEY *key)
132499ebb4caSwyllys {
132599ebb4caSwyllys 	if (key == NULL)
132699ebb4caSwyllys 		return;
132730a5e8faSwyllys 	kmf_free_bigint(&key->prime);
132830a5e8faSwyllys 	kmf_free_bigint(&key->subprime);
132930a5e8faSwyllys 	kmf_free_bigint(&key->base);
133030a5e8faSwyllys 	kmf_free_bigint(&key->value);
133199ebb4caSwyllys }
133299ebb4caSwyllys 
133399ebb4caSwyllys static void
133499ebb4caSwyllys free_raw_sym(KMF_RAW_SYM_KEY *key)
133599ebb4caSwyllys {
133699ebb4caSwyllys 	if (key == NULL)
133799ebb4caSwyllys 		return;
133830a5e8faSwyllys 	kmf_free_bigint(&key->keydata);
133999ebb4caSwyllys }
134099ebb4caSwyllys 
134199ebb4caSwyllys void
134230a5e8faSwyllys kmf_free_raw_key(KMF_RAW_KEY_DATA *key)
134399ebb4caSwyllys {
134499ebb4caSwyllys 	if (key == NULL)
134599ebb4caSwyllys 		return;
134699ebb4caSwyllys 
134799ebb4caSwyllys 	switch (key->keytype) {
134899ebb4caSwyllys 	case KMF_RSA:
134999ebb4caSwyllys 		free_raw_rsa(&key->rawdata.rsa);
135099ebb4caSwyllys 		break;
135199ebb4caSwyllys 	case KMF_DSA:
135299ebb4caSwyllys 		free_raw_dsa(&key->rawdata.dsa);
135399ebb4caSwyllys 		break;
135499ebb4caSwyllys 	case KMF_AES:
135599ebb4caSwyllys 	case KMF_RC4:
135699ebb4caSwyllys 	case KMF_DES:
135799ebb4caSwyllys 	case KMF_DES3:
135899ebb4caSwyllys 		free_raw_sym(&key->rawdata.sym);
135999ebb4caSwyllys 		break;
136099ebb4caSwyllys 	}
13615b3e1433Swyllys 	if (key->label) {
13625b3e1433Swyllys 		free(key->label);
13635b3e1433Swyllys 		key->label = NULL;
13645b3e1433Swyllys 	}
13655b3e1433Swyllys 	kmf_free_data(&key->id);
136699ebb4caSwyllys }
136799ebb4caSwyllys 
136899ebb4caSwyllys void
136930a5e8faSwyllys kmf_free_raw_sym_key(KMF_RAW_SYM_KEY *key)
137099ebb4caSwyllys {
137199ebb4caSwyllys 	if (key == NULL)
137299ebb4caSwyllys 		return;
137330a5e8faSwyllys 	kmf_free_bigint(&key->keydata);
137499ebb4caSwyllys 	free(key);
137599ebb4caSwyllys }
137699ebb4caSwyllys 
137799ebb4caSwyllys /*
137899ebb4caSwyllys  * This function frees the space allocated for the name portion of a
137999ebb4caSwyllys  * KMF_CRL_DIST_POINT.
138099ebb4caSwyllys  */
138199ebb4caSwyllys void
138299ebb4caSwyllys free_dp_name(KMF_CRL_DIST_POINT *dp)
138399ebb4caSwyllys {
138499ebb4caSwyllys 	KMF_GENERALNAMES *fullname;
138599ebb4caSwyllys 	KMF_DATA *urldata;
138699ebb4caSwyllys 	int i;
138799ebb4caSwyllys 
138899ebb4caSwyllys 	if (dp == NULL)
138999ebb4caSwyllys 		return;
139099ebb4caSwyllys 
139199ebb4caSwyllys 	/* For phase 1, we only need to free the fullname space. */
139299ebb4caSwyllys 	fullname = &(dp->name.full_name);
139399ebb4caSwyllys 	if (fullname->number == 0)
139499ebb4caSwyllys 		return;
139599ebb4caSwyllys 
139699ebb4caSwyllys 	for (i = 0; i < fullname->number; i++) {
139799ebb4caSwyllys 		urldata = &(fullname->namelist[fullname->number - 1].name);
139830a5e8faSwyllys 		kmf_free_data(urldata);
139999ebb4caSwyllys 	}
140099ebb4caSwyllys 
140199ebb4caSwyllys 	free(fullname->namelist);
140299ebb4caSwyllys }
140399ebb4caSwyllys 
140499ebb4caSwyllys /*
140599ebb4caSwyllys  * This function frees the space allocated for a KMF_CRL_DIST_POINT.
140699ebb4caSwyllys  */
140799ebb4caSwyllys void
140899ebb4caSwyllys free_dp(KMF_CRL_DIST_POINT *dp)
140999ebb4caSwyllys {
141099ebb4caSwyllys 	if (dp == NULL)
141199ebb4caSwyllys 		return;
141299ebb4caSwyllys 
141399ebb4caSwyllys 	free_dp_name(dp);
141430a5e8faSwyllys 	kmf_free_data(&(dp->reasons));
141599ebb4caSwyllys 	/* Need not to free crl_issuer space at phase 1 */
141699ebb4caSwyllys }
141799ebb4caSwyllys 
141899ebb4caSwyllys /*
141999ebb4caSwyllys  * This function frees space for a KMF_X509EXT_CRLDISTPOINTS internally.
142099ebb4caSwyllys  */
142199ebb4caSwyllys void
142230a5e8faSwyllys kmf_free_crl_dist_pts(KMF_X509EXT_CRLDISTPOINTS *crl_dps)
142399ebb4caSwyllys {
142499ebb4caSwyllys 	int i;
142599ebb4caSwyllys 
142699ebb4caSwyllys 	if (crl_dps == NULL)
142799ebb4caSwyllys 		return;
142899ebb4caSwyllys 
142999ebb4caSwyllys 	for (i = 0; i < crl_dps->number; i++)
143099ebb4caSwyllys 		free_dp(&(crl_dps->dplist[i]));
143199ebb4caSwyllys 
143299ebb4caSwyllys 	free(crl_dps->dplist);
143399ebb4caSwyllys }
143499ebb4caSwyllys 
143599ebb4caSwyllys KMF_RETURN
143630a5e8faSwyllys kmf_create_ocsp_request(KMF_HANDLE_T handle,
143730a5e8faSwyllys 	int	num_args,
143830a5e8faSwyllys 	KMF_ATTRIBUTE	*attrlist)
143999ebb4caSwyllys {
144030a5e8faSwyllys 	KMF_RETURN ret = KMF_OK;
144199ebb4caSwyllys 	KMF_PLUGIN *plugin;
144230a5e8faSwyllys 	KMF_RETURN (*createReqFn)(void *, int num_args,
144330a5e8faSwyllys 	    KMF_ATTRIBUTE *attrlist);
144499ebb4caSwyllys 
144530a5e8faSwyllys 	KMF_ATTRIBUTE_TESTER required_attrs[] = {
144630a5e8faSwyllys 		{KMF_OCSP_REQUEST_FILENAME_ATTR, FALSE, 1, 0},
144730a5e8faSwyllys 		{KMF_USER_CERT_DATA_ATTR, FALSE, sizeof (KMF_DATA),
144830a5e8faSwyllys 			sizeof (KMF_DATA)},
144930a5e8faSwyllys 		{KMF_ISSUER_CERT_DATA_ATTR, FALSE, sizeof (KMF_DATA),
145030a5e8faSwyllys 			sizeof (KMF_DATA)},
145130a5e8faSwyllys 	};
145299ebb4caSwyllys 
145330a5e8faSwyllys 	int num_req_attrs = sizeof (required_attrs) /
145430a5e8faSwyllys 	    sizeof (KMF_ATTRIBUTE_TESTER);
145599ebb4caSwyllys 
145630a5e8faSwyllys 	if (handle == NULL)
145799ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
145899ebb4caSwyllys 
145930a5e8faSwyllys 	CLEAR_ERROR(handle, ret);
146030a5e8faSwyllys 
146130a5e8faSwyllys 	ret = test_attributes(num_req_attrs, required_attrs,
146230a5e8faSwyllys 	    0, NULL, num_args, attrlist);
146330a5e8faSwyllys 
146430a5e8faSwyllys 	if (ret != KMF_OK)
146530a5e8faSwyllys 		return (ret);
146630a5e8faSwyllys 
146799ebb4caSwyllys 	/*
146899ebb4caSwyllys 	 * This framework function is actually implemented in the openssl
146999ebb4caSwyllys 	 * plugin library, so we find the function address and call it.
147099ebb4caSwyllys 	 */
147199ebb4caSwyllys 	plugin = FindPlugin(handle, KMF_KEYSTORE_OPENSSL);
147299ebb4caSwyllys 	if (plugin == NULL || plugin->dldesc == NULL) {
147399ebb4caSwyllys 		return (KMF_ERR_PLUGIN_NOTFOUND);
147499ebb4caSwyllys 	}
147599ebb4caSwyllys 
147699ebb4caSwyllys 	createReqFn = (KMF_RETURN(*)())dlsym(plugin->dldesc,
147799ebb4caSwyllys 	    "OpenSSL_CreateOCSPRequest");
147899ebb4caSwyllys 	if (createReqFn == NULL) {
147999ebb4caSwyllys 		return (KMF_ERR_FUNCTION_NOT_FOUND);
148099ebb4caSwyllys 	}
148199ebb4caSwyllys 
148230a5e8faSwyllys 	return (createReqFn(handle, num_args, attrlist));
148330a5e8faSwyllys 
148499ebb4caSwyllys }
148599ebb4caSwyllys 
148699ebb4caSwyllys KMF_RETURN
148730a5e8faSwyllys kmf_get_ocsp_status_for_cert(KMF_HANDLE_T handle,
148830a5e8faSwyllys 	int	num_args,
148930a5e8faSwyllys 	KMF_ATTRIBUTE	*attrlist)
149099ebb4caSwyllys {
149130a5e8faSwyllys 	KMF_RETURN ret = KMF_OK;
149299ebb4caSwyllys 	KMF_PLUGIN *plugin;
149330a5e8faSwyllys 	KMF_RETURN (*getCertStatusFn)(void *, int num_args,
149430a5e8faSwyllys 	    KMF_ATTRIBUTE *attrlist);
149530a5e8faSwyllys 
149630a5e8faSwyllys 	KMF_ATTRIBUTE_TESTER required_attrs[] = {
149730a5e8faSwyllys 		{KMF_USER_CERT_DATA_ATTR, FALSE, sizeof (KMF_DATA),
149830a5e8faSwyllys 			sizeof (KMF_DATA)},
149930a5e8faSwyllys 		{KMF_ISSUER_CERT_DATA_ATTR, FALSE, sizeof (KMF_DATA),
150030a5e8faSwyllys 			sizeof (KMF_DATA)},
150130a5e8faSwyllys 		{KMF_OCSP_RESPONSE_DATA_ATTR, FALSE, sizeof (KMF_DATA),
150230a5e8faSwyllys 			sizeof (KMF_DATA)},
150330a5e8faSwyllys 		{KMF_OCSP_RESPONSE_STATUS_ATTR, FALSE, sizeof (int),
150430a5e8faSwyllys 			sizeof (uint32_t)},
150530a5e8faSwyllys 		{KMF_OCSP_RESPONSE_REASON_ATTR, FALSE, sizeof (int),
150630a5e8faSwyllys 			sizeof (uint32_t)},
150730a5e8faSwyllys 		{KMF_OCSP_RESPONSE_CERT_STATUS_ATTR, FALSE, sizeof (int),
150830a5e8faSwyllys 			sizeof (uint32_t)},
150930a5e8faSwyllys 	};
151030a5e8faSwyllys 
151130a5e8faSwyllys 	int num_req_attrs = sizeof (required_attrs) /
151230a5e8faSwyllys 	    sizeof (KMF_ATTRIBUTE_TESTER);
151330a5e8faSwyllys 
151430a5e8faSwyllys 	if (handle == NULL)
151530a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
151699ebb4caSwyllys 
151799ebb4caSwyllys 	CLEAR_ERROR(handle, ret);
151899ebb4caSwyllys 
151930a5e8faSwyllys 	ret = test_attributes(num_req_attrs, required_attrs,
152030a5e8faSwyllys 	    0, NULL, num_args, attrlist);
152199ebb4caSwyllys 
152230a5e8faSwyllys 	if (ret != KMF_OK)
152330a5e8faSwyllys 		return (ret);
152499ebb4caSwyllys 
152599ebb4caSwyllys 	/*
152699ebb4caSwyllys 	 * This framework function is actually implemented in the openssl
152799ebb4caSwyllys 	 * plugin library, so we find the function address and call it.
152899ebb4caSwyllys 	 */
152999ebb4caSwyllys 	plugin = FindPlugin(handle, KMF_KEYSTORE_OPENSSL);
153099ebb4caSwyllys 	if (plugin == NULL || plugin->dldesc == NULL) {
153199ebb4caSwyllys 		return (KMF_ERR_INTERNAL);
153299ebb4caSwyllys 	}
153399ebb4caSwyllys 
153499ebb4caSwyllys 	getCertStatusFn = (KMF_RETURN(*)())dlsym(plugin->dldesc,
153599ebb4caSwyllys 	    "OpenSSL_GetOCSPStatusForCert");
153699ebb4caSwyllys 	if (getCertStatusFn == NULL) {
153799ebb4caSwyllys 		return (KMF_ERR_INTERNAL);
153899ebb4caSwyllys 	}
153999ebb4caSwyllys 
154030a5e8faSwyllys 	return (getCertStatusFn(handle, num_args, attrlist));
154130a5e8faSwyllys 
154299ebb4caSwyllys }
154399ebb4caSwyllys 
154499ebb4caSwyllys KMF_RETURN
154530a5e8faSwyllys kmf_string_to_oid(char *oidstring, KMF_OID *oid)
154699ebb4caSwyllys {
154799ebb4caSwyllys 	KMF_RETURN rv = KMF_OK;
154899ebb4caSwyllys 	char *cp, *bp, *startp;
154999ebb4caSwyllys 	int numbuf;
155099ebb4caSwyllys 	int onumbuf;
155199ebb4caSwyllys 	int nbytes, index;
155299ebb4caSwyllys 	int len;
155399ebb4caSwyllys 	unsigned char *op;
155499ebb4caSwyllys 
155599ebb4caSwyllys 	if (oidstring == NULL || oid == NULL)
155699ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
155799ebb4caSwyllys 
155899ebb4caSwyllys 	len = strlen(oidstring);
155999ebb4caSwyllys 
156099ebb4caSwyllys 	bp = oidstring;
156199ebb4caSwyllys 	cp = bp;
156299ebb4caSwyllys 	/* Skip over leading space */
156399ebb4caSwyllys 	while ((bp < &cp[len]) && isspace(*bp))
156499ebb4caSwyllys 		bp++;
156599ebb4caSwyllys 
156699ebb4caSwyllys 	startp = bp;
156799ebb4caSwyllys 	nbytes = 0;
156899ebb4caSwyllys 
156999ebb4caSwyllys 	/*
157099ebb4caSwyllys 	 * The first two numbers are chewed up by the first octet.
157199ebb4caSwyllys 	 */
157299ebb4caSwyllys 	if (sscanf(bp, "%d", &numbuf) != 1)
157399ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
157499ebb4caSwyllys 	while ((bp < &cp[len]) && isdigit(*bp))
157599ebb4caSwyllys 		bp++;
157699ebb4caSwyllys 	while ((bp < &cp[len]) && (isspace(*bp) || *bp == '.'))
157799ebb4caSwyllys 		bp++;
157899ebb4caSwyllys 	if (sscanf(bp, "%d", &numbuf) != 1)
157999ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
158099ebb4caSwyllys 	while ((bp < &cp[len]) && isdigit(*bp))
158199ebb4caSwyllys 		bp++;
158299ebb4caSwyllys 	while ((bp < &cp[len]) && (isspace(*bp) || *bp == '.'))
158399ebb4caSwyllys 		bp++;
158499ebb4caSwyllys 	nbytes++;
158599ebb4caSwyllys 
158699ebb4caSwyllys 	while (isdigit(*bp)) {
158799ebb4caSwyllys 		if (sscanf(bp, "%d", &numbuf) != 1)
158899ebb4caSwyllys 			return (KMF_ERR_BAD_PARAMETER);
158999ebb4caSwyllys 		while (numbuf) {
159099ebb4caSwyllys 			nbytes++;
159199ebb4caSwyllys 			numbuf >>= 7;
159299ebb4caSwyllys 		}
159399ebb4caSwyllys 		while ((bp < &cp[len]) && isdigit(*bp))
159499ebb4caSwyllys 			bp++;
159599ebb4caSwyllys 		while ((bp < &cp[len]) && (isspace(*bp) || *bp == '.'))
159699ebb4caSwyllys 			bp++;
159799ebb4caSwyllys 	}
159899ebb4caSwyllys 
159999ebb4caSwyllys 	oid->Length = nbytes;
160099ebb4caSwyllys 	oid->Data = malloc(oid->Length);
160199ebb4caSwyllys 	if (oid->Data == NULL) {
160299ebb4caSwyllys 		return (KMF_ERR_MEMORY);
160399ebb4caSwyllys 	}
160499ebb4caSwyllys 	(void) memset(oid->Data, 0, oid->Length);
160599ebb4caSwyllys 
160699ebb4caSwyllys 	op = oid->Data;
160799ebb4caSwyllys 
160899ebb4caSwyllys 	bp = startp;
160999ebb4caSwyllys 	(void) sscanf(bp, "%d", &numbuf);
161099ebb4caSwyllys 
161199ebb4caSwyllys 	while (isdigit(*bp)) bp++;
161299ebb4caSwyllys 	while (isspace(*bp) || *bp == '.') bp++;
161399ebb4caSwyllys 
161499ebb4caSwyllys 	onumbuf = 40 * numbuf;
161599ebb4caSwyllys 	(void) sscanf(bp, "%d", &numbuf);
161699ebb4caSwyllys 	onumbuf += numbuf;
161799ebb4caSwyllys 	*op = (unsigned char) onumbuf;
161899ebb4caSwyllys 	op++;
161999ebb4caSwyllys 
162099ebb4caSwyllys 	while (isdigit(*bp)) bp++;
162199ebb4caSwyllys 	while (isspace(*bp) || *bp == '.') bp++;
162299ebb4caSwyllys 	while (isdigit(*bp)) {
162399ebb4caSwyllys 		(void) sscanf(bp, "%d", &numbuf);
162499ebb4caSwyllys 		nbytes = 0;
162599ebb4caSwyllys 		/* Have to fill in the bytes msb-first */
162699ebb4caSwyllys 		onumbuf = numbuf;
162799ebb4caSwyllys 		while (numbuf) {
162899ebb4caSwyllys 			nbytes++;
162999ebb4caSwyllys 			numbuf >>= 7;
163099ebb4caSwyllys 		}
163199ebb4caSwyllys 		numbuf = onumbuf;
163299ebb4caSwyllys 		op += nbytes;
163399ebb4caSwyllys 		index = -1;
163499ebb4caSwyllys 		while (numbuf) {
163599ebb4caSwyllys 			op[index] = (unsigned char)numbuf & 0x7f;
163699ebb4caSwyllys 			if (index != -1)
163799ebb4caSwyllys 				op[index] |= 0x80;
163899ebb4caSwyllys 			index--;
163999ebb4caSwyllys 			numbuf >>= 7;
164099ebb4caSwyllys 		}
164199ebb4caSwyllys 		while (isdigit(*bp)) bp++;
164299ebb4caSwyllys 		while (isspace(*bp) || *bp == '.') bp++;
164399ebb4caSwyllys 	}
164499ebb4caSwyllys 
164599ebb4caSwyllys 	return (rv);
164699ebb4caSwyllys }
164799ebb4caSwyllys 
164899ebb4caSwyllys static KMF_RETURN
164999ebb4caSwyllys encode_rid(char *name, KMF_DATA *derdata)
165099ebb4caSwyllys {
165199ebb4caSwyllys 	KMF_RETURN rv = KMF_OK;
165299ebb4caSwyllys 
165399ebb4caSwyllys 	if (name == NULL || derdata == NULL)
165499ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
165599ebb4caSwyllys 
165630a5e8faSwyllys 	rv = kmf_string_to_oid(name, (KMF_OID *)derdata);
165799ebb4caSwyllys 
165899ebb4caSwyllys 	return (rv);
165999ebb4caSwyllys }
166099ebb4caSwyllys 
166199ebb4caSwyllys static KMF_RETURN
166299ebb4caSwyllys encode_ipaddr(char *name, KMF_DATA *derdata)
166399ebb4caSwyllys {
166499ebb4caSwyllys 	KMF_RETURN rv = KMF_OK;
166599ebb4caSwyllys 	size_t len;
166699ebb4caSwyllys 	in_addr_t v4;
166799ebb4caSwyllys 	in6_addr_t v6;
166899ebb4caSwyllys 	uint8_t *ptr;
166999ebb4caSwyllys 
167099ebb4caSwyllys 	if (name == NULL || derdata == NULL)
167199ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
167299ebb4caSwyllys 
167399ebb4caSwyllys 	v4 = inet_addr(name);
167499ebb4caSwyllys 	if (v4 == (in_addr_t)-1) {
167599ebb4caSwyllys 		ptr = (uint8_t *)&v6;
167699ebb4caSwyllys 		if (inet_pton(AF_INET6, name, ptr) != 1)
167799ebb4caSwyllys 			return (KMF_ERR_ENCODING);
167899ebb4caSwyllys 		len = sizeof (v6);
167999ebb4caSwyllys 	} else {
168099ebb4caSwyllys 		ptr = (uint8_t *)&v4;
168199ebb4caSwyllys 		len = sizeof (v4);
168299ebb4caSwyllys 	}
168399ebb4caSwyllys 
168499ebb4caSwyllys 	derdata->Data = malloc(len);
168599ebb4caSwyllys 	if (derdata->Data == NULL)
168699ebb4caSwyllys 		return (KMF_ERR_MEMORY);
168799ebb4caSwyllys 	(void) memcpy(derdata->Data, ptr, len);
168899ebb4caSwyllys 	derdata->Length = len;
168999ebb4caSwyllys 
169099ebb4caSwyllys 	return (rv);
169199ebb4caSwyllys }
169299ebb4caSwyllys 
1693*d00756ccSwyllys static KMF_RETURN
1694*d00756ccSwyllys encode_krb5(char *name, KMF_DATA *derdata)
1695*d00756ccSwyllys {
1696*d00756ccSwyllys 	KMF_RETURN rv = KMF_OK;
1697*d00756ccSwyllys 	char *at, *realm;
1698*d00756ccSwyllys 	BerElement *asn1 = NULL;
1699*d00756ccSwyllys 	BerValue *extdata = NULL;
1700*d00756ccSwyllys 
1701*d00756ccSwyllys 	at = strchr(name, '@');
1702*d00756ccSwyllys 	if (at == NULL)
1703*d00756ccSwyllys 		return (KMF_ERR_ENCODING);
1704*d00756ccSwyllys 
1705*d00756ccSwyllys 	realm = at+1;
1706*d00756ccSwyllys 	*at = 0;
1707*d00756ccSwyllys 
1708*d00756ccSwyllys 	if ((asn1 = kmfder_alloc()) == NULL)
1709*d00756ccSwyllys 		return (KMF_ERR_MEMORY);
1710*d00756ccSwyllys 
1711*d00756ccSwyllys 	if (kmfber_printf(asn1, "{D{", &KMFOID_PKINIT_san) == -1)
1712*d00756ccSwyllys 		goto cleanup;
1713*d00756ccSwyllys 
1714*d00756ccSwyllys 	if (kmfber_printf(asn1, "l", strlen(realm)) == -1)
1715*d00756ccSwyllys 		goto cleanup;
1716*d00756ccSwyllys 	if (kmfber_write(asn1, realm, strlen(realm), 0) != strlen(realm))
1717*d00756ccSwyllys 		goto cleanup;
1718*d00756ccSwyllys 	if (kmfber_printf(asn1, "l", strlen(name)) == -1)
1719*d00756ccSwyllys 		goto cleanup;
1720*d00756ccSwyllys 	if (kmfber_write(asn1, name, strlen(name), 0) != strlen(name))
1721*d00756ccSwyllys 		goto cleanup;
1722*d00756ccSwyllys 	if (kmfber_printf(asn1, "}}") == -1)
1723*d00756ccSwyllys 		goto cleanup;
1724*d00756ccSwyllys 
1725*d00756ccSwyllys 	if (kmfber_flatten(asn1, &extdata) == -1) {
1726*d00756ccSwyllys 		rv = KMF_ERR_ENCODING;
1727*d00756ccSwyllys 		goto cleanup;
1728*d00756ccSwyllys 	}
1729*d00756ccSwyllys 
1730*d00756ccSwyllys 	derdata->Data = (uchar_t *)extdata->bv_val;
1731*d00756ccSwyllys 	derdata->Length = extdata->bv_len;
1732*d00756ccSwyllys 
1733*d00756ccSwyllys 	free(extdata);
1734*d00756ccSwyllys cleanup:
1735*d00756ccSwyllys 	if (asn1 != NULL)
1736*d00756ccSwyllys 		kmfber_free(asn1, 1);
1737*d00756ccSwyllys 
1738*d00756ccSwyllys 	if (*at == 0)
1739*d00756ccSwyllys 		*at = '@';
1740*d00756ccSwyllys 
1741*d00756ccSwyllys 	return (rv);
1742*d00756ccSwyllys }
1743*d00756ccSwyllys 
1744*d00756ccSwyllys static KMF_RETURN
1745*d00756ccSwyllys encode_sclogon(char *name, KMF_DATA *derdata)
1746*d00756ccSwyllys {
1747*d00756ccSwyllys 	KMF_RETURN rv = KMF_OK;
1748*d00756ccSwyllys 	BerElement *asn1 = NULL;
1749*d00756ccSwyllys 	BerValue *extdata = NULL;
1750*d00756ccSwyllys 
1751*d00756ccSwyllys 	if ((asn1 = kmfder_alloc()) == NULL)
1752*d00756ccSwyllys 		return (KMF_ERR_MEMORY);
1753*d00756ccSwyllys 
1754*d00756ccSwyllys 	/* The name is encoded as a KerberosString (IA5STRING) */
1755*d00756ccSwyllys 	if (kmfber_printf(asn1, "{Ds}",
1756*d00756ccSwyllys 	    &KMFOID_MS_KP_SCLogon, name) == -1)
1757*d00756ccSwyllys 		goto cleanup;
1758*d00756ccSwyllys 
1759*d00756ccSwyllys 	if (kmfber_flatten(asn1, &extdata) == -1) {
1760*d00756ccSwyllys 		rv = KMF_ERR_ENCODING;
1761*d00756ccSwyllys 		goto cleanup;
1762*d00756ccSwyllys 	}
1763*d00756ccSwyllys 
1764*d00756ccSwyllys 	derdata->Data = (uchar_t *)extdata->bv_val;
1765*d00756ccSwyllys 	derdata->Length = extdata->bv_len;
1766*d00756ccSwyllys 
1767*d00756ccSwyllys 	free(extdata);
1768*d00756ccSwyllys cleanup:
1769*d00756ccSwyllys 	if (asn1 != NULL)
1770*d00756ccSwyllys 		kmfber_free(asn1, 1);
1771*d00756ccSwyllys 
1772*d00756ccSwyllys 	return (rv);
1773*d00756ccSwyllys }
1774*d00756ccSwyllys 
177599ebb4caSwyllys static KMF_RETURN
177699ebb4caSwyllys verify_uri_format(char *uristring)
177799ebb4caSwyllys {
177899ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
177999ebb4caSwyllys 	xmlURIPtr   uriptr = NULL;
178099ebb4caSwyllys 
178199ebb4caSwyllys 	/* Parse the URI string; get the hostname and port */
178299ebb4caSwyllys 	uriptr = xmlParseURI(uristring);
178399ebb4caSwyllys 	if (uriptr == NULL) {
178499ebb4caSwyllys 		ret = KMF_ERR_BAD_URI;
178599ebb4caSwyllys 		goto out;
178699ebb4caSwyllys 	}
178799ebb4caSwyllys 
178899ebb4caSwyllys 	if (uriptr->scheme == NULL || !strlen(uriptr->scheme)) {
178999ebb4caSwyllys 		ret = KMF_ERR_BAD_URI;
179099ebb4caSwyllys 		goto out;
179199ebb4caSwyllys 	}
179299ebb4caSwyllys 
179399ebb4caSwyllys 	if (uriptr->server == NULL || !strlen(uriptr->server)) {
179499ebb4caSwyllys 		ret = KMF_ERR_BAD_URI;
179599ebb4caSwyllys 		goto out;
179699ebb4caSwyllys 	}
179799ebb4caSwyllys out:
179899ebb4caSwyllys 	if (uriptr != NULL)
179999ebb4caSwyllys 		xmlFreeURI(uriptr);
180099ebb4caSwyllys 	return (ret);
180199ebb4caSwyllys }
180299ebb4caSwyllys 
180399ebb4caSwyllys static KMF_RETURN
180499ebb4caSwyllys encode_altname(char *namedata,
180599ebb4caSwyllys 	KMF_GENERALNAMECHOICES nametype, KMF_DATA *encodedname)
180699ebb4caSwyllys {
180799ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
180899ebb4caSwyllys 	KMF_X509_NAME dnname;
180999ebb4caSwyllys 	uchar_t tagval;
181099ebb4caSwyllys 	BerElement *asn1 = NULL;
181199ebb4caSwyllys 	BerValue *extdata;
181299ebb4caSwyllys 
181399ebb4caSwyllys 	if (namedata == NULL || encodedname == NULL)
181499ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
181599ebb4caSwyllys 
181699ebb4caSwyllys 	/*
181799ebb4caSwyllys 	 * Encode the namedata according to rules in RFC 3280 for GeneralName.
181899ebb4caSwyllys 	 * The input "namedata" is assumed to be an ASCII string representation
181999ebb4caSwyllys 	 * of the AltName, we need to convert it to correct ASN.1 here before
182099ebb4caSwyllys 	 * adding it to the cert.
182199ebb4caSwyllys 	 */
182299ebb4caSwyllys 	switch (nametype) {
182399ebb4caSwyllys 		case GENNAME_RFC822NAME: /* rfc 822 */
182499ebb4caSwyllys 			/* IA5String, no encoding needed */
182599ebb4caSwyllys 			encodedname->Data = (uchar_t *)strdup(namedata);
182699ebb4caSwyllys 			if (encodedname->Data == NULL)
182799ebb4caSwyllys 				return (KMF_ERR_MEMORY);
182899ebb4caSwyllys 			encodedname->Length = strlen(namedata);
182999ebb4caSwyllys 			tagval = (0x80 | nametype);
183099ebb4caSwyllys 			break;
183199ebb4caSwyllys 		case GENNAME_DNSNAME: /* rfc 1034 */
183299ebb4caSwyllys 			encodedname->Data = (uchar_t *)strdup(namedata);
183399ebb4caSwyllys 			if (encodedname->Data == NULL)
183499ebb4caSwyllys 				return (KMF_ERR_MEMORY);
183599ebb4caSwyllys 			encodedname->Length = strlen(namedata);
183699ebb4caSwyllys 			tagval = (0x80 | nametype);
183799ebb4caSwyllys 			break;
183899ebb4caSwyllys 		case GENNAME_URI: /* rfc 1738 */
183999ebb4caSwyllys 			ret = verify_uri_format(namedata);
184099ebb4caSwyllys 			if (ret != KMF_OK)
184199ebb4caSwyllys 				return (ret);
184299ebb4caSwyllys 			/* IA5String, no encoding needed */
184399ebb4caSwyllys 			encodedname->Data = (uchar_t *)strdup(namedata);
184499ebb4caSwyllys 			if (encodedname->Data == NULL)
184599ebb4caSwyllys 				return (KMF_ERR_MEMORY);
184699ebb4caSwyllys 			encodedname->Length = strlen(namedata);
184799ebb4caSwyllys 			tagval = (0x80 | nametype);
184899ebb4caSwyllys 			break;
184999ebb4caSwyllys 		case GENNAME_IPADDRESS:
185099ebb4caSwyllys 			ret =  encode_ipaddr(namedata, encodedname);
185199ebb4caSwyllys 			tagval = (0x80 | nametype);
185299ebb4caSwyllys 			break;
185399ebb4caSwyllys 		case GENNAME_REGISTEREDID:
185499ebb4caSwyllys 			ret = encode_rid(namedata, encodedname);
185599ebb4caSwyllys 			tagval = (0x80 | nametype);
185699ebb4caSwyllys 			break;
185799ebb4caSwyllys 		case GENNAME_DIRECTORYNAME:
185830a5e8faSwyllys 			ret = kmf_dn_parser(namedata, &dnname);
185999ebb4caSwyllys 			if (ret == KMF_OK) {
186030a5e8faSwyllys 				ret = DerEncodeName(&dnname, encodedname);
186199ebb4caSwyllys 			}
186230a5e8faSwyllys 			(void) kmf_free_dn(&dnname);
186399ebb4caSwyllys 			tagval = (0xA0 | nametype);
186499ebb4caSwyllys 			break;
1865*d00756ccSwyllys 		case GENNAME_KRB5PRINC:
1866*d00756ccSwyllys 			tagval = (0x80 | GENNAME_OTHERNAME);
1867*d00756ccSwyllys 			ret = encode_krb5(namedata, encodedname);
1868*d00756ccSwyllys 			break;
1869*d00756ccSwyllys 		case GENNAME_SCLOGON_UPN:
1870*d00756ccSwyllys 			tagval = (0x80 | GENNAME_OTHERNAME);
1871*d00756ccSwyllys 			ret = encode_sclogon(namedata, encodedname);
1872*d00756ccSwyllys 			break;
187399ebb4caSwyllys 		default:
187499ebb4caSwyllys 			/* unsupported */
187599ebb4caSwyllys 			return (KMF_ERR_BAD_PARAMETER);
187699ebb4caSwyllys 
187799ebb4caSwyllys 	}
187899ebb4caSwyllys 	if (ret != KMF_OK) {
187930a5e8faSwyllys 		kmf_free_data(encodedname);
188099ebb4caSwyllys 		return (ret);
188199ebb4caSwyllys 	}
188299ebb4caSwyllys 
188399ebb4caSwyllys 	if ((asn1 = kmfder_alloc()) == NULL)
188499ebb4caSwyllys 		return (KMF_ERR_MEMORY);
188599ebb4caSwyllys 
188630a5e8faSwyllys 	if (kmfber_printf(asn1, "Tl", tagval, encodedname->Length) == -1)
188799ebb4caSwyllys 		goto cleanup;
188899ebb4caSwyllys 
188999ebb4caSwyllys 	if (kmfber_write(asn1, (char *)encodedname->Data,
189030a5e8faSwyllys 	    encodedname->Length, 0) == -1) {
189199ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
189299ebb4caSwyllys 		goto cleanup;
189399ebb4caSwyllys 	}
189499ebb4caSwyllys 	if (kmfber_flatten(asn1, &extdata) == -1) {
189599ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
189699ebb4caSwyllys 		goto cleanup;
189799ebb4caSwyllys 	}
189899ebb4caSwyllys 
189930a5e8faSwyllys 	kmf_free_data(encodedname);
190099ebb4caSwyllys 	encodedname->Data = (uchar_t *)extdata->bv_val;
190199ebb4caSwyllys 	encodedname->Length = extdata->bv_len;
190299ebb4caSwyllys 
190399ebb4caSwyllys 	free(extdata);
190499ebb4caSwyllys 
190599ebb4caSwyllys cleanup:
190699ebb4caSwyllys 	if (asn1)
190799ebb4caSwyllys 		kmfber_free(asn1, 1);
190899ebb4caSwyllys 
190999ebb4caSwyllys 	if (ret != KMF_OK)
191030a5e8faSwyllys 		kmf_free_data(encodedname);
191199ebb4caSwyllys 
191299ebb4caSwyllys 	return (ret);
191399ebb4caSwyllys }
191499ebb4caSwyllys 
191599ebb4caSwyllys KMF_X509_EXTENSION *
191699ebb4caSwyllys FindExtn(KMF_X509_EXTENSIONS *exts, KMF_OID *oid)
191799ebb4caSwyllys {
191899ebb4caSwyllys 	KMF_X509_EXTENSION *foundextn = NULL;
191999ebb4caSwyllys 	int i;
192099ebb4caSwyllys 
192199ebb4caSwyllys 	if (exts == NULL || oid == NULL)
192299ebb4caSwyllys 		return (NULL);
192399ebb4caSwyllys 
192499ebb4caSwyllys 	for (i = 0; i < exts->numberOfExtensions; i++) {
192599ebb4caSwyllys 		if (IsEqualOid(oid, &exts->extensions[i].extnId))  {
192699ebb4caSwyllys 			foundextn = &exts->extensions[i];
192799ebb4caSwyllys 			break;
192899ebb4caSwyllys 		}
192999ebb4caSwyllys 	}
193099ebb4caSwyllys 	return (foundextn);
193199ebb4caSwyllys }
193299ebb4caSwyllys 
193399ebb4caSwyllys KMF_RETURN
193499ebb4caSwyllys GetSequenceContents(char *data, size_t len,
193599ebb4caSwyllys 	char **contents, size_t *outlen)
193699ebb4caSwyllys {
193799ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
193899ebb4caSwyllys 	BerElement *exasn1 = NULL;
193999ebb4caSwyllys 	BerValue oldextn;
194099ebb4caSwyllys 	int tag;
194199ebb4caSwyllys 	size_t oldsize;
194299ebb4caSwyllys 	char *olddata = NULL;
194399ebb4caSwyllys 
194499ebb4caSwyllys 	if (data == NULL || contents == NULL || outlen == NULL)
194599ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
194699ebb4caSwyllys 
194799ebb4caSwyllys 	/*
194899ebb4caSwyllys 	 * Decode the sequence of general names
194999ebb4caSwyllys 	 */
195099ebb4caSwyllys 	oldextn.bv_val = data;
195199ebb4caSwyllys 	oldextn.bv_len = len;
195299ebb4caSwyllys 
195399ebb4caSwyllys 	if ((exasn1 = kmfder_init(&oldextn)) == NULL) {
195499ebb4caSwyllys 		ret = KMF_ERR_MEMORY;
195599ebb4caSwyllys 		goto out;
195699ebb4caSwyllys 	}
195799ebb4caSwyllys 
195899ebb4caSwyllys 	/*
195999ebb4caSwyllys 	 * Unwrap the sequence to find the size of the block
196099ebb4caSwyllys 	 * of GeneralName items in the set.
196199ebb4caSwyllys 	 *
196299ebb4caSwyllys 	 * Peek at the tag and length ("tl"),
196399ebb4caSwyllys 	 * then consume them ("{").
196499ebb4caSwyllys 	 */
196599ebb4caSwyllys 	if (kmfber_scanf(exasn1, "tl{", &tag, &oldsize) == KMFBER_DEFAULT ||
196630a5e8faSwyllys 	    oldsize == 0) {
196799ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
196899ebb4caSwyllys 		goto out;
196999ebb4caSwyllys 	}
197099ebb4caSwyllys 
197199ebb4caSwyllys 	olddata = malloc(oldsize);
197299ebb4caSwyllys 	if (olddata == NULL) {
197399ebb4caSwyllys 		ret = KMF_ERR_MEMORY;
197499ebb4caSwyllys 		goto out;
197599ebb4caSwyllys 	}
197699ebb4caSwyllys 	(void) memset(olddata, 0, oldsize);
197799ebb4caSwyllys 	/*
197899ebb4caSwyllys 	 * Read the entire blob of GeneralNames, we don't
197999ebb4caSwyllys 	 * need to interpret them now.
198099ebb4caSwyllys 	 */
198199ebb4caSwyllys 	if (kmfber_read(exasn1, olddata, oldsize) != oldsize) {
198299ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
198399ebb4caSwyllys 		goto out;
198499ebb4caSwyllys 	}
198599ebb4caSwyllys out:
198699ebb4caSwyllys 	if (exasn1 != NULL)
198799ebb4caSwyllys 		kmfber_free(exasn1, 1);
198899ebb4caSwyllys 
198999ebb4caSwyllys 	if (ret != KMF_OK) {
199099ebb4caSwyllys 		*contents = NULL;
199199ebb4caSwyllys 		*outlen = 0;
199299ebb4caSwyllys 		if (olddata != NULL)
199399ebb4caSwyllys 			free(olddata);
199499ebb4caSwyllys 	} else {
199599ebb4caSwyllys 		*contents = olddata;
199699ebb4caSwyllys 		*outlen = oldsize;
199799ebb4caSwyllys 	}
199899ebb4caSwyllys 	return (ret);
199999ebb4caSwyllys }
200099ebb4caSwyllys 
200199ebb4caSwyllys KMF_RETURN
200299ebb4caSwyllys add_an_extension(KMF_X509_EXTENSIONS *exts, KMF_X509_EXTENSION *newextn)
200399ebb4caSwyllys {
200499ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
200599ebb4caSwyllys 	KMF_X509_EXTENSION *extlist;
200699ebb4caSwyllys 
200799ebb4caSwyllys 	if (exts == NULL || newextn == NULL)
200899ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
200999ebb4caSwyllys 
201099ebb4caSwyllys 	extlist = malloc(sizeof (KMF_X509_EXTENSION) *
201130a5e8faSwyllys 	    (exts->numberOfExtensions + 1));
201299ebb4caSwyllys 	if (extlist == NULL)
201399ebb4caSwyllys 		return (KMF_ERR_MEMORY);
201499ebb4caSwyllys 
201599ebb4caSwyllys 	(void) memcpy(extlist, exts->extensions,
201699ebb4caSwyllys 	    exts->numberOfExtensions * sizeof (KMF_X509_EXTENSION));
201799ebb4caSwyllys 
201899ebb4caSwyllys 	(void) memcpy(&extlist[exts->numberOfExtensions], newextn,
201930a5e8faSwyllys 	    sizeof (KMF_X509_EXTENSION));
202099ebb4caSwyllys 
202199ebb4caSwyllys 	free(exts->extensions);
202299ebb4caSwyllys 	exts->numberOfExtensions++;
202399ebb4caSwyllys 	exts->extensions = extlist;
202499ebb4caSwyllys 
202599ebb4caSwyllys 	return (ret);
202699ebb4caSwyllys }
202799ebb4caSwyllys 
202899ebb4caSwyllys KMF_RETURN
202930a5e8faSwyllys kmf_set_altname(KMF_X509_EXTENSIONS *extensions,
203099ebb4caSwyllys 	KMF_OID *oid,
203199ebb4caSwyllys 	int critical,
203299ebb4caSwyllys 	KMF_GENERALNAMECHOICES nametype,
203399ebb4caSwyllys 	char *namedata)
203499ebb4caSwyllys {
203599ebb4caSwyllys 	KMF_RETURN ret = KMF_OK;
203699ebb4caSwyllys 	KMF_X509_EXTENSION subjAltName;
203799ebb4caSwyllys 	KMF_DATA dername = { NULL, 0 };
203899ebb4caSwyllys 	BerElement *asn1 = NULL;
203999ebb4caSwyllys 	BerValue *extdata;
204099ebb4caSwyllys 	char *olddata = NULL;
204199ebb4caSwyllys 	KMF_X509_EXTENSION *foundextn = NULL;
204299ebb4caSwyllys 	size_t	oldsize = 0;
204399ebb4caSwyllys 
204499ebb4caSwyllys 	if (extensions == NULL || oid == NULL || namedata == NULL)
204599ebb4caSwyllys 		return (KMF_ERR_BAD_PARAMETER);
204699ebb4caSwyllys 
204799ebb4caSwyllys 	ret = encode_altname(namedata, nametype, &dername);
204899ebb4caSwyllys 
204999ebb4caSwyllys 	if (ret != KMF_OK)
205099ebb4caSwyllys 		return (ret);
205199ebb4caSwyllys 
205299ebb4caSwyllys 	(void) memset(&subjAltName, 0, sizeof (subjAltName));
205399ebb4caSwyllys 
205499ebb4caSwyllys 	ret = copy_data(&subjAltName.extnId, oid);
205599ebb4caSwyllys 	if (ret != KMF_OK)
205699ebb4caSwyllys 		goto out;
205799ebb4caSwyllys 	/*
205899ebb4caSwyllys 	 * Check to see if this cert already has a subjectAltName.
205999ebb4caSwyllys 	 */
206099ebb4caSwyllys 	foundextn = FindExtn(extensions, oid);
206199ebb4caSwyllys 
206299ebb4caSwyllys 	if (foundextn != NULL) {
206399ebb4caSwyllys 		ret = GetSequenceContents(
206430a5e8faSwyllys 		    (char *)foundextn->BERvalue.Data,
206530a5e8faSwyllys 		    foundextn->BERvalue.Length,
206630a5e8faSwyllys 		    &olddata, &oldsize);
206799ebb4caSwyllys 		if (ret != KMF_OK)
206899ebb4caSwyllys 			goto out;
206999ebb4caSwyllys 	}
207099ebb4caSwyllys 
207199ebb4caSwyllys 	/*
207299ebb4caSwyllys 	 * Assume (!!) that the namedata given is already properly encoded.
207399ebb4caSwyllys 	 */
207499ebb4caSwyllys 	if ((asn1 = kmfder_alloc()) == NULL)
207599ebb4caSwyllys 		return (KMF_ERR_MEMORY);
207699ebb4caSwyllys 
207799ebb4caSwyllys 	if (kmfber_printf(asn1, "{") == -1) {
207899ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
207999ebb4caSwyllys 		goto out;
208099ebb4caSwyllys 	}
208199ebb4caSwyllys 
208299ebb4caSwyllys 	/* Write the old extension data first */
208399ebb4caSwyllys 	if (olddata != NULL && oldsize > 0) {
208499ebb4caSwyllys 		if (kmfber_write(asn1, olddata, oldsize, 0) == -1) {
208599ebb4caSwyllys 			ret = KMF_ERR_ENCODING;
208699ebb4caSwyllys 			goto out;
208799ebb4caSwyllys 		}
208899ebb4caSwyllys 	}
208999ebb4caSwyllys 
209099ebb4caSwyllys 	/* Now add the new name to the list */
209199ebb4caSwyllys 	if (kmfber_write(asn1, (char *)dername.Data, dername.Length, 0) == -1) {
209299ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
209399ebb4caSwyllys 		goto out;
209499ebb4caSwyllys 	}
209599ebb4caSwyllys 
209699ebb4caSwyllys 	/* Now close the sequence */
209799ebb4caSwyllys 	if (kmfber_printf(asn1, "}") == -1) {
209899ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
209999ebb4caSwyllys 		goto out;
210099ebb4caSwyllys 	}
210199ebb4caSwyllys 	if (kmfber_flatten(asn1, &extdata) == -1) {
210299ebb4caSwyllys 		ret = KMF_ERR_ENCODING;
210399ebb4caSwyllys 		goto out;
210499ebb4caSwyllys 	}
210599ebb4caSwyllys 
210699ebb4caSwyllys 	/*
210799ebb4caSwyllys 	 * If we are just adding to an existing list of altNames,
210899ebb4caSwyllys 	 * just replace the BER data associated with the found extension.
210999ebb4caSwyllys 	 */
211099ebb4caSwyllys 	if (foundextn != NULL) {
211199ebb4caSwyllys 		free(foundextn->BERvalue.Data);
211299ebb4caSwyllys 		foundextn->critical = critical;
211399ebb4caSwyllys 		foundextn->BERvalue.Data = (uchar_t *)extdata->bv_val;
211499ebb4caSwyllys 		foundextn->BERvalue.Length = extdata->bv_len;
211599ebb4caSwyllys 	} else {
211699ebb4caSwyllys 		subjAltName.critical = critical;
211799ebb4caSwyllys 		subjAltName.format = KMF_X509_DATAFORMAT_ENCODED;
211899ebb4caSwyllys 		subjAltName.BERvalue.Data = (uchar_t *)extdata->bv_val;
211999ebb4caSwyllys 		subjAltName.BERvalue.Length = extdata->bv_len;
212099ebb4caSwyllys 		ret = add_an_extension(extensions, &subjAltName);
212199ebb4caSwyllys 		if (ret != KMF_OK)
212299ebb4caSwyllys 			free(subjAltName.BERvalue.Data);
212399ebb4caSwyllys 	}
212499ebb4caSwyllys 
212599ebb4caSwyllys 	free(extdata);
212699ebb4caSwyllys out:
212799ebb4caSwyllys 	if (olddata != NULL)
212899ebb4caSwyllys 		free(olddata);
212999ebb4caSwyllys 
213030a5e8faSwyllys 	kmf_free_data(&dername);
213199ebb4caSwyllys 	if (ret != KMF_OK)
213230a5e8faSwyllys 		kmf_free_data(&subjAltName.extnId);
213399ebb4caSwyllys 	if (asn1 != NULL)
213499ebb4caSwyllys 		kmfber_free(asn1, 1);
213599ebb4caSwyllys 	return (ret);
213699ebb4caSwyllys }
213730a5e8faSwyllys 
213830a5e8faSwyllys /*
213930a5e8faSwyllys  * Search a list of attributes for one that matches the given type.
214030a5e8faSwyllys  * Return a pointer into the attribute list.  This does not
214130a5e8faSwyllys  * return a copy of the value, it returns a reference into the
214230a5e8faSwyllys  * given list.
214330a5e8faSwyllys  */
214430a5e8faSwyllys int
214530a5e8faSwyllys kmf_find_attr(KMF_ATTR_TYPE type, KMF_ATTRIBUTE *attlist, int numattrs)
214630a5e8faSwyllys {
214730a5e8faSwyllys 	int i;
214830a5e8faSwyllys 	for (i = 0; i < numattrs; i++) {
214930a5e8faSwyllys 		if (attlist[i].type == type)
215030a5e8faSwyllys 			return (i);
215130a5e8faSwyllys 	}
215230a5e8faSwyllys 	return (-1);
215330a5e8faSwyllys }
215430a5e8faSwyllys 
215530a5e8faSwyllys /*
215630a5e8faSwyllys  * Verify that a given attribute is consistent with the
215730a5e8faSwyllys  * "test" attribute.
215830a5e8faSwyllys  */
215930a5e8faSwyllys static KMF_RETURN
216030a5e8faSwyllys verify_attribute(KMF_ATTRIBUTE *givenattr,
216130a5e8faSwyllys 	KMF_ATTRIBUTE_TESTER *testattr)
216230a5e8faSwyllys {
216330a5e8faSwyllys 	/* A NULL pValue was found where one is required */
216430a5e8faSwyllys 	if (testattr->null_value_ok == FALSE &&
216530a5e8faSwyllys 	    givenattr->pValue == NULL)
216630a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
216730a5e8faSwyllys 
216830a5e8faSwyllys 	/* If the given valueLen is too small, return error */
216930a5e8faSwyllys 	if (givenattr->pValue != NULL &&
217030a5e8faSwyllys 	    testattr->minlen > 0 &&
217130a5e8faSwyllys 	    (givenattr->valueLen < testattr->minlen))
217230a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
217330a5e8faSwyllys 
217430a5e8faSwyllys 	/* If the given valueLen is too big, return error */
217530a5e8faSwyllys 	if (givenattr->pValue != NULL &&
217630a5e8faSwyllys 	    testattr->maxlen > 0 &&
217730a5e8faSwyllys 	    (givenattr->valueLen > testattr->maxlen))
217830a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
217930a5e8faSwyllys 
218030a5e8faSwyllys 	return (KMF_OK);
218130a5e8faSwyllys }
218230a5e8faSwyllys 
218330a5e8faSwyllys /*
218430a5e8faSwyllys  * Given a set of required attribute tests and optional
218530a5e8faSwyllys  * attributes, make sure that the actual attributes
218630a5e8faSwyllys  * being tested (attrlist below) are allowed and are
218730a5e8faSwyllys  * properly specified.
218830a5e8faSwyllys  */
218930a5e8faSwyllys KMF_RETURN
219030a5e8faSwyllys test_attributes(int reqnum, KMF_ATTRIBUTE_TESTER *reqattrs,
219130a5e8faSwyllys 	int optnum, KMF_ATTRIBUTE_TESTER *optattrs,
219230a5e8faSwyllys 	int numattrs, KMF_ATTRIBUTE *attrlist)
219330a5e8faSwyllys {
219430a5e8faSwyllys 	KMF_RETURN ret = KMF_OK;
219530a5e8faSwyllys 	int i, idx;
219630a5e8faSwyllys 
219730a5e8faSwyllys 	/*
219830a5e8faSwyllys 	 * If the caller didn't supply enough attributes,
219930a5e8faSwyllys 	 * return an error.
220030a5e8faSwyllys 	 */
220130a5e8faSwyllys 	if (numattrs < reqnum || attrlist == NULL)
220230a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
220330a5e8faSwyllys 
220430a5e8faSwyllys 	/*
220530a5e8faSwyllys 	 * Make sure all required attrs are present and
220630a5e8faSwyllys 	 * correct.
220730a5e8faSwyllys 	 */
220830a5e8faSwyllys 	for (i = 0; i < reqnum && ret == KMF_OK; i++) {
220930a5e8faSwyllys 		idx = kmf_find_attr(reqattrs[i].type, attrlist, numattrs);
221030a5e8faSwyllys 		/* If a required attr is not found, return error */
221130a5e8faSwyllys 		if (idx == -1) {
221230a5e8faSwyllys 			return (KMF_ERR_BAD_PARAMETER);
221330a5e8faSwyllys 		}
221430a5e8faSwyllys 
221530a5e8faSwyllys 		ret = verify_attribute(&attrlist[idx], &reqattrs[i]);
221630a5e8faSwyllys 	}
221730a5e8faSwyllys 	/*
221830a5e8faSwyllys 	 * Now test the optional parameters.
221930a5e8faSwyllys 	 */
222030a5e8faSwyllys 	for (i = 0; i < optnum && ret == KMF_OK; i++) {
222130a5e8faSwyllys 		idx = kmf_find_attr(optattrs[i].type, attrlist, numattrs);
222230a5e8faSwyllys 		/* If a optional attr is not found, continue. */
222330a5e8faSwyllys 		if (idx == -1) {
222430a5e8faSwyllys 			continue;
222530a5e8faSwyllys 		}
222630a5e8faSwyllys 
222730a5e8faSwyllys 		ret = verify_attribute(&attrlist[idx], &optattrs[i]);
222830a5e8faSwyllys 	}
222930a5e8faSwyllys 
223030a5e8faSwyllys 	return (ret);
223130a5e8faSwyllys }
223230a5e8faSwyllys 
223330a5e8faSwyllys /*
223430a5e8faSwyllys  * Given an already allocated attribute list, insert
223530a5e8faSwyllys  * the given attribute information at a specific index
223630a5e8faSwyllys  * in the list.
223730a5e8faSwyllys  */
223830a5e8faSwyllys void
223930a5e8faSwyllys kmf_set_attr_at_index(KMF_ATTRIBUTE *attlist, int index,
224030a5e8faSwyllys 	KMF_ATTR_TYPE type,  void *pValue, uint32_t len)
224130a5e8faSwyllys {
224230a5e8faSwyllys 	if (attlist == NULL)
224330a5e8faSwyllys 		return;
224430a5e8faSwyllys 
224530a5e8faSwyllys 	attlist[index].type = type;
224630a5e8faSwyllys 	attlist[index].pValue = pValue;
224730a5e8faSwyllys 	attlist[index].valueLen = len;
224830a5e8faSwyllys }
224930a5e8faSwyllys 
225030a5e8faSwyllys /*
225130a5e8faSwyllys  * Find an attribute matching a particular type and set
225230a5e8faSwyllys  * the pValue and length fields to the given values.
225330a5e8faSwyllys  */
225430a5e8faSwyllys KMF_RETURN
225530a5e8faSwyllys kmf_set_attr(KMF_ATTRIBUTE *attlist, int numattr,
225630a5e8faSwyllys 	KMF_ATTR_TYPE type,  void *pValue, uint32_t len)
225730a5e8faSwyllys {
225830a5e8faSwyllys 	int idx;
225930a5e8faSwyllys 	if (attlist == NULL)
226030a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
226130a5e8faSwyllys 
226230a5e8faSwyllys 	idx = kmf_find_attr(type, attlist, numattr);
226330a5e8faSwyllys 	if (idx == -1)
226430a5e8faSwyllys 		return (KMF_ERR_ATTR_NOT_FOUND);
226530a5e8faSwyllys 
226630a5e8faSwyllys 	attlist[idx].type = type;
226730a5e8faSwyllys 	/* Assumes the attribute pValue can hold the result */
226830a5e8faSwyllys 	if (attlist[idx].pValue != NULL) {
226930a5e8faSwyllys 		if (attlist[idx].valueLen >= len)
227030a5e8faSwyllys 			(void) memcpy(attlist[idx].pValue, pValue, len);
227130a5e8faSwyllys 		else
227230a5e8faSwyllys 			return (KMF_ERR_BUFFER_SIZE);
227330a5e8faSwyllys 	}
227430a5e8faSwyllys 	attlist[idx].valueLen = len;
227530a5e8faSwyllys 	return (KMF_OK);
227630a5e8faSwyllys }
227730a5e8faSwyllys 
227830a5e8faSwyllys /*
227930a5e8faSwyllys  * Find a particular attribute in a list and return
228030a5e8faSwyllys  * a pointer to its value.
228130a5e8faSwyllys  */
228230a5e8faSwyllys void *
228330a5e8faSwyllys kmf_get_attr_ptr(KMF_ATTR_TYPE type, KMF_ATTRIBUTE *attlist,
228430a5e8faSwyllys 	int numattrs)
228530a5e8faSwyllys {
228630a5e8faSwyllys 	int i;
228730a5e8faSwyllys 
228830a5e8faSwyllys 	i = kmf_find_attr(type, attlist, numattrs);
228930a5e8faSwyllys 	if (i == -1)
229030a5e8faSwyllys 		return (NULL);
229130a5e8faSwyllys 
229230a5e8faSwyllys 	return (attlist[i].pValue);
229330a5e8faSwyllys }
229430a5e8faSwyllys 
229530a5e8faSwyllys /*
229630a5e8faSwyllys  * Find a particular attribute in a list and return
229730a5e8faSwyllys  * the value and length values.  Value and length
229830a5e8faSwyllys  * may be NULL if the caller doesn't want their values
229930a5e8faSwyllys  * to be filled in.
230030a5e8faSwyllys  */
230130a5e8faSwyllys KMF_RETURN
230230a5e8faSwyllys kmf_get_attr(KMF_ATTR_TYPE type, KMF_ATTRIBUTE *attlist,
230330a5e8faSwyllys 	int numattrs, void *outValue, uint32_t *outlen)
230430a5e8faSwyllys {
230530a5e8faSwyllys 	int i;
230630a5e8faSwyllys 	uint32_t len = 0;
230730a5e8faSwyllys 	uint32_t *lenptr = outlen;
230830a5e8faSwyllys 
230930a5e8faSwyllys 	if (lenptr == NULL)
231030a5e8faSwyllys 		lenptr = &len;
231130a5e8faSwyllys 
231230a5e8faSwyllys 	i = kmf_find_attr(type, attlist, numattrs);
231330a5e8faSwyllys 	if (i == -1)
231430a5e8faSwyllys 		return (KMF_ERR_ATTR_NOT_FOUND);
231530a5e8faSwyllys 
231630a5e8faSwyllys 	/* This assumes that the ptr passed in is pre-allocated space */
231730a5e8faSwyllys 	if (attlist[i].pValue != NULL && outValue != NULL) {
231830a5e8faSwyllys 		/*
231930a5e8faSwyllys 		 * If the caller did not specify a length,
232030a5e8faSwyllys 		 * assume "outValue" is big enough.
232130a5e8faSwyllys 		 */
232230a5e8faSwyllys 		if (outlen != NULL) {
232330a5e8faSwyllys 			if (*outlen >= attlist[i].valueLen)
232430a5e8faSwyllys 				(void) memcpy(outValue, attlist[i].pValue,
232530a5e8faSwyllys 				    attlist[i].valueLen);
232630a5e8faSwyllys 			else
232730a5e8faSwyllys 				return (KMF_ERR_BUFFER_SIZE);
232830a5e8faSwyllys 		} else {
232930a5e8faSwyllys 			(void) memcpy(outValue, attlist[i].pValue,
233030a5e8faSwyllys 			    attlist[i].valueLen);
233130a5e8faSwyllys 		}
233230a5e8faSwyllys 	}
233330a5e8faSwyllys 
233430a5e8faSwyllys 	if (outlen != NULL)
233530a5e8faSwyllys 		*outlen = attlist[i].valueLen;
233630a5e8faSwyllys 	return (KMF_OK);
233730a5e8faSwyllys }
233830a5e8faSwyllys 
233930a5e8faSwyllys /*
234030a5e8faSwyllys  * Utility routine to find a string type attribute, allocate it
234130a5e8faSwyllys  * and return the value to the caller.  This simplifies the
234230a5e8faSwyllys  * operation by doing both "kmf_get_attr" calls and avoids
234330a5e8faSwyllys  * duplicating this block of code in lots of places.
234430a5e8faSwyllys  */
234530a5e8faSwyllys KMF_RETURN
234630a5e8faSwyllys kmf_get_string_attr(KMF_ATTR_TYPE type, KMF_ATTRIBUTE *attrlist,
234730a5e8faSwyllys 	int numattrs, char **outstr)
234830a5e8faSwyllys {
234930a5e8faSwyllys 	KMF_RETURN rv;
235030a5e8faSwyllys 	uint32_t len;
235130a5e8faSwyllys 
235230a5e8faSwyllys 	if (outstr == NULL)
235330a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
235430a5e8faSwyllys 
235530a5e8faSwyllys 	if ((rv = kmf_get_attr(type, attrlist, numattrs, NULL, &len)) ==
235630a5e8faSwyllys 	    KMF_OK) {
235730a5e8faSwyllys 		*outstr = malloc(len + 1);
235830a5e8faSwyllys 		if ((*outstr) == NULL)
235930a5e8faSwyllys 			return (KMF_ERR_MEMORY);
236030a5e8faSwyllys 		(void) memset((*outstr), 0, len + 1);
236130a5e8faSwyllys 		rv = kmf_get_attr(type, attrlist, numattrs, (*outstr), &len);
236230a5e8faSwyllys 		if (rv != KMF_OK) {
236330a5e8faSwyllys 			free(*outstr);
236430a5e8faSwyllys 			*outstr = NULL;
236530a5e8faSwyllys 		}
236630a5e8faSwyllys 	}
236730a5e8faSwyllys 
236830a5e8faSwyllys 	return (rv);
236930a5e8faSwyllys }
237030a5e8faSwyllys 
2371431deaa0Shylee 
2372431deaa0Shylee void
2373431deaa0Shylee free_entry(conf_entry_t *entry)
2374431deaa0Shylee {
2375431deaa0Shylee 	if (entry == NULL)
2376431deaa0Shylee 		return;
2377431deaa0Shylee 	free(entry->keystore);
2378431deaa0Shylee 	free(entry->modulepath);
2379431deaa0Shylee 	free(entry->option);
2380431deaa0Shylee }
2381431deaa0Shylee 
2382431deaa0Shylee void
2383431deaa0Shylee free_entrylist(conf_entrylist_t *list)
2384431deaa0Shylee {
2385431deaa0Shylee 	conf_entrylist_t *next;
2386431deaa0Shylee 
2387431deaa0Shylee 	while (list != NULL) {
2388431deaa0Shylee 		next = list->next;
2389431deaa0Shylee 		free_entry(list->entry);
2390431deaa0Shylee 		free(list);
2391431deaa0Shylee 		list = next;
2392431deaa0Shylee 	}
2393431deaa0Shylee }
2394431deaa0Shylee 
2395431deaa0Shylee static KMF_RETURN
2396431deaa0Shylee parse_entry(char *buf, conf_entry_t **entry)
2397431deaa0Shylee {
2398431deaa0Shylee 	KMF_RETURN ret = KMF_OK;
2399431deaa0Shylee 	conf_entry_t *tmp = NULL;
2400431deaa0Shylee 	char *token1;
2401431deaa0Shylee 	char *token2;
2402431deaa0Shylee 	char *token3;
2403431deaa0Shylee 	char *lasts;
2404431deaa0Shylee 	char *value;
2405431deaa0Shylee 
2406431deaa0Shylee 	if ((token1 = strtok_r(buf, SEP_COLON, &lasts)) == NULL)
2407431deaa0Shylee 		return (KMF_ERR_KMF_CONF);
2408431deaa0Shylee 
2409431deaa0Shylee 	if ((tmp = calloc(sizeof (conf_entry_t), 1)) == NULL)
2410431deaa0Shylee 		return (KMF_ERR_MEMORY);
2411431deaa0Shylee 
2412431deaa0Shylee 	if ((tmp->keystore = strdup(token1)) == NULL) {
2413431deaa0Shylee 		ret = KMF_ERR_MEMORY;
2414431deaa0Shylee 		goto end;
2415431deaa0Shylee 	}
2416431deaa0Shylee 
2417431deaa0Shylee 	if ((token2 = strtok_r(NULL, SEP_SEMICOLON, &lasts)) == NULL) {
2418431deaa0Shylee 		ret = KMF_ERR_KMF_CONF;
2419431deaa0Shylee 		goto end;
2420431deaa0Shylee 	}
2421431deaa0Shylee 
2422431deaa0Shylee 	/* need to get token3 first to satisfy nested strtok invocations */
2423431deaa0Shylee 	token3 = strtok_r(NULL, SEP_SEMICOLON, &lasts);
2424431deaa0Shylee 
2425431deaa0Shylee 	/* parse token2 */
2426431deaa0Shylee 	if (strncmp(token2, CONF_MODULEPATH, strlen(CONF_MODULEPATH)) != 0) {
2427431deaa0Shylee 		ret = KMF_ERR_KMF_CONF;
2428431deaa0Shylee 		goto end;
2429431deaa0Shylee 	}
2430431deaa0Shylee 
2431431deaa0Shylee 	if (value = strpbrk(token2, SEP_EQUAL)) {
2432431deaa0Shylee 		value++; /* get rid of = */
2433431deaa0Shylee 	} else {
2434431deaa0Shylee 		ret = KMF_ERR_KMF_CONF;
2435431deaa0Shylee 		goto end;
2436431deaa0Shylee 	}
2437431deaa0Shylee 
2438431deaa0Shylee 	if ((tmp->modulepath = strdup(value)) == NULL) {
2439431deaa0Shylee 		ret = KMF_ERR_MEMORY;
2440431deaa0Shylee 		goto end;
2441431deaa0Shylee 	}
2442431deaa0Shylee 
2443431deaa0Shylee 	/* parse token3, if it exists */
2444431deaa0Shylee 	if (token3 != NULL) {
2445431deaa0Shylee 		if (strncmp(token3, CONF_OPTION, strlen(CONF_OPTION))
2446431deaa0Shylee 		    != 0) {
2447431deaa0Shylee 			ret = KMF_ERR_KMF_CONF;
2448431deaa0Shylee 			goto end;
2449431deaa0Shylee 		}
2450431deaa0Shylee 
2451431deaa0Shylee 		if (value = strpbrk(token3, SEP_EQUAL)) {
2452431deaa0Shylee 			value++; /* get rid of = */
2453431deaa0Shylee 		} else {
2454431deaa0Shylee 			ret = KMF_ERR_KMF_CONF;
2455431deaa0Shylee 			goto end;
2456431deaa0Shylee 		}
2457431deaa0Shylee 
2458431deaa0Shylee 		if ((tmp->option = strdup(value)) == NULL) {
2459431deaa0Shylee 			ret = KMF_ERR_MEMORY;
2460431deaa0Shylee 			goto end;
2461431deaa0Shylee 		}
2462431deaa0Shylee 	}
2463431deaa0Shylee 
2464431deaa0Shylee 	*entry = tmp;
2465431deaa0Shylee 
2466431deaa0Shylee end:
2467431deaa0Shylee 	if (ret != KMF_OK) {
2468431deaa0Shylee 		free_entry(tmp);
2469431deaa0Shylee 		free(tmp);
2470431deaa0Shylee 	}
2471431deaa0Shylee 	return (ret);
2472431deaa0Shylee }
2473431deaa0Shylee 
2474431deaa0Shylee 
2475431deaa0Shylee conf_entry_t *
2476431deaa0Shylee dup_entry(conf_entry_t *entry)
2477431deaa0Shylee {
2478431deaa0Shylee 	conf_entry_t *rtn_entry;
2479431deaa0Shylee 
2480431deaa0Shylee 	if (entry == NULL)
2481431deaa0Shylee 		return (NULL);
2482431deaa0Shylee 
2483431deaa0Shylee 	rtn_entry = malloc(sizeof (conf_entry_t));
2484431deaa0Shylee 	if (rtn_entry == NULL)
2485431deaa0Shylee 		return (NULL);
2486431deaa0Shylee 
2487431deaa0Shylee 	if ((rtn_entry->keystore = strdup(entry->keystore)) == NULL)
2488431deaa0Shylee 		goto out;
2489431deaa0Shylee 
2490431deaa0Shylee 	if ((rtn_entry->modulepath = strdup(entry->modulepath)) == NULL)
2491431deaa0Shylee 		goto out;
2492431deaa0Shylee 
2493431deaa0Shylee 	if (entry->option != NULL &&
2494431deaa0Shylee 	    (rtn_entry->option = strdup(entry->modulepath)) == NULL)
2495431deaa0Shylee 		goto out;
2496431deaa0Shylee 
2497431deaa0Shylee 	return (rtn_entry);
2498431deaa0Shylee 
2499431deaa0Shylee out:
2500431deaa0Shylee 	free_entry(rtn_entry);
2501431deaa0Shylee 	return (NULL);
2502431deaa0Shylee }
2503431deaa0Shylee 
2504431deaa0Shylee 
2505431deaa0Shylee /*
2506431deaa0Shylee  * This function takes a keystore_name as input and returns
2507431deaa0Shylee  * the KMF_KEYSTORE_TYPE value assigned to it.  If the "option"
2508431deaa0Shylee  * argument is not NULL, this function also returns the option string
2509431deaa0Shylee  * if there is an option string for the plugin module.
2510431deaa0Shylee  */
2511431deaa0Shylee KMF_RETURN
2512431deaa0Shylee kmf_get_plugin_info(KMF_HANDLE_T handle, char *keystore_name,
2513431deaa0Shylee     KMF_KEYSTORE_TYPE *kstype, char **option)
2514431deaa0Shylee {
2515431deaa0Shylee 	KMF_RETURN ret = KMF_OK;
2516431deaa0Shylee 	conf_entrylist_t  *phead = extra_plugin_list;
2517431deaa0Shylee 	boolean_t is_default = B_TRUE;
2518431deaa0Shylee 
2519431deaa0Shylee 	/*
2520431deaa0Shylee 	 * Although handle is not really used in the function, we will
2521431deaa0Shylee 	 * check the handle to make sure that kmf_intialize() is called
2522431deaa0Shylee 	 * before this function.
2523431deaa0Shylee 	 */
2524431deaa0Shylee 	if (handle == NULL || keystore_name == NULL || kstype == NULL)
2525431deaa0Shylee 		return (KMF_ERR_BAD_PARAMETER);
2526431deaa0Shylee 
2527431deaa0Shylee 	if (strcmp(keystore_name, "pkcs11") == 0) {
2528431deaa0Shylee 		*kstype = KMF_KEYSTORE_PK11TOKEN;
2529431deaa0Shylee 	} else if (strcmp(keystore_name, "file") == 0) {
2530431deaa0Shylee 		*kstype = KMF_KEYSTORE_OPENSSL;
2531431deaa0Shylee 	} else if (strcmp(keystore_name, "nss") == 0) {
2532431deaa0Shylee 		*kstype = KMF_KEYSTORE_NSS;
2533431deaa0Shylee 	} else {
2534431deaa0Shylee 		is_default = B_FALSE;
2535431deaa0Shylee 	}
2536431deaa0Shylee 
2537431deaa0Shylee 	if (is_default) {
2538431deaa0Shylee 		if (option != NULL)
2539431deaa0Shylee 			*option = NULL;
2540431deaa0Shylee 		goto out;
2541431deaa0Shylee 	}
2542431deaa0Shylee 
2543431deaa0Shylee 	/* Not a built-in plugin; check if it is in extra_plugin_list. */
2544431deaa0Shylee 	while (phead != NULL) {
2545431deaa0Shylee 		if (strcmp(phead->entry->keystore, keystore_name) == 0)
2546431deaa0Shylee 			break;
2547431deaa0Shylee 		phead = phead->next;
2548431deaa0Shylee 	}
2549431deaa0Shylee 
2550431deaa0Shylee 	if (phead == NULL) {
2551431deaa0Shylee 		ret = KMF_ERR_PLUGIN_NOTFOUND;
2552431deaa0Shylee 		goto out;
2553431deaa0Shylee 	}
2554431deaa0Shylee 
2555431deaa0Shylee 	/* found it */
2556431deaa0Shylee 	*kstype = phead->entry->kstype;
2557431deaa0Shylee 	if (option != NULL) {
2558431deaa0Shylee 		if (phead->entry->option == NULL)
2559431deaa0Shylee 			*option = NULL;
2560431deaa0Shylee 		else {
2561431deaa0Shylee 			*option = strdup(phead->entry->option);
2562431deaa0Shylee 			if (*option == NULL) {
2563431deaa0Shylee 				ret = KMF_ERR_MEMORY;
2564431deaa0Shylee 				goto out;
2565431deaa0Shylee 			}
2566431deaa0Shylee 		}
2567431deaa0Shylee 	}
2568431deaa0Shylee 
2569431deaa0Shylee out:
2570431deaa0Shylee 	return (ret);
2571431deaa0Shylee }
2572431deaa0Shylee 
2573431deaa0Shylee /*
2574431deaa0Shylee  * Retrieve the non-default plugin list from the kmf.conf file.
2575431deaa0Shylee  */
2576431deaa0Shylee KMF_RETURN
2577431deaa0Shylee get_entrylist(conf_entrylist_t **entlist)
2578431deaa0Shylee {
2579431deaa0Shylee 	KMF_RETURN rv = KMF_OK;
2580431deaa0Shylee 	FILE *pfile;
2581431deaa0Shylee 	conf_entry_t *entry;
2582431deaa0Shylee 	conf_entrylist_t *rtnlist = NULL;
2583431deaa0Shylee 	conf_entrylist_t *ptmp;
2584431deaa0Shylee 	conf_entrylist_t *pcur;
2585431deaa0Shylee 	char buffer[MAXPATHLEN];
2586431deaa0Shylee 	size_t len;
2587431deaa0Shylee 
2588431deaa0Shylee 	if ((pfile = fopen(_PATH_KMF_CONF, "rF")) == NULL) {
2589431deaa0Shylee 		cryptoerror(LOG_ERR, "failed to open %s.\n", _PATH_KMF_CONF);
2590431deaa0Shylee 		return (KMF_ERR_KMF_CONF);
2591431deaa0Shylee 	}
2592431deaa0Shylee 
2593431deaa0Shylee 	while (fgets(buffer, MAXPATHLEN, pfile) != NULL) {
2594431deaa0Shylee 		if (buffer[0] == '#' || buffer[0] == ' ' ||
2595431deaa0Shylee 		    buffer[0] == '\n'|| buffer[0] == '\t') {
2596431deaa0Shylee 			continue;   /* ignore comment lines */
2597431deaa0Shylee 		}
2598431deaa0Shylee 
2599431deaa0Shylee 		len = strlen(buffer);
2600431deaa0Shylee 		if (buffer[len-1] == '\n') { /* get rid of trailing '\n' */
2601431deaa0Shylee 			len--;
2602431deaa0Shylee 		}
2603431deaa0Shylee 		buffer[len] = '\0';
2604431deaa0Shylee 
2605431deaa0Shylee 		rv = parse_entry(buffer, &entry);
2606431deaa0Shylee 		if (rv != KMF_OK) {
2607431deaa0Shylee 			goto end;
2608431deaa0Shylee 		}
2609431deaa0Shylee 
2610431deaa0Shylee 		if ((ptmp = malloc(sizeof (conf_entrylist_t))) == NULL) {
2611431deaa0Shylee 			rv = KMF_ERR_MEMORY;
2612431deaa0Shylee 			goto end;
2613431deaa0Shylee 		}
2614431deaa0Shylee 		ptmp->entry = entry;
2615431deaa0Shylee 		ptmp->next = NULL;
2616431deaa0Shylee 
2617431deaa0Shylee 		if (rtnlist == NULL) {
2618431deaa0Shylee 			rtnlist = pcur = ptmp;
2619431deaa0Shylee 		} else {
2620431deaa0Shylee 			pcur->next = ptmp;
2621431deaa0Shylee 			pcur = ptmp;
2622431deaa0Shylee 		}
2623431deaa0Shylee 	}
2624431deaa0Shylee 
2625431deaa0Shylee end:
2626431deaa0Shylee 	(void) fclose(pfile);
2627431deaa0Shylee 
2628431deaa0Shylee 	if (rv == KMF_OK) {
2629431deaa0Shylee 		*entlist = rtnlist;
2630431deaa0Shylee 	} else if (rtnlist != NULL) {
2631431deaa0Shylee 		free_entrylist(rtnlist);
2632431deaa0Shylee 		*entlist = NULL;
2633431deaa0Shylee 		kstore_num = DEFAULT_KEYSTORE_NUM;
2634431deaa0Shylee 	}
2635431deaa0Shylee 
2636431deaa0Shylee 	return (rv);
2637431deaa0Shylee }
2638431deaa0Shylee 
2639431deaa0Shylee 
2640431deaa0Shylee boolean_t
2641431deaa0Shylee is_valid_keystore_type(KMF_KEYSTORE_TYPE kstype)
2642431deaa0Shylee {
2643431deaa0Shylee 
2644431deaa0Shylee 	if (kstype > 0 && kstype <= kstore_num)
2645431deaa0Shylee 		return (B_TRUE);
2646431deaa0Shylee 	else
2647431deaa0Shylee 		return (B_FALSE);
2648431deaa0Shylee }
2649431deaa0Shylee 
2650431deaa0Shylee 
265130a5e8faSwyllys /*
265230a5e8faSwyllys  * This API is used by elfsign. We must keep it in old API form.
265330a5e8faSwyllys  */
265430a5e8faSwyllys KMF_RETURN
265530a5e8faSwyllys KMF_ConfigureKeystore(KMF_HANDLE_T handle, KMF_CONFIG_PARAMS *params)
265630a5e8faSwyllys {
265730a5e8faSwyllys 
265830a5e8faSwyllys 	KMF_ATTRIBUTE attlist[32];
265930a5e8faSwyllys 	int i = 0;
266030a5e8faSwyllys 
266130a5e8faSwyllys 	if (params == NULL)
266230a5e8faSwyllys 		return (KMF_ERR_BAD_PARAMETER);
266330a5e8faSwyllys 
266430a5e8faSwyllys 	kmf_set_attr_at_index(attlist, i,
266530a5e8faSwyllys 	    KMF_KEYSTORE_TYPE_ATTR, &params->kstype, sizeof (params->kstype));
266630a5e8faSwyllys 	i++;
266730a5e8faSwyllys 
266830a5e8faSwyllys 	if (params->kstype == KMF_KEYSTORE_NSS) {
266930a5e8faSwyllys 		if (params->nssconfig.configdir != NULL) {
267030a5e8faSwyllys 			kmf_set_attr_at_index(attlist, i,
267130a5e8faSwyllys 			    KMF_DIRPATH_ATTR,
267230a5e8faSwyllys 			    params->nssconfig.configdir,
267330a5e8faSwyllys 			    strlen(params->nssconfig.configdir));
267430a5e8faSwyllys 			i++;
267530a5e8faSwyllys 		}
267630a5e8faSwyllys 		if (params->nssconfig.certPrefix != NULL) {
267730a5e8faSwyllys 			kmf_set_attr_at_index(attlist, i,
267830a5e8faSwyllys 			    KMF_CERTPREFIX_ATTR,
267930a5e8faSwyllys 			    params->nssconfig.certPrefix,
268030a5e8faSwyllys 			    strlen(params->nssconfig.certPrefix));
268130a5e8faSwyllys 			i++;
268230a5e8faSwyllys 		}
268330a5e8faSwyllys 		if (params->nssconfig.keyPrefix != NULL) {
268430a5e8faSwyllys 			kmf_set_attr_at_index(attlist, i,
268530a5e8faSwyllys 			    KMF_KEYPREFIX_ATTR,
268630a5e8faSwyllys 			    params->nssconfig.keyPrefix,
268730a5e8faSwyllys 			    strlen(params->nssconfig.keyPrefix));
268830a5e8faSwyllys 			i++;
268930a5e8faSwyllys 		}
269030a5e8faSwyllys 		if (params->nssconfig.secModName != NULL) {
269130a5e8faSwyllys 			kmf_set_attr_at_index(attlist, i,
269230a5e8faSwyllys 			    KMF_SECMODNAME_ATTR,
269330a5e8faSwyllys 			    params->nssconfig.secModName,
269430a5e8faSwyllys 			    strlen(params->nssconfig.secModName));
269530a5e8faSwyllys 			i++;
269630a5e8faSwyllys 		}
269730a5e8faSwyllys 	} else if (params->kstype == KMF_KEYSTORE_PK11TOKEN) {
269830a5e8faSwyllys 		if (params->pkcs11config.label != NULL) {
269930a5e8faSwyllys 			kmf_set_attr_at_index(attlist, i,
270030a5e8faSwyllys 			    KMF_TOKEN_LABEL_ATTR,
270130a5e8faSwyllys 			    params->pkcs11config.label,
270230a5e8faSwyllys 			    strlen(params->pkcs11config.label));
270330a5e8faSwyllys 			i++;
270430a5e8faSwyllys 		}
270530a5e8faSwyllys 		kmf_set_attr_at_index(attlist, i,
270630a5e8faSwyllys 		    KMF_READONLY_ATTR,
270730a5e8faSwyllys 		    &params->pkcs11config.readonly,
270830a5e8faSwyllys 		    sizeof (params->pkcs11config.readonly));
270930a5e8faSwyllys 		i++;
271030a5e8faSwyllys 	}
271130a5e8faSwyllys 
271230a5e8faSwyllys 	return (kmf_configure_keystore(handle, i, attlist));
271330a5e8faSwyllys }
271430a5e8faSwyllys 
271530a5e8faSwyllys /*
271630a5e8faSwyllys  * This API is used by elfsign. We must keep it in old API form.
271730a5e8faSwyllys  */
271830a5e8faSwyllys KMF_RETURN
271930a5e8faSwyllys KMF_Initialize(KMF_HANDLE_T *outhandle, char *policyfile, char *policyname)
272030a5e8faSwyllys {
272130a5e8faSwyllys 	return (kmf_initialize(outhandle, policyfile, policyname));
272230a5e8faSwyllys }
272330a5e8faSwyllys 
272430a5e8faSwyllys /*
272530a5e8faSwyllys  * This API is used by elfsign. We must keep it in old API form.
272630a5e8faSwyllys  */
272730a5e8faSwyllys KMF_RETURN
272830a5e8faSwyllys KMF_Finalize(KMF_HANDLE_T handle)
272930a5e8faSwyllys {
273030a5e8faSwyllys 	return (kmf_finalize(handle));
273130a5e8faSwyllys }
273230a5e8faSwyllys 
273330a5e8faSwyllys /*
273430a5e8faSwyllys  * This API is used by elfsign. We must keep it in old API form.
273530a5e8faSwyllys  */
273630a5e8faSwyllys KMF_RETURN
273730a5e8faSwyllys KMF_GetKMFErrorString(KMF_RETURN errcode, char **errmsg)
273830a5e8faSwyllys {
273930a5e8faSwyllys 	return (kmf_get_kmf_error_str(errcode, errmsg));
274030a5e8faSwyllys }
274130a5e8faSwyllys 
274230a5e8faSwyllys /*
274330a5e8faSwyllys  * This API is used by elfsign. We must keep it in old API form.
274430a5e8faSwyllys  */
274530a5e8faSwyllys KMF_RETURN
274630a5e8faSwyllys KMF_ReadInputFile(KMF_HANDLE_T handle, char *filename,  KMF_DATA *pdata)
274730a5e8faSwyllys {
274830a5e8faSwyllys 	return (kmf_read_input_file(handle, filename, pdata));
274930a5e8faSwyllys }
275030a5e8faSwyllys 
275130a5e8faSwyllys 
275230a5e8faSwyllys /*
275330a5e8faSwyllys  * This API is used by elfsign. We must keep it in old API form.
275430a5e8faSwyllys  */
275530a5e8faSwyllys void
275630a5e8faSwyllys KMF_FreeKMFCert(KMF_HANDLE_T handle, KMF_X509_DER_CERT *kmf_cert)
275730a5e8faSwyllys {
275830a5e8faSwyllys 	kmf_free_kmf_cert(handle, kmf_cert);
275930a5e8faSwyllys }
276030a5e8faSwyllys 
276130a5e8faSwyllys /*
276230a5e8faSwyllys  * This API is used by elfsign. We must keep it in old API form.
276330a5e8faSwyllys  */
276430a5e8faSwyllys void
276530a5e8faSwyllys KMF_FreeData(KMF_DATA *datablock)
276630a5e8faSwyllys {
276730a5e8faSwyllys 	kmf_free_data(datablock);
276830a5e8faSwyllys }
276930a5e8faSwyllys 
277030a5e8faSwyllys /*
277130a5e8faSwyllys  * This API is used by elfsign. We must keep it in old API form.
277230a5e8faSwyllys  */
277330a5e8faSwyllys void
277430a5e8faSwyllys KMF_FreeKMFKey(KMF_HANDLE_T handle, KMF_KEY_HANDLE *key)
277530a5e8faSwyllys {
277630a5e8faSwyllys 	kmf_free_kmf_key(handle, key);
277730a5e8faSwyllys }
277830a5e8faSwyllys 
277930a5e8faSwyllys /*
278030a5e8faSwyllys  * This API is used by elfsign. We must keep it in old API form.
278130a5e8faSwyllys  */
278230a5e8faSwyllys void
278330a5e8faSwyllys KMF_FreeSignedCSR(KMF_CSR_DATA *csr)
278430a5e8faSwyllys {
278530a5e8faSwyllys 	kmf_free_signed_csr(csr);
278630a5e8faSwyllys }
2787