Search in sources :

Example 1 with Connect

use of iso.std.iso_iec._24727.tech.schema.Connect in project open-ecard by ecsec.

the class IfdEventRunner method fireEvents.

private void fireEvents(@Nonnull List<IFDStatusType> diff) {
    for (IFDStatusType term : diff) {
        String ifdName = term.getIFDName();
        // find out if the terminal is new, or only a slot got updated
        IFDStatusType oldTerm = getCorresponding(ifdName, currentState);
        boolean terminalAdded = oldTerm == null;
        IFDCapabilitiesType slotCapabilities = getCapabilities(ifdName);
        if (terminalAdded) {
            // TERMINAL ADDED
            // make copy of term
            oldTerm = new IFDStatusType();
            oldTerm.setIFDName(ifdName);
            oldTerm.setConnected(true);
            // add to current list
            currentState.add(oldTerm);
            // create event
            ConnectionHandleType h = makeConnectionHandle(ifdName, null, slotCapabilities);
            LOG.debug("Found a terminal added event ({}).", ifdName);
            env.getEventDispatcher().notify(EventType.TERMINAL_ADDED, new IfdEventObject(h));
        }
        // check each slot
        for (SlotStatusType slot : term.getSlotStatus()) {
            SlotStatusType oldSlot = getCorresponding(slot.getIndex(), oldTerm.getSlotStatus());
            boolean cardPresent = slot.isCardAvailable();
            boolean cardWasPresent = oldSlot != null && oldSlot.isCardAvailable();
            if (cardPresent && !cardWasPresent) {
                // CARD INSERTED
                // copy slot and add to list
                SlotStatusType newSlot = oldSlot;
                if (newSlot == null) {
                    newSlot = new SlotStatusType();
                    oldTerm.getSlotStatus().add(newSlot);
                }
                newSlot.setIndex(slot.getIndex());
                newSlot.setCardAvailable(true);
                newSlot.setATRorATS(slot.getATRorATS());
                // create event
                LOG.debug("Found a card insert event ({}).", ifdName);
                LOG.info("Card with ATR={} inserted.", ByteUtils.toHexString(slot.getATRorATS()));
                ConnectionHandleType handle = makeUnknownCardHandle(ifdName, newSlot, slotCapabilities);
                env.getEventDispatcher().notify(EventType.CARD_INSERTED, new IfdEventObject(handle));
                try {
                    SingleThreadChannel ch = cm.openMasterChannel(ifdName);
                    if (evtManager.isRecognize()) {
                        String proto = ch.getChannel().getCard().getProtocol().toUri();
                        evtManager.threadPool.submit(new Recognizer(env, handle, proto));
                    }
                } catch (NoSuchTerminal | SCIOException ex) {
                    LOG.error("Failed to connect card, nevertheless sending CARD_INSERTED event.", ex);
                }
            } else if (!terminalAdded && !cardPresent && cardWasPresent) {
                // this makes only sense when the terminal was already there
                // CARD REMOVED
                // remove slot entry
                BigInteger idx = oldSlot.getIndex();
                Iterator<SlotStatusType> it = oldTerm.getSlotStatus().iterator();
                while (it.hasNext()) {
                    SlotStatusType next = it.next();
                    if (idx.equals(next.getIndex())) {
                        it.remove();
                        break;
                    }
                }
                LOG.debug("Found a card removed event ({}).", ifdName);
                ConnectionHandleType h = makeConnectionHandle(ifdName, idx, slotCapabilities);
                env.getEventDispatcher().notify(EventType.CARD_REMOVED, new IfdEventObject(h));
            }
        }
        // terminal removed event comes after card removed events
        boolean terminalPresent = term.isConnected();
        if (!terminalPresent) {
            // TERMINAL REMOVED
            Iterator<IFDStatusType> it = currentState.iterator();
            while (it.hasNext()) {
                IFDStatusType toDel = it.next();
                if (toDel.getIFDName().equals(term.getIFDName())) {
                    it.remove();
                }
            }
            ConnectionHandleType h = makeConnectionHandle(ifdName, null, slotCapabilities);
            LOG.debug("Found a terminal removed event ({}).", ifdName);
            env.getEventDispatcher().notify(EventType.TERMINAL_REMOVED, new IfdEventObject(h));
        }
    }
}
Also used : ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) NoSuchTerminal(org.openecard.common.ifd.scio.NoSuchTerminal) SingleThreadChannel(org.openecard.ifd.scio.wrapper.SingleThreadChannel) SCIOException(org.openecard.common.ifd.scio.SCIOException) IFDCapabilitiesType(iso.std.iso_iec._24727.tech.schema.IFDCapabilitiesType) Iterator(java.util.Iterator) BigInteger(java.math.BigInteger) IFDStatusType(iso.std.iso_iec._24727.tech.schema.IFDStatusType) SlotStatusType(iso.std.iso_iec._24727.tech.schema.SlotStatusType) IfdEventObject(org.openecard.common.event.IfdEventObject)

Example 2 with Connect

use of iso.std.iso_iec._24727.tech.schema.Connect 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 3 with Connect

use of iso.std.iso_iec._24727.tech.schema.Connect in project open-ecard by ecsec.

the class TerminalInfo method getStatus.

@Nonnull
public IFDStatusType getStatus() throws SCIOException {
    IFDStatusType status = new IFDStatusType();
    status.setIFDName(getName());
    status.setConnected(true);
    // set slot status type
    SlotStatusType stype = new SlotStatusType();
    status.getSlotStatus().add(stype);
    boolean cardPresent = isCardPresent();
    stype.setCardAvailable(cardPresent);
    stype.setIndex(BigInteger.ZERO);
    // get card status and stuff
    if (isConnected()) {
        SCIOATR atr = channel.getChannel().getCard().getATR();
        stype.setATRorATS(atr.getBytes());
    } else if (cardPresent) {
        // not connected, but card is present
        try {
            SingleThreadChannel ch = cm.openMasterChannel(getName());
            SCIOATR atr = ch.getChannel().getCard().getATR();
            stype.setATRorATS(atr.getBytes());
        } catch (NoSuchTerminal ex) {
            String msg = "Failed to connect card as terminal disappeared.";
            throw new SCIOException(msg, SCIOErrorCode.SCARD_E_UNKNOWN_READER, ex);
        }
    }
    // ifd status completely constructed
    return status;
}
Also used : NoSuchTerminal(org.openecard.common.ifd.scio.NoSuchTerminal) SCIOException(org.openecard.common.ifd.scio.SCIOException) IFDStatusType(iso.std.iso_iec._24727.tech.schema.IFDStatusType) SCIOATR(org.openecard.common.ifd.scio.SCIOATR) SlotStatusType(iso.std.iso_iec._24727.tech.schema.SlotStatusType) Nonnull(javax.annotation.Nonnull)

Example 4 with Connect

use of iso.std.iso_iec._24727.tech.schema.Connect in project open-ecard by ecsec.

the class PINTest method testModifyPin.

@Test(enabled = false)
public void testModifyPin() throws IFDException, WSMarshallerException, SAXException {
    IFD ifd = new IFD();
    ifd.setGUI(new SwingUserConsent(new SwingDialogWrapper()));
    EstablishContext eCtx = new EstablishContext();
    byte[] ctxHandle = ifd.establishContext(eCtx).getContextHandle();
    ListIFDs listIFDs = new ListIFDs();
    listIFDs.setContextHandle(ctxHandle);
    String ifdName = ifd.listIFDs(listIFDs).getIFDName().get(0);
    Connect connect = new Connect();
    connect.setContextHandle(ctxHandle);
    connect.setIFDName(ifdName);
    connect.setSlot(BigInteger.ZERO);
    byte[] slotHandle = ifd.connect(connect).getSlotHandle();
    // prepare pace call
    String xmlCall = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<iso:EstablishChannel xmlns:iso=\"urn:iso:std:iso-iec:24727:tech:schema\">\n" + "  <iso:SlotHandle>" + ByteUtils.toHexString(slotHandle) + "</iso:SlotHandle>\n" + "  <iso:AuthenticationProtocolData Protocol=\"urn:oid:0.4.0.127.0.7.2.2.4\">\n" + "    <iso:PinID>03</iso:PinID>\n" + "  </iso:AuthenticationProtocolData>\n" + "</iso:EstablishChannel>";
    WSMarshaller m = WSMarshallerFactory.createInstance();
    EstablishChannel eCh = (EstablishChannel) m.unmarshal(m.str2doc(xmlCall));
    // send pace call
    EstablishChannelResponse eChR = ifd.establishChannel(eCh);
    assertEquals(eChR.getResult().getResultMajor(), ECardConstants.Major.OK);
    PasswordAttributesType pwdAttr = create(true, ASCII_NUMERIC, 6, 6, 6);
    pwdAttr.setPadChar(new byte[] { (byte) 0x3F });
    PCSCPinModify ctrlStruct = new PCSCPinModify(pwdAttr, StringUtils.toByteArray("002C0203"));
    byte[] structData = ctrlStruct.toBytes();
    String pinStr = "00 2C 02 03 06 3F3F3F3F3F3F";
    String ctrlStr = "15 05 82 06 00 00 00 0606 01 02 02 0407 00 01 02 000000 0B000000";
    // This is the command the 'AusweisApp' sends
    // String ausweisApp = "150582080000000606010202090400010200000005000000002C020300";
    byte[] referenceData = StringUtils.toByteArray(ctrlStr + pinStr, true);
    assertEquals(referenceData, structData);
    ControlIFD controlIFD = new ControlIFD();
    controlIFD.setCommand(ByteUtils.concatenate((byte) PCSCFeatures.MODIFY_PIN_DIRECT, structData));
    controlIFD.setSlotHandle(slotHandle);
    ControlIFDResponse response = ifd.controlIFD(controlIFD);
}
Also used : ListIFDs(iso.std.iso_iec._24727.tech.schema.ListIFDs) PasswordAttributesType(iso.std.iso_iec._24727.tech.schema.PasswordAttributesType) ControlIFD(iso.std.iso_iec._24727.tech.schema.ControlIFD) ControlIFD(iso.std.iso_iec._24727.tech.schema.ControlIFD) PCSCPinModify(org.openecard.ifd.scio.reader.PCSCPinModify) Connect(iso.std.iso_iec._24727.tech.schema.Connect) EstablishChannelResponse(iso.std.iso_iec._24727.tech.schema.EstablishChannelResponse) WSMarshaller(org.openecard.ws.marshal.WSMarshaller) SwingDialogWrapper(org.openecard.gui.swing.SwingDialogWrapper) EstablishChannel(iso.std.iso_iec._24727.tech.schema.EstablishChannel) ControlIFDResponse(iso.std.iso_iec._24727.tech.schema.ControlIFDResponse) SwingUserConsent(org.openecard.gui.swing.SwingUserConsent) EstablishContext(iso.std.iso_iec._24727.tech.schema.EstablishContext) Test(org.testng.annotations.Test)

Example 5 with Connect

use of iso.std.iso_iec._24727.tech.schema.Connect in project open-ecard by ecsec.

the class PINTest method executePACE_PIN.

@Test(enabled = false)
public void executePACE_PIN() throws UnsupportedDataTypeException, JAXBException, SAXException, WSMarshallerException {
    IFD ifd = new IFD();
    ifd.setGUI(new SwingUserConsent(new SwingDialogWrapper()));
    EstablishContext eCtx = new EstablishContext();
    byte[] ctxHandle = ifd.establishContext(eCtx).getContextHandle();
    ListIFDs listIFDs = new ListIFDs();
    listIFDs.setContextHandle(ctxHandle);
    String ifdName = ifd.listIFDs(listIFDs).getIFDName().get(0);
    Connect connect = new Connect();
    connect.setContextHandle(ctxHandle);
    connect.setIFDName(ifdName);
    connect.setSlot(BigInteger.ZERO);
    byte[] slotHandle = ifd.connect(connect).getSlotHandle();
    // prepare pace call
    String xmlCall = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<iso:EstablishChannel xmlns:iso=\"urn:iso:std:iso-iec:24727:tech:schema\">\n" + "  <iso:SlotHandle>" + ByteUtils.toHexString(slotHandle) + "</iso:SlotHandle>\n" + "  <iso:AuthenticationProtocolData Protocol=\"urn:oid:0.4.0.127.0.7.2.2.4\">\n" + "    <iso:PinID>03</iso:PinID>\n" + "  </iso:AuthenticationProtocolData>\n" + "</iso:EstablishChannel>";
    WSMarshaller m = WSMarshallerFactory.createInstance();
    EstablishChannel eCh = (EstablishChannel) m.unmarshal(m.str2doc(xmlCall));
    // send pace call
    EstablishChannelResponse eChR = ifd.establishChannel(eCh);
}
Also used : SwingDialogWrapper(org.openecard.gui.swing.SwingDialogWrapper) ListIFDs(iso.std.iso_iec._24727.tech.schema.ListIFDs) EstablishChannel(iso.std.iso_iec._24727.tech.schema.EstablishChannel) ControlIFD(iso.std.iso_iec._24727.tech.schema.ControlIFD) Connect(iso.std.iso_iec._24727.tech.schema.Connect) EstablishChannelResponse(iso.std.iso_iec._24727.tech.schema.EstablishChannelResponse) SwingUserConsent(org.openecard.gui.swing.SwingUserConsent) WSMarshaller(org.openecard.ws.marshal.WSMarshaller) EstablishContext(iso.std.iso_iec._24727.tech.schema.EstablishContext) Test(org.testng.annotations.Test)

Aggregations

CardApplicationConnect (iso.std.iso_iec._24727.tech.schema.CardApplicationConnect)27 CardApplicationConnectResponse (iso.std.iso_iec._24727.tech.schema.CardApplicationConnectResponse)27 CardApplicationPathType (iso.std.iso_iec._24727.tech.schema.CardApplicationPathType)25 CardApplicationPath (iso.std.iso_iec._24727.tech.schema.CardApplicationPath)24 CardApplicationPathResponse (iso.std.iso_iec._24727.tech.schema.CardApplicationPathResponse)24 Test (org.testng.annotations.Test)23 Connect (iso.std.iso_iec._24727.tech.schema.Connect)11 ConnectionHandleType (iso.std.iso_iec._24727.tech.schema.ConnectionHandleType)10 DataSetList (iso.std.iso_iec._24727.tech.schema.DataSetList)7 DataSetListResponse (iso.std.iso_iec._24727.tech.schema.DataSetListResponse)7 EstablishContext (iso.std.iso_iec._24727.tech.schema.EstablishContext)7 ListIFDs (iso.std.iso_iec._24727.tech.schema.ListIFDs)7 ConnectResponse (iso.std.iso_iec._24727.tech.schema.ConnectResponse)6 CardApplicationList (iso.std.iso_iec._24727.tech.schema.CardApplicationList)5 CardApplicationListResponse (iso.std.iso_iec._24727.tech.schema.CardApplicationListResponse)5 CardApplicationDisconnect (iso.std.iso_iec._24727.tech.schema.CardApplicationDisconnect)4 EstablishChannel (iso.std.iso_iec._24727.tech.schema.EstablishChannel)4 EstablishChannelResponse (iso.std.iso_iec._24727.tech.schema.EstablishChannelResponse)4 BeginTransaction (iso.std.iso_iec._24727.tech.schema.BeginTransaction)3 BeginTransactionResponse (iso.std.iso_iec._24727.tech.schema.BeginTransactionResponse)3