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 = 6,
28 	not_implemented = 7,
29 
30 	/*EDHOC specifc errors*/
31 	/*todo implement error messages*/
32 	error_message_received = 101,
33 	error_message_sent = 102,
34 
35 	sign_failed = 103,
36 	sha_failed = 104,
37 
38 	unsupported_cipher_suite = 106,
39 	unsupported_ecdh_curve = 107,
40 	unsupported_signature_algorithm = 110,
41 
42 	signature_authentication_failed = 112,
43 	mac_authentication_failed = 113,
44 	certificate_authentication_failed = 115,
45 
46 	credential_not_found = 116,
47 	no_such_ca = 117,
48 
49 	cbor_encoding_error = 119,
50 	cbor_decoding_error = 120,
51 	suites_i_list_to_long = 121,
52 
53 	/*OSCORE specific errors*/
54 	not_oscore_pkt = 200,
55 	first_request_after_reboot = 201,
56 	echo_validation_failed = 202,
57 	oscore_unknown_hkdf = 203,
58 	token_mismatch = 204,
59 	oscore_invalid_algorithm_aead = 205,
60 	oscore_invalid_algorithm_hkdf = 206,
61 	oscore_kid_recipient_id_mismatch = 207,
62 	too_many_options = 208,
63 	oscore_valuelen_to_long_error = 209,
64 	oscore_inpkt_invalid_tkl = 210,
65 	oscore_inpkt_invalid_option_delta = 211,
66 	oscore_inpkt_invalid_optionlen = 212,
67 	oscore_inpkt_invalid_piv = 213,
68 	not_valid_input_packet = 214,
69 	oscore_replay_window_protection_error = 215,
70 	oscore_replay_notification_protection_error = 216,
71 	no_echo_option = 217,
72 	echo_val_mismatch = 218,
73 	oscore_ssn_overflow = 219,
74 	oscore_max_interactions = 220,
75 	oscore_interaction_duplicated_token = 221,
76 	oscore_interaction_not_found = 222,
77 	oscore_wrong_uri_path = 223,
78 };
79 
80 /*This macro checks if a function returns an error and if so it propagates
81 	the error to the caller function*/
82 #define TRY(x)                                                                 \
83 	do {                                                                   \
84 		enum err retval = (x);                                         \
85 		if (ok != retval) {                                            \
86 			handle_runtime_error(retval, __FILE__, __LINE__);      \
87 			return retval;                                         \
88 		}                                                              \
89 	} while (0)
90 
91 /* 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. */
92 #define TRY_EXPECT(x, expected_result)                                         \
93 	do {                                                                   \
94 		int retval = (x);                                              \
95 		if (retval != expected_result) {                               \
96 			handle_external_runtime_error(retval, __FILE__,        \
97 						      __LINE__);               \
98 			return unexpected_result_from_ext_lib;                 \
99 		}                                                              \
100 	} while (0)
101 
102 #endif
103