merged tag ooo/OOO330_m14
[LibreOffice.git] / xmlsecurity / tools / demo / performance.cxx
blob061091611de7c27ff5dc3223eb8d2024c83f1f35
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
31 #include <stdio.h>
32 #include <string.h>
34 #include <rtl/ustring.hxx>
35 #include <cppuhelper/bootstrap.hxx>
36 #include <cppuhelper/servicefactory.hxx>
37 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
38 #include <com/sun/star/registry/XImplementationRegistration.hpp>
39 #include <com/sun/star/beans/XPropertySet.hpp>
40 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
42 #include <comphelper/processfactory.hxx>
44 #include <iostream>
45 #include <fstream>
47 #include <util.hxx>
49 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
50 #include <com/sun/star/io/XOutputStream.hpp>
51 #include <com/sun/star/io/XInputStream.hpp>
52 #include <com/sun/star/xml/sax/XParser.hpp>
53 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
54 #include <com/sun/star/xml/sax/XAttributeList.hpp>
55 #include <cppuhelper/implbase4.hxx>
57 #include <com/sun/star/xml/crypto/sax/XSignatureCreationResultListener.hpp>
58 #include <com/sun/star/xml/crypto/sax/XSignatureVerifyResultListener.hpp>
59 #include <com/sun/star/xml/crypto/sax/XSAXEventKeeperStatusChangeListener.hpp>
60 #include <com/sun/star/xml/crypto/sax/XSecuritySAXEventKeeper.hpp>
61 #include <com/sun/star/xml/crypto/sax/XReferenceResolvedListener.hpp>
62 #include <com/sun/star/xml/crypto/XXMLSignature.hpp>
63 #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
64 #include <com/sun/star/xml/csax/XMLAttribute.hpp>
65 #include <com/sun/star/xml/crypto/XSEInitializer.hpp>
66 #include <com/sun/star/xml/crypto/SecurityOperationStatus.hpp>
67 #include <com/sun/star/io/XActiveDataSource.hpp>
68 #include <com/sun/star/lang/XInitialization.hpp>
69 #include <com/sun/star/xml/crypto/sax/XKeyCollector.hpp>
70 #include <com/sun/star/xml/crypto/sax/ElementMarkPriority.hpp>
71 #include <com/sun/star/xml/crypto/sax/XReferenceResolvedBroadcaster.hpp>
72 #include <com/sun/star/xml/crypto/sax/XMissionTaker.hpp>
73 #include <com/sun/star/xml/crypto/sax/XBlockerMonitor.hpp>
74 #include <com/sun/star/xml/crypto/sax/XSignatureCreationResultBroadcaster.hpp>
75 #include <com/sun/star/xml/crypto/sax/XSignatureVerifyResultBroadcaster.hpp>
76 #include <com/sun/star/xml/crypto/sax/XReferenceCollector.hpp>
77 #include <com/sun/star/xml/crypto/sax/XSAXEventKeeperStatusChangeBroadcaster.hpp>
78 #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
80 #include <xmloff/attrlist.hxx>
82 //#include <malloc.h>
83 #include <stdio.h>
84 #include <stdlib.h>
85 #include <string.h>
88 * Can not build under solaris.
89 * Delete the memory.h including by AF
90 #include <memory.h>
93 #include <sys/types.h>
94 #include <sys/stat.h>
95 #include <osl/time.h>
99 #ifndef INCLUDED_VECTOR
100 #include <vector>
101 #define INCLUDED_VECTOR
102 #endif
104 #ifndef INCLUDED_STACK
105 #include <stack>
106 #define INCLUDED_STACK
107 #endif
109 /* xml security framework components */
110 #define SIGNATURECREATOR_COMPONENT "com.sun.star.xml.crypto.sax.SignatureCreator"
111 #define SIGNATUREVERIFIER_COMPONENT "com.sun.star.xml.crypto.sax.SignatureVerifier"
112 #define JAVAFLATFILTER_COMPONENT "com.sun.star.xml.crypto.eval.JavaFlatFilter"
113 #define SAXEVENTKEEPER_COMPONENT "com.sun.star.xml.crypto.sax.SAXEventKeeper"
115 /* java based bridge components */
116 #define SEINITIALIZER_JAVA_COMPONENT "com.sun.star.xml.security.bridge.jxsec.SEInitializer_JxsecImpl"
117 #define XMLSIGNATURE_JAVA_COMPONENT "com.sun.star.xml.security.bridge.jxsec.XMLSignature_JxsecImpl"
118 #define XMLDOCUMENTWRAPPER_JAVA_COMPONENT "com.sun.star.xml.security.bridge.jxsec.XMLDocumentWrapper_JxsecImpl"
120 /* c based bridge components */
121 #define SEINITIALIZER_C_COMPONENT "com.sun.star.xml.crypto.SEInitializer"
122 #define XMLSIGNATURE_C_COMPONENT "com.sun.star.xml.crypto.XMLSignature"
123 #define XMLDOCUMENT_C_COMPONENT "com.sun.star.xml.wrapper.XMLDocumentWrapper"
125 /* security related elements and attributes */
126 #define SIGNATURE_STR "Signature"
127 #define REFERENCE_STR "Reference"
128 #define SIGNEDINFO_STR "SignedInfo"
129 #define KEYINFO_STR "KeyInfo"
130 #define KEYVALUE_STR "KeyValue"
131 #define KEYNAME_STR "KeyName"
132 #define X509DATA_STR "X509Data"
133 #define ENCRYPTEDKEY_STR "EncryptedKey"
134 #define RETRIEVALMETHOD_STR "RetrievalMethod"
135 #define OTHER_ELEMENT_STR "OTHER_ELEMENT_STR"
136 #define REFNUM_ATTR_STR "refNum"
137 #define URI_ATTR_STR "URI"
140 #define RTL_ASCII_USTRINGPARAM( asciiStr ) asciiStr, strlen( asciiStr ), RTL_TEXTENCODING_ASCII_US
142 namespace cssu = com::sun::star::uno;
143 namespace cssl = com::sun::star::lang;
144 namespace cssb = com::sun::star::beans;
145 namespace cssi = com::sun::star::io;
146 namespace cssxc = com::sun::star::xml::crypto;
147 namespace cssxs = com::sun::star::xml::sax;
148 namespace cssxw = com::sun::star::xml::wrapper;
149 namespace cssxcsax = com::sun::star::xml::csax;
152 using namespace ::com::sun::star;
155 class XSecTester;
158 * The XSecTester class is a C++ version of SecurityFramworkController.java
162 class SecurityEntity
164 private:
165 static int m_nNextSecurityId;
166 rtl::OUString m_ouKeyURI;
168 protected:
169 com::sun::star::uno::Reference<
170 com::sun::star::lang::XMultiServiceFactory > mxMSF;
172 com::sun::star::uno::Reference<
173 com::sun::star::xml::crypto::sax::XReferenceResolvedListener >
174 m_xReferenceListener;
176 com::sun::star::uno::Reference<
177 com::sun::star::xml::crypto::sax::XSecuritySAXEventKeeper >
178 m_xSAXEventKeeper;
180 com::sun::star::uno::Reference<
181 com::sun::star::xml::crypto::XXMLSecurityContext >
182 m_xXMLSecurityContext;
184 com::sun::star::uno::Reference<
185 com::sun::star::xml::crypto::XXMLSignature >
186 m_xXMLSignature;
188 int m_nSecurityId;
190 private:
191 int getNextSecurityId() const;
193 protected:
194 SecurityEntity(
195 const com::sun::star::uno::Reference<
196 com::sun::star::xml::crypto::sax::XSecuritySAXEventKeeper >&
197 xSAXEventKeeper,
198 const com::sun::star::uno::Reference<
199 com::sun::star::xml::crypto::XXMLSecurityContext >&
200 xXMLSecurityContext,
201 const com::sun::star::uno::Reference<
202 com::sun::star::xml::crypto::XXMLSignature >&
203 xXMLSignature,
204 const com::sun::star::uno::Reference<
205 com::sun::star::lang::XMultiServiceFactory >&
206 rsMSF);
208 public:
209 void setKeyId(int nId);
211 int getSecurityId() const;
213 com::sun::star::uno::Reference<
214 com::sun::star::xml::crypto::sax::XReferenceResolvedListener >
215 getReferenceListener() const;
217 bool setKey( const rtl::OUString& ouUri, bool bIsExporting );
219 void setKeyURI(const rtl::OUString& ouUri);
221 bool endMission();
225 class SignatureEntity : public SecurityEntity
227 private:
228 std::vector< rtl::OUString > m_vReferenceIds;
229 int m_nSignatureElementCollectorId;
231 bool hasReference(const rtl::OUString& ouUri) const;
233 public:
234 SignatureEntity(
235 const com::sun::star::uno::Reference<
236 com::sun::star::xml::crypto::sax::XSecuritySAXEventKeeper >&
237 xSAXEventKeeper,
238 bool bIsExporting,
239 XSecTester* pListener,
240 const com::sun::star::uno::Reference<
241 com::sun::star::xml::crypto::XXMLSecurityContext >&
242 xXMLSecurityContext,
243 const com::sun::star::uno::Reference<
244 com::sun::star::xml::crypto::XXMLSignature >&
245 xXMLSignature,
246 const com::sun::star::uno::Reference<
247 com::sun::star::lang::XMultiServiceFactory >&
248 rsMSF);
249 ~SignatureEntity(){};
251 void setReferenceNumber() const;
252 bool setReference( const rtl::OUString& ouUri, bool bIsExporting ) const;
253 void addReferenceURI( const rtl::OUString& ouUri );
256 struct AncestorEvent
258 AncestorEvent( sal_Int32 nAttrNum ):aAttributeList(nAttrNum){};
260 bool bIsStartElement;
261 rtl::OUString ouName;
263 com::sun::star::uno::Sequence<
264 com::sun::star::xml::csax::XMLAttribute >
265 aAttributeList;
268 class XSecTester : public cppu::WeakImplHelper4
270 com::sun::star::xml::crypto::sax::XSignatureCreationResultListener,
271 com::sun::star::xml::crypto::sax::XSignatureVerifyResultListener,
272 com::sun::star::xml::crypto::sax::XSAXEventKeeperStatusChangeListener,
273 com::sun::star::xml::sax::XDocumentHandler
276 private:
277 com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory > mxMSF;
279 sal_Int32 m_nTotalSignatureNumber;
280 sal_Int32 m_nSuccessfulSignatureNumber;
282 com::sun::star::uno::Reference<
283 com::sun::star::xml::sax::XDocumentHandler >
284 m_xExportHandler;
286 com::sun::star::uno::Reference<
287 com::sun::star::xml::crypto::sax::XSecuritySAXEventKeeper >
288 m_xSAXEventKeeper;
290 com::sun::star::uno::Reference<
291 com::sun::star::xml::wrapper::XXMLDocumentWrapper >
292 m_xXMLDocumentWrapper;
294 com::sun::star::uno::Reference<
295 com::sun::star::xml::sax::XDocumentHandler >
296 m_xOutputHandler;
298 com::sun::star::uno::Reference<
299 com::sun::star::xml::sax::XParser >
300 m_xSaxParser;
302 std::stack< void* > m_stCurrentPath;
303 std::stack< bool > m_stCurrentPathType;
305 std::vector< AncestorEvent* > m_vAncestorEvents;
306 std::vector< SignatureEntity* > m_vSignatureList;
308 std::vector< rtl::OUString > m_vUnsolvedReferenceURIs;
309 std::vector< int > m_vUnsolvedReferenceKeeperIds;
310 std::vector< int > m_vUnsolvedReferenceRefNums;
312 bool m_bIsExporting;
313 bool m_bIsBlocking;
315 bool m_bIsInsideCollectedElement;
316 bool m_bIsSAXEventKeeperOnTheSAXChain;
318 com::sun::star::uno::Reference<
319 com::sun::star::xml::crypto::XXMLSecurityContext >
320 m_xXMLSecurityContext;
322 com::sun::star::uno::Reference<
323 com::sun::star::xml::crypto::XXMLSignature >
324 m_xXMLSignature;
326 rtl::OUString m_ouJavaCryptokenDir;
327 rtl::OUString m_ouCCryptokenDir;
328 rtl::OUString m_ouXMLDocumentWrapperComponentName;
330 private:
331 com::sun::star::uno::Reference<
332 com::sun::star::io::XOutputStream >
333 createOutputStream( const rtl::OUString& ouFile );
335 rtl::OUString parseFile(
336 const rtl::OUString& ouInputFileName,
337 const rtl::OUString& ouOutputFileName,
338 bool bIsExporting,
339 bool bIsJavaBased);
341 void changeOutput();
343 bool foundSecurityRelated();
345 void findKeyOrReference(SecurityEntity* pSecurityEntity, const rtl::OUString& ouUri, bool bIsFindKey);
347 bool checkSecurityElement(
348 const rtl::OUString& ouLocalName,
349 const com::sun::star::uno::Reference<
350 com::sun::star::xml::sax::XAttributeList>& xAttribs);
352 void checkReference(
353 const rtl::OUString& ouLocalName,
354 const com::sun::star::uno::Reference<
355 com::sun::star::xml::sax::XAttributeList>& xAttribs,
356 const rtl::OUString& ouId);
358 void endMission();
360 void addStartAncestorEvent(
361 const rtl::OUString& ouName,
362 const com::sun::star::uno::Reference<
363 com::sun::star::xml::sax::XAttributeList>& xAttribs);
365 void addEndAncestorEvent( const rtl::OUString& ouName );
367 void flushAncestorEvents(
368 const com::sun::star::uno::Reference<
369 com::sun::star::xml::sax::XDocumentHandler >& xDocumentHandler);
371 void XSecTester::sendAncestorStartElementEvent(
372 const rtl::OUString& ouName,
373 const com::sun::star::uno::Sequence<
374 com::sun::star::xml::csax::XMLAttribute >& xAttrList,
375 const com::sun::star::uno::Reference<
376 com::sun::star::xml::sax::XDocumentHandler >& xDocumentHandler) const;
378 void XSecTester::sendAncestorEndElementEvent(
379 const rtl::OUString& ouName,
380 const com::sun::star::uno::Reference<
381 com::sun::star::xml::sax::XDocumentHandler >& xDocumentHandler) const;
383 std::vector< AncestorEvent* >::const_iterator XSecTester::checkAncestorStartElementEvent(
384 const std::vector< AncestorEvent* >::const_iterator& ii,
385 const com::sun::star::uno::Reference<
386 com::sun::star::xml::sax::XDocumentHandler >& xDocumentHandler) const;
388 public:
389 XSecTester(const com::sun::star::uno::Reference<
390 com::sun::star::lang::XMultiServiceFactory >& rxMSF)
391 :mxMSF( rxMSF ){};
392 virtual ~XSecTester(){};
394 /* XSignatureCreationResultListener */
395 virtual void SAL_CALL signatureCreated(
396 sal_Int32 securityId,
397 com::sun::star::xml::crypto::SecurityOperationStatus creationResult )
398 throw (com::sun::star::uno::RuntimeException);
400 /* XSignatureVerifyResultListener */
401 virtual void SAL_CALL signatureVerified(
402 sal_Int32 securityId,
403 com::sun::star::xml::crypto::SecurityOperationStatus verifyResult )
404 throw (com::sun::star::uno::RuntimeException);
406 /* XSAXEventKeeperStatusChangeListener */
407 virtual void SAL_CALL blockingStatusChanged( sal_Bool isBlocking )
408 throw (com::sun::star::uno::RuntimeException);
409 virtual void SAL_CALL collectionStatusChanged(
410 sal_Bool isInsideCollectedElement )
411 throw (com::sun::star::uno::RuntimeException);
412 virtual void SAL_CALL bufferStatusChanged( sal_Bool isBufferEmpty )
413 throw (com::sun::star::uno::RuntimeException);
415 /* XXMLSecTester */
416 virtual rtl::OUString SAL_CALL transfer_without_sec(
417 const rtl::OUString& inputFileName,
418 const rtl::OUString& outputFileName,
419 sal_Bool isBridgeInvolved)
420 throw (com::sun::star::uno::RuntimeException);
421 virtual rtl::OUString SAL_CALL export_xml(
422 const rtl::OUString& inputFileName,
423 const rtl::OUString& outputFileName,
424 sal_Bool isJavaBased)
425 throw (com::sun::star::uno::RuntimeException);
426 virtual rtl::OUString SAL_CALL import_xml(
427 const rtl::OUString& inputFileName,
428 const rtl::OUString& outputFileName,
429 sal_Bool isJavaBased)
430 throw (com::sun::star::uno::RuntimeException);
432 virtual void SAL_CALL setCryptoDir(
433 const rtl::OUString & javaDirName,
434 const rtl::OUString & cDirName)
435 throw (com::sun::star::uno::RuntimeException);
437 /* XDocumentHandler */
438 virtual void SAL_CALL endDocument()
439 throw (com::sun::star::uno::RuntimeException);
440 virtual void SAL_CALL startDocument()
441 throw (com::sun::star::uno::RuntimeException);
442 virtual void SAL_CALL characters(const class rtl::OUString&)
443 throw (com::sun::star::uno::RuntimeException);
444 virtual void SAL_CALL processingInstruction(const rtl::OUString&, const rtl::OUString&)
445 throw (com::sun::star::uno::RuntimeException);
446 virtual void SAL_CALL ignorableWhitespace(const rtl::OUString&)
447 throw (com::sun::star::uno::RuntimeException);
448 virtual void SAL_CALL startElement(
449 const rtl::OUString&,
450 const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList >&)
451 throw (com::sun::star::uno::RuntimeException);
452 virtual void SAL_CALL endElement(const rtl::OUString&)
453 throw (com::sun::star::uno::RuntimeException);
454 virtual void SAL_CALL setDocumentLocator(
455 const com::sun::star::uno::Reference< com::sun::star::xml::sax::XLocator >&)
456 throw (com::sun::star::uno::RuntimeException);
459 rtl::OUString XSecTester::parseFile(
460 const rtl::OUString& ouInputFileName,
461 const rtl::OUString& ouOutputFileName,
462 bool bIsExporting,
463 bool bIsJavaBased)
465 rtl::OUString ouMessage;
467 cssu::Reference<cssi::XInputStream> xInputStream = OpenInputStream(ouInputFileName);
469 if (xInputStream != NULL )
471 /* initialization */
472 rtl::OUString SEInitializer_comp;
473 rtl::OUString XMLSignature_comp;
474 rtl::OUString tokenPath;
475 cssu::Reference < cssxc::XSEInitializer > xSEInitializer;
477 if (bIsJavaBased)
479 SEInitializer_comp = rtl::OUString::createFromAscii( SEINITIALIZER_JAVA_COMPONENT );
480 XMLSignature_comp = rtl::OUString::createFromAscii( XMLSIGNATURE_JAVA_COMPONENT);
481 m_ouXMLDocumentWrapperComponentName = rtl::OUString::createFromAscii( XMLDOCUMENTWRAPPER_JAVA_COMPONENT );
482 tokenPath = m_ouJavaCryptokenDir;
484 else
486 SEInitializer_comp = rtl::OUString::createFromAscii( SEINITIALIZER_C_COMPONENT );
487 XMLSignature_comp = rtl::OUString::createFromAscii( XMLSIGNATURE_C_COMPONENT);
488 m_ouXMLDocumentWrapperComponentName = rtl::OUString::createFromAscii( XMLDOCUMENT_C_COMPONENT );
489 tokenPath = m_ouCCryptokenDir;
492 xSEInitializer = cssu::Reference < cssxc::XSEInitializer > (
493 mxMSF->createInstance( SEInitializer_comp ),
494 cssu::UNO_QUERY );
496 m_xXMLSignature = cssu::Reference<cssxc::XXMLSignature> (
497 mxMSF->createInstance( XMLSignature_comp ),
498 cssu::UNO_QUERY );
500 if ( xSEInitializer.is() && m_xXMLSignature.is())
502 /* create SAX Parser */
503 const rtl::OUString sSaxParser (
504 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser") );
505 m_xSaxParser = cssu::Reference < cssxs::XParser > ( mxMSF->createInstance( sSaxParser ), cssu::UNO_QUERY );
507 /* create SAX Writer */
508 const rtl::OUString sSaxWriter (
509 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer") );
510 cssu::Reference < cssi::XActiveDataSource > xSaxWriter
511 ( mxMSF->createInstance( sSaxWriter ), cssu::UNO_QUERY );
513 cssu::Reference< cssi::XOutputStream > xOutputStream = OpenOutputStream(ouOutputFileName);
514 xSaxWriter->setOutputStream( xOutputStream );
516 cssxs::InputSource aInput;
517 aInput.sSystemId = ouInputFileName;
518 aInput.aInputStream = xInputStream;
520 cssu::Reference < cssxs::XDocumentHandler > xSaxWriterHandler( xSaxWriter, cssu::UNO_QUERY);
522 m_xXMLSecurityContext =
523 xSEInitializer->createSecurityContext(tokenPath);
525 m_bIsExporting = bIsExporting;
526 m_xExportHandler = xSaxWriterHandler;
527 m_xOutputHandler = xSaxWriterHandler;
529 m_xXMLDocumentWrapper = NULL;
530 m_xSAXEventKeeper = NULL;
531 m_bIsSAXEventKeeperOnTheSAXChain = false;
533 m_bIsBlocking = false;
534 m_bIsInsideCollectedElement = false;
536 OSL_ASSERT(m_vSignatureList.size() == 0);
537 OSL_ASSERT(m_vUnsolvedReferenceURIs.size() == 0);
538 OSL_ASSERT(m_vUnsolvedReferenceKeeperIds.size() == 0);
539 OSL_ASSERT(m_vUnsolvedReferenceRefNums.size() == 0);
540 OSL_ASSERT(m_stCurrentPath.empty());
541 OSL_ASSERT(m_stCurrentPathType.empty());
542 OSL_ASSERT(m_vAncestorEvents.empty());
544 changeOutput();
546 /* foundSecurityRelated(); */
548 /* Begin to parse */
549 TimeValue startTime, endTime;
550 osl_getSystemTime( &startTime );
552 xSaxWriterHandler->startDocument();
554 if (m_bIsExporting)
556 m_xSaxParser->setDocumentHandler(this);
557 m_xSaxParser->parseStream(aInput);
559 else
561 m_xSaxParser->setDocumentHandler(this);
562 m_xSaxParser->parseStream(aInput);
565 endMission();
566 xSaxWriterHandler->endDocument();
568 osl_getSystemTime( &endTime );
570 flushAncestorEvents( NULL );
572 // Bug in SAXWriter, done in endDocument()
573 // xOutputStream->closeOutput();
574 xInputStream->closeInput();
578 * Free the security context
580 xSEInitializer->freeSecurityContext(m_xXMLSecurityContext);
581 m_xXMLSecurityContext = NULL;
583 /* Calculate the time */
584 double diff = ((double)((endTime.Nanosec + endTime.Seconds*1000000000.0)
585 - (startTime.Nanosec + startTime.Seconds*1000000000.0))) /
586 ((double)1000000000.0);
588 char buf[32];
589 sprintf(buf, "%.2f", diff);
590 ouMessage += rtl::OUString(RTL_ASCII_USTRINGPARAM(buf));
592 else
594 ouMessage += rtl::OUString::createFromAscii( "N/A" );
598 else
600 ouMessage += rtl::OUString::createFromAscii( "-" );
603 return ouMessage;
606 /* XSignatureCreationResultListener */
607 void SAL_CALL XSecTester::signatureCreated(
608 sal_Int32 securityId,
609 cssxc::SecurityOperationStatus creationResult )
610 throw (cssu::RuntimeException)
612 m_nTotalSignatureNumber++;
613 if (creationResult == cssxc::SecurityOperationStatus_OPERATION_SUCCEEDED)
615 m_nSuccessfulSignatureNumber++;
619 /* XSignatureVerifyResultListener */
620 void SAL_CALL XSecTester::signatureVerified(
621 sal_Int32 securityId,
622 cssxc::SecurityOperationStatus verifyResult )
623 throw (cssu::RuntimeException)
625 m_nTotalSignatureNumber++;
626 if (verifyResult == cssxc::SecurityOperationStatus_OPERATION_SUCCEEDED)
628 m_nSuccessfulSignatureNumber++;
632 /* XSAXEventKeeperStatusChangeListener */
633 void SAL_CALL XSecTester::blockingStatusChanged( sal_Bool isBlocking )
634 throw (cssu::RuntimeException)
636 this->m_bIsBlocking = isBlocking;
639 void SAL_CALL XSecTester::collectionStatusChanged( sal_Bool isInsideCollectedElement )
640 throw (cssu::RuntimeException)
642 this->m_bIsInsideCollectedElement = isInsideCollectedElement;
644 if ( !m_bIsInsideCollectedElement && !m_bIsBlocking)
646 m_bIsSAXEventKeeperOnTheSAXChain = false;
648 else
650 m_bIsSAXEventKeeperOnTheSAXChain = true;
652 changeOutput();
655 void SAL_CALL XSecTester::bufferStatusChanged( sal_Bool isBufferEmpty )
656 throw (cssu::RuntimeException)
658 if (isBufferEmpty)
660 m_xXMLDocumentWrapper = NULL;
662 m_xSAXEventKeeper = NULL;
663 m_bIsSAXEventKeeperOnTheSAXChain = false;
664 changeOutput();
668 /* XXMLSecTester */
669 rtl::OUString SAL_CALL XSecTester::export_xml( const rtl::OUString& inputFileName, const rtl::OUString& outputFileName, sal_Bool isJavaBased)
670 throw (cssu::RuntimeException)
672 rtl::OUString ouMessage;
674 m_nTotalSignatureNumber = 0;
675 m_nSuccessfulSignatureNumber = 0;
677 ouMessage += parseFile(inputFileName, outputFileName, sal_True, isJavaBased);
679 rtl::OUString ouRemark = rtl::OUString::valueOf(m_nSuccessfulSignatureNumber) +
680 rtl::OUString(RTL_ASCII_USTRINGPARAM( "/" ))
681 + rtl::OUString::valueOf(m_nTotalSignatureNumber);
682 ouMessage += rtl::OUString(RTL_ASCII_USTRINGPARAM("\t")) + ouRemark;
684 return ouMessage;
687 rtl::OUString SAL_CALL XSecTester::import_xml( const rtl::OUString& inputFileName, const rtl::OUString& outputFileName, sal_Bool isJavaBased)
688 throw (cssu::RuntimeException)
690 rtl::OUString ouMessage;
692 m_nTotalSignatureNumber = 0;
693 m_nSuccessfulSignatureNumber = 0;
695 ouMessage += parseFile(inputFileName, outputFileName, sal_False, isJavaBased);
697 rtl::OUString ouRemark = rtl::OUString::valueOf(m_nSuccessfulSignatureNumber) +
698 rtl::OUString(RTL_ASCII_USTRINGPARAM( "/" ))
699 + rtl::OUString::valueOf(m_nTotalSignatureNumber);
700 ouMessage += rtl::OUString(RTL_ASCII_USTRINGPARAM("\t")) + ouRemark;
702 return ouMessage;
705 rtl::OUString SAL_CALL XSecTester::transfer_without_sec(
706 const rtl::OUString& inputFileName,
707 const rtl::OUString& outputFileName,
708 sal_Bool isBridgeInvolved)
709 throw (cssu::RuntimeException)
711 rtl::OUString ouMessage;
713 cssu::Reference< cssi::XInputStream > xInputStream = OpenInputStream(inputFileName);
715 if (xInputStream != NULL )
717 /* create SAX Parser */
718 const rtl::OUString sSaxParser (
719 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser") );
720 m_xSaxParser = cssu::Reference < cssxs::XParser > ( mxMSF->createInstance( sSaxParser ), cssu::UNO_QUERY );
722 /* create SAX Writer */
723 const rtl::OUString sSaxWriter (
724 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer") );
725 cssu::Reference < cssi::XActiveDataSource > xSaxWriter
726 ( mxMSF->createInstance( sSaxWriter ), cssu::UNO_QUERY );
727 cssu::Reference < cssxs::XDocumentHandler > xSaxWriterHandler(
728 xSaxWriter, cssu::UNO_QUERY);
730 if (!isBridgeInvolved)
732 /* connect the SAX Parser and the SAX Writer */
733 m_xSaxParser->setDocumentHandler ( xSaxWriterHandler );
735 else
737 /* create Java Flat Filter */
738 const rtl::OUString sJavaFlatFilter(
739 RTL_CONSTASCII_USTRINGPARAM( JAVAFLATFILTER_COMPONENT ) );
740 cssu::Reference < cssxs::XParser > xJavaFilterParser
741 ( mxMSF->createInstance( sJavaFlatFilter ), cssu::UNO_QUERY );
742 cssu::Reference < cssxs::XDocumentHandler > xJavaFilterHandler(
743 xJavaFilterParser, cssu::UNO_QUERY );
745 if ( !xJavaFilterParser.is() )
746 return rtl::OUString::createFromAscii( "NO JAVA" );
748 /* connect the SAX Parser, the Java Flat Filter and the SAX Writer */
749 xJavaFilterParser->setDocumentHandler( xSaxWriterHandler );
750 m_xSaxParser->setDocumentHandler ( xJavaFilterHandler );
754 /* set output stream */
755 cssu::Reference< cssi::XOutputStream > xOutputStream =
756 OpenOutputStream(outputFileName);
757 xSaxWriter->setOutputStream( xOutputStream );
759 /* prepare input stream */
760 cssxs::InputSource aInput;
761 aInput.sSystemId = inputFileName;
762 aInput.aInputStream = xInputStream;
764 TimeValue startTime, endTime;
765 osl_getSystemTime( &startTime );
767 m_xSaxParser->parseStream ( aInput );
769 // xOutputStream->closeOutput();
770 xInputStream->closeInput();
772 osl_getSystemTime( &endTime );
774 double diff = ((double)((endTime.Nanosec + endTime.Seconds*1000000000.0)
775 - (startTime.Nanosec + startTime.Seconds*1000000000.0)))/((double)1000000000.0);
776 char buf[32];
777 sprintf(buf, "%.2f", diff);
778 ouMessage += rtl::OUString(RTL_ASCII_USTRINGPARAM(buf));
781 return ouMessage;
784 void SAL_CALL XSecTester::setCryptoDir(const rtl::OUString & javaDirName, const rtl::OUString & cDirName)
785 throw (cssu::RuntimeException)
787 m_ouJavaCryptokenDir = javaDirName;
788 m_ouCCryptokenDir = cDirName;
792 cssu::Reference< cssu::XInterface > SAL_CALL XSecTester_createInstance(
793 const cssu::Reference< cssl::XMultiServiceFactory > & rSMgr)
794 throw( cssu::Exception )
796 return (cppu::OWeakObject*) new XSecTester( rSMgr );
799 int SecurityEntity::m_nNextSecurityId = 1;
801 SecurityEntity::SecurityEntity(
802 const cssu::Reference<cssxc::sax::XSecuritySAXEventKeeper>& xSAXEventKeeper,
803 const cssu::Reference<cssxc::XXMLSecurityContext>& xXMLSecurityContext,
804 const cssu::Reference<cssxc::XXMLSignature>& xXMLSignature,
805 const cssu::Reference< cssl::XMultiServiceFactory > &rsMSF)
806 :m_xSAXEventKeeper(xSAXEventKeeper),
807 m_xXMLSecurityContext(xXMLSecurityContext),
808 m_xXMLSignature(xXMLSignature),
809 mxMSF(rsMSF),
810 m_ouKeyURI(RTL_ASCII_USTRINGPARAM(""))
812 m_nSecurityId = getNextSecurityId();
815 int SecurityEntity::getNextSecurityId() const
817 int nId = m_nNextSecurityId++;
818 return nId;
821 void SecurityEntity::setKeyId(int nId)
823 cssu::Reference<cssxc::sax::XKeyCollector> keyCollector (m_xReferenceListener, cssu::UNO_QUERY);
824 keyCollector->setKeyId(nId);
828 void SecurityEntity::setKeyURI(const rtl::OUString& ouUri)
830 m_ouKeyURI = ouUri;
833 cssu::Reference<cssxc::sax::XReferenceResolvedListener> SecurityEntity::getReferenceListener() const
835 return m_xReferenceListener;
838 int SecurityEntity::getSecurityId() const
840 return m_nSecurityId;
843 bool SecurityEntity::setKey(const rtl::OUString& ouUri, bool bIsExporting)
845 bool rc = false;
847 if (m_ouKeyURI != rtl::OUString(RTL_ASCII_USTRINGPARAM("")) &&
848 m_ouKeyURI == ouUri)
850 int nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
851 bIsExporting ?
852 (cssxc::sax::ElementMarkPriority_BEFOREMODIFY):
853 (cssxc::sax::ElementMarkPriority_AFTERMODIFY),
854 true);
856 setKeyId(nKeeperId);
857 m_xSAXEventKeeper->setSecurityId(nKeeperId, m_nSecurityId);
859 cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
860 (m_xSAXEventKeeper, cssu::UNO_QUERY);
861 xReferenceResolvedBroadcaster->addReferenceResolvedListener(nKeeperId,
862 m_xReferenceListener);
864 rc = true;
867 return rc;
870 bool SecurityEntity::endMission()
872 cssu::Reference<cssxc::sax::XMissionTaker> xMissionTaker
873 (m_xReferenceListener, cssu::UNO_QUERY);
875 return xMissionTaker->endMission();
878 SignatureEntity::SignatureEntity(
879 const cssu::Reference<cssxc::sax::XSecuritySAXEventKeeper>& xSAXEventKeeper,
880 bool bIsExporting,
881 XSecTester* pListener,
882 const cssu::Reference<cssxc::XXMLSecurityContext>& xXMLSecurityContext,
883 const cssu::Reference<cssxc::XXMLSignature>& xXMLSignature,
884 const cssu::Reference< cssl::XMultiServiceFactory >& rsMSF)
885 :SecurityEntity(xSAXEventKeeper,
886 xXMLSecurityContext,
887 xXMLSignature,
888 rsMSF)
890 if (bIsExporting)
892 m_nSignatureElementCollectorId =
893 m_xSAXEventKeeper->addSecurityElementCollector(
894 cssxc::sax::ElementMarkPriority_AFTERMODIFY,
895 true);
897 m_xSAXEventKeeper->setSecurityId(m_nSignatureElementCollectorId, m_nSecurityId);
899 m_xReferenceListener = cssu::Reference< cssxc::sax::XReferenceResolvedListener >(
900 mxMSF->createInstance( rtl::OUString::createFromAscii( SIGNATURECREATOR_COMPONENT )),
901 cssu::UNO_QUERY);
903 cssu::Reference<cssl::XInitialization> xInitialization(m_xReferenceListener, cssu::UNO_QUERY);
905 cssu::Sequence<cssu::Any> args(5);
906 char buf[16];
908 sprintf(buf, "%d", m_nSecurityId);
909 args[0] = cssu::makeAny(rtl::OUString(RTL_ASCII_USTRINGPARAM(buf)));
910 args[1] = cssu::makeAny(m_xSAXEventKeeper);
912 sprintf(buf, "%d", m_nSignatureElementCollectorId);
913 args[2] = cssu::makeAny(rtl::OUString(RTL_ASCII_USTRINGPARAM(buf)));
914 args[3] = cssu::makeAny(m_xXMLSecurityContext->getSecurityEnvironment());
915 args[4] = cssu::makeAny(m_xXMLSignature);
917 xInitialization->initialize(args);
919 int nBlockerId = m_xSAXEventKeeper->addBlocker();
920 m_xSAXEventKeeper->setSecurityId(nBlockerId, m_nSecurityId);
922 cssu::Reference<cssxc::sax::XBlockerMonitor> xBlockerMonitor(m_xReferenceListener, cssu::UNO_QUERY);
923 xBlockerMonitor->setBlockerId(nBlockerId);
925 cssu::Reference< cssxc::sax::XSignatureCreationResultBroadcaster > xSignatureCreationResultBroadcaster
926 (m_xReferenceListener, cssu::UNO_QUERY);
927 xSignatureCreationResultBroadcaster->addSignatureCreationResultListener(pListener);
929 else
931 m_nSignatureElementCollectorId =
932 m_xSAXEventKeeper->addSecurityElementCollector(
933 cssxc::sax::ElementMarkPriority_BEFOREMODIFY,
934 false);
936 m_xSAXEventKeeper->setSecurityId(m_nSignatureElementCollectorId, m_nSecurityId);
938 m_xReferenceListener = cssu::Reference< cssxc::sax::XReferenceResolvedListener >(
939 mxMSF->createInstance( rtl::OUString::createFromAscii( SIGNATUREVERIFIER_COMPONENT )),
940 cssu::UNO_QUERY);
942 cssu::Reference<cssl::XInitialization> xInitialization(m_xReferenceListener, cssu::UNO_QUERY);
944 cssu::Sequence<cssu::Any> args(5);
945 char buf[16];
947 sprintf(buf, "%d", m_nSecurityId);
948 args[0] = cssu::makeAny(rtl::OUString(RTL_ASCII_USTRINGPARAM(buf)));
949 args[1] = cssu::makeAny(m_xSAXEventKeeper);
951 sprintf(buf, "%d", m_nSignatureElementCollectorId);
952 args[2] = cssu::makeAny(rtl::OUString(RTL_ASCII_USTRINGPARAM(buf)));
953 args[3] = cssu::makeAny(m_xXMLSecurityContext);
954 args[4] = cssu::makeAny(m_xXMLSignature);
955 xInitialization->initialize(args);
957 cssu::Reference< cssxc::sax::XSignatureVerifyResultBroadcaster > xSignatureVerifyResultBroadcaster
958 (m_xReferenceListener, cssu::UNO_QUERY);
959 xSignatureVerifyResultBroadcaster->addSignatureVerifyResultListener(pListener);
962 cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
963 (m_xSAXEventKeeper, cssu::UNO_QUERY);
964 xReferenceResolvedBroadcaster->addReferenceResolvedListener(
965 m_nSignatureElementCollectorId, m_xReferenceListener);
968 void SignatureEntity::addReferenceURI(const rtl::OUString& ouUri)
970 m_vReferenceIds.push_back(ouUri);
973 void SignatureEntity::setReferenceNumber() const
975 cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
976 (m_xReferenceListener, cssu::UNO_QUERY);
977 xReferenceCollector->setReferenceCount(m_vReferenceIds.size());
980 bool SignatureEntity::hasReference(const rtl::OUString& ouUri) const
982 bool rc = false;
984 std::vector<const rtl::OUString>::const_iterator ii;
985 for (ii = m_vReferenceIds.begin(); ii != m_vReferenceIds.end(); ++ii)
987 if (ouUri == *ii)
989 rc = true;
990 break;
994 return rc;
997 bool SignatureEntity::setReference(const rtl::OUString& ouUri, bool bIsExporting) const
999 bool rc = false;
1001 if (hasReference(ouUri))
1003 int nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
1004 bIsExporting ?
1005 (cssxc::sax::ElementMarkPriority_AFTERMODIFY):
1006 (cssxc::sax::ElementMarkPriority_BEFOREMODIFY),
1007 false);
1009 m_xSAXEventKeeper->setSecurityId(nKeeperId, m_nSecurityId);
1011 cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
1012 (m_xSAXEventKeeper, cssu::UNO_QUERY);
1013 xReferenceResolvedBroadcaster->addReferenceResolvedListener(nKeeperId, m_xReferenceListener);
1015 cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
1016 (m_xReferenceListener, cssu::UNO_QUERY);
1017 xReferenceCollector->setReferenceId(nKeeperId);
1019 rc = true;
1022 return rc;
1025 /* XDocumentHandler */
1026 void SAL_CALL XSecTester::startDocument()
1027 throw (cssu::RuntimeException)
1031 void SAL_CALL XSecTester::endDocument()
1032 throw (cssu::RuntimeException)
1036 void SAL_CALL XSecTester::characters(const class rtl::OUString & chars)
1037 throw (cssu::RuntimeException)
1039 m_xExportHandler->characters(chars);
1042 void SAL_CALL XSecTester::processingInstruction(const rtl::OUString & target, const rtl::OUString &data)
1043 throw (cssu::RuntimeException)
1045 m_xExportHandler->processingInstruction(target, data);
1048 void SAL_CALL XSecTester::ignorableWhitespace(const rtl::OUString &)
1049 throw (cssu::RuntimeException)
1054 void SAL_CALL XSecTester::startElement(const rtl::OUString & name, const cssu::Reference<cssxs::XAttributeList> &xAttribs)
1055 throw (cssu::RuntimeException)
1057 rtl::OUString ouIdAttr = xAttribs->getValueByName(
1058 rtl::OUString(RTL_ASCII_USTRINGPARAM("id")));
1060 if (ouIdAttr == NULL)
1062 ouIdAttr = xAttribs->getValueByName(
1063 rtl::OUString(RTL_ASCII_USTRINGPARAM("Id")));
1066 bool bHasIdAttr = (ouIdAttr != NULL && ouIdAttr.getLength() > 0 );
1067 bool needResend = false;
1069 if (bHasIdAttr || name.equalsAscii( SIGNATURE_STR ))
1071 if (foundSecurityRelated() && ! m_bIsExporting)
1073 needResend = true;
1077 if ( !m_bIsSAXEventKeeperOnTheSAXChain )
1079 addStartAncestorEvent(name, xAttribs);
1082 bool bSuppressingForwarding = checkSecurityElement(name, xAttribs);
1084 checkReference(name, xAttribs, ouIdAttr);
1086 if (needResend)
1088 m_xSAXEventKeeper->setNextHandler(NULL);
1090 cssu::Reference<cssxs::XDocumentHandler> xSAXEventKeeperHandler
1091 (m_xSAXEventKeeper, cssu::UNO_QUERY);
1093 xSAXEventKeeperHandler->startElement(name, xAttribs);
1094 m_xSAXEventKeeper->setNextHandler(this);
1097 if (!bSuppressingForwarding)
1099 m_xExportHandler->startElement(name, xAttribs);
1103 void SAL_CALL XSecTester::endElement(const rtl::OUString& name)
1104 throw (cssu::RuntimeException)
1106 if (!m_stCurrentPath.empty())
1108 void* pSignedInfo = m_stCurrentPath.top();
1109 bool bIsStringType = m_stCurrentPathType.top();
1111 m_stCurrentPath.pop();
1112 m_stCurrentPathType.pop();
1114 if (bIsStringType && !strcmp((const char *)pSignedInfo, SIGNEDINFO_STR))
1116 if (!m_stCurrentPath.empty())
1118 void* pSignature = m_stCurrentPath.top();
1119 bIsStringType = m_stCurrentPathType.top();
1121 if (!bIsStringType && pSignature != NULL)
1123 ((SignatureEntity *) pSignature)->setReferenceNumber();
1129 if ( !m_bIsSAXEventKeeperOnTheSAXChain )
1131 addEndAncestorEvent(name);
1134 m_xExportHandler->endElement(name);
1137 void SAL_CALL XSecTester::setDocumentLocator( const cssu::Reference<cssxs::XLocator>& )
1138 throw (cssu::RuntimeException)
1142 void XSecTester::changeOutput()
1144 if (m_bIsExporting)
1146 if (m_bIsSAXEventKeeperOnTheSAXChain)
1148 m_xExportHandler = cssu::Reference<cssxs::XDocumentHandler>
1149 (m_xSAXEventKeeper, cssu::UNO_QUERY);
1151 m_xSAXEventKeeper->setNextHandler(NULL);
1153 flushAncestorEvents(m_xExportHandler);
1155 m_xSAXEventKeeper->setNextHandler(m_xOutputHandler);
1157 else
1159 m_xExportHandler = m_xOutputHandler;
1162 else
1164 if (m_bIsSAXEventKeeperOnTheSAXChain)
1166 cssu::Reference<cssxs::XDocumentHandler> xSAXEventKeeperHandler
1167 (m_xSAXEventKeeper, cssu::UNO_QUERY);
1169 m_xSAXEventKeeper->setNextHandler(NULL);
1171 flushAncestorEvents(xSAXEventKeeperHandler);
1173 m_xSaxParser->setDocumentHandler(xSAXEventKeeperHandler);
1174 m_xSAXEventKeeper->setNextHandler(this);
1176 else
1178 m_xSaxParser->setDocumentHandler(this);
1184 bool XSecTester::foundSecurityRelated()
1186 if (m_xSAXEventKeeper == NULL)
1188 m_bIsBlocking = false;
1189 m_bIsInsideCollectedElement = false;
1191 m_xXMLDocumentWrapper = cssu::Reference<cssxw::XXMLDocumentWrapper>
1192 (mxMSF->createInstance( m_ouXMLDocumentWrapperComponentName ),
1193 cssu::UNO_QUERY);
1195 m_xSAXEventKeeper = cssu::Reference< cssxc::sax::XSecuritySAXEventKeeper >
1196 (mxMSF->createInstance( rtl::OUString::createFromAscii( SAXEVENTKEEPER_COMPONENT )),
1197 cssu::UNO_QUERY);
1199 cssu::Reference<cssl::XInitialization> xInitialization(m_xSAXEventKeeper, cssu::UNO_QUERY);
1201 cssu::Sequence <cssu::Any> arg(1);
1202 arg[0] = cssu::makeAny(m_xXMLDocumentWrapper);
1203 xInitialization->initialize(arg);
1205 cssu::Reference<cssxc::sax::XSAXEventKeeperStatusChangeBroadcaster>
1206 xSAXEventKeeperStatusChangeBroadcaster(m_xSAXEventKeeper, cssu::UNO_QUERY);
1207 xSAXEventKeeperStatusChangeBroadcaster->addSAXEventKeeperStatusChangeListener(this);
1210 bool rc = false;
1212 if (!m_bIsSAXEventKeeperOnTheSAXChain)
1214 rc = true;
1217 m_bIsSAXEventKeeperOnTheSAXChain=true;
1218 changeOutput();
1220 return rc;
1223 void XSecTester::findKeyOrReference(SecurityEntity* pSecurityEntity, const rtl::OUString& ouUri, bool bIsFindingKey)
1225 std::vector<rtl::OUString>::iterator ii_referenceURIs;
1226 std::vector<int>::iterator ii_referenceKeeperIds;
1227 std::vector<int>::iterator ii_referenceRefNums;
1229 for (ii_referenceURIs = m_vUnsolvedReferenceURIs.begin(),
1230 ii_referenceKeeperIds = m_vUnsolvedReferenceKeeperIds.begin(),
1231 ii_referenceRefNums = m_vUnsolvedReferenceRefNums.begin();
1232 ii_referenceURIs != m_vUnsolvedReferenceURIs.end(); )
1234 rtl::OUString ouReferenceUri = *ii_referenceURIs;
1236 if (ouReferenceUri == ouUri)
1238 int nKeeperId = *ii_referenceKeeperIds;
1239 int nRefNum = *ii_referenceRefNums;
1241 if ( bIsFindingKey )
1243 int nClonedKeeperId = m_xSAXEventKeeper->cloneElementCollector(
1244 nKeeperId,
1245 m_bIsExporting?
1246 (cssxc::sax::ElementMarkPriority_BEFOREMODIFY):
1247 (cssxc::sax::ElementMarkPriority_AFTERMODIFY));
1249 pSecurityEntity->setKeyId(nClonedKeeperId);
1251 m_xSAXEventKeeper->setSecurityId(nClonedKeeperId, pSecurityEntity->getSecurityId());
1253 cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster>
1254 xReferenceResolvedBroadcaster(m_xSAXEventKeeper, cssu::UNO_QUERY);
1255 xReferenceResolvedBroadcaster->addReferenceResolvedListener(
1256 nClonedKeeperId,
1257 pSecurityEntity->getReferenceListener());
1259 else
1261 int nClonedKeeperId = m_xSAXEventKeeper->cloneElementCollector(
1262 nKeeperId,
1263 m_bIsExporting?
1264 (cssxc::sax::ElementMarkPriority_AFTERMODIFY):
1265 (cssxc::sax::ElementMarkPriority_BEFOREMODIFY));
1267 m_xSAXEventKeeper->setSecurityId(nClonedKeeperId, pSecurityEntity->getSecurityId());
1269 cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster>
1270 xReferenceResolvedBroadcaster
1271 (m_xSAXEventKeeper, cssu::UNO_QUERY);
1272 xReferenceResolvedBroadcaster->addReferenceResolvedListener(
1273 nClonedKeeperId,
1274 pSecurityEntity->getReferenceListener());
1276 cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
1277 (pSecurityEntity->getReferenceListener(), cssu::UNO_QUERY);
1278 xReferenceCollector->setReferenceId(nClonedKeeperId);
1281 nRefNum--;
1282 if (nRefNum == 0)
1284 m_xSAXEventKeeper->removeElementCollector(nKeeperId);
1286 ii_referenceURIs = m_vUnsolvedReferenceURIs.erase(ii_referenceURIs);
1287 ii_referenceKeeperIds = m_vUnsolvedReferenceKeeperIds.erase(ii_referenceKeeperIds);
1288 ii_referenceRefNums = m_vUnsolvedReferenceRefNums.erase(ii_referenceRefNums);
1290 else
1292 (*ii_referenceRefNums) = nRefNum;
1294 ii_referenceURIs++;
1295 ii_referenceKeeperIds++;
1296 ii_referenceRefNums++;
1299 if (bIsFindingKey)
1301 break;
1304 else
1306 ii_referenceURIs++;
1307 ii_referenceKeeperIds++;
1308 ii_referenceRefNums++;
1313 bool XSecTester::checkSecurityElement(
1314 const rtl::OUString& ouLocalName,
1315 const cssu::Reference<cssxs::XAttributeList>& xAttribs)
1317 bool rc = false;
1319 if (ouLocalName.equalsAscii(SIGNATURE_STR))
1321 SignatureEntity* pSignatureEntity = new SignatureEntity(
1322 m_xSAXEventKeeper,
1323 m_bIsExporting,
1324 this,
1325 m_xXMLSecurityContext,
1326 m_xXMLSignature,
1327 mxMSF);
1329 m_vSignatureList.push_back(pSignatureEntity);
1331 m_stCurrentPath.push(pSignatureEntity);
1332 m_stCurrentPathType.push(false);
1334 else if (ouLocalName.equalsAscii(REFERENCE_STR))
1336 if (!m_stCurrentPath.empty())
1338 void* pSignedInfo = m_stCurrentPath.top();
1339 bool bIsStringType = m_stCurrentPathType.top();
1341 m_stCurrentPath.pop();
1342 m_stCurrentPathType.pop();
1344 if (bIsStringType && !m_stCurrentPath.empty())
1346 void* pSignature = m_stCurrentPath.top();
1347 bool bIsStringType2 = m_stCurrentPathType.top();
1349 if (!strcmp((const char*)pSignedInfo, SIGNEDINFO_STR) && !bIsStringType2)
1351 rtl::OUString ouUri = xAttribs->getValueByName
1352 (rtl::OUString(RTL_ASCII_USTRINGPARAM( URI_ATTR_STR )));
1354 if (ouUri.matchAsciiL("#", 1, 0))
1356 rtl::OUString uri = ouUri.copy(1);
1357 SignatureEntity* pSignatureEntity = (SignatureEntity *)pSignature;
1359 if (uri != NULL && uri.getLength()>0)
1361 pSignatureEntity->addReferenceURI(uri);
1362 findKeyOrReference(pSignatureEntity, uri, true);
1367 m_stCurrentPath.push(pSignedInfo);
1368 m_stCurrentPathType.push(bIsStringType);
1370 m_stCurrentPath.push( (void *)REFERENCE_STR);
1371 m_stCurrentPathType.push(true);
1373 else if(ouLocalName.equalsAscii(KEYVALUE_STR) ||
1374 ouLocalName.equalsAscii(KEYNAME_STR) ||
1375 ouLocalName.equalsAscii(X509DATA_STR) ||
1376 ouLocalName.equalsAscii(ENCRYPTEDKEY_STR))
1378 if (!m_stCurrentPath.empty())
1380 void* pKeyInfo = m_stCurrentPath.top();
1381 bool bIsStringType = m_stCurrentPathType.top();
1383 m_stCurrentPath.pop();
1384 m_stCurrentPathType.pop();
1386 if (bIsStringType && !m_stCurrentPath.empty())
1388 bool bIsStringType2 = m_stCurrentPathType.top();
1390 if (!bIsStringType2)
1392 SecurityEntity *pSecurityEntity =
1393 (SecurityEntity *) (m_stCurrentPath.top());
1394 pSecurityEntity->setKeyId(0);
1398 m_stCurrentPath.push(pKeyInfo);
1399 m_stCurrentPathType.push(bIsStringType);
1402 m_stCurrentPath.push((void *)KEYVALUE_STR);
1403 m_stCurrentPathType.push(true);
1405 else if(ouLocalName.equalsAscii(RETRIEVALMETHOD_STR))
1407 if (!m_stCurrentPath.empty())
1409 void* pKeyInfo = m_stCurrentPath.top();
1410 bool bIsStringType = m_stCurrentPathType.top();
1412 m_stCurrentPath.pop();
1413 m_stCurrentPathType.pop();
1415 if (bIsStringType && !m_stCurrentPath.empty())
1417 bool bIsStringType2 = m_stCurrentPathType.top();
1419 if (!bIsStringType2)
1421 SecurityEntity *pSecurityEntity =
1422 (SecurityEntity *) m_stCurrentPath.top();
1423 rtl::OUString ouUri = xAttribs->getValueByName(
1424 rtl::OUString(RTL_ASCII_USTRINGPARAM( URI_ATTR_STR )));
1426 if (!strcmp((const char *)pKeyInfo, KEYINFO_STR) &&
1427 ouUri != NULL && ouUri.getLength()>0)
1429 pSecurityEntity->setKeyURI(ouUri);
1430 findKeyOrReference(pSecurityEntity, ouUri, true);
1436 m_stCurrentPath.push(pKeyInfo);
1437 m_stCurrentPathType.push(bIsStringType);
1440 m_stCurrentPath.push((void *)RETRIEVALMETHOD_STR);
1441 m_stCurrentPathType.push(true);
1443 else if(ouLocalName.equalsAscii(KEYINFO_STR))
1445 m_stCurrentPath.push((void *)KEYINFO_STR);
1446 m_stCurrentPathType.push(true);
1448 else if(ouLocalName.equalsAscii(SIGNEDINFO_STR))
1450 m_stCurrentPath.push((void *)SIGNEDINFO_STR);
1451 m_stCurrentPathType.push(true);
1453 else
1455 m_stCurrentPath.push((void *)OTHER_ELEMENT_STR);
1456 m_stCurrentPathType.push(true);
1459 return rc;
1462 void XSecTester::checkReference(
1463 const rtl::OUString& ouLocalName,
1464 const cssu::Reference<cssxs::XAttributeList>& xAttribs,
1465 const rtl::OUString& ouId)
1467 rtl::OUString refNumStr =
1468 xAttribs->getValueByName(rtl::OUString(RTL_ASCII_USTRINGPARAM(REFNUM_ATTR_STR)));
1470 if (ouId != NULL && ouId.getLength()>0 )
1472 int nRefNum = 999;
1473 if (refNumStr != NULL && refNumStr.getLength()>0 )
1475 nRefNum = refNumStr.toInt32();
1478 int nLength = m_vSignatureList.size();
1479 for (int i = 0; i<nLength; ++i)
1481 SignatureEntity* pSignatureEntity = m_vSignatureList.at(i);
1483 if (pSignatureEntity->setReference(ouId, m_bIsExporting))
1485 nRefNum--;
1488 if (pSignatureEntity->setKey(ouId, m_bIsExporting))
1490 nRefNum--;
1494 if (nRefNum>0)
1496 int nKeeperId;
1498 if (ouLocalName.equalsAscii(ENCRYPTEDKEY_STR))
1500 nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
1501 m_bIsExporting ?
1502 (cssxc::sax::ElementMarkPriority_BEFOREMODIFY):
1503 (cssxc::sax::ElementMarkPriority_AFTERMODIFY),
1504 true);
1506 else
1508 nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
1509 m_bIsExporting?
1510 (cssxc::sax::ElementMarkPriority_AFTERMODIFY):
1511 (cssxc::sax::ElementMarkPriority_BEFOREMODIFY),
1512 false);
1515 m_vUnsolvedReferenceURIs.push_back(ouId);
1516 m_vUnsolvedReferenceKeeperIds.push_back(nKeeperId);
1517 m_vUnsolvedReferenceRefNums.push_back(nRefNum);
1522 void XSecTester::endMission()
1524 while (m_vSignatureList.size() > 0)
1526 if (m_vSignatureList.size()>0)
1528 SignatureEntity * pSignatureEntity = m_vSignatureList.at(0);
1529 m_vSignatureList.erase(m_vSignatureList.begin());
1530 pSignatureEntity->endMission();
1531 delete pSignatureEntity;
1535 while (m_vUnsolvedReferenceURIs.size()>0)
1537 int nKeeperId = m_vUnsolvedReferenceKeeperIds.at(0);
1538 m_xSAXEventKeeper->removeElementCollector(nKeeperId);
1539 m_vUnsolvedReferenceURIs.erase(m_vUnsolvedReferenceURIs.begin());
1540 m_vUnsolvedReferenceKeeperIds.erase(m_vUnsolvedReferenceKeeperIds.begin());
1541 m_vUnsolvedReferenceRefNums.erase(m_vUnsolvedReferenceRefNums.begin());
1545 void XSecTester::addStartAncestorEvent(
1546 const rtl::OUString& ouName,
1547 const cssu::Reference< cssxs::XAttributeList >& xAttribs)
1549 sal_Int32 nLength = xAttribs->getLength();
1550 AncestorEvent* ancestorEvent = new AncestorEvent( nLength );
1552 ancestorEvent->bIsStartElement = true;
1553 ancestorEvent->ouName = ouName;
1555 for (int i = 0; i<nLength; ++i)
1557 (ancestorEvent->aAttributeList[i]).sName = xAttribs->getNameByIndex((short)i);
1558 (ancestorEvent->aAttributeList[i]).sValue =xAttribs->getValueByIndex((short)i);
1561 m_vAncestorEvents.push_back(ancestorEvent);
1564 void XSecTester::addEndAncestorEvent(const rtl::OUString& ouName)
1566 AncestorEvent* ancestorEvent = new AncestorEvent(0);
1568 ancestorEvent->bIsStartElement = false;
1569 ancestorEvent->ouName = ouName;
1571 m_vAncestorEvents.push_back(ancestorEvent);
1574 void XSecTester::sendAncestorStartElementEvent(
1575 const rtl::OUString& ouName,
1576 const cssu::Sequence< cssxcsax::XMLAttribute >& attrList,
1577 const cssu::Reference< cssxs::XDocumentHandler >& xDocumentHandler) const
1579 SvXMLAttributeList* pAttributeList = new SvXMLAttributeList();
1580 cssu::Reference < cssxs::XAttributeList > xAttrList
1581 = cssu::Reference< cssxs::XAttributeList > (pAttributeList);
1583 sal_Int32 nLength = attrList.getLength();
1585 for (int i = 0; i<nLength; ++i)
1587 pAttributeList->AddAttribute( attrList[i].sName, attrList[i].sValue);
1590 xDocumentHandler->startElement(ouName, xAttrList);
1593 void XSecTester::sendAncestorEndElementEvent(
1594 const rtl::OUString& ouName,
1595 const cssu::Reference< cssxs::XDocumentHandler >& xDocumentHandler) const
1597 xDocumentHandler->endElement(ouName);
1600 std::vector< AncestorEvent* >::const_iterator XSecTester::checkAncestorStartElementEvent(
1601 const std::vector< AncestorEvent* >::const_iterator& ii,
1602 const cssu::Reference< cssxs::XDocumentHandler >& xDocumentHandler) const
1604 std::vector< AncestorEvent* >::const_iterator next = ii+1;
1606 if (next == m_vAncestorEvents.end())
1608 sendAncestorStartElementEvent(
1609 (*ii)->ouName, (*ii)->aAttributeList, xDocumentHandler);
1611 else
1613 while ((next != m_vAncestorEvents.end()) && ((*next)->bIsStartElement))
1615 next = checkAncestorStartElementEvent(next, xDocumentHandler);
1618 if (next != m_vAncestorEvents.end())
1620 next++;
1624 return next;
1627 void XSecTester::flushAncestorEvents(
1628 const cssu::Reference< cssxs::XDocumentHandler >& xDocumentHandler)
1630 std::vector< AncestorEvent* >::const_iterator ii;
1632 if (xDocumentHandler != NULL)
1634 ii = m_vAncestorEvents.begin();
1636 while (ii != m_vAncestorEvents.end())
1638 AncestorEvent* ancestorEvent = *ii;
1640 if (ancestorEvent->bIsStartElement)
1642 ii = checkAncestorStartElementEvent(ii, xDocumentHandler);
1644 else
1646 sendAncestorEndElementEvent((*ii)->ouName, xDocumentHandler);
1647 ii++;
1652 /* free the ancestor events list */
1653 std::vector< AncestorEvent* >::iterator jj;
1655 while (m_vAncestorEvents.size()>0)
1657 jj = m_vAncestorEvents.begin();
1658 delete *jj;
1659 m_vAncestorEvents.erase(jj);
1664 * Get the length of a file in a platform independant fashion
1666 int getLength(const char *pInputFileName)
1668 int nSize = 0;
1669 std::ifstream data(pInputFileName);
1671 data.seekg(0, std::ios_base::end);
1672 nSize = data.tellg();
1674 return nSize;
1677 void outputHeader()
1679 fprintf(stderr, "%16s%4s%8s%12s%12s%12s%12s\n", "File Name", "E/I", "Size", "-C++", "-Java", "Forw-O", "No S/E");
1680 fprintf(stderr, "===============================================================================\n");
1684 * print the output on the screen as well as in the GNUPlot data file
1686 void output(const rtl::OUString& ouInputFileName,
1687 const rtl::OUString& ouTime_C,
1688 const rtl::OUString& ouTime_Java,
1689 const rtl::OUString& ouTime_NoSecurity,
1690 const rtl::OUString& ouTime_JavaForwardOnly,
1691 const rtl::OUString& ouRemark_C,
1692 const rtl::OUString& ouRemark_Java,
1693 bool bIsExporting)
1695 int nSize = getLength(rtl::OString(ouInputFileName, ouInputFileName.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1696 std::ofstream data;
1698 /* print screen */
1699 int nPosition = ouInputFileName.lastIndexOf('\\');
1700 rtl::OUString fileName = ouInputFileName.copy(nPosition + 1);
1702 fprintf(stderr, "%16s", rtl::OString(fileName, fileName.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1704 fprintf(stderr, "%4s", bIsExporting?"E":"I");
1705 fprintf(stderr, "%7dK", nSize/1024);
1706 fprintf(stderr, "%8s %3s",
1707 rtl::OString(ouTime_C, ouTime_C.getLength(), RTL_TEXTENCODING_ASCII_US).getStr(),
1708 rtl::OString(ouRemark_C, ouRemark_C.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1709 fprintf(stderr, "%8s %3s",
1710 rtl::OString(ouTime_Java, ouTime_Java.getLength(), RTL_TEXTENCODING_ASCII_US).getStr(),
1711 rtl::OString(ouRemark_Java, ouRemark_Java.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1712 fprintf(stderr, "%12s", rtl::OString(ouTime_JavaForwardOnly, ouTime_JavaForwardOnly.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1713 fprintf(stderr, "%12s", rtl::OString(ouTime_NoSecurity, ouTime_NoSecurity.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1714 fprintf(stderr, "\n");
1716 /* output the data as GNUPlot data file */
1718 char str[32];
1719 sprintf(str, "%d %s", nSize, rtl::OString(ouTime_C, ouTime_C.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1720 data.open("d:\\time_vs_size.txt", std::ios::app);
1721 data << str << std::endl;
1722 data.close();
1724 sprintf(str, "%d %s", nSize, rtl::OString(ouTime_Java, ouTime_Java.getLength(), RTL_TEXTENCODING_ASCII_US).getStr());
1725 data.open("d:\\time_vs_size_without_sec.txt", std::ios::app);
1726 data << str << std::endl;
1727 data.close();
1731 int main( int argc, char **argv )
1733 if (argc < 3)
1735 fprintf(stderr, "Usage: testtool <exportbatchfile> <importbatchfile> [<cppcryptotoken>] [<javacryptotoken>]\n");
1736 exit (-1);
1739 rtl::OUString aExportBatchFile = rtl::OUString::createFromAscii(argv[1]);
1740 rtl::OUString aImportBatchFile = rtl::OUString::createFromAscii(argv[2]);
1741 rtl::OUString aCPPCryptoToken;
1742 if ( argc > 3 )
1743 aCPPCryptoToken = rtl::OUString::createFromAscii(argv[3]);
1744 rtl::OUString aJavaCryptoToken;
1745 if ( argc > 4 )
1746 aJavaCryptoToken = rtl::OUString::createFromAscii(argv[4]);
1750 uno::Reference< lang::XMultiServiceFactory > xMSF = CreateDemoServiceFactory();
1752 XSecTester* pTester = new XSecTester( xMSF );
1753 uno::Reference< xml::sax::XDocumentHandler > xKeepARef = pTester;
1755 pTester->setCryptoDir( aJavaCryptoToken, aCPPCryptoToken );
1757 rtl::OUString ouTime_C, ouTime_Java, ouTime_NoSecurity, ouTime_JavaForwardOnly;
1758 rtl::OUString ouInputFileName;
1759 rtl::OUString outputFileName1;
1760 rtl::OUString outputFileName2;
1761 rtl::OUString ouRemark_C, ouRemark_Java;
1763 outputHeader();
1765 std::ifstream batch_export, batch_import;
1767 batch_export.open(OUStringToOString( aExportBatchFile, RTL_TEXTENCODING_ASCII_US ).getStr());
1769 const int MAX_LINE = 80;
1770 char line[MAX_LINE + 1];
1772 while (batch_export.getline(line, MAX_LINE))
1774 ouInputFileName = rtl::OUString::createFromAscii(line);
1775 int nPosition = ouInputFileName.lastIndexOf('.');
1776 int nPosition1;
1779 * export the file with signautre/encryption (C++)
1781 outputFileName1 = ouInputFileName.copy(0, nPosition) +
1782 rtl::OUString::createFromAscii("-ex.xml");
1783 ouTime_C = pTester->export_xml(ouInputFileName, outputFileName1, sal_False);
1784 nPosition1 = ouTime_C.lastIndexOf('\t');
1785 ouRemark_C = ouTime_C.copy(nPosition1 + 1);
1786 ouTime_C = ouTime_C.copy(0, nPosition1);
1789 * export the file with signautre/encryption (Java)
1791 outputFileName1 = ouInputFileName.copy(0, nPosition) +
1792 rtl::OUString::createFromAscii("-ex2.xml");
1793 ouTime_Java = pTester->export_xml(ouInputFileName, outputFileName1, sal_True);
1794 nPosition1 = ouTime_Java.lastIndexOf('\t');
1795 ouRemark_Java = ouTime_Java.copy(nPosition1 + 1);
1796 ouTime_Java = ouTime_Java.copy(0, nPosition1);
1799 * export the file without signautre/encryption
1801 outputFileName2 = ouInputFileName.copy(0, nPosition) +
1802 rtl::OUString::createFromAscii("-ex-no.xml");
1803 ouTime_NoSecurity = pTester->transfer_without_sec(ouInputFileName, outputFileName2, sal_False);
1806 * export the file with Java Flat Filter
1808 outputFileName2 = ouInputFileName.copy(0, nPosition) +
1809 rtl::OUString::createFromAscii("-ex-jf.xml");
1810 ouTime_JavaForwardOnly = pTester->transfer_without_sec(ouInputFileName, outputFileName2, sal_True);
1813 * print output
1815 output(ouInputFileName, ouTime_C, ouTime_Java, ouTime_NoSecurity, ouTime_JavaForwardOnly, ouRemark_C, ouRemark_Java, true);
1818 batch_export.close();
1820 batch_import.open(OUStringToOString( aImportBatchFile, RTL_TEXTENCODING_ASCII_US ).getStr());
1822 while (batch_import.getline(line, MAX_LINE))
1824 ouInputFileName = rtl::OUString::createFromAscii(line);
1825 int nPosition = ouInputFileName.lastIndexOf('.');
1826 int nPosition1;
1829 * import the file with signautre/encryption (C++)
1831 outputFileName1 = ouInputFileName.copy(0, nPosition) +
1832 rtl::OUString::createFromAscii("-im.xml");
1833 ouTime_C = pTester->import_xml(ouInputFileName, outputFileName1, sal_False);
1834 nPosition1 = ouTime_C.lastIndexOf('\t');
1835 ouRemark_C = ouTime_C.copy(nPosition1 + 1);
1836 ouTime_C = ouTime_C.copy(0, nPosition1);
1839 * import the file with signautre/encryption (Java)
1841 outputFileName1 = ouInputFileName.copy(0, nPosition) +
1842 rtl::OUString::createFromAscii("-im2.xml");
1843 ouTime_Java = pTester->import_xml(ouInputFileName, outputFileName1, sal_True);
1844 nPosition1 = ouTime_Java.lastIndexOf('\t');
1845 ouRemark_Java = ouTime_Java.copy(nPosition1 + 1);
1846 ouTime_Java = ouTime_Java.copy(0, nPosition1);
1849 * import the file without signautre/encryption
1851 outputFileName2 = ouInputFileName.copy(0, nPosition) +
1852 rtl::OUString::createFromAscii("-im-no.xml");
1853 ouTime_NoSecurity = pTester->transfer_without_sec(ouInputFileName, outputFileName2, sal_False);
1856 * import the file without signautre/encryption
1859 outputFileName2 = ouInputFileName.copy(0, nPosition) +
1860 rtl::OUString::createFromAscii("-im-jf.xml");
1861 ouTime_JavaForwardOnly = pTester->transfer_without_sec(ouInputFileName, outputFileName2, sal_True);
1864 * print output
1866 output(ouInputFileName, ouTime_C, ouTime_Java, ouTime_NoSecurity, ouTime_JavaForwardOnly, ouRemark_C, ouRemark_Java, false);
1869 batch_import.close();
1871 fprintf(stderr, "\n");
1873 catch( cssu::Exception& e )
1875 fprintf( stderr , "\nEXCEPTION! Error Message: %s\n" ,
1876 rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
1879 return 0;