2
|
1 |
/*
|
|
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 |
/* declare file private functions */
|
|
56 |
|
|
57 |
ModuleData * getModuleEntry(JNIEnv *env, jobject pkcs11Implementation);
|
|
58 |
int isModulePresent(JNIEnv *env, jobject pkcs11Implementation);
|
|
59 |
void removeAllModuleEntries(JNIEnv *env);
|
|
60 |
|
|
61 |
|
|
62 |
/* ************************************************************************** */
|
|
63 |
/* Functions for keeping track of currently active and loaded modules */
|
|
64 |
/* ************************************************************************** */
|
|
65 |
|
|
66 |
|
|
67 |
/*
|
|
68 |
* Create a new object for locking.
|
|
69 |
*/
|
|
70 |
jobject createLockObject(JNIEnv *env) {
|
|
71 |
jclass jObjectClass;
|
|
72 |
jobject jLockObject;
|
|
73 |
jmethodID jConstructor;
|
|
74 |
|
|
75 |
jObjectClass = (*env)->FindClass(env, "java/lang/Object");
|
|
76 |
assert(jObjectClass != 0);
|
|
77 |
jConstructor = (*env)->GetMethodID(env, jObjectClass, "<init>", "()V");
|
|
78 |
assert(jConstructor != 0);
|
|
79 |
jLockObject = (*env)->NewObject(env, jObjectClass, jConstructor);
|
|
80 |
assert(jLockObject != 0);
|
|
81 |
jLockObject = (*env)->NewGlobalRef(env, jLockObject);
|
|
82 |
|
|
83 |
return jLockObject ;
|
|
84 |
}
|
|
85 |
|
|
86 |
/*
|
|
87 |
* Create a new object for locking.
|
|
88 |
*/
|
|
89 |
void destroyLockObject(JNIEnv *env, jobject jLockObject) {
|
|
90 |
if (jLockObject != NULL) {
|
|
91 |
(*env)->DeleteGlobalRef(env, jLockObject);
|
|
92 |
}
|
|
93 |
}
|
|
94 |
|
|
95 |
/*
|
|
96 |
* Add the given pkcs11Implementation object to the list of present modules.
|
|
97 |
* Attach the given data to the entry. If the given pkcs11Implementation is
|
|
98 |
* already in the lsit, just override its old module data with the new one.
|
|
99 |
* None of the arguments can be NULL. If one of the arguments is NULL, this
|
|
100 |
* function does nothing.
|
|
101 |
*/
|
|
102 |
void putModuleEntry(JNIEnv *env, jobject pkcs11Implementation, ModuleData *moduleData) {
|
|
103 |
if (pkcs11Implementation == NULL_PTR) {
|
|
104 |
return ;
|
|
105 |
}
|
|
106 |
if (moduleData == NULL) {
|
|
107 |
return ;
|
|
108 |
}
|
|
109 |
(*env)->SetLongField(env, pkcs11Implementation, pNativeDataID, (jlong)moduleData);
|
|
110 |
}
|
|
111 |
|
|
112 |
|
|
113 |
/*
|
|
114 |
* Get the module data of the entry for the given pkcs11Implementation. Returns
|
|
115 |
* NULL, if the pkcs11Implementation is not in the list.
|
|
116 |
*/
|
|
117 |
ModuleData * getModuleEntry(JNIEnv *env, jobject pkcs11Implementation) {
|
|
118 |
jlong jData;
|
|
119 |
if (pkcs11Implementation == NULL) {
|
|
120 |
return NULL;
|
|
121 |
}
|
|
122 |
jData = (*env)->GetLongField(env, pkcs11Implementation, pNativeDataID);
|
|
123 |
return (ModuleData*)jData;
|
|
124 |
}
|
|
125 |
|
|
126 |
CK_FUNCTION_LIST_PTR getFunctionList(JNIEnv *env, jobject pkcs11Implementation) {
|
|
127 |
ModuleData *moduleData;
|
|
128 |
CK_FUNCTION_LIST_PTR ckpFunctions;
|
|
129 |
|
|
130 |
moduleData = getModuleEntry(env, pkcs11Implementation);
|
|
131 |
if (moduleData == NULL) {
|
|
132 |
throwDisconnectedRuntimeException(env);
|
|
133 |
return NULL;
|
|
134 |
}
|
|
135 |
ckpFunctions = moduleData->ckFunctionListPtr;
|
|
136 |
return ckpFunctions;
|
|
137 |
}
|
|
138 |
|
|
139 |
|
|
140 |
/*
|
|
141 |
* Returns 1, if the given pkcs11Implementation is in the list.
|
|
142 |
* 0, otherwise.
|
|
143 |
*/
|
|
144 |
int isModulePresent(JNIEnv *env, jobject pkcs11Implementation) {
|
|
145 |
int present;
|
|
146 |
|
|
147 |
ModuleData *moduleData = getModuleEntry(env, pkcs11Implementation);
|
|
148 |
|
|
149 |
present = (moduleData != NULL) ? 1 : 0;
|
|
150 |
|
|
151 |
return present ;
|
|
152 |
}
|
|
153 |
|
|
154 |
|
|
155 |
/*
|
|
156 |
* Removes the entry for the given pkcs11Implementation from the list. Returns
|
|
157 |
* the module's data, after the node was removed. If this function returns NULL
|
|
158 |
* the pkcs11Implementation was not in the list.
|
|
159 |
*/
|
|
160 |
ModuleData * removeModuleEntry(JNIEnv *env, jobject pkcs11Implementation) {
|
|
161 |
ModuleData *moduleData = getModuleEntry(env, pkcs11Implementation);
|
|
162 |
if (moduleData == NULL) {
|
|
163 |
return NULL;
|
|
164 |
}
|
|
165 |
(*env)->SetLongField(env, pkcs11Implementation, pNativeDataID, 0);
|
|
166 |
return moduleData;
|
|
167 |
}
|
|
168 |
|
|
169 |
/*
|
|
170 |
* Removes all present entries from the list of modules and frees all
|
|
171 |
* associated resources. This function is used for clean-up.
|
|
172 |
*/
|
|
173 |
void removeAllModuleEntries(JNIEnv *env) {
|
|
174 |
/* XXX empty */
|
|
175 |
}
|
|
176 |
|
|
177 |
/* ************************************************************************** */
|
|
178 |
/* Below there follow the helper functions to support conversions between */
|
|
179 |
/* Java and Cryptoki types */
|
|
180 |
/* ************************************************************************** */
|
|
181 |
|
|
182 |
/*
|
|
183 |
* function to convert a PKCS#11 return value into a PKCS#11Exception
|
|
184 |
*
|
|
185 |
* This function generates a PKCS#11Exception with the returnValue as the errorcode
|
|
186 |
* if the returnValue is not CKR_OK. The functin returns 0, if the returnValue is
|
|
187 |
* CKR_OK. Otherwise, it returns the returnValue as a jLong.
|
|
188 |
*
|
|
189 |
* @param env - used to call JNI funktions and to get the Exception class
|
|
190 |
* @param returnValue - of the PKCS#11 function
|
|
191 |
*/
|
|
192 |
jlong ckAssertReturnValueOK(JNIEnv *env, CK_RV returnValue)
|
|
193 |
{
|
|
194 |
jclass jPKCS11ExceptionClass;
|
|
195 |
jmethodID jConstructor;
|
|
196 |
jthrowable jPKCS11Exception;
|
|
197 |
jlong jErrorCode;
|
|
198 |
|
|
199 |
if (returnValue == CKR_OK) {
|
|
200 |
return 0L ;
|
|
201 |
} else {
|
|
202 |
jPKCS11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION);
|
|
203 |
assert(jPKCS11ExceptionClass != 0);
|
|
204 |
jConstructor = (*env)->GetMethodID(env, jPKCS11ExceptionClass, "<init>", "(J)V");
|
|
205 |
assert(jConstructor != 0);
|
|
206 |
jErrorCode = ckULongToJLong(returnValue);
|
|
207 |
jPKCS11Exception = (jthrowable) (*env)->NewObject(env, jPKCS11ExceptionClass, jConstructor, jErrorCode);
|
|
208 |
(*env)->Throw(env, jPKCS11Exception);
|
|
209 |
return jErrorCode ;
|
|
210 |
}
|
|
211 |
}
|
|
212 |
|
|
213 |
/*
|
|
214 |
* this function simply throws a FileNotFoundException
|
|
215 |
*
|
|
216 |
* @param env Used to call JNI funktions and to get the Exception class.
|
|
217 |
* @param jmessage The message string of the Exception object.
|
|
218 |
*/
|
|
219 |
void throwFileNotFoundException(JNIEnv *env, jstring jmessage)
|
|
220 |
{
|
|
221 |
jclass jFileNotFoundExceptionClass;
|
|
222 |
jmethodID jConstructor;
|
|
223 |
jthrowable jFileNotFoundException;
|
|
224 |
|
|
225 |
jFileNotFoundExceptionClass = (*env)->FindClass(env, CLASS_FILE_NOT_FOUND_EXCEPTION);
|
|
226 |
assert(jFileNotFoundExceptionClass != 0);
|
|
227 |
|
|
228 |
jConstructor = (*env)->GetMethodID(env, jFileNotFoundExceptionClass, "<init>", "(Ljava/lang/String;)V");
|
|
229 |
assert(jConstructor != 0);
|
|
230 |
jFileNotFoundException = (jthrowable) (*env)->NewObject(env, jFileNotFoundExceptionClass, jConstructor, jmessage);
|
|
231 |
(*env)->Throw(env, jFileNotFoundException);
|
|
232 |
}
|
|
233 |
|
|
234 |
/*
|
|
235 |
* this function simply throws an IOException
|
|
236 |
*
|
|
237 |
* @param env Used to call JNI funktions and to get the Exception class.
|
|
238 |
* @param message The message string of the Exception object.
|
|
239 |
*/
|
|
240 |
void throwIOException(JNIEnv *env, const char * message)
|
|
241 |
{
|
|
242 |
jclass jIOExceptionClass;
|
|
243 |
|
|
244 |
jIOExceptionClass = (*env)->FindClass(env, CLASS_IO_EXCEPTION);
|
|
245 |
assert(jIOExceptionClass != 0);
|
|
246 |
|
|
247 |
(*env)->ThrowNew(env, jIOExceptionClass, message);
|
|
248 |
}
|
|
249 |
|
|
250 |
/*
|
|
251 |
* this function simply throws an IOException and takes a unicode
|
|
252 |
* messge.
|
|
253 |
*
|
|
254 |
* @param env Used to call JNI funktions and to get the Exception class.
|
|
255 |
* @param message The unicode message string of the Exception object.
|
|
256 |
*/
|
|
257 |
void throwIOExceptionUnicodeMessage(JNIEnv *env, const short *message)
|
|
258 |
{
|
|
259 |
jclass jIOExceptionClass;
|
|
260 |
jmethodID jConstructor;
|
|
261 |
jthrowable jIOException;
|
|
262 |
jstring jmessage;
|
|
263 |
jsize length;
|
|
264 |
short *currentCharacter;
|
|
265 |
|
|
266 |
jIOExceptionClass = (*env)->FindClass(env, CLASS_IO_EXCEPTION);
|
|
267 |
assert(jIOExceptionClass != 0);
|
|
268 |
|
|
269 |
length = 0;
|
|
270 |
if (message != NULL) {
|
|
271 |
currentCharacter = (short *) message;
|
|
272 |
while (*(currentCharacter++) != 0) length++;
|
|
273 |
}
|
|
274 |
|
|
275 |
jmessage = (*env)->NewString(env, (const jchar *)message, length);
|
|
276 |
|
|
277 |
jConstructor = (*env)->GetMethodID(env, jIOExceptionClass, "<init>", "(Ljava/lang/String;)V");
|
|
278 |
assert(jConstructor != 0);
|
|
279 |
jIOException = (jthrowable) (*env)->NewObject(env, jIOExceptionClass, jConstructor, jmessage);
|
|
280 |
(*env)->Throw(env, jIOException);
|
|
281 |
}
|
|
282 |
|
|
283 |
/*
|
|
284 |
* This function simply throws a PKCS#11RuntimeException with the given
|
|
285 |
* string as its message. If the message is NULL, the exception is created
|
|
286 |
* using the default constructor.
|
|
287 |
*
|
|
288 |
* @param env Used to call JNI funktions and to get the Exception class.
|
|
289 |
* @param jmessage The message string of the Exception object.
|
|
290 |
*/
|
|
291 |
void throwPKCS11RuntimeException(JNIEnv *env, jstring jmessage)
|
|
292 |
{
|
|
293 |
jclass jPKCS11RuntimeExceptionClass;
|
|
294 |
jmethodID jConstructor;
|
|
295 |
jthrowable jPKCS11RuntimeException;
|
|
296 |
|
|
297 |
jPKCS11RuntimeExceptionClass = (*env)->FindClass(env, CLASS_PKCS11RUNTIMEEXCEPTION);
|
|
298 |
assert(jPKCS11RuntimeExceptionClass != 0);
|
|
299 |
|
|
300 |
if (jmessage == NULL) {
|
|
301 |
jConstructor = (*env)->GetMethodID(env, jPKCS11RuntimeExceptionClass, "<init>", "()V");
|
|
302 |
assert(jConstructor != 0);
|
|
303 |
jPKCS11RuntimeException = (jthrowable) (*env)->NewObject(env, jPKCS11RuntimeExceptionClass, jConstructor);
|
|
304 |
(*env)->Throw(env, jPKCS11RuntimeException);
|
|
305 |
} else {
|
|
306 |
jConstructor = (*env)->GetMethodID(env, jPKCS11RuntimeExceptionClass, "<init>", "(Ljava/lang/String;)V");
|
|
307 |
assert(jConstructor != 0);
|
|
308 |
jPKCS11RuntimeException = (jthrowable) (*env)->NewObject(env, jPKCS11RuntimeExceptionClass, jConstructor, jmessage);
|
|
309 |
(*env)->Throw(env, jPKCS11RuntimeException);
|
|
310 |
}
|
|
311 |
}
|
|
312 |
|
|
313 |
/*
|
|
314 |
* This function simply throws a PKCS#11RuntimeException. The message says that
|
|
315 |
* the object is not connected to the module.
|
|
316 |
*
|
|
317 |
* @param env Used to call JNI funktions and to get the Exception class.
|
|
318 |
*/
|
|
319 |
void throwDisconnectedRuntimeException(JNIEnv *env)
|
|
320 |
{
|
|
321 |
jstring jExceptionMessage = (*env)->NewStringUTF(env, "This object is not connected to a module.");
|
|
322 |
|
|
323 |
throwPKCS11RuntimeException(env, jExceptionMessage);
|
|
324 |
}
|
|
325 |
|
|
326 |
/*
|
|
327 |
* the following functions convert Java arrays to PKCS#11 array pointers and
|
|
328 |
* their array length and vice versa
|
|
329 |
*
|
|
330 |
* void j<Type>ArrayToCK<Type>Array(JNIEnv *env,
|
|
331 |
* const j<Type>Array jArray,
|
|
332 |
* CK_<Type>_PTR *ckpArray,
|
|
333 |
* CK_ULONG_PTR ckLength);
|
|
334 |
*
|
|
335 |
* j<Type>Array ck<Type>ArrayToJ<Type>Array(JNIEnv *env,
|
|
336 |
* const CK_<Type>_PTR ckpArray,
|
|
337 |
* CK_ULONG ckLength);
|
|
338 |
*
|
|
339 |
* PKCS#11 arrays consist always of a pointer to the beginning of the array and
|
|
340 |
* the array length whereas Java arrays carry their array length.
|
|
341 |
*
|
|
342 |
* The Functions to convert a Java array to a PKCS#11 array are void functions.
|
|
343 |
* Their arguments are the Java array object to convert, the reference to the
|
|
344 |
* array pointer, where the new PKCS#11 array should be stored and the reference
|
|
345 |
* to the array length where the PKCS#11 array length should be stored. These two
|
|
346 |
* references must not be NULL_PTR.
|
|
347 |
*
|
|
348 |
* The functions first obtain the array length of the Java array and then allocate
|
|
349 |
* the memory for the PKCS#11 array and set the array length. Then each element
|
|
350 |
* gets converted depending on their type. After use the allocated memory of the
|
|
351 |
* PKCS#11 array has to be explicitly freed.
|
|
352 |
*
|
|
353 |
* The Functions to convert a PKCS#11 array to a Java array get the PKCS#11 array
|
|
354 |
* pointer and the array length and they return the new Java array object. The
|
|
355 |
* Java array does not need to get freed after use.
|
|
356 |
*/
|
|
357 |
|
|
358 |
/*
|
|
359 |
* converts a jbooleanArray to a CK_BBOOL array. The allocated memory has to be freed after use!
|
|
360 |
*
|
|
361 |
* @param env - used to call JNI funktions to get the array informtaion
|
|
362 |
* @param jArray - the Java array to convert
|
|
363 |
* @param ckpArray - the reference, where the pointer to the new CK_BBOOL array will be stored
|
|
364 |
* @param ckpLength - the reference, where the array length will be stored
|
|
365 |
*/
|
|
366 |
void jBooleanArrayToCKBBoolArray(JNIEnv *env, const jbooleanArray jArray, CK_BBOOL **ckpArray, CK_ULONG_PTR ckpLength)
|
|
367 |
{
|
|
368 |
jboolean* jpTemp;
|
|
369 |
CK_ULONG i;
|
|
370 |
|
|
371 |
if(jArray == NULL) {
|
|
372 |
*ckpArray = NULL_PTR;
|
|
373 |
*ckpLength = 0L;
|
|
374 |
return;
|
|
375 |
}
|
|
376 |
*ckpLength = (*env)->GetArrayLength(env, jArray);
|
|
377 |
jpTemp = (jboolean*) malloc((*ckpLength) * sizeof(jboolean));
|
|
378 |
(*env)->GetBooleanArrayRegion(env, jArray, 0, *ckpLength, jpTemp);
|
|
379 |
*ckpArray = (CK_BBOOL*) malloc ((*ckpLength) * sizeof(CK_BBOOL));
|
|
380 |
for (i=0; i<(*ckpLength); i++) {
|
|
381 |
(*ckpArray)[i] = jBooleanToCKBBool(jpTemp[i]);
|
|
382 |
}
|
|
383 |
free(jpTemp);
|
|
384 |
}
|
|
385 |
|
|
386 |
/*
|
|
387 |
* converts a jbyteArray to a CK_BYTE array. The allocated memory has to be freed after use!
|
|
388 |
*
|
|
389 |
* @param env - used to call JNI funktions to get the array informtaion
|
|
390 |
* @param jArray - the Java array to convert
|
|
391 |
* @param ckpArray - the reference, where the pointer to the new CK_BYTE array will be stored
|
|
392 |
* @param ckpLength - the reference, where the array length will be stored
|
|
393 |
*/
|
|
394 |
void jByteArrayToCKByteArray(JNIEnv *env, const jbyteArray jArray, CK_BYTE_PTR *ckpArray, CK_ULONG_PTR ckpLength)
|
|
395 |
{
|
|
396 |
jbyte* jpTemp;
|
|
397 |
CK_ULONG i;
|
|
398 |
|
|
399 |
if(jArray == NULL) {
|
|
400 |
*ckpArray = NULL_PTR;
|
|
401 |
*ckpLength = 0L;
|
|
402 |
return;
|
|
403 |
}
|
|
404 |
*ckpLength = (*env)->GetArrayLength(env, jArray);
|
|
405 |
jpTemp = (jbyte*) malloc((*ckpLength) * sizeof(jbyte));
|
|
406 |
(*env)->GetByteArrayRegion(env, jArray, 0, *ckpLength, jpTemp);
|
|
407 |
|
|
408 |
/* if CK_BYTE is the same size as jbyte, we save an additional copy */
|
|
409 |
if (sizeof(CK_BYTE) == sizeof(jbyte)) {
|
|
410 |
*ckpArray = (CK_BYTE_PTR) jpTemp;
|
|
411 |
} else {
|
|
412 |
*ckpArray = (CK_BYTE_PTR) malloc ((*ckpLength) * sizeof(CK_BYTE));
|
|
413 |
for (i=0; i<(*ckpLength); i++) {
|
|
414 |
(*ckpArray)[i] = jByteToCKByte(jpTemp[i]);
|
|
415 |
}
|
|
416 |
free(jpTemp);
|
|
417 |
}
|
|
418 |
}
|
|
419 |
|
|
420 |
/*
|
|
421 |
* converts a jlongArray to a CK_ULONG array. The allocated memory has to be freed after use!
|
|
422 |
*
|
|
423 |
* @param env - used to call JNI funktions to get the array informtaion
|
|
424 |
* @param jArray - the Java array to convert
|
|
425 |
* @param ckpArray - the reference, where the pointer to the new CK_ULONG array will be stored
|
|
426 |
* @param ckpLength - the reference, where the array length will be stored
|
|
427 |
*/
|
|
428 |
void jLongArrayToCKULongArray(JNIEnv *env, const jlongArray jArray, CK_ULONG_PTR *ckpArray, CK_ULONG_PTR ckpLength)
|
|
429 |
{
|
|
430 |
jlong* jTemp;
|
|
431 |
CK_ULONG i;
|
|
432 |
|
|
433 |
if(jArray == NULL) {
|
|
434 |
*ckpArray = NULL_PTR;
|
|
435 |
*ckpLength = 0L;
|
|
436 |
return;
|
|
437 |
}
|
|
438 |
*ckpLength = (*env)->GetArrayLength(env, jArray);
|
|
439 |
jTemp = (jlong*) malloc((*ckpLength) * sizeof(jlong));
|
|
440 |
(*env)->GetLongArrayRegion(env, jArray, 0, *ckpLength, jTemp);
|
|
441 |
*ckpArray = (CK_ULONG_PTR) malloc (*ckpLength * sizeof(CK_ULONG));
|
|
442 |
for (i=0; i<(*ckpLength); i++) {
|
|
443 |
(*ckpArray)[i] = jLongToCKULong(jTemp[i]);
|
|
444 |
}
|
|
445 |
free(jTemp);
|
|
446 |
}
|
|
447 |
|
|
448 |
/*
|
|
449 |
* converts a jcharArray to a CK_CHAR array. The allocated memory has to be freed after use!
|
|
450 |
*
|
|
451 |
* @param env - used to call JNI funktions to get the array informtaion
|
|
452 |
* @param jArray - the Java array to convert
|
|
453 |
* @param ckpArray - the reference, where the pointer to the new CK_CHAR array will be stored
|
|
454 |
* @param ckpLength - the reference, where the array length will be stored
|
|
455 |
*/
|
|
456 |
void jCharArrayToCKCharArray(JNIEnv *env, const jcharArray jArray, CK_CHAR_PTR *ckpArray, CK_ULONG_PTR ckpLength)
|
|
457 |
{
|
|
458 |
jchar* jpTemp;
|
|
459 |
CK_ULONG i;
|
|
460 |
|
|
461 |
if(jArray == NULL) {
|
|
462 |
*ckpArray = NULL_PTR;
|
|
463 |
*ckpLength = 0L;
|
|
464 |
return;
|
|
465 |
}
|
|
466 |
*ckpLength = (*env)->GetArrayLength(env, jArray);
|
|
467 |
jpTemp = (jchar*) malloc((*ckpLength) * sizeof(jchar));
|
|
468 |
(*env)->GetCharArrayRegion(env, jArray, 0, *ckpLength, jpTemp);
|
|
469 |
*ckpArray = (CK_CHAR_PTR) malloc (*ckpLength * sizeof(CK_CHAR));
|
|
470 |
for (i=0; i<(*ckpLength); i++) {
|
|
471 |
(*ckpArray)[i] = jCharToCKChar(jpTemp[i]);
|
|
472 |
}
|
|
473 |
free(jpTemp);
|
|
474 |
}
|
|
475 |
|
|
476 |
/*
|
|
477 |
* converts a jcharArray to a CK_UTF8CHAR array. The allocated memory has to be freed after use!
|
|
478 |
*
|
|
479 |
* @param env - used to call JNI funktions to get the array informtaion
|
|
480 |
* @param jArray - the Java array to convert
|
|
481 |
* @param ckpArray - the reference, where the pointer to the new CK_UTF8CHAR array will be stored
|
|
482 |
* @param ckpLength - the reference, where the array length will be stored
|
|
483 |
*/
|
|
484 |
void jCharArrayToCKUTF8CharArray(JNIEnv *env, const jcharArray jArray, CK_UTF8CHAR_PTR *ckpArray, CK_ULONG_PTR ckpLength)
|
|
485 |
{
|
|
486 |
jchar* jTemp;
|
|
487 |
CK_ULONG i;
|
|
488 |
|
|
489 |
if(jArray == NULL) {
|
|
490 |
*ckpArray = NULL_PTR;
|
|
491 |
*ckpLength = 0L;
|
|
492 |
return;
|
|
493 |
}
|
|
494 |
*ckpLength = (*env)->GetArrayLength(env, jArray);
|
|
495 |
jTemp = (jchar*) malloc((*ckpLength) * sizeof(jchar));
|
|
496 |
(*env)->GetCharArrayRegion(env, jArray, 0, *ckpLength, jTemp);
|
|
497 |
*ckpArray = (CK_UTF8CHAR_PTR) malloc (*ckpLength * sizeof(CK_UTF8CHAR));
|
|
498 |
for (i=0; i<(*ckpLength); i++) {
|
|
499 |
(*ckpArray)[i] = jCharToCKUTF8Char(jTemp[i]);
|
|
500 |
}
|
|
501 |
free(jTemp);
|
|
502 |
}
|
|
503 |
|
|
504 |
/*
|
|
505 |
* converts a jstring to a CK_CHAR array. The allocated memory has to be freed after use!
|
|
506 |
*
|
|
507 |
* @param env - used to call JNI funktions to get the array informtaion
|
|
508 |
* @param jArray - the Java array to convert
|
|
509 |
* @param ckpArray - the reference, where the pointer to the new CK_CHAR array will be stored
|
|
510 |
* @param ckpLength - the reference, where the array length will be stored
|
|
511 |
*/
|
|
512 |
void jStringToCKUTF8CharArray(JNIEnv *env, const jstring jArray, CK_UTF8CHAR_PTR *ckpArray, CK_ULONG_PTR ckpLength)
|
|
513 |
{
|
|
514 |
const char* pCharArray;
|
|
515 |
jboolean isCopy;
|
|
516 |
|
|
517 |
if(jArray == NULL) {
|
|
518 |
*ckpArray = NULL_PTR;
|
|
519 |
*ckpLength = 0L;
|
|
520 |
return;
|
|
521 |
}
|
|
522 |
|
|
523 |
pCharArray = (*env)->GetStringUTFChars(env, jArray, &isCopy);
|
|
524 |
*ckpLength = strlen(pCharArray);
|
|
525 |
*ckpArray = (CK_UTF8CHAR_PTR) malloc((*ckpLength + 1) * sizeof(CK_UTF8CHAR));
|
|
526 |
strcpy((char*)*ckpArray, pCharArray);
|
|
527 |
(*env)->ReleaseStringUTFChars(env, (jstring) jArray, pCharArray);
|
|
528 |
}
|
|
529 |
|
|
530 |
/*
|
|
531 |
* converts a jobjectArray with Java Attributes to a CK_ATTRIBUTE array. The allocated memory
|
|
532 |
* has to be freed after use!
|
|
533 |
*
|
|
534 |
* @param env - used to call JNI funktions to get the array informtaion
|
|
535 |
* @param jArray - the Java Attribute array (template) to convert
|
|
536 |
* @param ckpArray - the reference, where the pointer to the new CK_ATTRIBUTE array will be
|
|
537 |
* stored
|
|
538 |
* @param ckpLength - the reference, where the array length will be stored
|
|
539 |
*/
|
|
540 |
void jAttributeArrayToCKAttributeArray(JNIEnv *env, jobjectArray jArray, CK_ATTRIBUTE_PTR *ckpArray, CK_ULONG_PTR ckpLength)
|
|
541 |
{
|
|
542 |
CK_ULONG i;
|
|
543 |
jlong jLength;
|
|
544 |
jobject jAttribute;
|
|
545 |
|
|
546 |
TRACE0("\nDEBUG: jAttributeArrayToCKAttributeArray");
|
|
547 |
if (jArray == NULL) {
|
|
548 |
*ckpArray = NULL_PTR;
|
|
549 |
*ckpLength = 0L;
|
|
550 |
return;
|
|
551 |
}
|
|
552 |
jLength = (*env)->GetArrayLength(env, jArray);
|
|
553 |
*ckpLength = jLongToCKULong(jLength);
|
|
554 |
*ckpArray = (CK_ATTRIBUTE_PTR) malloc(*ckpLength * sizeof(CK_ATTRIBUTE));
|
|
555 |
TRACE1(", converting %d attibutes", jLength);
|
|
556 |
for (i=0; i<(*ckpLength); i++) {
|
|
557 |
TRACE1(", getting %d. attibute", i);
|
|
558 |
jAttribute = (*env)->GetObjectArrayElement(env, jArray, i);
|
|
559 |
TRACE1(", jAttribute = %d", jAttribute);
|
|
560 |
TRACE1(", converting %d. attibute", i);
|
|
561 |
(*ckpArray)[i] = jAttributeToCKAttribute(env, jAttribute);
|
|
562 |
}
|
|
563 |
TRACE0("FINISHED\n");
|
|
564 |
}
|
|
565 |
|
|
566 |
/*
|
|
567 |
* converts a jobjectArray to a CK_VOID_PTR array. The allocated memory has to be freed after
|
|
568 |
* use!
|
|
569 |
* NOTE: this function does not work and is not used yet
|
|
570 |
*
|
|
571 |
* @param env - used to call JNI funktions to get the array informtaion
|
|
572 |
* @param jArray - the Java object array to convert
|
|
573 |
* @param ckpArray - the reference, where the pointer to the new CK_VOID_PTR array will be stored
|
|
574 |
* @param ckpLength - the reference, where the array length will be stored
|
|
575 |
*/
|
|
576 |
/*
|
|
577 |
void jObjectArrayToCKVoidPtrArray(JNIEnv *env, const jobjectArray jArray, CK_VOID_PTR_PTR *ckpArray, CK_ULONG_PTR ckpLength)
|
|
578 |
{
|
|
579 |
jobject jTemp;
|
|
580 |
CK_ULONG i;
|
|
581 |
|
|
582 |
if(jArray == NULL) {
|
|
583 |
*ckpArray = NULL_PTR;
|
|
584 |
*ckpLength = 0L;
|
|
585 |
return;
|
|
586 |
}
|
|
587 |
*ckpLength = (*env)->GetArrayLength(env, jArray);
|
|
588 |
*ckpArray = (CK_VOID_PTR_PTR) malloc (*ckpLength * sizeof(CK_VOID_PTR));
|
|
589 |
for (i=0; i<(*ckpLength); i++) {
|
|
590 |
jTemp = (*env)->GetObjectArrayElement(env, jArray, i);
|
|
591 |
(*ckpArray)[i] = jObjectToCKVoidPtr(jTemp);
|
|
592 |
}
|
|
593 |
free(jTemp);
|
|
594 |
}
|
|
595 |
*/
|
|
596 |
|
|
597 |
/*
|
|
598 |
* converts a CK_BYTE array and its length to a jbyteArray.
|
|
599 |
*
|
|
600 |
* @param env - used to call JNI funktions to create the new Java array
|
|
601 |
* @param ckpArray - the pointer to the CK_BYTE array to convert
|
|
602 |
* @param ckpLength - the length of the array to convert
|
|
603 |
* @return - the new Java byte array
|
|
604 |
*/
|
|
605 |
jbyteArray ckByteArrayToJByteArray(JNIEnv *env, const CK_BYTE_PTR ckpArray, CK_ULONG ckLength)
|
|
606 |
{
|
|
607 |
CK_ULONG i;
|
|
608 |
jbyte* jpTemp;
|
|
609 |
jbyteArray jArray;
|
|
610 |
|
|
611 |
/* if CK_BYTE is the same size as jbyte, we save an additional copy */
|
|
612 |
if (sizeof(CK_BYTE) == sizeof(jbyte)) {
|
|
613 |
jpTemp = (jbyte*) ckpArray;
|
|
614 |
} else {
|
|
615 |
jpTemp = (jbyte*) malloc((ckLength) * sizeof(jbyte));
|
|
616 |
for (i=0; i<ckLength; i++) {
|
|
617 |
jpTemp[i] = ckByteToJByte(ckpArray[i]);
|
|
618 |
}
|
|
619 |
}
|
|
620 |
|
|
621 |
jArray = (*env)->NewByteArray(env, ckULongToJSize(ckLength));
|
|
622 |
(*env)->SetByteArrayRegion(env, jArray, 0, ckULongToJSize(ckLength), jpTemp);
|
|
623 |
|
|
624 |
if (sizeof(CK_BYTE) != sizeof(jbyte)) {
|
|
625 |
free(jpTemp);
|
|
626 |
}
|
|
627 |
|
|
628 |
return jArray ;
|
|
629 |
}
|
|
630 |
|
|
631 |
/*
|
|
632 |
* converts a CK_ULONG array and its length to a jlongArray.
|
|
633 |
*
|
|
634 |
* @param env - used to call JNI funktions to create the new Java array
|
|
635 |
* @param ckpArray - the pointer to the CK_ULONG array to convert
|
|
636 |
* @param ckpLength - the length of the array to convert
|
|
637 |
* @return - the new Java long array
|
|
638 |
*/
|
|
639 |
jlongArray ckULongArrayToJLongArray(JNIEnv *env, const CK_ULONG_PTR ckpArray, CK_ULONG ckLength)
|
|
640 |
{
|
|
641 |
CK_ULONG i;
|
|
642 |
jlong* jpTemp;
|
|
643 |
jlongArray jArray;
|
|
644 |
|
|
645 |
jpTemp = (jlong*) malloc((ckLength) * sizeof(jlong));
|
|
646 |
for (i=0; i<ckLength; i++) {
|
|
647 |
jpTemp[i] = ckLongToJLong(ckpArray[i]);
|
|
648 |
}
|
|
649 |
jArray = (*env)->NewLongArray(env, ckULongToJSize(ckLength));
|
|
650 |
(*env)->SetLongArrayRegion(env, jArray, 0, ckULongToJSize(ckLength), jpTemp);
|
|
651 |
free(jpTemp);
|
|
652 |
|
|
653 |
return jArray ;
|
|
654 |
}
|
|
655 |
|
|
656 |
/*
|
|
657 |
* converts a CK_CHAR array and its length to a jcharArray.
|
|
658 |
*
|
|
659 |
* @param env - used to call JNI funktions to create the new Java array
|
|
660 |
* @param ckpArray - the pointer to the CK_CHAR array to convert
|
|
661 |
* @param ckpLength - the length of the array to convert
|
|
662 |
* @return - the new Java char array
|
|
663 |
*/
|
|
664 |
jcharArray ckCharArrayToJCharArray(JNIEnv *env, const CK_CHAR_PTR ckpArray, CK_ULONG ckLength)
|
|
665 |
{
|
|
666 |
CK_ULONG i;
|
|
667 |
jchar* jpTemp;
|
|
668 |
jcharArray jArray;
|
|
669 |
|
|
670 |
jpTemp = (jchar*) malloc(ckLength * sizeof(jchar));
|
|
671 |
for (i=0; i<ckLength; i++) {
|
|
672 |
jpTemp[i] = ckCharToJChar(ckpArray[i]);
|
|
673 |
}
|
|
674 |
jArray = (*env)->NewCharArray(env, ckULongToJSize(ckLength));
|
|
675 |
(*env)->SetCharArrayRegion(env, jArray, 0, ckULongToJSize(ckLength), jpTemp);
|
|
676 |
free(jpTemp);
|
|
677 |
|
|
678 |
return jArray ;
|
|
679 |
}
|
|
680 |
|
|
681 |
/*
|
|
682 |
* converts a CK_UTF8CHAR array and its length to a jcharArray.
|
|
683 |
*
|
|
684 |
* @param env - used to call JNI funktions to create the new Java array
|
|
685 |
* @param ckpArray - the pointer to the CK_UTF8CHAR array to convert
|
|
686 |
* @param ckpLength - the length of the array to convert
|
|
687 |
* @return - the new Java char array
|
|
688 |
*/
|
|
689 |
jcharArray ckUTF8CharArrayToJCharArray(JNIEnv *env, const CK_UTF8CHAR_PTR ckpArray, CK_ULONG ckLength)
|
|
690 |
{
|
|
691 |
CK_ULONG i;
|
|
692 |
jchar* jpTemp;
|
|
693 |
jcharArray jArray;
|
|
694 |
|
|
695 |
jpTemp = (jchar*) malloc(ckLength * sizeof(jchar));
|
|
696 |
for (i=0; i<ckLength; i++) {
|
|
697 |
jpTemp[i] = ckUTF8CharToJChar(ckpArray[i]);
|
|
698 |
}
|
|
699 |
jArray = (*env)->NewCharArray(env, ckULongToJSize(ckLength));
|
|
700 |
(*env)->SetCharArrayRegion(env, jArray, 0, ckULongToJSize(ckLength), jpTemp);
|
|
701 |
free(jpTemp);
|
|
702 |
|
|
703 |
return jArray ;
|
|
704 |
}
|
|
705 |
|
|
706 |
/*
|
|
707 |
* the following functions convert Java objects to PKCS#11 pointers and the
|
|
708 |
* length in bytes and vice versa
|
|
709 |
*
|
|
710 |
* CK_<Type>_PTR j<Object>ToCK<Type>Ptr(JNIEnv *env, jobject jObject);
|
|
711 |
*
|
|
712 |
* jobject ck<Type>PtrToJ<Object>(JNIEnv *env, const CK_<Type>_PTR ckpValue);
|
|
713 |
*
|
|
714 |
* The functions that convert a Java object to a PKCS#11 pointer first allocate
|
|
715 |
* the memory for the PKCS#11 pointer. Then they set each element corresponding
|
|
716 |
* to the fields in the Java object to convert. After use the allocated memory of
|
|
717 |
* the PKCS#11 pointer has to be explicitly freed.
|
|
718 |
*
|
|
719 |
* The functions to convert a PKCS#11 pointer to a Java object create a new Java
|
|
720 |
* object first and than they set all fields in the object depending on the values
|
|
721 |
* of the type or structure where the PKCS#11 pointer points to.
|
|
722 |
*/
|
|
723 |
|
|
724 |
/*
|
|
725 |
* converts a CK_BBOOL pointer to a Java boolean Object.
|
|
726 |
*
|
|
727 |
* @param env - used to call JNI funktions to create the new Java object
|
|
728 |
* @param ckpValue - the pointer to the CK_BBOOL value
|
|
729 |
* @return - the new Java boolean object with the boolean value
|
|
730 |
*/
|
|
731 |
jobject ckBBoolPtrToJBooleanObject(JNIEnv *env, const CK_BBOOL *ckpValue)
|
|
732 |
{
|
|
733 |
jclass jValueObjectClass;
|
|
734 |
jmethodID jConstructor;
|
|
735 |
jobject jValueObject;
|
|
736 |
jboolean jValue;
|
|
737 |
|
|
738 |
jValueObjectClass = (*env)->FindClass(env, "java/lang/Boolean");
|
|
739 |
assert(jValueObjectClass != 0);
|
|
740 |
jConstructor = (*env)->GetMethodID(env, jValueObjectClass, "<init>", "(Z)V");
|
|
741 |
assert(jConstructor != 0);
|
|
742 |
jValue = ckBBoolToJBoolean(*ckpValue);
|
|
743 |
jValueObject = (*env)->NewObject(env, jValueObjectClass, jConstructor, jValue);
|
|
744 |
assert(jValueObject != 0);
|
|
745 |
|
|
746 |
return jValueObject ;
|
|
747 |
}
|
|
748 |
|
|
749 |
/*
|
|
750 |
* converts a CK_ULONG pointer to a Java long Object.
|
|
751 |
*
|
|
752 |
* @param env - used to call JNI funktions to create the new Java object
|
|
753 |
* @param ckpValue - the pointer to the CK_ULONG value
|
|
754 |
* @return - the new Java long object with the long value
|
|
755 |
*/
|
|
756 |
jobject ckULongPtrToJLongObject(JNIEnv *env, const CK_ULONG_PTR ckpValue)
|
|
757 |
{
|
|
758 |
jclass jValueObjectClass;
|
|
759 |
jmethodID jConstructor;
|
|
760 |
jobject jValueObject;
|
|
761 |
jlong jValue;
|
|
762 |
|
|
763 |
jValueObjectClass = (*env)->FindClass(env, "java/lang/Long");
|
|
764 |
assert(jValueObjectClass != 0);
|
|
765 |
jConstructor = (*env)->GetMethodID(env, jValueObjectClass, "<init>", "(J)V");
|
|
766 |
assert(jConstructor != 0);
|
|
767 |
jValue = ckULongToJLong(*ckpValue);
|
|
768 |
jValueObject = (*env)->NewObject(env, jValueObjectClass, jConstructor, jValue);
|
|
769 |
assert(jValueObject != 0);
|
|
770 |
|
|
771 |
return jValueObject ;
|
|
772 |
}
|
|
773 |
|
|
774 |
/*
|
|
775 |
* converts a Java boolean object into a pointer to a CK_BBOOL value. The memory has to be
|
|
776 |
* freed after use!
|
|
777 |
*
|
|
778 |
* @param env - used to call JNI funktions to get the value out of the Java object
|
|
779 |
* @param jObject - the "java/lang/Boolean" object to convert
|
|
780 |
* @return - the pointer to the new CK_BBOOL value
|
|
781 |
*/
|
|
782 |
CK_BBOOL* jBooleanObjectToCKBBoolPtr(JNIEnv *env, jobject jObject)
|
|
783 |
{
|
|
784 |
jclass jObjectClass;
|
|
785 |
jmethodID jValueMethod;
|
|
786 |
jboolean jValue;
|
|
787 |
CK_BBOOL *ckpValue;
|
|
788 |
|
|
789 |
jObjectClass = (*env)->FindClass(env, "java/lang/Boolean");
|
|
790 |
assert(jObjectClass != 0);
|
|
791 |
jValueMethod = (*env)->GetMethodID(env, jObjectClass, "booleanValue", "()Z");
|
|
792 |
assert(jValueMethod != 0);
|
|
793 |
jValue = (*env)->CallBooleanMethod(env, jObject, jValueMethod);
|
|
794 |
ckpValue = (CK_BBOOL *) malloc(sizeof(CK_BBOOL));
|
|
795 |
*ckpValue = jBooleanToCKBBool(jValue);
|
|
796 |
|
|
797 |
return ckpValue ;
|
|
798 |
}
|
|
799 |
|
|
800 |
/*
|
|
801 |
* converts a Java byte object into a pointer to a CK_BYTE value. The memory has to be
|
|
802 |
* freed after use!
|
|
803 |
*
|
|
804 |
* @param env - used to call JNI funktions to get the value out of the Java object
|
|
805 |
* @param jObject - the "java/lang/Byte" object to convert
|
|
806 |
* @return - the pointer to the new CK_BYTE value
|
|
807 |
*/
|
|
808 |
CK_BYTE_PTR jByteObjectToCKBytePtr(JNIEnv *env, jobject jObject)
|
|
809 |
{
|
|
810 |
jclass jObjectClass;
|
|
811 |
jmethodID jValueMethod;
|
|
812 |
jbyte jValue;
|
|
813 |
CK_BYTE_PTR ckpValue;
|
|
814 |
|
|
815 |
jObjectClass = (*env)->FindClass(env, "java/lang/Byte");
|
|
816 |
assert(jObjectClass != 0);
|
|
817 |
jValueMethod = (*env)->GetMethodID(env, jObjectClass, "byteValue", "()B");
|
|
818 |
assert(jValueMethod != 0);
|
|
819 |
jValue = (*env)->CallByteMethod(env, jObject, jValueMethod);
|
|
820 |
ckpValue = (CK_BYTE_PTR) malloc(sizeof(CK_BYTE));
|
|
821 |
*ckpValue = jByteToCKByte(jValue);
|
|
822 |
|
|
823 |
return ckpValue ;
|
|
824 |
}
|
|
825 |
|
|
826 |
/*
|
|
827 |
* converts a Java integer object into a pointer to a CK_ULONG value. The memory has to be
|
|
828 |
* freed after use!
|
|
829 |
*
|
|
830 |
* @param env - used to call JNI funktions to get the value out of the Java object
|
|
831 |
* @param jObject - the "java/lang/Integer" object to convert
|
|
832 |
* @return - the pointer to the new CK_ULONG value
|
|
833 |
*/
|
|
834 |
CK_ULONG* jIntegerObjectToCKULongPtr(JNIEnv *env, jobject jObject)
|
|
835 |
{
|
|
836 |
jclass jObjectClass;
|
|
837 |
jmethodID jValueMethod;
|
|
838 |
jint jValue;
|
|
839 |
CK_ULONG *ckpValue;
|
|
840 |
|
|
841 |
jObjectClass = (*env)->FindClass(env, "java/lang/Integer");
|
|
842 |
assert(jObjectClass != 0);
|
|
843 |
jValueMethod = (*env)->GetMethodID(env, jObjectClass, "intValue", "()I");
|
|
844 |
assert(jValueMethod != 0);
|
|
845 |
jValue = (*env)->CallIntMethod(env, jObject, jValueMethod);
|
|
846 |
ckpValue = (CK_ULONG *) malloc(sizeof(CK_ULONG));
|
|
847 |
*ckpValue = jLongToCKLong(jValue);
|
|
848 |
|
|
849 |
return ckpValue ;
|
|
850 |
}
|
|
851 |
|
|
852 |
/*
|
|
853 |
* converts a Java long object into a pointer to a CK_ULONG value. The memory has to be
|
|
854 |
* freed after use!
|
|
855 |
*
|
|
856 |
* @param env - used to call JNI funktions to get the value out of the Java object
|
|
857 |
* @param jObject - the "java/lang/Long" object to convert
|
|
858 |
* @return - the pointer to the new CK_ULONG value
|
|
859 |
*/
|
|
860 |
CK_ULONG* jLongObjectToCKULongPtr(JNIEnv *env, jobject jObject)
|
|
861 |
{
|
|
862 |
jclass jObjectClass;
|
|
863 |
jmethodID jValueMethod;
|
|
864 |
jlong jValue;
|
|
865 |
CK_ULONG *ckpValue;
|
|
866 |
|
|
867 |
jObjectClass = (*env)->FindClass(env, "java/lang/Long");
|
|
868 |
assert(jObjectClass != 0);
|
|
869 |
jValueMethod = (*env)->GetMethodID(env, jObjectClass, "longValue", "()J");
|
|
870 |
assert(jValueMethod != 0);
|
|
871 |
jValue = (*env)->CallLongMethod(env, jObject, jValueMethod);
|
|
872 |
ckpValue = (CK_ULONG *) malloc(sizeof(CK_ULONG));
|
|
873 |
*ckpValue = jLongToCKULong(jValue);
|
|
874 |
|
|
875 |
return ckpValue ;
|
|
876 |
}
|
|
877 |
|
|
878 |
/*
|
|
879 |
* converts a Java char object into a pointer to a CK_CHAR value. The memory has to be
|
|
880 |
* freed after use!
|
|
881 |
*
|
|
882 |
* @param env - used to call JNI funktions to get the value out of the Java object
|
|
883 |
* @param jObject - the "java/lang/Char" object to convert
|
|
884 |
* @return - the pointer to the new CK_CHAR value
|
|
885 |
*/
|
|
886 |
CK_CHAR_PTR jCharObjectToCKCharPtr(JNIEnv *env, jobject jObject)
|
|
887 |
{
|
|
888 |
jclass jObjectClass;
|
|
889 |
jmethodID jValueMethod;
|
|
890 |
jchar jValue;
|
|
891 |
CK_CHAR_PTR ckpValue;
|
|
892 |
|
|
893 |
jObjectClass = (*env)->FindClass(env, "java/lang/Char");
|
|
894 |
assert(jObjectClass != 0);
|
|
895 |
jValueMethod = (*env)->GetMethodID(env, jObjectClass, "charValue", "()C");
|
|
896 |
assert(jValueMethod != 0);
|
|
897 |
jValue = (*env)->CallCharMethod(env, jObject, jValueMethod);
|
|
898 |
ckpValue = (CK_CHAR_PTR) malloc(sizeof(CK_CHAR));
|
|
899 |
*ckpValue = jCharToCKChar(jValue);
|
|
900 |
|
|
901 |
return ckpValue ;
|
|
902 |
}
|
|
903 |
|
|
904 |
/*
|
|
905 |
* converts a Java object into a pointer to CK-type or a CK-structure with the length in Bytes.
|
|
906 |
* The memory of *ckpObjectPtr to be freed after use! This function is only used by
|
|
907 |
* jAttributeToCKAttribute by now.
|
|
908 |
*
|
|
909 |
* @param env - used to call JNI funktions to get the Java classes and objects
|
|
910 |
* @param jObject - the Java object to convert
|
|
911 |
* @param ckpObjectPtr - the reference of the new pointer to the new CK-value or CK-structure
|
|
912 |
* @param ckpLength - the reference of the length in bytes of the new CK-value or CK-structure
|
|
913 |
*/
|
|
914 |
void jObjectToPrimitiveCKObjectPtrPtr(JNIEnv *env, jobject jObject, CK_VOID_PTR *ckpObjectPtr, CK_ULONG *ckpLength)
|
|
915 |
{
|
|
916 |
jclass jBooleanClass = (*env)->FindClass(env, "java/lang/Boolean");
|
|
917 |
jclass jByteClass = (*env)->FindClass(env, "java/lang/Byte");
|
|
918 |
jclass jCharacterClass = (*env)->FindClass(env, "java/lang/Character");
|
|
919 |
jclass jClassClass = (*env)->FindClass(env, "java/lang/Class");
|
|
920 |
/* jclass jShortClass = (*env)->FindClass(env, "java/lang/Short"); */
|
|
921 |
jclass jIntegerClass = (*env)->FindClass(env, "java/lang/Integer");
|
|
922 |
jclass jLongClass = (*env)->FindClass(env, "java/lang/Long");
|
|
923 |
/* jclass jFloatClass = (*env)->FindClass(env, "java/lang/Float"); */
|
|
924 |
/* jclass jDoubleClass = (*env)->FindClass(env, "java/lang/Double"); */
|
|
925 |
jclass jDateClass = (*env)->FindClass(env, CLASS_DATE);
|
|
926 |
jclass jStringClass = (*env)->FindClass(env, "java/lang/String");
|
|
927 |
jclass jStringBufferClass = (*env)->FindClass(env, "java/lang/StringBuffer");
|
|
928 |
jclass jBooleanArrayClass = (*env)->FindClass(env, "[Z");
|
|
929 |
jclass jByteArrayClass = (*env)->FindClass(env, "[B");
|
|
930 |
jclass jCharArrayClass = (*env)->FindClass(env, "[C");
|
|
931 |
/* jclass jShortArrayClass = (*env)->FindClass(env, "[S"); */
|
|
932 |
jclass jIntArrayClass = (*env)->FindClass(env, "[I");
|
|
933 |
jclass jLongArrayClass = (*env)->FindClass(env, "[J");
|
|
934 |
/* jclass jFloatArrayClass = (*env)->FindClass(env, "[F"); */
|
|
935 |
/* jclass jDoubleArrayClass = (*env)->FindClass(env, "[D"); */
|
|
936 |
jclass jObjectClass = (*env)->FindClass(env, "java/lang/Object");
|
|
937 |
/* jclass jObjectArrayClass = (*env)->FindClass(env, "[java/lang/Object"); */
|
|
938 |
/* ATTENTION: jObjectArrayClass is always NULL !! */
|
|
939 |
/* CK_ULONG ckArrayLength; */
|
|
940 |
/* CK_VOID_PTR *ckpElementObject; */
|
|
941 |
/* CK_ULONG ckElementLength; */
|
|
942 |
/* CK_ULONG i; */
|
|
943 |
CK_VOID_PTR ckpVoid = *ckpObjectPtr;
|
|
944 |
jmethodID jMethod;
|
|
945 |
jobject jClassObject;
|
|
946 |
jstring jClassNameString;
|
|
947 |
jstring jExceptionMessagePrefix;
|
|
948 |
jobject jExceptionMessageStringBuffer;
|
|
949 |
jstring jExceptionMessage;
|
|
950 |
|
|
951 |
TRACE0("\nDEBUG: jObjectToPrimitiveCKObjectPtrPtr");
|
|
952 |
if (jObject == NULL) {
|
|
953 |
*ckpObjectPtr = NULL;
|
|
954 |
*ckpLength = 0;
|
|
955 |
} else if ((*env)->IsInstanceOf(env, jObject, jLongClass)) {
|
|
956 |
*ckpObjectPtr = jLongObjectToCKULongPtr(env, jObject);
|
|
957 |
*ckpLength = sizeof(CK_ULONG);
|
|
958 |
TRACE1("<converted long value %X>", *((CK_ULONG *) *ckpObjectPtr));
|
|
959 |
} else if ((*env)->IsInstanceOf(env, jObject, jBooleanClass)) {
|
|
960 |
*ckpObjectPtr = jBooleanObjectToCKBBoolPtr(env, jObject);
|
|
961 |
*ckpLength = sizeof(CK_BBOOL);
|
|
962 |
TRACE0(" <converted boolean value ");
|
|
963 |
TRACE0((*((CK_BBOOL *) *ckpObjectPtr) == TRUE) ? "TRUE>" : "FALSE>");
|
|
964 |
} else if ((*env)->IsInstanceOf(env, jObject, jByteArrayClass)) {
|
|
965 |
jByteArrayToCKByteArray(env, jObject, (CK_BYTE_PTR*)ckpObjectPtr, ckpLength);
|
|
966 |
} else if ((*env)->IsInstanceOf(env, jObject, jCharArrayClass)) {
|
|
967 |
jCharArrayToCKUTF8CharArray(env, jObject, (CK_UTF8CHAR_PTR*)ckpObjectPtr, ckpLength);
|
|
968 |
} else if ((*env)->IsInstanceOf(env, jObject, jByteClass)) {
|
|
969 |
*ckpObjectPtr = jByteObjectToCKBytePtr(env, jObject);
|
|
970 |
*ckpLength = sizeof(CK_BYTE);
|
|
971 |
TRACE1("<converted byte value %X>", *((CK_BYTE *) *ckpObjectPtr));
|
|
972 |
} else if ((*env)->IsInstanceOf(env, jObject, jDateClass)) {
|
|
973 |
*ckpObjectPtr = jDateObjectPtrToCKDatePtr(env, jObject);
|
|
974 |
*ckpLength = sizeof(CK_DATE);
|
|
975 |
TRACE3("<converted date value %.4s-%.2s-%.2s>", (*((CK_DATE *) *ckpObjectPtr)).year,
|
|
976 |
(*((CK_DATE *) *ckpObjectPtr)).month,
|
|
977 |
(*((CK_DATE *) *ckpObjectPtr)).day);
|
|
978 |
} else if ((*env)->IsInstanceOf(env, jObject, jCharacterClass)) {
|
|
979 |
*ckpObjectPtr = jCharObjectToCKCharPtr(env, jObject);
|
|
980 |
*ckpLength = sizeof(CK_UTF8CHAR);
|
|
981 |
TRACE1("<converted char value %c>", *((CK_CHAR *) *ckpObjectPtr));
|
|
982 |
} else if ((*env)->IsInstanceOf(env, jObject, jIntegerClass)) {
|
|
983 |
*ckpObjectPtr = jIntegerObjectToCKULongPtr(env, jObject);
|
|
984 |
*ckpLength = sizeof(CK_ULONG);
|
|
985 |
TRACE1("<converted integer value %X>", *((CK_ULONG *) *ckpObjectPtr));
|
|
986 |
} else if ((*env)->IsInstanceOf(env, jObject, jBooleanArrayClass)) {
|
|
987 |
jBooleanArrayToCKBBoolArray(env, jObject, (CK_BBOOL**)ckpObjectPtr, ckpLength);
|
|
988 |
} else if ((*env)->IsInstanceOf(env, jObject, jIntArrayClass)) {
|
|
989 |
jLongArrayToCKULongArray(env, jObject, (CK_ULONG_PTR*)ckpObjectPtr, ckpLength);
|
|
990 |
} else if ((*env)->IsInstanceOf(env, jObject, jLongArrayClass)) {
|
|
991 |
jLongArrayToCKULongArray(env, jObject, (CK_ULONG_PTR*)ckpObjectPtr, ckpLength);
|
|
992 |
} else if ((*env)->IsInstanceOf(env, jObject, jStringClass)) {
|
|
993 |
jStringToCKUTF8CharArray(env, jObject, (CK_UTF8CHAR_PTR*)ckpObjectPtr, ckpLength);
|
|
994 |
|
|
995 |
/* a Java object array is not used by CK_ATTRIBUTE by now... */
|
|
996 |
/* } else if ((*env)->IsInstanceOf(env, jObject, jObjectArrayClass)) {
|
|
997 |
ckArrayLength = (*env)->GetArrayLength(env, (jarray) jObject);
|
|
998 |
ckpObjectPtr = (CK_VOID_PTR_PTR) malloc(sizeof(CK_VOID_PTR) * ckArrayLength);
|
|
999 |
*ckpLength = 0;
|
|
1000 |
for (i = 0; i < ckArrayLength; i++) {
|
|
1001 |
jObjectToPrimitiveCKObjectPtrPtr(env, (*env)->GetObjectArrayElement(env, (jarray) jObject, i),
|
|
1002 |
ckpElementObject, &ckElementLength);
|
|
1003 |
(*ckpObjectPtr)[i] = *ckpElementObject;
|
|
1004 |
*ckpLength += ckElementLength;
|
|
1005 |
}
|
|
1006 |
*/
|
|
1007 |
} else {
|
|
1008 |
/* type of jObject unknown, throw PKCS11RuntimeException */
|
|
1009 |
jMethod = (*env)->GetMethodID(env, jObjectClass, "getClass", "()Ljava/lang/Class;");
|
|
1010 |
assert(jMethod != 0);
|
|
1011 |
jClassObject = (*env)->CallObjectMethod(env, jObject, jMethod);
|
|
1012 |
assert(jClassObject != 0);
|
|
1013 |
jMethod = (*env)->GetMethodID(env, jClassClass, "getName", "()Ljava/lang/String;");
|
|
1014 |
assert(jMethod != 0);
|
|
1015 |
jClassNameString = (jstring)
|
|
1016 |
(*env)->CallObjectMethod(env, jClassObject, jMethod);
|
|
1017 |
assert(jClassNameString != 0);
|
|
1018 |
jExceptionMessagePrefix = (*env)->NewStringUTF(env, "Java object of this class cannot be converted to native PKCS#11 type: ");
|
|
1019 |
jMethod = (*env)->GetMethodID(env, jStringBufferClass, "<init>", "(Ljava/lang/String;)V");
|
|
1020 |
assert(jMethod != 0);
|
|
1021 |
jExceptionMessageStringBuffer = (*env)->NewObject(env, jStringBufferClass, jMethod, jExceptionMessagePrefix);
|
|
1022 |
assert(jClassNameString != 0);
|
|
1023 |
jMethod = (*env)->GetMethodID(env, jStringBufferClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
|
|
1024 |
assert(jMethod != 0);
|
|
1025 |
jExceptionMessage = (jstring)
|
|
1026 |
(*env)->CallObjectMethod(env, jExceptionMessageStringBuffer, jMethod, jClassNameString);
|
|
1027 |
assert(jExceptionMessage != 0);
|
|
1028 |
|
|
1029 |
throwPKCS11RuntimeException(env, jExceptionMessage);
|
|
1030 |
|
|
1031 |
*ckpObjectPtr = NULL;
|
|
1032 |
*ckpLength = 0;
|
|
1033 |
}
|
|
1034 |
|
|
1035 |
TRACE0("FINISHED\n");
|
|
1036 |
}
|