libcoap 4.3.5-develop-5347a62
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
45
47
48#if COAP_OSCORE_SUPPORT
49
50#include <stdio.h>
51
52/* Move ptr from b to a, and then clear b */
53#define OSC_MOVE_PTR(a,b) do { (a) = (b); (b) = NULL; } while(0)
54
55static int oscore_context_release_recipients(oscore_ctx_t *osc_ctx);
56
57static size_t
58compose_info(uint8_t *buffer,
59 size_t buf_size,
60 cose_alg_t alg,
62 coap_bin_const_t *id_context,
63 coap_str_const_t *type,
64 size_t out_len) {
65 size_t ret = 0;
66 size_t rem_size = buf_size;
67
68 ret += oscore_cbor_put_array(&buffer, &rem_size, 5);
69 ret += oscore_cbor_put_bytes(&buffer,
70 &rem_size,
71 id ? id->s : NULL,
72 id ? id->length : 0);
73 if (id_context != NULL && id_context->length > 0) {
74 ret += oscore_cbor_put_bytes(&buffer,
75 &rem_size,
76 id_context->s,
77 id_context->length);
78 } else {
79 ret += oscore_cbor_put_nil(&buffer, &rem_size);
80 }
81 ret += oscore_cbor_put_unsigned(&buffer, &rem_size, alg);
82 ret += oscore_cbor_put_text(&buffer,
83 &rem_size,
84 (const char *)type->s,
85 type->length);
86 ret += oscore_cbor_put_unsigned(&buffer, &rem_size, out_len);
87 return ret;
88}
89
90#if COAP_OSCORE_GROUP_SUPPORT
91int
93 cose_encrypt0_t *cose,
94 uint8_t coap_request,
95 coap_bin_const_t *sender_id,
96 coap_bin_const_t *id_context,
97 size_t cs_size,
98 uint8_t *keystream,
99 size_t keystream_size) {
100 uint8_t info_buffer[30];
101 uint8_t *buffer = info_buffer;
102 size_t info_len = 0;
103 size_t rem_size = sizeof(info_buffer);
104 ;
105
106 info_len += oscore_cbor_put_array(&buffer, &rem_size, 4);
107 /* 1. id */
108 info_len += oscore_cbor_put_bytes(&buffer,
109 &rem_size,
110 sender_id->s,
111 sender_id->length);
112 /* 2. id_context */
113 info_len += oscore_cbor_put_bytes(&buffer,
114 &rem_size,
115 id_context->s,
116 id_context->length);
117 /* 3. type */
118 if (coap_request)
119 info_len += oscore_cbor_put_true(&buffer, &rem_size);
120 else
121 info_len += oscore_cbor_put_false(&buffer, &rem_size);
122 /* 4. L */
123 info_len += oscore_cbor_put_unsigned(&buffer, &rem_size, cs_size);
124
125 oscore_hkdf(osc_ctx->hkdf_alg,
126 &cose->partial_iv,
127 osc_ctx->sign_enc_key,
128 info_buffer,
129 info_len,
130 keystream,
131 keystream_size);
132 return 1;
133}
134#endif /* COAP_OSCORE_GROUP_SUPPORT */
135
136uint8_t
137oscore_bytes_equal(uint8_t *a_ptr,
138 uint8_t a_len,
139 uint8_t *b_ptr,
140 uint8_t b_len) {
141 if (a_len != b_len) {
142 return 0;
143 }
144
145 if (memcmp(a_ptr, b_ptr, a_len) == 0) {
146 return 1;
147 } else {
148 return 0;
149 }
150}
151
152void
154 if (!osc_ctx)
155 return;
156 if (c_context->p_osc_ctx) {
157 oscore_ctx_t *head = c_context->p_osc_ctx;
158 oscore_ctx_t *prev = head;
159 oscore_ctx_t *next = head->next;
160
161 /* verify if oscore context is already attached */
162 if (head == osc_ctx) {
163 return;
164 }
165 while (next != head) {
166 if (next == osc_ctx) {
167 return;
168 }
169 prev = next;
170 next = next->next;
171 }
172 prev->next = osc_ctx;
173 osc_ctx->next = head;
174 } else {
175 c_context->p_osc_ctx = osc_ctx;
176 osc_ctx->next = osc_ctx;
177 }
178}
179
180static void
181oscore_free_recipient_ctx(oscore_recipient_ctx_t *rcp_ctx) {
182 if (rcp_ctx == NULL)
183 return;
184
185 assert(rcp_ctx->ref > 0);
186 if (--rcp_ctx->ref > 0) {
187 return;
188 }
189
190 /* remove recipient from oscore context chain if attached */
191 if (rcp_ctx->osc_ctx) {
192 if (rcp_ctx->osc_ctx->recipient_chain == rcp_ctx) {
193 rcp_ctx->osc_ctx->recipient_chain = rcp_ctx->next_recipient;
194 } else {
196
197 while (prev && prev->next_recipient != rcp_ctx) {
198 prev = prev->next_recipient;
199 }
200 if (prev) {
201 prev->next_recipient = rcp_ctx->next_recipient;
202 }
203 }
204 }
205
208#if COAP_OSCORE_GROUP_SUPPORT
209 coap_crypto_delete_public_cred(rcp_ctx->rem_cred);
210 coap_delete_bin_const(rcp_ctx->pw_rem_recipient_key);
211#endif /* COAP_OSCORE_GROUP_SUPPORT */
213}
214
215void
217#if COAP_OSCORE_GROUP_SUPPORT
218 oscore_pw_chain_t *next;
219 oscore_pw_chain_t *chain;
220#endif /* COAP_OSCORE_GROUP_SUPPORT */
221
222 if (snd_ctx == NULL)
223 return;
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;
230 while (chain) {
231 next = chain->next;
234 chain = next;
235 }
236#endif /* COAP_OSCORE_GROUP_SUPPORT */
238}
239
240void
242 if (osc_ctx == NULL)
243 return;
244
246
247 while (osc_ctx->recipient_chain) {
249
250 oscore_free_recipient_ctx(osc_ctx->recipient_chain);
251 osc_ctx->recipient_chain = next;
252 }
253
258#if COAP_OSCORE_GROUP_SUPPORT
259 coap_delete_bin_const(osc_ctx->sign_params);
260 coap_delete_bin_const(osc_ctx->sign_enc_key);
261 coap_crypto_delete_public_cred(osc_ctx->gm_cred);
262#endif /* COAP_OSCORE_GROUP_SUPPORT */
264}
265
266void
268 while (c_context->p_osc_ctx) {
269 oscore_ctx_t *osc_ctx = c_context->p_osc_ctx;
270
271 if (osc_ctx->next == osc_ctx) {
272 c_context->p_osc_ctx = NULL;
273 } else {
274 oscore_ctx_t *tail = osc_ctx;
275 c_context->p_osc_ctx = osc_ctx->next;
276 while (tail->next != osc_ctx) {
277 tail = tail->next;
278 }
279 tail->next = c_context->p_osc_ctx;
280 }
281 osc_ctx->next = NULL;
282
283 /*
284 * Verify if all recipients can be released, if not
285 * defer freeing of context until later when all recipients are released.
286 * The oscore context is flagged as ready to be freed by removing
287 * the oscore context from the coap context.
288 */
289 if (oscore_context_release_recipients(osc_ctx) > 0) {
290 oscore_free_context(osc_ctx);
291 }
292 }
293}
294
307static int
308oscore_context_release_recipients(oscore_ctx_t *osc_ctx) {
309 int ok = 1;
310
313
314 while (next != NULL) {
315 /* if reference is 1 or lower, ready to be freed */
316 if (next->ref <= 1) {
317 oscore_recipient_ctx_t *to_free = next;
318 next = next->next_recipient;
319 oscore_free_recipient_ctx(to_free);
320 continue;
321 }
322
323 /*
324 * Recipient is still attached to an oscore association or session,
325 * can not be freed yet. Decrease ref counter and keep in chain.
326 */
327 next->ref--;
328 ok = 0;
329
330 /* keep recipient in chain, since still referenced by other
331 session or association */
332 if (prev != NULL) {
333 prev->next_recipient = next;
334 } else {
335 osc_ctx->recipient_chain = next;
336 }
337 prev = next;
338 next = next->next_recipient;
339 }
340 return ok;
341}
342
343int
345 oscore_ctx_t *head = c_context->p_osc_ctx;
346 oscore_ctx_t *prev;
347 oscore_ctx_t *next;
348
349 if (head == NULL)
350 return 0;
351
352 prev = head;
353 next = head;
354 do {
355 if (next == osc_ctx) {
356 if (next->next == next) {
357 c_context->p_osc_ctx = NULL;
358 } else {
359 while (prev->next != next) {
360 prev = prev->next;
361 }
362 prev->next = next->next;
363 if (next == c_context->p_osc_ctx)
364 c_context->p_osc_ctx = next->next;
365 }
366 next->next = NULL;
367 /*
368 * Only free context if no recipient is still referenced by
369 * an association or session. Otherwise defer context to be
370 * freed after all references are resolved.
371 */
372 if (oscore_context_release_recipients(osc_ctx) <= 0) {
373 return 2;
374 }
375
376 /* all related recipient ctx attachments are released */
377 osc_ctx->recipient_chain = NULL;
378 oscore_free_context(osc_ctx);
379 return 1;
380 }
381 next = next->next;
382 } while (next != head);
383 return 0;
384}
385
388 const coap_bin_const_t rcpkey_id,
389 const coap_bin_const_t *ctxkey_id,
390 uint8_t *oscore_r2,
391 oscore_recipient_ctx_t **recipient_ctx) {
392 oscore_ctx_t *pt = c_context->p_osc_ctx;
393
394 *recipient_ctx = NULL;
395 assert(rcpkey_id.length == 0 || rcpkey_id.s != NULL);
396 if (pt == NULL)
397 return NULL;
398 do {
399 int ok = 0;
401
402 while (rpt) {
403 ok = 0;
404 if (rpt->recipient_id && rcpkey_id.length == rpt->recipient_id->length) {
405 if (rcpkey_id.length != 0)
406 ok = memcmp(rpt->recipient_id->s, rcpkey_id.s, rcpkey_id.length) != 0;
407 if (oscore_r2) {
408 if (pt->id_context != NULL && pt->id_context->length > 8) {
409 ok = ok + (memcmp(pt->id_context->s, oscore_r2, 8) != 0);
410 } else {
411 ok += 1;
412 }
413 } else if (ctxkey_id) {
414 if (pt->id_context != NULL) {
415 if (ctxkey_id->length != pt->id_context->length)
416 ok += 1;
417 else
418 ok = ok + (memcmp(pt->id_context->s,
419 ctxkey_id->s,
420 ctxkey_id->length) != 0);
421 } else if (ctxkey_id->length > 0)
422 ok += 1;
423 }
424 if (ok == 0) {
425 *recipient_ctx = rpt;
426 return pt;
427 }
428 }
429 rpt = rpt->next_recipient;
430 }
431 pt = pt->next;
432 } while (pt != c_context->p_osc_ctx);
433 return NULL;
434}
435
436/*
437 * oscore_find_context
438 * Finds OSCORE context for rcpkey_id and optional ctxkey_id
439 * rcpkey_id can be 0 length.
440 * Updates recipient_ctx.
441 */
444 const coap_bin_const_t rcpkey_id,
445 const coap_bin_const_t *ctxkey_id,
446 uint8_t *oscore_r2,
447 oscore_recipient_ctx_t **recipient_ctx) {
448 oscore_ctx_t *ram_ctx;
449
450 if (session->recipient_ctx) {
451 oscore_ctx_t *tmp_ctx = session->recipient_ctx->osc_ctx;
452
453 if (!tmp_ctx) {
454 return NULL;
455 }
456 if (coap_binary_equal(session->recipient_ctx->recipient_id, &rcpkey_id)) {
457 if (oscore_r2) {
458 if (tmp_ctx->id_context != NULL &&
459 tmp_ctx->id_context->length > 8) {
460 if (memcmp(tmp_ctx->id_context->s, oscore_r2, 8) == 0) {
461 goto match;
462 }
463 }
464 } else if (ctxkey_id) {
465 if (session->recipient_ctx->osc_ctx->id_context != NULL) {
466 if (!coap_binary_equal(tmp_ctx->id_context, ctxkey_id)) {
467 return NULL;
468 }
469 } else if (ctxkey_id->length > 0) {
470 return NULL;
471 }
472 }
473match:
474 *recipient_ctx = session->recipient_ctx;
475 return session->recipient_ctx->osc_ctx;
476 }
477 }
478
479 /*
480 * Reuse an in-RAM Common Context for this identity before calling
481 * oscore_find_cb. Concurrent sessions for the same (Recipient ID, ID
482 * Context) must share one sender sequence counter; rebuilding from
483 * storage per session creates duplicate sender contexts that can emit
484 * the same Partial IV under one sender key (RFC 8613 replay rule breach).
485 */
486 ram_ctx = oscore_find_context_in_ram(session->context, rcpkey_id, ctxkey_id,
487 oscore_r2, recipient_ctx);
488 if (ram_ctx != NULL) {
489 return ram_ctx;
490 }
491
492 if (session->context->oscore_find_cb != NULL) {
493 oscore_ctx_t *tmp_ctx = NULL;
494 coap_oscore_conf_t *oscore_conf;
495
496 coap_lock_callback_ret(oscore_conf, session->context->oscore_find_cb(session,
497 &rcpkey_id,
498 ctxkey_id));
499
500 if (oscore_conf) {
501 tmp_ctx = coap_init_oscore_context_from_conf(oscore_conf);
502 }
503
504 if (tmp_ctx != NULL) {
505 /*
506 * find matching recipient and remove others if multiple recipients
507 * have been provided. ensures unneeded memory is freed early
508 * to prevent memory leaks and unnecessary memory usage.
509 */
510 if (tmp_ctx->recipient_chain != NULL &&
511 tmp_ctx->recipient_chain->next_recipient != NULL) {
512 oscore_recipient_ctx_t *rcp_ctx = tmp_ctx->recipient_chain;
513 oscore_recipient_ctx_t *ref_ctx = NULL;
514
515 while (rcp_ctx != NULL) {
516 ref_ctx = rcp_ctx;
517 rcp_ctx = rcp_ctx->next_recipient;
518 if (coap_binary_equal(ref_ctx->recipient_id, &rcpkey_id)) {
519 ref_ctx->next_recipient = NULL;
520 *recipient_ctx = ref_ctx;
521 } else {
524 coap_free_type(COAP_OSCORE_REC, (void *)ref_ctx);
525 }
526 }
527 } else {
528 *recipient_ctx = tmp_ctx->recipient_chain;
529 }
530 tmp_ctx->recipient_chain = *recipient_ctx;
531 oscore_add_context(session->context, tmp_ctx);
532 coap_oscore_session_set_recipient_ctx(session, *recipient_ctx);
533 return tmp_ctx;
534 }
535 }
536
537 return NULL;
538}
539
540#define OSCORE_LOG_SIZE 16
541void
543 const char *name,
544 coap_bin_const_t *value) {
545 size_t i;
546
547 if (value == NULL) {
548 coap_log(level, " %-16s\n", name);
549 return;
550 }
551 if (value->length == 0) {
552 coap_log(level, " %-16s <>\n", name);
553 return;
554 }
555 if (coap_get_log_level() >= level) {
556 for (i = 0; i < value->length; i += OSCORE_LOG_SIZE) {
557 char number[3 * OSCORE_LOG_SIZE + 4];
558
559 oscore_convert_to_hex(&value->s[i],
560 value->length - i > OSCORE_LOG_SIZE ?
561 OSCORE_LOG_SIZE : value->length - i,
562 number,
563 sizeof(number));
564 coap_log(level, " %-16s %s\n", i == 0 ? name : "", number);
565 }
566 }
567}
568
569void
570oscore_log_int_value(coap_log_t level, const char *name, int value) {
571 coap_log(level, " %-16s %2d\n", name, value);
572}
573
574void
575oscore_log_char_value(coap_log_t level, const char *name, const char *value) {
576 coap_log(level, " %-16s %s\n", name, value);
577}
578
579void
580oscore_convert_to_hex(const uint8_t *src,
581 size_t src_len,
582 char *dest,
583 size_t dst_len) {
584 /*
585 * Last output character will be '\000'
586 * (If output undersized, add trailing ... to indicate this.
587 */
588 size_t space = (dst_len - 4) / 3;
589 uint32_t qq;
590
591 for (qq = 0; qq < src_len && qq < space; qq++) {
592 char tmp = src[qq] >> 4;
593 if (tmp > 9)
594 tmp = tmp + 0x61 - 10;
595 else
596 tmp = tmp + 0x30;
597 dest[qq * 3] = tmp;
598 tmp = src[qq] & 0xf;
599 if (tmp > 9)
600 tmp = tmp + 0x61 - 10;
601 else
602 tmp = tmp + 0x30;
603 dest[qq * 3 + 1] = tmp;
604 dest[qq * 3 + 2] = 0x20;
605 }
606 if (qq != src_len) {
607 dest[qq * 3] = '.';
608 dest[qq * 3 + 1] = '.';
609 dest[qq * 3 + 2] = '.';
610 qq++;
611 }
612 dest[qq * 3] = 0;
613}
614
617 coap_bin_const_t *salt,
618 coap_bin_const_t *ikm,
619 cose_alg_t aead_alg,
621 coap_str_const_t *type,
622 size_t out_len) {
623 uint8_t info_buffer[80];
624 size_t info_len;
625 coap_bin_const_t *hkdf;
626 uint8_t *hkdf_tmp = coap_malloc_type(COAP_STRING, out_len);
627
628 if (hkdf_tmp == NULL)
629 return NULL;
630
631 info_len = compose_info(info_buffer,
632 sizeof(info_buffer),
633 aead_alg,
634 id,
635 osc_ctx->id_context,
636 type,
637 out_len);
638 if (info_len == 0 || info_len > sizeof(info_buffer)) {
639 coap_free_type(COAP_STRING, hkdf_tmp);
640 return NULL;
641 }
642
643 oscore_hkdf(osc_ctx->hkdf_alg,
644 salt,
645 ikm,
646 info_buffer,
647 info_len,
648 hkdf_tmp,
649 out_len);
650 hkdf = coap_new_bin_const(hkdf_tmp, out_len);
651 coap_free_type(COAP_STRING, hkdf_tmp);
652 return hkdf;
653}
654
655static void
656oscore_log_context(oscore_ctx_t *osc_ctx, const char *heading) {
657#if COAP_MAX_LOGGING_LEVEL < _COAP_LOG_OSCORE
658 (void)osc_ctx;
659 (void)heading;
660#else /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_OSCORE */
662 char buffer[30];
664 size_t count = 0;
665
666 coap_log_oscore("%s\n", heading);
668 cose_get_alg_name(osc_ctx->aead_alg, buffer,
669 sizeof(buffer)));
671 cose_get_hkdf_alg_name(osc_ctx->hkdf_alg, buffer,
672 sizeof(buffer)));
673 oscore_log_hex_value(COAP_LOG_OSCORE, "ID Context", osc_ctx->id_context);
675 "Master Secret",
676 osc_ctx->master_secret);
677 oscore_log_hex_value(COAP_LOG_OSCORE, "Master Salt", osc_ctx->master_salt);
678 oscore_log_hex_value(COAP_LOG_OSCORE, "Common IV", osc_ctx->common_iv);
679#if COAP_OSCORE_GROUP_SUPPORT
681 "Sign Enc Key",
682 osc_ctx->sign_enc_key);
683#endif /* COAP_OSCORE_GROUP_SUPPORT */
685 "Sender ID",
686 osc_ctx->sender_context->sender_id);
688 "Sender Key",
689 osc_ctx->sender_context->sender_key);
690 while (next) {
691 snprintf(buffer, sizeof(buffer), "Recipient ID[%zu]", count);
693 buffer,
694 next->recipient_id);
695 snprintf(buffer, sizeof(buffer), "Recipient Key[%zu]", count);
697 buffer,
698 next->recipient_key);
699 count++;
700 next = next->next_recipient;
701 }
702 }
703#endif /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_OSCORE */
704}
705
706void
707oscore_update_ctx(oscore_ctx_t *osc_ctx, coap_bin_const_t *id_context) {
708 coap_bin_const_t *temp;
709
710 /* Update with new ID Context */
712 osc_ctx->id_context = id_context;
713
714 /* Update sender_key, recipient_key and common_iv */
715 temp = osc_ctx->sender_context->sender_key;
716 osc_ctx->sender_context->sender_key =
717 oscore_build_key(osc_ctx,
718 osc_ctx->master_salt,
719 osc_ctx->master_secret,
720 osc_ctx->aead_alg,
721 osc_ctx->sender_context->sender_id,
722 coap_make_str_const("Key"),
723 cose_key_len(osc_ctx->aead_alg));
724 if (!osc_ctx->sender_context->sender_key)
725 osc_ctx->sender_context->sender_key = temp;
726 else
728 temp = osc_ctx->recipient_chain->recipient_key;
730 oscore_build_key(osc_ctx,
731 osc_ctx->master_salt,
732 osc_ctx->master_secret,
733 osc_ctx->aead_alg,
735 coap_make_str_const("Key"),
736 cose_key_len(osc_ctx->aead_alg));
737 if (!osc_ctx->recipient_chain->recipient_key)
738 osc_ctx->recipient_chain->recipient_key = temp;
739 else
741 temp = osc_ctx->common_iv;
742 osc_ctx->common_iv = oscore_build_key(osc_ctx,
743 osc_ctx->master_salt,
744 osc_ctx->master_secret,
745 osc_ctx->aead_alg,
746 NULL,
748 cose_nonce_len(osc_ctx->aead_alg));
749 if (!osc_ctx->common_iv)
750 osc_ctx->common_iv = temp;
751 else
753#if COAP_OSCORE_GROUP_SUPPORT
754 /* Signature Encryption Key */
755 temp = osc_ctx->sign_enc_key;
756 osc_ctx->sign_enc_key =
757 oscore_build_key(osc_ctx,
758 osc_ctx->master_salt,
759 osc_ctx->master_secret,
760 osc_ctx->alg_group_enc,
761 NULL,
762 coap_make_str_const("SEKey"),
763 cose_key_len(osc_ctx->alg_group_enc));
764 if (!osc_ctx->sign_enc_key)
765 osc_ctx->sign_enc_key = temp;
766 else
768#endif /* COAP_OSCORE_GROUP_SUPPORT */
769
770 oscore_log_context(osc_ctx, "Updated Common context");
771}
772
775 oscore_ctx_t *o_osc_ctx,
776 coap_bin_const_t *sender_id,
777 coap_bin_const_t *recipient_id,
778 coap_bin_const_t *id_context) {
779 oscore_ctx_t *osc_ctx = NULL;
780 oscore_sender_ctx_t *sender_ctx = NULL;
781 coap_oscore_rcp_conf_t *rcp_conf;
782
784 if (osc_ctx == NULL)
785 goto error;
786 memset(osc_ctx, 0, sizeof(oscore_ctx_t));
787
789 if (sender_ctx == NULL)
790 goto error;
791 memset(sender_ctx, 0, sizeof(oscore_sender_ctx_t));
792
793 osc_ctx->sender_context = sender_ctx;
794 if (o_osc_ctx->master_secret)
795 osc_ctx->master_secret =
797 o_osc_ctx->master_secret->length);
798 if (o_osc_ctx->master_salt)
799 osc_ctx->master_salt = coap_new_bin_const(o_osc_ctx->master_salt->s,
800 o_osc_ctx->master_salt->length);
801 osc_ctx->aead_alg = o_osc_ctx->aead_alg;
802 osc_ctx->hkdf_alg = o_osc_ctx->hkdf_alg;
803 if (id_context)
804 osc_ctx->id_context = coap_new_bin_const(id_context->s, id_context->length);
805 osc_ctx->ssn_freq = o_osc_ctx->ssn_freq;
806 osc_ctx->replay_window_size = o_osc_ctx->replay_window_size;
807 osc_ctx->rfc8613_b_1_2 = o_osc_ctx->rfc8613_b_1_2;
808 osc_ctx->rfc8613_b_2 = o_osc_ctx->rfc8613_b_2;
809 osc_ctx->save_seq_num_func = o_osc_ctx->save_seq_num_func;
811
812 if (o_osc_ctx->master_secret) {
813 /* sender_ key */
814 sender_ctx->sender_key = oscore_build_key(osc_ctx,
815 osc_ctx->master_salt,
816 osc_ctx->master_secret,
817 osc_ctx->aead_alg,
818 sender_id,
819 coap_make_str_const("Key"),
820 cose_key_len(osc_ctx->aead_alg));
821 if (!sender_ctx->sender_key)
822 goto error;
823
824 /* common IV */
825 osc_ctx->common_iv = oscore_build_key(osc_ctx,
826 osc_ctx->master_salt,
827 osc_ctx->master_secret,
828 osc_ctx->aead_alg,
829 NULL,
831 cose_nonce_len(osc_ctx->aead_alg));
832 if (!osc_ctx->common_iv)
833 goto error;
834
835#if COAP_OSCORE_GROUP_SUPPORT
836 /* Signature Encryption Key */
837 osc_ctx->sign_enc_key =
838 oscore_build_key(osc_ctx,
839 osc_ctx->master_salt,
840 osc_ctx->master_secret,
841 osc_ctx->alg_group_enc,
842 NULL,
843 coap_make_str_const("SEKey"),
844 cose_key_len(osc_ctx->alg_group_enc));
845 if (!osc_ctx->sign_enc_key)
846 goto error;
847#endif /* COAP_OSCORE_GROUP_SUPPORT */
848 }
849
850 /*
851 * Need to set the last Sender Seq Num based on ssn_freq
852 * The value should only change if there is a change to ssn_freq
853 * and (potentially) be lower than seq, then save_seq_num_func() is
854 * immediately called on next SSN update.
855 */
856 sender_ctx->next_seq = 0;
857 sender_ctx->seq = 0;
858
859 sender_ctx->sender_id = coap_new_bin_const(sender_id->s, sender_id->length);
860
862 if (rcp_conf == NULL)
863 goto error;
864 memset(rcp_conf, 0, sizeof(coap_oscore_rcp_conf_t));
865 rcp_conf->recipient_id = coap_new_bin_const(recipient_id->s, recipient_id->length);
866 if (rcp_conf->recipient_id == NULL)
867 goto error;
868 /* rcp_conf is released in oscore_add_recipient() */
869 if (oscore_add_recipient(osc_ctx, rcp_conf, 0) == NULL)
870 goto error;
871
872 oscore_log_context(osc_ctx, "New Common context");
873 oscore_enter_context(c_context, osc_ctx);
874
875 return osc_ctx;
876
877error:
878 oscore_free_context(osc_ctx);
879 return NULL;
880}
881
884 oscore_ctx_t *osc_ctx = NULL;
885 oscore_sender_ctx_t *sender_ctx = NULL;
886 coap_oscore_rcp_conf_t *rcp_conf;
887 int ok;
888
890 if (osc_ctx == NULL)
891 goto error;
892 memset(osc_ctx, 0, sizeof(oscore_ctx_t));
893
895 if (sender_ctx == NULL)
896 goto error;
897 memset(sender_ctx, 0, sizeof(oscore_sender_ctx_t));
898
899 osc_ctx->sender_context = sender_ctx;
900 OSC_MOVE_PTR(osc_ctx->master_secret, oscore_conf->master_secret);
901 OSC_MOVE_PTR(osc_ctx->master_salt, oscore_conf->master_salt);
902 osc_ctx->aead_alg = oscore_conf->aead_alg;
903 osc_ctx->hkdf_alg = oscore_conf->hkdf_alg;
904 OSC_MOVE_PTR(osc_ctx->id_context, oscore_conf->id_context);
905 osc_ctx->ssn_freq = oscore_conf->ssn_freq ? oscore_conf->ssn_freq : 1;
906 osc_ctx->replay_window_size = oscore_conf->replay_window ?
907 oscore_conf->replay_window :
909#if COAP_OSCORE_GROUP_SUPPORT
910 OSC_MOVE_PTR(osc_ctx->gm_cred, oscore_conf->gm_cred);
911#endif /* COAP_OSCORE_GROUP_SUPPORT */
912 osc_ctx->rfc8613_b_1_2 = oscore_conf->rfc8613_b_1_2;
913 osc_ctx->rfc8613_b_2 = oscore_conf->rfc8613_b_2;
914 osc_ctx->save_seq_num_func = oscore_conf->save_seq_num_func;
915 osc_ctx->save_seq_num_func_param = oscore_conf->save_seq_num_func_param;
916
917 if (osc_ctx->master_secret) {
918 /* sender_ key */
919 if (oscore_conf->break_sender_key)
920 /* Interop testing */
921 sender_ctx->sender_key = oscore_build_key(osc_ctx,
922 osc_ctx->master_salt,
923 osc_ctx->master_secret,
924 osc_ctx->aead_alg,
925 oscore_conf->sender->sender_id,
926 coap_make_str_const("BAD"),
927 cose_key_len(osc_ctx->aead_alg));
928 else
929 sender_ctx->sender_key = oscore_build_key(osc_ctx,
930 osc_ctx->master_salt,
931 osc_ctx->master_secret,
932 osc_ctx->aead_alg,
933 oscore_conf->sender->sender_id,
934 coap_make_str_const("Key"),
935 cose_key_len(osc_ctx->aead_alg));
936 if (!sender_ctx->sender_key)
937 goto error;
938
939 /* common IV */
940 osc_ctx->common_iv = oscore_build_key(osc_ctx,
941 osc_ctx->master_salt,
942 osc_ctx->master_secret,
943 osc_ctx->aead_alg,
944 NULL,
946 cose_nonce_len(osc_ctx->aead_alg));
947 if (!osc_ctx->common_iv)
948 goto error;
949
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 =
954 oscore_cs_key_params(oscore_conf->sign_curve,
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;
960 osc_ctx->sign_params = coap_new_bin_const(counter_signature_parameters,
961 counter_signature_parameters_len);
962 if (oscore_conf->sender->group_mode && coap_get_log_level() >= COAP_LOG_OSCORE) {
963 oscore_log_hex_value(COAP_LOG_OSCORE, "Sign Params", osc_ctx->sign_params);
964 }
965 coap_free_type(COAP_STRING, counter_signature_parameters);
966 /* signature Encryption Key */
967 osc_ctx->sign_enc_key =
968 oscore_build_key(osc_ctx,
969 osc_ctx->master_salt,
970 osc_ctx->master_secret,
971 osc_ctx->alg_group_enc,
972 NULL,
973 coap_make_str_const("SEKey"),
974 cose_key_len(osc_ctx->alg_group_enc));
975 if (!osc_ctx->sign_enc_key)
976 goto error;
977 }
978#endif /* COAP_OSCORE_GROUP_SUPPORT */
979 }
980
981 /*
982 * Need to set the last Sender Seq Num based on ssn_freq
983 * The value should only change if there is a change to ssn_freq
984 * and (potentially) be lower than seq, then save_seq_num_func() is
985 * immediately called on next SSN update.
986 */
987 sender_ctx->next_seq = oscore_conf->start_seq_num -
988 (oscore_conf->start_seq_num % (oscore_conf->ssn_freq > 0 ? oscore_conf->ssn_freq : 1));
989
990 sender_ctx->seq = oscore_conf->start_seq_num;
991 if (oscore_conf->sender) {
992 OSC_MOVE_PTR(sender_ctx->sender_id, oscore_conf->sender->sender_id);
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);
998 if (sender_ctx->group_mode && coap_get_log_level() >= COAP_LOG_OSCORE) {
999 char buffer[30];
1000
1002 "Sender Priv DER",
1003 sender_ctx->my_private_key->pri_der);
1005 "Sender Priv RAW",
1006 sender_ctx->my_private_key->pri_raw);
1008 " Sig Wire Size",
1009 sender_ctx->my_private_key->wire_sign_size);
1011 "Sender Pub DER",
1012 sender_ctx->sender_cred->pub_der);
1014 "Sender Pub RAW",
1015 sender_ctx->sender_cred->pub_raw);
1017 "Sender Auth Cred",
1018 sender_ctx->sender_cred->auth_cred);
1020 " Sig Wire Size",
1021 sender_ctx->sender_cred->wire_sign_size);
1023 " Sig COSE Curve",
1024 cose_get_curve_name(sender_ctx->sender_cred->sign_curve,
1025 buffer, sizeof(buffer)));
1027 " Sig COSE Hash",
1028 cose_get_alg_name(sender_ctx->sender_cred->sign_hash,
1029 buffer, sizeof(buffer)));
1030 }
1031#endif /* COAP_OSCORE_GROUP_SUPPORT */
1032 coap_free_type(COAP_STRING, oscore_conf->sender);
1033 oscore_conf->sender = NULL;
1034 }
1035
1036 rcp_conf = oscore_conf->recipient_chain;
1037 ok = 1;
1038 while (rcp_conf) {
1039 coap_oscore_rcp_conf_t *rcp_next = rcp_conf->next_recipient;
1040
1041 /* rcp_conf is released in oscore_add_recipient() */
1042 if (oscore_add_recipient(osc_ctx, rcp_conf,
1043 oscore_conf->break_recipient_key) == NULL) {
1044 coap_log_warn("OSCORE: Failed to add Client ID\n");
1045 ok = 0;
1046 }
1047 rcp_conf = rcp_next;
1048 }
1049 oscore_conf->recipient_chain = NULL;
1050 if (!ok)
1051 goto error;
1052
1053 oscore_log_context(osc_ctx, "Common context");
1054 return osc_ctx;
1055error:
1056 oscore_free_context(osc_ctx);
1057 return NULL;
1058}
1059
1060int
1061oscore_add_context(coap_context_t *c_context, oscore_ctx_t *osc_ctx) {
1062 oscore_enter_context(c_context, osc_ctx);
1063 return 1;
1064}
1065
1066int
1068 return osc_ctx->next != NULL;
1069}
1070
1072oscore_derive_ctx(coap_context_t *c_context, coap_oscore_conf_t *oscore_conf) {
1073 oscore_ctx_t *osc_ctx = oscore_derive_ctx_from_conf(oscore_conf);
1074 oscore_enter_context(c_context, osc_ctx);
1075 return osc_ctx;
1076}
1077
1080 uint32_t break_key) {
1081 oscore_recipient_ctx_t *rcp_chain = osc_ctx->recipient_chain;
1082 oscore_recipient_ctx_t *rcp_ctx = NULL;
1083
1084 if (rcp_conf->recipient_id->length > 7) {
1085 coap_log_warn("oscore_add_recipient: Maximum size of recipient_id is 7 bytes\n");
1086 goto free_rcp_conf;
1087 }
1088 /* Check this is not a duplicate recipient id */
1089 while (rcp_chain) {
1090 if (rcp_chain->recipient_id->length == rcp_conf->recipient_id->length &&
1091 memcmp(rcp_chain->recipient_id->s, rcp_conf->recipient_id->s,
1092 rcp_conf->recipient_id->length) == 0) {
1093 goto free_rcp_conf;
1094 }
1095 rcp_chain = rcp_chain->next_recipient;
1096 }
1098 sizeof(oscore_recipient_ctx_t));
1099 if (rcp_ctx == NULL) {
1100 goto free_rcp_conf;
1101 }
1102 memset(rcp_ctx, 0, sizeof(oscore_recipient_ctx_t));
1103
1104 if (osc_ctx->master_secret) {
1105 if (break_key)
1106 /* Interop testing */
1107 rcp_ctx->recipient_key = oscore_build_key(osc_ctx,
1108 osc_ctx->master_salt,
1109 osc_ctx->master_secret,
1110 osc_ctx->aead_alg,
1111 rcp_conf->recipient_id,
1112 coap_make_str_const("BAD"),
1113 cose_key_len(osc_ctx->aead_alg));
1114 else
1115 rcp_ctx->recipient_key = oscore_build_key(osc_ctx,
1116 osc_ctx->master_salt,
1117 osc_ctx->master_secret,
1118 osc_ctx->aead_alg,
1119 rcp_conf->recipient_id,
1120 coap_make_str_const("Key"),
1121 cose_key_len(osc_ctx->aead_alg));
1122 if (!rcp_ctx->recipient_key) {
1123 goto free_rcp_conf;
1124 }
1125 }
1126 rcp_ctx->silent_server = rcp_conf->silent_server;
1127 if (rcp_conf->window_initialized) {
1128 rcp_ctx->last_seq = rcp_conf->last_seq;
1129 rcp_ctx->sliding_window = rcp_conf->sliding_window;
1130 rcp_ctx->initial_state = 0;
1131 } else {
1132 rcp_ctx->initial_state = 1;
1133 }
1134#if COAP_OSCORE_GROUP_SUPPORT
1135 if (osc_ctx->sender_context->group_mode) {
1136 if (osc_ctx->sender_context->pairwise_mode) {
1137 if (rcp_conf->group_mode) {
1138 rcp_ctx->mode = OSCORE_MODE_GROUP;
1139 } else {
1140 rcp_ctx->mode = OSCORE_MODE_PAIRWISE;
1141 }
1142 } else {
1143 rcp_ctx->mode = OSCORE_MODE_GROUP;
1144 }
1145 } else if (osc_ctx->sender_context->pairwise_mode) {
1146 if (rcp_conf->pairwise_mode) {
1147 rcp_ctx->mode = OSCORE_MODE_PAIRWISE;
1148 } else {
1149 rcp_ctx->mode = OSCORE_MODE_SINGLE;
1150 }
1151 } else {
1152 rcp_ctx->mode = OSCORE_MODE_SINGLE;
1153 }
1154
1155 OSC_MOVE_PTR(rcp_ctx->recipient_id, rcp_conf->recipient_id);
1156 OSC_MOVE_PTR(rcp_ctx->rem_cred, rcp_conf->gr_public_cred);
1157
1158 if (rcp_ctx->mode == OSCORE_MODE_GROUP &&
1160 coap_log_warn("OSCORE Group not supported by underlying TLS library\n");
1161 goto free_rcp_conf;
1162 }
1163 if (rcp_ctx->mode == OSCORE_MODE_PAIRWISE &&
1165 coap_log_warn("OSCORE Pairwise not supported by underlying TLS library\n");
1166 goto free_rcp_conf;
1167 }
1168
1169 if (rcp_ctx->mode == OSCORE_MODE_GROUP &&
1171 char buffer[30];
1172
1173 if (rcp_ctx->rem_cred != NULL) {
1175 "Rcpt Pub DER",
1176 rcp_ctx->rem_cred->pub_der);
1178 "Rcpt Pub RAW",
1179 rcp_ctx->rem_cred->pub_raw);
1181 "Rcpt Auth Cred",
1182 rcp_ctx->rem_cred->auth_cred);
1183 }
1185 " Sig Wire Size",
1186 rcp_ctx->rem_cred->wire_sign_size);
1188 " Sig COSE Curve",
1189 cose_get_curve_name(rcp_ctx->rem_cred->sign_curve,
1190 buffer, sizeof(buffer)));
1192 " Sig COSE Hash",
1193 cose_get_alg_name(rcp_ctx->rem_cred->sign_hash,
1194 buffer, sizeof(buffer)));
1195 }
1196 if (rcp_ctx->mode == OSCORE_MODE_PAIRWISE) {
1197 oscore_sender_ctx_t *snd_ctx = osc_ctx->sender_context;
1198 coap_bin_const_t *shared_secret = NULL;
1199 coap_binary_t *ikm = NULL;
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,
1203 &shared_secret)) {
1205 "Shared Secret",
1206 shared_secret);
1207 ikm = coap_new_binary(snd_ctx->sender_cred->pub_der->length +
1208 rcp_ctx->rem_cred->pub_der->length +
1209 shared_secret->length);
1210 if (!ikm)
1211 goto fail;
1212
1213 /* Do the Pairwise Sender Key */
1214 coap_delete_bin_const(rcp_ctx->pw_rem_recipient_key);
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],
1222 shared_secret->s,
1223 shared_secret->length);
1224 rcp_ctx->pw_rem_recipient_key =
1225 oscore_build_key(osc_ctx,
1226 snd_ctx->sender_key,
1227 (coap_bin_const_t *)ikm,
1228 osc_ctx->aead_alg,
1229 rcp_ctx->recipient_id,
1230 coap_make_str_const("Key"),
1231 cose_key_len(osc_ctx->aead_alg));
1232 if (!rcp_ctx->pw_rem_recipient_key)
1233 goto fail;
1235 "Recipient Key",
1236 rcp_ctx->pw_rem_recipient_key);
1237 /* Do the Pairwise Recipient Credentials */
1238 coap_delete_bin_const(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],
1246 shared_secret->s,
1247 shared_secret->length);
1248 rcp_ctx->pw_rem_recipient_key =
1249 oscore_build_key(osc_ctx,
1250 rcp_ctx->recipient_key,
1251 (coap_bin_const_t *)ikm,
1252 osc_ctx->aead_alg,
1253 snd_ctx->sender_id,
1254 coap_make_str_const("Key"),
1255 cose_key_len(osc_ctx->aead_alg));
1256 coap_delete_binary(ikm);
1257 coap_delete_bin_const(shared_secret);
1258 } else {
1259fail:
1260 coap_delete_binary(ikm);
1261 coap_delete_bin_const(shared_secret);
1262 oscore_free_recipient_ctx(rcp_ctx);
1263 coap_free_type(COAP_STRING, rcp_conf);
1264 return NULL;
1265 }
1266 }
1267#else /* ! COAP_OSCORE_GROUP_SUPPORT */
1268 rcp_ctx->mode = OSCORE_MODE_SINGLE;
1269 OSC_MOVE_PTR(rcp_ctx->recipient_id, rcp_conf->recipient_id);
1270#endif /* ! COAP_OSCORE_GROUP_SUPPORT */
1271
1272 rcp_ctx->osc_ctx = osc_ctx;
1273
1274 rcp_chain = osc_ctx->recipient_chain;
1275 rcp_ctx->next_recipient = rcp_chain;
1276 osc_ctx->recipient_chain = rcp_ctx;
1278 /* All configured values are now in rcp_ctx */
1279 coap_free_type(COAP_STRING, rcp_conf);
1280 return rcp_ctx;
1281
1282free_rcp_conf:
1285 return NULL;
1286}
1287
1288int
1291 oscore_recipient_ctx_t *next = osc_ctx->recipient_chain;
1292 while (next) {
1293 if (next->recipient_id->length == rid->length &&
1294 memcmp(next->recipient_id->s, rid->s, rid->length) == 0) {
1295 if (prev != NULL)
1296 prev->next_recipient = next->next_recipient;
1297 else
1298 osc_ctx->recipient_chain = next->next_recipient;
1299 oscore_free_recipient_ctx(next);
1300 return 1;
1301 }
1302 prev = next;
1303 next = next->next_recipient;
1304 }
1305 return 0;
1306}
1307
1308void
1310 recipient_ctx->ref++;
1311}
1312
1313void
1315 oscore_ctx_t *osc_ctx;
1316
1317 if (recipient_ctx == NULL || *recipient_ctx == NULL)
1318 return;
1319
1320 /* ensure oscore context is freed if not attached to coap context */
1321 osc_ctx = (*recipient_ctx)->osc_ctx;
1322 oscore_free_recipient_ctx(*recipient_ctx);
1323 /*
1324 * Free temporary oscore context if not attached to a coap context
1325 * and no recipients attached anymore.
1326 */
1327 if (!oscore_is_context_attached(osc_ctx) && osc_ctx->recipient_chain == NULL) {
1328 oscore_free_context(osc_ctx);
1329 }
1330 *recipient_ctx = NULL;
1331}
1332
1333void
1335 if (association) {
1337
1338 coap_delete_pdu_lkd(association->sent_pdu);
1339 coap_delete_bin_const(association->token);
1340 coap_delete_bin_const(association->aad);
1341 coap_delete_bin_const(association->nonce);
1342 coap_delete_bin_const(association->partial_iv);
1344 coap_free_type(COAP_STRING, association);
1345 }
1346}
1347
1348int
1350 coap_pdu_t *sent_pdu,
1351 coap_bin_const_t *token,
1352 oscore_recipient_ctx_t *recipient_ctx,
1353 coap_bin_const_t *aad,
1354 coap_bin_const_t *nonce,
1355 coap_bin_const_t *partial_iv,
1356 int is_observe) {
1357 oscore_association_t *association;
1358
1359 association = coap_malloc_type(COAP_STRING, sizeof(oscore_association_t));
1360 if (association == NULL)
1361 return 0;
1362
1363 coap_log_oscore("Association %p created\n", (void *)association);
1364 memset(association, 0, sizeof(oscore_association_t));
1365 coap_oscore_association_set_recipient_ctx(association, recipient_ctx);
1366 association->is_observe = is_observe;
1367 association->just_set_up = 1;
1368
1369 if (sent_pdu) {
1370 size_t size;
1371 const uint8_t *data;
1372
1373 association->sent_pdu = coap_pdu_duplicate_lkd(sent_pdu, session,
1374 token->length, token->s,
1376 if (association->sent_pdu == NULL)
1377 goto error;
1378 if (coap_get_data(sent_pdu, &size, &data)) {
1379 coap_add_data(association->sent_pdu, size, data);
1380 }
1381 }
1382 association->token = coap_new_bin_const(token->s, token->length);
1383 if (association->token == NULL)
1384 goto error;
1385
1386 if (aad) {
1387 association->aad = coap_new_bin_const(aad->s, aad->length);
1388 if (association->aad == NULL)
1389 goto error;
1390 }
1391
1392 if (nonce) {
1393 association->nonce = coap_new_bin_const(nonce->s, nonce->length);
1394 if (association->nonce == NULL)
1395 goto error;
1396 }
1397
1398 if (partial_iv) {
1399 association->partial_iv =
1400 coap_new_bin_const(partial_iv->s, partial_iv->length);
1401 if (association->partial_iv == NULL)
1402 goto error;
1403 }
1404
1405 OSCORE_ASSOCIATIONS_ADD(session->associations, association);
1406 return 1;
1407
1408error:
1409 oscore_free_association(association);
1410 return 0;
1411}
1412
1415 oscore_association_t *association;
1416
1417 OSCORE_ASSOCIATIONS_FIND(session->associations, token, association);
1418 return association;
1419}
1420
1421int
1423 oscore_association_t *association) {
1424 if (association) {
1425 OSCORE_ASSOCIATIONS_DELETE(session->associations, association);
1426 coap_log_oscore("Association %p deleted\n", (void *)association);
1427 oscore_free_association(association);
1428 return 1;
1429 }
1430 return 0;
1431}
1432
1433void
1435 if (session) {
1436 oscore_association_t *association;
1438
1439 OSCORE_ASSOCIATIONS_ITER_SAFE(session->associations, association, tmp) {
1440 OSCORE_ASSOCIATIONS_DELETE(session->associations, association);
1441 coap_log_oscore("Association %p deleted\n", (void *)association);
1442 oscore_free_association(association);
1443 }
1444 session->associations = NULL;
1445 }
1446}
1447
1448#else /* ! COAP_OSCORE_SUPPORT */
1449
1450#ifdef __clang__
1451/* Make compilers happy that do not like empty modules. As this function is
1452 * never used, we ignore -Wunused-function at the end of compiling this file
1453 */
1454#pragma GCC diagnostic ignored "-Wunused-function"
1455#endif
1456static inline void
1457dummy(void) {
1458}
1459
1460#endif /* ! COAP_OSCORE_SUPPORT */
Library specific build wrapper for coap_internal.h.
@ COAP_OSCORE_COM
Definition coap_mem.h:55
@ COAP_OSCORE_SEN
Definition coap_mem.h:56
@ COAP_OSCORE_REC
Definition coap_mem.h:57
@ COAP_STRING
Definition coap_mem.h:33
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 NULL
Definition coap_option.h:30
#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.
Definition coap_debug.c:103
#define coap_log_oscore(...)
Definition coap_debug.h:132
coap_log_t
Logging type.
Definition coap_debug.h:56
#define coap_log_warn(...)
Definition coap_debug.h:108
#define coap_log(level,...)
Logging function.
Definition coap_debug.h:290
@ COAP_LOG_OSCORE
Definition coap_debug.h:65
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)
cose_alg_t
const char * cose_get_curve_name(cose_curve_t id, char *buffer, size_t buflen)
@ COSE_KTY_OKP
Definition oscore_cose.h:73
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.
Definition coap_pdu.c:192
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.
Definition coap_pdu.c:232
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:961
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:930
@ COAP_BOOL_FALSE
Definition coap_pdu.h:306
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:130
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
Definition coap_str.c:81
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:70
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:119
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
Definition coap_str.c:114
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
Definition coap_str.h:222
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.
static void dummy(void)
CoAP binary data definition with const data.
Definition coap_str.h:65
size_t length
length of binary data
Definition coap_str.h:66
const uint8_t * s
read-only binary data
Definition coap_str.h:67
CoAP binary data definition.
Definition coap_str.h:57
uint8_t * s
binary data
Definition coap_str.h:59
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.
structure for CoAP PDUs
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.
Definition coap_str.h:47
const uint8_t * s
read-only string data
Definition coap_str.h:49
size_t length
length of string
Definition coap_str.h:48
coap_bin_const_t partial_iv
coap_bin_const_t * obs_partial_iv
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
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.