mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(paymentcryptography): the TR-34 key block (#8136)
* feat(paymentcryptography): the TR-34 key block * fix(paymentcryptography): the rules a TR-34 key block has to follow * docs(paymentcryptography): what a TR-34 key block is * test(paymentcryptography): the TR-34 round trip shows the key that came back * test(paymentcryptography): one exchange for the TR-34 tests * fix(paymentcryptography): the nonce and the token a TR-34 import names * docs(paymentcryptography): the nonce and the token a TR-34 import names * fix(paymentcryptography): a TR-34 key block is read whatever follows it * fix(paymentcryptography): a TR-34 header carries the optional blocks * chore(paymentcryptography): the imports of the round-trip test in order * fix(paymentcryptography): the TR-34 header is the reference's header * fix(paymentcryptography): the TR-34 header, stale comments * fix(paymentcryptography): the TR-34 comments match the code
This commit is contained in:
1 parent
8185a340c4
commit
4cd8ab991e
10 files changed
+1928
-31
No files matched your search
+62
-4
@@ -231,15 +231,31 @@ class PaymentCryptographyIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRequestNamingAFormTheServiceDoesNotServeIsRefused() {
|
||||
// The TR-34 form is one the model has and this service does not make.
|
||||
void theTr34FormIsServed() {
|
||||
// The TR-34 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\":{"
|
||||
+ "\"Tr34KeyBlock\":{\"CertificateAuthorityPublicKeyIdentifier\":\"" + arn
|
||||
+ "\",\"WrappingKeyCertificate\":\"x\",\"KeyBlockFormat\":\"X9_TR34_2012\"}}}")
|
||||
+ "\"Tr34KeyBlock\":{\"WrappingKeyCertificate\":\"x\","
|
||||
+ "\"KeyBlockFormat\":\"X9_TR34_2012\"}}}")
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
.body("message", containsString("CertificateAuthorityPublicKeyIdentifier"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRequestNamingAFormTheServiceDoesNotServeIsRefused() {
|
||||
// The AS2805 form is one the model has and this service does not make yet.
|
||||
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 + "\"}}}")
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
.body("message", containsString("not a form the service exports"));
|
||||
@@ -370,6 +386,48 @@ class PaymentCryptographyIntegrationTest {
|
||||
* The refusal a caller sees when a request is missing a member the model requires: the
|
||||
* shape check's, because the shape check runs before the operation does.
|
||||
*/
|
||||
@Test
|
||||
void theModelCheckRefusesATr34ExportWithoutItsWrappingKeyCertificate() {
|
||||
String arn = call("CreateKey", CREATE_KEY)
|
||||
.then().statusCode(200)
|
||||
.extract().path("Key.KeyArn");
|
||||
|
||||
Response answer = call("ExportKey", "{\"ExportKeyIdentifier\":\"" + arn + "\",\"KeyMaterial\":{"
|
||||
+ "\"Tr34KeyBlock\":{\"CertificateAuthorityPublicKeyIdentifier\":\"" + arn + "\","
|
||||
+ "\"KeyBlockFormat\":\"X9_TR34_2012\"}}}");
|
||||
|
||||
assertTheModelRefused(answer, "WrappingKeyCertificate");
|
||||
}
|
||||
|
||||
@Test
|
||||
void theModelCheckRefusesATr34ExportWithoutItsCertificateAuthority() {
|
||||
Response answer = call("ExportKey", "{\"ExportKeyIdentifier\":\"" + keyArn() + "\","
|
||||
+ "\"KeyMaterial\":{\"Tr34KeyBlock\":{\"WrappingKeyCertificate\":\"x\","
|
||||
+ "\"KeyBlockFormat\":\"X9_TR34_2012\"}}}");
|
||||
|
||||
assertTheModelRefused(answer, "CertificateAuthorityPublicKeyIdentifier");
|
||||
}
|
||||
|
||||
@Test
|
||||
void theModelCheckRefusesATr34ImportWithoutItsSigningKeyCertificate() {
|
||||
Response answer = call("ImportKey", "{\"KeyMaterial\":{\"Tr34KeyBlock\":{"
|
||||
+ "\"CertificateAuthorityPublicKeyIdentifier\":\"" + keyArn() + "\","
|
||||
+ "\"WrappedKeyBlock\":\"" + "0".repeat(64) + "\","
|
||||
+ "\"KeyBlockFormat\":\"X9_TR34_2012\"}}}");
|
||||
|
||||
assertTheModelRefused(answer, "SigningKeyCertificate");
|
||||
}
|
||||
|
||||
@Test
|
||||
void theModelCheckRefusesATr34ImportWithoutItsWrappedKeyBlock() {
|
||||
Response answer = call("ImportKey", "{\"KeyMaterial\":{\"Tr34KeyBlock\":{"
|
||||
+ "\"CertificateAuthorityPublicKeyIdentifier\":\"" + keyArn() + "\","
|
||||
+ "\"SigningKeyCertificate\":\"x\","
|
||||
+ "\"KeyBlockFormat\":\"X9_TR34_2012\"}}}");
|
||||
|
||||
assertTheModelRefused(answer, "WrappedKeyBlock");
|
||||
}
|
||||
|
||||
private static void assertTheModelRefused(Response answer, String member) {
|
||||
answer.then().statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
|
||||
+427
@@ -7,7 +7,9 @@ import io.floci.aws.core.storage.InMemoryStorage;
|
||||
import io.floci.aws.services.acm.CertificateGenerator;
|
||||
import io.floci.aws.services.paymentcryptography.crypto.Certificates;
|
||||
import io.floci.aws.services.paymentcryptography.crypto.CertificatesTest;
|
||||
import io.floci.aws.services.paymentcryptography.crypto.PaymentCryptographyTr34RoundTripTest;
|
||||
import io.floci.aws.services.paymentcryptography.crypto.RsaOaep;
|
||||
import io.floci.aws.services.paymentcryptography.crypto.Tr34;
|
||||
import io.floci.aws.services.paymentcryptography.model.PaymentCryptographyKey;
|
||||
import io.floci.aws.testing.ManualClock;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -414,6 +416,431 @@ class PaymentCryptographyKeyExchangeTest {
|
||||
() -> service.exportKey("us-east-1", key.getKeyId(), "Nonsense", Map.of(), "", ""));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
}
|
||||
/**
|
||||
* The exchange a TR-34 block goes through.
|
||||
*
|
||||
* <p>The parameters, the two authorities and the material of the export and of the
|
||||
* import are built here, so that a change to the TR-34 request is made in one place
|
||||
* and not in every test that makes a block.
|
||||
*
|
||||
* @param wrappingAlgorithm the algorithm of the receiver's key
|
||||
*/
|
||||
private Tr34Exchange tr34Exchange(String wrappingAlgorithm) {
|
||||
Map<String, Object> exportParameters = service.parametersForExport("TR34_KEY_BLOCK",
|
||||
"RSA_2048", false);
|
||||
Map<String, Object> importParameters = service.parametersForImport("TR34_KEY_BLOCK",
|
||||
wrappingAlgorithm, false);
|
||||
// The receiver's authority vouches for the certificate the key is wrapped under; the
|
||||
// sender's authority vouches for the certificate the block was signed with.
|
||||
PaymentCryptographyKey receiver = createAuthority(
|
||||
(String) importParameters.get("WrappingKeyCertificate"));
|
||||
PaymentCryptographyKey sender = createAuthority(
|
||||
(String) exportParameters.get("SigningKeyCertificate"));
|
||||
|
||||
Map<String, Object> exportMaterial = new LinkedHashMap<>();
|
||||
exportMaterial.put("ExportToken", exportParameters.get("ExportToken"));
|
||||
exportMaterial.put("WrappingKeyCertificate", importParameters.get("WrappingKeyCertificate"));
|
||||
exportMaterial.put("CertificateAuthorityPublicKeyIdentifier", receiver.getKeyId());
|
||||
exportMaterial.put("KeyBlockFormat", "X9_TR34_2012");
|
||||
exportMaterial.put("RandomNonce", "A1B2C3D4E5F60718");
|
||||
|
||||
return new Tr34Exchange(exportParameters, importParameters, exportMaterial, receiver, sender);
|
||||
}
|
||||
|
||||
/** The exchange a TR-34 block goes through, under the receiver's RSA-2048 key. */
|
||||
private Tr34Exchange tr34Exchange() {
|
||||
return tr34Exchange("RSA_2048");
|
||||
}
|
||||
|
||||
/** The parameters, the authorities and the material of a TR-34 exchange. */
|
||||
private record Tr34Exchange(Map<String, Object> exportParameters,
|
||||
Map<String, Object> importParameters,
|
||||
Map<String, Object> exportMaterial,
|
||||
PaymentCryptographyKey receiver,
|
||||
PaymentCryptographyKey sender) {
|
||||
|
||||
/** The material of a TR-34 import, for the block an export made. */
|
||||
Map<String, Object> importMaterial(String block) {
|
||||
Map<String, Object> incoming = new LinkedHashMap<>();
|
||||
incoming.put("ImportToken", importParameters.get("ImportToken"));
|
||||
incoming.put("SigningKeyCertificate", exportParameters.get("SigningKeyCertificate"));
|
||||
incoming.put("WrappedKeyBlock", block);
|
||||
incoming.put("CertificateAuthorityPublicKeyIdentifier", sender.getKeyId());
|
||||
incoming.put("KeyBlockFormat", "X9_TR34_2012");
|
||||
incoming.put("RandomNonce", "A1B2C3D4E5F60718");
|
||||
return incoming;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKeyThatLeavesUnderACertificateComesBack() {
|
||||
// The service that exports mints the parameters; the service that imports holds the
|
||||
// token that names the key the block is wrapped under.
|
||||
Tr34Exchange exchange = tr34Exchange();
|
||||
PaymentCryptographyKey key = createKey("TDES_2KEY",
|
||||
"TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY");
|
||||
|
||||
Map<String, Object> answer = service.exportKey("us-east-1", key.getKeyId(), "Tr34KeyBlock",
|
||||
exchange.exportMaterial(), "", "");
|
||||
Map<?, ?> wrapped = (Map<?, ?>) answer.get("WrappedKey");
|
||||
assertEquals("TR34_KEY_BLOCK", wrapped.get("WrappedKeyMaterialFormat"));
|
||||
|
||||
PaymentCryptographyKey imported = service.importKey("us-east-1", "Tr34KeyBlock",
|
||||
exchange.importMaterial((String) wrapped.get("KeyMaterial")), "", true, Map.of());
|
||||
|
||||
assertEquals(key.getKeyMaterialHex(), imported.getKeyMaterialHex());
|
||||
assertEquals(key.getKeyUsage(), imported.getKeyUsage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theTr34HeaderCarriesTheExportabilityTheRequestNames() throws Exception {
|
||||
// The request may narrow the exportability of the key, and the block carries the
|
||||
// narrowed one, so the key that comes back is one that may not leave again.
|
||||
Tr34Exchange exchange = tr34Exchange();
|
||||
PaymentCryptographyKey key = createKey("TDES_2KEY",
|
||||
"TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY");
|
||||
exchange.exportMaterial().put("KeyBlockHeaders", Map.of("KeyExportability", "NON_EXPORTABLE"));
|
||||
|
||||
Map<String, Object> answer = service.exportKey("us-east-1", key.getKeyId(), "Tr34KeyBlock",
|
||||
exchange.exportMaterial(), "", "");
|
||||
Map<?, ?> wrapped = (Map<?, ?>) answer.get("WrappedKey");
|
||||
|
||||
PaymentCryptographyKey imported = service.importKey("us-east-1", "Tr34KeyBlock",
|
||||
exchange.importMaterial((String) wrapped.get("KeyMaterial")), "", true, Map.of());
|
||||
|
||||
assertFalse(imported.isExportable(),
|
||||
"the key came back exportable although the request narrowed it");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTr34ExportCarriesTheOptionalBlocks() throws Exception {
|
||||
// The header of a TR-34 block carries the optional blocks the request names, the
|
||||
// way the header of a TR-31 block does, and the padding block that keeps the
|
||||
// header a whole number of cipher blocks is the last of them.
|
||||
Tr34Exchange exchange = tr34Exchange("RSA_3072");
|
||||
PaymentCryptographyKey key = createKey("AES_128",
|
||||
"TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY");
|
||||
exchange.exportMaterial().put("KeyBlockHeaders",
|
||||
Map.of("OptionalBlocks", Map.of("AB", "0011")));
|
||||
|
||||
Map<String, Object> answer = service.exportKey("us-east-1", key.getKeyId(), "Tr34KeyBlock",
|
||||
exchange.exportMaterial(), "", "");
|
||||
Map<?, ?> wrapped = (Map<?, ?>) answer.get("WrappedKey");
|
||||
|
||||
// The header travels in the clear, so it can be read without opening the block.
|
||||
String header = PaymentCryptographyTr34RoundTripTest.clearHeader(
|
||||
HexFormat.of().parseHex((String) wrapped.get("KeyMaterial")));
|
||||
// The version has no meaning in a TR-34 block, and the reference writes the same
|
||||
// one every time.
|
||||
assertEquals("D", header.substring(0, 1), "the version of the header");
|
||||
// The header says how many optional blocks there are, and the padding block is one
|
||||
// of them.
|
||||
assertEquals("02", header.substring(12, 14), "the number of optional blocks");
|
||||
assertTrue(header.endsWith("AB080011PB080000"), "the optional blocks are not in the header");
|
||||
|
||||
PaymentCryptographyKey imported = service.importKey("us-east-1", "Tr34KeyBlock",
|
||||
exchange.importMaterial((String) wrapped.get("KeyMaterial")), "", true, Map.of());
|
||||
|
||||
assertEquals(key.getKeyMaterialHex(), imported.getKeyMaterialHex());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTr34BlockWithoutANonceComesBack() {
|
||||
// The nonce is optional in the model, so a block the service made without one
|
||||
// comes back without one.
|
||||
Tr34Exchange exchange = tr34Exchange();
|
||||
PaymentCryptographyKey key = createKey("TDES_2KEY",
|
||||
"TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY");
|
||||
exchange.exportMaterial().remove("RandomNonce");
|
||||
|
||||
Map<String, Object> answer = service.exportKey("us-east-1", key.getKeyId(), "Tr34KeyBlock",
|
||||
exchange.exportMaterial(), "", "");
|
||||
Map<?, ?> wrapped = (Map<?, ?>) answer.get("WrappedKey");
|
||||
|
||||
Map<String, Object> incoming = exchange.importMaterial((String) wrapped.get("KeyMaterial"));
|
||||
incoming.remove("RandomNonce");
|
||||
|
||||
PaymentCryptographyKey imported = service.importKey("us-east-1", "Tr34KeyBlock", incoming,
|
||||
"", true, Map.of());
|
||||
|
||||
assertEquals(key.getKeyMaterialHex(), imported.getKeyMaterialHex());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTr34BlockWithANonceDoesNotComeBackWithoutIt() {
|
||||
// The nonce binds the block to the exchange, so a request that names none expects
|
||||
// a block that carries none, and a block that carries one is refused.
|
||||
Tr34Exchange exchange = tr34Exchange();
|
||||
PaymentCryptographyKey key = createKey("TDES_2KEY",
|
||||
"TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY");
|
||||
|
||||
Map<String, Object> answer = service.exportKey("us-east-1", key.getKeyId(), "Tr34KeyBlock",
|
||||
exchange.exportMaterial(), "", "");
|
||||
Map<?, ?> wrapped = (Map<?, ?>) answer.get("WrappedKey");
|
||||
|
||||
Map<String, Object> incoming = exchange.importMaterial((String) wrapped.get("KeyMaterial"));
|
||||
incoming.remove("RandomNonce");
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> service.importKey("us-east-1", "Tr34KeyBlock", incoming, "", true, Map.of()));
|
||||
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTr34ImportWithoutAnImportTokenIsRefused() {
|
||||
// The service imports a block under the token the import parameters issued, so a
|
||||
// request that names none is refused with the reason, not with a key that is not
|
||||
// found.
|
||||
Tr34Exchange exchange = tr34Exchange();
|
||||
PaymentCryptographyKey key = createKey("TDES_2KEY",
|
||||
"TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY");
|
||||
|
||||
Map<String, Object> answer = service.exportKey("us-east-1", key.getKeyId(), "Tr34KeyBlock",
|
||||
exchange.exportMaterial(), "", "");
|
||||
Map<?, ?> wrapped = (Map<?, ?>) answer.get("WrappedKey");
|
||||
|
||||
Map<String, Object> incoming = exchange.importMaterial((String) wrapped.get("KeyMaterial"));
|
||||
incoming.remove("ImportToken");
|
||||
incoming.put("WrappingKeyIdentifier", exchange.receiver().getKeyId());
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> service.importKey("us-east-1", "Tr34KeyBlock", incoming, "", true, Map.of()));
|
||||
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
assertTrue(refused.getMessage().contains("import token"), refused.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTr34ExportWithoutAnExportTokenIsRefused() {
|
||||
// The service signs the block with the key the export parameters issued, so a
|
||||
// request that names no token is refused with the reason, not with a key that is
|
||||
// not found.
|
||||
Tr34Exchange exchange = tr34Exchange();
|
||||
PaymentCryptographyKey key = createKey("TDES_2KEY",
|
||||
"TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY");
|
||||
exchange.exportMaterial().remove("ExportToken");
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> service.exportKey("us-east-1", key.getKeyId(), "Tr34KeyBlock",
|
||||
exchange.exportMaterial(), "", ""));
|
||||
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
assertTrue(refused.getMessage().contains("export token"), refused.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theImportParametersRefusalNamesEveryTypeItTakes() {
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> service.parametersForImport("TR31_KEY_BLOCK", "RSA_2048", false));
|
||||
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
assertTrue(refused.getMessage().contains("KEY_CRYPTOGRAM, TR34_KEY_BLOCK"),
|
||||
refused.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTr34BlockWithAnUnknownExportabilityIsRefused() throws Exception {
|
||||
// A caller that signs its own block can put an exportability the service does not
|
||||
// read into the header, and the service refuses it rather than taking it for an
|
||||
// exportable one.
|
||||
Tr34Exchange exchange = tr34Exchange();
|
||||
Crafted crafted = craftedBlock(exchange, "B9999D0ATB00X0000",
|
||||
HexFormat.of().parseHex("000102030405060708090A0B0C0D0E0F"), false);
|
||||
|
||||
Map<String, Object> incoming = exchange.importMaterial(crafted.block());
|
||||
incoming.put("SigningKeyCertificate", crafted.signingCertificate());
|
||||
incoming.put("CertificateAuthorityPublicKeyIdentifier", crafted.authority().getKeyId());
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> service.importKey("us-east-1", "Tr34KeyBlock", incoming, "", true, Map.of()));
|
||||
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
assertTrue(refused.getMessage().contains("exportability"), refused.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTr34BlockUnderATooWeakTokenIsRefused() throws Exception {
|
||||
// The import token's own key protects the key that comes in, so a key stronger
|
||||
// than the token may not come in under it.
|
||||
Tr34Exchange exchange = tr34Exchange();
|
||||
Crafted crafted = craftedBlock(exchange, "D9999D0AB00E0000",
|
||||
HexFormat.of().parseHex("000102030405060708090A0B0C0D0E0F"
|
||||
+ "101112131415161718191A1B1C1D1E1F"), true);
|
||||
|
||||
Map<String, Object> incoming = exchange.importMaterial(crafted.block());
|
||||
incoming.put("SigningKeyCertificate", crafted.signingCertificate());
|
||||
incoming.put("CertificateAuthorityPublicKeyIdentifier", crafted.authority().getKeyId());
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> service.importKey("us-east-1", "Tr34KeyBlock", incoming, "", true, Map.of()));
|
||||
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAesKeyLeavesUnderATr34BlockUnderItsOwnCipher() {
|
||||
// The key block is enciphered with the cipher the key needs, so an AES key is not
|
||||
// protected by something weaker than itself. An AES-128 key needs a receiver whose
|
||||
// certificate is RSA-3072 or RSA-4096.
|
||||
Tr34Exchange exchange = tr34Exchange("RSA_3072");
|
||||
PaymentCryptographyKey key = createKey("AES_128",
|
||||
"TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY");
|
||||
|
||||
Map<String, Object> answer = service.exportKey("us-east-1", key.getKeyId(), "Tr34KeyBlock",
|
||||
exchange.exportMaterial(), "", "");
|
||||
Map<?, ?> wrapped = (Map<?, ?>) answer.get("WrappedKey");
|
||||
// The content encryption is the AES one, and not the Triple DES one.
|
||||
assertTrue(((String) wrapped.get("KeyMaterial")).contains("608648016503040102"),
|
||||
"the block does not name the AES content encryption");
|
||||
|
||||
PaymentCryptographyKey imported = service.importKey("us-east-1", "Tr34KeyBlock",
|
||||
exchange.importMaterial((String) wrapped.get("KeyMaterial")), "", true, Map.of());
|
||||
|
||||
assertEquals(key.getKeyMaterialHex(), imported.getKeyMaterialHex());
|
||||
}
|
||||
|
||||
/** A block the test builds itself, and the authority that vouches for its signature. */
|
||||
private record Crafted(String block, PaymentCryptographyKey authority,
|
||||
String signingCertificate) {
|
||||
}
|
||||
|
||||
/**
|
||||
* A TR-34 block the test builds and signs itself.
|
||||
*
|
||||
* <p>The service signs the blocks it makes, so a block that carries something the
|
||||
* service would never write is put to it by a caller that signs its own.
|
||||
*
|
||||
* @param importParameters the import parameters, whose certificate the block is wrapped under
|
||||
* @param header the TR-31 header the block carries
|
||||
* @param clearKey the key the block carries
|
||||
* @param aes whether the key is an AES one
|
||||
* @return the block, the authority that vouches for its signature and the certificate
|
||||
*/
|
||||
/**
|
||||
* A TR-34 block the test builds and signs itself.
|
||||
*
|
||||
* <p>The service signs the blocks it makes, so a block that carries something the
|
||||
* service would never write is put to it by a caller that signs its own.
|
||||
*
|
||||
* @param exchange the exchange the block travels through
|
||||
* @param header the TR-31 header the block carries
|
||||
* @param clearKey the key the block carries
|
||||
* @param aes whether the key is an AES one
|
||||
* @return the block, the authority that vouches for its signature and the certificate
|
||||
*/
|
||||
private Crafted craftedBlock(Tr34Exchange exchange, String header, byte[] clearKey,
|
||||
boolean aes) throws Exception {
|
||||
SecureRandom random = new SecureRandom();
|
||||
KeyPair signer = PaymentCryptographyKeyPairs.rsa(2048, random);
|
||||
Certificates.Minted certificate = CertificatesTest.mint(signer.getPublic(), "floci sender",
|
||||
signer.getPrivate(), "floci sender");
|
||||
PaymentCryptographyKey authority = createAuthority(certificate.pem());
|
||||
X509Certificate wrapping = Certificates.parseCertificate(
|
||||
(String) exchange.importParameters().get("WrappingKeyCertificate"));
|
||||
byte[] block = Tr34.build(clearKey, header,
|
||||
HexFormat.of().parseHex("A1B2C3D4E5F60718"), signer.getPrivate(),
|
||||
wrapping.getPublicKey(), null, certificate.der(), aes);
|
||||
return new Crafted(HexFormat.of().withUpperCase().formatHex(block), authority,
|
||||
certificate.pem());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aBlockThatWasSignedByAnotherAuthorityDoesNotOpen() {
|
||||
Tr34Exchange exchange = tr34Exchange();
|
||||
PaymentCryptographyKey key = createKey("TDES_2KEY",
|
||||
"TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY");
|
||||
|
||||
Map<String, Object> answer = service.exportKey("us-east-1", key.getKeyId(), "Tr34KeyBlock",
|
||||
exchange.exportMaterial(), "", "");
|
||||
Map<?, ?> wrapped = (Map<?, ?>) answer.get("WrappedKey");
|
||||
|
||||
// Another authority signs it, so the signature does not verify against this one.
|
||||
SecureRandom random = new SecureRandom();
|
||||
KeyPair other = PaymentCryptographyKeyPairs.rsa(2048, random);
|
||||
Certificates.Minted otherCertificate = CertificatesTest.mint(other.getPublic(),
|
||||
"floci other", other.getPrivate(), "floci other");
|
||||
|
||||
Map<String, Object> incoming = exchange.importMaterial((String) wrapped.get("KeyMaterial"));
|
||||
incoming.put("SigningKeyCertificate", otherCertificate.pem());
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> service.importKey("us-east-1", "Tr34KeyBlock", incoming, "", 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) {
|
||||
// The block is signed with the key the export parameters issue, and the block's
|
||||
// signature is an RSA one, so a signing key that is not an RSA_2048 one cannot
|
||||
// sign it.
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> service.parametersForExport("TR34_KEY_BLOCK", algorithm, false));
|
||||
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
assertTrue(refused.getMessage().contains("signed with RSA_2048"), refused.getMessage());
|
||||
}
|
||||
@Test
|
||||
void aTr34BlockThatCarriesNoHeaderIsRefused() throws Exception {
|
||||
// A block that is signed correctly but whose key block names no key says nothing
|
||||
// about what it carries, so it is refused rather than read.
|
||||
Tr34Exchange exchange = tr34Exchange();
|
||||
Crafted crafted = craftedBlock(exchange, "",
|
||||
HexFormat.of().parseHex("000102030405060708090A0B0C0D0E0F"), false);
|
||||
|
||||
Map<String, Object> incoming = exchange.importMaterial(crafted.block());
|
||||
incoming.put("SigningKeyCertificate", crafted.signingCertificate());
|
||||
incoming.put("CertificateAuthorityPublicKeyIdentifier", crafted.authority().getKeyId());
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> service.importKey("us-east-1", "Tr34KeyBlock", incoming, "", true, Map.of()));
|
||||
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anInitialKeyThatLeavesUnderATr34BlockIsSensitive() throws Exception {
|
||||
// The initial key of the DUKPT derivation is sensitive, so the header that names it
|
||||
// says so, whichever form the key leaves in.
|
||||
Map<String, Object> attributes = new LinkedHashMap<>();
|
||||
attributes.put("KeyAlgorithm", "TDES_2KEY");
|
||||
attributes.put("KeyClass", "SYMMETRIC_KEY");
|
||||
attributes.put("KeyUsage", "TR31_B0_BASE_DERIVATION_KEY");
|
||||
PaymentCryptographyKey base = service.createKey("us-east-1", attributes,
|
||||
Map.of("DeriveKey", Boolean.TRUE), false, true, Map.of());
|
||||
|
||||
Tr34Exchange exchange = tr34Exchange();
|
||||
|
||||
Map<String, Object> answer = service.exportKey("us-east-1", base.getKeyId(), "Tr34KeyBlock",
|
||||
exchange.exportMaterial(), "12345678900000000001", "");
|
||||
Map<?, ?> wrapped = (Map<?, ?>) answer.get("WrappedKey");
|
||||
|
||||
// The header travels in the clear, so it can be read without opening the block.
|
||||
String header = PaymentCryptographyTr34RoundTripTest.clearHeader(
|
||||
HexFormat.of().parseHex((String) wrapped.get("KeyMaterial")));
|
||||
|
||||
assertEquals("B1", header.substring(5, 7), "the usage of the derived key");
|
||||
assertEquals("S", header.substring(11, 12), "the derived key is a sensitive one");
|
||||
}
|
||||
|
||||
@Test
|
||||
void anImportTokenIsRefusedForAnotherKindOfMaterial() {
|
||||
// A token is issued for one kind of material, so it cannot be spent on another.
|
||||
Map<String, Object> parameters = service.parametersForImport("KEY_CRYPTOGRAM", "RSA_2048",
|
||||
false);
|
||||
String token = (String) parameters.get("ImportToken");
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> service.importKey("us-east-1", "Tr34KeyBlock", Map.of(
|
||||
"ImportToken", token,
|
||||
"CertificateAuthorityPublicKeyIdentifier", protection.getKeyId(),
|
||||
"KeyBlockFormat", "X9_TR34_2012",
|
||||
"RandomNonce", "0102030405060708",
|
||||
"SigningKeyCertificate", "x",
|
||||
"WrappedKeyBlock", "00"), "", true, Map.of()));
|
||||
|
||||
assertEquals("ResourceNotFoundException", refused.getErrorCode());
|
||||
}
|
||||
|
||||
/** A symmetric key of the given algorithm and usage. */
|
||||
private PaymentCryptographyKey createKey(String algorithm, String usage) {
|
||||
|
||||
+304
@@ -0,0 +1,304 @@
|
||||
package io.floci.aws.services.paymentcryptography.crypto;
|
||||
|
||||
import org.bouncycastle.asn1.ASN1Encodable;
|
||||
import org.bouncycastle.asn1.ASN1EncodableVector;
|
||||
import org.bouncycastle.asn1.ASN1InputStream;
|
||||
import org.bouncycastle.asn1.ASN1Integer;
|
||||
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
|
||||
import org.bouncycastle.asn1.ASN1OctetString;
|
||||
import org.bouncycastle.asn1.ASN1Sequence;
|
||||
import org.bouncycastle.asn1.ASN1Set;
|
||||
import org.bouncycastle.asn1.ASN1TaggedObject;
|
||||
import org.bouncycastle.asn1.DEROctetString;
|
||||
import org.bouncycastle.asn1.DERSequence;
|
||||
import org.bouncycastle.asn1.DERSet;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.time.Instant;
|
||||
import java.util.HexFormat;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* The TR-34 builder, against the TR-34 verifier.
|
||||
*
|
||||
* <p>The two are the mirror of each other: what the builder produces, the verifier recovers.
|
||||
* A block that one makes and the other cannot read is a block the service would reject.
|
||||
*/
|
||||
public class PaymentCryptographyTr34RoundTripTest {
|
||||
|
||||
/** The hexadecimal the block is written in. */
|
||||
private static final HexFormat HEX = HexFormat.of();
|
||||
|
||||
/** The hexadecimal the block is written in, in the capitals the service uses. */
|
||||
private static final HexFormat HEX_UPPER = HexFormat.of().withUpperCase();
|
||||
|
||||
@Test
|
||||
void buildsWhatTheVerifierRecovers() throws Exception {
|
||||
// The key receiving device: its key pair and its certificate.
|
||||
KeyPair krd = rsaKeyPair();
|
||||
// The key distribution host: the service's signing key and its certificate.
|
||||
KeyPair kdh = rsaKeyPair();
|
||||
Certificates.Minted kdhCertificate = CertificatesTest.mint(kdh.getPublic(), "kdh",
|
||||
kdh.getPrivate(), "kdh");
|
||||
|
||||
byte[] clear = HEX.parseHex("404142434445464748494A4B4C4D4E4F");
|
||||
String header = "B0010P0TB00E0000";
|
||||
byte[] nonce = HEX.parseHex("0102030405060708");
|
||||
|
||||
byte[] block = Tr34.build(clear, header, nonce, kdh.getPrivate(), krd.getPublic());
|
||||
assertNotNull(block, "the block was not built");
|
||||
|
||||
Tr34.Recovered recovered = Tr34.recoverAndVerify(HEX_UPPER.formatHex(block),
|
||||
krd.getPrivate(), kdhCertificate.pem(), null, HEX_UPPER.formatHex(nonce));
|
||||
|
||||
assertEquals(HEX_UPPER.formatHex(clear), recovered.keyHex);
|
||||
assertEquals(header, recovered.header);
|
||||
}
|
||||
|
||||
@Test
|
||||
void carriesTheHeader() throws Exception {
|
||||
KeyPair krd = rsaKeyPair();
|
||||
KeyPair kdh = rsaKeyPair();
|
||||
Certificates.Minted kdhCertificate = CertificatesTest.mint(kdh.getPublic(), "kdh",
|
||||
kdh.getPrivate(), "kdh");
|
||||
|
||||
byte[] block = Tr34.build(HEX.parseHex("404142434445464748494A4B4C4D4E4F"),
|
||||
"B0010P0TB00E0000", null, kdh.getPrivate(), krd.getPublic());
|
||||
|
||||
Tr34.Recovered recovered = Tr34.recoverAndVerify(HEX_UPPER.formatHex(block),
|
||||
krd.getPrivate(), kdhCertificate.pem(), null, null);
|
||||
|
||||
assertEquals("B0010P0TB00E0000", recovered.header);
|
||||
}
|
||||
|
||||
@Test
|
||||
void namesBothPartiesByTheirCertificates() throws Exception {
|
||||
KeyPair krd = rsaKeyPair();
|
||||
KeyPair kdh = rsaKeyPair();
|
||||
Certificates.Minted krdCertificate = CertificatesTest.mint(krd.getPublic(), "krd",
|
||||
krd.getPrivate(), "krd");
|
||||
Certificates.Minted kdhCertificate = CertificatesTest.mint(kdh.getPublic(), "kdh",
|
||||
kdh.getPrivate(), "kdh");
|
||||
|
||||
byte[] block = Tr34.build(HEX.parseHex("404142434445464748494A4B4C4D4E4F"),
|
||||
"B0010P0TB00E0000", null, kdh.getPrivate(), krd.getPublic(),
|
||||
krdCertificate.der(), kdhCertificate.der());
|
||||
|
||||
Tr34.Recovered recovered = Tr34.recoverAndVerify(HEX_UPPER.formatHex(block),
|
||||
krd.getPrivate(), kdhCertificate.pem(), null, null);
|
||||
|
||||
assertEquals(HEX_UPPER.formatHex(HEX.parseHex("404142434445464748494A4B4C4D4E4F")),
|
||||
recovered.keyHex);
|
||||
// The block names its recipient by the certificate's own issuer and serial number,
|
||||
// so the receiver can tell which of its keys the block is for.
|
||||
assertEquals(HexFormat.of().formatHex(Certificates.certificateId(krdCertificate.der())),
|
||||
HexFormat.of().formatHex(recipientId(block)),
|
||||
"the recipient the block names");
|
||||
}
|
||||
|
||||
@Test
|
||||
void refusesASigningCertificateThatHasExpired() throws Exception {
|
||||
KeyPair krd = rsaKeyPair();
|
||||
KeyPair kdh = rsaKeyPair();
|
||||
Instant past = Instant.now().minusSeconds(7200);
|
||||
Instant expired = Instant.now().minusSeconds(3600);
|
||||
Certificates.Minted expiredCertificate = CertificatesTest.mint(kdh.getPublic(), "kdh",
|
||||
kdh.getPrivate(), "kdh", past, expired);
|
||||
|
||||
byte[] block = Tr34.build(HEX.parseHex("404142434445464748494A4B4C4D4E4F"),
|
||||
"B0010P0TB00E0000", null, kdh.getPrivate(), krd.getPublic());
|
||||
|
||||
IllegalArgumentException refused = assertThrows(IllegalArgumentException.class,
|
||||
() -> Tr34.recoverAndVerify(HEX_UPPER.formatHex(block), krd.getPrivate(),
|
||||
expiredCertificate.pem(), null, null));
|
||||
assertTrue(refused.getMessage().contains("validity window"), refused.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void refusesASigningCertificateThatIsNotYetValid() throws Exception {
|
||||
KeyPair krd = rsaKeyPair();
|
||||
KeyPair kdh = rsaKeyPair();
|
||||
Instant future = Instant.now().plusSeconds(3600);
|
||||
Instant later = Instant.now().plusSeconds(7200);
|
||||
Certificates.Minted earlyCertificate = CertificatesTest.mint(kdh.getPublic(), "kdh",
|
||||
kdh.getPrivate(), "kdh", future, later);
|
||||
|
||||
byte[] block = Tr34.build(HEX.parseHex("404142434445464748494A4B4C4D4E4F"),
|
||||
"B0010P0TB00E0000", null, kdh.getPrivate(), krd.getPublic());
|
||||
|
||||
IllegalArgumentException refused = assertThrows(IllegalArgumentException.class,
|
||||
() -> Tr34.recoverAndVerify(HEX_UPPER.formatHex(block), krd.getPrivate(),
|
||||
earlyCertificate.pem(), null, null));
|
||||
assertTrue(refused.getMessage().contains("validity window"), refused.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void refusesABlockThatCarriesNoHeader() throws Exception {
|
||||
KeyPair krd = rsaKeyPair();
|
||||
KeyPair kdh = rsaKeyPair();
|
||||
Certificates.Minted kdhCertificate = CertificatesTest.mint(kdh.getPublic(), "kdh",
|
||||
kdh.getPrivate(), "kdh");
|
||||
|
||||
// The block is signed correctly, but its key block names no key: the header that
|
||||
// says what the block carries is empty.
|
||||
byte[] block = Tr34.build(HEX.parseHex("404142434445464748494A4B4C4D4E4F"),
|
||||
"", null, kdh.getPrivate(), krd.getPublic());
|
||||
|
||||
IllegalArgumentException refused = assertThrows(IllegalArgumentException.class,
|
||||
() -> Tr34.recoverAndVerify(HEX_UPPER.formatHex(block), krd.getPrivate(),
|
||||
kdhCertificate.pem(), null, null));
|
||||
assertTrue(refused.getMessage().contains("no TR-31 header"), refused.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* The header a block carries in the clear.
|
||||
*
|
||||
* <p>The header travels in the clear beside the encrypted key block, so a reader sees
|
||||
* what the block carries without opening it.
|
||||
*
|
||||
* @param block the block
|
||||
* @return the header
|
||||
*/
|
||||
public static String clearHeader(byte[] block) throws IOException {
|
||||
ASN1Sequence contentInfo = ASN1Sequence.getInstance(block);
|
||||
ASN1Sequence signedData = ASN1Sequence.getInstance(
|
||||
ASN1TaggedObject.getInstance(contentInfo.getObjectAt(1)), true);
|
||||
ASN1Set signerInfos = ASN1Set.getInstance(signedData.getObjectAt(3));
|
||||
ASN1Sequence signerInfo = ASN1Sequence.getInstance(signerInfos.getObjectAt(0));
|
||||
ASN1Set attributes = ASN1Set.getInstance(
|
||||
ASN1TaggedObject.getInstance(signerInfo.getObjectAt(3)), false);
|
||||
for (ASN1Encodable element : attributes) {
|
||||
ASN1Sequence attribute = ASN1Sequence.getInstance(element);
|
||||
if (!"1.2.840.113549.1.7.1".equals(
|
||||
ASN1ObjectIdentifier.getInstance(attribute.getObjectAt(0)).getId())) {
|
||||
continue;
|
||||
}
|
||||
ASN1Set values = ASN1Set.getInstance(attribute.getObjectAt(1));
|
||||
return new String(ASN1OctetString.getInstance(values.getObjectAt(0)).getOctets(),
|
||||
StandardCharsets.US_ASCII);
|
||||
}
|
||||
throw new IllegalStateException("the block carries no header in the clear");
|
||||
}
|
||||
|
||||
/** The recipient identifier a block carries: the receiver's certificate identifier. */
|
||||
private static byte[] recipientId(byte[] block) throws IOException {
|
||||
ASN1Sequence contentInfo = ASN1Sequence.getInstance(block);
|
||||
ASN1Sequence signedData = ASN1Sequence.getInstance(
|
||||
ASN1TaggedObject.getInstance(contentInfo.getObjectAt(1)), true);
|
||||
ASN1Sequence encap = ASN1Sequence.getInstance(signedData.getObjectAt(2));
|
||||
byte[] envelope = ASN1OctetString.getInstance(
|
||||
ASN1TaggedObject.getInstance(encap.getObjectAt(1)), true).getOctets();
|
||||
try (ASN1InputStream stream = new ASN1InputStream(envelope)) {
|
||||
stream.readObject();
|
||||
ASN1Set recipients = ASN1Set.getInstance(stream.readObject());
|
||||
ASN1Sequence recipient = ASN1Sequence.getInstance(recipients.getObjectAt(0));
|
||||
// The identifier is the issuer and serial number of the receiver's certificate.
|
||||
return ASN1Sequence.getInstance(recipient.getObjectAt(1)).getEncoded();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAesKeyIsEncipheredWithAes() throws Exception {
|
||||
// The key block is enciphered with the cipher the key needs, so an AES key is not
|
||||
// protected by something weaker than itself.
|
||||
KeyPair krd = rsaKeyPair();
|
||||
KeyPair kdh = rsaKeyPair();
|
||||
Certificates.Minted kdhCertificate = CertificatesTest.mint(kdh.getPublic(), "kdh",
|
||||
kdh.getPrivate(), "kdh");
|
||||
|
||||
byte[] clear = HEX.parseHex("000102030405060708090A0B0C0D0E0F");
|
||||
String header = "D9999D0AB00E0000";
|
||||
byte[] nonce = HEX.parseHex("0102030405060708");
|
||||
|
||||
byte[] block = Tr34.build(clear, header, nonce, kdh.getPrivate(), krd.getPublic(),
|
||||
null, kdhCertificate.der(), true);
|
||||
|
||||
// The content encryption is the AES one: 2.16.840.1.101.3.4.1.2.
|
||||
assertTrue(HEX_UPPER.formatHex(block).contains("608648016503040102"),
|
||||
"the block does not name the AES content encryption");
|
||||
|
||||
Tr34.Recovered recovered = Tr34.recoverAndVerify(HEX_UPPER.formatHex(block),
|
||||
krd.getPrivate(), kdhCertificate.pem(), null, HEX_UPPER.formatHex(nonce));
|
||||
|
||||
assertEquals(HEX_UPPER.formatHex(clear), recovered.keyHex);
|
||||
assertEquals(header, recovered.header);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKeyBlockIsReadWhateverFollowsIt() throws Exception {
|
||||
// The bytes the cipher needed follow the key block, and a block whose plaintext was
|
||||
// already a whole number of cipher blocks carries none of them, so the sequence is
|
||||
// read from the front and the bytes after it are left alone.
|
||||
ASN1EncodableVector elements = new ASN1EncodableVector();
|
||||
elements.add(new ASN1Integer(1));
|
||||
elements.add(new DERSequence(new ASN1Integer(0)));
|
||||
elements.add(new DEROctetString(new byte[16]));
|
||||
elements.add(new DERSequence(new ASN1ObjectIdentifier("1.2.840.113549.1.7.1"),
|
||||
new DERSet(new DEROctetString(
|
||||
"B0010P0TB00E0000".getBytes(StandardCharsets.US_ASCII)))));
|
||||
byte[] keyBlock = new DERSequence(elements).getEncoded();
|
||||
|
||||
Tr34.Recovered recovered = Tr34.readKeyBlock(keyBlock);
|
||||
|
||||
assertEquals("B0010P0TB00E0000", recovered.header);
|
||||
assertEquals(HEX_UPPER.formatHex(new byte[16]), recovered.keyHex);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aOnePassBlockIsReadWithoutANonce() throws Exception {
|
||||
// A block with no nonce is a one-pass block, and it carries no nonce attribute at
|
||||
// all, which reads as an empty one.
|
||||
KeyPair krd = rsaKeyPair();
|
||||
KeyPair kdh = rsaKeyPair();
|
||||
Certificates.Minted kdhCertificate = CertificatesTest.mint(kdh.getPublic(), "kdh",
|
||||
kdh.getPrivate(), "kdh");
|
||||
|
||||
byte[] clear = HEX.parseHex("404142434445464748494A4B4C4D4E4F");
|
||||
String header = "B0010P0TB00E0000";
|
||||
|
||||
byte[] block = Tr34.build(clear, header, null, kdh.getPrivate(), krd.getPublic(),
|
||||
null, kdhCertificate.der());
|
||||
|
||||
Tr34.Recovered recovered = Tr34.recoverAndVerify(HEX_UPPER.formatHex(block),
|
||||
krd.getPrivate(), kdhCertificate.pem(), null, null);
|
||||
|
||||
assertEquals(HEX_UPPER.formatHex(clear), recovered.keyHex);
|
||||
assertEquals(header, recovered.header);
|
||||
}
|
||||
|
||||
@Test
|
||||
void refusesATamperedBlock() throws Exception {
|
||||
KeyPair krd = rsaKeyPair();
|
||||
KeyPair kdh = rsaKeyPair();
|
||||
Certificates.Minted kdhCertificate = CertificatesTest.mint(kdh.getPublic(), "kdh",
|
||||
kdh.getPrivate(), "kdh");
|
||||
|
||||
byte[] block = Tr34.build(HEX.parseHex("404142434445464748494A4B4C4D4E4F"),
|
||||
"B0010P0TB00E0000", null, kdh.getPrivate(), krd.getPublic());
|
||||
block[block.length - 1] ^= 0x01;
|
||||
|
||||
try {
|
||||
Tr34.recoverAndVerify(HEX_UPPER.formatHex(block), krd.getPrivate(), kdhCertificate.pem(),
|
||||
null, null);
|
||||
Assertions.fail("a tampered block was accepted");
|
||||
} catch (RuntimeException expected) {
|
||||
// The signature or the digest catches it.
|
||||
}
|
||||
}
|
||||
|
||||
/** An RSA key pair, for the device that receives the key. */
|
||||
private static KeyPair rsaKeyPair() throws Exception {
|
||||
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
|
||||
generator.initialize(2048);
|
||||
return generator.generateKeyPair();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user