1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: performance.cxx,v $
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"
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>
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>
91 * Can not build under solaris.
92 * Delete the memory.h including by AF
96 #include <sys/types.h>
102 #ifndef INCLUDED_VECTOR
104 #define INCLUDED_VECTOR
107 #ifndef INCLUDED_STACK
109 #define INCLUDED_STACK
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
;
161 * The XSecTester class is a C++ version of SecurityFramworkController.java
168 static int m_nNextSecurityId
;
169 rtl::OUString m_ouKeyURI
;
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
>
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
>
194 int getNextSecurityId() const;
198 const com::sun::star::uno::Reference
<
199 com::sun::star::xml::crypto::sax::XSecuritySAXEventKeeper
>&
201 const com::sun::star::uno::Reference
<
202 com::sun::star::xml::crypto::XXMLSecurityContext
>&
204 const com::sun::star::uno::Reference
<
205 com::sun::star::xml::crypto::XXMLSignature
>&
207 const com::sun::star::uno::Reference
<
208 com::sun::star::lang::XMultiServiceFactory
>&
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
);
228 class SignatureEntity
: public SecurityEntity
231 std::vector
< rtl::OUString
> m_vReferenceIds
;
232 int m_nSignatureElementCollectorId
;
234 bool hasReference(const rtl::OUString
& ouUri
) const;
238 const com::sun::star::uno::Reference
<
239 com::sun::star::xml::crypto::sax::XSecuritySAXEventKeeper
>&
242 XSecTester
* pListener
,
243 const com::sun::star::uno::Reference
<
244 com::sun::star::xml::crypto::XXMLSecurityContext
>&
246 const com::sun::star::uno::Reference
<
247 com::sun::star::xml::crypto::XXMLSignature
>&
249 const com::sun::star::uno::Reference
<
250 com::sun::star::lang::XMultiServiceFactory
>&
252 ~SignatureEntity(){};
254 void setReferenceNumber() const;
255 bool setReference( const rtl::OUString
& ouUri
, bool bIsExporting
) const;
256 void addReferenceURI( const rtl::OUString
& ouUri
);
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
>
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
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
>
289 com::sun::star::uno::Reference
<
290 com::sun::star::xml::crypto::sax::XSecuritySAXEventKeeper
>
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
>
301 com::sun::star::uno::Reference
<
302 com::sun::star::xml::sax::XParser
>
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
;
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
>
329 rtl::OUString m_ouJavaCryptokenDir
;
330 rtl::OUString m_ouCCryptokenDir
;
331 rtl::OUString m_ouXMLDocumentWrapperComponentName
;
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
,
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
);
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
);
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;
392 XSecTester(const com::sun::star::uno::Reference
<
393 com::sun::star::lang::XMultiServiceFactory
>& 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
);
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
,
468 rtl::OUString ouMessage
;
470 cssu::Reference
<cssi::XInputStream
> xInputStream
= OpenInputStream(ouInputFileName
);
472 if (xInputStream
!= NULL
)
475 rtl::OUString SEInitializer_comp
;
476 rtl::OUString XMLSignature_comp
;
477 rtl::OUString tokenPath
;
478 cssu::Reference
< cssxc::XSEInitializer
> xSEInitializer
;
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
;
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
),
499 m_xXMLSignature
= cssu::Reference
<cssxc::XXMLSignature
> (
500 mxMSF
->createInstance( XMLSignature_comp
),
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());
549 /* foundSecurityRelated(); */
552 TimeValue startTime
, endTime
;
553 osl_getSystemTime( &startTime
);
555 xSaxWriterHandler
->startDocument();
559 m_xSaxParser
->setDocumentHandler(this);
560 m_xSaxParser
->parseStream(aInput
);
564 m_xSaxParser
->setDocumentHandler(this);
565 m_xSaxParser
->parseStream(aInput
);
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);
592 sprintf(buf
, "%.2f", diff
);
593 ouMessage
+= rtl::OUString(RTL_ASCII_USTRINGPARAM(buf
));
597 ouMessage
+= rtl::OUString::createFromAscii( "N/A" );
603 ouMessage
+= rtl::OUString::createFromAscii( "-" );
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;
653 m_bIsSAXEventKeeperOnTheSAXChain
= true;
658 void SAL_CALL
XSecTester::bufferStatusChanged( sal_Bool isBufferEmpty
)
659 throw (cssu::RuntimeException
)
663 m_xXMLDocumentWrapper
= NULL
;
665 m_xSAXEventKeeper
= NULL
;
666 m_bIsSAXEventKeeperOnTheSAXChain
= false;
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
;
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
;
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
);
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);
780 sprintf(buf
, "%.2f", diff
);
781 ouMessage
+= rtl::OUString(RTL_ASCII_USTRINGPARAM(buf
));
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
),
813 m_ouKeyURI(RTL_ASCII_USTRINGPARAM(""))
815 m_nSecurityId
= getNextSecurityId();
818 int SecurityEntity::getNextSecurityId() const
820 int nId
= m_nNextSecurityId
++;
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
)
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
)
850 if (m_ouKeyURI
!= rtl::OUString(RTL_ASCII_USTRINGPARAM("")) &&
853 int nKeeperId
= m_xSAXEventKeeper
->addSecurityElementCollector(
855 (cssxc::sax::ElementMarkPriority_BEFOREMODIFY
):
856 (cssxc::sax::ElementMarkPriority_AFTERMODIFY
),
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
);
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
,
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
,
895 m_nSignatureElementCollectorId
=
896 m_xSAXEventKeeper
->addSecurityElementCollector(
897 cssxc::sax::ElementMarkPriority_AFTERMODIFY
,
900 m_xSAXEventKeeper
->setSecurityId(m_nSignatureElementCollectorId
, m_nSecurityId
);
902 m_xReferenceListener
= cssu::Reference
< cssxc::sax::XReferenceResolvedListener
>(
903 mxMSF
->createInstance( rtl::OUString::createFromAscii( SIGNATURECREATOR_COMPONENT
)),
906 cssu::Reference
<cssl::XInitialization
> xInitialization(m_xReferenceListener
, cssu::UNO_QUERY
);
908 cssu::Sequence
<cssu::Any
> args(5);
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
);
934 m_nSignatureElementCollectorId
=
935 m_xSAXEventKeeper
->addSecurityElementCollector(
936 cssxc::sax::ElementMarkPriority_BEFOREMODIFY
,
939 m_xSAXEventKeeper
->setSecurityId(m_nSignatureElementCollectorId
, m_nSecurityId
);
941 m_xReferenceListener
= cssu::Reference
< cssxc::sax::XReferenceResolvedListener
>(
942 mxMSF
->createInstance( rtl::OUString::createFromAscii( SIGNATUREVERIFIER_COMPONENT
)),
945 cssu::Reference
<cssl::XInitialization
> xInitialization(m_xReferenceListener
, cssu::UNO_QUERY
);
947 cssu::Sequence
<cssu::Any
> args(5);
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
987 std::vector
<const rtl::OUString
>::const_iterator ii
;
988 for (ii
= m_vReferenceIds
.begin(); ii
!= m_vReferenceIds
.end(); ++ii
)
1000 bool SignatureEntity::setReference(const rtl::OUString
& ouUri
, bool bIsExporting
) const
1004 if (hasReference(ouUri
))
1006 int nKeeperId
= m_xSAXEventKeeper
->addSecurityElementCollector(
1008 (cssxc::sax::ElementMarkPriority_AFTERMODIFY
):
1009 (cssxc::sax::ElementMarkPriority_BEFOREMODIFY
),
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
);
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
)
1080 if ( !m_bIsSAXEventKeeperOnTheSAXChain
)
1082 addStartAncestorEvent(name
, xAttribs
);
1085 bool bSuppressingForwarding
= checkSecurityElement(name
, xAttribs
);
1087 checkReference(name
, xAttribs
, ouIdAttr
);
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()
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
);
1162 m_xExportHandler
= m_xOutputHandler
;
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);
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
),
1198 m_xSAXEventKeeper
= cssu::Reference
< cssxc::sax::XSecuritySAXEventKeeper
>
1199 (mxMSF
->createInstance( rtl::OUString::createFromAscii( SAXEVENTKEEPER_COMPONENT
)),
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);
1215 if (!m_bIsSAXEventKeeperOnTheSAXChain
)
1220 m_bIsSAXEventKeeperOnTheSAXChain
=true;
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(
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(
1260 pSecurityEntity
->getReferenceListener());
1264 int nClonedKeeperId
= m_xSAXEventKeeper
->cloneElementCollector(
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(
1277 pSecurityEntity
->getReferenceListener());
1279 cssu::Reference
<cssxc::sax::XReferenceCollector
> xReferenceCollector
1280 (pSecurityEntity
->getReferenceListener(), cssu::UNO_QUERY
);
1281 xReferenceCollector
->setReferenceId(nClonedKeeperId
);
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
);
1295 (*ii_referenceRefNums
) = nRefNum
;
1298 ii_referenceKeeperIds
++;
1299 ii_referenceRefNums
++;
1310 ii_referenceKeeperIds
++;
1311 ii_referenceRefNums
++;
1316 bool XSecTester::checkSecurityElement(
1317 const rtl::OUString
& ouLocalName
,
1318 const cssu::Reference
<cssxs::XAttributeList
>& xAttribs
)
1322 if (ouLocalName
.equalsAscii(SIGNATURE_STR
))
1324 SignatureEntity
* pSignatureEntity
= new SignatureEntity(
1328 m_xXMLSecurityContext
,
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);
1458 m_stCurrentPath
.push((void *)OTHER_ELEMENT_STR
);
1459 m_stCurrentPathType
.push(true);
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 )
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
))
1491 if (pSignatureEntity
->setKey(ouId
, m_bIsExporting
))
1501 if (ouLocalName
.equalsAscii(ENCRYPTEDKEY_STR
))
1503 nKeeperId
= m_xSAXEventKeeper
->addSecurityElementCollector(
1505 (cssxc::sax::ElementMarkPriority_BEFOREMODIFY
):
1506 (cssxc::sax::ElementMarkPriority_AFTERMODIFY
),
1511 nKeeperId
= m_xSAXEventKeeper
->addSecurityElementCollector(
1513 (cssxc::sax::ElementMarkPriority_AFTERMODIFY
):
1514 (cssxc::sax::ElementMarkPriority_BEFOREMODIFY
),
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
);
1616 while ((next
!= m_vAncestorEvents
.end()) && ((*next
)->bIsStartElement
))
1618 next
= checkAncestorStartElementEvent(next
, xDocumentHandler
);
1621 if (next
!= m_vAncestorEvents
.end())
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
);
1649 sendAncestorEndElementEvent((*ii
)->ouName
, xDocumentHandler
);
1655 /* free the ancestor events list */
1656 std::vector
< AncestorEvent
* >::iterator jj
;
1658 while (m_vAncestorEvents
.size()>0)
1660 jj
= m_vAncestorEvents
.begin();
1662 m_vAncestorEvents
.erase(jj
);
1667 * Get the length of a file in a platform independant fashion
1669 int getLength(const char *pInputFileName
)
1672 std::ifstream
data(pInputFileName
);
1674 data
.seekg(0, std::ios_base::end
);
1675 nSize
= data
.tellg();
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
,
1698 int nSize
= getLength(rtl::OString(ouInputFileName
, ouInputFileName
.getLength(), RTL_TEXTENCODING_ASCII_US
).getStr());
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 */
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;
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;
1734 int main( int argc
, char **argv
)
1738 fprintf(stderr
, "Usage: testtool <exportbatchfile> <importbatchfile> [<cppcryptotoken>] [<javacryptotoken>]\n");
1742 rtl::OUString aExportBatchFile
= rtl::OUString::createFromAscii(argv
[1]);
1743 rtl::OUString aImportBatchFile
= rtl::OUString::createFromAscii(argv
[2]);
1744 rtl::OUString aCPPCryptoToken
;
1746 aCPPCryptoToken
= rtl::OUString::createFromAscii(argv
[3]);
1747 rtl::OUString aJavaCryptoToken
;
1749 aJavaCryptoToken
= rtl::OUString::createFromAscii(argv
[4]);
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
;
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('.');
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
);
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('.');
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
);
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() ) ;