Contiki-NG
Loading...
Searching...
No Matches
edhoc-msgs.h File Reference

EDHOC message serialization and parsing API. More...

#include <stdint.h>
#include <stddef.h>
#include "edhoc-key-storage.h"
#include "edhoc-config.h"
#include "edhoc-error.h"
#include "lib/cbor.h"

Go to the source code of this file.

Functions

void print_msg_1 (edhoc_msg_1_t *msg)
 Print EDHOC message 1 contents for debugging.
void print_msg_2 (edhoc_msg_2_t *msg)
 Print EDHOC message 2 contents for debugging.
void print_msg_3 (edhoc_msg_3_t *msg)
 Print EDHOC message 3 contents for debugging.
size_t edhoc_serialize_msg_1 (edhoc_msg_1_t *msg, unsigned char *buffer, size_t buffer_sz, bool suite_array)
 Serialize EDHOC message 1 to CBOR format.
size_t edhoc_serialize_err (const edhoc_msg_error_t *msg, unsigned char *buffer, size_t buffer_sz)
 Serialize EDHOC error message to CBOR format.
edhoc_error_t edhoc_deserialize_msg_1 (edhoc_msg_1_t *msg, unsigned char *buffer, size_t buff_sz)
 Deserialize EDHOC message 1 from CBOR data.
edhoc_error_t edhoc_deserialize_msg_2 (edhoc_msg_2_t *msg, unsigned char *buffer, size_t buff_sz)
 Deserialize EDHOC message 2 from CBOR data.
edhoc_error_t edhoc_deserialize_msg_3 (edhoc_msg_3_t *msg, unsigned char *buffer, size_t buff_sz)
 Deserialize EDHOC message 3 from CBOR data.
bool edhoc_deserialize_err (edhoc_msg_error_t *msg, const uint8_t *data, size_t data_size)
 Deserialize EDHOC error message from CBOR data.
edhoc_error_t edhoc_get_auth_key_from_kid (uint8_t *kid, uint8_t kid_sz, cose_key_t **key)
 Get authentication key by Key ID (KID).
int8_t edhoc_get_key_id_cred_x (cbor_reader_state_t *reader, uint8_t *id_cred_x, size_t id_cred_x_buffer_size, cose_key_t *key)
 Parse ID_CRED_X from CBOR data and extract authentication key.
uint8_t edhoc_get_sign (cbor_reader_state_t *reader, uint8_t **sign)
 Parse signature data from CBOR reader.
uint8_t edhoc_get_ad (cbor_reader_state_t *reader, uint8_t *ad, size_t ad_buffer_size)
 Parse Additional Data (AD) from CBOR reader.
edhoc_error_t edhoc_process_ead_item (const edhoc_ead_data_t *ead_data)
 Process and validate EAD (External Authorization Data) items.
void edhoc_write_byte_identifier (cbor_writer_state_t *state, const uint8_t *bytes, size_t len)
 Write EDHOC byte identifier according to RFC 9528 rules.
const uint8_t * edhoc_read_byte_identifier (cbor_reader_state_t *reader, size_t *size)
 Read byte identifier from CBOR reader.

Detailed Description

EDHOC message serialization and parsing API.

Author
Lidia Pocero pocer.nosp@m.o@is.nosp@m.i.gr, Peter A Jonsson, Rikard Höglund, Marco Tiloca, Niclas Finne nicla.nosp@m.s.fi.nosp@m.nne@r.nosp@m.i.se, Nicolas Tsiftes nicol.nosp@m.as.t.nosp@m.sifte.nosp@m.s@ri.nosp@m..se

Definition in file edhoc-msgs.h.

Function Documentation

◆ edhoc_deserialize_err()

bool edhoc_deserialize_err ( edhoc_msg_error_t * msg,
const uint8_t * data,
size_t data_size )

Deserialize EDHOC error message from CBOR data.

Parameters
msgoutput error message structure
datainput CBOR data to parse
data_sizeinput size of CBOR data
Returns
true if the data could be parsed as an EDHOC error message, or false on error.

Definition at line 227 of file edhoc-msgs.c.

References cbor_get_remaining(), cbor_init_reader(), cbor_read_signed(), cbor_read_simple(), cbor_read_text(), CBOR_SIZE_NONE, cbor_skip_next(), and value().

Referenced by edhoc_check_err_rx_msg().

◆ edhoc_deserialize_msg_1()

edhoc_error_t edhoc_deserialize_msg_1 ( edhoc_msg_1_t * msg,
unsigned char * buffer,
size_t buff_sz )

Deserialize EDHOC message 1 from CBOR data.

Parameters
msgoutput message 1 structure
bufferinput CBOR data to parse
buff_szinput size of CBOR data
Returns
EDHOC_SUCCESS on success, error code on failure

Definition at line 299 of file edhoc-msgs.c.

References cbor_get_remaining(), cbor_init_reader(), cbor_read_data(), cbor_read_signed(), cbor_read_unsigned(), CBOR_SIZE_1, CBOR_SIZE_NONE, EDHOC_ERR_MSG_MALFORMED, EDHOC_MAX_CID_LEN, edhoc_read_byte_identifier(), and value().

Referenced by edhoc_handler_msg_1().

◆ edhoc_deserialize_msg_2()

edhoc_error_t edhoc_deserialize_msg_2 ( edhoc_msg_2_t * msg,
unsigned char * buffer,
size_t buff_sz )

Deserialize EDHOC message 2 from CBOR data.

Parameters
msgoutput message 2 structure
bufferinput CBOR data to parse
buff_szinput size of CBOR data
Returns
EDHOC_SUCCESS on success, error code on failure

Definition at line 388 of file edhoc-msgs.c.

References cbor_init_reader(), cbor_read_data(), EDHOC_ERR_MSG_MALFORMED, EDHOC_ERR_NULL_POINTER, and EDHOC_MAX_PAYLOAD_LEN.

Referenced by edhoc_handler_msg_2().

◆ edhoc_deserialize_msg_3()

edhoc_error_t edhoc_deserialize_msg_3 ( edhoc_msg_3_t * msg,
unsigned char * buffer,
size_t buff_sz )

Deserialize EDHOC message 3 from CBOR data.

Parameters
msgoutput message 3 structure
bufferinput CBOR data to parse
buff_szinput size of CBOR data
Returns
EDHOC_SUCCESS on success, error code on failure

Definition at line 425 of file edhoc-msgs.c.

References cbor_init_reader(), cbor_read_data(), EDHOC_ERR_MSG_MALFORMED, EDHOC_ERR_NULL_POINTER, and EDHOC_MAX_PAYLOAD_LEN.

Referenced by edhoc_handler_msg_3().

◆ edhoc_get_ad()

uint8_t edhoc_get_ad ( cbor_reader_state_t * reader,
uint8_t * ad,
size_t ad_buffer_size )

Parse Additional Data (AD) from CBOR reader.

Parameters
readerinput CBOR reader state positioned at AD data
adoutput buffer to store parsed AD
ad_buffer_sizeinput size of ad buffer
Returns
AD size in bytes on success, 0 on failure

Reads Additional Data from CBOR stream and copies it to the provided buffer. Validates that the AD size does not exceed buffer or protocol limits.

Definition at line 734 of file edhoc-msgs.c.

References cbor_read_data(), and EDHOC_MAX_AD_SZ.

Referenced by edhoc_authenticate_msg().

◆ edhoc_get_auth_key_from_kid()

edhoc_error_t edhoc_get_auth_key_from_kid ( uint8_t * kid,
uint8_t kid_sz,
cose_key_t ** key )

Get authentication key by Key ID (KID).

Parameters
kidinput key identifier bytes
kid_szinput key identifier length
keyoutput pointer to retrieved authentication key
Returns
EDHOC_SUCCESS on success, error code on failure

Searches the key repository for a key matching the provided KID.

Definition at line 462 of file edhoc-msgs.c.

References edhoc_check_key_list_kid().

◆ edhoc_get_key_id_cred_x()

int8_t edhoc_get_key_id_cred_x ( cbor_reader_state_t * reader,
uint8_t * id_cred_x,
size_t id_cred_x_buffer_size,
cose_key_t * key )

Parse ID_CRED_X from CBOR data and extract authentication key.

Parameters
readerinput CBOR reader state positioned at ID_CRED_X data
id_cred_xoutput buffer to store parsed ID_CRED_X (can be NULL)
id_cred_x_buffer_sizeinput size of id_cred_x buffer
keyoutput parsed authentication key information
Returns
size of ID_CRED_X data on success, negative error code on failure

Parses ID_CRED_X in three supported formats:

  • Compact encoding: bare KID value
  • Map format: {4: KID}
  • Full credential inclusion: complete COSE key structure Populates the key structure with authentication information.

Definition at line 670 of file edhoc-msgs.c.

References cbor_get_position(), EDHOC_ERR_ID_CRED_MALFORMED, EDHOC_FAILED, and start().

Referenced by edhoc_authenticate_msg().

◆ edhoc_get_sign()

uint8_t edhoc_get_sign ( cbor_reader_state_t * reader,
uint8_t ** sign )

Parse signature data from CBOR reader.

Parameters
readerinput CBOR reader state positioned at signature data
signoutput pointer to signature data (points into reader buffer)
Returns
signature size in bytes on success, 0 on failure

Reads and validates signature data from CBOR stream. The returned pointer references data within the reader's buffer and remains valid until the reader is reinitialized or goes out of scope.

Definition at line 709 of file edhoc-msgs.c.

References cbor_read_data().

Referenced by edhoc_authenticate_msg().

◆ edhoc_process_ead_item()

edhoc_error_t edhoc_process_ead_item ( const edhoc_ead_data_t * ead_data)

Process and validate EAD (External Authorization Data) items.

Parameters
ead_datainput EAD data structure containing label and value
Returns
EDHOC_SUCCESS on success, EDHOC_ERR_CRITICAL_EAD_UNSUPPORTED if critical EAD cannot be processed, other error code on failure

Processes EAD items according to RFC 9528 Section 6:

  • Critical EAD items (negative ead_label) that cannot be processed trigger an error
  • Non-critical EAD items (non-negative ead_label) can be ignored Applications should implement custom handlers for recognized EAD labels.

Definition at line 765 of file edhoc-msgs.c.

References EDHOC_ERR_CRITICAL_EAD_UNSUPPORTED, and EDHOC_ERR_NULL_POINTER.

Referenced by edhoc_handler_msg_1().

◆ edhoc_read_byte_identifier()

const uint8_t * edhoc_read_byte_identifier ( cbor_reader_state_t * reader,
size_t * size )

Read byte identifier from CBOR reader.

Reads byte identifiers following RFC 9528 optimization. Returns pointer to the identifier bytes and sets size to the length.

Definition at line 85 of file edhoc-msgs.c.

References cbor_get_position(), cbor_peek_next(), cbor_read_data(), cbor_read_signed(), CBOR_SIZE_1, and value().

Referenced by edhoc_authenticate_msg(), edhoc_deserialize_msg_1(), and edhoc_handler_msg_2().

◆ edhoc_serialize_err()

size_t edhoc_serialize_err ( const edhoc_msg_error_t * msg,
unsigned char * buffer,
size_t buffer_sz )

Serialize EDHOC error message to CBOR format.

Parameters
msginput error message structure to serialize
bufferoutput buffer for serialized data
buffer_szinput size of output buffer
Returns
size of serialized data on success, 0 on failure

Definition at line 201 of file edhoc-msgs.c.

References cbor_end_writer(), cbor_init_writer(), cbor_write_bool(), cbor_write_text(), and cbor_write_unsigned().

Referenced by edhoc_generate_error_message().

◆ edhoc_serialize_msg_1()

size_t edhoc_serialize_msg_1 ( edhoc_msg_1_t * msg,
unsigned char * buffer,
size_t buffer_sz,
bool suite_array )

Serialize EDHOC message 1 to CBOR format.

Parameters
msginput message 1 structure to serialize
bufferoutput buffer for serialized data
buffer_szinput size of output buffer
suite_arrayinput whether to encode suites as array (unused)
Returns
size of serialized data on success, 0 on failure

Definition at line 177 of file edhoc-msgs.c.

References cbor_end_writer(), cbor_init_writer(), cbor_write_data(), cbor_write_unsigned(), and edhoc_write_byte_identifier().

◆ edhoc_write_byte_identifier()

void edhoc_write_byte_identifier ( cbor_writer_state_t * state,
const uint8_t * bytes,
size_t len )

Write EDHOC byte identifier according to RFC 9528 rules.

Parameters
stateoutput CBOR writer state
bytesinput identifier bytes to write
leninput length of identifier

Writes byte identifiers following RFC 9528 optimization: single-byte identifiers that coincide with CBOR integer encoding (0x00-0x17, 0x20-0x37) are written as integers, others as byte strings.

Definition at line 108 of file edhoc-msgs.c.

References cbor_write_data(), and cbor_write_object().

Referenced by edhoc_serialize_msg_1().

◆ print_msg_1()

void print_msg_1 ( edhoc_msg_1_t * msg)

Print EDHOC message 1 contents for debugging.

Parameters
msginput message 1 structure to print

Definition at line 51 of file edhoc-msgs.c.

Referenced by edhoc_handler_msg_1().

◆ print_msg_2()

void print_msg_2 ( edhoc_msg_2_t * msg)

Print EDHOC message 2 contents for debugging.

Parameters
msginput message 2 structure to print

Definition at line 69 of file edhoc-msgs.c.

Referenced by edhoc_handler_msg_2().

◆ print_msg_3()

void print_msg_3 ( edhoc_msg_3_t * msg)

Print EDHOC message 3 contents for debugging.

Parameters
msginput message 3 structure to print

Definition at line 77 of file edhoc-msgs.c.

Referenced by edhoc_handler_msg_3().