LCOV - code coverage report
Current view: top level - xmlsecurity/source/helper - xsecctl.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 405 0.0 %
Date: 2012-08-25 Functions: 0 26 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 720 0.0 %

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

Generated by: LCOV version 1.10