xref: /illumos-gate/usr/src/lib/pkcs11/libpkcs11/common/pkcs11Object.c (revision 59d65d3175825093531e82f44269d948ed510a00)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <security/cryptoki.h>
28 #include "pkcs11Global.h"
29 #include "pkcs11Session.h"
30 #include "pkcs11Slot.h"
31 
32 /*
33  * C_CreateObject is a pure wrapper to the underlying provider.
34  * The only argument checked is whether or not hSession is valid.
35  */
36 CK_RV
37 C_CreateObject(CK_SESSION_HANDLE hSession,
38     CK_ATTRIBUTE_PTR pTemplate,
39     CK_ULONG ulCount,
40     CK_OBJECT_HANDLE_PTR phObject)
41 {
42 
43 	CK_RV rv;
44 	pkcs11_session_t *sessp;
45 
46 	/* Check for a fastpath */
47 	if (purefastpath || policyfastpath) {
48 		return (fast_funcs->C_CreateObject(hSession, pTemplate,
49 			    ulCount, phObject));
50 	}
51 
52 	if (!pkcs11_initialized) {
53 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
54 	}
55 
56 	/* Obtain the session pointer */
57 	HANDLE2SESSION(hSession, sessp, rv);
58 
59 	if (rv != CKR_OK) {
60 		return (rv);
61 	}
62 
63 	/* Pass data to the provider */
64 	rv = FUNCLIST(sessp->se_slotid)->C_CreateObject(sessp->se_handle,
65 	    pTemplate, ulCount, phObject);
66 
67 	/* Present consistent interface to the application */
68 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
69 		return (CKR_FUNCTION_FAILED);
70 	}
71 
72 	return (rv);
73 }
74 
75 /*
76  * C_CopyObject is a pure wrapper to the underlying provider.
77  * The only argument checked is whether or not hSession is valid.
78  */
79 CK_RV
80 C_CopyObject(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
81     CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
82     CK_OBJECT_HANDLE_PTR phNewObject)
83 {
84 	CK_RV rv;
85 	pkcs11_session_t *sessp;
86 
87 	/* Check for a fastpath */
88 	if (purefastpath || policyfastpath) {
89 		return (fast_funcs->C_CopyObject(hSession, hObject,
90 			    pTemplate, ulCount, phNewObject));
91 	}
92 
93 	if (!pkcs11_initialized) {
94 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
95 	}
96 
97 	/* Obtain the session pointer */
98 	HANDLE2SESSION(hSession, sessp, rv);
99 
100 	if (rv != CKR_OK) {
101 		return (rv);
102 	}
103 
104 	/* Pass data to the provider */
105 	rv = FUNCLIST(sessp->se_slotid)->C_CopyObject(sessp->se_handle,
106 	    hObject, pTemplate, ulCount, phNewObject);
107 
108 	/* Present consistent interface to the application */
109 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
110 		return (CKR_FUNCTION_FAILED);
111 	}
112 
113 	return (rv);
114 }
115 
116 /*
117  * C_DestroyObject is a pure wrapper to the underlying provider.
118  * The only argument checked is whether or not hSession is valid.
119  */
120 CK_RV
121 C_DestroyObject(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject)
122 {
123 	CK_RV rv;
124 	pkcs11_session_t *sessp;
125 
126 	/* Check for a fastpath */
127 	if (purefastpath || policyfastpath) {
128 		return (fast_funcs->C_DestroyObject(hSession, hObject));
129 	}
130 
131 	if (!pkcs11_initialized) {
132 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
133 	}
134 
135 	/* Obtain the session pointer */
136 	HANDLE2SESSION(hSession, sessp, rv);
137 
138 	if (rv != CKR_OK) {
139 		return (rv);
140 	}
141 
142 	/* Pass data to the provider */
143 	rv = FUNCLIST(sessp->se_slotid)->C_DestroyObject(sessp->se_handle,
144 	    hObject);
145 
146 	/* Present consistent interface to the application */
147 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
148 		return (CKR_FUNCTION_FAILED);
149 	}
150 
151 	return (rv);
152 }
153 
154 /*
155  * C_GetAttributeValue is a pure wrapper to the underlying provider.
156  * The only argument checked is whether or not hSession is valid.
157  */
158 CK_RV
159 C_GetAttributeValue(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
160     CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)
161 {
162 	CK_RV rv;
163 	pkcs11_session_t *sessp;
164 
165 	/* Check for a fastpath */
166 	if (purefastpath || policyfastpath) {
167 		return (fast_funcs->C_GetAttributeValue(hSession, hObject,
168 			    pTemplate, ulCount));
169 	}
170 
171 	if (!pkcs11_initialized) {
172 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
173 	}
174 
175 	/* Obtain the session pointer */
176 	HANDLE2SESSION(hSession, sessp, rv);
177 
178 	if (rv != CKR_OK) {
179 		return (rv);
180 	}
181 
182 	/* Pass data to the provider */
183 	rv = FUNCLIST(sessp->se_slotid)->C_GetAttributeValue(sessp->se_handle,
184 	    hObject, pTemplate, ulCount);
185 
186 	/* Present consistent interface to the application */
187 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
188 		return (CKR_FUNCTION_FAILED);
189 	}
190 
191 	return (rv);
192 
193 }
194 
195 /*
196  * C_SetAttributeValue is a pure wrapper to the underlying provider.
197  * The only argument checked is whether or not hSession is valid.
198  */
199 CK_RV
200 C_SetAttributeValue(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
201     CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)
202 {
203 	CK_RV rv;
204 	pkcs11_session_t *sessp;
205 
206 	/* Check for a fastpath */
207 	if (purefastpath || policyfastpath) {
208 		return (fast_funcs->C_SetAttributeValue(hSession, hObject,
209 			    pTemplate, ulCount));
210 	}
211 
212 	if (!pkcs11_initialized) {
213 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
214 	}
215 
216 	/* Obtain the session pointer */
217 	HANDLE2SESSION(hSession, sessp, rv);
218 
219 	if (rv != CKR_OK) {
220 		return (rv);
221 	}
222 
223 	/* Pass data to the provider */
224 	rv = FUNCLIST(sessp->se_slotid)->C_SetAttributeValue(sessp->se_handle,
225 	    hObject, pTemplate, ulCount);
226 
227 	/* Present consistent interface to the application */
228 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
229 		return (CKR_FUNCTION_FAILED);
230 	}
231 
232 	return (rv);
233 }
234 
235 /*
236  * C_GetObjectSize is a pure wrapper to the underlying provider.
237  * The only argument checked is whether or not hSession is valid.
238  */
239 CK_RV
240 C_GetObjectSize(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
241     CK_ULONG_PTR pulSize)
242 {
243 	CK_RV rv;
244 	pkcs11_session_t *sessp;
245 
246 	/* Check for a fastpath */
247 	if (purefastpath || policyfastpath) {
248 		return (fast_funcs->C_GetObjectSize(hSession, hObject,
249 			    pulSize));
250 	}
251 
252 	if (!pkcs11_initialized) {
253 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
254 	}
255 
256 	/* Obtain the session pointer */
257 	HANDLE2SESSION(hSession, sessp, rv);
258 
259 	if (rv != CKR_OK) {
260 		return (rv);
261 	}
262 
263 	/* Pass data to the provider */
264 	rv = FUNCLIST(sessp->se_slotid)->C_GetObjectSize(sessp->se_handle,
265 	    hObject, pulSize);
266 
267 	/* Present consistent interface to the application */
268 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
269 		return (CKR_FUNCTION_FAILED);
270 	}
271 
272 	return (rv);
273 }
274 
275 /*
276  * C_FindObjectsInit is a pure wrapper to the underlying provider.
277  * The only argument checked is whether or not hSession is valid.
278  */
279 CK_RV
280 C_FindObjectsInit(CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate,
281     CK_ULONG ulCount)
282 {
283 	CK_RV rv;
284 	pkcs11_session_t *sessp;
285 
286 	/* Check for a fastpath */
287 	if (purefastpath || policyfastpath) {
288 		return (fast_funcs->C_FindObjectsInit(hSession, pTemplate,
289 			    ulCount));
290 	}
291 
292 	if (!pkcs11_initialized) {
293 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
294 	}
295 
296 	/* Obtain the session pointer */
297 	HANDLE2SESSION(hSession, sessp, rv);
298 
299 	if (rv != CKR_OK) {
300 		return (rv);
301 	}
302 
303 	/* Pass data to the provider */
304 	rv = FUNCLIST(sessp->se_slotid)->C_FindObjectsInit(sessp->se_handle,
305 	    pTemplate, ulCount);
306 
307 	/* Present consistent interface to the application */
308 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
309 		return (CKR_FUNCTION_FAILED);
310 	}
311 
312 	return (rv);
313 }
314 
315 /*
316  * C_FindObjects is a pure wrapper to the underlying provider.
317  * The only argument checked is whether or not hSession is valid.
318  */
319 CK_RV
320 C_FindObjects(CK_SESSION_HANDLE hSession,
321     CK_OBJECT_HANDLE_PTR phObject,
322     CK_ULONG ulMaxObjectCount,
323     CK_ULONG_PTR pulObjectCount)
324 {
325 	CK_RV rv;
326 	pkcs11_session_t *sessp;
327 
328 	/* Check for a fastpath */
329 	if (purefastpath || policyfastpath) {
330 		return (fast_funcs->C_FindObjects(hSession, phObject,
331 			    ulMaxObjectCount, pulObjectCount));
332 	}
333 
334 	if (!pkcs11_initialized) {
335 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
336 	}
337 
338 	/* Obtain the session pointer */
339 	HANDLE2SESSION(hSession, sessp, rv);
340 
341 	if (rv != CKR_OK) {
342 		return (rv);
343 	}
344 
345 	/* Pass data to the provider */
346 	rv = FUNCLIST(sessp->se_slotid)->C_FindObjects(sessp->se_handle,
347 	    phObject, ulMaxObjectCount, pulObjectCount);
348 
349 	/* Present consistent interface to the application */
350 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
351 		return (CKR_FUNCTION_FAILED);
352 	}
353 
354 	return (rv);
355 }
356 
357 /*
358  * C_FindObjectsFinal is a pure wrapper to the underlying provider.
359  * The only argument checked is whether or not hSession is valid.
360  */
361 CK_RV
362 C_FindObjectsFinal(CK_SESSION_HANDLE hSession)
363 {
364 	CK_RV rv;
365 	pkcs11_session_t *sessp;
366 
367 	/* Check for a fastpath */
368 	if (purefastpath || policyfastpath) {
369 		return (fast_funcs->C_FindObjectsFinal(hSession));
370 	}
371 
372 	if (!pkcs11_initialized) {
373 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
374 	}
375 
376 	/* Obtain the session pointer */
377 	HANDLE2SESSION(hSession, sessp, rv);
378 
379 	if (rv != CKR_OK) {
380 		return (rv);
381 	}
382 
383 	/* Pass data to the provider */
384 	rv = FUNCLIST(sessp->se_slotid)->C_FindObjectsFinal(sessp->se_handle);
385 
386 	/* Present consistent interface to the application */
387 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
388 		return (CKR_FUNCTION_FAILED);
389 	}
390 
391 	return (rv);
392 }
393