1 /*
2    Copyright (c) 2021 Fraunhofer AISEC. See the COPYRIGHT
3    file at the top-level directory of this distribution.
4 
5    Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6    http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7    <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8    option. This file may not be copied, modified, or distributed
9    except according to those terms.
10 */
11 
12 #ifndef OPTION_H
13 #define OPTION_H
14 
15 #include <stdint.h>
16 
17 #include "oscore_coap.h"
18 
19 #include "common/byte_array.h"
20 #include "common/oscore_edhoc_error.h"
21 
22 enum o_coap_option_num {
23 	COAP_OPTION_IF_MATCH = 1,
24 	COAP_OPTION_URI_HOST = 3,
25 	COAP_OPTION_ETAG = 4,
26 	COAP_OPTION_IF_NONE_MATCH = 5,
27 	COAP_OPTION_OBSERVE = 6,
28 	COAP_OPTION_URI_PORT = 7,
29 	COAP_OPTION_LOCATION_PATH = 8,
30 	COAP_OPTION_OSCORE = 9,
31 	COAP_OPTION_URI_PATH = 11,
32 	COAP_OPTION_CONTENT_FORMAT = 12,
33 	COAP_OPTION_MAX_AGE = 14,
34 	COAP_OPTION_URI_QUERY = 15,
35 	COAP_OPTION_ACCEPT = 17,
36 	COAP_OPTION_LOCATION_QUERY = 20,
37 	COAP_OPTION_BLOCK2 = 23,
38 	COAP_OPTION_BLOCK1 = 27,
39 	COAP_OPTION_SIZE2 = 28,
40 	COAP_OPTION_PROXY_URI = 35,
41 	COAP_OPTION_PROXY_SCHEME = 39,
42 	COAP_OPTION_SIZE1 = 60,
43 };
44 
45 enum option_class {
46 	CLASS_U, /*unprotected*/
47 	CLASS_I, /*integrity protected only*/
48 	CLASS_E, /*encrypted and integrity protected*/
49 };
50 
51 /**
52  * @brief   Returns whether the CoAP Option with given `code` is a
53  *          Class E Option (encrypted)
54  * @param   code CoAP Option's code
55  * @return  true if the option is a Class E Option
56  */
57 bool is_class_e(uint16_t code);
58 
59 /**
60  * @brief Checks if an option belongs to a certain class
61  *
62  * @param option_num the option number
63  * @return true if the option belongs to a given class
64  * @return false if the option does not belong to a given class
65  */
66 bool option_belongs_to_class(uint16_t option_num, enum option_class class);
67 
68 /**
69  * @brief   Parses the passed options until the payload marker of end of
70  *          array and writes them into @a out.
71  *          Returns the number of parsed options and writes the number of
72  *          bytes consumed into @a offset_out. If @a out is NULL, this function
73  *          doesn't write parsed options, but still returns the number
74  *          of options.
75  * @param   options
76  * @param   out Out-array. Must be at least `num_options(...)` long or NULL.
77  * @param   offset_out Pointer to write byte-length of options into.
78  *          Can be NULL.
79  * @return  err
80  */
81 enum err decode_options(struct byte_array options, struct o_coap_option *out,
82 			uint16_t *offset_out);
83 
84 /**
85  * @brief   Returns the length in bytes of the serialized options
86  *          of given class.
87  * @param   options CoAP Option array containing all options
88  *          (possibly including ones of other classes)
89  * @param   opt_num Number of CoAP options in @a options.
90  * @param   class Class of the options to encode
91  * @return  length in bytes
92  */
93 uint32_t encoded_option_len(struct o_coap_option *options, uint16_t opt_num,
94 			    enum option_class class);
95 
96 /**
97  * @brief   Encodes all options in given array having given class.
98  * @param   options CoAP Option array containing all options
99  *          (possibly including ones of other classes)
100  * @param   opt_num Number of CoAP options in @a options.
101  * @param   class Class of the options to encode
102  * @param   out out-pointer. Must be at least `encoded_option_len(...)`
103  *          bytes long.
104  * @param   out_buf_len the length of of the out buffer
105  * @return  err
106  */
107 enum err encode_options(struct o_coap_option *options, uint16_t opt_num,
108 			enum option_class class, uint8_t *out,
109 			uint32_t out_buf_len);
110 
111 #endif
112