libcoap 4.3.5-develop-19cef11
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-2024 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
22
23#if COAP_OSCORE_SUPPORT
24#include <ctype.h>
25
26#define AAD_BUF_LEN 200 /* length of aad_buffer */
27
28static oscore_ctx_t *coap_oscore_init(coap_context_t *c_context,
29 coap_oscore_conf_t *oscore_conf);
30
31#if COAP_CLIENT_SUPPORT
32
33int
35 if (oscore_conf) {
36 oscore_ctx_t *osc_ctx;
37
38 if (oscore_conf->recipient_id_count == 0) {
39 coap_log_warn("OSCORE: Recipient ID must be defined for a client\n");
40 return 0;
41 }
42 if (oscore_conf->rfc8613_b_2) {
43 /* Need to replace id_context with random value */
44 coap_binary_t *id_context = coap_new_binary(8);
45
46 if (id_context == NULL)
47 return 0;
49 coap_prng_lkd(id_context->s, id_context->length);
50 oscore_conf->id_context = (coap_bin_const_t *)id_context;
51 session->b_2_step = COAP_OSCORE_B_2_STEP_1;
52 coap_log_oscore("Appendix B.2 client step 1 (Generated ID1)\n");
53 }
54
55 osc_ctx = coap_oscore_init(session->context, oscore_conf);
56 if (osc_ctx == NULL) {
57 return 0;
58 }
59 session->recipient_ctx = osc_ctx->recipient_chain;
60 session->oscore_encryption = 1;
61 }
62 return 1;
63}
64
67 const coap_address_t *local_if,
68 const coap_address_t *server,
69 coap_proto_t proto,
70 coap_oscore_conf_t *oscore_conf) {
71 coap_session_t *session;
72
73 coap_lock_lock(ctx, return NULL);
74 session = coap_new_client_session_oscore_lkd(ctx, local_if, server, proto, oscore_conf);
76 return session;
77}
78
81 const coap_address_t *local_if,
82 const coap_address_t *server,
83 coap_proto_t proto,
84 coap_oscore_conf_t *oscore_conf) {
85 coap_session_t *session =
86 coap_new_client_session_lkd(ctx, local_if, server, proto);
87
88 if (!session)
89 return NULL;
90
91 if (coap_oscore_initiate(session, oscore_conf) == 0) {
93 return NULL;
94 }
95 return session;
96}
97
100 const coap_address_t *local_if,
101 const coap_address_t *server,
102 coap_proto_t proto,
103 coap_dtls_cpsk_t *psk_data,
104 coap_oscore_conf_t *oscore_conf) {
105 coap_session_t *session;
106
107 coap_lock_lock(ctx, return NULL);
108 session = coap_new_client_session_oscore_psk_lkd(ctx, local_if, server, proto, psk_data,
109 oscore_conf);
110 coap_lock_unlock(ctx);
111 return session;
112}
113
116 const coap_address_t *local_if,
117 const coap_address_t *server,
118 coap_proto_t proto,
119 coap_dtls_cpsk_t *psk_data,
120 coap_oscore_conf_t *oscore_conf) {
121 coap_session_t *session;
122
124 session = coap_new_client_session_psk2_lkd(ctx, local_if, server, proto, psk_data);
125
126 if (!session)
127 return NULL;
128
129 if (coap_oscore_initiate(session, oscore_conf) == 0) {
131 return NULL;
132 }
133 return session;
134}
135
138 const coap_address_t *local_if,
139 const coap_address_t *server,
140 coap_proto_t proto,
141 coap_dtls_pki_t *pki_data,
142 coap_oscore_conf_t *oscore_conf) {
143 coap_session_t *session;
144
145 coap_lock_lock(ctx, return NULL);
146 session = coap_new_client_session_oscore_pki_lkd(ctx, local_if, server, proto, pki_data,
147 oscore_conf);
148 coap_lock_unlock(ctx);
149 return session;
150}
151
154 const coap_address_t *local_if,
155 const coap_address_t *server,
156 coap_proto_t proto,
157 coap_dtls_pki_t *pki_data,
158 coap_oscore_conf_t *oscore_conf) {
159 coap_session_t *session;
160
162 session = coap_new_client_session_pki_lkd(ctx, local_if, server, proto, pki_data);
163
164 if (!session)
165 return NULL;
166
167 if (coap_oscore_initiate(session, oscore_conf) == 0) {
169 return NULL;
170 }
171 return session;
172}
173#endif /* COAP_CLIENT_SUPPORT */
174#if COAP_SERVER_SUPPORT
175
176COAP_API int
178 coap_oscore_conf_t *oscore_conf) {
179 int ret;
180
181 coap_lock_lock(context, return 0);
182 ret = coap_context_oscore_server_lkd(context, oscore_conf);
183 coap_lock_unlock(context);
184 return ret;
185}
186
187int
189 coap_oscore_conf_t *oscore_conf) {
190 oscore_ctx_t *osc_ctx;
191
192 coap_lock_check_locked(context);
193 osc_ctx = coap_oscore_init(context, oscore_conf);
194 /* osc_ctx already added to context->osc_ctx */
195 if (osc_ctx)
196 return 1;
197 return 0;
198}
199
200#endif /* COAP_SERVER_SUPPORT */
201
202int
204 coap_uri_t uri;
205 coap_opt_iterator_t opt_iter;
206 coap_opt_t *option;
207 uint8_t option_value_buffer[15];
208 coap_optlist_t *optlist_chain = NULL;
209
210 if ((option =
211 coap_check_option(pdu, COAP_OPTION_PROXY_URI, &opt_iter)) == NULL)
212 return 1;
213
214 /* Need to break down into the component parts, but keep data safe */
215 memset(&uri, 0, sizeof(uri));
216
218 coap_opt_length(option),
219 &uri) < 0 || uri.scheme >= COAP_URI_SCHEME_LAST) {
220 coap_log_warn("Proxy URI '%.*s' not decodable\n",
221 coap_opt_length(option),
222 (const char *)coap_opt_value(option));
223 goto error;
224 }
226 goto error;
227
228 if (!coap_insert_option(pdu,
230 uri.host.length,
231 uri.host.s))
232 goto error;
236 coap_encode_var_safe(option_value_buffer,
237 sizeof(option_value_buffer),
238 uri.port & 0xffff),
239 option_value_buffer))
240 goto error;
241 if (uri.path.length) {
242 /* Add in the Uri-Path options */
244 &optlist_chain))
245 goto error;
246 }
247 if (uri.query.length) {
248 /* Add in the Uri-Query options */
250 &optlist_chain))
251 goto error;
252 }
253 if (!coap_add_optlist_pdu(pdu, &optlist_chain))
254 goto error;
255
256 if (!coap_insert_option(pdu,
258 strlen(coap_uri_scheme[uri.scheme].name),
259 (const uint8_t *)coap_uri_scheme[uri.scheme].name))
260 goto error;
261
262 coap_delete_optlist(optlist_chain);
263 return 1;
264
265error:
266 coap_delete_optlist(optlist_chain);
267 return 0;
268}
269
270static void
271dump_cose(cose_encrypt0_t *cose, const char *message) {
272#if COAP_MAX_LOGGING_LEVEL < _COAP_LOG_OSCORE
273 (void)cose;
274 (void)message;
275#else /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_OSCORE */
277 char buffer[30];
278
279 coap_log_oscore("%s Cose information\n", message);
281 cose_get_alg_name(cose->alg, buffer, sizeof(buffer)));
283 oscore_log_hex_value(COAP_LOG_OSCORE, "partial_iv", &cose->partial_iv);
285 oscore_log_hex_value(COAP_LOG_OSCORE, "kid_context", &cose->kid_context);
287 "oscore_option",
288 &cose->oscore_option);
290 oscore_log_hex_value(COAP_LOG_OSCORE, "external_aad", &cose->external_aad);
292 }
293#endif /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_OSCORE */
294}
295
298 coap_pdu_t *pdu,
299 coap_bin_const_t *kid_context,
300 oscore_partial_iv_t send_partial_iv) {
301 coap_pdu_t *ret_pdu;
302
303 coap_lock_lock(session->context, return NULL);
304 ret_pdu = coap_oscore_new_pdu_encrypted_lkd(session, pdu, kid_context, send_partial_iv);
305 coap_lock_unlock(session->context);
306
307 return ret_pdu;
308}
309
310/*
311 * Take current PDU, create a new one approriately separated as per RFC8613
312 * and then encrypt / integrity check the OSCORE data
313 */
316 coap_pdu_t *pdu,
317 coap_bin_const_t *kid_context,
318 oscore_partial_iv_t send_partial_iv) {
319 uint8_t coap_request = COAP_PDU_IS_REQUEST(pdu) || COAP_PDU_IS_PING(pdu);
320 coap_pdu_code_t code =
321 coap_request ? COAP_REQUEST_CODE_POST : COAP_RESPONSE_CODE(204);
322 coap_pdu_t *osc_pdu;
323 coap_pdu_t *plain_pdu = NULL;
324 coap_bin_const_t pdu_token;
325 coap_opt_iterator_t opt_iter;
326 coap_opt_t *option;
327 uint8_t pdu_code = pdu->code;
328 size_t length;
329 const uint8_t *data;
330 uint8_t *ciphertext_buffer = NULL;
331 size_t ciphertext_len = 0;
332 uint8_t aad_buffer[AAD_BUF_LEN];
333 uint8_t nonce_buffer[13];
335 coap_bin_const_t nonce;
336 oscore_recipient_ctx_t *rcp_ctx;
337 oscore_ctx_t *osc_ctx;
338 cose_encrypt0_t cose[1];
339 uint8_t group_flag = 0;
340 int show_pdu = 0;
341 int doing_observe = 0;
342 uint32_t observe_value = 0;
343 oscore_association_t *association = NULL;
344 oscore_sender_ctx_t *snd_ctx;
345 uint8_t external_aad_buffer[200];
346 coap_bin_const_t external_aad;
347 uint8_t oscore_option[48];
348 size_t oscore_option_len;
349
350 /* Check that OSCORE has not already been done */
351 if (coap_check_option(pdu, COAP_OPTION_OSCORE, &opt_iter))
352 return NULL;
353
354 if (coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter))
355 doing_observe = 1;
356
357 coap_log_debug("PDU to encrypt\n");
359 osc_pdu = coap_pdu_init(pdu->type == COAP_MESSAGE_NON &&
360 session->b_2_step != COAP_OSCORE_B_2_NONE ?
361 COAP_MESSAGE_CON : pdu->type,
362 code,
363 pdu->mid,
364 pdu->used_size + coap_oscore_overhead(session, pdu));
365 if (osc_pdu == NULL)
366 return NULL;
367
368 cose_encrypt0_init(cose); /* clears cose memory */
369 pdu_token = coap_pdu_get_token(pdu);
370 if (coap_request) {
371 /*
372 * RFC8613 8.1 Step 1. Protecting the client's request
373 * Get the Sender Context
374 */
375 rcp_ctx = session->recipient_ctx;
376 if (rcp_ctx == NULL)
377 goto error;
378 osc_ctx = rcp_ctx->osc_ctx;
379 assert(osc_ctx);
380 snd_ctx = osc_ctx->sender_context;
381 } else {
382 /*
383 * RFC8613 8.3 Step 1. Protecting the server's response
384 * Get the Sender Context
385 */
386 association = oscore_find_association(session, &pdu_token);
387 if (association == NULL)
388 goto error;
389
390 rcp_ctx = association->recipient_ctx;
391 osc_ctx = rcp_ctx->osc_ctx;
392 snd_ctx = osc_ctx->sender_context;
393 cose_encrypt0_set_partial_iv(cose, association->partial_iv);
394 cose_encrypt0_set_aad(cose, association->aad);
395 }
396
397 cose_encrypt0_set_alg(cose, osc_ctx->aead_alg);
398
399 if (coap_request || doing_observe ||
400 send_partial_iv == OSCORE_SEND_PARTIAL_IV) {
401 uint8_t partial_iv_buffer[8];
402 size_t partial_iv_len;
403 coap_bin_const_t partial_iv;
404 partial_iv_len = coap_encode_var_safe8(partial_iv_buffer,
405 sizeof(partial_iv_buffer),
406 snd_ctx->seq);
407 if (snd_ctx->seq == 0) {
408 /* Need to special case */
409 partial_iv_buffer[0] = '\000';
410 partial_iv_len = 1;
411 }
412 partial_iv.s = partial_iv_buffer;
413 partial_iv.length = partial_iv_len;
414 cose_encrypt0_set_partial_iv(cose, &partial_iv);
415 }
416
417 if (coap_request)
419
420 cose_encrypt0_set_key_id(cose, snd_ctx->sender_id);
421
422 /* nonce (needs to have sender information correctly set up) */
423
424 if (coap_request || doing_observe ||
425 send_partial_iv == OSCORE_SEND_PARTIAL_IV) {
426 /*
427 * 8.1 Step 3 or RFC8613 8.3.1 Step A
428 * Compose the AEAD nonce
429 *
430 * Requires in COSE object as appropriate
431 * key_id (kid) (sender)
432 * partial_iv (sender)
433 * common_iv (already in osc_ctx)
434 */
435 nonce.s = nonce_buffer;
436 nonce.length = 13;
437 oscore_generate_nonce(cose, osc_ctx, nonce_buffer, 13);
438 cose_encrypt0_set_nonce(cose, &nonce);
439 if (!oscore_increment_sender_seq(osc_ctx))
440 goto error;
441 if (osc_ctx->save_seq_num_func) {
442 if (osc_ctx->sender_context->seq > osc_ctx->sender_context->next_seq) {
443 /* Only update at ssn_freq rate */
444 osc_ctx->sender_context->next_seq += osc_ctx->ssn_freq;
445 osc_ctx->save_seq_num_func(osc_ctx->sender_context->next_seq,
446 osc_ctx->save_seq_num_func_param);
447 }
448 }
449 } else {
450 /*
451 * 8.3 Step 3.
452 * Use nonce from request
453 */
454 cose_encrypt0_set_nonce(cose, association->nonce);
455 }
456
457 /* OSCORE_option (needs to be before AAD as included in AAD if group) */
458
459 /* cose is modified for encode option in response message */
460 if (!coap_request) {
461 /* no kid on response */
462 cose_encrypt0_set_key_id(cose, NULL);
463 if (!doing_observe && send_partial_iv == OSCORE_SEND_NO_IV)
465 }
466 if (kid_context) {
467 cose_encrypt0_set_kid_context(cose, kid_context);
468 }
469 oscore_option_len =
470 oscore_encode_option_value(oscore_option, sizeof(oscore_option), cose,
471 group_flag,
472 session->b_2_step != COAP_OSCORE_B_2_NONE);
473 if (!coap_request) {
474 /* Reset what was just unset as appropriate for AAD */
476 cose_encrypt0_set_partial_iv(cose, association->partial_iv);
477 }
478 if (kid_context)
480
481 /*
482 * RFC8613 8.1/8.3 Step 2(a) (5.4).
483 * Compose the External AAD and then AAD
484 *
485 * OSCORE_option requires
486 * partial_iv (cose partial_iv)
487 * kid_context (cose kid_context)
488 * key_id (cose key_id)
489 * group_flag
490 *
491 * Non Group (based on osc_tx->mode) requires the following
492 * aead_alg (osc_ctx)
493 * request_kid (request key_id using cose)
494 * request_piv (request partial_iv using cose)
495 * options (none at present)
496 * Group (based on osc_tx->mode) requires the following
497 * aead_alg (osc_ctx) (pairwise mode)
498 * sign_enc_alg (osc_ctx) (group mode)
499 * sign_alg (osc_ctx) (group mode)
500 * alg_pairwise_key_agreement (osc_ctx) (pairwise mode)
501 * request_kid (request key_id using cose)
502 * request_piv (request partial_iv using cose)
503 * options (none at present)
504 * request_kid_context (osc_ctx id_context)
505 * OSCORE_option (parameter)
506 * test_gs_public_key (osc_ctx sender_context public_key)
507 * gm_public_key (osc_ctx gm_public_key)
508 *
509 * Note: No I options at present
510 */
511 if (coap_request || osc_ctx->mode != OSCORE_MODE_SINGLE ||
512 send_partial_iv == OSCORE_SEND_PARTIAL_IV) {
513 /* External AAD */
514 external_aad.s = external_aad_buffer;
515 external_aad.length = oscore_prepare_e_aad(osc_ctx,
516 cose,
517 NULL,
518 0,
519 NULL,
520 external_aad_buffer,
521 sizeof(external_aad_buffer));
522 cose_encrypt0_set_external_aad(cose, &external_aad);
523
524 /* AAD */
525 aad.s = aad_buffer;
526 aad.length = oscore_prepare_aad(external_aad_buffer,
527 external_aad.length,
528 aad_buffer,
529 sizeof(aad_buffer));
530 assert(aad.length < AAD_BUF_LEN);
531 cose_encrypt0_set_aad(cose, &aad);
532 }
533
534 /*
535 * RFC8613 8.1/8.3 Step 2(b) (5.3).
536 *
537 * Set up temp plaintext pdu, the data including token, options and
538 * optional payload will get encrypted as COSE ciphertext.
539 */
540 plain_pdu = coap_pdu_init(pdu->type,
541 pdu->code,
542 pdu->mid,
543 pdu->used_size + 1 /* pseudo-token with actual code */);
544 if (plain_pdu == NULL)
545 goto error;
546
547 coap_add_token(osc_pdu, pdu_token.length, pdu_token.s);
548
549 /* First byte of plain is real CoAP code. Pretend it is token */
550 coap_add_token(plain_pdu, 1, &pdu_code);
551
552 /* Copy across the Outer/Inner Options to respective PDUs */
554 while ((option = coap_option_next(&opt_iter))) {
555 switch (opt_iter.number) {
560 /* Outer only */
561 if (!coap_insert_option(osc_pdu,
562 opt_iter.number,
563 coap_opt_length(option),
564 coap_opt_value(option)))
565 goto error;
566 break;
568 /* Make as Outer option as-is */
569 if (!coap_insert_option(osc_pdu,
570 opt_iter.number,
571 coap_opt_length(option),
572 coap_opt_value(option)))
573 goto error;
574 if (coap_request) {
575 /* Make as Inner option (unchanged) */
576 if (!coap_insert_option(plain_pdu,
577 opt_iter.number,
578 coap_opt_length(option),
579 coap_opt_value(option)))
580 goto error;
581 osc_pdu->code = COAP_REQUEST_CODE_FETCH;
582 } else {
583 /* Make as Inner option but empty */
584 if (!coap_insert_option(plain_pdu, opt_iter.number, 0, NULL))
585 goto error;
586 osc_pdu->code = COAP_RESPONSE_CODE(205);
587 }
588 show_pdu = 1;
589 doing_observe = 1;
590 observe_value = coap_decode_var_bytes(coap_opt_value(option),
591 coap_opt_length(option));
592 break;
594 /*
595 * Should have already been caught by doing
596 * coap_rebuild_pdu_for_proxy() before calling
597 * coap_oscore_new_pdu_encrypted_lkd()
598 */
599 assert(0);
600 break;
601 default:
602 /* Make as Inner option */
603 if (!coap_insert_option(plain_pdu,
604 opt_iter.number,
605 coap_opt_length(option),
606 coap_opt_value(option)))
607 goto error;
608 break;
609 }
610 }
611 /* Add in data to plain */
612 if (coap_get_data(pdu, &length, &data)) {
613 if (!coap_add_data(plain_pdu, length, data))
614 goto error;
615 }
616 if (show_pdu) {
617 coap_log_oscore("OSCORE payload\n");
618 coap_show_pdu(COAP_LOG_OSCORE, plain_pdu);
619 }
620
621 /*
622 * 8.1/8.3 Step 4.
623 * Encrypt the COSE object.
624 *
625 * Requires in COSE object as appropriate
626 * alg (already set)
627 * key (sender key)
628 * nonce (already set)
629 * aad (already set)
630 * plaintext
631 */
632 cose_encrypt0_set_key(cose, snd_ctx->sender_key);
633 cose_encrypt0_set_plaintext(cose, plain_pdu->token, plain_pdu->used_size);
634 dump_cose(cose, "Pre encrypt");
635 ciphertext_buffer =
636 coap_malloc_type(COAP_OSCORE_BUF, OSCORE_CRYPTO_BUFFER_SIZE);
637 if (ciphertext_buffer == NULL)
638 goto error;
639 ciphertext_len = cose_encrypt0_encrypt(cose,
640 ciphertext_buffer,
641 plain_pdu->used_size + AES_CCM_TAG);
642 if ((int)ciphertext_len <= 0) {
643 coap_log_warn("OSCORE: Encryption Failure, result code: %d \n",
644 (int)ciphertext_len);
645 goto error;
646 }
647 assert(ciphertext_len < OSCORE_CRYPTO_BUFFER_SIZE);
648
649 /* Add in OSCORE option (previously computed) */
650 if (!coap_insert_option(osc_pdu,
652 oscore_option_len,
653 oscore_option))
654 goto error;
655
656 /* Add now encrypted payload */
657 if (!coap_add_data(osc_pdu, ciphertext_len, ciphertext_buffer))
658 goto error;
659
660 coap_free_type(COAP_OSCORE_BUF, ciphertext_buffer);
661 ciphertext_buffer = NULL;
662
663 coap_delete_pdu(plain_pdu);
664 plain_pdu = NULL;
665
666 if (association && association->is_observe == 0)
667 oscore_delete_association(session, association);
668 association = NULL;
669
670 /*
671 * If this is a response ACK with data, make it a separate response
672 * by sending an Empty ACK and changing osc_pdu's MID and type. This
673 * then allows lost response ACK (now CON) with data to be recovered.
674 */
675 if (coap_request == 0 && osc_pdu->type == COAP_MESSAGE_ACK &&
676 COAP_RESPONSE_CLASS(pdu->code) == 2 &&
677 COAP_PROTO_NOT_RELIABLE(session->proto)) {
679 0,
680 osc_pdu->mid,
681 0);
682 if (empty) {
683 if (coap_send_internal(session, empty) != COAP_INVALID_MID) {
684 osc_pdu->mid = coap_new_message_id_lkd(session);
685 osc_pdu->type = COAP_MESSAGE_CON;
686 }
687 }
688 }
689
690 if (!coap_pdu_encode_header(osc_pdu, session->proto)) {
691 goto error;
692 }
693
694 /*
695 * Set up an association for handling a response if this is a request
696 */
697 if (coap_request) {
698 association = oscore_find_association(session, &pdu_token);
699 if (association) {
700 /* Refresh the association */
701 coap_delete_bin_const(association->nonce);
702 association->nonce =
703 coap_new_bin_const(cose->nonce.s, cose->nonce.length);
704 if (association->nonce == NULL)
705 goto error;
706 coap_delete_bin_const(association->aad);
707 association->aad = coap_new_bin_const(cose->aad.s, cose->aad.length);
708 if (association->aad == NULL)
709 goto error;
710 if (doing_observe && observe_value == 1) {
712 association->obs_partial_iv = association->partial_iv;
713 } else {
714 coap_delete_bin_const(association->partial_iv);
715 }
716 association->partial_iv =
718 if (association->partial_iv == NULL)
719 goto error;
720 association->recipient_ctx = rcp_ctx;
721 coap_delete_pdu(association->sent_pdu);
722 if (session->b_2_step != COAP_OSCORE_B_2_NONE && !session->done_b_1_2) {
723 size_t size;
724
725 association->sent_pdu = coap_pdu_duplicate_lkd(pdu, session,
726 pdu_token.length,
727 pdu_token.s, NULL);
728 if (association->sent_pdu == NULL)
729 goto error;
730 if (coap_get_data(pdu, &size, &data)) {
731 coap_add_data(association->sent_pdu, size, data);
732 }
733 } else {
734 association->sent_pdu = NULL;
735 }
736 } else if (!oscore_new_association(session,
737 pdu,
738 &pdu_token,
739 rcp_ctx,
740 &cose->aad,
741 &cose->nonce,
742 &cose->partial_iv,
743 doing_observe)) {
744 goto error;
745 }
746 session->done_b_1_2 = 1;
747 }
748 return osc_pdu;
749
750error:
751 if (ciphertext_buffer)
752 coap_free_type(COAP_OSCORE_BUF, ciphertext_buffer);
753 coap_delete_pdu(osc_pdu);
754 coap_delete_pdu(plain_pdu);
755 return NULL;
756}
757
758static void
759build_and_send_error_pdu(coap_session_t *session,
760 coap_pdu_t *rcvd,
761 coap_pdu_code_t code,
762 const char *diagnostic,
763 uint8_t *echo_data,
764 coap_bin_const_t *kid_context,
765 int encrypt_oscore) {
766 coap_pdu_t *err_pdu = NULL;
767 coap_bin_const_t token;
768 int oscore_encryption = session->oscore_encryption;
769 unsigned char buf[4];
770
771 token = coap_pdu_get_token(rcvd);
774 code,
775 rcvd->mid,
776 token.length + 2 + 8 +
777 (diagnostic ? strlen(diagnostic) : 0));
778 if (!err_pdu)
779 return;
780 coap_add_token(err_pdu, token.length, token.s);
781 if (echo_data) {
782 coap_add_option_internal(err_pdu, COAP_OPTION_ECHO, 8, echo_data);
783 } else if (kid_context == NULL) {
786 coap_encode_var_safe(buf, sizeof(buf), 0),
787 buf);
788 }
789 if (diagnostic)
790 coap_add_data(err_pdu, strlen(diagnostic), (const uint8_t *)diagnostic);
791 session->oscore_encryption = encrypt_oscore;
792
793 if ((echo_data || kid_context) && encrypt_oscore) {
794 coap_pdu_t *osc_pdu;
795
796 osc_pdu =
797 coap_oscore_new_pdu_encrypted_lkd(session, err_pdu, kid_context,
798 echo_data ? 1 : 0);
799 if (!osc_pdu)
800 goto fail_resp;
801 session->oscore_encryption = 0;
802 coap_send_internal(session, osc_pdu);
803 coap_delete_pdu(err_pdu);
804 err_pdu = NULL;
805 } else {
806 coap_send_internal(session, err_pdu);
807 err_pdu = NULL;
808 }
809fail_resp:
810 session->oscore_encryption = oscore_encryption;
811 coap_delete_pdu(err_pdu);
812 return;
813}
814
815/* pdu contains incoming message with encrypted COSE ciphertext payload
816 * function returns decrypted message
817 * and verifies signature, if present
818 * returns NULL when decryption,verification fails
819 */
822 coap_pdu_t *pdu) {
823 coap_pdu_t *decrypt_pdu = NULL;
824 coap_pdu_t *plain_pdu = NULL;
825 const uint8_t *osc_value; /* value of OSCORE option */
826 uint8_t osc_size; /* size of OSCORE OPTION */
827 coap_opt_iterator_t opt_iter;
828 coap_opt_t *opt = NULL;
829 cose_encrypt0_t cose[1];
830 oscore_ctx_t *osc_ctx = NULL;
831 uint8_t aad_buffer[AAD_BUF_LEN];
832 uint8_t nonce_buffer[13];
834 coap_bin_const_t nonce;
835 int pltxt_size = 0;
836 int got_resp_piv = 0;
837 int doing_resp_observe = 0;
838 uint8_t coap_request = COAP_PDU_IS_REQUEST(pdu);
839 coap_bin_const_t pdu_token;
840 uint8_t *st_encrypt;
841 size_t encrypt_len;
842 size_t tag_len;
843 oscore_recipient_ctx_t *rcp_ctx = NULL;
844 oscore_association_t *association = NULL;
845 uint8_t external_aad_buffer[100];
846 coap_bin_const_t external_aad;
847 oscore_sender_ctx_t *snd_ctx = NULL;
848#if COAP_CLIENT_SUPPORT
849 coap_pdu_t *sent_pdu = NULL;
850#endif /* COAP_CLIENT_SUPPORT */
851
852 opt = coap_check_option(pdu, COAP_OPTION_OSCORE, &opt_iter);
853 assert(opt);
854 if (opt == NULL)
855 return NULL;
856
857 if (session->context->p_osc_ctx == NULL) {
858 coap_log_warn("OSCORE: Not enabled\n");
859 if (!coap_request)
862 session);
863 return NULL;
864 }
865
866 if (pdu->data == NULL) {
867 coap_log_warn("OSCORE: No protected payload\n");
868 if (!coap_request)
871 session);
872 return NULL;
873 }
874
875 osc_size = coap_opt_length(opt);
876 osc_value = coap_opt_value(opt);
877
878 cose_encrypt0_init(cose); /* clear cose memory */
879
880 /* PDU code will be filled in after decryption */
881 decrypt_pdu =
882 coap_pdu_init(pdu->type, 0, pdu->mid, pdu->used_size);
883 if (decrypt_pdu == NULL) {
884 if (!coap_request)
887 session);
888 goto error;
889 }
890
891 /* Copy across the Token */
892 pdu_token = coap_pdu_get_token(pdu);
893 coap_add_token(decrypt_pdu, pdu_token.length, pdu_token.s);
894
895 /*
896 * 8.2/8.4 Step 1.
897 * Copy outer options across, except E and OSCORE options
898 */
900 while ((opt = coap_option_next(&opt_iter))) {
901 switch (opt_iter.number) {
902 /* 'E' options skipped */
904 case COAP_OPTION_ETAG:
919 case COAP_OPTION_ECHO:
920 case COAP_OPTION_RTAG:
921 /* OSCORE does not get copied across */
923 break;
924 default:
925 if (!coap_add_option_internal(decrypt_pdu,
926 opt_iter.number,
927 coap_opt_length(opt),
928 coap_opt_value(opt))) {
929 if (!coap_request)
932 session);
933 goto error;
934 }
935 break;
936 }
937 }
938
939 if (coap_request) {
940 uint64_t incoming_seq;
941 /*
942 * 8.2 Step 2
943 * Decompress COSE object
944 * Get Recipient Context based on kid and optional kid_context
945 */
946 if (oscore_decode_option_value(osc_value, osc_size, cose) == 0) {
947 coap_log_warn("OSCORE: OSCORE Option cannot be decoded.\n");
948 build_and_send_error_pdu(session,
949 pdu,
951 "Failed to decode COSE",
952 NULL,
953 NULL,
954 0);
955 goto error_no_ack;
956 }
957 osc_ctx = oscore_find_context(session->context,
958 cose->key_id,
959 &cose->kid_context,
960 NULL,
961 &rcp_ctx);
962 if (!osc_ctx) {
963 if (cose->kid_context.length > 0) {
964 const uint8_t *ptr;
965 size_t length;
966 /* Appendix B.2 protocol check - Is the recipient key_id known */
967 osc_ctx = oscore_find_context(session->context,
968 cose->key_id,
969 NULL,
970 session->oscore_r2 != 0 ? (uint8_t *)&session->oscore_r2 : NULL,
971 &rcp_ctx);
972 ptr = cose->kid_context.s;
973 length = cose->kid_context.length;
974 if (ptr && osc_ctx && osc_ctx->rfc8613_b_2 &&
975 osc_ctx->mode == OSCORE_MODE_SINGLE) {
976 /* Processing Appendix B.2 protocol */
977 /* Need to CBOR unwrap kid_context */
978 coap_bin_const_t kid_context;
979
980 kid_context.length = oscore_cbor_get_element_size(&ptr, &length);
981 kid_context.s = ptr;
982 cose_encrypt0_set_kid_context(cose, (coap_bin_const_t *)&kid_context);
983
984 if (session->oscore_r2 != 0) {
985 /* B.2 step 4 */
987 cose->kid_context.length);
988
989 if (kc == NULL)
990 goto error;
991
992 session->b_2_step = COAP_OSCORE_B_2_STEP_4;
993 coap_log_oscore("Appendix B.2 server step 4 (R2 || R3)\n");
994 oscore_update_ctx(osc_ctx, kc);
995 } else {
996 session->b_2_step = COAP_OSCORE_B_2_STEP_2;
997 coap_log_oscore("Appendix B.2 server step 2 (ID1)\n");
998 osc_ctx = oscore_duplicate_ctx(session->context,
999 osc_ctx,
1000 osc_ctx->sender_context->sender_id,
1001 &cose->key_id,
1002 &cose->kid_context);
1003 if (osc_ctx == NULL)
1004 goto error;
1005 /*
1006 * Complete the Verify (B.2 step 2)
1007 * before sending back the response
1008 */
1009 rcp_ctx = osc_ctx->recipient_chain;
1010 }
1011 } else {
1012 osc_ctx = NULL;
1013 }
1014 }
1015 } else if (session->b_2_step != COAP_OSCORE_B_2_NONE) {
1016 session->b_2_step = COAP_OSCORE_B_2_NONE;
1017 coap_log_oscore("Appendix B.2 server finished\n");
1018 }
1019 if (!osc_ctx) {
1020 coap_log_crit("OSCORE: Security Context not found\n");
1021 oscore_log_hex_value(COAP_LOG_OSCORE, "key_id", &cose->key_id);
1022 oscore_log_hex_value(COAP_LOG_OSCORE, "kid_context", &cose->kid_context);
1023 build_and_send_error_pdu(session,
1024 pdu,
1025 COAP_RESPONSE_CODE(401),
1026 "Security context not found",
1027 NULL,
1028 NULL,
1029 0);
1030 goto error_no_ack;
1031 }
1032 /* to be used for encryption of returned response later */
1033 session->recipient_ctx = rcp_ctx;
1034 snd_ctx = osc_ctx->sender_context;
1035
1036 /*
1037 * 8.2 Step 3.
1038 * Verify Partial IV is not duplicated.
1039 *
1040 * Requires in COSE object as appropriate
1041 * partial_iv (as received)
1042 */
1043 if (rcp_ctx->initial_state == 0 &&
1044 !oscore_validate_sender_seq(rcp_ctx, cose)) {
1045 coap_log_warn("OSCORE: Replayed or old message\n");
1046 build_and_send_error_pdu(session,
1047 pdu,
1048 COAP_RESPONSE_CODE(401),
1049 "Replay detected",
1050 NULL,
1051 NULL,
1052 0);
1053 goto error_no_ack;
1054 }
1055
1056 incoming_seq =
1058 rcp_ctx->last_seq = incoming_seq;
1059 } else { /* !coap_request */
1060 /*
1061 * 8.4 Step 2
1062 * Decompress COSE object
1063 * Get Recipient Context based on token
1064 */
1065 if (oscore_decode_option_value(osc_value, osc_size, cose) == 0) {
1066 coap_log_warn("OSCORE: OSCORE Option cannot be decoded.\n");
1069 session);
1070 goto error;
1071 }
1072 got_resp_piv = cose->partial_iv.length ? 1 : 0;
1073
1074 association = oscore_find_association(session, &pdu_token);
1075 if (association) {
1076 rcp_ctx = association->recipient_ctx;
1077 osc_ctx = rcp_ctx->osc_ctx;
1078 snd_ctx = osc_ctx->sender_context;
1079#if COAP_CLIENT_SUPPORT
1080 sent_pdu = association->sent_pdu;
1081 if (session->b_2_step != COAP_OSCORE_B_2_NONE) {
1082 const uint8_t *ptr = cose->kid_context.s;
1083 size_t length = cose->kid_context.length;
1084
1085 if (ptr) {
1086 /* Need to CBOR unwrap kid_context */
1087 coap_bin_const_t kid_context;
1088
1089 kid_context.length = oscore_cbor_get_element_size(&ptr, &length);
1090 kid_context.s = ptr;
1091 cose_encrypt0_set_kid_context(cose, &kid_context);
1092 }
1093 if (ptr && !coap_binary_equal(osc_ctx->id_context, &cose->kid_context)) {
1094 /* If Appendix B.2 step 3 is in operation */
1095 /* Need to update Security Context with new (R2 || ID1) ID Context */
1097 osc_ctx->id_context->length);
1098
1099 if (kc == NULL) {
1102 session);
1103 goto error;
1104 }
1105
1106 memcpy(kc->s, cose->kid_context.s, cose->kid_context.length);
1107 memcpy(&kc->s[cose->kid_context.length],
1108 osc_ctx->id_context->s,
1109 osc_ctx->id_context->length);
1110
1111 session->b_2_step = COAP_OSCORE_B_2_STEP_3;
1112 coap_log_oscore("Appendix B.2 client step 3 (R2 || ID1)\n");
1113 oscore_update_ctx(osc_ctx, (coap_bin_const_t *)kc);
1114 } else {
1115 session->b_2_step = COAP_OSCORE_B_2_STEP_5;
1116 coap_log_oscore("Appendix B.2 client step 5 (R2 || R3)\n");
1117 }
1118 }
1119#endif /* COAP_CLIENT_SUPPORT */
1120 } else {
1121 coap_log_crit("OSCORE: Security Context association not found\n");
1124 session);
1125 goto error;
1126 }
1127 }
1128
1129 cose_encrypt0_set_alg(cose, osc_ctx->aead_alg);
1130
1131 if (coap_request) {
1132 /*
1133 * RFC8613 8.2 Step 4.
1134 * Compose the External AAD and then AAD
1135 *
1136 * Non Group (based on osc_tx->mode) requires the following
1137 * aead_alg (osc_ctx)
1138 * request_kid (request key_id using cose)
1139 * request_piv (request partial_iv using cose)
1140 * options (none at present)
1141 * Group (based on osc_tx->mode) requires the following
1142 * aead_alg (osc_ctx) (pairwise mode)
1143 * sign_enc_alg (osc_ctx) (group mode)
1144 * sign_alg (osc_ctx) (group mode)
1145 * alg_pairwise_key_agreement (osc_ctx) (pairwise mode)
1146 * request_kid (request key_id using cose)
1147 * request_piv (request partial_iv using cose)
1148 * options (none at present)
1149 * request_kid_context (osc_ctx id_context)
1150 * OSCORE_option (as received in request)
1151 * test_gs_public_key (recipient public key)
1152 * gm_public_key (osc_ctx gm_public_key)
1153 *
1154 * Note: No I options at present
1155 */
1156
1157 /* External AAD */
1158 external_aad.s = external_aad_buffer;
1159 external_aad.length = oscore_prepare_e_aad(osc_ctx,
1160 cose,
1161 osc_value,
1162 osc_size,
1163 NULL,
1164 external_aad_buffer,
1165 sizeof(external_aad_buffer));
1166 cose_encrypt0_set_external_aad(cose, &external_aad);
1167
1168 /* AAD */
1169 aad.s = aad_buffer;
1170 aad.length = oscore_prepare_aad(external_aad_buffer,
1171 external_aad.length,
1172 aad_buffer,
1173 sizeof(aad_buffer));
1174 assert(aad.length < AAD_BUF_LEN);
1175 cose_encrypt0_set_aad(cose, &aad);
1176
1177 /*
1178 * RFC8613 8.2 Step 5.
1179 * Compute the AEAD nonce.
1180 *
1181 * Requires in COSE object as appropriate
1182 * key_id (kid) (Recipient ID)
1183 * partial_iv (as received in request)
1184 * common_iv (already in osc_ctx)
1185 */
1186 nonce.s = nonce_buffer;
1187 nonce.length = 13;
1188 oscore_generate_nonce(cose, osc_ctx, nonce_buffer, 13);
1189 cose_encrypt0_set_nonce(cose, &nonce);
1190 /*
1191 * Set up an association for use in the response
1192 */
1193 association = oscore_find_association(session, &pdu_token);
1194 if (association) {
1195 /* Refresh the association */
1196 coap_delete_bin_const(association->nonce);
1197 association->nonce =
1198 coap_new_bin_const(cose->nonce.s, cose->nonce.length);
1199 if (association->nonce == NULL)
1200 goto error;
1201 coap_delete_bin_const(association->partial_iv);
1202 association->partial_iv =
1204 if (association->partial_iv == NULL)
1205 goto error;
1206 coap_delete_bin_const(association->aad);
1207 association->aad = coap_new_bin_const(cose->aad.s, cose->aad.length);
1208 if (association->aad == NULL)
1209 goto error;
1210 association->recipient_ctx = rcp_ctx;
1211 } else if (!oscore_new_association(session,
1212 NULL,
1213 &pdu_token,
1214 rcp_ctx,
1215 &cose->aad,
1216 &cose->nonce,
1217 &cose->partial_iv,
1218 0)) {
1219 goto error;
1220 }
1221 /* So association is not released when handling decrypt */
1222 association = NULL;
1223 } else { /* ! coap_request */
1224 /* Need to do nonce before AAD because of different partial_iv */
1225 /*
1226 * 8.4 Step 4.
1227 * Compose the AEAD nonce.
1228 */
1229 cose_encrypt0_set_key_id(cose, rcp_ctx->recipient_id);
1230 if (cose->partial_iv.length == 0) {
1231 cose_encrypt0_set_partial_iv(cose, association->partial_iv);
1232 cose_encrypt0_set_nonce(cose, association->nonce);
1233 } else {
1234 uint64_t last_seq;
1235
1236 if (rcp_ctx->initial_state == 0 &&
1237 !oscore_validate_sender_seq(rcp_ctx, cose)) {
1238 coap_log_warn("OSCORE: Replayed or old message\n");
1239 goto error;
1240 }
1241 last_seq =
1243 if (rcp_ctx->last_seq>= OSCORE_SEQ_MAX) {
1244 coap_log_warn("OSCORE Replay protection, SEQ larger than SEQ_MAX.\n");
1245 goto error;
1246 }
1247 if (last_seq > rcp_ctx->last_seq)
1248 rcp_ctx->last_seq = last_seq;
1249 /*
1250 * Requires in COSE object as appropriate
1251 * kid (set above)
1252 * partial_iv (as received)
1253 * common_iv (already in osc_ctx)
1254 */
1255 oscore_generate_nonce(cose, osc_ctx, nonce_buffer, 13);
1256 nonce.s = nonce_buffer;
1257 nonce.length = 13;
1258 cose_encrypt0_set_nonce(cose, &nonce);
1259 }
1260#ifdef OSCORE_EXTRA_DEBUG
1261 dump_cose(cose, "!req post set nonce");
1262#endif /* OSCORE_EXTRA_DEBUG */
1263 /*
1264 * 8.4 Step 3.
1265 * Compose the External AAD and then AAD (same as request non-group (5.4)
1266 *
1267 * Non Group (based on osc_tx->mode) requires the following
1268 * aead_alg (osc_ctx)
1269 * request_kid (request key_id using cose)
1270 * request_piv (request partial_iv using cose)
1271 * options (none at present)
1272 * Group (based on osc_tx->mode) requires the following
1273 * aead_alg (osc_ctx) (pairwise mode)
1274 * sign_enc_alg (osc_ctx) (group mode)
1275 * sign_alg (osc_ctx) (group mode)
1276 * alg_pairwise_key_agreement (osc_ctx) (pairwise mode)
1277 * request_kid (request key_id using cose)
1278 * request_piv (request partial_iv using cose)
1279 * options (none at present)
1280 * request_kid_context (osc_ctx id_context)
1281 * OSCORE_option (as received in request)
1282 * test_gs_public_key (recipient public key)
1283 * gm_public_key (osc_ctx gm_public_key)
1284 *
1285 * Note: No I options at present
1286 */
1287
1288 /* External AAD */
1289 cose_encrypt0_set_key_id(cose, snd_ctx->sender_id);
1290 if (association->is_observe && association->obs_partial_iv && got_resp_piv) {
1291 cose_encrypt0_set_partial_iv(cose, association->obs_partial_iv);
1292 } else {
1293 cose_encrypt0_set_partial_iv(cose, association->partial_iv);
1294 }
1295#ifdef OSCORE_EXTRA_DEBUG
1296 dump_cose(cose, "!req pre aad");
1297#endif /* OSCORE_EXTRA_DEBUG */
1298 external_aad.s = external_aad_buffer;
1299 external_aad.length = oscore_prepare_e_aad(osc_ctx,
1300 cose,
1301 NULL,
1302 0,
1303 NULL,
1304 external_aad_buffer,
1305 sizeof(external_aad_buffer));
1306 cose_encrypt0_set_external_aad(cose, &external_aad);
1307
1308 /* AAD */
1309 aad.s = aad_buffer;
1310 aad.length = oscore_prepare_aad(external_aad_buffer,
1311 external_aad.length,
1312 aad_buffer,
1313 sizeof(aad_buffer));
1314 assert(aad.length < AAD_BUF_LEN);
1315 cose_encrypt0_set_aad(cose, &aad);
1316#ifdef OSCORE_EXTRA_DEBUG
1317 dump_cose(cose, "!req post set aad");
1318#endif /* OSCORE_EXTRA_DEBUG */
1319 }
1320
1321 /*
1322 * 8.2 Step 6 / 8.4 Step 5.
1323 * Decrypt the COSE object.
1324 *
1325 * Requires in COSE object as appropriate
1326 * alg (already set)
1327 * key
1328 * nonce (already set)
1329 * aad (already set)
1330 * ciphertext
1331 */
1332 st_encrypt = pdu->data;
1333 encrypt_len = pdu->used_size - (pdu->data - pdu->token);
1334 if (encrypt_len <= 0) {
1335 coap_log_warn("OSCORE: No protected payload\n");
1336 if (!coap_request)
1339 session);
1340 goto error;
1341 }
1342 cose_encrypt0_set_key(cose, rcp_ctx->recipient_key);
1343 cose_encrypt0_set_ciphertext(cose, st_encrypt, encrypt_len);
1344
1345 tag_len = cose_tag_len(cose->alg);
1346 /* Decrypt into plain_pdu, so code (token), options and data are in place */
1347 plain_pdu = coap_pdu_init(0, 0, 0, encrypt_len /* - tag_len */);
1348 if (plain_pdu == NULL) {
1349 if (!coap_request)
1352 session);
1353 goto error;
1354 }
1355
1356 /* need the tag_len on the end for TinyDTLS to do its work - yuk */
1357 if (!coap_pdu_resize(plain_pdu, encrypt_len /* - tag_len */)) {
1358 if (!coap_request)
1361 session);
1362 goto error;
1363 }
1364
1365 /* Account for 1 byte 'code' used as token */
1366 plain_pdu->e_token_length = 1;
1367 plain_pdu->actual_token.length = 1;
1368 /* Account for the decrypted data */
1369 plain_pdu->used_size = encrypt_len - tag_len;
1370
1371 dump_cose(cose, "Pre decrypt");
1372 pltxt_size =
1373 cose_encrypt0_decrypt(cose, plain_pdu->token, encrypt_len - tag_len);
1374 if (pltxt_size <= 0) {
1375 coap_log_warn("OSCORE: Decryption Failure, result code: %d \n",
1376 (int)pltxt_size);
1377 if (coap_request) {
1378 build_and_send_error_pdu(session,
1379 pdu,
1380 COAP_RESPONSE_CODE(400),
1381 "Decryption failed",
1382 NULL,
1383 NULL,
1384 0);
1385 oscore_roll_back_seq(rcp_ctx);
1386 goto error_no_ack;
1387 } else {
1390 session);
1391 }
1392 goto error;
1393 }
1394
1395 assert((size_t)pltxt_size < pdu->alloc_size + pdu->max_hdr_size);
1396
1397 /* Appendix B.2 Trap */
1398 if (session->b_2_step == COAP_OSCORE_B_2_STEP_2) {
1399 /* Need to update Security Context with new (R2 || ID1) ID Context */
1400 coap_binary_t *kc =
1401 coap_new_binary(sizeof(session->oscore_r2) + cose->kid_context.length);
1402 coap_bin_const_t oscore_r2;
1403
1404 if (kc == NULL) {
1405 if (!coap_request)
1408 session);
1409 goto error;
1410 }
1411
1412 coap_prng_lkd(&session->oscore_r2, sizeof(session->oscore_r2));
1413 memcpy(kc->s, &session->oscore_r2, sizeof(session->oscore_r2));
1414 memcpy(&kc->s[sizeof(session->oscore_r2)],
1415 cose->kid_context.s,
1416 cose->kid_context.length);
1417
1418 coap_log_oscore("Appendix B.2 server step 2 (R2 || ID1)\n");
1419 oscore_update_ctx(osc_ctx, (coap_bin_const_t *)kc);
1420
1421 oscore_r2.length = sizeof(session->oscore_r2);
1422 oscore_r2.s = (const uint8_t *)&session->oscore_r2;
1423 coap_log_oscore("Appendix B.2 server step 2 plain response\n");
1424 build_and_send_error_pdu(session,
1425 pdu,
1426 COAP_RESPONSE_CODE(401),
1427 NULL,
1428 NULL,
1429 &oscore_r2,
1430 1);
1431 goto error_no_ack;
1432 }
1433#if COAP_CLIENT_SUPPORT
1434 if (session->b_2_step == COAP_OSCORE_B_2_STEP_3) {
1435 coap_log_oscore("Appendix B.2 client step 3 (R2 || R3)\n");
1436 coap_pdu_encode_header(plain_pdu, session->proto);
1437 plain_pdu->actual_token.s = plain_pdu->token;
1438 plain_pdu->code = plain_pdu->token[0];
1439 if (plain_pdu->code != COAP_RESPONSE_CODE(401)) {
1440 coap_log_warn("OSCORE Appendix B.2: Expected 4.01 response\n");
1441 }
1442 /* Skip the options */
1443 coap_option_iterator_init(plain_pdu, &opt_iter, COAP_OPT_ALL);
1444 while (coap_option_next(&opt_iter)) {
1445 }
1446 if (opt_iter.length > 0 && opt_iter.next_option &&
1447 opt_iter.next_option[0] == COAP_PAYLOAD_START) {
1448 plain_pdu->data = &opt_iter.next_option[1];
1449 }
1450 coap_log_oscore("Inner Response PDU (plaintext)\n");
1451 coap_show_pdu(COAP_LOG_OSCORE, plain_pdu);
1452 /*
1453 * Need to update Security Context with new (R2 || R3) ID Context
1454 * and retransmit the request
1455 */
1457
1458 if (kc == NULL) {
1459 if (!coap_request)
1462 session);
1463 goto error;
1464 }
1465 memcpy(kc->s, cose->kid_context.s, cose->kid_context.length);
1466 coap_prng_lkd(&kc->s[cose->kid_context.length], 8);
1467
1468 oscore_update_ctx(osc_ctx, (coap_bin_const_t *)kc);
1469
1471 session,
1472 &pdu->actual_token);
1473 if (session->con_active)
1474 session->con_active--;
1475 coap_send_ack_lkd(session, pdu);
1476 if (sent_pdu) {
1477 coap_log_oscore("Appendix B.2 retransmit pdu\n");
1478 if (coap_retransmit_oscore_pdu(session, sent_pdu, NULL) ==
1480 goto error_no_ack;
1481 }
1482 goto error_no_ack;
1483 }
1484#endif /* COAP_CLIENT_SUPPORT */
1485
1486#if COAP_SERVER_SUPPORT
1487 /* Appendix B.1.2 request Trap */
1488 if (coap_request && osc_ctx->rfc8613_b_1_2) {
1489 if (rcp_ctx->initial_state == 1) {
1490 opt = coap_check_option(plain_pdu, COAP_OPTION_ECHO, &opt_iter);
1491 if (opt) {
1492 /* Verify Client is genuine */
1493 if (coap_opt_length(opt) == 8 &&
1494 memcmp(coap_opt_value(opt), rcp_ctx->echo_value, 8) == 0) {
1495 if (!oscore_validate_sender_seq(rcp_ctx, cose)) {
1496 coap_log_warn("OSCORE: Replayed or old message\n");
1497 build_and_send_error_pdu(session,
1498 pdu,
1499 COAP_RESPONSE_CODE(401),
1500 "Replay detected",
1501 NULL,
1502 NULL,
1503 0);
1504 goto error_no_ack;
1505 }
1506 } else
1507 goto error;
1508 } else {
1509 /* RFC 8163 Appendix B.1.2 */
1510 if (session->b_2_step == COAP_OSCORE_B_2_STEP_4) {
1511 session->b_2_step = COAP_OSCORE_B_2_NONE;
1512 coap_log_oscore("Appendix B.2 server finished\n");
1513 }
1514 coap_prng_lkd(rcp_ctx->echo_value, sizeof(rcp_ctx->echo_value));
1515 coap_log_oscore("Appendix B.1.2 server plain response\n");
1516 build_and_send_error_pdu(session,
1517 pdu,
1518 COAP_RESPONSE_CODE(401),
1519 NULL,
1520 rcp_ctx->echo_value,
1521 NULL,
1522 1);
1523 goto error_no_ack;
1524 }
1525 }
1526 }
1527#endif /* COAP_SERVER_SUPPORT */
1528
1529 /*
1530 * 8.2 Step 7 / 8.4 Step 6.
1531 * Add decrypted Code, options and payload
1532 * [OSCORE option not copied across previously]
1533 */
1534
1535 /* PDU code is pseudo plain_pdu token */
1536 decrypt_pdu->code = plain_pdu->token[0];
1537
1538 /* Copy inner decrypted options across */
1539 coap_option_iterator_init(plain_pdu, &opt_iter, COAP_OPT_ALL);
1540 while ((opt = coap_option_next(&opt_iter))) {
1541 size_t len;
1542 size_t bias;
1543
1544 switch (opt_iter.number) {
1545 case COAP_OPTION_OSCORE:
1546 break;
1548 if (!coap_request) {
1549 bias = cose->partial_iv.length > 3 ? cose->partial_iv.length - 3 : 0;
1550 len = cose->partial_iv.length > 3 ? 3 : cose->partial_iv.length;
1551 /* Make Observe option reflect last 3 bytes of partial_iv */
1553 decrypt_pdu,
1554 opt_iter.number,
1555 len,
1556 cose->partial_iv.s ? &cose->partial_iv.s[bias] : NULL)) {
1559 session);
1560 goto error;
1561 }
1562 doing_resp_observe = 1;
1563 break;
1564 }
1565 association = oscore_find_association(session, &pdu_token);
1566 if (association) {
1567 association->is_observe = 1;
1568 association = NULL;
1569 }
1570 /* Fall Through */
1571 default:
1572 if (!coap_insert_option(decrypt_pdu,
1573 opt_iter.number,
1574 coap_opt_length(opt),
1575 coap_opt_value(opt))) {
1576 if (!coap_request)
1579 session);
1580 goto error;
1581 }
1582 break;
1583 }
1584 }
1585 if (!coap_request && !doing_resp_observe) {
1586 if (association) {
1587 association->is_observe = 0;
1588 }
1589 }
1590 /* Need to copy across any data */
1591 if (opt_iter.length > 0 && opt_iter.next_option &&
1592 opt_iter.next_option[0] == COAP_PAYLOAD_START) {
1593 plain_pdu->data = &opt_iter.next_option[1];
1594 if (!coap_add_data(decrypt_pdu,
1595 plain_pdu->used_size -
1596 (plain_pdu->data - plain_pdu->token),
1597 plain_pdu->data)) {
1598 if (!coap_request)
1601 session);
1602 goto error;
1603 }
1604 }
1605 coap_delete_pdu(plain_pdu);
1606 plain_pdu = NULL;
1607
1608 /* Make sure headers are correctly set up */
1609 if (!coap_pdu_encode_header(decrypt_pdu, session->proto)) {
1610 if (!coap_request)
1613 session);
1614 goto error;
1615 }
1616
1617 if (session->b_2_step != COAP_OSCORE_B_2_NONE) {
1618 session->b_2_step = COAP_OSCORE_B_2_NONE;
1619 coap_log_oscore("Appendix B.2 client finished\n");
1620 }
1621#if COAP_CLIENT_SUPPORT
1622 if (decrypt_pdu->code == COAP_RESPONSE_CODE(401) &&
1623 (opt = coap_check_option(decrypt_pdu, COAP_OPTION_ECHO, &opt_iter))) {
1624 /* Server is requesting Echo refresh check */
1626 session,
1627 &pdu->actual_token);
1628 if (session->con_active)
1629 session->con_active--;
1630 if (sent_pdu) {
1631 coap_send_ack_lkd(session, pdu);
1632 coap_log_debug("PDU requesting re-transmit\n");
1633 coap_show_pdu(COAP_LOG_DEBUG, decrypt_pdu);
1634 coap_log_oscore("RFC9175 retransmit pdu\n");
1635 /* Do not care if this fails */
1636 coap_retransmit_oscore_pdu(session, sent_pdu, opt);
1637 goto error_no_ack;
1638 }
1639 }
1640#endif /* COAP_CLIENT_SUPPORT */
1641 if (association && association->is_observe == 0)
1642 oscore_delete_association(session, association);
1643 return decrypt_pdu;
1644
1645error:
1646 coap_send_ack_lkd(session, pdu);
1647error_no_ack:
1648 if (association && association->is_observe == 0)
1649 oscore_delete_association(session, association);
1650 coap_delete_pdu(decrypt_pdu);
1651 coap_delete_pdu(plain_pdu);
1652 return NULL;
1653}
1654
1655typedef enum {
1656 COAP_ENC_ASCII = 0x01,
1657 COAP_ENC_HEX = 0x02,
1658 COAP_ENC_INTEGER = 0x08,
1659 COAP_ENC_TEXT = 0x10,
1660 COAP_ENC_BOOL = 0x20,
1661 COAP_ENC_LAST
1662} coap_oscore_coding_t;
1663
1664#undef TEXT_MAPPING
1665#define TEXT_MAPPING(t, v) \
1666 { { sizeof(#t)-1, (const uint8_t *)#t }, v }
1667
1668static struct coap_oscore_encoding_t {
1669 coap_str_const_t name;
1670 coap_oscore_coding_t encoding;
1671} oscore_encoding[] = {
1672 TEXT_MAPPING(ascii, COAP_ENC_ASCII),
1673 TEXT_MAPPING(hex, COAP_ENC_HEX),
1674 TEXT_MAPPING(integer, COAP_ENC_INTEGER),
1675 TEXT_MAPPING(text, COAP_ENC_TEXT),
1676 TEXT_MAPPING(bool, COAP_ENC_BOOL),
1677 {{0, NULL}, COAP_ENC_LAST}
1678};
1679
1680typedef struct {
1681 coap_oscore_coding_t encoding;
1682 const char *encoding_name;
1683 union {
1684 int value_int;
1685 coap_bin_const_t *value_bin;
1686 coap_str_const_t value_str;
1687 } u;
1688} oscore_value_t;
1689
1690static uint8_t
1691hex2char(char c) {
1692 assert(isxdigit(c));
1693 if ('a' <= c && c <= 'f')
1694 return c - 'a' + 10;
1695 else if ('A' <= c && c <= 'F')
1696 return c - 'A' + 10;
1697 else
1698 return c - '0';
1699}
1700
1701/* Parse the hex into binary */
1702static coap_bin_const_t *
1703parse_hex_bin(const char *begin, const char *end) {
1704 coap_binary_t *binary = NULL;
1705 size_t i;
1706
1707 if ((end - begin) % 2 != 0)
1708 goto bad_entry;
1709 binary = coap_new_binary((end - begin) / 2);
1710 if (binary == NULL)
1711 goto bad_entry;
1712 for (i = 0; (i < (size_t)(end - begin)) && isxdigit((u_char)begin[i]) &&
1713 isxdigit((u_char)begin[i + 1]);
1714 i += 2) {
1715 binary->s[i / 2] = (hex2char(begin[i]) << 4) + hex2char(begin[i + 1]);
1716 }
1717 if (i != (size_t)(end - begin))
1718 goto bad_entry;
1719 return (coap_bin_const_t *)binary;
1720
1721bad_entry:
1722 coap_delete_binary(binary);
1723 return NULL;
1724}
1725
1726/*
1727 * Break up each OSCORE Configuration line entry into the 3 parts which
1728 * are comma separated
1729 *
1730 * keyword,encoding,value
1731 */
1732static int
1733get_split_entry(const char **start,
1734 size_t size,
1735 coap_str_const_t *keyword,
1736 oscore_value_t *value) {
1737 const char *begin = *start;
1738 const char *end;
1739 const char *kend;
1740 const char *split;
1741 size_t i;
1742
1743retry:
1744 kend = end = memchr(begin, '\n', size);
1745 if (end == NULL)
1746 return 0;
1747
1748 /* Track beginning of next line */
1749 *start = end + 1;
1750 if (end > begin && end[-1] == '\r')
1751 end--;
1752
1753 if (begin[0] == '#' || (end - begin) == 0) {
1754 /* Skip comment / blank line */
1755 size -= kend - begin + 1;
1756 begin = *start;
1757 goto retry;
1758 }
1759
1760 /* Get in the keyword */
1761 split = memchr(begin, ',', end - begin);
1762 if (split == NULL)
1763 goto bad_entry;
1764
1765 keyword->s = (const uint8_t *)begin;
1766 keyword->length = split - begin;
1767
1768 begin = split + 1;
1769 if ((end - begin) == 0)
1770 goto bad_entry;
1771 /* Get in the encoding */
1772 split = memchr(begin, ',', end - begin);
1773 if (split == NULL)
1774 goto bad_entry;
1775
1776 for (i = 0; oscore_encoding[i].name.s; i++) {
1777 coap_str_const_t temp = { split - begin, (const uint8_t *)begin };
1778
1779 if (coap_string_equal(&temp, &oscore_encoding[i].name)) {
1780 value->encoding = oscore_encoding[i].encoding;
1781 value->encoding_name = (const char *)oscore_encoding[i].name.s;
1782 break;
1783 }
1784 }
1785 if (oscore_encoding[i].name.s == NULL)
1786 goto bad_entry;
1787
1788 begin = split + 1;
1789 if ((end - begin) == 0)
1790 goto bad_entry;
1791 /* Get in the keyword's value */
1792 if (begin[0] == '"') {
1793 split = memchr(&begin[1], '"', end - split - 1);
1794 if (split == NULL)
1795 goto bad_entry;
1796 end = split;
1797 begin++;
1798 }
1799 switch (value->encoding) {
1800 case COAP_ENC_ASCII:
1801 value->u.value_bin =
1802 coap_new_bin_const((const uint8_t *)begin, end - begin);
1803 break;
1804 case COAP_ENC_HEX:
1805 /* Parse the hex into binary */
1806 value->u.value_bin = parse_hex_bin(begin, end);
1807 if (value->u.value_bin == NULL)
1808 goto bad_entry;
1809 break;
1810 case COAP_ENC_INTEGER:
1811 value->u.value_int = atoi(begin);
1812 break;
1813 case COAP_ENC_TEXT:
1814 value->u.value_str.s = (const uint8_t *)begin;
1815 value->u.value_str.length = end - begin;
1816 break;
1817 case COAP_ENC_BOOL:
1818 if (memcmp("true", begin, end - begin) == 0)
1819 value->u.value_int = 1;
1820 else if (memcmp("false", begin, end - begin) == 0)
1821 value->u.value_int = 0;
1822 else
1823 goto bad_entry;
1824 break;
1825 case COAP_ENC_LAST:
1826 default:
1827 goto bad_entry;
1828 }
1829 return 1;
1830
1831bad_entry:
1832 coap_log_warn("oscore_conf: Unrecognized configuration entry '%.*s'\n",
1833 (int)(end - begin),
1834 begin);
1835 return 0;
1836}
1837
1838#undef CONFIG_ENTRY
1839#define CONFIG_ENTRY(n, e, t) \
1840 { { sizeof(#n)-1, (const uint8_t *)#n }, e, \
1841 offsetof(coap_oscore_conf_t, n), t }
1842
1843typedef struct oscore_text_mapping_t {
1844 coap_str_const_t text;
1845 int value;
1846} oscore_text_mapping_t;
1847
1848/* Naming as per https://www.iana.org/assignments/cose/cose.xhtml#algorithms */
1849static oscore_text_mapping_t text_aead_alg[] = {
1850 TEXT_MAPPING(AES-CCM-16-64-128, COSE_ALGORITHM_AES_CCM_16_64_128),
1851 TEXT_MAPPING(AES-CCM-16-64-256, COSE_ALGORITHM_AES_CCM_16_64_256),
1852 {{0, NULL}, 0}
1853};
1854
1855static oscore_text_mapping_t text_hkdf_alg[] = {
1856 TEXT_MAPPING(direct+HKDF-SHA-256, COSE_HKDF_ALG_HKDF_SHA_256),
1857 {{0, NULL}, 0}
1858};
1859
1860static struct oscore_config_t {
1861 coap_str_const_t str_keyword;
1862 coap_oscore_coding_t encoding;
1863 size_t offset;
1864 oscore_text_mapping_t *text_mapping;
1865} oscore_config[] = {
1866 CONFIG_ENTRY(master_secret, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
1867 CONFIG_ENTRY(master_salt, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
1868 CONFIG_ENTRY(sender_id, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
1869 CONFIG_ENTRY(id_context, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
1870 CONFIG_ENTRY(recipient_id, COAP_ENC_HEX | COAP_ENC_ASCII, NULL),
1871 CONFIG_ENTRY(replay_window, COAP_ENC_INTEGER, NULL),
1872 CONFIG_ENTRY(ssn_freq, COAP_ENC_INTEGER, NULL),
1873 CONFIG_ENTRY(aead_alg, COAP_ENC_INTEGER | COAP_ENC_TEXT, text_aead_alg),
1874 CONFIG_ENTRY(hkdf_alg, COAP_ENC_INTEGER | COAP_ENC_TEXT, text_hkdf_alg),
1875 CONFIG_ENTRY(rfc8613_b_1_2, COAP_ENC_BOOL, NULL),
1876 CONFIG_ENTRY(rfc8613_b_2, COAP_ENC_BOOL, NULL),
1877 CONFIG_ENTRY(break_sender_key, COAP_ENC_BOOL, NULL),
1878 CONFIG_ENTRY(break_recipient_key, COAP_ENC_BOOL, NULL),
1879};
1880
1881int
1883 uint32_t i;
1884
1885 if (oscore_conf == NULL)
1886 return 0;
1887
1889 coap_delete_bin_const(oscore_conf->master_salt);
1890 coap_delete_bin_const(oscore_conf->id_context);
1891 coap_delete_bin_const(oscore_conf->sender_id);
1892 for (i = 0; i < oscore_conf->recipient_id_count; i++) {
1893 coap_delete_bin_const(oscore_conf->recipient_id[i]);
1894 }
1896 coap_free_type(COAP_STRING, oscore_conf);
1897 return 1;
1898}
1899
1900static coap_oscore_conf_t *
1901coap_parse_oscore_conf_mem(coap_str_const_t conf_mem) {
1902 const char *start = (const char *)conf_mem.s;
1903 const char *end = start + conf_mem.length;
1904 coap_str_const_t keyword;
1905 oscore_value_t value;
1906 coap_oscore_conf_t *oscore_conf;
1907
1908 oscore_conf = coap_malloc_type(COAP_STRING, sizeof(coap_oscore_conf_t));
1909 if (oscore_conf == NULL)
1910 return NULL;
1911 memset(oscore_conf, 0, sizeof(coap_oscore_conf_t));
1912
1913 memset(&value, 0, sizeof(value));
1914 /* Preset with defaults */
1916 oscore_conf->ssn_freq = 1;
1918 oscore_conf->hkdf_alg = COSE_HKDF_ALG_HKDF_SHA_256;
1919 oscore_conf->rfc8613_b_1_2 = 1;
1920 oscore_conf->rfc8613_b_2 = 0;
1921 oscore_conf->break_sender_key = 0;
1922 oscore_conf->break_recipient_key = 0;
1923
1924 while (end > start &&
1925 get_split_entry(&start, end - start, &keyword, &value)) {
1926 size_t i;
1927 size_t j;
1928
1929 for (i = 0; i < sizeof(oscore_config) / sizeof(oscore_config[0]); i++) {
1930 if (coap_string_equal(&oscore_config[i].str_keyword, &keyword) != 0 &&
1931 value.encoding & oscore_config[i].encoding) {
1932 if (coap_string_equal(coap_make_str_const("recipient_id"), &keyword)) {
1933 if (value.u.value_bin->length > 7) {
1934 coap_log_warn("oscore_conf: Maximum size of recipient_id is 7 bytes\n");
1935 goto error_free_value_bin;
1936 }
1937 /* Special case as there are potentially multiple entries */
1938 oscore_conf->recipient_id =
1940 oscore_conf->recipient_id,
1941 sizeof(oscore_conf->recipient_id[0]) *
1942 (oscore_conf->recipient_id_count + 1));
1943 if (oscore_conf->recipient_id == NULL) {
1944 goto error_free_value_bin;
1945 }
1946 oscore_conf->recipient_id[oscore_conf->recipient_id_count++] =
1947 value.u.value_bin;
1948 } else {
1949 coap_bin_const_t *unused_check;
1950
1951 switch (value.encoding) {
1952 case COAP_ENC_HEX:
1953 case COAP_ENC_ASCII:
1954 memcpy(&unused_check,
1955 &(((char *)oscore_conf)[oscore_config[i].offset]),
1956 sizeof(unused_check));
1957 if (unused_check != NULL) {
1958 coap_log_warn("oscore_conf: Keyword '%.*s' duplicated\n",
1959 (int)keyword.length,
1960 (const char *)keyword.s);
1961 goto error;
1962 }
1963 memcpy(&(((char *)oscore_conf)[oscore_config[i].offset]),
1964 &value.u.value_bin,
1965 sizeof(value.u.value_bin));
1966 break;
1967 case COAP_ENC_INTEGER:
1968 case COAP_ENC_BOOL:
1969 memcpy(&(((char *)oscore_conf)[oscore_config[i].offset]),
1970 &value.u.value_int,
1971 sizeof(value.u.value_int));
1972 break;
1973 case COAP_ENC_TEXT:
1974 for (j = 0; oscore_config[i].text_mapping[j].text.s != NULL; j++) {
1975 if (coap_string_equal(&value.u.value_str,
1976 &oscore_config[i].text_mapping[j].text)) {
1977 memcpy(&(((char *)oscore_conf)[oscore_config[i].offset]),
1978 &oscore_config[i].text_mapping[j].value,
1979 sizeof(oscore_config[i].text_mapping[j].value));
1980 break;
1981 }
1982 }
1983 if (oscore_config[i].text_mapping[j].text.s == NULL) {
1984 coap_log_warn("oscore_conf: Keyword '%.*s': value '%.*s' unknown\n",
1985 (int)keyword.length,
1986 (const char *)keyword.s,
1987 (int)value.u.value_str.length,
1988 (const char *)value.u.value_str.s);
1989 goto error;
1990 }
1991 break;
1992 case COAP_ENC_LAST:
1993 default:
1994 assert(0);
1995 break;
1996 }
1997 }
1998 break;
1999 }
2000 }
2001 if (i == sizeof(oscore_config) / sizeof(oscore_config[0])) {
2002 coap_log_warn("oscore_conf: Keyword '%.*s', type '%s' unknown\n",
2003 (int)keyword.length,
2004 (const char *)keyword.s,
2005 value.encoding_name);
2006 if (value.encoding == COAP_ENC_HEX || value.encoding == COAP_ENC_ASCII)
2007 coap_delete_bin_const(value.u.value_bin);
2008 goto error;
2009 }
2010 }
2011 if (!oscore_conf->master_secret) {
2012 coap_log_warn("oscore_conf: master_secret not defined\n");
2013 goto error;
2014 }
2015 if (!oscore_conf->sender_id) {
2016 coap_log_warn("oscore_conf: sender_id not defined\n");
2017 goto error;
2018 }
2019 if (oscore_conf->sender_id->length > 7) {
2020 coap_log_warn("oscore_conf: Maximum size of sender_id is 7 bytes\n");
2021 goto error;
2022 }
2023 if (oscore_conf->recipient_id && oscore_conf->recipient_id[0]->length > 7) {
2024 coap_log_warn("oscore_conf: Maximum size of recipient_id is 7 bytes\n");
2025 goto error;
2026 }
2027 return oscore_conf;
2028
2029error_free_value_bin:
2030 coap_delete_bin_const(value.u.value_bin);
2031error:
2032 coap_delete_oscore_conf(oscore_conf);
2033 return NULL;
2034}
2035
2036static oscore_ctx_t *
2037coap_oscore_init(coap_context_t *c_context, coap_oscore_conf_t *oscore_conf) {
2038 oscore_ctx_t *osc_ctx = NULL;
2039
2040 if (!coap_crypto_check_cipher_alg(oscore_conf->aead_alg)) {
2041 coap_log_warn("COSE: Cipher Algorithm %d not supported\n",
2042 oscore_conf->aead_alg);
2043 goto error;
2044 }
2045 if (!coap_crypto_check_hkdf_alg(oscore_conf->hkdf_alg)) {
2046 coap_log_warn("COSE: HKDF Algorithm %d not supported\n",
2047 oscore_conf->hkdf_alg);
2048 goto error;
2049 }
2050
2051 osc_ctx = oscore_derive_ctx(c_context, oscore_conf);
2052 if (!osc_ctx) {
2053 coap_log_crit("OSCORE: Could not create Security Context!\n");
2054 goto error;
2055 }
2056
2057 /* Free off the recipient_id array */
2059 oscore_conf->recipient_id = NULL;
2060
2061 /* As all is stored in osc_ctx, oscore_conf is no longer needed */
2062 coap_free_type(COAP_STRING, oscore_conf);
2063
2064 /* return default first context */
2065 return osc_ctx;
2066
2067error:
2068 /* Remove from linked chain */
2069 oscore_remove_context(c_context, osc_ctx);
2070
2071 coap_delete_oscore_conf(oscore_conf);
2072 return NULL;
2073}
2074
2075void
2077 oscore_free_contexts(c_context);
2078}
2079
2080void
2083}
2084
2087 coap_oscore_save_seq_num_t save_seq_num_func,
2088 void *save_seq_num_func_param,
2089 uint64_t start_seq_num) {
2090 coap_oscore_conf_t *oscore_conf = coap_parse_oscore_conf_mem(conf_mem);
2091
2092 if (oscore_conf == NULL)
2093 return NULL;
2094
2095 oscore_conf->save_seq_num_func = save_seq_num_func;
2096 oscore_conf->save_seq_num_func_param = save_seq_num_func_param;
2097 oscore_conf->start_seq_num = start_seq_num;
2098 coap_log_oscore("Start Seq no %" PRIu64 "\n", start_seq_num);
2099 return oscore_conf;
2100}
2101
2102/*
2103 * Compute the size of the potential OSCORE overhead
2104 */
2105size_t
2107 size_t overhead = 0;
2108 oscore_recipient_ctx_t *rcp_ctx = session->recipient_ctx;
2109 oscore_ctx_t *osc_ctx = rcp_ctx ? rcp_ctx->osc_ctx : NULL;
2110 coap_opt_iterator_t opt_iter;
2111 coap_opt_t *option;
2112
2113 if (osc_ctx == NULL)
2114 return 0;
2115
2116 /* Protected code held in inner PDU as token */
2117 overhead += 1;
2118
2119 /* Observe option (creates inner and outer */
2120 option = coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter);
2121 if (option) {
2122 /* Assume delta is small */
2123 overhead += 2 + coap_opt_length(option);
2124 }
2125
2126 /* Proxy URI option Split - covered by coap_rebuild_pdu_for_proxy () */
2127
2128 /* OSCORE option */
2129 /* Option header */
2130 overhead += 1 +
2131 /* Partial IV (64 bits max)*/
2132 8 +
2133 /* kid context */
2134 (osc_ctx->id_context ? osc_ctx->id_context->length : 0) +
2135 /* kid */
2136 osc_ctx->sender_context->sender_id->length;
2137
2138 /* AAD overhead */
2139 overhead += AES_CCM_TAG;
2140
2141 /* End of options marker */
2142 overhead += 1;
2143
2144 return overhead;
2145}
2146
2147COAP_API int
2149 coap_bin_const_t *recipient_id) {
2150 int ret;
2151
2152 coap_lock_lock(context, return 0);
2153 ret = coap_new_oscore_recipient_lkd(context, recipient_id);
2154 coap_lock_unlock(context);
2155 return ret;
2156}
2157
2158int
2160 coap_bin_const_t *recipient_id) {
2161 coap_lock_check_locked(context);
2162 if (context->p_osc_ctx == NULL)
2163 return 0;
2164 if (oscore_add_recipient(context->p_osc_ctx, recipient_id, 0) == NULL)
2165 return 0;
2166 return 1;
2167}
2168
2169COAP_API int
2171 coap_bin_const_t *recipient_id) {
2172 int ret;
2173
2174 if (!context || !recipient_id)
2175 return 0;
2176 coap_lock_lock(context, return 0);
2177 ret = coap_delete_oscore_recipient_lkd(context, recipient_id);
2178 coap_lock_unlock(context);
2179 return ret;
2180}
2181
2182int
2184 coap_bin_const_t *recipient_id) {
2185 coap_lock_check_locked(context);
2186 if (context->p_osc_ctx == NULL)
2187 return 0;
2188 return oscore_delete_recipient(context->p_osc_ctx, recipient_id);
2189}
2190
2193#else /* !COAP_OSCORE_SUPPORT */
2194int
2196 return 0;
2197}
2198
2201 const coap_address_t *local_if,
2202 const coap_address_t *server,
2203 coap_proto_t proto,
2204 coap_oscore_conf_t *oscore_conf) {
2205 (void)ctx;
2206 (void)local_if;
2207 (void)server;
2208 (void)proto;
2209 (void)oscore_conf;
2210 return NULL;
2211}
2212
2215 const coap_address_t *local_if,
2216 const coap_address_t *server,
2217 coap_proto_t proto,
2218 coap_dtls_cpsk_t *psk_data,
2219 coap_oscore_conf_t *oscore_conf) {
2220 (void)ctx;
2221 (void)local_if;
2222 (void)server;
2223 (void)proto;
2224 (void)psk_data;
2225 (void)oscore_conf;
2226 return NULL;
2227}
2228
2231 const coap_address_t *local_if,
2232 const coap_address_t *server,
2233 coap_proto_t proto,
2234 coap_dtls_pki_t *pki_data,
2235 coap_oscore_conf_t *oscore_conf) {
2236 (void)ctx;
2237 (void)local_if;
2238 (void)server;
2239 (void)proto;
2240 (void)pki_data;
2241 (void)oscore_conf;
2242 return NULL;
2243}
2244
2245int
2247 coap_oscore_conf_t *oscore_conf) {
2248 (void)context;
2249 (void)oscore_conf;
2250 return 0;
2251}
2252
2255 coap_oscore_save_seq_num_t save_seq_num_func,
2256 void *save_seq_num_func_param,
2257 uint64_t start_seq_num) {
2258 (void)conf_mem;
2259 (void)save_seq_num_func;
2260 (void)save_seq_num_func_param;
2261 (void)start_seq_num;
2262 return NULL;
2263}
2264
2265int
2267 (void)oscore_conf;
2268 return 0;
2269}
2270
2271int
2273 coap_bin_const_t *recipient_id) {
2274 (void)context;
2275 (void)recipient_id;
2276 return 0;
2277}
2278
2279int
2281 coap_bin_const_t *recipient_id) {
2282 (void)context;
2283 (void)recipient_id;
2284 return 0;
2285}
2286
2287#endif /* !COAP_OSCORE_SUPPORT */
#define PRIu64
Definition: coap_internal.h:51
Library specific build wrapper for coap_internal.h.
#define COAP_API
@ COAP_OSCORE_BUF
Definition: coap_mem.h:66
@ COAP_STRING
Definition: coap_mem.h:39
void * coap_realloc_type(coap_memory_tag_t type, void *p, size_t size)
Reallocates a chunk p of bytes created by coap_malloc_type() or coap_realloc_type() and returns a poi...
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().
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
Definition: coap_option.h:26
static int coap_uri_scheme_is_secure(const coap_uri_t *uri)
Definition: coap_uri.h:81
@ COAP_URI_SCHEME_LAST
Definition: coap_uri.h:37
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:986
coap_mid_t coap_retransmit_oscore_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_opt_t *echo)
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:178
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:4491
uint16_t coap_new_message_id_lkd(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: coap_net.c:1679
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:2757
#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:67
unsigned int coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val)
Encodes multiple-length byte sequences.
Definition: coap_encode.c:77
@ COAP_EVENT_OSCORE_DECODE_ERROR
Triggered when there is an OSCORE decode of OSCORE option failure.
Definition: coap_event.h:118
@ COAP_EVENT_OSCORE_INTERNAL_ERROR
Triggered when there is an OSCORE internal error i.e malloc failed.
Definition: coap_event.h:116
@ COAP_EVENT_OSCORE_NOT_ENABLED
Triggered when trying to use OSCORE to decrypt, but it is not enabled.
Definition: coap_event.h:110
@ COAP_EVENT_OSCORE_NO_SECURITY
Triggered when there is no OSCORE security definition found.
Definition: coap_event.h:114
@ COAP_EVENT_OSCORE_NO_PROTECTED_PAYLOAD
Triggered when there is no OSCORE encrypted payload provided.
Definition: coap_event.h:112
@ COAP_EVENT_OSCORE_DECRYPTION_FAILURE
Triggered when there is an OSCORE decryption failure.
Definition: coap_event.h:108
#define coap_lock_unlock(c)
Dummy for no thread-safe code.
#define coap_lock_lock(c, failed)
Dummy for no thread-safe code.
#define coap_lock_check_locked(c)
Dummy for no thread-safe code.
#define coap_log_debug(...)
Definition: coap_debug.h:120
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition: coap_debug.c:101
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
Definition: coap_debug.c:784
#define coap_log_oscore(...)
Definition: coap_debug.h:126
#define coap_log_warn(...)
Definition: coap_debug.h:102
#define coap_log_crit(...)
Definition: coap_debug.h:90
@ COAP_LOG_OSCORE
Definition: coap_debug.h:59
@ COAP_LOG_DEBUG
Definition: coap_debug.h:58
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
Definition: coap_option.c:153
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: coap_option.c:212
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.
Definition: coap_option.c:117
void coap_delete_optlist(coap_optlist_t *queue)
Removes all entries from the optlist_chain, freeing off their memory usage.
Definition: coap_option.c:592
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
Definition: coap_option.h:108
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 ...
Definition: coap_option.c:551
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.
Definition: coap_option.c:199
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition: coap_option.c:249
size_t oscore_cbor_get_element_size(const uint8_t **buffer, size_t *buf_len)
Definition: oscore_cbor.c:240
void cose_encrypt0_set_plaintext(cose_encrypt0_t *ptr, uint8_t *buffer, size_t size)
Definition: oscore_cose.c:315
int cose_encrypt0_set_key(cose_encrypt0_t *ptr, coap_bin_const_t *key)
Definition: oscore_cose.c:401
void cose_encrypt0_set_kid_context(cose_encrypt0_t *ptr, coap_bin_const_t *kid_context)
Definition: oscore_cose.c:368
const char * cose_get_alg_name(cose_alg_t id, char *buffer, size_t buflen)
Definition: oscore_cose.c:120
void cose_encrypt0_set_ciphertext(cose_encrypt0_t *ptr, uint8_t *buffer, size_t size)
Definition: oscore_cose.c:307
int cose_encrypt0_decrypt(cose_encrypt0_t *ptr, uint8_t *plaintext_buffer, size_t plaintext_len)
Definition: oscore_cose.c:460
size_t cose_tag_len(cose_alg_t cose_alg)
Definition: oscore_cose.c:196
void cose_encrypt0_set_aad(cose_encrypt0_t *ptr, coap_bin_const_t *aad)
Definition: oscore_cose.c:390
int cose_encrypt0_encrypt(cose_encrypt0_t *ptr, uint8_t *ciphertext_buffer, size_t ciphertext_len)
Definition: oscore_cose.c:421
void cose_encrypt0_set_partial_iv(cose_encrypt0_t *ptr, coap_bin_const_t *partial_iv)
Definition: oscore_cose.c:325
void cose_encrypt0_set_external_aad(cose_encrypt0_t *ptr, coap_bin_const_t *external_aad)
Definition: oscore_cose.c:379
void cose_encrypt0_init(cose_encrypt0_t *ptr)
Definition: oscore_cose.c:297
void cose_encrypt0_set_alg(cose_encrypt0_t *ptr, uint8_t alg)
Definition: oscore_cose.c:302
void cose_encrypt0_set_key_id(cose_encrypt0_t *ptr, coap_bin_const_t *key_id)
Definition: oscore_cose.c:346
void cose_encrypt0_set_nonce(cose_encrypt0_t *ptr, coap_bin_const_t *nonce)
Definition: oscore_cose.c:411
@ COSE_HKDF_ALG_HKDF_SHA_256
Definition: oscore_cose.h:167
@ COSE_ALGORITHM_AES_CCM_16_64_128
Definition: oscore_cose.h:145
@ COSE_ALGORITHM_AES_CCM_16_64_256
Definition: oscore_cose.h:146
coap_session_t * coap_new_client_session_oscore_psk_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)
Creates a new client session to the designated server with PSK credentials as well as protecting the ...
size_t oscore_prepare_aad(const uint8_t *external_aad_buffer, size_t external_aad_len, uint8_t *aad_buffer, size_t aad_size)
Definition: oscore.c:312
size_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)
Definition: oscore.c:170
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)
uint8_t oscore_validate_sender_seq(oscore_recipient_ctx_t *ctx, cose_encrypt0_t *cose)
Definition: oscore.c:366
oscore_recipient_ctx_t * oscore_add_recipient(oscore_ctx_t *osc_ctx, coap_bin_const_t *rid, uint32_t break_key)
oscore_add_recipient - add in recipient information
#define OSCORE_SEQ_MAX
#define AES_CCM_TAG
Definition: oscore_crypto.h:61
int oscore_decode_option_value(const uint8_t *opt_value, size_t option_len, cose_encrypt0_t *cose)
Definition: oscore.c:246
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.
coap_session_t * coap_new_client_session_oscore_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)
Creates a new client session to the designated server, protecting the data using OSCORE.
int oscore_delete_recipient(oscore_ctx_t *osc_ctx, coap_bin_const_t *rid)
uint8_t oscore_increment_sender_seq(oscore_ctx_t *ctx)
Definition: oscore.c:430
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
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)
Definition: oscore.c:447
int oscore_new_association(coap_session_t *session, coap_pdu_t *sent_pdu, coap_bin_const_t *token, oscore_recipient_ctx_t *recipient_ctx, coap_bin_const_t *aad, coap_bin_const_t *nonce, coap_bin_const_t *partial_iv, int is_observe)
size_t oscore_prepare_e_aad(oscore_ctx_t *ctx, cose_encrypt0_t *cose, const uint8_t *oscore_option, size_t oscore_option_len, coap_bin_const_t *sender_public_key, uint8_t *external_aad_ptr, size_t external_aad_size)
Definition: oscore.c:119
void oscore_delete_server_associations(coap_session_t *session)
void oscore_log_char_value(coap_log_t level, const char *name, const char *value)
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_rebuild_pdu_for_proxy(coap_pdu_t *pdu)
Convert PDU to use Proxy-Scheme option if Proxy-Uri option is present.
void oscore_free_contexts(coap_context_t *c_context)
void oscore_log_hex_value(coap_log_t level, const char *name, coap_bin_const_t *value)
void coap_delete_oscore_associations(coap_session_t *session)
Cleanup all allocated OSCORE association information.
int coap_oscore_initiate(coap_session_t *session, coap_oscore_conf_t *oscore_conf)
Initiate an OSCORE session.
int coap_new_oscore_recipient_lkd(coap_context_t *context, coap_bin_const_t *recipient_id)
Add in the specific Recipient ID into the OSCORE context (server only).
oscore_ctx_t * oscore_duplicate_ctx(coap_context_t *c_context, oscore_ctx_t *o_osc_ctx, coap_bin_const_t *sender_id, coap_bin_const_t *recipient_id, coap_bin_const_t *id_context)
oscore_duplicate_ctx - duplicate a osc_ctx
oscore_partial_iv_t
void coap_delete_all_oscore(coap_context_t *context)
Cleanup all allocated OSCORE information.
void oscore_generate_nonce(cose_encrypt0_t *ptr, oscore_ctx_t *ctx, uint8_t *buffer, uint8_t size)
Definition: oscore.c:343
int oscore_remove_context(coap_context_t *c_context, oscore_ctx_t *osc_ctx)
oscore_association_t * oscore_find_association(coap_session_t *session, coap_bin_const_t *token)
int coap_context_oscore_server_lkd(coap_context_t *context, coap_oscore_conf_t *oscore_conf)
Set the context's default OSCORE configuration for a server.
oscore_ctx_t * oscore_find_context(const coap_context_t *c_context, const coap_bin_const_t rcpkey_id, const coap_bin_const_t *ctxkey_id, uint8_t *oscore_r2, oscore_recipient_ctx_t **recipient_ctx)
oscore_find_context - Locate recipient context (and hence OSCORE context)
coap_session_t * coap_new_client_session_oscore_pki_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)
Creates a new client session to the designated server with PKI credentials as well as protecting the ...
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 RFC8613 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_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.
Definition: coap_oscore.c:2254
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 ...
Definition: coap_oscore.c:2214
int coap_delete_oscore_conf(coap_oscore_conf_t *oscore_conf)
Release all the information associated with the OSCORE configuration.
Definition: coap_oscore.c:2266
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.
Definition: coap_oscore.c:2200
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.
Definition: coap_oscore.c:2246
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 ...
Definition: coap_oscore.c:2230
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).
Definition: coap_oscore.c:2272
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.
Definition: coap_oscore.h:130
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 and stop any further ...
Definition: coap_oscore.c:2280
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:619
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:482
#define COAP_PDU_IS_PING(pdu)
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:1478
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)
Duplicate an existing PDU.
Definition: coap_pdu.c:223
#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:290
#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:769
#define COAP_OPTION_HOP_LIMIT
Definition: coap_pdu.h:133
#define COAP_OPTION_NORESPONSE
Definition: coap_pdu.h:145
#define COAP_OPTION_URI_HOST
Definition: coap_pdu.h:120
#define COAP_OPTION_IF_MATCH
Definition: coap_pdu.h:119
#define COAP_OPTION_BLOCK2
Definition: coap_pdu.h:137
#define COAP_OPTION_CONTENT_FORMAT
Definition: coap_pdu.h:128
#define COAP_OPTION_SIZE2
Definition: coap_pdu.h:139
#define COAP_OPTION_BLOCK1
Definition: coap_pdu.h:138
#define COAP_OPTION_PROXY_SCHEME
Definition: coap_pdu.h:142
#define COAP_DEFAULT_PORT
Definition: coap_pdu.h:37
#define COAP_OPTION_URI_QUERY
Definition: coap_pdu.h:132
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition: coap_pdu.c:183
#define COAP_OPTION_IF_NONE_MATCH
Definition: coap_pdu.h:122
#define COAP_OPTION_LOCATION_PATH
Definition: coap_pdu.h:125
#define COAP_OPTION_URI_PATH
Definition: coap_pdu.h:127
#define COAP_RESPONSE_CODE(N)
Definition: coap_pdu.h:160
#define COAP_RESPONSE_CLASS(C)
Definition: coap_pdu.h:163
coap_proto_t
CoAP protocol types.
Definition: coap_pdu.h:312
coap_pdu_code_t
Set of codes available for a PDU.
Definition: coap_pdu.h:326
#define COAP_OPTION_OSCORE
Definition: coap_pdu.h:126
#define COAP_OPTION_SIZE1
Definition: coap_pdu.h:143
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:349
#define COAP_OPTION_LOCATION_QUERY
Definition: coap_pdu.h:136
#define COAPS_DEFAULT_PORT
Definition: coap_pdu.h:38
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:865
#define COAP_OPTION_RTAG
Definition: coap_pdu.h:146
#define COAP_OPTION_URI_PORT
Definition: coap_pdu.h:124
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:99
#define COAP_OPTION_ACCEPT
Definition: coap_pdu.h:134
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition: coap_pdu.h:266
#define COAP_OPTION_MAXAGE
Definition: coap_pdu.h:131
#define COAP_OPTION_ETAG
Definition: coap_pdu.h:121
#define COAP_OPTION_PROXY_URI
Definition: coap_pdu.h:141
#define COAP_OPTION_OBSERVE
Definition: coap_pdu.h:123
#define COAP_OPTION_ECHO
Definition: coap_pdu.h:144
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:834
coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu)
Gets the token associated with pdu.
Definition: coap_pdu.c:1598
@ COAP_REQUEST_CODE_POST
Definition: coap_pdu.h:330
@ COAP_REQUEST_CODE_FETCH
Definition: coap_pdu.h:333
@ COAP_MESSAGE_NON
Definition: coap_pdu.h:70
@ COAP_MESSAGE_ACK
Definition: coap_pdu.h:71
@ COAP_MESSAGE_CON
Definition: coap_pdu.h:69
coap_session_t * coap_new_client_session_psk2_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)
Creates a new client session to the designated server with PSK credentials.
coap_session_t * coap_new_client_session_pki_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)
Creates a new client session to the designated server with PKI credentials.
coap_session_t * coap_new_client_session_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto)
Creates a new client session to the designated server.
void coap_session_release_lkd(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:376
@ 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
#define COAP_PROTO_NOT_RELIABLE(p)
Definition: coap_session.h:37
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
Definition: coap_str.c:120
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
Definition: coap_str.c:77
coap_str_const_t * coap_make_str_const(const char *string)
Take the specified byte array (text) and create a coap_str_const_t *.
Definition: coap_str.c:66
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
Definition: coap_str.c:110
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
Definition: coap_str.c:105
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
Definition: coap_str.h:211
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
Definition: coap_str.h:197
int coap_oscore_is_supported(void)
Check whether OSCORE is available.
Definition: coap_oscore.c:2195
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:822
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:732
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:276
coap_uri_info_t coap_uri_scheme[COAP_URI_SCHEME_LAST]
Definition: coap_uri.c:51
Multi-purpose address abstraction.
Definition: coap_address.h:148
CoAP binary data definition with const data.
Definition: coap_str.h:64
size_t length
length of binary data
Definition: coap_str.h:65
const uint8_t * s
read-only binary data
Definition: coap_str.h:66
CoAP binary data definition.
Definition: coap_str.h:56
size_t length
length of binary data
Definition: coap_str.h:57
uint8_t * s
binary data
Definition: coap_str.h:58
The CoAP stack's global state is stored in a coap_context_t object.
The structure used for defining the Client PSK setup data to be used.
Definition: coap_dtls.h:410
The structure used for defining the PKI setup data to be used.
Definition: coap_dtls.h:312
Iterator to run through PDU options.
Definition: coap_option.h:168
coap_opt_t * next_option
pointer to the unparsed next option
Definition: coap_option.h:173
size_t length
remaining length of PDU
Definition: coap_option.h:169
coap_option_num_t number
decoded option number
Definition: coap_option.h:170
Representation of chained list of CoAP options to install.
Definition: coap_option.h:325
The structure used to hold the OSCORE configuration information.
void * save_seq_num_func_param
Passed to save_seq_num_func()
uint32_t rfc8613_b_2
1 if rfc8613 B.2 protocol else 0
cose_hkdf_alg_t hkdf_alg
Set to one of COSE_HKDF_ALG_*.
uint32_t break_sender_key
1 if sender key to be broken, else 0
uint32_t ssn_freq
Sender Seq Num update frequency.
coap_oscore_save_seq_num_t save_seq_num_func
Called every seq num change.
uint32_t rfc8613_b_1_2
1 if rfc8613 B.1.2 enabled else 0
uint64_t start_seq_num
Used for ssn_freq updating.
coap_bin_const_t * sender_id
Sender ID (i.e.
coap_bin_const_t ** recipient_id
Recipient ID (i.e.
uint32_t break_recipient_key
1 if recipient key to be broken, else 0
coap_bin_const_t * master_secret
Common Master Secret.
cose_alg_t aead_alg
Set to one of COSE_ALGORITHM_AES*.
coap_bin_const_t * master_salt
Common Master Salt.
uint32_t replay_window
Replay window size Use COAP_OSCORE_DEFAULT_REPLAY_WINDOW.
coap_bin_const_t * id_context
Common ID context.
uint32_t recipient_id_count
Number of recipient_id entries.
structure for CoAP PDUs
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...
coap_proto_t proto
protocol used
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:46
const uint8_t * s
read-only string data
Definition: coap_str.h:48
size_t length
length of string
Definition: coap_str.h:47
const char * name
scheme name
Representation of parsed URI.
Definition: coap_uri.h:65
enum coap_uri_scheme_t scheme
The parsed scheme specifier.
Definition: coap_uri.h:77
coap_str_const_t path
The complete path if present or {0, NULL}.
Definition: coap_uri.h:68
uint16_t port
The port in host byte order.
Definition: coap_uri.h:67
coap_str_const_t query
The complete query if present or {0, NULL}.
Definition: coap_uri.h:72
coap_str_const_t host
The host part of the URI.
Definition: coap_uri.h:66
coap_bin_const_t aad
Definition: oscore_cose.h:208
coap_bin_const_t key
Definition: oscore_cose.h:199
coap_bin_const_t partial_iv
Definition: oscore_cose.h:202
coap_bin_const_t kid_context
Definition: oscore_cose.h:204
coap_bin_const_t nonce
Definition: oscore_cose.h:206
coap_bin_const_t external_aad
Definition: oscore_cose.h:207
coap_bin_const_t key_id
Definition: oscore_cose.h:203
coap_bin_const_t oscore_option
Definition: oscore_cose.h:205
cose_alg_t alg
Definition: oscore_cose.h:198
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
oscore_mode_t mode
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
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
oscore_ctx_t * osc_ctx
coap_bin_const_t * sender_id
coap_bin_const_t * sender_key
uint64_t next_seq
Used for ssn_freq updating.