libcoap 4.3.5-develop-5347a62
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/*
27 * RFC 10021 Section 3.4: the Group OSCORE external_aad carries the sender's
28 * and the Group Manager's authentication credentials (e.g. 118 B + 124 B CCS),
29 * the kid context twice and the OSCORE option, so it does not fit the RFC 8613
30 * sized buffers.
31 */
32#define E_AAD_BUF_LEN 512 /* length of external_aad_buffer */
33#define AAD_BUF_LEN (E_AAD_BUF_LEN + 32) /* length of aad_buffer */
34
35static oscore_ctx_t *coap_oscore_init(coap_context_t *c_context,
36 coap_oscore_conf_t *oscore_conf);
37
38#if COAP_CLIENT_SUPPORT
39
40int
42 if (oscore_conf) {
43 oscore_ctx_t *osc_ctx;
44
45 if (oscore_conf->recipient_chain == NULL) {
46 coap_log_warn("OSCORE: Recipient ID must be defined for a client\n");
47 coap_delete_oscore_conf(oscore_conf);
48 return 0;
49 }
50 if (oscore_conf->rfc8613_b_2) {
51 /* Need to replace id_context with random value */
52 coap_binary_t *id_context = coap_new_binary(8);
53
54 if (id_context == NULL) {
55 coap_delete_oscore_conf(oscore_conf);
56 return 0;
57 }
59 coap_prng_lkd(id_context->s, id_context->length);
60 oscore_conf->id_context = (coap_bin_const_t *)id_context;
61 session->b_2_step = COAP_OSCORE_B_2_STEP_1;
62 coap_log_oscore("Appendix B.2 client step 1 (Generated ID1)\n");
63 }
64
65 osc_ctx = coap_oscore_init(session->context, oscore_conf);
66 if (osc_ctx == NULL) {
67 return 0;
68 }
70 session->oscore_encryption = 1;
71 }
72 return 1;
73}
74
77 const coap_address_t *local_if,
78 const coap_address_t *server,
79 coap_proto_t proto,
80 coap_oscore_conf_t *oscore_conf) {
81 coap_session_t *session;
82
83 coap_lock_lock(return NULL);
84 session = coap_new_client_session_oscore3_lkd(ctx, local_if, server, proto, oscore_conf, NULL,
85 NULL, NULL);
87 return session;
88}
89
92 const coap_address_t *local_if,
93 const coap_address_t *server,
94 coap_proto_t proto,
95 coap_oscore_conf_t *oscore_conf,
96 void *app_data,
98 coap_str_const_t *ws_host) {
99 coap_session_t *session;
100
101 coap_lock_lock(return NULL);
102 session = coap_new_client_session_oscore3_lkd(ctx, local_if, server, proto, oscore_conf, app_data,
103 callback, ws_host);
105 return session;
106}
107
110 const coap_address_t *local_if,
111 const coap_address_t *server,
112 coap_proto_t proto,
113 coap_oscore_conf_t *oscore_conf,
114 void *app_data,
116 coap_str_const_t *ws_host) {
117 coap_session_t *session =
118 coap_new_client_session3_lkd(ctx, local_if, server, proto, app_data, callback, ws_host);
119
120 if (!session)
121 return NULL;
122
123 if (coap_oscore_initiate(session, oscore_conf) == 0) {
125 return NULL;
126 }
127 return session;
128}
129
132 const coap_address_t *local_if,
133 const coap_address_t *server,
134 coap_proto_t proto,
135 coap_dtls_cpsk_t *psk_data,
136 coap_oscore_conf_t *oscore_conf) {
137 coap_session_t *session;
138
139 coap_lock_lock(return NULL);
140 session = coap_new_client_session_oscore_psk3_lkd(ctx, local_if, server, proto, psk_data,
141 oscore_conf, NULL, NULL, NULL);
143 return session;
144}
145
148 const coap_address_t *local_if,
149 const coap_address_t *server,
150 coap_proto_t proto,
151 coap_dtls_cpsk_t *psk_data,
152 coap_oscore_conf_t *oscore_conf,
153 void *app_data,
155 coap_str_const_t *ws_host) {
156 coap_session_t *session;
157
158 coap_lock_lock(return NULL);
159 session = coap_new_client_session_oscore_psk3_lkd(ctx, local_if, server, proto, psk_data,
160 oscore_conf, app_data, callback, ws_host);
162 return session;
163}
164
167 const coap_address_t *local_if,
168 const coap_address_t *server,
169 coap_proto_t proto,
170 coap_dtls_cpsk_t *psk_data,
171 coap_oscore_conf_t *oscore_conf,
172 void *app_data,
174 coap_str_const_t *ws_host) {
175 coap_session_t *session;
176
178 session = coap_new_client_session_psk3_lkd(ctx, local_if, server, proto, psk_data,
179 app_data, callback, ws_host);
180
181 if (!session)
182 return NULL;
183
184 if (coap_oscore_initiate(session, oscore_conf) == 0) {
186 return NULL;
187 }
188 return session;
189}
190
193 const coap_address_t *local_if,
194 const coap_address_t *server,
195 coap_proto_t proto,
196 coap_dtls_pki_t *pki_data,
197 coap_oscore_conf_t *oscore_conf) {
198 coap_session_t *session;
199
200 coap_lock_lock(return NULL);
201 session = coap_new_client_session_oscore_pki3_lkd(ctx, local_if, server, proto, pki_data,
202 oscore_conf, NULL, NULL, NULL);
204 return session;
205}
206
209 const coap_address_t *local_if,
210 const coap_address_t *server,
211 coap_proto_t proto,
212 coap_dtls_pki_t *pki_data,
213 coap_oscore_conf_t *oscore_conf,
214 void *app_data,
216 coap_str_const_t *ws_host) {
217 coap_session_t *session;
218
219 coap_lock_lock(return NULL);
220 session = coap_new_client_session_oscore_pki3_lkd(ctx, local_if, server, proto, pki_data,
221 oscore_conf, app_data, callback, ws_host);
223 return session;
224}
225
228 const coap_address_t *local_if,
229 const coap_address_t *server,
230 coap_proto_t proto,
231 coap_dtls_pki_t *pki_data,
232 coap_oscore_conf_t *oscore_conf,
233 void *app_data,
235 coap_str_const_t *ws_host) {
236 coap_session_t *session;
237
239 session = coap_new_client_session_pki3_lkd(ctx, local_if, server, proto, pki_data,
240 app_data, callback, ws_host);
241
242 if (!session)
243 return NULL;
244
245 if (coap_oscore_initiate(session, oscore_conf) == 0) {
247 return NULL;
248 }
249 return session;
250}
251#endif /* COAP_CLIENT_SUPPORT */
252#if COAP_SERVER_SUPPORT
253
254COAP_API int
256 coap_oscore_conf_t *oscore_conf) {
257 int ret;
258
259 coap_lock_lock(return 0);
260 ret = coap_context_oscore_server_lkd(context, oscore_conf);
262 return ret;
263}
264
265int
267 coap_oscore_conf_t *oscore_conf) {
268 oscore_ctx_t *osc_ctx;
269
271 osc_ctx = coap_oscore_init(context, oscore_conf);
272 /* osc_ctx already added to context->osc_ctx */
273 if (osc_ctx)
274 return 1;
275 return 0;
276}
277
278#endif /* COAP_SERVER_SUPPORT */
279
280int
282 coap_uri_t uri;
283 coap_opt_iterator_t opt_iter;
284 coap_opt_t *option;
285 uint8_t option_value_buffer[15];
286 coap_optlist_t *optlist_chain = NULL;
287
288 if ((option =
289 coap_check_option(pdu, COAP_OPTION_PROXY_URI, &opt_iter)) == NULL)
290 return 1;
291
292 /* Need to break down into the component parts, but keep data safe */
293 memset(&uri, 0, sizeof(uri));
294
296 coap_opt_length(option),
297 &uri) < 0 || uri.scheme >= COAP_URI_SCHEME_LAST) {
298 coap_log_warn("Proxy URI '%.*s' not decodable\n",
299 coap_opt_length(option),
300 (const char *)coap_opt_value(option));
301 goto error;
302 }
304 goto error;
305
306 if (!coap_insert_option(pdu,
308 uri.host.length,
309 uri.host.s))
310 goto error;
314 coap_encode_var_safe(option_value_buffer,
315 sizeof(option_value_buffer),
316 uri.port & 0xffff),
317 option_value_buffer))
318 goto error;
319 if (uri.path.length) {
320 /* Add in the Uri-Path options */
322 &optlist_chain))
323 goto error;
324 }
325 if (uri.query.length) {
326 /* Add in the Uri-Query options */
328 &optlist_chain))
329 goto error;
330 }
331 if (!coap_add_optlist_pdu(pdu, &optlist_chain))
332 goto error;
333
334 if (!coap_insert_option(pdu,
336 strlen(coap_uri_scheme[uri.scheme].name),
337 (const uint8_t *)coap_uri_scheme[uri.scheme].name))
338 goto error;
339
340 coap_delete_optlist(optlist_chain);
341 return 1;
342
343error:
344 coap_delete_optlist(optlist_chain);
345 return 0;
346}
347
348static void
349dump_cose(cose_encrypt0_t *cose, const char *message) {
350#if COAP_MAX_LOGGING_LEVEL < _COAP_LOG_OSCORE
351 (void)cose;
352 (void)message;
353#else /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_OSCORE */
355 char buffer[30];
356
357 coap_log_oscore("%s COSE information\n", message);
359 cose_get_alg_name(cose->alg, buffer, sizeof(buffer)));
361 oscore_log_hex_value(COAP_LOG_OSCORE, "partial_iv", &cose->partial_iv);
363 oscore_log_hex_value(COAP_LOG_OSCORE, "kid_context", &cose->kid_context);
365 "oscore_option",
366 &cose->oscore_option);
368 oscore_log_hex_value(COAP_LOG_OSCORE, "external_aad", &cose->external_aad);
370#if COAP_OSCORE_GROUP_SUPPORT
371 oscore_log_int_value(COAP_LOG_OSCORE, "group_flag", cose->group_flag);
372#endif /* COAP_OSCORE_GROUP_SUPPORT */
373 }
374#endif /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_OSCORE */
375}
376
379 coap_pdu_t *pdu,
380 coap_bin_const_t *kid_context,
381 oscore_partial_iv_t send_partial_iv) {
382 coap_pdu_t *ret_pdu;
383
384 coap_lock_lock(return NULL);
385 ret_pdu = coap_oscore_new_pdu_encrypted_lkd(session, pdu, kid_context, send_partial_iv);
387
388 return ret_pdu;
389}
390
391/*
392 * Take current PDU, create a new one appropriately separated as per RFC8613
393 * and then encrypt / integrity check the OSCORE data
394 */
397 coap_pdu_t *pdu,
398 coap_bin_const_t *kid_context,
399 oscore_partial_iv_t send_partial_iv) {
400 uint8_t coap_request = COAP_PDU_IS_REQUEST(pdu) || COAP_PDU_IS_PING(pdu);
401 coap_pdu_code_t code =
402 coap_request ? COAP_REQUEST_CODE_POST : COAP_RESPONSE_CODE(204);
403 coap_pdu_t *osc_pdu;
404 coap_pdu_t *plain_pdu = NULL;
405 coap_bin_const_t pdu_token;
406 coap_opt_iterator_t opt_iter;
407 coap_opt_t *option;
408 uint8_t pdu_code = pdu->code;
409 size_t length;
410 const uint8_t *data;
411 uint8_t *ciphertext_buffer = NULL;
412 size_t ciphertext_len = 0;
413 uint8_t aad_buffer[AAD_BUF_LEN];
414 uint8_t nonce_buffer[13];
416 coap_bin_const_t nonce;
417 oscore_recipient_ctx_t *rcp_ctx;
418 oscore_ctx_t *osc_ctx;
419 cose_encrypt0_t cose[1];
420#if COAP_OSCORE_GROUP_SUPPORT
421 cose_sign1_t sign[1];
422#endif /* COAP_OSCORE_GROUP_SUPPORT */
423 uint8_t group_flag = 0;
424 int show_pdu = 0;
425 int doing_observe = 0;
426 uint32_t observe_value = 0;
427 oscore_association_t *association = NULL;
428 oscore_sender_ctx_t *snd_ctx;
429 uint8_t external_aad_buffer[E_AAD_BUF_LEN];
430 coap_bin_const_t external_aad;
431 uint8_t oscore_option[255];
432 ssize_t oscore_option_len;
433
434 /* Check that OSCORE has not already been done */
435 if (coap_check_option(pdu, COAP_OPTION_OSCORE, &opt_iter))
436 return NULL;
437
438 if (coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter))
439 doing_observe = 1;
440
441 coap_log_debug("PDU to encrypt\n");
443 osc_pdu = coap_pdu_init(pdu->type == COAP_MESSAGE_NON &&
444 session->b_2_step != COAP_OSCORE_B_2_NONE ?
445 COAP_MESSAGE_CON : pdu->type,
446 code,
447 pdu->mid,
448 pdu->used_size + coap_oscore_overhead(session, pdu));
449 if (osc_pdu == NULL)
450 return NULL;
451
452 cose_encrypt0_init(cose); /* clears cose memory */
453 pdu_token = coap_pdu_get_token(pdu);
454 if (coap_request) {
455 /*
456 * RFC8613 8.1 Step 1. Protecting the client's request
457 * Get the Sender Context
458 */
459 rcp_ctx = session->recipient_ctx;
460 if (rcp_ctx == NULL)
461 goto error;
462 osc_ctx = rcp_ctx->osc_ctx;
463 assert(osc_ctx);
464 snd_ctx = osc_ctx->sender_context;
465 } else {
466 /*
467 * RFC8613 8.3 Step 1. Protecting the server's response
468 * Get the Sender Context
469 */
470 association = oscore_find_association(session, &pdu_token);
471 if (association == NULL)
472 goto error;
473
474 rcp_ctx = association->recipient_ctx;
475 osc_ctx = rcp_ctx->osc_ctx;
476 snd_ctx = osc_ctx->sender_context;
477 cose_encrypt0_set_partial_iv(cose, association->partial_iv);
478 cose_encrypt0_set_aad(cose, association->aad);
479 }
480
481#if COAP_OSCORE_GROUP_SUPPORT
482 cose_sign1_init(sign); /* clear sign memory */
483 if (rcp_ctx->mode == OSCORE_MODE_GROUP)
484 group_flag = 1;
485
486 if (rcp_ctx->mode == OSCORE_MODE_GROUP && coap_request)
487 cose_encrypt0_set_alg(cose, osc_ctx->alg_group_enc);
488 else
489#endif /* COAP_OSCORE_GROUP_SUPPORT */
490 cose_encrypt0_set_alg(cose, osc_ctx->aead_alg);
491
492 if (coap_request || doing_observe ||
493 send_partial_iv == OSCORE_SEND_PARTIAL_IV) {
494 uint8_t partial_iv_buffer[8];
495 size_t partial_iv_len;
496 coap_bin_const_t partial_iv;
497 partial_iv_len = coap_encode_var_safe8(partial_iv_buffer,
498 sizeof(partial_iv_buffer),
499 snd_ctx->seq);
500 if (snd_ctx->seq == 0) {
501 /* Need to special case */
502 partial_iv_buffer[0] = '\000';
503 partial_iv_len = 1;
504 }
505 partial_iv.s = partial_iv_buffer;
506 partial_iv.length = partial_iv_len;
507 cose_encrypt0_set_partial_iv(cose, &partial_iv);
508 }
509
510 if (coap_request)
512
513 cose_encrypt0_set_key_id(cose, snd_ctx->sender_id);
514
515 /* nonce (needs to have sender information correctly set up) */
516
517 if (coap_request || doing_observe ||
518 send_partial_iv == OSCORE_SEND_PARTIAL_IV) {
519 /*
520 * 8.1 Step 3 or RFC8613 8.3.1 Step A
521 * Compose the AEAD nonce
522 *
523 * Requires in COSE object as appropriate
524 * key_id (kid) (sender)
525 * partial_iv (sender)
526 * common_iv (already in osc_ctx)
527 */
528 nonce.s = nonce_buffer;
529 nonce.length = 13;
530 oscore_generate_nonce(cose, osc_ctx, nonce_buffer, 13);
531 cose_encrypt0_set_nonce(cose, &nonce);
532 if (!oscore_increment_sender_seq(osc_ctx))
533 goto error;
534 if (osc_ctx->save_seq_num_func) {
535 if (osc_ctx->sender_context->seq > osc_ctx->sender_context->next_seq) {
536 /* Only update at ssn_freq rate */
537 osc_ctx->sender_context->next_seq += osc_ctx->ssn_freq;
538 osc_ctx->save_seq_num_func(osc_ctx->sender_context->next_seq,
539 osc_ctx->save_seq_num_func_param);
540 }
541 }
542 } else {
543 /*
544 * 8.3 Step 3.
545 * Use nonce from request
546 */
547 cose_encrypt0_set_nonce(cose, association->nonce);
548 }
549
550 /* OSCORE_option (needs to be before AAD as included in AAD if group) */
551
552 /* cose is modified for encode option in response message */
553 if (!coap_request) {
554 /* no kid on response */
556 if (!doing_observe && send_partial_iv == OSCORE_SEND_NO_IV)
558 }
559 if (kid_context) {
560 cose_encrypt0_set_kid_context(cose, kid_context);
561 }
562 oscore_option_len =
563 oscore_encode_option_value(oscore_option, sizeof(oscore_option), cose,
564 group_flag,
565 session->b_2_step != COAP_OSCORE_B_2_NONE);
566 if (oscore_option_len < 0) {
567 coap_log_oscore("Invalid parameters for creating OSCORE option\n");
568 goto error;
569 }
570 if (!coap_request) {
571 /* Reset what was just unset as appropriate for AAD */
573 cose_encrypt0_set_partial_iv(cose, association->partial_iv);
574 }
575 if (kid_context)
577
578 /*
579 * RFC8613 8.1/8.3 Step 2(a) (5.4).
580 * Compose the External AAD and then AAD
581 *
582 * OSCORE_option requires
583 * partial_iv (cose partial_iv)
584 * kid_context (cose kid_context)
585 * key_id (cose key_id)
586 * group_flag
587 *
588 * Non Group requires the following
589 * RFC8613 5.4
590 * oscore_version 1
591 * algorithms [
592 * aead_alg (osc_ctx)
593 * ]
594 * request_kid (request key_id using cose)
595 * request_piv (request partial_iv using cose)
596 * options (none at present)
597 * Group requires the following
598 * RFCxxxx 3.4
599 * oscore_version 1
600 * algorithms [
601 * aead_alg (osc_ctx) (pairwise mode)
602 * alg_group_enc (osc_ctx) (group mode)
603 * alg_signature (osc_ctx) (group mode)
604 * alg_pairwise_key_agreement (osc_ctx) (pairwise mode)
605 * ]
606 * request_kid (request key_id using cose)
607 * request_piv (request partial_iv using cose)
608 * options (none at present)
609 * request_kid_context (osc_ctx id_context)
610 * OSCORE_option (parameter)
611 * sender_cred (osc_ctx sender_context credentials)
612 * gm_cred (osc_ctx gm_cred)
613 *
614 * Note: No I options at present
615 */
616 if (coap_request || rcp_ctx->mode != OSCORE_MODE_SINGLE ||
617 send_partial_iv == OSCORE_SEND_PARTIAL_IV) {
618 /* External AAD */
619 external_aad.s = external_aad_buffer;
620 external_aad.length = oscore_prepare_e_aad(osc_ctx,
621 cose,
622#if COAP_OSCORE_GROUP_SUPPORT
623 oscore_option,
624 oscore_option_len,
625 osc_ctx->sender_context->sender_cred,
626 rcp_ctx->mode,
627#else /* COAP_OSCORE_GROUP_SUPPORT */
628 NULL,
629 0,
630 NULL,
631#endif /* COAP_OSCORE_GROUP_SUPPORT */
632 external_aad_buffer,
633 sizeof(external_aad_buffer));
634 if (external_aad.length == 0)
635 goto error;
636 cose_encrypt0_set_external_aad(cose, &external_aad);
637
638 /* AAD */
639 aad.s = aad_buffer;
640 aad.length = oscore_prepare_aad(external_aad_buffer,
641 external_aad.length,
642 aad_buffer,
643 sizeof(aad_buffer));
644 if (aad.length == 0)
645 goto error;
646 assert(aad.length < AAD_BUF_LEN);
647 cose_encrypt0_set_aad(cose, &aad);
648 }
649
650 /*
651 * RFC8613 8.1/8.3 Step 2(b) (5.3).
652 *
653 * Set up temp plaintext pdu, the data including token, options and
654 * optional payload will get encrypted as COSE ciphertext.
655 */
656 plain_pdu = coap_pdu_init(pdu->type,
657 pdu->code,
658 pdu->mid,
659 pdu->used_size + 1 /* pseudo-token with actual code */);
660 if (plain_pdu == NULL)
661 goto error;
662
663 coap_add_token(osc_pdu, pdu_token.length, pdu_token.s);
664
665 /* First byte of plain is real CoAP code. Pretend it is token */
666 coap_add_token(plain_pdu, 1, &pdu_code);
667
668 /* Copy across the Outer/Inner Options to respective PDUs */
670 while ((option = coap_option_next(&opt_iter))) {
671 switch ((coap_code_opt_num_t)opt_iter.number) {
677 /* Outer only */
678 if (!coap_insert_option(osc_pdu,
679 opt_iter.number,
680 coap_opt_length(option),
681 coap_opt_value(option)))
682 goto error;
683 break;
685 /* Make as Outer option as-is */
686 if (!coap_insert_option(osc_pdu,
687 opt_iter.number,
688 coap_opt_length(option),
689 coap_opt_value(option)))
690 goto error;
691 if (coap_request) {
692 /* Make as Inner option (unchanged) */
693 if (!coap_insert_option(plain_pdu,
694 opt_iter.number,
695 coap_opt_length(option),
696 coap_opt_value(option)))
697 goto error;
698 osc_pdu->code = COAP_REQUEST_CODE_FETCH;
699 } else {
700 /* Make as Inner option but empty */
701 if (!coap_insert_option(plain_pdu, opt_iter.number, 0, NULL))
702 goto error;
703 osc_pdu->code = COAP_RESPONSE_CODE(205);
704 }
705 show_pdu = 1;
706 doing_observe = 1;
707 observe_value = coap_decode_var_bytes(coap_opt_value(option),
708 coap_opt_length(option));
709 break;
711 /*
712 * Should have already been caught by doing
713 * coap_rebuild_pdu_for_proxy() before calling
714 * coap_oscore_new_pdu_encrypted_lkd()
715 */
716 assert(0);
717 break;
719 /* Dealt with separately */
720 break;
722 case COAP_OPTION_ETAG:
739 case COAP_OPTION_ECHO:
740 case COAP_OPTION_RTAG:
741 default:
742 /* Make as Inner option */
743 if (!coap_insert_option(plain_pdu,
744 opt_iter.number,
745 coap_opt_length(option),
746 coap_opt_value(option)))
747 goto error;
748 break;
749 }
750 }
751 /* Add in data to plain */
752 if (coap_get_data(pdu, &length, &data)) {
753 if (!coap_add_data(plain_pdu, length, data))
754 goto error;
755 }
756 if (show_pdu) {
757 coap_log_oscore("OSCORE payload\n");
758 coap_show_pdu(COAP_LOG_OSCORE, plain_pdu);
759 }
760
761 /*
762 * 8.1/8.3 Step 4.
763 * Encrypt the COSE object.
764 *
765 * Requires in COSE object as appropriate
766 * alg (already set)
767 * key (sender key)
768 * nonce (already set)
769 * aad (already set)
770 * plaintext
771 */
772 cose_encrypt0_set_key(cose, snd_ctx->sender_key);
773 cose_encrypt0_set_plaintext(cose, plain_pdu->token, plain_pdu->used_size);
774 dump_cose(cose, "Pre encrypt");
775 ciphertext_buffer =
776 coap_malloc_type(COAP_OSCORE_BUF, OSCORE_CRYPTO_BUFFER_SIZE);
777 if (ciphertext_buffer == NULL)
778 goto error;
779 ciphertext_len = cose_encrypt0_encrypt(cose,
780 ciphertext_buffer,
781 plain_pdu->used_size + AES_CCM_TAG);
782 if ((int)ciphertext_len <= 0) {
783 coap_log_warn("OSCORE: Encryption Failure, result code: %d \n",
784 (int)ciphertext_len);
785 goto error;
786 }
787 assert(ciphertext_len < OSCORE_CRYPTO_BUFFER_SIZE);
788
789#if COAP_OSCORE_GROUP_SUPPORT
790 if (rcp_ctx->mode == OSCORE_MODE_GROUP /* && coap_request */) {
791 /* sign request message */
792 size_t sign_size = snd_ctx->sender_cred->wire_sign_size;
793 uint8_t *sig_buffer = NULL;
794 size_t sig_len = external_aad.length + ciphertext_len + 30;
795 uint8_t *keystream = coap_malloc_type(COAP_STRING, sign_size);
796 uint8_t *buffer = ciphertext_buffer + ciphertext_len;
797
798 assert(ciphertext_len + sign_size < OSCORE_CRYPTO_BUFFER_SIZE);
799
800 if (keystream == NULL)
801 goto error;
802 sig_buffer = coap_malloc_type(COAP_STRING, sig_len);
803 if (sig_buffer == NULL)
804 goto error;
805 oscore_populate_sign(sign,
806 osc_ctx,
807 snd_ctx->sender_cred,
808 snd_ctx->my_private_key);
809 sig_len = oscore_prepare_sig_structure(sig_buffer,
810 sig_len,
811 external_aad_buffer,
812 external_aad.length,
813 ciphertext_buffer,
814 ciphertext_len);
815 cose_sign1_set_signature(sign,
816 ciphertext_buffer + ciphertext_len,
817 sign_size);
818 cose_sign1_set_ciphertext(sign, sig_buffer, sig_len);
819 sign_size = cose_sign1_sign(sign);
820 coap_free_type(COAP_STRING, sig_buffer);
821 if (sign_size == 0) {
822 coap_log_warn("OSCORE: Signature Failure \n");
823 coap_free_type(COAP_STRING, keystream);
824 goto error;
825 }
826 /* signature at end of encrypted text */
827 ciphertext_len += sign_size;
828
829 /* ENC_SIGNATURE = SIGNATURE XOR KEYSTREAM */
831 cose,
832 coap_request,
833 snd_ctx->sender_id,
834 osc_ctx->id_context,
835 sign_size,
836 keystream,
837 sign_size);
838 for (size_t i = 0; i < sign_size; i++) {
839 buffer[i] = buffer[i] ^ keystream[i];
840 }
841 coap_free_type(COAP_STRING, keystream);
842 }
843#endif /* COAP_OSCORE_GROUP_SUPPORT */
844
845 /* Add in OSCORE option (previously computed) */
846 if (!coap_insert_option(osc_pdu,
848 oscore_option_len,
849 oscore_option))
850 goto error;
851
852 /* Add now encrypted payload */
853 if (!coap_add_data(osc_pdu, ciphertext_len, ciphertext_buffer))
854 goto error;
855
856 coap_free_type(COAP_OSCORE_BUF, ciphertext_buffer);
857 ciphertext_buffer = NULL;
858
859 coap_delete_pdu_lkd(plain_pdu);
860 plain_pdu = NULL;
861
862 if (association && (association->is_observe == 0 || (COAP_RESPONSE_CLASS(pdu->code) != 2 &&
863 pdu->code != COAP_RESPONSE_CODE(401))))
864 oscore_delete_association(session, association);
865 association = NULL;
866
867 if (!coap_pdu_encode_header(osc_pdu, session->proto)) {
868 goto error;
869 }
870
871 /*
872 * Set up an association for handling a response if this is a request
873 */
874 if (coap_request) {
875 association = oscore_find_association(session, &pdu_token);
876 if (association) {
877 /* Refresh the association */
878 coap_delete_bin_const(association->nonce);
879 association->nonce =
880 coap_new_bin_const(cose->nonce.s, cose->nonce.length);
881 if (association->nonce == NULL)
882 goto error;
883 coap_delete_bin_const(association->aad);
884 association->aad = coap_new_bin_const(cose->aad.s, cose->aad.length);
885 if (association->aad == NULL)
886 goto error;
887 if (doing_observe && observe_value == 1) {
889 association->obs_partial_iv = association->partial_iv;
890 } else {
891 coap_delete_bin_const(association->partial_iv);
892 }
893 association->partial_iv =
895 if (association->partial_iv == NULL)
896 goto error;
897 coap_oscore_association_set_recipient_ctx(association, rcp_ctx);
898 coap_delete_pdu_lkd(association->sent_pdu);
899 if (session->b_2_step != COAP_OSCORE_B_2_NONE || association->just_set_up) {
900 size_t size;
901
902 association->sent_pdu = coap_pdu_duplicate_lkd(pdu, session,
903 pdu_token.length,
904 pdu_token.s, NULL,
906 if (association->sent_pdu == NULL)
907 goto error;
908 if (coap_get_data(pdu, &size, &data)) {
909 coap_add_data(association->sent_pdu, size, data);
910 }
911 association->just_set_up = 0;
912 } else {
913 association->sent_pdu = NULL;
914 }
915 } else if (!oscore_new_association(session,
916 pdu,
917 &pdu_token,
918 rcp_ctx,
919 &cose->aad,
920 &cose->nonce,
921 &cose->partial_iv,
922 doing_observe)) {
923 goto error;
924 }
925 }
926 return osc_pdu;
927
928error:
929 if (association && (association->is_observe == 0 || (COAP_RESPONSE_CLASS(pdu->code) != 2 &&
930 pdu->code != COAP_RESPONSE_CODE(401))))
931 oscore_delete_association(session, association);
932 if (ciphertext_buffer)
933 coap_free_type(COAP_OSCORE_BUF, ciphertext_buffer);
934 coap_delete_pdu_lkd(osc_pdu);
935 coap_delete_pdu_lkd(plain_pdu);
936 return NULL;
937}
938
939static void
940build_and_send_error_pdu(coap_session_t *session,
941 coap_pdu_t *rcvd,
942 coap_pdu_code_t code,
943 const char *diagnostic,
944 uint8_t *echo_data,
945 coap_bin_const_t *kid_context,
946 int encrypt_oscore) {
947 coap_pdu_t *err_pdu = NULL;
948 coap_bin_const_t token;
949 int oscore_encryption = session->oscore_encryption;
950 unsigned char buf[4];
951
952 token = coap_pdu_get_token(rcvd);
955 code,
956 rcvd->mid,
957 token.length + 2 + 8 +
958 (diagnostic ? strlen(diagnostic) : 0));
959 if (!err_pdu)
960 return;
961 coap_add_token(err_pdu, token.length, token.s);
962 if (echo_data) {
963 coap_add_option_internal(err_pdu, COAP_OPTION_ECHO, 8, echo_data);
964 } else if (kid_context == NULL) {
967 coap_encode_var_safe(buf, sizeof(buf), 0),
968 buf);
969 }
970 if (diagnostic)
971 coap_add_data(err_pdu, strlen(diagnostic), (const uint8_t *)diagnostic);
972 session->oscore_encryption = encrypt_oscore;
973
974 if ((echo_data || kid_context) && encrypt_oscore) {
975 coap_pdu_t *osc_pdu;
976
977 osc_pdu =
978 coap_oscore_new_pdu_encrypted_lkd(session, err_pdu, kid_context,
979 echo_data ? 1 : 0);
980 if (!osc_pdu)
981 goto fail_resp;
982 session->oscore_encryption = 0;
983 coap_send_internal(session, osc_pdu, NULL);
984 coap_delete_pdu_lkd(err_pdu);
985 err_pdu = NULL;
986 } else {
987 coap_send_internal(session, err_pdu, NULL);
988 err_pdu = NULL;
989 }
990fail_resp:
991 session->oscore_encryption = oscore_encryption;
992 coap_delete_pdu_lkd(err_pdu);
993 return;
994}
995
998 oscore_ctx_t *oscore_ctx = oscore_derive_ctx_from_conf(oscore_conf);
999
1000 if (oscore_ctx == NULL) {
1001 goto error;
1002 }
1003
1004 /* As all is stored in osc_ctx, oscore_conf is no longer needed */
1005 coap_free_type(COAP_STRING, oscore_conf);
1006
1007 return oscore_ctx;
1008error:
1009 coap_delete_oscore_conf(oscore_conf);
1010 return NULL;
1011}
1012
1013/*
1014 * Replay state of a Recipient Context before an incoming message advanced it.
1015 * Restored when the message then fails verification (RFC 8613 8.2 Step 6 /
1016 * 8.4 Step 5; Group OSCORE 7.2 / 7.4: "SHALL NOT update the Replay Window").
1017 * Unlike oscore_roll_back_seq(), a saved value of 0 is restored as well and
1018 * nothing is restored unless this message saved it.
1019 */
1020typedef struct {
1021 uint64_t last_seq;
1022 uint64_t sliding_window;
1023 uint8_t initial_state;
1024 uint8_t saved;
1025} oscore_replay_state_t;
1026
1027static void
1028oscore_replay_save(oscore_replay_state_t *st, const oscore_recipient_ctx_t *rcp) {
1029 st->last_seq = rcp->last_seq;
1030 st->sliding_window = rcp->sliding_window;
1031 st->initial_state = rcp->initial_state;
1032 st->saved = 1;
1033}
1034
1035static void
1036oscore_replay_restore(oscore_replay_state_t *st, oscore_recipient_ctx_t *rcp) {
1037 if (!st->saved)
1038 return;
1039 rcp->last_seq = st->last_seq;
1040 rcp->sliding_window = st->sliding_window;
1041 rcp->initial_state = st->initial_state;
1042 st->saved = 0;
1043 coap_log_oscore("OSCORE: replay state restored after verification failure\n");
1044}
1045
1046/* pdu contains incoming message with encrypted COSE ciphertext payload
1047 * function returns decrypted message
1048 * and verifies signature, if present
1049 * returns NULL when decryption,verification fails
1050 */
1051coap_pdu_t *
1053 coap_pdu_t *pdu) {
1054 return coap_oscore_decrypt_pdu_ext(session, pdu, 0);
1055}
1056
1057/*
1058 * As coap_oscore_decrypt_pdu(), with flags.
1059 *
1060 * COAP_OSCORE_DECRYPT_NO_REPLAY (requests only): verify-only processing of
1061 * a request that is not received as a request, e.g. the phantom observation
1062 * request of draft-ietf-core-observe-multicast-notifications, Section 9.3.1:
1063 * - no replay check (RFC 8613 8.2 Step 3 skipped);
1064 * - the Recipient Context replay state (last_seq, sliding window, rollback
1065 * values, initial_state) is neither read nor written;
1066 * - no Appendix B.1.2 / B.2 processing, no association (Token space), no
1067 * change to the session, and nothing is sent (no error response, no ACK).
1068 * Replay protection of every other message is unchanged.
1069 */
1070coap_pdu_t *
1072 coap_pdu_t *pdu,
1073 uint32_t flags) {
1074 coap_pdu_t *decrypt_pdu = NULL;
1075 coap_pdu_t *plain_pdu = NULL;
1076 const uint8_t *osc_value; /* value of OSCORE option */
1077 uint8_t osc_size; /* size of OSCORE OPTION */
1078 coap_opt_iterator_t opt_iter;
1079 coap_opt_t *opt = NULL;
1080#if COAP_OSCORE_GROUP_SUPPORT
1081 uint8_t group_message = 0;
1082 cose_sign1_t sign[1];
1083#endif /* COAP_OSCORE_GROUP_SUPPORT */
1084 cose_encrypt0_t cose[1];
1085 oscore_ctx_t *osc_ctx = NULL;
1086 uint8_t aad_buffer[AAD_BUF_LEN];
1087 uint8_t nonce_buffer[13];
1088 coap_bin_const_t aad;
1089 coap_bin_const_t nonce;
1090 int pltxt_size = 0;
1091 int got_resp_piv = 0;
1092 int doing_resp_observe = 0;
1093 uint8_t coap_request = COAP_PDU_IS_REQUEST(pdu);
1094 int no_replay = (flags & COAP_OSCORE_DECRYPT_NO_REPLAY) != 0;
1095 oscore_replay_state_t replay_saved = { 0, 0, 0, 0 };
1096 coap_bin_const_t pdu_token;
1097 uint8_t *st_encrypt;
1098 size_t encrypt_len;
1099 size_t tag_len;
1100 oscore_recipient_ctx_t *rcp_ctx = NULL;
1101 oscore_association_t *association = NULL;
1102 oscore_association_t *decrypt_request_association = NULL;
1103 uint8_t external_aad_buffer[E_AAD_BUF_LEN];
1104 coap_bin_const_t external_aad;
1105 oscore_sender_ctx_t *snd_ctx = NULL;
1106#if COAP_CLIENT_SUPPORT
1107 coap_pdu_t *sent_pdu = NULL;
1108#endif /* COAP_CLIENT_SUPPORT */
1109
1110 opt = coap_check_option(pdu, COAP_OPTION_OSCORE, &opt_iter);
1111 assert(opt);
1112 if (opt == NULL)
1113 return NULL;
1114 if (no_replay && !coap_request) {
1115 coap_log_warn("OSCORE: NO_REPLAY is only defined for requests\n");
1116 return NULL;
1117 }
1118
1119 if (session->context->p_osc_ctx == NULL && session->context->oscore_find_cb == NULL) {
1120 coap_log_warn("OSCORE: Not enabled\n");
1121 if (!coap_request)
1124 session);
1125 return NULL;
1126 }
1127
1128 if (pdu->data == NULL) {
1129 coap_log_warn("OSCORE: No protected payload\n");
1130 if (!coap_request)
1133 session);
1134 return NULL;
1135 }
1136
1137 osc_size = coap_opt_length(opt);
1138 osc_value = coap_opt_value(opt);
1139
1140 cose_encrypt0_init(cose); /* clear cose memory */
1141#if COAP_OSCORE_GROUP_SUPPORT
1142 cose_sign1_init(sign); /* clear sign memory */
1143#endif /* COAP_OSCORE_GROUP_SUPPORT */
1144
1145 /* PDU code will be filled in after decryption */
1146 decrypt_pdu =
1147 coap_pdu_init(pdu->type, 0, pdu->mid, pdu->used_size);
1148 if (decrypt_pdu == NULL) {
1149 if (!coap_request)
1152 session);
1153 goto error;
1154 }
1155
1156 /* Copy across the Token */
1157 pdu_token = coap_pdu_get_token(pdu);
1158 coap_add_token(decrypt_pdu, pdu_token.length, pdu_token.s);
1159
1160 /*
1161 * 8.2/8.4 Step 1.
1162 * Copy outer options across, except E and OSCORE options
1163 */
1164 coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
1165 while ((opt = coap_option_next(&opt_iter))) {
1166 switch ((coap_code_opt_num_t)opt_iter.number) {
1167 /* 'E' options skipped */
1169 case COAP_OPTION_ETAG:
1176 case COAP_OPTION_MAXAGE:
1178 case COAP_OPTION_ACCEPT:
1181 case COAP_OPTION_BLOCK2:
1182 case COAP_OPTION_BLOCK1:
1183 case COAP_OPTION_SIZE2:
1185 case COAP_OPTION_SIZE1:
1187 case COAP_OPTION_ECHO:
1188 case COAP_OPTION_RTAG:
1189 /* OSCORE does not get copied across */
1190 case COAP_OPTION_OSCORE:
1191 break;
1192 /* 'U' options included */
1196 case COAP_OPTION_EDHOC:
1199 default:
1200 if (!coap_add_option_internal(decrypt_pdu,
1201 opt_iter.number,
1202 coap_opt_length(opt),
1203 coap_opt_value(opt))) {
1204 if (!coap_request)
1207 session);
1208 goto error;
1209 }
1210 break;
1211 }
1212 }
1213
1214 if (coap_request) {
1215 uint64_t incoming_seq;
1216 /*
1217 * 8.2 Step 2
1218 * Decompress COSE object
1219 * Get Recipient Context based on kid and optional kid_context
1220 */
1221 if (oscore_decode_option_value(osc_value, osc_size, cose) == 0) {
1222 coap_log_warn("OSCORE: OSCORE Option cannot be decoded.\n");
1223 if (!no_replay)
1224 build_and_send_error_pdu(session,
1225 pdu,
1226 COAP_RESPONSE_CODE(402),
1227 "Failed to decode COSE",
1228 NULL,
1229 NULL,
1230 0);
1231 goto error_no_ack;
1232 }
1233 osc_ctx = oscore_find_context(session,
1234 cose->key_id,
1235 &cose->kid_context,
1236 NULL,
1237 &rcp_ctx);
1238 if (!osc_ctx) {
1239 if (cose->kid_context.length > 0 && !no_replay) {
1240 const uint8_t *ptr;
1241 size_t length;
1242 /* Appendix B.2 protocol check - Is the recipient key_id known */
1243 osc_ctx = oscore_find_context(session,
1244 cose->key_id,
1245 NULL,
1246 session->oscore_r2 != 0 ? (uint8_t *)&session->oscore_r2 : NULL,
1247 &rcp_ctx);
1248 ptr = cose->kid_context.s;
1249 length = cose->kid_context.length;
1250 if (ptr && osc_ctx && osc_ctx->rfc8613_b_2 &&
1251 rcp_ctx->mode == OSCORE_MODE_SINGLE) {
1252 /* Processing Appendix B.2 protocol */
1253 /* Need to CBOR unwrap kid_context */
1254 coap_bin_const_t kid_context;
1255
1256 kid_context.length = oscore_cbor_get_element_size(&ptr, &length);
1257 if (kid_context.length > length)
1258 goto error;
1259 /* This has to fit into an OSCORE option max 255.
1260 * Initial byte, kid_context size, partial IV size and kid size have to be there
1261 */
1262 if (kid_context.length >= 255 - 1 - 1 - cose->partial_iv.length - cose->key_id.length)
1263 goto error;
1264 kid_context.s = ptr;
1265 cose_encrypt0_set_kid_context(cose, (coap_bin_const_t *)&kid_context);
1266
1267 if (session->oscore_r2 != 0) {
1268 /* B.2 step 4 */
1270 cose->kid_context.length);
1271
1272 if (kc == NULL)
1273 goto error;
1274
1275 session->b_2_step = COAP_OSCORE_B_2_STEP_4;
1276 coap_log_oscore("Appendix B.2 server step 4 (R2 || R3)\n");
1277 oscore_update_ctx(osc_ctx, kc);
1278 } else {
1279 session->b_2_step = COAP_OSCORE_B_2_STEP_2;
1280 coap_log_oscore("Appendix B.2 server step 2 (ID1)\n");
1281 osc_ctx = oscore_duplicate_ctx(session->context,
1282 osc_ctx,
1283 osc_ctx->sender_context->sender_id,
1284 &cose->key_id,
1285 &cose->kid_context);
1286 if (osc_ctx == NULL)
1287 goto error;
1288 /*
1289 * Complete the Verify (B.2 step 2)
1290 * before sending back the response
1291 */
1292 rcp_ctx = osc_ctx->recipient_chain;
1293 }
1294 } else {
1295 osc_ctx = NULL;
1296 }
1297 }
1298 } else if (!no_replay && session->b_2_step != COAP_OSCORE_B_2_NONE) {
1299 session->b_2_step = COAP_OSCORE_B_2_NONE;
1300 coap_log_oscore("Appendix B.2 server finished\n");
1301 }
1302 if (!osc_ctx) {
1303 coap_log_err("OSCORE: Security Context not found\n");
1304 oscore_log_hex_value(COAP_LOG_OSCORE, "key_id", &cose->key_id);
1305 oscore_log_hex_value(COAP_LOG_OSCORE, "kid_context", &cose->kid_context);
1306 if (!no_replay)
1307 build_and_send_error_pdu(session,
1308 pdu,
1309 COAP_RESPONSE_CODE(401),
1310 "Security context not found",
1311 NULL,
1312 NULL,
1313 0);
1314 goto error_no_ack;
1315 }
1316 /* to be used for encryption of returned response later */
1317 if (!no_replay)
1318 coap_oscore_session_set_recipient_ctx(session, rcp_ctx);
1319 snd_ctx = osc_ctx->sender_context;
1320
1321 /*
1322 * 8.2 Step 3.
1323 * Verify Partial IV is not duplicated.
1324 *
1325 * Requires in COSE object as appropriate
1326 * partial_iv (as received)
1327 *
1328 * Skipped with COAP_OSCORE_DECRYPT_NO_REPLAY, together with the
1329 * last_seq update below.
1330 */
1331 if (!no_replay)
1332 oscore_replay_save(&replay_saved, rcp_ctx);
1333 if (no_replay) {
1334 coap_log_oscore("OSCORE: no replay check, replay state unchanged\n");
1335 } else if ((rcp_ctx->initial_state == 0 || osc_ctx->rfc8613_b_1_2 == 0) &&
1336 !oscore_validate_sender_seq(rcp_ctx, cose)) {
1337 coap_log_warn("OSCORE: Replayed or old message\n");
1338 build_and_send_error_pdu(session,
1339 pdu,
1340 COAP_RESPONSE_CODE(401),
1341 "Replay detected",
1342 NULL,
1343 NULL,
1344 0);
1345 goto error_no_ack;
1346 }
1347 if (!no_replay && rcp_ctx->initial_state == 1) {
1348 incoming_seq =
1350 rcp_ctx->last_seq = incoming_seq;
1351 }
1352 } else { /* !coap_request */
1353 /*
1354 * 8.4 Step 2
1355 * Decompress COSE object
1356 * Get Recipient Context based on token
1357 */
1358 if (oscore_decode_option_value(osc_value, osc_size, cose) == 0) {
1359 coap_log_warn("OSCORE: OSCORE Option cannot be decoded.\n");
1362 session);
1363 goto error;
1364 }
1365 got_resp_piv = cose->partial_iv.length ? 1 : 0;
1366
1367 association = oscore_find_association(session, &pdu_token);
1368 if (association) {
1369 rcp_ctx = association->recipient_ctx;
1370 osc_ctx = rcp_ctx->osc_ctx;
1371 snd_ctx = osc_ctx->sender_context;
1372#if COAP_CLIENT_SUPPORT
1373 sent_pdu = association->sent_pdu;
1374 if (session->b_2_step != COAP_OSCORE_B_2_NONE) {
1375 const uint8_t *ptr = cose->kid_context.s;
1376 size_t length = cose->kid_context.length;
1377
1378 if (ptr) {
1379 /* Need to CBOR unwrap kid_context */
1380 coap_bin_const_t kid_context;
1381
1382 kid_context.length = oscore_cbor_get_element_size(&ptr, &length);
1383 if (kid_context.length > length)
1384 goto error;
1385 /* This has to fit into an OSCORE option max 255.
1386 * Initial byte, kid_context size, partial IV size and kid size have to be there
1387 */
1388 if (kid_context.length >= 255 - 1 - 1 - cose->partial_iv.length - cose->key_id.length)
1389 goto error;
1390 kid_context.s = ptr;
1391 cose_encrypt0_set_kid_context(cose, &kid_context);
1392 }
1393 if (ptr && !coap_binary_equal(osc_ctx->id_context, &cose->kid_context)) {
1394 /* If Appendix B.2 step 3 is in operation */
1395 /* Need to update Security Context with new (R2 || ID1) ID Context */
1397 osc_ctx->id_context->length);
1398
1399 if (kc == NULL) {
1402 session);
1403 goto error;
1404 }
1405
1406 memcpy(kc->s, cose->kid_context.s, cose->kid_context.length);
1407 memcpy(&kc->s[cose->kid_context.length],
1408 osc_ctx->id_context->s,
1409 osc_ctx->id_context->length);
1410
1411 session->b_2_step = COAP_OSCORE_B_2_STEP_3;
1412 coap_log_oscore("Appendix B.2 client step 3 (R2 || ID1)\n");
1413 oscore_update_ctx(osc_ctx, (coap_bin_const_t *)kc);
1414 } else {
1415 session->b_2_step = COAP_OSCORE_B_2_STEP_5;
1416 coap_log_oscore("Appendix B.2 client step 5 (R2 || R3)\n");
1417 }
1418 }
1419#endif /* COAP_CLIENT_SUPPORT */
1420 } else {
1421 coap_log_crit("OSCORE: Security Context association not found\n");
1424 session);
1425 goto error;
1426 }
1427 }
1428
1429#if COAP_OSCORE_GROUP_SUPPORT
1430 group_message = rcp_ctx->mode == OSCORE_MODE_GROUP;
1431 if ((cose->group_flag == 1 && rcp_ctx->mode != OSCORE_MODE_GROUP) ||
1432 (cose->group_flag == 0 && rcp_ctx->mode == OSCORE_MODE_GROUP)) {
1433 /* mode cannot be treated according to oscore context */
1434 coap_log_warn("OSCORE: Unsupported mode\n");
1436 goto error;
1437 }
1438
1439 if (rcp_ctx->mode == OSCORE_MODE_GROUP && coap_request)
1440 cose_encrypt0_set_alg(cose, osc_ctx->alg_group_enc);
1441 else
1442#endif /* COAP_OSCORE_GROUP_SUPPORT */
1443 cose_encrypt0_set_alg(cose, osc_ctx->aead_alg);
1444
1445 if (coap_request) {
1446 /*
1447 * RFC8613 8.2 Step 4.
1448 * Compose the External AAD and then AAD
1449 *
1450 * OSCORE_option requires
1451 * partial_iv (cose partial_iv)
1452 * kid_context (cose kid_context)
1453 * key_id (cose key_id)
1454 * group_flag
1455 *
1456 * Non Group requires the following
1457 * RFC8613 5.4
1458 * oscore_version 1
1459 * algorithms [
1460 * aead_alg (osc_ctx)
1461 * ]
1462 * request_kid (request key_id using cose)
1463 * request_piv (request partial_iv using cose)
1464 * options (none at present)
1465 * Group requires the following
1466 * RFCxxxx 3.4
1467 * oscore_version 1
1468 * algorithms [
1469 * aead_alg (osc_ctx) (pairwise mode)
1470 * alg_group_enc (osc_ctx) (group mode)
1471 * alg_signature (osc_ctx) (group mode)
1472 * alg_pairwise_key_agreement (osc_ctx) (pairwise mode)
1473 * ]
1474 * request_kid (request key_id using cose)
1475 * request_piv (request partial_iv using cose)
1476 * options (none at present)
1477 * request_kid_context (osc_ctx id_context)
1478 * OSCORE_option (parameter)
1479 * sender_cred (osc_ctx sender_context credentials)
1480 * gm_cred (osc_ctx gm_cred)
1481 *
1482 * Note: No I options at present
1483 */
1484
1485 /* External AAD */
1486 external_aad.s = external_aad_buffer;
1487 external_aad.length = oscore_prepare_e_aad(osc_ctx,
1488 cose,
1489 osc_value,
1490 osc_size,
1491#if COAP_OSCORE_GROUP_SUPPORT
1492 rcp_ctx->rem_cred,
1493 rcp_ctx->mode,
1494#else /* COAP_OSCORE_GROUP_SUPPORT */
1495 NULL,
1496#endif /* COAP_OSCORE_GROUP_SUPPORT */
1497 external_aad_buffer,
1498 sizeof(external_aad_buffer));
1499 if (external_aad.length == 0)
1500 goto error;
1501 cose_encrypt0_set_external_aad(cose, &external_aad);
1502
1503 /* AAD */
1504 aad.s = aad_buffer;
1505 aad.length = oscore_prepare_aad(external_aad_buffer,
1506 external_aad.length,
1507 aad_buffer,
1508 sizeof(aad_buffer));
1509 if (aad.length == 0)
1510 goto error;
1511 assert(aad.length < AAD_BUF_LEN);
1512 cose_encrypt0_set_aad(cose, &aad);
1513
1514 /*
1515 * RFC8613 8.2 Step 5.
1516 * Compute the AEAD nonce.
1517 *
1518 * Requires in COSE object as appropriate
1519 * key_id (kid) (Recipient ID)
1520 * partial_iv (as received in request)
1521 * common_iv (already in osc_ctx)
1522 */
1523 nonce.s = nonce_buffer;
1524 nonce.length = 13;
1525 oscore_generate_nonce(cose, osc_ctx, nonce_buffer, 13);
1526 cose_encrypt0_set_nonce(cose, &nonce);
1527 /*
1528 * Set up an association for use in the response
1529 * (none with COAP_OSCORE_DECRYPT_NO_REPLAY: no response follows)
1530 */
1531 association = no_replay ? NULL :
1532 oscore_find_association(session, &pdu_token);
1533 if (association) {
1534 /* Refresh the association */
1535 coap_delete_bin_const(association->nonce);
1536 association->nonce =
1537 coap_new_bin_const(cose->nonce.s, cose->nonce.length);
1538 if (association->nonce == NULL)
1539 goto error;
1540 coap_delete_bin_const(association->partial_iv);
1541 association->partial_iv =
1543 if (association->partial_iv == NULL)
1544 goto error;
1545 coap_delete_bin_const(association->aad);
1546 association->aad = coap_new_bin_const(cose->aad.s, cose->aad.length);
1547 if (association->aad == NULL)
1548 goto error;
1549 coap_oscore_association_set_recipient_ctx(association, rcp_ctx);
1550 /* So association is not released when handling decrypt */
1551 association = NULL;
1552 } else {
1553 if (!no_replay && !oscore_new_association(session,
1554 NULL,
1555 &pdu_token,
1556 rcp_ctx,
1557 &cose->aad,
1558 &cose->nonce,
1559 &cose->partial_iv,
1560 0)) {
1561 goto error;
1562 }
1563 /*
1564 * New association for this request; delete on failure below if no
1565 * OSCORE-protected response will be sent. Refreshed associations are
1566 * left on the session (association cleared above).
1567 */
1568 decrypt_request_association = no_replay ? NULL :
1569 oscore_find_association(session, &pdu_token);
1570 }
1571 /* So association is not released when handling decrypt */
1572 association = NULL;
1573 } else { /* ! coap_request */
1574 /* Need to do nonce before AAD because of different partial_iv */
1575 /*
1576 * 8.4 Step 4.
1577 * Compose the AEAD nonce.
1578 */
1579 cose_encrypt0_set_key_id(cose, rcp_ctx->recipient_id);
1580 if (cose->partial_iv.length == 0) {
1581 cose_encrypt0_set_partial_iv(cose, association->partial_iv);
1582 cose_encrypt0_set_nonce(cose, association->nonce);
1583 } else {
1584 uint64_t last_seq;
1585
1586 oscore_replay_save(&replay_saved, rcp_ctx);
1587 if ((rcp_ctx->initial_state == 0 || osc_ctx->rfc8613_b_1_2 == 0) &&
1588 !oscore_validate_sender_seq(rcp_ctx, cose)) {
1589 coap_log_warn("OSCORE: Replayed or old message\n");
1590 goto error;
1591 }
1592 last_seq =
1594 if (rcp_ctx->last_seq>= OSCORE_SEQ_MAX) {
1595 coap_log_warn("OSCORE Replay protection, SEQ larger than SEQ_MAX.\n");
1596 goto error;
1597 }
1598 if (last_seq > rcp_ctx->last_seq)
1599 rcp_ctx->last_seq = last_seq;
1600 /*
1601 * Requires in COSE object as appropriate
1602 * kid (set above)
1603 * partial_iv (as received)
1604 * common_iv (already in osc_ctx)
1605 */
1606 oscore_generate_nonce(cose, osc_ctx, nonce_buffer, 13);
1607 nonce.s = nonce_buffer;
1608 nonce.length = 13;
1609 cose_encrypt0_set_nonce(cose, &nonce);
1610 }
1611#ifdef OSCORE_EXTRA_DEBUG
1612 dump_cose(cose, "!req post set nonce");
1613#endif /* OSCORE_EXTRA_DEBUG */
1614 /*
1615 * 8.4 Step 3.
1616 * Compose the External AAD and then AAD
1617 *
1618 * OSCORE_option requires
1619 * partial_iv (cose partial_iv)
1620 * kid_context (cose kid_context)
1621 * key_id (cose key_id)
1622 * group_flag
1623 *
1624 * Non Group requires the following
1625 * RFC8613 5.4
1626 * oscore_version 1
1627 * algorithms [
1628 * aead_alg (osc_ctx)
1629 * ]
1630 * request_kid (request key_id using cose)
1631 * request_piv (request partial_iv using cose)
1632 * options (none at present)
1633 * Group requires the following
1634 * RFCxxxx 3.4
1635 * oscore_version 1
1636 * algorithms [
1637 * aead_alg (osc_ctx) (pairwise mode)
1638 * alg_group_enc (osc_ctx) (group mode)
1639 * alg_signature (osc_ctx) (group mode)
1640 * alg_pairwise_key_agreement (osc_ctx) (pairwise mode)
1641 * ]
1642 * request_kid (request key_id using cose)
1643 * request_piv (request partial_iv using cose)
1644 * options (none at present)
1645 * request_kid_context (osc_ctx id_context)
1646 * OSCORE_option (parameter)
1647 * sender_cred (osc_ctx sender_context credentials)
1648 * gm_cred (osc_ctx gm_cred)
1649 *
1650 * Note: No I options at present
1651 */
1652
1653 /* External AAD */
1654 cose_encrypt0_set_key_id(cose, snd_ctx->sender_id);
1655 if (association->is_observe && association->obs_partial_iv && got_resp_piv) {
1656 cose_encrypt0_set_partial_iv(cose, association->obs_partial_iv);
1657 } else {
1658 cose_encrypt0_set_partial_iv(cose, association->partial_iv);
1659 }
1660#ifdef OSCORE_EXTRA_DEBUG
1661 dump_cose(cose, "!req pre aad");
1662#endif /* OSCORE_EXTRA_DEBUG */
1663 external_aad.s = external_aad_buffer;
1664 external_aad.length = oscore_prepare_e_aad(osc_ctx,
1665 cose,
1666#if COAP_OSCORE_GROUP_SUPPORT
1667 osc_value,
1668 osc_size,
1669 rcp_ctx->rem_cred,
1670 rcp_ctx->mode,
1671#else /* COAP_OSCORE_GROUP_SUPPORT */
1672 NULL,
1673 0,
1674 NULL,
1675#endif /* COAP_OSCORE_GROUP_SUPPORT */
1676 external_aad_buffer,
1677 sizeof(external_aad_buffer));
1678 if (external_aad.length == 0)
1679 goto error;
1680 cose_encrypt0_set_external_aad(cose, &external_aad);
1681
1682 /* AAD */
1683 aad.s = aad_buffer;
1684 aad.length = oscore_prepare_aad(external_aad_buffer,
1685 external_aad.length,
1686 aad_buffer,
1687 sizeof(aad_buffer));
1688 if (aad.length == 0)
1689 goto error;
1690 assert(aad.length < AAD_BUF_LEN);
1691 cose_encrypt0_set_aad(cose, &aad);
1692#ifdef OSCORE_EXTRA_DEBUG
1693 dump_cose(cose, "!req post set aad");
1694#endif /* OSCORE_EXTRA_DEBUG */
1695 }
1696
1697 /*
1698 * 8.2 Step 6 / 8.4 Step 5.
1699 * Decrypt the COSE object.
1700 *
1701 * Requires in COSE object as appropriate
1702 * alg (already set)
1703 * key
1704 * nonce (already set)
1705 * aad (already set)
1706 * ciphertext
1707 */
1708 st_encrypt = pdu->data;
1709 encrypt_len = pdu->used_size - (pdu->data - pdu->token);
1710#if COAP_OSCORE_GROUP_SUPPORT
1711 if (group_message == 1)
1712 encrypt_len = encrypt_len - 64 /* snd_ctx->sender_cred->sign_size */;
1713#endif /* COAP_OSCORE_GROUP_SUPPORT */
1714 if (encrypt_len <= 0) {
1715 coap_log_warn("OSCORE: No protected payload\n");
1716 if (!coap_request)
1719 session);
1720 goto error;
1721 }
1722 cose_encrypt0_set_key(cose, rcp_ctx->recipient_key);
1723 cose_encrypt0_set_ciphertext(cose, st_encrypt, encrypt_len);
1724
1725 tag_len = cose_tag_len(cose->alg);
1726 if (encrypt_len <= tag_len) {
1727 /* Too short to hold the AEAD tag plus a 1 byte inner code; a shorter
1728 value would underflow encrypt_len - tag_len below */
1729 coap_log_warn("OSCORE: Ciphertext too short\n");
1730 if (coap_request) {
1731 build_and_send_error_pdu(session,
1732 pdu,
1733 COAP_RESPONSE_CODE(400),
1734 "Decryption failed",
1735 NULL,
1736 NULL,
1737 0);
1738 oscore_roll_back_seq(rcp_ctx);
1739 goto error_no_ack;
1740 }
1743 session);
1744 goto error;
1745 }
1746 /* Decrypt into plain_pdu, so code (token), options and data are in place */
1747 plain_pdu = coap_pdu_init(pdu->type, 0, 0, encrypt_len /* - tag_len */);
1748 if (plain_pdu == NULL) {
1749 if (!coap_request)
1752 session);
1753 goto error;
1754 }
1755
1756 /* need the tag_len on the end for TinyDTLS to do its work - yuk */
1757 if (!coap_pdu_resize(plain_pdu, encrypt_len /* - tag_len */)) {
1758 if (!coap_request)
1761 session);
1762 goto error;
1763 }
1764
1765 /* Account for 1 byte 'code' used as token */
1766 plain_pdu->e_token_length = 1;
1767 plain_pdu->actual_token.length = 1;
1768 /* Account for the decrypted data */
1769 plain_pdu->used_size = encrypt_len - tag_len;
1770
1771#if COAP_OSCORE_GROUP_SUPPORT
1772 if (group_message == 1) {
1773 /* verify signature */
1774 size_t sign_size = 64 /* snd_ctx->sender_cred->sign_size */;
1775 uint8_t *st_signature = st_encrypt + encrypt_len;
1776 uint8_t *sig_buffer = NULL;
1777 size_t sig_len = external_aad.length + encrypt_len + 30;
1778 int sign_res;
1779 uint8_t *keystream = coap_malloc_type(COAP_STRING, sign_size);
1780
1781 sig_buffer = coap_malloc_type(COAP_STRING, sig_len);
1782 oscore_populate_sign(sign, osc_ctx, rcp_ctx->rem_cred, NULL);
1783 sig_len = oscore_prepare_sig_structure(sig_buffer,
1784 sig_len,
1785 external_aad_buffer,
1786 external_aad.length,
1787 st_encrypt,
1788 encrypt_len);
1789 assert(external_aad.length + encrypt_len + 30 > sig_len);
1790 cose_sign1_set_signature(sign, st_signature, sign_size);
1791 cose_sign1_set_ciphertext(sign, sig_buffer, sig_len);
1792
1793 /* SIGNATURE = ENC_SIGNATURE XOR KEYSTREAM */
1795 cose,
1796 coap_request,
1797 rcp_ctx->recipient_id,
1798 osc_ctx->id_context,
1799 sign_size,
1800 keystream,
1801 sign_size);
1802 for (size_t i = 0; i < sign_size; i++) {
1803 st_signature[i] = st_signature[i] ^ keystream[i];
1804 }
1805 coap_free_type(COAP_STRING, keystream);
1806
1807 sign_res = cose_sign1_verify(sign);
1808 coap_free_type(COAP_STRING, sig_buffer);
1809 if (sign_res == 0) {
1810 coap_log_warn("OSCORE: Signature verification Failure\n");
1811 oscore_replay_restore(&replay_saved, rcp_ctx);
1812 if (coap_request) {
1813 if (!no_replay)
1814 build_and_send_error_pdu(session,
1815 pdu,
1816 COAP_RESPONSE_CODE(400),
1817 "Decryption failed",
1818 NULL,
1819 NULL,
1820 0);
1821 goto error_no_ack;
1822 } else {
1825 session);
1826 goto error;
1827 }
1828 }
1829 }
1830#endif /* COAP_OSCORE_GROUP_SUPPORT */
1831 dump_cose(cose, "Pre decrypt");
1832 pltxt_size =
1833 cose_encrypt0_decrypt(cose, plain_pdu->token, encrypt_len - tag_len);
1834 if (pltxt_size <= 0) {
1835 coap_log_warn("OSCORE: Decryption Failure, result code: %d \n",
1836 (int)pltxt_size);
1837 oscore_replay_restore(&replay_saved, rcp_ctx);
1838 if (coap_request) {
1839 if (no_replay)
1840 goto error_no_ack; /* nothing was advanced, nothing to roll back */
1841 build_and_send_error_pdu(session,
1842 pdu,
1843 COAP_RESPONSE_CODE(400),
1844 "Decryption failed",
1845 NULL,
1846 NULL,
1847 0);
1848 oscore_roll_back_seq(rcp_ctx);
1849 goto error_no_ack;
1850 } else {
1853 session);
1854 oscore_roll_back_seq(rcp_ctx);
1855 }
1856 goto error;
1857 }
1858 /* Replay window update is now verified */
1859 rcp_ctx->rollback_valid = 0;
1860 if (coap_request && rcp_ctx->initial_state == 0 &&
1861 session->context->oscore_update_seq_num_cb != NULL) {
1862 /* notify custom credential storage */
1863 coap_lock_callback(session->context->oscore_update_seq_num_cb(session,
1864 rcp_ctx->recipient_id,
1865 osc_ctx->id_context,
1866 rcp_ctx->last_seq,
1867 rcp_ctx->sliding_window));
1868 }
1869
1870 assert((size_t)pltxt_size < pdu->alloc_size + pdu->max_hdr_size);
1871
1872 /* Appendix B.2 Trap */
1873 if (!no_replay && session->b_2_step == COAP_OSCORE_B_2_STEP_2) {
1874 /* Need to update Security Context with new (R2 || ID1) ID Context */
1875 coap_binary_t *kc =
1876 coap_new_binary(sizeof(session->oscore_r2) + cose->kid_context.length);
1877 coap_bin_const_t oscore_r2;
1878
1879 if (kc == NULL) {
1880 if (!coap_request)
1883 session);
1884 goto error;
1885 }
1886
1887 coap_prng_lkd(&session->oscore_r2, sizeof(session->oscore_r2));
1888 memcpy(kc->s, &session->oscore_r2, sizeof(session->oscore_r2));
1889 memcpy(&kc->s[sizeof(session->oscore_r2)],
1890 cose->kid_context.s,
1891 cose->kid_context.length);
1892
1893 coap_log_oscore("Appendix B.2 server step 2 (R2 || ID1)\n");
1894 oscore_update_ctx(osc_ctx, (coap_bin_const_t *)kc);
1895
1896 oscore_r2.length = sizeof(session->oscore_r2);
1897 oscore_r2.s = (const uint8_t *)&session->oscore_r2;
1898 coap_log_oscore("Appendix B.2 server step 2 plain response\n");
1899 build_and_send_error_pdu(session,
1900 pdu,
1901 COAP_RESPONSE_CODE(401),
1902 NULL,
1903 NULL,
1904 &oscore_r2,
1905 1);
1906 decrypt_request_association = NULL;
1907 goto error_no_ack;
1908 }
1909#if COAP_CLIENT_SUPPORT
1910 if (!no_replay && session->b_2_step == COAP_OSCORE_B_2_STEP_3) {
1911 coap_log_oscore("Appendix B.2 client step 3 (R2 || R3)\n");
1912 coap_pdu_encode_header(plain_pdu, session->proto);
1913 plain_pdu->actual_token.s = plain_pdu->token;
1914 plain_pdu->code = plain_pdu->token[0];
1915 if (plain_pdu->code != COAP_RESPONSE_CODE(401)) {
1916 coap_log_warn("OSCORE Appendix B.2: Expected 4.01 response\n");
1917 }
1918 /* Skip the options */
1919 coap_option_iterator_init(plain_pdu, &opt_iter, COAP_OPT_ALL);
1920 while (coap_option_next(&opt_iter)) {
1921 }
1922 if (opt_iter.length > 0 && opt_iter.next_option &&
1923 opt_iter.next_option[0] == COAP_PAYLOAD_START) {
1924 plain_pdu->data = &opt_iter.next_option[1];
1925 }
1926 coap_log_oscore("Inner Response PDU (plaintext)\n");
1927 coap_show_pdu(COAP_LOG_OSCORE, plain_pdu);
1928 /*
1929 * Need to update Security Context with new (R2 || R3) ID Context
1930 * and retransmit the request
1931 */
1933
1934 if (kc == NULL) {
1935 if (!coap_request)
1938 session);
1939 goto error;
1940 }
1941 memcpy(kc->s, cose->kid_context.s, cose->kid_context.length);
1942 coap_prng_lkd(&kc->s[cose->kid_context.length], 8);
1943
1944 oscore_update_ctx(osc_ctx, (coap_bin_const_t *)kc);
1945
1947 session,
1948 &pdu->actual_token);
1949 if (session->con_active)
1950 session->con_active--;
1951 coap_send_ack_lkd(session, pdu);
1952 if (sent_pdu) {
1953 coap_log_oscore("Appendix B.2 retransmit pdu\n");
1954 if (!session->b_2_retransmit_token) {
1955 session->b_2_retransmit_token = coap_new_str_const(sent_pdu->actual_token.s,
1956 sent_pdu->actual_token.length);
1957 }
1958 if (coap_retransmit_oscore_pdu(session, sent_pdu, NULL) ==
1960 goto error_no_ack;
1961 }
1962 goto error_no_ack;
1963 }
1964#endif /* COAP_CLIENT_SUPPORT */
1965
1966#if COAP_SERVER_SUPPORT
1967 /* Appendix B.1.2 request Trap */
1968 if (coap_request && !no_replay && osc_ctx->rfc8613_b_1_2) {
1969 if (rcp_ctx->initial_state == 1) {
1970 opt = coap_check_option(plain_pdu, COAP_OPTION_ECHO, &opt_iter);
1971 if (opt) {
1972 /* Verify Client is genuine */
1973 if (coap_opt_length(opt) == 8 &&
1974 memcmp(coap_opt_value(opt), rcp_ctx->echo_value, 8) == 0) {
1975 /* This clears rcp_ctx->initial_state */
1976 if (!oscore_validate_sender_seq(rcp_ctx, cose)) {
1977 coap_log_warn("OSCORE: Replayed or old message\n");
1978 build_and_send_error_pdu(session,
1979 pdu,
1980 COAP_RESPONSE_CODE(401),
1981 "Replay detected",
1982 NULL,
1983 NULL,
1984 0);
1985 goto error_no_ack;
1986 } else {
1987 if (session->context->oscore_update_seq_num_cb != NULL) {
1988 /* notify echo challenge changed */
1989 coap_lock_callback(session->context->oscore_update_seq_num_cb(session,
1990 rcp_ctx->recipient_id,
1991 osc_ctx->id_context,
1992 rcp_ctx->last_seq,
1993 rcp_ctx->sliding_window));
1994 }
1995 }
1996 } else
1997 goto error;
1998 } else {
1999 /* RFC 8163 Appendix B.1.2 */
2000 if (session->b_2_step == COAP_OSCORE_B_2_STEP_4) {
2001 session->b_2_step = COAP_OSCORE_B_2_NONE;
2002 coap_log_oscore("Appendix B.2 server finished\n");
2003 }
2004 coap_prng_lkd(rcp_ctx->echo_value, sizeof(rcp_ctx->echo_value));
2005 coap_log_oscore("Appendix B.1.2 server plain response\n");
2006
2007 build_and_send_error_pdu(session,
2008 pdu,
2009 COAP_RESPONSE_CODE(401),
2010 NULL,
2011 rcp_ctx->echo_value,
2012 NULL,
2013 1);
2014 decrypt_request_association = NULL;
2015 goto error_no_ack;
2016 }
2017 }
2018 }
2019#endif /* COAP_SERVER_SUPPORT */
2020
2021 /*
2022 * 8.2 Step 7 / 8.4 Step 6.
2023 * Add decrypted Code, options and payload
2024 * [OSCORE option not copied across previously]
2025 */
2026
2027 /* PDU code is pseudo plain_pdu token */
2028 decrypt_pdu->code = plain_pdu->token[0];
2029
2030 /* Copy inner decrypted options across */
2031 coap_option_iterator_init(plain_pdu, &opt_iter, COAP_OPT_ALL);
2032 while ((opt = coap_option_next(&opt_iter))) {
2033 size_t len;
2034 size_t bias;
2035
2036 switch (opt_iter.number) {
2037 case COAP_OPTION_OSCORE:
2038 break;
2040 if (!coap_request) {
2041 bias = cose->partial_iv.length > 3 ? cose->partial_iv.length - 3 : 0;
2042 len = cose->partial_iv.length > 3 ? 3 : cose->partial_iv.length;
2043 /* Make Observe option reflect last 3 bytes of partial_iv */
2045 decrypt_pdu,
2046 opt_iter.number,
2047 len,
2048 cose->partial_iv.s ? &cose->partial_iv.s[bias] : NULL)) {
2051 session);
2052 goto error;
2053 }
2054 doing_resp_observe = 1;
2055 break;
2056 }
2057 association = no_replay ? NULL :
2058 oscore_find_association(session, &pdu_token);
2059 if (association) {
2060 association->is_observe = 1;
2061 association = NULL;
2062 }
2063 /* Fall Through */
2064 default:
2065 if (!coap_insert_option(decrypt_pdu,
2066 opt_iter.number,
2067 coap_opt_length(opt),
2068 coap_opt_value(opt))) {
2069 if (!coap_request)
2072 session);
2073 goto error;
2074 }
2075 break;
2076 }
2077 }
2078 if (!coap_request && !doing_resp_observe) {
2079 if (association) {
2080 association->is_observe = 0;
2081 }
2082 }
2083 /* Need to copy across any data */
2084 if (opt_iter.length > 0 && opt_iter.next_option &&
2085 opt_iter.next_option[0] == COAP_PAYLOAD_START) {
2086 plain_pdu->data = &opt_iter.next_option[1];
2087 if (!coap_add_data(decrypt_pdu,
2088 plain_pdu->used_size -
2089 (plain_pdu->data - plain_pdu->token),
2090 plain_pdu->data)) {
2091 if (!coap_request)
2094 session);
2095 goto error;
2096 }
2097 }
2098 coap_delete_pdu_lkd(plain_pdu);
2099 plain_pdu = NULL;
2100
2101 /* Make sure headers are correctly set up */
2102 if (!coap_pdu_encode_header(decrypt_pdu, session->proto)) {
2103 if (!coap_request)
2106 session);
2107 goto error;
2108 }
2109
2110 if (!no_replay && session->b_2_step != COAP_OSCORE_B_2_NONE) {
2111 session->b_2_step = COAP_OSCORE_B_2_NONE;
2112 coap_log_oscore("Appendix B.2 client finished\n");
2113 if (session->b_2_retransmit_token) {
2114 coap_update_token(decrypt_pdu, session->b_2_retransmit_token->length,
2115 session->b_2_retransmit_token->s);
2116 coap_delete_str_const(session->b_2_retransmit_token);
2117 session->b_2_retransmit_token = NULL;
2118 }
2119 }
2120#if COAP_CLIENT_SUPPORT
2121 if (decrypt_pdu->code == COAP_RESPONSE_CODE(401) &&
2122 (opt = coap_check_option(decrypt_pdu, COAP_OPTION_ECHO, &opt_iter))) {
2123 /* Server is requesting Echo refresh check */
2125 session,
2126 &pdu->actual_token);
2127 if (session->con_active)
2128 session->con_active--;
2129 if (!sent_pdu) {
2130 coap_lg_crcv_t *lg_crcv = coap_find_lg_crcv(session, pdu);
2131
2132 if (lg_crcv)
2133 sent_pdu = lg_crcv->sent_pdu;
2134 }
2135 if (sent_pdu) {
2136 coap_send_ack_lkd(session, pdu);
2137 coap_log_debug("PDU requesting re-transmit\n");
2138 coap_show_pdu(COAP_LOG_DEBUG, decrypt_pdu);
2139 coap_log_oscore("RFC9175 retransmit pdu\n");
2140 /* Do not care if this fails */
2141 if (coap_retransmit_oscore_pdu(session, sent_pdu, opt) != COAP_INVALID_MID) {
2142 session->doing_b_1_2 = 1;
2143 }
2144 goto error_no_ack;
2145 }
2146 } else if (!no_replay && session->doing_b_1_2) {
2147 coap_lg_crcv_t *lg_crcv = coap_find_lg_crcv(session, pdu);
2148
2149 if (lg_crcv) {
2150 if (!coap_binary_equal(&decrypt_pdu->actual_token, lg_crcv->app_token)) {
2151 coap_update_token(decrypt_pdu, lg_crcv->app_token->length, lg_crcv->app_token->s);
2152 }
2153 }
2154 session->doing_b_1_2 = 0;
2155 }
2156#endif /* COAP_CLIENT_SUPPORT */
2157 if (association && association->is_observe == 0)
2158 oscore_delete_association(session, association);
2159 return decrypt_pdu;
2160
2161error:
2162 if (!no_replay)
2163 coap_send_ack_lkd(session, pdu);
2164error_no_ack:
2165 if (decrypt_request_association) {
2166 oscore_delete_association(session, decrypt_request_association);
2167 decrypt_request_association = NULL;
2168 }
2169 if (association && association->is_observe == 0)
2170 oscore_delete_association(session, association);
2171 coap_delete_pdu_lkd(decrypt_pdu);
2172 coap_delete_pdu_lkd(plain_pdu);
2173 return NULL;
2174}
2175
2176typedef enum {
2177 COAP_ENC_ASCII = 0x001,
2178 COAP_ENC_HEX = 0x002,
2179#if COAP_OSCORE_GROUP_SUPPORT
2180 COAP_ENC_PUB_RAW = 0x004,
2181 COAP_ENC_PUB_ASN1 = 0x008,
2182 COAP_ENC_PUB_FILE_PEM = 0x010,
2183 COAP_ENC_PUB_CCS = 0x020,
2184 COAP_ENC_PRI_RAW = 0x040,
2185 COAP_ENC_PRI_ASN1 = 0x080,
2186 COAP_ENC_PRI_FILE_PEM = 0x100,
2187#endif /* COAP_OSCORE_GROUP_SUPPORT */
2188 COAP_ENC_CONFIG = 0x0200,
2189 COAP_ENC_INTEGER = 0x400,
2190 COAP_ENC_TEXT = 0x800,
2191 COAP_ENC_BOOL = 0x1000,
2192 COAP_ENC_UNSIGNED64 = 0x2000,
2193 COAP_ENC_LAST
2194} coap_oscore_coding_t;
2195
2196#undef TEXT_MAPPING
2197#define TEXT_MAPPING(t, v) \
2198 { { sizeof(#t)-1, (const uint8_t *)#t }, v }
2199
2200static struct coap_oscore_encoding_t {
2201 coap_str_const_t name;
2202 coap_oscore_coding_t encoding;
2203} oscore_encoding[] = {
2204 TEXT_MAPPING(ascii, COAP_ENC_ASCII),
2205 TEXT_MAPPING(hex, COAP_ENC_HEX),
2206#if COAP_OSCORE_GROUP_SUPPORT
2207 TEXT_MAPPING(pub_raw, COAP_ENC_PUB_RAW),
2208 TEXT_MAPPING(pub_asn1, COAP_ENC_PUB_ASN1),
2209 TEXT_MAPPING(pub_file_pem, COAP_ENC_PUB_FILE_PEM),
2210 TEXT_MAPPING(pub_ccs, COAP_ENC_PUB_CCS),
2211 TEXT_MAPPING(pri_raw, COAP_ENC_PRI_RAW),
2212 TEXT_MAPPING(pri_asn1, COAP_ENC_PRI_ASN1),
2213 TEXT_MAPPING(pri_file_pem, COAP_ENC_PRI_FILE_PEM),
2214#endif /* COAP_OSCORE_GROUP_SUPPORT */
2215 TEXT_MAPPING(config, COAP_ENC_CONFIG),
2216 TEXT_MAPPING(integer, COAP_ENC_INTEGER),
2217 TEXT_MAPPING(unsigned64, COAP_ENC_UNSIGNED64),
2218 TEXT_MAPPING(text, COAP_ENC_TEXT),
2219 TEXT_MAPPING(bool, COAP_ENC_BOOL),
2220 {{0, NULL}, COAP_ENC_LAST}
2221};
2222
2223typedef struct {
2224 coap_oscore_coding_t encoding;
2225 const char *encoding_name;
2226 union {
2227 int value_int;
2228 uint64_t value_int64;
2229 coap_bin_const_t *value_bin;
2230#if COAP_OSCORE_GROUP_SUPPORT
2231 coap_crypto_cred_t *value_cred;
2232 coap_crypto_pri_key_t *value_pri;
2233#endif /* COAP_OSCORE_GROUP_SUPPORT */
2234 coap_str_const_t value_str;
2235 } u;
2236} oscore_value_t;
2237
2238/*
2239 * Return 1 if hex character
2240 * 0 not hex character
2241 */
2242static int
2243hex_to_char(char c, uint8_t *value) {
2244 if ('a' <= c && c <= 'f') {
2245 *value = c - 'a' + 10;
2246 } else if ('A' <= c && c <= 'F') {
2247 *value = c - 'A' + 10;
2248 } else if ('0' <= c && c <= '9') {
2249 *value = c - '0';
2250 } else {
2251 *value = 0;
2252 return 0;
2253 }
2254 return 1;
2255}
2256
2257/* Parse the hex into binary */
2258static coap_bin_const_t *
2259parse_hex_bin(const char *begin, const char *end) {
2260 coap_binary_t *binary = NULL;
2261 size_t i;
2262 size_t o = 0;
2263
2264 binary = coap_new_binary((end - begin) / 2);
2265 if (binary == NULL)
2266 goto bad_entry;
2267 for (i = 0; (i < (size_t)(end - begin)); i++) {
2268 while ((i < (size_t)(end - begin)) &&
2269 (begin[i] == '\r' || begin[i] == '\n' || begin[i] == ' ')) {
2270 i++;
2271 }
2272 if (i == (size_t)(end - begin))
2273 break;
2274 if (isxdigit((uint8_t)begin[i])) {
2275 uint8_t value;
2276
2277 if (!hex_to_char(begin[i], &value)) {
2278 goto bad_entry;
2279 }
2280 binary->s[o] = value << 4;
2281 i++;
2282 while ((i < (size_t)(end - begin)) &&
2283 (begin[i] == '\r' || begin[i] == '\n' || begin[i] == ' ')) {
2284 i++;
2285 }
2286 if (i == (size_t)(end - begin))
2287 goto bad_entry;
2288 if (!hex_to_char(begin[i], &value)) {
2289 goto bad_entry;
2290 }
2291 binary->s[o++] += value;
2292 } else {
2293 break;
2294 }
2295 }
2296 if (i != (size_t)(end - begin))
2297 goto bad_entry;
2298 return (coap_bin_const_t *)binary;
2299
2300bad_entry:
2301 coap_delete_binary(binary);
2302 return NULL;
2303}
2304
2305#if COAP_OSCORE_GROUP_SUPPORT
2306static int
2307coap_crypto_read_ccs_public_cred(coap_bin_const_t *binary,
2308 coap_crypto_cred_t **public) {
2309 int64_t mm = 0;
2310 int64_t nn = 0;
2311 int64_t oo = 0;
2312 size_t m;
2313 size_t n;
2314 size_t o;
2315 const uint8_t *data = binary->s;
2316 size_t data_len = binary->length;
2317 int8_t ok;
2318 size_t map_count_m;
2319 size_t map_count_n;
2320 size_t map_count_o;
2321 size_t elem_size;
2322 int8_t elem = oscore_cbor_get_next_element(&data, &data_len);
2323 coap_crypto_cred_t *local = NULL;
2324
2325 if (elem != CBOR_MAP)
2326 goto error;
2327
2329 if (local == NULL)
2330 goto error;
2331 memset(local, 0, sizeof(coap_crypto_cred_t));
2332
2333 map_count_m = oscore_cbor_get_element_size(&data, &data_len);
2334 if (map_count_m > data_len)
2335 goto error;
2336 for (m = 0; m < map_count_m; m++) {
2337 ok = oscore_cbor_get_number(&data, &data_len, &mm);
2338 if (ok != 0)
2339 goto error;
2340 switch (mm) {
2341 case 1: /* Issuer */
2342 case 2: /* Subject */
2343 case 3: /* Audience */
2344 /* text string */
2345 elem = oscore_cbor_get_next_element(&data, &data_len);
2346 if (elem != CBOR_TEXT_STRING)
2347 goto error;
2348 elem_size = oscore_cbor_get_element_size(&data, &data_len);
2349 if (elem_size > data_len)
2350 goto error;
2351 data += elem_size;
2352 data_len -= elem_size;
2353 break;
2354 case 4: /* Expiration Time */
2355 case 5: /* Not Before */
2356 case 6: /* Issued At */
2357 /* integer or floating-point number */
2358 elem = oscore_cbor_get_next_element(&data, &data_len);
2359 if (elem != CBOR_UNSIGNED_INTEGER && elem != CBOR_FLOATING_POINT)
2360 goto error;
2361 elem_size = oscore_cbor_get_element_size(&data, &data_len);
2362 if (elem_size > data_len)
2363 goto error;
2364 break;
2365 case 7: /* CWT ID */
2366 /* byte string */
2367 elem = oscore_cbor_get_next_element(&data, &data_len);
2368 if (elem != CBOR_BYTE_STRING)
2369 goto error;
2370 elem_size = oscore_cbor_get_element_size(&data, &data_len);
2371 if (elem_size > data_len)
2372 goto error;
2373 data += elem_size;
2374 data_len -= elem_size;
2375 break;
2376 case 8: /* Confirmation */
2377 /* map */
2378 elem = oscore_cbor_get_next_element(&data, &data_len);
2379 if (elem != CBOR_MAP)
2380 goto error;
2381 map_count_n = oscore_cbor_get_element_size(&data, &data_len);
2382 if (map_count_n > data_len)
2383 goto error;
2384 for (n = 0; n < map_count_n; n++) {
2385 ok = oscore_cbor_get_number(&data, &data_len, &nn);
2386 if (ok != 0)
2387 goto error;
2388 switch (nn) {
2389 case 1: /* COSE_key */
2390 /* map */
2391 elem = oscore_cbor_get_next_element(&data, &data_len);
2392 if (elem != CBOR_MAP)
2393 goto error;
2394 map_count_o = oscore_cbor_get_element_size(&data, &data_len);
2395 if (map_count_o > data_len)
2396 goto error;
2397 for (o = 0; o < map_count_o; o++) {
2398 ok = oscore_cbor_get_number(&data, &data_len, &oo);
2399 if (ok != 0)
2400 goto error;
2401 switch (oo) {
2402 case 1: /* kty */
2403 ok = oscore_cbor_get_number(&data, &data_len, &oo);
2404 if (ok != 0)
2405 goto error;
2406 local->cred_kty = oo;
2407 break;
2408 case 3: /* alg */
2409 ok = oscore_cbor_get_number(&data, &data_len, &oo);
2410 if (ok != 0)
2411 goto error;
2412 local->cred_alg = oo;
2413 break;
2414 case -1: /* crv */
2415 ok = oscore_cbor_get_number(&data, &data_len, &oo);
2416 if (ok != 0)
2417 goto error;
2418 local->cred_crv = oo;
2419 break;
2420 case -2: /* X */
2421 elem = oscore_cbor_get_next_element(&data, &data_len);
2422 if (elem != CBOR_BYTE_STRING)
2423 goto error;
2424 elem_size = oscore_cbor_get_element_size(&data, &data_len);
2425 if (elem_size > data_len)
2426 goto error;
2427 /* Duplicates not on */
2428 if (local->pub_raw)
2429 goto error;
2430 local->pub_raw = coap_new_bin_const(data, elem_size);
2431 if (!local->pub_raw)
2432 goto error;
2433 data += elem_size;
2434 data_len -= elem_size;
2435 break;
2436 default:
2437 coap_log_warn("coap_crypto_read_ccs_public_cred: COSE_Key %" PRIi64 " not supported\n", oo);
2438 goto error;
2439 }
2440 }
2441 break;
2442 case 2: /* Encrypted_COSE_Key */
2443 case 3: /* kid */
2444 default:
2445 coap_log_warn("coap_crypto_read_ccs_public_cred: CWT Confirmation Claim %" PRIi64
2446 " not supported\n", nn);
2447 goto error;
2448 }
2449 }
2450 if (local->cred_kty == COSE_KTY_OKP &&
2451 local->cred_crv == COSE_CURVE_ED25519 &&
2452 local->pub_raw && local->pub_raw->length == 32) {
2453 /* SubjectPublicKeyInfo for an Ed25519 key (RFC 8410) */
2454 static const uint8_t spki_ed25519[] = {
2455 0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
2456 0x70, 0x03, 0x21, 0x00
2457 };
2458 coap_binary_t *der = coap_new_binary(sizeof(spki_ed25519) +
2459 local->pub_raw->length);
2460 coap_crypto_cred_t *cred = NULL;
2461
2462 if (!der)
2463 goto error;
2464 memcpy(der->s, spki_ed25519, sizeof(spki_ed25519));
2465 memcpy(der->s + sizeof(spki_ed25519), local->pub_raw->s,
2466 local->pub_raw->length);
2467 /* der always gets deleted in coap_crypto_read_asn1_public_cred() */
2468 if (!coap_crypto_read_asn1_public_cred((coap_bin_const_t *)der, &cred))
2469 goto error;
2470 /*
2471 * RFC 10021 Sections 2.4 and 3.4: the
2472 * 'sender_cred' and 'gm_cred' of the external_aad are the original
2473 * binary representation of the authentication credential, i.e. the
2474 * CCS exactly as provided, not a re-encoding of the COSE Key.
2475 */
2476 cred->auth_cred = binary;
2477 cred->cred_type = OSCORE_AUTH_CRED_CCS;
2478 cred->cred_kty = local->cred_kty;
2479 cred->cred_crv = local->cred_crv;
2480 cred->cred_alg = local->cred_alg;
2483 *public = cred;
2484 return 1;
2485 }
2486 coap_log_warn("coap_crypto_read_ccs_public_cred: Not all of kty, crv and public_key defined\n");
2487 goto error;
2488 default:
2489 coap_log_warn("coap_crypto_read_ccs_public_cred: CBOR Web Token (CWT) Claims %" PRIi64
2490 " not supported\n", mm);
2491 goto error;
2492 }
2493 }
2494error:
2495 coap_delete_bin_const(binary);
2496 if (local) {
2501 }
2502 return 0;
2503}
2504#endif /* COAP_OSCORE_GROUP_SUPPORT */
2505
2506/*
2507 * Break up each OSCORE Configuration line entry into the 3 parts which
2508 * are comma separated
2509 *
2510 * keyword,encoding,value
2511 */
2512static int
2513get_split_entry(const char **start,
2514 size_t size,
2515 coap_str_const_t *keyword,
2516 oscore_value_t *value,
2517 cose_curve_t sign_curve) {
2518 const char *begin = *start;
2519 const char *keep_start = *start;
2520 const char *end;
2521 const char *kend;
2522 const char *tend;
2523 const char *split;
2524 size_t i;
2525 size_t len;
2526#if COAP_OSCORE_GROUP_SUPPORT
2527 coap_bin_const_t *binary = NULL;
2528 coap_binary_t *asn1;
2529 coap_string_t *file_name = NULL;
2530 coap_crypto_cred_t *cred;
2531 coap_crypto_pri_key_t *pri_key;
2532#else /* !COAP_OSCORE_GROUP_SUPPORT */
2533 (void)sign_curve;
2534#endif /* !COAP_OSCORE_GROUP_SUPPORT */
2535
2536retry:
2537 kend = end = memchr(begin, '\n', size);
2538 if (end == NULL)
2539 return 0;
2540
2541 /* Check for multi-line */
2542 if ((tend = memchr(begin, '"', end - begin))) {
2543 /* See if " terminator is on the same line */
2544 if (!memchr(tend + 1, '"', end - tend -1)) {
2545 /* Over multiple lines */
2546 kend = end = memchr(end, '"', size - (end - begin));
2547 if (end == NULL)
2548 return 0;
2549 kend = end = memchr(kend, '\n', size - (kend - begin));
2550 if (end == NULL)
2551 return 0;
2552 }
2553 }
2554
2555 /* Track beginning of next line */
2556 *start = end + 1;
2557 if (end > begin && end[-1] == '\r')
2558 end--;
2559
2560 if (begin[0] == '#' || (end - begin) == 0) {
2561 /* Skip comment / blank line */
2562 size -= kend - begin + 1;
2563 begin = *start;
2564 keep_start = *start;
2565 goto retry;
2566 }
2567
2568 /* Get in the keyword */
2569 split = memchr(begin, ',', end - begin);
2570 if (split == NULL)
2571 goto bad_entry;
2572
2573 keyword->s = (const uint8_t *)begin;
2574 keyword->length = split - begin;
2575
2576 begin = split + 1;
2577 if ((end - begin) == 0)
2578 goto bad_entry;
2579 /* Get in the encoding */
2580 split = memchr(begin, ',', end - begin);
2581 if (split == NULL)
2582 goto bad_entry;
2583
2584 for (i = 0; oscore_encoding[i].name.s; i++) {
2585 coap_str_const_t temp = { split - begin, (const uint8_t *)begin };
2586
2587 if (coap_string_equal(&temp, &oscore_encoding[i].name)) {
2588 value->encoding = oscore_encoding[i].encoding;
2589 value->encoding_name = (const char *)oscore_encoding[i].name.s;
2590 break;
2591 }
2592 }
2593 if (oscore_encoding[i].name.s == NULL)
2594 goto bad_entry;
2595
2596 begin = split + 1;
2597 if ((end - begin) == 0)
2598 goto bad_entry;
2599 /* Get in the keyword's value */
2600 if (value->encoding == COAP_ENC_CONFIG && begin[0] == '\'') {
2601 split = memchr(&begin[1], '\'', size - (begin - keep_start) - 1);
2602 if (split == NULL)
2603 goto bad_entry;
2604 end = memchr(split, '\n', size - (split - keep_start));
2605 if (end == NULL)
2606 return 0;
2607 *start = end + 1;
2608 end = split;
2609 begin++;
2610 } else {
2611 if (begin[0] == '"') {
2612 split = memchr(&begin[1], '"', end - split - 1);
2613 if (split == NULL)
2614 goto bad_entry;
2615 end = split;
2616 begin++;
2617 }
2618 }
2619 switch (value->encoding) {
2620 case COAP_ENC_CONFIG:
2621 case COAP_ENC_ASCII:
2622 value->u.value_bin =
2623 coap_new_bin_const((const uint8_t *)begin, end - begin);
2624 if (value->u.value_bin == NULL)
2625 goto bad_entry;
2626 break;
2627 case COAP_ENC_HEX:
2628 /* Parse the hex into binary */
2629 value->u.value_bin = parse_hex_bin(begin, end);
2630 if (value->u.value_bin == NULL)
2631 goto bad_entry;
2632 break;
2633#if COAP_OSCORE_GROUP_SUPPORT
2634 case COAP_ENC_PUB_FILE_PEM:
2635 /* Need NULL terminated file name */
2636 file_name = coap_new_string(end - begin);
2637 if (file_name == NULL)
2638 goto bad_file;
2639 memcpy(file_name->s, begin, end - begin);
2640 if (coap_crypto_read_pem_public_cred((const char *)file_name->s, &cred) ==
2641 0) {
2642 goto bad_file;
2643 }
2644 value->u.value_cred = cred;
2645 coap_delete_string(file_name);
2646 break;
2647 case COAP_ENC_PRI_FILE_PEM:
2648 /* Need NULL terminated file name */
2649 file_name = coap_new_string(end - begin);
2650 if (file_name == NULL)
2651 goto bad_file;
2652 memcpy(file_name->s, begin, end - begin);
2653 if (coap_crypto_read_pem_private_key((const char *)file_name->s,
2654 &pri_key) == 0) {
2655 goto bad_file;
2656 }
2657 value->u.value_pri = pri_key;
2658 coap_delete_string(file_name);
2659 break;
2660 case COAP_ENC_PUB_ASN1:
2661 /* Parse the hex into binary */
2662 binary = parse_hex_bin(begin, end);
2663 if (binary == NULL)
2664 goto bad_entry;
2665 /* binary alwayes gets deleted in coap_crypto_read_asn1_public_cred() */
2666 if (coap_crypto_read_asn1_public_cred(binary, &cred) == 0) {
2667 goto bad_entry;
2668 }
2669 value->u.value_cred = cred;
2670 break;
2671 case COAP_ENC_PRI_ASN1:
2672 /* Parse the hex into binary */
2673 binary = parse_hex_bin(begin, end);
2674 if (binary == NULL)
2675 goto bad_entry;
2676 /* binary alwayes gets deleted in coap_crypto_read_asn1_private_key() */
2677 if (coap_crypto_read_asn1_private_key(binary, &pri_key) == 0) {
2678 goto bad_entry;
2679 }
2680 value->u.value_pri = pri_key;
2681 break;
2682 case COAP_ENC_PUB_RAW:
2683 /* Parse the hex into binary */
2684 binary = parse_hex_bin(begin, end);
2685 if (binary == NULL)
2686 goto bad_entry;
2687 /* binary alwayes gets deleted in coap_crypto_read_raw_public_cred() */
2688 if (coap_crypto_read_raw_public_cred(sign_curve,
2689 binary,
2690 &cred) == 0) {
2691 goto bad_entry;
2692 }
2693 value->u.value_cred = cred;
2694 break;
2695 case COAP_ENC_PRI_RAW:
2696 if (sign_curve == 0)
2697 goto bad_order;
2698 /* Parse the hex into binary */
2699 binary = parse_hex_bin(begin, end);
2700 if (binary == NULL)
2701 goto bad_entry;
2702 asn1 = coap_asn1_pri_key(sign_curve, binary);
2703 coap_delete_bin_const(binary);
2704 if (asn1 == NULL)
2705 goto bad_entry;
2706 binary = (coap_bin_const_t *)asn1;
2707 /* binary alwayes gets deleted in coap_crypto_read_asn1_private_key() */
2708 if (coap_crypto_read_asn1_private_key(binary, &pri_key) == 0) {
2709 goto bad_entry;
2710 }
2711 value->u.value_pri = pri_key;
2712 break;
2713 case COAP_ENC_PUB_CCS:
2714 /* Parse the hex into binary */
2715 binary = parse_hex_bin(begin, end);
2716 if (binary == NULL)
2717 goto bad_entry;
2718 /* binary alwayes gets deleted in coap_crypto_read_ccs_public_cred() */
2719 if (coap_crypto_read_ccs_public_cred(binary,
2720 &cred) == 0) {
2721 goto bad_entry;
2722 }
2723 value->u.value_cred = cred;
2724 break;
2725#endif /* COAP_OSCORE_GROUP_SUPPORT */
2726 case COAP_ENC_INTEGER:
2727 value->u.value_int = atoi(begin);
2728 break;
2729 case COAP_ENC_UNSIGNED64: {
2730 value->u.value_int64 = strtoull(begin, NULL, 10);
2731 break;
2732 }
2733 case COAP_ENC_TEXT:
2734 value->u.value_str.s = (const uint8_t *)begin;
2735 value->u.value_str.length = end - begin;
2736 break;
2737 case COAP_ENC_BOOL:
2738 len = (size_t)(end - begin);
2739 if (len == 4 && memcmp("true", begin, len) == 0)
2740 value->u.value_int = 1;
2741 else if (len == 5 && memcmp("false", begin, len) == 0)
2742 value->u.value_int = 0;
2743 else
2744 goto bad_entry;
2745 break;
2746 case COAP_ENC_LAST:
2747 default:
2748 goto bad_entry;
2749 }
2750 return 1;
2751
2752bad_entry:
2753 coap_log_warn("oscore_conf: Unrecognized configuration entry '%.*s'\n",
2754 (int)(end - begin),
2755 begin);
2756 return -1;
2757
2758#if COAP_OSCORE_GROUP_SUPPORT
2759bad_file:
2760 if (file_name) {
2761 coap_log_warn("oscore_conf: Bad configuration file_pem entry '%.*s'\n",
2762 (int)file_name->length,
2763 file_name->s);
2764 coap_delete_string(file_name);
2765 }
2766 return 0;
2767bad_order:
2768 coap_log_warn("oscore_conf: RAW private key must be defined after public key\n");
2769 return 0;
2770#endif /* COAP_OSCORE_GROUP_SUPPORT */
2771}
2772
2773#undef CONFIG_ENTRY
2774#define CONFIG_ENTRY(n, e, t) \
2775 { { sizeof(#n)-1, (const uint8_t *)#n }, e, \
2776 offsetof(coap_oscore_conf_t, n), t }
2777
2778#undef CONFIG_SND_ENTRY
2779#define CONFIG_SND_ENTRY(n, e, t) \
2780 { { sizeof(#n)-1, (const uint8_t *)#n }, e, \
2781 offsetof(coap_oscore_snd_conf_t, n), t }
2782
2783#undef CONFIG_RCP_ENTRY
2784#define CONFIG_RCP_ENTRY(n, e, t) \
2785 { { sizeof(#n)-1, (const uint8_t *)#n }, e, \
2786 offsetof(coap_oscore_rcp_conf_t, n), t }
2787
2788#undef CONFIG_DUMMY_ENTRY
2789#define CONFIG_DUMMY_ENTRY(n, e, t) \
2790 { { sizeof(#n)-1, (const uint8_t *)#n }, e, 0, t }
2791
2792typedef struct oscore_text_mapping_t {
2793 coap_str_const_t text;
2794 int value;
2795} oscore_text_mapping_t;
2796
2797/* Naming as per https://www.iana.org/assignments/cose/cose.xhtml#algorithms */
2798static oscore_text_mapping_t text_aead_alg[] = {
2799 TEXT_MAPPING(AES-CCM-16-64-128, COSE_ALGORITHM_AES_CCM_16_64_128),
2800 TEXT_MAPPING(AES-CCM-16-64-256, COSE_ALGORITHM_AES_CCM_16_64_256),
2801 {{0, NULL}, 0}
2802};
2803
2804static oscore_text_mapping_t text_hkdf_alg[] = {
2805 TEXT_MAPPING(direct+HKDF-SHA-256, COSE_HKDF_ALG_HKDF_SHA_256),
2806 {{0, NULL}, 0}
2807};
2808
2809#if COAP_OSCORE_GROUP_SUPPORT
2810static oscore_text_mapping_t text_alg_signature[] = {
2811 TEXT_MAPPING(EdDSA, COSE_ALGORITHM_EDDSA),
2812 {{0, NULL}, 0}
2813};
2814
2815static oscore_text_mapping_t text_sign_curve[] = {
2816 TEXT_MAPPING(Ed25519, COSE_CURVE_ED25519),
2817 TEXT_MAPPING(X25519, COSE_CURVE_X25519),
2818 {{0, NULL}, 0}
2819};
2820
2821static oscore_text_mapping_t text_alg_pairwise[] = {
2822 TEXT_MAPPING(ECDH-SS + HKDF-256, COSE_ALGORITHM_ECDH_SS_HKDF_256),
2823 {{0, NULL}, 0}
2824};
2825
2826/*
2827static oscore_text_mapping_t text_auth_cred_fmt[] = {
2828 TEXT_MAPPING(CTW, OSCORE_AUTH_CRED_CWT),
2829 TEXT_MAPPING(CSS, OSCORE_AUTH_CRED_CCS),
2830 TEXT_MAPPING(X509, OSCORE_AUTH_CRED_X509),
2831 TEXT_MAPPING(C509, OSCORE_AUTH_CRED_C509),
2832 {{0, NULL}, 0}
2833};
2834*/
2835#endif /* COAP_OSCORE_GROUP_SUPPORT */
2836
2837typedef struct oscore_config_t {
2838 coap_str_const_t str_keyword;
2839 coap_oscore_coding_t encoding;
2840 size_t offset;
2841 oscore_text_mapping_t *text_mapping;
2842} oscore_config_t;
2843
2844static oscore_config_t oscore_config[] = {
2845 CONFIG_ENTRY(aead_alg, COAP_ENC_INTEGER | COAP_ENC_TEXT, text_aead_alg),
2846 CONFIG_ENTRY(hkdf_alg, COAP_ENC_INTEGER | COAP_ENC_TEXT, text_hkdf_alg),
2847 CONFIG_ENTRY(master_secret, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
2848 CONFIG_ENTRY(master_salt, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
2849 CONFIG_ENTRY(id_context, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
2850
2851 CONFIG_DUMMY_ENTRY(sender_id, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
2852
2853 CONFIG_DUMMY_ENTRY(recipient_id, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
2854
2855 CONFIG_ENTRY(replay_window, COAP_ENC_INTEGER, NULL),
2856 CONFIG_ENTRY(ssn_freq, COAP_ENC_INTEGER, NULL),
2857 CONFIG_ENTRY(rfc8613_b_1_2, COAP_ENC_BOOL, NULL),
2858 CONFIG_ENTRY(rfc8613_b_2, COAP_ENC_BOOL, NULL),
2859 CONFIG_ENTRY(break_sender_key, COAP_ENC_BOOL, NULL),
2860 CONFIG_ENTRY(break_recipient_key, COAP_ENC_BOOL, NULL),
2861 CONFIG_DUMMY_ENTRY(complex_sender, COAP_ENC_CONFIG, NULL),
2862 CONFIG_DUMMY_ENTRY(complex_recipient, COAP_ENC_CONFIG, NULL),
2863#if COAP_OSCORE_GROUP_SUPPORT
2864// CONFIG_ENTRY(auth_cred_fmt, COAP_ENC_INTEGER | COAP_ENC_TEXT, text_auth_cred_fmt),
2865// CONFIG_ENTRY(group_name, COAP_ENC_ASCII, NULL),
2866 CONFIG_DUMMY_ENTRY(complex_sender, COAP_ENC_CONFIG, NULL),
2867 CONFIG_DUMMY_ENTRY(complex_recipient, COAP_ENC_CONFIG, NULL),
2868 CONFIG_ENTRY(cred_fmt, COAP_ENC_INTEGER, NULL),
2869
2870 CONFIG_ENTRY(alg_group_enc, COAP_ENC_INTEGER | COAP_ENC_TEXT, text_aead_alg),
2871 CONFIG_ENTRY(alg_signature, COAP_ENC_INTEGER | COAP_ENC_TEXT, text_alg_signature),
2872 CONFIG_ENTRY(sign_curve, COAP_ENC_INTEGER | COAP_ENC_TEXT, text_sign_curve),
2873 /*CONFIG_ENTRY(sign_params, COAP_ENC_INTEGER | COAP_ENC_TEXT,
2874 text_alg_signature),*/
2875 CONFIG_ENTRY(ecdh_alg, COAP_ENC_INTEGER | COAP_ENC_TEXT, text_sign_curve),
2876 /*CONFIG_ENTRY(ecdh_params, COAP_ENC_INTEGER | COAP_ENC_TEXT,
2877 text_alg_signature),*/
2878 CONFIG_ENTRY(alg_pairwise_key_agreement, COAP_ENC_INTEGER | COAP_ENC_TEXT, text_alg_pairwise),
2879 CONFIG_ENTRY(
2880 gm_cred, COAP_ENC_PUB_ASN1 | COAP_ENC_PUB_FILE_PEM | COAP_ENC_PUB_CCS, NULL),
2881#endif /* COAP_OSCORE_GROUP_SUPPORT */
2882};
2883
2884/* As per draft-ietf-ace-oscore-gm-admin 5.1.1 to provide group support */
2885static oscore_config_t oscore_snd_config[] = {
2886 CONFIG_SND_ENTRY(sender_id, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
2887#if COAP_OSCORE_GROUP_SUPPORT
2888 CONFIG_SND_ENTRY(group_mode, COAP_ENC_BOOL, NULL),
2889 CONFIG_SND_ENTRY(pairwise_mode, COAP_ENC_BOOL, NULL),
2890 CONFIG_SND_ENTRY(
2891 gs_public_cred, COAP_ENC_PUB_ASN1 | COAP_ENC_PUB_FILE_PEM | COAP_ENC_PUB_CCS, NULL),
2892 CONFIG_SND_ENTRY(
2893 gs_private_key, COAP_ENC_PRI_ASN1 | COAP_ENC_PRI_FILE_PEM | COAP_ENC_PRI_RAW, NULL),
2894#endif /* COAP_OSCORE_GROUP_SUPPORT */
2895};
2896
2897static oscore_config_t oscore_rcp_config[] = {
2898 CONFIG_RCP_ENTRY(recipient_id, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
2899 CONFIG_RCP_ENTRY(silent_server, COAP_ENC_BOOL, NULL),
2900 CONFIG_DUMMY_ENTRY(last_seq, COAP_ENC_UNSIGNED64, NULL),
2901 CONFIG_DUMMY_ENTRY(sliding_window, COAP_ENC_UNSIGNED64, NULL),
2902#if COAP_OSCORE_GROUP_SUPPORT
2903 CONFIG_RCP_ENTRY(group_mode, COAP_ENC_BOOL, NULL),
2904 CONFIG_RCP_ENTRY(pairwise_mode, COAP_ENC_BOOL, NULL),
2905 CONFIG_RCP_ENTRY(silent_server, COAP_ENC_BOOL, NULL),
2906 CONFIG_RCP_ENTRY(
2907 gr_public_cred, COAP_ENC_PUB_ASN1 | COAP_ENC_PUB_FILE_PEM | COAP_ENC_PUB_CCS, NULL),
2908#endif /* COAP_OSCORE_GROUP_SUPPORT */
2909};
2910
2911int
2913 if (snd_conf == NULL)
2914 return 0;
2915
2917#if COAP_OSCORE_GROUP_SUPPORT
2918 coap_crypto_delete_public_cred(snd_conf->gs_public_cred);
2919 coap_crypto_delete_private_key(snd_conf->gs_private_key);
2920#endif /* COAP_OSCORE_GROUP_SUPPORT */
2921 coap_free_type(COAP_STRING, snd_conf);
2922 return 1;
2923}
2924
2925int
2927 if (rcp_conf == NULL)
2928 return 0;
2929
2931#if COAP_OSCORE_GROUP_SUPPORT
2932 coap_crypto_delete_public_cred(rcp_conf->gr_public_cred);
2933#endif /* COAP_OSCORE_GROUP_SUPPORT */
2934 coap_free_type(COAP_STRING, rcp_conf);
2935 return 1;
2936}
2937
2938int
2940 coap_oscore_rcp_conf_t *rcp_conf;
2941 if (oscore_conf == NULL)
2942 return 0;
2943
2945 coap_delete_bin_const(oscore_conf->master_salt);
2946 coap_delete_bin_const(oscore_conf->id_context);
2947 coap_delete_oscore_snd_conf(oscore_conf->sender);
2948
2949 rcp_conf = oscore_conf->recipient_chain;
2950 while (rcp_conf) {
2951 coap_oscore_rcp_conf_t *rcp_next = rcp_conf->next_recipient;
2952
2954 rcp_conf = rcp_next;
2955 }
2956#if COAP_OSCORE_GROUP_SUPPORT
2957 coap_crypto_delete_public_cred(oscore_conf->gm_cred);
2958#endif /* COAP_OSCORE_GROUP_SUPPORT */
2959
2960 coap_free_type(COAP_STRING, oscore_conf);
2961 return 1;
2962}
2963
2964static void
2965cleanup_value(oscore_value_t *value) {
2966 switch (value->encoding) {
2967 case COAP_ENC_ASCII:
2968 case COAP_ENC_HEX:
2969 case COAP_ENC_CONFIG:
2970 coap_delete_bin_const(value->u.value_bin);
2971 break;
2972#if COAP_OSCORE_GROUP_SUPPORT
2973 case COAP_ENC_PUB_RAW:
2974 case COAP_ENC_PUB_ASN1:
2975 case COAP_ENC_PUB_FILE_PEM:
2976 case COAP_ENC_PUB_CCS:
2977 coap_crypto_delete_public_cred(value->u.value_cred);
2978 break;
2979 case COAP_ENC_PRI_RAW:
2980 case COAP_ENC_PRI_ASN1:
2981 case COAP_ENC_PRI_FILE_PEM:
2982 coap_crypto_delete_private_key(value->u.value_pri);
2983 break;
2984#endif /* COAP_OSCORE_GROUP_SUPPORT */
2985 case COAP_ENC_INTEGER:
2986 case COAP_ENC_TEXT:
2987 case COAP_ENC_BOOL:
2988 case COAP_ENC_UNSIGNED64:
2989 case COAP_ENC_LAST:
2990 default:
2991 break;
2992 }
2993}
2994
2996coap_parse_oscore_snd_conf_mem(coap_bin_const_t conf_mem) {
2997 const char *start = (const char *)conf_mem.s;
2998 const char *end = start + conf_mem.length;
2999 coap_str_const_t keyword;
3000 oscore_value_t value;
3001 coap_oscore_snd_conf_t *snd_conf;
3002
3004 if (snd_conf == NULL)
3005 return NULL;
3006 memset(snd_conf, 0, sizeof(coap_oscore_snd_conf_t));
3007
3008 memset(&value, 0, sizeof(value));
3009
3010#if COAP_OSCORE_GROUP_SUPPORT
3011 while (end > start &&
3012 get_split_entry(&start, end - start, &keyword, &value,
3013 snd_conf->gs_public_cred ? snd_conf->gs_public_cred->sign_curve : 0) > 0)
3014#else /* ! COAP_OSCORE_GROUP_SUPPORT */
3015 while (end > start &&
3016 get_split_entry(&start, end - start, &keyword, &value, 0) > 0)
3017#endif /* ! COAP_OSCORE_GROUP_SUPPORT */
3018 {
3019 size_t i;
3020 size_t j;
3021
3022 for (i = 0; i < sizeof(oscore_snd_config) / sizeof(oscore_snd_config[0]); i++) {
3023 if (coap_string_equal(&oscore_snd_config[i].str_keyword, &keyword) != 0 &&
3024 value.encoding & oscore_snd_config[i].encoding) {
3025 coap_bin_const_t *unused_check;
3026
3027 switch (value.encoding) {
3028 case COAP_ENC_HEX:
3029 case COAP_ENC_ASCII:
3030 memcpy(&unused_check,
3031 &(((char *)snd_conf)[oscore_snd_config[i].offset]),
3032 sizeof(unused_check));
3033 if (unused_check != NULL) {
3034 coap_log_warn("oscore_snd_conf: Keyword '%.*s' duplicated\n",
3035 (int)keyword.length,
3036 (const char *)keyword.s);
3037 coap_delete_bin_const(value.u.value_bin);
3038 goto error;
3039 }
3040 memcpy(&(((char *)snd_conf)[oscore_snd_config[i].offset]),
3041 &value.u.value_bin,
3042 sizeof(value.u.value_bin));
3043 break;
3044#if COAP_OSCORE_GROUP_SUPPORT
3045 case COAP_ENC_PUB_RAW:
3046 case COAP_ENC_PUB_ASN1:
3047 case COAP_ENC_PUB_FILE_PEM:
3048 case COAP_ENC_PUB_CCS:
3049 memcpy(&unused_check,
3050 &(((char *)snd_conf)[oscore_snd_config[i].offset]),
3051 sizeof(unused_check));
3052 if (unused_check != NULL) {
3053 coap_log_warn("oscore_snd_conf: Keyword '%.*s' duplicated\n",
3054 (int)keyword.length,
3055 (const char *)keyword.s);
3056 cleanup_value(&value);
3057 goto error;
3058 }
3059 memcpy(&(((char *)snd_conf)[oscore_snd_config[i].offset]),
3060 &value.u.value_cred,
3061 sizeof(value.u.value_cred));
3062 break;
3063 case COAP_ENC_PRI_RAW:
3064 case COAP_ENC_PRI_ASN1:
3065 case COAP_ENC_PRI_FILE_PEM:
3066 memcpy(&unused_check,
3067 &(((char *)snd_conf)[oscore_snd_config[i].offset]),
3068 sizeof(unused_check));
3069 if (unused_check != NULL) {
3070 coap_log_warn("oscore_snd_conf: Keyword '%.*s' duplicated\n",
3071 (int)keyword.length,
3072 (const char *)keyword.s);
3073 cleanup_value(&value);
3074 goto error;
3075 }
3076 memcpy(&(((char *)snd_conf)[oscore_snd_config[i].offset]),
3077 &value.u.value_pri,
3078 sizeof(value.u.value_pri));
3079 break;
3080#endif /* COAP_OSCORE_GROUP_SUPPORT */
3081 case COAP_ENC_INTEGER:
3082 case COAP_ENC_BOOL:
3083 memcpy(&(((char *)snd_conf)[oscore_snd_config[i].offset]),
3084 &value.u.value_int,
3085 sizeof(value.u.value_int));
3086 break;
3087 case COAP_ENC_TEXT:
3088 for (j = 0; oscore_snd_config[i].text_mapping[j].text.s != NULL; j++) {
3089 if (memcmp(value.u.value_str.s,
3090 oscore_snd_config[i].text_mapping[j].text.s,
3091 value.u.value_str.length) == 0) {
3092 memcpy(&(((char *)snd_conf)[oscore_snd_config[i].offset]),
3093 &oscore_snd_config[i].text_mapping[j].value,
3094 sizeof(oscore_snd_config[i].text_mapping[j].value));
3095 break;
3096 }
3097 }
3098 if (oscore_snd_config[i].text_mapping[j].text.s == NULL) {
3099 coap_log_warn("oscore_snd_conf: Keyword '%.*s': value '%.*s' unknown\n",
3100 (int)keyword.length,
3101 (const char *)keyword.s,
3102 (int)value.u.value_str.length,
3103 (const char *)value.u.value_str.s);
3104 goto error;
3105 }
3106 break;
3107 case COAP_ENC_CONFIG:
3108 case COAP_ENC_UNSIGNED64:
3109 case COAP_ENC_LAST:
3110 default:
3111 assert(0);
3112 break;
3113 }
3114 break;
3115 }
3116 }
3117 if (i == sizeof(oscore_snd_config) / sizeof(oscore_snd_config[0])) {
3118 coap_log_warn("oscore_snd_conf: Keyword '%.*s', type '%s' unknown\n",
3119 (int)keyword.length,
3120 (const char *)keyword.s,
3121 value.encoding_name);
3122 cleanup_value(&value);
3123 goto error;
3124 }
3125 }
3126 if (!snd_conf->sender_id) {
3127 coap_log_warn("oscore_snd_conf: sender_id not defined\n");
3128 goto error;
3129 }
3130 if (snd_conf->sender_id->length > 7) {
3131 coap_log_warn("oscore_snd_conf: Maximum size of sender_id is 7 bytes\n");
3132 goto error;
3133 }
3134#if COAP_OSCORE_GROUP_SUPPORT
3135 if ((snd_conf->group_mode || snd_conf->pairwise_mode) &&
3136 (!snd_conf->gs_public_cred || !snd_conf->gs_private_key)) {
3137 coap_log_warn("oscore_snd_conf: group: One or more of"
3138 " gs_public_cred and gs_private_key not defined\n");
3139 goto error;
3140 }
3141#endif /* COAP_OSCORE_GROUP_SUPPORT */
3142 return snd_conf;
3143
3144error:
3146 return NULL;
3147}
3148
3150coap_parse_oscore_rcp_conf_mem(coap_bin_const_t conf_mem) {
3151 const char *start = (const char *)conf_mem.s;
3152 const char *end = start + conf_mem.length;
3153 coap_str_const_t keyword;
3154 oscore_value_t value;
3155 coap_oscore_rcp_conf_t *rcp_conf;
3156
3158 if (rcp_conf == NULL)
3159 return NULL;
3160 memset(rcp_conf, 0, sizeof(coap_oscore_rcp_conf_t));
3161
3162 memset(&value, 0, sizeof(value));
3163#if COAP_OSCORE_GROUP_SUPPORT
3164 /* Preset with defaults */
3165 rcp_conf->sign_curve = COSE_CURVE_ED25519;
3166#endif /* COAP_OSCORE_GROUP_SUPPORT */
3167
3168#if COAP_OSCORE_GROUP_SUPPORT
3169 while (end > start &&
3170 get_split_entry(&start, end - start, &keyword, &value, rcp_conf->sign_curve) > 0)
3171#else /* !COAP_OSCORE_GROUP_SUPPORT */
3172 while (end > start &&
3173 get_split_entry(&start, end - start, &keyword, &value, 0) > 0)
3174#endif /* !COAP_OSCORE_GROUP_SUPPORT */
3175 {
3176 size_t i;
3177 size_t j;
3178
3179 for (i = 0; i < sizeof(oscore_rcp_config) / sizeof(oscore_rcp_config[0]); i++) {
3180 if (coap_string_equal(&oscore_rcp_config[i].str_keyword, &keyword) != 0 &&
3181 value.encoding & oscore_rcp_config[i].encoding) {
3182 if (coap_string_equal(coap_make_str_const("last_seq"), &keyword)) {
3183 rcp_conf->last_seq = value.u.value_int64;
3184 rcp_conf->window_initialized = 1;
3185 } else if (coap_string_equal(coap_make_str_const("sliding_window"), &keyword)) {
3186 rcp_conf->sliding_window = value.u.value_int64;
3187 rcp_conf->window_initialized = 1;
3188 } else {
3189 coap_bin_const_t *unused_check;
3190
3191 switch (value.encoding) {
3192 case COAP_ENC_HEX:
3193 case COAP_ENC_ASCII:
3194 memcpy(&unused_check,
3195 &(((char *)rcp_conf)[oscore_rcp_config[i].offset]),
3196 sizeof(unused_check));
3197 if (unused_check != NULL) {
3198 coap_log_warn("oscore_rcp_conf: Keyword '%.*s' duplicated\n",
3199 (int)keyword.length,
3200 (const char *)keyword.s);
3201 coap_delete_bin_const(value.u.value_bin);
3202 goto error;
3203 }
3204 memcpy(&(((char *)rcp_conf)[oscore_rcp_config[i].offset]),
3205 &value.u.value_bin,
3206 sizeof(value.u.value_bin));
3207 break;
3208#if COAP_OSCORE_GROUP_SUPPORT
3209 case COAP_ENC_PUB_RAW:
3210 case COAP_ENC_PUB_ASN1:
3211 case COAP_ENC_PUB_FILE_PEM:
3212 case COAP_ENC_PUB_CCS:
3213 memcpy(&unused_check,
3214 &(((char *)rcp_conf)[oscore_rcp_config[i].offset]),
3215 sizeof(unused_check));
3216 if (unused_check != NULL) {
3217 coap_log_warn("oscore_rcp_conf: Keyword '%.*s' duplicated\n",
3218 (int)keyword.length,
3219 (const char *)keyword.s);
3220 cleanup_value(&value);
3221 goto error;
3222 }
3223 memcpy(&(((char *)rcp_conf)[oscore_rcp_config[i].offset]),
3224 &value.u.value_cred,
3225 sizeof(value.u.value_cred));
3226 break;
3227 case COAP_ENC_PRI_RAW:
3228 case COAP_ENC_PRI_ASN1:
3229 case COAP_ENC_PRI_FILE_PEM:
3230 memcpy(&unused_check,
3231 &(((char *)rcp_conf)[oscore_rcp_config[i].offset]),
3232 sizeof(unused_check));
3233 if (unused_check != NULL) {
3234 coap_log_warn("oscore_rcp_conf: Keyword '%.*s' duplicated\n",
3235 (int)keyword.length,
3236 (const char *)keyword.s);
3237 cleanup_value(&value);
3238 goto error;
3239 }
3240 memcpy(&(((char *)rcp_conf)[oscore_rcp_config[i].offset]),
3241 &value.u.value_pri,
3242 sizeof(value.u.value_pri));
3243 break;
3244#endif /* COAP_OSCORE_GROUP_SUPPORT */
3245 case COAP_ENC_INTEGER:
3246 case COAP_ENC_BOOL:
3247 memcpy(&(((char *)rcp_conf)[oscore_rcp_config[i].offset]),
3248 &value.u.value_int,
3249 sizeof(value.u.value_int));
3250 break;
3251 case COAP_ENC_TEXT:
3252 for (j = 0; oscore_rcp_config[i].text_mapping[j].text.s != NULL; j++) {
3253 if (memcmp(value.u.value_str.s,
3254 oscore_rcp_config[i].text_mapping[j].text.s,
3255 value.u.value_str.length) == 0) {
3256 memcpy(&(((char *)rcp_conf)[oscore_rcp_config[i].offset]),
3257 &oscore_rcp_config[i].text_mapping[j].value,
3258 sizeof(oscore_rcp_config[i].text_mapping[j].value));
3259 break;
3260 }
3261 }
3262 if (oscore_rcp_config[i].text_mapping[j].text.s == NULL) {
3263 coap_log_warn("oscore_rcp_conf: Keyword '%.*s': value '%.*s' unknown\n",
3264 (int)keyword.length,
3265 (const char *)keyword.s,
3266 (int)value.u.value_str.length,
3267 (const char *)value.u.value_str.s);
3268 goto error;
3269 }
3270 break;
3271 case COAP_ENC_CONFIG:
3272 case COAP_ENC_UNSIGNED64:
3273 case COAP_ENC_LAST:
3274 default:
3275 assert(0);
3276 break;
3277 }
3278 }
3279 break;
3280 }
3281 }
3282 if (i == sizeof(oscore_rcp_config) / sizeof(oscore_rcp_config[0])) {
3283 coap_log_warn("oscore_rcp_conf: Keyword '%.*s', type '%s' unknown\n",
3284 (int)keyword.length,
3285 (const char *)keyword.s,
3286 value.encoding_name);
3287 cleanup_value(&value);
3288 goto error;
3289 }
3290 }
3291 if (!rcp_conf->recipient_id) {
3292 coap_log_warn("oscore_rcp_conf: recipient_id not defined\n");
3293 goto error;
3294 }
3295 if (rcp_conf->recipient_id->length > 7) {
3296 coap_log_warn("oscore_rcp_conf: Maximum size of recipient_id is 7 bytes\n");
3297 goto error;
3298 }
3299#if COAP_OSCORE_GROUP_SUPPORT
3300 if ((rcp_conf->group_mode || rcp_conf->pairwise_mode) &&
3301 !rcp_conf->gr_public_cred) {
3302 coap_log_warn("oscore_rcp_conf: group: gr_public_cred not defined\n");
3303 goto error;
3304 }
3305#endif /* COAP_OSCORE_GROUP_SUPPORT */
3306 return rcp_conf;
3307
3308error:
3310 return NULL;
3311}
3312
3313static coap_oscore_conf_t *
3314coap_parse_oscore_conf_mem(coap_str_const_t conf_mem) {
3315 const char *start = (const char *)conf_mem.s;
3316 const char *end = start + conf_mem.length;
3317 coap_str_const_t keyword;
3318 oscore_value_t value;
3319 coap_oscore_conf_t *oscore_conf;
3320 int split_ok = -1;
3321
3322 oscore_conf = coap_malloc_type(COAP_STRING, sizeof(coap_oscore_conf_t));
3323 if (oscore_conf == NULL)
3324 return NULL;
3325 memset(oscore_conf, 0, sizeof(coap_oscore_conf_t));
3326
3327 memset(&value, 0, sizeof(value));
3328 /* Preset with defaults RFC8612 3.2 */
3330 oscore_conf->ssn_freq = 1;
3332 oscore_conf->hkdf_alg = COSE_HKDF_ALG_HKDF_SHA_256;
3333
3334 oscore_conf->rfc8613_b_1_2 = 1;
3335 oscore_conf->rfc8613_b_2 = 0;
3336#if COAP_OSCORE_GROUP_SUPPORT
3337 oscore_conf->ecdh_alg = COSE_CURVE_ED25519;
3338 oscore_conf->alg_group_enc = COSE_ALGORITHM_AES_CCM_16_64_128;
3339 oscore_conf->alg_signature = COSE_ALGORITHM_EDDSA;
3340 oscore_conf->sign_curve = COSE_CURVE_ED25519;
3341 oscore_conf->alg_pairwise_key_agreement = COSE_ALGORITHM_ECDH_SS_HKDF_256;
3342#endif /* COAP_OSCORE_GROUP_SUPPORT */
3343 /* For debugging only */
3344 oscore_conf->break_sender_key = 0;
3345 oscore_conf->break_recipient_key = 0;
3346
3347#if COAP_OSCORE_GROUP_SUPPORT
3348 while (end > start &&
3349 (split_ok = get_split_entry(&start, end - start, &keyword, &value, oscore_conf->sign_curve)) > 0)
3350#else /* ! COAP_OSCORE_GROUP_SUPPORT */
3351 while (end > start &&
3352 (split_ok = get_split_entry(&start, end - start, &keyword, &value, 0)) > 0)
3353#endif /* ! COAP_OSCORE_GROUP_SUPPORT */
3354 {
3355 size_t i;
3356 size_t j;
3357
3358 for (i = 0; i < sizeof(oscore_config) / sizeof(oscore_config[0]); i++) {
3359 if (coap_string_equal(&oscore_config[i].str_keyword, &keyword) != 0 &&
3360 value.encoding & oscore_config[i].encoding) {
3361 if (coap_string_equal(coap_make_str_const("sender_id"), &keyword)) {
3362 if (value.u.value_bin->length > 7) {
3363 coap_log_warn("oscore_conf: Maximum size of sender_id is 7 bytes\n");
3364 goto error_free_value;
3365 }
3366 if (oscore_conf->sender) {
3367 coap_log_warn("oscore_conf: sender_id duplicated\n");
3368 goto error_free_value;
3369 }
3371 if (!oscore_conf->sender)
3372 goto error_free_value;
3373
3374 memset(oscore_conf->sender, 0, sizeof(coap_oscore_snd_conf_t));
3375 oscore_conf->sender->sender_id = value.u.value_bin;
3376 } else if (coap_string_equal(coap_make_str_const("recipient_id"), &keyword)) {
3377 coap_oscore_rcp_conf_t *rcp_conf;
3378
3379 if (value.u.value_bin->length > 7) {
3380 coap_log_warn("oscore_conf: Maximum size of recipient_id is 7 bytes\n");
3381 goto error_free_value;
3382 }
3383 /* Special case as there are potentially multiple entries */
3385 if (!rcp_conf)
3386 goto error_free_value;
3387
3388 memset(rcp_conf, 0, sizeof(coap_oscore_rcp_conf_t));
3389 rcp_conf->recipient_id = value.u.value_bin;
3390 rcp_conf->next_recipient = oscore_conf->recipient_chain;
3391 oscore_conf->recipient_chain = rcp_conf;
3392 } else {
3393 coap_bin_const_t *unused_check;
3394
3395 switch (value.encoding) {
3396 case COAP_ENC_CONFIG:
3397 if (keyword.length == sizeof("complex_sender") - 1 &&
3398 memcmp(keyword.s, "complex_sender", keyword.length) == 0) {
3399 coap_oscore_snd_conf_t *snd_conf =
3400 coap_parse_oscore_snd_conf_mem(*value.u.value_bin);
3401
3402 if (!snd_conf) {
3403 coap_log_warn("oscore_conf: Keyword '%.*s' invalid\n",
3404 (int)keyword.length,
3405 (const char *)keyword.s);
3406 goto error_free_value;
3407 }
3408#if COAP_OSCORE_GROUP_SUPPORT
3409 if (snd_conf->group_mode || snd_conf->pairwise_mode) {
3410 if (oscore_conf->cred_type != 0 &&
3411 oscore_conf->cred_type != snd_conf->gs_public_cred->cred_type) {
3412 coap_log_warn("oscore_conf: cred type different\n");
3413 goto error_free_value;
3414 } else {
3415 oscore_conf->cred_type = snd_conf->gs_public_cred->cred_type;
3416 }
3417 }
3418#endif /* COAP_OSCORE_GROUP_SUPPORT */
3419 if (oscore_conf->sender) {
3420 coap_log_warn("oscore_conf: complex_sender duplicated\n");
3422 goto error_free_value;
3423 }
3424 oscore_conf->sender = snd_conf;
3425 coap_delete_bin_const(value.u.value_bin);
3426 break;
3427 }
3428 if (keyword.length == sizeof("complex_recipient") - 1 &&
3429 memcmp(keyword.s, "complex_recipient", keyword.length) == 0) {
3430 coap_oscore_rcp_conf_t *rcp_conf =
3431 coap_parse_oscore_rcp_conf_mem(*value.u.value_bin);
3432
3433 if (!rcp_conf) {
3434 coap_log_warn("oscore_conf: Keyword '%.*s' invalid\n",
3435 (int)keyword.length,
3436 (const char *)keyword.s);
3437 goto error_free_value;
3438 }
3439#if COAP_OSCORE_GROUP_SUPPORT
3440 if (rcp_conf->group_mode || rcp_conf->pairwise_mode) {
3441 if (oscore_conf->cred_type != 0 &&
3442 oscore_conf->cred_type != rcp_conf->gr_public_cred->cred_type) {
3443 coap_log_warn("oscore_conf: cred type different\n");
3444 goto error_free_value;
3445 } else {
3446 oscore_conf->cred_type = rcp_conf->gr_public_cred->cred_type;
3447 }
3448 }
3449#endif /* COAP_OSCORE_GROUP_SUPPORT */
3450 rcp_conf->next_recipient = oscore_conf->recipient_chain;
3451 oscore_conf->recipient_chain = rcp_conf;
3452 coap_delete_bin_const(value.u.value_bin);
3453 }
3454 break;
3455 case COAP_ENC_ASCII:
3456 case COAP_ENC_HEX:
3457 memcpy(&unused_check,
3458 &(((char *)oscore_conf)[oscore_config[i].offset]),
3459 sizeof(unused_check));
3460 if (unused_check != NULL) {
3461 coap_log_warn("oscore_conf: Keyword '%.*s' duplicated\n",
3462 (int)keyword.length,
3463 (const char *)keyword.s);
3464 goto error_free_value;
3465 }
3466 memcpy(&(((char *)oscore_conf)[oscore_config[i].offset]),
3467 &value.u.value_bin,
3468 sizeof(value.u.value_bin));
3469 break;
3470#if COAP_OSCORE_GROUP_SUPPORT
3471 case COAP_ENC_PUB_RAW:
3472 case COAP_ENC_PUB_ASN1:
3473 case COAP_ENC_PUB_FILE_PEM:
3474 case COAP_ENC_PUB_CCS:
3475 memcpy(&unused_check,
3476 &(((char *)oscore_conf)[oscore_config[i].offset]),
3477 sizeof(unused_check));
3478 if (unused_check != NULL) {
3479 coap_log_warn("oscore_conf: Keyword '%.*s' duplicated\n",
3480 (int)keyword.length,
3481 (const char *)keyword.s);
3482 cleanup_value(&value);
3483 goto error;
3484 }
3485 memcpy(&(((char *)oscore_conf)[oscore_config[i].offset]),
3486 &value.u.value_cred,
3487 sizeof(value.u.value_cred));
3488 break;
3489 case COAP_ENC_PRI_RAW:
3490 case COAP_ENC_PRI_ASN1:
3491 case COAP_ENC_PRI_FILE_PEM:
3492 memcpy(&unused_check,
3493 &(((char *)oscore_conf)[oscore_config[i].offset]),
3494 sizeof(unused_check));
3495 if (unused_check != NULL) {
3496 coap_log_warn("oscore_conf: Keyword '%.*s' duplicated\n",
3497 (int)keyword.length,
3498 (const char *)keyword.s);
3499 cleanup_value(&value);
3500 goto error;
3501 }
3502 memcpy(&(((char *)oscore_conf)[oscore_config[i].offset]),
3503 &value.u.value_pri,
3504 sizeof(value.u.value_pri));
3505 break;
3506#endif /* COAP_OSCORE_GROUP_SUPPORT */
3507 case COAP_ENC_INTEGER:
3508 case COAP_ENC_BOOL:
3509 memcpy(&(((char *)oscore_conf)[oscore_config[i].offset]),
3510 &value.u.value_int,
3511 sizeof(value.u.value_int));
3512 break;
3513 case COAP_ENC_TEXT:
3514 for (j = 0; oscore_config[i].text_mapping[j].text.s != NULL; j++) {
3515 if (coap_string_equal(&value.u.value_str,
3516 &oscore_config[i].text_mapping[j].text)) {
3517 memcpy(&(((char *)oscore_conf)[oscore_config[i].offset]),
3518 &oscore_config[i].text_mapping[j].value,
3519 sizeof(oscore_config[i].text_mapping[j].value));
3520 break;
3521 }
3522 }
3523 if (oscore_config[i].text_mapping[j].text.s == NULL) {
3524 coap_log_warn("oscore_conf: Keyword '%.*s': value '%.*s' unknown\n",
3525 (int)keyword.length,
3526 (const char *)keyword.s,
3527 (int)value.u.value_str.length,
3528 (const char *)value.u.value_str.s);
3529 goto error;
3530 }
3531 break;
3532 case COAP_ENC_UNSIGNED64:
3533 case COAP_ENC_LAST:
3534 default:
3535 assert(0);
3536 break;
3537 }
3538 }
3539 break;
3540 }
3541 }
3542 if (i == sizeof(oscore_config) / sizeof(oscore_config[0])) {
3543 coap_log_warn("oscore_conf: Keyword '%.*s', type '%s' unknown\n",
3544 (int)keyword.length,
3545 (const char *)keyword.s,
3546 value.encoding_name);
3547error_free_value:
3548 cleanup_value(&value);
3549 goto error;
3550 }
3551 }
3552 if (split_ok == -1)
3553 goto error;
3554 if (!oscore_conf->master_secret) {
3555 coap_log_warn("oscore_conf: master_secret not defined\n");
3556 goto error;
3557 }
3558 if (!oscore_conf->sender) {
3559 coap_log_warn("oscore_conf: sender_id not defined\n");
3560 goto error;
3561 }
3562 return oscore_conf;
3563
3564error:
3565 coap_delete_oscore_conf(oscore_conf);
3566 return NULL;
3567}
3568
3569static oscore_ctx_t *
3570coap_oscore_init(coap_context_t *c_context, coap_oscore_conf_t *oscore_conf) {
3571 oscore_ctx_t *osc_ctx = NULL;
3572
3573 if (!coap_crypto_check_cipher_alg(oscore_conf->aead_alg)) {
3574 coap_log_warn("COSE: Cipher Algorithm %d not supported\n",
3575 oscore_conf->aead_alg);
3576 goto error;
3577 }
3578 if (!coap_crypto_check_hkdf_alg(oscore_conf->hkdf_alg)) {
3579 coap_log_warn("COSE: HKDF Algorithm %d not supported\n",
3580 oscore_conf->hkdf_alg);
3581 goto error;
3582 }
3583#if COAP_OSCORE_GROUP_SUPPORT
3584 if (!coap_crypto_check_cipher_alg(oscore_conf->alg_group_enc)) {
3585 coap_log_warn("COSE: Group Encryption Algorithm %d not supported\n",
3586 oscore_conf->alg_group_enc);
3587 goto error;
3588 }
3589 /* TODO Check other algs */
3590#endif /* COAP_OSCORE_GROUP_SUPPORT */
3591
3592 osc_ctx = oscore_derive_ctx(c_context, oscore_conf);
3593 if (!osc_ctx) {
3594 coap_log_crit("OSCORE: Could not create Security Context!\n");
3595 coap_delete_oscore_conf(oscore_conf);
3596 return NULL;
3597 }
3598
3599 /* As all is stored in osc_ctx, oscore_conf is no longer needed */
3600 coap_free_type(COAP_STRING, oscore_conf);
3601
3602 /* return default first context */
3603 return osc_ctx;
3604
3605error:
3606 /* Remove from linked chain */
3607 oscore_remove_context(c_context, osc_ctx);
3608
3609 coap_delete_oscore_conf(oscore_conf);
3610 return NULL;
3611}
3612
3613void
3615 oscore_free_contexts(c_context);
3616}
3617
3618void
3621}
3622
3623void
3625 oscore_recipient_ctx_t *recipient_ctx) {
3626 oscore_recipient_ctx_t *old_ctx;
3627
3628 if (session == NULL || recipient_ctx == NULL)
3629 return;
3630
3631 if (session->recipient_ctx == recipient_ctx) {
3632 /* already attached */
3633 return;
3634 }
3635
3636 old_ctx = session->recipient_ctx;
3637
3638 if (old_ctx != NULL) {
3641 session);
3642 }
3643
3644 /* attach recipient context */
3645 session->recipient_ctx = recipient_ctx;
3646 session->recipient_ctx->ref++;
3647
3648 if (old_ctx != NULL) {
3650 }
3651}
3652
3653void
3655 oscore_recipient_ctx_t *recipient_ctx) {
3656 oscore_recipient_ctx_t *old_ctx;
3657
3658 if (association == NULL)
3659 return;
3660
3661 if (association->recipient_ctx == recipient_ctx)
3662 return;
3663
3664 old_ctx = association->recipient_ctx;
3665 association->recipient_ctx = recipient_ctx;
3666
3667 if (recipient_ctx != NULL)
3668 oscore_reference_recipient_ctx(recipient_ctx);
3669
3670 if (old_ctx != NULL)
3672}
3673
3676 coap_oscore_save_seq_num_t save_seq_num_func,
3677 void *save_seq_num_func_param,
3678 uint64_t start_seq_num) {
3679 coap_oscore_conf_t *oscore_conf = coap_parse_oscore_conf_mem(conf_mem);
3680
3681 if (oscore_conf == NULL)
3682 return NULL;
3683
3684 oscore_conf->save_seq_num_func = save_seq_num_func;
3685 oscore_conf->save_seq_num_func_param = save_seq_num_func_param;
3686 oscore_conf->start_seq_num = start_seq_num;
3687 coap_log_oscore("Start Seq no %" PRIu64 "\n", start_seq_num);
3688 return oscore_conf;
3689}
3690
3691/*
3692 * Compute the size of the potential OSCORE overhead
3693 */
3694size_t
3696 size_t overhead = 0;
3697 oscore_recipient_ctx_t *rcp_ctx = session->recipient_ctx;
3698 oscore_ctx_t *osc_ctx = rcp_ctx ? rcp_ctx->osc_ctx : NULL;
3699 coap_opt_iterator_t opt_iter;
3700 coap_opt_t *option;
3701
3702 if (osc_ctx == NULL)
3703 return 0;
3704
3705 /* Protected code held in inner PDU as token */
3706 overhead += 1;
3707
3708 /* Observe option (creates inner and outer */
3709 option = coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter);
3710 if (option) {
3711 /* Assume delta is small */
3712 overhead += 2 + coap_opt_length(option);
3713 }
3714
3715 /* Proxy URI option Split - covered by coap_rebuild_pdu_for_proxy () */
3716
3717 /* OSCORE option */
3718 /* Option header */
3719 overhead += 1 +
3720 /* Partial IV (64 bits max)*/
3721 8 +
3722 /* kid context */
3723 (osc_ctx->id_context ? osc_ctx->id_context->length : 0) +
3724 /* kid */
3725 osc_ctx->sender_context->sender_id->length;
3726
3727 /* AAD overhead */
3728 overhead += AES_CCM_TAG;
3729
3730 /* Extended Token Overhead */
3731 if (session->max_token_size)
3732 overhead += session->max_token_size - pdu->actual_token.length;
3733 /* Signing Overhead */
3734#if COAP_OSCORE_GROUP_SUPPORT
3735 if (rcp_ctx && rcp_ctx->mode != OSCORE_MODE_SINGLE)
3736 overhead += osc_ctx->sender_context->sender_cred->wire_sign_size;
3737#endif /* COAP_OSCORE_GROUP_SUPPORT */
3738
3739 /* End of options marker */
3740 overhead += 1;
3741
3742 return overhead;
3743}
3744
3745COAP_API int
3747 coap_bin_const_t *recipient_id) {
3748 int ret;
3749
3750 coap_lock_lock(return 0);
3751 ret = coap_new_oscore_recipient_lkd(context, recipient_id);
3753 return ret;
3754}
3755
3756int
3758 coap_bin_const_t *recipient_id) {
3759 coap_oscore_rcp_conf_t *rcp_conf;
3760 oscore_recipient_ctx_t *rcp_ctx;
3761
3763
3764 if (context->p_osc_ctx == NULL) {
3765 coap_delete_bin_const(recipient_id);
3766 return 0;
3767 }
3769 if (rcp_conf == NULL) {
3770 coap_delete_bin_const(recipient_id);
3771 return 0;
3772 }
3773 memset(rcp_conf, 0, sizeof(coap_oscore_rcp_conf_t));
3774 rcp_conf->recipient_id = recipient_id;
3775 /* rcp_conf is released in oscore_add_recipient() */
3776 rcp_ctx = oscore_add_recipient(context->p_osc_ctx, rcp_conf, 0);
3777 if (rcp_ctx == NULL)
3778 return 0;
3779 return 1;
3780}
3781
3782COAP_API int
3784 coap_bin_const_t *recipient_id) {
3785 int ret;
3786
3787 if (!context || !recipient_id)
3788 return 0;
3789 coap_lock_lock(return 0);
3790 ret = coap_delete_oscore_recipient_lkd(context, recipient_id);
3792 return ret;
3793}
3794
3795int
3797 coap_bin_const_t *recipient_id) {
3799 if (context->p_osc_ctx == NULL)
3800 return 0;
3801 return oscore_delete_recipient(context->p_osc_ctx, recipient_id);
3802}
3803
3804void
3806 coap_oscore_find_handler_t find_handler,
3807 coap_oscore_update_seq_num_handler_t update_seq_num_handler) {
3808 if (context == NULL)
3809 return;
3810
3811 coap_lock_lock(return);
3812 context->oscore_find_cb = find_handler;
3813 context->oscore_update_seq_num_cb = update_seq_num_handler;
3815}
3816
3818coap_oscore_get_recipient_conf(coap_oscore_conf_t *oscore_conf,
3819 const coap_bin_const_t *rid) {
3820 coap_oscore_rcp_conf_t *next = oscore_conf->recipient_chain;
3821
3822 while (next) {
3823 if (coap_binary_equal(next->recipient_id, rid)) {
3824 return next;
3825 }
3826 next = next->next_recipient;
3827 }
3828 return NULL;
3829}
3830
3831
3832COAP_API int
3834 const coap_bin_const_t *recipient_id,
3835 uint64_t last_seq, uint64_t seq_window) {
3836 int ret;
3837
3838 coap_lock_lock(return 0);
3839 ret = coap_oscore_recipient_set_latest_seq_lkd(oscore_conf, recipient_id, last_seq,
3840 seq_window);
3842 return ret;
3843}
3844
3845int
3847 const coap_bin_const_t *recipient_id,
3848 uint64_t last_seq, uint64_t seq_window) {
3849 coap_oscore_rcp_conf_t *rcp_conf;
3850
3851 rcp_conf = coap_oscore_get_recipient_conf(oscore_conf, recipient_id);
3852 if (rcp_conf) {
3853 rcp_conf->last_seq = last_seq;
3854 rcp_conf->sliding_window = seq_window;
3855 if (last_seq > 0) {
3856 rcp_conf->window_initialized = 1;
3857 }
3858 return 1;
3859 }
3860 return 0;
3861}
3862
3864
3865#else /* !COAP_OSCORE_SUPPORT */
3866int
3868 return 0;
3869}
3870
3871int
3873 return 0;
3874}
3875
3876int
3878 return 0;
3879}
3880
3883 const coap_address_t *local_if,
3884 const coap_address_t *server,
3885 coap_proto_t proto,
3886 coap_oscore_conf_t *oscore_conf) {
3887 (void)ctx;
3888 (void)local_if;
3889 (void)server;
3890 (void)proto;
3891 (void)oscore_conf;
3892 return NULL;
3893}
3894
3897 const coap_address_t *local_if,
3898 const coap_address_t *server,
3899 coap_proto_t proto,
3900 coap_oscore_conf_t *oscore_conf,
3901 void *app_data,
3903 coap_str_const_t *ws_host) {
3904 (void)ctx;
3905 (void)local_if;
3906 (void)server;
3907 (void)proto;
3908 (void)oscore_conf;
3909 (void)app_data;
3910 (void)callback;
3911 (void)ws_host;
3912 return NULL;
3913}
3914
3917 const coap_address_t *local_if,
3918 const coap_address_t *server,
3919 coap_proto_t proto,
3920 coap_dtls_cpsk_t *psk_data,
3921 coap_oscore_conf_t *oscore_conf) {
3922 (void)ctx;
3923 (void)local_if;
3924 (void)server;
3925 (void)proto;
3926 (void)psk_data;
3927 (void)oscore_conf;
3928 return NULL;
3929}
3930
3933 const coap_address_t *local_if,
3934 const coap_address_t *server,
3935 coap_proto_t proto,
3936 coap_dtls_cpsk_t *psk_data,
3937 coap_oscore_conf_t *oscore_conf,
3938 void *app_data,
3940 coap_str_const_t *ws_host) {
3941 (void)ctx;
3942 (void)local_if;
3943 (void)server;
3944 (void)proto;
3945 (void)psk_data;
3946 (void)oscore_conf;
3947 (void)app_data;
3948 (void)callback;
3949 (void)ws_host;
3950 return NULL;
3951}
3952
3955 const coap_address_t *local_if,
3956 const coap_address_t *server,
3957 coap_proto_t proto,
3958 coap_dtls_pki_t *pki_data,
3959 coap_oscore_conf_t *oscore_conf) {
3960 (void)ctx;
3961 (void)local_if;
3962 (void)server;
3963 (void)proto;
3964 (void)pki_data;
3965 (void)oscore_conf;
3966 return NULL;
3967}
3968
3971 const coap_address_t *local_if,
3972 const coap_address_t *server,
3973 coap_proto_t proto,
3974 coap_dtls_pki_t *pki_data,
3975 coap_oscore_conf_t *oscore_conf,
3976 void *app_data,
3978 coap_str_const_t *ws_host) {
3979 (void)ctx;
3980 (void)local_if;
3981 (void)server;
3982 (void)proto;
3983 (void)pki_data;
3984 (void)oscore_conf;
3985 (void)app_data;
3986 (void)callback;
3987 (void)ws_host;
3988 return NULL;
3989}
3990
3991int
3993 coap_oscore_conf_t *oscore_conf) {
3994 (void)context;
3995 (void)oscore_conf;
3996 return 0;
3997}
3998
4001 coap_oscore_save_seq_num_t save_seq_num_func,
4002 void *save_seq_num_func_param,
4003 uint64_t start_seq_num) {
4004 (void)conf_mem;
4005 (void)save_seq_num_func;
4006 (void)save_seq_num_func_param;
4007 (void)start_seq_num;
4008 return NULL;
4009}
4010
4011int
4013 (void)oscore_conf;
4014 return 0;
4015}
4016
4017int
4019 coap_bin_const_t *recipient_id) {
4020 (void)context;
4021 (void)recipient_id;
4022 return 0;
4023}
4024
4025int
4027 coap_bin_const_t *recipient_id) {
4028 (void)context;
4029 (void)recipient_id;
4030 return 0;
4031}
4032
4033void
4035 coap_oscore_find_handler_t find_handler,
4036 coap_oscore_update_seq_num_handler_t update_seq_num_handler) {
4037 (void)context;
4038 (void)find_handler;
4039 (void)update_seq_num_handler;
4040}
4041
4042COAP_API int
4044 const coap_bin_const_t *recipient_id,
4045 uint64_t last_seq, uint64_t seq_window) {
4046 (void)oscore_conf;
4047 (void)recipient_id;
4048 (void)last_seq;
4049 (void)seq_window;
4050 return 0;
4051}
4052
4053#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:1256
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:198
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:5563
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:2170
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:3485
#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_RECIPIENT_CHANGED
Triggered when coap_oscore_session_set_recipient_ctx() replaces the recipient context bound to a sess...
Definition coap_event.h:136
@ 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_SIGNATURE_FAILURE
Triggered when there is an OSCORE signature failure.
Definition coap_event.h:138
@ 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:845
#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.
#define CBOR_BYTE_STRING
Definition oscore_cbor.h:64
uint64_t oscore_cbor_get_element_size(const uint8_t **buffer, size_t *buf_size)
#define CBOR_TEXT_STRING
Definition oscore_cbor.h:65
#define CBOR_UNSIGNED_INTEGER
Definition oscore_cbor.h:62
uint8_t oscore_cbor_get_next_element(const uint8_t **buffer, size_t *buf_size)
#define CBOR_FLOATING_POINT
Definition oscore_cbor.h:70
uint8_t oscore_cbor_get_number(const uint8_t **data, size_t *buf_size, int64_t *value)
#define CBOR_MAP
Definition oscore_cbor.h:67
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_CURVE_X25519
Definition oscore_cose.h:64
@ COSE_CURVE_ED25519
Definition oscore_cose.h:66
@ COSE_ALGORITHM_ECDH_SS_HKDF_256
@ COSE_ALGORITHM_EDDSA
@ COSE_ALGORITHM_AES_CCM_16_64_128
@ COSE_ALGORITHM_AES_CCM_16_64_256
@ COSE_KTY_OKP
Definition oscore_cose.h:73
size_t oscore_prepare_aad(const uint8_t *external_aad_buffer, size_t external_aad_len, uint8_t *aad_buffer, size_t aad_size)
void oscore_reference_recipient_ctx(oscore_recipient_ctx_t *recipient_ctx)
Increment the recipient context reference count.
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)
#define COAP_OSCORE_DECRYPT_NO_REPLAY
coap_oscore_decrypt_pdu_ext() flag: verify a request without the replay check and without touching th...
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...
struct coap_pdu_t * coap_oscore_decrypt_pdu_ext(coap_session_t *session, coap_pdu_t *pdu, uint32_t flags)
As coap_oscore_decrypt_pdu(), with flags (COAP_OSCORE_DECRYPT_*).
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)
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.
void oscore_log_int_value(coap_log_t level, const char *name, int value)
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)
int oscore_derive_keystream(oscore_ctx_t *osc_ctx, cose_encrypt0_t *code, uint8_t coap_request, coap_bin_const_t *sender_key, coap_bin_const_t *id_context, size_t cs_size, uint8_t *keystream, size_t keystream_size)
oscore_association_t * oscore_find_association(coap_session_t *session, coap_bin_const_t *token)
int coap_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.
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_crypto_cred_t *sender_cred, uint8_t *external_aad_ptr, size_t external_aad_size)
void oscore_release_recipient_ctx(oscore_recipient_ctx_t **recipient_ctx)
Cleanup recipient context, including releasing the oscore context if the oscore context referenced is...
coap_session_t * coap_new_client_session_oscore_pki3_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_pki_t *pki_data, coap_oscore_conf_t *oscore_conf, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PKI credentials protecting the data using...
ssize_t oscore_encode_option_value(uint8_t *option_buffer, size_t option_buf_len, cose_encrypt0_t *cose, uint8_t group_flag, uint8_t appendix_b_2)
Encode the OSCORE option.
size_t coap_oscore_overhead(coap_session_t *session, coap_pdu_t *pdu)
Determine the additional data size requirements for adding in OSCORE.
@ OSCORE_MODE_SINGLE
Vanilla RFC 8613 support.
@ OSCORE_MODE_GROUP
RFC 10021 support.
@ 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:192
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:691
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:545
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:469
#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:232
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:1630
#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:335
#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:858
#define COAP_DEFAULT_PORT
Definition coap_pdu.h:39
#define COAP_RESPONSE_CODE(N)
Definition coap_pdu.h:96
#define COAP_RESPONSE_CLASS(C)
Definition coap_pdu.h:99
coap_proto_t
CoAP protocol types Note: coap_layers_coap[] needs updating if extended.
Definition coap_pdu.h:245
coap_pdu_code_t
Set of codes available for a PDU.
Definition coap_pdu.h:259
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:412
#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:961
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:198
int coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds given data to the pdu that is passed as first parameter.
Definition coap_pdu.c:930
coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu)
Gets the token associated with pdu.
Definition coap_pdu.c:1750
@ COAP_BOOL_FALSE
Definition coap_pdu.h:306
@ COAP_REQUEST_CODE_POST
Definition coap_pdu.h:263
@ COAP_REQUEST_CODE_FETCH
Definition coap_pdu.h:266
@ 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_string_t * coap_new_string(size_t size)
Returns a new string object with at least size+1 bytes storage allocated.
Definition coap_str.c:21
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
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition coap_str.c:50
int coap_oscore_group_is_supported(void)
Check whether OSCORE Group support is available.
int coap_oscore_pairwise_is_supported(void)
Check whether OSCORE Pairwise support is available.
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:989
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:872
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:356
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.
Holds credential definitions.
coap_bin_const_t * auth_cred
Authentication Credentials of type type.
coap_bin_const_t * pub_raw
Raw Value of the public key abstracted from key_tls.
coap_bin_const_t * pub_der
DER Value of the public key abstracted from key_tls.
Holds private key definitions.
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
CoAP string data definition.
Definition coap_str.h:39
uint8_t * s
string data
Definition coap_str.h:41
size_t length
length of string
Definition coap_str.h:40
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.
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.