/*
*/
/* 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>
/* declare file private functions */
/* ************************************************************************** */
/* Functions for keeping track of currently active and loaded modules */
/* ************************************************************************** */
/*
* Create a new object for locking.
*/
return jLockObject ;
}
/*
* Create a new object for locking.
*/
if (jLockObject != NULL) {
}
}
/*
* Add the given pkcs11Implementation object to the list of present modules.
* Attach the given data to the entry. If the given pkcs11Implementation is
* already in the lsit, just override its old module data with the new one.
* None of the arguments can be NULL. If one of the arguments is NULL, this
* function does nothing.
*/
if (pkcs11Implementation == NULL_PTR) {
return ;
}
if (moduleData == NULL) {
return ;
}
}
/*
* Get the module data of the entry for the given pkcs11Implementation. Returns
* NULL, if the pkcs11Implementation is not in the list.
*/
if (pkcs11Implementation == NULL) {
return NULL;
}
return (ModuleData*)jData;
}
if (moduleData == NULL) {
return NULL;
}
return ckpFunctions;
}
/*
* Returns 1, if the given pkcs11Implementation is in the list.
* 0, otherwise.
*/
int present;
return present ;
}
/*
* Removes the entry for the given pkcs11Implementation from the list. Returns
* the module's data, after the node was removed. If this function returns NULL
* the pkcs11Implementation was not in the list.
*/
if (moduleData == NULL) {
return NULL;
}
return moduleData;
}
/*
* Removes all present entries from the list of modules and frees all
* associated resources. This function is used for clean-up.
*/
/* XXX empty */
}
/* ************************************************************************** */
/* Below there follow the helper functions to support conversions between */
/* Java and Cryptoki types */
/* ************************************************************************** */
/*
* function to convert a PKCS#11 return value into a PKCS#11Exception
*
* This function generates a PKCS#11Exception with the returnValue as the errorcode
* if the returnValue is not CKR_OK. The functin returns 0, if the returnValue is
* CKR_OK. Otherwise, it returns the returnValue as a jLong.
*
* @param env - used to call JNI funktions and to get the Exception class
* @param returnValue - of the PKCS#11 function
*/
{
if (returnValue != CKR_OK) {
if (jPKCS11ExceptionClass != NULL) {
if (jConstructor != NULL) {
jPKCS11Exception = (jthrowable) (*env)->NewObject(env, jPKCS11ExceptionClass, jConstructor, jErrorCode);
if (jPKCS11Exception != NULL) {
}
}
}
}
return jErrorCode ;
}
/*
* This function simply throws an IOException
*
* @param env Used to call JNI funktions and to get the Exception class.
* @param message The message string of the Exception object.
*/
{
}
/*
* This function simply throws a PKCS#11RuntimeException with the given
* string as its message. If the message is NULL, the exception is created
* using the default constructor.
*
* @param env Used to call JNI funktions and to get the Exception class.
* @param jmessage The message string of the Exception object.
*/
{
}
/*
* This function simply throws a PKCS#11RuntimeException. The message says that
* the object is not connected to the module.
*
* @param env Used to call JNI funktions and to get the Exception class.
*/
{
}
/* This function frees the specified CK_ATTRIBUTE array.
*
* @param attrPtr pointer to the to-be-freed CK_ATTRIBUTE array.
* @param len the length of the array
*/
{
int i;
for (i=0; i<len; i++) {
}
}
}
/*
* the following functions convert Java arrays to PKCS#11 array pointers and
* their array length and vice versa
*
* void j<Type>ArrayToCK<Type>Array(JNIEnv *env,
* const j<Type>Array jArray,
* CK_<Type>_PTR *ckpArray,
* CK_ULONG_PTR ckLength);
*
* j<Type>Array ck<Type>ArrayToJ<Type>Array(JNIEnv *env,
* const CK_<Type>_PTR ckpArray,
* CK_ULONG ckLength);
*
* PKCS#11 arrays consist always of a pointer to the beginning of the array and
* the array length whereas Java arrays carry their array length.
*
* The Functions to convert a Java array to a PKCS#11 array are void functions.
* Their arguments are the Java array object to convert, the reference to the
* array pointer, where the new PKCS#11 array should be stored and the reference
* to the array length where the PKCS#11 array length should be stored. These two
* references must not be NULL_PTR.
*
* The functions first obtain the array length of the Java array and then allocate
* the memory for the PKCS#11 array and set the array length. Then each element
* gets converted depending on their type. After use the allocated memory of the
* PKCS#11 array has to be explicitly freed.
*
* The Functions to convert a PKCS#11 array to a Java array get the PKCS#11 array
* pointer and the array length and they return the new Java array object. The
* Java array does not need to get freed after use.
*/
/*
* converts a jbooleanArray to a CK_BBOOL array. The allocated memory has to be freed after use!
*
* @param env - used to call JNI funktions to get the array informtaion
* @param jArray - the Java array to convert
* @param ckpArray - the reference, where the pointer to the new CK_BBOOL array will be stored
* @param ckpLength - the reference, where the array length will be stored
*/
void jBooleanArrayToCKBBoolArray(JNIEnv *env, const jbooleanArray jArray, CK_BBOOL **ckpArray, CK_ULONG_PTR ckpLength)
{
CK_ULONG i;
*ckpLength = 0L;
return;
}
return;
}
return;
}
return;
}
for (i=0; i<(*ckpLength); i++) {
}
}
/*
* converts a jbyteArray to a CK_BYTE array. The allocated memory has to be freed after use!
*
* @param env - used to call JNI funktions to get the array informtaion
* @param jArray - the Java array to convert
* @param ckpArray - the reference, where the pointer to the new CK_BYTE array will be stored
* @param ckpLength - the reference, where the array length will be stored
*/
void jByteArrayToCKByteArray(JNIEnv *env, const jbyteArray jArray, CK_BYTE_PTR *ckpArray, CK_ULONG_PTR ckpLength)
{
CK_ULONG i;
*ckpLength = 0L;
return;
}
return;
}
return;
}
/* if CK_BYTE is the same size as jbyte, we save an additional copy */
} else {
return;
}
for (i=0; i<(*ckpLength); i++) {
}
}
}
/*
* converts a jlongArray to a CK_ULONG array. The allocated memory has to be freed after use!
*
* @param env - used to call JNI funktions to get the array informtaion
* @param jArray - the Java array to convert
* @param ckpArray - the reference, where the pointer to the new CK_ULONG array will be stored
* @param ckpLength - the reference, where the array length will be stored
*/
void jLongArrayToCKULongArray(JNIEnv *env, const jlongArray jArray, CK_ULONG_PTR *ckpArray, CK_ULONG_PTR ckpLength)
{
CK_ULONG i;
*ckpLength = 0L;
return;
}
return;
}
return;
}
return;
}
for (i=0; i<(*ckpLength); i++) {
}
}
/*
* converts a jcharArray to a CK_CHAR array. The allocated memory has to be freed after use!
*
* @param env - used to call JNI funktions to get the array informtaion
* @param jArray - the Java array to convert
* @param ckpArray - the reference, where the pointer to the new CK_CHAR array will be stored
* @param ckpLength - the reference, where the array length will be stored
*/
void jCharArrayToCKCharArray(JNIEnv *env, const jcharArray jArray, CK_CHAR_PTR *ckpArray, CK_ULONG_PTR ckpLength)
{
CK_ULONG i;
*ckpLength = 0L;
return;
}
return;
}
return;
}
return;
}
for (i=0; i<(*ckpLength); i++) {
}
}
/*
* converts a jcharArray to a CK_UTF8CHAR array. The allocated memory has to be freed after use!
*
* @param env - used to call JNI funktions to get the array informtaion
* @param jArray - the Java array to convert
* @param ckpArray - the reference, where the pointer to the new CK_UTF8CHAR array will be stored
* @param ckpLength - the reference, where the array length will be stored
*/
void jCharArrayToCKUTF8CharArray(JNIEnv *env, const jcharArray jArray, CK_UTF8CHAR_PTR *ckpArray, CK_ULONG_PTR ckpLength)
{
CK_ULONG i;
*ckpLength = 0L;
return;
}
return;
}
return;
}
return;
}
for (i=0; i<(*ckpLength); i++) {
}
}
/*
* converts a jstring to a CK_CHAR array. The allocated memory has to be freed after use!
*
* @param env - used to call JNI funktions to get the array informtaion
* @param jArray - the Java array to convert
* @param ckpArray - the reference, where the pointer to the new CK_CHAR array will be stored
* @param ckpLength - the reference, where the array length will be stored
*/
void jStringToCKUTF8CharArray(JNIEnv *env, const jstring jArray, CK_UTF8CHAR_PTR *ckpArray, CK_ULONG_PTR ckpLength)
{
const char* pCharArray;
*ckpLength = 0L;
return;
}
if (pCharArray == NULL) { return; }
return;
}
}
/*
* converts a jobjectArray with Java Attributes to a CK_ATTRIBUTE array. The allocated memory
* has to be freed after use!
*
* @param env - used to call JNI funktions to get the array informtaion
* @param jArray - the Java Attribute array (template) to convert
* @param ckpArray - the reference, where the pointer to the new CK_ATTRIBUTE array will be
* stored
* @param ckpLength - the reference, where the array length will be stored
*/
void jAttributeArrayToCKAttributeArray(JNIEnv *env, jobjectArray jArray, CK_ATTRIBUTE_PTR *ckpArray, CK_ULONG_PTR ckpLength)
{
CK_ULONG i;
TRACE0("\nDEBUG: jAttributeArrayToCKAttributeArray");
*ckpLength = 0L;
return;
}
return;
}
for (i=0; i<(*ckpLength); i++) {
TRACE1(", getting %d. attibute", i);
freeCKAttributeArray(*ckpArray, i);
return;
}
TRACE1(", converting %d. attibute", i);
freeCKAttributeArray(*ckpArray, i);
return;
}
}
TRACE0("FINISHED\n");
}
/*
* converts a CK_BYTE array and its length to a jbyteArray.
*
* @param env - used to call JNI funktions to create the new Java array
* @param ckpArray - the pointer to the CK_BYTE array to convert
* @param ckpLength - the length of the array to convert
* @return - the new Java byte array or NULL if error occurred
*/
{
CK_ULONG i;
/* if CK_BYTE is the same size as jbyte, we save an additional copy */
} else {
return NULL;
}
for (i=0; i<ckLength; i++) {
}
}
}
return jArray ;
}
/*
* converts a CK_ULONG array and its length to a jlongArray.
*
* @param env - used to call JNI funktions to create the new Java array
* @param ckpArray - the pointer to the CK_ULONG array to convert
* @param ckpLength - the length of the array to convert
* @return - the new Java long array
*/
{
CK_ULONG i;
return NULL;
}
for (i=0; i<ckLength; i++) {
}
}
return jArray ;
}
/*
* converts a CK_CHAR array and its length to a jcharArray.
*
* @param env - used to call JNI funktions to create the new Java array
* @param ckpArray - the pointer to the CK_CHAR array to convert
* @param ckpLength - the length of the array to convert
* @return - the new Java char array
*/
{
CK_ULONG i;
return NULL;
}
for (i=0; i<ckLength; i++) {
}
}
return jArray ;
}
/*
* converts a CK_UTF8CHAR array and its length to a jcharArray.
*
* @param env - used to call JNI funktions to create the new Java array
* @param ckpArray - the pointer to the CK_UTF8CHAR array to convert
* @param ckpLength - the length of the array to convert
* @return - the new Java char array
*/
jcharArray ckUTF8CharArrayToJCharArray(JNIEnv *env, const CK_UTF8CHAR_PTR ckpArray, CK_ULONG ckLength)
{
CK_ULONG i;
return NULL;
}
for (i=0; i<ckLength; i++) {
}
}
return jArray ;
}
/*
* the following functions convert Java objects to PKCS#11 pointers and the
* length in bytes and vice versa
*
* CK_<Type>_PTR j<Object>ToCK<Type>Ptr(JNIEnv *env, jobject jObject);
*
* jobject ck<Type>PtrToJ<Object>(JNIEnv *env, const CK_<Type>_PTR ckpValue);
*
* The functions that convert a Java object to a PKCS#11 pointer first allocate
* the memory for the PKCS#11 pointer. Then they set each element corresponding
* to the fields in the Java object to convert. After use the allocated memory of
* the PKCS#11 pointer has to be explicitly freed.
*
* The functions to convert a PKCS#11 pointer to a Java object create a new Java
* object first and than they set all fields in the object depending on the values
* of the type or structure where the PKCS#11 pointer points to.
*/
/*
* converts a CK_BBOOL pointer to a Java boolean Object.
*
* @param env - used to call JNI funktions to create the new Java object
* @param ckpValue - the pointer to the CK_BBOOL value
* @return - the new Java boolean object with the boolean value
*/
{
return jValueObject ;
}
/*
* converts a CK_ULONG pointer to a Java long Object.
*
* @param env - used to call JNI funktions to create the new Java object
* @param ckpValue - the pointer to the CK_ULONG value
* @return - the new Java long object with the long value
*/
{
return jValueObject ;
}
/*
* converts a Java boolean object into a pointer to a CK_BBOOL value. The memory has to be
* freed after use!
*
* @param env - used to call JNI funktions to get the value out of the Java object
* @return - the pointer to the new CK_BBOOL value
*/
{
return NULL;
}
return ckpValue ;
}
/*
* converts a Java byte object into a pointer to a CK_BYTE value. The memory has to be
* freed after use!
*
* @param env - used to call JNI funktions to get the value out of the Java object
* @return - the pointer to the new CK_BYTE value
*/
{
return NULL;
}
return ckpValue ;
}
/*
* converts a Java integer object into a pointer to a CK_ULONG value. The memory has to be
* freed after use!
*
* @param env - used to call JNI funktions to get the value out of the Java object
* @return - the pointer to the new CK_ULONG value
*/
{
return NULL;
}
return ckpValue ;
}
/*
* converts a Java long object into a pointer to a CK_ULONG value. The memory has to be
* freed after use!
*
* @param env - used to call JNI funktions to get the value out of the Java object
* @return - the pointer to the new CK_ULONG value
*/
{
return NULL;
}
return ckpValue ;
}
/*
* converts a Java char object into a pointer to a CK_CHAR value. The memory has to be
* freed after use!
*
* @param env - used to call JNI funktions to get the value out of the Java object
* @return - the pointer to the new CK_CHAR value
*/
{
return NULL;
}
return ckpValue ;
}
/*
* converts a Java object into a pointer to CK-type or a CK-structure with the length in Bytes.
* The memory of *ckpObjectPtr to be freed after use! This function is only used by
* jAttributeToCKAttribute by now.
*
* @param env - used to call JNI funktions to get the Java classes and objects
* @param jObject - the Java object to convert
* @param ckpObjectPtr - the reference of the new pointer to the new CK-value or CK-structure
* @param ckpLength - the reference of the length in bytes of the new CK-value or CK-structure
*/
void jObjectToPrimitiveCKObjectPtrPtr(JNIEnv *env, jobject jObject, CK_VOID_PTR *ckpObjectPtr, CK_ULONG *ckpLength)
{
TRACE0("\nDEBUG: jObjectToPrimitiveCKObjectPtrPtr");
*ckpObjectPtr = NULL;
*ckpLength = 0;
return;
}
if (jLongClass == NULL) { return; }
return;
}
if (jBooleanClass == NULL) { return; }
TRACE0(" <converted boolean value ");
return;
}
if (jByteArrayClass == NULL) { return; }
return;
}
if (jCharArrayClass == NULL) { return; }
return;
}
if (jByteClass == NULL) { return; }
return;
}
if (jDateClass == NULL) { return; }
TRACE3("<converted date value %.4s-%.2s-%.2s>", (*((CK_DATE *) *ckpObjectPtr)).year, (*((CK_DATE *) *ckpObjectPtr)).month, (*((CK_DATE *) *ckpObjectPtr)).day);
return;
}
if (jCharacterClass == NULL) { return; }
*ckpLength = sizeof(CK_UTF8CHAR);
return;
}
if (jIntegerClass == NULL) { return; }
return;
}
if (jBooleanArrayClass == NULL) { return; }
return;
}
if (jIntArrayClass == NULL) { return; }
return;
}
if (jLongArrayClass == NULL) { return; }
return;
}
if (jStringClass == NULL) { return; }
return;
}
/* type of jObject unknown, throw PKCS11RuntimeException */
if (jObjectClass == NULL) { return; }
assert(jClassObject != 0);
if (jClassClass == NULL) { return; }
assert(jClassNameString != 0);
classNameString = (char*)
if (classNameString == NULL) { return; }
exceptionMsgPrefix = "Java object of this class cannot be converted to native PKCS#11 type: ";
exceptionMsg = (char *)
if (exceptionMsg == NULL) {
return;
}
*ckpObjectPtr = NULL;
*ckpLength = 0;
TRACE0("FINISHED\n");
}
#ifdef P11_MEMORYDEBUG
void *p = malloc(c);
return p;
}
free(p);
}
#endif