Branch data Line data Source code
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 : : #include <stdint.h>
13 : : #include <stdbool.h>
14 : :
15 : : #include "edhoc/retrieve_cred.h"
16 : : #include "edhoc/plaintext.h"
17 : : #include "edhoc/signature_or_mac_msg.h"
18 : : #include "edhoc/int_encode_decode.h"
19 : :
20 : : #include "common/oscore_edhoc_error.h"
21 : : #include "common/memcpy_s.h"
22 : : #include "common/print_util.h"
23 : :
24 : : #include "cbor/edhoc_decode_plaintext2.h"
25 : : #include "cbor/edhoc_decode_plaintext3.h"
26 : : #include "cbor/edhoc_encode_id_cred_x.h"
27 : :
28 : : /**
29 : : * @brief Encodes ID_CRED_x as a CBOR map.
30 : : * @param label The CBOR map label.
31 : : * @param algo The EDHOC hash algorithm used in x5t. This
32 : : * parameter can take any other value when xchain
33 : : * or kid are used.
34 : : * @param id The actual credential identifier.
35 : : * @param id_len Length of id.
36 : : * @param[out] id_cred_x The encoded value.
37 : : * @retval Ok or error.
38 : : */
39 : 14 : static enum err id_cred_x_encode(enum id_cred_x_label label, int algo,
40 : : const void *id, uint32_t id_len,
41 : : struct byte_array *id_cred_x)
42 : : {
43 : 14 : struct id_cred_x_map map = { 0 };
44 : : size_t payload_len_out;
45 : :
46 [ + + + - ]: 14 : switch (label) {
47 : 8 : case kid:
48 : 8 : map.id_cred_x_map_kid_present = true;
49 : 8 : map.id_cred_x_map_kid.id_cred_x_map_kid_choice =
50 : : id_cred_x_map_kid_bstr_c;
51 : 8 : map.id_cred_x_map_kid.id_cred_x_map_kid_bstr.value = id;
52 : 8 : map.id_cred_x_map_kid.id_cred_x_map_kid_bstr.len = id_len;
53 : 8 : break;
54 : 2 : case x5chain:
55 : 2 : map.id_cred_x_map_x5chain_present = true;
56 : 2 : map.id_cred_x_map_x5chain.id_cred_x_map_x5chain.value = id;
57 : 2 : map.id_cred_x_map_x5chain.id_cred_x_map_x5chain.len = id_len;
58 : 2 : break;
59 : 4 : case x5t:
60 : 4 : map.id_cred_x_map_x5t_present = true;
61 : 4 : map.id_cred_x_map_x5t.id_cred_x_map_x5t_alg_choice =
62 : : id_cred_x_map_x5t_alg_int_c;
63 : 4 : map.id_cred_x_map_x5t.id_cred_x_map_x5t_alg_int = algo;
64 : 4 : map.id_cred_x_map_x5t.id_cred_x_map_x5t_hash.value = id;
65 : 4 : map.id_cred_x_map_x5t.id_cred_x_map_x5t_hash.len = id_len;
66 : 4 : break;
67 : 0 : default:
68 : 0 : break;
69 : : }
70 : :
71 [ - + ]: 14 : TRY_EXPECT(cbor_encode_id_cred_x_map(id_cred_x->ptr, id_cred_x->len,
72 : : &map, &payload_len_out),
73 : : 0);
74 : :
75 : 14 : id_cred_x->len = (uint32_t)payload_len_out;
76 : :
77 : 14 : return ok;
78 : : }
79 : :
80 : : /**
81 : : * @brief Reconstructs ID_CRED_x = { 4 : kid } from a kid
82 : : * that was conveyed as a CBOR int.
83 : : * @param kid_int The int from the plaintext.
84 : : * @param[out] id_cred_x The encoded ID_CRED_x.
85 : : * @retval Ok or error.
86 : : */
87 : 7 : static enum err kid_int2id_cred(int32_t kid_int, struct byte_array *id_cred_x)
88 : : {
89 [ + - + + ]: 7 : if (kid_int >= -24 && kid_int <= 23) {
90 : : /* The int stands for the byte string that holds its CBOR
91 : : * encoding (RFC 9528 Section 3.3.2). */
92 [ - + ]: 6 : BYTE_ARRAY_NEW(kid_bstr, 1, 1);
93 [ - + ]: 6 : TRY(encode_int(&kid_int, 1, &kid_bstr));
94 : 6 : return id_cred_x_encode(kid, 0, kid_bstr.ptr, kid_bstr.len,
95 : : id_cred_x);
96 : : }
97 : :
98 : : /* Not a valid compact kid in RFC 9528; keep the int, as in drafts
99 : : * before RFC 9528. */
100 : 1 : struct id_cred_x_map map = { 0 };
101 : : size_t payload_len_out;
102 : :
103 : 1 : map.id_cred_x_map_kid_present = true;
104 : 1 : map.id_cred_x_map_kid.id_cred_x_map_kid_choice =
105 : : id_cred_x_map_kid_int_c;
106 : 1 : map.id_cred_x_map_kid.id_cred_x_map_kid_int = kid_int;
107 [ - + ]: 1 : TRY_EXPECT(cbor_encode_id_cred_x_map(id_cred_x->ptr, id_cred_x->len,
108 : : &map, &payload_len_out),
109 : : 0);
110 : 1 : id_cred_x->len = (uint32_t)payload_len_out;
111 : :
112 : 1 : return ok;
113 : : }
114 : :
115 : 5 : static enum err plaintext2_split(struct byte_array *ptxt,
116 : : struct byte_array *c_r,
117 : : struct byte_array *id_cred_r,
118 : : struct byte_array *sign_or_mac,
119 : : struct byte_array *ead)
120 : : {
121 : 5 : size_t decode_len = 0;
122 : : struct ptxt2 p;
123 : :
124 [ - + ]: 5 : TRY_EXPECT(cbor_decode_ptxt2(ptxt->ptr, ptxt->len, &p, &decode_len), 0);
125 : :
126 : : /*decode C_R*/
127 [ + + ]: 5 : if (p.ptxt2_C_R_choice == ptxt2_C_R_bstr_c) {
128 [ - + ]: 2 : TRY(_memcpy_s(c_r->ptr, c_r->len, p.ptxt2_C_R_bstr.value,
129 : : (uint32_t)p.ptxt2_C_R_bstr.len));
130 : 2 : c_r->len = (uint32_t)p.ptxt2_C_R_bstr.len;
131 : : } else {
132 : : /*provide C_R in encoded form if it was an int*/
133 : : /*this is how it C_R was chosen by the responder*/
134 [ - + ]: 3 : TRY(c_x_int_encode(p.ptxt2_C_R_int, c_r));
135 : : }
136 : :
137 : : /*ID_CRED_R*/
138 [ + + ]: 5 : if (p.ptxt2_ID_CRED_R_choice == ptxt2_ID_CRED_R_map2_m_c) {
139 [ + + ]: 3 : if (p.ptxt2_ID_CRED_R_map2_m.map2_x5chain_present) {
140 : 1 : PRINT_MSG("ID_CRED_R is x5chain\n");
141 [ - + ]: 1 : TRY(id_cred_x_encode(
142 : : x5chain, 0,
143 : : p.ptxt2_ID_CRED_R_map2_m.map2_x5chain
144 : : .map2_x5chain.value,
145 : : (uint32_t)p.ptxt2_ID_CRED_R_map2_m.map2_x5chain
146 : : .map2_x5chain.len,
147 : : id_cred_r));
148 : : }
149 [ + + ]: 3 : if (p.ptxt2_ID_CRED_R_map2_m.map2_x5t_present) {
150 : 2 : PRINT_MSG("ID_CRED_R is x5t\n");
151 [ - + ]: 2 : TRY(id_cred_x_encode(x5t,
152 : : p.ptxt2_ID_CRED_R_map2_m.map2_x5t
153 : : .map2_x5t_alg_int,
154 : : p.ptxt2_ID_CRED_R_map2_m.map2_x5t
155 : : .map2_x5t_hash.value,
156 : : (uint32_t)p.ptxt2_ID_CRED_R_map2_m
157 : : .map2_x5t.map2_x5t_hash.len,
158 : : id_cred_r));
159 : : }
160 : : } else {
161 : : /*Note that if ID_CRED_x contains a single 'kid' parameter,
162 : : i.e., ID_CRED_R = { 4 : kid_x }, only the byte string kid_x
163 : : is conveyed in the plaintext encoded as a bstr or int*/
164 [ - + ]: 2 : if (p.ptxt2_ID_CRED_R_choice == ptxt2_ID_CRED_R_bstr_c) {
165 [ # # ]: 0 : TRY(id_cred_x_encode(
166 : : kid, 0, p.ptxt2_ID_CRED_R_bstr.value,
167 : : (uint32_t)p.ptxt2_ID_CRED_R_bstr.len,
168 : : id_cred_r));
169 : :
170 : : } else {
171 [ - + ]: 2 : TRY(kid_int2id_cred(p.ptxt2_ID_CRED_R_int, id_cred_r));
172 : : }
173 : : }
174 [ - + ]: 5 : TRY(_memcpy_s(sign_or_mac->ptr, sign_or_mac->len,
175 : : p.ptxt2_SGN_or_MAC_2.value,
176 : : (uint32_t)p.ptxt2_SGN_or_MAC_2.len));
177 : 5 : sign_or_mac->len = (uint32_t)p.ptxt2_SGN_or_MAC_2.len;
178 : :
179 [ - + ]: 5 : if (p.ptxt2_EAD_2_present == true) {
180 [ # # ]: 0 : TRY(_memcpy_s(ead->ptr, ead->len, p.ptxt2_EAD_2.value,
181 : : (uint32_t)p.ptxt2_EAD_2.len));
182 : 0 : ead->len = (uint32_t)p.ptxt2_EAD_2.len;
183 : : } else {
184 [ - + ]: 5 : if (ead->len) {
185 : 0 : ead->len = 0;
186 : : }
187 : : }
188 : :
189 : 5 : return ok;
190 : : }
191 : :
192 : 10 : static enum err plaintext3_split(struct byte_array *ptxt,
193 : : struct byte_array *id_cred_i,
194 : : struct byte_array *sign_or_mac,
195 : : struct byte_array *ead)
196 : : {
197 : 10 : size_t decode_len = 0;
198 : : struct ptxt3 p;
199 : :
200 [ - + ]: 10 : TRY_EXPECT(cbor_decode_ptxt3(ptxt->ptr, ptxt->len, &p, &decode_len), 0);
201 : :
202 : : /*ID_CRED_I*/
203 [ + + ]: 10 : if (p.ptxt3_ID_CRED_I_choice == ptxt3_ID_CRED_I_map3_m_c) {
204 [ + + ]: 3 : if (p.ptxt3_ID_CRED_I_map3_m.map3_x5chain_present) {
205 : 1 : PRINT_MSG("ID_CRED_I is x5chain\n");
206 [ - + ]: 1 : TRY(id_cred_x_encode(
207 : : x5chain, 0,
208 : : p.ptxt3_ID_CRED_I_map3_m.map3_x5chain
209 : : .map3_x5chain.value,
210 : : (uint32_t)p.ptxt3_ID_CRED_I_map3_m.map3_x5chain
211 : : .map3_x5chain.len,
212 : : id_cred_i));
213 : : }
214 [ + + ]: 3 : if (p.ptxt3_ID_CRED_I_map3_m.map3_x5t_present) {
215 : 2 : PRINT_MSG("ID_CRED_I is x5t\n");
216 [ - + ]: 2 : TRY(id_cred_x_encode(x5t,
217 : : p.ptxt3_ID_CRED_I_map3_m.map3_x5t
218 : : .map3_x5t_alg_int,
219 : : p.ptxt3_ID_CRED_I_map3_m.map3_x5t
220 : : .map3_x5t_hash.value,
221 : : (uint32_t)p.ptxt3_ID_CRED_I_map3_m
222 : : .map3_x5t.map3_x5t_hash.len,
223 : : id_cred_i));
224 : : }
225 : : } else {
226 : : /*Note that if ID_CRED_x contains a single 'kid' parameter,
227 : : i.e., ID_CRED_I = { 4 : kid_x }, only the byte string kid_x
228 : : is conveyed in the plaintext encoded as a bstr or int*/
229 [ + + ]: 7 : if (p.ptxt3_ID_CRED_I_choice == ptxt3_ID_CRED_I_bstr_c) {
230 [ - + ]: 2 : TRY(id_cred_x_encode(
231 : : kid, 0, p.ptxt3_ID_CRED_I_bstr.value,
232 : : (uint32_t)p.ptxt3_ID_CRED_I_bstr.len,
233 : : id_cred_i));
234 : :
235 : : } else {
236 [ - + ]: 5 : TRY(kid_int2id_cred(p.ptxt3_ID_CRED_I_int, id_cred_i));
237 : : }
238 : : }
239 [ - + ]: 10 : TRY(_memcpy_s(sign_or_mac->ptr, sign_or_mac->len,
240 : : p.ptxt3_SGN_or_MAC_3.value,
241 : : (uint32_t)p.ptxt3_SGN_or_MAC_3.len));
242 : 10 : sign_or_mac->len = (uint32_t)p.ptxt3_SGN_or_MAC_3.len;
243 : :
244 [ - + ]: 10 : if (p.ptxt3_EAD_3_present == true) {
245 [ # # ]: 0 : TRY(_memcpy_s(ead->ptr, ead->len, p.ptxt3_EAD_3.value,
246 : : (uint32_t)p.ptxt3_EAD_3.len));
247 : 0 : ead->len = (uint32_t)p.ptxt3_EAD_3.len;
248 : : } else {
249 [ + + ]: 10 : if (ead->len) {
250 : 5 : ead->len = 0;
251 : : }
252 : : }
253 : 10 : return ok;
254 : : }
255 : :
256 : 15 : enum err plaintext_split(struct byte_array *ptxt, struct byte_array *c_r,
257 : : struct byte_array *id_cred_x,
258 : : struct byte_array *sign_or_mac, struct byte_array *ead)
259 : : {
260 : : /*C_R is present only in plaintext 2*/
261 [ + + ]: 15 : if (c_r != NULL) {
262 : 5 : return plaintext2_split(ptxt, c_r, id_cred_x, sign_or_mac, ead);
263 : : } else {
264 : 10 : return plaintext3_split(ptxt, id_cred_x, sign_or_mac, ead);
265 : : }
266 : : }
|