Skip to content

Commit

Permalink
fix(RAMF): Encrypt Cargo Collection Requests inside CCAs (#205)
Browse files Browse the repository at this point in the history
* fix(RAMF): Encrypt Cargo Collection Requests inside CCAs

* delete UnencryptedRAMFMessage
  • Loading branch information
gnarea authored Nov 19, 2021
1 parent c58e396 commit 75c758c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package tech.relaycorp.relaynet.messages

import java.io.InputStream
import java.time.ZonedDateTime
import tech.relaycorp.relaynet.messages.payloads.EmptyPayload
import tech.relaycorp.relaynet.messages.payloads.CargoCollectionRequest
import tech.relaycorp.relaynet.ramf.EncryptedRAMFMessage
import tech.relaycorp.relaynet.ramf.RAMFException
import tech.relaycorp.relaynet.ramf.RAMFMessageCompanion
import tech.relaycorp.relaynet.ramf.RAMFSerializer
import tech.relaycorp.relaynet.ramf.UnencryptedRAMFMessage
import tech.relaycorp.relaynet.wrappers.x509.Certificate

private val SERIALIZER = RAMFSerializer(0x44, 0x00)
Expand All @@ -22,7 +22,7 @@ class CargoCollectionAuthorization(
creationDate: ZonedDateTime? = null,
ttl: Int? = null,
senderCertificateChain: Set<Certificate>? = null
) : UnencryptedRAMFMessage<EmptyPayload>(
) : EncryptedRAMFMessage<CargoCollectionRequest>(
SERIALIZER,
recipientAddress,
payload,
Expand All @@ -32,7 +32,8 @@ class CargoCollectionAuthorization(
ttl,
senderCertificateChain
) {
override fun deserializePayload() = EmptyPayload.deserialize(payload)
override fun deserializePayload(payloadPlaintext: ByteArray) =
CargoCollectionRequest.deserialize(payloadPlaintext)

companion object : RAMFMessageCompanion<CargoCollectionAuthorization> {
/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package tech.relaycorp.relaynet.messages

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import org.junit.jupiter.api.BeforeEach
import tech.relaycorp.relaynet.SessionKeyPair
import tech.relaycorp.relaynet.messages.payloads.CargoCollectionRequest
import tech.relaycorp.relaynet.ramf.RAMFSpecializationTestCase
import tech.relaycorp.relaynet.utils.CDACertPath
import tech.relaycorp.relaynet.utils.ID_CERTIFICATE
import tech.relaycorp.relaynet.utils.MockPrivateKeyStore
import tech.relaycorp.relaynet.wrappers.x509.Certificate

@OptIn(ExperimentalCoroutinesApi::class)
internal class CargoCollectionAuthorizationTest :
RAMFSpecializationTestCase<CargoCollectionAuthorization>(
::CargoCollectionAuthorization,
Expand All @@ -13,12 +22,36 @@ internal class CargoCollectionAuthorizationTest :
0x00,
CargoCollectionAuthorization.Companion
) {
@Test
fun `Payload deserialization should be delegated to EmptyPayload`() {
val cca = CargoCollectionAuthorization(
"https://gb.relaycorp.tech", "".toByteArray(), ID_CERTIFICATE
)
private val recipientSessionKeyPair = SessionKeyPair.generate()
private val senderSessionKeyPair = SessionKeyPair.generate()

cca.deserializePayload()
private val privateKeyStore = MockPrivateKeyStore()

@BeforeEach
fun registerSessionKey() = runBlockingTest {
privateKeyStore.saveSessionKey(
recipientSessionKeyPair.privateKey,
recipientSessionKeyPair.sessionKey.keyId,
CDACertPath.PRIVATE_GW.subjectPrivateAddress,
CDACertPath.PUBLIC_GW.subjectPrivateAddress,
)
}

@Test
fun `Payload deserialization should be delegated to CargoCollectionRequest`() =
runBlockingTest {
val ccr = CargoCollectionRequest(CDACertPath.PUBLIC_GW)
val cca = CargoCollectionAuthorization(
CDACertPath.PUBLIC_GW.subjectPrivateAddress,
ccr.encrypt(recipientSessionKeyPair.sessionKey, senderSessionKeyPair),
ID_CERTIFICATE
)

val (payloadDeserialized) = cca.unwrapPayload(recipientSessionKeyPair.privateKey)

assertEquals(
ccr.cargoDeliveryAuthorization,
payloadDeserialized.cargoDeliveryAuthorization
)
}
}

0 comments on commit 75c758c

Please sign in to comment.