mirror of
https://github.com/floci-io/floci.git
synced 2026-12-02 09:47:56 -05:00
feat(paymentcryptography): the data plane as a service of its own (#7396)
* feat(paymentcryptography): the data plane as a service of its own docs(paymentcryptography): the data plane has a page of its own fix(paymentcryptography): a MAC under a three-key Triple DES key is computed test(paymentcryptography): a key of another purpose does not encrypt data feat(paymentcryptography): the data plane's IAM is generated fix(paymentcryptography): a DUKPT MAC needs a base derivation key fix(paymentcryptography): a key whose material is not held is refused docs(paymentcryptography): the data plane's example runs test(paymentcryptography): the data plane through the AWS SDK test(paymentcryptography): the data plane through the AWS CLI fix(paymentcryptography): the CLI test removes the keys it makes docs(compat): the AWS CLI suite names the data plane test(paymentcryptography): the route is read as the registry reads it test(paymentcryptography): the request names its method and its URI fix(paymentcryptography): the cipher pads no data fix(paymentcryptography): the data plane takes the service scope fix(paymentcryptography): a MAC length that is not a number is refused docs(paymentcryptography): the data plane says the cipher pads no data fix(paymentcryptography): the data plane ciphers with Triple DES as well fix(paymentcryptography): the scope alias names the model's scope fix(paymentcryptography): the Triple DES cipher is BouncyCastle's fix(paymentcryptography): the usage and the blob keep their comments fix(paymentcryptography): a key's family is read from its algorithm fix(paymentcryptography): the cipher reads the vector through one helper test(paymentcryptography): the keys are built by one builder fix(paymentcryptography): the data plane checks the model's rules fix(paymentcryptography): the Triple DES check value is BouncyCastle's fix(paymentcryptography): the SDK names it TDES_2_KEY docs(contributing): the ciphers come from BouncyCastle refactor(paymentcryptography): the model owns the request shapes refactor(paymentcryptography): the ciphers are BouncyCastle's test(compat): one helper makes a key of either algorithm docs(contributing): the cipher rule comes out fix(paymentcryptography): the refusals are the model's refactor(paymentcryptography): the ciphers are one helper each refactor(paymentcryptography): the generators share the helper docs(paymentcryptography): the javadocs name both ciphers test(aws): the moved constants read from the shared helper fix(paymentcryptography): the deprecated cipher constructors go chore(paymentcryptography): the duplicate import goes test(paymentcryptography): the DUKPT vectors the standard publishes fix(paymentcryptography): the DUKPT refusal is the derivation's docs(paymentcryptography): the retail MAC is over a double-length key fix(paymentcryptography): the ISO 9797-1 MAC pads only a partial block refactor(paymentcryptography): the refusals the model already makes chore(aws): the routes are generated from the models again * chore(aws): the IAM resources are generated again
This commit is contained in:
1 parent
ae196fb9ac
commit
4617e4a4f8
45 files changed
+3942
-356
No files matched your search
@@ -107,6 +107,7 @@ import software.amazon.awssdk.services.oam.OamClient;
|
||||
import software.amazon.awssdk.services.opensearch.OpenSearchClient;
|
||||
import software.amazon.awssdk.services.organizations.OrganizationsClient;
|
||||
import software.amazon.awssdk.services.paymentcryptography.PaymentCryptographyClient;
|
||||
import software.amazon.awssdk.services.paymentcryptographydata.PaymentCryptographyDataClient;
|
||||
import software.amazon.awssdk.services.pipes.PipesClient;
|
||||
import software.amazon.awssdk.services.quicksight.QuickSightClient;
|
||||
import software.amazon.awssdk.services.ram.RamClient;
|
||||
@@ -1136,6 +1137,18 @@ public final class TestFixtures {
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* The Payment Cryptography data plane. The real service is a rest-json service of its
|
||||
* own, at its own host, so the client talks to it over the paths of that protocol.
|
||||
*/
|
||||
public static PaymentCryptographyDataClient paymentCryptographyDataClient() {
|
||||
return PaymentCryptographyDataClient.builder()
|
||||
.endpointOverride(ENDPOINT)
|
||||
.region(REGION)
|
||||
.credentialsProvider(CREDENTIALS)
|
||||
.build();
|
||||
}
|
||||
|
||||
public static SecretsManagerClient secretsManagerClient() {
|
||||
return SecretsManagerClient.builder()
|
||||
.endpointOverride(ENDPOINT)
|
||||
|
||||
+216
@@ -0,0 +1,216 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.awscore.exception.AwsServiceException;
|
||||
import software.amazon.awssdk.services.paymentcryptography.model.KeyAlgorithm;
|
||||
import software.amazon.awssdk.services.paymentcryptography.model.KeyClass;
|
||||
import software.amazon.awssdk.services.paymentcryptography.model.KeyUsage;
|
||||
import software.amazon.awssdk.services.paymentcryptographydata.PaymentCryptographyDataClient;
|
||||
import software.amazon.awssdk.services.paymentcryptographydata.model.EncryptionDecryptionAttributes;
|
||||
import software.amazon.awssdk.services.paymentcryptographydata.model.EncryptionMode;
|
||||
import software.amazon.awssdk.services.paymentcryptographydata.model.GenerateMacRequest;
|
||||
import software.amazon.awssdk.services.paymentcryptographydata.model.MacAlgorithm;
|
||||
import software.amazon.awssdk.services.paymentcryptographydata.model.MacAttributes;
|
||||
import software.amazon.awssdk.services.paymentcryptographydata.model.ReEncryptionAttributes;
|
||||
import software.amazon.awssdk.services.paymentcryptographydata.model.SymmetricEncryptionAttributes;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@DisplayName("Payment Cryptography data plane")
|
||||
class PaymentCryptographyDataPlaneTest {
|
||||
|
||||
/**
|
||||
* A plaintext long enough for the service to encrypt it. The cipher pads no data, so it
|
||||
* fills whole blocks.
|
||||
*/
|
||||
private static final String PLAIN_TEXT = hex("the quick brown fox jumps over the lazy dog!!!!!");
|
||||
|
||||
/** A message long enough for the service to make a MAC over it. */
|
||||
private static final String MESSAGE = hex("a message to authenticate");
|
||||
|
||||
private static PaymentCryptographyDataClient client;
|
||||
private static String keyArn;
|
||||
private static String macKeyArn;
|
||||
|
||||
@BeforeAll
|
||||
static void setup() {
|
||||
client = TestFixtures.paymentCryptographyDataClient();
|
||||
keyArn = TestFixtures.paymentCryptographyClient().createKey(r -> r
|
||||
.keyAttributes(a -> a
|
||||
.keyAlgorithm(KeyAlgorithm.AES_256)
|
||||
.keyClass(KeyClass.SYMMETRIC_KEY)
|
||||
.keyUsage(KeyUsage.TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY)
|
||||
.keyModesOfUse(m -> m.encrypt(true).decrypt(true).generate(true).verify(true)))
|
||||
.exportable(true))
|
||||
.key().keyArn();
|
||||
// A MAC is made under a key of the MAC usage, so it is one of its own.
|
||||
macKeyArn = TestFixtures.paymentCryptographyClient().createKey(r -> r
|
||||
.keyAttributes(a -> a
|
||||
.keyAlgorithm(KeyAlgorithm.AES_256)
|
||||
.keyClass(KeyClass.SYMMETRIC_KEY)
|
||||
.keyUsage(KeyUsage.TR31_M6_ISO_9797_5_CMAC_KEY)
|
||||
.keyModesOfUse(m -> m.generate(true).verify(true)))
|
||||
.exportable(true))
|
||||
.key().keyArn();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void cleanup() {
|
||||
if (client != null) {
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKeyEncryptsAndDecrypts() {
|
||||
String cipherText = client.encryptData(r -> r
|
||||
.keyIdentifier(keyArn)
|
||||
.plainText(PLAIN_TEXT)
|
||||
.encryptionAttributes(EncryptionDecryptionAttributes.builder()
|
||||
.symmetric(SymmetricEncryptionAttributes.builder().mode(EncryptionMode.CBC).build())
|
||||
.build()))
|
||||
.cipherText();
|
||||
|
||||
assertThat(cipherText).isNotBlank();
|
||||
assertThat(cipherText).isNotEqualTo(PLAIN_TEXT);
|
||||
|
||||
String plainText = client.decryptData(r -> r
|
||||
.keyIdentifier(keyArn)
|
||||
.cipherText(cipherText)
|
||||
.decryptionAttributes(EncryptionDecryptionAttributes.builder()
|
||||
.symmetric(SymmetricEncryptionAttributes.builder().mode(EncryptionMode.CBC).build())
|
||||
.build()))
|
||||
.plainText();
|
||||
|
||||
assertThat(plainText).isEqualTo(PLAIN_TEXT);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKeyReEncryptsUnderAnother() {
|
||||
String otherArn = TestFixtures.paymentCryptographyClient().createKey(r -> r
|
||||
.keyAttributes(a -> a
|
||||
.keyAlgorithm(KeyAlgorithm.AES_256)
|
||||
.keyClass(KeyClass.SYMMETRIC_KEY)
|
||||
.keyUsage(KeyUsage.TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY)
|
||||
.keyModesOfUse(m -> m.encrypt(true).decrypt(true)))
|
||||
.exportable(true))
|
||||
.key().keyArn();
|
||||
|
||||
String cipherText = client.encryptData(r -> r
|
||||
.keyIdentifier(keyArn)
|
||||
.plainText(PLAIN_TEXT)
|
||||
.encryptionAttributes(EncryptionDecryptionAttributes.builder()
|
||||
.symmetric(SymmetricEncryptionAttributes.builder().mode(EncryptionMode.CBC).build())
|
||||
.build()))
|
||||
.cipherText();
|
||||
|
||||
// The re-encryption names the key it decrypts under in the path and the key it encrypts
|
||||
// under in the body, so the request carries two keys.
|
||||
String reEncrypted = client.reEncryptData(r -> r
|
||||
.incomingKeyIdentifier(keyArn)
|
||||
.outgoingKeyIdentifier(otherArn)
|
||||
.cipherText(cipherText)
|
||||
.incomingEncryptionAttributes(ReEncryptionAttributes.builder()
|
||||
.symmetric(SymmetricEncryptionAttributes.builder().mode(EncryptionMode.CBC).build())
|
||||
.build())
|
||||
.outgoingEncryptionAttributes(ReEncryptionAttributes.builder()
|
||||
.symmetric(SymmetricEncryptionAttributes.builder().mode(EncryptionMode.CBC).build())
|
||||
.build()))
|
||||
.cipherText();
|
||||
|
||||
assertThat(reEncrypted).isNotBlank();
|
||||
assertThat(reEncrypted).isNotEqualTo(cipherText);
|
||||
|
||||
// The block under the other key decrypts there and not under the first.
|
||||
String plainText = client.decryptData(r -> r
|
||||
.keyIdentifier(otherArn)
|
||||
.cipherText(reEncrypted)
|
||||
.decryptionAttributes(EncryptionDecryptionAttributes.builder()
|
||||
.symmetric(SymmetricEncryptionAttributes.builder().mode(EncryptionMode.CBC).build())
|
||||
.build()))
|
||||
.plainText();
|
||||
|
||||
assertThat(plainText).isEqualTo(PLAIN_TEXT);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKeyMakesAMacAndVerifiesIt() {
|
||||
GenerateMacRequest mac = GenerateMacRequest.builder()
|
||||
.keyIdentifier(macKeyArn)
|
||||
.messageData(MESSAGE)
|
||||
.macLength(16)
|
||||
.generationAttributes(MacAttributes.builder()
|
||||
.algorithm(MacAlgorithm.CMAC)
|
||||
.build())
|
||||
.build();
|
||||
|
||||
String value = client.generateMac(mac).mac();
|
||||
|
||||
assertThat(value).isNotBlank();
|
||||
|
||||
// The service fails the request when the MAC is not the one over the message.
|
||||
client.verifyMac(r -> r
|
||||
.keyIdentifier(macKeyArn)
|
||||
.messageData(MESSAGE)
|
||||
.mac(value)
|
||||
.macLength(16)
|
||||
.verificationAttributes(MacAttributes.builder()
|
||||
.algorithm(MacAlgorithm.CMAC)
|
||||
.build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMacOfAnotherMessageIsRefused() {
|
||||
String value = client.generateMac(r -> r
|
||||
.keyIdentifier(macKeyArn)
|
||||
.messageData(MESSAGE)
|
||||
.macLength(16)
|
||||
.generationAttributes(MacAttributes.builder()
|
||||
.algorithm(MacAlgorithm.CMAC)
|
||||
.build()))
|
||||
.mac();
|
||||
|
||||
// The failure names the reason the model's shape carries, so a caller can tell a bad
|
||||
// MAC from a bad request.
|
||||
AwsServiceException refusal = assertThrows(AwsServiceException.class, () -> client.verifyMac(r -> r
|
||||
.keyIdentifier(macKeyArn)
|
||||
.messageData(hex("another message entirely"))
|
||||
.mac(value)
|
||||
.macLength(16)
|
||||
.verificationAttributes(MacAttributes.builder()
|
||||
.algorithm(MacAlgorithm.CMAC)
|
||||
.build())));
|
||||
assertThat(refusal.awsErrorDetails().errorCode()).isEqualTo("VerificationFailedException");
|
||||
assertThat(refusal.statusCode()).isEqualTo(400);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKeyThatIsNotThereIsRefused() {
|
||||
// The identifier matches the model's pattern, so the request reaches the key it names
|
||||
// rather than being refused as a malformed one.
|
||||
AwsServiceException refusal = assertThrows(AwsServiceException.class, () -> client.encryptData(r -> r
|
||||
.keyIdentifier("arn:aws:payment-cryptography:us-east-1:000000000000:key/0123456789abcdef")
|
||||
.plainText(PLAIN_TEXT)
|
||||
.encryptionAttributes(EncryptionDecryptionAttributes.builder()
|
||||
.symmetric(SymmetricEncryptionAttributes.builder().mode(EncryptionMode.CBC).build())
|
||||
.build())));
|
||||
assertThat(refusal.awsErrorDetails().errorCode()).isEqualTo("ResourceNotFoundException");
|
||||
assertThat(refusal.statusCode()).isEqualTo(404);
|
||||
}
|
||||
|
||||
/** Bytes as the data plane carries them: hexadecimal. */
|
||||
private static String hex(String text) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (byte b : text.getBytes(StandardCharsets.UTF_8)) {
|
||||
sb.append(String.format("%02X", b));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
+34
-1
@@ -68,9 +68,24 @@ class PaymentCryptographyTest {
|
||||
}
|
||||
|
||||
private static Key createAesKey() {
|
||||
return createKey(KeyAlgorithm.AES_256);
|
||||
}
|
||||
|
||||
/**
|
||||
* A Triple DES key, whose check value is the ANSI X9.24 one.
|
||||
*
|
||||
* <p>The key is created here because creating it computes that value, and the cipher
|
||||
* behind it is the one the native image has to carry.
|
||||
*/
|
||||
private static Key createTdesKey() {
|
||||
return createKey(KeyAlgorithm.TDES_2_KEY);
|
||||
}
|
||||
|
||||
/** A key of the given algorithm, for the data the cipher protects. */
|
||||
private static Key createKey(KeyAlgorithm algorithm) {
|
||||
Key key = client.createKey(r -> r
|
||||
.keyAttributes(a -> a
|
||||
.keyAlgorithm(KeyAlgorithm.AES_256)
|
||||
.keyAlgorithm(algorithm)
|
||||
.keyClass(KeyClass.SYMMETRIC_KEY)
|
||||
.keyUsage(KeyUsage.TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY)
|
||||
.keyModesOfUse(m -> m.encrypt(true).decrypt(true)))
|
||||
@@ -113,6 +128,24 @@ class PaymentCryptographyTest {
|
||||
assertThat(read.keyCheckValue()).isEqualTo(created.keyCheckValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTripleDesKeyIsCreatedAndItsCheckValueIsTheX924One() {
|
||||
// The check value of a Triple DES key is the ANSI X9.24 one, so it is the leftmost
|
||||
// three bytes of encrypting eight zero bytes with the key. The native image has to
|
||||
// carry that cipher, so the key is created here and the value is read back.
|
||||
Key created = createTdesKey();
|
||||
|
||||
assertThat(created.keyArn()).isNotBlank();
|
||||
assertThat(created.keyState()).isEqualTo(KeyState.CREATE_COMPLETE);
|
||||
assertThat(created.keyCheckValueAlgorithm()).isEqualTo(KeyCheckValueAlgorithm.ANSI_X9_24);
|
||||
assertThat(created.keyCheckValue()).hasSize(6);
|
||||
assertThat(created.keyAttributes().keyAlgorithm()).isEqualTo(KeyAlgorithm.TDES_2_KEY);
|
||||
|
||||
Key read = client.getKey(k -> k.keyIdentifier(created.keyArn())).key();
|
||||
assertThat(read.keyCheckValue()).isEqualTo(created.keyCheckValue());
|
||||
assertThat(read.keyCheckValueAlgorithm()).isEqualTo(KeyCheckValueAlgorithm.ANSI_X9_24);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKeyIsCreatedDisabledAndWithTags() {
|
||||
Key key = client.createKey(r -> r
|
||||
|
||||
Reference in new issue
Block a user