From 73a03b2d6561fdc44f063220184ca308ccccfe37 Mon Sep 17 00:00:00 2001 From: Federico Date: Fri, 5 May 2023 23:19:33 -0300 Subject: [PATCH 1/2] Add KSN parsing capabilities. --- .../org/jpos/security/KeySerialNumber.java | 29 +++++++++++++ .../jpos/security/KeySerialNumberTest.java | 42 +++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/jpos/src/main/java/org/jpos/security/KeySerialNumber.java b/jpos/src/main/java/org/jpos/security/KeySerialNumber.java index 49447e9742..8497cc76b8 100644 --- a/jpos/src/main/java/org/jpos/security/KeySerialNumber.java +++ b/jpos/src/main/java/org/jpos/security/KeySerialNumber.java @@ -18,6 +18,7 @@ package org.jpos.security; +import org.jpos.iso.ISOUtil; import org.jpos.util.Loggeable; import java.io.PrintStream; @@ -68,6 +69,34 @@ public KeySerialNumber (String baseKeyID, String deviceID, String transactionCou setTransactionCounter(transactionCounter); } + /** + * Constructs a key serial number object from its hexadecimal representation. + * @param hexKSN hexadecimal representation of the KSN. + * @param idLength length of the base key ID. + * @param deviceLength length of the device ID. + * @param counterLength length of the transaction counter. + */ + public KeySerialNumber(String hexKSN, int idLength, int deviceLength, int counterLength) { + if (hexKSN == null || hexKSN.trim().length() == 0) + throw new IllegalArgumentException("KSN cannot be empty."); + if (idLength + deviceLength + counterLength > hexKSN.length()) + throw new IllegalArgumentException("Length spec doesn't match KSN."); + setBaseKeyID(hexKSN.substring(0, idLength)); + setDeviceID(hexKSN.substring(idLength, idLength + deviceLength)); + setTransactionCounter(hexKSN.substring(idLength + deviceLength, idLength + deviceLength + counterLength)); + } + + /** + * Constructs a key serial number object from its binary representation. + * @param binKSN binary representation of the KSN. + * @param idLength length of the base key ID. + * @param deviceLength length of the device ID. + * @param counterLength length of the transaction counter. + */ + public KeySerialNumber(byte[] binKSN, int idLength, int deviceLength, int counterLength) { + this(ISOUtil.byte2hex(binKSN).toUpperCase(), idLength, deviceLength, counterLength); + } + /** * * @param baseKeyID a HexString representing the BaseKeyID (also called KeySet ID) diff --git a/jpos/src/test/java/org/jpos/security/KeySerialNumberTest.java b/jpos/src/test/java/org/jpos/security/KeySerialNumberTest.java index 07ec93d6ce..0301020fde 100644 --- a/jpos/src/test/java/org/jpos/security/KeySerialNumberTest.java +++ b/jpos/src/test/java/org/jpos/security/KeySerialNumberTest.java @@ -22,12 +22,14 @@ import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtMost; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import org.jpos.iso.ISOUtil; import org.junit.jupiter.api.Test; public class KeySerialNumberTest { @@ -123,4 +125,44 @@ public void testSetTransactionCounter() throws Throwable { assertEquals("testKeySerialNumberTransactionCounter1", keySerialNumber.transactionCounter, "keySerialNumber.transactionCounter"); } + + @Test + public void testBinaryConstructor() { + byte[] ksnBin = ISOUtil.hex2byte("9876543210E00008"); + KeySerialNumber ksn = new KeySerialNumber(ksnBin, 6, 5, 5); + assertEquals("987654", ksn.getBaseKeyID()); + assertEquals("3210E", ksn.getDeviceID()); + assertEquals("00008", ksn.getTransactionCounter()); + } + + @Test + public void testHexConstructor() { + String ksnHex = "9876543210E00008"; + KeySerialNumber ksn = new KeySerialNumber(ksnHex, 6, 5, 5); + assertEquals("987654", ksn.getBaseKeyID()); + assertEquals("3210E", ksn.getDeviceID()); + assertEquals("00008", ksn.getTransactionCounter()); + } + + + @Test + public void testHexConstructorWrongLength() { + assertThrows(IllegalArgumentException.class, () -> { + new KeySerialNumber("9876543210E008", 6, 5, 5); + }); + } + + @Test + public void testHexConstructorNullKSN() { + assertThrows(IllegalArgumentException.class, () -> { + new KeySerialNumber((String) null, 6, 5, 5); + }); + } + + @Test + public void testHexConstructorEmptyKSN() { + assertThrows(IllegalArgumentException.class, () -> { + new KeySerialNumber(" ", 6, 5, 5); + }); + } } From dd178414515ae64fcddffe6e5db6aa31b5523a86 Mon Sep 17 00:00:00 2001 From: Federico Date: Fri, 5 May 2023 23:22:00 -0300 Subject: [PATCH 2/2] Remove extra blank line. --- jpos/src/test/java/org/jpos/security/KeySerialNumberTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/jpos/src/test/java/org/jpos/security/KeySerialNumberTest.java b/jpos/src/test/java/org/jpos/security/KeySerialNumberTest.java index 0301020fde..6d2683104b 100644 --- a/jpos/src/test/java/org/jpos/security/KeySerialNumberTest.java +++ b/jpos/src/test/java/org/jpos/security/KeySerialNumberTest.java @@ -144,7 +144,6 @@ public void testHexConstructor() { assertEquals("00008", ksn.getTransactionCounter()); } - @Test public void testHexConstructorWrongLength() { assertThrows(IllegalArgumentException.class, () -> {