Skip to content

Commit 73a3438

Browse files
SeppoTakalofabiobaltieri
authored andcommitted
net: lwm2m: Remove hostname_verify flag from context
Use security mode (PSK or X509) to detect if we should set the socket option to verify hostname. PSK security mode cannot verify hostnames as this information is coming in the certificate, so don't set the options. Signed-off-by: Seppo Takalo <[email protected]>
1 parent 8c0026a commit 73a3438

File tree

4 files changed

+4
-10
lines changed

4 files changed

+4
-10
lines changed

include/zephyr/net/lwm2m.h

-2
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,6 @@ struct lwm2m_ctx {
245245
char *desthostname;
246246
/** Destination hostname length */
247247
uint16_t desthostnamelen;
248-
/** Flag to indicate if hostname verification is enabled */
249-
bool hostname_verify;
250248

251249
/** Custom load_credentials function.
252250
* Client can set load_credentials function as a way of overriding

subsys/net/lib/lwm2m/lwm2m_engine.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ int lwm2m_set_default_sockopt(struct lwm2m_ctx *ctx)
10701070
}
10711071
}
10721072

1073-
if (ctx->hostname_verify && (ctx->desthostname != NULL)) {
1073+
if (ctx->desthostname != NULL && lwm2m_security_mode(ctx) == LWM2M_SECURITY_CERT) {
10741074
/** store character at len position */
10751075
tmp = ctx->desthostname[ctx->desthostnamelen];
10761076

subsys/net/lib/lwm2m/lwm2m_message_handling.c

-1
Original file line numberDiff line numberDiff line change
@@ -3324,7 +3324,6 @@ int lwm2m_parse_peerinfo(char *url, struct lwm2m_ctx *client_ctx, bool is_firmwa
33243324
/** copy url pointer to be used in socket */
33253325
client_ctx->desthostname = url + off;
33263326
client_ctx->desthostnamelen = len;
3327-
client_ctx->hostname_verify = true;
33283327
#endif
33293328

33303329
#else

tests/net/lib/lwm2m/lwm2m_engine/src/main.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ ZTEST(lwm2m_engine, test_start_stop)
105105
ctx.load_credentials = NULL;
106106
ctx.desthostname = host_name;
107107
ctx.desthostnamelen = strlen(host_name);
108-
ctx.hostname_verify = true;
109108
ctx.use_dtls = true;
110109

111110
ret = lwm2m_engine_start(&ctx);
@@ -436,7 +435,6 @@ ZTEST(lwm2m_engine, test_security)
436435
ctx.load_credentials = NULL;
437436
ctx.desthostname = host_name;
438437
ctx.desthostnamelen = strlen(host_name);
439-
ctx.hostname_verify = true;
440438
ctx.use_dtls = false;
441439

442440
lwm2m_security_mode_fake.return_val = LWM2M_SECURITY_NOSEC;
@@ -452,9 +450,8 @@ ZTEST(lwm2m_engine, test_security)
452450
lwm2m_security_mode_fake.return_val = LWM2M_SECURITY_PSK;
453451
zassert_equal(lwm2m_engine_start(&ctx), 0);
454452
zassert_equal(z_impl_zsock_setsockopt_fake.arg2_history[0], TLS_SEC_TAG_LIST);
455-
zassert_equal(z_impl_zsock_setsockopt_fake.arg2_history[1], TLS_HOSTNAME);
456-
zassert_equal(z_impl_zsock_setsockopt_fake.arg2_history[2], TLS_PEER_VERIFY);
457-
zassert_equal(z_impl_zsock_setsockopt_fake.arg2_history[3], TLS_CIPHERSUITE_LIST);
453+
zassert_equal(z_impl_zsock_setsockopt_fake.arg2_history[1], TLS_PEER_VERIFY);
454+
zassert_equal(z_impl_zsock_setsockopt_fake.arg2_history[2], TLS_CIPHERSUITE_LIST);
458455
zassert_true(tls_credential_delete_fake.call_count > 3);
459456
zassert_true(tls_credential_add_fake.call_count == 2);
460457
zassert_equal(tls_credential_add_fake.arg1_history[0], TLS_CREDENTIAL_PSK_ID);
@@ -464,7 +461,7 @@ ZTEST(lwm2m_engine, test_security)
464461
RESET_FAKE(z_impl_zsock_setsockopt);
465462
RESET_FAKE(tls_credential_add);
466463
lwm2m_security_mode_fake.return_val = LWM2M_SECURITY_CERT;
467-
ctx.hostname_verify = false;
464+
ctx.desthostname = NULL;
468465
zassert_equal(lwm2m_engine_start(&ctx), 0);
469466
zassert_equal(z_impl_zsock_setsockopt_fake.arg2_history[0], TLS_SEC_TAG_LIST);
470467
zassert_equal(z_impl_zsock_setsockopt_fake.arg2_history[1], TLS_PEER_VERIFY);

0 commit comments

Comments
 (0)