From ab4afe8103ec08073fc0f07d215210b96996913f Mon Sep 17 00:00:00 2001 From: Martin Paljak Date: Fri, 25 Oct 2024 08:53:20 +0300 Subject: [PATCH] Log KCV-s from card. --- .../main/java/pro/javacard/gp/GPSession.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/library/src/main/java/pro/javacard/gp/GPSession.java b/library/src/main/java/pro/javacard/gp/GPSession.java index 9441db4e..4d2796f9 100644 --- a/library/src/main/java/pro/javacard/gp/GPSession.java +++ b/library/src/main/java/pro/javacard/gp/GPSession.java @@ -1060,6 +1060,14 @@ public void putKeys(GPCardKeys keys, boolean replace) throws GPException, IOExce CommandAPDU command = new CommandAPDU(CLA_GP, INS_PUT_KEY, P1, P2, bo.toByteArray()); ResponseAPDU response = transmit(command); GPException.check(response, "PUT KEY failed"); + // TODO: compare and complain + if (response.getData().length > 1) { + byte [] resp = response.getData(); + int kv = resp[0] & 0xFF; + byte[] kcvs = Arrays.copyOfRange(resp, 1, resp.length); + List kcvstrings = GPUtils.splitArray(kcvs, 3).stream().map(HexUtils::bin2hex).collect(Collectors.toList()); + logger.info("Card stored keys with KVN {} and with KCV-s: {}", GPUtils.intString(kv), String.join(", ", kcvstrings)); + } } @@ -1092,7 +1100,7 @@ byte[] encodeECKey(ECPublicKey pubkey) { bo.write(0xB0); // EC Public key bo.write(key.length); bo.write(key); - bo.write(0xF0); + bo.write(0xF0); // ECC key parameters reference bo.write(0x01); bo.write(0x00); // P-256 bo.write(0x00); // No KCV @@ -1128,10 +1136,12 @@ public void putKey(Key key, int version, boolean replace) throws IOException, GP CommandAPDU command = new CommandAPDU(CLA_GP, INS_PUT_KEY, replace ? version : 0x00, 0x01, bo.toByteArray(), 256); ResponseAPDU response = transmit(command); GPException.check(response, "PUT KEY failed"); - if (response.getData().length == 4) { - int kv = response.getData()[0] & 0xFF; - byte[] kcv = Arrays.copyOfRange(response.getData(), 1, 4); - logger.info("Card stored key {} with KCV {}", GPUtils.intString(kv), HexUtils.bin2hex(kcv)); + if (response.getData().length > 1) { + byte [] resp = response.getData(); + int kv = resp[0] & 0xFF; + byte[] kcvs = Arrays.copyOfRange(resp, 1, resp.length); + List kcvstrings = GPUtils.splitArray(kcvs, 3).stream().map(HexUtils::bin2hex).collect(Collectors.toList()); + logger.info("Card stored key(s) {} with KCV(s) {}", GPUtils.intString(kv), String.join(", ", kcvstrings)); } }