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 "edhoc/buffer_sizes.h"
13 : : #include "edhoc_internal.h"
14 : :
15 : : #include "common/memcpy_s.h"
16 : : #include "common/print_util.h"
17 : : #include "common/crypto_wrapper.h"
18 : : #include "common/oscore_edhoc_error.h"
19 : :
20 : : #include "edhoc/hkdf_info.h"
21 : : #include "edhoc/messages.h"
22 : : #include "edhoc/okm.h"
23 : : #include "edhoc/plaintext.h"
24 : : #include "edhoc/prk.h"
25 : : #include "edhoc/retrieve_cred.h"
26 : : #include "edhoc/signature_or_mac_msg.h"
27 : : #include "edhoc/suites.h"
28 : : #include "edhoc/th.h"
29 : : #include "edhoc/txrx_wrapper.h"
30 : : #include "edhoc/ciphertext.h"
31 : : #include "edhoc/suites.h"
32 : : #include "edhoc/runtime_context.h"
33 : : #include "edhoc/bstr_encode_decode.h"
34 : : #include "edhoc/int_encode_decode.h"
35 : :
36 : : #include "cbor/edhoc_decode_message_1.h"
37 : : #include "cbor/edhoc_encode_message_2.h"
38 : : #include "cbor/edhoc_decode_message_3.h"
39 : :
40 : : #define CBOR_UINT_SINGLE_BYTE_UINT_MAX_VALUE (0x17)
41 : : #define CBOR_UINT_MULTI_BYTE_UINT_MAX_VALUE (0x17)
42 : : #define CBOR_BSTR_TYPE_MIN_VALUE (0x40)
43 : : #define CBOR_BSTR_TYPE_MAX_VALUE (0x57)
44 : :
45 : : /**
46 : : * @brief Parses message 1.
47 : : * @param[in] msg1 Message 1.
48 : : * @param[out] method EDHOC method.
49 : : * @param[out] suites_i Cipher suites suported by the initiator
50 : : * @param[out] g_x Public ephemeral key of the initiator.
51 : : * @param[out] c_i Connection identifier of the initiator.
52 : : * @param[out] ead1 External authorized data 1.
53 : : * @retval Ok or error code.
54 : : */
55 : : static inline enum err
56 : 5 : msg1_parse(struct byte_array *msg1, enum method_type *method,
57 : : struct byte_array *suites_i, struct byte_array *g_x,
58 : : struct byte_array *c_i, struct byte_array *ead1)
59 : : {
60 : : uint32_t i;
61 : : struct message_1 m;
62 : 5 : size_t decode_len = 0;
63 : :
64 [ - + ]: 5 : TRY_EXPECT(cbor_decode_message_1(msg1->ptr, msg1->len, &m, &decode_len),
65 : : 0);
66 : :
67 : : /*METHOD*/
68 [ + - ]: 5 : if ((m.message_1_METHOD > INITIATOR_SDHK_RESPONDER_SDHK) ||
69 [ - + ]: 5 : (m.message_1_METHOD < INITIATOR_SK_RESPONDER_SK)) {
70 : 0 : return wrong_parameter;
71 : : }
72 : 5 : *method = (enum method_type)m.message_1_METHOD;
73 : 5 : PRINTF("msg1 METHOD: %d\n", (int)*method);
74 : :
75 : : /*SUITES_I*/
76 [ + + ]: 5 : if (m.message_1_SUITES_I_choice == message_1_SUITES_I_int_c) {
77 : : /*the initiator supports only one suite*/
78 : 3 : suites_i->ptr[0] = (uint8_t)m.message_1_SUITES_I_int;
79 : 3 : suites_i->len = 1;
80 : : } else {
81 [ - + ]: 2 : if (0 == m.SUITES_I_suite_l_suite_count) {
82 : 0 : return suites_i_list_empty;
83 : : }
84 : :
85 : : /*the initiator supports more than one suite*/
86 [ - + ]: 2 : if (m.SUITES_I_suite_l_suite_count > suites_i->len) {
87 : 0 : return suites_i_list_to_long;
88 : : }
89 : :
90 [ + + ]: 6 : for (i = 0; i < m.SUITES_I_suite_l_suite_count; i++) {
91 : 4 : suites_i->ptr[i] = (uint8_t)m.SUITES_I_suite_l_suite[i];
92 : : }
93 : 2 : suites_i->len = (uint32_t)m.SUITES_I_suite_l_suite_count;
94 : : }
95 : 5 : PRINT_ARRAY("msg1 SUITES_I", suites_i->ptr, suites_i->len);
96 : :
97 : : /*G_X*/
98 [ - + ]: 5 : TRY(_memcpy_s(g_x->ptr, g_x->len, m.message_1_G_X.value,
99 : : (uint32_t)m.message_1_G_X.len));
100 : 5 : g_x->len = (uint32_t)m.message_1_G_X.len;
101 : 5 : PRINT_ARRAY("msg1 G_X", g_x->ptr, g_x->len);
102 : :
103 : : /*C_I*/
104 [ + - ]: 5 : if (m.message_1_C_I_choice == message_1_C_I_int_c) {
105 : : /* keep C_I in its CBOR encoding, as done for C_R in
106 : : * plaintext2_split() and as used by the initiator */
107 [ - + ]: 5 : TRY(c_x_int_encode(m.message_1_C_I_int, c_i));
108 : : } else {
109 [ # # ]: 0 : TRY(_memcpy_s(c_i->ptr, c_i->len, m.message_1_C_I_bstr.value,
110 : : (uint32_t)m.message_1_C_I_bstr.len));
111 : 0 : c_i->len = (uint32_t)m.message_1_C_I_bstr.len;
112 : : }
113 : 5 : PRINT_ARRAY("msg1 C_I_raw", c_i->ptr, c_i->len);
114 : :
115 : : /*ead_1*/
116 [ - + ]: 5 : if (m.message_1_ead_1_present) {
117 [ # # ]: 0 : TRY(_memcpy_s(ead1->ptr, ead1->len, m.message_1_ead_1.value,
118 : : (uint32_t)m.message_1_ead_1.len));
119 : 0 : ead1->len = (uint32_t)m.message_1_ead_1.len;
120 : 0 : PRINT_ARRAY("msg1 ead_1", ead1->ptr, ead1->len);
121 : : }
122 : 5 : return ok;
123 : : }
124 : :
125 : : /**
126 : : * @brief Checks if the selected cipher suite
127 : : * (the first in the list received from the
128 : : * initiator) is supported.
129 : : * @param selected The selected suite.
130 : : * @param[in] suites_r The list of suported cipher suites.
131 : : * @retval True if supported.
132 : : */
133 : 5 : static inline bool selected_suite_is_supported(uint8_t selected,
134 : : struct byte_array *suites_r)
135 : : {
136 [ + - ]: 5 : for (uint32_t i = 0; i < suites_r->len; i++) {
137 [ + - ]: 5 : if (suites_r->ptr[i] == selected) {
138 : 5 : PRINTF("Suite %d will be used in this EDHOC run.\n",
139 : : selected);
140 : 5 : return true;
141 : : }
142 : : }
143 : 0 : return false;
144 : : }
145 : :
146 : : /**
147 : : * @brief Encodes message 2.
148 : : * @param[in] g_y Public ephemeral DH key of the responder.
149 : : * @param[in] c_r Connection identifier of the responder.
150 : : * @param[in] ciphertext_2 The ciphertext.
151 : : * @param[out] msg2 The encoded message.
152 : : * @retval Ok or error code.
153 : : */
154 : 5 : static inline enum err msg2_encode(const struct byte_array *g_y,
155 : : struct byte_array *c_r,
156 : : const struct byte_array *ciphertext_2,
157 : : struct byte_array *msg2)
158 : : {
159 [ - + - + ]: 5 : BYTE_ARRAY_NEW(g_y_ciphertext_2, G_Y_CIPHERTEXT_2,
160 : : g_y->len + ciphertext_2->len);
161 : :
162 : 5 : memcpy(g_y_ciphertext_2.ptr, g_y->ptr, g_y->len);
163 : 5 : memcpy(g_y_ciphertext_2.ptr + g_y->len, ciphertext_2->ptr,
164 : 5 : ciphertext_2->len);
165 : :
166 [ - + ]: 5 : TRY(encode_bstr(&g_y_ciphertext_2, msg2));
167 : :
168 : 5 : PRINT_ARRAY("message_2 (CBOR Sequence)", msg2->ptr, msg2->len);
169 : 5 : return ok;
170 : : }
171 : :
172 : 5 : enum err msg2_gen(struct edhoc_responder_context *c, struct runtime_context *rc,
173 : : struct byte_array *c_i)
174 : : {
175 : 5 : PRINT_ARRAY("message_1 (CBOR Sequence)", rc->msg.ptr, rc->msg.len);
176 : :
177 : 5 : enum method_type method = INITIATOR_SK_RESPONDER_SK;
178 [ - + ]: 5 : BYTE_ARRAY_NEW(suites_i, SUITES_I_SIZE, SUITES_I_SIZE);
179 [ - + ]: 5 : BYTE_ARRAY_NEW(g_x, G_X_SIZE, G_X_SIZE);
180 : :
181 [ - + ]: 5 : TRY(msg1_parse(&rc->msg, &method, &suites_i, &g_x, c_i, &rc->ead));
182 : :
183 : : // TODO this may be a vulnerability in case suites_i.len is zero
184 [ - + ]: 5 : if (!(selected_suite_is_supported(suites_i.ptr[suites_i.len - 1],
185 : : &c->suites_r))) {
186 : : // TODO implement here the sending of an error message
187 : 0 : return error_message_sent;
188 : : }
189 : :
190 : : /*get cipher suite*/
191 [ - + ]: 5 : TRY(get_suite((enum suite_label)suites_i.ptr[suites_i.len - 1],
192 : : &rc->suite));
193 : :
194 : : bool static_dh_r;
195 : 5 : authentication_type_get(method, &rc->static_dh_i, &static_dh_r);
196 : :
197 : : /******************* create and send message 2*************************/
198 [ - + - + ]: 5 : BYTE_ARRAY_NEW(th2, HASH_SIZE, get_hash_len(rc->suite.edhoc_hash));
199 [ - + ]: 5 : TRY(hash(rc->suite.edhoc_hash, &rc->msg, &rc->msg1_hash));
200 [ - + ]: 5 : TRY(th2_calculate(rc->suite.edhoc_hash, &rc->msg1_hash, &c->g_y, &th2));
201 : :
202 : : /*calculate the DH shared secret*/
203 [ - + ]: 5 : BYTE_ARRAY_NEW(g_xy, ECDH_SECRET_SIZE, ECDH_SECRET_SIZE);
204 [ - + ]: 5 : TRY(shared_secret_derive(rc->suite.edhoc_ecdh, &c->y, &g_x, g_xy.ptr));
205 : :
206 : 5 : PRINT_ARRAY("G_XY (ECDH shared secret) ", g_xy.ptr, g_xy.len);
207 : :
208 [ - + ]: 5 : BYTE_ARRAY_NEW(PRK_2e, PRK_SIZE, PRK_SIZE);
209 [ - + ]: 5 : TRY(hkdf_extract(rc->suite.edhoc_hash, &th2, &g_xy, PRK_2e.ptr));
210 : 5 : PRINT_ARRAY("PRK_2e", PRK_2e.ptr, PRK_2e.len);
211 : :
212 : : /*derive prk_3e2m*/
213 [ - + ]: 5 : TRY(prk_derive(static_dh_r, rc->suite, SALT_3e2m, &th2, &PRK_2e, &g_x,
214 : : &c->r, rc->prk_3e2m.ptr));
215 : 5 : PRINT_ARRAY("prk_3e2m", rc->prk_3e2m.ptr, rc->prk_3e2m.len);
216 : :
217 : : /*compute signature_or_MAC_2*/
218 [ - + - + ]: 5 : BYTE_ARRAY_NEW(sign_or_mac_2, SIGNATURE_SIZE,
219 : : get_signature_len(rc->suite.edhoc_sign));
220 : :
221 [ - + ]: 5 : TRY(signature_or_mac(GENERATE, static_dh_r, &rc->suite, &c->sk_r,
222 : : &c->pk_r, &rc->prk_3e2m, &c->c_r, &th2,
223 : : &c->id_cred_r, &c->cred_r, &c->ead_2, MAC_2,
224 : : &sign_or_mac_2));
225 : :
226 : : /*compute ciphertext_2*/
227 [ - + - - : 5 : BYTE_ARRAY_NEW(plaintext_2, PLAINTEXT2_SIZE,
- - + - -
+ - - - +
- + - - -
- + - - +
- - - + -
+ - - - -
+ - - + -
- ]
228 : : AS_BSTR_SIZE(c->c_r.len) + c->id_cred_r.len +
229 : : AS_BSTR_SIZE(sign_or_mac_2.len) + c->ead_2.len);
230 [ - + - + ]: 5 : BYTE_ARRAY_NEW(ciphertext_2, CIPHERTEXT2_SIZE, plaintext_2.len);
231 : :
232 [ - + ]: 5 : TRY(ciphertext_gen(CIPHERTEXT2, &rc->suite, &c->c_r, &c->id_cred_r,
233 : : &sign_or_mac_2, &c->ead_2, &PRK_2e, &th2,
234 : : &ciphertext_2, &plaintext_2));
235 : :
236 : : /* Clear the message buffer. */
237 : 5 : memset(rc->msg.ptr, 0, rc->msg.len);
238 : 5 : rc->msg.len = sizeof(rc->msg_buf);
239 : : /*message 2 create*/
240 [ - + ]: 5 : TRY(msg2_encode(&c->g_y, &c->c_r, &ciphertext_2, &rc->msg));
241 : :
242 [ - + ]: 5 : TRY(th34_calculate(rc->suite.edhoc_hash, &th2, &plaintext_2, &c->cred_r,
243 : : &rc->th3));
244 : :
245 : 5 : return ok;
246 : : }
247 : :
248 : 5 : enum err msg3_process(struct edhoc_responder_context *c,
249 : : struct runtime_context *rc,
250 : : struct cred_array *cred_i_array,
251 : : struct byte_array *prk_out,
252 : : struct byte_array *initiator_pk)
253 : 5 : {
254 [ - + - + ]: 5 : BYTE_ARRAY_NEW(ctxt3, CIPHERTEXT3_SIZE, rc->msg.len);
255 [ - + ]: 5 : TRY(decode_bstr(&rc->msg, &ctxt3));
256 : 5 : PRINT_ARRAY("CIPHERTEXT_3", ctxt3.ptr, ctxt3.len);
257 : :
258 [ - + ]: 5 : BYTE_ARRAY_NEW(id_cred_i, ID_CRED_I_SIZE, ID_CRED_I_SIZE);
259 [ - + ]: 5 : BYTE_ARRAY_NEW(sign_or_mac, SIG_OR_MAC_SIZE, SIG_OR_MAC_SIZE);
260 : :
261 : 5 : PRINTF("PLAINTEXT3_SIZE: %d\n", PLAINTEXT3_SIZE);
262 : 5 : PRINTF("ctxt3.len: %d\n", ctxt3.len);
263 : : #if defined(_WIN32)
264 : : BYTE_ARRAY_NEW(ptxt3,
265 : : PLAINTEXT3_SIZE + 16, // 16 is max aead mac length
266 : : ctxt3.len);
267 : : #else
268 [ - + - + ]: 5 : BYTE_ARRAY_NEW(ptxt3,
269 : : PLAINTEXT3_SIZE + get_aead_mac_len(rc->suite.edhoc_aead),
270 : : ctxt3.len);
271 : : #endif
272 : :
273 [ - + ]: 5 : TRY(ciphertext_decrypt_split(CIPHERTEXT3, &rc->suite, NULL, &id_cred_i,
274 : : &sign_or_mac, &rc->ead, &rc->prk_3e2m,
275 : : &rc->th3, &ctxt3, &ptxt3));
276 : :
277 : : /*check the authenticity of the initiator*/
278 [ - + ]: 5 : BYTE_ARRAY_NEW(cred_i, CRED_I_SIZE, CRED_I_SIZE);
279 [ - + ]: 5 : BYTE_ARRAY_NEW(pk, PK_SIZE, PK_SIZE);
280 [ - + ]: 5 : BYTE_ARRAY_NEW(g_i, G_I_SIZE, G_I_SIZE);
281 : :
282 [ - + ]: 5 : TRY(retrieve_cred(rc->static_dh_i, cred_i_array, &id_cred_i, &cred_i,
283 : : &pk, &g_i));
284 : 5 : PRINT_ARRAY("CRED_I", cred_i.ptr, cred_i.len);
285 : 5 : PRINT_ARRAY("pk", pk.ptr, pk.len);
286 : 5 : PRINT_ARRAY("g_i", g_i.ptr, g_i.len);
287 : :
288 : : /* Export public key. */
289 [ + - - + ]: 5 : if ((NULL != initiator_pk) && (NULL != initiator_pk->ptr)) {
290 [ # # ]: 0 : TRY(_memcpy_s(initiator_pk->ptr, initiator_pk->len, pk.ptr,
291 : : pk.len));
292 : 0 : initiator_pk->len = pk.len;
293 : : }
294 : :
295 : : /*derive prk_4e3m*/
296 [ - + ]: 5 : TRY(prk_derive(rc->static_dh_i, rc->suite, SALT_4e3m, &rc->th3,
297 : : &rc->prk_3e2m, &g_i, &c->y, rc->prk_4e3m.ptr));
298 : 5 : PRINT_ARRAY("prk_4e3m", rc->prk_4e3m.ptr, rc->prk_4e3m.len);
299 : :
300 [ - + ]: 5 : TRY(signature_or_mac(VERIFY, rc->static_dh_i, &rc->suite, NULL, &pk,
301 : : &rc->prk_4e3m, &NULL_ARRAY, &rc->th3, &id_cred_i,
302 : : &cred_i, &rc->ead, MAC_3, &sign_or_mac));
303 : :
304 : : /*TH4*/
305 : : // ptxt3.len = ptxt3.len - get_aead_mac_len(rc->suite.edhoc_aead);
306 [ - + ]: 5 : TRY(th34_calculate(rc->suite.edhoc_hash, &rc->th3, &ptxt3, &cred_i,
307 : : &rc->th4));
308 : :
309 : : /*PRK_out*/
310 [ - + ]: 5 : TRY(edhoc_kdf(rc->suite.edhoc_hash, &rc->prk_4e3m, PRK_out, &rc->th4,
311 : : prk_out));
312 : 5 : return ok;
313 : : }
314 : :
315 : : #ifdef MESSAGE_4
316 : : enum err msg4_gen(struct edhoc_responder_context *c, struct runtime_context *rc)
317 : : {
318 : : /*Ciphertext 4 calculate*/
319 : : BYTE_ARRAY_NEW(ctxt4, CIPHERTEXT4_SIZE, CIPHERTEXT4_SIZE);
320 : : #if PLAINTEXT4_SIZE != 0
321 : : BYTE_ARRAY_NEW(ptxt4, PLAINTEXT4_SIZE, PLAINTEXT4_SIZE);
322 : : #else
323 : : struct byte_array ptxt4 = BYTE_ARRAY_INIT(NULL, 0);
324 : : #endif
325 : :
326 : : TRY(ciphertext_gen(CIPHERTEXT4, &rc->suite, &NULL_ARRAY, &NULL_ARRAY,
327 : : &NULL_ARRAY, &c->ead_4, &rc->prk_4e3m, &rc->th4,
328 : : &ctxt4, &ptxt4));
329 : :
330 : : TRY(encode_bstr(&ctxt4, &rc->msg));
331 : :
332 : : PRINT_ARRAY("Message 4 ", rc->msg.ptr, rc->msg.len);
333 : : return ok;
334 : : }
335 : : #endif // MESSAGE_4
336 : :
337 : 5 : enum err edhoc_responder_run_extended(
338 : : struct edhoc_responder_context *c, struct cred_array *cred_i_array,
339 : : struct byte_array *err_msg, struct byte_array *prk_out,
340 : : struct byte_array *initiator_pub_key, struct byte_array *c_i_bytes,
341 : : enum err (*tx)(void *sock, struct byte_array *data),
342 : : enum err (*rx)(void *sock, struct byte_array *data),
343 : : enum err (*ead_process)(void *params, struct byte_array *ead13))
344 : : {
345 : 5 : struct runtime_context rc = { 0 };
346 : 5 : runtime_context_init(&rc);
347 : :
348 : : /*receive message 1*/
349 : 5 : PRINT_MSG("waiting to receive message 1...\n");
350 [ - + ]: 5 : TRY(rx(c->sock, &rc.msg));
351 : :
352 : : /*create and send message 2*/
353 [ - + ]: 5 : TRY(msg2_gen(c, &rc, c_i_bytes));
354 [ - + ]: 5 : TRY(ead_process(c->params_ead_process, &rc.ead));
355 [ - + ]: 5 : TRY(tx(c->sock, &rc.msg));
356 : :
357 : : /*receive message 3*/
358 : 5 : PRINT_MSG("waiting to receive message 3...\n");
359 : 5 : rc.msg.len = sizeof(rc.msg_buf);
360 [ - + ]: 5 : TRY(rx(c->sock, &rc.msg));
361 [ - + ]: 5 : TRY(msg3_process(c, &rc, cred_i_array, prk_out, initiator_pub_key));
362 [ - + ]: 5 : TRY(ead_process(c->params_ead_process, &rc.ead));
363 : :
364 : : /*create and send message 4*/
365 : : #ifdef MESSAGE_4
366 : : TRY(msg4_gen(c, &rc));
367 : : TRY(tx(c->sock, &rc.msg));
368 : : #endif // MESSAGE_4
369 : 5 : return ok;
370 : : }
371 : :
372 : 5 : enum err edhoc_responder_run(
373 : : struct edhoc_responder_context *c, struct cred_array *cred_i_array,
374 : : struct byte_array *err_msg, struct byte_array *prk_out,
375 : : enum err (*tx)(void *sock, struct byte_array *data),
376 : : enum err (*rx)(void *sock, struct byte_array *data),
377 : : enum err (*ead_process)(void *params, struct byte_array *ead13))
378 : : {
379 [ - + ]: 5 : BYTE_ARRAY_NEW(c_i, C_I_SIZE, C_I_SIZE);
380 : 5 : return edhoc_responder_run_extended(c, cred_i_array, err_msg, prk_out,
381 : : &NULL_ARRAY, &c_i, tx, rx,
382 : : ead_process);
383 : : }
|