Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix handling of packed MAC #78

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/main/java/nz/co/breakpoint/jmeter/iso8583/ISO8583Crypto.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,20 @@ protected void calculateMAC(ISO8583Sampler sampler) {
int macField = (fixedMacField != null && !fixedMacField.isEmpty()) ? Integer.parseInt(fixedMacField) :
((msg.getMaxField() - 1)/MAC_FIELD_NO + 1)*MAC_FIELD_NO; // round up to the next multiple of 64

int macLength = ((ISOBasePackager)msg.getPackager()).getFieldPackager(macField).getLength();
ISOFieldPackager fp = ((ISOBasePackager)msg.getPackager()).getFieldPackager(macField);
if (!(fp instanceof ISOBinaryFieldPackager)) {
log.error("MAC cannot be packed into non-binary field "+fp.getClass());
return;
}
int macLength = fp.getLength();
int packedLength = fp.getMaxPackedLength();

final String dummyMac = String.format("%0" + 2*macLength + "d", 0);
msg.set(macField, dummyMac);
try {
byte[] packedMsg = msg.pack();
// cut off MAC bytes and don't include them in calculation:
String mac = securityModule.generateMAC(Arrays.copyOf(packedMsg, packedMsg.length-macLength),
String mac = securityModule.generateMAC(Arrays.copyOf(packedMsg, packedMsg.length-packedLength),
macKey, macAlgorithm);
sampler.addField(String.valueOf(macField), ISOUtil.padright(mac, 2*macLength, 'f'));
} catch (ISOException e) {
Expand Down