Path: blob/master/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/wrapper/PKCS11.java
41161 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2*/34/* Copyright (c) 2002 Graz University of Technology. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions are met:8*9* 1. Redistributions of source code must retain the above copyright notice,10* this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright notice,13* this list of conditions and the following disclaimer in the documentation14* and/or other materials provided with the distribution.15*16* 3. The end-user documentation included with the redistribution, if any, must17* include the following acknowledgment:18*19* "This product includes software developed by IAIK of Graz University of20* Technology."21*22* Alternately, this acknowledgment may appear in the software itself, if23* and wherever such third-party acknowledgments normally appear.24*25* 4. The names "Graz University of Technology" and "IAIK of Graz University of26* Technology" must not be used to endorse or promote products derived from27* this software without prior written permission.28*29* 5. Products derived from this software may not be called30* "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior31* written permission of Graz University of Technology.32*33* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED34* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED35* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR36* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE37* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,38* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,39* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,40* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON41* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,42* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY43* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE44* POSSIBILITY OF SUCH DAMAGE.45*/4647package sun.security.pkcs11.wrapper;4849import java.io.File;50import java.io.IOException;51import java.util.*;5253import java.security.AccessController;54import java.security.PrivilegedAction;5556import sun.security.util.Debug;5758import static sun.security.pkcs11.wrapper.PKCS11Constants.*;59import static sun.security.pkcs11.wrapper.PKCS11Exception.*;6061/**62* This is the default implementation of the PKCS11 interface. IT connects to63* the pkcs11wrapper.dll file, which is the native part of this library.64* The strange and awkward looking initialization was chosen to avoid calling65* loadLibrary from a static initialization block, because this would complicate66* the use in applets.67*68* @author Karl Scheibelhofer <[email protected]>69* @author Martin Schlaeffer <[email protected]>70* @invariants (pkcs11ModulePath_ <> null)71*/72public class PKCS11 {7374/**75* The name of the native part of the wrapper; i.e. the filename without76* the extension (e.g. ".DLL" or ".so").77*/78private static final String PKCS11_WRAPPER = "j2pkcs11";7980static {81// cannot use LoadLibraryAction because that would make the native82// library available to the bootclassloader, but we run in the83// extension classloader.84@SuppressWarnings("removal")85var dummy = AccessController.doPrivileged(new PrivilegedAction<Object>() {86public Object run() {87System.loadLibrary(PKCS11_WRAPPER);88return null;89}90});91boolean enableDebug = Debug.getInstance("sunpkcs11") != null;92initializeLibrary(enableDebug);93}9495public static void loadNative() {96// dummy method that can be called to make sure the native97// portion has been loaded. actual loading happens in the98// static initializer, hence this method is empty.99}100101/* *****************************************************************************102* Utility, Resource Clean up103******************************************************************************/104// always return 0L105public static native long freeMechanism(long hMechanism);106107/**108* The PKCS#11 module to connect to. This is the PKCS#11 driver of the token;109* e.g. pk2priv.dll.110*/111private final String pkcs11ModulePath;112113private long pNativeData;114115/**116* This method does the initialization of the native library. It is called117* exactly once for this class.118*119* @preconditions120* @postconditions121*/122private static native void initializeLibrary(boolean debug);123124// XXX125/**126* This method does the finalization of the native library. It is called127* exactly once for this class. The library uses this method for a clean-up128* of any resources.129*130* @preconditions131* @postconditions132*/133private static native void finalizeLibrary();134135private static final Map<String,PKCS11> moduleMap =136new HashMap<String,PKCS11>();137138/**139* Connects to the PKCS#11 driver given. The filename must contain the140* path, if the driver is not in the system's search path.141*142* @param pkcs11ModulePath the PKCS#11 library path143* @preconditions (pkcs11ModulePath <> null)144* @postconditions145*/146PKCS11(String pkcs11ModulePath, String functionListName)147throws IOException {148connect(pkcs11ModulePath, functionListName);149this.pkcs11ModulePath = pkcs11ModulePath;150}151152public static synchronized PKCS11 getInstance(String pkcs11ModulePath,153String functionList, CK_C_INITIALIZE_ARGS pInitArgs,154boolean omitInitialize) throws IOException, PKCS11Exception {155// we may only call C_Initialize once per native .so/.dll156// so keep a cache using the (non-canonicalized!) path157PKCS11 pkcs11 = moduleMap.get(pkcs11ModulePath);158if (pkcs11 == null) {159if ((pInitArgs != null)160&& ((pInitArgs.flags & CKF_OS_LOCKING_OK) != 0)) {161pkcs11 = new PKCS11(pkcs11ModulePath, functionList);162} else {163pkcs11 = new SynchronizedPKCS11(pkcs11ModulePath, functionList);164}165if (omitInitialize == false) {166try {167pkcs11.C_Initialize(pInitArgs);168} catch (PKCS11Exception e) {169// ignore already-initialized error code170// rethrow all other errors171if (e.getErrorCode() != CKR_CRYPTOKI_ALREADY_INITIALIZED) {172throw e;173}174}175}176moduleMap.put(pkcs11ModulePath, pkcs11);177}178return pkcs11;179}180181/**182* Connects this object to the specified PKCS#11 library. This method is for183* internal use only.184* Declared private, because incorrect handling may result in errors in the185* native part.186*187* @param pkcs11ModulePath The PKCS#11 library path.188* @preconditions (pkcs11ModulePath <> null)189* @postconditions190*/191private native void connect(String pkcs11ModulePath, String functionListName)192throws IOException;193194/**195* Disconnects the PKCS#11 library from this object. After calling this196* method, this object is no longer connected to a native PKCS#11 module197* and any subsequent calls to C_ methods will fail. This method is for198* internal use only.199* Declared private, because incorrect handling may result in errors in the200* native part.201*202* @preconditions203* @postconditions204*/205private native void disconnect();206207208// Implementation of PKCS11 methods delegated to native pkcs11wrapper library209210/* *****************************************************************************211* General-purpose212******************************************************************************/213214/**215* C_Initialize initializes the Cryptoki library.216* (General-purpose)217*218* @param pInitArgs if pInitArgs is not NULL it gets casted to219* CK_C_INITIALIZE_ARGS_PTR and dereferenced220* (PKCS#11 param: CK_VOID_PTR pInitArgs)221* @exception PKCS11Exception If function returns other value than CKR_OK.222* @preconditions223* @postconditions224*/225native void C_Initialize(Object pInitArgs) throws PKCS11Exception;226227/**228* C_Finalize indicates that an application is done with the229* Cryptoki library230* (General-purpose)231*232* @param pReserved is reserved. Should be NULL_PTR233* (PKCS#11 param: CK_VOID_PTR pReserved)234* @exception PKCS11Exception If function returns other value than CKR_OK.235* @preconditions (pReserved == null)236* @postconditions237*/238public native void C_Finalize(Object pReserved) throws PKCS11Exception;239240241/**242* C_GetInfo returns general information about Cryptoki.243* (General-purpose)244*245* @return the information.246* (PKCS#11 param: CK_INFO_PTR pInfo)247* @exception PKCS11Exception If function returns other value than CKR_OK.248* @preconditions249* @postconditions (result <> null)250*/251public native CK_INFO C_GetInfo() throws PKCS11Exception;252253254/* *****************************************************************************255* Slot and token management256******************************************************************************/257258/**259* C_GetSlotList obtains a list of slots in the system.260* (Slot and token management)261*262* @param tokenPresent if true only Slot IDs with a token are returned263* (PKCS#11 param: CK_BBOOL tokenPresent)264* @return a long array of slot IDs and number of Slot IDs265* (PKCS#11 param: CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount)266* @exception PKCS11Exception If function returns other value than CKR_OK.267* @preconditions268* @postconditions (result <> null)269*/270public native long[] C_GetSlotList(boolean tokenPresent)271throws PKCS11Exception;272273274/**275* C_GetSlotInfo obtains information about a particular slot in276* the system.277* (Slot and token management)278*279* @param slotID the ID of the slot280* (PKCS#11 param: CK_SLOT_ID slotID)281* @return the slot information282* (PKCS#11 param: CK_SLOT_INFO_PTR pInfo)283* @exception PKCS11Exception If function returns other value than CKR_OK.284* @preconditions285* @postconditions (result <> null)286*/287public native CK_SLOT_INFO C_GetSlotInfo(long slotID) throws PKCS11Exception;288289290/**291* C_GetTokenInfo obtains information about a particular token292* in the system.293* (Slot and token management)294*295* @param slotID ID of the token's slot296* (PKCS#11 param: CK_SLOT_ID slotID)297* @return the token information298* (PKCS#11 param: CK_TOKEN_INFO_PTR pInfo)299* @exception PKCS11Exception If function returns other value than CKR_OK.300* @preconditions301* @postconditions (result <> null)302*/303public native CK_TOKEN_INFO C_GetTokenInfo(long slotID)304throws PKCS11Exception;305306307/**308* C_GetMechanismList obtains a list of mechanism types309* supported by a token.310* (Slot and token management)311*312* @param slotID ID of the token's slot313* (PKCS#11 param: CK_SLOT_ID slotID)314* @return a long array of mechanism types and number of mechanism types315* (PKCS#11 param: CK_MECHANISM_TYPE_PTR pMechanismList,316* CK_ULONG_PTR pulCount)317* @exception PKCS11Exception If function returns other value than CKR_OK.318* @preconditions319* @postconditions (result <> null)320*/321public native long[] C_GetMechanismList(long slotID) throws PKCS11Exception;322323324/**325* C_GetMechanismInfo obtains information about a particular326* mechanism possibly supported by a token.327* (Slot and token management)328*329* @param slotID ID of the token's slot330* (PKCS#11 param: CK_SLOT_ID slotID)331* @param type type of mechanism332* (PKCS#11 param: CK_MECHANISM_TYPE type)333* @return the mechanism info334* (PKCS#11 param: CK_MECHANISM_INFO_PTR pInfo)335* @exception PKCS11Exception If function returns other value than CKR_OK.336* @preconditions337* @postconditions (result <> null)338*/339public native CK_MECHANISM_INFO C_GetMechanismInfo(long slotID, long type)340throws PKCS11Exception;341342343/**344* C_InitToken initializes a token.345* (Slot and token management)346*347* @param slotID ID of the token's slot348* (PKCS#11 param: CK_SLOT_ID slotID)349* @param pPin the SO's initial PIN and the length in bytes of the PIN350* (PKCS#11 param: CK_CHAR_PTR pPin, CK_ULONG ulPinLen)351* @param pLabel 32-byte token label (blank padded)352* (PKCS#11 param: CK_UTF8CHAR_PTR pLabel)353* @exception PKCS11Exception If function returns other value than CKR_OK.354* @preconditions355* @postconditions356*/357// public native void C_InitToken(long slotID, char[] pPin, char[] pLabel)358// throws PKCS11Exception;359360361/**362* C_InitPIN initializes the normal user's PIN.363* (Slot and token management)364*365* @param hSession the session's handle366* (PKCS#11 param: CK_SESSION_HANDLE hSession)367* @param pPin the normal user's PIN and the length in bytes of the PIN368* (PKCS#11 param: CK_CHAR_PTR pPin, CK_ULONG ulPinLen)369* @exception PKCS11Exception If function returns other value than CKR_OK.370* @preconditions371* @postconditions372*/373// public native void C_InitPIN(long hSession, char[] pPin)374// throws PKCS11Exception;375376377/**378* C_SetPIN modifies the PIN of the user who is logged in.379* (Slot and token management)380*381* @param hSession the session's handle382* (PKCS#11 param: CK_SESSION_HANDLE hSession)383* @param pOldPin the old PIN and the length of the old PIN384* (PKCS#11 param: CK_CHAR_PTR pOldPin, CK_ULONG ulOldLen)385* @param pNewPin the new PIN and the length of the new PIN386* (PKCS#11 param: CK_CHAR_PTR pNewPin, CK_ULONG ulNewLen)387* @exception PKCS11Exception If function returns other value than CKR_OK.388* @preconditions389* @postconditions390*/391// public native void C_SetPIN(long hSession, char[] pOldPin, char[] pNewPin)392// throws PKCS11Exception;393394395396/* *****************************************************************************397* Session management398******************************************************************************/399400/**401* C_OpenSession opens a session between an application and a402* token.403* (Session management)404*405* @param slotID the slot's ID406* (PKCS#11 param: CK_SLOT_ID slotID)407* @param flags of CK_SESSION_INFO408* (PKCS#11 param: CK_FLAGS flags)409* @param pApplication passed to callback410* (PKCS#11 param: CK_VOID_PTR pApplication)411* @param Notify the callback function412* (PKCS#11 param: CK_NOTIFY Notify)413* @return the session handle414* (PKCS#11 param: CK_SESSION_HANDLE_PTR phSession)415* @exception PKCS11Exception If function returns other value than CKR_OK.416* @preconditions417* @postconditions418*/419public native long C_OpenSession(long slotID, long flags,420Object pApplication, CK_NOTIFY Notify) throws PKCS11Exception;421422423/**424* C_CloseSession closes a session between an application and a425* token.426* (Session management)427*428* @param hSession the session's handle429* (PKCS#11 param: CK_SESSION_HANDLE hSession)430* @exception PKCS11Exception If function returns other value than CKR_OK.431* @preconditions432* @postconditions433*/434public native void C_CloseSession(long hSession) throws PKCS11Exception;435436437/**438* C_CloseAllSessions closes all sessions with a token.439* (Session management)440*441* @param slotID the ID of the token's slot442* (PKCS#11 param: CK_SLOT_ID slotID)443* @exception PKCS11Exception If function returns other value than CKR_OK.444* @preconditions445* @postconditions446*/447// public native void C_CloseAllSessions(long slotID) throws PKCS11Exception;448449450/**451* C_GetSessionInfo obtains information about the session.452* (Session management)453*454* @param hSession the session's handle455* (PKCS#11 param: CK_SESSION_HANDLE hSession)456* @return the session info457* (PKCS#11 param: CK_SESSION_INFO_PTR pInfo)458* @exception PKCS11Exception If function returns other value than CKR_OK.459* @preconditions460* @postconditions (result <> null)461*/462public native CK_SESSION_INFO C_GetSessionInfo(long hSession)463throws PKCS11Exception;464465466/**467* C_GetOperationState obtains the state of the cryptographic operation468* in a session.469* (Session management)470*471* @param hSession session's handle472* (PKCS#11 param: CK_SESSION_HANDLE hSession)473* @return the state and the state length474* (PKCS#11 param: CK_BYTE_PTR pOperationState,475* CK_ULONG_PTR pulOperationStateLen)476* @exception PKCS11Exception If function returns other value than CKR_OK.477* @preconditions478* @postconditions (result <> null)479*/480public native byte[] C_GetOperationState(long hSession)481throws PKCS11Exception;482483484/**485* C_SetOperationState restores the state of the cryptographic486* operation in a session.487* (Session management)488*489* @param hSession session's handle490* (PKCS#11 param: CK_SESSION_HANDLE hSession)491* @param pOperationState the state and the state length492* (PKCS#11 param: CK_BYTE_PTR pOperationState,493* CK_ULONG ulOperationStateLen)494* @param hEncryptionKey en/decryption key495* (PKCS#11 param: CK_OBJECT_HANDLE hEncryptionKey)496* @param hAuthenticationKey sign/verify key497* (PKCS#11 param: CK_OBJECT_HANDLE hAuthenticationKey)498* @exception PKCS11Exception If function returns other value than CKR_OK.499* @preconditions500* @postconditions501*/502public native void C_SetOperationState(long hSession, byte[] pOperationState,503long hEncryptionKey, long hAuthenticationKey) throws PKCS11Exception;504505506/**507* C_Login logs a user into a token.508* (Session management)509*510* @param hSession the session's handle511* (PKCS#11 param: CK_SESSION_HANDLE hSession)512* @param userType the user type513* (PKCS#11 param: CK_USER_TYPE userType)514* @param pPin the user's PIN and the length of the PIN515* (PKCS#11 param: CK_CHAR_PTR pPin, CK_ULONG ulPinLen)516* @exception PKCS11Exception If function returns other value than CKR_OK.517* @preconditions518* @postconditions519*/520public native void C_Login(long hSession, long userType, char[] pPin)521throws PKCS11Exception;522523524/**525* C_Logout logs a user out from a token.526* (Session management)527*528* @param hSession the session's handle529* (PKCS#11 param: CK_SESSION_HANDLE hSession)530* @exception PKCS11Exception If function returns other value than CKR_OK.531* @preconditions532* @postconditions533*/534public native void C_Logout(long hSession) throws PKCS11Exception;535536537538/* *****************************************************************************539* Object management540******************************************************************************/541542/**543* C_CreateObject creates a new object.544* (Object management)545*546* @param hSession the session's handle547* (PKCS#11 param: CK_SESSION_HANDLE hSession)548* @param pTemplate the object's template and number of attributes in549* template550* (PKCS#11 param: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)551* @return the object's handle552* (PKCS#11 param: CK_OBJECT_HANDLE_PTR phObject)553* @exception PKCS11Exception If function returns other value than CKR_OK.554* @preconditions555* @postconditions556*/557public native long C_CreateObject(long hSession, CK_ATTRIBUTE[] pTemplate)558throws PKCS11Exception;559560561/**562* C_CopyObject copies an object, creating a new object for the563* copy.564* (Object management)565*566* @param hSession the session's handle567* (PKCS#11 param: CK_SESSION_HANDLE hSession)568* @param hObject the object's handle569* (PKCS#11 param: CK_OBJECT_HANDLE hObject)570* @param pTemplate the template for the new object and number of attributes571* in template572* (PKCS#11 param: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)573* @return the handle of the copy574* (PKCS#11 param: CK_OBJECT_HANDLE_PTR phNewObject)575* @exception PKCS11Exception If function returns other value than CKR_OK.576* @preconditions577* @postconditions578*/579public native long C_CopyObject(long hSession, long hObject,580CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception;581582583/**584* C_DestroyObject destroys an object.585* (Object management)586*587* @param hSession the session's handle588* (PKCS#11 param: CK_SESSION_HANDLE hSession)589* @param hObject the object's handle590* (PKCS#11 param: CK_OBJECT_HANDLE hObject)591* @exception PKCS11Exception If function returns other value than CKR_OK.592* @preconditions593* @postconditions594*/595public native void C_DestroyObject(long hSession, long hObject)596throws PKCS11Exception;597598599/**600* C_GetObjectSize gets the size of an object in bytes.601* (Object management)602*603* @param hSession the session's handle604* (PKCS#11 param: CK_SESSION_HANDLE hSession)605* @param hObject the object's handle606* (PKCS#11 param: CK_OBJECT_HANDLE hObject)607* @return the size of the object608* (PKCS#11 param: CK_ULONG_PTR pulSize)609* @exception PKCS11Exception If function returns other value than CKR_OK.610* @preconditions611* @postconditions612*/613// public native long C_GetObjectSize(long hSession, long hObject)614// throws PKCS11Exception;615616617/**618* C_GetAttributeValue obtains the value of one or more object619* attributes. The template attributes also receive the values.620* (Object management)621* note: in PKCS#11 pTemplate and the result template are the same622*623* @param hSession the session's handle624* (PKCS#11 param: CK_SESSION_HANDLE hSession)625* @param hObject the object's handle626* (PKCS#11 param: CK_OBJECT_HANDLE hObject)627* @param pTemplate specifies the attributes and number of attributes to get628* The template attributes also receive the values.629* (PKCS#11 param: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)630* @exception PKCS11Exception If function returns other value than CKR_OK.631* @preconditions (pTemplate <> null)632* @postconditions (result <> null)633*/634public native void C_GetAttributeValue(long hSession, long hObject,635CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception;636637638/**639* C_SetAttributeValue modifies the value of one or more object640* attributes641* (Object management)642*643* @param hSession the session's handle644* (PKCS#11 param: CK_SESSION_HANDLE hSession)645* @param hObject the object's handle646* (PKCS#11 param: CK_OBJECT_HANDLE hObject)647* @param pTemplate specifies the attributes and values to get; number of648* attributes in the template649* (PKCS#11 param: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)650* @exception PKCS11Exception If function returns other value than CKR_OK.651* @preconditions (pTemplate <> null)652* @postconditions653*/654public native void C_SetAttributeValue(long hSession, long hObject,655CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception;656657658/**659* C_FindObjectsInit initializes a search for token and session660* objects that match a template.661* (Object management)662*663* @param hSession the session's handle664* (PKCS#11 param: CK_SESSION_HANDLE hSession)665* @param pTemplate the object's attribute values to match and the number of666* attributes in search template667* (PKCS#11 param: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)668* @exception PKCS11Exception If function returns other value than CKR_OK.669* @preconditions670* @postconditions671*/672public native void C_FindObjectsInit(long hSession, CK_ATTRIBUTE[] pTemplate)673throws PKCS11Exception;674675676/**677* C_FindObjects continues a search for token and session678* objects that match a template, obtaining additional object679* handles.680* (Object management)681*682* @param hSession the session's handle683* (PKCS#11 param: CK_SESSION_HANDLE hSession)684* @param ulMaxObjectCount the max. object handles to get685* (PKCS#11 param: CK_ULONG ulMaxObjectCount)686* @return the object's handles and the actual number of objects returned687* (PKCS#11 param: CK_ULONG_PTR pulObjectCount)688* @exception PKCS11Exception If function returns other value than CKR_OK.689* @preconditions690* @postconditions (result <> null)691*/692public native long[] C_FindObjects(long hSession, long ulMaxObjectCount)693throws PKCS11Exception;694695696/**697* C_FindObjectsFinal finishes a search for token and session698* objects.699* (Object management)700*701* @param hSession the session's handle702* (PKCS#11 param: CK_SESSION_HANDLE hSession)703* @exception PKCS11Exception If function returns other value than CKR_OK.704* @preconditions705* @postconditions706*/707public native void C_FindObjectsFinal(long hSession) throws PKCS11Exception;708709710711/* *****************************************************************************712* Encryption and decryption713******************************************************************************/714715/**716* C_EncryptInit initializes an encryption operation.717* (Encryption and decryption)718*719* @param hSession the session's handle720* (PKCS#11 param: CK_SESSION_HANDLE hSession)721* @param pMechanism the encryption mechanism722* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)723* @param hKey the handle of the encryption key724* (PKCS#11 param: CK_OBJECT_HANDLE hKey)725* @exception PKCS11Exception If function returns other value than CKR_OK.726* @preconditions727* @postconditions728*/729public native void C_EncryptInit(long hSession, CK_MECHANISM pMechanism,730long hKey) throws PKCS11Exception;731732733/**734* C_Encrypt encrypts single-part data.735* (Encryption and decryption)736*737* @param hSession the session's handle738* (PKCS#11 param: CK_SESSION_HANDLE hSession)739* @param directIn the address of the to-be-encrypted data740* @param in buffer containing the to-be-encrypted data741* @param inOfs buffer offset of the to-be-encrypted data742* @param inLen length of the to-be-encrypted data743* (PKCS#11 param: CK_BYTE_PTR pData, CK_ULONG ulDataLen)744* @param directOut the address for the encrypted data745* @param out buffer for the encrypted data746* @param outOfs buffer offset for the encrypted data747* @param outLen buffer size for the encrypted data748* @return the length of encrypted data749* (PKCS#11 param: CK_BYTE_PTR pEncryptedData,750* CK_ULONG_PTR pulEncryptedDataLen)751* @exception PKCS11Exception If function returns other value than CKR_OK.752* @preconditions753* @postconditions754*/755public native int C_Encrypt(long hSession, long directIn, byte[] in,756int inOfs, int inLen, long directOut, byte[] out, int outOfs,757int outLen) throws PKCS11Exception;758759760/**761* C_EncryptUpdate continues a multiple-part encryption762* operation.763* (Encryption and decryption)764*765* @param hSession the session's handle766* (PKCS#11 param: CK_SESSION_HANDLE hSession)767* @param directIn the address of the to-be-encrypted data768* @param in buffer containing the to-be-encrypted data769* @param inOfs buffer offset of the to-be-encrypted data770* @param inLen length of the to-be-encrypted data771* (PKCS#11 param: CK_BYTE_PTR pPart, CK_ULONG ulPartLen)772* @param directOut the address for the encrypted data773* @param out buffer for the encrypted data774* @param outOfs buffer offset for the encrypted data775* @param outLen buffer size for the encrypted data776* @return the length of encrypted data for this update777* (PKCS#11 param: CK_BYTE_PTR pEncryptedPart,778* CK_ULONG_PTR pulEncryptedPartLen)779* @exception PKCS11Exception If function returns other value than CKR_OK.780* @preconditions781* @postconditions782*/783public native int C_EncryptUpdate(long hSession, long directIn, byte[] in,784int inOfs, int inLen, long directOut, byte[] out, int outOfs,785int outLen) throws PKCS11Exception;786787788/**789* C_EncryptFinal finishes a multiple-part encryption790* operation.791* (Encryption and decryption)792*793* @param hSession the session's handle794* (PKCS#11 param: CK_SESSION_HANDLE hSession)795* @param directOut the address for the encrypted data796* @param out buffer for the encrypted data797* @param outOfs buffer offset for the encrypted data798* @param outLen buffer size for the encrypted data799* @return the length of the last part of the encrypted data800* (PKCS#11 param: CK_BYTE_PTR pLastEncryptedPart,801* CK_ULONG_PTR pulLastEncryptedPartLen)802* @exception PKCS11Exception If function returns other value than CKR_OK.803* @preconditions804* @postconditions805*/806public native int C_EncryptFinal(long hSession, long directOut, byte[] out,807int outOfs, int outLen) throws PKCS11Exception;808809810/**811* C_DecryptInit initializes a decryption operation.812* (Encryption and decryption)813*814* @param hSession the session's handle815* (PKCS#11 param: CK_SESSION_HANDLE hSession)816* @param pMechanism the decryption mechanism817* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)818* @param hKey the handle of the decryption key819* (PKCS#11 param: CK_OBJECT_HANDLE hKey)820* @exception PKCS11Exception If function returns other value than CKR_OK.821* @preconditions822* @postconditions823*/824public native void C_DecryptInit(long hSession, CK_MECHANISM pMechanism,825long hKey) throws PKCS11Exception;826827828/**829* C_Decrypt decrypts encrypted data in a single part.830* (Encryption and decryption)831*832* @param hSession the session's handle833* (PKCS#11 param: CK_SESSION_HANDLE hSession)834* @param directIn the address of the to-be-decrypted data835* @param in buffer containing the to-be-decrypted data836* @param inOfs buffer offset of the to-be-decrypted data837* @param inLen length of the to-be-decrypted data838* (PKCS#11 param: CK_BYTE_PTR pDecryptedData,839* CK_ULONG ulDecryptedDataLen)840* @param directOut the address for the decrypted data841* @param out buffer for the decrypted data842* @param outOfs buffer offset for the decrypted data843* @param outLen buffer size for the decrypted data844* @return the length of decrypted data845* (PKCS#11 param: CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen)846* @exception PKCS11Exception If function returns other value than CKR_OK.847* @preconditions848* @postconditions849*/850public native int C_Decrypt(long hSession, long directIn, byte[] in,851int inOfs, int inLen, long directOut, byte[] out, int outOfs,852int outLen) throws PKCS11Exception;853854855/**856* C_DecryptUpdate continues a multiple-part decryption857* operation.858* (Encryption and decryption)859*860* @param hSession the session's handle861* (PKCS#11 param: CK_SESSION_HANDLE hSession)862* @param directIn the address of the to-be-decrypted data863* @param in buffer containing the to-be-decrypted data864* @param inOfs buffer offset of the to-be-decrypted data865* @param inLen length of the to-be-decrypted data866* (PKCS#11 param: CK_BYTE_PTR pDecryptedPart,867* CK_ULONG ulDecryptedPartLen)868* @param directOut the address for the decrypted data869* @param out buffer for the decrypted data870* @param outOfs buffer offset for the decrypted data871* @param outLen buffer size for the decrypted data872* @return the length of decrypted data for this update873* (PKCS#11 param: CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen)874* @exception PKCS11Exception If function returns other value than CKR_OK.875* @preconditions876* @postconditions877*/878public native int C_DecryptUpdate(long hSession, long directIn, byte[] in,879int inOfs, int inLen, long directOut, byte[] out, int outOfs,880int outLen) throws PKCS11Exception;881882883/**884* C_DecryptFinal finishes a multiple-part decryption885* operation.886* (Encryption and decryption)887*888* @param hSession the session's handle889* (PKCS#11 param: CK_SESSION_HANDLE hSession)890* @param directOut the address for the decrypted data891* @param out buffer for the decrypted data892* @param outOfs buffer offset for the decrypted data893* @param outLen buffer size for the decrypted data894* @return the length of this last part of decrypted data895* (PKCS#11 param: CK_BYTE_PTR pLastPart,896* CK_ULONG_PTR pulLastPartLen)897* @exception PKCS11Exception If function returns other value than CKR_OK.898* @preconditions899* @postconditions900*/901public native int C_DecryptFinal(long hSession, long directOut, byte[] out,902int outOfs, int outLen) throws PKCS11Exception;903904905906/* *****************************************************************************907* Message digesting908******************************************************************************/909910/**911* C_DigestInit initializes a message-digesting operation.912* (Message digesting)913*914* @param hSession the session's handle915* (PKCS#11 param: CK_SESSION_HANDLE hSession)916* @param pMechanism the digesting mechanism917* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)918* @exception PKCS11Exception If function returns other value than CKR_OK.919* @preconditions920* @postconditions921*/922public native void C_DigestInit(long hSession, CK_MECHANISM pMechanism)923throws PKCS11Exception;924925926// note that C_DigestSingle does not exist in PKCS#11927// we combined the C_DigestInit and C_Digest into a single function928// to save on Java<->C transitions and save 5-10% on small digests929// this made the C_Digest method redundant, it has been removed930/**931* C_Digest digests data in a single part.932* (Message digesting)933*934* @param hSession the session's handle935* (PKCS#11 param: CK_SESSION_HANDLE hSession)936* @param data the data to get digested and the data's length937* (PKCS#11 param: CK_BYTE_PTR pData, CK_ULONG ulDataLen)938* @return the message digest and the length of the message digest939* (PKCS#11 param: CK_BYTE_PTR pDigest, CK_ULONG_PTR pulDigestLen)940* @exception PKCS11Exception If function returns other value than CKR_OK.941* @preconditions (data <> null)942* @postconditions (result <> null)943*/944public native int C_DigestSingle(long hSession, CK_MECHANISM pMechanism,945byte[] in, int inOfs, int inLen, byte[] digest, int digestOfs,946int digestLen) throws PKCS11Exception;947948949/**950* C_DigestUpdate continues a multiple-part message-digesting951* operation.952* (Message digesting)953*954* @param hSession the session's handle955* (PKCS#11 param: CK_SESSION_HANDLE hSession)956* @param pPart the data to get digested and the data's length957* (PKCS#11 param: CK_BYTE_PTR pPart, CK_ULONG ulPartLen)958* @exception PKCS11Exception If function returns other value than CKR_OK.959* @preconditions (pPart <> null)960* @postconditions961*/962public native void C_DigestUpdate(long hSession, long directIn, byte[] in,963int inOfs, int inLen) throws PKCS11Exception;964965966/**967* C_DigestKey continues a multi-part message-digesting968* operation, by digesting the value of a secret key as part of969* the data already digested.970* (Message digesting)971*972* @param hSession the session's handle973* (PKCS#11 param: CK_SESSION_HANDLE hSession)974* @param hKey the handle of the secret key to be digested975* (PKCS#11 param: CK_OBJECT_HANDLE hKey)976* @exception PKCS11Exception If function returns other value than CKR_OK.977* @preconditions978* @postconditions979*/980public native void C_DigestKey(long hSession, long hKey)981throws PKCS11Exception;982983984/**985* C_DigestFinal finishes a multiple-part message-digesting986* operation.987* (Message digesting)988*989* @param hSession the session's handle990* (PKCS#11 param: CK_SESSION_HANDLE hSession)991* @return the message digest and the length of the message digest992* (PKCS#11 param: CK_BYTE_PTR pDigest, CK_ULONG_PTR pulDigestLen)993* @exception PKCS11Exception If function returns other value than CKR_OK.994* @preconditions995* @postconditions (result <> null)996*/997public native int C_DigestFinal(long hSession, byte[] pDigest, int digestOfs,998int digestLen) throws PKCS11Exception;999100010011002/* *****************************************************************************1003* Signing and MACing1004******************************************************************************/10051006/**1007* C_SignInit initializes a signature (private key encryption)1008* operation, where the signature is (will be) an appendix to1009* the data, and plaintext cannot be recovered from the1010* signature.1011* (Signing and MACing)1012*1013* @param hSession the session's handle1014* (PKCS#11 param: CK_SESSION_HANDLE hSession)1015* @param pMechanism the signature mechanism1016* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)1017* @param hKey the handle of the signature key1018* (PKCS#11 param: CK_OBJECT_HANDLE hKey)1019* @exception PKCS11Exception If function returns other value than CKR_OK.1020* @preconditions1021* @postconditions1022*/1023public native void C_SignInit(long hSession, CK_MECHANISM pMechanism,1024long hKey) throws PKCS11Exception;102510261027/**1028* C_Sign signs (encrypts with private key) data in a single1029* part, where the signature is (will be) an appendix to the1030* data, and plaintext cannot be recovered from the signature.1031* (Signing and MACing)1032*1033* @param hSession the session's handle1034* (PKCS#11 param: CK_SESSION_HANDLE hSession)1035* @param pData the data to sign and the data's length1036* (PKCS#11 param: CK_BYTE_PTR pData, CK_ULONG ulDataLen)1037* @return the signature and the signature's length1038* (PKCS#11 param: CK_BYTE_PTR pSignature,1039* CK_ULONG_PTR pulSignatureLen)1040* @exception PKCS11Exception If function returns other value than CKR_OK.1041* @preconditions (pData <> null)1042* @postconditions (result <> null)1043*/1044public native byte[] C_Sign(long hSession, byte[] pData)1045throws PKCS11Exception;104610471048/**1049* C_SignUpdate continues a multiple-part signature operation,1050* where the signature is (will be) an appendix to the data,1051* and plaintext cannot be recovered from the signature.1052* (Signing and MACing)1053*1054* @param hSession the session's handle1055* (PKCS#11 param: CK_SESSION_HANDLE hSession)1056* @param pPart the data part to sign and the data part's length1057* (PKCS#11 param: CK_BYTE_PTR pPart, CK_ULONG ulPartLen)1058* @exception PKCS11Exception If function returns other value than CKR_OK.1059* @preconditions (pPart <> null)1060* @postconditions1061*/1062public native void C_SignUpdate(long hSession, long directIn, byte[] in,1063int inOfs, int inLen) throws PKCS11Exception;106410651066/**1067* C_SignFinal finishes a multiple-part signature operation,1068* returning the signature.1069* (Signing and MACing)1070*1071* @param hSession the session's handle1072* (PKCS#11 param: CK_SESSION_HANDLE hSession)1073* @param expectedLen expected signature length, can be 0 if unknown1074* @return the signature and the signature's length1075* (PKCS#11 param: CK_BYTE_PTR pSignature,1076* CK_ULONG_PTR pulSignatureLen)1077* @exception PKCS11Exception If function returns other value than CKR_OK.1078* @preconditions1079* @postconditions (result <> null)1080*/1081public native byte[] C_SignFinal(long hSession, int expectedLen)1082throws PKCS11Exception;108310841085/**1086* C_SignRecoverInit initializes a signature operation, where1087* the data can be recovered from the signature.1088* (Signing and MACing)1089*1090* @param hSession the session's handle1091* (PKCS#11 param: CK_SESSION_HANDLE hSession)1092* @param pMechanism the signature mechanism1093* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)1094* @param hKey the handle of the signature key1095* (PKCS#11 param: CK_OBJECT_HANDLE hKey)1096* @exception PKCS11Exception If function returns other value than CKR_OK.1097* @preconditions1098* @postconditions1099*/1100public native void C_SignRecoverInit(long hSession, CK_MECHANISM pMechanism,1101long hKey) throws PKCS11Exception;110211031104/**1105* C_SignRecover signs data in a single operation, where the1106* data can be recovered from the signature.1107* (Signing and MACing)1108*1109* @param hSession the session's handle1110* (PKCS#11 param: CK_SESSION_HANDLE hSession)1111* @param pData the data to sign and the data's length1112* (PKCS#11 param: CK_BYTE_PTR pData, CK_ULONG ulDataLen)1113* @return the signature and the signature's length1114* (PKCS#11 param: CK_BYTE_PTR pSignature,1115* CK_ULONG_PTR pulSignatureLen)1116* @exception PKCS11Exception If function returns other value than CKR_OK.1117* @preconditions (pData <> null)1118* @postconditions (result <> null)1119*/1120public native int C_SignRecover(long hSession, byte[] in, int inOfs,1121int inLen, byte[] out, int outOufs, int outLen)1122throws PKCS11Exception;1123112411251126/* *****************************************************************************1127* Verifying signatures and MACs1128******************************************************************************/11291130/**1131* C_VerifyInit initializes a verification operation, where the1132* signature is an appendix to the data, and plaintext cannot1133* cannot be recovered from the signature (e.g. DSA).1134* (Signing and MACing)1135*1136* @param hSession the session's handle1137* (PKCS#11 param: CK_SESSION_HANDLE hSession)1138* @param pMechanism the verification mechanism1139* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)1140* @param hKey the handle of the verification key1141* (PKCS#11 param: CK_OBJECT_HANDLE hKey)1142* @exception PKCS11Exception If function returns other value than CKR_OK.1143* @preconditions1144* @postconditions1145*/1146public native void C_VerifyInit(long hSession, CK_MECHANISM pMechanism,1147long hKey) throws PKCS11Exception;114811491150/**1151* C_Verify verifies a signature in a single-part operation,1152* where the signature is an appendix to the data, and plaintext1153* cannot be recovered from the signature.1154* (Signing and MACing)1155*1156* @param hSession the session's handle1157* (PKCS#11 param: CK_SESSION_HANDLE hSession)1158* @param pData the signed data and the signed data's length1159* (PKCS#11 param: CK_BYTE_PTR pData, CK_ULONG ulDataLen)1160* @param pSignature the signature to verify and the signature's length1161* (PKCS#11 param: CK_BYTE_PTR pSignature, CK_ULONG ulSignatureLen)1162* @exception PKCS11Exception If function returns other value than CKR_OK.1163* @preconditions (pData <> null) and (pSignature <> null)1164* @postconditions1165*/1166public native void C_Verify(long hSession, byte[] pData, byte[] pSignature)1167throws PKCS11Exception;116811691170/**1171* C_VerifyUpdate continues a multiple-part verification1172* operation, where the signature is an appendix to the data,1173* and plaintext cannot be recovered from the signature.1174* (Signing and MACing)1175*1176* @param hSession the session's handle1177* (PKCS#11 param: CK_SESSION_HANDLE hSession)1178* @param pPart the signed data part and the signed data part's length1179* (PKCS#11 param: CK_BYTE_PTR pPart, CK_ULONG ulPartLen)1180* @exception PKCS11Exception If function returns other value than CKR_OK.1181* @preconditions (pPart <> null)1182* @postconditions1183*/1184public native void C_VerifyUpdate(long hSession, long directIn, byte[] in,1185int inOfs, int inLen) throws PKCS11Exception;118611871188/**1189* C_VerifyFinal finishes a multiple-part verification1190* operation, checking the signature.1191* (Signing and MACing)1192*1193* @param hSession the session's handle1194* (PKCS#11 param: CK_SESSION_HANDLE hSession)1195* @param pSignature the signature to verify and the signature's length1196* (PKCS#11 param: CK_BYTE_PTR pSignature, CK_ULONG ulSignatureLen)1197* @exception PKCS11Exception If function returns other value than CKR_OK.1198* @preconditions (pSignature <> null)1199* @postconditions1200*/1201public native void C_VerifyFinal(long hSession, byte[] pSignature)1202throws PKCS11Exception;120312041205/**1206* C_VerifyRecoverInit initializes a signature verification1207* operation, where the data is recovered from the signature.1208* (Signing and MACing)1209*1210* @param hSession the session's handle1211* (PKCS#11 param: CK_SESSION_HANDLE hSession)1212* @param pMechanism the verification mechanism1213* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)1214* @param hKey the handle of the verification key1215* (PKCS#11 param: CK_OBJECT_HANDLE hKey)1216* @exception PKCS11Exception If function returns other value than CKR_OK.1217* @preconditions1218* @postconditions1219*/1220public native void C_VerifyRecoverInit(long hSession,1221CK_MECHANISM pMechanism, long hKey) throws PKCS11Exception;122212231224/**1225* C_VerifyRecover verifies a signature in a single-part1226* operation, where the data is recovered from the signature.1227* (Signing and MACing)1228*1229* @param hSession the session's handle1230* (PKCS#11 param: CK_SESSION_HANDLE hSession)1231* @param pSignature the signature to verify and the signature's length1232* (PKCS#11 param: CK_BYTE_PTR pSignature, CK_ULONG ulSignatureLen)1233* @return the recovered data and the recovered data's length1234* (PKCS#11 param: CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen)1235* @exception PKCS11Exception If function returns other value than CKR_OK.1236* @preconditions (pSignature <> null)1237* @postconditions (result <> null)1238*/1239public native int C_VerifyRecover(long hSession, byte[] in, int inOfs,1240int inLen, byte[] out, int outOufs, int outLen)1241throws PKCS11Exception;1242124312441245/* *****************************************************************************1246* Dual-function cryptographic operations1247******************************************************************************/12481249/**1250* C_DigestEncryptUpdate continues a multiple-part digesting1251* and encryption operation.1252* (Dual-function cryptographic operations)1253*1254* @param hSession the session's handle1255* (PKCS#11 param: CK_SESSION_HANDLE hSession)1256* @param pPart the data part to digest and to encrypt and the data's length1257* (PKCS#11 param: CK_BYTE_PTR pPart, CK_ULONG ulPartLen)1258* @return the digested and encrypted data part and the data part's length1259* (PKCS#11 param: CK_BYTE_PTR pEncryptedPart,1260* CK_ULONG_PTR pulEncryptedPartLen)1261* @exception PKCS11Exception If function returns other value than CKR_OK.1262* @preconditions (pPart <> null)1263* @postconditions1264*/1265// public native byte[] C_DigestEncryptUpdate(long hSession, byte[] pPart)1266// throws PKCS11Exception;126712681269/**1270* C_DecryptDigestUpdate continues a multiple-part decryption and1271* digesting operation.1272* (Dual-function cryptographic operations)1273*1274* @param hSession the session's handle1275* (PKCS#11 param: CK_SESSION_HANDLE hSession)1276* @param pEncryptedPart the encrypted data part to decrypt and to digest1277* and encrypted data part's length1278* (PKCS#11 param: CK_BYTE_PTR pEncryptedPart,1279* CK_ULONG ulEncryptedPartLen)1280* @return the decrypted and digested data part and the data part's length1281* (PKCS#11 param: CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen)1282* @exception PKCS11Exception If function returns other value than CKR_OK.1283* @preconditions (pEncryptedPart <> null)1284* @postconditions1285*/1286// public native byte[] C_DecryptDigestUpdate(long hSession,1287// byte[] pEncryptedPart) throws PKCS11Exception;128812891290/**1291* C_SignEncryptUpdate continues a multiple-part signing and1292* encryption operation.1293* (Dual-function cryptographic operations)1294*1295* @param hSession the session's handle1296* (PKCS#11 param: CK_SESSION_HANDLE hSession)1297* @param pPart the data part to sign and to encrypt and the data part's1298* length1299* (PKCS#11 param: CK_BYTE_PTR pPart, CK_ULONG ulPartLen)1300* @return the signed and encrypted data part and the data part's length1301* (PKCS#11 param: CK_BYTE_PTR pEncryptedPart,1302* CK_ULONG_PTR pulEncryptedPartLen)1303* @exception PKCS11Exception If function returns other value than CKR_OK.1304* @preconditions (pPart <> null)1305* @postconditions1306*/1307// public native byte[] C_SignEncryptUpdate(long hSession, byte[] pPart)1308// throws PKCS11Exception;130913101311/**1312* C_DecryptVerifyUpdate continues a multiple-part decryption and1313* verify operation.1314* (Dual-function cryptographic operations)1315*1316* @param hSession the session's handle1317* (PKCS#11 param: CK_SESSION_HANDLE hSession)1318* @param pEncryptedPart the encrypted data part to decrypt and to verify1319* and the data part's length1320* (PKCS#11 param: CK_BYTE_PTR pEncryptedPart,1321* CK_ULONG ulEncryptedPartLen)1322* @return the decrypted and verified data part and the data part's length1323* (PKCS#11 param: CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen)1324* @exception PKCS11Exception If function returns other value than CKR_OK.1325* @preconditions (pEncryptedPart <> null)1326* @postconditions1327*/1328// public native byte[] C_DecryptVerifyUpdate(long hSession,1329// byte[] pEncryptedPart) throws PKCS11Exception;133013311332/* *****************************************************************************1333* Key management1334******************************************************************************/13351336/**1337* getNativeKeyInfo gets the key object attributes and values as an opaque1338* byte array to be used in createNativeKey method.1339* (Key management)1340*1341* @param hSession the session's handle1342* @param hKey key's handle1343* @param hWrappingKey key handle for wrapping the extracted sensitive keys.1344* -1 if not used.1345* @param pWrappingMech mechanism for wrapping the extracted sensitive keys1346* @return an opaque byte array containing the key object attributes1347* and values1348* @exception PKCS11Exception If an internal PKCS#11 function returns other1349* value than CKR_OK.1350* @preconditions1351* @postconditions1352*/1353public native byte[] getNativeKeyInfo(long hSession, long hKey,1354long hWrappingKey, CK_MECHANISM pWrappingMech) throws PKCS11Exception;13551356/**1357* createNativeKey creates a key object with attributes and values1358* specified by parameter as an opaque byte array.1359* (Key management)1360*1361* @param hSession the session's handle1362* @param keyInfo opaque byte array containing key object attributes1363* and values1364* @param hWrappingKey key handle for unwrapping the extracted sensitive keys.1365* -1 if not used.1366* @param pWrappingMech mechanism for unwrapping the extracted sensitive keys1367* @return key object handle1368* @exception PKCS11Exception If an internal PKCS#11 function returns other1369* value than CKR_OK.1370* @preconditions1371* @postconditions1372*/1373public native long createNativeKey(long hSession, byte[] keyInfo,1374long hWrappingKey, CK_MECHANISM pWrappingMech) throws PKCS11Exception;13751376/**1377* C_GenerateKey generates a secret key, creating a new key1378* object.1379* (Key management)1380*1381* @param hSession the session's handle1382* (PKCS#11 param: CK_SESSION_HANDLE hSession)1383* @param pMechanism the key generation mechanism1384* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)1385* @param pTemplate the template for the new key and the number of1386* attributes in the template1387* (PKCS#11 param: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)1388* @return the handle of the new key1389* (PKCS#11 param: CK_OBJECT_HANDLE_PTR phKey)1390* @exception PKCS11Exception If function returns other value than CKR_OK.1391* @preconditions1392* @postconditions1393*/1394public native long C_GenerateKey(long hSession, CK_MECHANISM pMechanism,1395CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception;139613971398/**1399* C_GenerateKeyPair generates a public-key/private-key pair,1400* creating new key objects.1401* (Key management)1402*1403* @param hSession the session's handle1404* (PKCS#11 param: CK_SESSION_HANDLE hSession)1405* @param pMechanism the key generation mechanism1406* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)1407* @param pPublicKeyTemplate the template for the new public key and the1408* number of attributes in the template1409* (PKCS#11 param: CK_ATTRIBUTE_PTR pPublicKeyTemplate,1410* CK_ULONG ulPublicKeyAttributeCount)1411* @param pPrivateKeyTemplate the template for the new private key and the1412* number of attributes in the template1413* (PKCS#11 param: CK_ATTRIBUTE_PTR pPrivateKeyTemplate1414* CK_ULONG ulPrivateKeyAttributeCount)1415* @return a long array with exactly two elements and the public key handle1416* as the first element and the private key handle as the second1417* element1418* (PKCS#11 param: CK_OBJECT_HANDLE_PTR phPublicKey,1419* CK_OBJECT_HANDLE_PTR phPrivateKey)1420* @exception PKCS11Exception If function returns other value than CKR_OK.1421* @preconditions (pMechanism <> null)1422* @postconditions (result <> null) and (result.length == 2)1423*/1424public native long[] C_GenerateKeyPair(long hSession,1425CK_MECHANISM pMechanism, CK_ATTRIBUTE[] pPublicKeyTemplate,1426CK_ATTRIBUTE[] pPrivateKeyTemplate) throws PKCS11Exception;1427142814291430/**1431* C_WrapKey wraps (i.e., encrypts) a key.1432* (Key management)1433*1434* @param hSession the session's handle1435* (PKCS#11 param: CK_SESSION_HANDLE hSession)1436* @param pMechanism the wrapping mechanism1437* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)1438* @param hWrappingKey the handle of the wrapping key1439* (PKCS#11 param: CK_OBJECT_HANDLE hWrappingKey)1440* @param hKey the handle of the key to be wrapped1441* (PKCS#11 param: CK_OBJECT_HANDLE hKey)1442* @return the wrapped key and the length of the wrapped key1443* (PKCS#11 param: CK_BYTE_PTR pWrappedKey,1444* CK_ULONG_PTR pulWrappedKeyLen)1445* @exception PKCS11Exception If function returns other value than CKR_OK.1446* @preconditions1447* @postconditions (result <> null)1448*/1449public native byte[] C_WrapKey(long hSession, CK_MECHANISM pMechanism,1450long hWrappingKey, long hKey) throws PKCS11Exception;145114521453/**1454* C_UnwrapKey unwraps (decrypts) a wrapped key, creating a new1455* key object.1456* (Key management)1457*1458* @param hSession the session's handle1459* (PKCS#11 param: CK_SESSION_HANDLE hSession)1460* @param pMechanism the unwrapping mechanism1461* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)1462* @param hUnwrappingKey the handle of the unwrapping key1463* (PKCS#11 param: CK_OBJECT_HANDLE hUnwrappingKey)1464* @param pWrappedKey the wrapped key to unwrap and the wrapped key's length1465* (PKCS#11 param: CK_BYTE_PTR pWrappedKey, CK_ULONG ulWrappedKeyLen)1466* @param pTemplate the template for the new key and the number of1467* attributes in the template1468* (PKCS#11 param: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)1469* @return the handle of the unwrapped key1470* (PKCS#11 param: CK_OBJECT_HANDLE_PTR phKey)1471* @exception PKCS11Exception If function returns other value than CKR_OK.1472* @preconditions (pWrappedKey <> null)1473* @postconditions1474*/1475public native long C_UnwrapKey(long hSession, CK_MECHANISM pMechanism,1476long hUnwrappingKey, byte[] pWrappedKey, CK_ATTRIBUTE[] pTemplate)1477throws PKCS11Exception;147814791480/**1481* C_DeriveKey derives a key from a base key, creating a new key1482* object.1483* (Key management)1484*1485* @param hSession the session's handle1486* (PKCS#11 param: CK_SESSION_HANDLE hSession)1487* @param pMechanism the key derivation mechanism1488* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)1489* @param hBaseKey the handle of the base key1490* (PKCS#11 param: CK_OBJECT_HANDLE hBaseKey)1491* @param pTemplate the template for the new key and the number of1492* attributes in the template1493* (PKCS#11 param: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)1494* @return the handle of the derived key1495* (PKCS#11 param: CK_OBJECT_HANDLE_PTR phKey)1496* @exception PKCS11Exception If function returns other value than CKR_OK.1497* @preconditions1498* @postconditions1499*/1500public native long C_DeriveKey(long hSession, CK_MECHANISM pMechanism,1501long hBaseKey, CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception;1502150315041505/* *****************************************************************************1506* Random number generation1507******************************************************************************/15081509/**1510* C_SeedRandom mixes additional seed material into the token's1511* random number generator.1512* (Random number generation)1513*1514* @param hSession the session's handle1515* (PKCS#11 param: CK_SESSION_HANDLE hSession)1516* @param pSeed the seed material and the seed material's length1517* (PKCS#11 param: CK_BYTE_PTR pSeed, CK_ULONG ulSeedLen)1518* @exception PKCS11Exception If function returns other value than CKR_OK.1519* @preconditions (pSeed <> null)1520* @postconditions1521*/1522public native void C_SeedRandom(long hSession, byte[] pSeed)1523throws PKCS11Exception;152415251526/**1527* C_GenerateRandom generates random data.1528* (Random number generation)1529*1530* @param hSession the session's handle1531* (PKCS#11 param: CK_SESSION_HANDLE hSession)1532* @param RandomData receives the random data and the length of RandomData1533* is the length of random data to be generated1534* (PKCS#11 param: CK_BYTE_PTR pRandomData, CK_ULONG ulRandomLen)1535* @exception PKCS11Exception If function returns other value than CKR_OK.1536* @preconditions (randomData <> null)1537* @postconditions1538*/1539public native void C_GenerateRandom(long hSession, byte[] randomData)1540throws PKCS11Exception;1541154215431544/* *****************************************************************************1545* Parallel function management1546******************************************************************************/15471548/**1549* C_GetFunctionStatus is a legacy function; it obtains an1550* updated status of a function running in parallel with an1551* application.1552* (Parallel function management)1553*1554* @param hSession the session's handle1555* (PKCS#11 param: CK_SESSION_HANDLE hSession)1556* @exception PKCS11Exception If function returns other value than CKR_OK.1557* @preconditions1558* @postconditions1559*/1560// public native void C_GetFunctionStatus(long hSession)1561// throws PKCS11Exception;156215631564/**1565* C_CancelFunction is a legacy function; it cancels a function1566* running in parallel.1567* (Parallel function management)1568*1569* @param hSession the session's handle1570* (PKCS#11 param: CK_SESSION_HANDLE hSession)1571* @exception PKCS11Exception If function returns other value than CKR_OK.1572* @preconditions1573* @postconditions1574*/1575// public native void C_CancelFunction(long hSession) throws PKCS11Exception;1576157715781579/* *****************************************************************************1580* Functions added in for Cryptoki Version 2.01 or later1581******************************************************************************/15821583/**1584* C_WaitForSlotEvent waits for a slot event (token insertion,1585* removal, etc.) to occur.1586* (General-purpose)1587*1588* @param flags blocking/nonblocking flag1589* (PKCS#11 param: CK_FLAGS flags)1590* @param pReserved reserved. Should be null1591* (PKCS#11 param: CK_VOID_PTR pReserved)1592* @return the slot ID where the event occurred1593* (PKCS#11 param: CK_SLOT_ID_PTR pSlot)1594* @exception PKCS11Exception If function returns other value than CKR_OK.1595* @preconditions (pRserved == null)1596* @postconditions1597*/1598// public native long C_WaitForSlotEvent(long flags, Object pRserved)1599// throws PKCS11Exception;16001601/**1602* Returns the string representation of this object.1603*1604* @return The string representation of object1605*/1606public String toString() {1607return "Module name: " + pkcs11ModulePath;1608}16091610/**1611* Calls disconnect() to cleanup the native part of the wrapper. Once this1612* method is called, this object cannot be used any longer. Any subsequent1613* call to a C_* method will result in a runtime exception.1614*1615* @exception Throwable If finalization fails.1616*/1617@SuppressWarnings("deprecation")1618protected void finalize() throws Throwable {1619disconnect();1620}16211622// PKCS11 subclass that has all methods synchronized and delegating to the1623// parent. Used for tokens that only support single threaded access1624static class SynchronizedPKCS11 extends PKCS11 {16251626SynchronizedPKCS11(String pkcs11ModulePath, String functionListName)1627throws IOException {1628super(pkcs11ModulePath, functionListName);1629}16301631synchronized void C_Initialize(Object pInitArgs) throws PKCS11Exception {1632super.C_Initialize(pInitArgs);1633}16341635public synchronized void C_Finalize(Object pReserved)1636throws PKCS11Exception {1637super.C_Finalize(pReserved);1638}16391640public synchronized CK_INFO C_GetInfo() throws PKCS11Exception {1641return super.C_GetInfo();1642}16431644public synchronized long[] C_GetSlotList(boolean tokenPresent)1645throws PKCS11Exception {1646return super.C_GetSlotList(tokenPresent);1647}16481649public synchronized CK_SLOT_INFO C_GetSlotInfo(long slotID)1650throws PKCS11Exception {1651return super.C_GetSlotInfo(slotID);1652}16531654public synchronized CK_TOKEN_INFO C_GetTokenInfo(long slotID)1655throws PKCS11Exception {1656return super.C_GetTokenInfo(slotID);1657}16581659public synchronized long[] C_GetMechanismList(long slotID)1660throws PKCS11Exception {1661return super.C_GetMechanismList(slotID);1662}16631664public synchronized CK_MECHANISM_INFO C_GetMechanismInfo(long slotID,1665long type) throws PKCS11Exception {1666return super.C_GetMechanismInfo(slotID, type);1667}16681669public synchronized long C_OpenSession(long slotID, long flags,1670Object pApplication, CK_NOTIFY Notify) throws PKCS11Exception {1671return super.C_OpenSession(slotID, flags, pApplication, Notify);1672}16731674public synchronized void C_CloseSession(long hSession)1675throws PKCS11Exception {1676super.C_CloseSession(hSession);1677}16781679public synchronized CK_SESSION_INFO C_GetSessionInfo(long hSession)1680throws PKCS11Exception {1681return super.C_GetSessionInfo(hSession);1682}16831684public synchronized void C_Login(long hSession, long userType, char[] pPin)1685throws PKCS11Exception {1686super.C_Login(hSession, userType, pPin);1687}16881689public synchronized void C_Logout(long hSession) throws PKCS11Exception {1690super.C_Logout(hSession);1691}16921693public synchronized long C_CreateObject(long hSession,1694CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception {1695return super.C_CreateObject(hSession, pTemplate);1696}16971698public synchronized long C_CopyObject(long hSession, long hObject,1699CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception {1700return super.C_CopyObject(hSession, hObject, pTemplate);1701}17021703public synchronized void C_DestroyObject(long hSession, long hObject)1704throws PKCS11Exception {1705super.C_DestroyObject(hSession, hObject);1706}17071708public synchronized void C_GetAttributeValue(long hSession, long hObject,1709CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception {1710super.C_GetAttributeValue(hSession, hObject, pTemplate);1711}17121713public synchronized void C_SetAttributeValue(long hSession, long hObject,1714CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception {1715super.C_SetAttributeValue(hSession, hObject, pTemplate);1716}17171718public synchronized void C_FindObjectsInit(long hSession,1719CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception {1720super.C_FindObjectsInit(hSession, pTemplate);1721}17221723public synchronized long[] C_FindObjects(long hSession,1724long ulMaxObjectCount) throws PKCS11Exception {1725return super.C_FindObjects(hSession, ulMaxObjectCount);1726}17271728public synchronized void C_FindObjectsFinal(long hSession)1729throws PKCS11Exception {1730super.C_FindObjectsFinal(hSession);1731}17321733public synchronized void C_EncryptInit(long hSession,1734CK_MECHANISM pMechanism, long hKey) throws PKCS11Exception {1735super.C_EncryptInit(hSession, pMechanism, hKey);1736}17371738public synchronized int C_Encrypt(long hSession, long directIn, byte[] in,1739int inOfs, int inLen, long directOut, byte[] out, int outOfs, int outLen)1740throws PKCS11Exception {1741return super.C_Encrypt(hSession, directIn, in, inOfs, inLen,1742directOut, out, outOfs, outLen);1743}17441745public synchronized int C_EncryptUpdate(long hSession, long directIn,1746byte[] in, int inOfs, int inLen, long directOut, byte[] out,1747int outOfs, int outLen) throws PKCS11Exception {1748return super.C_EncryptUpdate(hSession, directIn, in, inOfs, inLen,1749directOut, out, outOfs, outLen);1750}17511752public synchronized int C_EncryptFinal(long hSession, long directOut,1753byte[] out, int outOfs, int outLen) throws PKCS11Exception {1754return super.C_EncryptFinal(hSession, directOut, out, outOfs, outLen);1755}17561757public synchronized void C_DecryptInit(long hSession,1758CK_MECHANISM pMechanism, long hKey) throws PKCS11Exception {1759super.C_DecryptInit(hSession, pMechanism, hKey);1760}17611762public synchronized int C_Decrypt(long hSession, long directIn,1763byte[] in, int inOfs, int inLen, long directOut, byte[] out,1764int outOfs, int outLen) throws PKCS11Exception {1765return super.C_Decrypt(hSession, directIn, in, inOfs, inLen,1766directOut, out, outOfs, outLen);1767}17681769public synchronized int C_DecryptUpdate(long hSession, long directIn,1770byte[] in, int inOfs, int inLen, long directOut, byte[] out,1771int outOfs, int outLen) throws PKCS11Exception {1772return super.C_DecryptUpdate(hSession, directIn, in, inOfs, inLen,1773directOut, out, outOfs, outLen);1774}17751776public synchronized int C_DecryptFinal(long hSession, long directOut,1777byte[] out, int outOfs, int outLen) throws PKCS11Exception {1778return super.C_DecryptFinal(hSession, directOut, out, outOfs, outLen);1779}17801781public synchronized void C_DigestInit(long hSession, CK_MECHANISM pMechanism)1782throws PKCS11Exception {1783super.C_DigestInit(hSession, pMechanism);1784}17851786public synchronized int C_DigestSingle(long hSession,1787CK_MECHANISM pMechanism, byte[] in, int inOfs, int inLen,1788byte[] digest, int digestOfs, int digestLen) throws PKCS11Exception {1789return super.C_DigestSingle(hSession, pMechanism, in, inOfs, inLen,1790digest, digestOfs, digestLen);1791}17921793public synchronized void C_DigestUpdate(long hSession, long directIn,1794byte[] in, int inOfs, int inLen) throws PKCS11Exception {1795super.C_DigestUpdate(hSession, directIn, in, inOfs, inLen);1796}17971798public synchronized void C_DigestKey(long hSession, long hKey)1799throws PKCS11Exception {1800super.C_DigestKey(hSession, hKey);1801}18021803public synchronized int C_DigestFinal(long hSession, byte[] pDigest,1804int digestOfs, int digestLen) throws PKCS11Exception {1805return super.C_DigestFinal(hSession, pDigest, digestOfs, digestLen);1806}18071808public synchronized void C_SignInit(long hSession, CK_MECHANISM pMechanism,1809long hKey) throws PKCS11Exception {1810super.C_SignInit(hSession, pMechanism, hKey);1811}18121813public synchronized byte[] C_Sign(long hSession, byte[] pData)1814throws PKCS11Exception {1815return super.C_Sign(hSession, pData);1816}18171818public synchronized void C_SignUpdate(long hSession, long directIn,1819byte[] in, int inOfs, int inLen) throws PKCS11Exception {1820super.C_SignUpdate(hSession, directIn, in, inOfs, inLen);1821}18221823public synchronized byte[] C_SignFinal(long hSession, int expectedLen)1824throws PKCS11Exception {1825return super.C_SignFinal(hSession, expectedLen);1826}18271828public synchronized void C_SignRecoverInit(long hSession,1829CK_MECHANISM pMechanism, long hKey) throws PKCS11Exception {1830super.C_SignRecoverInit(hSession, pMechanism, hKey);1831}18321833public synchronized int C_SignRecover(long hSession, byte[] in, int inOfs,1834int inLen, byte[] out, int outOufs, int outLen)1835throws PKCS11Exception {1836return super.C_SignRecover(hSession, in, inOfs, inLen, out, outOufs,1837outLen);1838}18391840public synchronized void C_VerifyInit(long hSession, CK_MECHANISM pMechanism,1841long hKey) throws PKCS11Exception {1842super.C_VerifyInit(hSession, pMechanism, hKey);1843}18441845public synchronized void C_Verify(long hSession, byte[] pData,1846byte[] pSignature) throws PKCS11Exception {1847super.C_Verify(hSession, pData, pSignature);1848}18491850public synchronized void C_VerifyUpdate(long hSession, long directIn,1851byte[] in, int inOfs, int inLen) throws PKCS11Exception {1852super.C_VerifyUpdate(hSession, directIn, in, inOfs, inLen);1853}18541855public synchronized void C_VerifyFinal(long hSession, byte[] pSignature)1856throws PKCS11Exception {1857super.C_VerifyFinal(hSession, pSignature);1858}18591860public synchronized void C_VerifyRecoverInit(long hSession,1861CK_MECHANISM pMechanism, long hKey) throws PKCS11Exception {1862super.C_VerifyRecoverInit(hSession, pMechanism, hKey);1863}18641865public synchronized int C_VerifyRecover(long hSession, byte[] in, int inOfs,1866int inLen, byte[] out, int outOufs, int outLen)1867throws PKCS11Exception {1868return super.C_VerifyRecover(hSession, in, inOfs, inLen, out, outOufs,1869outLen);1870}18711872public synchronized long C_GenerateKey(long hSession,1873CK_MECHANISM pMechanism, CK_ATTRIBUTE[] pTemplate)1874throws PKCS11Exception {1875return super.C_GenerateKey(hSession, pMechanism, pTemplate);1876}18771878public synchronized long[] C_GenerateKeyPair(long hSession,1879CK_MECHANISM pMechanism, CK_ATTRIBUTE[] pPublicKeyTemplate,1880CK_ATTRIBUTE[] pPrivateKeyTemplate)1881throws PKCS11Exception {1882return super.C_GenerateKeyPair(hSession, pMechanism, pPublicKeyTemplate,1883pPrivateKeyTemplate);1884}18851886public synchronized byte[] C_WrapKey(long hSession, CK_MECHANISM pMechanism,1887long hWrappingKey, long hKey) throws PKCS11Exception {1888return super.C_WrapKey(hSession, pMechanism, hWrappingKey, hKey);1889}18901891public synchronized long C_UnwrapKey(long hSession, CK_MECHANISM pMechanism,1892long hUnwrappingKey, byte[] pWrappedKey, CK_ATTRIBUTE[] pTemplate)1893throws PKCS11Exception {1894return super.C_UnwrapKey(hSession, pMechanism, hUnwrappingKey,1895pWrappedKey, pTemplate);1896}18971898public synchronized long C_DeriveKey(long hSession, CK_MECHANISM pMechanism,1899long hBaseKey, CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception {1900return super.C_DeriveKey(hSession, pMechanism, hBaseKey, pTemplate);1901}19021903public synchronized void C_SeedRandom(long hSession, byte[] pSeed)1904throws PKCS11Exception {1905super.C_SeedRandom(hSession, pSeed);1906}19071908public synchronized void C_GenerateRandom(long hSession, byte[] randomData)1909throws PKCS11Exception {1910super.C_GenerateRandom(hSession, randomData);1911}1912}1913}191419151916