jdk
2254 строки · 81.0 Кб
1/*
2* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
3*/
4
5/* Copyright (c) 2002 Graz University of Technology. All rights reserved.
6*
7* Redistribution and use in source and binary forms, with or without
8* modification, are permitted provided that the following conditions are met:
9*
10* 1. Redistributions of source code must retain the above copyright notice,
11* this list of conditions and the following disclaimer.
12*
13* 2. Redistributions in binary form must reproduce the above copyright notice,
14* this list of conditions and the following disclaimer in the documentation
15* and/or other materials provided with the distribution.
16*
17* 3. The end-user documentation included with the redistribution, if any, must
18* include the following acknowledgment:
19*
20* "This product includes software developed by IAIK of Graz University of
21* Technology."
22*
23* Alternately, this acknowledgment may appear in the software itself, if
24* and wherever such third-party acknowledgments normally appear.
25*
26* 4. The names "Graz University of Technology" and "IAIK of Graz University of
27* Technology" must not be used to endorse or promote products derived from
28* this software without prior written permission.
29*
30* 5. Products derived from this software may not be called
31* "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior
32* written permission of Graz University of Technology.
33*
34* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
35* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
36* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE
38* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
41* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
42* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45* POSSIBILITY OF SUCH DAMAGE.
46*/
47
48/*
49* pkcs11wrapper.c
50* 18.05.2001
51*
52* This is the implementation of the native functions of the Java to PKCS#11 interface.
53* All function use some helper functions to convert the JNI types to PKCS#11 types.
54*
55* @author Karl Scheibelhofer <Karl.Scheibelhofer@iaik.at>
56* @author Martin Schlaeffer <schlaeff@sbox.tugraz.at>
57*/
58
59
60#include "pkcs11wrapper.h"61
62#include <stdio.h>63#include <stdlib.h>64#include <string.h>65#include <assert.h>66
67#include "sun_security_pkcs11_wrapper_PKCS11.h"68
69/* declare file private functions */
70
71CK_VOID_PTR jMechParamToCKMechParamPtrSlow(JNIEnv *env, jobject jParam,72CK_MECHANISM_TYPE ckMech, CK_ULONG *ckpLength);73
74
75/*
76* converts a CK_DATE pointer into a Java CK_DATE Object.
77*
78* @param env - used to call JNI funktions to create the new Java object
79* @param ckpValue - the pointer to the CK_DATE structure
80* @return - the new Java CK_DATE object
81*/
82jobject ckDatePtrToJDateObject(JNIEnv *env, const CK_DATE *ckpDate)83{
84jclass jDateClass;85jmethodID jCtrId;86jobject jDateObject;87jcharArray jYear;88jcharArray jMonth;89jcharArray jDay;90
91/* load CK_DATE class */92jDateClass = (*env)->FindClass(env, CLASS_DATE);93if (jDateClass == NULL) { return NULL; }94
95/* load CK_DATE constructor */96jCtrId = (*env)->GetMethodID(env, jDateClass, "<init>", "([C[C[C)V");97if (jCtrId == NULL) { return NULL; }98
99/* prep all fields */100jYear = ckCharArrayToJCharArray(env, (CK_CHAR_PTR)(ckpDate->year), 4);101if (jYear == NULL) { return NULL; }102jMonth = ckCharArrayToJCharArray(env, (CK_CHAR_PTR)(ckpDate->month), 2);103if (jMonth == NULL) { return NULL; }104jDay = ckCharArrayToJCharArray(env, (CK_CHAR_PTR)(ckpDate->day), 2);105if (jDay == NULL) { return NULL; }106
107/* create new CK_DATE object */108jDateObject =109(*env)->NewObject(env, jDateClass, jCtrId, jYear, jMonth, jDay);110if (jDateObject == NULL) { return NULL; }111
112/* free local references */113(*env)->DeleteLocalRef(env, jDateClass);114(*env)->DeleteLocalRef(env, jYear);115(*env)->DeleteLocalRef(env, jMonth);116(*env)->DeleteLocalRef(env, jDay);117
118return jDateObject ;119}
120
121/*
122* converts a CK_VERSION pointer into a Java CK_VERSION Object.
123*
124* @param env - used to call JNI funktions to create the new Java object
125* @param ckpVersion - the pointer to the CK_VERSION structure
126* @return the new Java CK_VERSION object
127*/
128jobject ckVersionPtrToJVersion(JNIEnv *env, const CK_VERSION_PTR ckpVersion)129{
130jclass jVersionClass;131jmethodID jCtrId;132jobject jVersionObject;133jint jMajor;134jint jMinor;135
136/* load CK_VERSION class */137jVersionClass = (*env)->FindClass(env, CLASS_VERSION);138if (jVersionClass == NULL) { return NULL; }139
140/* load CK_VERSION constructor */141jCtrId = (*env)->GetMethodID(env, jVersionClass, "<init>", "(II)V");142if (jCtrId == NULL) { return NULL; }143
144/* prep both fields */145jMajor = ckpVersion->major;146jMinor = ckpVersion->minor;147
148/* create new CK_VERSION object */149jVersionObject =150(*env)->NewObject(env, jVersionClass, jCtrId, jMajor, jMinor);151if (jVersionObject == NULL) { return NULL; }152
153/* free local references */154(*env)->DeleteLocalRef(env, jVersionClass);155
156return jVersionObject ;157}
158
159/*
160* converts a CK_SESSION_INFO pointer into a Java CK_SESSION_INFO Object.
161*
162* @param env - used to call JNI funktions to create the new Java object
163* @param ckpSessionInfo - the pointer to the CK_SESSION_INFO structure
164* @return the new Java CK_SESSION_INFO object
165*/
166jobject ckSessionInfoPtrToJSessionInfo(JNIEnv *env, const CK_SESSION_INFO_PTR ckpSessionInfo)167{
168jclass jSessionInfoClass;169jmethodID jCtrId;170jobject jSessionInfoObject;171jlong jSlotID;172jlong jState;173jlong jFlags;174jlong jDeviceError;175
176/* load CK_SESSION_INFO class */177jSessionInfoClass = (*env)->FindClass(env, CLASS_SESSION_INFO);178if (jSessionInfoClass == NULL) { return NULL; }179
180/* load CK_SESSION_INFO constructor */181jCtrId = (*env)->GetMethodID(env, jSessionInfoClass, "<init>", "(JJJJ)V");182if (jCtrId == NULL) { return NULL; }183
184/* prep all fields */185jSlotID = ckULongToJLong(ckpSessionInfo->slotID);186jState = ckULongToJLong(ckpSessionInfo->state);187jFlags = ckULongToJLong(ckpSessionInfo->flags);188jDeviceError = ckULongToJLong(ckpSessionInfo->ulDeviceError);189
190/* create new CK_SESSION_INFO object */191jSessionInfoObject =192(*env)->NewObject(env, jSessionInfoClass, jCtrId, jSlotID, jState,193jFlags, jDeviceError);194if (jSessionInfoObject == NULL) { return NULL; }195
196/* free local references */197(*env)->DeleteLocalRef(env, jSessionInfoClass);198
199return jSessionInfoObject ;200}
201
202/*
203* converts a CK_ATTRIBUTE pointer into a Java CK_ATTRIBUTE Object.
204*
205* @param env - used to call JNI funktions to create the new Java object
206* @param ckpAttribute - the pointer to the CK_ATTRIBUTE structure
207* @return the new Java CK_ATTRIBUTE object
208*/
209jobject ckAttributePtrToJAttribute(JNIEnv *env, const CK_ATTRIBUTE_PTR ckpAttribute)210{
211jclass jAttributeClass;212jmethodID jCtrId;213jobject jAttributeObject;214jlong jType;215jobject jPValue = NULL;216
217jAttributeClass = (*env)->FindClass(env, CLASS_ATTRIBUTE);218if (jAttributeClass == NULL) { return NULL; }219
220/* load CK_INFO constructor */221jCtrId = (*env)->GetMethodID(env, jAttributeClass, "<init>", "(JLjava/lang/Object;)V");222if (jCtrId == NULL) { return NULL; }223
224/* prep both fields */225jType = ckULongToJLong(ckpAttribute->type);226jPValue = ckAttributeValueToJObject(env, ckpAttribute);227if ((*env)->ExceptionCheck(env)) { return NULL; }228
229/* create new CK_ATTRIBUTE object */230jAttributeObject =231(*env)->NewObject(env, jAttributeClass, jCtrId, jType, jPValue);232if (jAttributeObject == NULL) { return NULL; }233
234/* free local references */235(*env)->DeleteLocalRef(env, jAttributeClass);236(*env)->DeleteLocalRef(env, jPValue);237
238return jAttributeObject;239}
240
241
242/*
243* converts a Java CK_VERSION object into a CK_VERSION pointer
244*
245* @param env - used to call JNI funktions to get the values out of the Java object
246* @param jVersion - the Java CK_VERSION object to convert
247* @return pointer to the new CK_VERSION structure
248*/
249CK_VERSION_PTR
250jVersionToCKVersionPtr(JNIEnv *env, jobject jVersion)251{
252CK_VERSION_PTR ckpVersion;253jclass jVersionClass;254jfieldID jFieldID;255jbyte jMajor, jMinor;256
257if (jVersion == NULL) {258return NULL;259}260
261// retrieve java values262jVersionClass = (*env)->GetObjectClass(env, jVersion);263if (jVersionClass == NULL) { return NULL; }264jFieldID = (*env)->GetFieldID(env, jVersionClass, "major", "B");265if (jFieldID == NULL) { return NULL; }266jMajor = (*env)->GetByteField(env, jVersion, jFieldID);267jFieldID = (*env)->GetFieldID(env, jVersionClass, "minor", "B");268if (jFieldID == NULL) { return NULL; }269jMinor = (*env)->GetByteField(env, jVersion, jFieldID);270
271// allocate memory for CK_VERSION pointer272ckpVersion = (CK_VERSION_PTR) calloc(1, sizeof(CK_VERSION));273if (ckpVersion == NULL) {274p11ThrowOutOfMemoryError(env, 0);275return NULL;276}277
278// populate using java values279ckpVersion->major = jByteToCKByte(jMajor);280ckpVersion->minor = jByteToCKByte(jMinor);281
282return ckpVersion;283}
284
285
286/*
287* converts a Java CK_DATE object into a CK_DATE pointer
288*
289* @param env - used to call JNI functions to get the values out of the Java object
290* @param jDate - the Java CK_DATE object to convert
291* @return pointer to the new CK_DATE structure
292*/
293CK_DATE * jDateObjectToCKDatePtr(JNIEnv *env, jobject jDate)294{
295CK_DATE * ckpDate = NULL;296CK_ULONG ckLength;297jclass jDateClass;298jfieldID jFieldID;299jobject jYear, jMonth, jDay;300jchar *jTempChars = NULL;301CK_ULONG i;302
303if (jDate == NULL) {304return NULL;305}306
307// retrieve java values308jDateClass = (*env)->FindClass(env, CLASS_DATE);309if (jDateClass == NULL) { return NULL; }310jFieldID = (*env)->GetFieldID(env, jDateClass, "year", "[C");311if (jFieldID == NULL) { return NULL; }312jYear = (*env)->GetObjectField(env, jDate, jFieldID);313jFieldID = (*env)->GetFieldID(env, jDateClass, "month", "[C");314if (jFieldID == NULL) { return NULL; }315jMonth = (*env)->GetObjectField(env, jDate, jFieldID);316jFieldID = (*env)->GetFieldID(env, jDateClass, "day", "[C");317if (jFieldID == NULL) { return NULL; }318jDay = (*env)->GetObjectField(env, jDate, jFieldID);319
320// allocate memory for CK_DATE pointer321ckpDate = (CK_DATE *) calloc(1, sizeof(CK_DATE));322if (ckpDate == NULL) {323p11ThrowOutOfMemoryError(env, 0);324return NULL;325}326
327// populate using java values328if (jYear == NULL) {329ckpDate->year[0] = 0;330ckpDate->year[1] = 0;331ckpDate->year[2] = 0;332ckpDate->year[3] = 0;333} else {334ckLength = (*env)->GetArrayLength(env, jYear);335jTempChars = (jchar*) calloc(ckLength, sizeof(jchar));336if (jTempChars == NULL) {337p11ThrowOutOfMemoryError(env, 0);338goto cleanup;339}340(*env)->GetCharArrayRegion(env, jYear, 0, ckLength, jTempChars);341if ((*env)->ExceptionCheck(env)) {342goto cleanup;343}344
345for (i = 0; (i < ckLength) && (i < 4) ; i++) {346ckpDate->year[i] = jCharToCKChar(jTempChars[i]);347}348free(jTempChars);349}350
351if (jMonth == NULL) {352ckpDate->month[0] = 0;353ckpDate->month[1] = 0;354} else {355ckLength = (*env)->GetArrayLength(env, jMonth);356jTempChars = (jchar*) calloc(ckLength, sizeof(jchar));357if (jTempChars == NULL) {358p11ThrowOutOfMemoryError(env, 0);359goto cleanup;360}361(*env)->GetCharArrayRegion(env, jMonth, 0, ckLength, jTempChars);362if ((*env)->ExceptionCheck(env)) {363goto cleanup;364}365
366for (i = 0; (i < ckLength) && (i < 2) ; i++) {367ckpDate->month[i] = jCharToCKChar(jTempChars[i]);368}369free(jTempChars);370}371
372if (jDay == NULL) {373ckpDate->day[0] = 0;374ckpDate->day[1] = 0;375} else {376ckLength = (*env)->GetArrayLength(env, jDay);377jTempChars = (jchar*) calloc(ckLength, sizeof(jchar));378if (jTempChars == NULL) {379p11ThrowOutOfMemoryError(env, 0);380goto cleanup;381}382(*env)->GetCharArrayRegion(env, jDay, 0, ckLength, jTempChars);383if ((*env)->ExceptionCheck(env)) {384goto cleanup;385}386
387for (i = 0; (i < ckLength) && (i < 2) ; i++) {388ckpDate->day[i] = jCharToCKChar(jTempChars[i]);389}390free(jTempChars);391}392
393return ckpDate;394cleanup:395free(jTempChars);396free(ckpDate);397return NULL;398}
399
400
401/*
402* converts a Java CK_ATTRIBUTE object into a CK_ATTRIBUTE structure
403*
404* @param env - used to call JNI funktions to get the values out of the Java object
405* @param jAttribute - the Java CK_ATTRIBUTE object to convert
406* @return the new CK_ATTRIBUTE structure
407*/
408CK_ATTRIBUTE jAttributeToCKAttribute(JNIEnv *env, jobject jAttribute)409{
410CK_ATTRIBUTE ckAttribute;411jclass jAttributeClass;412jfieldID jFieldID;413jlong jType;414jobject jPValue;415
416memset(&ckAttribute, 0, sizeof(CK_ATTRIBUTE));417
418// TBD: what if jAttribute == NULL?!419TRACE0("\nDEBUG: jAttributeToCKAttribute");420
421/* get CK_ATTRIBUTE class */422TRACE0(", getting attribute object class");423jAttributeClass = (*env)->GetObjectClass(env, jAttribute);424if (jAttributeClass == NULL) { return ckAttribute; }425
426/* get type */427TRACE0(", getting type field");428jFieldID = (*env)->GetFieldID(env, jAttributeClass, "type", "J");429if (jFieldID == NULL) { return ckAttribute; }430jType = (*env)->GetLongField(env, jAttribute, jFieldID);431TRACE1(", type=0x%lX", jType);432
433/* get pValue */434TRACE0(", getting pValue field");435jFieldID = (*env)->GetFieldID(env, jAttributeClass, "pValue", "Ljava/lang/Object;");436if (jFieldID == NULL) { return ckAttribute; }437jPValue = (*env)->GetObjectField(env, jAttribute, jFieldID);438TRACE1(", pValue=%p", jPValue);439
440ckAttribute.type = jLongToCKULong(jType);441TRACE0(", converting pValue to primitive object");442
443/* convert the Java pValue object to a CK-type pValue pointer */444ckAttribute.pValue = jObjectToPrimitiveCKObjectPtr(env, jPValue, &(ckAttribute.ulValueLen));445
446TRACE0("\nDEBUG: jAttributeToCKAttribute FINISHED\n");447
448return ckAttribute ;449}
450
451void masterKeyDeriveParamToCKMasterKeyDeriveParam(JNIEnv *env, jobject jParam,452jclass masterKeyDeriveParamClass,453CK_VERSION_PTR* cKMasterKeyDeriveParamVersion,454CK_SSL3_RANDOM_DATA* cKMasterKeyDeriveParamRandomInfo) {455jfieldID fieldID;456jclass jSsl3RandomDataClass;457jobject jRandomInfo, jRIClientRandom, jRIServerRandom, jVersion;458
459// retrieve java values460fieldID = (*env)->GetFieldID(env, masterKeyDeriveParamClass, "RandomInfo",461"Lsun/security/pkcs11/wrapper/CK_SSL3_RANDOM_DATA;");462if (fieldID == NULL) { return; }463jRandomInfo = (*env)->GetObjectField(env, jParam, fieldID);464jSsl3RandomDataClass = (*env)->FindClass(env, CLASS_SSL3_RANDOM_DATA);465if (jSsl3RandomDataClass == NULL) { return; }466fieldID = (*env)->GetFieldID(env, jSsl3RandomDataClass, "pClientRandom", "[B");467if (fieldID == NULL) { return; }468jRIClientRandom = (*env)->GetObjectField(env, jRandomInfo, fieldID);469fieldID = (*env)->GetFieldID(env, jSsl3RandomDataClass, "pServerRandom", "[B");470if (fieldID == NULL) { return; }471jRIServerRandom = (*env)->GetObjectField(env, jRandomInfo, fieldID);472fieldID = (*env)->GetFieldID(env, masterKeyDeriveParamClass, "pVersion",473"Lsun/security/pkcs11/wrapper/CK_VERSION;");474if (fieldID == NULL) { return; }475jVersion = (*env)->GetObjectField(env, jParam, fieldID);476
477// populate using java values478*cKMasterKeyDeriveParamVersion = jVersionToCKVersionPtr(env, jVersion);479if ((*env)->ExceptionCheck(env)) { return; }480jByteArrayToCKByteArray(env, jRIClientRandom,481&(cKMasterKeyDeriveParamRandomInfo->pClientRandom),482&(cKMasterKeyDeriveParamRandomInfo->ulClientRandomLen));483if ((*env)->ExceptionCheck(env)) {484goto cleanup;485}486jByteArrayToCKByteArray(env, jRIServerRandom,487&(cKMasterKeyDeriveParamRandomInfo->pServerRandom),488&(cKMasterKeyDeriveParamRandomInfo->ulServerRandomLen));489if ((*env)->ExceptionCheck(env)) {490goto cleanup;491}492return;493cleanup:494free(*cKMasterKeyDeriveParamVersion);495free(cKMasterKeyDeriveParamRandomInfo->pClientRandom);496cKMasterKeyDeriveParamRandomInfo->ulClientRandomLen = 0L;497free(cKMasterKeyDeriveParamRandomInfo->pServerRandom);498cKMasterKeyDeriveParamRandomInfo->ulServerRandomLen = 0L;499// explicitly set to NULL to ensure no double free possible500*cKMasterKeyDeriveParamVersion = NULL;501cKMasterKeyDeriveParamRandomInfo->pClientRandom = NULL;502cKMasterKeyDeriveParamRandomInfo->pServerRandom = NULL;503}
504
505/*
506* converts the Java CK_SSL3_MASTER_KEY_DERIVE_PARAMS object to a
507* CK_SSL3_MASTER_KEY_DERIVE_PARAMS pointer
508*
509* @param env - used to call JNI functions to get the Java classes and objects
510* @param jParam - the Java CK_SSL3_MASTER_KEY_DERIVE_PARAMS object to convert
511* @param pLength - length of the allocated memory of the returned pointer
512* @return pointer to the new CK_SSL3_MASTER_KEY_DERIVE_PARAMS structure
513*/
514CK_SSL3_MASTER_KEY_DERIVE_PARAMS_PTR
515jSsl3MasterKeyDeriveParamToCKSsl3MasterKeyDeriveParamPtr(JNIEnv *env,516jobject jParam, CK_ULONG *pLength)517{
518CK_SSL3_MASTER_KEY_DERIVE_PARAMS_PTR ckParamPtr;519jclass jSsl3MasterKeyDeriveParamsClass;520
521if (pLength != NULL) {522*pLength = 0L;523}524
525// allocate memory for CK_SSL3_MASTER_KEY_DERIVE_PARAMS pointer526ckParamPtr = calloc(1, sizeof(CK_SSL3_MASTER_KEY_DERIVE_PARAMS));527if (ckParamPtr == NULL) {528p11ThrowOutOfMemoryError(env, 0);529return NULL;530}531
532// retrieve and populate using java values533jSsl3MasterKeyDeriveParamsClass =534(*env)->FindClass(env, CLASS_SSL3_MASTER_KEY_DERIVE_PARAMS);535if (jSsl3MasterKeyDeriveParamsClass == NULL) {536goto cleanup;537}538masterKeyDeriveParamToCKMasterKeyDeriveParam(env, jParam,539jSsl3MasterKeyDeriveParamsClass,540&(ckParamPtr->pVersion), &(ckParamPtr->RandomInfo));541if ((*env)->ExceptionCheck(env)) {542goto cleanup;543}544
545if (pLength != NULL) {546*pLength = sizeof(CK_SSL3_MASTER_KEY_DERIVE_PARAMS);547}548return ckParamPtr;549cleanup:550free(ckParamPtr);551return NULL;552}
553
554/*
555* converts the Java CK_TLS12_MASTER_KEY_DERIVE_PARAMS object to a
556* CK_TLS12_MASTER_KEY_DERIVE_PARAMS pointer
557*
558* @param env - used to call JNI functions to get the Java classes and objects
559* @param jParam - the Java CK_TLS12_MASTER_KEY_DERIVE_PARAMS object to convert
560* @param pLength - length of the allocated memory of the returned pointer
561* @return pointer to the new CK_TLS12_MASTER_KEY_DERIVE_PARAMS structure
562*/
563CK_TLS12_MASTER_KEY_DERIVE_PARAMS_PTR
564jTls12MasterKeyDeriveParamToCKTls12MasterKeyDeriveParamPtr(JNIEnv *env,565jobject jParam, CK_ULONG *pLength)566{
567CK_TLS12_MASTER_KEY_DERIVE_PARAMS_PTR ckParamPtr;568jclass jTls12MasterKeyDeriveParamsClass;569jfieldID fieldID;570jlong prfHashMechanism;571
572if (pLength != NULL) {573*pLength = 0L;574}575
576// retrieve java values577jTls12MasterKeyDeriveParamsClass =578(*env)->FindClass(env, CLASS_TLS12_MASTER_KEY_DERIVE_PARAMS);579if (jTls12MasterKeyDeriveParamsClass == NULL) { return NULL; }580fieldID = (*env)->GetFieldID(env,581jTls12MasterKeyDeriveParamsClass, "prfHashMechanism", "J");582if (fieldID == NULL) { return NULL; }583prfHashMechanism = (*env)->GetLongField(env, jParam, fieldID);584
585// allocate memory for CK_TLS12_MASTER_KEY_DERIVE_PARAMS pointer586ckParamPtr = calloc(1, sizeof(CK_TLS12_MASTER_KEY_DERIVE_PARAMS));587if (ckParamPtr == NULL) {588p11ThrowOutOfMemoryError(env, 0);589return NULL;590}591
592// populate using java values593masterKeyDeriveParamToCKMasterKeyDeriveParam(env, jParam,594jTls12MasterKeyDeriveParamsClass, &ckParamPtr->pVersion,595&ckParamPtr->RandomInfo);596if ((*env)->ExceptionCheck(env)) {597goto cleanup;598}599
600ckParamPtr->prfHashMechanism = (CK_MECHANISM_TYPE) prfHashMechanism;601
602if (pLength != NULL) {603*pLength = sizeof(CK_TLS12_MASTER_KEY_DERIVE_PARAMS);604}605return ckParamPtr;606cleanup:607free(ckParamPtr);608return NULL;609}
610
611/*
612* converts the Java CK_TLS_PRF_PARAMS object to a CK_TLS_PRF_PARAMS pointer
613*
614* @param env - used to call JNI functions to get the Java classes and objects
615* @param jParam - the Java CK_TLS_PRF_PARAMS object to convert
616* @param pLength - length of the allocated memory of the returned pointer
617* @return pointer to the new CK_TLS_PRF_PARAMS structure
618*/
619CK_TLS_PRF_PARAMS_PTR
620jTlsPrfParamsToCKTlsPrfParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)621{
622CK_TLS_PRF_PARAMS_PTR ckParamPtr;623jclass jTlsPrfParamsClass;624jfieldID fieldID;625jobject jSeed, jLabel, jOutput;626
627if (pLength != NULL) {628*pLength = 0;629}630
631// retrieve java values632jTlsPrfParamsClass = (*env)->FindClass(env, CLASS_TLS_PRF_PARAMS);633if (jTlsPrfParamsClass == NULL) { return NULL; }634fieldID = (*env)->GetFieldID(env, jTlsPrfParamsClass, "pSeed", "[B");635if (fieldID == NULL) { return NULL; }636jSeed = (*env)->GetObjectField(env, jParam, fieldID);637fieldID = (*env)->GetFieldID(env, jTlsPrfParamsClass, "pLabel", "[B");638if (fieldID == NULL) { return NULL; }639jLabel = (*env)->GetObjectField(env, jParam, fieldID);640fieldID = (*env)->GetFieldID(env, jTlsPrfParamsClass, "pOutput", "[B");641if (fieldID == NULL) { return NULL; }642jOutput = (*env)->GetObjectField(env, jParam, fieldID);643
644// allocate memory for CK_TLS_PRF_PARAMS pointer645ckParamPtr = calloc(1, sizeof(CK_TLS_PRF_PARAMS));646if (ckParamPtr == NULL) {647p11ThrowOutOfMemoryError(env, 0);648return NULL;649}650
651// populate using java values652jByteArrayToCKByteArray(env, jSeed, &(ckParamPtr->pSeed), &(ckParamPtr->ulSeedLen));653if ((*env)->ExceptionCheck(env)) {654goto cleanup;655}656jByteArrayToCKByteArray(env, jLabel, &(ckParamPtr->pLabel), &(ckParamPtr->ulLabelLen));657if ((*env)->ExceptionCheck(env)) {658goto cleanup;659}660ckParamPtr->pulOutputLen = calloc(1, sizeof(CK_ULONG));661if (ckParamPtr->pulOutputLen == NULL) {662goto cleanup;663}664jByteArrayToCKByteArray(env, jOutput, &(ckParamPtr->pOutput), ckParamPtr->pulOutputLen);665if ((*env)->ExceptionCheck(env)) {666goto cleanup;667}668
669if (pLength != NULL) {670*pLength = sizeof(CK_TLS_PRF_PARAMS);671}672return ckParamPtr;673cleanup:674free(ckParamPtr->pSeed);675free(ckParamPtr->pLabel);676free(ckParamPtr->pOutput);677free(ckParamPtr->pulOutputLen);678free(ckParamPtr);679return NULL;680}
681
682/*
683* converts the Java CK_TLS_MAC_PARAMS object to a CK_TLS_MAC_PARAMS pointer
684*
685* @param env - used to call JNI functions to get the Java classes and objects
686* @param jParam - the Java CK_TLS_MAC_PARAMS object to convert
687* @param pLength - length of the allocated memory of the returned pointer
688* @return pointer to the new CK_TLS_MAC_PARAMS structure
689*/
690
691CK_TLS_MAC_PARAMS_PTR
692jTlsMacParamsToCKTlsMacParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)693{
694CK_TLS_MAC_PARAMS_PTR ckParamPtr;695jclass jTlsMacParamsClass;696jfieldID fieldID;697jlong jPrfMechanism, jUlMacLength, jUlServerOrClient;698
699if (pLength != NULL) {700*pLength = 0L;701}702
703// retrieve java values704jTlsMacParamsClass = (*env)->FindClass(env, CLASS_TLS_MAC_PARAMS);705if (jTlsMacParamsClass == NULL) { return NULL; }706fieldID = (*env)->GetFieldID(env, jTlsMacParamsClass, "prfMechanism", "J");707if (fieldID == NULL) { return NULL; }708jPrfMechanism = (*env)->GetLongField(env, jParam, fieldID);709fieldID = (*env)->GetFieldID(env, jTlsMacParamsClass, "ulMacLength", "J");710if (fieldID == NULL) { return NULL; }711jUlMacLength = (*env)->GetLongField(env, jParam, fieldID);712fieldID = (*env)->GetFieldID(env, jTlsMacParamsClass, "ulServerOrClient", "J");713if (fieldID == NULL) { return NULL; }714jUlServerOrClient = (*env)->GetLongField(env, jParam, fieldID);715
716// allocate memory for CK_TLS_MAC_PARAMS pointer717ckParamPtr = calloc(1, sizeof(CK_TLS_MAC_PARAMS));718if (ckParamPtr == NULL) {719p11ThrowOutOfMemoryError(env, 0);720return NULL;721}722
723// populate using java values724ckParamPtr->prfHashMechanism = jLongToCKULong(jPrfMechanism);725ckParamPtr->ulMacLength = jLongToCKULong(jUlMacLength);726ckParamPtr->ulServerOrClient = jLongToCKULong(jUlServerOrClient);727
728if (pLength != NULL) {729*pLength = sizeof(CK_TLS_MAC_PARAMS);730}731return ckParamPtr;732}
733
734void keyMatParamToCKKeyMatParam(JNIEnv *env, jobject jParam,735jclass jKeyMatParamClass,736CK_ULONG* cKKeyMatParamUlMacSizeInBits,737CK_ULONG* cKKeyMatParamUlKeySizeInBits,738CK_ULONG* cKKeyMatParamUlIVSizeInBits,739CK_BBOOL* cKKeyMatParamBIsExport,740CK_SSL3_RANDOM_DATA* cKKeyMatParamRandomInfo,741CK_SSL3_KEY_MAT_OUT_PTR* cKKeyMatParamPReturnedKeyMaterial)742{
743jclass jSsl3RandomDataClass, jSsl3KeyMatOutClass;744jfieldID fieldID;745jlong jMacSizeInBits, jKeySizeInBits, jIVSizeInBits;746jboolean jIsExport;747jobject jRandomInfo, jRIClientRandom, jRIServerRandom;748jobject jReturnedKeyMaterial, jRMIvClient, jRMIvServer;749CK_ULONG ckTemp;750
751// the pointer arguments should already be initialized by caller752
753// retrieve java values754fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "ulMacSizeInBits", "J");755if (fieldID == NULL) { return; }756jMacSizeInBits = (*env)->GetLongField(env, jParam, fieldID);757fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "ulKeySizeInBits", "J");758if (fieldID == NULL) { return; }759jKeySizeInBits = (*env)->GetLongField(env, jParam, fieldID);760fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "ulIVSizeInBits", "J");761if (fieldID == NULL) { return; }762jIVSizeInBits = (*env)->GetLongField(env, jParam, fieldID);763fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "bIsExport", "Z");764if (fieldID == NULL) { return; }765jIsExport = (*env)->GetBooleanField(env, jParam, fieldID);766jSsl3RandomDataClass = (*env)->FindClass(env, CLASS_SSL3_RANDOM_DATA);767if (jSsl3RandomDataClass == NULL) { return; }768fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "RandomInfo",769"Lsun/security/pkcs11/wrapper/CK_SSL3_RANDOM_DATA;");770if (fieldID == NULL) { return; }771jRandomInfo = (*env)->GetObjectField(env, jParam, fieldID);772fieldID = (*env)->GetFieldID(env, jSsl3RandomDataClass, "pClientRandom", "[B");773if (fieldID == NULL) { return; }774jRIClientRandom = (*env)->GetObjectField(env, jRandomInfo, fieldID);775fieldID = (*env)->GetFieldID(env, jSsl3RandomDataClass, "pServerRandom", "[B");776if (fieldID == NULL) { return; }777jRIServerRandom = (*env)->GetObjectField(env, jRandomInfo, fieldID);778jSsl3KeyMatOutClass = (*env)->FindClass(env, CLASS_SSL3_KEY_MAT_OUT);779if (jSsl3KeyMatOutClass == NULL) { return; }780fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "pReturnedKeyMaterial",781"Lsun/security/pkcs11/wrapper/CK_SSL3_KEY_MAT_OUT;");782if (fieldID == NULL) { return; }783jReturnedKeyMaterial = (*env)->GetObjectField(env, jParam, fieldID);784fieldID = (*env)->GetFieldID(env, jSsl3KeyMatOutClass, "pIVClient", "[B");785if (fieldID == NULL) { return; }786jRMIvClient = (*env)->GetObjectField(env, jReturnedKeyMaterial, fieldID);787fieldID = (*env)->GetFieldID(env, jSsl3KeyMatOutClass, "pIVServer", "[B");788if (fieldID == NULL) { return; }789jRMIvServer = (*env)->GetObjectField(env, jReturnedKeyMaterial, fieldID);790
791// populate the specified pointers using java values792*cKKeyMatParamUlMacSizeInBits = jLongToCKULong(jMacSizeInBits);793*cKKeyMatParamUlKeySizeInBits = jLongToCKULong(jKeySizeInBits);794*cKKeyMatParamUlIVSizeInBits = jLongToCKULong(jIVSizeInBits);795*cKKeyMatParamBIsExport = jBooleanToCKBBool(jIsExport);796jByteArrayToCKByteArray(env, jRIClientRandom,797&(cKKeyMatParamRandomInfo->pClientRandom),798&(cKKeyMatParamRandomInfo->ulClientRandomLen));799if ((*env)->ExceptionCheck(env)) {800// just return as no memory allocation yet801return;802}803jByteArrayToCKByteArray(env, jRIServerRandom,804&(cKKeyMatParamRandomInfo->pServerRandom),805&(cKKeyMatParamRandomInfo->ulServerRandomLen));806if ((*env)->ExceptionCheck(env)) {807goto cleanup;808}809/* allocate memory for pReturnedKeyMaterial */810*cKKeyMatParamPReturnedKeyMaterial =811(CK_SSL3_KEY_MAT_OUT_PTR) calloc(1, sizeof(CK_SSL3_KEY_MAT_OUT));812if (*cKKeyMatParamPReturnedKeyMaterial == NULL) {813p11ThrowOutOfMemoryError(env, 0);814goto cleanup;815}816
817// the handles are output params only, no need to fetch them from Java818(*cKKeyMatParamPReturnedKeyMaterial)->hClientMacSecret = 0;819(*cKKeyMatParamPReturnedKeyMaterial)->hServerMacSecret = 0;820(*cKKeyMatParamPReturnedKeyMaterial)->hClientKey = 0;821(*cKKeyMatParamPReturnedKeyMaterial)->hServerKey = 0;822
823jByteArrayToCKByteArray(env, jRMIvClient,824&((*cKKeyMatParamPReturnedKeyMaterial)->pIVClient), &ckTemp);825if ((*env)->ExceptionCheck(env)) {826goto cleanup;827}828jByteArrayToCKByteArray(env, jRMIvServer,829&((*cKKeyMatParamPReturnedKeyMaterial)->pIVServer), &ckTemp);830if ((*env)->ExceptionCheck(env)) {831goto cleanup;832}833
834return;835cleanup:836free(cKKeyMatParamRandomInfo->pClientRandom);837free(cKKeyMatParamRandomInfo->pServerRandom);838if ((*cKKeyMatParamPReturnedKeyMaterial) != NULL) {839free((*cKKeyMatParamPReturnedKeyMaterial)->pIVClient);840free(*cKKeyMatParamPReturnedKeyMaterial);841}842// explicitly set to NULL to ensure no double free possible843cKKeyMatParamRandomInfo->pClientRandom = NULL;844cKKeyMatParamRandomInfo->pServerRandom = NULL;845*cKKeyMatParamPReturnedKeyMaterial = NULL;846return;847}
848/*
849* converts the Java CK_SSL3_KEY_MAT_PARAMS object to a
850* CK_SSL3_KEY_MAT_PARAMS pointer
851*
852* @param env - used to call JNI funktions to get the Java classes and objects
853* @param jParam - the Java CK_SSL3_KEY_MAT_PARAMS object to convert
854* @param pLength - length of the allocated memory of the returned pointer
855* @return pointer to the new CK_SSL3_KEY_MAT_PARAMS structure
856*/
857CK_SSL3_KEY_MAT_PARAMS_PTR
858jSsl3KeyMatParamToCKSsl3KeyMatParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)859{
860CK_SSL3_KEY_MAT_PARAMS_PTR ckParamPtr;861jclass jSsl3KeyMatParamsClass;862
863if (pLength != NULL) {864*pLength = 0;865}866
867// allocate memory for CK_SSL3_KEY_MAT_PARAMS pointer868ckParamPtr = calloc(1, sizeof(CK_SSL3_KEY_MAT_PARAMS));869if (ckParamPtr == NULL) {870p11ThrowOutOfMemoryError(env, 0);871return NULL;872}873
874// retrieve and populate using java values875jSsl3KeyMatParamsClass = (*env)->FindClass(env,876CLASS_SSL3_KEY_MAT_PARAMS);877if (jSsl3KeyMatParamsClass == NULL) {878goto cleanup;879}880keyMatParamToCKKeyMatParam(env, jParam, jSsl3KeyMatParamsClass,881&(ckParamPtr->ulMacSizeInBits), &(ckParamPtr->ulKeySizeInBits),882&(ckParamPtr->ulIVSizeInBits), &(ckParamPtr->bIsExport),883&(ckParamPtr->RandomInfo), &(ckParamPtr->pReturnedKeyMaterial));884if ((*env)->ExceptionCheck(env)) {885goto cleanup;886}887
888if (pLength != NULL) {889*pLength = sizeof(CK_SSL3_KEY_MAT_PARAMS);890}891return ckParamPtr;892cleanup:893free(ckParamPtr);894return NULL;895}
896
897/*
898* converts the Java CK_TLS12_KEY_MAT_PARAMS object to a
899* CK_TLS12_KEY_MAT_PARAMS pointer
900*
901* @param env - used to call JNI functions to get the Java classes and objects
902* @param jParam - the Java CK_TLS12_KEY_MAT_PARAMS object to convert
903* @param pLength - length of the allocated memory of the returned pointer
904* @return pointer to the new CK_TLS12_KEY_MAT_PARAMS structure
905*/
906CK_TLS12_KEY_MAT_PARAMS_PTR jTls12KeyMatParamToCKTls12KeyMatParamPtr(JNIEnv *env,907jobject jParam, CK_ULONG *pLength)908{
909CK_TLS12_KEY_MAT_PARAMS_PTR ckParamPtr;910jclass jTls12KeyMatParamsClass;911jfieldID fieldID;912jlong prfHashMechanism;913
914if (pLength != NULL) {915*pLength = 0;916}917
918// retrieve java values919jTls12KeyMatParamsClass = (*env)->FindClass(env,920CLASS_TLS12_KEY_MAT_PARAMS);921if (jTls12KeyMatParamsClass == NULL) { return NULL; }922fieldID = (*env)->GetFieldID(env, jTls12KeyMatParamsClass,923"prfHashMechanism", "J");924if (fieldID == NULL) { return NULL; }925prfHashMechanism = (*env)->GetLongField(env, jParam, fieldID);926
927// allocate memory for CK_TLS12_KEY_MAT_PARAMS pointer928ckParamPtr = calloc(1, sizeof(CK_TLS12_KEY_MAT_PARAMS));929if (ckParamPtr == NULL) {930p11ThrowOutOfMemoryError(env, 0);931return NULL;932}933
934// populate using java values935keyMatParamToCKKeyMatParam(env, jParam, jTls12KeyMatParamsClass,936&(ckParamPtr->ulMacSizeInBits), &(ckParamPtr->ulKeySizeInBits),937&(ckParamPtr->ulIVSizeInBits), &(ckParamPtr->bIsExport),938&(ckParamPtr->RandomInfo), &(ckParamPtr->pReturnedKeyMaterial));939if ((*env)->ExceptionCheck(env)) {940goto cleanup;941}942ckParamPtr->prfHashMechanism = (CK_MECHANISM_TYPE)prfHashMechanism;943
944if (pLength != NULL) {945*pLength = sizeof(CK_TLS12_KEY_MAT_PARAMS);946}947return ckParamPtr;948cleanup:949free(ckParamPtr);950return NULL;951}
952
953/*
954* converts the Java CK_AES_CTR_PARAMS object to a CK_AES_CTR_PARAMS pointer
955*
956* @param env - used to call JNI funktions to get the Java classes and objects
957* @param jParam - the Java CK_AES_CTR_PARAMS object to convert
958* @param pLength - length of the allocated memory of the returned pointer
959* @return pointer to the new CK_AES_CTR_PARAMS structure
960*/
961CK_AES_CTR_PARAMS_PTR
962jAesCtrParamsToCKAesCtrParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)963{
964CK_AES_CTR_PARAMS_PTR ckParamPtr;965jclass jAesCtrParamsClass;966jfieldID fieldID;967jlong jCounterBits;968jobject jCb;969CK_BYTE_PTR ckBytes = NULL;970CK_ULONG ckTemp;971
972if (pLength != NULL) {973*pLength = 0L;974}975
976// retrieve java values977jAesCtrParamsClass = (*env)->FindClass(env, CLASS_AES_CTR_PARAMS);978if (jAesCtrParamsClass == NULL) { return NULL; }979if (!(*env)->IsInstanceOf(env, jParam, jAesCtrParamsClass)) {980return NULL;981}982fieldID = (*env)->GetFieldID(env, jAesCtrParamsClass, "ulCounterBits", "J");983if (fieldID == NULL) { return NULL; }984jCounterBits = (*env)->GetLongField(env, jParam, fieldID);985fieldID = (*env)->GetFieldID(env, jAesCtrParamsClass, "cb", "[B");986if (fieldID == NULL) { return NULL; }987jCb = (*env)->GetObjectField(env, jParam, fieldID);988
989// allocate memory for CK_AES_CTR_PARAMS pointer990ckParamPtr = calloc(1, sizeof(CK_AES_CTR_PARAMS));991if (ckParamPtr == NULL) {992p11ThrowOutOfMemoryError(env, 0);993return NULL;994}995
996// populate using java values997jByteArrayToCKByteArray(env, jCb, &ckBytes, &ckTemp);998if ((*env)->ExceptionCheck(env) || ckTemp != 16) {999goto cleanup;1000}1001memcpy(ckParamPtr->cb, ckBytes, ckTemp);1002free(ckBytes);1003
1004ckParamPtr->ulCounterBits = jLongToCKULong(jCounterBits);1005
1006if (pLength != NULL) {1007*pLength = sizeof(CK_AES_CTR_PARAMS);1008}1009return ckParamPtr;1010cleanup:1011free(ckBytes);1012free(ckParamPtr);1013return NULL;1014}
1015
1016/*
1017* converts the Java CK_GCM_PARAMS object to a CK_GCM_PARAMS pointer
1018* Note: Early NSS versions crash w/ CK_GCM_PARAMS and need to use
1019* CK_GCM_PARAMS_NO_IVBITS to avoid SIGSEGV.
1020*
1021* @param env - used to call JNI funktions to get the Java classes and objects
1022* @param jParam - the Java CK_GCM_PARAMS object to convert
1023* @param pLength - length of the allocated memory of the returned pointer
1024* @return pointer to the new CK_GCM_PARAMS structure
1025*/
1026CK_GCM_PARAMS_PTR
1027jGCMParamsToCKGCMParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)1028{
1029CK_GCM_PARAMS_PTR ckParamPtr;1030jclass jGcmParamsClass;1031jfieldID fieldID;1032jobject jIv, jAad;1033jlong jTagLen;1034
1035TRACE0("DEBUG jGCMParamsToCKGCMParam is called\n");1036
1037if (pLength != NULL) {1038*pLength = 0L;1039}1040
1041// retrieve java values1042jGcmParamsClass = (*env)->FindClass(env, CLASS_GCM_PARAMS);1043if (jGcmParamsClass == NULL) { return NULL; }1044if (!(*env)->IsInstanceOf(env, jParam, jGcmParamsClass)) {1045return NULL;1046}1047fieldID = (*env)->GetFieldID(env, jGcmParamsClass, "iv", "[B");1048if (fieldID == NULL) { return NULL; }1049jIv = (*env)->GetObjectField(env, jParam, fieldID);1050fieldID = (*env)->GetFieldID(env, jGcmParamsClass, "aad", "[B");1051if (fieldID == NULL) { return NULL; }1052jAad = (*env)->GetObjectField(env, jParam, fieldID);1053fieldID = (*env)->GetFieldID(env, jGcmParamsClass, "tagBits", "J");1054if (fieldID == NULL) { return NULL; }1055jTagLen = (*env)->GetLongField(env, jParam, fieldID);1056
1057// allocate memory for CK_GCM_PARAMS pointer1058ckParamPtr = calloc(1, sizeof(CK_GCM_PARAMS));1059if (ckParamPtr == NULL) {1060p11ThrowOutOfMemoryError(env, 0);1061return NULL;1062}1063
1064// populate using java values1065jByteArrayToCKByteArray(env, jIv, &(ckParamPtr->pIv), &(ckParamPtr->ulIvLen));1066if ((*env)->ExceptionCheck(env)) {1067goto cleanup;1068}1069// adjust since the value is in bits1070ckParamPtr->ulIvBits = ckParamPtr->ulIvLen << 3;1071
1072jByteArrayToCKByteArray(env, jAad, &(ckParamPtr->pAAD), &(ckParamPtr->ulAADLen));1073if ((*env)->ExceptionCheck(env)) {1074goto cleanup;1075}1076
1077ckParamPtr->ulTagBits = jLongToCKULong(jTagLen);1078
1079if (pLength != NULL) {1080*pLength = sizeof(CK_GCM_PARAMS);1081}1082TRACE1("Created inner GCM_PARAMS PTR %p\n", ckParamPtr);1083return ckParamPtr;1084cleanup:1085free(ckParamPtr->pIv);1086free(ckParamPtr->pAAD);1087free(ckParamPtr);1088return NULL;1089}
1090
1091/*
1092* converts the Java CK_CCM_PARAMS object to a CK_CCM_PARAMS pointer
1093*
1094* @param env - used to call JNI functions to get the Java classes and objects
1095* @param jParam - the Java CK_CCM_PARAMS object to convert
1096* @param pLength - length of the allocated memory of the returned pointer
1097* @return pointer to the new CK_CCM_PARAMS structure
1098*/
1099CK_CCM_PARAMS_PTR
1100jCCMParamsToCKCCMParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)1101{
1102CK_CCM_PARAMS_PTR ckParamPtr;1103jclass jCcmParamsClass;1104jfieldID fieldID;1105jobject jNonce, jAad;1106jlong jDataLen, jMacLen;1107
1108if (pLength != NULL) {1109*pLength = 0;1110}1111
1112// retrieve java values1113jCcmParamsClass = (*env)->FindClass(env, CLASS_CCM_PARAMS);1114if (jCcmParamsClass == NULL) { return NULL; }1115if (!(*env)->IsInstanceOf(env, jParam, jCcmParamsClass)) {1116return NULL;1117}1118fieldID = (*env)->GetFieldID(env, jCcmParamsClass, "dataLen", "J");1119if (fieldID == NULL) { return NULL; }1120jDataLen = (*env)->GetLongField(env, jParam, fieldID);1121fieldID = (*env)->GetFieldID(env, jCcmParamsClass, "nonce", "[B");1122if (fieldID == NULL) { return NULL; }1123jNonce = (*env)->GetObjectField(env, jParam, fieldID);1124fieldID = (*env)->GetFieldID(env, jCcmParamsClass, "aad", "[B");1125if (fieldID == NULL) { return NULL; }1126jAad = (*env)->GetObjectField(env, jParam, fieldID);1127fieldID = (*env)->GetFieldID(env, jCcmParamsClass, "macLen", "J");1128if (fieldID == NULL) { return NULL; }1129jMacLen = (*env)->GetLongField(env, jParam, fieldID);1130
1131// allocate memory for CK_CCM_PARAMS pointer1132ckParamPtr = calloc(1, sizeof(CK_CCM_PARAMS));1133if (ckParamPtr == NULL) {1134p11ThrowOutOfMemoryError(env, 0);1135return NULL;1136}1137
1138// populate using java values1139ckParamPtr->ulDataLen = jLongToCKULong(jDataLen);1140jByteArrayToCKByteArray(env, jNonce, &(ckParamPtr->pNonce),1141&(ckParamPtr->ulNonceLen));1142if ((*env)->ExceptionCheck(env)) {1143goto cleanup;1144}1145
1146jByteArrayToCKByteArray(env, jAad, &(ckParamPtr->pAAD),1147&(ckParamPtr->ulAADLen));1148if ((*env)->ExceptionCheck(env)) {1149goto cleanup;1150}1151
1152ckParamPtr->ulMACLen = jLongToCKULong(jMacLen);1153
1154if (pLength != NULL) {1155*pLength = sizeof(CK_CCM_PARAMS);1156}1157return ckParamPtr;1158cleanup:1159free(ckParamPtr->pNonce);1160free(ckParamPtr->pAAD);1161free(ckParamPtr);1162return NULL;1163}
1164
1165/*
1166* converts the Java CK_SALSA20_CHACHA20_POLY1305_PARAMS object to a
1167* CK_SALSA20_CHACHA20_POLY1305_PARAMS pointer
1168*
1169* @param env - used to call JNI functions to get the Java classes and objects
1170* @param jParam - the Java CK_SALSA20_CHACHA20_POLY1305_PARAMS object to
1171* convert
1172* @param pLength - length of the allocated memory of the returned pointer
1173* @return pointer to the new CK_SALSA20_CHACHA20_POLY1305_PARAMS structure
1174*/
1175CK_SALSA20_CHACHA20_POLY1305_PARAMS_PTR
1176jSalsaChaChaPolyParamsToCKSalsaChaChaPolyParamPtr(1177JNIEnv *env, jobject jParam, CK_ULONG *pLength)1178{
1179CK_SALSA20_CHACHA20_POLY1305_PARAMS_PTR ckParamPtr;1180jclass jParamsClass;1181jfieldID fieldID;1182jobject jNonce, jAad;1183
1184if (pLength != NULL) {1185*pLength = 0;1186}1187
1188// retrieve java values1189jParamsClass = (*env)->FindClass(env,1190CLASS_SALSA20_CHACHA20_POLY1305_PARAMS);1191if (jParamsClass == NULL) { return NULL; }1192if (!(*env)->IsInstanceOf(env, jParam, jParamsClass)) {1193return NULL;1194}1195fieldID = (*env)->GetFieldID(env, jParamsClass, "nonce", "[B");1196if (fieldID == NULL) { return NULL; }1197jNonce = (*env)->GetObjectField(env, jParam, fieldID);1198fieldID = (*env)->GetFieldID(env, jParamsClass, "aad", "[B");1199if (fieldID == NULL) { return NULL; }1200jAad = (*env)->GetObjectField(env, jParam, fieldID);1201// allocate memory for CK_SALSA20_CHACHA20_POLY1305_PARAMS pointer1202ckParamPtr = calloc(1, sizeof(CK_SALSA20_CHACHA20_POLY1305_PARAMS));1203if (ckParamPtr == NULL) {1204p11ThrowOutOfMemoryError(env, 0);1205return NULL;1206}1207
1208// populate using java values1209jByteArrayToCKByteArray(env, jNonce, &(ckParamPtr->pNonce),1210&(ckParamPtr->ulNonceLen));1211if ((*env)->ExceptionCheck(env)) {1212goto cleanup;1213}1214
1215jByteArrayToCKByteArray(env, jAad, &(ckParamPtr->pAAD),1216&(ckParamPtr->ulAADLen));1217if ((*env)->ExceptionCheck(env)) {1218goto cleanup;1219}1220
1221if (pLength != NULL) {1222*pLength = sizeof(CK_SALSA20_CHACHA20_POLY1305_PARAMS);1223}1224return ckParamPtr;1225cleanup:1226free(ckParamPtr->pNonce);1227free(ckParamPtr->pAAD);1228free(ckParamPtr);1229return NULL;1230}
1231
1232/*
1233* converts a Java CK_MECHANISM object into a CK_MECHANISM pointer
1234* pointer.
1235*
1236* @param env - used to call JNI funktions to get the values out of the Java object
1237* @param jMech - the Java CK_MECHANISM object to convert
1238* @return pointer to the new CK_MECHANISM structure
1239*/
1240CK_MECHANISM_PTR jMechanismToCKMechanismPtr(JNIEnv *env, jobject jMech)1241{
1242CK_MECHANISM_PTR ckpMech;1243jlong jMechType = (*env)->GetLongField(env, jMech, mech_mechanismID);1244jobject jParam = (*env)->GetObjectField(env, jMech, mech_pParameterID);1245
1246/* allocate memory for CK_MECHANISM_PTR */1247ckpMech = (CK_MECHANISM_PTR) calloc(1, sizeof(CK_MECHANISM));1248if (ckpMech == NULL) {1249p11ThrowOutOfMemoryError(env, 0);1250return NULL;1251}1252TRACE1("DEBUG jMechanismToCKMechanismPtr: allocated mech %p\n", ckpMech);1253
1254ckpMech->mechanism = jLongToCKULong(jMechType);1255
1256/* convert the specific Java mechanism parameter object to a pointer to a1257* CK-type mechanism structure
1258*/
1259if (jParam == NULL) {1260ckpMech->pParameter = NULL;1261ckpMech->ulParameterLen = 0;1262} else {1263ckpMech->pParameter = jMechParamToCKMechParamPtr(env, jParam,1264ckpMech->mechanism, &(ckpMech->ulParameterLen));1265}1266return ckpMech;1267}
1268
1269/*
1270* converts the pValue of a CK_ATTRIBUTE structure into a Java Object by
1271* checking the type of the attribute. A PKCS#11 attribute value can
1272* be a CK_ULONG, CK_BYTE[], CK_CHAR[], big integer, CK_BBOOL, CK_UTF8CHAR[],
1273* CK_DATE or CK_FLAGS that gets converted to a corresponding Java object.
1274*
1275* @param env - used to call JNI functions to create the new Java object
1276* @param ckpAttribute - the pointer to the CK_ATTRIBUTE structure that contains the type
1277* and the pValue to convert
1278* @return the new Java object of the CK-type pValue
1279*/
1280jobject ckAttributeValueToJObject(JNIEnv *env, const CK_ATTRIBUTE_PTR ckpAttribute)1281{
1282jint jValueLength;1283jobject jValueObject = NULL;1284
1285jValueLength = ckULongToJInt(ckpAttribute->ulValueLen);1286
1287if ((jValueLength <= 0) || (ckpAttribute->pValue == NULL)) {1288return NULL ;1289}1290
1291switch(ckpAttribute->type) {1292case CKA_CLASS:1293/* value CK_OBJECT_CLASS, defacto a CK_ULONG */1294case CKA_KEY_TYPE:1295/* value CK_KEY_TYPE, defacto a CK_ULONG */1296case CKA_CERTIFICATE_TYPE:1297/* value CK_CERTIFICATE_TYPE, defacto a CK_ULONG */1298case CKA_HW_FEATURE_TYPE:1299/* value CK_HW_FEATURE_TYPE, defacto a CK_ULONG */1300case CKA_MODULUS_BITS:1301case CKA_VALUE_BITS:1302case CKA_VALUE_LEN:1303case CKA_KEY_GEN_MECHANISM:1304case CKA_PRIME_BITS:1305case CKA_SUB_PRIME_BITS:1306/* value CK_ULONG */1307jValueObject = ckULongPtrToJLongObject(env, (CK_ULONG*) ckpAttribute->pValue);1308break;1309
1310/* can be CK_BYTE[],CK_CHAR[] or big integer; defacto always CK_BYTE[] */1311case CKA_VALUE:1312case CKA_OBJECT_ID:1313case CKA_SUBJECT:1314case CKA_ID:1315case CKA_ISSUER:1316case CKA_SERIAL_NUMBER:1317case CKA_OWNER:1318case CKA_AC_ISSUER:1319case CKA_ATTR_TYPES:1320case CKA_ECDSA_PARAMS:1321/* CKA_EC_PARAMS is the same, these two are equivalent */1322case CKA_EC_POINT:1323case CKA_PRIVATE_EXPONENT:1324case CKA_PRIME_1:1325case CKA_PRIME_2:1326case CKA_EXPONENT_1:1327case CKA_EXPONENT_2:1328case CKA_COEFFICIENT:1329/* value CK_BYTE[] */1330jValueObject = ckByteArrayToJByteArray(env, (CK_BYTE*) ckpAttribute->pValue, jValueLength);1331break;1332
1333case CKA_RESET_ON_INIT:1334case CKA_HAS_RESET:1335case CKA_TOKEN:1336case CKA_PRIVATE:1337case CKA_MODIFIABLE:1338case CKA_DERIVE:1339case CKA_LOCAL:1340case CKA_ENCRYPT:1341case CKA_VERIFY:1342case CKA_VERIFY_RECOVER:1343case CKA_WRAP:1344case CKA_SENSITIVE:1345case CKA_SECONDARY_AUTH:1346case CKA_DECRYPT:1347case CKA_SIGN:1348case CKA_SIGN_RECOVER:1349case CKA_UNWRAP:1350case CKA_EXTRACTABLE:1351case CKA_ALWAYS_SENSITIVE:1352case CKA_NEVER_EXTRACTABLE:1353case CKA_TRUSTED:1354/* value CK_BBOOL */1355jValueObject = ckBBoolPtrToJBooleanObject(env, (CK_BBOOL*) ckpAttribute->pValue);1356break;1357
1358case CKA_LABEL:1359case CKA_APPLICATION:1360/* value RFC 2279 (UTF-8) string */1361jValueObject = ckUTF8CharArrayToJCharArray(env, (CK_UTF8CHAR*) ckpAttribute->pValue, jValueLength);1362break;1363
1364case CKA_START_DATE:1365case CKA_END_DATE:1366/* value CK_DATE */1367jValueObject = ckDatePtrToJDateObject(env, (CK_DATE*) ckpAttribute->pValue);1368break;1369
1370case CKA_MODULUS:1371case CKA_PUBLIC_EXPONENT:1372case CKA_PRIME:1373case CKA_SUBPRIME:1374case CKA_BASE:1375/* value big integer, i.e. CK_BYTE[] */1376jValueObject = ckByteArrayToJByteArray(env, (CK_BYTE*) ckpAttribute->pValue, jValueLength);1377break;1378
1379case CKA_AUTH_PIN_FLAGS:1380jValueObject = ckULongPtrToJLongObject(env, (CK_ULONG*) ckpAttribute->pValue);1381/* value FLAGS, defacto a CK_ULONG */1382break;1383
1384case CKA_VENDOR_DEFINED:1385/* we make a CK_BYTE[] out of this */1386jValueObject = ckByteArrayToJByteArray(env, (CK_BYTE*) ckpAttribute->pValue, jValueLength);1387break;1388
1389// Netscape trust attributes1390case CKA_NETSCAPE_TRUST_SERVER_AUTH:1391case CKA_NETSCAPE_TRUST_CLIENT_AUTH:1392case CKA_NETSCAPE_TRUST_CODE_SIGNING:1393case CKA_NETSCAPE_TRUST_EMAIL_PROTECTION:1394/* value CK_ULONG */1395jValueObject = ckULongPtrToJLongObject(env, (CK_ULONG*) ckpAttribute->pValue);1396break;1397
1398default:1399/* we make a CK_BYTE[] out of this */1400jValueObject = ckByteArrayToJByteArray(env, (CK_BYTE*) ckpAttribute->pValue, jValueLength);1401break;1402}1403
1404return jValueObject ;1405}
1406
1407/*
1408* the following functions convert a Java mechanism parameter object to a PKCS#11
1409* mechanism parameter structure
1410*
1411* CK_<Param>_PARAMS j<Param>ParamToCK<Param>Param(JNIEnv *env,
1412* jobject jParam);
1413*
1414* These functions get a Java object, that must be the right Java mechanism
1415* object and they return the new PKCS#11 mechanism parameter structure.
1416* Every field of the Java object is retrieved, gets converted to a corresponding
1417* PKCS#11 type and is set in the new PKCS#11 structure.
1418*/
1419
1420/*
1421* converts the given Java mechanism parameter to a CK mechanism parameter
1422* pointer and store the length in bytes in the length variable.
1423*
1424* @param env - used to call JNI funktions to get the Java classes and objects
1425* @param jParam - the Java mechanism parameter object to convert
1426* @param ckMech - the PKCS#11 mechanism type
1427* @param ckpLength - the reference of the length in bytes of the new CK mechanism parameter
1428* structure
1429* @return pointer to the new CK mechanism parameter structure
1430*/
1431CK_VOID_PTR jMechParamToCKMechParamPtr(JNIEnv *env, jobject jParam,1432CK_MECHANISM_TYPE ckMech, CK_ULONG *ckpLength)1433{
1434CK_VOID_PTR ckpParamPtr;1435if (jParam == NULL) {1436ckpParamPtr = NULL;1437*ckpLength = 0;1438} else if ((*env)->IsInstanceOf(env, jParam, jByteArrayClass)) {1439jByteArrayToCKByteArray(env, jParam, (CK_BYTE_PTR *) &ckpParamPtr, ckpLength);1440} else if ((*env)->IsInstanceOf(env, jParam, jLongClass)) {1441ckpParamPtr = jLongObjectToCKULongPtr(env, jParam);1442*ckpLength = sizeof(CK_ULONG);1443} else {1444ckpParamPtr = jMechParamToCKMechParamPtrSlow(env, jParam, ckMech, ckpLength);1445}1446return ckpParamPtr;1447}
1448
1449CK_VOID_PTR jMechParamToCKMechParamPtrSlow(JNIEnv *env, jobject jParam,1450CK_MECHANISM_TYPE ckMech, CK_ULONG *ckpLength)1451{
1452CK_VOID_PTR ckpParamPtr = NULL;1453
1454/*1455* Most common cases, i.e. NULL/byte[]/long, are already handled by
1456* jMechParamToCKMechParam before calling this method.
1457*/
1458TRACE1("\nDEBUG: jMechParamToCKMechParamPtrSlow, mech=0x%lX\n", ckMech);1459
1460switch (ckMech) {1461case CKM_SSL3_PRE_MASTER_KEY_GEN:1462case CKM_TLS_PRE_MASTER_KEY_GEN:1463ckpParamPtr = jVersionToCKVersionPtr(env, jParam);1464if (ckpParamPtr != NULL) {1465*ckpLength = sizeof(CK_VERSION);1466} else {1467*ckpLength = 0;1468}1469break;1470case CKM_SSL3_MASTER_KEY_DERIVE:1471case CKM_TLS_MASTER_KEY_DERIVE:1472case CKM_SSL3_MASTER_KEY_DERIVE_DH:1473case CKM_TLS_MASTER_KEY_DERIVE_DH:1474ckpParamPtr = jSsl3MasterKeyDeriveParamToCKSsl3MasterKeyDeriveParamPtr(env, jParam,1475ckpLength);1476break;1477case CKM_SSL3_KEY_AND_MAC_DERIVE:1478case CKM_TLS_KEY_AND_MAC_DERIVE:1479ckpParamPtr = jSsl3KeyMatParamToCKSsl3KeyMatParamPtr(env, jParam,1480ckpLength);1481break;1482case CKM_TLS12_KEY_AND_MAC_DERIVE:1483ckpParamPtr = jTls12KeyMatParamToCKTls12KeyMatParamPtr(env, jParam,1484ckpLength);1485break;1486case CKM_TLS12_MASTER_KEY_DERIVE:1487case CKM_TLS12_MASTER_KEY_DERIVE_DH:1488ckpParamPtr = jTls12MasterKeyDeriveParamToCKTls12MasterKeyDeriveParamPtr(env, jParam,1489ckpLength);1490break;1491case CKM_TLS_PRF:1492case CKM_NSS_TLS_PRF_GENERAL:1493ckpParamPtr = jTlsPrfParamsToCKTlsPrfParamPtr(env, jParam,1494ckpLength);1495break;1496case CKM_TLS_MAC:1497ckpParamPtr = jTlsMacParamsToCKTlsMacParamPtr(env, jParam,1498ckpLength);1499break;1500case CKM_AES_CTR:1501ckpParamPtr = jAesCtrParamsToCKAesCtrParamPtr(env, jParam,1502ckpLength);1503break;1504case CKM_AES_GCM:1505ckpParamPtr = jGCMParamsToCKGCMParamPtr(env, jParam, ckpLength);1506break;1507case CKM_AES_CCM:1508ckpParamPtr = jCCMParamsToCKCCMParamPtr(env, jParam, ckpLength);1509break;1510case CKM_CHACHA20_POLY1305:1511ckpParamPtr =1512jSalsaChaChaPolyParamsToCKSalsaChaChaPolyParamPtr(env,1513jParam, ckpLength);1514break;1515case CKM_RSA_PKCS_OAEP:1516ckpParamPtr = jRsaPkcsOaepParamToCKRsaPkcsOaepParamPtr(env, jParam, ckpLength);1517break;1518case CKM_PBA_SHA1_WITH_SHA1_HMAC:1519case CKM_NSS_PKCS12_PBE_SHA224_HMAC_KEY_GEN:1520case CKM_NSS_PKCS12_PBE_SHA256_HMAC_KEY_GEN:1521case CKM_NSS_PKCS12_PBE_SHA384_HMAC_KEY_GEN:1522case CKM_NSS_PKCS12_PBE_SHA512_HMAC_KEY_GEN:1523ckpParamPtr = jPbeParamToCKPbeParamPtr(env, jParam, ckpLength);1524break;1525case CKM_PKCS5_PBKD2:1526ckpParamPtr = jPkcs5Pbkd2ParamToCKPkcs5Pbkd2ParamPtr(env, jParam, ckpLength);1527break;1528case CKM_RSA_PKCS_PSS:1529case CKM_SHA1_RSA_PKCS_PSS:1530case CKM_SHA256_RSA_PKCS_PSS:1531case CKM_SHA384_RSA_PKCS_PSS:1532case CKM_SHA512_RSA_PKCS_PSS:1533case CKM_SHA224_RSA_PKCS_PSS:1534ckpParamPtr = jRsaPkcsPssParamToCKRsaPkcsPssParamPtr(env, jParam, ckpLength);1535break;1536case CKM_ECDH1_DERIVE:1537case CKM_ECDH1_COFACTOR_DERIVE:1538ckpParamPtr = jEcdh1DeriveParamToCKEcdh1DeriveParamPtr(env, jParam, ckpLength);1539break;1540case CKM_ECMQV_DERIVE:1541ckpParamPtr = jEcdh2DeriveParamToCKEcdh2DeriveParamPtr(env, jParam, ckpLength);1542break;1543case CKM_X9_42_DH_DERIVE:1544ckpParamPtr = jX942Dh1DeriveParamToCKX942Dh1DeriveParamPtr(env, jParam, ckpLength);1545break;1546case CKM_X9_42_DH_HYBRID_DERIVE:1547case CKM_X9_42_MQV_DERIVE:1548ckpParamPtr = jX942Dh2DeriveParamToCKX942Dh2DeriveParamPtr(env, jParam, ckpLength);1549break;1550// defined by pkcs11.h but we don't support1551case CKM_KEA_DERIVE: // CK_KEA_DERIVE_PARAMS1552case CKM_RC2_CBC: // CK_RC2_CBC_PARAMS1553case CKM_RC2_MAC_GENERAL: // CK_RC2_MAC_GENERAL_PARAMS1554case CKM_RC5_ECB: // CK_RC5_PARAMS1555case CKM_RC5_MAC: // CK_RC5_PARAMS1556case CKM_RC5_CBC: // CK_RC5_CBC_PARAMS1557case CKM_RC5_MAC_GENERAL: // CK_RC5_MAC_GENERAL_PARAMS1558case CKM_SKIPJACK_PRIVATE_WRAP: // CK_SKIPJACK_PRIVATE_WRAP_PARAMS1559case CKM_SKIPJACK_RELAYX: // CK_SKIPJACK_RELAYX_PARAMS1560case CKM_KEY_WRAP_SET_OAEP: // CK_KEY_WRAP_SET_OAEP_PARAMS1561p11ThrowPKCS11RuntimeException(env, "No parameter support for this mechanism");1562break;1563default:1564/* if everything failed up to here */1565/* try if the parameter is a primitive Java type */1566ckpParamPtr = jObjectToPrimitiveCKObjectPtr(env, jParam, ckpLength);1567/* *ckpParamPtr = jObjectToCKVoidPtr(jParam); */1568/* *ckpLength = 1; */1569}1570TRACE0("\nDEBUG: jMechParamToCKMechParamPtrSlow FINISHED\n");1571
1572if ((*env)->ExceptionCheck(env)) {1573return NULL;1574}1575
1576return ckpParamPtr;1577}
1578
1579/*
1580* converts the Java CK_RSA_PKCS_OAEP_PARAMS object to a
1581* CK_RSA_PKCS_OAEP_PARAMS pointer
1582*
1583* @param env - used to call JNI funktions to get the Java classes and objects
1584* @param jParam - the Java CK_RSA_PKCS_OAEP_PARAMS object to convert
1585* @param pLength - length of the allocated memory of the returned pointer
1586* @return pointer to the new CK_RSA_PKCS_OAEP_PARAMS structure
1587*/
1588CK_RSA_PKCS_OAEP_PARAMS_PTR
1589jRsaPkcsOaepParamToCKRsaPkcsOaepParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)1590{
1591CK_RSA_PKCS_OAEP_PARAMS_PTR ckParamPtr;1592jclass jRsaPkcsOaepParamsClass;1593jfieldID fieldID;1594jlong jHashAlg, jMgf, jSource;1595jobject jSourceData;1596
1597if (pLength!= NULL) {1598*pLength = 0L;1599}1600
1601// retrieve java values1602jRsaPkcsOaepParamsClass = (*env)->FindClass(env, CLASS_RSA_PKCS_OAEP_PARAMS);1603if (jRsaPkcsOaepParamsClass == NULL) { return NULL; }1604fieldID = (*env)->GetFieldID(env, jRsaPkcsOaepParamsClass, "hashAlg", "J");1605if (fieldID == NULL) { return NULL; }1606jHashAlg = (*env)->GetLongField(env, jParam, fieldID);1607fieldID = (*env)->GetFieldID(env, jRsaPkcsOaepParamsClass, "mgf", "J");1608if (fieldID == NULL) { return NULL; }1609jMgf = (*env)->GetLongField(env, jParam, fieldID);1610fieldID = (*env)->GetFieldID(env, jRsaPkcsOaepParamsClass, "source", "J");1611if (fieldID == NULL) { return NULL; }1612jSource = (*env)->GetLongField(env, jParam, fieldID);1613fieldID = (*env)->GetFieldID(env, jRsaPkcsOaepParamsClass, "pSourceData", "[B");1614if (fieldID == NULL) { return NULL; }1615jSourceData = (*env)->GetObjectField(env, jParam, fieldID);1616
1617// allocate memory for CK_RSA_PKCS_OAEP_PARAMS pointer1618ckParamPtr = calloc(1, sizeof(CK_RSA_PKCS_OAEP_PARAMS));1619if (ckParamPtr == NULL) {1620p11ThrowOutOfMemoryError(env, 0);1621return NULL;1622}1623
1624// populate using java values1625ckParamPtr->hashAlg = jLongToCKULong(jHashAlg);1626ckParamPtr->mgf = jLongToCKULong(jMgf);1627ckParamPtr->source = jLongToCKULong(jSource);1628jByteArrayToCKByteArray(env, jSourceData, (CK_BYTE_PTR*) &(ckParamPtr->pSourceData),1629&(ckParamPtr->ulSourceDataLen));1630if ((*env)->ExceptionCheck(env)) {1631free(ckParamPtr);1632return NULL;1633}1634
1635if (pLength!= NULL) {1636*pLength = sizeof(CK_RSA_PKCS_OAEP_PARAMS);1637}1638return ckParamPtr;1639}
1640
1641/*
1642* converts the Java CK_PBE_PARAMS object to a CK_PBE_PARAMS pointer
1643*
1644* @param env - used to call JNI funktions to get the Java classes and objects
1645* @param jParam - the Java CK_PBE_PARAMS object to convert
1646* @param pLength - length of the allocated memory of the returned pointer
1647* @return pointer to the new CK_PBE_PARAMS structure
1648*/
1649CK_PBE_PARAMS_PTR
1650jPbeParamToCKPbeParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)1651{
1652CK_PBE_PARAMS_PTR ckParamPtr;1653jclass jPbeParamsClass;1654jfieldID fieldID;1655jlong jIteration;1656jobject jInitVector, jPassword, jSalt;1657CK_ULONG ckTemp;1658
1659if (pLength != NULL) {1660*pLength = 0;1661}1662
1663// retrieve java values1664jPbeParamsClass = (*env)->FindClass(env, CLASS_PBE_PARAMS);1665if (jPbeParamsClass == NULL) { return NULL; }1666fieldID = (*env)->GetFieldID(env, jPbeParamsClass, "pInitVector", "[B");1667if (fieldID == NULL) { return NULL; }1668jInitVector = (*env)->GetObjectField(env, jParam, fieldID);1669fieldID = (*env)->GetFieldID(env, jPbeParamsClass, "pPassword", "[C");1670if (fieldID == NULL) { return NULL; }1671jPassword = (*env)->GetObjectField(env, jParam, fieldID);1672fieldID = (*env)->GetFieldID(env, jPbeParamsClass, "pSalt", "[B");1673if (fieldID == NULL) { return NULL; }1674jSalt = (*env)->GetObjectField(env, jParam, fieldID);1675fieldID = (*env)->GetFieldID(env, jPbeParamsClass, "ulIteration", "J");1676if (fieldID == NULL) { return NULL; }1677jIteration = (*env)->GetLongField(env, jParam, fieldID);1678
1679// allocate memory for CK_PBE_PARAMS pointer1680ckParamPtr = calloc(1, sizeof(CK_PBE_PARAMS));1681if (ckParamPtr == NULL) {1682p11ThrowOutOfMemoryError(env, 0);1683return NULL;1684}1685
1686// populate using java values1687ckParamPtr->ulIteration = jLongToCKULong(jIteration);1688jByteArrayToCKByteArray(env, jInitVector, &(ckParamPtr->pInitVector), &ckTemp);1689if ((*env)->ExceptionCheck(env)) {1690goto cleanup;1691}1692jCharArrayToCKUTF8CharArray(env, jPassword, &(ckParamPtr->pPassword), &(ckParamPtr->ulPasswordLen));1693if ((*env)->ExceptionCheck(env)) {1694goto cleanup;1695}1696jByteArrayToCKByteArray(env, jSalt, &(ckParamPtr->pSalt), &(ckParamPtr->ulSaltLen));1697if ((*env)->ExceptionCheck(env)) {1698goto cleanup;1699}1700
1701if (pLength != NULL) {1702*pLength = sizeof(CK_PBE_PARAMS);1703}1704return ckParamPtr;1705cleanup:1706free(ckParamPtr->pInitVector);1707if (ckParamPtr->pPassword != NULL) {1708memset(ckParamPtr->pPassword, 0, ckParamPtr->ulPasswordLen);1709}1710free(ckParamPtr->pPassword);1711free(ckParamPtr->pSalt);1712free(ckParamPtr);1713return NULL;1714}
1715
1716/*
1717* Copy back the initialization vector from the native structure to the
1718* Java object. This is only used for CKM_PBE_* mechanisms and their
1719* CK_PBE_PARAMS parameters.
1720*
1721*/
1722void copyBackPBEInitializationVector(JNIEnv *env, CK_MECHANISM *ckMechanism, jobject jMechanism)1723{
1724jclass jMechanismClass, jPbeParamsClass;1725CK_PBE_PARAMS *ckParam;1726jfieldID fieldID;1727CK_MECHANISM_TYPE ckMechanismType;1728jlong jMechanismType;1729jobject jParameter;1730jobject jInitVector;1731jint jInitVectorLength;1732CK_CHAR_PTR initVector;1733int i;1734jchar* jInitVectorChars;1735
1736/* get mechanism */1737jMechanismClass = (*env)->FindClass(env, CLASS_MECHANISM);1738if (jMechanismClass == NULL) { return; }1739fieldID = (*env)->GetFieldID(env, jMechanismClass, "mechanism", "J");1740if (fieldID == NULL) { return; }1741jMechanismType = (*env)->GetLongField(env, jMechanism, fieldID);1742ckMechanismType = jLongToCKULong(jMechanismType);1743if (ckMechanismType != ckMechanism->mechanism) {1744/* we do not have matching types, this should not occur */1745return;1746}1747
1748jPbeParamsClass = (*env)->FindClass(env, CLASS_PBE_PARAMS);1749if (jPbeParamsClass == NULL) { return; }1750ckParam = (CK_PBE_PARAMS *) ckMechanism->pParameter;1751if (ckParam != NULL_PTR) {1752initVector = ckParam->pInitVector;1753if (initVector != NULL_PTR) {1754/* get pParameter */1755fieldID = (*env)->GetFieldID(env, jMechanismClass, "pParameter", "Ljava/lang/Object;");1756if (fieldID == NULL) { return; }1757jParameter = (*env)->GetObjectField(env, jMechanism, fieldID);1758fieldID = (*env)->GetFieldID(env, jPbeParamsClass, "pInitVektor", "[C");1759if (fieldID == NULL) { return; }1760jInitVector = (*env)->GetObjectField(env, jParameter, fieldID);1761
1762if (jInitVector != NULL) {1763jInitVectorLength = (*env)->GetArrayLength(env, jInitVector);1764jInitVectorChars = (*env)->GetCharArrayElements(env, jInitVector, NULL);1765if (jInitVectorChars == NULL) { return; }1766
1767/* copy the chars to the Java buffer */1768for (i=0; i < jInitVectorLength; i++) {1769jInitVectorChars[i] = ckCharToJChar(initVector[i]);1770}1771/* copy back the Java buffer to the object */1772(*env)->ReleaseCharArrayElements(env, jInitVector, jInitVectorChars, 0);1773}1774}1775}1776}
1777
1778#define PBKD2_PARAM_SET(member, value) \1779do { \1780if(ckParamPtr->version == PARAMS) { \1781ckParamPtr->params.v1.member = value; \1782} else { \1783ckParamPtr->params.v2.member = value; \1784} \1785} while(0)1786
1787#define PBKD2_PARAM_ADDR(member) \1788( \1789(ckParamPtr->version == PARAMS) ? \1790(void*) &ckParamPtr->params.v1.member : \1791(void*) &ckParamPtr->params.v2.member \1792)1793
1794/*
1795* converts a Java CK_PKCS5_PBKD2_PARAMS object to a CK_PKCS5_PBKD2_PARAMS
1796* pointer, or a Java CK_PKCS5_PBKD2_PARAMS2 object to a CK_PKCS5_PBKD2_PARAMS2
1797* pointer
1798*
1799* @param env - used to call JNI functions to get the Java classes and objects
1800* @param jParam - the Java object to convert
1801* @param pLength - length of the allocated memory of the returned pointer
1802* @return pointer to the new structure
1803*/
1804CK_VOID_PTR
1805jPkcs5Pbkd2ParamToCKPkcs5Pbkd2ParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)1806{
1807VersionedPbkd2ParamsPtr ckParamPtr;1808ParamVersion paramVersion;1809CK_ULONG_PTR pUlPasswordLen;1810jclass jPkcs5Pbkd2ParamsClass;1811jfieldID fieldID;1812jlong jSaltSource, jIteration, jPrf;1813jobject jSaltSourceData, jPrfData, jPassword;1814
1815if (pLength != NULL) {1816*pLength = 0L;1817}1818
1819// retrieve java values1820if ((jPkcs5Pbkd2ParamsClass =1821(*env)->FindClass(env, CLASS_PKCS5_PBKD2_PARAMS)) != NULL1822&& (*env)->IsInstanceOf(env, jParam, jPkcs5Pbkd2ParamsClass)) {1823paramVersion = PARAMS;1824} else if ((jPkcs5Pbkd2ParamsClass =1825(*env)->FindClass(env, CLASS_PKCS5_PBKD2_PARAMS2)) != NULL1826&& (*env)->IsInstanceOf(env, jParam, jPkcs5Pbkd2ParamsClass)) {1827paramVersion = PARAMS2;1828} else {1829p11ThrowPKCS11RuntimeException(env, "Unknown PBKD2 mechanism parameters class.");1830return NULL;1831}1832fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "saltSource", "J");1833if (fieldID == NULL) { return NULL; }1834jSaltSource = (*env)->GetLongField(env, jParam, fieldID);1835fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "pSaltSourceData", "[B");1836if (fieldID == NULL) { return NULL; }1837jSaltSourceData = (*env)->GetObjectField(env, jParam, fieldID);1838fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "iterations", "J");1839if (fieldID == NULL) { return NULL; }1840jIteration = (*env)->GetLongField(env, jParam, fieldID);1841fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "prf", "J");1842if (fieldID == NULL) { return NULL; }1843jPrf = (*env)->GetLongField(env, jParam, fieldID);1844fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "pPrfData", "[B");1845if (fieldID == NULL) { return NULL; }1846jPrfData = (*env)->GetObjectField(env, jParam, fieldID);1847fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "pPassword", "[C");1848if (fieldID == NULL) { return NULL; }1849jPassword = (*env)->GetObjectField(env, jParam, fieldID);1850
1851// allocate memory for VersionedPbkd2Params and store the structure version1852ckParamPtr = calloc(1, sizeof(VersionedPbkd2Params));1853if (ckParamPtr == NULL) {1854p11ThrowOutOfMemoryError(env, 0);1855return NULL;1856}1857ckParamPtr->version = paramVersion;1858
1859// populate using java values1860PBKD2_PARAM_SET(saltSource, jLongToCKULong(jSaltSource));1861jByteArrayToCKByteArray(env, jSaltSourceData,1862(CK_BYTE_PTR *) PBKD2_PARAM_ADDR(pSaltSourceData),1863PBKD2_PARAM_ADDR(ulSaltSourceDataLen));1864if ((*env)->ExceptionCheck(env)) {1865goto cleanup;1866}1867PBKD2_PARAM_SET(iterations, jLongToCKULong(jIteration));1868PBKD2_PARAM_SET(prf, jLongToCKULong(jPrf));1869jByteArrayToCKByteArray(env, jPrfData,1870(CK_BYTE_PTR *) PBKD2_PARAM_ADDR(pPrfData),1871PBKD2_PARAM_ADDR(ulPrfDataLen));1872if ((*env)->ExceptionCheck(env)) {1873goto cleanup;1874}1875if (ckParamPtr->version == PARAMS) {1876pUlPasswordLen = calloc(1, sizeof(CK_ULONG));1877if (pUlPasswordLen == NULL) {1878p11ThrowOutOfMemoryError(env, 0);1879goto cleanup;1880}1881ckParamPtr->params.v1.ulPasswordLen = pUlPasswordLen;1882} else {1883pUlPasswordLen = &ckParamPtr->params.v2.ulPasswordLen;1884}1885jCharArrayToCKUTF8CharArray(env, jPassword,1886(CK_CHAR_PTR *) PBKD2_PARAM_ADDR(pPassword),1887pUlPasswordLen);1888if ((*env)->ExceptionCheck(env)) {1889goto cleanup;1890}1891
1892if (pLength != NULL) {1893*pLength = (ckParamPtr->version == PARAMS ?1894sizeof(ckParamPtr->params.v1) :1895sizeof(ckParamPtr->params.v2));1896}1897// VersionedPbkd2ParamsPtr is equivalent to CK_PKCS5_PBKD2_PARAMS[2]_PTR1898return ckParamPtr;1899cleanup:1900FREE_VERSIONED_PBKD2_MEMBERS(ckParamPtr);1901free(ckParamPtr);1902return NULL;1903
1904}
1905
1906/*
1907* converts the Java CK_RSA_PKCS_PSS_PARAMS object to a CK_RSA_PKCS_PSS_PARAMS
1908* pointer
1909*
1910* @param env - used to call JNI funktions to get the Java classes and objects
1911* @param jParam - the Java CK_RSA_PKCS_PSS_PARAMS object to convert
1912* @param pLength - length of the allocated memory of the returned pointer
1913* @return pointer to the new CK_RSA_PKCS_PSS_PARAMS structure
1914*/
1915CK_RSA_PKCS_PSS_PARAMS_PTR
1916jRsaPkcsPssParamToCKRsaPkcsPssParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)1917{
1918CK_RSA_PKCS_PSS_PARAMS_PTR ckParamPtr;1919jclass jRsaPkcsPssParamsClass;1920jfieldID fieldID;1921jlong jHashAlg, jMgf, jSLen;1922
1923if (pLength != NULL) {1924*pLength = 0;1925}1926
1927// retrieve java values1928jRsaPkcsPssParamsClass = (*env)->FindClass(env, CLASS_RSA_PKCS_PSS_PARAMS);1929if (jRsaPkcsPssParamsClass == NULL) { return NULL; }1930fieldID = (*env)->GetFieldID(env, jRsaPkcsPssParamsClass, "hashAlg", "J");1931if (fieldID == NULL) { return NULL; }1932jHashAlg = (*env)->GetLongField(env, jParam, fieldID);1933fieldID = (*env)->GetFieldID(env, jRsaPkcsPssParamsClass, "mgf", "J");1934if (fieldID == NULL) { return NULL; }1935jMgf = (*env)->GetLongField(env, jParam, fieldID);1936fieldID = (*env)->GetFieldID(env, jRsaPkcsPssParamsClass, "sLen", "J");1937if (fieldID == NULL) { return NULL; }1938jSLen = (*env)->GetLongField(env, jParam, fieldID);1939
1940// allocate memory for CK_RSA_PKCS_PSS_PARAMS pointer1941ckParamPtr = calloc(1, sizeof(CK_RSA_PKCS_PSS_PARAMS));1942if (ckParamPtr == NULL) {1943p11ThrowOutOfMemoryError(env, 0);1944return NULL;1945}1946
1947// populate using java values1948ckParamPtr->hashAlg = jLongToCKULong(jHashAlg);1949ckParamPtr->mgf = jLongToCKULong(jMgf);1950ckParamPtr->sLen = jLongToCKULong(jSLen);1951TRACE1("DEBUG: jRsaPkcsPssParamToCKRsaPkcsPssParam, hashAlg=0x%lX\n", ckParamPtr->hashAlg);1952TRACE1("DEBUG: jRsaPkcsPssParamToCKRsaPkcsPssParam, mgf=0x%lX\n", ckParamPtr->mgf);1953TRACE1("DEBUG: jRsaPkcsPssParamToCKRsaPkcsPssParam, sLen=%lu\n", ckParamPtr->sLen);1954
1955if (pLength != NULL) {1956*pLength = sizeof(CK_RSA_PKCS_PSS_PARAMS);1957}1958return ckParamPtr;1959
1960}
1961
1962/*
1963* converts the Java CK_ECDH1_DERIVE_PARAMS object to a CK_ECDH1_DERIVE_PARAMS
1964* pointer
1965*
1966* @param env - used to call JNI funktions to get the Java classes and objects
1967* @param jParam - the Java CK_ECDH1_DERIVE_PARAMS object to convert
1968* @param pLength - length of the allocated memory of the returned pointer
1969* @return pointer to nthe new CK_ECDH1_DERIVE_PARAMS structure
1970*/
1971CK_ECDH1_DERIVE_PARAMS_PTR
1972jEcdh1DeriveParamToCKEcdh1DeriveParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)1973{
1974CK_ECDH1_DERIVE_PARAMS_PTR ckParamPtr;1975jclass jEcdh1DeriveParamsClass;1976jfieldID fieldID;1977jlong jLong;1978jobject jSharedData, jPublicData;1979
1980if (pLength != NULL) {1981*pLength = 0;1982}1983
1984// retrieve java values1985jEcdh1DeriveParamsClass = (*env)->FindClass(env, CLASS_ECDH1_DERIVE_PARAMS);1986if (jEcdh1DeriveParamsClass == NULL) { return NULL; }1987fieldID = (*env)->GetFieldID(env, jEcdh1DeriveParamsClass, "kdf", "J");1988if (fieldID == NULL) { return NULL; }1989jLong = (*env)->GetLongField(env, jParam, fieldID);1990fieldID = (*env)->GetFieldID(env, jEcdh1DeriveParamsClass, "pSharedData", "[B");1991if (fieldID == NULL) { return NULL; }1992jSharedData = (*env)->GetObjectField(env, jParam, fieldID);1993fieldID = (*env)->GetFieldID(env, jEcdh1DeriveParamsClass, "pPublicData", "[B");1994if (fieldID == NULL) { return NULL; }1995jPublicData = (*env)->GetObjectField(env, jParam, fieldID);1996
1997// allocate memory for CK_ECDH1_DERIVE_PARAMS pointer1998ckParamPtr = calloc(1, sizeof(CK_ECDH1_DERIVE_PARAMS));1999if (ckParamPtr == NULL) {2000p11ThrowOutOfMemoryError(env, 0);2001return NULL;2002}2003
2004// populate using java values2005ckParamPtr->kdf = jLongToCKULong(jLong);2006jByteArrayToCKByteArray(env, jSharedData, &(ckParamPtr->pSharedData),2007&(ckParamPtr->ulSharedDataLen));2008if ((*env)->ExceptionCheck(env)) {2009goto cleanup;2010}2011jByteArrayToCKByteArray(env, jPublicData, &(ckParamPtr->pPublicData),2012&(ckParamPtr->ulPublicDataLen));2013if ((*env)->ExceptionCheck(env)) {2014goto cleanup;2015}2016
2017if (pLength != NULL) {2018*pLength = sizeof(CK_ECDH1_DERIVE_PARAMS);2019}2020return ckParamPtr;2021cleanup:2022free(ckParamPtr->pSharedData);2023free(ckParamPtr->pPublicData);2024free(ckParamPtr);2025return NULL;2026}
2027
2028/*
2029* converts the Java CK_ECDH2_DERIVE_PARAMS object to a CK_ECDH2_DERIVE_PARAMS
2030* pointer
2031*
2032* @param env - used to call JNI funktions to get the Java classes and objects
2033* @param jParam - the Java CK_ECDH2_DERIVE_PARAMS object to convert
2034* @param pLength - length of the allocated memory of the returned pointer
2035* @return pointer to the new CK_ECDH2_DERIVE_PARAMS structure
2036*/
2037CK_ECDH2_DERIVE_PARAMS_PTR
2038jEcdh2DeriveParamToCKEcdh2DeriveParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)2039{
2040CK_ECDH2_DERIVE_PARAMS_PTR ckParamPtr;2041jclass jEcdh2DeriveParamsClass;2042jfieldID fieldID;2043jlong jKdf, jPrivateDataLen, jPrivateData;2044jobject jSharedData, jPublicData, jPublicData2;2045
2046// retrieve java values2047jEcdh2DeriveParamsClass = (*env)->FindClass(env, CLASS_ECDH2_DERIVE_PARAMS);2048if (jEcdh2DeriveParamsClass == NULL) { return NULL; }2049fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "kdf", "J");2050if (fieldID == NULL) { return NULL; }2051jKdf = (*env)->GetLongField(env, jParam, fieldID);2052fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "pSharedData", "[B");2053if (fieldID == NULL) { return NULL; }2054jSharedData = (*env)->GetObjectField(env, jParam, fieldID);2055fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "pPublicData", "[B");2056if (fieldID == NULL) { return NULL; }2057jPublicData = (*env)->GetObjectField(env, jParam, fieldID);2058fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "ulPrivateDataLen", "J");2059if (fieldID == NULL) { return NULL; }2060jPrivateDataLen = (*env)->GetLongField(env, jParam, fieldID);2061fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "hPrivateData", "J");2062if (fieldID == NULL) { return NULL; }2063jPrivateData = (*env)->GetLongField(env, jParam, fieldID);2064fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "pPublicData2", "[B");2065if (fieldID == NULL) { return NULL; }2066jPublicData2 = (*env)->GetObjectField(env, jParam, fieldID);2067
2068// allocate memory for CK_ECDH2_DERIVE_PARAMS pointer2069ckParamPtr = calloc(1, sizeof(CK_ECDH2_DERIVE_PARAMS));2070if (ckParamPtr == NULL) {2071p11ThrowOutOfMemoryError(env, 0);2072return NULL;2073}2074
2075// populate using java values2076ckParamPtr->kdf = jLongToCKULong(jKdf);2077jByteArrayToCKByteArray(env, jSharedData, &(ckParamPtr->pSharedData),2078&(ckParamPtr->ulSharedDataLen));2079if ((*env)->ExceptionCheck(env)) {2080goto cleanup;2081}2082jByteArrayToCKByteArray(env, jPublicData, &(ckParamPtr->pPublicData),2083&(ckParamPtr->ulPublicDataLen));2084if ((*env)->ExceptionCheck(env)) {2085goto cleanup;2086}2087ckParamPtr->ulPrivateDataLen = jLongToCKULong(jPrivateDataLen);2088ckParamPtr->hPrivateData = jLongToCKULong(jPrivateData);2089jByteArrayToCKByteArray(env, jPublicData2, &(ckParamPtr->pPublicData2),2090&(ckParamPtr->ulPublicDataLen2));2091if ((*env)->ExceptionCheck(env)) {2092goto cleanup;2093}2094
2095if (pLength != NULL) {2096*pLength = sizeof(CK_ECDH2_DERIVE_PARAMS);2097}2098return ckParamPtr;2099cleanup:2100free(ckParamPtr->pSharedData);2101free(ckParamPtr->pPublicData);2102free(ckParamPtr->pPublicData2);2103free(ckParamPtr);2104return NULL;2105}
2106
2107/*
2108* converts the Java CK_X9_42_DH1_DERIVE_PARAMS object to a
2109* CK_X9_42_DH1_DERIVE_PARAMS pointer
2110*
2111* @param env - used to call JNI funktions to get the Java classes and objects
2112* @param jParam - the Java CK_X9_42_DH1_DERIVE_PARAMS object to convert
2113* @param pLength - length of the allocated memory of the returned pointer
2114* @return pointer to the new CK_X9_42_DH1_DERIVE_PARAMS structure
2115*/
2116CK_X9_42_DH1_DERIVE_PARAMS_PTR
2117jX942Dh1DeriveParamToCKX942Dh1DeriveParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)2118{
2119CK_X9_42_DH1_DERIVE_PARAMS_PTR ckParamPtr;2120jclass jX942Dh1DeriveParamsClass;2121jfieldID fieldID;2122jlong jKdf;2123jobject jOtherInfo, jPublicData;2124
2125if (pLength != NULL) {2126*pLength = 0;2127}2128
2129// retrieve java values2130jX942Dh1DeriveParamsClass = (*env)->FindClass(env, CLASS_X9_42_DH1_DERIVE_PARAMS);2131if (jX942Dh1DeriveParamsClass == NULL) { return NULL; }2132fieldID = (*env)->GetFieldID(env, jX942Dh1DeriveParamsClass, "kdf", "J");2133if (fieldID == NULL) { return NULL; }2134jKdf = (*env)->GetLongField(env, jParam, fieldID);2135fieldID = (*env)->GetFieldID(env, jX942Dh1DeriveParamsClass, "pOtherInfo", "[B");2136if (fieldID == NULL) { return NULL; }2137jOtherInfo = (*env)->GetObjectField(env, jParam, fieldID);2138fieldID = (*env)->GetFieldID(env, jX942Dh1DeriveParamsClass, "pPublicData", "[B");2139if (fieldID == NULL) { return NULL; }2140jPublicData = (*env)->GetObjectField(env, jParam, fieldID);2141
2142// allocate memory for CK_X9_42_DH1_DERIVE_PARAMS pointer2143ckParamPtr = calloc(1, sizeof(CK_X9_42_DH1_DERIVE_PARAMS));2144if (ckParamPtr == NULL) {2145p11ThrowOutOfMemoryError(env, 0);2146return NULL;2147}2148
2149// populate using java values2150ckParamPtr->kdf = jLongToCKULong(jKdf);2151jByteArrayToCKByteArray(env, jOtherInfo, &(ckParamPtr->pOtherInfo),2152&(ckParamPtr->ulOtherInfoLen));2153if ((*env)->ExceptionCheck(env)) {2154goto cleanup;2155}2156jByteArrayToCKByteArray(env, jPublicData, &(ckParamPtr->pPublicData),2157&(ckParamPtr->ulPublicDataLen));2158if ((*env)->ExceptionCheck(env)) {2159goto cleanup;2160}2161
2162if (pLength != NULL) {2163*pLength = sizeof(CK_X9_42_DH1_DERIVE_PARAMS);2164}2165return ckParamPtr;2166cleanup:2167free(ckParamPtr->pOtherInfo);2168free(ckParamPtr->pPublicData);2169free(ckParamPtr);2170return NULL;2171}
2172
2173/*
2174* converts the Java CK_X9_42_DH2_DERIVE_PARAMS object to a
2175* CK_X9_42_DH2_DERIVE_PARAMS pointer
2176*
2177* @param env - used to call JNI funktions to get the Java classes and objects
2178* @param jParam - the Java CK_X9_42_DH2_DERIVE_PARAMS object to convert
2179* @param pLength - length of the allocated memory of the returned pointer
2180* @return pointer to the new CK_X9_42_DH2_DERIVE_PARAMS structure
2181*/
2182CK_X9_42_DH2_DERIVE_PARAMS_PTR
2183jX942Dh2DeriveParamToCKX942Dh2DeriveParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)2184{
2185CK_X9_42_DH2_DERIVE_PARAMS_PTR ckParamPtr;2186jclass jX942Dh2DeriveParamsClass;2187jfieldID fieldID;2188jlong jKdf, jPrivateDataLen, jPrivateData;2189jobject jOtherInfo, jPublicData, jPublicData2;2190
2191if (pLength != NULL) {2192*pLength = 0L;2193}2194
2195// retrieve java values2196jX942Dh2DeriveParamsClass = (*env)->FindClass(env, CLASS_X9_42_DH2_DERIVE_PARAMS);2197if (jX942Dh2DeriveParamsClass == NULL) { return NULL; }2198fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "kdf", "J");2199if (fieldID == NULL) { return NULL; }2200jKdf = (*env)->GetLongField(env, jParam, fieldID);2201fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "pOtherInfo", "[B");2202if (fieldID == NULL) { return NULL; }2203jOtherInfo = (*env)->GetObjectField(env, jParam, fieldID);2204fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "pPublicData", "[B");2205if (fieldID == NULL) { return NULL; }2206jPublicData = (*env)->GetObjectField(env, jParam, fieldID);2207fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "ulPrivateDataLen", "J");2208if (fieldID == NULL) { return NULL; }2209jPrivateDataLen = (*env)->GetLongField(env, jParam, fieldID);2210fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "hPrivateData", "J");2211if (fieldID == NULL) { return NULL; }2212jPrivateData = (*env)->GetLongField(env, jParam, fieldID);2213fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "pPublicData2", "[B");2214if (fieldID == NULL) { return NULL; }2215jPublicData2 = (*env)->GetObjectField(env, jParam, fieldID);2216
2217// allocate memory for CK_DATE pointer2218ckParamPtr = calloc(1, sizeof(CK_X9_42_DH2_DERIVE_PARAMS));2219if (ckParamPtr == NULL) {2220p11ThrowOutOfMemoryError(env, 0);2221return NULL;2222}2223
2224// populate using java values2225ckParamPtr->kdf = jLongToCKULong(jKdf);2226jByteArrayToCKByteArray(env, jOtherInfo, &(ckParamPtr->pOtherInfo),2227&(ckParamPtr->ulOtherInfoLen));2228if ((*env)->ExceptionCheck(env)) {2229goto cleanup;2230}2231jByteArrayToCKByteArray(env, jPublicData, &(ckParamPtr->pPublicData),2232&(ckParamPtr->ulPublicDataLen));2233if ((*env)->ExceptionCheck(env)) {2234goto cleanup;2235}2236ckParamPtr->ulPrivateDataLen = jLongToCKULong(jPrivateDataLen);2237ckParamPtr->hPrivateData = jLongToCKULong(jPrivateData);2238jByteArrayToCKByteArray(env, jPublicData2, &(ckParamPtr->pPublicData2),2239&(ckParamPtr->ulPublicDataLen2));2240if ((*env)->ExceptionCheck(env)) {2241goto cleanup;2242}2243
2244if (pLength != NULL) {2245*pLength = sizeof(CK_X9_42_DH2_DERIVE_PARAMS);2246}2247return ckParamPtr;2248cleanup:2249free(ckParamPtr->pOtherInfo);2250free(ckParamPtr->pPublicData);2251free(ckParamPtr->pPublicData2);2252free(ckParamPtr);2253return NULL;2254}
2255