1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 04:52:59 +01:00

net/sock/dtls: allow to set PSK Identity hint

This commit is contained in:
Leandro Lanzieri 2021-03-09 18:41:25 +01:00
parent 91bf7bc376
commit d7440ce1e3
No known key found for this signature in database
GPG Key ID: F4E9A721761C7593
4 changed files with 88 additions and 2 deletions

View File

@ -182,8 +182,16 @@ static int _get_psk_info(struct dtls_context_t *ctx, const session_t *session,
switch (type) {
case DTLS_PSK_HINT:
DEBUG("sock_dtls: psk hint request\n");
/* Ignored. See https://tools.ietf.org/html/rfc4279#section-5.2 */
return 0;
/* return a hint to the client if set */
c_len = strlen(sock->psk_hint);
if (c_len) {
c = sock->psk_hint;
break;
}
else {
DEBUG("sock_dtls: no hint provided\n");
return 0;
}
case DTLS_PSK_IDENTITY:
DEBUG("sock_dtls: psk id request\n");
c = credential.params.psk.id.s;
@ -279,6 +287,7 @@ int sock_dtls_create(sock_dtls_t *sock, sock_udp_t *udp_sock,
sock->udp_sock = udp_sock;
sock->buffer.data = NULL;
sock->psk_hint[0] = '\0';
#ifdef SOCK_HAS_ASYNC
sock->async_cb = NULL;
sock->buf_ctx = NULL;
@ -296,6 +305,17 @@ int sock_dtls_create(sock_dtls_t *sock, sock_udp_t *udp_sock,
return 0;
}
int sock_dtls_set_server_psk_id_hint(sock_dtls_t *sock, const char *hint)
{
assert(sock);
if (strlen(hint) > CONFIG_DTLS_PSK_ID_HINT_MAX_SIZE) {
DEBUG("sock_dtls: could not set hint due to buffer size\n");
return -1;
}
strcpy(sock->psk_hint, hint);
return 0;
}
sock_udp_t *sock_dtls_get_udp_sock(sock_dtls_t *sock)
{
assert(sock);

View File

@ -22,6 +22,7 @@
#include "dtls.h"
#include "net/sock/udp.h"
#include "net/credman.h"
#include "net/sock/dtls/creds.h"
#ifdef SOCK_HAS_ASYNC
#include "net/sock/async/types.h"
#endif
@ -76,6 +77,7 @@ struct sock_dtls {
} buffer;
credman_tag_t tag; /**< Credential tag of a registered
(D)TLS credential */
char psk_hint[CONFIG_DTLS_PSK_ID_HINT_MAX_SIZE]; /**< PSK Identity hint */
dtls_peer_type role; /**< DTLS role of the socket */
};

View File

@ -0,0 +1,60 @@
/*
* Copyright (C) 2021 HAW Hamburg
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @defgroup net_sock_dtls_creds DTLS sock credentials API
* @ingroup net_sock_dtls
* @brief Credential handling for DTLS sock
* @{
*
* @file
* @brief DTLS sock definitions
*
* @author Leandro Lanzieri <leandro.lanzieri@haw-hamburg.de>
*/
#ifndef NET_SOCK_DTLS_CREDS_H
#define NET_SOCK_DTLS_CREDS_H
#include "net/sock/udp.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @ingroup net_sock_dtls_conf
* @{
*/
/**
* @brief Default maximum size for the PSK Identity hint string
*/
#ifndef CONFIG_DTLS_PSK_ID_HINT_MAX_SIZE
#define CONFIG_DTLS_PSK_ID_HINT_MAX_SIZE 32
#endif
/** @} */
/**
* @brief Sets the PSK Identity hint to be sent to clients during handshake.
*
* This hint is optional. It helps clients to decide which PSK Identity to use.
*
* @param[in, out] sock The DTLS sock object to set the hint to.
* @param[in] hint PSK Identity hint as NULL-terminated string.
*
* @retval 0 on success
* @retval -1 on error
*/
int sock_dtls_set_server_psk_id_hint(sock_dtls_t *sock, const char *hint);
#ifdef __cplusplus
}
#endif
#endif /* NET_SOCK_DTLS_CREDS_H */
/** @} */

View File

@ -44,4 +44,8 @@ config DTLS_HANDSHAKE_BUFSIZE_EXP
represents the exponent of 2^n, which will be used as the size of the
buffer. The buffer is used to hold credentials during DTLS handshakes.
config DTLS_PSK_ID_HINT_MAX_SIZE
int "Maximum size for a PSK Identity hint string"
default 32
endif # KCONFIG_USEMODULE_SOCK_DTLS