update dev300-m58
[ooovba.git] / xmlsecurity / tools / demo / performance.cxx
blob1d2ea731dfb59e0056520ce259f10153fdab390c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: performance.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
34 #include <stdio.h>
35 #include <string.h>
37 #include <rtl/ustring.hxx>
38 #include <cppuhelper/bootstrap.hxx>
39 #include <cppuhelper/servicefactory.hxx>
40 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
41 #include <com/sun/star/registry/XImplementationRegistration.hpp>
42 #include <com/sun/star/beans/XPropertySet.hpp>
43 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
45 #include <comphelper/processfactory.hxx>
47 #include <iostream>
48 #include <fstream>
50 #include <util.hxx>
52 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
53 #include <com/sun/star/io/XOutputStream.hpp>
54 #include <com/sun/star/io/XInputStream.hpp>
55 #include <com/sun/star/xml/sax/XParser.hpp>
56 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
57 #include <com/sun/star/xml/sax/XAttributeList.hpp>
58 #include <cppuhelper/implbase4.hxx>
60 #include <com/sun/star/xml/crypto/sax/XSignatureCreationResultListener.hpp>
61 #include <com/sun/star/xml/crypto/sax/XSignatureVerifyResultListener.hpp>
62 #include <com/sun/star/xml/crypto/sax/XSAXEventKeeperStatusChangeListener.hpp>
63 #include <com/sun/star/xml/crypto/sax/XSecuritySAXEventKeeper.hpp>
64 #include <com/sun/star/xml/crypto/sax/XReferenceResolvedListener.hpp>
65 #include <com/sun/star/xml/crypto/XXMLSignature.hpp>
66 #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
67 #include <com/sun/star/xml/csax/XMLAttribute.hpp>
68 #include <com/sun/star/xml/crypto/XSEInitializer.hpp>
69 #include <com/sun/star/xml/crypto/SecurityOperationStatus.hpp>
70 #include <com/sun/star/io/XActiveDataSource.hpp>
71 #include <com/sun/star/lang/XInitialization.hpp>
72 #include <com/sun/star/xml/crypto/sax/XKeyCollector.hpp>
73 #include <com/sun/star/xml/crypto/sax/ElementMarkPriority.hpp>
74 #include <com/sun/star/xml/crypto/sax/XReferenceResolvedBroadcaster.hpp>
75 #include <com/sun/star/xml/crypto/sax/XMissionTaker.hpp>
76 #include <com/sun/star/xml/crypto/sax/XBlockerMonitor.hpp>
77 #include <com/sun/star/xml/crypto/sax/XSignatureCreationResultBroadcaster.hpp>
78 #include <com/sun/star/xml/crypto/sax/XSignatureVerifyResultBroadcaster.hpp>
79 #include <com/sun/star/xml/crypto/sax/XReferenceCollector.hpp>
80 #include <com/sun/star/xml/crypto/sax/XSAXEventKeeperStatusChangeBroadcaster.hpp>
81 #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
83 #include <xmloff/attrlist.hxx>
85 //#include <malloc.h>
86 #include <stdio.h>
87 #include <stdlib.h>
88 #include <string.h>
91 * Can not build under solaris.
92 * Delete the memory.h including by AF
93 #include <memory.h>
96 #include <sys/types.h>
97 #include <sys/stat.h>
98 #include <osl/time.h>
102 #ifndef INCLUDED_VECTOR
103 #include <vector>
104 #define INCLUDED_VECTOR
105 #endif
107 #ifndef INCLUDED_STACK
108 #include <stack>
109 #define INCLUDED_STACK
110 #endif
112 /* xml security framework components */
113 #define SIGNATURECREATOR_COMPONENT "com.sun.star.xml.crypto.sax.SignatureCreator"
114 #define SIGNATUREVERIFIER_COMPONENT "com.sun.star.xml.crypto.sax.SignatureVerifier"
115 #define JAVAFLATFILTER_COMPONENT "com.sun.star.xml.crypto.eval.JavaFlatFilter"
116 #define SAXEVENTKEEPER_COMPONENT "com.sun.star.xml.crypto.sax.SAXEventKeeper"
118 /* java based bridge components */
119 #define SEINITIALIZER_JAVA_COMPONENT "com.sun.star.xml.security.bridge.jxsec.SEInitializer_JxsecImpl"
120 #define XMLSIGNATURE_JAVA_COMPONENT "com.sun.star.xml.security.bridge.jxsec.XMLSignature_JxsecImpl"
121 #define XMLDOCUMENTWRAPPER_JAVA_COMPONENT "com.sun.star.xml.security.bridge.jxsec.XMLDocumentWrapper_JxsecImpl"
123 /* c based bridge components */
124 #define SEINITIALIZER_C_COMPONENT "com.sun.star.xml.crypto.SEInitializer"
125 #define XMLSIGNATURE_C_COMPONENT "com.sun.star.xml.crypto.XMLSignature"
126 #define XMLDOCUMENT_C_COMPONENT "com.sun.star.xml.wrapper.XMLDocumentWrapper"
128 /* security related elements and attributes */
129 #define SIGNATURE_STR "Signature"
130 #define REFERENCE_STR "Reference"
131 #define SIGNEDINFO_STR "SignedInfo"
132 #define KEYINFO_STR "KeyInfo"
133 #define KEYVALUE_STR "KeyValue"
134 #define KEYNAME_STR "KeyName"
135 #define X509DATA_STR "X509Data"
136 #define ENCRYPTEDKEY_STR "EncryptedKey"
137 #define RETRIEVALMETHOD_STR "RetrievalMethod"
138 #define OTHER_ELEMENT_STR "OTHER_ELEMENT_STR"
139 #define REFNUM_ATTR_STR "refNum"
140 #define URI_ATTR_STR "URI"
143 #define RTL_ASCII_USTRINGPARAM( asciiStr ) asciiStr, strlen( asciiStr ), RTL_TEXTENCODING_ASCII_US
145 namespace cssu = com::sun::star::uno;
146 namespace cssl = com::sun::star::lang;
147 namespace cssb = com::sun::star::beans;
148 namespace cssi = com::sun::star::io;
149 namespace cssxc = com::sun::star::xml::crypto;
150 namespace cssxs = com::sun::star::xml::sax;
151 namespace cssxw = com::sun::star::xml::wrapper;
152 namespace cssxcsax = com::sun::star::xml::csax;
155 using namespace ::com::sun::star;
158 class XSecTester;
161 * The XSecTester class is a C++ version of SecurityFramworkController.java
165 class SecurityEntity
167 private:
168 static int m_nNextSecurityId;
169 rtl::OUString m_ouKeyURI;
171 protected:
172 com::sun::star::uno::Reference<
173 com::sun::star::lang::XMultiServiceFactory > mxMSF;
175 com::sun::star::uno::Reference<
176 com::sun::star::xml::crypto::sax::XReferenceResolvedListener >
177 m_xReferenceListener;
179 com::sun::star::uno::Reference<
180 com::sun::star::xml::crypto::sax::XSecuritySAXEventKeeper >
181 m_xSAXEventKeeper;
183 com::sun::star::uno::Reference<
184 com::sun::star::xml::crypto::XXMLSecurityContext >
185 m_xXMLSecurityContext;
187 com::sun::star::uno::Reference<
188 com::sun::star::xml::crypto::XXMLSignature >
189 m_xXMLSignature;
191 int m_nSecurityId;
193 private:
194 int getNextSecurityId() const;
196 protected:
197 SecurityEntity(
198 const com::sun::star::uno::Reference<
199 com::sun::star::xml::crypto::sax::XSecuritySAXEventKeeper >&
200 xSAXEventKeeper,
201 const com::sun::star::uno::Reference<
202 com::sun::star::xml::crypto::XXMLSecurityContext >&
203 xXMLSecurityContext,
204 const com::sun::star::uno::Reference<
205 com::sun::star::xml::crypto::XXMLSignature >&
206 xXMLSignature,
207 const com::sun::star::uno::Reference<
208 com::sun::star::lang::XMultiServiceFactory >&
209 rsMSF);
211 public:
212 void setKeyId(int nId);
214 int getSecurityId() const;
216 com::sun::star::uno::Reference<
217 com::sun::star::xml::crypto::sax::XReferenceResolvedListener >
218 getReferenceListener() const;
220 bool setKey( const rtl::OUString& ouUri, bool bIsExporting );
222 void setKeyURI(const rtl::OUString& ouUri);
224 bool endMission();
228 class SignatureEntity : public SecurityEntity
230 private:
231 std::vector< rtl::OUString > m_vReferenceIds;
232 int m_nSignatureElementCollectorId;
234 bool hasReference(const rtl::OUString& ouUri) const;
236 public:
237 SignatureEntity(
238 const com::sun::star::uno::Reference<
239 com::sun::star::xml::crypto::sax::XSecuritySAXEventKeeper >&
240 xSAXEventKeeper,
241 bool bIsExporting,
242 XSecTester* pListener,
243 const com::sun::star::uno::Reference<
244 com::sun::star::xml::crypto::XXMLSecurityContext >&
245 xXMLSecurityContext,
246 const com::sun::star::uno::Reference<
247 com::sun::star::xml::crypto::XXMLSignature >&
248 xXMLSignature,
249 const com::sun::star::uno::Reference<
250 com::sun::star::lang::XMultiServiceFactory >&
251 rsMSF);
252 ~SignatureEntity(){};
254 void setReferenceNumber() const;
255 bool setReference( const rtl::OUString& ouUri, bool bIsExporting ) const;
256 void addReferenceURI( const rtl::OUString& ouUri );
259 struct AncestorEvent
261 AncestorEvent( sal_Int32 nAttrNum ):aAttributeList(nAttrNum){};
263 bool bIsStartElement;
264 rtl::OUString ouName;
266 com::sun::star::uno::Sequence<
267 com::sun::star::xml::csax::XMLAttribute >
268 aAttributeList;
271 class XSecTester : public cppu::WeakImplHelper4
273 com::sun::star::xml::crypto::sax::XSignatureCreationResultListener,
274 com::sun::star::xml::crypto::sax::XSignatureVerifyResultListener,
275 com::sun::star::xml::crypto::sax::XSAXEventKeeperStatusChangeListener,
276 com::sun::star::xml::sax::XDocumentHandler
279 private:
280 com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory > mxMSF;
282 sal_Int32 m_nTotalSignatureNumber;
283 sal_Int32 m_nSuccessfulSignatureNumber;
285 com::sun::star::uno::Reference<
286 com::sun::star::xml::sax::XDocumentHandler >
287 m_xExportHandler;
289 com::sun::star::uno::Reference<
290 com::sun::star::xml::crypto::sax::XSecuritySAXEventKeeper >
291 m_xSAXEventKeeper;
293 com::sun::star::uno::Reference<
294 com::sun::star::xml::wrapper::XXMLDocumentWrapper >
295 m_xXMLDocumentWrapper;
297 com::sun::star::uno::Reference<
298 com::sun::star::xml::sax::XDocumentHandler >
299 m_xOutputHandler;
301 com::sun::star::uno::Reference<
302 com::sun::star::xml::sax::XParser >
303 m_xSaxParser;
305 std::stack< void* > m_stCurrentPath;
306 std::stack< bool > m_stCurrentPathType;
308 std::vector< AncestorEvent* > m_vAncestorEvents;
309 std::vector< SignatureEntity* > m_vSignatureList;
311 std::vector< rtl::OUString > m_vUnsolvedReferenceURIs;
312 std::vector< int > m_vUnsolvedReferenceKeeperIds;
313 std::vector< int > m_vUnsolvedReferenceRefNums;
315 bool m_bIsExporting;
316 bool m_bIsBlocking;
318 bool m_bIsInsideCollectedElement;
319 bool m_bIsSAXEventKeeperOnTheSAXChain;
321 com::sun::star::uno::Reference<
322 com::sun::star::xml::crypto::XXMLSecurityContext >
323 m_xXMLSecurityContext;
325 com::sun::star::uno::Reference<
326 com::sun::star::xml::crypto::XXMLSignature >
327 m_xXMLSignature;
329 rtl::OUString m_ouJavaCryptokenDir;
330 rtl::OUString m_ouCCryptokenDir;
331 rtl::OUString m_ouXMLDocumentWrapperComponentName;
333 private:
334 com::sun::star::uno::Reference<
335 com::sun::star::io::XOutputStream >
336 createOutputStream( const rtl::OUString& ouFile );
338 rtl::OUString parseFile(
339 const rtl::OUString& ouInputFileName,
340 const rtl::OUString& ouOutputFileName,
341 bool bIsExporting,
342 bool bIsJavaBased);
344 void changeOutput();
346 bool foundSecurityRelated();
348 void findKeyOrReference(SecurityEntity* pSecurityEntity, const rtl::OUString& ouUri, bool bIsFindKey);
350 bool checkSecurityElement(
351 const rtl::OUString& ouLocalName,
352 const com::sun::star::uno::Reference<
353 com::sun::star::xml::sax::XAttributeList>& xAttribs);
355 void checkReference(
356 const rtl::OUString& ouLocalName,
357 const com::sun::star::uno::Reference<
358 com::sun::star::xml::sax::XAttributeList>& xAttribs,
359 const rtl::OUString& ouId);
361 void endMission();
363 void addStartAncestorEvent(
364 const rtl::OUString& ouName,
365 const com::sun::star::uno::Reference<
366 com::sun::star::xml::sax::XAttributeList>& xAttribs);
368 void addEndAncestorEvent( const rtl::OUString& ouName );
370 void flushAncestorEvents(
371 const com::sun::star::uno::Reference<
372 com::sun::star::xml::sax::XDocumentHandler >& xDocumentHandler);
374 void XSecTester::sendAncestorStartElementEvent(
375 const rtl::OUString& ouName,
376 const com::sun::star::uno::Sequence<
377 com::sun::star::xml::csax::XMLAttribute >& xAttrList,
378 const com::sun::star::uno::Reference<
379 com::sun::star::xml::sax::XDocumentHandler >& xDocumentHandler) const;
381 void XSecTester::sendAncestorEndElementEvent(
382 const rtl::OUString& ouName,
383 const com::sun::star::uno::Reference<
384 com::sun::star::xml::sax::XDocumentHandler >& xDocumentHandler) const;
386 std::vector< AncestorEvent* >::const_iterator XSecTester::checkAncestorStartElementEvent(
387 const std::vector< AncestorEvent* >::const_iterator& ii,
388 const com::sun::star::uno::Reference<
389 com::sun::star::xml::sax::XDocumentHandler >& xDocumentHandler) const;
391 public:
392 XSecTester(const com::sun::star::uno::Reference<
393 com::sun::star::lang::XMultiServiceFactory >& rxMSF)
394 :mxMSF( rxMSF ){};
395 virtual ~XSecTester(){};
397 /* XSignatureCreationResultListener */
398 virtual void SAL_CALL signatureCreated(
399 sal_Int32 securityId,
400 com::sun::star::xml::crypto::SecurityOperationStatus creationResult )
401 throw (com::sun::star::uno::RuntimeException);
403 /* XSignatureVerifyResultListener */
404 virtual void SAL_CALL signatureVerified(
405 sal_Int32 securityId,
406 com::sun::star::xml::crypto::SecurityOperationStatus verifyResult )
407 throw (com::sun::star::uno::RuntimeException);
409 /* XSAXEventKeeperStatusChangeListener */
410 virtual void SAL_CALL blockingStatusChanged( sal_Bool isBlocking )
411 throw (com::sun::star::uno::RuntimeException);
412 virtual void SAL_CALL collectionStatusChanged(
413 sal_Bool isInsideCollectedElement )
414 throw (com::sun::star::uno::RuntimeException);
415 virtual void SAL_CALL bufferStatusChanged( sal_Bool isBufferEmpty )
416 throw (com::sun::star::uno::RuntimeException);
418 /* XXMLSecTester */
419 virtual rtl::OUString SAL_CALL transfer_without_sec(
420 const rtl::OUString& inputFileName,
421 const rtl::OUString& outputFileName,
422 sal_Bool isBridgeInvolved)
423 throw (com::sun::star::uno::RuntimeException);
424 virtual rtl::OUString SAL_CALL export_xml(
425 const rtl::OUString& inputFileName,
426 const rtl::OUString& outputFileName,
427 sal_Bool isJavaBased)
428 throw (com::sun::star::uno::RuntimeException);
429 virtual rtl::OUString SAL_CALL import_xml(
430 const rtl::OUString& inputFileName,
431 const rtl::OUString& outputFileName,
432 sal_Bool isJavaBased)
433 throw (com::sun::star::uno::RuntimeException);
435 virtual void SAL_CALL setCryptoDir(
436 const rtl::OUString & javaDirName,
437 const rtl::OUString & cDirName)
438 throw (com::sun::star::uno::RuntimeException);
440 /* XDocumentHandler */
441 virtual void SAL_CALL endDocument()
442 throw (com::sun::star::uno::RuntimeException);
443 virtual void SAL_CALL startDocument()
444 throw (com::sun::star::uno::RuntimeException);
445 virtual void SAL_CALL characters(const class rtl::OUString&)
446 throw (com::sun::star::uno::RuntimeException);
447 virtual void SAL_CALL processingInstruction(const rtl::OUString&, const rtl::OUString&)
448 throw (com::sun::star::uno::RuntimeException);
449 virtual void SAL_CALL ignorableWhitespace(const rtl::OUString&)
450 throw (com::sun::star::uno::RuntimeException);
451 virtual void SAL_CALL startElement(
452 const rtl::OUString&,
453 const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList >&)
454 throw (com::sun::star::uno::RuntimeException);
455 virtual void SAL_CALL endElement(const rtl::OUString&)
456 throw (com::sun::star::uno::RuntimeException);
457 virtual void SAL_CALL setDocumentLocator(
458 const com::sun::star::uno::Reference< com::sun::star::xml::sax::XLocator >&)
459 throw (com::sun::star::uno::RuntimeException);
462 rtl::OUString XSecTester::parseFile(
463 const rtl::OUString& ouInputFileName,
464 const rtl::OUString& ouOutputFileName,
465 bool bIsExporting,
466 bool bIsJavaBased)
468 rtl::OUString ouMessage;
470 cssu::Reference<cssi::XInputStream> xInputStream = OpenInputStream(ouInputFileName);
472 if (xInputStream != NULL )
474 /* initialization */
475 rtl::OUString SEInitializer_comp;
476 rtl::OUString XMLSignature_comp;
477 rtl::OUString tokenPath;
478 cssu::Reference < cssxc::XSEInitializer > xSEInitializer;
480 if (bIsJavaBased)
482 SEInitializer_comp = rtl::OUString::createFromAscii( SEINITIALIZER_JAVA_COMPONENT );
483 XMLSignature_comp = rtl::OUString::createFromAscii( XMLSIGNATURE_JAVA_COMPONENT);
484 m_ouXMLDocumentWrapperComponentName = rtl::OUString::createFromAscii( XMLDOCUMENTWRAPPER_JAVA_COMPONENT );
485 tokenPath = m_ouJavaCryptokenDir;
487 else
489 SEInitializer_comp = rtl::OUString::createFromAscii( SEINITIALIZER_C_COMPONENT );
490 XMLSignature_comp = rtl::OUString::createFromAscii( XMLSIGNATURE_C_COMPONENT);
491 m_ouXMLDocumentWrapperComponentName = rtl::OUString::createFromAscii( XMLDOCUMENT_C_COMPONENT );
492 tokenPath = m_ouCCryptokenDir;
495 xSEInitializer = cssu::Reference < cssxc::XSEInitializer > (
496 mxMSF->createInstance( SEInitializer_comp ),
497 cssu::UNO_QUERY );
499 m_xXMLSignature = cssu::Reference<cssxc::XXMLSignature> (
500 mxMSF->createInstance( XMLSignature_comp ),
501 cssu::UNO_QUERY );
503 if ( xSEInitializer.is() && m_xXMLSignature.is())
505 /* create SAX Parser */
506 const rtl::OUString sSaxParser (
507 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser") );
508 m_xSaxParser = cssu::Reference < cssxs::XParser > ( mxMSF->createInstance( sSaxParser ), cssu::UNO_QUERY );
510 /* create SAX Writer */
511 const rtl::OUString sSaxWriter (
512 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer") );
513 cssu::Reference < cssi::XActiveDataSource > xSaxWriter
514 ( mxMSF->createInstance( sSaxWriter ), cssu::UNO_QUERY );
516 cssu::Reference< cssi::XOutputStream > xOutputStream = OpenOutputStream(ouOutputFileName);
517 xSaxWriter->setOutputStream( xOutputStream );
519 cssxs::InputSource aInput;
520 aInput.sSystemId = ouInputFileName;
521 aInput.aInputStream = xInputStream;
523 cssu::Reference < cssxs::XDocumentHandler > xSaxWriterHandler( xSaxWriter, cssu::UNO_QUERY);
525 m_xXMLSecurityContext =
526 xSEInitializer->createSecurityContext(tokenPath);
528 m_bIsExporting = bIsExporting;
529 m_xExportHandler = xSaxWriterHandler;
530 m_xOutputHandler = xSaxWriterHandler;
532 m_xXMLDocumentWrapper = NULL;
533 m_xSAXEventKeeper = NULL;
534 m_bIsSAXEventKeeperOnTheSAXChain = false;
536 m_bIsBlocking = false;
537 m_bIsInsideCollectedElement = false;
539 OSL_ASSERT(m_vSignatureList.size() == 0);
540 OSL_ASSERT(m_vUnsolvedReferenceURIs.size() == 0);
541 OSL_ASSERT(m_vUnsolvedReferenceKeeperIds.size() == 0);
542 OSL_ASSERT(m_vUnsolvedReferenceRefNums.size() == 0);
543 OSL_ASSERT(m_stCurrentPath.empty());
544 OSL_ASSERT(m_stCurrentPathType.empty());
545 OSL_ASSERT(m_vAncestorEvents.empty());
547 changeOutput();
549 /* foundSecurityRelated(); */
551 /* Begin to parse */
552 TimeValue startTime, endTime;
553 osl_getSystemTime( &startTime );
555 xSaxWriterHandler->startDocument();
557 if (m_bIsExporting)
559 m_xSaxParser->setDocumentHandler(this);
560 m_xSaxParser->parseStream(aInput);
562 else
564 m_xSaxParser->setDocumentHandler(this);
565 m_xSaxParser->parseStream(aInput);
568 endMission();
569 xSaxWriterHandler->endDocument();
571 osl_getSystemTime( &endTime );
573 flushAncestorEvents( NULL );
575 // Bug in SAXWriter, done in endDocument()
576 // xOutputStream->closeOutput();
577 xInputStream->closeInput();
581 * Free the security context
583 xSEInitializer->freeSecurityContext(m_xXMLSecurityContext);
584 m_xXMLSecurityContext = NULL;
586 /* Calculate the time */
587 double diff = ((double)((endTime.Nanosec + endTime.Seconds*1000000000.0)
588 - (startTime.Nanosec + startTime.Seconds*1000000000.0))) /
589 ((double)1000000000.0);
591 char buf[32];
592 sprintf(buf, "%.2f", diff);
593 ouMessage += rtl::OUString(RTL_ASCII_USTRINGPARAM(buf));
595 else
597 ouMessage += rtl::OUString::createFromAscii( "N/A" );
601 else
603 ouMessage += rtl::OUString::createFromAscii( "-" );
606 return ouMessage;
609 /* XSignatureCreationResultListener */
610 void SAL_CALL XSecTester::signatureCreated(
611 sal_Int32 securityId,
612 cssxc::SecurityOperationStatus creationResult )
613 throw (cssu::RuntimeException)
615 m_nTotalSignatureNumber++;
616 if (creationResult == cssxc::SecurityOperationStatus_OPERATION_SUCCEEDED)
618 m_nSuccessfulSignatureNumber++;
622 /* XSignatureVerifyResultListener */
623 void SAL_CALL XSecTester::signatureVerified(
624 sal_Int32 securityId,
625 cssxc::SecurityOperationStatus verifyResult )
626 throw (cssu::RuntimeException)
628 m_nTotalSignatureNumber++;
629 if (verifyResult == cssxc::SecurityOperationStatus_OPERATION_SUCCEEDED)
631 m_nSuccessfulSignatureNumber++;
635 /* XSAXEventKeeperStatusChangeListener */
636 void SAL_CALL XSecTester::blockingStatusChanged( sal_Bool isBlocking )
637 throw (cssu::RuntimeException)
639 this->m_bIsBlocking = isBlocking;
642 void SAL_CALL XSecTester::collectionStatusChanged( sal_Bool isInsideCollectedElement )
643 throw (cssu::RuntimeException)
645 this->m_bIsInsideCollectedElement = isInsideCollectedElement;
647 if ( !m_bIsInsideCollectedElement && !m_bIsBlocking)
649 m_bIsSAXEventKeeperOnTheSAXChain = false;
651 else
653 m_bIsSAXEventKeeperOnTheSAXChain = true;
655 changeOutput();
658 void SAL_CALL XSecTester::bufferStatusChanged( sal_Bool isBufferEmpty )
659 throw (cssu::RuntimeException)
661 if (isBufferEmpty)
663 m_xXMLDocumentWrapper = NULL;
665 m_xSAXEventKeeper = NULL;
666 m_bIsSAXEventKeeperOnTheSAXChain = false;
667 changeOutput();
671 /* XXMLSecTester */
672 rtl::OUString SAL_CALL XSecTester::export_xml( const rtl::OUString& inputFileName, const rtl::OUString& outputFileName, sal_Bool isJavaBased)
673 throw (cssu::RuntimeException)
675 rtl::OUString ouMessage;
677 m_nTotalSignatureNumber = 0;
678 m_nSuccessfulSignatureNumber = 0;
680 ouMessage += parseFile(inputFileName, outputFileName, sal_True, isJavaBased);
682 rtl::OUString ouRemark = rtl::OUString::valueOf(m_nSuccessfulSignatureNumber) +
683 rtl::OUString(RTL_ASCII_USTRINGPARAM( "/" ))
684 + rtl::OUString::valueOf(m_nTotalSignatureNumber);
685 ouMessage += rtl::OUString(RTL_ASCII_USTRINGPARAM("\t")) + ouRemark;
687 return ouMessage;
690 rtl::OUString SAL_CALL XSecTester::import_xml( const rtl::OUString& inputFileName, const rtl::OUString& outputFileName, sal_Bool isJavaBased)
691 throw (cssu::RuntimeException)
693 rtl::OUString ouMessage;
695 m_nTotalSignatureNumber = 0;
696 m_nSuccessfulSignatureNumber = 0;
698 ouMessage += parseFile(inputFileName, outputFileName, sal_False, isJavaBased);
700 rtl::OUString ouRemark = rtl::OUString::valueOf(m_nSuccessfulSignatureNumber) +
701 rtl::OUString(RTL_ASCII_USTRINGPARAM( "/" ))
702 + rtl::OUString::valueOf(m_nTotalSignatureNumber);
703 ouMessage += rtl::OUString(RTL_ASCII_USTRINGPARAM("\t")) + ouRemark;
705 return ouMessage;
708 rtl::OUString SAL_CALL XSecTester::transfer_without_sec(
709 const rtl::OUString& inputFileName,
710 const rtl::OUString& outputFileName,
711 sal_Bool isBridgeInvolved)
712 throw (cssu::RuntimeException)
714 rtl::OUString ouMessage;
716 cssu::Reference< cssi::XInputStream > xInputStream = OpenInputStream(inputFileName);
718 if (xInputStream != NULL )
720 /* create SAX Parser */
721 const rtl::OUString sSaxParser (
722 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser") );
723 m_xSaxParser = cssu::Reference < cssxs::XParser > ( mxMSF->createInstance( sSaxParser ), cssu::UNO_QUERY );
725 /* create SAX Writer */
726 const rtl::OUString sSaxWriter (
727 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer") );
728 cssu::Reference < cssi::XActiveDataSource > xSaxWriter
729 ( mxMSF->createInstance( sSaxWriter ), cssu::UNO_QUERY );
730 cssu::Reference < cssxs::XDocumentHandler > xSaxWriterHandler(
731 xSaxWriter, cssu::UNO_QUERY);
733 if (!isBridgeInvolved)
735 /* connect the SAX Parser and the SAX Writer */
736 m_xSaxParser->setDocumentHandler ( xSaxWriterHandler );
738 else
740 /* create Java Flat Filter */
741 const rtl::OUString sJavaFlatFilter(
742 RTL_CONSTASCII_USTRINGPARAM( JAVAFLATFILTER_COMPONENT ) );
743 cssu::Reference < cssxs::XParser > xJavaFilterParser
744 ( mxMSF->createInstance( sJavaFlatFilter ), cssu::UNO_QUERY );
745 cssu::Reference < cssxs::XDocumentHandler > xJavaFilterHandler(
746 xJavaFilterParser, cssu::UNO_QUERY );
748 if ( !xJavaFilterParser.is() )
749 return rtl::OUString::createFromAscii( "NO JAVA" );
751 /* connect the SAX Parser, the Java Flat Filter and the SAX Writer */
752 xJavaFilterParser->setDocumentHandler( xSaxWriterHandler );
753 m_xSaxParser->setDocumentHandler ( xJavaFilterHandler );
757 /* set output stream */
758 cssu::Reference< cssi::XOutputStream > xOutputStream =
759 OpenOutputStream(outputFileName);
760 xSaxWriter->setOutputStream( xOutputStream );
762 /* prepare input stream */
763 cssxs::InputSource aInput;
764 aInput.sSystemId = inputFileName;
765 aInput.aInputStream = xInputStream;
767 TimeValue startTime, endTime;
768 osl_getSystemTime( &startTime );
770 m_xSaxParser->parseStream ( aInput );
772 // xOutputStream->closeOutput();
773 xInputStream->closeInput();
775 osl_getSystemTime( &endTime );
777 double diff = ((double)((endTime.Nanosec + endTime.Seconds*1000000000.0)
778 - (startTime.Nanosec + startTime.Seconds*1000000000.0)))/((double)1000000000.0);
779 char buf[32];
780 sprintf(buf, "%.2f", diff);
781 ouMessage += rtl::OUString(RTL_ASCII_USTRINGPARAM(buf));
784 return ouMessage;
787 void SAL_CALL XSecTester::setCryptoDir(const rtl::OUString & javaDirName, const rtl::OUString & cDirName)
788 throw (cssu::RuntimeException)
790 m_ouJavaCryptokenDir = javaDirName;
791 m_ouCCryptokenDir = cDirName;
795 cssu::Reference< cssu::XInterface > SAL_CALL XSecTester_createInstance(
796 const cssu::Reference< cssl::XMultiServiceFactory > & rSMgr)
797 throw( cssu::Exception )
799 return (cppu::OWeakObject*) new XSecTester( rSMgr );
802 int SecurityEntity::m_nNextSecurityId = 1;
804 SecurityEntity::SecurityEntity(
805 const cssu::Reference<cssxc::sax::XSecuritySAXEventKeeper>& xSAXEventKeeper,
806 const cssu::Reference<cssxc::XXMLSecurityContext>& xXMLSecurityContext,
807 const cssu::Reference<cssxc::XXMLSignature>& xXMLSignature,
808 const cssu::Reference< cssl::XMultiServiceFactory > &rsMSF)
809 :m_xSAXEventKeeper(xSAXEventKeeper),
810 m_xXMLSecurityContext(xXMLSecurityContext),
811 m_xXMLSignature(xXMLSignature),
812 mxMSF(rsMSF),
813 m_ouKeyURI(RTL_ASCII_USTRINGPARAM(""))
815 m_nSecurityId = getNextSecurityId();
818 int SecurityEntity::getNextSecurityId() const
820 int nId = m_nNextSecurityId++;
821 return nId;
824 void SecurityEntity::setKeyId(int nId)
826 cssu::Reference<cssxc::sax::XKeyCollector> keyCollector (m_xReferenceListener, cssu::UNO_QUERY);
827 keyCollector->setKeyId(nId);
831 void SecurityEntity::setKeyURI(const rtl::OUString& ouUri)
833 m_ouKeyURI = ouUri;
836 cssu::Reference<cssxc::sax::XReferenceResolvedListener> SecurityEntity::getReferenceListener() const
838 return m_xReferenceListener;
841 int SecurityEntity::getSecurityId() const
843 return m_nSecurityId;
846 bool SecurityEntity::setKey(const rtl::OUString& ouUri, bool bIsExporting)
848 bool rc = false;
850 if (m_ouKeyURI != rtl::OUString(RTL_ASCII_USTRINGPARAM("")) &&
851 m_ouKeyURI == ouUri)
853 int nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
854 bIsExporting ?
855 (cssxc::sax::ElementMarkPriority_BEFOREMODIFY):
856 (cssxc::sax::ElementMarkPriority_AFTERMODIFY),
857 true);
859 setKeyId(nKeeperId);
860 m_xSAXEventKeeper->setSecurityId(nKeeperId, m_nSecurityId);
862 cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
863 (m_xSAXEventKeeper, cssu::UNO_QUERY);
864 xReferenceResolvedBroadcaster->addReferenceResolvedListener(nKeeperId,
865 m_xReferenceListener);
867 rc = true;
870 return rc;
873 bool SecurityEntity::endMission()
875 cssu::Reference<cssxc::sax::XMissionTaker> xMissionTaker
876 (m_xReferenceListener, cssu::UNO_QUERY);
878 return xMissionTaker->endMission();
881 SignatureEntity::SignatureEntity(
882 const cssu::Reference<cssxc::sax::XSecuritySAXEventKeeper>& xSAXEventKeeper,
883 bool bIsExporting,
884 XSecTester* pListener,
885 const cssu::Reference<cssxc::XXMLSecurityContext>& xXMLSecurityContext,
886 const cssu::Reference<cssxc::XXMLSignature>& xXMLSignature,
887 const cssu::Reference< cssl::XMultiServiceFactory >& rsMSF)
888 :SecurityEntity(xSAXEventKeeper,
889 xXMLSecurityContext,
890 xXMLSignature,
891 rsMSF)
893 if (bIsExporting)
895 m_nSignatureElementCollectorId =
896 m_xSAXEventKeeper->addSecurityElementCollector(
897 cssxc::sax::ElementMarkPriority_AFTERMODIFY,
898 true);
900 m_xSAXEventKeeper->setSecurityId(m_nSignatureElementCollectorId, m_nSecurityId);
902 m_xReferenceListener = cssu::Reference< cssxc::sax::XReferenceResolvedListener >(
903 mxMSF->createInstance( rtl::OUString::createFromAscii( SIGNATURECREATOR_COMPONENT )),
904 cssu::UNO_QUERY);
906 cssu::Reference<cssl::XInitialization> xInitialization(m_xReferenceListener, cssu::UNO_QUERY);
908 cssu::Sequence<cssu::Any> args(5);
909 char buf[16];
911 sprintf(buf, "%d", m_nSecurityId);
912 args[0] = cssu::makeAny(rtl::OUString(RTL_ASCII_USTRINGPARAM(buf)));
913 args[1] = cssu::makeAny(m_xSAXEventKeeper);
915 sprintf(buf, "%d", m_nSignatureElementCollectorId);
916 args[2] = cssu::makeAny(rtl::OUString(RTL_ASCII_USTRINGPARAM(buf)));
917 args[3] = cssu::makeAny(m_xXMLSecurityContext->getSecurityEnvironment());
918 args[4] = cssu::makeAny(m_xXMLSignature);
920 xInitialization->initialize(args);
922 int nBlockerId = m_xSAXEventKeeper->addBlocker();
923 m_xSAXEventKeeper->setSecurityId(nBlockerId, m_nSecurityId);
925 cssu::Reference<cssxc::sax::XBlockerMonitor> xBlockerMonitor(m_xReferenceListener, cssu::UNO_QUERY);
926 xBlockerMonitor->setBlockerId(nBlockerId);
928 cssu::Reference< cssxc::sax::XSignatureCreationResultBroadcaster > xSignatureCreationResultBroadcaster
929 (m_xReferenceListener, cssu::UNO_QUERY);
930 xSignatureCreationResultBroadcaster->addSignatureCreationResultListener(pListener);
932 else
934 m_nSignatureElementCollectorId =
935 m_xSAXEventKeeper->addSecurityElementCollector(
936 cssxc::sax::ElementMarkPriority_BEFOREMODIFY,
937 false);
939 m_xSAXEventKeeper->setSecurityId(m_nSignatureElementCollectorId, m_nSecurityId);
941 m_xReferenceListener = cssu::Reference< cssxc::sax::XReferenceResolvedListener >(
942 mxMSF->createInstance( rtl::OUString::createFromAscii( SIGNATUREVERIFIER_COMPONENT )),
943 cssu::UNO_QUERY);
945 cssu::Reference<cssl::XInitialization> xInitialization(m_xReferenceListener, cssu::UNO_QUERY);
947 cssu::Sequence<cssu::Any> args(5);
948 char buf[16];
950 sprintf(buf, "%d", m_nSecurityId);
951 args[0] = cssu::makeAny(rtl::OUString(RTL_ASCII_USTRINGPARAM(buf)));
952 args[1] = cssu::makeAny(m_xSAXEventKeeper);
954 sprintf(buf, "%d", m_nSignatureElementCollectorId);
955 args[2] = cssu::makeAny(rtl::OUString(RTL_ASCII_USTRINGPARAM(buf)));
956 args[3] = cssu::makeAny(m_xXMLSecurityContext);
957 args[4] = cssu::makeAny(m_xXMLSignature);
958 xInitialization->initialize(args);
960 cssu::Reference< cssxc::sax::XSignatureVerifyResultBroadcaster > xSignatureVerifyResultBroadcaster
961 (m_xReferenceListener, cssu::UNO_QUERY);
962 xSignatureVerifyResultBroadcaster->addSignatureVerifyResultListener(pListener);
965 cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
966 (m_xSAXEventKeeper, cssu::UNO_QUERY);
967 xReferenceResolvedBroadcaster->addReferenceResolvedListener(
968 m_nSignatureElementCollectorId, m_xReferenceListener);
971 void SignatureEntity::addReferenceURI(const rtl::OUString& ouUri)
973 m_vReferenceIds.push_back(ouUri);
976 void SignatureEntity::setReferenceNumber() const
978 cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
979 (m_xReferenceListener, cssu::UNO_QUERY);
980 xReferenceCollector->setReferenceCount(m_vReferenceIds.size());
983 bool SignatureEntity::hasReference(const rtl::OUString& ouUri) const
985 bool rc = false;
987 std::vector<const rtl::OUString>::const_iterator ii;
988 for (ii = m_vReferenceIds.begin(); ii != m_vReferenceIds.end(); ++ii)
990 if (ouUri == *ii)
992 rc = true;
993 break;
997 return rc;
1000 bool SignatureEntity::setReference(const rtl::OUString& ouUri, bool bIsExporting) const
1002 bool rc = false;
1004 if (hasReference(ouUri))
1006 int nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
1007 bIsExporting ?
1008 (cssxc::sax::ElementMarkPriority_AFTERMODIFY):
1009 (cssxc::sax::ElementMarkPriority_BEFOREMODIFY),
1010 false);
1012 m_xSAXEventKeeper->setSecurityId(nKeeperId, m_nSecurityId);
1014 cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
1015 (m_xSAXEventKeeper, cssu::UNO_QUERY);
1016 xReferenceResolvedBroadcaster->addReferenceResolvedListener(nKeeperId, m_xReferenceListener);
1018 cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
1019 (m_xReferenceListener, cssu::UNO_QUERY);
1020 xReferenceCollector->setReferenceId(nKeeperId);
1022 rc = true;
1025 return rc;
1028 /* XDocumentHandler */
1029 void SAL_CALL XSecTester::startDocument()
1030 throw (cssu::RuntimeException)
1034 void SAL_CALL XSecTester::endDocument()
1035 throw (cssu::RuntimeException)
1039 void SAL_CALL XSecTester::characters(const class rtl::OUString & chars)
1040 throw (cssu::RuntimeException)
1042 m_xExportHandler->characters(chars);
1045 void SAL_CALL XSecTester::processingInstruction(const rtl::OUString & target, const rtl::OUString &data)
1046 throw (cssu::RuntimeException)
1048 m_xExportHandler->processingInstruction(target, data);
1051 void SAL_CALL XSecTester::ignorableWhitespace(const rtl::OUString &)
1052 throw (cssu::RuntimeException)
1057 void SAL_CALL XSecTester::startElement(const rtl::OUString & name, const cssu::Reference<cssxs::XAttributeList> &xAttribs)
1058 throw (cssu::RuntimeException)
1060 rtl::OUString ouIdAttr = xAttribs->getValueByName(
1061 rtl::OUString(RTL_ASCII_USTRINGPARAM("id")));
1063 if (ouIdAttr == NULL)
1065 ouIdAttr = xAttribs->getValueByName(
1066 rtl::OUString(RTL_ASCII_USTRINGPARAM("Id")));
1069 bool bHasIdAttr = (ouIdAttr != NULL && ouIdAttr.getLength() > 0 );
1070 bool needResend = false;
1072 if (bHasIdAttr || name.equalsAscii( SIGNATURE_STR ))
1074 if (foundSecurityRelated() && ! m_bIsExporting)
1076 needResend = true;
1080 if ( !m_bIsSAXEventKeeperOnTheSAXChain )
1082 addStartAncestorEvent(name, xAttribs);
1085 bool bSuppressingForwarding = checkSecurityElement(name, xAttribs);
1087 checkReference(name, xAttribs, ouIdAttr);
1089 if (needResend)
1091 m_xSAXEventKeeper->setNextHandler(NULL);
1093 cssu::Reference<cssxs::XDocumentHandler> xSAXEventKeeperHandler
1094 (m_xSAXEventKeeper, cssu::UNO_QUERY);
1096 xSAXEventKeeperHandler->startElement(name, xAttribs);
1097 m_xSAXEventKeeper->setNextHandler(this);
1100 if (!bSuppressingForwarding)
1102 m_xExportHandler->startElement(name, xAttribs);
1106 void SAL_CALL XSecTester::endElement(const rtl::OUString& name)
1107 throw (cssu::RuntimeException)
1109 if (!m_stCurrentPath.empty())
1111 void* pSignedInfo = m_stCurrentPath.top();
1112 bool bIsStringType = m_stCurrentPathType.top();
1114 m_stCurrentPath.pop();
1115 m_stCurrentPathType.pop();
1117 if (bIsStringType && !strcmp((const char *)pSignedInfo, SIGNEDINFO_STR))
1119 if (!m_stCurrentPath.empty())
1121 void* pSignature = m_stCurrentPath.top();
1122 bIsStringType = m_stCurrentPathType.top();
1124 if (!bIsStringType && pSignature != NULL)
1126 ((SignatureEntity *) pSignature)->setReferenceNumber();
1132 if ( !m_bIsSAXEventKeeperOnTheSAXChain )
1134 addEndAncestorEvent(name);
1137 m_xExportHandler->endElement(name);
1140 void SAL_CALL XSecTester::setDocumentLocator( const cssu::Reference<cssxs::XLocator>& )
1141 throw (cssu::RuntimeException)
1145 void XSecTester::changeOutput()
1147 if (m_bIsExporting)
1149 if (m_bIsSAXEventKeeperOnTheSAXChain)
1151 m_xExportHandler = cssu::Reference<cssxs::XDocumentHandler>
1152 (m_xSAXEventKeeper, cssu::UNO_QUERY);
1154 m_xSAXEventKeeper->setNextHandler(NULL);
1156 flushAncestorEvents(m_xExportHandler);
1158 m_xSAXEventKeeper->setNextHandler(m_xOutputHandler);
1160 else
1162 m_xExportHandler = m_xOutputHandler;
1165 else
1167 if (m_bIsSAXEventKeeperOnTheSAXChain)
1169 cssu::Reference<cssxs::XDocumentHandler> xSAXEventKeeperHandler
1170 (m_xSAXEventKeeper, cssu::UNO_QUERY);
1172 m_xSAXEventKeeper->setNextHandler(NULL);
1174 flushAncestorEvents(xSAXEventKeeperHandler);
1176 m_xSaxParser->setDocumentHandler(xSAXEventKeeperHandler);
1177 m_xSAXEventKeeper->setNextHandler(this);
1179 else
1181 m_xSaxParser->setDocumentHandler(this);
1187 bool XSecTester::foundSecurityRelated()
1189 if (m_xSAXEventKeeper == NULL)
1191 m_bIsBlocking = false;
1192 m_bIsInsideCollectedElement = false;
1194 m_xXMLDocumentWrapper = cssu::Reference<cssxw::XXMLDocumentWrapper>
1195 (mxMSF->createInstance( m_ouXMLDocumentWrapperComponentName ),
1196 cssu::UNO_QUERY);
1198 m_xSAXEventKeeper = cssu::Reference< cssxc::sax::XSecuritySAXEventKeeper >
1199 (mxMSF->createInstance( rtl::OUString::createFromAscii( SAXEVENTKEEPER_COMPONENT )),
1200 cssu::UNO_QUERY);
1202 cssu::Reference<cssl::XInitialization> xInitialization(m_xSAXEventKeeper, cssu::UNO_QUERY);
1204 cssu::Sequence <cssu::Any> arg(1);
1205 arg[0] = cssu::makeAny(m_xXMLDocumentWrapper);
1206 xInitialization->initialize(arg);
1208 cssu::Reference<cssxc::sax::XSAXEventKeeperStatusChangeBroadcaster>
1209 xSAXEventKeeperStatusChangeBroadcaster(m_xSAXEventKeeper, cssu::UNO_QUERY);
1210 xSAXEventKeeperStatusChangeBroadcaster->addSAXEventKeeperStatusChangeListener(this);
1213 bool rc = false;
1215 if (!m_bIsSAXEventKeeperOnTheSAXChain)
1217 rc = true;
1220 m_bIsSAXEventKeeperOnTheSAXChain=true;
1221 changeOutput();
1223 return rc;
1226 void XSecTester::findKeyOrReference(SecurityEntity* pSecurityEntity, const rtl::OUString& ouUri, bool bIsFindingKey)
1228 std::vector<rtl::OUString>::iterator ii_referenceURIs;
1229 std::vector<int>::iterator ii_referenceKeeperIds;
1230 std::vector<int>::iterator ii_referenceRefNums;
1232 for (ii_referenceURIs = m_vUnsolvedReferenceURIs.begin(),
1233 ii_referenceKeeperIds = m_vUnsolvedReferenceKeeperIds.begin(),
1234 ii_referenceRefNums = m_vUnsolvedReferenceRefNums.begin();
1235 ii_referenceURIs != m_vUnsolvedReferenceURIs.end(); )
1237 rtl::OUString ouReferenceUri = *ii_referenceURIs;
1239 if (ouReferenceUri == ouUri)
1241 int nKeeperId = *ii_referenceKeeperIds;
1242 int nRefNum = *ii_referenceRefNums;
1244 if ( bIsFindingKey )
1246 int nClonedKeeperId = m_xSAXEventKeeper->cloneElementCollector(
1247 nKeeperId,
1248 m_bIsExporting?
1249 (cssxc::sax::ElementMarkPriority_BEFOREMODIFY):
1250 (cssxc::sax::ElementMarkPriority_AFTERMODIFY));
1252 pSecurityEntity->setKeyId(nClonedKeeperId);
1254 m_xSAXEventKeeper->setSecurityId(nClonedKeeperId, pSecurityEntity->getSecurityId());
1256 cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster>
1257 xReferenceResolvedBroadcaster(m_xSAXEventKeeper, cssu::UNO_QUERY);
1258 xReferenceResolvedBroadcaster->addReferenceResolvedListener(
1259 nClonedKeeperId,
1260 pSecurityEntity->getReferenceListener());
1262 else
1264 int nClonedKeeperId = m_xSAXEventKeeper->cloneElementCollector(
1265 nKeeperId,
1266 m_bIsExporting?
1267 (cssxc::sax::ElementMarkPriority_AFTERMODIFY):
1268 (cssxc::sax::ElementMarkPriority_BEFOREMODIFY));
1270 m_xSAXEventKeeper->setSecurityId(nClonedKeeperId, pSecurityEntity->getSecurityId());
1272 cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster>
1273 xReferenceResolvedBroadcaster
1274 (m_xSAXEventKeeper, cssu::UNO_QUERY);
1275 xReferenceResolvedBroadcaster->addReferenceResolvedListener(
1276 nClonedKeeperId,
1277 pSecurityEntity->getReferenceListener());
1279 cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
1280 (pSecurityEntity->getReferenceListener(), cssu::UNO_QUERY);
1281 xReferenceCollector->setReferenceId(nClonedKeeperId);
1284 nRefNum--;
1285 if (nRefNum == 0)
1287 m_xSAXEventKeeper->removeElementCollector(nKeeperId);
1289 ii_referenceURIs = m_vUnsolvedReferenceURIs.erase(ii_referenceURIs);
1290 ii_referenceKeeperIds = m_vUnsolvedReferenceKeeperIds.erase(ii_referenceKeeperIds);
1291 ii_referenceRefNums = m_vUnsolvedReferenceRefNums.erase(ii_referenceRefNums);
1293 else
1295 (*ii_referenceRefNums) = nRefNum;
1297 ii_referenceURIs++;
1298 ii_referenceKeeperIds++;
1299 ii_referenceRefNums++;
1302 if (bIsFindingKey)
1304 break;
1307 else
1309 ii_referenceURIs++;
1310 ii_referenceKeeperIds++;
1311 ii_referenceRefNums++;
1316 bool XSecTester::checkSecurityElement(
1317 const rtl::OUString& ouLocalName,
1318 const cssu::Reference<cssxs::XAttributeList>& xAttribs)
1320 bool rc = false;
1322 if (ouLocalName.equalsAscii(SIGNATURE_STR))
1324 SignatureEntity* pSignatureEntity = new SignatureEntity(
1325 m_xSAXEventKeeper,
1326 m_bIsExporting,
1327 this,
1328 m_xXMLSecurityContext,
1329 m_xXMLSignature,
1330 mxMSF);
1332 m_vSignatureList.push_back(pSignatureEntity);
1334 m_stCurrentPath.push(pSignatureEntity);
1335 m_stCurrentPathType.push(false);
1337 else if (ouLocalName.equalsAscii(REFERENCE_STR))
1339 if (!m_stCurrentPath.empty())
1341 void* pSignedInfo = m_stCurrentPath.top();
1342 bool bIsStringType = m_stCurrentPathType.top();
1344 m_stCurrentPath.pop();
1345 m_stCurrentPathType.pop();
1347 if (bIsStringType && !m_stCurrentPath.empty())
1349 void* pSignature = m_stCurrentPath.top();
1350 bool bIsStringType2 = m_stCurrentPathType.top();
1352 if (!strcmp((const char*)pSignedInfo, SIGNEDINFO_STR) && !bIsStringType2)
1354 rtl::OUString ouUri = xAttribs->getValueByName
1355 (rtl::OUString(RTL_ASCII_USTRINGPARAM( URI_ATTR_STR )));
1357 if (ouUri.matchAsciiL("#", 1, 0))
1359 rtl::OUString uri = ouUri.copy(1);
1360 SignatureEntity* pSignatureEntity = (SignatureEntity *)pSignature;
1362 if (uri != NULL && uri.getLength()>0)
1364 pSignatureEntity->addReferenceURI(uri);
1365 findKeyOrReference(pSignatureEntity, uri, true);
1370 m_stCurrentPath.push(pSignedInfo);
1371 m_stCurrentPathType.push(bIsStringType);
1373 m_stCurrentPath.push( (void *)REFERENCE_STR);
1374 m_stCurrentPathType.push(true);
1376 else if(ouLocalName.equalsAscii(KEYVALUE_STR) ||
1377 ouLocalName.equalsAscii(KEYNAME_STR) ||
1378 ouLocalName.equalsAscii(X509DATA_STR) ||
1379 ouLocalName.equalsAscii(ENCRYPTEDKEY_STR))
1381 if (!m_stCurrentPath.empty())
1383 void* pKeyInfo = m_stCurrentPath.top();
1384 bool bIsStringType = m_stCurrentPathType.top();
1386 m_stCurrentPath.pop();
1387 m_stCurrentPathType.pop();
1389 if (bIsStringType && !m_stCurrentPath.empty())
1391 bool bIsStringType2 = m_stCurrentPathType.top();
1393 if (!bIsStringType2)
1395 SecurityEntity *pSecurityEntity =
1396 (SecurityEntity *) (m_stCurrentPath.top());
1397 pSecurityEntity->setKeyId(0);
1401 m_stCurrentPath.push(pKeyInfo);
1402 m_stCurrentPathType.push(bIsStringType);
1405 m_stCurrentPath.push((void *)KEYVALUE_STR);
1406 m_stCurrentPathType.push(true);
1408 else if(ouLocalName.equalsAscii(RETRIEVALMETHOD_STR))
1410 if (!m_stCurrentPath.empty())
1412 void* pKeyInfo = m_stCurrentPath.top();
1413 bool bIsStringType = m_stCurrentPathType.top();
1415 m_stCurrentPath.pop();
1416 m_stCurrentPathType.pop();
1418 if (bIsStringType && !m_stCurrentPath.empty())
1420 bool bIsStringType2 = m_stCurrentPathType.top();
1422 if (!bIsStringType2)
1424 SecurityEntity *pSecurityEntity =
1425 (SecurityEntity *) m_stCurrentPath.top();
1426 rtl::OUString ouUri = xAttribs->getValueByName(
1427 rtl::OUString(RTL_ASCII_USTRINGPARAM( URI_ATTR_STR )));
1429 if (!strcmp((const char *)pKeyInfo, KEYINFO_STR) &&
1430 ouUri != NULL && ouUri.getLength()>0)
1432 pSecurityEntity->setKeyURI(ouUri);
1433 findKeyOrReference(pSecurityEntity, ouUri, true);
1439 m_stCurrentPath.push(pKeyInfo);
1440 m_stCurrentPathType.push(bIsStringType);
1443 m_stCurrentPath.push((void *)RETRIEVALMETHOD_STR);
1444 m_stCurrentPathType.push(true);
1446 else if(ouLocalName.equalsAscii(KEYINFO_STR))
1448 m_stCurrentPath.push((void *)KEYINFO_STR);
1449 m_stCurrentPathType.push(true);
1451 else if(ouLocalName.equalsAscii(SIGNEDINFO_STR))
1453 m_stCurrentPath.push((void *)SIGNEDINFO_STR);
1454 m_stCurrentPathType.push(true);
1456 else
1458 m_stCurrentPath.push((void *)OTHER_ELEMENT_STR);
1459 m_stCurrentPathType.push(true);
1462 return rc;
1465 void XSecTester::checkReference(
1466 const rtl::OUString& ouLocalName,
1467 const cssu::Reference<cssxs::XAttributeList>& xAttribs,
1468 const rtl::OUString& ouId)
1470 rtl::OUString refNumStr =
1471 xAttribs->getValueByName(rtl::OUString(RTL_ASCII_USTRINGPARAM(REFNUM_ATTR_STR)));
1473 if (ouId != NULL && ouId.getLength()>0 )
1475 int nRefNum = 999;
1476 if (refNumStr != NULL && refNumStr.getLength()>0 )
1478 nRefNum = refNumStr.toInt32();
1481 int nLength = m_vSignatureList.size();
1482 for (int i = 0; i<nLength; ++i)
1484 SignatureEntity* pSignatureEntity = m_vSignatureList.at(i);
1486 if (pSignatureEntity->setReference(ouId, m_bIsExporting))
1488 nRefNum--;
1491 if (pSignatureEntity->setKey(ouId, m_bIsExporting))
1493 nRefNum--;
1497 if (nRefNum>0)
1499 int nKeeperId;
1501 if (ouLocalName.equalsAscii(ENCRYPTEDKEY_STR))
1503 nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
1504 m_bIsExporting ?
1505 (cssxc::sax::ElementMarkPriority_BEFOREMODIFY):
1506 (cssxc::sax::ElementMarkPriority_AFTERMODIFY),
1507 true);
1509 else
1511 nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
1512 m_bIsExporting?
1513 (cssxc::sax::ElementMarkPriority_AFTERMODIFY):
1514 (cssxc::sax::ElementMarkPriority_BEFOREMODIFY),
1515 false);
1518 m_vUnsolvedReferenceURIs.push_back(ouId);
1519 m_vUnsolvedReferenceKeeperIds.push_back(nKeeperId);
1520 m_vUnsolvedReferenceRefNums.push_back(nRefNum);
1525 void XSecTester::endMission()
1527 while (m_vSignatureList.size() > 0)
1529 if (m_vSignatureList.size()>0)
1531 SignatureEntity * pSignatureEntity = m_vSignatureList.at(0);
1532 m_vSignatureList.erase(m_vSignatureList.begin());
1533 pSignatureEntity->endMission();
1534 delete pSignatureEntity;
1538 while (m_vUnsolvedReferenceURIs.size()>0)
1540 int nKeeperId = m_vUnsolvedReferenceKeeperIds.at(0);
1541 m_xSAXEventKeeper->removeElementCollector(nKeeperId);
1542 m_vUnsolvedReferenceURIs.erase(m_vUnsolvedReferenceURIs.begin());
1543 m_vUnsolvedReferenceKeeperIds.erase(m_vUnsolvedReferenceKeeperIds.begin());
1544 m_vUnsolvedReferenceRefNums.erase(m_vUnsolvedReferenceRefNums.begin());
1548 void XSecTester::addStartAncestorEvent(
1549 const rtl::OUString& ouName,
1550 const cssu::Reference< cssxs::XAttributeList >& xAttribs)
1552 sal_Int32 nLength = xAttribs->getLength();
1553 AncestorEvent* ancestorEvent = new AncestorEvent( nLength );
1555 ancestorEvent->bIsStartElement = true;
1556 ancestorEvent->ouName = ouName;
1558 for (int i = 0; i<nLength; ++i)
1560 (ancestorEvent->aAttributeList[i]).sName = xAttribs->getNameByIndex((short)i);
1561 (ancestorEvent->aAttributeList[i]).sValue =xAttribs->getValueByIndex((short)i);
1564 m_vAncestorEvents.push_back(ancestorEvent);
1567 void XSecTester::addEndAncestorEvent(const rtl::OUString& ouName)
1569 AncestorEvent* ancestorEvent = new AncestorEvent(0);
1571 ancestorEvent->bIsStartElement = false;
1572 ancestorEvent->ouName = ouName;
1574 m_vAncestorEvents.push_back(ancestorEvent);
1577 void XSecTester::sendAncestorStartElementEvent(
1578 const rtl::OUString& ouName,
1579 const cssu::Sequence< cssxcsax::XMLAttribute >& attrList,
1580 const cssu::Reference< cssxs::XDocumentHandler >& xDocumentHandler) const
1582 SvXMLAttributeList* pAttributeList = new SvXMLAttributeList();
1583 cssu::Reference < cssxs::XAttributeList > xAttrList
1584 = cssu::Reference< cssxs::XAttributeList > (pAttributeList);
1586 sal_Int32 nLength = attrList.getLength();
1588 for (int i = 0; i<nLength; ++i)
1590 pAttributeList->AddAttribute( attrList[i].sName, attrList[i].sValue);
1593 xDocumentHandler->startElement(ouName, xAttrList);
1596 void XSecTester::sendAncestorEndElementEvent(
1597 const rtl::OUString& ouName,
1598 const cssu::Reference< cssxs::XDocumentHandler >& xDocumentHandler) const
1600 xDocumentHandler->endElement(ouName);
1603 std::vector< AncestorEvent* >::const_iterator XSecTester::checkAncestorStartElementEvent(
1604 const std::vector< AncestorEvent* >::const_iterator& ii,
1605 const cssu::Reference< cssxs::XDocumentHandler >& xDocumentHandler) const
1607 std::vector< AncestorEvent* >::const_iterator next = ii+1;
1609 if (next == m_vAncestorEvents.end())
1611 sendAncestorStartElementEvent(
1612 (*ii)->ouName, (*ii)->aAttributeList, xDocumentHandler);
1614 else
1616 while ((next != m_vAncestorEvents.end()) && ((*next)->bIsStartElement))
1618 next = checkAncestorStartElementEvent(next, xDocumentHandler);
1621 if (next != m_vAncestorEvents.end())
1623 next++;
1627 return next;
1630 void XSecTester::flushAncestorEvents(
1631 const cssu::Reference< cssxs::XDocumentHandler >& xDocumentHandler)
1633 std::vector< AncestorEvent* >::const_iterator ii;
1635 if (xDocumentHandler != NULL)
1637 ii = m_vAncestorEvents.begin();
1639 while (ii != m_vAncestorEvents.end())
1641 AncestorEvent* ancestorEvent = *ii;
1643 if (ancestorEvent->bIsStartElement)
1645 ii = checkAncestorStartElementEvent(ii, xDocumentHandler);
1647 else
1649 sendAncestorEndElementEvent((*ii)->ouName, xDocumentHandler);
1650 ii++;
1655 /* free the ancestor events list */
1656 std::vector< AncestorEvent* >::iterator jj;
1658 while (m_vAncestorEvents.size()>0)
1660 jj = m_vAncestorEvents.begin();
1661 delete *jj;
1662 m_vAncestorEvents.erase(jj);
1667 * Get the length of a file in a platform independant fashion
1669 int getLength(const char *pInputFileName)
1671 int nSize = 0;
1672 std::ifstream data(pInputFileName);
1674 data.seekg(0, std::ios_base::end);
1675 nSize = data.tellg();
1677 return nSize;
1680 void outputHeader()
1682 fprintf(stderr, "%16s%4s%8s%12s%12s%12s%12s\n", "File Name", "E/I", "Size", "-C++", "-Java", "Forw-O", "No S/E");
1683 fprintf(stderr, "===============================================================================\n");
1687 * print the output on the screen as well as in the GNUPlot data file
1689 void output(const rtl::OUString& ouInputFileName,
1690 const rtl::OUString& ouTime_C,
1691 const rtl::OUString& ouTime_Java,
1692 const rtl::OUString& ouTime_NoSecurity,
1693 const rtl::OUString& ouTime_JavaForwardOnly,
1694 const rtl::OUString& ouRemark_C,
1695 const rtl::OUString& ouRemark_Java,
1696 bool bIsExporting)
1698 int nSize = getLength(rtl::OString(ouInputFileName, ouInputFileName.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1699 std::ofstream data;
1701 /* print screen */
1702 int nPosition = ouInputFileName.lastIndexOf('\\');
1703 rtl::OUString fileName = ouInputFileName.copy(nPosition + 1);
1705 fprintf(stderr, "%16s", rtl::OString(fileName, fileName.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1707 fprintf(stderr, "%4s", bIsExporting?"E":"I");
1708 fprintf(stderr, "%7dK", nSize/1024);
1709 fprintf(stderr, "%8s %3s",
1710 rtl::OString(ouTime_C, ouTime_C.getLength(), RTL_TEXTENCODING_ASCII_US).getStr(),
1711 rtl::OString(ouRemark_C, ouRemark_C.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1712 fprintf(stderr, "%8s %3s",
1713 rtl::OString(ouTime_Java, ouTime_Java.getLength(), RTL_TEXTENCODING_ASCII_US).getStr(),
1714 rtl::OString(ouRemark_Java, ouRemark_Java.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1715 fprintf(stderr, "%12s", rtl::OString(ouTime_JavaForwardOnly, ouTime_JavaForwardOnly.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1716 fprintf(stderr, "%12s", rtl::OString(ouTime_NoSecurity, ouTime_NoSecurity.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1717 fprintf(stderr, "\n");
1719 /* output the data as GNUPlot data file */
1721 char str[32];
1722 sprintf(str, "%d %s", nSize, rtl::OString(ouTime_C, ouTime_C.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1723 data.open("d:\\time_vs_size.txt", std::ios::app);
1724 data << str << std::endl;
1725 data.close();
1727 sprintf(str, "%d %s", nSize, rtl::OString(ouTime_Java, ouTime_Java.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1728 data.open("d:\\time_vs_size_without_sec.txt", std::ios::app);
1729 data << str << std::endl;
1730 data.close();
1734 int main( int argc, char **argv )
1736 if (argc < 3)
1738 fprintf(stderr, "Usage: testtool <exportbatchfile> <importbatchfile> [<cppcryptotoken>] [<javacryptotoken>]\n");
1739 exit (-1);
1742 rtl::OUString aExportBatchFile = rtl::OUString::createFromAscii(argv[1]);
1743 rtl::OUString aImportBatchFile = rtl::OUString::createFromAscii(argv[2]);
1744 rtl::OUString aCPPCryptoToken;
1745 if ( argc > 3 )
1746 aCPPCryptoToken = rtl::OUString::createFromAscii(argv[3]);
1747 rtl::OUString aJavaCryptoToken;
1748 if ( argc > 4 )
1749 aJavaCryptoToken = rtl::OUString::createFromAscii(argv[4]);
1751 try
1753 uno::Reference< lang::XMultiServiceFactory > xMSF = CreateDemoServiceFactory();
1755 XSecTester* pTester = new XSecTester( xMSF );
1756 uno::Reference< xml::sax::XDocumentHandler > xKeepARef = pTester;
1758 pTester->setCryptoDir( aJavaCryptoToken, aCPPCryptoToken );
1760 rtl::OUString ouTime_C, ouTime_Java, ouTime_NoSecurity, ouTime_JavaForwardOnly;
1761 rtl::OUString ouInputFileName;
1762 rtl::OUString outputFileName1;
1763 rtl::OUString outputFileName2;
1764 rtl::OUString ouRemark_C, ouRemark_Java;
1766 outputHeader();
1768 std::ifstream batch_export, batch_import;
1770 batch_export.open(OUStringToOString( aExportBatchFile, RTL_TEXTENCODING_ASCII_US ).getStr());
1772 const int MAX_LINE = 80;
1773 char line[MAX_LINE + 1];
1775 while (batch_export.getline(line, MAX_LINE))
1777 ouInputFileName = rtl::OUString::createFromAscii(line);
1778 int nPosition = ouInputFileName.lastIndexOf('.');
1779 int nPosition1;
1782 * export the file with signautre/encryption (C++)
1784 outputFileName1 = ouInputFileName.copy(0, nPosition) +
1785 rtl::OUString::createFromAscii("-ex.xml");
1786 ouTime_C = pTester->export_xml(ouInputFileName, outputFileName1, sal_False);
1787 nPosition1 = ouTime_C.lastIndexOf('\t');
1788 ouRemark_C = ouTime_C.copy(nPosition1 + 1);
1789 ouTime_C = ouTime_C.copy(0, nPosition1);
1792 * export the file with signautre/encryption (Java)
1794 outputFileName1 = ouInputFileName.copy(0, nPosition) +
1795 rtl::OUString::createFromAscii("-ex2.xml");
1796 ouTime_Java = pTester->export_xml(ouInputFileName, outputFileName1, sal_True);
1797 nPosition1 = ouTime_Java.lastIndexOf('\t');
1798 ouRemark_Java = ouTime_Java.copy(nPosition1 + 1);
1799 ouTime_Java = ouTime_Java.copy(0, nPosition1);
1802 * export the file without signautre/encryption
1804 outputFileName2 = ouInputFileName.copy(0, nPosition) +
1805 rtl::OUString::createFromAscii("-ex-no.xml");
1806 ouTime_NoSecurity = pTester->transfer_without_sec(ouInputFileName, outputFileName2, sal_False);
1809 * export the file with Java Flat Filter
1811 outputFileName2 = ouInputFileName.copy(0, nPosition) +
1812 rtl::OUString::createFromAscii("-ex-jf.xml");
1813 ouTime_JavaForwardOnly = pTester->transfer_without_sec(ouInputFileName, outputFileName2, sal_True);
1816 * print output
1818 output(ouInputFileName, ouTime_C, ouTime_Java, ouTime_NoSecurity, ouTime_JavaForwardOnly, ouRemark_C, ouRemark_Java, true);
1821 batch_export.close();
1823 batch_import.open(OUStringToOString( aImportBatchFile, RTL_TEXTENCODING_ASCII_US ).getStr());
1825 while (batch_import.getline(line, MAX_LINE))
1827 ouInputFileName = rtl::OUString::createFromAscii(line);
1828 int nPosition = ouInputFileName.lastIndexOf('.');
1829 int nPosition1;
1832 * import the file with signautre/encryption (C++)
1834 outputFileName1 = ouInputFileName.copy(0, nPosition) +
1835 rtl::OUString::createFromAscii("-im.xml");
1836 ouTime_C = pTester->import_xml(ouInputFileName, outputFileName1, sal_False);
1837 nPosition1 = ouTime_C.lastIndexOf('\t');
1838 ouRemark_C = ouTime_C.copy(nPosition1 + 1);
1839 ouTime_C = ouTime_C.copy(0, nPosition1);
1842 * import the file with signautre/encryption (Java)
1844 outputFileName1 = ouInputFileName.copy(0, nPosition) +
1845 rtl::OUString::createFromAscii("-im2.xml");
1846 ouTime_Java = pTester->import_xml(ouInputFileName, outputFileName1, sal_True);
1847 nPosition1 = ouTime_Java.lastIndexOf('\t');
1848 ouRemark_Java = ouTime_Java.copy(nPosition1 + 1);
1849 ouTime_Java = ouTime_Java.copy(0, nPosition1);
1852 * import the file without signautre/encryption
1854 outputFileName2 = ouInputFileName.copy(0, nPosition) +
1855 rtl::OUString::createFromAscii("-im-no.xml");
1856 ouTime_NoSecurity = pTester->transfer_without_sec(ouInputFileName, outputFileName2, sal_False);
1859 * import the file without signautre/encryption
1862 outputFileName2 = ouInputFileName.copy(0, nPosition) +
1863 rtl::OUString::createFromAscii("-im-jf.xml");
1864 ouTime_JavaForwardOnly = pTester->transfer_without_sec(ouInputFileName, outputFileName2, sal_True);
1867 * print output
1869 output(ouInputFileName, ouTime_C, ouTime_Java, ouTime_NoSecurity, ouTime_JavaForwardOnly, ouRemark_C, ouRemark_Java, false);
1872 batch_import.close();
1874 fprintf(stderr, "\n");
1876 catch( cssu::Exception& e )
1878 fprintf( stderr , "\nEXCEPTION! Error Message: %s\n" ,
1879 rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
1882 return 0;