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#ifdef P11_ENABLE_C_CREATEOBJECT
0N/A/*
0N/A * Class: sun_security_pkcs11_wrapper_PKCS11
0N/A * Method: C_CreateObject
0N/A * Signature: (J[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)J
0N/A * Parametermapping: *PKCS11*
0N/A * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
0N/A * @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate
0N/A * CK_ULONG ulCount
0N/A * @return jlong jObjectHandle CK_OBJECT_HANDLE_PTR phObject
0N/A */
0N/AJNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CreateObject
0N/A (JNIEnv *env, jobject obj, jlong jSessionHandle, jobjectArray jTemplate)
0N/A{
0N/A CK_SESSION_HANDLE ckSessionHandle;
0N/A CK_OBJECT_HANDLE ckObjectHandle;
0N/A CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
0N/A CK_ULONG ckAttributesLength;
1428N/A jlong jObjectHandle = 0L;
0N/A CK_RV rv;
0N/A
0N/A CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
0N/A if (ckpFunctions == NULL) { return 0L; }
0N/A
0N/A ckSessionHandle = jLongToCKULong(jSessionHandle);
0N/A jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
936N/A if ((*env)->ExceptionCheck(env)) { return 0L; }
0N/A
0N/A rv = (*ckpFunctions->C_CreateObject)(ckSessionHandle, ckpAttributes, ckAttributesLength, &ckObjectHandle);
0N/A
0N/A jObjectHandle = ckULongToJLong(ckObjectHandle);
936N/A freeCKAttributeArray(ckpAttributes, ckAttributesLength);
0N/A
936N/A if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; }
0N/A
0N/A return jObjectHandle ;
0N/A}
0N/A#endif
0N/A
0N/A#ifdef P11_ENABLE_C_COPYOBJECT
0N/A/*
0N/A * Class: sun_security_pkcs11_wrapper_PKCS11
0N/A * Method: C_CopyObject
0N/A * Signature: (JJ[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)J
0N/A * Parametermapping: *PKCS11*
0N/A * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
0N/A * @param jlong jObjectHandle CK_OBJECT_HANDLE hObject
0N/A * @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate
0N/A * CK_ULONG ulCount
0N/A * @return jlong jNewObjectHandle CK_OBJECT_HANDLE_PTR phNewObject
0N/A */
0N/AJNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CopyObject
0N/A (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jObjectHandle, jobjectArray jTemplate)
0N/A{
0N/A CK_SESSION_HANDLE ckSessionHandle;
0N/A CK_OBJECT_HANDLE ckObjectHandle;
0N/A CK_OBJECT_HANDLE ckNewObjectHandle;
0N/A CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
0N/A CK_ULONG ckAttributesLength;
1428N/A jlong jNewObjectHandle = 0L;
0N/A CK_RV rv;
0N/A
0N/A CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
0N/A if (ckpFunctions == NULL) { return 0L; }
0N/A
0N/A ckSessionHandle = jLongToCKULong(jSessionHandle);
0N/A ckObjectHandle = jLongToCKULong(jObjectHandle);
0N/A jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
936N/A if ((*env)->ExceptionCheck(env)) { return 0L; }
0N/A
0N/A rv = (*ckpFunctions->C_CopyObject)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength, &ckNewObjectHandle);
0N/A
0N/A jNewObjectHandle = ckULongToJLong(ckNewObjectHandle);
936N/A freeCKAttributeArray(ckpAttributes, ckAttributesLength);
0N/A
0N/A if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; }
0N/A
0N/A return jNewObjectHandle ;
0N/A}
0N/A#endif
0N/A
0N/A#ifdef P11_ENABLE_C_DESTROYOBJECT
0N/A/*
0N/A * Class: sun_security_pkcs11_wrapper_PKCS11
0N/A * Method: C_DestroyObject
0N/A * Signature: (JJ)V
0N/A * Parametermapping: *PKCS11*
0N/A * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
0N/A * @param jlong jObjectHandle CK_OBJECT_HANDLE hObject
0N/A */
0N/AJNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DestroyObject
0N/A (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jObjectHandle)
0N/A{
0N/A CK_SESSION_HANDLE ckSessionHandle;
0N/A CK_OBJECT_HANDLE ckObjectHandle;
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 ckObjectHandle = jLongToCKULong(jObjectHandle);
0N/A
0N/A rv = (*ckpFunctions->C_DestroyObject)(ckSessionHandle, ckObjectHandle);
936N/A if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
0N/A}
0N/A#endif
0N/A
0N/A#ifdef P11_ENABLE_C_GETOBJECTSIZE
0N/A/*
0N/A * Class: sun_security_pkcs11_wrapper_PKCS11
0N/A * Method: C_GetObjectSize
0N/A * Signature: (JJ)J
0N/A * Parametermapping: *PKCS11*
0N/A * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
0N/A * @param jlong jObjectHandle CK_OBJECT_HANDLE hObject
0N/A * @return jlong jObjectSize CK_ULONG_PTR pulSize
0N/A */
0N/AJNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetObjectSize
0N/A (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jObjectHandle)
0N/A{
0N/A CK_SESSION_HANDLE ckSessionHandle;
0N/A CK_OBJECT_HANDLE ckObjectHandle;
0N/A CK_ULONG ckObjectSize;
1428N/A jlong jObjectSize = 0L;
0N/A CK_RV rv;
0N/A
0N/A CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
0N/A if (ckpFunctions == NULL) { return 0L; }
0N/A
0N/A ckSessionHandle = jLongToCKULong(jSessionHandle);
0N/A ckObjectHandle = jLongToCKULong(jObjectHandle);
0N/A
0N/A rv = (*ckpFunctions->C_GetObjectSize)(ckSessionHandle, ckObjectHandle, &ckObjectSize);
936N/A if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; }
0N/A
0N/A jObjectSize = ckULongToJLong(ckObjectSize);
0N/A
0N/A return jObjectSize ;
0N/A}
0N/A#endif
0N/A
0N/A#ifdef P11_ENABLE_C_GETATTRIBUTEVALUE
0N/A/*
0N/A * Class: sun_security_pkcs11_wrapper_PKCS11
0N/A * Method: C_GetAttributeValue
0N/A * Signature: (JJ[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;
0N/A * Parametermapping: *PKCS11*
0N/A * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
0N/A * @param jlong jObjectHandle CK_OBJECT_HANDLE hObject
0N/A * @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate
0N/A * CK_ULONG ulCount
0N/A */
0N/AJNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetAttributeValue
0N/A (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jObjectHandle, jobjectArray jTemplate)
0N/A{
0N/A CK_SESSION_HANDLE ckSessionHandle;
0N/A CK_OBJECT_HANDLE ckObjectHandle;
0N/A CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
0N/A CK_ULONG ckAttributesLength;
0N/A CK_ULONG ckBufferLength;
1428N/A CK_ULONG i;
0N/A jobject jAttribute;
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 TRACE0("DEBUG: C_GetAttributeValue");
0N/A TRACE1(", hSession=%u", jSessionHandle);
0N/A TRACE1(", hObject=%u", jObjectHandle);
0N/A TRACE1(", pTemplate=%p", jTemplate);
0N/A TRACE0(" ... ");
0N/A
0N/A ckSessionHandle = jLongToCKULong(jSessionHandle);
0N/A ckObjectHandle = jLongToCKULong(jObjectHandle);
0N/A TRACE1("jAttributeArrayToCKAttributeArray now with jTemplate = %d", jTemplate);
0N/A jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
936N/A if ((*env)->ExceptionCheck(env)) { return; }
936N/A
0N/A TRACE2("DEBUG: jAttributeArrayToCKAttributeArray finished with ckpAttribute = %d, Length = %d\n", ckpAttributes, ckAttributesLength);
0N/A
0N/A /* first set all pValue to NULL, to get the needed buffer length */
0N/A for(i = 0; i < ckAttributesLength; i++) {
936N/A if (ckpAttributes[i].pValue != NULL_PTR) {
0N/A free(ckpAttributes[i].pValue);
936N/A ckpAttributes[i].pValue = NULL_PTR;
0N/A }
0N/A }
936N/A
0N/A rv = (*ckpFunctions->C_GetAttributeValue)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength);
936N/A if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {
0N/A free(ckpAttributes);
0N/A return ;
0N/A }
0N/A
0N/A /* now, the ulValueLength field of each attribute should hold the exact buffer length needed
0N/A * allocate the needed buffers accordingly
0N/A */
0N/A for (i = 0; i < ckAttributesLength; i++) {
0N/A ckBufferLength = sizeof(CK_BYTE) * ckpAttributes[i].ulValueLen;
0N/A ckpAttributes[i].pValue = (void *) malloc(ckBufferLength);
936N/A if (ckpAttributes[i].pValue == NULL) {
936N/A freeCKAttributeArray(ckpAttributes, i);
936N/A JNU_ThrowOutOfMemoryError(env, 0);
936N/A return;
936N/A }
0N/A ckpAttributes[i].ulValueLen = ckBufferLength;
0N/A }
0N/A
0N/A /* now get the attributes with all values */
0N/A rv = (*ckpFunctions->C_GetAttributeValue)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength);
0N/A
936N/A if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
936N/A /* copy back the values to the Java attributes */
936N/A for (i = 0; i < ckAttributesLength; i++) {
936N/A jAttribute = ckAttributePtrToJAttribute(env, &(ckpAttributes[i]));
936N/A if (jAttribute == NULL) {
936N/A freeCKAttributeArray(ckpAttributes, ckAttributesLength);
936N/A return;
936N/A }
936N/A (*env)->SetObjectArrayElement(env, jTemplate, i, jAttribute);
936N/A if ((*env)->ExceptionCheck(env)) {
936N/A freeCKAttributeArray(ckpAttributes, ckAttributesLength);
936N/A return;
936N/A }
0N/A }
0N/A }
936N/A freeCKAttributeArray(ckpAttributes, ckAttributesLength);
0N/A TRACE0("FINISHED\n");
0N/A}
0N/A#endif
0N/A
0N/A#ifdef P11_ENABLE_C_SETATTRIBUTEVALUE
0N/A/*
0N/A * Class: sun_security_pkcs11_wrapper_PKCS11
0N/A * Method: C_SetAttributeValue
0N/A * Signature: (JJ[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)V
0N/A * Parametermapping: *PKCS11*
0N/A * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
0N/A * @param jlong jObjectHandle CK_OBJECT_HANDLE hObject
0N/A * @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate
0N/A * CK_ULONG ulCount
0N/A */
0N/AJNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SetAttributeValue
0N/A (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jObjectHandle, jobjectArray jTemplate)
0N/A{
0N/A CK_SESSION_HANDLE ckSessionHandle;
0N/A CK_OBJECT_HANDLE ckObjectHandle;
0N/A CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
0N/A CK_ULONG ckAttributesLength;
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 ckObjectHandle = jLongToCKULong(jObjectHandle);
0N/A jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
936N/A if ((*env)->ExceptionCheck(env)) { return; }
0N/A
0N/A rv = (*ckpFunctions->C_SetAttributeValue)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength);
0N/A
936N/A freeCKAttributeArray(ckpAttributes, ckAttributesLength);
0N/A
0N/A if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
0N/A}
0N/A#endif
0N/A
0N/A#ifdef P11_ENABLE_C_FINDOBJECTSINIT
0N/A/*
0N/A * Class: sun_security_pkcs11_wrapper_PKCS11
0N/A * Method: C_FindObjectsInit
0N/A * Signature: (J[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)V
0N/A * Parametermapping: *PKCS11*
0N/A * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
0N/A * @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate
0N/A * CK_ULONG ulCount
0N/A */
0N/AJNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1FindObjectsInit
0N/A (JNIEnv *env, jobject obj, jlong jSessionHandle, jobjectArray jTemplate)
0N/A{
0N/A CK_SESSION_HANDLE ckSessionHandle;
0N/A CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;
0N/A CK_ULONG ckAttributesLength;
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 TRACE0("DEBUG: C_FindObjectsInit");
0N/A TRACE1(", hSession=%u", jSessionHandle);
0N/A TRACE1(", pTemplate=%p", jTemplate);
0N/A TRACE0(" ... ");
0N/A
0N/A ckSessionHandle = jLongToCKULong(jSessionHandle);
0N/A jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);
936N/A if ((*env)->ExceptionCheck(env)) { return; }
0N/A
0N/A rv = (*ckpFunctions->C_FindObjectsInit)(ckSessionHandle, ckpAttributes, ckAttributesLength);
0N/A
936N/A freeCKAttributeArray(ckpAttributes, ckAttributesLength);
0N/A TRACE0("FINISHED\n");
0N/A
0N/A if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
0N/A}
0N/A#endif
0N/A
0N/A#ifdef P11_ENABLE_C_FINDOBJECTS
0N/A/*
0N/A * Class: sun_security_pkcs11_wrapper_PKCS11
0N/A * Method: C_FindObjects
0N/A * Signature: (JJ)[J
0N/A * Parametermapping: *PKCS11*
0N/A * @param jlong jSessionHandle CK_SESSION_HANDLE hSession
0N/A * @param jlong jMaxObjectCount CK_ULONG ulMaxObjectCount
0N/A * @return jlongArray jObjectHandleArray CK_OBJECT_HANDLE_PTR phObject
0N/A * CK_ULONG_PTR pulObjectCount
0N/A */
0N/AJNIEXPORT jlongArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1FindObjects
0N/A (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jMaxObjectCount)
0N/A{
0N/A CK_RV rv;
0N/A CK_SESSION_HANDLE ckSessionHandle;
0N/A CK_ULONG ckMaxObjectLength;
0N/A CK_OBJECT_HANDLE_PTR ckpObjectHandleArray;
0N/A CK_ULONG ckActualObjectCount;
1428N/A jlongArray jObjectHandleArray = NULL;
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 ckMaxObjectLength = jLongToCKULong(jMaxObjectCount);
0N/A ckpObjectHandleArray = (CK_OBJECT_HANDLE_PTR) malloc(sizeof(CK_OBJECT_HANDLE) * ckMaxObjectLength);
936N/A if (ckpObjectHandleArray == NULL) {
936N/A JNU_ThrowOutOfMemoryError(env, 0);
936N/A return NULL;
936N/A }
0N/A
0N/A rv = (*ckpFunctions->C_FindObjects)(ckSessionHandle, ckpObjectHandleArray, ckMaxObjectLength, &ckActualObjectCount);
936N/A if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
936N/A jObjectHandleArray = ckULongArrayToJLongArray(env, ckpObjectHandleArray, ckActualObjectCount);
936N/A }
0N/A
0N/A free(ckpObjectHandleArray);
0N/A
0N/A return jObjectHandleArray ;
0N/A}
0N/A#endif
0N/A
0N/A#ifdef P11_ENABLE_C_FINDOBJECTSFINAL
0N/A/*
0N/A * Class: sun_security_pkcs11_wrapper_PKCS11
0N/A * Method: C_FindObjectsFinal
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_1FindObjectsFinal
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 rv = (*ckpFunctions->C_FindObjectsFinal)(ckSessionHandle);
0N/A if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
0N/A}
0N/A#endif