Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(PublicThirdPartyEndpoint): Import certificate as ByteArray #63

Merged
merged 1 commit into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,26 @@ public class PublicThirdPartyEndpoint internal constructor(
* Import the public endpoint at [publicAddress].
*
* @param publicAddress The public address of the endpoint (e.g., `ping.awala.services`).
* @param identityCertificate The identity certificate of the endpoint.
* @param identityCertificateSerialized The DER serialization of identity certificate of the
* endpoint.
*/
@Throws(
PersistenceException::class,
InvalidThirdPartyEndpoint::class
)
public suspend fun import(
publicAddress: String,
identityCertificate: Certificate
identityCertificateSerialized: ByteArray
): PublicThirdPartyEndpoint {
val identityCertificate = try {
Certificate.deserialize(identityCertificateSerialized)
} catch (exc: CertificateException) {
throw InvalidThirdPartyEndpoint("Malformed identity certificate", exc)
}
try {
identityCertificate.validate()
} catch (exc: CertificateException) {
throw InvalidThirdPartyEndpoint("Invalid identity certificate")
throw InvalidThirdPartyEndpoint("Invalid identity certificate", exc)
}
val thirdPartyAddress = identityCertificate.subjectPrivateAddress
Storage.publicThirdParty.set(
Expand All @@ -175,6 +181,7 @@ public class PublicThirdPartyEndpoint internal constructor(

public class UnknownThirdPartyEndpointException(message: String) : AwaladroidException(message)
public class UnknownFirstPartyEndpointException(message: String) : AwaladroidException(message)
public class InvalidThirdPartyEndpoint(message: String) : AwaladroidException(message)
public class InvalidThirdPartyEndpoint(message: String, cause: Throwable? = null) :
AwaladroidException(message, cause)
public class InvalidAuthorizationException(message: String, cause: Throwable) :
AwaladroidException(message, cause)
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal class PublicThirdPartyEndpointTest {
@Test
fun import_successful() = runBlockingTest {
val publicAddress = "example.org"
with(PublicThirdPartyEndpoint.import(publicAddress, PDACertPath.PDA)) {
with(PublicThirdPartyEndpoint.import(publicAddress, PDACertPath.PDA.serialize())) {
assertEquals(publicAddress, this.publicAddress)
assertEquals(PDACertPath.PDA, identityCertificate)
assertEquals("https://$publicAddress", this.address)
Expand All @@ -71,6 +71,14 @@ internal class PublicThirdPartyEndpointTest {
)
}

@Test
fun import_malformedCertificate() = runBlockingTest {
expectedException.expect(InvalidThirdPartyEndpoint::class.java)
expectedException.expectMessage("Malformed identity certificate")

PublicThirdPartyEndpoint.import("example.org", "malformed".toByteArray())
}

@Test
fun import_invalidCertificate() = runBlockingTest {
val cert = issueEndpointCertificate(
Expand All @@ -83,7 +91,7 @@ internal class PublicThirdPartyEndpointTest {
expectedException.expect(InvalidThirdPartyEndpoint::class.java)
expectedException.expectMessage("Invalid identity certificate")

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

@Test
Expand Down