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 #include <stdio.h>
28cdf0e10cSrcweir #include <string.h>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <rtl/ustring.hxx>
31cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx>
32cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
33cdf0e10cSrcweir #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
34cdf0e10cSrcweir #include <com/sun/star/registry/XImplementationRegistration.hpp>
35cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
36cdf0e10cSrcweir #include <com/sun/star/lang/XMultiComponentFactory.hpp>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include <iostream>
41cdf0e10cSrcweir #include <fstream>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #include <util.hxx>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
46cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
47cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
48cdf0e10cSrcweir #include <com/sun/star/xml/sax/XParser.hpp>
49cdf0e10cSrcweir #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
50cdf0e10cSrcweir #include <com/sun/star/xml/sax/XAttributeList.hpp>
51cdf0e10cSrcweir #include <cppuhelper/implbase4.hxx>
52cdf0e10cSrcweir 
53cdf0e10cSrcweir #include <com/sun/star/xml/crypto/sax/XSignatureCreationResultListener.hpp>
54cdf0e10cSrcweir #include <com/sun/star/xml/crypto/sax/XSignatureVerifyResultListener.hpp>
55cdf0e10cSrcweir #include <com/sun/star/xml/crypto/sax/XSAXEventKeeperStatusChangeListener.hpp>
56cdf0e10cSrcweir #include <com/sun/star/xml/crypto/sax/XSecuritySAXEventKeeper.hpp>
57cdf0e10cSrcweir #include <com/sun/star/xml/crypto/sax/XReferenceResolvedListener.hpp>
58cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XXMLSignature.hpp>
59cdf0e10cSrcweir #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
60cdf0e10cSrcweir #include <com/sun/star/xml/csax/XMLAttribute.hpp>
61cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XSEInitializer.hpp>
62cdf0e10cSrcweir #include <com/sun/star/xml/crypto/SecurityOperationStatus.hpp>
63cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
64cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
65cdf0e10cSrcweir #include <com/sun/star/xml/crypto/sax/XKeyCollector.hpp>
66cdf0e10cSrcweir #include <com/sun/star/xml/crypto/sax/ElementMarkPriority.hpp>
67cdf0e10cSrcweir #include <com/sun/star/xml/crypto/sax/XReferenceResolvedBroadcaster.hpp>
68cdf0e10cSrcweir #include <com/sun/star/xml/crypto/sax/XMissionTaker.hpp>
69cdf0e10cSrcweir #include <com/sun/star/xml/crypto/sax/XBlockerMonitor.hpp>
70cdf0e10cSrcweir #include <com/sun/star/xml/crypto/sax/XSignatureCreationResultBroadcaster.hpp>
71cdf0e10cSrcweir #include <com/sun/star/xml/crypto/sax/XSignatureVerifyResultBroadcaster.hpp>
72cdf0e10cSrcweir #include <com/sun/star/xml/crypto/sax/XReferenceCollector.hpp>
73cdf0e10cSrcweir #include <com/sun/star/xml/crypto/sax/XSAXEventKeeperStatusChangeBroadcaster.hpp>
74cdf0e10cSrcweir #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
75cdf0e10cSrcweir 
76cdf0e10cSrcweir #include <xmloff/attrlist.hxx>
77cdf0e10cSrcweir 
78cdf0e10cSrcweir //#include <malloc.h>
79cdf0e10cSrcweir #include <stdio.h>
80cdf0e10cSrcweir #include <stdlib.h>
81cdf0e10cSrcweir #include <string.h>
82cdf0e10cSrcweir 
83cdf0e10cSrcweir /*
84cdf0e10cSrcweir  * Can not build under solaris.
85cdf0e10cSrcweir  * Delete the memory.h including by AF
86cdf0e10cSrcweir #include <memory.h>
87cdf0e10cSrcweir */
88cdf0e10cSrcweir 
89cdf0e10cSrcweir #include <sys/types.h>
90cdf0e10cSrcweir #include <sys/stat.h>
91cdf0e10cSrcweir #include <osl/time.h>
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 
95cdf0e10cSrcweir #ifndef INCLUDED_VECTOR
96cdf0e10cSrcweir #include <vector>
97cdf0e10cSrcweir #define INCLUDED_VECTOR
98cdf0e10cSrcweir #endif
99cdf0e10cSrcweir 
100cdf0e10cSrcweir #ifndef INCLUDED_STACK
101cdf0e10cSrcweir #include <stack>
102cdf0e10cSrcweir #define INCLUDED_STACK
103cdf0e10cSrcweir #endif
104cdf0e10cSrcweir 
105cdf0e10cSrcweir /* xml security framework components */
106cdf0e10cSrcweir #define SIGNATURECREATOR_COMPONENT  "com.sun.star.xml.crypto.sax.SignatureCreator"
107cdf0e10cSrcweir #define SIGNATUREVERIFIER_COMPONENT "com.sun.star.xml.crypto.sax.SignatureVerifier"
108cdf0e10cSrcweir #define JAVAFLATFILTER_COMPONENT    "com.sun.star.xml.crypto.eval.JavaFlatFilter"
109cdf0e10cSrcweir #define SAXEVENTKEEPER_COMPONENT    "com.sun.star.xml.crypto.sax.SAXEventKeeper"
110cdf0e10cSrcweir 
111cdf0e10cSrcweir /* java based bridge components */
112cdf0e10cSrcweir #define SEINITIALIZER_JAVA_COMPONENT      "com.sun.star.xml.security.bridge.jxsec.SEInitializer_JxsecImpl"
113cdf0e10cSrcweir #define XMLSIGNATURE_JAVA_COMPONENT       "com.sun.star.xml.security.bridge.jxsec.XMLSignature_JxsecImpl"
114cdf0e10cSrcweir #define XMLDOCUMENTWRAPPER_JAVA_COMPONENT "com.sun.star.xml.security.bridge.jxsec.XMLDocumentWrapper_JxsecImpl"
115cdf0e10cSrcweir 
116cdf0e10cSrcweir /* c based bridge components */
117cdf0e10cSrcweir #define SEINITIALIZER_C_COMPONENT "com.sun.star.xml.crypto.SEInitializer"
118cdf0e10cSrcweir #define XMLSIGNATURE_C_COMPONENT "com.sun.star.xml.crypto.XMLSignature"
119cdf0e10cSrcweir #define XMLDOCUMENT_C_COMPONENT "com.sun.star.xml.wrapper.XMLDocumentWrapper"
120cdf0e10cSrcweir 
121cdf0e10cSrcweir /* security related elements and attributes */
122cdf0e10cSrcweir #define SIGNATURE_STR       "Signature"
123cdf0e10cSrcweir #define REFERENCE_STR       "Reference"
124cdf0e10cSrcweir #define SIGNEDINFO_STR      "SignedInfo"
125cdf0e10cSrcweir #define KEYINFO_STR         "KeyInfo"
126cdf0e10cSrcweir #define KEYVALUE_STR        "KeyValue"
127cdf0e10cSrcweir #define KEYNAME_STR         "KeyName"
128cdf0e10cSrcweir #define X509DATA_STR        "X509Data"
129cdf0e10cSrcweir #define ENCRYPTEDKEY_STR    "EncryptedKey"
130cdf0e10cSrcweir #define RETRIEVALMETHOD_STR "RetrievalMethod"
131cdf0e10cSrcweir #define OTHER_ELEMENT_STR   "OTHER_ELEMENT_STR"
132cdf0e10cSrcweir #define REFNUM_ATTR_STR     "refNum"
133cdf0e10cSrcweir #define URI_ATTR_STR        "URI"
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 
136cdf0e10cSrcweir #define RTL_ASCII_USTRINGPARAM( asciiStr ) asciiStr, strlen( asciiStr ), RTL_TEXTENCODING_ASCII_US
137cdf0e10cSrcweir 
138cdf0e10cSrcweir namespace cssu = com::sun::star::uno;
139cdf0e10cSrcweir namespace cssl = com::sun::star::lang;
140cdf0e10cSrcweir namespace cssb = com::sun::star::beans;
141cdf0e10cSrcweir namespace cssi = com::sun::star::io;
142cdf0e10cSrcweir namespace cssxc = com::sun::star::xml::crypto;
143cdf0e10cSrcweir namespace cssxs = com::sun::star::xml::sax;
144cdf0e10cSrcweir namespace cssxw = com::sun::star::xml::wrapper;
145cdf0e10cSrcweir namespace cssxcsax = com::sun::star::xml::csax;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 
148cdf0e10cSrcweir using namespace ::com::sun::star;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 
151cdf0e10cSrcweir class XSecTester;
152cdf0e10cSrcweir 
153cdf0e10cSrcweir /*
154cdf0e10cSrcweir  * The XSecTester class is a C++ version of SecurityFramworkController.java
155cdf0e10cSrcweir  *
156cdf0e10cSrcweir  */
157cdf0e10cSrcweir 
158cdf0e10cSrcweir class SecurityEntity
159cdf0e10cSrcweir {
160cdf0e10cSrcweir private:
161cdf0e10cSrcweir 	static int m_nNextSecurityId;
162cdf0e10cSrcweir 	rtl::OUString m_ouKeyURI;
163cdf0e10cSrcweir 
164cdf0e10cSrcweir protected:
165cdf0e10cSrcweir 	com::sun::star::uno::Reference<
166cdf0e10cSrcweir 		com::sun::star::lang::XMultiServiceFactory > mxMSF;
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 	com::sun::star::uno::Reference<
169cdf0e10cSrcweir 		com::sun::star::xml::crypto::sax::XReferenceResolvedListener >
170cdf0e10cSrcweir 		m_xReferenceListener;
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 	com::sun::star::uno::Reference<
173cdf0e10cSrcweir 		com::sun::star::xml::crypto::sax::XSecuritySAXEventKeeper >
174cdf0e10cSrcweir 		m_xSAXEventKeeper;
175cdf0e10cSrcweir 
176cdf0e10cSrcweir 	com::sun::star::uno::Reference<
177cdf0e10cSrcweir 		com::sun::star::xml::crypto::XXMLSecurityContext >
178cdf0e10cSrcweir 		m_xXMLSecurityContext;
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 	com::sun::star::uno::Reference<
181cdf0e10cSrcweir 		com::sun::star::xml::crypto::XXMLSignature >
182cdf0e10cSrcweir 		m_xXMLSignature;
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 	int m_nSecurityId;
185cdf0e10cSrcweir 
186cdf0e10cSrcweir private:
187cdf0e10cSrcweir 	int getNextSecurityId() const;
188cdf0e10cSrcweir 
189cdf0e10cSrcweir protected:
190cdf0e10cSrcweir 	SecurityEntity(
191cdf0e10cSrcweir 		const com::sun::star::uno::Reference<
192cdf0e10cSrcweir 			com::sun::star::xml::crypto::sax::XSecuritySAXEventKeeper >&
193cdf0e10cSrcweir 			xSAXEventKeeper,
194cdf0e10cSrcweir 		const com::sun::star::uno::Reference<
195cdf0e10cSrcweir 			com::sun::star::xml::crypto::XXMLSecurityContext >&
196cdf0e10cSrcweir 			xXMLSecurityContext,
197cdf0e10cSrcweir 		const com::sun::star::uno::Reference<
198cdf0e10cSrcweir 			com::sun::star::xml::crypto::XXMLSignature >&
199cdf0e10cSrcweir 			xXMLSignature,
200cdf0e10cSrcweir 		const com::sun::star::uno::Reference<
201cdf0e10cSrcweir 			com::sun::star::lang::XMultiServiceFactory >&
202cdf0e10cSrcweir 			rsMSF);
203cdf0e10cSrcweir 
204cdf0e10cSrcweir public:
205cdf0e10cSrcweir 	void setKeyId(int nId);
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 	int getSecurityId() const;
208cdf0e10cSrcweir 
209cdf0e10cSrcweir 	com::sun::star::uno::Reference<
210cdf0e10cSrcweir 		com::sun::star::xml::crypto::sax::XReferenceResolvedListener >
211cdf0e10cSrcweir 		getReferenceListener() const;
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 	bool setKey( const rtl::OUString& ouUri, bool bIsExporting );
214cdf0e10cSrcweir 
215cdf0e10cSrcweir 	void setKeyURI(const rtl::OUString& ouUri);
216cdf0e10cSrcweir 
217cdf0e10cSrcweir 	bool endMission();
218cdf0e10cSrcweir };
219cdf0e10cSrcweir 
220cdf0e10cSrcweir 
221cdf0e10cSrcweir class SignatureEntity : public SecurityEntity
222cdf0e10cSrcweir {
223cdf0e10cSrcweir private:
224cdf0e10cSrcweir 	std::vector< rtl::OUString > m_vReferenceIds;
225cdf0e10cSrcweir 	int m_nSignatureElementCollectorId;
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 	bool hasReference(const rtl::OUString& ouUri) const;
228cdf0e10cSrcweir 
229cdf0e10cSrcweir public:
230cdf0e10cSrcweir 	SignatureEntity(
231cdf0e10cSrcweir 		const com::sun::star::uno::Reference<
232cdf0e10cSrcweir 			com::sun::star::xml::crypto::sax::XSecuritySAXEventKeeper >&
233cdf0e10cSrcweir 			xSAXEventKeeper,
234cdf0e10cSrcweir 		bool bIsExporting,
235cdf0e10cSrcweir 		XSecTester* pListener,
236cdf0e10cSrcweir 		const com::sun::star::uno::Reference<
237cdf0e10cSrcweir 			com::sun::star::xml::crypto::XXMLSecurityContext >&
238cdf0e10cSrcweir 			xXMLSecurityContext,
239cdf0e10cSrcweir 		const com::sun::star::uno::Reference<
240cdf0e10cSrcweir 			com::sun::star::xml::crypto::XXMLSignature >&
241cdf0e10cSrcweir 			xXMLSignature,
242cdf0e10cSrcweir 		const com::sun::star::uno::Reference<
243cdf0e10cSrcweir 			com::sun::star::lang::XMultiServiceFactory >&
244cdf0e10cSrcweir 			rsMSF);
~SignatureEntity()245cdf0e10cSrcweir 	~SignatureEntity(){};
246cdf0e10cSrcweir 
247cdf0e10cSrcweir 	void setReferenceNumber() const;
248cdf0e10cSrcweir 	bool setReference( const rtl::OUString& ouUri, bool bIsExporting ) const;
249cdf0e10cSrcweir 	void addReferenceURI( const rtl::OUString& ouUri );
250cdf0e10cSrcweir };
251cdf0e10cSrcweir 
252cdf0e10cSrcweir struct AncestorEvent
253cdf0e10cSrcweir {
AncestorEventAncestorEvent254cdf0e10cSrcweir 	AncestorEvent( sal_Int32 nAttrNum ):aAttributeList(nAttrNum){};
255cdf0e10cSrcweir 
256cdf0e10cSrcweir 	bool bIsStartElement;
257cdf0e10cSrcweir 	rtl::OUString ouName;
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 	com::sun::star::uno::Sequence<
260cdf0e10cSrcweir 		com::sun::star::xml::csax::XMLAttribute >
261cdf0e10cSrcweir 		aAttributeList;
262cdf0e10cSrcweir };
263cdf0e10cSrcweir 
264cdf0e10cSrcweir class XSecTester : public cppu::WeakImplHelper4
265cdf0e10cSrcweir <
266cdf0e10cSrcweir 	com::sun::star::xml::crypto::sax::XSignatureCreationResultListener,
267cdf0e10cSrcweir 	com::sun::star::xml::crypto::sax::XSignatureVerifyResultListener,
268cdf0e10cSrcweir 	com::sun::star::xml::crypto::sax::XSAXEventKeeperStatusChangeListener,
269cdf0e10cSrcweir 	com::sun::star::xml::sax::XDocumentHandler
270cdf0e10cSrcweir >
271cdf0e10cSrcweir {
272cdf0e10cSrcweir private:
273cdf0e10cSrcweir 	com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory > mxMSF;
274cdf0e10cSrcweir 
275cdf0e10cSrcweir 	sal_Int32 m_nTotalSignatureNumber;
276cdf0e10cSrcweir 	sal_Int32 m_nSuccessfulSignatureNumber;
277cdf0e10cSrcweir 
278cdf0e10cSrcweir 	com::sun::star::uno::Reference<
279cdf0e10cSrcweir 		com::sun::star::xml::sax::XDocumentHandler >
280cdf0e10cSrcweir 		m_xExportHandler;
281cdf0e10cSrcweir 
282cdf0e10cSrcweir 	com::sun::star::uno::Reference<
283cdf0e10cSrcweir 		com::sun::star::xml::crypto::sax::XSecuritySAXEventKeeper >
284cdf0e10cSrcweir 		m_xSAXEventKeeper;
285cdf0e10cSrcweir 
286cdf0e10cSrcweir 	com::sun::star::uno::Reference<
287cdf0e10cSrcweir 		com::sun::star::xml::wrapper::XXMLDocumentWrapper >
288cdf0e10cSrcweir 		m_xXMLDocumentWrapper;
289cdf0e10cSrcweir 
290cdf0e10cSrcweir 	com::sun::star::uno::Reference<
291cdf0e10cSrcweir 		com::sun::star::xml::sax::XDocumentHandler >
292cdf0e10cSrcweir 		m_xOutputHandler;
293cdf0e10cSrcweir 
294cdf0e10cSrcweir 	com::sun::star::uno::Reference<
295cdf0e10cSrcweir 		com::sun::star::xml::sax::XParser >
296cdf0e10cSrcweir 		m_xSaxParser;
297cdf0e10cSrcweir 
298cdf0e10cSrcweir 	std::stack< void* > m_stCurrentPath;
299cdf0e10cSrcweir 	std::stack< bool > m_stCurrentPathType;
300cdf0e10cSrcweir 
301cdf0e10cSrcweir 	std::vector< AncestorEvent* > m_vAncestorEvents;
302cdf0e10cSrcweir 	std::vector< SignatureEntity* > m_vSignatureList;
303cdf0e10cSrcweir 
304cdf0e10cSrcweir 	std::vector< rtl::OUString > m_vUnsolvedReferenceURIs;
305cdf0e10cSrcweir 	std::vector< int > m_vUnsolvedReferenceKeeperIds;
306cdf0e10cSrcweir 	std::vector< int > m_vUnsolvedReferenceRefNums;
307cdf0e10cSrcweir 
308cdf0e10cSrcweir 	bool m_bIsExporting;
309cdf0e10cSrcweir 	bool m_bIsBlocking;
310cdf0e10cSrcweir 
311cdf0e10cSrcweir 	bool m_bIsInsideCollectedElement;
312cdf0e10cSrcweir 	bool m_bIsSAXEventKeeperOnTheSAXChain;
313cdf0e10cSrcweir 
314cdf0e10cSrcweir 	com::sun::star::uno::Reference<
315cdf0e10cSrcweir 		com::sun::star::xml::crypto::XXMLSecurityContext >
316cdf0e10cSrcweir 		m_xXMLSecurityContext;
317cdf0e10cSrcweir 
318cdf0e10cSrcweir 	com::sun::star::uno::Reference<
319cdf0e10cSrcweir 		com::sun::star::xml::crypto::XXMLSignature >
320cdf0e10cSrcweir 		m_xXMLSignature;
321cdf0e10cSrcweir 
322cdf0e10cSrcweir 	rtl::OUString m_ouJavaCryptokenDir;
323cdf0e10cSrcweir 	rtl::OUString m_ouCCryptokenDir;
324cdf0e10cSrcweir 	rtl::OUString m_ouXMLDocumentWrapperComponentName;
325cdf0e10cSrcweir 
326cdf0e10cSrcweir private:
327cdf0e10cSrcweir 	com::sun::star::uno::Reference<
328cdf0e10cSrcweir 		com::sun::star::io::XOutputStream >
329cdf0e10cSrcweir 		createOutputStream( const rtl::OUString& ouFile );
330cdf0e10cSrcweir 
331cdf0e10cSrcweir 	rtl::OUString parseFile(
332cdf0e10cSrcweir 		const rtl::OUString& ouInputFileName,
333cdf0e10cSrcweir 		const rtl::OUString& ouOutputFileName,
334cdf0e10cSrcweir 		bool bIsExporting,
335cdf0e10cSrcweir 		bool bIsJavaBased);
336cdf0e10cSrcweir 
337cdf0e10cSrcweir 	void changeOutput();
338cdf0e10cSrcweir 
339cdf0e10cSrcweir 	bool foundSecurityRelated();
340cdf0e10cSrcweir 
341cdf0e10cSrcweir 	void findKeyOrReference(SecurityEntity* pSecurityEntity, const rtl::OUString& ouUri, bool bIsFindKey);
342cdf0e10cSrcweir 
343cdf0e10cSrcweir 	bool checkSecurityElement(
344cdf0e10cSrcweir 		const rtl::OUString& ouLocalName,
345cdf0e10cSrcweir 		const com::sun::star::uno::Reference<
346cdf0e10cSrcweir 			com::sun::star::xml::sax::XAttributeList>& xAttribs);
347cdf0e10cSrcweir 
348cdf0e10cSrcweir 	void checkReference(
349cdf0e10cSrcweir 		const rtl::OUString& ouLocalName,
350cdf0e10cSrcweir 		const com::sun::star::uno::Reference<
351cdf0e10cSrcweir 			com::sun::star::xml::sax::XAttributeList>& xAttribs,
352cdf0e10cSrcweir 		const rtl::OUString& ouId);
353cdf0e10cSrcweir 
354cdf0e10cSrcweir 	void endMission();
355cdf0e10cSrcweir 
356cdf0e10cSrcweir 	void addStartAncestorEvent(
357cdf0e10cSrcweir 		const rtl::OUString& ouName,
358cdf0e10cSrcweir 		const com::sun::star::uno::Reference<
359cdf0e10cSrcweir 			com::sun::star::xml::sax::XAttributeList>& xAttribs);
360cdf0e10cSrcweir 
361cdf0e10cSrcweir 	void addEndAncestorEvent( const rtl::OUString& ouName );
362cdf0e10cSrcweir 
363cdf0e10cSrcweir 	void flushAncestorEvents(
364cdf0e10cSrcweir 		const com::sun::star::uno::Reference<
365cdf0e10cSrcweir 			com::sun::star::xml::sax::XDocumentHandler >& xDocumentHandler);
366cdf0e10cSrcweir 
367cdf0e10cSrcweir 	void XSecTester::sendAncestorStartElementEvent(
368cdf0e10cSrcweir 		const rtl::OUString& ouName,
369cdf0e10cSrcweir 		const com::sun::star::uno::Sequence<
370cdf0e10cSrcweir 			com::sun::star::xml::csax::XMLAttribute >& xAttrList,
371cdf0e10cSrcweir 		const com::sun::star::uno::Reference<
372cdf0e10cSrcweir 			com::sun::star::xml::sax::XDocumentHandler >& xDocumentHandler) const;
373cdf0e10cSrcweir 
374cdf0e10cSrcweir 	void XSecTester::sendAncestorEndElementEvent(
375cdf0e10cSrcweir 		const rtl::OUString& ouName,
376cdf0e10cSrcweir 		const com::sun::star::uno::Reference<
377cdf0e10cSrcweir 			com::sun::star::xml::sax::XDocumentHandler >& xDocumentHandler) const;
378cdf0e10cSrcweir 
379cdf0e10cSrcweir 	std::vector< AncestorEvent* >::const_iterator XSecTester::checkAncestorStartElementEvent(
380cdf0e10cSrcweir 		const std::vector< AncestorEvent* >::const_iterator& ii,
381cdf0e10cSrcweir 		const com::sun::star::uno::Reference<
382cdf0e10cSrcweir 			com::sun::star::xml::sax::XDocumentHandler >& xDocumentHandler) const;
383cdf0e10cSrcweir 
384cdf0e10cSrcweir public:
XSecTester(const com::sun::star::uno::Reference<com::sun::star::lang::XMultiServiceFactory> & rxMSF)385cdf0e10cSrcweir 	XSecTester(const com::sun::star::uno::Reference<
386cdf0e10cSrcweir 		com::sun::star::lang::XMultiServiceFactory >& rxMSF)
387cdf0e10cSrcweir 		:mxMSF( rxMSF ){};
~XSecTester()388cdf0e10cSrcweir 	virtual ~XSecTester(){};
389cdf0e10cSrcweir 
390cdf0e10cSrcweir 	/* XSignatureCreationResultListener */
391cdf0e10cSrcweir 	virtual void SAL_CALL signatureCreated(
392cdf0e10cSrcweir 		sal_Int32 securityId,
393cdf0e10cSrcweir 		com::sun::star::xml::crypto::SecurityOperationStatus creationResult )
394cdf0e10cSrcweir 		throw (com::sun::star::uno::RuntimeException);
395cdf0e10cSrcweir 
396cdf0e10cSrcweir 	/* XSignatureVerifyResultListener */
397cdf0e10cSrcweir 	virtual void SAL_CALL signatureVerified(
398cdf0e10cSrcweir 		sal_Int32 securityId,
399cdf0e10cSrcweir 		com::sun::star::xml::crypto::SecurityOperationStatus verifyResult )
400cdf0e10cSrcweir 		throw (com::sun::star::uno::RuntimeException);
401cdf0e10cSrcweir 
402cdf0e10cSrcweir 	/* XSAXEventKeeperStatusChangeListener */
403cdf0e10cSrcweir 	virtual void SAL_CALL blockingStatusChanged( sal_Bool isBlocking )
404cdf0e10cSrcweir 		throw (com::sun::star::uno::RuntimeException);
405cdf0e10cSrcweir 	virtual void SAL_CALL collectionStatusChanged(
406cdf0e10cSrcweir 		sal_Bool isInsideCollectedElement )
407cdf0e10cSrcweir 		throw (com::sun::star::uno::RuntimeException);
408cdf0e10cSrcweir 	virtual void SAL_CALL bufferStatusChanged( sal_Bool isBufferEmpty )
409cdf0e10cSrcweir 		throw (com::sun::star::uno::RuntimeException);
410cdf0e10cSrcweir 
411cdf0e10cSrcweir 	/* XXMLSecTester */
412cdf0e10cSrcweir 	virtual rtl::OUString SAL_CALL transfer_without_sec(
413cdf0e10cSrcweir 		const rtl::OUString& inputFileName,
414cdf0e10cSrcweir 		const rtl::OUString& outputFileName,
415cdf0e10cSrcweir 		sal_Bool isBridgeInvolved)
416cdf0e10cSrcweir 		throw (com::sun::star::uno::RuntimeException);
417cdf0e10cSrcweir 	virtual rtl::OUString SAL_CALL export_xml(
418cdf0e10cSrcweir 		const rtl::OUString& inputFileName,
419cdf0e10cSrcweir 		const rtl::OUString& outputFileName,
420cdf0e10cSrcweir 		sal_Bool isJavaBased)
421cdf0e10cSrcweir 		throw (com::sun::star::uno::RuntimeException);
422cdf0e10cSrcweir 	virtual rtl::OUString SAL_CALL import_xml(
423cdf0e10cSrcweir 		const rtl::OUString& inputFileName,
424cdf0e10cSrcweir 		const rtl::OUString& outputFileName,
425cdf0e10cSrcweir 		sal_Bool isJavaBased)
426cdf0e10cSrcweir 		throw (com::sun::star::uno::RuntimeException);
427cdf0e10cSrcweir 
428cdf0e10cSrcweir 	virtual void SAL_CALL setCryptoDir(
429cdf0e10cSrcweir 		const rtl::OUString & javaDirName,
430cdf0e10cSrcweir 		const rtl::OUString & cDirName)
431cdf0e10cSrcweir 		throw (com::sun::star::uno::RuntimeException);
432cdf0e10cSrcweir 
433cdf0e10cSrcweir 	/* XDocumentHandler */
434cdf0e10cSrcweir 	virtual void SAL_CALL endDocument()
435cdf0e10cSrcweir 		throw (com::sun::star::uno::RuntimeException);
436cdf0e10cSrcweir 	virtual void SAL_CALL startDocument()
437cdf0e10cSrcweir 		throw (com::sun::star::uno::RuntimeException);
438cdf0e10cSrcweir 	virtual void SAL_CALL characters(const class rtl::OUString&)
439cdf0e10cSrcweir 		throw (com::sun::star::uno::RuntimeException);
440cdf0e10cSrcweir 	virtual void SAL_CALL processingInstruction(const rtl::OUString&, const rtl::OUString&)
441cdf0e10cSrcweir 		throw (com::sun::star::uno::RuntimeException);
442cdf0e10cSrcweir 	virtual void SAL_CALL ignorableWhitespace(const rtl::OUString&)
443cdf0e10cSrcweir 		throw (com::sun::star::uno::RuntimeException);
444cdf0e10cSrcweir 	virtual void SAL_CALL startElement(
445cdf0e10cSrcweir 		const rtl::OUString&,
446cdf0e10cSrcweir 		const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList >&)
447cdf0e10cSrcweir 		throw (com::sun::star::uno::RuntimeException);
448cdf0e10cSrcweir 	virtual void SAL_CALL endElement(const rtl::OUString&)
449cdf0e10cSrcweir 		throw (com::sun::star::uno::RuntimeException);
450cdf0e10cSrcweir 	virtual void SAL_CALL setDocumentLocator(
451cdf0e10cSrcweir 		const com::sun::star::uno::Reference< com::sun::star::xml::sax::XLocator >&)
452cdf0e10cSrcweir 		throw (com::sun::star::uno::RuntimeException);
453cdf0e10cSrcweir };
454cdf0e10cSrcweir 
parseFile(const rtl::OUString & ouInputFileName,const rtl::OUString & ouOutputFileName,bool bIsExporting,bool bIsJavaBased)455cdf0e10cSrcweir rtl::OUString XSecTester::parseFile(
456cdf0e10cSrcweir 	const rtl::OUString& ouInputFileName,
457cdf0e10cSrcweir 	const rtl::OUString& ouOutputFileName,
458cdf0e10cSrcweir 	bool bIsExporting,
459cdf0e10cSrcweir 	bool bIsJavaBased)
460cdf0e10cSrcweir {
461cdf0e10cSrcweir 	rtl::OUString ouMessage;
462cdf0e10cSrcweir 
463cdf0e10cSrcweir 	cssu::Reference<cssi::XInputStream> xInputStream = OpenInputStream(ouInputFileName);
464cdf0e10cSrcweir 
465cdf0e10cSrcweir 	if (xInputStream != NULL )
466cdf0e10cSrcweir 	{
467cdf0e10cSrcweir 		/* initialization */
468cdf0e10cSrcweir 		rtl::OUString SEInitializer_comp;
469cdf0e10cSrcweir 		rtl::OUString XMLSignature_comp;
470cdf0e10cSrcweir 		rtl::OUString tokenPath;
471cdf0e10cSrcweir 		cssu::Reference < cssxc::XSEInitializer > xSEInitializer;
472cdf0e10cSrcweir 
473cdf0e10cSrcweir 		if (bIsJavaBased)
474cdf0e10cSrcweir 		{
475cdf0e10cSrcweir 			SEInitializer_comp = rtl::OUString::createFromAscii( SEINITIALIZER_JAVA_COMPONENT );
476cdf0e10cSrcweir 			XMLSignature_comp = rtl::OUString::createFromAscii( XMLSIGNATURE_JAVA_COMPONENT);
477cdf0e10cSrcweir 			m_ouXMLDocumentWrapperComponentName = rtl::OUString::createFromAscii( XMLDOCUMENTWRAPPER_JAVA_COMPONENT );
478cdf0e10cSrcweir 			tokenPath = m_ouJavaCryptokenDir;
479cdf0e10cSrcweir 		}
480cdf0e10cSrcweir 		else
481cdf0e10cSrcweir 		{
482cdf0e10cSrcweir 			SEInitializer_comp = rtl::OUString::createFromAscii( SEINITIALIZER_C_COMPONENT );
483cdf0e10cSrcweir 			XMLSignature_comp = rtl::OUString::createFromAscii( XMLSIGNATURE_C_COMPONENT);
484cdf0e10cSrcweir 			m_ouXMLDocumentWrapperComponentName = rtl::OUString::createFromAscii( XMLDOCUMENT_C_COMPONENT );
485cdf0e10cSrcweir 			tokenPath = m_ouCCryptokenDir;
486cdf0e10cSrcweir 		}
487cdf0e10cSrcweir 
488cdf0e10cSrcweir 		xSEInitializer = cssu::Reference < cssxc::XSEInitializer > (
489cdf0e10cSrcweir 			 mxMSF->createInstance( SEInitializer_comp ),
490cdf0e10cSrcweir 			 cssu::UNO_QUERY );
491cdf0e10cSrcweir 
492cdf0e10cSrcweir 		m_xXMLSignature = cssu::Reference<cssxc::XXMLSignature> (
493cdf0e10cSrcweir 			mxMSF->createInstance( XMLSignature_comp ),
494cdf0e10cSrcweir 			cssu::UNO_QUERY );
495cdf0e10cSrcweir 
496cdf0e10cSrcweir 		if ( xSEInitializer.is() && m_xXMLSignature.is())
497cdf0e10cSrcweir 		{
498cdf0e10cSrcweir 			/* create SAX Parser */
499cdf0e10cSrcweir 			const rtl::OUString sSaxParser (
500cdf0e10cSrcweir 				RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser") );
501cdf0e10cSrcweir 			m_xSaxParser = cssu::Reference < cssxs::XParser > ( mxMSF->createInstance( sSaxParser ), cssu::UNO_QUERY );
502cdf0e10cSrcweir 
503cdf0e10cSrcweir 			/* create SAX Writer */
504cdf0e10cSrcweir 			const rtl::OUString sSaxWriter (
505cdf0e10cSrcweir 				RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer") );
506cdf0e10cSrcweir 			cssu::Reference < cssi::XActiveDataSource > xSaxWriter
507cdf0e10cSrcweir 				( mxMSF->createInstance( sSaxWriter ), cssu::UNO_QUERY );
508cdf0e10cSrcweir 
509cdf0e10cSrcweir 			cssu::Reference< cssi::XOutputStream > xOutputStream = OpenOutputStream(ouOutputFileName);
510cdf0e10cSrcweir 			xSaxWriter->setOutputStream( xOutputStream );
511cdf0e10cSrcweir 
512cdf0e10cSrcweir 			cssxs::InputSource aInput;
513cdf0e10cSrcweir 			aInput.sSystemId = ouInputFileName;
514cdf0e10cSrcweir 			aInput.aInputStream = xInputStream;
515cdf0e10cSrcweir 
516cdf0e10cSrcweir 			cssu::Reference < cssxs::XDocumentHandler > xSaxWriterHandler( xSaxWriter, cssu::UNO_QUERY);
517cdf0e10cSrcweir 
518cdf0e10cSrcweir 			m_xXMLSecurityContext =
519cdf0e10cSrcweir 				xSEInitializer->createSecurityContext(tokenPath);
520cdf0e10cSrcweir 
521cdf0e10cSrcweir 			m_bIsExporting = bIsExporting;
522cdf0e10cSrcweir 			m_xExportHandler = xSaxWriterHandler;
523cdf0e10cSrcweir 			m_xOutputHandler = xSaxWriterHandler;
524cdf0e10cSrcweir 
525cdf0e10cSrcweir 			m_xXMLDocumentWrapper = NULL;
526cdf0e10cSrcweir 			m_xSAXEventKeeper = NULL;
527cdf0e10cSrcweir 			m_bIsSAXEventKeeperOnTheSAXChain = false;
528cdf0e10cSrcweir 
529cdf0e10cSrcweir 			m_bIsBlocking = false;
530cdf0e10cSrcweir 			m_bIsInsideCollectedElement = false;
531cdf0e10cSrcweir 
532cdf0e10cSrcweir 			OSL_ASSERT(m_vSignatureList.size() == 0);
533cdf0e10cSrcweir 			OSL_ASSERT(m_vUnsolvedReferenceURIs.size() == 0);
534cdf0e10cSrcweir 			OSL_ASSERT(m_vUnsolvedReferenceKeeperIds.size() == 0);
535cdf0e10cSrcweir 			OSL_ASSERT(m_vUnsolvedReferenceRefNums.size() == 0);
536cdf0e10cSrcweir 			OSL_ASSERT(m_stCurrentPath.empty());
537cdf0e10cSrcweir 			OSL_ASSERT(m_stCurrentPathType.empty());
538cdf0e10cSrcweir 			OSL_ASSERT(m_vAncestorEvents.empty());
539cdf0e10cSrcweir 
540cdf0e10cSrcweir 			changeOutput();
541cdf0e10cSrcweir 
542cdf0e10cSrcweir 			/* foundSecurityRelated(); */
543cdf0e10cSrcweir 
544cdf0e10cSrcweir 			/* Begin to parse */
545cdf0e10cSrcweir 			TimeValue startTime, endTime;
546cdf0e10cSrcweir 			osl_getSystemTime( &startTime );
547cdf0e10cSrcweir 
548cdf0e10cSrcweir 			xSaxWriterHandler->startDocument();
549cdf0e10cSrcweir 
550cdf0e10cSrcweir 			if (m_bIsExporting)
551cdf0e10cSrcweir 			{
552cdf0e10cSrcweir 				m_xSaxParser->setDocumentHandler(this);
553cdf0e10cSrcweir 				m_xSaxParser->parseStream(aInput);
554cdf0e10cSrcweir 			}
555cdf0e10cSrcweir 			else
556cdf0e10cSrcweir 			{
557cdf0e10cSrcweir 				m_xSaxParser->setDocumentHandler(this);
558cdf0e10cSrcweir 				m_xSaxParser->parseStream(aInput);
559cdf0e10cSrcweir 			}
560cdf0e10cSrcweir 
561cdf0e10cSrcweir 			endMission();
562cdf0e10cSrcweir 			xSaxWriterHandler->endDocument();
563cdf0e10cSrcweir 
564cdf0e10cSrcweir 			osl_getSystemTime( &endTime );
565cdf0e10cSrcweir 
566cdf0e10cSrcweir 			flushAncestorEvents( NULL );
567cdf0e10cSrcweir 
568cdf0e10cSrcweir 			// Bug in SAXWriter, done in endDocument()
569cdf0e10cSrcweir 			// xOutputStream->closeOutput();
570cdf0e10cSrcweir 			xInputStream->closeInput();
571cdf0e10cSrcweir 
572cdf0e10cSrcweir 
573cdf0e10cSrcweir 			/*
574cdf0e10cSrcweir 			 * Free the security context
575cdf0e10cSrcweir 			 */
576cdf0e10cSrcweir 			xSEInitializer->freeSecurityContext(m_xXMLSecurityContext);
577cdf0e10cSrcweir 			m_xXMLSecurityContext = NULL;
578cdf0e10cSrcweir 
579cdf0e10cSrcweir 			/* Calculate the time */
580cdf0e10cSrcweir 			double diff = ((double)((endTime.Nanosec + endTime.Seconds*1000000000.0)
581cdf0e10cSrcweir 					- (startTime.Nanosec + startTime.Seconds*1000000000.0))) /
582cdf0e10cSrcweir 				((double)1000000000.0);
583cdf0e10cSrcweir 
584cdf0e10cSrcweir 			char buf[32];
585cdf0e10cSrcweir 			sprintf(buf, "%.2f", diff);
586cdf0e10cSrcweir 			ouMessage += rtl::OUString(RTL_ASCII_USTRINGPARAM(buf));
587cdf0e10cSrcweir 		}
588cdf0e10cSrcweir 		else
589cdf0e10cSrcweir 		{
590cdf0e10cSrcweir 			ouMessage += rtl::OUString::createFromAscii( "N/A" );
591cdf0e10cSrcweir 		}
592cdf0e10cSrcweir 
593cdf0e10cSrcweir 	}
594cdf0e10cSrcweir 	else
595cdf0e10cSrcweir 	{
596cdf0e10cSrcweir 		ouMessage += rtl::OUString::createFromAscii( "-" );
597cdf0e10cSrcweir 	}
598cdf0e10cSrcweir 
599cdf0e10cSrcweir 	return ouMessage;
600cdf0e10cSrcweir }
601cdf0e10cSrcweir 
602cdf0e10cSrcweir /* XSignatureCreationResultListener */
signatureCreated(sal_Int32 securityId,cssxc::SecurityOperationStatus creationResult)603cdf0e10cSrcweir void SAL_CALL XSecTester::signatureCreated(
604cdf0e10cSrcweir 	sal_Int32 securityId,
605cdf0e10cSrcweir 	cssxc::SecurityOperationStatus creationResult )
606cdf0e10cSrcweir 	throw (cssu::RuntimeException)
607cdf0e10cSrcweir {
608cdf0e10cSrcweir 	m_nTotalSignatureNumber++;
609cdf0e10cSrcweir 	if (creationResult == cssxc::SecurityOperationStatus_OPERATION_SUCCEEDED)
610cdf0e10cSrcweir 	{
611cdf0e10cSrcweir 		m_nSuccessfulSignatureNumber++;
612cdf0e10cSrcweir 	}
613cdf0e10cSrcweir }
614cdf0e10cSrcweir 
615cdf0e10cSrcweir /* XSignatureVerifyResultListener */
signatureVerified(sal_Int32 securityId,cssxc::SecurityOperationStatus verifyResult)616cdf0e10cSrcweir void SAL_CALL XSecTester::signatureVerified(
617cdf0e10cSrcweir 	sal_Int32 securityId,
618cdf0e10cSrcweir 	cssxc::SecurityOperationStatus verifyResult )
619cdf0e10cSrcweir 	throw (cssu::RuntimeException)
620cdf0e10cSrcweir {
621cdf0e10cSrcweir 	m_nTotalSignatureNumber++;
622cdf0e10cSrcweir 	if (verifyResult == cssxc::SecurityOperationStatus_OPERATION_SUCCEEDED)
623cdf0e10cSrcweir 	{
624cdf0e10cSrcweir 		m_nSuccessfulSignatureNumber++;
625cdf0e10cSrcweir 	}
626cdf0e10cSrcweir }
627cdf0e10cSrcweir 
628cdf0e10cSrcweir /* XSAXEventKeeperStatusChangeListener */
blockingStatusChanged(sal_Bool isBlocking)629cdf0e10cSrcweir void SAL_CALL XSecTester::blockingStatusChanged( sal_Bool isBlocking )
630cdf0e10cSrcweir 	throw (cssu::RuntimeException)
631cdf0e10cSrcweir {
632cdf0e10cSrcweir 	this->m_bIsBlocking = isBlocking;
633cdf0e10cSrcweir }
634cdf0e10cSrcweir 
collectionStatusChanged(sal_Bool isInsideCollectedElement)635cdf0e10cSrcweir void SAL_CALL XSecTester::collectionStatusChanged( sal_Bool isInsideCollectedElement )
636cdf0e10cSrcweir 	throw (cssu::RuntimeException)
637cdf0e10cSrcweir {
638cdf0e10cSrcweir 	this->m_bIsInsideCollectedElement = isInsideCollectedElement;
639cdf0e10cSrcweir 
640cdf0e10cSrcweir 	if ( !m_bIsInsideCollectedElement && !m_bIsBlocking)
641cdf0e10cSrcweir 	{
642cdf0e10cSrcweir 		m_bIsSAXEventKeeperOnTheSAXChain = false;
643cdf0e10cSrcweir 	}
644cdf0e10cSrcweir 	else
645cdf0e10cSrcweir 	{
646cdf0e10cSrcweir 		m_bIsSAXEventKeeperOnTheSAXChain = true;
647cdf0e10cSrcweir 	}
648cdf0e10cSrcweir 	changeOutput();
649cdf0e10cSrcweir }
650cdf0e10cSrcweir 
bufferStatusChanged(sal_Bool isBufferEmpty)651cdf0e10cSrcweir void SAL_CALL XSecTester::bufferStatusChanged( sal_Bool isBufferEmpty )
652cdf0e10cSrcweir 	throw (cssu::RuntimeException)
653cdf0e10cSrcweir {
654cdf0e10cSrcweir 	if (isBufferEmpty)
655cdf0e10cSrcweir 	{
656cdf0e10cSrcweir 		m_xXMLDocumentWrapper = NULL;
657cdf0e10cSrcweir 
658cdf0e10cSrcweir 		m_xSAXEventKeeper = NULL;
659cdf0e10cSrcweir 		m_bIsSAXEventKeeperOnTheSAXChain = false;
660cdf0e10cSrcweir 		changeOutput();
661cdf0e10cSrcweir 	}
662cdf0e10cSrcweir }
663cdf0e10cSrcweir 
664cdf0e10cSrcweir /* XXMLSecTester */
export_xml(const rtl::OUString & inputFileName,const rtl::OUString & outputFileName,sal_Bool isJavaBased)665cdf0e10cSrcweir rtl::OUString SAL_CALL XSecTester::export_xml( const rtl::OUString& inputFileName, const rtl::OUString& outputFileName, sal_Bool isJavaBased)
666cdf0e10cSrcweir 	throw (cssu::RuntimeException)
667cdf0e10cSrcweir {
668cdf0e10cSrcweir 	rtl::OUString ouMessage;
669cdf0e10cSrcweir 
670cdf0e10cSrcweir 	m_nTotalSignatureNumber = 0;
671cdf0e10cSrcweir 	m_nSuccessfulSignatureNumber = 0;
672cdf0e10cSrcweir 
673cdf0e10cSrcweir 	ouMessage += parseFile(inputFileName, outputFileName, sal_True, isJavaBased);
674cdf0e10cSrcweir 
675cdf0e10cSrcweir 	rtl::OUString ouRemark = rtl::OUString::valueOf(m_nSuccessfulSignatureNumber) +
676cdf0e10cSrcweir 		rtl::OUString(RTL_ASCII_USTRINGPARAM( "/" ))
677cdf0e10cSrcweir 		+ rtl::OUString::valueOf(m_nTotalSignatureNumber);
678cdf0e10cSrcweir 	ouMessage += rtl::OUString(RTL_ASCII_USTRINGPARAM("\t")) + ouRemark;
679cdf0e10cSrcweir 
680cdf0e10cSrcweir 	return ouMessage;
681cdf0e10cSrcweir }
682cdf0e10cSrcweir 
import_xml(const rtl::OUString & inputFileName,const rtl::OUString & outputFileName,sal_Bool isJavaBased)683cdf0e10cSrcweir rtl::OUString SAL_CALL XSecTester::import_xml( const rtl::OUString& inputFileName, const rtl::OUString& outputFileName, sal_Bool isJavaBased)
684cdf0e10cSrcweir 	throw (cssu::RuntimeException)
685cdf0e10cSrcweir {
686cdf0e10cSrcweir 	rtl::OUString ouMessage;
687cdf0e10cSrcweir 
688cdf0e10cSrcweir 	m_nTotalSignatureNumber = 0;
689cdf0e10cSrcweir 	m_nSuccessfulSignatureNumber = 0;
690cdf0e10cSrcweir 
691cdf0e10cSrcweir 	ouMessage += parseFile(inputFileName, outputFileName, sal_False, isJavaBased);
692cdf0e10cSrcweir 
693cdf0e10cSrcweir 	rtl::OUString ouRemark = rtl::OUString::valueOf(m_nSuccessfulSignatureNumber) +
694cdf0e10cSrcweir 		rtl::OUString(RTL_ASCII_USTRINGPARAM( "/" ))
695cdf0e10cSrcweir 		+ rtl::OUString::valueOf(m_nTotalSignatureNumber);
696cdf0e10cSrcweir 	ouMessage += rtl::OUString(RTL_ASCII_USTRINGPARAM("\t")) + ouRemark;
697cdf0e10cSrcweir 
698cdf0e10cSrcweir 	return ouMessage;
699cdf0e10cSrcweir }
700cdf0e10cSrcweir 
transfer_without_sec(const rtl::OUString & inputFileName,const rtl::OUString & outputFileName,sal_Bool isBridgeInvolved)701cdf0e10cSrcweir rtl::OUString SAL_CALL XSecTester::transfer_without_sec(
702cdf0e10cSrcweir 	const rtl::OUString& inputFileName,
703cdf0e10cSrcweir 	const rtl::OUString& outputFileName,
704cdf0e10cSrcweir 	sal_Bool isBridgeInvolved)
705cdf0e10cSrcweir 	throw (cssu::RuntimeException)
706cdf0e10cSrcweir {
707cdf0e10cSrcweir 	rtl::OUString ouMessage;
708cdf0e10cSrcweir 
709cdf0e10cSrcweir 	cssu::Reference< cssi::XInputStream > xInputStream = OpenInputStream(inputFileName);
710cdf0e10cSrcweir 
711cdf0e10cSrcweir 	if (xInputStream != NULL )
712cdf0e10cSrcweir 	{
713cdf0e10cSrcweir 		/* create SAX Parser */
714cdf0e10cSrcweir 		const rtl::OUString sSaxParser (
715cdf0e10cSrcweir 			RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser") );
716cdf0e10cSrcweir 		m_xSaxParser = cssu::Reference < cssxs::XParser > ( mxMSF->createInstance( sSaxParser ), cssu::UNO_QUERY );
717cdf0e10cSrcweir 
718cdf0e10cSrcweir 		/* create SAX Writer */
719cdf0e10cSrcweir 		const rtl::OUString sSaxWriter (
720cdf0e10cSrcweir 			RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer") );
721cdf0e10cSrcweir 		cssu::Reference < cssi::XActiveDataSource > xSaxWriter
722cdf0e10cSrcweir 			( mxMSF->createInstance( sSaxWriter ), cssu::UNO_QUERY );
723cdf0e10cSrcweir 		cssu::Reference < cssxs::XDocumentHandler > xSaxWriterHandler(
724cdf0e10cSrcweir 			xSaxWriter, cssu::UNO_QUERY);
725cdf0e10cSrcweir 
726cdf0e10cSrcweir 		if (!isBridgeInvolved)
727cdf0e10cSrcweir 		{
728cdf0e10cSrcweir 			/* connect the SAX Parser and the SAX Writer */
729cdf0e10cSrcweir 			m_xSaxParser->setDocumentHandler ( xSaxWriterHandler );
730cdf0e10cSrcweir 		}
731cdf0e10cSrcweir 		else
732cdf0e10cSrcweir 		{
733cdf0e10cSrcweir 			/* create Java Flat Filter */
734cdf0e10cSrcweir 			const rtl::OUString sJavaFlatFilter(
735cdf0e10cSrcweir 				RTL_CONSTASCII_USTRINGPARAM( JAVAFLATFILTER_COMPONENT ) );
736cdf0e10cSrcweir 			cssu::Reference < cssxs::XParser > xJavaFilterParser
737cdf0e10cSrcweir 				( mxMSF->createInstance( sJavaFlatFilter ), cssu::UNO_QUERY );
738cdf0e10cSrcweir 			cssu::Reference < cssxs::XDocumentHandler > xJavaFilterHandler(
739cdf0e10cSrcweir 				xJavaFilterParser, cssu::UNO_QUERY );
740cdf0e10cSrcweir 
741cdf0e10cSrcweir 			if ( !xJavaFilterParser.is() )
742cdf0e10cSrcweir 				return rtl::OUString::createFromAscii( "NO JAVA" );
743cdf0e10cSrcweir 
744cdf0e10cSrcweir 			/* connect the SAX Parser, the Java Flat Filter and the SAX Writer */
745cdf0e10cSrcweir 			xJavaFilterParser->setDocumentHandler( xSaxWriterHandler );
746cdf0e10cSrcweir 			m_xSaxParser->setDocumentHandler ( xJavaFilterHandler );
747cdf0e10cSrcweir 		}
748cdf0e10cSrcweir 
749cdf0e10cSrcweir 
750cdf0e10cSrcweir 		/* set output stream */
751cdf0e10cSrcweir 		cssu::Reference< cssi::XOutputStream > xOutputStream =
752cdf0e10cSrcweir 				OpenOutputStream(outputFileName);
753cdf0e10cSrcweir 		xSaxWriter->setOutputStream( xOutputStream );
754cdf0e10cSrcweir 
755cdf0e10cSrcweir 		/* prepare input stream */
756cdf0e10cSrcweir 		cssxs::InputSource aInput;
757cdf0e10cSrcweir 		aInput.sSystemId = inputFileName;
758cdf0e10cSrcweir 		aInput.aInputStream = xInputStream;
759cdf0e10cSrcweir 
760cdf0e10cSrcweir 		TimeValue startTime, endTime;
761cdf0e10cSrcweir 		osl_getSystemTime( &startTime );
762cdf0e10cSrcweir 
763cdf0e10cSrcweir 		m_xSaxParser->parseStream ( aInput );
764cdf0e10cSrcweir 
765cdf0e10cSrcweir 		// xOutputStream->closeOutput();
766cdf0e10cSrcweir 		xInputStream->closeInput();
767cdf0e10cSrcweir 
768cdf0e10cSrcweir 		osl_getSystemTime( &endTime );
769cdf0e10cSrcweir 
770cdf0e10cSrcweir 		double diff = ((double)((endTime.Nanosec + endTime.Seconds*1000000000.0)
771cdf0e10cSrcweir 				 - (startTime.Nanosec + startTime.Seconds*1000000000.0)))/((double)1000000000.0);
772cdf0e10cSrcweir 		char buf[32];
773cdf0e10cSrcweir 		sprintf(buf, "%.2f", diff);
774cdf0e10cSrcweir 		ouMessage += rtl::OUString(RTL_ASCII_USTRINGPARAM(buf));
775cdf0e10cSrcweir 	}
776cdf0e10cSrcweir 
777cdf0e10cSrcweir 	return ouMessage;
778cdf0e10cSrcweir }
779cdf0e10cSrcweir 
setCryptoDir(const rtl::OUString & javaDirName,const rtl::OUString & cDirName)780cdf0e10cSrcweir void SAL_CALL XSecTester::setCryptoDir(const rtl::OUString & javaDirName, const rtl::OUString & cDirName)
781cdf0e10cSrcweir 	throw (cssu::RuntimeException)
782cdf0e10cSrcweir {
783cdf0e10cSrcweir 	m_ouJavaCryptokenDir = javaDirName;
784cdf0e10cSrcweir 	m_ouCCryptokenDir = cDirName;
785cdf0e10cSrcweir }
786cdf0e10cSrcweir 
787cdf0e10cSrcweir 
XSecTester_createInstance(const cssu::Reference<cssl::XMultiServiceFactory> & rSMgr)788cdf0e10cSrcweir cssu::Reference< cssu::XInterface > SAL_CALL XSecTester_createInstance(
789cdf0e10cSrcweir 	const cssu::Reference< cssl::XMultiServiceFactory > & rSMgr)
790cdf0e10cSrcweir 	throw( cssu::Exception )
791cdf0e10cSrcweir {
792cdf0e10cSrcweir 	return (cppu::OWeakObject*) new XSecTester( rSMgr );
793cdf0e10cSrcweir }
794cdf0e10cSrcweir 
795cdf0e10cSrcweir int SecurityEntity::m_nNextSecurityId = 1;
796cdf0e10cSrcweir 
SecurityEntity(const cssu::Reference<cssxc::sax::XSecuritySAXEventKeeper> & xSAXEventKeeper,const cssu::Reference<cssxc::XXMLSecurityContext> & xXMLSecurityContext,const cssu::Reference<cssxc::XXMLSignature> & xXMLSignature,const cssu::Reference<cssl::XMultiServiceFactory> & rsMSF)797cdf0e10cSrcweir SecurityEntity::SecurityEntity(
798cdf0e10cSrcweir 	const cssu::Reference<cssxc::sax::XSecuritySAXEventKeeper>& xSAXEventKeeper,
799cdf0e10cSrcweir 	const cssu::Reference<cssxc::XXMLSecurityContext>& xXMLSecurityContext,
800cdf0e10cSrcweir 	const cssu::Reference<cssxc::XXMLSignature>& xXMLSignature,
801cdf0e10cSrcweir 	const cssu::Reference< cssl::XMultiServiceFactory > &rsMSF)
802cdf0e10cSrcweir 	:m_xSAXEventKeeper(xSAXEventKeeper),
803cdf0e10cSrcweir 	 m_xXMLSecurityContext(xXMLSecurityContext),
804cdf0e10cSrcweir 	 m_xXMLSignature(xXMLSignature),
805cdf0e10cSrcweir 	 mxMSF(rsMSF),
806cdf0e10cSrcweir 	 m_ouKeyURI(RTL_ASCII_USTRINGPARAM(""))
807cdf0e10cSrcweir {
808cdf0e10cSrcweir 	m_nSecurityId = getNextSecurityId();
809cdf0e10cSrcweir }
810cdf0e10cSrcweir 
getNextSecurityId() const811cdf0e10cSrcweir int SecurityEntity::getNextSecurityId() const
812cdf0e10cSrcweir {
813cdf0e10cSrcweir 	int nId = m_nNextSecurityId++;
814cdf0e10cSrcweir 	return nId;
815cdf0e10cSrcweir }
816cdf0e10cSrcweir 
setKeyId(int nId)817cdf0e10cSrcweir void SecurityEntity::setKeyId(int nId)
818cdf0e10cSrcweir {
819cdf0e10cSrcweir 	cssu::Reference<cssxc::sax::XKeyCollector> keyCollector (m_xReferenceListener, cssu::UNO_QUERY);
820cdf0e10cSrcweir 	keyCollector->setKeyId(nId);
821cdf0e10cSrcweir }
822cdf0e10cSrcweir 
823cdf0e10cSrcweir 
setKeyURI(const rtl::OUString & ouUri)824cdf0e10cSrcweir void SecurityEntity::setKeyURI(const rtl::OUString& ouUri)
825cdf0e10cSrcweir {
826cdf0e10cSrcweir 	m_ouKeyURI = ouUri;
827cdf0e10cSrcweir }
828cdf0e10cSrcweir 
getReferenceListener() const829cdf0e10cSrcweir cssu::Reference<cssxc::sax::XReferenceResolvedListener> SecurityEntity::getReferenceListener() const
830cdf0e10cSrcweir {
831cdf0e10cSrcweir 	return m_xReferenceListener;
832cdf0e10cSrcweir }
833cdf0e10cSrcweir 
getSecurityId() const834cdf0e10cSrcweir int SecurityEntity::getSecurityId() const
835cdf0e10cSrcweir {
836cdf0e10cSrcweir 	return m_nSecurityId;
837cdf0e10cSrcweir }
838cdf0e10cSrcweir 
setKey(const rtl::OUString & ouUri,bool bIsExporting)839cdf0e10cSrcweir bool SecurityEntity::setKey(const rtl::OUString& ouUri, bool bIsExporting)
840cdf0e10cSrcweir {
841cdf0e10cSrcweir 	bool rc = false;
842cdf0e10cSrcweir 
843cdf0e10cSrcweir  	if (m_ouKeyURI != rtl::OUString(RTL_ASCII_USTRINGPARAM("")) &&
844cdf0e10cSrcweir  		m_ouKeyURI == ouUri)
845cdf0e10cSrcweir 	{
846cdf0e10cSrcweir 		int nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
847cdf0e10cSrcweir 			bIsExporting ?
848cdf0e10cSrcweir 			(cssxc::sax::ElementMarkPriority_BEFOREMODIFY):
849cdf0e10cSrcweir 			(cssxc::sax::ElementMarkPriority_AFTERMODIFY),
850cdf0e10cSrcweir 			true);
851cdf0e10cSrcweir 
852cdf0e10cSrcweir 		setKeyId(nKeeperId);
853cdf0e10cSrcweir 		m_xSAXEventKeeper->setSecurityId(nKeeperId, m_nSecurityId);
854cdf0e10cSrcweir 
855cdf0e10cSrcweir 		cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
856cdf0e10cSrcweir 			(m_xSAXEventKeeper, cssu::UNO_QUERY);
857cdf0e10cSrcweir 		xReferenceResolvedBroadcaster->addReferenceResolvedListener(nKeeperId,
858cdf0e10cSrcweir 			m_xReferenceListener);
859cdf0e10cSrcweir 
860cdf0e10cSrcweir 		rc = true;
861cdf0e10cSrcweir 	}
862cdf0e10cSrcweir 
863cdf0e10cSrcweir 	return rc;
864cdf0e10cSrcweir }
865cdf0e10cSrcweir 
endMission()866cdf0e10cSrcweir bool SecurityEntity::endMission()
867cdf0e10cSrcweir {
868cdf0e10cSrcweir 	cssu::Reference<cssxc::sax::XMissionTaker> xMissionTaker
869cdf0e10cSrcweir 		(m_xReferenceListener, cssu::UNO_QUERY);
870cdf0e10cSrcweir 
871cdf0e10cSrcweir 	return xMissionTaker->endMission();
872cdf0e10cSrcweir }
873cdf0e10cSrcweir 
SignatureEntity(const cssu::Reference<cssxc::sax::XSecuritySAXEventKeeper> & xSAXEventKeeper,bool bIsExporting,XSecTester * pListener,const cssu::Reference<cssxc::XXMLSecurityContext> & xXMLSecurityContext,const cssu::Reference<cssxc::XXMLSignature> & xXMLSignature,const cssu::Reference<cssl::XMultiServiceFactory> & rsMSF)874cdf0e10cSrcweir SignatureEntity::SignatureEntity(
875cdf0e10cSrcweir 	const cssu::Reference<cssxc::sax::XSecuritySAXEventKeeper>& xSAXEventKeeper,
876cdf0e10cSrcweir 	bool bIsExporting,
877cdf0e10cSrcweir 	XSecTester* pListener,
878cdf0e10cSrcweir 	const cssu::Reference<cssxc::XXMLSecurityContext>& xXMLSecurityContext,
879cdf0e10cSrcweir 	const cssu::Reference<cssxc::XXMLSignature>& xXMLSignature,
880cdf0e10cSrcweir 	const cssu::Reference< cssl::XMultiServiceFactory >& rsMSF)
881cdf0e10cSrcweir 	:SecurityEntity(xSAXEventKeeper,
882cdf0e10cSrcweir 			xXMLSecurityContext,
883cdf0e10cSrcweir 			xXMLSignature,
884cdf0e10cSrcweir 			rsMSF)
885cdf0e10cSrcweir {
886cdf0e10cSrcweir 	if (bIsExporting)
887cdf0e10cSrcweir 	{
888cdf0e10cSrcweir 		m_nSignatureElementCollectorId =
889cdf0e10cSrcweir 			m_xSAXEventKeeper->addSecurityElementCollector(
890cdf0e10cSrcweir 				cssxc::sax::ElementMarkPriority_AFTERMODIFY,
891cdf0e10cSrcweir 				true);
892cdf0e10cSrcweir 
893cdf0e10cSrcweir 		m_xSAXEventKeeper->setSecurityId(m_nSignatureElementCollectorId, m_nSecurityId);
894cdf0e10cSrcweir 
895cdf0e10cSrcweir 		m_xReferenceListener = cssu::Reference< cssxc::sax::XReferenceResolvedListener >(
896cdf0e10cSrcweir 			mxMSF->createInstance( rtl::OUString::createFromAscii( SIGNATURECREATOR_COMPONENT )),
897cdf0e10cSrcweir 			cssu::UNO_QUERY);
898cdf0e10cSrcweir 
899cdf0e10cSrcweir 		cssu::Reference<cssl::XInitialization> xInitialization(m_xReferenceListener, cssu::UNO_QUERY);
900cdf0e10cSrcweir 
901cdf0e10cSrcweir 		cssu::Sequence<cssu::Any> args(5);
902cdf0e10cSrcweir 		char buf[16];
903cdf0e10cSrcweir 
904cdf0e10cSrcweir 		sprintf(buf, "%d", m_nSecurityId);
905cdf0e10cSrcweir 		args[0] = cssu::makeAny(rtl::OUString(RTL_ASCII_USTRINGPARAM(buf)));
906cdf0e10cSrcweir 		args[1] = cssu::makeAny(m_xSAXEventKeeper);
907cdf0e10cSrcweir 
908cdf0e10cSrcweir 		sprintf(buf, "%d", m_nSignatureElementCollectorId);
909cdf0e10cSrcweir 		args[2] = cssu::makeAny(rtl::OUString(RTL_ASCII_USTRINGPARAM(buf)));
910cdf0e10cSrcweir 		args[3] = cssu::makeAny(m_xXMLSecurityContext->getSecurityEnvironment());
911cdf0e10cSrcweir 		args[4] = cssu::makeAny(m_xXMLSignature);
912cdf0e10cSrcweir 
913cdf0e10cSrcweir 		xInitialization->initialize(args);
914cdf0e10cSrcweir 
915cdf0e10cSrcweir 		int nBlockerId = m_xSAXEventKeeper->addBlocker();
916cdf0e10cSrcweir 		m_xSAXEventKeeper->setSecurityId(nBlockerId, m_nSecurityId);
917cdf0e10cSrcweir 
918cdf0e10cSrcweir 		cssu::Reference<cssxc::sax::XBlockerMonitor> xBlockerMonitor(m_xReferenceListener, cssu::UNO_QUERY);
919cdf0e10cSrcweir 		xBlockerMonitor->setBlockerId(nBlockerId);
920cdf0e10cSrcweir 
921cdf0e10cSrcweir 		cssu::Reference< cssxc::sax::XSignatureCreationResultBroadcaster > xSignatureCreationResultBroadcaster
922cdf0e10cSrcweir 			(m_xReferenceListener, cssu::UNO_QUERY);
923cdf0e10cSrcweir 		xSignatureCreationResultBroadcaster->addSignatureCreationResultListener(pListener);
924cdf0e10cSrcweir 	}
925cdf0e10cSrcweir 	else
926cdf0e10cSrcweir 	{
927cdf0e10cSrcweir 		m_nSignatureElementCollectorId =
928cdf0e10cSrcweir 			m_xSAXEventKeeper->addSecurityElementCollector(
929cdf0e10cSrcweir 				cssxc::sax::ElementMarkPriority_BEFOREMODIFY,
930cdf0e10cSrcweir 				false);
931cdf0e10cSrcweir 
932cdf0e10cSrcweir 		m_xSAXEventKeeper->setSecurityId(m_nSignatureElementCollectorId, m_nSecurityId);
933cdf0e10cSrcweir 
934cdf0e10cSrcweir 		m_xReferenceListener = cssu::Reference< cssxc::sax::XReferenceResolvedListener >(
935cdf0e10cSrcweir 			mxMSF->createInstance( rtl::OUString::createFromAscii( SIGNATUREVERIFIER_COMPONENT )),
936cdf0e10cSrcweir 			cssu::UNO_QUERY);
937cdf0e10cSrcweir 
938cdf0e10cSrcweir 		cssu::Reference<cssl::XInitialization> xInitialization(m_xReferenceListener, cssu::UNO_QUERY);
939cdf0e10cSrcweir 
940cdf0e10cSrcweir 		cssu::Sequence<cssu::Any> args(5);
941cdf0e10cSrcweir 		char buf[16];
942cdf0e10cSrcweir 
943cdf0e10cSrcweir 		sprintf(buf, "%d", m_nSecurityId);
944cdf0e10cSrcweir 		args[0] = cssu::makeAny(rtl::OUString(RTL_ASCII_USTRINGPARAM(buf)));
945cdf0e10cSrcweir 		args[1] = cssu::makeAny(m_xSAXEventKeeper);
946cdf0e10cSrcweir 
947cdf0e10cSrcweir 		sprintf(buf, "%d", m_nSignatureElementCollectorId);
948cdf0e10cSrcweir 		args[2] = cssu::makeAny(rtl::OUString(RTL_ASCII_USTRINGPARAM(buf)));
949cdf0e10cSrcweir 		args[3] = cssu::makeAny(m_xXMLSecurityContext);
950cdf0e10cSrcweir 		args[4] = cssu::makeAny(m_xXMLSignature);
951cdf0e10cSrcweir 		xInitialization->initialize(args);
952cdf0e10cSrcweir 
953cdf0e10cSrcweir 		cssu::Reference< cssxc::sax::XSignatureVerifyResultBroadcaster > xSignatureVerifyResultBroadcaster
954cdf0e10cSrcweir 			(m_xReferenceListener, cssu::UNO_QUERY);
955cdf0e10cSrcweir 		xSignatureVerifyResultBroadcaster->addSignatureVerifyResultListener(pListener);
956cdf0e10cSrcweir 	}
957cdf0e10cSrcweir 
958cdf0e10cSrcweir 	cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
959cdf0e10cSrcweir 		(m_xSAXEventKeeper, cssu::UNO_QUERY);
960cdf0e10cSrcweir 	xReferenceResolvedBroadcaster->addReferenceResolvedListener(
961cdf0e10cSrcweir 		m_nSignatureElementCollectorId, m_xReferenceListener);
962cdf0e10cSrcweir }
963cdf0e10cSrcweir 
addReferenceURI(const rtl::OUString & ouUri)964cdf0e10cSrcweir void SignatureEntity::addReferenceURI(const rtl::OUString& ouUri)
965cdf0e10cSrcweir {
966cdf0e10cSrcweir 	m_vReferenceIds.push_back(ouUri);
967cdf0e10cSrcweir }
968cdf0e10cSrcweir 
setReferenceNumber() const969cdf0e10cSrcweir void SignatureEntity::setReferenceNumber() const
970cdf0e10cSrcweir {
971cdf0e10cSrcweir 	cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
972cdf0e10cSrcweir 		(m_xReferenceListener, cssu::UNO_QUERY);
973cdf0e10cSrcweir 	xReferenceCollector->setReferenceCount(m_vReferenceIds.size());
974cdf0e10cSrcweir }
975cdf0e10cSrcweir 
hasReference(const rtl::OUString & ouUri) const976cdf0e10cSrcweir bool SignatureEntity::hasReference(const rtl::OUString& ouUri) const
977cdf0e10cSrcweir {
978cdf0e10cSrcweir 	bool rc = false;
979cdf0e10cSrcweir 
980cdf0e10cSrcweir 	std::vector<const rtl::OUString>::const_iterator ii;
981cdf0e10cSrcweir 	for (ii = m_vReferenceIds.begin(); ii != m_vReferenceIds.end(); ++ii)
982cdf0e10cSrcweir 	{
983cdf0e10cSrcweir 		if (ouUri == *ii)
984cdf0e10cSrcweir 		{
985cdf0e10cSrcweir 			rc = true;
986cdf0e10cSrcweir 			break;
987cdf0e10cSrcweir 		}
988cdf0e10cSrcweir 	}
989cdf0e10cSrcweir 
990cdf0e10cSrcweir 	return rc;
991cdf0e10cSrcweir }
992cdf0e10cSrcweir 
setReference(const rtl::OUString & ouUri,bool bIsExporting) const993cdf0e10cSrcweir bool SignatureEntity::setReference(const rtl::OUString& ouUri, bool bIsExporting) const
994cdf0e10cSrcweir {
995cdf0e10cSrcweir 	bool rc = false;
996cdf0e10cSrcweir 
997cdf0e10cSrcweir 	if (hasReference(ouUri))
998cdf0e10cSrcweir 	{
999cdf0e10cSrcweir 		int nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
1000cdf0e10cSrcweir 				bIsExporting ?
1001cdf0e10cSrcweir 				(cssxc::sax::ElementMarkPriority_AFTERMODIFY):
1002cdf0e10cSrcweir 				(cssxc::sax::ElementMarkPriority_BEFOREMODIFY),
1003cdf0e10cSrcweir 				false);
1004cdf0e10cSrcweir 
1005cdf0e10cSrcweir 		m_xSAXEventKeeper->setSecurityId(nKeeperId, m_nSecurityId);
1006cdf0e10cSrcweir 
1007cdf0e10cSrcweir 		cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
1008cdf0e10cSrcweir 			(m_xSAXEventKeeper, cssu::UNO_QUERY);
1009cdf0e10cSrcweir 		xReferenceResolvedBroadcaster->addReferenceResolvedListener(nKeeperId, m_xReferenceListener);
1010cdf0e10cSrcweir 
1011cdf0e10cSrcweir 		cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
1012cdf0e10cSrcweir 			(m_xReferenceListener, cssu::UNO_QUERY);
1013cdf0e10cSrcweir 		xReferenceCollector->setReferenceId(nKeeperId);
1014cdf0e10cSrcweir 
1015cdf0e10cSrcweir 		rc = true;
1016cdf0e10cSrcweir 	}
1017cdf0e10cSrcweir 
1018cdf0e10cSrcweir 	return rc;
1019cdf0e10cSrcweir }
1020cdf0e10cSrcweir 
1021cdf0e10cSrcweir /* XDocumentHandler */
startDocument()1022cdf0e10cSrcweir void SAL_CALL XSecTester::startDocument()
1023cdf0e10cSrcweir 	throw (cssu::RuntimeException)
1024cdf0e10cSrcweir {
1025cdf0e10cSrcweir }
1026cdf0e10cSrcweir 
endDocument()1027cdf0e10cSrcweir void SAL_CALL XSecTester::endDocument()
1028cdf0e10cSrcweir 	throw (cssu::RuntimeException)
1029cdf0e10cSrcweir {
1030cdf0e10cSrcweir }
1031cdf0e10cSrcweir 
characters(const class rtl::OUString & chars)1032cdf0e10cSrcweir void SAL_CALL XSecTester::characters(const class rtl::OUString & chars)
1033cdf0e10cSrcweir 	throw (cssu::RuntimeException)
1034cdf0e10cSrcweir {
1035cdf0e10cSrcweir 	m_xExportHandler->characters(chars);
1036cdf0e10cSrcweir }
1037cdf0e10cSrcweir 
processingInstruction(const rtl::OUString & target,const rtl::OUString & data)1038cdf0e10cSrcweir void SAL_CALL XSecTester::processingInstruction(const rtl::OUString & target, const rtl::OUString &data)
1039cdf0e10cSrcweir 	throw (cssu::RuntimeException)
1040cdf0e10cSrcweir {
1041cdf0e10cSrcweir 	m_xExportHandler->processingInstruction(target, data);
1042cdf0e10cSrcweir }
1043cdf0e10cSrcweir 
ignorableWhitespace(const rtl::OUString &)1044cdf0e10cSrcweir void SAL_CALL XSecTester::ignorableWhitespace(const rtl::OUString &)
1045cdf0e10cSrcweir 	throw (cssu::RuntimeException)
1046cdf0e10cSrcweir {
1047cdf0e10cSrcweir 
1048cdf0e10cSrcweir }
1049cdf0e10cSrcweir 
startElement(const rtl::OUString & name,const cssu::Reference<cssxs::XAttributeList> & xAttribs)1050cdf0e10cSrcweir void SAL_CALL XSecTester::startElement(const rtl::OUString & name, const cssu::Reference<cssxs::XAttributeList> &xAttribs)
1051cdf0e10cSrcweir 	throw (cssu::RuntimeException)
1052cdf0e10cSrcweir {
1053cdf0e10cSrcweir 	rtl::OUString ouIdAttr = xAttribs->getValueByName(
1054cdf0e10cSrcweir 		rtl::OUString(RTL_ASCII_USTRINGPARAM("id")));
1055cdf0e10cSrcweir 
1056cdf0e10cSrcweir 	if (ouIdAttr == NULL)
1057cdf0e10cSrcweir 	{
1058cdf0e10cSrcweir 		ouIdAttr = xAttribs->getValueByName(
1059cdf0e10cSrcweir 			rtl::OUString(RTL_ASCII_USTRINGPARAM("Id")));
1060cdf0e10cSrcweir 	}
1061cdf0e10cSrcweir 
1062cdf0e10cSrcweir 	bool bHasIdAttr = (ouIdAttr != NULL && ouIdAttr.getLength() > 0 );
1063cdf0e10cSrcweir 	bool needResend = false;
1064cdf0e10cSrcweir 
1065cdf0e10cSrcweir 	if (bHasIdAttr || name.equalsAscii( SIGNATURE_STR ))
1066cdf0e10cSrcweir 	{
1067cdf0e10cSrcweir 		if (foundSecurityRelated() && ! m_bIsExporting)
1068cdf0e10cSrcweir 		{
1069cdf0e10cSrcweir 			needResend = true;
1070cdf0e10cSrcweir 		}
1071cdf0e10cSrcweir 	}
1072cdf0e10cSrcweir 
1073cdf0e10cSrcweir 	if ( !m_bIsSAXEventKeeperOnTheSAXChain )
1074cdf0e10cSrcweir 	{
1075cdf0e10cSrcweir 		addStartAncestorEvent(name, xAttribs);
1076cdf0e10cSrcweir 	}
1077cdf0e10cSrcweir 
1078cdf0e10cSrcweir 	bool bSuppressingForwarding = checkSecurityElement(name, xAttribs);
1079cdf0e10cSrcweir 
1080cdf0e10cSrcweir 	checkReference(name, xAttribs, ouIdAttr);
1081cdf0e10cSrcweir 
1082cdf0e10cSrcweir 	if (needResend)
1083cdf0e10cSrcweir 	{
1084cdf0e10cSrcweir 		m_xSAXEventKeeper->setNextHandler(NULL);
1085cdf0e10cSrcweir 
1086cdf0e10cSrcweir 		cssu::Reference<cssxs::XDocumentHandler> xSAXEventKeeperHandler
1087cdf0e10cSrcweir 			(m_xSAXEventKeeper, cssu::UNO_QUERY);
1088cdf0e10cSrcweir 
1089cdf0e10cSrcweir 		xSAXEventKeeperHandler->startElement(name, xAttribs);
1090cdf0e10cSrcweir 		m_xSAXEventKeeper->setNextHandler(this);
1091cdf0e10cSrcweir 	}
1092cdf0e10cSrcweir 
1093cdf0e10cSrcweir 	if (!bSuppressingForwarding)
1094cdf0e10cSrcweir 	{
1095cdf0e10cSrcweir 		m_xExportHandler->startElement(name, xAttribs);
1096cdf0e10cSrcweir 	}
1097cdf0e10cSrcweir }
1098cdf0e10cSrcweir 
endElement(const rtl::OUString & name)1099cdf0e10cSrcweir void SAL_CALL XSecTester::endElement(const rtl::OUString& name)
1100cdf0e10cSrcweir 	throw (cssu::RuntimeException)
1101cdf0e10cSrcweir {
1102cdf0e10cSrcweir 	if (!m_stCurrentPath.empty())
1103cdf0e10cSrcweir 	{
1104cdf0e10cSrcweir 		void* pSignedInfo = m_stCurrentPath.top();
1105cdf0e10cSrcweir 		bool bIsStringType = m_stCurrentPathType.top();
1106cdf0e10cSrcweir 
1107cdf0e10cSrcweir 		m_stCurrentPath.pop();
1108cdf0e10cSrcweir 		m_stCurrentPathType.pop();
1109cdf0e10cSrcweir 
1110cdf0e10cSrcweir 		if (bIsStringType && !strcmp((const char *)pSignedInfo, SIGNEDINFO_STR))
1111cdf0e10cSrcweir 		{
1112cdf0e10cSrcweir 			if (!m_stCurrentPath.empty())
1113cdf0e10cSrcweir 			{
1114cdf0e10cSrcweir 				void* pSignature = m_stCurrentPath.top();
1115cdf0e10cSrcweir 				bIsStringType = m_stCurrentPathType.top();
1116cdf0e10cSrcweir 
1117cdf0e10cSrcweir 				if (!bIsStringType && pSignature != NULL)
1118cdf0e10cSrcweir 				{
1119cdf0e10cSrcweir 					((SignatureEntity *) pSignature)->setReferenceNumber();
1120cdf0e10cSrcweir 				}
1121cdf0e10cSrcweir 			}
1122cdf0e10cSrcweir 		}
1123cdf0e10cSrcweir 	}
1124cdf0e10cSrcweir 
1125cdf0e10cSrcweir 	if ( !m_bIsSAXEventKeeperOnTheSAXChain )
1126cdf0e10cSrcweir 	{
1127cdf0e10cSrcweir 		addEndAncestorEvent(name);
1128cdf0e10cSrcweir 	}
1129cdf0e10cSrcweir 
1130cdf0e10cSrcweir 	m_xExportHandler->endElement(name);
1131cdf0e10cSrcweir }
1132cdf0e10cSrcweir 
setDocumentLocator(const cssu::Reference<cssxs::XLocator> &)1133cdf0e10cSrcweir void SAL_CALL XSecTester::setDocumentLocator( const cssu::Reference<cssxs::XLocator>& )
1134cdf0e10cSrcweir 	throw (cssu::RuntimeException)
1135cdf0e10cSrcweir {
1136cdf0e10cSrcweir }
1137cdf0e10cSrcweir 
changeOutput()1138cdf0e10cSrcweir void XSecTester::changeOutput()
1139cdf0e10cSrcweir {
1140cdf0e10cSrcweir 	if (m_bIsExporting)
1141cdf0e10cSrcweir 	{
1142cdf0e10cSrcweir 		if (m_bIsSAXEventKeeperOnTheSAXChain)
1143cdf0e10cSrcweir 		{
1144cdf0e10cSrcweir 			m_xExportHandler = cssu::Reference<cssxs::XDocumentHandler>
1145cdf0e10cSrcweir 				(m_xSAXEventKeeper, cssu::UNO_QUERY);
1146cdf0e10cSrcweir 
1147cdf0e10cSrcweir 			m_xSAXEventKeeper->setNextHandler(NULL);
1148cdf0e10cSrcweir 
1149cdf0e10cSrcweir 			flushAncestorEvents(m_xExportHandler);
1150cdf0e10cSrcweir 
1151cdf0e10cSrcweir 			m_xSAXEventKeeper->setNextHandler(m_xOutputHandler);
1152cdf0e10cSrcweir 		}
1153cdf0e10cSrcweir 		else
1154cdf0e10cSrcweir 		{
1155cdf0e10cSrcweir 			m_xExportHandler = m_xOutputHandler;
1156cdf0e10cSrcweir 		}
1157cdf0e10cSrcweir 	}
1158cdf0e10cSrcweir  	else
1159cdf0e10cSrcweir  	{
1160cdf0e10cSrcweir 		if (m_bIsSAXEventKeeperOnTheSAXChain)
1161cdf0e10cSrcweir 		{
1162cdf0e10cSrcweir 			cssu::Reference<cssxs::XDocumentHandler> xSAXEventKeeperHandler
1163cdf0e10cSrcweir 				(m_xSAXEventKeeper, cssu::UNO_QUERY);
1164cdf0e10cSrcweir 
1165cdf0e10cSrcweir 			m_xSAXEventKeeper->setNextHandler(NULL);
1166cdf0e10cSrcweir 
1167cdf0e10cSrcweir 			flushAncestorEvents(xSAXEventKeeperHandler);
1168cdf0e10cSrcweir 
1169cdf0e10cSrcweir 			m_xSaxParser->setDocumentHandler(xSAXEventKeeperHandler);
1170cdf0e10cSrcweir 			m_xSAXEventKeeper->setNextHandler(this);
1171cdf0e10cSrcweir 		}
1172cdf0e10cSrcweir 		else
1173cdf0e10cSrcweir 		{
1174cdf0e10cSrcweir 			m_xSaxParser->setDocumentHandler(this);
1175cdf0e10cSrcweir 		}
1176cdf0e10cSrcweir 
1177cdf0e10cSrcweir 	}
1178cdf0e10cSrcweir }
1179cdf0e10cSrcweir 
foundSecurityRelated()1180cdf0e10cSrcweir bool XSecTester::foundSecurityRelated()
1181cdf0e10cSrcweir {
1182cdf0e10cSrcweir 	if (m_xSAXEventKeeper == NULL)
1183cdf0e10cSrcweir 	{
1184cdf0e10cSrcweir 		m_bIsBlocking = false;
1185cdf0e10cSrcweir 		m_bIsInsideCollectedElement = false;
1186cdf0e10cSrcweir 
1187cdf0e10cSrcweir 		m_xXMLDocumentWrapper = cssu::Reference<cssxw::XXMLDocumentWrapper>
1188cdf0e10cSrcweir 			(mxMSF->createInstance( m_ouXMLDocumentWrapperComponentName ),
1189cdf0e10cSrcweir 			 cssu::UNO_QUERY);
1190cdf0e10cSrcweir 
1191cdf0e10cSrcweir 		m_xSAXEventKeeper = cssu::Reference< cssxc::sax::XSecuritySAXEventKeeper >
1192cdf0e10cSrcweir 			(mxMSF->createInstance( rtl::OUString::createFromAscii( SAXEVENTKEEPER_COMPONENT )),
1193cdf0e10cSrcweir 			 cssu::UNO_QUERY);
1194cdf0e10cSrcweir 
1195cdf0e10cSrcweir 		cssu::Reference<cssl::XInitialization> xInitialization(m_xSAXEventKeeper,  cssu::UNO_QUERY);
1196cdf0e10cSrcweir 
1197cdf0e10cSrcweir 		cssu::Sequence <cssu::Any> arg(1);
1198cdf0e10cSrcweir 		arg[0] = cssu::makeAny(m_xXMLDocumentWrapper);
1199cdf0e10cSrcweir 		xInitialization->initialize(arg);
1200cdf0e10cSrcweir 
1201cdf0e10cSrcweir 		cssu::Reference<cssxc::sax::XSAXEventKeeperStatusChangeBroadcaster>
1202cdf0e10cSrcweir 			xSAXEventKeeperStatusChangeBroadcaster(m_xSAXEventKeeper, cssu::UNO_QUERY);
1203cdf0e10cSrcweir 		xSAXEventKeeperStatusChangeBroadcaster->addSAXEventKeeperStatusChangeListener(this);
1204cdf0e10cSrcweir 	}
1205cdf0e10cSrcweir 
1206cdf0e10cSrcweir 	bool rc = false;
1207cdf0e10cSrcweir 
1208cdf0e10cSrcweir 	if (!m_bIsSAXEventKeeperOnTheSAXChain)
1209cdf0e10cSrcweir 	{
1210cdf0e10cSrcweir 		rc = true;
1211cdf0e10cSrcweir 	}
1212cdf0e10cSrcweir 
1213cdf0e10cSrcweir 	m_bIsSAXEventKeeperOnTheSAXChain=true;
1214cdf0e10cSrcweir 	changeOutput();
1215cdf0e10cSrcweir 
1216cdf0e10cSrcweir 	return rc;
1217cdf0e10cSrcweir }
1218cdf0e10cSrcweir 
findKeyOrReference(SecurityEntity * pSecurityEntity,const rtl::OUString & ouUri,bool bIsFindingKey)1219cdf0e10cSrcweir void XSecTester::findKeyOrReference(SecurityEntity* pSecurityEntity, const rtl::OUString& ouUri, bool bIsFindingKey)
1220cdf0e10cSrcweir {
1221cdf0e10cSrcweir 	std::vector<rtl::OUString>::iterator ii_referenceURIs;
1222cdf0e10cSrcweir 	std::vector<int>::iterator ii_referenceKeeperIds;
1223cdf0e10cSrcweir 	std::vector<int>::iterator ii_referenceRefNums;
1224cdf0e10cSrcweir 
1225cdf0e10cSrcweir 	for (ii_referenceURIs = m_vUnsolvedReferenceURIs.begin(),
1226cdf0e10cSrcweir 	     ii_referenceKeeperIds = m_vUnsolvedReferenceKeeperIds.begin(),
1227cdf0e10cSrcweir 	     ii_referenceRefNums = m_vUnsolvedReferenceRefNums.begin();
1228cdf0e10cSrcweir  	     ii_referenceURIs != m_vUnsolvedReferenceURIs.end(); )
1229cdf0e10cSrcweir 	{
1230cdf0e10cSrcweir 		rtl::OUString ouReferenceUri = *ii_referenceURIs;
1231cdf0e10cSrcweir 
1232cdf0e10cSrcweir 		if (ouReferenceUri == ouUri)
1233cdf0e10cSrcweir 		{
1234cdf0e10cSrcweir 			int nKeeperId = *ii_referenceKeeperIds;
1235cdf0e10cSrcweir 			int nRefNum = *ii_referenceRefNums;
1236cdf0e10cSrcweir 
1237cdf0e10cSrcweir 			if ( bIsFindingKey )
1238cdf0e10cSrcweir 			{
1239cdf0e10cSrcweir 			 	int nClonedKeeperId = m_xSAXEventKeeper->cloneElementCollector(
1240cdf0e10cSrcweir 			 		nKeeperId,
1241cdf0e10cSrcweir 			 		m_bIsExporting?
1242cdf0e10cSrcweir 			 		(cssxc::sax::ElementMarkPriority_BEFOREMODIFY):
1243cdf0e10cSrcweir 					(cssxc::sax::ElementMarkPriority_AFTERMODIFY));
1244cdf0e10cSrcweir 
1245cdf0e10cSrcweir 			 	pSecurityEntity->setKeyId(nClonedKeeperId);
1246cdf0e10cSrcweir 
1247cdf0e10cSrcweir 			 	m_xSAXEventKeeper->setSecurityId(nClonedKeeperId, pSecurityEntity->getSecurityId());
1248cdf0e10cSrcweir 
1249cdf0e10cSrcweir 			 	cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster>
1250cdf0e10cSrcweir 					xReferenceResolvedBroadcaster(m_xSAXEventKeeper, cssu::UNO_QUERY);
1251cdf0e10cSrcweir 				xReferenceResolvedBroadcaster->addReferenceResolvedListener(
1252cdf0e10cSrcweir 			 		nClonedKeeperId,
1253cdf0e10cSrcweir 			 		pSecurityEntity->getReferenceListener());
1254cdf0e10cSrcweir 			}
1255cdf0e10cSrcweir 			else
1256cdf0e10cSrcweir 			{
1257cdf0e10cSrcweir 				int nClonedKeeperId = m_xSAXEventKeeper->cloneElementCollector(
1258cdf0e10cSrcweir 					nKeeperId,
1259cdf0e10cSrcweir 					m_bIsExporting?
1260cdf0e10cSrcweir 					(cssxc::sax::ElementMarkPriority_AFTERMODIFY):
1261cdf0e10cSrcweir 					(cssxc::sax::ElementMarkPriority_BEFOREMODIFY));
1262cdf0e10cSrcweir 
1263cdf0e10cSrcweir 				m_xSAXEventKeeper->setSecurityId(nClonedKeeperId, pSecurityEntity->getSecurityId());
1264cdf0e10cSrcweir 
1265cdf0e10cSrcweir 				cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster>
1266cdf0e10cSrcweir 					xReferenceResolvedBroadcaster
1267cdf0e10cSrcweir 					(m_xSAXEventKeeper, cssu::UNO_QUERY);
1268cdf0e10cSrcweir 				xReferenceResolvedBroadcaster->addReferenceResolvedListener(
1269cdf0e10cSrcweir 					nClonedKeeperId,
1270cdf0e10cSrcweir 					pSecurityEntity->getReferenceListener());
1271cdf0e10cSrcweir 
1272cdf0e10cSrcweir 				cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
1273cdf0e10cSrcweir 						(pSecurityEntity->getReferenceListener(), cssu::UNO_QUERY);
1274cdf0e10cSrcweir 				xReferenceCollector->setReferenceId(nClonedKeeperId);
1275cdf0e10cSrcweir 			}
1276cdf0e10cSrcweir 
1277cdf0e10cSrcweir 			nRefNum--;
1278cdf0e10cSrcweir 			if (nRefNum == 0)
1279cdf0e10cSrcweir 			{
1280cdf0e10cSrcweir 				m_xSAXEventKeeper->removeElementCollector(nKeeperId);
1281cdf0e10cSrcweir 
1282cdf0e10cSrcweir 				ii_referenceURIs = m_vUnsolvedReferenceURIs.erase(ii_referenceURIs);
1283cdf0e10cSrcweir 				ii_referenceKeeperIds = m_vUnsolvedReferenceKeeperIds.erase(ii_referenceKeeperIds);
1284cdf0e10cSrcweir 				ii_referenceRefNums = m_vUnsolvedReferenceRefNums.erase(ii_referenceRefNums);
1285cdf0e10cSrcweir 			}
1286cdf0e10cSrcweir 			else
1287cdf0e10cSrcweir 			{
1288cdf0e10cSrcweir 				(*ii_referenceRefNums) = nRefNum;
1289cdf0e10cSrcweir 
1290cdf0e10cSrcweir 				ii_referenceURIs++;
1291cdf0e10cSrcweir 				ii_referenceKeeperIds++;
1292cdf0e10cSrcweir 				ii_referenceRefNums++;
1293cdf0e10cSrcweir 			}
1294cdf0e10cSrcweir 
1295cdf0e10cSrcweir 			if (bIsFindingKey)
1296cdf0e10cSrcweir 			{
1297cdf0e10cSrcweir 				break;
1298cdf0e10cSrcweir 			}
1299cdf0e10cSrcweir 		}
1300cdf0e10cSrcweir 		else
1301cdf0e10cSrcweir 		{
1302cdf0e10cSrcweir 			ii_referenceURIs++;
1303cdf0e10cSrcweir 			ii_referenceKeeperIds++;
1304cdf0e10cSrcweir 			ii_referenceRefNums++;
1305cdf0e10cSrcweir 		}
1306cdf0e10cSrcweir 	}
1307cdf0e10cSrcweir }
1308cdf0e10cSrcweir 
checkSecurityElement(const rtl::OUString & ouLocalName,const cssu::Reference<cssxs::XAttributeList> & xAttribs)1309cdf0e10cSrcweir bool XSecTester::checkSecurityElement(
1310cdf0e10cSrcweir 	const rtl::OUString& ouLocalName,
1311cdf0e10cSrcweir 	const cssu::Reference<cssxs::XAttributeList>& xAttribs)
1312cdf0e10cSrcweir {
1313cdf0e10cSrcweir 	bool rc = false;
1314cdf0e10cSrcweir 
1315cdf0e10cSrcweir 	if (ouLocalName.equalsAscii(SIGNATURE_STR))
1316cdf0e10cSrcweir 	{
1317cdf0e10cSrcweir 		SignatureEntity* pSignatureEntity = new SignatureEntity(
1318cdf0e10cSrcweir 			m_xSAXEventKeeper,
1319cdf0e10cSrcweir 			m_bIsExporting,
1320cdf0e10cSrcweir 			this,
1321cdf0e10cSrcweir 			m_xXMLSecurityContext,
1322cdf0e10cSrcweir 			m_xXMLSignature,
1323cdf0e10cSrcweir 			mxMSF);
1324cdf0e10cSrcweir 
1325cdf0e10cSrcweir 		m_vSignatureList.push_back(pSignatureEntity);
1326cdf0e10cSrcweir 
1327cdf0e10cSrcweir 		m_stCurrentPath.push(pSignatureEntity);
1328cdf0e10cSrcweir 		m_stCurrentPathType.push(false);
1329cdf0e10cSrcweir 	}
1330cdf0e10cSrcweir 	else if (ouLocalName.equalsAscii(REFERENCE_STR))
1331cdf0e10cSrcweir 	{
1332cdf0e10cSrcweir 		if (!m_stCurrentPath.empty())
1333cdf0e10cSrcweir 		{
1334cdf0e10cSrcweir 	 		void* pSignedInfo = m_stCurrentPath.top();
1335cdf0e10cSrcweir 	 		bool bIsStringType = m_stCurrentPathType.top();
1336cdf0e10cSrcweir 
1337cdf0e10cSrcweir 			m_stCurrentPath.pop();
1338cdf0e10cSrcweir 			m_stCurrentPathType.pop();
1339cdf0e10cSrcweir 
1340cdf0e10cSrcweir 			if (bIsStringType && !m_stCurrentPath.empty())
1341cdf0e10cSrcweir 			{
1342cdf0e10cSrcweir 				void* pSignature = m_stCurrentPath.top();
1343cdf0e10cSrcweir 				bool bIsStringType2 = m_stCurrentPathType.top();
1344cdf0e10cSrcweir 
1345cdf0e10cSrcweir 				if (!strcmp((const char*)pSignedInfo, SIGNEDINFO_STR) && !bIsStringType2)
1346cdf0e10cSrcweir 				{
1347cdf0e10cSrcweir 					rtl::OUString ouUri = xAttribs->getValueByName
1348cdf0e10cSrcweir 						(rtl::OUString(RTL_ASCII_USTRINGPARAM( URI_ATTR_STR )));
1349cdf0e10cSrcweir 
1350cdf0e10cSrcweir 					if (ouUri.matchAsciiL("#", 1, 0))
1351cdf0e10cSrcweir 					{
1352cdf0e10cSrcweir 						rtl::OUString uri = ouUri.copy(1);
1353cdf0e10cSrcweir  						SignatureEntity* pSignatureEntity = (SignatureEntity *)pSignature;
1354cdf0e10cSrcweir 
1355cdf0e10cSrcweir 						if (uri != NULL && uri.getLength()>0)
1356cdf0e10cSrcweir 						{
1357cdf0e10cSrcweir 							pSignatureEntity->addReferenceURI(uri);
1358cdf0e10cSrcweir 							findKeyOrReference(pSignatureEntity, uri, true);
1359cdf0e10cSrcweir 						}
1360cdf0e10cSrcweir 					}
1361cdf0e10cSrcweir 				}
1362cdf0e10cSrcweir 			}
1363cdf0e10cSrcweir 			m_stCurrentPath.push(pSignedInfo);
1364cdf0e10cSrcweir 			m_stCurrentPathType.push(bIsStringType);
1365cdf0e10cSrcweir 		}
1366cdf0e10cSrcweir 		m_stCurrentPath.push( (void *)REFERENCE_STR);
1367cdf0e10cSrcweir 		m_stCurrentPathType.push(true);
1368cdf0e10cSrcweir 	}
1369cdf0e10cSrcweir 	else if(ouLocalName.equalsAscii(KEYVALUE_STR) ||
1370cdf0e10cSrcweir 		ouLocalName.equalsAscii(KEYNAME_STR) ||
1371cdf0e10cSrcweir 		ouLocalName.equalsAscii(X509DATA_STR) ||
1372cdf0e10cSrcweir 		ouLocalName.equalsAscii(ENCRYPTEDKEY_STR))
1373cdf0e10cSrcweir 	{
1374cdf0e10cSrcweir 		if (!m_stCurrentPath.empty())
1375cdf0e10cSrcweir 		{
1376cdf0e10cSrcweir 			void* pKeyInfo = m_stCurrentPath.top();
1377cdf0e10cSrcweir 			bool bIsStringType = m_stCurrentPathType.top();
1378cdf0e10cSrcweir 
1379cdf0e10cSrcweir 			m_stCurrentPath.pop();
1380cdf0e10cSrcweir 			m_stCurrentPathType.pop();
1381cdf0e10cSrcweir 
1382cdf0e10cSrcweir  			if (bIsStringType && !m_stCurrentPath.empty())
1383cdf0e10cSrcweir  			{
1384cdf0e10cSrcweir 				bool bIsStringType2 = m_stCurrentPathType.top();
1385cdf0e10cSrcweir 
1386cdf0e10cSrcweir 				if (!bIsStringType2)
1387cdf0e10cSrcweir 				{
1388cdf0e10cSrcweir 					SecurityEntity *pSecurityEntity =
1389cdf0e10cSrcweir 						(SecurityEntity *) (m_stCurrentPath.top());
1390cdf0e10cSrcweir 					pSecurityEntity->setKeyId(0);
1391cdf0e10cSrcweir 				}
1392cdf0e10cSrcweir  			}
1393cdf0e10cSrcweir 
1394cdf0e10cSrcweir 			m_stCurrentPath.push(pKeyInfo);
1395cdf0e10cSrcweir 			m_stCurrentPathType.push(bIsStringType);
1396cdf0e10cSrcweir 		}
1397cdf0e10cSrcweir 
1398cdf0e10cSrcweir 		m_stCurrentPath.push((void *)KEYVALUE_STR);
1399cdf0e10cSrcweir 		m_stCurrentPathType.push(true);
1400cdf0e10cSrcweir 	}
1401cdf0e10cSrcweir 	else if(ouLocalName.equalsAscii(RETRIEVALMETHOD_STR))
1402cdf0e10cSrcweir 	{
1403cdf0e10cSrcweir 		if (!m_stCurrentPath.empty())
1404cdf0e10cSrcweir 		{
1405cdf0e10cSrcweir 			void* pKeyInfo = m_stCurrentPath.top();
1406cdf0e10cSrcweir 			bool bIsStringType = m_stCurrentPathType.top();
1407cdf0e10cSrcweir 
1408cdf0e10cSrcweir 			m_stCurrentPath.pop();
1409cdf0e10cSrcweir 			m_stCurrentPathType.pop();
1410cdf0e10cSrcweir 
1411cdf0e10cSrcweir 			if (bIsStringType && !m_stCurrentPath.empty())
1412cdf0e10cSrcweir 			{
1413cdf0e10cSrcweir 				bool bIsStringType2 = m_stCurrentPathType.top();
1414cdf0e10cSrcweir 
1415cdf0e10cSrcweir 				if (!bIsStringType2)
1416cdf0e10cSrcweir 				{
1417cdf0e10cSrcweir 					SecurityEntity *pSecurityEntity =
1418cdf0e10cSrcweir 						(SecurityEntity *) m_stCurrentPath.top();
1419cdf0e10cSrcweir 					rtl::OUString ouUri = xAttribs->getValueByName(
1420cdf0e10cSrcweir 						rtl::OUString(RTL_ASCII_USTRINGPARAM( URI_ATTR_STR )));
1421cdf0e10cSrcweir 
1422cdf0e10cSrcweir 					if (!strcmp((const char *)pKeyInfo, KEYINFO_STR) &&
1423cdf0e10cSrcweir 						ouUri != NULL && ouUri.getLength()>0)
1424cdf0e10cSrcweir 					{
1425cdf0e10cSrcweir 						pSecurityEntity->setKeyURI(ouUri);
1426cdf0e10cSrcweir 						findKeyOrReference(pSecurityEntity, ouUri, true);
1427cdf0e10cSrcweir 					}
1428cdf0e10cSrcweir 				}
1429cdf0e10cSrcweir 
1430cdf0e10cSrcweir 			}
1431cdf0e10cSrcweir 
1432cdf0e10cSrcweir 			m_stCurrentPath.push(pKeyInfo);
1433cdf0e10cSrcweir 			m_stCurrentPathType.push(bIsStringType);
1434cdf0e10cSrcweir 		}
1435cdf0e10cSrcweir 
1436cdf0e10cSrcweir 		m_stCurrentPath.push((void *)RETRIEVALMETHOD_STR);
1437cdf0e10cSrcweir 		m_stCurrentPathType.push(true);
1438cdf0e10cSrcweir 	}
1439cdf0e10cSrcweir 	else if(ouLocalName.equalsAscii(KEYINFO_STR))
1440cdf0e10cSrcweir 	{
1441cdf0e10cSrcweir 		m_stCurrentPath.push((void *)KEYINFO_STR);
1442cdf0e10cSrcweir 		m_stCurrentPathType.push(true);
1443cdf0e10cSrcweir 	}
1444cdf0e10cSrcweir 	else if(ouLocalName.equalsAscii(SIGNEDINFO_STR))
1445cdf0e10cSrcweir 	{
1446cdf0e10cSrcweir 		m_stCurrentPath.push((void *)SIGNEDINFO_STR);
1447cdf0e10cSrcweir 		m_stCurrentPathType.push(true);
1448cdf0e10cSrcweir 	}
1449cdf0e10cSrcweir 	else
1450cdf0e10cSrcweir 	{
1451cdf0e10cSrcweir 		m_stCurrentPath.push((void *)OTHER_ELEMENT_STR);
1452cdf0e10cSrcweir 		m_stCurrentPathType.push(true);
1453cdf0e10cSrcweir 	}
1454cdf0e10cSrcweir 
1455cdf0e10cSrcweir 	return rc;
1456cdf0e10cSrcweir }
1457cdf0e10cSrcweir 
checkReference(const rtl::OUString & ouLocalName,const cssu::Reference<cssxs::XAttributeList> & xAttribs,const rtl::OUString & ouId)1458cdf0e10cSrcweir void XSecTester::checkReference(
1459cdf0e10cSrcweir 	const rtl::OUString& ouLocalName,
1460cdf0e10cSrcweir 	const cssu::Reference<cssxs::XAttributeList>& xAttribs,
1461cdf0e10cSrcweir 	const rtl::OUString& ouId)
1462cdf0e10cSrcweir {
1463cdf0e10cSrcweir 	rtl::OUString refNumStr =
1464cdf0e10cSrcweir 		xAttribs->getValueByName(rtl::OUString(RTL_ASCII_USTRINGPARAM(REFNUM_ATTR_STR)));
1465cdf0e10cSrcweir 
1466cdf0e10cSrcweir 	if (ouId != NULL && ouId.getLength()>0 )
1467cdf0e10cSrcweir 	{
1468cdf0e10cSrcweir 		int nRefNum = 999;
1469cdf0e10cSrcweir 		if (refNumStr != NULL && refNumStr.getLength()>0 )
1470cdf0e10cSrcweir 		{
1471cdf0e10cSrcweir 			nRefNum = refNumStr.toInt32();
1472cdf0e10cSrcweir 		}
1473cdf0e10cSrcweir 
1474cdf0e10cSrcweir 		int nLength = m_vSignatureList.size();
1475cdf0e10cSrcweir 		for (int i = 0; i<nLength; ++i)
1476cdf0e10cSrcweir 		{
1477cdf0e10cSrcweir 			SignatureEntity* pSignatureEntity = m_vSignatureList.at(i);
1478cdf0e10cSrcweir 
1479cdf0e10cSrcweir 			if (pSignatureEntity->setReference(ouId, m_bIsExporting))
1480cdf0e10cSrcweir 			{
1481cdf0e10cSrcweir 				nRefNum--;
1482cdf0e10cSrcweir 			}
1483cdf0e10cSrcweir 
1484cdf0e10cSrcweir 			if (pSignatureEntity->setKey(ouId, m_bIsExporting))
1485cdf0e10cSrcweir 			{
1486cdf0e10cSrcweir 				nRefNum--;
1487cdf0e10cSrcweir 			}
1488cdf0e10cSrcweir 		}
1489cdf0e10cSrcweir 
1490cdf0e10cSrcweir 		if (nRefNum>0)
1491cdf0e10cSrcweir 		{
1492cdf0e10cSrcweir 			int nKeeperId;
1493cdf0e10cSrcweir 
1494cdf0e10cSrcweir 			if (ouLocalName.equalsAscii(ENCRYPTEDKEY_STR))
1495cdf0e10cSrcweir 			{
1496cdf0e10cSrcweir 				nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
1497cdf0e10cSrcweir 					m_bIsExporting ?
1498cdf0e10cSrcweir 					(cssxc::sax::ElementMarkPriority_BEFOREMODIFY):
1499cdf0e10cSrcweir 					(cssxc::sax::ElementMarkPriority_AFTERMODIFY),
1500cdf0e10cSrcweir 					true);
1501cdf0e10cSrcweir 			}
1502cdf0e10cSrcweir 			else
1503cdf0e10cSrcweir 			{
1504cdf0e10cSrcweir 				nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
1505cdf0e10cSrcweir 					m_bIsExporting?
1506cdf0e10cSrcweir 					(cssxc::sax::ElementMarkPriority_AFTERMODIFY):
1507cdf0e10cSrcweir 					(cssxc::sax::ElementMarkPriority_BEFOREMODIFY),
1508cdf0e10cSrcweir 					false);
1509cdf0e10cSrcweir 			}
1510cdf0e10cSrcweir 
1511cdf0e10cSrcweir 			m_vUnsolvedReferenceURIs.push_back(ouId);
1512cdf0e10cSrcweir 			m_vUnsolvedReferenceKeeperIds.push_back(nKeeperId);
1513cdf0e10cSrcweir 			m_vUnsolvedReferenceRefNums.push_back(nRefNum);
1514cdf0e10cSrcweir 		}
1515cdf0e10cSrcweir 	}
1516cdf0e10cSrcweir }
1517cdf0e10cSrcweir 
endMission()1518cdf0e10cSrcweir void XSecTester::endMission()
1519cdf0e10cSrcweir {
1520cdf0e10cSrcweir 	while (m_vSignatureList.size() > 0)
1521cdf0e10cSrcweir 	{
1522cdf0e10cSrcweir 		if (m_vSignatureList.size()>0)
1523cdf0e10cSrcweir 		{
1524cdf0e10cSrcweir 			SignatureEntity * pSignatureEntity = m_vSignatureList.at(0);
1525cdf0e10cSrcweir 			m_vSignatureList.erase(m_vSignatureList.begin());
1526cdf0e10cSrcweir 			pSignatureEntity->endMission();
1527cdf0e10cSrcweir 			delete pSignatureEntity;
1528cdf0e10cSrcweir 		}
1529cdf0e10cSrcweir 	}
1530cdf0e10cSrcweir 
1531cdf0e10cSrcweir 	while (m_vUnsolvedReferenceURIs.size()>0)
1532cdf0e10cSrcweir 	{
1533cdf0e10cSrcweir 		int nKeeperId = m_vUnsolvedReferenceKeeperIds.at(0);
1534cdf0e10cSrcweir 		m_xSAXEventKeeper->removeElementCollector(nKeeperId);
1535cdf0e10cSrcweir 		m_vUnsolvedReferenceURIs.erase(m_vUnsolvedReferenceURIs.begin());
1536cdf0e10cSrcweir 		m_vUnsolvedReferenceKeeperIds.erase(m_vUnsolvedReferenceKeeperIds.begin());
1537cdf0e10cSrcweir 		m_vUnsolvedReferenceRefNums.erase(m_vUnsolvedReferenceRefNums.begin());
1538cdf0e10cSrcweir 	}
1539cdf0e10cSrcweir }
1540cdf0e10cSrcweir 
addStartAncestorEvent(const rtl::OUString & ouName,const cssu::Reference<cssxs::XAttributeList> & xAttribs)1541cdf0e10cSrcweir void XSecTester::addStartAncestorEvent(
1542cdf0e10cSrcweir 	const rtl::OUString& ouName,
1543cdf0e10cSrcweir 	const cssu::Reference< cssxs::XAttributeList >& xAttribs)
1544cdf0e10cSrcweir {
1545cdf0e10cSrcweir 	sal_Int32 nLength = xAttribs->getLength();
1546cdf0e10cSrcweir 	AncestorEvent* ancestorEvent = new AncestorEvent( nLength );
1547cdf0e10cSrcweir 
1548cdf0e10cSrcweir 	ancestorEvent->bIsStartElement = true;
1549cdf0e10cSrcweir 	ancestorEvent->ouName = ouName;
1550cdf0e10cSrcweir 
1551cdf0e10cSrcweir 	for (int i = 0; i<nLength; ++i)
1552cdf0e10cSrcweir 	{
1553cdf0e10cSrcweir 		(ancestorEvent->aAttributeList[i]).sName = xAttribs->getNameByIndex((short)i);
1554cdf0e10cSrcweir 		(ancestorEvent->aAttributeList[i]).sValue =xAttribs->getValueByIndex((short)i);
1555cdf0e10cSrcweir 	}
1556cdf0e10cSrcweir 
1557cdf0e10cSrcweir 	m_vAncestorEvents.push_back(ancestorEvent);
1558cdf0e10cSrcweir }
1559cdf0e10cSrcweir 
addEndAncestorEvent(const rtl::OUString & ouName)1560cdf0e10cSrcweir void XSecTester::addEndAncestorEvent(const rtl::OUString& ouName)
1561cdf0e10cSrcweir {
1562cdf0e10cSrcweir 	AncestorEvent* ancestorEvent = new AncestorEvent(0);
1563cdf0e10cSrcweir 
1564cdf0e10cSrcweir 	ancestorEvent->bIsStartElement = false;
1565cdf0e10cSrcweir 	ancestorEvent->ouName = ouName;
1566cdf0e10cSrcweir 
1567cdf0e10cSrcweir 	m_vAncestorEvents.push_back(ancestorEvent);
1568cdf0e10cSrcweir }
1569cdf0e10cSrcweir 
sendAncestorStartElementEvent(const rtl::OUString & ouName,const cssu::Sequence<cssxcsax::XMLAttribute> & attrList,const cssu::Reference<cssxs::XDocumentHandler> & xDocumentHandler) const1570cdf0e10cSrcweir void XSecTester::sendAncestorStartElementEvent(
1571cdf0e10cSrcweir 	const rtl::OUString& ouName,
1572cdf0e10cSrcweir 	const cssu::Sequence< cssxcsax::XMLAttribute >& attrList,
1573cdf0e10cSrcweir 	const cssu::Reference< cssxs::XDocumentHandler >& xDocumentHandler) const
1574cdf0e10cSrcweir {
1575cdf0e10cSrcweir 	SvXMLAttributeList* pAttributeList = new SvXMLAttributeList();
1576cdf0e10cSrcweir 	cssu::Reference < cssxs::XAttributeList > xAttrList
1577cdf0e10cSrcweir 		= cssu::Reference< cssxs::XAttributeList > (pAttributeList);
1578cdf0e10cSrcweir 
1579cdf0e10cSrcweir 	sal_Int32 nLength = attrList.getLength();
1580cdf0e10cSrcweir 
1581cdf0e10cSrcweir 	for (int i = 0; i<nLength; ++i)
1582cdf0e10cSrcweir 	{
1583cdf0e10cSrcweir 		pAttributeList->AddAttribute( attrList[i].sName, attrList[i].sValue);
1584cdf0e10cSrcweir 	}
1585cdf0e10cSrcweir 
1586cdf0e10cSrcweir 	xDocumentHandler->startElement(ouName, xAttrList);
1587cdf0e10cSrcweir }
1588cdf0e10cSrcweir 
sendAncestorEndElementEvent(const rtl::OUString & ouName,const cssu::Reference<cssxs::XDocumentHandler> & xDocumentHandler) const1589cdf0e10cSrcweir void XSecTester::sendAncestorEndElementEvent(
1590cdf0e10cSrcweir 	const rtl::OUString& ouName,
1591cdf0e10cSrcweir 	const cssu::Reference< cssxs::XDocumentHandler >& xDocumentHandler) const
1592cdf0e10cSrcweir {
1593cdf0e10cSrcweir 	xDocumentHandler->endElement(ouName);
1594cdf0e10cSrcweir }
1595cdf0e10cSrcweir 
checkAncestorStartElementEvent(const std::vector<AncestorEvent * >::const_iterator & ii,const cssu::Reference<cssxs::XDocumentHandler> & xDocumentHandler) const1596cdf0e10cSrcweir std::vector< AncestorEvent* >::const_iterator XSecTester::checkAncestorStartElementEvent(
1597cdf0e10cSrcweir 	const std::vector< AncestorEvent* >::const_iterator& ii,
1598cdf0e10cSrcweir 	const cssu::Reference< cssxs::XDocumentHandler >& xDocumentHandler) const
1599cdf0e10cSrcweir {
1600cdf0e10cSrcweir 	std::vector< AncestorEvent* >::const_iterator next = ii+1;
1601cdf0e10cSrcweir 
1602cdf0e10cSrcweir 	if (next == m_vAncestorEvents.end())
1603cdf0e10cSrcweir 	{
1604cdf0e10cSrcweir 		sendAncestorStartElementEvent(
1605cdf0e10cSrcweir 			(*ii)->ouName, (*ii)->aAttributeList, xDocumentHandler);
1606cdf0e10cSrcweir 	}
1607cdf0e10cSrcweir 	else
1608cdf0e10cSrcweir 	{
1609cdf0e10cSrcweir 		while ((next != m_vAncestorEvents.end()) && ((*next)->bIsStartElement))
1610cdf0e10cSrcweir 		{
1611cdf0e10cSrcweir 			next = checkAncestorStartElementEvent(next, xDocumentHandler);
1612cdf0e10cSrcweir 		}
1613cdf0e10cSrcweir 
1614cdf0e10cSrcweir 		if (next != m_vAncestorEvents.end())
1615cdf0e10cSrcweir 		{
1616cdf0e10cSrcweir 			next++;
1617cdf0e10cSrcweir 		}
1618cdf0e10cSrcweir 	}
1619cdf0e10cSrcweir 
1620cdf0e10cSrcweir 	return next;
1621cdf0e10cSrcweir }
1622cdf0e10cSrcweir 
flushAncestorEvents(const cssu::Reference<cssxs::XDocumentHandler> & xDocumentHandler)1623cdf0e10cSrcweir void XSecTester::flushAncestorEvents(
1624cdf0e10cSrcweir 	const cssu::Reference< cssxs::XDocumentHandler >& xDocumentHandler)
1625cdf0e10cSrcweir {
1626cdf0e10cSrcweir 	std::vector< AncestorEvent* >::const_iterator ii;
1627cdf0e10cSrcweir 
1628cdf0e10cSrcweir 	if (xDocumentHandler != NULL)
1629cdf0e10cSrcweir 	{
1630cdf0e10cSrcweir 		ii = m_vAncestorEvents.begin();
1631cdf0e10cSrcweir 
1632cdf0e10cSrcweir 		while (ii != m_vAncestorEvents.end())
1633cdf0e10cSrcweir 		{
1634cdf0e10cSrcweir 			AncestorEvent* ancestorEvent = *ii;
1635cdf0e10cSrcweir 
1636cdf0e10cSrcweir 			if (ancestorEvent->bIsStartElement)
1637cdf0e10cSrcweir 			{
1638cdf0e10cSrcweir 				ii = checkAncestorStartElementEvent(ii, xDocumentHandler);
1639cdf0e10cSrcweir 			}
1640cdf0e10cSrcweir 			else
1641cdf0e10cSrcweir 			{
1642cdf0e10cSrcweir 				sendAncestorEndElementEvent((*ii)->ouName, xDocumentHandler);
1643cdf0e10cSrcweir 				ii++;
1644cdf0e10cSrcweir 			}
1645cdf0e10cSrcweir 		}
1646cdf0e10cSrcweir 	}
1647cdf0e10cSrcweir 
1648cdf0e10cSrcweir 	/* free the ancestor events list */
1649cdf0e10cSrcweir 	std::vector< AncestorEvent* >::iterator jj;
1650cdf0e10cSrcweir 
1651cdf0e10cSrcweir 	while (m_vAncestorEvents.size()>0)
1652cdf0e10cSrcweir 	{
1653cdf0e10cSrcweir 		jj = m_vAncestorEvents.begin();
1654cdf0e10cSrcweir 		delete *jj;
1655cdf0e10cSrcweir 		m_vAncestorEvents.erase(jj);
1656cdf0e10cSrcweir 	}
1657cdf0e10cSrcweir }
1658cdf0e10cSrcweir 
1659cdf0e10cSrcweir /*
1660cdf0e10cSrcweir  * Get the length of a file in a platform independant fashion
1661cdf0e10cSrcweir  */
getLength(const char * pInputFileName)1662cdf0e10cSrcweir int getLength(const char *pInputFileName)
1663cdf0e10cSrcweir {
1664cdf0e10cSrcweir 	int nSize = 0;
1665cdf0e10cSrcweir 	std::ifstream data(pInputFileName);
1666cdf0e10cSrcweir 
1667cdf0e10cSrcweir 	data.seekg(0, std::ios_base::end);
1668cdf0e10cSrcweir 	nSize = data.tellg();
1669cdf0e10cSrcweir 
1670cdf0e10cSrcweir 	return nSize;
1671cdf0e10cSrcweir }
1672cdf0e10cSrcweir 
outputHeader()1673cdf0e10cSrcweir void outputHeader()
1674cdf0e10cSrcweir {
1675cdf0e10cSrcweir 	fprintf(stderr, "%16s%4s%8s%12s%12s%12s%12s\n", "File Name", "E/I", "Size", "-C++", "-Java", "Forw-O", "No S/E");
1676cdf0e10cSrcweir 	fprintf(stderr, "===============================================================================\n");
1677cdf0e10cSrcweir }
1678cdf0e10cSrcweir 
1679cdf0e10cSrcweir /*
1680cdf0e10cSrcweir  * print the output on the screen as well as in the GNUPlot data file
1681cdf0e10cSrcweir  */
output(const rtl::OUString & ouInputFileName,const rtl::OUString & ouTime_C,const rtl::OUString & ouTime_Java,const rtl::OUString & ouTime_NoSecurity,const rtl::OUString & ouTime_JavaForwardOnly,const rtl::OUString & ouRemark_C,const rtl::OUString & ouRemark_Java,bool bIsExporting)1682cdf0e10cSrcweir void output(const rtl::OUString& ouInputFileName,
1683cdf0e10cSrcweir 	const rtl::OUString& ouTime_C,
1684cdf0e10cSrcweir 	const rtl::OUString& ouTime_Java,
1685cdf0e10cSrcweir 	const rtl::OUString& ouTime_NoSecurity,
1686cdf0e10cSrcweir 	const rtl::OUString& ouTime_JavaForwardOnly,
1687cdf0e10cSrcweir 	const rtl::OUString& ouRemark_C,
1688cdf0e10cSrcweir 	const rtl::OUString& ouRemark_Java,
1689cdf0e10cSrcweir 	bool bIsExporting)
1690cdf0e10cSrcweir {
1691cdf0e10cSrcweir 	int nSize = getLength(rtl::OString(ouInputFileName, ouInputFileName.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1692cdf0e10cSrcweir 	std::ofstream data;
1693cdf0e10cSrcweir 
1694cdf0e10cSrcweir 	/* print screen */
1695cdf0e10cSrcweir 	int nPosition = ouInputFileName.lastIndexOf('\\');
1696cdf0e10cSrcweir 	rtl::OUString fileName = ouInputFileName.copy(nPosition + 1);
1697cdf0e10cSrcweir 
1698cdf0e10cSrcweir 	fprintf(stderr, "%16s", rtl::OString(fileName, fileName.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1699cdf0e10cSrcweir 
1700cdf0e10cSrcweir 	fprintf(stderr, "%4s", bIsExporting?"E":"I");
1701cdf0e10cSrcweir 	fprintf(stderr, "%7dK", nSize/1024);
1702cdf0e10cSrcweir 	fprintf(stderr, "%8s %3s",
1703cdf0e10cSrcweir 		rtl::OString(ouTime_C, ouTime_C.getLength(), RTL_TEXTENCODING_ASCII_US).getStr(),
1704cdf0e10cSrcweir 		rtl::OString(ouRemark_C, ouRemark_C.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1705cdf0e10cSrcweir 	fprintf(stderr, "%8s %3s",
1706cdf0e10cSrcweir 		rtl::OString(ouTime_Java, ouTime_Java.getLength(), RTL_TEXTENCODING_ASCII_US).getStr(),
1707cdf0e10cSrcweir 		rtl::OString(ouRemark_Java, ouRemark_Java.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1708cdf0e10cSrcweir 	fprintf(stderr, "%12s", rtl::OString(ouTime_JavaForwardOnly, ouTime_JavaForwardOnly.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1709cdf0e10cSrcweir 	fprintf(stderr, "%12s", rtl::OString(ouTime_NoSecurity, ouTime_NoSecurity.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1710cdf0e10cSrcweir 	fprintf(stderr, "\n");
1711cdf0e10cSrcweir 
1712cdf0e10cSrcweir 	/* output the data as GNUPlot data file */
1713cdf0e10cSrcweir 	/*
1714cdf0e10cSrcweir 	char str[32];
1715cdf0e10cSrcweir 	sprintf(str, "%d %s", nSize, rtl::OString(ouTime_C, ouTime_C.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1716cdf0e10cSrcweir 	data.open("d:\\time_vs_size.txt", std::ios::app);
1717cdf0e10cSrcweir 	data <<  str << std::endl;
1718cdf0e10cSrcweir 	data.close();
1719cdf0e10cSrcweir 
1720cdf0e10cSrcweir 	sprintf(str, "%d %s", nSize, rtl::OString(ouTime_Java, ouTime_Java.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1721cdf0e10cSrcweir 	data.open("d:\\time_vs_size_without_sec.txt", std::ios::app);
1722cdf0e10cSrcweir 	data <<  str << std::endl;
1723cdf0e10cSrcweir 	data.close();
1724cdf0e10cSrcweir 	*/
1725cdf0e10cSrcweir }
1726cdf0e10cSrcweir 
main(int argc,char ** argv)1727cdf0e10cSrcweir int main( int argc, char **argv )
1728cdf0e10cSrcweir {
1729cdf0e10cSrcweir 	if (argc < 3)
1730cdf0e10cSrcweir 	{
1731cdf0e10cSrcweir 		fprintf(stderr, "Usage: testtool <exportbatchfile> <importbatchfile> [<cppcryptotoken>] [<javacryptotoken>]\n");
1732cdf0e10cSrcweir 		exit (-1);
1733cdf0e10cSrcweir 	}
1734cdf0e10cSrcweir 
1735cdf0e10cSrcweir 	rtl::OUString aExportBatchFile = rtl::OUString::createFromAscii(argv[1]);
1736cdf0e10cSrcweir 	rtl::OUString aImportBatchFile = rtl::OUString::createFromAscii(argv[2]);
1737cdf0e10cSrcweir 	rtl::OUString aCPPCryptoToken;
1738cdf0e10cSrcweir 	if ( argc > 3 )
1739cdf0e10cSrcweir 	    aCPPCryptoToken = rtl::OUString::createFromAscii(argv[3]);
1740cdf0e10cSrcweir 	rtl::OUString aJavaCryptoToken;
1741cdf0e10cSrcweir 	if ( argc > 4 )
1742cdf0e10cSrcweir 	    aJavaCryptoToken = rtl::OUString::createFromAscii(argv[4]);
1743cdf0e10cSrcweir 
1744cdf0e10cSrcweir 	try
1745cdf0e10cSrcweir 	{
1746cdf0e10cSrcweir 		uno::Reference< lang::XMultiServiceFactory > xMSF = CreateDemoServiceFactory();
1747cdf0e10cSrcweir 
1748cdf0e10cSrcweir 		XSecTester* pTester = new XSecTester( xMSF );
1749cdf0e10cSrcweir 		uno::Reference< xml::sax::XDocumentHandler > xKeepARef = pTester;
1750cdf0e10cSrcweir 
1751cdf0e10cSrcweir 		pTester->setCryptoDir( aJavaCryptoToken, aCPPCryptoToken );
1752cdf0e10cSrcweir 
1753cdf0e10cSrcweir 		rtl::OUString ouTime_C, ouTime_Java, ouTime_NoSecurity, ouTime_JavaForwardOnly;
1754cdf0e10cSrcweir 		rtl::OUString ouInputFileName;
1755cdf0e10cSrcweir 		rtl::OUString outputFileName1;
1756cdf0e10cSrcweir 		rtl::OUString outputFileName2;
1757cdf0e10cSrcweir 		rtl::OUString ouRemark_C, ouRemark_Java;
1758cdf0e10cSrcweir 
1759cdf0e10cSrcweir 		outputHeader();
1760cdf0e10cSrcweir 
1761cdf0e10cSrcweir 		std::ifstream batch_export, batch_import;
1762cdf0e10cSrcweir 
1763cdf0e10cSrcweir 		batch_export.open(OUStringToOString( aExportBatchFile, RTL_TEXTENCODING_ASCII_US ).getStr());
1764cdf0e10cSrcweir 
1765cdf0e10cSrcweir 		const int MAX_LINE = 80;
1766cdf0e10cSrcweir 		char line[MAX_LINE + 1];
1767cdf0e10cSrcweir 
1768cdf0e10cSrcweir 		while (batch_export.getline(line, MAX_LINE))
1769cdf0e10cSrcweir 		{
1770cdf0e10cSrcweir 			ouInputFileName = rtl::OUString::createFromAscii(line);
1771cdf0e10cSrcweir 			int nPosition = ouInputFileName.lastIndexOf('.');
1772cdf0e10cSrcweir 			int nPosition1;
1773cdf0e10cSrcweir 
1774cdf0e10cSrcweir 			/*
1775cdf0e10cSrcweir 			 * export the file with signautre/encryption (C++)
1776cdf0e10cSrcweir 			 */
1777cdf0e10cSrcweir 			outputFileName1 = ouInputFileName.copy(0, nPosition) +
1778cdf0e10cSrcweir 				rtl::OUString::createFromAscii("-ex.xml");
1779cdf0e10cSrcweir 			ouTime_C = pTester->export_xml(ouInputFileName, outputFileName1, sal_False);
1780cdf0e10cSrcweir 			nPosition1 = ouTime_C.lastIndexOf('\t');
1781cdf0e10cSrcweir 			ouRemark_C = ouTime_C.copy(nPosition1 + 1);
1782cdf0e10cSrcweir 			ouTime_C = ouTime_C.copy(0, nPosition1);
1783cdf0e10cSrcweir 
1784cdf0e10cSrcweir 			/*
1785cdf0e10cSrcweir 			 * export the file with signautre/encryption (Java)
1786cdf0e10cSrcweir 			 */
1787cdf0e10cSrcweir 			outputFileName1 = ouInputFileName.copy(0, nPosition) +
1788cdf0e10cSrcweir 				rtl::OUString::createFromAscii("-ex2.xml");
1789cdf0e10cSrcweir 			ouTime_Java = pTester->export_xml(ouInputFileName, outputFileName1, sal_True);
1790cdf0e10cSrcweir 			nPosition1 = ouTime_Java.lastIndexOf('\t');
1791cdf0e10cSrcweir 			ouRemark_Java = ouTime_Java.copy(nPosition1 + 1);
1792cdf0e10cSrcweir 			ouTime_Java = ouTime_Java.copy(0, nPosition1);
1793cdf0e10cSrcweir 
1794cdf0e10cSrcweir 			/*
1795cdf0e10cSrcweir 			 * export the file without signautre/encryption
1796cdf0e10cSrcweir 			 */
1797cdf0e10cSrcweir 			outputFileName2 = ouInputFileName.copy(0, nPosition) +
1798cdf0e10cSrcweir 				rtl::OUString::createFromAscii("-ex-no.xml");
1799cdf0e10cSrcweir 			ouTime_NoSecurity = pTester->transfer_without_sec(ouInputFileName, outputFileName2, sal_False);
1800cdf0e10cSrcweir 
1801cdf0e10cSrcweir 			/*
1802cdf0e10cSrcweir 			 * export the file with Java Flat Filter
1803cdf0e10cSrcweir 			 */
1804cdf0e10cSrcweir 			outputFileName2 = ouInputFileName.copy(0, nPosition) +
1805cdf0e10cSrcweir 				rtl::OUString::createFromAscii("-ex-jf.xml");
1806cdf0e10cSrcweir 			ouTime_JavaForwardOnly = pTester->transfer_without_sec(ouInputFileName, outputFileName2, sal_True);
1807cdf0e10cSrcweir 
1808cdf0e10cSrcweir 			/*
1809cdf0e10cSrcweir 			 * print output
1810cdf0e10cSrcweir 			 */
1811cdf0e10cSrcweir 			output(ouInputFileName, ouTime_C, ouTime_Java, ouTime_NoSecurity, ouTime_JavaForwardOnly, ouRemark_C, ouRemark_Java, true);
1812cdf0e10cSrcweir 		}
1813cdf0e10cSrcweir 
1814cdf0e10cSrcweir 		batch_export.close();
1815cdf0e10cSrcweir 
1816cdf0e10cSrcweir 		batch_import.open(OUStringToOString( aImportBatchFile, RTL_TEXTENCODING_ASCII_US ).getStr());
1817cdf0e10cSrcweir 
1818cdf0e10cSrcweir 		while (batch_import.getline(line, MAX_LINE))
1819cdf0e10cSrcweir 		{
1820cdf0e10cSrcweir 			ouInputFileName = rtl::OUString::createFromAscii(line);
1821cdf0e10cSrcweir 			int nPosition = ouInputFileName.lastIndexOf('.');
1822cdf0e10cSrcweir 			int nPosition1;
1823cdf0e10cSrcweir 
1824cdf0e10cSrcweir 			/*
1825cdf0e10cSrcweir 			 * import the file with signautre/encryption (C++)
1826cdf0e10cSrcweir 			 */
1827cdf0e10cSrcweir 			outputFileName1 = ouInputFileName.copy(0, nPosition) +
1828cdf0e10cSrcweir 				rtl::OUString::createFromAscii("-im.xml");
1829cdf0e10cSrcweir 			ouTime_C = pTester->import_xml(ouInputFileName, outputFileName1, sal_False);
1830cdf0e10cSrcweir 			nPosition1 = ouTime_C.lastIndexOf('\t');
1831cdf0e10cSrcweir 			ouRemark_C = ouTime_C.copy(nPosition1 + 1);
1832cdf0e10cSrcweir 			ouTime_C = ouTime_C.copy(0, nPosition1);
1833cdf0e10cSrcweir 
1834cdf0e10cSrcweir 			/*
1835cdf0e10cSrcweir 			 * import the file with signautre/encryption (Java)
1836cdf0e10cSrcweir 			 */
1837cdf0e10cSrcweir 			outputFileName1 = ouInputFileName.copy(0, nPosition) +
1838cdf0e10cSrcweir 				rtl::OUString::createFromAscii("-im2.xml");
1839cdf0e10cSrcweir 			ouTime_Java = pTester->import_xml(ouInputFileName, outputFileName1, sal_True);
1840cdf0e10cSrcweir 			nPosition1 = ouTime_Java.lastIndexOf('\t');
1841cdf0e10cSrcweir 			ouRemark_Java = ouTime_Java.copy(nPosition1 + 1);
1842cdf0e10cSrcweir 			ouTime_Java = ouTime_Java.copy(0, nPosition1);
1843cdf0e10cSrcweir 
1844cdf0e10cSrcweir 			/*
1845cdf0e10cSrcweir 			 * import the file without signautre/encryption
1846cdf0e10cSrcweir 			 */
1847cdf0e10cSrcweir 			outputFileName2 = ouInputFileName.copy(0, nPosition) +
1848cdf0e10cSrcweir 				rtl::OUString::createFromAscii("-im-no.xml");
1849cdf0e10cSrcweir 			ouTime_NoSecurity = pTester->transfer_without_sec(ouInputFileName, outputFileName2, sal_False);
1850cdf0e10cSrcweir 
1851cdf0e10cSrcweir 			/*
1852cdf0e10cSrcweir 			 * import the file without signautre/encryption
1853cdf0e10cSrcweir 			 */
1854cdf0e10cSrcweir 
1855cdf0e10cSrcweir 			outputFileName2 = ouInputFileName.copy(0, nPosition) +
1856cdf0e10cSrcweir 				rtl::OUString::createFromAscii("-im-jf.xml");
1857cdf0e10cSrcweir 			ouTime_JavaForwardOnly = pTester->transfer_without_sec(ouInputFileName, outputFileName2, sal_True);
1858cdf0e10cSrcweir 
1859cdf0e10cSrcweir 			/*
1860cdf0e10cSrcweir 			 * print output
1861cdf0e10cSrcweir 			 */
1862cdf0e10cSrcweir 			output(ouInputFileName, ouTime_C, ouTime_Java, ouTime_NoSecurity, ouTime_JavaForwardOnly, ouRemark_C, ouRemark_Java, false);
1863cdf0e10cSrcweir 		}
1864cdf0e10cSrcweir 
1865cdf0e10cSrcweir 		batch_import.close();
1866cdf0e10cSrcweir 
1867cdf0e10cSrcweir 		fprintf(stderr, "\n");
1868cdf0e10cSrcweir 	}
1869cdf0e10cSrcweir 	catch( cssu::Exception& e )
1870cdf0e10cSrcweir 	{
1871cdf0e10cSrcweir 		fprintf( stderr , "\nEXCEPTION! Error Message: %s\n" ,
1872cdf0e10cSrcweir 				 rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
1873cdf0e10cSrcweir 	}
1874cdf0e10cSrcweir 
1875cdf0e10cSrcweir 	return 0;
1876cdf0e10cSrcweir }
1877