feat(paymentcryptography): the AS2805 key cryptogram (#8172)

ExportKey and ImportKey take an As2805KeyCryptogram: a TDES key leaves and
comes back under a TR31_K0 TDES key encryption key, bound to one of the
four AS2805 variants. The cryptogram carries a tag over the whole of it,
made under a key derived from the wrapping key and checked before anything
is decrypted, and the key's ANSI X9.24 check value, which the import
compares. A wrapping key that is not K0, not TDES or weaker than the key is
refused, as is a variant that does not match the key's purpose.

Bytes holds the concat, PKCS#7 padding and isHex the payment cryptography
classes kept copies of, and the SDK and CLI compatibility tests cover the
round trip and its refusals.
This commit is contained in:
Murtuza Chhil authored and GitHub committed 2026-11-15 08:44:38 -04:00
1 parent b8c08f1be4
commit ff467b2e09
14 files changed
+1361 -105

No files matched your search

@@ -0,0 +1,272 @@
package io.floci.aws.services.paymentcryptography;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.ValueSource;
import java.util.HexFormat;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* The AS2805 key cryptogram.
*
* <p>The cryptogram is this emulator's own construction, so the tests here are about the
* rules it has to follow: a cryptogram opens under the key it was wrapped under and under
* the variant it was bound to, and a cryptogram that was changed in any way does not open
* at all.
*/
class PaymentCryptographyAs2805CryptogramTest {
/** The hexadecimal the cryptograms are written in. */
private static final HexFormat HEX = HexFormat.of().withUpperCase();
/** The variant the PIN encryption key is bound to. */
private static final int PIN_VARIANT = 0x1C;
/** The variant the data encryption key is bound to. */
private static final int DATA_VARIANT = 0x16;
/** The length of the marker, in bytes. */
private static final int MARKER_BYTES = 4;
/** The length of the initialization vector, in bytes: the Triple DES block size. */
private static final int IV_BYTES = 8;
/** The length of the tag, in bytes. */
private static final int TAG_BYTES = 32;
/** The byte the key the cryptogram carries starts at. */
private static final int KEY_SEED = 0x00;
/** The byte the other key the cryptogram carries starts at. */
private static final int OTHER_KEY_SEED = 0x10;
/** The byte the wrapping key starts at. */
private static final int WRAPPING_KEY_SEED = 0xF0;
/** The byte the other wrapping key starts at. */
private static final int OTHER_WRAPPING_KEY_SEED = 0x20;
@ParameterizedTest
@EnumSource(value = PaymentCryptographyKeyAlgorithm.class,
names = {"TDES_2KEY", "TDES_3KEY"})
void aCryptogramComesBackUnderTheKeyItWasWrappedUnder(PaymentCryptographyKeyAlgorithm algorithm) {
String key = keyOf(algorithm.bytes(), KEY_SEED);
String wrappingKey = keyOf(algorithm.bytes(), WRAPPING_KEY_SEED);
String cryptogram = wrap(key, PIN_VARIANT, algorithm, wrappingKey);
byte[] clear = unwrap(cryptogram, PIN_VARIANT, algorithm, wrappingKey);
assertArrayEquals(PaymentCryptographyCrypto.fromHex(key), clear);
}
@ParameterizedTest
@ValueSource(strings = {"marker", "ciphertext", "tag"})
void aCryptogramThatWasChangedDoesNotOpen(String part) {
PaymentCryptographyKeyAlgorithm algorithm = PaymentCryptographyKeyAlgorithm.TDES_3KEY;
String key = keyOf(algorithm.bytes(), KEY_SEED);
String wrappingKey = keyOf(algorithm.bytes(), WRAPPING_KEY_SEED);
String cryptogram = wrap(key, PIN_VARIANT, algorithm, wrappingKey);
String changed = changed(cryptogram, atOf(part, cryptogram));
assertThrows(IllegalArgumentException.class,
() -> unwrap(changed, PIN_VARIANT, algorithm, wrappingKey));
}
@Test
void aCryptogramThatCarriesAnotherKeysCiphertextDoesNotOpen() {
// The check value inside the cryptogram is the check value of the key the
// ciphertext carries, so a ciphertext of another key passes it. The tag is over the
// ciphertext, so the substitution is caught.
PaymentCryptographyKeyAlgorithm algorithm = PaymentCryptographyKeyAlgorithm.TDES_3KEY;
String wrappingKey = keyOf(algorithm.bytes(), WRAPPING_KEY_SEED);
String cryptogram = wrap(keyOf(algorithm.bytes(), KEY_SEED), PIN_VARIANT, algorithm,
wrappingKey);
String other = wrap(keyOf(algorithm.bytes(), OTHER_KEY_SEED), PIN_VARIANT, algorithm,
wrappingKey);
String spliced = splice(cryptogram, other);
IllegalArgumentException refused = assertThrows(IllegalArgumentException.class,
() -> unwrap(spliced, PIN_VARIANT, algorithm, wrappingKey));
assertEquals("the cryptogram did not open under the wrapping key", refused.getMessage());
}
@Test
void aCryptogramUnderOneVariantDoesNotOpenUnderAnother() {
// The tag is over the variant, so a cryptogram bound to one purpose does not open
// under another even though the key it carries is the same.
PaymentCryptographyKeyAlgorithm algorithm = PaymentCryptographyKeyAlgorithm.TDES_3KEY;
String key = keyOf(algorithm.bytes(), KEY_SEED);
String wrappingKey = keyOf(algorithm.bytes(), WRAPPING_KEY_SEED);
String cryptogram = wrap(key, PIN_VARIANT, algorithm, wrappingKey);
IllegalArgumentException refused = assertThrows(IllegalArgumentException.class,
() -> unwrap(cryptogram, DATA_VARIANT, algorithm, wrappingKey));
assertEquals("the cryptogram did not open under the wrapping key", refused.getMessage());
}
@Test
void aCryptogramWhoseAlgorithmWasChangedDoesNotOpen() {
// The tag is over the algorithm as well, so a cryptogram that names another
// algorithm does not open under it.
PaymentCryptographyKeyAlgorithm algorithm = PaymentCryptographyKeyAlgorithm.TDES_3KEY;
String key = keyOf(algorithm.bytes(), KEY_SEED);
String wrappingKey = keyOf(algorithm.bytes(), WRAPPING_KEY_SEED);
String cryptogram = wrap(key, PIN_VARIANT, algorithm, wrappingKey);
IllegalArgumentException refused = assertThrows(IllegalArgumentException.class,
() -> unwrap(cryptogram, PIN_VARIANT, PaymentCryptographyKeyAlgorithm.TDES_2KEY,
wrappingKey));
assertEquals("the cryptogram did not open under the wrapping key", refused.getMessage());
}
@Test
void aCryptogramUnderOneWrappingKeyDoesNotOpenUnderAnother() {
PaymentCryptographyKeyAlgorithm algorithm = PaymentCryptographyKeyAlgorithm.TDES_3KEY;
String key = keyOf(algorithm.bytes(), KEY_SEED);
String wrappingKey = keyOf(algorithm.bytes(), WRAPPING_KEY_SEED);
String cryptogram = wrap(key, PIN_VARIANT, algorithm, wrappingKey);
IllegalArgumentException refused = assertThrows(IllegalArgumentException.class,
() -> unwrap(cryptogram, PIN_VARIANT, algorithm,
keyOf(algorithm.bytes(), OTHER_WRAPPING_KEY_SEED)));
assertEquals("the cryptogram did not open under the wrapping key", refused.getMessage());
}
@Test
void twoCryptogramsOfTheSameKeyAreNotTheSameBytes() {
// The vector is fresh for every cryptogram, so the same key under the same wrapping
// key does not give the same bytes twice.
PaymentCryptographyKeyAlgorithm algorithm = PaymentCryptographyKeyAlgorithm.TDES_3KEY;
String key = keyOf(algorithm.bytes(), KEY_SEED);
String wrappingKey = keyOf(algorithm.bytes(), WRAPPING_KEY_SEED);
String first = wrap(key, PIN_VARIANT, algorithm, wrappingKey);
String second = wrap(key, PIN_VARIANT, algorithm, wrappingKey);
assertNotEquals(first, second);
}
@Test
void aCryptogramThatIsNotHexadecimalIsRefused() {
assertThrows(IllegalArgumentException.class,
() -> unwrap("not hexadecimal", PIN_VARIANT, PaymentCryptographyKeyAlgorithm.TDES_3KEY,
keyOf(PaymentCryptographyKeyAlgorithm.TDES_3KEY.bytes(), WRAPPING_KEY_SEED)));
}
/**
* The cryptogram of a key under a wrapping key.
*
* <p>The cryptogram is written under Triple DES, the cipher AS2805 uses.
*
* @param key the key that is carried, in hexadecimal
* @param variant the variant the key is bound to
* @param algorithm the algorithm of the key
* @param wrappingKey the key the cryptogram is wrapped under, in hexadecimal
* @return the cryptogram, in hexadecimal
*/
private static String wrap(String key, int variant, PaymentCryptographyKeyAlgorithm algorithm,
String wrappingKey) {
return PaymentCryptographyAs2805Cryptogram.wrap(
PaymentCryptographyCrypto.fromHex(key), variant, algorithm,
PaymentCryptographyCrypto.fromHex(wrappingKey));
}
/**
* The key a cryptogram opens under.
*
* @param cryptogram the cryptogram, in hexadecimal
* @param variant the variant the key is bound to
* @param algorithm the algorithm of the key
* @param wrappingKey the key the cryptogram is wrapped under, in hexadecimal
* @return the clear key
*/
private static byte[] unwrap(String cryptogram, int variant,
PaymentCryptographyKeyAlgorithm algorithm, String wrappingKey) {
return PaymentCryptographyAs2805Cryptogram.unwrap(cryptogram, variant, algorithm,
PaymentCryptographyCrypto.fromHex(wrappingKey));
}
/**
* A key of the given length, in hexadecimal.
*
* <p>The seed tells the key apart from the other keys of the same length, so a key and
* the wrapping key that carries it are never the same bytes.
*
* @param length the length of the key, in bytes
* @param seed the byte the key starts at
* @return the key
*/
private static String keyOf(int length, int seed) {
StringBuilder hex = new StringBuilder();
for (int i = 0; i < length; i++) {
hex.append(String.format("%02X", (seed + i) & 0xFF));
}
return hex.toString();
}
/**
* The byte a part of the cryptogram is at.
*
* @param part the part of the cryptogram
* @param cryptogram the cryptogram, in hexadecimal
* @return the byte, counting from the front
*/
private static int atOf(String part, String cryptogram) {
return switch (part) {
case "marker" -> 0;
case "ciphertext" -> MARKER_BYTES + IV_BYTES;
case "tag" -> HEX.parseHex(cryptogram).length - 1;
default -> throw new IllegalArgumentException("no part named " + part);
};
}
/**
* The cryptogram with one of its bytes changed.
*
* @param cryptogram the cryptogram, in hexadecimal
* @param at the byte that is changed, counting from the front
* @return the changed cryptogram
*/
private static String changed(String cryptogram, int at) {
byte[] bytes = HEX.parseHex(cryptogram);
bytes[at] = (byte) (bytes[at] ^ 0x01);
return HEX.formatHex(bytes);
}
/**
* One cryptogram with the encrypted part of another one.
*
* <p>The marker and the tag are the first one's, and the vector and the ciphertext are
* the second one's, so the ciphertext is not the one the tag was made over. The
* ciphertext decrypts under the first cryptogram's own vector, because the second
* cryptogram carries its own.
*
* @param cryptogram the cryptogram the marker and the tag come from
* @param other the cryptogram the vector and the ciphertext come from
* @return the spliced cryptogram
*/
private static String splice(String cryptogram, String other) {
byte[] first = HEX.parseHex(cryptogram);
byte[] second = HEX.parseHex(other);
byte[] spliced = new byte[first.length];
System.arraycopy(first, 0, spliced, 0, MARKER_BYTES);
System.arraycopy(second, MARKER_BYTES, spliced, MARKER_BYTES,
second.length - MARKER_BYTES - TAG_BYTES);
System.arraycopy(first, first.length - TAG_BYTES, spliced, spliced.length - TAG_BYTES,
TAG_BYTES);
return HEX.formatHex(spliced);
}
}
@@ -247,18 +247,34 @@ class PaymentCryptographyIntegrationTest {
}
@Test
void aRequestNamingAFormTheServiceDoesNotServeIsRefused() {
// The AS2805 form is one the model has and this service does not make yet.
void theAs2805FormIsServed() {
// The AS2805 form is one the service makes now, so a request that names one is
// refused for what it is missing and not for naming an unserved form.
String arn = call("CreateKey", CREATE_KEY)
.then().statusCode(200)
.extract().path("Key.KeyArn");
call("ExportKey", "{\"ExportKeyIdentifier\":\"" + arn + "\",\"KeyMaterial\":{"
+ "\"As2805KeyCryptogram\":{\"As2805KeyVariant\":"
+ "\"PIN_ENCRYPTION_KEY_VARIANT_28\",\"WrappingKeyIdentifier\":\"" + arn + "\"}}}")
+ "\"PIN_ENCRYPTION_KEY_VARIANT_28\"}}}")
.then().statusCode(400)
.body("__type", equalTo("ValidationException"))
.body("message", containsString("not a form the service exports"));
.body("message", containsString("WrappingKeyIdentifier"));
}
@Test
void aRequestNamingNoFormIsRefused() {
// Every form the model names is one the service makes now, so a request that
// names none of them names no form at all.
String arn = call("CreateKey", CREATE_KEY)
.then().statusCode(200)
.extract().path("Key.KeyArn");
call("ExportKey", "{\"ExportKeyIdentifier\":\"" + arn + "\",\"KeyMaterial\":{}}")
.then().statusCode(400)
.body("__type", equalTo("ValidationException"))
.body("message", containsString("must specify exactly one variant"));
}
/**
@@ -428,6 +444,37 @@ class PaymentCryptographyIntegrationTest {
assertTheModelRefused(answer, "WrappedKeyBlock");
}
@Test
void theModelCheckRefusesAnAs2805ExportWithoutItsWrappingKey() {
Response answer = call("ExportKey", "{\"ExportKeyIdentifier\":\"" + keyArn() + "\","
+ "\"KeyMaterial\":{\"As2805KeyCryptogram\":{"
+ "\"As2805KeyVariant\":\"PIN_ENCRYPTION_KEY_VARIANT_28\"}}}");
assertTheModelRefused(answer, "WrappingKeyIdentifier");
}
@Test
void theModelCheckRefusesAnAs2805ExportUnderAVariantItDoesNotName() {
Response answer = call("ExportKey", "{\"ExportKeyIdentifier\":\"" + keyArn() + "\","
+ "\"KeyMaterial\":{\"As2805KeyCryptogram\":{"
+ "\"As2805KeyVariant\":\"TERMINAL_MINOR_KEY_VARIANT_01\","
+ "\"WrappingKeyIdentifier\":\"" + keyArn() + "\"}}}");
assertTheModelRefused(answer, "As2805KeyVariant");
}
@Test
void theModelCheckRefusesAnAs2805ImportWithoutItsWrappedKeyCryptogram() {
Response answer = call("ImportKey", "{\"KeyMaterial\":{\"As2805KeyCryptogram\":{"
+ "\"As2805KeyVariant\":\"PIN_ENCRYPTION_KEY_VARIANT_28\","
+ "\"KeyModesOfUse\":{\"Encrypt\":true,\"Decrypt\":true},"
+ "\"KeyAlgorithm\":\"AES_256\","
+ "\"Exportable\":true,"
+ "\"WrappingKeyIdentifier\":\"" + keyArn() + "\"}}}");
assertTheModelRefused(answer, "WrappedKeyCryptogram");
}
private static void assertTheModelRefused(Response answer, String member) {
answer.then().statusCode(400)
.body("__type", equalTo("ValidationException"))
@@ -48,6 +48,15 @@ class PaymentCryptographyKeyExchangeTest {
/** The key a key block is wrapped under. */
private PaymentCryptographyKey protection;
/** The variant the PIN encryption key is bound to. */
private static final String PIN_VARIANT = "PIN_ENCRYPTION_KEY_VARIANT_28";
/** The variant the data encryption key is bound to. */
private static final String DATA_VARIANT = "DATA_ENCRYPTION_KEY_VARIANT_22";
/** The variant the terminal major key is bound to. */
private static final String MAJOR_VARIANT = "TERMINAL_MAJOR_KEY_VARIANT_00";
@BeforeEach
void setUp() {
service = new PaymentCryptographyService(
@@ -768,6 +777,146 @@ class PaymentCryptographyKeyExchangeTest {
() -> service.importKey("us-east-1", "Tr34KeyBlock", incoming, "", true, Map.of()));
assertEquals("ValidationException", refused.getErrorCode());
}
/**
* The wrapped key an AS2805 export answers with.
*
* @param key the key that leaves
* @param kek the key the cryptogram is wrapped under
* @param variant the variant the key is bound to
* @return the wrapped key
*/
private Map<?, ?> as2805Export(PaymentCryptographyKey key, PaymentCryptographyKey kek,
String variant) {
Map<String, Object> answer = service.exportKey("us-east-1", key.getKeyId(),
"As2805KeyCryptogram", Map.of(
"As2805KeyVariant", variant,
"WrappingKeyIdentifier", kek.getKeyId()), "", "");
return (Map<?, ?>) answer.get("WrappedKey");
}
/**
* The material of an AS2805 import.
*
* @param variant the variant the cryptogram is bound to
* @param wrappingKeyId the key the cryptogram is wrapped under
* @param cryptogram the cryptogram that comes in
* @param algorithm the algorithm of the key that comes in
* @param usage the usage of the key that comes in
* @return the material
*/
private static Map<String, Object> as2805Import(String variant, String wrappingKeyId,
String cryptogram, String algorithm, String usage) {
Map<String, Object> incoming = new LinkedHashMap<>();
incoming.put("As2805KeyVariant", variant);
incoming.put("WrappingKeyIdentifier", wrappingKeyId);
incoming.put("WrappedKeyCryptogram", cryptogram);
incoming.put("KeyAlgorithm", algorithm);
incoming.put("KeyAttributes", Map.of(
"KeyAlgorithm", algorithm,
"KeyClass", "SYMMETRIC_KEY",
"KeyUsage", usage,
"KeyModesOfUse", Map.of("Encrypt", true, "Decrypt", true)));
incoming.put("Exportable", true);
return incoming;
}
@Test
void aKeyThatLeavesUnderAnAs2805CryptogramComesBack() {
PaymentCryptographyKey kek = createKey("TDES_3KEY", "TR31_K0_KEY_ENCRYPTION_KEY");
PaymentCryptographyKey key = createKey("TDES_3KEY", "TR31_P0_PIN_ENCRYPTION_KEY");
Map<?, ?> wrapped = as2805Export(key, kek, PIN_VARIANT);
assertEquals("KEY_CRYPTOGRAM", wrapped.get("WrappedKeyMaterialFormat"));
PaymentCryptographyKey imported = service.importKey("us-east-1", "As2805KeyCryptogram",
as2805Import(PIN_VARIANT, kek.getKeyId(), (String) wrapped.get("KeyMaterial"),
"TDES_3KEY", "TR31_P0_PIN_ENCRYPTION_KEY"), "", true, Map.of());
assertEquals(key.getKeyMaterialHex(), imported.getKeyMaterialHex());
assertEquals("TR31_P0_PIN_ENCRYPTION_KEY", imported.getKeyUsage());
}
@Test
void aCryptogramThatWasChangedDoesNotOpen() {
// The cryptogram carries a tag over the whole of it, so a cryptogram that was
// changed does not open at all, and not under a key that is wrong but plausible.
PaymentCryptographyKey kek = createKey("TDES_3KEY", "TR31_K0_KEY_ENCRYPTION_KEY");
PaymentCryptographyKey key = createKey("TDES_3KEY", "TR31_P0_PIN_ENCRYPTION_KEY");
String cryptogram = (String) as2805Export(key, kek, PIN_VARIANT).get("KeyMaterial");
// The character is inside the ciphertext, which is where the key it carries is.
String changed = changed(cryptogram, 50);
AwsException refused = assertThrows(AwsException.class,
() -> service.importKey("us-east-1", "As2805KeyCryptogram",
as2805Import(PIN_VARIANT, kek.getKeyId(), changed, "TDES_3KEY",
"TR31_P0_PIN_ENCRYPTION_KEY"), "", true, Map.of()));
assertEquals("ValidationException", refused.getErrorCode());
}
@Test
void aCryptogramUnderAKeyWhoseMaterialIsNotHeldIsRefused() {
// The material of a key is held while the emulator runs and is not written to the
// store, so a key read back after a restart has none, and the export says so
// rather than failing on the missing value.
PaymentCryptographyKey kek = createKey("TDES_3KEY", "TR31_K0_KEY_ENCRYPTION_KEY");
PaymentCryptographyKey key = createKey("TDES_3KEY", "TR31_P0_PIN_ENCRYPTION_KEY");
kek.setKeyMaterialHex(null);
AwsException refused = assertThrows(AwsException.class,
() -> service.exportKey("us-east-1", key.getKeyId(), "As2805KeyCryptogram",
Map.of(
"As2805KeyVariant", PIN_VARIANT,
"WrappingKeyIdentifier", kek.getKeyId()), "", ""));
assertEquals("ValidationException", refused.getErrorCode());
assertTrue(refused.getMessage().contains("material"), refused.getMessage());
}
/**
* The cryptogram with one of its characters changed.
*
* @param cryptogram the cryptogram, in hexadecimal
* @param at the character that is changed, counting from the front
* @return the changed cryptogram
*/
private static String changed(String cryptogram, int at) {
char[] characters = cryptogram.toCharArray();
characters[at] = characters[at] == 'A' ? 'B' : 'A';
return new String(characters);
}
@Test
void aCryptogramUnderAVariantTheServiceDoesNotServeIsRefused() {
// The model's union names four variants, and the service serves every one of them,
// so a variant outside the four names no variant at all.
PaymentCryptographyKey kek = createKey("TDES_3KEY", "TR31_K0_KEY_ENCRYPTION_KEY");
PaymentCryptographyKey key = createKey("TDES_3KEY", "TR31_P0_PIN_ENCRYPTION_KEY");
AwsException refused = assertThrows(AwsException.class,
() -> service.exportKey("us-east-1", key.getKeyId(), "As2805KeyCryptogram",
Map.of(
"As2805KeyVariant", "TERMINAL_MINOR_KEY_VARIANT_01",
"WrappingKeyIdentifier", kek.getKeyId()), "", ""));
assertEquals("ValidationException", refused.getErrorCode());
}
@Test
void aCryptogramThatIsWrappedUnderTheWrongVariantDoesNotOpen() {
PaymentCryptographyKey kek = createKey("TDES_3KEY", "TR31_K0_KEY_ENCRYPTION_KEY");
PaymentCryptographyKey key = createKey("TDES_3KEY", "TR31_P0_PIN_ENCRYPTION_KEY");
String cryptogram = (String) as2805Export(key, kek, PIN_VARIANT).get("KeyMaterial");
AwsException refused = assertThrows(AwsException.class,
() -> service.importKey("us-east-1", "As2805KeyCryptogram",
as2805Import(DATA_VARIANT, kek.getKeyId(), cryptogram, "TDES_3KEY",
"TR31_P0_PIN_ENCRYPTION_KEY"), "", true, Map.of()));
assertEquals("ValidationException", refused.getErrorCode());
}
@ParameterizedTest
@ValueSource(strings = {"RSA_3072", "RSA_4096", "ECC_NIST_P256", "ECC_NIST_P384", "ECC_NIST_P521"})
void anExportSigningKeyThatIsNotRsa2048IsRefused(String algorithm) {
@@ -780,6 +929,82 @@ class PaymentCryptographyKeyExchangeTest {
assertEquals("ValidationException", refused.getErrorCode());
assertTrue(refused.getMessage().contains("signed with RSA_2048"), refused.getMessage());
}
@Test
void aCryptogramUnderAVariantThatIsNotTheKeysPurposeIsRefused() {
// The variant says what the key is for, so a key exported under another variant
// would come back with the other variant's usage, and the export would change what
// the key is for.
PaymentCryptographyKey kek = createKey("TDES_3KEY", "TR31_K0_KEY_ENCRYPTION_KEY");
PaymentCryptographyKey key = createKey("TDES_3KEY", "TR31_P0_PIN_ENCRYPTION_KEY");
AwsException refused = assertThrows(AwsException.class,
() -> service.exportKey("us-east-1", key.getKeyId(), "As2805KeyCryptogram",
Map.of(
"As2805KeyVariant", MAJOR_VARIANT,
"WrappingKeyIdentifier", kek.getKeyId()), "", ""));
assertEquals("ValidationException", refused.getErrorCode());
}
@Test
void aCryptogramUnderAWeakerWrappingKeyIsRefused() {
// The wrapping key has to be at least as strong as the key it protects, the same
// rule the export enforces, so a cryptogram the export would refuse to write does
// not open here either.
// TDES_2KEY is weaker than TDES_3KEY, so a TDES_2KEY wrapping key cannot protect
// a TDES_3KEY key.
PaymentCryptographyKey kek = createKey("TDES_2KEY", "TR31_K0_KEY_ENCRYPTION_KEY");
String cryptogram = PaymentCryptographyAs2805Cryptogram.wrap(
HexFormat.of().parseHex("000102030405060708090A0B0C0D0E0F1011121314151617"),
0x00, PaymentCryptographyKeyAlgorithm.TDES_3KEY,
PaymentCryptographyCrypto.fromHex(kek.getKeyMaterialHex()));
AwsException refused = assertThrows(AwsException.class,
() -> service.importKey("us-east-1", "As2805KeyCryptogram",
as2805Import(MAJOR_VARIANT, kek.getKeyId(), cryptogram, "TDES_3KEY",
"TR31_K0_KEY_ENCRYPTION_KEY"), "", true, Map.of()));
assertEquals("ValidationException", refused.getErrorCode());
}
@Test
void anExportUnderAnAesWrappingKeyIsRefused() {
// AS2805's cryptography is all TDES, so a key encryption key that is AES cannot
// wrap a key cryptogram even though it is a key encryption key.
PaymentCryptographyKey kek = createKey("AES_128", "TR31_K0_KEY_ENCRYPTION_KEY");
PaymentCryptographyKey key = createKey("TDES_3KEY", "TR31_P0_PIN_ENCRYPTION_KEY");
AwsException refused = assertThrows(AwsException.class,
() -> service.exportKey("us-east-1", key.getKeyId(), "As2805KeyCryptogram",
Map.of(
"As2805KeyVariant", PIN_VARIANT,
"WrappingKeyIdentifier", kek.getKeyId()), "", ""));
assertEquals("ValidationException", refused.getErrorCode());
assertTrue(refused.getMessage().contains("TDES"), refused.getMessage());
assertTrue(refused.getMessage().contains("AES_128"), refused.getMessage());
}
@Test
void anImportUnderAnAesWrappingKeyIsRefused() {
// The export and the import refuse the same wrapping key, so a cryptogram the
// export would not write does not open here either.
PaymentCryptographyKey tdesKek = createKey("TDES_3KEY", "TR31_K0_KEY_ENCRYPTION_KEY");
PaymentCryptographyKey key = createKey("TDES_3KEY", "TR31_P0_PIN_ENCRYPTION_KEY");
String cryptogram = (String) as2805Export(key, tdesKek, PIN_VARIANT).get("KeyMaterial");
PaymentCryptographyKey kek = createKey("AES_128", "TR31_K0_KEY_ENCRYPTION_KEY");
AwsException refused = assertThrows(AwsException.class,
() -> service.importKey("us-east-1", "As2805KeyCryptogram",
as2805Import(PIN_VARIANT, kek.getKeyId(), cryptogram, "TDES_3KEY",
"TR31_P0_PIN_ENCRYPTION_KEY"), "", true, Map.of()));
assertEquals("ValidationException", refused.getErrorCode());
assertTrue(refused.getMessage().contains("TDES"), refused.getMessage());
assertTrue(refused.getMessage().contains("AES_128"), refused.getMessage());
}
@Test
void aTr34BlockThatCarriesNoHeaderIsRefused() throws Exception {
// A block that is signed correctly but whose key block names no key says nothing