fix(paymentcryptography): a member the request carries as null is absent (#8975)

* fix(paymentcryptography): a member the request carries as null is absent
This commit is contained in:
Murtuza Chhil authored and GitHub committed 2026-11-19 11:25:17 -04:00
1 parent 030ba3d72b
commit c2721ca938
5 files changed
+213 -59

No files matched your search

@@ -9,7 +9,6 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HexFormat;
import java.util.LinkedHashMap;
import java.util.List;
@@ -23,6 +22,7 @@ import static io.floci.aws.services.paymentcryptography.PaymentCryptographyKeyRu
import static io.floci.aws.services.paymentcryptography.PaymentCryptographyKeyRules.requireFamily;
import static io.floci.aws.services.paymentcryptography.PaymentCryptographyKeyRules.requireMode;
import static io.floci.aws.services.paymentcryptography.PaymentCryptographyKeyRules.requireUsage;
import static io.floci.aws.services.paymentcryptography.PaymentCryptographyMembers.members;
import static io.floci.aws.services.paymentcryptography.PaymentCryptographyService.isAes;
import static io.floci.aws.services.paymentcryptography.PaymentCryptographyService.isTdes;
@@ -221,29 +221,6 @@ public class PaymentCryptographyCryptoHandler {
return service.translateKeyMaterial(region, members(request));
}
/** The members of a request, as the map the exchange reads. */
private static Map<String, Object> members(JsonNode node) {
Map<String, Object> out = new LinkedHashMap<>();
if (node == null || !node.isObject()) {
return out;
}
node.properties().forEach(entry -> out.put(entry.getKey(), member(entry.getValue())));
return out;
}
/** The value of a member, as the exchange reads it. */
private static Object member(JsonNode node) {
if (node.isObject()) {
return members(node);
}
if (node.isArray()) {
List<Object> values = new ArrayList<>();
node.forEach(value -> values.add(member(value)));
return values;
}
return node.asText();
}
/**
* The operation is not served, and says so.
*/
@@ -21,6 +21,8 @@ import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import static io.floci.aws.services.paymentcryptography.PaymentCryptographyMembers.members;
/**
* The Payment Cryptography control plane.
*
@@ -831,38 +833,6 @@ public class PaymentCryptographyJsonHandler {
return form;
}
/** The members of a form, as the service reads them. */
private static Map<String, Object> members(JsonNode node) {
Map<String, Object> out = new LinkedHashMap<>();
if (node == null || !node.isObject()) {
return out;
}
node.properties().forEach(entry -> out.put(entry.getKey(), value(entry.getValue())));
return out;
}
/** One member of a form, as the service reads it. */
private static Object value(JsonNode node) {
if (node == null || node.isNull()) {
return null;
}
if (node.isObject()) {
return members(node);
}
if (node.isArray()) {
List<Object> values = new ArrayList<>();
node.forEach(child -> values.add(value(child)));
return values;
}
if (node.isBoolean()) {
return node.asBoolean();
}
if (node.isNumber()) {
return node.asText();
}
return node.asText();
}
/**
* The key with the regions the request names added to its replication.
*/
@@ -0,0 +1,65 @@
package io.floci.aws.services.paymentcryptography;
import com.fasterxml.jackson.databind.JsonNode;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* How the members of a request are read.
*
* <p>The control plane's handler and the data plane's handler both turn the JSON of a
* request into the map the service reads, and the map is the same either way: the
* members of the request, in the order they came in, with an object a map of its own and
* a list a list of its own. Reading them is therefore read in one place.
*/
final class PaymentCryptographyMembers {
private PaymentCryptographyMembers() {
}
/**
* The members of a node, as the service reads them.
*
* @param node the object to read
* @return the members of the object, in the order they came in
*/
static Map<String, Object> members(JsonNode node) {
Map<String, Object> out = new LinkedHashMap<>();
if (node == null || !node.isObject()) {
return out;
}
node.properties().forEach(entry -> out.put(entry.getKey(), value(entry.getValue())));
return out;
}
/**
* One member of a node, as the service reads it.
*
* <p>A member the request carries as a null is a member the request does not carry,
* and not a member that carries the text {@code "null"}. A boolean is a boolean, and
* not its text.
*
* @param node the value to read
* @return the value, as the service reads it
*/
static Object value(JsonNode node) {
if (node == null || node.isNull()) {
return null;
}
if (node.isObject()) {
return members(node);
}
if (node.isArray()) {
List<Object> values = new ArrayList<>();
node.forEach(child -> values.add(value(child)));
return values;
}
if (node.isBoolean()) {
return node.asBoolean();
}
return node.asText();
}
}
@@ -1,6 +1,8 @@
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;
@@ -427,6 +429,27 @@ class PaymentCryptographyKeyExchangeTest {
refused.getMessage());
}
@Test
void aCheckValueAlgorithmThatIsNullIsAMemberTheRequestDoesNotCarry() {
// The request carries the member as a null, which is the member it does not carry,
// so the key is translated and it is not refused for a check value algorithm named
// "null".
Exchange exchange = agreedExchange();
Map<?, ?> wrapped = (Map<?, ?>) service.exportKey("us-east-1", exchange.key().getKeyId(),
"DiffieHellmanTr31KeyBlock", exchange.material(), "", "").get("WrappedKey");
PaymentCryptographyKey outgoing = createKey("AES_128", "TR31_K0_KEY_ENCRYPTION_KEY");
ObjectNode request = (ObjectNode) translateBody(exchange,
(String) wrapped.get("KeyMaterial"), outgoing);
request.putNull("KeyCheckValueAlgorithm");
Map<?, ?> translated = (Map<?, ?>) new PaymentCryptographyCryptoHandler(service)
.translateKeyMaterial(request, "us-east-1").get("WrappedKey");
assertEquals("TR31_KEY_BLOCK", translated.get("WrappedKeyMaterialFormat"));
assertEquals(cmacKeyCheckValue(exchange.key().getKeyMaterialHex()),
translated.get("KeyCheckValue"));
}
@Test
void aKeyIsTranslatedUnderTheKeyTheSampleDerives() throws Exception {
// The sample derives the one-time wrapping key with the Concat KDF of NIST SP
@@ -482,6 +505,32 @@ class PaymentCryptographyKeyExchangeTest {
return request;
}
/**
* The body of a request that translates a block that came in under the agreed key.
*
* <p>The identifiers are the ARNs the model asks for, and the request names no check
* value algorithm.
*
* @param exchange the exchange the two parties made
* @param block the block that comes in
* @param wrappingKey the key the block goes out under
* @return the body of the request
*/
private static JsonNode translateBody(Exchange exchange, String block,
PaymentCryptographyKey wrappingKey) {
Map<String, Object> material = new LinkedHashMap<>(exchange.material());
material.put("PrivateKeyIdentifier", exchange.agreement().getArn());
material.put("CertificateAuthorityPublicKeyIdentifier", exchange.authority().getArn());
material.put("WrappedKeyBlock", block);
ObjectMapper mapper = new ObjectMapper();
ObjectNode body = mapper.createObjectNode();
body.set("IncomingKeyMaterial", mapper.valueToTree(Map.of(
"DiffieHellmanTr31KeyBlock", material)));
body.set("OutgoingKeyMaterial", mapper.valueToTree(Map.of(
"Tr31KeyBlock", Map.of("WrappingKeyIdentifier", wrappingKey.getArn()))));
return body;
}
/**
* The material of the sample's agreement: a key derived with the Concat KDF of NIST
* SP 800-56A.
@@ -1516,11 +1565,20 @@ class PaymentCryptographyKeyExchangeTest {
material.put("DerivationData", Map.of(
"SharedInformation", "00112233445566778899AABBCCDDEEFF"));
return new Exchange(material,
createKey("AES_128", "TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY"));
createKey("AES_128", "TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY"),
agreement, authority);
}
/** The material of a key exchange, and the key that leaves under it. */
private record Exchange(Map<String, Object> material, PaymentCryptographyKey key) {
/**
* The material of a key exchange, and the key that leaves under it.
*
* @param material the material of the agreement
* @param key the key that leaves under it
* @param agreement the key the two parties agreed with
* @param authority the key that vouches for the counterparty's certificate
*/
private record Exchange(Map<String, Object> material, PaymentCryptographyKey key,
PaymentCryptographyKey agreement, PaymentCryptographyKey authority) {
}
@@ -0,0 +1,84 @@
package io.floci.aws.services.paymentcryptography;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* How the members of a request are read.
*
* <p>The control plane and the data plane read the members of a request the same way, so
* they read them through one converter, and the converter is what this holds to.
*/
class PaymentCryptographyMembersTest {
private final ObjectMapper mapper = new ObjectMapper();
@Test
void aMemberThatIsNullIsAMemberTheRequestDoesNotCarry() throws JsonProcessingException {
// The request carries the member as a null, which is the member it does not carry,
// and not a member that carries the text "null".
JsonNode request = mapper.readTree("""
{"KeyCheckValueAlgorithm": null}""");
Map<String, Object> members = PaymentCryptographyMembers.members(request);
assertTrue(members.containsKey("KeyCheckValueAlgorithm"));
assertNull(members.get("KeyCheckValueAlgorithm"));
}
@Test
void aBooleanIsABooleanAndNotItsText() throws JsonProcessingException {
JsonNode request = mapper.readTree("""
{"Enabled": true}""");
Map<String, Object> members = PaymentCryptographyMembers.members(request);
assertEquals(Boolean.TRUE, members.get("Enabled"));
}
@Test
void aNumberIsItsText() throws JsonProcessingException {
JsonNode request = mapper.readTree("""
{"KeySerialNumber": 42}""");
Map<String, Object> members = PaymentCryptographyMembers.members(request);
assertEquals("42", members.get("KeySerialNumber"));
}
@Test
void anObjectIsAMapOfItsOwn() throws JsonProcessingException {
JsonNode request = mapper.readTree("""
{"DerivationData": {"SharedInformation": "0011"}}""");
Map<String, Object> members = PaymentCryptographyMembers.members(request);
assertEquals(Map.of("SharedInformation", "0011"), members.get("DerivationData"));
}
@Test
void aListIsAListOfItsOwn() throws JsonProcessingException {
JsonNode request = mapper.readTree("""
{"ReplicationRegions": ["us-east-1", "eu-west-1"]}""");
Map<String, Object> members = PaymentCryptographyMembers.members(request);
assertEquals(List.of("us-east-1", "eu-west-1"), members.get("ReplicationRegions"));
}
@Test
void aNodeThatIsNotAnObjectHasNoMembers() {
assertTrue(PaymentCryptographyMembers.members(null).isEmpty());
assertTrue(PaymentCryptographyMembers.members(
mapper.getNodeFactory().textNode("not an object")).isEmpty());
}
}