From c58e39691bd14c95409204b9858129639423c65b Mon Sep 17 00:00:00 2001 From: Gus Narea Date: Thu, 18 Nov 2021 15:22:13 +0000 Subject: [PATCH] feat(nodes): Implement GatewayManager (#204) --- .../payloads/CargoCollectionRequest.kt | 4 +- .../messages/payloads/CargoMessageSet.kt | 2 +- .../payloads/GatewayEncryptedPayload.kt | 3 ++ .../relaynet/nodes/GatewayManager.kt | 11 ++++++ .../relaynet/nodes/GatewayManagerTest.kt | 37 +++++++++++++++++++ 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/tech/relaycorp/relaynet/messages/payloads/GatewayEncryptedPayload.kt create mode 100644 src/main/kotlin/tech/relaycorp/relaynet/nodes/GatewayManager.kt create mode 100644 src/test/kotlin/tech/relaycorp/relaynet/nodes/GatewayManagerTest.kt diff --git a/src/main/kotlin/tech/relaycorp/relaynet/messages/payloads/CargoCollectionRequest.kt b/src/main/kotlin/tech/relaycorp/relaynet/messages/payloads/CargoCollectionRequest.kt index 1ed3241f..9995c60c 100644 --- a/src/main/kotlin/tech/relaycorp/relaynet/messages/payloads/CargoCollectionRequest.kt +++ b/src/main/kotlin/tech/relaycorp/relaynet/messages/payloads/CargoCollectionRequest.kt @@ -7,7 +7,9 @@ import tech.relaycorp.relaynet.wrappers.asn1.ASN1Utils import tech.relaycorp.relaynet.wrappers.x509.Certificate import tech.relaycorp.relaynet.wrappers.x509.CertificateException -class CargoCollectionRequest(val cargoDeliveryAuthorization: Certificate) : EncryptedPayload() { +class CargoCollectionRequest( + val cargoDeliveryAuthorization: Certificate +) : GatewayEncryptedPayload() { override fun serializePlaintext(): ByteArray { val cdaASN1 = DEROctetString(cargoDeliveryAuthorization.serialize()) return ASN1Utils.serializeSequence(listOf(cdaASN1), false) diff --git a/src/main/kotlin/tech/relaycorp/relaynet/messages/payloads/CargoMessageSet.kt b/src/main/kotlin/tech/relaycorp/relaynet/messages/payloads/CargoMessageSet.kt index 836ce070..94acb025 100644 --- a/src/main/kotlin/tech/relaycorp/relaynet/messages/payloads/CargoMessageSet.kt +++ b/src/main/kotlin/tech/relaycorp/relaynet/messages/payloads/CargoMessageSet.kt @@ -8,7 +8,7 @@ import tech.relaycorp.relaynet.wrappers.asn1.ASN1Utils /** * Cargo message set. */ -class CargoMessageSet(val messages: Array) : EncryptedPayload() { +class CargoMessageSet(val messages: Array) : GatewayEncryptedPayload() { /** * Serialize cargo message set. */ diff --git a/src/main/kotlin/tech/relaycorp/relaynet/messages/payloads/GatewayEncryptedPayload.kt b/src/main/kotlin/tech/relaycorp/relaynet/messages/payloads/GatewayEncryptedPayload.kt new file mode 100644 index 00000000..c11aeb5e --- /dev/null +++ b/src/main/kotlin/tech/relaycorp/relaynet/messages/payloads/GatewayEncryptedPayload.kt @@ -0,0 +1,3 @@ +package tech.relaycorp.relaynet.messages.payloads + +abstract class GatewayEncryptedPayload : EncryptedPayload() diff --git a/src/main/kotlin/tech/relaycorp/relaynet/nodes/GatewayManager.kt b/src/main/kotlin/tech/relaycorp/relaynet/nodes/GatewayManager.kt new file mode 100644 index 00000000..e3df902f --- /dev/null +++ b/src/main/kotlin/tech/relaycorp/relaynet/nodes/GatewayManager.kt @@ -0,0 +1,11 @@ +package tech.relaycorp.relaynet.nodes + +import tech.relaycorp.relaynet.keystores.PrivateKeyStore +import tech.relaycorp.relaynet.keystores.SessionPublicKeyStore +import tech.relaycorp.relaynet.messages.payloads.GatewayEncryptedPayload + +class GatewayManager( + privateKeyStore: PrivateKeyStore, + sessionPublicKeyStore: SessionPublicKeyStore, + cryptoOptions: NodeCryptoOptions = NodeCryptoOptions(), +) : NodeManager (privateKeyStore, sessionPublicKeyStore, cryptoOptions) diff --git a/src/test/kotlin/tech/relaycorp/relaynet/nodes/GatewayManagerTest.kt b/src/test/kotlin/tech/relaycorp/relaynet/nodes/GatewayManagerTest.kt new file mode 100644 index 00000000..b776d554 --- /dev/null +++ b/src/test/kotlin/tech/relaycorp/relaynet/nodes/GatewayManagerTest.kt @@ -0,0 +1,37 @@ +package tech.relaycorp.relaynet.nodes + +import kotlin.test.assertEquals +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test +import tech.relaycorp.relaynet.ECDHCurve +import tech.relaycorp.relaynet.HashingAlgorithm +import tech.relaycorp.relaynet.SymmetricCipher +import tech.relaycorp.relaynet.utils.MockPrivateKeyStore +import tech.relaycorp.relaynet.utils.MockSessionPublicKeyStore + +class GatewayManagerTest { + private val privateKeyStore = MockPrivateKeyStore() + private val sessionPublicKeyStore = MockSessionPublicKeyStore() + + @Nested + inner class Constructor { + @Test + fun `Default crypto algorithms should be used by default`() { + val gatewayManager = GatewayManager(privateKeyStore, sessionPublicKeyStore) + + assertEquals(NodeCryptoOptions(), gatewayManager.cryptoOptions) + } + + @Test + fun `Custom crypto algorithms should be honored`() { + val options = NodeCryptoOptions( + ECDHCurve.P521, + SymmetricCipher.AES_256, + HashingAlgorithm.SHA512, + ) + val gatewayManager = GatewayManager(privateKeyStore, sessionPublicKeyStore, options) + + assertEquals(options, gatewayManager.cryptoOptions) + } + } +}