Making Zephyr COAP Server Sample+ IPv4/Ethernet + DTLS + NS-Target to work

I wanted a COAP Server running on nRF5340dk with DTLS and NS-Target without success.
Whenever there is an attempt to connect to it from outside the app freezes, eg. shell becomes non responsive.

TL; DR

What I was able to do until now, was first do use the coap_server sample without DTLS and using the secure (standard) target.

Then I wanted first to use DTLS, which I was able to do, but some adaptions were required, because the current state of nordic sdk is a little bit behind (as of 10th June 2025 comparing SDK 3.0.2 to current zephyr main). The coap_server sample and stack have implemented support for DTLS, but after importing the few differences COAP+DTLS was running on the nRF, yet!.

But the final step is where I am stuck, switching from secure to non-secure target. I was able to switch some get rid of some mbed_tls options originally from the sample to use TLS/PSA instead. Here the official nrf TLS API sample helped to find options required to activate DTLS as well.

Long story short, I get to compile finally for NS-Target, but as described, when the connection is made, the application freezes. Does some one have seen similar behaviour, perhaps not explicitly COAP but in a Sense related to DTLS/Socket, or similar?

Here just more Details of my previous steps. 

1) Ethernet Connection is working via additional spin/ethernet W5500 with the help of the existing W5500 ethernet driver, and device tree is adapted for using spi accordingly. But surely may work as well with rtt-driver as well.

2) Besides the required ethernet/ip config, the zephyr coap_server sample works practically out of the box, I adapted the config not to use IPv6 but IPv4 instead. Target here is currently secure (non ns)

3) Enable DTLS support by including the respective DTLS overlay included with the coap_server sample, and voila DTLS works as well.

4) Switch to new build config for NS-Target, does not compile. So Adaptions are required, here is the updated overlay for NS-Target which finally compiles.

#Config to make the project compile for NS Target
#The first part comes from original overlay 
CONFIG_NET_SAMPLE_COAPS_SERVICE=y

# # Secure Socket
CONFIG_NET_SOCKETS_SOCKOPT_TLS=y
CONFIG_NET_SOCKETS_ENABLE_DTLS=y
CONFIG_NET_SOCKETS_TLS_MAX_CLIENT_SESSION_COUNT=6
CONFIG_NET_SOCKETS_TLS_MAX_CONTEXTS=6
CONFIG_NET_SOCKETS_DTLS_TIMEOUT=30000

##ORIGINAL Overlay for secure target adapted for ns
# # TLS configuration
#CONFIG_MBEDTLS_DEBUG=y
#CONFIG_MBEDTLS=y
# #CONFIG_MBEDTLS_BUILTIN=y
# CONFIG_MBEDTLS_ENABLE_HEAP=y
# CONFIG_MBEDTLS_HEAP_SIZE=60000
CONFIG_MBEDTLS_SSL_DTLS_CONNECTION_ID=y
#CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=2048
CONFIG_MBEDTLS_KEY_EXCHANGE_PSK_ENABLED=n


# #Additional Changes (needed more stack)
CONFIG_COAP_SERVER_STACK_SIZE=8192


############ Imports from NRF TLS API SAMPLE

# Networking
CONFIG_NETWORKING=y
CONFIG_NET_UDP=y
CONFIG_NET_TCP=n
CONFIG_NET_IPV6=n
CONFIG_NET_IPV4=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_TCP_ISN_RFC6528=n
CONFIG_NET_CONFIG_SETTINGS=y
CONFIG_NET_CONFIG_NEED_IPV6=n
CONFIG_NET_CONFIG_NEED_IPV4=y
CONFIG_POSIX_API=y
CONFIG_NET_CONNECTION_MANAGER=n
CONFIG_NET_CONTEXT_NET_PKT_POOL=y


CONFIG_MBEDTLS_SSL_COOKIE_C=y

# TLS networking
CONFIG_ZVFS_OPEN_MAX=8
#CONFIG_NET_SOCKETS_TLS_MAX_CONTEXTS=6
CONFIG_NET_SOCKETS_TLS_MAX_CIPHERSUITES=16



# mbed TLS and security
CONFIG_MBEDTLS_PK_C=y

#CONFIG_MBEDTLS_ENABLE_HEAP=y
#CONFIG_MBEDTLS_HEAP_SIZE=32768
CONFIG_MBEDTLS_SSL_IN_CONTENT_LEN=2304
CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=2304
CONFIG_MBEDTLS_SSL_CLI_C=y
CONFIG_MBEDTLS_TLS_LIBRARY=y
CONFIG_MBEDTLS_X509_LIBRARY=y

# NB: This list of PSA dependencies may be too long
CONFIG_PSA_WANT_GENERATE_RANDOM=y
CONFIG_PSA_WANT_KEY_TYPE_AES=y
CONFIG_PSA_WANT_ALG_CCM=y
CONFIG_PSA_WANT_ALG_GCM=y
CONFIG_PSA_WANT_ALG_CHACHA20_POLY1305=y
CONFIG_PSA_WANT_ALG_CMAC=y
CONFIG_PSA_WANT_ALG_HMAC=y
CONFIG_PSA_WANT_ALG_SHA_1=y
CONFIG_PSA_WANT_ALG_SHA_224=y
CONFIG_PSA_WANT_ALG_SHA_256=y
CONFIG_PSA_WANT_ALG_SHA_384=y
CONFIG_PSA_WANT_ALG_SHA_512=y
CONFIG_PSA_WANT_ALG_ECB_NO_PADDING=y
CONFIG_PSA_WANT_ALG_CBC_NO_PADDING=y
CONFIG_PSA_WANT_ALG_CBC_PKCS7=y
CONFIG_PSA_WANT_ALG_CTR=y
CONFIG_PSA_WANT_ALG_HKDF=y
CONFIG_PSA_WANT_ALG_TLS12_PRF=y
CONFIG_PSA_WANT_ALG_ECDH=y
CONFIG_PSA_WANT_ALG_ECDSA=y
CONFIG_PSA_WANT_ALG_DETERMINISTIC_ECDSA=y
CONFIG_PSA_WANT_ALG_JPAKE=y
CONFIG_PSA_WANT_ECC_SECP_R1_256=y
CONFIG_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_IMPORT=y
CONFIG_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_EXPORT=y
CONFIG_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE=y
CONFIG_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE=y
CONFIG_PSA_WANT_ALG_STREAM_CIPHER=y
CONFIG_PSA_WANT_KEY_TYPE_CHACHA20=y
CONFIG_PSA_WANT_ALG_TLS12_PSK_TO_MS=y


#very important import from nrf tls api sample (nrf5340 specific overlay for ns-target)
CONFIG_TFM_PROFILE_TYPE_NOT_SET=y
CONFIG_NRF_ENABLE_ICACHE=n
CONFIG_MBEDTLS_USE_PSA_CRYPTO=y
CONFIG_MBEDTLS_PSA_CRYPTO_C=y

CONFIG_PM_PARTITION_SIZE_TFM=0x60000

5) It seems that the COAP Client does not get a response at all to start a handshake, so I assume happens at connection level already, but can not tell what it is. So I think that the network stack does not even arrive to a step to check credentials at all.

Any suggestions, recommendations?

  • It seems that the COAP Client does not get a response at all to start a handshake

    The coap-client will need to start with the handshake even before sending the request. That may be be done internally in the coap-client/dtls mapping.

    Are you able to get some captures? E.g. run the coap/dtls client on an PC with wireshark ?

  • well, actually what I meant, was that not even handshake was possible, after the first "Client Hello" the nrf becomes non responsive.(non secure target)

    ❯ coap-client -v 7 -m get coaps://192.168.0.8/core1
    Jun 10 16:51:36.443 DEBG ***192.168.0.102:59711 <-> 192.168.0.8:5684 DTLS: new outgoing session
    Jun 10 16:51:36.444 DEBG *  192.168.0.102:59711 <-> 192.168.0.8:5684 DTLS: netif: sent  258 bytes
    Jun 10 16:51:36.444 DEBG timeout is set to 90 seconds
    Jun 10 16:51:36.444 DEBG sending CoAP request:
    Jun 10 16:51:36.444 DEBG ** 192.168.0.102:59711 <-> 192.168.0.8:5684 DTLS: mid=0x64d7: delayed
    Jun 10 16:51:37.448 DEBG ** 192.168.0.102:59711 <-> 192.168.0.8:5684 DTLS: DTLS retransmit timeout
    Jun 10 16:51:37.448 DEBG *  192.168.0.102:59711 <-> 192.168.0.8:5684 DTLS: netif: sent  258 bytes
    Jun 10 16:51:39.450 DEBG ** 192.168.0.102:59711 <-> 192.168.0.8:5684 DTLS: DTLS retransmit timeout
    Jun 10 16:51:39.451 DEBG *  192.168.0.102:59711 <-> 192.168.0.8:5684 DTLS: netif: sent  258 bytes
    Jun 10 16:51:43.452 DEBG ** 192.168.0.102:59711 <-> 192.168.0.8:5684 DTLS: DTLS retransmit timeout
    Jun 10 16:51:43.452 DEBG *  192.168.0.102:59711 <-> 192.168.0.8:5684 DTLS: netif: sent  258 bytes
    Jun 10 16:51:51.457 DEBG ** 192.168.0.102:59711 <-> 192.168.0.8:5684 DTLS: DTLS retransmit timeout
    Jun 10 16:51:51.457 DEBG *  192.168.0.102:59711 <-> 192.168.0.8:5684 DTLS: netif: sent  258 bytes
    Jun 10 16:52:07.459 DEBG ** 192.168.0.102:59711 <-> 192.168.0.8:5684 DTLS: DTLS retransmit timeout
    Jun 10 16:52:07.459 DEBG ** 192.168.0.102:59711 <-> 192.168.0.8:5684 DTLS: mid=0x64d7: not transmitted after disconnect
    Jun 10 16:52:07.459 ERR  cannot send CoAP pdu
    Jun 10 16:52:07.459 DEBG ***192.168.0.102:59711 <-> 192.168.0.8:5684 DTLS: session disconnected (COAP_NACK_TLS_FAILED)
    Jun 10 16:52:07.459 DEBG ***EVENT: COAP_EVENT_DTLS_CLOSED
    ^CJun 10 16:52:36.129 DEBG ***192.168.0.102:59711 <-> 192.168.0.8:5684 DTLS: session 0x1431078e0: closed


    but in contrast, lets say if I do it with the secure target, then it works as expected.

    ❯ coap-client -v 7 -m get coaps://192.168.0.8/core1
    Jun 10 16:40:12.006 DEBG ***192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: new outgoing session
    Jun 10 16:40:12.006 DEBG *  192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: netif: sent  258 bytes
    Jun 10 16:40:12.006 DEBG timeout is set to 90 seconds
    Jun 10 16:40:12.006 DEBG sending CoAP request:
    Jun 10 16:40:12.006 DEBG ** 192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: mid=0x1648: delayed
    Jun 10 16:40:12.021 DEBG *  192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: netif: recv   60 bytes
    Jun 10 16:40:12.022 DEBG *  192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: netif: sent  290 bytes
    Jun 10 16:40:12.034 DEBG *  192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: netif: recv  925 bytes
    Jun 10 16:40:12.035 INFO    192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: self-signed certificate: overridden: 'localhost' depth=0
    Jun 10 16:40:12.035 INFO    192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: unable to get certificate CRL: overridden: 'localhost' depth=0
    Jun 10 16:40:12.035 INFO CN 'localhost' presented by server (Certificate)
    Jun 10 16:40:12.035 DEBG *  192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: netif: sent  390 bytes
    Jun 10 16:40:13.040 DEBG ** 192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: DTLS retransmit timeout
    Jun 10 16:40:13.040 DEBG *  192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: netif: sent  283 bytes
    Jun 10 16:40:13.040 DEBG *  192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: netif: sent   14 bytes
    Jun 10 16:40:13.041 DEBG *  192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: netif: sent   93 bytes
    Jun 10 16:40:14.465 DEBG *  192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: netif: recv  107 bytes
    Jun 10 16:40:14.465 DEBG ***EVENT: COAP_EVENT_DTLS_CONNECTED
    Jun 10 16:40:14.465 DEBG ***192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: session connected
    Jun 10 16:40:14.465 DEBG ** 192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: mid=0x1648: transmitted after delay
    Jun 10 16:40:14.465 DEBG *  192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: netif: sent   93 bytes
    Jun 10 16:40:14.465 DEBG *  192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: dtls:  sent   17 bytes
    v:1 t:CON c:GET i:1648 {} [ Uri-Path:core1, Request-Tag:0xc9075556 ]
    Jun 10 16:40:14.466 DEBG ** 192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: mid=0x1648: added to retransmit queue (2844ms)
    Jun 10 16:40:14.466 DEBG *  192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: netif: recv  107 bytes
    Jun 10 16:40:14.477 DEBG *  192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: netif: recv   93 bytes
    v:1 t:ACK c:2.05 i:1648 {} [ ] :: 'Just a test\x0A\x00'
    Jun 10 16:40:14.477 DEBG ** 192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: mid=0x1648: removed (1)
    Jun 10 16:40:14.477 DEBG ** process incoming 2.05 response:
    Just a test
    Jun 10 16:40:14.477 DEBG *  192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: netif: sent   77 bytes
    Jun 10 16:40:14.477 INFO *  192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: SSL3 alert write:warning:close notify
    Jun 10 16:40:14.477 DEBG ***EVENT: COAP_EVENT_DTLS_CLOSED
    Jun 10 16:40:14.478 DEBG ***192.168.0.102:60602 <-> 192.168.0.8:5684 DTLS: session 0x158907e50: closed

    and because the system freezes on the ns target, I am not able to see a single line in the shell, which may or may not throw a warning, error, info, whatever, it is frozen.


    Another test, lets say I address the wrong Port number in the non secure target, then the system behaves correctly rejecting connection and no freeze happens.

    ❯ coap-client -v 7 -m get coaps://192.168.0.8:5685/core1
    Jun 10 17:02:54.621 DEBG ***192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: new outgoing session
    Jun 10 17:02:54.621 DEBG *  192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: netif: sent  258 bytes
    Jun 10 17:02:54.621 DEBG timeout is set to 90 seconds
    Jun 10 17:02:54.621 DEBG sending CoAP request:
    Jun 10 17:02:54.621 DEBG ** 192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: mid=0x2729: delayed
    Jun 10 17:02:54.679 WARN ** 192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: coap_socket_recv: ICMP: Connection refused
    Jun 10 17:02:54.679 DEBG ***192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: session ICMP issue (COAP_NACK_ICMP_ISSUE)
    Jun 10 17:02:55.625 DEBG ** 192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: DTLS retransmit timeout
    Jun 10 17:02:55.625 DEBG *  192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: netif: sent  258 bytes
    Jun 10 17:02:55.636 WARN ** 192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: coap_socket_recv: ICMP: Connection refused
    Jun 10 17:02:55.636 DEBG ***192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: session ICMP issue (COAP_NACK_ICMP_ISSUE)
    Jun 10 17:02:57.630 DEBG ** 192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: DTLS retransmit timeout
    Jun 10 17:02:57.630 DEBG *  192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: netif: sent  258 bytes
    Jun 10 17:02:57.636 WARN ** 192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: coap_socket_recv: ICMP: Connection refused
    Jun 10 17:02:57.636 DEBG ***192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: session ICMP issue (COAP_NACK_ICMP_ISSUE)
    Jun 10 17:03:01.631 DEBG ** 192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: DTLS retransmit timeout
    Jun 10 17:03:01.632 DEBG *  192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: netif: sent  258 bytes
    Jun 10 17:03:01.637 WARN ** 192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: coap_socket_recv: ICMP: Connection refused
    Jun 10 17:03:01.637 DEBG ***192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: session ICMP issue (COAP_NACK_ICMP_ISSUE)
    Jun 10 17:03:09.636 DEBG ** 192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: DTLS retransmit timeout
    Jun 10 17:03:09.637 DEBG *  192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: netif: sent  258 bytes
    Jun 10 17:03:09.641 WARN ** 192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: coap_socket_recv: ICMP: Connection refused
    Jun 10 17:03:09.641 DEBG ***192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: session ICMP issue (COAP_NACK_ICMP_ISSUE)
    Jun 10 17:03:25.641 DEBG ** 192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: DTLS retransmit timeout
    Jun 10 17:03:25.642 DEBG ** 192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: mid=0x2729: not transmitted after disconnect
    Jun 10 17:03:25.642 ERR  cannot send CoAP pdu
    Jun 10 17:03:25.642 DEBG ***192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: session disconnected (COAP_NACK_TLS_FAILED)
    Jun 10 17:03:25.642 DEBG ***EVENT: COAP_EVENT_DTLS_CLOSED
    
    ^CJun 10 17:03:34.344 DEBG ***192.168.0.102:49748 <-> 192.168.0.8:5685 DTLS: session 0x122007ac0: closed

  • My suspicion, is that coap server stack implementation, which was adapted on zephyr's main line in order to support DTLS, may not be compatible to NS/TFM yet, and hopefully is adapted soon to be integrated in the NCS. Am I right?

Related