libcoap 4.3.5-develop-5347a62
Loading...
Searching...
No Matches
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 * Copyright (c) 2018, SICS, RISE AB
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Institute nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 */
32
46
48
49#if COAP_OSCORE_SUPPORT
50
51/* oscore_cs_params
52 * returns cbor array [[param_type], [paramtype, param]]
53 */
54uint8_t *
55oscore_cs_params(int8_t param, int8_t param_type, size_t *len) {
56 uint8_t buf[50];
57 size_t rem_size = sizeof(buf);
58 uint8_t *pt = buf;
59
60 *len = 0;
61 *len += oscore_cbor_put_array(&pt, &rem_size, 2);
62 *len += oscore_cbor_put_array(&pt, &rem_size, 1);
63 *len += oscore_cbor_put_number(&pt, &rem_size, param_type);
64 *len += oscore_cbor_put_array(&pt, &rem_size, 2);
65 *len += oscore_cbor_put_number(&pt, &rem_size, param_type);
66 *len += oscore_cbor_put_number(&pt, &rem_size, param);
67 uint8_t *result = coap_malloc_type(COAP_STRING, *len);
68 memcpy(result, buf, *len);
69 return result;
70}
71
72/* oscore_cs_key_params
73 * returns cbor array [paramtype, param]
74 */
75uint8_t *
76oscore_cs_key_params(cose_curve_t param, int8_t param_type, size_t *len) {
77 uint8_t buf[50];
78 size_t rem_size = sizeof(buf);
79 uint8_t *pt = buf;
80
81 *len = 0;
82 *len += oscore_cbor_put_array(&pt, &rem_size, 2);
83 *len += oscore_cbor_put_number(&pt, &rem_size, param_type);
84 *len += oscore_cbor_put_number(&pt, &rem_size, param);
85 uint8_t *result = coap_malloc_type(COAP_STRING, *len);
86 memcpy(result, buf, *len);
87 return result;
88}
89
90#if COAP_OSCORE_GROUP_SUPPORT
91/* extract_param
92 * extract algorithm parameter from [type, param]
93 */
94static int
95extract_param(const uint8_t **oscore_cbor_array, size_t *array_len) {
96 int64_t mm = 0;
97 uint8_t elem = oscore_cbor_get_next_element(oscore_cbor_array, array_len);
98 if (elem == CBOR_ARRAY) {
99 uint64_t arr_size = oscore_cbor_get_element_size(oscore_cbor_array, array_len);
100 if (arr_size != 2)
101 return 0;
102 for (uint16_t i = 0; i < arr_size; i++) {
103 int8_t ok = oscore_cbor_get_number(oscore_cbor_array, array_len, &mm);
104 if (ok != 0)
105 return 0;
106 }
107 return (int)mm;
108 }
109 return 0;
110}
111
112/* extract_type
113 * extract algorithm parameter from [type, param]
114 */
115static int
116extract_type(const uint8_t **oscore_cbor_array, size_t *array_len) {
117 int64_t mm = 0;
118 uint8_t elem = oscore_cbor_get_next_element(oscore_cbor_array, array_len);
119 if (elem == CBOR_ARRAY) {
120 uint64_t arr_size = oscore_cbor_get_element_size(oscore_cbor_array, array_len);
121 if (arr_size != 2)
122 return 0;
123 if (oscore_cbor_get_number(oscore_cbor_array, array_len, &mm) == 1)
124 return 0;
125 return (int)mm;
126 }
127 return 0;
128}
129#endif /* COAP_OSCORE_GROUP_SUPPORT */
130
131/*
132 * Build the CBOR for external_aad
133 *
134 * external_aad = bstr .cbor aad_array
135 *
136 * No group mode
137 * aad_array = [
138 * oscore_version : uint,
139 * algorithms : [ aead_alg : int / tstr ],
140 * request_kid : bstr,
141 * request_piv : bstr,
142 * options : bstr,
143 * ]
144 *
145 * Group mode
146 * aad_array = [
147 * oscore_version : uint,
148 * algorithms : [aead_alg : int / tstr / null,
149 * alg_group_enc : int / tstr / null,
150 * alg_signature : int / tstr / null,
151 * alg_pairwise_key_agreement : int / tstr / null],
152 * request_kid : bstr,
153 * request_piv : bstr,
154 * options : bstr,
155 * request_kid_context : bstr,
156 * OSCORE_option: bstr,
157 * sender_cred: bstr, (initiator's key)
158 * gm_cred: bstr / null
159 * ]
160 */
161size_t
163 cose_encrypt0_t *cose,
164 const uint8_t *oscore_option,
165 size_t oscore_option_len,
166 coap_crypto_cred_t *sender_cred,
167#if COAP_OSCORE_GROUP_SUPPORT
168 oscore_mode_t mode,
169#endif /* COAP_OSCORE_GROUP_SUPPORT */
170 uint8_t *external_aad_ptr,
171 size_t external_aad_size) {
172 size_t external_aad_len = 0;
173 size_t rem_size = external_aad_size;
174 size_t len;
175
176#if ! COAP_OSCORE_GROUP_SUPPORT
177 (void)oscore_option;
178 (void)oscore_option_len;
179 (void)sender_cred;
180#endif /* ! COAP_OSCORE_GROUP_SUPPORT */
181
182#if COAP_OSCORE_GROUP_SUPPORT
183 if (mode != OSCORE_MODE_SINGLE) {
184 if ((len = oscore_cbor_put_array(&external_aad_ptr, &rem_size, 9)) == 0)
185 goto fail;
186 external_aad_len += len;
187 } else {
188#endif /* COAP_OSCORE_GROUP_SUPPORT */
189 if ((len = oscore_cbor_put_array(&external_aad_ptr, &rem_size, 5)) == 0)
190 goto fail;
191 external_aad_len += len;
192#if COAP_OSCORE_GROUP_SUPPORT
193 }
194#endif /* COAP_OSCORE_GROUP_SUPPORT */
195
196 /* oscore_version, always "1" */
197 if ((len = oscore_cbor_put_unsigned(&external_aad_ptr, &rem_size, 1)) == 0)
198 goto fail;
199 external_aad_len += len;
200
201#if COAP_OSCORE_GROUP_SUPPORT
202 if (mode == OSCORE_MODE_SINGLE) {
203#endif /* COAP_OSCORE_GROUP_SUPPORT */
204 /* Algorithms array with one item*/
205 if ((len = oscore_cbor_put_array(&external_aad_ptr, &rem_size, 1)) == 0)
206 goto fail;
207 external_aad_len += len;
208
209 /* Encryption Algorithm */
210 if ((len = oscore_cbor_put_number(&external_aad_ptr, &rem_size, ctx->aead_alg)) == 0)
211 goto fail;
212 external_aad_len += len;
213#if COAP_OSCORE_GROUP_SUPPORT
214 } else {
215 /*
216 * Algorithms array with 4 items. RFC 10021 Section 3.4: each entry is the
217 * algorithm from the Common Context, "regardless of whether the endpoint
218 * supports" the group or the pairwise mode, and the CBOR simple value null
219 * only if it is not set.
220 */
221 if ((len = oscore_cbor_put_array(&external_aad_ptr, &rem_size, 4)) == 0)
222 goto fail;
223 external_aad_len += len;
224 /* alg_aead */
225 if (ctx->aead_alg) {
226 if ((len = oscore_cbor_put_number(&external_aad_ptr, &rem_size, ctx->aead_alg)) == 0)
227 goto fail;
228 external_aad_len += len;
229 } else {
230 if ((len = oscore_cbor_put_nil(&external_aad_ptr, &rem_size)) == 0)
231 goto fail;
232 external_aad_len += len;
233 }
234 /* alg_group_enc */
235 if (ctx->alg_group_enc) {
236 if ((len = oscore_cbor_put_number(&external_aad_ptr,
237 &rem_size,
238 ctx->alg_group_enc)) == 0)
239 goto fail;
240 external_aad_len += len;
241 } else {
242 if ((len = oscore_cbor_put_nil(&external_aad_ptr, &rem_size)) == 0)
243 goto fail;
244 external_aad_len += len;
245 }
246 /* alg_signature */
247 if (ctx->alg_signature) {
248 if ((len = oscore_cbor_put_number(&external_aad_ptr, &rem_size, ctx->alg_signature)) == 0)
249 goto fail;
250 external_aad_len += len;
251 } else {
252 if ((len = oscore_cbor_put_nil(&external_aad_ptr, &rem_size)) == 0)
253 goto fail;
254 external_aad_len += len;
255 }
256 /* alg_pairwise_key_agreement */
257 if (ctx->alg_pairwise_key_agreement) {
258 if ((len = oscore_cbor_put_number(&external_aad_ptr,
259 &rem_size,
260 ctx->alg_pairwise_key_agreement)) == 0)
261 goto fail;
262 external_aad_len += len;
263 } else {
264 if ((len = oscore_cbor_put_nil(&external_aad_ptr, &rem_size)) == 0)
265 goto fail;
266 external_aad_len += len;
267 }
268 }
269#endif /* COAP_OSCORE_GROUP_SUPPORT */
270
271 /* request_kid */
272 if ((len = oscore_cbor_put_bytes(&external_aad_ptr,
273 &rem_size,
274 cose->key_id.s,
275 cose->key_id.length)) == 0)
276 goto fail;
277 external_aad_len += len;
278
279 /* request_piv */
280 if ((len = oscore_cbor_put_bytes(&external_aad_ptr,
281 &rem_size,
282 cose->partial_iv.s,
283 cose->partial_iv.length)) == 0)
284 goto fail;
285 external_aad_len += len;
286
287 /* options */
288 /* Put integrity protected options, at present there are none. */
289 if ((len = oscore_cbor_put_bytes(&external_aad_ptr, &rem_size, NULL, 0)) == 0)
290 goto fail;
291 external_aad_len += len;
292
293#if COAP_OSCORE_GROUP_SUPPORT
294 if (mode != OSCORE_MODE_SINGLE) {
295 /* request_kid_context */
296 external_aad_len += oscore_cbor_put_bytes(&external_aad_ptr,
297 &rem_size,
298 ctx->id_context->s,
299 ctx->id_context->length);
300 /* OSCORE_option */
301 external_aad_len += oscore_cbor_put_bytes(&external_aad_ptr,
302 &rem_size,
303 oscore_option,
304 oscore_option_len);
305 /* sender_cred */
306 if (sender_cred->cred_type == OSCORE_AUTH_CRED_CCS) {
307 external_aad_len += oscore_cbor_put_bytes(&external_aad_ptr,
308 &rem_size,
309 sender_cred->auth_cred->s,
310 sender_cred->auth_cred->length);
311 } else {
312 external_aad_len += oscore_cbor_put_bytes(&external_aad_ptr,
313 &rem_size,
314 sender_cred->pub_der->s,
315 sender_cred->pub_der->length);
316 }
317 /* gm_cred */
318 if (ctx->gm_cred)
319 if (ctx->gm_cred->cred_type == OSCORE_AUTH_CRED_CCS) {
320 external_aad_len +=
321 oscore_cbor_put_bytes(&external_aad_ptr,
322 &rem_size,
323 ctx->gm_cred->auth_cred->s,
324 ctx->gm_cred->auth_cred->length);
325 } else {
326 external_aad_len +=
327 oscore_cbor_put_bytes(&external_aad_ptr,
328 &rem_size,
329 ctx->gm_cred->pub_der->s,
330 ctx->gm_cred->pub_der->length);
331 } else
332 external_aad_len += oscore_cbor_put_nil(&external_aad_ptr, &rem_size);
333 }
334#endif /* COAP_OSCORE_GROUP_SUPPORT */
335 return external_aad_len;
336
337fail:
338 coap_log_info("oscore_prepare_e_aad: Buffer size too small: %" PRIuS "\n", external_aad_size);
339 return 0;
340}
341
342/*
343 * oscore_encode_option_value
344 */
345ssize_t
346oscore_encode_option_value(uint8_t *option_buffer,
347 size_t option_buf_len,
348 cose_encrypt0_t *cose,
349 uint8_t group_flag,
350 uint8_t appendix_b_2) {
351 size_t offset;
352 size_t rem_space;
353
354#if ! COAP_OSCORE_GROUP_SUPPORT
355 (void)group_flag;
356#endif /* ! COAP_OSCORE_GROUP_SUPPORT */
357 if (cose->partial_iv.length > 5 || option_buf_len == 0) {
358 return -1;
359 }
360 if (option_buf_len > 255)
361 option_buf_len = 255;
362#if COAP_OSCORE_GROUP_SUPPORT
363 if (group_flag == 1) {
364 option_buffer[0] = 0x20;
365 cose->group_flag = 1;
366 } else
367#endif /* COAP_OSCORE_GROUP_SUPPORT */
368 option_buffer[0] = 0;
369 offset = 1;
370 rem_space = option_buf_len - 1;
371
372 if (cose->partial_iv.length > 0 && cose->partial_iv.length <= 5 &&
373 cose->partial_iv.s != NULL) {
374 if (rem_space < cose->partial_iv.length)
375 return -1;
376 option_buffer[0] |= (0x07 & cose->partial_iv.length);
377 memcpy(&(option_buffer[offset]),
378 cose->partial_iv.s,
379 cose->partial_iv.length);
380 offset += cose->partial_iv.length;
381 rem_space -= cose->partial_iv.length;
382 }
383
384 if (cose->kid_context.length > 0 && cose->kid_context.s != NULL) {
385 if (appendix_b_2) {
386 /* Need to CBOR wrap kid_context - yuk! */
387 uint8_t *ptr = &option_buffer[offset+1];
388
389 if (rem_space < 1)
390 return -1;
391 option_buffer[0] |= 0x10;
392 option_buffer[offset] = (uint8_t)oscore_cbor_put_bytes(&ptr, &rem_space,
393 cose->kid_context.s,
394 cose->kid_context.length);
395 offset += option_buffer[offset] + 1;
396 /* rem_space updated by oscore_cbor_put_bytes() */
397 rem_space -= 1;
398 } else {
399 if (rem_space < 1 + cose->kid_context.length)
400 return -1;
401 option_buffer[0] |= 0x10;
402 option_buffer[offset] = (uint8_t)cose->kid_context.length;
403 offset++;
404 rem_space--;
405 memcpy(&(option_buffer[offset]),
406 cose->kid_context.s,
407 (uint8_t)cose->kid_context.length);
408 offset += cose->kid_context.length;
409 rem_space -= cose->kid_context.length;
410 }
411 }
412
413 if (cose->key_id.s != NULL) {
414 option_buffer[0] |= 0x08;
415 if (rem_space < cose->key_id.length)
416 return -1;
417 if (cose->key_id.length) {
418 memcpy(&(option_buffer[offset]), cose->key_id.s, cose->key_id.length);
419 offset += cose->key_id.length;
420 rem_space -= cose->key_id.length;
421 }
422 }
423
424 if (offset == 1 && option_buffer[0] == 0) {
425 /* If option_value is 0x00 it should be empty. */
426 offset = 0;
427 }
428 cose->oscore_option.s = option_buffer;
429 cose->oscore_option.length = offset;
430 return offset;
431}
432
433/*
434 * oscore_decode_option_value
435 * error: return 0
436 * OK: return 1
437 *
438 * Basic assumption is that all is preset to 0 or NULL on entry
439 */
440int
441oscore_decode_option_value(const uint8_t *opt_value,
442 size_t option_len,
443 cose_encrypt0_t *cose) {
444 uint8_t partial_iv_len;
445 size_t offset = 1;
446
447 cose->oscore_option.s = opt_value;
448 cose->oscore_option.length = option_len;
449
450 if (option_len == 0)
451 return 1; /* empty option */
452
453 partial_iv_len = (opt_value[0] & 0x07);
454 if (option_len > 255 || partial_iv_len == 6 || partial_iv_len == 7 ||
455 (opt_value[0] & 0xC0) != 0) {
456 return 0;
457 }
458
459 if ((opt_value[0] & 0x20) != 0) {
460#if COAP_OSCORE_GROUP_SUPPORT
461 cose->group_flag = 1;
462 } else {
463 cose->group_flag = 0;
464#else /* ! COAP_OSCORE_GROUP_SUPPORT */
465 return 0;
466#endif /* ! COAP_OSCORE_GROUP_SUPPORT */
467 }
468
469 if (partial_iv_len != 0) {
470 coap_bin_const_t partial_iv;
471 if (offset + partial_iv_len > option_len) {
472 return 0;
473 }
474 partial_iv.s = &(opt_value[offset]);
475 partial_iv.length = partial_iv_len;
476 cose_encrypt0_set_partial_iv(cose, &partial_iv);
477 offset += partial_iv_len;
478 }
479
480 if ((opt_value[0] & 0x10) != 0) {
481 coap_bin_const_t kid_context;
482
483 if (offset >= option_len)
484 return 0;
485 kid_context.length = opt_value[offset];
486 offset++;
487 if (offset + kid_context.length > option_len) {
488 return 0;
489 }
490 kid_context.s = &(opt_value[offset]);
491 cose_encrypt0_set_kid_context(cose, &kid_context);
492 offset = offset + kid_context.length;
493 }
494
495 if ((opt_value[0] & 0x08) != 0) {
496 coap_bin_const_t key_id;
497
498 key_id.length = option_len - offset;
499 if ((int)key_id.length < 0) {
500 return 0;
501 }
502 key_id.s = &(opt_value[offset]);
503 cose_encrypt0_set_key_id(cose, &key_id);
504 }
505 return 1;
506}
507
508#if COAP_OSCORE_GROUP_SUPPORT
509/* Sets alg and keys in COSE SIGN */
510void
511oscore_populate_sign(cose_sign1_t *sign,
512 oscore_ctx_t *ctx,
513 coap_crypto_cred_t *cred,
514 coap_crypto_pri_key_t *private_key) {
515 coap_bin_const_t sign_param = *ctx->sign_params;
516 coap_bin_const_t sign_type = *ctx->sign_params;
517
518 cose_sign1_set_alg(sign,
519 ctx->alg_signature,
520 extract_param(&sign_param.s, &sign_param.length),
521 extract_type(&sign_type.s, &sign_type.length));
522
523 if (private_key)
524 cose_sign1_set_private_key(sign, private_key);
525
526 cose_sign1_set_public_cred(sign, cred);
527}
528
529/*
530 * oscore_prepare_sig_structure
531 *
532 * creates and sets structure to be signed
533 */
534size_t
535oscore_prepare_sig_structure(uint8_t *sig_ptr,
536 size_t sig_size,
537 const uint8_t *e_aad_buffer,
538 uint16_t e_aad_len,
539 const uint8_t *text,
540 uint16_t text_len) {
541 size_t sig_len = 0;
542 size_t rem_size = sig_size;
543 char countersig0[] = "CounterSignature0";
544
545 (void)sig_size;
546 sig_len += oscore_cbor_put_array(&sig_ptr, &rem_size, 5);
547 /* 1. "CounterSignature0" */
548 sig_len += oscore_cbor_put_text(&sig_ptr,
549 &rem_size,
550 countersig0,
551 strlen(countersig0));
552 /* 2. Protected attributes from target structure */
553 sig_len += oscore_cbor_put_bytes(&sig_ptr, &rem_size, NULL, 0);
554 /* 3. Protected attributes from signer structure */
555 sig_len += oscore_cbor_put_bytes(&sig_ptr, &rem_size, NULL, 0);
556 /* 4. External AAD */
557 sig_len +=
558 oscore_cbor_put_bytes(&sig_ptr, &rem_size, e_aad_buffer, e_aad_len);
559 /* 5. Payload */
560 sig_len += oscore_cbor_put_bytes(&sig_ptr, &rem_size, text, text_len);
561 return sig_len;
562}
563#endif /* COAP_OSCORE_GROUP_SUPPORT */
564
565/*
566 * oscore_prepare_aad
567 *
568 * Creates and sets External AAD for encryption
569 */
570size_t
571oscore_prepare_aad(const uint8_t *external_aad_buffer,
572 size_t external_aad_len,
573 uint8_t *aad_buffer,
574 size_t aad_size) {
575 size_t ret = 0;
576 size_t len;
577 size_t rem_size = aad_size;
578 char encrypt0[] = "Encrypt0";
579
580 (void)aad_size; /* TODO */
581 /* Creating the AAD */
582 if ((len = oscore_cbor_put_array(&aad_buffer, &rem_size, 3)) == 0)
583 goto fail;
584 ret += len;
585
586 /* 1. "Encrypt0" */
587 if ((len = oscore_cbor_put_text(&aad_buffer, &rem_size, encrypt0, strlen(encrypt0))) == 0)
588 goto fail;
589 ret += len;
590
591 /* 2. Empty h'' entry */
592 if ((len = oscore_cbor_put_bytes(&aad_buffer, &rem_size, NULL, 0)) == 0)
593 goto fail;
594 ret += len;
595
596 /* 3. External AAD */
597 if ((len = oscore_cbor_put_bytes(&aad_buffer,
598 &rem_size,
599 external_aad_buffer,
600 external_aad_len)) == 0)
601 goto fail;
602 ret += len;
603
604 return ret;
605
606fail:
607 coap_log_info("oscore_prepare_aad: Buffer size too small: %" PRIuS "\n", aad_size);
608 return 0;
609}
610
611/*
612 * oscore_generate_nonce
613 *
614 * Creates Nonce
615 * See https://datatracker.ietf.org/doc/html/rfc8613#section-5.2 and Figure 8.
616 */
617void
619 oscore_ctx_t *ctx,
620 uint8_t *buffer,
621 uint8_t size) {
622 size_t tmp_len;
623
624 memset(buffer, 0, size);
625 /* ID_PIV */
626 if (ptr->key_id.length > size - 6UL) {
627 tmp_len = size - 6;
628 } else {
629 tmp_len = ptr->key_id.length;
630 }
631 if (ptr->key_id.s)
632 memcpy(&(buffer[((size - 5) - tmp_len)]),
633 ptr->key_id.s,
634 tmp_len);
635 /* S */
636 buffer[0] = (uint8_t)tmp_len;
637 /* PIV */
638 if (ptr->partial_iv.length > size) {
639 tmp_len = size;
640 } else {
641 tmp_len = ptr->partial_iv.length;
642 }
643 if (tmp_len > 5) {
644 tmp_len = 5;
645 }
646 if (tmp_len) {
647 memcpy(&(buffer[size - tmp_len]),
648 ptr->partial_iv.s,
649 tmp_len);
650 }
651 /* XOR */
652 for (int i = 0; i < size; i++) {
653 buffer[i] = buffer[i] ^ (uint8_t)ctx->common_iv->s[i];
654 }
655}
656
657/*
658 * oscore_validate_sender_seq
659 *
660 * Return 1 if OK, 0 otherwise
661 */
662uint8_t
664 uint64_t incoming_seq =
666
667 if (incoming_seq >= OSCORE_SEQ_MAX) {
668 coap_log_warn("OSCORE Replay protection, SEQ larger than SEQ_MAX.\n");
669 return 0;
670 }
671
672 ctx->rollback_last_seq = ctx->last_seq;
675 ctx->rollback_valid = 1;
676
677 /* Special case since we do not use unsigned int for seq */
678 if (ctx->initial_state == 1) {
679 ctx->initial_state = 0;
680 /* bitfield. B0 biggest seq seen. B1 seq-1 seen, B2 seq-2 seen etc. */
681 ctx->sliding_window = 1;
682 ctx->last_seq = incoming_seq;
683 } else if (incoming_seq > ctx->last_seq) {
684 /* Update the replay window */
685 uint64_t shift = incoming_seq - ctx->last_seq;
686 /* bitfield. B0 biggest seq seen. B1 seq-1 seen, B2 seq-2 seen etc. */
687 if (shift >= ctx->osc_ctx->replay_window_size || shift >= 64) {
688 ctx->sliding_window = 0;
689 } else {
690 ctx->sliding_window <<= shift;
691 }
692 ctx->sliding_window |= 1;
693 ctx->last_seq = incoming_seq;
694 } else if (incoming_seq == ctx->last_seq) {
695 coap_log_warn("OSCORE: Replay protection, replayed SEQ (%" PRIu64 ")\n",
696 incoming_seq);
697 return 0;
698 } else { /* incoming_seq < last_seq */
699 uint64_t shift = ctx->last_seq - incoming_seq;
700 uint64_t pattern;
701
702 if (shift >= ctx->osc_ctx->replay_window_size || shift >= 64) {
704 "OSCORE: Replay protection, SEQ outside of replay window (%" PRIu64
705 " %" PRIu64 ")\n",
706 ctx->last_seq, incoming_seq);
707 return 0;
708 }
709 /* seq + replay_window_size > last_seq */
710 pattern = 1ULL << shift;
711 if (ctx->sliding_window & pattern) {
712 coap_log_warn("OSCORE: Replay protection, replayed SEQ (%" PRIu64 ")\n",
713 incoming_seq);
714 return 0;
715 }
716 /* bitfield. B0 biggest seq seen. B1 seq-1 seen, B2 seq-2 seen etc. */
717 ctx->sliding_window |= pattern;
718 }
719 coap_log_oscore("OSCORE: window 0x%" PRIx64 " seq-B0 %" PRIu64 " SEQ %"
720 PRIu64 "\n",
721 ctx->sliding_window,
722 ctx->last_seq,
723 incoming_seq);
724 return 1;
725}
726
727/*
728 * oscore_increment_sender_seq
729 *
730 * Return 0 if SEQ MAX, return 1 if OK
731 */
732uint8_t
734 ctx->sender_context->seq++;
735
736 if (ctx->sender_context->seq >= OSCORE_SEQ_MAX) {
737 return 0;
738 } else {
739 return 1;
740 }
741}
742
743/*
744 * oscore_roll_back_seq
745 *
746 * Restore the sequence number and replay-window to the previous state. This
747 * is to be used when decryption fail.
748 */
749void
751
752 if (ctx->rollback_valid) {
754 ctx->last_seq = ctx->rollback_last_seq;
756 ctx->rollback_valid = 0;
757 }
758}
759
760#else /* ! COAP_OSCORE_SUPPORT */
761
762#ifdef __clang__
763/* Make compilers happy that do not like empty modules. As this function is
764 * never used, we ignore -Wunused-function at the end of compiling this file
765 */
766#pragma GCC diagnostic ignored "-Wunused-function"
767#endif
768static inline void
769dummy(void) {
770}
771
772#endif /* ! COAP_OSCORE_SUPPORT */
#define PRIuS
#define PRIx64
#define PRIu64
Library specific build wrapper for coap_internal.h.
@ 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.
#define NULL
Definition coap_option.h:30
uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition coap_encode.c:71
#define coap_log_oscore(...)
Definition coap_debug.h:132
#define coap_log_info(...)
Definition coap_debug.h:114
#define coap_log_warn(...)
Definition coap_debug.h:108
size_t oscore_cbor_put_text(uint8_t **buffer, size_t *buf_size, const char *text, size_t text_len)
uint64_t oscore_cbor_get_element_size(const uint8_t **buffer, size_t *buf_size)
size_t oscore_cbor_put_number(uint8_t **buffer, size_t *buf_size, int64_t value)
size_t oscore_cbor_put_nil(uint8_t **buffer, size_t *buf_size)
uint8_t oscore_cbor_get_next_element(const uint8_t **buffer, size_t *buf_size)
size_t oscore_cbor_put_unsigned(uint8_t **buffer, size_t *buf_size, uint64_t value)
size_t oscore_cbor_put_bytes(uint8_t **buffer, size_t *buf_size, const uint8_t *bytes, size_t bytes_len)
uint8_t oscore_cbor_get_number(const uint8_t **data, size_t *buf_size, int64_t *value)
#define CBOR_ARRAY
Definition oscore_cbor.h:66
size_t oscore_cbor_put_array(uint8_t **buffer, size_t *buf_size, size_t elements)
void cose_encrypt0_set_kid_context(cose_encrypt0_t *ptr, coap_bin_const_t *kid_context)
cose_curve_t
Definition oscore_cose.h:62
void cose_encrypt0_set_partial_iv(cose_encrypt0_t *ptr, coap_bin_const_t *partial_iv)
void cose_encrypt0_set_key_id(cose_encrypt0_t *ptr, coap_bin_const_t *key_id)
size_t oscore_prepare_aad(const uint8_t *external_aad_buffer, size_t external_aad_len, uint8_t *aad_buffer, size_t aad_size)
uint8_t oscore_validate_sender_seq(oscore_recipient_ctx_t *ctx, cose_encrypt0_t *cose)
#define OSCORE_SEQ_MAX
int oscore_decode_option_value(const uint8_t *option_value, size_t option_len, cose_encrypt0_t *cose)
oscore_mode_t
uint8_t oscore_increment_sender_seq(oscore_ctx_t *ctx)
void oscore_roll_back_seq(oscore_recipient_ctx_t *ctx)
uint8_t * oscore_cs_key_params(cose_curve_t param, int8_t param_type, size_t *len)
void oscore_generate_nonce(cose_encrypt0_t *ptr, oscore_ctx_t *ctx, uint8_t *buffer, uint8_t size)
uint8_t * oscore_cs_params(int8_t param, int8_t param_type, size_t *len)
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)
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.
@ OSCORE_MODE_SINGLE
Vanilla RFC 8613 support.
static void dummy(void)
Definition oscore.c:769
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
Holds credential definitions.
coap_bin_const_t * auth_cred
Authentication Credentials of type type.
coap_bin_const_t * pub_der
DER Value of the public key abstracted from key_tls.
Holds private key definitions.
coap_bin_const_t partial_iv
coap_bin_const_t kid_context
coap_bin_const_t key_id
coap_bin_const_t oscore_option
uint32_t replay_window_size
coap_bin_const_t * common_iv
Derived from Master Secret, Master Salt, and ID Context.
oscore_sender_ctx_t * sender_context
cose_alg_t aead_alg
Set to one of COSE_ALGORITHM_AES*.
coap_bin_const_t * id_context
contains GID in case of group
uint64_t seq
Sender Sequence Number.