Path: blob/master/src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_crypt.c
41152 views
/*1* Copyright (c) 2003, 2020, 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* ===========================================================================46*/4748#include "pkcs11wrapper.h"4950#include <stdio.h>51#include <stdlib.h>52#include <string.h>53#include <assert.h>5455#include "sun_security_pkcs11_wrapper_PKCS11.h"5657#ifdef P11_ENABLE_C_ENCRYPTINIT58/*59* Class: sun_security_pkcs11_wrapper_PKCS1160* Method: C_EncryptInit61* Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;J)V62* Parametermapping: *PKCS11*63* @param jlong jSessionHandle CK_SESSION_HANDLE hSession64* @param jobject jMechanism CK_MECHANISM_PTR pMechanism65* @param jlong jKeyHandle CK_OBJECT_HANDLE hKey66*/67JNIEXPORT void JNICALL68Java_sun_security_pkcs11_wrapper_PKCS11_C_1EncryptInit69(JNIEnv *env, jobject obj, jlong jSessionHandle,70jobject jMechanism, jlong jKeyHandle)71{72CK_SESSION_HANDLE ckSessionHandle;73CK_MECHANISM_PTR ckpMechanism = NULL;74CK_MECHANISM_PTR ckpTemp;75CK_OBJECT_HANDLE ckKeyHandle;76CK_RV rv;7778CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);79if (ckpFunctions == NULL) { return; }8081ckSessionHandle = jLongToCKULong(jSessionHandle);82ckKeyHandle = jLongToCKULong(jKeyHandle);83ckpMechanism = jMechanismToCKMechanismPtr(env, jMechanism);84TRACE1("DEBUG C_EncryptInit: created pMech = %p\n",85ckpMechanism);8687if ((*env)->ExceptionCheck(env)) { return; }8889rv = (*ckpFunctions->C_EncryptInit)(ckSessionHandle, ckpMechanism,90ckKeyHandle);9192if (ckpMechanism->mechanism == CKM_AES_GCM) {93if (rv == CKR_ARGUMENTS_BAD || rv == CKR_MECHANISM_PARAM_INVALID) {94// retry with CKM_GCM_PARAMS structure in pkcs11t.h95TRACE0("DEBUG C_EncryptInit: retry with CK_GCM_PARAMS\n");96ckpTemp = updateGCMParams(env, ckpMechanism);97if (ckpTemp != NULL) { // only re-call if conversion succeeds98ckpMechanism = ckpTemp;99rv = (*ckpFunctions->C_EncryptInit)(ckSessionHandle, ckpMechanism,100ckKeyHandle);101}102}103}104105TRACE1("DEBUG C_EncryptInit: freed pMech = %p\n", ckpMechanism);106freeCKMechanismPtr(ckpMechanism);107if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }108109TRACE0("FINISHED\n");110}111#endif112113#ifdef P11_ENABLE_C_ENCRYPT114/*115* Class: sun_security_pkcs11_wrapper_PKCS11116* Method: C_Encrypt117* Signature: (JJ[BIIJ[BII)I118* Parametermapping: *PKCS11*119* @param jlong jSessionHandle CK_SESSION_HANDLE hSession120* @param jlong directIn CK_BYTE_PTR pData121* @param jbyteArray jData CK_BYTE_PTR pData122* CK_ULONG ulDataLen123* @param jlong directOut CK_BYTE_PTR pEncryptedData124* @return jint encryptedDataLen CK_BYTE_PTR pEncryptedData125* CK_ULONG_PTR pulEncryptedDataLen126*/127JNIEXPORT jint JNICALL128Java_sun_security_pkcs11_wrapper_PKCS11_C_1Encrypt129(JNIEnv *env, jobject obj, jlong jSessionHandle,130jlong directIn, jbyteArray jIn, jint jInOfs, jint jInLen,131jlong directOut, jbyteArray jOut, jint jOutOfs, jint jOutLen)132{133CK_SESSION_HANDLE ckSessionHandle;134CK_RV rv;135136CK_BYTE_PTR inBufP;137CK_BYTE_PTR outBufP;138CK_ULONG ckEncryptedLen = 0;139140CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);141if (ckpFunctions == NULL) { return 0; }142143ckSessionHandle = jLongToCKULong(jSessionHandle);144145if (directIn != 0) {146inBufP = (CK_BYTE_PTR) jlong_to_ptr(directIn);147} else {148inBufP = (*env)->GetPrimitiveArrayCritical(env, jIn, NULL);149if (inBufP == NULL) { return 0; }150}151152if (directOut != 0) {153outBufP = (CK_BYTE_PTR) jlong_to_ptr(directOut);154} else {155outBufP = (*env)->GetPrimitiveArrayCritical(env, jOut, NULL);156if (outBufP == NULL) {157goto cleanup;158}159}160161ckEncryptedLen = jOutLen;162163rv = (*ckpFunctions->C_Encrypt)(ckSessionHandle,164(CK_BYTE_PTR)(inBufP + jInOfs), jInLen,165(CK_BYTE_PTR)(outBufP + jOutOfs),166&ckEncryptedLen);167168ckAssertReturnValueOK(env, rv);169170cleanup:171if (directIn == 0 && inBufP != NULL) {172(*env)->ReleasePrimitiveArrayCritical(env, jIn, inBufP, JNI_ABORT);173}174if (directOut == 0 && outBufP != NULL) {175(*env)->ReleasePrimitiveArrayCritical(env, jOut, outBufP, 0);176}177return ckEncryptedLen;178}179#endif180181#ifdef P11_ENABLE_C_ENCRYPTUPDATE182/*183* Class: sun_security_pkcs11_wrapper_PKCS11184* Method: C_EncryptUpdate185* Signature: (J[BII[BII)I186* Parametermapping: *PKCS11*187* @param jlong jSessionHandle CK_SESSION_HANDLE hSession188* @param jbyteArray jPart CK_BYTE_PTR pPart189* CK_ULONG ulPartLen190* @return jbyteArray jEncryptedPart CK_BYTE_PTR pEncryptedPart191* CK_ULONG_PTR pulEncryptedPartLen192*/193JNIEXPORT jint JNICALL194Java_sun_security_pkcs11_wrapper_PKCS11_C_1EncryptUpdate195(JNIEnv *env, jobject obj, jlong jSessionHandle,196jlong directIn, jbyteArray jIn, jint jInOfs, jint jInLen,197jlong directOut, jbyteArray jOut, jint jOutOfs, jint jOutLen)198{199CK_SESSION_HANDLE ckSessionHandle;200CK_RV rv;201202CK_BYTE_PTR inBufP;203CK_BYTE_PTR outBufP;204CK_ULONG ckEncryptedPartLen = 0;205206CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);207if (ckpFunctions == NULL) { return 0; }208209ckSessionHandle = jLongToCKULong(jSessionHandle);210211if (directIn != 0) {212inBufP = (CK_BYTE_PTR) jlong_to_ptr(directIn);213} else {214inBufP = (*env)->GetPrimitiveArrayCritical(env, jIn, NULL);215if (inBufP == NULL) { return 0; }216}217218if (directOut != 0) {219outBufP = (CK_BYTE_PTR) jlong_to_ptr(directOut);220} else {221outBufP = (*env)->GetPrimitiveArrayCritical(env, jOut, NULL);222if (outBufP == NULL) {223goto cleanup;224}225}226227ckEncryptedPartLen = jOutLen;228229rv = (*ckpFunctions->C_EncryptUpdate)(ckSessionHandle,230(CK_BYTE_PTR)(inBufP + jInOfs), jInLen,231(CK_BYTE_PTR)(outBufP + jOutOfs),232&ckEncryptedPartLen);233234ckAssertReturnValueOK(env, rv);235236cleanup:237if (directIn == 0 && inBufP != NULL) {238(*env)->ReleasePrimitiveArrayCritical(env, jIn, inBufP, JNI_ABORT);239}240if (directOut == 0 && outBufP != NULL) {241(*env)->ReleasePrimitiveArrayCritical(env, jOut, outBufP, 0);242}243return ckEncryptedPartLen;244}245#endif246247#ifdef P11_ENABLE_C_ENCRYPTFINAL248/*249* Class: sun_security_pkcs11_wrapper_PKCS11250* Method: C_EncryptFinal251* Signature: (J[BII)I252* Parametermapping: *PKCS11*253* @param jlong jSessionHandle CK_SESSION_HANDLE hSession254* @return jbyteArray jLastEncryptedPart CK_BYTE_PTR pLastEncryptedDataPart255* CK_ULONG_PTR pulLastEncryptedDataPartLen256*/257JNIEXPORT jint JNICALL258Java_sun_security_pkcs11_wrapper_PKCS11_C_1EncryptFinal259(JNIEnv *env, jobject obj, jlong jSessionHandle,260jlong directOut, jbyteArray jOut, jint jOutOfs, jint jOutLen)261{262CK_SESSION_HANDLE ckSessionHandle;263CK_RV rv;264CK_BYTE_PTR outBufP;265CK_ULONG ckLastEncryptedPartLen;266267CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);268if (ckpFunctions == NULL) { return 0; }269270ckSessionHandle = jLongToCKULong(jSessionHandle);271272if (directOut != 0) {273outBufP = (CK_BYTE_PTR) jlong_to_ptr(directOut);274} else {275outBufP = (*env)->GetPrimitiveArrayCritical(env, jOut, NULL);276if (outBufP == NULL) { return 0; }277}278279ckLastEncryptedPartLen = jOutLen;280281rv = (*ckpFunctions->C_EncryptFinal)(ckSessionHandle,282(CK_BYTE_PTR)(outBufP + jOutOfs),283&ckLastEncryptedPartLen);284285if (directOut == 0) {286(*env)->ReleasePrimitiveArrayCritical(env, jOut, outBufP, 0);287}288289ckAssertReturnValueOK(env, rv);290291return ckLastEncryptedPartLen;292}293#endif294295#ifdef P11_ENABLE_C_DECRYPTINIT296/*297* Class: sun_security_pkcs11_wrapper_PKCS11298* Method: C_DecryptInit299* Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;J)V300* Parametermapping: *PKCS11*301* @param jlong jSessionHandle CK_SESSION_HANDLE hSession302* @param jobject jMechanism CK_MECHANISM_PTR pMechanism303* @param jlong jKeyHandle CK_OBJECT_HANDLE hKey304*/305JNIEXPORT void JNICALL306Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptInit307(JNIEnv *env, jobject obj, jlong jSessionHandle,308jobject jMechanism, jlong jKeyHandle)309{310CK_SESSION_HANDLE ckSessionHandle;311CK_MECHANISM_PTR ckpMechanism = NULL;312CK_MECHANISM_PTR ckpTemp;313CK_OBJECT_HANDLE ckKeyHandle;314CK_RV rv;315316CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);317if (ckpFunctions == NULL) { return; }318319ckSessionHandle = jLongToCKULong(jSessionHandle);320ckKeyHandle = jLongToCKULong(jKeyHandle);321ckpMechanism = jMechanismToCKMechanismPtr(env, jMechanism);322TRACE1("DEBUG C_DecryptInit: created pMech = %p\n",323ckpMechanism);324325if ((*env)->ExceptionCheck(env)) { return; }326327rv = (*ckpFunctions->C_DecryptInit)(ckSessionHandle, ckpMechanism,328ckKeyHandle);329330if (ckpMechanism->mechanism == CKM_AES_GCM) {331if (rv == CKR_ARGUMENTS_BAD || rv == CKR_MECHANISM_PARAM_INVALID) {332// retry with CKM_GCM_PARAMS structure in pkcs11t.h333TRACE0("DEBUG C_DecryptInit: retry with CK_GCM_PARAMS\n");334ckpTemp = updateGCMParams(env, ckpMechanism);335if (ckpTemp != NULL) { // only re-call if conversion succeeds336ckpMechanism = ckpTemp;337rv = (*ckpFunctions->C_DecryptInit)(ckSessionHandle, ckpMechanism,338ckKeyHandle);339}340}341}342343TRACE1("DEBUG C_DecryptInit: freed pMech = %p\n", ckpMechanism);344freeCKMechanismPtr(ckpMechanism);345if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }346347TRACE0("FINISHED\n");348}349#endif350351#ifdef P11_ENABLE_C_DECRYPT352/*353* Class: sun_security_pkcs11_wrapper_PKCS11354* Method: C_Decrypt355* Signature: (JJ[BIIJ[BII)I356* Parametermapping: *PKCS11*357* @param jlong jSessionHandle CK_SESSION_HANDLE hSession358* @param jbyteArray jEncryptedData CK_BYTE_PTR pEncryptedData359* CK_ULONG ulEncryptedDataLen360* @return jbyteArray jData CK_BYTE_PTR pData361* CK_ULONG_PTR pulDataLen362*/363JNIEXPORT jint JNICALL364Java_sun_security_pkcs11_wrapper_PKCS11_C_1Decrypt365(JNIEnv *env, jobject obj, jlong jSessionHandle,366jlong directIn, jbyteArray jIn, jint jInOfs, jint jInLen,367jlong directOut, jbyteArray jOut, jint jOutOfs, jint jOutLen)368{369CK_SESSION_HANDLE ckSessionHandle;370CK_RV rv;371372CK_BYTE_PTR inBufP;373CK_BYTE_PTR outBufP;374CK_ULONG ckOutLen = 0;375376CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);377if (ckpFunctions == NULL) { return 0; }378379ckSessionHandle = jLongToCKULong(jSessionHandle);380381if (directIn != 0) {382inBufP = (CK_BYTE_PTR) jlong_to_ptr(directIn);383} else {384inBufP = (*env)->GetPrimitiveArrayCritical(env, jIn, NULL);385if (inBufP == NULL) { return 0; }386}387388if (directOut != 0) {389outBufP = (CK_BYTE_PTR) jlong_to_ptr(directOut);390} else {391outBufP = (*env)->GetPrimitiveArrayCritical(env, jOut, NULL);392if (outBufP == NULL) {393goto cleanup;394}395}396ckOutLen = jOutLen;397398rv = (*ckpFunctions->C_Decrypt)(ckSessionHandle,399(CK_BYTE_PTR)(inBufP + jInOfs), jInLen,400(CK_BYTE_PTR)(outBufP + jOutOfs),401&ckOutLen);402403ckAssertReturnValueOK(env, rv);404405cleanup:406if (directIn == 0 && inBufP != NULL) {407(*env)->ReleasePrimitiveArrayCritical(env, jIn, inBufP, JNI_ABORT);408}409if (directOut == 0 && outBufP != NULL) {410(*env)->ReleasePrimitiveArrayCritical(env, jOut, outBufP, 0);411}412return ckOutLen;413}414#endif415416#ifdef P11_ENABLE_C_DECRYPTUPDATE417/*418* Class: sun_security_pkcs11_wrapper_PKCS11419* Method: C_DecryptUpdate420* Signature: (J[BII[BII)I421* Parametermapping: *PKCS11*422* @param jlong jSessionHandle CK_SESSION_HANDLE hSession423* @param jbyteArray jEncryptedPart CK_BYTE_PTR pEncryptedPart424* CK_ULONG ulEncryptedPartLen425* @return jbyteArray jPart CK_BYTE_PTR pPart426* CK_ULONG_PTR pulPartLen427*/428JNIEXPORT jint JNICALL429Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptUpdate430(JNIEnv *env, jobject obj, jlong jSessionHandle,431jlong directIn, jbyteArray jIn, jint jInOfs, jint jInLen,432jlong directOut, jbyteArray jOut, jint jOutOfs, jint jOutLen)433{434CK_SESSION_HANDLE ckSessionHandle;435CK_RV rv;436437CK_BYTE_PTR inBufP;438CK_BYTE_PTR outBufP;439CK_ULONG ckDecryptedPartLen = 0;440441CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);442if (ckpFunctions == NULL) { return 0; }443444ckSessionHandle = jLongToCKULong(jSessionHandle);445446if (directIn != 0) {447inBufP = (CK_BYTE_PTR) jlong_to_ptr(directIn);448} else {449inBufP = (*env)->GetPrimitiveArrayCritical(env, jIn, NULL);450if (inBufP == NULL) { return 0; }451}452453if (directOut != 0) {454outBufP = (CK_BYTE_PTR) jlong_to_ptr(directOut);455} else {456outBufP = (*env)->GetPrimitiveArrayCritical(env, jOut, NULL);457if (outBufP == NULL) {458goto cleanup;459}460}461462ckDecryptedPartLen = jOutLen;463rv = (*ckpFunctions->C_DecryptUpdate)(ckSessionHandle,464(CK_BYTE_PTR)(inBufP + jInOfs), jInLen,465(CK_BYTE_PTR)(outBufP + jOutOfs),466&ckDecryptedPartLen);467ckAssertReturnValueOK(env, rv);468469cleanup:470if (directIn == 0 && inBufP != NULL) {471(*env)->ReleasePrimitiveArrayCritical(env, jIn, inBufP, JNI_ABORT);472}473if (directOut == 0 && outBufP != NULL) {474(*env)->ReleasePrimitiveArrayCritical(env, jOut, outBufP, 0);475}476return ckDecryptedPartLen;477}478479#endif480481#ifdef P11_ENABLE_C_DECRYPTFINAL482/*483* Class: sun_security_pkcs11_wrapper_PKCS11484* Method: C_DecryptFinal485* Signature: (J[BII)I486* Parametermapping: *PKCS11*487* @param jlong jSessionHandle CK_SESSION_HANDLE hSession488* @return jbyteArray jLastPart CK_BYTE_PTR pLastPart489* CK_ULONG_PTR pulLastPartLen490*/491JNIEXPORT jint JNICALL492Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptFinal493(JNIEnv *env, jobject obj, jlong jSessionHandle,494jlong directOut, jbyteArray jOut, jint jOutOfs, jint jOutLen)495{496CK_SESSION_HANDLE ckSessionHandle;497CK_RV rv;498CK_BYTE_PTR outBufP;499CK_ULONG ckLastPartLen;500501CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);502if (ckpFunctions == NULL) { return 0; }503504ckSessionHandle = jLongToCKULong(jSessionHandle);505506if (directOut != 0) {507outBufP = (CK_BYTE_PTR) jlong_to_ptr(directOut);508} else {509outBufP = (*env)->GetPrimitiveArrayCritical(env, jOut, NULL);510if (outBufP == NULL) { return 0; }511}512513ckLastPartLen = jOutLen;514515rv = (*ckpFunctions->C_DecryptFinal)(ckSessionHandle,516(CK_BYTE_PTR)(outBufP + jOutOfs),517&ckLastPartLen);518519if (directOut == 0) {520(*env)->ReleasePrimitiveArrayCritical(env, jOut, outBufP, 0);521522}523524ckAssertReturnValueOK(env, rv);525526return ckLastPartLen;527}528#endif529530531