jdk/src/jdk.crypto.token/share/native/libj2pkcs11/p11_convert.c
changeset 42693 6645de32a866
parent 32094 bebc489aace3
equal deleted inserted replaced
42692:97247477b481 42693:6645de32a866
       
     1 /*
       
     2  * Copyright (c) 2003, 2015, 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 
       
    71 void jMechanismParameterToCKMechanismParameterSlow(JNIEnv *env, jobject jParam, CK_VOID_PTR *ckpParamPtr, CK_ULONG *ckpLength);
       
    72 
       
    73 
       
    74 /*
       
    75  * converts a pointer to a CK_DATE structure into a Java CK_DATE Object.
       
    76  *
       
    77  * @param env - used to call JNI funktions to create the new Java object
       
    78  * @param ckpValue - the pointer to the CK_DATE structure
       
    79  * @return - the new Java CK_DATE object
       
    80  */
       
    81 jobject ckDatePtrToJDateObject(JNIEnv *env, const CK_DATE *ckpDate)
       
    82 {
       
    83     jclass jDateClass;
       
    84     jmethodID jCtrId;
       
    85     jobject jDateObject;
       
    86     jcharArray jYear;
       
    87     jcharArray jMonth;
       
    88     jcharArray jDay;
       
    89 
       
    90     /* load CK_DATE class */
       
    91     jDateClass = (*env)->FindClass(env, CLASS_DATE);
       
    92     if (jDateClass == NULL) { return NULL; }
       
    93 
       
    94     /* load CK_DATE constructor */
       
    95     jCtrId = (*env)->GetMethodID(env, jDateClass, "<init>", "([C[C[C)V");
       
    96     if (jCtrId == NULL) { return NULL; }
       
    97 
       
    98     /* prep all fields */
       
    99     jYear = ckCharArrayToJCharArray(env, (CK_CHAR_PTR)(ckpDate->year), 4);
       
   100     if (jYear == NULL) { return NULL; }
       
   101     jMonth = ckCharArrayToJCharArray(env, (CK_CHAR_PTR)(ckpDate->month), 2);
       
   102     if (jMonth == NULL) { return NULL; }
       
   103     jDay = ckCharArrayToJCharArray(env, (CK_CHAR_PTR)(ckpDate->day), 2);
       
   104     if (jDay == NULL) { return NULL; }
       
   105 
       
   106     /* create new CK_DATE object */
       
   107     jDateObject =
       
   108       (*env)->NewObject(env, jDateClass, jCtrId, jYear, jMonth, jDay);
       
   109     if (jDateObject == NULL) { return NULL; }
       
   110 
       
   111     /* free local references */
       
   112     (*env)->DeleteLocalRef(env, jDateClass);
       
   113     (*env)->DeleteLocalRef(env, jYear);
       
   114     (*env)->DeleteLocalRef(env, jMonth);
       
   115     (*env)->DeleteLocalRef(env, jDay);
       
   116 
       
   117     return jDateObject ;
       
   118 }
       
   119 
       
   120 /*
       
   121  * converts a pointer to a CK_VERSION structure into a Java CK_VERSION Object.
       
   122  *
       
   123  * @param env - used to call JNI funktions to create the new Java object
       
   124  * @param ckpVersion - the pointer to the CK_VERSION structure
       
   125  * @return - the new Java CK_VERSION object
       
   126  */
       
   127 jobject ckVersionPtrToJVersion(JNIEnv *env, const CK_VERSION_PTR ckpVersion)
       
   128 {
       
   129     jclass jVersionClass;
       
   130     jmethodID jCtrId;
       
   131     jobject jVersionObject;
       
   132     jint jMajor;
       
   133     jint jMinor;
       
   134 
       
   135     /* load CK_VERSION class */
       
   136     jVersionClass = (*env)->FindClass(env, CLASS_VERSION);
       
   137     if (jVersionClass == NULL) { return NULL; }
       
   138 
       
   139     /* load CK_VERSION constructor */
       
   140     jCtrId = (*env)->GetMethodID(env, jVersionClass, "<init>", "(II)V");
       
   141     if (jCtrId == NULL) { return NULL; }
       
   142 
       
   143     /* prep both fields */
       
   144     jMajor = ckpVersion->major;
       
   145     jMinor = ckpVersion->minor;
       
   146 
       
   147     /* create new CK_VERSION object */
       
   148     jVersionObject =
       
   149       (*env)->NewObject(env, jVersionClass, jCtrId, jMajor, jMinor);
       
   150     if (jVersionObject == NULL) { return NULL; }
       
   151 
       
   152     /* free local references */
       
   153     (*env)->DeleteLocalRef(env, jVersionClass);
       
   154 
       
   155     return jVersionObject ;
       
   156 }
       
   157 
       
   158 /*
       
   159  * converts a pointer to a CK_SESSION_INFO structure into a Java CK_SESSION_INFO Object.
       
   160  *
       
   161  * @param env - used to call JNI funktions to create the new Java object
       
   162  * @param ckpSessionInfo - the pointer to the CK_SESSION_INFO structure
       
   163  * @return - the new Java CK_SESSION_INFO object
       
   164  */
       
   165 jobject ckSessionInfoPtrToJSessionInfo(JNIEnv *env, const CK_SESSION_INFO_PTR ckpSessionInfo)
       
   166 {
       
   167     jclass jSessionInfoClass;
       
   168     jmethodID jCtrId;
       
   169     jobject jSessionInfoObject;
       
   170     jlong jSlotID;
       
   171     jlong jState;
       
   172     jlong jFlags;
       
   173     jlong jDeviceError;
       
   174 
       
   175     /* load CK_SESSION_INFO class */
       
   176     jSessionInfoClass = (*env)->FindClass(env, CLASS_SESSION_INFO);
       
   177     if (jSessionInfoClass == NULL) { return NULL; }
       
   178 
       
   179     /* load CK_SESSION_INFO constructor */
       
   180     jCtrId = (*env)->GetMethodID(env, jSessionInfoClass, "<init>", "(JJJJ)V");
       
   181     if (jCtrId == NULL) { return NULL; }
       
   182 
       
   183     /* prep all fields */
       
   184     jSlotID = ckULongToJLong(ckpSessionInfo->slotID);
       
   185     jState = ckULongToJLong(ckpSessionInfo->state);
       
   186     jFlags = ckULongToJLong(ckpSessionInfo->flags);
       
   187     jDeviceError = ckULongToJLong(ckpSessionInfo->ulDeviceError);
       
   188 
       
   189     /* create new CK_SESSION_INFO object */
       
   190     jSessionInfoObject =
       
   191       (*env)->NewObject(env, jSessionInfoClass, jCtrId, jSlotID, jState,
       
   192                         jFlags, jDeviceError);
       
   193     if (jSessionInfoObject == NULL) { return NULL; }
       
   194 
       
   195     /* free local references */
       
   196     (*env)->DeleteLocalRef(env, jSessionInfoClass);
       
   197 
       
   198     return jSessionInfoObject ;
       
   199 }
       
   200 
       
   201 /*
       
   202  * converts a pointer to a CK_ATTRIBUTE structure into a Java CK_ATTRIBUTE Object.
       
   203  *
       
   204  * @param env - used to call JNI funktions to create the new Java object
       
   205  * @param ckpAttribute - the pointer to the CK_ATTRIBUTE structure
       
   206  * @return - the new Java CK_ATTRIBUTE object
       
   207  */
       
   208 jobject ckAttributePtrToJAttribute(JNIEnv *env, const CK_ATTRIBUTE_PTR ckpAttribute)
       
   209 {
       
   210     jclass jAttributeClass;
       
   211     jmethodID jCtrId;
       
   212     jobject jAttributeObject;
       
   213     jlong jType;
       
   214     jobject jPValue = NULL;
       
   215 
       
   216     jAttributeClass = (*env)->FindClass(env, CLASS_ATTRIBUTE);
       
   217     if (jAttributeClass == NULL) { return NULL; }
       
   218 
       
   219     /* load CK_INFO constructor */
       
   220     jCtrId = (*env)->GetMethodID(env, jAttributeClass, "<init>", "(JLjava/lang/Object;)V");
       
   221     if (jCtrId == NULL) { return NULL; }
       
   222 
       
   223     /* prep both fields */
       
   224     jType = ckULongToJLong(ckpAttribute->type);
       
   225     jPValue = ckAttributeValueToJObject(env, ckpAttribute);
       
   226     if ((*env)->ExceptionCheck(env)) { return NULL; }
       
   227 
       
   228     /* create new CK_ATTRIBUTE object */
       
   229     jAttributeObject =
       
   230       (*env)->NewObject(env, jAttributeClass, jCtrId, jType, jPValue);
       
   231     if (jAttributeObject == NULL) { return NULL; }
       
   232 
       
   233     /* free local references */
       
   234     (*env)->DeleteLocalRef(env, jAttributeClass);
       
   235     (*env)->DeleteLocalRef(env, jPValue);
       
   236 
       
   237     return jAttributeObject;
       
   238 }
       
   239 
       
   240 
       
   241 /*
       
   242  * converts a Java CK_VERSION object into a pointer to a CK_VERSION structure
       
   243  *
       
   244  * @param env - used to call JNI funktions to get the values out of the Java object
       
   245  * @param jVersion - the Java CK_VERSION object to convert
       
   246  * @return - the pointer to the new CK_VERSION structure
       
   247  */
       
   248 CK_VERSION_PTR jVersionToCKVersionPtr(JNIEnv *env, jobject jVersion)
       
   249 {
       
   250     CK_VERSION_PTR ckpVersion;
       
   251     jclass jVersionClass;
       
   252     jfieldID jFieldID;
       
   253     jbyte jMajor, jMinor;
       
   254 
       
   255     if (jVersion == NULL) {
       
   256         return NULL;
       
   257     }
       
   258 
       
   259     /* get CK_VERSION class */
       
   260     jVersionClass = (*env)->GetObjectClass(env, jVersion);
       
   261     if (jVersionClass == NULL) { return NULL; }
       
   262 
       
   263     /* get Major */
       
   264     jFieldID = (*env)->GetFieldID(env, jVersionClass, "major", "B");
       
   265     if (jFieldID == NULL) { return NULL; }
       
   266     jMajor = (*env)->GetByteField(env, jVersion, jFieldID);
       
   267 
       
   268     /* get Minor */
       
   269     jFieldID = (*env)->GetFieldID(env, jVersionClass, "minor", "B");
       
   270     if (jFieldID == NULL) { return NULL; }
       
   271     jMinor = (*env)->GetByteField(env, jVersion, jFieldID);
       
   272 
       
   273     /* allocate memory for CK_VERSION pointer */
       
   274     ckpVersion = (CK_VERSION_PTR) malloc(sizeof(CK_VERSION));
       
   275     if (ckpVersion == NULL) {
       
   276         throwOutOfMemoryError(env, 0);
       
   277         return NULL;
       
   278     }
       
   279     ckpVersion->major = jByteToCKByte(jMajor);
       
   280     ckpVersion->minor = jByteToCKByte(jMinor);
       
   281 
       
   282     return ckpVersion ;
       
   283 }
       
   284 
       
   285 
       
   286 /*
       
   287  * converts a Java CK_DATE object into a pointer to a CK_DATE structure
       
   288  *
       
   289  * @param env - used to call JNI funktions to get the values out of the Java object
       
   290  * @param jVersion - the Java CK_DATE object to convert
       
   291  * @return - the pointer to the new CK_DATE structure
       
   292  */
       
   293 CK_DATE * jDateObjectPtrToCKDatePtr(JNIEnv *env, jobject jDate)
       
   294 {
       
   295     CK_DATE * ckpDate;
       
   296     CK_ULONG ckLength;
       
   297     jclass jDateClass;
       
   298     jfieldID jFieldID;
       
   299     jobject jYear, jMonth, jDay;
       
   300     jchar *jTempChars;
       
   301     CK_ULONG i;
       
   302 
       
   303     if (jDate == NULL) {
       
   304         return NULL;
       
   305     }
       
   306 
       
   307     /* get CK_DATE class */
       
   308     jDateClass = (*env)->FindClass(env, CLASS_DATE);
       
   309     if (jDateClass == NULL) { return NULL; }
       
   310 
       
   311     /* get Year */
       
   312     jFieldID = (*env)->GetFieldID(env, jDateClass, "year", "[C");
       
   313     if (jFieldID == NULL) { return NULL; }
       
   314     jYear = (*env)->GetObjectField(env, jDate, jFieldID);
       
   315 
       
   316     /* get Month */
       
   317     jFieldID = (*env)->GetFieldID(env, jDateClass, "month", "[C");
       
   318     if (jFieldID == NULL) { return NULL; }
       
   319     jMonth = (*env)->GetObjectField(env, jDate, jFieldID);
       
   320 
       
   321     /* get Day */
       
   322     jFieldID = (*env)->GetFieldID(env, jDateClass, "day", "[C");
       
   323     if (jFieldID == NULL) { return NULL; }
       
   324     jDay = (*env)->GetObjectField(env, jDate, jFieldID);
       
   325 
       
   326     /* allocate memory for CK_DATE pointer */
       
   327     ckpDate = (CK_DATE *) malloc(sizeof(CK_DATE));
       
   328     if (ckpDate == NULL) {
       
   329         throwOutOfMemoryError(env, 0);
       
   330         return NULL;
       
   331     }
       
   332 
       
   333     if (jYear == NULL) {
       
   334         ckpDate->year[0] = 0;
       
   335         ckpDate->year[1] = 0;
       
   336         ckpDate->year[2] = 0;
       
   337         ckpDate->year[3] = 0;
       
   338     } else {
       
   339         ckLength = (*env)->GetArrayLength(env, jYear);
       
   340         jTempChars = (jchar*) malloc((ckLength) * sizeof(jchar));
       
   341         if (jTempChars == NULL) {
       
   342             free(ckpDate);
       
   343             throwOutOfMemoryError(env, 0);
       
   344             return NULL;
       
   345         }
       
   346         (*env)->GetCharArrayRegion(env, jYear, 0, ckLength, jTempChars);
       
   347         if ((*env)->ExceptionCheck(env)) {
       
   348             free(ckpDate);
       
   349             free(jTempChars);
       
   350             return NULL;
       
   351         }
       
   352 
       
   353         for (i = 0; (i < ckLength) && (i < 4) ; i++) {
       
   354             ckpDate->year[i] = jCharToCKChar(jTempChars[i]);
       
   355         }
       
   356         free(jTempChars);
       
   357     }
       
   358 
       
   359     if (jMonth == NULL) {
       
   360         ckpDate->month[0] = 0;
       
   361         ckpDate->month[1] = 0;
       
   362     } else {
       
   363         ckLength = (*env)->GetArrayLength(env, jMonth);
       
   364         jTempChars = (jchar*) malloc((ckLength) * sizeof(jchar));
       
   365         if (jTempChars == NULL) {
       
   366             free(ckpDate);
       
   367             throwOutOfMemoryError(env, 0);
       
   368             return NULL;
       
   369         }
       
   370         (*env)->GetCharArrayRegion(env, jMonth, 0, ckLength, jTempChars);
       
   371         if ((*env)->ExceptionCheck(env)) {
       
   372             free(ckpDate);
       
   373             free(jTempChars);
       
   374             return NULL;
       
   375         }
       
   376 
       
   377         for (i = 0; (i < ckLength) && (i < 2) ; i++) {
       
   378             ckpDate->month[i] = jCharToCKChar(jTempChars[i]);
       
   379         }
       
   380         free(jTempChars);
       
   381     }
       
   382 
       
   383     if (jDay == NULL) {
       
   384         ckpDate->day[0] = 0;
       
   385         ckpDate->day[1] = 0;
       
   386     } else {
       
   387         ckLength = (*env)->GetArrayLength(env, jDay);
       
   388         jTempChars = (jchar*) malloc((ckLength) * sizeof(jchar));
       
   389         if (jTempChars == NULL) {
       
   390             free(ckpDate);
       
   391             throwOutOfMemoryError(env, 0);
       
   392             return NULL;
       
   393         }
       
   394         (*env)->GetCharArrayRegion(env, jDay, 0, ckLength, jTempChars);
       
   395         if ((*env)->ExceptionCheck(env)) {
       
   396             free(ckpDate);
       
   397             free(jTempChars);
       
   398             return NULL;
       
   399         }
       
   400 
       
   401         for (i = 0; (i < ckLength) && (i < 2) ; i++) {
       
   402             ckpDate->day[i] = jCharToCKChar(jTempChars[i]);
       
   403         }
       
   404         free(jTempChars);
       
   405     }
       
   406 
       
   407     return ckpDate ;
       
   408 }
       
   409 
       
   410 
       
   411 /*
       
   412  * converts a Java CK_ATTRIBUTE object into a CK_ATTRIBUTE structure
       
   413  *
       
   414  * @param env - used to call JNI funktions to get the values out of the Java object
       
   415  * @param jAttribute - the Java CK_ATTRIBUTE object to convert
       
   416  * @return - the new CK_ATTRIBUTE structure
       
   417  */
       
   418 CK_ATTRIBUTE jAttributeToCKAttribute(JNIEnv *env, jobject jAttribute)
       
   419 {
       
   420     CK_ATTRIBUTE ckAttribute;
       
   421     jclass jAttributeClass;
       
   422     jfieldID jFieldID;
       
   423     jlong jType;
       
   424     jobject jPValue;
       
   425     memset(&ckAttribute, 0, sizeof(CK_ATTRIBUTE));
       
   426 
       
   427     // TBD: what if jAttribute == NULL?!
       
   428 
       
   429     TRACE0("\nDEBUG: jAttributeToCKAttribute");
       
   430     /* get CK_ATTRIBUTE class */
       
   431     TRACE0(", getting attribute object class");
       
   432     jAttributeClass = (*env)->GetObjectClass(env, jAttribute);
       
   433     if (jAttributeClass == NULL) { return ckAttribute; }
       
   434 
       
   435     /* get type */
       
   436     TRACE0(", getting type field");
       
   437     jFieldID = (*env)->GetFieldID(env, jAttributeClass, "type", "J");
       
   438     if (jFieldID == NULL) { return ckAttribute; }
       
   439     jType = (*env)->GetLongField(env, jAttribute, jFieldID);
       
   440     TRACE1(", type=0x%X", jType);
       
   441 
       
   442     /* get pValue */
       
   443     TRACE0(", getting pValue field");
       
   444     jFieldID = (*env)->GetFieldID(env, jAttributeClass, "pValue", "Ljava/lang/Object;");
       
   445     if (jFieldID == NULL) { return ckAttribute; }
       
   446     jPValue = (*env)->GetObjectField(env, jAttribute, jFieldID);
       
   447     TRACE1(", pValue=%p", jPValue);
       
   448 
       
   449     ckAttribute.type = jLongToCKULong(jType);
       
   450     TRACE0(", converting pValue to primitive object");
       
   451 
       
   452     /* convert the Java pValue object to a CK-type pValue pointer */
       
   453     jObjectToPrimitiveCKObjectPtrPtr(env, jPValue, &(ckAttribute.pValue), &(ckAttribute.ulValueLen));
       
   454 
       
   455     TRACE0("\nFINISHED\n");
       
   456 
       
   457     return ckAttribute ;
       
   458 }
       
   459 
       
   460 /*
       
   461  * converts the Java CK_SSL3_MASTER_KEY_DERIVE_PARAMS object to a
       
   462  * CK_SSL3_MASTER_KEY_DERIVE_PARAMS structure
       
   463  *
       
   464  * @param env - used to call JNI funktions to get the Java classes and objects
       
   465  * @param jParam - the Java CK_SSL3_MASTER_KEY_DERIVE_PARAMS object to convert
       
   466  * @return - the new CK_SSL3_MASTER_KEY_DERIVE_PARAMS structure
       
   467  */
       
   468 CK_SSL3_MASTER_KEY_DERIVE_PARAMS jSsl3MasterKeyDeriveParamToCKSsl3MasterKeyDeriveParam(JNIEnv *env, jobject jParam)
       
   469 {
       
   470     // XXX don't return structs
       
   471     // XXX prefetch class and field ids
       
   472     jclass jSsl3MasterKeyDeriveParamsClass;
       
   473     CK_SSL3_MASTER_KEY_DERIVE_PARAMS ckParam;
       
   474     jfieldID fieldID;
       
   475     jclass jSsl3RandomDataClass;
       
   476     jobject jRandomInfo, jRIClientRandom, jRIServerRandom, jVersion;
       
   477     memset(&ckParam, 0, sizeof(CK_SSL3_MASTER_KEY_DERIVE_PARAMS));
       
   478 
       
   479     /* get RandomInfo */
       
   480     jSsl3MasterKeyDeriveParamsClass = (*env)->FindClass(env, CLASS_SSL3_MASTER_KEY_DERIVE_PARAMS);
       
   481     if (jSsl3MasterKeyDeriveParamsClass == NULL) { return ckParam; }
       
   482     fieldID = (*env)->GetFieldID(env, jSsl3MasterKeyDeriveParamsClass, "RandomInfo", "Lsun/security/pkcs11/wrapper/CK_SSL3_RANDOM_DATA;");
       
   483     if (fieldID == NULL) { return ckParam; }
       
   484     jRandomInfo = (*env)->GetObjectField(env, jParam, fieldID);
       
   485 
       
   486     /* get pClientRandom and ulClientRandomLength out of RandomInfo */
       
   487     jSsl3RandomDataClass = (*env)->FindClass(env, CLASS_SSL3_RANDOM_DATA);
       
   488     if (jSsl3RandomDataClass == NULL) { return ckParam; }
       
   489     fieldID = (*env)->GetFieldID(env, jSsl3RandomDataClass, "pClientRandom", "[B");
       
   490     if (fieldID == NULL) { return ckParam; }
       
   491     jRIClientRandom = (*env)->GetObjectField(env, jRandomInfo, fieldID);
       
   492 
       
   493     /* get pServerRandom and ulServerRandomLength out of RandomInfo */
       
   494     fieldID = (*env)->GetFieldID(env, jSsl3RandomDataClass, "pServerRandom", "[B");
       
   495     if (fieldID == NULL) { return ckParam; }
       
   496     jRIServerRandom = (*env)->GetObjectField(env, jRandomInfo, fieldID);
       
   497 
       
   498     /* get pVersion */
       
   499     fieldID = (*env)->GetFieldID(env, jSsl3MasterKeyDeriveParamsClass, "pVersion",  "Lsun/security/pkcs11/wrapper/CK_VERSION;");
       
   500     if (fieldID == NULL) { return ckParam; }
       
   501     jVersion = (*env)->GetObjectField(env, jParam, fieldID);
       
   502 
       
   503     /* populate java values */
       
   504     ckParam.pVersion = jVersionToCKVersionPtr(env, jVersion);
       
   505     if ((*env)->ExceptionCheck(env)) { return ckParam; }
       
   506     jByteArrayToCKByteArray(env, jRIClientRandom, &(ckParam.RandomInfo.pClientRandom), &(ckParam.RandomInfo.ulClientRandomLen));
       
   507     if ((*env)->ExceptionCheck(env)) {
       
   508         free(ckParam.pVersion);
       
   509         return ckParam;
       
   510     }
       
   511     jByteArrayToCKByteArray(env, jRIServerRandom, &(ckParam.RandomInfo.pServerRandom), &(ckParam.RandomInfo.ulServerRandomLen));
       
   512     if ((*env)->ExceptionCheck(env)) {
       
   513         free(ckParam.pVersion);
       
   514         free(ckParam.RandomInfo.pClientRandom);
       
   515         return ckParam;
       
   516     }
       
   517 
       
   518     return ckParam ;
       
   519 }
       
   520 
       
   521 
       
   522 /*
       
   523  * converts the Java CK_TLS_PRF_PARAMS object to a CK_TLS_PRF_PARAMS structure
       
   524  */
       
   525 CK_TLS_PRF_PARAMS jTlsPrfParamsToCKTlsPrfParam(JNIEnv *env, jobject jParam)
       
   526 {
       
   527     jclass jTlsPrfParamsClass;
       
   528     CK_TLS_PRF_PARAMS ckParam;
       
   529     jfieldID fieldID;
       
   530     jobject jSeed, jLabel, jOutput;
       
   531     memset(&ckParam, 0, sizeof(CK_TLS_PRF_PARAMS));
       
   532 
       
   533     // TBD: what if jParam == NULL?!
       
   534 
       
   535     /* get pSeed */
       
   536     jTlsPrfParamsClass = (*env)->FindClass(env, CLASS_TLS_PRF_PARAMS);
       
   537     if (jTlsPrfParamsClass == NULL) { return ckParam; }
       
   538     fieldID = (*env)->GetFieldID(env, jTlsPrfParamsClass, "pSeed", "[B");
       
   539     if (fieldID == NULL) { return ckParam; }
       
   540     jSeed = (*env)->GetObjectField(env, jParam, fieldID);
       
   541 
       
   542     /* get pLabel */
       
   543     fieldID = (*env)->GetFieldID(env, jTlsPrfParamsClass, "pLabel", "[B");
       
   544     if (fieldID == NULL) { return ckParam; }
       
   545     jLabel = (*env)->GetObjectField(env, jParam, fieldID);
       
   546 
       
   547     /* get pOutput */
       
   548     fieldID = (*env)->GetFieldID(env, jTlsPrfParamsClass, "pOutput", "[B");
       
   549     if (fieldID == NULL) { return ckParam; }
       
   550     jOutput = (*env)->GetObjectField(env, jParam, fieldID);
       
   551 
       
   552     /* populate java values */
       
   553     jByteArrayToCKByteArray(env, jSeed, &(ckParam.pSeed), &(ckParam.ulSeedLen));
       
   554     if ((*env)->ExceptionCheck(env)) { return ckParam; }
       
   555     jByteArrayToCKByteArray(env, jLabel, &(ckParam.pLabel), &(ckParam.ulLabelLen));
       
   556     if ((*env)->ExceptionCheck(env)) {
       
   557         free(ckParam.pSeed);
       
   558         return ckParam;
       
   559     }
       
   560     ckParam.pulOutputLen = malloc(sizeof(CK_ULONG));
       
   561     if (ckParam.pulOutputLen == NULL) {
       
   562         free(ckParam.pSeed);
       
   563         free(ckParam.pLabel);
       
   564         throwOutOfMemoryError(env, 0);
       
   565         return ckParam;
       
   566     }
       
   567     jByteArrayToCKByteArray(env, jOutput, &(ckParam.pOutput), ckParam.pulOutputLen);
       
   568     if ((*env)->ExceptionCheck(env)) {
       
   569         free(ckParam.pSeed);
       
   570         free(ckParam.pLabel);
       
   571         free(ckParam.pulOutputLen);
       
   572         return ckParam;
       
   573     }
       
   574 
       
   575     return ckParam ;
       
   576 }
       
   577 
       
   578 /*
       
   579  * converts the Java CK_SSL3_KEY_MAT_PARAMS object to a CK_SSL3_KEY_MAT_PARAMS structure
       
   580  *
       
   581  * @param env - used to call JNI funktions to get the Java classes and objects
       
   582  * @param jParam - the Java CK_SSL3_KEY_MAT_PARAMS object to convert
       
   583  * @return - the new CK_SSL3_KEY_MAT_PARAMS structure
       
   584  */
       
   585 CK_SSL3_KEY_MAT_PARAMS jSsl3KeyMatParamToCKSsl3KeyMatParam(JNIEnv *env, jobject jParam)
       
   586 {
       
   587     // XXX don't return structs
       
   588     // XXX prefetch class and field ids
       
   589     jclass jSsl3KeyMatParamsClass, jSsl3RandomDataClass, jSsl3KeyMatOutClass;
       
   590     CK_SSL3_KEY_MAT_PARAMS ckParam;
       
   591     jfieldID fieldID;
       
   592     jlong jMacSizeInBits, jKeySizeInBits, jIVSizeInBits;
       
   593     jboolean jIsExport;
       
   594     jobject jRandomInfo, jRIClientRandom, jRIServerRandom;
       
   595     jobject jReturnedKeyMaterial, jRMIvClient, jRMIvServer;
       
   596     CK_ULONG ckTemp;
       
   597     memset(&ckParam, 0, sizeof(CK_SSL3_KEY_MAT_PARAMS));
       
   598 
       
   599     /* get ulMacSizeInBits */
       
   600     jSsl3KeyMatParamsClass = (*env)->FindClass(env, CLASS_SSL3_KEY_MAT_PARAMS);
       
   601     if (jSsl3KeyMatParamsClass == NULL) { return ckParam; }
       
   602     fieldID = (*env)->GetFieldID(env, jSsl3KeyMatParamsClass, "ulMacSizeInBits", "J");
       
   603     if (fieldID == NULL) { return ckParam; }
       
   604     jMacSizeInBits = (*env)->GetLongField(env, jParam, fieldID);
       
   605 
       
   606     /* get ulKeySizeInBits */
       
   607     fieldID = (*env)->GetFieldID(env, jSsl3KeyMatParamsClass, "ulKeySizeInBits", "J");
       
   608     if (fieldID == NULL) { return ckParam; }
       
   609     jKeySizeInBits = (*env)->GetLongField(env, jParam, fieldID);
       
   610 
       
   611     /* get ulIVSizeInBits */
       
   612     fieldID = (*env)->GetFieldID(env, jSsl3KeyMatParamsClass, "ulIVSizeInBits", "J");
       
   613     if (fieldID == NULL) { return ckParam; }
       
   614     jIVSizeInBits = (*env)->GetLongField(env, jParam, fieldID);
       
   615 
       
   616     /* get bIsExport */
       
   617     fieldID = (*env)->GetFieldID(env, jSsl3KeyMatParamsClass, "bIsExport", "Z");
       
   618     if (fieldID == NULL) { return ckParam; }
       
   619     jIsExport = (*env)->GetBooleanField(env, jParam, fieldID);
       
   620 
       
   621     /* get RandomInfo */
       
   622     jSsl3RandomDataClass = (*env)->FindClass(env, CLASS_SSL3_RANDOM_DATA);
       
   623     if (jSsl3RandomDataClass == NULL) { return ckParam; }
       
   624     fieldID = (*env)->GetFieldID(env, jSsl3KeyMatParamsClass, "RandomInfo",  "Lsun/security/pkcs11/wrapper/CK_SSL3_RANDOM_DATA;");
       
   625     if (fieldID == NULL) { return ckParam; }
       
   626     jRandomInfo = (*env)->GetObjectField(env, jParam, fieldID);
       
   627 
       
   628     /* get pClientRandom and ulClientRandomLength out of RandomInfo */
       
   629     fieldID = (*env)->GetFieldID(env, jSsl3RandomDataClass, "pClientRandom", "[B");
       
   630     if (fieldID == NULL) { return ckParam; }
       
   631     jRIClientRandom = (*env)->GetObjectField(env, jRandomInfo, fieldID);
       
   632 
       
   633     /* get pServerRandom and ulServerRandomLength out of RandomInfo */
       
   634     fieldID = (*env)->GetFieldID(env, jSsl3RandomDataClass, "pServerRandom", "[B");
       
   635     if (fieldID == NULL) { return ckParam; }
       
   636     jRIServerRandom = (*env)->GetObjectField(env, jRandomInfo, fieldID);
       
   637 
       
   638     /* get pReturnedKeyMaterial */
       
   639     jSsl3KeyMatOutClass = (*env)->FindClass(env, CLASS_SSL3_KEY_MAT_OUT);
       
   640     if (jSsl3KeyMatOutClass == NULL) { return ckParam; }
       
   641     fieldID = (*env)->GetFieldID(env, jSsl3KeyMatParamsClass, "pReturnedKeyMaterial",  "Lsun/security/pkcs11/wrapper/CK_SSL3_KEY_MAT_OUT;");
       
   642     if (fieldID == NULL) { return ckParam; }
       
   643     jReturnedKeyMaterial = (*env)->GetObjectField(env, jParam, fieldID);
       
   644 
       
   645     /* get pIVClient out of pReturnedKeyMaterial */
       
   646     fieldID = (*env)->GetFieldID(env, jSsl3KeyMatOutClass, "pIVClient", "[B");
       
   647     if (fieldID == NULL) { return ckParam; }
       
   648     jRMIvClient = (*env)->GetObjectField(env, jReturnedKeyMaterial, fieldID);
       
   649 
       
   650     /* get pIVServer out of pReturnedKeyMaterial */
       
   651     fieldID = (*env)->GetFieldID(env, jSsl3KeyMatOutClass, "pIVServer", "[B");
       
   652     if (fieldID == NULL) { return ckParam; }
       
   653     jRMIvServer = (*env)->GetObjectField(env, jReturnedKeyMaterial, fieldID);
       
   654 
       
   655     /* populate java values */
       
   656     ckParam.ulMacSizeInBits = jLongToCKULong(jMacSizeInBits);
       
   657     ckParam.ulKeySizeInBits = jLongToCKULong(jKeySizeInBits);
       
   658     ckParam.ulIVSizeInBits = jLongToCKULong(jIVSizeInBits);
       
   659     ckParam.bIsExport = jBooleanToCKBBool(jIsExport);
       
   660     jByteArrayToCKByteArray(env, jRIClientRandom, &(ckParam.RandomInfo.pClientRandom), &(ckParam.RandomInfo.ulClientRandomLen));
       
   661     if ((*env)->ExceptionCheck(env)) { return ckParam; }
       
   662     jByteArrayToCKByteArray(env, jRIServerRandom, &(ckParam.RandomInfo.pServerRandom), &(ckParam.RandomInfo.ulServerRandomLen));
       
   663     if ((*env)->ExceptionCheck(env)) {
       
   664         free(ckParam.RandomInfo.pClientRandom);
       
   665         return ckParam;
       
   666     }
       
   667     /* allocate memory for pRetrunedKeyMaterial */
       
   668     ckParam.pReturnedKeyMaterial = (CK_SSL3_KEY_MAT_OUT_PTR) malloc(sizeof(CK_SSL3_KEY_MAT_OUT));
       
   669     if (ckParam.pReturnedKeyMaterial == NULL) {
       
   670         free(ckParam.RandomInfo.pClientRandom);
       
   671         free(ckParam.RandomInfo.pServerRandom);
       
   672         throwOutOfMemoryError(env, 0);
       
   673         return ckParam;
       
   674     }
       
   675 
       
   676     // the handles are output params only, no need to fetch them from Java
       
   677     ckParam.pReturnedKeyMaterial->hClientMacSecret = 0;
       
   678     ckParam.pReturnedKeyMaterial->hServerMacSecret = 0;
       
   679     ckParam.pReturnedKeyMaterial->hClientKey = 0;
       
   680     ckParam.pReturnedKeyMaterial->hServerKey = 0;
       
   681 
       
   682     jByteArrayToCKByteArray(env, jRMIvClient, &(ckParam.pReturnedKeyMaterial->pIVClient), &ckTemp);
       
   683     if ((*env)->ExceptionCheck(env)) {
       
   684         free(ckParam.RandomInfo.pClientRandom);
       
   685         free(ckParam.RandomInfo.pServerRandom);
       
   686         free(ckParam.pReturnedKeyMaterial);
       
   687         return ckParam;
       
   688     }
       
   689     jByteArrayToCKByteArray(env, jRMIvServer, &(ckParam.pReturnedKeyMaterial->pIVServer), &ckTemp);
       
   690     if ((*env)->ExceptionCheck(env)) {
       
   691         free(ckParam.RandomInfo.pClientRandom);
       
   692         free(ckParam.RandomInfo.pServerRandom);
       
   693         free(ckParam.pReturnedKeyMaterial->pIVClient);
       
   694         free(ckParam.pReturnedKeyMaterial);
       
   695         return ckParam;
       
   696     }
       
   697 
       
   698     return ckParam ;
       
   699 }
       
   700 
       
   701 /*
       
   702  * converts the Java CK_AES_CTR_PARAMS object to a CK_AES_CTR_PARAMS structure
       
   703  *
       
   704  * @param env - used to call JNI funktions to get the Java classes and objects
       
   705  * @param jParam - the Java CK_AES_CTR_PARAMS object to convert
       
   706  * @param ckpParam - pointer to the new CK_AES_CTR_PARAMS structure
       
   707  */
       
   708 void jAesCtrParamsToCKAesCtrParam(JNIEnv *env, jobject jParam,
       
   709                                   CK_AES_CTR_PARAMS_PTR ckpParam) {
       
   710     jclass jAesCtrParamsClass;
       
   711     jfieldID fieldID;
       
   712     jlong jCounterBits;
       
   713     jobject jCb;
       
   714     CK_BYTE_PTR ckBytes;
       
   715     CK_ULONG ckTemp;
       
   716 
       
   717     /* get ulCounterBits */
       
   718     jAesCtrParamsClass = (*env)->FindClass(env, CLASS_AES_CTR_PARAMS);
       
   719     if (jAesCtrParamsClass == NULL) { return; }
       
   720     fieldID = (*env)->GetFieldID(env, jAesCtrParamsClass, "ulCounterBits", "J");
       
   721     if (fieldID == NULL) { return; }
       
   722     jCounterBits = (*env)->GetLongField(env, jParam, fieldID);
       
   723 
       
   724     /* get cb */
       
   725     fieldID = (*env)->GetFieldID(env, jAesCtrParamsClass, "cb", "[B");
       
   726     if (fieldID == NULL) { return; }
       
   727     jCb = (*env)->GetObjectField(env, jParam, fieldID);
       
   728 
       
   729     /* populate java values */
       
   730     ckpParam->ulCounterBits = jLongToCKULong(jCounterBits);
       
   731     jByteArrayToCKByteArray(env, jCb, &ckBytes, &ckTemp);
       
   732     if ((*env)->ExceptionCheck(env)) { return; }
       
   733     if (ckTemp != 16) {
       
   734         TRACE1("ERROR: WRONG CTR IV LENGTH %d", ckTemp);
       
   735     } else {
       
   736         memcpy(ckpParam->cb, ckBytes, ckTemp);
       
   737         free(ckBytes);
       
   738     }
       
   739 }
       
   740 
       
   741 /*
       
   742  * converts a Java CK_MECHANISM object into a CK_MECHANISM structure
       
   743  *
       
   744  * @param env - used to call JNI funktions to get the values out of the Java object
       
   745  * @param jMechanism - the Java CK_MECHANISM object to convert
       
   746  * @return - the new CK_MECHANISM structure
       
   747  */
       
   748 void jMechanismToCKMechanism(JNIEnv *env, jobject jMechanism, CK_MECHANISM_PTR ckMechanismPtr)
       
   749 {
       
   750     jlong jMechanismType = (*env)->GetLongField(env, jMechanism, mech_mechanismID);
       
   751     jobject jParameter = (*env)->GetObjectField(env, jMechanism, mech_pParameterID);
       
   752 
       
   753     (*ckMechanismPtr).mechanism = jLongToCKULong(jMechanismType);
       
   754 
       
   755     /* convert the specific Java mechanism parameter object to a pointer to a CK-type mechanism
       
   756      * structure
       
   757      */
       
   758     if (jParameter == NULL) {
       
   759         (*ckMechanismPtr).pParameter = NULL;
       
   760         (*ckMechanismPtr).ulParameterLen = 0;
       
   761     } else {
       
   762         jMechanismParameterToCKMechanismParameter(env, jParameter, &(*ckMechanismPtr).pParameter, &(*ckMechanismPtr).ulParameterLen);
       
   763     }
       
   764 }
       
   765 
       
   766 /*
       
   767  * the following functions convert Attribute and Mechanism value pointers
       
   768  *
       
   769  * jobject ckAttributeValueToJObject(JNIEnv *env,
       
   770  *                                   const CK_ATTRIBUTE_PTR ckpAttribute);
       
   771  *
       
   772  * void jObjectToPrimitiveCKObjectPtrPtr(JNIEnv *env,
       
   773  *                                       jobject jObject,
       
   774  *                                       CK_VOID_PTR *ckpObjectPtr,
       
   775  *                                       CK_ULONG *pLength);
       
   776  *
       
   777  * void jMechanismParameterToCKMechanismParameter(JNIEnv *env,
       
   778  *                                                jobject jParam,
       
   779  *                                                CK_VOID_PTR *ckpParamPtr,
       
   780  *                                                CK_ULONG *ckpLength);
       
   781  *
       
   782  * These functions are used if a PKCS#11 mechanism or attribute structure gets
       
   783  * convertet to a Java attribute or mechanism object or vice versa.
       
   784  *
       
   785  * ckAttributeValueToJObject converts a PKCS#11 attribute value pointer to a Java
       
   786  * object depending on the type of the Attribute. A PKCS#11 attribute value can
       
   787  * be a CK_ULONG, CK_BYTE[], CK_CHAR[], big integer, CK_BBOOL, CK_UTF8CHAR[],
       
   788  * CK_DATE or CK_FLAGS that gets converted to a corresponding Java object.
       
   789  *
       
   790  * jObjectToPrimitiveCKObjectPtrPtr is used by jAttributeToCKAttributePtr for
       
   791  * converting the Java attribute value to a PKCS#11 attribute value pointer.
       
   792  * For now only primitive datatypes and arrays of primitive datatypes can get
       
   793  * converted. Otherwise this function throws a PKCS#11Exception with the
       
   794  * errorcode CKR_VENDOR_DEFINED.
       
   795  *
       
   796  * jMechanismParameterToCKMechanismParameter converts a Java mechanism parameter
       
   797  * to a PKCS#11 mechanism parameter. First this function determines what mechanism
       
   798  * parameter the Java object is, then it allocates the memory for the new PKCS#11
       
   799  * structure and calls the corresponding function to convert the Java object to
       
   800  * a PKCS#11 mechanism parameter structure.
       
   801  */
       
   802 
       
   803 /*
       
   804  * converts the pValue of a CK_ATTRIBUTE structure into a Java Object by checking the type
       
   805  * of the attribute.
       
   806  *
       
   807  * @param env - used to call JNI funktions to create the new Java object
       
   808  * @param ckpAttribute - the pointer to the CK_ATTRIBUTE structure that contains the type
       
   809  *                       and the pValue to convert
       
   810  * @return - the new Java object of the CK-type pValue
       
   811  */
       
   812 jobject ckAttributeValueToJObject(JNIEnv *env, const CK_ATTRIBUTE_PTR ckpAttribute)
       
   813 {
       
   814     jint jValueLength;
       
   815     jobject jValueObject = NULL;
       
   816 
       
   817     jValueLength = ckULongToJInt(ckpAttribute->ulValueLen);
       
   818 
       
   819     if ((jValueLength <= 0) || (ckpAttribute->pValue == NULL)) {
       
   820         return NULL ;
       
   821     }
       
   822 
       
   823     switch(ckpAttribute->type) {
       
   824         case CKA_CLASS:
       
   825             /* value CK_OBJECT_CLASS, defacto a CK_ULONG */
       
   826         case CKA_KEY_TYPE:
       
   827             /* value CK_KEY_TYPE, defacto a CK_ULONG */
       
   828         case CKA_CERTIFICATE_TYPE:
       
   829             /* value CK_CERTIFICATE_TYPE, defacto a CK_ULONG */
       
   830         case CKA_HW_FEATURE_TYPE:
       
   831             /* value CK_HW_FEATURE_TYPE, defacto a CK_ULONG */
       
   832         case CKA_MODULUS_BITS:
       
   833         case CKA_VALUE_BITS:
       
   834         case CKA_VALUE_LEN:
       
   835         case CKA_KEY_GEN_MECHANISM:
       
   836         case CKA_PRIME_BITS:
       
   837         case CKA_SUB_PRIME_BITS:
       
   838             /* value CK_ULONG */
       
   839             jValueObject = ckULongPtrToJLongObject(env, (CK_ULONG*) ckpAttribute->pValue);
       
   840             break;
       
   841 
       
   842             /* can be CK_BYTE[],CK_CHAR[] or big integer; defacto always CK_BYTE[] */
       
   843         case CKA_VALUE:
       
   844         case CKA_OBJECT_ID:
       
   845         case CKA_SUBJECT:
       
   846         case CKA_ID:
       
   847         case CKA_ISSUER:
       
   848         case CKA_SERIAL_NUMBER:
       
   849         case CKA_OWNER:
       
   850         case CKA_AC_ISSUER:
       
   851         case CKA_ATTR_TYPES:
       
   852         case CKA_ECDSA_PARAMS:
       
   853             /* CKA_EC_PARAMS is the same, these two are equivalent */
       
   854         case CKA_EC_POINT:
       
   855         case CKA_PRIVATE_EXPONENT:
       
   856         case CKA_PRIME_1:
       
   857         case CKA_PRIME_2:
       
   858         case CKA_EXPONENT_1:
       
   859         case CKA_EXPONENT_2:
       
   860         case CKA_COEFFICIENT:
       
   861             /* value CK_BYTE[] */
       
   862             jValueObject = ckByteArrayToJByteArray(env, (CK_BYTE*) ckpAttribute->pValue, jValueLength);
       
   863             break;
       
   864 
       
   865         case CKA_RESET_ON_INIT:
       
   866         case CKA_HAS_RESET:
       
   867         case CKA_TOKEN:
       
   868         case CKA_PRIVATE:
       
   869         case CKA_MODIFIABLE:
       
   870         case CKA_DERIVE:
       
   871         case CKA_LOCAL:
       
   872         case CKA_ENCRYPT:
       
   873         case CKA_VERIFY:
       
   874         case CKA_VERIFY_RECOVER:
       
   875         case CKA_WRAP:
       
   876         case CKA_SENSITIVE:
       
   877         case CKA_SECONDARY_AUTH:
       
   878         case CKA_DECRYPT:
       
   879         case CKA_SIGN:
       
   880         case CKA_SIGN_RECOVER:
       
   881         case CKA_UNWRAP:
       
   882         case CKA_EXTRACTABLE:
       
   883         case CKA_ALWAYS_SENSITIVE:
       
   884         case CKA_NEVER_EXTRACTABLE:
       
   885         case CKA_TRUSTED:
       
   886             /* value CK_BBOOL */
       
   887             jValueObject = ckBBoolPtrToJBooleanObject(env, (CK_BBOOL*) ckpAttribute->pValue);
       
   888             break;
       
   889 
       
   890         case CKA_LABEL:
       
   891         case CKA_APPLICATION:
       
   892             /* value RFC 2279 (UTF-8) string */
       
   893             jValueObject = ckUTF8CharArrayToJCharArray(env, (CK_UTF8CHAR*) ckpAttribute->pValue, jValueLength);
       
   894             break;
       
   895 
       
   896         case CKA_START_DATE:
       
   897         case CKA_END_DATE:
       
   898             /* value CK_DATE */
       
   899             jValueObject = ckDatePtrToJDateObject(env, (CK_DATE*) ckpAttribute->pValue);
       
   900             break;
       
   901 
       
   902         case CKA_MODULUS:
       
   903         case CKA_PUBLIC_EXPONENT:
       
   904         case CKA_PRIME:
       
   905         case CKA_SUBPRIME:
       
   906         case CKA_BASE:
       
   907             /* value big integer, i.e. CK_BYTE[] */
       
   908             jValueObject = ckByteArrayToJByteArray(env, (CK_BYTE*) ckpAttribute->pValue, jValueLength);
       
   909             break;
       
   910 
       
   911         case CKA_AUTH_PIN_FLAGS:
       
   912             jValueObject = ckULongPtrToJLongObject(env, (CK_ULONG*) ckpAttribute->pValue);
       
   913             /* value FLAGS, defacto a CK_ULONG */
       
   914             break;
       
   915 
       
   916         case CKA_VENDOR_DEFINED:
       
   917             /* we make a CK_BYTE[] out of this */
       
   918             jValueObject = ckByteArrayToJByteArray(env, (CK_BYTE*) ckpAttribute->pValue, jValueLength);
       
   919             break;
       
   920 
       
   921         // Netscape trust attributes
       
   922         case CKA_NETSCAPE_TRUST_SERVER_AUTH:
       
   923         case CKA_NETSCAPE_TRUST_CLIENT_AUTH:
       
   924         case CKA_NETSCAPE_TRUST_CODE_SIGNING:
       
   925         case CKA_NETSCAPE_TRUST_EMAIL_PROTECTION:
       
   926             /* value CK_ULONG */
       
   927             jValueObject = ckULongPtrToJLongObject(env, (CK_ULONG*) ckpAttribute->pValue);
       
   928             break;
       
   929 
       
   930         default:
       
   931             /* we make a CK_BYTE[] out of this */
       
   932             jValueObject = ckByteArrayToJByteArray(env, (CK_BYTE*) ckpAttribute->pValue, jValueLength);
       
   933             break;
       
   934     }
       
   935 
       
   936     return jValueObject ;
       
   937 }
       
   938 
       
   939 /*
       
   940  * the following functions convert a Java mechanism parameter object to a PKCS#11
       
   941  * mechanism parameter structure
       
   942  *
       
   943  * CK_<Param>_PARAMS j<Param>ParamToCK<Param>Param(JNIEnv *env,
       
   944  *                                                 jobject jParam);
       
   945  *
       
   946  * These functions get a Java object, that must be the right Java mechanism
       
   947  * object and they return the new PKCS#11 mechanism parameter structure.
       
   948  * Every field of the Java object is retrieved, gets converted to a corresponding
       
   949  * PKCS#11 type and is set in the new PKCS#11 structure.
       
   950  */
       
   951 
       
   952 /*
       
   953  * converts the given Java mechanism parameter to a CK mechanism parameter structure
       
   954  * and store the length in bytes in the length variable.
       
   955  * The memory of *ckpParamPtr has to be freed after use!
       
   956  *
       
   957  * @param env - used to call JNI funktions to get the Java classes and objects
       
   958  * @param jParam - the Java mechanism parameter object to convert
       
   959  * @param ckpParamPtr - the reference of the new pointer to the new CK mechanism parameter
       
   960  *                      structure
       
   961  * @param ckpLength - the reference of the length in bytes of the new CK mechanism parameter
       
   962  *                    structure
       
   963  */
       
   964 void jMechanismParameterToCKMechanismParameter(JNIEnv *env, jobject jParam, CK_VOID_PTR *ckpParamPtr, CK_ULONG *ckpLength)
       
   965 {
       
   966     if (jParam == NULL) {
       
   967         *ckpParamPtr = NULL;
       
   968         *ckpLength = 0;
       
   969     } else if ((*env)->IsInstanceOf(env, jParam, jByteArrayClass)) {
       
   970         jByteArrayToCKByteArray(env, jParam, (CK_BYTE_PTR *)ckpParamPtr, ckpLength);
       
   971     } else if ((*env)->IsInstanceOf(env, jParam, jLongClass)) {
       
   972         *ckpParamPtr = jLongObjectToCKULongPtr(env, jParam);
       
   973         *ckpLength = sizeof(CK_ULONG);
       
   974     } else {
       
   975         TRACE0("\nSLOW PATH jMechanismParameterToCKMechanismParameter\n");
       
   976         jMechanismParameterToCKMechanismParameterSlow(env, jParam, ckpParamPtr, ckpLength);
       
   977     }
       
   978 }
       
   979 
       
   980 void jMechanismParameterToCKMechanismParameterSlow(JNIEnv *env, jobject jParam, CK_VOID_PTR *ckpParamPtr, CK_ULONG *ckpLength)
       
   981 {
       
   982     /* get all Java mechanism parameter classes */
       
   983     jclass jVersionClass, jSsl3MasterKeyDeriveParamsClass, jSsl3KeyMatParamsClass;
       
   984     jclass jTlsPrfParamsClass, jAesCtrParamsClass, jRsaPkcsOaepParamsClass;
       
   985     jclass jPbeParamsClass, jPkcs5Pbkd2ParamsClass, jRsaPkcsPssParamsClass;
       
   986     jclass jEcdh1DeriveParamsClass, jEcdh2DeriveParamsClass;
       
   987     jclass jX942Dh1DeriveParamsClass, jX942Dh2DeriveParamsClass;
       
   988     TRACE0("\nDEBUG: jMechanismParameterToCKMechanismParameter");
       
   989 
       
   990     /* most common cases, i.e. NULL/byte[]/long, are already handled by
       
   991      * jMechanismParameterToCKMechanismParameter before calling this method.
       
   992      */
       
   993     jVersionClass = (*env)->FindClass(env, CLASS_VERSION);
       
   994     if (jVersionClass == NULL) { return; }
       
   995     if ((*env)->IsInstanceOf(env, jParam, jVersionClass)) {
       
   996         /*
       
   997          * CK_VERSION used by CKM_SSL3_PRE_MASTER_KEY_GEN
       
   998          */
       
   999         CK_VERSION_PTR ckpParam;
       
  1000 
       
  1001         /* convert jParameter to CKParameter */
       
  1002         ckpParam = jVersionToCKVersionPtr(env, jParam);
       
  1003 
       
  1004         /* get length and pointer of parameter */
       
  1005         *ckpLength = sizeof(CK_VERSION);
       
  1006         *ckpParamPtr = ckpParam;
       
  1007         return;
       
  1008     }
       
  1009 
       
  1010     jSsl3MasterKeyDeriveParamsClass = (*env)->FindClass(env, CLASS_SSL3_MASTER_KEY_DERIVE_PARAMS);
       
  1011     if (jSsl3MasterKeyDeriveParamsClass == NULL) { return; }
       
  1012     if ((*env)->IsInstanceOf(env, jParam, jSsl3MasterKeyDeriveParamsClass)) {
       
  1013         /*
       
  1014          * CK_SSL3_MASTER_KEY_DERIVE_PARAMS
       
  1015          */
       
  1016         CK_SSL3_MASTER_KEY_DERIVE_PARAMS_PTR ckpParam;
       
  1017 
       
  1018         ckpParam = (CK_SSL3_MASTER_KEY_DERIVE_PARAMS_PTR) malloc(sizeof(CK_SSL3_MASTER_KEY_DERIVE_PARAMS));
       
  1019         if (ckpParam == NULL) {
       
  1020             throwOutOfMemoryError(env, 0);
       
  1021             return;
       
  1022         }
       
  1023 
       
  1024         /* convert jParameter to CKParameter */
       
  1025         *ckpParam = jSsl3MasterKeyDeriveParamToCKSsl3MasterKeyDeriveParam(env, jParam);
       
  1026         if ((*env)->ExceptionCheck(env)) {
       
  1027             free(ckpParam);
       
  1028             return;
       
  1029         }
       
  1030 
       
  1031         /* get length and pointer of parameter */
       
  1032         *ckpLength = sizeof(CK_SSL3_MASTER_KEY_DERIVE_PARAMS);
       
  1033         *ckpParamPtr = ckpParam;
       
  1034         return;
       
  1035     }
       
  1036 
       
  1037     jSsl3KeyMatParamsClass = (*env)->FindClass(env, CLASS_SSL3_KEY_MAT_PARAMS);
       
  1038     if (jSsl3KeyMatParamsClass == NULL) { return; }
       
  1039     if ((*env)->IsInstanceOf(env, jParam, jSsl3KeyMatParamsClass)) {
       
  1040         /*
       
  1041          * CK_SSL3_KEY_MAT_PARAMS
       
  1042          */
       
  1043         CK_SSL3_KEY_MAT_PARAMS_PTR ckpParam;
       
  1044 
       
  1045         ckpParam = (CK_SSL3_KEY_MAT_PARAMS_PTR) malloc(sizeof(CK_SSL3_KEY_MAT_PARAMS));
       
  1046         if (ckpParam == NULL) {
       
  1047             throwOutOfMemoryError(env, 0);
       
  1048             return;
       
  1049         }
       
  1050 
       
  1051         /* convert jParameter to CKParameter */
       
  1052         *ckpParam = jSsl3KeyMatParamToCKSsl3KeyMatParam(env, jParam);
       
  1053         if ((*env)->ExceptionCheck(env)) {
       
  1054             free(ckpParam);
       
  1055             return;
       
  1056         }
       
  1057 
       
  1058         /* get length and pointer of parameter */
       
  1059         *ckpLength = sizeof(CK_SSL3_KEY_MAT_PARAMS);
       
  1060         *ckpParamPtr = ckpParam;
       
  1061         return;
       
  1062     }
       
  1063 
       
  1064     jTlsPrfParamsClass = (*env)->FindClass(env, CLASS_TLS_PRF_PARAMS);
       
  1065     if (jTlsPrfParamsClass == NULL) { return; }
       
  1066     if ((*env)->IsInstanceOf(env, jParam, jTlsPrfParamsClass)) {
       
  1067         /*
       
  1068          * CK_TLS_PRF_PARAMS
       
  1069          */
       
  1070         CK_TLS_PRF_PARAMS_PTR ckpParam;
       
  1071 
       
  1072         ckpParam = (CK_TLS_PRF_PARAMS_PTR) malloc(sizeof(CK_TLS_PRF_PARAMS));
       
  1073         if (ckpParam == NULL) {
       
  1074             throwOutOfMemoryError(env, 0);
       
  1075             return;
       
  1076         }
       
  1077 
       
  1078         /* convert jParameter to CKParameter */
       
  1079         *ckpParam = jTlsPrfParamsToCKTlsPrfParam(env, jParam);
       
  1080         if ((*env)->ExceptionCheck(env)) {
       
  1081             free(ckpParam);
       
  1082             return;
       
  1083         }
       
  1084 
       
  1085         /* get length and pointer of parameter */
       
  1086         *ckpLength = sizeof(CK_TLS_PRF_PARAMS);
       
  1087         *ckpParamPtr = ckpParam;
       
  1088         return;
       
  1089     }
       
  1090 
       
  1091     jAesCtrParamsClass = (*env)->FindClass(env, CLASS_AES_CTR_PARAMS);
       
  1092     if (jAesCtrParamsClass == NULL) { return; }
       
  1093     if ((*env)->IsInstanceOf(env, jParam, jAesCtrParamsClass)) {
       
  1094         /*
       
  1095          * CK_AES_CTR_PARAMS
       
  1096          */
       
  1097         CK_AES_CTR_PARAMS_PTR ckpParam;
       
  1098 
       
  1099         ckpParam = (CK_AES_CTR_PARAMS_PTR) malloc(sizeof(CK_AES_CTR_PARAMS));
       
  1100         if (ckpParam == NULL) {
       
  1101             throwOutOfMemoryError(env, 0);
       
  1102             return;
       
  1103         }
       
  1104 
       
  1105         /* convert jParameter to CKParameter */
       
  1106         jAesCtrParamsToCKAesCtrParam(env, jParam, ckpParam);
       
  1107         if ((*env)->ExceptionCheck(env)) {
       
  1108             free(ckpParam);
       
  1109             return;
       
  1110         }
       
  1111 
       
  1112         /* get length and pointer of parameter */
       
  1113         *ckpLength = sizeof(CK_AES_CTR_PARAMS);
       
  1114         *ckpParamPtr = ckpParam;
       
  1115         return;
       
  1116     }
       
  1117 
       
  1118     jRsaPkcsOaepParamsClass = (*env)->FindClass(env, CLASS_RSA_PKCS_OAEP_PARAMS);
       
  1119     if (jRsaPkcsOaepParamsClass == NULL) { return; }
       
  1120     if ((*env)->IsInstanceOf(env, jParam, jRsaPkcsOaepParamsClass)) {
       
  1121         /*
       
  1122          * CK_RSA_PKCS_OAEP_PARAMS
       
  1123          */
       
  1124         CK_RSA_PKCS_OAEP_PARAMS_PTR ckpParam;
       
  1125 
       
  1126         ckpParam = (CK_RSA_PKCS_OAEP_PARAMS_PTR) malloc(sizeof(CK_RSA_PKCS_OAEP_PARAMS));
       
  1127         if (ckpParam == NULL) {
       
  1128             throwOutOfMemoryError(env, 0);
       
  1129             return;
       
  1130         }
       
  1131 
       
  1132         /* convert jParameter to CKParameter */
       
  1133         *ckpParam = jRsaPkcsOaepParamToCKRsaPkcsOaepParam(env, jParam);
       
  1134         if ((*env)->ExceptionCheck(env)) {
       
  1135             free(ckpParam);
       
  1136             return;
       
  1137         }
       
  1138 
       
  1139         /* get length and pointer of parameter */
       
  1140         *ckpLength = sizeof(CK_RSA_PKCS_OAEP_PARAMS);
       
  1141         *ckpParamPtr = ckpParam;
       
  1142         return;
       
  1143     }
       
  1144 
       
  1145     jPbeParamsClass = (*env)->FindClass(env, CLASS_PBE_PARAMS);
       
  1146     if (jPbeParamsClass == NULL) { return; }
       
  1147     if ((*env)->IsInstanceOf(env, jParam, jPbeParamsClass)) {
       
  1148         /*
       
  1149          * CK_PBE_PARAMS
       
  1150          */
       
  1151         CK_PBE_PARAMS_PTR ckpParam;
       
  1152 
       
  1153         ckpParam = (CK_PBE_PARAMS_PTR) malloc(sizeof(CK_PBE_PARAMS));
       
  1154         if (ckpParam == NULL) {
       
  1155             throwOutOfMemoryError(env, 0);
       
  1156             return;
       
  1157         }
       
  1158 
       
  1159         /* convert jParameter to CKParameter */
       
  1160         *ckpParam = jPbeParamToCKPbeParam(env, jParam);
       
  1161         if ((*env)->ExceptionCheck(env)) {
       
  1162             free(ckpParam);
       
  1163             return;
       
  1164         }
       
  1165 
       
  1166         /* get length and pointer of parameter */
       
  1167         *ckpLength = sizeof(CK_PBE_PARAMS);
       
  1168         *ckpParamPtr = ckpParam;
       
  1169         return;
       
  1170     }
       
  1171 
       
  1172     jPkcs5Pbkd2ParamsClass = (*env)->FindClass(env, CLASS_PKCS5_PBKD2_PARAMS);
       
  1173     if (jPkcs5Pbkd2ParamsClass == NULL) { return; }
       
  1174     if ((*env)->IsInstanceOf(env, jParam, jPkcs5Pbkd2ParamsClass)) {
       
  1175         /*
       
  1176          * CK_PKCS5_PBKD2_PARAMS
       
  1177          */
       
  1178         CK_PKCS5_PBKD2_PARAMS_PTR ckpParam;
       
  1179 
       
  1180         ckpParam = (CK_PKCS5_PBKD2_PARAMS_PTR) malloc(sizeof(CK_PKCS5_PBKD2_PARAMS));
       
  1181         if (ckpParam == NULL) {
       
  1182             throwOutOfMemoryError(env, 0);
       
  1183             return;
       
  1184         }
       
  1185 
       
  1186         /* convert jParameter to CKParameter */
       
  1187         *ckpParam = jPkcs5Pbkd2ParamToCKPkcs5Pbkd2Param(env, jParam);
       
  1188         if ((*env)->ExceptionCheck(env)) {
       
  1189             free(ckpParam);
       
  1190             return;
       
  1191         }
       
  1192 
       
  1193         /* get length and pointer of parameter */
       
  1194         *ckpLength = sizeof(CK_PKCS5_PBKD2_PARAMS);
       
  1195         *ckpParamPtr = ckpParam;
       
  1196         return;
       
  1197     }
       
  1198 
       
  1199     jRsaPkcsPssParamsClass = (*env)->FindClass(env, CLASS_RSA_PKCS_PSS_PARAMS);
       
  1200     if (jRsaPkcsPssParamsClass == NULL) { return; }
       
  1201     if ((*env)->IsInstanceOf(env, jParam, jRsaPkcsPssParamsClass)) {
       
  1202         /*
       
  1203          * CK_RSA_PKCS_PSS_PARAMS
       
  1204          */
       
  1205         CK_RSA_PKCS_PSS_PARAMS_PTR ckpParam;
       
  1206 
       
  1207         ckpParam = (CK_RSA_PKCS_PSS_PARAMS_PTR) malloc(sizeof(CK_RSA_PKCS_PSS_PARAMS));
       
  1208         if (ckpParam == NULL) {
       
  1209             throwOutOfMemoryError(env, 0);
       
  1210             return;
       
  1211         }
       
  1212 
       
  1213         /* convert jParameter to CKParameter */
       
  1214         *ckpParam = jRsaPkcsPssParamToCKRsaPkcsPssParam(env, jParam);
       
  1215         if ((*env)->ExceptionCheck(env)) {
       
  1216             free(ckpParam);
       
  1217             return;
       
  1218         }
       
  1219 
       
  1220         /* get length and pointer of parameter */
       
  1221         *ckpLength = sizeof(CK_RSA_PKCS_PSS_PARAMS);
       
  1222         *ckpParamPtr = ckpParam;
       
  1223         return;
       
  1224     }
       
  1225 
       
  1226     jEcdh1DeriveParamsClass = (*env)->FindClass(env, CLASS_ECDH1_DERIVE_PARAMS);
       
  1227     if (jEcdh1DeriveParamsClass == NULL) { return; }
       
  1228     if ((*env)->IsInstanceOf(env, jParam, jEcdh1DeriveParamsClass)) {
       
  1229         /*
       
  1230          * CK_ECDH1_DERIVE_PARAMS
       
  1231          */
       
  1232         CK_ECDH1_DERIVE_PARAMS_PTR ckpParam;
       
  1233 
       
  1234         ckpParam = (CK_ECDH1_DERIVE_PARAMS_PTR) malloc(sizeof(CK_ECDH1_DERIVE_PARAMS));
       
  1235         if (ckpParam == NULL) {
       
  1236             throwOutOfMemoryError(env, 0);
       
  1237             return;
       
  1238         }
       
  1239 
       
  1240         /* convert jParameter to CKParameter */
       
  1241         *ckpParam = jEcdh1DeriveParamToCKEcdh1DeriveParam(env, jParam);
       
  1242         if ((*env)->ExceptionCheck(env)) {
       
  1243             free(ckpParam);
       
  1244             return;
       
  1245         }
       
  1246 
       
  1247         /* get length and pointer of parameter */
       
  1248         *ckpLength = sizeof(CK_ECDH1_DERIVE_PARAMS);
       
  1249         *ckpParamPtr = ckpParam;
       
  1250         return;
       
  1251     }
       
  1252 
       
  1253     jEcdh2DeriveParamsClass = (*env)->FindClass(env, CLASS_ECDH2_DERIVE_PARAMS);
       
  1254     if (jEcdh2DeriveParamsClass == NULL) { return; }
       
  1255     if ((*env)->IsInstanceOf(env, jParam, jEcdh2DeriveParamsClass)) {
       
  1256         /*
       
  1257          * CK_ECDH2_DERIVE_PARAMS
       
  1258          */
       
  1259         CK_ECDH2_DERIVE_PARAMS_PTR ckpParam;
       
  1260 
       
  1261         ckpParam = (CK_ECDH2_DERIVE_PARAMS_PTR) malloc(sizeof(CK_ECDH2_DERIVE_PARAMS));
       
  1262         if (ckpParam == NULL) {
       
  1263             throwOutOfMemoryError(env, 0);
       
  1264             return;
       
  1265         }
       
  1266 
       
  1267         /* convert jParameter to CKParameter */
       
  1268         *ckpParam = jEcdh2DeriveParamToCKEcdh2DeriveParam(env, jParam);
       
  1269         if ((*env)->ExceptionCheck(env)) {
       
  1270             free(ckpParam);
       
  1271             return;
       
  1272         }
       
  1273 
       
  1274         /* get length and pointer of parameter */
       
  1275         *ckpLength = sizeof(CK_ECDH2_DERIVE_PARAMS);
       
  1276         *ckpParamPtr = ckpParam;
       
  1277         return;
       
  1278     }
       
  1279 
       
  1280     jX942Dh1DeriveParamsClass = (*env)->FindClass(env, CLASS_X9_42_DH1_DERIVE_PARAMS);
       
  1281     if (jX942Dh1DeriveParamsClass == NULL) { return; }
       
  1282     if ((*env)->IsInstanceOf(env, jParam, jX942Dh1DeriveParamsClass)) {
       
  1283         /*
       
  1284          * CK_X9_42_DH1_DERIVE_PARAMS
       
  1285          */
       
  1286         CK_X9_42_DH1_DERIVE_PARAMS_PTR ckpParam;
       
  1287 
       
  1288         ckpParam = (CK_X9_42_DH1_DERIVE_PARAMS_PTR) malloc(sizeof(CK_X9_42_DH1_DERIVE_PARAMS));
       
  1289         if (ckpParam == NULL) {
       
  1290             throwOutOfMemoryError(env, 0);
       
  1291             return;
       
  1292         }
       
  1293 
       
  1294         /* convert jParameter to CKParameter */
       
  1295         *ckpParam = jX942Dh1DeriveParamToCKX942Dh1DeriveParam(env, jParam);
       
  1296         if ((*env)->ExceptionCheck(env)) {
       
  1297             free(ckpParam);
       
  1298             return;
       
  1299         }
       
  1300 
       
  1301         /* get length and pointer of parameter */
       
  1302         *ckpLength = sizeof(CK_X9_42_DH1_DERIVE_PARAMS);
       
  1303         *ckpParamPtr = ckpParam;
       
  1304         return;
       
  1305     }
       
  1306 
       
  1307     jX942Dh2DeriveParamsClass = (*env)->FindClass(env, CLASS_X9_42_DH2_DERIVE_PARAMS);
       
  1308     if (jX942Dh2DeriveParamsClass == NULL) { return; }
       
  1309     if ((*env)->IsInstanceOf(env, jParam, jX942Dh2DeriveParamsClass)) {
       
  1310         /*
       
  1311          * CK_X9_42_DH2_DERIVE_PARAMS
       
  1312          */
       
  1313         CK_X9_42_DH2_DERIVE_PARAMS_PTR ckpParam;
       
  1314 
       
  1315         ckpParam = (CK_X9_42_DH2_DERIVE_PARAMS_PTR) malloc(sizeof(CK_X9_42_DH2_DERIVE_PARAMS));
       
  1316         if (ckpParam == NULL) {
       
  1317             throwOutOfMemoryError(env, 0);
       
  1318             return;
       
  1319         }
       
  1320 
       
  1321         /* convert jParameter to CKParameter */
       
  1322         *ckpParam = jX942Dh2DeriveParamToCKX942Dh2DeriveParam(env, jParam);
       
  1323         if ((*env)->ExceptionCheck(env)) {
       
  1324             free(ckpParam);
       
  1325             return;
       
  1326         }
       
  1327 
       
  1328         /* get length and pointer of parameter */
       
  1329         *ckpLength = sizeof(CK_X9_42_DH2_DERIVE_PARAMS);
       
  1330         *ckpParamPtr = ckpParam;
       
  1331         return;
       
  1332     }
       
  1333 
       
  1334     /* if everything faild up to here */
       
  1335     /* try if the parameter is a primitive Java type */
       
  1336     jObjectToPrimitiveCKObjectPtrPtr(env, jParam, ckpParamPtr, ckpLength);
       
  1337     /* *ckpParamPtr = jObjectToCKVoidPtr(jParam); */
       
  1338     /* *ckpLength = 1; */
       
  1339 
       
  1340     TRACE0("FINISHED\n");
       
  1341 }
       
  1342 
       
  1343 
       
  1344 /* the mechanism parameter convertion functions: */
       
  1345 
       
  1346 /*
       
  1347  * converts the Java CK_RSA_PKCS_OAEP_PARAMS object to a CK_RSA_PKCS_OAEP_PARAMS structure
       
  1348  *
       
  1349  * @param env - used to call JNI funktions to get the Java classes and objects
       
  1350  * @param jParam - the Java CK_RSA_PKCS_OAEP_PARAMS object to convert
       
  1351  * @return - the new CK_RSA_PKCS_OAEP_PARAMS structure
       
  1352  */
       
  1353 CK_RSA_PKCS_OAEP_PARAMS jRsaPkcsOaepParamToCKRsaPkcsOaepParam(JNIEnv *env, jobject jParam)
       
  1354 {
       
  1355     jclass jRsaPkcsOaepParamsClass;
       
  1356     CK_RSA_PKCS_OAEP_PARAMS ckParam;
       
  1357     jfieldID fieldID;
       
  1358     jlong jHashAlg, jMgf, jSource;
       
  1359     jobject jSourceData;
       
  1360     CK_BYTE_PTR ckpByte;
       
  1361     memset(&ckParam, 0, sizeof(CK_RSA_PKCS_OAEP_PARAMS));
       
  1362 
       
  1363     /* get hashAlg */
       
  1364     jRsaPkcsOaepParamsClass = (*env)->FindClass(env, CLASS_RSA_PKCS_OAEP_PARAMS);
       
  1365     if (jRsaPkcsOaepParamsClass == NULL) { return ckParam; }
       
  1366     fieldID = (*env)->GetFieldID(env, jRsaPkcsOaepParamsClass, "hashAlg", "J");
       
  1367     if (fieldID == NULL) { return ckParam; }
       
  1368     jHashAlg = (*env)->GetLongField(env, jParam, fieldID);
       
  1369 
       
  1370     /* get mgf */
       
  1371     fieldID = (*env)->GetFieldID(env, jRsaPkcsOaepParamsClass, "mgf", "J");
       
  1372     if (fieldID == NULL) { return ckParam; }
       
  1373     jMgf = (*env)->GetLongField(env, jParam, fieldID);
       
  1374 
       
  1375     /* get source */
       
  1376     fieldID = (*env)->GetFieldID(env, jRsaPkcsOaepParamsClass, "source", "J");
       
  1377     if (fieldID == NULL) { return ckParam; }
       
  1378     jSource = (*env)->GetLongField(env, jParam, fieldID);
       
  1379 
       
  1380     /* get sourceData and sourceDataLength */
       
  1381     fieldID = (*env)->GetFieldID(env, jRsaPkcsOaepParamsClass, "pSourceData", "[B");
       
  1382     if (fieldID == NULL) { return ckParam; }
       
  1383     jSourceData = (*env)->GetObjectField(env, jParam, fieldID);
       
  1384 
       
  1385     /* populate java values */
       
  1386     ckParam.hashAlg = jLongToCKULong(jHashAlg);
       
  1387     ckParam.mgf = jLongToCKULong(jMgf);
       
  1388     ckParam.source = jLongToCKULong(jSource);
       
  1389     jByteArrayToCKByteArray(env, jSourceData, & ckpByte, &(ckParam.ulSourceDataLen));
       
  1390     if ((*env)->ExceptionCheck(env)) { return ckParam; }
       
  1391     ckParam.pSourceData = (CK_VOID_PTR) ckpByte;
       
  1392 
       
  1393     return ckParam ;
       
  1394 }
       
  1395 
       
  1396 /*
       
  1397  * converts the Java CK_PBE_PARAMS object to a CK_PBE_PARAMS structure
       
  1398  *
       
  1399  * @param env - used to call JNI funktions to get the Java classes and objects
       
  1400  * @param jParam - the Java CK_PBE_PARAMS object to convert
       
  1401  * @return - the new CK_PBE_PARAMS structure
       
  1402  */
       
  1403 CK_PBE_PARAMS jPbeParamToCKPbeParam(JNIEnv *env, jobject jParam)
       
  1404 {
       
  1405     jclass jPbeParamsClass;
       
  1406     CK_PBE_PARAMS ckParam;
       
  1407     jfieldID fieldID;
       
  1408     jlong jIteration;
       
  1409     jobject jInitVector, jPassword, jSalt;
       
  1410     CK_ULONG ckTemp;
       
  1411     memset(&ckParam, 0, sizeof(CK_PBE_PARAMS));
       
  1412 
       
  1413     /* get pInitVector */
       
  1414     jPbeParamsClass = (*env)->FindClass(env, CLASS_PBE_PARAMS);
       
  1415     if (jPbeParamsClass == NULL) { return ckParam; }
       
  1416     fieldID = (*env)->GetFieldID(env, jPbeParamsClass, "pInitVector", "[C");
       
  1417     if (fieldID == NULL) { return ckParam; }
       
  1418     jInitVector = (*env)->GetObjectField(env, jParam, fieldID);
       
  1419 
       
  1420     /* get pPassword and ulPasswordLength */
       
  1421     fieldID = (*env)->GetFieldID(env, jPbeParamsClass, "pPassword", "[C");
       
  1422     if (fieldID == NULL) { return ckParam; }
       
  1423     jPassword = (*env)->GetObjectField(env, jParam, fieldID);
       
  1424 
       
  1425     /* get pSalt and ulSaltLength */
       
  1426     fieldID = (*env)->GetFieldID(env, jPbeParamsClass, "pSalt", "[C");
       
  1427     if (fieldID == NULL) { return ckParam; }
       
  1428     jSalt = (*env)->GetObjectField(env, jParam, fieldID);
       
  1429 
       
  1430     /* get ulIteration */
       
  1431     fieldID = (*env)->GetFieldID(env, jPbeParamsClass, "ulIteration", "J");
       
  1432     if (fieldID == NULL) { return ckParam; }
       
  1433     jIteration = (*env)->GetLongField(env, jParam, fieldID);
       
  1434 
       
  1435     /* populate java values */
       
  1436     ckParam.ulIteration = jLongToCKULong(jIteration);
       
  1437     jCharArrayToCKCharArray(env, jInitVector, &(ckParam.pInitVector), &ckTemp);
       
  1438     if ((*env)->ExceptionCheck(env)) { return ckParam; }
       
  1439     jCharArrayToCKCharArray(env, jPassword, &(ckParam.pPassword), &(ckParam.ulPasswordLen));
       
  1440     if ((*env)->ExceptionCheck(env)) {
       
  1441         free(ckParam.pInitVector);
       
  1442         return ckParam;
       
  1443     }
       
  1444     jCharArrayToCKCharArray(env, jSalt, &(ckParam.pSalt), &(ckParam.ulSaltLen));
       
  1445     if ((*env)->ExceptionCheck(env)) {
       
  1446         free(ckParam.pInitVector);
       
  1447         free(ckParam.pPassword);
       
  1448         return ckParam;
       
  1449     }
       
  1450 
       
  1451     return ckParam ;
       
  1452 }
       
  1453 
       
  1454 /*
       
  1455  * Copy back the initialization vector from the native structure to the
       
  1456  * Java object. This is only used for CKM_PBE_* mechanisms and their
       
  1457  * CK_PBE_PARAMS parameters.
       
  1458  *
       
  1459  */
       
  1460 void copyBackPBEInitializationVector(JNIEnv *env, CK_MECHANISM *ckMechanism, jobject jMechanism)
       
  1461 {
       
  1462     jclass jMechanismClass, jPbeParamsClass;
       
  1463     CK_PBE_PARAMS *ckParam;
       
  1464     jfieldID fieldID;
       
  1465     CK_MECHANISM_TYPE ckMechanismType;
       
  1466     jlong jMechanismType;
       
  1467     jobject jParameter;
       
  1468     jobject jInitVector;
       
  1469     jint jInitVectorLength;
       
  1470     CK_CHAR_PTR initVector;
       
  1471     int i;
       
  1472     jchar* jInitVectorChars;
       
  1473 
       
  1474     /* get mechanism */
       
  1475     jMechanismClass = (*env)->FindClass(env, CLASS_MECHANISM);
       
  1476     if (jMechanismClass == NULL) { return; }
       
  1477     fieldID = (*env)->GetFieldID(env, jMechanismClass, "mechanism", "J");
       
  1478     if (fieldID == NULL) { return; }
       
  1479     jMechanismType = (*env)->GetLongField(env, jMechanism, fieldID);
       
  1480     ckMechanismType = jLongToCKULong(jMechanismType);
       
  1481     if (ckMechanismType != ckMechanism->mechanism) {
       
  1482         /* we do not have maching types, this should not occur */
       
  1483         return;
       
  1484     }
       
  1485 
       
  1486     jPbeParamsClass = (*env)->FindClass(env, CLASS_PBE_PARAMS);
       
  1487     if (jPbeParamsClass == NULL) { return; }
       
  1488     ckParam = (CK_PBE_PARAMS *) ckMechanism->pParameter;
       
  1489     if (ckParam != NULL_PTR) {
       
  1490         initVector = ckParam->pInitVector;
       
  1491         if (initVector != NULL_PTR) {
       
  1492             /* get pParameter */
       
  1493             fieldID = (*env)->GetFieldID(env, jMechanismClass, "pParameter", "Ljava/lang/Object;");
       
  1494             if (fieldID == NULL) { return; }
       
  1495             jParameter = (*env)->GetObjectField(env, jMechanism, fieldID);
       
  1496             fieldID = (*env)->GetFieldID(env, jPbeParamsClass, "pInitVektor", "[C");
       
  1497             if (fieldID == NULL) { return; }
       
  1498             jInitVector = (*env)->GetObjectField(env, jParameter, fieldID);
       
  1499 
       
  1500             if (jInitVector != NULL) {
       
  1501                 jInitVectorLength = (*env)->GetArrayLength(env, jInitVector);
       
  1502                 jInitVectorChars = (*env)->GetCharArrayElements(env, jInitVector, NULL);
       
  1503                 if (jInitVectorChars == NULL) { return; }
       
  1504 
       
  1505                 /* copy the chars to the Java buffer */
       
  1506                 for (i=0; i < jInitVectorLength; i++) {
       
  1507                     jInitVectorChars[i] = ckCharToJChar(initVector[i]);
       
  1508                 }
       
  1509                 /* copy back the Java buffer to the object */
       
  1510                 (*env)->ReleaseCharArrayElements(env, jInitVector, jInitVectorChars, 0);
       
  1511             }
       
  1512         }
       
  1513     }
       
  1514 }
       
  1515 
       
  1516 /*
       
  1517  * converts the Java CK_PKCS5_PBKD2_PARAMS object to a CK_PKCS5_PBKD2_PARAMS structure
       
  1518  *
       
  1519  * @param env - used to call JNI funktions to get the Java classes and objects
       
  1520  * @param jParam - the Java CK_PKCS5_PBKD2_PARAMS object to convert
       
  1521  * @return - the new CK_PKCS5_PBKD2_PARAMS structure
       
  1522  */
       
  1523 CK_PKCS5_PBKD2_PARAMS jPkcs5Pbkd2ParamToCKPkcs5Pbkd2Param(JNIEnv *env, jobject jParam)
       
  1524 {
       
  1525     jclass jPkcs5Pbkd2ParamsClass;
       
  1526     CK_PKCS5_PBKD2_PARAMS ckParam;
       
  1527     jfieldID fieldID;
       
  1528     jlong jSaltSource, jIteration, jPrf;
       
  1529     jobject jSaltSourceData, jPrfData;
       
  1530     memset(&ckParam, 0, sizeof(CK_PKCS5_PBKD2_PARAMS));
       
  1531 
       
  1532     /* get saltSource */
       
  1533     jPkcs5Pbkd2ParamsClass = (*env)->FindClass(env, CLASS_PKCS5_PBKD2_PARAMS);
       
  1534     if (jPkcs5Pbkd2ParamsClass == NULL) { return ckParam; }
       
  1535     fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "saltSource", "J");
       
  1536     if (fieldID == NULL) { return ckParam; }
       
  1537     jSaltSource = (*env)->GetLongField(env, jParam, fieldID);
       
  1538 
       
  1539     /* get pSaltSourceData */
       
  1540     fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "pSaltSourceData", "[B");
       
  1541     if (fieldID == NULL) { return ckParam; }
       
  1542     jSaltSourceData = (*env)->GetObjectField(env, jParam, fieldID);
       
  1543 
       
  1544     /* get iterations */
       
  1545     fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "iterations", "J");
       
  1546     if (fieldID == NULL) { return ckParam; }
       
  1547     jIteration = (*env)->GetLongField(env, jParam, fieldID);
       
  1548 
       
  1549     /* get prf */
       
  1550     fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "prf", "J");
       
  1551     if (fieldID == NULL) { return ckParam; }
       
  1552     jPrf = (*env)->GetLongField(env, jParam, fieldID);
       
  1553 
       
  1554     /* get pPrfData and ulPrfDataLength in byte */
       
  1555     fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "pPrfData", "[B");
       
  1556     if (fieldID == NULL) { return ckParam; }
       
  1557     jPrfData = (*env)->GetObjectField(env, jParam, fieldID);
       
  1558 
       
  1559     /* populate java values */
       
  1560     ckParam.saltSource = jLongToCKULong(jSaltSource);
       
  1561     jByteArrayToCKByteArray(env, jSaltSourceData, (CK_BYTE_PTR *) &(ckParam.pSaltSourceData), &(ckParam.ulSaltSourceDataLen));
       
  1562     if ((*env)->ExceptionCheck(env)) { return ckParam; }
       
  1563     ckParam.iterations = jLongToCKULong(jIteration);
       
  1564     ckParam.prf = jLongToCKULong(jPrf);
       
  1565     jByteArrayToCKByteArray(env, jPrfData, (CK_BYTE_PTR *) &(ckParam.pPrfData), &(ckParam.ulPrfDataLen));
       
  1566     if ((*env)->ExceptionCheck(env)) {
       
  1567         free(ckParam.pSaltSourceData);
       
  1568         return ckParam;
       
  1569     }
       
  1570 
       
  1571     return ckParam ;
       
  1572 }
       
  1573 
       
  1574 /*
       
  1575  * converts the Java CK_RSA_PKCS_PSS_PARAMS object to a CK_RSA_PKCS_PSS_PARAMS structure
       
  1576  *
       
  1577  * @param env - used to call JNI funktions to get the Java classes and objects
       
  1578  * @param jParam - the Java CK_RSA_PKCS_PSS_PARAMS object to convert
       
  1579  * @return - the new CK_RSA_PKCS_PSS_PARAMS structure
       
  1580  */
       
  1581 CK_RSA_PKCS_PSS_PARAMS jRsaPkcsPssParamToCKRsaPkcsPssParam(JNIEnv *env, jobject jParam)
       
  1582 {
       
  1583     jclass jRsaPkcsPssParamsClass;
       
  1584     CK_RSA_PKCS_PSS_PARAMS ckParam;
       
  1585     jfieldID fieldID;
       
  1586     jlong jHashAlg, jMgf, jSLen;
       
  1587     memset(&ckParam, 0, sizeof(CK_RSA_PKCS_PSS_PARAMS));
       
  1588 
       
  1589     /* get hashAlg */
       
  1590     jRsaPkcsPssParamsClass = (*env)->FindClass(env, CLASS_RSA_PKCS_PSS_PARAMS);
       
  1591     if (jRsaPkcsPssParamsClass == NULL) { return ckParam; }
       
  1592     fieldID = (*env)->GetFieldID(env, jRsaPkcsPssParamsClass, "hashAlg", "J");
       
  1593     if (fieldID == NULL) { return ckParam; }
       
  1594     jHashAlg = (*env)->GetLongField(env, jParam, fieldID);
       
  1595 
       
  1596     /* get mgf */
       
  1597     fieldID = (*env)->GetFieldID(env, jRsaPkcsPssParamsClass, "mgf", "J");
       
  1598     if (fieldID == NULL) { return ckParam; }
       
  1599     jMgf = (*env)->GetLongField(env, jParam, fieldID);
       
  1600 
       
  1601     /* get sLen */
       
  1602     fieldID = (*env)->GetFieldID(env, jRsaPkcsPssParamsClass, "sLen", "J");
       
  1603     if (fieldID == NULL) { return ckParam; }
       
  1604     jSLen = (*env)->GetLongField(env, jParam, fieldID);
       
  1605 
       
  1606     /* populate java values */
       
  1607     ckParam.hashAlg = jLongToCKULong(jHashAlg);
       
  1608     ckParam.mgf = jLongToCKULong(jMgf);
       
  1609     ckParam.sLen = jLongToCKULong(jSLen);
       
  1610 
       
  1611     return ckParam ;
       
  1612 }
       
  1613 
       
  1614 /*
       
  1615  * converts the Java CK_ECDH1_DERIVE_PARAMS object to a CK_ECDH1_DERIVE_PARAMS structure
       
  1616  *
       
  1617  * @param env - used to call JNI funktions to get the Java classes and objects
       
  1618  * @param jParam - the Java CK_ECDH1_DERIVE_PARAMS object to convert
       
  1619  * @return - the new CK_ECDH1_DERIVE_PARAMS structure
       
  1620  */
       
  1621 CK_ECDH1_DERIVE_PARAMS jEcdh1DeriveParamToCKEcdh1DeriveParam(JNIEnv *env, jobject jParam)
       
  1622 {
       
  1623     jclass jEcdh1DeriveParamsClass;
       
  1624     CK_ECDH1_DERIVE_PARAMS ckParam;
       
  1625     jfieldID fieldID;
       
  1626     jlong jLong;
       
  1627     jobject jSharedData, jPublicData;
       
  1628     memset(&ckParam, 0, sizeof(CK_ECDH1_DERIVE_PARAMS));
       
  1629 
       
  1630     /* get kdf */
       
  1631     jEcdh1DeriveParamsClass = (*env)->FindClass(env, CLASS_ECDH1_DERIVE_PARAMS);
       
  1632     if (jEcdh1DeriveParamsClass == NULL) { return ckParam; }
       
  1633     fieldID = (*env)->GetFieldID(env, jEcdh1DeriveParamsClass, "kdf", "J");
       
  1634     if (fieldID == NULL) { return ckParam; }
       
  1635     jLong = (*env)->GetLongField(env, jParam, fieldID);
       
  1636     ckParam.kdf = jLongToCKULong(jLong);
       
  1637 
       
  1638     /* get pSharedData and ulSharedDataLen */
       
  1639     fieldID = (*env)->GetFieldID(env, jEcdh1DeriveParamsClass, "pSharedData", "[B");
       
  1640     if (fieldID == NULL) { return ckParam; }
       
  1641     jSharedData = (*env)->GetObjectField(env, jParam, fieldID);
       
  1642 
       
  1643     /* get pPublicData and ulPublicDataLen */
       
  1644     fieldID = (*env)->GetFieldID(env, jEcdh1DeriveParamsClass, "pPublicData", "[B");
       
  1645     if (fieldID == NULL) { return ckParam; }
       
  1646     jPublicData = (*env)->GetObjectField(env, jParam, fieldID);
       
  1647 
       
  1648     /* populate java values */
       
  1649     ckParam.kdf = jLongToCKULong(jLong);
       
  1650     jByteArrayToCKByteArray(env, jSharedData, &(ckParam.pSharedData), &(ckParam.ulSharedDataLen));
       
  1651     if ((*env)->ExceptionCheck(env)) { return ckParam; }
       
  1652     jByteArrayToCKByteArray(env, jPublicData, &(ckParam.pPublicData), &(ckParam.ulPublicDataLen));
       
  1653     if ((*env)->ExceptionCheck(env)) {
       
  1654         free(ckParam.pSharedData);
       
  1655         return ckParam;
       
  1656     }
       
  1657 
       
  1658     return ckParam ;
       
  1659 }
       
  1660 
       
  1661 /*
       
  1662  * converts the Java CK_ECDH2_DERIVE_PARAMS object to a CK_ECDH2_DERIVE_PARAMS structure
       
  1663  *
       
  1664  * @param env - used to call JNI funktions to get the Java classes and objects
       
  1665  * @param jParam - the Java CK_ECDH2_DERIVE_PARAMS object to convert
       
  1666  * @return - the new CK_ECDH2_DERIVE_PARAMS structure
       
  1667  */
       
  1668 CK_ECDH2_DERIVE_PARAMS jEcdh2DeriveParamToCKEcdh2DeriveParam(JNIEnv *env, jobject jParam)
       
  1669 {
       
  1670     jclass jEcdh2DeriveParamsClass;
       
  1671     CK_ECDH2_DERIVE_PARAMS ckParam;
       
  1672     jfieldID fieldID;
       
  1673     jlong jKdf, jPrivateDataLen, jPrivateData;
       
  1674     jobject jSharedData, jPublicData, jPublicData2;
       
  1675     memset(&ckParam, 0, sizeof(CK_ECDH2_DERIVE_PARAMS));
       
  1676 
       
  1677     /* get kdf */
       
  1678     jEcdh2DeriveParamsClass = (*env)->FindClass(env, CLASS_ECDH2_DERIVE_PARAMS);
       
  1679     if (jEcdh2DeriveParamsClass == NULL) { return ckParam; }
       
  1680     fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "kdf", "J");
       
  1681     if (fieldID == NULL) { return ckParam; }
       
  1682     jKdf = (*env)->GetLongField(env, jParam, fieldID);
       
  1683 
       
  1684     /* get pSharedData and ulSharedDataLen */
       
  1685     fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "pSharedData", "[B");
       
  1686     if (fieldID == NULL) { return ckParam; }
       
  1687     jSharedData = (*env)->GetObjectField(env, jParam, fieldID);
       
  1688 
       
  1689     /* get pPublicData and ulPublicDataLen */
       
  1690     fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "pPublicData", "[B");
       
  1691     if (fieldID == NULL) { return ckParam; }
       
  1692     jPublicData = (*env)->GetObjectField(env, jParam, fieldID);
       
  1693 
       
  1694     /* get ulPrivateDataLen */
       
  1695     fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "ulPrivateDataLen", "J");
       
  1696     if (fieldID == NULL) { return ckParam; }
       
  1697     jPrivateDataLen = (*env)->GetLongField(env, jParam, fieldID);
       
  1698 
       
  1699     /* get hPrivateData */
       
  1700     fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "hPrivateData", "J");
       
  1701     if (fieldID == NULL) { return ckParam; }
       
  1702     jPrivateData = (*env)->GetLongField(env, jParam, fieldID);
       
  1703 
       
  1704     /* get pPublicData2 and ulPublicDataLen2 */
       
  1705     fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "pPublicData2", "[B");
       
  1706     if (fieldID == NULL) { return ckParam; }
       
  1707     jPublicData2 = (*env)->GetObjectField(env, jParam, fieldID);
       
  1708 
       
  1709     /* populate java values */
       
  1710     ckParam.kdf = jLongToCKULong(jKdf);
       
  1711     jByteArrayToCKByteArray(env, jSharedData, &(ckParam.pSharedData), &(ckParam.ulSharedDataLen));
       
  1712     if ((*env)->ExceptionCheck(env)) { return ckParam; }
       
  1713     jByteArrayToCKByteArray(env, jPublicData, &(ckParam.pPublicData), &(ckParam.ulPublicDataLen));
       
  1714     if ((*env)->ExceptionCheck(env)) {
       
  1715         free(ckParam.pSharedData);
       
  1716         return ckParam;
       
  1717     }
       
  1718     ckParam.ulPrivateDataLen = jLongToCKULong(jPrivateDataLen);
       
  1719     ckParam.hPrivateData = jLongToCKULong(jPrivateData);
       
  1720     jByteArrayToCKByteArray(env, jPublicData2, &(ckParam.pPublicData2), &(ckParam.ulPublicDataLen2));
       
  1721     if ((*env)->ExceptionCheck(env)) {
       
  1722         free(ckParam.pSharedData);
       
  1723         free(ckParam.pPublicData);
       
  1724         return ckParam;
       
  1725     }
       
  1726     return ckParam ;
       
  1727 }
       
  1728 
       
  1729 /*
       
  1730  * converts the Java CK_X9_42_DH1_DERIVE_PARAMS object to a CK_X9_42_DH1_DERIVE_PARAMS structure
       
  1731  *
       
  1732  * @param env - used to call JNI funktions to get the Java classes and objects
       
  1733  * @param jParam - the Java CK_X9_42_DH1_DERIVE_PARAMS object to convert
       
  1734  * @return - the new CK_X9_42_DH1_DERIVE_PARAMS structure
       
  1735  */
       
  1736 CK_X9_42_DH1_DERIVE_PARAMS jX942Dh1DeriveParamToCKX942Dh1DeriveParam(JNIEnv *env, jobject jParam)
       
  1737 {
       
  1738     jclass jX942Dh1DeriveParamsClass;
       
  1739     CK_X9_42_DH1_DERIVE_PARAMS ckParam;
       
  1740     jfieldID fieldID;
       
  1741     jlong jKdf;
       
  1742     jobject jOtherInfo, jPublicData;
       
  1743     memset(&ckParam, 0, sizeof(CK_X9_42_DH1_DERIVE_PARAMS));
       
  1744 
       
  1745     /* get kdf */
       
  1746     jX942Dh1DeriveParamsClass = (*env)->FindClass(env, CLASS_X9_42_DH1_DERIVE_PARAMS);
       
  1747     if (jX942Dh1DeriveParamsClass == NULL) { return ckParam; }
       
  1748     fieldID = (*env)->GetFieldID(env, jX942Dh1DeriveParamsClass, "kdf", "J");
       
  1749     if (fieldID == NULL) { return ckParam; }
       
  1750     jKdf = (*env)->GetLongField(env, jParam, fieldID);
       
  1751 
       
  1752     /* get pOtherInfo and ulOtherInfoLen */
       
  1753     fieldID = (*env)->GetFieldID(env, jX942Dh1DeriveParamsClass, "pOtherInfo", "[B");
       
  1754     if (fieldID == NULL) { return ckParam; }
       
  1755     jOtherInfo = (*env)->GetObjectField(env, jParam, fieldID);
       
  1756 
       
  1757     /* get pPublicData and ulPublicDataLen */
       
  1758     fieldID = (*env)->GetFieldID(env, jX942Dh1DeriveParamsClass, "pPublicData", "[B");
       
  1759     if (fieldID == NULL) { return ckParam; }
       
  1760     jPublicData = (*env)->GetObjectField(env, jParam, fieldID);
       
  1761 
       
  1762     /* populate java values */
       
  1763     ckParam.kdf = jLongToCKULong(jKdf);
       
  1764     jByteArrayToCKByteArray(env, jOtherInfo, &(ckParam.pOtherInfo), &(ckParam.ulOtherInfoLen));
       
  1765     if ((*env)->ExceptionCheck(env)) { return ckParam; }
       
  1766     jByteArrayToCKByteArray(env, jPublicData, &(ckParam.pPublicData), &(ckParam.ulPublicDataLen));
       
  1767     if ((*env)->ExceptionCheck(env)) {
       
  1768         free(ckParam.pOtherInfo);
       
  1769         return ckParam;
       
  1770     }
       
  1771 
       
  1772     return ckParam ;
       
  1773 }
       
  1774 
       
  1775 /*
       
  1776  * converts the Java CK_X9_42_DH2_DERIVE_PARAMS object to a CK_X9_42_DH2_DERIVE_PARAMS structure
       
  1777  *
       
  1778  * @param env - used to call JNI funktions to get the Java classes and objects
       
  1779  * @param jParam - the Java CK_X9_42_DH2_DERIVE_PARAMS object to convert
       
  1780  * @return - the new CK_X9_42_DH2_DERIVE_PARAMS structure
       
  1781  */
       
  1782 CK_X9_42_DH2_DERIVE_PARAMS jX942Dh2DeriveParamToCKX942Dh2DeriveParam(JNIEnv *env, jobject jParam)
       
  1783 {
       
  1784     jclass jX942Dh2DeriveParamsClass;
       
  1785     CK_X9_42_DH2_DERIVE_PARAMS ckParam;
       
  1786     jfieldID fieldID;
       
  1787     jlong jKdf, jPrivateDataLen, jPrivateData;
       
  1788     jobject jOtherInfo, jPublicData, jPublicData2;
       
  1789     memset(&ckParam, 0, sizeof(CK_X9_42_DH2_DERIVE_PARAMS));
       
  1790 
       
  1791     /* get kdf */
       
  1792     jX942Dh2DeriveParamsClass = (*env)->FindClass(env, CLASS_X9_42_DH2_DERIVE_PARAMS);
       
  1793     if (jX942Dh2DeriveParamsClass == NULL) { return ckParam; }
       
  1794     fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "kdf", "J");
       
  1795     if (fieldID == NULL) { return ckParam; }
       
  1796     jKdf = (*env)->GetLongField(env, jParam, fieldID);
       
  1797 
       
  1798     /* get pOtherInfo and ulOtherInfoLen */
       
  1799     fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "pOtherInfo", "[B");
       
  1800     if (fieldID == NULL) { return ckParam; }
       
  1801     jOtherInfo = (*env)->GetObjectField(env, jParam, fieldID);
       
  1802 
       
  1803     /* get pPublicData and ulPublicDataLen */
       
  1804     fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "pPublicData", "[B");
       
  1805     if (fieldID == NULL) { return ckParam; }
       
  1806     jPublicData = (*env)->GetObjectField(env, jParam, fieldID);
       
  1807 
       
  1808     /* get ulPrivateDataLen */
       
  1809     fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "ulPrivateDataLen", "J");
       
  1810     if (fieldID == NULL) { return ckParam; }
       
  1811     jPrivateDataLen = (*env)->GetLongField(env, jParam, fieldID);
       
  1812 
       
  1813     /* get hPrivateData */
       
  1814     fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "hPrivateData", "J");
       
  1815     if (fieldID == NULL) { return ckParam; }
       
  1816     jPrivateData = (*env)->GetLongField(env, jParam, fieldID);
       
  1817 
       
  1818     /* get pPublicData2 and ulPublicDataLen2 */
       
  1819     fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "pPublicData2", "[B");
       
  1820     if (fieldID == NULL) { return ckParam; }
       
  1821     jPublicData2 = (*env)->GetObjectField(env, jParam, fieldID);
       
  1822 
       
  1823     /* populate java values */
       
  1824     ckParam.kdf = jLongToCKULong(jKdf);
       
  1825     jByteArrayToCKByteArray(env, jOtherInfo, &(ckParam.pOtherInfo), &(ckParam.ulOtherInfoLen));
       
  1826     if ((*env)->ExceptionCheck(env)) { return ckParam; }
       
  1827     jByteArrayToCKByteArray(env, jPublicData, &(ckParam.pPublicData), &(ckParam.ulPublicDataLen));
       
  1828     if ((*env)->ExceptionCheck(env)) {
       
  1829         free(ckParam.pOtherInfo);
       
  1830         return ckParam;
       
  1831     }
       
  1832     ckParam.ulPrivateDataLen = jLongToCKULong(jPrivateDataLen);
       
  1833     ckParam.hPrivateData = jLongToCKULong(jPrivateData);
       
  1834     jByteArrayToCKByteArray(env, jPublicData2, &(ckParam.pPublicData2), &(ckParam.ulPublicDataLen2));
       
  1835     if ((*env)->ExceptionCheck(env)) {
       
  1836         free(ckParam.pOtherInfo);
       
  1837         free(ckParam.pPublicData);
       
  1838         return ckParam;
       
  1839     }
       
  1840 
       
  1841     return ckParam ;
       
  1842 }