| File: | jdk/src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_sessmgmt.c |
| Warning: | line 710, column 9 Value stored to 'returnValue' is never read |
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 | #include "sun_security_pkcs11_wrapper_PKCS11.h" |
| 56 | |
| 57 | /* The list of notify callback handles that are currently active and waiting |
| 58 | * for callbacks from their sessions. |
| 59 | */ |
| 60 | #ifndef NO_CALLBACKS |
| 61 | NotifyListNode *notifyListHead = NULL((void*)0); |
| 62 | jobject notifyListLock = NULL((void*)0); |
| 63 | #endif /* NO_CALLBACKS */ |
| 64 | |
| 65 | #ifdef P11_ENABLE_C_OPENSESSION |
| 66 | /* |
| 67 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 68 | * Method: C_OpenSession |
| 69 | * Signature: (JJLjava/lang/Object;Lsun/security/pkcs11/wrapper/CK_NOTIFY;)J |
| 70 | * Parametermapping: *PKCS11* |
| 71 | * @param jlong jSlotID CK_SLOT_ID slotID |
| 72 | * @param jlong jFlags CK_FLAGS flags |
| 73 | * @param jobject jApplication CK_VOID_PTR pApplication |
| 74 | * @param jobject jNotify CK_NOTIFY Notify |
| 75 | * @return jlong jSessionHandle CK_SESSION_HANDLE_PTR phSession |
| 76 | */ |
| 77 | JNIEXPORT__attribute__((visibility("default"))) jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1OpenSession |
| 78 | (JNIEnv *env, jobject obj, jlong jSlotID, jlong jFlags, jobject jApplication, jobject jNotify) |
| 79 | { |
| 80 | CK_SESSION_HANDLE ckSessionHandle; |
| 81 | CK_SLOT_ID ckSlotID; |
| 82 | CK_FLAGS ckFlags; |
| 83 | CK_VOID_PTR ckpApplication; |
| 84 | CK_NOTIFY ckNotify; |
| 85 | jlong jSessionHandle; |
| 86 | CK_RV rv; |
| 87 | #ifndef NO_CALLBACKS |
| 88 | NotifyEncapsulation *notifyEncapsulation = NULL((void*)0); |
| 89 | #endif /* NO_CALLBACKS */ |
| 90 | |
| 91 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
| 92 | if (ckpFunctions == NULL((void*)0)) { return 0L; } |
| 93 | |
| 94 | ckSlotID = jLongToCKULong(jSlotID)((CK_ULONG) jSlotID); |
| 95 | ckFlags = jLongToCKULong(jFlags)((CK_ULONG) jFlags); |
| 96 | |
| 97 | #ifndef NO_CALLBACKS |
| 98 | if (jNotify != NULL((void*)0)) { |
| 99 | notifyEncapsulation = (NotifyEncapsulation *) malloc(sizeof(NotifyEncapsulation)); |
| 100 | if (notifyEncapsulation == NULL((void*)0)) { |
| 101 | throwOutOfMemoryError(env, 0); |
| 102 | return 0L; |
| 103 | } |
| 104 | notifyEncapsulation->jApplicationData = (jApplication != NULL((void*)0)) |
| 105 | ? (*env)->NewGlobalRef(env, jApplication) |
| 106 | : NULL((void*)0); |
| 107 | notifyEncapsulation->jNotifyObject = (*env)->NewGlobalRef(env, jNotify); |
| 108 | ckpApplication = notifyEncapsulation; |
| 109 | ckNotify = (CK_NOTIFY) ¬ifyCallback; |
| 110 | } else { |
| 111 | ckpApplication = NULL_PTR0; |
| 112 | ckNotify = NULL_PTR0; |
| 113 | } |
| 114 | #else |
| 115 | ckpApplication = NULL_PTR0; |
| 116 | ckNotify = NULL_PTR0; |
| 117 | #endif /* NO_CALLBACKS */ |
| 118 | |
| 119 | TRACE0("DEBUG: C_OpenSession"); |
| 120 | TRACE1(", slotID=%lu", ckSlotID); |
| 121 | TRACE1(", flags=%lu", (unsigned long) ckFlags); |
| 122 | TRACE0(" ... "); |
| 123 | |
| 124 | rv = (*ckpFunctions->C_OpenSession)(ckSlotID, ckFlags, ckpApplication, ckNotify, &ckSessionHandle); |
| 125 | if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK0L) { |
| 126 | #ifndef NO_CALLBACKS |
| 127 | if (notifyEncapsulation != NULL((void*)0)) { |
| 128 | if (notifyEncapsulation->jApplicationData != NULL((void*)0)) { |
| 129 | (*env)->DeleteGlobalRef(env, jApplication); |
| 130 | } |
| 131 | (*env)->DeleteGlobalRef(env, jNotify); |
| 132 | free(notifyEncapsulation); |
| 133 | } |
| 134 | #endif /* NO_CALLBACKS */ |
| 135 | return 0L; |
| 136 | } |
| 137 | |
| 138 | TRACE0("got session"); |
| 139 | TRACE1(", SessionHandle=%lu", (unsigned long) ckSessionHandle); |
| 140 | TRACE0(" ... "); |
| 141 | |
| 142 | jSessionHandle = ckULongToJLong(ckSessionHandle)((jlong) ckSessionHandle); |
| 143 | |
| 144 | #ifndef NO_CALLBACKS |
| 145 | if (notifyEncapsulation != NULL((void*)0)) { |
| 146 | /* store the notifyEncapsulation to enable later cleanup */ |
| 147 | putNotifyEntry(env, ckSessionHandle, notifyEncapsulation); |
| 148 | } |
| 149 | #endif /* NO_CALLBACKS */ |
| 150 | |
| 151 | TRACE0("FINISHED\n"); |
| 152 | |
| 153 | return jSessionHandle ; |
| 154 | } |
| 155 | #endif |
| 156 | |
| 157 | #ifdef P11_ENABLE_C_CLOSESESSION |
| 158 | /* |
| 159 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 160 | * Method: C_CloseSession |
| 161 | * Signature: (J)V |
| 162 | * Parametermapping: *PKCS11* |
| 163 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
| 164 | */ |
| 165 | JNIEXPORT__attribute__((visibility("default"))) void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CloseSession |
| 166 | (JNIEnv *env, jobject obj, jlong jSessionHandle) |
| 167 | { |
| 168 | CK_SESSION_HANDLE ckSessionHandle; |
| 169 | CK_RV rv; |
| 170 | #ifndef NO_CALLBACKS |
| 171 | NotifyEncapsulation *notifyEncapsulation; |
| 172 | jobject jApplicationData; |
| 173 | #endif /* NO_CALLBACKS */ |
| 174 | |
| 175 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
| 176 | if (ckpFunctions == NULL((void*)0)) { return; } |
| 177 | |
| 178 | ckSessionHandle = jLongToCKULong(jSessionHandle)((CK_ULONG) jSessionHandle); |
| 179 | |
| 180 | rv = (*ckpFunctions->C_CloseSession)(ckSessionHandle); |
| 181 | if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK0L) { return; } |
| 182 | |
| 183 | #ifndef NO_CALLBACKS |
| 184 | notifyEncapsulation = removeNotifyEntry(env, ckSessionHandle); |
| 185 | |
| 186 | if (notifyEncapsulation != NULL((void*)0)) { |
| 187 | /* there was a notify object used with this session, now dump the |
| 188 | * encapsulation object |
| 189 | */ |
| 190 | (*env)->DeleteGlobalRef(env, notifyEncapsulation->jNotifyObject); |
| 191 | jApplicationData = notifyEncapsulation->jApplicationData; |
| 192 | if (jApplicationData != NULL((void*)0)) { |
| 193 | (*env)->DeleteGlobalRef(env, jApplicationData); |
| 194 | } |
| 195 | free(notifyEncapsulation); |
| 196 | } |
| 197 | #endif /* NO_CALLBACKS */ |
| 198 | |
| 199 | } |
| 200 | #endif |
| 201 | |
| 202 | #ifdef P11_ENABLE_C_CLOSEALLSESSIONS |
| 203 | /* |
| 204 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 205 | * Method: C_CloseAllSessions |
| 206 | * Signature: (J)V |
| 207 | * Parametermapping: *PKCS11* |
| 208 | * @param jlong jSlotID CK_SLOT_ID slotID |
| 209 | */ |
| 210 | JNIEXPORT__attribute__((visibility("default"))) void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CloseAllSessions |
| 211 | (JNIEnv *env, jobject obj, jlong jSlotID) |
| 212 | { |
| 213 | CK_SLOT_ID ckSlotID; |
| 214 | CK_RV rv; |
| 215 | #ifndef NO_CALLBACKS |
| 216 | NotifyEncapsulation *notifyEncapsulation; |
| 217 | jobject jApplicationData; |
| 218 | #endif /* NO_CALLBACKS */ |
| 219 | |
| 220 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
| 221 | if (ckpFunctions == NULL((void*)0)) { return; } |
| 222 | |
| 223 | ckSlotID = jLongToCKULong(jSlotID)((CK_ULONG) jSlotID); |
| 224 | |
| 225 | rv = (*ckpFunctions->C_CloseAllSessions)(ckSlotID); |
| 226 | if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK0L) { return; } |
| 227 | |
| 228 | #ifndef NO_CALLBACKS |
| 229 | /* Remove all notify callback helper objects. */ |
| 230 | while ((notifyEncapsulation = removeFirstNotifyEntry(env)) != NULL((void*)0)) { |
| 231 | /* there was a notify object used with this session, now dump the |
| 232 | * encapsulation object |
| 233 | */ |
| 234 | (*env)->DeleteGlobalRef(env, notifyEncapsulation->jNotifyObject); |
| 235 | jApplicationData = notifyEncapsulation->jApplicationData; |
| 236 | if (jApplicationData != NULL((void*)0)) { |
| 237 | (*env)->DeleteGlobalRef(env, jApplicationData); |
| 238 | } |
| 239 | free(notifyEncapsulation); |
| 240 | } |
| 241 | #endif /* NO_CALLBACKS */ |
| 242 | } |
| 243 | #endif |
| 244 | |
| 245 | #ifdef P11_ENABLE_C_GETSESSIONINFO |
| 246 | /* |
| 247 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 248 | * Method: C_GetSessionInfo |
| 249 | * Signature: (J)Lsun/security/pkcs11/wrapper/CK_SESSION_INFO; |
| 250 | * Parametermapping: *PKCS11* |
| 251 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
| 252 | * @return jobject jSessionInfo CK_SESSION_INFO_PTR pInfo |
| 253 | */ |
| 254 | JNIEXPORT__attribute__((visibility("default"))) jobject JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetSessionInfo |
| 255 | (JNIEnv *env, jobject obj, jlong jSessionHandle) |
| 256 | { |
| 257 | CK_SESSION_HANDLE ckSessionHandle; |
| 258 | CK_SESSION_INFO ckSessionInfo; |
| 259 | jobject jSessionInfo=NULL((void*)0); |
| 260 | CK_RV rv; |
| 261 | |
| 262 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
| 263 | if (ckpFunctions == NULL((void*)0)) { return NULL((void*)0); } |
| 264 | |
| 265 | ckSessionHandle = jLongToCKULong(jSessionHandle)((CK_ULONG) jSessionHandle); |
| 266 | |
| 267 | rv = (*ckpFunctions->C_GetSessionInfo)(ckSessionHandle, &ckSessionInfo); |
| 268 | if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK0L) { |
| 269 | jSessionInfo = ckSessionInfoPtrToJSessionInfo(env, &ckSessionInfo); |
| 270 | } |
| 271 | return jSessionInfo ; |
| 272 | } |
| 273 | #endif |
| 274 | |
| 275 | #ifdef P11_ENABLE_C_SESSIONCANCEL |
| 276 | /* |
| 277 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 278 | * Method: C_SessionCancel |
| 279 | * Signature: (JJ)V |
| 280 | * Parametermapping: *PKCS11* |
| 281 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
| 282 | * @param jlong jFlags CK_FLAGS flags |
| 283 | */ |
| 284 | JNIEXPORT__attribute__((visibility("default"))) void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SessionCancel |
| 285 | (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jFlags) |
| 286 | { |
| 287 | CK_SESSION_HANDLE ckSessionHandle; |
| 288 | CK_RV rv; |
| 289 | |
| 290 | CK_FUNCTION_LIST_3_0_PTR ckpFunctions30 = getFunctionList30(env, obj); |
| 291 | if (ckpFunctions30 == NULL((void*)0)) { return; } |
| 292 | |
| 293 | ckSessionHandle = jLongToCKULong(jSessionHandle)((CK_ULONG) jSessionHandle); |
| 294 | |
| 295 | rv = (*ckpFunctions30->C_SessionCancel)(ckSessionHandle, |
| 296 | jLongToCKULong(jFlags)((CK_ULONG) jFlags)); |
| 297 | |
| 298 | ckAssertReturnValueOK(env, rv); |
| 299 | } |
| 300 | #endif |
| 301 | |
| 302 | #ifdef P11_ENABLE_C_GETOPERATIONSTATE |
| 303 | /* |
| 304 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 305 | * Method: C_GetOperationState |
| 306 | * Signature: (J)[B |
| 307 | * Parametermapping: *PKCS11* |
| 308 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
| 309 | * @return jbyteArray jState CK_BYTE_PTR pOperationState |
| 310 | * CK_ULONG_PTR pulOperationStateLen |
| 311 | */ |
| 312 | JNIEXPORT__attribute__((visibility("default"))) jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetOperationState |
| 313 | (JNIEnv *env, jobject obj, jlong jSessionHandle) |
| 314 | { |
| 315 | CK_SESSION_HANDLE ckSessionHandle; |
| 316 | CK_BYTE_PTR ckpState; |
| 317 | CK_ULONG ckStateLength; |
| 318 | jbyteArray jState = NULL((void*)0); |
| 319 | CK_RV rv; |
| 320 | |
| 321 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
| 322 | if (ckpFunctions == NULL((void*)0)) { return NULL((void*)0); } |
| 323 | |
| 324 | ckSessionHandle = jLongToCKULong(jSessionHandle)((CK_ULONG) jSessionHandle); |
| 325 | |
| 326 | rv = (*ckpFunctions->C_GetOperationState)(ckSessionHandle, NULL_PTR0, &ckStateLength); |
| 327 | if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK0L) { return NULL((void*)0) ; } |
| 328 | |
| 329 | ckpState = (CK_BYTE_PTR) malloc(ckStateLength); |
| 330 | if (ckpState == NULL((void*)0)) { |
| 331 | throwOutOfMemoryError(env, 0); |
| 332 | return NULL((void*)0); |
| 333 | } |
| 334 | |
| 335 | rv = (*ckpFunctions->C_GetOperationState)(ckSessionHandle, ckpState, &ckStateLength); |
| 336 | if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK0L) { |
| 337 | jState = ckByteArrayToJByteArray(env, ckpState, ckStateLength); |
| 338 | } |
| 339 | free(ckpState); |
| 340 | |
| 341 | return jState ; |
| 342 | } |
| 343 | #endif |
| 344 | |
| 345 | #ifdef P11_ENABLE_C_SETOPERATIONSTATE |
| 346 | /* |
| 347 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 348 | * Method: C_SetOperationState |
| 349 | * Signature: (J[BJJ)V |
| 350 | * Parametermapping: *PKCS11* |
| 351 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
| 352 | * @param jbyteArray jOperationState CK_BYTE_PTR pOperationState |
| 353 | * CK_ULONG ulOperationStateLen |
| 354 | * @param jlong jEncryptionKeyHandle CK_OBJECT_HANDLE hEncryptionKey |
| 355 | * @param jlong jAuthenticationKeyHandle CK_OBJECT_HANDLE hAuthenticationKey |
| 356 | */ |
| 357 | JNIEXPORT__attribute__((visibility("default"))) void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SetOperationState |
| 358 | (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jOperationState, jlong jEncryptionKeyHandle, jlong jAuthenticationKeyHandle) |
| 359 | { |
| 360 | CK_SESSION_HANDLE ckSessionHandle; |
| 361 | CK_BYTE_PTR ckpState = NULL_PTR0; |
| 362 | CK_ULONG ckStateLength; |
| 363 | CK_OBJECT_HANDLE ckEncryptionKeyHandle; |
| 364 | CK_OBJECT_HANDLE ckAuthenticationKeyHandle; |
| 365 | CK_RV rv; |
| 366 | |
| 367 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
| 368 | if (ckpFunctions == NULL((void*)0)) { return; } |
| 369 | |
| 370 | ckSessionHandle = jLongToCKULong(jSessionHandle)((CK_ULONG) jSessionHandle); |
| 371 | jByteArrayToCKByteArray(env, jOperationState, &ckpState, &ckStateLength); |
| 372 | if ((*env)->ExceptionCheck(env)) { return; } |
| 373 | |
| 374 | ckEncryptionKeyHandle = jLongToCKULong(jEncryptionKeyHandle)((CK_ULONG) jEncryptionKeyHandle); |
| 375 | ckAuthenticationKeyHandle = jLongToCKULong(jAuthenticationKeyHandle)((CK_ULONG) jAuthenticationKeyHandle); |
| 376 | |
| 377 | rv = (*ckpFunctions->C_SetOperationState)(ckSessionHandle, ckpState, ckStateLength, ckEncryptionKeyHandle, ckAuthenticationKeyHandle); |
| 378 | |
| 379 | free(ckpState); |
| 380 | |
| 381 | ckAssertReturnValueOK(env, rv); |
| 382 | } |
| 383 | #endif |
| 384 | |
| 385 | #ifdef P11_ENABLE_C_LOGIN |
| 386 | /* |
| 387 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 388 | * Method: C_Login |
| 389 | * Signature: (JJ[C)V |
| 390 | * Parametermapping: *PKCS11* |
| 391 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
| 392 | * @param jlong jUserType CK_USER_TYPE userType |
| 393 | * @param jcharArray jPin CK_CHAR_PTR pPin |
| 394 | * CK_ULONG ulPinLen |
| 395 | */ |
| 396 | JNIEXPORT__attribute__((visibility("default"))) void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Login |
| 397 | (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jUserType, |
| 398 | jcharArray jPin) |
| 399 | { |
| 400 | CK_SESSION_HANDLE ckSessionHandle; |
| 401 | CK_USER_TYPE ckUserType; |
| 402 | CK_CHAR_PTR ckpPinArray = NULL_PTR0; |
| 403 | CK_ULONG ckPinLength; |
| 404 | CK_RV rv; |
| 405 | CK_FUNCTION_LIST_PTR ckpFunctions; |
| 406 | |
| 407 | ckpFunctions = getFunctionList(env, obj); |
| 408 | |
| 409 | if (ckpFunctions == NULL((void*)0)) { |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | ckSessionHandle = jLongToCKULong(jSessionHandle)((CK_ULONG) jSessionHandle); |
| 414 | ckUserType = jLongToCKULong(jUserType)((CK_ULONG) jUserType); |
| 415 | jCharArrayToCKCharArray(env, jPin, &ckpPinArray, &ckPinLength); |
| 416 | if ((*env)->ExceptionCheck(env)) { return; } |
| 417 | |
| 418 | rv = (*ckpFunctions->C_Login)(ckSessionHandle, ckUserType, ckpPinArray, |
| 419 | ckPinLength); |
| 420 | free(ckpPinArray); |
| 421 | |
| 422 | ckAssertReturnValueOK(env, rv); |
| 423 | } |
| 424 | #endif |
| 425 | |
| 426 | #ifdef P11_ENABLE_C_LOGINUSER |
| 427 | /* |
| 428 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 429 | * Method: C_LoginUser |
| 430 | * Signature: (JJ[C;Ljava/lang/String;)V |
| 431 | * Parametermapping: *PKCS11* |
| 432 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
| 433 | * @param jlong jUserType CK_USER_TYPE userType |
| 434 | * @param jcharArray jPin CK_CHAR_PTR pPin |
| 435 | * CK_ULONG ulPinLen |
| 436 | * @param jstring jUsername CK_CHAR_PTR pUsername |
| 437 | * CK_ULONG ulUsernameLen |
| 438 | */ |
| 439 | JNIEXPORT__attribute__((visibility("default"))) void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1LoginUser |
| 440 | (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jUserType, |
| 441 | jcharArray jPin, jstring jUsername) |
| 442 | { |
| 443 | CK_SESSION_HANDLE ckSessionHandle; |
| 444 | CK_USER_TYPE ckUserType; |
| 445 | CK_CHAR_PTR ckpPinArray = NULL_PTR0; |
| 446 | CK_ULONG ckPinLength; |
| 447 | CK_CHAR_PTR ckpUsername = NULL_PTR0; |
| 448 | CK_ULONG ckUsernameLength; |
| 449 | CK_RV rv; |
| 450 | CK_FUNCTION_LIST_3_0_PTR ckpFunctions30; |
| 451 | |
| 452 | ckpFunctions30 = getFunctionList30(env, obj); |
| 453 | |
| 454 | ckSessionHandle = jLongToCKULong(jSessionHandle)((CK_ULONG) jSessionHandle); |
| 455 | ckUserType = jLongToCKULong(jUserType)((CK_ULONG) jUserType); |
| 456 | jCharArrayToCKCharArray(env, jPin, &ckpPinArray, &ckPinLength); |
| 457 | if ((*env)->ExceptionCheck(env)) { return; } |
| 458 | jStringToCKUTF8CharArray(env, jUsername, &ckpUsername, |
| 459 | &ckUsernameLength); |
| 460 | if ((*env)->ExceptionCheck(env)) { return; } |
| 461 | |
| 462 | if (ckpFunctions30 == NULL((void*)0)) { |
| 463 | return; |
| 464 | } |
| 465 | rv = (*ckpFunctions30->C_LoginUser)(ckSessionHandle, ckUserType, |
| 466 | ckpPinArray, ckPinLength, ckpUsername, ckUsernameLength); |
| 467 | |
| 468 | free(ckpPinArray); |
| 469 | free(ckpUsername); |
| 470 | |
| 471 | ckAssertReturnValueOK(env, rv); |
| 472 | } |
| 473 | |
| 474 | #endif |
| 475 | |
| 476 | #ifdef P11_ENABLE_C_LOGOUT |
| 477 | /* |
| 478 | * Class: sun_security_pkcs11_wrapper_PKCS11 |
| 479 | * Method: C_Logout |
| 480 | * Signature: (J)V |
| 481 | * Parametermapping: *PKCS11* |
| 482 | * @param jlong jSessionHandle CK_SESSION_HANDLE hSession |
| 483 | */ |
| 484 | JNIEXPORT__attribute__((visibility("default"))) void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Logout |
| 485 | (JNIEnv *env, jobject obj, jlong jSessionHandle) |
| 486 | { |
| 487 | CK_SESSION_HANDLE ckSessionHandle; |
| 488 | CK_RV rv; |
| 489 | |
| 490 | CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); |
| 491 | if (ckpFunctions == NULL((void*)0)) { return; } |
| 492 | |
| 493 | ckSessionHandle = jLongToCKULong(jSessionHandle)((CK_ULONG) jSessionHandle); |
| 494 | |
| 495 | rv = (*ckpFunctions->C_Logout)(ckSessionHandle); |
| 496 | ckAssertReturnValueOK(env, rv); |
| 497 | } |
| 498 | #endif |
| 499 | |
| 500 | /* ************************************************************************** */ |
| 501 | /* Functions for keeping track of notify callbacks */ |
| 502 | /* ************************************************************************** */ |
| 503 | |
| 504 | #ifndef NO_CALLBACKS |
| 505 | |
| 506 | /* |
| 507 | * Add the given notify encapsulation object to the list of active notify |
| 508 | * objects. |
| 509 | * If notifyEncapsulation is NULL, this function does nothing. |
| 510 | */ |
| 511 | void putNotifyEntry(JNIEnv *env, CK_SESSION_HANDLE hSession, NotifyEncapsulation *notifyEncapsulation) { |
| 512 | NotifyListNode *currentNode, *newNode; |
| 513 | |
| 514 | if (notifyEncapsulation == NULL((void*)0)) { |
| 515 | return; |
| 516 | } |
| 517 | |
| 518 | newNode = (NotifyListNode *) malloc(sizeof(NotifyListNode)); |
| 519 | if (newNode == NULL((void*)0)) { |
| 520 | throwOutOfMemoryError(env, 0); |
| 521 | return; |
| 522 | } |
| 523 | newNode->hSession = hSession; |
| 524 | newNode->notifyEncapsulation = notifyEncapsulation; |
| 525 | newNode->next = NULL((void*)0); |
| 526 | |
| 527 | (*env)->MonitorEnter(env, notifyListLock); /* synchronize access to list */ |
| 528 | |
| 529 | if (notifyListHead == NULL((void*)0)) { |
| 530 | /* this is the first entry */ |
| 531 | notifyListHead = newNode; |
| 532 | } else { |
| 533 | /* go to the last entry; i.e. the first node which's 'next' is NULL. |
| 534 | */ |
| 535 | currentNode = notifyListHead; |
| 536 | while (currentNode->next != NULL((void*)0)) { |
| 537 | currentNode = currentNode->next; |
| 538 | } |
| 539 | currentNode->next = newNode; |
| 540 | } |
| 541 | |
| 542 | (*env)->MonitorExit(env, notifyListLock); /* synchronize access to list */ |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | * Removes the active notifyEncapsulation object used with the given session and |
| 547 | * returns it. If there is no notifyEncapsulation active for this session, this |
| 548 | * function returns NULL. |
| 549 | */ |
| 550 | NotifyEncapsulation * removeNotifyEntry(JNIEnv *env, CK_SESSION_HANDLE hSession) { |
| 551 | NotifyEncapsulation *notifyEncapsulation; |
| 552 | NotifyListNode *currentNode, *previousNode; |
| 553 | |
| 554 | (*env)->MonitorEnter(env, notifyListLock); /* synchronize access to list */ |
| 555 | |
| 556 | if (notifyListHead == NULL((void*)0)) { |
| 557 | /* this is the first entry */ |
| 558 | notifyEncapsulation = NULL((void*)0); |
| 559 | } else { |
| 560 | /* Find the node with the wanted session handle. Also stop, when we reach |
| 561 | * the last entry; i.e. the first node which's 'next' is NULL. |
| 562 | */ |
| 563 | currentNode = notifyListHead; |
| 564 | previousNode = NULL((void*)0); |
| 565 | |
| 566 | while ((currentNode->hSession != hSession) && (currentNode->next != NULL((void*)0))) { |
| 567 | previousNode = currentNode; |
| 568 | currentNode = currentNode->next; |
| 569 | } |
| 570 | |
| 571 | if (currentNode->hSession == hSession) { |
| 572 | /* We found a entry for the wanted session, now remove it. */ |
| 573 | if (previousNode == NULL((void*)0)) { |
| 574 | /* it's the first node */ |
| 575 | notifyListHead = currentNode->next; |
| 576 | } else { |
| 577 | previousNode->next = currentNode->next; |
| 578 | } |
| 579 | notifyEncapsulation = currentNode->notifyEncapsulation; |
| 580 | free(currentNode); |
| 581 | } else { |
| 582 | /* We did not find a entry for this session */ |
| 583 | notifyEncapsulation = NULL((void*)0); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | (*env)->MonitorExit(env, notifyListLock); /* synchronize access to list */ |
| 588 | |
| 589 | return notifyEncapsulation ; |
| 590 | } |
| 591 | |
| 592 | /* |
| 593 | |
| 594 | * Removes the first notifyEncapsulation object. If there is no notifyEncapsulation, |
| 595 | * this function returns NULL. |
| 596 | */ |
| 597 | NotifyEncapsulation * removeFirstNotifyEntry(JNIEnv *env) { |
| 598 | NotifyEncapsulation *notifyEncapsulation; |
| 599 | NotifyListNode *currentNode; |
| 600 | |
| 601 | (*env)->MonitorEnter(env, notifyListLock); /* synchronize access to list */ |
| 602 | |
| 603 | if (notifyListHead == NULL((void*)0)) { |
| 604 | /* this is the first entry */ |
| 605 | notifyEncapsulation = NULL((void*)0); |
| 606 | } else { |
| 607 | /* Remove the first entry. */ |
| 608 | currentNode = notifyListHead; |
| 609 | notifyListHead = notifyListHead->next; |
| 610 | notifyEncapsulation = currentNode->notifyEncapsulation; |
| 611 | free(currentNode); |
| 612 | } |
| 613 | |
| 614 | (*env)->MonitorExit(env, notifyListLock); /* synchronize access to list */ |
| 615 | |
| 616 | return notifyEncapsulation ; |
| 617 | } |
| 618 | |
| 619 | #endif /* NO_CALLBACKS */ |
| 620 | |
| 621 | #ifndef NO_CALLBACKS |
| 622 | |
| 623 | /* |
| 624 | * The function handling notify callbacks. It casts the pApplication parameter |
| 625 | * back to a NotifyEncapsulation structure and retrieves the Notify object and |
| 626 | * the application data from it. |
| 627 | * |
| 628 | * @param hSession The session, this callback is comming from. |
| 629 | * @param event The type of event that occurred. |
| 630 | * @param pApplication The application data as passed in upon OpenSession. In |
| 631 | this wrapper we always pass in a NotifyEncapsulation |
| 632 | object, which holds necessary information for delegating |
| 633 | the callback to the Java VM. |
| 634 | * @return |
| 635 | */ |
| 636 | CK_RV notifyCallback( |
| 637 | CK_SESSION_HANDLE hSession, /* the session's handle */ |
| 638 | CK_NOTIFICATION event, |
| 639 | CK_VOID_PTR pApplication /* passed to C_OpenSession */ |
| 640 | ) |
| 641 | { |
| 642 | NotifyEncapsulation *notifyEncapsulation; |
| 643 | extern JavaVM *jvm; |
| 644 | JNIEnv *env; |
| 645 | jint returnValue; |
| 646 | jlong jSessionHandle; |
| 647 | jlong jEvent; |
| 648 | jclass ckNotifyClass; |
| 649 | jmethodID jmethod; |
| 650 | jthrowable pkcs11Exception; |
| 651 | jclass pkcs11ExceptionClass; |
| 652 | jlong errorCode; |
| 653 | CK_RV rv = CKR_OK0x00000000UL; |
| 654 | int wasAttached = 1; |
| 655 | |
| 656 | if (pApplication == NULL((void*)0)) { return rv ; } /* This should not occur in this wrapper. */ |
| 657 | |
| 658 | notifyEncapsulation = (NotifyEncapsulation *) pApplication; |
| 659 | |
| 660 | /* Get the currently running Java VM */ |
| 661 | if (jvm == NULL((void*)0)) { return rv ; } /* there is no VM running */ |
| 662 | |
| 663 | /* Determine, if current thread is already attached */ |
| 664 | returnValue = (*jvm)->GetEnv(jvm, (void **) &env, JNI_VERSION_1_20x00010002); |
| 665 | if (returnValue == JNI_EDETACHED(-2)) { |
| 666 | /* thread detached, so attach it */ |
| 667 | wasAttached = 0; |
| 668 | returnValue = (*jvm)->AttachCurrentThread(jvm, (void **) &env, NULL((void*)0)); |
| 669 | } else if (returnValue == JNI_EVERSION(-3)) { |
| 670 | /* this version of JNI is not supported, so just try to attach */ |
| 671 | /* we assume it was attached to ensure that this thread is not detached |
| 672 | * afterwards even though it should not |
| 673 | */ |
| 674 | wasAttached = 1; |
| 675 | returnValue = (*jvm)->AttachCurrentThread(jvm, (void **) &env, NULL((void*)0)); |
| 676 | } else { |
| 677 | /* attached */ |
| 678 | wasAttached = 1; |
| 679 | } |
| 680 | |
| 681 | jSessionHandle = ckULongToJLong(hSession)((jlong) hSession); |
| 682 | jEvent = ckULongToJLong(event)((jlong) event); |
| 683 | |
| 684 | ckNotifyClass = (*env)->FindClass(env, CLASS_NOTIFY"sun/security/pkcs11/wrapper/CK_NOTIFY"); |
| 685 | if (ckNotifyClass == NULL((void*)0)) { return rv; } |
| 686 | jmethod = (*env)->GetMethodID(env, ckNotifyClass, "CK_NOTIFY", "(JJLjava/lang/Object;)V"); |
| 687 | if (jmethod == NULL((void*)0)) { return rv; } |
| 688 | |
| 689 | (*env)->CallVoidMethod(env, notifyEncapsulation->jNotifyObject, jmethod, |
| 690 | jSessionHandle, jEvent, notifyEncapsulation->jApplicationData); |
| 691 | |
| 692 | /* check, if callback threw an exception */ |
| 693 | pkcs11Exception = (*env)->ExceptionOccurred(env); |
| 694 | |
| 695 | if (pkcs11Exception != NULL((void*)0)) { |
| 696 | /* TBD: clear the pending exception with ExceptionClear? */ |
| 697 | /* The was an exception thrown, now we get the error-code from it */ |
| 698 | pkcs11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION"sun/security/pkcs11/wrapper/PKCS11Exception"); |
| 699 | if (pkcs11ExceptionClass == NULL((void*)0)) { return rv; } |
| 700 | |
| 701 | jmethod = (*env)->GetMethodID(env, pkcs11ExceptionClass, "getErrorCode", "()J"); |
| 702 | if (jmethod == NULL((void*)0)) { return rv; } |
| 703 | |
| 704 | errorCode = (*env)->CallLongMethod(env, pkcs11Exception, jmethod); |
| 705 | rv = jLongToCKULong(errorCode)((CK_ULONG) errorCode); |
| 706 | } |
| 707 | |
| 708 | /* if we attached this thread to the VM just for callback, we detach it now */ |
| 709 | if (wasAttached) { |
| 710 | returnValue = (*jvm)->DetachCurrentThread(jvm); |
Value stored to 'returnValue' is never read | |
| 711 | } |
| 712 | |
| 713 | return rv ; |
| 714 | } |
| 715 | |
| 716 | #endif /* NO_CALLBACKS */ |