jdk

Форк
0
2254 строки · 81.0 Кб
1
/*
2
 * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
3
 */
4

5
/* Copyright  (c) 2002 Graz University of Technology. All rights reserved.
6
 *
7
 * Redistribution and use in  source and binary forms, with or without
8
 * modification, are permitted  provided that the following conditions are met:
9
 *
10
 * 1. Redistributions of  source code must retain the above copyright notice,
11
 *    this list of conditions and the following disclaimer.
12
 *
13
 * 2. Redistributions in  binary form must reproduce the above copyright notice,
14
 *    this list of conditions and the following disclaimer in the documentation
15
 *    and/or other materials provided with the distribution.
16
 *
17
 * 3. The end-user documentation included with the redistribution, if any, must
18
 *    include the following acknowledgment:
19
 *
20
 *    "This product includes software developed by IAIK of Graz University of
21
 *     Technology."
22
 *
23
 *    Alternately, this acknowledgment may appear in the software itself, if
24
 *    and wherever such third-party acknowledgments normally appear.
25
 *
26
 * 4. The names "Graz University of Technology" and "IAIK of Graz University of
27
 *    Technology" must not be used to endorse or promote products derived from
28
 *    this software without prior written permission.
29
 *
30
 * 5. Products derived from this software may not be called
31
 *    "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior
32
 *    written permission of Graz University of Technology.
33
 *
34
 *  THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
35
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
36
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE
38
 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39
 *  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40
 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
41
 *  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
42
 *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43
 *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44
 *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45
 *  POSSIBILITY  OF SUCH DAMAGE.
46
 */
47

48
/*
49
 * pkcs11wrapper.c
50
 * 18.05.2001
51
 *
52
 * This is the implementation of the native functions of the Java to PKCS#11 interface.
53
 * All function use some helper functions to convert the JNI types to PKCS#11 types.
54
 *
55
 * @author Karl Scheibelhofer <Karl.Scheibelhofer@iaik.at>
56
 * @author Martin Schlaeffer <schlaeff@sbox.tugraz.at>
57
 */
58

59

60
#include "pkcs11wrapper.h"
61

62
#include <stdio.h>
63
#include <stdlib.h>
64
#include <string.h>
65
#include <assert.h>
66

67
#include "sun_security_pkcs11_wrapper_PKCS11.h"
68

69
/* declare file private functions */
70

71
CK_VOID_PTR jMechParamToCKMechParamPtrSlow(JNIEnv *env, jobject jParam,
72
        CK_MECHANISM_TYPE ckMech, CK_ULONG *ckpLength);
73

74

75
/*
76
 * converts a CK_DATE pointer into a Java CK_DATE Object.
77
 *
78
 * @param env - used to call JNI funktions to create the new Java object
79
 * @param ckpValue - the pointer to the CK_DATE structure
80
 * @return - the new Java CK_DATE object
81
 */
82
jobject ckDatePtrToJDateObject(JNIEnv *env, const CK_DATE *ckpDate)
83
{
84
    jclass jDateClass;
85
    jmethodID jCtrId;
86
    jobject jDateObject;
87
    jcharArray jYear;
88
    jcharArray jMonth;
89
    jcharArray jDay;
90

91
    /* load CK_DATE class */
92
    jDateClass = (*env)->FindClass(env, CLASS_DATE);
93
    if (jDateClass == NULL) { return NULL; }
94

95
    /* load CK_DATE constructor */
96
    jCtrId = (*env)->GetMethodID(env, jDateClass, "<init>", "([C[C[C)V");
97
    if (jCtrId == NULL) { return NULL; }
98

99
    /* prep all fields */
100
    jYear = ckCharArrayToJCharArray(env, (CK_CHAR_PTR)(ckpDate->year), 4);
101
    if (jYear == NULL) { return NULL; }
102
    jMonth = ckCharArrayToJCharArray(env, (CK_CHAR_PTR)(ckpDate->month), 2);
103
    if (jMonth == NULL) { return NULL; }
104
    jDay = ckCharArrayToJCharArray(env, (CK_CHAR_PTR)(ckpDate->day), 2);
105
    if (jDay == NULL) { return NULL; }
106

107
    /* create new CK_DATE object */
108
    jDateObject =
109
      (*env)->NewObject(env, jDateClass, jCtrId, jYear, jMonth, jDay);
110
    if (jDateObject == NULL) { return NULL; }
111

112
    /* free local references */
113
    (*env)->DeleteLocalRef(env, jDateClass);
114
    (*env)->DeleteLocalRef(env, jYear);
115
    (*env)->DeleteLocalRef(env, jMonth);
116
    (*env)->DeleteLocalRef(env, jDay);
117

118
    return jDateObject ;
119
}
120

121
/*
122
 * converts a CK_VERSION pointer into a Java CK_VERSION Object.
123
 *
124
 * @param env - used to call JNI funktions to create the new Java object
125
 * @param ckpVersion - the pointer to the CK_VERSION structure
126
 * @return the new Java CK_VERSION object
127
 */
128
jobject ckVersionPtrToJVersion(JNIEnv *env, const CK_VERSION_PTR ckpVersion)
129
{
130
    jclass jVersionClass;
131
    jmethodID jCtrId;
132
    jobject jVersionObject;
133
    jint jMajor;
134
    jint jMinor;
135

136
    /* load CK_VERSION class */
137
    jVersionClass = (*env)->FindClass(env, CLASS_VERSION);
138
    if (jVersionClass == NULL) { return NULL; }
139

140
    /* load CK_VERSION constructor */
141
    jCtrId = (*env)->GetMethodID(env, jVersionClass, "<init>", "(II)V");
142
    if (jCtrId == NULL) { return NULL; }
143

144
    /* prep both fields */
145
    jMajor = ckpVersion->major;
146
    jMinor = ckpVersion->minor;
147

148
    /* create new CK_VERSION object */
149
    jVersionObject =
150
      (*env)->NewObject(env, jVersionClass, jCtrId, jMajor, jMinor);
151
    if (jVersionObject == NULL) { return NULL; }
152

153
    /* free local references */
154
    (*env)->DeleteLocalRef(env, jVersionClass);
155

156
    return jVersionObject ;
157
}
158

159
/*
160
 * converts a CK_SESSION_INFO pointer into a Java CK_SESSION_INFO Object.
161
 *
162
 * @param env - used to call JNI funktions to create the new Java object
163
 * @param ckpSessionInfo - the pointer to the CK_SESSION_INFO structure
164
 * @return the new Java CK_SESSION_INFO object
165
 */
166
jobject ckSessionInfoPtrToJSessionInfo(JNIEnv *env, const CK_SESSION_INFO_PTR ckpSessionInfo)
167
{
168
    jclass jSessionInfoClass;
169
    jmethodID jCtrId;
170
    jobject jSessionInfoObject;
171
    jlong jSlotID;
172
    jlong jState;
173
    jlong jFlags;
174
    jlong jDeviceError;
175

176
    /* load CK_SESSION_INFO class */
177
    jSessionInfoClass = (*env)->FindClass(env, CLASS_SESSION_INFO);
178
    if (jSessionInfoClass == NULL) { return NULL; }
179

180
    /* load CK_SESSION_INFO constructor */
181
    jCtrId = (*env)->GetMethodID(env, jSessionInfoClass, "<init>", "(JJJJ)V");
182
    if (jCtrId == NULL) { return NULL; }
183

184
    /* prep all fields */
185
    jSlotID = ckULongToJLong(ckpSessionInfo->slotID);
186
    jState = ckULongToJLong(ckpSessionInfo->state);
187
    jFlags = ckULongToJLong(ckpSessionInfo->flags);
188
    jDeviceError = ckULongToJLong(ckpSessionInfo->ulDeviceError);
189

190
    /* create new CK_SESSION_INFO object */
191
    jSessionInfoObject =
192
      (*env)->NewObject(env, jSessionInfoClass, jCtrId, jSlotID, jState,
193
                        jFlags, jDeviceError);
194
    if (jSessionInfoObject == NULL) { return NULL; }
195

196
    /* free local references */
197
    (*env)->DeleteLocalRef(env, jSessionInfoClass);
198

199
    return jSessionInfoObject ;
200
}
201

202
/*
203
 * converts a CK_ATTRIBUTE pointer into a Java CK_ATTRIBUTE Object.
204
 *
205
 * @param env - used to call JNI funktions to create the new Java object
206
 * @param ckpAttribute - the pointer to the CK_ATTRIBUTE structure
207
 * @return the new Java CK_ATTRIBUTE object
208
 */
209
jobject ckAttributePtrToJAttribute(JNIEnv *env, const CK_ATTRIBUTE_PTR ckpAttribute)
210
{
211
    jclass jAttributeClass;
212
    jmethodID jCtrId;
213
    jobject jAttributeObject;
214
    jlong jType;
215
    jobject jPValue = NULL;
216

217
    jAttributeClass = (*env)->FindClass(env, CLASS_ATTRIBUTE);
218
    if (jAttributeClass == NULL) { return NULL; }
219

220
    /* load CK_INFO constructor */
221
    jCtrId = (*env)->GetMethodID(env, jAttributeClass, "<init>", "(JLjava/lang/Object;)V");
222
    if (jCtrId == NULL) { return NULL; }
223

224
    /* prep both fields */
225
    jType = ckULongToJLong(ckpAttribute->type);
226
    jPValue = ckAttributeValueToJObject(env, ckpAttribute);
227
    if ((*env)->ExceptionCheck(env)) { return NULL; }
228

229
    /* create new CK_ATTRIBUTE object */
230
    jAttributeObject =
231
      (*env)->NewObject(env, jAttributeClass, jCtrId, jType, jPValue);
232
    if (jAttributeObject == NULL) { return NULL; }
233

234
    /* free local references */
235
    (*env)->DeleteLocalRef(env, jAttributeClass);
236
    (*env)->DeleteLocalRef(env, jPValue);
237

238
    return jAttributeObject;
239
}
240

241

242
/*
243
 * converts a Java CK_VERSION object into a CK_VERSION pointer
244
 *
245
 * @param env - used to call JNI funktions to get the values out of the Java object
246
 * @param jVersion - the Java CK_VERSION object to convert
247
 * @return pointer to the new CK_VERSION structure
248
 */
249
CK_VERSION_PTR
250
jVersionToCKVersionPtr(JNIEnv *env, jobject jVersion)
251
{
252
    CK_VERSION_PTR ckpVersion;
253
    jclass jVersionClass;
254
    jfieldID jFieldID;
255
    jbyte jMajor, jMinor;
256

257
    if (jVersion == NULL) {
258
        return NULL;
259
    }
260

261
    // retrieve java values
262
    jVersionClass = (*env)->GetObjectClass(env, jVersion);
263
    if (jVersionClass == NULL) { return NULL; }
264
    jFieldID = (*env)->GetFieldID(env, jVersionClass, "major", "B");
265
    if (jFieldID == NULL) { return NULL; }
266
    jMajor = (*env)->GetByteField(env, jVersion, jFieldID);
267
    jFieldID = (*env)->GetFieldID(env, jVersionClass, "minor", "B");
268
    if (jFieldID == NULL) { return NULL; }
269
    jMinor = (*env)->GetByteField(env, jVersion, jFieldID);
270

271
    // allocate memory for CK_VERSION pointer
272
    ckpVersion = (CK_VERSION_PTR) calloc(1, sizeof(CK_VERSION));
273
    if (ckpVersion == NULL) {
274
        p11ThrowOutOfMemoryError(env, 0);
275
        return NULL;
276
    }
277

278
    // populate using java values
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 CK_DATE pointer
288
 *
289
 * @param env - used to call JNI functions to get the values out of the Java object
290
 * @param jDate - the Java CK_DATE object to convert
291
 * @return pointer to the new CK_DATE structure
292
 */
293
CK_DATE * jDateObjectToCKDatePtr(JNIEnv *env, jobject jDate)
294
{
295
    CK_DATE * ckpDate = NULL;
296
    CK_ULONG ckLength;
297
    jclass jDateClass;
298
    jfieldID jFieldID;
299
    jobject jYear, jMonth, jDay;
300
    jchar *jTempChars = NULL;
301
    CK_ULONG i;
302

303
    if (jDate == NULL) {
304
        return NULL;
305
    }
306

307
    // retrieve java values
308
    jDateClass = (*env)->FindClass(env, CLASS_DATE);
309
    if (jDateClass == NULL) { return NULL; }
310
    jFieldID = (*env)->GetFieldID(env, jDateClass, "year", "[C");
311
    if (jFieldID == NULL) { return NULL; }
312
    jYear = (*env)->GetObjectField(env, jDate, jFieldID);
313
    jFieldID = (*env)->GetFieldID(env, jDateClass, "month", "[C");
314
    if (jFieldID == NULL) { return NULL; }
315
    jMonth = (*env)->GetObjectField(env, jDate, jFieldID);
316
    jFieldID = (*env)->GetFieldID(env, jDateClass, "day", "[C");
317
    if (jFieldID == NULL) { return NULL; }
318
    jDay = (*env)->GetObjectField(env, jDate, jFieldID);
319

320
    // allocate memory for CK_DATE pointer
321
    ckpDate = (CK_DATE *) calloc(1, sizeof(CK_DATE));
322
    if (ckpDate == NULL) {
323
        p11ThrowOutOfMemoryError(env, 0);
324
        return NULL;
325
    }
326

327
    // populate using java values
328
    if (jYear == NULL) {
329
        ckpDate->year[0] = 0;
330
        ckpDate->year[1] = 0;
331
        ckpDate->year[2] = 0;
332
        ckpDate->year[3] = 0;
333
    } else {
334
        ckLength = (*env)->GetArrayLength(env, jYear);
335
        jTempChars = (jchar*) calloc(ckLength, sizeof(jchar));
336
        if (jTempChars == NULL) {
337
            p11ThrowOutOfMemoryError(env, 0);
338
            goto cleanup;
339
        }
340
        (*env)->GetCharArrayRegion(env, jYear, 0, ckLength, jTempChars);
341
        if ((*env)->ExceptionCheck(env)) {
342
            goto cleanup;
343
        }
344

345
        for (i = 0; (i < ckLength) && (i < 4) ; i++) {
346
            ckpDate->year[i] = jCharToCKChar(jTempChars[i]);
347
        }
348
        free(jTempChars);
349
    }
350

351
    if (jMonth == NULL) {
352
        ckpDate->month[0] = 0;
353
        ckpDate->month[1] = 0;
354
    } else {
355
        ckLength = (*env)->GetArrayLength(env, jMonth);
356
        jTempChars = (jchar*) calloc(ckLength, sizeof(jchar));
357
        if (jTempChars == NULL) {
358
            p11ThrowOutOfMemoryError(env, 0);
359
            goto cleanup;
360
        }
361
        (*env)->GetCharArrayRegion(env, jMonth, 0, ckLength, jTempChars);
362
        if ((*env)->ExceptionCheck(env)) {
363
            goto cleanup;
364
        }
365

366
        for (i = 0; (i < ckLength) && (i < 2) ; i++) {
367
            ckpDate->month[i] = jCharToCKChar(jTempChars[i]);
368
        }
369
        free(jTempChars);
370
    }
371

372
    if (jDay == NULL) {
373
        ckpDate->day[0] = 0;
374
        ckpDate->day[1] = 0;
375
    } else {
376
        ckLength = (*env)->GetArrayLength(env, jDay);
377
        jTempChars = (jchar*) calloc(ckLength, sizeof(jchar));
378
        if (jTempChars == NULL) {
379
            p11ThrowOutOfMemoryError(env, 0);
380
            goto cleanup;
381
        }
382
        (*env)->GetCharArrayRegion(env, jDay, 0, ckLength, jTempChars);
383
        if ((*env)->ExceptionCheck(env)) {
384
            goto cleanup;
385
        }
386

387
        for (i = 0; (i < ckLength) && (i < 2) ; i++) {
388
            ckpDate->day[i] = jCharToCKChar(jTempChars[i]);
389
        }
390
        free(jTempChars);
391
    }
392

393
    return ckpDate;
394
cleanup:
395
    free(jTempChars);
396
    free(ckpDate);
397
    return NULL;
398
}
399

400

401
/*
402
 * converts a Java CK_ATTRIBUTE object into a CK_ATTRIBUTE structure
403
 *
404
 * @param env - used to call JNI funktions to get the values out of the Java object
405
 * @param jAttribute - the Java CK_ATTRIBUTE object to convert
406
 * @return the new CK_ATTRIBUTE structure
407
 */
408
CK_ATTRIBUTE jAttributeToCKAttribute(JNIEnv *env, jobject jAttribute)
409
{
410
    CK_ATTRIBUTE ckAttribute;
411
    jclass jAttributeClass;
412
    jfieldID jFieldID;
413
    jlong jType;
414
    jobject jPValue;
415

416
    memset(&ckAttribute, 0, sizeof(CK_ATTRIBUTE));
417

418
    // TBD: what if jAttribute == NULL?!
419
    TRACE0("\nDEBUG: jAttributeToCKAttribute");
420

421
    /* get CK_ATTRIBUTE class */
422
    TRACE0(", getting attribute object class");
423
    jAttributeClass = (*env)->GetObjectClass(env, jAttribute);
424
    if (jAttributeClass == NULL) { return ckAttribute; }
425

426
    /* get type */
427
    TRACE0(", getting type field");
428
    jFieldID = (*env)->GetFieldID(env, jAttributeClass, "type", "J");
429
    if (jFieldID == NULL) { return ckAttribute; }
430
    jType = (*env)->GetLongField(env, jAttribute, jFieldID);
431
    TRACE1(", type=0x%lX", jType);
432

433
    /* get pValue */
434
    TRACE0(", getting pValue field");
435
    jFieldID = (*env)->GetFieldID(env, jAttributeClass, "pValue", "Ljava/lang/Object;");
436
    if (jFieldID == NULL) { return ckAttribute; }
437
    jPValue = (*env)->GetObjectField(env, jAttribute, jFieldID);
438
    TRACE1(", pValue=%p", jPValue);
439

440
    ckAttribute.type = jLongToCKULong(jType);
441
    TRACE0(", converting pValue to primitive object");
442

443
    /* convert the Java pValue object to a CK-type pValue pointer */
444
    ckAttribute.pValue = jObjectToPrimitiveCKObjectPtr(env, jPValue, &(ckAttribute.ulValueLen));
445

446
    TRACE0("\nDEBUG: jAttributeToCKAttribute FINISHED\n");
447

448
    return ckAttribute ;
449
}
450

451
void masterKeyDeriveParamToCKMasterKeyDeriveParam(JNIEnv *env, jobject jParam,
452
        jclass masterKeyDeriveParamClass,
453
        CK_VERSION_PTR* cKMasterKeyDeriveParamVersion,
454
        CK_SSL3_RANDOM_DATA* cKMasterKeyDeriveParamRandomInfo) {
455
    jfieldID fieldID;
456
    jclass jSsl3RandomDataClass;
457
    jobject jRandomInfo, jRIClientRandom, jRIServerRandom, jVersion;
458

459
    // retrieve java values
460
    fieldID = (*env)->GetFieldID(env, masterKeyDeriveParamClass, "RandomInfo",
461
            "Lsun/security/pkcs11/wrapper/CK_SSL3_RANDOM_DATA;");
462
    if (fieldID == NULL) { return; }
463
    jRandomInfo = (*env)->GetObjectField(env, jParam, fieldID);
464
    jSsl3RandomDataClass = (*env)->FindClass(env, CLASS_SSL3_RANDOM_DATA);
465
    if (jSsl3RandomDataClass == NULL) { return; }
466
    fieldID = (*env)->GetFieldID(env, jSsl3RandomDataClass, "pClientRandom", "[B");
467
    if (fieldID == NULL) { return; }
468
    jRIClientRandom = (*env)->GetObjectField(env, jRandomInfo, fieldID);
469
    fieldID = (*env)->GetFieldID(env, jSsl3RandomDataClass, "pServerRandom", "[B");
470
    if (fieldID == NULL) { return; }
471
    jRIServerRandom = (*env)->GetObjectField(env, jRandomInfo, fieldID);
472
    fieldID = (*env)->GetFieldID(env, masterKeyDeriveParamClass, "pVersion",
473
            "Lsun/security/pkcs11/wrapper/CK_VERSION;");
474
    if (fieldID == NULL) { return; }
475
    jVersion = (*env)->GetObjectField(env, jParam, fieldID);
476

477
    // populate using java values
478
    *cKMasterKeyDeriveParamVersion = jVersionToCKVersionPtr(env, jVersion);
479
    if ((*env)->ExceptionCheck(env)) { return; }
480
    jByteArrayToCKByteArray(env, jRIClientRandom,
481
            &(cKMasterKeyDeriveParamRandomInfo->pClientRandom),
482
            &(cKMasterKeyDeriveParamRandomInfo->ulClientRandomLen));
483
    if ((*env)->ExceptionCheck(env)) {
484
        goto cleanup;
485
    }
486
    jByteArrayToCKByteArray(env, jRIServerRandom,
487
            &(cKMasterKeyDeriveParamRandomInfo->pServerRandom),
488
            &(cKMasterKeyDeriveParamRandomInfo->ulServerRandomLen));
489
    if ((*env)->ExceptionCheck(env)) {
490
        goto cleanup;
491
    }
492
    return;
493
cleanup:
494
    free(*cKMasterKeyDeriveParamVersion);
495
    free(cKMasterKeyDeriveParamRandomInfo->pClientRandom);
496
    cKMasterKeyDeriveParamRandomInfo->ulClientRandomLen = 0L;
497
    free(cKMasterKeyDeriveParamRandomInfo->pServerRandom);
498
    cKMasterKeyDeriveParamRandomInfo->ulServerRandomLen = 0L;
499
    // explicitly set to NULL to ensure no double free possible
500
    *cKMasterKeyDeriveParamVersion = NULL;
501
    cKMasterKeyDeriveParamRandomInfo->pClientRandom = NULL;
502
    cKMasterKeyDeriveParamRandomInfo->pServerRandom = NULL;
503
}
504

505
/*
506
 * converts the Java CK_SSL3_MASTER_KEY_DERIVE_PARAMS object to a
507
 * CK_SSL3_MASTER_KEY_DERIVE_PARAMS pointer
508
 *
509
 * @param env - used to call JNI functions to get the Java classes and objects
510
 * @param jParam - the Java CK_SSL3_MASTER_KEY_DERIVE_PARAMS object to convert
511
 * @param pLength - length of the allocated memory of the returned pointer
512
 * @return pointer to the new CK_SSL3_MASTER_KEY_DERIVE_PARAMS structure
513
 */
514
CK_SSL3_MASTER_KEY_DERIVE_PARAMS_PTR
515
jSsl3MasterKeyDeriveParamToCKSsl3MasterKeyDeriveParamPtr(JNIEnv *env,
516
        jobject jParam, CK_ULONG *pLength)
517
{
518
    CK_SSL3_MASTER_KEY_DERIVE_PARAMS_PTR ckParamPtr;
519
    jclass jSsl3MasterKeyDeriveParamsClass;
520

521
    if (pLength != NULL) {
522
        *pLength = 0L;
523
    }
524

525
    // allocate memory for CK_SSL3_MASTER_KEY_DERIVE_PARAMS pointer
526
    ckParamPtr = calloc(1, sizeof(CK_SSL3_MASTER_KEY_DERIVE_PARAMS));
527
    if (ckParamPtr == NULL) {
528
        p11ThrowOutOfMemoryError(env, 0);
529
        return NULL;
530
    }
531

532
    // retrieve and populate using java values
533
    jSsl3MasterKeyDeriveParamsClass =
534
            (*env)->FindClass(env, CLASS_SSL3_MASTER_KEY_DERIVE_PARAMS);
535
    if (jSsl3MasterKeyDeriveParamsClass == NULL) {
536
        goto cleanup;
537
    }
538
    masterKeyDeriveParamToCKMasterKeyDeriveParam(env, jParam,
539
            jSsl3MasterKeyDeriveParamsClass,
540
            &(ckParamPtr->pVersion), &(ckParamPtr->RandomInfo));
541
    if ((*env)->ExceptionCheck(env)) {
542
        goto cleanup;
543
    }
544

545
    if (pLength != NULL) {
546
        *pLength = sizeof(CK_SSL3_MASTER_KEY_DERIVE_PARAMS);
547
    }
548
    return ckParamPtr;
549
cleanup:
550
    free(ckParamPtr);
551
    return NULL;
552
}
553

554
/*
555
 * converts the Java CK_TLS12_MASTER_KEY_DERIVE_PARAMS object to a
556
 * CK_TLS12_MASTER_KEY_DERIVE_PARAMS pointer
557
 *
558
 * @param env - used to call JNI functions to get the Java classes and objects
559
 * @param jParam - the Java CK_TLS12_MASTER_KEY_DERIVE_PARAMS object to convert
560
 * @param pLength - length of the allocated memory of the returned pointer
561
 * @return pointer to the new CK_TLS12_MASTER_KEY_DERIVE_PARAMS structure
562
 */
563
CK_TLS12_MASTER_KEY_DERIVE_PARAMS_PTR
564
jTls12MasterKeyDeriveParamToCKTls12MasterKeyDeriveParamPtr(JNIEnv *env,
565
        jobject jParam, CK_ULONG *pLength)
566
{
567
    CK_TLS12_MASTER_KEY_DERIVE_PARAMS_PTR ckParamPtr;
568
    jclass jTls12MasterKeyDeriveParamsClass;
569
    jfieldID fieldID;
570
    jlong prfHashMechanism;
571

572
    if (pLength != NULL) {
573
        *pLength = 0L;
574
    }
575

576
    // retrieve java values
577
    jTls12MasterKeyDeriveParamsClass =
578
        (*env)->FindClass(env, CLASS_TLS12_MASTER_KEY_DERIVE_PARAMS);
579
    if (jTls12MasterKeyDeriveParamsClass == NULL) { return NULL; }
580
    fieldID = (*env)->GetFieldID(env,
581
            jTls12MasterKeyDeriveParamsClass, "prfHashMechanism", "J");
582
    if (fieldID == NULL) { return NULL; }
583
    prfHashMechanism = (*env)->GetLongField(env, jParam, fieldID);
584

585
    // allocate memory for CK_TLS12_MASTER_KEY_DERIVE_PARAMS pointer
586
    ckParamPtr = calloc(1, sizeof(CK_TLS12_MASTER_KEY_DERIVE_PARAMS));
587
    if (ckParamPtr == NULL) {
588
        p11ThrowOutOfMemoryError(env, 0);
589
        return NULL;
590
    }
591

592
    // populate using java values
593
    masterKeyDeriveParamToCKMasterKeyDeriveParam(env, jParam,
594
            jTls12MasterKeyDeriveParamsClass, &ckParamPtr->pVersion,
595
            &ckParamPtr->RandomInfo);
596
    if ((*env)->ExceptionCheck(env)) {
597
        goto cleanup;
598
    }
599

600
    ckParamPtr->prfHashMechanism = (CK_MECHANISM_TYPE) prfHashMechanism;
601

602
    if (pLength != NULL) {
603
        *pLength = sizeof(CK_TLS12_MASTER_KEY_DERIVE_PARAMS);
604
    }
605
    return ckParamPtr;
606
cleanup:
607
    free(ckParamPtr);
608
    return NULL;
609
}
610

611
/*
612
 * converts the Java CK_TLS_PRF_PARAMS object to a CK_TLS_PRF_PARAMS pointer
613
 *
614
 * @param env - used to call JNI functions to get the Java classes and objects
615
 * @param jParam - the Java CK_TLS_PRF_PARAMS object to convert
616
 * @param pLength - length of the allocated memory of the returned pointer
617
 * @return pointer to the new CK_TLS_PRF_PARAMS structure
618
 */
619
CK_TLS_PRF_PARAMS_PTR
620
jTlsPrfParamsToCKTlsPrfParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
621
{
622
    CK_TLS_PRF_PARAMS_PTR ckParamPtr;
623
    jclass jTlsPrfParamsClass;
624
    jfieldID fieldID;
625
    jobject jSeed, jLabel, jOutput;
626

627
    if (pLength != NULL) {
628
        *pLength = 0;
629
    }
630

631
    // retrieve java values
632
    jTlsPrfParamsClass = (*env)->FindClass(env, CLASS_TLS_PRF_PARAMS);
633
    if (jTlsPrfParamsClass == NULL) { return NULL; }
634
    fieldID = (*env)->GetFieldID(env, jTlsPrfParamsClass, "pSeed", "[B");
635
    if (fieldID == NULL) { return NULL; }
636
    jSeed = (*env)->GetObjectField(env, jParam, fieldID);
637
    fieldID = (*env)->GetFieldID(env, jTlsPrfParamsClass, "pLabel", "[B");
638
    if (fieldID == NULL) { return NULL; }
639
    jLabel = (*env)->GetObjectField(env, jParam, fieldID);
640
    fieldID = (*env)->GetFieldID(env, jTlsPrfParamsClass, "pOutput", "[B");
641
    if (fieldID == NULL) { return NULL; }
642
    jOutput = (*env)->GetObjectField(env, jParam, fieldID);
643

644
    // allocate memory for CK_TLS_PRF_PARAMS pointer
645
    ckParamPtr = calloc(1, sizeof(CK_TLS_PRF_PARAMS));
646
    if (ckParamPtr == NULL) {
647
        p11ThrowOutOfMemoryError(env, 0);
648
        return NULL;
649
    }
650

651
    // populate using java values
652
    jByteArrayToCKByteArray(env, jSeed, &(ckParamPtr->pSeed), &(ckParamPtr->ulSeedLen));
653
    if ((*env)->ExceptionCheck(env)) {
654
        goto cleanup;
655
    }
656
    jByteArrayToCKByteArray(env, jLabel, &(ckParamPtr->pLabel), &(ckParamPtr->ulLabelLen));
657
    if ((*env)->ExceptionCheck(env)) {
658
        goto cleanup;
659
    }
660
    ckParamPtr->pulOutputLen = calloc(1, sizeof(CK_ULONG));
661
    if (ckParamPtr->pulOutputLen == NULL) {
662
        goto cleanup;
663
    }
664
    jByteArrayToCKByteArray(env, jOutput, &(ckParamPtr->pOutput), ckParamPtr->pulOutputLen);
665
    if ((*env)->ExceptionCheck(env)) {
666
        goto cleanup;
667
    }
668

669
    if (pLength != NULL) {
670
        *pLength = sizeof(CK_TLS_PRF_PARAMS);
671
    }
672
    return ckParamPtr;
673
cleanup:
674
    free(ckParamPtr->pSeed);
675
    free(ckParamPtr->pLabel);
676
    free(ckParamPtr->pOutput);
677
    free(ckParamPtr->pulOutputLen);
678
    free(ckParamPtr);
679
    return NULL;
680
}
681

682
/*
683
 * converts the Java CK_TLS_MAC_PARAMS object to a CK_TLS_MAC_PARAMS pointer
684
 *
685
 * @param env - used to call JNI functions to get the Java classes and objects
686
 * @param jParam - the Java CK_TLS_MAC_PARAMS object to convert
687
 * @param pLength - length of the allocated memory of the returned pointer
688
 * @return pointer to the new CK_TLS_MAC_PARAMS structure
689
 */
690

691
CK_TLS_MAC_PARAMS_PTR
692
jTlsMacParamsToCKTlsMacParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
693
{
694
    CK_TLS_MAC_PARAMS_PTR ckParamPtr;
695
    jclass jTlsMacParamsClass;
696
    jfieldID fieldID;
697
    jlong jPrfMechanism, jUlMacLength, jUlServerOrClient;
698

699
    if (pLength != NULL) {
700
        *pLength = 0L;
701
    }
702

703
    // retrieve java values
704
    jTlsMacParamsClass = (*env)->FindClass(env, CLASS_TLS_MAC_PARAMS);
705
    if (jTlsMacParamsClass == NULL) { return NULL; }
706
    fieldID = (*env)->GetFieldID(env, jTlsMacParamsClass, "prfMechanism", "J");
707
    if (fieldID == NULL) { return NULL; }
708
    jPrfMechanism = (*env)->GetLongField(env, jParam, fieldID);
709
    fieldID = (*env)->GetFieldID(env, jTlsMacParamsClass, "ulMacLength", "J");
710
    if (fieldID == NULL) { return NULL; }
711
    jUlMacLength = (*env)->GetLongField(env, jParam, fieldID);
712
    fieldID = (*env)->GetFieldID(env, jTlsMacParamsClass, "ulServerOrClient", "J");
713
    if (fieldID == NULL) { return NULL; }
714
    jUlServerOrClient = (*env)->GetLongField(env, jParam, fieldID);
715

716
    // allocate memory for CK_TLS_MAC_PARAMS pointer
717
    ckParamPtr = calloc(1, sizeof(CK_TLS_MAC_PARAMS));
718
    if (ckParamPtr == NULL) {
719
        p11ThrowOutOfMemoryError(env, 0);
720
        return NULL;
721
    }
722

723
    // populate using java values
724
    ckParamPtr->prfHashMechanism = jLongToCKULong(jPrfMechanism);
725
    ckParamPtr->ulMacLength = jLongToCKULong(jUlMacLength);
726
    ckParamPtr->ulServerOrClient = jLongToCKULong(jUlServerOrClient);
727

728
    if (pLength != NULL) {
729
        *pLength = sizeof(CK_TLS_MAC_PARAMS);
730
    }
731
    return ckParamPtr;
732
}
733

734
void keyMatParamToCKKeyMatParam(JNIEnv *env, jobject jParam,
735
        jclass jKeyMatParamClass,
736
        CK_ULONG* cKKeyMatParamUlMacSizeInBits,
737
        CK_ULONG* cKKeyMatParamUlKeySizeInBits,
738
        CK_ULONG* cKKeyMatParamUlIVSizeInBits,
739
        CK_BBOOL* cKKeyMatParamBIsExport,
740
        CK_SSL3_RANDOM_DATA* cKKeyMatParamRandomInfo,
741
        CK_SSL3_KEY_MAT_OUT_PTR* cKKeyMatParamPReturnedKeyMaterial)
742
{
743
    jclass jSsl3RandomDataClass, jSsl3KeyMatOutClass;
744
    jfieldID fieldID;
745
    jlong jMacSizeInBits, jKeySizeInBits, jIVSizeInBits;
746
    jboolean jIsExport;
747
    jobject jRandomInfo, jRIClientRandom, jRIServerRandom;
748
    jobject jReturnedKeyMaterial, jRMIvClient, jRMIvServer;
749
    CK_ULONG ckTemp;
750

751
    // the pointer arguments should already be initialized by caller
752

753
    // retrieve java values
754
    fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "ulMacSizeInBits", "J");
755
    if (fieldID == NULL) { return; }
756
    jMacSizeInBits = (*env)->GetLongField(env, jParam, fieldID);
757
    fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "ulKeySizeInBits", "J");
758
    if (fieldID == NULL) { return; }
759
    jKeySizeInBits = (*env)->GetLongField(env, jParam, fieldID);
760
    fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "ulIVSizeInBits", "J");
761
    if (fieldID == NULL) { return; }
762
    jIVSizeInBits = (*env)->GetLongField(env, jParam, fieldID);
763
    fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "bIsExport", "Z");
764
    if (fieldID == NULL) { return; }
765
    jIsExport = (*env)->GetBooleanField(env, jParam, fieldID);
766
    jSsl3RandomDataClass = (*env)->FindClass(env, CLASS_SSL3_RANDOM_DATA);
767
    if (jSsl3RandomDataClass == NULL) { return; }
768
    fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "RandomInfo",
769
            "Lsun/security/pkcs11/wrapper/CK_SSL3_RANDOM_DATA;");
770
    if (fieldID == NULL) { return; }
771
    jRandomInfo = (*env)->GetObjectField(env, jParam, fieldID);
772
    fieldID = (*env)->GetFieldID(env, jSsl3RandomDataClass, "pClientRandom", "[B");
773
    if (fieldID == NULL) { return; }
774
    jRIClientRandom = (*env)->GetObjectField(env, jRandomInfo, fieldID);
775
    fieldID = (*env)->GetFieldID(env, jSsl3RandomDataClass, "pServerRandom", "[B");
776
    if (fieldID == NULL) { return; }
777
    jRIServerRandom = (*env)->GetObjectField(env, jRandomInfo, fieldID);
778
    jSsl3KeyMatOutClass = (*env)->FindClass(env, CLASS_SSL3_KEY_MAT_OUT);
779
    if (jSsl3KeyMatOutClass == NULL) { return; }
780
    fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "pReturnedKeyMaterial",
781
            "Lsun/security/pkcs11/wrapper/CK_SSL3_KEY_MAT_OUT;");
782
    if (fieldID == NULL) { return; }
783
    jReturnedKeyMaterial = (*env)->GetObjectField(env, jParam, fieldID);
784
    fieldID = (*env)->GetFieldID(env, jSsl3KeyMatOutClass, "pIVClient", "[B");
785
    if (fieldID == NULL) { return; }
786
    jRMIvClient = (*env)->GetObjectField(env, jReturnedKeyMaterial, fieldID);
787
    fieldID = (*env)->GetFieldID(env, jSsl3KeyMatOutClass, "pIVServer", "[B");
788
    if (fieldID == NULL) { return; }
789
    jRMIvServer = (*env)->GetObjectField(env, jReturnedKeyMaterial, fieldID);
790

791
    // populate the specified pointers using java values
792
    *cKKeyMatParamUlMacSizeInBits = jLongToCKULong(jMacSizeInBits);
793
    *cKKeyMatParamUlKeySizeInBits = jLongToCKULong(jKeySizeInBits);
794
    *cKKeyMatParamUlIVSizeInBits = jLongToCKULong(jIVSizeInBits);
795
    *cKKeyMatParamBIsExport = jBooleanToCKBBool(jIsExport);
796
    jByteArrayToCKByteArray(env, jRIClientRandom,
797
            &(cKKeyMatParamRandomInfo->pClientRandom),
798
            &(cKKeyMatParamRandomInfo->ulClientRandomLen));
799
    if ((*env)->ExceptionCheck(env)) {
800
        // just return as no memory allocation yet
801
        return;
802
    }
803
    jByteArrayToCKByteArray(env, jRIServerRandom,
804
            &(cKKeyMatParamRandomInfo->pServerRandom),
805
            &(cKKeyMatParamRandomInfo->ulServerRandomLen));
806
    if ((*env)->ExceptionCheck(env)) {
807
        goto cleanup;
808
    }
809
    /* allocate memory for pReturnedKeyMaterial */
810
    *cKKeyMatParamPReturnedKeyMaterial =
811
            (CK_SSL3_KEY_MAT_OUT_PTR) calloc(1, sizeof(CK_SSL3_KEY_MAT_OUT));
812
    if (*cKKeyMatParamPReturnedKeyMaterial == NULL) {
813
        p11ThrowOutOfMemoryError(env, 0);
814
        goto cleanup;
815
    }
816

817
    // the handles are output params only, no need to fetch them from Java
818
    (*cKKeyMatParamPReturnedKeyMaterial)->hClientMacSecret = 0;
819
    (*cKKeyMatParamPReturnedKeyMaterial)->hServerMacSecret = 0;
820
    (*cKKeyMatParamPReturnedKeyMaterial)->hClientKey = 0;
821
    (*cKKeyMatParamPReturnedKeyMaterial)->hServerKey = 0;
822

823
    jByteArrayToCKByteArray(env, jRMIvClient,
824
            &((*cKKeyMatParamPReturnedKeyMaterial)->pIVClient), &ckTemp);
825
    if ((*env)->ExceptionCheck(env)) {
826
        goto cleanup;
827
    }
828
    jByteArrayToCKByteArray(env, jRMIvServer,
829
            &((*cKKeyMatParamPReturnedKeyMaterial)->pIVServer), &ckTemp);
830
    if ((*env)->ExceptionCheck(env)) {
831
        goto cleanup;
832
    }
833

834
    return;
835
cleanup:
836
    free(cKKeyMatParamRandomInfo->pClientRandom);
837
    free(cKKeyMatParamRandomInfo->pServerRandom);
838
    if ((*cKKeyMatParamPReturnedKeyMaterial) != NULL) {
839
        free((*cKKeyMatParamPReturnedKeyMaterial)->pIVClient);
840
        free(*cKKeyMatParamPReturnedKeyMaterial);
841
    }
842
    // explicitly set to NULL to ensure no double free possible
843
    cKKeyMatParamRandomInfo->pClientRandom = NULL;
844
    cKKeyMatParamRandomInfo->pServerRandom = NULL;
845
    *cKKeyMatParamPReturnedKeyMaterial = NULL;
846
    return;
847
}
848
/*
849
 * converts the Java CK_SSL3_KEY_MAT_PARAMS object to a
850
 * CK_SSL3_KEY_MAT_PARAMS pointer
851
 *
852
 * @param env - used to call JNI funktions to get the Java classes and objects
853
 * @param jParam - the Java CK_SSL3_KEY_MAT_PARAMS object to convert
854
 * @param pLength - length of the allocated memory of the returned pointer
855
 * @return pointer to the new CK_SSL3_KEY_MAT_PARAMS structure
856
 */
857
CK_SSL3_KEY_MAT_PARAMS_PTR
858
jSsl3KeyMatParamToCKSsl3KeyMatParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
859
{
860
    CK_SSL3_KEY_MAT_PARAMS_PTR ckParamPtr;
861
    jclass jSsl3KeyMatParamsClass;
862

863
    if (pLength != NULL) {
864
        *pLength = 0;
865
    }
866

867
    // allocate memory for CK_SSL3_KEY_MAT_PARAMS pointer
868
    ckParamPtr = calloc(1, sizeof(CK_SSL3_KEY_MAT_PARAMS));
869
    if (ckParamPtr == NULL) {
870
        p11ThrowOutOfMemoryError(env, 0);
871
        return NULL;
872
    }
873

874
    // retrieve and  populate using java values
875
    jSsl3KeyMatParamsClass = (*env)->FindClass(env,
876
            CLASS_SSL3_KEY_MAT_PARAMS);
877
    if (jSsl3KeyMatParamsClass == NULL) {
878
        goto cleanup;
879
    }
880
    keyMatParamToCKKeyMatParam(env, jParam, jSsl3KeyMatParamsClass,
881
            &(ckParamPtr->ulMacSizeInBits), &(ckParamPtr->ulKeySizeInBits),
882
            &(ckParamPtr->ulIVSizeInBits), &(ckParamPtr->bIsExport),
883
            &(ckParamPtr->RandomInfo), &(ckParamPtr->pReturnedKeyMaterial));
884
    if ((*env)->ExceptionCheck(env)) {
885
        goto cleanup;
886
    }
887

888
    if (pLength != NULL) {
889
        *pLength = sizeof(CK_SSL3_KEY_MAT_PARAMS);
890
    }
891
    return ckParamPtr;
892
cleanup:
893
    free(ckParamPtr);
894
    return NULL;
895
}
896

897
/*
898
 * converts the Java CK_TLS12_KEY_MAT_PARAMS object to a
899
 * CK_TLS12_KEY_MAT_PARAMS pointer
900
 *
901
 * @param env - used to call JNI functions to get the Java classes and objects
902
 * @param jParam - the Java CK_TLS12_KEY_MAT_PARAMS object to convert
903
 * @param pLength - length of the allocated memory of the returned pointer
904
 * @return pointer to the new CK_TLS12_KEY_MAT_PARAMS structure
905
 */
906
CK_TLS12_KEY_MAT_PARAMS_PTR jTls12KeyMatParamToCKTls12KeyMatParamPtr(JNIEnv *env,
907
        jobject jParam, CK_ULONG *pLength)
908
{
909
    CK_TLS12_KEY_MAT_PARAMS_PTR ckParamPtr;
910
    jclass jTls12KeyMatParamsClass;
911
    jfieldID fieldID;
912
    jlong prfHashMechanism;
913

914
    if (pLength != NULL) {
915
        *pLength = 0;
916
    }
917

918
    // retrieve java values
919
    jTls12KeyMatParamsClass = (*env)->FindClass(env,
920
            CLASS_TLS12_KEY_MAT_PARAMS);
921
    if (jTls12KeyMatParamsClass == NULL) { return NULL; }
922
    fieldID = (*env)->GetFieldID(env, jTls12KeyMatParamsClass,
923
            "prfHashMechanism", "J");
924
    if (fieldID == NULL) { return NULL; }
925
    prfHashMechanism = (*env)->GetLongField(env, jParam, fieldID);
926

927
    // allocate memory for CK_TLS12_KEY_MAT_PARAMS pointer
928
    ckParamPtr = calloc(1, sizeof(CK_TLS12_KEY_MAT_PARAMS));
929
    if (ckParamPtr == NULL) {
930
        p11ThrowOutOfMemoryError(env, 0);
931
        return NULL;
932
    }
933

934
    // populate using java values
935
    keyMatParamToCKKeyMatParam(env, jParam, jTls12KeyMatParamsClass,
936
            &(ckParamPtr->ulMacSizeInBits), &(ckParamPtr->ulKeySizeInBits),
937
            &(ckParamPtr->ulIVSizeInBits), &(ckParamPtr->bIsExport),
938
            &(ckParamPtr->RandomInfo), &(ckParamPtr->pReturnedKeyMaterial));
939
    if ((*env)->ExceptionCheck(env)) {
940
        goto cleanup;
941
    }
942
    ckParamPtr->prfHashMechanism = (CK_MECHANISM_TYPE)prfHashMechanism;
943

944
    if (pLength != NULL) {
945
        *pLength = sizeof(CK_TLS12_KEY_MAT_PARAMS);
946
    }
947
    return ckParamPtr;
948
cleanup:
949
    free(ckParamPtr);
950
    return NULL;
951
}
952

953
/*
954
 * converts the Java CK_AES_CTR_PARAMS object to a CK_AES_CTR_PARAMS pointer
955
 *
956
 * @param env - used to call JNI funktions to get the Java classes and objects
957
 * @param jParam - the Java CK_AES_CTR_PARAMS object to convert
958
 * @param pLength - length of the allocated memory of the returned pointer
959
 * @return pointer to the new CK_AES_CTR_PARAMS structure
960
 */
961
CK_AES_CTR_PARAMS_PTR
962
jAesCtrParamsToCKAesCtrParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
963
{
964
    CK_AES_CTR_PARAMS_PTR ckParamPtr;
965
    jclass jAesCtrParamsClass;
966
    jfieldID fieldID;
967
    jlong jCounterBits;
968
    jobject jCb;
969
    CK_BYTE_PTR ckBytes = NULL;
970
    CK_ULONG ckTemp;
971

972
    if (pLength != NULL) {
973
        *pLength = 0L;
974
    }
975

976
    // retrieve java values
977
    jAesCtrParamsClass = (*env)->FindClass(env, CLASS_AES_CTR_PARAMS);
978
    if (jAesCtrParamsClass == NULL) { return NULL; }
979
    if (!(*env)->IsInstanceOf(env, jParam, jAesCtrParamsClass)) {
980
        return NULL;
981
    }
982
    fieldID = (*env)->GetFieldID(env, jAesCtrParamsClass, "ulCounterBits", "J");
983
    if (fieldID == NULL) { return NULL; }
984
    jCounterBits = (*env)->GetLongField(env, jParam, fieldID);
985
    fieldID = (*env)->GetFieldID(env, jAesCtrParamsClass, "cb", "[B");
986
    if (fieldID == NULL) { return NULL; }
987
    jCb = (*env)->GetObjectField(env, jParam, fieldID);
988

989
    // allocate memory for CK_AES_CTR_PARAMS pointer
990
    ckParamPtr = calloc(1, sizeof(CK_AES_CTR_PARAMS));
991
    if (ckParamPtr == NULL) {
992
        p11ThrowOutOfMemoryError(env, 0);
993
        return NULL;
994
    }
995

996
    // populate using java values
997
    jByteArrayToCKByteArray(env, jCb, &ckBytes, &ckTemp);
998
    if ((*env)->ExceptionCheck(env) || ckTemp != 16) {
999
        goto cleanup;
1000
    }
1001
    memcpy(ckParamPtr->cb, ckBytes, ckTemp);
1002
    free(ckBytes);
1003

1004
    ckParamPtr->ulCounterBits = jLongToCKULong(jCounterBits);
1005

1006
    if (pLength != NULL) {
1007
        *pLength = sizeof(CK_AES_CTR_PARAMS);
1008
    }
1009
    return ckParamPtr;
1010
cleanup:
1011
    free(ckBytes);
1012
    free(ckParamPtr);
1013
    return NULL;
1014
}
1015

1016
/*
1017
 * converts the Java CK_GCM_PARAMS object to a CK_GCM_PARAMS pointer
1018
 * Note: Early NSS versions crash w/ CK_GCM_PARAMS and need to use
1019
 * CK_GCM_PARAMS_NO_IVBITS to avoid SIGSEGV.
1020
 *
1021
 * @param env - used to call JNI funktions to get the Java classes and objects
1022
 * @param jParam - the Java CK_GCM_PARAMS object to convert
1023
 * @param pLength - length of the allocated memory of the returned pointer
1024
 * @return pointer to the new CK_GCM_PARAMS structure
1025
 */
1026
CK_GCM_PARAMS_PTR
1027
jGCMParamsToCKGCMParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
1028
{
1029
    CK_GCM_PARAMS_PTR ckParamPtr;
1030
    jclass jGcmParamsClass;
1031
    jfieldID fieldID;
1032
    jobject jIv, jAad;
1033
    jlong jTagLen;
1034

1035
    TRACE0("DEBUG jGCMParamsToCKGCMParam is called\n");
1036

1037
    if (pLength != NULL) {
1038
        *pLength = 0L;
1039
    }
1040

1041
    // retrieve java values
1042
    jGcmParamsClass = (*env)->FindClass(env, CLASS_GCM_PARAMS);
1043
    if (jGcmParamsClass == NULL) { return NULL; }
1044
    if (!(*env)->IsInstanceOf(env, jParam, jGcmParamsClass)) {
1045
        return NULL;
1046
    }
1047
    fieldID = (*env)->GetFieldID(env, jGcmParamsClass, "iv", "[B");
1048
    if (fieldID == NULL) { return NULL; }
1049
    jIv = (*env)->GetObjectField(env, jParam, fieldID);
1050
    fieldID = (*env)->GetFieldID(env, jGcmParamsClass, "aad", "[B");
1051
    if (fieldID == NULL) { return NULL; }
1052
    jAad = (*env)->GetObjectField(env, jParam, fieldID);
1053
    fieldID = (*env)->GetFieldID(env, jGcmParamsClass, "tagBits", "J");
1054
    if (fieldID == NULL) { return NULL; }
1055
    jTagLen = (*env)->GetLongField(env, jParam, fieldID);
1056

1057
    // allocate memory for CK_GCM_PARAMS pointer
1058
    ckParamPtr = calloc(1, sizeof(CK_GCM_PARAMS));
1059
    if (ckParamPtr == NULL) {
1060
        p11ThrowOutOfMemoryError(env, 0);
1061
        return NULL;
1062
    }
1063

1064
    // populate using java values
1065
    jByteArrayToCKByteArray(env, jIv, &(ckParamPtr->pIv), &(ckParamPtr->ulIvLen));
1066
    if ((*env)->ExceptionCheck(env)) {
1067
        goto cleanup;
1068
    }
1069
    // adjust since the value is in bits
1070
    ckParamPtr->ulIvBits = ckParamPtr->ulIvLen << 3;
1071

1072
    jByteArrayToCKByteArray(env, jAad, &(ckParamPtr->pAAD), &(ckParamPtr->ulAADLen));
1073
    if ((*env)->ExceptionCheck(env)) {
1074
        goto cleanup;
1075
    }
1076

1077
    ckParamPtr->ulTagBits = jLongToCKULong(jTagLen);
1078

1079
    if (pLength != NULL) {
1080
        *pLength = sizeof(CK_GCM_PARAMS);
1081
    }
1082
    TRACE1("Created inner GCM_PARAMS PTR %p\n", ckParamPtr);
1083
    return ckParamPtr;
1084
cleanup:
1085
    free(ckParamPtr->pIv);
1086
    free(ckParamPtr->pAAD);
1087
    free(ckParamPtr);
1088
    return NULL;
1089
}
1090

1091
/*
1092
 * converts the Java CK_CCM_PARAMS object to a CK_CCM_PARAMS pointer
1093
 *
1094
 * @param env - used to call JNI functions to get the Java classes and objects
1095
 * @param jParam - the Java CK_CCM_PARAMS object to convert
1096
 * @param pLength - length of the allocated memory of the returned pointer
1097
 * @return pointer to the new CK_CCM_PARAMS structure
1098
 */
1099
CK_CCM_PARAMS_PTR
1100
jCCMParamsToCKCCMParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
1101
{
1102
    CK_CCM_PARAMS_PTR ckParamPtr;
1103
    jclass jCcmParamsClass;
1104
    jfieldID fieldID;
1105
    jobject jNonce, jAad;
1106
    jlong jDataLen, jMacLen;
1107

1108
    if (pLength != NULL) {
1109
        *pLength = 0;
1110
    }
1111

1112
    // retrieve java values
1113
    jCcmParamsClass = (*env)->FindClass(env, CLASS_CCM_PARAMS);
1114
    if (jCcmParamsClass == NULL) { return NULL; }
1115
    if (!(*env)->IsInstanceOf(env, jParam, jCcmParamsClass)) {
1116
        return NULL;
1117
    }
1118
    fieldID = (*env)->GetFieldID(env, jCcmParamsClass, "dataLen", "J");
1119
    if (fieldID == NULL) { return NULL; }
1120
    jDataLen = (*env)->GetLongField(env, jParam, fieldID);
1121
    fieldID = (*env)->GetFieldID(env, jCcmParamsClass, "nonce", "[B");
1122
    if (fieldID == NULL) { return NULL; }
1123
    jNonce = (*env)->GetObjectField(env, jParam, fieldID);
1124
    fieldID = (*env)->GetFieldID(env, jCcmParamsClass, "aad", "[B");
1125
    if (fieldID == NULL) { return NULL; }
1126
    jAad = (*env)->GetObjectField(env, jParam, fieldID);
1127
    fieldID = (*env)->GetFieldID(env, jCcmParamsClass, "macLen", "J");
1128
    if (fieldID == NULL) { return NULL; }
1129
    jMacLen = (*env)->GetLongField(env, jParam, fieldID);
1130

1131
    // allocate memory for CK_CCM_PARAMS pointer
1132
    ckParamPtr = calloc(1, sizeof(CK_CCM_PARAMS));
1133
    if (ckParamPtr == NULL) {
1134
        p11ThrowOutOfMemoryError(env, 0);
1135
        return NULL;
1136
    }
1137

1138
    // populate using java values
1139
    ckParamPtr->ulDataLen = jLongToCKULong(jDataLen);
1140
    jByteArrayToCKByteArray(env, jNonce, &(ckParamPtr->pNonce),
1141
            &(ckParamPtr->ulNonceLen));
1142
    if ((*env)->ExceptionCheck(env)) {
1143
        goto cleanup;
1144
    }
1145

1146
    jByteArrayToCKByteArray(env, jAad, &(ckParamPtr->pAAD),
1147
            &(ckParamPtr->ulAADLen));
1148
    if ((*env)->ExceptionCheck(env)) {
1149
        goto cleanup;
1150
    }
1151

1152
    ckParamPtr->ulMACLen = jLongToCKULong(jMacLen);
1153

1154
    if (pLength != NULL) {
1155
        *pLength = sizeof(CK_CCM_PARAMS);
1156
    }
1157
    return ckParamPtr;
1158
cleanup:
1159
    free(ckParamPtr->pNonce);
1160
    free(ckParamPtr->pAAD);
1161
    free(ckParamPtr);
1162
    return NULL;
1163
}
1164

1165
/*
1166
 * converts the Java CK_SALSA20_CHACHA20_POLY1305_PARAMS object to a
1167
 * CK_SALSA20_CHACHA20_POLY1305_PARAMS pointer
1168
 *
1169
 * @param env - used to call JNI functions to get the Java classes and objects
1170
 * @param jParam - the Java CK_SALSA20_CHACHA20_POLY1305_PARAMS object to
1171
 *         convert
1172
 * @param pLength - length of the allocated memory of the returned pointer
1173
 * @return pointer to the new CK_SALSA20_CHACHA20_POLY1305_PARAMS structure
1174
 */
1175
CK_SALSA20_CHACHA20_POLY1305_PARAMS_PTR
1176
jSalsaChaChaPolyParamsToCKSalsaChaChaPolyParamPtr(
1177
        JNIEnv *env, jobject jParam, CK_ULONG *pLength)
1178
{
1179
    CK_SALSA20_CHACHA20_POLY1305_PARAMS_PTR ckParamPtr;
1180
    jclass jParamsClass;
1181
    jfieldID fieldID;
1182
    jobject jNonce, jAad;
1183

1184
    if (pLength != NULL) {
1185
        *pLength = 0;
1186
    }
1187

1188
    // retrieve java values
1189
    jParamsClass = (*env)->FindClass(env,
1190
            CLASS_SALSA20_CHACHA20_POLY1305_PARAMS);
1191
    if (jParamsClass == NULL) { return NULL; }
1192
    if (!(*env)->IsInstanceOf(env, jParam, jParamsClass)) {
1193
        return NULL;
1194
    }
1195
    fieldID = (*env)->GetFieldID(env, jParamsClass, "nonce", "[B");
1196
    if (fieldID == NULL) { return NULL; }
1197
    jNonce = (*env)->GetObjectField(env, jParam, fieldID);
1198
    fieldID = (*env)->GetFieldID(env, jParamsClass, "aad", "[B");
1199
    if (fieldID == NULL) { return NULL; }
1200
    jAad = (*env)->GetObjectField(env, jParam, fieldID);
1201
    // allocate memory for CK_SALSA20_CHACHA20_POLY1305_PARAMS pointer
1202
    ckParamPtr = calloc(1, sizeof(CK_SALSA20_CHACHA20_POLY1305_PARAMS));
1203
    if (ckParamPtr == NULL) {
1204
        p11ThrowOutOfMemoryError(env, 0);
1205
        return NULL;
1206
    }
1207

1208
    // populate using java values
1209
    jByteArrayToCKByteArray(env, jNonce, &(ckParamPtr->pNonce),
1210
            &(ckParamPtr->ulNonceLen));
1211
    if ((*env)->ExceptionCheck(env)) {
1212
        goto cleanup;
1213
    }
1214

1215
    jByteArrayToCKByteArray(env, jAad, &(ckParamPtr->pAAD),
1216
            &(ckParamPtr->ulAADLen));
1217
    if ((*env)->ExceptionCheck(env)) {
1218
        goto cleanup;
1219
    }
1220

1221
    if (pLength != NULL) {
1222
        *pLength = sizeof(CK_SALSA20_CHACHA20_POLY1305_PARAMS);
1223
    }
1224
    return ckParamPtr;
1225
cleanup:
1226
    free(ckParamPtr->pNonce);
1227
    free(ckParamPtr->pAAD);
1228
    free(ckParamPtr);
1229
    return NULL;
1230
}
1231

1232
/*
1233
 * converts a Java CK_MECHANISM object into a CK_MECHANISM pointer
1234
 * pointer.
1235
 *
1236
 * @param env - used to call JNI funktions to get the values out of the Java object
1237
 * @param jMech - the Java CK_MECHANISM object to convert
1238
 * @return pointer to the new CK_MECHANISM structure
1239
 */
1240
CK_MECHANISM_PTR jMechanismToCKMechanismPtr(JNIEnv *env, jobject jMech)
1241
{
1242
    CK_MECHANISM_PTR ckpMech;
1243
    jlong jMechType = (*env)->GetLongField(env, jMech, mech_mechanismID);
1244
    jobject jParam = (*env)->GetObjectField(env, jMech, mech_pParameterID);
1245

1246
    /* allocate memory for CK_MECHANISM_PTR */
1247
    ckpMech =  (CK_MECHANISM_PTR) calloc(1, sizeof(CK_MECHANISM));
1248
    if (ckpMech == NULL) {
1249
        p11ThrowOutOfMemoryError(env, 0);
1250
        return NULL;
1251
    }
1252
    TRACE1("DEBUG jMechanismToCKMechanismPtr: allocated mech %p\n", ckpMech);
1253

1254
    ckpMech->mechanism = jLongToCKULong(jMechType);
1255

1256
    /* convert the specific Java mechanism parameter object to a pointer to a
1257
     *  CK-type mechanism structure
1258
     */
1259
    if (jParam == NULL) {
1260
        ckpMech->pParameter = NULL;
1261
        ckpMech->ulParameterLen = 0;
1262
    } else {
1263
        ckpMech->pParameter = jMechParamToCKMechParamPtr(env, jParam,
1264
            ckpMech->mechanism, &(ckpMech->ulParameterLen));
1265
    }
1266
    return ckpMech;
1267
}
1268

1269
/*
1270
 * converts the pValue of a CK_ATTRIBUTE structure into a Java Object by
1271
 * checking the type of the attribute. A PKCS#11 attribute value can
1272
 * be a CK_ULONG, CK_BYTE[], CK_CHAR[], big integer, CK_BBOOL, CK_UTF8CHAR[],
1273
 * CK_DATE or CK_FLAGS that gets converted to a corresponding Java object.
1274
 *
1275
 * @param env - used to call JNI functions to create the new Java object
1276
 * @param ckpAttribute - the pointer to the CK_ATTRIBUTE structure that contains the type
1277
 *                       and the pValue to convert
1278
 * @return the new Java object of the CK-type pValue
1279
 */
1280
jobject ckAttributeValueToJObject(JNIEnv *env, const CK_ATTRIBUTE_PTR ckpAttribute)
1281
{
1282
    jint jValueLength;
1283
    jobject jValueObject = NULL;
1284

1285
    jValueLength = ckULongToJInt(ckpAttribute->ulValueLen);
1286

1287
    if ((jValueLength <= 0) || (ckpAttribute->pValue == NULL)) {
1288
        return NULL ;
1289
    }
1290

1291
    switch(ckpAttribute->type) {
1292
        case CKA_CLASS:
1293
            /* value CK_OBJECT_CLASS, defacto a CK_ULONG */
1294
        case CKA_KEY_TYPE:
1295
            /* value CK_KEY_TYPE, defacto a CK_ULONG */
1296
        case CKA_CERTIFICATE_TYPE:
1297
            /* value CK_CERTIFICATE_TYPE, defacto a CK_ULONG */
1298
        case CKA_HW_FEATURE_TYPE:
1299
            /* value CK_HW_FEATURE_TYPE, defacto a CK_ULONG */
1300
        case CKA_MODULUS_BITS:
1301
        case CKA_VALUE_BITS:
1302
        case CKA_VALUE_LEN:
1303
        case CKA_KEY_GEN_MECHANISM:
1304
        case CKA_PRIME_BITS:
1305
        case CKA_SUB_PRIME_BITS:
1306
            /* value CK_ULONG */
1307
            jValueObject = ckULongPtrToJLongObject(env, (CK_ULONG*) ckpAttribute->pValue);
1308
            break;
1309

1310
            /* can be CK_BYTE[],CK_CHAR[] or big integer; defacto always CK_BYTE[] */
1311
        case CKA_VALUE:
1312
        case CKA_OBJECT_ID:
1313
        case CKA_SUBJECT:
1314
        case CKA_ID:
1315
        case CKA_ISSUER:
1316
        case CKA_SERIAL_NUMBER:
1317
        case CKA_OWNER:
1318
        case CKA_AC_ISSUER:
1319
        case CKA_ATTR_TYPES:
1320
        case CKA_ECDSA_PARAMS:
1321
            /* CKA_EC_PARAMS is the same, these two are equivalent */
1322
        case CKA_EC_POINT:
1323
        case CKA_PRIVATE_EXPONENT:
1324
        case CKA_PRIME_1:
1325
        case CKA_PRIME_2:
1326
        case CKA_EXPONENT_1:
1327
        case CKA_EXPONENT_2:
1328
        case CKA_COEFFICIENT:
1329
            /* value CK_BYTE[] */
1330
            jValueObject = ckByteArrayToJByteArray(env, (CK_BYTE*) ckpAttribute->pValue, jValueLength);
1331
            break;
1332

1333
        case CKA_RESET_ON_INIT:
1334
        case CKA_HAS_RESET:
1335
        case CKA_TOKEN:
1336
        case CKA_PRIVATE:
1337
        case CKA_MODIFIABLE:
1338
        case CKA_DERIVE:
1339
        case CKA_LOCAL:
1340
        case CKA_ENCRYPT:
1341
        case CKA_VERIFY:
1342
        case CKA_VERIFY_RECOVER:
1343
        case CKA_WRAP:
1344
        case CKA_SENSITIVE:
1345
        case CKA_SECONDARY_AUTH:
1346
        case CKA_DECRYPT:
1347
        case CKA_SIGN:
1348
        case CKA_SIGN_RECOVER:
1349
        case CKA_UNWRAP:
1350
        case CKA_EXTRACTABLE:
1351
        case CKA_ALWAYS_SENSITIVE:
1352
        case CKA_NEVER_EXTRACTABLE:
1353
        case CKA_TRUSTED:
1354
            /* value CK_BBOOL */
1355
            jValueObject = ckBBoolPtrToJBooleanObject(env, (CK_BBOOL*) ckpAttribute->pValue);
1356
            break;
1357

1358
        case CKA_LABEL:
1359
        case CKA_APPLICATION:
1360
            /* value RFC 2279 (UTF-8) string */
1361
            jValueObject = ckUTF8CharArrayToJCharArray(env, (CK_UTF8CHAR*) ckpAttribute->pValue, jValueLength);
1362
            break;
1363

1364
        case CKA_START_DATE:
1365
        case CKA_END_DATE:
1366
            /* value CK_DATE */
1367
            jValueObject = ckDatePtrToJDateObject(env, (CK_DATE*) ckpAttribute->pValue);
1368
            break;
1369

1370
        case CKA_MODULUS:
1371
        case CKA_PUBLIC_EXPONENT:
1372
        case CKA_PRIME:
1373
        case CKA_SUBPRIME:
1374
        case CKA_BASE:
1375
            /* value big integer, i.e. CK_BYTE[] */
1376
            jValueObject = ckByteArrayToJByteArray(env, (CK_BYTE*) ckpAttribute->pValue, jValueLength);
1377
            break;
1378

1379
        case CKA_AUTH_PIN_FLAGS:
1380
            jValueObject = ckULongPtrToJLongObject(env, (CK_ULONG*) ckpAttribute->pValue);
1381
            /* value FLAGS, defacto a CK_ULONG */
1382
            break;
1383

1384
        case CKA_VENDOR_DEFINED:
1385
            /* we make a CK_BYTE[] out of this */
1386
            jValueObject = ckByteArrayToJByteArray(env, (CK_BYTE*) ckpAttribute->pValue, jValueLength);
1387
            break;
1388

1389
        // Netscape trust attributes
1390
        case CKA_NETSCAPE_TRUST_SERVER_AUTH:
1391
        case CKA_NETSCAPE_TRUST_CLIENT_AUTH:
1392
        case CKA_NETSCAPE_TRUST_CODE_SIGNING:
1393
        case CKA_NETSCAPE_TRUST_EMAIL_PROTECTION:
1394
            /* value CK_ULONG */
1395
            jValueObject = ckULongPtrToJLongObject(env, (CK_ULONG*) ckpAttribute->pValue);
1396
            break;
1397

1398
        default:
1399
            /* we make a CK_BYTE[] out of this */
1400
            jValueObject = ckByteArrayToJByteArray(env, (CK_BYTE*) ckpAttribute->pValue, jValueLength);
1401
            break;
1402
    }
1403

1404
    return jValueObject ;
1405
}
1406

1407
/*
1408
 * the following functions convert a Java mechanism parameter object to a PKCS#11
1409
 * mechanism parameter structure
1410
 *
1411
 * CK_<Param>_PARAMS j<Param>ParamToCK<Param>Param(JNIEnv *env,
1412
 *                                                 jobject jParam);
1413
 *
1414
 * These functions get a Java object, that must be the right Java mechanism
1415
 * object and they return the new PKCS#11 mechanism parameter structure.
1416
 * Every field of the Java object is retrieved, gets converted to a corresponding
1417
 * PKCS#11 type and is set in the new PKCS#11 structure.
1418
 */
1419

1420
/*
1421
 * converts the given Java mechanism parameter to a CK mechanism parameter
1422
 * pointer and store the length in bytes in the length variable.
1423
 *
1424
 * @param env - used to call JNI funktions to get the Java classes and objects
1425
 * @param jParam - the Java mechanism parameter object to convert
1426
 * @param ckMech - the PKCS#11 mechanism type
1427
 * @param ckpLength - the reference of the length in bytes of the new CK mechanism parameter
1428
 *                    structure
1429
 * @return pointer to the new CK mechanism parameter structure
1430
 */
1431
CK_VOID_PTR jMechParamToCKMechParamPtr(JNIEnv *env, jobject jParam,
1432
        CK_MECHANISM_TYPE ckMech, CK_ULONG *ckpLength)
1433
{
1434
    CK_VOID_PTR ckpParamPtr;
1435
    if (jParam == NULL) {
1436
        ckpParamPtr = NULL;
1437
        *ckpLength = 0;
1438
    } else if ((*env)->IsInstanceOf(env, jParam, jByteArrayClass)) {
1439
        jByteArrayToCKByteArray(env, jParam, (CK_BYTE_PTR *) &ckpParamPtr, ckpLength);
1440
    } else if ((*env)->IsInstanceOf(env, jParam, jLongClass)) {
1441
        ckpParamPtr = jLongObjectToCKULongPtr(env, jParam);
1442
        *ckpLength = sizeof(CK_ULONG);
1443
    } else {
1444
        ckpParamPtr = jMechParamToCKMechParamPtrSlow(env, jParam, ckMech, ckpLength);
1445
    }
1446
    return ckpParamPtr;
1447
}
1448

1449
CK_VOID_PTR jMechParamToCKMechParamPtrSlow(JNIEnv *env, jobject jParam,
1450
        CK_MECHANISM_TYPE ckMech, CK_ULONG *ckpLength)
1451
{
1452
    CK_VOID_PTR ckpParamPtr = NULL;
1453

1454
    /*
1455
     * Most common cases, i.e. NULL/byte[]/long, are already handled by
1456
     * jMechParamToCKMechParam before calling this method.
1457
     */
1458
    TRACE1("\nDEBUG: jMechParamToCKMechParamPtrSlow, mech=0x%lX\n", ckMech);
1459

1460
    switch (ckMech) {
1461
        case CKM_SSL3_PRE_MASTER_KEY_GEN:
1462
        case CKM_TLS_PRE_MASTER_KEY_GEN:
1463
            ckpParamPtr = jVersionToCKVersionPtr(env, jParam);
1464
            if (ckpParamPtr != NULL) {
1465
                *ckpLength = sizeof(CK_VERSION);
1466
            } else {
1467
                *ckpLength = 0;
1468
            }
1469
            break;
1470
        case CKM_SSL3_MASTER_KEY_DERIVE:
1471
        case CKM_TLS_MASTER_KEY_DERIVE:
1472
        case CKM_SSL3_MASTER_KEY_DERIVE_DH:
1473
        case CKM_TLS_MASTER_KEY_DERIVE_DH:
1474
            ckpParamPtr = jSsl3MasterKeyDeriveParamToCKSsl3MasterKeyDeriveParamPtr(env, jParam,
1475
                    ckpLength);
1476
            break;
1477
        case CKM_SSL3_KEY_AND_MAC_DERIVE:
1478
        case CKM_TLS_KEY_AND_MAC_DERIVE:
1479
            ckpParamPtr = jSsl3KeyMatParamToCKSsl3KeyMatParamPtr(env, jParam,
1480
                    ckpLength);
1481
            break;
1482
        case CKM_TLS12_KEY_AND_MAC_DERIVE:
1483
            ckpParamPtr = jTls12KeyMatParamToCKTls12KeyMatParamPtr(env, jParam,
1484
                    ckpLength);
1485
            break;
1486
        case CKM_TLS12_MASTER_KEY_DERIVE:
1487
        case CKM_TLS12_MASTER_KEY_DERIVE_DH:
1488
            ckpParamPtr = jTls12MasterKeyDeriveParamToCKTls12MasterKeyDeriveParamPtr(env, jParam,
1489
                    ckpLength);
1490
            break;
1491
        case CKM_TLS_PRF:
1492
        case CKM_NSS_TLS_PRF_GENERAL:
1493
            ckpParamPtr = jTlsPrfParamsToCKTlsPrfParamPtr(env, jParam,
1494
                    ckpLength);
1495
            break;
1496
        case CKM_TLS_MAC:
1497
            ckpParamPtr = jTlsMacParamsToCKTlsMacParamPtr(env, jParam,
1498
                    ckpLength);
1499
            break;
1500
        case CKM_AES_CTR:
1501
            ckpParamPtr = jAesCtrParamsToCKAesCtrParamPtr(env, jParam,
1502
                    ckpLength);
1503
            break;
1504
        case CKM_AES_GCM:
1505
            ckpParamPtr = jGCMParamsToCKGCMParamPtr(env, jParam, ckpLength);
1506
            break;
1507
        case CKM_AES_CCM:
1508
            ckpParamPtr = jCCMParamsToCKCCMParamPtr(env, jParam, ckpLength);
1509
            break;
1510
       case CKM_CHACHA20_POLY1305:
1511
            ckpParamPtr =
1512
                    jSalsaChaChaPolyParamsToCKSalsaChaChaPolyParamPtr(env,
1513
                    jParam, ckpLength);
1514
            break;
1515
        case CKM_RSA_PKCS_OAEP:
1516
            ckpParamPtr = jRsaPkcsOaepParamToCKRsaPkcsOaepParamPtr(env, jParam, ckpLength);
1517
            break;
1518
        case CKM_PBA_SHA1_WITH_SHA1_HMAC:
1519
        case CKM_NSS_PKCS12_PBE_SHA224_HMAC_KEY_GEN:
1520
        case CKM_NSS_PKCS12_PBE_SHA256_HMAC_KEY_GEN:
1521
        case CKM_NSS_PKCS12_PBE_SHA384_HMAC_KEY_GEN:
1522
        case CKM_NSS_PKCS12_PBE_SHA512_HMAC_KEY_GEN:
1523
            ckpParamPtr = jPbeParamToCKPbeParamPtr(env, jParam, ckpLength);
1524
            break;
1525
        case CKM_PKCS5_PBKD2:
1526
            ckpParamPtr = jPkcs5Pbkd2ParamToCKPkcs5Pbkd2ParamPtr(env, jParam, ckpLength);
1527
            break;
1528
        case CKM_RSA_PKCS_PSS:
1529
        case CKM_SHA1_RSA_PKCS_PSS:
1530
        case CKM_SHA256_RSA_PKCS_PSS:
1531
        case CKM_SHA384_RSA_PKCS_PSS:
1532
        case CKM_SHA512_RSA_PKCS_PSS:
1533
        case CKM_SHA224_RSA_PKCS_PSS:
1534
            ckpParamPtr = jRsaPkcsPssParamToCKRsaPkcsPssParamPtr(env, jParam, ckpLength);
1535
            break;
1536
        case CKM_ECDH1_DERIVE:
1537
        case CKM_ECDH1_COFACTOR_DERIVE:
1538
            ckpParamPtr = jEcdh1DeriveParamToCKEcdh1DeriveParamPtr(env, jParam, ckpLength);
1539
            break;
1540
        case CKM_ECMQV_DERIVE:
1541
            ckpParamPtr = jEcdh2DeriveParamToCKEcdh2DeriveParamPtr(env, jParam, ckpLength);
1542
            break;
1543
        case CKM_X9_42_DH_DERIVE:
1544
            ckpParamPtr = jX942Dh1DeriveParamToCKX942Dh1DeriveParamPtr(env, jParam, ckpLength);
1545
            break;
1546
        case CKM_X9_42_DH_HYBRID_DERIVE:
1547
        case CKM_X9_42_MQV_DERIVE:
1548
            ckpParamPtr = jX942Dh2DeriveParamToCKX942Dh2DeriveParamPtr(env, jParam, ckpLength);
1549
            break;
1550
        // defined by pkcs11.h but we don't support
1551
        case CKM_KEA_DERIVE: // CK_KEA_DERIVE_PARAMS
1552
        case CKM_RC2_CBC: // CK_RC2_CBC_PARAMS
1553
        case CKM_RC2_MAC_GENERAL: // CK_RC2_MAC_GENERAL_PARAMS
1554
        case CKM_RC5_ECB: // CK_RC5_PARAMS
1555
        case CKM_RC5_MAC: // CK_RC5_PARAMS
1556
        case CKM_RC5_CBC: // CK_RC5_CBC_PARAMS
1557
        case CKM_RC5_MAC_GENERAL: // CK_RC5_MAC_GENERAL_PARAMS
1558
        case CKM_SKIPJACK_PRIVATE_WRAP: // CK_SKIPJACK_PRIVATE_WRAP_PARAMS
1559
        case CKM_SKIPJACK_RELAYX: // CK_SKIPJACK_RELAYX_PARAMS
1560
        case CKM_KEY_WRAP_SET_OAEP: // CK_KEY_WRAP_SET_OAEP_PARAMS
1561
            p11ThrowPKCS11RuntimeException(env, "No parameter support for this mechanism");
1562
            break;
1563
        default:
1564
            /* if everything failed up to here */
1565
            /* try if the parameter is a primitive Java type */
1566
            ckpParamPtr = jObjectToPrimitiveCKObjectPtr(env, jParam, ckpLength);
1567
            /* *ckpParamPtr = jObjectToCKVoidPtr(jParam); */
1568
            /* *ckpLength = 1; */
1569
    }
1570
    TRACE0("\nDEBUG: jMechParamToCKMechParamPtrSlow FINISHED\n");
1571

1572
    if ((*env)->ExceptionCheck(env)) {
1573
        return NULL;
1574
    }
1575

1576
    return ckpParamPtr;
1577
}
1578

1579
/*
1580
 * converts the Java CK_RSA_PKCS_OAEP_PARAMS object to a
1581
 * CK_RSA_PKCS_OAEP_PARAMS pointer
1582
 *
1583
 * @param env - used to call JNI funktions to get the Java classes and objects
1584
 * @param jParam - the Java CK_RSA_PKCS_OAEP_PARAMS object to convert
1585
 * @param pLength - length of the allocated memory of the returned pointer
1586
 * @return pointer to the new CK_RSA_PKCS_OAEP_PARAMS structure
1587
 */
1588
CK_RSA_PKCS_OAEP_PARAMS_PTR
1589
jRsaPkcsOaepParamToCKRsaPkcsOaepParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
1590
{
1591
    CK_RSA_PKCS_OAEP_PARAMS_PTR ckParamPtr;
1592
    jclass jRsaPkcsOaepParamsClass;
1593
    jfieldID fieldID;
1594
    jlong jHashAlg, jMgf, jSource;
1595
    jobject jSourceData;
1596

1597
    if (pLength!= NULL) {
1598
        *pLength = 0L;
1599
    }
1600

1601
    // retrieve java values
1602
    jRsaPkcsOaepParamsClass = (*env)->FindClass(env, CLASS_RSA_PKCS_OAEP_PARAMS);
1603
    if (jRsaPkcsOaepParamsClass == NULL) { return NULL; }
1604
    fieldID = (*env)->GetFieldID(env, jRsaPkcsOaepParamsClass, "hashAlg", "J");
1605
    if (fieldID == NULL) { return NULL; }
1606
    jHashAlg = (*env)->GetLongField(env, jParam, fieldID);
1607
    fieldID = (*env)->GetFieldID(env, jRsaPkcsOaepParamsClass, "mgf", "J");
1608
    if (fieldID == NULL) { return NULL; }
1609
    jMgf = (*env)->GetLongField(env, jParam, fieldID);
1610
    fieldID = (*env)->GetFieldID(env, jRsaPkcsOaepParamsClass, "source", "J");
1611
    if (fieldID == NULL) { return NULL; }
1612
    jSource = (*env)->GetLongField(env, jParam, fieldID);
1613
    fieldID = (*env)->GetFieldID(env, jRsaPkcsOaepParamsClass, "pSourceData", "[B");
1614
    if (fieldID == NULL) { return NULL; }
1615
    jSourceData = (*env)->GetObjectField(env, jParam, fieldID);
1616

1617
    // allocate memory for CK_RSA_PKCS_OAEP_PARAMS pointer
1618
    ckParamPtr = calloc(1, sizeof(CK_RSA_PKCS_OAEP_PARAMS));
1619
    if (ckParamPtr == NULL) {
1620
        p11ThrowOutOfMemoryError(env, 0);
1621
        return NULL;
1622
    }
1623

1624
    // populate using java values
1625
    ckParamPtr->hashAlg = jLongToCKULong(jHashAlg);
1626
    ckParamPtr->mgf = jLongToCKULong(jMgf);
1627
    ckParamPtr->source = jLongToCKULong(jSource);
1628
    jByteArrayToCKByteArray(env, jSourceData, (CK_BYTE_PTR*) &(ckParamPtr->pSourceData),
1629
            &(ckParamPtr->ulSourceDataLen));
1630
    if ((*env)->ExceptionCheck(env)) {
1631
        free(ckParamPtr);
1632
        return NULL;
1633
    }
1634

1635
    if (pLength!= NULL) {
1636
        *pLength = sizeof(CK_RSA_PKCS_OAEP_PARAMS);
1637
    }
1638
    return ckParamPtr;
1639
}
1640

1641
/*
1642
 * converts the Java CK_PBE_PARAMS object to a CK_PBE_PARAMS pointer
1643
 *
1644
 * @param env - used to call JNI funktions to get the Java classes and objects
1645
 * @param jParam - the Java CK_PBE_PARAMS object to convert
1646
 * @param pLength - length of the allocated memory of the returned pointer
1647
 * @return pointer to the new CK_PBE_PARAMS structure
1648
 */
1649
CK_PBE_PARAMS_PTR
1650
jPbeParamToCKPbeParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
1651
{
1652
    CK_PBE_PARAMS_PTR ckParamPtr;
1653
    jclass jPbeParamsClass;
1654
    jfieldID fieldID;
1655
    jlong jIteration;
1656
    jobject jInitVector, jPassword, jSalt;
1657
    CK_ULONG ckTemp;
1658

1659
    if (pLength != NULL) {
1660
        *pLength = 0;
1661
    }
1662

1663
    // retrieve java values
1664
    jPbeParamsClass = (*env)->FindClass(env, CLASS_PBE_PARAMS);
1665
    if (jPbeParamsClass == NULL) { return NULL; }
1666
    fieldID = (*env)->GetFieldID(env, jPbeParamsClass, "pInitVector", "[B");
1667
    if (fieldID == NULL) { return NULL; }
1668
    jInitVector = (*env)->GetObjectField(env, jParam, fieldID);
1669
    fieldID = (*env)->GetFieldID(env, jPbeParamsClass, "pPassword", "[C");
1670
    if (fieldID == NULL) { return NULL; }
1671
    jPassword = (*env)->GetObjectField(env, jParam, fieldID);
1672
    fieldID = (*env)->GetFieldID(env, jPbeParamsClass, "pSalt", "[B");
1673
    if (fieldID == NULL) { return NULL; }
1674
    jSalt = (*env)->GetObjectField(env, jParam, fieldID);
1675
    fieldID = (*env)->GetFieldID(env, jPbeParamsClass, "ulIteration", "J");
1676
    if (fieldID == NULL) { return NULL; }
1677
    jIteration = (*env)->GetLongField(env, jParam, fieldID);
1678

1679
    // allocate memory for CK_PBE_PARAMS pointer
1680
    ckParamPtr = calloc(1, sizeof(CK_PBE_PARAMS));
1681
    if (ckParamPtr == NULL) {
1682
        p11ThrowOutOfMemoryError(env, 0);
1683
        return NULL;
1684
    }
1685

1686
    // populate using java values
1687
    ckParamPtr->ulIteration = jLongToCKULong(jIteration);
1688
    jByteArrayToCKByteArray(env, jInitVector, &(ckParamPtr->pInitVector), &ckTemp);
1689
    if ((*env)->ExceptionCheck(env)) {
1690
        goto cleanup;
1691
    }
1692
    jCharArrayToCKUTF8CharArray(env, jPassword, &(ckParamPtr->pPassword), &(ckParamPtr->ulPasswordLen));
1693
    if ((*env)->ExceptionCheck(env)) {
1694
        goto cleanup;
1695
    }
1696
    jByteArrayToCKByteArray(env, jSalt, &(ckParamPtr->pSalt), &(ckParamPtr->ulSaltLen));
1697
    if ((*env)->ExceptionCheck(env)) {
1698
        goto cleanup;
1699
    }
1700

1701
    if (pLength != NULL) {
1702
        *pLength = sizeof(CK_PBE_PARAMS);
1703
    }
1704
    return ckParamPtr;
1705
cleanup:
1706
    free(ckParamPtr->pInitVector);
1707
    if (ckParamPtr->pPassword != NULL) {
1708
        memset(ckParamPtr->pPassword, 0, ckParamPtr->ulPasswordLen);
1709
    }
1710
    free(ckParamPtr->pPassword);
1711
    free(ckParamPtr->pSalt);
1712
    free(ckParamPtr);
1713
    return NULL;
1714
}
1715

1716
/*
1717
 * Copy back the initialization vector from the native structure to the
1718
 * Java object. This is only used for CKM_PBE_* mechanisms and their
1719
 * CK_PBE_PARAMS parameters.
1720
 *
1721
 */
1722
void copyBackPBEInitializationVector(JNIEnv *env, CK_MECHANISM *ckMechanism, jobject jMechanism)
1723
{
1724
    jclass jMechanismClass, jPbeParamsClass;
1725
    CK_PBE_PARAMS *ckParam;
1726
    jfieldID fieldID;
1727
    CK_MECHANISM_TYPE ckMechanismType;
1728
    jlong jMechanismType;
1729
    jobject jParameter;
1730
    jobject jInitVector;
1731
    jint jInitVectorLength;
1732
    CK_CHAR_PTR initVector;
1733
    int i;
1734
    jchar* jInitVectorChars;
1735

1736
    /* get mechanism */
1737
    jMechanismClass = (*env)->FindClass(env, CLASS_MECHANISM);
1738
    if (jMechanismClass == NULL) { return; }
1739
    fieldID = (*env)->GetFieldID(env, jMechanismClass, "mechanism", "J");
1740
    if (fieldID == NULL) { return; }
1741
    jMechanismType = (*env)->GetLongField(env, jMechanism, fieldID);
1742
    ckMechanismType = jLongToCKULong(jMechanismType);
1743
    if (ckMechanismType != ckMechanism->mechanism) {
1744
        /* we do not have matching types, this should not occur */
1745
        return;
1746
    }
1747

1748
    jPbeParamsClass = (*env)->FindClass(env, CLASS_PBE_PARAMS);
1749
    if (jPbeParamsClass == NULL) { return; }
1750
    ckParam = (CK_PBE_PARAMS *) ckMechanism->pParameter;
1751
    if (ckParam != NULL_PTR) {
1752
        initVector = ckParam->pInitVector;
1753
        if (initVector != NULL_PTR) {
1754
            /* get pParameter */
1755
            fieldID = (*env)->GetFieldID(env, jMechanismClass, "pParameter", "Ljava/lang/Object;");
1756
            if (fieldID == NULL) { return; }
1757
            jParameter = (*env)->GetObjectField(env, jMechanism, fieldID);
1758
            fieldID = (*env)->GetFieldID(env, jPbeParamsClass, "pInitVektor", "[C");
1759
            if (fieldID == NULL) { return; }
1760
            jInitVector = (*env)->GetObjectField(env, jParameter, fieldID);
1761

1762
            if (jInitVector != NULL) {
1763
                jInitVectorLength = (*env)->GetArrayLength(env, jInitVector);
1764
                jInitVectorChars = (*env)->GetCharArrayElements(env, jInitVector, NULL);
1765
                if (jInitVectorChars == NULL) { return; }
1766

1767
                /* copy the chars to the Java buffer */
1768
                for (i=0; i < jInitVectorLength; i++) {
1769
                    jInitVectorChars[i] = ckCharToJChar(initVector[i]);
1770
                }
1771
                /* copy back the Java buffer to the object */
1772
                (*env)->ReleaseCharArrayElements(env, jInitVector, jInitVectorChars, 0);
1773
            }
1774
        }
1775
    }
1776
}
1777

1778
#define PBKD2_PARAM_SET(member, value)                              \
1779
    do {                                                            \
1780
        if(ckParamPtr->version == PARAMS) {                         \
1781
            ckParamPtr->params.v1.member = value;                   \
1782
        } else {                                                    \
1783
            ckParamPtr->params.v2.member = value;                   \
1784
        }                                                           \
1785
    } while(0)
1786

1787
#define PBKD2_PARAM_ADDR(member)                                    \
1788
    (                                                               \
1789
        (ckParamPtr->version == PARAMS) ?                           \
1790
        (void*) &ckParamPtr->params.v1.member :                     \
1791
        (void*) &ckParamPtr->params.v2.member                       \
1792
    )
1793

1794
/*
1795
 * converts a Java CK_PKCS5_PBKD2_PARAMS object to a CK_PKCS5_PBKD2_PARAMS
1796
 * pointer, or a Java CK_PKCS5_PBKD2_PARAMS2 object to a CK_PKCS5_PBKD2_PARAMS2
1797
 * pointer
1798
 *
1799
 * @param env - used to call JNI functions to get the Java classes and objects
1800
 * @param jParam - the Java object to convert
1801
 * @param pLength - length of the allocated memory of the returned pointer
1802
 * @return pointer to the new structure
1803
 */
1804
CK_VOID_PTR
1805
jPkcs5Pbkd2ParamToCKPkcs5Pbkd2ParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
1806
{
1807
    VersionedPbkd2ParamsPtr ckParamPtr;
1808
    ParamVersion paramVersion;
1809
    CK_ULONG_PTR pUlPasswordLen;
1810
    jclass jPkcs5Pbkd2ParamsClass;
1811
    jfieldID fieldID;
1812
    jlong jSaltSource, jIteration, jPrf;
1813
    jobject jSaltSourceData, jPrfData, jPassword;
1814

1815
    if (pLength != NULL) {
1816
        *pLength = 0L;
1817
    }
1818

1819
    // retrieve java values
1820
    if ((jPkcs5Pbkd2ParamsClass =
1821
            (*env)->FindClass(env, CLASS_PKCS5_PBKD2_PARAMS)) != NULL
1822
            && (*env)->IsInstanceOf(env, jParam, jPkcs5Pbkd2ParamsClass)) {
1823
        paramVersion = PARAMS;
1824
    } else if ((jPkcs5Pbkd2ParamsClass =
1825
            (*env)->FindClass(env, CLASS_PKCS5_PBKD2_PARAMS2)) != NULL
1826
            && (*env)->IsInstanceOf(env, jParam, jPkcs5Pbkd2ParamsClass)) {
1827
        paramVersion = PARAMS2;
1828
    } else {
1829
        p11ThrowPKCS11RuntimeException(env, "Unknown PBKD2 mechanism parameters class.");
1830
        return NULL;
1831
    }
1832
    fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "saltSource", "J");
1833
    if (fieldID == NULL) { return NULL; }
1834
    jSaltSource = (*env)->GetLongField(env, jParam, fieldID);
1835
    fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "pSaltSourceData", "[B");
1836
    if (fieldID == NULL) { return NULL; }
1837
    jSaltSourceData = (*env)->GetObjectField(env, jParam, fieldID);
1838
    fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "iterations", "J");
1839
    if (fieldID == NULL) { return NULL; }
1840
    jIteration = (*env)->GetLongField(env, jParam, fieldID);
1841
    fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "prf", "J");
1842
    if (fieldID == NULL) { return NULL; }
1843
    jPrf = (*env)->GetLongField(env, jParam, fieldID);
1844
    fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "pPrfData", "[B");
1845
    if (fieldID == NULL) { return NULL; }
1846
    jPrfData = (*env)->GetObjectField(env, jParam, fieldID);
1847
    fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "pPassword", "[C");
1848
    if (fieldID == NULL) { return NULL; }
1849
    jPassword = (*env)->GetObjectField(env, jParam, fieldID);
1850

1851
    // allocate memory for VersionedPbkd2Params and store the structure version
1852
    ckParamPtr = calloc(1, sizeof(VersionedPbkd2Params));
1853
    if (ckParamPtr == NULL) {
1854
        p11ThrowOutOfMemoryError(env, 0);
1855
        return NULL;
1856
    }
1857
    ckParamPtr->version = paramVersion;
1858

1859
    // populate using java values
1860
    PBKD2_PARAM_SET(saltSource, jLongToCKULong(jSaltSource));
1861
    jByteArrayToCKByteArray(env, jSaltSourceData,
1862
            (CK_BYTE_PTR *) PBKD2_PARAM_ADDR(pSaltSourceData),
1863
            PBKD2_PARAM_ADDR(ulSaltSourceDataLen));
1864
    if ((*env)->ExceptionCheck(env)) {
1865
        goto cleanup;
1866
    }
1867
    PBKD2_PARAM_SET(iterations, jLongToCKULong(jIteration));
1868
    PBKD2_PARAM_SET(prf, jLongToCKULong(jPrf));
1869
    jByteArrayToCKByteArray(env, jPrfData,
1870
            (CK_BYTE_PTR *) PBKD2_PARAM_ADDR(pPrfData),
1871
            PBKD2_PARAM_ADDR(ulPrfDataLen));
1872
    if ((*env)->ExceptionCheck(env)) {
1873
        goto cleanup;
1874
    }
1875
    if (ckParamPtr->version == PARAMS) {
1876
        pUlPasswordLen = calloc(1, sizeof(CK_ULONG));
1877
        if (pUlPasswordLen == NULL) {
1878
            p11ThrowOutOfMemoryError(env, 0);
1879
            goto cleanup;
1880
        }
1881
        ckParamPtr->params.v1.ulPasswordLen = pUlPasswordLen;
1882
    } else {
1883
        pUlPasswordLen = &ckParamPtr->params.v2.ulPasswordLen;
1884
    }
1885
    jCharArrayToCKUTF8CharArray(env, jPassword,
1886
            (CK_CHAR_PTR *) PBKD2_PARAM_ADDR(pPassword),
1887
            pUlPasswordLen);
1888
    if ((*env)->ExceptionCheck(env)) {
1889
        goto cleanup;
1890
    }
1891

1892
    if (pLength != NULL) {
1893
        *pLength = (ckParamPtr->version == PARAMS ?
1894
            sizeof(ckParamPtr->params.v1) :
1895
            sizeof(ckParamPtr->params.v2));
1896
    }
1897
    // VersionedPbkd2ParamsPtr is equivalent to CK_PKCS5_PBKD2_PARAMS[2]_PTR
1898
    return ckParamPtr;
1899
cleanup:
1900
    FREE_VERSIONED_PBKD2_MEMBERS(ckParamPtr);
1901
    free(ckParamPtr);
1902
    return NULL;
1903

1904
}
1905

1906
/*
1907
 * converts the Java CK_RSA_PKCS_PSS_PARAMS object to a CK_RSA_PKCS_PSS_PARAMS
1908
 * pointer
1909
 *
1910
 * @param env - used to call JNI funktions to get the Java classes and objects
1911
 * @param jParam - the Java CK_RSA_PKCS_PSS_PARAMS object to convert
1912
 * @param pLength - length of the allocated memory of the returned pointer
1913
 * @return pointer to the new CK_RSA_PKCS_PSS_PARAMS structure
1914
 */
1915
CK_RSA_PKCS_PSS_PARAMS_PTR
1916
jRsaPkcsPssParamToCKRsaPkcsPssParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
1917
{
1918
    CK_RSA_PKCS_PSS_PARAMS_PTR ckParamPtr;
1919
    jclass jRsaPkcsPssParamsClass;
1920
    jfieldID fieldID;
1921
    jlong jHashAlg, jMgf, jSLen;
1922

1923
    if (pLength != NULL) {
1924
        *pLength = 0;
1925
    }
1926

1927
    // retrieve java values
1928
    jRsaPkcsPssParamsClass = (*env)->FindClass(env, CLASS_RSA_PKCS_PSS_PARAMS);
1929
    if (jRsaPkcsPssParamsClass == NULL) { return NULL; }
1930
    fieldID = (*env)->GetFieldID(env, jRsaPkcsPssParamsClass, "hashAlg", "J");
1931
    if (fieldID == NULL) { return NULL; }
1932
    jHashAlg = (*env)->GetLongField(env, jParam, fieldID);
1933
    fieldID = (*env)->GetFieldID(env, jRsaPkcsPssParamsClass, "mgf", "J");
1934
    if (fieldID == NULL) { return NULL; }
1935
    jMgf = (*env)->GetLongField(env, jParam, fieldID);
1936
    fieldID = (*env)->GetFieldID(env, jRsaPkcsPssParamsClass, "sLen", "J");
1937
    if (fieldID == NULL) { return NULL; }
1938
    jSLen = (*env)->GetLongField(env, jParam, fieldID);
1939

1940
    // allocate memory for CK_RSA_PKCS_PSS_PARAMS pointer
1941
    ckParamPtr = calloc(1, sizeof(CK_RSA_PKCS_PSS_PARAMS));
1942
    if (ckParamPtr == NULL) {
1943
        p11ThrowOutOfMemoryError(env, 0);
1944
        return NULL;
1945
    }
1946

1947
    // populate using java values
1948
    ckParamPtr->hashAlg = jLongToCKULong(jHashAlg);
1949
    ckParamPtr->mgf = jLongToCKULong(jMgf);
1950
    ckParamPtr->sLen = jLongToCKULong(jSLen);
1951
    TRACE1("DEBUG: jRsaPkcsPssParamToCKRsaPkcsPssParam, hashAlg=0x%lX\n", ckParamPtr->hashAlg);
1952
    TRACE1("DEBUG: jRsaPkcsPssParamToCKRsaPkcsPssParam, mgf=0x%lX\n", ckParamPtr->mgf);
1953
    TRACE1("DEBUG: jRsaPkcsPssParamToCKRsaPkcsPssParam, sLen=%lu\n", ckParamPtr->sLen);
1954

1955
    if (pLength != NULL) {
1956
        *pLength = sizeof(CK_RSA_PKCS_PSS_PARAMS);
1957
    }
1958
    return ckParamPtr;
1959

1960
}
1961

1962
/*
1963
 * converts the Java CK_ECDH1_DERIVE_PARAMS object to a CK_ECDH1_DERIVE_PARAMS
1964
 * pointer
1965
 *
1966
 * @param env - used to call JNI funktions to get the Java classes and objects
1967
 * @param jParam - the Java CK_ECDH1_DERIVE_PARAMS object to convert
1968
 * @param pLength - length of the allocated memory of the returned pointer
1969
 * @return pointer to nthe new CK_ECDH1_DERIVE_PARAMS structure
1970
 */
1971
CK_ECDH1_DERIVE_PARAMS_PTR
1972
jEcdh1DeriveParamToCKEcdh1DeriveParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
1973
{
1974
    CK_ECDH1_DERIVE_PARAMS_PTR ckParamPtr;
1975
    jclass jEcdh1DeriveParamsClass;
1976
    jfieldID fieldID;
1977
    jlong jLong;
1978
    jobject jSharedData, jPublicData;
1979

1980
    if (pLength != NULL) {
1981
        *pLength = 0;
1982
    }
1983

1984
    // retrieve java values
1985
    jEcdh1DeriveParamsClass = (*env)->FindClass(env, CLASS_ECDH1_DERIVE_PARAMS);
1986
    if (jEcdh1DeriveParamsClass == NULL) { return NULL; }
1987
    fieldID = (*env)->GetFieldID(env, jEcdh1DeriveParamsClass, "kdf", "J");
1988
    if (fieldID == NULL) { return NULL; }
1989
    jLong = (*env)->GetLongField(env, jParam, fieldID);
1990
    fieldID = (*env)->GetFieldID(env, jEcdh1DeriveParamsClass, "pSharedData", "[B");
1991
    if (fieldID == NULL) { return NULL; }
1992
    jSharedData = (*env)->GetObjectField(env, jParam, fieldID);
1993
    fieldID = (*env)->GetFieldID(env, jEcdh1DeriveParamsClass, "pPublicData", "[B");
1994
    if (fieldID == NULL) { return NULL; }
1995
    jPublicData = (*env)->GetObjectField(env, jParam, fieldID);
1996

1997
    // allocate memory for CK_ECDH1_DERIVE_PARAMS pointer
1998
    ckParamPtr = calloc(1, sizeof(CK_ECDH1_DERIVE_PARAMS));
1999
    if (ckParamPtr == NULL) {
2000
        p11ThrowOutOfMemoryError(env, 0);
2001
        return NULL;
2002
    }
2003

2004
    // populate using java values
2005
    ckParamPtr->kdf = jLongToCKULong(jLong);
2006
    jByteArrayToCKByteArray(env, jSharedData, &(ckParamPtr->pSharedData),
2007
            &(ckParamPtr->ulSharedDataLen));
2008
    if ((*env)->ExceptionCheck(env)) {
2009
        goto cleanup;
2010
    }
2011
    jByteArrayToCKByteArray(env, jPublicData, &(ckParamPtr->pPublicData),
2012
            &(ckParamPtr->ulPublicDataLen));
2013
    if ((*env)->ExceptionCheck(env)) {
2014
        goto cleanup;
2015
    }
2016

2017
    if (pLength != NULL) {
2018
        *pLength = sizeof(CK_ECDH1_DERIVE_PARAMS);
2019
    }
2020
    return ckParamPtr;
2021
cleanup:
2022
    free(ckParamPtr->pSharedData);
2023
    free(ckParamPtr->pPublicData);
2024
    free(ckParamPtr);
2025
    return NULL;
2026
}
2027

2028
/*
2029
 * converts the Java CK_ECDH2_DERIVE_PARAMS object to a CK_ECDH2_DERIVE_PARAMS
2030
 * pointer
2031
 *
2032
 * @param env - used to call JNI funktions to get the Java classes and objects
2033
 * @param jParam - the Java CK_ECDH2_DERIVE_PARAMS object to convert
2034
 * @param pLength - length of the allocated memory of the returned pointer
2035
 * @return pointer to the new CK_ECDH2_DERIVE_PARAMS structure
2036
 */
2037
CK_ECDH2_DERIVE_PARAMS_PTR
2038
jEcdh2DeriveParamToCKEcdh2DeriveParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
2039
{
2040
    CK_ECDH2_DERIVE_PARAMS_PTR ckParamPtr;
2041
    jclass jEcdh2DeriveParamsClass;
2042
    jfieldID fieldID;
2043
    jlong jKdf, jPrivateDataLen, jPrivateData;
2044
    jobject jSharedData, jPublicData, jPublicData2;
2045

2046
    // retrieve java values
2047
    jEcdh2DeriveParamsClass = (*env)->FindClass(env, CLASS_ECDH2_DERIVE_PARAMS);
2048
    if (jEcdh2DeriveParamsClass == NULL) { return NULL; }
2049
    fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "kdf", "J");
2050
    if (fieldID == NULL) { return NULL; }
2051
    jKdf = (*env)->GetLongField(env, jParam, fieldID);
2052
    fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "pSharedData", "[B");
2053
    if (fieldID == NULL) { return NULL; }
2054
    jSharedData = (*env)->GetObjectField(env, jParam, fieldID);
2055
    fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "pPublicData", "[B");
2056
    if (fieldID == NULL) { return NULL; }
2057
    jPublicData = (*env)->GetObjectField(env, jParam, fieldID);
2058
    fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "ulPrivateDataLen", "J");
2059
    if (fieldID == NULL) { return NULL; }
2060
    jPrivateDataLen = (*env)->GetLongField(env, jParam, fieldID);
2061
    fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "hPrivateData", "J");
2062
    if (fieldID == NULL) { return NULL; }
2063
    jPrivateData = (*env)->GetLongField(env, jParam, fieldID);
2064
    fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "pPublicData2", "[B");
2065
    if (fieldID == NULL) { return NULL; }
2066
    jPublicData2 = (*env)->GetObjectField(env, jParam, fieldID);
2067

2068
    // allocate memory for CK_ECDH2_DERIVE_PARAMS pointer
2069
    ckParamPtr = calloc(1, sizeof(CK_ECDH2_DERIVE_PARAMS));
2070
    if (ckParamPtr == NULL) {
2071
        p11ThrowOutOfMemoryError(env, 0);
2072
        return NULL;
2073
    }
2074

2075
    // populate using java values
2076
    ckParamPtr->kdf = jLongToCKULong(jKdf);
2077
    jByteArrayToCKByteArray(env, jSharedData, &(ckParamPtr->pSharedData),
2078
            &(ckParamPtr->ulSharedDataLen));
2079
    if ((*env)->ExceptionCheck(env)) {
2080
        goto cleanup;
2081
    }
2082
    jByteArrayToCKByteArray(env, jPublicData, &(ckParamPtr->pPublicData),
2083
            &(ckParamPtr->ulPublicDataLen));
2084
    if ((*env)->ExceptionCheck(env)) {
2085
        goto cleanup;
2086
    }
2087
    ckParamPtr->ulPrivateDataLen = jLongToCKULong(jPrivateDataLen);
2088
    ckParamPtr->hPrivateData = jLongToCKULong(jPrivateData);
2089
    jByteArrayToCKByteArray(env, jPublicData2, &(ckParamPtr->pPublicData2),
2090
            &(ckParamPtr->ulPublicDataLen2));
2091
    if ((*env)->ExceptionCheck(env)) {
2092
        goto cleanup;
2093
    }
2094

2095
    if (pLength != NULL) {
2096
        *pLength = sizeof(CK_ECDH2_DERIVE_PARAMS);
2097
    }
2098
    return ckParamPtr;
2099
cleanup:
2100
    free(ckParamPtr->pSharedData);
2101
    free(ckParamPtr->pPublicData);
2102
    free(ckParamPtr->pPublicData2);
2103
    free(ckParamPtr);
2104
    return NULL;
2105
}
2106

2107
/*
2108
 * converts the Java CK_X9_42_DH1_DERIVE_PARAMS object to a
2109
 * CK_X9_42_DH1_DERIVE_PARAMS pointer
2110
 *
2111
 * @param env - used to call JNI funktions to get the Java classes and objects
2112
 * @param jParam - the Java CK_X9_42_DH1_DERIVE_PARAMS object to convert
2113
 * @param pLength - length of the allocated memory of the returned pointer
2114
 * @return pointer to the new CK_X9_42_DH1_DERIVE_PARAMS structure
2115
 */
2116
CK_X9_42_DH1_DERIVE_PARAMS_PTR
2117
jX942Dh1DeriveParamToCKX942Dh1DeriveParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
2118
{
2119
    CK_X9_42_DH1_DERIVE_PARAMS_PTR ckParamPtr;
2120
    jclass jX942Dh1DeriveParamsClass;
2121
    jfieldID fieldID;
2122
    jlong jKdf;
2123
    jobject jOtherInfo, jPublicData;
2124

2125
    if (pLength != NULL) {
2126
        *pLength = 0;
2127
    }
2128

2129
    // retrieve java values
2130
    jX942Dh1DeriveParamsClass = (*env)->FindClass(env, CLASS_X9_42_DH1_DERIVE_PARAMS);
2131
    if (jX942Dh1DeriveParamsClass == NULL) { return NULL; }
2132
    fieldID = (*env)->GetFieldID(env, jX942Dh1DeriveParamsClass, "kdf", "J");
2133
    if (fieldID == NULL) { return NULL; }
2134
    jKdf = (*env)->GetLongField(env, jParam, fieldID);
2135
    fieldID = (*env)->GetFieldID(env, jX942Dh1DeriveParamsClass, "pOtherInfo", "[B");
2136
    if (fieldID == NULL) { return NULL; }
2137
    jOtherInfo = (*env)->GetObjectField(env, jParam, fieldID);
2138
    fieldID = (*env)->GetFieldID(env, jX942Dh1DeriveParamsClass, "pPublicData", "[B");
2139
    if (fieldID == NULL) { return NULL; }
2140
    jPublicData = (*env)->GetObjectField(env, jParam, fieldID);
2141

2142
    // allocate memory for CK_X9_42_DH1_DERIVE_PARAMS pointer
2143
    ckParamPtr = calloc(1, sizeof(CK_X9_42_DH1_DERIVE_PARAMS));
2144
    if (ckParamPtr == NULL) {
2145
        p11ThrowOutOfMemoryError(env, 0);
2146
        return NULL;
2147
    }
2148

2149
    // populate using java values
2150
    ckParamPtr->kdf = jLongToCKULong(jKdf);
2151
    jByteArrayToCKByteArray(env, jOtherInfo, &(ckParamPtr->pOtherInfo),
2152
            &(ckParamPtr->ulOtherInfoLen));
2153
    if ((*env)->ExceptionCheck(env)) {
2154
        goto cleanup;
2155
    }
2156
    jByteArrayToCKByteArray(env, jPublicData, &(ckParamPtr->pPublicData),
2157
            &(ckParamPtr->ulPublicDataLen));
2158
    if ((*env)->ExceptionCheck(env)) {
2159
        goto cleanup;
2160
    }
2161

2162
    if (pLength != NULL) {
2163
        *pLength = sizeof(CK_X9_42_DH1_DERIVE_PARAMS);
2164
    }
2165
    return ckParamPtr;
2166
cleanup:
2167
    free(ckParamPtr->pOtherInfo);
2168
    free(ckParamPtr->pPublicData);
2169
    free(ckParamPtr);
2170
    return NULL;
2171
}
2172

2173
/*
2174
 * converts the Java CK_X9_42_DH2_DERIVE_PARAMS object to a
2175
 * CK_X9_42_DH2_DERIVE_PARAMS pointer
2176
 *
2177
 * @param env - used to call JNI funktions to get the Java classes and objects
2178
 * @param jParam - the Java CK_X9_42_DH2_DERIVE_PARAMS object to convert
2179
 * @param pLength - length of the allocated memory of the returned pointer
2180
 * @return pointer to the new CK_X9_42_DH2_DERIVE_PARAMS structure
2181
 */
2182
CK_X9_42_DH2_DERIVE_PARAMS_PTR
2183
jX942Dh2DeriveParamToCKX942Dh2DeriveParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
2184
{
2185
    CK_X9_42_DH2_DERIVE_PARAMS_PTR ckParamPtr;
2186
    jclass jX942Dh2DeriveParamsClass;
2187
    jfieldID fieldID;
2188
    jlong jKdf, jPrivateDataLen, jPrivateData;
2189
    jobject jOtherInfo, jPublicData, jPublicData2;
2190

2191
    if (pLength != NULL) {
2192
        *pLength = 0L;
2193
    }
2194

2195
    // retrieve java values
2196
    jX942Dh2DeriveParamsClass = (*env)->FindClass(env, CLASS_X9_42_DH2_DERIVE_PARAMS);
2197
    if (jX942Dh2DeriveParamsClass == NULL) { return NULL; }
2198
    fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "kdf", "J");
2199
    if (fieldID == NULL) { return NULL; }
2200
    jKdf = (*env)->GetLongField(env, jParam, fieldID);
2201
    fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "pOtherInfo", "[B");
2202
    if (fieldID == NULL) { return NULL; }
2203
    jOtherInfo = (*env)->GetObjectField(env, jParam, fieldID);
2204
    fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "pPublicData", "[B");
2205
    if (fieldID == NULL) { return NULL; }
2206
    jPublicData = (*env)->GetObjectField(env, jParam, fieldID);
2207
    fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "ulPrivateDataLen", "J");
2208
    if (fieldID == NULL) { return NULL; }
2209
    jPrivateDataLen = (*env)->GetLongField(env, jParam, fieldID);
2210
    fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "hPrivateData", "J");
2211
    if (fieldID == NULL) { return NULL; }
2212
    jPrivateData = (*env)->GetLongField(env, jParam, fieldID);
2213
    fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "pPublicData2", "[B");
2214
    if (fieldID == NULL) { return NULL; }
2215
    jPublicData2 = (*env)->GetObjectField(env, jParam, fieldID);
2216

2217
    // allocate memory for CK_DATE pointer
2218
    ckParamPtr = calloc(1, sizeof(CK_X9_42_DH2_DERIVE_PARAMS));
2219
    if (ckParamPtr == NULL) {
2220
        p11ThrowOutOfMemoryError(env, 0);
2221
        return NULL;
2222
    }
2223

2224
    // populate using java values
2225
    ckParamPtr->kdf = jLongToCKULong(jKdf);
2226
    jByteArrayToCKByteArray(env, jOtherInfo, &(ckParamPtr->pOtherInfo),
2227
            &(ckParamPtr->ulOtherInfoLen));
2228
    if ((*env)->ExceptionCheck(env)) {
2229
        goto cleanup;
2230
    }
2231
    jByteArrayToCKByteArray(env, jPublicData, &(ckParamPtr->pPublicData),
2232
            &(ckParamPtr->ulPublicDataLen));
2233
    if ((*env)->ExceptionCheck(env)) {
2234
        goto cleanup;
2235
    }
2236
    ckParamPtr->ulPrivateDataLen = jLongToCKULong(jPrivateDataLen);
2237
    ckParamPtr->hPrivateData = jLongToCKULong(jPrivateData);
2238
    jByteArrayToCKByteArray(env, jPublicData2, &(ckParamPtr->pPublicData2),
2239
            &(ckParamPtr->ulPublicDataLen2));
2240
    if ((*env)->ExceptionCheck(env)) {
2241
        goto cleanup;
2242
    }
2243

2244
    if (pLength != NULL) {
2245
        *pLength = sizeof(CK_X9_42_DH2_DERIVE_PARAMS);
2246
    }
2247
    return ckParamPtr;
2248
cleanup:
2249
    free(ckParamPtr->pOtherInfo);
2250
    free(ckParamPtr->pPublicData);
2251
    free(ckParamPtr->pPublicData2);
2252
    free(ckParamPtr);
2253
    return NULL;
2254
}
2255

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.