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