mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(paymentcryptography): the PIN operations (#7397)
This commit is contained in:
1 parent
d0b50384fc
commit
6130fe9568
27 files changed
+3427
-191
No files matched your search
+78
-18
@@ -54,33 +54,52 @@ class PaymentCryptographyDataPlaneIamEnforcementTest {
|
||||
/** The key the MAC operations use, which is a MAC key and not a data-encryption one. */
|
||||
private String macKeyArn;
|
||||
|
||||
/** The key the PIN operations use to protect the block. */
|
||||
private String pinEncryptionKeyArn;
|
||||
|
||||
/** The key the PIN operations compute the verification value under. */
|
||||
private String visaVerificationKeyArn;
|
||||
|
||||
@TestHTTPResource("/")
|
||||
static URI baseUri;
|
||||
|
||||
private final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
/**
|
||||
* The ARN of a key the control plane makes from the given CreateKey body.
|
||||
*
|
||||
* <p>Every key this test needs is made the same way, so the request is written once
|
||||
* and each key only differs in the body it carries.
|
||||
*
|
||||
* @param body the CreateKey request body
|
||||
* @return the ARN of the key that was made
|
||||
*/
|
||||
private static String createKeyArn(String body) {
|
||||
return given().contentType("application/x-amz-json-1.0")
|
||||
.header("X-Amz-Target", "PaymentCryptographyControlPlane.CreateKey")
|
||||
.header("Authorization", auth(ADMIN, "payment-cryptography"))
|
||||
.body(body)
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().path("Key.KeyArn");
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void createTheKeyAndTheCaller() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
keyArn = given().contentType("application/x-amz-json-1.0")
|
||||
.header("X-Amz-Target", "PaymentCryptographyControlPlane.CreateKey")
|
||||
.header("Authorization", auth(ADMIN, "payment-cryptography"))
|
||||
.body("{\"KeyAttributes\":{\"KeyAlgorithm\":\"AES_256\",\"KeyClass\":\"SYMMETRIC_KEY\","
|
||||
+ "\"KeyUsage\":\"TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY\","
|
||||
+ "\"KeyModesOfUse\":{\"Encrypt\":true,\"Decrypt\":true}},\"Exportable\":true}")
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().path("Key.KeyArn");
|
||||
keyArn = createKeyArn("{\"KeyAttributes\":{\"KeyAlgorithm\":\"AES_256\",\"KeyClass\":\"SYMMETRIC_KEY\","
|
||||
+ "\"KeyUsage\":\"TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY\","
|
||||
+ "\"KeyModesOfUse\":{\"Encrypt\":true,\"Decrypt\":true}},\"Exportable\":true}");
|
||||
keyId = keyArn.substring(keyArn.lastIndexOf('/') + 1);
|
||||
macKeyArn = given().contentType("application/x-amz-json-1.0")
|
||||
.header("X-Amz-Target", "PaymentCryptographyControlPlane.CreateKey")
|
||||
.header("Authorization", auth(ADMIN, "payment-cryptography"))
|
||||
.body("{\"KeyAttributes\":{\"KeyAlgorithm\":\"AES_256\",\"KeyClass\":\"SYMMETRIC_KEY\","
|
||||
+ "\"KeyUsage\":\"TR31_M6_ISO_9797_5_CMAC_KEY\","
|
||||
+ "\"KeyModesOfUse\":{\"Generate\":true,\"Verify\":true}},\"Exportable\":true}")
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().path("Key.KeyArn");
|
||||
macKeyArn = createKeyArn("{\"KeyAttributes\":{\"KeyAlgorithm\":\"AES_256\",\"KeyClass\":\"SYMMETRIC_KEY\","
|
||||
+ "\"KeyUsage\":\"TR31_M6_ISO_9797_5_CMAC_KEY\","
|
||||
+ "\"KeyModesOfUse\":{\"Generate\":true,\"Verify\":true}},\"Exportable\":true}");
|
||||
pinEncryptionKeyArn = createKeyArn("{\"KeyAttributes\":{\"KeyAlgorithm\":\"TDES_2KEY\","
|
||||
+ "\"KeyClass\":\"SYMMETRIC_KEY\",\"KeyUsage\":\"TR31_P0_PIN_ENCRYPTION_KEY\","
|
||||
+ "\"KeyModesOfUse\":{\"Encrypt\":true,\"Decrypt\":true}},\"Exportable\":true}");
|
||||
visaVerificationKeyArn = createKeyArn("{\"KeyAttributes\":{\"KeyAlgorithm\":\"TDES_2KEY\","
|
||||
+ "\"KeyClass\":\"SYMMETRIC_KEY\",\"KeyUsage\":\"TR31_V2_VISA_PIN_VERIFICATION_KEY\","
|
||||
+ "\"KeyModesOfUse\":{\"Generate\":true,\"Verify\":true}},\"Exportable\":true}");
|
||||
callerName = "payment-cryptography-data-" + Long.toString(System.nanoTime(), 36);
|
||||
caller = EnforcementFixtures.createUser(cleanup, port(), auth(ADMIN, "iam"), callerName);
|
||||
}
|
||||
@@ -236,6 +255,47 @@ class PaymentCryptographyDataPlaneIamEnforcementTest {
|
||||
.then();
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDenialOnOneKeyIsEnforcedWhenThePinNamesIt() {
|
||||
// The PIN data names its keys in the body, and the action is checked on them, so a
|
||||
// denial limited to the PIN encryption key holds.
|
||||
EnforcementFixtures.putUserPolicy(port(), auth(ADMIN, "iam"), callerName, "p",
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":["
|
||||
+ "{\"Effect\":\"Allow\",\"Action\":\"*\",\"Resource\":\"*\"},"
|
||||
+ "{\"Effect\":\"Deny\",\"Action\":\"payment-cryptography:GeneratePinData\","
|
||||
+ "\"Resource\":\"" + pinEncryptionKeyArn + "\"}]}");
|
||||
|
||||
assertEquals(403, generatePinData().extract().statusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAllowOnTheKeysThePinNamesIsEnough() {
|
||||
// The PIN data names two keys, and the action is checked on both, so the policy has to
|
||||
// allow the two of them and nothing else.
|
||||
EnforcementFixtures.putUserPolicy(port(), auth(ADMIN, "iam"), callerName, "p",
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":["
|
||||
+ "{\"Effect\":\"Allow\",\"Action\":\"payment-cryptography:*\","
|
||||
+ "\"Resource\":[" + "\"" + pinEncryptionKeyArn + "\","
|
||||
+ "\"" + visaVerificationKeyArn + "\"]}]}");
|
||||
|
||||
ValidatableResponse response = generatePinData();
|
||||
assertEquals(200, response.extract().statusCode());
|
||||
response.body("EncryptedPinBlock", notNullValue());
|
||||
}
|
||||
|
||||
/** The request is made as the caller, whose policy is what is under test. */
|
||||
private ValidatableResponse generatePinData() {
|
||||
return given().contentType("application/json")
|
||||
.header("Authorization", auth(caller.accessKeyId(), "payment-cryptography"))
|
||||
.body("{\"EncryptionKeyIdentifier\":\"" + pinEncryptionKeyArn + "\","
|
||||
+ "\"GenerationKeyIdentifier\":\"" + visaVerificationKeyArn + "\","
|
||||
+ "\"PinBlockFormat\":\"ISO_FORMAT_0\","
|
||||
+ "\"PrimaryAccountNumber\":\"5081470082639564\","
|
||||
+ "\"GenerationAttributes\":{\"VisaPin\":{\"PinVerificationKeyIndex\":1}}}")
|
||||
.when().post("/pindata/generate")
|
||||
.then();
|
||||
}
|
||||
|
||||
/** The request is made as the caller, whose policy is what is under test. */
|
||||
private ValidatableResponse reEncrypt(String path, String outgoingKeyArn) {
|
||||
return given().contentType("application/json")
|
||||
|
||||
@@ -72,6 +72,18 @@ class PaymentCryptographyDataPlaneIamRouteTest {
|
||||
assertEquals("payment-cryptography:VerifyMac", resolve("/_paymentcryptography/mac/verify"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void thePinOperationsResolveToTheirActions() {
|
||||
assertEquals("payment-cryptography:GeneratePinData",
|
||||
resolve("/_paymentcryptography/pindata/generate"));
|
||||
assertEquals("payment-cryptography:VerifyPinData",
|
||||
resolve("/_paymentcryptography/pindata/verify"));
|
||||
assertEquals("payment-cryptography:TranslatePinData",
|
||||
resolve("/_paymentcryptography/pindata/translate"));
|
||||
assertEquals("payment-cryptography:GenerateMacEmvPinChange",
|
||||
resolve("/_paymentcryptography/macemvpinchange/generate"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theRoutesAreReadUnderTheServicesScope() {
|
||||
// A client of the data plane signs for the service, which is one scope for both of
|
||||
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
package io.floci.aws.services.paymentcryptography;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Every route of the data plane is rewritten onto the internal prefix.
|
||||
*
|
||||
* <p>The data plane's routes are the ones the model names, and they sit under paths that S3's
|
||||
* path-style routes would claim. A request for one of them is rewritten before the routes
|
||||
* match, and a request for one whose prefix the filter does not name is not rewritten: it
|
||||
* never reaches its controller, and the request is answered by whichever controller claims it
|
||||
* instead. The prefixes are therefore held to the generated routes, so a route that is added
|
||||
* without its prefix is caught here rather than by a client.
|
||||
*/
|
||||
class PaymentCryptographyDataPlaneRouteFilterTest {
|
||||
|
||||
/** The routes the generated table holds. */
|
||||
private static final String ROUTES = "aws/rest-routes.json";
|
||||
|
||||
/** The model the data plane's routes come from. */
|
||||
private static final String MODEL = "payment-cryptography-data";
|
||||
|
||||
@Test
|
||||
void everyRouteOfTheDataPlaneSitsUnderAPrefixTheFilterNames() {
|
||||
for (String prefix : prefixesOfTheRoutes()) {
|
||||
assertTrue(PaymentCryptographyDataPlaneRouteFilter.CONTESTED_PATHS.contains(prefix),
|
||||
"the route prefix " + prefix + " is not one the route filter rewrites, so a "
|
||||
+ "request for it would never reach its controller");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyPrefixTheFilterNamesIsARouteOfTheDataPlane() {
|
||||
Set<String> prefixes = prefixesOfTheRoutes();
|
||||
assertTrue(!prefixes.isEmpty(), "the data plane has no routes");
|
||||
for (String prefix : PaymentCryptographyDataPlaneRouteFilter.CONTESTED_PATHS) {
|
||||
assertTrue(prefixes.contains(prefix),
|
||||
"the route filter rewrites the prefix " + prefix + ", which no route of the "
|
||||
+ "data plane sits under");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The path prefixes the data plane's routes sit under.
|
||||
*
|
||||
* @return the prefixes, each with its leading slash
|
||||
*/
|
||||
private static Set<String> prefixesOfTheRoutes() {
|
||||
JsonNode routes = read(ROUTES).path("models").path(MODEL).path("operations");
|
||||
Set<String> prefixes = new LinkedHashSet<>();
|
||||
routes.fields().forEachRemaining(entry -> {
|
||||
String uri = entry.getValue().path("requestUri").asText();
|
||||
// The route is the model's path under the internal prefix.
|
||||
assertTrue(uri.startsWith(PaymentCryptographyDataPlaneRouteFilter.INTERNAL_PREFIX),
|
||||
"the route " + uri + " does not sit under the internal prefix");
|
||||
String rest = uri.substring(PaymentCryptographyDataPlaneRouteFilter.INTERNAL_PREFIX.length());
|
||||
assertTrue(rest.startsWith("/"), "the route " + uri + " names no path");
|
||||
rest = rest.substring(1);
|
||||
int slash = rest.indexOf('/');
|
||||
assertTrue(slash > 0, "the route " + uri + " names no path");
|
||||
prefixes.add("/" + rest.substring(0, slash));
|
||||
});
|
||||
return prefixes;
|
||||
}
|
||||
|
||||
/** The generated table, read from the classpath. */
|
||||
private static JsonNode read(String resource) {
|
||||
try (InputStream in = PaymentCryptographyDataPlaneRouteFilterTest.class
|
||||
.getClassLoader().getResourceAsStream(resource)) {
|
||||
assertNotNull(in, "the generated table " + resource + " is not on the classpath");
|
||||
return new ObjectMapper().readTree(in);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException("the generated table " + resource + " could not be read", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+788
@@ -0,0 +1,788 @@
|
||||
package io.floci.aws.services.paymentcryptography;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.storage.InMemoryStorage;
|
||||
import io.floci.aws.services.paymentcryptography.crypto.DukptCrypto;
|
||||
import io.floci.aws.services.paymentcryptography.crypto.PinBlockFormat;
|
||||
import io.floci.aws.services.paymentcryptography.crypto.PinCrypto;
|
||||
import io.floci.aws.services.paymentcryptography.model.PaymentCryptographyKey;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.HexFormat;
|
||||
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;
|
||||
|
||||
/**
|
||||
* The PIN operations: the PIN blocks of the ISO 9564-1 formats, the Visa PIN verification
|
||||
* value, and the rules a key has to satisfy before it protects one.
|
||||
*
|
||||
* <p>The block and the verification value are checked against the values the standards
|
||||
* give, and the rules are checked by refusing: a key of another usage, a key of another
|
||||
* family, a key that may not do what the operation needs, and a verification value that is
|
||||
* not the one the issuer holds.
|
||||
*/
|
||||
class PaymentCryptographyPinOperationsTest {
|
||||
|
||||
/** The account number the PIN belongs to. */
|
||||
private static final String PAN = "5081470082639564";
|
||||
|
||||
/** The PIN the account holds. */
|
||||
private static final String PIN = "1234";
|
||||
|
||||
/** The Visa PIN verification key the PVV is computed under. */
|
||||
private static final String PVK = "0123456789ABCDEFFEDCBA9876543210";
|
||||
|
||||
private PaymentCryptographyService service;
|
||||
|
||||
private PaymentCryptographyPinOperations pin;
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new PaymentCryptographyService(
|
||||
new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(),
|
||||
new RegionResolver("us-east-1", "000000000000"));
|
||||
pin = new PaymentCryptographyPinOperations(service);
|
||||
}
|
||||
|
||||
/** A key with the algorithm, the usage and the modes the test needs. */
|
||||
private PaymentCryptographyKey createKey(String algorithm, String usage, String... modes) {
|
||||
Map<String, Object> attributes = new LinkedHashMap<>();
|
||||
attributes.put("KeyAlgorithm", algorithm);
|
||||
attributes.put("KeyClass", "SYMMETRIC_KEY");
|
||||
attributes.put("KeyUsage", usage);
|
||||
Map<String, Boolean> declared = new LinkedHashMap<>();
|
||||
for (String mode : modes) {
|
||||
declared.put(mode, Boolean.TRUE);
|
||||
}
|
||||
return service.createKey("us-east-1", attributes, declared, true, true, Map.of());
|
||||
}
|
||||
|
||||
/** The PIN encryption key of the tests: a Triple DES one that may encrypt and decrypt. */
|
||||
private PaymentCryptographyKey pinEncryptionKey() {
|
||||
return createKey("TDES_2KEY", "TR31_P0_PIN_ENCRYPTION_KEY", "Encrypt", "Decrypt");
|
||||
}
|
||||
|
||||
/** The Visa PIN verification key of the tests. */
|
||||
private PaymentCryptographyKey visaVerificationKey() {
|
||||
return createKey("TDES_2KEY", "TR31_V2_VISA_PIN_VERIFICATION_KEY", "Generate", "Verify");
|
||||
}
|
||||
|
||||
private static JsonNode request(Map<String, Object> members) {
|
||||
ObjectNode node = new ObjectMapper().createObjectNode();
|
||||
members.forEach((name, value) -> {
|
||||
if (value instanceof JsonNode json) {
|
||||
node.set(name, json);
|
||||
} else if (value instanceof Integer number) {
|
||||
node.put(name, number);
|
||||
} else {
|
||||
node.put(name, String.valueOf(value));
|
||||
}
|
||||
});
|
||||
return node;
|
||||
}
|
||||
|
||||
private static JsonNode nested(String member, String... pairs) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
ObjectNode node = mapper.createObjectNode();
|
||||
for (int i = 0; i < pairs.length; i += 2) {
|
||||
node.put(pairs[i], pairs[i + 1]);
|
||||
}
|
||||
ObjectNode outer = mapper.createObjectNode();
|
||||
outer.set(member, node);
|
||||
return outer;
|
||||
}
|
||||
|
||||
private static JsonNode visaPin(int pvki) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
ObjectNode visa = mapper.createObjectNode();
|
||||
visa.put("PinVerificationKeyIndex", pvki);
|
||||
ObjectNode outer = mapper.createObjectNode();
|
||||
outer.set("VisaPin", visa);
|
||||
return outer;
|
||||
}
|
||||
|
||||
private static JsonNode visaPin(int pvki, String value) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
ObjectNode visa = mapper.createObjectNode();
|
||||
visa.put("PinVerificationKeyIndex", pvki);
|
||||
visa.put("VerificationValue", value);
|
||||
ObjectNode outer = mapper.createObjectNode();
|
||||
outer.set("VisaPin", visa);
|
||||
return outer;
|
||||
}
|
||||
|
||||
@Test
|
||||
void theAccountFieldAndTheBlocksAreTheOnesTheStandardGives() {
|
||||
assertEquals("147008263956", PinCrypto.account12(PAN));
|
||||
assertEquals("0412208FF7D9C6A9", PinCrypto.iso0PinBlock(PIN, PAN));
|
||||
assertEquals("441234AAAAAAAAAA0102030405060708",
|
||||
HexFormat.of().withUpperCase().formatHex(PinCrypto.iso4PinField(PIN,
|
||||
new byte[] {1, 2, 3, 4, 5, 6, 7, 8})));
|
||||
assertEquals("45081470082639564000000000000000",
|
||||
HexFormat.of().withUpperCase().formatHex(PinCrypto.iso4PanField(PAN)));
|
||||
}
|
||||
|
||||
@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"));
|
||||
// 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));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theVerificationValueIsTheOneTheStandardGives() {
|
||||
assertEquals("0463", PinCrypto.computePvv(PAN, 1, PIN, PVK));
|
||||
}
|
||||
|
||||
private static JsonNode visaPinVerificationValue(String block) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
ObjectNode value = mapper.createObjectNode();
|
||||
value.put("EncryptedPinBlock", block);
|
||||
value.put("PinVerificationKeyIndex", 1);
|
||||
ObjectNode variant = mapper.createObjectNode();
|
||||
variant.set("VisaPinVerificationValue", value);
|
||||
return variant;
|
||||
}
|
||||
|
||||
@Test
|
||||
void aGeneratedBlockVerifiesAgainstTheValueThatGoesWithIt() {
|
||||
PaymentCryptographyKey pek = pinEncryptionKey();
|
||||
PaymentCryptographyKey pvk = visaVerificationKey();
|
||||
Map<String, Object> generated = pin.generatePinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", pek.getArn(),
|
||||
"GenerationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"GenerationAttributes", visaPin(1))),
|
||||
"us-east-1");
|
||||
String block = (String) generated.get("EncryptedPinBlock");
|
||||
assertEquals(16, block.length());
|
||||
assertEquals(pek.getArn(), generated.get("EncryptionKeyArn"));
|
||||
assertEquals(pvk.getArn(), generated.get("GenerationKeyArn"));
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> pinData = (Map<String, Object>) generated.get("PinData");
|
||||
String pvv = (String) pinData.get("VerificationValue");
|
||||
assertNotNull(pvv);
|
||||
// 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 and nothing else.
|
||||
assertEquals(Map.of("VerificationValue", pvv), pinData);
|
||||
|
||||
Map<String, Object> verified = pin.verifyPinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", pek.getArn(),
|
||||
"VerificationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"EncryptedPinBlock", block,
|
||||
"VerificationAttributes", visaPin(1, pvv))),
|
||||
"us-east-1");
|
||||
assertEquals(pek.getArn(), verified.get("EncryptionKeyArn"));
|
||||
assertEquals(pvk.getArn(), verified.get("VerificationKeyArn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aBlockWithTheWrongVerificationValueIsRefused() {
|
||||
PaymentCryptographyKey pek = pinEncryptionKey();
|
||||
PaymentCryptographyKey pvk = visaVerificationKey();
|
||||
Map<String, Object> generated = pin.generatePinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", pek.getArn(),
|
||||
"GenerationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"GenerationAttributes", visaPin(1))),
|
||||
"us-east-1");
|
||||
AwsException refused = assertThrows(AwsException.class, () -> pin.verifyPinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", pek.getArn(),
|
||||
"VerificationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"EncryptedPinBlock", generated.get("EncryptedPinBlock"),
|
||||
"VerificationAttributes", visaPin(1, "9999"))),
|
||||
"us-east-1"));
|
||||
assertEquals("VerificationFailedException", refused.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPinLongerThanFourDigitsDoesNotVerify() {
|
||||
// The Visa verification value is computed over four PIN digits, so a block that
|
||||
// carries a longer PIN is not one the value can be compared with. A block for
|
||||
// "12345" must not verify against the value over "1234".
|
||||
PaymentCryptographyKey pek = pinEncryptionKey();
|
||||
PaymentCryptographyKey pvk = visaVerificationKey();
|
||||
byte[] key = HexFormat.of().parseHex(pek.getKeyMaterialHex());
|
||||
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());
|
||||
AwsException refused = assertThrows(AwsException.class, () -> pin.verifyPinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", pek.getArn(),
|
||||
"VerificationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"EncryptedPinBlock", block,
|
||||
"VerificationAttributes", visaPin(1, pvv))),
|
||||
"us-east-1"));
|
||||
assertEquals("VerificationFailedException", refused.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDeclaredPinLengthThatIsNotFourIsRefused() {
|
||||
// The Visa verification value is over four PIN digits, so a request that declares
|
||||
// another length names something this path cannot verify.
|
||||
PaymentCryptographyKey pek = pinEncryptionKey();
|
||||
PaymentCryptographyKey pvk = visaVerificationKey();
|
||||
Map<String, Object> generated = pin.generatePinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", pek.getArn(),
|
||||
"GenerationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"GenerationAttributes", visaPin(1))),
|
||||
"us-east-1");
|
||||
AwsException refused = assertThrows(AwsException.class, () -> pin.verifyPinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", pek.getArn(),
|
||||
"VerificationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"PinDataLength", 5,
|
||||
"EncryptedPinBlock", generated.get("EncryptedPinBlock"),
|
||||
"VerificationAttributes", visaPin(1, "1234"))),
|
||||
"us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aGivenBlockWithALongerPinIsRefused() {
|
||||
// The Visa verification value is a four digit number, so a block that carries a
|
||||
// longer PIN has no value to be computed for it. The request is refused, rather
|
||||
// than the computation failing inside the request.
|
||||
PaymentCryptographyKey pek = pinEncryptionKey();
|
||||
PaymentCryptographyKey pvk = visaVerificationKey();
|
||||
byte[] key = HexFormat.of().parseHex(pek.getKeyMaterialHex());
|
||||
String block = PinCrypto.encipherPinBlock(PinBlockFormat.ISO_FORMAT_0, "12345", PAN, key,
|
||||
new SecureRandom());
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> pin.generatePinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", pek.getArn(),
|
||||
"GenerationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"GenerationAttributes", visaPinVerificationValue(block))),
|
||||
"us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aBlockTranslatesFromFormatFourToFormatZero() {
|
||||
PaymentCryptographyKey aesPek = createKey("AES_128", "TR31_P0_PIN_ENCRYPTION_KEY",
|
||||
"Encrypt", "Decrypt");
|
||||
PaymentCryptographyKey tdesPek = pinEncryptionKey();
|
||||
PaymentCryptographyKey pvk = visaVerificationKey();
|
||||
Map<String, Object> generated = pin.generatePinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", aesPek.getArn(),
|
||||
"GenerationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_4",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"GenerationAttributes", visaPin(1))),
|
||||
"us-east-1");
|
||||
String block4 = (String) generated.get("EncryptedPinBlock");
|
||||
assertEquals(32, block4.length());
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> pinData = (Map<String, Object>) generated.get("PinData");
|
||||
String pvv = (String) pinData.get("VerificationValue");
|
||||
|
||||
Map<String, Object> translated = pin.translatePinData(request(Map.of(
|
||||
"EncryptedPinBlock", block4,
|
||||
"IncomingKeyIdentifier", aesPek.getArn(),
|
||||
"OutgoingKeyIdentifier", tdesPek.getArn(),
|
||||
"IncomingTranslationAttributes", nested("IsoFormat4", "PrimaryAccountNumber", PAN),
|
||||
"OutgoingTranslationAttributes", nested("IsoFormat0", "PrimaryAccountNumber", PAN))),
|
||||
"us-east-1");
|
||||
String block0 = (String) translated.get("PinBlock");
|
||||
assertEquals(16, block0.length());
|
||||
// The answer names the outgoing key, which is the key the block is enciphered under.
|
||||
assertEquals(tdesPek.getArn(), translated.get("KeyArn"));
|
||||
assertEquals(tdesPek.getKeyCheckValue(), translated.get("KeyCheckValue"));
|
||||
|
||||
// The PIN survived the translation: the translated block still verifies against the
|
||||
// verification value the format 4 block carried.
|
||||
Map<String, Object> verified = pin.verifyPinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", tdesPek.getArn(),
|
||||
"VerificationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"EncryptedPinBlock", block0,
|
||||
"VerificationAttributes", visaPin(1, pvv))),
|
||||
"us-east-1");
|
||||
assertEquals(tdesPek.getArn(), verified.get("EncryptionKeyArn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFormatThatNeedsAnAesKeyRefusesATdesKey() {
|
||||
PaymentCryptographyKey tdesPek = pinEncryptionKey();
|
||||
PaymentCryptographyKey pvk = visaVerificationKey();
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> pin.generatePinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", tdesPek.getArn(),
|
||||
"GenerationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_4",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"GenerationAttributes", visaPin(1))),
|
||||
"us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
assertTrue(refused.getMessage().contains("AES"), refused.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aVerificationKeyThatIsNotTdesIsRefused() {
|
||||
// The PVV is defined over Triple DES, so an AES verification key has
|
||||
// no such computation. It is refused with the reason rather than
|
||||
// failing inside the cipher.
|
||||
PaymentCryptographyKey pek = pinEncryptionKey();
|
||||
PaymentCryptographyKey pvk = createKey("AES_256", "TR31_V2_VISA_PIN_VERIFICATION_KEY",
|
||||
"Generate", "Verify");
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> pin.generatePinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", pek.getArn(),
|
||||
"GenerationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"GenerationAttributes", visaPin(1))),
|
||||
"us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aVerificationKeyOfAnotherUsageIsRefused() {
|
||||
// The Visa PIN verification value is computed under a Visa PIN verification key, and
|
||||
// the usage is what says which key that is.
|
||||
PaymentCryptographyKey pek = pinEncryptionKey();
|
||||
PaymentCryptographyKey pvk = createKey("TDES_2KEY", "TR31_P1_PIN_GENERATION_KEY",
|
||||
"Generate", "Verify");
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> pin.generatePinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", pek.getArn(),
|
||||
"GenerationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"GenerationAttributes", visaPin(1))),
|
||||
"us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
assertTrue(refused.getMessage().contains("TR31_V2_VISA_PIN_VERIFICATION_KEY"),
|
||||
refused.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKeyThatMayNotEncryptIsRefused() {
|
||||
PaymentCryptographyKey pek = createKey("TDES_2KEY", "TR31_P0_PIN_ENCRYPTION_KEY", "Decrypt");
|
||||
PaymentCryptographyKey pvk = visaVerificationKey();
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> pin.generatePinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", pek.getArn(),
|
||||
"GenerationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"GenerationAttributes", visaPin(1))),
|
||||
"us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aVerificationKeyThatMayNotVerifyIsRefused() {
|
||||
PaymentCryptographyKey pek = pinEncryptionKey();
|
||||
PaymentCryptographyKey pvk = createKey("TDES_2KEY", "TR31_V2_VISA_PIN_VERIFICATION_KEY",
|
||||
"Generate");
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> pin.verifyPinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", pek.getArn(),
|
||||
"VerificationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"EncryptedPinBlock", "0000000000000000",
|
||||
"VerificationAttributes", visaPin(1, "1234"))),
|
||||
"us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theTranslationOfAnAs2805BlockIsRefused() {
|
||||
// The byte layout of the AS2805 cryptogram is in the standard and is not published,
|
||||
// so a cryptogram the service produced would not open here.
|
||||
AwsException refusal = assertThrows(AwsException.class, () -> pin.translatePinData(request(Map.of(
|
||||
"IncomingKeyIdentifier", "arn:aws:payment-cryptography:us-east-1:000000000000:key/x",
|
||||
"OutgoingKeyIdentifier", "arn:aws:payment-cryptography:us-east-1:000000000000:key/y",
|
||||
"IncomingAs2805Attributes", Map.of("WrappingKeyIdentifier", "x"),
|
||||
"EncryptedPinBlock", "0011223344556677")), "us-east-1"));
|
||||
assertEquals("ValidationException", refusal.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aVerificationKeyIndexThatIsOutOfRangeIsRefused() {
|
||||
// The Visa PVV names one of ten keys, and the API's index is between zero and six.
|
||||
PaymentCryptographyKey pek = pinEncryptionKey();
|
||||
PaymentCryptographyKey pvk = visaVerificationKey();
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> pin.generatePinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", pek.getArn(),
|
||||
"GenerationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"GenerationAttributes", visaPin(7))),
|
||||
"us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFormatThatIsNotMadeIsRefused() {
|
||||
PaymentCryptographyKey pek = pinEncryptionKey();
|
||||
PaymentCryptographyKey pvk = visaVerificationKey();
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> pin.generatePinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", pek.getArn(),
|
||||
"GenerationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_1",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"GenerationAttributes", visaPin(1))),
|
||||
"us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
assertTrue(refused.getMessage().contains("ISO_FORMAT_0"), refused.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theWrappedKeyFormIsRefused() {
|
||||
// A wrapped key is unwrapped with a key-encryption key, and the unwrapping is part of
|
||||
// the key exchange, which this service does not make yet.
|
||||
PaymentCryptographyKey pek = pinEncryptionKey();
|
||||
PaymentCryptographyKey pvk = visaVerificationKey();
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> pin.generatePinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", pek.getArn(),
|
||||
"GenerationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"GenerationAttributes", visaPin(1),
|
||||
"EncryptionWrappedKey", Map.of("KeyIdentifier", "arn:aws:payment-cryptography"
|
||||
+ ":us-east-1:000000000000:key/x"))),
|
||||
"us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theEmvPinChangeMacIsRefused() {
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> pin.generateMacEmvPinChange(request(Map.of(
|
||||
"NewPinPekIdentifier", "arn:aws:payment-cryptography:us-east-1:000000000000:key/x",
|
||||
"SecureMessagingIntegrityKeyIdentifier",
|
||||
"arn:aws:payment-cryptography:us-east-1:000000000000:key/y",
|
||||
"SecureMessagingConfidentialityKeyIdentifier",
|
||||
"arn:aws:payment-cryptography:us-east-1:000000000000:key/z",
|
||||
"NewEncryptedPinBlock", "0000000000000000",
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"MessageData", "0000",
|
||||
"DerivationMethodAttributes", Map.of("DerivationMethod", "V2"))),
|
||||
"us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKeyThatIsNotThereIsRefused() {
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> pin.verifyPinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", "arn:aws:payment-cryptography:us-east-1:"
|
||||
+ "000000000000:key/1234abcd1234abcd1234abcd1234abcd",
|
||||
"VerificationKeyIdentifier", "arn:aws:payment-cryptography:us-east-1:"
|
||||
+ "000000000000:key/1234abcd1234abcd1234abcd1234abcd",
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"EncryptedPinBlock", "0000000000000000",
|
||||
"VerificationAttributes", visaPin(1, "1234"))),
|
||||
"us-east-1"));
|
||||
assertEquals("ResourceNotFoundException", refused.getErrorCode());
|
||||
assertEquals(404, refused.getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aBlockThatIsNotReadAsAPinBlockIsRefused() {
|
||||
// The block is enciphered under one key and verified under another, so what it
|
||||
// deciphers to is not a PIN. The reference names that an invalid PIN block, and lists
|
||||
// the wrong key among its causes.
|
||||
PaymentCryptographyKey pek = pinEncryptionKey();
|
||||
PaymentCryptographyKey otherPek = pinEncryptionKey();
|
||||
PaymentCryptographyKey pvk = visaVerificationKey();
|
||||
Map<String, Object> generated = pin.generatePinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", pek.getArn(),
|
||||
"GenerationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"GenerationAttributes", visaPin(1))),
|
||||
"us-east-1");
|
||||
AwsException refused = assertThrows(AwsException.class, () -> pin.verifyPinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", otherPek.getArn(),
|
||||
"VerificationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"EncryptedPinBlock", generated.get("EncryptedPinBlock"),
|
||||
"VerificationAttributes", visaPin(1, "1234"))),
|
||||
"us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPinThatDoesNotVerifyIsRefusedWithTheVerificationError() {
|
||||
// The block is read, and the PIN it carries does not match the verification value.
|
||||
// That is a verification that failed, not an invalid block.
|
||||
PaymentCryptographyKey pek = pinEncryptionKey();
|
||||
PaymentCryptographyKey pvk = visaVerificationKey();
|
||||
Map<String, Object> generated = pin.generatePinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", pek.getArn(),
|
||||
"GenerationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"GenerationAttributes", visaPin(1))),
|
||||
"us-east-1");
|
||||
AwsException refused = assertThrows(AwsException.class, () -> pin.verifyPinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", pek.getArn(),
|
||||
"VerificationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"EncryptedPinBlock", generated.get("EncryptedPinBlock"),
|
||||
"VerificationAttributes", visaPin(1, "9999"))),
|
||||
"us-east-1"));
|
||||
assertEquals("VerificationFailedException", refused.getErrorCode());
|
||||
assertEquals("Pin block verification failed.", refused.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTranslationBetweenTwoAccountNumbersIsRefused() {
|
||||
// For PCI compliance the incoming and the outgoing account number are the same one,
|
||||
// and a PIN is not translated from one account number to another.
|
||||
PaymentCryptographyKey aesPek = createKey("AES_128", "TR31_P0_PIN_ENCRYPTION_KEY",
|
||||
"Encrypt", "Decrypt");
|
||||
PaymentCryptographyKey tdesPek = pinEncryptionKey();
|
||||
Map<String, Object> generated = pin.generatePinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", aesPek.getArn(),
|
||||
"GenerationKeyIdentifier", visaVerificationKey().getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_4",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"GenerationAttributes", visaPin(1))),
|
||||
"us-east-1");
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> pin.translatePinData(request(Map.of(
|
||||
"EncryptedPinBlock", generated.get("EncryptedPinBlock"),
|
||||
"IncomingKeyIdentifier", aesPek.getArn(),
|
||||
"OutgoingKeyIdentifier", tdesPek.getArn(),
|
||||
"IncomingTranslationAttributes", nested("IsoFormat4", "PrimaryAccountNumber", PAN),
|
||||
"OutgoingTranslationAttributes", nested("IsoFormat0",
|
||||
"PrimaryAccountNumber", "5081470082639565"))),
|
||||
"us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTranslationWhoseAttributesCarryNoAccountNumberIsRefused() {
|
||||
// The model's translation attributes carry the account number, and a request that
|
||||
// leaves it out is refused rather than translated.
|
||||
PaymentCryptographyKey aesPek = createKey("AES_128", "TR31_P0_PIN_ENCRYPTION_KEY",
|
||||
"Encrypt", "Decrypt");
|
||||
PaymentCryptographyKey tdesPek = pinEncryptionKey();
|
||||
ObjectNode incoming = mapper.createObjectNode();
|
||||
incoming.putNull("IsoFormat4");
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> pin.translatePinData(request(Map.of(
|
||||
"EncryptedPinBlock", "00112233445566778899AABBCCDDEEFF",
|
||||
"IncomingKeyIdentifier", aesPek.getArn(),
|
||||
"OutgoingKeyIdentifier", tdesPek.getArn(),
|
||||
"IncomingTranslationAttributes", incoming,
|
||||
"OutgoingTranslationAttributes", nested("IsoFormat0",
|
||||
"PrimaryAccountNumber", PAN))),
|
||||
"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
|
||||
// identifier names, and the key has to allow the derivation.
|
||||
PaymentCryptographyKey bdk = createKey("TDES_2KEY", "TR31_B0_BASE_DERIVATION_KEY",
|
||||
"DeriveKey");
|
||||
PaymentCryptographyKey pvk = visaVerificationKey();
|
||||
String ksn = "12345678901234567890";
|
||||
ObjectNode attributes = mapper.createObjectNode();
|
||||
attributes.put("KeySerialNumber", ksn);
|
||||
attributes.put("DukptDerivationType", "TDES_2KEY");
|
||||
attributes.put("DukptKeyVariant", "BIDIRECTIONAL");
|
||||
|
||||
// The block is enciphered under the derived PIN key, which is what the service
|
||||
// derives from the base derivation key and the KSN.
|
||||
byte[] pinKey = DukptCrypto.pinKey(bdk.getKeyMaterialHex(), ksn);
|
||||
String block = PinCrypto.encipherPinBlock(PinBlockFormat.ISO_FORMAT_0, PIN, PAN, pinKey,
|
||||
new SecureRandom());
|
||||
|
||||
Map<String, Object> verified = pin.verifyPinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", bdk.getArn(),
|
||||
"VerificationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"EncryptedPinBlock", block,
|
||||
"DukptAttributes", attributes,
|
||||
"VerificationAttributes", visaPin(1,
|
||||
PinCrypto.computePvv(PAN, 1, PIN, pvk.getKeyMaterialHex())))),
|
||||
"us-east-1");
|
||||
assertEquals(bdk.getArn(), verified.get("EncryptionKeyArn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDukptTranslationDerivesItsPinKeyFromTheBaseDerivationKey() {
|
||||
// The translation's DUKPT attributes are the model's other shape: the key serial
|
||||
// number, an optional derivation type and an optional key variant.
|
||||
PaymentCryptographyKey bdk = createKey("TDES_2KEY", "TR31_B0_BASE_DERIVATION_KEY",
|
||||
"DeriveKey");
|
||||
PaymentCryptographyKey tdesPek = pinEncryptionKey();
|
||||
String ksn = "12345678901234567890";
|
||||
ObjectNode attributes = mapper.createObjectNode();
|
||||
attributes.put("KeySerialNumber", ksn);
|
||||
attributes.put("DukptKeyDerivationType", "TDES_2KEY");
|
||||
attributes.put("DukptKeyVariant", "BIDIRECTIONAL");
|
||||
|
||||
// The block is enciphered under the PIN key the DUKPT derivation makes.
|
||||
byte[] pinKey = DukptCrypto.pinKey(bdk.getKeyMaterialHex(), ksn);
|
||||
String block = PinCrypto.encipherPinBlock(PinBlockFormat.ISO_FORMAT_0, PIN, PAN, pinKey,
|
||||
new SecureRandom());
|
||||
|
||||
Map<String, Object> translated = pin.translatePinData(request(Map.of(
|
||||
"EncryptedPinBlock", block,
|
||||
"IncomingKeyIdentifier", bdk.getArn(),
|
||||
"OutgoingKeyIdentifier", tdesPek.getArn(),
|
||||
"IncomingDukptAttributes", attributes,
|
||||
"IncomingTranslationAttributes", nested("IsoFormat0", "PrimaryAccountNumber", PAN),
|
||||
"OutgoingTranslationAttributes", nested("IsoFormat0", "PrimaryAccountNumber", PAN))),
|
||||
"us-east-1");
|
||||
// The answer names the outgoing key, which is the key the block is enciphered under.
|
||||
assertEquals(tdesPek.getArn(), translated.get("KeyArn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDukptTranslationOfAnotherKeyVariantIsRefused() {
|
||||
// The PIN key this path derives both encrypts and decrypts, so it is the
|
||||
// bidirectional one.
|
||||
PaymentCryptographyKey bdk = createKey("TDES_2KEY", "TR31_B0_BASE_DERIVATION_KEY",
|
||||
"DeriveKey");
|
||||
PaymentCryptographyKey tdesPek = pinEncryptionKey();
|
||||
ObjectNode attributes = mapper.createObjectNode();
|
||||
attributes.put("KeySerialNumber", "12345678901234567890");
|
||||
attributes.put("DukptKeyDerivationType", "TDES_2KEY");
|
||||
attributes.put("DukptKeyVariant", "REQUEST");
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> pin.translatePinData(request(Map.of(
|
||||
"EncryptedPinBlock", "0000000000000000",
|
||||
"IncomingKeyIdentifier", bdk.getArn(),
|
||||
"OutgoingKeyIdentifier", tdesPek.getArn(),
|
||||
"IncomingDukptAttributes", attributes,
|
||||
"IncomingTranslationAttributes", nested("IsoFormat0",
|
||||
"PrimaryAccountNumber", PAN),
|
||||
"OutgoingTranslationAttributes", nested("IsoFormat0",
|
||||
"PrimaryAccountNumber", PAN))),
|
||||
"us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTranslationWithAWrappedKeyIsRefused() {
|
||||
// The wrapped-key form of a PIN encryption key is refused: the unwrapping is part of
|
||||
// the key exchange, which this service does not make.
|
||||
PaymentCryptographyKey pek = pinEncryptionKey();
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> pin.translatePinData(request(Map.of(
|
||||
"EncryptedPinBlock", "0000000000000000",
|
||||
"IncomingKeyIdentifier", pek.getArn(),
|
||||
"OutgoingKeyIdentifier", pek.getArn(),
|
||||
"IncomingWrappedKey", Map.of("WrappedKeyMaterial",
|
||||
Map.of("Tr31KeyBlock", "0123456789ABCDEF0123456789ABCDEF")),
|
||||
"OutgoingWrappedKey", Map.of("WrappedKeyMaterial",
|
||||
Map.of("Tr31KeyBlock", "0123456789ABCDEF0123456789ABCDEF")),
|
||||
"IncomingTranslationAttributes", nested("IsoFormat0",
|
||||
"PrimaryAccountNumber", PAN),
|
||||
"OutgoingTranslationAttributes", nested("IsoFormat0",
|
||||
"PrimaryAccountNumber", PAN))),
|
||||
"us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDukptTranslationEncryptsTheOutgoingBlockUnderTheDerivedKey() {
|
||||
// The outgoing leg may be a DUKPT key as well, and the block is enciphered under
|
||||
// the PIN key the derivation makes.
|
||||
PaymentCryptographyKey pek = pinEncryptionKey();
|
||||
PaymentCryptographyKey bdk = createKey("TDES_2KEY", "TR31_B0_BASE_DERIVATION_KEY",
|
||||
"DeriveKey");
|
||||
ObjectNode attributes = mapper.createObjectNode();
|
||||
attributes.put("KeySerialNumber", "12345678901234567890");
|
||||
attributes.put("DukptKeyDerivationType", "TDES_2KEY");
|
||||
attributes.put("DukptKeyVariant", "BIDIRECTIONAL");
|
||||
|
||||
Map<String, Object> generated = pin.generatePinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", pek.getArn(),
|
||||
"GenerationKeyIdentifier", visaVerificationKey().getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"GenerationAttributes", visaPin(1))),
|
||||
"us-east-1");
|
||||
|
||||
Map<String, Object> translated = pin.translatePinData(request(Map.of(
|
||||
"EncryptedPinBlock", generated.get("EncryptedPinBlock"),
|
||||
"IncomingKeyIdentifier", pek.getArn(),
|
||||
"OutgoingKeyIdentifier", bdk.getArn(),
|
||||
"OutgoingDukptAttributes", attributes,
|
||||
"IncomingTranslationAttributes", nested("IsoFormat0", "PrimaryAccountNumber", PAN),
|
||||
"OutgoingTranslationAttributes", nested("IsoFormat0", "PrimaryAccountNumber", PAN))),
|
||||
"us-east-1");
|
||||
// The answer names the outgoing key, and the block it carries is enciphered under
|
||||
// the key that derivation makes.
|
||||
assertEquals(bdk.getArn(), translated.get("KeyArn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDukptDerivationOfAnotherTypeIsRefused() {
|
||||
PaymentCryptographyKey bdk = createKey("TDES_2KEY", "TR31_B0_BASE_DERIVATION_KEY",
|
||||
"DeriveKey");
|
||||
PaymentCryptographyKey pvk = visaVerificationKey();
|
||||
ObjectNode attributes = mapper.createObjectNode();
|
||||
attributes.put("KeySerialNumber", "12345678901234567890");
|
||||
attributes.put("DukptDerivationType", "AES_128");
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> pin.verifyPinData(request(Map.of(
|
||||
"EncryptionKeyIdentifier", bdk.getArn(),
|
||||
"VerificationKeyIdentifier", pvk.getArn(),
|
||||
"PinBlockFormat", "ISO_FORMAT_0",
|
||||
"PrimaryAccountNumber", PAN,
|
||||
"EncryptedPinBlock", "0000000000000000",
|
||||
"DukptAttributes", attributes,
|
||||
"VerificationAttributes", visaPin(1, "1234"))),
|
||||
"us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package io.floci.aws.services.paymentcryptography.crypto;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* The decimalization the Visa PIN verification value is made of.
|
||||
*
|
||||
* <p>The digits of the enciphered block come first, and the letters fill the gap when there
|
||||
* are not enough of them: a letter stands for its own value less ten, so A is zero and F is
|
||||
* five.
|
||||
*/
|
||||
class DecimalizeTest {
|
||||
|
||||
@Test
|
||||
void theDigitsComeFirst() {
|
||||
assertEquals("1234", Decimalize.of("1234ABCDEF", 4));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theLettersFillTheGap() {
|
||||
// The example the reference gives: the block holds three digits, so the first letter
|
||||
// completes the value, and F is five.
|
||||
assertEquals("0975", Decimalize.of("0FAB9CDEFFE7DCBA", 4));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLetterStandsForItsOwnValueLessTen() {
|
||||
assertEquals("0123", Decimalize.of("ABCDEF", 4));
|
||||
assertEquals("012345", Decimalize.of("ABCDEF", 6));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user