LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/xmlsecurity/source/helper - xsecctl.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 296 0.0 %
Date: 2013-07-09 Functions: 0 23 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : 
      21             : #include <xsecctl.hxx>
      22             : #include <tools/debug.hxx>
      23             : 
      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>
      30             : 
      31             : #include <xmloff/attrlist.hxx>
      32             : #include <rtl/math.hxx>
      33             : #include <unotools/datetime.hxx>
      34             : 
      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;
      40             : 
      41             : const sal_Int8 XML_MAXDIGITSCOUNT_TIME = 11;
      42             : const sal_Int8 XML_MAXDIGITSCOUNT_DATETIME = 6;
      43             : 
      44             : /* bridge component names */
      45             : #define XMLSIGNATURE_COMPONENT "com.sun.star.xml.crypto.XMLSignature"
      46             : #define XMLDOCUMENTWRAPPER_COMPONENT "com.sun.star.xml.wrapper.XMLDocumentWrapper"
      47             : 
      48             : /* xml security framework components */
      49             : #define SAXEVENTKEEPER_COMPONENT "com.sun.star.xml.crypto.sax.SAXEventKeeper"
      50             : 
      51           0 : XSecController::XSecController( const cssu::Reference<cssu::XComponentContext>& rxCtx )
      52             :     :mxCtx(rxCtx),
      53             :      m_nNextSecurityId(1),
      54             :       m_bIsSAXEventKeeperConnected(false),
      55             :      m_nStatusOfSecurityComponents(UNINITIALIZED),
      56             :       m_bIsSAXEventKeeperSticky(false),
      57             :      m_pErrorMessage(NULL),
      58           0 :      m_pXSecParser(NULL)
      59             : {
      60           0 : }
      61             : 
      62           0 : XSecController::~XSecController()
      63             : {
      64           0 : }
      65             : 
      66             : 
      67             : /*
      68             :  * private methods
      69             :  */
      70           0 : int XSecController::findSignatureInfor( sal_Int32 nSecurityId) const
      71             : /****** XSecController/findSignatureInfor *************************************
      72             :  *
      73             :  *   NAME
      74             :  *  findSignatureInfor -- find SignatureInformation struct for a particular
      75             :  *                        signature
      76             :  *
      77             :  *   SYNOPSIS
      78             :  *  index = findSignatureInfor( nSecurityId );
      79             :  *
      80             :  *   FUNCTION
      81             :  *  see NAME.
      82             :  *
      83             :  *   INPUTS
      84             :  *  nSecurityId - the signature's id
      85             :  *
      86             :  *   RESULT
      87             :  *  index - the index of the signature, or -1 when no such signature
      88             :  *          existing
      89             :  *
      90             :  *   AUTHOR
      91             :  *  Michael Mi
      92             :  *  Email: michael.mi@sun.com
      93             :  ******************************************************************************/
      94             : {
      95             :     int i;
      96           0 :     int size = m_vInternalSignatureInformations.size();
      97             : 
      98           0 :     for (i=0; i<size; ++i)
      99             :     {
     100           0 :         if (m_vInternalSignatureInformations[i].signatureInfor.nSecurityId == nSecurityId)
     101             :         {
     102           0 :             return i;
     103             :         }
     104             :     }
     105             : 
     106           0 :     return -1;
     107             : }
     108             : 
     109           0 : void XSecController::createXSecComponent( )
     110             : /****** XSecController/createXSecComponent ************************************
     111             :  *
     112             :  *   NAME
     113             :  *  bResult = createXSecComponent -- creates xml security components
     114             :  *
     115             :  *   SYNOPSIS
     116             :  *  createXSecComponent( );
     117             :  *
     118             :  *   FUNCTION
     119             :  *  Creates xml security components, including:
     120             :  *  1. an xml signature bridge component ( Java based or C based)
     121             :  *  2. an XMLDocumentWrapper component ( Java based or C based)
     122             :  *  3. a SAXEventKeeper component
     123             :  *
     124             :  *   INPUTS
     125             :  *  empty
     126             :  *
     127             :  *   RESULT
     128             :  *  empty
     129             :  *
     130             :  *   AUTHOR
     131             :  *  Michael Mi
     132             :  *  Email: michael.mi@sun.com
     133             :  ******************************************************************************/
     134             : {
     135           0 :     OUString sSAXEventKeeper( SAXEVENTKEEPER_COMPONENT );
     136           0 :     OUString sXMLSignature( XMLSIGNATURE_COMPONENT );
     137           0 :     OUString sXMLDocument( XMLDOCUMENTWRAPPER_COMPONENT );
     138             : 
     139             :     /*
     140             :      * marks all security components are not available.
     141             :      */
     142           0 :     m_nStatusOfSecurityComponents = FAILTOINITIALIZED;
     143           0 :     m_xXMLSignature = NULL;
     144           0 :     m_xXMLDocumentWrapper = NULL;
     145           0 :     m_xSAXEventKeeper = NULL;
     146             : 
     147           0 :     cssu::Reference< cssl::XMultiComponentFactory > xMCF( mxCtx->getServiceManager() );
     148             : 
     149           0 :     m_xXMLSignature = cssu::Reference< cssxc::XXMLSignature >(
     150           0 :         xMCF->createInstanceWithContext( sXMLSignature, mxCtx ),
     151           0 :         cssu::UNO_QUERY );
     152             : 
     153           0 :     bool bSuccess = (0!=m_xXMLSignature.is());
     154           0 :     if ( bSuccess )
     155             :     /*
     156             :      * XMLSignature created successfully.
     157             :      */
     158             :     {
     159           0 :         m_xXMLDocumentWrapper = cssu::Reference< cssxw::XXMLDocumentWrapper >(
     160           0 :             xMCF->createInstanceWithContext( sXMLDocument, mxCtx ),
     161           0 :             cssu::UNO_QUERY );
     162             :     }
     163             : 
     164           0 :     bSuccess &= (0!=m_xXMLDocumentWrapper.is());
     165           0 :     if ( bSuccess )
     166             :     /*
     167             :      * XMLDocumentWrapper created successfully.
     168             :      */
     169             :     {
     170           0 :         m_xSAXEventKeeper = cssu::Reference< cssxc::sax::XSecuritySAXEventKeeper >(
     171           0 :             xMCF->createInstanceWithContext( sSAXEventKeeper, mxCtx ),
     172           0 :             cssu::UNO_QUERY );
     173             :     }
     174             : 
     175           0 :     bSuccess &= (0!=m_xSAXEventKeeper.is());
     176             : 
     177           0 :     if (bSuccess)
     178             :     /*
     179             :      * SAXEventKeeper created successfully.
     180             :      */
     181             :     {
     182           0 :         cssu::Reference< cssl::XInitialization > xInitialization(m_xSAXEventKeeper,  cssu::UNO_QUERY);
     183             : 
     184           0 :         cssu::Sequence <cssu::Any> arg(1);
     185           0 :         arg[0] = cssu::makeAny(m_xXMLDocumentWrapper);
     186           0 :         xInitialization->initialize(arg);
     187             : 
     188             :         cssu::Reference<cssxc::sax::XSAXEventKeeperStatusChangeBroadcaster>
     189           0 :             xSAXEventKeeperStatusChangeBroadcaster(m_xSAXEventKeeper, cssu::UNO_QUERY);
     190             :         cssu::Reference< cssxc::sax::XSAXEventKeeperStatusChangeListener >
     191           0 :             xStatusChangeListener = this;
     192             : 
     193             :         xSAXEventKeeperStatusChangeBroadcaster
     194           0 :             ->addSAXEventKeeperStatusChangeListener( xStatusChangeListener );
     195             : 
     196           0 :         m_nStatusOfSecurityComponents = INITIALIZED;
     197           0 :     }
     198           0 : }
     199             : 
     200           0 : bool XSecController::chainOn( bool bRetrievingLastEvent )
     201             : /****** XSecController/chainOn ************************************************
     202             :  *
     203             :  *   NAME
     204             :  *  chainOn -- tyies to connect the SAXEventKeeper with the SAX chain.
     205             :  *
     206             :  *   SYNOPSIS
     207             :  *  bJustChainingOn = chainOn( bRetrievingLastEvent );
     208             :  *
     209             :  *   FUNCTION
     210             :  *  First, checks whether the SAXEventKeeper is on the SAX chain. If not,
     211             :  *  creates xml security components, and chains the SAXEventKeeper into
     212             :  *  the SAX chain.
     213             :  *  Before being chained in, the SAXEventKeeper needs to receive all
     214             :  *  missed key SAX events, which can promise the DOM tree bufferred by the
     215             :  *  SAXEventKeeper has the same structure with the original document.
     216             :  *
     217             :  *   INPUTS
     218             :  *  bRetrievingLastEvent - whether to retrieve the last key SAX event from
     219             :  *                         the ElementStackKeeper.
     220             :  *
     221             :  *   RESULT
     222             :  *  bJustChainingOn - whether the SAXEventKeeper is just chained into the
     223             :  *                    SAX chain.
     224             :  *
     225             :  *   NOTES
     226             :  *  Sometimes, the last key SAX event can't be transferred to the
     227             :  *  SAXEventKeeper together.
     228             :  *  For instance, at the time an referenced element is detected, the
     229             :  *  startElement event has already been reserved by the ElementStackKeeper.
     230             :  *  Meanwhile, an ElementCollector needs to be created before the
     231             :  *  SAXEventKeeper receives that startElement event.
     232             :  *  So for the SAXEventKeeper, it needs to receive all missed key SAX
     233             :  *  events except that startElement event, then adds a new
     234             :  *  ElementCollector, then receives that startElement event.
     235             :  *
     236             :  *   AUTHOR
     237             :  *  Michael Mi
     238             :  *  Email: michael.mi@sun.com
     239             :  ******************************************************************************/
     240             : {
     241           0 :     bool rc = false;
     242             : 
     243           0 :     if (!m_bIsSAXEventKeeperSticky && !m_bIsSAXEventKeeperConnected)
     244             :     {
     245           0 :         if ( m_nStatusOfSecurityComponents == UNINITIALIZED )
     246             :         {
     247           0 :             createXSecComponent();
     248             :         }
     249             : 
     250           0 :         if ( m_nStatusOfSecurityComponents == INITIALIZED )
     251             :         /*
     252             :          * if all security components are ready, chains on the SAXEventKeeper
     253             :          */
     254             :         {
     255             :             /*
     256             :              * disconnect the SAXEventKeeper with its current output handler,
     257             :              * to make sure no SAX event is forwarded during the connecting
     258             :              * phase.
     259             :              */
     260           0 :             m_xSAXEventKeeper->setNextHandler( NULL );
     261             : 
     262           0 :             cssu::Reference< cssxs::XDocumentHandler > xSEKHandler(m_xSAXEventKeeper, cssu::UNO_QUERY);
     263             : 
     264             :             /*
     265             :              * connects the previous document handler on the SAX chain
     266             :              */
     267           0 :             if ( m_xPreviousNodeOnSAXChain.is() )
     268             :             {
     269           0 :                 if ( m_bIsPreviousNodeInitializable )
     270             :                 {
     271             :                     cssu::Reference< cssl::XInitialization > xInitialization
     272           0 :                         (m_xPreviousNodeOnSAXChain, cssu::UNO_QUERY);
     273             : 
     274           0 :                     cssu::Sequence<cssu::Any> aArgs( 1 );
     275           0 :                     aArgs[0] <<= xSEKHandler;
     276           0 :                     xInitialization->initialize(aArgs);
     277             :                 }
     278             :                 else
     279             :                 {
     280             :                     cssu::Reference< cssxs::XParser > xParser
     281           0 :                         (m_xPreviousNodeOnSAXChain, cssu::UNO_QUERY);
     282           0 :                     xParser->setDocumentHandler( xSEKHandler );
     283             :                 }
     284             :             }
     285             : 
     286             :             /*
     287             :              * get missed key SAX events
     288             :              */
     289           0 :             if (m_xElementStackKeeper.is())
     290             :             {
     291           0 :                 m_xElementStackKeeper->retrieve(xSEKHandler, bRetrievingLastEvent);
     292             : 
     293             :                 /*
     294             :                  * now the ElementStackKeeper can stop its work, because the
     295             :                  * SAXEventKeeper is on the SAX chain, no SAX events will be
     296             :                  * missed.
     297             :                  */
     298           0 :                 m_xElementStackKeeper->stop();
     299             :             }
     300             : 
     301             :             /*
     302             :              * connects the next document handler on the SAX chain
     303             :              */
     304           0 :             m_xSAXEventKeeper->setNextHandler( m_xNextNodeOnSAXChain );
     305             : 
     306           0 :             m_bIsSAXEventKeeperConnected = true;
     307             : 
     308           0 :             rc = true;
     309             :         }
     310             :     }
     311             : 
     312           0 :     return rc;
     313             : }
     314             : 
     315           0 : void XSecController::chainOff()
     316             : /****** XSecController/chainOff ***********************************************
     317             :  *
     318             :  *   NAME
     319             :  *  chainOff -- disconnects the SAXEventKeeper from the SAX chain.
     320             :  *
     321             :  *   SYNOPSIS
     322             :  *  chainOff( );
     323             :  *
     324             :  *   FUNCTION
     325             :  *  See NAME.
     326             :  *
     327             :  *   INPUTS
     328             :  *  empty
     329             :  *
     330             :  *   RESULT
     331             :  *  empty
     332             :  *
     333             :  *   AUTHOR
     334             :  *  Michael Mi
     335             :  *  Email: michael.mi@sun.com
     336             :  ******************************************************************************/
     337             : {
     338           0 :     if (!m_bIsSAXEventKeeperSticky )
     339             :     {
     340           0 :         if (m_bIsSAXEventKeeperConnected)
     341             :         {
     342           0 :             m_xSAXEventKeeper->setNextHandler( NULL );
     343             : 
     344           0 :             if ( m_xPreviousNodeOnSAXChain.is() )
     345             :             {
     346           0 :                 if ( m_bIsPreviousNodeInitializable )
     347             :                 {
     348             :                     cssu::Reference< cssl::XInitialization > xInitialization
     349           0 :                         (m_xPreviousNodeOnSAXChain, cssu::UNO_QUERY);
     350             : 
     351           0 :                     cssu::Sequence<cssu::Any> aArgs( 1 );
     352           0 :                     aArgs[0] <<= m_xNextNodeOnSAXChain;
     353           0 :                     xInitialization->initialize(aArgs);
     354             :                 }
     355             :                 else
     356             :                 {
     357           0 :                     cssu::Reference< cssxs::XParser > xParser(m_xPreviousNodeOnSAXChain, cssu::UNO_QUERY);
     358           0 :                     xParser->setDocumentHandler( m_xNextNodeOnSAXChain );
     359             :                 }
     360             :             }
     361             : 
     362           0 :             if (m_xElementStackKeeper.is())
     363             :             {
     364             :                 /*
     365             :                  * start the ElementStackKeeper to reserve any possible
     366             :                  * missed key SAX events
     367             :                  */
     368           0 :                 m_xElementStackKeeper->start();
     369             :             }
     370             : 
     371           0 :             m_bIsSAXEventKeeperConnected = false;
     372             :         }
     373             :     }
     374           0 : }
     375             : 
     376           0 : void XSecController::checkChainingStatus()
     377             : /****** XSecController/checkChainingStatus ************************************
     378             :  *
     379             :  *   NAME
     380             :  *  checkChainingStatus -- connects or disconnects the SAXEventKeeper
     381             :  *  according to the current situation.
     382             :  *
     383             :  *   SYNOPSIS
     384             :  *  checkChainingStatus( );
     385             :  *
     386             :  *   FUNCTION
     387             :  *  The SAXEventKeeper is chained into the SAX chain, when:
     388             :  *  1. some element is being collected, or
     389             :  *  2. the SAX event stream is blocking.
     390             :  *  Otherwise, chain off the SAXEventKeeper.
     391             :  *
     392             :  *   INPUTS
     393             :  *  empty
     394             :  *
     395             :  *   RESULT
     396             :  *  empty
     397             :  *
     398             :  *   AUTHOR
     399             :  *  Michael Mi
     400             :  *  Email: michael.mi@sun.com
     401             :  ******************************************************************************/
     402             : {
     403           0 :     if ( m_bIsCollectingElement || m_bIsBlocking )
     404             :     {
     405           0 :         chainOn(true);
     406             :     }
     407             :     else
     408             :     {
     409           0 :         chainOff();
     410             :     }
     411           0 : }
     412             : 
     413           0 : void XSecController::initializeSAXChain()
     414             : /****** XSecController/initializeSAXChain *************************************
     415             :  *
     416             :  *   NAME
     417             :  *  initializeSAXChain -- initializes the SAX chain according to the
     418             :  *  current setting.
     419             :  *
     420             :  *   SYNOPSIS
     421             :  *  initializeSAXChain( );
     422             :  *
     423             :  *   FUNCTION
     424             :  *  Initializes the SAX chain, if the SAXEventKeeper is asked to be always
     425             :  *  on the SAX chain, chains it on. Otherwise, starts the
     426             :  *  ElementStackKeeper to reserve key SAX events.
     427             :  *
     428             :  *   INPUTS
     429             :  *  empty
     430             :  *
     431             :  *   RESULT
     432             :  *  empty
     433             :  *
     434             :  *   AUTHOR
     435             :  *  Michael Mi
     436             :  *  Email: michael.mi@sun.com
     437             :  ******************************************************************************/
     438             : {
     439           0 :     m_bIsSAXEventKeeperConnected = false;
     440           0 :     m_bIsCollectingElement = false;
     441           0 :     m_bIsBlocking = false;
     442             : 
     443           0 :     if (m_xElementStackKeeper.is())
     444             :     {
     445             :         /*
     446             :          * starts the ElementStackKeeper
     447             :          */
     448           0 :         m_xElementStackKeeper->start();
     449             :     }
     450             : 
     451           0 :     chainOff();
     452           0 : }
     453             : 
     454             : cssu::Reference< com::sun::star::io::XInputStream >
     455           0 :     XSecController::getObjectInputStream( const OUString& objectURL )
     456             : /****** XSecController/getObjectInputStream ************************************
     457             :  *
     458             :  *   NAME
     459             :  *  getObjectInputStream -- get a XInputStream interface from a SvStorage
     460             :  *
     461             :  *   SYNOPSIS
     462             :  *  xInputStream = getObjectInputStream( objectURL );
     463             :  *
     464             :  *   FUNCTION
     465             :  *  See NAME.
     466             :  *
     467             :  *   INPUTS
     468             :  *  objectURL - the object uri
     469             :  *
     470             :  *   RESULT
     471             :  *  xInputStream - the XInputStream interface
     472             :  *
     473             :  *   AUTHOR
     474             :  *  Michael Mi
     475             :  *  Email: michael.mi@sun.com
     476             :  ******************************************************************************/
     477             : {
     478           0 :         cssu::Reference< com::sun::star::io::XInputStream > xObjectInputStream;
     479             : 
     480             :     DBG_ASSERT( m_xUriBinding.is(), "Need XUriBinding!" );
     481             : 
     482           0 :     xObjectInputStream = m_xUriBinding->getUriBinding(objectURL);
     483             : 
     484           0 :     return xObjectInputStream;
     485             : }
     486             : 
     487             : /*
     488             :  * public methods
     489             :  */
     490             : 
     491           0 : sal_Int32 XSecController::getNewSecurityId(  )
     492             : {
     493           0 :     sal_Int32 nId = m_nNextSecurityId;
     494           0 :     m_nNextSecurityId++;
     495           0 :     return nId;
     496             : }
     497             : 
     498           0 : void XSecController::startMission(
     499             :     const cssu::Reference< cssxc::XUriBinding >& xUriBinding,
     500             :     const cssu::Reference< cssxc::XXMLSecurityContext >& xSecurityContext )
     501             : /****** XSecController/startMission *******************************************
     502             :  *
     503             :  *   NAME
     504             :  *  startMission -- starts a new security mission.
     505             :  *
     506             :  *   SYNOPSIS
     507             :  *  startMission( xUriBinding, xSecurityContect );
     508             :  *
     509             :  *   FUNCTION
     510             :  *  get ready for a new mission.
     511             :  *
     512             :  *   INPUTS
     513             :  *  xUriBinding       - the Uri binding that provide maps between uris and
     514             :  *                          XInputStreams
     515             :  *  xSecurityContext  - the security context component which can provide
     516             :  *                      cryptoken
     517             :  *
     518             :  *   RESULT
     519             :  *  empty
     520             :  *
     521             :  *   AUTHOR
     522             :  *  Michael Mi
     523             :  *  Email: michael.mi@sun.com
     524             :  ******************************************************************************/
     525             : {
     526           0 :     m_xUriBinding = xUriBinding;
     527             : 
     528           0 :     m_nStatusOfSecurityComponents = UNINITIALIZED;
     529           0 :     m_xSecurityContext = xSecurityContext;
     530           0 :     m_pErrorMessage = NULL;
     531             : 
     532           0 :     m_vInternalSignatureInformations.clear();
     533             : 
     534           0 :     m_bVerifyCurrentSignature = false;
     535           0 : }
     536             : 
     537           0 : void XSecController::setSAXChainConnector(
     538             :     const cssu::Reference< cssl::XInitialization >& xInitialization,
     539             :     const cssu::Reference< cssxs::XDocumentHandler >& xDocumentHandler,
     540             :     const cssu::Reference< cssxc::sax::XElementStackKeeper >& xElementStackKeeper)
     541             : /****** XSecController/setSAXChainConnector ***********************************
     542             :  *
     543             :  *   NAME
     544             :  *  setSAXChainConnector -- configures the components which will
     545             :  *  collaborate with the SAXEventKeeper on the SAX chain.
     546             :  *
     547             :  *   SYNOPSIS
     548             :  *  setSAXChainConnector( xInitialization,
     549             :  *                        xDocumentHandler,
     550             :  *                        xElementStackKeeper );
     551             :  *
     552             :  *   FUNCTION
     553             :  *  See NAME.
     554             :  *
     555             :  *   INPUTS
     556             :  *  xInitialization     - the previous node on the SAX chain
     557             :  *  xDocumentHandler    - the next node on the SAX chain
     558             :  *  xElementStackKeeper - the ElementStackKeeper component which reserves
     559             :  *                        missed key SAX events for the SAXEventKeeper
     560             :  *
     561             :  *   RESULT
     562             :  *  empty
     563             :  *
     564             :  *   AUTHOR
     565             :  *  Michael Mi
     566             :  *  Email: michael.mi@sun.com
     567             :  ******************************************************************************/
     568             : {
     569           0 :     m_bIsPreviousNodeInitializable = true;
     570           0 :     m_xPreviousNodeOnSAXChain = xInitialization;
     571           0 :     m_xNextNodeOnSAXChain = xDocumentHandler;
     572           0 :     m_xElementStackKeeper = xElementStackKeeper;
     573             : 
     574           0 :     initializeSAXChain( );
     575           0 : }
     576             : 
     577           0 : void XSecController::clearSAXChainConnector()
     578             : /****** XSecController/clearSAXChainConnector *********************************
     579             :  *
     580             :  *   NAME
     581             :  *  clearSAXChainConnector -- resets the collaborating components.
     582             :  *
     583             :  *   SYNOPSIS
     584             :  *  clearSAXChainConnector( );
     585             :  *
     586             :  *   FUNCTION
     587             :  *  See NAME.
     588             :  *
     589             :  *   INPUTS
     590             :  *  empty
     591             :  *
     592             :  *   RESULT
     593             :  *  empty
     594             :  *
     595             :  *   AUTHOR
     596             :  *  Michael Mi
     597             :  *  Email: michael.mi@sun.com
     598             :  ******************************************************************************/
     599             : {
     600             :     /*
     601             :      * before reseting, if the ElementStackKeeper has kept something, then
     602             :      * those kept key SAX events must be transferred to the SAXEventKeeper
     603             :      * first. This is to promise the next node to the SAXEventKeeper on the
     604             :      * SAX chain always receives a complete document.
     605             :      */
     606           0 :     if (m_xElementStackKeeper.is() && m_xSAXEventKeeper.is())
     607             :     {
     608           0 :         cssu::Reference< cssxs::XDocumentHandler > xSEKHandler(m_xSAXEventKeeper, cssu::UNO_QUERY);
     609           0 :         m_xElementStackKeeper->retrieve(xSEKHandler, sal_True);
     610             :     }
     611             : 
     612           0 :     chainOff();
     613             : 
     614           0 :     m_xPreviousNodeOnSAXChain = NULL;
     615           0 :     m_xNextNodeOnSAXChain = NULL;
     616           0 :     m_xElementStackKeeper = NULL;
     617           0 : }
     618             : 
     619           0 : void XSecController::endMission()
     620             : /****** XSecController/endMission *********************************************
     621             :  *
     622             :  *   NAME
     623             :  *  endMission -- forces to end all missions
     624             :  *
     625             :  *   SYNOPSIS
     626             :  *  endMission( );
     627             :  *
     628             :  *   FUNCTION
     629             :  *  Deletes all signature information and forces all missions to an end.
     630             :  *
     631             :  *   INPUTS
     632             :  *  empty
     633             :  *
     634             :  *   RESULT
     635             :  *  empty
     636             :  *
     637             :  *   AUTHOR
     638             :  *  Michael Mi
     639             :  *  Email: michael.mi@sun.com
     640             :  ******************************************************************************/
     641             : {
     642           0 :     sal_Int32 size = m_vInternalSignatureInformations.size();
     643             : 
     644           0 :     for (int i=0; i<size; ++i)
     645             :     {
     646           0 :         if ( m_nStatusOfSecurityComponents == INITIALIZED )
     647             :         /*
     648             :          * ResolvedListener only exist when the security components are created.
     649             :          */
     650             :         {
     651             :             cssu::Reference< cssxc::sax::XMissionTaker > xMissionTaker
     652           0 :                 ( m_vInternalSignatureInformations[i].xReferenceResolvedListener, cssu::UNO_QUERY );
     653             : 
     654             :             /*
     655             :              * askes the SignatureCreator/SignatureVerifier to release
     656             :              * all resouces it uses.
     657             :              */
     658           0 :             xMissionTaker->endMission();
     659             :         }
     660             :     }
     661             : 
     662           0 :     m_xUriBinding = NULL;
     663           0 :     m_xSecurityContext = NULL;
     664             : 
     665             :     /*
     666             :      * free the status change listener reference to this object
     667             :      */
     668           0 :     if (m_xSAXEventKeeper.is())
     669             :     {
     670             :         cssu::Reference<cssxc::sax::XSAXEventKeeperStatusChangeBroadcaster>
     671           0 :             xSAXEventKeeperStatusChangeBroadcaster(m_xSAXEventKeeper, cssu::UNO_QUERY);
     672             :         xSAXEventKeeperStatusChangeBroadcaster
     673           0 :             ->addSAXEventKeeperStatusChangeListener( NULL );
     674             :     }
     675           0 : }
     676             : 
     677           0 : void XSecController::exportSignature(
     678             :     const cssu::Reference<cssxs::XDocumentHandler>& xDocumentHandler,
     679             :     const SignatureInformation& signatureInfo )
     680             : /****** XSecController/exportSignature ****************************************
     681             :  *
     682             :  *   NAME
     683             :  *  exportSignature -- export a signature structure to an XDocumentHandler
     684             :  *
     685             :  *   SYNOPSIS
     686             :  *  exportSignature( xDocumentHandler, signatureInfo);
     687             :  *
     688             :  *   FUNCTION
     689             :  *  see NAME.
     690             :  *
     691             :  *   INPUTS
     692             :  *  xDocumentHandler    - the document handler to receive the signature
     693             :  *  signatureInfo       - signature to be exported
     694             :  *
     695             :  *   RESULT
     696             :  *  empty
     697             :  *
     698             :  *   AUTHOR
     699             :  *  Michael Mi
     700             :  *  Email: michael.mi@sun.com
     701             :  ******************************************************************************/
     702             : {
     703             :     /*
     704             :      * defines all element tags in Signature element.
     705             :      */
     706           0 :     OUString tag_Signature(TAG_SIGNATURE);
     707           0 :     OUString tag_SignedInfo(TAG_SIGNEDINFO);
     708           0 :     OUString tag_CanonicalizationMethod(TAG_CANONICALIZATIONMETHOD);
     709           0 :     OUString tag_SignatureMethod(TAG_SIGNATUREMETHOD);
     710           0 :     OUString tag_Reference(TAG_REFERENCE);
     711           0 :     OUString tag_Transforms(TAG_TRANSFORMS);
     712           0 :     OUString tag_Transform(TAG_TRANSFORM);
     713           0 :     OUString tag_DigestMethod(TAG_DIGESTMETHOD);
     714           0 :     OUString tag_DigestValue(TAG_DIGESTVALUE);
     715           0 :     OUString tag_SignatureValue(TAG_SIGNATUREVALUE);
     716           0 :     OUString tag_KeyInfo(TAG_KEYINFO);
     717           0 :     OUString tag_X509Data(TAG_X509DATA);
     718           0 :     OUString tag_X509IssuerSerial(TAG_X509ISSUERSERIAL);
     719           0 :     OUString tag_X509IssuerName(TAG_X509ISSUERNAME);
     720           0 :     OUString tag_X509SerialNumber(TAG_X509SERIALNUMBER);
     721           0 :     OUString tag_X509Certificate(TAG_X509CERTIFICATE);
     722           0 :     OUString tag_Object(TAG_OBJECT);
     723           0 :     OUString tag_SignatureProperties(TAG_SIGNATUREPROPERTIES);
     724           0 :     OUString tag_SignatureProperty(TAG_SIGNATUREPROPERTY);
     725           0 :     OUString tag_Date(TAG_DATE);
     726             : 
     727           0 :     const SignatureReferenceInformations& vReferenceInfors = signatureInfo.vSignatureReferenceInfors;
     728             :     SvXMLAttributeList *pAttributeList;
     729             : 
     730             :     /*
     731             :      * Write Signature element
     732             :      */
     733           0 :     pAttributeList = new SvXMLAttributeList();
     734             :     pAttributeList->AddAttribute(
     735             :         OUString(ATTR_XMLNS),
     736           0 :         OUString(NS_XMLDSIG));
     737             : 
     738           0 :     if (!signatureInfo.ouSignatureId.isEmpty())
     739             :     {
     740             :         pAttributeList->AddAttribute(
     741             :             OUString(ATTR_ID),
     742           0 :             OUString(signatureInfo.ouSignatureId));
     743             :     }
     744             : 
     745           0 :     xDocumentHandler->startElement( tag_Signature, cssu::Reference< cssxs::XAttributeList > (pAttributeList));
     746             :     {
     747             :         /* Write SignedInfo element */
     748           0 :         xDocumentHandler->startElement(
     749             :             tag_SignedInfo,
     750           0 :             cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
     751             :         {
     752             :             /* Write CanonicalizationMethod element */
     753           0 :             pAttributeList = new SvXMLAttributeList();
     754             :             pAttributeList->AddAttribute(
     755             :                 OUString(ATTR_ALGORITHM),
     756           0 :                 OUString(ALGO_C14N));
     757           0 :             xDocumentHandler->startElement( tag_CanonicalizationMethod, cssu::Reference< cssxs::XAttributeList > (pAttributeList) );
     758           0 :             xDocumentHandler->endElement( tag_CanonicalizationMethod );
     759             : 
     760             :             /* Write SignatureMethod element */
     761           0 :             pAttributeList = new SvXMLAttributeList();
     762             :             pAttributeList->AddAttribute(
     763             :                 OUString(ATTR_ALGORITHM),
     764           0 :                 OUString(ALGO_RSASHA1));
     765           0 :             xDocumentHandler->startElement( tag_SignatureMethod, cssu::Reference< cssxs::XAttributeList > (pAttributeList) );
     766           0 :             xDocumentHandler->endElement( tag_SignatureMethod );
     767             : 
     768             :             /* Write Reference element */
     769             :             int j;
     770           0 :             int refNum = vReferenceInfors.size();
     771             : 
     772           0 :             for(j=0; j<refNum; ++j)
     773             :             {
     774           0 :                 const SignatureReferenceInformation& refInfor = vReferenceInfors[j];
     775             : 
     776           0 :                 pAttributeList = new SvXMLAttributeList();
     777           0 :                 if ( refInfor.nType != TYPE_SAMEDOCUMENT_REFERENCE )
     778             :                 /*
     779             :                  * stream reference
     780             :                  */
     781             :                 {
     782             :                     pAttributeList->AddAttribute(
     783             :                         OUString(ATTR_URI),
     784           0 :                         refInfor.ouURI);
     785             :                 }
     786             :                 else
     787             :                 /*
     788             :                  * same-document reference
     789             :                  */
     790             :                 {
     791             :                     pAttributeList->AddAttribute(
     792             :                         OUString(ATTR_URI),
     793           0 :                         OUString(CHAR_FRAGMENT)+refInfor.ouURI);
     794             :                 }
     795             : 
     796           0 :                 xDocumentHandler->startElement( tag_Reference, cssu::Reference< cssxs::XAttributeList > (pAttributeList) );
     797             :                 {
     798             :                     /* Write Transforms element */
     799           0 :                     if (refInfor.nType == TYPE_XMLSTREAM_REFERENCE)
     800             :                     /*
     801             :                      * xml stream, so c14n transform is needed
     802             :                      */
     803             :                     {
     804           0 :                         xDocumentHandler->startElement(
     805             :                             tag_Transforms,
     806           0 :                             cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
     807             :                         {
     808           0 :                             pAttributeList = new SvXMLAttributeList();
     809             :                             pAttributeList->AddAttribute(
     810             :                                 OUString(ATTR_ALGORITHM),
     811           0 :                                 OUString(ALGO_C14N));
     812           0 :                             xDocumentHandler->startElement(
     813             :                                 tag_Transform,
     814           0 :                                 cssu::Reference< cssxs::XAttributeList > (pAttributeList) );
     815           0 :                             xDocumentHandler->endElement( tag_Transform );
     816             :                         }
     817           0 :                         xDocumentHandler->endElement( tag_Transforms );
     818             :                     }
     819             : 
     820             :                     /* Write DigestMethod element */
     821           0 :                     pAttributeList = new SvXMLAttributeList();
     822             :                     pAttributeList->AddAttribute(
     823             :                         OUString(ATTR_ALGORITHM),
     824           0 :                         OUString(ALGO_XMLDSIGSHA1));
     825           0 :                     xDocumentHandler->startElement(
     826             :                         tag_DigestMethod,
     827           0 :                         cssu::Reference< cssxs::XAttributeList > (pAttributeList) );
     828           0 :                     xDocumentHandler->endElement( tag_DigestMethod );
     829             : 
     830             :                     /* Write DigestValue element */
     831           0 :                     xDocumentHandler->startElement(
     832             :                         tag_DigestValue,
     833           0 :                         cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
     834           0 :                     xDocumentHandler->characters( refInfor.ouDigestValue );
     835           0 :                     xDocumentHandler->endElement( tag_DigestValue );
     836             :                 }
     837           0 :                 xDocumentHandler->endElement( tag_Reference );
     838             :             }
     839             :         }
     840           0 :         xDocumentHandler->endElement( tag_SignedInfo );
     841             : 
     842             :         /* Write SignatureValue element */
     843           0 :         xDocumentHandler->startElement(
     844             :             tag_SignatureValue,
     845           0 :             cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
     846           0 :         xDocumentHandler->characters( signatureInfo.ouSignatureValue );
     847           0 :         xDocumentHandler->endElement( tag_SignatureValue );
     848             : 
     849             :         /* Write KeyInfo element */
     850           0 :         xDocumentHandler->startElement(
     851             :             tag_KeyInfo,
     852           0 :             cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
     853             :         {
     854             :             /* Write X509Data element */
     855           0 :             xDocumentHandler->startElement(
     856             :                 tag_X509Data,
     857           0 :                 cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
     858             :             {
     859             :                 /* Write X509IssuerSerial element */
     860           0 :                 xDocumentHandler->startElement(
     861             :                     tag_X509IssuerSerial,
     862           0 :                     cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
     863             :                 {
     864             :                     /* Write X509IssuerName element */
     865           0 :                     xDocumentHandler->startElement(
     866             :                         tag_X509IssuerName,
     867           0 :                         cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
     868           0 :                     xDocumentHandler->characters( signatureInfo.ouX509IssuerName );
     869           0 :                     xDocumentHandler->endElement( tag_X509IssuerName );
     870             : 
     871             :                     /* Write X509SerialNumber element */
     872           0 :                     xDocumentHandler->startElement(
     873             :                         tag_X509SerialNumber,
     874           0 :                         cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
     875           0 :                     xDocumentHandler->characters( signatureInfo.ouX509SerialNumber );
     876           0 :                     xDocumentHandler->endElement( tag_X509SerialNumber );
     877             :                 }
     878           0 :                 xDocumentHandler->endElement( tag_X509IssuerSerial );
     879             : 
     880             :                 /* Write X509Certificate element */
     881           0 :                 if (!signatureInfo.ouX509Certificate.isEmpty())
     882             :                 {
     883           0 :                     xDocumentHandler->startElement(
     884             :                         tag_X509Certificate,
     885           0 :                         cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
     886           0 :                     xDocumentHandler->characters( signatureInfo.ouX509Certificate );
     887           0 :                     xDocumentHandler->endElement( tag_X509Certificate );
     888             :                 }
     889             :             }
     890           0 :             xDocumentHandler->endElement( tag_X509Data );
     891             :         }
     892           0 :         xDocumentHandler->endElement( tag_KeyInfo );
     893             : 
     894             :         /* Write Object element */
     895           0 :         xDocumentHandler->startElement(
     896             :             tag_Object,
     897           0 :             cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
     898             :         {
     899             :             /* Write SignatureProperties element */
     900           0 :             xDocumentHandler->startElement(
     901             :                 tag_SignatureProperties,
     902           0 :                 cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
     903             :             {
     904             :                 /* Write SignatureProperty element */
     905           0 :                 pAttributeList = new SvXMLAttributeList();
     906             :                 pAttributeList->AddAttribute(
     907             :                     OUString(ATTR_ID),
     908           0 :                     signatureInfo.ouPropertyId);
     909             :                 pAttributeList->AddAttribute(
     910             :                     OUString(ATTR_TARGET),
     911           0 :                     OUString(CHAR_FRAGMENT)+signatureInfo.ouSignatureId);
     912           0 :                 xDocumentHandler->startElement(
     913             :                     tag_SignatureProperty,
     914           0 :                     cssu::Reference< cssxs::XAttributeList > (pAttributeList));
     915             :                 {
     916             :                     /* Write timestamp element */
     917             : 
     918           0 :                     pAttributeList = new SvXMLAttributeList();
     919             :                     pAttributeList->AddAttribute(
     920             :                         OUString(ATTR_XMLNS)
     921           0 :                             +OUString(":")
     922           0 :                             +OUString(NSTAG_DC),
     923           0 :                         OUString(NS_DC));
     924             : 
     925           0 :                     xDocumentHandler->startElement(
     926             :                         OUString(NSTAG_DC)
     927           0 :                             +OUString(":")
     928           0 :                             +tag_Date,
     929           0 :                         cssu::Reference< cssxs::XAttributeList > (pAttributeList));
     930             : 
     931           0 :                     OUStringBuffer buffer;
     932             :                     //If the xml signature was already contained in the document,
     933             :                     //then we use the original date and time string, rather then the
     934             :                     //converted one. This avoids writing a different string due to
     935             :                     //e.g. rounding issues and thus breaking the signature.
     936           0 :                     if (!signatureInfo.ouDateTime.isEmpty())
     937           0 :                         buffer = signatureInfo.ouDateTime;
     938             :                     else
     939             :                     {
     940           0 :                         buffer = utl::toISO8601(signatureInfo.stDateTime);
     941             :                     }
     942           0 :                     xDocumentHandler->characters( buffer.makeStringAndClear() );
     943             : 
     944           0 :                     xDocumentHandler->endElement(
     945             :                         OUString(NSTAG_DC)
     946           0 :                             +OUString(":")
     947           0 :                             +tag_Date);
     948             :                 }
     949           0 :                 xDocumentHandler->endElement( tag_SignatureProperty );
     950             :             }
     951           0 :             xDocumentHandler->endElement( tag_SignatureProperties );
     952             :         }
     953           0 :         xDocumentHandler->endElement( tag_Object );
     954             :     }
     955           0 :     xDocumentHandler->endElement( tag_Signature );
     956           0 : }
     957             : 
     958           0 : SignatureInformation XSecController::getSignatureInformation( sal_Int32 nSecurityId ) const
     959             : {
     960           0 :     SignatureInformation aInf( 0 );
     961           0 :     int nIndex = findSignatureInfor(nSecurityId);
     962             :     DBG_ASSERT( nIndex != -1, "getSignatureInformation - SecurityId is invalid!" );
     963           0 :     if ( nIndex != -1)
     964             :     {
     965           0 :         aInf = m_vInternalSignatureInformations[nIndex].signatureInfor;
     966             :     }
     967           0 :     return aInf;
     968             : }
     969             : 
     970           0 : SignatureInformations XSecController::getSignatureInformations() const
     971             : {
     972           0 :     SignatureInformations vInfors;
     973           0 :     int sigNum = m_vInternalSignatureInformations.size();
     974             : 
     975           0 :     for (int i=0; i<sigNum; ++i)
     976             :     {
     977           0 :         SignatureInformation si = m_vInternalSignatureInformations[i].signatureInfor;
     978           0 :         vInfors.push_back(si);
     979           0 :     }
     980             : 
     981           0 :     return vInfors;
     982             : }
     983             : 
     984             : /*
     985             :  * XSecurityController
     986             :  *
     987             :  * no methods
     988             :  */
     989             : 
     990             : /*
     991             :  * XFastPropertySet
     992             :  */
     993             : 
     994             : /*
     995             :  * XSAXEventKeeperStatusChangeListener
     996             :  */
     997             : 
     998           0 : void SAL_CALL XSecController::blockingStatusChanged( sal_Bool isBlocking )
     999             :     throw (cssu::RuntimeException)
    1000             : {
    1001           0 :     this->m_bIsBlocking = isBlocking;
    1002           0 :     checkChainingStatus();
    1003           0 : }
    1004             : 
    1005           0 : void SAL_CALL XSecController::collectionStatusChanged(
    1006             :     sal_Bool isInsideCollectedElement )
    1007             :     throw (cssu::RuntimeException)
    1008             : {
    1009           0 :     this->m_bIsCollectingElement = isInsideCollectedElement;
    1010           0 :     checkChainingStatus();
    1011           0 : }
    1012             : 
    1013           0 : void SAL_CALL XSecController::bufferStatusChanged( sal_Bool /*isBufferEmpty*/)
    1014             :     throw (cssu::RuntimeException)
    1015             : {
    1016             : 
    1017           0 : }
    1018             : 
    1019             : /*
    1020             :  * XSignatureCreationResultListener
    1021             :  */
    1022           0 : void SAL_CALL XSecController::signatureCreated( sal_Int32 securityId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
    1023             :         throw (com::sun::star::uno::RuntimeException)
    1024             : {
    1025           0 :     int index = findSignatureInfor(securityId);
    1026             :     DBG_ASSERT( index != -1, "Signature Not Found!" );
    1027             : 
    1028           0 :     SignatureInformation& signatureInfor = m_vInternalSignatureInformations[index].signatureInfor;
    1029             : 
    1030           0 :     signatureInfor.nStatus = nResult;
    1031           0 : }
    1032             : 
    1033             : /*
    1034             :  * XSignatureVerifyResultListener
    1035             :  */
    1036           0 : void SAL_CALL XSecController::signatureVerified( sal_Int32 securityId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
    1037             :         throw (com::sun::star::uno::RuntimeException)
    1038             : {
    1039           0 :     int index = findSignatureInfor(securityId);
    1040             :     DBG_ASSERT( index != -1, "Signature Not Found!" );
    1041             : 
    1042           0 :     SignatureInformation& signatureInfor = m_vInternalSignatureInformations[index].signatureInfor;
    1043             : 
    1044           0 :     signatureInfor.nStatus = nResult;
    1045           0 : }
    1046             : 
    1047             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10