43#include "contiki-lib.h"
47#define LOG_MODULE "EDHOC"
48#define LOG_LEVEL LOG_LEVEL_EDHOC
66 if(identity == NULL || authentication_key == NULL) {
76 if(key->identity_sz == identity_size &&
77 memcmp(key->identity, identity, identity_size) == 0) {
78 *authentication_key = key;
88 if(key_id == NULL || authentication_key == NULL) {
92 if(key_id_size == 0 || key_id_size >
sizeof(((
cose_key_t*)0)->kid)) {
98 if(key->kid_sz == key_id_size &&
99 memcmp(key->kid, key_id, key_id_size) == 0) {
100 *authentication_key = key;
101 return EDHOC_SUCCESS;
122 LOG_ERR(
"Failed to allocate memory for key\n");
129 LOG_DBG(
"Added key KID 0x%02x (sz=%d), identity='%.*s'\n",
130 k->kid[0], k->kid_sz, k->identity_sz, k->identity);
132 return EDHOC_SUCCESS;
138 if(k == NULL || key == NULL) {
143 return EDHOC_SUCCESS;
155 if(result == EDHOC_SUCCESS) {
158 return EDHOC_SUCCESS;
166 if(identity == NULL) {
172 if(result == EDHOC_SUCCESS) {
175 return EDHOC_SUCCESS;
183 if(auth_key == NULL) {
189 return EDHOC_SUCCESS;
196 LOG_DBG_BYTES(cose->kid, cose->kid_sz);
198 LOG_DBG(
"identity: ");
199 LOG_DBG_BYTES((uint8_t *)cose->identity, cose->identity_sz);
201 LOG_DBG(
"kty: %d\n", cose->kty);
202 LOG_DBG(
"crv: %d\n", cose->crv);
204 LOG_DBG_BYTES(cose->ecc.pub.x, ECC_KEY_LEN);
207 LOG_DBG_BYTES(cose->ecc.pub.y, ECC_KEY_LEN);
214 if(key == NULL || label == NULL) {
215 LOG_ERR(
"Invalid parameters for key info print\n");
219 LOG_INFO(
"=== %s Key Information ===\n", label);
221 for(
int i = 0; i < key->kid_sz; i++) {
222 LOG_INFO_(
"%02x", key->kid[i]);
225 LOG_INFO(
"Identity: %.*s\n", key->identity_sz, key->identity);
226 LOG_INFO(
"Key Type: %s\n", key->kty == 2 ?
"EC2" :
"Unknown");
227 LOG_INFO(
"Curve: %s\n", key->crv == 1 ?
"P-256" :
"Unknown");
228 LOG_INFO(
"Public Key X: ");
229 for(
int i = 0; i < 8; i++) {
230 LOG_INFO_(
"%02x ", key->ecc.pub.x[i]);
232 LOG_INFO_(
"... (32 bytes total)\n");
233 LOG_INFO(
"Public Key Y: ");
234 for(
int i = 0; i < 8; i++) {
235 LOG_INFO_(
"%02x ", key->ecc.pub.y[i]);
237 LOG_INFO_(
"... (32 bytes total)\n");
238 LOG_INFO(
"Private Key: %s\n",
239 (key->ecc.priv[0] == 0 && key->ecc.priv[1] == 0) ?
"Not present" :
"Present");
240 LOG_INFO(
"=============================\n");
246 if(label == NULL || cred == NULL || cred_sz == 0) {
247 LOG_ERR(
"Invalid parameters for credential print\n");
251 LOG_INFO(
"%s (%zu bytes): ", label, cred_sz);
252 for(
size_t i = 0; i < cred_sz && i < 32; i++) {
253 LOG_INFO_(
"%02x ", cred[i]);
256 LOG_INFO_(
"... (%zu more bytes)", cred_sz - 32);
260 LOG_INFO(
"%s structure: {identity: ..., cose_key: {kty, kid, crv, x, y}}\n", label);
282 LOG_INFO(
" %d. KID: %02x, Identity: %.*s, Type: %s\n",
283 count, key->kid[0], key->identity_sz, key->identity,
284 key->kty == 2 ?
"EC2" :
"Unknown");
288 LOG_INFO(
" No keys loaded\n");
295 LOG_INFO(
"=== EDHOC Key Configuration Check ===\n");
299 LOG_ERR(
"No keys loaded!\n");
303 LOG_INFO(
"Loaded keys (%d total):\n", key_count);
307 uint8_t keys_with_private = 0;
308 uint8_t keys_public_only = 0;
311 if(key->ecc.priv[0] != 0 || key->ecc.priv[1] != 0) {
318 LOG_WARN(
"Key %02x: Non-EC2 key type (%d)\n", key->kid[0], key->kty);
322 LOG_WARN(
"Key %02x: Non-P-256 curve (%d)\n", key->kid[0], key->crv);
326 LOG_INFO(
"Keys with private key: %d (for own identity)\n", keys_with_private);
327 LOG_INFO(
"Public-only keys: %d (for peer verification)\n", keys_public_only);
329 if(keys_with_private == 0) {
330 LOG_WARN(
"No keys with private key found - cannot authenticate as any identity\n");
333 if(keys_public_only == 0) {
334 LOG_WARN(
"No public-only keys found - cannot verify peer credentials\n");
337 LOG_INFO(
"=====================================\n");
338 return EDHOC_SUCCESS;
343 const char *own_label,
const char *peer_label)
345 if(own_key == NULL || peer_key == NULL || own_label == NULL || peer_label == NULL) {
349 memset(peer_key->ecc.priv, 0,
sizeof(peer_key->ecc.priv));
352 if(result != EDHOC_SUCCESS) {
358 if(result != EDHOC_SUCCESS) {
363 LOG_INFO(
"Key pair configured: %s (KID: %02x) <-> %s (KID: %02x)\n",
364 own_label, own_key->kid[0], peer_label, peer_key->kid[0]);
366 return EDHOC_SUCCESS;
const char * edhoc_error_string(edhoc_error_t error)
Get a human-readable error message for an error code.
Error handling module header for EDHOC.
edhoc_error_t
Unified error type for EDHOC operations.
@ EDHOC_ERR_NULL_POINTER
Null pointer provided.
@ EDHOC_ERR_KEY_NOT_FOUND
Key not found in storage.
@ EDHOC_ERR_DUPLICATE_KEY
Duplicate key in storage.
@ EDHOC_ERR_INVALID_LENGTH
Invalid length parameter.
@ EDHOC_ERR_MEMORY_ALLOCATION
Memory allocation failed.
void edhoc_print_credential(const char *label, const uint8_t *cred, size_t cred_sz)
Print credential (CRED_I/CRED_R) values in readable format.
edhoc_error_t edhoc_create_key_list(void)
Create the keys repository.
void edhoc_list_all_keys(void)
List all keys in the key storage with basic information.
edhoc_error_t edhoc_remove_key_identity(char *identity, uint8_t identity_sz)
Remove from the keys repository the key with the specific identity.
edhoc_error_t edhoc_check_key_list_kid(uint8_t *key_id, uint8_t key_id_size, cose_key_t **authentication_key)
Check in the keys repository for the key with the specific KID.
edhoc_error_t edhoc_add_key(cose_key_t *key)
Add a DH key to the repository.
edhoc_error_t edhoc_remove_key_kid(uint8_t *kid, uint8_t kid_sz)
Remove from the keys repository the key with the specific KID.
void edhoc_print_key_info(const cose_key_t *key, const char *label)
Print detailed key information at INFO level for user visibility.
void cose_print_key(cose_key_t *cose)
Print a key in cose_key_t struct format for debugging.
edhoc_error_t edhoc_validate_key_setup(void)
Validate the current key setup and print diagnostic information.
edhoc_error_t edhoc_copy_key(cose_key_t *k, cose_key_t *key)
Copy a COSE key from one structure to another.
edhoc_error_t edhoc_check_key_list_identity(char *identity, uint8_t identity_size, cose_key_t **authentication_key)
Check in the keys repository for the key with the specific identity.
edhoc_error_t edhoc_setup_key_pair(cose_key_t *own_key, cose_key_t *peer_key, const char *own_label, const char *peer_label)
Helper function to set up a key pair (own + peer) with validation.
edhoc_error_t edhoc_remove_key(cose_key_t *auth_key)
Remove from the keys repository the specific DH cose_key_t key.
uint8_t edhoc_get_key_count(void)
Get the total number of keys loaded in the key storage.
EDHOC key storage - Implementation of key storage for managing DH-static authentication key pairs.
struct cose_key cose_key_t
KEY length in bytes.
static volatile uint64_t count
Num.
#define EDHOC_MAX_IDENTITY_LEN
Maximum length of identity strings in credentials Used for human-readable credential identities (e....
static void list_init(list_t list)
Initialize a list.
#define LIST(name)
Declare a linked list.
static void * list_item_next(const void *item)
Get the next item following this item.
void list_add(list_t list, void *item)
Add an item at the end of a list.
void list_remove(list_t list, const void *item)
Remove a specific element from a list.
static void * list_head(const_list_t list)
Get a pointer to the first element of a list.
int memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
void memb_init(struct memb *m)
Initialize a memory block that was declared with MEMB().
#define MEMB(name, structure, num)
Declare a memory block.
Header file for the logging system.