1*06b3ce53SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*06b3ce53SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*06b3ce53SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*06b3ce53SAndrew Rist  * distributed with this work for additional information
6*06b3ce53SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*06b3ce53SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*06b3ce53SAndrew Rist  * "License"); you may not use this file except in compliance
9*06b3ce53SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*06b3ce53SAndrew Rist  *
11*06b3ce53SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*06b3ce53SAndrew Rist  *
13*06b3ce53SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*06b3ce53SAndrew Rist  * software distributed under the License is distributed on an
15*06b3ce53SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*06b3ce53SAndrew Rist  * KIND, either express or implied.  See the License for the
17*06b3ce53SAndrew Rist  * specific language governing permissions and limitations
18*06b3ce53SAndrew Rist  * under the License.
19*06b3ce53SAndrew Rist  *
20*06b3ce53SAndrew Rist  *************************************************************/
21*06b3ce53SAndrew Rist 
22*06b3ce53SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmlsecurity.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir /*
28cdf0e10cSrcweir  * Implementation of the I/O interfaces based on stream and URI binding
29cdf0e10cSrcweir  */
30cdf0e10cSrcweir #include "errorcallback.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <sal/types.h>
33cdf0e10cSrcweir //For reasons that escape me, this is what xmlsec does when size_t is not 4
34cdf0e10cSrcweir #if SAL_TYPES_SIZEOFPOINTER != 4
35cdf0e10cSrcweir #    define XMLSEC_NO_SIZE_T
36cdf0e10cSrcweir #endif
37cdf0e10cSrcweir #include "xmlsec/xmlsec.h"
38cdf0e10cSrcweir #include "xmlsec/errors.h"
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using namespace ::com::sun::star::xml::crypto;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir // ::com::sun::star::uno::Reference< XXMLSecurityTemplate > g_xErrorRecorder;
43cdf0e10cSrcweir // bool g_bErrorRecorded;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir // SecurityOperationStatus getOperationStatus(int reason)
46cdf0e10cSrcweir // {
47cdf0e10cSrcweir // 	switch (reason)
48cdf0e10cSrcweir // 	{
49cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_XMLSEC_FAILED:
50cdf0e10cSrcweir // 		return SecurityOperationStatus_ENGINE_FAILED;
51cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_MALLOC_FAILED:
52cdf0e10cSrcweir // 		return SecurityOperationStatus_MALLOC_FAILED;
53cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_STRDUP_FAILED:
54cdf0e10cSrcweir // 		return SecurityOperationStatus_STRDUP_FAILED;
55cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_CRYPTO_FAILED:
56cdf0e10cSrcweir // 		return SecurityOperationStatus_CRYPTO_FAILED;
57cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_XML_FAILED:
58cdf0e10cSrcweir // 		return SecurityOperationStatus_XML_FAILED;
59cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_XSLT_FAILED:
60cdf0e10cSrcweir // 		return SecurityOperationStatus_XSLT_FAILED;
61cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_IO_FAILED:
62cdf0e10cSrcweir // 		return SecurityOperationStatus_IO_FAILED;
63cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_DISABLED:
64cdf0e10cSrcweir // 		return SecurityOperationStatus_DISABLED;
65cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_NOT_IMPLEMENTED:
66cdf0e10cSrcweir // 		return SecurityOperationStatus_NOT_IMPLEMENTED;
67cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_INVALID_SIZE:
68cdf0e10cSrcweir // 		return SecurityOperationStatus_INVALID_SIZE;
69cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_INVALID_DATA:
70cdf0e10cSrcweir // 		return SecurityOperationStatus_INVALID_DATA;
71cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_INVALID_RESULT:
72cdf0e10cSrcweir // 		return SecurityOperationStatus_INVALID_RESULT;
73cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_INVALID_TYPE:
74cdf0e10cSrcweir // 		return SecurityOperationStatus_INVALID_TYPE;
75cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_INVALID_OPERATION:
76cdf0e10cSrcweir // 		return SecurityOperationStatus_INVALID_OPERATION;
77cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_INVALID_STATUS:
78cdf0e10cSrcweir // 		return SecurityOperationStatus_INVALID_STATUS;
79cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_INVALID_FORMAT:
80cdf0e10cSrcweir // 		return SecurityOperationStatus_INVALID_FORMAT;
81cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_DATA_NOT_MATCH:
82cdf0e10cSrcweir // 		return SecurityOperationStatus_DATA_NOT_MATCH;
83cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_INVALID_NODE:
84cdf0e10cSrcweir // 		return SecurityOperationStatus_INVALID_NODE;
85cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_INVALID_NODE_CONTENT:
86cdf0e10cSrcweir // 		return SecurityOperationStatus_INVALID_NODE_CONTENT;
87cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_INVALID_NODE_ATTRIBUTE:
88cdf0e10cSrcweir // 		return SecurityOperationStatus_INVALID_NODE_ATTRIBUTE;
89cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_MISSING_NODE_ATTRIBUTE:
90cdf0e10cSrcweir // 		return SecurityOperationStatus_MISSING_NODE_ATTRIBUTE;
91cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_NODE_ALREADY_PRESENT:
92cdf0e10cSrcweir // 		return SecurityOperationStatus_NODE_ALREADY_PRESENT;
93cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_UNEXPECTED_NODE:
94cdf0e10cSrcweir // 		return SecurityOperationStatus_UNEXPECTED_NODE;
95cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_NODE_NOT_FOUND:
96cdf0e10cSrcweir // 		return SecurityOperationStatus_NODE_NOT_FOUND;
97cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_INVALID_TRANSFORM:
98cdf0e10cSrcweir // 		return SecurityOperationStatus_INVALID_TRANSFORM;
99cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_INVALID_TRANSFORM_KEY:
100cdf0e10cSrcweir // 		return SecurityOperationStatus_INVALID_TRANSFORM_KEY;
101cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_INVALID_URI_TYPE:
102cdf0e10cSrcweir // 		return SecurityOperationStatus_INVALID_URI_TYPE;
103cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_TRANSFORM_SAME_DOCUMENT_REQUIRED:
104cdf0e10cSrcweir // 		return SecurityOperationStatus_TRANSFORM_SAME_DOCUMENT_REQUIRED;
105cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_TRANSFORM_DISABLED:
106cdf0e10cSrcweir // 		return SecurityOperationStatus_TRANSFORM_DISABLED;
107cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_INVALID_KEY_DATA:
108cdf0e10cSrcweir // 		return SecurityOperationStatus_INVALID_KEY_DATA;
109cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_KEY_DATA_NOT_FOUND:
110cdf0e10cSrcweir // 		return SecurityOperationStatus_KEY_DATA_NOT_FOUND;
111cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_KEY_DATA_ALREADY_EXIST:
112cdf0e10cSrcweir // 		return SecurityOperationStatus_KEY_DATA_ALREADY_EXIST;
113cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_INVALID_KEY_DATA_SIZE:
114cdf0e10cSrcweir // 		return SecurityOperationStatus_INVALID_KEY_DATA_SIZE;
115cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_KEY_NOT_FOUND:
116cdf0e10cSrcweir // 		return SecurityOperationStatus_KEY_NOT_FOUND;
117cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_KEYDATA_DISABLED:
118cdf0e10cSrcweir // 		return SecurityOperationStatus_KEYDATA_DISABLED;
119cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_MAX_RETRIEVALS_LEVEL:
120cdf0e10cSrcweir // 		return SecurityOperationStatus_MAX_RETRIEVALS_LEVEL;
121cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_MAX_RETRIEVAL_TYPE_MISMATCH:
122cdf0e10cSrcweir // 		return SecurityOperationStatus_MAX_RETRIEVAL_TYPE_MISMATCH;
123cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_MAX_ENCKEY_LEVEL:
124cdf0e10cSrcweir // 		return SecurityOperationStatus_MAX_ENCKEY_LEVEL;
125cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_CERT_VERIFY_FAILED:
126cdf0e10cSrcweir // 		return SecurityOperationStatus_CERT_VERIFY_FAILED;
127cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_CERT_NOT_FOUND:
128cdf0e10cSrcweir // 		return SecurityOperationStatus_CERT_NOT_FOUND;
129cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_CERT_REVOKED:
130cdf0e10cSrcweir // 		return SecurityOperationStatus_CERT_REVOKED;
131cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_CERT_ISSUER_FAILED:
132cdf0e10cSrcweir // 		return SecurityOperationStatus_CERT_ISSUER_FAILED;
133cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_CERT_NOT_YET_VALID:
134cdf0e10cSrcweir // 		return SecurityOperationStatus_CERT_NOT_YET_VALID;
135cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_CERT_HAS_EXPIRED:
136cdf0e10cSrcweir // 		return SecurityOperationStatus_CERT_HAS_EXPIRED;
137cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_DSIG_NO_REFERENCES:
138cdf0e10cSrcweir // 		return SecurityOperationStatus_DSIG_NO_REFERENCES;
139cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_DSIG_INVALID_REFERENCE:
140cdf0e10cSrcweir // 		return SecurityOperationStatus_DSIG_INVALID_REFERENCE;
141cdf0e10cSrcweir // 	case XMLSEC_ERRORS_R_ASSERTION:
142cdf0e10cSrcweir // 		return SecurityOperationStatus_ASSERTION;
143cdf0e10cSrcweir // 	default:
144cdf0e10cSrcweir // 		return SecurityOperationStatus_RUNTIMEERROR_FAILED;
145cdf0e10cSrcweir // 	}
146cdf0e10cSrcweir // }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 
149cdf0e10cSrcweir extern "C"
errorCallback(const char *,int,const char *,const char *,const char *,int,const char *)150cdf0e10cSrcweir void errorCallback(const char * /*file*/,
151cdf0e10cSrcweir                    int /*line*/,
152cdf0e10cSrcweir                    const char * /*func*/,
153cdf0e10cSrcweir                    const char * /*errorObject*/,
154cdf0e10cSrcweir                    const char * /*errorSubject*/,
155cdf0e10cSrcweir                    int /*reason*/,
156cdf0e10cSrcweir                    const char  * /*msg*/)
157cdf0e10cSrcweir {
158cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
159cdf0e10cSrcweir //     const char * afunc = func ? func : "";
160cdf0e10cSrcweir //     const char * errObj = errorObject ? errorObject : "";
161cdf0e10cSrcweir //     const char * errSub = errorSubject ? errorSubject : "";
162cdf0e10cSrcweir //     const char * amsg = msg ? msg : "";
163cdf0e10cSrcweir // 	fprintf(stdout, "xmlsec error: %s, %s,  %s, %i %s  \n", afunc, errObj, errSub, reason, amsg);
164cdf0e10cSrcweir #endif
165cdf0e10cSrcweir     //ToDo write log message
166cdf0e10cSrcweir // 	if (g_xErrorRecorder.is() && !g_bErrorRecorded)
167cdf0e10cSrcweir // 	{
168cdf0e10cSrcweir // 		g_xErrorRecorder->setStatus(getOperationStatus(reason));
169cdf0e10cSrcweir 
170cdf0e10cSrcweir // 		if ( reason != XMLSEC_ERRORS_R_ASSERTION && reason!=XMLSEC_ERRORS_R_XMLSEC_FAILED)
171cdf0e10cSrcweir // 		{
172cdf0e10cSrcweir //             g_bErrorRecorded = true;
173cdf0e10cSrcweir // 		}
174cdf0e10cSrcweir // 	}
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir // void setErrorRecorder(const ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XXMLSecurityTemplate >& xTemplate)
178cdf0e10cSrcweir // {
179cdf0e10cSrcweir // 	g_xErrorRecorder = xTemplate;
180cdf0e10cSrcweir // 	g_xErrorRecorder->setStatus(SecurityOperationStatus_OPERATION_SUCCEEDED);
181cdf0e10cSrcweir // 	g_bErrorRecorded = false;
182cdf0e10cSrcweir // 	xmlSecErrorsSetCallback(errorCallback);
183cdf0e10cSrcweir // }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir //void setErrorRecorder(const ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XXMLSignatureTemplate >& xTemplate)
186cdf0e10cSrcweir 
setErrorRecorder()187cdf0e10cSrcweir void setErrorRecorder()
188cdf0e10cSrcweir {
189cdf0e10cSrcweir // 	::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XXMLSecurityTemplate >
190cdf0e10cSrcweir // 		xSecurityTemplate(xTemplate, ::com::sun::star::uno::UNO_QUERY);
191cdf0e10cSrcweir // 	setErrorRecorder( xSecurityTemplate );
192cdf0e10cSrcweir     xmlSecErrorsSetCallback(errorCallback);
193cdf0e10cSrcweir }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir // void setErrorRecorder(const ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XXMLEncryptionTemplate >& xTemplate)
196cdf0e10cSrcweir // {
197cdf0e10cSrcweir // 	::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XXMLSecurityTemplate >
198cdf0e10cSrcweir // 		xSecurityTemplate(xTemplate, ::com::sun::star::uno::UNO_QUERY);
199cdf0e10cSrcweir // 	setErrorRecorder( xSecurityTemplate );
200cdf0e10cSrcweir // }
201cdf0e10cSrcweir 
clearErrorRecorder()202cdf0e10cSrcweir void clearErrorRecorder()
203cdf0e10cSrcweir {
204cdf0e10cSrcweir 	xmlSecErrorsSetCallback(NULL);
205cdf0e10cSrcweir //	g_xErrorRecorder = NULL;
206cdf0e10cSrcweir }
207cdf0e10cSrcweir 
208