blob: 0fb10f151a0c4a41576297f6ee4cef1d1c257058 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Portions Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
3 */
4
5/* Copyright (c) 2002 Graz University of Technology. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * 3. The end-user documentation included with the redistribution, if any, must
18 * include the following acknowledgment:
19 *
20 * "This product includes software developed by IAIK of Graz University of
21 * Technology."
22 *
23 * Alternately, this acknowledgment may appear in the software itself, if
24 * and wherever such third-party acknowledgments normally appear.
25 *
26 * 4. The names "Graz University of Technology" and "IAIK of Graz University of
27 * Technology" must not be used to endorse or promote products derived from
28 * this software without prior written permission.
29 *
30 * 5. Products derived from this software may not be called
31 * "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior
32 * written permission of Graz University of Technology.
33 *
34 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
35 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
36 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE
38 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
41 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
42 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45 * POSSIBILITY OF SUCH DAMAGE.
46 */
47
48#include "pkcs11wrapper.h"
49
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <assert.h>
54
55#include "sun_security_pkcs11_wrapper_PKCS11.h"
56
57#ifdef P11_ENABLE_C_CREATEOBJECT
58/*
59 * Class: sun_security_pkcs11_wrapper_PKCS11
60 * Method: C_CreateObject
61 * Signature: (J[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)J
62 * Parametermapping: *PKCS11*
63 * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
64 * @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate
65 * CK_ULONG ulCount
66 * @return jlong jObjectHandle CK_OBJECT_HANDLE_PTR phObject
67 */
68JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CreateObject
69 (JNIEnv *env, jobject obj, jlong jSessionHandle, jobjectArray jTemplate)
70{
71 CK_SESSION_HANDLE ckSessionHandle;
72 CK_OBJECT_HANDLE ckObjectHandle;
73 CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
74 CK_ULONG ckAttributesLength;
75 jlong jObjectHandle;
76 CK_ULONG i;
77 CK_RV rv;
78
79 CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
80 if (ckpFunctions == NULL) { return 0L; }
81
82 ckSessionHandle = jLongToCKULong(jSessionHandle);
83 jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
84
85 rv = (*ckpFunctions->C_CreateObject)(ckSessionHandle, ckpAttributes, ckAttributesLength, &ckObjectHandle);
86
87 jObjectHandle = ckULongToJLong(ckObjectHandle);
88 for(i=0; i<ckAttributesLength; i++)
89 if(ckpAttributes[i].pValue != NULL_PTR)
90 free(ckpAttributes[i].pValue);
91 free(ckpAttributes);
92
93 if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; }
94
95 return jObjectHandle ;
96}
97#endif
98
99#ifdef P11_ENABLE_C_COPYOBJECT
100/*
101 * Class: sun_security_pkcs11_wrapper_PKCS11
102 * Method: C_CopyObject
103 * Signature: (JJ[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)J
104 * Parametermapping: *PKCS11*
105 * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
106 * @param jlong jObjectHandle CK_OBJECT_HANDLE hObject
107 * @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate
108 * CK_ULONG ulCount
109 * @return jlong jNewObjectHandle CK_OBJECT_HANDLE_PTR phNewObject
110 */
111JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CopyObject
112 (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jObjectHandle, jobjectArray jTemplate)
113{
114 CK_SESSION_HANDLE ckSessionHandle;
115 CK_OBJECT_HANDLE ckObjectHandle;
116 CK_OBJECT_HANDLE ckNewObjectHandle;
117 CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
118 CK_ULONG ckAttributesLength;
119 jlong jNewObjectHandle;
120 CK_ULONG i;
121 CK_RV rv;
122
123 CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
124 if (ckpFunctions == NULL) { return 0L; }
125
126 ckSessionHandle = jLongToCKULong(jSessionHandle);
127 ckObjectHandle = jLongToCKULong(jObjectHandle);
128 jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
129
130 rv = (*ckpFunctions->C_CopyObject)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength, &ckNewObjectHandle);
131
132 jNewObjectHandle = ckULongToJLong(ckNewObjectHandle);
133 for(i=0; i<ckAttributesLength; i++)
134 if(ckpAttributes[i].pValue != NULL_PTR)
135 free(ckpAttributes[i].pValue);
136 free(ckpAttributes);
137
138 if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; }
139
140 return jNewObjectHandle ;
141}
142#endif
143
144#ifdef P11_ENABLE_C_DESTROYOBJECT
145/*
146 * Class: sun_security_pkcs11_wrapper_PKCS11
147 * Method: C_DestroyObject
148 * Signature: (JJ)V
149 * Parametermapping: *PKCS11*
150 * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
151 * @param jlong jObjectHandle CK_OBJECT_HANDLE hObject
152 */
153JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DestroyObject
154 (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jObjectHandle)
155{
156 CK_SESSION_HANDLE ckSessionHandle;
157 CK_OBJECT_HANDLE ckObjectHandle;
158 CK_RV rv;
159
160 CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
161 if (ckpFunctions == NULL) { return; }
162
163 ckSessionHandle = jLongToCKULong(jSessionHandle);
164 ckObjectHandle = jLongToCKULong(jObjectHandle);
165
166 rv = (*ckpFunctions->C_DestroyObject)(ckSessionHandle, ckObjectHandle);
167 if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
168}
169#endif
170
171#ifdef P11_ENABLE_C_GETOBJECTSIZE
172/*
173 * Class: sun_security_pkcs11_wrapper_PKCS11
174 * Method: C_GetObjectSize
175 * Signature: (JJ)J
176 * Parametermapping: *PKCS11*
177 * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
178 * @param jlong jObjectHandle CK_OBJECT_HANDLE hObject
179 * @return jlong jObjectSize CK_ULONG_PTR pulSize
180 */
181JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetObjectSize
182 (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jObjectHandle)
183{
184 CK_SESSION_HANDLE ckSessionHandle;
185 CK_OBJECT_HANDLE ckObjectHandle;
186 CK_ULONG ckObjectSize;
187 jlong jObjectSize;
188 CK_RV rv;
189
190 CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
191 if (ckpFunctions == NULL) { return 0L; }
192
193 ckSessionHandle = jLongToCKULong(jSessionHandle);
194 ckObjectHandle = jLongToCKULong(jObjectHandle);
195
196 rv = (*ckpFunctions->C_GetObjectSize)(ckSessionHandle, ckObjectHandle, &ckObjectSize);
197 if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; }
198
199 jObjectSize = ckULongToJLong(ckObjectSize);
200
201 return jObjectSize ;
202}
203#endif
204
205#ifdef P11_ENABLE_C_GETATTRIBUTEVALUE
206/*
207 * Class: sun_security_pkcs11_wrapper_PKCS11
208 * Method: C_GetAttributeValue
209 * Signature: (JJ[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;
210 * Parametermapping: *PKCS11*
211 * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
212 * @param jlong jObjectHandle CK_OBJECT_HANDLE hObject
213 * @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate
214 * CK_ULONG ulCount
215 */
216JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetAttributeValue
217 (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jObjectHandle, jobjectArray jTemplate)
218{
219 CK_SESSION_HANDLE ckSessionHandle;
220 CK_OBJECT_HANDLE ckObjectHandle;
221 CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
222 CK_ULONG ckAttributesLength;
223 CK_ULONG ckBufferLength;
224 CK_ULONG i;
225 jobject jAttribute;
226 CK_RV rv;
227
228 CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
229 if (ckpFunctions == NULL) { return; }
230
231 TRACE0("DEBUG: C_GetAttributeValue");
232 TRACE1(", hSession=%u", jSessionHandle);
233 TRACE1(", hObject=%u", jObjectHandle);
234 TRACE1(", pTemplate=%p", jTemplate);
235 TRACE0(" ... ");
236
237 ckSessionHandle = jLongToCKULong(jSessionHandle);
238 ckObjectHandle = jLongToCKULong(jObjectHandle);
239 TRACE1("jAttributeArrayToCKAttributeArray now with jTemplate = %d", jTemplate);
240 jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
241 TRACE2("DEBUG: jAttributeArrayToCKAttributeArray finished with ckpAttribute = %d, Length = %d\n", ckpAttributes, ckAttributesLength);
242
243 /* first set all pValue to NULL, to get the needed buffer length */
244 for(i = 0; i < ckAttributesLength; i++) {
245 if(ckpAttributes[i].pValue != NULL_PTR) {
246 free(ckpAttributes[i].pValue);
247 }
248 }
249 for (i = 0; i < ckAttributesLength; i++) {
250 ckpAttributes[i].pValue = NULL_PTR;
251 }
252 rv = (*ckpFunctions->C_GetAttributeValue)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength);
253 if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {
254 free(ckpAttributes);
255 return ;
256 }
257
258 /* now, the ulValueLength field of each attribute should hold the exact buffer length needed
259 * allocate the needed buffers accordingly
260 */
261 for (i = 0; i < ckAttributesLength; i++) {
262 ckBufferLength = sizeof(CK_BYTE) * ckpAttributes[i].ulValueLen;
263 ckpAttributes[i].pValue = (void *) malloc(ckBufferLength);
264 ckpAttributes[i].ulValueLen = ckBufferLength;
265 }
266
267 /* now get the attributes with all values */
268 rv = (*ckpFunctions->C_GetAttributeValue)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength);
269
270 /* copy back the values to the Java attributes */
271 for (i = 0; i < ckAttributesLength; i++) {
272 jAttribute = ckAttributePtrToJAttribute(env, &(ckpAttributes[i]));
273 (*env)->SetObjectArrayElement(env, jTemplate, i, jAttribute);
274 }
275
276 for(i=0; i<ckAttributesLength; i++) {
277 if(ckpAttributes[i].pValue != NULL_PTR) {
278 free(ckpAttributes[i].pValue);
279 }
280 }
281 free(ckpAttributes);
282 TRACE0("FINISHED\n");
283
284 if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return ; }
285}
286#endif
287
288#ifdef P11_ENABLE_C_SETATTRIBUTEVALUE
289/*
290 * Class: sun_security_pkcs11_wrapper_PKCS11
291 * Method: C_SetAttributeValue
292 * Signature: (JJ[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)V
293 * Parametermapping: *PKCS11*
294 * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
295 * @param jlong jObjectHandle CK_OBJECT_HANDLE hObject
296 * @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate
297 * CK_ULONG ulCount
298 */
299JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SetAttributeValue
300 (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jObjectHandle, jobjectArray jTemplate)
301{
302 CK_SESSION_HANDLE ckSessionHandle;
303 CK_OBJECT_HANDLE ckObjectHandle;
304 CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
305 CK_ULONG ckAttributesLength;
306 CK_ULONG i;
307 CK_RV rv;
308
309 CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
310 if (ckpFunctions == NULL) { return; }
311
312 ckSessionHandle = jLongToCKULong(jSessionHandle);
313 ckObjectHandle = jLongToCKULong(jObjectHandle);
314 jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
315
316 rv = (*ckpFunctions->C_SetAttributeValue)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength);
317
318 for(i=0; i<ckAttributesLength; i++) {
319 if(ckpAttributes[i].pValue != NULL_PTR) {
320 free(ckpAttributes[i].pValue);
321 }
322 }
323 free(ckpAttributes);
324
325 if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
326}
327#endif
328
329#ifdef P11_ENABLE_C_FINDOBJECTSINIT
330/*
331 * Class: sun_security_pkcs11_wrapper_PKCS11
332 * Method: C_FindObjectsInit
333 * Signature: (J[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)V
334 * Parametermapping: *PKCS11*
335 * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
336 * @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate
337 * CK_ULONG ulCount
338 */
339JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1FindObjectsInit
340 (JNIEnv *env, jobject obj, jlong jSessionHandle, jobjectArray jTemplate)
341{
342 CK_SESSION_HANDLE ckSessionHandle;
343 CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
344 CK_ULONG ckAttributesLength;
345 CK_ULONG i;
346 CK_RV rv;
347
348 CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
349 if (ckpFunctions == NULL) { return; }
350
351 TRACE0("DEBUG: C_FindObjectsInit");
352 TRACE1(", hSession=%u", jSessionHandle);
353 TRACE1(", pTemplate=%p", jTemplate);
354 TRACE0(" ... ");
355
356 ckSessionHandle = jLongToCKULong(jSessionHandle);
357 jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
358
359 rv = (*ckpFunctions->C_FindObjectsInit)(ckSessionHandle, ckpAttributes, ckAttributesLength);
360
361 for(i=0; i<ckAttributesLength; i++) {
362 if(ckpAttributes[i].pValue != NULL_PTR) {
363 free(ckpAttributes[i].pValue);
364 }
365 }
366 free(ckpAttributes);
367 TRACE0("FINISHED\n");
368
369 if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
370}
371#endif
372
373#ifdef P11_ENABLE_C_FINDOBJECTS
374/*
375 * Class: sun_security_pkcs11_wrapper_PKCS11
376 * Method: C_FindObjects
377 * Signature: (JJ)[J
378 * Parametermapping: *PKCS11*
379 * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
380 * @param jlong jMaxObjectCount CK_ULONG ulMaxObjectCount
381 * @return jlongArray jObjectHandleArray CK_OBJECT_HANDLE_PTR phObject
382 * CK_ULONG_PTR pulObjectCount
383 */
384JNIEXPORT jlongArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1FindObjects
385 (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jMaxObjectCount)
386{
387 CK_RV rv;
388 CK_SESSION_HANDLE ckSessionHandle;
389 CK_ULONG ckMaxObjectLength;
390 CK_OBJECT_HANDLE_PTR ckpObjectHandleArray;
391 CK_ULONG ckActualObjectCount;
392 jlongArray jObjectHandleArray;
393
394 CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
395 if (ckpFunctions == NULL) { return NULL; }
396
397 ckSessionHandle = jLongToCKULong(jSessionHandle);
398 ckMaxObjectLength = jLongToCKULong(jMaxObjectCount);
399 ckpObjectHandleArray = (CK_OBJECT_HANDLE_PTR) malloc(sizeof(CK_OBJECT_HANDLE) * ckMaxObjectLength);
400
401 rv = (*ckpFunctions->C_FindObjects)(ckSessionHandle, ckpObjectHandleArray, ckMaxObjectLength, &ckActualObjectCount);
402
403 jObjectHandleArray = ckULongArrayToJLongArray(env, ckpObjectHandleArray, ckActualObjectCount);
404 free(ckpObjectHandleArray);
405
406 if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; }
407
408 return jObjectHandleArray ;
409}
410#endif
411
412#ifdef P11_ENABLE_C_FINDOBJECTSFINAL
413/*
414 * Class: sun_security_pkcs11_wrapper_PKCS11
415 * Method: C_FindObjectsFinal
416 * Signature: (J)V
417 * Parametermapping: *PKCS11*
418 * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
419 */
420JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1FindObjectsFinal
421 (JNIEnv *env, jobject obj, jlong jSessionHandle)
422{
423 CK_SESSION_HANDLE ckSessionHandle;
424 CK_RV rv;
425
426 CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
427 if (ckpFunctions == NULL) { return; }
428
429 ckSessionHandle = jLongToCKULong(jSessionHandle);
430 rv = (*ckpFunctions->C_FindObjectsFinal)(ckSessionHandle);
431 if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
432}
433#endif