Skip to content

Commit 3b5e701

Browse files
committed
Fix #217: don't query card if only -V used
1 parent f369d64 commit 3b5e701

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

tool/src/main/java/pro/javacard/gp/GPCommandLineInterface.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ abstract class GPCommandLineInterface {
6262
protected static OptionSpec<String> OPT_DELETE = parser.accepts("delete", "Delete applet/package").withOptionalArg().describedAs("AID");
6363

6464
protected static OptionSpec<Void> OPT_DEFAULT = parser.accepts("default", "Indicate Default Selected privilege");
65-
protected static OptionSpec<Void> OPT_TERMINATE = parser.accepts("terminate", "Indicate Card Lock+Terminate privilege");
6665
protected static OptionSpec<String> OPT_DOMAIN = parser.accepts("domain", "Create supplementary security domain").withRequiredArg().describedAs("AID");
67-
protected static OptionSpec<Void> OPT_LIST_PRIVS = parser.accepts("list-privs", "List known privileges");
6866

6967
// Card an applet lifecycle management
7068
protected static OptionSpec<String> OPT_LOCK_APPLET = parser.accepts("lock-applet", "Lock applet").withRequiredArg().describedAs("AID");

tool/src/main/java/pro/javacard/gp/GPTool.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ public static void main(String[] argv) {
8383
OptionSet args = parseArguments(argv);
8484
setupLogging(args);
8585

86-
if (isVerbose) {
86+
if (args.has(OPT_VERBOSE) || args.has(OPT_VERSION)) {
8787
System.out.println("# " + String.join(" ", System.getenv().entrySet().stream().filter(e -> e.getKey().startsWith("GP_")).map(e -> String.format("%s=%s", e.getKey(), e.getValue())).collect(Collectors.toList())));
88-
System.out.println("# " + String.join(" ", argv));
88+
System.out.println("# gp " + String.join(" ", argv));
8989
}
9090
TerminalFactory tf = TerminalManager.getTerminalFactory();
9191
String reader = args.valueOf(OPT_READER);
@@ -105,7 +105,7 @@ public static void main(String[] argv) {
105105
System.err.println("Error: " + e.getMessage());
106106
e.printStackTrace();
107107
} finally {
108-
if (c!= null) {
108+
if (c != null) {
109109
try {
110110
c.disconnect(true);
111111
} catch (CardException e) {
@@ -116,6 +116,12 @@ public static void main(String[] argv) {
116116
System.exit(ret);
117117
}
118118

119+
static boolean onlyHasArg(OptionSet args, OptionSpec<?> s) {
120+
long needle = args.specs().stream().filter(e -> args.has(e)).count();
121+
long hay = args.specs().stream().filter(e -> args.has(e) && e != s).count();
122+
return needle == 1 && hay == 0;
123+
}
124+
119125
// For running in apdu4j mode
120126
public int run(BIBO bibo, String[] argv) {
121127
try {
@@ -136,6 +142,8 @@ public int run(BIBO bibo, String[] argv) {
136142
if (Cipher.getMaxAllowedKeyLength("AES") == 128) {
137143
System.err.println("Unlimited crypto policy is NOT installed!");
138144
}
145+
if (onlyHasArg(args, OPT_VERSION))
146+
return 0;
139147
}
140148

141149
// Load a CAP file, if specified
@@ -155,11 +163,6 @@ public int run(BIBO bibo, String[] argv) {
155163
}
156164
}
157165

158-
if (args.has(OPT_LIST_PRIVS)) {
159-
System.out.println("# Known privileges:");
160-
System.out.println(Arrays.asList(Privilege.values()).stream().map(i -> i.toString()).collect(Collectors.joining("\n")));
161-
}
162-
163166
// Now actually talk to possible terminals
164167
APDUBIBO channel = new APDUBIBO(bibo);
165168
// Send all raw APDU-s to the default-selected application of the card
@@ -764,7 +767,7 @@ private static Privileges getInstPrivs(OptionSet args) {
764767
Privileges privs = new Privileges();
765768
if (args.has(OPT_PRIVS)) {
766769
for (String s : args.valueOf(OPT_PRIVS).split(","))
767-
privs.add(Privilege.lookup(s.trim()).orElseThrow(() -> new IllegalArgumentException("Unknown privilege: " + s.trim())));
770+
privs.add(Privilege.lookup(s.trim()).orElseThrow(() -> new IllegalArgumentException("Unknown privilege: " + s.trim() + "\nValid values are: " + Arrays.asList(Privilege.values()).stream().map(i -> i.toString()).collect(Collectors.joining(", ")))));
768771
}
769772
return privs;
770773
}
@@ -806,6 +809,7 @@ private static boolean needsAuthentication(OptionSet args) {
806809
return Arrays.stream(yes).anyMatch(args::has);
807810
}
808811

812+
// FIXME: replace with IllegalArgumentException
809813
public static void fail(String msg) {
810814
System.err.println(msg);
811815
System.exit(1);

0 commit comments

Comments
 (0)