8228835: Memory leak in PKCS11 provider when using AES GCM

Updated freeCKMechanismPtr to free mechanism-specific memories

Reviewed-by: jnimeh
This commit is contained in:
Valerie Peng 2019-08-14 01:40:29 +00:00
parent effe3eb806
commit 6212473fc4
4 changed files with 990 additions and 777 deletions

File diff suppressed because it is too large Load Diff

View File

@ -198,8 +198,8 @@ Java_sun_security_pkcs11_wrapper_PKCS11_getNativeKeyInfo
TRACE0("DEBUG: override CKA_NETSCAPE_DB attr value to TRUE\n");
}
ckpAttributes = (CK_ATTRIBUTE_PTR)malloc(
CK_ATTRIBUTES_TEMPLATE_LENGTH * sizeof(CK_ATTRIBUTE));
ckpAttributes = (CK_ATTRIBUTE_PTR) calloc(
CK_ATTRIBUTES_TEMPLATE_LENGTH, sizeof(CK_ATTRIBUTE));
if (ckpAttributes == NULL) {
throwOutOfMemoryError(env, 0);
goto cleanup;
@ -599,7 +599,7 @@ JNIEXPORT jlongArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Generate
ckpMechanism = jMechanismToCKMechanismPtr(env, jMechanism);
if ((*env)->ExceptionCheck(env)) { return NULL; }
ckpKeyHandles = (CK_OBJECT_HANDLE_PTR) malloc(2 * sizeof(CK_OBJECT_HANDLE));
ckpKeyHandles = (CK_OBJECT_HANDLE_PTR) calloc(2, sizeof(CK_OBJECT_HANDLE));
if (ckpKeyHandles == NULL) {
throwOutOfMemoryError(env, 0);
goto cleanup;
@ -698,7 +698,8 @@ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1WrapKey
rv = (*ckpFunctions->C_WrapKey)(ckSessionHandle, ckpMechanism, ckWrappingKeyHandle, ckKeyHandle, ckpWrappedKey, &ckWrappedKeyLength);
if (rv == CKR_BUFFER_TOO_SMALL) {
ckpWrappedKey = (CK_BYTE_PTR) malloc(ckWrappedKeyLength);
ckpWrappedKey = (CK_BYTE_PTR)
calloc(ckWrappedKeyLength, sizeof(CK_BYTE));
if (ckpWrappedKey == NULL) {
throwOutOfMemoryError(env, 0);
goto cleanup;
@ -793,51 +794,6 @@ cleanup:
#ifdef P11_ENABLE_C_DERIVEKEY
static void freeMasterKeyDeriveParams(CK_SSL3_RANDOM_DATA *RandomInfo, CK_VERSION_PTR pVersion) {
if (RandomInfo->pClientRandom != NULL) {
free(RandomInfo->pClientRandom);
}
if (RandomInfo->pServerRandom != NULL) {
free(RandomInfo->pServerRandom);
}
if (pVersion != NULL) {
free(pVersion);
}
}
void ssl3FreeMasterKeyDeriveParams(CK_MECHANISM_PTR ckpMechanism) {
CK_SSL3_MASTER_KEY_DERIVE_PARAMS *params =
(CK_SSL3_MASTER_KEY_DERIVE_PARAMS *) ckpMechanism->pParameter;
if (params == NULL) {
return;
}
freeMasterKeyDeriveParams(&(params->RandomInfo), params->pVersion);
}
void tls12FreeMasterKeyDeriveParams(CK_MECHANISM_PTR ckpMechanism) {
CK_TLS12_MASTER_KEY_DERIVE_PARAMS *params =
(CK_TLS12_MASTER_KEY_DERIVE_PARAMS *)ckpMechanism->pParameter;
if (params == NULL) {
return;
}
freeMasterKeyDeriveParams(&(params->RandomInfo), params->pVersion);
}
void freeEcdh1DeriveParams(CK_MECHANISM_PTR ckpMechanism) {
CK_ECDH1_DERIVE_PARAMS *params =
(CK_ECDH1_DERIVE_PARAMS *)ckpMechanism->pParameter;
if (params == NULL) {
return;
}
if (params->pSharedData != NULL) {
free(params->pSharedData);
}
if (params->pPublicData != NULL) {
free(params->pPublicData);
}
}
/*
* Copy back the PRF output to Java.
*/
@ -897,12 +853,6 @@ void copyBackTLSPrfParams(JNIEnv *env, CK_MECHANISM_PTR ckpMechanism, jobject jM
/* copy back the Java buffer to the object */
(*env)->ReleaseByteArrayElements(env, jOutput, jBytes, 0);
}
// free malloc'd data
free(ckTLSPrfParams->pSeed);
free(ckTLSPrfParams->pLabel);
free(ckTLSPrfParams->pulOutputLen);
free(ckTLSPrfParams->pOutput);
}
}
@ -968,18 +918,9 @@ JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DeriveKey
case CKM_TLS_MASTER_KEY_DERIVE:
/* we must copy back the client version */
ssl3CopyBackClientVersion(env, ckpMechanism, jMechanism);
ssl3FreeMasterKeyDeriveParams(ckpMechanism);
break;
case CKM_TLS12_MASTER_KEY_DERIVE:
tls12CopyBackClientVersion(env, ckpMechanism, jMechanism);
tls12FreeMasterKeyDeriveParams(ckpMechanism);
break;
case CKM_SSL3_MASTER_KEY_DERIVE_DH:
case CKM_TLS_MASTER_KEY_DERIVE_DH:
ssl3FreeMasterKeyDeriveParams(ckpMechanism);
break;
case CKM_TLS12_MASTER_KEY_DERIVE_DH:
tls12FreeMasterKeyDeriveParams(ckpMechanism);
break;
case CKM_SSL3_KEY_AND_MAC_DERIVE:
case CKM_TLS_KEY_AND_MAC_DERIVE:
@ -993,9 +934,6 @@ JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DeriveKey
case CKM_TLS_PRF:
copyBackTLSPrfParams(env, ckpMechanism, jMechanism);
break;
case CKM_ECDH1_DERIVE:
freeEcdh1DeriveParams(ckpMechanism);
break;
default:
// empty
break;
@ -1131,14 +1069,6 @@ static void copyBackKeyMatParams(JNIEnv *env, CK_MECHANISM_PTR ckpMechanism,
return;
}
// free malloc'd data
if (RandomInfo->pClientRandom != NULL) {
free(RandomInfo->pClientRandom);
}
if (RandomInfo->pServerRandom != NULL) {
free(RandomInfo->pServerRandom);
}
if (ckSSL3KeyMatOut != NULL_PTR) {
/* get the Java params object (pParameter) */
fieldID = (*env)->GetFieldID(env, jMechanismClass, "pParameter",
@ -1200,8 +1130,6 @@ static void copyBackKeyMatParams(JNIEnv *env, CK_MECHANISM_PTR ckpMechanism,
/* copy back the Java buffer to the object */
(*env)->ReleaseByteArrayElements(env, jIV, jBytes, 0);
}
// free malloc'd data
free(ckSSL3KeyMatOut->pIVClient);
/* copy back the server IV */
fieldID = (*env)->GetFieldID(env, jSSL3KeyMatOutClass, "pIVServer", "[B");
@ -1220,9 +1148,6 @@ static void copyBackKeyMatParams(JNIEnv *env, CK_MECHANISM_PTR ckpMechanism,
/* copy back the Java buffer to the object */
(*env)->ReleaseByteArrayElements(env, jIV, jBytes, 0);
}
// free malloc'd data
free(ckSSL3KeyMatOut->pIVServer);
free(ckSSL3KeyMatOut);
}
}

View File

@ -290,17 +290,122 @@ void freeCKAttributeArray(CK_ATTRIBUTE_PTR attrPtr, int len) {
}
/* This function frees the specified CK_MECHANISM_PTR pointer and its
* pParameter. NOTE: mechanism-specific memory allocations have to be
* freed before this call as this method only frees the generic
* memory associated with CK_MECHANISM structure.
* pParameter including mechanism-specific memory allocations.
*
* @param mechPtr pointer to the to-be-freed CK_MECHANISM structure.
*/
void freeCKMechanismPtr(CK_MECHANISM_PTR mechPtr) {
void *tmp;
CK_SSL3_MASTER_KEY_DERIVE_PARAMS *sslMkdTmp;
CK_SSL3_KEY_MAT_PARAMS* sslKmTmp;
CK_TLS12_MASTER_KEY_DERIVE_PARAMS *tlsMkdTmp;
CK_TLS12_KEY_MAT_PARAMS* tlsKmTmp;
if (mechPtr != NULL) {
TRACE1("DEBUG: free CK_MECHANISM %x", mechPtr);
free(mechPtr->pParameter);
TRACE2("DEBUG: free mech %lX (mech id = 0x%lX)\n",
ptr_to_jlong(mechPtr), mechPtr->mechanism);
if (mechPtr->pParameter != NULL) {
switch (mechPtr->mechanism) {
case CKM_AES_GCM:
tmp = mechPtr->pParameter;
TRACE1("\t=> free GCM_PARAMS %lX\n",
ptr_to_jlong(tmp));
free(((CK_GCM_PARAMS*)tmp)->pIv);
free(((CK_GCM_PARAMS*)tmp)->pAAD);
break;
case CKM_AES_CCM:
tmp = mechPtr->pParameter;
TRACE1("\t=> free CK_CCM_PARAMS %lX\n",
ptr_to_jlong(tmp));
free(((CK_CCM_PARAMS*)tmp)->pNonce);
free(((CK_CCM_PARAMS*)tmp)->pAAD);
break;
case CKM_TLS_PRF:
case CKM_NSS_TLS_PRF_GENERAL:
tmp = mechPtr->pParameter;
TRACE1("\t=> free CK_TLS_PRF_PARAMS %lX\n",
ptr_to_jlong(tmp));
free(((CK_TLS_PRF_PARAMS*)tmp)->pSeed);
free(((CK_TLS_PRF_PARAMS*)tmp)->pLabel);
free(((CK_TLS_PRF_PARAMS*)tmp)->pulOutputLen);
free(((CK_TLS_PRF_PARAMS*)tmp)->pOutput);
break;
case CKM_SSL3_MASTER_KEY_DERIVE:
case CKM_TLS_MASTER_KEY_DERIVE:
case CKM_SSL3_MASTER_KEY_DERIVE_DH:
case CKM_TLS_MASTER_KEY_DERIVE_DH:
sslMkdTmp = mechPtr->pParameter;
TRACE1("\t=> free CK_SSL3_MASTER_KEY_DERIVE_PARAMS %lX\n",
ptr_to_jlong(sslMkdTmp));
free(sslMkdTmp->RandomInfo.pClientRandom);
free(sslMkdTmp->RandomInfo.pServerRandom);
free(sslMkdTmp->pVersion);
break;
case CKM_SSL3_KEY_AND_MAC_DERIVE:
case CKM_TLS_KEY_AND_MAC_DERIVE:
sslKmTmp = mechPtr->pParameter;
TRACE1("\t=> free CK_SSL3_KEY_MAT_PARAMS %lX\n",
ptr_to_jlong(sslKmTmp));
free(sslKmTmp->RandomInfo.pClientRandom);
free(sslKmTmp->RandomInfo.pServerRandom);
if (sslKmTmp->pReturnedKeyMaterial != NULL) {
free(sslKmTmp->pReturnedKeyMaterial->pIVClient);
free(sslKmTmp->pReturnedKeyMaterial->pIVServer);
free(sslKmTmp->pReturnedKeyMaterial);
}
break;
case CKM_TLS12_MASTER_KEY_DERIVE:
case CKM_TLS12_MASTER_KEY_DERIVE_DH:
tlsMkdTmp = mechPtr->pParameter;
TRACE1("\t=> CK_TLS12_MASTER_KEY_DERIVE_PARAMS %lX\n",
ptr_to_jlong(tlsMkdTmp));
free(tlsMkdTmp->RandomInfo.pClientRandom);
free(tlsMkdTmp->RandomInfo.pServerRandom);
free(tlsMkdTmp->pVersion);
break;
case CKM_TLS12_KEY_AND_MAC_DERIVE:
tlsKmTmp = mechPtr->pParameter;
TRACE1("\t=> free CK_TLS12_KEY_MAT_PARAMS %lX\n",
ptr_to_jlong(tlsKmTmp));
free(tlsKmTmp->RandomInfo.pClientRandom);
free(tlsKmTmp->RandomInfo.pServerRandom);
if (tlsKmTmp->pReturnedKeyMaterial != NULL) {
free(tlsKmTmp->pReturnedKeyMaterial->pIVClient);
free(tlsKmTmp->pReturnedKeyMaterial->pIVServer);
free(tlsKmTmp->pReturnedKeyMaterial);
}
break;
case CKM_ECDH1_DERIVE:
case CKM_ECDH1_COFACTOR_DERIVE:
tmp = mechPtr->pParameter;
TRACE1("\t=> free CK_ECDH1_DERIVE_PARAMS %lX\n",
ptr_to_jlong(tmp));
free(((CK_ECDH1_DERIVE_PARAMS *)tmp)->pSharedData);
free(((CK_ECDH1_DERIVE_PARAMS *)tmp)->pPublicData);
break;
case CKM_TLS_MAC:
case CKM_AES_CTR:
case CKM_RSA_PKCS_PSS:
case CKM_CAMELLIA_CTR:
TRACE0("\t=> NO OP\n");
// params do not contain pointers
break;
default:
// currently unsupported mechs by SunPKCS11 provider
// CKM_RSA_PKCS_OAEP, CKM_ECMQV_DERIVE,
// CKM_X9_42_*, CKM_KEA_DERIVE, CKM_RC2_*, CKM_RC5_*,
// CKM_SKIPJACK_*, CKM_KEY_WRAP_SET_OAEP, CKM_PKCS5_PBKD2,
// PBE mechs, WTLS mechs, CMS mechs,
// CKM_EXTRACT_KEY_FROM_KEY, CKM_OTP, CKM_KIP,
// CKM_DSA_PARAMETER_GEN?, CKM_GOSTR3410_*
// CK_any_CBC_ENCRYPT_DATA?
TRACE0("\t=> ERROR UNSUPPORTED CK PARAMS\n");
break;
}
free(mechPtr->pParameter);
} else {
TRACE0("DEBUG => Parameter NULL\n");
}
free(mechPtr);
}
}
@ -356,7 +461,7 @@ void jBooleanArrayToCKBBoolArray(JNIEnv *env, const jbooleanArray jArray, CK_BBO
return;
}
*ckpLength = (*env)->GetArrayLength(env, jArray);
jpTemp = (jboolean*) malloc((*ckpLength) * sizeof(jboolean));
jpTemp = (jboolean*) calloc(*ckpLength, sizeof(jboolean));
if (jpTemp == NULL) {
throwOutOfMemoryError(env, 0);
return;
@ -367,7 +472,7 @@ void jBooleanArrayToCKBBoolArray(JNIEnv *env, const jbooleanArray jArray, CK_BBO
return;
}
*ckpArray = (CK_BBOOL*) malloc ((*ckpLength) * sizeof(CK_BBOOL));
*ckpArray = (CK_BBOOL*) calloc (*ckpLength, sizeof(CK_BBOOL));
if (*ckpArray == NULL) {
free(jpTemp);
throwOutOfMemoryError(env, 0);
@ -398,7 +503,7 @@ void jByteArrayToCKByteArray(JNIEnv *env, const jbyteArray jArray, CK_BYTE_PTR *
return;
}
*ckpLength = (*env)->GetArrayLength(env, jArray);
jpTemp = (jbyte*) malloc((*ckpLength) * sizeof(jbyte));
jpTemp = (jbyte*) calloc(*ckpLength, sizeof(jbyte));
if (jpTemp == NULL) {
throwOutOfMemoryError(env, 0);
return;
@ -413,7 +518,7 @@ void jByteArrayToCKByteArray(JNIEnv *env, const jbyteArray jArray, CK_BYTE_PTR *
if (sizeof(CK_BYTE) == sizeof(jbyte)) {
*ckpArray = (CK_BYTE_PTR) jpTemp;
} else {
*ckpArray = (CK_BYTE_PTR) malloc ((*ckpLength) * sizeof(CK_BYTE));
*ckpArray = (CK_BYTE_PTR) calloc (*ckpLength, sizeof(CK_BYTE));
if (*ckpArray == NULL) {
free(jpTemp);
throwOutOfMemoryError(env, 0);
@ -445,7 +550,7 @@ void jLongArrayToCKULongArray(JNIEnv *env, const jlongArray jArray, CK_ULONG_PTR
return;
}
*ckpLength = (*env)->GetArrayLength(env, jArray);
jTemp = (jlong*) malloc((*ckpLength) * sizeof(jlong));
jTemp = (jlong*) calloc(*ckpLength, sizeof(jlong));
if (jTemp == NULL) {
throwOutOfMemoryError(env, 0);
return;
@ -456,7 +561,7 @@ void jLongArrayToCKULongArray(JNIEnv *env, const jlongArray jArray, CK_ULONG_PTR
return;
}
*ckpArray = (CK_ULONG_PTR) malloc (*ckpLength * sizeof(CK_ULONG));
*ckpArray = (CK_ULONG_PTR) calloc(*ckpLength, sizeof(CK_ULONG));
if (*ckpArray == NULL) {
free(jTemp);
throwOutOfMemoryError(env, 0);
@ -487,7 +592,7 @@ void jCharArrayToCKCharArray(JNIEnv *env, const jcharArray jArray, CK_CHAR_PTR *
return;
}
*ckpLength = (*env)->GetArrayLength(env, jArray);
jpTemp = (jchar*) malloc((*ckpLength) * sizeof(jchar));
jpTemp = (jchar*) calloc(*ckpLength, sizeof(jchar));
if (jpTemp == NULL) {
throwOutOfMemoryError(env, 0);
return;
@ -498,7 +603,7 @@ void jCharArrayToCKCharArray(JNIEnv *env, const jcharArray jArray, CK_CHAR_PTR *
return;
}
*ckpArray = (CK_CHAR_PTR) malloc (*ckpLength * sizeof(CK_CHAR));
*ckpArray = (CK_CHAR_PTR) calloc (*ckpLength, sizeof(CK_CHAR));
if (*ckpArray == NULL) {
free(jpTemp);
throwOutOfMemoryError(env, 0);
@ -529,7 +634,7 @@ void jCharArrayToCKUTF8CharArray(JNIEnv *env, const jcharArray jArray, CK_UTF8CH
return;
}
*ckpLength = (*env)->GetArrayLength(env, jArray);
jTemp = (jchar*) malloc((*ckpLength) * sizeof(jchar));
jTemp = (jchar*) calloc(*ckpLength, sizeof(jchar));
if (jTemp == NULL) {
throwOutOfMemoryError(env, 0);
return;
@ -540,7 +645,7 @@ void jCharArrayToCKUTF8CharArray(JNIEnv *env, const jcharArray jArray, CK_UTF8CH
return;
}
*ckpArray = (CK_UTF8CHAR_PTR) malloc (*ckpLength * sizeof(CK_UTF8CHAR));
*ckpArray = (CK_UTF8CHAR_PTR) calloc(*ckpLength, sizeof(CK_UTF8CHAR));
if (*ckpArray == NULL) {
free(jTemp);
throwOutOfMemoryError(env, 0);
@ -575,7 +680,7 @@ void jStringToCKUTF8CharArray(JNIEnv *env, const jstring jArray, CK_UTF8CHAR_PTR
if (pCharArray == NULL) { return; }
*ckpLength = (CK_ULONG) strlen(pCharArray);
*ckpArray = (CK_UTF8CHAR_PTR) malloc((*ckpLength + 1) * sizeof(CK_UTF8CHAR));
*ckpArray = (CK_UTF8CHAR_PTR) calloc(*ckpLength + 1, sizeof(CK_UTF8CHAR));
if (*ckpArray == NULL) {
(*env)->ReleaseStringUTFChars(env, (jstring) jArray, pCharArray);
throwOutOfMemoryError(env, 0);
@ -609,7 +714,7 @@ void jAttributeArrayToCKAttributeArray(JNIEnv *env, jobjectArray jArray, CK_ATTR
}
jLength = (*env)->GetArrayLength(env, jArray);
*ckpLength = jLongToCKULong(jLength);
*ckpArray = (CK_ATTRIBUTE_PTR) malloc(*ckpLength * sizeof(CK_ATTRIBUTE));
*ckpArray = (CK_ATTRIBUTE_PTR) calloc(*ckpLength, sizeof(CK_ATTRIBUTE));
if (*ckpArray == NULL) {
throwOutOfMemoryError(env, 0);
return;
@ -651,7 +756,7 @@ jbyteArray ckByteArrayToJByteArray(JNIEnv *env, const CK_BYTE_PTR ckpArray, CK_U
if (sizeof(CK_BYTE) == sizeof(jbyte)) {
jpTemp = (jbyte*) ckpArray;
} else {
jpTemp = (jbyte*) malloc((ckLength) * sizeof(jbyte));
jpTemp = (jbyte*) calloc(ckLength, sizeof(jbyte));
if (jpTemp == NULL) {
throwOutOfMemoryError(env, 0);
return NULL;
@ -685,7 +790,7 @@ jlongArray ckULongArrayToJLongArray(JNIEnv *env, const CK_ULONG_PTR ckpArray, CK
jlong* jpTemp;
jlongArray jArray;
jpTemp = (jlong*) malloc((ckLength) * sizeof(jlong));
jpTemp = (jlong*) calloc(ckLength, sizeof(jlong));
if (jpTemp == NULL) {
throwOutOfMemoryError(env, 0);
return NULL;
@ -716,7 +821,7 @@ jcharArray ckCharArrayToJCharArray(JNIEnv *env, const CK_CHAR_PTR ckpArray, CK_U
jchar* jpTemp;
jcharArray jArray;
jpTemp = (jchar*) malloc(ckLength * sizeof(jchar));
jpTemp = (jchar*) calloc(ckLength, sizeof(jchar));
if (jpTemp == NULL) {
throwOutOfMemoryError(env, 0);
return NULL;
@ -747,7 +852,7 @@ jcharArray ckUTF8CharArrayToJCharArray(JNIEnv *env, const CK_UTF8CHAR_PTR ckpArr
jchar* jpTemp;
jcharArray jArray;
jpTemp = (jchar*) malloc(ckLength * sizeof(jchar));
jpTemp = (jchar*) calloc(ckLength, sizeof(jchar));
if (jpTemp == NULL) {
throwOutOfMemoryError(env, 0);
return NULL;
@ -1051,7 +1156,7 @@ CK_VOID_PTR jObjectToPrimitiveCKObjectPtr(JNIEnv *env, jobject jObject, CK_ULONG
jDateClass = (*env)->FindClass(env, CLASS_DATE);
if (jDateClass == NULL) { return NULL; }
if ((*env)->IsInstanceOf(env, jObject, jDateClass)) {
ckpObject = jDateObjectPtrToCKDatePtr(env, jObject);
ckpObject = jDateObjectToCKDatePtr(env, jObject);
*ckpLength = sizeof(CK_DATE);
TRACE3("<converted date value %.4s-%.2s-%.2s>", ((CK_DATE *) ckpObject)->year,
((CK_DATE *) ckpObject)->month, ((CK_DATE *) ckpObject)->day);
@ -1123,7 +1228,7 @@ CK_VOID_PTR jObjectToPrimitiveCKObjectPtr(JNIEnv *env, jobject jObject, CK_ULONG
if (classNameString == NULL) { return NULL; }
exceptionMsgPrefix = "Java object of this class cannot be converted to native PKCS#11 type: ";
exceptionMsg = (char *)
malloc((strlen(exceptionMsgPrefix) + strlen(classNameString) + 1));
malloc(strlen(exceptionMsgPrefix) + strlen(classNameString) + 1);
if (exceptionMsg == NULL) {
(*env)->ReleaseStringUTFChars(env, jClassNameString, classNameString);
throwOutOfMemoryError(env, 0);
@ -1143,16 +1248,26 @@ CK_VOID_PTR jObjectToPrimitiveCKObjectPtr(JNIEnv *env, jobject jObject, CK_ULONG
#ifdef P11_MEMORYDEBUG
#undef malloc
#undef calloc
#undef free
void *p11malloc(size_t c, char *file, int line) {
void *p = malloc(c);
printf("malloc\t%08x\t%d\t%s:%d\n", p, c, file, line); fflush(stdout);
fprintf(stdout, "malloc\t%08lX\t%lX\t%s:%d\n", ptr_to_jlong(p), c, file, line);
fflush(stdout);
return p;
}
void *p11calloc(size_t c, size_t s, char *file, int line) {
void *p = calloc(c, s);
fprintf(stdout, "calloc\t%08lX\t%lX\t%lX\t%s:%d\n", ptr_to_jlong(p), c, s, file, line);
fflush(stdout);
return p;
}
void p11free(void *p, char *file, int line) {
printf("free\t%08x\t\t%s:%d\n", p, file, line); fflush(stdout);
fprintf(stdout, "free\t%08lX\t\t%s:%d\n", ptr_to_jlong(p), file, line);
fflush(stdout);
free(p);
}

View File

@ -207,6 +207,10 @@
#define ckULongToJSize(x) ((jsize) x)
#define unsignedIntToCKULong(x) ((CK_ULONG) x)
//#define TRACE0d(s) { printf(s); fflush(stdout); }
//#define TRACE1d(s, p1) { printf(s, p1); fflush(stdout); }
//#define TRACE2d(s, p1, p2) { printf(s, p1, p2); fflush(stdout); }
#ifdef P11_DEBUG
#define TRACE0(s) { printf(s); fflush(stdout); }
#define TRACE1(s, p1) { printf(s, p1); fflush(stdout); }
@ -352,7 +356,7 @@ CK_ULONG* jIntegerObjectToCKULongPtr(JNIEnv *env, jobject jObject);
CK_ULONG* jLongObjectToCKULongPtr(JNIEnv *env, jobject jObject);
CK_CHAR_PTR jCharObjectToCKCharPtr(JNIEnv *env, jobject jObject);
CK_VERSION_PTR jVersionToCKVersionPtr(JNIEnv *env, jobject jVersion);
CK_DATE * jDateObjectPtrToCKDatePtr(JNIEnv *env, jobject jDate);
CK_DATE * jDateObjectToCKDatePtr(JNIEnv *env, jobject jDate);
CK_ATTRIBUTE jAttributeToCKAttribute(JNIEnv *env, jobject jAttribute);
CK_MECHANISM_PTR jMechanismToCKMechanismPtr(JNIEnv *env, jobject jMechanism);
@ -363,26 +367,29 @@ CK_VOID_PTR jMechParamToCKMechParamPtr(JNIEnv *env, jobject jParam, CK_MECHANISM
*ckpLength);
/* functions to convert a specific Java mechanism parameter object to a CK-mechanism parameter structure */
/* functions to convert a specific Java mechanism parameter object to a CK-mechanism parameter pointer */
CK_RSA_PKCS_OAEP_PARAMS_PTR jRsaPkcsOaepParamToCKRsaPkcsOaepParamPtr(JNIEnv *env,
jobject jParam, CK_ULONG* pLength);
CK_PBE_PARAMS_PTR jPbeParamToCKPbeParamPtr(JNIEnv *env, jobject jParam, CK_ULONG* pLength);
CK_PKCS5_PBKD2_PARAMS_PTR jPkcs5Pbkd2ParamToCKPkcs5Pbkd2ParamPtr(JNIEnv *env, jobject jParam, CK_ULONG* pLength);
CK_SSL3_MASTER_KEY_DERIVE_PARAMS_PTR jSsl3MasterKeyDeriveParamToCKSsl3MasterKeyDeriveParamPtr(JNIEnv *env, jobject jParam, CK_ULONG* pLength);
CK_SSL3_KEY_MAT_PARAMS_PTR jSsl3KeyMatParamToCKSsl3KeyMatParamPtr(JNIEnv *env, jobject jParam, CK_ULONG* pLength);
CK_KEY_DERIVATION_STRING_DATA jKeyDerivationStringDataToCKKeyDerivationStringData(JNIEnv *env, jobject jParam);
CK_RSA_PKCS_PSS_PARAMS_PTR jRsaPkcsPssParamToCKRsaPkcsPssParamPtr(JNIEnv *env, jobject jParam, CK_ULONG* pLength);
CK_ECDH1_DERIVE_PARAMS_PTR jEcdh1DeriveParamToCKEcdh1DeriveParamPtr(JNIEnv *env, jobject jParam, CK_ULONG* pLength);
CK_ECDH2_DERIVE_PARAMS_PTR jEcdh2DeriveParamToCKEcdh2DeriveParamPtr(JNIEnv *env, jobject jParam, CK_ULONG* pLength);
CK_X9_42_DH1_DERIVE_PARAMS_PTR jX942Dh1DeriveParamToCKX942Dh1DeriveParamPtr(JNIEnv *env, jobject jParam, CK_ULONG* pLength);
CK_X9_42_DH2_DERIVE_PARAMS_PTR jX942Dh2DeriveParamToCKX942Dh2DeriveParamPtr(JNIEnv *env, jobject jParam, CK_ULONG* pLength);
/* functions to copy the returned values inside CK-mechanism back to Java object */
void jRsaPkcsOaepParamToCKRsaPkcsOaepParam(JNIEnv *env, jobject jParam, CK_RSA_PKCS_OAEP_PARAMS_PTR ckParamPtr);
void jPbeParamToCKPbeParam(JNIEnv *env, jobject jParam, CK_PBE_PARAMS_PTR ckParamPtr);
void copyBackPBEInitializationVector(JNIEnv *env, CK_MECHANISM *ckMechanism, jobject jMechanism);
void jPkcs5Pbkd2ParamToCKPkcs5Pbkd2Param(JNIEnv *env, jobject jParam, CK_PKCS5_PBKD2_PARAMS_PTR ckParamPtr);
void copyBackSetUnwrappedKey(JNIEnv *env, CK_MECHANISM *ckMechanism, jobject jMechanism);
void jSsl3MasterKeyDeriveParamToCKSsl3MasterKeyDeriveParam(JNIEnv *env, jobject jParam, CK_SSL3_MASTER_KEY_DERIVE_PARAMS_PTR ckParamPtr);
void ssl3CopyBackClientVersion(JNIEnv *env, CK_MECHANISM *ckMechanism, jobject jMechanism);
void tls12CopyBackClientVersion(JNIEnv *env, CK_MECHANISM *ckMechanism, jobject jMechanism);
void jSsl3KeyMatParamToCKSsl3KeyMatParam(JNIEnv *env, jobject jParam, CK_SSL3_KEY_MAT_PARAMS_PTR ckParamPtr);
void ssl3CopyBackKeyMatParams(JNIEnv *env, CK_MECHANISM *ckMechanism, jobject jMechanism);
void tls12CopyBackKeyMatParams(JNIEnv *env, CK_MECHANISM *ckMechanism, jobject jMechanism);
CK_KEY_DERIVATION_STRING_DATA jKeyDerivationStringDataToCKKeyDerivationStringData(JNIEnv *env, jobject jParam);
void jRsaPkcsPssParamToCKRsaPkcsPssParam(JNIEnv *env, jobject jParam, CK_RSA_PKCS_PSS_PARAMS_PTR ckParamPtr);
void jEcdh1DeriveParamToCKEcdh1DeriveParam(JNIEnv *env, jobject jParam, CK_ECDH1_DERIVE_PARAMS_PTR ckParamPtr);
void jEcdh2DeriveParamToCKEcdh2DeriveParam(JNIEnv *env, jobject jParam,
CK_ECDH2_DERIVE_PARAMS_PTR ckParamPtr);
void jX942Dh1DeriveParamToCKX942Dh1DeriveParam(JNIEnv *env, jobject jParam, CK_X9_42_DH1_DERIVE_PARAMS_PTR ckParamPtr);
void jX942Dh2DeriveParamToCKX942Dh2DeriveParam(JNIEnv *env, jobject jParam, CK_X9_42_DH2_DERIVE_PARAMS_PTR ckParamPtr);
/* functions to convert the InitArgs object for calling the right Java mutex functions */
@ -462,8 +469,9 @@ extern CK_C_INITIALIZE_ARGS_PTR ckpGlobalInitArgs;
#ifdef P11_MEMORYDEBUG
#include <stdlib.h>
/* Simple malloc/free dumper */
/* Simple malloc/calloc/free dumper */
void *p11malloc(size_t c, char *file, int line);
void *p11calloc(size_t c, size_t s, char *file, int line);
void p11free(void *p, char *file, int line);
/* Use THIS_FILE when it is available. */
@ -472,6 +480,7 @@ void p11free(void *p, char *file, int line);
#endif
#define malloc(c) (p11malloc((c), THIS_FILE, __LINE__))
#define calloc(c, s) (p11calloc((c), (s), THIS_FILE, __LINE__))
#define free(c) (p11free((c), THIS_FILE, __LINE__))
#endif