Add certificate verification
[platform/upstream/iotivity.git] / resource / csdk / connectivity / inc / pkix / pki_errors.h
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      LICENSE-2.0" target="_blank">http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19
20  ******************************************************************/
21
22
23 #ifndef _PKI_ERRORS_H_
24 #define _PKI_ERRORS_H_
25
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif //__cplusplus
30
31 #ifdef X509_DEBUG
32 #warning "DEBUG is enabled"
33 #include <stdio.h>  // <printf>
34 #endif
35
36 /**
37  * @enum PKIError
38  *
39  * The enumeration of error codes.
40  */
41
42 typedef enum
43 {
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. */
62
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
73                                                      storage */
74     ISSUER_CA_STORAGE_CRL_SN_UNDEFINED,         /**< CRL serial number is not defined in CA
75                                                      storage */
76     ISSUER_CA_STORAGE_WRONG_PRIVATE_KEY_LEN,    /**< Passed private key length not equal to
77                                                      PRIVATE_KEY_SIZE*/
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
80                                                      PUBLIC_KEY_SIZE*/
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
83                                                      CA storage */
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
91                                                      CA storage */
92
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 */
100
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
103                                                  in CRL encoder */
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
107                                                  CRL encoder */
108
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 */
118
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 */
124 } PKIError;
125
126
127 /** @def CHECK_PRINT(err_code, ...)
128  *
129  * Prints debug information \a err_code and  __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__ values.
130  *
131  * @param[in] err_code Error code
132 */
133 #ifdef X509_DEBUG
134 #define CHECK_PRINT(err_code) \
135 fprintf(stderr, "%s() in %s, line %i: %d\n",__func__, __FILE__, __LINE__, err_code);
136 #else
137 # define CHECK_PRINT(...)
138 #endif
139
140 /**
141  * @def CHECK_NULL(param, error)
142  * A macro that checks whether \a param is not NULL.
143  *
144  * If \a (param = NULL) it goes to error processing with \a error code.
145  *
146  * @param[in] param  Parameter to check
147  * @param[in] error Error code
148  */
149 #define CHECK_NULL(param, error) do {   \
150         if  (!(param)) {                \
151             error_value = error;        \
152             CHECK_PRINT(error);         \
153             goto ERROR_PROC;            \
154         } } while(0)
155
156 /** @def CHECK_EQUAL(param, checker, err_code)
157  *
158  * A macro that checks whether \a param equal to \a checker.
159  *
160  * If \a (param != checker) it goes to error processing with \a err_code error code.
161  *
162  * @param[in] param  Parameter to check
163  * @param[in] error Error code
164  */
165 #define CHECK_EQUAL(param, checker, err_code) do { \
166         if ((param) != (checker)) {                \
167             error_value = err_code;                \
168             CHECK_PRINT(err_code);                 \
169             goto ERROR_PROC;                       \
170         } } while(0)
171
172 /** @def CHECK_NOT_EQUAL(param, checker, err_code)
173  * A macro that checks whether \a param not equal to \a checker.
174  *
175  * If \a (param == checker) it goes to error processing with \a err_code error code.
176  *
177  * @param[in] param  Parameter to check
178  * @param[in] error Error code
179  */
180 #define CHECK_NOT_EQUAL(param, checker, err_code) do { \
181         if ((param) == (checker)) {                    \
182             error_value = err_code;                    \
183             CHECK_PRINT(err_code);                     \
184             goto ERROR_PROC;                           \
185         } } while(0)
186
187 /** @def CHECK_LESS(param, checker, err_code)
188  * A macro that checks whether \a param less then \a checker.
189  *
190  * If \a (param > checker) it goes to error processing with \a err_code error code.
191  *
192  * @param[in] param  Parameter to check
193  * @param[in] error error code
194  */
195 #define CHECK_LESS(param, checker, err_code) do { \
196         if ((param) >= (checker)) {               \
197             error_value = err_code;               \
198             CHECK_PRINT(err_code);                \
199             goto ERROR_PROC;                      \
200         } } while(0)
201
202 /** @def CHECK_COND(param, err_code)
203  *
204  * A macro that checks whether condition \a param is true.
205  *
206  * If \a (param != true) it goes to error processing with \a err_code error code.
207  *
208  * @param[in] param  Parameter to check
209  * @param[in] error error code
210  */
211 #define CHECK_COND(param, err_code) do { \
212         if (!(param)) {                  \
213             error_value = err_code;      \
214             CHECK_PRINT(err_code);       \
215             goto ERROR_PROC;             \
216         } } while(0)
217
218 /** @def CHECK_LESS_EQUAL(param, checker, err_code)
219 *
220 * A macro that checks whether \a param <= \a checker.
221 *
222 * If \a (param < checker) it goes to error processing with \a err_code error code.
223 *
224 * @param[in] param  Parameter to check
225 * @param[in] error error code
226 */
227 #define CHECK_LESS_EQUAL(param, checker, err_code) do { \
228         if ((param) > (checker)) {                      \
229             error_value = err_code;                     \
230             CHECK_PRINT(err_code);                      \
231             goto ERROR_PROC;                            \
232         } } while(0)
233
234 /** @def CHECK_NULL_BYTE_ARRAY_PTR(param, err_code)
235  *
236  * A macro that checks whether pointer to ByteArray \a param is not NULL and contains a valid pointer.
237  *
238  * If \a (param != checker) it goes to error processing with \a err_code error code.
239  *
240  * @param[in] param  Parameter to check
241  * @param[in] err_code Error code
242  */
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);             \
247     } while(0)
248
249 /** @def FUNCTION_INIT(...)
250  * A macro for initializations function variables.
251  *
252  * If error occurs it goes to error processing.
253  */
254 #define FUNCTION_INIT(...)                    \
255     PKIError error_value = PKI_UNKNOWN_ERROR; \
256     __VA_ARGS__;
257
258 /** @def FUNCTION_CLEAR(...)
259  *
260  * A macro for freeing  function variables.
261  *
262  * @return  0 if successful
263  */
264 #define FUNCTION_CLEAR(...)    \
265     error_value = PKI_SUCCESS; \
266     ERROR_PROC:                \
267     __VA_ARGS__                \
268     return error_value;
269
270 /** @def CHECK_CALL(fn, ...)
271  * A macro that checks \a fn function return code
272  *
273  * If function return error code it goes to error processing.
274  *
275  * @param[in] fn  Function to call
276  */
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);               \
281             goto ERROR_PROC;                        \
282         } } while(0)
283
284 /** @def CHECK_INC_BYTE_ARRAY_PTR(array, size)
285  *
286  * Increments byte array pointer \a array by \a size with bound checking.
287  *
288  * @param array byte array pointer
289  * @param size number of positions
290  */
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);               \
296             goto ERROR_PROC; }                      \
297         INC_BYTE_ARRAY_PTR(array, size);            \
298     }while(0)
299
300 /** @def CHECK_INC_BYTE_ARRAY(array, size)
301  *
302  * Increments byte array \a array by \a size with bound checking.
303  *
304  * @param array byte array pointer
305  * @param size number of positions
306  */
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);           \
312             goto ERROR_PROC; }                  \
313         INC_BYTE_ARRAY(array, size);            \
314     }while(0)
315
316 #ifdef __cplusplus
317 }
318 #endif //__cplusplus
319
320 #endif // _PKI_ERRORS_H_