17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
58047c9fbSmcpowers  * Common Development and Distribution License (the "License").
68047c9fbSmcpowers  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2219193bb6SDina K Nimeh  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*05d57413SDan McDonald  *
25*05d57413SDan McDonald  * Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * General-Purpose Functions
297c478bd9Sstevel@tonic-gate  * (as defined in PKCS#11 spec section 11.4)
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include "metaGlobal.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate extern meta_session_t *meta_sessionlist_head;
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate struct CK_FUNCTION_LIST metaslot_functionList = {
40f66d273dSizick 	{ 2, 20 },	/* version */
417c478bd9Sstevel@tonic-gate 	meta_Initialize,
427c478bd9Sstevel@tonic-gate 	meta_Finalize,
437c478bd9Sstevel@tonic-gate 	meta_GetInfo,
447c478bd9Sstevel@tonic-gate 	meta_GetFunctionList,
457c478bd9Sstevel@tonic-gate 	meta_GetSlotList,
467c478bd9Sstevel@tonic-gate 	meta_GetSlotInfo,
477c478bd9Sstevel@tonic-gate 	meta_GetTokenInfo,
487c478bd9Sstevel@tonic-gate 	meta_GetMechanismList,
497c478bd9Sstevel@tonic-gate 	meta_GetMechanismInfo,
507c478bd9Sstevel@tonic-gate 	meta_InitToken,
517c478bd9Sstevel@tonic-gate 	meta_InitPIN,
527c478bd9Sstevel@tonic-gate 	meta_SetPIN,
537c478bd9Sstevel@tonic-gate 	meta_OpenSession,
547c478bd9Sstevel@tonic-gate 	meta_CloseSession,
557c478bd9Sstevel@tonic-gate 	meta_CloseAllSessions,
567c478bd9Sstevel@tonic-gate 	meta_GetSessionInfo,
577c478bd9Sstevel@tonic-gate 	meta_GetOperationState,
587c478bd9Sstevel@tonic-gate 	meta_SetOperationState,
597c478bd9Sstevel@tonic-gate 	meta_Login,
607c478bd9Sstevel@tonic-gate 	meta_Logout,
617c478bd9Sstevel@tonic-gate 	meta_CreateObject,
627c478bd9Sstevel@tonic-gate 	meta_CopyObject,
637c478bd9Sstevel@tonic-gate 	meta_DestroyObject,
647c478bd9Sstevel@tonic-gate 	meta_GetObjectSize,
657c478bd9Sstevel@tonic-gate 	meta_GetAttributeValue,
667c478bd9Sstevel@tonic-gate 	meta_SetAttributeValue,
677c478bd9Sstevel@tonic-gate 	meta_FindObjectsInit,
687c478bd9Sstevel@tonic-gate 	meta_FindObjects,
697c478bd9Sstevel@tonic-gate 	meta_FindObjectsFinal,
707c478bd9Sstevel@tonic-gate 	meta_EncryptInit,
717c478bd9Sstevel@tonic-gate 	meta_Encrypt,
727c478bd9Sstevel@tonic-gate 	meta_EncryptUpdate,
737c478bd9Sstevel@tonic-gate 	meta_EncryptFinal,
747c478bd9Sstevel@tonic-gate 	meta_DecryptInit,
757c478bd9Sstevel@tonic-gate 	meta_Decrypt,
767c478bd9Sstevel@tonic-gate 	meta_DecryptUpdate,
777c478bd9Sstevel@tonic-gate 	meta_DecryptFinal,
787c478bd9Sstevel@tonic-gate 	meta_DigestInit,
797c478bd9Sstevel@tonic-gate 	meta_Digest,
807c478bd9Sstevel@tonic-gate 	meta_DigestUpdate,
817c478bd9Sstevel@tonic-gate 	meta_DigestKey,
827c478bd9Sstevel@tonic-gate 	meta_DigestFinal,
837c478bd9Sstevel@tonic-gate 	meta_SignInit,
847c478bd9Sstevel@tonic-gate 	meta_Sign,
857c478bd9Sstevel@tonic-gate 	meta_SignUpdate,
867c478bd9Sstevel@tonic-gate 	meta_SignFinal,
877c478bd9Sstevel@tonic-gate 	meta_SignRecoverInit,
887c478bd9Sstevel@tonic-gate 	meta_SignRecover,
897c478bd9Sstevel@tonic-gate 	meta_VerifyInit,
907c478bd9Sstevel@tonic-gate 	meta_Verify,
917c478bd9Sstevel@tonic-gate 	meta_VerifyUpdate,
927c478bd9Sstevel@tonic-gate 	meta_VerifyFinal,
937c478bd9Sstevel@tonic-gate 	meta_VerifyRecoverInit,
947c478bd9Sstevel@tonic-gate 	meta_VerifyRecover,
957c478bd9Sstevel@tonic-gate 	meta_DigestEncryptUpdate,
967c478bd9Sstevel@tonic-gate 	meta_DecryptDigestUpdate,
977c478bd9Sstevel@tonic-gate 	meta_SignEncryptUpdate,
987c478bd9Sstevel@tonic-gate 	meta_DecryptVerifyUpdate,
997c478bd9Sstevel@tonic-gate 	meta_GenerateKey,
1007c478bd9Sstevel@tonic-gate 	meta_GenerateKeyPair,
1017c478bd9Sstevel@tonic-gate 	meta_WrapKey,
1027c478bd9Sstevel@tonic-gate 	meta_UnwrapKey,
1037c478bd9Sstevel@tonic-gate 	meta_DeriveKey,
1047c478bd9Sstevel@tonic-gate 	meta_SeedRandom,
1057c478bd9Sstevel@tonic-gate 	meta_GenerateRandom,
1067c478bd9Sstevel@tonic-gate 	meta_GetFunctionStatus,
1077c478bd9Sstevel@tonic-gate 	meta_CancelFunction,
1087c478bd9Sstevel@tonic-gate 	meta_WaitForSlotEvent
1097c478bd9Sstevel@tonic-gate };
1107c478bd9Sstevel@tonic-gate 
1118047c9fbSmcpowers pthread_mutex_t initmutex = PTHREAD_MUTEX_INITIALIZER;
1128047c9fbSmcpowers 
1137c478bd9Sstevel@tonic-gate ses_to_be_freed_list_t ses_delay_freed;
1147c478bd9Sstevel@tonic-gate object_to_be_freed_list_t obj_delay_freed;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * meta_Initialize
1187c478bd9Sstevel@tonic-gate  *
1197c478bd9Sstevel@tonic-gate  * This function is never called by the application.  It is only
1207c478bd9Sstevel@tonic-gate  * called by uCF to initialize metaslot.  The pInitArgs argument is ignored.
1217c478bd9Sstevel@tonic-gate  *
1227c478bd9Sstevel@tonic-gate  */
1237c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1247c478bd9Sstevel@tonic-gate CK_RV
meta_Initialize(CK_VOID_PTR pInitArgs)1257c478bd9Sstevel@tonic-gate meta_Initialize(CK_VOID_PTR pInitArgs)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	CK_RV rv;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/* Make sure function hasn't been called twice */
1307c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&initmutex);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	rv = meta_slotManager_initialize();
1337c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
1347c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&initmutex);
1357c478bd9Sstevel@tonic-gate 		return (rv);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	rv = meta_mechManager_initialize();
1397c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
1407c478bd9Sstevel@tonic-gate 		(void) meta_slotManager_finalize();
1417c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&initmutex);
1427c478bd9Sstevel@tonic-gate 		return (rv);
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	rv = meta_objectManager_initialize();
1467c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
1477c478bd9Sstevel@tonic-gate 		(void) meta_slotManager_finalize();
1487c478bd9Sstevel@tonic-gate 		(void) meta_mechManager_finalize();
1497c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&initmutex);
1507c478bd9Sstevel@tonic-gate 		return (rv);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	rv = meta_sessionManager_initialize();
1547c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
1557c478bd9Sstevel@tonic-gate 		(void) meta_slotManager_finalize();
1567c478bd9Sstevel@tonic-gate 		(void) meta_mechManager_finalize();
1577c478bd9Sstevel@tonic-gate 		(void) meta_objectManager_finalize();
1587c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&initmutex);
1597c478bd9Sstevel@tonic-gate 		return (rv);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	meta_slotManager_find_object_token();
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	/* Initialize the object_to_be_freed list */
1657c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&obj_delay_freed.obj_to_be_free_mutex, NULL);
1667c478bd9Sstevel@tonic-gate 	obj_delay_freed.count = 0;
1677c478bd9Sstevel@tonic-gate 	obj_delay_freed.first = NULL;
1687c478bd9Sstevel@tonic-gate 	obj_delay_freed.last = NULL;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/* Initialize the session_to_be_freed list */
1717c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&ses_delay_freed.ses_to_be_free_mutex, NULL);
1727c478bd9Sstevel@tonic-gate 	ses_delay_freed.count = 0;
1737c478bd9Sstevel@tonic-gate 	ses_delay_freed.first = NULL;
1747c478bd9Sstevel@tonic-gate 	ses_delay_freed.last = NULL;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&initmutex);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	return (CKR_OK);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate  * meta_Finalize
1847c478bd9Sstevel@tonic-gate  *
1857c478bd9Sstevel@tonic-gate  * Called by uCF only, "pReserved" argument is ignored.
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1887c478bd9Sstevel@tonic-gate CK_RV
meta_Finalize(CK_VOID_PTR pReserved)1897c478bd9Sstevel@tonic-gate meta_Finalize(CK_VOID_PTR pReserved)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
1927c478bd9Sstevel@tonic-gate 	meta_object_t *delay_free_obj, *tmpo;
1937c478bd9Sstevel@tonic-gate 	meta_session_t *delay_free_ses, *tmps;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	if (pReserved != NULL)
1967c478bd9Sstevel@tonic-gate 		return (CKR_ARGUMENTS_BAD);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&initmutex);
1997c478bd9Sstevel@tonic-gate 
200*05d57413SDan McDonald 	/*
201*05d57413SDan McDonald 	 * There used to be calls to cleanup libcryptoutil here.  Given that
202*05d57413SDan McDonald 	 * libcryptoutil can be linked and invoked independently of PKCS#11,
203*05d57413SDan McDonald 	 * cleaning up libcryptoutil here makes no sense.  Decoupling these
204*05d57413SDan McDonald 	 * two also prevent deadlocks and other artificial dependencies.
205*05d57413SDan McDonald 	 */
2068047c9fbSmcpowers 
2077c478bd9Sstevel@tonic-gate 	meta_objectManager_finalize();
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	meta_sessionManager_finalize();
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	meta_mechManager_finalize();
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	meta_slotManager_finalize();
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	/*
2167c478bd9Sstevel@tonic-gate 	 * free all entries in the delay_freed list
2177c478bd9Sstevel@tonic-gate 	 */
2187c478bd9Sstevel@tonic-gate 	delay_free_obj = obj_delay_freed.first;
2197c478bd9Sstevel@tonic-gate 	while (delay_free_obj != NULL) {
2207c478bd9Sstevel@tonic-gate 		tmpo = delay_free_obj->next;
2217c478bd9Sstevel@tonic-gate 		free(delay_free_obj);
2227c478bd9Sstevel@tonic-gate 		delay_free_obj = tmpo;
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_destroy(&obj_delay_freed.obj_to_be_free_mutex);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	delay_free_ses = ses_delay_freed.first;
2277c478bd9Sstevel@tonic-gate 	while (delay_free_ses != NULL) {
2287c478bd9Sstevel@tonic-gate 		tmps = delay_free_ses->next;
2297c478bd9Sstevel@tonic-gate 		free(delay_free_ses);
2307c478bd9Sstevel@tonic-gate 		delay_free_ses = tmps;
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_destroy(&ses_delay_freed.ses_to_be_free_mutex);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&initmutex);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	return (rv);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate /*
2407c478bd9Sstevel@tonic-gate  * meta_GetInfo
2417c478bd9Sstevel@tonic-gate  *
2427c478bd9Sstevel@tonic-gate  * NOTE: This function will never be called by applications because it's
2437c478bd9Sstevel@tonic-gate  * hidden behind the uCF C_GetInfo. So, it is not implemented.
2447c478bd9Sstevel@tonic-gate  */
2457c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2467c478bd9Sstevel@tonic-gate CK_RV
meta_GetInfo(CK_INFO_PTR pInfo)2477c478bd9Sstevel@tonic-gate meta_GetInfo(CK_INFO_PTR pInfo)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	return (CKR_FUNCTION_NOT_SUPPORTED);
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate  * meta_GetFunctionList
2557c478bd9Sstevel@tonic-gate  *
2567c478bd9Sstevel@tonic-gate  * This function is not implemented because metaslot is part of the framework,
2577c478bd9Sstevel@tonic-gate  * so, the framework can just do a static assignment to metaslot's
2587c478bd9Sstevel@tonic-gate  * function list instead of calling this function.
2597c478bd9Sstevel@tonic-gate  */
2607c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2617c478bd9Sstevel@tonic-gate CK_RV
meta_GetFunctionList(CK_FUNCTION_LIST_PTR_PTR ppFunctionList)2627c478bd9Sstevel@tonic-gate meta_GetFunctionList(CK_FUNCTION_LIST_PTR_PTR ppFunctionList)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate 	return (CKR_FUNCTION_NOT_SUPPORTED);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate /*
2697c478bd9Sstevel@tonic-gate  * Parallel Function Management Function
2707c478bd9Sstevel@tonic-gate  * (as defined in PKCS#11 spec section 11.16)
2717c478bd9Sstevel@tonic-gate  */
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate  * This function is no longer supported in this revision of the PKCS#11
2757c478bd9Sstevel@tonic-gate  * standard.  It is maintained for backwards compatibility only.
2767c478bd9Sstevel@tonic-gate  */
2777c478bd9Sstevel@tonic-gate /* ARGSUSED */
2787c478bd9Sstevel@tonic-gate CK_RV
meta_GetFunctionStatus(CK_SESSION_HANDLE hSession)2797c478bd9Sstevel@tonic-gate meta_GetFunctionStatus(CK_SESSION_HANDLE hSession)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate 	return (CKR_FUNCTION_NOT_PARALLEL);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate /*
2867c478bd9Sstevel@tonic-gate  * This function is no longer supported in this revision of the PKCS#11
2877c478bd9Sstevel@tonic-gate  * standard.  It is maintained for backwards compatibility only.
2887c478bd9Sstevel@tonic-gate  */
2897c478bd9Sstevel@tonic-gate /* ARGSUSED */
2907c478bd9Sstevel@tonic-gate CK_RV
meta_CancelFunction(CK_SESSION_HANDLE hSession)2917c478bd9Sstevel@tonic-gate meta_CancelFunction(CK_SESSION_HANDLE hSession)
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate 	return (CKR_FUNCTION_NOT_PARALLEL);
2947c478bd9Sstevel@tonic-gate }
295