dtls: Added basic keystore implementation

coap_keystore.[hc] provides a simple data structure for managing
credentials to be used during DTLS handshake. Each coap_context_t
has its own keystore object that is independent of the actual
DTLS context. Applications can access the keystore during the
entire lifetime of the coap context. The existing implementation
is not thread-safe.
This commit is contained in:
Olaf Bergmann committed 2016-04-08 15:14:20 +02:00
1 parent e3b82756c0
commit 90fb5fdffc
7 files changed
+265 -7

No files matched your search

+180
View File
@@ -0,0 +1,180 @@
/*
* coap_keystore.c -- libcoap keystore
*
* Copyright (C) 2016 Olaf Bergmann <bergmann@tzi.org>
*
* 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;
}
+11 -7
View File
@@ -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;