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

@@ -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());
}
}