/
PKCS11.java
2009 lines (1772 loc) · 78.9 KB
/
PKCS11.java
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, 2024, 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.
*/
package sun.security.pkcs11.wrapper;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
import sun.security.util.Debug;
import sun.security.pkcs11.P11Util;
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
import static sun.security.pkcs11.wrapper.PKCS11Exception.RV.*;
/**
* This is the default implementation of the PKCS11 interface. IT connects to
* the pkcs11wrapper.dll file, which is the native part of this library.
* The strange and awkward looking initialization was chosen to avoid calling
* loadLibrary from a static initialization block, because this would complicate
* the use in applets.
*
* @author Karl Scheibelhofer <Karl.Scheibelhofer@iaik.at>
* @author Martin Schlaeffer <schlaeff@sbox.tugraz.at>
* @invariants (pkcs11ModulePath_ <> null)
*/
public class PKCS11 {
/**
* The name of the native part of the wrapper; i.e. the filename without
* the extension (e.g. ".DLL" or ".so").
*/
private static final String PKCS11_WRAPPER = "j2pkcs11";
static {
// cannot use LoadLibraryAction because that would make the native
// library available to the bootclassloader, but we run in the
// extension classloader.
@SuppressWarnings("removal")
var dummy = AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
System.loadLibrary(PKCS11_WRAPPER);
return null;
}
});
boolean enableDebug = Debug.getInstance("sunpkcs11") != null;
initializeLibrary(enableDebug);
}
public static void loadNative() {
// dummy method that can be called to make sure the native
// portion has been loaded. actual loading happens in the
// static initializer, hence this method is empty.
}
/**
* The PKCS#11 module to connect to. This is the PKCS#11 driver of the token;
* e.g. pk2priv.dll.
*/
private final String pkcs11ModulePath;
private final CK_VERSION version;
// Note: Please don't update this field other than the constructor.
// Otherwise, the native data is not able to be collected.
private long pNativeData;
/**
* This method does the initialization of the native library. It is called
* exactly once for this class.
*
* @preconditions
* @postconditions
*/
private static native void initializeLibrary(boolean debug);
// XXX
/**
* This method does the finalization of the native library. It is called
* exactly once for this class. The library uses this method for a clean-up
* of any resources.
*
* @preconditions
* @postconditions
*/
private static native void finalizeLibrary();
private static final Map<String,PKCS11> moduleMap =
new HashMap<String,PKCS11>();
/**
* Connects to the PKCS#11 driver given. The filename must contain the
* path, if the driver is not in the system's search path.
*
* @param pkcs11ModulePath the PKCS#11 library path
* @param functionList the method name for retrieving the PKCS#11
* function list; may be null if not set in config file
* @preconditions (pkcs11ModulePath <> null)
* @postconditions
*/
PKCS11(String pkcs11ModulePath, String functionList)
throws IOException {
this.version = connect(pkcs11ModulePath, functionList);
this.pkcs11ModulePath = pkcs11ModulePath;
// bug in native PKCS#11 lib; workaround it by calling C_GetInfo()
// and get cryptoki version from there
if (this.version.major != 2 && this.version.major != 3) {
try {
CK_INFO p11Info = C_GetInfo();
this.version.major = p11Info.cryptokiVersion.major;
this.version.minor = p11Info.cryptokiVersion.minor;
} catch (PKCS11Exception e) {
// give up; just use what is returned by connect()
}
}
// Calls disconnect() to cleanup the native part of the wrapper.
P11Util.cleaner.register(this, releaserFor(pNativeData));
}
public CK_VERSION getVersion() {
return version;
}
public static synchronized PKCS11 getInstance(String pkcs11ModulePath,
String functionList, CK_C_INITIALIZE_ARGS pInitArgs,
boolean omitInitialize) throws IOException, PKCS11Exception {
// we may only call C_Initialize once per native .so/.dll
// so keep a cache using the (non-canonicalized!) path
PKCS11 pkcs11 = moduleMap.get(pkcs11ModulePath);
if (pkcs11 == null) {
if ((pInitArgs != null)
&& ((pInitArgs.flags & CKF_OS_LOCKING_OK) != 0)) {
pkcs11 = new PKCS11(pkcs11ModulePath, functionList);
} else {
pkcs11 = new SynchronizedPKCS11(pkcs11ModulePath, functionList);
}
if (omitInitialize == false) {
try {
pkcs11.C_Initialize(pInitArgs);
} catch (PKCS11Exception e) {
// ignore already-initialized error code
// rethrow all other errors
if (!e.match(CKR_CRYPTOKI_ALREADY_INITIALIZED)) {
throw e;
}
}
}
moduleMap.put(pkcs11ModulePath, pkcs11);
}
return pkcs11;
}
private static Runnable releaserFor(long pNativeData) {
return () -> {
if (pNativeData != 0) {
PKCS11.disconnect(pNativeData);
}
};
}
/**
* Connects this object to the specified PKCS#11 library. This method is for
* internal use only.
* Declared private, because incorrect handling may result in errors in the
* native part. Please don't use this method other than the constructor.
*
* @param pkcs11ModulePath The PKCS#11 library path.
* @param functionList the method name for retrieving the PKCS#11
* function list; may be null if not set in config file
* @return the actual PKCS11 interface version
* @preconditions (pkcs11ModulePath <> null)
* @postconditions
*/
private native CK_VERSION connect(String pkcs11ModulePath,
String functionList) throws IOException;
/**
* Disconnects the PKCS#11 library from this object. After calling this
* method, this object is no longer connected to a native PKCS#11 module
* and any subsequent calls to C_ methods will fail. This method is for
* internal use only. Please don't use this method other than finalization
* as implemented in the releaserFor() method.
*
* Declared private, because incorrect handling may result in errors in the
* native part.
*
* @preconditions
* @postconditions
*/
private static native void disconnect(long pNativeData);
// Implementation of PKCS11 methods delegated to native pkcs11wrapper library
/* *****************************************************************************
* General-purpose
******************************************************************************/
/**
* C_Initialize initializes the Cryptoki library.
* (General-purpose)
*
* @param pInitArgs if pInitArgs is not NULL it gets casted to
* CK_C_INITIALIZE_ARGS_PTR and dereferenced
* (PKCS#11 param: CK_VOID_PTR pInitArgs)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
native void C_Initialize(Object pInitArgs) throws PKCS11Exception;
/**
* C_Finalize indicates that an application is done with the
* Cryptoki library
* (General-purpose)
*
* @param pReserved is reserved. Should be NULL_PTR
* (PKCS#11 param: CK_VOID_PTR pReserved)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions (pReserved == null)
* @postconditions
*/
public native void C_Finalize(Object pReserved) throws PKCS11Exception;
/**
* C_GetInfo returns general information about Cryptoki.
* (General-purpose)
*
* @return the information.
* (PKCS#11 param: CK_INFO_PTR pInfo)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions (result <> null)
*/
public native CK_INFO C_GetInfo() throws PKCS11Exception;
/* *****************************************************************************
* Slot and token management
******************************************************************************/
/**
* C_GetSlotList obtains a list of slots in the system.
* (Slot and token management)
*
* @param tokenPresent if true only Slot IDs with a token are returned
* (PKCS#11 param: CK_BBOOL tokenPresent)
* @return a long array of slot IDs and number of Slot IDs
* (PKCS#11 param: CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions (result <> null)
*/
public native long[] C_GetSlotList(boolean tokenPresent)
throws PKCS11Exception;
/**
* C_GetSlotInfo obtains information about a particular slot in
* the system.
* (Slot and token management)
*
* @param slotID the ID of the slot
* (PKCS#11 param: CK_SLOT_ID slotID)
* @return the slot information
* (PKCS#11 param: CK_SLOT_INFO_PTR pInfo)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions (result <> null)
*/
public native CK_SLOT_INFO C_GetSlotInfo(long slotID) throws PKCS11Exception;
/**
* C_GetTokenInfo obtains information about a particular token
* in the system.
* (Slot and token management)
*
* @param slotID ID of the token's slot
* (PKCS#11 param: CK_SLOT_ID slotID)
* @return the token information
* (PKCS#11 param: CK_TOKEN_INFO_PTR pInfo)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions (result <> null)
*/
public native CK_TOKEN_INFO C_GetTokenInfo(long slotID)
throws PKCS11Exception;
/**
* C_GetMechanismList obtains a list of mechanism types
* supported by a token.
* (Slot and token management)
*
* @param slotID ID of the token's slot
* (PKCS#11 param: CK_SLOT_ID slotID)
* @return a long array of mechanism types and number of mechanism types
* (PKCS#11 param: CK_MECHANISM_TYPE_PTR pMechanismList,
* CK_ULONG_PTR pulCount)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions (result <> null)
*/
public native long[] C_GetMechanismList(long slotID) throws PKCS11Exception;
/**
* C_GetMechanismInfo obtains information about a particular
* mechanism possibly supported by a token.
* (Slot and token management)
*
* @param slotID ID of the token's slot
* (PKCS#11 param: CK_SLOT_ID slotID)
* @param type type of mechanism
* (PKCS#11 param: CK_MECHANISM_TYPE type)
* @return the mechanism info
* (PKCS#11 param: CK_MECHANISM_INFO_PTR pInfo)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions (result <> null)
*/
public native CK_MECHANISM_INFO C_GetMechanismInfo(long slotID, long type)
throws PKCS11Exception;
/**
* C_InitToken initializes a token.
* (Slot and token management)
*
* @param slotID ID of the token's slot
* (PKCS#11 param: CK_SLOT_ID slotID)
* @param pPin the SO's initial PIN and the length in bytes of the PIN
* (PKCS#11 param: CK_CHAR_PTR pPin, CK_ULONG ulPinLen)
* @param pLabel 32-byte token label (blank padded)
* (PKCS#11 param: CK_UTF8CHAR_PTR pLabel)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
// public native void C_InitToken(long slotID, char[] pPin, char[] pLabel)
// throws PKCS11Exception;
/**
* C_InitPIN initializes the normal user's PIN.
* (Slot and token management)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param pPin the normal user's PIN and the length in bytes of the PIN
* (PKCS#11 param: CK_CHAR_PTR pPin, CK_ULONG ulPinLen)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
// public native void C_InitPIN(long hSession, char[] pPin)
// throws PKCS11Exception;
/**
* C_SetPIN modifies the PIN of the user who is logged in.
* (Slot and token management)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param pOldPin the old PIN and the length of the old PIN
* (PKCS#11 param: CK_CHAR_PTR pOldPin, CK_ULONG ulOldLen)
* @param pNewPin the new PIN and the length of the new PIN
* (PKCS#11 param: CK_CHAR_PTR pNewPin, CK_ULONG ulNewLen)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
// public native void C_SetPIN(long hSession, char[] pOldPin, char[] pNewPin)
// throws PKCS11Exception;
/* *****************************************************************************
* Session management
******************************************************************************/
/**
* C_OpenSession opens a session between an application and a
* token.
* (Session management)
*
* @param slotID the slot's ID
* (PKCS#11 param: CK_SLOT_ID slotID)
* @param flags of CK_SESSION_INFO
* (PKCS#11 param: CK_FLAGS flags)
* @param pApplication passed to callback
* (PKCS#11 param: CK_VOID_PTR pApplication)
* @param Notify the callback function
* (PKCS#11 param: CK_NOTIFY Notify)
* @return the session handle
* (PKCS#11 param: CK_SESSION_HANDLE_PTR phSession)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
public native long C_OpenSession(long slotID, long flags,
Object pApplication, CK_NOTIFY Notify) throws PKCS11Exception;
/**
* C_CloseSession closes a session between an application and a
* token.
* (Session management)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
public native void C_CloseSession(long hSession) throws PKCS11Exception;
/**
* C_CloseAllSessions closes all sessions with a token.
* (Session management)
*
* @param slotID the ID of the token's slot
* (PKCS#11 param: CK_SLOT_ID slotID)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
// public native void C_CloseAllSessions(long slotID) throws PKCS11Exception;
/**
* C_GetSessionInfo obtains information about the session.
* (Session management)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @return the session info
* (PKCS#11 param: CK_SESSION_INFO_PTR pInfo)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions (result <> null)
*/
public native CK_SESSION_INFO C_GetSessionInfo(long hSession)
throws PKCS11Exception;
/**
* C_SessionCancel terminates active session based operations.
* (Session management) (New in PKCS#11 v3.0)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param flags indicates the operations to cancel.
* (PKCS#11 param: CK_FLAGS flags)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
public native void C_SessionCancel(long hSession, long flags)
throws PKCS11Exception;
/**
* C_GetOperationState obtains the state of the cryptographic operation
* in a session.
* (Session management)
*
* @param hSession session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @return the state and the state length
* (PKCS#11 param: CK_BYTE_PTR pOperationState,
* CK_ULONG_PTR pulOperationStateLen)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions (result <> null)
*/
public native byte[] C_GetOperationState(long hSession)
throws PKCS11Exception;
/**
* C_SetOperationState restores the state of the cryptographic
* operation in a session.
* (Session management)
*
* @param hSession session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param pOperationState the state and the state length
* (PKCS#11 param: CK_BYTE_PTR pOperationState,
* CK_ULONG ulOperationStateLen)
* @param hEncryptionKey en/decryption key
* (PKCS#11 param: CK_OBJECT_HANDLE hEncryptionKey)
* @param hAuthenticationKey sign/verify key
* (PKCS#11 param: CK_OBJECT_HANDLE hAuthenticationKey)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
public native void C_SetOperationState(long hSession, byte[] pOperationState,
long hEncryptionKey, long hAuthenticationKey) throws PKCS11Exception;
/**
* C_Login logs a user into a token.
* (Session management)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param userType the user type
* (PKCS#11 param: CK_USER_TYPE userType)
* @param pPin the user's PIN and the length of the PIN
* (PKCS#11 param: CK_CHAR_PTR pPin, CK_ULONG ulPinLen)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
public native void C_Login(long hSession, long userType, char[] pPin)
throws PKCS11Exception;
///**
// * C_LoginUser logs a user into a token. (New in PKCS#11 v3.0)
// * (Session management)
// *
// * @param hSession the session's handle
// * (PKCS#11 param: CK_SESSION_HANDLE hSession)
// * @param userType the user type
// * (PKCS#11 param: CK_USER_TYPE userType)
// * @param pPin the user's PIN and the length of the PIN
// * (PKCS#11 param: CK_CHAR_PTR pPin, CK_ULONG ulPinLen)
// * @param pUsername the user name and the length of the user name
// * (PKCS#11 param: CK_CHAR_PTR pUsername, CK_ULONG ulUsernameLen)
// * @exception PKCS11Exception If function returns other value than CKR_OK.
// * @preconditions
// * @postconditions
// */
//public native void C_LoginUser(long hSession, long userType, char[] pPin,
// String pUsername) throws PKCS11Exception;
/**
* C_Logout logs a user out from a token.
* (Session management)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
public native void C_Logout(long hSession) throws PKCS11Exception;
/* *****************************************************************************
* Object management
******************************************************************************/
/**
* C_CreateObject creates a new object.
* (Object management)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param pTemplate the object's template and number of attributes in
* template
* (PKCS#11 param: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)
* @return the object's handle
* (PKCS#11 param: CK_OBJECT_HANDLE_PTR phObject)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
public native long C_CreateObject(long hSession, CK_ATTRIBUTE[] pTemplate)
throws PKCS11Exception;
/**
* C_CopyObject copies an object, creating a new object for the
* copy.
* (Object management)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param hObject the object's handle
* (PKCS#11 param: CK_OBJECT_HANDLE hObject)
* @param pTemplate the template for the new object and number of attributes
* in template
* (PKCS#11 param: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)
* @return the handle of the copy
* (PKCS#11 param: CK_OBJECT_HANDLE_PTR phNewObject)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
public native long C_CopyObject(long hSession, long hObject,
CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception;
/**
* C_DestroyObject destroys an object.
* (Object management)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param hObject the object's handle
* (PKCS#11 param: CK_OBJECT_HANDLE hObject)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
public native void C_DestroyObject(long hSession, long hObject)
throws PKCS11Exception;
/**
* C_GetObjectSize gets the size of an object in bytes.
* (Object management)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param hObject the object's handle
* (PKCS#11 param: CK_OBJECT_HANDLE hObject)
* @return the size of the object
* (PKCS#11 param: CK_ULONG_PTR pulSize)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
// public native long C_GetObjectSize(long hSession, long hObject)
// throws PKCS11Exception;
/**
* C_GetAttributeValue obtains the value of one or more object
* attributes. The template attributes also receive the values.
* (Object management)
* note: in PKCS#11 pTemplate and the result template are the same
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param hObject the object's handle
* (PKCS#11 param: CK_OBJECT_HANDLE hObject)
* @param pTemplate specifies the attributes and number of attributes to get
* The template attributes also receive the values.
* (PKCS#11 param: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions (pTemplate <> null)
* @postconditions (result <> null)
*/
public native void C_GetAttributeValue(long hSession, long hObject,
CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception;
/**
* C_SetAttributeValue modifies the value of one or more object
* attributes
* (Object management)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param hObject the object's handle
* (PKCS#11 param: CK_OBJECT_HANDLE hObject)
* @param pTemplate specifies the attributes and values to get; number of
* attributes in the template
* (PKCS#11 param: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions (pTemplate <> null)
* @postconditions
*/
public native void C_SetAttributeValue(long hSession, long hObject,
CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception;
/**
* C_FindObjectsInit initializes a search for token and session
* objects that match a template.
* (Object management)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param pTemplate the object's attribute values to match and the number of
* attributes in search template
* (PKCS#11 param: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
public native void C_FindObjectsInit(long hSession, CK_ATTRIBUTE[] pTemplate)
throws PKCS11Exception;
/**
* C_FindObjects continues a search for token and session
* objects that match a template, obtaining additional object
* handles.
* (Object management)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param ulMaxObjectCount the max. object handles to get
* (PKCS#11 param: CK_ULONG ulMaxObjectCount)
* @return the object's handles and the actual number of objects returned
* (PKCS#11 param: CK_ULONG_PTR pulObjectCount)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions (result <> null)
*/
public native long[] C_FindObjects(long hSession, long ulMaxObjectCount)
throws PKCS11Exception;
/**
* C_FindObjectsFinal finishes a search for token and session
* objects.
* (Object management)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
public native void C_FindObjectsFinal(long hSession) throws PKCS11Exception;
/* *****************************************************************************
* Encryption and decryption
******************************************************************************/
/**
* C_EncryptInit initializes an encryption operation.
* (Encryption and decryption)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param pMechanism the encryption mechanism
* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)
* @param hKey the handle of the encryption key
* (PKCS#11 param: CK_OBJECT_HANDLE hKey)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
public native void C_EncryptInit(long hSession, CK_MECHANISM pMechanism,
long hKey) throws PKCS11Exception;
/**
* C_GCMEncryptInitWithRetry initializes a GCM encryption operation and retry
* with alternative param structure for max compatibility.
* (Encryption and decryption)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param pMechanism the encryption mechanism
* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)
* @param hKey the handle of the encryption key
* (PKCS#11 param: CK_OBJECT_HANDLE hKey)
* @param useNormativeVerFirst whether to use normative version of GCM parameter first
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
public native void C_GCMEncryptInitWithRetry(long hSession, CK_MECHANISM pMechanism,
long hKey, boolean useNormativeVerFirst) throws PKCS11Exception;
/**
* C_Encrypt encrypts single-part data.
* (Encryption and decryption)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param directIn the address of the to-be-encrypted data
* @param in buffer containing the to-be-encrypted data
* @param inOfs buffer offset of the to-be-encrypted data
* @param inLen length of the to-be-encrypted data
* (PKCS#11 param: CK_BYTE_PTR pData, CK_ULONG ulDataLen)
* @param directOut the address for the encrypted data
* @param out buffer for the encrypted data
* @param outOfs buffer offset for the encrypted data
* @param outLen buffer size for the encrypted data
* @return the length of encrypted data
* (PKCS#11 param: CK_BYTE_PTR pEncryptedData,
* CK_ULONG_PTR pulEncryptedDataLen)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
public native int C_Encrypt(long hSession, long directIn, byte[] in,
int inOfs, int inLen, long directOut, byte[] out, int outOfs,
int outLen) throws PKCS11Exception;
/**
* C_EncryptUpdate continues a multiple-part encryption
* operation.
* (Encryption and decryption)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param directIn the address of the to-be-encrypted data
* @param in buffer containing the to-be-encrypted data
* @param inOfs buffer offset of the to-be-encrypted data
* @param inLen length of the to-be-encrypted data
* (PKCS#11 param: CK_BYTE_PTR pPart, CK_ULONG ulPartLen)
* @param directOut the address for the encrypted data
* @param out buffer for the encrypted data
* @param outOfs buffer offset for the encrypted data
* @param outLen buffer size for the encrypted data
* @return the length of encrypted data for this update
* (PKCS#11 param: CK_BYTE_PTR pEncryptedPart,
* CK_ULONG_PTR pulEncryptedPartLen)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
public native int C_EncryptUpdate(long hSession, long directIn, byte[] in,
int inOfs, int inLen, long directOut, byte[] out, int outOfs,
int outLen) throws PKCS11Exception;
/**
* C_EncryptFinal finishes a multiple-part encryption
* operation.
* (Encryption and decryption)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param directOut the address for the encrypted data
* @param out buffer for the encrypted data
* @param outOfs buffer offset for the encrypted data
* @param outLen buffer size for the encrypted data
* @return the length of the last part of the encrypted data
* (PKCS#11 param: CK_BYTE_PTR pLastEncryptedPart,
* CK_ULONG_PTR pulLastEncryptedPartLen)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
public native int C_EncryptFinal(long hSession, long directOut, byte[] out,
int outOfs, int outLen) throws PKCS11Exception;
/**
* C_DecryptInit initializes a decryption operation.
* (Encryption and decryption)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param pMechanism the decryption mechanism
* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)
* @param hKey the handle of the decryption key
* (PKCS#11 param: CK_OBJECT_HANDLE hKey)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
public native void C_DecryptInit(long hSession, CK_MECHANISM pMechanism,
long hKey) throws PKCS11Exception;
/**
* C_GCMDecryptInitWithRetry initializes a GCM decryption operation
* with alternative param structure for max compatibility.
* (Encryption and decryption)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param pMechanism the decryption mechanism
* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)
* @param hKey the handle of the decryption key
* (PKCS#11 param: CK_OBJECT_HANDLE hKey)
* @param useNormativeVerFirst whether to use normative version of GCM parameter first
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
public native void C_GCMDecryptInitWithRetry(long hSession, CK_MECHANISM pMechanism,
long hKey, boolean useNormativeVerFirst) throws PKCS11Exception;
/**
* C_Decrypt decrypts encrypted data in a single part.
* (Encryption and decryption)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param directIn the address of the to-be-decrypted data
* @param in buffer containing the to-be-decrypted data
* @param inOfs buffer offset of the to-be-decrypted data
* @param inLen length of the to-be-decrypted data
* (PKCS#11 param: CK_BYTE_PTR pDecryptedData,
* CK_ULONG ulDecryptedDataLen)
* @param directOut the address for the decrypted data
* @param out buffer for the decrypted data
* @param outOfs buffer offset for the decrypted data
* @param outLen buffer size for the decrypted data
* @return the length of decrypted data
* (PKCS#11 param: CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
public native int C_Decrypt(long hSession, long directIn, byte[] in,
int inOfs, int inLen, long directOut, byte[] out, int outOfs,
int outLen) throws PKCS11Exception;
/**
* C_DecryptUpdate continues a multiple-part decryption
* operation.
* (Encryption and decryption)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param directIn the address of the to-be-decrypted data
* @param in buffer containing the to-be-decrypted data
* @param inOfs buffer offset of the to-be-decrypted data
* @param inLen length of the to-be-decrypted data
* (PKCS#11 param: CK_BYTE_PTR pDecryptedPart,
* CK_ULONG ulDecryptedPartLen)
* @param directOut the address for the decrypted data
* @param out buffer for the decrypted data
* @param outOfs buffer offset for the decrypted data
* @param outLen buffer size for the decrypted data
* @return the length of decrypted data for this update
* (PKCS#11 param: CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
public native int C_DecryptUpdate(long hSession, long directIn, byte[] in,
int inOfs, int inLen, long directOut, byte[] out, int outOfs,
int outLen) throws PKCS11Exception;
/**
* C_DecryptFinal finishes a multiple-part decryption
* operation.
* (Encryption and decryption)
*
* @param hSession the session's handle
* (PKCS#11 param: CK_SESSION_HANDLE hSession)
* @param directOut the address for the decrypted data
* @param out buffer for the decrypted data
* @param outOfs buffer offset for the decrypted data
* @param outLen buffer size for the decrypted data
* @return the length of this last part of decrypted data
* (PKCS#11 param: CK_BYTE_PTR pLastPart,
* CK_ULONG_PTR pulLastPartLen)
* @exception PKCS11Exception If function returns other value than CKR_OK.
* @preconditions
* @postconditions
*/
public native int C_DecryptFinal(long hSession, long directOut, byte[] out,