Contiki-NG
Loading...
Searching...
No Matches
mbedtls-support.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022, RISE Research Institutes of Sweden AB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * 3. Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/**
35 * \file
36 * DTLS (Mbed TLS implementation) support for CoAP
37 * \author
38 * Jayendra Ellamathy <ejayen@gmail.com>
39 */
40
41#ifndef MBEDTLS_SUPPORT_H_
42#define MBEDTLS_SUPPORT_H_
43
44#include MBEDTLS_CONFIG_FILE
45#include "mbedtls/ssl.h"
46#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
47#include "mbedtls/entropy.h"
48#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
49#include "mbedtls/ctr_drbg.h"
50
51/*TODO: agree on config structure! */
52#ifdef MBEDTLS_TIMING_ALT
53#include "timing_alt.h"
54#else
55#include "mbedtls/timing.h"
56#endif
57
58#ifdef COAP_DTLS_CONF_WITH_CERT
59#include "mbedtls/x509.h"
60#endif /* COAP_DTLS_CONF_WITH_CERT */
61#ifdef COAP_DTLS_CONF_WITH_SERVER
62#include "mbedtls/ssl_cookie.h"
63#if defined(MBEDTLS_SSL_CACHE_C)
64#include "mbedtls/ssl_cache.h"
65#endif /* MBEDTLS_SSL_CACHE_C */
66#endif /* COAP_DTLS_CONF_WITH_SERVER */
67
68#include "dtls-support-config.h"
69
70#include "coap-endpoint.h"
71#include "coap-keystore.h"
72
73typedef enum coap_dtls_sec_mode_e {
74 COAP_DTLS_SEC_MODE_NONE = 0,
75 COAP_DTLS_SEC_MODE_PSK = 1,
76 COAP_DTLS_SEC_MODE_CERT = 2,
77} coap_dtls_sec_mode_t;
78
79typedef enum coap_mbedtls_role_e {
80 COAP_MBEDTLS_ROLE_NONE = 0,
81 COAP_MBEDTLS_ROLE_CLIENT = 1,
82 COAP_MBEDTLS_ROLE_SERVER = 2,
83} coap_mbedtls_role_t;
84
85typedef enum coap_mbedtls_event_e {
86 COAP_MBEDTLS_EVENT_NONE = 0,
87 COAP_MBEDTLS_EVENT_RETRANSMISSION_EVENT = 1,
88 COAP_MBEDTLS_EVENT_SEND_MESSAGE_EVENT = 2,
89} coap_mbedtls_event_t;
90
91/* DTLS session info -- config, current state, etc */
92typedef struct coap_dtls_session_info {
93 struct coap_dtls_session_info *next;
94 enum coap_mbedtls_role_e role;
95 coap_endpoint_t ep; /* Server/Client address when role
96 is Client/Server respectively */
97 bool is_packet_consumed; /* To prevent Mbed TLS from reading
98 the same packet more than once. */
99 mbedtls_ssl_context ssl;
100 mbedtls_ssl_config conf;
101 uint32_t ciphersuite;
102#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
103 mbedtls_entropy_context entropy;
104#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
105 mbedtls_ctr_drbg_context ctr_drbg;
106 struct mbedtls_timing_delay_context timer;
107 struct etimer retransmission_et; /* Event timer to call the handshake function
108 for re-transmssion */
109#ifdef COAP_DTLS_CONF_WITH_CERT
110 char *hostname; /* Used for SNI (Server Name Indication) */
111 mbedtls_x509_crt ca_cert; /* Root CA certificate */
112 mbedtls_x509_crt own_cert; /* Our (Client/Server) certificate */
113 mbedtls_pk_context pkey; /* Our (Client/Server) private key */
114#endif /* COAP_DTLS_CONF_WITH_CERT */
115#ifdef COAP_DTLS_CONF_WITH_SERVER
116 bool in_use; /* Determines if this server session is in use by a client. */
117 mbedtls_ssl_cookie_ctx cookie_ctx;
118#if defined(MBEDTLS_SSL_CACHE_C)
119 mbedtls_ssl_cache_context cache;
120#endif /* MBEDTLS_SSL_CACHE_C */
121#endif /* COAP_DTLS_CONF_WITH_SERVER */
122} coap_dtls_session_info_t;
123
124/* DTLS message info */
125typedef struct coap_dtls_send_message {
126 struct coap_mbedtls_send_message *next;
127 coap_endpoint_t ep;
128 unsigned char send_buf[COAP_MBEDTLS_MTU];
129 size_t len;
130} coap_dtls_send_message_t;
131
132/* Struct stores global DTLS info */
133typedef struct coap_dtls_context {
134 struct etimer fragmentation_et;
135 LIST_STRUCT(sessions); /* List of DTLS sessions */
136 LIST_STRUCT(send_message_fifo); /* DTLS message to send queue */
137 struct uip_udp_conn *udp_conn; /* DTLS will listen on this udp port */
138 struct process *host_process; /* Process which will take care of sending
139 DTLS messages -- CoAP UIP process */
140 bool ready; /* Determines whether DTLS is initialized and ready. */
141} coap_dtls_context_t;
142
143/**
144 * \brief Initializes CoAP-MbedTLS global info. Must be the first thing that is
145 * called before using CoAP-MbedTLS.
146 */
147void coap_dtls_init(void);
148
149/**
150 * \brief Handler for timer, and process-poll events.
151 * Must be called by the host process (CoAP Engine).
152 */
153void coap_dtls_event_handler(void);
154
155/**
156 * \brief Registers, 1. UDP port info. 2. Host process (Coap Engine).
157 *
158 * \param udp_conn Pointer to UDP port information. This will be used when
159 * CoAP-MbedTLS needs to send messages via UDP.
160 *
161 * \param host_process Pointer to the host process. This process will recieve
162 * a poll event when a DTLS message needs to be sent.
163 */
164void coap_dtls_conn_init(struct uip_udp_conn *udp_conn,
165 struct process *host_process);
166
167/**
168 * \brief Encrypt app. data and send via UDP.
169 *
170 * \param ep Pointer to destination CoAP endpoint.
171 * \param message Pointer to the buffer holding app. data.
172 * \param len Length of message to be sent.
173 *
174 * \return SUCCESS: Number of bytes written.
175 * FAILURE: -1
176 */
177int coap_ep_dtls_write(const coap_endpoint_t *ep,
178 const unsigned char *message, int len);
179
180/**
181 * \brief Handler for new DTLS messages. Handles both handshake and decryption
182 * of record layer messages.
183 *
184 * \param ep Pointer of source CoAP endpoint.
185 *
186 * \return SUCCESS: 0 for Handshake; Number of bytes read for record layer packet.
187 * FAILURE: -1
188 */
189int coap_ep_dtls_handle_message(const coap_endpoint_t *ep);
190
191/**
192 * \brief Disconnect a peer. Sends a close notification message to peer.
193 * Followed by cleanup of session struct, free memory.
194 *
195 * \param ep Pointer of peer CoAP endpoint.
196 */
197void coap_ep_dtls_disconnect(const coap_endpoint_t *ep);
198
199/**
200 * \brief Get session struct associated with CoAP endpoint.
201 *
202 * \param ep Pointer of peer CoAP endpoint.
203 */
204coap_dtls_session_info_t *
205coap_ep_get_dtls_session_info(const coap_endpoint_t *ep);
206
207#ifdef COAP_DTLS_CONF_WITH_CLIENT
208/**
209 * \brief Connect to a DTLS server. To be used by the client.
210 * Sets up a client session and initiates the handshake.
211 *
212 * \param ep Pointer of peer CoAP endpoint.
213 * \param sec_mode Enum representing mode of security: certificates or PSK.
214 * \param keystore_entry Pointer to PSK or certificate info struct. Will be
215 * type-casted internall based on sec_mode enum.
216 *
217 * \return SUCCESS: 1
218 * FAILURE: -1
219 */
220int coap_ep_dtls_connect(const coap_endpoint_t *ep,
221 coap_dtls_sec_mode_t sec_mode, const void *keystore_entry);
222#endif /* COAP_DTLS_CONF_WITH_CLIENT */
223
224#ifdef COAP_DTLS_CONF_WITH_SERVER
225/**
226 * \brief Setup a DTLS server session. Must be done before any client connection
227 * can be accepted.
228 *
229 * \param ep Pointer of peer CoAP endpoint.
230 * \param sec_mode Enum representing mode of security: certificates or PSK.
231 * \param keystore_entry Pointer to PSK or certificate info struct. Will be
232 * type-casted internall based on sec_mode enum.
233 *
234 * \return SUCCESS: 1
235 * FAILURE: -1
236 */
237int coap_dtls_server_setup(const coap_dtls_sec_mode_t sec_mode,
238 const void *keystore_entry);
239#endif /* COAP_DTLS_CONF_WITH_SERVER */
240
241/**
242 * \brief Check if a CoAP endpoint is a peer in the list of DTLS sessions
243 *
244 * \param ep Pointer of peer CoAP endpoint.
245 *
246 * \return SUCCESS: true
247 * FAILURE: false
248 */
249bool coap_ep_is_dtls_peer(const coap_endpoint_t *ep);
250
251/**
252 * \brief Check if a peer has completed the handshake successfully
253 *
254 * \param ep Pointer of peer CoAP endpoint.
255 *
256 * \return SUCCESS: true
257 * FAILURE: false
258 */
259bool coap_ep_is_dtls_connected(const coap_endpoint_t *ep);
260
261/**
262 * \brief Check in what DTLS state the peer is in.
263 *
264 * \param ep Pointer of peer CoAP endpoint.
265 * \warning Uses deprecated mbedtls getters
266 *
267 * \return enum mbedtls_ssl_states
268 */
269int coap_ep_get_dtls_state(const coap_endpoint_t *ep);
270
271#endif /* MBEDTLS_SUPPORT_H_ */
API to address CoAP endpoints.
API for CoAP keystore.
#define LIST_STRUCT(name)
Declare a linked list inside a structure declaraction.
Definition list.h:112
void coap_dtls_conn_init(struct uip_udp_conn *udp_conn, struct process *host_process)
Registers, 1.
int coap_ep_get_dtls_state(const coap_endpoint_t *ep)
Check in what DTLS state the peer is in.
int coap_ep_dtls_write(const coap_endpoint_t *ep, const unsigned char *message, int len)
Encrypt app.
bool coap_ep_is_dtls_peer(const coap_endpoint_t *ep)
Check if a CoAP endpoint is a peer in the list of DTLS sessions.
bool coap_ep_is_dtls_connected(const coap_endpoint_t *ep)
Check if a peer has completed the handshake successfully.
void coap_dtls_init(void)
Initializes CoAP-MbedTLS global info.
void coap_ep_dtls_disconnect(const coap_endpoint_t *ep)
Disconnect a peer.
void coap_dtls_event_handler(void)
Handler for timer, and process-poll events.
int coap_ep_dtls_handle_message(const coap_endpoint_t *ep)
Handler for new DTLS messages.
coap_dtls_session_info_t * coap_ep_get_dtls_session_info(const coap_endpoint_t *ep)
Get session struct associated with CoAP endpoint.
A timer.
Definition etimer.h:79
A timer.
Definition timer.h:84
Representation of a uIP UDP connection.
Definition uip.h:1309