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