public class PKCS11Token extends Object
| Constructor and Description |
|---|
PKCS11Token(Token token,
boolean readOnly,
char[] pin)
The simple constructor.
|
PKCS11Token(Token token,
boolean readOnly,
long userType,
char[] userName,
List<char[]> pins,
Integer numSessions)
The advanced constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
closeAllSessions()
Closes all sessions.
|
long |
copyObject(long sourceObjectHandle,
AttributeVector template)
Copy an existing object.
|
long |
createECPrivateKeyObject(AttributeVector template,
byte[] ecPoint)
Create EC private key object in the PKCS#11 device.
|
long |
createObject(AttributeVector template)
Create a new object on the token (or in the session).
|
long |
createPrivateKeyObject(AttributeVector template,
PublicKey publicKey) |
byte[] |
decrypt(Mechanism mechanism,
long keyHandle,
byte[] ciphertext)
Decrypts the given data with the key and mechanism.
|
int |
decrypt(OutputStream out,
Mechanism mechanism,
long keyHandle,
InputStream ciphertext)
This method can be used to decrypt large data.
|
byte[][] |
decryptMessages(Mechanism mechanism,
long keyHandle,
DecryptMessageBytesEntry[] entries)
Decrypts the given ciphertexts using the given mechanism and keys.
|
int[] |
decryptMessages(Mechanism mechanism,
long keyHandle,
DecryptMessageStreamEntry[] entries)
Decrypts the given ciphertexts using the given mechanism and key.
|
long |
deriveKey(Mechanism mechanism,
long baseKeyHandle,
AttributeVector template)
Derives a new key from a specified base key using the given mechanism.
|
void |
destroyObject(long objectHandle)
Destroy a certain object on the token (or in the session).
|
List<Long> |
destroyObjects(List<Long> objectHandles) |
long[] |
destroyObjects(long... objectHandles)
Destroy a certain object on the token (or in the session).
|
byte[] |
digest(Mechanism mechanism,
byte[] data)
Digests the given data with the mechanism.
|
byte[] |
digest(Mechanism mechanism,
InputStream data)
Digests the large data with the mechanism.
|
byte[] |
digestKey(Mechanism mechanism,
long keyHandle)
Digests the given key with the mechanism.
|
byte[] |
encrypt(Mechanism mechanism,
long keyHandle,
byte[] plaintext)
Encrypts the given data with the key and mechanism.
|
int |
encrypt(OutputStream out,
Mechanism mechanism,
long keyHandle,
InputStream plaintext)
This method can be used to encrypt large data.
|
byte[][] |
encryptMessages(Mechanism mechanism,
long keyHandle,
EncryptMessageBytesEntry[] entries)
Encrypts the given messages using the given mechanism and key
|
int[] |
encryptMessages(Mechanism mechanism,
long keyHandle,
EncryptMessageStreamEntry[] entries)
Encrypts the given messages using the given mechanism and key.
|
long[] |
findAllObjects(AttributeVector template)
Finds all objects that match the template.
|
long[] |
findObjects(AttributeVector template,
int maxObjectCount)
Finds objects that match the template.
|
long |
generateKey(Mechanism mechanism,
AttributeVector template)
Generate a new secret key or a set of domain parameters.
|
PKCS11KeyPair |
generateKeyPair(Mechanism mechanism,
KeyPairTemplate template)
Generate a new public key - private key key-pair and use the set attributes of the template
objects for setting the attributes of the new public key and private key objects.
|
byte[] |
generateRandom(int numberOfBytesToGenerate)
Generates a certain number of random bytes.
|
byte[] |
generateRandom(int numberOfBytesToGenerate,
byte[] extraSeed)
Generates a certain number of random bytes.
|
byte[] |
generateUniqueId(AttributeVector template,
int idLength,
Random random)
Generate a unique CKA_ID.
|
AttributeVector |
getAttrValues(long objectHandle,
List<Long> attributeTypes)
Gets give attributes for the given object handle.
|
AttributeVector |
getAttrValues(long objectHandle,
long... attributeTypes)
Gets give attributes for the given object handle.
|
AttributeVector |
getDefaultAttrValues(long objectHandle)
Gets all attributes for the given object handle.
|
PKCS11Key |
getKey(AttributeVector criteria)
Gets the
PKCS11Key of a key satisfying the given criteria. |
PKCS11Key |
getKey(PKCS11KeyId keyId)
Gets the
PKCS11Key identified by the given PKCS11KeyId. |
PKCS11KeyId |
getKeyId(AttributeVector criteria)
Gets the
PKCS11KeyId of a key satisfying the given criteria. |
MechanismInfo |
getMechanismInfo(long mechanism)
Gets the
MechanismInfo for given mechanism code. |
Set<Long> |
getMechanisms() |
PKCS11Module |
getModule() |
String |
getModuleInfo() |
long |
getObjectSize(long objectHandle)
Get the size of the specified object in bytes.
|
Token |
getToken()
Get the token that created this Session object.
|
long |
getTokenId()
Get the token (slot) identifier of this token.
|
void |
initPIN(char[] pin)
Initializes the user-PIN.
|
boolean |
isReadOnly()
Returns whether this token is read-only.
|
void |
logInSecurityOfficer(char[] userName,
char[] pin)
Login this session as CKU_SO (Security Officer).
|
void |
logout()
Logs out this session.
|
void |
setAttributeValues(long objectToUpdateHandle,
AttributeVector template)
Gets all present attributes of the given template object and writes them to the object to update
on the token (or in the session).
|
void |
setMaxMessageSize(int maxMessageSize)
Sets the maximal message size sent to the PKCS#11 device in one command.
|
void |
setPIN(char[] oldPin,
char[] newPin)
Set the user-PIN to a new value.
|
void |
setTimeOutWaitNewSession(int timeOutWaitNewSessionMs) |
byte[] |
sign(Mechanism mechanism,
long keyHandle,
byte[] data)
Signs the given data with the key and mechanism.
|
byte[] |
sign(Mechanism mechanism,
long keyHandle,
InputStream data)
This method can be used to sign large data.
|
byte[][] |
signMessages(Mechanism mechanism,
long keyHandle,
SignMessageBytesEntry[] entries)
Signs the given messages using the given mechanism and key.
|
byte[][] |
signMessages(Mechanism mechanism,
long keyHandle,
SignMessageStreamEntry[] entries)
Signs the given messages using the given mechanism and key.
|
byte[] |
signRecover(Mechanism mechanism,
long keyHandle,
byte[] data)
Sign-recovers the given data with the key and mechanism.
|
boolean |
supportsMechanism(long mechanism,
long flagBit)
Returns whether the mechanism for given purpose is supported.
|
String |
toString()
Returns the string representation of this object.
|
long |
unwrapKey(Mechanism mechanism,
long unwrappingKeyHandle,
byte[] wrappedKey,
AttributeVector keyTemplate)
Unwraps (decrypts) the given encrypted key with the unwrapping key using the given mechanism.
|
boolean |
verify(Mechanism mechanism,
long keyHandle,
byte[] data,
byte[] signature)
Verifies the given signature against the given data with the key and mechanism.
|
boolean |
verify(Mechanism mechanism,
long keyHandle,
InputStream data,
byte[] signature)
This method can be used to verify large data.
|
boolean[] |
verifyMessages(Mechanism mechanism,
long keyHandle,
VerifyMessageBytesEntry[] entries) |
boolean[] |
verifyMessages(Mechanism mechanism,
long keyHandle,
VerifyMessageStreamEntry[] entries)
Verify the signatures over given messages using the given mechanism and key.
|
byte[] |
verifyRecover(Mechanism mechanism,
long keyHandle,
byte[] data)
Verify-recovers the given data with the key and mechanism.
|
byte[] |
wrapKey(Mechanism mechanism,
long wrappingKeyHandle,
long keyHandle)
Wraps (encrypts) the given key with the wrapping key using the given mechanism.
|
public PKCS11Token(Token token, boolean readOnly, char[] pin) throws TokenException
token - The tokenreadOnly - True if this token is read only, false if read-write.pin - The PIN of user type CKU_USER. May be null.TokenException - If accessing the PKCS#11 device failed.public PKCS11Token(Token token, boolean readOnly, long userType, char[] userName, List<char[]> pins, Integer numSessions) throws TokenException
token - The tokenreadOnly - True if this token is read only, false if read-write.userType - The user type. In general, it is CKU_USER.userName - The user name. In general, it is null.pins - The PINs. May be null and empty list.numSessions - Number of sessions. May be null.TokenException - If accessing the PKCS#11 device failed.public PKCS11Module getModule()
public void setTimeOutWaitNewSession(int timeOutWaitNewSessionMs)
public void setMaxMessageSize(int maxMessageSize)
maxMessageSize - the maximal message size in bytes.public MechanismInfo getMechanismInfo(long mechanism)
MechanismInfo for given mechanism code.mechanism - The mechanism code.MechanismInfo.public boolean supportsMechanism(long mechanism,
long flagBit)
mechanism - The mechanism.flagBit - The purpose. Valid values are (could be extended in the future PKCS#11 version):
PKCS11Constants.CKF_SIGN, PKCS11Constants.CKF_VERIFY,
PKCS11Constants.CKF_SIGN_RECOVER, PKCS11Constants.CKF_VERIFY_RECOVER,
PKCS11Constants.CKF_ENCRYPT, PKCS11Constants.CKF_DECRYPT,
PKCS11Constants.CKF_DERIVE, PKCS11Constants.CKF_DIGEST,
PKCS11Constants.CKF_UNWRAP, PKCS11Constants.CKF_WRAP.public void setPIN(char[] oldPin,
char[] newPin)
throws TokenException
oldPin - The old (current) user-PIN.newPin - The new value for the user-PIN.TokenException - If setting the new PIN fails.public void initPIN(char[] pin)
throws TokenException
pin - The new user-PIN. This parameter may be null, if the token has a protected
authentication path. Refer to the PKCS#11 standard for details.TokenException - If the session has not the right to set the PIN of if the operation fails for some
other reason.public void closeAllSessions()
public long getTokenId()
public Token getToken()
public String getModuleInfo() throws TokenException
TokenExceptionpublic boolean isReadOnly()
public void logInSecurityOfficer(char[] userName,
char[] pin)
throws TokenException
userName - Username of user type CKU_SO.pin - PIN.TokenException - If logging in the session fails.public void logout()
throws TokenException
TokenException - If logging out the session fails.public long createObject(AttributeVector template) throws TokenException
AttributesTemplate desKeyTemplate = AttributesTemplate.newSecretKey(CKK_DES3);
// the key type is set by the DESSecretKey's constructor, so you need not do it
desKeyTemplate.value(myDesKeyValueAs8BytesLongByteArray)
.token(true)
.private(true);
.encrypt(true);
.decrypt(true);
...
long theCreatedDESKeyObjectHandle = userSession.createObject(desKeyTemplate);
Refer to the PKCS#11 standard to find out what attributes must be set for certain types
of objects to create them on the token.template - The template object that holds all values that the new object on the token should
contain.TokenException - If the creation of the new object fails. If it fails, the no new object was
created on the token.public long createPrivateKeyObject(AttributeVector template, PublicKey publicKey) throws TokenException
TokenExceptionpublic long createECPrivateKeyObject(AttributeVector template, byte[] ecPoint) throws TokenException
template - Template of the EC private key.ecPoint - The encoded EC-Point. May be null.TokenException - if creating new object failed.public long copyObject(long sourceObjectHandle,
AttributeVector template)
throws TokenException
sourceObjectHandle - The source object of the copy operation.template - A template object whose attribute values are used for the new object; i.e. they have
higher priority than the attribute values from the source object. May be null; in that
case the new object is just a one-to-one copy of the sourceObject.TokenException - If copying the object fails for some reason.public void setAttributeValues(long objectToUpdateHandle,
AttributeVector template)
throws TokenException
objectToUpdateHandle - The attributes of this object get updated.template - This method gets all present attributes of this template object and set this
attributes at the objectToUpdate.TokenException - If updating the attributes fails. All or no attributes are updated.public void destroyObject(long objectHandle)
throws TokenException
objectHandle - The object handle that should be destroyed.TokenException - If the object could not be destroyed.public long[] destroyObjects(long... objectHandles)
throws TokenException
objectHandles - The object handles that should be destroyed.TokenException - If could not get a valid session.public List<Long> destroyObjects(List<Long> objectHandles) throws TokenException
TokenExceptionpublic long getObjectSize(long objectHandle)
throws TokenException
objectHandle - The object to get the size for.TokenException - If determining the size fails.public byte[] generateUniqueId(AttributeVector template, int idLength, Random random) throws TokenException
template - The search criteria for the uniqueness.idLength - Length of the CKA_ID.random - random to generate the random CKA_ID.TokenException - If executing operation fails.public PKCS11Key getKey(PKCS11KeyId keyId) throws TokenException
PKCS11Key identified by the given PKCS11KeyId.keyId - The key identifier.PKCS11Key identified by the given PKCS11KeyId.TokenException - If executing operation fails.public PKCS11Key getKey(AttributeVector criteria) throws TokenException
PKCS11Key of a key satisfying the given criteria.criteria - The criteria. At one of the CKA_ID and CKA_LABEL must be set.PKCS11Key of a key satisfying the given criteriaTokenException - If executing operation fails.public PKCS11KeyId getKeyId(AttributeVector criteria) throws TokenException
PKCS11KeyId of a key satisfying the given criteria.criteria - The criteria. At one of the CKA_ID and CKA_LABEL must be set.PKCS11KeyId of a key satisfying the given criteriaTokenException - If executing operation fails.public long[] findAllObjects(AttributeVector template) throws TokenException
template - The object that serves as a template for searching. If this object is null, the find
operation will find all objects that this session can see. Notice, that only a user
session will see private objects.TokenException - if finding objects failed.public long[] findObjects(AttributeVector template, int maxObjectCount) throws TokenException
template - The object that serves as a template for searching. If this object is null, the find
operation will find all objects that this session can see. Notice, that only a user
session will see private objects.maxObjectCount - Specifies how many objects to return with this call.TokenException - if finding objects failed.public byte[] encrypt(Mechanism mechanism, long keyHandle, byte[] plaintext) throws TokenException
mechanism - The mechanism to use; e.g. Mechanism.DES_CBC.keyHandle - The decryption key to use.plaintext - the to-be-encrypted datanull.TokenException - If encrypting failed.public int encrypt(OutputStream out, Mechanism mechanism, long keyHandle, InputStream plaintext) throws TokenException, IOException
out - Stream to which the cipher text is written.mechanism - The mechanism to use.keyHandle - The decryption key to use.plaintext - Input-stream of the to-be-encrypted dataTokenException - If encrypting the data failed.IOException - if reading data from the plaintext stream failed or writing to the ciphertext stream failed.public byte[] decrypt(Mechanism mechanism, long keyHandle, byte[] ciphertext) throws TokenException
mechanism - The mechanism to use.keyHandle - The decryption key to use.ciphertext - the to-be-decrypted datanull.TokenException - If encrypting failed.public int decrypt(OutputStream out, Mechanism mechanism, long keyHandle, InputStream ciphertext) throws TokenException, IOException
out - Stream to which the plain text is written.mechanism - The mechanism to use.keyHandle - The decryption key to use.ciphertext - Input-stream of the to-be-encrypted dataTokenException - If decrypting the data failed.IOException - if reading data from the ciphertext stream failed or writing to the plaintext stream failed.public byte[] digest(Mechanism mechanism, byte[] data) throws TokenException
mechanism - The mechanism to use; e.g. Mechanism.SHA_1.data - the to-be-digested datanull.TokenException - If digesting the data failed.public byte[] digestKey(Mechanism mechanism, long keyHandle) throws TokenException
mechanism - The mechanism to use; e.g. Mechanism.SHA_1.keyHandle - handle of the to-be-digested key.null.TokenException - If digesting the data failed.public byte[] digest(Mechanism mechanism, InputStream data) throws TokenException, IOException
mechanism - The mechanism to use; e.g. Mechanism.SHA_1.data - the to-be-digested datanull.TokenException - If digesting the data failed.IOException - if reading data from stream failed.public byte[] sign(Mechanism mechanism, long keyHandle, byte[] data) throws TokenException
mechanism - The mechanism to use.keyHandle - The signing key to use.data - The data to sign.null.TokenException - If signing the data failed.public byte[] sign(Mechanism mechanism, long keyHandle, InputStream data) throws TokenException, IOException
mechanism - The mechanism to use.keyHandle - The signing key to use.data - Input-stream of the to-be-signed dataTokenException - If signing the data failed.IOException - If reading data stream failed.public byte[] signRecover(Mechanism mechanism, long keyHandle, byte[] data) throws TokenException
mechanism - The mechanism to use.keyHandle - The signing key to use.data - The data to sign-recovers.null.TokenException - If signing the data failed.public boolean verify(Mechanism mechanism, long keyHandle, byte[] data, byte[] signature) throws TokenException
mechanism - The mechanism to use; e.g. Mechanism.RSA_PKCS.keyHandle - The verification key to use.data - The data that was signed.signature - The signature or MAC to verify.TokenException - If verifying the signature fails.public boolean verify(Mechanism mechanism, long keyHandle, InputStream data, byte[] signature) throws TokenException, IOException
mechanism - The mechanism to use.keyHandle - The signing key to use.data - Input-stream of the to-be-verified datasignature - the signature.TokenException - If signing the data failed.IOException - If reading data stream failed.public byte[] verifyRecover(Mechanism mechanism, long keyHandle, byte[] data) throws TokenException
mechanism - The mechanism to use.keyHandle - The signing key to use.data - The data to be verify-recovered.null.TokenException - If signing the data failed.public long generateKey(Mechanism mechanism, AttributeVector template) throws TokenException
mechanism - The mechanism to generate a key for; e.g. Mechanism.DES to generate a DES key.template - The template for the new key or domain parameters; e.g. a DESSecretKey object which
has set certain attributes.TokenException - If generating a new secret key or domain parameters failed.public PKCS11KeyPair generateKeyPair(Mechanism mechanism, KeyPairTemplate template) throws TokenException
mechanism - The mechanism to generate a key for; e.g. Mechanism.RSA to generate a new RSA
key-pair.template - The template for the new keypair.TokenException - If generating a new key-pair failed.public byte[] wrapKey(Mechanism mechanism, long wrappingKeyHandle, long keyHandle) throws TokenException
mechanism - The mechanism to use for wrapping the key.wrappingKeyHandle - The key to use for wrapping (encrypting).keyHandle - The key to wrap (encrypt).null.TokenException - If wrapping the key failed.public long unwrapKey(Mechanism mechanism, long unwrappingKeyHandle, byte[] wrappedKey, AttributeVector keyTemplate) throws TokenException
mechanism - The mechanism to use for unwrapping the key.unwrappingKeyHandle - The key to use for unwrapping (decrypting).wrappedKey - The encrypted key to unwrap (decrypt).keyTemplate - The template for creating the new key object.TokenException - If unwrapping the key or creating a new key object failed.public long deriveKey(Mechanism mechanism, long baseKeyHandle, AttributeVector template) throws TokenException
mechanism - The mechanism to use for deriving the new key from the base key.baseKeyHandle - The key to use as base for derivation.template - The template for creating the new key object.TokenException - If deriving the key or creating a new key object failed.public byte[] generateRandom(int numberOfBytesToGenerate)
throws TokenException
numberOfBytesToGenerate - The number of random bytes to generate.TokenException - If generating random bytes failed.public byte[] generateRandom(int numberOfBytesToGenerate,
byte[] extraSeed)
throws TokenException
numberOfBytesToGenerate - The number of random bytes to generate.extraSeed - The seed bytes to mix in.TokenException - If generating random bytes failed.public byte[][] encryptMessages(Mechanism mechanism, long keyHandle, EncryptMessageBytesEntry[] entries) throws TokenException
mechanism - The encryption mechanismkeyHandle - Handle of the encryption key.entries - Arrays of plaintexts in byte[] with additional parameters to be encrypted.TokenException - If encrypting failed.public int[] encryptMessages(Mechanism mechanism, long keyHandle, EncryptMessageStreamEntry[] entries) throws TokenException, IOException
mechanism - The encryption mechanismkeyHandle - Handle of the encryption key.entries - Arrays of plaintexts in stream with additional parameters to be encrypted.TokenException - If encrypting failed.IOException - if reading or writing stream failed.public byte[][] decryptMessages(Mechanism mechanism, long keyHandle, DecryptMessageBytesEntry[] entries) throws TokenException
mechanism - The encryption mechanismkeyHandle - Handle of the encryption key.entries - Arrays of ciphertexts in byte[] with additional parameters to be encrypted.TokenException - If encrypting failed.public int[] decryptMessages(Mechanism mechanism, long keyHandle, DecryptMessageStreamEntry[] entries) throws TokenException, IOException
mechanism - The encryption mechanismkeyHandle - Handle of the encryption key.entries - Arrays of ciphertexts in stream with additional parameters to be encrypted.TokenException - If encrypting failed.IOException - if reading or writing stream failed.public byte[][] signMessages(Mechanism mechanism, long keyHandle, SignMessageBytesEntry[] entries) throws TokenException
mechanism - The encryption mechanismkeyHandle - Handle of the signing key.entries - Arrays of messages in byte[] with additional parameters to be encrypted.TokenException - If signing failed.public byte[][] signMessages(Mechanism mechanism, long keyHandle, SignMessageStreamEntry[] entries) throws TokenException, IOException
mechanism - The encryption mechanismkeyHandle - Handle of the signing key.entries - Arrays of messages in stream with additional parameters to be encrypted.TokenException - If signing failed.IOException - if reading or writing stream failed.public boolean[] verifyMessages(Mechanism mechanism, long keyHandle, VerifyMessageBytesEntry[] entries) throws TokenException
TokenExceptionpublic boolean[] verifyMessages(Mechanism mechanism, long keyHandle, VerifyMessageStreamEntry[] entries) throws TokenException, IOException
mechanism - The signature verification mechanismkeyHandle - Handle of the verification key.entries - Arrays of messages in stream with additional parameters to be verified.TokenException - If verifying failed.IOException - if reading or writing stream failed.public String toString()
public AttributeVector getAttrValues(long objectHandle, long... attributeTypes) throws TokenException
objectHandle - the object handle.attributeTypes - types of attributes to be read.TokenException - if getting attributes failed.public AttributeVector getAttrValues(long objectHandle, List<Long> attributeTypes) throws TokenException
objectHandle - the object handle.attributeTypes - types of attributes to be read.TokenException - if getting attributes failed.public AttributeVector getDefaultAttrValues(long objectHandle) throws TokenException
objectHandle - the object handle.TokenException - if getting attributes failed.Copyright © 2023. All rights reserved.