1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
30 
31 #include <xsecctl.hxx>
32 #include <tools/debug.hxx>
33 
34 #include <com/sun/star/xml/crypto/sax/ElementMarkPriority.hpp>
35 #include <com/sun/star/xml/crypto/sax/XReferenceResolvedBroadcaster.hpp>
36 #include <com/sun/star/xml/crypto/sax/XMissionTaker.hpp>
37 #include <com/sun/star/xml/crypto/sax/XReferenceCollector.hpp>
38 #include <com/sun/star/xml/crypto/sax/XSAXEventKeeperStatusChangeBroadcaster.hpp>
39 #include <com/sun/star/xml/crypto/SecurityOperationStatus.hpp>
40 
41 #include <xmloff/attrlist.hxx>
42 #include <rtl/math.hxx>
43 #include <tools/string.hxx>
44 
45 namespace cssu = com::sun::star::uno;
46 namespace cssl = com::sun::star::lang;
47 namespace cssxc = com::sun::star::xml::crypto;
48 namespace cssxs = com::sun::star::xml::sax;
49 namespace cssxw = com::sun::star::xml::wrapper;
50 namespace cssb = com::sun::star::beans;
51 
52 const sal_Int8 XML_MAXDIGITSCOUNT_TIME = 11;
53 const sal_Int8 XML_MAXDIGITSCOUNT_DATETIME = 6;
54 
55 /* bridge component names */
56 #define XMLSIGNATURE_COMPONENT "com.sun.star.xml.crypto.XMLSignature"
57 #define XMLDOCUMENTWRAPPER_COMPONENT "com.sun.star.xml.wrapper.XMLDocumentWrapper"
58 
59 /* xml security framework components */
60 #define SAXEVENTKEEPER_COMPONENT "com.sun.star.xml.crypto.sax.SAXEventKeeper"
61 
62 /* string for package protocol */
63 #define PACKAGEPROTOCOL "vnd.sun.star.Package:"
64 
65 XSecController::XSecController( const cssu::Reference<cssu::XComponentContext>& rxCtx )
66 	:mxCtx(rxCtx),
67 	 m_nNextSecurityId(1),
68  	 m_bIsSAXEventKeeperConnected(false),
69 	 m_nStatusOfSecurityComponents(UNINITIALIZED),
70  	 m_bIsSAXEventKeeperSticky(false),
71 	 m_pErrorMessage(NULL),
72 	 m_pXSecParser(NULL)
73 {
74 }
75 
76 XSecController::~XSecController()
77 {
78 }
79 
80 
81 /*
82  * private methods
83  */
84 /** convert string to number with optional min and max values */
85 sal_Bool XSecController::convertNumber( sal_Int32& rValue,
86                                         const rtl::OUString& rString,
87                                         sal_Int32 /*nMin*/, sal_Int32 /*nMax*/ )
88 {
89     sal_Bool bNeg = sal_False;
90     rValue = 0;
91 
92     sal_Int32 nPos = 0L;
93     sal_Int32 nLen = rString.getLength();
94 
95     // skip white space
96     while( nPos < nLen && sal_Unicode(' ') == rString[nPos] )
97         nPos++;
98 
99     if( nPos < nLen && sal_Unicode('-') == rString[nPos] )
100     {
101         bNeg = sal_True;
102         nPos++;
103     }
104 
105     // get number
106     while( nPos < nLen &&
107            sal_Unicode('0') <= rString[nPos] &&
108            sal_Unicode('9') >= rString[nPos] )
109     {
110         // TODO: check overflow!
111         rValue *= 10;
112         rValue += (rString[nPos] - sal_Unicode('0'));
113         nPos++;
114     }
115 
116     if( bNeg )
117         rValue *= -1;
118 
119     return nPos == nLen;
120 }
121 
122 /** convert util::DateTime to ISO Date String */
123 void XSecController::convertDateTime( ::rtl::OUStringBuffer& rBuffer,
124                                 const com::sun::star::util::DateTime& rDateTime )
125 {
126     String aString( String::CreateFromInt32( rDateTime.Year ) );
127     aString += '-';
128     if( rDateTime.Month < 10 )
129         aString += '0';
130     aString += String::CreateFromInt32( rDateTime.Month );
131     aString += '-';
132     if( rDateTime.Day < 10 )
133         aString += '0';
134     aString += String::CreateFromInt32( rDateTime.Day );
135 
136     if( rDateTime.Seconds != 0 ||
137         rDateTime.Minutes != 0 ||
138         rDateTime.Hours   != 0 )
139     {
140         aString += 'T';
141         if( rDateTime.Hours < 10 )
142             aString += '0';
143         aString += String::CreateFromInt32( rDateTime.Hours );
144         aString += ':';
145         if( rDateTime.Minutes < 10 )
146             aString += '0';
147         aString += String::CreateFromInt32( rDateTime.Minutes );
148         aString += ':';
149         if( rDateTime.Seconds < 10 )
150             aString += '0';
151         aString += String::CreateFromInt32( rDateTime.Seconds );
152 		if ( rDateTime.HundredthSeconds > 0)
153 		{
154 	        aString += ',';
155 			if (rDateTime.HundredthSeconds < 10)
156 				aString += '0';
157 			aString += String::CreateFromInt32( rDateTime.HundredthSeconds );
158 		}
159     }
160 
161     rBuffer.append( aString );
162 }
163 
164 /** convert ISO Date String to util::DateTime */
165 sal_Bool XSecController::convertDateTime( com::sun::star::util::DateTime& rDateTime,
166                                      const ::rtl::OUString& rString )
167 {
168     sal_Bool bSuccess = sal_True;
169 
170     rtl::OUString aDateStr, aTimeStr, sHundredth;
171     sal_Int32 nPos = rString.indexOf( (sal_Unicode) 'T' );
172     sal_Int32 nPos2 = rString.indexOf( (sal_Unicode) ',' );
173     if ( nPos >= 0 )
174     {
175         aDateStr = rString.copy( 0, nPos );
176         if ( nPos2 >= 0 )
177         {
178             aTimeStr = rString.copy( nPos + 1, nPos2 - nPos - 1 );
179 
180 			//Get the fraction of a second with the accuracy of one hundreds second.
181 			//The fraction part of the date could have different accuracies. To calculate
182 			//the count of a hundredth units one could form a fractional number by appending
183 			//the value of the time string to 0. Then multiply it by 100 and use only the whole number.
184 			//For example: 5:27:46,1 -> 0,1 * 100 = 10
185 			//5:27:46,01 -> 0,01 * 100 = 1
186 			//5:27:46,001 -> 0,001 * 100 = 0
187 			//Due to the inaccuracy of floating point numbers the result may not be the same on different
188 			//platforms. We had the case where we had a value of 24 hundredth of second, which converted to
189 			//23 on Linux and 24 on Solaris and Windows.
190 
191 			//we only support a hundredth second
192 			//make ,1 -> 10   ,01 -> 1    ,001 -> only use first two diggits
193 			sHundredth = rString.copy(nPos2 + 1);
194 			sal_Int32 len = sHundredth.getLength();
195 			if (len == 1)
196 				sHundredth += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("0"));
197 			if (len > 2)
198 				sHundredth = sHundredth.copy(0, 2);
199         }
200         else
201         {
202             aTimeStr = rString.copy(nPos + 1);
203 			sHundredth = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("0"));
204         }
205     }
206     else
207         aDateStr = rString;         // no separator: only date part
208 
209     sal_Int32 nYear  = 1899;
210     sal_Int32 nMonth = 12;
211     sal_Int32 nDay   = 30;
212     sal_Int32 nHour  = 0;
213     sal_Int32 nMin   = 0;
214     sal_Int32 nSec   = 0;
215 
216     const sal_Unicode* pStr = aDateStr.getStr();
217     sal_Int32 nDateTokens = 1;
218     while ( *pStr )
219     {
220         if ( *pStr == '-' )
221             nDateTokens++;
222         pStr++;
223     }
224     if ( nDateTokens > 3 || aDateStr.getLength() == 0 )
225         bSuccess = sal_False;
226     else
227     {
228         sal_Int32 n = 0;
229         if ( !convertNumber( nYear, aDateStr.getToken( 0, '-', n ), 0, 9999 ) )
230             bSuccess = sal_False;
231         if ( nDateTokens >= 2 )
232             if ( !convertNumber( nMonth, aDateStr.getToken( 0, '-', n ), 0, 12 ) )
233                 bSuccess = sal_False;
234         if ( nDateTokens >= 3 )
235             if ( !convertNumber( nDay, aDateStr.getToken( 0, '-', n ), 0, 31 ) )
236                 bSuccess = sal_False;
237     }
238 
239     if ( aTimeStr.getLength() > 0 )           // time is optional
240     {
241         pStr = aTimeStr.getStr();
242         sal_Int32 nTimeTokens = 1;
243         while ( *pStr )
244         {
245             if ( *pStr == ':' )
246                 nTimeTokens++;
247             pStr++;
248         }
249         if ( nTimeTokens > 3 )
250             bSuccess = sal_False;
251         else
252         {
253             sal_Int32 n = 0;
254             if ( !convertNumber( nHour, aTimeStr.getToken( 0, ':', n ), 0, 23 ) )
255                 bSuccess = sal_False;
256             if ( nTimeTokens >= 2 )
257                 if ( !convertNumber( nMin, aTimeStr.getToken( 0, ':', n ), 0, 59 ) )
258                     bSuccess = sal_False;
259             if ( nTimeTokens >= 3 )
260                 if ( !convertNumber( nSec, aTimeStr.getToken( 0, ':', n ), 0, 59 ) )
261                     bSuccess = sal_False;
262         }
263     }
264 
265     if (bSuccess)
266     {
267         rDateTime.Year = (sal_uInt16)nYear;
268         rDateTime.Month = (sal_uInt16)nMonth;
269         rDateTime.Day = (sal_uInt16)nDay;
270         rDateTime.Hours = (sal_uInt16)nHour;
271         rDateTime.Minutes = (sal_uInt16)nMin;
272         rDateTime.Seconds = (sal_uInt16)nSec;
273  //       rDateTime.HundredthSeconds = sDoubleStr.toDouble() * 100;
274 		rDateTime.HundredthSeconds = static_cast<sal_uInt16>(sHundredth.toInt32());
275     }
276     return bSuccess;
277 }
278 
279 int XSecController::findSignatureInfor( sal_Int32 nSecurityId) const
280 /****** XSecController/findSignatureInfor *************************************
281  *
282  *   NAME
283  *	findSignatureInfor -- find SignatureInformation struct for a particular
284  *	                      signature
285  *
286  *   SYNOPSIS
287  *	index = findSignatureInfor( nSecurityId );
288  *
289  *   FUNCTION
290  *	see NAME.
291  *
292  *   INPUTS
293  *	nSecurityId - the signature's id
294  *
295  *   RESULT
296  *	index - the index of the signature, or -1 when no such signature
297  *	        existing
298  *
299  *   HISTORY
300  *	08.05.2004 -	implemented
301  *
302  *   AUTHOR
303  *	Michael Mi
304  *	Email: michael.mi@sun.com
305  ******************************************************************************/
306 {
307 	int i;
308 	int size = m_vInternalSignatureInformations.size();
309 
310 	for (i=0; i<size; ++i)
311 	{
312 		if (m_vInternalSignatureInformations[i].signatureInfor.nSecurityId == nSecurityId)
313 		{
314 			return i;
315 		}
316 	}
317 
318 	return -1;
319 }
320 
321 void XSecController::createXSecComponent( )
322 /****** XSecController/createXSecComponent ************************************
323  *
324  *   NAME
325  *	bResult = createXSecComponent -- creates xml security components
326  *
327  *   SYNOPSIS
328  *	createXSecComponent( );
329  *
330  *   FUNCTION
331  *	Creates xml security components, including:
332  *	1. an xml signature bridge component ( Java based or C based)
333  *	2. an XMLDocumentWrapper component ( Java based or C based)
334  *	3. a SAXEventKeeper component
335  *
336  *   INPUTS
337  *	empty
338  *
339  *   RESULT
340  *	empty
341  *
342  *   HISTORY
343  *	05.01.2004 -	implemented
344  *
345  *   AUTHOR
346  *	Michael Mi
347  *	Email: michael.mi@sun.com
348  ******************************************************************************/
349 {
350 	rtl::OUString sSAXEventKeeper(rtl::OUString::createFromAscii( SAXEVENTKEEPER_COMPONENT ));
351 	rtl::OUString sXMLSignature(rtl::OUString::createFromAscii( XMLSIGNATURE_COMPONENT ));
352 	rtl::OUString sXMLDocument(rtl::OUString::createFromAscii( XMLDOCUMENTWRAPPER_COMPONENT ));
353 
354 	/*
355 	 * marks all security components are not available.
356 	 */
357 	m_nStatusOfSecurityComponents = FAILTOINITIALIZED;
358 	m_xXMLSignature = NULL;
359 	m_xXMLDocumentWrapper = NULL;
360 	m_xSAXEventKeeper = NULL;
361 
362 	cssu::Reference< cssl::XMultiComponentFactory > xMCF( mxCtx->getServiceManager() );
363 
364 	m_xXMLSignature = cssu::Reference< cssxc::XXMLSignature >(
365 		xMCF->createInstanceWithContext( sXMLSignature, mxCtx ),
366 		cssu::UNO_QUERY );
367 
368 	bool bSuccess = (0!=m_xXMLSignature.is());
369 	if ( bSuccess )
370 	/*
371 	 * XMLSignature created successfully.
372 	 */
373 	{
374 		m_xXMLDocumentWrapper = cssu::Reference< cssxw::XXMLDocumentWrapper >(
375 			xMCF->createInstanceWithContext( sXMLDocument, mxCtx ),
376 			cssu::UNO_QUERY );
377 	}
378 
379 	bSuccess &= (0!=m_xXMLDocumentWrapper.is());
380 	if ( bSuccess )
381 	/*
382 	 * XMLDocumentWrapper created successfully.
383 	 */
384 	{
385 		m_xSAXEventKeeper = cssu::Reference< cssxc::sax::XSecuritySAXEventKeeper >(
386 			xMCF->createInstanceWithContext( sSAXEventKeeper, mxCtx ),
387 			cssu::UNO_QUERY );
388 	}
389 
390 	bSuccess &= (0!=m_xSAXEventKeeper.is());
391 
392 	if (bSuccess)
393 	/*
394 	 * SAXEventKeeper created successfully.
395 	 */
396 	{
397 		cssu::Reference< cssl::XInitialization > xInitialization(m_xSAXEventKeeper,  cssu::UNO_QUERY);
398 
399 		cssu::Sequence <cssu::Any> arg(1);
400 		arg[0] = cssu::makeAny(m_xXMLDocumentWrapper);
401 		xInitialization->initialize(arg);
402 
403 		cssu::Reference<cssxc::sax::XSAXEventKeeperStatusChangeBroadcaster>
404 			xSAXEventKeeperStatusChangeBroadcaster(m_xSAXEventKeeper, cssu::UNO_QUERY);
405 		cssu::Reference< cssxc::sax::XSAXEventKeeperStatusChangeListener >
406 			xStatusChangeListener = this;
407 
408 		xSAXEventKeeperStatusChangeBroadcaster
409 			->addSAXEventKeeperStatusChangeListener( xStatusChangeListener );
410 
411 		m_nStatusOfSecurityComponents = INITIALIZED;
412 	}
413 }
414 
415 bool XSecController::chainOn( bool bRetrievingLastEvent )
416 /****** XSecController/chainOn ************************************************
417  *
418  *   NAME
419  *	chainOn -- tyies to connect the SAXEventKeeper with the SAX chain.
420  *
421  *   SYNOPSIS
422  *	bJustChainingOn = chainOn( bRetrievingLastEvent );
423  *
424  *   FUNCTION
425  *	First, checks whether the SAXEventKeeper is on the SAX chain. If not,
426  *	creates xml security components, and chains the SAXEventKeeper into
427  *	the SAX chain.
428  *	Before being chained in, the SAXEventKeeper needs to receive all
429  *	missed key SAX events, which can promise the DOM tree bufferred by the
430  *	SAXEventKeeper has the same structure with the original document.
431  *
432  *   INPUTS
433  *	bRetrievingLastEvent - whether to retrieve the last key SAX event from
434  *	                       the ElementStackKeeper.
435  *
436  *   RESULT
437  *	bJustChainingOn - whether the SAXEventKeeper is just chained into the
438  *	                  SAX chain.
439  *
440  *   NOTES
441  *	Sometimes, the last key SAX event can't be transferred to the
442  *	SAXEventKeeper together.
443  *	For instance, at the time an referenced element is detected, the
444  *	startElement event has already been reserved by the ElementStackKeeper.
445  *	Meanwhile, an ElementCollector needs to be created before the
446  *	SAXEventKeeper receives that startElement event.
447  *	So for the SAXEventKeeper, it needs to receive all missed key SAX
448  *	events except that startElement event, then adds a new
449  *	ElementCollector, then receives that startElement event.
450  *
451  *   HISTORY
452  *	05.01.2004 -	implemented
453  *
454  *   AUTHOR
455  *	Michael Mi
456  *	Email: michael.mi@sun.com
457  ******************************************************************************/
458 {
459 	bool rc = false;
460 
461 	if (!m_bIsSAXEventKeeperSticky && !m_bIsSAXEventKeeperConnected)
462 	{
463 		if ( m_nStatusOfSecurityComponents == UNINITIALIZED )
464 		{
465 			createXSecComponent();
466 		}
467 
468 		if ( m_nStatusOfSecurityComponents == INITIALIZED )
469 		/*
470 		 * if all security components are ready, chains on the SAXEventKeeper
471 		 */
472 		{
473 			/*
474 			 * disconnect the SAXEventKeeper with its current output handler,
475 			 * to make sure no SAX event is forwarded during the connecting
476 			 * phase.
477 			 */
478 			m_xSAXEventKeeper->setNextHandler( NULL );
479 
480 			cssu::Reference< cssxs::XDocumentHandler > xSEKHandler(m_xSAXEventKeeper, cssu::UNO_QUERY);
481 
482 			/*
483 			 * connects the previous document handler on the SAX chain
484 			 */
485 			if ( m_xPreviousNodeOnSAXChain.is() )
486 			{
487 				if ( m_bIsPreviousNodeInitializable )
488 				{
489 					cssu::Reference< cssl::XInitialization > xInitialization
490 						(m_xPreviousNodeOnSAXChain, cssu::UNO_QUERY);
491 
492 					cssu::Sequence<cssu::Any> aArgs( 1 );
493 					aArgs[0] <<= xSEKHandler;
494 					xInitialization->initialize(aArgs);
495 				}
496 				else
497 				{
498 					cssu::Reference< cssxs::XParser > xParser
499 						(m_xPreviousNodeOnSAXChain, cssu::UNO_QUERY);
500 					xParser->setDocumentHandler( xSEKHandler );
501 				}
502 			}
503 
504 			/*
505 			 * get missed key SAX events
506 			 */
507 			if (m_xElementStackKeeper.is())
508 			{
509 				m_xElementStackKeeper->retrieve(xSEKHandler, bRetrievingLastEvent);
510 
511 				/*
512 				 * now the ElementStackKeeper can stop its work, because the
513 				 * SAXEventKeeper is on the SAX chain, no SAX events will be
514 				 * missed.
515 				 */
516 				m_xElementStackKeeper->stop();
517 			}
518 
519 			/*
520 			 * connects the next document handler on the SAX chain
521 			 */
522 			m_xSAXEventKeeper->setNextHandler( m_xNextNodeOnSAXChain );
523 
524 			m_bIsSAXEventKeeperConnected = true;
525 
526 			rc = true;
527 		}
528 	}
529 
530 	return rc;
531 }
532 
533 void XSecController::chainOff()
534 /****** XSecController/chainOff ***********************************************
535  *
536  *   NAME
537  *	chainOff -- disconnects the SAXEventKeeper from the SAX chain.
538  *
539  *   SYNOPSIS
540  *	chainOff( );
541  *
542  *   FUNCTION
543  *	See NAME.
544  *
545  *   INPUTS
546  *	empty
547  *
548  *   RESULT
549  *	empty
550  *
551  *   HISTORY
552  *	05.01.2004 -	implemented
553  *
554  *   AUTHOR
555  *	Michael Mi
556  *	Email: michael.mi@sun.com
557  ******************************************************************************/
558 {
559 	if (!m_bIsSAXEventKeeperSticky )
560 	{
561 		if (m_bIsSAXEventKeeperConnected)
562 		{
563 			m_xSAXEventKeeper->setNextHandler( NULL );
564 
565 			if ( m_xPreviousNodeOnSAXChain.is() )
566 			{
567 				if ( m_bIsPreviousNodeInitializable )
568 				{
569 					cssu::Reference< cssl::XInitialization > xInitialization
570 						(m_xPreviousNodeOnSAXChain, cssu::UNO_QUERY);
571 
572 					cssu::Sequence<cssu::Any> aArgs( 1 );
573 					aArgs[0] <<= m_xNextNodeOnSAXChain;
574 					xInitialization->initialize(aArgs);
575 				}
576 				else
577 				{
578 					cssu::Reference< cssxs::XParser > xParser(m_xPreviousNodeOnSAXChain, cssu::UNO_QUERY);
579 					xParser->setDocumentHandler( m_xNextNodeOnSAXChain );
580 				}
581 			}
582 
583 			if (m_xElementStackKeeper.is())
584 			{
585 				/*
586 				 * start the ElementStackKeeper to reserve any possible
587 				 * missed key SAX events
588 				 */
589 				m_xElementStackKeeper->start();
590 			}
591 
592 			m_bIsSAXEventKeeperConnected = false;
593 		}
594 	}
595 }
596 
597 void XSecController::checkChainingStatus()
598 /****** XSecController/checkChainingStatus ************************************
599  *
600  *   NAME
601  *	checkChainingStatus -- connects or disconnects the SAXEventKeeper
602  *	according to the current situation.
603  *
604  *   SYNOPSIS
605  *	checkChainingStatus( );
606  *
607  *   FUNCTION
608  *	The SAXEventKeeper is chained into the SAX chain, when:
609  *	1. some element is being collected, or
610  *	2. the SAX event stream is blocking.
611  *	Otherwise, chain off the SAXEventKeeper.
612  *
613  *   INPUTS
614  *	empty
615  *
616  *   RESULT
617  *	empty
618  *
619  *   HISTORY
620  *	05.01.2004 -	implemented
621  *
622  *   AUTHOR
623  *	Michael Mi
624  *	Email: michael.mi@sun.com
625  ******************************************************************************/
626 {
627 	if ( m_bIsCollectingElement || m_bIsBlocking )
628 	{
629 		chainOn(true);
630 	}
631 	else
632 	{
633 		chainOff();
634 	}
635 }
636 
637 void XSecController::initializeSAXChain()
638 /****** XSecController/initializeSAXChain *************************************
639  *
640  *   NAME
641  *	initializeSAXChain -- initializes the SAX chain according to the
642  *	current setting.
643  *
644  *   SYNOPSIS
645  *	initializeSAXChain( );
646  *
647  *   FUNCTION
648  *	Initializes the SAX chain, if the SAXEventKeeper is asked to be always
649  *	on the SAX chain, chains it on. Otherwise, starts the
650  *	ElementStackKeeper to reserve key SAX events.
651  *
652  *   INPUTS
653  *	empty
654  *
655  *   RESULT
656  *	empty
657  *
658  *   HISTORY
659  *	05.01.2004 -	implemented
660  *
661  *   AUTHOR
662  *	Michael Mi
663  *	Email: michael.mi@sun.com
664  ******************************************************************************/
665 {
666 	m_bIsSAXEventKeeperConnected = false;
667 	m_bIsCollectingElement = false;
668 	m_bIsBlocking = false;
669 
670 	if (m_xElementStackKeeper.is())
671 	{
672 		/*
673 		 * starts the ElementStackKeeper
674 		 */
675 		m_xElementStackKeeper->start();
676 	}
677 
678 	chainOff();
679 }
680 
681 cssu::Reference< com::sun::star::io::XInputStream >
682 	XSecController::getObjectInputStream( const rtl::OUString& objectURL )
683 /****** XSecController/getObjectInputStream ************************************
684  *
685  *   NAME
686  *	getObjectInputStream -- get a XInputStream interface from a SvStorage
687  *
688  *   SYNOPSIS
689  *	xInputStream = getObjectInputStream( objectURL );
690  *
691  *   FUNCTION
692  *	See NAME.
693  *
694  *   INPUTS
695  *	objectURL - the object uri
696  *
697  *   RESULT
698  *	xInputStream - the XInputStream interface
699  *
700  *   HISTORY
701  *	15.04.2004 -	implemented
702  *
703  *   AUTHOR
704  *	Michael Mi
705  *	Email: michael.mi@sun.com
706  ******************************************************************************/
707 {
708         cssu::Reference< com::sun::star::io::XInputStream > xObjectInputStream;
709 
710 	DBG_ASSERT( m_xUriBinding.is(), "Need XUriBinding!" );
711 
712 	xObjectInputStream = m_xUriBinding->getUriBinding(objectURL);
713 
714 	return xObjectInputStream;
715 }
716 
717 #if 0
718 sal_Int32 XSecController::getFastPropertyIndex(sal_Int32 nHandle) const
719 /****** XSecController/getFastPropertyIndex ***********************************
720  *
721  *   NAME
722  *	getFastPropertyIndex -- gets the index of a particular fast property
723  *
724  *   SYNOPSIS
725  *	nIndex = getFastPropertyIndex( nHandle );
726  *
727  *   FUNCTION
728  *	See NAME.
729  *
730  *   INPUTS
731  *	nHandle - the key for the fast property
732  *
733  *   RESULT
734  *	nIndex - the index of the fast property, or -1
735  *	         if the key is not found.
736  *
737  *   HISTORY
738  *	05.01.2004 -	implemented
739  *
740  *   AUTHOR
741  *	Michael Mi
742  *	Email: michael.mi@sun.com
743  ******************************************************************************/
744 {
745 	std::vector< sal_Int32 >::const_iterator ii = m_vFastPropertyIndexs.begin();
746 	sal_Int32 nIndex = 0;
747 
748 	bool bFound = false;
749 
750 	for( ; ii != m_vFastPropertyIndexs.end(); ++ii,++nIndex )
751 	{
752 		if ( nHandle == (*ii))
753 		{
754 			bFound = true;
755 			break;
756 		}
757 	}
758 
759 	if (!bFound)
760 	{
761 		nIndex = -1;
762 	}
763 
764 	return nIndex;
765 }
766 #endif
767 
768 /*
769  * public methods
770  */
771 
772 sal_Int32 XSecController::getNewSecurityId(  )
773 {
774 	sal_Int32 nId = m_nNextSecurityId;
775 	m_nNextSecurityId++;
776 	return nId;
777 }
778 
779 void XSecController::startMission(
780 	const cssu::Reference< cssxc::XUriBinding >& xUriBinding,
781 	const cssu::Reference< cssxc::XXMLSecurityContext >& xSecurityContext )
782 /****** XSecController/startMission *******************************************
783  *
784  *   NAME
785  *	startMission -- starts a new security mission.
786  *
787  *   SYNOPSIS
788  *	startMission( xUriBinding, xSecurityContect );
789  *
790  *   FUNCTION
791  *	get ready for a new mission.
792  *
793  *   INPUTS
794  *	xUriBinding       - the Uri binding that provide maps between uris and
795  *                          XInputStreams
796  *	xSecurityContext  - the security context component which can provide
797  *	                    cryptoken
798  *
799  *   RESULT
800  *	empty
801  *
802  *   HISTORY
803  *	05.01.2004 -	implemented
804  *
805  *   AUTHOR
806  *	Michael Mi
807  *	Email: michael.mi@sun.com
808  ******************************************************************************/
809 {
810 	m_xUriBinding = xUriBinding;
811 
812 	m_nStatusOfSecurityComponents = UNINITIALIZED;
813 	m_xSecurityContext = xSecurityContext;
814 	m_pErrorMessage = NULL;
815 
816 	m_vInternalSignatureInformations.clear();
817 
818 	m_bVerifyCurrentSignature = false;
819 }
820 
821 void XSecController::setSAXChainConnector(
822 	const cssu::Reference< cssl::XInitialization >& xInitialization,
823 	const cssu::Reference< cssxs::XDocumentHandler >& xDocumentHandler,
824 	const cssu::Reference< cssxc::sax::XElementStackKeeper >& xElementStackKeeper)
825 /****** XSecController/setSAXChainConnector ***********************************
826  *
827  *   NAME
828  *	setSAXChainConnector -- configures the components which will
829  *	collaborate with the SAXEventKeeper on the SAX chain.
830  *
831  *   SYNOPSIS
832  *	setSAXChainConnector( xInitialization,
833  *	                      xDocumentHandler,
834  *	                      xElementStackKeeper );
835  *
836  *   FUNCTION
837  *	See NAME.
838  *
839  *   INPUTS
840  *	xInitialization     - the previous node on the SAX chain
841  *	xDocumentHandler    - the next node on the SAX chain
842  *	xElementStackKeeper - the ElementStackKeeper component which reserves
843  *	                      missed key SAX events for the SAXEventKeeper
844  *
845  *   RESULT
846  *	empty
847  *
848  *   HISTORY
849  *	05.01.2004 -	implemented
850  *
851  *   AUTHOR
852  *	Michael Mi
853  *	Email: michael.mi@sun.com
854  ******************************************************************************/
855 {
856 	m_bIsPreviousNodeInitializable = true;
857 	m_xPreviousNodeOnSAXChain = xInitialization;
858 	m_xNextNodeOnSAXChain = xDocumentHandler;
859 	m_xElementStackKeeper = xElementStackKeeper;
860 
861 	initializeSAXChain( );
862 }
863 
864 void XSecController::setSAXChainConnector(
865 	const cssu::Reference< cssxs::XParser >& xParser,
866 	const cssu::Reference< cssxs::XDocumentHandler >& xDocumentHandler,
867 	const cssu::Reference< cssxc::sax::XElementStackKeeper >& xElementStackKeeper)
868 /****** XSecController/setSAXChainConnector ***********************************
869  *
870  *   NAME
871  *	setSAXChainConnector -- configures the components which will
872  *	collaborate with the SAXEventKeeper on the SAX chain.
873  *
874  *   SYNOPSIS
875  *	setSAXChainConnector( xParser, xDocumentHandler, xElementStackKeeper );
876  *
877  *   FUNCTION
878  *	See NAME.
879  *
880  *   INPUTS
881  *	xParser             - the previous node on the SAX chain
882  *	xDocumentHandler    - the next node on the SAX chain
883  *	xElementStackKeeper -the ElementStackKeeper component which reserves
884  *	                      missed key SAX events for the SAXEventKeeper
885  *
886  *   RESULT
887  *	empty
888  *
889  *   HISTORY
890  *	05.01.2004 -	implemented
891  *
892  *   AUTHOR
893  *	Michael Mi
894  *	Email: michael.mi@sun.com
895  ******************************************************************************/
896 {
897 	m_bIsPreviousNodeInitializable = false;
898 	m_xPreviousNodeOnSAXChain = xParser;
899 	m_xNextNodeOnSAXChain = xDocumentHandler;
900 	m_xElementStackKeeper = xElementStackKeeper;
901 
902 	initializeSAXChain( );
903 }
904 
905 void XSecController::clearSAXChainConnector()
906 /****** XSecController/clearSAXChainConnector *********************************
907  *
908  *   NAME
909  *	clearSAXChainConnector -- resets the collaborating components.
910  *
911  *   SYNOPSIS
912  *	clearSAXChainConnector( );
913  *
914  *   FUNCTION
915  *	See NAME.
916  *
917  *   INPUTS
918  *	empty
919  *
920  *   RESULT
921  *	empty
922  *
923  *   HISTORY
924  *	05.01.2004 -	implemented
925  *
926  *   AUTHOR
927  *	Michael Mi
928  *	Email: michael.mi@sun.com
929  ******************************************************************************/
930 {
931 	/*
932 	 * before reseting, if the ElementStackKeeper has kept something, then
933 	 * those kept key SAX events must be transferred to the SAXEventKeeper
934 	 * first. This is to promise the next node to the SAXEventKeeper on the
935 	 * SAX chain always receives a complete document.
936 	 */
937 	if (m_xElementStackKeeper.is() && m_xSAXEventKeeper.is())
938 	{
939 		cssu::Reference< cssxs::XDocumentHandler > xSEKHandler(m_xSAXEventKeeper, cssu::UNO_QUERY);
940 		m_xElementStackKeeper->retrieve(xSEKHandler, sal_True);
941 	}
942 
943 	chainOff();
944 
945 	m_xPreviousNodeOnSAXChain = NULL;
946 	m_xNextNodeOnSAXChain = NULL;
947 	m_xElementStackKeeper = NULL;
948 }
949 
950 void XSecController::endMission()
951 /****** XSecController/endMission *********************************************
952  *
953  *   NAME
954  *	endMission -- forces to end all missions
955  *
956  *   SYNOPSIS
957  *	endMission( );
958  *
959  *   FUNCTION
960  *	Deletes all signature information and forces all missions to an end.
961  *
962  *   INPUTS
963  *	empty
964  *
965  *   RESULT
966  *	empty
967  *
968  *   HISTORY
969  *	05.01.2004 -	implemented
970  *
971  *   AUTHOR
972  *	Michael Mi
973  *	Email: michael.mi@sun.com
974  ******************************************************************************/
975 {
976 	sal_Int32 size = m_vInternalSignatureInformations.size();
977 
978 	for (int i=0; i<size; ++i)
979 	{
980 		if ( m_nStatusOfSecurityComponents == INITIALIZED )
981 		/*
982 		 * ResolvedListener only exist when the security components are created.
983 		 */
984 		{
985 			cssu::Reference< cssxc::sax::XMissionTaker > xMissionTaker
986 				( m_vInternalSignatureInformations[i].xReferenceResolvedListener, cssu::UNO_QUERY );
987 
988 			/*
989 			 * askes the SignatureCreator/SignatureVerifier to release
990 			 * all resouces it uses.
991 			 */
992 			xMissionTaker->endMission();
993 		}
994 	}
995 
996 	m_xUriBinding = NULL;
997 	m_xSecurityContext = NULL;
998 
999 	/*
1000 	 * free the status change listener reference to this object
1001 	 */
1002 	if (m_xSAXEventKeeper.is())
1003 	{
1004 		cssu::Reference<cssxc::sax::XSAXEventKeeperStatusChangeBroadcaster>
1005 			xSAXEventKeeperStatusChangeBroadcaster(m_xSAXEventKeeper, cssu::UNO_QUERY);
1006 		xSAXEventKeeperStatusChangeBroadcaster
1007 			->addSAXEventKeeperStatusChangeListener( NULL );
1008 	}
1009 }
1010 
1011 const char* XSecController::getErrorMessage()
1012 /****** XSecController/getErrorMessage ****************************************
1013  *
1014  *   NAME
1015  *	getErrorMessage -- get the last error message
1016  *
1017  *   SYNOPSIS
1018  *	pErrorMessage = getErrorMessage( );
1019  *
1020  *   FUNCTION
1021  *	see NAME.
1022  *
1023  *   INPUTS
1024  *	empty
1025  *
1026  *   RESULT
1027  *	empty
1028  *
1029  *   HISTORY
1030  *	22.04.2004 -	implemented
1031  *
1032  *   AUTHOR
1033  *	Michael Mi
1034  *	Email: michael.mi@sun.com
1035  ******************************************************************************/
1036 {
1037 	return m_pErrorMessage;
1038 }
1039 
1040 void XSecController::exportSignature(
1041 	const cssu::Reference<cssxs::XDocumentHandler>& xDocumentHandler,
1042 	const SignatureInformation& signatureInfo )
1043 /****** XSecController/exportSignature ****************************************
1044  *
1045  *   NAME
1046  *	exportSignature -- export a signature structure to an XDocumentHandler
1047  *
1048  *   SYNOPSIS
1049  *	exportSignature( xDocumentHandler, signatureInfo);
1050  *
1051  *   FUNCTION
1052  *	see NAME.
1053  *
1054  *   INPUTS
1055  *	xDocumentHandler    - the document handler to receive the signature
1056  *	signatureInfo       - signature to be exported
1057  *
1058  *   RESULT
1059  *	empty
1060  *
1061  *   HISTORY
1062  *	26.05.2004 -	implemented
1063  *
1064  *   AUTHOR
1065  *	Michael Mi
1066  *	Email: michael.mi@sun.com
1067  ******************************************************************************/
1068 {
1069 	/*
1070 	 * defines all element tags in Signature element.
1071 	 */
1072 	rtl::OUString tag_Signature(RTL_CONSTASCII_USTRINGPARAM(TAG_SIGNATURE));
1073 		rtl::OUString tag_SignedInfo(RTL_CONSTASCII_USTRINGPARAM(TAG_SIGNEDINFO));
1074 			rtl::OUString tag_CanonicalizationMethod(RTL_CONSTASCII_USTRINGPARAM(TAG_CANONICALIZATIONMETHOD));
1075 			rtl::OUString tag_SignatureMethod(RTL_CONSTASCII_USTRINGPARAM(TAG_SIGNATUREMETHOD));
1076 			rtl::OUString tag_Reference(RTL_CONSTASCII_USTRINGPARAM(TAG_REFERENCE));
1077 				rtl::OUString tag_Transforms(RTL_CONSTASCII_USTRINGPARAM(TAG_TRANSFORMS));
1078 					rtl::OUString tag_Transform(RTL_CONSTASCII_USTRINGPARAM(TAG_TRANSFORM));
1079 				rtl::OUString tag_DigestMethod(RTL_CONSTASCII_USTRINGPARAM(TAG_DIGESTMETHOD));
1080 				rtl::OUString tag_DigestValue(RTL_CONSTASCII_USTRINGPARAM(TAG_DIGESTVALUE));
1081 		rtl::OUString tag_SignatureValue(RTL_CONSTASCII_USTRINGPARAM(TAG_SIGNATUREVALUE));
1082 		rtl::OUString tag_KeyInfo(RTL_CONSTASCII_USTRINGPARAM(TAG_KEYINFO));
1083 			rtl::OUString tag_X509Data(RTL_CONSTASCII_USTRINGPARAM(TAG_X509DATA));
1084 				rtl::OUString tag_X509IssuerSerial(RTL_CONSTASCII_USTRINGPARAM(TAG_X509ISSUERSERIAL));
1085 					rtl::OUString tag_X509IssuerName(RTL_CONSTASCII_USTRINGPARAM(TAG_X509ISSUERNAME));
1086 					rtl::OUString tag_X509SerialNumber(RTL_CONSTASCII_USTRINGPARAM(TAG_X509SERIALNUMBER));
1087 					rtl::OUString tag_X509Certificate(RTL_CONSTASCII_USTRINGPARAM(TAG_X509CERTIFICATE));
1088 
1089 		rtl::OUString tag_Object(RTL_CONSTASCII_USTRINGPARAM(TAG_OBJECT));
1090 			rtl::OUString tag_SignatureProperties(RTL_CONSTASCII_USTRINGPARAM(TAG_SIGNATUREPROPERTIES));
1091 				rtl::OUString tag_SignatureProperty(RTL_CONSTASCII_USTRINGPARAM(TAG_SIGNATUREPROPERTY));
1092 					rtl::OUString tag_Date(RTL_CONSTASCII_USTRINGPARAM(TAG_DATE));
1093 #if 0
1094 					rtl::OUString tag_Timestamp(RTL_CONSTASCII_USTRINGPARAM(TAG_TIMESTAMP));
1095 						rtl::OUString tag_Date(RTL_CONSTASCII_USTRINGPARAM(TAG_DATE));
1096 						rtl::OUString tag_Time(RTL_CONSTASCII_USTRINGPARAM(TAG_TIME));
1097 #endif
1098 
1099 	const SignatureReferenceInformations& vReferenceInfors = signatureInfo.vSignatureReferenceInfors;
1100 	SvXMLAttributeList *pAttributeList;
1101 
1102 	/*
1103 	 * Write Signature element
1104 	 */
1105 	pAttributeList = new SvXMLAttributeList();
1106 	pAttributeList->AddAttribute(
1107 		rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_XMLNS)),
1108 		rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(NS_XMLDSIG)));
1109 
1110 	if (signatureInfo.ouSignatureId.getLength()>0)
1111 	{
1112 		pAttributeList->AddAttribute(
1113 			rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_ID)),
1114 			rtl::OUString(signatureInfo.ouSignatureId));
1115 	}
1116 
1117 	xDocumentHandler->startElement( tag_Signature, cssu::Reference< cssxs::XAttributeList > (pAttributeList));
1118 	{
1119 		/* Write SignedInfo element */
1120 		xDocumentHandler->startElement(
1121 			tag_SignedInfo,
1122 			cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
1123 		{
1124 			/* Write CanonicalizationMethod element */
1125 			pAttributeList = new SvXMLAttributeList();
1126 			pAttributeList->AddAttribute(
1127 				rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_ALGORITHM)),
1128 				rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ALGO_C14N)));
1129 			xDocumentHandler->startElement( tag_CanonicalizationMethod, cssu::Reference< cssxs::XAttributeList > (pAttributeList) );
1130 			xDocumentHandler->endElement( tag_CanonicalizationMethod );
1131 
1132 			/* Write SignatureMethod element */
1133 			pAttributeList = new SvXMLAttributeList();
1134 			pAttributeList->AddAttribute(
1135 				rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_ALGORITHM)),
1136 				rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ALGO_RSASHA1)));
1137 			xDocumentHandler->startElement( tag_SignatureMethod, cssu::Reference< cssxs::XAttributeList > (pAttributeList) );
1138 			xDocumentHandler->endElement( tag_SignatureMethod );
1139 
1140 			/* Write Reference element */
1141 			int j;
1142 			int refNum = vReferenceInfors.size();
1143 
1144 			for(j=0; j<refNum; ++j)
1145 			{
1146 				const SignatureReferenceInformation& refInfor = vReferenceInfors[j];
1147 
1148 				pAttributeList = new SvXMLAttributeList();
1149 				if ( refInfor.nType != TYPE_SAMEDOCUMENT_REFERENCE )
1150 				/*
1151 				 * stream reference
1152 				 */
1153 				{
1154 					pAttributeList->AddAttribute(
1155 						rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_URI)),
1156 						refInfor.ouURI);
1157 				}
1158 				else
1159 				/*
1160 				 * same-document reference
1161 				 */
1162 				{
1163 					pAttributeList->AddAttribute(
1164 						rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_URI)),
1165 						rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CHAR_FRAGMENT))+refInfor.ouURI);
1166 				}
1167 
1168 				xDocumentHandler->startElement( tag_Reference, cssu::Reference< cssxs::XAttributeList > (pAttributeList) );
1169 				{
1170 					/* Write Transforms element */
1171 					if (refInfor.nType == TYPE_XMLSTREAM_REFERENCE)
1172 					/*
1173 					 * xml stream, so c14n transform is needed
1174 					 */
1175 					{
1176 						xDocumentHandler->startElement(
1177 							tag_Transforms,
1178 							cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
1179 						{
1180 							pAttributeList = new SvXMLAttributeList();
1181 							pAttributeList->AddAttribute(
1182 								rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_ALGORITHM)),
1183 								rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ALGO_C14N)));
1184 							xDocumentHandler->startElement(
1185 								tag_Transform,
1186 								cssu::Reference< cssxs::XAttributeList > (pAttributeList) );
1187 							xDocumentHandler->endElement( tag_Transform );
1188 						}
1189 						xDocumentHandler->endElement( tag_Transforms );
1190 					}
1191 
1192 					/* Write DigestMethod element */
1193 					pAttributeList = new SvXMLAttributeList();
1194 					pAttributeList->AddAttribute(
1195 						rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_ALGORITHM)),
1196 						rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ALGO_XMLDSIGSHA1)));
1197 					xDocumentHandler->startElement(
1198 						tag_DigestMethod,
1199 						cssu::Reference< cssxs::XAttributeList > (pAttributeList) );
1200 					xDocumentHandler->endElement( tag_DigestMethod );
1201 
1202 					/* Write DigestValue element */
1203 					xDocumentHandler->startElement(
1204 						tag_DigestValue,
1205 						cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
1206 					xDocumentHandler->characters( refInfor.ouDigestValue );
1207 					xDocumentHandler->endElement( tag_DigestValue );
1208 				}
1209 				xDocumentHandler->endElement( tag_Reference );
1210 			}
1211 		}
1212 		xDocumentHandler->endElement( tag_SignedInfo );
1213 
1214 		/* Write SignatureValue element */
1215 		xDocumentHandler->startElement(
1216 			tag_SignatureValue,
1217 			cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
1218 		xDocumentHandler->characters( signatureInfo.ouSignatureValue );
1219 		xDocumentHandler->endElement( tag_SignatureValue );
1220 
1221 		/* Write KeyInfo element */
1222 		xDocumentHandler->startElement(
1223 			tag_KeyInfo,
1224 			cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
1225 		{
1226 			/* Write X509Data element */
1227 			xDocumentHandler->startElement(
1228 				tag_X509Data,
1229 				cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
1230 			{
1231 				/* Write X509IssuerSerial element */
1232 				xDocumentHandler->startElement(
1233 					tag_X509IssuerSerial,
1234 					cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
1235 				{
1236 					/* Write X509IssuerName element */
1237 					xDocumentHandler->startElement(
1238 						tag_X509IssuerName,
1239 						cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
1240 					xDocumentHandler->characters( signatureInfo.ouX509IssuerName );
1241 					xDocumentHandler->endElement( tag_X509IssuerName );
1242 
1243 					/* Write X509SerialNumber element */
1244 					xDocumentHandler->startElement(
1245 						tag_X509SerialNumber,
1246 						cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
1247 					xDocumentHandler->characters( signatureInfo.ouX509SerialNumber );
1248 					xDocumentHandler->endElement( tag_X509SerialNumber );
1249 				}
1250 				xDocumentHandler->endElement( tag_X509IssuerSerial );
1251 
1252 				/* Write X509Certificate element */
1253 				if (signatureInfo.ouX509Certificate.getLength()>0)
1254 				{
1255 					xDocumentHandler->startElement(
1256 						tag_X509Certificate,
1257 						cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
1258 					xDocumentHandler->characters( signatureInfo.ouX509Certificate );
1259 					xDocumentHandler->endElement( tag_X509Certificate );
1260 				}
1261 			}
1262 			xDocumentHandler->endElement( tag_X509Data );
1263 		}
1264 		xDocumentHandler->endElement( tag_KeyInfo );
1265 
1266 		/* Write Object element */
1267 		xDocumentHandler->startElement(
1268 			tag_Object,
1269 			cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
1270 		{
1271 			/* Write SignatureProperties element */
1272 			xDocumentHandler->startElement(
1273 				tag_SignatureProperties,
1274 				cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
1275 			{
1276 				/* Write SignatureProperty element */
1277 				pAttributeList = new SvXMLAttributeList();
1278 				pAttributeList->AddAttribute(
1279 					rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_ID)),
1280 					signatureInfo.ouPropertyId);
1281 				pAttributeList->AddAttribute(
1282 					rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_TARGET)),
1283 					rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CHAR_FRAGMENT))+signatureInfo.ouSignatureId);
1284 				xDocumentHandler->startElement(
1285 					tag_SignatureProperty,
1286 					cssu::Reference< cssxs::XAttributeList > (pAttributeList));
1287 				{
1288 					/* Write timestamp element */
1289 
1290 					pAttributeList = new SvXMLAttributeList();
1291 					pAttributeList->AddAttribute(
1292 						rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_XMLNS))
1293 							+rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(":"))
1294 							+rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(NSTAG_DC)),
1295 						rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(NS_DC)));
1296 
1297 					xDocumentHandler->startElement(
1298 						rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(NSTAG_DC))
1299 							+rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(":"))
1300 							+tag_Date,
1301 						cssu::Reference< cssxs::XAttributeList > (pAttributeList));
1302 
1303 					::rtl::OUStringBuffer buffer;
1304 					//If the xml signature was already contained in the document,
1305 					//then we use the original date and time string, rather then the
1306 					//converted one. When the original string is converted to the DateTime
1307 					//structure then information may be lost because it only holds a fractional
1308 					//of a second with a accuracy of one hundredth of second. If the string contains
1309 					//milli seconds (document was signed by an application other than OOo)
1310 					//and the converted time is written back, then the string looks different
1311 					//and the signature is broken.
1312 					if (signatureInfo.ouDateTime.getLength() > 0)
1313 						buffer = signatureInfo.ouDateTime;
1314 					else
1315 						convertDateTime( buffer, signatureInfo.stDateTime );
1316 					xDocumentHandler->characters( buffer.makeStringAndClear() );
1317 
1318 					xDocumentHandler->endElement(
1319 						rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(NSTAG_DC))
1320 							+rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(":"))
1321 							+tag_Date);
1322 				}
1323 				xDocumentHandler->endElement( tag_SignatureProperty );
1324 			}
1325 			xDocumentHandler->endElement( tag_SignatureProperties );
1326 		}
1327 		xDocumentHandler->endElement( tag_Object );
1328 	}
1329 	xDocumentHandler->endElement( tag_Signature );
1330 }
1331 
1332 SignatureInformation XSecController::getSignatureInformation( sal_Int32 nSecurityId ) const
1333 {
1334     SignatureInformation aInf( 0 );
1335 	int nIndex = findSignatureInfor(nSecurityId);
1336 	DBG_ASSERT( nIndex != -1, "getSignatureInformation - SecurityId is invalid!" );
1337 	if ( nIndex != -1)
1338 	{
1339 		aInf = m_vInternalSignatureInformations[nIndex].signatureInfor;
1340 	}
1341 	return aInf;
1342 }
1343 
1344 SignatureInformations XSecController::getSignatureInformations() const
1345 {
1346 	SignatureInformations vInfors;
1347 	int sigNum = m_vInternalSignatureInformations.size();
1348 
1349 	for (int i=0; i<sigNum; ++i)
1350 	{
1351 		SignatureInformation si = m_vInternalSignatureInformations[i].signatureInfor;
1352 		vInfors.push_back(si);
1353 	}
1354 
1355 	return vInfors;
1356 }
1357 
1358 /*
1359  * XSecurityController
1360  *
1361  * no methods
1362  */
1363 
1364 /*
1365  * XFastPropertySet
1366  */
1367 /*
1368 void SAL_CALL XSecController::setFastPropertyValue(
1369 	sal_Int32 nHandle,
1370 	const cssu::Any& aValue )
1371 	throw (	cssb::UnknownPropertyException,
1372 		cssb::PropertyVetoException,
1373 		cssl::IllegalArgumentException,
1374 		cssl::WrappedTargetException,
1375 		cssu::RuntimeException)
1376 {
1377 	sal_Int32 nIndex = getFastPropertyIndex(nHandle);
1378 	if (nIndex == -1)
1379 	{
1380 		m_vFastPropertyIndexs.push_back( nHandle );
1381 		m_vFastPropertyValues.push_back( aValue );
1382 	}
1383 	else
1384 	{
1385 		m_vFastPropertyValues[nIndex] = aValue;
1386 	}
1387 }
1388 
1389 cssu::Any SAL_CALL XSecController::getFastPropertyValue(
1390 	sal_Int32 nHandle )
1391 	throw (
1392 		cssb::UnknownPropertyException,
1393 		cssl::WrappedTargetException,
1394 		cssu::RuntimeException)
1395 {
1396 	cssu::Any aValue;
1397 
1398 	sal_Int32 nIndex = getFastPropertyIndex(nHandle);
1399 	if (nIndex != -1)
1400 	{
1401 		aValue = m_vFastPropertyValues[nIndex];
1402 	}
1403 
1404 	return aValue;
1405 }
1406 */
1407 
1408 /*
1409  * XSAXEventKeeperStatusChangeListener
1410  */
1411 
1412 void SAL_CALL XSecController::blockingStatusChanged( sal_Bool isBlocking )
1413 	throw (cssu::RuntimeException)
1414 {
1415 	/*
1416 	showMessageBox( rtl::OUString::createFromAscii((isBlocking?
1417 						"Blocking Status => TRUE":
1418 						"Blocking Status => FALSE")),
1419 			rtl::OUString::createFromAscii("SAXEventKeeper Status"));
1420 	*/
1421 
1422 	this->m_bIsBlocking = isBlocking;
1423 	checkChainingStatus();
1424 }
1425 
1426 void SAL_CALL XSecController::collectionStatusChanged(
1427 	sal_Bool isInsideCollectedElement )
1428 	throw (cssu::RuntimeException)
1429 {
1430 	/*
1431 	showMessageBox(	rtl::OUString::createFromAscii((isInsideCollectedElement?
1432 						"Collection Status => TRUE":
1433 						"Collection Status => FALSE")),
1434 			rtl::OUString::createFromAscii("SAXEventKeeper Status"));
1435 	*/
1436 
1437 	this->m_bIsCollectingElement = isInsideCollectedElement;
1438 	checkChainingStatus();
1439 }
1440 
1441 void SAL_CALL XSecController::bufferStatusChanged( sal_Bool /*isBufferEmpty*/)
1442 	throw (cssu::RuntimeException)
1443 {
1444 	/*
1445 	showMessageBox(	rtl::OUString::createFromAscii((isBufferEmpty?
1446 						"Buffer Empty => TRUE":
1447 						"Buffer Empty => FALSE")),
1448 			rtl::OUString::createFromAscii("SAXEventKeeper Status"));
1449 	*/
1450 }
1451 
1452 /*
1453  * XSignatureCreationResultListener
1454  */
1455 void SAL_CALL XSecController::signatureCreated( sal_Int32 securityId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
1456 		throw (com::sun::star::uno::RuntimeException)
1457 {
1458 	int index = findSignatureInfor(securityId);
1459 	DBG_ASSERT( index != -1, "Signature Not Found!" );
1460 
1461 	SignatureInformation& signatureInfor = m_vInternalSignatureInformations[index].signatureInfor;
1462 
1463 	/*
1464 	if (nResult == cssxc::sax::SignatureCreationResult_CREATIONSUCCEED)
1465 	{
1466 		signatureInfor.nStatus = STATUS_CREATION_SUCCEED;
1467 	}
1468 	else
1469 	{
1470 		signatureInfor.nStatus = STATUS_CREATION_FAIL;
1471 	}
1472 	*/
1473 	signatureInfor.nStatus = nResult;
1474 }
1475 
1476 /*
1477  * XSignatureVerifyResultListener
1478  */
1479 void SAL_CALL XSecController::signatureVerified( sal_Int32 securityId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
1480 		throw (com::sun::star::uno::RuntimeException)
1481 {
1482 	int index = findSignatureInfor(securityId);
1483 	DBG_ASSERT( index != -1, "Signature Not Found!" );
1484 
1485 	SignatureInformation& signatureInfor = m_vInternalSignatureInformations[index].signatureInfor;
1486 
1487 	/*
1488 	if (nResult == cssxc::sax::SignatureVerifyResult_VERIFYSUCCEED)
1489 	{
1490 		signatureInfor.nStatus = STATUS_VERIFY_SUCCEED;
1491 	}
1492 	else
1493 	{
1494 		signatureInfor.nStatus = STATUS_VERIFY_FAIL;
1495 	}
1496 	*/
1497 	signatureInfor.nStatus = nResult;
1498 }
1499