Skip to content

Commit d99cddd

Browse files
nordicjmstephanosio
authored andcommitted
samples: mgmt: mcumgr: smp_svr: Add udp_dtls sample
Adds a sample which demonstrates how to use DTLS over UDP. This copies the certificate files from the echo server sample to use as dummy certificates Signed-off-by: Jamie McCrae <[email protected]>
1 parent a44efd9 commit d99cddd

File tree

8 files changed

+128
-0
lines changed

8 files changed

+128
-0
lines changed

samples/subsys/mgmt/mcumgr/smp_svr/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,22 @@ project(smp_svr)
1414

1515
target_sources(app PRIVATE src/main.c)
1616
target_sources_ifdef(CONFIG_MCUMGR_TRANSPORT_BT app PRIVATE src/bluetooth.c)
17+
18+
if(CONFIG_MCUMGR_TRANSPORT_UDP_DTLS)
19+
# Use dummy certificate files
20+
set(cert_dir certificates)
21+
set(cert_files echo-apps-cert.der;echo-apps-key.der)
22+
set(gen_dir ${ZEPHYR_BINARY_DIR}/include/generated)
23+
24+
message(WARNING "Using dummy certificate files, these are provided for demonstration only")
25+
26+
foreach(inc_file ${cert_files})
27+
generate_inc_file_for_target(
28+
app
29+
${cert_dir}/${inc_file}
30+
${gen_dir}/${inc_file}.inc
31+
)
32+
endforeach()
33+
34+
target_sources(app PRIVATE src/udp_dtls.c)
35+
endif()
767 Bytes
Binary file not shown.
Binary file not shown.

samples/subsys/mgmt/mcumgr/smp_svr/sample.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ tests:
3737
- frdm_k64f
3838
integration_platforms:
3939
- frdm_k64f
40+
sample.mcumgr.smp_svr.udp_dtls:
41+
extra_args:
42+
- EXTRA_CONF_FILE="udp_dtls.conf"
43+
- CONFIG_IMG_MANAGER=n
44+
- SB_CONFIG_BOOTLOADER_NONE=y
45+
platform_allow:
46+
- native_sim
47+
integration_platforms:
48+
- native_sim
49+
build_only: true
4050
sample.mcumgr.smp_svr.udp.802154.subg:
4151
extra_args: EXTRA_CONF_FILE="udp.conf;802154-subg.conf"
4252
platform_allow: beagleconnect_freedom

samples/subsys/mgmt/mcumgr/smp_svr/src/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
*/
66

77
void start_smp_bluetooth_adverts(void);
8+
int setup_udp_dtls(void);

samples/subsys/mgmt/mcumgr/smp_svr/src/main.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#ifdef CONFIG_MCUMGR_GRP_STAT
1818
#include <zephyr/mgmt/mcumgr/grp/stat_mgmt/stat_mgmt.h>
1919
#endif
20+
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_DTLS
21+
#include <zephyr/mgmt/mcumgr/transport/smp_udp.h>
22+
#endif
2023

2124
#define LOG_LEVEL LOG_LEVEL_DBG
2225
#include <zephyr/logging/log.h>
@@ -67,6 +70,20 @@ int main(void)
6770
}
6871
#endif
6972

73+
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_DTLS
74+
rc = setup_udp_dtls();
75+
76+
if (rc == 0) {
77+
rc = smp_udp_open();
78+
79+
if (rc != 0) {
80+
LOG_ERR("UDP transport open failed: %d", rc);
81+
}
82+
} else {
83+
LOG_ERR("TLS init failed, cannot start UDP transport");
84+
}
85+
#endif
86+
7087
#ifdef CONFIG_MCUMGR_TRANSPORT_BT
7188
start_smp_bluetooth_adverts();
7289
#endif
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/net/tls_credentials.h>
9+
#include <zephyr/logging/log.h>
10+
11+
LOG_MODULE_DECLARE(smp_sample);
12+
13+
static const unsigned char server_certificate[] = {
14+
#include "echo-apps-cert.der.inc"
15+
};
16+
17+
/* This is the private key in pkcs#8 format. */
18+
static const unsigned char private_key[] = {
19+
#include "echo-apps-key.der.inc"
20+
};
21+
22+
int setup_udp_dtls(void)
23+
{
24+
int rc;
25+
26+
rc = tls_credential_add(CONFIG_MCUMGR_TRANSPORT_UDP_DTLS_TLS_TAG,
27+
TLS_CREDENTIAL_PUBLIC_CERTIFICATE, server_certificate,
28+
sizeof(server_certificate));
29+
30+
if (rc < 0) {
31+
LOG_ERR("Failed to register public certificate: %d", rc);
32+
return rc;
33+
}
34+
35+
rc = tls_credential_add(CONFIG_MCUMGR_TRANSPORT_UDP_DTLS_TLS_TAG,
36+
TLS_CREDENTIAL_PRIVATE_KEY, private_key, sizeof(private_key));
37+
38+
if (rc < 0) {
39+
LOG_ERR("Failed to register private key: %d", rc);
40+
}
41+
42+
return rc;
43+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Enable the UDP DTLS MCUmgr transport.
2+
CONFIG_MCUMGR_TRANSPORT_UDP=y
3+
CONFIG_MCUMGR_TRANSPORT_UDP_DTLS=y
4+
CONFIG_MCUMGR_TRANSPORT_UDP_IPV4=y
5+
CONFIG_MCUMGR_TRANSPORT_UDP_IPV6=y
6+
7+
# Network settings
8+
CONFIG_NETWORKING=y
9+
CONFIG_NET_UDP=y
10+
CONFIG_NET_IPV4=y
11+
CONFIG_NET_IPV6=y
12+
CONFIG_NET_SOCKETS=y
13+
CONFIG_NET_SOCKETS_SOCKOPT_TLS=y
14+
CONFIG_NET_SOCKETS_ENABLE_DTLS=y
15+
CONFIG_NET_SOCKETS_DTLS_TIMEOUT=30000
16+
CONFIG_NET_SOCKETS_DTLS_MAX_FRAGMENT_LENGTH=2048
17+
CONFIG_NET_SOCKETS_DTLS_SENDMSG_BUF_SIZE=0
18+
CONFIG_NET_SOCKETS_TLS_MAX_CONTEXTS=4
19+
CONFIG_NET_SOCKETS_TLS_MAX_CREDENTIALS=4
20+
CONFIG_NET_SOCKETS_TLS_MAX_CIPHERSUITES=4
21+
CONFIG_NET_SOCKETS_TLS_MAX_CLIENT_SESSION_COUNT=1
22+
CONFIG_NET_CONNECTION_MANAGER=y
23+
CONFIG_NET_CONFIG_SETTINGS=y
24+
CONFIG_TEST_RANDOM_GENERATOR=y
25+
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.168.1.1"
26+
CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1"
27+
28+
# mbedtls settings
29+
CONFIG_MBEDTLS_TLS_VERSION_1_2=y
30+
CONFIG_MBEDTLS_DTLS=y
31+
CONFIG_MBEDTLS_RSA_C=y
32+
CONFIG_MBEDTLS_PKCS1_V15=y
33+
CONFIG_MBEDTLS_PKCS1_V21=y
34+
CONFIG_MBEDTLS_KEY_EXCHANGE_RSA_ENABLED=y
35+
CONFIG_MBEDTLS_MD=y
36+
CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=2048
37+
CONFIG_MBEDTLS_ENABLE_HEAP=y
38+
CONFIG_MBEDTLS_HEAP_SIZE=60000

0 commit comments

Comments
 (0)