12 #ifdef HAVE_LIBTINYDTLS 15 #undef PACKAGE_BUGREPORT 18 #undef PACKAGE_TARNAME 20 #undef PACKAGE_VERSION 24 #include <dtls_debug.h> 26 static dtls_tick_t dtls_tick_0 = 0;
36 dtls_ticks(&dtls_tick_0);
42 dtls_set_log_level(level);
47 return dtls_get_log_level();
50 static void get_session_addr(
const session_t *s,
coap_address_t *a) {
55 if (s->addr.sa.sa_family == AF_INET6) {
58 }
else if (s->addr.sa.sa_family == AF_INET) {
62 a->
size = (socklen_t)s->size;
68 static void put_session_addr(
const coap_address_t *a, session_t *s) {
70 s->size = (
unsigned char)
sizeof(s->addr);
74 if (a->
addr.
sa.sa_family == AF_INET6) {
75 s->
size = (socklen_t)
sizeof(s->addr.sin6);
77 }
else if (a->
addr.
sa.sa_family == AF_INET) {
78 s->size = (socklen_t)
sizeof(s->addr.sin);
81 s->size = (socklen_t)a->
size;
89 session_t *dtls_session, uint8 *data,
size_t len) {
94 get_session_addr(dtls_session, &remote_addr);
104 dtls_application_data(
struct dtls_context_t *dtls_context,
105 session_t *dtls_session, uint8 *data,
size_t len) {
110 get_session_addr(dtls_session, &remote_addr);
114 "dropped message that was received on invalid interface\n");
121 static int coap_event_dtls = 0;
124 dtls_event(
struct dtls_context_t *dtls_context,
125 session_t *dtls_session,
126 dtls_alert_level_t level,
131 if (level == DTLS_ALERT_LEVEL_FATAL)
136 case DTLS_ALERT_CLOSE_NOTIFY:
141 case DTLS_EVENT_CONNECTED:
146 case DTLS_EVENT_RENEGOTIATE:
162 get_psk_info(
struct dtls_context_t *dtls_context,
163 const session_t *dtls_session,
164 dtls_credentials_type_t type,
165 const uint8_t *
id,
size_t id_len,
166 unsigned char *result,
size_t result_length) {
169 int fatal_error = DTLS_ALERT_INTERNAL_ERROR;
170 size_t identity_length;
171 static int client = 0;
173 static size_t psk_len = 0;
177 if (type == DTLS_PSK_KEY && client) {
178 if (psk_len > result_length) {
182 memcpy(result, psk, psk_len);
189 get_session_addr(dtls_session, &remote_addr);
197 case DTLS_PSK_IDENTITY:
206 psk_len = coap_context->
get_client_psk(coap_session, (
const uint8_t*)
id, id_len, (
uint8_t*)result, &identity_length, result_length, psk,
sizeof(psk));
209 fatal_error = DTLS_ALERT_CLOSE_NOTIFY;
213 return (
int)identity_length;
233 return dtls_alert_fatal_create(fatal_error);
236 static dtls_handler_t cb = {
238 .read = dtls_application_data,
240 .get_psk_info = get_psk_info,
242 .get_ecdsa_key = NULL,
243 .verify_ecdsa_key = NULL
249 struct dtls_context_t *dtls_context = dtls_new_context(coap_context);
252 dtls_set_handler(dtls_context, &cb);
262 struct dtls_context_t *dtls_context = (
struct dtls_context_t *)handle;
263 dtls_free_context(dtls_context);
269 session_t *dtls_session =
coap_malloc_type(COAP_DTLS_SESSION,
sizeof(session_t));
274 dtls_session_init(dtls_session);
276 dtls_session->ifindex = session->
ifindex;
301 dtls_session) >= 0) {
324 struct dtls_context_t *ctx;
325 if (coap_session->
context == NULL)
328 if (coap_session->
tls && ctx) {
329 dtls_peer_t *peer = dtls_get_peer(ctx, (session_t *)coap_session->
tls);
331 dtls_reset_peer(ctx, peer);
333 dtls_close(ctx, (session_t *)coap_session->
tls);
336 coap_session->
tls = NULL;
351 coap_event_dtls = -1;
353 memcpy (&data_rw, &data,
sizeof(data_rw));
355 (session_t *)session->
tls, data_rw, data_len);
360 if (coap_event_dtls >= 0) {
378 clock_time_t next = 0;
379 dtls_check_retransmit((
struct dtls_context_t *)dtls_context, &next);
401 session_t *dtls_session = (session_t *)session->
tls;
405 coap_event_dtls = -1;
407 memcpy (&data_rw, &data,
sizeof(data_rw));
408 err = dtls_handle_message(
410 dtls_session, data_rw, (
int)data_len);
416 if (coap_event_dtls >= 0) {
434 session_t dtls_session;
435 struct dtls_context_t *dtls_context =
439 dtls_session_init(&dtls_session);
441 dtls_session.ifindex = session->
ifindex;
443 memcpy (&data_rw, &data,
sizeof(data_rw));
444 int res = dtls_handle_message(dtls_context, &dtls_session,
445 data_rw, (
int)data_len);
447 if (dtls_get_peer(dtls_context, &dtls_session))
461 #define UNUSED __attribute__((unused)) 473 const char *vers = dtls_package_version();
477 long int p1, p2 = 0, p3 = 0;
480 p1 = strtol(vers, &endptr, 10);
481 if (*endptr ==
'.') {
482 p2 = strtol(endptr+1, &endptr, 10);
483 if (*endptr ==
'.') {
484 p3 = strtol(endptr+1, &endptr, 10);
487 version.
version = (p1 << 16) | (p2 << 8) | p3;
504 const char *ca_file UNUSED,
505 const char *ca_path UNUSED
512 const char *hint UNUSED,
537 size_t data_len UNUSED
544 size_t data_len UNUSED
557 #pragma GCC diagnostic ignored "-Wunused-function" void coap_dtls_set_log_level(int level)
Sets the log level to the specified value.
void coap_dtls_free_session(struct coap_dtls_context_t *dtls_context, struct coap_dtls_session_t *session)
int coap_dtls_hello(coap_session_t *session UNUSED, const uint8_t *data UNUSED, size_t data_len UNUSED)
coap_address_t remote
remote address and port
void coap_tls_free_session(coap_session_t *coap_session UNUSED)
struct coap_context_t * context
session's context
struct coap_dtls_context_t * coap_dtls_new_context(struct coap_context_t *coap_context)
Creates a new DTLS context for the given coap_context.
void * tls
security parameters
int coap_dtls_receive(coap_session_t *session UNUSED, const uint8_t *data UNUSED, size_t data_len UNUSED)
struct coap_dtls_session_t * coap_dtls_new_session(struct coap_dtls_context_t *dtls_context, const coap_endpoint_t *local_interface, const coap_address_t *remote)
multi-purpose address abstraction
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx UNUSED)
#define COAP_EVENT_DTLS_RENEGOTIATE
ssize_t coap_tls_read(coap_session_t *session UNUSED, uint8_t *data UNUSED, size_t data_len UNUSED)
int coap_dtls_get_log_level(void)
Returns the current log level.
void * coap_dtls_new_client_session(coap_session_t *session UNUSED)
int coap_dtls_is_supported(void)
Returns 1 if support for DTLS is enabled, or 0 otherwise.
int coap_dtls_context_set_pki(coap_context_t *ctx UNUSED, coap_dtls_pki_t *setup_data UNUSED, coap_dtls_role_t role UNUSED)
void * coap_tls_new_server_session(coap_session_t *session UNUSED, int *connected UNUSED)
ssize_t coap_tls_write(coap_session_t *session UNUSED, const uint8_t *data UNUSED, size_t data_len UNUSED)
ssize_t coap_session_send(coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for datagram data transmission.
coap_session_t * coap_session_get_by_peer(coap_context_t *ctx, const coap_address_t *remote_addr, int ifindex)
uint64_t version
(D)TLS runtime Library Version
size_t(* get_client_psk)(const coap_session_t *session, const uint8_t *hint, size_t hint_len, uint8_t *identity, size_t *identity_len, size_t max_identity_len, uint8_t *psk, size_t max_psk_len)
void * coap_tls_new_client_session(coap_session_t *session UNUSED, int *connected UNUSED)
int coap_dtls_send(struct coap_context_t *coap_context, struct coap_dtls_session_t *session, const coap_pdu_t *pdu)
int coap_tls_is_supported(void)
Check whether TLS is available.
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
coap_tls_library_t type
Library type.
coap_tls_version_t * coap_get_tls_library_version(void)
Determine the type and version of the underlying (D)TLS library.
coap_addr_tuple_t addr_info
key: remote/local address info
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
unsigned int coap_dtls_get_overhead(coap_session_t *session UNUSED)
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
The structure used for returning the underlying (D)TLS library information.
#define COAP_EVENT_DTLS_CLOSED
(D)TLS events for COAP_PROTO_DTLS and COAP_PROTO_TLS
int coap_handle_dgram(coap_context_t *ctx, coap_session_t *session, uint8_t *msg, size_t msg_len)
Parses and interprets a CoAP datagram with context ctx.
int coap_dtls_context_set_psk(coap_context_t *ctx UNUSED, const char *hint UNUSED, coap_dtls_role_t role UNUSED)
int ifindex
interface index
size_t(* get_server_hint)(const coap_session_t *session, uint8_t *hint, size_t max_hint_len)
#define COAP_EVENT_DTLS_ERROR
#define COAP_EVENT_DTLS_CONNECTED
int coap_handle_event(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
static ssize_t dtls_send_to_peer(gnutls_transport_ptr_t context, const void *send_buffer, size_t send_buffer_length)
void * coap_dtls_new_server_session(coap_session_t *session UNUSED)
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
The structure used for defining the PKI setup data to be used.
int coap_dtls_context_set_pki_root_cas(struct coap_context_t *ctx UNUSED, const char *ca_file UNUSED, const char *ca_path UNUSED)
size_t(* get_server_psk)(const coap_session_t *session, const uint8_t *identity, size_t identity_len, uint8_t *psk, size_t max_psk_len)
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_dtls_handle_timeout(coap_session_t *session UNUSED)
void coap_dtls_startup(void)
Initialize the underlying (D)TLS Library layer.
coap_tick_t coap_dtls_get_timeout(coap_session_t *session UNUSED, coap_tick_t now UNUSED)
void coap_dtls_free_context(struct coap_dtls_context_t *dtls_context)
Releases the storage allocated for dtls_context.
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
#define coap_log(level,...)
Logging function.
union coap_address_t::@0 addr
socklen_t size
size of addr
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context UNUSED)
uint64_t built_version
(D)TLS Built against Library Version
void coap_dtls_session_update_mtu(coap_session_t *session UNUSED)
The CoAP stack's global state is stored in a coap_context_t object.
int coap_dtls_is_context_timeout(void)
Check if timeout is handled per CoAP session or per CoAP context.
Pulls together all the internal only header files.