libcoap 4.3.5-develop-d0fa34c
Loading...
Searching...
No Matches
coap_oscore.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 * coap_oscore.c -- Object Security for Constrained RESTful Environments
5 * (OSCORE) support for libcoap
6 *
7 * Copyright (C) 2019-2021 Olaf Bergmann <bergmann@tzi.org>
8 * Copyright (C) 2021-2026 Jon Shallow <supjps-libcoap@jpshallow.com>
9 *
10 * SPDX-License-Identifier: BSD-2-Clause
11 *
12 * This file is part of the CoAP library libcoap. Please see README for terms
13 * of use.
14 */
15
20
22
23#if COAP_OSCORE_SUPPORT
24#include <ctype.h>
25
26#define AAD_BUF_LEN 200 /* length of aad_buffer */
27
28static oscore_ctx_t *coap_oscore_init(coap_context_t *c_context,
29 coap_oscore_conf_t *oscore_conf);
30
31#if COAP_CLIENT_SUPPORT
32
33int
35 if (oscore_conf) {
36 oscore_ctx_t *osc_ctx;
37
38 if (oscore_conf->recipient_chain == NULL) {
39 coap_log_warn("OSCORE: Recipient ID must be defined for a client\n");
40 coap_delete_oscore_conf(oscore_conf);
41 return 0;
42 }
43 if (oscore_conf->rfc8613_b_2) {
44 /* Need to replace id_context with random value */
45 coap_binary_t *id_context = coap_new_binary(8);
46
47 if (id_context == NULL) {
48 coap_delete_oscore_conf(oscore_conf);
49 return 0;
50 }
52 coap_prng_lkd(id_context->s, id_context->length);
53 oscore_conf->id_context = (coap_bin_const_t *)id_context;
54 session->b_2_step = COAP_OSCORE_B_2_STEP_1;
55 coap_log_oscore("Appendix B.2 client step 1 (Generated ID1)\n");
56 }
57
58 osc_ctx = coap_oscore_init(session->context, oscore_conf);
59 if (osc_ctx == NULL) {
60 return 0;
61 }
63 session->oscore_encryption = 1;
64 }
65 return 1;
66}
67
70 const coap_address_t *local_if,
71 const coap_address_t *server,
72 coap_proto_t proto,
73 coap_oscore_conf_t *oscore_conf) {
74 coap_session_t *session;
75
76 coap_lock_lock(return NULL);
77 session = coap_new_client_session_oscore3_lkd(ctx, local_if, server, proto, oscore_conf, NULL,
78 NULL, NULL);
80 return session;
81}
82
85 const coap_address_t *local_if,
86 const coap_address_t *server,
87 coap_proto_t proto,
88 coap_oscore_conf_t *oscore_conf,
89 void *app_data,
91 coap_str_const_t *ws_host) {
92 coap_session_t *session;
93
94 coap_lock_lock(return NULL);
95 session = coap_new_client_session_oscore3_lkd(ctx, local_if, server, proto, oscore_conf, app_data,
96 callback, ws_host);
98 return session;
99}
100
103 const coap_address_t *local_if,
104 const coap_address_t *server,
105 coap_proto_t proto,
106 coap_oscore_conf_t *oscore_conf,
107 void *app_data,
109 coap_str_const_t *ws_host) {
110 coap_session_t *session =
111 coap_new_client_session3_lkd(ctx, local_if, server, proto, app_data, callback, ws_host);
112
113 if (!session)
114 return NULL;
115
116 if (coap_oscore_initiate(session, oscore_conf) == 0) {
118 return NULL;
119 }
120 return session;
121}
122
125 const coap_address_t *local_if,
126 const coap_address_t *server,
127 coap_proto_t proto,
128 coap_dtls_cpsk_t *psk_data,
129 coap_oscore_conf_t *oscore_conf) {
130 coap_session_t *session;
131
132 coap_lock_lock(return NULL);
133 session = coap_new_client_session_oscore_psk3_lkd(ctx, local_if, server, proto, psk_data,
134 oscore_conf, NULL, NULL, NULL);
136 return session;
137}
138
141 const coap_address_t *local_if,
142 const coap_address_t *server,
143 coap_proto_t proto,
144 coap_dtls_cpsk_t *psk_data,
145 coap_oscore_conf_t *oscore_conf,
146 void *app_data,
148 coap_str_const_t *ws_host) {
149 coap_session_t *session;
150
151 coap_lock_lock(return NULL);
152 session = coap_new_client_session_oscore_psk3_lkd(ctx, local_if, server, proto, psk_data,
153 oscore_conf, app_data, callback, ws_host);
155 return session;
156}
157
160 const coap_address_t *local_if,
161 const coap_address_t *server,
162 coap_proto_t proto,
163 coap_dtls_cpsk_t *psk_data,
164 coap_oscore_conf_t *oscore_conf,
165 void *app_data,
167 coap_str_const_t *ws_host) {
168 coap_session_t *session;
169
171 session = coap_new_client_session_psk3_lkd(ctx, local_if, server, proto, psk_data,
172 app_data, callback, ws_host);
173
174 if (!session)
175 return NULL;
176
177 if (coap_oscore_initiate(session, oscore_conf) == 0) {
179 return NULL;
180 }
181 return session;
182}
183
186 const coap_address_t *local_if,
187 const coap_address_t *server,
188 coap_proto_t proto,
189 coap_dtls_pki_t *pki_data,
190 coap_oscore_conf_t *oscore_conf) {
191 coap_session_t *session;
192
193 coap_lock_lock(return NULL);
194 session = coap_new_client_session_oscore_pki3_lkd(ctx, local_if, server, proto, pki_data,
195 oscore_conf, NULL, NULL, NULL);
197 return session;
198}
199
202 const coap_address_t *local_if,
203 const coap_address_t *server,
204 coap_proto_t proto,
205 coap_dtls_pki_t *pki_data,
206 coap_oscore_conf_t *oscore_conf,
207 void *app_data,
209 coap_str_const_t *ws_host) {
210 coap_session_t *session;
211
212 coap_lock_lock(return NULL);
213 session = coap_new_client_session_oscore_pki3_lkd(ctx, local_if, server, proto, pki_data,
214 oscore_conf, app_data, callback, ws_host);
216 return session;
217}
218
221 const coap_address_t *local_if,
222 const coap_address_t *server,
223 coap_proto_t proto,
224 coap_dtls_pki_t *pki_data,
225 coap_oscore_conf_t *oscore_conf,
226 void *app_data,
228 coap_str_const_t *ws_host) {
229 coap_session_t *session;
230
232 session = coap_new_client_session_pki3_lkd(ctx, local_if, server, proto, pki_data,
233 app_data, callback, ws_host);
234
235 if (!session)
236 return NULL;
237
238 if (coap_oscore_initiate(session, oscore_conf) == 0) {
240 return NULL;
241 }
242 return session;
243}
244#endif /* COAP_CLIENT_SUPPORT */
245#if COAP_SERVER_SUPPORT
246
247COAP_API int
249 coap_oscore_conf_t *oscore_conf) {
250 int ret;
251
252 coap_lock_lock(return 0);
253 ret = coap_context_oscore_server_lkd(context, oscore_conf);
255 return ret;
256}
257
258int
260 coap_oscore_conf_t *oscore_conf) {
261 oscore_ctx_t *osc_ctx;
262
264 osc_ctx = coap_oscore_init(context, oscore_conf);
265 /* osc_ctx already added to context->osc_ctx */
266 if (osc_ctx)
267 return 1;
268 return 0;
269}
270
271#endif /* COAP_SERVER_SUPPORT */
272
273int
275 coap_uri_t uri;
276 coap_opt_iterator_t opt_iter;
277 coap_opt_t *option;
278 uint8_t option_value_buffer[15];
279 coap_optlist_t *optlist_chain = NULL;
280
281 if ((option =
282 coap_check_option(pdu, COAP_OPTION_PROXY_URI, &opt_iter)) == NULL)
283 return 1;
284
285 /* Need to break down into the component parts, but keep data safe */
286 memset(&uri, 0, sizeof(uri));
287
289 coap_opt_length(option),
290 &uri) < 0 || uri.scheme >= COAP_URI_SCHEME_LAST) {
291 coap_log_warn("Proxy URI '%.*s' not decodable\n",
292 coap_opt_length(option),
293 (const char *)coap_opt_value(option));
294 goto error;
295 }
297 goto error;
298
299 if (!coap_insert_option(pdu,
301 uri.host.length,
302 uri.host.s))
303 goto error;
307 coap_encode_var_safe(option_value_buffer,
308 sizeof(option_value_buffer),
309 uri.port & 0xffff),
310 option_value_buffer))
311 goto error;
312 if (uri.path.length) {
313 /* Add in the Uri-Path options */
315 &optlist_chain))
316 goto error;
317 }
318 if (uri.query.length) {
319 /* Add in the Uri-Query options */
321 &optlist_chain))
322 goto error;
323 }
324 if (!coap_add_optlist_pdu(pdu, &optlist_chain))
325 goto error;
326
327 if (!coap_insert_option(pdu,
329 strlen(coap_uri_scheme[uri.scheme].name),
330 (const uint8_t *)coap_uri_scheme[uri.scheme].name))
331 goto error;
332
333 coap_delete_optlist(optlist_chain);
334 return 1;
335
336error:
337 coap_delete_optlist(optlist_chain);
338 return 0;
339}
340
341static void
342dump_cose(cose_encrypt0_t *cose, const char *message) {
343#if COAP_MAX_LOGGING_LEVEL < _COAP_LOG_OSCORE
344 (void)cose;
345 (void)message;
346#else /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_OSCORE */
348 char buffer[30];
349
350 coap_log_oscore("%s COSE information\n", message);
352 cose_get_alg_name(cose->alg, buffer, sizeof(buffer)));
354 oscore_log_hex_value(COAP_LOG_OSCORE, "partial_iv", &cose->partial_iv);
356 oscore_log_hex_value(COAP_LOG_OSCORE, "kid_context", &cose->kid_context);
358 "oscore_option",
359 &cose->oscore_option);
361 oscore_log_hex_value(COAP_LOG_OSCORE, "external_aad", &cose->external_aad);
363 }
364#endif /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_OSCORE */
365}
366
369 coap_pdu_t *pdu,
370 coap_bin_const_t *kid_context,
371 oscore_partial_iv_t send_partial_iv) {
372 coap_pdu_t *ret_pdu;
373
374 coap_lock_lock(return NULL);
375 ret_pdu = coap_oscore_new_pdu_encrypted_lkd(session, pdu, kid_context, send_partial_iv);
377
378 return ret_pdu;
379}
380
381/*
382 * Take current PDU, create a new one appropriately separated as per RFC8613
383 * and then encrypt / integrity check the OSCORE data
384 */
387 coap_pdu_t *pdu,
388 coap_bin_const_t *kid_context,
389 oscore_partial_iv_t send_partial_iv) {
390 uint8_t coap_request = COAP_PDU_IS_REQUEST(pdu) || COAP_PDU_IS_PING(pdu);
391 coap_pdu_code_t code =
392 coap_request ? COAP_REQUEST_CODE_POST : COAP_RESPONSE_CODE(204);
393 coap_pdu_t *osc_pdu;
394 coap_pdu_t *plain_pdu = NULL;
395 coap_bin_const_t pdu_token;
396 coap_opt_iterator_t opt_iter;
397 coap_opt_t *option;
398 uint8_t pdu_code = pdu->code;
399 size_t length;
400 const uint8_t *data;
401 uint8_t *ciphertext_buffer = NULL;
402 size_t ciphertext_len = 0;
403 uint8_t aad_buffer[AAD_BUF_LEN];
404 uint8_t nonce_buffer[13];
406 coap_bin_const_t nonce;
407 oscore_recipient_ctx_t *rcp_ctx;
408 oscore_ctx_t *osc_ctx;
409 cose_encrypt0_t cose[1];
410 uint8_t group_flag = 0;
411 int show_pdu = 0;
412 int doing_observe = 0;
413 uint32_t observe_value = 0;
414 oscore_association_t *association = NULL;
415 oscore_sender_ctx_t *snd_ctx;
416 uint8_t external_aad_buffer[200];
417 coap_bin_const_t external_aad;
418 uint8_t oscore_option[255];
419 ssize_t oscore_option_len;
420
421 /* Check that OSCORE has not already been done */
422 if (coap_check_option(pdu, COAP_OPTION_OSCORE, &opt_iter))
423 return NULL;
424
425 if (coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter))
426 doing_observe = 1;
427
428 coap_log_debug("PDU to encrypt\n");
430 osc_pdu = coap_pdu_init(pdu->type == COAP_MESSAGE_NON &&
431 session->b_2_step != COAP_OSCORE_B_2_NONE ?
432 COAP_MESSAGE_CON : pdu->type,
433 code,
434 pdu->mid,
435 pdu->used_size + coap_oscore_overhead(session, pdu));
436 if (osc_pdu == NULL)
437 return NULL;
438
439 cose_encrypt0_init(cose); /* clears cose memory */
440 pdu_token = coap_pdu_get_token(pdu);
441 if (coap_request) {
442 /*
443 * RFC8613 8.1 Step 1. Protecting the client's request
444 * Get the Sender Context
445 */
446 rcp_ctx = session->recipient_ctx;
447 if (rcp_ctx == NULL)
448 goto error;
449 osc_ctx = rcp_ctx->osc_ctx;
450 assert(osc_ctx);
451 snd_ctx = osc_ctx->sender_context;
452 } else {
453 /*
454 * RFC8613 8.3 Step 1. Protecting the server's response
455 * Get the Sender Context
456 */
457 association = oscore_find_association(session, &pdu_token);
458 if (association == NULL)
459 goto error;
460
461 rcp_ctx = association->recipient_ctx;
462 osc_ctx = rcp_ctx->osc_ctx;
463 snd_ctx = osc_ctx->sender_context;
464 cose_encrypt0_set_partial_iv(cose, association->partial_iv);
465 cose_encrypt0_set_aad(cose, association->aad);
466 }
467
468 cose_encrypt0_set_alg(cose, osc_ctx->aead_alg);
469
470 if (coap_request || doing_observe ||
471 send_partial_iv == OSCORE_SEND_PARTIAL_IV) {
472 uint8_t partial_iv_buffer[8];
473 size_t partial_iv_len;
474 coap_bin_const_t partial_iv;
475 partial_iv_len = coap_encode_var_safe8(partial_iv_buffer,
476 sizeof(partial_iv_buffer),
477 snd_ctx->seq);
478 if (snd_ctx->seq == 0) {
479 /* Need to special case */
480 partial_iv_buffer[0] = '\000';
481 partial_iv_len = 1;
482 }
483 partial_iv.s = partial_iv_buffer;
484 partial_iv.length = partial_iv_len;
485 cose_encrypt0_set_partial_iv(cose, &partial_iv);
486 }
487
488 if (coap_request)
490
491 cose_encrypt0_set_key_id(cose, snd_ctx->sender_id);
492
493 /* nonce (needs to have sender information correctly set up) */
494
495 if (coap_request || doing_observe ||
496 send_partial_iv == OSCORE_SEND_PARTIAL_IV) {
497 /*
498 * 8.1 Step 3 or RFC8613 8.3.1 Step A
499 * Compose the AEAD nonce
500 *
501 * Requires in COSE object as appropriate
502 * key_id (kid) (sender)
503 * partial_iv (sender)
504 * common_iv (already in osc_ctx)
505 */
506 nonce.s = nonce_buffer;
507 nonce.length = 13;
508 oscore_generate_nonce(cose, osc_ctx, nonce_buffer, 13);
509 cose_encrypt0_set_nonce(cose, &nonce);
510 if (!oscore_increment_sender_seq(osc_ctx))
511 goto error;
512 if (osc_ctx->save_seq_num_func) {
513 if (osc_ctx->sender_context->seq > osc_ctx->sender_context->next_seq) {
514 /* Only update at ssn_freq rate */
515 osc_ctx->sender_context->next_seq += osc_ctx->ssn_freq;
516 osc_ctx->save_seq_num_func(osc_ctx->sender_context->next_seq,
517 osc_ctx->save_seq_num_func_param);
518 }
519 }
520 } else {
521 /*
522 * 8.3 Step 3.
523 * Use nonce from request
524 */
525 cose_encrypt0_set_nonce(cose, association->nonce);
526 }
527
528 /* OSCORE_option (needs to be before AAD as included in AAD if group) */
529
530 /* cose is modified for encode option in response message */
531 if (!coap_request) {
532 /* no kid on response */
534 if (!doing_observe && send_partial_iv == OSCORE_SEND_NO_IV)
536 }
537 if (kid_context) {
538 cose_encrypt0_set_kid_context(cose, kid_context);
539 }
540 oscore_option_len =
541 oscore_encode_option_value(oscore_option, sizeof(oscore_option), cose,
542 group_flag,
543 session->b_2_step != COAP_OSCORE_B_2_NONE);
544 if (oscore_option_len < 0) {
545 coap_log_oscore("Invalid parameters for creating OSCORE option\n");
546 goto error;
547 }
548 if (!coap_request) {
549 /* Reset what was just unset as appropriate for AAD */
551 cose_encrypt0_set_partial_iv(cose, association->partial_iv);
552 }
553 if (kid_context)
555
556 /*
557 * RFC8613 8.1/8.3 Step 2(a) (5.4).
558 * Compose the External AAD and then AAD
559 *
560 * OSCORE_option requires
561 * partial_iv (cose partial_iv)
562 * kid_context (cose kid_context)
563 * key_id (cose key_id)
564 * group_flag
565 *
566 * Non Group requires the following
567 * RFC8613 5.4
568 * oscore_version 1
569 * algorithms [
570 * aead_alg (osc_ctx)
571 * ]
572 * request_kid (request key_id using cose)
573 * request_piv (request partial_iv using cose)
574 * options (none at present)
575 *
576 * Note: No I options at present
577 */
578 if (coap_request || send_partial_iv == OSCORE_SEND_PARTIAL_IV) {
579 /* External AAD */
580 external_aad.s = external_aad_buffer;
581 external_aad.length = oscore_prepare_e_aad(osc_ctx,
582 cose,
583 NULL,
584 0,
585 NULL,
586 external_aad_buffer,
587 sizeof(external_aad_buffer));
588 cose_encrypt0_set_external_aad(cose, &external_aad);
589
590 /* AAD */
591 aad.s = aad_buffer;
592 aad.length = oscore_prepare_aad(external_aad_buffer,
593 external_aad.length,
594 aad_buffer,
595 sizeof(aad_buffer));
596 assert(aad.length < AAD_BUF_LEN);
597 cose_encrypt0_set_aad(cose, &aad);
598 }
599
600 /*
601 * RFC8613 8.1/8.3 Step 2(b) (5.3).
602 *
603 * Set up temp plaintext pdu, the data including token, options and
604 * optional payload will get encrypted as COSE ciphertext.
605 */
606 plain_pdu = coap_pdu_init(pdu->type,
607 pdu->code,
608 pdu->mid,
609 pdu->used_size + 1 /* pseudo-token with actual code */);
610 if (plain_pdu == NULL)
611 goto error;
612
613 coap_add_token(osc_pdu, pdu_token.length, pdu_token.s);
614
615 /* First byte of plain is real CoAP code. Pretend it is token */
616 coap_add_token(plain_pdu, 1, &pdu_code);
617
618 /* Copy across the Outer/Inner Options to respective PDUs */
620 while ((option = coap_option_next(&opt_iter))) {
621 switch ((coap_code_opt_num_t)opt_iter.number) {
627 /* Outer only */
628 if (!coap_insert_option(osc_pdu,
629 opt_iter.number,
630 coap_opt_length(option),
631 coap_opt_value(option)))
632 goto error;
633 break;
635 /* Make as Outer option as-is */
636 if (!coap_insert_option(osc_pdu,
637 opt_iter.number,
638 coap_opt_length(option),
639 coap_opt_value(option)))
640 goto error;
641 if (coap_request) {
642 /* Make as Inner option (unchanged) */
643 if (!coap_insert_option(plain_pdu,
644 opt_iter.number,
645 coap_opt_length(option),
646 coap_opt_value(option)))
647 goto error;
648 osc_pdu->code = COAP_REQUEST_CODE_FETCH;
649 } else {
650 /* Make as Inner option but empty */
651 if (!coap_insert_option(plain_pdu, opt_iter.number, 0, NULL))
652 goto error;
653 osc_pdu->code = COAP_RESPONSE_CODE(205);
654 }
655 show_pdu = 1;
656 doing_observe = 1;
657 observe_value = coap_decode_var_bytes(coap_opt_value(option),
658 coap_opt_length(option));
659 break;
661 /*
662 * Should have already been caught by doing
663 * coap_rebuild_pdu_for_proxy() before calling
664 * coap_oscore_new_pdu_encrypted_lkd()
665 */
666 assert(0);
667 break;
669 /* Dealt with separately */
670 break;
672 case COAP_OPTION_ETAG:
689 case COAP_OPTION_ECHO:
690 case COAP_OPTION_RTAG:
691 default:
692 /* Make as Inner option */
693 if (!coap_insert_option(plain_pdu,
694 opt_iter.number,
695 coap_opt_length(option),
696 coap_opt_value(option)))
697 goto error;
698 break;
699 }
700 }
701 /* Add in data to plain */
702 if (coap_get_data(pdu, &length, &data)) {
703 if (!coap_add_data(plain_pdu, length, data))
704 goto error;
705 }
706 if (show_pdu) {
707 coap_log_oscore("OSCORE payload\n");
708 coap_show_pdu(COAP_LOG_OSCORE, plain_pdu);
709 }
710
711 /*
712 * 8.1/8.3 Step 4.
713 * Encrypt the COSE object.
714 *
715 * Requires in COSE object as appropriate
716 * alg (already set)
717 * key (sender key)
718 * nonce (already set)
719 * aad (already set)
720 * plaintext
721 */
722 cose_encrypt0_set_key(cose, snd_ctx->sender_key);
723 cose_encrypt0_set_plaintext(cose, plain_pdu->token, plain_pdu->used_size);
724 dump_cose(cose, "Pre encrypt");
725 ciphertext_buffer =
726 coap_malloc_type(COAP_OSCORE_BUF, OSCORE_CRYPTO_BUFFER_SIZE);
727 if (ciphertext_buffer == NULL)
728 goto error;
729 ciphertext_len = cose_encrypt0_encrypt(cose,
730 ciphertext_buffer,
731 plain_pdu->used_size + AES_CCM_TAG);
732 if ((int)ciphertext_len <= 0) {
733 coap_log_warn("OSCORE: Encryption Failure, result code: %d \n",
734 (int)ciphertext_len);
735 goto error;
736 }
737 assert(ciphertext_len < OSCORE_CRYPTO_BUFFER_SIZE);
738
739 /* Add in OSCORE option (previously computed) */
740 if (!coap_insert_option(osc_pdu,
742 oscore_option_len,
743 oscore_option))
744 goto error;
745
746 /* Add now encrypted payload */
747 if (!coap_add_data(osc_pdu, ciphertext_len, ciphertext_buffer))
748 goto error;
749
750 coap_free_type(COAP_OSCORE_BUF, ciphertext_buffer);
751 ciphertext_buffer = NULL;
752
753 coap_delete_pdu_lkd(plain_pdu);
754 plain_pdu = NULL;
755
756 if (association && association->is_observe == 0)
757 oscore_delete_association(session, association);
758 association = NULL;
759
760 if (!coap_pdu_encode_header(osc_pdu, session->proto)) {
761 goto error;
762 }
763
764 /*
765 * Set up an association for handling a response if this is a request
766 */
767 if (coap_request) {
768 association = oscore_find_association(session, &pdu_token);
769 if (association) {
770 /* Refresh the association */
771 coap_delete_bin_const(association->nonce);
772 association->nonce =
773 coap_new_bin_const(cose->nonce.s, cose->nonce.length);
774 if (association->nonce == NULL)
775 goto error;
776 coap_delete_bin_const(association->aad);
777 association->aad = coap_new_bin_const(cose->aad.s, cose->aad.length);
778 if (association->aad == NULL)
779 goto error;
780 if (doing_observe && observe_value == 1) {
782 association->obs_partial_iv = association->partial_iv;
783 } else {
784 coap_delete_bin_const(association->partial_iv);
785 }
786 association->partial_iv =
788 if (association->partial_iv == NULL)
789 goto error;
790 coap_oscore_association_set_recipient_ctx(association, rcp_ctx);
791 coap_delete_pdu_lkd(association->sent_pdu);
792 if (session->b_2_step != COAP_OSCORE_B_2_NONE || association->just_set_up) {
793 size_t size;
794
795 association->sent_pdu = coap_pdu_duplicate_lkd(pdu, session,
796 pdu_token.length,
797 pdu_token.s, NULL,
799 if (association->sent_pdu == NULL)
800 goto error;
801 if (coap_get_data(pdu, &size, &data)) {
802 coap_add_data(association->sent_pdu, size, data);
803 }
804 association->just_set_up = 0;
805 } else {
806 association->sent_pdu = NULL;
807 }
808 } else if (!oscore_new_association(session,
809 pdu,
810 &pdu_token,
811 rcp_ctx,
812 &cose->aad,
813 &cose->nonce,
814 &cose->partial_iv,
815 doing_observe)) {
816 goto error;
817 }
818 }
819 return osc_pdu;
820
821error:
822 if (ciphertext_buffer)
823 coap_free_type(COAP_OSCORE_BUF, ciphertext_buffer);
824 coap_delete_pdu_lkd(osc_pdu);
825 coap_delete_pdu_lkd(plain_pdu);
826 return NULL;
827}
828
829static void
830build_and_send_error_pdu(coap_session_t *session,
831 coap_pdu_t *rcvd,
832 coap_pdu_code_t code,
833 const char *diagnostic,
834 uint8_t *echo_data,
835 coap_bin_const_t *kid_context,
836 int encrypt_oscore) {
837 coap_pdu_t *err_pdu = NULL;
838 coap_bin_const_t token;
839 int oscore_encryption = session->oscore_encryption;
840 unsigned char buf[4];
841
842 token = coap_pdu_get_token(rcvd);
845 code,
846 rcvd->mid,
847 token.length + 2 + 8 +
848 (diagnostic ? strlen(diagnostic) : 0));
849 if (!err_pdu)
850 return;
851 coap_add_token(err_pdu, token.length, token.s);
852 if (echo_data) {
853 coap_add_option_internal(err_pdu, COAP_OPTION_ECHO, 8, echo_data);
854 } else if (kid_context == NULL) {
857 coap_encode_var_safe(buf, sizeof(buf), 0),
858 buf);
859 }
860 if (diagnostic)
861 coap_add_data(err_pdu, strlen(diagnostic), (const uint8_t *)diagnostic);
862 session->oscore_encryption = encrypt_oscore;
863
864 if ((echo_data || kid_context) && encrypt_oscore) {
865 coap_pdu_t *osc_pdu;
866
867 osc_pdu =
868 coap_oscore_new_pdu_encrypted_lkd(session, err_pdu, kid_context,
869 echo_data ? 1 : 0);
870 if (!osc_pdu)
871 goto fail_resp;
872 session->oscore_encryption = 0;
873 coap_send_internal(session, osc_pdu, NULL);
874 coap_delete_pdu_lkd(err_pdu);
875 err_pdu = NULL;
876 } else {
877 coap_send_internal(session, err_pdu, NULL);
878 err_pdu = NULL;
879 }
880fail_resp:
881 session->oscore_encryption = oscore_encryption;
882 coap_delete_pdu_lkd(err_pdu);
883 return;
884}
885
888 oscore_ctx_t *oscore_ctx = oscore_derive_ctx_from_conf(oscore_conf);
889
890 if (oscore_ctx == NULL) {
891 goto error;
892 }
893
894 /* As all is stored in osc_ctx, oscore_conf is no longer needed */
895 coap_free_type(COAP_STRING, oscore_conf);
896
897 return oscore_ctx;
898error:
899 coap_delete_oscore_conf(oscore_conf);
900 return NULL;
901}
902
903/* pdu contains incoming message with encrypted COSE ciphertext payload
904 * function returns decrypted message
905 * and verifies signature, if present
906 * returns NULL when decryption,verification fails
907 */
910 coap_pdu_t *pdu) {
911 coap_pdu_t *decrypt_pdu = NULL;
912 coap_pdu_t *plain_pdu = NULL;
913 const uint8_t *osc_value; /* value of OSCORE option */
914 uint8_t osc_size; /* size of OSCORE OPTION */
915 coap_opt_iterator_t opt_iter;
916 coap_opt_t *opt = NULL;
917 cose_encrypt0_t cose[1];
918 oscore_ctx_t *osc_ctx = NULL;
919 uint8_t aad_buffer[AAD_BUF_LEN];
920 uint8_t nonce_buffer[13];
922 coap_bin_const_t nonce;
923 int pltxt_size = 0;
924 int got_resp_piv = 0;
925 int doing_resp_observe = 0;
926 uint8_t coap_request = COAP_PDU_IS_REQUEST(pdu);
927 coap_bin_const_t pdu_token;
928 uint8_t *st_encrypt;
929 size_t encrypt_len;
930 size_t tag_len;
931 oscore_recipient_ctx_t *rcp_ctx = NULL;
932 oscore_association_t *association = NULL;
933 uint8_t external_aad_buffer[100];
934 coap_bin_const_t external_aad;
935 oscore_sender_ctx_t *snd_ctx = NULL;
936#if COAP_CLIENT_SUPPORT
937 coap_pdu_t *sent_pdu = NULL;
938#endif /* COAP_CLIENT_SUPPORT */
939
940 opt = coap_check_option(pdu, COAP_OPTION_OSCORE, &opt_iter);
941 assert(opt);
942 if (opt == NULL)
943 return NULL;
944
945 if (session->context->p_osc_ctx == NULL && session->context->oscore_find_cb == NULL) {
946 coap_log_warn("OSCORE: Not enabled\n");
947 if (!coap_request)
950 session);
951 return NULL;
952 }
953
954 if (pdu->data == NULL) {
955 coap_log_warn("OSCORE: No protected payload\n");
956 if (!coap_request)
959 session);
960 return NULL;
961 }
962
963 osc_size = coap_opt_length(opt);
964 osc_value = coap_opt_value(opt);
965
966 cose_encrypt0_init(cose); /* clear cose memory */
967
968 /* PDU code will be filled in after decryption */
969 decrypt_pdu =
970 coap_pdu_init(pdu->type, 0, pdu->mid, pdu->used_size);
971 if (decrypt_pdu == NULL) {
972 if (!coap_request)
975 session);
976 goto error;
977 }
978
979 /* Copy across the Token */
980 pdu_token = coap_pdu_get_token(pdu);
981 coap_add_token(decrypt_pdu, pdu_token.length, pdu_token.s);
982
983 /*
984 * 8.2/8.4 Step 1.
985 * Copy outer options across, except E and OSCORE options
986 */
988 while ((opt = coap_option_next(&opt_iter))) {
989 switch ((coap_code_opt_num_t)opt_iter.number) {
990 /* 'E' options skipped */
992 case COAP_OPTION_ETAG:
1001 case COAP_OPTION_ACCEPT:
1004 case COAP_OPTION_BLOCK2:
1005 case COAP_OPTION_BLOCK1:
1006 case COAP_OPTION_SIZE2:
1008 case COAP_OPTION_SIZE1:
1010 case COAP_OPTION_ECHO:
1011 case COAP_OPTION_RTAG:
1012 /* OSCORE does not get copied across */
1013 case COAP_OPTION_OSCORE:
1014 break;
1015 /* 'U' options included */
1019 case COAP_OPTION_EDHOC:
1022 default:
1023 if (!coap_add_option_internal(decrypt_pdu,
1024 opt_iter.number,
1025 coap_opt_length(opt),
1026 coap_opt_value(opt))) {
1027 if (!coap_request)
1030 session);
1031 goto error;
1032 }
1033 break;
1034 }
1035 }
1036
1037 if (coap_request) {
1038 uint64_t incoming_seq;
1039 /*
1040 * 8.2 Step 2
1041 * Decompress COSE object
1042 * Get Recipient Context based on kid and optional kid_context
1043 */
1044 if (oscore_decode_option_value(osc_value, osc_size, cose) == 0) {
1045 coap_log_warn("OSCORE: OSCORE Option cannot be decoded.\n");
1046 build_and_send_error_pdu(session,
1047 pdu,
1048 COAP_RESPONSE_CODE(402),
1049 "Failed to decode COSE",
1050 NULL,
1051 NULL,
1052 0);
1053 goto error_no_ack;
1054 }
1055 osc_ctx = oscore_find_context(session,
1056 cose->key_id,
1057 &cose->kid_context,
1058 NULL,
1059 &rcp_ctx);
1060 if (!osc_ctx) {
1061 if (cose->kid_context.length > 0) {
1062 const uint8_t *ptr;
1063 size_t length;
1064 /* Appendix B.2 protocol check - Is the recipient key_id known */
1065 osc_ctx = oscore_find_context(session,
1066 cose->key_id,
1067 NULL,
1068 session->oscore_r2 != 0 ? (uint8_t *)&session->oscore_r2 : NULL,
1069 &rcp_ctx);
1070 ptr = cose->kid_context.s;
1071 length = cose->kid_context.length;
1072 if (ptr && osc_ctx && osc_ctx->rfc8613_b_2) {
1073 /* Processing Appendix B.2 protocol */
1074 /* Need to CBOR unwrap kid_context */
1075 coap_bin_const_t kid_context;
1076
1077 kid_context.length = oscore_cbor_get_element_size(&ptr, &length);
1078 if (kid_context.length > length)
1079 goto error;
1080 /* This has to fit into an OSCORE option max 255.
1081 * Initial byte, kid_context size, partial IV size and kid size have to be there
1082 */
1083 if (kid_context.length >= 255 - 1 - 1 - cose->partial_iv.length - cose->key_id.length)
1084 goto error;
1085 kid_context.s = ptr;
1086 cose_encrypt0_set_kid_context(cose, (coap_bin_const_t *)&kid_context);
1087
1088 if (session->oscore_r2 != 0) {
1089 /* B.2 step 4 */
1091 cose->kid_context.length);
1092
1093 if (kc == NULL)
1094 goto error;
1095
1096 session->b_2_step = COAP_OSCORE_B_2_STEP_4;
1097 coap_log_oscore("Appendix B.2 server step 4 (R2 || R3)\n");
1098 oscore_update_ctx(osc_ctx, kc);
1099 } else {
1100 session->b_2_step = COAP_OSCORE_B_2_STEP_2;
1101 coap_log_oscore("Appendix B.2 server step 2 (ID1)\n");
1102 osc_ctx = oscore_duplicate_ctx(session->context,
1103 osc_ctx,
1104 osc_ctx->sender_context->sender_id,
1105 &cose->key_id,
1106 &cose->kid_context);
1107 if (osc_ctx == NULL)
1108 goto error;
1109 /*
1110 * Complete the Verify (B.2 step 2)
1111 * before sending back the response
1112 */
1113 rcp_ctx = osc_ctx->recipient_chain;
1114 }
1115 } else {
1116 osc_ctx = NULL;
1117 }
1118 }
1119 } else if (session->b_2_step != COAP_OSCORE_B_2_NONE) {
1120 session->b_2_step = COAP_OSCORE_B_2_NONE;
1121 coap_log_oscore("Appendix B.2 server finished\n");
1122 }
1123 if (!osc_ctx) {
1124 coap_log_err("OSCORE: Security Context not found\n");
1125 oscore_log_hex_value(COAP_LOG_OSCORE, "key_id", &cose->key_id);
1126 oscore_log_hex_value(COAP_LOG_OSCORE, "kid_context", &cose->kid_context);
1127 build_and_send_error_pdu(session,
1128 pdu,
1129 COAP_RESPONSE_CODE(401),
1130 "Security context not found",
1131 NULL,
1132 NULL,
1133 0);
1134 goto error_no_ack;
1135 }
1136 /* to be used for encryption of returned response later */
1137 coap_oscore_session_set_recipient_ctx(session, rcp_ctx);
1138 snd_ctx = osc_ctx->sender_context;
1139
1140 /*
1141 * 8.2 Step 3.
1142 * Verify Partial IV is not duplicated.
1143 *
1144 * Requires in COSE object as appropriate
1145 * partial_iv (as received)
1146 */
1147 if ((rcp_ctx->initial_state == 0 || osc_ctx->rfc8613_b_1_2 == 0) &&
1148 !oscore_validate_sender_seq(rcp_ctx, cose)) {
1149 coap_log_warn("OSCORE: Replayed or old message\n");
1150 build_and_send_error_pdu(session,
1151 pdu,
1152 COAP_RESPONSE_CODE(401),
1153 "Replay detected",
1154 NULL,
1155 NULL,
1156 0);
1157 goto error_no_ack;
1158 }
1159 if (rcp_ctx->initial_state == 1) {
1160 incoming_seq =
1162 rcp_ctx->last_seq = incoming_seq;
1163 } else if (session->context->oscore_update_seq_num_cb != NULL) {
1164 /* notify custom credential storage */
1165 coap_lock_callback(session->context->oscore_update_seq_num_cb(session,
1166 rcp_ctx->recipient_id,
1167 osc_ctx->id_context,
1168 rcp_ctx->last_seq,
1169 rcp_ctx->sliding_window));
1170 }
1171 } else { /* !coap_request */
1172 /*
1173 * 8.4 Step 2
1174 * Decompress COSE object
1175 * Get Recipient Context based on token
1176 */
1177 if (oscore_decode_option_value(osc_value, osc_size, cose) == 0) {
1178 coap_log_warn("OSCORE: OSCORE Option cannot be decoded.\n");
1181 session);
1182 goto error;
1183 }
1184 got_resp_piv = cose->partial_iv.length ? 1 : 0;
1185
1186 association = oscore_find_association(session, &pdu_token);
1187 if (association) {
1188 rcp_ctx = association->recipient_ctx;
1189 osc_ctx = rcp_ctx->osc_ctx;
1190 snd_ctx = osc_ctx->sender_context;
1191#if COAP_CLIENT_SUPPORT
1192 sent_pdu = association->sent_pdu;
1193 if (session->b_2_step != COAP_OSCORE_B_2_NONE) {
1194 const uint8_t *ptr = cose->kid_context.s;
1195 size_t length = cose->kid_context.length;
1196
1197 if (ptr) {
1198 /* Need to CBOR unwrap kid_context */
1199 coap_bin_const_t kid_context;
1200
1201 kid_context.length = oscore_cbor_get_element_size(&ptr, &length);
1202 if (kid_context.length > length)
1203 goto error;
1204 /* This has to fit into an OSCORE option max 255.
1205 * Initial byte, kid_context size, partial IV size and kid size have to be there
1206 */
1207 if (kid_context.length >= 255 - 1 - 1 - cose->partial_iv.length - cose->key_id.length)
1208 goto error;
1209 kid_context.s = ptr;
1210 cose_encrypt0_set_kid_context(cose, &kid_context);
1211 }
1212 if (ptr && !coap_binary_equal(osc_ctx->id_context, &cose->kid_context)) {
1213 /* If Appendix B.2 step 3 is in operation */
1214 /* Need to update Security Context with new (R2 || ID1) ID Context */
1216 osc_ctx->id_context->length);
1217
1218 if (kc == NULL) {
1221 session);
1222 goto error;
1223 }
1224
1225 memcpy(kc->s, cose->kid_context.s, cose->kid_context.length);
1226 memcpy(&kc->s[cose->kid_context.length],
1227 osc_ctx->id_context->s,
1228 osc_ctx->id_context->length);
1229
1230 session->b_2_step = COAP_OSCORE_B_2_STEP_3;
1231 coap_log_oscore("Appendix B.2 client step 3 (R2 || ID1)\n");
1232 oscore_update_ctx(osc_ctx, (coap_bin_const_t *)kc);
1233 } else {
1234 session->b_2_step = COAP_OSCORE_B_2_STEP_5;
1235 coap_log_oscore("Appendix B.2 client step 5 (R2 || R3)\n");
1236 }
1237 }
1238#endif /* COAP_CLIENT_SUPPORT */
1239 } else {
1240 coap_log_crit("OSCORE: Security Context association not found\n");
1243 session);
1244 goto error;
1245 }
1246 }
1247
1248 cose_encrypt0_set_alg(cose, osc_ctx->aead_alg);
1249
1250 if (coap_request) {
1251 /*
1252 * RFC8613 8.2 Step 4.
1253 * Compose the External AAD and then AAD
1254 *
1255 * OSCORE_option requires
1256 * partial_iv (cose partial_iv)
1257 * kid_context (cose kid_context)
1258 * key_id (cose key_id)
1259 * group_flag
1260 *
1261 * Non Group requires the following
1262 * RFC8613 5.4
1263 * oscore_version 1
1264 * algorithms [
1265 * aead_alg (osc_ctx)
1266 * ]
1267 * request_kid (request key_id using cose)
1268 * request_piv (request partial_iv using cose)
1269 * options (none at present)
1270 *
1271 * Note: No I options at present
1272 */
1273
1274 /* External AAD */
1275 external_aad.s = external_aad_buffer;
1276 external_aad.length = oscore_prepare_e_aad(osc_ctx,
1277 cose,
1278 osc_value,
1279 osc_size,
1280 NULL,
1281 external_aad_buffer,
1282 sizeof(external_aad_buffer));
1283 cose_encrypt0_set_external_aad(cose, &external_aad);
1284
1285 /* AAD */
1286 aad.s = aad_buffer;
1287 aad.length = oscore_prepare_aad(external_aad_buffer,
1288 external_aad.length,
1289 aad_buffer,
1290 sizeof(aad_buffer));
1291 assert(aad.length < AAD_BUF_LEN);
1292 cose_encrypt0_set_aad(cose, &aad);
1293
1294 /*
1295 * RFC8613 8.2 Step 5.
1296 * Compute the AEAD nonce.
1297 *
1298 * Requires in COSE object as appropriate
1299 * key_id (kid) (Recipient ID)
1300 * partial_iv (as received in request)
1301 * common_iv (already in osc_ctx)
1302 */
1303 nonce.s = nonce_buffer;
1304 nonce.length = 13;
1305 oscore_generate_nonce(cose, osc_ctx, nonce_buffer, 13);
1306 cose_encrypt0_set_nonce(cose, &nonce);
1307 /*
1308 * Set up an association for use in the response
1309 */
1310 association = oscore_find_association(session, &pdu_token);
1311 if (association) {
1312 /* Refresh the association */
1313 coap_delete_bin_const(association->nonce);
1314 association->nonce =
1315 coap_new_bin_const(cose->nonce.s, cose->nonce.length);
1316 if (association->nonce == NULL)
1317 goto error;
1318 coap_delete_bin_const(association->partial_iv);
1319 association->partial_iv =
1321 if (association->partial_iv == NULL)
1322 goto error;
1323 coap_delete_bin_const(association->aad);
1324 association->aad = coap_new_bin_const(cose->aad.s, cose->aad.length);
1325 if (association->aad == NULL)
1326 goto error;
1327 coap_oscore_association_set_recipient_ctx(association, rcp_ctx);
1328 /* So association is not released when handling decrypt */
1329 association = NULL;
1330 } else if (!oscore_new_association(session,
1331 NULL,
1332 &pdu_token,
1333 rcp_ctx,
1334 &cose->aad,
1335 &cose->nonce,
1336 &cose->partial_iv,
1337 0)) {
1338 goto error;
1339 }
1340 /* So association is not released when handling decrypt */
1341 association = NULL;
1342 } else { /* ! coap_request */
1343 /* Need to do nonce before AAD because of different partial_iv */
1344 /*
1345 * 8.4 Step 4.
1346 * Compose the AEAD nonce.
1347 */
1348 cose_encrypt0_set_key_id(cose, rcp_ctx->recipient_id);
1349 if (cose->partial_iv.length == 0) {
1350 cose_encrypt0_set_partial_iv(cose, association->partial_iv);
1351 cose_encrypt0_set_nonce(cose, association->nonce);
1352 } else {
1353 uint64_t last_seq;
1354
1355 if ((rcp_ctx->initial_state == 0 || osc_ctx->rfc8613_b_1_2 == 0) &&
1356 !oscore_validate_sender_seq(rcp_ctx, cose)) {
1357 coap_log_warn("OSCORE: Replayed or old message\n");
1358 goto error;
1359 }
1360 last_seq =
1362 if (rcp_ctx->last_seq>= OSCORE_SEQ_MAX) {
1363 coap_log_warn("OSCORE Replay protection, SEQ larger than SEQ_MAX.\n");
1364 goto error;
1365 }
1366 if (last_seq > rcp_ctx->last_seq)
1367 rcp_ctx->last_seq = last_seq;
1368 /*
1369 * Requires in COSE object as appropriate
1370 * kid (set above)
1371 * partial_iv (as received)
1372 * common_iv (already in osc_ctx)
1373 */
1374 oscore_generate_nonce(cose, osc_ctx, nonce_buffer, 13);
1375 nonce.s = nonce_buffer;
1376 nonce.length = 13;
1377 cose_encrypt0_set_nonce(cose, &nonce);
1378 }
1379#ifdef OSCORE_EXTRA_DEBUG
1380 dump_cose(cose, "!req post set nonce");
1381#endif /* OSCORE_EXTRA_DEBUG */
1382 /*
1383 * 8.4 Step 3.
1384 * Compose the External AAD and then AAD
1385 *
1386 * OSCORE_option requires
1387 * partial_iv (cose partial_iv)
1388 * kid_context (cose kid_context)
1389 * key_id (cose key_id)
1390 * group_flag
1391 *
1392 * Non Group requires the following
1393 * RFC8613 5.4
1394 * oscore_version 1
1395 * algorithms [
1396 * aead_alg (osc_ctx)
1397 * ]
1398 * request_kid (request key_id using cose)
1399 * request_piv (request partial_iv using cose)
1400 * options (none at present)
1401 *
1402 * Note: No I options at present
1403 */
1404
1405 /* External AAD */
1406 cose_encrypt0_set_key_id(cose, snd_ctx->sender_id);
1407 if (association->is_observe && association->obs_partial_iv && got_resp_piv) {
1408 cose_encrypt0_set_partial_iv(cose, association->obs_partial_iv);
1409 } else {
1410 cose_encrypt0_set_partial_iv(cose, association->partial_iv);
1411 }
1412#ifdef OSCORE_EXTRA_DEBUG
1413 dump_cose(cose, "!req pre aad");
1414#endif /* OSCORE_EXTRA_DEBUG */
1415 external_aad.s = external_aad_buffer;
1416 external_aad.length = oscore_prepare_e_aad(osc_ctx,
1417 cose,
1418 NULL,
1419 0,
1420 NULL,
1421 external_aad_buffer,
1422 sizeof(external_aad_buffer));
1423 cose_encrypt0_set_external_aad(cose, &external_aad);
1424
1425 /* AAD */
1426 aad.s = aad_buffer;
1427 aad.length = oscore_prepare_aad(external_aad_buffer,
1428 external_aad.length,
1429 aad_buffer,
1430 sizeof(aad_buffer));
1431 assert(aad.length < AAD_BUF_LEN);
1432 cose_encrypt0_set_aad(cose, &aad);
1433#ifdef OSCORE_EXTRA_DEBUG
1434 dump_cose(cose, "!req post set aad");
1435#endif /* OSCORE_EXTRA_DEBUG */
1436 }
1437
1438 /*
1439 * 8.2 Step 6 / 8.4 Step 5.
1440 * Decrypt the COSE object.
1441 *
1442 * Requires in COSE object as appropriate
1443 * alg (already set)
1444 * key
1445 * nonce (already set)
1446 * aad (already set)
1447 * ciphertext
1448 */
1449 st_encrypt = pdu->data;
1450 encrypt_len = pdu->used_size - (pdu->data - pdu->token);
1451 if (encrypt_len <= 0) {
1452 coap_log_warn("OSCORE: No protected payload\n");
1453 if (!coap_request)
1456 session);
1457 goto error;
1458 }
1459 cose_encrypt0_set_key(cose, rcp_ctx->recipient_key);
1460 cose_encrypt0_set_ciphertext(cose, st_encrypt, encrypt_len);
1461
1462 tag_len = cose_tag_len(cose->alg);
1463 /* Decrypt into plain_pdu, so code (token), options and data are in place */
1464 plain_pdu = coap_pdu_init(pdu->type, 0, 0, encrypt_len /* - tag_len */);
1465 if (plain_pdu == NULL) {
1466 if (!coap_request)
1469 session);
1470 goto error;
1471 }
1472
1473 /* need the tag_len on the end for TinyDTLS to do its work - yuk */
1474 if (!coap_pdu_resize(plain_pdu, encrypt_len /* - tag_len */)) {
1475 if (!coap_request)
1478 session);
1479 goto error;
1480 }
1481
1482 /* Account for 1 byte 'code' used as token */
1483 plain_pdu->e_token_length = 1;
1484 plain_pdu->actual_token.length = 1;
1485 /* Account for the decrypted data */
1486 plain_pdu->used_size = encrypt_len - tag_len;
1487
1488 dump_cose(cose, "Pre decrypt");
1489 pltxt_size =
1490 cose_encrypt0_decrypt(cose, plain_pdu->token, encrypt_len - tag_len);
1491 if (pltxt_size <= 0) {
1492 coap_log_warn("OSCORE: Decryption Failure, result code: %d \n",
1493 (int)pltxt_size);
1494 if (coap_request) {
1495 build_and_send_error_pdu(session,
1496 pdu,
1497 COAP_RESPONSE_CODE(400),
1498 "Decryption failed",
1499 NULL,
1500 NULL,
1501 0);
1502 oscore_roll_back_seq(rcp_ctx);
1503 goto error_no_ack;
1504 } else {
1507 session);
1508 }
1509 goto error;
1510 }
1511
1512 assert((size_t)pltxt_size < pdu->alloc_size + pdu->max_hdr_size);
1513
1514 /* Appendix B.2 Trap */
1515 if (session->b_2_step == COAP_OSCORE_B_2_STEP_2) {
1516 /* Need to update Security Context with new (R2 || ID1) ID Context */
1517 coap_binary_t *kc =
1518 coap_new_binary(sizeof(session->oscore_r2) + cose->kid_context.length);
1519 coap_bin_const_t oscore_r2;
1520
1521 if (kc == NULL) {
1522 if (!coap_request)
1525 session);
1526 goto error;
1527 }
1528
1529 coap_prng_lkd(&session->oscore_r2, sizeof(session->oscore_r2));
1530 memcpy(kc->s, &session->oscore_r2, sizeof(session->oscore_r2));
1531 memcpy(&kc->s[sizeof(session->oscore_r2)],
1532 cose->kid_context.s,
1533 cose->kid_context.length);
1534
1535 coap_log_oscore("Appendix B.2 server step 2 (R2 || ID1)\n");
1536 oscore_update_ctx(osc_ctx, (coap_bin_const_t *)kc);
1537
1538 oscore_r2.length = sizeof(session->oscore_r2);
1539 oscore_r2.s = (const uint8_t *)&session->oscore_r2;
1540 coap_log_oscore("Appendix B.2 server step 2 plain response\n");
1541 build_and_send_error_pdu(session,
1542 pdu,
1543 COAP_RESPONSE_CODE(401),
1544 NULL,
1545 NULL,
1546 &oscore_r2,
1547 1);
1548 goto error_no_ack;
1549 }
1550#if COAP_CLIENT_SUPPORT
1551 if (session->b_2_step == COAP_OSCORE_B_2_STEP_3) {
1552 coap_log_oscore("Appendix B.2 client step 3 (R2 || R3)\n");
1553 coap_pdu_encode_header(plain_pdu, session->proto);
1554 plain_pdu->actual_token.s = plain_pdu->token;
1555 plain_pdu->code = plain_pdu->token[0];
1556 if (plain_pdu->code != COAP_RESPONSE_CODE(401)) {
1557 coap_log_warn("OSCORE Appendix B.2: Expected 4.01 response\n");
1558 }
1559 /* Skip the options */
1560 coap_option_iterator_init(plain_pdu, &opt_iter, COAP_OPT_ALL);
1561 while (coap_option_next(&opt_iter)) {
1562 }
1563 if (opt_iter.length > 0 && opt_iter.next_option &&
1564 opt_iter.next_option[0] == COAP_PAYLOAD_START) {
1565 plain_pdu->data = &opt_iter.next_option[1];
1566 }
1567 coap_log_oscore("Inner Response PDU (plaintext)\n");
1568 coap_show_pdu(COAP_LOG_OSCORE, plain_pdu);
1569 /*
1570 * Need to update Security Context with new (R2 || R3) ID Context
1571 * and retransmit the request
1572 */
1574
1575 if (kc == NULL) {
1576 if (!coap_request)
1579 session);
1580 goto error;
1581 }
1582 memcpy(kc->s, cose->kid_context.s, cose->kid_context.length);
1583 coap_prng_lkd(&kc->s[cose->kid_context.length], 8);
1584
1585 oscore_update_ctx(osc_ctx, (coap_bin_const_t *)kc);
1586
1588 session,
1589 &pdu->actual_token);
1590 if (session->con_active)
1591 session->con_active--;
1592 coap_send_ack_lkd(session, pdu);
1593 if (sent_pdu) {
1594 coap_log_oscore("Appendix B.2 retransmit pdu\n");
1595 if (!session->b_2_retransmit_token) {
1596 session->b_2_retransmit_token = coap_new_str_const(sent_pdu->actual_token.s,
1597 sent_pdu->actual_token.length);
1598 }
1599 if (coap_retransmit_oscore_pdu(session, sent_pdu, NULL) ==
1601 goto error_no_ack;
1602 }
1603 goto error_no_ack;
1604 }
1605#endif /* COAP_CLIENT_SUPPORT */
1606
1607#if COAP_SERVER_SUPPORT
1608 /* Appendix B.1.2 request Trap */
1609 if (coap_request && osc_ctx->rfc8613_b_1_2) {
1610 if (rcp_ctx->initial_state == 1) {
1611 opt = coap_check_option(plain_pdu, COAP_OPTION_ECHO, &opt_iter);
1612 if (opt) {
1613 /* Verify Client is genuine */
1614 if (coap_opt_length(opt) == 8 &&
1615 memcmp(coap_opt_value(opt), rcp_ctx->echo_value, 8) == 0) {
1616 /* This clears rcp_ctx->initial_state */
1617 if (!oscore_validate_sender_seq(rcp_ctx, cose)) {
1618 coap_log_warn("OSCORE: Replayed or old message\n");
1619 build_and_send_error_pdu(session,
1620 pdu,
1621 COAP_RESPONSE_CODE(401),
1622 "Replay detected",
1623 NULL,
1624 NULL,
1625 0);
1626 goto error_no_ack;
1627 } else {
1628 if (session->context->oscore_update_seq_num_cb != NULL) {
1629 /* notify echo challenge changed */
1630 coap_lock_callback(session->context->oscore_update_seq_num_cb(session,
1631 rcp_ctx->recipient_id,
1632 osc_ctx->id_context,
1633 rcp_ctx->last_seq,
1634 rcp_ctx->sliding_window));
1635 }
1636 }
1637 } else
1638 goto error;
1639 } else {
1640 /* RFC 8163 Appendix B.1.2 */
1641 if (session->b_2_step == COAP_OSCORE_B_2_STEP_4) {
1642 session->b_2_step = COAP_OSCORE_B_2_NONE;
1643 coap_log_oscore("Appendix B.2 server finished\n");
1644 }
1645 coap_prng_lkd(rcp_ctx->echo_value, sizeof(rcp_ctx->echo_value));
1646 coap_log_oscore("Appendix B.1.2 server plain response\n");
1647
1648 build_and_send_error_pdu(session,
1649 pdu,
1650 COAP_RESPONSE_CODE(401),
1651 NULL,
1652 rcp_ctx->echo_value,
1653 NULL,
1654 1);
1655 goto error_no_ack;
1656 }
1657 }
1658 }
1659#endif /* COAP_SERVER_SUPPORT */
1660
1661 /*
1662 * 8.2 Step 7 / 8.4 Step 6.
1663 * Add decrypted Code, options and payload
1664 * [OSCORE option not copied across previously]
1665 */
1666
1667 /* PDU code is pseudo plain_pdu token */
1668 decrypt_pdu->code = plain_pdu->token[0];
1669
1670 /* Copy inner decrypted options across */
1671 coap_option_iterator_init(plain_pdu, &opt_iter, COAP_OPT_ALL);
1672 while ((opt = coap_option_next(&opt_iter))) {
1673 size_t len;
1674 size_t bias;
1675
1676 switch (opt_iter.number) {
1677 case COAP_OPTION_OSCORE:
1678 break;
1680 if (!coap_request) {
1681 bias = cose->partial_iv.length > 3 ? cose->partial_iv.length - 3 : 0;
1682 len = cose->partial_iv.length > 3 ? 3 : cose->partial_iv.length;
1683 /* Make Observe option reflect last 3 bytes of partial_iv */
1685 decrypt_pdu,
1686 opt_iter.number,
1687 len,
1688 cose->partial_iv.s ? &cose->partial_iv.s[bias] : NULL)) {
1691 session);
1692 goto error;
1693 }
1694 doing_resp_observe = 1;
1695 break;
1696 }
1697 association = oscore_find_association(session, &pdu_token);
1698 if (association) {
1699 association->is_observe = 1;
1700 association = NULL;
1701 }
1702 /* Fall Through */
1703 default:
1704 if (!coap_insert_option(decrypt_pdu,
1705 opt_iter.number,
1706 coap_opt_length(opt),
1707 coap_opt_value(opt))) {
1708 if (!coap_request)
1711 session);
1712 goto error;
1713 }
1714 break;
1715 }
1716 }
1717 if (!coap_request && !doing_resp_observe) {
1718 if (association) {
1719 association->is_observe = 0;
1720 }
1721 }
1722 /* Need to copy across any data */
1723 if (opt_iter.length > 0 && opt_iter.next_option &&
1724 opt_iter.next_option[0] == COAP_PAYLOAD_START) {
1725 plain_pdu->data = &opt_iter.next_option[1];
1726 if (!coap_add_data(decrypt_pdu,
1727 plain_pdu->used_size -
1728 (plain_pdu->data - plain_pdu->token),
1729 plain_pdu->data)) {
1730 if (!coap_request)
1733 session);
1734 goto error;
1735 }
1736 }
1737 coap_delete_pdu_lkd(plain_pdu);
1738 plain_pdu = NULL;
1739
1740 /* Make sure headers are correctly set up */
1741 if (!coap_pdu_encode_header(decrypt_pdu, session->proto)) {
1742 if (!coap_request)
1745 session);
1746 goto error;
1747 }
1748
1749 if (session->b_2_step != COAP_OSCORE_B_2_NONE) {
1750 session->b_2_step = COAP_OSCORE_B_2_NONE;
1751 coap_log_oscore("Appendix B.2 client finished\n");
1752 if (session->b_2_retransmit_token) {
1753 coap_update_token(decrypt_pdu, session->b_2_retransmit_token->length,
1754 session->b_2_retransmit_token->s);
1755 coap_delete_str_const(session->b_2_retransmit_token);
1756 session->b_2_retransmit_token = NULL;
1757 }
1758 }
1759#if COAP_CLIENT_SUPPORT
1760 if (decrypt_pdu->code == COAP_RESPONSE_CODE(401) &&
1761 (opt = coap_check_option(decrypt_pdu, COAP_OPTION_ECHO, &opt_iter))) {
1762 /* Server is requesting Echo refresh check */
1764 session,
1765 &pdu->actual_token);
1766 if (session->con_active)
1767 session->con_active--;
1768 if (!sent_pdu) {
1769 coap_lg_crcv_t *lg_crcv = coap_find_lg_crcv(session, pdu);
1770
1771 if (lg_crcv)
1772 sent_pdu = lg_crcv->sent_pdu;
1773 }
1774 if (sent_pdu) {
1775 coap_send_ack_lkd(session, pdu);
1776 coap_log_debug("PDU requesting re-transmit\n");
1777 coap_show_pdu(COAP_LOG_DEBUG, decrypt_pdu);
1778 coap_log_oscore("RFC9175 retransmit pdu\n");
1779 /* Do not care if this fails */
1780 if (coap_retransmit_oscore_pdu(session, sent_pdu, opt) != COAP_INVALID_MID) {
1781 session->doing_b_1_2 = 1;
1782 }
1783 goto error_no_ack;
1784 }
1785 } else if (session->doing_b_1_2) {
1786 coap_lg_crcv_t *lg_crcv = coap_find_lg_crcv(session, pdu);
1787
1788 if (lg_crcv) {
1789 if (!coap_binary_equal(&decrypt_pdu->actual_token, lg_crcv->app_token)) {
1790 coap_update_token(decrypt_pdu, lg_crcv->app_token->length, lg_crcv->app_token->s);
1791 }
1792 }
1793 session->doing_b_1_2 = 0;
1794 }
1795#endif /* COAP_CLIENT_SUPPORT */
1796 if (association && association->is_observe == 0)
1797 oscore_delete_association(session, association);
1798 return decrypt_pdu;
1799
1800error:
1801 coap_send_ack_lkd(session, pdu);
1802error_no_ack:
1803 if (association && association->is_observe == 0)
1804 oscore_delete_association(session, association);
1805 coap_delete_pdu_lkd(decrypt_pdu);
1806 coap_delete_pdu_lkd(plain_pdu);
1807 return NULL;
1808}
1809
1810typedef enum {
1811 COAP_ENC_ASCII = 0x001,
1812 COAP_ENC_HEX = 0x002,
1813 COAP_ENC_CONFIG = 0x0200,
1814 COAP_ENC_INTEGER = 0x400,
1815 COAP_ENC_TEXT = 0x800,
1816 COAP_ENC_BOOL = 0x1000,
1817 COAP_ENC_UNSIGNED64 = 0x2000,
1818 COAP_ENC_LAST
1819} coap_oscore_coding_t;
1820
1821#undef TEXT_MAPPING
1822#define TEXT_MAPPING(t, v) \
1823 { { sizeof(#t)-1, (const uint8_t *)#t }, v }
1824
1825static struct coap_oscore_encoding_t {
1826 coap_str_const_t name;
1827 coap_oscore_coding_t encoding;
1828} oscore_encoding[] = {
1829 TEXT_MAPPING(ascii, COAP_ENC_ASCII),
1830 TEXT_MAPPING(hex, COAP_ENC_HEX),
1831 TEXT_MAPPING(config, COAP_ENC_CONFIG),
1832 TEXT_MAPPING(integer, COAP_ENC_INTEGER),
1833 TEXT_MAPPING(unsigned64, COAP_ENC_UNSIGNED64),
1834 TEXT_MAPPING(text, COAP_ENC_TEXT),
1835 TEXT_MAPPING(bool, COAP_ENC_BOOL),
1836 {{0, NULL}, COAP_ENC_LAST}
1837};
1838
1839typedef struct {
1840 coap_oscore_coding_t encoding;
1841 const char *encoding_name;
1842 union {
1843 int value_int;
1844 uint64_t value_int64;
1845 coap_bin_const_t *value_bin;
1846 coap_str_const_t value_str;
1847 } u;
1848} oscore_value_t;
1849
1850/*
1851 * Return 1 if hex character
1852 * 0 not hex character
1853 */
1854static int
1855hex_to_char(char c, uint8_t *value) {
1856 if ('a' <= c && c <= 'f') {
1857 *value = c - 'a' + 10;
1858 } else if ('A' <= c && c <= 'F') {
1859 *value = c - 'A' + 10;
1860 } else if ('0' <= c && c <= '9') {
1861 *value = c - '0';
1862 } else {
1863 *value = 0;
1864 return 0;
1865 }
1866 return 1;
1867}
1868
1869/* Parse the hex into binary */
1870static coap_bin_const_t *
1871parse_hex_bin(const char *begin, const char *end) {
1872 coap_binary_t *binary = NULL;
1873 size_t i;
1874 size_t o = 0;
1875
1876 binary = coap_new_binary((end - begin) / 2);
1877 if (binary == NULL)
1878 goto bad_entry;
1879 for (i = 0; (i < (size_t)(end - begin)); i++) {
1880 while ((i < (size_t)(end - begin)) &&
1881 (begin[i] == '\r' || begin[i] == '\n' || begin[i] == ' ')) {
1882 i++;
1883 }
1884 if (i == (size_t)(end - begin))
1885 break;
1886 if (isxdigit((uint8_t)begin[i])) {
1887 uint8_t value;
1888
1889 if (!hex_to_char(begin[i], &value)) {
1890 goto bad_entry;
1891 }
1892 binary->s[o] = value << 4;
1893 i++;
1894 while ((i < (size_t)(end - begin)) &&
1895 (begin[i] == '\r' || begin[i] == '\n' || begin[i] == ' ')) {
1896 i++;
1897 }
1898 if (i == (size_t)(end - begin))
1899 goto bad_entry;
1900 if (!hex_to_char(begin[i], &value)) {
1901 goto bad_entry;
1902 }
1903 binary->s[o++] += value;
1904 } else {
1905 break;
1906 }
1907 }
1908 if (i != (size_t)(end - begin))
1909 goto bad_entry;
1910 return (coap_bin_const_t *)binary;
1911
1912bad_entry:
1913 coap_delete_binary(binary);
1914 return NULL;
1915}
1916
1917/*
1918 * Break up each OSCORE Configuration line entry into the 3 parts which
1919 * are comma separated
1920 *
1921 * keyword,encoding,value
1922 */
1923static int
1924get_split_entry(const char **start,
1925 size_t size,
1926 coap_str_const_t *keyword,
1927 oscore_value_t *value,
1928 cose_curve_t sign_curve) {
1929 const char *begin = *start;
1930 const char *keep_start = *start;
1931 const char *end;
1932 const char *kend;
1933 const char *tend;
1934 const char *split;
1935 size_t i;
1936 size_t len;
1937 (void)sign_curve;
1938
1939retry:
1940 kend = end = memchr(begin, '\n', size);
1941 if (end == NULL)
1942 return 0;
1943
1944 /* Check for multi-line */
1945 if ((tend = memchr(begin, '"', end - begin))) {
1946 /* See if " terminator is on the same line */
1947 if (!memchr(tend + 1, '"', end - tend -1)) {
1948 /* Over multiple lines */
1949 kend = end = memchr(end, '"', size - (end - begin));
1950 if (end == NULL)
1951 return 0;
1952 kend = end = memchr(kend, '\n', size - (kend - begin));
1953 if (end == NULL)
1954 return 0;
1955 }
1956 }
1957
1958 /* Track beginning of next line */
1959 *start = end + 1;
1960 if (end > begin && end[-1] == '\r')
1961 end--;
1962
1963 if (begin[0] == '#' || (end - begin) == 0) {
1964 /* Skip comment / blank line */
1965 size -= kend - begin + 1;
1966 begin = *start;
1967 keep_start = *start;
1968 goto retry;
1969 }
1970
1971 /* Get in the keyword */
1972 split = memchr(begin, ',', end - begin);
1973 if (split == NULL)
1974 goto bad_entry;
1975
1976 keyword->s = (const uint8_t *)begin;
1977 keyword->length = split - begin;
1978
1979 begin = split + 1;
1980 if ((end - begin) == 0)
1981 goto bad_entry;
1982 /* Get in the encoding */
1983 split = memchr(begin, ',', end - begin);
1984 if (split == NULL)
1985 goto bad_entry;
1986
1987 for (i = 0; oscore_encoding[i].name.s; i++) {
1988 coap_str_const_t temp = { split - begin, (const uint8_t *)begin };
1989
1990 if (coap_string_equal(&temp, &oscore_encoding[i].name)) {
1991 value->encoding = oscore_encoding[i].encoding;
1992 value->encoding_name = (const char *)oscore_encoding[i].name.s;
1993 break;
1994 }
1995 }
1996 if (oscore_encoding[i].name.s == NULL)
1997 goto bad_entry;
1998
1999 begin = split + 1;
2000 if ((end - begin) == 0)
2001 goto bad_entry;
2002 /* Get in the keyword's value */
2003 if (value->encoding == COAP_ENC_CONFIG && begin[0] == '\'') {
2004 split = memchr(&begin[1], '\'', size - (begin - keep_start) - 1);
2005 if (split == NULL)
2006 goto bad_entry;
2007 end = memchr(split, '\n', size - (split - keep_start));
2008 if (end == NULL)
2009 return 0;
2010 *start = end + 1;
2011 end = split;
2012 begin++;
2013 } else {
2014 if (begin[0] == '"') {
2015 split = memchr(&begin[1], '"', end - split - 1);
2016 if (split == NULL)
2017 goto bad_entry;
2018 end = split;
2019 begin++;
2020 }
2021 }
2022 switch (value->encoding) {
2023 case COAP_ENC_CONFIG:
2024 case COAP_ENC_ASCII:
2025 value->u.value_bin =
2026 coap_new_bin_const((const uint8_t *)begin, end - begin);
2027 if (value->u.value_bin == NULL)
2028 goto bad_entry;
2029 break;
2030 case COAP_ENC_HEX:
2031 /* Parse the hex into binary */
2032 value->u.value_bin = parse_hex_bin(begin, end);
2033 if (value->u.value_bin == NULL)
2034 goto bad_entry;
2035 break;
2036 case COAP_ENC_INTEGER:
2037 value->u.value_int = atoi(begin);
2038 break;
2039 case COAP_ENC_UNSIGNED64: {
2040 value->u.value_int64 = strtoull(begin, NULL, 10);
2041 break;
2042 }
2043 case COAP_ENC_TEXT:
2044 value->u.value_str.s = (const uint8_t *)begin;
2045 value->u.value_str.length = end - begin;
2046 break;
2047 case COAP_ENC_BOOL:
2048 len = (size_t)(end - begin);
2049 if (len == 4 && memcmp("true", begin, len) == 0)
2050 value->u.value_int = 1;
2051 else if (len == 5 && memcmp("false", begin, len) == 0)
2052 value->u.value_int = 0;
2053 else
2054 goto bad_entry;
2055 break;
2056 case COAP_ENC_LAST:
2057 default:
2058 goto bad_entry;
2059 }
2060 return 1;
2061
2062bad_entry:
2063 coap_log_warn("oscore_conf: Unrecognized configuration entry '%.*s'\n",
2064 (int)(end - begin),
2065 begin);
2066 return -1;
2067
2068}
2069
2070#undef CONFIG_ENTRY
2071#define CONFIG_ENTRY(n, e, t) \
2072 { { sizeof(#n)-1, (const uint8_t *)#n }, e, \
2073 offsetof(coap_oscore_conf_t, n), t }
2074
2075#undef CONFIG_SND_ENTRY
2076#define CONFIG_SND_ENTRY(n, e, t) \
2077 { { sizeof(#n)-1, (const uint8_t *)#n }, e, \
2078 offsetof(coap_oscore_snd_conf_t, n), t }
2079
2080#undef CONFIG_RCP_ENTRY
2081#define CONFIG_RCP_ENTRY(n, e, t) \
2082 { { sizeof(#n)-1, (const uint8_t *)#n }, e, \
2083 offsetof(coap_oscore_rcp_conf_t, n), t }
2084
2085#undef CONFIG_DUMMY_ENTRY
2086#define CONFIG_DUMMY_ENTRY(n, e, t) \
2087 { { sizeof(#n)-1, (const uint8_t *)#n }, e, 0, t }
2088
2089typedef struct oscore_text_mapping_t {
2090 coap_str_const_t text;
2091 int value;
2092} oscore_text_mapping_t;
2093
2094/* Naming as per https://www.iana.org/assignments/cose/cose.xhtml#algorithms */
2095static oscore_text_mapping_t text_aead_alg[] = {
2096 TEXT_MAPPING(AES-CCM-16-64-128, COSE_ALGORITHM_AES_CCM_16_64_128),
2097 TEXT_MAPPING(AES-CCM-16-64-256, COSE_ALGORITHM_AES_CCM_16_64_256),
2098 {{0, NULL}, 0}
2099};
2100
2101static oscore_text_mapping_t text_hkdf_alg[] = {
2102 TEXT_MAPPING(direct+HKDF-SHA-256, COSE_HKDF_ALG_HKDF_SHA_256),
2103 {{0, NULL}, 0}
2104};
2105
2106typedef struct oscore_config_t {
2107 coap_str_const_t str_keyword;
2108 coap_oscore_coding_t encoding;
2109 size_t offset;
2110 oscore_text_mapping_t *text_mapping;
2111} oscore_config_t;
2112
2113static oscore_config_t oscore_config[] = {
2114 CONFIG_ENTRY(aead_alg, COAP_ENC_INTEGER | COAP_ENC_TEXT, text_aead_alg),
2115 CONFIG_ENTRY(hkdf_alg, COAP_ENC_INTEGER | COAP_ENC_TEXT, text_hkdf_alg),
2116 CONFIG_ENTRY(master_secret, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
2117 CONFIG_ENTRY(master_salt, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
2118 CONFIG_ENTRY(id_context, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
2119
2120 CONFIG_DUMMY_ENTRY(sender_id, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
2121
2122 CONFIG_DUMMY_ENTRY(recipient_id, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
2123
2124 CONFIG_ENTRY(replay_window, COAP_ENC_INTEGER, NULL),
2125 CONFIG_ENTRY(ssn_freq, COAP_ENC_INTEGER, NULL),
2126 CONFIG_ENTRY(rfc8613_b_1_2, COAP_ENC_BOOL, NULL),
2127 CONFIG_ENTRY(rfc8613_b_2, COAP_ENC_BOOL, NULL),
2128 CONFIG_ENTRY(break_sender_key, COAP_ENC_BOOL, NULL),
2129 CONFIG_ENTRY(break_recipient_key, COAP_ENC_BOOL, NULL),
2130 CONFIG_DUMMY_ENTRY(complex_sender, COAP_ENC_CONFIG, NULL),
2131 CONFIG_DUMMY_ENTRY(complex_recipient, COAP_ENC_CONFIG, NULL),
2132};
2133
2134static oscore_config_t oscore_snd_config[] = {
2135 CONFIG_SND_ENTRY(sender_id, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
2136};
2137
2138static oscore_config_t oscore_rcp_config[] = {
2139 CONFIG_RCP_ENTRY(recipient_id, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
2140 CONFIG_RCP_ENTRY(silent_server, COAP_ENC_BOOL, NULL),
2141 CONFIG_DUMMY_ENTRY(last_seq, COAP_ENC_UNSIGNED64, NULL),
2142 CONFIG_DUMMY_ENTRY(sliding_window, COAP_ENC_UNSIGNED64, NULL),
2143};
2144
2145
2146int
2148 if (snd_conf == NULL)
2149 return 0;
2150
2152 coap_free_type(COAP_STRING, snd_conf);
2153 return 1;
2154}
2155
2156int
2158 if (rcp_conf == NULL)
2159 return 0;
2160
2162 coap_free_type(COAP_STRING, rcp_conf);
2163 return 1;
2164}
2165
2166int
2168 coap_oscore_rcp_conf_t *rcp_conf;
2169 if (oscore_conf == NULL)
2170 return 0;
2171
2173 coap_delete_bin_const(oscore_conf->master_salt);
2174 coap_delete_bin_const(oscore_conf->id_context);
2175 coap_delete_oscore_snd_conf(oscore_conf->sender);
2176
2177 rcp_conf = oscore_conf->recipient_chain;
2178 while (rcp_conf) {
2179 coap_oscore_rcp_conf_t *rcp_next = rcp_conf->next_recipient;
2180
2182 rcp_conf = rcp_next;
2183 }
2184
2185 coap_free_type(COAP_STRING, oscore_conf);
2186 return 1;
2187}
2188
2190coap_parse_oscore_snd_conf_mem(coap_bin_const_t conf_mem) {
2191 const char *start = (const char *)conf_mem.s;
2192 const char *end = start + conf_mem.length;
2193 coap_str_const_t keyword;
2194 oscore_value_t value;
2195 coap_oscore_snd_conf_t *snd_conf;
2196
2198 if (snd_conf == NULL)
2199 return NULL;
2200 memset(snd_conf, 0, sizeof(coap_oscore_snd_conf_t));
2201
2202 memset(&value, 0, sizeof(value));
2203
2204 while (end > start &&
2205 get_split_entry(&start, end - start, &keyword, &value, 0) > 0) {
2206 size_t i;
2207 size_t j;
2208
2209 for (i = 0; i < sizeof(oscore_snd_config) / sizeof(oscore_snd_config[0]); i++) {
2210 if (coap_string_equal(&oscore_snd_config[i].str_keyword, &keyword) != 0 &&
2211 value.encoding & oscore_snd_config[i].encoding) {
2212 coap_bin_const_t *unused_check;
2213
2214 switch (value.encoding) {
2215 case COAP_ENC_HEX:
2216 case COAP_ENC_ASCII:
2217 memcpy(&unused_check,
2218 &(((char *)snd_conf)[oscore_snd_config[i].offset]),
2219 sizeof(unused_check));
2220 if (unused_check != NULL) {
2221 coap_log_warn("oscore_snd_conf: Keyword '%.*s' duplicated\n",
2222 (int)keyword.length,
2223 (const char *)keyword.s);
2224 coap_delete_bin_const(value.u.value_bin);
2225 goto error;
2226 }
2227 memcpy(&(((char *)snd_conf)[oscore_snd_config[i].offset]),
2228 &value.u.value_bin,
2229 sizeof(value.u.value_bin));
2230 break;
2231 case COAP_ENC_INTEGER:
2232 case COAP_ENC_BOOL:
2233 memcpy(&(((char *)snd_conf)[oscore_snd_config[i].offset]),
2234 &value.u.value_int,
2235 sizeof(value.u.value_int));
2236 break;
2237 case COAP_ENC_TEXT:
2238 for (j = 0; oscore_snd_config[i].text_mapping[j].text.s != NULL; j++) {
2239 if (memcmp(value.u.value_str.s,
2240 oscore_snd_config[i].text_mapping[j].text.s,
2241 value.u.value_str.length) == 0) {
2242 memcpy(&(((char *)snd_conf)[oscore_snd_config[i].offset]),
2243 &oscore_snd_config[i].text_mapping[j].value,
2244 sizeof(oscore_snd_config[i].text_mapping[j].value));
2245 break;
2246 }
2247 }
2248 if (oscore_snd_config[i].text_mapping[j].text.s == NULL) {
2249 coap_log_warn("oscore_snd_conf: Keyword '%.*s': value '%.*s' unknown\n",
2250 (int)keyword.length,
2251 (const char *)keyword.s,
2252 (int)value.u.value_str.length,
2253 (const char *)value.u.value_str.s);
2254 goto error;
2255 }
2256 break;
2257 case COAP_ENC_CONFIG:
2258 case COAP_ENC_UNSIGNED64:
2259 case COAP_ENC_LAST:
2260 default:
2261 assert(0);
2262 break;
2263 }
2264 break;
2265 }
2266 }
2267 if (i == sizeof(oscore_snd_config) / sizeof(oscore_snd_config[0])) {
2268 coap_log_warn("oscore_snd_conf: Keyword '%.*s', type '%s' unknown\n",
2269 (int)keyword.length,
2270 (const char *)keyword.s,
2271 value.encoding_name);
2272 if (value.encoding == COAP_ENC_HEX || value.encoding == COAP_ENC_ASCII ||
2273 value.encoding == COAP_ENC_CONFIG)
2274 coap_delete_bin_const(value.u.value_bin);
2275 goto error;
2276 }
2277 }
2278 if (!snd_conf->sender_id) {
2279 coap_log_warn("oscore_snd_conf: sender_id not defined\n");
2280 goto error;
2281 }
2282 if (snd_conf->sender_id->length > 7) {
2283 coap_log_warn("oscore_snd_conf: Maximum size of sender_id is 7 bytes\n");
2284 goto error;
2285 }
2286 return snd_conf;
2287
2288error:
2290 return NULL;
2291}
2292
2294coap_parse_oscore_rcp_conf_mem(coap_bin_const_t conf_mem) {
2295 const char *start = (const char *)conf_mem.s;
2296 const char *end = start + conf_mem.length;
2297 coap_str_const_t keyword;
2298 oscore_value_t value;
2299 coap_oscore_rcp_conf_t *rcp_conf;
2300
2302 if (rcp_conf == NULL)
2303 return NULL;
2304 memset(rcp_conf, 0, sizeof(coap_oscore_rcp_conf_t));
2305
2306 memset(&value, 0, sizeof(value));
2307
2308 while (end > start &&
2309 get_split_entry(&start, end - start, &keyword, &value, 0) > 0) {
2310 size_t i;
2311 size_t j;
2312
2313 for (i = 0; i < sizeof(oscore_rcp_config) / sizeof(oscore_rcp_config[0]); i++) {
2314 if (coap_string_equal(&oscore_rcp_config[i].str_keyword, &keyword) != 0 &&
2315 value.encoding & oscore_rcp_config[i].encoding) {
2316 if (coap_string_equal(coap_make_str_const("last_seq"), &keyword)) {
2317 rcp_conf->last_seq = value.u.value_int64;
2318 rcp_conf->window_initialized = 1;
2319 } else if (coap_string_equal(coap_make_str_const("sliding_window"), &keyword)) {
2320 rcp_conf->sliding_window = value.u.value_int64;
2321 rcp_conf->window_initialized = 1;
2322 } else {
2323 coap_bin_const_t *unused_check;
2324
2325 switch (value.encoding) {
2326 case COAP_ENC_HEX:
2327 case COAP_ENC_ASCII:
2328 memcpy(&unused_check,
2329 &(((char *)rcp_conf)[oscore_rcp_config[i].offset]),
2330 sizeof(unused_check));
2331 if (unused_check != NULL) {
2332 coap_log_warn("oscore_rcp_conf: Keyword '%.*s' duplicated\n",
2333 (int)keyword.length,
2334 (const char *)keyword.s);
2335 coap_delete_bin_const(value.u.value_bin);
2336 goto error;
2337 }
2338 memcpy(&(((char *)rcp_conf)[oscore_rcp_config[i].offset]),
2339 &value.u.value_bin,
2340 sizeof(value.u.value_bin));
2341 break;
2342 case COAP_ENC_INTEGER:
2343 case COAP_ENC_BOOL:
2344 memcpy(&(((char *)rcp_conf)[oscore_rcp_config[i].offset]),
2345 &value.u.value_int,
2346 sizeof(value.u.value_int));
2347 break;
2348 case COAP_ENC_TEXT:
2349 for (j = 0; oscore_rcp_config[i].text_mapping[j].text.s != NULL; j++) {
2350 if (memcmp(value.u.value_str.s,
2351 oscore_rcp_config[i].text_mapping[j].text.s,
2352 value.u.value_str.length) == 0) {
2353 memcpy(&(((char *)rcp_conf)[oscore_rcp_config[i].offset]),
2354 &oscore_rcp_config[i].text_mapping[j].value,
2355 sizeof(oscore_rcp_config[i].text_mapping[j].value));
2356 break;
2357 }
2358 }
2359 if (oscore_rcp_config[i].text_mapping[j].text.s == NULL) {
2360 coap_log_warn("oscore_rcp_conf: Keyword '%.*s': value '%.*s' unknown\n",
2361 (int)keyword.length,
2362 (const char *)keyword.s,
2363 (int)value.u.value_str.length,
2364 (const char *)value.u.value_str.s);
2365 goto error;
2366 }
2367 break;
2368 case COAP_ENC_CONFIG:
2369 case COAP_ENC_UNSIGNED64:
2370 case COAP_ENC_LAST:
2371 default:
2372 assert(0);
2373 break;
2374 }
2375 }
2376 break;
2377 }
2378 }
2379 if (i == sizeof(oscore_rcp_config) / sizeof(oscore_rcp_config[0])) {
2380 coap_log_warn("oscore_rcp_conf: Keyword '%.*s', type '%s' unknown\n",
2381 (int)keyword.length,
2382 (const char *)keyword.s,
2383 value.encoding_name);
2384 if (value.encoding == COAP_ENC_HEX || value.encoding == COAP_ENC_ASCII ||
2385 value.encoding == COAP_ENC_CONFIG)
2386 coap_delete_bin_const(value.u.value_bin);
2387 goto error;
2388 }
2389 }
2390 if (!rcp_conf->recipient_id) {
2391 coap_log_warn("oscore_rcp_conf: recipient_id not defined\n");
2392 goto error;
2393 }
2394 if (rcp_conf->recipient_id->length > 7) {
2395 coap_log_warn("oscore_rcp_conf: Maximum size of recipient_id is 7 bytes\n");
2396 goto error;
2397 }
2398 return rcp_conf;
2399
2400error:
2402 return NULL;
2403}
2404
2405static coap_oscore_conf_t *
2406coap_parse_oscore_conf_mem(coap_str_const_t conf_mem) {
2407 const char *start = (const char *)conf_mem.s;
2408 const char *end = start + conf_mem.length;
2409 coap_str_const_t keyword;
2410 oscore_value_t value;
2411 coap_oscore_conf_t *oscore_conf;
2412 int split_ok = -1;
2413
2414 oscore_conf = coap_malloc_type(COAP_STRING, sizeof(coap_oscore_conf_t));
2415 if (oscore_conf == NULL)
2416 return NULL;
2417 memset(oscore_conf, 0, sizeof(coap_oscore_conf_t));
2418
2419 memset(&value, 0, sizeof(value));
2420 /* Preset with defaults RFC8612 3.2 */
2422 oscore_conf->ssn_freq = 1;
2424 oscore_conf->hkdf_alg = COSE_HKDF_ALG_HKDF_SHA_256;
2425
2426 oscore_conf->rfc8613_b_1_2 = 1;
2427 oscore_conf->rfc8613_b_2 = 0;
2428 /* For debugging only */
2429 oscore_conf->break_sender_key = 0;
2430 oscore_conf->break_recipient_key = 0;
2431
2432 while (end > start &&
2433 (split_ok = get_split_entry(&start, end - start, &keyword, &value, 0)) > 0) {
2434 size_t i;
2435 size_t j;
2436
2437 for (i = 0; i < sizeof(oscore_config) / sizeof(oscore_config[0]); i++) {
2438 if (coap_string_equal(&oscore_config[i].str_keyword, &keyword) != 0 &&
2439 value.encoding & oscore_config[i].encoding) {
2440 if (coap_string_equal(coap_make_str_const("sender_id"), &keyword)) {
2441 if (value.u.value_bin->length > 7) {
2442 coap_log_warn("oscore_conf: Maximum size of sender_id is 7 bytes\n");
2443 goto error_free_value_bin;
2444 }
2445 if (oscore_conf->sender) {
2446 coap_log_warn("oscore_conf: sender_id duplicated\n");
2447 goto error_free_value_bin;
2448 }
2450 if (!oscore_conf->sender)
2451 goto error_free_value_bin;
2452
2453 memset(oscore_conf->sender, 0, sizeof(coap_oscore_snd_conf_t));
2454 oscore_conf->sender->sender_id = value.u.value_bin;
2455 } else if (coap_string_equal(coap_make_str_const("recipient_id"), &keyword)) {
2456 coap_oscore_rcp_conf_t *rcp_conf;
2457
2458 if (value.u.value_bin->length > 7) {
2459 coap_log_warn("oscore_conf: Maximum size of recipient_id is 7 bytes\n");
2460 goto error_free_value_bin;
2461 }
2462 /* Special case as there are potentially multiple entries */
2464 if (!rcp_conf)
2465 goto error_free_value_bin;
2466
2467 memset(rcp_conf, 0, sizeof(coap_oscore_rcp_conf_t));
2468 rcp_conf->recipient_id = value.u.value_bin;
2469 rcp_conf->next_recipient = oscore_conf->recipient_chain;
2470 oscore_conf->recipient_chain = rcp_conf;
2471 } else {
2472 coap_bin_const_t *unused_check;
2473
2474 switch (value.encoding) {
2475 case COAP_ENC_CONFIG:
2476 if (keyword.length == sizeof("complex_sender") - 1 &&
2477 memcmp(keyword.s, "complex_sender", keyword.length) == 0) {
2478 coap_oscore_snd_conf_t *snd_conf =
2479 coap_parse_oscore_snd_conf_mem(*value.u.value_bin);
2480
2481 if (!snd_conf) {
2482 coap_log_warn("oscore_conf: Keyword '%.*s' invalid\n",
2483 (int)keyword.length,
2484 (const char *)keyword.s);
2485 goto error_free_value_bin;
2486 }
2487 if (oscore_conf->sender) {
2488 coap_log_warn("oscore_conf: complex_sender duplicated\n");
2490 goto error_free_value_bin;
2491 }
2492 oscore_conf->sender = snd_conf;
2493 coap_delete_bin_const(value.u.value_bin);
2494 break;
2495 }
2496 if (keyword.length == sizeof("complex_recipient") - 1 &&
2497 memcmp(keyword.s, "complex_recipient", keyword.length) == 0) {
2498 coap_oscore_rcp_conf_t *rcp_conf =
2499 coap_parse_oscore_rcp_conf_mem(*value.u.value_bin);
2500
2501 if (!rcp_conf) {
2502 coap_log_warn("oscore_conf: Keyword '%.*s' invalid\n",
2503 (int)keyword.length,
2504 (const char *)keyword.s);
2505 goto error_free_value_bin;
2506 }
2507 rcp_conf->next_recipient = oscore_conf->recipient_chain;
2508 oscore_conf->recipient_chain = rcp_conf;
2509 coap_delete_bin_const(value.u.value_bin);
2510 }
2511 break;
2512 case COAP_ENC_ASCII:
2513 case COAP_ENC_HEX:
2514 memcpy(&unused_check,
2515 &(((char *)oscore_conf)[oscore_config[i].offset]),
2516 sizeof(unused_check));
2517 if (unused_check != NULL) {
2518 coap_log_warn("oscore_conf: Keyword '%.*s' duplicated\n",
2519 (int)keyword.length,
2520 (const char *)keyword.s);
2521 goto error_free_value_bin;
2522 }
2523 memcpy(&(((char *)oscore_conf)[oscore_config[i].offset]),
2524 &value.u.value_bin,
2525 sizeof(value.u.value_bin));
2526 break;
2527 case COAP_ENC_INTEGER:
2528 case COAP_ENC_BOOL:
2529 memcpy(&(((char *)oscore_conf)[oscore_config[i].offset]),
2530 &value.u.value_int,
2531 sizeof(value.u.value_int));
2532 break;
2533 case COAP_ENC_TEXT:
2534 for (j = 0; oscore_config[i].text_mapping[j].text.s != NULL; j++) {
2535 if (coap_string_equal(&value.u.value_str,
2536 &oscore_config[i].text_mapping[j].text)) {
2537 memcpy(&(((char *)oscore_conf)[oscore_config[i].offset]),
2538 &oscore_config[i].text_mapping[j].value,
2539 sizeof(oscore_config[i].text_mapping[j].value));
2540 break;
2541 }
2542 }
2543 if (oscore_config[i].text_mapping[j].text.s == NULL) {
2544 coap_log_warn("oscore_conf: Keyword '%.*s': value '%.*s' unknown\n",
2545 (int)keyword.length,
2546 (const char *)keyword.s,
2547 (int)value.u.value_str.length,
2548 (const char *)value.u.value_str.s);
2549 goto error;
2550 }
2551 break;
2552 case COAP_ENC_UNSIGNED64:
2553 case COAP_ENC_LAST:
2554 default:
2555 assert(0);
2556 break;
2557 }
2558 }
2559 break;
2560 }
2561 }
2562 if (i == sizeof(oscore_config) / sizeof(oscore_config[0])) {
2563 coap_log_warn("oscore_conf: Keyword '%.*s', type '%s' unknown\n",
2564 (int)keyword.length,
2565 (const char *)keyword.s,
2566 value.encoding_name);
2567 if (value.encoding == COAP_ENC_HEX || value.encoding == COAP_ENC_ASCII ||
2568 value.encoding == COAP_ENC_CONFIG) {
2569 coap_delete_bin_const(value.u.value_bin);
2570 }
2571 goto error;
2572 }
2573 }
2574 if (split_ok == -1)
2575 goto error;
2576 if (!oscore_conf->master_secret) {
2577 coap_log_warn("oscore_conf: master_secret not defined\n");
2578 goto error;
2579 }
2580 if (!oscore_conf->sender) {
2581 coap_log_warn("oscore_conf: sender_id not defined\n");
2582 goto error;
2583 }
2584 return oscore_conf;
2585
2586error_free_value_bin:
2587 coap_delete_bin_const(value.u.value_bin);
2588error:
2589 coap_delete_oscore_conf(oscore_conf);
2590 return NULL;
2591}
2592
2593static oscore_ctx_t *
2594coap_oscore_init(coap_context_t *c_context, coap_oscore_conf_t *oscore_conf) {
2595 oscore_ctx_t *osc_ctx = NULL;
2596
2597 if (!coap_crypto_check_cipher_alg(oscore_conf->aead_alg)) {
2598 coap_log_warn("COSE: Cipher Algorithm %d not supported\n",
2599 oscore_conf->aead_alg);
2600 goto error;
2601 }
2602 if (!coap_crypto_check_hkdf_alg(oscore_conf->hkdf_alg)) {
2603 coap_log_warn("COSE: HKDF Algorithm %d not supported\n",
2604 oscore_conf->hkdf_alg);
2605 goto error;
2606 }
2607
2608 osc_ctx = oscore_derive_ctx(c_context, oscore_conf);
2609 if (!osc_ctx) {
2610 coap_log_crit("OSCORE: Could not create Security Context!\n");
2611 coap_free_type(COAP_STRING, oscore_conf);
2612 return NULL;
2613 }
2614
2615 /* As all is stored in osc_ctx, oscore_conf is no longer needed */
2616 coap_free_type(COAP_STRING, oscore_conf);
2617
2618 /* return default first context */
2619 return osc_ctx;
2620
2621error:
2622 /* Remove from linked chain */
2623 oscore_remove_context(c_context, osc_ctx);
2624
2625 coap_delete_oscore_conf(oscore_conf);
2626 return NULL;
2627}
2628
2629void
2631 oscore_free_contexts(c_context);
2632}
2633
2634void
2637}
2638
2639void
2641 oscore_recipient_ctx_t *recipient_ctx) {
2642 oscore_recipient_ctx_t *old_ctx;
2643
2644 if (session == NULL || recipient_ctx == NULL)
2645 return;
2646
2647 if (session->recipient_ctx == recipient_ctx) {
2648 /* already attached */
2649 return;
2650 }
2651
2652 old_ctx = session->recipient_ctx;
2653
2654 /* attach recipient context */
2655 session->recipient_ctx = recipient_ctx;
2656 session->recipient_ctx->ref++;
2657
2658 if (old_ctx != NULL) {
2660 }
2661}
2662
2663void
2665 oscore_recipient_ctx_t *recipient_ctx) {
2666 if (association == NULL)
2667 return;
2668
2669 if (association->recipient_ctx == recipient_ctx)
2670 return;
2671
2672 if (association->recipient_ctx != NULL) {
2673 association->recipient_ctx->ref--;
2674 }
2675
2676 association->recipient_ctx = recipient_ctx;
2677 association->recipient_ctx->ref++;
2678}
2679
2682 coap_oscore_save_seq_num_t save_seq_num_func,
2683 void *save_seq_num_func_param,
2684 uint64_t start_seq_num) {
2685 coap_oscore_conf_t *oscore_conf = coap_parse_oscore_conf_mem(conf_mem);
2686
2687 if (oscore_conf == NULL)
2688 return NULL;
2689
2690 oscore_conf->save_seq_num_func = save_seq_num_func;
2691 oscore_conf->save_seq_num_func_param = save_seq_num_func_param;
2692 oscore_conf->start_seq_num = start_seq_num;
2693 coap_log_oscore("Start Seq no %" PRIu64 "\n", start_seq_num);
2694 return oscore_conf;
2695}
2696
2697/*
2698 * Compute the size of the potential OSCORE overhead
2699 */
2700size_t
2702 size_t overhead = 0;
2703 oscore_recipient_ctx_t *rcp_ctx = session->recipient_ctx;
2704 oscore_ctx_t *osc_ctx = rcp_ctx ? rcp_ctx->osc_ctx : NULL;
2705 coap_opt_iterator_t opt_iter;
2706 coap_opt_t *option;
2707
2708 if (osc_ctx == NULL)
2709 return 0;
2710
2711 /* Protected code held in inner PDU as token */
2712 overhead += 1;
2713
2714 /* Observe option (creates inner and outer */
2715 option = coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter);
2716 if (option) {
2717 /* Assume delta is small */
2718 overhead += 2 + coap_opt_length(option);
2719 }
2720
2721 /* Proxy URI option Split - covered by coap_rebuild_pdu_for_proxy () */
2722
2723 /* OSCORE option */
2724 /* Option header */
2725 overhead += 1 +
2726 /* Partial IV (64 bits max)*/
2727 8 +
2728 /* kid context */
2729 (osc_ctx->id_context ? osc_ctx->id_context->length : 0) +
2730 /* kid */
2731 osc_ctx->sender_context->sender_id->length;
2732
2733 /* AAD overhead */
2734 overhead += AES_CCM_TAG;
2735
2736 /* Extended Token Overhead */
2737 if (session->max_token_size)
2738 overhead += session->max_token_size - pdu->actual_token.length;
2739
2740 /* End of options marker */
2741 overhead += 1;
2742
2743 return overhead;
2744}
2745
2746COAP_API int
2748 coap_bin_const_t *recipient_id) {
2749 int ret;
2750
2751 coap_lock_lock(return 0);
2752 ret = coap_new_oscore_recipient_lkd(context, recipient_id);
2754 return ret;
2755}
2756
2757int
2759 coap_bin_const_t *recipient_id) {
2760 coap_oscore_rcp_conf_t *rcp_conf;
2761 oscore_recipient_ctx_t *rcp_ctx;
2762
2764
2765 if (context->p_osc_ctx == NULL) {
2766 coap_delete_bin_const(recipient_id);
2767 return 0;
2768 }
2770 if (rcp_conf == NULL) {
2771 coap_delete_bin_const(recipient_id);
2772 return 0;
2773 }
2774 memset(rcp_conf, 0, sizeof(coap_oscore_rcp_conf_t));
2775 rcp_conf->recipient_id = recipient_id;
2776 /* rcp_conf is released in oscore_add_recipient() */
2777 rcp_ctx = oscore_add_recipient(context->p_osc_ctx, rcp_conf, 0);
2778 if (rcp_ctx == NULL)
2779 return 0;
2780 return 1;
2781}
2782
2783COAP_API int
2785 coap_bin_const_t *recipient_id) {
2786 int ret;
2787
2788 if (!context || !recipient_id)
2789 return 0;
2790 coap_lock_lock(return 0);
2791 ret = coap_delete_oscore_recipient_lkd(context, recipient_id);
2793 return ret;
2794}
2795
2796int
2798 coap_bin_const_t *recipient_id) {
2800 if (context->p_osc_ctx == NULL)
2801 return 0;
2802 return oscore_delete_recipient(context->p_osc_ctx, recipient_id);
2803}
2804
2805void
2807 coap_oscore_find_handler_t find_handler,
2808 coap_oscore_update_seq_num_handler_t update_seq_num_handler) {
2809 if (context == NULL)
2810 return;
2811
2812 coap_lock_lock(return);
2813 context->oscore_find_cb = find_handler;
2814 context->oscore_update_seq_num_cb = update_seq_num_handler;
2816}
2817
2819coap_oscore_get_recipient_conf(coap_oscore_conf_t *oscore_conf,
2820 const coap_bin_const_t *rid) {
2821 coap_oscore_rcp_conf_t *next = oscore_conf->recipient_chain;
2822
2823 while (next) {
2824 if (coap_binary_equal(next->recipient_id, rid)) {
2825 return next;
2826 }
2827 next = next->next_recipient;
2828 }
2829 return NULL;
2830}
2831
2832
2833COAP_API int
2835 const coap_bin_const_t *recipient_id,
2836 uint64_t last_seq, uint64_t seq_window) {
2837 int ret;
2838
2839 coap_lock_lock(return 0);
2840 ret = coap_oscore_recipient_set_latest_seq_lkd(oscore_conf, recipient_id, last_seq,
2841 seq_window);
2843 return ret;
2844}
2845
2846int
2848 const coap_bin_const_t *recipient_id,
2849 uint64_t last_seq, uint64_t seq_window) {
2850 coap_oscore_rcp_conf_t *rcp_conf;
2851
2852 rcp_conf = coap_oscore_get_recipient_conf(oscore_conf, recipient_id);
2853 if (rcp_conf) {
2854 rcp_conf->last_seq = last_seq;
2855 rcp_conf->sliding_window = seq_window;
2856 return 1;
2857 }
2858 return 0;
2859}
2860
2862
2863#else /* !COAP_OSCORE_SUPPORT */
2864int
2866 return 0;
2867}
2868
2871 const coap_address_t *local_if,
2872 const coap_address_t *server,
2873 coap_proto_t proto,
2874 coap_oscore_conf_t *oscore_conf) {
2875 (void)ctx;
2876 (void)local_if;
2877 (void)server;
2878 (void)proto;
2879 (void)oscore_conf;
2880 return NULL;
2881}
2882
2885 const coap_address_t *local_if,
2886 const coap_address_t *server,
2887 coap_proto_t proto,
2888 coap_oscore_conf_t *oscore_conf,
2889 void *app_data,
2891 coap_str_const_t *ws_host) {
2892 (void)ctx;
2893 (void)local_if;
2894 (void)server;
2895 (void)proto;
2896 (void)oscore_conf;
2897 (void)app_data;
2898 (void)callback;
2899 (void)ws_host;
2900 return NULL;
2901}
2902
2905 const coap_address_t *local_if,
2906 const coap_address_t *server,
2907 coap_proto_t proto,
2908 coap_dtls_cpsk_t *psk_data,
2909 coap_oscore_conf_t *oscore_conf) {
2910 (void)ctx;
2911 (void)local_if;
2912 (void)server;
2913 (void)proto;
2914 (void)psk_data;
2915 (void)oscore_conf;
2916 return NULL;
2917}
2918
2921 const coap_address_t *local_if,
2922 const coap_address_t *server,
2923 coap_proto_t proto,
2924 coap_dtls_cpsk_t *psk_data,
2925 coap_oscore_conf_t *oscore_conf,
2926 void *app_data,
2928 coap_str_const_t *ws_host) {
2929 (void)ctx;
2930 (void)local_if;
2931 (void)server;
2932 (void)proto;
2933 (void)psk_data;
2934 (void)oscore_conf;
2935 (void)app_data;
2936 (void)callback;
2937 (void)ws_host;
2938 return NULL;
2939}
2940
2943 const coap_address_t *local_if,
2944 const coap_address_t *server,
2945 coap_proto_t proto,
2946 coap_dtls_pki_t *pki_data,
2947 coap_oscore_conf_t *oscore_conf) {
2948 (void)ctx;
2949 (void)local_if;
2950 (void)server;
2951 (void)proto;
2952 (void)pki_data;
2953 (void)oscore_conf;
2954 return NULL;
2955}
2956
2959 const coap_address_t *local_if,
2960 const coap_address_t *server,
2961 coap_proto_t proto,
2962 coap_dtls_pki_t *pki_data,
2963 coap_oscore_conf_t *oscore_conf,
2964 void *app_data,
2966 coap_str_const_t *ws_host) {
2967 (void)ctx;
2968 (void)local_if;
2969 (void)server;
2970 (void)proto;
2971 (void)pki_data;
2972 (void)oscore_conf;
2973 (void)app_data;
2974 (void)callback;
2975 (void)ws_host;
2976 return NULL;
2977}
2978
2979int
2981 coap_oscore_conf_t *oscore_conf) {
2982 (void)context;
2983 (void)oscore_conf;
2984 return 0;
2985}
2986
2989 coap_oscore_save_seq_num_t save_seq_num_func,
2990 void *save_seq_num_func_param,
2991 uint64_t start_seq_num) {
2992 (void)conf_mem;
2993 (void)save_seq_num_func;
2994 (void)save_seq_num_func_param;
2995 (void)start_seq_num;
2996 return NULL;
2997}
2998
2999int
3001 (void)oscore_conf;
3002 return 0;
3003}
3004
3005int
3007 coap_bin_const_t *recipient_id) {
3008 (void)context;
3009 (void)recipient_id;
3010 return 0;
3011}
3012
3013int
3015 coap_bin_const_t *recipient_id) {
3016 (void)context;
3017 (void)recipient_id;
3018 return 0;
3019}
3020
3021void
3023 coap_oscore_find_handler_t find_handler,
3024 coap_oscore_update_seq_num_handler_t update_seq_num_handler) {
3025 (void)context;
3026 (void)find_handler;
3027 (void)update_seq_num_handler;
3028}
3029
3030COAP_API int
3032 const coap_bin_const_t *recipient_id,
3033 uint64_t last_seq, uint64_t seq_window) {
3034 (void)oscore_conf;
3035 (void)recipient_id;
3036 (void)last_seq;
3037 (void)seq_window;
3038 return 0;
3039}
3040
3041#endif /* !COAP_OSCORE_SUPPORT */
struct coap_lg_crcv_t coap_lg_crcv_t
#define PRIu64
Library specific build wrapper for coap_internal.h.
#define COAP_API
@ COAP_OSCORE_BUF
Definition coap_mem.h:60
@ 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
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
coap_code_opt_num_t
Definition coap_option.h:70
@ COAP_OPTION_OBSERVE
Definition coap_option.h:75
@ COAP_OPTION_IF_NONE_MATCH
Definition coap_option.h:74
@ COAP_OPTION_ETAG
Definition coap_option.h:73
@ COAP_OPTION_EDHOC
Definition coap_option.h:89
@ COAP_OPTION_NORESPONSE
Definition coap_option.h:98
@ COAP_OPTION_MAXAGE
Definition coap_option.h:83
@ COAP_OPTION_SIZE2
Definition coap_option.h:92
@ COAP_OPTION_Q_BLOCK2
Definition coap_option.h:93
@ COAP_OPTION_PROXY_SCHEME
Definition coap_option.h:95
@ COAP_OPTION_HOP_LIMIT
Definition coap_option.h:85
@ COAP_OPTION_URI_PORT
Definition coap_option.h:76
@ COAP_OPTION_URI_HOST
Definition coap_option.h:72
@ COAP_OPTION_BLOCK2
Definition coap_option.h:90
@ COAP_OPTION_IF_MATCH
Definition coap_option.h:71
@ COAP_OPTION_SIZE1
Definition coap_option.h:96
@ COAP_OPTION_ECHO
Definition coap_option.h:97
@ COAP_OPTION_RTAG
Definition coap_option.h:99
@ COAP_OPTION_BLOCK1
Definition coap_option.h:91
@ COAP_OPTION_URI_PATH
Definition coap_option.h:79
@ COAP_OPTION_Q_BLOCK1
Definition coap_option.h:87
@ COAP_OPTION_OSCORE
Definition coap_option.h:78
@ COAP_OPTION_CONTENT_FORMAT
Definition coap_option.h:80
@ COAP_OPTION_LOCATION_QUERY
Definition coap_option.h:88
@ COAP_OPTION_URI_QUERY
Definition coap_option.h:84
@ COAP_OPTION_PROXY_URI
Definition coap_option.h:94
@ COAP_OPTION_LOCATION_PATH
Definition coap_option.h:77
@ COAP_OPTION_URI_PATH_ABB
Definition coap_option.h:81
@ COAP_OPTION_ACCEPT
Definition coap_option.h:86
static int coap_uri_scheme_is_secure(const coap_uri_t *uri)
Definition coap_uri.h:92
@ COAP_URI_SCHEME_LAST
Definition coap_uri.h:39
coap_mid_t coap_send_ack_lkd(coap_session_t *session, const coap_pdu_t *request)
Sends an ACK message with code 0 for the specified request to dst.
Definition coap_net.c:1220
int coap_prng_lkd(void *buf, size_t len)
Fills buf with len random bytes using the default pseudo random number generator.
Definition coap_prng.c:192
int coap_handle_event_lkd(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
Definition coap_net.c:5207
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu, coap_pdu_t *request_pdu)
Sends a CoAP message to given peer.
Definition coap_net.c:2097
void coap_cancel_all_messages(coap_context_t *context, coap_session_t *session, coap_bin_const_t *token)
Cancels all outstanding messages for session session that have the specified token.
Definition coap_net.c:3306
#define COAP_OSCORE_DEFAULT_REPLAY_WINDOW
int coap_crypto_check_hkdf_alg(cose_hkdf_alg_t hkdf_alg)
Check whether the defined hkdf algorithm is supported by the underlying crypto library.
int coap_crypto_check_cipher_alg(cose_alg_t alg)
Check whether the defined cipher algorithm is supported by the underlying crypto library.
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
Definition coap_encode.c:47
unsigned int coap_decode_var_bytes(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition coap_encode.c:38
uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition coap_encode.c:71
unsigned int coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val)
Encodes multiple-length byte sequences.
Definition coap_encode.c:81
@ COAP_EVENT_OSCORE_DECODE_ERROR
Triggered when there is an OSCORE decode of OSCORE option failure.
Definition coap_event.h:130
@ COAP_EVENT_OSCORE_INTERNAL_ERROR
Triggered when there is an OSCORE internal error i.e malloc failed.
Definition coap_event.h:128
@ COAP_EVENT_OSCORE_NOT_ENABLED
Triggered when trying to use OSCORE to decrypt, but it is not enabled.
Definition coap_event.h:122
@ COAP_EVENT_OSCORE_NO_SECURITY
Triggered when there is no OSCORE security definition found.
Definition coap_event.h:126
@ COAP_EVENT_OSCORE_NO_PROTECTED_PAYLOAD
Triggered when there is no OSCORE encrypted payload provided.
Definition coap_event.h:124
@ COAP_EVENT_OSCORE_DECRYPTION_FAILURE
Triggered when there is an OSCORE decryption failure.
Definition coap_event.h:120
#define coap_lock_callback(func)
Dummy for no thread-safe code.
#define coap_lock_unlock()
Dummy for no thread-safe code.
#define coap_lock_check_locked()
Dummy for no thread-safe code.
#define coap_lock_lock(failed)
Dummy for no thread-safe code.
#define coap_log_debug(...)
Definition coap_debug.h:126
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition coap_debug.c:103
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
Definition coap_debug.c:812
#define coap_log_oscore(...)
Definition coap_debug.h:132
#define coap_log_warn(...)
Definition coap_debug.h:108
#define coap_log_err(...)
Definition coap_debug.h:102
#define coap_log_crit(...)
Definition coap_debug.h:96
@ COAP_LOG_OSCORE
Definition coap_debug.h:65
@ COAP_LOG_DEBUG
Definition coap_debug.h:64
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
coap_opt_iterator_t * coap_option_iterator_init(const coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t *filter)
Initializes the given option iterator oi to point to the beginning of the pdu's option list.
void coap_delete_optlist(coap_optlist_t *queue)
Removes all entries from the optlist_chain, freeing off their memory usage.
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
int coap_add_optlist_pdu(coap_pdu_t *pdu, coap_optlist_t **options)
The current optlist of optlist_chain is first sorted (as per RFC7272 ordering requirements) and then ...
coap_opt_t * coap_check_option(const coap_pdu_t *pdu, coap_option_num_t number, coap_opt_iterator_t *oi)
Retrieves the first option of number number from pdu.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
uint64_t oscore_cbor_get_element_size(const uint8_t **buffer, size_t *buf_size)
void cose_encrypt0_set_plaintext(cose_encrypt0_t *ptr, uint8_t *buffer, size_t size)
int cose_encrypt0_set_key(cose_encrypt0_t *ptr, coap_bin_const_t *key)
void cose_encrypt0_set_kid_context(cose_encrypt0_t *ptr, coap_bin_const_t *kid_context)
const char * cose_get_alg_name(cose_alg_t id, char *buffer, size_t buflen)
void cose_encrypt0_set_ciphertext(cose_encrypt0_t *ptr, uint8_t *buffer, size_t size)
int cose_encrypt0_decrypt(cose_encrypt0_t *ptr, uint8_t *plaintext_buffer, size_t plaintext_len)
size_t cose_tag_len(cose_alg_t cose_alg)
void cose_encrypt0_set_aad(cose_encrypt0_t *ptr, coap_bin_const_t *aad)
cose_curve_t
Definition oscore_cose.h:62
int cose_encrypt0_encrypt(cose_encrypt0_t *ptr, uint8_t *ciphertext_buffer, size_t ciphertext_len)
void cose_encrypt0_set_partial_iv(cose_encrypt0_t *ptr, coap_bin_const_t *partial_iv)
void cose_encrypt0_set_external_aad(cose_encrypt0_t *ptr, coap_bin_const_t *external_aad)
void cose_encrypt0_init(cose_encrypt0_t *ptr)
void cose_encrypt0_set_alg(cose_encrypt0_t *ptr, uint8_t alg)
void cose_encrypt0_set_key_id(cose_encrypt0_t *ptr, coap_bin_const_t *key_id)
void cose_encrypt0_set_nonce(cose_encrypt0_t *ptr, coap_bin_const_t *nonce)
@ COSE_HKDF_ALG_HKDF_SHA_256
@ COSE_ALGORITHM_AES_CCM_16_64_128
@ COSE_ALGORITHM_AES_CCM_16_64_256
size_t oscore_prepare_aad(const uint8_t *external_aad_buffer, size_t external_aad_len, uint8_t *aad_buffer, size_t aad_size)
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 coap_delete_oscore_recipient_lkd(coap_context_t *context, coap_bin_const_t *recipient_id)
Release all the information associated for the specific Recipient ID (and hence and stop any further ...
int oscore_delete_association(coap_session_t *session, oscore_association_t *association)
coap_session_t * coap_new_client_session_oscore3_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_oscore_conf_t *oscore_conf, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, protecting the data using OSCORE,...
uint8_t oscore_validate_sender_seq(oscore_recipient_ctx_t *ctx, cose_encrypt0_t *cose)
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
#define OSCORE_SEQ_MAX
oscore_ctx_t * coap_init_oscore_context_from_conf(coap_oscore_conf_t *oscore_conf)
Initializes an OSCORE context from the given configuration.
#define AES_CCM_TAG
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.
int oscore_decode_option_value(const uint8_t *option_value, size_t option_len, cose_encrypt0_t *cose)
int coap_delete_oscore_snd_conf(coap_oscore_snd_conf_t *oscore_snd_conf)
Release all the information associated with the OSCORE complex Sender configuration.
coap_pdu_t * coap_oscore_new_pdu_encrypted_lkd(coap_session_t *session, coap_pdu_t *pdu, coap_bin_const_t *kid_context, oscore_partial_iv_t send_partial_iv)
Encrypts the specified pdu when OSCORE encryption is required on session.
int oscore_delete_recipient(oscore_ctx_t *osc_ctx, coap_bin_const_t *rid)
uint8_t oscore_increment_sender_seq(oscore_ctx_t *ctx)
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
coap_session_t * coap_new_client_session_oscore_psk3_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_cpsk_t *psk_data, coap_oscore_conf_t *oscore_conf, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PSK credentials protecting the data using...
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
COAP_API coap_pdu_t * coap_oscore_new_pdu_encrypted(coap_session_t *session, coap_pdu_t *pdu, coap_bin_const_t *kid_context, oscore_partial_iv_t send_partial_iv)
Encrypts the specified pdu when OSCORE encryption is required on session.
void oscore_roll_back_seq(oscore_recipient_ctx_t *ctx)
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)
size_t oscore_prepare_e_aad(oscore_ctx_t *ctx, cose_encrypt0_t *cose, const uint8_t *oscore_option, size_t oscore_option_len, coap_bin_const_t *sender_public_key, uint8_t *external_aad_ptr, size_t external_aad_size)
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)
struct coap_pdu_t * coap_oscore_decrypt_pdu(coap_session_t *session, coap_pdu_t *pdu)
Decrypts the OSCORE-encrypted parts of pdu when OSCORE is used.
int coap_oscore_recipient_set_latest_seq_lkd(coap_oscore_conf_t *oscore_conf, const coap_bin_const_t *recipient_id, uint64_t last_seq, uint64_t seq_window)
Set the latest sequence number and sliding window for the specified recipient id in the compiled conf...
int coap_rebuild_pdu_for_proxy(coap_pdu_t *pdu)
Convert PDU to use Proxy-Scheme option if Proxy-Uri option is present.
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 coap_delete_oscore_associations(coap_session_t *session)
Cleanup all allocated OSCORE association information.
int coap_oscore_initiate(coap_session_t *session, coap_oscore_conf_t *oscore_conf)
Initiate an OSCORE session.
int coap_new_oscore_recipient_lkd(coap_context_t *context, coap_bin_const_t *recipient_id)
Add in the specific Recipient ID into the OSCORE context (server only).
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
oscore_partial_iv_t
void coap_delete_all_oscore(coap_context_t *context)
Cleanup all allocated OSCORE information.
void oscore_generate_nonce(cose_encrypt0_t *ptr, oscore_ctx_t *ctx, uint8_t *buffer, uint8_t size)
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)
int coap_context_oscore_server_lkd(coap_context_t *context, coap_oscore_conf_t *oscore_conf)
Set the context's default OSCORE configuration for a server.
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.
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...
coap_session_t * coap_new_client_session_oscore_pki3_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_pki_t *pki_data, coap_oscore_conf_t *oscore_conf, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PKI credentials protecting the data using...
ssize_t oscore_encode_option_value(uint8_t *option_buffer, size_t option_buf_len, cose_encrypt0_t *cose, uint8_t group_flag, uint8_t appendix_b_2)
Encode the OSCORE option.
size_t coap_oscore_overhead(coap_session_t *session, coap_pdu_t *pdu)
Determine the additional data size requirements for adding in OSCORE.
@ OSCORE_SEND_PARTIAL_IV
Send partial IV with encrypted PDU.
@ OSCORE_SEND_NO_IV
Do not send partial IV unless added by a response.
COAP_API int coap_oscore_recipient_set_latest_seq(coap_oscore_conf_t *oscore_conf, const coap_bin_const_t *recipient_id, uint64_t last_seq, uint64_t seq_window)
Set the latest sequence number and sliding window for the specified recipient id in the compiled conf...
coap_oscore_conf_t * coap_new_oscore_conf(coap_str_const_t conf_mem, coap_oscore_save_seq_num_t save_seq_num_func, void *save_seq_num_func_param, uint64_t start_seq_num)
Parse an OSCORE configuration (held in memory) and populate a OSCORE configuration structure.
coap_session_t * coap_new_client_session_oscore_psk(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_cpsk_t *psk_data, coap_oscore_conf_t *oscore_conf)
Creates a new client session to the designated server with PSK credentials as well as protecting the ...
coap_session_t * coap_new_client_session_oscore_psk3(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_cpsk_t *psk_data, coap_oscore_conf_t *oscore_conf, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PSK credentials protecting the data using...
int(* coap_oscore_update_seq_num_handler_t)(const coap_session_t *session, const coap_bin_const_t *rcpkey_id, const coap_bin_const_t *ctxkey_id, uint64_t receiver_seq_num, uint64_t seq_num_window)
Callback function type for persisting the OSCORE receiver sequence number and anti-replay sliding win...
Definition coap_oscore.h:77
int coap_delete_oscore_conf(coap_oscore_conf_t *oscore_conf)
Release all the information associated with the OSCORE configuration.
coap_session_t * coap_new_client_session_oscore(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_oscore_conf_t *oscore_conf)
Creates a new client session to the designated server, protecting the data using OSCORE.
int coap_context_oscore_server(coap_context_t *context, coap_oscore_conf_t *oscore_conf)
Set the context's default OSCORE configuration for a server.
coap_oscore_conf_t *(* coap_oscore_find_handler_t)(const coap_session_t *session, const coap_bin_const_t *rcpkey_id, const coap_bin_const_t *ctxkey_id)
Callback function type for overriding oscore_find_context().
Definition coap_oscore.h:54
coap_session_t * coap_new_client_session_oscore_pki(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_pki_t *pki_data, coap_oscore_conf_t *oscore_conf)
Creates a new client session to the designated server with PKI credentials as well as protecting the ...
coap_session_t * coap_new_client_session_oscore3(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_oscore_conf_t *oscore_conf, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, protecting the data using OSCORE,...
int coap_new_oscore_recipient(coap_context_t *context, coap_bin_const_t *recipient_id)
Add in the specific Recipient ID into the OSCORE context (server only).
int(* coap_oscore_save_seq_num_t)(uint64_t sender_seq_num, void *param)
Definition of the function used to save the current Sender Sequence Number.
int coap_delete_oscore_recipient(coap_context_t *context, coap_bin_const_t *recipient_id)
Release all the information associated for the specific Recipient ID (and hence stop any further OSCO...
void coap_oscore_register_external_handlers(coap_context_t *context, coap_oscore_find_handler_t find_handler, coap_oscore_update_seq_num_handler_t update_seq_num_handler)
Register external storage handlers for OSCORE session state.
coap_session_t * coap_new_client_session_oscore_pki3(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_pki_t *pki_data, coap_oscore_conf_t *oscore_conf, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PKI credentials protecting the data using...
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:197
size_t coap_insert_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Inserts option of given number in the pdu with the appropriate data.
Definition coap_pdu.c:692
int coap_remove_option(coap_pdu_t *pdu, coap_option_num_t number)
Removes (first) option of given number from the pdu.
Definition coap_pdu.c:546
int coap_update_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Updates token in pdu with length len and data.
Definition coap_pdu.c:470
#define COAP_PDU_IS_PING(pdu)
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:237
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto)
Compose the protocol specific header for the specified PDU.
Definition coap_pdu.c:1603
#define COAP_PAYLOAD_START
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size)
Dynamically grows the size of pdu to new_size.
Definition coap_pdu.c:341
#define COAP_PDU_IS_REQUEST(pdu)
size_t coap_add_option_internal(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Adds option of given number to pdu that is passed as first parameter.
Definition coap_pdu.c:851
#define COAP_DEFAULT_PORT
Definition coap_pdu.h:39
#define COAP_RESPONSE_CODE(N)
Definition coap_pdu.h:96
coap_proto_t
CoAP protocol types Note: coap_layers_coap[] needs updating if extended.
Definition coap_pdu.h:234
coap_pdu_code_t
Set of codes available for a PDU.
Definition coap_pdu.h:248
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
Definition coap_pdu.c:413
#define COAPS_DEFAULT_PORT
Definition coap_pdu.h:40
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:947
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
Definition coap_pdu.c:104
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition coap_pdu.h:187
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:916
coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu)
Gets the token associated with pdu.
Definition coap_pdu.c:1723
@ COAP_BOOL_FALSE
Definition coap_pdu.h:295
@ COAP_REQUEST_CODE_POST
Definition coap_pdu.h:252
@ COAP_REQUEST_CODE_FETCH
Definition coap_pdu.h:255
@ COAP_MESSAGE_NON
Definition coap_pdu.h:72
@ COAP_MESSAGE_ACK
Definition coap_pdu.h:73
@ COAP_MESSAGE_CON
Definition coap_pdu.h:71
coap_session_t * coap_new_client_session_pki3_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_pki_t *setup_data, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PKI credentials along with app_data infor...
coap_session_t * coap_new_client_session_psk3_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_cpsk_t *setup_data, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PSK credentials along with app_data infor...
void coap_session_release_lkd(coap_session_t *session)
Decrement reference counter on a session.
coap_session_t * coap_new_client_session3_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PSK credentials along with app_data infor...
@ COAP_OSCORE_B_2_NONE
@ COAP_OSCORE_B_2_STEP_3
@ COAP_OSCORE_B_2_STEP_1
@ COAP_OSCORE_B_2_STEP_4
@ COAP_OSCORE_B_2_STEP_5
@ COAP_OSCORE_B_2_STEP_2
void(* coap_app_data_free_callback_t)(void *data)
Callback to free off the app data when the entry is being deleted / freed off.
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
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
Definition coap_str.c:65
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
struct coap_str_const_t coap_str_const_t
CoAP string data definition with const data.
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
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
Definition coap_str.h:208
coap_str_const_t * coap_new_str_const(const uint8_t *data, size_t size)
Returns a new const string object with at least size+1 bytes storage allocated, and the provided data...
Definition coap_str.c:55
int coap_oscore_is_supported(void)
Check whether OSCORE is available.
int coap_query_into_optlist(const uint8_t *s, size_t length, coap_option_num_t optnum, coap_optlist_t **optlist_chain)
Splits the given URI query into '&' separate segments, and then adds the Uri-Query / Location-Query o...
Definition coap_uri.c:985
int coap_path_into_optlist(const uint8_t *s, size_t length, coap_option_num_t optnum, coap_optlist_t **optlist_chain)
Splits the given URI path into '/' separate segments, and then adds the Uri-Path / Location-Path opti...
Definition coap_uri.c:865
int coap_split_proxy_uri(const uint8_t *str_var, size_t len, coap_uri_t *uri)
Parses a given string into URI components.
Definition coap_uri.c:351
coap_uri_info_t coap_uri_scheme[COAP_URI_SCHEME_LAST]
Definition coap_uri.c:59
Multi-purpose address abstraction.
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
size_t length
length of binary data
Definition coap_str.h:58
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 for defining the Client PSK setup data to be used.
Definition coap_dtls.h:418
The structure used for defining the PKI setup data to be used.
Definition coap_dtls.h:317
Iterator to run through PDU options.
coap_opt_t * next_option
pointer to the unparsed next option
size_t length
remaining length of PDU
coap_option_num_t number
decoded option number
Representation of chained list of CoAP options to install.
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.
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
The structure used to hold the OSCORE Sender configuration information.
coap_bin_const_t * sender_id
Sender ID (i.e.
structure for CoAP PDUs
uint8_t max_hdr_size
space reserved for protocol-specific header
uint8_t * token
first byte of token (or extended length bytes prefix), if any, or options
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
coap_bin_const_t actual_token
Actual token in pdu.
uint8_t * data
first byte of payload, if any
coap_mid_t mid
message id, if any, in regular host byte order
uint32_t e_token_length
length of Token space (includes leading extended bytes
size_t used_size
used bytes of storage for token, options and payload
coap_pdu_type_t type
message type
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
uint32_t max_token_size
Largest token size supported RFC8974.
coap_proto_t proto
protocol used
unsigned ref
reference count from queues
uint8_t con_active
Active CON request sent.
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
Representation of parsed URI.
Definition coap_uri.h:70
enum coap_uri_scheme_t scheme
The parsed scheme specifier.
Definition coap_uri.h:82
coap_str_const_t path
The complete path if present or {0, NULL}.
Definition coap_uri.h:73
uint16_t port
The port in host byte order.
Definition coap_uri.h:72
coap_str_const_t query
The complete query if present or {0, NULL}.
Definition coap_uri.h:77
coap_str_const_t host
The host part of the URI.
Definition coap_uri.h:71
coap_bin_const_t aad
coap_bin_const_t key
coap_bin_const_t partial_iv
coap_bin_const_t kid_context
coap_bin_const_t nonce
coap_bin_const_t external_aad
coap_bin_const_t key_id
coap_bin_const_t oscore_option
cose_alg_t alg
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
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_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_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.
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.