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

Netty handler: fail updating session context if it does not exist #29

Merged
merged 1 commit into from
Aug 22, 2023
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 @@ -21,6 +21,7 @@ import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelPromise
import io.netty.channel.socket.DatagramPacket
import org.opencoap.ssl.SslConfig
import org.opencoap.ssl.SslException
import org.opencoap.ssl.transport.ByteBufferPacket
import org.opencoap.ssl.transport.DtlsServer
import org.opencoap.ssl.transport.DtlsSessionLifecycleCallbacks
Expand Down Expand Up @@ -90,8 +91,14 @@ class DtlsChannelHandler @JvmOverloads constructor(
when (msg) {
is DatagramPacket -> write(msg, promise, ctx)
is SessionAuthenticationContext -> {
dtlsServer.putSessionAuthenticationContext(msg.adr, msg.key, msg.value)
promise.setSuccess()
msg.map.forEach { (key, value) ->
if (!dtlsServer.putSessionAuthenticationContext(msg.adr, key, value)) {
promise.setFailure(SslException("Session does not exists"))
}
}
if (!promise.isDone) {
promise.setSuccess()
}
}

else -> ctx.write(msg, promise)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ package org.opencoap.ssl.netty

import java.net.InetSocketAddress

data class SessionAuthenticationContext(val adr: InetSocketAddress, val key: String, val value: String?)
data class SessionAuthenticationContext(val adr: InetSocketAddress, val map: Map<String, String?>)
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import org.opencoap.ssl.EmptyCidSupplier
import org.opencoap.ssl.PskAuth
import org.opencoap.ssl.RandomCidSupplier
import org.opencoap.ssl.SslConfig
import org.opencoap.ssl.SslException
import org.opencoap.ssl.netty.NettyHelpers.createBootstrap
import org.opencoap.ssl.transport.DtlsServer
import org.opencoap.ssl.transport.HashMapSessionStore
Expand Down Expand Up @@ -180,7 +181,7 @@ class NettyTest {
assertEquals("ECHO:hi", client.receive(5.seconds).await())

// when
srvChannel.writeAndFlush(SessionAuthenticationContext(client.localAddress(), "AUTH", "007:")).get()
srvChannel.writeAndFlush(SessionAuthenticationContext(client.localAddress(), mapOf("AUTH" to "007:"))).get()

// then
assertTrue(client.send("hi").await())
Expand All @@ -189,6 +190,13 @@ class NettyTest {
client.close()
}

@Test
fun `should fail to forward authentication context for non existing client`() {
assertThatThrownBy {
srvChannel.writeAndFlush(SessionAuthenticationContext(localAddress(1), mapOf("AUTH" to "007:"))).get()
}.hasRootCause(SslException("Session does not exists"))
}

@Test
fun `server should load session from store`() {
sessionStore.write("059876266f7c5734fd352c5a3b7b3be2".decodeHex(), SessionWithContext(StoredSessionPair.srvSession, mapOf(), Instant.ofEpochSecond(123456789)))
Expand Down
Loading