diff --git a/Makefile.am b/Makefile.am index 6395dc59..2bda7f4a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -55,6 +55,7 @@ libcoap_@LIBCOAP_API_VERSION@_la_SOURCES = \ src/async.c \ src/block.c \ src/coap_dtls.c \ + src/coap_keystore.c \ src/coap_time.c \ src/debug.c \ src/encode.c \ @@ -83,6 +84,7 @@ libcoap_include_HEADERS = \ include/coap/block.h \ include/coap/coap.h \ include/coap/coap_dtls.h \ + include/coap/coap_keystore.h \ include/coap/coap_io.h \ include/coap/coap_time.h \ include/coap/debug.h \ diff --git a/include/coap/async.h b/include/coap/async.h index 0c36defa..cb21b02e 100644 --- a/include/coap/async.h +++ b/include/coap/async.h @@ -15,6 +15,8 @@ #ifndef _COAP_ASYNC_H_ #define _COAP_ASYNC_H_ +#include "coap_time.h" +#include "pdu.h" #include "net.h" #ifndef WITHOUT_ASYNC diff --git a/include/coap/coap.h.in b/include/coap/coap.h.in index f11e3476..8715a4c7 100644 --- a/include/coap/coap.h.in +++ b/include/coap/coap.h.in @@ -36,6 +36,8 @@ extern "C" { #include "async.h" #include "bits.h" #include "block.h" +#include "coap_dtls.h" +#include "coap_keystore.h" #include "coap_io.h" #include "coap_time.h" #include "debug.h" diff --git a/include/coap/coap_keystore.h b/include/coap/coap_keystore.h new file mode 100644 index 00000000..da8a49e4 --- /dev/null +++ b/include/coap/coap_keystore.h @@ -0,0 +1,66 @@ +/* + * coap_keystore.h -- libcoap keystore + * + * Copyright (C) 2016 Olaf Bergmann + * + * This file is part of the CoAP library libcoap. Please see README for terms + * of use. + */ + +#ifndef _COAP_KEYSTORE_H_ +#define _COAP_KEYSTORE_H_ + +#include "coap_config.h" +#include "address.h" + +/** + * @defgroup keys Keystore + * Generic container to store keys for secure communication. + * @{ + */ + +struct coap_keystore_t; +typedef struct coap_keystore_t coap_keystore_t; +struct coap_keystore_item_t; +typedef struct coap_keystore_item_t coap_keystore_item_t; + +coap_keystore_t *coap_new_keystore(void); + +void coap_free_keystore(coap_keystore_t *keystore); + +#define COAP_KEYSTORE_RELEASE_REALM 0x01 +#define COAP_KEYSTORE_RELEASE_ID 0x02 +#define COAP_KEYSTORE_RELEASE_KEY 0x04 + +coap_keystore_item_t * +coap_keystore_new_psk(void *realm, size_t realm_length, + void *id, size_t id_length, + void *key, size_t key_length, + int flags); + + +void coap_keystore_free_item(coap_keystore_item_t *item); + +int coap_keystore_store_item(coap_keystore_t *keystore, + coap_keystore_item_t *item, + const coap_address_t *remote); + +void coap_keystore_remove_item(coap_keystore_t *keystore, + coap_keystore_item_t *item); + +coap_keystore_item_t *coap_keystore_find_psk(const coap_keystore_t *keystore, + const void *realm, + size_t realm_length, + const void *id, + size_t identity_length, + const coap_address_t *remote); + +ssize_t coap_psk_set_identity(const coap_keystore_item_t *psk, + uint8_t *buf, size_t max_len); + +ssize_t coap_psk_set_key(const coap_keystore_item_t *psk, + uint8_t *buf, size_t max_len); + +/** @} */ + +#endif /* COAP_KEYSTORE_H */ diff --git a/include/coap/net.h b/include/coap/net.h index 9bf18d82..fbc432b3 100644 --- a/include/coap/net.h +++ b/include/coap/net.h @@ -22,6 +22,7 @@ #include "coap_io.h" #include "coap_dtls.h" +#include "coap_keystore.h" #include "coap_time.h" #include "option.h" #include "pdu.h" @@ -125,6 +126,7 @@ typedef struct coap_context_t { ssize_t (*network_read)(coap_endpoint_t *ep, coap_packet_t **packet); + coap_keystore_t *keystore; struct coap_dtls_context_t *dtls_context; } coap_context_t; diff --git a/src/coap_keystore.c b/src/coap_keystore.c new file mode 100644 index 00000000..2af00bfd --- /dev/null +++ b/src/coap_keystore.c @@ -0,0 +1,180 @@ +/* + * coap_keystore.c -- libcoap keystore + * + * Copyright (C) 2016 Olaf Bergmann + * + * This file is part of the CoAP library libcoap. Please see README for terms + * of use. + */ + +#include "coap_config.h" +#include "coap.h" + +#include "coap_keystore.h" + +typedef enum coap_credentials_type_t { + COAP_KEYSTORE_UNKNOWN=0, + COAP_KEYSTORE_PSK +} coap_credentials_type_t ; + +typedef struct coap_keystore_psk_t { + void *realm; + size_t realm_length; + void *identity; + size_t identity_length; + void *key; + size_t key_length; + int flags; +} coap_keystore_psk_t; + +struct coap_keystore_item_t { + coap_credentials_type_t type; + struct coap_keystore_item_t *next; + union { + coap_keystore_psk_t psk; + } entry; +}; + +struct coap_keystore_t { + struct coap_keystore_item_t *store; +}; + +coap_keystore_t * +coap_new_keystore(void) { + coap_keystore_t *ks; + + ks = (coap_keystore_t *)coap_malloc(sizeof(coap_keystore_t)); + if (ks) { + memset(ks, 0, sizeof(coap_keystore_t)); + } + return ks; +} + +void +coap_free_keystore(coap_keystore_t *keystore) { + coap_keystore_item_t *item, *tmp; + + if (keystore) { + LL_FOREACH_SAFE(keystore->store, item, tmp) { + coap_keystore_free_item(item); + } + coap_free(keystore); + } +} + + +coap_keystore_item_t * +coap_keystore_new_psk(void *realm, size_t realm_length, + void *id, size_t id_length, + void *key, size_t key_length, int flags) { + coap_keystore_item_t *item; + item = (coap_keystore_item_t *)coap_malloc(sizeof(coap_keystore_item_t)); + + if (item) { + memset(item, 0, sizeof(coap_keystore_item_t)); + item->type = COAP_KEYSTORE_PSK; + item->entry.psk.realm = realm; + item->entry.psk.realm_length = realm_length; + item->entry.psk.identity = id; + item->entry.psk.identity_length = id_length; + item->entry.psk.key = key; + item->entry.psk.key_length = key_length; + item->entry.psk.flags = flags; + } + + return item; +} + +static void +free_psk(coap_keystore_psk_t *psk) { + if (psk) { + if ((psk->flags & COAP_KEYSTORE_RELEASE_REALM) != 0) { + coap_free(psk->realm); + } + if ((psk->flags & COAP_KEYSTORE_RELEASE_ID) != 0) { + coap_free(psk->identity); + } + if ((psk->flags & COAP_KEYSTORE_RELEASE_KEY) != 0) { + coap_free(psk->key); + } + } +} + +void +coap_keystore_free_item(coap_keystore_item_t *item) { + if (item) { + switch (item->type) { + case COAP_KEYSTORE_PSK: free_psk(&item->entry.psk); break; + case COAP_KEYSTORE_UNKNOWN: + default: + ; + } + coap_free(item); + } +} + +int +coap_keystore_store_item(coap_keystore_t *keystore, + coap_keystore_item_t *item, + const coap_address_t *remote __attribute__((unused))) { + LL_PREPEND(keystore->store, item); + return 1; +} + +void +coap_keystore_remove_item(coap_keystore_t *keystore, + coap_keystore_item_t *item) { + LL_DELETE(keystore->store, item); + coap_keystore_free_item(item); +} + +static inline int +match(const void *a, size_t alen, const void *b, size_t blen) { + return !a || !b || ((alen == blen) && (memcmp(a, b, alen) == 0)); +} + +coap_keystore_item_t * +coap_keystore_find_psk(const coap_keystore_t *keystore, + const void *realm, size_t realm_length, + const void *identity, size_t identity_length, + const coap_address_t *remote __attribute__((unused))) { + coap_keystore_item_t *item; +#define MATCH_PSK_FIELD(Field, Object) \ + match((Field), \ + Field##_length, \ + (Object)->entry.psk.Field, \ + (Object)->entry.psk.Field##_length) + + LL_FOREACH(keystore->store, item) { + if (item->type == COAP_KEYSTORE_PSK) { + if (MATCH_PSK_FIELD(realm, item) && + MATCH_PSK_FIELD(identity, item)) { + return item; + } + } + } + return NULL; +} + +ssize_t +coap_psk_set_identity(const coap_keystore_item_t *psk, + uint8_t *buf, size_t max_len) { + if (psk->type != COAP_KEYSTORE_PSK || + max_len < psk->entry.psk.identity_length) { + return -1; + } + + memcpy(buf, psk->entry.psk.identity, psk->entry.psk.identity_length); + return psk->entry.psk.identity_length; +} + +ssize_t coap_psk_set_key(const coap_keystore_item_t *psk, + uint8_t *buf, size_t max_len) { + if (psk->type != COAP_KEYSTORE_PSK || + max_len < psk->entry.psk.key_length) { + return -1; + } + + memcpy(buf, psk->entry.psk.key, psk->entry.psk.key_length); + return psk->entry.psk.key_length; +} diff --git a/src/net.c b/src/net.c index 7fee81e3..cbcbc8fb 100644 --- a/src/net.c +++ b/src/net.c @@ -386,9 +386,12 @@ coap_new_context( memset(c, 0, sizeof( coap_context_t ) ); + c->keystore = coap_new_keystore(); c->dtls_context = coap_dtls_new_context(c); - if (c->dtls_context == NULL) { - coap_log(LOG_WARNING, "coap_init: no DTLS context available\n"); + if (!c->keystore || !c->dtls_context) { + coap_log(LOG_EMERG, "coap_init: no DTLS context available\n"); + coap_free_context(c); + return NULL; } /* initialize message id */ @@ -427,7 +430,7 @@ coap_new_context( void coap_free_context(coap_context_t *context) { - coap_endpoint_t *ep; + coap_endpoint_t *ep, *tmp; if (!context) return; @@ -441,12 +444,13 @@ coap_free_context(coap_context_t *context) { coap_delete_all_resources(context); - LL_FOREACH(context->endpoint, ep) { + coap_dtls_free_context(context->dtls_context); + coap_free_keystore(context->keystore); + + LL_FOREACH_SAFE(context->endpoint, ep, tmp) { coap_free_endpoint(ep); } - coap_dtls_free_context(context->dtls_context); - #ifndef WITH_CONTIKI coap_free_type(COAP_CONTEXT, context); #endif/* not WITH_CONTIKI */ @@ -664,7 +668,6 @@ coap_send(coap_context_t *context, return id; } - coap_log(LOG_WARNING, "coap_send: huh, no DTLS\n"); return coap_send_impl(context, local_interface, dst, pdu); } @@ -873,6 +876,7 @@ coap_read(coap_context_t *ctx) { return -1; } #else /* WITH_LWIP */ + static int coap_read_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint) { ssize_t bytes_read = -1;