libcoap 4.3.2rc1
Loading...
Searching...
No Matches
oscore_context.c
Go to the documentation of this file.
1/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
3/*
4 * Copyright (c) 2018, SICS, RISE AB
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Institute nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 */
32
46#include "coap3/coap_internal.h"
47
48#include <stdio.h>
49
50static void oscore_enter_context(coap_context_t *c_context,
51 oscore_ctx_t *osc_ctx);
52
53static size_t
54compose_info(uint8_t *buffer,
55 size_t buf_size,
56 uint8_t alg,
58 coap_bin_const_t *id_context,
59 coap_str_const_t *type,
60 size_t out_len) {
61 size_t ret = 0;
62 size_t rem_size = buf_size;
63
64 ret += oscore_cbor_put_array(&buffer, &rem_size, 5);
65 ret += oscore_cbor_put_bytes(&buffer,
66 &rem_size,
67 id ? id->s : NULL,
68 id ? id->length : 0);
69 if (id_context != NULL && id_context->length > 0) {
70 ret += oscore_cbor_put_bytes(&buffer,
71 &rem_size,
72 id_context->s,
73 id_context->length);
74 } else {
75 ret += oscore_cbor_put_nil(&buffer, &rem_size);
76 }
77 ret += oscore_cbor_put_unsigned(&buffer, &rem_size, alg);
78 ret += oscore_cbor_put_text(&buffer,
79 &rem_size,
80 (const char *)type->s,
81 type->length);
82 ret += oscore_cbor_put_unsigned(&buffer, &rem_size, out_len);
83 return ret;
84}
85
86uint8_t
87oscore_bytes_equal(uint8_t *a_ptr,
88 uint8_t a_len,
89 uint8_t *b_ptr,
90 uint8_t b_len) {
91 if (a_len != b_len) {
92 return 0;
93 }
94
95 if (memcmp(a_ptr, b_ptr, a_len) == 0) {
96 return 1;
97 } else {
98 return 0;
99 }
100}
101
102static void
104 if (c_context->p_osc_ctx) {
105 oscore_ctx_t *prev = c_context->p_osc_ctx;
106 oscore_ctx_t *next = c_context->p_osc_ctx->next;
107
108 while (next) {
109 prev = next;
110 next = next->next;
111 }
112 prev->next = osc_ctx;
113 } else
114 c_context->p_osc_ctx = osc_ctx;
115}
116
117static void
123
124void
126 if (osc_ctx == NULL)
127 return;
128 if (osc_ctx->sender_context) {
132 }
133
134 while (osc_ctx->recipient_chain) {
136
138 osc_ctx->recipient_chain = next;
139 }
140
146}
147
148void
150 while (c_context->p_osc_ctx) {
151 oscore_ctx_t *osc_ctx = c_context->p_osc_ctx;
152
153 c_context->p_osc_ctx = osc_ctx->next;
154
155 oscore_free_context(osc_ctx);
156 }
157}
158
159int
161 oscore_ctx_t *prev = NULL;
162 oscore_ctx_t *next = c_context->p_osc_ctx;
163 while (next) {
164 if (next == osc_ctx) {
165 if (prev != NULL)
166 prev->next = next->next;
167 else
168 c_context->p_osc_ctx = next->next;
170 return 1;
171 }
172 next = next->next;
173 }
174 return 0;
175}
176
177/*
178 * oscore_find_context
179 * Finds OSCORE context for rcpkey_id and optional ctxkey_id
180 * rcpkey_id can be 0 length.
181 * Updates recipient_ctx.
182 */
185 const coap_bin_const_t rcpkey_id,
186 const coap_bin_const_t *ctxkey_id,
187 uint8_t *oscore_r2,
188 oscore_recipient_ctx_t **recipient_ctx) {
189 oscore_ctx_t *pt = c_context->p_osc_ctx;
190
191 *recipient_ctx = NULL;
192 assert(rcpkey_id.length == 0 || rcpkey_id.s != NULL);
193 while (pt != NULL) {
194 int ok = 0;
196
197 while (rpt) {
198 ok = 0;
199 if (rcpkey_id.length == rpt->recipient_id->length) {
200 if (rcpkey_id.length != 0)
201 ok = memcmp(rpt->recipient_id->s, rcpkey_id.s, rcpkey_id.length) != 0;
202 if (oscore_r2) {
203 if (pt->id_context != NULL && pt->id_context->length > 8) {
204 ok = ok + (memcmp(pt->id_context->s, oscore_r2, 8) != 0);
205 } else {
206 ok += 1;
207 }
208 } else if (ctxkey_id) {
209 if (pt->id_context != NULL) {
210 if (ctxkey_id->length != pt->id_context->length)
211 ok += 1;
212 else
213 ok = ok + (memcmp(pt->id_context->s,
214 ctxkey_id->s,
215 ctxkey_id->length) != 0);
216 } else if (ctxkey_id->length > 0)
217 ok += 1;
218 }
219 if (ok == 0) {
220 /* optional id context and recipient id are the same */
221 *recipient_ctx = rpt;
222 return pt; /* OSCORE context found */
223 }
224 }
225 rpt = rpt->next_recipient;
226 } /* while rpt */
227 pt = pt->next;
228 } /* end while */
229 return NULL;
230}
231
232#define OSCORE_LOG_SIZE 16
233void
235 const char *name,
236 coap_bin_const_t *value) {
237 size_t i;
238
239 if (value == NULL) {
240 coap_log(level, " %-16s\n", name);
241 return;
242 }
243 if (value->length == 0) {
244 coap_log(level, " %-16s <>\n", name);
245 return;
246 }
247 if (coap_get_log_level() >= level) {
248 for (i = 0; i < value->length; i += OSCORE_LOG_SIZE) {
249 char number[3 * OSCORE_LOG_SIZE + 4];
250
251 oscore_convert_to_hex(&value->s[i],
252 value->length - i > OSCORE_LOG_SIZE ?
253 OSCORE_LOG_SIZE : value->length - i,
254 number,
255 sizeof(number));
256 coap_log(level, " %-16s %s\n", i == 0 ? name : "", number);
257 }
258 }
259}
260
261void
262oscore_log_int_value(coap_log_t level, const char *name, int value) {
263 coap_log(level, " %-16s %2d\n", name, value);
264}
265
266void
267oscore_log_char_value(coap_log_t level, const char *name, const char *value) {
268 coap_log(level, " %-16s %s\n", name, value);
269}
270
271void
272oscore_convert_to_hex(const uint8_t *src,
273 size_t src_len,
274 char *dest,
275 size_t dst_len) {
276 /*
277 * Last output character will be '\000'
278 * (If output undersized, add trailing ... to indicate this.
279 */
280 size_t space = (dst_len - 4) / 3;
281 uint32_t qq;
282
283 for (qq = 0; qq < src_len && qq < space; qq++) {
284 char tmp = src[qq] >> 4;
285 if (tmp > 9)
286 tmp = tmp + 0x61 - 10;
287 else
288 tmp = tmp + 0x30;
289 dest[qq * 3] = tmp;
290 tmp = src[qq] & 0xf;
291 if (tmp > 9)
292 tmp = tmp + 0x61 - 10;
293 else
294 tmp = tmp + 0x30;
295 dest[qq * 3 + 1] = tmp;
296 dest[qq * 3 + 2] = 0x20;
297 }
298 if (qq != src_len) {
299 dest[qq * 3] = '.';
300 dest[qq * 3 + 1] = '.';
301 dest[qq * 3 + 2] = '.';
302 qq++;
303 }
304 dest[qq * 3] = 0;
305}
306
307static coap_bin_const_t *
310 coap_str_const_t *type,
311 size_t out_len) {
312 uint8_t info_buffer[60];
313 size_t info_len;
314 uint8_t hkdf_tmp[CONTEXT_KEY_LEN > CONTEXT_INIT_VECT_LEN ?
317
318 info_len = compose_info(info_buffer,
319 sizeof(info_buffer),
320 osc_ctx->aead_alg,
321 id,
322 osc_ctx->id_context,
323 type,
324 out_len);
325 if (info_len == 0)
326 return NULL;
327
328 if (!oscore_hkdf(osc_ctx->hkdf_alg,
329 osc_ctx->master_salt,
330 osc_ctx->master_secret,
331 info_buffer,
332 info_len,
333 hkdf_tmp,
334 out_len))
335 return NULL;
336 return coap_new_bin_const(hkdf_tmp, out_len);
337}
338
339static void
340oscore_log_context(oscore_ctx_t *osc_ctx, const char *heading) {
341#if COAP_MAX_LOGGING_LEVEL < _COAP_LOG_OSCORE
342 (void)osc_ctx;
343 (void)heading;
344#else /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_OSCORE */
346 char buffer[30];
348 size_t count = 0;
349
350 coap_log_oscore("%s\n", heading);
352 cose_get_alg_name(osc_ctx->aead_alg, buffer,
353 sizeof(buffer)));
355 cose_get_hkdf_alg_name(osc_ctx->hkdf_alg, buffer,
356 sizeof(buffer)));
357 oscore_log_hex_value(COAP_LOG_OSCORE, "ID Context", osc_ctx->id_context);
359 "Master Secret",
360 osc_ctx->master_secret);
361 oscore_log_hex_value(COAP_LOG_OSCORE, "Master Salt", osc_ctx->master_salt);
362 oscore_log_hex_value(COAP_LOG_OSCORE, "Common IV", osc_ctx->common_iv);
364 "Sender ID",
365 osc_ctx->sender_context->sender_id);
367 "Sender Key",
368 osc_ctx->sender_context->sender_key);
369 while (next) {
370 snprintf(buffer, sizeof(buffer), "Recipient ID[%zu]", count);
372 buffer,
373 next->recipient_id);
374 snprintf(buffer, sizeof(buffer), "Recipient Key[%zu]", count);
376 buffer,
377 next->recipient_key);
378 count++;
379 next = next->next_recipient;
380 }
381 }
382#endif /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_OSCORE */
383}
384
385void
387 coap_bin_const_t *temp;
388
389 /* Update with new ID Context */
391 osc_ctx->id_context = id_context;
392
393 /* Update sender_key, recipient_key and common_iv */
394 temp = osc_ctx->sender_context->sender_key;
395 osc_ctx->sender_context->sender_key =
396 oscore_build_key(osc_ctx,
397 osc_ctx->sender_context->sender_id,
398 coap_make_str_const("Key"),
400 if (!osc_ctx->sender_context->sender_key)
401 osc_ctx->sender_context->sender_key = temp;
402 else
404 temp = osc_ctx->recipient_chain->recipient_key;
406 oscore_build_key(osc_ctx,
408 coap_make_str_const("Key"),
410 if (!osc_ctx->recipient_chain->recipient_key)
411 osc_ctx->recipient_chain->recipient_key = temp;
412 else
414 temp = osc_ctx->common_iv;
415 osc_ctx->common_iv = oscore_build_key(osc_ctx,
416 NULL,
419 if (!osc_ctx->common_iv)
420 osc_ctx->common_iv = temp;
421 else
423
424 oscore_log_context(osc_ctx, "Updated Common context");
425}
426
429 oscore_ctx_t *o_osc_ctx,
430 coap_bin_const_t *sender_id,
431 coap_bin_const_t *recipient_id,
432 coap_bin_const_t *id_context) {
433 oscore_ctx_t *osc_ctx = NULL;
434 oscore_sender_ctx_t *sender_ctx = NULL;
435 coap_bin_const_t *copy_rid = NULL;
436
438 if (osc_ctx == NULL)
439 goto error;
440 memset(osc_ctx, 0, sizeof(oscore_ctx_t));
441
443 if (sender_ctx == NULL)
444 goto error;
445 memset(sender_ctx, 0, sizeof(oscore_sender_ctx_t));
446
447 osc_ctx->sender_context = sender_ctx;
448 if (o_osc_ctx->master_secret)
449 osc_ctx->master_secret =
451 o_osc_ctx->master_secret->length);
452 if (o_osc_ctx->master_salt)
453 osc_ctx->master_salt = coap_new_bin_const(o_osc_ctx->master_salt->s,
454 o_osc_ctx->master_salt->length);
455 osc_ctx->aead_alg = o_osc_ctx->aead_alg;
456 osc_ctx->hkdf_alg = o_osc_ctx->hkdf_alg;
457 if (id_context)
458 osc_ctx->id_context = coap_new_bin_const(id_context->s, id_context->length);
459 osc_ctx->ssn_freq = o_osc_ctx->ssn_freq;
460 osc_ctx->replay_window_size = o_osc_ctx->replay_window_size;
461 osc_ctx->rfc8613_b_1_2 = o_osc_ctx->rfc8613_b_1_2;
462 osc_ctx->rfc8613_b_2 = o_osc_ctx->rfc8613_b_2;
463 osc_ctx->save_seq_num_func = o_osc_ctx->save_seq_num_func;
465
466 if (o_osc_ctx->master_secret) {
467 /* sender_ key */
468 sender_ctx->sender_key = oscore_build_key(osc_ctx,
469 sender_id,
470 coap_make_str_const("Key"),
472 if (!sender_ctx->sender_key)
473 goto error;
474
475 /* common IV */
476 osc_ctx->common_iv = oscore_build_key(osc_ctx,
477 NULL,
480 if (!osc_ctx->common_iv)
481 goto error;
482 }
483
484 /*
485 * Need to set the last Sender Seq Num based on ssn_freq
486 * The value should only change if there is a change to ssn_freq
487 * and (potentially) be lower than seq, then save_seq_num_func() is
488 * immediately called on next SSN update.
489 */
490 sender_ctx->next_seq = 0;
491 sender_ctx->seq = 0;
492
493 sender_ctx->sender_id = coap_new_bin_const(sender_id->s, sender_id->length);
494
495 copy_rid = coap_new_bin_const(recipient_id->s, recipient_id->length);
496 if (copy_rid == NULL)
497 goto error;
498 if (oscore_add_recipient(osc_ctx, copy_rid, 0) == NULL)
499 goto error;
500
501 oscore_log_context(osc_ctx, "New Common context");
502 oscore_enter_context(c_context, osc_ctx);
503
504 return osc_ctx;
505
506error:
507 oscore_free_context(osc_ctx);
508 return NULL;
509}
510
513 oscore_ctx_t *osc_ctx = NULL;
514 oscore_sender_ctx_t *sender_ctx = NULL;
515 size_t i;
516
518 if (osc_ctx == NULL)
519 goto error;
520 memset(osc_ctx, 0, sizeof(oscore_ctx_t));
521
523 if (sender_ctx == NULL)
524 goto error;
525 memset(sender_ctx, 0, sizeof(oscore_sender_ctx_t));
526
527 osc_ctx->sender_context = sender_ctx;
528 osc_ctx->master_secret = oscore_conf->master_secret;
529 osc_ctx->master_salt = oscore_conf->master_salt;
530 osc_ctx->aead_alg = oscore_conf->aead_alg;
531 osc_ctx->hkdf_alg = oscore_conf->hkdf_alg;
532 osc_ctx->id_context = oscore_conf->id_context;
533 osc_ctx->ssn_freq = oscore_conf->ssn_freq ? oscore_conf->ssn_freq : 1;
534 osc_ctx->replay_window_size = oscore_conf->replay_window ?
535 oscore_conf->replay_window :
537 osc_ctx->rfc8613_b_1_2 = oscore_conf->rfc8613_b_1_2;
538 osc_ctx->rfc8613_b_2 = oscore_conf->rfc8613_b_2;
539 osc_ctx->save_seq_num_func = oscore_conf->save_seq_num_func;
540 osc_ctx->save_seq_num_func_param = oscore_conf->save_seq_num_func_param;
541
542 if (oscore_conf->master_secret) {
543 /* sender_ key */
544 if (oscore_conf->break_sender_key)
545 /* Interop testing */
546 sender_ctx->sender_key = oscore_build_key(osc_ctx,
547 oscore_conf->sender_id,
548 coap_make_str_const("BAD"),
550 else
551 sender_ctx->sender_key = oscore_build_key(osc_ctx,
552 oscore_conf->sender_id,
553 coap_make_str_const("Key"),
555 if (!sender_ctx->sender_key)
556 goto error;
557
558 /* common IV */
559 osc_ctx->common_iv = oscore_build_key(osc_ctx,
560 NULL,
563 if (!osc_ctx->common_iv)
564 goto error;
565 }
566
567 /*
568 * Need to set the last Sender Seq Num based on ssn_freq
569 * The value should only change if there is a change to ssn_freq
570 * and (potentially) be lower than seq, then save_seq_num_func() is
571 * immediately called on next SSN update.
572 */
573 sender_ctx->next_seq = oscore_conf->start_seq_num -
574 (oscore_conf->start_seq_num % oscore_conf->ssn_freq);
575
576 sender_ctx->sender_id = oscore_conf->sender_id;
577 sender_ctx->seq = oscore_conf->start_seq_num;
578
579 for (i = 0; i < oscore_conf->recipient_id_count; i++) {
580 if (oscore_add_recipient(osc_ctx, oscore_conf->recipient_id[i],
581 oscore_conf->break_recipient_key) == NULL) {
582 coap_log_warn("OSCORE: Failed to add Client ID\n");
583 goto error;
584 }
585 }
586 oscore_log_context(osc_ctx, "Common context");
587
588 oscore_enter_context(c_context, osc_ctx);
589
590 return osc_ctx;
591
592error:
594 coap_free_type(COAP_OSCORE_SEN, sender_ctx);
595 return NULL;
596}
597
600 uint32_t break_key) {
601 oscore_recipient_ctx_t *rcp_ctx = osc_ctx->recipient_chain;
602 oscore_recipient_ctx_t *recipient_ctx = NULL;
603
604 if (rid->length > 7) {
605 coap_log_warn("oscore_add_recipient: Maximum size of recipient_id is 7 bytes\n");
606 return NULL;
607 }
608 /* Check this is not a duplicate recipient id */
609 while (rcp_ctx) {
610 if (rcp_ctx->recipient_id->length == rid->length &&
611 memcmp(rcp_ctx->recipient_id->s, rid->s, rid->length) == 0) {
613 return NULL;
614 }
615 rcp_ctx = rcp_ctx->next_recipient;
616 }
617 recipient_ctx = (oscore_recipient_ctx_t *)coap_malloc_type(
619 sizeof(oscore_recipient_ctx_t));
620 if (recipient_ctx == NULL)
621 return NULL;
622 memset(recipient_ctx, 0, sizeof(oscore_recipient_ctx_t));
623
624 if (osc_ctx->master_secret) {
625 if (break_key)
626 /* Interop testing */
627 recipient_ctx->recipient_key = oscore_build_key(osc_ctx,
628 rid,
629 coap_make_str_const("BAD"),
631 else
632 recipient_ctx->recipient_key = oscore_build_key(osc_ctx,
633 rid,
634 coap_make_str_const("Key"),
636 if (!recipient_ctx->recipient_key) {
637 coap_free_type(COAP_OSCORE_REC, recipient_ctx);
638 return NULL;
639 }
640 }
641
642 recipient_ctx->recipient_id = rid;
643 recipient_ctx->initial_state = 1;
644 recipient_ctx->osc_ctx = osc_ctx;
645
646 rcp_ctx = osc_ctx->recipient_chain;
647 recipient_ctx->next_recipient = rcp_ctx;
648 osc_ctx->recipient_chain = recipient_ctx;
649 return recipient_ctx;
650}
651
652int
654 oscore_recipient_ctx_t *prev = NULL;
656 while (next) {
657 if (next->recipient_id->length == rid->length &&
658 memcmp(next->recipient_id->s, rid->s, rid->length) == 0) {
659 if (prev != NULL)
660 prev->next_recipient = next->next_recipient;
661 else
662 osc_ctx->recipient_chain = next->next_recipient;
664 return 1;
665 }
666 next = next->next_recipient;
667 }
668 return 0;
669}
670
671void
673 if (association) {
674 coap_delete_pdu(association->sent_pdu);
675 coap_delete_bin_const(association->token);
676 coap_delete_bin_const(association->aad);
677 coap_delete_bin_const(association->nonce);
678 coap_delete_bin_const(association->partial_iv);
679 coap_free_type(COAP_STRING, association);
680 }
681}
682
683int
685 coap_pdu_t *sent_pdu,
686 coap_bin_const_t *token,
687 oscore_recipient_ctx_t *recipient_ctx,
688 coap_bin_const_t *aad,
689 coap_bin_const_t *nonce,
690 coap_bin_const_t *partial_iv,
691 int is_observe) {
692 oscore_association_t *association;
693
694 association = coap_malloc_type(COAP_STRING, sizeof(oscore_association_t));
695 if (association == NULL)
696 return 0;
697
698 memset(association, 0, sizeof(oscore_association_t));
699 association->recipient_ctx = recipient_ctx;
700 association->is_observe = is_observe;
701
702 if (sent_pdu) {
703 size_t size;
704 const uint8_t *data;
705
706 association->sent_pdu = coap_pdu_duplicate(sent_pdu, session,
707 token->length, token->s, NULL);
708 if (association->sent_pdu == NULL)
709 goto error;
710 if (coap_get_data(sent_pdu, &size, &data)) {
711 coap_add_data(association->sent_pdu, size, data);
712 }
713 }
714 association->token = coap_new_bin_const(token->s, token->length);
715 if (association->token == NULL)
716 goto error;
717
718 if (aad) {
719 association->aad = coap_new_bin_const(aad->s, aad->length);
720 if (association->aad == NULL)
721 goto error;
722 }
723
724 if (nonce) {
725 association->nonce = coap_new_bin_const(nonce->s, nonce->length);
726 if (association->nonce == NULL)
727 goto error;
728 }
729
730 if (partial_iv) {
731 association->partial_iv =
732 coap_new_bin_const(partial_iv->s, partial_iv->length);
733 if (association->partial_iv == NULL)
734 goto error;
735 }
736
737 OSCORE_ASSOCIATIONS_ADD(session->associations, association);
738 return 1;
739
740error:
741 oscore_free_association(association);
742 return 0;
743}
744
747 oscore_association_t *association;
748
749 OSCORE_ASSOCIATIONS_FIND(session->associations, token, association);
750 return association;
751}
752
753int
755 oscore_association_t *association) {
756 if (association) {
757 OSCORE_ASSOCIATIONS_DELETE(session->associations, association);
758 oscore_free_association(association);
759 return 1;
760 }
761 return 0;
762}
763
764void
766 if (session) {
767 oscore_association_t *association;
769
770 OSCORE_ASSOCIATIONS_ITER_SAFE(session->associations, association, tmp) {
771 OSCORE_ASSOCIATIONS_DELETE(session->associations, association);
772 oscore_free_association(association);
773 }
774 session->associations = NULL;
775 }
776}
Pulls together all the internal only header files.
@ COAP_OSCORE_COM
Definition coap_mem.h:60
@ COAP_OSCORE_SEN
Definition coap_mem.h:61
@ COAP_OSCORE_REC
Definition coap_mem.h:62
@ COAP_STRING
Definition coap_mem.h:38
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
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition coap_debug.c:91
#define coap_log_oscore(...)
Definition coap_debug.h:126
coap_log_t
Logging type.
Definition coap_debug.h:50
#define coap_log_warn(...)
Definition coap_debug.h:102
#define coap_log(level,...)
Logging function.
Definition coap_debug.h:277
@ COAP_LOG_OSCORE
Definition coap_debug.h:59
size_t oscore_cbor_put_text(uint8_t **buffer, size_t *buf_size, const char *text, size_t text_len)
Definition oscore_cbor.c:77
size_t oscore_cbor_put_nil(uint8_t **buffer, size_t *buf_size)
Definition oscore_cbor.c:59
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)
Definition oscore_cbor.c:92
const char * cose_get_hkdf_alg_name(cose_hkdf_alg_t id, char *buffer, size_t buflen)
const char * cose_get_alg_name(cose_alg_t id, char *buffer, size_t buflen)
#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)
int oscore_delete_association(coap_session_t *session, oscore_association_t *association)
oscore_recipient_ctx_t * oscore_add_recipient(oscore_ctx_t *osc_ctx, coap_bin_const_t *rid, uint32_t break_key)
oscore_add_recipient - add in recipient information
int oscore_delete_recipient(oscore_ctx_t *osc_ctx, coap_bin_const_t *rid)
#define OSCORE_ASSOCIATIONS_DELETE(r, obj)
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
#define CONTEXT_KEY_LEN
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)
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)
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)
oscore_association_t * oscore_find_association(coap_session_t *session, coap_bin_const_t *token)
oscore_ctx_t * oscore_find_context(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)
oscore_find_context - Locate recipient context (and hence OSCORE context)
#define OSCORE_ASSOCIATIONS_FIND(r, k, res)
#define CONTEXT_INIT_VECT_LEN
uint8_t oscore_bytes_equal(uint8_t *a_ptr, uint8_t a_len, uint8_t *b_ptr, uint8_t b_len)
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition coap_pdu.c:163
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.
Definition coap_pdu.c:797
coap_pdu_t * coap_pdu_duplicate(const coap_pdu_t *old_pdu, coap_session_t *session, size_t token_length, const uint8_t *token, coap_opt_filter_t *drop_options)
Duplicate an existing PDU.
Definition coap_pdu.c:179
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.
Definition coap_pdu.c:766
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
Definition coap_str.c:120
coap_str_const_t * coap_make_str_const(const char *string)
Take the specified byte array (text) and create a coap_str_const_t *.
Definition coap_str.c:66
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...
Definition coap_str.c:110
#define OSCORE_LOG_SIZE
static void oscore_log_context(oscore_ctx_t *osc_ctx, const char *heading)
static void oscore_free_recipient(oscore_recipient_ctx_t *recipient)
static coap_bin_const_t * oscore_build_key(oscore_ctx_t *osc_ctx, coap_bin_const_t *id, coap_str_const_t *type, size_t out_len)
static void oscore_enter_context(coap_context_t *c_context, oscore_ctx_t *osc_ctx)
static size_t compose_info(uint8_t *buffer, size_t buf_size, uint8_t alg, coap_bin_const_t *id, coap_bin_const_t *id_context, coap_str_const_t *type, size_t out_len)
CoAP binary data definition with const data.
Definition coap_str.h:64
size_t length
length of binary data
Definition coap_str.h:65
const uint8_t * s
read-only binary data
Definition coap_str.h:66
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
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.
coap_bin_const_t * sender_id
Sender ID (i.e.
coap_bin_const_t ** recipient_id
Recipient ID (i.e.
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.
uint32_t recipient_id_count
Number of recipient_id entries.
structure for CoAP PDUs
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
CoAP string data definition with const data.
Definition coap_str.h:46
const uint8_t * s
read-only string data
Definition coap_str.h:48
size_t length
length of string
Definition coap_str.h:47
coap_bin_const_t * partial_iv
coap_bin_const_t * aad
coap_bin_const_t * nonce
oscore_recipient_ctx_t * recipient_ctx
coap_bin_const_t * token
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
cose_alg_t aead_alg
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.
oscore_recipient_ctx_t * next_recipient
coap_bin_const_t * recipient_key
coap_bin_const_t * recipient_id
coap_bin_const_t * sender_id
coap_bin_const_t * sender_key
uint64_t next_seq
Used for ssn_freq updating.