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 
13 #pragma once
14 
532 #include <assert.h>
533 #include <errno.h>
534 #include <stdint.h>
535 #include <stdlib.h>
536 #include <sys/types.h>
537 
538 /* net/sock/async/types.h included by net/sock.h needs to re-typedef the
539  * `sock_dtls_t` to prevent cyclic includes */
540 #if defined (__clang__)
541 # pragma clang diagnostic push
542 # pragma clang diagnostic ignored "-Wtypedef-redefinition"
543 #endif
544 
545 #include "net/sock.h"
546 #include "net/sock/udp.h"
547 #include "net/credman.h"
548 
549 #ifdef __cplusplus
550 extern "C" {
551 #endif
552 
566 #ifndef CONFIG_DTLS_HANDSHAKE_BUFSIZE_EXP
567 #define CONFIG_DTLS_HANDSHAKE_BUFSIZE_EXP 8
568 #endif
574 #ifndef DTLS_HANDSHAKE_BUFSIZE
575 #define DTLS_HANDSHAKE_BUFSIZE (1 << CONFIG_DTLS_HANDSHAKE_BUFSIZE_EXP)
576 #endif
577 
581 #define SOCK_DTLS_HANDSHAKE (EXDEV)
582 
586 #ifndef CONFIG_DTLS_FORCE_EXTENDED_MASTER_SECRET
587 #define CONFIG_DTLS_FORCE_EXTENDED_MASTER_SECRET 1
588 #endif
589 
593 #ifndef CONFIG_DTLS_FORCE_RENEGOTIATION_INFO
594 #define CONFIG_DTLS_FORCE_RENEGOTIATION_INFO 1
595 #endif
596 
602 enum {
606 };
614 enum {
617 };
626 typedef struct sock_dtls sock_dtls_t;
627 
628 #if defined (__clang__)
629 # pragma clang diagnostic pop
630 #endif
631 
636 
646 
656 
662 void sock_dtls_init(void);
663 
687  credman_tag_t tag, unsigned version, unsigned role);
688 
699 
718  sock_dtls_session_t *remote);
719 
734 
744  sock_udp_ep_t *ep);
745 
758  const sock_udp_ep_t *ep);
759 
790  void *data, size_t maxlen, uint32_t timeout,
791  sock_dtls_aux_rx_t *aux);
792 
820 static inline ssize_t sock_dtls_recv(sock_dtls_t *sock,
821  sock_dtls_session_t *remote,
822  void *data, size_t maxlen,
823  uint32_t timeout)
824 {
825  return sock_dtls_recv_aux(sock, remote, data, maxlen, timeout, NULL);
826 }
827 
871  void **data, void **buf_ctx, uint32_t timeout,
872  sock_dtls_aux_rx_t *aux);
873 
914 static inline ssize_t sock_dtls_recv_buf(sock_dtls_t *sock,
915  sock_dtls_session_t *remote,
916  void **data, void **buf_ctx,
917  uint32_t timeout)
918 {
919  return sock_dtls_recv_buf_aux(sock, remote, data, buf_ctx, timeout, NULL);
920 }
921 
957  const iolist_t *snips, uint32_t timeout,
958  sock_dtls_aux_tx_t *aux);
959 
994 static inline ssize_t sock_dtls_send_aux(sock_dtls_t *sock,
995  sock_dtls_session_t *remote,
996  const void *data, size_t len,
997  uint32_t timeout,
998  sock_dtls_aux_tx_t *aux)
999 {
1000  const iolist_t snip = {
1001  .iol_base = (void *)data,
1002  .iol_len = len,
1003  };
1004 
1005  return sock_dtls_sendv_aux(sock, remote, &snip, timeout, aux);
1006 }
1007 
1048 static inline ssize_t sock_dtls_send(sock_dtls_t *sock,
1049  sock_dtls_session_t *remote,
1050  const void *data, size_t len,
1051  uint32_t timeout)
1052 {
1053  return sock_dtls_send_aux(sock, remote, data, len, timeout, NULL);
1054 }
1055 
1096 static inline ssize_t sock_dtls_sendv(sock_dtls_t *sock,
1097  sock_dtls_session_t *remote,
1098  const iolist_t *snips,
1099  uint32_t timeout)
1100 {
1101  return sock_dtls_sendv_aux(sock, remote, snips, timeout, NULL);
1102 }
1103 
1117 
1118 #ifdef MODULE_SOCK_DTLS
1119 #include "sock_dtls_types.h"
1120 #endif
1121 
1122 #ifdef __cplusplus
1123 }
1124 #endif
1125 
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:95
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:914
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:1096
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:994
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:1048
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:820
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:645
sock_udp_aux_tx_t sock_dtls_aux_tx_t
Auxiliary data provided when sending using an DTLS sock object.
Definition: dtls.h:655
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:603
@ SOCK_DTLS_1_2
DTLS version 1.2.
Definition: dtls.h:604
@ SOCK_DTLS_1_3
DTLS version 1.3.
Definition: dtls.h:605
@ SOCK_DTLS_CLIENT
Endpoint client role.
Definition: dtls.h:615
@ SOCK_DTLS_SERVER
Endpoint server role.
Definition: dtls.h:616
UDP sock definitions.
Common sock API definitions.
tinydtls-specific types and functions definitions
Common IP-based transport layer end point.
Definition: sock.h:214
iolist structure definition
Definition: iolist.h:38
void * iol_base
ptr to this list entries data
Definition: iolist.h:40
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