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));
}
}
}
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));
}
}
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;
}
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);
}
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);
}
Aggregations