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.h"
15 
16 #include "edhoc/retrieve_cred.h"
17 #include "edhoc/signature_or_mac_msg.h"
18 #include "edhoc/plaintext.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_id_cred_x.h"
25 #include "cbor/edhoc_encode_int_type.h"
26 
id_cred2kid(const uint8_t * id_cred,uint32_t id_cred_len,uint8_t * _kid,uint32_t * kid_len)27 enum err id_cred2kid(const uint8_t *id_cred, uint32_t id_cred_len,
28 		     uint8_t *_kid, uint32_t *kid_len)
29 {
30 	struct id_cred_x_map map;
31 	size_t payload_len_out;
32 	size_t decode_len = 0;
33 	TRY_EXPECT(cbor_decode_id_cred_x_map(id_cred, id_cred_len, &map,
34 					     &decode_len),
35 		   true);
36 
37 	if (map._id_cred_x_map_kid_present != 0) {
38 		// if (map._id_cred_x_map_kid._id_cred_x_map_kid.len == 1) {
39 		// 	int32_t i =
40 		// 		*map._id_cred_x_map_kid._id_cred_x_map_kid.value;
41 		// 	ok = cbor_encode_int_type_i(_kid, *kid_len, &i,
42 		// 				    &payload_len_out);
43 		// 	if (!ok) {
44 		// 		return cbor_encoding_error;
45 		// 	}
46 		// 	*kid_len = payload_len_out;
47 		// } else {
48 		// 	r = _memcpy_s(
49 		// 		_kid, *kid_len,
50 		// 		map._id_cred_x_map_kid._id_cred_x_map_kid.value,
51 		// 		map._id_cred_x_map_kid._id_cred_x_map_kid.len);
52 		// 	if (r != ok) {
53 		// 		return r;
54 		// 	}
55 		// 	*kid_len =
56 		// 		map._id_cred_x_map_kid._id_cred_x_map_kid.len;
57 		// }
58 		//*_kid = map._id_cred_x_map_kid._id_cred_x_map_kid;
59 		TRY_EXPECT(cbor_encode_int_type_i(
60 				   _kid, *kid_len,
61 				   &map._id_cred_x_map_kid._id_cred_x_map_kid,
62 				   &payload_len_out),
63 			   true);
64 		*kid_len = (uint32_t) payload_len_out;
65 	} else {
66 		*kid_len = 0;
67 	}
68 
69 	return ok;
70 }
71 
plaintext_encode(const uint8_t * id_cred,uint32_t id_cred_len,const uint8_t * sgn_or_mac,uint32_t sgn_or_mac_len,const uint8_t * ad,uint32_t ad_len,uint8_t * plaintext,uint32_t * plaintext_len)72 enum err plaintext_encode(const uint8_t *id_cred, uint32_t id_cred_len,
73 			  const uint8_t *sgn_or_mac, uint32_t sgn_or_mac_len,
74 			  const uint8_t *ad, uint32_t ad_len,
75 			  uint8_t *plaintext, uint32_t *plaintext_len)
76 {
77 	uint32_t l;
78 	uint32_t enc_sgn_or_mac_len = sgn_or_mac_len + 2;
79 	uint8_t kid_buf[KID_DEFAULT_SIZE];
80 	uint32_t kid_len = sizeof(kid_buf);
81 	TRY(id_cred2kid(id_cred, id_cred_len, kid_buf, &kid_len));
82 
83 	PRINT_ARRAY("kid", kid_buf, kid_len);
84 	if (kid_len != 0) {
85 		/*id cred contains a kid*/
86 		TRY(_memcpy_s(plaintext, *plaintext_len, kid_buf, kid_len));
87 		l = kid_len;
88 	} else {
89 		TRY(_memcpy_s(plaintext, *plaintext_len, id_cred, id_cred_len));
90 		l = id_cred_len;
91 	}
92 
93 	TRY(encode_byte_string(sgn_or_mac, sgn_or_mac_len, plaintext + l,
94 			       &enc_sgn_or_mac_len));
95 
96 	TRY(_memcpy_s(plaintext + l + enc_sgn_or_mac_len,
97 		      *plaintext_len - l - enc_sgn_or_mac_len, ad, ad_len));
98 
99 	*plaintext_len = l + enc_sgn_or_mac_len + ad_len;
100 
101 	return ok;
102 }
103