Contiki-NG
Loading...
Searching...
No Matches
edhoc-error.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024, RISE Research Institutes of Sweden AB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holder nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30/**
31 * \file
32 * Error handling module header for EDHOC.
33 *
34 * This header defines a unified error handling system that provides
35 * consistent error reporting across all EDHOC operations.
36 *
37 * \author
38 * Niclas Finne <niclas.finne@ri.se>, Nicolas Tsiftes <nicolas.tsiftes@ri.se>
39 */
40
41#ifndef EDHOC_ERROR_H_
42#define EDHOC_ERROR_H_
43
44#include <stdint.h>
45#include <stdbool.h>
46
47/**
48 * \brief Unified error type for EDHOC operations
49 *
50 * This enum defines all possible error conditions that can occur in EDHOC
51 * operations. Error codes are categorized by type and use negative values
52 * to distinguish them from success (0) and positive return values
53 * (e.g., sizes, counts).
54 *
55 * Convention:
56 * - EDHOC_SUCCESS (0): Operation completed successfully
57 * - Negative values: Specific error conditions
58 * - Positive values: Success with data (size, count, etc.)
59 */
60typedef enum {
61 /* Success */
62 EDHOC_SUCCESS = 0,
63
64 /* Memory errors (-1 to -9) */
65 EDHOC_ERR_MEMORY_ALLOCATION = -1, /**< Memory allocation failed */
66 EDHOC_ERR_BUFFER_TOO_SMALL = -2, /**< Provided buffer is too small */
67 EDHOC_ERR_BUFFER_OVERFLOW = -3, /**< Buffer overflow detected */
68 EDHOC_ERR_NULL_POINTER = -4, /**< Null pointer provided */
69 EDHOC_ERR_INVALID_LENGTH = -5, /**< Invalid length parameter */
70
71 /* Cryptographic errors (-10 to -19) */
72 EDHOC_ERR_CRYPTO_KEYGEN = -10, /**< Key generation failed */
73 EDHOC_ERR_CRYPTO_SIGN = -11, /**< Digital signature failed */
74 EDHOC_ERR_CRYPTO_VERIFY = -12, /**< Signature verification failed */
75 EDHOC_ERR_CRYPTO_ENCRYPT = -13, /**< Encryption operation failed */
76 EDHOC_ERR_CRYPTO_DECRYPT = -14, /**< Decryption operation failed */
77 EDHOC_ERR_CRYPTO_HASH = -15, /**< Hash operation failed */
78 EDHOC_ERR_CRYPTO_KDF = -16, /**< Key derivation failed */
79 EDHOC_ERR_CRYPTO_AUTHENTICATION = -17, /**< Authentication failed */
80 EDHOC_ERR_CRYPTO_INVALID_KEY = -18, /**< Invalid cryptographic key */
81
82 /* Protocol errors (-20 to -29) */
83 EDHOC_ERR_SUITE_NOT_SUPPORTED = -20, /**< Cipher suite not supported */
84 EDHOC_ERR_MSG_MALFORMED = -21, /**< Malformed message received */
85 EDHOC_ERR_METHOD_NOT_SUPPORTED = -22, /**< EDHOC method not supported */
86 EDHOC_ERR_CID_INVALID = -23, /**< Invalid connection identifier */
87 EDHOC_ERR_WRONG_CID = -24, /**< Wrong connection identifier */
88 EDHOC_ERR_CREDENTIAL_INVALID = -25, /**< Invalid credential */
89 EDHOC_ERR_ID_CRED_MALFORMED = -26, /**< Malformed credential identifier */
90 EDHOC_ERR_SEQUENCE_ERROR = -27, /**< Message sequence error */
91 EDHOC_ERR_CORRELATION = -28, /**< Message correlation error */
92 EDHOC_ERR_CRITICAL_EAD_UNSUPPORTED = -29, /**< Critical EAD item cannot be processed (RFC 9528 Section 6) */
93
94 /* Storage errors (-30 to -39) */
95 EDHOC_ERR_KEY_NOT_FOUND = -30, /**< Key not found in storage */
96 EDHOC_ERR_KEY_STORAGE_FULL = -31, /**< Key storage is full */
97 EDHOC_ERR_CREDENTIAL_NOT_FOUND = -32, /**< Credential not found */
98 EDHOC_ERR_STORAGE_INIT_FAILED = -33, /**< Storage initialization failed */
99 EDHOC_ERR_DUPLICATE_KEY = -34, /**< Duplicate key in storage */
100
101 /* Network errors (-40 to -49) */
102 EDHOC_ERR_NETWORK_TIMEOUT = -40, /**< Network operation timeout */
103 EDHOC_ERR_NETWORK_CONNECTION = -41, /**< Network connection error */
104 EDHOC_ERR_COAP_ERROR = -42, /**< CoAP protocol error */
105 EDHOC_ERR_MESSAGE_TOO_LARGE = -43, /**< Message too large for transport */
106
107 /* CBOR errors (-50 to -59) */
108 EDHOC_ERR_CBOR_ENCODING = -50, /**< CBOR encoding failed */
109 EDHOC_ERR_CBOR_DECODING = -51, /**< CBOR decoding failed */
110 EDHOC_ERR_CBOR_INVALID_TYPE = -52, /**< Invalid CBOR data type */
111 EDHOC_ERR_CBOR_MALFORMED = -53, /**< Malformed CBOR data */
112
113 /* State errors (-60 to -69) */
114 EDHOC_ERR_INVALID_STATE = -60, /**< Invalid context state */
115 EDHOC_ERR_CONTEXT_NOT_INITIALIZED = -61, /**< Context not initialized */
116 EDHOC_ERR_ALREADY_INITIALIZED = -62, /**< Already initialized */
117 EDHOC_ERR_NOT_ALLOWED_IDENTITY = -63, /**< Identity not allowed */
118
119 /* General errors (-70 to -79) */
120 EDHOC_ERR_INVALID_PARAMETER = -70, /**< Invalid parameter provided */
121 EDHOC_ERR_NOT_IMPLEMENTED = -71, /**< Feature not implemented */
122 EDHOC_ERR_INTERNAL_ERROR = -72, /**< Internal implementation error */
123 EDHOC_ERR_UNKNOWN = -73 /**< Unknown error condition */
125
126/**
127 * \brief Check if an operation was successful
128 * \param result The result to check
129 * \return true if successful, false otherwise
130 */
131#define EDHOC_SUCCESS_CHECK(result) ((result) == EDHOC_SUCCESS)
132
133/**
134 * \brief Check if an operation failed
135 * \param result The result to check
136 * \return true if failed, false otherwise
137 */
138#define EDHOC_FAILED(result) ((result) < EDHOC_SUCCESS)
139
140/**
141 * \brief Get a human-readable error message for an error code
142 * \param error The error code
143 * \return String description of the error
144 */
145const char *edhoc_error_string(edhoc_error_t error);
146
147/**
148 * \brief Error context structure for debugging
149 *
150 * This structure provides detailed context information about where
151 * an error occurred, useful for debugging and logging.
152 */
153typedef struct {
154 edhoc_error_t code; /**< Error code */
155 const char *file; /**< Source file where error occurred */
156 int line; /**< Line number where error occurred */
157 const char *function; /**< Function where error occurred */
158 const char *message; /**< Additional error message */
160
161/**
162 * \brief Set error context for debugging (internal use)
163 * \param code Error code
164 * \param file Source file name
165 * \param line Line number
166 * \param function Function name
167 * \param message Additional message
168 * \return The error code passed in
169 */
171 int line, const char *function,
172 const char *message);
173
174/**
175 * \brief Get the last error context (for debugging)
176 * \return Pointer to last error context, or NULL if none
177 */
179
180/**
181 * \brief Macro for setting error with context information
182 *
183 * Usage: return EDHOC_ERROR(EDHOC_ERR_MEMORY_ALLOCATION, "Failed to allocate key storage");
184 */
185#define EDHOC_ERROR(code, msg) \
186 edhoc_set_error_context((code), __FILE__, __LINE__, __func__, (msg))
187
188/**
189 * \brief Macro for checking and propagating errors
190 *
191 * Usage: EDHOC_CHECK(some_function(params));
192 */
193#define EDHOC_CHECK(call) do { \
194 edhoc_error_t _result = (call); \
195 if(EDHOC_FAILED(_result)) { \
196 return _result; \
197 } \
198} while(0)
199
200
201#endif /* EDHOC_ERROR_H_ */
edhoc_error_t edhoc_set_error_context(edhoc_error_t code, const char *file, int line, const char *function, const char *message)
Set error context for debugging (internal use).
edhoc_error_t
Unified error type for EDHOC operations.
Definition edhoc-error.h:60
@ EDHOC_ERR_CBOR_ENCODING
CBOR encoding failed.
@ EDHOC_ERR_CRYPTO_DECRYPT
Decryption operation failed.
Definition edhoc-error.h:76
@ EDHOC_ERR_CREDENTIAL_INVALID
Invalid credential.
Definition edhoc-error.h:88
@ EDHOC_ERR_CREDENTIAL_NOT_FOUND
Credential not found.
Definition edhoc-error.h:97
@ EDHOC_ERR_ALREADY_INITIALIZED
Already initialized.
@ EDHOC_ERR_CRYPTO_VERIFY
Signature verification failed.
Definition edhoc-error.h:74
@ EDHOC_ERR_STORAGE_INIT_FAILED
Storage initialization failed.
Definition edhoc-error.h:98
@ EDHOC_ERR_NETWORK_TIMEOUT
Network operation timeout.
@ EDHOC_ERR_NULL_POINTER
Null pointer provided.
Definition edhoc-error.h:68
@ EDHOC_ERR_CRYPTO_HASH
Hash operation failed.
Definition edhoc-error.h:77
@ EDHOC_ERR_NETWORK_CONNECTION
Network connection error.
@ EDHOC_ERR_MESSAGE_TOO_LARGE
Message too large for transport.
@ EDHOC_ERR_ID_CRED_MALFORMED
Malformed credential identifier.
Definition edhoc-error.h:89
@ EDHOC_ERR_CRITICAL_EAD_UNSUPPORTED
Critical EAD item cannot be processed (RFC 9528 Section 6).
Definition edhoc-error.h:92
@ EDHOC_ERR_CRYPTO_ENCRYPT
Encryption operation failed.
Definition edhoc-error.h:75
@ EDHOC_ERR_INTERNAL_ERROR
Internal implementation error.
@ EDHOC_ERR_CRYPTO_INVALID_KEY
Invalid cryptographic key.
Definition edhoc-error.h:80
@ EDHOC_ERR_WRONG_CID
Wrong connection identifier.
Definition edhoc-error.h:87
@ EDHOC_ERR_CRYPTO_AUTHENTICATION
Authentication failed.
Definition edhoc-error.h:79
@ EDHOC_ERR_INVALID_STATE
Invalid context state.
@ EDHOC_ERR_KEY_NOT_FOUND
Key not found in storage.
Definition edhoc-error.h:95
@ EDHOC_ERR_DUPLICATE_KEY
Duplicate key in storage.
Definition edhoc-error.h:99
@ EDHOC_ERR_CRYPTO_KDF
Key derivation failed.
Definition edhoc-error.h:78
@ EDHOC_ERR_NOT_IMPLEMENTED
Feature not implemented.
@ EDHOC_ERR_BUFFER_OVERFLOW
Buffer overflow detected.
Definition edhoc-error.h:67
@ EDHOC_ERR_SUITE_NOT_SUPPORTED
Cipher suite not supported.
Definition edhoc-error.h:83
@ EDHOC_ERR_MSG_MALFORMED
Malformed message received.
Definition edhoc-error.h:84
@ EDHOC_ERR_COAP_ERROR
CoAP protocol error.
@ EDHOC_ERR_CORRELATION
Message correlation error.
Definition edhoc-error.h:91
@ EDHOC_ERR_KEY_STORAGE_FULL
Key storage is full.
Definition edhoc-error.h:96
@ EDHOC_ERR_INVALID_LENGTH
Invalid length parameter.
Definition edhoc-error.h:69
@ EDHOC_ERR_UNKNOWN
Unknown error condition.
@ EDHOC_ERR_CID_INVALID
Invalid connection identifier.
Definition edhoc-error.h:86
@ EDHOC_ERR_NOT_ALLOWED_IDENTITY
Identity not allowed.
@ EDHOC_ERR_BUFFER_TOO_SMALL
Provided buffer is too small.
Definition edhoc-error.h:66
@ EDHOC_ERR_CRYPTO_KEYGEN
Key generation failed.
Definition edhoc-error.h:72
@ EDHOC_ERR_CBOR_MALFORMED
Malformed CBOR data.
@ EDHOC_ERR_CRYPTO_SIGN
Digital signature failed.
Definition edhoc-error.h:73
@ EDHOC_ERR_CONTEXT_NOT_INITIALIZED
Context not initialized.
@ EDHOC_ERR_METHOD_NOT_SUPPORTED
EDHOC method not supported.
Definition edhoc-error.h:85
@ EDHOC_ERR_CBOR_DECODING
CBOR decoding failed.
@ EDHOC_ERR_CBOR_INVALID_TYPE
Invalid CBOR data type.
@ EDHOC_ERR_INVALID_PARAMETER
Invalid parameter provided.
@ EDHOC_ERR_MEMORY_ALLOCATION
Memory allocation failed.
Definition edhoc-error.h:65
@ EDHOC_ERR_SEQUENCE_ERROR
Message sequence error.
Definition edhoc-error.h:90
const char * edhoc_error_string(edhoc_error_t error)
Get a human-readable error message for an error code.
Definition edhoc-error.c:44
const edhoc_error_context_t * edhoc_get_last_error_context(void)
Get the last error context (for debugging).
Error context structure for debugging.
const char * message
Additional error message.
int line
Line number where error occurred.
const char * function
Function where error occurred.
const char * file
Source file where error occurred.
edhoc_error_t code
Error code.