-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(RAMF): Implement payload wrapping (#71)
- Loading branch information
Showing
26 changed files
with
330 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/tech/relaycorp/relaynet/messages/payloads/EncryptedPayload.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package tech.relaycorp.relaynet.messages.payloads | ||
|
||
import tech.relaycorp.relaynet.SymmetricEncryption | ||
import tech.relaycorp.relaynet.wrappers.cms.SessionlessEnvelopedData | ||
import tech.relaycorp.relaynet.wrappers.x509.Certificate | ||
|
||
abstract class EncryptedPayload : Payload { | ||
fun encrypt( | ||
recipientCertificate: Certificate, | ||
symmetricEncryptionAlgorithm: SymmetricEncryption = SymmetricEncryption.AES_128 | ||
): ByteArray { | ||
val envelopedData = SessionlessEnvelopedData.encrypt( | ||
this.serializePlaintext(), | ||
recipientCertificate, | ||
symmetricEncryptionAlgorithm | ||
) | ||
return envelopedData.serialize() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/main/kotlin/tech/relaycorp/relaynet/messages/payloads/ServiceMessage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package tech.relaycorp.relaynet.messages.payloads | ||
|
||
class ServiceMessage : PayloadPlaintext { | ||
override fun serialize(): ByteArray { | ||
class ServiceMessage : EncryptedPayload() { | ||
override fun serializePlaintext(): ByteArray { | ||
TODO("Not yet implemented") | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/main/kotlin/tech/relaycorp/relaynet/messages/payloads/UnencryptedPayload.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package tech.relaycorp.relaynet.messages.payloads | ||
|
||
abstract class UnencryptedPayload : Payload |
46 changes: 46 additions & 0 deletions
46
src/main/kotlin/tech/relaycorp/relaynet/ramf/EncryptedRAMFMessage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package tech.relaycorp.relaynet.ramf | ||
|
||
import tech.relaycorp.relaynet.messages.payloads.EncryptedPayload | ||
import tech.relaycorp.relaynet.wrappers.cms.EnvelopedData | ||
import tech.relaycorp.relaynet.wrappers.cms.EnvelopedDataException | ||
import tech.relaycorp.relaynet.wrappers.cms.SessionlessEnvelopedData | ||
import tech.relaycorp.relaynet.wrappers.x509.Certificate | ||
import java.security.PrivateKey | ||
import java.time.ZonedDateTime | ||
|
||
abstract class EncryptedRAMFMessage<P : EncryptedPayload> internal constructor( | ||
serializer: RAMFSerializer, | ||
recipientAddress: String, | ||
payload: ByteArray, | ||
senderCertificate: Certificate, | ||
messageId: String?, | ||
creationDate: ZonedDateTime?, | ||
ttl: Int?, | ||
senderCertificateChain: Set<Certificate>? | ||
) : RAMFMessage<P>( | ||
serializer, | ||
recipientAddress, | ||
payload, | ||
senderCertificate, | ||
messageId, | ||
creationDate, | ||
ttl, | ||
senderCertificateChain | ||
) { | ||
/** | ||
* Decrypt and deserialize payload. | ||
* | ||
* @throws EnvelopedDataException if the CMS EnvelopedData value is invalid or the | ||
* `privateKey` is invalid. | ||
* @throws RAMFException if the plaintext is invalid. | ||
*/ | ||
@Throws(RAMFException::class, EnvelopedDataException::class) | ||
fun unwrapPayload(privateKey: PrivateKey): P { | ||
val envelopedData = EnvelopedData.deserialize(payload) as SessionlessEnvelopedData | ||
val plaintext = envelopedData.decrypt(privateKey) | ||
return deserializePayload(plaintext) | ||
} | ||
|
||
@Throws(RAMFException::class) | ||
internal abstract fun deserializePayload(payloadPlaintext: ByteArray): P | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/tech/relaycorp/relaynet/ramf/UnencryptedRAMFMessage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package tech.relaycorp.relaynet.ramf | ||
|
||
import tech.relaycorp.relaynet.messages.payloads.UnencryptedPayload | ||
import tech.relaycorp.relaynet.wrappers.x509.Certificate | ||
import java.time.ZonedDateTime | ||
|
||
abstract class UnencryptedRAMFMessage<P : UnencryptedPayload> internal constructor( | ||
serializer: RAMFSerializer, | ||
recipientAddress: String, | ||
payload: ByteArray, | ||
senderCertificate: Certificate, | ||
messageId: String?, | ||
creationDate: ZonedDateTime?, | ||
ttl: Int?, | ||
senderCertificateChain: Set<Certificate>? | ||
) : RAMFMessage<P>( | ||
serializer, | ||
recipientAddress, | ||
payload, | ||
senderCertificate, | ||
messageId, | ||
creationDate, | ||
ttl, | ||
senderCertificateChain | ||
) { | ||
@Throws(RAMFException::class) | ||
abstract fun deserializePayload(): P | ||
} |
8 changes: 4 additions & 4 deletions
8
src/test/kotlin/tech/relaycorp/relaynet/messages/CargoCollectionAuthorizationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
package tech.relaycorp.relaynet.messages | ||
|
||
import tech.relaycorp.relaynet.CERTIFICATE | ||
import tech.relaycorp.relaynet.ramf.RAMFSerializationTestCase | ||
import tech.relaycorp.relaynet.ramf.RAMFSpecializationTestCase | ||
import tech.relaycorp.relaynet.wrappers.x509.Certificate | ||
import kotlin.test.Test | ||
|
||
internal class CargoCollectionAuthorizationTest : | ||
RAMFSerializationTestCase<CargoCollectionAuthorization>( | ||
RAMFSpecializationTestCase<CargoCollectionAuthorization>( | ||
::CargoCollectionAuthorization, | ||
{ r: String, p: ByteArray, s: Certificate -> CargoCollectionAuthorization(r, p, s) }, | ||
0x44, | ||
0x00, | ||
CargoCollectionAuthorization.Companion | ||
) { | ||
@Test | ||
fun `Payload deserialization should be delegated to EmptyPayloadPlaintext`() { | ||
fun `Payload deserialization should be delegated to EmptyPayload`() { | ||
val cca = CargoCollectionAuthorization( | ||
"https://gb.relaycorp.tech", "".toByteArray(), CERTIFICATE | ||
) | ||
|
||
cca.deserializePayload("".toByteArray()) | ||
cca.deserializePayload() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.