dtls.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 HAW Hamburg
3  * Freie Universität Berlin
4  * Inria
5  * Daniele Lacamera
6  * Ken Bannister
7  *
8  * This file is subject to the terms and conditions of the GNU Lesser
9  * General Public License v2.1. See the file LICENSE in the top level
10  * directory for more details.
11  */
12 
530 #ifndef NET_SOCK_DTLS_H
531 #define NET_SOCK_DTLS_H
532 
533 #include <assert.h>
534 #include <errno.h>
535 #include <stdint.h>
536 #include <stdlib.h>
537 #include <sys/types.h>
538 
539 /* net/sock/async/types.h included by net/sock.h needs to re-typedef the
540  * `sock_dtls_t` to prevent cyclic includes */
541 #if defined (__clang__)
542 # pragma clang diagnostic push
543 # pragma clang diagnostic ignored "-Wtypedef-redefinition"
544 #endif
545 
546 #include "net/sock.h"
547 #include "net/sock/udp.h"
548 #include "net/credman.h"
549 
550 #ifdef __cplusplus
551 extern "C" {
552 #endif
553 
567 #ifndef CONFIG_DTLS_HANDSHAKE_BUFSIZE_EXP
568 #define CONFIG_DTLS_HANDSHAKE_BUFSIZE_EXP 8
569 #endif
575 #ifndef DTLS_HANDSHAKE_BUFSIZE
576 #define DTLS_HANDSHAKE_BUFSIZE (1 << CONFIG_DTLS_HANDSHAKE_BUFSIZE_EXP)
577 #endif
578 
582 #define SOCK_DTLS_HANDSHAKE (EXDEV)
583 
587 #ifndef CONFIG_DTLS_FORCE_EXTENDED_MASTER_SECRET
588 #define CONFIG_DTLS_FORCE_EXTENDED_MASTER_SECRET 1
589 #endif
590 
594 #ifndef CONFIG_DTLS_FORCE_RENEGOTIATION_INFO
595 #define CONFIG_DTLS_FORCE_RENEGOTIATION_INFO 1
596 #endif
597 
603 enum {
607 };
615 enum {
618 };
627 typedef struct sock_dtls sock_dtls_t;
628 
629 #if defined (__clang__)
630 # pragma clang diagnostic pop
631 #endif
632 
637 
647 
657 
663 void sock_dtls_init(void);
664 
688  credman_tag_t tag, unsigned version, unsigned role);
689 
700 
719  sock_dtls_session_t *remote);
720 
735 
745  sock_udp_ep_t *ep);
746 
759  const sock_udp_ep_t *ep);
760 
791  void *data, size_t maxlen, uint32_t timeout,
792  sock_dtls_aux_rx_t *aux);
793 
821 static inline ssize_t sock_dtls_recv(sock_dtls_t *sock,
822  sock_dtls_session_t *remote,
823  void *data, size_t maxlen,
824  uint32_t timeout)
825 {
826  return sock_dtls_recv_aux(sock, remote, data, maxlen, timeout, NULL);
827 }
828 
872  void **data, void **buf_ctx, uint32_t timeout,
873  sock_dtls_aux_rx_t *aux);
874 
915 static inline ssize_t sock_dtls_recv_buf(sock_dtls_t *sock,
916  sock_dtls_session_t *remote,
917  void **data, void **buf_ctx,
918  uint32_t timeout)
919 {
920  return sock_dtls_recv_buf_aux(sock, remote, data, buf_ctx, timeout, NULL);
921 }
922 
958  const iolist_t *snips, uint32_t timeout,
959  sock_dtls_aux_tx_t *aux);
960 
995 static inline ssize_t sock_dtls_send_aux(sock_dtls_t *sock,
996  sock_dtls_session_t *remote,
997  const void *data, size_t len,
998  uint32_t timeout,
999  sock_dtls_aux_tx_t *aux)
1000 {
1001  const iolist_t snip = {
1002  .iol_base = (void *)data,
1003  .iol_len = len,
1004  };
1005 
1006  return sock_dtls_sendv_aux(sock, remote, &snip, timeout, aux);
1007 }
1008 
1049 static inline ssize_t sock_dtls_send(sock_dtls_t *sock,
1050  sock_dtls_session_t *remote,
1051  const void *data, size_t len,
1052  uint32_t timeout)
1053 {
1054  return sock_dtls_send_aux(sock, remote, data, len, timeout, NULL);
1055 }
1056 
1097 static inline ssize_t sock_dtls_sendv(sock_dtls_t *sock,
1098  sock_dtls_session_t *remote,
1099  const iolist_t *snips,
1100  uint32_t timeout)
1101 {
1102  return sock_dtls_sendv_aux(sock, remote, snips, timeout, NULL);
1103 }
1104 
1118 
1119 #ifdef MODULE_SOCK_DTLS
1120 #include "sock_dtls_types.h"
1121 #endif
1122 
1123 #ifdef __cplusplus
1124 }
1125 #endif
1126 
1127 #endif /* NET_SOCK_DTLS_H */
POSIX.1-2008 compliant version of the assert macro.
(D)TLS credentials management module definitions
uint16_t credman_tag_t
Tag of the credential.
Definition: credman.h:96
static ssize_t sock_dtls_recv_buf(sock_dtls_t *sock, sock_dtls_session_t *remote, void **data, void **buf_ctx, uint32_t timeout)
Decrypts and provides stack-internal buffer space containing a message from a remote peer.
Definition: dtls.h:915
int sock_dtls_create(sock_dtls_t *sock, sock_udp_t *udp_sock, credman_tag_t tag, unsigned version, unsigned role)
Creates a new DTLS sock object.
static ssize_t sock_dtls_sendv(sock_dtls_t *sock, sock_dtls_session_t *remote, const iolist_t *snips, uint32_t timeout)
Encrypts and sends a message to a remote peer with non-continuous payload.
Definition: dtls.h:1097
static ssize_t sock_dtls_send_aux(sock_dtls_t *sock, sock_dtls_session_t *remote, const void *data, size_t len, uint32_t timeout, sock_dtls_aux_tx_t *aux)
Encrypts and sends a message to a remote peer.
Definition: dtls.h:995
ssize_t sock_dtls_recv_buf_aux(sock_dtls_t *sock, sock_dtls_session_t *remote, void **data, void **buf_ctx, uint32_t timeout, sock_dtls_aux_rx_t *aux)
Decrypts and provides stack-internal buffer space containing a message from a remote peer.
sock_udp_t * sock_dtls_get_udp_sock(sock_dtls_t *sock)
Get underlying UDP sock.
void sock_dtls_close(sock_dtls_t *sock)
Closes a DTLS sock.
void sock_dtls_init(void)
Called exactly once during auto_init.
void sock_dtls_session_destroy(sock_dtls_t *sock, sock_dtls_session_t *remote)
Destroys an existing DTLS session.
static ssize_t sock_dtls_send(sock_dtls_t *sock, sock_dtls_session_t *remote, const void *data, size_t len, uint32_t timeout)
Encrypts and sends a message to a remote peer.
Definition: dtls.h:1049
int sock_dtls_session_init(sock_dtls_t *sock, const sock_udp_ep_t *ep, sock_dtls_session_t *remote)
Initialize session handshake.
static ssize_t sock_dtls_recv(sock_dtls_t *sock, sock_dtls_session_t *remote, void *data, size_t maxlen, uint32_t timeout)
Receive handshake messages and application data from remote peer.
Definition: dtls.h:821
ssize_t sock_dtls_recv_aux(sock_dtls_t *sock, sock_dtls_session_t *remote, void *data, size_t maxlen, uint32_t timeout, sock_dtls_aux_rx_t *aux)
Receive handshake messages and application data from remote peer.
sock_udp_aux_rx_t sock_dtls_aux_rx_t
Auxiliary data provided when receiving using an DTLS sock object.
Definition: dtls.h:646
sock_udp_aux_tx_t sock_dtls_aux_tx_t
Auxiliary data provided when sending using an DTLS sock object.
Definition: dtls.h:656
void sock_dtls_session_get_udp_ep(const sock_dtls_session_t *session, sock_udp_ep_t *ep)
Get the remote UDP endpoint from a session.
void sock_dtls_session_set_udp_ep(sock_dtls_session_t *session, const sock_udp_ep_t *ep)
Set the remote UDP endpoint from a session.
ssize_t sock_dtls_sendv_aux(sock_dtls_t *sock, sock_dtls_session_t *remote, const iolist_t *snips, uint32_t timeout, sock_dtls_aux_tx_t *aux)
Encrypts and sends a message to a remote peer with non-continuous payload.
@ SOCK_DTLS_1_0
DTLS version 1.0.
Definition: dtls.h:604
@ SOCK_DTLS_1_2
DTLS version 1.2.
Definition: dtls.h:605
@ SOCK_DTLS_1_3
DTLS version 1.3.
Definition: dtls.h:606
@ SOCK_DTLS_CLIENT
Endpoint client role.
Definition: dtls.h:616
@ SOCK_DTLS_SERVER
Endpoint server role.
Definition: dtls.h:617
UDP sock definitions.
Common sock API definitions.
tinydtls-specific types and functions definitions
Common IP-based transport layer end point.
Definition: sock.h:215
iolist structure definition
Definition: iolist.h:39
void * iol_base
ptr to this list entries data
Definition: iolist.h:41
Information about remote client connected to the server.
Information about DTLS sock.
Auxiliary data provided when receiving using an UDP sock object.
Definition: udp.h:310
Auxiliary data provided when sending using an UDP sock object.
Definition: udp.h:349
UDP sock type.
Definition: sock_types.h:128