Skip to content

Commit 571ad19

Browse files
maass-hamburgfabiobaltieri
authored andcommitted
mgmt: hawkbit: change the tls certificate tag
Be able to change the tls certicicate tag. Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
1 parent 7ae9a16 commit 571ad19

File tree

5 files changed

+89
-12
lines changed

5 files changed

+89
-12
lines changed

include/zephyr/mgmt/hawkbit.h

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#ifndef ZEPHYR_INCLUDE_MGMT_HAWKBIT_H_
1414
#define ZEPHYR_INCLUDE_MGMT_HAWKBIT_H_
1515

16+
#include <zephyr/net/tls_credentials.h>
17+
1618
#define HAWKBIT_JSON_URL "/default/controller/v1"
1719

1820
/**
@@ -47,6 +49,7 @@ struct hawkbit_runtime_config {
4749
char *server_addr;
4850
uint16_t server_port;
4951
char *auth_token;
52+
sec_tag_t tls_tag;
5053
};
5154

5255
/**
@@ -160,7 +163,7 @@ struct hawkbit_runtime_config hawkbit_get_config(void);
160163
static inline int hawkbit_set_server_addr(char *addr_str)
161164
{
162165
struct hawkbit_runtime_config set_config = {
163-
.server_addr = addr_str, .server_port = 0, .auth_token = NULL};
166+
.server_addr = addr_str, .server_port = 0, .auth_token = NULL, .tls_tag = 0};
164167

165168
return hawkbit_set_config(&set_config);
166169
}
@@ -175,7 +178,7 @@ static inline int hawkbit_set_server_addr(char *addr_str)
175178
static inline int hawkbit_set_server_port(uint16_t port)
176179
{
177180
struct hawkbit_runtime_config set_config = {
178-
.server_addr = NULL, .server_port = port, .auth_token = NULL};
181+
.server_addr = NULL, .server_port = port, .auth_token = NULL, .tls_tag = 0};
179182

180183
return hawkbit_set_config(&set_config);
181184
}
@@ -190,7 +193,22 @@ static inline int hawkbit_set_server_port(uint16_t port)
190193
static inline int hawkbit_set_ddi_security_token(char *token)
191194
{
192195
struct hawkbit_runtime_config set_config = {
193-
.server_addr = NULL, .server_port = 0, .auth_token = token};
196+
.server_addr = NULL, .server_port = 0, .auth_token = token, .tls_tag = 0};
197+
198+
return hawkbit_set_config(&set_config);
199+
}
200+
201+
/**
202+
* @brief Set the hawkBit TLS tag
203+
*
204+
* @param tag TLS tag to set.
205+
* @retval 0 on success.
206+
* @retval -EAGAIN if probe is currently running.
207+
*/
208+
static inline int hawkbit_set_tls_tag(sec_tag_t tag)
209+
{
210+
struct hawkbit_runtime_config set_config = {
211+
.server_addr = NULL, .server_port = 0, .auth_token = NULL, .tls_tag = tag};
194212

195213
return hawkbit_set_config(&set_config);
196214
}
@@ -225,6 +243,16 @@ static inline char *hawkbit_get_ddi_security_token(void)
225243
return hawkbit_get_config().auth_token;
226244
}
227245

246+
/**
247+
* @brief Get the hawkBit TLS tag.
248+
*
249+
* @return TLS tag.
250+
*/
251+
static inline sec_tag_t hawkbit_get_tls_tag(void)
252+
{
253+
return hawkbit_get_config().tls_tag;
254+
}
255+
228256
/**
229257
* @brief Get the hawkBit action id.
230258
*

samples/subsys/mgmt/hawkbit/overlay-tls.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ CONFIG_MBEDTLS_HEAP_SIZE=60000
88
CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=16384
99

1010
CONFIG_NET_SOCKETS_SOCKOPT_TLS=y
11+
CONFIG_HAWKBIT_USE_TLS=y

samples/subsys/mgmt/hawkbit/sample.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ tests:
2929
- CONFIG_HAWKBIT_DDI_SECURITY_TOKEN="abcd1234"
3030
sample.net.hawkbit.tls:
3131
extra_configs:
32+
- CONFIG_HAWKBIT_USE_TLS=y
3233
- CONFIG_NET_SOCKETS_SOCKOPT_TLS=y
3334
sample.net.hawkbit.set_settings_runtime:
3435
extra_configs:

subsys/mgmt/hawkbit/Kconfig

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,40 @@ config HAWKBIT_DEVICE_ID_MAX_LENGTH
111111
help
112112
Maximum length of the device id.
113113

114+
config HAWKBIT_USE_TLS
115+
bool "Use TLS for hawkBit server connection"
116+
depends on NET_SOCKETS_SOCKOPT_TLS
117+
help
118+
Use TLS for hawkBit connection.
119+
120+
if HAWKBIT_USE_TLS
121+
122+
choice HAWKBIT_CERT_TAG
123+
prompt "hawkBit certificate tag"
124+
default HAWKBIT_USE_STATIC_CERT_TAG
125+
126+
config HAWKBIT_USE_STATIC_CERT_TAG
127+
bool "Use static certificate tag"
128+
help
129+
Use static certificate tag for TLS connection to the hawkBit server.
130+
131+
config HAWKBIT_USE_DYNAMIC_CERT_TAG
132+
bool "Use dynamic certificate tag"
133+
depends on HAWKBIT_SET_SETTINGS_RUNTIME
134+
help
135+
Use dynamic certificate tag for TLS connection to the hawkBit server.
136+
137+
endchoice
138+
139+
config HAWKBIT_STATIC_CERT_TAG
140+
int "Static certificate tag"
141+
depends on HAWKBIT_USE_STATIC_CERT_TAG
142+
default 1
143+
help
144+
Static certificate tag for TLS connection to the hawkBit server.
145+
146+
endif
147+
114148
module = HAWKBIT
115149
module-str = Log Level for hawkbit
116150
module-help = Enables logging for hawkBit code.

subsys/mgmt/hawkbit/hawkbit.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@
3131
#include "hawkbit_firmware.h"
3232
#include "hawkbit_priv.h"
3333

34-
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
35-
#define CA_CERTIFICATE_TAG 1
36-
#include <zephyr/net/tls_credentials.h>
37-
#endif
38-
3934
LOG_MODULE_REGISTER(hawkbit, CONFIG_HAWKBIT_LOG_LEVEL);
4035

4136
#define CANCEL_BASE_SIZE 50
@@ -82,6 +77,9 @@ static struct hawkbit_config {
8277
#ifndef CONFIG_HAWKBIT_DDI_NO_SECURITY
8378
char ddi_security_token[DDI_SECURITY_TOKEN_SIZE + 1];
8479
#endif
80+
#ifdef CONFIG_HAWKBIT_USE_DYNAMIC_CERT_TAG
81+
sec_tag_t tls_tag;
82+
#endif
8583
#endif /* CONFIG_HAWKBIT_SET_SETTINGS_RUNTIME */
8684
} hb_cfg;
8785

@@ -103,6 +101,14 @@ static struct hawkbit_config {
103101
#define HAWKBIT_DDI_SECURITY_TOKEN CONFIG_HAWKBIT_DDI_SECURITY_TOKEN
104102
#endif /* CONFIG_HAWKBIT_DDI_NO_SECURITY */
105103

104+
#ifdef CONFIG_HAWKBIT_USE_DYNAMIC_CERT_TAG
105+
#define HAWKBIT_CERT_TAG hb_cfg.tls_tag
106+
#elif defined(HAWKBIT_USE_STATIC_CERT_TAG)
107+
#define HAWKBIT_CERT_TAG CONFIG_HAWKBIT_STATIC_CERT_TAG
108+
#else
109+
#define HAWKBIT_CERT_TAG 0
110+
#endif /* CONFIG_HAWKBIT_USE_DYNAMIC_CERT_TAG */
111+
106112
struct hawkbit_download {
107113
int download_status;
108114
int download_progress;
@@ -341,7 +347,7 @@ static bool start_http_client(void)
341347
struct zsock_addrinfo *addr;
342348
struct zsock_addrinfo hints = {0};
343349
int resolve_attempts = 10;
344-
int protocol = IS_ENABLED(CONFIG_NET_SOCKETS_SOCKOPT_TLS) ? IPPROTO_TLS_1_2 : IPPROTO_TCP;
350+
int protocol = IS_ENABLED(CONFIG_HAWKBIT_USE_TLS) ? IPPROTO_TLS_1_2 : IPPROTO_TCP;
345351

346352
if (IS_ENABLED(CONFIG_NET_IPV6)) {
347353
hints.ai_family = AF_INET6;
@@ -371,9 +377,9 @@ static bool start_http_client(void)
371377
goto err;
372378
}
373379

374-
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
380+
#ifdef CONFIG_HAWKBIT_USE_TLS
375381
sec_tag_t sec_tag_opt[] = {
376-
CA_CERTIFICATE_TAG,
382+
HAWKBIT_CERT_TAG,
377383
};
378384

379385
if (zsock_setsockopt(hb_context.sock, SOL_TLS, TLS_SEC_TAG_LIST, sec_tag_opt,
@@ -385,7 +391,7 @@ static bool start_http_client(void)
385391
sizeof(HAWKBIT_SERVER)) < 0) {
386392
goto err_sock;
387393
}
388-
#endif
394+
#endif /* CONFIG_HAWKBIT_USE_TLS */
389395

390396
if (zsock_connect(hb_context.sock, addr->ai_addr, addr->ai_addrlen) < 0) {
391397
LOG_ERR("Failed to connect to server");
@@ -759,6 +765,12 @@ int hawkbit_set_config(struct hawkbit_runtime_config *config)
759765
hb_cfg.ddi_security_token);
760766
}
761767
#endif /* CONFIG_HAWKBIT_DDI_NO_SECURITY */
768+
#ifdef CONFIG_HAWKBIT_USE_DYNAMIC_CERT_TAG
769+
if (config->tls_tag != 0) {
770+
hb_cfg.tls_tag = config->tls_tag;
771+
LOG_DBG("configured %s: %d", "hawkbit/tls_tag", hb_cfg.tls_tag);
772+
}
773+
#endif /* CONFIG_HAWKBIT_USE_DYNAMIC_CERT_TAG */
762774
settings_save();
763775
k_sem_give(&probe_sem);
764776
} else {
@@ -776,6 +788,7 @@ struct hawkbit_runtime_config hawkbit_get_config(void)
776788
.server_addr = HAWKBIT_SERVER,
777789
.server_port = HAWKBIT_PORT_INT,
778790
.auth_token = HAWKBIT_DDI_SECURITY_TOKEN,
791+
.tls_tag = HAWKBIT_CERT_TAG,
779792
};
780793

781794
return config;

0 commit comments

Comments
 (0)