Skip to content

Commit

Permalink
Fix hex parsing of integer values from command line; add $GP_KEY_VERSION
Browse files Browse the repository at this point in the history
And remove key replacement checks from library, to be moved to GPTool
  • Loading branch information
martinpaljak committed Sep 14, 2018
1 parent e2bf7a3 commit b439b52
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.github.martinpaljak</groupId>
<artifactId>globalplatformpro</artifactId>
<packaging>jar</packaging>
<version>18.08.16.1</version>
<version>18.09.14</version>
<name>GlobalPlatformPro</name>
<url>https://github.com/martinpaljak/GlobalPlatformPro</url>
<description>Manage applets and keys on JavaCard-s like a pro</description>
Expand Down Expand Up @@ -92,7 +92,7 @@
<version>1.6.8</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<serverId>ossrh-martinpaljak</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/pro/javacard/gp/GPTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ public static void main(String[] argv) throws Exception {
GPKey mac = new GPKey(HexUtils.stringToBin(env.get("GP_KEY_MAC")));
GPKey dek = new GPKey(HexUtils.stringToBin(env.get("GP_KEY_DEK")));
keyz = PlaintextKeys.fromKeys(enc, mac, dek);
if (env.containsKey("GP_KEY_VERSION")) {
keyz.setVersion(GPUtils.intValue(env.get("GP_KEY_VERSION")));
}
} else {
if (needsAuthentication(args)) {
System.out.println("Warning: no keys given, using default test key " + HexUtils.bin2hex(GPData.defaultKeyBytes));
Expand Down Expand Up @@ -737,6 +740,7 @@ public static void main(String[] argv) throws Exception {
// If a specific new key version is specified, use that instead.
if (args.has(OPT_NEW_KEY_VERSION)) {
new_version = GPUtils.intValue((String) args.valueOf(OPT_NEW_KEY_VERSION));
replace = false;
System.out.println("New version: " + new_version);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/pro/javacard/gp/GPUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class GPUtils {

public static int intValue(String s) {
if (s.trim().toLowerCase().startsWith("0x")) {
return Integer.parseInt(s, 16);
return Integer.parseInt(s.substring(2), 16);
}
return Integer.parseInt(s);
}
Expand Down
39 changes: 20 additions & 19 deletions src/main/java/pro/javacard/gp/GlobalPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -978,24 +978,25 @@ public void putKeys(List<GPKey> keys, boolean replace) throws GPException, CardE
List<GPKey> tmpl = getKeyInfoTemplate();

if (tmpl.size() > 0) {
if ((tmpl.get(0).getVersion() < 1 || tmpl.get(0).getVersion() > 0x7F) && replace) {
giveStrictWarning("Trying to replace factory keys, when you need to add new ones? Is this a virgin card? (use --virgin)");
}

// Check if key types and lengths are the same when replacing
if (replace && (keys.get(0).getType() != tmpl.get(0).getType() || keys.get(0).getLength() != tmpl.get(0).getLength())) {
// FIXME: SCE60 template has 3DES keys but uses AES.
giveStrictWarning("Can not replace keys of different type or size: " + tmpl.get(0).getType() + "->" + keys.get(0).getType());
}

// Check for matching version numbers if replacing and vice versa
if (!replace && (keys.get(0).getVersion() == tmpl.get(0).getVersion())) {
throw new IllegalArgumentException("Not adding keys and version matches existing?");
}

if (replace && (keys.get(0).getVersion() != tmpl.get(0).getVersion())) {
throw new IllegalArgumentException("Replacing keys and versions don't match existing?");
}
// // TODO: move to GPTool
// if ((tmpl.get(0).getVersion() < 1 || tmpl.get(0).getVersion() > 0x7F) && replace) {
// giveStrictWarning("Trying to replace factory keys, when you need to add new ones? Is this a virgin card? (use --virgin)");
// }
//
// // Check if key types and lengths are the same when replacing
// if (replace && (keys.get(0).getType() != tmpl.get(0).getType() || keys.get(0).getLength() != tmpl.get(0).getLength())) {
// // FIXME: SCE60 template has 3DES keys but uses AES.
// giveStrictWarning("Can not replace keys of different type or size: " + tmpl.get(0).getType() + "->" + keys.get(0).getType());
// }
//
// // Check for matching version numbers if replacing and vice versa
// if (!replace && (keys.get(0).getVersion() == tmpl.get(0).getVersion())) {
// throw new IllegalArgumentException("Not adding keys and version matches existing?");
// }
//
// if (replace && (keys.get(0).getVersion() != tmpl.get(0).getVersion())) {
// throw new IllegalArgumentException("Replacing keys and versions don't match existing?");
// }
} else {
if (replace) {
logger.warn("No key template on card but trying to replace. Implying add");
Expand Down Expand Up @@ -1052,7 +1053,7 @@ public void putKey(RSAPublicKey pubkey, int version) throws CardException, GPExc
ResponseAPDU response = transmit(command);
GPException.check(response, "PUT KEY failed");
}

public GPRegistry getRegistry() throws GPException, CardException {
if (dirty) {
registry = getStatus();
Expand Down

0 comments on commit b439b52

Please sign in to comment.