Skip to content

Commit

Permalink
Get rid of inner classes and some related shenanigans
Browse files Browse the repository at this point in the history
  • Loading branch information
martinpaljak committed Mar 15, 2024
1 parent 74d8248 commit 9a9b513
Show file tree
Hide file tree
Showing 14 changed files with 970 additions and 914 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ Notes:
- run off-card verifier
- `java -jar ant-javacard.jar <sdk> <capfile> <expfiles>`

### Environment variables
- `JAVA_HOME` - path to the JDK to be used.
- `JC_HOME` - path to the JavaCard SDK to be used if not specified in the build file.
- `ANT_JAVACARD_TMP` - path to the temporary folder to be used for building CAP files. This is not cleaned after use.
- `DEBUG` - if set, shows debug output.

## Maven dependency
Releases are published to [`https://mvn.javacard.pro/maven/`](https://mvn.javacard.pro/maven/). To use it, add this to your `pom.xml`:
Expand Down
8 changes: 0 additions & 8 deletions capfile/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>4.8.3.1</version>
<configuration>
<excludeFilterFile>spotbugs.xml</excludeFilterFile>
</configuration>
</plugin>
</plugins>
</build>
</project>
30 changes: 16 additions & 14 deletions capfile/src/main/java/pro/javacard/capfile/CAPFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@
* CAP files are tiny, so we keep it in memory.
*/
public final class CAPFile {
public static final String DAP_RSA_V1_SHA1_FILE = "dap.rsa.sha1";
public static final String DAP_RSA_V1_SHA256_FILE = "dap.rsa.sha256";
public static final String DAP_P256_SHA1_FILE = "dap.p256.sha1";
public static final String DAP_P256_SHA256_FILE = "dap.p256.sha256";

private static final String[] componentNames = {"Header", "Directory", "Import", "Applet", "Class", "Method", "StaticField", "Export",
"ConstantPool", "RefLocation", "Descriptor", "Debug"};
Expand All @@ -66,7 +62,7 @@ public final class CAPFile {
private final List<CAPPackage> imports = new ArrayList<>();
private CAPPackage pkg;
private byte flags;
private String cap_version; // 2.1 and 2.2 supported, 2.3 new format not
private String cap_version;
// Metadata
private Manifest manifest = null; // From 2.2.2
private Document appletxml = null; // From 3.0.1
Expand Down Expand Up @@ -102,6 +98,10 @@ public byte[] getMetaInfEntry(String name) {
return entries.get("META-INF/" + name);
}

public Optional<byte[]> getZipComponent(String name) {
return Optional.ofNullable(entries.get(name));
}

public void store(OutputStream to) throws IOException {
try (ZipOutputStream out = new ZipOutputStream(to)) {
for (Map.Entry<String, byte[]> e : entries.entrySet()) {
Expand Down Expand Up @@ -246,16 +246,20 @@ public byte[] getCode(boolean includeDebug) {
}

byte[] _getCode(boolean includeDebug) {
byte[] result = new byte[0];
ByteArrayOutputStream result = new ByteArrayOutputStream();
for (String name : componentNames) {
byte[] c = getComponent(name);
if (c == null)
continue;
if (!includeDebug && (name.equals("Debug") || name.equals("Descriptor")))
continue;
result = concat(result, c);
try {
result.write(c);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
return result;
return result.toByteArray();
}

public byte[] getLoadFileDataHash(String hash) {
Expand Down Expand Up @@ -316,6 +320,10 @@ public void dump(PrintStream out) {
}

public List<String> getFlags() {
return flags2strings(flags);
}

public static List<String> flags2strings(byte flags) {
ArrayList<String> result = new ArrayList<>();
// Table 6-3: CAP File Package Flags
if ((flags & 0x01) == 0x01) {
Expand Down Expand Up @@ -425,12 +433,6 @@ private static String jcdir2pkg(String jcdir) {
return jcdir.substring(0, jcdir.lastIndexOf("/javacard/")).replace('/', '.');
}

private static byte[] concat(byte[] a, byte[] b) {
byte[] r = Arrays.copyOf(a, a.length + b.length);
System.arraycopy(b, 0, r, a.length, b.length);
return r;
}

public static void uncheckedDelete(Path p) throws UncheckedIOException {
try {
Files.delete(p);
Expand Down
2 changes: 1 addition & 1 deletion capfile/src/main/java/pro/javacard/capfile/CAPPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

public final class CAPPackage {
final AID aid;
final int minor;
final int major;
final int minor;
final String name;

public CAPPackage(AID aid, int major, int minor) {
Expand Down
8 changes: 5 additions & 3 deletions capfile/src/main/java/pro/javacard/sdk/OffCardVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@
import java.util.jar.JarFile;
import java.util.stream.Collectors;

import static pro.javacard.sdk.SDKVersion.*;

public final class OffCardVerifier {
private final JavaCardSDK sdk;

public static OffCardVerifier withSDK(JavaCardSDK sdk) {
// Only main method in 2.1 SDK
if (sdk.getVersion().isOneOf(SDKVersion.V211, SDKVersion.V212))
if (sdk.getVersion().isOneOf(V211, V212))
throw new RuntimeException("Verification is supported with JavaCard SDK 2.2.1 or later");
return new OffCardVerifier(sdk);
}
Expand All @@ -57,7 +59,7 @@ public void verifyAgainst(File f, JavaCardSDK target, Vector<File> exps) throws

public void verifyAgainst(Path f, JavaCardSDK target, List<Path> exps) throws VerifierError, IOException {
// Warn about recommended usage
if (target.getVersion().isOneOf(SDKVersion.V304, SDKVersion.V305, SDKVersion.V310) && sdk.getVersion() != SDKVersion.V320) {
if (target.getVersion().isOneOf(V304, V305, V310) && sdk.getVersion() != V320) {
System.err.println("NB! Please use JavaCard SDK 3.2.0 for verifying!");
} else {
if (!sdk.getRelease().equals("3.0.5u3")) {
Expand Down Expand Up @@ -97,7 +99,7 @@ public void verify(Path f, List<Path> exps) throws VerifierError, IOException {

try (FileInputStream input = new FileInputStream(f.toFile())) {
// 3.0.5u1 still uses old signature
if (sdk.getRelease().equals("3.0.5u3") || sdk.getRelease().equals("3.0.5u2") || sdk.getVersion().isOneOf(SDKVersion.V310, SDKVersion.V320)) {
if (sdk.getRelease().equals("3.0.5u3") || sdk.getRelease().equals("3.0.5u2") || sdk.getVersion().isOneOf(V310, V320)) {
Method m = verifier.getMethod("verifyCap", File.class, String.class, Vector.class);
m.invoke(null, f.toFile(), packagename, expfiles);
} else {
Expand Down
18 changes: 18 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@
<version>3.5.3</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>4.8.3.1</version>
<configuration>
<excludeFilterFile>spotbugs.xml</excludeFilterFile>
</configuration>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<!-- Metadata -->
Expand Down
3 changes: 3 additions & 0 deletions spotbugs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<FindBugsFilter xmlns="https://github.com/spotbugs/filter/3.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubusercontent.com/spotbugs/spotbugs/3.1.0/spotbugs/etc/findbugsfilter.xsd">
</FindBugsFilter>
7 changes: 4 additions & 3 deletions task/src/main/java/pro/javacard/ant/DummyMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ public final class DummyMain {
public static void main(String[] argv) {
try {
Vector<String> args = new Vector<>(Arrays.asList(argv));
System.out.println("args: " + args);

if (args.isEmpty()) {
System.out.println("This is an ANT task, not a program!");
System.out.println("This is an ANT task.");
System.out.println("Read usage instructions from https://github.com/martinpaljak/ant-javacard#syntax");
System.out.println();
System.out.println("But you can use it to dump/verify CAP files, like this:");
System.out.println("$ java -jar ant-javacard.jar <capfile>");
System.exit(1);
} else if (args.size() == 1) {
final String capfile = args.remove(0);
Expand Down Expand Up @@ -87,7 +88,7 @@ public static void main(String[] argv) {
System.exit(1);
}
}
} catch (Exception e) {
} catch (Throwable e) {
System.err.printf("Error: %s: %s%n", e.getClass().getSimpleName(), e.getMessage());
if (System.getenv("DEBUG") != null) {
e.printStackTrace();
Expand Down
11 changes: 11 additions & 0 deletions task/src/main/java/pro/javacard/ant/HelpingBuildException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pro.javacard.ant;

import org.apache.tools.ant.BuildException;

public class HelpingBuildException extends BuildException {
private final static long serialVersionUID = -2365126253968479314L;

public HelpingBuildException(String msg) {
super(msg + "\n\nPLEASE READ https://github.com/martinpaljak/ant-javacard#readme");
}
}
27 changes: 27 additions & 0 deletions task/src/main/java/pro/javacard/ant/JCApplet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package pro.javacard.ant;

import org.apache.tools.ant.BuildException;

// Just for Ant
public class JCApplet {
String klass = null;
byte[] aid = null;

public JCApplet() {
}

public void setClass(String msg) {
klass = msg;
}

public void setAID(String msg) {
try {
aid = Misc.stringToBin(msg);
if (aid.length < 5 || aid.length > 16) {
throw new BuildException("Applet AID must be between 5 and 16 bytes: " + aid.length);
}
} catch (IllegalArgumentException e) {
throw new BuildException("Not a valid applet AID: " + e.getMessage());
}
}
}
Loading

0 comments on commit 9a9b513

Please sign in to comment.