Version 0.5.0 of tinydtls

Dependents:   tinydtls_test_cellular tinydtls_test_ethernet tiny-dtls

Committer:
ashleymills
Date:
Fri Oct 18 13:18:30 2013 +0000
Revision:
0:ff9ebe0cf0e9
Upgraded to tinydtls 0.5.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ashleymills 0:ff9ebe0cf0e9 1 /* dtls -- a very basic DTLS implementation
ashleymills 0:ff9ebe0cf0e9 2 *
ashleymills 0:ff9ebe0cf0e9 3 * Copyright (C) 2011--2013 Olaf Bergmann <bergmann@tzi.org>
ashleymills 0:ff9ebe0cf0e9 4 *
ashleymills 0:ff9ebe0cf0e9 5 * Permission is hereby granted, free of charge, to any person
ashleymills 0:ff9ebe0cf0e9 6 * obtaining a copy of this software and associated documentation
ashleymills 0:ff9ebe0cf0e9 7 * files (the "Software"), to deal in the Software without
ashleymills 0:ff9ebe0cf0e9 8 * restriction, including without limitation the rights to use, copy,
ashleymills 0:ff9ebe0cf0e9 9 * modify, merge, publish, distribute, sublicense, and/or sell copies
ashleymills 0:ff9ebe0cf0e9 10 * of the Software, and to permit persons to whom the Software is
ashleymills 0:ff9ebe0cf0e9 11 * furnished to do so, subject to the following conditions:
ashleymills 0:ff9ebe0cf0e9 12 *
ashleymills 0:ff9ebe0cf0e9 13 * The above copyright notice and this permission notice shall be
ashleymills 0:ff9ebe0cf0e9 14 * included in all copies or substantial portions of the Software.
ashleymills 0:ff9ebe0cf0e9 15 *
ashleymills 0:ff9ebe0cf0e9 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
ashleymills 0:ff9ebe0cf0e9 17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
ashleymills 0:ff9ebe0cf0e9 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ashleymills 0:ff9ebe0cf0e9 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
ashleymills 0:ff9ebe0cf0e9 20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ashleymills 0:ff9ebe0cf0e9 21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
ashleymills 0:ff9ebe0cf0e9 22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
ashleymills 0:ff9ebe0cf0e9 23 * SOFTWARE.
ashleymills 0:ff9ebe0cf0e9 24 */
ashleymills 0:ff9ebe0cf0e9 25
ashleymills 0:ff9ebe0cf0e9 26 /**
ashleymills 0:ff9ebe0cf0e9 27 * @file peer.h
ashleymills 0:ff9ebe0cf0e9 28 * @brief information about peers in a DTLS session
ashleymills 0:ff9ebe0cf0e9 29 */
ashleymills 0:ff9ebe0cf0e9 30
ashleymills 0:ff9ebe0cf0e9 31 #ifndef _PEER_H_
ashleymills 0:ff9ebe0cf0e9 32 #define _PEER_H_
ashleymills 0:ff9ebe0cf0e9 33
ashleymills 0:ff9ebe0cf0e9 34 #include "config.h"
ashleymills 0:ff9ebe0cf0e9 35 #include "global.h"
ashleymills 0:ff9ebe0cf0e9 36
ashleymills 0:ff9ebe0cf0e9 37 #include "state.h"
ashleymills 0:ff9ebe0cf0e9 38 #include "crypto.h"
ashleymills 0:ff9ebe0cf0e9 39
ashleymills 0:ff9ebe0cf0e9 40 #ifndef WITH_CONTIKI
ashleymills 0:ff9ebe0cf0e9 41 #include "uthash.h"
ashleymills 0:ff9ebe0cf0e9 42 #endif /* WITH_CONTIKI */
ashleymills 0:ff9ebe0cf0e9 43
ashleymills 0:ff9ebe0cf0e9 44 typedef enum { DTLS_CLIENT=0, DTLS_SERVER } dtls_peer_type;
ashleymills 0:ff9ebe0cf0e9 45
ashleymills 0:ff9ebe0cf0e9 46 /**
ashleymills 0:ff9ebe0cf0e9 47 * Holds security parameters, local state and the transport address
ashleymills 0:ff9ebe0cf0e9 48 * for each peer. */
ashleymills 0:ff9ebe0cf0e9 49 typedef struct dtls_peer_t {
ashleymills 0:ff9ebe0cf0e9 50 #ifndef WITH_CONTIKI
ashleymills 0:ff9ebe0cf0e9 51 UT_hash_handle hh;
ashleymills 0:ff9ebe0cf0e9 52 #else /* WITH_CONTIKI */
ashleymills 0:ff9ebe0cf0e9 53 struct dtls_peer_t *next;
ashleymills 0:ff9ebe0cf0e9 54 #endif /* WITH_CONTIKI */
ashleymills 0:ff9ebe0cf0e9 55
ashleymills 0:ff9ebe0cf0e9 56 session_t session; /**< peer address and local interface */
ashleymills 0:ff9ebe0cf0e9 57
ashleymills 0:ff9ebe0cf0e9 58 dtls_peer_type role; /**< denotes if this host is DTLS_CLIENT or DTLS_SERVER */
ashleymills 0:ff9ebe0cf0e9 59 dtls_state_t state; /**< DTLS engine state */
ashleymills 0:ff9ebe0cf0e9 60 uint16 epoch; /**< counter for cipher state changes*/
ashleymills 0:ff9ebe0cf0e9 61 uint48 rseq; /**< sequence number of last record sent */
ashleymills 0:ff9ebe0cf0e9 62
ashleymills 0:ff9ebe0cf0e9 63 dtls_hs_state_t hs_state; /**< handshake protocol status */
ashleymills 0:ff9ebe0cf0e9 64
ashleymills 0:ff9ebe0cf0e9 65 dtls_security_parameters_t security_params;
ashleymills 0:ff9ebe0cf0e9 66 dtls_handshake_parameters_t handshake_params;
ashleymills 0:ff9ebe0cf0e9 67 } dtls_peer_t;
ashleymills 0:ff9ebe0cf0e9 68
ashleymills 0:ff9ebe0cf0e9 69 void peer_init();
ashleymills 0:ff9ebe0cf0e9 70
ashleymills 0:ff9ebe0cf0e9 71 /**
ashleymills 0:ff9ebe0cf0e9 72 * Creates a new peer for given @p session. The current configuration
ashleymills 0:ff9ebe0cf0e9 73 * is initialized with the cipher suite TLS_NULL_WITH_NULL_NULL (i.e.
ashleymills 0:ff9ebe0cf0e9 74 * no security at all). This function returns a pointer to the new
ashleymills 0:ff9ebe0cf0e9 75 * peer or NULL on error. The caller is responsible for releasing the
ashleymills 0:ff9ebe0cf0e9 76 * storage allocated for this peer using dtls_free_peer().
ashleymills 0:ff9ebe0cf0e9 77 *
ashleymills 0:ff9ebe0cf0e9 78 * @param session The remote peer's address and local interface index.
ashleymills 0:ff9ebe0cf0e9 79 * @return A pointer to a newly created and initialized peer object
ashleymills 0:ff9ebe0cf0e9 80 * or NULL on error.
ashleymills 0:ff9ebe0cf0e9 81 */
ashleymills 0:ff9ebe0cf0e9 82 dtls_peer_t *dtls_new_peer(const session_t *session);
ashleymills 0:ff9ebe0cf0e9 83
ashleymills 0:ff9ebe0cf0e9 84 /** Releases the storage allocated to @p peer. */
ashleymills 0:ff9ebe0cf0e9 85 void dtls_free_peer(dtls_peer_t *peer);
ashleymills 0:ff9ebe0cf0e9 86
ashleymills 0:ff9ebe0cf0e9 87 /** Returns the current state of @p peer. */
ashleymills 0:ff9ebe0cf0e9 88 static inline dtls_state_t dtls_peer_state(const dtls_peer_t *peer) {
ashleymills 0:ff9ebe0cf0e9 89 return peer->state;
ashleymills 0:ff9ebe0cf0e9 90 }
ashleymills 0:ff9ebe0cf0e9 91
ashleymills 0:ff9ebe0cf0e9 92 /**
ashleymills 0:ff9ebe0cf0e9 93 * Checks if given @p peer is connected. This function returns
ashleymills 0:ff9ebe0cf0e9 94 * @c 1 if connected, or @c 0 otherwise.
ashleymills 0:ff9ebe0cf0e9 95 */
ashleymills 0:ff9ebe0cf0e9 96 static inline int dtls_peer_is_connected(const dtls_peer_t *peer) {
ashleymills 0:ff9ebe0cf0e9 97 return peer->state == DTLS_STATE_CONNECTED;
ashleymills 0:ff9ebe0cf0e9 98 }
ashleymills 0:ff9ebe0cf0e9 99
ashleymills 0:ff9ebe0cf0e9 100 #endif /* _PEER_H_ */