mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(paymentcryptography): the material of a key may be translated (#8941)
* feat(paymentcryptography): the material of a key may be translated * test(paymentcryptography): the sample's key agreement in the compatibility test * test(paymentcryptography): the wrapped key's format is the model's enum * fix(paymentcryptography): a verification reads the formats the reference verifies * docs(paymentcryptography): the deviations the reference's documentation shows * test(compatibility-tests): a verification of a format the reference does not read * fix(paymentcryptography): a translation holds to the rules the export and the import hold to * fix(paymentcryptography): a translation refuses a key whose usage its algorithm does not serve
This commit is contained in:
1 parent
d28bdf664b
commit
d2fde68865
16 files changed
+1246
-51
No files matched your search
+6
@@ -55,6 +55,12 @@ public class PaymentCryptographyCryptoController extends PaymentCryptographyData
|
||||
return respond(handler.reEncryptData(request, region(headers)));
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/keymaterial/translate")
|
||||
public Response translateKeyMaterial(String body, @Context HttpHeaders headers) {
|
||||
return respond(handler.translateKeyMaterial(parse(body), region(headers)));
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/mac/generate")
|
||||
public Response generateMac(String body, @Context HttpHeaders headers) {
|
||||
|
||||
+34
-3
@@ -9,6 +9,7 @@ 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;
|
||||
@@ -207,10 +208,40 @@ public class PaymentCryptographyCryptoHandler {
|
||||
|
||||
/** The cryptogram of an authorization request, generated. */
|
||||
|
||||
/** The material of a key translated into another form. */
|
||||
/**
|
||||
* The material of a key translated into another form.
|
||||
*
|
||||
* <p>The key material never leaves the service: the block comes in under the key the two
|
||||
* parties agreed with ECDH, it is opened there, and it is wrapped again under the key the
|
||||
* request names.
|
||||
*/
|
||||
public Map<String, Object> translateKeyMaterial(JsonNode request, String region) {
|
||||
refused("TranslateKeyMaterial");
|
||||
return Map.of();
|
||||
// The request is checked against the constraints the model states for it.
|
||||
PaymentCryptographyDataRequestShapes.check("TranslateKeyMaterial", request);
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+163
-21
@@ -248,15 +248,7 @@ final class PaymentCryptographyKeyExchange {
|
||||
private Exported exportTr31(String region, PaymentCryptographyKey key, byte[] clear,
|
||||
Map<String, Object> material) {
|
||||
String wrappingKeyIdentifier = text(material, "WrappingKeyIdentifier");
|
||||
PaymentCryptographyKey protection = usableKey(wrappingKeyIdentifier, region);
|
||||
if (!PaymentCryptographyService.isKeyEncryptionKey(protection.getKeyUsage())) {
|
||||
throw validation("the key " + protection.getArn() + " is not a key encryption key, so "
|
||||
+ "it cannot wrap a key block");
|
||||
}
|
||||
if (!PaymentCryptographyService.mayWrap(protection)) {
|
||||
throw validation("the key " + protection.getArn() + " does not have the mode of use "
|
||||
+ "Wrap, which it needs to wrap a key");
|
||||
}
|
||||
PaymentCryptographyKey protection = wrappingKey(wrappingKeyIdentifier, region);
|
||||
String tooWeak = PaymentCryptographyKeyStrength.refusal(
|
||||
protection.getKeyAlgorithm(), key.getKeyAlgorithm());
|
||||
if (tooWeak != null) {
|
||||
@@ -558,6 +550,144 @@ final class PaymentCryptographyKeyExchange {
|
||||
}
|
||||
|
||||
/** A key out of a block the two parties agreed with ECDH. */
|
||||
|
||||
/**
|
||||
* The material of a key that came under a key the two parties agreed with, and goes out
|
||||
* under another key.
|
||||
*
|
||||
* <p>The key material never leaves the service: the block comes in under the key the two
|
||||
* parties agreed with ECDH, it is opened there, and it is wrapped again under the key the
|
||||
* request names. The block that goes out carries the attributes of the key it holds, so
|
||||
* the key that is translated is the same key on the way out.
|
||||
*
|
||||
* @param region the region the request is in
|
||||
* @param material the request's members
|
||||
* @return the wrapped key, its check value and the format it is in
|
||||
*/
|
||||
Map<String, Object> translateKeyMaterial(String region, Map<String, Object> material) {
|
||||
Map<String, Object> incoming = object(material, "IncomingKeyMaterial");
|
||||
Map<String, Object> incomingBlock = object(incoming, "DiffieHellmanTr31KeyBlock");
|
||||
Map<String, Object> outgoing = object(material, "OutgoingKeyMaterial");
|
||||
Map<String, Object> outgoingBlock = object(outgoing, "Tr31KeyBlock");
|
||||
|
||||
String wrappingKeyIdentifier = text(outgoingBlock, "WrappingKeyIdentifier");
|
||||
PaymentCryptographyKey protection = wrappingKey(wrappingKeyIdentifier, region);
|
||||
|
||||
Agreed agreed = agreed(region, incomingBlock);
|
||||
String block = text(incomingBlock, "WrappedKeyBlock");
|
||||
byte[] clear;
|
||||
try {
|
||||
clear = PaymentCryptographyTr31.unwrap(block, agreed.derived());
|
||||
} catch (RuntimeException e) {
|
||||
throw validation("the key block did not open under the key the two parties agreed: "
|
||||
+ e.getMessage());
|
||||
}
|
||||
// The block that came in carries the attributes of the key it holds, and the block
|
||||
// that goes out carries the same ones. The usage and the mode of use are read
|
||||
// through the helpers the import reads them through, and they refuse a value this
|
||||
// service does not serve, so a block that names one is refused here rather than
|
||||
// answered with a block the import would refuse.
|
||||
String usage = PaymentCryptographyTr31.usageOf(block);
|
||||
String mode = PaymentCryptographyTr31.modeOf(block);
|
||||
String exportability = PaymentCryptographyTr31.exportabilityOf(block);
|
||||
String usageName = PaymentCryptographyService.usageName(usage);
|
||||
PaymentCryptographyService.modesFor(mode);
|
||||
if (!"E".equals(exportability) && !"N".equals(exportability) && !"S".equals(exportability)) {
|
||||
throw validation("the key block carries the exportability '" + exportability
|
||||
+ "', which is not one of E, N or S");
|
||||
}
|
||||
if ("N".equals(exportability)) {
|
||||
// The key in the block may not leave the service again, and the block that goes
|
||||
// out is handed to the caller, so a key that may not leave is refused.
|
||||
throw validation("the key block carries the exportability N, so the key in it may "
|
||||
+ "not leave the service and may not be translated out of it");
|
||||
}
|
||||
|
||||
PaymentCryptographyKeyAlgorithm algorithm = PaymentCryptographyKeyAlgorithm.from(
|
||||
PaymentCryptographyService.algorithmOf(block, clear.length));
|
||||
// The usage binds the key to its purpose, so a key whose algorithm does not serve
|
||||
// that purpose is not one the service makes. The rule is the one the import and the
|
||||
// creation enforce, and a block that breaks it is refused here rather than answered
|
||||
// with a block the import would refuse.
|
||||
String badAlgorithm = PaymentCryptographyKeyAlgorithm.refusal(usageName, algorithm);
|
||||
if (badAlgorithm != null) {
|
||||
throw validation(badAlgorithm);
|
||||
}
|
||||
// The agreed key opens the block that comes in, and the wrapping key closes the one
|
||||
// that goes out, so both have to be at least as strong as the key they protect. The
|
||||
// rule is the one the import and the export enforce.
|
||||
requireAgreedStrength(agreed, algorithm.name());
|
||||
String tooWeak = PaymentCryptographyKeyStrength.refusal(protection.getKeyAlgorithm(),
|
||||
algorithm.name());
|
||||
if (tooWeak != null) {
|
||||
throw validation(tooWeak);
|
||||
}
|
||||
requireCheckValueAlgorithm(material, algorithm);
|
||||
|
||||
String wrapped = PaymentCryptographyTr31.wrap(clear,
|
||||
PaymentCryptographyKeyRules.material(protection),
|
||||
PaymentCryptographyService.isAes(protection),
|
||||
PaymentCryptographyTr31.algorithmInHeader(block),
|
||||
usage,
|
||||
mode,
|
||||
exportability,
|
||||
PaymentCryptographyTr31.versionOf(block),
|
||||
PaymentCryptographyTr31.optionalBlocksOf(block));
|
||||
|
||||
Map<String, Object> out = new LinkedHashMap<>();
|
||||
out.put("WrappedKeyMaterial", wrapped);
|
||||
out.put("KeyCheckValue", PaymentCryptographyCrypto.checkValue(clear, algorithm));
|
||||
out.put("WrappedKeyMaterialFormat", "TR31_KEY_BLOCK");
|
||||
return Map.of("WrappedKey", out);
|
||||
}
|
||||
|
||||
/**
|
||||
* The check value algorithm a request names, and it has to be the one the key has.
|
||||
*
|
||||
* <p>An AES key's check value is a CMAC and a Triple DES key's is the ANSI X9.24 one,
|
||||
* so a request that names another one asks for a value the service does not compute,
|
||||
* and it is refused rather than answered with the value of another algorithm.
|
||||
*
|
||||
* @param material the request's members
|
||||
* @param algorithm the algorithm of the key that is translated
|
||||
*/
|
||||
private static void requireCheckValueAlgorithm(Map<String, Object> material,
|
||||
PaymentCryptographyKeyAlgorithm algorithm) {
|
||||
String wanted = text(material, "KeyCheckValueAlgorithm");
|
||||
if (wanted.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
String own = algorithm.checkValueAlgorithm();
|
||||
if (!own.equals(wanted)) {
|
||||
throw validation("the check value algorithm " + wanted + " is not the one a "
|
||||
+ algorithm.name() + " key has, which is " + own);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The strength of the key the two parties agreed, held against the key it protects.
|
||||
*
|
||||
* <p>The agreement is only as strong as the curve it was made on, so the weaker of the
|
||||
* derivation and the two curves decides, the way the export decides.
|
||||
*
|
||||
* @param agreed the agreed key
|
||||
* @param algorithm the algorithm of the key the agreement protects
|
||||
*/
|
||||
private static void requireAgreedStrength(Agreed agreed, String algorithm) {
|
||||
int derivedBits = Math.min(
|
||||
PaymentCryptographyKeyStrength.bitsOf(agreed.deriveAlgorithm().name()),
|
||||
Math.min(
|
||||
PaymentCryptographyKeyStrength.eccBitsOf(
|
||||
agreed.serviceKey().getKeyAlgorithm()),
|
||||
PaymentCryptographyKeyStrength.eccBitsOf((ECKey) agreed.counterpartyKey())));
|
||||
String tooWeak = PaymentCryptographyKeyStrength.refusal(derivedBits,
|
||||
PaymentCryptographyKeyStrength.bitsOf(algorithm),
|
||||
agreed.deriveAlgorithm().name(), algorithm);
|
||||
if (tooWeak != null) {
|
||||
throw validation(tooWeak);
|
||||
}
|
||||
}
|
||||
|
||||
private Imported importEcdh(String region, Map<String, Object> material) {
|
||||
String block = text(material, "WrappedKeyBlock");
|
||||
Agreed agreed = agreed(region, material);
|
||||
@@ -574,18 +704,7 @@ final class PaymentCryptographyKeyExchange {
|
||||
boolean exportable = exportableOnImport(PaymentCryptographyTr31.exportabilityOf(block));
|
||||
String algorithm = PaymentCryptographyService.algorithmOf(block, clear.length);
|
||||
// The derived key protects the imported one, so it has to be as strong as it.
|
||||
int derivedBits = Math.min(
|
||||
PaymentCryptographyKeyStrength.bitsOf(agreed.deriveAlgorithm().name()),
|
||||
Math.min(
|
||||
PaymentCryptographyKeyStrength.eccBitsOf(
|
||||
agreed.serviceKey().getKeyAlgorithm()),
|
||||
PaymentCryptographyKeyStrength.eccBitsOf((ECKey) agreed.counterpartyKey())));
|
||||
String tooWeak = PaymentCryptographyKeyStrength.refusal(derivedBits,
|
||||
PaymentCryptographyKeyStrength.bitsOf(algorithm),
|
||||
agreed.deriveAlgorithm().name(), algorithm);
|
||||
if (tooWeak != null) {
|
||||
throw validation(tooWeak);
|
||||
}
|
||||
requireAgreedStrength(agreed, algorithm);
|
||||
return new Imported(algorithm, PaymentCryptographyService.usageName(usage),
|
||||
PaymentCryptographyService.modesFor(mode),
|
||||
exportable, clear, null, null, null, "SYMMETRIC_KEY");
|
||||
@@ -924,6 +1043,29 @@ final class PaymentCryptographyKeyExchange {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The key a block is wrapped under, and it has to be one that may wrap.
|
||||
*
|
||||
* <p>The export and the translation both wrap a block under a key the request names, so
|
||||
* they insist on the same of it: a key encryption key that is allowed to wrap.
|
||||
*
|
||||
* @param identifier the key's identifier
|
||||
* @param region the region the key is in
|
||||
* @return the key the block is wrapped under
|
||||
*/
|
||||
private PaymentCryptographyKey wrappingKey(String identifier, String region) {
|
||||
PaymentCryptographyKey protection = usableKey(identifier, region);
|
||||
if (!PaymentCryptographyService.isKeyEncryptionKey(protection.getKeyUsage())) {
|
||||
throw validation("the key " + protection.getArn() + " is not a key encryption key, so "
|
||||
+ "it cannot wrap a key block");
|
||||
}
|
||||
if (!PaymentCryptographyService.mayWrap(protection)) {
|
||||
throw validation("the key " + protection.getArn() + " does not have the mode of use "
|
||||
+ "Wrap, which it needs to wrap a key");
|
||||
}
|
||||
return protection;
|
||||
}
|
||||
|
||||
/**
|
||||
* The key an identifier names, and it has to be one the service does work with.
|
||||
*
|
||||
|
||||
+22
-1
@@ -105,7 +105,7 @@ public class PaymentCryptographyPinOperations {
|
||||
String encryptionKeyIdentifier = text(request, "EncryptionKeyIdentifier");
|
||||
String verificationKeyIdentifier = text(request, "VerificationKeyIdentifier");
|
||||
refuseWrappedKey(request, "EncryptionWrappedKey");
|
||||
PinBlockFormat format = format(text(request, "PinBlockFormat"));
|
||||
PinBlockFormat format = verificationFormat(text(request, "PinBlockFormat"));
|
||||
String pan = requirePan(text(request, "PrimaryAccountNumber"),
|
||||
"PrimaryAccountNumber is required for Visa PVV verification");
|
||||
requireVisaPinLength(request);
|
||||
@@ -516,6 +516,27 @@ public class PaymentCryptographyPinOperations {
|
||||
return format;
|
||||
}
|
||||
|
||||
/**
|
||||
* The PIN block format a verification reads.
|
||||
*
|
||||
* <p>The reference names the formats it supports for this operation: "AWS Payment
|
||||
* Cryptography supports {@code ISO_Format_0} and {@code ISO_Format_3}". The format 4
|
||||
* block is an AES block, and the reference's verification reads the Triple DES blocks, so
|
||||
* a request that names the format 4 is refused rather than answered as if the reference
|
||||
* read it.
|
||||
*
|
||||
* @param wireName the name the request carries
|
||||
* @return the format the block is in
|
||||
*/
|
||||
private static PinBlockFormat verificationFormat(String wireName) {
|
||||
PinBlockFormat format = PinBlockFormat.from(wireName);
|
||||
if (format != PinBlockFormat.ISO_FORMAT_0 && format != PinBlockFormat.ISO_FORMAT_3) {
|
||||
throw validation("PinBlockFormat '" + wireName + "' is not a format the reference "
|
||||
+ "verifies (ISO_FORMAT_0, ISO_FORMAT_3)");
|
||||
}
|
||||
return format;
|
||||
}
|
||||
|
||||
/**
|
||||
* The verification key index the attributes name.
|
||||
*
|
||||
|
||||
+12
@@ -1570,6 +1570,18 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
|
||||
return exchange.publicKeyCertificate(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* The material of a key that came under a key the two parties agreed with, and goes out
|
||||
* under another key.
|
||||
*
|
||||
* @param region the region the request is in
|
||||
* @param material the request's members
|
||||
* @return the wrapped key, its check value and the format it is in
|
||||
*/
|
||||
public Map<String, Object> translateKeyMaterial(String region, Map<String, Object> material) {
|
||||
return exchange.translateKeyMaterial(region, material);
|
||||
}
|
||||
|
||||
/** The parameters a counterparty needs to import a key into this service. */
|
||||
public Map<String, Object> parametersForImport(String materialType, String algorithm,
|
||||
boolean reuse) {
|
||||
|
||||
@@ -271,6 +271,74 @@ final class PaymentCryptographyTr31 {
|
||||
return block.substring(5, 7);
|
||||
}
|
||||
|
||||
/**
|
||||
* The version of the key a block carries, over two characters.
|
||||
*
|
||||
* <p>The version is in the header, so it is read from there and not carried separately.
|
||||
*/
|
||||
static String versionOf(String block) {
|
||||
return block.substring(9, 11);
|
||||
}
|
||||
|
||||
/**
|
||||
* The optional blocks a block carries, each its identifier and its value.
|
||||
*
|
||||
* <p>The padding block is not one of them: it is the block that keeps the optional
|
||||
* blocks aligned, and the block that goes out is padded for its own protection, so the
|
||||
* padding of the block that came in is dropped.
|
||||
*
|
||||
* @param block the block
|
||||
* @return the optional blocks, without the padding block
|
||||
*/
|
||||
static List<String[]> optionalBlocksOf(String block) {
|
||||
int count = parseInt(block.substring(12, 14), "the number of optional blocks");
|
||||
List<String[]> blocks = new ArrayList<>();
|
||||
int at = HEADER_LENGTH;
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (block.length() < at + 4) {
|
||||
throw new IllegalArgumentException("the key block says it has optional blocks, and "
|
||||
+ "it is too short to hold them");
|
||||
}
|
||||
String id = block.substring(at, at + 2);
|
||||
int length = parseInt(block.substring(at + 2, at + 4),
|
||||
"the length of an optional block", 16);
|
||||
int valueStart;
|
||||
int valueLength;
|
||||
int total;
|
||||
if (length != 0) {
|
||||
valueStart = at + 4;
|
||||
valueLength = length - 4;
|
||||
total = length;
|
||||
} else {
|
||||
// The length did not fit in its field, so the field says so and the length
|
||||
// follows the number of characters it takes.
|
||||
if (block.length() < at + 6) {
|
||||
throw new IllegalArgumentException("the key block says it has optional blocks, "
|
||||
+ "and it is too short to hold them");
|
||||
}
|
||||
int digits = parseInt(block.substring(at + 4, at + 6),
|
||||
"the length of the length of an optional block", 16);
|
||||
if (digits == 0 || block.length() < at + 6 + digits) {
|
||||
throw new IllegalArgumentException("the key block says it has optional blocks, "
|
||||
+ "and it is too short to hold them");
|
||||
}
|
||||
valueLength = parseInt(block.substring(at + 6, at + 6 + digits),
|
||||
"the length of an optional block", 16);
|
||||
valueStart = at + 6 + digits;
|
||||
total = 6 + digits + valueLength;
|
||||
}
|
||||
if (valueLength < 0 || block.length() < at + total) {
|
||||
throw new IllegalArgumentException("the key block says it has optional blocks, and "
|
||||
+ "it is too short to hold them");
|
||||
}
|
||||
if (!"PB".equals(id)) {
|
||||
blocks.add(new String[] {id, block.substring(valueStart, valueStart + valueLength)});
|
||||
}
|
||||
at += total;
|
||||
}
|
||||
return blocks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a block is a type D one, which is derived with AES.
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user