libcoap 4.3.2rc2

oscore_cbor.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
47#include "coap3/coap_internal.h"
48#include <string.h>
49
50 static inline void
51 util_write_byte(uint8_t **buffer, size_t *buf_size, uint8_t value) {
52 assert(*buf_size >= 1);
53 (*buf_size)--;
54 **buffer = value;
55 (*buffer)++;
56}
57
58 size_t
59 oscore_cbor_put_nil(uint8_t **buffer, size_t *buf_size) {
60 util_write_byte(buffer, buf_size, 0xF6);
61 return 1;
62}
63
64 size_t
65 oscore_cbor_put_true(uint8_t **buffer, size_t *buf_size) {
66 util_write_byte(buffer, buf_size, 0xF5);
67 return 1;
68}
69
70 size_t
71 oscore_cbor_put_false(uint8_t **buffer, size_t *buf_size) {
72 util_write_byte(buffer, buf_size, 0xF4);
73 return 1;
74}
75
76 size_t
77 oscore_cbor_put_text(uint8_t **buffer,
78 size_t *buf_size,
79 const char *text,
80 size_t text_len) {
81 uint8_t *pt = *buffer;
82 size_t nb = oscore_cbor_put_unsigned(buffer, buf_size, text_len);
83 assert(*buf_size >= text_len);
84 (*buf_size) -= text_len;
85 *pt = (*pt | 0x60);
86 memcpy(*buffer, text, text_len);
87 (*buffer) += text_len;
88 return nb + text_len;
89}
90
91 size_t
92 oscore_cbor_put_array(uint8_t **buffer, size_t *buf_size, size_t elements) {
93 uint8_t *pt = *buffer;
94 size_t nb = oscore_cbor_put_unsigned(buffer, buf_size, elements);
95 *pt = (*pt | 0x80);
96 return nb;
97}
98
99 size_t
100 oscore_cbor_put_bytes(uint8_t **buffer,
101 size_t *buf_size,
102 const uint8_t *bytes,
103 size_t bytes_len) {
104 uint8_t *pt = *buffer;
105 size_t nb = oscore_cbor_put_unsigned(buffer, buf_size, bytes_len);
106 assert(*buf_size >= bytes_len);
107 (*buf_size) -= bytes_len;
108 *pt = (*pt | 0x40);
109 memcpy(*buffer, bytes, bytes_len);
110 (*buffer) += bytes_len;
111 return nb + bytes_len;
112}
113
114 size_t
115 oscore_cbor_put_map(uint8_t **buffer, size_t *buf_size, size_t elements) {
116 uint8_t *pt = *buffer;
117 size_t nb = oscore_cbor_put_unsigned(buffer, buf_size, elements);
118 *pt = (*pt | 0xa0);
119 return nb;
120}
121
122 size_t
123 oscore_cbor_put_number(uint8_t **buffer, size_t *buf_size, int64_t value) {
124 if (value < 0)
125 return oscore_cbor_put_negative(buffer, buf_size, -value);
126 else
127 return oscore_cbor_put_unsigned(buffer, buf_size, value);
128}
129
130 size_t
131 oscore_cbor_put_simple_value(uint8_t **buffer,
132 size_t *buf_size,
133 uint8_t value) {
134 uint8_t *pt = *buffer;
135 size_t nb = oscore_cbor_put_unsigned(buffer, buf_size, value);
136 *pt = (*pt | 0xe0);
137 return nb;
138}
139
140 size_t
141 oscore_cbor_put_tag(uint8_t **buffer, size_t *buf_size, uint64_t value) {
142 uint8_t *pt = *buffer;
143 size_t nb = oscore_cbor_put_unsigned(buffer, buf_size, value);
144 *pt = (*pt | 0xc0);
145 return nb;
146}
147
148 size_t
149 oscore_cbor_put_negative(uint8_t **buffer, size_t *buf_size, int64_t value) {
150 value--;
151 uint8_t *pt = *buffer;
152 size_t nb = oscore_cbor_put_unsigned(buffer, buf_size, value);
153 *pt = (*pt | 0x20);
154 return nb;
155}
156
157 static void
158 put_b_f(uint8_t **buffer, uint64_t value, uint8_t nr) {
159 uint8_t *pt = *buffer - 1;
160 uint64_t vv = value;
161 for (int q = nr; q > -1; q--) {
162 (*pt--) = (uint8_t)(vv & 0xff);
163 vv = (vv >> 8);
164 }
165}
166
167 size_t
168 oscore_cbor_put_unsigned(uint8_t **buffer, size_t *buf_size, uint64_t value) {
169 if (value < 0x18) { /* small value half a byte */
170 assert(*buf_size >= 1);
171 (*buf_size)--;
172 (**buffer) = (uint8_t)value;
173 (*buffer)++;
174 return 1;
175 } else if ((value > 0x17) && (value < 0x100)) {
176 /* one byte uint8_t */
177 assert(*buf_size >= 2);
178 (*buf_size) -= 2;
179 (**buffer) = (0x18);
180 *buffer = (*buffer) + 2;
181 put_b_f(buffer, value, 0);
182 return 2;
183 } else if ((value > 0xff) && (value < 0x10000)) {
184 /* 2 bytes uint16_t */
185 assert(*buf_size >= 3);
186 (*buf_size) -= 3;
187 (**buffer) = (0x19);
188 *buffer = (*buffer) + 3;
189 put_b_f(buffer, value, 1);
190 return 3;
191 } else if ((value > 0xffff) && (value < 0x100000000)) {
192 /* 4 bytes uint32_t */
193 assert(*buf_size >= 5);
194 (*buf_size) -= 5;
195 (**buffer) = (0x1a);
196 *buffer = (*buffer) + 5;
197 put_b_f(buffer, value, 3);
198 return 5;
199 } else { /*if(value > 0xffffffff)*/
200 /* 8 bytes uint64_t */
201 assert(*buf_size >= 9);
202 (*buf_size) -= 9;
203 (**buffer) = (0x1b);
204 *buffer = (*buffer) + 9;
205 put_b_f(buffer, value, 7);
206 return 9;
207 }
208}
209
210 static inline uint8_t
211 get_byte(const uint8_t **buffer, size_t *buf_len) {
212 assert((*buf_len) > 0);
213 return (*buffer)[0];
214}
215
216 static inline uint8_t
217 get_byte_inc(const uint8_t **buffer, size_t *buf_len) {
218 assert((*buf_len) > 0);
219 (*buf_len)--;
220 return ((*buffer)++)[0];
221}
222
223uint8_t
224 oscore_cbor_get_next_element(const uint8_t **buffer, size_t *buf_len) {
225 uint8_t element = get_byte(buffer, buf_len);
226 return element >> 5;
227}
228
229/* oscore_cbor_get_element_size returns
230 * - size of byte strings of character strings
231 * - size of array
232 * - size of map
233 * - value of unsigned integer
234 */
235
236 size_t
237 oscore_cbor_get_element_size(const uint8_t **buffer, size_t *buf_len) {
238 uint8_t control = get_byte(buffer, buf_len) & 0x1f;
239 size_t size = get_byte_inc(buffer, buf_len);
240
241 if (control < 0x18) {
242 size = (uint64_t)control;
243 } else {
244 control = control & 0x3;
245 int num = 1 << control;
246 size = 0;
247 size_t getal;
248 for (int i = 0; i < num; i++) {
249 getal = get_byte_inc(buffer, buf_len);
250 size = (size << 8) + getal;
251 }
252 }
253 return size;
254}
255
256uint8_t
257 oscore_cbor_elem_contained(const uint8_t *data, size_t *buf_len, uint8_t *end) {
258 const uint8_t *buf = data;
259 const uint8_t *last = data + oscore_cbor_get_element_size(&buf, buf_len);
260 if (last > end) {
261 coap_log_err("oscore_cbor_elem_contained returns 1 \n");
262 return 1;
263 } else
264 return 0;
265}
266
267int64_t
268 oscore_cbor_get_negative_integer(const uint8_t **buffer, size_t *buf_len) {
269 return -(int64_t)(oscore_cbor_get_element_size(buffer, buf_len) + 1);
270}
271
272uint64_t
273 oscore_cbor_get_unsigned_integer(const uint8_t **buffer, size_t *buf_len) {
274 return oscore_cbor_get_element_size(buffer, buf_len);
275}
276
277/*
278 * oscore_cbor_get_number
279 *
280 * gets a negative or positive number from data
281 * OK: return 0 ; NOK: return 1
282 */
283uint8_t
284 oscore_cbor_get_number(const uint8_t **data, size_t *buf_len, int64_t *value) {
285 uint8_t elem = oscore_cbor_get_next_element(data, buf_len);
286 if (elem == CBOR_UNSIGNED_INTEGER) {
287 *value = oscore_cbor_get_unsigned_integer(data, buf_len);
288 return 0;
289 } else if (elem == CBOR_NEGATIVE_INTEGER) {
290 *value = oscore_cbor_get_negative_integer(data, buf_len);
291 return 0;
292 } else
293 return 1;
294}
295
296/*
297 * oscore_cbor_get_simple_value
298 *
299 * gets a simple value from data
300 * OK: return 0 ; NOK: return 1
301 */
302uint8_t
303 oscore_cbor_get_simple_value(const uint8_t **data, size_t *buf_len, uint8_t *value) {
304 uint8_t elem = oscore_cbor_get_next_element(data, buf_len);
305 if (elem == CBOR_SIMPLE_VALUE) {
306 *value = get_byte_inc(data, buf_len) & 0x1f;
307 return 0;
308 } else
309 return 1;
310}
311
312 void
313 oscore_cbor_get_string(const uint8_t **buffer, size_t *buf_len, char *str, size_t size) {
314 (void)buf_len;
315 for (size_t i = 0; i < size; i++) {
316 *str++ = (char)get_byte_inc(buffer, buf_len);
317 }
318}
319
320 void
321 oscore_cbor_get_array(const uint8_t **buffer, size_t *buf_len, uint8_t *arr, size_t size) {
322 (void)buf_len;
323 for (size_t i = 0; i < size; i++) {
324 *arr++ = get_byte_inc(buffer, buf_len);
325 }
326}
327
328/* oscore_cbor_get_string_array
329 * fills the the size and the array from the cbor element
330 */
331uint8_t
332 oscore_cbor_get_string_array(const uint8_t **data, size_t *buf_len,
333 uint8_t **result,
334 size_t *len) {
335
336 uint8_t elem = oscore_cbor_get_next_element(data, buf_len);
337 *len = oscore_cbor_get_element_size(data, buf_len);
338 *result = NULL;
339 void *rs = coap_malloc_type(COAP_STRING, *len);
340 *result = (uint8_t *)rs;
341 if (elem == CBOR_TEXT_STRING) {
342 oscore_cbor_get_string(data, buf_len, (char *)*result, *len);
343 return 0;
344 } else if (elem == CBOR_BYTE_STRING) {
345 oscore_cbor_get_array(data, buf_len, *result, *len);
346 return 0; /* all is well */
347 } else {
348 free(*result);
349 *result = NULL;
350 return 1; /* failure */
351 }
352}
353
354/* oscore_cbor_skip value
355 * returns number of CBOR bytes
356 */
357 static size_t
358 oscore_cbor_skip_value(const uint8_t **data, size_t *buf_len) {
359 uint8_t elem = oscore_cbor_get_next_element(data, buf_len);
360 uint8_t control = get_byte(data, buf_len) & 0x1f;
361 size_t nb = 0; /* number of elements in array or map */
362 size_t num = 0; /* number of bytes of length or number */
363 size_t size = 0; /* size of value to be skipped */
364 if (control < 0x18) {
365 num = 1;
366 } else {
367 control = control & 0x3;
368 num = 1 + (1 << control);
369 }
370 switch (elem) {
371 case CBOR_UNSIGNED_INTEGER:
372 case CBOR_NEGATIVE_INTEGER:
373 assert((*buf_len) >= num);
374 *buf_len -= num;
375 *data = *data + num;
376 size = num;
377 break;
378 case CBOR_BYTE_STRING:
379 case CBOR_TEXT_STRING:
380 size = num;
381 size += oscore_cbor_get_element_size(data, buf_len);
382 assert((*buf_len) >= (size - num));
383 *buf_len -= (size - num);
384 (*data) = (*data) + size - num;
385 break;
386 case CBOR_ARRAY:
387 nb = oscore_cbor_get_element_size(data, buf_len);
388 size = num;
389 for (uint16_t qq = 0; qq < nb; qq++)
390 size += oscore_cbor_skip_value(data, buf_len);
391 break;
392 case CBOR_MAP:
393 nb = oscore_cbor_get_element_size(data, buf_len);
394 size = num;
395 for (uint16_t qq = 0; qq < nb; qq++) {
396 size += oscore_cbor_skip_value(data, buf_len);
397 size += oscore_cbor_skip_value(data, buf_len);
398 }
399 break;
400 case CBOR_TAG:
401 assert((*buf_len) >= 1);
402 *buf_len -= 1;
403 (*data)++;
404 size = 1;
405 break;
406 default:
407 return 0;
408 break;
409 } /* switch */
410 return size;
411}
412
413/* oscore_cbor_strip value
414 * strips the value of the cbor element into result
415 * and returns size
416 */
417uint8_t
418 oscore_cbor_strip_value(const uint8_t **data, size_t *buf_len, uint8_t **result, size_t *len) {
419 const uint8_t *st_data = *data;
420 size_t size = oscore_cbor_skip_value(data, buf_len);
421 *result = coap_malloc_type(COAP_STRING, size);
422 for (uint16_t qq = 0; qq < size; qq++)
423 (*result)[qq] = st_data[qq];
424 *len = size;
425 return 0;
426}
coap_internal.h
Pulls together all the internal only header files.
COAP_STRING
@ COAP_STRING
Definition: coap_mem.h:38
coap_malloc_type
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.
coap_log_err
#define coap_log_err(...)
Definition: coap_debug.h:96
CBOR_BYTE_STRING
#define CBOR_BYTE_STRING
Definition: oscore_cbor.h:62
oscore_cbor_put_text
size_t oscore_cbor_put_text(uint8_t **buffer, size_t *buf_size, const char *text, size_t text_len)
Definition: oscore_cbor.c:77
CBOR_TAG
#define CBOR_TAG
Definition: oscore_cbor.h:66
oscore_cbor_get_element_size
size_t oscore_cbor_get_element_size(const uint8_t **buffer, size_t *buf_len)
Definition: oscore_cbor.c:237
CBOR_NEGATIVE_INTEGER
#define CBOR_NEGATIVE_INTEGER
Definition: oscore_cbor.h:61
oscore_cbor_get_negative_integer
int64_t oscore_cbor_get_negative_integer(const uint8_t **buffer, size_t *buf_len)
Definition: oscore_cbor.c:268
CBOR_TEXT_STRING
#define CBOR_TEXT_STRING
Definition: oscore_cbor.h:63
CBOR_SIMPLE_VALUE
#define CBOR_SIMPLE_VALUE
Definition: oscore_cbor.h:67
oscore_cbor_put_number
size_t oscore_cbor_put_number(uint8_t **buffer, size_t *buf_size, int64_t value)
Definition: oscore_cbor.c:123
oscore_cbor_put_simple_value
size_t oscore_cbor_put_simple_value(uint8_t **buffer, size_t *buf_size, uint8_t value)
Definition: oscore_cbor.c:131
oscore_cbor_get_string_array
uint8_t oscore_cbor_get_string_array(const uint8_t **data, size_t *buf_len, uint8_t **result, size_t *len)
Definition: oscore_cbor.c:332
CBOR_UNSIGNED_INTEGER
#define CBOR_UNSIGNED_INTEGER
Definition: oscore_cbor.h:60
oscore_cbor_strip_value
uint8_t oscore_cbor_strip_value(const uint8_t **data, size_t *buf_len, uint8_t **result, size_t *len)
Definition: oscore_cbor.c:418
oscore_cbor_put_nil
size_t oscore_cbor_put_nil(uint8_t **buffer, size_t *buf_size)
Definition: oscore_cbor.c:59
oscore_cbor_get_string
void oscore_cbor_get_string(const uint8_t **buffer, size_t *buf_len, char *str, size_t size)
Definition: oscore_cbor.c:313
oscore_cbor_get_simple_value
uint8_t oscore_cbor_get_simple_value(const uint8_t **data, size_t *buf_len, uint8_t *value)
Definition: oscore_cbor.c:303
oscore_cbor_put_false
size_t oscore_cbor_put_false(uint8_t **buffer, size_t *buf_size)
Definition: oscore_cbor.c:71
oscore_cbor_get_next_element
uint8_t oscore_cbor_get_next_element(const uint8_t **buffer, size_t *buf_len)
Definition: oscore_cbor.c:224
oscore_cbor_put_negative
size_t oscore_cbor_put_negative(uint8_t **buffer, size_t *buf_size, int64_t value)
Definition: oscore_cbor.c:149
oscore_cbor_put_true
size_t oscore_cbor_put_true(uint8_t **buffer, size_t *buf_size)
Definition: oscore_cbor.c:65
oscore_cbor_put_unsigned
size_t oscore_cbor_put_unsigned(uint8_t **buffer, size_t *buf_size, uint64_t value)
Definition: oscore_cbor.c:168
oscore_cbor_put_bytes
size_t oscore_cbor_put_bytes(uint8_t **buffer, size_t *buf_size, const uint8_t *bytes, size_t bytes_len)
Definition: oscore_cbor.c:100
oscore_cbor_get_array
void oscore_cbor_get_array(const uint8_t **buffer, size_t *buf_len, uint8_t *arr, size_t size)
Definition: oscore_cbor.c:321
oscore_cbor_get_unsigned_integer
uint64_t oscore_cbor_get_unsigned_integer(const uint8_t **buffer, size_t *buf_len)
Definition: oscore_cbor.c:273
oscore_cbor_elem_contained
uint8_t oscore_cbor_elem_contained(const uint8_t *data, size_t *buf_len, uint8_t *end)
Definition: oscore_cbor.c:257
oscore_cbor_get_number
uint8_t oscore_cbor_get_number(const uint8_t **data, size_t *buf_len, int64_t *value)
Definition: oscore_cbor.c:284
CBOR_MAP
#define CBOR_MAP
Definition: oscore_cbor.h:65
CBOR_ARRAY
#define CBOR_ARRAY
Definition: oscore_cbor.h:64
oscore_cbor_put_array
size_t oscore_cbor_put_array(uint8_t **buffer, size_t *buf_size, size_t elements)
Definition: oscore_cbor.c:92
oscore_cbor_put_map
size_t oscore_cbor_put_map(uint8_t **buffer, size_t *buf_size, size_t elements)
Definition: oscore_cbor.c:115
oscore_cbor_put_tag
size_t oscore_cbor_put_tag(uint8_t **buffer, size_t *buf_size, uint64_t value)
Definition: oscore_cbor.c:141
get_byte
static uint8_t get_byte(const uint8_t **buffer, size_t *buf_len)
Definition: oscore_cbor.c:211
get_byte_inc
static uint8_t get_byte_inc(const uint8_t **buffer, size_t *buf_len)
Definition: oscore_cbor.c:217
util_write_byte
static void util_write_byte(uint8_t **buffer, size_t *buf_size, uint8_t value)
Definition: oscore_cbor.c:51
put_b_f
static void put_b_f(uint8_t **buffer, uint64_t value, uint8_t nr)
Definition: oscore_cbor.c:158
oscore_cbor_skip_value
static size_t oscore_cbor_skip_value(const uint8_t **data, size_t *buf_len)
Definition: oscore_cbor.c:358