/* * Class: sun_security_pkcs11_wrapper_PKCS11 * Method: C_SignFinal * Signature: (J)[B * Parametermapping: *PKCS11* * @param jlong jSessionHandle CK_SESSION_HANDLE hSession * @return jbyteArray jSignature CK_BYTE_PTR pSignature * CK_ULONG_PTR pulSignatureLen */ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SignFinal (JNIEnv *env, jobject obj, jlong jSessionHandle, jint jExpectedLength) { CK_SESSION_HANDLE ckSessionHandle; jbyteArray jSignature = NULL; CK_RV rv; CK_BYTE BUF[MAX_STACK_BUFFER_LEN]; CK_BYTE_PTR bufP = BUF; CK_ULONG ckSignatureLength = MAX_STACK_BUFFER_LEN; CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); if (ckpFunctions == NULL) { return NULL; } ckSessionHandle = jLongToCKULong(jSessionHandle); if ((jExpectedLength > 0) && (jExpectedLength < ckSignatureLength)) { ckSignatureLength = jExpectedLength; } rv = (*ckpFunctions->C_SignFinal)(ckSessionHandle, bufP, &ckSignatureLength); if (rv == CKR_BUFFER_TOO_SMALL) { bufP = (CK_BYTE_PTR) malloc(ckSignatureLength); rv = (*ckpFunctions->C_SignFinal)(ckSessionHandle, bufP, &ckSignatureLength); } if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) { jSignature = ckByteArrayToJByteArray(env, bufP, ckSignatureLength); } if (bufP != BUF) { free(bufP); } return jSignature; }
/* * Class: sun_security_pkcs11_wrapper_PKCS11 * Method: C_GetOperationState * Signature: (J)[B * Parametermapping: *PKCS11* * @param jlong jSessionHandle CK_SESSION_HANDLE hSession * @return jbyteArray jState CK_BYTE_PTR pOperationState * CK_ULONG_PTR pulOperationStateLen */ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GetOperationState (JNIEnv *env, jobject obj, jlong jSessionHandle) { CK_SESSION_HANDLE ckSessionHandle; CK_BYTE_PTR ckpState; CK_ULONG ckStateLength; jbyteArray jState = NULL; CK_RV rv; CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); if (ckpFunctions == NULL) { return NULL; } ckSessionHandle = jLongToCKULong(jSessionHandle); rv = (*ckpFunctions->C_GetOperationState)(ckSessionHandle, NULL_PTR, &ckStateLength); if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; } ckpState = (CK_BYTE_PTR) malloc(ckStateLength); if (ckpState == NULL) { JNU_ThrowOutOfMemoryError(env, 0); return NULL; } rv = (*ckpFunctions->C_GetOperationState)(ckSessionHandle, ckpState, &ckStateLength); if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) { jState = ckByteArrayToJByteArray(env, ckpState, ckStateLength); } free(ckpState); return jState ; }
/* * Class: sun_security_pkcs11_wrapper_PKCS11 * Method: C_Sign * Signature: (J[B)[B * Parametermapping: *PKCS11* * @param jlong jSessionHandle CK_SESSION_HANDLE hSession * @param jbyteArray jData CK_BYTE_PTR pData * CK_ULONG ulDataLen * @return jbyteArray jSignature CK_BYTE_PTR pSignature * CK_ULONG_PTR pulSignatureLen */ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1Sign (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jData) { CK_SESSION_HANDLE ckSessionHandle; CK_BYTE_PTR ckpData = NULL_PTR; CK_BYTE_PTR ckpSignature; CK_ULONG ckDataLength; CK_ULONG ckSignatureLength = 0; jbyteArray jSignature; CK_RV rv; CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); if (ckpFunctions == NULL) { return NULL; } ckSessionHandle = jLongToCKULong(jSessionHandle); jByteArrayToCKByteArray(env, jData, &ckpData, &ckDataLength); /* START standard code */ /* first determine the length of the signature */ rv = (*ckpFunctions->C_Sign)(ckSessionHandle, ckpData, ckDataLength, NULL_PTR, &ckSignatureLength); if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; } ckpSignature = (CK_BYTE_PTR) malloc(ckSignatureLength * sizeof(CK_BYTE)); /* now get the signature */ rv = (*ckpFunctions->C_Sign)(ckSessionHandle, ckpData, ckDataLength, ckpSignature, &ckSignatureLength); /* END standard code */ /* START workaround code for operation abort bug in pkcs#11 of Datakey and iButton */ /* ckpSignature = (CK_BYTE_PTR) malloc(256 * sizeof(CK_BYTE)); rv = (*ckpFunctions->C_Sign)(ckSessionHandle, ckpData, ckDataLength, ckpSignature, &ckSignatureLength); if (rv == CKR_BUFFER_TOO_SMALL) { free(ckpSignature); ckpSignature = (CK_BYTE_PTR) malloc(ckSignatureLength * sizeof(CK_BYTE)); rv = (*ckpFunctions->C_Sign)(ckSessionHandle, ckpData, ckDataLength, ckpSignature, &ckSignatureLength); } */ /* END workaround code */ jSignature = ckByteArrayToJByteArray(env, ckpSignature, ckSignatureLength); free(ckpData); free(ckpSignature); if(ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return NULL ; } return jSignature ; }
/* * Class: sun_security_pkcs11_wrapper_PKCS11 * Method: C_WrapKey * Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;JJ)[B * Parametermapping: *PKCS11* * @param jlong jSessionHandle CK_SESSION_HANDLE hSession * @param jobject jMechanism CK_MECHANISM_PTR pMechanism * @param jlong jWrappingKeyHandle CK_OBJECT_HANDLE hWrappingKey * @param jlong jKeyHandle CK_OBJECT_HANDLE hKey * @return jbyteArray jWrappedKey CK_BYTE_PTR pWrappedKey * CK_ULONG_PTR pulWrappedKeyLen */ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1WrapKey (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jlong jWrappingKeyHandle, jlong jKeyHandle) { CK_SESSION_HANDLE ckSessionHandle; CK_MECHANISM ckMechanism; CK_OBJECT_HANDLE ckWrappingKeyHandle; CK_OBJECT_HANDLE ckKeyHandle; jbyteArray jWrappedKey = NULL; CK_RV rv; CK_BYTE BUF[MAX_STACK_BUFFER_LEN]; CK_BYTE_PTR ckpWrappedKey = BUF; CK_ULONG ckWrappedKeyLength = MAX_STACK_BUFFER_LEN; CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); if (ckpFunctions == NULL) { return NULL; } ckSessionHandle = jLongToCKULong(jSessionHandle); jMechanismToCKMechanism(env, jMechanism, &ckMechanism); if ((*env)->ExceptionCheck(env)) { return NULL; } ckWrappingKeyHandle = jLongToCKULong(jWrappingKeyHandle); ckKeyHandle = jLongToCKULong(jKeyHandle); rv = (*ckpFunctions->C_WrapKey)(ckSessionHandle, &ckMechanism, ckWrappingKeyHandle, ckKeyHandle, ckpWrappedKey, &ckWrappedKeyLength); if (rv == CKR_BUFFER_TOO_SMALL) { ckpWrappedKey = (CK_BYTE_PTR) malloc(ckWrappedKeyLength); if (ckpWrappedKey == NULL) { if (ckMechanism.pParameter != NULL_PTR) { free(ckMechanism.pParameter); } throwOutOfMemoryError(env, 0); return NULL; } rv = (*ckpFunctions->C_WrapKey)(ckSessionHandle, &ckMechanism, ckWrappingKeyHandle, ckKeyHandle, ckpWrappedKey, &ckWrappedKeyLength); } if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) { jWrappedKey = ckByteArrayToJByteArray(env, ckpWrappedKey, ckWrappedKeyLength); } if (ckpWrappedKey != BUF) { free(ckpWrappedKey); } if (ckMechanism.pParameter != NULL_PTR) { free(ckMechanism.pParameter); } return jWrappedKey ; }
/* * Class: sun_security_pkcs11_wrapper_PKCS11 * Method: C_DigestEncryptUpdate * Signature: (J[B)[B * Parametermapping: *PKCS11* * @param jlong jSessionHandle CK_SESSION_HANDLE hSession * @param jbyteArray jPart CK_BYTE_PTR pPart * CK_ULONG ulPartLen * @return jbyteArray jEncryptedPart CK_BYTE_PTR pEncryptedPart * CK_ULONG_PTR pulEncryptedPartLen */ JNIEXPORT jbyteArray JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestEncryptUpdate (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jPart) { CK_SESSION_HANDLE ckSessionHandle; CK_BYTE_PTR ckpPart = NULL_PTR, ckpEncryptedPart; CK_ULONG ckPartLength, ckEncryptedPartLength = 0; jbyteArray jEncryptedPart = NULL; CK_RV rv; CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj); if (ckpFunctions == NULL) { return NULL; } ckSessionHandle = jLongToCKULong(jSessionHandle); jByteArrayToCKByteArray(env, jPart, &ckpPart, &ckPartLength); if ((*env)->ExceptionCheck(env)) { return NULL; } rv = (*ckpFunctions->C_DigestEncryptUpdate)(ckSessionHandle, ckpPart, ckPartLength, NULL_PTR, &ckEncryptedPartLength); if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { free(ckpPart); return NULL; } ckpEncryptedPart = (CK_BYTE_PTR) malloc(ckEncryptedPartLength * sizeof(CK_BYTE)); if (ckpEncryptedPart == NULL) { free(ckpPart); JNU_ThrowOutOfMemoryError(env, 0); return NULL; } rv = (*ckpFunctions->C_DigestEncryptUpdate)(ckSessionHandle, ckpPart, ckPartLength, ckpEncryptedPart, &ckEncryptedPartLength); if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) { jEncryptedPart = ckByteArrayToJByteArray(env, ckpEncryptedPart, ckEncryptedPartLength); } free(ckpPart); free(ckpEncryptedPart); return jEncryptedPart ; }