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