/*
*/
/* Copyright (c) 2002 Graz University of Technology. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
*
* 3. The end-user documentation included with the redistribution, if any, must
* include the following acknowledgment:
*
* "This product includes software developed by IAIK of Graz University of
* Technology."
*
* Alternately, this acknowledgment may appear in the software itself, if
* and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Graz University of Technology" and "IAIK of Graz University of
* Technology" must not be used to endorse or promote products derived from
* this software without prior written permission.
*
* 5. Products derived from this software may not be called
* "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior
* written permission of Graz University of Technology.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "pkcs11wrapper.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "sun_security_pkcs11_wrapper_PKCS11.h"
/* The initArgs that enable the application to do custom mutex-handling */
#ifndef NO_CALLBACKS
#endif /* NO_CALLBACKS */
/* ************************************************************************** */
/* Now come the functions for mutex handling and notification callbacks */
/* ************************************************************************** */
/*
* converts the InitArgs object to a CK_C_INITIALIZE_ARGS structure and sets the functions
* that will call the right Java mutex functions
*
* @param env - used to call JNI funktions to get the Java classes, objects, methods and fields
* @param pInitArgs - the InitArgs object with the Java mutex functions to call
* @return - the pointer to the CK_C_INITIALIZE_ARGS structure with the functions that will call
* the corresponding Java functions
*/
{
#ifndef NO_CALLBACKS
#endif /* NO_CALLBACKS */
return NULL_PTR;
}
/* convert the Java InitArgs object to a pointer to a CK_C_INITIALIZE_ARGS structure */
if (ckpInitArgs == NULL) {
return NULL_PTR;
}
/* Set the mutex functions that will call the Java mutex functions, but
* only set it, if the field is not null.
*/
if (jInitArgsClass == NULL) {
return NULL;
}
#ifdef NO_CALLBACKS
#else
fieldID = (*env)->GetFieldID(env, jInitArgsClass, "CreateMutex", "Lsun/security/pkcs11/wrapper/CK_CREATEMUTEX;");
fieldID = (*env)->GetFieldID(env, jInitArgsClass, "DestroyMutex", "Lsun/security/pkcs11/wrapper/CK_DESTROYMUTEX;");
fieldID = (*env)->GetFieldID(env, jInitArgsClass, "LockMutex", "Lsun/security/pkcs11/wrapper/CK_LOCKMUTEX;");
fieldID = (*env)->GetFieldID(env, jInitArgsClass, "UnlockMutex", "Lsun/security/pkcs11/wrapper/CK_UNLOCKMUTEX;");
/* we only need to keep a global copy, if we need callbacks */
/* set the global object jInitArgs so that the right Java mutex functions will be called */
if (ckpGlobalInitArgs == NULL) {
return NULL_PTR;
}
}
#endif /* NO_CALLBACKS */
/* convert and set the flags field */
/* pReserved should be NULL_PTR in this version */
/* we try to convert the reserved parameter also */
return ckpInitArgs ;
}
#ifndef NO_CALLBACKS
/*
* is the function that gets called by PKCS#11 to create a mutex and calls the Java
* CreateMutex function
*
* @param env - used to call JNI funktions to get the Java classes, objects, methods and fields
* @param ppMutex - the new created mutex
* @return - should return CKR_OK if the mutex creation was ok
*/
{
/* Get the currently running Java VM */
/* Determine, if current thread is already attached */
if (returnValue == JNI_EDETACHED) {
/* thread detached, so attach it */
wasAttached = 0;
} else if (returnValue == JNI_EVERSION) {
/* this version of JNI is not supported, so just try to attach */
/* we assume it was attached to ensure that this thread is not detached
* afterwards even though it should not
*/
wasAttached = 1;
} else {
/* attached */
wasAttached = 1;
}
/* get the CreateMutex object out of the jInitArgs object */
fieldID = (*env)->GetFieldID(env, jInitArgsClass, "CreateMutex", "Lsun/security/pkcs11/wrapper/CK_CREATEMUTEX;");
assert(jCreateMutex != 0);
/* call the CK_CREATEMUTEX function of the CreateMutex object */
/* and get the new Java mutex object */
/* set a global reference on the Java mutex */
/* convert the Java mutex to a CK mutex */
/* check, if callback threw an exception */
if (pkcs11Exception != NULL) {
/* TBD: clear the pending exception with ExceptionClear? */
/* The was an exception thrown, now we get the error-code from it */
}
/* if we attached this thread to the VM just for callback, we detach it now */
if (wasAttached) {
}
return rv ;
}
/*
* is the function that gets called by PKCS#11 to destroy a mutex and calls the Java
* DestroyMutex function
*
* @param env - used to call JNI funktions to get the Java classes, objects, methods and fields
* @param pMutex - the mutex to destroy
* @return - should return CKR_OK if the mutex was destroyed
*/
{
/* Get the currently running Java VM */
/* Determine, if current thread is already attached */
if (returnValue == JNI_EDETACHED) {
/* thread detached, so attach it */
wasAttached = 0;
} else if (returnValue == JNI_EVERSION) {
/* this version of JNI is not supported, so just try to attach */
/* we assume it was attached to ensure that this thread is not detached
* afterwards even though it should not
*/
wasAttached = 1;
} else {
/* attached */
wasAttached = 1;
}
/* convert the CK mutex to a Java mutex */
/* get the DestroyMutex object out of the jInitArgs object */
fieldID = (*env)->GetFieldID(env, jInitArgsClass, "DestroyMutex", "Lsun/security/pkcs11/wrapper/CK_DESTROYMUTEX;");
assert(jDestroyMutex != 0);
/* call the CK_DESTROYMUTEX method of the DestroyMutex object */
methodID = (*env)->GetMethodID(env, jDestroyMutexClass, "CK_DESTROYMUTEX", "(Ljava/lang/Object;)V");
/* delete the global reference on the Java mutex */
/* check, if callback threw an exception */
if (pkcs11Exception != NULL) {
/* TBD: clear the pending exception with ExceptionClear? */
/* The was an exception thrown, now we get the error-code from it */
}
/* if we attached this thread to the VM just for callback, we detach it now */
if (wasAttached) {
}
return rv ;
}
/*
* is the function that gets called by PKCS#11 to lock a mutex and calls the Java
* LockMutex function
*
* @param env - used to call JNI funktions to get the Java classes, objects, methods and fields
* @param pMutex - the mutex to lock
* @return - should return CKR_OK if the mutex was not locked already
*/
{
/* Get the currently running Java VM */
/* Determine, if current thread is already attached */
if (returnValue == JNI_EDETACHED) {
/* thread detached, so attach it */
wasAttached = 0;
} else if (returnValue == JNI_EVERSION) {
/* this version of JNI is not supported, so just try to attach */
/* we assume it was attached to ensure that this thread is not detached
* afterwards even though it should not
*/
wasAttached = 1;
} else {
/* attached */
wasAttached = 1;
}
/* convert the CK mutex to a Java mutex */
/* get the LockMutex object out of the jInitArgs object */
fieldID = (*env)->GetFieldID(env, jInitArgsClass, "LockMutex", "Lsun/security/pkcs11/wrapper/CK_LOCKMUTEX;");
assert(jLockMutex != 0);
/* call the CK_LOCKMUTEX method of the LockMutex object */
/* check, if callback threw an exception */
if (pkcs11Exception != NULL) {
/* TBD: clear the pending exception with ExceptionClear? */
/* The was an exception thrown, now we get the error-code from it */
}
/* if we attached this thread to the VM just for callback, we detach it now */
if (wasAttached) {
}
return rv ;
}
/*
* is the function that gets called by PKCS#11 to unlock a mutex and calls the Java
* UnlockMutex function
*
* @param env - used to call JNI funktions to get the Java classes, objects, methods and fields
* @param pMutex - the mutex to unlock
* @return - should return CKR_OK if the mutex was not unlocked already
*/
{
/* Get the currently running Java VM */
/* Determine, if current thread is already attached */
if (returnValue == JNI_EDETACHED) {
/* thread detached, so attach it */
wasAttached = 0;
} else if (returnValue == JNI_EVERSION) {
/* this version of JNI is not supported, so just try to attach */
/* we assume it was attached to ensure that this thread is not detached
* afterwards even though it should not
*/
wasAttached = 1;
} else {
/* attached */
wasAttached = 1;
}
/* convert the CK-type mutex to a Java mutex */
/* get the UnlockMutex object out of the jInitArgs object */
fieldID = (*env)->GetFieldID(env, jInitArgsClass, "UnlockMutex", "Lsun/security/pkcs11/wrapper/CK_UNLOCKMUTEX;");
assert(jUnlockMutex != 0);
/* call the CK_UNLOCKMUTEX method of the UnLockMutex object */
/* check, if callback threw an exception */
if (pkcs11Exception != NULL) {
/* TBD: clear the pending exception with ExceptionClear? */
/* The was an exception thrown, now we get the error-code from it */
}
/* if we attached this thread to the VM just for callback, we detach it now */
if (wasAttached) {
}
return rv ;
}
#endif /* NO_CALLBACKS */