Path: blob/master/src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_objmgmt.c
41149 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2*/34/* Copyright (c) 2002 Graz University of Technology. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions are met:8*9* 1. Redistributions of source code must retain the above copyright notice,10* this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright notice,13* this list of conditions and the following disclaimer in the documentation14* and/or other materials provided with the distribution.15*16* 3. The end-user documentation included with the redistribution, if any, must17* include the following acknowledgment:18*19* "This product includes software developed by IAIK of Graz University of20* Technology."21*22* Alternately, this acknowledgment may appear in the software itself, if23* and wherever such third-party acknowledgments normally appear.24*25* 4. The names "Graz University of Technology" and "IAIK of Graz University of26* Technology" must not be used to endorse or promote products derived from27* this software without prior written permission.28*29* 5. Products derived from this software may not be called30* "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior31* written permission of Graz University of Technology.32*33* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED34* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED35* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR36* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE37* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,38* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,39* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,40* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON41* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,42* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY43* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE44* POSSIBILITY OF SUCH DAMAGE.45*/4647#include "pkcs11wrapper.h"4849#include <stdio.h>50#include <stdlib.h>51#include <string.h>52#include <assert.h>5354#include "sun_security_pkcs11_wrapper_PKCS11.h"5556#ifdef P11_ENABLE_C_CREATEOBJECT57/*58* Class: sun_security_pkcs11_wrapper_PKCS1159* Method: C_CreateObject60* Signature: (J[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)J61* Parametermapping: *PKCS11*62* @param jlong jSessionHandle CK_SESSION_HANDLE hSession63* @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate64* CK_ULONG ulCount65* @return jlong jObjectHandle CK_OBJECT_HANDLE_PTR phObject66*/67JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CreateObject68(JNIEnv *env, jobject obj, jlong jSessionHandle, jobjectArray jTemplate)69{70CK_SESSION_HANDLE ckSessionHandle;71CK_OBJECT_HANDLE ckObjectHandle;72CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;73CK_ULONG ckAttributesLength;74jlong jObjectHandle = 0L;75CK_RV rv;7677CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);78if (ckpFunctions == NULL) { return 0L; }7980ckSessionHandle = jLongToCKULong(jSessionHandle);81jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);82if ((*env)->ExceptionCheck(env)) { return 0L; }8384rv = (*ckpFunctions->C_CreateObject)(ckSessionHandle, ckpAttributes, ckAttributesLength, &ckObjectHandle);8586jObjectHandle = ckULongToJLong(ckObjectHandle);87freeCKAttributeArray(ckpAttributes, ckAttributesLength);8889if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; }9091return jObjectHandle ;92}93#endif9495#ifdef P11_ENABLE_C_COPYOBJECT96/*97* Class: sun_security_pkcs11_wrapper_PKCS1198* Method: C_CopyObject99* Signature: (JJ[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)J100* Parametermapping: *PKCS11*101* @param jlong jSessionHandle CK_SESSION_HANDLE hSession102* @param jlong jObjectHandle CK_OBJECT_HANDLE hObject103* @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate104* CK_ULONG ulCount105* @return jlong jNewObjectHandle CK_OBJECT_HANDLE_PTR phNewObject106*/107JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1CopyObject108(JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jObjectHandle, jobjectArray jTemplate)109{110CK_SESSION_HANDLE ckSessionHandle;111CK_OBJECT_HANDLE ckObjectHandle;112CK_OBJECT_HANDLE ckNewObjectHandle;113CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;114CK_ULONG ckAttributesLength;115jlong jNewObjectHandle = 0L;116CK_RV rv;117118CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);119if (ckpFunctions == NULL) { return 0L; }120121ckSessionHandle = jLongToCKULong(jSessionHandle);122ckObjectHandle = jLongToCKULong(jObjectHandle);123jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);124if ((*env)->ExceptionCheck(env)) { return 0L; }125126rv = (*ckpFunctions->C_CopyObject)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength, &ckNewObjectHandle);127128jNewObjectHandle = ckULongToJLong(ckNewObjectHandle);129freeCKAttributeArray(ckpAttributes, ckAttributesLength);130131if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; }132133return jNewObjectHandle ;134}135#endif136137#ifdef P11_ENABLE_C_DESTROYOBJECT138/*139* Class: sun_security_pkcs11_wrapper_PKCS11140* Method: C_DestroyObject141* Signature: (JJ)V142* Parametermapping: *PKCS11*143* @param jlong jSessionHandle CK_SESSION_HANDLE hSession144* @param jlong jObjectHandle CK_OBJECT_HANDLE hObject145*/146JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DestroyObject147(JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jObjectHandle)148{149CK_SESSION_HANDLE ckSessionHandle;150CK_OBJECT_HANDLE ckObjectHandle;151CK_RV rv;152153CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);154if (ckpFunctions == NULL) { return; }155156ckSessionHandle = jLongToCKULong(jSessionHandle);157ckObjectHandle = jLongToCKULong(jObjectHandle);158159rv = (*ckpFunctions->C_DestroyObject)(ckSessionHandle, ckObjectHandle);160if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }161}162#endif163164#ifdef P11_ENABLE_C_GETOBJECTSIZE165/*166* Class: sun_security_pkcs11_wrapper_PKCS11167* Method: C_GetObjectSize168* Signature: (JJ)J169* Parametermapping: *PKCS11*170* @param jlong jSessionHandle CK_SESSION_HANDLE hSession171* @param jlong jObjectHandle CK_OBJECT_HANDLE hObject172* @return jlong jObjectSize CK_ULONG_PTR pulSize173*/174JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetObjectSize175(JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jObjectHandle)176{177CK_SESSION_HANDLE ckSessionHandle;178CK_OBJECT_HANDLE ckObjectHandle;179CK_ULONG ckObjectSize;180jlong jObjectSize = 0L;181CK_RV rv;182183CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);184if (ckpFunctions == NULL) { return 0L; }185186ckSessionHandle = jLongToCKULong(jSessionHandle);187ckObjectHandle = jLongToCKULong(jObjectHandle);188189rv = (*ckpFunctions->C_GetObjectSize)(ckSessionHandle, ckObjectHandle, &ckObjectSize);190if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0L ; }191192jObjectSize = ckULongToJLong(ckObjectSize);193194return jObjectSize ;195}196#endif197198#ifdef P11_ENABLE_C_GETATTRIBUTEVALUE199/*200* Class: sun_security_pkcs11_wrapper_PKCS11201* Method: C_GetAttributeValue202* Signature: (JJ[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;203* Parametermapping: *PKCS11*204* @param jlong jSessionHandle CK_SESSION_HANDLE hSession205* @param jlong jObjectHandle CK_OBJECT_HANDLE hObject206* @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate207* CK_ULONG ulCount208*/209JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetAttributeValue210(JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jObjectHandle, jobjectArray jTemplate)211{212CK_SESSION_HANDLE ckSessionHandle;213CK_OBJECT_HANDLE ckObjectHandle;214CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;215CK_ULONG ckAttributesLength;216CK_ULONG ckBufferLength;217CK_ULONG i;218jobject jAttribute;219CK_RV rv;220char* msg = NULL;221char* temp1, *temp2;222223CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);224if (ckpFunctions == NULL) { return; }225226TRACE0("DEBUG: C_GetAttributeValue");227TRACE1(", hSession=%lld", (long long) jSessionHandle);228TRACE1(", hObject=%lld", (long long) jObjectHandle);229TRACE1(", pTemplate=%p", jTemplate);230TRACE0(" ... ");231232ckSessionHandle = jLongToCKULong(jSessionHandle);233ckObjectHandle = jLongToCKULong(jObjectHandle);234TRACE1("jAttributeArrayToCKAttributeArray now with jTemplate = %p", jTemplate);235jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);236if ((*env)->ExceptionCheck(env)) { return; }237238TRACE2("DEBUG: jAttributeArrayToCKAttributeArray finished with ckpAttribute = %p, Length = %lu\n", ckpAttributes, (unsigned long) ckAttributesLength);239240/* first set all pValue to NULL, to get the needed buffer length */241for(i = 0; i < ckAttributesLength; i++) {242if (ckpAttributes[i].pValue != NULL_PTR) {243free(ckpAttributes[i].pValue);244ckpAttributes[i].pValue = NULL_PTR;245}246}247248rv = (*ckpFunctions->C_GetAttributeValue)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength);249250if (rv != CKR_OK) {251if (rv == CKR_ATTRIBUTE_SENSITIVE || rv == CKR_ATTRIBUTE_TYPE_INVALID) {252msg = malloc(80); // should be more than sufficient253if (msg == NULL) {254throwOutOfMemoryError(env, 0);255free(ckpAttributes);256return;257}258// format msg w/ attribute(s) whose value is unavailable259temp1 = msg;260temp2 = msg + 80;261for (i = 0; i < ckAttributesLength && temp1 < temp2; i++) {262if (ckpAttributes[i].ulValueLen == CK_UNAVAILABLE_INFORMATION) {263temp1 += snprintf(temp1, (temp2-temp1), " 0x%lX",264ckpAttributes[i].type);265}266}267ckAssertReturnValueOK2(env, rv, msg);268free(msg);269} else {270ckAssertReturnValueOK(env, rv);271}272free(ckpAttributes);273return;274}275276/* now, the ulValueLength field of each attribute should hold the exact277* buffer length needed.278*/279for (i = 0; i < ckAttributesLength; i++) {280ckBufferLength = sizeof(CK_BYTE) * ckpAttributes[i].ulValueLen;281ckpAttributes[i].pValue = (void *) malloc(ckBufferLength);282if (ckpAttributes[i].pValue == NULL) {283freeCKAttributeArray(ckpAttributes, i);284throwOutOfMemoryError(env, 0);285return;286}287ckpAttributes[i].ulValueLen = ckBufferLength;288}289290/* now get all attribute values */291rv = (*ckpFunctions->C_GetAttributeValue)(ckSessionHandle,292ckObjectHandle, ckpAttributes, ckAttributesLength);293294if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {295/* copy back the values to the Java attributes */296for (i = 0; i < ckAttributesLength; i++) {297jAttribute = ckAttributePtrToJAttribute(env, &(ckpAttributes[i]));298if (jAttribute == NULL) {299freeCKAttributeArray(ckpAttributes, ckAttributesLength);300return;301}302(*env)->SetObjectArrayElement(env, jTemplate, i, jAttribute);303if ((*env)->ExceptionCheck(env)) {304freeCKAttributeArray(ckpAttributes, ckAttributesLength);305return;306}307}308}309freeCKAttributeArray(ckpAttributes, ckAttributesLength);310TRACE0("FINISHED\n");311}312#endif313314#ifdef P11_ENABLE_C_SETATTRIBUTEVALUE315/*316* Class: sun_security_pkcs11_wrapper_PKCS11317* Method: C_SetAttributeValue318* Signature: (JJ[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)V319* Parametermapping: *PKCS11*320* @param jlong jSessionHandle CK_SESSION_HANDLE hSession321* @param jlong jObjectHandle CK_OBJECT_HANDLE hObject322* @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate323* CK_ULONG ulCount324*/325JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SetAttributeValue326(JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jObjectHandle, jobjectArray jTemplate)327{328CK_SESSION_HANDLE ckSessionHandle;329CK_OBJECT_HANDLE ckObjectHandle;330CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;331CK_ULONG ckAttributesLength;332CK_RV rv;333334CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);335if (ckpFunctions == NULL) { return; }336337ckSessionHandle = jLongToCKULong(jSessionHandle);338ckObjectHandle = jLongToCKULong(jObjectHandle);339jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);340if ((*env)->ExceptionCheck(env)) { return; }341342rv = (*ckpFunctions->C_SetAttributeValue)(ckSessionHandle, ckObjectHandle, ckpAttributes, ckAttributesLength);343344freeCKAttributeArray(ckpAttributes, ckAttributesLength);345346if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }347}348#endif349350#ifdef P11_ENABLE_C_FINDOBJECTSINIT351/*352* Class: sun_security_pkcs11_wrapper_PKCS11353* Method: C_FindObjectsInit354* Signature: (J[Lsun/security/pkcs11/wrapper/CK_ATTRIBUTE;)V355* Parametermapping: *PKCS11*356* @param jlong jSessionHandle CK_SESSION_HANDLE hSession357* @param jobjectArray jTemplate CK_ATTRIBUTE_PTR pTemplate358* CK_ULONG ulCount359*/360JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1FindObjectsInit361(JNIEnv *env, jobject obj, jlong jSessionHandle, jobjectArray jTemplate)362{363CK_SESSION_HANDLE ckSessionHandle;364CK_ATTRIBUTE_PTR ckpAttributes = NULL_PTR;365CK_ULONG ckAttributesLength;366CK_RV rv;367368CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);369if (ckpFunctions == NULL) { return; }370371TRACE0("DEBUG: C_FindObjectsInit");372TRACE1(", hSession=%lld", (long long int) jSessionHandle);373TRACE1(", pTemplate=%p", jTemplate);374TRACE0(" ... ");375376ckSessionHandle = jLongToCKULong(jSessionHandle);377jAttributeArrayToCKAttributeArray(env, jTemplate, &ckpAttributes, &ckAttributesLength);378if ((*env)->ExceptionCheck(env)) { return; }379380rv = (*ckpFunctions->C_FindObjectsInit)(ckSessionHandle, ckpAttributes, ckAttributesLength);381382freeCKAttributeArray(ckpAttributes, ckAttributesLength);383TRACE0("FINISHED\n");384385if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }386}387#endif388389#ifdef P11_ENABLE_C_FINDOBJECTS390/*391* Class: sun_security_pkcs11_wrapper_PKCS11392* Method: C_FindObjects393* Signature: (JJ)[J394* Parametermapping: *PKCS11*395* @param jlong jSessionHandle CK_SESSION_HANDLE hSession396* @param jlong jMaxObjectCount CK_ULONG ulMaxObjectCount397* @return jlongArray jObjectHandleArray CK_OBJECT_HANDLE_PTR phObject398* CK_ULONG_PTR pulObjectCount399*/400JNIEXPORT jlongArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1FindObjects401(JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jMaxObjectCount)402{403CK_RV rv;404CK_SESSION_HANDLE ckSessionHandle;405CK_ULONG ckMaxObjectLength;406CK_OBJECT_HANDLE_PTR ckpObjectHandleArray;407CK_ULONG ckActualObjectCount;408jlongArray jObjectHandleArray = NULL;409410CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);411if (ckpFunctions == NULL) { return NULL; }412413ckSessionHandle = jLongToCKULong(jSessionHandle);414ckMaxObjectLength = jLongToCKULong(jMaxObjectCount);415ckpObjectHandleArray = (CK_OBJECT_HANDLE_PTR) malloc(sizeof(CK_OBJECT_HANDLE) * ckMaxObjectLength);416if (ckpObjectHandleArray == NULL) {417throwOutOfMemoryError(env, 0);418return NULL;419}420421rv = (*ckpFunctions->C_FindObjects)(ckSessionHandle, ckpObjectHandleArray, ckMaxObjectLength, &ckActualObjectCount);422if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {423jObjectHandleArray = ckULongArrayToJLongArray(env, ckpObjectHandleArray, ckActualObjectCount);424}425426free(ckpObjectHandleArray);427428return jObjectHandleArray ;429}430#endif431432#ifdef P11_ENABLE_C_FINDOBJECTSFINAL433/*434* Class: sun_security_pkcs11_wrapper_PKCS11435* Method: C_FindObjectsFinal436* Signature: (J)V437* Parametermapping: *PKCS11*438* @param jlong jSessionHandle CK_SESSION_HANDLE hSession439*/440JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1FindObjectsFinal441(JNIEnv *env, jobject obj, jlong jSessionHandle)442{443CK_SESSION_HANDLE ckSessionHandle;444CK_RV rv;445446CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);447if (ckpFunctions == NULL) { return; }448449ckSessionHandle = jLongToCKULong(jSessionHandle);450rv = (*ckpFunctions->C_FindObjectsFinal)(ckSessionHandle);451if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }452}453#endif454455456