1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-17 18:43:50 +01:00

tinydtls_sock_dtls: save session information after data decrypted

This commit is contained in:
M Aiman Ismail 2020-06-18 11:44:02 +00:00
parent 2bdf9b16ee
commit 2e16b90b38
3 changed files with 26 additions and 17 deletions

View File

@ -86,7 +86,6 @@ void *dtls_server_wrapper(void *arg)
/* Prepare (thread) messages reception */ /* Prepare (thread) messages reception */
msg_init_queue(_reader_queue, READER_QUEUE_SIZE); msg_init_queue(_reader_queue, READER_QUEUE_SIZE);
sock_dtls_session_t session;
sock_dtls_t sock; sock_dtls_t sock;
sock_udp_t udp_sock; sock_udp_t udp_sock;
sock_udp_ep_t local = SOCK_IPV6_EP_ANY; sock_udp_ep_t local = SOCK_IPV6_EP_ANY;
@ -113,6 +112,7 @@ void *dtls_server_wrapper(void *arg)
active = false; active = false;
} }
else { else {
sock_dtls_session_t session = { 0 };
res = sock_dtls_recv(&sock, &session, rcv, sizeof(rcv), res = sock_dtls_recv(&sock, &session, rcv, sizeof(rcv),
10 * US_PER_SEC); 10 * US_PER_SEC);
if (res >= 0) { if (res >= 0) {
@ -121,14 +121,13 @@ void *dtls_server_wrapper(void *arg)
if (res < 0) { if (res < 0) {
printf("Error resending DTLS message: %d", (int)res); printf("Error resending DTLS message: %d", (int)res);
} }
sock_dtls_session_destroy(&sock, &session);
} }
else if (res == -SOCK_DTLS_HANDSHAKE) { else if (res == -SOCK_DTLS_HANDSHAKE) {
printf("New client connected\n"); printf("New client connected\n");
} }
} }
} }
sock_dtls_session_destroy(&sock, &session);
sock_dtls_close(&sock); sock_dtls_close(&sock);
sock_udp_close(&udp_sock); sock_udp_close(&udp_sock);
puts("Terminating"); puts("Terminating");

View File

@ -74,8 +74,9 @@ static int _read(struct dtls_context_t *ctx, session_t *session, uint8_t *buf,
sock_dtls_t *sock = dtls_get_app_data(ctx); sock_dtls_t *sock = dtls_get_app_data(ctx);
DEBUG("sock_dtls: decrypted message arrived\n"); DEBUG("sock_dtls: decrypted message arrived\n");
sock->buf = buf; sock->buffer.data = buf;
sock->buflen = len; sock->buffer.datalen = len;
sock->buffer.session = session;
return len; return len;
} }
@ -246,7 +247,7 @@ int sock_dtls_create(sock_dtls_t *sock, sock_udp_t *udp_sock,
} }
sock->udp_sock = udp_sock; sock->udp_sock = udp_sock;
sock->buf = NULL; sock->buffer.data = NULL;
sock->role = role; sock->role = role;
sock->tag = tag; sock->tag = tag;
sock->dtls_ctx = dtls_new_context(sock); sock->dtls_ctx = dtls_new_context(sock);
@ -367,18 +368,22 @@ ssize_t sock_dtls_send(sock_dtls_t *sock, sock_dtls_session_t *remote,
(uint8_t *)data, len); (uint8_t *)data, len);
} }
static ssize_t _copy_buffer(sock_dtls_t *sock, void *data, size_t max_len) static ssize_t _copy_buffer(sock_dtls_t *sock, sock_dtls_session_t *remote,
void *data, size_t max_len)
{ {
uint8_t *buf = sock->buf; uint8_t *buf = sock->buffer.data;
size_t buflen = sock->buflen; size_t buflen = sock->buffer.datalen;
sock->buf = NULL; sock->buffer.data = NULL;
if (buflen > max_len) { if (buflen > max_len) {
return -ENOBUFS; return -ENOBUFS;
} }
/* use `memmove()` as tinydtls reuses `data` to store decrypted data with an /* use `memmove()` as tinydtls reuses `data` to store decrypted data with an
* offset in `buf`. This prevents problems with overlapping buffers. */ * offset in `buf`. This prevents problems with overlapping buffers. */
memmove(data, buf, buflen); memmove(data, buf, buflen);
memcpy(&remote->dtls_session, sock->buffer.session,
sizeof(remote->dtls_session));
_session_to_ep(&remote->dtls_session, &remote->ep);
return buflen; return buflen;
} }
@ -389,9 +394,9 @@ ssize_t sock_dtls_recv(sock_dtls_t *sock, sock_dtls_session_t *remote,
assert(data); assert(data);
assert(remote); assert(remote);
if (sock->buf != NULL) { if (sock->buffer.data != NULL) {
/* there is already decrypted data available */ /* there is already decrypted data available */
return _copy_buffer(sock, data, max_len); return _copy_buffer(sock, remote, data, max_len);
} }
/* loop breaks when timeout or application data read */ /* loop breaks when timeout or application data read */
@ -413,8 +418,8 @@ ssize_t sock_dtls_recv(sock_dtls_t *sock, sock_dtls_session_t *remote,
} }
msg_t msg; msg_t msg;
if (sock->buf != NULL) { if (sock->buffer.data != NULL) {
return _copy_buffer(sock, data, max_len); return _copy_buffer(sock, remote, data, max_len);
} }
else if (mbox_try_get(&sock->mbox, &msg) && else if (mbox_try_get(&sock->mbox, &msg) &&
msg.type == DTLS_EVENT_CONNECTED) { msg.type == DTLS_EVENT_CONNECTED) {

View File

@ -41,9 +41,14 @@ struct sock_dtls {
handling */ handling */
msg_t mbox_queue[SOCK_DTLS_MBOX_SIZE]; /**< Queue for struct msg_t mbox_queue[SOCK_DTLS_MBOX_SIZE]; /**< Queue for struct
sock_dtls::mbox */ sock_dtls::mbox */
uint8_t *buf; /**< Buffer to pass decrypted data /**
back to user */ * @brief Buffer used to pass decrypted data and its session information.
size_t buflen; /**< Size of buffer */ */
struct {
uint8_t *data; /**< Pointer to the decrypted data */
size_t datalen; /**< data length */
session_t *session; /**< Session information */
} buffer;
credman_tag_t tag; /**< Credential tag of a registered credman_tag_t tag; /**< Credential tag of a registered
(D)TLS credential */ (D)TLS credential */
dtls_peer_type role; /**< DTLS role of the socket */ dtls_peer_type role; /**< DTLS role of the socket */