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 : :
14 : : #include "edhoc/edhoc_cose.h"
15 : :
16 : : #include "common/oscore_edhoc_error.h"
17 : :
18 : : #include "cbor/edhoc_encode_enc_structure.h"
19 : : #include "cbor/edhoc_encode_sig_structure.h"
20 : :
21 : 9 : enum err cose_enc_structure_encode(const struct byte_array *context,
22 : : const struct byte_array *protected,
23 : : const struct byte_array *external_aad,
24 : : struct byte_array *out)
25 : : {
26 : : struct edhoc_enc_structure enc_structure;
27 : :
28 : 9 : enc_structure.edhoc_enc_structure_context.value = context->ptr;
29 : 9 : enc_structure.edhoc_enc_structure_context.len = context->len;
30 : 9 : enc_structure.edhoc_enc_structure_external_aad.value =
31 : 9 : external_aad->ptr;
32 : 9 : enc_structure.edhoc_enc_structure_external_aad.len = external_aad->len;
33 : :
34 : : /* NULL protected with zero size is acceptable from EDHOC point of view,
35 : : * but CBOR encoder does not accept NULL as input argument.
36 : : * Internally it calls memmove that generates runtime error when input
37 : : * is NULL even if length is set to 0.
38 : : * Workaround is to provide dummy buffer to avoid passing NULL. It does not
39 : : * impact the EDHOC process, since protected length is set to 0 and no value
40 : : * is copied to the EDHOC message. */
41 : : const char dummy_buffer;
42 : :
43 [ + - ]: 9 : if (NULL == protected->ptr) {
44 [ - + ]: 9 : if (0 != protected->len) {
45 : 0 : return wrong_parameter;
46 : : } else {
47 : 9 : enc_structure.edhoc_enc_structure_protected.value =
48 : : (const uint8_t *)&dummy_buffer;
49 : : }
50 : : } else {
51 : 0 : enc_structure.edhoc_enc_structure_protected.value =
52 : 0 : protected->ptr;
53 : : }
54 : :
55 : 9 : enc_structure.edhoc_enc_structure_protected.len = protected->len;
56 : :
57 : : size_t payload_len_out;
58 [ - + ]: 9 : TRY_EXPECT(cbor_encode_edhoc_enc_structure(out->ptr, out->len,
59 : : &enc_structure,
60 : : &payload_len_out),
61 : : 0);
62 : 9 : out->len = (uint32_t)payload_len_out;
63 : 9 : return ok;
64 : : }
65 : :
66 : 12 : enum err cose_sig_structure_encode(const struct byte_array *context,
67 : : const struct byte_array *protected,
68 : : const struct byte_array *external_aad,
69 : : const struct byte_array *payload,
70 : : struct byte_array *out)
71 : : {
72 : : struct sig_structure sig_structure;
73 : :
74 : 12 : sig_structure.sig_structure_context.value = context->ptr;
75 : 12 : sig_structure.sig_structure_context.len = context->len;
76 : 12 : sig_structure.sig_structure_protected.value = protected->ptr;
77 : 12 : sig_structure.sig_structure_protected.len = protected->len;
78 : 12 : sig_structure.sig_structure_external_aad.value = external_aad->ptr;
79 : 12 : sig_structure.sig_structure_external_aad.len = external_aad->len;
80 : 12 : sig_structure.sig_structure_payload.value = payload->ptr;
81 : 12 : sig_structure.sig_structure_payload.len = payload->len;
82 : :
83 : : size_t payload_len_out;
84 [ - + ]: 12 : TRY_EXPECT(cbor_encode_sig_structure(out->ptr, out->len, &sig_structure,
85 : : &payload_len_out),
86 : : 0);
87 : :
88 : 12 : out->len = (uint32_t)payload_len_out;
89 : 12 : return ok;
90 : : }
|