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