Skip to content

Commit eab24ad

Browse files
committed
Fix #384, #386: PlaintextKeys are exported from gptool and live in a separate package
1 parent a4628d3 commit eab24ad

File tree

5 files changed

+24
-10
lines changed

5 files changed

+24
-10
lines changed

tool/src/main/java/module-info.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// https://stackoverflow.com/a/67895919/44289
22
@SuppressWarnings({"requires-automatic"})
33
module gptool {
4-
requires pro.javacard.globalplatform;
4+
requires transitive pro.javacard.globalplatform;
55
requires pro.javacard.pace;
66
requires java.smartcardio;
77
requires jopt.simple;
@@ -14,4 +14,6 @@
1414
requires org.bouncycastle.provider;
1515
requires org.bouncycastle.pkix;
1616
requires org.slf4j;
17+
18+
exports pro.javacard.gptool.keys;
1719
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import pro.javacard.gp.*;
3737
import pro.javacard.gp.GPRegistryEntry.Privilege;
3838
import pro.javacard.gp.GPSession.APDUMode;
39+
import pro.javacard.gptool.keys.PlaintextKeys;
3940
import pro.javacard.pace.AESSecureChannel;
4041
import pro.javacard.pace.PACE;
4142
import pro.javacard.pace.PACEException;
@@ -340,7 +341,7 @@ public int run(BIBO bibo, String[] argv) {
340341
System.err.println("Error: no keys given");
341342
return 1;
342343
} else
343-
System.err.println("# Warning: no keys given, defaulting to " + HexUtils.bin2hex(PlaintextKeys.defaultKeyBytes));
344+
System.err.println("# Warning: no keys given, defaulting to " + HexUtils.bin2hex(PlaintextKeys.DEFAULT_KEY()));
344345
}
345346
keys = cliKeys.or(() -> envKeys).orElse(PlaintextKeys.defaultKey());
346347
}
@@ -847,8 +848,8 @@ else if (keyver >= 0x30 && keyver <= 0x3F)
847848
PlaintextKeys pk = (PlaintextKeys) newKeys;
848849
if (pk.getMasterKey().isPresent())
849850
System.out.println(gp.getAID() + " locked with: " + HexUtils.bin2hex(pk.getMasterKey().get()));
850-
if (pk.kdf_template != null)
851-
System.out.println("Keys were diversified with " + pk.kdf_template + " and " + HexUtils.bin2hex(kdd));
851+
if (pk.getTemplate() != null)
852+
System.out.println("Keys were diversified with " + pk.getTemplate() + " and " + HexUtils.bin2hex(kdd));
852853
System.out.println("Write this down, DO NOT FORGET/LOSE IT!");
853854
} else {
854855
System.out.println("Card locked with new keys.");

tool/src/main/java/pro/javacard/gptool/PlaintextKeysProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.auto.service.AutoService;
2525
import pro.javacard.gp.CardKeysProvider;
2626
import pro.javacard.gp.GPCardKeys;
27+
import pro.javacard.gptool.keys.PlaintextKeys;
2728

2829
import java.util.Map;
2930
import java.util.Optional;
@@ -59,7 +60,7 @@ public Optional<GPCardKeys> getCardKeys(String spec) {
5960

6061
static byte[] hexOrDefault(String v) {
6162
if ("default".startsWith(v.toLowerCase()))
62-
return PlaintextKeys.defaultKeyBytes;
63+
return PlaintextKeys.DEFAULT_KEY();
6364
return HexUtils.stringToBin(v);
6465
}
6566
}

tool/src/main/java/pro/javacard/gptool/PlaintextKeys.java renamed to tool/src/main/java/pro/javacard/gptool/keys/PlaintextKeys.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* License along with this library; if not, write to the Free Software
1818
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1919
*/
20-
package pro.javacard.gptool;
20+
package pro.javacard.gptool.keys;
2121

2222
import apdu4j.core.HexUtils;
2323
import org.slf4j.Logger;
@@ -33,11 +33,15 @@
3333

3434
// Handles plaintext card keys.
3535
// Supports diversification of card keys with a few known algorithms.
36-
class PlaintextKeys extends GPCardKeys {
36+
public class PlaintextKeys extends GPCardKeys {
3737
private static final Logger logger = LoggerFactory.getLogger(PlaintextKeys.class);
3838

3939
// After diversify() we know for which protocol we have keys for, unless known before
40-
static final byte[] defaultKeyBytes = HexUtils.hex2bin("404142434445464748494A4B4C4D4E4F");
40+
private static final byte[] defaultKeyBytes = HexUtils.hex2bin("404142434445464748494A4B4C4D4E4F");
41+
42+
public static byte[] DEFAULT_KEY() {
43+
return defaultKeyBytes.clone();
44+
}
4145

4246
// Derivation constants for session keys
4347
public static final Map<KeyPurpose, byte[]> SCP02_CONSTANTS;
@@ -70,7 +74,11 @@ class PlaintextKeys extends GPCardKeys {
7074
}
7175

7276
// If diverisification is to be used
73-
String kdf_template;
77+
private String kdf_template;
78+
79+
public String getTemplate() {
80+
return kdf_template;
81+
}
7482

7583
// Keyset version
7684
private int version = 0x00;
@@ -202,8 +210,9 @@ public static Optional<PlaintextKeys> fromEnvironment(Map<String, String> env, S
202210
String kdd = env.get(prefix + "_KDD");
203211
String ver = env.get(prefix + "_VER");
204212
Optional<PlaintextKeys> r = fromStrings(enc, mac, dek, mk, div, kdd, ver);
205-
if (r.isPresent())
213+
if (r.isPresent()) {
206214
logger.debug("Got keys from environment, prefix=" + prefix);
215+
}
207216
return r;
208217
}
209218

tool/src/test/java/pro/javacard/gptool/TestPlaintextKeysProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pro.javacard.gp.GPCardKeys;
88
import pro.javacard.gp.GPCrypto;
99
import pro.javacard.gp.GPSecureChannelVersion;
10+
import pro.javacard.gptool.keys.PlaintextKeys;
1011

1112
import java.util.Optional;
1213

0 commit comments

Comments
 (0)