Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/pkcs11/wrapper/PKCS11.java
38920 views
/*1* Copyright (c) 2003, 2019, 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 static sun.security.pkcs11.wrapper.PKCS11Constants.*;5758/**59* This is the default implementation of the PKCS11 interface. IT connects to60* the pkcs11wrapper.dll file, which is the native part of this library.61* The strange and awkward looking initialization was chosen to avoid calling62* loadLibrary from a static initialization block, because this would complicate63* the use in applets.64*65* @author Karl Scheibelhofer <[email protected]>66* @author Martin Schlaeffer <[email protected]>67* @invariants (pkcs11ModulePath_ <> null)68*/69public class PKCS11 {7071/**72* The name of the native part of the wrapper; i.e. the filename without73* the extension (e.g. ".DLL" or ".so").74*/75private static final String PKCS11_WRAPPER = "j2pkcs11";7677static {78// cannot use LoadLibraryAction because that would make the native79// library available to the bootclassloader, but we run in the80// extension classloader.81AccessController.doPrivileged(new PrivilegedAction<Object>() {82public Object run() {83System.loadLibrary(PKCS11_WRAPPER);84return null;85}86});87initializeLibrary();88}8990public static void loadNative() {91// dummy method that can be called to make sure the native92// portion has been loaded. actual loading happens in the93// static initializer, hence this method is empty.94}9596/* *****************************************************************************97* Utility, Resource Clean up98******************************************************************************/99// always return 0L100public static native long freeMechanism(long hMechanism);101102/**103* The PKCS#11 module to connect to. This is the PKCS#11 driver of the token;104* e.g. pk2priv.dll.105*/106private final String pkcs11ModulePath;107108private long pNativeData;109110/**111* This method does the initialization of the native library. It is called112* exactly once for this class.113*114* @preconditions115* @postconditions116*/117private static native void initializeLibrary();118119// XXX120/**121* This method does the finalization of the native library. It is called122* exactly once for this class. The library uses this method for a clean-up123* of any resources.124*125* @preconditions126* @postconditions127*/128private static native void finalizeLibrary();129130private static final Map<String,PKCS11> moduleMap =131new HashMap<String,PKCS11>();132133/**134* Connects to the PKCS#11 driver given. The filename must contain the135* path, if the driver is not in the system's search path.136*137* @param pkcs11ModulePath the PKCS#11 library path138* @preconditions (pkcs11ModulePath <> null)139* @postconditions140*/141PKCS11(String pkcs11ModulePath, String functionListName)142throws IOException {143connect(pkcs11ModulePath, functionListName);144this.pkcs11ModulePath = pkcs11ModulePath;145}146147public static synchronized PKCS11 getInstance(String pkcs11ModulePath,148String functionList, CK_C_INITIALIZE_ARGS pInitArgs,149boolean omitInitialize) throws IOException, PKCS11Exception {150// we may only call C_Initialize once per native .so/.dll151// so keep a cache using the (non-canonicalized!) path152PKCS11 pkcs11 = moduleMap.get(pkcs11ModulePath);153if (pkcs11 == null) {154if ((pInitArgs != null)155&& ((pInitArgs.flags & CKF_OS_LOCKING_OK) != 0)) {156pkcs11 = new PKCS11(pkcs11ModulePath, functionList);157} else {158pkcs11 = new SynchronizedPKCS11(pkcs11ModulePath, functionList);159}160if (omitInitialize == false) {161try {162pkcs11.C_Initialize(pInitArgs);163} catch (PKCS11Exception e) {164// ignore already-initialized error code165// rethrow all other errors166if (e.getErrorCode() != CKR_CRYPTOKI_ALREADY_INITIALIZED) {167throw e;168}169}170}171moduleMap.put(pkcs11ModulePath, pkcs11);172}173return pkcs11;174}175176/**177* Connects this object to the specified PKCS#11 library. This method is for178* internal use only.179* Declared private, because incorrect handling may result in errors in the180* native part.181*182* @param pkcs11ModulePath The PKCS#11 library path.183* @preconditions (pkcs11ModulePath <> null)184* @postconditions185*/186private native void connect(String pkcs11ModulePath, String functionListName)187throws IOException;188189/**190* Disconnects the PKCS#11 library from this object. After calling this191* method, this object is no longer connected to a native PKCS#11 module192* and any subsequent calls to C_ methods will fail. This method is for193* internal use only.194* Declared private, because incorrect handling may result in errors in the195* native part.196*197* @preconditions198* @postconditions199*/200private native void disconnect();201202203// Implementation of PKCS11 methods delegated to native pkcs11wrapper library204205/* *****************************************************************************206* General-purpose207******************************************************************************/208209/**210* C_Initialize initializes the Cryptoki library.211* (General-purpose)212*213* @param pInitArgs if pInitArgs is not NULL it gets casted to214* CK_C_INITIALIZE_ARGS_PTR and dereferenced215* (PKCS#11 param: CK_VOID_PTR pInitArgs)216* @exception PKCS11Exception If function returns other value than CKR_OK.217* @preconditions218* @postconditions219*/220native void C_Initialize(Object pInitArgs) throws PKCS11Exception;221222/**223* C_Finalize indicates that an application is done with the224* Cryptoki library225* (General-purpose)226*227* @param pReserved is reserved. Should be NULL_PTR228* (PKCS#11 param: CK_VOID_PTR pReserved)229* @exception PKCS11Exception If function returns other value than CKR_OK.230* @preconditions (pReserved == null)231* @postconditions232*/233public native void C_Finalize(Object pReserved) throws PKCS11Exception;234235236/**237* C_GetInfo returns general information about Cryptoki.238* (General-purpose)239*240* @return the information.241* (PKCS#11 param: CK_INFO_PTR pInfo)242* @exception PKCS11Exception If function returns other value than CKR_OK.243* @preconditions244* @postconditions (result <> null)245*/246public native CK_INFO C_GetInfo() throws PKCS11Exception;247248249/* *****************************************************************************250* Slot and token management251******************************************************************************/252253/**254* C_GetSlotList obtains a list of slots in the system.255* (Slot and token management)256*257* @param tokenPresent if true only Slot IDs with a token are returned258* (PKCS#11 param: CK_BBOOL tokenPresent)259* @return a long array of slot IDs and number of Slot IDs260* (PKCS#11 param: CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount)261* @exception PKCS11Exception If function returns other value than CKR_OK.262* @preconditions263* @postconditions (result <> null)264*/265public native long[] C_GetSlotList(boolean tokenPresent)266throws PKCS11Exception;267268269/**270* C_GetSlotInfo obtains information about a particular slot in271* the system.272* (Slot and token management)273*274* @param slotID the ID of the slot275* (PKCS#11 param: CK_SLOT_ID slotID)276* @return the slot information277* (PKCS#11 param: CK_SLOT_INFO_PTR pInfo)278* @exception PKCS11Exception If function returns other value than CKR_OK.279* @preconditions280* @postconditions (result <> null)281*/282public native CK_SLOT_INFO C_GetSlotInfo(long slotID) throws PKCS11Exception;283284285/**286* C_GetTokenInfo obtains information about a particular token287* in the system.288* (Slot and token management)289*290* @param slotID ID of the token's slot291* (PKCS#11 param: CK_SLOT_ID slotID)292* @return the token information293* (PKCS#11 param: CK_TOKEN_INFO_PTR pInfo)294* @exception PKCS11Exception If function returns other value than CKR_OK.295* @preconditions296* @postconditions (result <> null)297*/298public native CK_TOKEN_INFO C_GetTokenInfo(long slotID)299throws PKCS11Exception;300301302/**303* C_GetMechanismList obtains a list of mechanism types304* supported by a token.305* (Slot and token management)306*307* @param slotID ID of the token's slot308* (PKCS#11 param: CK_SLOT_ID slotID)309* @return a long array of mechanism types and number of mechanism types310* (PKCS#11 param: CK_MECHANISM_TYPE_PTR pMechanismList,311* CK_ULONG_PTR pulCount)312* @exception PKCS11Exception If function returns other value than CKR_OK.313* @preconditions314* @postconditions (result <> null)315*/316public native long[] C_GetMechanismList(long slotID) throws PKCS11Exception;317318319/**320* C_GetMechanismInfo obtains information about a particular321* mechanism possibly supported by a token.322* (Slot and token management)323*324* @param slotID ID of the token's slot325* (PKCS#11 param: CK_SLOT_ID slotID)326* @param type type of mechanism327* (PKCS#11 param: CK_MECHANISM_TYPE type)328* @return the mechanism info329* (PKCS#11 param: CK_MECHANISM_INFO_PTR pInfo)330* @exception PKCS11Exception If function returns other value than CKR_OK.331* @preconditions332* @postconditions (result <> null)333*/334public native CK_MECHANISM_INFO C_GetMechanismInfo(long slotID, long type)335throws PKCS11Exception;336337338/**339* C_InitToken initializes a token.340* (Slot and token management)341*342* @param slotID ID of the token's slot343* (PKCS#11 param: CK_SLOT_ID slotID)344* @param pPin the SO's initial PIN and the length in bytes of the PIN345* (PKCS#11 param: CK_CHAR_PTR pPin, CK_ULONG ulPinLen)346* @param pLabel 32-byte token label (blank padded)347* (PKCS#11 param: CK_UTF8CHAR_PTR pLabel)348* @exception PKCS11Exception If function returns other value than CKR_OK.349* @preconditions350* @postconditions351*/352// public native void C_InitToken(long slotID, char[] pPin, char[] pLabel)353// throws PKCS11Exception;354355356/**357* C_InitPIN initializes the normal user's PIN.358* (Slot and token management)359*360* @param hSession the session's handle361* (PKCS#11 param: CK_SESSION_HANDLE hSession)362* @param pPin the normal user's PIN and the length in bytes of the PIN363* (PKCS#11 param: CK_CHAR_PTR pPin, CK_ULONG ulPinLen)364* @exception PKCS11Exception If function returns other value than CKR_OK.365* @preconditions366* @postconditions367*/368// public native void C_InitPIN(long hSession, char[] pPin)369// throws PKCS11Exception;370371372/**373* C_SetPIN modifies the PIN of the user who is logged in.374* (Slot and token management)375*376* @param hSession the session's handle377* (PKCS#11 param: CK_SESSION_HANDLE hSession)378* @param pOldPin the old PIN and the length of the old PIN379* (PKCS#11 param: CK_CHAR_PTR pOldPin, CK_ULONG ulOldLen)380* @param pNewPin the new PIN and the length of the new PIN381* (PKCS#11 param: CK_CHAR_PTR pNewPin, CK_ULONG ulNewLen)382* @exception PKCS11Exception If function returns other value than CKR_OK.383* @preconditions384* @postconditions385*/386// public native void C_SetPIN(long hSession, char[] pOldPin, char[] pNewPin)387// throws PKCS11Exception;388389390391/* *****************************************************************************392* Session management393******************************************************************************/394395/**396* C_OpenSession opens a session between an application and a397* token.398* (Session management)399*400* @param slotID the slot's ID401* (PKCS#11 param: CK_SLOT_ID slotID)402* @param flags of CK_SESSION_INFO403* (PKCS#11 param: CK_FLAGS flags)404* @param pApplication passed to callback405* (PKCS#11 param: CK_VOID_PTR pApplication)406* @param Notify the callback function407* (PKCS#11 param: CK_NOTIFY Notify)408* @return the session handle409* (PKCS#11 param: CK_SESSION_HANDLE_PTR phSession)410* @exception PKCS11Exception If function returns other value than CKR_OK.411* @preconditions412* @postconditions413*/414public native long C_OpenSession(long slotID, long flags,415Object pApplication, CK_NOTIFY Notify) throws PKCS11Exception;416417418/**419* C_CloseSession closes a session between an application and a420* token.421* (Session management)422*423* @param hSession the session's handle424* (PKCS#11 param: CK_SESSION_HANDLE hSession)425* @exception PKCS11Exception If function returns other value than CKR_OK.426* @preconditions427* @postconditions428*/429public native void C_CloseSession(long hSession) throws PKCS11Exception;430431432/**433* C_CloseAllSessions closes all sessions with a token.434* (Session management)435*436* @param slotID the ID of the token's slot437* (PKCS#11 param: CK_SLOT_ID slotID)438* @exception PKCS11Exception If function returns other value than CKR_OK.439* @preconditions440* @postconditions441*/442// public native void C_CloseAllSessions(long slotID) throws PKCS11Exception;443444445/**446* C_GetSessionInfo obtains information about the session.447* (Session management)448*449* @param hSession the session's handle450* (PKCS#11 param: CK_SESSION_HANDLE hSession)451* @return the session info452* (PKCS#11 param: CK_SESSION_INFO_PTR pInfo)453* @exception PKCS11Exception If function returns other value than CKR_OK.454* @preconditions455* @postconditions (result <> null)456*/457public native CK_SESSION_INFO C_GetSessionInfo(long hSession)458throws PKCS11Exception;459460461/**462* C_GetOperationState obtains the state of the cryptographic operation463* in a session.464* (Session management)465*466* @param hSession session's handle467* (PKCS#11 param: CK_SESSION_HANDLE hSession)468* @return the state and the state length469* (PKCS#11 param: CK_BYTE_PTR pOperationState,470* CK_ULONG_PTR pulOperationStateLen)471* @exception PKCS11Exception If function returns other value than CKR_OK.472* @preconditions473* @postconditions (result <> null)474*/475public native byte[] C_GetOperationState(long hSession)476throws PKCS11Exception;477478479/**480* C_SetOperationState restores the state of the cryptographic481* operation in a session.482* (Session management)483*484* @param hSession session's handle485* (PKCS#11 param: CK_SESSION_HANDLE hSession)486* @param pOperationState the state and the state length487* (PKCS#11 param: CK_BYTE_PTR pOperationState,488* CK_ULONG ulOperationStateLen)489* @param hEncryptionKey en/decryption key490* (PKCS#11 param: CK_OBJECT_HANDLE hEncryptionKey)491* @param hAuthenticationKey sign/verify key492* (PKCS#11 param: CK_OBJECT_HANDLE hAuthenticationKey)493* @exception PKCS11Exception If function returns other value than CKR_OK.494* @preconditions495* @postconditions496*/497public native void C_SetOperationState(long hSession, byte[] pOperationState,498long hEncryptionKey, long hAuthenticationKey) throws PKCS11Exception;499500501/**502* C_Login logs a user into a token.503* (Session management)504*505* @param hSession the session's handle506* (PKCS#11 param: CK_SESSION_HANDLE hSession)507* @param userType the user type508* (PKCS#11 param: CK_USER_TYPE userType)509* @param pPin the user's PIN and the length of the PIN510* (PKCS#11 param: CK_CHAR_PTR pPin, CK_ULONG ulPinLen)511* @exception PKCS11Exception If function returns other value than CKR_OK.512* @preconditions513* @postconditions514*/515public native void C_Login(long hSession, long userType, char[] pPin)516throws PKCS11Exception;517518519/**520* C_Logout logs a user out from a token.521* (Session management)522*523* @param hSession the session's handle524* (PKCS#11 param: CK_SESSION_HANDLE hSession)525* @exception PKCS11Exception If function returns other value than CKR_OK.526* @preconditions527* @postconditions528*/529public native void C_Logout(long hSession) throws PKCS11Exception;530531532533/* *****************************************************************************534* Object management535******************************************************************************/536537/**538* C_CreateObject creates a new object.539* (Object management)540*541* @param hSession the session's handle542* (PKCS#11 param: CK_SESSION_HANDLE hSession)543* @param pTemplate the object's template and number of attributes in544* template545* (PKCS#11 param: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)546* @return the object's handle547* (PKCS#11 param: CK_OBJECT_HANDLE_PTR phObject)548* @exception PKCS11Exception If function returns other value than CKR_OK.549* @preconditions550* @postconditions551*/552public native long C_CreateObject(long hSession, CK_ATTRIBUTE[] pTemplate)553throws PKCS11Exception;554555556/**557* C_CopyObject copies an object, creating a new object for the558* copy.559* (Object management)560*561* @param hSession the session's handle562* (PKCS#11 param: CK_SESSION_HANDLE hSession)563* @param hObject the object's handle564* (PKCS#11 param: CK_OBJECT_HANDLE hObject)565* @param pTemplate the template for the new object and number of attributes566* in template567* (PKCS#11 param: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)568* @return the handle of the copy569* (PKCS#11 param: CK_OBJECT_HANDLE_PTR phNewObject)570* @exception PKCS11Exception If function returns other value than CKR_OK.571* @preconditions572* @postconditions573*/574public native long C_CopyObject(long hSession, long hObject,575CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception;576577578/**579* C_DestroyObject destroys an object.580* (Object management)581*582* @param hSession the session's handle583* (PKCS#11 param: CK_SESSION_HANDLE hSession)584* @param hObject the object's handle585* (PKCS#11 param: CK_OBJECT_HANDLE hObject)586* @exception PKCS11Exception If function returns other value than CKR_OK.587* @preconditions588* @postconditions589*/590public native void C_DestroyObject(long hSession, long hObject)591throws PKCS11Exception;592593594/**595* C_GetObjectSize gets the size of an object in bytes.596* (Object management)597*598* @param hSession the session's handle599* (PKCS#11 param: CK_SESSION_HANDLE hSession)600* @param hObject the object's handle601* (PKCS#11 param: CK_OBJECT_HANDLE hObject)602* @return the size of the object603* (PKCS#11 param: CK_ULONG_PTR pulSize)604* @exception PKCS11Exception If function returns other value than CKR_OK.605* @preconditions606* @postconditions607*/608// public native long C_GetObjectSize(long hSession, long hObject)609// throws PKCS11Exception;610611612/**613* C_GetAttributeValue obtains the value of one or more object614* attributes. The template attributes also receive the values.615* (Object management)616* note: in PKCS#11 pTemplate and the result template are the same617*618* @param hSession the session's handle619* (PKCS#11 param: CK_SESSION_HANDLE hSession)620* @param hObject the object's handle621* (PKCS#11 param: CK_OBJECT_HANDLE hObject)622* @param pTemplate specifies the attributes and number of attributes to get623* The template attributes also receive the values.624* (PKCS#11 param: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)625* @exception PKCS11Exception If function returns other value than CKR_OK.626* @preconditions (pTemplate <> null)627* @postconditions (result <> null)628*/629public native void C_GetAttributeValue(long hSession, long hObject,630CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception;631632633/**634* C_SetAttributeValue modifies the value of one or more object635* attributes636* (Object management)637*638* @param hSession the session's handle639* (PKCS#11 param: CK_SESSION_HANDLE hSession)640* @param hObject the object's handle641* (PKCS#11 param: CK_OBJECT_HANDLE hObject)642* @param pTemplate specifies the attributes and values to get; number of643* attributes in the template644* (PKCS#11 param: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)645* @exception PKCS11Exception If function returns other value than CKR_OK.646* @preconditions (pTemplate <> null)647* @postconditions648*/649public native void C_SetAttributeValue(long hSession, long hObject,650CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception;651652653/**654* C_FindObjectsInit initializes a search for token and session655* objects that match a template.656* (Object management)657*658* @param hSession the session's handle659* (PKCS#11 param: CK_SESSION_HANDLE hSession)660* @param pTemplate the object's attribute values to match and the number of661* attributes in search template662* (PKCS#11 param: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)663* @exception PKCS11Exception If function returns other value than CKR_OK.664* @preconditions665* @postconditions666*/667public native void C_FindObjectsInit(long hSession, CK_ATTRIBUTE[] pTemplate)668throws PKCS11Exception;669670671/**672* C_FindObjects continues a search for token and session673* objects that match a template, obtaining additional object674* handles.675* (Object management)676*677* @param hSession the session's handle678* (PKCS#11 param: CK_SESSION_HANDLE hSession)679* @param ulMaxObjectCount the max. object handles to get680* (PKCS#11 param: CK_ULONG ulMaxObjectCount)681* @return the object's handles and the actual number of objects returned682* (PKCS#11 param: CK_ULONG_PTR pulObjectCount)683* @exception PKCS11Exception If function returns other value than CKR_OK.684* @preconditions685* @postconditions (result <> null)686*/687public native long[] C_FindObjects(long hSession, long ulMaxObjectCount)688throws PKCS11Exception;689690691/**692* C_FindObjectsFinal finishes a search for token and session693* objects.694* (Object management)695*696* @param hSession the session's handle697* (PKCS#11 param: CK_SESSION_HANDLE hSession)698* @exception PKCS11Exception If function returns other value than CKR_OK.699* @preconditions700* @postconditions701*/702public native void C_FindObjectsFinal(long hSession) throws PKCS11Exception;703704705706/* *****************************************************************************707* Encryption and decryption708******************************************************************************/709710/**711* C_EncryptInit initializes an encryption operation.712* (Encryption and decryption)713*714* @param hSession the session's handle715* (PKCS#11 param: CK_SESSION_HANDLE hSession)716* @param pMechanism the encryption mechanism717* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)718* @param hKey the handle of the encryption key719* (PKCS#11 param: CK_OBJECT_HANDLE hKey)720* @exception PKCS11Exception If function returns other value than CKR_OK.721* @preconditions722* @postconditions723*/724public native void C_EncryptInit(long hSession, CK_MECHANISM pMechanism,725long hKey) throws PKCS11Exception;726727728/**729* C_Encrypt encrypts single-part data.730* (Encryption and decryption)731*732* @param hSession the session's handle733* (PKCS#11 param: CK_SESSION_HANDLE hSession)734* @param directIn the address of the to-be-encrypted data735* @param in buffer containing the to-be-encrypted data736* @param inOfs buffer offset of the to-be-encrypted data737* @param inLen length of the to-be-encrypted data738* (PKCS#11 param: CK_BYTE_PTR pData, CK_ULONG ulDataLen)739* @param directOut the address for the encrypted data740* @param out buffer for the encrypted data741* @param outOfs buffer offset for the encrypted data742* @param outLen buffer size for the encrypted data743* @return the length of encrypted data744* (PKCS#11 param: CK_BYTE_PTR pEncryptedData,745* CK_ULONG_PTR pulEncryptedDataLen)746* @exception PKCS11Exception If function returns other value than CKR_OK.747* @preconditions748* @postconditions749*/750public native int C_Encrypt(long hSession, long directIn, byte[] in,751int inOfs, int inLen, long directOut, byte[] out, int outOfs,752int outLen) throws PKCS11Exception;753754755/**756* C_EncryptUpdate continues a multiple-part encryption757* operation.758* (Encryption and decryption)759*760* @param hSession the session's handle761* (PKCS#11 param: CK_SESSION_HANDLE hSession)762* @param directIn the address of the to-be-encrypted data763* @param in buffer containing the to-be-encrypted data764* @param inOfs buffer offset of the to-be-encrypted data765* @param inLen length of the to-be-encrypted data766* (PKCS#11 param: CK_BYTE_PTR pPart, CK_ULONG ulPartLen)767* @param directOut the address for the encrypted data768* @param out buffer for the encrypted data769* @param outOfs buffer offset for the encrypted data770* @param outLen buffer size for the encrypted data771* @return the length of encrypted data for this update772* (PKCS#11 param: CK_BYTE_PTR pEncryptedPart,773CK_ULONG_PTR pulEncryptedPartLen)774* @exception PKCS11Exception If function returns other value than CKR_OK.775* @preconditions776* @postconditions777*/778public native int C_EncryptUpdate(long hSession, long directIn, byte[] in,779int inOfs, int inLen, long directOut, byte[] out, int outOfs,780int outLen) throws PKCS11Exception;781782783/**784* C_EncryptFinal finishes a multiple-part encryption785* operation.786* (Encryption and decryption)787*788* @param hSession the session's handle789* (PKCS#11 param: CK_SESSION_HANDLE hSession)790* @param directOut the address for the encrypted data791* @param out buffer for the encrypted data792* @param outOfs buffer offset for the encrypted data793* @param outLen buffer size for the encrypted data794* @return the length of the last part of the encrypted data795* (PKCS#11 param: CK_BYTE_PTR pLastEncryptedPart,796CK_ULONG_PTR pulLastEncryptedPartLen)797* @exception PKCS11Exception If function returns other value than CKR_OK.798* @preconditions799* @postconditions800*/801public native int C_EncryptFinal(long hSession, long directOut, byte[] out,802int outOfs, int outLen) throws PKCS11Exception;803804805/**806* C_DecryptInit initializes a decryption operation.807* (Encryption and decryption)808*809* @param hSession the session's handle810* (PKCS#11 param: CK_SESSION_HANDLE hSession)811* @param pMechanism the decryption mechanism812* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)813* @param hKey the handle of the decryption key814* (PKCS#11 param: CK_OBJECT_HANDLE hKey)815* @exception PKCS11Exception If function returns other value than CKR_OK.816* @preconditions817* @postconditions818*/819public native void C_DecryptInit(long hSession, CK_MECHANISM pMechanism,820long hKey) throws PKCS11Exception;821822823/**824* C_Decrypt decrypts encrypted data in a single part.825* (Encryption and decryption)826*827* @param hSession the session's handle828* (PKCS#11 param: CK_SESSION_HANDLE hSession)829* @param directIn the address of the to-be-decrypted data830* @param in buffer containing the to-be-decrypted data831* @param inOfs buffer offset of the to-be-decrypted data832* @param inLen length of the to-be-decrypted data833* (PKCS#11 param: CK_BYTE_PTR pDecryptedData,834* CK_ULONG ulDecryptedDataLen)835* @param directOut the address for the decrypted data836* @param out buffer for the decrypted data837* @param outOfs buffer offset for the decrypted data838* @param outLen buffer size for the decrypted data839* @return the length of decrypted data840* (PKCS#11 param: CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen)841* @exception PKCS11Exception If function returns other value than CKR_OK.842* @preconditions843* @postconditions844*/845public native int C_Decrypt(long hSession, long directIn, byte[] in,846int inOfs, int inLen, long directOut, byte[] out, int outOfs,847int outLen) throws PKCS11Exception;848849850/**851* C_DecryptUpdate continues a multiple-part decryption852* operation.853* (Encryption and decryption)854*855* @param hSession the session's handle856* (PKCS#11 param: CK_SESSION_HANDLE hSession)857* @param directIn the address of the to-be-decrypted data858* @param in buffer containing the to-be-decrypted data859* @param inOfs buffer offset of the to-be-decrypted data860* @param inLen length of the to-be-decrypted data861* (PKCS#11 param: CK_BYTE_PTR pDecryptedPart,862* CK_ULONG ulDecryptedPartLen)863* @param directOut the address for the decrypted data864* @param out buffer for the decrypted data865* @param outOfs buffer offset for the decrypted data866* @param outLen buffer size for the decrypted data867* @return the length of decrypted data for this update868* (PKCS#11 param: CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen)869* @exception PKCS11Exception If function returns other value than CKR_OK.870* @preconditions871* @postconditions872*/873public native int C_DecryptUpdate(long hSession, long directIn, byte[] in,874int inOfs, int inLen, long directOut, byte[] out, int outOfs,875int outLen) throws PKCS11Exception;876877878/**879* C_DecryptFinal finishes a multiple-part decryption880* operation.881* (Encryption and decryption)882*883* @param hSession the session's handle884* (PKCS#11 param: CK_SESSION_HANDLE hSession)885* @param directOut the address for the decrypted data886* @param out buffer for the decrypted data887* @param outOfs buffer offset for the decrypted data888* @param outLen buffer size for the decrypted data889* @return the length of this last part of decrypted data890* (PKCS#11 param: CK_BYTE_PTR pLastPart,891* CK_ULONG_PTR pulLastPartLen)892* @exception PKCS11Exception If function returns other value than CKR_OK.893* @preconditions894* @postconditions895*/896public native int C_DecryptFinal(long hSession, long directOut, byte[] out,897int outOfs, int outLen) throws PKCS11Exception;898899900901/* *****************************************************************************902* Message digesting903******************************************************************************/904905/**906* C_DigestInit initializes a message-digesting operation.907* (Message digesting)908*909* @param hSession the session's handle910* (PKCS#11 param: CK_SESSION_HANDLE hSession)911* @param pMechanism the digesting mechanism912* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)913* @exception PKCS11Exception If function returns other value than CKR_OK.914* @preconditions915* @postconditions916*/917public native void C_DigestInit(long hSession, CK_MECHANISM pMechanism)918throws PKCS11Exception;919920921// note that C_DigestSingle does not exist in PKCS#11922// we combined the C_DigestInit and C_Digest into a single function923// to save on Java<->C transitions and save 5-10% on small digests924// this made the C_Digest method redundant, it has been removed925/**926* C_Digest digests data in a single part.927* (Message digesting)928*929* @param hSession the session's handle930* (PKCS#11 param: CK_SESSION_HANDLE hSession)931* @param data the data to get digested and the data's length932* (PKCS#11 param: CK_BYTE_PTR pData, CK_ULONG ulDataLen)933* @return the message digest and the length of the message digest934* (PKCS#11 param: CK_BYTE_PTR pDigest, CK_ULONG_PTR pulDigestLen)935* @exception PKCS11Exception If function returns other value than CKR_OK.936* @preconditions (data <> null)937* @postconditions (result <> null)938*/939public native int C_DigestSingle(long hSession, CK_MECHANISM pMechanism,940byte[] in, int inOfs, int inLen, byte[] digest, int digestOfs,941int digestLen) throws PKCS11Exception;942943944/**945* C_DigestUpdate continues a multiple-part message-digesting946* operation.947* (Message digesting)948*949* @param hSession the session's handle950* (PKCS#11 param: CK_SESSION_HANDLE hSession)951* @param pPart the data to get digested and the data's length952* (PKCS#11 param: CK_BYTE_PTR pPart, CK_ULONG ulPartLen)953* @exception PKCS11Exception If function returns other value than CKR_OK.954* @preconditions (pPart <> null)955* @postconditions956*/957public native void C_DigestUpdate(long hSession, long directIn, byte[] in,958int inOfs, int inLen) throws PKCS11Exception;959960961/**962* C_DigestKey continues a multi-part message-digesting963* operation, by digesting the value of a secret key as part of964* the data already digested.965* (Message digesting)966*967* @param hSession the session's handle968* (PKCS#11 param: CK_SESSION_HANDLE hSession)969* @param hKey the handle of the secret key to be digested970* (PKCS#11 param: CK_OBJECT_HANDLE hKey)971* @exception PKCS11Exception If function returns other value than CKR_OK.972* @preconditions973* @postconditions974*/975public native void C_DigestKey(long hSession, long hKey)976throws PKCS11Exception;977978979/**980* C_DigestFinal finishes a multiple-part message-digesting981* operation.982* (Message digesting)983*984* @param hSession the session's handle985* (PKCS#11 param: CK_SESSION_HANDLE hSession)986* @return the message digest and the length of the message digest987* (PKCS#11 param: CK_BYTE_PTR pDigest, CK_ULONG_PTR pulDigestLen)988* @exception PKCS11Exception If function returns other value than CKR_OK.989* @preconditions990* @postconditions (result <> null)991*/992public native int C_DigestFinal(long hSession, byte[] pDigest, int digestOfs,993int digestLen) throws PKCS11Exception;994995996997/* *****************************************************************************998* Signing and MACing999******************************************************************************/10001001/**1002* C_SignInit initializes a signature (private key encryption)1003* operation, where the signature is (will be) an appendix to1004* the data, and plaintext cannot be recovered from the1005* signature.1006* (Signing and MACing)1007*1008* @param hSession the session's handle1009* (PKCS#11 param: CK_SESSION_HANDLE hSession)1010* @param pMechanism the signature mechanism1011* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)1012* @param hKey the handle of the signature key1013* (PKCS#11 param: CK_OBJECT_HANDLE hKey)1014* @exception PKCS11Exception If function returns other value than CKR_OK.1015* @preconditions1016* @postconditions1017*/1018public native void C_SignInit(long hSession, CK_MECHANISM pMechanism,1019long hKey) throws PKCS11Exception;102010211022/**1023* C_Sign signs (encrypts with private key) data in a single1024* part, where the signature is (will be) an appendix to the1025* data, and plaintext cannot be recovered from the signature.1026* (Signing and MACing)1027*1028* @param hSession the session's handle1029* (PKCS#11 param: CK_SESSION_HANDLE hSession)1030* @param pData the data to sign and the data's length1031* (PKCS#11 param: CK_BYTE_PTR pData, CK_ULONG ulDataLen)1032* @return the signature and the signature's length1033* (PKCS#11 param: CK_BYTE_PTR pSignature,1034* CK_ULONG_PTR pulSignatureLen)1035* @exception PKCS11Exception If function returns other value than CKR_OK.1036* @preconditions (pData <> null)1037* @postconditions (result <> null)1038*/1039public native byte[] C_Sign(long hSession, byte[] pData)1040throws PKCS11Exception;104110421043/**1044* C_SignUpdate continues a multiple-part signature operation,1045* where the signature is (will be) an appendix to the data,1046* and plaintext cannot be recovered from the signature.1047* (Signing and MACing)1048*1049* @param hSession the session's handle1050* (PKCS#11 param: CK_SESSION_HANDLE hSession)1051* @param pPart the data part to sign and the data part's length1052* (PKCS#11 param: CK_BYTE_PTR pPart, CK_ULONG ulPartLen)1053* @exception PKCS11Exception If function returns other value than CKR_OK.1054* @preconditions (pPart <> null)1055* @postconditions1056*/1057public native void C_SignUpdate(long hSession, long directIn, byte[] in,1058int inOfs, int inLen) throws PKCS11Exception;105910601061/**1062* C_SignFinal finishes a multiple-part signature operation,1063* returning the signature.1064* (Signing and MACing)1065*1066* @param hSession the session's handle1067* (PKCS#11 param: CK_SESSION_HANDLE hSession)1068* @param expectedLen expected signature length, can be 0 if unknown1069* @return the signature and the signature's length1070* (PKCS#11 param: CK_BYTE_PTR pSignature,1071* CK_ULONG_PTR pulSignatureLen)1072* @exception PKCS11Exception If function returns other value than CKR_OK.1073* @preconditions1074* @postconditions (result <> null)1075*/1076public native byte[] C_SignFinal(long hSession, int expectedLen)1077throws PKCS11Exception;107810791080/**1081* C_SignRecoverInit initializes a signature operation, where1082* the data can be recovered from the signature.1083* (Signing and MACing)1084*1085* @param hSession the session's handle1086* (PKCS#11 param: CK_SESSION_HANDLE hSession)1087* @param pMechanism the signature mechanism1088* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)1089* @param hKey the handle of the signature key1090* (PKCS#11 param: CK_OBJECT_HANDLE hKey)1091* @exception PKCS11Exception If function returns other value than CKR_OK.1092* @preconditions1093* @postconditions1094*/1095public native void C_SignRecoverInit(long hSession, CK_MECHANISM pMechanism,1096long hKey) throws PKCS11Exception;109710981099/**1100* C_SignRecover signs data in a single operation, where the1101* data can be recovered from the signature.1102* (Signing and MACing)1103*1104* @param hSession the session's handle1105* (PKCS#11 param: CK_SESSION_HANDLE hSession)1106* @param pData the data to sign and the data's length1107* (PKCS#11 param: CK_BYTE_PTR pData, CK_ULONG ulDataLen)1108* @return the signature and the signature's length1109* (PKCS#11 param: CK_BYTE_PTR pSignature,1110* CK_ULONG_PTR pulSignatureLen)1111* @exception PKCS11Exception If function returns other value than CKR_OK.1112* @preconditions (pData <> null)1113* @postconditions (result <> null)1114*/1115public native int C_SignRecover(long hSession, byte[] in, int inOfs,1116int inLen, byte[] out, int outOufs, int outLen)1117throws PKCS11Exception;1118111911201121/* *****************************************************************************1122* Verifying signatures and MACs1123******************************************************************************/11241125/**1126* C_VerifyInit initializes a verification operation, where the1127* signature is an appendix to the data, and plaintext cannot1128* cannot be recovered from the signature (e.g. DSA).1129* (Signing and MACing)1130*1131* @param hSession the session's handle1132* (PKCS#11 param: CK_SESSION_HANDLE hSession)1133* @param pMechanism the verification mechanism1134* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)1135* @param hKey the handle of the verification key1136* (PKCS#11 param: CK_OBJECT_HANDLE hKey)1137* @exception PKCS11Exception If function returns other value than CKR_OK.1138* @preconditions1139* @postconditions1140*/1141public native void C_VerifyInit(long hSession, CK_MECHANISM pMechanism,1142long hKey) throws PKCS11Exception;114311441145/**1146* C_Verify verifies a signature in a single-part operation,1147* where the signature is an appendix to the data, and plaintext1148* cannot be recovered from the signature.1149* (Signing and MACing)1150*1151* @param hSession the session's handle1152* (PKCS#11 param: CK_SESSION_HANDLE hSession)1153* @param pData the signed data and the signed data's length1154* (PKCS#11 param: CK_BYTE_PTR pData, CK_ULONG ulDataLen)1155* @param pSignature the signature to verify and the signature's length1156* (PKCS#11 param: CK_BYTE_PTR pSignature, CK_ULONG ulSignatureLen)1157* @exception PKCS11Exception If function returns other value than CKR_OK.1158* @preconditions (pData <> null) and (pSignature <> null)1159* @postconditions1160*/1161public native void C_Verify(long hSession, byte[] pData, byte[] pSignature)1162throws PKCS11Exception;116311641165/**1166* C_VerifyUpdate continues a multiple-part verification1167* operation, where the signature is an appendix to the data,1168* and plaintext cannot be recovered from the signature.1169* (Signing and MACing)1170*1171* @param hSession the session's handle1172* (PKCS#11 param: CK_SESSION_HANDLE hSession)1173* @param pPart the signed data part and the signed data part's length1174* (PKCS#11 param: CK_BYTE_PTR pPart, CK_ULONG ulPartLen)1175* @exception PKCS11Exception If function returns other value than CKR_OK.1176* @preconditions (pPart <> null)1177* @postconditions1178*/1179public native void C_VerifyUpdate(long hSession, long directIn, byte[] in,1180int inOfs, int inLen) throws PKCS11Exception;118111821183/**1184* C_VerifyFinal finishes a multiple-part verification1185* operation, checking the signature.1186* (Signing and MACing)1187*1188* @param hSession the session's handle1189* (PKCS#11 param: CK_SESSION_HANDLE hSession)1190* @param pSignature the signature to verify and the signature's length1191* (PKCS#11 param: CK_BYTE_PTR pSignature, CK_ULONG ulSignatureLen)1192* @exception PKCS11Exception If function returns other value than CKR_OK.1193* @preconditions (pSignature <> null)1194* @postconditions1195*/1196public native void C_VerifyFinal(long hSession, byte[] pSignature)1197throws PKCS11Exception;119811991200/**1201* C_VerifyRecoverInit initializes a signature verification1202* operation, where the data is recovered from the signature.1203* (Signing and MACing)1204*1205* @param hSession the session's handle1206* (PKCS#11 param: CK_SESSION_HANDLE hSession)1207* @param pMechanism the verification mechanism1208* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)1209* @param hKey the handle of the verification key1210* (PKCS#11 param: CK_OBJECT_HANDLE hKey)1211* @exception PKCS11Exception If function returns other value than CKR_OK.1212* @preconditions1213* @postconditions1214*/1215public native void C_VerifyRecoverInit(long hSession,1216CK_MECHANISM pMechanism, long hKey) throws PKCS11Exception;121712181219/**1220* C_VerifyRecover verifies a signature in a single-part1221* operation, where the data is recovered from the signature.1222* (Signing and MACing)1223*1224* @param hSession the session's handle1225* (PKCS#11 param: CK_SESSION_HANDLE hSession)1226* @param pSignature the signature to verify and the signature's length1227* (PKCS#11 param: CK_BYTE_PTR pSignature, CK_ULONG ulSignatureLen)1228* @return the recovered data and the recovered data's length1229* (PKCS#11 param: CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen)1230* @exception PKCS11Exception If function returns other value than CKR_OK.1231* @preconditions (pSignature <> null)1232* @postconditions (result <> null)1233*/1234public native int C_VerifyRecover(long hSession, byte[] in, int inOfs,1235int inLen, byte[] out, int outOufs, int outLen)1236throws PKCS11Exception;1237123812391240/* *****************************************************************************1241* Dual-function cryptographic operations1242******************************************************************************/12431244/**1245* C_DigestEncryptUpdate continues a multiple-part digesting1246* and encryption operation.1247* (Dual-function cryptographic operations)1248*1249* @param hSession the session's handle1250* (PKCS#11 param: CK_SESSION_HANDLE hSession)1251* @param pPart the data part to digest and to encrypt and the data's length1252* (PKCS#11 param: CK_BYTE_PTR pPart, CK_ULONG ulPartLen)1253* @return the digested and encrypted data part and the data part's length1254* (PKCS#11 param: CK_BYTE_PTR pEncryptedPart,1255* CK_ULONG_PTR pulEncryptedPartLen)1256* @exception PKCS11Exception If function returns other value than CKR_OK.1257* @preconditions (pPart <> null)1258* @postconditions1259*/1260// public native byte[] C_DigestEncryptUpdate(long hSession, byte[] pPart)1261// throws PKCS11Exception;126212631264/**1265* C_DecryptDigestUpdate continues a multiple-part decryption and1266* digesting operation.1267* (Dual-function cryptographic operations)1268*1269* @param hSession the session's handle1270* (PKCS#11 param: CK_SESSION_HANDLE hSession)1271* @param pEncryptedPart the encrypted data part to decrypt and to digest1272* and encrypted data part's length1273* (PKCS#11 param: CK_BYTE_PTR pEncryptedPart,1274* CK_ULONG ulEncryptedPartLen)1275* @return the decrypted and digested data part and the data part's length1276* (PKCS#11 param: CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen)1277* @exception PKCS11Exception If function returns other value than CKR_OK.1278* @preconditions (pEncryptedPart <> null)1279* @postconditions1280*/1281// public native byte[] C_DecryptDigestUpdate(long hSession,1282// byte[] pEncryptedPart) throws PKCS11Exception;128312841285/**1286* C_SignEncryptUpdate continues a multiple-part signing and1287* encryption operation.1288* (Dual-function cryptographic operations)1289*1290* @param hSession the session's handle1291* (PKCS#11 param: CK_SESSION_HANDLE hSession)1292* @param pPart the data part to sign and to encrypt and the data part's1293* length1294* (PKCS#11 param: CK_BYTE_PTR pPart, CK_ULONG ulPartLen)1295* @return the signed and encrypted data part and the data part's length1296* (PKCS#11 param: CK_BYTE_PTR pEncryptedPart,1297* CK_ULONG_PTR pulEncryptedPartLen)1298* @exception PKCS11Exception If function returns other value than CKR_OK.1299* @preconditions (pPart <> null)1300* @postconditions1301*/1302// public native byte[] C_SignEncryptUpdate(long hSession, byte[] pPart)1303// throws PKCS11Exception;130413051306/**1307* C_DecryptVerifyUpdate continues a multiple-part decryption and1308* verify operation.1309* (Dual-function cryptographic operations)1310*1311* @param hSession the session's handle1312* (PKCS#11 param: CK_SESSION_HANDLE hSession)1313* @param pEncryptedPart the encrypted data part to decrypt and to verify1314* and the data part's length1315* (PKCS#11 param: CK_BYTE_PTR pEncryptedPart,1316* CK_ULONG ulEncryptedPartLen)1317* @return the decrypted and verified data part and the data part's length1318* (PKCS#11 param: CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen)1319* @exception PKCS11Exception If function returns other value than CKR_OK.1320* @preconditions (pEncryptedPart <> null)1321* @postconditions1322*/1323// public native byte[] C_DecryptVerifyUpdate(long hSession,1324// byte[] pEncryptedPart) throws PKCS11Exception;132513261327/* *****************************************************************************1328* Key management1329******************************************************************************/13301331/**1332* getNativeKeyInfo gets the key object attributes and values as an opaque1333* byte array to be used in createNativeKey method.1334* (Key management)1335*1336* @param hSession the session's handle1337* @param hKey key's handle1338* @param hWrappingKey key handle for wrapping the extracted sensitive keys.1339* -1 if not used.1340* @param pWrappingMech mechanism for wrapping the extracted sensitive keys1341* @return an opaque byte array containing the key object attributes1342* and values1343* @exception PKCS11Exception If an internal PKCS#11 function returns other1344* value than CKR_OK.1345* @preconditions1346* @postconditions1347*/1348public native byte[] getNativeKeyInfo(long hSession, long hKey,1349long hWrappingKey, CK_MECHANISM pWrappingMech) throws PKCS11Exception;13501351/**1352* createNativeKey creates a key object with attributes and values1353* specified by parameter as an opaque byte array.1354* (Key management)1355*1356* @param hSession the session's handle1357* @param keyInfo opaque byte array containing key object attributes1358* and values1359* @param hWrappingKey key handle for unwrapping the extracted sensitive keys.1360* -1 if not used.1361* @param pWrappingMech mechanism for unwrapping the extracted sensitive keys1362* @return key object handle1363* @exception PKCS11Exception If an internal PKCS#11 function returns other1364* value than CKR_OK.1365* @preconditions1366* @postconditions1367*/1368public native long createNativeKey(long hSession, byte[] keyInfo,1369long hWrappingKey, CK_MECHANISM pWrappingMech) throws PKCS11Exception;13701371/**1372* C_GenerateKey generates a secret key, creating a new key1373* object.1374* (Key management)1375*1376* @param hSession the session's handle1377* (PKCS#11 param: CK_SESSION_HANDLE hSession)1378* @param pMechanism the key generation mechanism1379* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)1380* @param pTemplate the template for the new key and the number of1381* attributes in the template1382* (PKCS#11 param: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)1383* @return the handle of the new key1384* (PKCS#11 param: CK_OBJECT_HANDLE_PTR phKey)1385* @exception PKCS11Exception If function returns other value than CKR_OK.1386* @preconditions1387* @postconditions1388*/1389public native long C_GenerateKey(long hSession, CK_MECHANISM pMechanism,1390CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception;139113921393/**1394* C_GenerateKeyPair generates a public-key/private-key pair,1395* creating new key objects.1396* (Key management)1397*1398* @param hSession the session's handle1399* (PKCS#11 param: CK_SESSION_HANDLE hSession)1400* @param pMechanism the key generation mechanism1401* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)1402* @param pPublicKeyTemplate the template for the new public key and the1403* number of attributes in the template1404* (PKCS#11 param: CK_ATTRIBUTE_PTR pPublicKeyTemplate,1405* CK_ULONG ulPublicKeyAttributeCount)1406* @param pPrivateKeyTemplate the template for the new private key and the1407* number of attributes in the template1408* (PKCS#11 param: CK_ATTRIBUTE_PTR pPrivateKeyTemplate1409* CK_ULONG ulPrivateKeyAttributeCount)1410* @return a long array with exactly two elements and the public key handle1411* as the first element and the private key handle as the second1412* element1413* (PKCS#11 param: CK_OBJECT_HANDLE_PTR phPublicKey,1414* CK_OBJECT_HANDLE_PTR phPrivateKey)1415* @exception PKCS11Exception If function returns other value than CKR_OK.1416* @preconditions (pMechanism <> null)1417* @postconditions (result <> null) and (result.length == 2)1418*/1419public native long[] C_GenerateKeyPair(long hSession,1420CK_MECHANISM pMechanism, CK_ATTRIBUTE[] pPublicKeyTemplate,1421CK_ATTRIBUTE[] pPrivateKeyTemplate) throws PKCS11Exception;1422142314241425/**1426* C_WrapKey wraps (i.e., encrypts) a key.1427* (Key management)1428*1429* @param hSession the session's handle1430* (PKCS#11 param: CK_SESSION_HANDLE hSession)1431* @param pMechanism the wrapping mechanism1432* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)1433* @param hWrappingKey the handle of the wrapping key1434* (PKCS#11 param: CK_OBJECT_HANDLE hWrappingKey)1435* @param hKey the handle of the key to be wrapped1436* (PKCS#11 param: CK_OBJECT_HANDLE hKey)1437* @return the wrapped key and the length of the wrapped key1438* (PKCS#11 param: CK_BYTE_PTR pWrappedKey,1439* CK_ULONG_PTR pulWrappedKeyLen)1440* @exception PKCS11Exception If function returns other value than CKR_OK.1441* @preconditions1442* @postconditions (result <> null)1443*/1444public native byte[] C_WrapKey(long hSession, CK_MECHANISM pMechanism,1445long hWrappingKey, long hKey) throws PKCS11Exception;144614471448/**1449* C_UnwrapKey unwraps (decrypts) a wrapped key, creating a new1450* key object.1451* (Key management)1452*1453* @param hSession the session's handle1454* (PKCS#11 param: CK_SESSION_HANDLE hSession)1455* @param pMechanism the unwrapping mechanism1456* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)1457* @param hUnwrappingKey the handle of the unwrapping key1458* (PKCS#11 param: CK_OBJECT_HANDLE hUnwrappingKey)1459* @param pWrappedKey the wrapped key to unwrap and the wrapped key's length1460* (PKCS#11 param: CK_BYTE_PTR pWrappedKey, CK_ULONG ulWrappedKeyLen)1461* @param pTemplate the template for the new key and the number of1462* attributes in the template1463* (PKCS#11 param: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)1464* @return the handle of the unwrapped key1465* (PKCS#11 param: CK_OBJECT_HANDLE_PTR phKey)1466* @exception PKCS11Exception If function returns other value than CKR_OK.1467* @preconditions (pWrappedKey <> null)1468* @postconditions1469*/1470public native long C_UnwrapKey(long hSession, CK_MECHANISM pMechanism,1471long hUnwrappingKey, byte[] pWrappedKey, CK_ATTRIBUTE[] pTemplate)1472throws PKCS11Exception;147314741475/**1476* C_DeriveKey derives a key from a base key, creating a new key1477* object.1478* (Key management)1479*1480* @param hSession the session's handle1481* (PKCS#11 param: CK_SESSION_HANDLE hSession)1482* @param pMechanism the key derivation mechanism1483* (PKCS#11 param: CK_MECHANISM_PTR pMechanism)1484* @param hBaseKey the handle of the base key1485* (PKCS#11 param: CK_OBJECT_HANDLE hBaseKey)1486* @param pTemplate the template for the new key and the number of1487* attributes in the template1488* (PKCS#11 param: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)1489* @return the handle of the derived key1490* (PKCS#11 param: CK_OBJECT_HANDLE_PTR phKey)1491* @exception PKCS11Exception If function returns other value than CKR_OK.1492* @preconditions1493* @postconditions1494*/1495public native long C_DeriveKey(long hSession, CK_MECHANISM pMechanism,1496long hBaseKey, CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception;1497149814991500/* *****************************************************************************1501* Random number generation1502******************************************************************************/15031504/**1505* C_SeedRandom mixes additional seed material into the token's1506* random number generator.1507* (Random number generation)1508*1509* @param hSession the session's handle1510* (PKCS#11 param: CK_SESSION_HANDLE hSession)1511* @param pSeed the seed material and the seed material's length1512* (PKCS#11 param: CK_BYTE_PTR pSeed, CK_ULONG ulSeedLen)1513* @exception PKCS11Exception If function returns other value than CKR_OK.1514* @preconditions (pSeed <> null)1515* @postconditions1516*/1517public native void C_SeedRandom(long hSession, byte[] pSeed)1518throws PKCS11Exception;151915201521/**1522* C_GenerateRandom generates random data.1523* (Random number generation)1524*1525* @param hSession the session's handle1526* (PKCS#11 param: CK_SESSION_HANDLE hSession)1527* @param RandomData receives the random data and the length of RandomData1528* is the length of random data to be generated1529* (PKCS#11 param: CK_BYTE_PTR pRandomData, CK_ULONG ulRandomLen)1530* @exception PKCS11Exception If function returns other value than CKR_OK.1531* @preconditions (randomData <> null)1532* @postconditions1533*/1534public native void C_GenerateRandom(long hSession, byte[] randomData)1535throws PKCS11Exception;1536153715381539/* *****************************************************************************1540* Parallel function management1541******************************************************************************/15421543/**1544* C_GetFunctionStatus is a legacy function; it obtains an1545* updated status of a function running in parallel with an1546* application.1547* (Parallel function management)1548*1549* @param hSession the session's handle1550* (PKCS#11 param: CK_SESSION_HANDLE hSession)1551* @exception PKCS11Exception If function returns other value than CKR_OK.1552* @preconditions1553* @postconditions1554*/1555// public native void C_GetFunctionStatus(long hSession)1556// throws PKCS11Exception;155715581559/**1560* C_CancelFunction is a legacy function; it cancels a function1561* running in parallel.1562* (Parallel function management)1563*1564* @param hSession the session's handle1565* (PKCS#11 param: CK_SESSION_HANDLE hSession)1566* @exception PKCS11Exception If function returns other value than CKR_OK.1567* @preconditions1568* @postconditions1569*/1570// public native void C_CancelFunction(long hSession) throws PKCS11Exception;1571157215731574/* *****************************************************************************1575* Functions added in for Cryptoki Version 2.01 or later1576******************************************************************************/15771578/**1579* C_WaitForSlotEvent waits for a slot event (token insertion,1580* removal, etc.) to occur.1581* (General-purpose)1582*1583* @param flags blocking/nonblocking flag1584* (PKCS#11 param: CK_FLAGS flags)1585* @param pReserved reserved. Should be null1586* (PKCS#11 param: CK_VOID_PTR pReserved)1587* @return the slot ID where the event occurred1588* (PKCS#11 param: CK_SLOT_ID_PTR pSlot)1589* @exception PKCS11Exception If function returns other value than CKR_OK.1590* @preconditions (pRserved == null)1591* @postconditions1592*/1593// public native long C_WaitForSlotEvent(long flags, Object pRserved)1594// throws PKCS11Exception;15951596/**1597* Returns the string representation of this object.1598*1599* @return The string representation of object1600*/1601public String toString() {1602return "Module name: " + pkcs11ModulePath;1603}16041605/**1606* Calls disconnect() to cleanup the native part of the wrapper. Once this1607* method is called, this object cannot be used any longer. Any subsequent1608* call to a C_* method will result in a runtime exception.1609*1610* @exception Throwable If finalization fails.1611*/1612protected void finalize() throws Throwable {1613disconnect();1614}16151616// PKCS11 subclass that has all methods synchronized and delegating to the1617// parent. Used for tokens that only support single threaded access1618static class SynchronizedPKCS11 extends PKCS11 {16191620SynchronizedPKCS11(String pkcs11ModulePath, String functionListName)1621throws IOException {1622super(pkcs11ModulePath, functionListName);1623}16241625synchronized void C_Initialize(Object pInitArgs) throws PKCS11Exception {1626super.C_Initialize(pInitArgs);1627}16281629public synchronized void C_Finalize(Object pReserved)1630throws PKCS11Exception {1631super.C_Finalize(pReserved);1632}16331634public synchronized CK_INFO C_GetInfo() throws PKCS11Exception {1635return super.C_GetInfo();1636}16371638public synchronized long[] C_GetSlotList(boolean tokenPresent)1639throws PKCS11Exception {1640return super.C_GetSlotList(tokenPresent);1641}16421643public synchronized CK_SLOT_INFO C_GetSlotInfo(long slotID)1644throws PKCS11Exception {1645return super.C_GetSlotInfo(slotID);1646}16471648public synchronized CK_TOKEN_INFO C_GetTokenInfo(long slotID)1649throws PKCS11Exception {1650return super.C_GetTokenInfo(slotID);1651}16521653public synchronized long[] C_GetMechanismList(long slotID)1654throws PKCS11Exception {1655return super.C_GetMechanismList(slotID);1656}16571658public synchronized CK_MECHANISM_INFO C_GetMechanismInfo(long slotID,1659long type) throws PKCS11Exception {1660return super.C_GetMechanismInfo(slotID, type);1661}16621663public synchronized long C_OpenSession(long slotID, long flags,1664Object pApplication, CK_NOTIFY Notify) throws PKCS11Exception {1665return super.C_OpenSession(slotID, flags, pApplication, Notify);1666}16671668public synchronized void C_CloseSession(long hSession)1669throws PKCS11Exception {1670super.C_CloseSession(hSession);1671}16721673public synchronized CK_SESSION_INFO C_GetSessionInfo(long hSession)1674throws PKCS11Exception {1675return super.C_GetSessionInfo(hSession);1676}16771678public synchronized void C_Login(long hSession, long userType, char[] pPin)1679throws PKCS11Exception {1680super.C_Login(hSession, userType, pPin);1681}16821683public synchronized void C_Logout(long hSession) throws PKCS11Exception {1684super.C_Logout(hSession);1685}16861687public synchronized long C_CreateObject(long hSession,1688CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception {1689return super.C_CreateObject(hSession, pTemplate);1690}16911692public synchronized long C_CopyObject(long hSession, long hObject,1693CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception {1694return super.C_CopyObject(hSession, hObject, pTemplate);1695}16961697public synchronized void C_DestroyObject(long hSession, long hObject)1698throws PKCS11Exception {1699super.C_DestroyObject(hSession, hObject);1700}17011702public synchronized void C_GetAttributeValue(long hSession, long hObject,1703CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception {1704super.C_GetAttributeValue(hSession, hObject, pTemplate);1705}17061707public synchronized void C_SetAttributeValue(long hSession, long hObject,1708CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception {1709super.C_SetAttributeValue(hSession, hObject, pTemplate);1710}17111712public synchronized void C_FindObjectsInit(long hSession,1713CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception {1714super.C_FindObjectsInit(hSession, pTemplate);1715}17161717public synchronized long[] C_FindObjects(long hSession,1718long ulMaxObjectCount) throws PKCS11Exception {1719return super.C_FindObjects(hSession, ulMaxObjectCount);1720}17211722public synchronized void C_FindObjectsFinal(long hSession)1723throws PKCS11Exception {1724super.C_FindObjectsFinal(hSession);1725}17261727public synchronized void C_EncryptInit(long hSession,1728CK_MECHANISM pMechanism, long hKey) throws PKCS11Exception {1729super.C_EncryptInit(hSession, pMechanism, hKey);1730}17311732public synchronized int C_Encrypt(long hSession, long directIn, byte[] in,1733int inOfs, int inLen, long directOut, byte[] out, int outOfs, int outLen)1734throws PKCS11Exception {1735return super.C_Encrypt(hSession, directIn, in, inOfs, inLen,1736directOut, out, outOfs, outLen);1737}17381739public synchronized int C_EncryptUpdate(long hSession, long directIn,1740byte[] in, int inOfs, int inLen, long directOut, byte[] out,1741int outOfs, int outLen) throws PKCS11Exception {1742return super.C_EncryptUpdate(hSession, directIn, in, inOfs, inLen,1743directOut, out, outOfs, outLen);1744}17451746public synchronized int C_EncryptFinal(long hSession, long directOut,1747byte[] out, int outOfs, int outLen) throws PKCS11Exception {1748return super.C_EncryptFinal(hSession, directOut, out, outOfs, outLen);1749}17501751public synchronized void C_DecryptInit(long hSession,1752CK_MECHANISM pMechanism, long hKey) throws PKCS11Exception {1753super.C_DecryptInit(hSession, pMechanism, hKey);1754}17551756public synchronized int C_Decrypt(long hSession, long directIn,1757byte[] in, int inOfs, int inLen, long directOut, byte[] out,1758int outOfs, int outLen) throws PKCS11Exception {1759return super.C_Decrypt(hSession, directIn, in, inOfs, inLen,1760directOut, out, outOfs, outLen);1761}17621763public synchronized int C_DecryptUpdate(long hSession, long directIn,1764byte[] in, int inOfs, int inLen, long directOut, byte[] out,1765int outOfs, int outLen) throws PKCS11Exception {1766return super.C_DecryptUpdate(hSession, directIn, in, inOfs, inLen,1767directOut, out, outOfs, outLen);1768}17691770public synchronized int C_DecryptFinal(long hSession, long directOut,1771byte[] out, int outOfs, int outLen) throws PKCS11Exception {1772return super.C_DecryptFinal(hSession, directOut, out, outOfs, outLen);1773}17741775public synchronized void C_DigestInit(long hSession, CK_MECHANISM pMechanism)1776throws PKCS11Exception {1777super.C_DigestInit(hSession, pMechanism);1778}17791780public synchronized int C_DigestSingle(long hSession,1781CK_MECHANISM pMechanism, byte[] in, int inOfs, int inLen,1782byte[] digest, int digestOfs, int digestLen) throws PKCS11Exception {1783return super.C_DigestSingle(hSession, pMechanism, in, inOfs, inLen,1784digest, digestOfs, digestLen);1785}17861787public synchronized void C_DigestUpdate(long hSession, long directIn,1788byte[] in, int inOfs, int inLen) throws PKCS11Exception {1789super.C_DigestUpdate(hSession, directIn, in, inOfs, inLen);1790}17911792public synchronized void C_DigestKey(long hSession, long hKey)1793throws PKCS11Exception {1794super.C_DigestKey(hSession, hKey);1795}17961797public synchronized int C_DigestFinal(long hSession, byte[] pDigest,1798int digestOfs, int digestLen) throws PKCS11Exception {1799return super.C_DigestFinal(hSession, pDigest, digestOfs, digestLen);1800}18011802public synchronized void C_SignInit(long hSession, CK_MECHANISM pMechanism,1803long hKey) throws PKCS11Exception {1804super.C_SignInit(hSession, pMechanism, hKey);1805}18061807public synchronized byte[] C_Sign(long hSession, byte[] pData)1808throws PKCS11Exception {1809return super.C_Sign(hSession, pData);1810}18111812public synchronized void C_SignUpdate(long hSession, long directIn,1813byte[] in, int inOfs, int inLen) throws PKCS11Exception {1814super.C_SignUpdate(hSession, directIn, in, inOfs, inLen);1815}18161817public synchronized byte[] C_SignFinal(long hSession, int expectedLen)1818throws PKCS11Exception {1819return super.C_SignFinal(hSession, expectedLen);1820}18211822public synchronized void C_SignRecoverInit(long hSession,1823CK_MECHANISM pMechanism, long hKey) throws PKCS11Exception {1824super.C_SignRecoverInit(hSession, pMechanism, hKey);1825}18261827public synchronized int C_SignRecover(long hSession, byte[] in, int inOfs,1828int inLen, byte[] out, int outOufs, int outLen)1829throws PKCS11Exception {1830return super.C_SignRecover(hSession, in, inOfs, inLen, out, outOufs,1831outLen);1832}18331834public synchronized void C_VerifyInit(long hSession, CK_MECHANISM pMechanism,1835long hKey) throws PKCS11Exception {1836super.C_VerifyInit(hSession, pMechanism, hKey);1837}18381839public synchronized void C_Verify(long hSession, byte[] pData,1840byte[] pSignature) throws PKCS11Exception {1841super.C_Verify(hSession, pData, pSignature);1842}18431844public synchronized void C_VerifyUpdate(long hSession, long directIn,1845byte[] in, int inOfs, int inLen) throws PKCS11Exception {1846super.C_VerifyUpdate(hSession, directIn, in, inOfs, inLen);1847}18481849public synchronized void C_VerifyFinal(long hSession, byte[] pSignature)1850throws PKCS11Exception {1851super.C_VerifyFinal(hSession, pSignature);1852}18531854public synchronized void C_VerifyRecoverInit(long hSession,1855CK_MECHANISM pMechanism, long hKey) throws PKCS11Exception {1856super.C_VerifyRecoverInit(hSession, pMechanism, hKey);1857}18581859public synchronized int C_VerifyRecover(long hSession, byte[] in, int inOfs,1860int inLen, byte[] out, int outOufs, int outLen)1861throws PKCS11Exception {1862return super.C_VerifyRecover(hSession, in, inOfs, inLen, out, outOufs,1863outLen);1864}18651866public synchronized long C_GenerateKey(long hSession,1867CK_MECHANISM pMechanism, CK_ATTRIBUTE[] pTemplate)1868throws PKCS11Exception {1869return super.C_GenerateKey(hSession, pMechanism, pTemplate);1870}18711872public synchronized long[] C_GenerateKeyPair(long hSession,1873CK_MECHANISM pMechanism, CK_ATTRIBUTE[] pPublicKeyTemplate,1874CK_ATTRIBUTE[] pPrivateKeyTemplate)1875throws PKCS11Exception {1876return super.C_GenerateKeyPair(hSession, pMechanism, pPublicKeyTemplate,1877pPrivateKeyTemplate);1878}18791880public synchronized byte[] C_WrapKey(long hSession, CK_MECHANISM pMechanism,1881long hWrappingKey, long hKey) throws PKCS11Exception {1882return super.C_WrapKey(hSession, pMechanism, hWrappingKey, hKey);1883}18841885public synchronized long C_UnwrapKey(long hSession, CK_MECHANISM pMechanism,1886long hUnwrappingKey, byte[] pWrappedKey, CK_ATTRIBUTE[] pTemplate)1887throws PKCS11Exception {1888return super.C_UnwrapKey(hSession, pMechanism, hUnwrappingKey,1889pWrappedKey, pTemplate);1890}18911892public synchronized long C_DeriveKey(long hSession, CK_MECHANISM pMechanism,1893long hBaseKey, CK_ATTRIBUTE[] pTemplate) throws PKCS11Exception {1894return super.C_DeriveKey(hSession, pMechanism, hBaseKey, pTemplate);1895}18961897public synchronized void C_SeedRandom(long hSession, byte[] pSeed)1898throws PKCS11Exception {1899super.C_SeedRandom(hSession, pSeed);1900}19011902public synchronized void C_GenerateRandom(long hSession, byte[] randomData)1903throws PKCS11Exception {1904super.C_GenerateRandom(hSession, randomData);1905}1906}1907}190819091910