From c95aecbb9227e42e8d311a669f36209998392eb3 Mon Sep 17 00:00:00 2001 From: Murtuza Chhil Date: Fri, 4 Dec 2026 03:54:56 +0530 Subject: [PATCH] fix(paymentcryptography): the account number may be the account field (#10076) TranslatePinData binds a format 0 or 3 PIN block to the twelve-digit account field, the twelve right-most digits of the account number without its check digit, and a caller holding the block sends that field as PrimaryAccountNumber. A twelve-digit value was read as a PAN whose check digit was dropped, so the derived field was one digit short and the block did not decipher. For the formats 0 and 3 a twelve-digit value is now the field itself, while format 4, which binds the PIN to the whole PAN, reads it as a PAN as before; the Visa verification value follows the same reading. A request whose translation attributes name no account number is refused with ValidationException instead of throwing. Closes #10077 --- .../test/PaymentCryptographyPinTest.java | 62 +++++++++++++ docs/services/payment-cryptography-data.md | 8 ++ .../PaymentCryptographyPinOperations.java | 4 +- .../paymentcryptography/crypto/PinCrypto.java | 77 ++++++++++------ .../PaymentCryptographyPinOperationsTest.java | 88 ++++++++++++++++--- 5 files changed, 196 insertions(+), 43 deletions(-) diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/PaymentCryptographyPinTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/PaymentCryptographyPinTest.java index 09379c1979..db9c6f2d4e 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/PaymentCryptographyPinTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/PaymentCryptographyPinTest.java @@ -32,6 +32,10 @@ class PaymentCryptographyPinTest { /** The account number the PIN belongs to. */ private static final String PAN = "5081470082639564"; + /** The account field of the account number: its twelve right-most digits without the + * check digit. It is what a caller that holds the PIN block holds. */ + private static final String ACCOUNT_FIELD = "147008263956"; + /** The index of the verification key the PVV is computed under. */ private static final int VERIFICATION_KEY_INDEX = 1; @@ -226,6 +230,64 @@ class PaymentCryptographyPinTest { assertThat(block0).hasSize(16); } + @Test + @DisplayName("a PIN block bound to the account field translates") + void aPinBlockBoundToTheAccountFieldTranslates() { + // The account field is the twelve right-most digits of the account number without + // its check digit, and a caller that holds the PIN block holds the field: the + // translation attributes carry it, and the service reads it as the field. + String outgoingKeyArn = createKeyArn(KeyAlgorithm.TDES_2_KEY, + KeyUsage.TR31_P0_PIN_ENCRYPTION_KEY, m -> m.encrypt(true).decrypt(true)); + + // The block the acquirer sends is bound to the account field of the account number. + GeneratePinDataResponse generated = client.generatePinData(r -> r + .encryptionKeyIdentifier(pinEncryptionKeyArn) + .generationKeyIdentifier(visaVerificationKeyArn) + .pinBlockFormat(PinBlockFormatForPinData.ISO_FORMAT_0) + .primaryAccountNumber(PAN) + .generationAttributes(PinGenerationAttributes.builder() + .visaPin(VisaPin.builder() + .pinVerificationKeyIndex(VERIFICATION_KEY_INDEX) + .build()) + .build())); + String block0 = generated.encryptedPinBlock(); + String value = generated.pinData().verificationValue(); + + String translated = client.translatePinData(r -> r + .encryptedPinBlock(block0) + .incomingKeyIdentifier(pinEncryptionKeyArn) + .outgoingKeyIdentifier(outgoingKeyArn) + .incomingTranslationAttributes(TranslationIsoFormats.builder() + .isoFormat0(TranslationPinDataIsoFormat034.builder() + .primaryAccountNumber(ACCOUNT_FIELD) + .build()) + .build()) + .outgoingTranslationAttributes(TranslationIsoFormats.builder() + .isoFormat0(TranslationPinDataIsoFormat034.builder() + .primaryAccountNumber(ACCOUNT_FIELD) + .build()) + .build())) + .pinBlock(); + + assertThat(translated).isNotBlank(); + assertThat(translated).hasSize(16); + + // The translated block still verifies against the verification value that goes + // with it: the PIN survived the translation, bound to the same account. + client.verifyPinData(r -> r + .encryptionKeyIdentifier(outgoingKeyArn) + .verificationKeyIdentifier(visaVerificationKeyArn) + .pinBlockFormat(PinBlockFormatForPinData.ISO_FORMAT_0) + .primaryAccountNumber(PAN) + .encryptedPinBlock(translated) + .verificationAttributes(PinVerificationAttributes.builder() + .visaPin(VisaPinVerification.builder() + .pinVerificationKeyIndex(VERIFICATION_KEY_INDEX) + .verificationValue(value) + .build()) + .build())); + } + @Test void aKeyThatMayNotEncryptIsRefused() { String readOnlyKeyArn = createKeyArn(KeyAlgorithm.TDES_2_KEY, diff --git a/docs/services/payment-cryptography-data.md b/docs/services/payment-cryptography-data.md index da9e68226b..50d0587b12 100644 --- a/docs/services/payment-cryptography-data.md +++ b/docs/services/payment-cryptography-data.md @@ -97,6 +97,14 @@ client that talks to the control plane does not reach it by accident. over is a four-digit one and a longer PIN has no verification value. A PIN that does not verify fails the request with `VerificationFailedException`, because the answer of a verification has no verdict in it. +- **The account field is the account number without its check digit.** The formats 0 and 3 + bind the PIN to the twelve-digit account field: the twelve right-most digits of the + account number, which is the PAN without its check digit. A `PrimaryAccountNumber` of + twelve digits is the field itself there, and a longer one is a PAN whose check digit is + dropped to leave the field, so a caller that holds the field and one that holds the PAN + agree on the block and on the verification value. The format 4 binds the PIN to the whole + account number, so a twelve-digit account number is a PAN there, and its check digit is + the one the verification value leaves out. - **The PIN block formats this service makes are the three the model names beside the format 1.** The reference's model names four formats for the PIN data: the format 0, the format 1, the format 3 and the format 4. This service makes the format 0, the format 3 and the format 4, so diff --git a/src/main/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyPinOperations.java b/src/main/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyPinOperations.java index 0742c59080..cd1432047b 100644 --- a/src/main/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyPinOperations.java +++ b/src/main/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyPinOperations.java @@ -158,7 +158,7 @@ public class PaymentCryptographyPinOperations { if (pin.length() != VISA_PIN_LENGTH) { throw verificationFailed(); } - if (!PinCrypto.computePvv(pan, pvki, pin, materialHex(pvk)).equals(pvv)) { + if (!PinCrypto.computePvv(format, pan, pvki, pin, materialHex(pvk)).equals(pvv)) { throw verificationFailed(); } @@ -245,7 +245,7 @@ public class PaymentCryptographyPinOperations { throw validation("the Visa verification value is computed over a four digit PIN, and " + "the PIN block carries " + pin.length() + " digits"); } - String pvv = PinCrypto.computePvv(pan, pvki, pin, materialHex(pvk)); + String pvv = PinCrypto.computePvv(format, pan, pvki, pin, materialHex(pvk)); // The model's PinData is a union of the PIN offset and the verification value, and a // union carries one of them: this is the verification value. diff --git a/src/main/java/io/floci/aws/services/paymentcryptography/crypto/PinCrypto.java b/src/main/java/io/floci/aws/services/paymentcryptography/crypto/PinCrypto.java index 3d8ee4031c..472d64de73 100644 --- a/src/main/java/io/floci/aws/services/paymentcryptography/crypto/PinCrypto.java +++ b/src/main/java/io/floci/aws/services/paymentcryptography/crypto/PinCrypto.java @@ -7,10 +7,11 @@ import java.util.Random; * Issuer PIN primitives: the ISO 9564-1 PIN blocks and their decoders. * *

The account field is the 12 right-most PAN digits excluding the check - * digit. The API takes a primary account number, which carries its own - * check digit, so the digit is dropped and the twelve right-most of what is - * left are the field. A PAN of twelve digits leaves eleven, and the field is - * padded with a zero on the left. + * digit. The API takes a primary account number, which may be the whole + * PAN or the account field itself. A whole PAN carries its own check digit, + * so the digit is dropped and the twelve right-most of what is left are the + * field. A twelve-digit account number carries no check digit, so it is the + * field itself. * *

Format 4 is the odd one out. It does not use the account field at all: * it carries the whole PAN, and it is the only format an AES key can protect. @@ -30,25 +31,50 @@ public final class PinCrypto { /** * The 12-digit ISO-0 account field. * - *

The field is the twelve right-most digits of the PAN without its check - * digit, so the digit is dropped and the twelve right-most of what is left - * are the field. A PAN of twelve digits leaves eleven, and the field is - * padded with a zero on the left. + *

The field is the twelve right-most digits of the account number, and the account + * number is the PAN without its check digit, so the digit is dropped and the twelve + * right-most of what is left are the field.

+ * + *

A twelve-digit account number is the field itself. The formats 0 and 3 bind the + * PIN to the field, and a caller that holds the field sends it as it is: it carries no + * check digit to drop, and dropping one would leave eleven digits, which is not a + * field. A caller that holds the field rather than the PAN is therefore read the way + * the reference reads it.

* * @throws IllegalArgumentException if the PAN has fewer than twelve digits, * which is fewer than the field holds */ public static String account12(String pan) { + String accountNumber = accountNumber(PinBlockFormat.ISO_FORMAT_0, pan); + return accountNumber.substring(accountNumber.length() - 12); + } + + /** + * The account number the request carries. + * + *

The account number is the PAN without its check digit, and the PIN block's + * account field is the twelve right-most digits of it. The formats 0 and 3 bind the + * PIN to that field, and a caller that holds the field sends it as it is, so a + * twelve-digit value is the field and carries no check digit to leave out. The format + * 4 binds the PIN to the whole PAN, so the value carries its check digit, and that is + * the digit left out. + * + *

Whatever the form, the twelve right-most digits of the account number are the + * field and the eleven right-most are the digits of the verification value. + * + * @throws IllegalArgumentException if the PAN has fewer than twelve digits, + * which is fewer than the account number holds + */ + private static String accountNumber(PinBlockFormat format, String pan) { String d = digits(pan); if (d.length() < 12) { throw new IllegalArgumentException("the account field is twelve digits, and the PAN " + "has fewer"); } - String withoutCheckDigit = d.substring(0, d.length() - 1); - if (withoutCheckDigit.length() >= 12) { - return withoutCheckDigit.substring(withoutCheckDigit.length() - 12); + if (!format.isAes() && d.length() == 12) { + return d; } - return "0" + withoutCheckDigit; + return d.substring(0, d.length() - 1); } /** @@ -346,11 +372,19 @@ public final class PinCrypto { * PVV = decimalize(3DES_EDE(PVK, TSP as an 8-byte block), 4) * * + *

The eleven digits of the transformed security parameter are the eleven right-most + * of the account number. The formats 0 and 3 bind the PIN to the account field, so the + * value may be that field, and the two forms of the account number give the same + * eleven digits and the same value: a party that holds the field and one that holds + * the PAN agree on it. The format 4 binds the PIN to the whole PAN, so its value + * carries the check digit, and that is the digit left out. + * *

{@code pvkHex} is a double-length (16-byte) key. */ - public static String computePvv(String pan, int pvki, String pin, String pvkHex) { - String d = digits(pan); - String pan11 = tspDigits(d); + public static String computePvv(PinBlockFormat format, String pan, int pvki, String pin, + String pvkHex) { + String accountNumber = accountNumber(format, pan); + String pan11 = accountNumber.substring(accountNumber.length() - 11); String p4 = digits(pin); if (p4.length() != 4) { throw new IllegalArgumentException("Visa PVV uses a 4-digit PIN"); @@ -363,19 +397,6 @@ public final class PinCrypto { HEX.formatHex(Ciphers.tdes2Encrypt(HEX.parseHex(tsp), HEX.parseHex(pvkHex))), 4); } - /** - * The 11 digits of the TSP: the 11 right-most digits of the account - * number, excluding the check digit. - * - *

The API takes a primary account number, which carries its own check - * digit, so the digit is dropped and the eleven right-most of what is left - * are the TSP's. - */ - private static String tspDigits(String pan) { - return pan.substring(0, pan.length() - 1) - .substring(pan.length() - 1 - 11); - } - /** Strip everything that is not a decimal digit. */ public static String digits(String s) { if (s == null) { diff --git a/src/test/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyPinOperationsTest.java b/src/test/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyPinOperationsTest.java index f439b50de9..1d8b6bd9f1 100644 --- a/src/test/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyPinOperationsTest.java +++ b/src/test/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyPinOperationsTest.java @@ -20,7 +20,6 @@ import java.util.LinkedHashMap; import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -39,6 +38,10 @@ class PaymentCryptographyPinOperationsTest { /** The account number the PIN belongs to. */ private static final String PAN = "5081470082639564"; + /** The account field of the account number: its twelve right-most digits without the + * check digit. It is what a caller that holds the PIN block holds. */ + private static final String ACCOUNT_FIELD = "147008263956"; + /** The PIN the account holds. */ private static final String PIN = "1234"; @@ -139,22 +142,27 @@ class PaymentCryptographyPinOperationsTest { } @Test - void aPanOfTwelveDigitsIsAPanAndNotAnAccountField() { - // The API takes a primary account number, which carries its own check digit, so a - // twelve-digit one leaves eleven digits and the account field is padded with a - // zero on the left. - assertEquals("014700826395", PinCrypto.account12("147008263956")); + void aTwelveDigitAccountNumberIsTheAccountField() { + // The account field is the twelve right-most digits of the account number, and the + // account number is the PAN without its check digit. The formats 0 and 3 bind the + // PIN to the field, so a twelve-digit account number carries no check digit and + // is the field itself. + assertEquals("147008263956", PinCrypto.account12(ACCOUNT_FIELD)); // A longer PAN gives the twelve right-most digits without its check digit. assertEquals("147008263956", PinCrypto.account12(PAN)); - // The verification value follows the account field, so the twelve-digit PAN does - // not give the one its own account field would. - assertNotEquals(PinCrypto.computePvv(PAN, 1, PIN, PVK), - PinCrypto.computePvv("147008263956", 1, PIN, PVK)); + // The verification value follows the account number, so the two forms of it give + // the same value: one party may hold the PAN and another the field. + assertEquals(PinCrypto.computePvv(PinBlockFormat.ISO_FORMAT_0, PAN, 1, PIN, PVK), + PinCrypto.computePvv(PinBlockFormat.ISO_FORMAT_0, ACCOUNT_FIELD, 1, PIN, PVK)); + // The format 4 binds the PIN to the whole PAN, so there a twelve-digit account + // number is a PAN, and the digit the value leaves out is its check digit. + assertEquals("4747", PinCrypto.computePvv(PinBlockFormat.ISO_FORMAT_4, ACCOUNT_FIELD, 1, + PIN, PVK)); } @Test void theVerificationValueIsTheOneTheStandardGives() { - assertEquals("0463", PinCrypto.computePvv(PAN, 1, PIN, PVK)); + assertEquals("0463", PinCrypto.computePvv(PinBlockFormat.ISO_FORMAT_0, PAN, 1, PIN, PVK)); } private static JsonNode visaPinVerificationValue(String block) { @@ -259,7 +267,8 @@ class PaymentCryptographyPinOperationsTest { String block = PinCrypto.encipherPinBlock(PinBlockFormat.ISO_FORMAT_0, "12345", PAN, key, new SecureRandom()); // The verification value is over the four-digit PIN the issuer holds. - String pvv = PinCrypto.computePvv(PAN, 1, PIN, pvk.getKeyMaterialHex()); + String pvv = PinCrypto.computePvv(PinBlockFormat.ISO_FORMAT_0, PAN, 1, PIN, + pvk.getKeyMaterialHex()); AwsException refused = assertThrows(AwsException.class, () -> pin.verifyPinData(request(Map.of( "EncryptionKeyIdentifier", pek.getArn(), "VerificationKeyIdentifier", pvk.getArn(), @@ -363,6 +372,36 @@ class PaymentCryptographyPinOperationsTest { assertEquals(tdesPek.getArn(), verified.get("EncryptionKeyArn")); } + @Test + void aFormatZeroBlockIsTranslatedWhenTheAccountNumberIsTheAccountField() { + // The translation attributes carry the account field, which is the twelve right-most + // digits of the account number without its check digit: a caller that holds the PIN + // block holds the field, not the whole PAN. + PaymentCryptographyKey inPek = pinEncryptionKey(); + PaymentCryptographyKey outPek = pinEncryptionKey(); + // The block the acquirer sends is bound to the account field of the account number. + String block = PinCrypto.encipherPinBlock(PinBlockFormat.ISO_FORMAT_0, PIN, PAN, + HexFormat.of().parseHex(inPek.getKeyMaterialHex()), new SecureRandom()); + + Map translated = pin.translatePinData(request(Map.of( + "EncryptedPinBlock", block, + "IncomingKeyIdentifier", inPek.getArn(), + "OutgoingKeyIdentifier", outPek.getArn(), + "IncomingTranslationAttributes", nested("IsoFormat0", + "PrimaryAccountNumber", ACCOUNT_FIELD), + "OutgoingTranslationAttributes", nested("IsoFormat0", + "PrimaryAccountNumber", ACCOUNT_FIELD))), + "us-east-1"); + // The answer names the outgoing key, which is the key the block is enciphered under. + assertEquals(outPek.getArn(), translated.get("KeyArn")); + + // The PIN survived the translation: the outgoing block carries it under the outgoing + // key, bound to the same account field. + assertEquals(PIN, PinCrypto.decipherPinBlock(PinBlockFormat.ISO_FORMAT_0, + (String) translated.get("PinBlock"), ACCOUNT_FIELD, + HexFormat.of().parseHex(outPek.getKeyMaterialHex()))); + } + @Test void aFormatThatNeedsAnAesKeyRefusesATdesKey() { PaymentCryptographyKey tdesPek = pinEncryptionKey(); @@ -645,6 +684,28 @@ class PaymentCryptographyPinOperationsTest { assertEquals("ValidationException", refused.getErrorCode()); } + @Test + void aTranslationWhoseAttributesCarryANullFormatIsRefused() { + // The model's translation attributes carry the account number, and a member that + // is null is absent as far as the service is concerned. A request that names no + // account number is refused with the refusal the caller can read, not with an + // error the service did not map. + PaymentCryptographyKey tdesPek = pinEncryptionKey(); + ObjectNode incoming = mapper.createObjectNode(); + incoming.putNull("IsoFormat0"); + ObjectNode outgoing = mapper.createObjectNode(); + outgoing.putNull("IsoFormat0"); + AwsException refused = assertThrows(AwsException.class, + () -> pin.translatePinData(request(Map.of( + "EncryptedPinBlock", "0000000000000000", + "IncomingKeyIdentifier", tdesPek.getArn(), + "OutgoingKeyIdentifier", tdesPek.getArn(), + "IncomingTranslationAttributes", incoming, + "OutgoingTranslationAttributes", outgoing)), + "us-east-1")); + assertEquals("ValidationException", refused.getErrorCode()); + } + @Test void aDukptPinKeyIsDerivedFromTheBaseDerivationKey() { // A DUKPT PIN key is derived from the base derivation key, so that is what the @@ -672,7 +733,8 @@ class PaymentCryptographyPinOperationsTest { "EncryptedPinBlock", block, "DukptAttributes", attributes, "VerificationAttributes", visaPin(1, - PinCrypto.computePvv(PAN, 1, PIN, pvk.getKeyMaterialHex())))), + PinCrypto.computePvv(PinBlockFormat.ISO_FORMAT_0, PAN, 1, PIN, + pvk.getKeyMaterialHex())))), "us-east-1"); assertEquals(bdk.getArn(), verified.get("EncryptionKeyArn")); }