1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * LICENSE-2.0" target="_blank">http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 ******************************************************************/
23 #ifndef _PKI_ERRORS_H_
24 #define _PKI_ERRORS_H_
32 #warning "DEBUG is enabled"
33 #include <stdio.h> // <printf>
39 * The enumeration of error codes.
44 PKI_SUCCESS = 0, /**< No error occurred. */
45 PKI_UNKNOWN_ERROR, /**< Unknown error occurred. */
46 PKI_NULL_PASSED, /**< NULL passed to function. */
47 PKI_CERT_DATE_INVALID, /**< Certificate date expired. */
48 PKI_BUFFER_OVERFLOW, /**< Array out of range. */
49 PKI_WRONG_OCTET_LEN, /**< Wrong length of octet. */
50 PKI_UNKNOWN_OID, /**< Requested OID is unknown. */
51 PKI_INVALID_FORMAT, /**< The CRT/CRL/CSR format is invalid. */
52 PKI_INVALID_DATE_FORMAT, /**< The date tag or value is invalid. */
53 PKI_INVALID_SIGNATURE, /**< The signature tag or value invalid. */
54 PKI_SIG_MISMATCH, /**< Signature algorithms do not match. */
55 PKI_CERT_VERIFY_FAILED, /**< Certificate verification failed*/
56 PKI_CERT_REVOKED, /**< Certificate is revoked. */
57 PKI_WRONG_ARRAY_LEN, /**< Wrong length of input array*/
58 PKI_MEMORY_ALLOC_FAILED, /**< Failed to allocate memory */
59 PKI_BASE64_ERROR, /**< Base64 convertion error occurred. */
60 PKI_JSON_ERROR, /**< JSON convertion error occurred. */
61 PKI_JSON_NOT_FOUND, /**< JSON object not found. */
63 ISSUER_CA_STORAGE_FILE_READ_ERROR, /**< File read error in CA storage */
64 ISSUER_CA_STORAGE_FILE_WRITE_ERROR, /**< File write error in CA storage */
65 ISSUER_CA_STORAGE_CRL_READ_ERROR, /**< CRL file read error in CA storage */
66 ISSUER_CA_STORAGE_CRL_WRITE_ERROR, /**< CRL file write error in CA storage */
67 ISSUER_CA_STORAGE_CRT_READ_ERROR, /**< Certificate file read error in CA storage */
68 ISSUER_CA_STORAGE_CRT_WRITE_ERROR, /**< Certificate file write error in CA storage */
69 ISSUER_CA_STORAGE_MEMORY_ALLOC_FAILED, /**< Failed to allocate memory in CA storage */
70 ISSUER_CA_STORAGE_WRONG_SERIAL_NUMBER, /**< Wrong serial number passed to CA storage */
71 ISSUER_CA_STORAGE_SN_UNDEFINED, /**< Serial number is not defined in CA storage */
72 ISSUER_CA_STORAGE_WRONG_CRL_SERIAL_NUMBER, /**< Wrong CRL serial number passed to CA
74 ISSUER_CA_STORAGE_CRL_SN_UNDEFINED, /**< CRL serial number is not defined in CA
76 ISSUER_CA_STORAGE_WRONG_PRIVATE_KEY_LEN, /**< Passed private key length not equal to
78 ISSUER_CA_STORAGE_PRIVATE_KEY_UNDEFINED, /**< CA private key is not defined in CA storage */
79 ISSUER_CA_STORAGE_WRONG_PUBLIC_KEY_LEN, /**< Passed public key length not equal to
81 ISSUER_CA_STORAGE_PUBLIC_KEY_UNDEFINED, /**< CA public key is not defined in CA storage */
82 ISSUER_CA_STORAGE_CA_CHAIN_LENGTH_UNDEFINED,/**< CA certificate chain length is not defined in
84 ISSUER_CA_STORAGE_WRONG_CA_NAME_LEN, /**< CA name length is bigger than
85 ISSUER_MAX_NAME_SIZE */
86 ISSUER_CA_STORAGE_CA_NAME_UNDEFINED, /**< CA name is not defined in CA storage */
87 ISSUER_CA_STORAGE_CRL_UNDEFINED, /**< CRL is not defined in CA storage */
88 ISSUER_CA_STORAGE_NULL_PASSED, /**< NULL passed to function in CA storage */
89 CKM_INFO_IS_NOT_INIT, /**< CKM info storage was not init */
90 ISSUER_CA_STORAGE_WRONG_BYTE_ARRAY_LEN, /**< ByteArray with wrong lenth passed into
93 ISSUER_MAKE_KEY_ERROR, /**< Error during uECC_make_key() */
94 ISSUER_MEMORY_ALLOC_FAILED, /**< Failed to allocate memory in issuer */
95 ISSUER_FILE_WRITE_ERROR, /**< File write error in issuer */
96 ISSUER_WRONG_SERIAL_NUMBER, /**< Wrong serial number passed to issuer */
97 ISSUER_WRONG_ROOT_NAME_LEN, /**< CA name length is bigger than ISSUER_MAX_NAME_SIZE */
98 ISSUER_NULL_PASSED, /**< NULL passed to function in issuer */
99 ISSUER_WRONG_BYTE_ARRAY_LEN,/**< ByteArray with wrong length passed into issuer */
101 ISSUER_CRL_ENCODER_MEMORY_ALLOC_FAILED, /**< Failed to allocate memory in CRL encoder */
102 ISSUER_CRL_ENCODER_DER_ENCODE_FAIL, /**< Failed to encode structure into DER
104 ISSUER_CRL_ENCODER_SIGNATURE_FAIL, /**< Failed to sign TBS in CRL encoder */
105 ISSUER_CRL_NULL_PASSED, /**< NULL passed to function in CRL encoder */
106 ISSUER_CRL_WRONG_BYTE_ARRAY_LEN, /**< ByteArray with wrong length passed into
109 ISSUER_CSR_MEMORY_ALLOC_FAILED, /**< Failed to allocate memory in CSR unit */
110 ISSUER_CSR_DER_ENCODE_FAIL, /**< Failed to encode structure into DER in CSR unit */
111 ISSUER_CSR_SIGNATURE_FAIL, /**< Failed to sign TBS in CSR unit */
112 ISSUER_CSR_DER_DECODE_FAIL, /**< Failed to decode structure from DER in CSR unit */
113 ISSUER_CSR_INVALID_SIGNATURE, /**< Signature check fail in CSR unit. */
114 ISSUER_CSR_TOO_LONG_NAME, /**< CSR subject name length is bigger than CSR_MAX_NAME_SIZE */
115 ISSUER_CSR_INVALID_KEY_FORMAT, /**< Public key format is invalid in CSR unit. */
116 ISSUER_CSR_NULL_PASSED, /**< NULL passed to function in CSR unit */
117 ISSUER_CSR_WRONG_BYTE_ARRAY_LEN,/**< ByteArray with wrong length passed into CSR unit */
119 ISSUER_X509_MEMORY_ALLOC_FAILED, /**< Failed to allocate memory in X.509 encoder */
120 ISSUER_X509_DER_ENCODE_FAIL, /**< Failed to encode structure into DER in X.509 encoder */
121 ISSUER_X509_SIGNATURE_FAIL, /**< Failed to sign TBS in X.509 encoder */
122 ISSUER_X509_NULL_PASSED, /**< NULL passed to function in X.509 encoder */
123 ISSUER_X509_WRONG_BYTE_ARRAY_LEN /**< ByteArray with wrong length passed into X.509 encoder */
127 /** @def CHECK_PRINT(err_code, ...)
129 * Prints debug information \a err_code and __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__ values.
131 * @param[in] err_code Error code
134 #define CHECK_PRINT(err_code) \
135 fprintf(stderr, "%s() in %s, line %i: %d\n",__func__, __FILE__, __LINE__, err_code);
137 # define CHECK_PRINT(...)
141 * @def CHECK_NULL(param, error)
142 * A macro that checks whether \a param is not NULL.
144 * If \a (param = NULL) it goes to error processing with \a error code.
146 * @param[in] param Parameter to check
147 * @param[in] error Error code
149 #define CHECK_NULL(param, error) do { \
151 error_value = error; \
152 CHECK_PRINT(error); \
156 /** @def CHECK_EQUAL(param, checker, err_code)
158 * A macro that checks whether \a param equal to \a checker.
160 * If \a (param != checker) it goes to error processing with \a err_code error code.
162 * @param[in] param Parameter to check
163 * @param[in] error Error code
165 #define CHECK_EQUAL(param, checker, err_code) do { \
166 if ((param) != (checker)) { \
167 error_value = err_code; \
168 CHECK_PRINT(err_code); \
172 /** @def CHECK_NOT_EQUAL(param, checker, err_code)
173 * A macro that checks whether \a param not equal to \a checker.
175 * If \a (param == checker) it goes to error processing with \a err_code error code.
177 * @param[in] param Parameter to check
178 * @param[in] error Error code
180 #define CHECK_NOT_EQUAL(param, checker, err_code) do { \
181 if ((param) == (checker)) { \
182 error_value = err_code; \
183 CHECK_PRINT(err_code); \
187 /** @def CHECK_LESS(param, checker, err_code)
188 * A macro that checks whether \a param less then \a checker.
190 * If \a (param > checker) it goes to error processing with \a err_code error code.
192 * @param[in] param Parameter to check
193 * @param[in] error error code
195 #define CHECK_LESS(param, checker, err_code) do { \
196 if ((param) >= (checker)) { \
197 error_value = err_code; \
198 CHECK_PRINT(err_code); \
202 /** @def CHECK_COND(param, err_code)
204 * A macro that checks whether condition \a param is true.
206 * If \a (param != true) it goes to error processing with \a err_code error code.
208 * @param[in] param Parameter to check
209 * @param[in] error error code
211 #define CHECK_COND(param, err_code) do { \
213 error_value = err_code; \
214 CHECK_PRINT(err_code); \
218 /** @def CHECK_LESS_EQUAL(param, checker, err_code)
220 * A macro that checks whether \a param <= \a checker.
222 * If \a (param < checker) it goes to error processing with \a err_code error code.
224 * @param[in] param Parameter to check
225 * @param[in] error error code
227 #define CHECK_LESS_EQUAL(param, checker, err_code) do { \
228 if ((param) > (checker)) { \
229 error_value = err_code; \
230 CHECK_PRINT(err_code); \
234 /** @def CHECK_NULL_BYTE_ARRAY_PTR(param, err_code)
236 * A macro that checks whether pointer to ByteArray \a param is not NULL and contains a valid pointer.
238 * If \a (param != checker) it goes to error processing with \a err_code error code.
240 * @param[in] param Parameter to check
241 * @param[in] err_code Error code
243 #define CHECK_NULL_BYTE_ARRAY_PTR(param, err_code) do { \
244 CHECK_NULL(param, err_code); \
245 CHECK_NULL((param)->data, err_code); \
246 CHECK_NULL((param)->len, err_code); \
249 /** @def FUNCTION_INIT(...)
250 * A macro for initializations function variables.
252 * If error occurs it goes to error processing.
254 #define FUNCTION_INIT(...) \
255 PKIError error_value = PKI_UNKNOWN_ERROR; \
258 /** @def FUNCTION_CLEAR(...)
260 * A macro for freeing function variables.
262 * @return 0 if successful
264 #define FUNCTION_CLEAR(...) \
265 error_value = PKI_SUCCESS; \
270 /** @def CHECK_CALL(fn, ...)
271 * A macro that checks \a fn function return code
273 * If function return error code it goes to error processing.
275 * @param[in] fn Function to call
277 #define CHECK_CALL(fn, ...) do { \
278 error_value = fn(__VA_ARGS__); \
279 if ((int)error_value != (int)PKI_SUCCESS) { \
280 CHECK_PRINT(error_value); \
284 /** @def CHECK_INC_BYTE_ARRAY_PTR(array, size)
286 * Increments byte array pointer \a array by \a size with bound checking.
288 * @param array byte array pointer
289 * @param size number of positions
291 #undef CHECK_INC_BYTE_ARRAY_PTR
292 #define CHECK_INC_BYTE_ARRAY_PTR(array, size) do{ \
293 if (size > ((array)->len)){ \
294 error_value = PKI_BUFFER_OVERFLOW; \
295 CHECK_PRINT(error_value); \
297 INC_BYTE_ARRAY_PTR(array, size); \
300 /** @def CHECK_INC_BYTE_ARRAY(array, size)
302 * Increments byte array \a array by \a size with bound checking.
304 * @param array byte array pointer
305 * @param size number of positions
307 #undef CHECK_INC_BYTE_ARRAY
308 #define CHECK_INC_BYTE_ARRAY(array, size) do{ \
309 if (size > ((array).len)) { \
310 error_value = PKI_BUFFER_OVERFLOW; \
311 CHECK_PRINT(error_value); \
313 INC_BYTE_ARRAY(array, size); \
320 #endif // _PKI_ERRORS_H_