-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement WebSocket handshake (#16)
Fixes #3
- Loading branch information
Showing
9 changed files
with
767 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package tech.relaycorp.poweb | ||
|
||
import io.ktor.client.HttpClient | ||
import io.ktor.client.engine.okhttp.OkHttp | ||
import io.ktor.client.features.websocket.DefaultClientWebSocketSession | ||
import io.ktor.client.features.websocket.WebSockets | ||
import io.ktor.client.features.websocket.webSocket | ||
import io.ktor.http.cio.websocket.CloseReason | ||
import io.ktor.http.cio.websocket.Frame | ||
import io.ktor.http.cio.websocket.close | ||
import io.ktor.http.cio.websocket.readBytes | ||
import io.ktor.util.KtorExperimentalAPI | ||
import tech.relaycorp.poweb.handshake.Challenge | ||
import tech.relaycorp.poweb.handshake.InvalidMessageException | ||
import tech.relaycorp.poweb.handshake.NonceSigner | ||
import tech.relaycorp.poweb.handshake.Response | ||
import java.io.Closeable | ||
|
||
class PoWebClient internal constructor( | ||
internal val hostName: String, | ||
internal val port: Int, | ||
internal val useTls: Boolean | ||
) : Closeable { | ||
@KtorExperimentalAPI | ||
internal var ktorClient = HttpClient(OkHttp) { | ||
install(WebSockets) | ||
} | ||
|
||
@KtorExperimentalAPI | ||
override fun close() = ktorClient.close() | ||
|
||
private val wsUrl = "ws${if (useTls) "s" else ""}://$hostName:$port" | ||
|
||
@KtorExperimentalAPI | ||
internal suspend fun wsConnect( | ||
path: String, | ||
block: suspend DefaultClientWebSocketSession.() -> Unit | ||
) = ktorClient.webSocket("${wsUrl}$path", block = block) | ||
|
||
companion object { | ||
private const val defaultLocalPort = 276 | ||
private const val defaultRemotePort = 443 | ||
|
||
fun initLocal(port: Int = defaultLocalPort) = | ||
PoWebClient("127.0.0.1", port, false) | ||
|
||
fun initRemote(hostName: String, port: Int = defaultRemotePort) = | ||
PoWebClient(hostName, port, true) | ||
} | ||
} | ||
|
||
@Throws(PoWebException::class) | ||
internal suspend fun DefaultClientWebSocketSession.handshake(nonceSigners: Array<NonceSigner>) { | ||
if (nonceSigners.isEmpty()) { | ||
throw PoWebException("At least one nonce signer must be specified") | ||
} | ||
val challengeRaw = incoming.receive() | ||
val challenge = try { | ||
Challenge.deserialize(challengeRaw.readBytes()) | ||
} catch (exc: InvalidMessageException) { | ||
close(CloseReason(CloseReason.Codes.VIOLATED_POLICY, "")) | ||
throw PoWebException("Server sent an invalid handshake challenge", exc) | ||
} | ||
val nonceSignatures = nonceSigners.map { it.sign(challenge.nonce) }.toTypedArray() | ||
val response = Response(nonceSignatures) | ||
outgoing.send(Frame.Binary(true, response.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package tech.relaycorp.poweb | ||
|
||
class PoWebException(message: String, cause: Throwable? = null) : Exception(message, cause) |
33 changes: 33 additions & 0 deletions
33
src/main/kotlin/tech/relaycorp/poweb/handshake/NonceSigner.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,33 @@ | ||
package tech.relaycorp.poweb.handshake | ||
|
||
import org.bouncycastle.cms.CMSProcessableByteArray | ||
import org.bouncycastle.cms.CMSSignedDataGenerator | ||
import org.bouncycastle.cms.CMSTypedData | ||
import org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder | ||
import org.bouncycastle.operator.ContentSigner | ||
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder | ||
import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder | ||
import tech.relaycorp.relaynet.wrappers.x509.Certificate | ||
import java.security.PrivateKey | ||
|
||
class NonceSigner(internal val certificate: Certificate, private val privateKey: PrivateKey) { | ||
fun sign(nonce: ByteArray): ByteArray { | ||
val signedDataGenerator = CMSSignedDataGenerator() | ||
|
||
val signerBuilder = JcaContentSignerBuilder("SHA256withRSA") | ||
val contentSigner: ContentSigner = signerBuilder.build(privateKey) | ||
val signerInfoGenerator = JcaSignerInfoGeneratorBuilder( | ||
JcaDigestCalculatorProviderBuilder() | ||
.build() | ||
).build(contentSigner, certificate.certificateHolder) | ||
signedDataGenerator.addSignerInfoGenerator( | ||
signerInfoGenerator | ||
) | ||
|
||
signedDataGenerator.addCertificate(certificate.certificateHolder) | ||
|
||
val plaintextCms: CMSTypedData = CMSProcessableByteArray(nonce) | ||
val cmsSignedData = signedDataGenerator.generate(plaintextCms, false) | ||
return cmsSignedData.encoded | ||
} | ||
} |
Oops, something went wrong.