|
1 /** |
|
2 * XML Security Library (http://www.aleksey.com/xmlsec). |
|
3 * |
|
4 * Error codes and error reporting functions. |
|
5 * |
|
6 * This is free software; see Copyright file in the source |
|
7 * distribution for preciese wording. |
|
8 * |
|
9 * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com> |
|
10 * Portion Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. |
|
11 */ |
|
12 #include "xmlsec_globals.h" |
|
13 |
|
14 #include <stdlib.h> |
|
15 #include <stdio.h> |
|
16 #include <stdarg.h> |
|
17 #include <time.h> |
|
18 |
|
19 #include <libxml2_tree.h> |
|
20 |
|
21 #include "xmlsec_xmlsec.h" |
|
22 #include "xmlsec_xmltree.h" |
|
23 #include "xmlsec_private.h" |
|
24 #include "xmlsec_errors.h" |
|
25 |
|
26 |
|
27 //added for symbian port |
|
28 #include "libxml2_globals.h" |
|
29 |
|
30 #define XMLSEC_ERRORS_BUFFER_SIZE 1024 |
|
31 |
|
32 typedef struct _xmlSecErrorDescription xmlSecErrorDescription, *xmlSecErrorDescriptionPtr; |
|
33 struct _xmlSecErrorDescription { |
|
34 int errorCode; |
|
35 const char* errorMsg; |
|
36 }; |
|
37 |
|
38 static xmlSecErrorDescription xmlSecErrorsTable[XMLSEC_ERRORS_MAX_NUMBER + 1] = { |
|
39 { XMLSEC_ERRORS_R_XMLSEC_FAILED, "xmlsec library function failed" }, |
|
40 { XMLSEC_ERRORS_R_MALLOC_FAILED, "malloc function failed" }, |
|
41 { XMLSEC_ERRORS_R_STRDUP_FAILED, "strdup function failed" }, |
|
42 { XMLSEC_ERRORS_R_CRYPTO_FAILED, "crypto library function failed" }, |
|
43 { XMLSEC_ERRORS_R_XML_FAILED, "libxml2 library function failed" }, |
|
44 { XMLSEC_ERRORS_R_XSLT_FAILED, "libxslt library function failed" }, |
|
45 { XMLSEC_ERRORS_R_IO_FAILED, "io function failed" }, |
|
46 { XMLSEC_ERRORS_R_DISABLED, "feature is disabled" }, |
|
47 { XMLSEC_ERRORS_R_NOT_IMPLEMENTED, "feature is not implemented" }, |
|
48 { XMLSEC_ERRORS_R_INVALID_SIZE, "invalid size" }, |
|
49 { XMLSEC_ERRORS_R_INVALID_DATA, "invalid data" }, |
|
50 { XMLSEC_ERRORS_R_INVALID_RESULT, "invalid result" }, |
|
51 { XMLSEC_ERRORS_R_INVALID_TYPE, "invalid type" }, |
|
52 { XMLSEC_ERRORS_R_INVALID_OPERATION, "invalid operation" }, |
|
53 { XMLSEC_ERRORS_R_INVALID_STATUS, "invalid status" }, |
|
54 { XMLSEC_ERRORS_R_INVALID_FORMAT, "invalid format" }, |
|
55 { XMLSEC_ERRORS_R_DATA_NOT_MATCH, "data do not match" }, |
|
56 { XMLSEC_ERRORS_R_INVALID_NODE, "invalid node" }, |
|
57 { XMLSEC_ERRORS_R_INVALID_NODE_CONTENT, "invalid node content" }, |
|
58 { XMLSEC_ERRORS_R_INVALID_NODE_ATTRIBUTE, "invalid node attribute" }, |
|
59 { XMLSEC_ERRORS_R_MISSING_NODE_ATTRIBUTE, "missing node attribute" }, |
|
60 { XMLSEC_ERRORS_R_NODE_ALREADY_PRESENT, "node already present" }, |
|
61 { XMLSEC_ERRORS_R_UNEXPECTED_NODE, "unexpected node" }, |
|
62 { XMLSEC_ERRORS_R_NODE_NOT_FOUND, "node node found" }, |
|
63 { XMLSEC_ERRORS_R_INVALID_TRANSFORM, "invalid transform" }, |
|
64 { XMLSEC_ERRORS_R_INVALID_TRANSFORM_KEY, "invalid transform key" }, |
|
65 { XMLSEC_ERRORS_R_INVALID_URI_TYPE, "invalid URI type" }, |
|
66 { XMLSEC_ERRORS_R_TRANSFORM_SAME_DOCUMENT_REQUIRED, "same document is required for transform" }, |
|
67 { XMLSEC_ERRORS_R_TRANSFORM_DISABLED, "transform is disabled" }, |
|
68 { XMLSEC_ERRORS_R_INVALID_KEY_DATA, "invalid key data" }, |
|
69 { XMLSEC_ERRORS_R_KEY_DATA_NOT_FOUND, "key data is not found" }, |
|
70 { XMLSEC_ERRORS_R_KEY_DATA_ALREADY_EXIST, "key data already exist" }, |
|
71 { XMLSEC_ERRORS_R_INVALID_KEY_DATA_SIZE, "invalid key data size" }, |
|
72 { XMLSEC_ERRORS_R_KEY_NOT_FOUND, "key is not found" }, |
|
73 { XMLSEC_ERRORS_R_KEYDATA_DISABLED, "key data is disabled" }, |
|
74 { XMLSEC_ERRORS_R_MAX_RETRIEVALS_LEVEL, "maximum key retrieval level" }, |
|
75 { XMLSEC_ERRORS_R_MAX_RETRIEVAL_TYPE_MISMATCH,"key retrieval type mismatch" }, |
|
76 { XMLSEC_ERRORS_R_MAX_ENCKEY_LEVEL, "maximum encrypted key level" }, |
|
77 { XMLSEC_ERRORS_R_CERT_VERIFY_FAILED, "certificate verification failed" }, |
|
78 { XMLSEC_ERRORS_R_CERT_NOT_FOUND, "certificate is not found" }, |
|
79 { XMLSEC_ERRORS_R_CERT_REVOKED, "certificate is revoked" }, |
|
80 { XMLSEC_ERRORS_R_CERT_ISSUER_FAILED, "certificate issuer check failed" }, |
|
81 { XMLSEC_ERRORS_R_CERT_NOT_YET_VALID, "certificate is not yet valid" }, |
|
82 { XMLSEC_ERRORS_R_CERT_HAS_EXPIRED, "certificate has expirred" }, |
|
83 { XMLSEC_ERRORS_R_DSIG_NO_REFERENCES, "Reference nodes are not found" }, |
|
84 { XMLSEC_ERRORS_R_DSIG_INVALID_REFERENCE, "Reference verification failed" }, |
|
85 { XMLSEC_ERRORS_R_ASSERTION, "assertion" }, |
|
86 { 0, NULL} |
|
87 }; |
|
88 |
|
89 static xmlSecErrorsCallback xmlSecErrorsClbk = xmlSecErrorsDefaultCallback; |
|
90 static int xmlSecPrintErrorMessages = 1; /* whether the error messages will be printed immidiatelly */ |
|
91 |
|
92 /** |
|
93 * xmlSecErrorsInit: |
|
94 * |
|
95 * Initializes the errors reporting. It is called from #xmlSecInit function. |
|
96 * and applications must not call this function directly. |
|
97 */ |
|
98 EXPORT_C |
|
99 void |
|
100 xmlSecErrorsInit(void) { |
|
101 } |
|
102 |
|
103 /** |
|
104 * xmlSecErrorsShutdown: |
|
105 * |
|
106 * Cleanups the errors reporting. It is called from #xmlSecShutdown function. |
|
107 * and applications must not call this function directly. |
|
108 */ |
|
109 EXPORT_C |
|
110 void |
|
111 xmlSecErrorsShutdown(void) { |
|
112 } |
|
113 |
|
114 /** |
|
115 * xmlSecErrorsSetCallback: |
|
116 * @callback: the new errors callback function. |
|
117 * |
|
118 * Sets the errors callback function to @callback that will be called |
|
119 * every time an error occurs. |
|
120 */ |
|
121 EXPORT_C |
|
122 void |
|
123 xmlSecErrorsSetCallback(xmlSecErrorsCallback callback) { |
|
124 xmlSecErrorsClbk = callback; |
|
125 } |
|
126 |
|
127 /** |
|
128 * xmlSecErrorsDefaultCallback: |
|
129 * @file: the error location file name (__FILE__ macro). |
|
130 * @line: the error location line number (__LINE__ macro). |
|
131 * @func: the error location function name (__FUNCTION__ macro). |
|
132 * @errorObject: the error specific error object |
|
133 * @errorSubject: the error specific error subject. |
|
134 * @reason: the error code. |
|
135 * @msg: the additional error message. |
|
136 * |
|
137 * The default error reporting callback that utilizes LibXML |
|
138 * error reporting #xmlGenericError function. |
|
139 */ |
|
140 EXPORT_C |
|
141 void |
|
142 xmlSecErrorsDefaultCallback(const char* file, int line, const char* func, |
|
143 const char* errorObject, const char* errorSubject, |
|
144 int reason, const char* msg) { |
|
145 if(xmlSecPrintErrorMessages) { |
|
146 const char* error_msg = NULL; |
|
147 xmlSecSize i; |
|
148 |
|
149 for(i = 0; (i < XMLSEC_ERRORS_MAX_NUMBER) && (xmlSecErrorsGetMsg(i) != NULL); ++i) { |
|
150 if(xmlSecErrorsGetCode(i) == reason) { |
|
151 error_msg = xmlSecErrorsGetMsg(i); |
|
152 break; |
|
153 } |
|
154 } |
|
155 xmlGenericError(xmlGenericErrorContext, |
|
156 "func=%s:file=%s:line=%d:obj=%s:subj=%s:error=%d:%s:%s\n", |
|
157 (func != NULL) ? func : "unknown", |
|
158 (file != NULL) ? file : "unknown", |
|
159 line, |
|
160 (errorObject != NULL) ? errorObject : "unknown", |
|
161 (errorSubject != NULL) ? errorSubject : "unknown", |
|
162 reason, |
|
163 (error_msg != NULL) ? error_msg : "", |
|
164 (msg != NULL) ? msg : ""); |
|
165 } |
|
166 } |
|
167 |
|
168 /** |
|
169 * xmlSecErrorsDefaultCallbackEnableOutput: |
|
170 * @enabled: the flag. |
|
171 * |
|
172 * Enables or disables calling LibXML2 callback from the default |
|
173 * errors callback. |
|
174 */ |
|
175 EXPORT_C |
|
176 void |
|
177 xmlSecErrorsDefaultCallbackEnableOutput(int enabled) { |
|
178 xmlSecPrintErrorMessages = enabled; |
|
179 } |
|
180 |
|
181 /** |
|
182 * xmlSecErrorsGetCode: |
|
183 * @pos: the error position. |
|
184 * |
|
185 * Gets the known error code at position @pos. |
|
186 * |
|
187 * Returns the known error code or 0 if @pos is greater than |
|
188 * total number of known error codes. |
|
189 */ |
|
190 EXPORT_C |
|
191 int |
|
192 xmlSecErrorsGetCode(xmlSecSize pos) { |
|
193 /* could not use asserts here! */ |
|
194 if(pos < sizeof(xmlSecErrorsTable) / sizeof(xmlSecErrorsTable[0])) { |
|
195 return(xmlSecErrorsTable[pos].errorCode); |
|
196 } |
|
197 return(0); |
|
198 } |
|
199 |
|
200 /** |
|
201 * xmlSecErrorsGetMsg: |
|
202 * @pos: the error position. |
|
203 * |
|
204 * Gets the known error message at position @pos. |
|
205 * |
|
206 * Returns the known error message or NULL if @pos is greater than |
|
207 * total number of known error codes. |
|
208 */ |
|
209 EXPORT_C |
|
210 const char* |
|
211 xmlSecErrorsGetMsg(xmlSecSize pos) { |
|
212 /* could not use asserts here! */ |
|
213 if(pos < sizeof(xmlSecErrorsTable) / sizeof(xmlSecErrorsTable[0])) { |
|
214 return(xmlSecErrorsTable[pos].errorMsg); |
|
215 } |
|
216 return(NULL); |
|
217 } |
|
218 |
|
219 /** |
|
220 * xmlSecError: |
|
221 * @file: the error location filename (__FILE__). |
|
222 * @line: the error location line number (__LINE__). |
|
223 * @func: the error location function (__FUNCTIION__). |
|
224 * @errorObject: the error specific error object |
|
225 * @errorSubject: the error specific error subject. |
|
226 * @reason: the error code. |
|
227 * @msg: the error message in printf format. |
|
228 * @...: the parameters for the @msg. |
|
229 * |
|
230 * Reports an error to the default (#xmlSecErrorsDefaultCallback) or |
|
231 * application specific callback installed using #xmlSecErrorsSetCallback |
|
232 * function. |
|
233 */ |
|
234 EXPORT_C |
|
235 void |
|
236 xmlSecError(const char* file, int line, const char* func, |
|
237 const char* errorObject, const char* errorSubject, |
|
238 int reason, const char* msg, ...) { |
|
239 |
|
240 if(xmlSecErrorsClbk != NULL) { |
|
241 xmlChar error_msg[XMLSEC_ERRORS_BUFFER_SIZE]; |
|
242 |
|
243 if(msg != NULL) { |
|
244 va_list va; |
|
245 |
|
246 va_start(va, msg); |
|
247 xmlSecStrVPrintf(error_msg, sizeof(error_msg), BAD_CAST msg, va); |
|
248 error_msg[sizeof(error_msg) - 1] = '\0'; |
|
249 va_end(va); |
|
250 } else { |
|
251 error_msg[0] = '\0'; |
|
252 } |
|
253 xmlSecErrorsClbk(file, line, func, errorObject, errorSubject, reason, (char*)error_msg); |
|
254 } |
|
255 } |