tinydtls  0.8.6
dtls.h
Go to the documentation of this file.
1 /*******************************************************************************
2  *
3  * Copyright (c) 2011, 2012, 2013, 2014, 2015 Olaf Bergmann (TZI) and others.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
7  *
8  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
9  * and the Eclipse Distribution License is available at
10  * http://www.eclipse.org/org/documents/edl-v10.php.
11  *
12  * Contributors:
13  * Olaf Bergmann - initial API and implementation
14  * Hauke Mehrtens - memory optimization, ECC integration
15  * Achim Kraus - session recovery
16  *
17  *******************************************************************************/
18 
24 #ifndef _DTLS_DTLS_H_
25 #define _DTLS_DTLS_H_
26 
27 #include <stdint.h>
28 
29 #include "tinydtls.h"
30 
31 #include "state.h"
32 #include "peer.h"
33 
34 #include "uthash.h"
35 
36 #include "alert.h"
37 #include "crypto.h"
38 #include "hmac.h"
39 
40 #include "global.h"
41 #include "dtls_time.h"
42 
43 #ifndef DTLSv12
44 #define DTLS_VERSION 0xfeff /* DTLS v1.1 */
45 #else
46 #define DTLS_VERSION 0xfefd /* DTLS v1.2 */
47 #endif
48 
52 
53 typedef struct dtls_ecdsa_key_t {
55  const unsigned char *priv_key;
56  const unsigned char *pub_key_x;
57  const unsigned char *pub_key_y;
59 
61 #define DTLS_COOKIE_SECRET_LENGTH 12
62 
63 struct dtls_context_t;
64 
74 typedef struct {
89  int (*write)(struct dtls_context_t *ctx,
90  session_t *session, uint8 *buf, size_t len);
91 
104  int (*read)(struct dtls_context_t *ctx,
105  session_t *session, uint8 *buf, size_t len);
106 
119  int (*event)(struct dtls_context_t *ctx, session_t *session,
120  dtls_alert_level_t level, unsigned short code);
121 
122 #ifdef DTLS_PSK
123 
145  int (*get_psk_info)(struct dtls_context_t *ctx,
146  const session_t *session,
148  const unsigned char *desc, size_t desc_len,
149  unsigned char *result, size_t result_length);
150 
151 #endif /* DTLS_PSK */
152 
153 #ifdef DTLS_ECC
154 
174  int (*get_ecdsa_key)(struct dtls_context_t *ctx,
175  const session_t *session,
176  const dtls_ecdsa_key_t **result);
177 
204  int (*verify_ecdsa_key)(struct dtls_context_t *ctx,
205  const session_t *session,
206  const unsigned char *other_pub_x,
207  const unsigned char *other_pub_y,
208  size_t key_size);
209 #endif /* DTLS_ECC */
211 
212 struct netq_t;
213 
215 typedef struct dtls_context_t {
216  unsigned char cookie_secret[DTLS_COOKIE_SECRET_LENGTH];
220 #ifdef WITH_CONTIKI
221  struct etimer retransmit_timer;
222 #endif /* WITH_CONTIKI */
223 
224  struct netq_t *sendqueue;
226  void *app;
230  unsigned char readbuf[DTLS_MAX_BUF];
232 
237 void dtls_init(void);
238 
242 dtls_context_t *dtls_new_context(void *app_data);
243 
246 
247 #define dtls_set_app_data(CTX,DATA) ((CTX)->app = (DATA))
248 #define dtls_get_app_data(CTX) ((CTX)->app)
249 
251 static inline void dtls_set_handler(dtls_context_t *ctx, dtls_handler_t *h) {
252  ctx->h = h;
253 }
254 
265 int dtls_connect(dtls_context_t *ctx, const session_t *dst);
266 
278 
283 int dtls_close(dtls_context_t *ctx, const session_t *remote);
284 
285 int dtls_renegotiate(dtls_context_t *ctx, const session_t *dst);
286 
298 int dtls_write(struct dtls_context_t *ctx, session_t *session,
299  uint8 *buf, size_t len);
300 
311 
312 #define DTLS_COOKIE_LENGTH 16
313 
314 #define DTLS_CT_CHANGE_CIPHER_SPEC 20
315 #define DTLS_CT_ALERT 21
316 #define DTLS_CT_HANDSHAKE 22
317 #define DTLS_CT_APPLICATION_DATA 23
318 
320 typedef struct __attribute__((__packed__)) {
321  uint8 content_type;
322  uint16 version;
323  uint16 epoch;
324  uint48 sequence_number;
325  uint16 length;
326  /* fragment */
328 
329 /* Handshake types */
330 
331 #define DTLS_HT_HELLO_REQUEST 0
332 #define DTLS_HT_CLIENT_HELLO 1
333 #define DTLS_HT_SERVER_HELLO 2
334 #define DTLS_HT_HELLO_VERIFY_REQUEST 3
335 #define DTLS_HT_CERTIFICATE 11
336 #define DTLS_HT_SERVER_KEY_EXCHANGE 12
337 #define DTLS_HT_CERTIFICATE_REQUEST 13
338 #define DTLS_HT_SERVER_HELLO_DONE 14
339 #define DTLS_HT_CERTIFICATE_VERIFY 15
340 #define DTLS_HT_CLIENT_KEY_EXCHANGE 16
341 #define DTLS_HT_FINISHED 20
342 
344 typedef struct __attribute__((__packed__)) {
345  uint8 msg_type;
346  uint24 length;
347  uint16 message_seq;
348  uint24 fragment_offset;
349  uint24 fragment_length;
350  /* body */
352 
354 typedef struct __attribute__((__packed__)) {
355  uint16 version;
356  uint32 gmt_random;
357  unsigned char random[28];
358  /* session id (up to 32 bytes) */
359  /* cookie (up to 32 bytes) */
360  /* cipher suite (2 to 2^16 -1 bytes) */
361  /* compression method */
363 
365 typedef struct __attribute__((__packed__)) {
366  uint16 version;
367  uint8 cookie_length;
368  uint8 cookie[];
370 
371 #if 0
372 
380 int dtls_record_read(dtls_state_t *state, uint8 *msg, int msglen);
381 #endif
382 
392 int dtls_handle_message(dtls_context_t *ctx, session_t *session,
393  uint8 *msg, int msglen);
394 
405  const session_t *session);
406 
413 void dtls_reset_peer(dtls_context_t *context, dtls_peer_t *peer);
414 
415 #endif /* _DTLS_DTLS_H_ */
416 
dtls_ecdh_curve curve
Definition: dtls.h:54
struct netq_t * sendqueue
Definition: dtls.h:224
void dtls_reset_peer(dtls_context_t *context, dtls_peer_t *peer)
Definition: dtls.c:3965
public tinydtls API
void dtls_free_context(dtls_context_t *ctx)
Definition: dtls.c:3972
const unsigned char * priv_key
Definition: dtls.h:55
dtls_context_t * dtls_new_context(void *app_data)
Definition: dtls.c:3909
void dtls_init(void)
Definition: dtls.c:193
struct netq_t * next
Definition: netq.h:48
dtls_state_t
Definition: state.h:32
dtls_peer_t * dtls_get_peer(const dtls_context_t *context, const session_t *session)
Definition: dtls.c:243
Definition: netq.h:47
dtls_record_header_t
Definition: dtls.h:327
dtls_alert_level_t
Definition: alert.h:26
clock_time_t cookie_secret_age
Definition: dtls.h:217
DTLS alert protocol.
dtls_credentials_type_t
Definition: dtls.h:49
dtls_peer_t * peers
Definition: dtls.h:219
uint32_t clock_time_t
Definition: dtls_time.h:46
unsigned char uint48[6]
Definition: global.h:43
static void dtls_set_handler(dtls_context_t *ctx, dtls_handler_t *h)
Definition: dtls.h:251
information about peers in a DTLS session
dtls_handler_t * h
Definition: dtls.h:228
struct __attribute__((__packed__))
Definition: dtls.h:320
unsigned char uint24[3]
Definition: global.h:41
struct dtls_ecdsa_key_t dtls_ecdsa_key_t
Clock Handling.
dtls_handshake_header_t
Definition: dtls.h:351
const unsigned char * pub_key_y
Definition: dtls.h:57
dtls_peer_t * peer
Definition: netq.h:53
dtls_ecdh_curve
Definition: crypto.h:55
struct dtls_context_t dtls_context_t
unsigned char uint8
Definition: global.h:39
const unsigned char * pub_key_x
Definition: dtls.h:56
int dtls_connect(dtls_context_t *ctx, const session_t *dst)
Definition: dtls.c:4031
int dtls_handle_message(dtls_context_t *ctx, session_t *session, uint8 *msg, int msglen)
Definition: dtls.c:3703
void dtls_check_retransmit(dtls_context_t *context, clock_time_t *next)
Definition: dtls.c:4126
state information for DTLS FSM
#define DTLS_MAX_BUF
Definition: global.h:56
unsigned char uint32[4]
Definition: global.h:42
dtls_client_hello_t
Definition: dtls.h:362
void * app
Definition: dtls.h:226
int dtls_renegotiate(dtls_context_t *ctx, const session_t *dst)
Definition: dtls.c:3053
dtls_hello_verify_t
Definition: dtls.h:369
int dtls_close(dtls_context_t *ctx, const session_t *remote)
Definition: dtls.c:1576
#define DTLS_COOKIE_SECRET_LENGTH
Definition: dtls.h:61
unsigned char uint16[2]
Definition: global.h:40
int dtls_connect_peer(dtls_context_t *ctx, dtls_peer_t *peer)
Definition: dtls.c:3993
int dtls_write(struct dtls_context_t *ctx, session_t *session, uint8 *buf, size_t len)
Definition: dtls.c:261