Search in sources :

Example 11 with Result

use of oasis.names.tc.dss._1_0.core.schema.Result in project open-ecard by ecsec.

the class IFD method verifyUser.

@Override
public VerifyUserResponse verifyUser(VerifyUser parameters) {
    // TODO: convert to IFD Protocol
    try {
        VerifyUserResponse response;
        if (!hasContext()) {
            String msg = "Context not initialized.";
            Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.INVALID_SLOT_HANDLE, msg);
            response = WSHelper.makeResponse(VerifyUserResponse.class, r);
            return response;
        }
        SingleThreadChannel channel = cm.getSlaveChannel(parameters.getSlotHandle());
        AbstractTerminal aTerm = new AbstractTerminal(this, cm, channel, gui, ctxHandle, parameters.getDisplayIndex());
        try {
            response = aTerm.verifyUser(parameters);
            return response;
        } catch (IFDException ex) {
            response = WSHelper.makeResponse(VerifyUserResponse.class, ex.getResult());
            return response;
        }
    } catch (Exception ex) {
        LOG.warn(ex.getMessage(), ex);
        throwThreadKillException(ex);
        return WSHelper.makeResponse(VerifyUserResponse.class, WSHelper.makeResult(ex));
    }
}
Also used : SingleThreadChannel(org.openecard.ifd.scio.wrapper.SingleThreadChannel) VerifyUserResponse(iso.std.iso_iec._24727.tech.schema.VerifyUserResponse) ThreadTerminateException(org.openecard.common.ThreadTerminateException) SCIOException(org.openecard.common.ifd.scio.SCIOException) ExecutionException(java.util.concurrent.ExecutionException) Result(oasis.names.tc.dss._1_0.core.schema.Result)

Example 12 with Result

use of oasis.names.tc.dss._1_0.core.schema.Result in project open-ecard by ecsec.

the class IFD method wait.

@Override
public WaitResponse wait(Wait parameters) {
    WaitResponse response;
    // you thought of a different IFD obviously
    if (!ByteUtils.compare(ctxHandle, parameters.getContextHandle())) {
        String msg = "Invalid context handle specified.";
        Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.INVALID_CONTEXT_HANDLE, msg);
        response = WSHelper.makeResponse(WaitResponse.class, r);
        return response;
    }
    // get timeout value
    BigInteger timeout = parameters.getTimeOut();
    if (timeout == null) {
        timeout = BigInteger.valueOf(Long.MAX_VALUE);
    }
    if (timeout.signum() == -1 || timeout.signum() == 0) {
        String msg = "Invalid timeout value given, must be strictly positive.";
        Result r = WSHelper.makeResultUnknownError(msg);
        response = WSHelper.makeResponse(WaitResponse.class, r);
        return response;
    }
    long timeoutL;
    try {
        timeoutL = (long) timeout.doubleValue();
    } catch (ArithmeticException ex) {
        LOG.warn("Too big timeout value give, shortening to Long.MAX_VALUE.");
        timeoutL = Long.MAX_VALUE;
    }
    ChannelHandleType callback = parameters.getCallback();
    // callback is only useful with a protocol termination point
    if (callback != null && callback.getProtocolTerminationPoint() == null) {
        callback = null;
    }
    // if callback, generate session id
    String sessionId = null;
    if (callback != null) {
        ChannelHandleType newCallback = new ChannelHandleType();
        newCallback.setBinding(callback.getBinding());
        newCallback.setPathSecurity(callback.getPathSecurity());
        newCallback.setProtocolTerminationPoint(callback.getProtocolTerminationPoint());
        sessionId = ValueGenerators.genBase64Session();
        newCallback.setSessionIdentifier(sessionId);
        callback = newCallback;
    }
    try {
        EventWatcher watcher = new EventWatcher(cm, timeoutL, callback);
        List<IFDStatusType> initialState = watcher.start();
        // get expected status or initial status for all if none specified
        List<IFDStatusType> expectedState = parameters.getIFDStatus();
        if (expectedState.isEmpty()) {
            expectedState = initialState;
        } else {
            for (IFDStatusType s : expectedState) {
                // check that ifdname is present, needed for comparison
                if (s.getIFDName() == null) {
                    String msg = "IFD in a request IFDStatus not known.";
                    Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.Terminal.UNKNOWN_IFD, msg);
                    response = WSHelper.makeResponse(WaitResponse.class, r);
                    return response;
                }
                // check that at least one slot entry is present
                if (s.getSlotStatus().isEmpty()) {
                    // assume an empty one
                    SlotStatusType slot = new SlotStatusType();
                    slot.setCardAvailable(false);
                    slot.setIndex(BigInteger.ZERO);
                    s.getSlotStatus().add(slot);
                }
            }
        }
        watcher.setExpectedState(expectedState);
        // create the future and fire
        FutureTask<List<IFDStatusType>> future = new FutureTask<>(watcher);
        if (watcher.isAsync()) {
            // add future to async wait list
            asyncWaitThreads.put(sessionId, future);
            // finally run this darn thingy
            threadPool.execute(future);
            // prepare result with session id in it
            response = WSHelper.makeResponse(WaitResponse.class, WSHelper.makeResultOK());
            response.setSessionIdentifier(sessionId);
            return response;
        } else {
            // run wait in a future so it can be easily interrupted
            syncWaitThread = future;
            threadPool.execute(future);
            // get results from the future
            List<IFDStatusType> events = future.get();
            // prepare response
            response = WSHelper.makeResponse(WaitResponse.class, WSHelper.makeResultOK());
            response.getIFDEvent().addAll(events);
            return response;
        }
    } catch (SCIOException ex) {
        String msg = "Unknown SCIO error occured during wait call.";
        LOG.warn(msg, ex);
        Result r = WSHelper.makeResultUnknownError(msg);
        response = WSHelper.makeResponse(WaitResponse.class, r);
        return response;
    } catch (ExecutionException ex) {
        // this is the exception from within the future
        Throwable cause = ex.getCause();
        if (cause instanceof SCIOException) {
            String msg = "Unknown SCIO error occured during wait call.";
            LOG.warn(msg, cause);
            Result r = WSHelper.makeResultUnknownError(msg);
            response = WSHelper.makeResponse(WaitResponse.class, r);
        } else {
            String msg = "Unknown error during wait call.";
            LOG.error(msg, cause);
            Result r = WSHelper.makeResultUnknownError(msg);
            response = WSHelper.makeResponse(WaitResponse.class, r);
        }
        return response;
    } catch (InterruptedException ex) {
        String msg = "Wait interrupted by another thread.";
        LOG.warn(msg, ex);
        Result r = WSHelper.makeResultUnknownError(msg);
        response = WSHelper.makeResponse(WaitResponse.class, r);
        return response;
    }
}
Also used : SCIOException(org.openecard.common.ifd.scio.SCIOException) ChannelHandleType(iso.std.iso_iec._24727.tech.schema.ChannelHandleType) WaitResponse(iso.std.iso_iec._24727.tech.schema.WaitResponse) Result(oasis.names.tc.dss._1_0.core.schema.Result) FutureTask(java.util.concurrent.FutureTask) BigInteger(java.math.BigInteger) IFDStatusType(iso.std.iso_iec._24727.tech.schema.IFDStatusType) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) SlotStatusType(iso.std.iso_iec._24727.tech.schema.SlotStatusType) ExecutionException(java.util.concurrent.ExecutionException)

Example 13 with Result

use of oasis.names.tc.dss._1_0.core.schema.Result in project open-ecard by ecsec.

the class IFD method destroyChannel.

@Override
public DestroyChannelResponse destroyChannel(DestroyChannel parameters) {
    try {
        DestroyChannelResponse destroyChannelResponse = new DestroyChannelResponse();
        byte[] slotHandle = parameters.getSlotHandle();
        SingleThreadChannel channel = cm.getSlaveChannel(slotHandle);
        TerminalInfo termInfo = new TerminalInfo(cm, channel);
        // check if it is PACE and try to perform native implementation
        // get pace capabilities
        List<PACECapabilities.PACECapability> paceCapabilities = termInfo.getPACECapabilities();
        if (paceCapabilities.contains(PACECapabilities.PACECapability.DestroyPACEChannel)) {
            ExecutePACERequest execPaceReq = new ExecutePACERequest(ExecutePACERequest.Function.DestroyPACEChannel);
            byte[] reqData = execPaceReq.toBytes();
            LOG.debug("executeCtrlCode request: {}", ByteUtils.toHexString(reqData));
            // execute pace
            Map<Integer, Integer> features = termInfo.getFeatureCodes();
            byte[] resData = channel.transmitControlCommand(features.get(PCSCFeatures.EXECUTE_PACE), reqData);
            LOG.debug("Response of executeCtrlCode: {}", ByteUtils.toHexString(resData));
            // evaluate response
            ExecutePACEResponse execPaceRes = new ExecutePACEResponse(resData);
            if (execPaceRes.isError()) {
                destroyChannelResponse = WSHelper.makeResponse(DestroyChannelResponse.class, execPaceRes.getResult());
            }
        }
        channel.removeSecureMessaging();
        if (destroyChannelResponse.getResult() == null) {
            Result r = new Result();
            r.setResultMajor(ECardConstants.Major.OK);
            destroyChannelResponse.setResult(r);
        }
        return destroyChannelResponse;
    } catch (Throwable t) {
        return WSHelper.makeResponse(DestroyChannelResponse.class, WSHelper.makeResult(t));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) SingleThreadChannel(org.openecard.ifd.scio.wrapper.SingleThreadChannel) TerminalInfo(org.openecard.ifd.scio.wrapper.TerminalInfo) ExecutePACEResponse(org.openecard.ifd.scio.reader.ExecutePACEResponse) ExecutePACERequest(org.openecard.ifd.scio.reader.ExecutePACERequest) DestroyChannelResponse(iso.std.iso_iec._24727.tech.schema.DestroyChannelResponse) Result(oasis.names.tc.dss._1_0.core.schema.Result)

Example 14 with Result

use of oasis.names.tc.dss._1_0.core.schema.Result in project open-ecard by ecsec.

the class IFD method connect.

@Override
public ConnectResponse connect(Connect parameters) {
    try {
        ConnectResponse response;
        // check if the requested handle is valid
        if (!ByteUtils.compare(ctxHandle, parameters.getContextHandle())) {
            String msg = "Invalid context handle specified.";
            Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.INVALID_CONTEXT_HANDLE, msg);
            response = WSHelper.makeResponse(ConnectResponse.class, r);
            return response;
        } else {
            try {
                String name = parameters.getIFDName();
                // make sure the slot is connected before attemting to get a slave channel
                cm.openMasterChannel(name);
                byte[] slotHandle = cm.openSlaveChannel(name).p1;
                SingleThreadChannel ch = cm.getSlaveChannel(slotHandle);
                // make connection exclusive
                Boolean exclusive = parameters.isExclusive();
                if (exclusive != null && exclusive == true) {
                    BeginTransaction transact = new BeginTransaction();
                    transact.setSlotHandle(slotHandle);
                    BeginTransactionResponse resp = beginTransaction(transact);
                    if (resp.getResult().getResultMajor().equals(ECardConstants.Major.ERROR)) {
                        // destroy channel, when not successful here
                        ch.shutdown();
                        response = WSHelper.makeResponse(ConnectResponse.class, resp.getResult());
                        return response;
                    }
                }
                // connection established, return result
                response = WSHelper.makeResponse(ConnectResponse.class, WSHelper.makeResultOK());
                response.setSlotHandle(slotHandle);
                return response;
            } catch (NoSuchTerminal | NullPointerException ex) {
                String msg = "The requested terminal does not exist.";
                Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.Terminal.UNKNOWN_IFD, msg);
                response = WSHelper.makeResponse(ConnectResponse.class, r);
                LOG.warn(msg, ex);
                return response;
            } catch (IllegalStateException ex) {
                String msg = "No card available in the requested terminal.";
                Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.Terminal.NO_CARD, msg);
                response = WSHelper.makeResponse(ConnectResponse.class, r);
                LOG.warn(msg, ex);
                return response;
            } catch (SCIOException ex) {
                String msg = "Unknown error in the underlying SCIO implementation.";
                Result r = WSHelper.makeResultUnknownError(msg);
                response = WSHelper.makeResponse(ConnectResponse.class, r);
                LOG.warn(msg, ex);
                return response;
            }
        }
    } catch (Exception ex) {
        LOG.warn(ex.getMessage(), ex);
        throwThreadKillException(ex);
        return WSHelper.makeResponse(ConnectResponse.class, WSHelper.makeResult(ex));
    }
}
Also used : NoSuchTerminal(org.openecard.common.ifd.scio.NoSuchTerminal) ConnectResponse(iso.std.iso_iec._24727.tech.schema.ConnectResponse) SingleThreadChannel(org.openecard.ifd.scio.wrapper.SingleThreadChannel) SCIOException(org.openecard.common.ifd.scio.SCIOException) ThreadTerminateException(org.openecard.common.ThreadTerminateException) SCIOException(org.openecard.common.ifd.scio.SCIOException) ExecutionException(java.util.concurrent.ExecutionException) Result(oasis.names.tc.dss._1_0.core.schema.Result) BeginTransactionResponse(iso.std.iso_iec._24727.tech.schema.BeginTransactionResponse) BeginTransaction(iso.std.iso_iec._24727.tech.schema.BeginTransaction)

Example 15 with Result

use of oasis.names.tc.dss._1_0.core.schema.Result in project open-ecard by ecsec.

the class IFD method listIFDs.

@Override
public ListIFDsResponse listIFDs(ListIFDs parameters) {
    ListIFDsResponse response;
    if (!ByteUtils.compare(ctxHandle, parameters.getContextHandle())) {
        String msg = "Invalid context handle specified.";
        Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.INVALID_CONTEXT_HANDLE, msg);
        response = WSHelper.makeResponse(ListIFDsResponse.class, r);
        return response;
    } else {
        try {
            List<SCIOTerminal> terminals = cm.getTerminals().list();
            ArrayList<String> ifds = new ArrayList<>(terminals.size());
            for (SCIOTerminal next : terminals) {
                ifds.add(next.getName());
            }
            response = WSHelper.makeResponse(ListIFDsResponse.class, WSHelper.makeResultOK());
            response.getIFDName().addAll(ifds);
            return response;
        } catch (SCIOException ex) {
            LOG.warn(ex.getMessage(), ex);
            Result r = WSHelper.makeResultUnknownError(ex.getMessage());
            response = WSHelper.makeResponse(ListIFDsResponse.class, r);
            return response;
        }
    }
}
Also used : ListIFDsResponse(iso.std.iso_iec._24727.tech.schema.ListIFDsResponse) SCIOException(org.openecard.common.ifd.scio.SCIOException) SCIOTerminal(org.openecard.common.ifd.scio.SCIOTerminal) ArrayList(java.util.ArrayList) Result(oasis.names.tc.dss._1_0.core.schema.Result)

Aggregations

Result (oasis.names.tc.dss._1_0.core.schema.Result)42 InternationalStringType (oasis.names.tc.dss._1_0.core.schema.InternationalStringType)12 SCIOException (org.openecard.common.ifd.scio.SCIOException)11 SingleThreadChannel (org.openecard.ifd.scio.wrapper.SingleThreadChannel)11 DIDAuthenticateResponse (iso.std.iso_iec._24727.tech.schema.DIDAuthenticateResponse)9 BigInteger (java.math.BigInteger)8 ThreadTerminateException (org.openecard.common.ThreadTerminateException)8 Test (org.testng.annotations.Test)8 Document (org.w3c.dom.Document)8 Calendar (java.util.Calendar)7 GregorianCalendar (java.util.GregorianCalendar)7 ExecutionException (java.util.concurrent.ExecutionException)7 ConnectionHandleType (iso.std.iso_iec._24727.tech.schema.ConnectionHandleType)6 TransmitResponse (iso.std.iso_iec._24727.tech.schema.TransmitResponse)6 InitializeFrameworkResponse (de.bund.bsi.ecard.api._1.InitializeFrameworkResponse)4 BeginTransactionResponse (iso.std.iso_iec._24727.tech.schema.BeginTransactionResponse)4 DIDAuthenticate (iso.std.iso_iec._24727.tech.schema.DIDAuthenticate)4 GetIFDCapabilitiesResponse (iso.std.iso_iec._24727.tech.schema.GetIFDCapabilitiesResponse)4 Transmit (iso.std.iso_iec._24727.tech.schema.Transmit)4 IOException (java.io.IOException)4