Skip to content

Commit

Permalink
Make DTLS session start available throgh the TransportContext (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
akolosov-n committed Aug 22, 2023
1 parent c4b9670 commit ceb4f5d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions coap-mbedtls/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ description = "coap-mbedtls"

dependencies {
api(project(":coap-core"))
api("io.github.open-coap:kotlin-mbedtls:1.17.2")
api("io.github.open-coap:kotlin-mbedtls:1.18.0")

testImplementation(project(":coap-netty"))
testImplementation("io.github.open-coap:kotlin-mbedtls-netty:1.17.2")
testImplementation("io.github.open-coap:kotlin-mbedtls-netty:1.18.0")

testImplementation(testFixtures(project(":coap-core")))
testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.3")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.opencoap.transport.mbedtls;

import com.mbed.coap.transport.TransportContext;
import java.time.Instant;
import java.util.Collections;
import java.util.Map;
import org.opencoap.ssl.transport.DtlsSessionContext;
Expand All @@ -24,6 +25,7 @@ public class DtlsTransportContext {
public static final TransportContext.Key<Map<String, String>> DTLS_AUTHENTICATION = new TransportContext.Key<>(Collections.emptyMap());
public static final TransportContext.Key<String> DTLS_PEER_CERTIFICATE_SUBJECT = new TransportContext.Key<>(null);
public static final TransportContext.Key<byte[]> DTLS_CID = new TransportContext.Key<>(null);
public static final TransportContext.Key<Instant> DTLS_SESSION_START_TIMESTAMP = new TransportContext.Key<>(null);

public static TransportContext toTransportContext(DtlsSessionContext dtlsSessionContext) {
if (dtlsSessionContext.equals(DtlsSessionContext.EMPTY)) {
Expand All @@ -37,6 +39,9 @@ public static TransportContext toTransportContext(DtlsSessionContext dtlsSession
if (dtlsSessionContext.getCid() != null) {
dtlsContext = dtlsContext.with(DTLS_CID, dtlsSessionContext.getCid());
}
if (dtlsSessionContext.getSessionStartTimestamp() != null) {
dtlsContext = dtlsContext.with(DTLS_SESSION_START_TIMESTAMP, dtlsSessionContext.getSessionStartTimestamp());
}

return dtlsContext;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import static org.opencoap.transport.mbedtls.DtlsTransportContext.DTLS_AUTHENTICATION;
import static org.opencoap.transport.mbedtls.DtlsTransportContext.DTLS_CID;
import static org.opencoap.transport.mbedtls.DtlsTransportContext.DTLS_PEER_CERTIFICATE_SUBJECT;
import static org.opencoap.transport.mbedtls.DtlsTransportContext.DTLS_SESSION_START_TIMESTAMP;
import com.mbed.coap.transport.TransportContext;
import java.time.Instant;
import java.util.Collections;
import org.junit.jupiter.api.Test;
import org.opencoap.ssl.transport.DtlsSessionContext;
Expand All @@ -36,19 +38,21 @@ void shouldConvertEmptyDtlsSessionContext() {
assertTrue(transCtx.get(DTLS_AUTHENTICATION).isEmpty());
assertNull(transCtx.get(DTLS_PEER_CERTIFICATE_SUBJECT));
assertNull(transCtx.get(DTLS_CID));
assertNull(transCtx.get(DTLS_SESSION_START_TIMESTAMP));

assertEquals(transCtx, TransportContext.EMPTY);
}

@Test
void shouldConvertDtlsSessionContext() {
TransportContext transCtx = DtlsTransportContext.toTransportContext(
new DtlsSessionContext(Collections.singletonMap("a", "b"), "CN:aa", new byte[]{1, 2})
new DtlsSessionContext(Collections.singletonMap("a", "b"), "CN:aa", new byte[]{1, 2}, Instant.ofEpochSecond(123456789))
);

assertEquals("b", transCtx.get(DTLS_AUTHENTICATION).get("a"));
assertNull(transCtx.get(DTLS_AUTHENTICATION).get("fdsfs"));
assertEquals("CN:aa", transCtx.get(DTLS_PEER_CERTIFICATE_SUBJECT));
assertEquals(Instant.ofEpochSecond(123456789), transCtx.get(DTLS_SESSION_START_TIMESTAMP));
assertArrayEquals(new byte[]{1, 2}, transCtx.get(DTLS_CID));
}
}

0 comments on commit ceb4f5d

Please sign in to comment.