mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(paymentcryptography): the list of keys answers with their summaries (#8135)
The model's KeySummary holds the identifier, the state, the attributes, the check value and the two flags, and not the whole key. The list answered with the whole key, so it carried the timestamps and the origin that a summary does not hold. The list answers with the summary now, and the whole key stays the answer of the operation that names one key. The control plane's answers are driven through a real SDK and through the AWS CLI, so the wire format of the operations the summary fix touches is the one the clients read. The certificate of a key pair, a tag that is added and removed, a key pair that keeps the usage its derived keys have, the RSA cryptogram the key exchange serves, the key the two parties agreed, and the timestamps the answers carry. The page said the member was read and checked. Nothing reads it: the shape check is what checks it, against the range the model states. The page said each key in the list is the whole key rather than the summary the model names, which the change that answers with the summary makes untrue. The page says what a listed key carries now. The round trip through the RSA cryptogram asserted the ARN of the key that came in and the usage it was given, which a wrong key of the same usage would also pass. It compares the check value with the exported key's, so the test shows that the same key came back. The key that came in was not added to the list the cleanup walks, so it was left in the service when the class finished. It is added now.
This commit is contained in:
1 parent
9fac49cdb0
commit
7c138ce3f1
7 files changed
+404
-8
No files matched your search
@@ -0,0 +1,87 @@
|
|||||||
|
#!/usr/bin/env bats
|
||||||
|
# Payment Cryptography control plane tests
|
||||||
|
#
|
||||||
|
# The operations that are not the key exchange itself: the certificate of a key pair, the
|
||||||
|
# tags of a key, and the usage a derived key is for. The tests drive them with the AWS CLI,
|
||||||
|
# which is such a client.
|
||||||
|
|
||||||
|
setup() {
|
||||||
|
load 'test_helper/common-setup'
|
||||||
|
KEY_ARN=""
|
||||||
|
PAIR_ARN=""
|
||||||
|
}
|
||||||
|
|
||||||
|
teardown() {
|
||||||
|
for key in "$KEY_ARN" "$PAIR_ARN"; do
|
||||||
|
if [ -n "$key" ]; then
|
||||||
|
aws_cmd payment-cryptography delete-key --key-identifier "$key" \
|
||||||
|
--delete-key-in-days 7 >/dev/null 2>&1 || true
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
# A key of the given algorithm, for the data the cipher protects.
|
||||||
|
create_key() {
|
||||||
|
aws_cmd payment-cryptography create-key \
|
||||||
|
--key-attributes "KeyAlgorithm=$1,KeyClass=SYMMETRIC_KEY,KeyUsage=TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY,KeyModesOfUse={Encrypt=true,Decrypt=true}" \
|
||||||
|
--exportable \
|
||||||
|
--query 'Key.KeyArn' --output text
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "Payment Cryptography control plane: the certificate of a key pair is served" {
|
||||||
|
PAIR_ARN=$(aws_cmd payment-cryptography create-key \
|
||||||
|
--key-attributes 'KeyAlgorithm=ECC_NIST_P256,KeyClass=ASYMMETRIC_KEY_PAIR,KeyUsage=TR31_S0_ASYMMETRIC_KEY_FOR_DIGITAL_SIGNATURE,KeyModesOfUse={Sign=true,Verify=true}' \
|
||||||
|
--exportable \
|
||||||
|
--query 'Key.KeyArn' --output text)
|
||||||
|
[ -n "$PAIR_ARN" ]
|
||||||
|
|
||||||
|
run aws_cmd payment-cryptography get-public-key-certificate \
|
||||||
|
--key-identifier "$PAIR_ARN"
|
||||||
|
assert_success
|
||||||
|
CERTIFICATE=$(json_get "$output" '.KeyCertificate')
|
||||||
|
CHAIN=$(json_get "$output" '.KeyCertificateChain')
|
||||||
|
[ -n "$CERTIFICATE" ]
|
||||||
|
[ -n "$CHAIN" ]
|
||||||
|
# The certificate is base64 of a PEM, so it decodes to one.
|
||||||
|
echo "$CERTIFICATE" | base64 --decode | grep -q "BEGIN CERTIFICATE"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "Payment Cryptography control plane: a tag is added to a key and removed again" {
|
||||||
|
KEY_ARN=$(create_key AES_256)
|
||||||
|
[ -n "$KEY_ARN" ]
|
||||||
|
|
||||||
|
run aws_cmd payment-cryptography tag-resource \
|
||||||
|
--resource-arn "$KEY_ARN" \
|
||||||
|
--tags "Key=owner,Value=team"
|
||||||
|
assert_success
|
||||||
|
|
||||||
|
run aws_cmd payment-cryptography list-tags-for-resource \
|
||||||
|
--resource-arn "$KEY_ARN"
|
||||||
|
assert_success
|
||||||
|
[ "$(json_get "$output" '.Tags[0].Value')" = "team" ]
|
||||||
|
|
||||||
|
run aws_cmd payment-cryptography untag-resource \
|
||||||
|
--resource-arn "$KEY_ARN" \
|
||||||
|
--tag-keys owner
|
||||||
|
assert_success
|
||||||
|
|
||||||
|
run aws_cmd payment-cryptography list-tags-for-resource \
|
||||||
|
--resource-arn "$KEY_ARN"
|
||||||
|
assert_success
|
||||||
|
[ "$(json_get "$output" '.Tags | length')" = "0" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "Payment Cryptography control plane: a key pair is created with the usage its derived keys have" {
|
||||||
|
# The usage a derived key is for is named when the key pair is made, as the export
|
||||||
|
# guide's key agreement flow does.
|
||||||
|
PAIR_ARN=$(aws_cmd payment-cryptography create-key \
|
||||||
|
--key-attributes 'KeyAlgorithm=ECC_NIST_P256,KeyClass=ASYMMETRIC_KEY_PAIR,KeyUsage=TR31_K3_ASYMMETRIC_KEY_FOR_KEY_AGREEMENT,KeyModesOfUse={DeriveKey=true}' \
|
||||||
|
--derive-key-usage TR31_K1_KEY_BLOCK_PROTECTION_KEY \
|
||||||
|
--exportable \
|
||||||
|
--query 'Key.KeyArn' --output text)
|
||||||
|
[ -n "$PAIR_ARN" ]
|
||||||
|
|
||||||
|
run aws_cmd payment-cryptography get-key --key-identifier "$PAIR_ARN"
|
||||||
|
assert_success
|
||||||
|
[ "$(json_get "$output" '.Key.DeriveKeyUsage')" = "TR31_K1_KEY_BLOCK_PROTECTION_KEY" ]
|
||||||
|
}
|
||||||
+232
@@ -1,5 +1,9 @@
|
|||||||
package com.floci.test;
|
package com.floci.test;
|
||||||
|
|
||||||
|
import org.bouncycastle.asn1.x500.X500Name;
|
||||||
|
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
|
||||||
|
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
|
||||||
|
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
|
||||||
import org.junit.jupiter.api.AfterAll;
|
import org.junit.jupiter.api.AfterAll;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
@@ -8,9 +12,11 @@ import software.amazon.awssdk.awscore.exception.AwsServiceException;
|
|||||||
import software.amazon.awssdk.regions.Region;
|
import software.amazon.awssdk.regions.Region;
|
||||||
import software.amazon.awssdk.services.paymentcryptography.PaymentCryptographyClient;
|
import software.amazon.awssdk.services.paymentcryptography.PaymentCryptographyClient;
|
||||||
import software.amazon.awssdk.services.paymentcryptography.model.Alias;
|
import software.amazon.awssdk.services.paymentcryptography.model.Alias;
|
||||||
|
import software.amazon.awssdk.services.paymentcryptography.model.DeriveKeyUsage;
|
||||||
import software.amazon.awssdk.services.paymentcryptography.model.ExportKeyResponse;
|
import software.amazon.awssdk.services.paymentcryptography.model.ExportKeyResponse;
|
||||||
import software.amazon.awssdk.services.paymentcryptography.model.GetParametersForExportResponse;
|
import software.amazon.awssdk.services.paymentcryptography.model.GetParametersForExportResponse;
|
||||||
import software.amazon.awssdk.services.paymentcryptography.model.GetParametersForImportResponse;
|
import software.amazon.awssdk.services.paymentcryptography.model.GetParametersForImportResponse;
|
||||||
|
import software.amazon.awssdk.services.paymentcryptography.model.GetPublicKeyCertificateResponse;
|
||||||
import software.amazon.awssdk.services.paymentcryptography.model.ImportKeyResponse;
|
import software.amazon.awssdk.services.paymentcryptography.model.ImportKeyResponse;
|
||||||
import software.amazon.awssdk.services.paymentcryptography.model.Key;
|
import software.amazon.awssdk.services.paymentcryptography.model.Key;
|
||||||
import software.amazon.awssdk.services.paymentcryptography.model.KeyAlgorithm;
|
import software.amazon.awssdk.services.paymentcryptography.model.KeyAlgorithm;
|
||||||
@@ -24,7 +30,17 @@ import software.amazon.awssdk.services.paymentcryptography.model.ListKeysRespons
|
|||||||
import software.amazon.awssdk.services.paymentcryptography.model.Tag;
|
import software.amazon.awssdk.services.paymentcryptography.model.Tag;
|
||||||
import software.amazon.awssdk.services.paymentcryptography.model.WrappedKeyMaterialFormat;
|
import software.amazon.awssdk.services.paymentcryptography.model.WrappedKeyMaterialFormat;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.security.KeyPair;
|
||||||
|
import java.security.KeyPairGenerator;
|
||||||
|
import java.security.cert.CertificateFactory;
|
||||||
|
import java.security.cert.X509Certificate;
|
||||||
|
import java.security.spec.ECGenParameterSpec;
|
||||||
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@@ -101,6 +117,26 @@ class PaymentCryptographyTest {
|
|||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The public half of a certificate, held by the service as a key.
|
||||||
|
*
|
||||||
|
* <p>The certificate is the one the service minted for the occasion, and it stands on
|
||||||
|
* its own, so it comes in as the public key of a root certificate.
|
||||||
|
*/
|
||||||
|
private static Key importPublicKey(String certificate) {
|
||||||
|
Key key = client.importKey(r -> r
|
||||||
|
.keyMaterial(m -> m.rootCertificatePublicKey(b -> b
|
||||||
|
.publicKeyCertificate(certificate)
|
||||||
|
.keyAttributes(a -> a
|
||||||
|
.keyAlgorithm(KeyAlgorithm.RSA_2048)
|
||||||
|
.keyClass(KeyClass.PUBLIC_KEY)
|
||||||
|
.keyUsage(KeyUsage.TR31_S0_ASYMMETRIC_KEY_FOR_DIGITAL_SIGNATURE)
|
||||||
|
.keyModesOfUse(modes -> modes.verify(true))))))
|
||||||
|
.key();
|
||||||
|
keyArns.add(key.keyArn());
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
/** A key of the given algorithm, for the data the cipher protects. */
|
/** A key of the given algorithm, for the data the cipher protects. */
|
||||||
private static Key createKey(KeyAlgorithm algorithm) {
|
private static Key createKey(KeyAlgorithm algorithm) {
|
||||||
Key key = client.createKey(r -> r
|
Key key = client.createKey(r -> r
|
||||||
@@ -362,4 +398,200 @@ class PaymentCryptographyTest {
|
|||||||
elsewhere.close();
|
elsewhere.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void aPublicKeyCertificateIsServedForAKeyPair() throws Exception {
|
||||||
|
// The certificate of a key pair is how the pair is handed to a counterparty.
|
||||||
|
Key key = client.createKey(r -> r
|
||||||
|
.keyAttributes(a -> a
|
||||||
|
.keyAlgorithm(KeyAlgorithm.ECC_NIST_P256)
|
||||||
|
.keyClass(KeyClass.ASYMMETRIC_KEY_PAIR)
|
||||||
|
.keyUsage(KeyUsage.TR31_S0_ASYMMETRIC_KEY_FOR_DIGITAL_SIGNATURE)
|
||||||
|
.keyModesOfUse(m -> m.sign(true).verify(true)))
|
||||||
|
.exportable(true))
|
||||||
|
.key();
|
||||||
|
keyArns.add(key.keyArn());
|
||||||
|
|
||||||
|
GetPublicKeyCertificateResponse certificate = client.getPublicKeyCertificate(r -> r
|
||||||
|
.keyIdentifier(key.keyArn()));
|
||||||
|
|
||||||
|
assertThat(certificate.keyCertificate()).isNotNull();
|
||||||
|
// The certificate is base64 of a PEM, so it parses as one.
|
||||||
|
X509Certificate parsed = (X509Certificate) CertificateFactory.getInstance("X.509")
|
||||||
|
.generateCertificate(new ByteArrayInputStream(
|
||||||
|
Base64.getDecoder().decode(certificate.keyCertificate())));
|
||||||
|
assertThat(parsed.getPublicKey()).isNotNull();
|
||||||
|
// The chain of a certificate the service minted is the certificate itself.
|
||||||
|
assertThat(certificate.keyCertificateChain()).isNotNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void aTagIsRemovedFromAKey() {
|
||||||
|
Key key = createAesKey();
|
||||||
|
|
||||||
|
client.tagResource(t -> t
|
||||||
|
.resourceArn(key.keyArn())
|
||||||
|
.tags(Tag.builder().key("owner").value("team").build()));
|
||||||
|
assertThat(client.listTagsForResource(r -> r.resourceArn(key.keyArn())).tags())
|
||||||
|
.anySatisfy(tag -> assertThat(tag.key()).isEqualTo("owner"));
|
||||||
|
|
||||||
|
client.untagResource(u -> u
|
||||||
|
.resourceArn(key.keyArn())
|
||||||
|
.tagKeys("owner"));
|
||||||
|
assertThat(client.listTagsForResource(r -> r.resourceArn(key.keyArn())).tags())
|
||||||
|
.noneSatisfy(tag -> assertThat(tag.key()).isEqualTo("owner"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void aKeyLeavesUnderAnRsaCryptogramAndComesBack() {
|
||||||
|
// The RSA wrap form: the key is encrypted under the counterparty's certificate and
|
||||||
|
// comes back under the token the import holds.
|
||||||
|
GetParametersForImportResponse parameters = client.getParametersForImport(r -> r
|
||||||
|
.keyMaterialType(KeyMaterialType.KEY_CRYPTOGRAM)
|
||||||
|
.wrappingKeyAlgorithm(KeyAlgorithm.RSA_2048));
|
||||||
|
|
||||||
|
// The certificate the service minted is the authority that vouches for it.
|
||||||
|
Key authority = importPublicKey(parameters.wrappingKeyCertificate());
|
||||||
|
|
||||||
|
Key key = createTdesKey();
|
||||||
|
|
||||||
|
ExportKeyResponse exported = client.exportKey(r -> r
|
||||||
|
.exportKeyIdentifier(key.keyArn())
|
||||||
|
.keyMaterial(m -> m.keyCryptogram(b -> b
|
||||||
|
.certificateAuthorityPublicKeyIdentifier(authority.keyArn())
|
||||||
|
.wrappingKeyCertificate(parameters.wrappingKeyCertificate())
|
||||||
|
.wrappingSpec("RSA_OAEP_SHA_256"))));
|
||||||
|
|
||||||
|
assertThat(exported.wrappedKey().wrappedKeyMaterialFormat())
|
||||||
|
.isEqualTo(WrappedKeyMaterialFormat.KEY_CRYPTOGRAM);
|
||||||
|
assertThat(exported.wrappedKey().keyMaterial()).isNotBlank();
|
||||||
|
|
||||||
|
ImportKeyResponse imported = client.importKey(r -> r
|
||||||
|
.keyMaterial(m -> m.keyCryptogram(b -> b
|
||||||
|
.importToken(parameters.importToken())
|
||||||
|
.wrappedKeyCryptogram(exported.wrappedKey().keyMaterial())
|
||||||
|
.wrappingSpec("RSA_OAEP_SHA_256")
|
||||||
|
.exportable(true)
|
||||||
|
.keyAttributes(a -> a
|
||||||
|
.keyAlgorithm(KeyAlgorithm.TDES_2_KEY)
|
||||||
|
.keyClass(KeyClass.SYMMETRIC_KEY)
|
||||||
|
.keyUsage(KeyUsage.TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY)
|
||||||
|
.keyModesOfUse(modes -> modes.encrypt(true).decrypt(true))))));
|
||||||
|
|
||||||
|
assertThat(imported.key().keyArn()).isNotBlank();
|
||||||
|
assertThat(imported.key().keyAttributes().keyUsage())
|
||||||
|
.isEqualTo(KeyUsage.TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY);
|
||||||
|
// The key that comes back is the key that left, so its check value is the one the
|
||||||
|
// exported key had.
|
||||||
|
assertThat(imported.key().keyCheckValue()).isEqualTo(key.keyCheckValue());
|
||||||
|
assertThat(imported.key().keyAttributes().keyAlgorithm()).isEqualTo(KeyAlgorithm.TDES_2_KEY);
|
||||||
|
// The key came into the service, so the cleanup has to take it away again.
|
||||||
|
keyArns.add(imported.key().keyArn());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void aKeyLeavesUnderAKeyTheTwoPartiesAgreed() throws Exception {
|
||||||
|
// The ECDH form: the two parties agree on a key, and the service wraps the key under
|
||||||
|
// it. The counterparty's certificate is minted here, signed by an authority the
|
||||||
|
// service holds.
|
||||||
|
KeyPair authority = rsaKeyPair();
|
||||||
|
KeyPair counterparty = eccKeyPair();
|
||||||
|
X509Certificate authorityCertificate = certificate(authority, "floci CA", authority, "floci CA");
|
||||||
|
X509Certificate counterpartyCertificate =
|
||||||
|
certificate(counterparty, "floci peer", authority, "floci CA");
|
||||||
|
|
||||||
|
Key ca = importPublicKey(Base64.getEncoder().encodeToString(authorityCertificate.getEncoded()));
|
||||||
|
|
||||||
|
// The service's own half of the agreement.
|
||||||
|
Key pair = client.createKey(r -> r
|
||||||
|
.keyAttributes(a -> a
|
||||||
|
.keyAlgorithm(KeyAlgorithm.ECC_NIST_P256)
|
||||||
|
.keyClass(KeyClass.ASYMMETRIC_KEY_PAIR)
|
||||||
|
.keyUsage(KeyUsage.TR31_K3_ASYMMETRIC_KEY_FOR_KEY_AGREEMENT)
|
||||||
|
.keyModesOfUse(m -> m.deriveKey(true)))
|
||||||
|
.deriveKeyUsage("TR31_K1_KEY_BLOCK_PROTECTION_KEY")
|
||||||
|
.exportable(true))
|
||||||
|
.key();
|
||||||
|
keyArns.add(pair.keyArn());
|
||||||
|
|
||||||
|
Key key = createTdesKey();
|
||||||
|
String peerCertificate = Base64.getEncoder().encodeToString(counterpartyCertificate.getEncoded());
|
||||||
|
|
||||||
|
ExportKeyResponse exported = client.exportKey(r -> r
|
||||||
|
.exportKeyIdentifier(key.keyArn())
|
||||||
|
.keyMaterial(m -> m.diffieHellmanTr31KeyBlock(b -> b
|
||||||
|
.privateKeyIdentifier(pair.keyArn())
|
||||||
|
.certificateAuthorityPublicKeyIdentifier(ca.keyArn())
|
||||||
|
.publicKeyCertificate(peerCertificate)
|
||||||
|
.deriveKeyAlgorithm("AES_256")
|
||||||
|
.keyDerivationFunction("NIST_SP800")
|
||||||
|
.keyDerivationHashAlgorithm("SHA_256")
|
||||||
|
.derivationData(d -> d.sharedInformation("ADEF567890")))));
|
||||||
|
|
||||||
|
assertThat(exported.wrappedKey().wrappedKeyMaterialFormat())
|
||||||
|
.isEqualTo(WrappedKeyMaterialFormat.TR31_KEY_BLOCK);
|
||||||
|
assertThat(exported.wrappedKey().keyMaterial()).isNotBlank();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void theTimestampsAreInstants() {
|
||||||
|
// A timestamp that is in the wrong unit is still a timestamp, so the answers are
|
||||||
|
// checked against a range a clock could have produced.
|
||||||
|
Key key = createAesKey();
|
||||||
|
assertThat(key.createTimestamp()).isAfter(Instant.parse("2020-01-01T00:00:00Z"));
|
||||||
|
assertThat(key.createTimestamp()).isBefore(Instant.parse("2100-01-01T00:00:00Z"));
|
||||||
|
assertThat(key.usageStartTimestamp()).isAfter(Instant.parse("2020-01-01T00:00:00Z"));
|
||||||
|
|
||||||
|
GetParametersForImportResponse parameters = client.getParametersForImport(r -> r
|
||||||
|
.keyMaterialType(KeyMaterialType.KEY_CRYPTOGRAM)
|
||||||
|
.wrappingKeyAlgorithm(KeyAlgorithm.RSA_2048));
|
||||||
|
assertThat(parameters.parametersValidUntilTimestamp())
|
||||||
|
.isAfter(Instant.parse("2020-01-01T00:00:00Z"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void aKeyIsCreatedWithADeriveKeyUsage() {
|
||||||
|
// The usage a derived key is for is named when the key pair is made.
|
||||||
|
Key key = client.createKey(r -> r
|
||||||
|
.keyAttributes(a -> a
|
||||||
|
.keyAlgorithm(KeyAlgorithm.ECC_NIST_P256)
|
||||||
|
.keyClass(KeyClass.ASYMMETRIC_KEY_PAIR)
|
||||||
|
.keyUsage(KeyUsage.TR31_K3_ASYMMETRIC_KEY_FOR_KEY_AGREEMENT)
|
||||||
|
.keyModesOfUse(m -> m.deriveKey(true)))
|
||||||
|
.deriveKeyUsage("TR31_K1_KEY_BLOCK_PROTECTION_KEY")
|
||||||
|
.exportable(true))
|
||||||
|
.key();
|
||||||
|
keyArns.add(key.keyArn());
|
||||||
|
|
||||||
|
assertThat(key.deriveKeyUsage()).isEqualTo(DeriveKeyUsage.TR31_K1_KEY_BLOCK_PROTECTION_KEY);
|
||||||
|
assertThat(client.getKey(r -> r.keyIdentifier(key.keyArn())).key().deriveKeyUsage())
|
||||||
|
.isEqualTo(DeriveKeyUsage.TR31_K1_KEY_BLOCK_PROTECTION_KEY);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** An RSA key pair, for the authority that signs the certificates. */
|
||||||
|
private static KeyPair rsaKeyPair() throws Exception {
|
||||||
|
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
|
||||||
|
generator.initialize(2048);
|
||||||
|
return generator.generateKeyPair();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** An elliptic curve key pair, for the counterparty that agrees on a key. */
|
||||||
|
private static KeyPair eccKeyPair() throws Exception {
|
||||||
|
KeyPairGenerator generator = KeyPairGenerator.getInstance("EC");
|
||||||
|
generator.initialize(new ECGenParameterSpec("secp256r1"));
|
||||||
|
return generator.generateKeyPair();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A certificate over a key pair, signed by an issuer. */
|
||||||
|
private static X509Certificate certificate(KeyPair subject, String subjectName,
|
||||||
|
KeyPair issuer, String issuerName) throws Exception {
|
||||||
|
Instant now = Instant.now();
|
||||||
|
JcaX509v3CertificateBuilder builder = new JcaX509v3CertificateBuilder(
|
||||||
|
new X500Name("CN=" + issuerName), BigInteger.valueOf(now.toEpochMilli()),
|
||||||
|
Date.from(now.minusSeconds(3600)), Date.from(now.plusSeconds(86400)),
|
||||||
|
new X500Name("CN=" + subjectName), subject.getPublic());
|
||||||
|
return new JcaX509CertificateConverter().getCertificate(builder.build(
|
||||||
|
new JcaContentSignerBuilder("SHA256withRSA").build(issuer.getPrivate())));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -46,7 +46,7 @@
|
|||||||
with the key, its state `DELETE_PENDING` and the moment its deletion takes effect.
|
with the key, its state `DELETE_PENDING` and the moment its deletion takes effect.
|
||||||
A key that is gone does not come back: a tagging request for it is refused. The
|
A key that is gone does not come back: a tagging request for it is refused. The
|
||||||
service deletes a key the moment it is asked to, so the waiting period of
|
service deletes a key the moment it is asked to, so the waiting period of
|
||||||
`DeleteKeyInDays` is read and checked but not waited for.
|
`DeleteKeyInDays` is checked against the range the model states and is not waited for.
|
||||||
- **Creating a key:** `CreateKey` takes `Exportable`, `Enabled` and `Tags`, and a key
|
- **Creating a key:** `CreateKey` takes `Exportable`, `Enabled` and `Tags`, and a key
|
||||||
created disabled or with tags comes back that way. The `KeyUsage` and the
|
created disabled or with tags comes back that way. The `KeyUsage` and the
|
||||||
`KeyModesOfUse` of the key are required, as the reference requires them. The answer
|
`KeyModesOfUse` of the key are required, as the reference requires them. The answer
|
||||||
@@ -60,7 +60,8 @@
|
|||||||
refused, and the permission check reads the same member the request does.
|
refused, and the permission check reads the same member the request does.
|
||||||
- **Listing by state:** `ListKeys` takes a `KeyState` and answers with the keys in
|
- **Listing by state:** `ListKeys` takes a `KeyState` and answers with the keys in
|
||||||
that state alone, and with every key when it names none. Each key in the list is the
|
that state alone, and with every key when it names none. Each key in the list is the
|
||||||
whole key rather than the summary the model names, which a client reads as the summary.
|
summary the model names: its identifier, its state, its attributes, its check value
|
||||||
|
and its two flags.
|
||||||
- **The members a request may name that the service does not read:** `CreateKey` and
|
- **The members a request may name that the service does not read:** `CreateKey` and
|
||||||
`ImportKey` take `ReplicationRegions`, and `ImportKey` takes `RequesterComment`, and the
|
`ImportKey` take `ReplicationRegions`, and `ImportKey` takes `RequesterComment`, and the
|
||||||
service reads none of them. No replica is made and no approval is recorded, because the
|
service reads none of them. No replica is made and no approval is recorded, because the
|
||||||
|
|||||||
+30
-6
@@ -146,7 +146,7 @@ public class PaymentCryptographyJsonHandler {
|
|||||||
Pagination.parseNextToken(request, "NextToken", PAGINATION_ERROR));
|
Pagination.parseNextToken(request, "NextToken", PAGINATION_ERROR));
|
||||||
List<Map<String, Object>> keys = new ArrayList<>();
|
List<Map<String, Object>> keys = new ArrayList<>();
|
||||||
for (PaymentCryptographyKey key : page.items()) {
|
for (PaymentCryptographyKey key : page.items()) {
|
||||||
keys.add(keyObject(key));
|
keys.add(keySummary(key));
|
||||||
}
|
}
|
||||||
Map<String, Object> out = new LinkedHashMap<>();
|
Map<String, Object> out = new LinkedHashMap<>();
|
||||||
out.put("Keys", keys);
|
out.put("Keys", keys);
|
||||||
@@ -345,14 +345,28 @@ public class PaymentCryptographyJsonHandler {
|
|||||||
return request.path("AliasName").asText();
|
return request.path("AliasName").asText();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A key as a list answers it.
|
||||||
|
*
|
||||||
|
* <p>A list carries the key's summary and not the whole key: its identifier, its state,
|
||||||
|
* its attributes, its check value and the two flags, which is what the model says a
|
||||||
|
* summary holds.
|
||||||
|
*/
|
||||||
|
private static Map<String, Object> keySummary(PaymentCryptographyKey key) {
|
||||||
|
Map<String, Object> out = new LinkedHashMap<>();
|
||||||
|
out.put("KeyArn", key.getArn());
|
||||||
|
out.put("KeyState", key.getKeyState());
|
||||||
|
out.put("KeyAttributes", keyAttributes(key));
|
||||||
|
out.put("KeyCheckValue", key.getKeyCheckValue());
|
||||||
|
out.put("Enabled", key.isEnabled());
|
||||||
|
out.put("Exportable", key.isExportable());
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
private static Map<String, Object> keyObject(PaymentCryptographyKey key) {
|
private static Map<String, Object> keyObject(PaymentCryptographyKey key) {
|
||||||
Map<String, Object> out = new LinkedHashMap<>();
|
Map<String, Object> out = new LinkedHashMap<>();
|
||||||
out.put("KeyArn", key.getArn());
|
out.put("KeyArn", key.getArn());
|
||||||
Map<String, Object> attributes = new LinkedHashMap<>();
|
Map<String, Object> attributes = keyAttributes(key);
|
||||||
attributes.put("KeyAlgorithm", key.getKeyAlgorithm());
|
|
||||||
attributes.put("KeyClass", key.getKeyClass());
|
|
||||||
attributes.put("KeyUsage", key.getKeyUsage());
|
|
||||||
attributes.put("KeyModesOfUse", answeredModes(key));
|
|
||||||
out.put("KeyAttributes", attributes);
|
out.put("KeyAttributes", attributes);
|
||||||
if (key.getDeriveKeyUsage() != null && !key.getDeriveKeyUsage().isEmpty()) {
|
if (key.getDeriveKeyUsage() != null && !key.getDeriveKeyUsage().isEmpty()) {
|
||||||
out.put("DeriveKeyUsage", key.getDeriveKeyUsage());
|
out.put("DeriveKeyUsage", key.getDeriveKeyUsage());
|
||||||
@@ -374,6 +388,16 @@ public class PaymentCryptographyJsonHandler {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The attributes of a key, as the answers carry them. */
|
||||||
|
private static Map<String, Object> keyAttributes(PaymentCryptographyKey key) {
|
||||||
|
Map<String, Object> attributes = new LinkedHashMap<>();
|
||||||
|
attributes.put("KeyAlgorithm", key.getKeyAlgorithm());
|
||||||
|
attributes.put("KeyClass", key.getKeyClass());
|
||||||
|
attributes.put("KeyUsage", key.getKeyUsage());
|
||||||
|
attributes.put("KeyModesOfUse", answeredModes(key));
|
||||||
|
return attributes;
|
||||||
|
}
|
||||||
|
|
||||||
private static Map<String, Object> aliasObject(PaymentCryptographyAlias alias) {
|
private static Map<String, Object> aliasObject(PaymentCryptographyAlias alias) {
|
||||||
Map<String, Object> out = new LinkedHashMap<>();
|
Map<String, Object> out = new LinkedHashMap<>();
|
||||||
out.put("AliasName", alias.getAliasName());
|
out.put("AliasName", alias.getAliasName());
|
||||||
|
|||||||
+25
@@ -400,6 +400,31 @@ class PaymentCryptographyIntegrationTest {
|
|||||||
.body("Keys.KeyArn", hasItem(equalTo(arn)));
|
.body("Keys.KeyArn", hasItem(equalTo(arn)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void aListedKeyIsItsSummary() {
|
||||||
|
// A list carries the key's summary and not the whole key: the identifier, the state,
|
||||||
|
// the attributes, the check value and the two flags.
|
||||||
|
String arn = call("CreateKey", CREATE_KEY).then().statusCode(200)
|
||||||
|
.extract().path("Key.KeyArn");
|
||||||
|
|
||||||
|
call("ListKeys", "{}")
|
||||||
|
.then().statusCode(200)
|
||||||
|
.body("Keys.find { it.KeyArn == '" + arn + "' }.KeyState",
|
||||||
|
equalTo("CREATE_COMPLETE"))
|
||||||
|
.body("Keys.find { it.KeyArn == '" + arn + "' }.KeyAttributes.KeyAlgorithm",
|
||||||
|
equalTo("AES_256"))
|
||||||
|
.body("Keys.find { it.KeyArn == '" + arn + "' }.KeyAttributes.KeyUsage",
|
||||||
|
equalTo("TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY"))
|
||||||
|
.body("Keys.find { it.KeyArn == '" + arn + "' }.KeyCheckValue", notNullValue())
|
||||||
|
.body("Keys.find { it.KeyArn == '" + arn + "' }.Enabled", equalTo(true))
|
||||||
|
.body("Keys.find { it.KeyArn == '" + arn + "' }.Exportable", equalTo(true))
|
||||||
|
// The summary holds no timestamp and no origin: those belong to the whole
|
||||||
|
// key, which the list does not carry.
|
||||||
|
.body("Keys.find { it.KeyArn == '" + arn + "' }.CreateTimestamp", nullValue())
|
||||||
|
.body("Keys.find { it.KeyArn == '" + arn + "' }.KeyOrigin", nullValue())
|
||||||
|
.body("Keys.find { it.KeyArn == '" + arn + "' }.UsageStartTimestamp", nullValue());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void anAliasIsCreatedAndListed() {
|
void anAliasIsCreatedAndListed() {
|
||||||
String arn = call("CreateKey", CREATE_KEY).then().statusCode(200)
|
String arn = call("CreateKey", CREATE_KEY).then().statusCode(200)
|
||||||
|
|||||||
+11
@@ -395,6 +395,17 @@ class PaymentCryptographyKeyExchangeTest {
|
|||||||
"the private key was written to the store");
|
"the private key was written to the store");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void aKeyPairHasACertificate() {
|
||||||
|
PaymentCryptographyKey key = createKeyPair("RSA_2048");
|
||||||
|
|
||||||
|
Map<String, Object> answer = service.publicKeyCertificate(key);
|
||||||
|
|
||||||
|
String certificate = (String) answer.get("KeyCertificate");
|
||||||
|
assertNotNull(certificate);
|
||||||
|
assertNotNull(Certificates.parseCertificate(certificate));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void aFormTheServiceDoesNotKnowIsRefused() {
|
void aFormTheServiceDoesNotKnowIsRefused() {
|
||||||
PaymentCryptographyKey key = createKey("AES_256", "TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY");
|
PaymentCryptographyKey key = createKey("AES_256", "TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY");
|
||||||
|
|||||||
+16
@@ -174,6 +174,22 @@ class PaymentCryptographyServiceTest {
|
|||||||
"the two pairs are different");
|
"the two pairs are different");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void aKeyPairCarriesTheUsageItsDerivedKeysHave() {
|
||||||
|
// The usage a derived key is for is named when the key pair is made, as the export
|
||||||
|
// guide's key agreement flow does.
|
||||||
|
Map<String, Object> attributes = new LinkedHashMap<>();
|
||||||
|
attributes.put("KeyAlgorithm", "ECC_NIST_P256");
|
||||||
|
attributes.put("KeyClass", "ASYMMETRIC_KEY_PAIR");
|
||||||
|
attributes.put("KeyUsage", "TR31_K3_ASYMMETRIC_KEY_FOR_KEY_AGREEMENT");
|
||||||
|
attributes.put("DeriveKeyUsage", "TR31_K1_KEY_BLOCK_PROTECTION_KEY");
|
||||||
|
|
||||||
|
PaymentCryptographyKey key = service.createKey("us-east-1", attributes,
|
||||||
|
Map.of("DeriveKey", Boolean.TRUE), true, true, Map.of());
|
||||||
|
|
||||||
|
assertEquals("TR31_K1_KEY_BLOCK_PROTECTION_KEY", key.getDeriveKeyUsage());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void everyAsymmetricUsageTakesAKeyPair() {
|
void everyAsymmetricUsageTakesAKeyPair() {
|
||||||
// The usages the model names for a key pair, each of which is made.
|
// The usages the model names for a key pair, each of which is made.
|
||||||
|
|||||||
Reference in new issue
Block a user