mbed port of tinydtls
global.h@1:bc8a649bad13, 2013-10-11 (annotated)
- Committer:
- ashleymills
- Date:
- Fri Oct 11 08:46:21 2013 +0000
- Revision:
- 1:bc8a649bad13
- Parent:
- 0:04990d454f45
Cleaned up all the debug stuff I added finding the hash table bug.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ashleymills | 0:04990d454f45 | 1 | /* dtls -- a very basic DTLS implementation |
ashleymills | 0:04990d454f45 | 2 | * |
ashleymills | 0:04990d454f45 | 3 | * Copyright (C) 2011--2012 Olaf Bergmann <bergmann@tzi.org> |
ashleymills | 0:04990d454f45 | 4 | * |
ashleymills | 0:04990d454f45 | 5 | * Permission is hereby granted, free of charge, to any person |
ashleymills | 0:04990d454f45 | 6 | * obtaining a copy of this software and associated documentation |
ashleymills | 0:04990d454f45 | 7 | * files (the "Software"), to deal in the Software without |
ashleymills | 0:04990d454f45 | 8 | * restriction, including without limitation the rights to use, copy, |
ashleymills | 0:04990d454f45 | 9 | * modify, merge, publish, distribute, sublicense, and/or sell copies |
ashleymills | 0:04990d454f45 | 10 | * of the Software, and to permit persons to whom the Software is |
ashleymills | 0:04990d454f45 | 11 | * furnished to do so, subject to the following conditions: |
ashleymills | 0:04990d454f45 | 12 | * |
ashleymills | 0:04990d454f45 | 13 | * The above copyright notice and this permission notice shall be |
ashleymills | 0:04990d454f45 | 14 | * included in all copies or substantial portions of the Software. |
ashleymills | 0:04990d454f45 | 15 | * |
ashleymills | 0:04990d454f45 | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
ashleymills | 0:04990d454f45 | 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
ashleymills | 0:04990d454f45 | 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
ashleymills | 0:04990d454f45 | 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
ashleymills | 0:04990d454f45 | 20 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
ashleymills | 0:04990d454f45 | 21 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
ashleymills | 0:04990d454f45 | 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
ashleymills | 0:04990d454f45 | 23 | * SOFTWARE. |
ashleymills | 0:04990d454f45 | 24 | */ |
ashleymills | 0:04990d454f45 | 25 | |
ashleymills | 0:04990d454f45 | 26 | #ifndef _GLOBAL_H_ |
ashleymills | 0:04990d454f45 | 27 | #define _GLOBAL_H_ |
ashleymills | 0:04990d454f45 | 28 | |
ashleymills | 0:04990d454f45 | 29 | #include "config.h" |
ashleymills | 0:04990d454f45 | 30 | |
ashleymills | 0:04990d454f45 | 31 | #ifdef HAVE_ASSERT_H |
ashleymills | 0:04990d454f45 | 32 | #include <assert.h> |
ashleymills | 0:04990d454f45 | 33 | #else |
ashleymills | 0:04990d454f45 | 34 | #ifndef assert |
ashleymills | 0:04990d454f45 | 35 | #warning "assertions are disabled" |
ashleymills | 0:04990d454f45 | 36 | # define assert(x) |
ashleymills | 0:04990d454f45 | 37 | #endif |
ashleymills | 0:04990d454f45 | 38 | #endif |
ashleymills | 0:04990d454f45 | 39 | |
ashleymills | 0:04990d454f45 | 40 | #include <string.h> |
ashleymills | 0:04990d454f45 | 41 | #ifdef HAVE_SYS_SOCKET_H |
ashleymills | 0:04990d454f45 | 42 | #include <sys/socket.h> |
ashleymills | 0:04990d454f45 | 43 | #endif |
ashleymills | 0:04990d454f45 | 44 | #ifdef HAVE_SYS_TYPES_H |
ashleymills | 0:04990d454f45 | 45 | #include <sys/types.h> |
ashleymills | 0:04990d454f45 | 46 | #endif |
ashleymills | 0:04990d454f45 | 47 | #ifdef HAVE_NETINET_IN_H |
ashleymills | 0:04990d454f45 | 48 | #include <netinet/in.h> |
ashleymills | 0:04990d454f45 | 49 | #endif |
ashleymills | 0:04990d454f45 | 50 | #ifdef HAVE_ARPA_INET_H |
ashleymills | 0:04990d454f45 | 51 | #include <arpa/inet.h> |
ashleymills | 0:04990d454f45 | 52 | #endif |
ashleymills | 0:04990d454f45 | 53 | |
ashleymills | 0:04990d454f45 | 54 | #ifdef MBED |
ashleymills | 0:04990d454f45 | 55 | //#include "lwip/inet.h" |
ashleymills | 0:04990d454f45 | 56 | #include "lwip/sockets.h" |
ashleymills | 0:04990d454f45 | 57 | #include "lwip/netdb.h" |
ashleymills | 0:04990d454f45 | 58 | #endif |
ashleymills | 0:04990d454f45 | 59 | |
ashleymills | 0:04990d454f45 | 60 | #ifndef DTLSv12 |
ashleymills | 0:04990d454f45 | 61 | /* The current version of tinyDTLS supports DTLSv1.2 only. */ |
ashleymills | 0:04990d454f45 | 62 | #define DTLSv12 1 |
ashleymills | 0:04990d454f45 | 63 | #endif |
ashleymills | 0:04990d454f45 | 64 | |
ashleymills | 0:04990d454f45 | 65 | #ifndef WITH_SHA256 |
ashleymills | 0:04990d454f45 | 66 | /* The current version of tinyDTLS supports DTLSv1.2 with SHA256 PRF |
ashleymills | 0:04990d454f45 | 67 | only. */ |
ashleymills | 0:04990d454f45 | 68 | #define WITH_SHA256 1 |
ashleymills | 0:04990d454f45 | 69 | #endif |
ashleymills | 0:04990d454f45 | 70 | |
ashleymills | 0:04990d454f45 | 71 | #ifndef WITH_CONTIKI |
ashleymills | 0:04990d454f45 | 72 | typedef unsigned int clock_time_t; |
ashleymills | 0:04990d454f45 | 73 | #else /* WITH_CONTIKI */ |
ashleymills | 0:04990d454f45 | 74 | #include "uip.h" |
ashleymills | 0:04990d454f45 | 75 | typedef struct { |
ashleymills | 0:04990d454f45 | 76 | unsigned char size; |
ashleymills | 0:04990d454f45 | 77 | uip_ipaddr_t addr; |
ashleymills | 0:04990d454f45 | 78 | unsigned short port; |
ashleymills | 0:04990d454f45 | 79 | int ifindex; |
ashleymills | 0:04990d454f45 | 80 | } __uip_session_t; |
ashleymills | 0:04990d454f45 | 81 | #define session_t __uip_session_t |
ashleymills | 0:04990d454f45 | 82 | |
ashleymills | 0:04990d454f45 | 83 | #define _dtls_address_equals_impl(A,B) \ |
ashleymills | 0:04990d454f45 | 84 | ((A)->size == (B)->size \ |
ashleymills | 0:04990d454f45 | 85 | && (A)->port == (B)->port \ |
ashleymills | 0:04990d454f45 | 86 | && uip_ipaddr_cmp(&((A)->addr),&((B)->addr)) \ |
ashleymills | 0:04990d454f45 | 87 | && (A)->ifindex == (B)->ifindex) |
ashleymills | 0:04990d454f45 | 88 | |
ashleymills | 0:04990d454f45 | 89 | #endif /* WITH_CONTIKI */ |
ashleymills | 0:04990d454f45 | 90 | |
ashleymills | 0:04990d454f45 | 91 | /** multi-purpose address abstraction */ |
ashleymills | 0:04990d454f45 | 92 | #ifndef session_t |
ashleymills | 0:04990d454f45 | 93 | typedef struct __session_t { |
ashleymills | 0:04990d454f45 | 94 | socklen_t size; /**< size of addr */ |
ashleymills | 0:04990d454f45 | 95 | union { |
ashleymills | 0:04990d454f45 | 96 | struct sockaddr sa; |
ashleymills | 0:04990d454f45 | 97 | //struct sockaddr_storage st; |
ashleymills | 0:04990d454f45 | 98 | struct sockaddr_in sin; |
ashleymills | 0:04990d454f45 | 99 | //struct sockaddr_in6 sin6; |
ashleymills | 0:04990d454f45 | 100 | } addr; |
ashleymills | 0:04990d454f45 | 101 | int ifindex; |
ashleymills | 0:04990d454f45 | 102 | } __session_t; |
ashleymills | 0:04990d454f45 | 103 | |
ashleymills | 0:04990d454f45 | 104 | #define session_t __session_t |
ashleymills | 0:04990d454f45 | 105 | |
ashleymills | 0:04990d454f45 | 106 | |
ashleymills | 0:04990d454f45 | 107 | |
ashleymills | 0:04990d454f45 | 108 | static inline int |
ashleymills | 0:04990d454f45 | 109 | _dtls_address_equals_impl(const session_t *a, |
ashleymills | 0:04990d454f45 | 110 | const session_t *b) { |
ashleymills | 0:04990d454f45 | 111 | if (a->ifindex != b->ifindex || |
ashleymills | 0:04990d454f45 | 112 | a->size != b->size || a->addr.sa.sa_family != b->addr.sa.sa_family) |
ashleymills | 0:04990d454f45 | 113 | return 0; |
ashleymills | 0:04990d454f45 | 114 | |
ashleymills | 0:04990d454f45 | 115 | /* need to compare only relevant parts of sockaddr_in6 */ |
ashleymills | 0:04990d454f45 | 116 | switch (a->addr.sa.sa_family) { |
ashleymills | 0:04990d454f45 | 117 | case AF_INET: |
ashleymills | 0:04990d454f45 | 118 | return |
ashleymills | 0:04990d454f45 | 119 | a->addr.sin.sin_port == b->addr.sin.sin_port && |
ashleymills | 0:04990d454f45 | 120 | memcmp(&a->addr.sin.sin_addr, &b->addr.sin.sin_addr, |
ashleymills | 0:04990d454f45 | 121 | sizeof(struct in_addr)) == 0; |
ashleymills | 0:04990d454f45 | 122 | /* |
ashleymills | 0:04990d454f45 | 123 | case AF_INET6: |
ashleymills | 0:04990d454f45 | 124 | return a->addr.sin6.sin6_port == b->addr.sin6.sin6_port && |
ashleymills | 0:04990d454f45 | 125 | memcmp(&a->addr.sin6.sin6_addr, &b->addr.sin6.sin6_addr, |
ashleymills | 0:04990d454f45 | 126 | sizeof(struct in6_addr)) == 0; |
ashleymills | 0:04990d454f45 | 127 | */ |
ashleymills | 0:04990d454f45 | 128 | default: /* fall through and signal error */ |
ashleymills | 0:04990d454f45 | 129 | ; |
ashleymills | 0:04990d454f45 | 130 | } |
ashleymills | 0:04990d454f45 | 131 | return 0; |
ashleymills | 0:04990d454f45 | 132 | } |
ashleymills | 0:04990d454f45 | 133 | #endif /* session_t */ |
ashleymills | 0:04990d454f45 | 134 | |
ashleymills | 0:04990d454f45 | 135 | /* Define our own types as at least uint32_t does not work on my amd64. */ |
ashleymills | 0:04990d454f45 | 136 | |
ashleymills | 0:04990d454f45 | 137 | typedef unsigned char uint8; |
ashleymills | 0:04990d454f45 | 138 | typedef unsigned char uint16[2]; |
ashleymills | 0:04990d454f45 | 139 | typedef unsigned char uint24[3]; |
ashleymills | 0:04990d454f45 | 140 | typedef unsigned char uint32[4]; |
ashleymills | 0:04990d454f45 | 141 | typedef unsigned char uint48[6]; |
ashleymills | 0:04990d454f45 | 142 | |
ashleymills | 0:04990d454f45 | 143 | #ifndef HAVE_STR |
ashleymills | 0:04990d454f45 | 144 | typedef struct { |
ashleymills | 0:04990d454f45 | 145 | size_t length; /* length of string */ |
ashleymills | 0:04990d454f45 | 146 | unsigned char *s; /* string data */ |
ashleymills | 0:04990d454f45 | 147 | } str; |
ashleymills | 0:04990d454f45 | 148 | #endif |
ashleymills | 0:04990d454f45 | 149 | |
ashleymills | 0:04990d454f45 | 150 | #ifndef DTLS_MAX_BUF |
ashleymills | 0:04990d454f45 | 151 | /** Maximum size of DTLS message. */ |
ashleymills | 0:04990d454f45 | 152 | #define DTLS_MAX_BUF 256 |
ashleymills | 0:04990d454f45 | 153 | #endif |
ashleymills | 0:04990d454f45 | 154 | |
ashleymills | 0:04990d454f45 | 155 | #ifndef DTLS_DEFAULT_MAX_RETRANSMIT |
ashleymills | 0:04990d454f45 | 156 | /** Number of message retransmissions. */ |
ashleymills | 0:04990d454f45 | 157 | #define DTLS_DEFAULT_MAX_RETRANSMIT 5 |
ashleymills | 0:04990d454f45 | 158 | #endif |
ashleymills | 0:04990d454f45 | 159 | |
ashleymills | 0:04990d454f45 | 160 | /** Known cipher suites.*/ |
ashleymills | 0:04990d454f45 | 161 | typedef enum { |
ashleymills | 0:04990d454f45 | 162 | TLS_NULL_WITH_NULL_NULL = 0x0000, /**< NULL cipher */ |
ashleymills | 0:04990d454f45 | 163 | TLS_PSK_WITH_AES_128_CCM_8 = 0xC0A8 /**< see RFC 6655 */ |
ashleymills | 0:04990d454f45 | 164 | } dtls_cipher_t; |
ashleymills | 0:04990d454f45 | 165 | |
ashleymills | 0:04990d454f45 | 166 | /** |
ashleymills | 0:04990d454f45 | 167 | * XORs \p n bytes byte-by-byte starting at \p y to the memory area |
ashleymills | 0:04990d454f45 | 168 | * starting at \p x. */ |
ashleymills | 0:04990d454f45 | 169 | static inline void |
ashleymills | 0:04990d454f45 | 170 | memxor(unsigned char *x, const unsigned char *y, size_t n) { |
ashleymills | 0:04990d454f45 | 171 | while(n--) { |
ashleymills | 0:04990d454f45 | 172 | *x ^= *y; |
ashleymills | 0:04990d454f45 | 173 | x++; y++; |
ashleymills | 0:04990d454f45 | 174 | } |
ashleymills | 0:04990d454f45 | 175 | } |
ashleymills | 0:04990d454f45 | 176 | |
ashleymills | 0:04990d454f45 | 177 | #ifdef HAVE_FLS |
ashleymills | 0:04990d454f45 | 178 | #define dtls_fls(i) fls(i) |
ashleymills | 0:04990d454f45 | 179 | #else |
ashleymills | 0:04990d454f45 | 180 | static inline int |
ashleymills | 0:04990d454f45 | 181 | dtls_fls(unsigned int i) { |
ashleymills | 0:04990d454f45 | 182 | int n; |
ashleymills | 0:04990d454f45 | 183 | for (n = 0; i; n++) |
ashleymills | 0:04990d454f45 | 184 | i >>= 1; |
ashleymills | 0:04990d454f45 | 185 | return n; |
ashleymills | 0:04990d454f45 | 186 | } |
ashleymills | 0:04990d454f45 | 187 | #endif /* HAVE_FLS */ |
ashleymills | 0:04990d454f45 | 188 | |
ashleymills | 0:04990d454f45 | 189 | /** |
ashleymills | 0:04990d454f45 | 190 | * Resets the given session_t object @p sess to its default |
ashleymills | 0:04990d454f45 | 191 | * values. In particular, the member rlen must be initialized to the |
ashleymills | 0:04990d454f45 | 192 | * available size for storing addresses. |
ashleymills | 0:04990d454f45 | 193 | * |
ashleymills | 0:04990d454f45 | 194 | * @param sess The session_t object to initialize. |
ashleymills | 0:04990d454f45 | 195 | */ |
ashleymills | 0:04990d454f45 | 196 | static inline void |
ashleymills | 0:04990d454f45 | 197 | dtls_session_init(session_t *sess) { |
ashleymills | 0:04990d454f45 | 198 | assert(sess); |
ashleymills | 0:04990d454f45 | 199 | memset(sess, 0, sizeof(session_t)); |
ashleymills | 0:04990d454f45 | 200 | sess->size = sizeof(sess->addr); |
ashleymills | 0:04990d454f45 | 201 | } |
ashleymills | 0:04990d454f45 | 202 | |
ashleymills | 0:04990d454f45 | 203 | static inline int |
ashleymills | 0:04990d454f45 | 204 | dtls_session_equals(const session_t *a, const session_t *b) { |
ashleymills | 0:04990d454f45 | 205 | assert(a); assert(b); |
ashleymills | 0:04990d454f45 | 206 | return _dtls_address_equals_impl(a, b); |
ashleymills | 0:04990d454f45 | 207 | } |
ashleymills | 0:04990d454f45 | 208 | #endif /* _GLOBAL_H_ */ |