tinydtls  0.8.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
dtls.h
Go to the documentation of this file.
1 /* dtls -- a very basic DTLS implementation
2  *
3  * Copyright (C) 2011--2013 Olaf Bergmann <bergmann@tzi.org>
4  * Copyright (C) 2013 Hauke Mehrtens <hauke@hauke-m.de>
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use, copy,
10  * modify, merge, publish, distribute, sublicense, and/or sell copies
11  * of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26 
32 #ifndef _DTLS_DTLS_H_
33 #define _DTLS_DTLS_H_
34 
35 #include <stdint.h>
36 
37 #include "t_list.h"
38 #include "state.h"
39 #include "peer.h"
40 
41 #ifndef WITH_CONTIKI
42 #include "uthash.h"
43 #include "t_list.h"
44 #endif /* WITH_CONTIKI */
45 
46 #include "alert.h"
47 #include "crypto.h"
48 #include "hmac.h"
49 
50 #include "global.h"
51 #include "dtls_time.h"
52 
53 #ifndef DTLSv12
54 #define DTLS_VERSION 0xfeff /* DTLS v1.1 */
55 #else
56 #define DTLS_VERSION 0xfefd /* DTLS v1.2 */
57 #endif
58 
62 
63 typedef struct dtls_ecdsa_key_t {
65  const unsigned char *priv_key;
66  const unsigned char *pub_key_x;
67  const unsigned char *pub_key_y;
69 
71 #define DTLS_COOKIE_SECRET_LENGTH 12
72 
73 struct dtls_context_t;
74 
84 typedef struct {
99  int (*write)(struct dtls_context_t *ctx,
100  session_t *session, uint8 *buf, size_t len);
101 
114  int (*read)(struct dtls_context_t *ctx,
115  session_t *session, uint8 *buf, size_t len);
116 
129  int (*event)(struct dtls_context_t *ctx, session_t *session,
130  dtls_alert_level_t level, unsigned short code);
131 
132 #ifdef DTLS_PSK
133 
155  int (*get_psk_info)(struct dtls_context_t *ctx,
156  const session_t *session,
158  const unsigned char *desc, size_t desc_len,
159  unsigned char *result, size_t result_length);
160 
161 #endif /* DTLS_PSK */
162 
163 #ifdef DTLS_ECC
164 
184  int (*get_ecdsa_key)(struct dtls_context_t *ctx,
185  const session_t *session,
186  const dtls_ecdsa_key_t **result);
187 
214  int (*verify_ecdsa_key)(struct dtls_context_t *ctx,
215  const session_t *session,
216  const unsigned char *other_pub_x,
217  const unsigned char *other_pub_y,
218  size_t key_size);
219 #endif /* DTLS_ECC */
221 
223 typedef struct dtls_context_t {
227 #ifndef WITH_CONTIKI
229 #else /* WITH_CONTIKI */
231 
232  struct etimer retransmit_timer;
233 #endif /* WITH_CONTIKI */
234 
235  LIST_STRUCT(sendqueue);
237  void *app;
241  unsigned char readbuf[DTLS_MAX_BUF];
243 
248 void dtls_init();
249 
253 dtls_context_t *dtls_new_context(void *app_data);
254 
257 
258 #define dtls_set_app_data(CTX,DATA) ((CTX)->app = (DATA))
259 #define dtls_get_app_data(CTX) ((CTX)->app)
260 
262 static inline void dtls_set_handler(dtls_context_t *ctx, dtls_handler_t *h) {
263  ctx->h = h;
264 }
265 
276 int dtls_connect(dtls_context_t *ctx, const session_t *dst);
277 
289 
294 int dtls_close(dtls_context_t *ctx, const session_t *remote);
295 
296 int dtls_renegotiate(dtls_context_t *ctx, const session_t *dst);
297 
309 int dtls_write(struct dtls_context_t *ctx, session_t *session,
310  uint8 *buf, size_t len);
311 
322 
323 #define DTLS_COOKIE_LENGTH 16
324 
325 #define DTLS_CT_CHANGE_CIPHER_SPEC 20
326 #define DTLS_CT_ALERT 21
327 #define DTLS_CT_HANDSHAKE 22
328 #define DTLS_CT_APPLICATION_DATA 23
329 
331 typedef struct __attribute__((__packed__)) {
332  uint8 content_type;
333  uint16 version;
334  uint16 epoch;
335  uint48 sequence_number;
336  uint16 length;
337  /* fragment */
339 
340 /* Handshake types */
341 
342 #define DTLS_HT_HELLO_REQUEST 0
343 #define DTLS_HT_CLIENT_HELLO 1
344 #define DTLS_HT_SERVER_HELLO 2
345 #define DTLS_HT_HELLO_VERIFY_REQUEST 3
346 #define DTLS_HT_CERTIFICATE 11
347 #define DTLS_HT_SERVER_KEY_EXCHANGE 12
348 #define DTLS_HT_CERTIFICATE_REQUEST 13
349 #define DTLS_HT_SERVER_HELLO_DONE 14
350 #define DTLS_HT_CERTIFICATE_VERIFY 15
351 #define DTLS_HT_CLIENT_KEY_EXCHANGE 16
352 #define DTLS_HT_FINISHED 20
353 
355 typedef struct __attribute__((__packed__)) {
356  uint8 msg_type;
357  uint24 length;
358  uint16 message_seq;
359  uint24 fragment_offset;
360  uint24 fragment_length;
361  /* body */
363 
365 typedef struct __attribute__((__packed__)) {
366  uint16 version;
367  uint32 gmt_random;
368  unsigned char random[28];
369  /* session id (up to 32 bytes) */
370  /* cookie (up to 32 bytes) */
371  /* cipher suite (2 to 2^16 -1 bytes) */
372  /* compression method */
374 
376 typedef struct __attribute__((__packed__)) {
377  uint16 version;
378  uint8 cookie_length;
379  uint8 cookie[];
381 
382 #if 0
383 
391 int dtls_record_read(dtls_state_t *state, uint8 *msg, int msglen);
392 #endif
393 
403 int dtls_handle_message(dtls_context_t *ctx, session_t *session,
404  uint8 *msg, int msglen);
405 
416  const session_t *session);
417 
418 
419 #endif /* _DTLS_DTLS_H_ */
420 
dtls_ecdh_curve curve
Definition: dtls.h:64
unsigned char cookie_secret[DTLS_COOKIE_SECRET_LENGTH]
Definition: dtls.h:224
void dtls_free_context(dtls_context_t *ctx)
Definition: dtls.c:3802
const unsigned char * priv_key
Definition: dtls.h:65
Wrappers for list structures and functions.
dtls_context_t * dtls_new_context(void *app_data)
Definition: dtls.c:3740
LIST_STRUCT(sendqueue)
dtls_state_t
Definition: state.h:40
dtls_peer_t * dtls_get_peer(const dtls_context_t *context, const session_t *session)
Definition: dtls.c:212
dtls_record_header_t
Definition: dtls.h:338
dtls_alert_level_t
Definition: alert.h:34
clock_time_t cookie_secret_age
Definition: dtls.h:225
DTLS alert protocol.
dtls_credentials_type_t
Definition: dtls.h:59
dtls_peer_t * peers
Definition: dtls.h:228
uint32_t clock_time_t
Definition: dtls_time.h:55
unsigned char uint48[6]
Definition: global.h:51
static void dtls_set_handler(dtls_context_t *ctx, dtls_handler_t *h)
Definition: dtls.h:262
information about peers in a DTLS session
dtls_handler_t * h
Definition: dtls.h:239
struct __attribute__((__packed__))
Definition: dtls.h:331
unsigned char uint24[3]
Definition: global.h:49
struct dtls_ecdsa_key_t dtls_ecdsa_key_t
Clock Handling.
dtls_handshake_header_t
Definition: dtls.h:362
unsigned char readbuf[DTLS_MAX_BUF]
Definition: dtls.h:241
const unsigned char * pub_key_y
Definition: dtls.h:67
dtls_ecdh_curve
Definition: crypto.h:65
struct dtls_context_t dtls_context_t
unsigned char uint8
Definition: global.h:47
void dtls_init()
Definition: dtls.c:163
const unsigned char * pub_key_x
Definition: dtls.h:66
int dtls_connect(dtls_context_t *ctx, const session_t *dst)
Definition: dtls.c:3862
int dtls_handle_message(dtls_context_t *ctx, session_t *session, uint8 *msg, int msglen)
Definition: dtls.c:3591
void dtls_check_retransmit(dtls_context_t *context, clock_time_t *next)
Definition: dtls.c:3957
state information for DTLS FSM
#define DTLS_MAX_BUF
Definition: global.h:64
unsigned char uint32[4]
Definition: global.h:50
dtls_client_hello_t
Definition: dtls.h:373
void * app
Definition: dtls.h:237
int dtls_renegotiate(dtls_context_t *ctx, const session_t *dst)
Definition: dtls.c:2956
dtls_hello_verify_t
Definition: dtls.h:380
int dtls_close(dtls_context_t *ctx, const session_t *remote)
Definition: dtls.c:1494
#define DTLS_COOKIE_SECRET_LENGTH
Definition: dtls.h:71
unsigned char uint16[2]
Definition: global.h:48
int dtls_connect_peer(dtls_context_t *ctx, dtls_peer_t *peer)
Definition: dtls.c:3826
int dtls_write(struct dtls_context_t *ctx, session_t *session, uint8 *buf, size_t len)
Definition: dtls.c:236