xref: /trunk/main/xmlsecurity/tools/demo/performance.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 
395     /* XSignatureVerifyResultListener */
396     virtual void SAL_CALL signatureVerified(
397         sal_Int32 securityId,
398         com::sun::star::xml::crypto::SecurityOperationStatus verifyResult );
399 
400     /* XSAXEventKeeperStatusChangeListener */
401     virtual void SAL_CALL blockingStatusChanged( sal_Bool isBlocking );
402     virtual void SAL_CALL collectionStatusChanged(
403         sal_Bool isInsideCollectedElement );
404     virtual void SAL_CALL bufferStatusChanged( sal_Bool isBufferEmpty );
405 
406     /* XXMLSecTester */
407     virtual rtl::OUString SAL_CALL transfer_without_sec(
408         const rtl::OUString& inputFileName,
409         const rtl::OUString& outputFileName,
410         sal_Bool isBridgeInvolved);
411     virtual rtl::OUString SAL_CALL export_xml(
412         const rtl::OUString& inputFileName,
413         const rtl::OUString& outputFileName,
414         sal_Bool isJavaBased);
415     virtual rtl::OUString SAL_CALL import_xml(
416         const rtl::OUString& inputFileName,
417         const rtl::OUString& outputFileName,
418         sal_Bool isJavaBased);
419 
420     virtual void SAL_CALL setCryptoDir(
421         const rtl::OUString & javaDirName,
422         const rtl::OUString & cDirName);
423 
424     /* XDocumentHandler */
425     virtual void SAL_CALL endDocument();
426     virtual void SAL_CALL startDocument();
427     virtual void SAL_CALL characters(const class rtl::OUString&);
428     virtual void SAL_CALL processingInstruction(const rtl::OUString&, const rtl::OUString&);
429     virtual void SAL_CALL ignorableWhitespace(const rtl::OUString&);
430     virtual void SAL_CALL startElement(
431         const rtl::OUString&,
432         const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList >&);
433     virtual void SAL_CALL endElement(const rtl::OUString&);
434     virtual void SAL_CALL setDocumentLocator(
435         const com::sun::star::uno::Reference< com::sun::star::xml::sax::XLocator >&);
436 };
437 
parseFile(const rtl::OUString & ouInputFileName,const rtl::OUString & ouOutputFileName,bool bIsExporting,bool bIsJavaBased)438 rtl::OUString XSecTester::parseFile(
439     const rtl::OUString& ouInputFileName,
440     const rtl::OUString& ouOutputFileName,
441     bool bIsExporting,
442     bool bIsJavaBased)
443 {
444     rtl::OUString ouMessage;
445 
446     cssu::Reference<cssi::XInputStream> xInputStream = OpenInputStream(ouInputFileName);
447 
448     if (xInputStream != NULL )
449     {
450         /* initialization */
451         rtl::OUString SEInitializer_comp;
452         rtl::OUString XMLSignature_comp;
453         rtl::OUString tokenPath;
454         cssu::Reference < cssxc::XSEInitializer > xSEInitializer;
455 
456         if (bIsJavaBased)
457         {
458             SEInitializer_comp = rtl::OUString::createFromAscii( SEINITIALIZER_JAVA_COMPONENT );
459             XMLSignature_comp = rtl::OUString::createFromAscii( XMLSIGNATURE_JAVA_COMPONENT);
460             m_ouXMLDocumentWrapperComponentName = rtl::OUString::createFromAscii( XMLDOCUMENTWRAPPER_JAVA_COMPONENT );
461             tokenPath = m_ouJavaCryptokenDir;
462         }
463         else
464         {
465             SEInitializer_comp = rtl::OUString::createFromAscii( SEINITIALIZER_C_COMPONENT );
466             XMLSignature_comp = rtl::OUString::createFromAscii( XMLSIGNATURE_C_COMPONENT);
467             m_ouXMLDocumentWrapperComponentName = rtl::OUString::createFromAscii( XMLDOCUMENT_C_COMPONENT );
468             tokenPath = m_ouCCryptokenDir;
469         }
470 
471         xSEInitializer = cssu::Reference < cssxc::XSEInitializer > (
472              mxMSF->createInstance( SEInitializer_comp ),
473              cssu::UNO_QUERY );
474 
475         m_xXMLSignature = cssu::Reference<cssxc::XXMLSignature> (
476             mxMSF->createInstance( XMLSignature_comp ),
477             cssu::UNO_QUERY );
478 
479         if ( xSEInitializer.is() && m_xXMLSignature.is())
480         {
481             /* create SAX Parser */
482             const rtl::OUString sSaxParser (
483                 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser") );
484             m_xSaxParser = cssu::Reference < cssxs::XParser > ( mxMSF->createInstance( sSaxParser ), cssu::UNO_QUERY );
485 
486             /* create SAX Writer */
487             const rtl::OUString sSaxWriter (
488                 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer") );
489             cssu::Reference < cssi::XActiveDataSource > xSaxWriter
490                 ( mxMSF->createInstance( sSaxWriter ), cssu::UNO_QUERY );
491 
492             cssu::Reference< cssi::XOutputStream > xOutputStream = OpenOutputStream(ouOutputFileName);
493             xSaxWriter->setOutputStream( xOutputStream );
494 
495             cssxs::InputSource aInput;
496             aInput.sSystemId = ouInputFileName;
497             aInput.aInputStream = xInputStream;
498 
499             cssu::Reference < cssxs::XDocumentHandler > xSaxWriterHandler( xSaxWriter, cssu::UNO_QUERY);
500 
501             m_xXMLSecurityContext =
502                 xSEInitializer->createSecurityContext(tokenPath);
503 
504             m_bIsExporting = bIsExporting;
505             m_xExportHandler = xSaxWriterHandler;
506             m_xOutputHandler = xSaxWriterHandler;
507 
508             m_xXMLDocumentWrapper = NULL;
509             m_xSAXEventKeeper = NULL;
510             m_bIsSAXEventKeeperOnTheSAXChain = false;
511 
512             m_bIsBlocking = false;
513             m_bIsInsideCollectedElement = false;
514 
515             OSL_ASSERT(m_vSignatureList.size() == 0);
516             OSL_ASSERT(m_vUnsolvedReferenceURIs.size() == 0);
517             OSL_ASSERT(m_vUnsolvedReferenceKeeperIds.size() == 0);
518             OSL_ASSERT(m_vUnsolvedReferenceRefNums.size() == 0);
519             OSL_ASSERT(m_stCurrentPath.empty());
520             OSL_ASSERT(m_stCurrentPathType.empty());
521             OSL_ASSERT(m_vAncestorEvents.empty());
522 
523             changeOutput();
524 
525             /* foundSecurityRelated(); */
526 
527             /* Begin to parse */
528             TimeValue startTime, endTime;
529             osl_getSystemTime( &startTime );
530 
531             xSaxWriterHandler->startDocument();
532 
533             if (m_bIsExporting)
534             {
535                 m_xSaxParser->setDocumentHandler(this);
536                 m_xSaxParser->parseStream(aInput);
537             }
538             else
539             {
540                 m_xSaxParser->setDocumentHandler(this);
541                 m_xSaxParser->parseStream(aInput);
542             }
543 
544             endMission();
545             xSaxWriterHandler->endDocument();
546 
547             osl_getSystemTime( &endTime );
548 
549             flushAncestorEvents( NULL );
550 
551             // Bug in SAXWriter, done in endDocument()
552             // xOutputStream->closeOutput();
553             xInputStream->closeInput();
554 
555 
556             /*
557              * Free the security context
558              */
559             xSEInitializer->freeSecurityContext(m_xXMLSecurityContext);
560             m_xXMLSecurityContext = NULL;
561 
562             /* Calculate the time */
563             double diff = ((double)((endTime.Nanosec + endTime.Seconds*1000000000.0)
564                     - (startTime.Nanosec + startTime.Seconds*1000000000.0))) /
565                 ((double)1000000000.0);
566 
567             char buf[32];
568             sprintf(buf, "%.2f", diff);
569             ouMessage += rtl::OUString(RTL_ASCII_USTRINGPARAM(buf));
570         }
571         else
572         {
573             ouMessage += rtl::OUString::createFromAscii( "N/A" );
574         }
575 
576     }
577     else
578     {
579         ouMessage += rtl::OUString::createFromAscii( "-" );
580     }
581 
582     return ouMessage;
583 }
584 
585 /* XSignatureCreationResultListener */
signatureCreated(sal_Int32 securityId,cssxc::SecurityOperationStatus creationResult)586 void SAL_CALL XSecTester::signatureCreated(
587     sal_Int32 securityId,
588     cssxc::SecurityOperationStatus creationResult )
589 {
590     m_nTotalSignatureNumber++;
591     if (creationResult == cssxc::SecurityOperationStatus_OPERATION_SUCCEEDED)
592     {
593         m_nSuccessfulSignatureNumber++;
594     }
595 }
596 
597 /* XSignatureVerifyResultListener */
signatureVerified(sal_Int32 securityId,cssxc::SecurityOperationStatus verifyResult)598 void SAL_CALL XSecTester::signatureVerified(
599     sal_Int32 securityId,
600     cssxc::SecurityOperationStatus verifyResult )
601 {
602     m_nTotalSignatureNumber++;
603     if (verifyResult == cssxc::SecurityOperationStatus_OPERATION_SUCCEEDED)
604     {
605         m_nSuccessfulSignatureNumber++;
606     }
607 }
608 
609 /* XSAXEventKeeperStatusChangeListener */
blockingStatusChanged(sal_Bool isBlocking)610 void SAL_CALL XSecTester::blockingStatusChanged( sal_Bool isBlocking )
611 {
612     this->m_bIsBlocking = isBlocking;
613 }
614 
collectionStatusChanged(sal_Bool isInsideCollectedElement)615 void SAL_CALL XSecTester::collectionStatusChanged( sal_Bool isInsideCollectedElement )
616 {
617     this->m_bIsInsideCollectedElement = isInsideCollectedElement;
618 
619     if ( !m_bIsInsideCollectedElement && !m_bIsBlocking)
620     {
621         m_bIsSAXEventKeeperOnTheSAXChain = false;
622     }
623     else
624     {
625         m_bIsSAXEventKeeperOnTheSAXChain = true;
626     }
627     changeOutput();
628 }
629 
bufferStatusChanged(sal_Bool isBufferEmpty)630 void SAL_CALL XSecTester::bufferStatusChanged( sal_Bool isBufferEmpty )
631 {
632     if (isBufferEmpty)
633     {
634         m_xXMLDocumentWrapper = NULL;
635 
636         m_xSAXEventKeeper = NULL;
637         m_bIsSAXEventKeeperOnTheSAXChain = false;
638         changeOutput();
639     }
640 }
641 
642 /* XXMLSecTester */
export_xml(const rtl::OUString & inputFileName,const rtl::OUString & outputFileName,sal_Bool isJavaBased)643 rtl::OUString SAL_CALL XSecTester::export_xml( const rtl::OUString& inputFileName, const rtl::OUString& outputFileName, sal_Bool isJavaBased)
644 {
645     rtl::OUString ouMessage;
646 
647     m_nTotalSignatureNumber = 0;
648     m_nSuccessfulSignatureNumber = 0;
649 
650     ouMessage += parseFile(inputFileName, outputFileName, sal_True, isJavaBased);
651 
652     rtl::OUString ouRemark = rtl::OUString::valueOf(m_nSuccessfulSignatureNumber) +
653         rtl::OUString(RTL_ASCII_USTRINGPARAM( "/" ))
654         + rtl::OUString::valueOf(m_nTotalSignatureNumber);
655     ouMessage += rtl::OUString(RTL_ASCII_USTRINGPARAM("\t")) + ouRemark;
656 
657     return ouMessage;
658 }
659 
import_xml(const rtl::OUString & inputFileName,const rtl::OUString & outputFileName,sal_Bool isJavaBased)660 rtl::OUString SAL_CALL XSecTester::import_xml( const rtl::OUString& inputFileName, const rtl::OUString& outputFileName, sal_Bool isJavaBased)
661 {
662     rtl::OUString ouMessage;
663 
664     m_nTotalSignatureNumber = 0;
665     m_nSuccessfulSignatureNumber = 0;
666 
667     ouMessage += parseFile(inputFileName, outputFileName, sal_False, isJavaBased);
668 
669     rtl::OUString ouRemark = rtl::OUString::valueOf(m_nSuccessfulSignatureNumber) +
670         rtl::OUString(RTL_ASCII_USTRINGPARAM( "/" ))
671         + rtl::OUString::valueOf(m_nTotalSignatureNumber);
672     ouMessage += rtl::OUString(RTL_ASCII_USTRINGPARAM("\t")) + ouRemark;
673 
674     return ouMessage;
675 }
676 
transfer_without_sec(const rtl::OUString & inputFileName,const rtl::OUString & outputFileName,sal_Bool isBridgeInvolved)677 rtl::OUString SAL_CALL XSecTester::transfer_without_sec(
678     const rtl::OUString& inputFileName,
679     const rtl::OUString& outputFileName,
680     sal_Bool isBridgeInvolved)
681 {
682     rtl::OUString ouMessage;
683 
684     cssu::Reference< cssi::XInputStream > xInputStream = OpenInputStream(inputFileName);
685 
686     if (xInputStream != NULL )
687     {
688         /* create SAX Parser */
689         const rtl::OUString sSaxParser (
690             RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser") );
691         m_xSaxParser = cssu::Reference < cssxs::XParser > ( mxMSF->createInstance( sSaxParser ), cssu::UNO_QUERY );
692 
693         /* create SAX Writer */
694         const rtl::OUString sSaxWriter (
695             RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer") );
696         cssu::Reference < cssi::XActiveDataSource > xSaxWriter
697             ( mxMSF->createInstance( sSaxWriter ), cssu::UNO_QUERY );
698         cssu::Reference < cssxs::XDocumentHandler > xSaxWriterHandler(
699             xSaxWriter, cssu::UNO_QUERY);
700 
701         if (!isBridgeInvolved)
702         {
703             /* connect the SAX Parser and the SAX Writer */
704             m_xSaxParser->setDocumentHandler ( xSaxWriterHandler );
705         }
706         else
707         {
708             /* create Java Flat Filter */
709             const rtl::OUString sJavaFlatFilter(
710                 RTL_CONSTASCII_USTRINGPARAM( JAVAFLATFILTER_COMPONENT ) );
711             cssu::Reference < cssxs::XParser > xJavaFilterParser
712                 ( mxMSF->createInstance( sJavaFlatFilter ), cssu::UNO_QUERY );
713             cssu::Reference < cssxs::XDocumentHandler > xJavaFilterHandler(
714                 xJavaFilterParser, cssu::UNO_QUERY );
715 
716             if ( !xJavaFilterParser.is() )
717                 return rtl::OUString::createFromAscii( "NO JAVA" );
718 
719             /* connect the SAX Parser, the Java Flat Filter and the SAX Writer */
720             xJavaFilterParser->setDocumentHandler( xSaxWriterHandler );
721             m_xSaxParser->setDocumentHandler ( xJavaFilterHandler );
722         }
723 
724 
725         /* set output stream */
726         cssu::Reference< cssi::XOutputStream > xOutputStream =
727                 OpenOutputStream(outputFileName);
728         xSaxWriter->setOutputStream( xOutputStream );
729 
730         /* prepare input stream */
731         cssxs::InputSource aInput;
732         aInput.sSystemId = inputFileName;
733         aInput.aInputStream = xInputStream;
734 
735         TimeValue startTime, endTime;
736         osl_getSystemTime( &startTime );
737 
738         m_xSaxParser->parseStream ( aInput );
739 
740         // xOutputStream->closeOutput();
741         xInputStream->closeInput();
742 
743         osl_getSystemTime( &endTime );
744 
745         double diff = ((double)((endTime.Nanosec + endTime.Seconds*1000000000.0)
746                  - (startTime.Nanosec + startTime.Seconds*1000000000.0)))/((double)1000000000.0);
747         char buf[32];
748         sprintf(buf, "%.2f", diff);
749         ouMessage += rtl::OUString(RTL_ASCII_USTRINGPARAM(buf));
750     }
751 
752     return ouMessage;
753 }
754 
setCryptoDir(const rtl::OUString & javaDirName,const rtl::OUString & cDirName)755 void SAL_CALL XSecTester::setCryptoDir(const rtl::OUString & javaDirName, const rtl::OUString & cDirName)
756 {
757     m_ouJavaCryptokenDir = javaDirName;
758     m_ouCCryptokenDir = cDirName;
759 }
760 
761 
XSecTester_createInstance(const cssu::Reference<cssl::XMultiServiceFactory> & rSMgr)762 cssu::Reference< cssu::XInterface > SAL_CALL XSecTester_createInstance(
763     const cssu::Reference< cssl::XMultiServiceFactory > & rSMgr)
764 {
765     return (cppu::OWeakObject*) new XSecTester( rSMgr );
766 }
767 
768 int SecurityEntity::m_nNextSecurityId = 1;
769 
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)770 SecurityEntity::SecurityEntity(
771     const cssu::Reference<cssxc::sax::XSecuritySAXEventKeeper>& xSAXEventKeeper,
772     const cssu::Reference<cssxc::XXMLSecurityContext>& xXMLSecurityContext,
773     const cssu::Reference<cssxc::XXMLSignature>& xXMLSignature,
774     const cssu::Reference< cssl::XMultiServiceFactory > &rsMSF)
775     :m_xSAXEventKeeper(xSAXEventKeeper),
776      m_xXMLSecurityContext(xXMLSecurityContext),
777      m_xXMLSignature(xXMLSignature),
778      mxMSF(rsMSF),
779      m_ouKeyURI(RTL_ASCII_USTRINGPARAM(""))
780 {
781     m_nSecurityId = getNextSecurityId();
782 }
783 
getNextSecurityId() const784 int SecurityEntity::getNextSecurityId() const
785 {
786     int nId = m_nNextSecurityId++;
787     return nId;
788 }
789 
setKeyId(int nId)790 void SecurityEntity::setKeyId(int nId)
791 {
792     cssu::Reference<cssxc::sax::XKeyCollector> keyCollector (m_xReferenceListener, cssu::UNO_QUERY);
793     keyCollector->setKeyId(nId);
794 }
795 
796 
setKeyURI(const rtl::OUString & ouUri)797 void SecurityEntity::setKeyURI(const rtl::OUString& ouUri)
798 {
799     m_ouKeyURI = ouUri;
800 }
801 
getReferenceListener() const802 cssu::Reference<cssxc::sax::XReferenceResolvedListener> SecurityEntity::getReferenceListener() const
803 {
804     return m_xReferenceListener;
805 }
806 
getSecurityId() const807 int SecurityEntity::getSecurityId() const
808 {
809     return m_nSecurityId;
810 }
811 
setKey(const rtl::OUString & ouUri,bool bIsExporting)812 bool SecurityEntity::setKey(const rtl::OUString& ouUri, bool bIsExporting)
813 {
814     bool rc = false;
815 
816     if (m_ouKeyURI != rtl::OUString(RTL_ASCII_USTRINGPARAM("")) &&
817         m_ouKeyURI == ouUri)
818     {
819         int nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
820             bIsExporting ?
821             (cssxc::sax::ElementMarkPriority_BEFOREMODIFY):
822             (cssxc::sax::ElementMarkPriority_AFTERMODIFY),
823             true);
824 
825         setKeyId(nKeeperId);
826         m_xSAXEventKeeper->setSecurityId(nKeeperId, m_nSecurityId);
827 
828         cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
829             (m_xSAXEventKeeper, cssu::UNO_QUERY);
830         xReferenceResolvedBroadcaster->addReferenceResolvedListener(nKeeperId,
831             m_xReferenceListener);
832 
833         rc = true;
834     }
835 
836     return rc;
837 }
838 
endMission()839 bool SecurityEntity::endMission()
840 {
841     cssu::Reference<cssxc::sax::XMissionTaker> xMissionTaker
842         (m_xReferenceListener, cssu::UNO_QUERY);
843 
844     return xMissionTaker->endMission();
845 }
846 
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)847 SignatureEntity::SignatureEntity(
848     const cssu::Reference<cssxc::sax::XSecuritySAXEventKeeper>& xSAXEventKeeper,
849     bool bIsExporting,
850     XSecTester* pListener,
851     const cssu::Reference<cssxc::XXMLSecurityContext>& xXMLSecurityContext,
852     const cssu::Reference<cssxc::XXMLSignature>& xXMLSignature,
853     const cssu::Reference< cssl::XMultiServiceFactory >& rsMSF)
854     :SecurityEntity(xSAXEventKeeper,
855             xXMLSecurityContext,
856             xXMLSignature,
857             rsMSF)
858 {
859     if (bIsExporting)
860     {
861         m_nSignatureElementCollectorId =
862             m_xSAXEventKeeper->addSecurityElementCollector(
863                 cssxc::sax::ElementMarkPriority_AFTERMODIFY,
864                 true);
865 
866         m_xSAXEventKeeper->setSecurityId(m_nSignatureElementCollectorId, m_nSecurityId);
867 
868         m_xReferenceListener = cssu::Reference< cssxc::sax::XReferenceResolvedListener >(
869             mxMSF->createInstance( rtl::OUString::createFromAscii( SIGNATURECREATOR_COMPONENT )),
870             cssu::UNO_QUERY);
871 
872         cssu::Reference<cssl::XInitialization> xInitialization(m_xReferenceListener, cssu::UNO_QUERY);
873 
874         cssu::Sequence<cssu::Any> args(5);
875         char buf[16];
876 
877         sprintf(buf, "%d", m_nSecurityId);
878         args[0] = cssu::makeAny(rtl::OUString(RTL_ASCII_USTRINGPARAM(buf)));
879         args[1] = cssu::makeAny(m_xSAXEventKeeper);
880 
881         sprintf(buf, "%d", m_nSignatureElementCollectorId);
882         args[2] = cssu::makeAny(rtl::OUString(RTL_ASCII_USTRINGPARAM(buf)));
883         args[3] = cssu::makeAny(m_xXMLSecurityContext->getSecurityEnvironment());
884         args[4] = cssu::makeAny(m_xXMLSignature);
885 
886         xInitialization->initialize(args);
887 
888         int nBlockerId = m_xSAXEventKeeper->addBlocker();
889         m_xSAXEventKeeper->setSecurityId(nBlockerId, m_nSecurityId);
890 
891         cssu::Reference<cssxc::sax::XBlockerMonitor> xBlockerMonitor(m_xReferenceListener, cssu::UNO_QUERY);
892         xBlockerMonitor->setBlockerId(nBlockerId);
893 
894         cssu::Reference< cssxc::sax::XSignatureCreationResultBroadcaster > xSignatureCreationResultBroadcaster
895             (m_xReferenceListener, cssu::UNO_QUERY);
896         xSignatureCreationResultBroadcaster->addSignatureCreationResultListener(pListener);
897     }
898     else
899     {
900         m_nSignatureElementCollectorId =
901             m_xSAXEventKeeper->addSecurityElementCollector(
902                 cssxc::sax::ElementMarkPriority_BEFOREMODIFY,
903                 false);
904 
905         m_xSAXEventKeeper->setSecurityId(m_nSignatureElementCollectorId, m_nSecurityId);
906 
907         m_xReferenceListener = cssu::Reference< cssxc::sax::XReferenceResolvedListener >(
908             mxMSF->createInstance( rtl::OUString::createFromAscii( SIGNATUREVERIFIER_COMPONENT )),
909             cssu::UNO_QUERY);
910 
911         cssu::Reference<cssl::XInitialization> xInitialization(m_xReferenceListener, cssu::UNO_QUERY);
912 
913         cssu::Sequence<cssu::Any> args(5);
914         char buf[16];
915 
916         sprintf(buf, "%d", m_nSecurityId);
917         args[0] = cssu::makeAny(rtl::OUString(RTL_ASCII_USTRINGPARAM(buf)));
918         args[1] = cssu::makeAny(m_xSAXEventKeeper);
919 
920         sprintf(buf, "%d", m_nSignatureElementCollectorId);
921         args[2] = cssu::makeAny(rtl::OUString(RTL_ASCII_USTRINGPARAM(buf)));
922         args[3] = cssu::makeAny(m_xXMLSecurityContext);
923         args[4] = cssu::makeAny(m_xXMLSignature);
924         xInitialization->initialize(args);
925 
926         cssu::Reference< cssxc::sax::XSignatureVerifyResultBroadcaster > xSignatureVerifyResultBroadcaster
927             (m_xReferenceListener, cssu::UNO_QUERY);
928         xSignatureVerifyResultBroadcaster->addSignatureVerifyResultListener(pListener);
929     }
930 
931     cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
932         (m_xSAXEventKeeper, cssu::UNO_QUERY);
933     xReferenceResolvedBroadcaster->addReferenceResolvedListener(
934         m_nSignatureElementCollectorId, m_xReferenceListener);
935 }
936 
addReferenceURI(const rtl::OUString & ouUri)937 void SignatureEntity::addReferenceURI(const rtl::OUString& ouUri)
938 {
939     m_vReferenceIds.push_back(ouUri);
940 }
941 
setReferenceNumber() const942 void SignatureEntity::setReferenceNumber() const
943 {
944     cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
945         (m_xReferenceListener, cssu::UNO_QUERY);
946     xReferenceCollector->setReferenceCount(m_vReferenceIds.size());
947 }
948 
hasReference(const rtl::OUString & ouUri) const949 bool SignatureEntity::hasReference(const rtl::OUString& ouUri) const
950 {
951     bool rc = false;
952 
953     std::vector<const rtl::OUString>::const_iterator ii;
954     for (ii = m_vReferenceIds.begin(); ii != m_vReferenceIds.end(); ++ii)
955     {
956         if (ouUri == *ii)
957         {
958             rc = true;
959             break;
960         }
961     }
962 
963     return rc;
964 }
965 
setReference(const rtl::OUString & ouUri,bool bIsExporting) const966 bool SignatureEntity::setReference(const rtl::OUString& ouUri, bool bIsExporting) const
967 {
968     bool rc = false;
969 
970     if (hasReference(ouUri))
971     {
972         int nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
973                 bIsExporting ?
974                 (cssxc::sax::ElementMarkPriority_AFTERMODIFY):
975                 (cssxc::sax::ElementMarkPriority_BEFOREMODIFY),
976                 false);
977 
978         m_xSAXEventKeeper->setSecurityId(nKeeperId, m_nSecurityId);
979 
980         cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
981             (m_xSAXEventKeeper, cssu::UNO_QUERY);
982         xReferenceResolvedBroadcaster->addReferenceResolvedListener(nKeeperId, m_xReferenceListener);
983 
984         cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
985             (m_xReferenceListener, cssu::UNO_QUERY);
986         xReferenceCollector->setReferenceId(nKeeperId);
987 
988         rc = true;
989     }
990 
991     return rc;
992 }
993 
994 /* XDocumentHandler */
startDocument()995 void SAL_CALL XSecTester::startDocument()
996 {
997 }
998 
endDocument()999 void SAL_CALL XSecTester::endDocument()
1000 {
1001 }
1002 
characters(const class rtl::OUString & chars)1003 void SAL_CALL XSecTester::characters(const class rtl::OUString & chars)
1004 {
1005     m_xExportHandler->characters(chars);
1006 }
1007 
processingInstruction(const rtl::OUString & target,const rtl::OUString & data)1008 void SAL_CALL XSecTester::processingInstruction(const rtl::OUString & target, const rtl::OUString &data)
1009 {
1010     m_xExportHandler->processingInstruction(target, data);
1011 }
1012 
ignorableWhitespace(const rtl::OUString &)1013 void SAL_CALL XSecTester::ignorableWhitespace(const rtl::OUString &)
1014 {
1015 
1016 }
1017 
startElement(const rtl::OUString & name,const cssu::Reference<cssxs::XAttributeList> & xAttribs)1018 void SAL_CALL XSecTester::startElement(const rtl::OUString & name, const cssu::Reference<cssxs::XAttributeList> &xAttribs)
1019 {
1020     rtl::OUString ouIdAttr = xAttribs->getValueByName(
1021         rtl::OUString(RTL_ASCII_USTRINGPARAM("id")));
1022 
1023     if (ouIdAttr == NULL)
1024     {
1025         ouIdAttr = xAttribs->getValueByName(
1026             rtl::OUString(RTL_ASCII_USTRINGPARAM("Id")));
1027     }
1028 
1029     bool bHasIdAttr = (ouIdAttr != NULL && ouIdAttr.getLength() > 0 );
1030     bool needResend = false;
1031 
1032     if (bHasIdAttr || name.equalsAscii( SIGNATURE_STR ))
1033     {
1034         if (foundSecurityRelated() && ! m_bIsExporting)
1035         {
1036             needResend = true;
1037         }
1038     }
1039 
1040     if ( !m_bIsSAXEventKeeperOnTheSAXChain )
1041     {
1042         addStartAncestorEvent(name, xAttribs);
1043     }
1044 
1045     bool bSuppressingForwarding = checkSecurityElement(name, xAttribs);
1046 
1047     checkReference(name, xAttribs, ouIdAttr);
1048 
1049     if (needResend)
1050     {
1051         m_xSAXEventKeeper->setNextHandler(NULL);
1052 
1053         cssu::Reference<cssxs::XDocumentHandler> xSAXEventKeeperHandler
1054             (m_xSAXEventKeeper, cssu::UNO_QUERY);
1055 
1056         xSAXEventKeeperHandler->startElement(name, xAttribs);
1057         m_xSAXEventKeeper->setNextHandler(this);
1058     }
1059 
1060     if (!bSuppressingForwarding)
1061     {
1062         m_xExportHandler->startElement(name, xAttribs);
1063     }
1064 }
1065 
endElement(const rtl::OUString & name)1066 void SAL_CALL XSecTester::endElement(const rtl::OUString& name)
1067 {
1068     if (!m_stCurrentPath.empty())
1069     {
1070         void* pSignedInfo = m_stCurrentPath.top();
1071         bool bIsStringType = m_stCurrentPathType.top();
1072 
1073         m_stCurrentPath.pop();
1074         m_stCurrentPathType.pop();
1075 
1076         if (bIsStringType && !strcmp((const char *)pSignedInfo, SIGNEDINFO_STR))
1077         {
1078             if (!m_stCurrentPath.empty())
1079             {
1080                 void* pSignature = m_stCurrentPath.top();
1081                 bIsStringType = m_stCurrentPathType.top();
1082 
1083                 if (!bIsStringType && pSignature != NULL)
1084                 {
1085                     ((SignatureEntity *) pSignature)->setReferenceNumber();
1086                 }
1087             }
1088         }
1089     }
1090 
1091     if ( !m_bIsSAXEventKeeperOnTheSAXChain )
1092     {
1093         addEndAncestorEvent(name);
1094     }
1095 
1096     m_xExportHandler->endElement(name);
1097 }
1098 
setDocumentLocator(const cssu::Reference<cssxs::XLocator> &)1099 void SAL_CALL XSecTester::setDocumentLocator( const cssu::Reference<cssxs::XLocator>& )
1100 {
1101 }
1102 
changeOutput()1103 void XSecTester::changeOutput()
1104 {
1105     if (m_bIsExporting)
1106     {
1107         if (m_bIsSAXEventKeeperOnTheSAXChain)
1108         {
1109             m_xExportHandler = cssu::Reference<cssxs::XDocumentHandler>
1110                 (m_xSAXEventKeeper, cssu::UNO_QUERY);
1111 
1112             m_xSAXEventKeeper->setNextHandler(NULL);
1113 
1114             flushAncestorEvents(m_xExportHandler);
1115 
1116             m_xSAXEventKeeper->setNextHandler(m_xOutputHandler);
1117         }
1118         else
1119         {
1120             m_xExportHandler = m_xOutputHandler;
1121         }
1122     }
1123     else
1124     {
1125         if (m_bIsSAXEventKeeperOnTheSAXChain)
1126         {
1127             cssu::Reference<cssxs::XDocumentHandler> xSAXEventKeeperHandler
1128                 (m_xSAXEventKeeper, cssu::UNO_QUERY);
1129 
1130             m_xSAXEventKeeper->setNextHandler(NULL);
1131 
1132             flushAncestorEvents(xSAXEventKeeperHandler);
1133 
1134             m_xSaxParser->setDocumentHandler(xSAXEventKeeperHandler);
1135             m_xSAXEventKeeper->setNextHandler(this);
1136         }
1137         else
1138         {
1139             m_xSaxParser->setDocumentHandler(this);
1140         }
1141 
1142     }
1143 }
1144 
foundSecurityRelated()1145 bool XSecTester::foundSecurityRelated()
1146 {
1147     if (m_xSAXEventKeeper == NULL)
1148     {
1149         m_bIsBlocking = false;
1150         m_bIsInsideCollectedElement = false;
1151 
1152         m_xXMLDocumentWrapper = cssu::Reference<cssxw::XXMLDocumentWrapper>
1153             (mxMSF->createInstance( m_ouXMLDocumentWrapperComponentName ),
1154              cssu::UNO_QUERY);
1155 
1156         m_xSAXEventKeeper = cssu::Reference< cssxc::sax::XSecuritySAXEventKeeper >
1157             (mxMSF->createInstance( rtl::OUString::createFromAscii( SAXEVENTKEEPER_COMPONENT )),
1158              cssu::UNO_QUERY);
1159 
1160         cssu::Reference<cssl::XInitialization> xInitialization(m_xSAXEventKeeper,  cssu::UNO_QUERY);
1161 
1162         cssu::Sequence <cssu::Any> arg(1);
1163         arg[0] = cssu::makeAny(m_xXMLDocumentWrapper);
1164         xInitialization->initialize(arg);
1165 
1166         cssu::Reference<cssxc::sax::XSAXEventKeeperStatusChangeBroadcaster>
1167             xSAXEventKeeperStatusChangeBroadcaster(m_xSAXEventKeeper, cssu::UNO_QUERY);
1168         xSAXEventKeeperStatusChangeBroadcaster->addSAXEventKeeperStatusChangeListener(this);
1169     }
1170 
1171     bool rc = false;
1172 
1173     if (!m_bIsSAXEventKeeperOnTheSAXChain)
1174     {
1175         rc = true;
1176     }
1177 
1178     m_bIsSAXEventKeeperOnTheSAXChain=true;
1179     changeOutput();
1180 
1181     return rc;
1182 }
1183 
findKeyOrReference(SecurityEntity * pSecurityEntity,const rtl::OUString & ouUri,bool bIsFindingKey)1184 void XSecTester::findKeyOrReference(SecurityEntity* pSecurityEntity, const rtl::OUString& ouUri, bool bIsFindingKey)
1185 {
1186     std::vector<rtl::OUString>::iterator ii_referenceURIs;
1187     std::vector<int>::iterator ii_referenceKeeperIds;
1188     std::vector<int>::iterator ii_referenceRefNums;
1189 
1190     for (ii_referenceURIs = m_vUnsolvedReferenceURIs.begin(),
1191          ii_referenceKeeperIds = m_vUnsolvedReferenceKeeperIds.begin(),
1192          ii_referenceRefNums = m_vUnsolvedReferenceRefNums.begin();
1193          ii_referenceURIs != m_vUnsolvedReferenceURIs.end(); )
1194     {
1195         rtl::OUString ouReferenceUri = *ii_referenceURIs;
1196 
1197         if (ouReferenceUri == ouUri)
1198         {
1199             int nKeeperId = *ii_referenceKeeperIds;
1200             int nRefNum = *ii_referenceRefNums;
1201 
1202             if ( bIsFindingKey )
1203             {
1204                 int nClonedKeeperId = m_xSAXEventKeeper->cloneElementCollector(
1205                     nKeeperId,
1206                     m_bIsExporting?
1207                     (cssxc::sax::ElementMarkPriority_BEFOREMODIFY):
1208                     (cssxc::sax::ElementMarkPriority_AFTERMODIFY));
1209 
1210                 pSecurityEntity->setKeyId(nClonedKeeperId);
1211 
1212                 m_xSAXEventKeeper->setSecurityId(nClonedKeeperId, pSecurityEntity->getSecurityId());
1213 
1214                 cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster>
1215                     xReferenceResolvedBroadcaster(m_xSAXEventKeeper, cssu::UNO_QUERY);
1216                 xReferenceResolvedBroadcaster->addReferenceResolvedListener(
1217                     nClonedKeeperId,
1218                     pSecurityEntity->getReferenceListener());
1219             }
1220             else
1221             {
1222                 int nClonedKeeperId = m_xSAXEventKeeper->cloneElementCollector(
1223                     nKeeperId,
1224                     m_bIsExporting?
1225                     (cssxc::sax::ElementMarkPriority_AFTERMODIFY):
1226                     (cssxc::sax::ElementMarkPriority_BEFOREMODIFY));
1227 
1228                 m_xSAXEventKeeper->setSecurityId(nClonedKeeperId, pSecurityEntity->getSecurityId());
1229 
1230                 cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster>
1231                     xReferenceResolvedBroadcaster
1232                     (m_xSAXEventKeeper, cssu::UNO_QUERY);
1233                 xReferenceResolvedBroadcaster->addReferenceResolvedListener(
1234                     nClonedKeeperId,
1235                     pSecurityEntity->getReferenceListener());
1236 
1237                 cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
1238                         (pSecurityEntity->getReferenceListener(), cssu::UNO_QUERY);
1239                 xReferenceCollector->setReferenceId(nClonedKeeperId);
1240             }
1241 
1242             nRefNum--;
1243             if (nRefNum == 0)
1244             {
1245                 m_xSAXEventKeeper->removeElementCollector(nKeeperId);
1246 
1247                 ii_referenceURIs = m_vUnsolvedReferenceURIs.erase(ii_referenceURIs);
1248                 ii_referenceKeeperIds = m_vUnsolvedReferenceKeeperIds.erase(ii_referenceKeeperIds);
1249                 ii_referenceRefNums = m_vUnsolvedReferenceRefNums.erase(ii_referenceRefNums);
1250             }
1251             else
1252             {
1253                 (*ii_referenceRefNums) = nRefNum;
1254 
1255                 ii_referenceURIs++;
1256                 ii_referenceKeeperIds++;
1257                 ii_referenceRefNums++;
1258             }
1259 
1260             if (bIsFindingKey)
1261             {
1262                 break;
1263             }
1264         }
1265         else
1266         {
1267             ii_referenceURIs++;
1268             ii_referenceKeeperIds++;
1269             ii_referenceRefNums++;
1270         }
1271     }
1272 }
1273 
checkSecurityElement(const rtl::OUString & ouLocalName,const cssu::Reference<cssxs::XAttributeList> & xAttribs)1274 bool XSecTester::checkSecurityElement(
1275     const rtl::OUString& ouLocalName,
1276     const cssu::Reference<cssxs::XAttributeList>& xAttribs)
1277 {
1278     bool rc = false;
1279 
1280     if (ouLocalName.equalsAscii(SIGNATURE_STR))
1281     {
1282         SignatureEntity* pSignatureEntity = new SignatureEntity(
1283             m_xSAXEventKeeper,
1284             m_bIsExporting,
1285             this,
1286             m_xXMLSecurityContext,
1287             m_xXMLSignature,
1288             mxMSF);
1289 
1290         m_vSignatureList.push_back(pSignatureEntity);
1291 
1292         m_stCurrentPath.push(pSignatureEntity);
1293         m_stCurrentPathType.push(false);
1294     }
1295     else if (ouLocalName.equalsAscii(REFERENCE_STR))
1296     {
1297         if (!m_stCurrentPath.empty())
1298         {
1299             void* pSignedInfo = m_stCurrentPath.top();
1300             bool bIsStringType = m_stCurrentPathType.top();
1301 
1302             m_stCurrentPath.pop();
1303             m_stCurrentPathType.pop();
1304 
1305             if (bIsStringType && !m_stCurrentPath.empty())
1306             {
1307                 void* pSignature = m_stCurrentPath.top();
1308                 bool bIsStringType2 = m_stCurrentPathType.top();
1309 
1310                 if (!strcmp((const char*)pSignedInfo, SIGNEDINFO_STR) && !bIsStringType2)
1311                 {
1312                     rtl::OUString ouUri = xAttribs->getValueByName
1313                         (rtl::OUString(RTL_ASCII_USTRINGPARAM( URI_ATTR_STR )));
1314 
1315                     if (ouUri.matchAsciiL("#", 1, 0))
1316                     {
1317                         rtl::OUString uri = ouUri.copy(1);
1318                         SignatureEntity* pSignatureEntity = (SignatureEntity *)pSignature;
1319 
1320                         if (uri != NULL && uri.getLength()>0)
1321                         {
1322                             pSignatureEntity->addReferenceURI(uri);
1323                             findKeyOrReference(pSignatureEntity, uri, true);
1324                         }
1325                     }
1326                 }
1327             }
1328             m_stCurrentPath.push(pSignedInfo);
1329             m_stCurrentPathType.push(bIsStringType);
1330         }
1331         m_stCurrentPath.push( (void *)REFERENCE_STR);
1332         m_stCurrentPathType.push(true);
1333     }
1334     else if(ouLocalName.equalsAscii(KEYVALUE_STR) ||
1335         ouLocalName.equalsAscii(KEYNAME_STR) ||
1336         ouLocalName.equalsAscii(X509DATA_STR) ||
1337         ouLocalName.equalsAscii(ENCRYPTEDKEY_STR))
1338     {
1339         if (!m_stCurrentPath.empty())
1340         {
1341             void* pKeyInfo = m_stCurrentPath.top();
1342             bool bIsStringType = m_stCurrentPathType.top();
1343 
1344             m_stCurrentPath.pop();
1345             m_stCurrentPathType.pop();
1346 
1347             if (bIsStringType && !m_stCurrentPath.empty())
1348             {
1349                 bool bIsStringType2 = m_stCurrentPathType.top();
1350 
1351                 if (!bIsStringType2)
1352                 {
1353                     SecurityEntity *pSecurityEntity =
1354                         (SecurityEntity *) (m_stCurrentPath.top());
1355                     pSecurityEntity->setKeyId(0);
1356                 }
1357             }
1358 
1359             m_stCurrentPath.push(pKeyInfo);
1360             m_stCurrentPathType.push(bIsStringType);
1361         }
1362 
1363         m_stCurrentPath.push((void *)KEYVALUE_STR);
1364         m_stCurrentPathType.push(true);
1365     }
1366     else if(ouLocalName.equalsAscii(RETRIEVALMETHOD_STR))
1367     {
1368         if (!m_stCurrentPath.empty())
1369         {
1370             void* pKeyInfo = m_stCurrentPath.top();
1371             bool bIsStringType = m_stCurrentPathType.top();
1372 
1373             m_stCurrentPath.pop();
1374             m_stCurrentPathType.pop();
1375 
1376             if (bIsStringType && !m_stCurrentPath.empty())
1377             {
1378                 bool bIsStringType2 = m_stCurrentPathType.top();
1379 
1380                 if (!bIsStringType2)
1381                 {
1382                     SecurityEntity *pSecurityEntity =
1383                         (SecurityEntity *) m_stCurrentPath.top();
1384                     rtl::OUString ouUri = xAttribs->getValueByName(
1385                         rtl::OUString(RTL_ASCII_USTRINGPARAM( URI_ATTR_STR )));
1386 
1387                     if (!strcmp((const char *)pKeyInfo, KEYINFO_STR) &&
1388                         ouUri != NULL && ouUri.getLength()>0)
1389                     {
1390                         pSecurityEntity->setKeyURI(ouUri);
1391                         findKeyOrReference(pSecurityEntity, ouUri, true);
1392                     }
1393                 }
1394 
1395             }
1396 
1397             m_stCurrentPath.push(pKeyInfo);
1398             m_stCurrentPathType.push(bIsStringType);
1399         }
1400 
1401         m_stCurrentPath.push((void *)RETRIEVALMETHOD_STR);
1402         m_stCurrentPathType.push(true);
1403     }
1404     else if(ouLocalName.equalsAscii(KEYINFO_STR))
1405     {
1406         m_stCurrentPath.push((void *)KEYINFO_STR);
1407         m_stCurrentPathType.push(true);
1408     }
1409     else if(ouLocalName.equalsAscii(SIGNEDINFO_STR))
1410     {
1411         m_stCurrentPath.push((void *)SIGNEDINFO_STR);
1412         m_stCurrentPathType.push(true);
1413     }
1414     else
1415     {
1416         m_stCurrentPath.push((void *)OTHER_ELEMENT_STR);
1417         m_stCurrentPathType.push(true);
1418     }
1419 
1420     return rc;
1421 }
1422 
checkReference(const rtl::OUString & ouLocalName,const cssu::Reference<cssxs::XAttributeList> & xAttribs,const rtl::OUString & ouId)1423 void XSecTester::checkReference(
1424     const rtl::OUString& ouLocalName,
1425     const cssu::Reference<cssxs::XAttributeList>& xAttribs,
1426     const rtl::OUString& ouId)
1427 {
1428     rtl::OUString refNumStr =
1429         xAttribs->getValueByName(rtl::OUString(RTL_ASCII_USTRINGPARAM(REFNUM_ATTR_STR)));
1430 
1431     if (ouId != NULL && ouId.getLength()>0 )
1432     {
1433         int nRefNum = 999;
1434         if (refNumStr != NULL && refNumStr.getLength()>0 )
1435         {
1436             nRefNum = refNumStr.toInt32();
1437         }
1438 
1439         int nLength = m_vSignatureList.size();
1440         for (int i = 0; i<nLength; ++i)
1441         {
1442             SignatureEntity* pSignatureEntity = m_vSignatureList.at(i);
1443 
1444             if (pSignatureEntity->setReference(ouId, m_bIsExporting))
1445             {
1446                 nRefNum--;
1447             }
1448 
1449             if (pSignatureEntity->setKey(ouId, m_bIsExporting))
1450             {
1451                 nRefNum--;
1452             }
1453         }
1454 
1455         if (nRefNum>0)
1456         {
1457             int nKeeperId;
1458 
1459             if (ouLocalName.equalsAscii(ENCRYPTEDKEY_STR))
1460             {
1461                 nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
1462                     m_bIsExporting ?
1463                     (cssxc::sax::ElementMarkPriority_BEFOREMODIFY):
1464                     (cssxc::sax::ElementMarkPriority_AFTERMODIFY),
1465                     true);
1466             }
1467             else
1468             {
1469                 nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
1470                     m_bIsExporting?
1471                     (cssxc::sax::ElementMarkPriority_AFTERMODIFY):
1472                     (cssxc::sax::ElementMarkPriority_BEFOREMODIFY),
1473                     false);
1474             }
1475 
1476             m_vUnsolvedReferenceURIs.push_back(ouId);
1477             m_vUnsolvedReferenceKeeperIds.push_back(nKeeperId);
1478             m_vUnsolvedReferenceRefNums.push_back(nRefNum);
1479         }
1480     }
1481 }
1482 
endMission()1483 void XSecTester::endMission()
1484 {
1485     while (m_vSignatureList.size() > 0)
1486     {
1487         if (m_vSignatureList.size()>0)
1488         {
1489             SignatureEntity * pSignatureEntity = m_vSignatureList.at(0);
1490             m_vSignatureList.erase(m_vSignatureList.begin());
1491             pSignatureEntity->endMission();
1492             delete pSignatureEntity;
1493         }
1494     }
1495 
1496     while (m_vUnsolvedReferenceURIs.size()>0)
1497     {
1498         int nKeeperId = m_vUnsolvedReferenceKeeperIds.at(0);
1499         m_xSAXEventKeeper->removeElementCollector(nKeeperId);
1500         m_vUnsolvedReferenceURIs.erase(m_vUnsolvedReferenceURIs.begin());
1501         m_vUnsolvedReferenceKeeperIds.erase(m_vUnsolvedReferenceKeeperIds.begin());
1502         m_vUnsolvedReferenceRefNums.erase(m_vUnsolvedReferenceRefNums.begin());
1503     }
1504 }
1505 
addStartAncestorEvent(const rtl::OUString & ouName,const cssu::Reference<cssxs::XAttributeList> & xAttribs)1506 void XSecTester::addStartAncestorEvent(
1507     const rtl::OUString& ouName,
1508     const cssu::Reference< cssxs::XAttributeList >& xAttribs)
1509 {
1510     sal_Int32 nLength = xAttribs->getLength();
1511     AncestorEvent* ancestorEvent = new AncestorEvent( nLength );
1512 
1513     ancestorEvent->bIsStartElement = true;
1514     ancestorEvent->ouName = ouName;
1515 
1516     for (int i = 0; i<nLength; ++i)
1517     {
1518         (ancestorEvent->aAttributeList[i]).sName = xAttribs->getNameByIndex((short)i);
1519         (ancestorEvent->aAttributeList[i]).sValue =xAttribs->getValueByIndex((short)i);
1520     }
1521 
1522     m_vAncestorEvents.push_back(ancestorEvent);
1523 }
1524 
addEndAncestorEvent(const rtl::OUString & ouName)1525 void XSecTester::addEndAncestorEvent(const rtl::OUString& ouName)
1526 {
1527     AncestorEvent* ancestorEvent = new AncestorEvent(0);
1528 
1529     ancestorEvent->bIsStartElement = false;
1530     ancestorEvent->ouName = ouName;
1531 
1532     m_vAncestorEvents.push_back(ancestorEvent);
1533 }
1534 
sendAncestorStartElementEvent(const rtl::OUString & ouName,const cssu::Sequence<cssxcsax::XMLAttribute> & attrList,const cssu::Reference<cssxs::XDocumentHandler> & xDocumentHandler) const1535 void XSecTester::sendAncestorStartElementEvent(
1536     const rtl::OUString& ouName,
1537     const cssu::Sequence< cssxcsax::XMLAttribute >& attrList,
1538     const cssu::Reference< cssxs::XDocumentHandler >& xDocumentHandler) const
1539 {
1540     SvXMLAttributeList* pAttributeList = new SvXMLAttributeList();
1541     cssu::Reference < cssxs::XAttributeList > xAttrList
1542         = cssu::Reference< cssxs::XAttributeList > (pAttributeList);
1543 
1544     sal_Int32 nLength = attrList.getLength();
1545 
1546     for (int i = 0; i<nLength; ++i)
1547     {
1548         pAttributeList->AddAttribute( attrList[i].sName, attrList[i].sValue);
1549     }
1550 
1551     xDocumentHandler->startElement(ouName, xAttrList);
1552 }
1553 
sendAncestorEndElementEvent(const rtl::OUString & ouName,const cssu::Reference<cssxs::XDocumentHandler> & xDocumentHandler) const1554 void XSecTester::sendAncestorEndElementEvent(
1555     const rtl::OUString& ouName,
1556     const cssu::Reference< cssxs::XDocumentHandler >& xDocumentHandler) const
1557 {
1558     xDocumentHandler->endElement(ouName);
1559 }
1560 
checkAncestorStartElementEvent(const std::vector<AncestorEvent * >::const_iterator & ii,const cssu::Reference<cssxs::XDocumentHandler> & xDocumentHandler) const1561 std::vector< AncestorEvent* >::const_iterator XSecTester::checkAncestorStartElementEvent(
1562     const std::vector< AncestorEvent* >::const_iterator& ii,
1563     const cssu::Reference< cssxs::XDocumentHandler >& xDocumentHandler) const
1564 {
1565     std::vector< AncestorEvent* >::const_iterator next = ii+1;
1566 
1567     if (next == m_vAncestorEvents.end())
1568     {
1569         sendAncestorStartElementEvent(
1570             (*ii)->ouName, (*ii)->aAttributeList, xDocumentHandler);
1571     }
1572     else
1573     {
1574         while ((next != m_vAncestorEvents.end()) && ((*next)->bIsStartElement))
1575         {
1576             next = checkAncestorStartElementEvent(next, xDocumentHandler);
1577         }
1578 
1579         if (next != m_vAncestorEvents.end())
1580         {
1581             next++;
1582         }
1583     }
1584 
1585     return next;
1586 }
1587 
flushAncestorEvents(const cssu::Reference<cssxs::XDocumentHandler> & xDocumentHandler)1588 void XSecTester::flushAncestorEvents(
1589     const cssu::Reference< cssxs::XDocumentHandler >& xDocumentHandler)
1590 {
1591     std::vector< AncestorEvent* >::const_iterator ii;
1592 
1593     if (xDocumentHandler != NULL)
1594     {
1595         ii = m_vAncestorEvents.begin();
1596 
1597         while (ii != m_vAncestorEvents.end())
1598         {
1599             AncestorEvent* ancestorEvent = *ii;
1600 
1601             if (ancestorEvent->bIsStartElement)
1602             {
1603                 ii = checkAncestorStartElementEvent(ii, xDocumentHandler);
1604             }
1605             else
1606             {
1607                 sendAncestorEndElementEvent((*ii)->ouName, xDocumentHandler);
1608                 ii++;
1609             }
1610         }
1611     }
1612 
1613     /* free the ancestor events list */
1614     std::vector< AncestorEvent* >::iterator jj;
1615 
1616     while (m_vAncestorEvents.size()>0)
1617     {
1618         jj = m_vAncestorEvents.begin();
1619         delete *jj;
1620         m_vAncestorEvents.erase(jj);
1621     }
1622 }
1623 
1624 /*
1625  * Get the length of a file in a platform independant fashion
1626  */
getLength(const char * pInputFileName)1627 int getLength(const char *pInputFileName)
1628 {
1629     int nSize = 0;
1630     std::ifstream data(pInputFileName);
1631 
1632     data.seekg(0, std::ios_base::end);
1633     nSize = data.tellg();
1634 
1635     return nSize;
1636 }
1637 
outputHeader()1638 void outputHeader()
1639 {
1640     fprintf(stderr, "%16s%4s%8s%12s%12s%12s%12s\n", "File Name", "E/I", "Size", "-C++", "-Java", "Forw-O", "No S/E");
1641     fprintf(stderr, "===============================================================================\n");
1642 }
1643 
1644 /*
1645  * print the output on the screen as well as in the GNUPlot data file
1646  */
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)1647 void output(const rtl::OUString& ouInputFileName,
1648     const rtl::OUString& ouTime_C,
1649     const rtl::OUString& ouTime_Java,
1650     const rtl::OUString& ouTime_NoSecurity,
1651     const rtl::OUString& ouTime_JavaForwardOnly,
1652     const rtl::OUString& ouRemark_C,
1653     const rtl::OUString& ouRemark_Java,
1654     bool bIsExporting)
1655 {
1656     int nSize = getLength(rtl::OString(ouInputFileName, ouInputFileName.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1657     std::ofstream data;
1658 
1659     /* print screen */
1660     int nPosition = ouInputFileName.lastIndexOf('\\');
1661     rtl::OUString fileName = ouInputFileName.copy(nPosition + 1);
1662 
1663     fprintf(stderr, "%16s", rtl::OString(fileName, fileName.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1664 
1665     fprintf(stderr, "%4s", bIsExporting?"E":"I");
1666     fprintf(stderr, "%7dK", nSize/1024);
1667     fprintf(stderr, "%8s %3s",
1668         rtl::OString(ouTime_C, ouTime_C.getLength(), RTL_TEXTENCODING_ASCII_US).getStr(),
1669         rtl::OString(ouRemark_C, ouRemark_C.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1670     fprintf(stderr, "%8s %3s",
1671         rtl::OString(ouTime_Java, ouTime_Java.getLength(), RTL_TEXTENCODING_ASCII_US).getStr(),
1672         rtl::OString(ouRemark_Java, ouRemark_Java.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1673     fprintf(stderr, "%12s", rtl::OString(ouTime_JavaForwardOnly, ouTime_JavaForwardOnly.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1674     fprintf(stderr, "%12s", rtl::OString(ouTime_NoSecurity, ouTime_NoSecurity.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1675     fprintf(stderr, "\n");
1676 
1677     /* output the data as GNUPlot data file */
1678     /*
1679     char str[32];
1680     sprintf(str, "%d %s", nSize, rtl::OString(ouTime_C, ouTime_C.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1681     data.open("d:\\time_vs_size.txt", std::ios::app);
1682     data <<  str << std::endl;
1683     data.close();
1684 
1685     sprintf(str, "%d %s", nSize, rtl::OString(ouTime_Java, ouTime_Java.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1686     data.open("d:\\time_vs_size_without_sec.txt", std::ios::app);
1687     data <<  str << std::endl;
1688     data.close();
1689     */
1690 }
1691 
main(int argc,char ** argv)1692 int main( int argc, char **argv )
1693 {
1694     if (argc < 3)
1695     {
1696         fprintf(stderr, "Usage: testtool <exportbatchfile> <importbatchfile> [<cppcryptotoken>] [<javacryptotoken>]\n");
1697         exit (-1);
1698     }
1699 
1700     rtl::OUString aExportBatchFile = rtl::OUString::createFromAscii(argv[1]);
1701     rtl::OUString aImportBatchFile = rtl::OUString::createFromAscii(argv[2]);
1702     rtl::OUString aCPPCryptoToken;
1703     if ( argc > 3 )
1704         aCPPCryptoToken = rtl::OUString::createFromAscii(argv[3]);
1705     rtl::OUString aJavaCryptoToken;
1706     if ( argc > 4 )
1707         aJavaCryptoToken = rtl::OUString::createFromAscii(argv[4]);
1708 
1709     try
1710     {
1711         uno::Reference< lang::XMultiServiceFactory > xMSF = CreateDemoServiceFactory();
1712 
1713         XSecTester* pTester = new XSecTester( xMSF );
1714         uno::Reference< xml::sax::XDocumentHandler > xKeepARef = pTester;
1715 
1716         pTester->setCryptoDir( aJavaCryptoToken, aCPPCryptoToken );
1717 
1718         rtl::OUString ouTime_C, ouTime_Java, ouTime_NoSecurity, ouTime_JavaForwardOnly;
1719         rtl::OUString ouInputFileName;
1720         rtl::OUString outputFileName1;
1721         rtl::OUString outputFileName2;
1722         rtl::OUString ouRemark_C, ouRemark_Java;
1723 
1724         outputHeader();
1725 
1726         std::ifstream batch_export, batch_import;
1727 
1728         batch_export.open(OUStringToOString( aExportBatchFile, RTL_TEXTENCODING_ASCII_US ).getStr());
1729 
1730         const int MAX_LINE = 80;
1731         char line[MAX_LINE + 1];
1732 
1733         while (batch_export.getline(line, MAX_LINE))
1734         {
1735             ouInputFileName = rtl::OUString::createFromAscii(line);
1736             int nPosition = ouInputFileName.lastIndexOf('.');
1737             int nPosition1;
1738 
1739             /*
1740              * export the file with signautre/encryption (C++)
1741              */
1742             outputFileName1 = ouInputFileName.copy(0, nPosition) +
1743                 rtl::OUString::createFromAscii("-ex.xml");
1744             ouTime_C = pTester->export_xml(ouInputFileName, outputFileName1, sal_False);
1745             nPosition1 = ouTime_C.lastIndexOf('\t');
1746             ouRemark_C = ouTime_C.copy(nPosition1 + 1);
1747             ouTime_C = ouTime_C.copy(0, nPosition1);
1748 
1749             /*
1750              * export the file with signautre/encryption (Java)
1751              */
1752             outputFileName1 = ouInputFileName.copy(0, nPosition) +
1753                 rtl::OUString::createFromAscii("-ex2.xml");
1754             ouTime_Java = pTester->export_xml(ouInputFileName, outputFileName1, sal_True);
1755             nPosition1 = ouTime_Java.lastIndexOf('\t');
1756             ouRemark_Java = ouTime_Java.copy(nPosition1 + 1);
1757             ouTime_Java = ouTime_Java.copy(0, nPosition1);
1758 
1759             /*
1760              * export the file without signautre/encryption
1761              */
1762             outputFileName2 = ouInputFileName.copy(0, nPosition) +
1763                 rtl::OUString::createFromAscii("-ex-no.xml");
1764             ouTime_NoSecurity = pTester->transfer_without_sec(ouInputFileName, outputFileName2, sal_False);
1765 
1766             /*
1767              * export the file with Java Flat Filter
1768              */
1769             outputFileName2 = ouInputFileName.copy(0, nPosition) +
1770                 rtl::OUString::createFromAscii("-ex-jf.xml");
1771             ouTime_JavaForwardOnly = pTester->transfer_without_sec(ouInputFileName, outputFileName2, sal_True);
1772 
1773             /*
1774              * print output
1775              */
1776             output(ouInputFileName, ouTime_C, ouTime_Java, ouTime_NoSecurity, ouTime_JavaForwardOnly, ouRemark_C, ouRemark_Java, true);
1777         }
1778 
1779         batch_export.close();
1780 
1781         batch_import.open(OUStringToOString( aImportBatchFile, RTL_TEXTENCODING_ASCII_US ).getStr());
1782 
1783         while (batch_import.getline(line, MAX_LINE))
1784         {
1785             ouInputFileName = rtl::OUString::createFromAscii(line);
1786             int nPosition = ouInputFileName.lastIndexOf('.');
1787             int nPosition1;
1788 
1789             /*
1790              * import the file with signautre/encryption (C++)
1791              */
1792             outputFileName1 = ouInputFileName.copy(0, nPosition) +
1793                 rtl::OUString::createFromAscii("-im.xml");
1794             ouTime_C = pTester->import_xml(ouInputFileName, outputFileName1, sal_False);
1795             nPosition1 = ouTime_C.lastIndexOf('\t');
1796             ouRemark_C = ouTime_C.copy(nPosition1 + 1);
1797             ouTime_C = ouTime_C.copy(0, nPosition1);
1798 
1799             /*
1800              * import the file with signautre/encryption (Java)
1801              */
1802             outputFileName1 = ouInputFileName.copy(0, nPosition) +
1803                 rtl::OUString::createFromAscii("-im2.xml");
1804             ouTime_Java = pTester->import_xml(ouInputFileName, outputFileName1, sal_True);
1805             nPosition1 = ouTime_Java.lastIndexOf('\t');
1806             ouRemark_Java = ouTime_Java.copy(nPosition1 + 1);
1807             ouTime_Java = ouTime_Java.copy(0, nPosition1);
1808 
1809             /*
1810              * import the file without signautre/encryption
1811              */
1812             outputFileName2 = ouInputFileName.copy(0, nPosition) +
1813                 rtl::OUString::createFromAscii("-im-no.xml");
1814             ouTime_NoSecurity = pTester->transfer_without_sec(ouInputFileName, outputFileName2, sal_False);
1815 
1816             /*
1817              * import the file without signautre/encryption
1818              */
1819 
1820             outputFileName2 = ouInputFileName.copy(0, nPosition) +
1821                 rtl::OUString::createFromAscii("-im-jf.xml");
1822             ouTime_JavaForwardOnly = pTester->transfer_without_sec(ouInputFileName, outputFileName2, sal_True);
1823 
1824             /*
1825              * print output
1826              */
1827             output(ouInputFileName, ouTime_C, ouTime_Java, ouTime_NoSecurity, ouTime_JavaForwardOnly, ouRemark_C, ouRemark_Java, false);
1828         }
1829 
1830         batch_import.close();
1831 
1832         fprintf(stderr, "\n");
1833     }
1834     catch( cssu::Exception& e )
1835     {
1836         fprintf( stderr , "\nEXCEPTION! Error Message: %s\n" ,
1837                  rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
1838     }
1839 
1840     return 0;
1841 }
1842