Skip to content

Commit 4e7252c

Browse files
committed
Hacky workaround for #383
1 parent a532554 commit 4e7252c

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// https://stackoverflow.com/a/67895919/44289
22
@SuppressWarnings({"requires-automatic"})
33
module gptool {
4+
uses pro.javacard.gp.CardKeysProvider;
45
requires transitive pro.javacard.globalplatform;
56
requires pro.javacard.pace;
67
requires java.smartcardio;
@@ -14,4 +15,4 @@
1415
requires org.bouncycastle.provider;
1516
requires org.bouncycastle.pkix;
1617
requires org.slf4j;
17-
}
18+
}

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -811,20 +811,30 @@ public int run(BIBO bibo, String[] argv) {
811811
// By default we try to change an existing key
812812
boolean replace = true;
813813

814+
String kdf = PlaintextKeys.kdf_templates.getOrDefault(args.valueOf(OPT_LOCK_KDF), args.valueOf(OPT_LOCK_KDF));
815+
816+
817+
// XXX: remove the combined "keys or master" interfaces from PlaintextKeys
818+
final Optional<GPCardKeys> lockKey;
814819
// Get new key values
815-
Optional<GPCardKeys> lockKey = keyFromPlugin(args.valueOf(OPT_LOCK));
820+
if (args.has(OPT_LOCK)) {
821+
lockKey = keyFromPlugin(args.valueOf(OPT_LOCK));
822+
} else if (args.has(OPT_LOCK_ENC) && args.has(OPT_LOCK_MAC) && args.has(OPT_LOCK_DEK)) {
823+
lockKey = Optional.of(PlaintextKeys.fromKeys(args.valueOf(OPT_LOCK_ENC).value(), args.valueOf(OPT_LOCK_MAC).value(), args.valueOf(OPT_LOCK_DEK).value()));
824+
} else {
825+
throw new IllegalArgumentException("Use either --lock or --lock-enc/mac/dek");
826+
}
816827

817-
String kdf = PlaintextKeys.kdf_templates.getOrDefault(args.valueOf(OPT_LOCK_KDF), args.valueOf(OPT_LOCK_KDF));
818828
// From provider
819-
newKeys = lockKey.
820-
orElseGet(() -> PlaintextKeys.fromBytes(args.valueOf(OPT_LOCK_ENC).value(), args.valueOf(OPT_LOCK_MAC).value(), args.valueOf(OPT_LOCK_DEK).value(), HexBytes.v(args.valueOf(OPT_LOCK)).v(), kdf, null, args.valueOf(OPT_NEW_KEY_VERSION)).
821-
orElseThrow(() -> new IllegalArgumentException("Can not lock without keys :)")));
822-
829+
newKeys = lockKey.orElseThrow(() -> new IllegalArgumentException("Can not lock without keys :)"));
823830
if (newKeys instanceof PlaintextKeys) {
824831
// Adjust the mode and version with plaintext keys
825832
PlaintextKeys pk = (PlaintextKeys) newKeys;
826833
List<GPKeyInfo> current = gp.getKeyInfoTemplate();
827-
// By default use key version 1
834+
if (kdf != null) {
835+
pk.setDiversifier(kdf);
836+
}
837+
// By default, use key version 1
828838
final int keyver;
829839
if (args.has(OPT_NEW_KEY_VERSION)) {
830840
keyver = args.valueOf(OPT_NEW_KEY_VERSION);
@@ -833,7 +843,7 @@ public int run(BIBO bibo, String[] argv) {
833843
replace = false;
834844
}
835845
} else {
836-
if (current.size() == 0 || gp.getScpKeyVersion() == 255) {
846+
if (current.isEmpty() || gp.getScpKeyVersion() == 255) {
837847
keyver = 1;
838848
replace = false;
839849
} else {

0 commit comments

Comments
 (0)