Skip to content

Commit

Permalink
fix: Change to BSON serialization for public 3rd party (#52)
Browse files Browse the repository at this point in the history
* fix: Change to BSON serialization for public 3rd party

* Add throws

* Only catch BSONException
  • Loading branch information
sdsantos authored Mar 9, 2021
1 parent af659f0 commit 64aa2af
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
3 changes: 3 additions & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ dependencies {
implementation 'androidx.security:security-crypto:1.1.0-alpha03'
implementation "org.bouncycastle:bcpkix-jdk15on:1.67"

// Serialization
implementation 'org.mongodb:bson:4.2.2'

// Testing
testImplementation 'junit:junit:4.13.2'
testImplementation 'androidx.test:core:1.3.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package tech.relaycorp.relaydroid.endpoint

import org.json.JSONObject
import org.bson.BSONException
import org.bson.BsonBinary
import org.bson.BsonBinaryReader
import org.bson.BsonBinaryWriter
import org.bson.io.BasicOutputBuffer
import tech.relaycorp.relaydroid.Storage
import tech.relaycorp.relaydroid.common.decodeBase64
import tech.relaycorp.relaydroid.common.encodeBase64
import tech.relaycorp.relaydroid.storage.persistence.PersistenceException
import tech.relaycorp.relaynet.RelaynetException
import tech.relaycorp.relaynet.wrappers.x509.Certificate
import tech.relaycorp.relaynet.wrappers.x509.CertificateException
import java.nio.charset.Charset
import java.nio.ByteBuffer


public sealed class ThirdPartyEndpoint(
public val thirdPartyAddress: String, // Private address
Expand Down Expand Up @@ -109,28 +112,40 @@ public class PublicThirdPartyEndpoint(
internal data class StoredData(
val publicAddress: String, val identityCertificate: Certificate
) {
fun serialize() =
JSONObject().also { json ->
json.put("public_address", publicAddress)
json.put(
"identity_certificate",
identityCertificate.serialize().encodeBase64()
)
@Throws(PersistenceException::class)
fun serialize(): ByteArray {
try {
val output = BasicOutputBuffer()
BsonBinaryWriter(output).use {
it.writeStartDocument()
it.writeString("public_address", publicAddress)
it.writeBinaryData("identity_certificate", BsonBinary(identityCertificate.serialize()))
it.writeEndDocument()
}
return output.toByteArray()
} catch (exp: BSONException) {
throw PersistenceException("Could not serialize PublicThirdPartyEndpoint", exp)
}
.toString()
.toByteArray(Charset.forName("UTF-8"))
}

companion object {
fun deserialize(byteArray: ByteArray): StoredData {
val jsonString = byteArray.toString(Charset.forName("UTF-8"))
val json = JSONObject(jsonString)
return StoredData(
json.getString("public_address"),
Certificate.deserialize(
json.getString("identity_certificate").decodeBase64()
)
)
}
@Throws(PersistenceException::class)
fun deserialize(byteArray: ByteArray): StoredData =
try {
BsonBinaryReader(ByteBuffer.wrap(byteArray)).use { reader ->
reader.readStartDocument()
StoredData(
reader.readString("public_address"),
Certificate.deserialize(
reader.readBinaryData("identity_certificate").data
)
).also {
reader.readEndDocument()
}
}
} catch (exp: BSONException) {
throw PersistenceException("Could not deserialize PublicThirdPartyEndpoint", exp)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import tech.relaycorp.relaydroid.Relaynet
import tech.relaycorp.relaydroid.storage.StorageImpl
import tech.relaycorp.relaydroid.storage.mockStorage
Expand Down Expand Up @@ -77,4 +79,17 @@ internal class PublicThirdPartyEndpointTest {

PublicThirdPartyEndpoint.import("example.org", cert)
}

@Test
fun storedDataSerialization() {
val publicAddress = "example.org"
val certificate = PDACertPath.PUBLIC_GW

val dataSerialized =
PublicThirdPartyEndpoint.StoredData(publicAddress, certificate).serialize()
val data = PublicThirdPartyEndpoint.StoredData.deserialize(dataSerialized)

assertEquals(publicAddress, data.publicAddress)
assertEquals(certificate, data.identityCertificate)
}
}

0 comments on commit 64aa2af

Please sign in to comment.