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/signature_or_mac_msg.h"
17 : : #include "edhoc/plaintext.h"
18 : : #include "edhoc/bstr_encode_decode.h"
19 : : #include "edhoc/buffer_sizes.h"
20 : : #include "edhoc/int_encode_decode.h"
21 : :
22 : : #include "common/oscore_edhoc_error.h"
23 : : #include "common/memcpy_s.h"
24 : : #include "common/print_util.h"
25 : :
26 : : #include "cbor/edhoc_decode_id_cred_x.h"
27 : : #include "cbor/edhoc_encode_int_type.h"
28 : :
29 : 18 : enum err id_cred2kid(const struct byte_array *id_cred, struct byte_array *kid)
30 : : {
31 : 18 : struct id_cred_x_map map = { 0 };
32 : : size_t payload_len_out;
33 : 18 : size_t decode_len = 0;
34 [ - + ]: 18 : TRY_EXPECT(cbor_decode_id_cred_x_map(id_cred->ptr, id_cred->len, &map,
35 : : &decode_len),
36 : : 0);
37 : :
38 : : /* The compact encoding applies only if ID_CRED_x contains a single
39 : : * 'kid' parameter (RFC 9528 Section 3.5.3.2). */
40 : 48 : bool kid_only = map.id_cred_x_map_kid_present &&
41 [ + - ]: 12 : !map.id_cred_x_map_x5bag_present &&
42 [ + - ]: 12 : !map.id_cred_x_map_x5chain_present &&
43 [ + + ]: 12 : !map.id_cred_x_map_x5t_present &&
44 [ + - ]: 11 : !map.id_cred_x_map_x5u_present &&
45 [ + - ]: 11 : !map.id_cred_x_map_c5b_present &&
46 [ + - ]: 11 : !map.id_cred_x_map_c5c_present &&
47 [ + + + - ]: 41 : !map.id_cred_x_map_c5t_present &&
48 [ + - ]: 11 : !map.id_cred_x_map_c5u_present;
49 : :
50 [ + + ]: 18 : if (!kid_only) {
51 : 7 : kid->len = 0;
52 : 7 : return ok;
53 : : }
54 : :
55 [ + + ]: 11 : if (map.id_cred_x_map_kid.id_cred_x_map_kid_choice ==
56 : : id_cred_x_map_kid_int_c) {
57 : : /* kid given as an int, as in drafts before RFC 9528 */
58 [ - + ]: 1 : TRY_EXPECT(
59 : : cbor_encode_int_type_i(
60 : : kid->ptr, kid->len,
61 : : &map.id_cred_x_map_kid.id_cred_x_map_kid_int,
62 : : &payload_len_out),
63 : : ZCBOR_SUCCESS);
64 : 1 : kid->len = (uint32_t)payload_len_out;
65 : 1 : return ok;
66 : : }
67 : :
68 : : /* A kid byte string that is the CBOR encoding of an int in -24..23 is
69 : : * sent as that int, any other kid as a byte string (RFC 9528 Section
70 : : * 3.3.2). */
71 : 10 : const struct zcbor_string *kid_str =
72 : : &map.id_cred_x_map_kid.id_cred_x_map_kid_bstr;
73 [ - + - + ]: 10 : BYTE_ARRAY_NEW(kid_bstr, KID_SIZE, (uint32_t)kid_str->len);
74 [ - + ]: 10 : TRY(_memcpy_s(kid_bstr.ptr, kid_bstr.len, kid_str->value,
75 : : (uint32_t)kid_str->len));
76 [ + + ]: 10 : if (c_x_is_encoded_int(&kid_bstr)) {
77 [ - + ]: 7 : TRY(_memcpy_s(kid->ptr, kid->len, kid_bstr.ptr, kid_bstr.len));
78 : 7 : kid->len = kid_bstr.len;
79 : : } else {
80 [ - + ]: 3 : TRY(encode_bstr(&kid_bstr, kid));
81 : : }
82 : :
83 : 10 : return ok;
84 : : }
|