use of org.openecard.mdlw.sal.exceptions.CryptokiException in project open-ecard by ecsec.
the class CardInfoPrinter method test.
@Test
public void test() throws CryptokiException, InterruptedException {
MwModule module = new MwModule(mwConfig);
module.initialize();
List<MwSlot> list = module.getSlotList(true);
for (int i = 0; i < list.size(); i++) {
MwSlot s = list.get(i);
System.out.println(s.getSlotInfo().getSlotDescription());
System.out.println("SlotID: " + i);
}
System.out.println("");
System.out.println("SlotID eingeben: ");
Scanner in = new Scanner(System.in);
int num = in.nextInt();
MwSlot selectedSlot = list.get(num);
System.out.println("Manufactor: " + selectedSlot.getSlotInfo().getManufactor());
System.out.println("SlotDescription: " + selectedSlot.getSlotInfo().getSlotDescription());
MwToken token = selectedSlot.getTokenInfo();
System.out.println("");
System.out.println("#######################Infos########################");
System.out.println("");
System.out.println("PinLabel: " + token.getLabel());
System.out.println("");
System.out.println(String.format("ObjectIdentifier: %s_%s", token.getManufacturerID(), token.getModel()));
System.out.println("");
MwSession session = selectedSlot.openSession();
session.login(UserType.User, PIN_VALUE.toCharArray());
List<MwData> datas = session.getData();
List<MwPrivateKey> keys = session.getPrivateKeys();
for (MwPrivateKey key : keys) {
System.out.println("Private KeyType: " + key.getKeyTypeName());
System.out.println("Private KeyLabel: " + key.getKeyLabel());
System.out.println("");
}
System.out.println("");
List<MwPublicKey> pubKeys = session.getPublicKeys();
for (MwPublicKey pubKey : pubKeys) {
System.out.println("Public KeyType " + pubKey.getKeyTypeName());
System.out.println("Public KeyLabel: " + pubKey.getKeyLabel());
System.out.println("");
}
System.out.println("");
List<MwCertificate> certs = session.getCertificates();
for (MwCertificate cert : certs) {
try {
System.out.println("CertType: " + cert.getCertificateType());
System.out.println("CertLabel: " + cert.getLabel());
System.out.println("CertVal: " + cert.getValue());
System.out.println("");
} catch (CryptokiException ex) {
System.out.println("Skipping certificate due to error.");
ex.printStackTrace(System.out);
System.out.println("");
}
}
module.destroy();
System.out.println("");
System.out.println("####################################################");
System.out.println("Finished!");
}
use of org.openecard.mdlw.sal.exceptions.CryptokiException in project open-ecard by ecsec.
the class MwSession method getPublicKeys.
/**
* Returns all Public Keys from the Token of the selected Session
*
* @return List public keys.
* @throws CryptokiException
*/
public List<MwPublicKey> getPublicKeys() throws CryptokiException {
List<MwPublicKey> keyList = new ArrayList<>();
NativeLongByReference temp = new NativeLongByReference(new NativeLong(CryptokiLibrary.CKO_PUBLIC_KEY, true));
CK_ATTRIBUTE pTemplate = new CK_ATTRIBUTE();
pTemplate.setType(CKA_CLASS);
pTemplate.setPValue(temp.getPointer());
pTemplate.setUlValueLen(new NativeLong(NativeLong.SIZE));
List<Long> res = findObjects(pTemplate);
for (long l : res) {
try {
keyList.add(new MwPublicKey(l, mw, this));
} catch (CryptokiException ex) {
LOG.warn("Skipping public key due to error.", ex);
}
}
return keyList;
}
use of org.openecard.mdlw.sal.exceptions.CryptokiException in project open-ecard by ecsec.
the class MwSession method getPrivateKeys.
/**
* Returns all Private Keys from the Token of the selected Session
*
* @return List of private keys.
* @throws CryptokiException
*/
public List<MwPrivateKey> getPrivateKeys() throws CryptokiException {
LOG.debug("Trying to get private key objects from middleware.");
NativeLong privkey = new NativeLong(CryptokiLibrary.CKO_PRIVATE_KEY, true);
NativeLongByReference temp = new NativeLongByReference(privkey);
CK_ATTRIBUTE pTemplate = new CK_ATTRIBUTE();
pTemplate.setType(CKA_CLASS);
pTemplate.setPValue(temp.getPointer());
pTemplate.setUlValueLen(new NativeLong(NativeLong.SIZE));
List<Long> res = findObjects(pTemplate);
List<MwPrivateKey> keyList = new ArrayList<>();
for (long l : res) {
try {
MwPrivateKey key = new MwPrivateKey(l, mw, this);
LOG.debug("Found private key {} (handle={}).", key, l);
keyList.add(key);
} catch (CryptokiException ex) {
LOG.warn("Skipping private key due to error.", ex);
}
}
return keyList;
}
use of org.openecard.mdlw.sal.exceptions.CryptokiException in project open-ecard by ecsec.
the class MiddleWareWrapper method initialize.
public void initialize() throws CryptokiException {
try {
CK_C_INITIALIZE_ARGS arg = new CK_C_INITIALIZE_ARGS();
arg.setFlags(CryptokiLibrary.CKF_OS_LOCKING_OK);
initialize(arg);
return;
} catch (CryptokiException ex) {
LOG.warn("Failed to initialize middleware to perform locking by itself.");
}
try {
CK_C_INITIALIZE_ARGS arg = new CK_C_INITIALIZE_ARGS();
arg.setFlags(CryptokiLibrary.CKF_OS_LOCKING_OK);
MutexStore mutexStore = new MutexStore();
arg.setCreateMutex(mutexStore.getCreateMutexFun());
arg.setDestroyMutex(mutexStore.getDestroyMutexFun());
arg.setLockMutex(mutexStore.getLockMutexFun());
arg.setUnlockMutex(mutexStore.getUnlockMutexFun());
initialize(arg);
return;
} catch (CryptokiException ex) {
LOG.warn("Failed to initialize middleware to perform locking with Java locks.");
}
LOG.warn("Initializing middleware without thread safety values.");
initialize(null);
}
use of org.openecard.mdlw.sal.exceptions.CryptokiException in project open-ecard by ecsec.
the class MiddleWareWrapper method login.
public void login(final long hSession, final long userType, @Nullable byte[] pPin) throws CryptokiException {
ByteBuffer pinBytesTmp = null;
final NativeLong pinLen = new NativeLong(0);
if (pPin != null) {
pinBytesTmp = ByteBuffer.wrap(pPin);
pinLen.setValue(pPin.length);
}
final ByteBuffer pinBytes = pinBytesTmp;
try (LockedObject lo = lockInternal()) {
FutureTask<Void> task = new FutureTask<>(new Callable<Void>() {
@Override
public Void call() throws Exception {
// login to session with pin and usertype
check("C_Login", MiddleWareWrapper.this.lib.C_Login(new NativeLong(hSession), new NativeLong(userType), pinBytes, pinLen), (long) CryptokiLibrary.CKR_OK, (long) CryptokiLibrary.CKR_USER_ALREADY_LOGGED_IN);
return null;
}
});
Thread t = new Thread(task, "Middleware-Login");
t.setDaemon(true);
t.start();
try {
task.get();
} catch (ExecutionException ex) {
Throwable cause = ex.getCause();
if (cause instanceof CryptokiException) {
throw (CryptokiException) cause;
} else if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else {
throw new RuntimeException("Unexpected error received during C_Login call.", cause);
}
} catch (InterruptedException ex) {
task.cancel(true);
LOG.info("Interrupted while waiting for C_Login task.", ex);
throw new ThreadTerminateException("Waiting interrupted by an external thread.", ex);
}
} catch (InterruptedException ex) {
throw new IllegalStateException("Failed to release lock for middleware access.");
}
}
Aggregations