public class TokenManager
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
protected java.util.Set |
activeSessions_
Holds the currently active sessions_.
|
protected java.util.Set |
keyStores_
This is a set of SoftReferences.
|
protected java.util.LinkedList |
lockedSessionROPool_
The pool of PKCS#11 read-only sessions for which there is a close lock.
|
protected static java.lang.Object |
moduleMapsLock_
This is used as synchronization lock for accessing the modules maps.
|
protected static java.util.HashMap |
modulesUsageCounterMap_
The keys are set of already loaded and initialized Modules, to avoid multiple calls to
initialize of the same Module.
|
protected IAIKPkcs11 |
myProvider_
Get the configuration from this provider.
|
protected int |
openedSessionsTotal_
This is the total number of session that this token manager opened (including any already
closed sessions).
|
protected int |
openSessions_
This is the number of currently open sessions in general, not only those we have in our pool.
|
protected iaik.pkcs.pkcs11.Module |
pkcs11module_
Reference to the PKCS#11 module of the wrapper.
|
protected java.lang.String |
pkcs11ModulePath_
Path to the PKCS#11 driver; e.g.
|
protected java.util.Map |
sessionCloseLockCounts_
The sessions of session keys.
|
protected int |
sessionPoolMaxSize_
This is the maximum number of sessions to keep in to pool of open sessions.
|
protected java.util.LinkedList |
sessionROPool_
The pool of PKCS#11 read-only session objects.
|
protected java.util.LinkedList |
sessionRWPool_
The pool of PKCS#11 read-write session objects.
|
protected int |
sessionsInPool_
This is the number of sessions currently in our pool.
|
protected java.util.Set |
sessionsToBeClosed_
This set contains sessions that must be closed when they are no longer used; i.e.
|
protected iaik.pkcs.pkcs11.Slot |
slot_
Reference to the PKCS#11 slot this token manager works with.
|
protected static java.util.HashMap |
usedModulesMap_
A map that contains all modules in use.
|
protected static java.util.HashMap |
usedSlotsMap_
The map of slots already used by n token managers.
|
Constructor and Description |
---|
TokenManager(IAIKPkcs11 myProvider)
Create and initialize this token manager.
|
Modifier and Type | Method and Description |
---|---|
void |
acquireSessionCloseLock(iaik.pkcs.pkcs11.Session session)
Acquires a lock on this session which prevents that this token manager will close this session.
|
protected void |
addKeyStoreReference(TokenKeyStoreSpi keyStore)
Notify this token manager that a new key store object has been created.
|
void |
clearSessionPool(boolean tryToCloseSessions)
Clears the internal session pool of this token manager.
|
void |
closeSession(iaik.pkcs.pkcs11.Session session)
This method closes the given session and does not put it in the session pool.
|
protected void |
closeSessionNow(iaik.pkcs.pkcs11.Session session)
This method definitely closes the given session unconditionally.
|
void |
closeSessions()
Close all sessions managed by this provider instance.
|
void |
disposeSession(iaik.pkcs.pkcs11.Session session)
Dispose the given session that is no longer used.
|
void |
finalize()
Try to finalize the underlying PKCS#11 module if there is no other token manager that uses the
same module.
|
static TokenManager |
getDefaultTokenManager()
Gets the token manager of the first registered pkcs11 provider.
|
TokenKeyStore |
getKeyStore()
Get a key store that is associated with the slot of this token manager.
|
TokenKeyStore |
getKeyStore(java.lang.String keyStoreName)
Get a key store that is associated with the slot of this token manager.
|
iaik.pkcs.pkcs11.Module |
getModule()
Get the PKCS#11 module of this token manager.
|
java.lang.String |
getModulePath()
Get the PKCS#11 module path of this token manager.
|
IAIKPkcs11 |
getProvider()
Get the provider of this token manager.
|
iaik.pkcs.pkcs11.Session |
getSession(boolean rwBahavior)
Get a session with the given read-write behavior.
|
iaik.pkcs.pkcs11.Session |
getSession(boolean rwBahavior,
boolean forSessionKey) |
iaik.pkcs.pkcs11.Slot |
getSlot()
Get the slot of this token manager.
|
iaik.pkcs.pkcs11.Token |
getToken()
Get the token that is in the slot of this token manager.
|
boolean |
isMechanismFeatureSupported(iaik.pkcs.pkcs11.Mechanism[] mechanisms,
iaik.pkcs.pkcs11.MechanismInfo[][] mechanismFeatures)
Check, if the current token supports one of the given mechanism and the given features of this
mechanism.
|
boolean |
isMechanismFeatureSupported(iaik.pkcs.pkcs11.Mechanism mechanism,
iaik.pkcs.pkcs11.MechanismInfo mechanismFeatures)
Check, if the current token supports the given mechanism and the given features of this
mechanism.
|
boolean |
isRemovable()
This method returns
true if the slot of this token manager is a slot with a
removable token. |
boolean |
isSessionCloseLocked(iaik.pkcs.pkcs11.Session session)
Check, if the given session is close-locked; i.e.
|
protected boolean |
isToBeClosed(iaik.pkcs.pkcs11.Session session)
Check if the given session is to be closed.
|
boolean |
isTokenPresent()
Check, if there is a token present in the slot of this token manager.
|
boolean |
login(boolean SORole,
char[] PIN)
Login.
|
boolean |
login(iaik.pkcs.pkcs11.Session session,
boolean SORole,
char[] PIN)
Login the user into the given session.
|
boolean |
loginSO(char[] PIN)
Open a read-only user session and login as SO.
|
boolean |
loginUser(char[] PIN)
Open a read-only user session and login the user.
|
void |
logout()
Log out the current user (user or SO).
|
void |
logout(iaik.pkcs.pkcs11.Session session)
Deprecated.
|
boolean |
makeAuthorizedSession(iaik.pkcs.pkcs11.Session session,
boolean SORole,
char[] PIN)
Make the given session authorized.
|
boolean |
makeAuthorizedSession(iaik.pkcs.pkcs11.Session session,
char[] PIN)
Make the given session authorized.
|
void |
notifyKeyStores()
Notify all key store which use this token manager that they should refresh their contents.
|
void |
releaseSessionCloseLock(iaik.pkcs.pkcs11.Session session)
Releases a previously acquired close-lock for the given session.
|
protected void |
setToBeClosed(iaik.pkcs.pkcs11.Session session)
Remember the given session to be closed.
|
void |
setUserPIN(iaik.pkcs.pkcs11.Session session,
char[] oldUserPIN,
char[] newUserPIN)
Set or change the user PIN of the token.
|
java.lang.String |
toString()
Returns a string with the name and the version number of this provider.
|
void |
waitForSlotEvent()
This method blocks until an event for the slot of this token manager occurs.
|
protected static final java.lang.Object moduleMapsLock_
protected static final java.util.HashMap usedModulesMap_
protected static final java.util.HashMap modulesUsageCounterMap_
protected static final java.util.HashMap usedSlotsMap_
protected IAIKPkcs11 myProvider_
protected java.lang.String pkcs11ModulePath_
protected iaik.pkcs.pkcs11.Module pkcs11module_
protected iaik.pkcs.pkcs11.Slot slot_
protected java.util.Set activeSessions_
protected java.util.LinkedList sessionROPool_
protected java.util.LinkedList lockedSessionROPool_
protected java.util.LinkedList sessionRWPool_
protected int sessionPoolMaxSize_
protected int openSessions_
protected int sessionsInPool_
protected int openedSessionsTotal_
protected java.util.Map sessionCloseLockCounts_
protected java.util.Set sessionsToBeClosed_
protected java.util.Set keyStores_
public TokenManager(IAIKPkcs11 myProvider) throws java.io.IOException, iaik.pkcs.pkcs11.TokenException, IAIKPkcs11Exception
myProvider
- The provider to get the configuration from.java.io.IOException
- If loading the module fails.iaik.pkcs.pkcs11.TokenException
- If investigating the slots fails.IAIKPkcs11Exception
- If the configured module is unavailable.public void clearSessionPool(boolean tryToCloseSessions)
tryToCloseSessions
- If true, the method tries to close each session in the pool before throwing the object
away.public void disposeSession(iaik.pkcs.pkcs11.Session session)
session
- The session to dispose.public void closeSession(iaik.pkcs.pkcs11.Session session)
session
- The session to close.protected void closeSessionNow(iaik.pkcs.pkcs11.Session session)
session
- The session to dispose.public TokenKeyStore getKeyStore()
public TokenKeyStore getKeyStore(java.lang.String keyStoreName)
keyStoreName
- name of the keystore to create (e.g. PKCS11 for standard keystore, FastPKCS11 for
fastPKCS11Keystore)public iaik.pkcs.pkcs11.Module getModule()
public java.lang.String getModulePath()
public IAIKPkcs11 getProvider()
public iaik.pkcs.pkcs11.Session getSession(boolean rwBahavior) throws iaik.pkcs.pkcs11.TokenException, IAIKPkcs11TokenUnavailableException
rwBahavior
- Must be either Token.SessionReadWriteBehavior.RO_SESSION for read-only sessions or
Token.SessionReadWriteBehavior.RW_SESSION for read-write sessions.iaik.pkcs.pkcs11.TokenException
- If getting such a session fails.IAIKPkcs11TokenUnavailableException
- If there is no token in the slot.public iaik.pkcs.pkcs11.Session getSession(boolean rwBahavior, boolean forSessionKey) throws iaik.pkcs.pkcs11.TokenException, IAIKPkcs11TokenUnavailableException
iaik.pkcs.pkcs11.TokenException
IAIKPkcs11TokenUnavailableException
public iaik.pkcs.pkcs11.Slot getSlot()
public iaik.pkcs.pkcs11.Token getToken() throws iaik.pkcs.pkcs11.TokenException
iaik.pkcs.pkcs11.TokenException
- If getting the token object fails.public void acquireSessionCloseLock(iaik.pkcs.pkcs11.Session session)
session
- The session to acquire a lock.public void releaseSessionCloseLock(iaik.pkcs.pkcs11.Session session)
session
- The session to release the lock.protected void setToBeClosed(iaik.pkcs.pkcs11.Session session)
session
- The session to remember.protected boolean isToBeClosed(iaik.pkcs.pkcs11.Session session)
session
- The session to check.public boolean isSessionCloseLocked(iaik.pkcs.pkcs11.Session session)
session
- The session to release the lock.public boolean isMechanismFeatureSupported(iaik.pkcs.pkcs11.Mechanism mechanism, iaik.pkcs.pkcs11.MechanismInfo mechanismFeatures) throws iaik.pkcs.pkcs11.TokenException
mechanism
- The requested mechanism.mechanismFeatures
- The requested features of the mechanism. This is optional and may be null. In this
case, the method just checks the mechanism itself.iaik.pkcs.pkcs11.TokenException
- If getting the mechanism infos failed.public boolean isMechanismFeatureSupported(iaik.pkcs.pkcs11.Mechanism[] mechanisms, iaik.pkcs.pkcs11.MechanismInfo[][] mechanismFeatures) throws iaik.pkcs.pkcs11.TokenException
mechanisms
- The mechanisms of which at least one must be supported.mechanismFeatures
- The requested features of the mechanisms. The first-dimension length of this parameter
must be the same as the length of the mechanisms parameter. The arrays at each index
may be null. In this case, the method just checks the mechanism itself.iaik.pkcs.pkcs11.TokenException
- If getting the mechanism infos failed.public boolean isRemovable()
true
if the slot of this token manager is a slot with a
removable token. For smart cards reader, this is usually true
. HSMs often have
slots with fixed tokens which cannot be removed. In this case, this method returns
false
, meaning that the token is always present in the slot. This information may
be useful for certain applications.true
if the token in this slot is removable; e.g. if it is a smart card in
a card reader. false
if the token is fixed in the slot and cannot be
removed during runtime; e.g. a token of a HSM.public boolean isTokenPresent() throws iaik.pkcs.pkcs11.TokenException
iaik.pkcs.pkcs11.TokenException
- If getting the slot info fails.public boolean loginUser(char[] PIN) throws iaik.pkcs.pkcs11.TokenException, IAIKPkcs11AuthenticationCanceledException, IAIKPkcs11AuthenticationException
PIN
- The user PIN if already known, or null
if unavailable.true
, if this call did actually call the login method of the underlying
PKCS#11 module, false
, if the user was already logged in and a call to the
login method was not required.iaik.pkcs.pkcs11.TokenException
- If token or session handling fails.IAIKPkcs11AuthenticationCanceledException
- If user canceled the PIN entry.IAIKPkcs11AuthenticationException
- If the authentication failed.public boolean loginSO(char[] PIN) throws iaik.pkcs.pkcs11.TokenException, IAIKPkcs11AuthenticationCanceledException, IAIKPkcs11AuthenticationException
PIN
- The PIN if already known, or null
if unavailable.true
, if this call did actually call the login method of the underlying
PKCS#11 module, false
, if the user was already logged in and a call to the
login method was not required.iaik.pkcs.pkcs11.TokenException
- If token or session handling fails.IAIKPkcs11AuthenticationCanceledException
- If user canceled the PIN entry.IAIKPkcs11AuthenticationException
- If the authentication failed.public boolean login(boolean SORole, char[] PIN) throws iaik.pkcs.pkcs11.TokenException, IAIKPkcs11AuthenticationCanceledException, IAIKPkcs11AuthenticationException
If the session has already been logged in, this method will not try to login the session again
and return false
.
SORole
- Whether or not to login as Security Officer (SO)PIN
- The user PIN if already known, or null
if unavailable.true
, if this call did actually call the login method of the underlying
PKCS#11 module, false
, if the user was already logged in and a call to the
login method was not required.iaik.pkcs.pkcs11.TokenException
- If token or session handling fails.IAIKPkcs11AuthenticationCanceledException
- If user canceled the PIN entry.IAIKPkcs11AuthenticationException
- If the authentication failed.public boolean login(iaik.pkcs.pkcs11.Session session, boolean SORole, char[] PIN) throws iaik.pkcs.pkcs11.TokenException, IAIKPkcs11AuthenticationCanceledException, IAIKPkcs11AuthenticationException
If the session has already been logged in, this method will not try to login the session again
and return false
.
session
- The session to login. If the session is null
, this method will open a
read-only user session.SORole
- Whether or not to login as Security Officer (SO)PIN
- The user PIN if already known, or null
if unavailable.true
, if this call did actually call the login method of the underlying
PKCS#11 module, false
, if the user was already logged in and a call to the
login method was not required.iaik.pkcs.pkcs11.TokenException
- If token or session handling fails.IAIKPkcs11AuthenticationCanceledException
- If user canceled the PIN entry.IAIKPkcs11AuthenticationException
- If the authentication failed.public void logout() throws iaik.pkcs.pkcs11.TokenException
iaik.pkcs.pkcs11.TokenException
- If logout fails.public void logout(iaik.pkcs.pkcs11.Session session) throws iaik.pkcs.pkcs11.TokenException
null
, the configured login manager may acquire a suitable
session for logout.session
- The session to logout or null
to just logout from the token using any
session.iaik.pkcs.pkcs11.TokenException
- If logout fails.public void setUserPIN(iaik.pkcs.pkcs11.Session session, char[] oldUserPIN, char[] newUserPIN) throws iaik.pkcs.pkcs11.TokenException, IAIKPkcs11AuthenticationCanceledException, IAIKPkcs11AuthenticationException
session
- The session to use for setting the PIN. If this is null, the method will open a
read-write use session.oldUserPIN
- The old (current) user PIN if already known, or null if unavailable.newUserPIN
- The new user PIN if already known, or null if unavailable.iaik.pkcs.pkcs11.TokenException
- If changing the PIN fails.IAIKPkcs11AuthenticationCanceledException
- If the user PIN has to be prompted and the user canceled the PIN entry.IAIKPkcs11AuthenticationException
- If PIN verification failed.LoginManager.setUserPIN(TokenManager, Session, char[], char[])
public boolean makeAuthorizedSession(iaik.pkcs.pkcs11.Session session, char[] PIN) throws iaik.pkcs.pkcs11.TokenException, IAIKPkcs11TokenUnavailableException, IAIKPkcs11AuthenticationException
session
- The session to login, if a login is required.PIN
- The user PIN if already known, or null
is unavailable.true
, if this call did actually call the login method of the underlying
PKCS#11 module, false
, if the user was already logged in or a login is not
required by the token.iaik.pkcs.pkcs11.TokenException
- If disposing the session fails.IAIKPkcs11TokenUnavailableException
- If the token is unavailableIAIKPkcs11AuthenticationException
- If authenticating the user failed.public boolean makeAuthorizedSession(iaik.pkcs.pkcs11.Session session, boolean SORole, char[] PIN) throws iaik.pkcs.pkcs11.TokenException, IAIKPkcs11TokenUnavailableException, IAIKPkcs11AuthenticationException
session
- The session to login, if a login is required.PIN
- The user PIN if already known, or null
is unavailable.true
, if this call did actually call the login method of the underlying
PKCS#11 module, false
, if the user was already logged in or a login is not
required by the token.iaik.pkcs.pkcs11.TokenException
- If disposing the session fails.IAIKPkcs11TokenUnavailableException
- If the token is unavailableIAIKPkcs11AuthenticationException
- If authenticating the user failed.public void waitForSlotEvent() throws iaik.pkcs.pkcs11.TokenException
iaik.pkcs.pkcs11.TokenException
- If waiting for an event fails.protected void addKeyStoreReference(TokenKeyStoreSpi keyStore)
keyStore
- The new key store object.public void notifyKeyStores()
public java.lang.String toString()
toString
in class java.lang.Object
public void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable
- If anything fails. Caught by VM.public void closeSessions()
public static TokenManager getDefaultTokenManager()
IAIK JavaSecurity Website https://jce.iaik.tugraz.at/
IAIK at Graz University of Technology, Austria, Europe
Copyright 2001-2023 IAIK, Graz University of Technology, Inffeldgasse 16a, 8010 Graz, Austria. All Rights Reserved. Version 1.9.4