Bump version to 5.0-14
[LibreOffice.git] / xmlsecurity / source / helper / xsecctl.cxx
blobac75ea87747d7428a2cfa0a2599ba00d763dd4e1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "xsecctl.hxx"
22 #include <tools/debug.hxx>
24 #include <com/sun/star/xml/crypto/sax/ElementMarkPriority.hpp>
25 #include <com/sun/star/xml/crypto/sax/XReferenceResolvedBroadcaster.hpp>
26 #include <com/sun/star/xml/crypto/sax/XMissionTaker.hpp>
27 #include <com/sun/star/xml/crypto/sax/XReferenceCollector.hpp>
28 #include <com/sun/star/xml/crypto/sax/XSAXEventKeeperStatusChangeBroadcaster.hpp>
29 #include <com/sun/star/xml/crypto/SecurityOperationStatus.hpp>
31 #include <xmloff/attrlist.hxx>
32 #include <rtl/math.hxx>
33 #include <unotools/datetime.hxx>
35 namespace cssu = com::sun::star::uno;
36 namespace cssl = com::sun::star::lang;
37 namespace cssxc = com::sun::star::xml::crypto;
38 namespace cssxs = com::sun::star::xml::sax;
39 namespace cssxw = com::sun::star::xml::wrapper;
41 /* bridge component names */
42 #define XMLSIGNATURE_COMPONENT "com.sun.star.xml.crypto.XMLSignature"
43 #define XMLDOCUMENTWRAPPER_COMPONENT "com.sun.star.xml.wrapper.XMLDocumentWrapper"
45 /* xml security framework components */
46 #define SAXEVENTKEEPER_COMPONENT "com.sun.star.xml.crypto.sax.SAXEventKeeper"
48 XSecController::XSecController( const cssu::Reference<cssu::XComponentContext>& rxCtx )
49 : mxCtx(rxCtx)
50 , m_nNextSecurityId(1)
51 , m_bIsPreviousNodeInitializable(false)
52 , m_bIsSAXEventKeeperConnected(false)
53 , m_bIsCollectingElement(false)
54 , m_bIsBlocking(false)
55 , m_nStatusOfSecurityComponents(UNINITIALIZED)
56 , m_bIsSAXEventKeeperSticky(false)
57 , m_pErrorMessage(NULL)
58 , m_pXSecParser(NULL)
59 , m_nReservedSignatureId(0)
60 , m_bVerifyCurrentSignature(false)
64 XSecController::~XSecController()
70 * private methods
72 int XSecController::findSignatureInfor( sal_Int32 nSecurityId) const
73 /****** XSecController/findSignatureInfor *************************************
75 * NAME
76 * findSignatureInfor -- find SignatureInformation struct for a particular
77 * signature
79 * SYNOPSIS
80 * index = findSignatureInfor( nSecurityId );
82 * FUNCTION
83 * see NAME.
85 * INPUTS
86 * nSecurityId - the signature's id
88 * RESULT
89 * index - the index of the signature, or -1 when no such signature
90 * existing
92 * AUTHOR
93 * Michael Mi
94 * Email: michael.mi@sun.com
95 ******************************************************************************/
97 int i;
98 int size = m_vInternalSignatureInformations.size();
100 for (i=0; i<size; ++i)
102 if (m_vInternalSignatureInformations[i].signatureInfor.nSecurityId == nSecurityId)
104 return i;
108 return -1;
111 void XSecController::createXSecComponent( )
112 /****** XSecController/createXSecComponent ************************************
114 * NAME
115 * bResult = createXSecComponent -- creates xml security components
117 * SYNOPSIS
118 * createXSecComponent( );
120 * FUNCTION
121 * Creates xml security components, including:
122 * 1. an xml signature bridge component ( Java based or C based)
123 * 2. an XMLDocumentWrapper component ( Java based or C based)
124 * 3. a SAXEventKeeper component
126 * INPUTS
127 * empty
129 * RESULT
130 * empty
132 * AUTHOR
133 * Michael Mi
134 * Email: michael.mi@sun.com
135 ******************************************************************************/
137 OUString sSAXEventKeeper( SAXEVENTKEEPER_COMPONENT );
138 OUString sXMLSignature( XMLSIGNATURE_COMPONENT );
139 OUString sXMLDocument( XMLDOCUMENTWRAPPER_COMPONENT );
142 * marks all security components are not available.
144 m_nStatusOfSecurityComponents = FAILTOINITIALIZED;
145 m_xXMLSignature = NULL;
146 m_xXMLDocumentWrapper = NULL;
147 m_xSAXEventKeeper = NULL;
149 cssu::Reference< cssl::XMultiComponentFactory > xMCF( mxCtx->getServiceManager() );
151 m_xXMLSignature = cssu::Reference< cssxc::XXMLSignature >(
152 xMCF->createInstanceWithContext( sXMLSignature, mxCtx ),
153 cssu::UNO_QUERY );
155 bool bSuccess = m_xXMLSignature.is();
156 if ( bSuccess )
158 * XMLSignature created successfully.
161 m_xXMLDocumentWrapper = cssu::Reference< cssxw::XXMLDocumentWrapper >(
162 xMCF->createInstanceWithContext( sXMLDocument, mxCtx ),
163 cssu::UNO_QUERY );
166 bSuccess &= m_xXMLDocumentWrapper.is();
167 if ( bSuccess )
169 * XMLDocumentWrapper created successfully.
172 m_xSAXEventKeeper = cssu::Reference< cssxc::sax::XSecuritySAXEventKeeper >(
173 xMCF->createInstanceWithContext( sSAXEventKeeper, mxCtx ),
174 cssu::UNO_QUERY );
177 bSuccess &= m_xSAXEventKeeper.is();
179 if (bSuccess)
181 * SAXEventKeeper created successfully.
184 cssu::Reference< cssl::XInitialization > xInitialization(m_xSAXEventKeeper, cssu::UNO_QUERY);
186 cssu::Sequence <cssu::Any> arg(1);
187 arg[0] = cssu::makeAny(m_xXMLDocumentWrapper);
188 xInitialization->initialize(arg);
190 cssu::Reference<cssxc::sax::XSAXEventKeeperStatusChangeBroadcaster>
191 xSAXEventKeeperStatusChangeBroadcaster(m_xSAXEventKeeper, cssu::UNO_QUERY);
192 cssu::Reference< cssxc::sax::XSAXEventKeeperStatusChangeListener >
193 xStatusChangeListener = this;
195 xSAXEventKeeperStatusChangeBroadcaster
196 ->addSAXEventKeeperStatusChangeListener( xStatusChangeListener );
198 m_nStatusOfSecurityComponents = INITIALIZED;
202 bool XSecController::chainOn( bool bRetrievingLastEvent )
203 /****** XSecController/chainOn ************************************************
205 * NAME
206 * chainOn -- tyies to connect the SAXEventKeeper with the SAX chain.
208 * SYNOPSIS
209 * bJustChainingOn = chainOn( bRetrievingLastEvent );
211 * FUNCTION
212 * First, checks whether the SAXEventKeeper is on the SAX chain. If not,
213 * creates xml security components, and chains the SAXEventKeeper into
214 * the SAX chain.
215 * Before being chained in, the SAXEventKeeper needs to receive all
216 * missed key SAX events, which can promise the DOM tree bufferred by the
217 * SAXEventKeeper has the same structure with the original document.
219 * INPUTS
220 * bRetrievingLastEvent - whether to retrieve the last key SAX event from
221 * the ElementStackKeeper.
223 * RESULT
224 * bJustChainingOn - whether the SAXEventKeeper is just chained into the
225 * SAX chain.
227 * NOTES
228 * Sometimes, the last key SAX event can't be transferred to the
229 * SAXEventKeeper together.
230 * For instance, at the time an referenced element is detected, the
231 * startElement event has already been reserved by the ElementStackKeeper.
232 * Meanwhile, an ElementCollector needs to be created before the
233 * SAXEventKeeper receives that startElement event.
234 * So for the SAXEventKeeper, it needs to receive all missed key SAX
235 * events except that startElement event, then adds a new
236 * ElementCollector, then receives that startElement event.
238 * AUTHOR
239 * Michael Mi
240 * Email: michael.mi@sun.com
241 ******************************************************************************/
243 bool rc = false;
245 if (!m_bIsSAXEventKeeperSticky && !m_bIsSAXEventKeeperConnected)
247 if ( m_nStatusOfSecurityComponents == UNINITIALIZED )
249 createXSecComponent();
252 if ( m_nStatusOfSecurityComponents == INITIALIZED )
254 * if all security components are ready, chains on the SAXEventKeeper
258 * disconnect the SAXEventKeeper with its current output handler,
259 * to make sure no SAX event is forwarded during the connecting
260 * phase.
262 m_xSAXEventKeeper->setNextHandler( NULL );
264 cssu::Reference< cssxs::XDocumentHandler > xSEKHandler(m_xSAXEventKeeper, cssu::UNO_QUERY);
267 * connects the previous document handler on the SAX chain
269 if ( m_xPreviousNodeOnSAXChain.is() )
271 if ( m_bIsPreviousNodeInitializable )
273 cssu::Reference< cssl::XInitialization > xInitialization
274 (m_xPreviousNodeOnSAXChain, cssu::UNO_QUERY);
276 cssu::Sequence<cssu::Any> aArgs( 1 );
277 aArgs[0] <<= xSEKHandler;
278 xInitialization->initialize(aArgs);
280 else
282 cssu::Reference< cssxs::XParser > xParser
283 (m_xPreviousNodeOnSAXChain, cssu::UNO_QUERY);
284 xParser->setDocumentHandler( xSEKHandler );
289 * get missed key SAX events
291 if (m_xElementStackKeeper.is())
293 m_xElementStackKeeper->retrieve(xSEKHandler, bRetrievingLastEvent);
296 * now the ElementStackKeeper can stop its work, because the
297 * SAXEventKeeper is on the SAX chain, no SAX events will be
298 * missed.
300 m_xElementStackKeeper->stop();
304 * connects the next document handler on the SAX chain
306 m_xSAXEventKeeper->setNextHandler( m_xNextNodeOnSAXChain );
308 m_bIsSAXEventKeeperConnected = true;
310 rc = true;
314 return rc;
317 void XSecController::chainOff()
318 /****** XSecController/chainOff ***********************************************
320 * NAME
321 * chainOff -- disconnects the SAXEventKeeper from the SAX chain.
323 * SYNOPSIS
324 * chainOff( );
326 * FUNCTION
327 * See NAME.
329 * INPUTS
330 * empty
332 * RESULT
333 * empty
335 * AUTHOR
336 * Michael Mi
337 * Email: michael.mi@sun.com
338 ******************************************************************************/
340 if (!m_bIsSAXEventKeeperSticky )
342 if (m_bIsSAXEventKeeperConnected)
344 m_xSAXEventKeeper->setNextHandler( NULL );
346 if ( m_xPreviousNodeOnSAXChain.is() )
348 if ( m_bIsPreviousNodeInitializable )
350 cssu::Reference< cssl::XInitialization > xInitialization
351 (m_xPreviousNodeOnSAXChain, cssu::UNO_QUERY);
353 cssu::Sequence<cssu::Any> aArgs( 1 );
354 aArgs[0] <<= m_xNextNodeOnSAXChain;
355 xInitialization->initialize(aArgs);
357 else
359 cssu::Reference< cssxs::XParser > xParser(m_xPreviousNodeOnSAXChain, cssu::UNO_QUERY);
360 xParser->setDocumentHandler( m_xNextNodeOnSAXChain );
364 if (m_xElementStackKeeper.is())
367 * start the ElementStackKeeper to reserve any possible
368 * missed key SAX events
370 m_xElementStackKeeper->start();
373 m_bIsSAXEventKeeperConnected = false;
378 void XSecController::checkChainingStatus()
379 /****** XSecController/checkChainingStatus ************************************
381 * NAME
382 * checkChainingStatus -- connects or disconnects the SAXEventKeeper
383 * according to the current situation.
385 * SYNOPSIS
386 * checkChainingStatus( );
388 * FUNCTION
389 * The SAXEventKeeper is chained into the SAX chain, when:
390 * 1. some element is being collected, or
391 * 2. the SAX event stream is blocking.
392 * Otherwise, chain off the SAXEventKeeper.
394 * INPUTS
395 * empty
397 * RESULT
398 * empty
400 * AUTHOR
401 * Michael Mi
402 * Email: michael.mi@sun.com
403 ******************************************************************************/
405 if ( m_bIsCollectingElement || m_bIsBlocking )
407 chainOn(true);
409 else
411 chainOff();
415 void XSecController::initializeSAXChain()
416 /****** XSecController/initializeSAXChain *************************************
418 * NAME
419 * initializeSAXChain -- initializes the SAX chain according to the
420 * current setting.
422 * SYNOPSIS
423 * initializeSAXChain( );
425 * FUNCTION
426 * Initializes the SAX chain, if the SAXEventKeeper is asked to be always
427 * on the SAX chain, chains it on. Otherwise, starts the
428 * ElementStackKeeper to reserve key SAX events.
430 * INPUTS
431 * empty
433 * RESULT
434 * empty
436 * AUTHOR
437 * Michael Mi
438 * Email: michael.mi@sun.com
439 ******************************************************************************/
441 m_bIsSAXEventKeeperConnected = false;
442 m_bIsCollectingElement = false;
443 m_bIsBlocking = false;
445 if (m_xElementStackKeeper.is())
448 * starts the ElementStackKeeper
450 m_xElementStackKeeper->start();
453 chainOff();
456 cssu::Reference< com::sun::star::io::XInputStream >
457 XSecController::getObjectInputStream( const OUString& objectURL )
458 /****** XSecController/getObjectInputStream ************************************
460 * NAME
461 * getObjectInputStream -- get a XInputStream interface from a SotStorage
463 * SYNOPSIS
464 * xInputStream = getObjectInputStream( objectURL );
466 * FUNCTION
467 * See NAME.
469 * INPUTS
470 * objectURL - the object uri
472 * RESULT
473 * xInputStream - the XInputStream interface
475 * AUTHOR
476 * Michael Mi
477 * Email: michael.mi@sun.com
478 ******************************************************************************/
480 cssu::Reference< com::sun::star::io::XInputStream > xObjectInputStream;
482 DBG_ASSERT( m_xUriBinding.is(), "Need XUriBinding!" );
484 xObjectInputStream = m_xUriBinding->getUriBinding(objectURL);
486 return xObjectInputStream;
490 * public methods
493 sal_Int32 XSecController::getNewSecurityId( )
495 sal_Int32 nId = m_nNextSecurityId;
496 m_nNextSecurityId++;
497 return nId;
500 void XSecController::startMission(
501 const cssu::Reference< cssxc::XUriBinding >& xUriBinding,
502 const cssu::Reference< cssxc::XXMLSecurityContext >& xSecurityContext )
503 /****** XSecController/startMission *******************************************
505 * NAME
506 * startMission -- starts a new security mission.
508 * SYNOPSIS
509 * startMission( xUriBinding, xSecurityContect );
511 * FUNCTION
512 * get ready for a new mission.
514 * INPUTS
515 * xUriBinding - the Uri binding that provide maps between uris and
516 * XInputStreams
517 * xSecurityContext - the security context component which can provide
518 * cryptoken
520 * RESULT
521 * empty
523 * AUTHOR
524 * Michael Mi
525 * Email: michael.mi@sun.com
526 ******************************************************************************/
528 m_xUriBinding = xUriBinding;
530 m_nStatusOfSecurityComponents = UNINITIALIZED;
531 m_xSecurityContext = xSecurityContext;
532 m_pErrorMessage = NULL;
534 m_vInternalSignatureInformations.clear();
536 m_bVerifyCurrentSignature = false;
539 void XSecController::setSAXChainConnector(
540 const cssu::Reference< cssl::XInitialization >& xInitialization,
541 const cssu::Reference< cssxs::XDocumentHandler >& xDocumentHandler,
542 const cssu::Reference< cssxc::sax::XElementStackKeeper >& xElementStackKeeper)
543 /****** XSecController/setSAXChainConnector ***********************************
545 * NAME
546 * setSAXChainConnector -- configures the components which will
547 * collaborate with the SAXEventKeeper on the SAX chain.
549 * SYNOPSIS
550 * setSAXChainConnector( xInitialization,
551 * xDocumentHandler,
552 * xElementStackKeeper );
554 * FUNCTION
555 * See NAME.
557 * INPUTS
558 * xInitialization - the previous node on the SAX chain
559 * xDocumentHandler - the next node on the SAX chain
560 * xElementStackKeeper - the ElementStackKeeper component which reserves
561 * missed key SAX events for the SAXEventKeeper
563 * RESULT
564 * empty
566 * AUTHOR
567 * Michael Mi
568 * Email: michael.mi@sun.com
569 ******************************************************************************/
571 m_bIsPreviousNodeInitializable = true;
572 m_xPreviousNodeOnSAXChain = xInitialization;
573 m_xNextNodeOnSAXChain = xDocumentHandler;
574 m_xElementStackKeeper = xElementStackKeeper;
576 initializeSAXChain( );
579 void XSecController::clearSAXChainConnector()
580 /****** XSecController/clearSAXChainConnector *********************************
582 * NAME
583 * clearSAXChainConnector -- resets the collaborating components.
585 * SYNOPSIS
586 * clearSAXChainConnector( );
588 * FUNCTION
589 * See NAME.
591 * INPUTS
592 * empty
594 * RESULT
595 * empty
597 * AUTHOR
598 * Michael Mi
599 * Email: michael.mi@sun.com
600 ******************************************************************************/
603 * before reseting, if the ElementStackKeeper has kept something, then
604 * those kept key SAX events must be transferred to the SAXEventKeeper
605 * first. This is to promise the next node to the SAXEventKeeper on the
606 * SAX chain always receives a complete document.
608 if (m_xElementStackKeeper.is() && m_xSAXEventKeeper.is())
610 cssu::Reference< cssxs::XDocumentHandler > xSEKHandler(m_xSAXEventKeeper, cssu::UNO_QUERY);
611 m_xElementStackKeeper->retrieve(xSEKHandler, sal_True);
614 chainOff();
616 m_xPreviousNodeOnSAXChain = NULL;
617 m_xNextNodeOnSAXChain = NULL;
618 m_xElementStackKeeper = NULL;
621 void XSecController::endMission()
622 /****** XSecController/endMission *********************************************
624 * NAME
625 * endMission -- forces to end all missions
627 * SYNOPSIS
628 * endMission( );
630 * FUNCTION
631 * Deletes all signature information and forces all missions to an end.
633 * INPUTS
634 * empty
636 * RESULT
637 * empty
639 * AUTHOR
640 * Michael Mi
641 * Email: michael.mi@sun.com
642 ******************************************************************************/
644 sal_Int32 size = m_vInternalSignatureInformations.size();
646 for (int i=0; i<size; ++i)
648 if ( m_nStatusOfSecurityComponents == INITIALIZED )
650 * ResolvedListener only exist when the security components are created.
653 cssu::Reference< cssxc::sax::XMissionTaker > xMissionTaker
654 ( m_vInternalSignatureInformations[i].xReferenceResolvedListener, cssu::UNO_QUERY );
657 * askes the SignatureCreator/SignatureVerifier to release
658 * all resources it uses.
660 xMissionTaker->endMission();
664 m_xUriBinding = NULL;
665 m_xSecurityContext = NULL;
668 * free the status change listener reference to this object
670 if (m_xSAXEventKeeper.is())
672 cssu::Reference<cssxc::sax::XSAXEventKeeperStatusChangeBroadcaster>
673 xSAXEventKeeperStatusChangeBroadcaster(m_xSAXEventKeeper, cssu::UNO_QUERY);
674 xSAXEventKeeperStatusChangeBroadcaster
675 ->addSAXEventKeeperStatusChangeListener( NULL );
679 void XSecController::exportSignature(
680 const cssu::Reference<cssxs::XDocumentHandler>& xDocumentHandler,
681 const SignatureInformation& signatureInfo )
682 /****** XSecController/exportSignature ****************************************
684 * NAME
685 * exportSignature -- export a signature structure to an XDocumentHandler
687 * SYNOPSIS
688 * exportSignature( xDocumentHandler, signatureInfo);
690 * FUNCTION
691 * see NAME.
693 * INPUTS
694 * xDocumentHandler - the document handler to receive the signature
695 * signatureInfo - signature to be exported
697 * RESULT
698 * empty
700 * AUTHOR
701 * Michael Mi
702 * Email: michael.mi@sun.com
703 ******************************************************************************/
706 * defines all element tags in Signature element.
708 OUString tag_Signature(TAG_SIGNATURE);
709 OUString tag_SignedInfo(TAG_SIGNEDINFO);
710 OUString tag_CanonicalizationMethod(TAG_CANONICALIZATIONMETHOD);
711 OUString tag_SignatureMethod(TAG_SIGNATUREMETHOD);
712 OUString tag_Reference(TAG_REFERENCE);
713 OUString tag_Transforms(TAG_TRANSFORMS);
714 OUString tag_Transform(TAG_TRANSFORM);
715 OUString tag_DigestMethod(TAG_DIGESTMETHOD);
716 OUString tag_DigestValue(TAG_DIGESTVALUE);
717 OUString tag_SignatureValue(TAG_SIGNATUREVALUE);
718 OUString tag_KeyInfo(TAG_KEYINFO);
719 OUString tag_X509Data(TAG_X509DATA);
720 OUString tag_X509IssuerSerial(TAG_X509ISSUERSERIAL);
721 OUString tag_X509IssuerName(TAG_X509ISSUERNAME);
722 OUString tag_X509SerialNumber(TAG_X509SERIALNUMBER);
723 OUString tag_X509Certificate(TAG_X509CERTIFICATE);
724 OUString tag_Object(TAG_OBJECT);
725 OUString tag_SignatureProperties(TAG_SIGNATUREPROPERTIES);
726 OUString tag_SignatureProperty(TAG_SIGNATUREPROPERTY);
727 OUString tag_Date(TAG_DATE);
729 const SignatureReferenceInformations& vReferenceInfors = signatureInfo.vSignatureReferenceInfors;
730 SvXMLAttributeList *pAttributeList;
733 * Write Signature element
735 pAttributeList = new SvXMLAttributeList();
736 pAttributeList->AddAttribute(
737 OUString(ATTR_XMLNS),
738 OUString(NS_XMLDSIG));
740 if (!signatureInfo.ouSignatureId.isEmpty())
742 pAttributeList->AddAttribute(
743 OUString(ATTR_ID),
744 OUString(signatureInfo.ouSignatureId));
747 xDocumentHandler->startElement( tag_Signature, cssu::Reference< cssxs::XAttributeList > (pAttributeList));
749 /* Write SignedInfo element */
750 xDocumentHandler->startElement(
751 tag_SignedInfo,
752 cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
754 /* Write CanonicalizationMethod element */
755 pAttributeList = new SvXMLAttributeList();
756 pAttributeList->AddAttribute(
757 OUString(ATTR_ALGORITHM),
758 OUString(ALGO_C14N));
759 xDocumentHandler->startElement( tag_CanonicalizationMethod, cssu::Reference< cssxs::XAttributeList > (pAttributeList) );
760 xDocumentHandler->endElement( tag_CanonicalizationMethod );
762 /* Write SignatureMethod element */
763 pAttributeList = new SvXMLAttributeList();
764 pAttributeList->AddAttribute(
765 OUString(ATTR_ALGORITHM),
766 OUString(ALGO_RSASHA1));
767 xDocumentHandler->startElement( tag_SignatureMethod, cssu::Reference< cssxs::XAttributeList > (pAttributeList) );
768 xDocumentHandler->endElement( tag_SignatureMethod );
770 /* Write Reference element */
771 int j;
772 int refNum = vReferenceInfors.size();
774 for(j=0; j<refNum; ++j)
776 const SignatureReferenceInformation& refInfor = vReferenceInfors[j];
778 pAttributeList = new SvXMLAttributeList();
779 if ( refInfor.nType != TYPE_SAMEDOCUMENT_REFERENCE )
781 * stream reference
784 pAttributeList->AddAttribute(
785 OUString(ATTR_URI),
786 refInfor.ouURI);
788 else
790 * same-document reference
793 pAttributeList->AddAttribute(
794 OUString(ATTR_URI),
795 CHAR_FRAGMENT+refInfor.ouURI);
798 xDocumentHandler->startElement( tag_Reference, cssu::Reference< cssxs::XAttributeList > (pAttributeList) );
800 /* Write Transforms element */
801 if (refInfor.nType == TYPE_XMLSTREAM_REFERENCE)
803 * xml stream, so c14n transform is needed
806 xDocumentHandler->startElement(
807 tag_Transforms,
808 cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
810 pAttributeList = new SvXMLAttributeList();
811 pAttributeList->AddAttribute(
812 OUString(ATTR_ALGORITHM),
813 OUString(ALGO_C14N));
814 xDocumentHandler->startElement(
815 tag_Transform,
816 cssu::Reference< cssxs::XAttributeList > (pAttributeList) );
817 xDocumentHandler->endElement( tag_Transform );
819 xDocumentHandler->endElement( tag_Transforms );
822 /* Write DigestMethod element */
823 pAttributeList = new SvXMLAttributeList();
824 pAttributeList->AddAttribute(
825 OUString(ATTR_ALGORITHM),
826 OUString(ALGO_XMLDSIGSHA1));
827 xDocumentHandler->startElement(
828 tag_DigestMethod,
829 cssu::Reference< cssxs::XAttributeList > (pAttributeList) );
830 xDocumentHandler->endElement( tag_DigestMethod );
832 /* Write DigestValue element */
833 xDocumentHandler->startElement(
834 tag_DigestValue,
835 cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
836 xDocumentHandler->characters( refInfor.ouDigestValue );
837 xDocumentHandler->endElement( tag_DigestValue );
839 xDocumentHandler->endElement( tag_Reference );
842 xDocumentHandler->endElement( tag_SignedInfo );
844 /* Write SignatureValue element */
845 xDocumentHandler->startElement(
846 tag_SignatureValue,
847 cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
848 xDocumentHandler->characters( signatureInfo.ouSignatureValue );
849 xDocumentHandler->endElement( tag_SignatureValue );
851 /* Write KeyInfo element */
852 xDocumentHandler->startElement(
853 tag_KeyInfo,
854 cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
856 /* Write X509Data element */
857 xDocumentHandler->startElement(
858 tag_X509Data,
859 cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
861 /* Write X509IssuerSerial element */
862 xDocumentHandler->startElement(
863 tag_X509IssuerSerial,
864 cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
866 /* Write X509IssuerName element */
867 xDocumentHandler->startElement(
868 tag_X509IssuerName,
869 cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
870 xDocumentHandler->characters( signatureInfo.ouX509IssuerName );
871 xDocumentHandler->endElement( tag_X509IssuerName );
873 /* Write X509SerialNumber element */
874 xDocumentHandler->startElement(
875 tag_X509SerialNumber,
876 cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
877 xDocumentHandler->characters( signatureInfo.ouX509SerialNumber );
878 xDocumentHandler->endElement( tag_X509SerialNumber );
880 xDocumentHandler->endElement( tag_X509IssuerSerial );
882 /* Write X509Certificate element */
883 if (!signatureInfo.ouX509Certificate.isEmpty())
885 xDocumentHandler->startElement(
886 tag_X509Certificate,
887 cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
888 xDocumentHandler->characters( signatureInfo.ouX509Certificate );
889 xDocumentHandler->endElement( tag_X509Certificate );
892 xDocumentHandler->endElement( tag_X509Data );
894 xDocumentHandler->endElement( tag_KeyInfo );
896 /* Write Object element */
897 xDocumentHandler->startElement(
898 tag_Object,
899 cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
901 /* Write SignatureProperties element */
902 xDocumentHandler->startElement(
903 tag_SignatureProperties,
904 cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
906 /* Write SignatureProperty element */
907 pAttributeList = new SvXMLAttributeList();
908 pAttributeList->AddAttribute(
909 OUString(ATTR_ID),
910 signatureInfo.ouPropertyId);
911 pAttributeList->AddAttribute(
912 OUString(ATTR_TARGET),
913 CHAR_FRAGMENT+signatureInfo.ouSignatureId);
914 xDocumentHandler->startElement(
915 tag_SignatureProperty,
916 cssu::Reference< cssxs::XAttributeList > (pAttributeList));
918 /* Write timestamp element */
920 pAttributeList = new SvXMLAttributeList();
921 pAttributeList->AddAttribute(
922 ATTR_XMLNS ":" NSTAG_DC,
923 OUString(NS_DC));
925 xDocumentHandler->startElement(
926 NSTAG_DC ":" + tag_Date,
927 cssu::Reference< cssxs::XAttributeList > (pAttributeList));
929 OUStringBuffer buffer;
930 //If the xml signature was already contained in the document,
931 //then we use the original date and time string, rather then the
932 //converted one. This avoids writing a different string due to
933 //e.g. rounding issues and thus breaking the signature.
934 if (!signatureInfo.ouDateTime.isEmpty())
935 buffer = signatureInfo.ouDateTime;
936 else
938 buffer = utl::toISO8601(signatureInfo.stDateTime);
940 xDocumentHandler->characters( buffer.makeStringAndClear() );
942 xDocumentHandler->endElement(
943 NSTAG_DC ":" + tag_Date);
945 xDocumentHandler->endElement( tag_SignatureProperty );
947 xDocumentHandler->endElement( tag_SignatureProperties );
949 xDocumentHandler->endElement( tag_Object );
951 xDocumentHandler->endElement( tag_Signature );
954 SignatureInformation XSecController::getSignatureInformation( sal_Int32 nSecurityId ) const
956 SignatureInformation aInf( 0 );
957 int nIndex = findSignatureInfor(nSecurityId);
958 DBG_ASSERT( nIndex != -1, "getSignatureInformation - SecurityId is invalid!" );
959 if ( nIndex != -1)
961 aInf = m_vInternalSignatureInformations[nIndex].signatureInfor;
963 return aInf;
966 SignatureInformations XSecController::getSignatureInformations() const
968 SignatureInformations vInfors;
969 int sigNum = m_vInternalSignatureInformations.size();
971 for (int i=0; i<sigNum; ++i)
973 SignatureInformation si = m_vInternalSignatureInformations[i].signatureInfor;
974 vInfors.push_back(si);
977 return vInfors;
981 * XSecurityController
983 * no methods
987 * XFastPropertySet
991 * XSAXEventKeeperStatusChangeListener
994 void SAL_CALL XSecController::blockingStatusChanged( sal_Bool isBlocking )
995 throw (cssu::RuntimeException, std::exception)
997 this->m_bIsBlocking = isBlocking;
998 checkChainingStatus();
1001 void SAL_CALL XSecController::collectionStatusChanged(
1002 sal_Bool isInsideCollectedElement )
1003 throw (cssu::RuntimeException, std::exception)
1005 this->m_bIsCollectingElement = isInsideCollectedElement;
1006 checkChainingStatus();
1009 void SAL_CALL XSecController::bufferStatusChanged( sal_Bool /*isBufferEmpty*/)
1010 throw (cssu::RuntimeException, std::exception)
1016 * XSignatureCreationResultListener
1018 void SAL_CALL XSecController::signatureCreated( sal_Int32 securityId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
1019 throw (com::sun::star::uno::RuntimeException, std::exception)
1021 int index = findSignatureInfor(securityId);
1022 assert(index != -1 && "Signature Not Found!");
1023 SignatureInformation& signatureInfor = m_vInternalSignatureInformations.at(index).signatureInfor;
1024 signatureInfor.nStatus = nResult;
1028 * XSignatureVerifyResultListener
1030 void SAL_CALL XSecController::signatureVerified( sal_Int32 securityId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
1031 throw (com::sun::star::uno::RuntimeException, std::exception)
1033 int index = findSignatureInfor(securityId);
1034 assert(index != -1 && "Signature Not Found!");
1035 SignatureInformation& signatureInfor = m_vInternalSignatureInformations.at(index).signatureInfor;
1036 signatureInfor.nStatus = nResult;
1039 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */