Skip to content

Commit

Permalink
Move to jopt-simple.
Browse files Browse the repository at this point in the history
This should make the command line utility way more easy to read.

Also: build a .exe for Windows
  • Loading branch information
martinpaljak committed Feb 13, 2014
1 parent 7a6357a commit 5e85ad6
Show file tree
Hide file tree
Showing 19 changed files with 708 additions and 775 deletions.
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="lib/jnasmartcardio.jar"/>
<classpathentry kind="lib" path="lib/bcprov-jdk15on-150.jar"/>
<classpathentry kind="lib" path="lib/jopt-simple-4.6.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
*~
/gp.jar
/javadoc/
/gp.exe
/cov-int
/ext/launch4j
27 changes: 18 additions & 9 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
<project name="GlobalPlatform Java" default="dist" basedir=".">
<description>ANT build file for OpenKMS GlobalPlatform</description>
<!-- Build the software -->
<path id="build.classpath">
<pathelement location="lib/jnasmartcardio.jar"/>
<pathelement location="lib/bcprov-jdk15on-150.jar"/>
<pathelement location="lib/jopt-simple-4.6.jar"/>
</path>
<target name="compile" description="compile the source">
<mkdir dir="build"/>
<javac srcdir="src" destdir="build" includeantruntime="false" excludes="**/tests/**" target="1.7" debug="true" debuglevel="lines,vars,source">
<compilerarg value="-Xlint"/>
<classpath>
<pathelement location="lib/jnasmartcardio.jar"/>
<pathelement location="lib/bcprov-jdk15on-150.jar"/>
</classpath>
<classpath refid="build.classpath"/>
</javac>
</target>
<!-- Package it into a nice little JAR -->
Expand All @@ -22,10 +24,20 @@
</java>
<jar manifest="Manifest.mf" destfile="gp.jar" level="9">
<zipfileset src="lib/jnasmartcardio.jar"/>
<zipfileset src="optimized-globalplatform.jar" excludes="META-INF/BCKEY.*,org/bouncycastle/x509/"/>
<zipfileset src="optimized-globalplatform.jar" excludes="org/bouncycastle/x509/"/>
</jar>
<delete file="optimized-globalplatform.jar"/>
</target>
<!-- Wrap the JAR into .EXE -->
<target name="windist" depends="dist" description="package as .exe">
<property name="launch4j.dir" location="ext/launch4j"/>
<taskdef name="launch4j" classname="net.sf.launch4j.ant.Launch4jTask" classpath="${launch4j.dir}/launch4j.jar:${launch4j.dir}/lib/xstream.jar"/>
<launch4j>
<config headerType="console" outfile="gp.exe" jar="gp.jar" errTitle="GlobalPlatform">
<jre minVersion="1.7.0"/>
</config>
</launch4j>
</target>
<!-- Clean the source tree -->
<target name="clean" description="clean up">
<delete dir="build"/>
Expand All @@ -39,10 +51,7 @@
<include name="openkms/gpj/"/>
<exclude name="openkms/gpj/tests/**"/>
</packageset>
<classpath>
<pathelement location="lib/jnasmartcardio.jar"/>
<pathelement location="lib/bcprov-jdk15on-150.jar"/>
</classpath>
<classpath refid="build.classpath"/>
</javadoc>
</target>
<!-- Do a full release to github -->
Expand Down
9 changes: 7 additions & 2 deletions globalplatform.pro
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
-injars build
-injars lib/bcprov-jdk15on-150.jar
-injars lib/bcprov-jdk15on-150.jar(!META-INF/*)
-injars lib/jopt-simple-4.6.jar(!META-INF/*)
# JNA is library because we package everything back in
-libraryjars lib/jnasmartcardio.jar
-libraryjars <java.home>/lib/rt.jar:<java.home>/lib/jce.jar
-outjars optimized-globalplatform.jar
Expand All @@ -11,6 +13,9 @@
-keep public class openkms.gpj.GPJTool {
public static void main(java.lang.String[]);
}
-keepclassmembers,allowoptimization enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-printseeds
-dontnote

Binary file added lib/jopt-simple-4.6.jar
Binary file not shown.
20 changes: 10 additions & 10 deletions src/openkms/gpj/AID.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,9 @@
package openkms.gpj;

import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;

public class AID {

public static Map<String, AID> SD_AIDS = new TreeMap<String, AID>();

static {
SD_AIDS.put("OP201", new AID(new byte[] { (byte) 0xa0, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00 }));
SD_AIDS.put("GP211", new AID(new byte[] { (byte) 0xa0, 0x00, 0x00, 0x01, 0x51, 0x00, 0x00 }));
}

private byte[] aidBytes = null;

/**
Expand All @@ -50,6 +41,10 @@ public AID(byte[] bytes) {
this(bytes, 0, bytes.length);
}


public AID(String str) {
this(GPUtils.stringToByteArray(str));
}
/**
* Construct an application identifier from a part of a byte array.
*
Expand All @@ -63,7 +58,7 @@ public AID(byte[] bytes) {
*
*/
public AID(byte[] bytes, int offset, int length) {
this(bytes, offset, length, false);
this(bytes, offset, length, true);
}

/**
Expand Down Expand Up @@ -115,4 +110,9 @@ public boolean equals(Object o) {
}
return false;
}

public static AID valueOf(String s) {
return new AID(s);
}

}
26 changes: 26 additions & 0 deletions src/openkms/gpj/AIDRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public Iterator<AIDRegistryEntry> iterator() {
return entries.iterator();
}


/**
* Returns a list of all packages in this registry.
*
Expand All @@ -70,6 +71,31 @@ public List<AIDRegistryEntry> allPackages() {
return res;
}

public List<AID> allPackageAIDs() {
List<AID> res = new ArrayList<AID>();
for (AIDRegistryEntry e : entries) {
if (e.isPackage()) {
res.add(e.getAID());
}
}
return res;
}
public List<AID> allAppletAIDs() {
List<AID> res = new ArrayList<AID>();
for (AIDRegistryEntry e : entries) {
if (e.isApplet()) {
res.add(e.getAID());
}
}
return res;
}
public List<AID> allAIDs() {
List<AID> res = new ArrayList<AID>();
for (AIDRegistryEntry e : entries) {
res.add(e.getAID());
}
return res;
}
/**
* Returns a list of all applets in this registry.
*
Expand Down
21 changes: 11 additions & 10 deletions src/openkms/gpj/AIDRegistryEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,13 @@ public List<AID> getExecutableAIDs() {
* @return description
*/
public String toString() {
String result = "";
result += "AID: " + aid + ", " + lifeCycleState + ", " + privileges + ", Kind: " + kind.toShortString();
StringBuffer result = new StringBuffer();
result.append("AID: " + aid + ", " + lifeCycleState + ", " + privileges + ", Kind: " + kind.toShortString());

for (AID a : executableAIDS) {
result = result + "\n " + a;
result.append("\n " + a);
}
return result;
return result.toString();
}

public String getLifeCycleString() {
Expand Down Expand Up @@ -265,19 +265,20 @@ public String getPrivilegesString() {
privs.add("CVM (PIN) management");
}
}
String result = "";
StringBuffer result = new StringBuffer();
// http://findbugs.sourceforge.net/bugDescriptions.html#SBSC_USE_STRINGBUFFER_CONCATENATION

for (int i = 0; i < privs.size(); i++) {
if (i != 0) {
result += ", ";
result.append(", ");
}
result += privs.get(i);
result.append(privs.get(i));
}

// TODO: Wait until actual cards discovered
if (r>0) {
result += " " + Integer.toHexString(r);
result.append(" " + Integer.toHexString(r));
}

return result.trim();
return result.toString().trim();
}
}
40 changes: 8 additions & 32 deletions src/openkms/gpj/CapFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
public class CapFile {

public static final String[] componentNames = { "Header", "Directory", "Import", "Applet", "Class", "Method", "StaticField", "Export",
"ConstantPool", "RefLocation", "Descriptor", "Debug" };
"ConstantPool", "RefLocation", "Descriptor", "Debug" };

private final HashMap<String, byte[]> capComponents = new HashMap<String, byte[]>();

Expand Down Expand Up @@ -80,9 +80,7 @@ public CapFile(InputStream in, String packageName) throws IOException {
throw new RuntimeException("Could not figure out the package name of the applet!");
}

GPUtils.debug("packagePath: " + packageName);
this.packageName = packageName.substring(0, packageName.lastIndexOf("/javacard/")).replace('/', '.');
GPUtils.debug("package: " + this.packageName);
for (String name : componentNames) {
String fullName = packageName + name + ".cap";
byte[] contents = entries.get(fullName);
Expand Down Expand Up @@ -125,7 +123,7 @@ public CapFile(InputStream in, String packageName) throws IOException {
// header[12] should be the length of AID
int len = header[i++];
packageAID = new AID(header, i, len);
GPUtils.debug("package AID: " + packageAID);
//GPUtils.debug("package AID: " + packageAID);

byte[] applet = capComponents.get("Applet");
if (applet != null) {
Expand All @@ -143,9 +141,9 @@ public CapFile(InputStream in, String packageName) throws IOException {
appletAIDs.add(new AID(applet, i, len));
i += len + 2;
}
GPUtils.debug("applet AIDs: " + appletAIDs);
//GPUtils.debug("applet AIDs: " + appletAIDs);
} else {
GPUtils.debug("No Applet component.");
//GPUtils.debug("No Applet component.");
}
}

Expand Down Expand Up @@ -180,6 +178,10 @@ public List<AID> getAppletAIDs() {
return result;
}

public String getPackageName() {
return packageName;
}

public int getCodeLength(boolean includeDebug) {
int result = 0;
for (String name : componentNames) {
Expand Down Expand Up @@ -299,30 +301,4 @@ private List<byte[]> splitArray(byte[] array, int blockSize) {
}
return result;
}

public String dump() {
String result = "";
for (String name : componentNames) {
result = result + name + ".cap:\n";
byte[] b = capComponents.get(name);
if (b != null) {
result = result + GPUtils.byteArrayToString(b) + "\n";
} else {
result = result + "(empty)\n";
}
}
List<List<byte[]>> tables = new ArrayList<List<byte[]>>();
tables.add(dapBlocks);
tables.add(loadTokens);
tables.add(installTokens);
String[] names = { "DAP Blocks", "Load Tokens", "Install Tokens" };
for (int i = 0; i < names.length; i++) {
result = result + names[i] + ":\n";
for (byte[] o : tables.get(i)) {
result = result + GPUtils.byteArrayToString(o) + "\n";
}
}

return result;
}
}
Loading

0 comments on commit 5e85ad6

Please sign in to comment.