com.unbound.client.pkcs11.PKCS11Partition Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unbound-java-provider Show documentation
Show all versions of unbound-java-provider Show documentation
This is a collection of JAVA libraries that implement Unbound cryptographic classes for JAVA provider, PKCS11 wrapper, cryptoki, and advapi
package com.unbound.client.pkcs11;
import com.dyadicsec.cryptoki.*;
import com.unbound.client.Session;
import com.unbound.provider.UBKeyStore;
import com.unbound.client.Partition;
import java.security.ProviderException;
import java.util.*;
public final class PKCS11Partition extends Partition
{
static final Map partitions = new HashMap();
static PKCS11Partition defaultPartition = null;
private PKCS11Session loginSession = null;
static void register(int slotId)
{
try
{
CK_SLOT_INFO info = Library.C_GetSlotInfo(slotId);
String name = new String(info.slotDescription);
PKCS11Partition partition = new PKCS11Partition(name, slotId);
if (defaultPartition==null) defaultPartition = partition;
partitions.put(name, partition);
}
catch (CKR_Exception e) { }
}
public static synchronized PKCS11Partition get(String name)
{
if (name==null) return defaultPartition;
return partitions.get(name);
}
private final String name;
private final int slotId;
private final UBKeyStore keyStore = new UBKeyStore(this);
private final Queue sessions = new LinkedList();
private int userReqAuth = -1;
public int getSlotId()
{
return slotId;
}
PKCS11Partition(String name, int slotId)
{
this.name = name;
this.slotId = slotId;
}
@Override
public String getName()
{
return name;
}
@Override
public UBKeyStore getKeyStore()
{
return keyStore;
}
@Override
public Session acquireSession()
{
synchronized (sessions)
{
if (!sessions.isEmpty()) return sessions.remove();
}
try { return PKCS11Session.open(this, slotId); }
catch (CKR_Exception e) { throw new ProviderException(e); }
}
public void releaseSession(Session session)
{
PKCS11Session pkcs11Session = (PKCS11Session)session;
if (pkcs11Session.isOperationInProgress())
{
pkcs11Session.close();
return;
}
synchronized (sessions)
{
sessions.add(pkcs11Session);
}
}
private synchronized boolean isUserLoginRequired()
{
if (userReqAuth<0)
{
try
{
CK_TOKEN_INFO info = Library.C_GetTokenInfo(slotId);
userReqAuth = ((info.flags & CK.CKF_LOGIN_REQUIRED)==0) ? 0 : 1;
}
catch (CKR_Exception e) { throw new ProviderException(e); }
}
return userReqAuth>0;
}
@Override
public synchronized void login(char[] password)
{
try
{
boolean so = false;
if (password!=null && password.length>0)
{
StringTokenizer stok = new StringTokenizer(new String(password), "\t\n\r\f\" :,{}");
String[] tokens = new String[stok.countTokens()];
for(int i=0; i2 && tokens[0].equalsIgnoreCase("USERNAME") && tokens[1].equalsIgnoreCase("SO");
}
if (!so && !isUserLoginRequired()) return;
if (loginSession==null) loginSession = PKCS11Session.open(this, slotId);
Library.C_Login(loginSession.getHandle(), CK.DYCKU_USER_CHECK, password);
}
catch (CKR_Exception e) { throw new ProviderException(e); }
}
}