mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(paymentcryptography): the AS2805 KEK validation (#9065)
GenerateAs2805KekValidation is served on the Payment Cryptography data plane. Two AS2805 nodes prove they hold the same key encryption key: the request form makes a random number with Single DES odd parity and returns it enciphered under one variant of the key, and the response form deciphers a number, inverts it and returns it enciphered under another variant. The key is a Triple DES TR31_K0 key encryption key; the request form needs Encrypt and the response form both Decrypt and Encrypt. The AS2805 Triple DES check and the key lookup now live in PaymentCryptographyKeyRules, shared with the key exchange, card validation, authorization cryptogram and PIN operations.
This commit is contained in:
1 parent
2402267966
commit
017e635edf
13 files changed
+846
-70
No files matched your search
+2
-11
@@ -101,7 +101,7 @@ public class PaymentCryptographyAuthCryptogramOperations {
|
||||
text(request, "TransactionData")));
|
||||
SessionKey sk = resolveDerivation(request);
|
||||
|
||||
PaymentCryptographyKey key = requiredKey(keyIdentifier, region);
|
||||
PaymentCryptographyKey key = PaymentCryptographyKeyRules.requiredKey(service, keyIdentifier, region);
|
||||
PaymentCryptographyKeyRules.requireUsage(key,
|
||||
List.of("TR31_E0_EMV_MKEY_APP_CRYPTOGRAMS"), "computing a cryptogram");
|
||||
PaymentCryptographyKeyRules.requireAlgorithm(key, "TDES_2KEY", "computing a cryptogram");
|
||||
@@ -169,7 +169,7 @@ public class PaymentCryptographyAuthCryptogramOperations {
|
||||
}
|
||||
}
|
||||
|
||||
PaymentCryptographyKey key = requiredKey(keyIdentifier, region);
|
||||
PaymentCryptographyKey key = PaymentCryptographyKeyRules.requiredKey(service, keyIdentifier, region);
|
||||
PaymentCryptographyKeyRules.requireUsage(key,
|
||||
List.of("TR31_E0_EMV_MKEY_APP_CRYPTOGRAMS"), "computing a cryptogram");
|
||||
PaymentCryptographyKeyRules.requireAlgorithm(key, "TDES_2KEY", "computing a cryptogram");
|
||||
@@ -307,15 +307,6 @@ public class PaymentCryptographyAuthCryptogramOperations {
|
||||
|
||||
// ---- the helpers ---------------------------------------------------------
|
||||
|
||||
private PaymentCryptographyKey requiredKey(String identifier, String region) {
|
||||
PaymentCryptographyKey key = service.findKey(region, identifier);
|
||||
if (key == null) {
|
||||
throw new AwsException("ResourceNotFoundException", "Key not found: " + identifier, 404);
|
||||
}
|
||||
PaymentCryptographyKeyRules.requireEnabled(key);
|
||||
return key;
|
||||
}
|
||||
|
||||
private static AwsException validation(String message) {
|
||||
return new AwsException("ValidationException", message, 400);
|
||||
}
|
||||
|
||||
+2
-11
@@ -72,7 +72,7 @@ public class PaymentCryptographyCardValidationOperations {
|
||||
: validationDataLength;
|
||||
CardAttrs attrs = resolveCardAttrs(request.get("GenerationAttributes"));
|
||||
|
||||
PaymentCryptographyKey key = requiredKey(keyIdentifier, region);
|
||||
PaymentCryptographyKey key = PaymentCryptographyKeyRules.requiredKey(service, keyIdentifier, region);
|
||||
requireKeyForVariant(key, attrs.variant, "Generate");
|
||||
|
||||
Map<String, Object> out = new LinkedHashMap<>();
|
||||
@@ -95,7 +95,7 @@ public class PaymentCryptographyCardValidationOperations {
|
||||
int length = validationData.length();
|
||||
CardAttrs attrs = resolveCardAttrs(request.get("VerificationAttributes"));
|
||||
|
||||
PaymentCryptographyKey key = requiredKey(keyIdentifier, region);
|
||||
PaymentCryptographyKey key = PaymentCryptographyKeyRules.requiredKey(service, keyIdentifier, region);
|
||||
requireKeyForVariant(key, attrs.variant, "Verify");
|
||||
|
||||
String computed = computeValue(pan, attrs, key, length);
|
||||
@@ -224,15 +224,6 @@ public class PaymentCryptographyCardValidationOperations {
|
||||
}
|
||||
}
|
||||
|
||||
private PaymentCryptographyKey requiredKey(String identifier, String region) {
|
||||
PaymentCryptographyKey key = service.findKey(region, identifier);
|
||||
if (key == null) {
|
||||
throw new AwsException("ResourceNotFoundException", "Key not found: " + identifier, 404);
|
||||
}
|
||||
PaymentCryptographyKeyRules.requireEnabled(key);
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* The error a card request is refused with.
|
||||
*/
|
||||
|
||||
+175
@@ -0,0 +1,175 @@
|
||||
package io.floci.aws.services.paymentcryptography;
|
||||
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.services.paymentcryptography.crypto.BlockMode;
|
||||
import io.floci.aws.services.paymentcryptography.crypto.Ciphers;
|
||||
import io.floci.aws.services.paymentcryptography.crypto.EmvCrypto;
|
||||
|
||||
/**
|
||||
* The AS2805 KEK validation: the ceremony two nodes use to prove they hold the same key.
|
||||
*
|
||||
* <p>The node that starts the exchange enciphers a random number under a variant of the key
|
||||
* it holds and sends it. The node it talks to deciphers that number, inverts every bit of
|
||||
* it, and enciphers the inverted value under another variant of its own key. The first node
|
||||
* compares the answer with the one it computed, and the two agree only when both hold the
|
||||
* same key.
|
||||
*
|
||||
* <p>A variant is the key exclusive-ored with a constant, tiled over the key's length: the
|
||||
* constant says what the variant is for, so a key made for one purpose does not open under
|
||||
* the variant of another. The reference names the constant of the random number's variant
|
||||
* 82 and that of the inverted number's 84, and the longer form of the first 82C0.
|
||||
*
|
||||
* <p>The random number is eight, sixteen or twenty-four bytes, and every byte of it carries
|
||||
* the odd parity the standard's Single DES asks for. It is enciphered a block at a time
|
||||
* under the variant, in Triple DES, the cipher AS2805's cryptography is made of.
|
||||
*/
|
||||
final class PaymentCryptographyKekValidation {
|
||||
|
||||
/** The block of the cipher the ceremony runs in: the Triple DES block of eight bytes. */
|
||||
private static final int BLOCK = 8;
|
||||
|
||||
/**
|
||||
* The variant masks a request may name, and the mask of the inverted number's variant.
|
||||
*/
|
||||
enum Variant {
|
||||
|
||||
/** The random number is enciphered under the 82 variant, the inverted one under 84. */
|
||||
VARIANT_MASK_82("82", "84"),
|
||||
|
||||
/** The random number is enciphered under the 82C0 variant, the inverted one under 84C0. */
|
||||
VARIANT_MASK_82C0("82C0", "84C0");
|
||||
|
||||
private final String sendMask;
|
||||
private final String receiveMask;
|
||||
|
||||
Variant(String sendMask, String receiveMask) {
|
||||
this.sendMask = sendMask;
|
||||
this.receiveMask = receiveMask;
|
||||
}
|
||||
|
||||
/** The mask of the random number's variant. */
|
||||
String sendMask() {
|
||||
return sendMask;
|
||||
}
|
||||
|
||||
/** The mask of the inverted number's variant. */
|
||||
String receiveMask() {
|
||||
return receiveMask;
|
||||
}
|
||||
|
||||
/**
|
||||
* The variant a request names.
|
||||
*
|
||||
* <p>The request's mask is one of the model's two, and the shape check settles it
|
||||
* before the operation reads it, so a name that is not one of them is a fault of the
|
||||
* service rather than of the caller.
|
||||
*
|
||||
* @param name the name of the mask
|
||||
* @return the variant
|
||||
*/
|
||||
static Variant from(String name) {
|
||||
for (Variant variant : values()) {
|
||||
if (variant.name().equals(name)) {
|
||||
return variant;
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("the variant mask " + name + " is not one of them");
|
||||
}
|
||||
}
|
||||
|
||||
private PaymentCryptographyKekValidation() {
|
||||
}
|
||||
|
||||
/**
|
||||
* The variant of a key: the key exclusive-ored with the mask, tiled over its length.
|
||||
*
|
||||
* <p>The mask is a constant the standard gives for the purpose, and it is repeated over
|
||||
* the whole key, so a key of any length gets a variant of the same length.
|
||||
*
|
||||
* @param key the key the variant is of
|
||||
* @param mask the mask, as hexadecimal
|
||||
* @return the variant key
|
||||
*/
|
||||
static byte[] variantKey(byte[] key, String mask) {
|
||||
byte[] constant = PaymentCryptographyCrypto.fromHex(mask);
|
||||
byte[] out = new byte[key.length];
|
||||
for (int i = 0; i < key.length; i++) {
|
||||
out[i] = (byte) (key[i] ^ constant[i % constant.length]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* The odd parity every byte of the random number carries.
|
||||
*
|
||||
* <p>The standard's Single DES reads a key whose bytes each hold an odd number of bits,
|
||||
* so the random number is put to that parity before it is enciphered. The rule is the
|
||||
* service's own DES parity rule, so there is one of it: a byte whose lowest bit is
|
||||
* already set and whose count is even is corrected, not left as it is.
|
||||
*
|
||||
* @param data the random number
|
||||
* @return the random number, with the parity put to it
|
||||
*/
|
||||
static byte[] oddParity(byte[] data) {
|
||||
return EmvCrypto.adjustDesParity(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* The inversion of every byte: the number the node answers with.
|
||||
*
|
||||
* @param data the number to invert
|
||||
* @return the inverted number
|
||||
*/
|
||||
static byte[] invert(byte[] data) {
|
||||
byte[] out = new byte[data.length];
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
out[i] = (byte) ~data[i];
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* The enciphering of the number under a variant, a block at a time.
|
||||
*
|
||||
* <p>The cipher is Triple DES, the one AS2805's cryptography is made of, and it is the
|
||||
* cipher's own ECB mode: every block stands alone, so the number is enciphered without
|
||||
* a vector and without a chain.
|
||||
*
|
||||
* @param data the number to encipher
|
||||
* @param variantKey the variant key
|
||||
* @return the enciphered number
|
||||
*/
|
||||
static byte[] encrypt(byte[] data, byte[] variantKey) {
|
||||
requireWholeBlocks(data);
|
||||
return Ciphers.tdesEncrypt(data, variantKey, null, BlockMode.ECB);
|
||||
}
|
||||
|
||||
/**
|
||||
* The deciphering of the number under a variant, a block at a time.
|
||||
*
|
||||
* @param data the enciphered number
|
||||
* @param variantKey the variant key
|
||||
* @return the number
|
||||
*/
|
||||
static byte[] decrypt(byte[] data, byte[] variantKey) {
|
||||
requireWholeBlocks(data);
|
||||
return Ciphers.tdesDecrypt(data, variantKey, null, BlockMode.ECB);
|
||||
}
|
||||
|
||||
/**
|
||||
* The number fills whole Triple DES blocks.
|
||||
*
|
||||
* <p>The block is the Triple DES block of eight bytes, and the lengths the model names
|
||||
* for the random number are whole numbers of them, so this is a guard on the cipher
|
||||
* rather than a refusal the model settles.
|
||||
*
|
||||
* @param data the number
|
||||
*/
|
||||
private static void requireWholeBlocks(byte[] data) {
|
||||
if (data.length % BLOCK != 0) {
|
||||
throw new AwsException("ValidationException",
|
||||
"the number is " + data.length + " bytes; it is a whole number of "
|
||||
+ BLOCK + "-byte blocks", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package io.floci.aws.services.paymentcryptography;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.POST;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.core.Context;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
/**
|
||||
* The data plane's AS2805 KEK validation.
|
||||
*
|
||||
* <p>The route is the one the AWS SDK builds, and the key the validation is made with is in
|
||||
* the body, because the reference's operation takes no URI parameter.
|
||||
*/
|
||||
@Path(PaymentCryptographyDataPlaneRouteFilter.INTERNAL_PREFIX)
|
||||
public class PaymentCryptographyKekValidationController extends PaymentCryptographyDataPlaneController {
|
||||
|
||||
private final PaymentCryptographyKekValidationOperations operations;
|
||||
|
||||
@Inject
|
||||
public PaymentCryptographyKekValidationController(PaymentCryptographyKekValidationOperations operations,
|
||||
ObjectMapper objectMapper,
|
||||
RegionResolver regionResolver) {
|
||||
super(objectMapper, regionResolver);
|
||||
this.operations = operations;
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/as2805kekvalidation/generate")
|
||||
public Response generateAs2805KekValidation(String body, @Context HttpHeaders headers) {
|
||||
return respond(operations.generate(parse(body), region(headers)));
|
||||
}
|
||||
}
|
||||
+203
@@ -0,0 +1,203 @@
|
||||
package io.floci.aws.services.paymentcryptography;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.services.paymentcryptography.model.PaymentCryptographyKey;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.HexFormat;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.floci.aws.services.paymentcryptography.PaymentCryptographyAttributes.soleVariant;
|
||||
import static io.floci.aws.services.paymentcryptography.PaymentCryptographyAttributes.text;
|
||||
|
||||
/**
|
||||
* The AS2805 KEK validation: the ceremony two nodes use to prove they hold the same key.
|
||||
*
|
||||
* <p>The operation has two forms. The request form is the node that starts the exchange: it
|
||||
* makes a random number, enciphers it under a variant of the key it holds, and answers with
|
||||
* that enciphered number and the answer it expects the other node to send back. The response
|
||||
* form is the node that answers: it deciphers the number it was given, inverts it and
|
||||
* enciphers the inverted value, which is the answer the first node compares.
|
||||
*
|
||||
* <p>The key is a Triple DES key encryption key, the one AS2805 calls the terminal major
|
||||
* key. The reference does not name the usage the key must have for this operation, so the
|
||||
* one the service's own AS2805 import gives that key is the one it asks for, and AS2805's
|
||||
* cryptography is all TDES, so a key of another family is refused.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class PaymentCryptographyKekValidationOperations {
|
||||
|
||||
/** The usages a key may have to be the key of a KEK validation. */
|
||||
private static final List<String> KEK_USAGES = List.of("TR31_K0_KEY_ENCRYPTION_KEY");
|
||||
|
||||
/** The length of the random number when the request does not name one. */
|
||||
private static final int DEFAULT_RANDOM_LENGTH = 16;
|
||||
|
||||
/** The lengths the request may name, in bytes. */
|
||||
private static final Map<String, Integer> RANDOM_LENGTHS = Map.of(
|
||||
"BYTES_8", 8,
|
||||
"BYTES_16", 16,
|
||||
"BYTES_24", 24);
|
||||
|
||||
/** The hexadecimal the data plane carries its blobs in. */
|
||||
private static final HexFormat HEX = HexFormat.of().withUpperCase();
|
||||
|
||||
private final SecureRandom random;
|
||||
|
||||
private final PaymentCryptographyService service;
|
||||
|
||||
@Inject
|
||||
public PaymentCryptographyKekValidationOperations(PaymentCryptographyService service) {
|
||||
this(service, new SecureRandom());
|
||||
}
|
||||
|
||||
PaymentCryptographyKekValidationOperations(PaymentCryptographyService service, SecureRandom random) {
|
||||
this.service = service;
|
||||
this.random = random;
|
||||
}
|
||||
|
||||
/**
|
||||
* The KEK validation of a key encryption key.
|
||||
*/
|
||||
Map<String, Object> generate(JsonNode request, String region) {
|
||||
// The request is checked against the constraints the model states for it.
|
||||
PaymentCryptographyDataRequestShapes.check("GenerateAs2805KekValidation", request);
|
||||
PaymentCryptographyKekValidation.Variant variant =
|
||||
variant(text(request, "RandomKeySendVariantMask"));
|
||||
JsonNode type = request.get("KekValidationType");
|
||||
String form = soleVariant(type);
|
||||
if (form == null) {
|
||||
throw validation("KekValidationType must specify exactly one variant");
|
||||
}
|
||||
if (!"KekValidationRequest".equals(form) && !"KekValidationResponse".equals(form)) {
|
||||
throw validation("KekValidationType." + form + " not supported by this service "
|
||||
+ "(KekValidationRequest, KekValidationResponse)");
|
||||
}
|
||||
|
||||
PaymentCryptographyKey key = requiredKey(text(request, "KeyIdentifier"), region);
|
||||
PaymentCryptographyKeyAlgorithm algorithm = algorithmOf(key);
|
||||
byte[] material = PaymentCryptographyKeyRules.material(key);
|
||||
byte[] variantRn = PaymentCryptographyKekValidation.variantKey(
|
||||
material, variant.sendMask());
|
||||
byte[] variantNotRn = PaymentCryptographyKekValidation.variantKey(
|
||||
material, variant.receiveMask());
|
||||
|
||||
Map<String, Object> out = new LinkedHashMap<>();
|
||||
out.put("KeyArn", key.getArn());
|
||||
out.put("KeyCheckValue", PaymentCryptographyCrypto.checkValue(material, algorithm));
|
||||
|
||||
byte[] send;
|
||||
byte[] receive;
|
||||
if ("KekValidationRequest".equals(form)) {
|
||||
// The node that starts the exchange enciphers under the key.
|
||||
PaymentCryptographyKeyRules.requireMode(key, "Encrypt",
|
||||
"enciphering a random number for a KEK validation");
|
||||
JsonNode attributes = type.get("KekValidationRequest");
|
||||
requireAlgorithm(key, algorithm, text(attributes, "DeriveKeyAlgorithm"));
|
||||
byte[] rn = PaymentCryptographyKekValidation.oddParity(
|
||||
randomBytes(randomLength(text(attributes, "RandomKeyMaxLength"))));
|
||||
send = PaymentCryptographyKekValidation.encrypt(rn, variantRn);
|
||||
receive = PaymentCryptographyKekValidation.encrypt(
|
||||
PaymentCryptographyKekValidation.invert(rn), variantNotRn);
|
||||
} else {
|
||||
// The node that answers deciphers the number and enciphers the inverted one.
|
||||
PaymentCryptographyKeyRules.requireMode(key, "Decrypt",
|
||||
"deciphering a random number for a KEK validation");
|
||||
PaymentCryptographyKeyRules.requireMode(key, "Encrypt",
|
||||
"enciphering the inverted number for a KEK validation");
|
||||
send = HEX.parseHex(text(type.get("KekValidationResponse"), "RandomKeySend"));
|
||||
byte[] rn = PaymentCryptographyKekValidation.decrypt(send, variantRn);
|
||||
receive = PaymentCryptographyKekValidation.encrypt(
|
||||
PaymentCryptographyKekValidation.invert(rn), variantNotRn);
|
||||
}
|
||||
out.put("RandomKeySend", HEX.formatHex(send));
|
||||
out.put("RandomKeyReceive", HEX.formatHex(receive));
|
||||
return out;
|
||||
}
|
||||
|
||||
// ---- the parts -----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The variant mask the request names.
|
||||
*
|
||||
* <p>The mask is one of the model's two, and the shape check settles it before the
|
||||
* operation reads it, so a name that is not one of them is a fault of the service rather
|
||||
* than of the caller.
|
||||
*/
|
||||
private static PaymentCryptographyKekValidation.Variant variant(String name) {
|
||||
return PaymentCryptographyKekValidation.Variant.from(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* The length of the random number the request names.
|
||||
*
|
||||
* <p>The length is one of the model's three, and the shape check settles it before the
|
||||
* operation reads it. A request that names none gets the length the reference's own
|
||||
* example answers with.
|
||||
*/
|
||||
private static int randomLength(String name) {
|
||||
if (name.isEmpty()) {
|
||||
return DEFAULT_RANDOM_LENGTH;
|
||||
}
|
||||
Integer length = RANDOM_LENGTHS.get(name);
|
||||
if (length == null) {
|
||||
throw new IllegalStateException("the random key length " + name + " is not one of them");
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
/**
|
||||
* The algorithm of the key, which is the algorithm the variant is used with.
|
||||
*
|
||||
* <p>AS2805's cryptography is all TDES: the ceremony runs over Single DES blocks and
|
||||
* the key variants are DES key variants, so a key of another family is refused.
|
||||
*/
|
||||
private static PaymentCryptographyKeyAlgorithm algorithmOf(PaymentCryptographyKey key) {
|
||||
return PaymentCryptographyKeyRules.requireTdes(key.getKeyAlgorithm(),
|
||||
"key " + key.getArn());
|
||||
}
|
||||
|
||||
/**
|
||||
* The algorithm the request says the derived key is of, which is the key's own.
|
||||
*
|
||||
* <p>The derived key is the variant of the key the request names, so it is of the same
|
||||
* algorithm and the same length. A request that names another one is a request for a
|
||||
* key the service does not hold.
|
||||
*/
|
||||
private static void requireAlgorithm(PaymentCryptographyKey key,
|
||||
PaymentCryptographyKeyAlgorithm algorithm,
|
||||
String declared) {
|
||||
if (declared.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (!declared.equals(algorithm.name())) {
|
||||
throw validation("DeriveKeyAlgorithm '" + declared + "' is not the algorithm of the "
|
||||
+ "key " + key.getArn() + ", which is " + algorithm.name());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The random number, of the length the request names.
|
||||
*/
|
||||
private byte[] randomBytes(int length) {
|
||||
byte[] out = new byte[length];
|
||||
random.nextBytes(out);
|
||||
return out;
|
||||
}
|
||||
|
||||
private PaymentCryptographyKey requiredKey(String identifier, String region) {
|
||||
PaymentCryptographyKey key =
|
||||
PaymentCryptographyKeyRules.requiredKey(service, identifier, region);
|
||||
PaymentCryptographyKeyRules.requireUsage(key, KEK_USAGES, "validating a key encryption key");
|
||||
return key;
|
||||
}
|
||||
|
||||
private static AwsException validation(String message) {
|
||||
return new AwsException("ValidationException", message, 400);
|
||||
}
|
||||
}
|
||||
+5
-22
@@ -941,24 +941,6 @@ final class PaymentCryptographyKeyExchange {
|
||||
return value instanceof Map<?, ?> map ? (Map<String, Object>) map : Map.of();
|
||||
}
|
||||
|
||||
/**
|
||||
* Refuses a key that is not TDES, and returns its algorithm: AS2805
|
||||
* cryptography is all TDES.
|
||||
*
|
||||
* @param algorithmName the key's algorithm
|
||||
* @param role what the key is, for the refusal
|
||||
* @return the algorithm
|
||||
*/
|
||||
private static PaymentCryptographyKeyAlgorithm requireTdes(String algorithmName, String role) {
|
||||
PaymentCryptographyKeyAlgorithm algorithm =
|
||||
PaymentCryptographyKeyAlgorithm.from(algorithmName);
|
||||
if (algorithm == null || !algorithm.isTdes()) {
|
||||
throw validation("an AS2805 key is a TDES key, but the " + role + " is "
|
||||
+ algorithmName);
|
||||
}
|
||||
return algorithm;
|
||||
}
|
||||
|
||||
/**
|
||||
* The parameter a token names, when it is one this service issued for that kind of
|
||||
* material and it has not expired.
|
||||
@@ -1447,11 +1429,11 @@ final class PaymentCryptographyKeyExchange {
|
||||
+ "which it needs to wrap a key cryptogram");
|
||||
}
|
||||
requireKeyEncryptionKey(kek);
|
||||
requireTdes(kek.getKeyAlgorithm(), "AS2805 wrapping key " + kek.getArn());
|
||||
PaymentCryptographyKeyRules.requireTdes(kek.getKeyAlgorithm(), "AS2805 wrapping key " + kek.getArn());
|
||||
// AS2805's cryptography is all TDES, so only TDES keys leave and come back in
|
||||
// an AS2805 cryptogram.
|
||||
PaymentCryptographyKeyAlgorithm algorithm =
|
||||
requireTdes(key.getKeyAlgorithm(), "key " + key.getArn());
|
||||
PaymentCryptographyKeyRules.requireTdes(key.getKeyAlgorithm(), "key " + key.getArn());
|
||||
String tooWeak = PaymentCryptographyKeyStrength.refusal(kek.getKeyAlgorithm(),
|
||||
key.getKeyAlgorithm());
|
||||
if (tooWeak != null) {
|
||||
@@ -1581,7 +1563,7 @@ final class PaymentCryptographyKeyExchange {
|
||||
String wrappingKeyIdentifier = text(material, "WrappingKeyIdentifier");
|
||||
PaymentCryptographyKey kek = usableKey(wrappingKeyIdentifier, region);
|
||||
requireKeyEncryptionKey(kek);
|
||||
requireTdes(kek.getKeyAlgorithm(), "AS2805 wrapping key " + kek.getArn());
|
||||
PaymentCryptographyKeyRules.requireTdes(kek.getKeyAlgorithm(), "AS2805 wrapping key " + kek.getArn());
|
||||
// The wrapping key opens an incoming cryptogram, so it has to be allowed to unwrap.
|
||||
// The model names no error code of its own for this, so it is a validation error,
|
||||
// as the rest of the refusals about a key's modes of use are.
|
||||
@@ -1593,7 +1575,8 @@ final class PaymentCryptographyKeyExchange {
|
||||
String algorithmName = text(material, "KeyAlgorithm");
|
||||
// AS2805's cryptography is all TDES, so only TDES keys leave and come back in
|
||||
// an AS2805 cryptogram.
|
||||
PaymentCryptographyKeyAlgorithm algorithm = requireTdes(algorithmName, "carried key");
|
||||
PaymentCryptographyKeyAlgorithm algorithm =
|
||||
PaymentCryptographyKeyRules.requireTdes(algorithmName, "carried key");
|
||||
// The wrapping key has to be at least as strong as the key it protects, the same
|
||||
// rule the export enforces, so a cryptogram that the export would refuse to write
|
||||
// does not open here either.
|
||||
|
||||
+44
@@ -126,6 +126,50 @@ final class PaymentCryptographyKeyRules {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The key a request names, and the rules it has to satisfy before it does any work.
|
||||
*
|
||||
* <p>The key is the one the request names in the region the request is for, and it has
|
||||
* to be there and enabled: a key the service does not hold is the not-found error, and
|
||||
* a key that is disabled does no work.
|
||||
*
|
||||
* @param service the service the key is held by
|
||||
* @param identifier the key the request names
|
||||
* @param region the region the request is for
|
||||
* @return the key
|
||||
*/
|
||||
static PaymentCryptographyKey requiredKey(PaymentCryptographyService service, String identifier,
|
||||
String region) {
|
||||
PaymentCryptographyKey key = service.findKey(region, identifier);
|
||||
if (key == null) {
|
||||
throw new AwsException("ResourceNotFoundException", "Key not found: " + identifier, 404);
|
||||
}
|
||||
requireEnabled(key);
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* The Triple DES algorithm an AS2805 key has to have.
|
||||
*
|
||||
* <p>AS2805's cryptography is all TDES: the ceremony runs over Single DES blocks, the
|
||||
* key variants are DES key variants, and the random numbers the standard names are
|
||||
* whole DES blocks. A key of another family is refused rather than read as bytes it is
|
||||
* not.
|
||||
*
|
||||
* @param algorithmName the algorithm of the key
|
||||
* @param role what the key is, for the message
|
||||
* @return the algorithm
|
||||
*/
|
||||
static PaymentCryptographyKeyAlgorithm requireTdes(String algorithmName, String role) {
|
||||
PaymentCryptographyKeyAlgorithm algorithm =
|
||||
PaymentCryptographyKeyAlgorithm.from(algorithmName);
|
||||
if (algorithm == null || !algorithm.isTdes()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"an AS2805 key is a TDES key, but the " + role + " is " + algorithmName, 400);
|
||||
}
|
||||
return algorithm;
|
||||
}
|
||||
|
||||
/**
|
||||
* The mode of use a key has to have to derive from it.
|
||||
*
|
||||
|
||||
+4
-21
@@ -17,10 +17,10 @@ import java.util.Map;
|
||||
import static io.floci.aws.services.paymentcryptography.PaymentCryptographyAttributes.soleVariant;
|
||||
import static io.floci.aws.services.paymentcryptography.PaymentCryptographyAttributes.text;
|
||||
import static io.floci.aws.services.paymentcryptography.PaymentCryptographyKeyRules.materialHex;
|
||||
import static io.floci.aws.services.paymentcryptography.PaymentCryptographyKeyRules.requireEnabled;
|
||||
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.PaymentCryptographyKeyRules.requiredKey;
|
||||
|
||||
/**
|
||||
* The PIN operations, over the Visa PVV path.
|
||||
@@ -131,7 +131,7 @@ public class PaymentCryptographyPinOperations {
|
||||
requireNoDukptForAes(format, request.get("DukptAttributes"));
|
||||
requireKeyMatchesFormat(pek, format, "EncryptionKeyIdentifier");
|
||||
|
||||
PaymentCryptographyKey pvk = requiredKey(verificationKeyIdentifier, region);
|
||||
PaymentCryptographyKey pvk = requiredKey(service, verificationKeyIdentifier, region);
|
||||
requireMode(pvk, "Verify", "verifying a PIN");
|
||||
requireUsage(pvk, VISA_VERIFICATION_USAGES, "verifying a PIN");
|
||||
// The Visa PVV is defined over Triple DES, so the key is one.
|
||||
@@ -202,7 +202,7 @@ public class PaymentCryptographyPinOperations {
|
||||
|
||||
PinLeg pek = resolvePinLeg(encryptionKeyIdentifier, null, region,
|
||||
"Encrypt", "encrypting a PIN block");
|
||||
PaymentCryptographyKey pvk = requiredKey(generationKeyIdentifier, region);
|
||||
PaymentCryptographyKey pvk = requiredKey(service, generationKeyIdentifier, region);
|
||||
requireMode(pvk, "Generate", "generating a PIN verification value");
|
||||
requireUsage(pvk, VISA_VERIFICATION_USAGES, "generating a PIN verification value");
|
||||
// The Visa PVV is defined over Triple DES, so the key is one.
|
||||
@@ -376,11 +376,7 @@ public class PaymentCryptographyPinOperations {
|
||||
*/
|
||||
private PinLeg resolvePinLeg(String identifier, JsonNode dukptAttributes, String region,
|
||||
String mode, String what) {
|
||||
PaymentCryptographyKey key = service.findKey(region, identifier);
|
||||
if (key == null) {
|
||||
throw notFound(identifier);
|
||||
}
|
||||
requireEnabled(key);
|
||||
PaymentCryptographyKey key = requiredKey(service, identifier, region);
|
||||
if (dukptAttributes == null) {
|
||||
requireMode(key, mode, what);
|
||||
requireUsage(key, PIN_ENCRYPTION_USAGES, what);
|
||||
@@ -452,15 +448,6 @@ public class PaymentCryptographyPinOperations {
|
||||
|
||||
// ---- the helpers ---------------------------------------------------------
|
||||
|
||||
private PaymentCryptographyKey requiredKey(String identifier, String region) {
|
||||
PaymentCryptographyKey key = service.findKey(region, identifier);
|
||||
if (key == null) {
|
||||
throw notFound(identifier);
|
||||
}
|
||||
requireEnabled(key);
|
||||
return key;
|
||||
}
|
||||
|
||||
private static PaymentCryptographyKeyAlgorithm algorithmOf(PaymentCryptographyKey key) {
|
||||
PaymentCryptographyKeyAlgorithm algorithm =
|
||||
PaymentCryptographyKeyAlgorithm.from(key.getKeyAlgorithm());
|
||||
@@ -615,10 +602,6 @@ public class PaymentCryptographyPinOperations {
|
||||
return new AwsException("ValidationException", message, 400);
|
||||
}
|
||||
|
||||
private static AwsException notFound(String identifier) {
|
||||
return new AwsException("ResourceNotFoundException", "Key not found: " + identifier, 404);
|
||||
}
|
||||
|
||||
/**
|
||||
* The PIN did not verify.
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user