0N/A/*
2362N/A * Copyright (c) 2003, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A */
0N/A
0N/A/* Copyright (c) 2002 Graz University of Technology. All rights reserved.
0N/A *
0N/A * Redistribution and use in source and binary forms, with or without
0N/A * modification, are permitted provided that the following conditions are met:
0N/A *
0N/A * 1. Redistributions of source code must retain the above copyright notice,
0N/A * this list of conditions and the following disclaimer.
0N/A *
0N/A * 2. Redistributions in binary form must reproduce the above copyright notice,
0N/A * this list of conditions and the following disclaimer in the documentation
0N/A * and/or other materials provided with the distribution.
0N/A *
0N/A * 3. The end-user documentation included with the redistribution, if any, must
0N/A * include the following acknowledgment:
0N/A *
0N/A * "This product includes software developed by IAIK of Graz University of
0N/A * Technology."
0N/A *
0N/A * Alternately, this acknowledgment may appear in the software itself, if
0N/A * and wherever such third-party acknowledgments normally appear.
0N/A *
0N/A * 4. The names "Graz University of Technology" and "IAIK of Graz University of
0N/A * Technology" must not be used to endorse or promote products derived from
0N/A * this software without prior written permission.
0N/A *
0N/A * 5. Products derived from this software may not be called
0N/A * "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior
0N/A * written permission of Graz University of Technology.
0N/A *
0N/A * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
0N/A * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
0N/A * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE
0N/A * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
0N/A * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
0N/A * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
0N/A * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
0N/A * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0N/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0N/A * POSSIBILITY OF SUCH DAMAGE.
0N/A */
0N/A
0N/A#include "pkcs11wrapper.h"
0N/A
0N/A#include <stdio.h>
0N/A#include <stdlib.h>
0N/A#include <string.h>
0N/A#include <assert.h>
0N/A
0N/A#include "sun_security_pkcs11_wrapper_PKCS11.h"
0N/A
0N/A/* The list of notify callback handles that are currently active and waiting
0N/A * for callbacks from their sessions.
0N/A */
0N/A#ifndef NO_CALLBACKS
0N/ANotifyListNode *notifyListHead = NULL;
0N/Ajobject notifyListLock = NULL;
0N/A#endif /* NO_CALLBACKS */
0N/A
0N/A#ifdef P11_ENABLE_C_OPENSESSION
0N/A/*
0N/A * Class: sun_security_pkcs11_wrapper_PKCS11
0N/A * Method: C_OpenSession
0N/A * Signature: (JJLjava/lang/Object;Lsun/security/pkcs11/wrapper/CK_NOTIFY;)J
0N/A * Parametermapping: *PKCS11*
0N/A * @param jlong jSlotID CK_SLOT_ID slotID
0N/A * @param jlong jFlags CK_FLAGS flags
0N/A * @param jobject jApplication CK_VOID_PTR pApplication
0N/A * @param jobject jNotify CK_NOTIFY Notify
0N/A * @return jlong jSessionHandle CK_SESSION_HANDLE_PTR phSession
0N/A */
0N/AJNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1OpenSession
0N/A (JNIEnv *env, jobject obj, jlong jSlotID, jlong jFlags, jobject jApplication, jobject jNotify)
0N/A{
0N/A CK_SESSION_HANDLE ckSessionHandle;
0N/A CK_SLOT_ID ckSlotID;
0N/A CK_FLAGS ckFlags;
0N/A CK_VOID_PTR ckpApplication;
0N/A CK_NOTIFY ckNotify;
0N/A jlong jSessionHandle;
0N/A CK_RV rv;
0N/A#ifndef NO_CALLBACKS
0N/A NotifyEncapsulation *notifyEncapsulation = NULL;
0N/A#endif /* NO_CALLBACKS */
0N/A
0N/A CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
0N/A if (ckpFunctions == NULL) { return 0L; }
0N/A
0N/A ckSlotID = jLongToCKULong(jSlotID);
0N/A ckFlags = jLongToCKULong(jFlags);
0N/A
0N/A#ifndef NO_CALLBACKS
0N/A if (jNotify != NULL) {
0N/A notifyEncapsulation = (NotifyEncapsulation *) malloc(sizeof(NotifyEncapsulation));
936N/A if (notifyEncapsulation == NULL) {
936N/A JNU_ThrowOutOfMemoryError(env, 0);
936N/A return 0L;
936N/A }
0N/A notifyEncapsulation->jApplicationData = (jApplication != NULL)
0N/A ? (*env)->NewGlobalRef(env, jApplication)
0N/A : NULL;
0N/A notifyEncapsulation->jNotifyObject = (*env)->NewGlobalRef(env, jNotify);
0N/A ckpApplication = notifyEncapsulation;
0N/A ckNotify = (CK_NOTIFY) &notifyCallback;
0N/A } else {
0N/A ckpApplication = NULL_PTR;
0N/A ckNotify = NULL_PTR;
0N/A }
0N/A#else
0N/A ckpApplication = NULL_PTR;
0N/A ckNotify = NULL_PTR;
0N/A#endif /* NO_CALLBACKS */
0N/A
0N/A TRACE0("DEBUG: C_OpenSession");
0N/A TRACE1(", slotID=%u", ckSlotID);
0N/A TRACE1(", flags=%x", ckFlags);
0N/A TRACE0(" ... ");
0N/A
0N/A rv = (*ckpFunctions->C_OpenSession)(ckSlotID, ckFlags, ckpApplication, ckNotify, &ckSessionHandle);
936N/A if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {
936N/A#ifndef NO_CALLBACKS
936N/A if (notifyEncapsulation != NULL) {
936N/A if (notifyEncapsulation->jApplicationData != NULL) {
936N/A (*env)->DeleteGlobalRef(env, jApplication);
936N/A }
936N/A (*env)->DeleteGlobalRef(env, jNotify);
936N/A free(notifyEncapsulation);
936N/A }
936N/A#endif /* NO_CALLBACKS */
936N/A return 0L;
936N/A }
0N/A
0N/A TRACE0("got session");
0N/A TRACE1(", SessionHandle=%u", ckSessionHandle);
0N/A TRACE0(" ... ");
0N/A
0N/A jSessionHandle = ckULongToJLong(ckSessionHandle);
0N/A
0N/A#ifndef NO_CALLBACKS
0N/A if (notifyEncapsulation != NULL) {
0N/A /* store the notifyEncapsulation to enable later cleanup */
0N/A putNotifyEntry(env, ckSessionHandle, notifyEncapsulation);
0N/A }
0N/A#endif /* NO_CALLBACKS */
0N/A
0N/A TRACE0("FINISHED\n");
0N/A
0N/A return jSessionHandle ;
0N/A}
0N/A#endif
0N/A
0N/A#ifdef P11_ENABLE_C_CLOSESESSION
0N/A/*
0N/A * Class: sun_security_pkcs11_wrapper_PKCS11
0N/A * Method: C_CloseSession
0N/A * Signature: (J)V
0N/A * Parametermapping: *PKCS11*
0N/A * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
0N/A */
0N/AJNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CloseSession
0N/A (JNIEnv *env, jobject obj, jlong jSessionHandle)
0N/A{
0N/A CK_SESSION_HANDLE ckSessionHandle;
0N/A CK_RV rv;
0N/A#ifndef NO_CALLBACKS
0N/A NotifyEncapsulation *notifyEncapsulation;
0N/A jobject jApplicationData;
0N/A#endif /* NO_CALLBACKS */
0N/A
0N/A CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
0N/A if (ckpFunctions == NULL) { return; }
0N/A
0N/A ckSessionHandle = jLongToCKULong(jSessionHandle);
0N/A
0N/A rv = (*ckpFunctions->C_CloseSession)(ckSessionHandle);
936N/A if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
0N/A
0N/A#ifndef NO_CALLBACKS
0N/A notifyEncapsulation = removeNotifyEntry(env, ckSessionHandle);
0N/A
0N/A if (notifyEncapsulation != NULL) {
0N/A /* there was a notify object used with this session, now dump the
0N/A * encapsulation object
0N/A */
0N/A (*env)->DeleteGlobalRef(env, notifyEncapsulation->jNotifyObject);
0N/A jApplicationData = notifyEncapsulation->jApplicationData;
0N/A if (jApplicationData != NULL) {
0N/A (*env)->DeleteGlobalRef(env, jApplicationData);
0N/A }
0N/A free(notifyEncapsulation);
0N/A }
0N/A#endif /* NO_CALLBACKS */
0N/A
0N/A}
0N/A#endif
0N/A
0N/A#ifdef P11_ENABLE_C_CLOSEALLSESSIONS
0N/A/*
0N/A * Class: sun_security_pkcs11_wrapper_PKCS11
0N/A * Method: C_CloseAllSessions
0N/A * Signature: (J)V
0N/A * Parametermapping: *PKCS11*
0N/A * @param jlong jSlotID CK_SLOT_ID slotID
0N/A */
0N/AJNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CloseAllSessions
0N/A (JNIEnv *env, jobject obj, jlong jSlotID)
0N/A{
0N/A CK_SLOT_ID ckSlotID;
0N/A CK_RV rv;
0N/A#ifndef NO_CALLBACKS
0N/A NotifyEncapsulation *notifyEncapsulation;
0N/A jobject jApplicationData;
0N/A#endif /* NO_CALLBACKS */
0N/A
0N/A CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
0N/A if (ckpFunctions == NULL) { return; }
0N/A
0N/A ckSlotID = jLongToCKULong(jSlotID);
0N/A
0N/A rv = (*ckpFunctions->C_CloseAllSessions)(ckSlotID);
936N/A if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
0N/A
0N/A#ifndef NO_CALLBACKS
0N/A /* Remove all notify callback helper objects. */
0N/A while ((notifyEncapsulation = removeFirstNotifyEntry(env)) != NULL) {
0N/A /* there was a notify object used with this session, now dump the
0N/A * encapsulation object
0N/A */
0N/A (*env)->DeleteGlobalRef(env, notifyEncapsulation->jNotifyObject);
0N/A jApplicationData = notifyEncapsulation->jApplicationData;
0N/A if (jApplicationData != NULL) {
0N/A (*env)->DeleteGlobalRef(env, jApplicationData);
0N/A }
0N/A free(notifyEncapsulation);
0N/A }
0N/A#endif /* NO_CALLBACKS */
0N/A}
0N/A#endif
0N/A
0N/A#ifdef P11_ENABLE_C_GETSESSIONINFO
0N/A/*
0N/A * Class: sun_security_pkcs11_wrapper_PKCS11
0N/A * Method: C_GetSessionInfo
0N/A * Signature: (J)Lsun/security/pkcs11/wrapper/CK_SESSION_INFO;
0N/A * Parametermapping: *PKCS11*
0N/A * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
0N/A * @return jobject jSessionInfo CK_SESSION_INFO_PTR pInfo
0N/A */
0N/AJNIEXPORT jobject JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetSessionInfo
0N/A (JNIEnv *env, jobject obj, jlong jSessionHandle)
0N/A{
0N/A CK_SESSION_HANDLE ckSessionHandle;
0N/A CK_SESSION_INFO ckSessionInfo;
0N/A jobject jSessionInfo;
0N/A CK_RV rv;
0N/A
0N/A CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
0N/A if (ckpFunctions == NULL) { return NULL; }
0N/A
0N/A ckSessionHandle = jLongToCKULong(jSessionHandle);
0N/A
0N/A rv = (*ckpFunctions->C_GetSessionInfo)(ckSessionHandle, &ckSessionInfo);
936N/A if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
936N/A jSessionInfo = ckSessionInfoPtrToJSessionInfo(env, &ckSessionInfo);
936N/A }
0N/A return jSessionInfo ;
0N/A}
0N/A#endif
0N/A
0N/A#ifdef P11_ENABLE_C_GETOPERATIONSTATE
0N/A/*
0N/A * Class: sun_security_pkcs11_wrapper_PKCS11
0N/A * Method: C_GetOperationState
0N/A * Signature: (J)[B
0N/A * Parametermapping: *PKCS11*
0N/A * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
0N/A * @return jbyteArray jState CK_BYTE_PTR pOperationState
0N/A * CK_ULONG_PTR pulOperationStateLen
0N/A */
0N/AJNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetOperationState
0N/A (JNIEnv *env, jobject obj, jlong jSessionHandle)
0N/A{
0N/A CK_SESSION_HANDLE ckSessionHandle;
0N/A CK_BYTE_PTR ckpState;
0N/A CK_ULONG ckStateLength;
936N/A jbyteArray jState = NULL;
0N/A CK_RV rv;
0N/A
0N/A CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
0N/A if (ckpFunctions == NULL) { return NULL; }
0N/A
0N/A ckSessionHandle = jLongToCKULong(jSessionHandle);
0N/A
0N/A rv = (*ckpFunctions->C_GetOperationState)(ckSessionHandle, NULL_PTR, &ckStateLength);
936N/A if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; }
0N/A
0N/A ckpState = (CK_BYTE_PTR) malloc(ckStateLength);
936N/A if (ckpState == NULL) {
936N/A JNU_ThrowOutOfMemoryError(env, 0);
936N/A return NULL;
936N/A }
0N/A
0N/A rv = (*ckpFunctions->C_GetOperationState)(ckSessionHandle, ckpState, &ckStateLength);
936N/A if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
936N/A jState = ckByteArrayToJByteArray(env, ckpState, ckStateLength);
936N/A }
0N/A free(ckpState);
0N/A
0N/A return jState ;
0N/A}
0N/A#endif
0N/A
0N/A#ifdef P11_ENABLE_C_SETOPERATIONSTATE
0N/A/*
0N/A * Class: sun_security_pkcs11_wrapper_PKCS11
0N/A * Method: C_SetOperationState
0N/A * Signature: (J[BJJ)V
0N/A * Parametermapping: *PKCS11*
0N/A * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
0N/A * @param jbyteArray jOperationState CK_BYTE_PTR pOperationState
0N/A * CK_ULONG ulOperationStateLen
0N/A * @param jlong jEncryptionKeyHandle CK_OBJECT_HANDLE hEncryptionKey
0N/A * @param jlong jAuthenticationKeyHandle CK_OBJECT_HANDLE hAuthenticationKey
0N/A */
0N/AJNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SetOperationState
0N/A (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jOperationState, jlong jEncryptionKeyHandle, jlong jAuthenticationKeyHandle)
0N/A{
0N/A CK_SESSION_HANDLE ckSessionHandle;
0N/A CK_BYTE_PTR ckpState = NULL_PTR;
0N/A CK_ULONG ckStateLength;
0N/A CK_OBJECT_HANDLE ckEncryptionKeyHandle;
0N/A CK_OBJECT_HANDLE ckAuthenticationKeyHandle;
0N/A CK_RV rv;
0N/A
0N/A CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
0N/A if (ckpFunctions == NULL) { return; }
0N/A
0N/A ckSessionHandle = jLongToCKULong(jSessionHandle);
0N/A jByteArrayToCKByteArray(env, jOperationState, &ckpState, &ckStateLength);
936N/A if ((*env)->ExceptionCheck(env)) { return; }
936N/A
0N/A ckEncryptionKeyHandle = jLongToCKULong(jEncryptionKeyHandle);
0N/A ckAuthenticationKeyHandle = jLongToCKULong(jAuthenticationKeyHandle);
0N/A
0N/A rv = (*ckpFunctions->C_SetOperationState)(ckSessionHandle, ckpState, ckStateLength, ckEncryptionKeyHandle, ckAuthenticationKeyHandle);
0N/A
0N/A free(ckpState);
0N/A
936N/A if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
0N/A}
0N/A#endif
0N/A
0N/A#ifdef P11_ENABLE_C_LOGIN
0N/A/*
0N/A * Class: sun_security_pkcs11_wrapper_PKCS11
0N/A * Method: C_Login
0N/A * Signature: (JJ[C)V
0N/A * Parametermapping: *PKCS11*
0N/A * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
0N/A * @param jlong jUserType CK_USER_TYPE userType
0N/A * @param jcharArray jPin CK_CHAR_PTR pPin
0N/A * CK_ULONG ulPinLen
0N/A */
0N/AJNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Login
0N/A (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jUserType, jcharArray jPin)
0N/A{
0N/A CK_SESSION_HANDLE ckSessionHandle;
0N/A CK_USER_TYPE ckUserType;
0N/A CK_CHAR_PTR ckpPinArray = NULL_PTR;
0N/A CK_ULONG ckPinLength;
0N/A CK_RV rv;
0N/A
0N/A CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
0N/A if (ckpFunctions == NULL) { return; }
0N/A
0N/A ckSessionHandle = jLongToCKULong(jSessionHandle);
0N/A ckUserType = jLongToCKULong(jUserType);
0N/A jCharArrayToCKCharArray(env, jPin, &ckpPinArray, &ckPinLength);
936N/A if ((*env)->ExceptionCheck(env)) { return; }
0N/A
0N/A rv = (*ckpFunctions->C_Login)(ckSessionHandle, ckUserType, ckpPinArray, ckPinLength);
0N/A
0N/A free(ckpPinArray);
0N/A
936N/A if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
0N/A}
0N/A#endif
0N/A
0N/A#ifdef P11_ENABLE_C_LOGOUT
0N/A/*
0N/A * Class: sun_security_pkcs11_wrapper_PKCS11
0N/A * Method: C_Logout
0N/A * Signature: (J)V
0N/A * Parametermapping: *PKCS11*
0N/A * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
0N/A */
0N/AJNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Logout
0N/A (JNIEnv *env, jobject obj, jlong jSessionHandle)
0N/A{
0N/A CK_SESSION_HANDLE ckSessionHandle;
0N/A CK_RV rv;
0N/A
0N/A CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
0N/A if (ckpFunctions == NULL) { return; }
0N/A
0N/A ckSessionHandle = jLongToCKULong(jSessionHandle);
0N/A
0N/A rv = (*ckpFunctions->C_Logout)(ckSessionHandle);
936N/A if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
0N/A}
0N/A#endif
0N/A
0N/A/* ************************************************************************** */
0N/A/* Functions for keeping track of notify callbacks */
0N/A/* ************************************************************************** */
0N/A
0N/A#ifndef NO_CALLBACKS
0N/A
0N/A/*
0N/A * Add the given notify encapsulation object to the list of active notify
0N/A * objects.
0N/A * If notifyEncapsulation is NULL, this function does nothing.
0N/A */
0N/Avoid putNotifyEntry(JNIEnv *env, CK_SESSION_HANDLE hSession, NotifyEncapsulation *notifyEncapsulation) {
0N/A NotifyListNode *currentNode, *newNode;
0N/A
0N/A if (notifyEncapsulation == NULL) {
936N/A return;
0N/A }
0N/A
0N/A newNode = (NotifyListNode *) malloc(sizeof(NotifyListNode));
936N/A if (newNode == NULL) {
936N/A JNU_ThrowOutOfMemoryError(env, 0);
936N/A return;
936N/A }
0N/A newNode->hSession = hSession;
0N/A newNode->notifyEncapsulation = notifyEncapsulation;
0N/A newNode->next = NULL;
0N/A
0N/A (*env)->MonitorEnter(env, notifyListLock); /* synchronize access to list */
0N/A
0N/A if (notifyListHead == NULL) {
0N/A /* this is the first entry */
0N/A notifyListHead = newNode;
0N/A } else {
0N/A /* go to the last entry; i.e. the first node which's 'next' is NULL.
0N/A */
0N/A currentNode = notifyListHead;
0N/A while (currentNode->next != NULL) {
0N/A currentNode = currentNode->next;
0N/A }
0N/A currentNode->next = newNode;
0N/A }
0N/A
0N/A (*env)->MonitorExit(env, notifyListLock); /* synchronize access to list */
0N/A}
0N/A
0N/A/*
0N/A * Removes the active notifyEncapsulation object used with the given session and
0N/A * returns it. If there is no notifyEncapsulation active for this session, this
0N/A * function returns NULL.
0N/A */
0N/ANotifyEncapsulation * removeNotifyEntry(JNIEnv *env, CK_SESSION_HANDLE hSession) {
0N/A NotifyEncapsulation *notifyEncapsulation;
0N/A NotifyListNode *currentNode, *previousNode;
0N/A
0N/A (*env)->MonitorEnter(env, notifyListLock); /* synchronize access to list */
0N/A
0N/A if (notifyListHead == NULL) {
0N/A /* this is the first entry */
0N/A notifyEncapsulation = NULL;
0N/A } else {
0N/A /* Find the node with the wanted session handle. Also stop, when we reach
0N/A * the last entry; i.e. the first node which's 'next' is NULL.
0N/A */
0N/A currentNode = notifyListHead;
0N/A previousNode = NULL;
0N/A
0N/A while ((currentNode->hSession != hSession) && (currentNode->next != NULL)) {
0N/A previousNode = currentNode;
0N/A currentNode = currentNode->next;
0N/A }
0N/A
0N/A if (currentNode->hSession == hSession) {
0N/A /* We found a entry for the wanted session, now remove it. */
0N/A if (previousNode == NULL) {
0N/A /* it's the first node */
0N/A notifyListHead = currentNode->next;
0N/A } else {
0N/A previousNode->next = currentNode->next;
0N/A }
0N/A notifyEncapsulation = currentNode->notifyEncapsulation;
0N/A free(currentNode);
0N/A } else {
0N/A /* We did not find a entry for this session */
0N/A notifyEncapsulation = NULL;
0N/A }
0N/A }
0N/A
0N/A (*env)->MonitorExit(env, notifyListLock); /* synchronize access to list */
0N/A
0N/A return notifyEncapsulation ;
0N/A}
0N/A
0N/A/*
0N/A
0N/A * Removes the first notifyEncapsulation object. If there is no notifyEncapsulation,
0N/A * this function returns NULL.
0N/A */
0N/ANotifyEncapsulation * removeFirstNotifyEntry(JNIEnv *env) {
0N/A NotifyEncapsulation *notifyEncapsulation;
0N/A NotifyListNode *currentNode;
0N/A
0N/A (*env)->MonitorEnter(env, notifyListLock); /* synchronize access to list */
0N/A
0N/A if (notifyListHead == NULL) {
0N/A /* this is the first entry */
0N/A notifyEncapsulation = NULL;
0N/A } else {
0N/A /* Remove the first entry. */
0N/A currentNode = notifyListHead;
0N/A notifyListHead = notifyListHead->next;
0N/A notifyEncapsulation = currentNode->notifyEncapsulation;
0N/A free(currentNode);
0N/A }
0N/A
0N/A (*env)->MonitorExit(env, notifyListLock); /* synchronize access to list */
0N/A
0N/A return notifyEncapsulation ;
0N/A}
0N/A
0N/A#endif /* NO_CALLBACKS */
0N/A
0N/A#ifndef NO_CALLBACKS
0N/A
0N/A/*
0N/A * The function handling notify callbacks. It casts the pApplication paramter
0N/A * back to a NotifyEncapsulation structure and retrieves the Notify object and
0N/A * the application data from it.
0N/A *
0N/A * @param hSession The session, this callback is comming from.
0N/A * @param event The type of event that occurred.
0N/A * @param pApplication The application data as passed in upon OpenSession. In
0N/A this wrapper we always pass in a NotifyEncapsulation
0N/A object, which holds necessary information for delegating
0N/A the callback to the Java VM.
0N/A * @return
0N/A */
0N/ACK_RV notifyCallback(
0N/A CK_SESSION_HANDLE hSession, /* the session's handle */
0N/A CK_NOTIFICATION event,
0N/A CK_VOID_PTR pApplication /* passed to C_OpenSession */
0N/A)
0N/A{
0N/A NotifyEncapsulation *notifyEncapsulation;
0N/A JavaVM *jvm;
0N/A JNIEnv *env;
0N/A jsize actualNumberVMs;
0N/A jint returnValue;
0N/A jlong jSessionHandle;
0N/A jlong jEvent;
0N/A jclass ckNotifyClass;
0N/A jmethodID jmethod;
0N/A jthrowable pkcs11Exception;
0N/A jclass pkcs11ExceptionClass;
0N/A jlong errorCode;
0N/A CK_RV rv = CKR_OK;
0N/A int wasAttached = 1;
0N/A
0N/A if (pApplication == NULL) { return rv ; } /* This should not occur in this wrapper. */
0N/A
0N/A notifyEncapsulation = (NotifyEncapsulation *) pApplication;
0N/A
0N/A /* Get the currently running Java VM */
0N/A returnValue = JNI_GetCreatedJavaVMs(&jvm, (jsize) 1, &actualNumberVMs);
0N/A if ((returnValue != 0) || (actualNumberVMs <= 0)) { return rv ; } /* there is no VM running */
0N/A
0N/A /* Determine, if current thread is already attached */
0N/A returnValue = (*jvm)->GetEnv(jvm, (void **) &env, JNI_VERSION_1_2);
0N/A if (returnValue == JNI_EDETACHED) {
0N/A /* thread detached, so attach it */
0N/A wasAttached = 0;
0N/A returnValue = (*jvm)->AttachCurrentThread(jvm, (void **) &env, NULL);
0N/A } else if (returnValue == JNI_EVERSION) {
0N/A /* this version of JNI is not supported, so just try to attach */
0N/A /* we assume it was attached to ensure that this thread is not detached
0N/A * afterwards even though it should not
0N/A */
0N/A wasAttached = 1;
0N/A returnValue = (*jvm)->AttachCurrentThread(jvm, (void **) &env, NULL);
0N/A } else {
0N/A /* attached */
0N/A wasAttached = 1;
0N/A }
0N/A
0N/A jSessionHandle = ckULongToJLong(hSession);
0N/A jEvent = ckULongToJLong(event);
0N/A
0N/A ckNotifyClass = (*env)->FindClass(env, CLASS_NOTIFY);
936N/A if (ckNotifyClass == NULL) { return rv; }
0N/A jmethod = (*env)->GetMethodID(env, ckNotifyClass, "CK_NOTIFY", "(JJLjava/lang/Object;)V");
936N/A if (jmethod == NULL) { return rv; }
936N/A
0N/A (*env)->CallVoidMethod(env, notifyEncapsulation->jNotifyObject, jmethod,
0N/A jSessionHandle, jEvent, notifyEncapsulation->jApplicationData);
0N/A
0N/A /* check, if callback threw an exception */
0N/A pkcs11Exception = (*env)->ExceptionOccurred(env);
0N/A
0N/A if (pkcs11Exception != NULL) {
936N/A /* TBD: clear the pending exception with ExceptionClear? */
0N/A /* The was an exception thrown, now we get the error-code from it */
0N/A pkcs11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION);
936N/A if (pkcs11ExceptionClass == NULL) { return rv; }
936N/A
0N/A jmethod = (*env)->GetMethodID(env, pkcs11ExceptionClass, "getErrorCode", "()J");
936N/A if (jmethod == NULL) { return rv; }
936N/A
0N/A errorCode = (*env)->CallLongMethod(env, pkcs11Exception, jmethod);
0N/A rv = jLongToCKULong(errorCode);
0N/A }
0N/A
0N/A /* if we attached this thread to the VM just for callback, we detach it now */
0N/A if (wasAttached) {
0N/A returnValue = (*jvm)->DetachCurrentThread(jvm);
0N/A }
0N/A
0N/A return rv ;
0N/A}
0N/A
0N/A#endif /* NO_CALLBACKS */