xref: /titanic_41/usr/src/lib/pkcs11/libpkcs11/common/metaGeneral.c (revision 9232efa04937a223f1f7ead806594aca7f293518)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
26  */
27 /*
28  * General-Purpose Functions
29  * (as defined in PKCS#11 spec section 11.4)
30  */
31 
32 #include <unistd.h>
33 #include <errno.h>
34 #include <string.h>
35 #include "metaGlobal.h"
36 
37 extern meta_session_t *meta_sessionlist_head;
38 
39 struct CK_FUNCTION_LIST metaslot_functionList = {
40 	{ 2, 20 },	/* version */
41 	meta_Initialize,
42 	meta_Finalize,
43 	meta_GetInfo,
44 	meta_GetFunctionList,
45 	meta_GetSlotList,
46 	meta_GetSlotInfo,
47 	meta_GetTokenInfo,
48 	meta_GetMechanismList,
49 	meta_GetMechanismInfo,
50 	meta_InitToken,
51 	meta_InitPIN,
52 	meta_SetPIN,
53 	meta_OpenSession,
54 	meta_CloseSession,
55 	meta_CloseAllSessions,
56 	meta_GetSessionInfo,
57 	meta_GetOperationState,
58 	meta_SetOperationState,
59 	meta_Login,
60 	meta_Logout,
61 	meta_CreateObject,
62 	meta_CopyObject,
63 	meta_DestroyObject,
64 	meta_GetObjectSize,
65 	meta_GetAttributeValue,
66 	meta_SetAttributeValue,
67 	meta_FindObjectsInit,
68 	meta_FindObjects,
69 	meta_FindObjectsFinal,
70 	meta_EncryptInit,
71 	meta_Encrypt,
72 	meta_EncryptUpdate,
73 	meta_EncryptFinal,
74 	meta_DecryptInit,
75 	meta_Decrypt,
76 	meta_DecryptUpdate,
77 	meta_DecryptFinal,
78 	meta_DigestInit,
79 	meta_Digest,
80 	meta_DigestUpdate,
81 	meta_DigestKey,
82 	meta_DigestFinal,
83 	meta_SignInit,
84 	meta_Sign,
85 	meta_SignUpdate,
86 	meta_SignFinal,
87 	meta_SignRecoverInit,
88 	meta_SignRecover,
89 	meta_VerifyInit,
90 	meta_Verify,
91 	meta_VerifyUpdate,
92 	meta_VerifyFinal,
93 	meta_VerifyRecoverInit,
94 	meta_VerifyRecover,
95 	meta_DigestEncryptUpdate,
96 	meta_DecryptDigestUpdate,
97 	meta_SignEncryptUpdate,
98 	meta_DecryptVerifyUpdate,
99 	meta_GenerateKey,
100 	meta_GenerateKeyPair,
101 	meta_WrapKey,
102 	meta_UnwrapKey,
103 	meta_DeriveKey,
104 	meta_SeedRandom,
105 	meta_GenerateRandom,
106 	meta_GetFunctionStatus,
107 	meta_CancelFunction,
108 	meta_WaitForSlotEvent
109 };
110 
111 pthread_mutex_t initmutex = PTHREAD_MUTEX_INITIALIZER;
112 
113 ses_to_be_freed_list_t ses_delay_freed;
114 object_to_be_freed_list_t obj_delay_freed;
115 
116 /*
117  * meta_Initialize
118  *
119  * This function is never called by the application.  It is only
120  * called by uCF to initialize metaslot.  The pInitArgs argument is ignored.
121  *
122  */
123 /*ARGSUSED*/
124 CK_RV
meta_Initialize(CK_VOID_PTR pInitArgs)125 meta_Initialize(CK_VOID_PTR pInitArgs)
126 {
127 	CK_RV rv;
128 
129 	/* Make sure function hasn't been called twice */
130 	(void) pthread_mutex_lock(&initmutex);
131 
132 	rv = meta_slotManager_initialize();
133 	if (rv != CKR_OK) {
134 		(void) pthread_mutex_unlock(&initmutex);
135 		return (rv);
136 	}
137 
138 	rv = meta_mechManager_initialize();
139 	if (rv != CKR_OK) {
140 		(void) meta_slotManager_finalize();
141 		(void) pthread_mutex_unlock(&initmutex);
142 		return (rv);
143 	}
144 
145 	rv = meta_objectManager_initialize();
146 	if (rv != CKR_OK) {
147 		(void) meta_slotManager_finalize();
148 		(void) meta_mechManager_finalize();
149 		(void) pthread_mutex_unlock(&initmutex);
150 		return (rv);
151 	}
152 
153 	rv = meta_sessionManager_initialize();
154 	if (rv != CKR_OK) {
155 		(void) meta_slotManager_finalize();
156 		(void) meta_mechManager_finalize();
157 		(void) meta_objectManager_finalize();
158 		(void) pthread_mutex_unlock(&initmutex);
159 		return (rv);
160 	}
161 
162 	meta_slotManager_find_object_token();
163 
164 	/* Initialize the object_to_be_freed list */
165 	(void) pthread_mutex_init(&obj_delay_freed.obj_to_be_free_mutex, NULL);
166 	obj_delay_freed.count = 0;
167 	obj_delay_freed.first = NULL;
168 	obj_delay_freed.last = NULL;
169 
170 	/* Initialize the session_to_be_freed list */
171 	(void) pthread_mutex_init(&ses_delay_freed.ses_to_be_free_mutex, NULL);
172 	ses_delay_freed.count = 0;
173 	ses_delay_freed.first = NULL;
174 	ses_delay_freed.last = NULL;
175 
176 	(void) pthread_mutex_unlock(&initmutex);
177 
178 	return (CKR_OK);
179 }
180 
181 
182 /*
183  * meta_Finalize
184  *
185  * Called by uCF only, "pReserved" argument is ignored.
186  */
187 /*ARGSUSED*/
188 CK_RV
meta_Finalize(CK_VOID_PTR pReserved)189 meta_Finalize(CK_VOID_PTR pReserved)
190 {
191 	CK_RV rv = CKR_OK;
192 	meta_object_t *delay_free_obj, *tmpo;
193 	meta_session_t *delay_free_ses, *tmps;
194 
195 	if (pReserved != NULL)
196 		return (CKR_ARGUMENTS_BAD);
197 
198 	(void) pthread_mutex_lock(&initmutex);
199 
200 	/*
201 	 * There used to be calls to cleanup libcryptoutil here.  Given that
202 	 * libcryptoutil can be linked and invoked independently of PKCS#11,
203 	 * cleaning up libcryptoutil here makes no sense.  Decoupling these
204 	 * two also prevent deadlocks and other artificial dependencies.
205 	 */
206 
207 	meta_objectManager_finalize();
208 
209 	meta_sessionManager_finalize();
210 
211 	meta_mechManager_finalize();
212 
213 	meta_slotManager_finalize();
214 
215 	/*
216 	 * free all entries in the delay_freed list
217 	 */
218 	delay_free_obj = obj_delay_freed.first;
219 	while (delay_free_obj != NULL) {
220 		tmpo = delay_free_obj->next;
221 		free(delay_free_obj);
222 		delay_free_obj = tmpo;
223 	}
224 	(void) pthread_mutex_destroy(&obj_delay_freed.obj_to_be_free_mutex);
225 
226 	delay_free_ses = ses_delay_freed.first;
227 	while (delay_free_ses != NULL) {
228 		tmps = delay_free_ses->next;
229 		free(delay_free_ses);
230 		delay_free_ses = tmps;
231 	}
232 	(void) pthread_mutex_destroy(&ses_delay_freed.ses_to_be_free_mutex);
233 
234 	(void) pthread_mutex_unlock(&initmutex);
235 
236 	return (rv);
237 }
238 
239 /*
240  * meta_GetInfo
241  *
242  * NOTE: This function will never be called by applications because it's
243  * hidden behind the uCF C_GetInfo. So, it is not implemented.
244  */
245 /*ARGSUSED*/
246 CK_RV
meta_GetInfo(CK_INFO_PTR pInfo)247 meta_GetInfo(CK_INFO_PTR pInfo)
248 {
249 	return (CKR_FUNCTION_NOT_SUPPORTED);
250 }
251 
252 
253 /*
254  * meta_GetFunctionList
255  *
256  * This function is not implemented because metaslot is part of the framework,
257  * so, the framework can just do a static assignment to metaslot's
258  * function list instead of calling this function.
259  */
260 /*ARGSUSED*/
261 CK_RV
meta_GetFunctionList(CK_FUNCTION_LIST_PTR_PTR ppFunctionList)262 meta_GetFunctionList(CK_FUNCTION_LIST_PTR_PTR ppFunctionList)
263 {
264 	return (CKR_FUNCTION_NOT_SUPPORTED);
265 }
266 
267 
268 /*
269  * Parallel Function Management Function
270  * (as defined in PKCS#11 spec section 11.16)
271  */
272 
273 /*
274  * This function is no longer supported in this revision of the PKCS#11
275  * standard.  It is maintained for backwards compatibility only.
276  */
277 /* ARGSUSED */
278 CK_RV
meta_GetFunctionStatus(CK_SESSION_HANDLE hSession)279 meta_GetFunctionStatus(CK_SESSION_HANDLE hSession)
280 {
281 	return (CKR_FUNCTION_NOT_PARALLEL);
282 }
283 
284 
285 /*
286  * This function is no longer supported in this revision of the PKCS#11
287  * standard.  It is maintained for backwards compatibility only.
288  */
289 /* ARGSUSED */
290 CK_RV
meta_CancelFunction(CK_SESSION_HANDLE hSession)291 meta_CancelFunction(CK_SESSION_HANDLE hSession)
292 {
293 	return (CKR_FUNCTION_NOT_PARALLEL);
294 }
295