libcoap 4.3.5-develop-eafc023
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 approriately 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[48];
419 size_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 (!coap_request) {
545 /* Reset what was just unset as appropriate for AAD */
547 cose_encrypt0_set_partial_iv(cose, association->partial_iv);
548 }
549 if (kid_context)
551
552 /*
553 * RFC8613 8.1/8.3 Step 2(a) (5.4).
554 * Compose the External AAD and then AAD
555 *
556 * OSCORE_option requires
557 * partial_iv (cose partial_iv)
558 * kid_context (cose kid_context)
559 * key_id (cose key_id)
560 * group_flag
561 *
562 * Non Group requires the following
563 * RFC8613 5.4
564 * oscore_version 1
565 * algorithms [
566 * aead_alg (osc_ctx)
567 * ]
568 * request_kid (request key_id using cose)
569 * request_piv (request partial_iv using cose)
570 * options (none at present)
571 *
572 * Note: No I options at present
573 */
574 if (coap_request || send_partial_iv == OSCORE_SEND_PARTIAL_IV) {
575 /* External AAD */
576 external_aad.s = external_aad_buffer;
577 external_aad.length = oscore_prepare_e_aad(osc_ctx,
578 cose,
579 NULL,
580 0,
581 NULL,
582 external_aad_buffer,
583 sizeof(external_aad_buffer));
584 cose_encrypt0_set_external_aad(cose, &external_aad);
585
586 /* AAD */
587 aad.s = aad_buffer;
588 aad.length = oscore_prepare_aad(external_aad_buffer,
589 external_aad.length,
590 aad_buffer,
591 sizeof(aad_buffer));
592 assert(aad.length < AAD_BUF_LEN);
593 cose_encrypt0_set_aad(cose, &aad);
594 }
595
596 /*
597 * RFC8613 8.1/8.3 Step 2(b) (5.3).
598 *
599 * Set up temp plaintext pdu, the data including token, options and
600 * optional payload will get encrypted as COSE ciphertext.
601 */
602 plain_pdu = coap_pdu_init(pdu->type,
603 pdu->code,
604 pdu->mid,
605 pdu->used_size + 1 /* pseudo-token with actual code */);
606 if (plain_pdu == NULL)
607 goto error;
608
609 coap_add_token(osc_pdu, pdu_token.length, pdu_token.s);
610
611 /* First byte of plain is real CoAP code. Pretend it is token */
612 coap_add_token(plain_pdu, 1, &pdu_code);
613
614 /* Copy across the Outer/Inner Options to respective PDUs */
616 while ((option = coap_option_next(&opt_iter))) {
617 switch ((coap_code_opt_num_t)opt_iter.number) {
623 /* Outer only */
624 if (!coap_insert_option(osc_pdu,
625 opt_iter.number,
626 coap_opt_length(option),
627 coap_opt_value(option)))
628 goto error;
629 break;
631 /* Make as Outer option as-is */
632 if (!coap_insert_option(osc_pdu,
633 opt_iter.number,
634 coap_opt_length(option),
635 coap_opt_value(option)))
636 goto error;
637 if (coap_request) {
638 /* Make as Inner option (unchanged) */
639 if (!coap_insert_option(plain_pdu,
640 opt_iter.number,
641 coap_opt_length(option),
642 coap_opt_value(option)))
643 goto error;
644 osc_pdu->code = COAP_REQUEST_CODE_FETCH;
645 } else {
646 /* Make as Inner option but empty */
647 if (!coap_insert_option(plain_pdu, opt_iter.number, 0, NULL))
648 goto error;
649 osc_pdu->code = COAP_RESPONSE_CODE(205);
650 }
651 show_pdu = 1;
652 doing_observe = 1;
653 observe_value = coap_decode_var_bytes(coap_opt_value(option),
654 coap_opt_length(option));
655 break;
657 /*
658 * Should have already been caught by doing
659 * coap_rebuild_pdu_for_proxy() before calling
660 * coap_oscore_new_pdu_encrypted_lkd()
661 */
662 assert(0);
663 break;
665 /* Dealt with separately */
666 break;
668 case COAP_OPTION_ETAG:
685 case COAP_OPTION_ECHO:
686 case COAP_OPTION_RTAG:
687 default:
688 /* Make as Inner option */
689 if (!coap_insert_option(plain_pdu,
690 opt_iter.number,
691 coap_opt_length(option),
692 coap_opt_value(option)))
693 goto error;
694 break;
695 }
696 }
697 /* Add in data to plain */
698 if (coap_get_data(pdu, &length, &data)) {
699 if (!coap_add_data(plain_pdu, length, data))
700 goto error;
701 }
702 if (show_pdu) {
703 coap_log_oscore("OSCORE payload\n");
704 coap_show_pdu(COAP_LOG_OSCORE, plain_pdu);
705 }
706
707 /*
708 * 8.1/8.3 Step 4.
709 * Encrypt the COSE object.
710 *
711 * Requires in COSE object as appropriate
712 * alg (already set)
713 * key (sender key)
714 * nonce (already set)
715 * aad (already set)
716 * plaintext
717 */
718 cose_encrypt0_set_key(cose, snd_ctx->sender_key);
719 cose_encrypt0_set_plaintext(cose, plain_pdu->token, plain_pdu->used_size);
720 dump_cose(cose, "Pre encrypt");
721 ciphertext_buffer =
722 coap_malloc_type(COAP_OSCORE_BUF, OSCORE_CRYPTO_BUFFER_SIZE);
723 if (ciphertext_buffer == NULL)
724 goto error;
725 ciphertext_len = cose_encrypt0_encrypt(cose,
726 ciphertext_buffer,
727 plain_pdu->used_size + AES_CCM_TAG);
728 if ((int)ciphertext_len <= 0) {
729 coap_log_warn("OSCORE: Encryption Failure, result code: %d \n",
730 (int)ciphertext_len);
731 goto error;
732 }
733 assert(ciphertext_len < OSCORE_CRYPTO_BUFFER_SIZE);
734
735 /* Add in OSCORE option (previously computed) */
736 if (!coap_insert_option(osc_pdu,
738 oscore_option_len,
739 oscore_option))
740 goto error;
741
742 /* Add now encrypted payload */
743 if (!coap_add_data(osc_pdu, ciphertext_len, ciphertext_buffer))
744 goto error;
745
746 coap_free_type(COAP_OSCORE_BUF, ciphertext_buffer);
747 ciphertext_buffer = NULL;
748
749 coap_delete_pdu_lkd(plain_pdu);
750 plain_pdu = NULL;
751
752 if (association && association->is_observe == 0)
753 oscore_delete_association(session, association);
754 association = NULL;
755
756 if (!coap_pdu_encode_header(osc_pdu, session->proto)) {
757 goto error;
758 }
759
760 /*
761 * Set up an association for handling a response if this is a request
762 */
763 if (coap_request) {
764 association = oscore_find_association(session, &pdu_token);
765 if (association) {
766 /* Refresh the association */
767 coap_delete_bin_const(association->nonce);
768 association->nonce =
769 coap_new_bin_const(cose->nonce.s, cose->nonce.length);
770 if (association->nonce == NULL)
771 goto error;
772 coap_delete_bin_const(association->aad);
773 association->aad = coap_new_bin_const(cose->aad.s, cose->aad.length);
774 if (association->aad == NULL)
775 goto error;
776 if (doing_observe && observe_value == 1) {
778 association->obs_partial_iv = association->partial_iv;
779 } else {
780 coap_delete_bin_const(association->partial_iv);
781 }
782 association->partial_iv =
784 if (association->partial_iv == NULL)
785 goto error;
786 coap_oscore_association_set_recipient_ctx(association, rcp_ctx);
787 coap_delete_pdu_lkd(association->sent_pdu);
788 if (session->b_2_step != COAP_OSCORE_B_2_NONE || association->just_set_up) {
789 size_t size;
790
791 association->sent_pdu = coap_pdu_duplicate_lkd(pdu, session,
792 pdu_token.length,
793 pdu_token.s, NULL,
795 if (association->sent_pdu == NULL)
796 goto error;
797 if (coap_get_data(pdu, &size, &data)) {
798 coap_add_data(association->sent_pdu, size, data);
799 }
800 association->just_set_up = 0;
801 } else {
802 association->sent_pdu = NULL;
803 }
804 } else if (!oscore_new_association(session,
805 pdu,
806 &pdu_token,
807 rcp_ctx,
808 &cose->aad,
809 &cose->nonce,
810 &cose->partial_iv,
811 doing_observe)) {
812 goto error;
813 }
814 }
815 return osc_pdu;
816
817error:
818 if (ciphertext_buffer)
819 coap_free_type(COAP_OSCORE_BUF, ciphertext_buffer);
820 coap_delete_pdu_lkd(osc_pdu);
821 coap_delete_pdu_lkd(plain_pdu);
822 return NULL;
823}
824
825static void
826build_and_send_error_pdu(coap_session_t *session,
827 coap_pdu_t *rcvd,
828 coap_pdu_code_t code,
829 const char *diagnostic,
830 uint8_t *echo_data,
831 coap_bin_const_t *kid_context,
832 int encrypt_oscore) {
833 coap_pdu_t *err_pdu = NULL;
834 coap_bin_const_t token;
835 int oscore_encryption = session->oscore_encryption;
836 unsigned char buf[4];
837
838 token = coap_pdu_get_token(rcvd);
841 code,
842 rcvd->mid,
843 token.length + 2 + 8 +
844 (diagnostic ? strlen(diagnostic) : 0));
845 if (!err_pdu)
846 return;
847 coap_add_token(err_pdu, token.length, token.s);
848 if (echo_data) {
849 coap_add_option_internal(err_pdu, COAP_OPTION_ECHO, 8, echo_data);
850 } else if (kid_context == NULL) {
853 coap_encode_var_safe(buf, sizeof(buf), 0),
854 buf);
855 }
856 if (diagnostic)
857 coap_add_data(err_pdu, strlen(diagnostic), (const uint8_t *)diagnostic);
858 session->oscore_encryption = encrypt_oscore;
859
860 if ((echo_data || kid_context) && encrypt_oscore) {
861 coap_pdu_t *osc_pdu;
862
863 osc_pdu =
864 coap_oscore_new_pdu_encrypted_lkd(session, err_pdu, kid_context,
865 echo_data ? 1 : 0);
866 if (!osc_pdu)
867 goto fail_resp;
868 session->oscore_encryption = 0;
869 coap_send_internal(session, osc_pdu, NULL);
870 coap_delete_pdu_lkd(err_pdu);
871 err_pdu = NULL;
872 } else {
873 coap_send_internal(session, err_pdu, NULL);
874 err_pdu = NULL;
875 }
876fail_resp:
877 session->oscore_encryption = oscore_encryption;
878 coap_delete_pdu_lkd(err_pdu);
879 return;
880}
881
884 oscore_ctx_t *oscore_ctx = oscore_derive_ctx_from_conf(oscore_conf);
885
886 if (oscore_ctx == NULL) {
887 goto error;
888 }
889
890 /* As all is stored in osc_ctx, oscore_conf is no longer needed */
891 coap_free_type(COAP_STRING, oscore_conf);
892
893 return oscore_ctx;
894error:
895 coap_delete_oscore_conf(oscore_conf);
896 return NULL;
897}
898
899/* pdu contains incoming message with encrypted COSE ciphertext payload
900 * function returns decrypted message
901 * and verifies signature, if present
902 * returns NULL when decryption,verification fails
903 */
906 coap_pdu_t *pdu) {
907 coap_pdu_t *decrypt_pdu = NULL;
908 coap_pdu_t *plain_pdu = NULL;
909 const uint8_t *osc_value; /* value of OSCORE option */
910 uint8_t osc_size; /* size of OSCORE OPTION */
911 coap_opt_iterator_t opt_iter;
912 coap_opt_t *opt = NULL;
913 cose_encrypt0_t cose[1];
914 oscore_ctx_t *osc_ctx = NULL;
915 uint8_t aad_buffer[AAD_BUF_LEN];
916 uint8_t nonce_buffer[13];
918 coap_bin_const_t nonce;
919 int pltxt_size = 0;
920 int got_resp_piv = 0;
921 int doing_resp_observe = 0;
922 uint8_t coap_request = COAP_PDU_IS_REQUEST(pdu);
923 coap_bin_const_t pdu_token;
924 uint8_t *st_encrypt;
925 size_t encrypt_len;
926 size_t tag_len;
927 oscore_recipient_ctx_t *rcp_ctx = NULL;
928 oscore_association_t *association = NULL;
929 uint8_t external_aad_buffer[100];
930 coap_bin_const_t external_aad;
931 oscore_sender_ctx_t *snd_ctx = NULL;
932#if COAP_CLIENT_SUPPORT
933 coap_pdu_t *sent_pdu = NULL;
934#endif /* COAP_CLIENT_SUPPORT */
935
936 opt = coap_check_option(pdu, COAP_OPTION_OSCORE, &opt_iter);
937 assert(opt);
938 if (opt == NULL)
939 return NULL;
940
941 if (session->context->p_osc_ctx == NULL && session->context->oscore_find_cb == NULL) {
942 coap_log_warn("OSCORE: Not enabled\n");
943 if (!coap_request)
946 session);
947 return NULL;
948 }
949
950 if (pdu->data == NULL) {
951 coap_log_warn("OSCORE: No protected payload\n");
952 if (!coap_request)
955 session);
956 return NULL;
957 }
958
959 osc_size = coap_opt_length(opt);
960 osc_value = coap_opt_value(opt);
961
962 cose_encrypt0_init(cose); /* clear cose memory */
963
964 /* PDU code will be filled in after decryption */
965 decrypt_pdu =
966 coap_pdu_init(pdu->type, 0, pdu->mid, pdu->used_size);
967 if (decrypt_pdu == NULL) {
968 if (!coap_request)
971 session);
972 goto error;
973 }
974
975 /* Copy across the Token */
976 pdu_token = coap_pdu_get_token(pdu);
977 coap_add_token(decrypt_pdu, pdu_token.length, pdu_token.s);
978
979 /*
980 * 8.2/8.4 Step 1.
981 * Copy outer options across, except E and OSCORE options
982 */
984 while ((opt = coap_option_next(&opt_iter))) {
985 switch ((coap_code_opt_num_t)opt_iter.number) {
986 /* 'E' options skipped */
988 case COAP_OPTION_ETAG:
1000 case COAP_OPTION_BLOCK2:
1001 case COAP_OPTION_BLOCK1:
1002 case COAP_OPTION_SIZE2:
1004 case COAP_OPTION_SIZE1:
1006 case COAP_OPTION_ECHO:
1007 case COAP_OPTION_RTAG:
1008 /* OSCORE does not get copied across */
1009 case COAP_OPTION_OSCORE:
1010 break;
1011 /* 'U' options included */
1015 case COAP_OPTION_EDHOC:
1018 default:
1019 if (!coap_add_option_internal(decrypt_pdu,
1020 opt_iter.number,
1021 coap_opt_length(opt),
1022 coap_opt_value(opt))) {
1023 if (!coap_request)
1026 session);
1027 goto error;
1028 }
1029 break;
1030 }
1031 }
1032
1033 if (coap_request) {
1034 uint64_t incoming_seq;
1035 /*
1036 * 8.2 Step 2
1037 * Decompress COSE object
1038 * Get Recipient Context based on kid and optional kid_context
1039 */
1040 if (oscore_decode_option_value(osc_value, osc_size, cose) == 0) {
1041 coap_log_warn("OSCORE: OSCORE Option cannot be decoded.\n");
1042 build_and_send_error_pdu(session,
1043 pdu,
1044 COAP_RESPONSE_CODE(402),
1045 "Failed to decode COSE",
1046 NULL,
1047 NULL,
1048 0);
1049 goto error_no_ack;
1050 }
1051 osc_ctx = oscore_find_context(session,
1052 cose->key_id,
1053 &cose->kid_context,
1054 NULL,
1055 &rcp_ctx);
1056 if (!osc_ctx) {
1057 if (cose->kid_context.length > 0) {
1058 const uint8_t *ptr;
1059 size_t length;
1060 /* Appendix B.2 protocol check - Is the recipient key_id known */
1061 osc_ctx = oscore_find_context(session,
1062 cose->key_id,
1063 NULL,
1064 session->oscore_r2 != 0 ? (uint8_t *)&session->oscore_r2 : NULL,
1065 &rcp_ctx);
1066 ptr = cose->kid_context.s;
1067 length = cose->kid_context.length;
1068 if (ptr && osc_ctx && osc_ctx->rfc8613_b_2) {
1069 /* Processing Appendix B.2 protocol */
1070 /* Need to CBOR unwrap kid_context */
1071 coap_bin_const_t kid_context;
1072
1073 kid_context.length = oscore_cbor_get_element_size(&ptr, &length);
1074 if (kid_context.length > length)
1075 goto error;
1076 /* This has to fit into an OSCORE option max 255.
1077 * Initial byte, kid_context size, partial IV size and kid size have to be there
1078 */
1079 if (kid_context.length >= 255 - 1 - 1 - cose->partial_iv.length - cose->key_id.length)
1080 goto error;
1081 kid_context.s = ptr;
1082 cose_encrypt0_set_kid_context(cose, (coap_bin_const_t *)&kid_context);
1083
1084 if (session->oscore_r2 != 0) {
1085 /* B.2 step 4 */
1087 cose->kid_context.length);
1088
1089 if (kc == NULL)
1090 goto error;
1091
1092 session->b_2_step = COAP_OSCORE_B_2_STEP_4;
1093 coap_log_oscore("Appendix B.2 server step 4 (R2 || R3)\n");
1094 oscore_update_ctx(osc_ctx, kc);
1095 } else {
1096 session->b_2_step = COAP_OSCORE_B_2_STEP_2;
1097 coap_log_oscore("Appendix B.2 server step 2 (ID1)\n");
1098 osc_ctx = oscore_duplicate_ctx(session->context,
1099 osc_ctx,
1100 osc_ctx->sender_context->sender_id,
1101 &cose->key_id,
1102 &cose->kid_context);
1103 if (osc_ctx == NULL)
1104 goto error;
1105 /*
1106 * Complete the Verify (B.2 step 2)
1107 * before sending back the response
1108 */
1109 rcp_ctx = osc_ctx->recipient_chain;
1110 }
1111 } else {
1112 osc_ctx = NULL;
1113 }
1114 }
1115 } else if (session->b_2_step != COAP_OSCORE_B_2_NONE) {
1116 session->b_2_step = COAP_OSCORE_B_2_NONE;
1117 coap_log_oscore("Appendix B.2 server finished\n");
1118 }
1119 if (!osc_ctx) {
1120 coap_log_err("OSCORE: Security Context not found\n");
1121 oscore_log_hex_value(COAP_LOG_OSCORE, "key_id", &cose->key_id);
1122 oscore_log_hex_value(COAP_LOG_OSCORE, "kid_context", &cose->kid_context);
1123 build_and_send_error_pdu(session,
1124 pdu,
1125 COAP_RESPONSE_CODE(401),
1126 "Security context not found",
1127 NULL,
1128 NULL,
1129 0);
1130 goto error_no_ack;
1131 }
1132 /* to be used for encryption of returned response later */
1133 coap_oscore_session_set_recipient_ctx(session, rcp_ctx);
1134 snd_ctx = osc_ctx->sender_context;
1135
1136 /*
1137 * 8.2 Step 3.
1138 * Verify Partial IV is not duplicated.
1139 *
1140 * Requires in COSE object as appropriate
1141 * partial_iv (as received)
1142 */
1143 if ((rcp_ctx->initial_state == 0 || osc_ctx->rfc8613_b_1_2 == 0) &&
1144 !oscore_validate_sender_seq(rcp_ctx, cose)) {
1145 coap_log_warn("OSCORE: Replayed or old message\n");
1146 build_and_send_error_pdu(session,
1147 pdu,
1148 COAP_RESPONSE_CODE(401),
1149 "Replay detected",
1150 NULL,
1151 NULL,
1152 0);
1153 goto error_no_ack;
1154 }
1155 if (rcp_ctx->initial_state == 1) {
1156 incoming_seq =
1158 rcp_ctx->last_seq = incoming_seq;
1159 } else if (session->context->oscore_update_seq_num_cb != NULL) {
1160 /* notify custom credential storage */
1161 coap_lock_callback(session->context->oscore_update_seq_num_cb(session,
1162 rcp_ctx->recipient_id,
1163 osc_ctx->id_context,
1164 rcp_ctx->last_seq,
1165 rcp_ctx->sliding_window));
1166 }
1167 } else { /* !coap_request */
1168 /*
1169 * 8.4 Step 2
1170 * Decompress COSE object
1171 * Get Recipient Context based on token
1172 */
1173 if (oscore_decode_option_value(osc_value, osc_size, cose) == 0) {
1174 coap_log_warn("OSCORE: OSCORE Option cannot be decoded.\n");
1177 session);
1178 goto error;
1179 }
1180 got_resp_piv = cose->partial_iv.length ? 1 : 0;
1181
1182 association = oscore_find_association(session, &pdu_token);
1183 if (association) {
1184 rcp_ctx = association->recipient_ctx;
1185 osc_ctx = rcp_ctx->osc_ctx;
1186 snd_ctx = osc_ctx->sender_context;
1187#if COAP_CLIENT_SUPPORT
1188 sent_pdu = association->sent_pdu;
1189 if (session->b_2_step != COAP_OSCORE_B_2_NONE) {
1190 const uint8_t *ptr = cose->kid_context.s;
1191 size_t length = cose->kid_context.length;
1192
1193 if (ptr) {
1194 /* Need to CBOR unwrap kid_context */
1195 coap_bin_const_t kid_context;
1196
1197 kid_context.length = oscore_cbor_get_element_size(&ptr, &length);
1198 if (kid_context.length > length)
1199 goto error;
1200 /* This has to fit into an OSCORE option max 255.
1201 * Initial byte, kid_context size, partial IV size and kid size have to be there
1202 */
1203 if (kid_context.length >= 255 - 1 - 1 - cose->partial_iv.length - cose->key_id.length)
1204 goto error;
1205 kid_context.s = ptr;
1206 cose_encrypt0_set_kid_context(cose, &kid_context);
1207 }
1208 if (ptr && !coap_binary_equal(osc_ctx->id_context, &cose->kid_context)) {
1209 /* If Appendix B.2 step 3 is in operation */
1210 /* Need to update Security Context with new (R2 || ID1) ID Context */
1212 osc_ctx->id_context->length);
1213
1214 if (kc == NULL) {
1217 session);
1218 goto error;
1219 }
1220
1221 memcpy(kc->s, cose->kid_context.s, cose->kid_context.length);
1222 memcpy(&kc->s[cose->kid_context.length],
1223 osc_ctx->id_context->s,
1224 osc_ctx->id_context->length);
1225
1226 session->b_2_step = COAP_OSCORE_B_2_STEP_3;
1227 coap_log_oscore("Appendix B.2 client step 3 (R2 || ID1)\n");
1228 oscore_update_ctx(osc_ctx, (coap_bin_const_t *)kc);
1229 } else {
1230 session->b_2_step = COAP_OSCORE_B_2_STEP_5;
1231 coap_log_oscore("Appendix B.2 client step 5 (R2 || R3)\n");
1232 }
1233 }
1234#endif /* COAP_CLIENT_SUPPORT */
1235 } else {
1236 coap_log_crit("OSCORE: Security Context association not found\n");
1239 session);
1240 goto error;
1241 }
1242 }
1243
1244 cose_encrypt0_set_alg(cose, osc_ctx->aead_alg);
1245
1246 if (coap_request) {
1247 /*
1248 * RFC8613 8.2 Step 4.
1249 * Compose the External AAD and then AAD
1250 *
1251 * OSCORE_option requires
1252 * partial_iv (cose partial_iv)
1253 * kid_context (cose kid_context)
1254 * key_id (cose key_id)
1255 * group_flag
1256 *
1257 * Non Group requires the following
1258 * RFC8613 5.4
1259 * oscore_version 1
1260 * algorithms [
1261 * aead_alg (osc_ctx)
1262 * ]
1263 * request_kid (request key_id using cose)
1264 * request_piv (request partial_iv using cose)
1265 * options (none at present)
1266 *
1267 * Note: No I options at present
1268 */
1269
1270 /* External AAD */
1271 external_aad.s = external_aad_buffer;
1272 external_aad.length = oscore_prepare_e_aad(osc_ctx,
1273 cose,
1274 osc_value,
1275 osc_size,
1276 NULL,
1277 external_aad_buffer,
1278 sizeof(external_aad_buffer));
1279 cose_encrypt0_set_external_aad(cose, &external_aad);
1280
1281 /* AAD */
1282 aad.s = aad_buffer;
1283 aad.length = oscore_prepare_aad(external_aad_buffer,
1284 external_aad.length,
1285 aad_buffer,
1286 sizeof(aad_buffer));
1287 assert(aad.length < AAD_BUF_LEN);
1288 cose_encrypt0_set_aad(cose, &aad);
1289
1290 /*
1291 * RFC8613 8.2 Step 5.
1292 * Compute the AEAD nonce.
1293 *
1294 * Requires in COSE object as appropriate
1295 * key_id (kid) (Recipient ID)
1296 * partial_iv (as received in request)
1297 * common_iv (already in osc_ctx)
1298 */
1299 nonce.s = nonce_buffer;
1300 nonce.length = 13;
1301 oscore_generate_nonce(cose, osc_ctx, nonce_buffer, 13);
1302 cose_encrypt0_set_nonce(cose, &nonce);
1303 /*
1304 * Set up an association for use in the response
1305 */
1306 association = oscore_find_association(session, &pdu_token);
1307 if (association) {
1308 /* Refresh the association */
1309 coap_delete_bin_const(association->nonce);
1310 association->nonce =
1311 coap_new_bin_const(cose->nonce.s, cose->nonce.length);
1312 if (association->nonce == NULL)
1313 goto error;
1314 coap_delete_bin_const(association->partial_iv);
1315 association->partial_iv =
1317 if (association->partial_iv == NULL)
1318 goto error;
1319 coap_delete_bin_const(association->aad);
1320 association->aad = coap_new_bin_const(cose->aad.s, cose->aad.length);
1321 if (association->aad == NULL)
1322 goto error;
1323 coap_oscore_association_set_recipient_ctx(association, rcp_ctx);
1324 /* So association is not released when handling decrypt */
1325 association = NULL;
1326 } else if (!oscore_new_association(session,
1327 NULL,
1328 &pdu_token,
1329 rcp_ctx,
1330 &cose->aad,
1331 &cose->nonce,
1332 &cose->partial_iv,
1333 0)) {
1334 goto error;
1335 }
1336 /* So association is not released when handling decrypt */
1337 association = NULL;
1338 } else { /* ! coap_request */
1339 /* Need to do nonce before AAD because of different partial_iv */
1340 /*
1341 * 8.4 Step 4.
1342 * Compose the AEAD nonce.
1343 */
1344 cose_encrypt0_set_key_id(cose, rcp_ctx->recipient_id);
1345 if (cose->partial_iv.length == 0) {
1346 cose_encrypt0_set_partial_iv(cose, association->partial_iv);
1347 cose_encrypt0_set_nonce(cose, association->nonce);
1348 } else {
1349 uint64_t last_seq;
1350
1351 if ((rcp_ctx->initial_state == 0 || osc_ctx->rfc8613_b_1_2 == 0) &&
1352 !oscore_validate_sender_seq(rcp_ctx, cose)) {
1353 coap_log_warn("OSCORE: Replayed or old message\n");
1354 goto error;
1355 }
1356 last_seq =
1358 if (rcp_ctx->last_seq>= OSCORE_SEQ_MAX) {
1359 coap_log_warn("OSCORE Replay protection, SEQ larger than SEQ_MAX.\n");
1360 goto error;
1361 }
1362 if (last_seq > rcp_ctx->last_seq)
1363 rcp_ctx->last_seq = last_seq;
1364 /*
1365 * Requires in COSE object as appropriate
1366 * kid (set above)
1367 * partial_iv (as received)
1368 * common_iv (already in osc_ctx)
1369 */
1370 oscore_generate_nonce(cose, osc_ctx, nonce_buffer, 13);
1371 nonce.s = nonce_buffer;
1372 nonce.length = 13;
1373 cose_encrypt0_set_nonce(cose, &nonce);
1374 }
1375#ifdef OSCORE_EXTRA_DEBUG
1376 dump_cose(cose, "!req post set nonce");
1377#endif /* OSCORE_EXTRA_DEBUG */
1378 /*
1379 * 8.4 Step 3.
1380 * Compose the External AAD and then AAD
1381 *
1382 * OSCORE_option requires
1383 * partial_iv (cose partial_iv)
1384 * kid_context (cose kid_context)
1385 * key_id (cose key_id)
1386 * group_flag
1387 *
1388 * Non Group requires the following
1389 * RFC8613 5.4
1390 * oscore_version 1
1391 * algorithms [
1392 * aead_alg (osc_ctx)
1393 * ]
1394 * request_kid (request key_id using cose)
1395 * request_piv (request partial_iv using cose)
1396 * options (none at present)
1397 *
1398 * Note: No I options at present
1399 */
1400
1401 /* External AAD */
1402 cose_encrypt0_set_key_id(cose, snd_ctx->sender_id);
1403 if (association->is_observe && association->obs_partial_iv && got_resp_piv) {
1404 cose_encrypt0_set_partial_iv(cose, association->obs_partial_iv);
1405 } else {
1406 cose_encrypt0_set_partial_iv(cose, association->partial_iv);
1407 }
1408#ifdef OSCORE_EXTRA_DEBUG
1409 dump_cose(cose, "!req pre aad");
1410#endif /* OSCORE_EXTRA_DEBUG */
1411 external_aad.s = external_aad_buffer;
1412 external_aad.length = oscore_prepare_e_aad(osc_ctx,
1413 cose,
1414 NULL,
1415 0,
1416 NULL,
1417 external_aad_buffer,
1418 sizeof(external_aad_buffer));
1419 cose_encrypt0_set_external_aad(cose, &external_aad);
1420
1421 /* AAD */
1422 aad.s = aad_buffer;
1423 aad.length = oscore_prepare_aad(external_aad_buffer,
1424 external_aad.length,
1425 aad_buffer,
1426 sizeof(aad_buffer));
1427 assert(aad.length < AAD_BUF_LEN);
1428 cose_encrypt0_set_aad(cose, &aad);
1429#ifdef OSCORE_EXTRA_DEBUG
1430 dump_cose(cose, "!req post set aad");
1431#endif /* OSCORE_EXTRA_DEBUG */
1432 }
1433
1434 /*
1435 * 8.2 Step 6 / 8.4 Step 5.
1436 * Decrypt the COSE object.
1437 *
1438 * Requires in COSE object as appropriate
1439 * alg (already set)
1440 * key
1441 * nonce (already set)
1442 * aad (already set)
1443 * ciphertext
1444 */
1445 st_encrypt = pdu->data;
1446 encrypt_len = pdu->used_size - (pdu->data - pdu->token);
1447 if (encrypt_len <= 0) {
1448 coap_log_warn("OSCORE: No protected payload\n");
1449 if (!coap_request)
1452 session);
1453 goto error;
1454 }
1455 cose_encrypt0_set_key(cose, rcp_ctx->recipient_key);
1456 cose_encrypt0_set_ciphertext(cose, st_encrypt, encrypt_len);
1457
1458 tag_len = cose_tag_len(cose->alg);
1459 /* Decrypt into plain_pdu, so code (token), options and data are in place */
1460 plain_pdu = coap_pdu_init(pdu->type, 0, 0, encrypt_len /* - tag_len */);
1461 if (plain_pdu == NULL) {
1462 if (!coap_request)
1465 session);
1466 goto error;
1467 }
1468
1469 /* need the tag_len on the end for TinyDTLS to do its work - yuk */
1470 if (!coap_pdu_resize(plain_pdu, encrypt_len /* - tag_len */)) {
1471 if (!coap_request)
1474 session);
1475 goto error;
1476 }
1477
1478 /* Account for 1 byte 'code' used as token */
1479 plain_pdu->e_token_length = 1;
1480 plain_pdu->actual_token.length = 1;
1481 /* Account for the decrypted data */
1482 plain_pdu->used_size = encrypt_len - tag_len;
1483
1484 dump_cose(cose, "Pre decrypt");
1485 pltxt_size =
1486 cose_encrypt0_decrypt(cose, plain_pdu->token, encrypt_len - tag_len);
1487 if (pltxt_size <= 0) {
1488 coap_log_warn("OSCORE: Decryption Failure, result code: %d \n",
1489 (int)pltxt_size);
1490 if (coap_request) {
1491 build_and_send_error_pdu(session,
1492 pdu,
1493 COAP_RESPONSE_CODE(400),
1494 "Decryption failed",
1495 NULL,
1496 NULL,
1497 0);
1498 oscore_roll_back_seq(rcp_ctx);
1499 goto error_no_ack;
1500 } else {
1503 session);
1504 }
1505 goto error;
1506 }
1507
1508 assert((size_t)pltxt_size < pdu->alloc_size + pdu->max_hdr_size);
1509
1510 /* Appendix B.2 Trap */
1511 if (session->b_2_step == COAP_OSCORE_B_2_STEP_2) {
1512 /* Need to update Security Context with new (R2 || ID1) ID Context */
1513 coap_binary_t *kc =
1514 coap_new_binary(sizeof(session->oscore_r2) + cose->kid_context.length);
1515 coap_bin_const_t oscore_r2;
1516
1517 if (kc == NULL) {
1518 if (!coap_request)
1521 session);
1522 goto error;
1523 }
1524
1525 coap_prng_lkd(&session->oscore_r2, sizeof(session->oscore_r2));
1526 memcpy(kc->s, &session->oscore_r2, sizeof(session->oscore_r2));
1527 memcpy(&kc->s[sizeof(session->oscore_r2)],
1528 cose->kid_context.s,
1529 cose->kid_context.length);
1530
1531 coap_log_oscore("Appendix B.2 server step 2 (R2 || ID1)\n");
1532 oscore_update_ctx(osc_ctx, (coap_bin_const_t *)kc);
1533
1534 oscore_r2.length = sizeof(session->oscore_r2);
1535 oscore_r2.s = (const uint8_t *)&session->oscore_r2;
1536 coap_log_oscore("Appendix B.2 server step 2 plain response\n");
1537 build_and_send_error_pdu(session,
1538 pdu,
1539 COAP_RESPONSE_CODE(401),
1540 NULL,
1541 NULL,
1542 &oscore_r2,
1543 1);
1544 goto error_no_ack;
1545 }
1546#if COAP_CLIENT_SUPPORT
1547 if (session->b_2_step == COAP_OSCORE_B_2_STEP_3) {
1548 coap_log_oscore("Appendix B.2 client step 3 (R2 || R3)\n");
1549 coap_pdu_encode_header(plain_pdu, session->proto);
1550 plain_pdu->actual_token.s = plain_pdu->token;
1551 plain_pdu->code = plain_pdu->token[0];
1552 if (plain_pdu->code != COAP_RESPONSE_CODE(401)) {
1553 coap_log_warn("OSCORE Appendix B.2: Expected 4.01 response\n");
1554 }
1555 /* Skip the options */
1556 coap_option_iterator_init(plain_pdu, &opt_iter, COAP_OPT_ALL);
1557 while (coap_option_next(&opt_iter)) {
1558 }
1559 if (opt_iter.length > 0 && opt_iter.next_option &&
1560 opt_iter.next_option[0] == COAP_PAYLOAD_START) {
1561 plain_pdu->data = &opt_iter.next_option[1];
1562 }
1563 coap_log_oscore("Inner Response PDU (plaintext)\n");
1564 coap_show_pdu(COAP_LOG_OSCORE, plain_pdu);
1565 /*
1566 * Need to update Security Context with new (R2 || R3) ID Context
1567 * and retransmit the request
1568 */
1570
1571 if (kc == NULL) {
1572 if (!coap_request)
1575 session);
1576 goto error;
1577 }
1578 memcpy(kc->s, cose->kid_context.s, cose->kid_context.length);
1579 coap_prng_lkd(&kc->s[cose->kid_context.length], 8);
1580
1581 oscore_update_ctx(osc_ctx, (coap_bin_const_t *)kc);
1582
1584 session,
1585 &pdu->actual_token);
1586 if (session->con_active)
1587 session->con_active--;
1588 coap_send_ack_lkd(session, pdu);
1589 if (sent_pdu) {
1590 coap_log_oscore("Appendix B.2 retransmit pdu\n");
1591 if (!session->b_2_retransmit_token) {
1592 session->b_2_retransmit_token = coap_new_str_const(sent_pdu->actual_token.s,
1593 sent_pdu->actual_token.length);
1594 }
1595 if (coap_retransmit_oscore_pdu(session, sent_pdu, NULL) ==
1597 goto error_no_ack;
1598 }
1599 goto error_no_ack;
1600 }
1601#endif /* COAP_CLIENT_SUPPORT */
1602
1603#if COAP_SERVER_SUPPORT
1604 /* Appendix B.1.2 request Trap */
1605 if (coap_request && osc_ctx->rfc8613_b_1_2) {
1606 if (rcp_ctx->initial_state == 1) {
1607 opt = coap_check_option(plain_pdu, COAP_OPTION_ECHO, &opt_iter);
1608 if (opt) {
1609 /* Verify Client is genuine */
1610 if (coap_opt_length(opt) == 8 &&
1611 memcmp(coap_opt_value(opt), rcp_ctx->echo_value, 8) == 0) {
1612 /* This clears rcp_ctx->initial_state */
1613 if (!oscore_validate_sender_seq(rcp_ctx, cose)) {
1614 coap_log_warn("OSCORE: Replayed or old message\n");
1615 build_and_send_error_pdu(session,
1616 pdu,
1617 COAP_RESPONSE_CODE(401),
1618 "Replay detected",
1619 NULL,
1620 NULL,
1621 0);
1622 goto error_no_ack;
1623 } else {
1624 if (session->context->oscore_update_seq_num_cb != NULL) {
1625 /* notify echo challenge changed */
1626 coap_lock_callback(session->context->oscore_update_seq_num_cb(session,
1627 rcp_ctx->recipient_id,
1628 osc_ctx->id_context,
1629 rcp_ctx->last_seq,
1630 rcp_ctx->sliding_window));
1631 }
1632 }
1633 } else
1634 goto error;
1635 } else {
1636 /* RFC 8163 Appendix B.1.2 */
1637 if (session->b_2_step == COAP_OSCORE_B_2_STEP_4) {
1638 session->b_2_step = COAP_OSCORE_B_2_NONE;
1639 coap_log_oscore("Appendix B.2 server finished\n");
1640 }
1641 coap_prng_lkd(rcp_ctx->echo_value, sizeof(rcp_ctx->echo_value));
1642 coap_log_oscore("Appendix B.1.2 server plain response\n");
1643
1644 build_and_send_error_pdu(session,
1645 pdu,
1646 COAP_RESPONSE_CODE(401),
1647 NULL,
1648 rcp_ctx->echo_value,
1649 NULL,
1650 1);
1651 goto error_no_ack;
1652 }
1653 }
1654 }
1655#endif /* COAP_SERVER_SUPPORT */
1656
1657 /*
1658 * 8.2 Step 7 / 8.4 Step 6.
1659 * Add decrypted Code, options and payload
1660 * [OSCORE option not copied across previously]
1661 */
1662
1663 /* PDU code is pseudo plain_pdu token */
1664 decrypt_pdu->code = plain_pdu->token[0];
1665
1666 /* Copy inner decrypted options across */
1667 coap_option_iterator_init(plain_pdu, &opt_iter, COAP_OPT_ALL);
1668 while ((opt = coap_option_next(&opt_iter))) {
1669 size_t len;
1670 size_t bias;
1671
1672 switch (opt_iter.number) {
1673 case COAP_OPTION_OSCORE:
1674 break;
1676 if (!coap_request) {
1677 bias = cose->partial_iv.length > 3 ? cose->partial_iv.length - 3 : 0;
1678 len = cose->partial_iv.length > 3 ? 3 : cose->partial_iv.length;
1679 /* Make Observe option reflect last 3 bytes of partial_iv */
1681 decrypt_pdu,
1682 opt_iter.number,
1683 len,
1684 cose->partial_iv.s ? &cose->partial_iv.s[bias] : NULL)) {
1687 session);
1688 goto error;
1689 }
1690 doing_resp_observe = 1;
1691 break;
1692 }
1693 association = oscore_find_association(session, &pdu_token);
1694 if (association) {
1695 association->is_observe = 1;
1696 association = NULL;
1697 }
1698 /* Fall Through */
1699 default:
1700 if (!coap_insert_option(decrypt_pdu,
1701 opt_iter.number,
1702 coap_opt_length(opt),
1703 coap_opt_value(opt))) {
1704 if (!coap_request)
1707 session);
1708 goto error;
1709 }
1710 break;
1711 }
1712 }
1713 if (!coap_request && !doing_resp_observe) {
1714 if (association) {
1715 association->is_observe = 0;
1716 }
1717 }
1718 /* Need to copy across any data */
1719 if (opt_iter.length > 0 && opt_iter.next_option &&
1720 opt_iter.next_option[0] == COAP_PAYLOAD_START) {
1721 plain_pdu->data = &opt_iter.next_option[1];
1722 if (!coap_add_data(decrypt_pdu,
1723 plain_pdu->used_size -
1724 (plain_pdu->data - plain_pdu->token),
1725 plain_pdu->data)) {
1726 if (!coap_request)
1729 session);
1730 goto error;
1731 }
1732 }
1733 coap_delete_pdu_lkd(plain_pdu);
1734 plain_pdu = NULL;
1735
1736 /* Make sure headers are correctly set up */
1737 if (!coap_pdu_encode_header(decrypt_pdu, session->proto)) {
1738 if (!coap_request)
1741 session);
1742 goto error;
1743 }
1744
1745 if (session->b_2_step != COAP_OSCORE_B_2_NONE) {
1746 session->b_2_step = COAP_OSCORE_B_2_NONE;
1747 coap_log_oscore("Appendix B.2 client finished\n");
1748 if (session->b_2_retransmit_token) {
1749 coap_update_token(decrypt_pdu, session->b_2_retransmit_token->length,
1750 session->b_2_retransmit_token->s);
1751 coap_delete_str_const(session->b_2_retransmit_token);
1752 session->b_2_retransmit_token = NULL;
1753 }
1754 }
1755#if COAP_CLIENT_SUPPORT
1756 if (decrypt_pdu->code == COAP_RESPONSE_CODE(401) &&
1757 (opt = coap_check_option(decrypt_pdu, COAP_OPTION_ECHO, &opt_iter))) {
1758 /* Server is requesting Echo refresh check */
1760 session,
1761 &pdu->actual_token);
1762 if (session->con_active)
1763 session->con_active--;
1764 if (!sent_pdu) {
1765 coap_lg_crcv_t *lg_crcv = coap_find_lg_crcv(session, pdu);
1766
1767 if (lg_crcv)
1768 sent_pdu = lg_crcv->sent_pdu;
1769 }
1770 if (sent_pdu) {
1771 coap_send_ack_lkd(session, pdu);
1772 coap_log_debug("PDU requesting re-transmit\n");
1773 coap_show_pdu(COAP_LOG_DEBUG, decrypt_pdu);
1774 coap_log_oscore("RFC9175 retransmit pdu\n");
1775 /* Do not care if this fails */
1776 if (coap_retransmit_oscore_pdu(session, sent_pdu, opt) != COAP_INVALID_MID) {
1777 session->doing_b_1_2 = 1;
1778 }
1779 goto error_no_ack;
1780 }
1781 } else if (session->doing_b_1_2) {
1782 coap_lg_crcv_t *lg_crcv = coap_find_lg_crcv(session, pdu);
1783
1784 if (lg_crcv) {
1785 if (!coap_binary_equal(&decrypt_pdu->actual_token, lg_crcv->app_token)) {
1786 coap_update_token(decrypt_pdu, lg_crcv->app_token->length, lg_crcv->app_token->s);
1787 }
1788 }
1789 session->doing_b_1_2 = 0;
1790 }
1791#endif /* COAP_CLIENT_SUPPORT */
1792 if (association && association->is_observe == 0)
1793 oscore_delete_association(session, association);
1794 return decrypt_pdu;
1795
1796error:
1797 coap_send_ack_lkd(session, pdu);
1798error_no_ack:
1799 if (association && association->is_observe == 0)
1800 oscore_delete_association(session, association);
1801 coap_delete_pdu_lkd(decrypt_pdu);
1802 coap_delete_pdu_lkd(plain_pdu);
1803 return NULL;
1804}
1805
1806typedef enum {
1807 COAP_ENC_ASCII = 0x001,
1808 COAP_ENC_HEX = 0x002,
1809 COAP_ENC_CONFIG = 0x0200,
1810 COAP_ENC_INTEGER = 0x400,
1811 COAP_ENC_TEXT = 0x800,
1812 COAP_ENC_BOOL = 0x1000,
1813 COAP_ENC_UNSIGNED64 = 0x2000,
1814 COAP_ENC_LAST
1815} coap_oscore_coding_t;
1816
1817#undef TEXT_MAPPING
1818#define TEXT_MAPPING(t, v) \
1819 { { sizeof(#t)-1, (const uint8_t *)#t }, v }
1820
1821static struct coap_oscore_encoding_t {
1822 coap_str_const_t name;
1823 coap_oscore_coding_t encoding;
1824} oscore_encoding[] = {
1825 TEXT_MAPPING(ascii, COAP_ENC_ASCII),
1826 TEXT_MAPPING(hex, COAP_ENC_HEX),
1827 TEXT_MAPPING(config, COAP_ENC_CONFIG),
1828 TEXT_MAPPING(integer, COAP_ENC_INTEGER),
1829 TEXT_MAPPING(unsigned64, COAP_ENC_UNSIGNED64),
1830 TEXT_MAPPING(text, COAP_ENC_TEXT),
1831 TEXT_MAPPING(bool, COAP_ENC_BOOL),
1832 {{0, NULL}, COAP_ENC_LAST}
1833};
1834
1835typedef struct {
1836 coap_oscore_coding_t encoding;
1837 const char *encoding_name;
1838 union {
1839 int value_int;
1840 uint64_t value_int64;
1841 coap_bin_const_t *value_bin;
1842 coap_str_const_t value_str;
1843 } u;
1844} oscore_value_t;
1845
1846/*
1847 * Return 1 if hex character
1848 * 0 not hex character
1849 */
1850static int
1851hex_to_char(char c, uint8_t *value) {
1852 if ('a' <= c && c <= 'f') {
1853 *value = c - 'a' + 10;
1854 } else if ('A' <= c && c <= 'F') {
1855 *value = c - 'A' + 10;
1856 } else if ('0' <= c && c <= '9') {
1857 *value = c - '0';
1858 } else {
1859 *value = 0;
1860 return 0;
1861 }
1862 return 1;
1863}
1864
1865/* Parse the hex into binary */
1866static coap_bin_const_t *
1867parse_hex_bin(const char *begin, const char *end) {
1868 coap_binary_t *binary = NULL;
1869 size_t i;
1870 size_t o = 0;
1871
1872 binary = coap_new_binary((end - begin) / 2);
1873 if (binary == NULL)
1874 goto bad_entry;
1875 for (i = 0; (i < (size_t)(end - begin)); i++) {
1876 while ((i < (size_t)(end - begin)) &&
1877 (begin[i] == '\r' || begin[i] == '\n' || begin[i] == ' ')) {
1878 i++;
1879 }
1880 if (i == (size_t)(end - begin))
1881 break;
1882 if (isxdigit((uint8_t)begin[i])) {
1883 uint8_t value;
1884
1885 if (!hex_to_char(begin[i], &value)) {
1886 goto bad_entry;
1887 }
1888 binary->s[o] = value << 4;
1889 i++;
1890 while ((i < (size_t)(end - begin)) &&
1891 (begin[i] == '\r' || begin[i] == '\n' || begin[i] == ' ')) {
1892 i++;
1893 }
1894 if (i == (size_t)(end - begin))
1895 goto bad_entry;
1896 if (!hex_to_char(begin[i], &value)) {
1897 goto bad_entry;
1898 }
1899 binary->s[o++] += value;
1900 } else {
1901 break;
1902 }
1903 }
1904 if (i != (size_t)(end - begin))
1905 goto bad_entry;
1906 return (coap_bin_const_t *)binary;
1907
1908bad_entry:
1909 coap_delete_binary(binary);
1910 return NULL;
1911}
1912
1913/*
1914 * Break up each OSCORE Configuration line entry into the 3 parts which
1915 * are comma separated
1916 *
1917 * keyword,encoding,value
1918 */
1919static int
1920get_split_entry(const char **start,
1921 size_t size,
1922 coap_str_const_t *keyword,
1923 oscore_value_t *value,
1924 cose_curve_t sign_curve) {
1925 const char *begin = *start;
1926 const char *keep_start = *start;
1927 const char *end;
1928 const char *kend;
1929 const char *tend;
1930 const char *split;
1931 size_t i;
1932 size_t len;
1933 (void)sign_curve;
1934
1935retry:
1936 kend = end = memchr(begin, '\n', size);
1937 if (end == NULL)
1938 return 0;
1939
1940 /* Check for multi-line */
1941 if ((tend = memchr(begin, '"', end - begin))) {
1942 /* See if " terminator is on the same line */
1943 if (!memchr(tend + 1, '"', end - tend -1)) {
1944 /* Over multiple lines */
1945 kend = end = memchr(end, '"', size - (end - begin));
1946 if (end == NULL)
1947 return 0;
1948 kend = end = memchr(kend, '\n', size - (kend - begin));
1949 if (end == NULL)
1950 return 0;
1951 }
1952 }
1953
1954 /* Track beginning of next line */
1955 *start = end + 1;
1956 if (end > begin && end[-1] == '\r')
1957 end--;
1958
1959 if (begin[0] == '#' || (end - begin) == 0) {
1960 /* Skip comment / blank line */
1961 size -= kend - begin + 1;
1962 begin = *start;
1963 keep_start = *start;
1964 goto retry;
1965 }
1966
1967 /* Get in the keyword */
1968 split = memchr(begin, ',', end - begin);
1969 if (split == NULL)
1970 goto bad_entry;
1971
1972 keyword->s = (const uint8_t *)begin;
1973 keyword->length = split - begin;
1974
1975 begin = split + 1;
1976 if ((end - begin) == 0)
1977 goto bad_entry;
1978 /* Get in the encoding */
1979 split = memchr(begin, ',', end - begin);
1980 if (split == NULL)
1981 goto bad_entry;
1982
1983 for (i = 0; oscore_encoding[i].name.s; i++) {
1984 coap_str_const_t temp = { split - begin, (const uint8_t *)begin };
1985
1986 if (coap_string_equal(&temp, &oscore_encoding[i].name)) {
1987 value->encoding = oscore_encoding[i].encoding;
1988 value->encoding_name = (const char *)oscore_encoding[i].name.s;
1989 break;
1990 }
1991 }
1992 if (oscore_encoding[i].name.s == NULL)
1993 goto bad_entry;
1994
1995 begin = split + 1;
1996 if ((end - begin) == 0)
1997 goto bad_entry;
1998 /* Get in the keyword's value */
1999 if (value->encoding == COAP_ENC_CONFIG && begin[0] == '\'') {
2000 split = memchr(&begin[1], '\'', size - (begin - keep_start) - 1);
2001 if (split == NULL)
2002 goto bad_entry;
2003 end = memchr(split, '\n', size - (split - keep_start));
2004 if (end == NULL)
2005 return 0;
2006 *start = end + 1;
2007 end = split;
2008 begin++;
2009 } else {
2010 if (begin[0] == '"') {
2011 split = memchr(&begin[1], '"', end - split - 1);
2012 if (split == NULL)
2013 goto bad_entry;
2014 end = split;
2015 begin++;
2016 }
2017 }
2018 switch (value->encoding) {
2019 case COAP_ENC_CONFIG:
2020 case COAP_ENC_ASCII:
2021 value->u.value_bin =
2022 coap_new_bin_const((const uint8_t *)begin, end - begin);
2023 if (value->u.value_bin == NULL)
2024 goto bad_entry;
2025 break;
2026 case COAP_ENC_HEX:
2027 /* Parse the hex into binary */
2028 value->u.value_bin = parse_hex_bin(begin, end);
2029 if (value->u.value_bin == NULL)
2030 goto bad_entry;
2031 break;
2032 case COAP_ENC_INTEGER:
2033 value->u.value_int = atoi(begin);
2034 break;
2035 case COAP_ENC_UNSIGNED64: {
2036 value->u.value_int64 = strtoull(begin, NULL, 10);
2037 break;
2038 }
2039 case COAP_ENC_TEXT:
2040 value->u.value_str.s = (const uint8_t *)begin;
2041 value->u.value_str.length = end - begin;
2042 break;
2043 case COAP_ENC_BOOL:
2044 len = (size_t)(end - begin);
2045 if (len == 4 && memcmp("true", begin, len) == 0)
2046 value->u.value_int = 1;
2047 else if (len == 5 && memcmp("false", begin, len) == 0)
2048 value->u.value_int = 0;
2049 else
2050 goto bad_entry;
2051 break;
2052 case COAP_ENC_LAST:
2053 default:
2054 goto bad_entry;
2055 }
2056 return 1;
2057
2058bad_entry:
2059 coap_log_warn("oscore_conf: Unrecognized configuration entry '%.*s'\n",
2060 (int)(end - begin),
2061 begin);
2062 return -1;
2063
2064}
2065
2066#undef CONFIG_ENTRY
2067#define CONFIG_ENTRY(n, e, t) \
2068 { { sizeof(#n)-1, (const uint8_t *)#n }, e, \
2069 offsetof(coap_oscore_conf_t, n), t }
2070
2071#undef CONFIG_SND_ENTRY
2072#define CONFIG_SND_ENTRY(n, e, t) \
2073 { { sizeof(#n)-1, (const uint8_t *)#n }, e, \
2074 offsetof(coap_oscore_snd_conf_t, n), t }
2075
2076#undef CONFIG_RCP_ENTRY
2077#define CONFIG_RCP_ENTRY(n, e, t) \
2078 { { sizeof(#n)-1, (const uint8_t *)#n }, e, \
2079 offsetof(coap_oscore_rcp_conf_t, n), t }
2080
2081#undef CONFIG_DUMMY_ENTRY
2082#define CONFIG_DUMMY_ENTRY(n, e, t) \
2083 { { sizeof(#n)-1, (const uint8_t *)#n }, e, 0, t }
2084
2085typedef struct oscore_text_mapping_t {
2086 coap_str_const_t text;
2087 int value;
2088} oscore_text_mapping_t;
2089
2090/* Naming as per https://www.iana.org/assignments/cose/cose.xhtml#algorithms */
2091static oscore_text_mapping_t text_aead_alg[] = {
2092 TEXT_MAPPING(AES-CCM-16-64-128, COSE_ALGORITHM_AES_CCM_16_64_128),
2093 TEXT_MAPPING(AES-CCM-16-64-256, COSE_ALGORITHM_AES_CCM_16_64_256),
2094 {{0, NULL}, 0}
2095};
2096
2097static oscore_text_mapping_t text_hkdf_alg[] = {
2098 TEXT_MAPPING(direct+HKDF-SHA-256, COSE_HKDF_ALG_HKDF_SHA_256),
2099 {{0, NULL}, 0}
2100};
2101
2102typedef struct oscore_config_t {
2103 coap_str_const_t str_keyword;
2104 coap_oscore_coding_t encoding;
2105 size_t offset;
2106 oscore_text_mapping_t *text_mapping;
2107} oscore_config_t;
2108
2109static oscore_config_t oscore_config[] = {
2110 CONFIG_ENTRY(aead_alg, COAP_ENC_INTEGER | COAP_ENC_TEXT, text_aead_alg),
2111 CONFIG_ENTRY(hkdf_alg, COAP_ENC_INTEGER | COAP_ENC_TEXT, text_hkdf_alg),
2112 CONFIG_ENTRY(master_secret, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
2113 CONFIG_ENTRY(master_salt, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
2114 CONFIG_ENTRY(id_context, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
2115
2116 CONFIG_DUMMY_ENTRY(sender_id, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
2117
2118 CONFIG_DUMMY_ENTRY(recipient_id, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
2119
2120 CONFIG_ENTRY(replay_window, COAP_ENC_INTEGER, NULL),
2121 CONFIG_ENTRY(ssn_freq, COAP_ENC_INTEGER, NULL),
2122 CONFIG_ENTRY(rfc8613_b_1_2, COAP_ENC_BOOL, NULL),
2123 CONFIG_ENTRY(rfc8613_b_2, COAP_ENC_BOOL, NULL),
2124 CONFIG_ENTRY(break_sender_key, COAP_ENC_BOOL, NULL),
2125 CONFIG_ENTRY(break_recipient_key, COAP_ENC_BOOL, NULL),
2126 CONFIG_DUMMY_ENTRY(complex_sender, COAP_ENC_CONFIG, NULL),
2127 CONFIG_DUMMY_ENTRY(complex_recipient, COAP_ENC_CONFIG, NULL),
2128};
2129
2130static oscore_config_t oscore_snd_config[] = {
2131 CONFIG_SND_ENTRY(sender_id, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
2132};
2133
2134static oscore_config_t oscore_rcp_config[] = {
2135 CONFIG_RCP_ENTRY(recipient_id, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
2136 CONFIG_RCP_ENTRY(silent_server, COAP_ENC_BOOL, NULL),
2137 CONFIG_DUMMY_ENTRY(last_seq, COAP_ENC_UNSIGNED64, NULL),
2138 CONFIG_DUMMY_ENTRY(sliding_window, COAP_ENC_UNSIGNED64, NULL),
2139};
2140
2141
2142int
2144 if (snd_conf == NULL)
2145 return 0;
2146
2148 coap_free_type(COAP_STRING, snd_conf);
2149 return 1;
2150}
2151
2152int
2154 if (rcp_conf == NULL)
2155 return 0;
2156
2158 coap_free_type(COAP_STRING, rcp_conf);
2159 return 1;
2160}
2161
2162int
2164 coap_oscore_rcp_conf_t *rcp_conf;
2165 if (oscore_conf == NULL)
2166 return 0;
2167
2169 coap_delete_bin_const(oscore_conf->master_salt);
2170 coap_delete_bin_const(oscore_conf->id_context);
2171 coap_delete_oscore_snd_conf(oscore_conf->sender);
2172
2173 rcp_conf = oscore_conf->recipient_chain;
2174 while (rcp_conf) {
2175 coap_oscore_rcp_conf_t *rcp_next = rcp_conf->next_recipient;
2176
2178 rcp_conf = rcp_next;
2179 }
2180
2181 coap_free_type(COAP_STRING, oscore_conf);
2182 return 1;
2183}
2184
2186coap_parse_oscore_snd_conf_mem(coap_bin_const_t conf_mem) {
2187 const char *start = (const char *)conf_mem.s;
2188 const char *end = start + conf_mem.length;
2189 coap_str_const_t keyword;
2190 oscore_value_t value;
2191 coap_oscore_snd_conf_t *snd_conf;
2192
2194 if (snd_conf == NULL)
2195 return NULL;
2196 memset(snd_conf, 0, sizeof(coap_oscore_snd_conf_t));
2197
2198 memset(&value, 0, sizeof(value));
2199
2200 while (end > start &&
2201 get_split_entry(&start, end - start, &keyword, &value, 0) > 0) {
2202 size_t i;
2203 size_t j;
2204
2205 for (i = 0; i < sizeof(oscore_snd_config) / sizeof(oscore_snd_config[0]); i++) {
2206 if (coap_string_equal(&oscore_snd_config[i].str_keyword, &keyword) != 0 &&
2207 value.encoding & oscore_snd_config[i].encoding) {
2208 coap_bin_const_t *unused_check;
2209
2210 switch (value.encoding) {
2211 case COAP_ENC_HEX:
2212 case COAP_ENC_ASCII:
2213 memcpy(&unused_check,
2214 &(((char *)snd_conf)[oscore_snd_config[i].offset]),
2215 sizeof(unused_check));
2216 if (unused_check != NULL) {
2217 coap_log_warn("oscore_snd_conf: Keyword '%.*s' duplicated\n",
2218 (int)keyword.length,
2219 (const char *)keyword.s);
2220 coap_delete_bin_const(value.u.value_bin);
2221 goto error;
2222 }
2223 memcpy(&(((char *)snd_conf)[oscore_snd_config[i].offset]),
2224 &value.u.value_bin,
2225 sizeof(value.u.value_bin));
2226 break;
2227 case COAP_ENC_INTEGER:
2228 case COAP_ENC_BOOL:
2229 memcpy(&(((char *)snd_conf)[oscore_snd_config[i].offset]),
2230 &value.u.value_int,
2231 sizeof(value.u.value_int));
2232 break;
2233 case COAP_ENC_TEXT:
2234 for (j = 0; oscore_snd_config[i].text_mapping[j].text.s != NULL; j++) {
2235 if (memcmp(value.u.value_str.s,
2236 oscore_snd_config[i].text_mapping[j].text.s,
2237 value.u.value_str.length) == 0) {
2238 memcpy(&(((char *)snd_conf)[oscore_snd_config[i].offset]),
2239 &oscore_snd_config[i].text_mapping[j].value,
2240 sizeof(oscore_snd_config[i].text_mapping[j].value));
2241 break;
2242 }
2243 }
2244 if (oscore_snd_config[i].text_mapping[j].text.s == NULL) {
2245 coap_log_warn("oscore_snd_conf: Keyword '%.*s': value '%.*s' unknown\n",
2246 (int)keyword.length,
2247 (const char *)keyword.s,
2248 (int)value.u.value_str.length,
2249 (const char *)value.u.value_str.s);
2250 goto error;
2251 }
2252 break;
2253 case COAP_ENC_CONFIG:
2254 case COAP_ENC_UNSIGNED64:
2255 case COAP_ENC_LAST:
2256 default:
2257 assert(0);
2258 break;
2259 }
2260 break;
2261 }
2262 }
2263 if (i == sizeof(oscore_snd_config) / sizeof(oscore_snd_config[0])) {
2264 coap_log_warn("oscore_snd_conf: Keyword '%.*s', type '%s' unknown\n",
2265 (int)keyword.length,
2266 (const char *)keyword.s,
2267 value.encoding_name);
2268 if (value.encoding == COAP_ENC_HEX || value.encoding == COAP_ENC_ASCII ||
2269 value.encoding == COAP_ENC_CONFIG)
2270 coap_delete_bin_const(value.u.value_bin);
2271 goto error;
2272 }
2273 }
2274 if (!snd_conf->sender_id) {
2275 coap_log_warn("oscore_snd_conf: sender_id not defined\n");
2276 goto error;
2277 }
2278 if (snd_conf->sender_id->length > 7) {
2279 coap_log_warn("oscore_snd_conf: Maximum size of sender_id is 7 bytes\n");
2280 goto error;
2281 }
2282 return snd_conf;
2283
2284error:
2286 return NULL;
2287}
2288
2290coap_parse_oscore_rcp_conf_mem(coap_bin_const_t conf_mem) {
2291 const char *start = (const char *)conf_mem.s;
2292 const char *end = start + conf_mem.length;
2293 coap_str_const_t keyword;
2294 oscore_value_t value;
2295 coap_oscore_rcp_conf_t *rcp_conf;
2296
2298 if (rcp_conf == NULL)
2299 return NULL;
2300 memset(rcp_conf, 0, sizeof(coap_oscore_rcp_conf_t));
2301
2302 memset(&value, 0, sizeof(value));
2303
2304 while (end > start &&
2305 get_split_entry(&start, end - start, &keyword, &value, 0) > 0) {
2306 size_t i;
2307 size_t j;
2308
2309 for (i = 0; i < sizeof(oscore_rcp_config) / sizeof(oscore_rcp_config[0]); i++) {
2310 if (coap_string_equal(&oscore_rcp_config[i].str_keyword, &keyword) != 0 &&
2311 value.encoding & oscore_rcp_config[i].encoding) {
2312 if (coap_string_equal(coap_make_str_const("last_seq"), &keyword)) {
2313 rcp_conf->last_seq = value.u.value_int64;
2314 rcp_conf->window_initialized = 1;
2315 } else if (coap_string_equal(coap_make_str_const("sliding_window"), &keyword)) {
2316 rcp_conf->sliding_window = value.u.value_int64;
2317 rcp_conf->window_initialized = 1;
2318 } else {
2319 coap_bin_const_t *unused_check;
2320
2321 switch (value.encoding) {
2322 case COAP_ENC_HEX:
2323 case COAP_ENC_ASCII:
2324 memcpy(&unused_check,
2325 &(((char *)rcp_conf)[oscore_rcp_config[i].offset]),
2326 sizeof(unused_check));
2327 if (unused_check != NULL) {
2328 coap_log_warn("oscore_rcp_conf: Keyword '%.*s' duplicated\n",
2329 (int)keyword.length,
2330 (const char *)keyword.s);
2331 coap_delete_bin_const(value.u.value_bin);
2332 goto error;
2333 }
2334 memcpy(&(((char *)rcp_conf)[oscore_rcp_config[i].offset]),
2335 &value.u.value_bin,
2336 sizeof(value.u.value_bin));
2337 break;
2338 case COAP_ENC_INTEGER:
2339 case COAP_ENC_BOOL:
2340 memcpy(&(((char *)rcp_conf)[oscore_rcp_config[i].offset]),
2341 &value.u.value_int,
2342 sizeof(value.u.value_int));
2343 break;
2344 case COAP_ENC_TEXT:
2345 for (j = 0; oscore_rcp_config[i].text_mapping[j].text.s != NULL; j++) {
2346 if (memcmp(value.u.value_str.s,
2347 oscore_rcp_config[i].text_mapping[j].text.s,
2348 value.u.value_str.length) == 0) {
2349 memcpy(&(((char *)rcp_conf)[oscore_rcp_config[i].offset]),
2350 &oscore_rcp_config[i].text_mapping[j].value,
2351 sizeof(oscore_rcp_config[i].text_mapping[j].value));
2352 break;
2353 }
2354 }
2355 if (oscore_rcp_config[i].text_mapping[j].text.s == NULL) {
2356 coap_log_warn("oscore_rcp_conf: Keyword '%.*s': value '%.*s' unknown\n",
2357 (int)keyword.length,
2358 (const char *)keyword.s,
2359 (int)value.u.value_str.length,
2360 (const char *)value.u.value_str.s);
2361 goto error;
2362 }
2363 break;
2364 case COAP_ENC_CONFIG:
2365 case COAP_ENC_UNSIGNED64:
2366 case COAP_ENC_LAST:
2367 default:
2368 assert(0);
2369 break;
2370 }
2371 }
2372 break;
2373 }
2374 }
2375 if (i == sizeof(oscore_rcp_config) / sizeof(oscore_rcp_config[0])) {
2376 coap_log_warn("oscore_rcp_conf: Keyword '%.*s', type '%s' unknown\n",
2377 (int)keyword.length,
2378 (const char *)keyword.s,
2379 value.encoding_name);
2380 if (value.encoding == COAP_ENC_HEX || value.encoding == COAP_ENC_ASCII ||
2381 value.encoding == COAP_ENC_CONFIG)
2382 coap_delete_bin_const(value.u.value_bin);
2383 goto error;
2384 }
2385 }
2386 if (!rcp_conf->recipient_id) {
2387 coap_log_warn("oscore_rcp_conf: recipient_id not defined\n");
2388 goto error;
2389 }
2390 if (rcp_conf->recipient_id->length > 7) {
2391 coap_log_warn("oscore_rcp_conf: Maximum size of recipient_id is 7 bytes\n");
2392 goto error;
2393 }
2394 return rcp_conf;
2395
2396error:
2398 return NULL;
2399}
2400
2401static coap_oscore_conf_t *
2402coap_parse_oscore_conf_mem(coap_str_const_t conf_mem) {
2403 const char *start = (const char *)conf_mem.s;
2404 const char *end = start + conf_mem.length;
2405 coap_str_const_t keyword;
2406 oscore_value_t value;
2407 coap_oscore_conf_t *oscore_conf;
2408 int split_ok = -1;
2409
2410 oscore_conf = coap_malloc_type(COAP_STRING, sizeof(coap_oscore_conf_t));
2411 if (oscore_conf == NULL)
2412 return NULL;
2413 memset(oscore_conf, 0, sizeof(coap_oscore_conf_t));
2414
2415 memset(&value, 0, sizeof(value));
2416 /* Preset with defaults RFC8612 3.2 */
2418 oscore_conf->ssn_freq = 1;
2420 oscore_conf->hkdf_alg = COSE_HKDF_ALG_HKDF_SHA_256;
2421
2422 oscore_conf->rfc8613_b_1_2 = 1;
2423 oscore_conf->rfc8613_b_2 = 0;
2424 /* For debugging only */
2425 oscore_conf->break_sender_key = 0;
2426 oscore_conf->break_recipient_key = 0;
2427
2428 while (end > start &&
2429 (split_ok = get_split_entry(&start, end - start, &keyword, &value, 0)) > 0) {
2430 size_t i;
2431 size_t j;
2432
2433 for (i = 0; i < sizeof(oscore_config) / sizeof(oscore_config[0]); i++) {
2434 if (coap_string_equal(&oscore_config[i].str_keyword, &keyword) != 0 &&
2435 value.encoding & oscore_config[i].encoding) {
2436 if (coap_string_equal(coap_make_str_const("sender_id"), &keyword)) {
2437 if (value.u.value_bin->length > 7) {
2438 coap_log_warn("oscore_conf: Maximum size of sender_id is 7 bytes\n");
2439 goto error_free_value_bin;
2440 }
2441 if (oscore_conf->sender) {
2442 coap_log_warn("oscore_conf: sender_id duplicated\n");
2443 goto error_free_value_bin;
2444 }
2446 if (!oscore_conf->sender)
2447 goto error_free_value_bin;
2448
2449 memset(oscore_conf->sender, 0, sizeof(coap_oscore_snd_conf_t));
2450 oscore_conf->sender->sender_id = value.u.value_bin;
2451 } else if (coap_string_equal(coap_make_str_const("recipient_id"), &keyword)) {
2452 coap_oscore_rcp_conf_t *rcp_conf;
2453
2454 if (value.u.value_bin->length > 7) {
2455 coap_log_warn("oscore_conf: Maximum size of recipient_id is 7 bytes\n");
2456 goto error_free_value_bin;
2457 }
2458 /* Special case as there are potentially multiple entries */
2460 if (!rcp_conf)
2461 goto error_free_value_bin;
2462
2463 memset(rcp_conf, 0, sizeof(coap_oscore_rcp_conf_t));
2464 rcp_conf->recipient_id = value.u.value_bin;
2465 rcp_conf->next_recipient = oscore_conf->recipient_chain;
2466 oscore_conf->recipient_chain = rcp_conf;
2467 } else {
2468 coap_bin_const_t *unused_check;
2469
2470 switch (value.encoding) {
2471 case COAP_ENC_CONFIG:
2472 if (keyword.length == sizeof("complex_sender") - 1 &&
2473 memcmp(keyword.s, "complex_sender", keyword.length) == 0) {
2474 coap_oscore_snd_conf_t *snd_conf =
2475 coap_parse_oscore_snd_conf_mem(*value.u.value_bin);
2476
2477 if (!snd_conf) {
2478 coap_log_warn("oscore_conf: Keyword '%.*s' invalid\n",
2479 (int)keyword.length,
2480 (const char *)keyword.s);
2481 goto error_free_value_bin;
2482 }
2483 if (oscore_conf->sender) {
2484 coap_log_warn("oscore_conf: complex_sender duplicated\n");
2486 goto error_free_value_bin;
2487 }
2488 oscore_conf->sender = snd_conf;
2489 coap_delete_bin_const(value.u.value_bin);
2490 break;
2491 }
2492 if (keyword.length == sizeof("complex_recipient") - 1 &&
2493 memcmp(keyword.s, "complex_recipient", keyword.length) == 0) {
2494 coap_oscore_rcp_conf_t *rcp_conf =
2495 coap_parse_oscore_rcp_conf_mem(*value.u.value_bin);
2496
2497 if (!rcp_conf) {
2498 coap_log_warn("oscore_conf: Keyword '%.*s' invalid\n",
2499 (int)keyword.length,
2500 (const char *)keyword.s);
2501 goto error_free_value_bin;
2502 }
2503 rcp_conf->next_recipient = oscore_conf->recipient_chain;
2504 oscore_conf->recipient_chain = rcp_conf;
2505 coap_delete_bin_const(value.u.value_bin);
2506 }
2507 break;
2508 case COAP_ENC_ASCII:
2509 case COAP_ENC_HEX:
2510 memcpy(&unused_check,
2511 &(((char *)oscore_conf)[oscore_config[i].offset]),
2512 sizeof(unused_check));
2513 if (unused_check != NULL) {
2514 coap_log_warn("oscore_conf: Keyword '%.*s' duplicated\n",
2515 (int)keyword.length,
2516 (const char *)keyword.s);
2517 goto error_free_value_bin;
2518 }
2519 memcpy(&(((char *)oscore_conf)[oscore_config[i].offset]),
2520 &value.u.value_bin,
2521 sizeof(value.u.value_bin));
2522 break;
2523 case COAP_ENC_INTEGER:
2524 case COAP_ENC_BOOL:
2525 memcpy(&(((char *)oscore_conf)[oscore_config[i].offset]),
2526 &value.u.value_int,
2527 sizeof(value.u.value_int));
2528 break;
2529 case COAP_ENC_TEXT:
2530 for (j = 0; oscore_config[i].text_mapping[j].text.s != NULL; j++) {
2531 if (coap_string_equal(&value.u.value_str,
2532 &oscore_config[i].text_mapping[j].text)) {
2533 memcpy(&(((char *)oscore_conf)[oscore_config[i].offset]),
2534 &oscore_config[i].text_mapping[j].value,
2535 sizeof(oscore_config[i].text_mapping[j].value));
2536 break;
2537 }
2538 }
2539 if (oscore_config[i].text_mapping[j].text.s == NULL) {
2540 coap_log_warn("oscore_conf: Keyword '%.*s': value '%.*s' unknown\n",
2541 (int)keyword.length,
2542 (const char *)keyword.s,
2543 (int)value.u.value_str.length,
2544 (const char *)value.u.value_str.s);
2545 goto error;
2546 }
2547 break;
2548 case COAP_ENC_UNSIGNED64:
2549 case COAP_ENC_LAST:
2550 default:
2551 assert(0);
2552 break;
2553 }
2554 }
2555 break;
2556 }
2557 }
2558 if (i == sizeof(oscore_config) / sizeof(oscore_config[0])) {
2559 coap_log_warn("oscore_conf: Keyword '%.*s', type '%s' unknown\n",
2560 (int)keyword.length,
2561 (const char *)keyword.s,
2562 value.encoding_name);
2563 if (value.encoding == COAP_ENC_HEX || value.encoding == COAP_ENC_ASCII ||
2564 value.encoding == COAP_ENC_CONFIG) {
2565 coap_delete_bin_const(value.u.value_bin);
2566 }
2567 goto error;
2568 }
2569 }
2570 if (split_ok == -1)
2571 goto error;
2572 if (!oscore_conf->master_secret) {
2573 coap_log_warn("oscore_conf: master_secret not defined\n");
2574 goto error;
2575 }
2576 if (!oscore_conf->sender) {
2577 coap_log_warn("oscore_conf: sender_id not defined\n");
2578 goto error;
2579 }
2580 return oscore_conf;
2581
2582error_free_value_bin:
2583 coap_delete_bin_const(value.u.value_bin);
2584error:
2585 coap_delete_oscore_conf(oscore_conf);
2586 return NULL;
2587}
2588
2589static oscore_ctx_t *
2590coap_oscore_init(coap_context_t *c_context, coap_oscore_conf_t *oscore_conf) {
2591 oscore_ctx_t *osc_ctx = NULL;
2592
2593 if (!coap_crypto_check_cipher_alg(oscore_conf->aead_alg)) {
2594 coap_log_warn("COSE: Cipher Algorithm %d not supported\n",
2595 oscore_conf->aead_alg);
2596 goto error;
2597 }
2598 if (!coap_crypto_check_hkdf_alg(oscore_conf->hkdf_alg)) {
2599 coap_log_warn("COSE: HKDF Algorithm %d not supported\n",
2600 oscore_conf->hkdf_alg);
2601 goto error;
2602 }
2603
2604 osc_ctx = oscore_derive_ctx(c_context, oscore_conf);
2605 if (!osc_ctx) {
2606 coap_log_crit("OSCORE: Could not create Security Context!\n");
2607 coap_free_type(COAP_STRING, oscore_conf);
2608 return NULL;
2609 }
2610
2611 /* As all is stored in osc_ctx, oscore_conf is no longer needed */
2612 coap_free_type(COAP_STRING, oscore_conf);
2613
2614 /* return default first context */
2615 return osc_ctx;
2616
2617error:
2618 /* Remove from linked chain */
2619 oscore_remove_context(c_context, osc_ctx);
2620
2621 coap_delete_oscore_conf(oscore_conf);
2622 return NULL;
2623}
2624
2625void
2627 oscore_free_contexts(c_context);
2628}
2629
2630void
2633}
2634
2635void
2637 oscore_recipient_ctx_t *recipient_ctx) {
2638 oscore_recipient_ctx_t *old_ctx;
2639
2640 if (session == NULL || recipient_ctx == NULL)
2641 return;
2642
2643 if (session->recipient_ctx == recipient_ctx) {
2644 /* already attached */
2645 return;
2646 }
2647
2648 old_ctx = session->recipient_ctx;
2649
2650 /* attach recipient context */
2651 session->recipient_ctx = recipient_ctx;
2652 session->recipient_ctx->ref++;
2653
2654 if (old_ctx != NULL) {
2656 }
2657}
2658
2659void
2661 oscore_recipient_ctx_t *recipient_ctx) {
2662 if (association == NULL)
2663 return;
2664
2665 if (association->recipient_ctx == recipient_ctx)
2666 return;
2667
2668 if (association->recipient_ctx != NULL) {
2669 association->recipient_ctx->ref--;
2670 }
2671
2672 association->recipient_ctx = recipient_ctx;
2673 association->recipient_ctx->ref++;
2674}
2675
2678 coap_oscore_save_seq_num_t save_seq_num_func,
2679 void *save_seq_num_func_param,
2680 uint64_t start_seq_num) {
2681 coap_oscore_conf_t *oscore_conf = coap_parse_oscore_conf_mem(conf_mem);
2682
2683 if (oscore_conf == NULL)
2684 return NULL;
2685
2686 oscore_conf->save_seq_num_func = save_seq_num_func;
2687 oscore_conf->save_seq_num_func_param = save_seq_num_func_param;
2688 oscore_conf->start_seq_num = start_seq_num;
2689 coap_log_oscore("Start Seq no %" PRIu64 "\n", start_seq_num);
2690 return oscore_conf;
2691}
2692
2693/*
2694 * Compute the size of the potential OSCORE overhead
2695 */
2696size_t
2698 size_t overhead = 0;
2699 oscore_recipient_ctx_t *rcp_ctx = session->recipient_ctx;
2700 oscore_ctx_t *osc_ctx = rcp_ctx ? rcp_ctx->osc_ctx : NULL;
2701 coap_opt_iterator_t opt_iter;
2702 coap_opt_t *option;
2703
2704 if (osc_ctx == NULL)
2705 return 0;
2706
2707 /* Protected code held in inner PDU as token */
2708 overhead += 1;
2709
2710 /* Observe option (creates inner and outer */
2711 option = coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter);
2712 if (option) {
2713 /* Assume delta is small */
2714 overhead += 2 + coap_opt_length(option);
2715 }
2716
2717 /* Proxy URI option Split - covered by coap_rebuild_pdu_for_proxy () */
2718
2719 /* OSCORE option */
2720 /* Option header */
2721 overhead += 1 +
2722 /* Partial IV (64 bits max)*/
2723 8 +
2724 /* kid context */
2725 (osc_ctx->id_context ? osc_ctx->id_context->length : 0) +
2726 /* kid */
2727 osc_ctx->sender_context->sender_id->length;
2728
2729 /* AAD overhead */
2730 overhead += AES_CCM_TAG;
2731
2732 /* Extended Token Overhead */
2733 if (session->max_token_size)
2734 overhead += session->max_token_size - pdu->actual_token.length;
2735
2736 /* End of options marker */
2737 overhead += 1;
2738
2739 return overhead;
2740}
2741
2742COAP_API int
2744 coap_bin_const_t *recipient_id) {
2745 int ret;
2746
2747 coap_lock_lock(return 0);
2748 ret = coap_new_oscore_recipient_lkd(context, recipient_id);
2750 return ret;
2751}
2752
2753int
2755 coap_bin_const_t *recipient_id) {
2756 coap_oscore_rcp_conf_t *rcp_conf;
2757 oscore_recipient_ctx_t *rcp_ctx;
2758
2760
2761 if (context->p_osc_ctx == NULL) {
2762 coap_delete_bin_const(recipient_id);
2763 return 0;
2764 }
2766 if (rcp_conf == NULL) {
2767 coap_delete_bin_const(recipient_id);
2768 return 0;
2769 }
2770 memset(rcp_conf, 0, sizeof(coap_oscore_rcp_conf_t));
2771 rcp_conf->recipient_id = recipient_id;
2772 /* rcp_conf is released in oscore_add_recipient() */
2773 rcp_ctx = oscore_add_recipient(context->p_osc_ctx, rcp_conf, 0);
2774 if (rcp_ctx == NULL)
2775 return 0;
2776 return 1;
2777}
2778
2779COAP_API int
2781 coap_bin_const_t *recipient_id) {
2782 int ret;
2783
2784 if (!context || !recipient_id)
2785 return 0;
2786 coap_lock_lock(return 0);
2787 ret = coap_delete_oscore_recipient_lkd(context, recipient_id);
2789 return ret;
2790}
2791
2792int
2794 coap_bin_const_t *recipient_id) {
2796 if (context->p_osc_ctx == NULL)
2797 return 0;
2798 return oscore_delete_recipient(context->p_osc_ctx, recipient_id);
2799}
2800
2801void
2803 coap_oscore_find_handler_t find_handler,
2804 coap_oscore_update_seq_num_handler_t update_seq_num_handler) {
2805 if (context == NULL)
2806 return;
2807
2808 coap_lock_lock(return);
2809 context->oscore_find_cb = find_handler;
2810 context->oscore_update_seq_num_cb = update_seq_num_handler;
2812}
2813
2815coap_oscore_get_recipient_conf(coap_oscore_conf_t *oscore_conf,
2816 const coap_bin_const_t *rid) {
2817 coap_oscore_rcp_conf_t *next = oscore_conf->recipient_chain;
2818
2819 while (next) {
2820 if (coap_binary_equal(next->recipient_id, rid)) {
2821 return next;
2822 }
2823 next = next->next_recipient;
2824 }
2825 return NULL;
2826}
2827
2828
2829COAP_API int
2831 const coap_bin_const_t *recipient_id,
2832 uint64_t last_seq, uint64_t seq_window) {
2833 int ret;
2834
2835 coap_lock_lock(return 0);
2836 ret = coap_oscore_recipient_set_latest_seq_lkd(oscore_conf, recipient_id, last_seq,
2837 seq_window);
2839 return ret;
2840}
2841
2842int
2844 const coap_bin_const_t *recipient_id,
2845 uint64_t last_seq, uint64_t seq_window) {
2846 coap_oscore_rcp_conf_t *rcp_conf;
2847
2848 rcp_conf = coap_oscore_get_recipient_conf(oscore_conf, recipient_id);
2849 if (rcp_conf) {
2850 rcp_conf->last_seq = last_seq;
2851 rcp_conf->sliding_window = seq_window;
2852 return 1;
2853 }
2854 return 0;
2855}
2856
2858
2859#else /* !COAP_OSCORE_SUPPORT */
2860int
2862 return 0;
2863}
2864
2867 const coap_address_t *local_if,
2868 const coap_address_t *server,
2869 coap_proto_t proto,
2870 coap_oscore_conf_t *oscore_conf) {
2871 (void)ctx;
2872 (void)local_if;
2873 (void)server;
2874 (void)proto;
2875 (void)oscore_conf;
2876 return NULL;
2877}
2878
2881 const coap_address_t *local_if,
2882 const coap_address_t *server,
2883 coap_proto_t proto,
2884 coap_oscore_conf_t *oscore_conf,
2885 void *app_data,
2887 coap_str_const_t *ws_host) {
2888 (void)ctx;
2889 (void)local_if;
2890 (void)server;
2891 (void)proto;
2892 (void)oscore_conf;
2893 (void)app_data;
2894 (void)callback;
2895 (void)ws_host;
2896 return NULL;
2897}
2898
2901 const coap_address_t *local_if,
2902 const coap_address_t *server,
2903 coap_proto_t proto,
2904 coap_dtls_cpsk_t *psk_data,
2905 coap_oscore_conf_t *oscore_conf) {
2906 (void)ctx;
2907 (void)local_if;
2908 (void)server;
2909 (void)proto;
2910 (void)psk_data;
2911 (void)oscore_conf;
2912 return NULL;
2913}
2914
2917 const coap_address_t *local_if,
2918 const coap_address_t *server,
2919 coap_proto_t proto,
2920 coap_dtls_cpsk_t *psk_data,
2921 coap_oscore_conf_t *oscore_conf,
2922 void *app_data,
2924 coap_str_const_t *ws_host) {
2925 (void)ctx;
2926 (void)local_if;
2927 (void)server;
2928 (void)proto;
2929 (void)psk_data;
2930 (void)oscore_conf;
2931 (void)app_data;
2932 (void)callback;
2933 (void)ws_host;
2934 return NULL;
2935}
2936
2939 const coap_address_t *local_if,
2940 const coap_address_t *server,
2941 coap_proto_t proto,
2942 coap_dtls_pki_t *pki_data,
2943 coap_oscore_conf_t *oscore_conf) {
2944 (void)ctx;
2945 (void)local_if;
2946 (void)server;
2947 (void)proto;
2948 (void)pki_data;
2949 (void)oscore_conf;
2950 return NULL;
2951}
2952
2955 const coap_address_t *local_if,
2956 const coap_address_t *server,
2957 coap_proto_t proto,
2958 coap_dtls_pki_t *pki_data,
2959 coap_oscore_conf_t *oscore_conf,
2960 void *app_data,
2962 coap_str_const_t *ws_host) {
2963 (void)ctx;
2964 (void)local_if;
2965 (void)server;
2966 (void)proto;
2967 (void)pki_data;
2968 (void)oscore_conf;
2969 (void)app_data;
2970 (void)callback;
2971 (void)ws_host;
2972 return NULL;
2973}
2974
2975int
2977 coap_oscore_conf_t *oscore_conf) {
2978 (void)context;
2979 (void)oscore_conf;
2980 return 0;
2981}
2982
2985 coap_oscore_save_seq_num_t save_seq_num_func,
2986 void *save_seq_num_func_param,
2987 uint64_t start_seq_num) {
2988 (void)conf_mem;
2989 (void)save_seq_num_func;
2990 (void)save_seq_num_func_param;
2991 (void)start_seq_num;
2992 return NULL;
2993}
2994
2995int
2997 (void)oscore_conf;
2998 return 0;
2999}
3000
3001int
3003 coap_bin_const_t *recipient_id) {
3004 (void)context;
3005 (void)recipient_id;
3006 return 0;
3007}
3008
3009int
3011 coap_bin_const_t *recipient_id) {
3012 (void)context;
3013 (void)recipient_id;
3014 return 0;
3015}
3016
3017void
3019 coap_oscore_find_handler_t find_handler,
3020 coap_oscore_update_seq_num_handler_t update_seq_num_handler) {
3021 (void)context;
3022 (void)find_handler;
3023 (void)update_seq_num_handler;
3024}
3025
3026COAP_API int
3028 const coap_bin_const_t *recipient_id,
3029 uint64_t last_seq, uint64_t seq_window) {
3030 (void)oscore_conf;
3031 (void)recipient_id;
3032 (void)last_seq;
3033 (void)seq_window;
3034 return 0;
3035}
3036
3037#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:5202
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)
size_t oscore_encode_option_value(uint8_t *option_buffer, size_t option_buf_len, cose_encrypt0_t *cose, uint8_t group, uint8_t appendix_b_2)
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...
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.