Search in sources :

Example 1 with ControlIFD

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

the class IFD method disconnect.

@Override
public synchronized DisconnectResponse disconnect(Disconnect parameters) {
    try {
        DisconnectResponse response;
        if (!hasContext()) {
            String msg = "Context not initialized.";
            Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.INVALID_SLOT_HANDLE, msg);
            response = WSHelper.makeResponse(DisconnectResponse.class, r);
            return response;
        }
        try {
            byte[] handle = parameters.getSlotHandle();
            SingleThreadChannel ch = cm.getSlaveChannel(handle);
            cm.closeSlaveChannel(handle);
            // process actions
            SCIOCard card = ch.getChannel().getCard();
            ActionType action = parameters.getAction();
            if (ActionType.RESET == action) {
                String ifdName = card.getTerminal().getName();
                SingleThreadChannel master = cm.getMasterChannel(ifdName);
                HandlerBuilder builder = HandlerBuilder.create();
                ConnectionHandleType cHandleIn = builder.setCardType(ECardConstants.UNKNOWN_CARD).setCardIdentifier(card.getATR().getBytes()).setContextHandle(ctxHandle).setIfdName(ifdName).setSlotIdx(BigInteger.ZERO).buildConnectionHandle();
                builder = HandlerBuilder.create();
                ConnectionHandleType cHandleRm = builder.setContextHandle(ctxHandle).setIfdName(ifdName).setSlotIdx(BigInteger.ZERO).buildConnectionHandle();
                try {
                    master.reconnect();
                    evManager.resetCard(cHandleRm, cHandleIn, card.getProtocol().toUri());
                } catch (IllegalStateException ex) {
                    LOG.warn("Card reconnect failed, trying to establish new card connection.", ex);
                    cm.closeMasterChannel(ifdName);
                    LOG.debug("Master channel closed successfully.");
                    try {
                        cm.getMasterChannel(ifdName);
                        LOG.debug("New card connection established successfully.");
                        evManager.resetCard(cHandleRm, cHandleIn, card.getProtocol().toUri());
                    } catch (NoSuchTerminal ex2) {
                        LOG.error("No terminal present anymore.", ex);
                    }
                }
            }
            // TODO: take care of other actions (probably over ControlIFD)
            // the default is to not disconnect the card, because all existing connections would be broken
            response = WSHelper.makeResponse(DisconnectResponse.class, WSHelper.makeResultOK());
            return response;
        } catch (NoSuchChannel ex) {
            String msg = "No card available in the requested terminal.";
            Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.INVALID_SLOT_HANDLE, msg);
            response = WSHelper.makeResponse(DisconnectResponse.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(DisconnectResponse.class, r);
            LOG.warn(msg, ex);
            return response;
        }
    } catch (Exception ex) {
        LOG.warn(ex.getMessage(), ex);
        throwThreadKillException(ex);
        return WSHelper.makeResponse(DisconnectResponse.class, WSHelper.makeResult(ex));
    }
}
Also used : ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) ActionType(iso.std.iso_iec._24727.tech.schema.ActionType) NoSuchTerminal(org.openecard.common.ifd.scio.NoSuchTerminal) SingleThreadChannel(org.openecard.ifd.scio.wrapper.SingleThreadChannel) NoSuchChannel(org.openecard.ifd.scio.wrapper.NoSuchChannel) SCIOException(org.openecard.common.ifd.scio.SCIOException) SCIOCard(org.openecard.common.ifd.scio.SCIOCard) 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) DisconnectResponse(iso.std.iso_iec._24727.tech.schema.DisconnectResponse) HandlerBuilder(org.openecard.common.util.HandlerBuilder)

Example 2 with ControlIFD

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

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

the class IFD method controlIFD.

/**
 * Note: the first byte of the command data is the control code.
 */
@Override
public ControlIFDResponse controlIFD(ControlIFD parameters) {
    ControlIFDResponse response;
    if (!hasContext()) {
        String msg = "Context not initialized.";
        Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.INVALID_SLOT_HANDLE, msg);
        response = WSHelper.makeResponse(ControlIFDResponse.class, r);
        return response;
    }
    byte[] handle = parameters.getSlotHandle();
    byte[] command = parameters.getCommand();
    if (handle == null || command == null) {
        String msg = "Missing parameter.";
        Result r = WSHelper.makeResultUnknownError(msg);
        response = WSHelper.makeResponse(ControlIFDResponse.class, r);
        return response;
    }
    byte ctrlCode = command[0];
    command = Arrays.copyOfRange(command, 1, command.length);
    try {
        SingleThreadChannel ch = cm.getSlaveChannel(handle);
        TerminalInfo info = new TerminalInfo(cm, ch);
        Integer featureCode = info.getFeatureCodes().get(Integer.valueOf(ctrlCode));
        // see if the terminal can deal with that
        if (featureCode != null) {
            byte[] resultCommand = ch.transmitControlCommand(featureCode, command);
            // evaluate result
            Result result = evaluateControlIFDRAPDU(resultCommand);
            response = WSHelper.makeResponse(ControlIFDResponse.class, result);
            response.setResponse(resultCommand);
            return response;
        } else {
            String msg = "The terminal is not capable of performing the requested action.";
            Result r = WSHelper.makeResultUnknownError(msg);
            response = WSHelper.makeResponse(ControlIFDResponse.class, r);
            return response;
        }
    } catch (NoSuchChannel | IllegalStateException ex) {
        String msg = "The card or the terminal is not available anymore.";
        Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.Terminal.UNKNOWN_IFD, msg);
        response = WSHelper.makeResponse(ControlIFDResponse.class, r);
        LOG.warn(msg, ex);
        return response;
    } catch (SCIOException ex) {
        String msg = "Unknown error while sending transmit control command.";
        Result r = WSHelper.makeResultUnknownError(msg);
        response = WSHelper.makeResponse(ControlIFDResponse.class, r);
        LOG.warn(msg, ex);
        return response;
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) ControlIFDResponse(iso.std.iso_iec._24727.tech.schema.ControlIFDResponse) SingleThreadChannel(org.openecard.ifd.scio.wrapper.SingleThreadChannel) NoSuchChannel(org.openecard.ifd.scio.wrapper.NoSuchChannel) SCIOException(org.openecard.common.ifd.scio.SCIOException) TerminalInfo(org.openecard.ifd.scio.wrapper.TerminalInfo) Result(oasis.names.tc.dss._1_0.core.schema.Result)

Example 4 with ControlIFD

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

the class GenericPINAction method sendModifyPIN.

/**
 * Send a ModifyPIN-PCSC-Command to the Terminal.
 *
 * @throws IFDException If building the Command fails.
 */
private ControlIFDResponse sendModifyPIN() throws IFDException {
    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();
    ControlIFD controlIFD = new ControlIFD();
    controlIFD.setCommand(ByteUtils.concatenate((byte) PCSCFeatures.MODIFY_PIN_DIRECT, structData));
    controlIFD.setSlotHandle(slotHandle);
    return (ControlIFDResponse) dispatcher.safeDeliver(controlIFD);
}
Also used : PasswordAttributesType(iso.std.iso_iec._24727.tech.schema.PasswordAttributesType) ControlIFDResponse(iso.std.iso_iec._24727.tech.schema.ControlIFDResponse) ControlIFD(iso.std.iso_iec._24727.tech.schema.ControlIFD) PCSCPinModify(org.openecard.ifd.scio.reader.PCSCPinModify)

Example 5 with ControlIFD

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

the class PINStepAction method sendModifyPIN.

/**
 * Send a ModifyPIN-PCSC-Command to the Terminal.
 *
 * @throws IFDException If building the Command fails.
 */
private void sendModifyPIN() throws IFDException {
    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();
    ControlIFD controlIFD = new ControlIFD();
    controlIFD.setCommand(ByteUtils.concatenate((byte) PCSCFeatures.MODIFY_PIN_DIRECT, structData));
    controlIFD.setSlotHandle(conHandle.getSlotHandle());
    dispatcher.safeDeliver(controlIFD);
}
Also used : PasswordAttributesType(iso.std.iso_iec._24727.tech.schema.PasswordAttributesType) ControlIFD(iso.std.iso_iec._24727.tech.schema.ControlIFD) PCSCPinModify(org.openecard.ifd.scio.reader.PCSCPinModify)

Aggregations

ControlIFD (iso.std.iso_iec._24727.tech.schema.ControlIFD)3 ControlIFDResponse (iso.std.iso_iec._24727.tech.schema.ControlIFDResponse)3 PasswordAttributesType (iso.std.iso_iec._24727.tech.schema.PasswordAttributesType)3 PCSCPinModify (org.openecard.ifd.scio.reader.PCSCPinModify)3 Result (oasis.names.tc.dss._1_0.core.schema.Result)2 SCIOException (org.openecard.common.ifd.scio.SCIOException)2 NoSuchChannel (org.openecard.ifd.scio.wrapper.NoSuchChannel)2 SingleThreadChannel (org.openecard.ifd.scio.wrapper.SingleThreadChannel)2 ActionType (iso.std.iso_iec._24727.tech.schema.ActionType)1 Connect (iso.std.iso_iec._24727.tech.schema.Connect)1 ConnectionHandleType (iso.std.iso_iec._24727.tech.schema.ConnectionHandleType)1 DisconnectResponse (iso.std.iso_iec._24727.tech.schema.DisconnectResponse)1 EstablishChannel (iso.std.iso_iec._24727.tech.schema.EstablishChannel)1 EstablishChannelResponse (iso.std.iso_iec._24727.tech.schema.EstablishChannelResponse)1 EstablishContext (iso.std.iso_iec._24727.tech.schema.EstablishContext)1 ListIFDs (iso.std.iso_iec._24727.tech.schema.ListIFDs)1 BigInteger (java.math.BigInteger)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ThreadTerminateException (org.openecard.common.ThreadTerminateException)1