-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
p11_convert.c
2193 lines (1972 loc) · 78.3 KB
/
p11_convert.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
*/
/* Copyright (c) 2002 Graz University of Technology. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The end-user documentation included with the redistribution, if any, must
* include the following acknowledgment:
*
* "This product includes software developed by IAIK of Graz University of
* Technology."
*
* Alternately, this acknowledgment may appear in the software itself, if
* and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Graz University of Technology" and "IAIK of Graz University of
* Technology" must not be used to endorse or promote products derived from
* this software without prior written permission.
*
* 5. Products derived from this software may not be called
* "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior
* written permission of Graz University of Technology.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* pkcs11wrapper.c
* 18.05.2001
*
* This is the implementation of the native functions of the Java to PKCS#11 interface.
* All function use some helper functions to convert the JNI types to PKCS#11 types.
*
* @author Karl Scheibelhofer <Karl.Scheibelhofer@iaik.at>
* @author Martin Schlaeffer <schlaeff@sbox.tugraz.at>
*/
#include "pkcs11wrapper.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "sun_security_pkcs11_wrapper_PKCS11.h"
/* declare file private functions */
CK_VOID_PTR jMechParamToCKMechParamPtrSlow(JNIEnv *env, jobject jParam,
CK_MECHANISM_TYPE ckMech, CK_ULONG *ckpLength);
/*
* converts a CK_DATE pointer into a Java CK_DATE Object.
*
* @param env - used to call JNI funktions to create the new Java object
* @param ckpValue - the pointer to the CK_DATE structure
* @return - the new Java CK_DATE object
*/
jobject ckDatePtrToJDateObject(JNIEnv *env, const CK_DATE *ckpDate)
{
jclass jDateClass;
jmethodID jCtrId;
jobject jDateObject;
jcharArray jYear;
jcharArray jMonth;
jcharArray jDay;
/* load CK_DATE class */
jDateClass = (*env)->FindClass(env, CLASS_DATE);
if (jDateClass == NULL) { return NULL; }
/* load CK_DATE constructor */
jCtrId = (*env)->GetMethodID(env, jDateClass, "<init>", "([C[C[C)V");
if (jCtrId == NULL) { return NULL; }
/* prep all fields */
jYear = ckCharArrayToJCharArray(env, (CK_CHAR_PTR)(ckpDate->year), 4);
if (jYear == NULL) { return NULL; }
jMonth = ckCharArrayToJCharArray(env, (CK_CHAR_PTR)(ckpDate->month), 2);
if (jMonth == NULL) { return NULL; }
jDay = ckCharArrayToJCharArray(env, (CK_CHAR_PTR)(ckpDate->day), 2);
if (jDay == NULL) { return NULL; }
/* create new CK_DATE object */
jDateObject =
(*env)->NewObject(env, jDateClass, jCtrId, jYear, jMonth, jDay);
if (jDateObject == NULL) { return NULL; }
/* free local references */
(*env)->DeleteLocalRef(env, jDateClass);
(*env)->DeleteLocalRef(env, jYear);
(*env)->DeleteLocalRef(env, jMonth);
(*env)->DeleteLocalRef(env, jDay);
return jDateObject ;
}
/*
* converts a CK_VERSION pointer into a Java CK_VERSION Object.
*
* @param env - used to call JNI funktions to create the new Java object
* @param ckpVersion - the pointer to the CK_VERSION structure
* @return the new Java CK_VERSION object
*/
jobject ckVersionPtrToJVersion(JNIEnv *env, const CK_VERSION_PTR ckpVersion)
{
jclass jVersionClass;
jmethodID jCtrId;
jobject jVersionObject;
jint jMajor;
jint jMinor;
/* load CK_VERSION class */
jVersionClass = (*env)->FindClass(env, CLASS_VERSION);
if (jVersionClass == NULL) { return NULL; }
/* load CK_VERSION constructor */
jCtrId = (*env)->GetMethodID(env, jVersionClass, "<init>", "(II)V");
if (jCtrId == NULL) { return NULL; }
/* prep both fields */
jMajor = ckpVersion->major;
jMinor = ckpVersion->minor;
/* create new CK_VERSION object */
jVersionObject =
(*env)->NewObject(env, jVersionClass, jCtrId, jMajor, jMinor);
if (jVersionObject == NULL) { return NULL; }
/* free local references */
(*env)->DeleteLocalRef(env, jVersionClass);
return jVersionObject ;
}
/*
* converts a CK_SESSION_INFO pointer into a Java CK_SESSION_INFO Object.
*
* @param env - used to call JNI funktions to create the new Java object
* @param ckpSessionInfo - the pointer to the CK_SESSION_INFO structure
* @return the new Java CK_SESSION_INFO object
*/
jobject ckSessionInfoPtrToJSessionInfo(JNIEnv *env, const CK_SESSION_INFO_PTR ckpSessionInfo)
{
jclass jSessionInfoClass;
jmethodID jCtrId;
jobject jSessionInfoObject;
jlong jSlotID;
jlong jState;
jlong jFlags;
jlong jDeviceError;
/* load CK_SESSION_INFO class */
jSessionInfoClass = (*env)->FindClass(env, CLASS_SESSION_INFO);
if (jSessionInfoClass == NULL) { return NULL; }
/* load CK_SESSION_INFO constructor */
jCtrId = (*env)->GetMethodID(env, jSessionInfoClass, "<init>", "(JJJJ)V");
if (jCtrId == NULL) { return NULL; }
/* prep all fields */
jSlotID = ckULongToJLong(ckpSessionInfo->slotID);
jState = ckULongToJLong(ckpSessionInfo->state);
jFlags = ckULongToJLong(ckpSessionInfo->flags);
jDeviceError = ckULongToJLong(ckpSessionInfo->ulDeviceError);
/* create new CK_SESSION_INFO object */
jSessionInfoObject =
(*env)->NewObject(env, jSessionInfoClass, jCtrId, jSlotID, jState,
jFlags, jDeviceError);
if (jSessionInfoObject == NULL) { return NULL; }
/* free local references */
(*env)->DeleteLocalRef(env, jSessionInfoClass);
return jSessionInfoObject ;
}
/*
* converts a CK_ATTRIBUTE pointer into a Java CK_ATTRIBUTE Object.
*
* @param env - used to call JNI funktions to create the new Java object
* @param ckpAttribute - the pointer to the CK_ATTRIBUTE structure
* @return the new Java CK_ATTRIBUTE object
*/
jobject ckAttributePtrToJAttribute(JNIEnv *env, const CK_ATTRIBUTE_PTR ckpAttribute)
{
jclass jAttributeClass;
jmethodID jCtrId;
jobject jAttributeObject;
jlong jType;
jobject jPValue = NULL;
jAttributeClass = (*env)->FindClass(env, CLASS_ATTRIBUTE);
if (jAttributeClass == NULL) { return NULL; }
/* load CK_INFO constructor */
jCtrId = (*env)->GetMethodID(env, jAttributeClass, "<init>", "(JLjava/lang/Object;)V");
if (jCtrId == NULL) { return NULL; }
/* prep both fields */
jType = ckULongToJLong(ckpAttribute->type);
jPValue = ckAttributeValueToJObject(env, ckpAttribute);
if ((*env)->ExceptionCheck(env)) { return NULL; }
/* create new CK_ATTRIBUTE object */
jAttributeObject =
(*env)->NewObject(env, jAttributeClass, jCtrId, jType, jPValue);
if (jAttributeObject == NULL) { return NULL; }
/* free local references */
(*env)->DeleteLocalRef(env, jAttributeClass);
(*env)->DeleteLocalRef(env, jPValue);
return jAttributeObject;
}
/*
* converts a Java CK_VERSION object into a CK_VERSION pointer
*
* @param env - used to call JNI funktions to get the values out of the Java object
* @param jVersion - the Java CK_VERSION object to convert
* @return pointer to the new CK_VERSION structure
*/
CK_VERSION_PTR
jVersionToCKVersionPtr(JNIEnv *env, jobject jVersion)
{
CK_VERSION_PTR ckpVersion;
jclass jVersionClass;
jfieldID jFieldID;
jbyte jMajor, jMinor;
if (jVersion == NULL) {
return NULL;
}
// retrieve java values
jVersionClass = (*env)->GetObjectClass(env, jVersion);
if (jVersionClass == NULL) { return NULL; }
jFieldID = (*env)->GetFieldID(env, jVersionClass, "major", "B");
if (jFieldID == NULL) { return NULL; }
jMajor = (*env)->GetByteField(env, jVersion, jFieldID);
jFieldID = (*env)->GetFieldID(env, jVersionClass, "minor", "B");
if (jFieldID == NULL) { return NULL; }
jMinor = (*env)->GetByteField(env, jVersion, jFieldID);
// allocate memory for CK_VERSION pointer
ckpVersion = (CK_VERSION_PTR) calloc(1, sizeof(CK_VERSION));
if (ckpVersion == NULL) {
throwOutOfMemoryError(env, 0);
return NULL;
}
// populate using java values
ckpVersion->major = jByteToCKByte(jMajor);
ckpVersion->minor = jByteToCKByte(jMinor);
return ckpVersion;
}
/*
* converts a Java CK_DATE object into a CK_DATE pointer
*
* @param env - used to call JNI functions to get the values out of the Java object
* @param jDate - the Java CK_DATE object to convert
* @return pointer to the new CK_DATE structure
*/
CK_DATE * jDateObjectToCKDatePtr(JNIEnv *env, jobject jDate)
{
CK_DATE * ckpDate = NULL;
CK_ULONG ckLength;
jclass jDateClass;
jfieldID jFieldID;
jobject jYear, jMonth, jDay;
jchar *jTempChars = NULL;
CK_ULONG i;
if (jDate == NULL) {
return NULL;
}
// retrieve java values
jDateClass = (*env)->FindClass(env, CLASS_DATE);
if (jDateClass == NULL) { return NULL; }
jFieldID = (*env)->GetFieldID(env, jDateClass, "year", "[C");
if (jFieldID == NULL) { return NULL; }
jYear = (*env)->GetObjectField(env, jDate, jFieldID);
jFieldID = (*env)->GetFieldID(env, jDateClass, "month", "[C");
if (jFieldID == NULL) { return NULL; }
jMonth = (*env)->GetObjectField(env, jDate, jFieldID);
jFieldID = (*env)->GetFieldID(env, jDateClass, "day", "[C");
if (jFieldID == NULL) { return NULL; }
jDay = (*env)->GetObjectField(env, jDate, jFieldID);
// allocate memory for CK_DATE pointer
ckpDate = (CK_DATE *) calloc(1, sizeof(CK_DATE));
if (ckpDate == NULL) {
throwOutOfMemoryError(env, 0);
return NULL;
}
// populate using java values
if (jYear == NULL) {
ckpDate->year[0] = 0;
ckpDate->year[1] = 0;
ckpDate->year[2] = 0;
ckpDate->year[3] = 0;
} else {
ckLength = (*env)->GetArrayLength(env, jYear);
jTempChars = (jchar*) calloc(ckLength, sizeof(jchar));
if (jTempChars == NULL) {
throwOutOfMemoryError(env, 0);
goto cleanup;
}
(*env)->GetCharArrayRegion(env, jYear, 0, ckLength, jTempChars);
if ((*env)->ExceptionCheck(env)) {
goto cleanup;
}
for (i = 0; (i < ckLength) && (i < 4) ; i++) {
ckpDate->year[i] = jCharToCKChar(jTempChars[i]);
}
free(jTempChars);
}
if (jMonth == NULL) {
ckpDate->month[0] = 0;
ckpDate->month[1] = 0;
} else {
ckLength = (*env)->GetArrayLength(env, jMonth);
jTempChars = (jchar*) calloc(ckLength, sizeof(jchar));
if (jTempChars == NULL) {
throwOutOfMemoryError(env, 0);
goto cleanup;
}
(*env)->GetCharArrayRegion(env, jMonth, 0, ckLength, jTempChars);
if ((*env)->ExceptionCheck(env)) {
goto cleanup;
}
for (i = 0; (i < ckLength) && (i < 2) ; i++) {
ckpDate->month[i] = jCharToCKChar(jTempChars[i]);
}
free(jTempChars);
}
if (jDay == NULL) {
ckpDate->day[0] = 0;
ckpDate->day[1] = 0;
} else {
ckLength = (*env)->GetArrayLength(env, jDay);
jTempChars = (jchar*) calloc(ckLength, sizeof(jchar));
if (jTempChars == NULL) {
throwOutOfMemoryError(env, 0);
goto cleanup;
}
(*env)->GetCharArrayRegion(env, jDay, 0, ckLength, jTempChars);
if ((*env)->ExceptionCheck(env)) {
goto cleanup;
}
for (i = 0; (i < ckLength) && (i < 2) ; i++) {
ckpDate->day[i] = jCharToCKChar(jTempChars[i]);
}
free(jTempChars);
}
return ckpDate;
cleanup:
free(jTempChars);
free(ckpDate);
return NULL;
}
/*
* converts a Java CK_ATTRIBUTE object into a CK_ATTRIBUTE structure
*
* @param env - used to call JNI funktions to get the values out of the Java object
* @param jAttribute - the Java CK_ATTRIBUTE object to convert
* @return the new CK_ATTRIBUTE structure
*/
CK_ATTRIBUTE jAttributeToCKAttribute(JNIEnv *env, jobject jAttribute)
{
CK_ATTRIBUTE ckAttribute;
jclass jAttributeClass;
jfieldID jFieldID;
jlong jType;
jobject jPValue;
memset(&ckAttribute, 0, sizeof(CK_ATTRIBUTE));
// TBD: what if jAttribute == NULL?!
TRACE0("\nDEBUG: jAttributeToCKAttribute");
/* get CK_ATTRIBUTE class */
TRACE0(", getting attribute object class");
jAttributeClass = (*env)->GetObjectClass(env, jAttribute);
if (jAttributeClass == NULL) { return ckAttribute; }
/* get type */
TRACE0(", getting type field");
jFieldID = (*env)->GetFieldID(env, jAttributeClass, "type", "J");
if (jFieldID == NULL) { return ckAttribute; }
jType = (*env)->GetLongField(env, jAttribute, jFieldID);
TRACE1(", type=0x%lX", jType);
/* get pValue */
TRACE0(", getting pValue field");
jFieldID = (*env)->GetFieldID(env, jAttributeClass, "pValue", "Ljava/lang/Object;");
if (jFieldID == NULL) { return ckAttribute; }
jPValue = (*env)->GetObjectField(env, jAttribute, jFieldID);
TRACE1(", pValue=%p", jPValue);
ckAttribute.type = jLongToCKULong(jType);
TRACE0(", converting pValue to primitive object");
/* convert the Java pValue object to a CK-type pValue pointer */
ckAttribute.pValue = jObjectToPrimitiveCKObjectPtr(env, jPValue, &(ckAttribute.ulValueLen));
TRACE0("\nDEBUG: jAttributeToCKAttribute FINISHED\n");
return ckAttribute ;
}
void masterKeyDeriveParamToCKMasterKeyDeriveParam(JNIEnv *env, jobject jParam,
jclass masterKeyDeriveParamClass,
CK_VERSION_PTR* cKMasterKeyDeriveParamVersion,
CK_SSL3_RANDOM_DATA* cKMasterKeyDeriveParamRandomInfo) {
jfieldID fieldID;
jclass jSsl3RandomDataClass;
jobject jRandomInfo, jRIClientRandom, jRIServerRandom, jVersion;
// retrieve java values
fieldID = (*env)->GetFieldID(env, masterKeyDeriveParamClass, "RandomInfo",
"Lsun/security/pkcs11/wrapper/CK_SSL3_RANDOM_DATA;");
if (fieldID == NULL) { return; }
jRandomInfo = (*env)->GetObjectField(env, jParam, fieldID);
jSsl3RandomDataClass = (*env)->FindClass(env, CLASS_SSL3_RANDOM_DATA);
if (jSsl3RandomDataClass == NULL) { return; }
fieldID = (*env)->GetFieldID(env, jSsl3RandomDataClass, "pClientRandom", "[B");
if (fieldID == NULL) { return; }
jRIClientRandom = (*env)->GetObjectField(env, jRandomInfo, fieldID);
fieldID = (*env)->GetFieldID(env, jSsl3RandomDataClass, "pServerRandom", "[B");
if (fieldID == NULL) { return; }
jRIServerRandom = (*env)->GetObjectField(env, jRandomInfo, fieldID);
fieldID = (*env)->GetFieldID(env, masterKeyDeriveParamClass, "pVersion",
"Lsun/security/pkcs11/wrapper/CK_VERSION;");
if (fieldID == NULL) { return; }
jVersion = (*env)->GetObjectField(env, jParam, fieldID);
// populate using java values
*cKMasterKeyDeriveParamVersion = jVersionToCKVersionPtr(env, jVersion);
if ((*env)->ExceptionCheck(env)) { return; }
jByteArrayToCKByteArray(env, jRIClientRandom,
&(cKMasterKeyDeriveParamRandomInfo->pClientRandom),
&(cKMasterKeyDeriveParamRandomInfo->ulClientRandomLen));
if ((*env)->ExceptionCheck(env)) {
goto cleanup;
}
jByteArrayToCKByteArray(env, jRIServerRandom,
&(cKMasterKeyDeriveParamRandomInfo->pServerRandom),
&(cKMasterKeyDeriveParamRandomInfo->ulServerRandomLen));
if ((*env)->ExceptionCheck(env)) {
goto cleanup;
}
return;
cleanup:
free(*cKMasterKeyDeriveParamVersion);
free(cKMasterKeyDeriveParamRandomInfo->pClientRandom);
cKMasterKeyDeriveParamRandomInfo->ulClientRandomLen = 0L;
free(cKMasterKeyDeriveParamRandomInfo->pServerRandom);
cKMasterKeyDeriveParamRandomInfo->ulServerRandomLen = 0L;
// explicitly set to NULL to ensure no double free possible
*cKMasterKeyDeriveParamVersion = NULL;
cKMasterKeyDeriveParamRandomInfo->pClientRandom = NULL;
cKMasterKeyDeriveParamRandomInfo->pServerRandom = NULL;
}
/*
* converts the Java CK_SSL3_MASTER_KEY_DERIVE_PARAMS object to a
* CK_SSL3_MASTER_KEY_DERIVE_PARAMS pointer
*
* @param env - used to call JNI functions to get the Java classes and objects
* @param jParam - the Java CK_SSL3_MASTER_KEY_DERIVE_PARAMS object to convert
* @param pLength - length of the allocated memory of the returned pointer
* @return pointer to the new CK_SSL3_MASTER_KEY_DERIVE_PARAMS structure
*/
CK_SSL3_MASTER_KEY_DERIVE_PARAMS_PTR
jSsl3MasterKeyDeriveParamToCKSsl3MasterKeyDeriveParamPtr(JNIEnv *env,
jobject jParam, CK_ULONG *pLength)
{
CK_SSL3_MASTER_KEY_DERIVE_PARAMS_PTR ckParamPtr;
jclass jSsl3MasterKeyDeriveParamsClass;
if (pLength != NULL) {
*pLength = 0L;
}
// allocate memory for CK_SSL3_MASTER_KEY_DERIVE_PARAMS pointer
ckParamPtr = calloc(1, sizeof(CK_SSL3_MASTER_KEY_DERIVE_PARAMS));
if (ckParamPtr == NULL) {
throwOutOfMemoryError(env, 0);
return NULL;
}
// retrieve and populate using java values
jSsl3MasterKeyDeriveParamsClass =
(*env)->FindClass(env, CLASS_SSL3_MASTER_KEY_DERIVE_PARAMS);
if (jSsl3MasterKeyDeriveParamsClass == NULL) {
goto cleanup;
}
masterKeyDeriveParamToCKMasterKeyDeriveParam(env, jParam,
jSsl3MasterKeyDeriveParamsClass,
&(ckParamPtr->pVersion), &(ckParamPtr->RandomInfo));
if ((*env)->ExceptionCheck(env)) {
goto cleanup;
}
if (pLength != NULL) {
*pLength = sizeof(CK_SSL3_MASTER_KEY_DERIVE_PARAMS);
}
return ckParamPtr;
cleanup:
free(ckParamPtr);
return NULL;
}
/*
* converts the Java CK_TLS12_MASTER_KEY_DERIVE_PARAMS object to a
* CK_TLS12_MASTER_KEY_DERIVE_PARAMS pointer
*
* @param env - used to call JNI functions to get the Java classes and objects
* @param jParam - the Java CK_TLS12_MASTER_KEY_DERIVE_PARAMS object to convert
* @param pLength - length of the allocated memory of the returned pointer
* @return pointer to the new CK_TLS12_MASTER_KEY_DERIVE_PARAMS structure
*/
CK_TLS12_MASTER_KEY_DERIVE_PARAMS_PTR
jTls12MasterKeyDeriveParamToCKTls12MasterKeyDeriveParamPtr(JNIEnv *env,
jobject jParam, CK_ULONG *pLength)
{
CK_TLS12_MASTER_KEY_DERIVE_PARAMS_PTR ckParamPtr;
jclass jTls12MasterKeyDeriveParamsClass;
jfieldID fieldID;
jlong prfHashMechanism;
if (pLength != NULL) {
*pLength = 0L;
}
// retrieve java values
jTls12MasterKeyDeriveParamsClass =
(*env)->FindClass(env, CLASS_TLS12_MASTER_KEY_DERIVE_PARAMS);
if (jTls12MasterKeyDeriveParamsClass == NULL) { return NULL; }
fieldID = (*env)->GetFieldID(env,
jTls12MasterKeyDeriveParamsClass, "prfHashMechanism", "J");
if (fieldID == NULL) { return NULL; }
prfHashMechanism = (*env)->GetLongField(env, jParam, fieldID);
// allocate memory for CK_TLS12_MASTER_KEY_DERIVE_PARAMS pointer
ckParamPtr = calloc(1, sizeof(CK_TLS12_MASTER_KEY_DERIVE_PARAMS));
if (ckParamPtr == NULL) {
throwOutOfMemoryError(env, 0);
return NULL;
}
// populate using java values
masterKeyDeriveParamToCKMasterKeyDeriveParam(env, jParam,
jTls12MasterKeyDeriveParamsClass, &ckParamPtr->pVersion,
&ckParamPtr->RandomInfo);
if ((*env)->ExceptionCheck(env)) {
goto cleanup;
}
ckParamPtr->prfHashMechanism = (CK_MECHANISM_TYPE) prfHashMechanism;
if (pLength != NULL) {
*pLength = sizeof(CK_TLS12_MASTER_KEY_DERIVE_PARAMS);
}
return ckParamPtr;
cleanup:
free(ckParamPtr);
return NULL;
}
/*
* converts the Java CK_TLS_PRF_PARAMS object to a CK_TLS_PRF_PARAMS pointer
*
* @param env - used to call JNI functions to get the Java classes and objects
* @param jParam - the Java CK_TLS_PRF_PARAMS object to convert
* @param pLength - length of the allocated memory of the returned pointer
* @return pointer to the new CK_TLS_PRF_PARAMS structure
*/
CK_TLS_PRF_PARAMS_PTR
jTlsPrfParamsToCKTlsPrfParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
{
CK_TLS_PRF_PARAMS_PTR ckParamPtr;
jclass jTlsPrfParamsClass;
jfieldID fieldID;
jobject jSeed, jLabel, jOutput;
if (pLength != NULL) {
*pLength = 0;
}
// retrieve java values
jTlsPrfParamsClass = (*env)->FindClass(env, CLASS_TLS_PRF_PARAMS);
if (jTlsPrfParamsClass == NULL) { return NULL; }
fieldID = (*env)->GetFieldID(env, jTlsPrfParamsClass, "pSeed", "[B");
if (fieldID == NULL) { return NULL; }
jSeed = (*env)->GetObjectField(env, jParam, fieldID);
fieldID = (*env)->GetFieldID(env, jTlsPrfParamsClass, "pLabel", "[B");
if (fieldID == NULL) { return NULL; }
jLabel = (*env)->GetObjectField(env, jParam, fieldID);
fieldID = (*env)->GetFieldID(env, jTlsPrfParamsClass, "pOutput", "[B");
if (fieldID == NULL) { return NULL; }
jOutput = (*env)->GetObjectField(env, jParam, fieldID);
// allocate memory for CK_TLS_PRF_PARAMS pointer
ckParamPtr = calloc(1, sizeof(CK_TLS_PRF_PARAMS));
if (ckParamPtr == NULL) {
throwOutOfMemoryError(env, 0);
return NULL;
}
// populate using java values
jByteArrayToCKByteArray(env, jSeed, &(ckParamPtr->pSeed), &(ckParamPtr->ulSeedLen));
if ((*env)->ExceptionCheck(env)) {
goto cleanup;
}
jByteArrayToCKByteArray(env, jLabel, &(ckParamPtr->pLabel), &(ckParamPtr->ulLabelLen));
if ((*env)->ExceptionCheck(env)) {
goto cleanup;
}
ckParamPtr->pulOutputLen = calloc(1, sizeof(CK_ULONG));
if (ckParamPtr->pulOutputLen == NULL) {
goto cleanup;
}
jByteArrayToCKByteArray(env, jOutput, &(ckParamPtr->pOutput), ckParamPtr->pulOutputLen);
if ((*env)->ExceptionCheck(env)) {
goto cleanup;
}
if (pLength != NULL) {
*pLength = sizeof(CK_TLS_PRF_PARAMS);
}
return ckParamPtr;
cleanup:
free(ckParamPtr->pSeed);
free(ckParamPtr->pLabel);
free(ckParamPtr->pOutput);
free(ckParamPtr->pulOutputLen);
free(ckParamPtr);
return NULL;
}
/*
* converts the Java CK_TLS_MAC_PARAMS object to a CK_TLS_MAC_PARAMS pointer
*
* @param env - used to call JNI functions to get the Java classes and objects
* @param jParam - the Java CK_TLS_MAC_PARAMS object to convert
* @param pLength - length of the allocated memory of the returned pointer
* @return pointer to the new CK_TLS_MAC_PARAMS structure
*/
CK_TLS_MAC_PARAMS_PTR
jTlsMacParamsToCKTlsMacParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
{
CK_TLS_MAC_PARAMS_PTR ckParamPtr;
jclass jTlsMacParamsClass;
jfieldID fieldID;
jlong jPrfMechanism, jUlMacLength, jUlServerOrClient;
if (pLength != NULL) {
*pLength = 0L;
}
// retrieve java values
jTlsMacParamsClass = (*env)->FindClass(env, CLASS_TLS_MAC_PARAMS);
if (jTlsMacParamsClass == NULL) { return NULL; }
fieldID = (*env)->GetFieldID(env, jTlsMacParamsClass, "prfMechanism", "J");
if (fieldID == NULL) { return NULL; }
jPrfMechanism = (*env)->GetLongField(env, jParam, fieldID);
fieldID = (*env)->GetFieldID(env, jTlsMacParamsClass, "ulMacLength", "J");
if (fieldID == NULL) { return NULL; }
jUlMacLength = (*env)->GetLongField(env, jParam, fieldID);
fieldID = (*env)->GetFieldID(env, jTlsMacParamsClass, "ulServerOrClient", "J");
if (fieldID == NULL) { return NULL; }
jUlServerOrClient = (*env)->GetLongField(env, jParam, fieldID);
// allocate memory for CK_TLS_MAC_PARAMS pointer
ckParamPtr = calloc(1, sizeof(CK_TLS_MAC_PARAMS));
if (ckParamPtr == NULL) {
throwOutOfMemoryError(env, 0);
return NULL;
}
// populate using java values
ckParamPtr->prfHashMechanism = jLongToCKULong(jPrfMechanism);
ckParamPtr->ulMacLength = jLongToCKULong(jUlMacLength);
ckParamPtr->ulServerOrClient = jLongToCKULong(jUlServerOrClient);
if (pLength != NULL) {
*pLength = sizeof(CK_TLS_MAC_PARAMS);
}
return ckParamPtr;
}
void keyMatParamToCKKeyMatParam(JNIEnv *env, jobject jParam,
jclass jKeyMatParamClass,
CK_ULONG* cKKeyMatParamUlMacSizeInBits,
CK_ULONG* cKKeyMatParamUlKeySizeInBits,
CK_ULONG* cKKeyMatParamUlIVSizeInBits,
CK_BBOOL* cKKeyMatParamBIsExport,
CK_SSL3_RANDOM_DATA* cKKeyMatParamRandomInfo,
CK_SSL3_KEY_MAT_OUT_PTR* cKKeyMatParamPReturnedKeyMaterial)
{
jclass jSsl3RandomDataClass, jSsl3KeyMatOutClass;
jfieldID fieldID;
jlong jMacSizeInBits, jKeySizeInBits, jIVSizeInBits;
jboolean jIsExport;
jobject jRandomInfo, jRIClientRandom, jRIServerRandom;
jobject jReturnedKeyMaterial, jRMIvClient, jRMIvServer;
CK_ULONG ckTemp;
// the pointer arguments should already be initialized by caller
// retrieve java values
fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "ulMacSizeInBits", "J");
if (fieldID == NULL) { return; }
jMacSizeInBits = (*env)->GetLongField(env, jParam, fieldID);
fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "ulKeySizeInBits", "J");
if (fieldID == NULL) { return; }
jKeySizeInBits = (*env)->GetLongField(env, jParam, fieldID);
fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "ulIVSizeInBits", "J");
if (fieldID == NULL) { return; }
jIVSizeInBits = (*env)->GetLongField(env, jParam, fieldID);
fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "bIsExport", "Z");
if (fieldID == NULL) { return; }
jIsExport = (*env)->GetBooleanField(env, jParam, fieldID);
jSsl3RandomDataClass = (*env)->FindClass(env, CLASS_SSL3_RANDOM_DATA);
if (jSsl3RandomDataClass == NULL) { return; }
fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "RandomInfo",
"Lsun/security/pkcs11/wrapper/CK_SSL3_RANDOM_DATA;");
if (fieldID == NULL) { return; }
jRandomInfo = (*env)->GetObjectField(env, jParam, fieldID);
fieldID = (*env)->GetFieldID(env, jSsl3RandomDataClass, "pClientRandom", "[B");
if (fieldID == NULL) { return; }
jRIClientRandom = (*env)->GetObjectField(env, jRandomInfo, fieldID);
fieldID = (*env)->GetFieldID(env, jSsl3RandomDataClass, "pServerRandom", "[B");
if (fieldID == NULL) { return; }
jRIServerRandom = (*env)->GetObjectField(env, jRandomInfo, fieldID);
jSsl3KeyMatOutClass = (*env)->FindClass(env, CLASS_SSL3_KEY_MAT_OUT);
if (jSsl3KeyMatOutClass == NULL) { return; }
fieldID = (*env)->GetFieldID(env, jKeyMatParamClass, "pReturnedKeyMaterial",
"Lsun/security/pkcs11/wrapper/CK_SSL3_KEY_MAT_OUT;");
if (fieldID == NULL) { return; }
jReturnedKeyMaterial = (*env)->GetObjectField(env, jParam, fieldID);
fieldID = (*env)->GetFieldID(env, jSsl3KeyMatOutClass, "pIVClient", "[B");
if (fieldID == NULL) { return; }
jRMIvClient = (*env)->GetObjectField(env, jReturnedKeyMaterial, fieldID);
fieldID = (*env)->GetFieldID(env, jSsl3KeyMatOutClass, "pIVServer", "[B");
if (fieldID == NULL) { return; }
jRMIvServer = (*env)->GetObjectField(env, jReturnedKeyMaterial, fieldID);
// populate the specified pointers using java values
*cKKeyMatParamUlMacSizeInBits = jLongToCKULong(jMacSizeInBits);
*cKKeyMatParamUlKeySizeInBits = jLongToCKULong(jKeySizeInBits);
*cKKeyMatParamUlIVSizeInBits = jLongToCKULong(jIVSizeInBits);
*cKKeyMatParamBIsExport = jBooleanToCKBBool(jIsExport);
jByteArrayToCKByteArray(env, jRIClientRandom,
&(cKKeyMatParamRandomInfo->pClientRandom),
&(cKKeyMatParamRandomInfo->ulClientRandomLen));
if ((*env)->ExceptionCheck(env)) {
// just return as no memory allocation yet
return;
}
jByteArrayToCKByteArray(env, jRIServerRandom,
&(cKKeyMatParamRandomInfo->pServerRandom),
&(cKKeyMatParamRandomInfo->ulServerRandomLen));
if ((*env)->ExceptionCheck(env)) {
goto cleanup;
}
/* allocate memory for pReturnedKeyMaterial */
*cKKeyMatParamPReturnedKeyMaterial =
(CK_SSL3_KEY_MAT_OUT_PTR) calloc(1, sizeof(CK_SSL3_KEY_MAT_OUT));
if (*cKKeyMatParamPReturnedKeyMaterial == NULL) {
throwOutOfMemoryError(env, 0);
goto cleanup;
}
// the handles are output params only, no need to fetch them from Java
(*cKKeyMatParamPReturnedKeyMaterial)->hClientMacSecret = 0;
(*cKKeyMatParamPReturnedKeyMaterial)->hServerMacSecret = 0;
(*cKKeyMatParamPReturnedKeyMaterial)->hClientKey = 0;
(*cKKeyMatParamPReturnedKeyMaterial)->hServerKey = 0;
jByteArrayToCKByteArray(env, jRMIvClient,
&((*cKKeyMatParamPReturnedKeyMaterial)->pIVClient), &ckTemp);
if ((*env)->ExceptionCheck(env)) {
goto cleanup;
}
jByteArrayToCKByteArray(env, jRMIvServer,
&((*cKKeyMatParamPReturnedKeyMaterial)->pIVServer), &ckTemp);
if ((*env)->ExceptionCheck(env)) {
goto cleanup;
}
return;
cleanup:
free(cKKeyMatParamRandomInfo->pClientRandom);
free(cKKeyMatParamRandomInfo->pServerRandom);
if ((*cKKeyMatParamPReturnedKeyMaterial) != NULL) {
free((*cKKeyMatParamPReturnedKeyMaterial)->pIVClient);
free(*cKKeyMatParamPReturnedKeyMaterial);
}
// explicitly set to NULL to ensure no double free possible
cKKeyMatParamRandomInfo->pClientRandom = NULL;
cKKeyMatParamRandomInfo->pServerRandom = NULL;
*cKKeyMatParamPReturnedKeyMaterial = NULL;
return;
}
/*
* converts the Java CK_SSL3_KEY_MAT_PARAMS object to a
* CK_SSL3_KEY_MAT_PARAMS pointer
*
* @param env - used to call JNI funktions to get the Java classes and objects
* @param jParam - the Java CK_SSL3_KEY_MAT_PARAMS object to convert
* @param pLength - length of the allocated memory of the returned pointer
* @return pointer to the new CK_SSL3_KEY_MAT_PARAMS structure
*/
CK_SSL3_KEY_MAT_PARAMS_PTR
jSsl3KeyMatParamToCKSsl3KeyMatParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
{
CK_SSL3_KEY_MAT_PARAMS_PTR ckParamPtr;
jclass jSsl3KeyMatParamsClass;
if (pLength != NULL) {
*pLength = 0;
}
// allocate memory for CK_SSL3_KEY_MAT_PARAMS pointer
ckParamPtr = calloc(1, sizeof(CK_SSL3_KEY_MAT_PARAMS));
if (ckParamPtr == NULL) {
throwOutOfMemoryError(env, 0);
return NULL;
}
// retrieve and populate using java values
jSsl3KeyMatParamsClass = (*env)->FindClass(env,
CLASS_SSL3_KEY_MAT_PARAMS);
if (jSsl3KeyMatParamsClass == NULL) {
goto cleanup;
}
keyMatParamToCKKeyMatParam(env, jParam, jSsl3KeyMatParamsClass,
&(ckParamPtr->ulMacSizeInBits), &(ckParamPtr->ulKeySizeInBits),
&(ckParamPtr->ulIVSizeInBits), &(ckParamPtr->bIsExport),
&(ckParamPtr->RandomInfo), &(ckParamPtr->pReturnedKeyMaterial));
if ((*env)->ExceptionCheck(env)) {
goto cleanup;
}
if (pLength != NULL) {
*pLength = sizeof(CK_SSL3_KEY_MAT_PARAMS);
}
return ckParamPtr;
cleanup:
free(ckParamPtr);
return NULL;
}
/*
* converts the Java CK_TLS12_KEY_MAT_PARAMS object to a
* CK_TLS12_KEY_MAT_PARAMS pointer
*
* @param env - used to call JNI functions to get the Java classes and objects
* @param jParam - the Java CK_TLS12_KEY_MAT_PARAMS object to convert
* @param pLength - length of the allocated memory of the returned pointer
* @return pointer to the new CK_TLS12_KEY_MAT_PARAMS structure
*/
CK_TLS12_KEY_MAT_PARAMS_PTR jTls12KeyMatParamToCKTls12KeyMatParamPtr(JNIEnv *env,
jobject jParam, CK_ULONG *pLength)
{
CK_TLS12_KEY_MAT_PARAMS_PTR ckParamPtr;
jclass jTls12KeyMatParamsClass;
jfieldID fieldID;
jlong prfHashMechanism;
if (pLength != NULL) {
*pLength = 0;
}
// retrieve java values
jTls12KeyMatParamsClass = (*env)->FindClass(env,
CLASS_TLS12_KEY_MAT_PARAMS);
if (jTls12KeyMatParamsClass == NULL) { return NULL; }
fieldID = (*env)->GetFieldID(env, jTls12KeyMatParamsClass,
"prfHashMechanism", "J");
if (fieldID == NULL) { return NULL; }
prfHashMechanism = (*env)->GetLongField(env, jParam, fieldID);
// allocate memory for CK_TLS12_KEY_MAT_PARAMS pointer
ckParamPtr = calloc(1, sizeof(CK_TLS12_KEY_MAT_PARAMS));
if (ckParamPtr == NULL) {
throwOutOfMemoryError(env, 0);
return NULL;
}
// populate using java values
keyMatParamToCKKeyMatParam(env, jParam, jTls12KeyMatParamsClass,
&(ckParamPtr->ulMacSizeInBits), &(ckParamPtr->ulKeySizeInBits),
&(ckParamPtr->ulIVSizeInBits), &(ckParamPtr->bIsExport),
&(ckParamPtr->RandomInfo), &(ckParamPtr->pReturnedKeyMaterial));
if ((*env)->ExceptionCheck(env)) {
goto cleanup;
}
ckParamPtr->prfHashMechanism = (CK_MECHANISM_TYPE)prfHashMechanism;
if (pLength != NULL) {
*pLength = sizeof(CK_TLS12_KEY_MAT_PARAMS);
}
return ckParamPtr;
cleanup:
free(ckParamPtr);
return NULL;
}
/*
* converts the Java CK_AES_CTR_PARAMS object to a CK_AES_CTR_PARAMS pointer
*
* @param env - used to call JNI funktions to get the Java classes and objects
* @param jParam - the Java CK_AES_CTR_PARAMS object to convert
* @param pLength - length of the allocated memory of the returned pointer
* @return pointer to the new CK_AES_CTR_PARAMS structure
*/
CK_AES_CTR_PARAMS_PTR
jAesCtrParamsToCKAesCtrParamPtr(JNIEnv *env, jobject jParam, CK_ULONG *pLength)
{
CK_AES_CTR_PARAMS_PTR ckParamPtr;
jclass jAesCtrParamsClass;
jfieldID fieldID;
jlong jCounterBits;
jobject jCb;
CK_BYTE_PTR ckBytes = NULL;
CK_ULONG ckTemp;
if (pLength != NULL) {
*pLength = 0L;
}
// retrieve java values
jAesCtrParamsClass = (*env)->FindClass(env, CLASS_AES_CTR_PARAMS);
if (jAesCtrParamsClass == NULL) { return NULL; }
if (!(*env)->IsInstanceOf(env, jParam, jAesCtrParamsClass)) {
return NULL;
}
fieldID = (*env)->GetFieldID(env, jAesCtrParamsClass, "ulCounterBits", "J");
if (fieldID == NULL) { return NULL; }
jCounterBits = (*env)->GetLongField(env, jParam, fieldID);
fieldID = (*env)->GetFieldID(env, jAesCtrParamsClass, "cb", "[B");
if (fieldID == NULL) { return NULL; }
jCb = (*env)->GetObjectField(env, jParam, fieldID);
// allocate memory for CK_AES_CTR_PARAMS pointer
ckParamPtr = calloc(1, sizeof(CK_AES_CTR_PARAMS));
if (ckParamPtr == NULL) {
throwOutOfMemoryError(env, 0);
return NULL;
}
// populate using java values
jByteArrayToCKByteArray(env, jCb, &ckBytes, &ckTemp);
if ((*env)->ExceptionCheck(env) || ckTemp != 16) {
goto cleanup;
}