Skip to content

Commit c769c27

Browse files
committed
GPContext: remove reader finding
This should go in the tool. Needs cleanup now.
1 parent 480c78b commit c769c27

File tree

4 files changed

+48
-46
lines changed

4 files changed

+48
-46
lines changed

globalplatform/src/main/java/org/openjavacard/gp/client/GPContext.java

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@
1919

2020
package org.openjavacard.gp.client;
2121

22-
import org.openjavacard.generic.GenericContext;
23-
import org.openjavacard.gp.keys.GPKeySet;
24-
import org.openjavacard.iso.AID;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
2524

26-
import javax.smartcardio.CardException;
27-
import javax.smartcardio.CardTerminal;
25+
public class GPContext {
2826

29-
public class GPContext extends GenericContext {
27+
private static final Logger LOG = LoggerFactory.getLogger(GPContext.class);
3028

3129
private boolean mKeyLoggingEnabled = false;
3230

@@ -46,37 +44,4 @@ public void enableKeyLogging() {
4644
mKeyLoggingEnabled = true;
4745
}
4846

49-
public GPCard findSingleGPCard(String prefix) {
50-
return findSingleGPCard(prefix, null, GPKeySet.GLOBALPLATFORM);
51-
}
52-
53-
public GPCard findSingleGPCard(String prefix, AID sd, GPKeySet keys) {
54-
LOG.debug("findSingleCard()");
55-
GPCard card;
56-
CardTerminal terminal = findSingleTerminal(prefix);
57-
try {
58-
// check card presence
59-
if (!terminal.isCardPresent()) {
60-
throw new Error("No card present in terminal");
61-
}
62-
// create GP client
63-
card = new GPCard(this, terminal);
64-
// tell the client if we know the SD AID
65-
if(sd != null) {
66-
card.setISD(sd);
67-
}
68-
if(keys != null) {
69-
card.setKeys(keys);
70-
}
71-
// detect GP applet
72-
//boolean detected = card.detect();
73-
//if (!detected) {
74-
// throw new Error("Could not find a GlobalPlatform applet on the card");
75-
//}
76-
} catch (CardException e) {
77-
throw new Error("Error detecting card", e);
78-
}
79-
return card;
80-
}
81-
8247
}

testbench/src/main/java/org/openjavacard/testbench/main/Bench.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.openjavacard.testbench.main;
2121

22+
import org.openjavacard.generic.GenericContext;
2223
import org.openjavacard.gp.client.GPContext;
2324
import org.slf4j.Logger;
2425
import org.slf4j.LoggerFactory;
@@ -35,6 +36,7 @@ public class Bench {
3536
private BenchConfiguration mConfig;
3637

3738
private GPContext mContext;
39+
private GenericContext mGeneric;
3840

3941
private List<BenchReader> mReaders;
4042

@@ -57,12 +59,12 @@ public void configure() {
5759
List<CardTerminal> terminals;
5860
if(mConfig.allReaders) {
5961
// all means all
60-
terminals = mContext.findTerminals();
62+
terminals = mGeneric.findTerminals();
6163
} else {
6264
// else filter for given names/prefixes
6365
terminals = new ArrayList<>();
6466
for(String readerName: mConfig.reader) {
65-
terminals.add(mContext.findSingleTerminal(readerName));
67+
terminals.add(mGeneric.findSingleTerminal(readerName));
6668
}
6769
}
6870
// complain if no readers at all

tool/src/main/java/org/openjavacard/tool/command/gp/GPCommand.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.beust.jcommander.Parameter;
2323
import com.beust.jcommander.validators.PositiveInteger;
24+
import org.openjavacard.generic.GenericContext;
2425
import org.openjavacard.gp.client.GPCard;
2526
import org.openjavacard.gp.client.GPContext;
2627
import org.openjavacard.gp.keys.GPKey;
@@ -36,6 +37,7 @@
3637
import org.openjavacard.util.HexUtil;
3738

3839
import javax.smartcardio.CardException;
40+
import javax.smartcardio.CardTerminal;
3941
import java.io.PrintStream;
4042

4143
public abstract class GPCommand implements Runnable {
@@ -121,9 +123,11 @@ public abstract class GPCommand implements Runnable {
121123
private boolean logKeys = false;
122124

123125
private GPContext mContext;
126+
private GenericContext mGeneric;
124127

125128
public GPCommand(GPContext context) {
126129
mContext = context;
130+
mGeneric = new GenericContext();
127131
}
128132

129133
public GPContext getContext() {
@@ -159,7 +163,35 @@ private GPCard prepareOperation() {
159163
keys = buildKeysFromParameters(scpKeyId, scpKeyVersion, scpKeyCipher, scpKeyTypes, scpKeySecrets);
160164
}
161165

162-
return mContext.findSingleGPCard(reader, isd, keys);
166+
return findSingleGPCard(reader, isd, keys);
167+
}
168+
169+
public GPCard findSingleGPCard(String prefix, AID sd, GPKeySet keys) {
170+
GPCard card;
171+
CardTerminal terminal = mGeneric.findSingleTerminal(prefix);
172+
try {
173+
// check card presence
174+
if (!terminal.isCardPresent()) {
175+
throw new Error("No card present in terminal");
176+
}
177+
// create GP client
178+
card = new GPCard(mContext, terminal);
179+
// tell the client if we know the SD AID
180+
if(sd != null) {
181+
card.setISD(sd);
182+
}
183+
if(keys != null) {
184+
card.setKeys(keys);
185+
}
186+
// detect GP applet
187+
//boolean detected = card.detect();
188+
//if (!detected) {
189+
// throw new Error("Could not find a GlobalPlatform applet on the card");
190+
//}
191+
} catch (CardException e) {
192+
throw new Error("Error detecting card", e);
193+
}
194+
return card;
163195
}
164196

165197
/**

tool/src/main/java/org/openjavacard/tool/main/Tool.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.beust.jcommander.JCommander;
2323
import com.beust.jcommander.Parameter;
24+
import org.openjavacard.generic.GenericContext;
2425
import org.openjavacard.gp.client.GPContext;
2526
import org.openjavacard.tool.command.aid.AIDInfo;
2627
import org.openjavacard.tool.command.aid.AIDNow;
@@ -63,9 +64,11 @@ public class Tool {
6364
)
6465
boolean help = false;
6566

67+
private final GenericContext mGeneric;
6668
private final GPContext mContext;
6769

6870
Tool() {
71+
mGeneric = new GenericContext();
6972
mContext = new GPContext();
7073
}
7174

@@ -117,8 +120,8 @@ JCommander makeCommander() {
117120
JCommander jc = new JCommander();
118121
jc.addConverterFactory(new ConverterFactory());
119122

120-
jc.addCommand(new GenericAPDU(mContext));
121-
jc.addCommand(new GenericReaders(mContext));
123+
jc.addCommand(new GenericAPDU(mGeneric));
124+
jc.addCommand(new GenericReaders(mGeneric));
122125

123126
jc.addCommand(new AIDInfo());
124127
jc.addCommand(new AIDNow());
@@ -144,8 +147,8 @@ JCommander makeCommander() {
144147
jc.addCommand(new PkgList(mContext));
145148
jc.addCommand(new PkgSearch(mContext));
146149

147-
jc.addCommand(new ScanName(mContext));
148-
jc.addCommand(new ScanFID(mContext));
150+
jc.addCommand(new ScanName(mGeneric));
151+
jc.addCommand(new ScanFID(mGeneric));
149152

150153
return jc;
151154
}

0 commit comments

Comments
 (0)