48#if COAP_OSCORE_SUPPORT
53#define OSC_MOVE_PTR(a,b) do { (a) = (b); (b) = NULL; } while(0)
55static int oscore_context_release_recipients(
oscore_ctx_t *osc_ctx);
58compose_info(uint8_t *buffer,
66 size_t rem_size = buf_size;
73 if (id_context !=
NULL && id_context->
length > 0) {
84 (
const char *)type->
s,
90#if COAP_OSCORE_GROUP_SUPPORT
99 size_t keystream_size) {
100 uint8_t info_buffer[30];
101 uint8_t *buffer = info_buffer;
103 size_t rem_size =
sizeof(info_buffer);
127 osc_ctx->sign_enc_key,
141 if (a_len != b_len) {
145 if (memcmp(a_ptr, b_ptr, a_len) == 0) {
156 if (c_context->p_osc_ctx) {
162 if (head == osc_ctx) {
165 while (next != head) {
166 if (next == osc_ctx) {
172 prev->
next = osc_ctx;
173 osc_ctx->
next = head;
175 c_context->p_osc_ctx = osc_ctx;
176 osc_ctx->
next = osc_ctx;
185 assert(rcp_ctx->
ref > 0);
186 if (--rcp_ctx->
ref > 0) {
208#if COAP_OSCORE_GROUP_SUPPORT
209 coap_crypto_delete_public_cred(rcp_ctx->rem_cred);
217#if COAP_OSCORE_GROUP_SUPPORT
226#if COAP_OSCORE_GROUP_SUPPORT
227 coap_crypto_delete_private_key(snd_ctx->my_private_key);
228 coap_crypto_delete_public_cred(snd_ctx->sender_cred);
229 chain = snd_ctx->pw_sender_key_chain;
258#if COAP_OSCORE_GROUP_SUPPORT
261 coap_crypto_delete_public_cred(osc_ctx->gm_cred);
268 while (c_context->p_osc_ctx) {
271 if (osc_ctx->
next == osc_ctx) {
272 c_context->p_osc_ctx =
NULL;
275 c_context->p_osc_ctx = osc_ctx->
next;
276 while (tail->
next != osc_ctx) {
279 tail->
next = c_context->p_osc_ctx;
289 if (oscore_context_release_recipients(osc_ctx) > 0) {
308oscore_context_release_recipients(
oscore_ctx_t *osc_ctx) {
314 while (next !=
NULL) {
316 if (next->
ref <= 1) {
319 oscore_free_recipient_ctx(to_free);
355 if (next == osc_ctx) {
356 if (next->
next == next) {
357 c_context->p_osc_ctx =
NULL;
359 while (prev->
next != next) {
363 if (next == c_context->p_osc_ctx)
364 c_context->p_osc_ctx = next->
next;
372 if (oscore_context_release_recipients(osc_ctx) <= 0) {
382 }
while (next != head);
394 *recipient_ctx =
NULL;
395 assert(rcpkey_id.
length == 0 || rcpkey_id.
s !=
NULL);
405 if (rcpkey_id.
length != 0)
409 ok = ok + (memcmp(pt->
id_context->
s, oscore_r2, 8) != 0);
413 }
else if (ctxkey_id) {
421 }
else if (ctxkey_id->
length > 0)
425 *recipient_ctx = rpt;
432 }
while (pt != c_context->p_osc_ctx);
450 if (session->recipient_ctx) {
451 oscore_ctx_t *tmp_ctx = session->recipient_ctx->osc_ctx;
460 if (memcmp(tmp_ctx->
id_context->
s, oscore_r2, 8) == 0) {
464 }
else if (ctxkey_id) {
465 if (session->recipient_ctx->osc_ctx->id_context !=
NULL) {
469 }
else if (ctxkey_id->
length > 0) {
474 *recipient_ctx = session->recipient_ctx;
475 return session->recipient_ctx->osc_ctx;
487 oscore_r2, recipient_ctx);
488 if (ram_ctx !=
NULL) {
504 if (tmp_ctx !=
NULL) {
515 while (rcp_ctx !=
NULL) {
520 *recipient_ctx = ref_ctx;
540#define OSCORE_LOG_SIZE 16
552 coap_log(level,
" %-16s <>\n", name);
556 for (i = 0; i < value->
length; i += OSCORE_LOG_SIZE) {
557 char number[3 * OSCORE_LOG_SIZE + 4];
560 value->
length - i > OSCORE_LOG_SIZE ?
561 OSCORE_LOG_SIZE : value->
length - i,
564 coap_log(level,
" %-16s %s\n", i == 0 ? name :
"", number);
571 coap_log(level,
" %-16s %2d\n", name, value);
576 coap_log(level,
" %-16s %s\n", name, value);
588 size_t space = (dst_len - 4) / 3;
591 for (qq = 0; qq < src_len && qq < space; qq++) {
592 char tmp = src[qq] >> 4;
594 tmp = tmp + 0x61 - 10;
600 tmp = tmp + 0x61 - 10;
603 dest[qq * 3 + 1] = tmp;
604 dest[qq * 3 + 2] = 0x20;
608 dest[qq * 3 + 1] =
'.';
609 dest[qq * 3 + 2] =
'.';
623 uint8_t info_buffer[80];
628 if (hkdf_tmp ==
NULL)
631 info_len = compose_info(info_buffer,
638 if (info_len == 0 || info_len >
sizeof(info_buffer)) {
656oscore_log_context(
oscore_ctx_t *osc_ctx,
const char *heading) {
657#if COAP_MAX_LOGGING_LEVEL < _COAP_LOG_OSCORE
679#if COAP_OSCORE_GROUP_SUPPORT
682 osc_ctx->sign_enc_key);
691 snprintf(buffer,
sizeof(buffer),
"Recipient ID[%zu]", count);
695 snprintf(buffer,
sizeof(buffer),
"Recipient Key[%zu]", count);
753#if COAP_OSCORE_GROUP_SUPPORT
755 temp = osc_ctx->sign_enc_key;
756 osc_ctx->sign_enc_key =
760 osc_ctx->alg_group_enc,
764 if (!osc_ctx->sign_enc_key)
765 osc_ctx->sign_enc_key = temp;
770 oscore_log_context(osc_ctx,
"Updated Common context");
789 if (sender_ctx ==
NULL)
835#if COAP_OSCORE_GROUP_SUPPORT
837 osc_ctx->sign_enc_key =
841 osc_ctx->alg_group_enc,
845 if (!osc_ctx->sign_enc_key)
862 if (rcp_conf ==
NULL)
872 oscore_log_context(osc_ctx,
"New Common context");
895 if (sender_ctx ==
NULL)
909#if COAP_OSCORE_GROUP_SUPPORT
910 OSC_MOVE_PTR(osc_ctx->gm_cred, oscore_conf->gm_cred);
950#if COAP_OSCORE_GROUP_SUPPORT
951 if (oscore_conf->
sender->group_mode || oscore_conf->
sender->pairwise_mode) {
952 size_t counter_signature_parameters_len = 0;
953 uint8_t *counter_signature_parameters =
956 &counter_signature_parameters_len);
957 osc_ctx->alg_group_enc = oscore_conf->alg_group_enc;
958 osc_ctx->alg_signature = oscore_conf->alg_signature;
959 osc_ctx->alg_pairwise_key_agreement = oscore_conf->alg_pairwise_key_agreement;
961 counter_signature_parameters_len);
967 osc_ctx->sign_enc_key =
971 osc_ctx->alg_group_enc,
975 if (!osc_ctx->sign_enc_key)
991 if (oscore_conf->
sender) {
993#if COAP_OSCORE_GROUP_SUPPORT
994 sender_ctx->group_mode = oscore_conf->
sender->group_mode;
995 sender_ctx->pairwise_mode = oscore_conf->
sender->pairwise_mode;
996 OSC_MOVE_PTR(sender_ctx->sender_cred, oscore_conf->
sender->gs_public_cred);
997 OSC_MOVE_PTR(sender_ctx->my_private_key, oscore_conf->
sender->gs_private_key);
1003 sender_ctx->my_private_key->pri_der);
1006 sender_ctx->my_private_key->pri_raw);
1009 sender_ctx->my_private_key->wire_sign_size);
1012 sender_ctx->sender_cred->pub_der);
1015 sender_ctx->sender_cred->pub_raw);
1018 sender_ctx->sender_cred->auth_cred);
1021 sender_ctx->sender_cred->wire_sign_size);
1025 buffer,
sizeof(buffer)));
1029 buffer,
sizeof(buffer)));
1047 rcp_conf = rcp_next;
1053 oscore_log_context(osc_ctx,
"Common context");
1080 uint32_t break_key) {
1085 coap_log_warn(
"oscore_add_recipient: Maximum size of recipient_id is 7 bytes\n");
1099 if (rcp_ctx ==
NULL) {
1134#if COAP_OSCORE_GROUP_SUPPORT
1137 if (rcp_conf->group_mode) {
1146 if (rcp_conf->pairwise_mode) {
1156 OSC_MOVE_PTR(rcp_ctx->rem_cred, rcp_conf->gr_public_cred);
1160 coap_log_warn(
"OSCORE Group not supported by underlying TLS library\n");
1165 coap_log_warn(
"OSCORE Pairwise not supported by underlying TLS library\n");
1173 if (rcp_ctx->rem_cred !=
NULL) {
1176 rcp_ctx->rem_cred->pub_der);
1179 rcp_ctx->rem_cred->pub_raw);
1182 rcp_ctx->rem_cred->auth_cred);
1186 rcp_ctx->rem_cred->wire_sign_size);
1190 buffer,
sizeof(buffer)));
1194 buffer,
sizeof(buffer)));
1200 if (coap_crypto_derive_shared_secret(osc_ctx->alg_pairwise_key_agreement,
1201 snd_ctx->my_private_key->pri_raw,
1202 rcp_ctx->rem_cred->pub_raw,
1208 rcp_ctx->rem_cred->pub_der->length +
1215 memcpy(&ikm->
s[0], snd_ctx->sender_cred->pub_der->s,
1216 snd_ctx->sender_cred->pub_der->length);
1217 memcpy(&ikm->
s[snd_ctx->sender_cred->pub_der->length],
1218 rcp_ctx->rem_cred->pub_der->s,
1219 rcp_ctx->rem_cred->pub_der->length);
1220 memcpy(&ikm->
s[snd_ctx->sender_cred->pub_der->length +
1221 rcp_ctx->rem_cred->pub_der->length],
1224 rcp_ctx->pw_rem_recipient_key =
1232 if (!rcp_ctx->pw_rem_recipient_key)
1236 rcp_ctx->pw_rem_recipient_key);
1239 memcpy(&ikm->
s[0], rcp_ctx->rem_cred->pub_der->s,
1240 rcp_ctx->rem_cred->pub_der->length);
1241 memcpy(&ikm->
s[rcp_ctx->rem_cred->pub_der->length],
1242 snd_ctx->sender_cred->pub_der->s,
1243 snd_ctx->sender_cred->pub_der->length);
1244 memcpy(&ikm->
s[rcp_ctx->rem_cred->pub_der->length +
1245 snd_ctx->sender_cred->pub_der->length],
1248 rcp_ctx->pw_rem_recipient_key =
1262 oscore_free_recipient_ctx(rcp_ctx);
1299 oscore_free_recipient_ctx(next);
1310 recipient_ctx->
ref++;
1317 if (recipient_ctx ==
NULL || *recipient_ctx ==
NULL)
1321 osc_ctx = (*recipient_ctx)->osc_ctx;
1322 oscore_free_recipient_ctx(*recipient_ctx);
1330 *recipient_ctx =
NULL;
1360 if (association ==
NULL)
1371 const uint8_t *data;
1444 session->associations =
NULL;
1454#pragma GCC diagnostic ignored "-Wunused-function"
Library specific build wrapper for coap_internal.h.
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_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
#define COAP_OSCORE_DEFAULT_REPLAY_WINDOW
#define coap_lock_callback_ret(r, func)
Dummy for no thread-safe code.
coap_log_t coap_get_log_level(void)
Get the current logging level.
#define coap_log_oscore(...)
#define coap_log_warn(...)
#define coap_log(level,...)
Logging function.
size_t oscore_cbor_put_text(uint8_t **buffer, size_t *buf_size, const char *text, size_t text_len)
size_t oscore_cbor_put_nil(uint8_t **buffer, size_t *buf_size)
size_t oscore_cbor_put_false(uint8_t **buffer, size_t *buf_size)
size_t oscore_cbor_put_true(uint8_t **buffer, size_t *buf_size)
size_t oscore_cbor_put_unsigned(uint8_t **buffer, size_t *buf_size, uint64_t value)
size_t oscore_cbor_put_bytes(uint8_t **buffer, size_t *buf_size, const uint8_t *bytes, size_t bytes_len)
size_t oscore_cbor_put_array(uint8_t **buffer, size_t *buf_size, size_t elements)
const char * cose_get_hkdf_alg_name(cose_hkdf_alg_t id, char *buffer, size_t buflen)
size_t cose_nonce_len(cose_alg_t cose_alg)
size_t cose_key_len(cose_alg_t cose_alg)
const char * cose_get_alg_name(cose_alg_t id, char *buffer, size_t buflen)
const char * cose_get_curve_name(cose_curve_t id, char *buffer, size_t buflen)
void oscore_reference_recipient_ctx(oscore_recipient_ctx_t *recipient_ctx)
Increment the recipient context reference count.
#define OSCORE_ASSOCIATIONS_ITER_SAFE(e, el, rtmp)
void oscore_convert_to_hex(const uint8_t *src, size_t src_len, char *dest, size_t dst_len)
void coap_oscore_association_set_recipient_ctx(oscore_association_t *association, oscore_recipient_ctx_t *recipient_ctx)
Set the recipient context of an association.
oscore_ctx_t * oscore_derive_ctx_from_conf(coap_oscore_conf_t *oscore_conf)
oscore_derive_ctx_from_conf - derive a osc_ctx from oscore_conf information
int oscore_delete_association(coap_session_t *session, oscore_association_t *association)
void oscore_enter_context(coap_context_t *c_context, oscore_ctx_t *osc_ctx)
oscore_recipient_ctx_t * oscore_add_recipient(oscore_ctx_t *ctx, coap_oscore_rcp_conf_t *rcp_conf, uint32_t break_key)
oscore_add_recipient - add in recipient information
oscore_ctx_t * coap_init_oscore_context_from_conf(coap_oscore_conf_t *oscore_conf)
Initializes an OSCORE context from the given configuration.
int oscore_add_context(coap_context_t *c_context, oscore_ctx_t *osc_ctx)
void oscore_free_sender(oscore_sender_ctx_t *snd_ctx)
void coap_oscore_session_set_recipient_ctx(coap_session_t *session, oscore_recipient_ctx_t *recipient_ctx)
Attach the OSCORE recipient context information to the session.
coap_bin_const_t * oscore_build_key(oscore_ctx_t *osc_ctx, coap_bin_const_t *salt, coap_bin_const_t *ikm, cose_alg_t aead_alg, coap_bin_const_t *id, coap_str_const_t *type, size_t out_len)
int oscore_delete_recipient(oscore_ctx_t *osc_ctx, coap_bin_const_t *rid)
#define OSCORE_ASSOCIATIONS_DELETE(r, obj)
oscore_ctx_t * oscore_find_context_in_ram(const coap_context_t *c_context, const coap_bin_const_t rcpkey_id, const coap_bin_const_t *ctxkey_id, uint8_t *oscore_r2, oscore_recipient_ctx_t **recipient_ctx)
Locate an OSCORE security context in libcoap's in-RAM registry only.
void oscore_update_ctx(oscore_ctx_t *osc_ctx, coap_bin_const_t *id_context)
oscore_update_ctx - update a osc_ctx with a new id_context
void oscore_free_context(oscore_ctx_t *osc_ctx)
#define OSCORE_ASSOCIATIONS_ADD(r, obj)
oscore_ctx_t * oscore_derive_ctx(coap_context_t *c_context, coap_oscore_conf_t *oscore_conf)
oscore_derive_ctx - derive a osc_ctx from oscore_conf information
void oscore_free_association(oscore_association_t *association)
int oscore_hkdf(cose_hkdf_alg_t hkdf_alg, coap_bin_const_t *salt, coap_bin_const_t *ikm, uint8_t *info, size_t info_len, uint8_t *okm, size_t okm_len)
Derive the key using HKDF() function.
int oscore_new_association(coap_session_t *session, coap_pdu_t *sent_pdu, coap_bin_const_t *token, oscore_recipient_ctx_t *recipient_ctx, coap_bin_const_t *aad, coap_bin_const_t *nonce, coap_bin_const_t *partial_iv, int is_observe)
void oscore_delete_server_associations(coap_session_t *session)
void oscore_log_char_value(coap_log_t level, const char *name, const char *value)
oscore_ctx_t * oscore_find_context(coap_session_t *session, const coap_bin_const_t rcpkey_id, const coap_bin_const_t *ctxkey_id, uint8_t *oscore_r2, oscore_recipient_ctx_t **recipient_ctx)
oscore_find_context - Locate recipient context (and hence OSCORE context)
uint8_t * oscore_cs_key_params(cose_curve_t param, int8_t param_type, size_t *len)
void oscore_free_contexts(coap_context_t *c_context)
void oscore_log_hex_value(coap_log_t level, const char *name, coap_bin_const_t *value)
void oscore_log_int_value(coap_log_t level, const char *name, int value)
int oscore_is_context_attached(const oscore_ctx_t *osc_ctx)
Check if oscore context is attached to a the provided context.
oscore_ctx_t * oscore_duplicate_ctx(coap_context_t *c_context, oscore_ctx_t *o_osc_ctx, coap_bin_const_t *sender_id, coap_bin_const_t *recipient_id, coap_bin_const_t *id_context)
oscore_duplicate_ctx - duplicate a osc_ctx
int oscore_remove_context(coap_context_t *c_context, oscore_ctx_t *osc_ctx)
int oscore_derive_keystream(oscore_ctx_t *osc_ctx, cose_encrypt0_t *code, uint8_t coap_request, coap_bin_const_t *sender_key, coap_bin_const_t *id_context, size_t cs_size, uint8_t *keystream, size_t keystream_size)
oscore_association_t * oscore_find_association(coap_session_t *session, coap_bin_const_t *token)
int coap_delete_oscore_rcp_conf(coap_oscore_rcp_conf_t *oscore_rcp_conf)
Release all the information associated with the OSCORE complex Recipient configuration.
#define OSCORE_ASSOCIATIONS_FIND(r, k, res)
uint8_t oscore_bytes_equal(uint8_t *a_ptr, uint8_t a_len, uint8_t *b_ptr, uint8_t b_len)
void oscore_release_recipient_ctx(oscore_recipient_ctx_t **recipient_ctx)
Cleanup recipient context, including releasing the oscore context if the oscore context referenced is...
@ OSCORE_MODE_SINGLE
Vanilla RFC 8613 support.
@ OSCORE_MODE_PAIRWISE
RFC 10021 support.
@ OSCORE_MODE_GROUP
RFC 10021 support.
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
coap_pdu_t * coap_pdu_duplicate_lkd(const coap_pdu_t *old_pdu, coap_session_t *session, size_t token_length, const uint8_t *token, coap_opt_filter_t *drop_options, coap_bool_t expand_opt_abb)
Duplicate an existing PDU.
int coap_get_data(const coap_pdu_t *pdu, size_t *len, const uint8_t **data)
Retrieves the length and data pointer of specified PDU.
int coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds given data to the pdu that is passed as first parameter.
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
coap_str_const_t * coap_make_str_const(const char *string)
Take the specified byte array (text) and create a coap_str_const_t *.
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
int coap_oscore_group_is_supported(void)
Check whether OSCORE Group support is available.
int coap_oscore_pairwise_is_supported(void)
Check whether OSCORE Pairwise support is available.
CoAP binary data definition with const data.
size_t length
length of binary data
const uint8_t * s
read-only binary data
CoAP binary data definition.
The CoAP stack's global state is stored in a coap_context_t object.
The structure used to hold the OSCORE configuration information.
void * save_seq_num_func_param
Passed to save_seq_num_func().
uint32_t rfc8613_b_2
1 if rfc8613 B.2 protocol else 0
cose_hkdf_alg_t hkdf_alg
Set to one of COSE_HKDF_ALG_*.
uint32_t break_sender_key
1 if sender key to be broken, else 0
coap_oscore_snd_conf_t * sender
The sender - i.e.
coap_oscore_rcp_conf_t * recipient_chain
The recipients as a chain.
uint32_t ssn_freq
Sender Seq Num update frequency.
coap_oscore_save_seq_num_t save_seq_num_func
Called every seq num change.
uint32_t rfc8613_b_1_2
1 if rfc8613 B.1.2 enabled else 0
uint64_t start_seq_num
Used for ssn_freq updating.
uint32_t break_recipient_key
1 if recipient key to be broken, else 0
coap_bin_const_t * master_secret
Common Master Secret.
cose_alg_t aead_alg
Set to one of COSE_ALGORITHM_AES*.
coap_bin_const_t * master_salt
Common Master Salt.
uint32_t replay_window
Replay window size Use COAP_OSCORE_DEFAULT_REPLAY_WINDOW.
coap_bin_const_t * id_context
Common ID context.
The structure used to hold the OSCORE Recipient configuration.
int silent_server
1 if server is likely to be silent else 0
coap_bin_const_t * recipient_id
Recipient ID (i.e.
uint64_t last_seq
Highest sequence number used for this recipient.
uint8_t window_initialized
Contains if the sliding window is initialized 1 if initialized, 0 otherwise.
struct coap_oscore_rcp_conf_t * next_recipient
Used to maintain the chain.
uint64_t sliding_window
bitfield sequence counter window
coap_bin_const_t * sender_id
Sender ID (i.e.
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_context_t * context
session's context
CoAP string data definition with const data.
const uint8_t * s
read-only string data
size_t length
length of string
coap_bin_const_t partial_iv
coap_bin_const_t * obs_partial_iv
coap_bin_const_t * partial_iv
oscore_recipient_ctx_t * recipient_ctx
coap_bin_const_t * master_secret
uint32_t replay_window_size
coap_bin_const_t * common_iv
Derived from Master Secret, Master Salt, and ID Context.
struct oscore_ctx_t * next
uint8_t rfc8613_b_1_2
1 if rfc8613 B.1.2 enabled else 0
void * save_seq_num_func_param
Passed to save_seq_num_func().
oscore_sender_ctx_t * sender_context
cose_hkdf_alg_t hkdf_alg
Set to one of COSE_HKDF_ALG_*.
cose_alg_t aead_alg
Set to one of COSE_ALGORITHM_AES*.
uint8_t rfc8613_b_2
1 if rfc8613 B.2 protocol else 0
coap_bin_const_t * master_salt
coap_oscore_save_seq_num_t save_seq_num_func
Called every seq num change.
oscore_recipient_ctx_t * recipient_chain
coap_bin_const_t * id_context
contains GID in case of group
uint32_t ssn_freq
Sender Seq Num update frequency.
coap_bin_const_t * pw_sender_key
struct oscore_pw_chain_t * next
oscore_recipient_ctx_t * next_recipient
This field allows recipient chaining.
unsigned ref
Reference counter to keep track of linked associations / active sessions.
coap_bin_const_t * recipient_key
coap_bin_const_t * recipient_id
coap_bin_const_t * sender_id
uint64_t seq
Sender Sequence Number.
coap_bin_const_t * sender_key
uint64_t next_seq
Used for ssn_freq updating.