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: xsecctl.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"
34 #include <xsecctl.hxx>
35 #include <tools/debug.hxx>
37 #include <com/sun/star/xml/crypto/sax/ElementMarkPriority.hpp>
38 #include <com/sun/star/xml/crypto/sax/XReferenceResolvedBroadcaster.hpp>
39 #include <com/sun/star/xml/crypto/sax/XMissionTaker.hpp>
40 #include <com/sun/star/xml/crypto/sax/XReferenceCollector.hpp>
41 #include <com/sun/star/xml/crypto/sax/XSAXEventKeeperStatusChangeBroadcaster.hpp>
42 #include <com/sun/star/xml/crypto/SecurityOperationStatus.hpp>
44 #include <xmloff/attrlist.hxx>
45 #include <rtl/math.hxx>
46 #include <tools/string.hxx>
48 namespace cssu
= com::sun::star::uno
;
49 namespace cssl
= com::sun::star::lang
;
50 namespace cssxc
= com::sun::star::xml::crypto
;
51 namespace cssxs
= com::sun::star::xml::sax
;
52 namespace cssxw
= com::sun::star::xml::wrapper
;
53 namespace cssb
= com::sun::star::beans
;
55 const sal_Int8 XML_MAXDIGITSCOUNT_TIME
= 11;
56 const sal_Int8 XML_MAXDIGITSCOUNT_DATETIME
= 6;
58 /* bridge component names */
59 #define XMLSIGNATURE_COMPONENT "com.sun.star.xml.crypto.XMLSignature"
60 #define XMLDOCUMENTWRAPPER_COMPONENT "com.sun.star.xml.wrapper.XMLDocumentWrapper"
62 /* xml security framework components */
63 #define SAXEVENTKEEPER_COMPONENT "com.sun.star.xml.crypto.sax.SAXEventKeeper"
65 /* string for package protocol */
66 #define PACKAGEPROTOCOL "vnd.sun.star.Package:"
68 XSecController::XSecController( const cssu::Reference
<cssu::XComponentContext
>& rxCtx
)
71 m_bIsSAXEventKeeperConnected(false),
72 m_nStatusOfSecurityComponents(UNINITIALIZED
),
73 m_bIsSAXEventKeeperSticky(false),
74 m_pErrorMessage(NULL
),
79 XSecController::~XSecController()
87 /** convert string to number with optional min and max values */
88 sal_Bool
XSecController::convertNumber( sal_Int32
& rValue
,
89 const rtl::OUString
& rString
,
90 sal_Int32
/*nMin*/, sal_Int32
/*nMax*/ )
92 sal_Bool bNeg
= sal_False
;
96 sal_Int32 nLen
= rString
.getLength();
99 while( nPos
< nLen
&& sal_Unicode(' ') == rString
[nPos
] )
102 if( nPos
< nLen
&& sal_Unicode('-') == rString
[nPos
] )
109 while( nPos
< nLen
&&
110 sal_Unicode('0') <= rString
[nPos
] &&
111 sal_Unicode('9') >= rString
[nPos
] )
113 // TODO: check overflow!
115 rValue
+= (rString
[nPos
] - sal_Unicode('0'));
125 /** convert util::DateTime to ISO Date String */
126 void XSecController::convertDateTime( ::rtl::OUStringBuffer
& rBuffer
,
127 const com::sun::star::util::DateTime
& rDateTime
)
129 String
aString( String::CreateFromInt32( rDateTime
.Year
) );
131 if( rDateTime
.Month
< 10 )
133 aString
+= String::CreateFromInt32( rDateTime
.Month
);
135 if( rDateTime
.Day
< 10 )
137 aString
+= String::CreateFromInt32( rDateTime
.Day
);
139 if( rDateTime
.Seconds
!= 0 ||
140 rDateTime
.Minutes
!= 0 ||
141 rDateTime
.Hours
!= 0 )
144 if( rDateTime
.Hours
< 10 )
146 aString
+= String::CreateFromInt32( rDateTime
.Hours
);
148 if( rDateTime
.Minutes
< 10 )
150 aString
+= String::CreateFromInt32( rDateTime
.Minutes
);
152 if( rDateTime
.Seconds
< 10 )
154 aString
+= String::CreateFromInt32( rDateTime
.Seconds
);
155 if ( rDateTime
.HundredthSeconds
> 0)
158 if (rDateTime
.HundredthSeconds
< 10)
160 aString
+= String::CreateFromInt32( rDateTime
.HundredthSeconds
);
164 rBuffer
.append( aString
);
167 /** convert ISO Date String to util::DateTime */
168 sal_Bool
XSecController::convertDateTime( com::sun::star::util::DateTime
& rDateTime
,
169 const ::rtl::OUString
& rString
)
171 sal_Bool bSuccess
= sal_True
;
173 rtl::OUString aDateStr
, aTimeStr
, sHundredth
;
174 sal_Int32 nPos
= rString
.indexOf( (sal_Unicode
) 'T' );
175 sal_Int32 nPos2
= rString
.indexOf( (sal_Unicode
) ',' );
178 aDateStr
= rString
.copy( 0, nPos
);
181 aTimeStr
= rString
.copy( nPos
+ 1, nPos2
- nPos
- 1 );
183 //Get the fraction of a second with the accuracy of one hundreds second.
184 //The fraction part of the date could have different accuracies. To calculate
185 //the count of a hundredth units one could form a fractional number by appending
186 //the value of the time string to 0. Then multiply it by 100 and use only the whole number.
187 //For example: 5:27:46,1 -> 0,1 * 100 = 10
188 //5:27:46,01 -> 0,01 * 100 = 1
189 //5:27:46,001 -> 0,001 * 100 = 0
190 //Due to the inaccuracy of floating point numbers the result may not be the same on different
191 //platforms. We had the case where we had a value of 24 hundredth of second, which converted to
192 //23 on Linux and 24 on Solaris and Windows.
194 //we only support a hundredth second
195 //make ,1 -> 10 ,01 -> 1 ,001 -> only use first two diggits
196 sHundredth
= rString
.copy(nPos2
+ 1);
197 sal_Int32 len
= sHundredth
.getLength();
199 sHundredth
+= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("0"));
201 sHundredth
= sHundredth
.copy(0, 2);
205 aTimeStr
= rString
.copy(nPos
+ 1);
206 sHundredth
= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("0"));
210 aDateStr
= rString
; // no separator: only date part
212 sal_Int32 nYear
= 1899;
213 sal_Int32 nMonth
= 12;
219 const sal_Unicode
* pStr
= aDateStr
.getStr();
220 sal_Int32 nDateTokens
= 1;
227 if ( nDateTokens
> 3 || aDateStr
.getLength() == 0 )
228 bSuccess
= sal_False
;
232 if ( !convertNumber( nYear
, aDateStr
.getToken( 0, '-', n
), 0, 9999 ) )
233 bSuccess
= sal_False
;
234 if ( nDateTokens
>= 2 )
235 if ( !convertNumber( nMonth
, aDateStr
.getToken( 0, '-', n
), 0, 12 ) )
236 bSuccess
= sal_False
;
237 if ( nDateTokens
>= 3 )
238 if ( !convertNumber( nDay
, aDateStr
.getToken( 0, '-', n
), 0, 31 ) )
239 bSuccess
= sal_False
;
242 if ( aTimeStr
.getLength() > 0 ) // time is optional
244 pStr
= aTimeStr
.getStr();
245 sal_Int32 nTimeTokens
= 1;
252 if ( nTimeTokens
> 3 )
253 bSuccess
= sal_False
;
257 if ( !convertNumber( nHour
, aTimeStr
.getToken( 0, ':', n
), 0, 23 ) )
258 bSuccess
= sal_False
;
259 if ( nTimeTokens
>= 2 )
260 if ( !convertNumber( nMin
, aTimeStr
.getToken( 0, ':', n
), 0, 59 ) )
261 bSuccess
= sal_False
;
262 if ( nTimeTokens
>= 3 )
263 if ( !convertNumber( nSec
, aTimeStr
.getToken( 0, ':', n
), 0, 59 ) )
264 bSuccess
= sal_False
;
270 rDateTime
.Year
= (sal_uInt16
)nYear
;
271 rDateTime
.Month
= (sal_uInt16
)nMonth
;
272 rDateTime
.Day
= (sal_uInt16
)nDay
;
273 rDateTime
.Hours
= (sal_uInt16
)nHour
;
274 rDateTime
.Minutes
= (sal_uInt16
)nMin
;
275 rDateTime
.Seconds
= (sal_uInt16
)nSec
;
276 // rDateTime.HundredthSeconds = sDoubleStr.toDouble() * 100;
277 rDateTime
.HundredthSeconds
= static_cast<sal_uInt16
>(sHundredth
.toInt32());
282 int XSecController::findSignatureInfor( sal_Int32 nSecurityId
) const
283 /****** XSecController/findSignatureInfor *************************************
286 * findSignatureInfor -- find SignatureInformation struct for a particular
290 * index = findSignatureInfor( nSecurityId );
296 * nSecurityId - the signature's id
299 * index - the index of the signature, or -1 when no such signature
303 * 08.05.2004 - implemented
307 * Email: michael.mi@sun.com
308 ******************************************************************************/
311 int size
= m_vInternalSignatureInformations
.size();
313 for (i
=0; i
<size
; ++i
)
315 if (m_vInternalSignatureInformations
[i
].signatureInfor
.nSecurityId
== nSecurityId
)
324 void XSecController::createXSecComponent( )
325 /****** XSecController/createXSecComponent ************************************
328 * bResult = createXSecComponent -- creates xml security components
331 * createXSecComponent( );
334 * Creates xml security components, including:
335 * 1. an xml signature bridge component ( Java based or C based)
336 * 2. an XMLDocumentWrapper component ( Java based or C based)
337 * 3. a SAXEventKeeper component
346 * 05.01.2004 - implemented
350 * Email: michael.mi@sun.com
351 ******************************************************************************/
353 rtl::OUString
sSAXEventKeeper(rtl::OUString::createFromAscii( SAXEVENTKEEPER_COMPONENT
));
354 rtl::OUString
sXMLSignature(rtl::OUString::createFromAscii( XMLSIGNATURE_COMPONENT
));
355 rtl::OUString
sXMLDocument(rtl::OUString::createFromAscii( XMLDOCUMENTWRAPPER_COMPONENT
));
358 * marks all security components are not available.
360 m_nStatusOfSecurityComponents
= FAILTOINITIALIZED
;
361 m_xXMLSignature
= NULL
;
362 m_xXMLDocumentWrapper
= NULL
;
363 m_xSAXEventKeeper
= NULL
;
365 cssu::Reference
< cssl::XMultiComponentFactory
> xMCF( mxCtx
->getServiceManager() );
367 m_xXMLSignature
= cssu::Reference
< cssxc::XXMLSignature
>(
368 xMCF
->createInstanceWithContext( sXMLSignature
, mxCtx
),
371 bool bSuccess
= (0!=m_xXMLSignature
.is());
374 * XMLSignature created successfully.
377 m_xXMLDocumentWrapper
= cssu::Reference
< cssxw::XXMLDocumentWrapper
>(
378 xMCF
->createInstanceWithContext( sXMLDocument
, mxCtx
),
382 bSuccess
&= (0!=m_xXMLDocumentWrapper
.is());
385 * XMLDocumentWrapper created successfully.
388 m_xSAXEventKeeper
= cssu::Reference
< cssxc::sax::XSecuritySAXEventKeeper
>(
389 xMCF
->createInstanceWithContext( sSAXEventKeeper
, mxCtx
),
393 bSuccess
&= (0!=m_xSAXEventKeeper
.is());
397 * SAXEventKeeper created successfully.
400 cssu::Reference
< cssl::XInitialization
> xInitialization(m_xSAXEventKeeper
, cssu::UNO_QUERY
);
402 cssu::Sequence
<cssu::Any
> arg(1);
403 arg
[0] = cssu::makeAny(m_xXMLDocumentWrapper
);
404 xInitialization
->initialize(arg
);
406 cssu::Reference
<cssxc::sax::XSAXEventKeeperStatusChangeBroadcaster
>
407 xSAXEventKeeperStatusChangeBroadcaster(m_xSAXEventKeeper
, cssu::UNO_QUERY
);
408 cssu::Reference
< cssxc::sax::XSAXEventKeeperStatusChangeListener
>
409 xStatusChangeListener
= this;
411 xSAXEventKeeperStatusChangeBroadcaster
412 ->addSAXEventKeeperStatusChangeListener( xStatusChangeListener
);
414 m_nStatusOfSecurityComponents
= INITIALIZED
;
418 bool XSecController::chainOn( bool bRetrievingLastEvent
)
419 /****** XSecController/chainOn ************************************************
422 * chainOn -- tyies to connect the SAXEventKeeper with the SAX chain.
425 * bJustChainingOn = chainOn( bRetrievingLastEvent );
428 * First, checks whether the SAXEventKeeper is on the SAX chain. If not,
429 * creates xml security components, and chains the SAXEventKeeper into
431 * Before being chained in, the SAXEventKeeper needs to receive all
432 * missed key SAX events, which can promise the DOM tree bufferred by the
433 * SAXEventKeeper has the same structure with the original document.
436 * bRetrievingLastEvent - whether to retrieve the last key SAX event from
437 * the ElementStackKeeper.
440 * bJustChainingOn - whether the SAXEventKeeper is just chained into the
444 * Sometimes, the last key SAX event can't be transferred to the
445 * SAXEventKeeper together.
446 * For instance, at the time an referenced element is detected, the
447 * startElement event has already been reserved by the ElementStackKeeper.
448 * Meanwhile, an ElementCollector needs to be created before the
449 * SAXEventKeeper receives that startElement event.
450 * So for the SAXEventKeeper, it needs to receive all missed key SAX
451 * events except that startElement event, then adds a new
452 * ElementCollector, then receives that startElement event.
455 * 05.01.2004 - implemented
459 * Email: michael.mi@sun.com
460 ******************************************************************************/
464 if (!m_bIsSAXEventKeeperSticky
&& !m_bIsSAXEventKeeperConnected
)
466 if ( m_nStatusOfSecurityComponents
== UNINITIALIZED
)
468 createXSecComponent();
471 if ( m_nStatusOfSecurityComponents
== INITIALIZED
)
473 * if all security components are ready, chains on the SAXEventKeeper
477 * disconnect the SAXEventKeeper with its current output handler,
478 * to make sure no SAX event is forwarded during the connecting
481 m_xSAXEventKeeper
->setNextHandler( NULL
);
483 cssu::Reference
< cssxs::XDocumentHandler
> xSEKHandler(m_xSAXEventKeeper
, cssu::UNO_QUERY
);
486 * connects the previous document handler on the SAX chain
488 if ( m_xPreviousNodeOnSAXChain
.is() )
490 if ( m_bIsPreviousNodeInitializable
)
492 cssu::Reference
< cssl::XInitialization
> xInitialization
493 (m_xPreviousNodeOnSAXChain
, cssu::UNO_QUERY
);
495 cssu::Sequence
<cssu::Any
> aArgs( 1 );
496 aArgs
[0] <<= xSEKHandler
;
497 xInitialization
->initialize(aArgs
);
501 cssu::Reference
< cssxs::XParser
> xParser
502 (m_xPreviousNodeOnSAXChain
, cssu::UNO_QUERY
);
503 xParser
->setDocumentHandler( xSEKHandler
);
508 * get missed key SAX events
510 if (m_xElementStackKeeper
.is())
512 m_xElementStackKeeper
->retrieve(xSEKHandler
, bRetrievingLastEvent
);
515 * now the ElementStackKeeper can stop its work, because the
516 * SAXEventKeeper is on the SAX chain, no SAX events will be
519 m_xElementStackKeeper
->stop();
523 * connects the next document handler on the SAX chain
525 m_xSAXEventKeeper
->setNextHandler( m_xNextNodeOnSAXChain
);
527 m_bIsSAXEventKeeperConnected
= true;
536 void XSecController::chainOff()
537 /****** XSecController/chainOff ***********************************************
540 * chainOff -- disconnects the SAXEventKeeper from the SAX chain.
555 * 05.01.2004 - implemented
559 * Email: michael.mi@sun.com
560 ******************************************************************************/
562 if (!m_bIsSAXEventKeeperSticky
)
564 if (m_bIsSAXEventKeeperConnected
)
566 m_xSAXEventKeeper
->setNextHandler( NULL
);
568 if ( m_xPreviousNodeOnSAXChain
.is() )
570 if ( m_bIsPreviousNodeInitializable
)
572 cssu::Reference
< cssl::XInitialization
> xInitialization
573 (m_xPreviousNodeOnSAXChain
, cssu::UNO_QUERY
);
575 cssu::Sequence
<cssu::Any
> aArgs( 1 );
576 aArgs
[0] <<= m_xNextNodeOnSAXChain
;
577 xInitialization
->initialize(aArgs
);
581 cssu::Reference
< cssxs::XParser
> xParser(m_xPreviousNodeOnSAXChain
, cssu::UNO_QUERY
);
582 xParser
->setDocumentHandler( m_xNextNodeOnSAXChain
);
586 if (m_xElementStackKeeper
.is())
589 * start the ElementStackKeeper to reserve any possible
590 * missed key SAX events
592 m_xElementStackKeeper
->start();
595 m_bIsSAXEventKeeperConnected
= false;
600 void XSecController::checkChainingStatus()
601 /****** XSecController/checkChainingStatus ************************************
604 * checkChainingStatus -- connects or disconnects the SAXEventKeeper
605 * according to the current situation.
608 * checkChainingStatus( );
611 * The SAXEventKeeper is chained into the SAX chain, when:
612 * 1. some element is being collected, or
613 * 2. the SAX event stream is blocking.
614 * Otherwise, chain off the SAXEventKeeper.
623 * 05.01.2004 - implemented
627 * Email: michael.mi@sun.com
628 ******************************************************************************/
630 if ( m_bIsCollectingElement
|| m_bIsBlocking
)
640 void XSecController::initializeSAXChain()
641 /****** XSecController/initializeSAXChain *************************************
644 * initializeSAXChain -- initializes the SAX chain according to the
648 * initializeSAXChain( );
651 * Initializes the SAX chain, if the SAXEventKeeper is asked to be always
652 * on the SAX chain, chains it on. Otherwise, starts the
653 * ElementStackKeeper to reserve key SAX events.
662 * 05.01.2004 - implemented
666 * Email: michael.mi@sun.com
667 ******************************************************************************/
669 m_bIsSAXEventKeeperConnected
= false;
670 m_bIsCollectingElement
= false;
671 m_bIsBlocking
= false;
673 if (m_xElementStackKeeper
.is())
676 * starts the ElementStackKeeper
678 m_xElementStackKeeper
->start();
684 cssu::Reference
< com::sun::star::io::XInputStream
>
685 XSecController::getObjectInputStream( const rtl::OUString
& objectURL
)
686 /****** XSecController/getObjectInputStream ************************************
689 * getObjectInputStream -- get a XInputStream interface from a SvStorage
692 * xInputStream = getObjectInputStream( objectURL );
698 * objectURL - the object uri
701 * xInputStream - the XInputStream interface
704 * 15.04.2004 - implemented
708 * Email: michael.mi@sun.com
709 ******************************************************************************/
711 cssu::Reference
< com::sun::star::io::XInputStream
> xObjectInputStream
;
713 DBG_ASSERT( m_xUriBinding
.is(), "Need XUriBinding!" );
715 xObjectInputStream
= m_xUriBinding
->getUriBinding(objectURL
);
717 return xObjectInputStream
;
721 sal_Int32
XSecController::getFastPropertyIndex(sal_Int32 nHandle
) const
722 /****** XSecController/getFastPropertyIndex ***********************************
725 * getFastPropertyIndex -- gets the index of a particular fast property
728 * nIndex = getFastPropertyIndex( nHandle );
734 * nHandle - the key for the fast property
737 * nIndex - the index of the fast property, or -1
738 * if the key is not found.
741 * 05.01.2004 - implemented
745 * Email: michael.mi@sun.com
746 ******************************************************************************/
748 std::vector
< sal_Int32
>::const_iterator ii
= m_vFastPropertyIndexs
.begin();
749 sal_Int32 nIndex
= 0;
753 for( ; ii
!= m_vFastPropertyIndexs
.end(); ++ii
,++nIndex
)
755 if ( nHandle
== (*ii
))
775 sal_Int32
XSecController::getNewSecurityId( )
777 sal_Int32 nId
= m_nNextSecurityId
;
782 void XSecController::startMission(
783 const cssu::Reference
< cssxc::XUriBinding
>& xUriBinding
,
784 const cssu::Reference
< cssxc::XXMLSecurityContext
>& xSecurityContext
)
785 /****** XSecController/startMission *******************************************
788 * startMission -- starts a new security mission.
791 * startMission( xUriBinding, xSecurityContect );
794 * get ready for a new mission.
797 * xUriBinding - the Uri binding that provide maps between uris and
799 * xSecurityContext - the security context component which can provide
806 * 05.01.2004 - implemented
810 * Email: michael.mi@sun.com
811 ******************************************************************************/
813 m_xUriBinding
= xUriBinding
;
815 m_nStatusOfSecurityComponents
= UNINITIALIZED
;
816 m_xSecurityContext
= xSecurityContext
;
817 m_pErrorMessage
= NULL
;
819 m_vInternalSignatureInformations
.clear();
821 m_bVerifyCurrentSignature
= false;
824 void XSecController::setSAXChainConnector(
825 const cssu::Reference
< cssl::XInitialization
>& xInitialization
,
826 const cssu::Reference
< cssxs::XDocumentHandler
>& xDocumentHandler
,
827 const cssu::Reference
< cssxc::sax::XElementStackKeeper
>& xElementStackKeeper
)
828 /****** XSecController/setSAXChainConnector ***********************************
831 * setSAXChainConnector -- configures the components which will
832 * collaborate with the SAXEventKeeper on the SAX chain.
835 * setSAXChainConnector( xInitialization,
837 * xElementStackKeeper );
843 * xInitialization - the previous node on the SAX chain
844 * xDocumentHandler - the next node on the SAX chain
845 * xElementStackKeeper - the ElementStackKeeper component which reserves
846 * missed key SAX events for the SAXEventKeeper
852 * 05.01.2004 - implemented
856 * Email: michael.mi@sun.com
857 ******************************************************************************/
859 m_bIsPreviousNodeInitializable
= true;
860 m_xPreviousNodeOnSAXChain
= xInitialization
;
861 m_xNextNodeOnSAXChain
= xDocumentHandler
;
862 m_xElementStackKeeper
= xElementStackKeeper
;
864 initializeSAXChain( );
867 void XSecController::setSAXChainConnector(
868 const cssu::Reference
< cssxs::XParser
>& xParser
,
869 const cssu::Reference
< cssxs::XDocumentHandler
>& xDocumentHandler
,
870 const cssu::Reference
< cssxc::sax::XElementStackKeeper
>& xElementStackKeeper
)
871 /****** XSecController/setSAXChainConnector ***********************************
874 * setSAXChainConnector -- configures the components which will
875 * collaborate with the SAXEventKeeper on the SAX chain.
878 * setSAXChainConnector( xParser, xDocumentHandler, xElementStackKeeper );
884 * xParser - the previous node on the SAX chain
885 * xDocumentHandler - the next node on the SAX chain
886 * xElementStackKeeper -the ElementStackKeeper component which reserves
887 * missed key SAX events for the SAXEventKeeper
893 * 05.01.2004 - implemented
897 * Email: michael.mi@sun.com
898 ******************************************************************************/
900 m_bIsPreviousNodeInitializable
= false;
901 m_xPreviousNodeOnSAXChain
= xParser
;
902 m_xNextNodeOnSAXChain
= xDocumentHandler
;
903 m_xElementStackKeeper
= xElementStackKeeper
;
905 initializeSAXChain( );
908 void XSecController::clearSAXChainConnector()
909 /****** XSecController/clearSAXChainConnector *********************************
912 * clearSAXChainConnector -- resets the collaborating components.
915 * clearSAXChainConnector( );
927 * 05.01.2004 - implemented
931 * Email: michael.mi@sun.com
932 ******************************************************************************/
935 * before reseting, if the ElementStackKeeper has kept something, then
936 * those kept key SAX events must be transferred to the SAXEventKeeper
937 * first. This is to promise the next node to the SAXEventKeeper on the
938 * SAX chain always receives a complete document.
940 if (m_xElementStackKeeper
.is() && m_xSAXEventKeeper
.is())
942 cssu::Reference
< cssxs::XDocumentHandler
> xSEKHandler(m_xSAXEventKeeper
, cssu::UNO_QUERY
);
943 m_xElementStackKeeper
->retrieve(xSEKHandler
, sal_True
);
948 m_xPreviousNodeOnSAXChain
= NULL
;
949 m_xNextNodeOnSAXChain
= NULL
;
950 m_xElementStackKeeper
= NULL
;
953 void XSecController::endMission()
954 /****** XSecController/endMission *********************************************
957 * endMission -- forces to end all missions
963 * Deletes all signature information and forces all missions to an end.
972 * 05.01.2004 - implemented
976 * Email: michael.mi@sun.com
977 ******************************************************************************/
979 sal_Int32 size
= m_vInternalSignatureInformations
.size();
981 for (int i
=0; i
<size
; ++i
)
983 if ( m_nStatusOfSecurityComponents
== INITIALIZED
)
985 * ResolvedListener only exist when the security components are created.
988 cssu::Reference
< cssxc::sax::XMissionTaker
> xMissionTaker
989 ( m_vInternalSignatureInformations
[i
].xReferenceResolvedListener
, cssu::UNO_QUERY
);
992 * askes the SignatureCreator/SignatureVerifier to release
993 * all resouces it uses.
995 xMissionTaker
->endMission();
999 m_xUriBinding
= NULL
;
1000 m_xSecurityContext
= NULL
;
1003 * free the status change listener reference to this object
1005 if (m_xSAXEventKeeper
.is())
1007 cssu::Reference
<cssxc::sax::XSAXEventKeeperStatusChangeBroadcaster
>
1008 xSAXEventKeeperStatusChangeBroadcaster(m_xSAXEventKeeper
, cssu::UNO_QUERY
);
1009 xSAXEventKeeperStatusChangeBroadcaster
1010 ->addSAXEventKeeperStatusChangeListener( NULL
);
1014 const char* XSecController::getErrorMessage()
1015 /****** XSecController/getErrorMessage ****************************************
1018 * getErrorMessage -- get the last error message
1021 * pErrorMessage = getErrorMessage( );
1033 * 22.04.2004 - implemented
1037 * Email: michael.mi@sun.com
1038 ******************************************************************************/
1040 return m_pErrorMessage
;
1043 void XSecController::exportSignature(
1044 const cssu::Reference
<cssxs::XDocumentHandler
>& xDocumentHandler
,
1045 const SignatureInformation
& signatureInfo
)
1046 /****** XSecController/exportSignature ****************************************
1049 * exportSignature -- export a signature structure to an XDocumentHandler
1052 * exportSignature( xDocumentHandler, signatureInfo);
1058 * xDocumentHandler - the document handler to receive the signature
1059 * signatureInfo - signature to be exported
1065 * 26.05.2004 - implemented
1069 * Email: michael.mi@sun.com
1070 ******************************************************************************/
1073 * defines all element tags in Signature element.
1075 rtl::OUString
tag_Signature(RTL_CONSTASCII_USTRINGPARAM(TAG_SIGNATURE
));
1076 rtl::OUString
tag_SignedInfo(RTL_CONSTASCII_USTRINGPARAM(TAG_SIGNEDINFO
));
1077 rtl::OUString
tag_CanonicalizationMethod(RTL_CONSTASCII_USTRINGPARAM(TAG_CANONICALIZATIONMETHOD
));
1078 rtl::OUString
tag_SignatureMethod(RTL_CONSTASCII_USTRINGPARAM(TAG_SIGNATUREMETHOD
));
1079 rtl::OUString
tag_Reference(RTL_CONSTASCII_USTRINGPARAM(TAG_REFERENCE
));
1080 rtl::OUString
tag_Transforms(RTL_CONSTASCII_USTRINGPARAM(TAG_TRANSFORMS
));
1081 rtl::OUString
tag_Transform(RTL_CONSTASCII_USTRINGPARAM(TAG_TRANSFORM
));
1082 rtl::OUString
tag_DigestMethod(RTL_CONSTASCII_USTRINGPARAM(TAG_DIGESTMETHOD
));
1083 rtl::OUString
tag_DigestValue(RTL_CONSTASCII_USTRINGPARAM(TAG_DIGESTVALUE
));
1084 rtl::OUString
tag_SignatureValue(RTL_CONSTASCII_USTRINGPARAM(TAG_SIGNATUREVALUE
));
1085 rtl::OUString
tag_KeyInfo(RTL_CONSTASCII_USTRINGPARAM(TAG_KEYINFO
));
1086 rtl::OUString
tag_X509Data(RTL_CONSTASCII_USTRINGPARAM(TAG_X509DATA
));
1087 rtl::OUString
tag_X509IssuerSerial(RTL_CONSTASCII_USTRINGPARAM(TAG_X509ISSUERSERIAL
));
1088 rtl::OUString
tag_X509IssuerName(RTL_CONSTASCII_USTRINGPARAM(TAG_X509ISSUERNAME
));
1089 rtl::OUString
tag_X509SerialNumber(RTL_CONSTASCII_USTRINGPARAM(TAG_X509SERIALNUMBER
));
1090 rtl::OUString
tag_X509Certificate(RTL_CONSTASCII_USTRINGPARAM(TAG_X509CERTIFICATE
));
1092 rtl::OUString
tag_Object(RTL_CONSTASCII_USTRINGPARAM(TAG_OBJECT
));
1093 rtl::OUString
tag_SignatureProperties(RTL_CONSTASCII_USTRINGPARAM(TAG_SIGNATUREPROPERTIES
));
1094 rtl::OUString
tag_SignatureProperty(RTL_CONSTASCII_USTRINGPARAM(TAG_SIGNATUREPROPERTY
));
1095 rtl::OUString
tag_Date(RTL_CONSTASCII_USTRINGPARAM(TAG_DATE
));
1097 rtl::OUString
tag_Timestamp(RTL_CONSTASCII_USTRINGPARAM(TAG_TIMESTAMP
));
1098 rtl::OUString
tag_Date(RTL_CONSTASCII_USTRINGPARAM(TAG_DATE
));
1099 rtl::OUString
tag_Time(RTL_CONSTASCII_USTRINGPARAM(TAG_TIME
));
1102 const SignatureReferenceInformations
& vReferenceInfors
= signatureInfo
.vSignatureReferenceInfors
;
1103 SvXMLAttributeList
*pAttributeList
;
1106 * Write Signature element
1108 pAttributeList
= new SvXMLAttributeList();
1109 pAttributeList
->AddAttribute(
1110 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_XMLNS
)),
1111 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(NS_XMLDSIG
)));
1113 if (signatureInfo
.ouSignatureId
.getLength()>0)
1115 pAttributeList
->AddAttribute(
1116 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_ID
)),
1117 rtl::OUString(signatureInfo
.ouSignatureId
));
1120 xDocumentHandler
->startElement( tag_Signature
, cssu::Reference
< cssxs::XAttributeList
> (pAttributeList
));
1122 /* Write SignedInfo element */
1123 xDocumentHandler
->startElement(
1125 cssu::Reference
< cssxs::XAttributeList
> (new SvXMLAttributeList()));
1127 /* Write CanonicalizationMethod element */
1128 pAttributeList
= new SvXMLAttributeList();
1129 pAttributeList
->AddAttribute(
1130 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_ALGORITHM
)),
1131 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ALGO_C14N
)));
1132 xDocumentHandler
->startElement( tag_CanonicalizationMethod
, cssu::Reference
< cssxs::XAttributeList
> (pAttributeList
) );
1133 xDocumentHandler
->endElement( tag_CanonicalizationMethod
);
1135 /* Write SignatureMethod element */
1136 pAttributeList
= new SvXMLAttributeList();
1137 pAttributeList
->AddAttribute(
1138 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_ALGORITHM
)),
1139 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ALGO_RSASHA1
)));
1140 xDocumentHandler
->startElement( tag_SignatureMethod
, cssu::Reference
< cssxs::XAttributeList
> (pAttributeList
) );
1141 xDocumentHandler
->endElement( tag_SignatureMethod
);
1143 /* Write Reference element */
1145 int refNum
= vReferenceInfors
.size();
1147 for(j
=0; j
<refNum
; ++j
)
1149 const SignatureReferenceInformation
& refInfor
= vReferenceInfors
[j
];
1151 pAttributeList
= new SvXMLAttributeList();
1152 if ( refInfor
.nType
!= TYPE_SAMEDOCUMENT_REFERENCE
)
1157 pAttributeList
->AddAttribute(
1158 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_URI
)),
1163 * same-document reference
1166 pAttributeList
->AddAttribute(
1167 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_URI
)),
1168 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CHAR_FRAGMENT
))+refInfor
.ouURI
);
1171 xDocumentHandler
->startElement( tag_Reference
, cssu::Reference
< cssxs::XAttributeList
> (pAttributeList
) );
1173 /* Write Transforms element */
1174 if (refInfor
.nType
== TYPE_XMLSTREAM_REFERENCE
)
1176 * xml stream, so c14n transform is needed
1179 xDocumentHandler
->startElement(
1181 cssu::Reference
< cssxs::XAttributeList
> (new SvXMLAttributeList()));
1183 pAttributeList
= new SvXMLAttributeList();
1184 pAttributeList
->AddAttribute(
1185 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_ALGORITHM
)),
1186 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ALGO_C14N
)));
1187 xDocumentHandler
->startElement(
1189 cssu::Reference
< cssxs::XAttributeList
> (pAttributeList
) );
1190 xDocumentHandler
->endElement( tag_Transform
);
1192 xDocumentHandler
->endElement( tag_Transforms
);
1195 /* Write DigestMethod element */
1196 pAttributeList
= new SvXMLAttributeList();
1197 pAttributeList
->AddAttribute(
1198 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_ALGORITHM
)),
1199 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ALGO_XMLDSIGSHA1
)));
1200 xDocumentHandler
->startElement(
1202 cssu::Reference
< cssxs::XAttributeList
> (pAttributeList
) );
1203 xDocumentHandler
->endElement( tag_DigestMethod
);
1205 /* Write DigestValue element */
1206 xDocumentHandler
->startElement(
1208 cssu::Reference
< cssxs::XAttributeList
> (new SvXMLAttributeList()));
1209 xDocumentHandler
->characters( refInfor
.ouDigestValue
);
1210 xDocumentHandler
->endElement( tag_DigestValue
);
1212 xDocumentHandler
->endElement( tag_Reference
);
1215 xDocumentHandler
->endElement( tag_SignedInfo
);
1217 /* Write SignatureValue element */
1218 xDocumentHandler
->startElement(
1220 cssu::Reference
< cssxs::XAttributeList
> (new SvXMLAttributeList()));
1221 xDocumentHandler
->characters( signatureInfo
.ouSignatureValue
);
1222 xDocumentHandler
->endElement( tag_SignatureValue
);
1224 /* Write KeyInfo element */
1225 xDocumentHandler
->startElement(
1227 cssu::Reference
< cssxs::XAttributeList
> (new SvXMLAttributeList()));
1229 /* Write X509Data element */
1230 xDocumentHandler
->startElement(
1232 cssu::Reference
< cssxs::XAttributeList
> (new SvXMLAttributeList()));
1234 /* Write X509IssuerSerial element */
1235 xDocumentHandler
->startElement(
1236 tag_X509IssuerSerial
,
1237 cssu::Reference
< cssxs::XAttributeList
> (new SvXMLAttributeList()));
1239 /* Write X509IssuerName element */
1240 xDocumentHandler
->startElement(
1242 cssu::Reference
< cssxs::XAttributeList
> (new SvXMLAttributeList()));
1243 xDocumentHandler
->characters( signatureInfo
.ouX509IssuerName
);
1244 xDocumentHandler
->endElement( tag_X509IssuerName
);
1246 /* Write X509SerialNumber element */
1247 xDocumentHandler
->startElement(
1248 tag_X509SerialNumber
,
1249 cssu::Reference
< cssxs::XAttributeList
> (new SvXMLAttributeList()));
1250 xDocumentHandler
->characters( signatureInfo
.ouX509SerialNumber
);
1251 xDocumentHandler
->endElement( tag_X509SerialNumber
);
1253 xDocumentHandler
->endElement( tag_X509IssuerSerial
);
1255 /* Write X509Certificate element */
1256 if (signatureInfo
.ouX509Certificate
.getLength()>0)
1258 xDocumentHandler
->startElement(
1259 tag_X509Certificate
,
1260 cssu::Reference
< cssxs::XAttributeList
> (new SvXMLAttributeList()));
1261 xDocumentHandler
->characters( signatureInfo
.ouX509Certificate
);
1262 xDocumentHandler
->endElement( tag_X509Certificate
);
1265 xDocumentHandler
->endElement( tag_X509Data
);
1267 xDocumentHandler
->endElement( tag_KeyInfo
);
1269 /* Write Object element */
1270 xDocumentHandler
->startElement(
1272 cssu::Reference
< cssxs::XAttributeList
> (new SvXMLAttributeList()));
1274 /* Write SignatureProperties element */
1275 xDocumentHandler
->startElement(
1276 tag_SignatureProperties
,
1277 cssu::Reference
< cssxs::XAttributeList
> (new SvXMLAttributeList()));
1279 /* Write SignatureProperty element */
1280 pAttributeList
= new SvXMLAttributeList();
1281 pAttributeList
->AddAttribute(
1282 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_ID
)),
1283 signatureInfo
.ouPropertyId
);
1284 pAttributeList
->AddAttribute(
1285 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_TARGET
)),
1286 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CHAR_FRAGMENT
))+signatureInfo
.ouSignatureId
);
1287 xDocumentHandler
->startElement(
1288 tag_SignatureProperty
,
1289 cssu::Reference
< cssxs::XAttributeList
> (pAttributeList
));
1291 /* Write timestamp element */
1293 pAttributeList
= new SvXMLAttributeList();
1294 pAttributeList
->AddAttribute(
1295 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_XMLNS
))
1296 +rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(":"))
1297 +rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(NSTAG_DC
)),
1298 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(NS_DC
)));
1300 xDocumentHandler
->startElement(
1301 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(NSTAG_DC
))
1302 +rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(":"))
1304 cssu::Reference
< cssxs::XAttributeList
> (pAttributeList
));
1306 ::rtl::OUStringBuffer buffer
;
1307 //If the xml signature was already contained in the document,
1308 //then we use the original date and time string, rather then the
1309 //converted one. When the original string is converted to the DateTime
1310 //structure then information may be lost because it only holds a fractional
1311 //of a second with a accuracy of one hundredth of second. If the string contains
1312 //milli seconds (document was signed by an application other than OOo)
1313 //and the converted time is written back, then the string looks different
1314 //and the signature is broken.
1315 if (signatureInfo
.ouDateTime
.getLength() > 0)
1316 buffer
= signatureInfo
.ouDateTime
;
1318 convertDateTime( buffer
, signatureInfo
.stDateTime
);
1319 xDocumentHandler
->characters( buffer
.makeStringAndClear() );
1321 xDocumentHandler
->endElement(
1322 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(NSTAG_DC
))
1323 +rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(":"))
1326 xDocumentHandler
->endElement( tag_SignatureProperty
);
1328 xDocumentHandler
->endElement( tag_SignatureProperties
);
1330 xDocumentHandler
->endElement( tag_Object
);
1332 xDocumentHandler
->endElement( tag_Signature
);
1335 SignatureInformation
XSecController::getSignatureInformation( sal_Int32 nSecurityId
) const
1337 SignatureInformation
aInf( 0 );
1338 int nIndex
= findSignatureInfor(nSecurityId
);
1339 DBG_ASSERT( nIndex
!= -1, "getSignatureInformation - SecurityId is invalid!" );
1342 aInf
= m_vInternalSignatureInformations
[nIndex
].signatureInfor
;
1347 SignatureInformations
XSecController::getSignatureInformations() const
1349 SignatureInformations vInfors
;
1350 int sigNum
= m_vInternalSignatureInformations
.size();
1352 for (int i
=0; i
<sigNum
; ++i
)
1354 SignatureInformation si
= m_vInternalSignatureInformations
[i
].signatureInfor
;
1355 vInfors
.push_back(si
);
1362 * XSecurityController
1371 void SAL_CALL XSecController::setFastPropertyValue(
1373 const cssu::Any& aValue )
1374 throw ( cssb::UnknownPropertyException,
1375 cssb::PropertyVetoException,
1376 cssl::IllegalArgumentException,
1377 cssl::WrappedTargetException,
1378 cssu::RuntimeException)
1380 sal_Int32 nIndex = getFastPropertyIndex(nHandle);
1383 m_vFastPropertyIndexs.push_back( nHandle );
1384 m_vFastPropertyValues.push_back( aValue );
1388 m_vFastPropertyValues[nIndex] = aValue;
1392 cssu::Any SAL_CALL XSecController::getFastPropertyValue(
1395 cssb::UnknownPropertyException,
1396 cssl::WrappedTargetException,
1397 cssu::RuntimeException)
1401 sal_Int32 nIndex = getFastPropertyIndex(nHandle);
1404 aValue = m_vFastPropertyValues[nIndex];
1412 * XSAXEventKeeperStatusChangeListener
1415 void SAL_CALL
XSecController::blockingStatusChanged( sal_Bool isBlocking
)
1416 throw (cssu::RuntimeException
)
1419 showMessageBox( rtl::OUString::createFromAscii((isBlocking?
1420 "Blocking Status => TRUE":
1421 "Blocking Status => FALSE")),
1422 rtl::OUString::createFromAscii("SAXEventKeeper Status"));
1425 this->m_bIsBlocking
= isBlocking
;
1426 checkChainingStatus();
1429 void SAL_CALL
XSecController::collectionStatusChanged(
1430 sal_Bool isInsideCollectedElement
)
1431 throw (cssu::RuntimeException
)
1434 showMessageBox( rtl::OUString::createFromAscii((isInsideCollectedElement?
1435 "Collection Status => TRUE":
1436 "Collection Status => FALSE")),
1437 rtl::OUString::createFromAscii("SAXEventKeeper Status"));
1440 this->m_bIsCollectingElement
= isInsideCollectedElement
;
1441 checkChainingStatus();
1444 void SAL_CALL
XSecController::bufferStatusChanged( sal_Bool
/*isBufferEmpty*/)
1445 throw (cssu::RuntimeException
)
1448 showMessageBox( rtl::OUString::createFromAscii((isBufferEmpty?
1449 "Buffer Empty => TRUE":
1450 "Buffer Empty => FALSE")),
1451 rtl::OUString::createFromAscii("SAXEventKeeper Status"));
1456 * XSignatureCreationResultListener
1458 void SAL_CALL
XSecController::signatureCreated( sal_Int32 securityId
, com::sun::star::xml::crypto::SecurityOperationStatus nResult
)
1459 throw (com::sun::star::uno::RuntimeException
)
1461 int index
= findSignatureInfor(securityId
);
1462 DBG_ASSERT( index
!= -1, "Signature Not Found!" );
1464 SignatureInformation
& signatureInfor
= m_vInternalSignatureInformations
[index
].signatureInfor
;
1467 if (nResult == cssxc::sax::SignatureCreationResult_CREATIONSUCCEED)
1469 signatureInfor.nStatus = STATUS_CREATION_SUCCEED;
1473 signatureInfor.nStatus = STATUS_CREATION_FAIL;
1476 signatureInfor
.nStatus
= nResult
;
1480 * XSignatureVerifyResultListener
1482 void SAL_CALL
XSecController::signatureVerified( sal_Int32 securityId
, com::sun::star::xml::crypto::SecurityOperationStatus nResult
)
1483 throw (com::sun::star::uno::RuntimeException
)
1485 int index
= findSignatureInfor(securityId
);
1486 DBG_ASSERT( index
!= -1, "Signature Not Found!" );
1488 SignatureInformation
& signatureInfor
= m_vInternalSignatureInformations
[index
].signatureInfor
;
1491 if (nResult == cssxc::sax::SignatureVerifyResult_VERIFYSUCCEED)
1493 signatureInfor.nStatus = STATUS_VERIFY_SUCCEED;
1497 signatureInfor.nStatus = STATUS_VERIFY_FAIL;
1500 signatureInfor
.nStatus
= nResult
;