Search in sources :

Example 11 with CryptokiException

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!");
}
Also used : Scanner(java.util.Scanner) CryptokiException(org.openecard.mdlw.sal.exceptions.CryptokiException) Test(org.testng.annotations.Test)

Example 12 with CryptokiException

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;
}
Also used : CryptokiException(org.openecard.mdlw.sal.exceptions.CryptokiException) NativeLong(com.sun.jna.NativeLong) ArrayList(java.util.ArrayList) NativeLong(com.sun.jna.NativeLong) CK_ATTRIBUTE(org.openecard.mdlw.sal.cryptoki.CK_ATTRIBUTE) NativeLongByReference(com.sun.jna.ptr.NativeLongByReference)

Example 13 with CryptokiException

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;
}
Also used : CryptokiException(org.openecard.mdlw.sal.exceptions.CryptokiException) NativeLong(com.sun.jna.NativeLong) NativeLong(com.sun.jna.NativeLong) ArrayList(java.util.ArrayList) CK_ATTRIBUTE(org.openecard.mdlw.sal.cryptoki.CK_ATTRIBUTE) NativeLongByReference(com.sun.jna.ptr.NativeLongByReference)

Example 14 with CryptokiException

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);
}
Also used : CryptokiException(org.openecard.mdlw.sal.exceptions.CryptokiException) CK_C_INITIALIZE_ARGS(org.openecard.mdlw.sal.cryptoki.CK_C_INITIALIZE_ARGS)

Example 15 with CryptokiException

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.");
    }
}
Also used : NativeLong(com.sun.jna.NativeLong) ByteBuffer(java.nio.ByteBuffer) SessionException(org.openecard.mdlw.sal.exceptions.SessionException) TokenException(org.openecard.mdlw.sal.exceptions.TokenException) CryptographicException(org.openecard.mdlw.sal.exceptions.CryptographicException) ThreadTerminateException(org.openecard.common.ThreadTerminateException) PinBlockedException(org.openecard.mdlw.sal.exceptions.PinBlockedException) CancellationException(org.openecard.mdlw.sal.exceptions.CancellationException) UnsupportedAlgorithmException(org.openecard.crypto.common.UnsupportedAlgorithmException) DataInvalidException(org.openecard.mdlw.sal.exceptions.DataInvalidException) PinIncorrectException(org.openecard.mdlw.sal.exceptions.PinIncorrectException) AlreadyInitializedException(org.openecard.mdlw.sal.exceptions.AlreadyInitializedException) InvalidArgumentsException(org.openecard.mdlw.sal.exceptions.InvalidArgumentsException) ExecutionException(java.util.concurrent.ExecutionException) CryptokiException(org.openecard.mdlw.sal.exceptions.CryptokiException) AuthenticationException(org.openecard.mdlw.sal.exceptions.AuthenticationException) FutureTask(java.util.concurrent.FutureTask) CryptokiException(org.openecard.mdlw.sal.exceptions.CryptokiException) ExecutionException(java.util.concurrent.ExecutionException) ThreadTerminateException(org.openecard.common.ThreadTerminateException)

Aggregations

CryptokiException (org.openecard.mdlw.sal.exceptions.CryptokiException)24 ArrayList (java.util.ArrayList)7 NativeLong (com.sun.jna.NativeLong)5 ConnectionHandleType (iso.std.iso_iec._24727.tech.schema.ConnectionHandleType)5 PinBlockedException (org.openecard.mdlw.sal.exceptions.PinBlockedException)5 PinIncorrectException (org.openecard.mdlw.sal.exceptions.PinIncorrectException)5 TokenException (org.openecard.mdlw.sal.exceptions.TokenException)5 NativeLongByReference (com.sun.jna.ptr.NativeLongByReference)4 DIDInfoType (iso.std.iso_iec._24727.tech.schema.DIDInfoType)4 CardStateEntry (org.openecard.common.sal.state.CardStateEntry)4 UnsupportedAlgorithmException (org.openecard.crypto.common.UnsupportedAlgorithmException)4 CK_ATTRIBUTE (org.openecard.mdlw.sal.cryptoki.CK_ATTRIBUTE)4 InitializationException (org.openecard.mdlw.sal.exceptions.InitializationException)4 ECardException (org.openecard.common.ECardException)3 ThreadTerminateException (org.openecard.common.ThreadTerminateException)3 IncorrectParameterException (org.openecard.common.sal.exception.IncorrectParameterException)3 WSMarshallerException (org.openecard.ws.marshal.WSMarshallerException)3 AccessControlListType (iso.std.iso_iec._24727.tech.schema.AccessControlListType)2 AccessRuleType (iso.std.iso_iec._24727.tech.schema.AccessRuleType)2 CardInfoType (iso.std.iso_iec._24727.tech.schema.CardInfoType)2