1/*
2 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
3 */
4
5/* Copyright (c) 2002 Graz University of Technology. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * 3. The end-user documentation included with the redistribution, if any, must
18 * include the following acknowledgment:
19 *
20 * "This product includes software developed by IAIK of Graz University of
21 * Technology."
22 *
23 * Alternately, this acknowledgment may appear in the software itself, if
24 * and wherever such third-party acknowledgments normally appear.
25 *
26 * 4. The names "Graz University of Technology" and "IAIK of Graz University of
27 * Technology" must not be used to endorse or promote products derived from
28 * this software without prior written permission.
29 *
30 * 5. Products derived from this software may not be called
31 * "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior
32 * written permission of Graz University of Technology.
33 *
34 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
35 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
36 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE
38 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
41 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
42 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45 * POSSIBILITY OF SUCH DAMAGE.
46 */
47
48/*
49 * pkcs11wrapper.c
50 * 18.05.2001
51 *
52 * This is the implementation of the native functions of the Java to PKCS#11 interface.
53 * All function use some helper functions to convert the JNI types to PKCS#11 types.
54 *
55 * @author Karl Scheibelhofer <Karl.Scheibelhofer@iaik.at>
56 * @author Martin Schlaeffer <schlaeff@sbox.tugraz.at>
57 */
58
59
60#include "pkcs11wrapper.h"
61
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
65#include <assert.h>
66
67#include "sun_security_pkcs11_wrapper_PKCS11.h"
68
69/* declare file private functions */
70
71CK_VOID_PTR jMechParamToCKMechParamPtrSlow(JNIEnv *env, jobject jParam,
72 CK_MECHANISM_TYPE ckMech, CK_ULONG *ckpLength);
73
74
75/*
76 * converts a pointer to a CK_DATE structure into a Java CK_DATE Object.
77 *
78 * @param env - used to call JNI funktions to create the new Java object
79 * @param ckpValue - the pointer to the CK_DATE structure
80 * @return - the new Java CK_DATE object
81 */
82jobject ckDatePtrToJDateObject(JNIEnv *env, const CK_DATE *ckpDate)
83{
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 pointer to a CK_VERSION structure into a Java CK_VERSION Object.
123 *
124 * @param env - used to call JNI funktions to create the new Java object
125 * @param ckpVersion - the pointer to the CK_VERSION structure
126 * @return - the new Java CK_VERSION object
127 */
128jobject ckVersionPtrToJVersion(JNIEnv *env, const CK_VERSION_PTR ckpVersion)
129{
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 pointer to a CK_SESSION_INFO structure into a Java CK_SESSION_INFO Object.
161 *
162 * @param env - used to call JNI funktions to create the new Java object
163 * @param ckpSessionInfo - the pointer to the CK_SESSION_INFO structure
164 * @return - the new Java CK_SESSION_INFO object
165 */
166jobject ckSessionInfoPtrToJSessionInfo(JNIEnv *env, const CK_SESSION_INFO_PTR ckpSessionInfo)
167{
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 pointer to a CK_ATTRIBUTE structure into a Java CK_ATTRIBUTE Object.
204 *
205 * @param env - used to call JNI funktions to create the new Java object
206 * @param ckpAttribute - the pointer to the CK_ATTRIBUTE structure
207 * @return - the new Java CK_ATTRIBUTE object
208 */
209jobject ckAttributePtrToJAttribute(JNIEnv *env, const CK_ATTRIBUTE_PTR ckpAttribute)
210{
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 pointer to a CK_VERSION structure
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 - the pointer to the new CK_VERSION structure
248 */
249CK_VERSION_PTR jVersionToCKVersionPtr(JNIEnv *env, jobject jVersion)
250{
251 CK_VERSION_PTR ckpVersion;
252 jclass jVersionClass;
253 jfieldID jFieldID;
254 jbyte jMajor, jMinor;
255
256 if (jVersion == NULL) {
257 return NULL;
258 }
259
260 /* get CK_VERSION class */
261 jVersionClass = (*env)->GetObjectClass(env, jVersion);
262 if (jVersionClass == NULL) { return NULL; }
263
264 /* get Major */
265 jFieldID = (*env)->GetFieldID(env, jVersionClass, "major", "B");
266 if (jFieldID == NULL) { return NULL; }
267 jMajor = (*env)->GetByteField(env, jVersion, jFieldID);
268
269 /* get Minor */
270 jFieldID = (*env)->GetFieldID(env, jVersionClass, "minor", "B");
271 if (jFieldID == NULL) { return NULL; }
272 jMinor = (*env)->GetByteField(env, jVersion, jFieldID);
273
274 /* allocate memory for CK_VERSION pointer */
275 ckpVersion = (CK_VERSION_PTR) malloc(sizeof(CK_VERSION));
276 if (ckpVersion == NULL) {
277 throwOutOfMemoryError(env, 0);
278 return NULL;
279 }
280 ckpVersion->major = jByteToCKByte(jMajor);
281 ckpVersion->minor = jByteToCKByte(jMinor);
282
283 return ckpVersion ;
284}
285
286
287/*
288 * converts a Java CK_DATE object into a pointer to a CK_DATE structure
289 *
290 * @param env - used to call JNI funktions to get the values out of the Java object
291 * @param jVersion - the Java CK_DATE object to convert
292 * @return - the pointer to the new CK_DATE structure
293 */
294CK_DATE * jDateObjectPtrToCKDatePtr(JNIEnv *env, jobject jDate)
295{
296 CK_DATE * ckpDate;
297 CK_ULONG ckLength;
298 jclass jDateClass;
299 jfieldID jFieldID;
300 jobject jYear, jMonth, jDay;
301 jchar *jTempChars;
302 CK_ULONG i;
303
304 if (jDate == NULL) {
305 return NULL;
306 }
307
308 /* get CK_DATE class */
309 jDateClass = (*env)->FindClass(env, CLASS_DATE);
310 if (jDateClass == NULL) { return NULL; }
311
312 /* get Year */
313 jFieldID = (*env)->GetFieldID(env, jDateClass, "year", "[C");
314 if (jFieldID == NULL) { return NULL; }
315 jYear = (*env)->GetObjectField(env, jDate, jFieldID);
316
317 /* get Month */
318 jFieldID = (*env)->GetFieldID(env, jDateClass, "month", "[C");
319 if (jFieldID == NULL) { return NULL; }
320 jMonth = (*env)->GetObjectField(env, jDate, jFieldID);
321
322 /* get Day */
323 jFieldID = (*env)->GetFieldID(env, jDateClass, "day", "[C");
324 if (jFieldID == NULL) { return NULL; }
325 jDay = (*env)->GetObjectField(env, jDate, jFieldID);
326
327 /* allocate memory for CK_DATE pointer */
328 ckpDate = (CK_DATE *) malloc(sizeof(CK_DATE));
329 if (ckpDate == NULL) {
330 throwOutOfMemoryError(env, 0);
331 return NULL;
332 }
333
334 if (jYear == NULL) {
335 ckpDate->year[0] = 0;
336 ckpDate->year[1] = 0;
337 ckpDate->year[2] = 0;
338 ckpDate->year[3] = 0;
339 } else {
340 ckLength = (*env)->GetArrayLength(env, jYear);
341 jTempChars = (jchar*) malloc((ckLength) * sizeof(jchar));
342 if (jTempChars == NULL) {
343 free(ckpDate);
344 throwOutOfMemoryError(env, 0);
345 return NULL;
346 }
347 (*env)->GetCharArrayRegion(env, jYear, 0, ckLength, jTempChars);
348 if ((*env)->ExceptionCheck(env)) {
349 free(ckpDate);
350 free(jTempChars);
351 return NULL;
352 }
353
354 for (i = 0; (i < ckLength) && (i < 4) ; i++) {
355 ckpDate->year[i] = jCharToCKChar(jTempChars[i]);
356 }
357 free(jTempChars);
358 }
359
360 if (jMonth == NULL) {
361 ckpDate->month[0] = 0;
362 ckpDate->month[1] = 0;
363 } else {
364 ckLength = (*env)->GetArrayLength(env, jMonth);
365 jTempChars = (jchar*) malloc((ckLength) * sizeof(jchar));
366 if (jTempChars == NULL) {
367 free(ckpDate);
368 throwOutOfMemoryError(env, 0);
369 return NULL;
370 }
371 (*env)->GetCharArrayRegion(env, jMonth, 0, ckLength, jTempChars);
372 if ((*env)->ExceptionCheck(env)) {
373 free(ckpDate);
374 free(jTempChars);
375 return NULL;
376 }
377
378 for (i = 0; (i < ckLength) && (i < 2) ; i++) {
379 ckpDate->month[i] = jCharToCKChar(jTempChars[i]);
380 }
381 free(jTempChars);
382 }
383
384 if (jDay == NULL) {
385 ckpDate->day[0] = 0;
386 ckpDate->day[1] = 0;
387 } else {
388 ckLength = (*env)->GetArrayLength(env, jDay);
389 jTempChars = (jchar*) malloc((ckLength) * sizeof(jchar));
390 if (jTempChars == NULL) {
391 free(ckpDate);
392 throwOutOfMemoryError(env, 0);
393 return NULL;
394 }
395 (*env)->GetCharArrayRegion(env, jDay, 0, ckLength, jTempChars);
396 if ((*env)->ExceptionCheck(env)) {
397 free(ckpDate);
398 free(jTempChars);
399 return NULL;
400 }
401
402 for (i = 0; (i < ckLength) && (i < 2) ; i++) {
403 ckpDate->day[i] = jCharToCKChar(jTempChars[i]);
404 }
405 free(jTempChars);
406 }
407
408 return ckpDate ;
409}
410
411
412/*
413 * converts a Java CK_ATTRIBUTE object into a CK_ATTRIBUTE structure
414 *
415 * @param env - used to call JNI funktions to get the values out of the Java object
416 * @param jAttribute - the Java CK_ATTRIBUTE object to convert
417 * @return - the new CK_ATTRIBUTE structure
418 */
419CK_ATTRIBUTE jAttributeToCKAttribute(JNIEnv *env, jobject jAttribute)
420{
421 CK_ATTRIBUTE ckAttribute;
422 jclass jAttributeClass;
423 jfieldID jFieldID;
424 jlong jType;
425 jobject jPValue;
426 memset(&ckAttribute, 0, sizeof(CK_ATTRIBUTE));
427
428 // TBD: what if jAttribute == NULL?!
429 TRACE0("\nDEBUG: jAttributeToCKAttribute");
430
431 /* get CK_ATTRIBUTE class */
432 TRACE0(", getting attribute object class");
433 jAttributeClass = (*env)->GetObjectClass(env, jAttribute);
434 if (jAttributeClass == NULL) { return ckAttribute; }
435
436 /* get type */
437 TRACE0(", getting type field");
438 jFieldID = (*env)->GetFieldID(env, jAttributeClass, "type", "J");
439 if (jFieldID == NULL) { return ckAttribute; }
440 jType = (*env)->GetLongField(env, jAttribute, jFieldID);
441 TRACE1(", type=0x%lX", jType);
442
443 /* get pValue */
444 TRACE0(", getting pValue field");
445 jFieldID = (*env)->GetFieldID(env, jAttributeClass, "pValue", "Ljava/lang/Object;");
446 if (jFieldID == NULL) { return ckAttribute; }
447 jPValue = (*env)->GetObjectField(env, jAttribute, jFieldID);
448 TRACE1(", pValue=%p", jPValue);
449
450 ckAttribute.type = jLongToCKULong(jType);
451 TRACE0(", converting pValue to primitive object");
452
453 /* convert the Java pValue object to a CK-type pValue pointer */
454 ckAttribute.pValue = jObjectToPrimitiveCKObjectPtr(env, jPValue, &(ckAttribute.ulValueLen));
455
456 TRACE0("\nDEBUG: jAttributeToCKAttribute FINISHED\n");
457
458 return ckAttribute ;
459}
460
461void masterKeyDeriveParamToCKMasterKeyDeriveParam(JNIEnv *env, jobject jParam,
462 jclass masterKeyDeriveParamClass,
463 CK_VERSION_PTR* cKMasterKeyDeriveParamVersion,
464 CK_SSL3_RANDOM_DATA* cKMasterKeyDeriveParamRandomInfo) {
465 jfieldID fieldID;
466 jclass jSsl3RandomDataClass;
467 jobject jRandomInfo, jRIClientRandom, jRIServerRandom, jVersion;
468
469 /* get RandomInfo */
470 fieldID = (*env)->GetFieldID(env, masterKeyDeriveParamClass, "RandomInfo",
471 "Lsun/security/pkcs11/wrapper/CK_SSL3_RANDOM_DATA;");
472 if (fieldID == NULL) { return; }
473 jRandomInfo = (*env)->GetObjectField(env, jParam, fieldID);
474
475 /* get pClientRandom and ulClientRandomLength out of RandomInfo */
476 jSsl3RandomDataClass = (*env)->FindClass(env, CLASS_SSL3_RANDOM_DATA);
477 if (jSsl3RandomDataClass == NULL) { return; }
478 fieldID = (*env)->GetFieldID(env, jSsl3RandomDataClass, "pClientRandom", "[B");
479 if (fieldID == NULL) { return; }
480 jRIClientRandom = (*env)->GetObjectField(env, jRandomInfo, fieldID);
481
482 /* get pServerRandom and ulServerRandomLength out of RandomInfo */
483 fieldID = (*env)->GetFieldID(env, jSsl3RandomDataClass, "pServerRandom", "[B");
484 if (fieldID == NULL) { return; }
485 jRIServerRandom = (*env)->GetObjectField(env, jRandomInfo, fieldID);
486
487 /* get pVersion */
488 fieldID = (*env)->GetFieldID(env, masterKeyDeriveParamClass, "pVersion",
489 "Lsun/security/pkcs11/wrapper/CK_VERSION;");
490 if (fieldID == NULL) { return; }
491 jVersion = (*env)->GetObjectField(env, jParam, fieldID);
492
493 /* populate java values */
494 *cKMasterKeyDeriveParamVersion = jVersionToCKVersionPtr(env, jVersion);
495 if ((*env)->ExceptionCheck(env)) { return; }
496 jByteArrayToCKByteArray(env, jRIClientRandom,
497 &(cKMasterKeyDeriveParamRandomInfo->pClientRandom),
498 &(cKMasterKeyDeriveParamRandomInfo->ulClientRandomLen));
499 if ((*env)->ExceptionCheck(env)) {
500 free(*cKMasterKeyDeriveParamVersion);
501 return;
502 }
503 jByteArrayToCKByteArray(env, jRIServerRandom,
504 &(cKMasterKeyDeriveParamRandomInfo->pServerRandom),
505 &(cKMasterKeyDeriveParamRandomInfo->ulServerRandomLen));
506 if ((*env)->ExceptionCheck(env)) {
507 free(*cKMasterKeyDeriveParamVersion);
508 free(cKMasterKeyDeriveParamRandomInfo->pClientRandom);
509 return;
510 }
511}
512
513/*
514 * converts the Java CK_SSL3_MASTER_KEY_DERIVE_PARAMS object to a
515 * CK_SSL3_MASTER_KEY_DERIVE_PARAMS structure
516 *
517 * @param env - used to call JNI functions to get the Java classes and objects
518 * @param jParam - the Java CK_SSL3_MASTER_KEY_DERIVE_PARAMS object to convert
519 * @param ckParamPtr - pointer to the new CK_SSL3_MASTER_KEY_DERIVE_PARAMS structure
520 */
521void jSsl3MasterKeyDeriveParamToCKSsl3MasterKeyDeriveParam(JNIEnv *env,
522 jobject jParam, CK_SSL3_MASTER_KEY_DERIVE_PARAMS_PTR ckParamPtr)
523{
524 jclass jSsl3MasterKeyDeriveParamsClass;
525 memset(ckParamPtr, 0, sizeof(CK_SSL3_MASTER_KEY_DERIVE_PARAMS));
526 jSsl3MasterKeyDeriveParamsClass =
527 (*env)->FindClass(env, CLASS_SSL3_MASTER_KEY_DERIVE_PARAMS);
528 if (jSsl3MasterKeyDeriveParamsClass == NULL) { return; }
529 masterKeyDeriveParamToCKMasterKeyDeriveParam(env, jParam,
530 jSsl3MasterKeyDeriveParamsClass,
531 &(ckParamPtr->pVersion), &(ckParamPtr->RandomInfo));
532}
533
534/*
535 * converts the Java CK_TLS12_MASTER_KEY_DERIVE_PARAMS object to a
536 * CK_TLS12_MASTER_KEY_DERIVE_PARAMS structure
537 *
538 * @param env - used to call JNI functions to get the Java classes and objects
539 * @param jParam - the Java CK_TLS12_MASTER_KEY_DERIVE_PARAMS object to convert
540 * @param ckParamPtr - pointer to the new CK_TLS12_MASTER_KEY_DERIVE_PARAMS structure
541 */
542void jTls12MasterKeyDeriveParamToCKTls12MasterKeyDeriveParam(JNIEnv *env,
543 jobject jParam, CK_TLS12_MASTER_KEY_DERIVE_PARAMS_PTR ckParamPtr)
544{
545 jclass jTls12MasterKeyDeriveParamsClass;
546 jfieldID fieldID;
547 memset(ckParamPtr, 0, sizeof(CK_TLS12_MASTER_KEY_DERIVE_PARAMS));
548 jTls12MasterKeyDeriveParamsClass =
549 (*env)->FindClass(env, CLASS_TLS12_MASTER_KEY_DERIVE_PARAMS);
550 if (jTls12MasterKeyDeriveParamsClass == NULL) { return; }
551 masterKeyDeriveParamToCKMasterKeyDeriveParam(env, jParam,
552 jTls12MasterKeyDeriveParamsClass, &ckParamPtr->pVersion,
553 &ckParamPtr->RandomInfo);
554 fieldID = (*env)->GetFieldID(env,
555 jTls12MasterKeyDeriveParamsClass, "prfHashMechanism", "J");
556 if (fieldID != NULL) {
557 jlong prfHashMechanism =
558 (*env)->GetLongField(env, jParam, fieldID);
559 ckParamPtr->prfHashMechanism = (CK_MECHANISM_TYPE)prfHashMechanism;
560 }
561}
562
563/*
564 * converts the Java CK_TLS_PRF_PARAMS object to a CK_TLS_PRF_PARAMS structure
565 */
566void jTlsPrfParamsToCKTlsPrfParam(JNIEnv *env, jobject jParam,
567CK_TLS_PRF_PARAMS_PTR ckParamPtr)
568{
569 jclass jTlsPrfParamsClass;
570 jfieldID fieldID;
571 jobject jSeed, jLabel, jOutput;
572 memset(ckParamPtr, 0, sizeof(CK_TLS_PRF_PARAMS));
573
574 // TBD: what if jParam == NULL?!
575
576 /* get pSeed */
577 jTlsPrfParamsClass = (*env)->FindClass(env, CLASS_TLS_PRF_PARAMS);
578 if (jTlsPrfParamsClass == NULL) { return; }
579 fieldID = (*env)->GetFieldID(env, jTlsPrfParamsClass, "pSeed", "[B");
580 if (fieldID == NULL) { return; }
581 jSeed = (*env)->GetObjectField(env, jParam, fieldID);
582
583 /* get pLabel */
584 fieldID = (*env)->GetFieldID(env, jTlsPrfParamsClass, "pLabel", "[B");
585 if (fieldID == NULL) { return; }
586 jLabel = (*env)->GetObjectField(env, jParam, fieldID);
587
588 /* get pOutput */
589 fieldID = (*env)->GetFieldID(env, jTlsPrfParamsClass, "pOutput", "[B");
590 if (fieldID == NULL) { return; }
591 jOutput = (*env)->GetObjectField(env, jParam, fieldID);
592
593 /* populate java values */
594 jByteArrayToCKByteArray(env, jSeed, &(ckParamPtr->pSeed), &(ckParamPtr->ulSeedLen));
595 if ((*env)->ExceptionCheck(env)) { return; }
596 jByteArrayToCKByteArray(env, jLabel, &(ckParamPtr->pLabel), &(ckParamPtr->ulLabelLen));
597 if ((*env)->ExceptionCheck(env)) {
598 free(ckParamPtr->pSeed);
599 return;
600 }
601 ckParamPtr->pulOutputLen = malloc(sizeof(CK_ULONG));
602 if (ckParamPtr->pulOutputLen == NULL) {
603 free(ckParamPtr->pSeed);
604 free(ckParamPtr->pLabel);
605 throwOutOfMemoryError(env, 0);
606 return;
607 }
608 jByteArrayToCKByteArray(env, jOutput, &(ckParamPtr->pOutput), ckParamPtr->pulOutputLen);
609 if ((*env)->ExceptionCheck(env)) {
610 free(ckParamPtr->pSeed);
611 free(ckParamPtr->pLabel);
612 free(ckParamPtr->pOutput);
613 return;
614 }
615}
616
617/*
618 * converts the Java CK_TLS_MAC_PARAMS object to a CK_TLS_MAC_PARAMS structure
619 */
620void jTlsMacParamsToCKTlsMacParam(JNIEnv *env, jobject jParam,
621 CK_TLS_MAC_PARAMS_PTR ckParamPtr)
622{
623 jclass jTlsMacParamsClass;
624 jfieldID fieldID;
625 jlong jPrfMechanism, jUlMacLength, jUlServerOrClient;
626 memset(ckParamPtr, 0, sizeof(CK_TLS_MAC_PARAMS));
627
628 jTlsMacParamsClass = (*env)->FindClass(env, CLASS_TLS_MAC_PARAMS);
629 if (jTlsMacParamsClass == NULL) { return; }
630
631 /* get prfMechanism */
632 fieldID = (*env)->GetFieldID(env, jTlsMacParamsClass, "prfMechanism", "J");
633 if (fieldID == NULL) { return; }
634 jPrfMechanism = (*env)->GetLongField(env, jParam, fieldID);
635
636 /* get ulMacLength */
637 fieldID = (*env)->GetFieldID(env, jTlsMacParamsClass, "ulMacLength", "J");
638 if (fieldID == NULL) { return; }
639 jUlMacLength = (*env)->GetLongField(env, jParam, fieldID);
640
641 /* get ulServerOrClient */
642 fieldID = (*env)->GetFieldID(env, jTlsMacParamsClass, "ulServerOrClient", "J");
643 if (fieldID == NULL) { return; }
644 jUlServerOrClient = (*env)->GetLongField(env, jParam, fieldID);
645
646 /* populate java values */
647 ckParamPtr->prfMechanism = jLongToCKULong(jPrfMechanism);
648 ckParamPtr->ulMacLength = jLongToCKULong(jUlMacLength);
649 ckParamPtr->ulServerOrClient = jLongToCKULong(jUlServerOrClient);
650}
651
652void keyMatParamToCKKeyMatParam(JNIEnv *env, jobject jParam,
653 jclass jKeyMatParamClass,
654 CK_ULONG* cKKeyMatParamUlMacSizeInBits,
655 CK_ULONG* cKKeyMatParamUlKeySizeInBits,
656 CK_ULONG* cKKeyMatParamUlIVSizeInBits,
657 CK_BBOOL* cKKeyMatParamBIsExport,
658 CK_SSL3_RANDOM_DATA* cKKeyMatParamRandomInfo,
659 CK_SSL3_KEY_MAT_OUT_PTR* cKKeyMatParamPReturnedKeyMaterial)
660{
661 jclass jSsl3RandomDataClass, jSsl3KeyMatOutClass;
662 jfieldID fieldID;
663 jlong jMacSizeInBits, jKeySizeInBits, jIVSizeInBits;
664 jboolean jIsExport;
665 jobject jRandomInfo, jRIClientRandom, jRIServerRandom;
666 jobject jReturnedKeyMaterial, jRMIvClient, jRMIvServer;
667 CK_ULONG ckTemp;
668
669 /* get ulMacSizeInBits */
670 fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "ulMacSizeInBits", "J");
671 if (fieldID == NULL) { return; }
672 jMacSizeInBits = (*env)->GetLongField(env, jParam, fieldID);
673
674 /* get ulKeySizeInBits */
675 fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "ulKeySizeInBits", "J");
676 if (fieldID == NULL) { return; }
677 jKeySizeInBits = (*env)->GetLongField(env, jParam, fieldID);
678
679 /* get ulIVSizeInBits */
680 fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "ulIVSizeInBits", "J");
681 if (fieldID == NULL) { return; }
682 jIVSizeInBits = (*env)->GetLongField(env, jParam, fieldID);
683
684 /* get bIsExport */
685 fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "bIsExport", "Z");
686 if (fieldID == NULL) { return; }
687 jIsExport = (*env)->GetBooleanField(env, jParam, fieldID);
688
689 /* get RandomInfo */
690 jSsl3RandomDataClass = (*env)->FindClass(env, CLASS_SSL3_RANDOM_DATA);
691 if (jSsl3RandomDataClass == NULL) { return; }
692 fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "RandomInfo",
693 "Lsun/security/pkcs11/wrapper/CK_SSL3_RANDOM_DATA;");
694 if (fieldID == NULL) { return; }
695 jRandomInfo = (*env)->GetObjectField(env, jParam, fieldID);
696
697 /* get pClientRandom and ulClientRandomLength out of RandomInfo */
698 fieldID = (*env)->GetFieldID(env, jSsl3RandomDataClass, "pClientRandom", "[B");
699 if (fieldID == NULL) { return; }
700 jRIClientRandom = (*env)->GetObjectField(env, jRandomInfo, fieldID);
701
702 /* get pServerRandom and ulServerRandomLength out of RandomInfo */
703 fieldID = (*env)->GetFieldID(env, jSsl3RandomDataClass, "pServerRandom", "[B");
704 if (fieldID == NULL) { return; }
705 jRIServerRandom = (*env)->GetObjectField(env, jRandomInfo, fieldID);
706
707 /* get pReturnedKeyMaterial */
708 jSsl3KeyMatOutClass = (*env)->FindClass(env, CLASS_SSL3_KEY_MAT_OUT);
709 if (jSsl3KeyMatOutClass == NULL) { return; }
710 fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "pReturnedKeyMaterial",
711 "Lsun/security/pkcs11/wrapper/CK_SSL3_KEY_MAT_OUT;");
712 if (fieldID == NULL) { return; }
713 jReturnedKeyMaterial = (*env)->GetObjectField(env, jParam, fieldID);
714
715 /* get pIVClient out of pReturnedKeyMaterial */
716 fieldID = (*env)->GetFieldID(env, jSsl3KeyMatOutClass, "pIVClient", "[B");
717 if (fieldID == NULL) { return; }
718 jRMIvClient = (*env)->GetObjectField(env, jReturnedKeyMaterial, fieldID);
719
720 /* get pIVServer out of pReturnedKeyMaterial */
721 fieldID = (*env)->GetFieldID(env, jSsl3KeyMatOutClass, "pIVServer", "[B");
722 if (fieldID == NULL) { return; }
723 jRMIvServer = (*env)->GetObjectField(env, jReturnedKeyMaterial, fieldID);
724
725 /* populate java values */
726 *cKKeyMatParamUlMacSizeInBits = jLongToCKULong(jMacSizeInBits);
727 *cKKeyMatParamUlKeySizeInBits = jLongToCKULong(jKeySizeInBits);
728 *cKKeyMatParamUlIVSizeInBits = jLongToCKULong(jIVSizeInBits);
729 *cKKeyMatParamBIsExport = jBooleanToCKBBool(jIsExport);
730 jByteArrayToCKByteArray(env, jRIClientRandom,
731 &(cKKeyMatParamRandomInfo->pClientRandom),
732 &(cKKeyMatParamRandomInfo->ulClientRandomLen));
733 if ((*env)->ExceptionCheck(env)) { return; }
734 jByteArrayToCKByteArray(env, jRIServerRandom,
735 &(cKKeyMatParamRandomInfo->pServerRandom),
736 &(cKKeyMatParamRandomInfo->ulServerRandomLen));
737 if ((*env)->ExceptionCheck(env)) {
738 free(cKKeyMatParamRandomInfo->pClientRandom);
739 return;
740 }
741 /* allocate memory for pReturnedKeyMaterial */
742 *cKKeyMatParamPReturnedKeyMaterial =
743 (CK_SSL3_KEY_MAT_OUT_PTR)malloc(sizeof(CK_SSL3_KEY_MAT_OUT));
744 if (*cKKeyMatParamPReturnedKeyMaterial == NULL) {
745 free(cKKeyMatParamRandomInfo->pClientRandom);
746 free(cKKeyMatParamRandomInfo->pServerRandom);
747 throwOutOfMemoryError(env, 0);
748 return;
749 }
750
751 // the handles are output params only, no need to fetch them from Java
752 (*cKKeyMatParamPReturnedKeyMaterial)->hClientMacSecret = 0;
753 (*cKKeyMatParamPReturnedKeyMaterial)->hServerMacSecret = 0;
754 (*cKKeyMatParamPReturnedKeyMaterial)->hClientKey = 0;
755 (*cKKeyMatParamPReturnedKeyMaterial)->hServerKey = 0;
756
757 jByteArrayToCKByteArray(env, jRMIvClient,
758 &((*cKKeyMatParamPReturnedKeyMaterial)->pIVClient), &ckTemp);
759 if ((*env)->ExceptionCheck(env)) {
760 free(cKKeyMatParamRandomInfo->pClientRandom);
761 free(cKKeyMatParamRandomInfo->pServerRandom);
762 free((*cKKeyMatParamPReturnedKeyMaterial));
763 return;
764 }
765 jByteArrayToCKByteArray(env, jRMIvServer,
766 &((*cKKeyMatParamPReturnedKeyMaterial)->pIVServer), &ckTemp);
767 if ((*env)->ExceptionCheck(env)) {
768 free(cKKeyMatParamRandomInfo->pClientRandom);
769 free(cKKeyMatParamRandomInfo->pServerRandom);
770 free((*cKKeyMatParamPReturnedKeyMaterial)->pIVClient);
771 free((*cKKeyMatParamPReturnedKeyMaterial));
772 return;
773 }
774
775 return;
776}
777/*
778 * converts the Java CK_SSL3_KEY_MAT_PARAMS object to a
779 * CK_SSL3_KEY_MAT_PARAMS structure
780 *
781 * @param env - used to call JNI funktions to get the Java classes and objects
782 * @param jParam - the Java CK_SSL3_KEY_MAT_PARAMS object to convert
783 * @param ckParamPtr - pointer to the new CK_SSL3_KEY_MAT_PARAMS structure
784 */
785void jSsl3KeyMatParamToCKSsl3KeyMatParam(JNIEnv *env, jobject jParam,
786 CK_SSL3_KEY_MAT_PARAMS_PTR ckParamPtr)
787{
788 jclass jSsl3KeyMatParamsClass;
789 memset(ckParamPtr, 0, sizeof(CK_SSL3_KEY_MAT_PARAMS));
790 jSsl3KeyMatParamsClass = (*env)->FindClass(env,
791 CLASS_SSL3_KEY_MAT_PARAMS);
792 if (jSsl3KeyMatParamsClass == NULL) { return; }
793 keyMatParamToCKKeyMatParam(env, jParam, jSsl3KeyMatParamsClass,
794 &(ckParamPtr->ulMacSizeInBits), &(ckParamPtr->ulKeySizeInBits),
795 &(ckParamPtr->ulIVSizeInBits), &(ckParamPtr->bIsExport),
796 &(ckParamPtr->RandomInfo), &(ckParamPtr->pReturnedKeyMaterial));
797}
798
799/*
800 * converts the Java CK_TLS12_KEY_MAT_PARAMS object to a
801 * CK_TLS12_KEY_MAT_PARAMS structure
802 *
803 * @param env - used to call JNI functions to get the Java classes and objects
804 * @param jParam - the Java CK_TLS12_KEY_MAT_PARAMS object to convert
805 * @param ckParamPtr - pointer to the new CK_TLS12_KEY_MAT_PARAMS structure
806 */
807void jTls12KeyMatParamToCKTls12KeyMatParam(JNIEnv *env,
808 jobject jParam, CK_TLS12_KEY_MAT_PARAMS_PTR ckParamPtr)
809{
810 jclass jTls12KeyMatParamsClass;
811 jfieldID fieldID;
812 memset(ckParamPtr, 0, sizeof(CK_TLS12_KEY_MAT_PARAMS));
813 jTls12KeyMatParamsClass = (*env)->FindClass(env,
814 CLASS_TLS12_KEY_MAT_PARAMS);
815 if (jTls12KeyMatParamsClass == NULL) { return; }
816 keyMatParamToCKKeyMatParam(env, jParam, jTls12KeyMatParamsClass,
817 &(ckParamPtr->ulMacSizeInBits), &(ckParamPtr->ulKeySizeInBits),
818 &(ckParamPtr->ulIVSizeInBits), &(ckParamPtr->bIsExport),
819 &(ckParamPtr->RandomInfo), &(ckParamPtr->pReturnedKeyMaterial));
820 fieldID = (*env)->GetFieldID(env, jTls12KeyMatParamsClass,
821 "prfHashMechanism", "J");
822 if (fieldID != NULL) {
823 jlong prfHashMechanism = (*env)->GetLongField(env, jParam, fieldID);
824 ckParamPtr->prfHashMechanism = (CK_MECHANISM_TYPE)prfHashMechanism;
825 }
826}
827
828/*
829 * converts the Java CK_AES_CTR_PARAMS object to a CK_AES_CTR_PARAMS structure
830 *
831 * @param env - used to call JNI funktions to get the Java classes and objects
832 * @param jParam - the Java CK_AES_CTR_PARAMS object to convert
833 * @param ckParamPtr - pointer to the new CK_AES_CTR_PARAMS structure
834 */
835void jAesCtrParamsToCKAesCtrParam(JNIEnv *env, jobject jParam,
836 CK_AES_CTR_PARAMS_PTR ckParamPtr) {
837 jclass jAesCtrParamsClass;
838 jfieldID fieldID;
839 jlong jCounterBits;
840 jobject jCb;
841 CK_BYTE_PTR ckBytes;
842 CK_ULONG ckTemp;
843
844 /* get ulCounterBits */
845 jAesCtrParamsClass = (*env)->FindClass(env, CLASS_AES_CTR_PARAMS);
846 if (jAesCtrParamsClass == NULL) { return; }
847 if (!(*env)->IsInstanceOf(env, jParam, jAesCtrParamsClass)) {
848 return;
849 }
850 fieldID = (*env)->GetFieldID(env, jAesCtrParamsClass, "ulCounterBits", "J");
851 if (fieldID == NULL) { return; }
852 jCounterBits = (*env)->GetLongField(env, jParam, fieldID);
853
854 /* get cb */
855 fieldID = (*env)->GetFieldID(env, jAesCtrParamsClass, "cb", "[B");
856 if (fieldID == NULL) { return; }
857 jCb = (*env)->GetObjectField(env, jParam, fieldID);
858
859 /* populate java values */
860 ckParamPtr->ulCounterBits = jLongToCKULong(jCounterBits);
861 jByteArrayToCKByteArray(env, jCb, &ckBytes, &ckTemp);
862 if ((*env)->ExceptionCheck(env)) { return; }
863 if (ckTemp != 16) {
864 TRACE1("\nERROR: WRONG CTR IV LENGTH %lu", ckTemp);
865 } else {
866 memcpy(ckParamPtr->cb, ckBytes, ckTemp);
867 free(ckBytes);
868 }
869}
870
871/*
872 * converts the Java CK_GCM_PARAMS object to a CK_GCM_PARAMS structure
873 *
874 * @param env - used to call JNI funktions to get the Java classes and objects
875 * @param jParam - the Java CK_GCM_PARAMS object to convert
876 * @param ckpParam - pointer to the new CK_GCM_PARAMS structure
877 */
878void jGCMParamsToCKGCMParam(JNIEnv *env, jobject jParam,
879 CK_GCM_PARAMS_PTR ckpParam) {
880 jclass jGcmParamsClass;
881 jfieldID fieldID;
882 jobject jIv, jAad;
883 jlong jTagLen;
884
885 /* get iv */
886 jGcmParamsClass = (*env)->FindClass(env, CLASS_GCM_PARAMS);
887 if (jGcmParamsClass == NULL) { return; }
888 if (!(*env)->IsInstanceOf(env, jParam, jGcmParamsClass)) {
889 return;
890 }
891
892 fieldID = (*env)->GetFieldID(env, jGcmParamsClass, "iv", "[B");
893 if (fieldID == NULL) { return; }
894 jIv = (*env)->GetObjectField(env, jParam, fieldID);
895
896 /* get aad */
897 fieldID = (*env)->GetFieldID(env, jGcmParamsClass, "aad", "[B");
898 if (fieldID == NULL) { return; }
899 jAad = (*env)->GetObjectField(env, jParam, fieldID);
900
901 /* get tagLength */
902 fieldID = (*env)->GetFieldID(env, jGcmParamsClass, "tagBits", "J");
903 if (fieldID == NULL) { return; }
904 jTagLen = (*env)->GetLongField(env, jParam, fieldID);
905
906
907 /* populate java values */
908 jByteArrayToCKByteArray(env, jIv, &(ckpParam->pIv), &(ckpParam->ulIvLen));
909 if ((*env)->ExceptionCheck(env)) { return; }
910
911 jByteArrayToCKByteArray(env, jAad, &(ckpParam->pAAD), &(ckpParam->ulAADLen));
912 if ((*env)->ExceptionCheck(env)) { return; }
913
914 ckpParam->ulTagBits = jLongToCKULong(jTagLen);
915}
916
917/*
918 * converts the Java CK_CCM_PARAMS object to a CK_CCM_PARAMS structure
919 *
920 * @param env - used to call JNI functions to get the Java classes and objects
921 * @param jParam - the Java CK_CCM_PARAMS object to convert
922 * @param ckpParam - pointer to the new CK_CCM_PARAMS structure
923 */
924void jCCMParamsToCKCCMParam(JNIEnv *env, jobject jParam,
925 CK_CCM_PARAMS_PTR ckpParam) {
926 jclass jCcmParamsClass;
927 jfieldID fieldID;
928 jobject jNonce, jAad;
929 jlong jDataLen, jMacLen;
930
931 /* get iv */
932 jCcmParamsClass = (*env)->FindClass(env, CLASS_CCM_PARAMS);
933 if (jCcmParamsClass == NULL) { return; }
934
935 if (!(*env)->IsInstanceOf(env, jParam, jCcmParamsClass)) {
936 return;
937 }
938 fieldID = (*env)->GetFieldID(env, jCcmParamsClass, "dataLen", "J");
939 if (fieldID == NULL) { return; }
940 jDataLen = (*env)->GetLongField(env, jParam, fieldID);
941
942 fieldID = (*env)->GetFieldID(env, jCcmParamsClass, "nonce", "[B");
943 if (fieldID == NULL) { return; }
944 jNonce = (*env)->GetObjectField(env, jParam, fieldID);
945
946 /* get aad */
947 fieldID = (*env)->GetFieldID(env, jCcmParamsClass, "aad", "[B");
948 if (fieldID == NULL) { return; }
949 jAad = (*env)->GetObjectField(env, jParam, fieldID);
950
951 /* get macLen */
952 fieldID = (*env)->GetFieldID(env, jCcmParamsClass, "macLen", "J");
953 if (fieldID == NULL) { return; }
954 jMacLen = (*env)->GetLongField(env, jParam, fieldID);
955
956 /* populate java values */
957 ckpParam->ulDataLen = jLongToCKULong(jDataLen);
958 jByteArrayToCKByteArray(env, jNonce, &(ckpParam->pNonce),
959 &(ckpParam->ulNonceLen));
960 jByteArrayToCKByteArray(env, jAad, &(ckpParam->pAAD),
961 &(ckpParam->ulAADLen));
962 ckpParam->ulMACLen = jLongToCKULong(jMacLen);
963 if ((*env)->ExceptionCheck(env)) { return; }
964}
965
966/*
967 * converts a Java CK_MECHANISM object into a pointer to a CK_MECHANISM
968 * structure. NOTE: CALLER IS RESPONSIBLE FOR FREEING THE RETURNED POINTER
969 *
970 * @param env - used to call JNI funktions to get the values out of the Java object
971 * @param jMechanism - the Java CK_MECHANISM object to convert
972 * @return - pointer to the new CK_MECHANISM structure
973 */
974CK_MECHANISM_PTR jMechanismToCKMechanismPtr(JNIEnv *env, jobject jMech)
975{
976 CK_MECHANISM_PTR ckpMech;
977 jlong jMechType = (*env)->GetLongField(env, jMech, mech_mechanismID);
978 jobject jParam = (*env)->GetObjectField(env, jMech, mech_pParameterID);
979
980 /* allocate memory for CK_MECHANISM_PTR */
981 ckpMech = (CK_MECHANISM_PTR) malloc(sizeof(CK_MECHANISM));
982 if (ckpMech == NULL) {
983 throwOutOfMemoryError(env, 0);
984 return NULL;
985 }
986 TRACE1("DEBUG jMechanismToCKMechanismPtr: allocated mech 0x%lX\n", ckpMech);
987
988 ckpMech->mechanism = jLongToCKULong(jMechType);
989
990 /* convert the specific Java mechanism parameter object to a pointer to a
991 * CK-type mechanism structure
992 */
993 if (jParam == NULL) {
994 ckpMech->pParameter = NULL;
995 ckpMech->ulParameterLen = 0;
996 } else {
997 ckpMech->pParameter = jMechParamToCKMechParamPtr(env, jParam,
998 ckpMech->mechanism, &(ckpMech->ulParameterLen));
999 }
1000 return ckpMech;
1001}
1002
1003/*
1004 * the following functions convert Attribute and Mechanism value pointers
1005 *
1006 * jobject ckAttributeValueToJObject(JNIEnv *env,
1007 * const CK_ATTRIBUTE_PTR ckpAttribute);
1008 *
1009 * CK_VOID_PTR jObjectToPrimitiveCKObjectPtr(JNIEnv *env,
1010 * jobject jObject,
1011 * CK_ULONG *pLength);
1012 *
1013 * CK_VOID_PTR jMechParamToCKMechParamPtr(JNIEnv *env,
1014 * jobject jParam,
1015 * CK_MECHANISM_TYPE ckMech,
1016 * CK_ULONG *ckpLength);
1017 *
1018 * These functions are used if a PKCS#11 mechanism or attribute structure gets
1019 * convertet to a Java attribute or mechanism object or vice versa.
1020 *
1021 * ckAttributeValueToJObject converts a PKCS#11 attribute value pointer to a Java
1022 * object depending on the type of the Attribute. A PKCS#11 attribute value can
1023 * be a CK_ULONG, CK_BYTE[], CK_CHAR[], big integer, CK_BBOOL, CK_UTF8CHAR[],
1024 * CK_DATE or CK_FLAGS that gets converted to a corresponding Java object.
1025 *
1026 * jObjectToPrimitiveCKObjectPtr is used by jAttributeToCKAttributePtr for
1027 * converting the Java attribute value to a PKCS#11 attribute value pointer.
1028 * For now only primitive datatypes and arrays of primitive datatypes can get
1029 * converted. Otherwise this function throws a PKCS#11Exception with the
1030 * errorcode CKR_VENDOR_DEFINED.
1031 *
1032 * jMechParamToCKMechParamPtr converts a Java mechanism parameter to a PKCS#11
1033 * mechanism parameter. Based on the specified PKCS#11 mechanism type, this
1034 * function will allocate memory for a PKCS#11 parameter structure and
1035 * populate its field using the Java object.
1036 */
1037
1038/*
1039 * converts the pValue of a CK_ATTRIBUTE structure into a Java Object by
1040 * checking the type of the attribute.
1041 *
1042 * @param env - used to call JNI funktions to create the new Java object
1043 * @param ckpAttribute - the pointer to the CK_ATTRIBUTE structure that contains the type
1044 * and the pValue to convert
1045 * @return - the new Java object of the CK-type pValue
1046 */
1047jobject ckAttributeValueToJObject(JNIEnv *env, const CK_ATTRIBUTE_PTR ckpAttribute)
1048{
1049 jint jValueLength;
1050 jobject jValueObject = NULL;
1051
1052 jValueLength = ckULongToJInt(ckpAttribute->ulValueLen);
1053
1054 if ((jValueLength <= 0) || (ckpAttribute->pValue == NULL)) {
1055 return NULL ;
1056 }
1057
1058 switch(ckpAttribute->type) {
1059 case CKA_CLASS:
1060 /* value CK_OBJECT_CLASS, defacto a CK_ULONG */
1061 case CKA_KEY_TYPE:
1062 /* value CK_KEY_TYPE, defacto a CK_ULONG */
1063 case CKA_CERTIFICATE_TYPE:
1064 /* value CK_CERTIFICATE_TYPE, defacto a CK_ULONG */
1065 case CKA_HW_FEATURE_TYPE:
1066 /* value CK_HW_FEATURE_TYPE, defacto a CK_ULONG */
1067 case CKA_MODULUS_BITS:
1068 case CKA_VALUE_BITS:
1069 case CKA_VALUE_LEN:
1070 case CKA_KEY_GEN_MECHANISM:
1071 case CKA_PRIME_BITS:
1072 case CKA_SUB_PRIME_BITS:
1073 /* value CK_ULONG */
1074 jValueObject = ckULongPtrToJLongObject(env, (CK_ULONG*) ckpAttribute->pValue);
1075 break;
1076
1077 /* can be CK_BYTE[],CK_CHAR[] or big integer; defacto always CK_BYTE[] */
1078 case CKA_VALUE:
1079 case CKA_OBJECT_ID:
1080 case CKA_SUBJECT:
1081 case CKA_ID:
1082 case CKA_ISSUER:
1083 case CKA_SERIAL_NUMBER:
1084 case CKA_OWNER:
1085 case CKA_AC_ISSUER:
1086 case CKA_ATTR_TYPES:
1087 case CKA_ECDSA_PARAMS:
1088 /* CKA_EC_PARAMS is the same, these two are equivalent */
1089 case CKA_EC_POINT:
1090 case CKA_PRIVATE_EXPONENT:
1091 case CKA_PRIME_1:
1092 case CKA_PRIME_2:
1093 case CKA_EXPONENT_1:
1094 case CKA_EXPONENT_2:
1095 case CKA_COEFFICIENT:
1096 /* value CK_BYTE[] */
1097 jValueObject = ckByteArrayToJByteArray(env, (CK_BYTE*) ckpAttribute->pValue, jValueLength);
1098 break;
1099
1100 case CKA_RESET_ON_INIT:
1101 case CKA_HAS_RESET:
1102 case CKA_TOKEN:
1103 case CKA_PRIVATE:
1104 case CKA_MODIFIABLE:
1105 case CKA_DERIVE:
1106 case CKA_LOCAL:
1107 case CKA_ENCRYPT:
1108 case CKA_VERIFY:
1109 case CKA_VERIFY_RECOVER:
1110 case CKA_WRAP:
1111 case CKA_SENSITIVE:
1112 case CKA_SECONDARY_AUTH:
1113 case CKA_DECRYPT:
1114 case CKA_SIGN:
1115 case CKA_SIGN_RECOVER:
1116 case CKA_UNWRAP:
1117 case CKA_EXTRACTABLE:
1118 case CKA_ALWAYS_SENSITIVE:
1119 case CKA_NEVER_EXTRACTABLE:
1120 case CKA_TRUSTED:
1121 /* value CK_BBOOL */
1122 jValueObject = ckBBoolPtrToJBooleanObject(env, (CK_BBOOL*) ckpAttribute->pValue);
1123 break;
1124
1125 case CKA_LABEL:
1126 case CKA_APPLICATION:
1127 /* value RFC 2279 (UTF-8) string */
1128 jValueObject = ckUTF8CharArrayToJCharArray(env, (CK_UTF8CHAR*) ckpAttribute->pValue, jValueLength);
1129 break;
1130
1131 case CKA_START_DATE:
1132 case CKA_END_DATE:
1133 /* value CK_DATE */
1134 jValueObject = ckDatePtrToJDateObject(env, (CK_DATE*) ckpAttribute->pValue);
1135 break;
1136
1137 case CKA_MODULUS:
1138 case CKA_PUBLIC_EXPONENT:
1139 case CKA_PRIME:
1140 case CKA_SUBPRIME:
1141 case CKA_BASE:
1142 /* value big integer, i.e. CK_BYTE[] */
1143 jValueObject = ckByteArrayToJByteArray(env, (CK_BYTE*) ckpAttribute->pValue, jValueLength);
1144 break;
1145
1146 case CKA_AUTH_PIN_FLAGS:
1147 jValueObject = ckULongPtrToJLongObject(env, (CK_ULONG*) ckpAttribute->pValue);
1148 /* value FLAGS, defacto a CK_ULONG */
1149 break;
1150
1151 case CKA_VENDOR_DEFINED:
1152 /* we make a CK_BYTE[] out of this */
1153 jValueObject = ckByteArrayToJByteArray(env, (CK_BYTE*) ckpAttribute->pValue, jValueLength);
1154 break;
1155
1156 // Netscape trust attributes
1157 case CKA_NETSCAPE_TRUST_SERVER_AUTH:
1158 case CKA_NETSCAPE_TRUST_CLIENT_AUTH:
1159 case CKA_NETSCAPE_TRUST_CODE_SIGNING:
1160 case CKA_NETSCAPE_TRUST_EMAIL_PROTECTION:
1161 /* value CK_ULONG */
1162 jValueObject = ckULongPtrToJLongObject(env, (CK_ULONG*) ckpAttribute->pValue);
1163 break;
1164
1165 default:
1166 /* we make a CK_BYTE[] out of this */
1167 jValueObject = ckByteArrayToJByteArray(env, (CK_BYTE*) ckpAttribute->pValue, jValueLength);
1168 break;
1169 }
1170
1171 return jValueObject ;
1172}
1173
1174/*
1175 * the following functions convert a Java mechanism parameter object to a PKCS#11
1176 * mechanism parameter structure
1177 *
1178 * CK_<Param>_PARAMS j<Param>ParamToCK<Param>Param(JNIEnv *env,
1179 * jobject jParam);
1180 *
1181 * These functions get a Java object, that must be the right Java mechanism
1182 * object and they return the new PKCS#11 mechanism parameter structure.
1183 * Every field of the Java object is retrieved, gets converted to a corresponding
1184 * PKCS#11 type and is set in the new PKCS#11 structure.
1185 */
1186
1187/*
1188 * converts the given Java mechanism parameter to a CK mechanism parameter structure
1189 * and store the length in bytes in the length variable.
1190 * The memory of *ckpParamPtr has to be freed after use!
1191 *
1192 * @param env - used to call JNI funktions to get the Java classes and objects
1193 * @param jParam - the Java mechanism parameter object to convert
1194 * @param ckMech - the PKCS#11 mechanism type
1195 * @param ckpLength - the reference of the length in bytes of the new CK mechanism parameter
1196 * structure
1197 * @return pointer to the new CK mechanism parameter structure
1198 */
1199CK_VOID_PTR jMechParamToCKMechParamPtr(JNIEnv *env, jobject jParam,
1200 CK_MECHANISM_TYPE ckMech, CK_ULONG *ckpLength)
1201{
1202 CK_VOID_PTR ckpParamPtr;
1203 if (jParam == NULL) {
1204 ckpParamPtr = NULL;
1205 *ckpLength = 0;
1206 } else if ((*env)->IsInstanceOf(env, jParam, jByteArrayClass)) {
1207 jByteArrayToCKByteArray(env, jParam, (CK_BYTE_PTR *) &ckpParamPtr, ckpLength);
1208 } else if ((*env)->IsInstanceOf(env, jParam, jLongClass)) {
1209 ckpParamPtr = jLongObjectToCKULongPtr(env, jParam);
1210 *ckpLength = sizeof(CK_ULONG);
1211 } else {
1212 ckpParamPtr = jMechParamToCKMechParamPtrSlow(env, jParam, ckMech, ckpLength);
1213 }
1214 return ckpParamPtr;
1215}
1216
1217CK_VOID_PTR jMechParamToCKMechParamPtrSlow(JNIEnv *env, jobject jParam,
1218 CK_MECHANISM_TYPE ckMech, CK_ULONG *ckpLength)
1219{
1220 CK_VOID_PTR ckpParamPtr = NULL;
1221
1222 /*
1223 * Most common cases, i.e. NULL/byte[]/long, are already handled by
1224 * jMechParamToCKMechParam before calling this method.
1225 */
1226 TRACE1("\nDEBUG: jMechParamToCKMechParamPtrSlow, mech=0x%lX", ckMech);
1227
1228 switch (ckMech) {
1229 case CKM_SSL3_PRE_MASTER_KEY_GEN:
1230 case CKM_TLS_PRE_MASTER_KEY_GEN:
1231 *ckpLength = sizeof(CK_VERSION);
1232 ckpParamPtr = jVersionToCKVersionPtr(env, jParam);
1233 break;
1234 case CKM_SSL3_MASTER_KEY_DERIVE:
1235 case CKM_TLS_MASTER_KEY_DERIVE:
1236 case CKM_SSL3_MASTER_KEY_DERIVE_DH:
1237 case CKM_TLS_MASTER_KEY_DERIVE_DH:
1238 ckpParamPtr =
1239 malloc(sizeof(CK_SSL3_MASTER_KEY_DERIVE_PARAMS));
1240 if (ckpParamPtr == NULL) {
1241 throwOutOfMemoryError(env, 0);
1242 return NULL;
1243 }
1244 *ckpLength = sizeof(CK_SSL3_MASTER_KEY_DERIVE_PARAMS);
1245
1246 jSsl3MasterKeyDeriveParamToCKSsl3MasterKeyDeriveParam(env, jParam,
1247 (CK_SSL3_MASTER_KEY_DERIVE_PARAMS_PTR) ckpParamPtr);
1248 break;
1249 case CKM_SSL3_KEY_AND_MAC_DERIVE:
1250 case CKM_TLS_KEY_AND_MAC_DERIVE:
1251 ckpParamPtr =
1252 malloc(sizeof(CK_SSL3_KEY_MAT_PARAMS));
1253 if (ckpParamPtr == NULL) {
1254 throwOutOfMemoryError(env, 0);
1255 return NULL;
1256 }
1257 *ckpLength = sizeof(CK_SSL3_KEY_MAT_PARAMS);
1258
1259 jSsl3KeyMatParamToCKSsl3KeyMatParam(env, jParam,
1260 (CK_SSL3_KEY_MAT_PARAMS_PTR) ckpParamPtr);
1261 break;
1262 case CKM_TLS12_KEY_AND_MAC_DERIVE:
1263 ckpParamPtr =
1264 malloc(sizeof(CK_TLS12_KEY_MAT_PARAMS));
1265 if (ckpParamPtr == NULL) {
1266 throwOutOfMemoryError(env, 0);
1267 return NULL;
1268 }
1269 *ckpLength = sizeof(CK_TLS12_KEY_MAT_PARAMS);
1270
1271 jTls12KeyMatParamToCKTls12KeyMatParam(env, jParam,
1272 (CK_TLS12_KEY_MAT_PARAMS_PTR) ckpParamPtr);
1273 break;
1274 case CKM_TLS12_MASTER_KEY_DERIVE:
1275 case CKM_TLS12_MASTER_KEY_DERIVE_DH:
1276 ckpParamPtr =
1277 malloc(sizeof(CK_TLS12_MASTER_KEY_DERIVE_PARAMS));
1278 if (ckpParamPtr == NULL) {
1279 throwOutOfMemoryError(env, 0);
1280 return NULL;
1281 }
1282 *ckpLength = sizeof(CK_TLS12_MASTER_KEY_DERIVE_PARAMS);
1283
1284 jTls12MasterKeyDeriveParamToCKTls12MasterKeyDeriveParam(env, jParam,
1285 (CK_TLS12_MASTER_KEY_DERIVE_PARAMS_PTR) ckpParamPtr);
1286 break;
1287 case CKM_TLS_PRF:
1288 case CKM_NSS_TLS_PRF_GENERAL:
1289 ckpParamPtr = malloc(sizeof(CK_TLS_PRF_PARAMS));
1290 if (ckpParamPtr == NULL) {
1291 throwOutOfMemoryError(env, 0);
1292 return NULL;
1293 }
1294 *ckpLength = sizeof(CK_TLS_PRF_PARAMS);
1295
1296 jTlsPrfParamsToCKTlsPrfParam(env, jParam,
1297 (CK_TLS_PRF_PARAMS_PTR) ckpParamPtr);
1298 break;
1299 case CKM_TLS_MAC:
1300 ckpParamPtr = malloc(sizeof(CK_TLS_MAC_PARAMS));
1301 if (ckpParamPtr == NULL) {
1302 throwOutOfMemoryError(env, 0);
1303 return NULL;
1304 }
1305 *ckpLength = sizeof(CK_TLS_MAC_PARAMS);
1306
1307 jTlsMacParamsToCKTlsMacParam(env, jParam,
1308 (CK_TLS_MAC_PARAMS_PTR) ckpParamPtr);
1309 break;
1310 case CKM_AES_CTR:
1311 ckpParamPtr = malloc(sizeof(CK_AES_CTR_PARAMS));
1312 if (ckpParamPtr == NULL) {
1313 throwOutOfMemoryError(env, 0);
1314 return NULL;
1315 }
1316 *ckpLength = sizeof(CK_AES_CTR_PARAMS);
1317
1318 jAesCtrParamsToCKAesCtrParam(env, jParam,
1319 (CK_AES_CTR_PARAMS_PTR) ckpParamPtr);
1320 break;
1321 case CKM_AES_GCM:
1322 ckpParamPtr = malloc(sizeof(CK_GCM_PARAMS));
1323 if (ckpParamPtr == NULL) {
1324 throwOutOfMemoryError(env, 0);
1325 return NULL;
1326 }
1327 *ckpLength = sizeof(CK_GCM_PARAMS);
1328
1329 jGCMParamsToCKGCMParam(env, jParam, (CK_GCM_PARAMS_PTR) ckpParamPtr);
1330 break;
1331 case CKM_AES_CCM:
1332 ckpParamPtr = malloc(sizeof(CK_CCM_PARAMS));
1333 if (ckpParamPtr == NULL) {
1334 throwOutOfMemoryError(env, 0);
1335 return NULL;
1336 }
1337 *ckpLength = sizeof(CK_CCM_PARAMS);
1338
1339 jCCMParamsToCKCCMParam(env, jParam,
1340 (CK_CCM_PARAMS_PTR) ckpParamPtr);
1341 break;
1342 case CKM_RSA_PKCS_OAEP:
1343 ckpParamPtr = malloc(sizeof(CK_RSA_PKCS_OAEP_PARAMS));
1344 if (ckpParamPtr == NULL) {
1345 throwOutOfMemoryError(env, 0);
1346 return NULL;
1347 }
1348 *ckpLength = sizeof(CK_RSA_PKCS_OAEP_PARAMS);
1349
1350 jRsaPkcsOaepParamToCKRsaPkcsOaepParam(env, jParam,
1351 (CK_RSA_PKCS_OAEP_PARAMS_PTR) ckpParamPtr);
1352 break;
1353 case CKM_PBE_SHA1_DES3_EDE_CBC:
1354 case CKM_PBE_SHA1_DES2_EDE_CBC:
1355 case CKM_PBA_SHA1_WITH_SHA1_HMAC:
1356 ckpParamPtr = malloc(sizeof(CK_PBE_PARAMS));
1357 if (ckpParamPtr == NULL) {
1358 throwOutOfMemoryError(env, 0);
1359 return NULL;
1360 }
1361
1362 *ckpLength = sizeof(CK_PBE_PARAMS);
1363
1364 jPbeParamToCKPbeParam(env, jParam, (CK_PBE_PARAMS_PTR) ckpParamPtr);
1365 break;
1366 case CKM_PKCS5_PBKD2:
1367 ckpParamPtr = malloc(sizeof(CK_PKCS5_PBKD2_PARAMS));
1368 if (ckpParamPtr == NULL) {
1369 throwOutOfMemoryError(env, 0);
1370 return NULL;
1371 }
1372 *ckpLength = sizeof(CK_PKCS5_PBKD2_PARAMS);
1373
1374 jPkcs5Pbkd2ParamToCKPkcs5Pbkd2Param(env, jParam,
1375 (CK_PKCS5_PBKD2_PARAMS_PTR) ckpParamPtr);
1376 break;
1377 case CKM_RSA_PKCS_PSS:
1378 case CKM_SHA1_RSA_PKCS_PSS:
1379 case CKM_SHA256_RSA_PKCS_PSS:
1380 case CKM_SHA384_RSA_PKCS_PSS:
1381 case CKM_SHA512_RSA_PKCS_PSS:
1382 case CKM_SHA224_RSA_PKCS_PSS:
1383 ckpParamPtr = malloc(sizeof(CK_RSA_PKCS_PSS_PARAMS));
1384 if (ckpParamPtr == NULL) {
1385 throwOutOfMemoryError(env, 0);
1386 return NULL;
1387 }
1388 *ckpLength = sizeof(CK_RSA_PKCS_PSS_PARAMS);
1389
1390 jRsaPkcsPssParamToCKRsaPkcsPssParam(env, jParam,
1391 (CK_RSA_PKCS_PSS_PARAMS_PTR) ckpParamPtr);
1392 break;
1393 case CKM_ECDH1_DERIVE:
1394 case CKM_ECDH1_COFACTOR_DERIVE:
1395 ckpParamPtr = malloc(sizeof(CK_ECDH1_DERIVE_PARAMS));
1396 if (ckpParamPtr == NULL) {
1397 throwOutOfMemoryError(env, 0);
1398 return NULL;
1399 }
1400 *ckpLength = sizeof(CK_ECDH1_DERIVE_PARAMS);
1401
1402 jEcdh1DeriveParamToCKEcdh1DeriveParam(env, jParam,
1403 (CK_ECDH1_DERIVE_PARAMS_PTR) ckpParamPtr);
1404 break;
1405 case CKM_ECMQV_DERIVE:
1406 ckpParamPtr = malloc(sizeof(CK_ECDH2_DERIVE_PARAMS));
1407 if (ckpParamPtr == NULL) {
1408 throwOutOfMemoryError(env, 0);
1409 return NULL;
1410 }
1411 *ckpLength = sizeof(CK_ECDH2_DERIVE_PARAMS);
1412
1413 jEcdh2DeriveParamToCKEcdh2DeriveParam(env, jParam,
1414 (CK_ECDH2_DERIVE_PARAMS_PTR) ckpParamPtr);
1415 break;
1416 case CKM_X9_42_DH_DERIVE:
1417 ckpParamPtr = malloc(sizeof(CK_X9_42_DH1_DERIVE_PARAMS));
1418 if (ckpParamPtr == NULL) {
1419 throwOutOfMemoryError(env, 0);
1420 return NULL;
1421 }
1422 *ckpLength = sizeof(CK_X9_42_DH1_DERIVE_PARAMS);
1423
1424 /* convert jParameter to CKParameter */
1425 jX942Dh1DeriveParamToCKX942Dh1DeriveParam(env, jParam,
1426 (CK_X9_42_DH1_DERIVE_PARAMS_PTR) ckpParamPtr);
1427 break;
1428 case CKM_X9_42_DH_HYBRID_DERIVE:
1429 case CKM_X9_42_MQV_DERIVE:
1430 ckpParamPtr = malloc(sizeof(CK_X9_42_DH2_DERIVE_PARAMS));
1431 if (ckpParamPtr == NULL) {
1432 throwOutOfMemoryError(env, 0);
1433 return NULL;
1434 }
1435 *ckpLength = sizeof(CK_X9_42_DH2_DERIVE_PARAMS);
1436
1437 jX942Dh2DeriveParamToCKX942Dh2DeriveParam(env, jParam,
1438 (CK_X9_42_DH2_DERIVE_PARAMS_PTR) ckpParamPtr);
1439 break;
1440 // defined by pkcs11.h but we don't support
1441 case CKM_KEA_DERIVE: // CK_KEA_DERIVE_PARAMS
1442 case CKM_RC2_CBC: // CK_RC2_CBC_PARAMS
1443 case CKM_RC2_MAC_GENERAL: // CK_RC2_MAC_GENERAL_PARAMS
1444 case CKM_RC5_ECB: // CK_RC5_PARAMS
1445 case CKM_RC5_MAC: // CK_RC5_PARAMS
1446 case CKM_RC5_CBC: // CK_RC5_CBC_PARAMS
1447 case CKM_RC5_MAC_GENERAL: // CK_RC5_MAC_GENERAL_PARAMS
1448 case CKM_SKIPJACK_PRIVATE_WRAP: // CK_SKIPJACK_PRIVATE_WRAP_PARAMS
1449 case CKM_SKIPJACK_RELAYX: // CK_SKIPJACK_RELAYX_PARAMS
1450 case CKM_KEY_WRAP_SET_OAEP: // CK_KEY_WRAP_SET_OAEP_PARAMS
1451 throwPKCS11RuntimeException(env, "No parameter support for this mchanism");
1452 break;
1453 default:
1454 /* if everything faild up to here */
1455 /* try if the parameter is a primitive Java type */
1456 ckpParamPtr = jObjectToPrimitiveCKObjectPtr(env, jParam, ckpLength);
1457 /* *ckpParamPtr = jObjectToCKVoidPtr(jParam); */
1458 /* *ckpLength = 1; */
1459 }
1460 TRACE0("\nDEBUG: jMechParamToCKMechParamPtrSlow FINISHED\n");
1461
1462 if ((*env)->ExceptionCheck(env)) {
1463 free(ckpParamPtr);
1464 *ckpLength = 0;
1465 return NULL;
1466 }
1467
1468 return ckpParamPtr;
1469}
1470
1471/* the mechanism parameter convertion functions: */
1472
1473/*
1474 * converts the Java CK_RSA_PKCS_OAEP_PARAMS object to a CK_RSA_PKCS_OAEP_PARAMS structure
1475 *
1476 * @param env - used to call JNI funktions to get the Java classes and objects
1477 * @param jParam - the Java CK_RSA_PKCS_OAEP_PARAMS object to convert
1478 * @return - the new CK_RSA_PKCS_OAEP_PARAMS structure
1479 */
1480void jRsaPkcsOaepParamToCKRsaPkcsOaepParam(JNIEnv *env, jobject jParam,
1481CK_RSA_PKCS_OAEP_PARAMS_PTR ckParamPtr)
1482{
1483 jclass jRsaPkcsOaepParamsClass;
1484 jfieldID fieldID;
1485 jlong jHashAlg, jMgf, jSource;
1486 jobject jSourceData;
1487 CK_BYTE_PTR ckpByte;
1488 memset(ckParamPtr, 0, sizeof(CK_RSA_PKCS_OAEP_PARAMS));
1489
1490 /* get hashAlg */
1491 jRsaPkcsOaepParamsClass = (*env)->FindClass(env, CLASS_RSA_PKCS_OAEP_PARAMS);
1492 if (jRsaPkcsOaepParamsClass == NULL) { return; }
1493 fieldID = (*env)->GetFieldID(env, jRsaPkcsOaepParamsClass, "hashAlg", "J");
1494 if (fieldID == NULL) { return; }
1495 jHashAlg = (*env)->GetLongField(env, jParam, fieldID);
1496
1497 /* get mgf */
1498 fieldID = (*env)->GetFieldID(env, jRsaPkcsOaepParamsClass, "mgf", "J");
1499 if (fieldID == NULL) { return; }
1500 jMgf = (*env)->GetLongField(env, jParam, fieldID);
1501
1502 /* get source */
1503 fieldID = (*env)->GetFieldID(env, jRsaPkcsOaepParamsClass, "source", "J");
1504 if (fieldID == NULL) { return; }
1505 jSource = (*env)->GetLongField(env, jParam, fieldID);
1506
1507 /* get sourceData and sourceDataLength */
1508 fieldID = (*env)->GetFieldID(env, jRsaPkcsOaepParamsClass, "pSourceData", "[B");
1509 if (fieldID == NULL) { return; }
1510 jSourceData = (*env)->GetObjectField(env, jParam, fieldID);
1511
1512 /* populate java values */
1513 ckParamPtr->hashAlg = jLongToCKULong(jHashAlg);
1514 ckParamPtr->mgf = jLongToCKULong(jMgf);
1515 ckParamPtr->source = jLongToCKULong(jSource);
1516 jByteArrayToCKByteArray(env, jSourceData, & ckpByte, &(ckParamPtr->ulSourceDataLen));
1517 if ((*env)->ExceptionCheck(env)) { return; }
1518 ckParamPtr->pSourceData = (CK_VOID_PTR) ckpByte;
1519}
1520
1521/*
1522 * converts the Java CK_PBE_PARAMS object to a CK_PBE_PARAMS structure
1523 *
1524 * @param env - used to call JNI funktions to get the Java classes and objects
1525 * @param jParam - the Java CK_PBE_PARAMS object to convert
1526 * @param ckParamPtr pointer to the new CK_PBE_PARAMS structure
1527 */
1528void jPbeParamToCKPbeParam(JNIEnv *env, jobject jParam,
1529CK_PBE_PARAMS_PTR ckParamPtr)
1530{
1531 jclass jPbeParamsClass;
1532 jfieldID fieldID;
1533 jlong jIteration;
1534 jobject jInitVector, jPassword, jSalt;
1535 CK_ULONG ckTemp;
1536 memset(ckParamPtr, 0, sizeof(CK_PBE_PARAMS));
1537
1538 /* get pInitVector */
1539 jPbeParamsClass = (*env)->FindClass(env, CLASS_PBE_PARAMS);
1540 if (jPbeParamsClass == NULL) { return; }
1541 fieldID = (*env)->GetFieldID(env, jPbeParamsClass, "pInitVector", "[C");
1542 if (fieldID == NULL) { return; }
1543 jInitVector = (*env)->GetObjectField(env, jParam, fieldID);
1544
1545 /* get pPassword and ulPasswordLength */
1546 fieldID = (*env)->GetFieldID(env, jPbeParamsClass, "pPassword", "[C");
1547 if (fieldID == NULL) { return; }
1548 jPassword = (*env)->GetObjectField(env, jParam, fieldID);
1549
1550 /* get pSalt and ulSaltLength */
1551 fieldID = (*env)->GetFieldID(env, jPbeParamsClass, "pSalt", "[C");
1552 if (fieldID == NULL) { return; }
1553 jSalt = (*env)->GetObjectField(env, jParam, fieldID);
1554
1555 /* get ulIteration */
1556 fieldID = (*env)->GetFieldID(env, jPbeParamsClass, "ulIteration", "J");
1557 if (fieldID == NULL) { return; }
1558 jIteration = (*env)->GetLongField(env, jParam, fieldID);
1559
1560 /* populate java values */
1561 ckParamPtr->ulIteration = jLongToCKULong(jIteration);
1562 jCharArrayToCKCharArray(env, jInitVector, &(ckParamPtr->pInitVector), &ckTemp);
1563 if ((*env)->ExceptionCheck(env)) { return; }
1564 jCharArrayToCKCharArray(env, jPassword, &(ckParamPtr->pPassword), &(ckParamPtr->ulPasswordLen));
1565 if ((*env)->ExceptionCheck(env)) {
1566 free(ckParamPtr->pInitVector);
1567 return;
1568 }
1569 jCharArrayToCKCharArray(env, jSalt, &(ckParamPtr->pSalt), &(ckParamPtr->ulSaltLen));
1570 if ((*env)->ExceptionCheck(env)) {
1571 free(ckParamPtr->pInitVector);
1572 free(ckParamPtr->pPassword);
1573 return;
1574 }
1575}
1576
1577/*
1578 * Copy back the initialization vector from the native structure to the
1579 * Java object. This is only used for CKM_PBE_* mechanisms and their
1580 * CK_PBE_PARAMS parameters.
1581 *
1582 */
1583void copyBackPBEInitializationVector(JNIEnv *env, CK_MECHANISM *ckMechanism, jobject jMechanism)
1584{
1585 jclass jMechanismClass, jPbeParamsClass;
1586 CK_PBE_PARAMS *ckParam;
1587 jfieldID fieldID;
1588 CK_MECHANISM_TYPE ckMechanismType;
1589 jlong jMechanismType;
1590 jobject jParameter;
1591 jobject jInitVector;
1592 jint jInitVectorLength;
1593 CK_CHAR_PTR initVector;
1594 int i;
1595 jchar* jInitVectorChars;
1596
1597 /* get mechanism */
1598 jMechanismClass = (*env)->FindClass(env, CLASS_MECHANISM);
1599 if (jMechanismClass == NULL) { return; }
1600 fieldID = (*env)->GetFieldID(env, jMechanismClass, "mechanism", "J");
1601 if (fieldID == NULL) { return; }
1602 jMechanismType = (*env)->GetLongField(env, jMechanism, fieldID);
1603 ckMechanismType = jLongToCKULong(jMechanismType);
1604 if (ckMechanismType != ckMechanism->mechanism) {
1605 /* we do not have maching types, this should not occur */
1606 return;
1607 }
1608
1609 jPbeParamsClass = (*env)->FindClass(env, CLASS_PBE_PARAMS);
1610 if (jPbeParamsClass == NULL) { return; }
1611 ckParam = (CK_PBE_PARAMS *) ckMechanism->pParameter;
1612 if (ckParam != NULL_PTR) {
1613 initVector = ckParam->pInitVector;
1614 if (initVector != NULL_PTR) {
1615 /* get pParameter */
1616 fieldID = (*env)->GetFieldID(env, jMechanismClass, "pParameter", "Ljava/lang/Object;");
1617 if (fieldID == NULL) { return; }
1618 jParameter = (*env)->GetObjectField(env, jMechanism, fieldID);
1619 fieldID = (*env)->GetFieldID(env, jPbeParamsClass, "pInitVektor", "[C");
1620 if (fieldID == NULL) { return; }
1621 jInitVector = (*env)->GetObjectField(env, jParameter, fieldID);
1622
1623 if (jInitVector != NULL) {
1624 jInitVectorLength = (*env)->GetArrayLength(env, jInitVector);
1625 jInitVectorChars = (*env)->GetCharArrayElements(env, jInitVector, NULL);
1626 if (jInitVectorChars == NULL) { return; }
1627
1628 /* copy the chars to the Java buffer */
1629 for (i=0; i < jInitVectorLength; i++) {
1630 jInitVectorChars[i] = ckCharToJChar(initVector[i]);
1631 }
1632 /* copy back the Java buffer to the object */
1633 (*env)->ReleaseCharArrayElements(env, jInitVector, jInitVectorChars, 0);
1634 }
1635 }
1636 }
1637}
1638
1639/*
1640 * converts the Java CK_PKCS5_PBKD2_PARAMS object to a CK_PKCS5_PBKD2_PARAMS structure
1641 *
1642 * @param env - used to call JNI funktions to get the Java classes and objects
1643 * @param jParam - the Java CK_PKCS5_PBKD2_PARAMS object to convert
1644 * @param ckParamPtr - pointer to the new CK_PKCS5_PBKD2_PARAMS structure
1645 */
1646void jPkcs5Pbkd2ParamToCKPkcs5Pbkd2Param(JNIEnv *env, jobject jParam,
1647CK_PKCS5_PBKD2_PARAMS_PTR ckParamPtr)
1648{
1649 jclass jPkcs5Pbkd2ParamsClass;
1650 jfieldID fieldID;
1651 jlong jSaltSource, jIteration, jPrf;
1652 jobject jSaltSourceData, jPrfData;
1653 memset(ckParamPtr, 0, sizeof(CK_PKCS5_PBKD2_PARAMS));
1654
1655 /* get saltSource */
1656 jPkcs5Pbkd2ParamsClass = (*env)->FindClass(env, CLASS_PKCS5_PBKD2_PARAMS);
1657 if (jPkcs5Pbkd2ParamsClass == NULL) { return; }
1658 fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "saltSource", "J");
1659 if (fieldID == NULL) { return; }
1660 jSaltSource = (*env)->GetLongField(env, jParam, fieldID);
1661
1662 /* get pSaltSourceData */
1663 fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "pSaltSourceData", "[B");
1664 if (fieldID == NULL) { return; }
1665 jSaltSourceData = (*env)->GetObjectField(env, jParam, fieldID);
1666
1667 /* get iterations */
1668 fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "iterations", "J");
1669 if (fieldID == NULL) { return; }
1670 jIteration = (*env)->GetLongField(env, jParam, fieldID);
1671
1672 /* get prf */
1673 fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "prf", "J");
1674 if (fieldID == NULL) { return; }
1675 jPrf = (*env)->GetLongField(env, jParam, fieldID);
1676
1677 /* get pPrfData and ulPrfDataLength in byte */
1678 fieldID = (*env)->GetFieldID(env, jPkcs5Pbkd2ParamsClass, "pPrfData", "[B");
1679 if (fieldID == NULL) { return; }
1680 jPrfData = (*env)->GetObjectField(env, jParam, fieldID);
1681
1682 /* populate java values */
1683 ckParamPtr->saltSource = jLongToCKULong(jSaltSource);
1684 jByteArrayToCKByteArray(env, jSaltSourceData, (CK_BYTE_PTR *) &(ckParamPtr->pSaltSourceData), &(ckParamPtr->ulSaltSourceDataLen));
1685 if ((*env)->ExceptionCheck(env)) { return; }
1686 ckParamPtr->iterations = jLongToCKULong(jIteration);
1687 ckParamPtr->prf = jLongToCKULong(jPrf);
1688 jByteArrayToCKByteArray(env, jPrfData, (CK_BYTE_PTR *) &(ckParamPtr->pPrfData), &(ckParamPtr->ulPrfDataLen));
1689 if ((*env)->ExceptionCheck(env)) {
1690 free(ckParamPtr->pSaltSourceData);
1691 return;
1692 }
1693}
1694
1695/*
1696 * converts the Java CK_RSA_PKCS_PSS_PARAMS object to a CK_RSA_PKCS_PSS_PARAMS structure
1697 *
1698 * @param env - used to call JNI funktions to get the Java classes and objects
1699 * @param jParam - the Java CK_RSA_PKCS_PSS_PARAMS object to convert
1700 * @param ckParamPtr - pointer to the new CK_RSA_PKCS_PSS_PARAMS structure
1701 */
1702void jRsaPkcsPssParamToCKRsaPkcsPssParam(JNIEnv *env, jobject jParam,
1703CK_RSA_PKCS_PSS_PARAMS_PTR ckParamPtr)
1704{
1705 jclass jRsaPkcsPssParamsClass;
1706 jfieldID fieldID;
1707 jlong jHashAlg, jMgf, jSLen;
1708 memset(ckParamPtr, 0, sizeof(CK_RSA_PKCS_PSS_PARAMS));
1709
1710 /* get hashAlg */
1711 jRsaPkcsPssParamsClass = (*env)->FindClass(env, CLASS_RSA_PKCS_PSS_PARAMS);
1712 if (jRsaPkcsPssParamsClass == NULL) { return; }
1713 fieldID = (*env)->GetFieldID(env, jRsaPkcsPssParamsClass, "hashAlg", "J");
1714 if (fieldID == NULL) { return; }
1715 jHashAlg = (*env)->GetLongField(env, jParam, fieldID);
1716
1717 /* get mgf */
1718 fieldID = (*env)->GetFieldID(env, jRsaPkcsPssParamsClass, "mgf", "J");
1719 if (fieldID == NULL) { return; }
1720 jMgf = (*env)->GetLongField(env, jParam, fieldID);
1721
1722 /* get sLen */
1723 fieldID = (*env)->GetFieldID(env, jRsaPkcsPssParamsClass, "sLen", "J");
1724 if (fieldID == NULL) { return; }
1725 jSLen = (*env)->GetLongField(env, jParam, fieldID);
1726
1727 /* populate java values */
1728 ckParamPtr->hashAlg = jLongToCKULong(jHashAlg);
1729 ckParamPtr->mgf = jLongToCKULong(jMgf);
1730 ckParamPtr->sLen = jLongToCKULong(jSLen);
1731 TRACE1("DEBUG: jRsaPkcsPssParamToCKRsaPkcsPssParam, hashAlg=0x%lX\n", ckParamPtr->hashAlg);
1732 TRACE1("DEBUG: jRsaPkcsPssParamToCKRsaPkcsPssParam, mgf=0x%lX\n", ckParamPtr->mgf);
1733 TRACE1("DEBUG: jRsaPkcsPssParamToCKRsaPkcsPssParam, sLen=%lu\n", ckParamPtr->sLen);
1734}
1735
1736/*
1737 * converts the Java CK_ECDH1_DERIVE_PARAMS object to a CK_ECDH1_DERIVE_PARAMS structure
1738 *
1739 * @param env - used to call JNI funktions to get the Java classes and objects
1740 * @param jParam - the Java CK_ECDH1_DERIVE_PARAMS object to convert
1741 * @param ckParamPtr - the new CK_ECDH1_DERIVE_PARAMS structure
1742 */
1743void jEcdh1DeriveParamToCKEcdh1DeriveParam(JNIEnv *env, jobject jParam,
1744CK_ECDH1_DERIVE_PARAMS_PTR ckParamPtr)
1745{
1746 jclass jEcdh1DeriveParamsClass;
1747 jfieldID fieldID;
1748 jlong jLong;
1749 jobject jSharedData, jPublicData;
1750 memset(ckParamPtr, 0, sizeof(CK_ECDH1_DERIVE_PARAMS));
1751
1752 /* get kdf */
1753 jEcdh1DeriveParamsClass = (*env)->FindClass(env, CLASS_ECDH1_DERIVE_PARAMS);
1754 if (jEcdh1DeriveParamsClass == NULL) { return; }
1755 fieldID = (*env)->GetFieldID(env, jEcdh1DeriveParamsClass, "kdf", "J");
1756 if (fieldID == NULL) { return; }
1757 jLong = (*env)->GetLongField(env, jParam, fieldID);
1758 ckParamPtr->kdf = jLongToCKULong(jLong);
1759
1760 /* get pSharedData and ulSharedDataLen */
1761 fieldID = (*env)->GetFieldID(env, jEcdh1DeriveParamsClass, "pSharedData", "[B");
1762 if (fieldID == NULL) { return; }
1763 jSharedData = (*env)->GetObjectField(env, jParam, fieldID);
1764
1765 /* get pPublicData and ulPublicDataLen */
1766 fieldID = (*env)->GetFieldID(env, jEcdh1DeriveParamsClass, "pPublicData", "[B");
1767 if (fieldID == NULL) { return; }
1768 jPublicData = (*env)->GetObjectField(env, jParam, fieldID);
1769
1770 /* populate java values */
1771 ckParamPtr->kdf = jLongToCKULong(jLong);
1772 jByteArrayToCKByteArray(env, jSharedData, &(ckParamPtr->pSharedData), &(ckParamPtr->ulSharedDataLen));
1773 if ((*env)->ExceptionCheck(env)) { return; }
1774 jByteArrayToCKByteArray(env, jPublicData, &(ckParamPtr->pPublicData), &(ckParamPtr->ulPublicDataLen));
1775 if ((*env)->ExceptionCheck(env)) {
1776 free(ckParamPtr->pSharedData);
1777 return;
1778 }
1779}
1780
1781/*
1782 * converts the Java CK_ECDH2_DERIVE_PARAMS object to a CK_ECDH2_DERIVE_PARAMS structure
1783 *
1784 * @param env - used to call JNI funktions to get the Java classes and objects
1785 * @param jParam - the Java CK_ECDH2_DERIVE_PARAMS object to convert
1786 * @param ckParamPtr - pointer to the new CK_ECDH2_DERIVE_PARAMS structure
1787 */
1788void jEcdh2DeriveParamToCKEcdh2DeriveParam(JNIEnv *env, jobject jParam,
1789CK_ECDH2_DERIVE_PARAMS_PTR ckParamPtr)
1790{
1791 jclass jEcdh2DeriveParamsClass;
1792 jfieldID fieldID;
1793 jlong jKdf, jPrivateDataLen, jPrivateData;
1794 jobject jSharedData, jPublicData, jPublicData2;
1795 memset(ckParamPtr, 0, sizeof(CK_ECDH2_DERIVE_PARAMS));
1796
1797 /* get kdf */
1798 jEcdh2DeriveParamsClass = (*env)->FindClass(env, CLASS_ECDH2_DERIVE_PARAMS);
1799 if (jEcdh2DeriveParamsClass == NULL) { return; }
1800 fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "kdf", "J");
1801 if (fieldID == NULL) { return; }
1802 jKdf = (*env)->GetLongField(env, jParam, fieldID);
1803
1804 /* get pSharedData and ulSharedDataLen */
1805 fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "pSharedData", "[B");
1806 if (fieldID == NULL) { return; }
1807 jSharedData = (*env)->GetObjectField(env, jParam, fieldID);
1808
1809 /* get pPublicData and ulPublicDataLen */
1810 fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "pPublicData", "[B");
1811 if (fieldID == NULL) { return; }
1812 jPublicData = (*env)->GetObjectField(env, jParam, fieldID);
1813
1814 /* get ulPrivateDataLen */
1815 fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "ulPrivateDataLen", "J");
1816 if (fieldID == NULL) { return; }
1817 jPrivateDataLen = (*env)->GetLongField(env, jParam, fieldID);
1818
1819 /* get hPrivateData */
1820 fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "hPrivateData", "J");
1821 if (fieldID == NULL) { return; }
1822 jPrivateData = (*env)->GetLongField(env, jParam, fieldID);
1823
1824 /* get pPublicData2 and ulPublicDataLen2 */
1825 fieldID = (*env)->GetFieldID(env, jEcdh2DeriveParamsClass, "pPublicData2", "[B");
1826 if (fieldID == NULL) { return; }
1827 jPublicData2 = (*env)->GetObjectField(env, jParam, fieldID);
1828
1829 /* populate java values */
1830 ckParamPtr->kdf = jLongToCKULong(jKdf);
1831 jByteArrayToCKByteArray(env, jSharedData, &(ckParamPtr->pSharedData), &(ckParamPtr->ulSharedDataLen));
1832 if ((*env)->ExceptionCheck(env)) { return; }
1833 jByteArrayToCKByteArray(env, jPublicData, &(ckParamPtr->pPublicData), &(ckParamPtr->ulPublicDataLen));
1834 if ((*env)->ExceptionCheck(env)) {
1835 free(ckParamPtr->pSharedData);
1836 return;
1837 }
1838 ckParamPtr->ulPrivateDataLen = jLongToCKULong(jPrivateDataLen);
1839 ckParamPtr->hPrivateData = jLongToCKULong(jPrivateData);
1840 jByteArrayToCKByteArray(env, jPublicData2, &(ckParamPtr->pPublicData2), &(ckParamPtr->ulPublicDataLen2));
1841 if ((*env)->ExceptionCheck(env)) {
1842 free(ckParamPtr->pSharedData);
1843 free(ckParamPtr->pPublicData);
1844 return;
1845 }
1846}
1847
1848/*
1849 * converts the Java CK_X9_42_DH1_DERIVE_PARAMS object to a CK_X9_42_DH1_DERIVE_PARAMS structure
1850 *
1851 * @param env - used to call JNI funktions to get the Java classes and objects
1852 * @param jParam - the Java CK_X9_42_DH1_DERIVE_PARAMS object to convert
1853 * @param ckParamPtr - pointer to the new CK_X9_42_DH1_DERIVE_PARAMS structure
1854 */
1855void jX942Dh1DeriveParamToCKX942Dh1DeriveParam(JNIEnv *env, jobject jParam,
1856 CK_X9_42_DH1_DERIVE_PARAMS_PTR ckParamPtr)
1857{
1858 jclass jX942Dh1DeriveParamsClass;
1859 jfieldID fieldID;
1860 jlong jKdf;
1861 jobject jOtherInfo, jPublicData;
1862 memset(ckParamPtr, 0, sizeof(CK_X9_42_DH1_DERIVE_PARAMS));
1863
1864 /* get kdf */
1865 jX942Dh1DeriveParamsClass = (*env)->FindClass(env, CLASS_X9_42_DH1_DERIVE_PARAMS);
1866 if (jX942Dh1DeriveParamsClass == NULL) { return; }
1867 fieldID = (*env)->GetFieldID(env, jX942Dh1DeriveParamsClass, "kdf", "J");
1868 if (fieldID == NULL) { return; }
1869 jKdf = (*env)->GetLongField(env, jParam, fieldID);
1870
1871 /* get pOtherInfo and ulOtherInfoLen */
1872 fieldID = (*env)->GetFieldID(env, jX942Dh1DeriveParamsClass, "pOtherInfo", "[B");
1873 if (fieldID == NULL) { return; }
1874 jOtherInfo = (*env)->GetObjectField(env, jParam, fieldID);
1875
1876 /* get pPublicData and ulPublicDataLen */
1877 fieldID = (*env)->GetFieldID(env, jX942Dh1DeriveParamsClass, "pPublicData", "[B");
1878 if (fieldID == NULL) { return; }
1879 jPublicData = (*env)->GetObjectField(env, jParam, fieldID);
1880
1881 /* populate java values */
1882 ckParamPtr->kdf = jLongToCKULong(jKdf);
1883 jByteArrayToCKByteArray(env, jOtherInfo, &(ckParamPtr->pOtherInfo), &(ckParamPtr->ulOtherInfoLen));
1884 if ((*env)->ExceptionCheck(env)) { return; }
1885 jByteArrayToCKByteArray(env, jPublicData, &(ckParamPtr->pPublicData), &(ckParamPtr->ulPublicDataLen));
1886 if ((*env)->ExceptionCheck(env)) {
1887 free(ckParamPtr->pOtherInfo);
1888 return;
1889 }
1890}
1891
1892/*
1893 * converts the Java CK_X9_42_DH2_DERIVE_PARAMS object to a CK_X9_42_DH2_DERIVE_PARAMS structure
1894 *
1895 * @param env - used to call JNI funktions to get the Java classes and objects
1896 * @param jParam - the Java CK_X9_42_DH2_DERIVE_PARAMS object to convert
1897 * @param ckParamPtr - pointer to the new CK_X9_42_DH2_DERIVE_PARAMS structure
1898 */
1899void jX942Dh2DeriveParamToCKX942Dh2DeriveParam(JNIEnv *env, jobject jParam,
1900 CK_X9_42_DH2_DERIVE_PARAMS_PTR ckParamPtr)
1901{
1902 jclass jX942Dh2DeriveParamsClass;
1903 jfieldID fieldID;
1904 jlong jKdf, jPrivateDataLen, jPrivateData;
1905 jobject jOtherInfo, jPublicData, jPublicData2;
1906 memset(ckParamPtr, 0, sizeof(CK_X9_42_DH2_DERIVE_PARAMS));
1907
1908 /* get kdf */
1909 jX942Dh2DeriveParamsClass = (*env)->FindClass(env, CLASS_X9_42_DH2_DERIVE_PARAMS);
1910 if (jX942Dh2DeriveParamsClass == NULL) { return; }
1911 fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "kdf", "J");
1912 if (fieldID == NULL) { return; }
1913 jKdf = (*env)->GetLongField(env, jParam, fieldID);
1914
1915 /* get pOtherInfo and ulOtherInfoLen */
1916 fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "pOtherInfo", "[B");
1917 if (fieldID == NULL) { return; }
1918 jOtherInfo = (*env)->GetObjectField(env, jParam, fieldID);
1919
1920 /* get pPublicData and ulPublicDataLen */
1921 fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "pPublicData", "[B");
1922 if (fieldID == NULL) { return; }
1923 jPublicData = (*env)->GetObjectField(env, jParam, fieldID);
1924
1925 /* get ulPrivateDataLen */
1926 fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "ulPrivateDataLen", "J");
1927 if (fieldID == NULL) { return; }
1928 jPrivateDataLen = (*env)->GetLongField(env, jParam, fieldID);
1929
1930 /* get hPrivateData */
1931 fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "hPrivateData", "J");
1932 if (fieldID == NULL) { return; }
1933 jPrivateData = (*env)->GetLongField(env, jParam, fieldID);
1934
1935 /* get pPublicData2 and ulPublicDataLen2 */
1936 fieldID = (*env)->GetFieldID(env, jX942Dh2DeriveParamsClass, "pPublicData2", "[B");
1937 if (fieldID == NULL) { return; }
1938 jPublicData2 = (*env)->GetObjectField(env, jParam, fieldID);
1939
1940 /* populate java values */
1941 ckParamPtr->kdf = jLongToCKULong(jKdf);
1942 jByteArrayToCKByteArray(env, jOtherInfo, &(ckParamPtr->pOtherInfo), &(ckParamPtr->ulOtherInfoLen));
1943 if ((*env)->ExceptionCheck(env)) { return; }
1944 jByteArrayToCKByteArray(env, jPublicData, &(ckParamPtr->pPublicData), &(ckParamPtr->ulPublicDataLen));
1945 if ((*env)->ExceptionCheck(env)) {
1946 free(ckParamPtr->pOtherInfo);
1947 return;
1948 }
1949 ckParamPtr->ulPrivateDataLen = jLongToCKULong(jPrivateDataLen);
1950 ckParamPtr->hPrivateData = jLongToCKULong(jPrivateData);
1951 jByteArrayToCKByteArray(env, jPublicData2, &(ckParamPtr->pPublicData2), &(ckParamPtr->ulPublicDataLen2));
1952 if ((*env)->ExceptionCheck(env)) {
1953 free(ckParamPtr->pOtherInfo);
1954 free(ckParamPtr->pPublicData);
1955 return;
1956 }
1957}
1958