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 #ifndef ERROR_H
13 #define ERROR_H
14 #include "print_util.h"
15 
16 /* All possible errors that EDHOC and OSCORE can have */
17 enum err {
18 	/*common errors*/
19 	ok = 0,
20 	buffer_to_small = 1,
21 	hkdf_failed = 2,
22 	unexpected_result_from_ext_lib = 3,
23 	wrong_parameter = 4,
24 	crypto_operation_not_implemented = 5,
25 	not_supported_feature = 6,
26 	/*indicates that transport layer is not initialized*/
27 	transport_deinitialized = 7,
28 	not_implemented = 8,
29 	vla_insufficient_size = 9,
30 
31 
32 	/*EDHOC specific errors*/
33 	/*todo implement error messages*/
34 	error_message_received = 101,
35 	error_message_sent = 102,
36 
37 	sign_failed = 103,
38 	sha_failed = 104,
39 
40 	unsupported_cipher_suite = 106,
41 	unsupported_ecdh_curve = 107,
42 	unsupported_signature_algorithm = 110,
43 
44 	signature_authentication_failed = 112,
45 	mac_authentication_failed = 113,
46 	certificate_authentication_failed = 115,
47 
48 	credential_not_found = 116,
49 	no_such_ca = 117,
50 
51 	cbor_encoding_error = 119,
52 	cbor_decoding_error = 120,
53 	suites_i_list_to_long = 121,
54 	xor_error = 122,
55         suites_i_list_empty = 123,
56 
57 	/*OSCORE specific errors*/
58 	not_oscore_pkt = 200,
59 	first_request_after_reboot = 201,
60 	echo_validation_failed = 202,
61 	oscore_unknown_hkdf = 203,
62 	token_mismatch = 204,
63 	oscore_invalid_algorithm_aead = 205,
64 	oscore_invalid_algorithm_hkdf = 206,
65 	oscore_kid_recipient_id_mismatch = 207,
66 	too_many_options = 208,
67 	oscore_valuelen_to_long_error = 209,
68 	oscore_inpkt_invalid_tkl = 210,
69 	oscore_inpkt_invalid_option_delta = 211,
70 	oscore_inpkt_invalid_optionlen = 212,
71 	oscore_inpkt_invalid_piv = 213,
72 	not_valid_input_packet = 214,
73 	oscore_replay_window_protection_error = 215,
74 	oscore_replay_notification_protection_error = 216,
75 	no_echo_option = 217,
76 	echo_val_mismatch = 218,
77 	oscore_ssn_overflow = 219,
78 	oscore_max_interactions = 220,
79 	oscore_interaction_duplicated_token = 221,
80 	oscore_interaction_not_found = 222,
81 	oscore_wrong_uri_path = 223,
82 };
83 
84 /*This macro checks if a function returns an error and if so it propagates
85 	the error to the caller function*/
86 #define TRY(x)                                                                 \
87 	do {                                                                   \
88 		enum err retval = (x);                                         \
89 		if (ok != retval) {                                            \
90 			handle_runtime_error(retval, __FILE__, __LINE__);      \
91 			return retval;                                         \
92 		}                                                              \
93 	} while (0)
94 
95 /* This macro checks if a function belonging to an external library returns an expected result or an error. If an error is returned the macro returns unexpected_result_from_ext_lib. */
96 #define TRY_EXPECT(x, expected_result)                                         \
97 	do {                                                                   \
98 		int retval = (x);                                              \
99 		if (retval != expected_result) {                               \
100 			handle_external_runtime_error(retval, __FILE__,        \
101 						      __LINE__);               \
102 			return unexpected_result_from_ext_lib;                 \
103 		}                                                              \
104 	} while (0)
105 
106 #endif
107