Skip to content

Commit

Permalink
Do not use transactions on Windows, if exclusive mode has been specif…
Browse files Browse the repository at this point in the history
  • Loading branch information
martinpaljak committed Jan 22, 2016
1 parent be1e4ee commit fcad87f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/apdu4j/SCTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,17 @@ private static void work(CardTerminal reader, OptionSet args) throws CardExcepti
transport = SocketTransport.connect(string2socket(remote), null);
}

// Windows 8+ have the "5 seconds of transaction" limit. Because we want reliability
// and don't have access to arbitrary SCard* calls via javax.smartcardio, we rely on
// JNA interface and its EXCLUSIVE access instead and do NOT use the SCardBeginTransaction
// capability of the JNA interface.
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa379469%28v=vs.85%29.aspx
boolean transact = true;
if (System.getProperty("os.name").toLowerCase().contains("windows") && args.has(OPT_EXCLUSIVE)) {
transact = false;
}
// Connect the transport and the terminal
CmdlineRemoteTerminal c = new CmdlineRemoteTerminal(transport, reader);
CmdlineRemoteTerminal c = new CmdlineRemoteTerminal(transport, reader, transact);
c.forceProtocol(protocol);
// Run
c.run();
Expand Down
4 changes: 2 additions & 2 deletions src/apdu4j/remote/CmdlineRemoteTerminal.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public class CmdlineRemoteTerminal implements Runnable {
// The terminal that is tunneled
private JSONCardTerminalClient jsonterminal;

public CmdlineRemoteTerminal(JSONMessagePipe pipe, CardTerminal terminal) {
public CmdlineRemoteTerminal(JSONMessagePipe pipe, CardTerminal terminal, boolean transact) {
this.pipe = pipe;
this.jsonterminal = new JSONCardTerminalClient(terminal, pipe);
this.jsonterminal = new JSONCardTerminalClient(terminal, pipe, transact);
}

@Override
Expand Down
7 changes: 5 additions & 2 deletions src/apdu4j/remote/JSONCardTerminalClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ class JSONCardTerminalClient {
private final JSONMessagePipe pipe;
protected Card card = null; // There can be several connects-disconnects
private String protocol = null; // Local protocol to use, overriding the other side
private boolean transact = true;

public JSONCardTerminalClient(CardTerminal terminal, JSONMessagePipe pipe) {
public JSONCardTerminalClient(CardTerminal terminal, JSONMessagePipe pipe, boolean transact) {
this.terminal = terminal;
this.pipe = pipe;
this.transact = transact;
}

public void forceProtocol(String protocol) {
Expand All @@ -59,7 +61,8 @@ public boolean processMessage(Map<String, Object> msg) throws IOException, CardE
protocol = (String) msg.get("protocol");
}
card = terminal.connect(protocol);
card.beginExclusive();
if (transact)
card.beginExclusive();
Map<String, Object> m = JSONProtocol.ok(msg);
m.put("atr", HexUtils.encodeHexString(card.getATR().getBytes()));
m.put("reader", terminal.getName());
Expand Down

0 comments on commit fcad87f

Please sign in to comment.