Skip to content

Commit

Permalink
coap-client.c: Fix SNI generation
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdeep1 committed Jun 6, 2024
1 parent e095d5e commit 161e03d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
16 changes: 8 additions & 8 deletions examples/coap-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1408,11 +1408,11 @@ setup_pki(coap_context_t *ctx) {
}
dtls_pki.is_rpk_not_cert = is_rpk_not_cert;
dtls_pki.validate_cn_call_back = verify_cn_callback;
if ((uri.host.length == 3 && memcmp(uri.host.s, "::1", 3) != 0) ||
(uri.host.length == 9 && memcmp(uri.host.s, "127.0.0.1", 9) != 0))
memcpy(client_sni, uri.host.s, min(uri.host.length, sizeof(client_sni)-1));
else
if ((uri.host.length == 3 && memcmp(uri.host.s, "::1", 3) == 0) ||
(uri.host.length == 9 && memcmp(uri.host.s, "127.0.0.1", 9) == 0))
memcpy(client_sni, "localhost", 9);
else
memcpy(client_sni, uri.host.s, min(uri.host.length, sizeof(client_sni)-1));

dtls_pki.client_sni = client_sni;
if (doing_tls_engine) {
Expand Down Expand Up @@ -1472,11 +1472,11 @@ setup_psk(const uint8_t *identity,
dtls_psk.validate_ih_call_back = verify_ih_callback;
}
dtls_psk.ih_call_back_arg = &dtls_psk.psk_info;
if ((uri.host.length == 3 && memcmp(uri.host.s, "::1", 3) != 0) ||
(uri.host.length == 9 && memcmp(uri.host.s, "127.0.0.1", 9) != 0))
memcpy(client_sni, uri.host.s, min(uri.host.length, sizeof(client_sni)-1));
else
if ((uri.host.length == 3 && memcmp(uri.host.s, "::1", 3) == 0) ||
(uri.host.length == 9 && memcmp(uri.host.s, "127.0.0.1", 9) == 0))
memcpy(client_sni, "localhost", 9);
else
memcpy(client_sni, uri.host.s, min(uri.host.length, sizeof(client_sni)-1));
dtls_psk.client_sni = client_sni;
dtls_psk.psk_info.identity.s = identity;
dtls_psk.psk_info.identity.length = identity_len;
Expand Down
8 changes: 4 additions & 4 deletions examples/coap-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -940,11 +940,11 @@ get_ongoing_proxy_session(coap_session_t *session,
case COAP_URI_SCHEME_COAPS_TCP:
case COAP_URI_SCHEME_COAPS_WS:
memset(client_sni, 0, sizeof(client_sni));
if ((server.length == 3 && memcmp(server.s, "::1", 3) != 0) ||
(server.length == 9 && memcmp(server.s, "127.0.0.1", 9) != 0))
memcpy(client_sni, server.s, min(server.length, sizeof(client_sni)-1));
else
if ((server.length == 3 && memcmp(server.s, "::1", 3) == 0) ||
(server.length == 9 && memcmp(server.s, "127.0.0.1", 9) == 0))
memcpy(client_sni, "localhost", 9);
else
memcpy(client_sni, server.s, min(server.length, sizeof(client_sni)-1));

if (!key_defined) {
/* Use our defined PKI certs (or NULL) */
Expand Down
8 changes: 4 additions & 4 deletions examples/lwip/client-coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@ client_coap_init(coap_lwip_input_wait_handler_t input_wait, void *input_arg,
memset(client_sni, 0, sizeof(client_sni));
memset(&dtls_psk, 0, sizeof(dtls_psk));
dtls_psk.version = COAP_DTLS_CPSK_SETUP_VERSION;
if (uri.host.length)
memcpy(client_sni, uri.host.s,
min(uri.host.length, sizeof(client_sni) - 1));
else
if ((uri.host.length == 3 && memcmp(uri.host.s, "::1", 3) == 0) ||
(uri.host.length == 9 && memcmp(uri.host.s, "127.0.0.1", 9) == 0))
memcpy(client_sni, "localhost", 9);
else
memcpy(client_sni, uri.host.s, min(uri.host.length, sizeof(client_sni)-1));
dtls_psk.client_sni = client_sni;
dtls_psk.psk_info.identity.s = (const uint8_t *)use_id;
dtls_psk.psk_info.identity.length = strlen(use_id);
Expand Down
12 changes: 8 additions & 4 deletions examples/riot/examples_libcoap_client/client-coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include <stdio.h>
#include "macros/utils.h"

#ifndef min
#define min(a,b) ((a) < (b) ? (a) : (b))
#endif

#ifdef CONFIG_LIBCOAP_CLIENT_URI
#define COAP_CLIENT_URI CONFIG_LIBCOAP_CLIENT_URI
#else /* ! CONFIG_LIBCOAP_CLIENT_URI */
Expand Down Expand Up @@ -186,12 +190,12 @@ client_coap_init(int argc, char **argv)
memset(client_sni, 0, sizeof(client_sni));
memset(&dtls_psk, 0, sizeof(dtls_psk));
dtls_psk.version = COAP_DTLS_CPSK_SETUP_VERSION;
if (uri.host.length) {
memcpy(client_sni, uri.host.s,
MIN(uri.host.length, sizeof(client_sni) - 1));
if ((uri.host.length == 3 && memcmp(uri.host.s, "::1", 3) == 0) ||
(uri.host.length == 9 && memcmp(uri.host.s, "127.0.0.1", 9) == 0)) {
memcpy(client_sni, "localhost", 9);
}
else {
memcpy(client_sni, "localhost", 9);
memcpy(client_sni, uri.host.s, min(uri.host.length, sizeof(client_sni)-1));
}
dtls_psk.client_sni = client_sni;
dtls_psk.psk_info.identity.s = (const uint8_t *)COAP_USE_PSK_ID;
Expand Down

0 comments on commit 161e03d

Please sign in to comment.