libcoap 4.3.5-develop-dfd5545
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/*
91 * Build the CBOR for external_aad
92 *
93 * external_aad = bstr .cbor aad_array
94 *
95 * No group mode
96 * aad_array = [
97 * oscore_version : uint,
98 * algorithms : [ alg_aead : int / tstr ],
99 * request_kid : bstr,
100 * request_piv : bstr,
101 * options : bstr,
102 * ]
103 *
104 * Group mode
105 * aad_array = [
106 * oscore_version : uint,
107 * algorithms : [alg_aead : int / tstr / null,
108 * alg_signature_enc : int / tstr / null,
109 * alg_signature : int / tstr / null,
110 * alg_pairwise_key_agreement : int / tstr / null],
111 * request_kid : bstr,
112 * request_piv : bstr,
113 * options : bstr,
114 * request_kid_context : bstr,
115 * OSCORE_option: bstr,
116 * sender_public_key: bstr, (initiator's key)
117 * gm_public_key: bstr / null
118 * ]
119 */
120size_t
122 cose_encrypt0_t *cose,
123 const uint8_t *oscore_option,
124 size_t oscore_option_len,
125 coap_bin_const_t *sender_public_key,
126 uint8_t *external_aad_ptr,
127 size_t external_aad_size) {
128 size_t external_aad_len = 0;
129 size_t rem_size = external_aad_size;
130
131 (void)oscore_option;
132 (void)oscore_option_len;
133 (void)sender_public_key;
134
135 external_aad_len += oscore_cbor_put_array(&external_aad_ptr, &rem_size, 5);
136
137 /* oscore_version, always "1" */
138 external_aad_len += oscore_cbor_put_unsigned(&external_aad_ptr, &rem_size, 1);
139
140 /* Algorithms array with one item*/
141 external_aad_len += oscore_cbor_put_array(&external_aad_ptr, &rem_size, 1);
142 /* Encryption Algorithm */
143 external_aad_len +=
144 oscore_cbor_put_number(&external_aad_ptr, &rem_size, ctx->aead_alg);
145 /* request_kid */
146 external_aad_len += oscore_cbor_put_bytes(&external_aad_ptr,
147 &rem_size,
148 cose->key_id.s,
149 cose->key_id.length);
150 /* request_piv */
151 external_aad_len += oscore_cbor_put_bytes(&external_aad_ptr,
152 &rem_size,
153 cose->partial_iv.s,
154 cose->partial_iv.length);
155 /* options */
156 /* Put integrity protected options, at present there are none. */
157 external_aad_len +=
158 oscore_cbor_put_bytes(&external_aad_ptr, &rem_size, NULL, 0);
159
160 return external_aad_len;
161}
162
163/*
164 * oscore_encode_option_value
165 */
166ssize_t
167oscore_encode_option_value(uint8_t *option_buffer,
168 size_t option_buf_len,
169 cose_encrypt0_t *cose,
170 uint8_t group_flag,
171 uint8_t appendix_b_2) {
172 size_t offset;
173 size_t rem_space;
174
175 (void)group_flag;
176 if (cose->partial_iv.length > 5 || option_buf_len == 0) {
177 return -1;
178 }
179 if (option_buf_len > 255)
180 option_buf_len = 255;
181 option_buffer[0] = 0;
182 offset = 1;
183 rem_space = option_buf_len - 1;
184
185 if (cose->partial_iv.length > 0 && cose->partial_iv.length <= 5 &&
186 cose->partial_iv.s != NULL) {
187 if (rem_space < cose->partial_iv.length)
188 return -1;
189 option_buffer[0] |= (0x07 & cose->partial_iv.length);
190 memcpy(&(option_buffer[offset]),
191 cose->partial_iv.s,
192 cose->partial_iv.length);
193 offset += cose->partial_iv.length;
194 rem_space -= cose->partial_iv.length;
195 }
196
197 if (cose->kid_context.length > 0 && cose->kid_context.s != NULL) {
198 if (appendix_b_2) {
199 /* Need to CBOR wrap kid_context - yuk! */
200 uint8_t *ptr = &option_buffer[offset+1];
201
202 if (rem_space < 1)
203 return -1;
204 option_buffer[0] |= 0x10;
205 option_buffer[offset] = (uint8_t)oscore_cbor_put_bytes(&ptr, &rem_space,
206 cose->kid_context.s,
207 cose->kid_context.length);
208 offset += option_buffer[offset] + 1;
209 /* rem_space updated by oscore_cbor_put_bytes() */
210 rem_space -= 1;
211 } else {
212 if (rem_space < 1 + cose->kid_context.length)
213 return -1;
214 option_buffer[0] |= 0x10;
215 option_buffer[offset] = (uint8_t)cose->kid_context.length;
216 offset++;
217 rem_space--;
218 memcpy(&(option_buffer[offset]),
219 cose->kid_context.s,
220 (uint8_t)cose->kid_context.length);
221 offset += cose->kid_context.length;
222 rem_space -= cose->kid_context.length;
223 }
224 }
225
226 if (cose->key_id.s != NULL) {
227 option_buffer[0] |= 0x08;
228 if (rem_space < cose->key_id.length)
229 return -1;
230 if (cose->key_id.length) {
231 memcpy(&(option_buffer[offset]), cose->key_id.s, cose->key_id.length);
232 offset += cose->key_id.length;
233 rem_space -= cose->key_id.length;
234 }
235 }
236
237 if (offset == 1 && option_buffer[0] == 0) {
238 /* If option_value is 0x00 it should be empty. */
239 offset = 0;
240 }
241 cose->oscore_option.s = option_buffer;
242 cose->oscore_option.length = offset;
243 return offset;
244}
245
246/*
247 * oscore_decode_option_value
248 * error: return 0
249 * OK: return 1
250 *
251 * Basic assumption is that all is preset to 0 or NULL on entry
252 */
253int
254oscore_decode_option_value(const uint8_t *opt_value,
255 size_t option_len,
256 cose_encrypt0_t *cose) {
257 uint8_t partial_iv_len = (opt_value[0] & 0x07);
258 size_t offset = 1;
259
260 cose->oscore_option.s = opt_value;
261 cose->oscore_option.length = option_len;
262
263 if (option_len == 0)
264 return 1; /* empty option */
265
266 if (option_len > 255 || partial_iv_len == 6 || partial_iv_len == 7 ||
267 (opt_value[0] & 0xC0) != 0) {
268 return 0;
269 }
270
271 if ((opt_value[0] & 0x20) != 0) {
272 return 0;
273 }
274
275 if (partial_iv_len != 0) {
276 coap_bin_const_t partial_iv;
277 if (offset + partial_iv_len > option_len) {
278 return 0;
279 }
280 partial_iv.s = &(opt_value[offset]);
281 partial_iv.length = partial_iv_len;
282 cose_encrypt0_set_partial_iv(cose, &partial_iv);
283 offset += partial_iv_len;
284 }
285
286 if ((opt_value[0] & 0x10) != 0) {
287 coap_bin_const_t kid_context;
288
289 if (offset >= option_len)
290 return 0;
291 kid_context.length = opt_value[offset];
292 offset++;
293 if (offset + kid_context.length > option_len) {
294 return 0;
295 }
296 kid_context.s = &(opt_value[offset]);
297 cose_encrypt0_set_kid_context(cose, &kid_context);
298 offset = offset + kid_context.length;
299 }
300
301 if ((opt_value[0] & 0x08) != 0) {
302 coap_bin_const_t key_id;
303
304 key_id.length = option_len - offset;
305 if ((int)key_id.length < 0) {
306 return 0;
307 }
308 key_id.s = &(opt_value[offset]);
309 cose_encrypt0_set_key_id(cose, &key_id);
310 }
311 return 1;
312}
313
314/*
315 * oscore_prepare_aad
316 *
317 * Creates and sets External AAD for encryption
318 */
319size_t
320oscore_prepare_aad(const uint8_t *external_aad_buffer,
321 size_t external_aad_len,
322 uint8_t *aad_buffer,
323 size_t aad_size) {
324 size_t ret = 0;
325 size_t rem_size = aad_size;
326 char encrypt0[] = "Encrypt0";
327
328 (void)aad_size; /* TODO */
329 /* Creating the AAD */
330 ret += oscore_cbor_put_array(&aad_buffer, &rem_size, 3);
331 /* 1. "Encrypt0" */
332 ret +=
333 oscore_cbor_put_text(&aad_buffer, &rem_size, encrypt0, strlen(encrypt0));
334 /* 2. Empty h'' entry */
335 ret += oscore_cbor_put_bytes(&aad_buffer, &rem_size, NULL, 0);
336 /* 3. External AAD */
337 ret += oscore_cbor_put_bytes(&aad_buffer,
338 &rem_size,
339 external_aad_buffer,
340 external_aad_len);
341
342 return ret;
343}
344
345/*
346 * oscore_generate_nonce
347 *
348 * Creates Nonce
349 * See https://datatracker.ietf.org/doc/html/rfc8613#section-5.2 and Figure 8.
350 */
351void
353 oscore_ctx_t *ctx,
354 uint8_t *buffer,
355 uint8_t size) {
356 size_t tmp_len;
357
358 memset(buffer, 0, size);
359 /* ID_PIV */
360 if (ptr->key_id.length > size - 6UL) {
361 tmp_len = size - 6;
362 } else {
363 tmp_len = ptr->key_id.length;
364 }
365 if (ptr->key_id.s)
366 memcpy(&(buffer[((size - 5) - tmp_len)]),
367 ptr->key_id.s,
368 tmp_len);
369 /* S */
370 buffer[0] = (uint8_t)tmp_len;
371 /* PIV */
372 if (ptr->partial_iv.length > size) {
373 tmp_len = size;
374 } else {
375 tmp_len = ptr->partial_iv.length;
376 }
377 if (tmp_len > 5) {
378 tmp_len = 5;
379 }
380 if (tmp_len) {
381 memcpy(&(buffer[size - tmp_len]),
382 ptr->partial_iv.s,
383 tmp_len);
384 }
385 /* XOR */
386 for (int i = 0; i < size; i++) {
387 buffer[i] = buffer[i] ^ (uint8_t)ctx->common_iv->s[i];
388 }
389}
390
391/*
392 * oscore_validate_sender_seq
393 *
394 * Return 1 if OK, 0 otherwise
395 */
396uint8_t
398 uint64_t incoming_seq =
400
401 if (incoming_seq >= OSCORE_SEQ_MAX) {
402 coap_log_warn("OSCORE Replay protection, SEQ larger than SEQ_MAX.\n");
403 return 0;
404 }
405
406 ctx->rollback_last_seq = ctx->last_seq;
408
409 /* Special case since we do not use unsigned int for seq */
410 if (ctx->initial_state == 1) {
411 ctx->initial_state = 0;
412 /* bitfield. B0 biggest seq seen. B1 seq-1 seen, B2 seq-2 seen etc. */
413 ctx->sliding_window = 1;
414 ctx->last_seq = incoming_seq;
415 } else if (incoming_seq > ctx->last_seq) {
416 /* Update the replay window */
417 uint64_t shift = incoming_seq - ctx->last_seq;
418 /* bitfield. B0 biggest seq seen. B1 seq-1 seen, B2 seq-2 seen etc. */
419 if (shift >= ctx->osc_ctx->replay_window_size || shift >= 64) {
420 ctx->sliding_window = 0;
421 } else {
422 ctx->sliding_window <<= shift;
423 }
424 ctx->sliding_window |= 1;
425 ctx->last_seq = incoming_seq;
426 } else if (incoming_seq == ctx->last_seq) {
427 coap_log_warn("OSCORE: Replay protection, replayed SEQ (%" PRIu64 ")\n",
428 incoming_seq);
429 return 0;
430 } else { /* incoming_seq < last_seq */
431 uint64_t shift = ctx->last_seq - incoming_seq;
432 uint64_t pattern;
433
434 if (shift >= ctx->osc_ctx->replay_window_size || shift >= 64) {
436 "OSCORE: Replay protection, SEQ outside of replay window (%" PRIu64
437 " %" PRIu64 ")\n",
438 ctx->last_seq, incoming_seq);
439 return 0;
440 }
441 /* seq + replay_window_size > last_seq */
442 pattern = 1ULL << shift;
443 if (ctx->sliding_window & pattern) {
444 coap_log_warn("OSCORE: Replay protection, replayed SEQ (%" PRIu64 ")\n",
445 incoming_seq);
446 return 0;
447 }
448 /* bitfield. B0 biggest seq seen. B1 seq-1 seen, B2 seq-2 seen etc. */
449 ctx->sliding_window |= pattern;
450 }
451 coap_log_oscore("OSCORE: window 0x%" PRIx64 " seq-B0 %" PRIu64 " SEQ %"
452 PRIu64 "\n",
453 ctx->sliding_window,
454 ctx->last_seq,
455 incoming_seq);
456 return 1;
457}
458
459/*
460 * oscore_increment_sender_seq
461 *
462 * Return 0 if SEQ MAX, return 1 if OK
463 */
464uint8_t
466 ctx->sender_context->seq++;
467
468 if (ctx->sender_context->seq >= OSCORE_SEQ_MAX) {
469 return 0;
470 } else {
471 return 1;
472 }
473}
474
475/*
476 * oscore_roll_back_seq
477 *
478 * Restore the sequence number and replay-window to the previous state. This
479 * is to be used when decryption fail.
480 */
481void
483
484 if (ctx->rollback_sliding_window != 0) {
487 }
488 if (ctx->rollback_last_seq != 0) {
489 ctx->last_seq = ctx->rollback_last_seq;
490 ctx->rollback_last_seq = 0;
491 }
492}
493
494#else /* ! COAP_OSCORE_SUPPORT */
495
496#ifdef __clang__
497/* Make compilers happy that do not like empty modules. As this function is
498 * never used, we ignore -Wunused-function at the end of compiling this file
499 */
500#pragma GCC diagnostic ignored "-Wunused-function"
501#endif
502static inline void
503dummy(void) {
504}
505
506#endif /* ! COAP_OSCORE_SUPPORT */
#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_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)
size_t oscore_cbor_put_number(uint8_t **buffer, size_t *buf_size, int64_t value)
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)
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)
uint8_t oscore_increment_sender_seq(oscore_ctx_t *ctx)
void oscore_roll_back_seq(oscore_recipient_ctx_t *ctx)
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)
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)
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.
static void dummy(void)
Definition oscore.c:503
CoAP binary data definition with const data.
Definition coap_str.h:65
size_t length
length of binary data
Definition coap_str.h:66
const uint8_t * s
read-only binary data
Definition coap_str.h:67
coap_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*.
uint64_t seq
Sender Sequence Number.