Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_util.c
41149 views
1
/*
2
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. 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
if (jObjectClass == NULL) { return NULL; }
77
jConstructor = (*env)->GetMethodID(env, jObjectClass, "<init>", "()V");
78
if (jConstructor == NULL) { return NULL; }
79
jLockObject = (*env)->NewObject(env, jObjectClass, jConstructor);
80
if (jLockObject == NULL) { return NULL; }
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 list, 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, ptr_to_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*)jlong_to_ptr(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
186
* errorcode. If the returnValue is not CKR_OK. The function returns 0, if the
187
* returnValue is CKR_OK. Otherwise, it returns the returnValue as a jLong.
188
*
189
* @param env - used to call JNI functions and to get the Exception class
190
* @param returnValue - of the PKCS#11 function
191
*/
192
jlong ckAssertReturnValueOK(JNIEnv *env, CK_RV returnValue) {
193
return ckAssertReturnValueOK2(env, returnValue, NULL);
194
}
195
196
/*
197
* function to convert a PKCS#11 return value and additional message into a
198
* PKCS#11Exception
199
*
200
* This function generates a PKCS#11Exception with the returnValue as the
201
* errorcode. If the returnValue is not CKR_OK. The function returns 0, if the
202
* returnValue is CKR_OK. Otherwise, it returns the returnValue as a jLong.
203
*
204
* @param env - used to call JNI functions and to get the Exception class
205
* @param returnValue - of the PKCS#11 function
206
* @param msg - additional message for the generated PKCS11Exception
207
*/
208
jlong ckAssertReturnValueOK2(JNIEnv *env, CK_RV returnValue, const char* msg) {
209
jclass jPKCS11ExceptionClass;
210
jmethodID jConstructor;
211
jthrowable jPKCS11Exception;
212
jlong jErrorCode = 0L;
213
jstring jMsg = NULL;
214
215
if (returnValue != CKR_OK) {
216
jErrorCode = ckULongToJLong(returnValue);
217
jPKCS11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION);
218
if (jPKCS11ExceptionClass != NULL) {
219
jConstructor = (*env)->GetMethodID(env, jPKCS11ExceptionClass,
220
"<init>", "(JLjava/lang/String;)V");
221
if (jConstructor != NULL) {
222
if (msg != NULL) {
223
jMsg = (*env)->NewStringUTF(env, msg);
224
}
225
jPKCS11Exception = (jthrowable) (*env)->NewObject(env,
226
jPKCS11ExceptionClass, jConstructor, jErrorCode, jMsg);
227
if (jPKCS11Exception != NULL) {
228
(*env)->Throw(env, jPKCS11Exception);
229
}
230
}
231
}
232
(*env)->DeleteLocalRef(env, jPKCS11ExceptionClass);
233
}
234
return jErrorCode;
235
}
236
237
238
/*
239
* Throws a Java Exception by name
240
*/
241
void throwByName(JNIEnv *env, const char *name, const char *msg)
242
{
243
jclass cls = (*env)->FindClass(env, name);
244
245
if (cls != 0) /* Otherwise an exception has already been thrown */
246
(*env)->ThrowNew(env, cls, msg);
247
}
248
249
/*
250
* Throws java.lang.OutOfMemoryError
251
*/
252
void throwOutOfMemoryError(JNIEnv *env, const char *msg)
253
{
254
throwByName(env, "java/lang/OutOfMemoryError", msg);
255
}
256
257
/*
258
* Throws java.lang.NullPointerException
259
*/
260
void throwNullPointerException(JNIEnv *env, const char *msg)
261
{
262
throwByName(env, "java/lang/NullPointerException", msg);
263
}
264
265
/*
266
* Throws java.io.IOException
267
*/
268
void throwIOException(JNIEnv *env, const char *msg)
269
{
270
throwByName(env, "java/io/IOException", msg);
271
}
272
273
/*
274
* This function simply throws a PKCS#11RuntimeException with the given
275
* string as its message.
276
*
277
* @param env Used to call JNI functions and to get the Exception class.
278
* @param jmessage The message string of the Exception object.
279
*/
280
void throwPKCS11RuntimeException(JNIEnv *env, const char *message)
281
{
282
throwByName(env, CLASS_PKCS11RUNTIMEEXCEPTION, message);
283
}
284
285
/*
286
* This function simply throws a PKCS#11RuntimeException. The message says that
287
* the object is not connected to the module.
288
*
289
* @param env Used to call JNI functions and to get the Exception class.
290
*/
291
void throwDisconnectedRuntimeException(JNIEnv *env)
292
{
293
throwPKCS11RuntimeException(env, "This object is not connected to a module.");
294
}
295
296
/* This function frees the specified CK_ATTRIBUTE array.
297
*
298
* @param attrPtr pointer to the to-be-freed CK_ATTRIBUTE array.
299
* @param len the length of the array
300
*/
301
void freeCKAttributeArray(CK_ATTRIBUTE_PTR attrPtr, int len) {
302
if (attrPtr != NULL) {
303
int i;
304
for (i=0; i<len; i++) {
305
if (attrPtr[i].pValue != NULL_PTR) {
306
free(attrPtr[i].pValue);
307
}
308
}
309
free(attrPtr);
310
}
311
}
312
313
/* This function frees the specified CK_MECHANISM_PTR pointer and its
314
* pParameter including mechanism-specific memory allocations.
315
*
316
* @param mechPtr pointer to the to-be-freed CK_MECHANISM structure.
317
*/
318
void freeCKMechanismPtr(CK_MECHANISM_PTR mechPtr) {
319
void *tmp;
320
CK_SSL3_MASTER_KEY_DERIVE_PARAMS *sslMkdTmp;
321
CK_SSL3_KEY_MAT_PARAMS* sslKmTmp;
322
CK_TLS12_MASTER_KEY_DERIVE_PARAMS *tlsMkdTmp;
323
CK_TLS12_KEY_MAT_PARAMS* tlsKmTmp;
324
325
if (mechPtr != NULL) {
326
TRACE2("DEBUG freeCKMechanismPtr: free pMech %p (mech 0x%lX)\n",
327
mechPtr, mechPtr->mechanism);
328
if (mechPtr->pParameter != NULL) {
329
tmp = mechPtr->pParameter;
330
switch (mechPtr->mechanism) {
331
case CKM_AES_GCM:
332
if (mechPtr->ulParameterLen == sizeof(CK_GCM_PARAMS_NO_IVBITS)) {
333
TRACE0("[ GCM_PARAMS w/o ulIvBits ]\n");
334
free(((CK_GCM_PARAMS_NO_IVBITS*)tmp)->pIv);
335
free(((CK_GCM_PARAMS_NO_IVBITS*)tmp)->pAAD);
336
} else if (mechPtr->ulParameterLen == sizeof(CK_GCM_PARAMS)) {
337
TRACE0("[ GCM_PARAMS ]\n");
338
free(((CK_GCM_PARAMS*)tmp)->pIv);
339
free(((CK_GCM_PARAMS*)tmp)->pAAD);
340
}
341
break;
342
case CKM_AES_CCM:
343
TRACE0("[ CK_CCM_PARAMS ]\n");
344
free(((CK_CCM_PARAMS*)tmp)->pNonce);
345
free(((CK_CCM_PARAMS*)tmp)->pAAD);
346
break;
347
case CKM_CHACHA20_POLY1305:
348
TRACE0("[ CK_SALSA20_CHACHA20_POLY1305_PARAMS ]\n");
349
free(((CK_SALSA20_CHACHA20_POLY1305_PARAMS*)tmp)->pNonce);
350
free(((CK_SALSA20_CHACHA20_POLY1305_PARAMS*)tmp)->pAAD);
351
break;
352
case CKM_TLS_PRF:
353
case CKM_NSS_TLS_PRF_GENERAL:
354
TRACE0("[ CK_TLS_PRF_PARAMS ]\n");
355
free(((CK_TLS_PRF_PARAMS*)tmp)->pSeed);
356
free(((CK_TLS_PRF_PARAMS*)tmp)->pLabel);
357
free(((CK_TLS_PRF_PARAMS*)tmp)->pulOutputLen);
358
free(((CK_TLS_PRF_PARAMS*)tmp)->pOutput);
359
break;
360
case CKM_SSL3_MASTER_KEY_DERIVE:
361
case CKM_TLS_MASTER_KEY_DERIVE:
362
case CKM_SSL3_MASTER_KEY_DERIVE_DH:
363
case CKM_TLS_MASTER_KEY_DERIVE_DH:
364
sslMkdTmp = tmp;
365
TRACE0("[ CK_SSL3_MASTER_KEY_DERIVE_PARAMS ]\n");
366
free(sslMkdTmp->RandomInfo.pClientRandom);
367
free(sslMkdTmp->RandomInfo.pServerRandom);
368
free(sslMkdTmp->pVersion);
369
break;
370
case CKM_SSL3_KEY_AND_MAC_DERIVE:
371
case CKM_TLS_KEY_AND_MAC_DERIVE:
372
sslKmTmp = tmp;
373
TRACE0("[ CK_SSL3_KEY_MAT_PARAMS ]\n");
374
free(sslKmTmp->RandomInfo.pClientRandom);
375
free(sslKmTmp->RandomInfo.pServerRandom);
376
if (sslKmTmp->pReturnedKeyMaterial != NULL) {
377
free(sslKmTmp->pReturnedKeyMaterial->pIVClient);
378
free(sslKmTmp->pReturnedKeyMaterial->pIVServer);
379
free(sslKmTmp->pReturnedKeyMaterial);
380
}
381
break;
382
case CKM_TLS12_MASTER_KEY_DERIVE:
383
case CKM_TLS12_MASTER_KEY_DERIVE_DH:
384
tlsMkdTmp = tmp;
385
TRACE0("[ CK_TLS12_MASTER_KEY_DERIVE_PARAMS ]\n");
386
free(tlsMkdTmp->RandomInfo.pClientRandom);
387
free(tlsMkdTmp->RandomInfo.pServerRandom);
388
free(tlsMkdTmp->pVersion);
389
break;
390
case CKM_TLS12_KEY_AND_MAC_DERIVE:
391
tlsKmTmp = tmp;
392
TRACE0("[ CK_TLS12_KEY_MAT_PARAMS ]\n");
393
free(tlsKmTmp->RandomInfo.pClientRandom);
394
free(tlsKmTmp->RandomInfo.pServerRandom);
395
if (tlsKmTmp->pReturnedKeyMaterial != NULL) {
396
free(tlsKmTmp->pReturnedKeyMaterial->pIVClient);
397
free(tlsKmTmp->pReturnedKeyMaterial->pIVServer);
398
free(tlsKmTmp->pReturnedKeyMaterial);
399
}
400
break;
401
case CKM_ECDH1_DERIVE:
402
case CKM_ECDH1_COFACTOR_DERIVE:
403
TRACE0("[ CK_ECDH1_DERIVE_PARAMS ]\n");
404
free(((CK_ECDH1_DERIVE_PARAMS *)tmp)->pSharedData);
405
free(((CK_ECDH1_DERIVE_PARAMS *)tmp)->pPublicData);
406
break;
407
case CKM_TLS_MAC:
408
case CKM_AES_CTR:
409
case CKM_RSA_PKCS_PSS:
410
case CKM_CAMELLIA_CTR:
411
// params do not contain pointers
412
break;
413
default:
414
// currently unsupported mechs by SunPKCS11 provider
415
// CKM_RSA_PKCS_OAEP, CKM_ECMQV_DERIVE,
416
// CKM_X9_42_*, CKM_KEA_DERIVE, CKM_RC2_*, CKM_RC5_*,
417
// CKM_SKIPJACK_*, CKM_KEY_WRAP_SET_OAEP, CKM_PKCS5_PBKD2,
418
// PBE mechs, WTLS mechs, CMS mechs,
419
// CKM_EXTRACT_KEY_FROM_KEY, CKM_OTP, CKM_KIP,
420
// CKM_DSA_PARAMETER_GEN?, CKM_GOSTR3410_*
421
// CK_any_CBC_ENCRYPT_DATA?
422
TRACE0("ERROR: UNSUPPORTED CK_MECHANISM\n");
423
break;
424
}
425
TRACE1("\t=> freed param %p\n", tmp);
426
free(tmp);
427
} else {
428
TRACE0("\t=> param NULL\n");
429
}
430
free(mechPtr);
431
TRACE0("FINISHED\n");
432
}
433
}
434
435
/* This function replaces the CK_GCM_PARAMS_NO_IVBITS structure associated
436
* with the specified CK_MECHANISM structure with CK_GCM_PARAMS
437
* structure.
438
*
439
* @param mechPtr pointer to the CK_MECHANISM structure containing
440
* the to-be-converted CK_GCM_PARAMS_NO_IVBITS structure.
441
* @return pointer to the CK_MECHANISM structure containing the
442
* converted CK_GCM_PARAMS structure or NULL if no conversion took place.
443
*/
444
CK_MECHANISM_PTR updateGCMParams(JNIEnv *env, CK_MECHANISM_PTR mechPtr) {
445
CK_GCM_PARAMS* pGcmParams2 = NULL;
446
CK_GCM_PARAMS_NO_IVBITS* pParams = NULL;
447
if ((mechPtr->mechanism == CKM_AES_GCM) &&
448
(mechPtr->pParameter != NULL_PTR) &&
449
(mechPtr->ulParameterLen == sizeof(CK_GCM_PARAMS_NO_IVBITS))) {
450
pGcmParams2 = calloc(1, sizeof(CK_GCM_PARAMS));
451
if (pGcmParams2 == NULL) {
452
throwOutOfMemoryError(env, 0);
453
return NULL;
454
}
455
pParams = (CK_GCM_PARAMS_NO_IVBITS*) mechPtr->pParameter;
456
pGcmParams2->pIv = pParams->pIv;
457
pGcmParams2->ulIvLen = pParams->ulIvLen;
458
pGcmParams2->ulIvBits = (pGcmParams2->ulIvLen << 3);
459
pGcmParams2->pAAD = pParams->pAAD;
460
pGcmParams2->ulAADLen = pParams->ulAADLen;
461
pGcmParams2->ulTagBits = pParams->ulTagBits;
462
TRACE1("DEBUG updateGCMParams: pMech %p\n", mechPtr);
463
TRACE2("\t=> GCM param w/o ulIvBits %p => GCM param %p\n", pParams,
464
pGcmParams2);
465
free(pParams);
466
mechPtr->pParameter = pGcmParams2;
467
mechPtr->ulParameterLen = sizeof(CK_GCM_PARAMS);
468
return mechPtr;
469
} else {
470
TRACE0("DEBUG updateGCMParams: no conversion done\n");
471
}
472
return NULL;
473
}
474
475
/*
476
* the following functions convert Java arrays to PKCS#11 array pointers and
477
* their array length and vice versa
478
*
479
* void j<Type>ArrayToCK<Type>Array(JNIEnv *env,
480
* const j<Type>Array jArray,
481
* CK_<Type>_PTR *ckpArray,
482
* CK_ULONG_PTR ckLength);
483
*
484
* j<Type>Array ck<Type>ArrayToJ<Type>Array(JNIEnv *env,
485
* const CK_<Type>_PTR ckpArray,
486
* CK_ULONG ckLength);
487
*
488
* PKCS#11 arrays consist always of a pointer to the beginning of the array and
489
* the array length whereas Java arrays carry their array length.
490
*
491
* The Functions to convert a Java array to a PKCS#11 array are void functions.
492
* Their arguments are the Java array object to convert, the reference to the
493
* array pointer, where the new PKCS#11 array should be stored and the reference
494
* to the array length where the PKCS#11 array length should be stored. These two
495
* references must not be NULL_PTR.
496
*
497
* The functions first obtain the array length of the Java array and then allocate
498
* the memory for the PKCS#11 array and set the array length. Then each element
499
* gets converted depending on their type. After use the allocated memory of the
500
* PKCS#11 array has to be explicitly freed.
501
*
502
* The Functions to convert a PKCS#11 array to a Java array get the PKCS#11 array
503
* pointer and the array length and they return the new Java array object. The
504
* Java array does not need to get freed after use.
505
*/
506
507
/*
508
* converts a jbooleanArray to a CK_BBOOL array. The allocated memory has to be freed after use!
509
*
510
* @param env - used to call JNI functions to get the array informtaion
511
* @param jArray - the Java array to convert
512
* @param ckpArray - the reference, where the pointer to the new CK_BBOOL array will be stored
513
* @param ckpLength - the reference, where the array length will be stored
514
*/
515
void jBooleanArrayToCKBBoolArray(JNIEnv *env, const jbooleanArray jArray, CK_BBOOL **ckpArray, CK_ULONG_PTR ckpLength)
516
{
517
jboolean* jpTemp;
518
CK_ULONG i;
519
520
if(jArray == NULL) {
521
*ckpArray = NULL_PTR;
522
*ckpLength = 0L;
523
return;
524
}
525
*ckpLength = (*env)->GetArrayLength(env, jArray);
526
jpTemp = (jboolean*) calloc(*ckpLength, sizeof(jboolean));
527
if (jpTemp == NULL) {
528
throwOutOfMemoryError(env, 0);
529
return;
530
}
531
(*env)->GetBooleanArrayRegion(env, jArray, 0, *ckpLength, jpTemp);
532
if ((*env)->ExceptionCheck(env)) {
533
free(jpTemp);
534
return;
535
}
536
537
*ckpArray = (CK_BBOOL*) calloc (*ckpLength, sizeof(CK_BBOOL));
538
if (*ckpArray == NULL) {
539
free(jpTemp);
540
throwOutOfMemoryError(env, 0);
541
return;
542
}
543
for (i=0; i<(*ckpLength); i++) {
544
(*ckpArray)[i] = jBooleanToCKBBool(jpTemp[i]);
545
}
546
free(jpTemp);
547
}
548
549
/*
550
* converts a jbyteArray to a CK_BYTE array. The allocated memory has to be freed after use!
551
*
552
* @param env - used to call JNI functions to get the array informtaion
553
* @param jArray - the Java array to convert
554
* @param ckpArray - the reference, where the pointer to the new CK_BYTE array will be stored
555
* @param ckpLength - the reference, where the array length will be stored
556
*/
557
void jByteArrayToCKByteArray(JNIEnv *env, const jbyteArray jArray, CK_BYTE_PTR *ckpArray, CK_ULONG_PTR ckpLength)
558
{
559
jbyte* jpTemp;
560
CK_ULONG i;
561
562
if(jArray == NULL) {
563
*ckpArray = NULL_PTR;
564
*ckpLength = 0L;
565
return;
566
}
567
*ckpLength = (*env)->GetArrayLength(env, jArray);
568
jpTemp = (jbyte*) calloc(*ckpLength, sizeof(jbyte));
569
if (jpTemp == NULL) {
570
throwOutOfMemoryError(env, 0);
571
return;
572
}
573
(*env)->GetByteArrayRegion(env, jArray, 0, *ckpLength, jpTemp);
574
if ((*env)->ExceptionCheck(env)) {
575
free(jpTemp);
576
return;
577
}
578
579
/* if CK_BYTE is the same size as jbyte, we save an additional copy */
580
if (sizeof(CK_BYTE) == sizeof(jbyte)) {
581
*ckpArray = (CK_BYTE_PTR) jpTemp;
582
} else {
583
*ckpArray = (CK_BYTE_PTR) calloc (*ckpLength, sizeof(CK_BYTE));
584
if (*ckpArray == NULL) {
585
free(jpTemp);
586
throwOutOfMemoryError(env, 0);
587
return;
588
}
589
for (i=0; i<(*ckpLength); i++) {
590
(*ckpArray)[i] = jByteToCKByte(jpTemp[i]);
591
}
592
free(jpTemp);
593
}
594
}
595
596
/*
597
* converts a jlongArray to a CK_ULONG array. The allocated memory has to be freed after use!
598
*
599
* @param env - used to call JNI functions to get the array informtaion
600
* @param jArray - the Java array to convert
601
* @param ckpArray - the reference, where the pointer to the new CK_ULONG array will be stored
602
* @param ckpLength - the reference, where the array length will be stored
603
*/
604
void jLongArrayToCKULongArray(JNIEnv *env, const jlongArray jArray, CK_ULONG_PTR *ckpArray, CK_ULONG_PTR ckpLength)
605
{
606
jlong* jTemp;
607
CK_ULONG i;
608
609
if(jArray == NULL) {
610
*ckpArray = NULL_PTR;
611
*ckpLength = 0L;
612
return;
613
}
614
*ckpLength = (*env)->GetArrayLength(env, jArray);
615
jTemp = (jlong*) calloc(*ckpLength, sizeof(jlong));
616
if (jTemp == NULL) {
617
throwOutOfMemoryError(env, 0);
618
return;
619
}
620
(*env)->GetLongArrayRegion(env, jArray, 0, *ckpLength, jTemp);
621
if ((*env)->ExceptionCheck(env)) {
622
free(jTemp);
623
return;
624
}
625
626
*ckpArray = (CK_ULONG_PTR) calloc(*ckpLength, sizeof(CK_ULONG));
627
if (*ckpArray == NULL) {
628
free(jTemp);
629
throwOutOfMemoryError(env, 0);
630
return;
631
}
632
for (i=0; i<(*ckpLength); i++) {
633
(*ckpArray)[i] = jLongToCKULong(jTemp[i]);
634
}
635
free(jTemp);
636
}
637
638
/*
639
* converts a jcharArray to a CK_CHAR array. The allocated memory has to be freed after use!
640
*
641
* @param env - used to call JNI functions to get the array informtaion
642
* @param jArray - the Java array to convert
643
* @param ckpArray - the reference, where the pointer to the new CK_CHAR array will be stored
644
* @param ckpLength - the reference, where the array length will be stored
645
*/
646
void jCharArrayToCKCharArray(JNIEnv *env, const jcharArray jArray, CK_CHAR_PTR *ckpArray, CK_ULONG_PTR ckpLength)
647
{
648
jchar* jpTemp;
649
CK_ULONG i;
650
651
if(jArray == NULL) {
652
*ckpArray = NULL_PTR;
653
*ckpLength = 0L;
654
return;
655
}
656
*ckpLength = (*env)->GetArrayLength(env, jArray);
657
jpTemp = (jchar*) calloc(*ckpLength, sizeof(jchar));
658
if (jpTemp == NULL) {
659
throwOutOfMemoryError(env, 0);
660
return;
661
}
662
(*env)->GetCharArrayRegion(env, jArray, 0, *ckpLength, jpTemp);
663
if ((*env)->ExceptionCheck(env)) {
664
free(jpTemp);
665
return;
666
}
667
668
*ckpArray = (CK_CHAR_PTR) calloc (*ckpLength, sizeof(CK_CHAR));
669
if (*ckpArray == NULL) {
670
free(jpTemp);
671
throwOutOfMemoryError(env, 0);
672
return;
673
}
674
for (i=0; i<(*ckpLength); i++) {
675
(*ckpArray)[i] = jCharToCKChar(jpTemp[i]);
676
}
677
free(jpTemp);
678
}
679
680
/*
681
* converts a jcharArray to a CK_UTF8CHAR array. The allocated memory has to be freed after use!
682
*
683
* @param env - used to call JNI functions to get the array informtaion
684
* @param jArray - the Java array to convert
685
* @param ckpArray - the reference, where the pointer to the new CK_UTF8CHAR array will be stored
686
* @param ckpLength - the reference, where the array length will be stored
687
*/
688
void jCharArrayToCKUTF8CharArray(JNIEnv *env, const jcharArray jArray, CK_UTF8CHAR_PTR *ckpArray, CK_ULONG_PTR ckpLength)
689
{
690
jchar* jTemp;
691
CK_ULONG i;
692
693
if(jArray == NULL) {
694
*ckpArray = NULL_PTR;
695
*ckpLength = 0L;
696
return;
697
}
698
*ckpLength = (*env)->GetArrayLength(env, jArray);
699
jTemp = (jchar*) calloc(*ckpLength, sizeof(jchar));
700
if (jTemp == NULL) {
701
throwOutOfMemoryError(env, 0);
702
return;
703
}
704
(*env)->GetCharArrayRegion(env, jArray, 0, *ckpLength, jTemp);
705
if ((*env)->ExceptionCheck(env)) {
706
free(jTemp);
707
return;
708
}
709
710
*ckpArray = (CK_UTF8CHAR_PTR) calloc(*ckpLength, sizeof(CK_UTF8CHAR));
711
if (*ckpArray == NULL) {
712
free(jTemp);
713
throwOutOfMemoryError(env, 0);
714
return;
715
}
716
for (i=0; i<(*ckpLength); i++) {
717
(*ckpArray)[i] = jCharToCKUTF8Char(jTemp[i]);
718
}
719
free(jTemp);
720
}
721
722
/*
723
* converts a jstring to a CK_CHAR array. The allocated memory has to be freed after use!
724
*
725
* @param env - used to call JNI functions to get the array informtaion
726
* @param jArray - the Java array to convert
727
* @param ckpArray - the reference, where the pointer to the new CK_CHAR array will be stored
728
* @param ckpLength - the reference, where the array length will be stored
729
*/
730
void jStringToCKUTF8CharArray(JNIEnv *env, const jstring jArray, CK_UTF8CHAR_PTR *ckpArray, CK_ULONG_PTR ckpLength)
731
{
732
const char* pCharArray;
733
jboolean isCopy;
734
735
if(jArray == NULL) {
736
*ckpArray = NULL_PTR;
737
*ckpLength = 0L;
738
return;
739
}
740
741
pCharArray = (*env)->GetStringUTFChars(env, jArray, &isCopy);
742
if (pCharArray == NULL) { return; }
743
744
*ckpLength = (CK_ULONG) strlen(pCharArray);
745
*ckpArray = (CK_UTF8CHAR_PTR) calloc(*ckpLength + 1, sizeof(CK_UTF8CHAR));
746
if (*ckpArray == NULL) {
747
(*env)->ReleaseStringUTFChars(env, (jstring) jArray, pCharArray);
748
throwOutOfMemoryError(env, 0);
749
return;
750
}
751
strcpy((char*)*ckpArray, pCharArray);
752
(*env)->ReleaseStringUTFChars(env, (jstring) jArray, pCharArray);
753
}
754
755
/*
756
* converts a jobjectArray with Java Attributes to a CK_ATTRIBUTE array. The allocated memory
757
* has to be freed after use!
758
*
759
* @param env - used to call JNI functions to get the array informtaion
760
* @param jArray - the Java Attribute array (template) to convert
761
* @param ckpArray - the reference, where the pointer to the new CK_ATTRIBUTE array will be
762
* stored
763
* @param ckpLength - the reference, where the array length will be stored
764
*/
765
void jAttributeArrayToCKAttributeArray(JNIEnv *env, jobjectArray jArray, CK_ATTRIBUTE_PTR *ckpArray, CK_ULONG_PTR ckpLength)
766
{
767
CK_ULONG i;
768
jlong jLength;
769
jobject jAttribute;
770
771
TRACE0("\nDEBUG: jAttributeArrayToCKAttributeArray");
772
if (jArray == NULL) {
773
*ckpArray = NULL_PTR;
774
*ckpLength = 0L;
775
return;
776
}
777
jLength = (*env)->GetArrayLength(env, jArray);
778
*ckpLength = jLongToCKULong(jLength);
779
*ckpArray = (CK_ATTRIBUTE_PTR) calloc(*ckpLength, sizeof(CK_ATTRIBUTE));
780
if (*ckpArray == NULL) {
781
throwOutOfMemoryError(env, 0);
782
return;
783
}
784
TRACE1(", converting %lld attributes", (long long int) jLength);
785
for (i=0; i<(*ckpLength); i++) {
786
TRACE1(", getting %lu. attribute", i);
787
jAttribute = (*env)->GetObjectArrayElement(env, jArray, i);
788
if ((*env)->ExceptionCheck(env)) {
789
freeCKAttributeArray(*ckpArray, i);
790
return;
791
}
792
TRACE1(", jAttribute , converting %lu. attribute", i);
793
(*ckpArray)[i] = jAttributeToCKAttribute(env, jAttribute);
794
if ((*env)->ExceptionCheck(env)) {
795
freeCKAttributeArray(*ckpArray, i);
796
return;
797
}
798
}
799
TRACE0("FINISHED\n");
800
}
801
802
/*
803
* converts a CK_BYTE array and its length to a jbyteArray.
804
*
805
* @param env - used to call JNI functions to create the new Java array
806
* @param ckpArray - the pointer to the CK_BYTE array to convert
807
* @param ckpLength - the length of the array to convert
808
* @return - the new Java byte array or NULL if error occurred
809
*/
810
jbyteArray ckByteArrayToJByteArray(JNIEnv *env, const CK_BYTE_PTR ckpArray, CK_ULONG ckLength)
811
{
812
CK_ULONG i;
813
jbyte* jpTemp;
814
jbyteArray jArray;
815
816
/* if CK_BYTE is the same size as jbyte, we save an additional copy */
817
if (sizeof(CK_BYTE) == sizeof(jbyte)) {
818
jpTemp = (jbyte*) ckpArray;
819
} else {
820
jpTemp = (jbyte*) calloc(ckLength, sizeof(jbyte));
821
if (jpTemp == NULL) {
822
throwOutOfMemoryError(env, 0);
823
return NULL;
824
}
825
for (i=0; i<ckLength; i++) {
826
jpTemp[i] = ckByteToJByte(ckpArray[i]);
827
}
828
}
829
830
jArray = (*env)->NewByteArray(env, ckULongToJSize(ckLength));
831
if (jArray != NULL) {
832
(*env)->SetByteArrayRegion(env, jArray, 0, ckULongToJSize(ckLength), jpTemp);
833
}
834
835
if (sizeof(CK_BYTE) != sizeof(jbyte)) { free(jpTemp); }
836
837
return jArray ;
838
}
839
840
/*
841
* converts a CK_ULONG array and its length to a jlongArray.
842
*
843
* @param env - used to call JNI functions to create the new Java array
844
* @param ckpArray - the pointer to the CK_ULONG array to convert
845
* @param ckpLength - the length of the array to convert
846
* @return - the new Java long array
847
*/
848
jlongArray ckULongArrayToJLongArray(JNIEnv *env, const CK_ULONG_PTR ckpArray, CK_ULONG ckLength)
849
{
850
CK_ULONG i;
851
jlong* jpTemp;
852
jlongArray jArray;
853
854
jpTemp = (jlong*) calloc(ckLength, sizeof(jlong));
855
if (jpTemp == NULL) {
856
throwOutOfMemoryError(env, 0);
857
return NULL;
858
}
859
for (i=0; i<ckLength; i++) {
860
jpTemp[i] = ckLongToJLong(ckpArray[i]);
861
}
862
jArray = (*env)->NewLongArray(env, ckULongToJSize(ckLength));
863
if (jArray != NULL) {
864
(*env)->SetLongArrayRegion(env, jArray, 0, ckULongToJSize(ckLength), jpTemp);
865
}
866
free(jpTemp);
867
868
return jArray ;
869
}
870
871
/*
872
* converts a CK_CHAR array and its length to a jcharArray.
873
*
874
* @param env - used to call JNI functions to create the new Java array
875
* @param ckpArray - the pointer to the CK_CHAR array to convert
876
* @param ckpLength - the length of the array to convert
877
* @return - the new Java char array
878
*/
879
jcharArray ckCharArrayToJCharArray(JNIEnv *env, const CK_CHAR_PTR ckpArray, CK_ULONG ckLength)
880
{
881
CK_ULONG i;
882
jchar* jpTemp;
883
jcharArray jArray;
884
885
jpTemp = (jchar*) calloc(ckLength, sizeof(jchar));
886
if (jpTemp == NULL) {
887
throwOutOfMemoryError(env, 0);
888
return NULL;
889
}
890
for (i=0; i<ckLength; i++) {
891
jpTemp[i] = ckCharToJChar(ckpArray[i]);
892
}
893
jArray = (*env)->NewCharArray(env, ckULongToJSize(ckLength));
894
if (jArray != NULL) {
895
(*env)->SetCharArrayRegion(env, jArray, 0, ckULongToJSize(ckLength), jpTemp);
896
}
897
free(jpTemp);
898
899
return jArray ;
900
}
901
902
/*
903
* converts a CK_UTF8CHAR array and its length to a jcharArray.
904
*
905
* @param env - used to call JNI functions to create the new Java array
906
* @param ckpArray - the pointer to the CK_UTF8CHAR array to convert
907
* @param ckpLength - the length of the array to convert
908
* @return - the new Java char array
909
*/
910
jcharArray ckUTF8CharArrayToJCharArray(JNIEnv *env, const CK_UTF8CHAR_PTR ckpArray, CK_ULONG ckLength)
911
{
912
CK_ULONG i;
913
jchar* jpTemp;
914
jcharArray jArray;
915
916
jpTemp = (jchar*) calloc(ckLength, sizeof(jchar));
917
if (jpTemp == NULL) {
918
throwOutOfMemoryError(env, 0);
919
return NULL;
920
}
921
for (i=0; i<ckLength; i++) {
922
jpTemp[i] = ckUTF8CharToJChar(ckpArray[i]);
923
}
924
jArray = (*env)->NewCharArray(env, ckULongToJSize(ckLength));
925
if (jArray != NULL) {
926
(*env)->SetCharArrayRegion(env, jArray, 0, ckULongToJSize(ckLength), jpTemp);
927
}
928
free(jpTemp);
929
930
return jArray ;
931
}
932
933
/*
934
* the following functions convert Java objects to PKCS#11 pointers and the
935
* length in bytes and vice versa
936
*
937
* CK_<Type>_PTR j<Object>ToCK<Type>Ptr(JNIEnv *env, jobject jObject);
938
*
939
* jobject ck<Type>PtrToJ<Object>(JNIEnv *env, const CK_<Type>_PTR ckpValue);
940
*
941
* The functions that convert a Java object to a PKCS#11 pointer first allocate
942
* the memory for the PKCS#11 pointer. Then they set each element corresponding
943
* to the fields in the Java object to convert. After use the allocated memory of
944
* the PKCS#11 pointer has to be explicitly freed.
945
*
946
* The functions to convert a PKCS#11 pointer to a Java object create a new Java
947
* object first and than they set all fields in the object depending on the values
948
* of the type or structure where the PKCS#11 pointer points to.
949
*/
950
951
/*
952
* converts a CK_BBOOL pointer to a Java boolean Object.
953
*
954
* @param env - used to call JNI functions to create the new Java object
955
* @param ckpValue - the pointer to the CK_BBOOL value
956
* @return - the new Java boolean object with the boolean value
957
*/
958
jobject ckBBoolPtrToJBooleanObject(JNIEnv *env, const CK_BBOOL *ckpValue)
959
{
960
jclass jValueObjectClass;
961
jmethodID jConstructor;
962
jobject jValueObject;
963
jboolean jValue;
964
965
jValueObjectClass = (*env)->FindClass(env, "java/lang/Boolean");
966
if (jValueObjectClass == NULL) { return NULL; }
967
jConstructor = (*env)->GetMethodID(env, jValueObjectClass, "<init>", "(Z)V");
968
if (jConstructor == NULL) { return NULL; }
969
jValue = ckBBoolToJBoolean(*ckpValue);
970
jValueObject = (*env)->NewObject(env, jValueObjectClass, jConstructor, jValue);
971
972
return jValueObject ;
973
}
974
975
/*
976
* converts a CK_ULONG pointer to a Java long Object.
977
*
978
* @param env - used to call JNI functions to create the new Java object
979
* @param ckpValue - the pointer to the CK_ULONG value
980
* @return - the new Java long object with the long value
981
*/
982
jobject ckULongPtrToJLongObject(JNIEnv *env, const CK_ULONG_PTR ckpValue)
983
{
984
jclass jValueObjectClass;
985
jmethodID jConstructor;
986
jobject jValueObject;
987
jlong jValue;
988
989
jValueObjectClass = (*env)->FindClass(env, "java/lang/Long");
990
if (jValueObjectClass == NULL) { return NULL; }
991
jConstructor = (*env)->GetMethodID(env, jValueObjectClass, "<init>", "(J)V");
992
if (jConstructor == NULL) { return NULL; }
993
jValue = ckULongToJLong(*ckpValue);
994
jValueObject = (*env)->NewObject(env, jValueObjectClass, jConstructor, jValue);
995
996
return jValueObject ;
997
}
998
999
/*
1000
* converts a Java boolean object into a pointer to a CK_BBOOL value. The memory has to be
1001
* freed after use!
1002
*
1003
* @param env - used to call JNI functions to get the value out of the Java object
1004
* @param jObject - the "java/lang/Boolean" object to convert
1005
* @return - the pointer to the new CK_BBOOL value
1006
*/
1007
CK_BBOOL* jBooleanObjectToCKBBoolPtr(JNIEnv *env, jobject jObject)
1008
{
1009
jclass jObjectClass;
1010
jmethodID jValueMethod;
1011
jboolean jValue;
1012
CK_BBOOL *ckpValue;
1013
1014
jObjectClass = (*env)->FindClass(env, "java/lang/Boolean");
1015
if (jObjectClass == NULL) { return NULL; }
1016
jValueMethod = (*env)->GetMethodID(env, jObjectClass, "booleanValue", "()Z");
1017
if (jValueMethod == NULL) { return NULL; }
1018
jValue = (*env)->CallBooleanMethod(env, jObject, jValueMethod);
1019
ckpValue = (CK_BBOOL *) malloc(sizeof(CK_BBOOL));
1020
if (ckpValue == NULL) {
1021
throwOutOfMemoryError(env, 0);
1022
return NULL;
1023
}
1024
*ckpValue = jBooleanToCKBBool(jValue);
1025
1026
return ckpValue ;
1027
}
1028
1029
/*
1030
* converts a Java byte object into a pointer to a CK_BYTE value. The memory has to be
1031
* freed after use!
1032
*
1033
* @param env - used to call JNI functions to get the value out of the Java object
1034
* @param jObject - the "java/lang/Byte" object to convert
1035
* @return - the pointer to the new CK_BYTE value
1036
*/
1037
CK_BYTE_PTR jByteObjectToCKBytePtr(JNIEnv *env, jobject jObject)
1038
{
1039
jclass jObjectClass;
1040
jmethodID jValueMethod;
1041
jbyte jValue;
1042
CK_BYTE_PTR ckpValue;
1043
1044
jObjectClass = (*env)->FindClass(env, "java/lang/Byte");
1045
if (jObjectClass == NULL) { return NULL; }
1046
jValueMethod = (*env)->GetMethodID(env, jObjectClass, "byteValue", "()B");
1047
if (jValueMethod == NULL) { return NULL; }
1048
jValue = (*env)->CallByteMethod(env, jObject, jValueMethod);
1049
ckpValue = (CK_BYTE_PTR) malloc(sizeof(CK_BYTE));
1050
if (ckpValue == NULL) {
1051
throwOutOfMemoryError(env, 0);
1052
return NULL;
1053
}
1054
*ckpValue = jByteToCKByte(jValue);
1055
return ckpValue ;
1056
}
1057
1058
/*
1059
* converts a Java integer object into a pointer to a CK_ULONG value. The memory has to be
1060
* freed after use!
1061
*
1062
* @param env - used to call JNI functions to get the value out of the Java object
1063
* @param jObject - the "java/lang/Integer" object to convert
1064
* @return - the pointer to the new CK_ULONG value
1065
*/
1066
CK_ULONG* jIntegerObjectToCKULongPtr(JNIEnv *env, jobject jObject)
1067
{
1068
jclass jObjectClass;
1069
jmethodID jValueMethod;
1070
jint jValue;
1071
CK_ULONG *ckpValue;
1072
1073
jObjectClass = (*env)->FindClass(env, "java/lang/Integer");
1074
if (jObjectClass == NULL) { return NULL; }
1075
jValueMethod = (*env)->GetMethodID(env, jObjectClass, "intValue", "()I");
1076
if (jValueMethod == NULL) { return NULL; }
1077
jValue = (*env)->CallIntMethod(env, jObject, jValueMethod);
1078
ckpValue = (CK_ULONG *) malloc(sizeof(CK_ULONG));
1079
if (ckpValue == NULL) {
1080
throwOutOfMemoryError(env, 0);
1081
return NULL;
1082
}
1083
*ckpValue = jLongToCKLong(jValue);
1084
return ckpValue ;
1085
}
1086
1087
/*
1088
* converts a Java long object into a pointer to a CK_ULONG value. The memory has to be
1089
* freed after use!
1090
*
1091
* @param env - used to call JNI functions to get the value out of the Java object
1092
* @param jObject - the "java/lang/Long" object to convert
1093
* @return - the pointer to the new CK_ULONG value
1094
*/
1095
CK_ULONG* jLongObjectToCKULongPtr(JNIEnv *env, jobject jObject)
1096
{
1097
jclass jObjectClass;
1098
jmethodID jValueMethod;
1099
jlong jValue;
1100
CK_ULONG *ckpValue;
1101
1102
jObjectClass = (*env)->FindClass(env, "java/lang/Long");
1103
if (jObjectClass == NULL) { return NULL; }
1104
jValueMethod = (*env)->GetMethodID(env, jObjectClass, "longValue", "()J");
1105
if (jValueMethod == NULL) { return NULL; }
1106
jValue = (*env)->CallLongMethod(env, jObject, jValueMethod);
1107
ckpValue = (CK_ULONG *) malloc(sizeof(CK_ULONG));
1108
if (ckpValue == NULL) {
1109
throwOutOfMemoryError(env, 0);
1110
return NULL;
1111
}
1112
*ckpValue = jLongToCKULong(jValue);
1113
1114
return ckpValue ;
1115
}
1116
1117
/*
1118
* converts a Java char object into a pointer to a CK_CHAR value. The memory has to be
1119
* freed after use!
1120
*
1121
* @param env - used to call JNI functions to get the value out of the Java object
1122
* @param jObject - the "java/lang/Char" object to convert
1123
* @return - the pointer to the new CK_CHAR value
1124
*/
1125
CK_CHAR_PTR jCharObjectToCKCharPtr(JNIEnv *env, jobject jObject)
1126
{
1127
jclass jObjectClass;
1128
jmethodID jValueMethod;
1129
jchar jValue;
1130
CK_CHAR_PTR ckpValue;
1131
1132
jObjectClass = (*env)->FindClass(env, "java/lang/Char");
1133
if (jObjectClass == NULL) { return NULL; }
1134
jValueMethod = (*env)->GetMethodID(env, jObjectClass, "charValue", "()C");
1135
if (jValueMethod == NULL) { return NULL; }
1136
jValue = (*env)->CallCharMethod(env, jObject, jValueMethod);
1137
ckpValue = (CK_CHAR_PTR) malloc(sizeof(CK_CHAR));
1138
if (ckpValue == NULL) {
1139
throwOutOfMemoryError(env, 0);
1140
return NULL;
1141
}
1142
*ckpValue = jCharToCKChar(jValue);
1143
1144
return ckpValue ;
1145
}
1146
1147
/*
1148
* converts a Java object into a pointer to CK-type or a CK-structure with the length in Bytes.
1149
* The memory of the returned pointer MUST BE FREED BY CALLER!
1150
*
1151
* @param env - used to call JNI functions to get the Java classes and objects
1152
* @param jObject - the Java object to convert
1153
* @param ckpLength - pointer to the length (bytes) of the newly-allocated CK-value or CK-structure
1154
* @return ckpObject - pointer to the newly-allocated CK-value or CK-structure
1155
*/
1156
CK_VOID_PTR jObjectToPrimitiveCKObjectPtr(JNIEnv *env, jobject jObject, CK_ULONG *ckpLength)
1157
{
1158
jclass jLongClass, jBooleanClass, jByteArrayClass, jCharArrayClass;
1159
jclass jByteClass, jDateClass, jCharacterClass, jIntegerClass;
1160
jclass jBooleanArrayClass, jIntArrayClass, jLongArrayClass;
1161
jclass jStringClass;
1162
jclass jObjectClass, jClassClass;
1163
CK_VOID_PTR ckpObject;
1164
jmethodID jMethod;
1165
jobject jClassObject;
1166
jstring jClassNameString;
1167
char *classNameString, *exceptionMsgPrefix, *exceptionMsg;
1168
1169
TRACE0("\nDEBUG: jObjectToPrimitiveCKObjectPtr");
1170
if (jObject == NULL) {
1171
*ckpLength = 0;
1172
return NULL;
1173
}
1174
1175
jLongClass = (*env)->FindClass(env, "java/lang/Long");
1176
if (jLongClass == NULL) { return NULL; }
1177
if ((*env)->IsInstanceOf(env, jObject, jLongClass)) {
1178
ckpObject = jLongObjectToCKULongPtr(env, jObject);
1179
*ckpLength = sizeof(CK_ULONG);
1180
TRACE1("<converted long value %lu>", *((CK_ULONG *) ckpObject));
1181
return ckpObject;
1182
}
1183
1184
jBooleanClass = (*env)->FindClass(env, "java/lang/Boolean");
1185
if (jBooleanClass == NULL) { return NULL; }
1186
if ((*env)->IsInstanceOf(env, jObject, jBooleanClass)) {
1187
ckpObject = jBooleanObjectToCKBBoolPtr(env, jObject);
1188
*ckpLength = sizeof(CK_BBOOL);
1189
TRACE0(" <converted boolean value ");
1190
TRACE0((*((CK_BBOOL *) ckpObject) == TRUE) ? "TRUE>" : "FALSE>");
1191
return ckpObject;
1192
}
1193
1194
jByteArrayClass = (*env)->FindClass(env, "[B");
1195
if (jByteArrayClass == NULL) { return NULL; }
1196
if ((*env)->IsInstanceOf(env, jObject, jByteArrayClass)) {
1197
jByteArrayToCKByteArray(env, jObject, (CK_BYTE_PTR*) &ckpObject, ckpLength);
1198
return ckpObject;
1199
}
1200
1201
jCharArrayClass = (*env)->FindClass(env, "[C");
1202
if (jCharArrayClass == NULL) { return NULL; }
1203
if ((*env)->IsInstanceOf(env, jObject, jCharArrayClass)) {
1204
jCharArrayToCKUTF8CharArray(env, jObject, (CK_UTF8CHAR_PTR*) &ckpObject, ckpLength);
1205
return ckpObject;
1206
}
1207
1208
jByteClass = (*env)->FindClass(env, "java/lang/Byte");
1209
if (jByteClass == NULL) { return NULL; }
1210
if ((*env)->IsInstanceOf(env, jObject, jByteClass)) {
1211
ckpObject = jByteObjectToCKBytePtr(env, jObject);
1212
*ckpLength = sizeof(CK_BYTE);
1213
TRACE1("<converted byte value %X>", *((CK_BYTE *) ckpObject));
1214
return ckpObject;
1215
}
1216
1217
jDateClass = (*env)->FindClass(env, CLASS_DATE);
1218
if (jDateClass == NULL) { return NULL; }
1219
if ((*env)->IsInstanceOf(env, jObject, jDateClass)) {
1220
ckpObject = jDateObjectToCKDatePtr(env, jObject);
1221
*ckpLength = sizeof(CK_DATE);
1222
TRACE3("<converted date value %.4s-%.2s-%.2s>", ((CK_DATE *) ckpObject)->year,
1223
((CK_DATE *) ckpObject)->month, ((CK_DATE *) ckpObject)->day);
1224
return ckpObject;
1225
}
1226
1227
jCharacterClass = (*env)->FindClass(env, "java/lang/Character");
1228
if (jCharacterClass == NULL) { return NULL; }
1229
if ((*env)->IsInstanceOf(env, jObject, jCharacterClass)) {
1230
ckpObject = jCharObjectToCKCharPtr(env, jObject);
1231
*ckpLength = sizeof(CK_UTF8CHAR);
1232
TRACE1("<converted char value %c>", *((CK_CHAR *) ckpObject));
1233
return ckpObject;
1234
}
1235
1236
jIntegerClass = (*env)->FindClass(env, "java/lang/Integer");
1237
if (jIntegerClass == NULL) { return NULL; }
1238
if ((*env)->IsInstanceOf(env, jObject, jIntegerClass)) {
1239
ckpObject = jIntegerObjectToCKULongPtr(env, jObject);
1240
*ckpLength = sizeof(CK_ULONG);
1241
TRACE1("<converted integer value %lu>", *((CK_ULONG *) ckpObject));
1242
return ckpObject;
1243
}
1244
1245
jBooleanArrayClass = (*env)->FindClass(env, "[Z");
1246
if (jBooleanArrayClass == NULL) { return NULL; }
1247
if ((*env)->IsInstanceOf(env, jObject, jBooleanArrayClass)) {
1248
jBooleanArrayToCKBBoolArray(env, jObject, (CK_BBOOL**) &ckpObject, ckpLength);
1249
return ckpObject;
1250
}
1251
1252
jIntArrayClass = (*env)->FindClass(env, "[I");
1253
if (jIntArrayClass == NULL) { return NULL; }
1254
if ((*env)->IsInstanceOf(env, jObject, jIntArrayClass)) {
1255
jLongArrayToCKULongArray(env, jObject, (CK_ULONG_PTR*) &ckpObject, ckpLength);
1256
return ckpObject;
1257
}
1258
1259
jLongArrayClass = (*env)->FindClass(env, "[J");
1260
if (jLongArrayClass == NULL) { return NULL; }
1261
if ((*env)->IsInstanceOf(env, jObject, jLongArrayClass)) {
1262
jLongArrayToCKULongArray(env, jObject, (CK_ULONG_PTR*) &ckpObject, ckpLength);
1263
return ckpObject;
1264
}
1265
1266
jStringClass = (*env)->FindClass(env, "java/lang/String");
1267
if (jStringClass == NULL) { return NULL; }
1268
if ((*env)->IsInstanceOf(env, jObject, jStringClass)) {
1269
jStringToCKUTF8CharArray(env, jObject, (CK_UTF8CHAR_PTR*) &ckpObject, ckpLength);
1270
return ckpObject;
1271
}
1272
1273
/* type of jObject unknown, throw PKCS11RuntimeException */
1274
jObjectClass = (*env)->FindClass(env, "java/lang/Object");
1275
if (jObjectClass == NULL) { return NULL; }
1276
jMethod = (*env)->GetMethodID(env, jObjectClass, "getClass", "()Ljava/lang/Class;");
1277
if (jMethod == NULL) { return NULL; }
1278
jClassObject = (*env)->CallObjectMethod(env, jObject, jMethod);
1279
assert(jClassObject != 0);
1280
jClassClass = (*env)->FindClass(env, "java/lang/Class");
1281
if (jClassClass == NULL) { return NULL; }
1282
jMethod = (*env)->GetMethodID(env, jClassClass, "getName", "()Ljava/lang/String;");
1283
if (jMethod == NULL) { return NULL; }
1284
jClassNameString = (jstring)
1285
(*env)->CallObjectMethod(env, jClassObject, jMethod);
1286
assert(jClassNameString != 0);
1287
classNameString = (char*)
1288
(*env)->GetStringUTFChars(env, jClassNameString, NULL);
1289
if (classNameString == NULL) { return NULL; }
1290
exceptionMsgPrefix = "Java object of this class cannot be converted to native PKCS#11 type: ";
1291
exceptionMsg = (char *)
1292
malloc(strlen(exceptionMsgPrefix) + strlen(classNameString) + 1);
1293
if (exceptionMsg == NULL) {
1294
(*env)->ReleaseStringUTFChars(env, jClassNameString, classNameString);
1295
throwOutOfMemoryError(env, 0);
1296
return NULL;
1297
}
1298
strcpy(exceptionMsg, exceptionMsgPrefix);
1299
strcat(exceptionMsg, classNameString);
1300
(*env)->ReleaseStringUTFChars(env, jClassNameString, classNameString);
1301
throwPKCS11RuntimeException(env, exceptionMsg);
1302
free(exceptionMsg);
1303
*ckpLength = 0;
1304
1305
TRACE0("FINISHED\n");
1306
return NULL;
1307
}
1308
1309
#ifdef P11_MEMORYDEBUG
1310
1311
#undef malloc
1312
#undef calloc
1313
#undef free
1314
1315
void *p11malloc(size_t c, char *file, int line) {
1316
void *p = malloc(c);
1317
fprintf(stdout, "malloc\t%08lX\t%lX\t%s:%d\n", ptr_to_jlong(p), c, file, line);
1318
fflush(stdout);
1319
return p;
1320
}
1321
1322
void *p11calloc(size_t c, size_t s, char *file, int line) {
1323
void *p = calloc(c, s);
1324
fprintf(stdout, "calloc\t%08lX\t%lX\t%lX\t%s:%d\n", ptr_to_jlong(p), c, s, file, line);
1325
fflush(stdout);
1326
return p;
1327
}
1328
1329
void p11free(void *p, char *file, int line) {
1330
fprintf(stdout, "free\t%08lX\t\t%s:%d\n", ptr_to_jlong(p), file, line);
1331
fflush(stdout);
1332
free(p);
1333
}
1334
1335
#endif
1336
1337
// prints a message to stdout if debug output is enabled
1338
void printDebug(const char *format, ...) {
1339
if (debug == JNI_TRUE) {
1340
va_list args;
1341
fprintf(stdout, "sunpkcs11: ");
1342
va_start(args, format);
1343
vfprintf(stdout, format, args);
1344
va_end(args);
1345
fflush(stdout);
1346
}
1347
}
1348
1349
1350