LCOV - code coverage report
Current view: top level - libreoffice/xmlsecurity/source/helper - xsecverify.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 155 0.0 %
Date: 2012-12-27 Functions: 0 17 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : 
      21             : #include <xsecctl.hxx>
      22             : #include "xsecparser.hxx"
      23             : #include <tools/debug.hxx>
      24             : 
      25             : #include <com/sun/star/xml/crypto/sax/XKeyCollector.hpp>
      26             : #include <com/sun/star/xml/crypto/sax/ElementMarkPriority.hpp>
      27             : #include <com/sun/star/xml/crypto/sax/XReferenceResolvedBroadcaster.hpp>
      28             : #include <com/sun/star/xml/crypto/sax/XReferenceCollector.hpp>
      29             : #include <com/sun/star/xml/crypto/sax/XSignatureVerifyResultBroadcaster.hpp>
      30             : #include <com/sun/star/xml/sax/SAXParseException.hpp>
      31             : 
      32             : namespace cssu = com::sun::star::uno;
      33             : namespace cssl = com::sun::star::lang;
      34             : namespace cssxc = com::sun::star::xml::crypto;
      35             : namespace cssxs = com::sun::star::xml::sax;
      36             : 
      37             : /* xml security framework components */
      38             : #define SIGNATUREVERIFIER_COMPONENT "com.sun.star.xml.crypto.sax.SignatureVerifier"
      39             : 
      40             : /* protected: for signature verify */
      41           0 : cssu::Reference< cssxc::sax::XReferenceResolvedListener > XSecController::prepareSignatureToRead(
      42             :     sal_Int32 nSecurityId)
      43             : {
      44           0 :     if ( m_nStatusOfSecurityComponents != INITIALIZED )
      45             :     {
      46           0 :         return NULL;
      47             :     }
      48             : 
      49             :     sal_Int32 nIdOfSignatureElementCollector;
      50           0 :     cssu::Reference< cssxc::sax::XReferenceResolvedListener > xReferenceResolvedListener;
      51             : 
      52             :     nIdOfSignatureElementCollector =
      53           0 :         m_xSAXEventKeeper->addSecurityElementCollector( cssxc::sax::ElementMarkPriority_BEFOREMODIFY, sal_False);
      54             : 
      55           0 :     m_xSAXEventKeeper->setSecurityId(nIdOfSignatureElementCollector, nSecurityId);
      56             : 
      57             :         /*
      58             :          * create a SignatureVerifier
      59             :          */
      60           0 :     cssu::Reference< cssl::XMultiComponentFactory > xMCF( mxCtx->getServiceManager() );
      61             :     xReferenceResolvedListener = cssu::Reference< cssxc::sax::XReferenceResolvedListener >(
      62           0 :         xMCF->createInstanceWithContext(
      63           0 :             rtl::OUString( SIGNATUREVERIFIER_COMPONENT ), mxCtx),
      64           0 :         cssu::UNO_QUERY);
      65             : 
      66           0 :     cssu::Reference<cssl::XInitialization> xInitialization(xReferenceResolvedListener, cssu::UNO_QUERY);
      67             : 
      68           0 :     cssu::Sequence<cssu::Any> args(5);
      69           0 :     args[0] = cssu::makeAny(rtl::OUString::valueOf(nSecurityId));
      70           0 :     args[1] = cssu::makeAny(m_xSAXEventKeeper);
      71           0 :     args[2] = cssu::makeAny(rtl::OUString::valueOf(nIdOfSignatureElementCollector));
      72           0 :     args[3] = cssu::makeAny(m_xSecurityContext);
      73           0 :     args[4] = cssu::makeAny(m_xXMLSignature);
      74           0 :     xInitialization->initialize(args);
      75             : 
      76             :     cssu::Reference< cssxc::sax::XSignatureVerifyResultBroadcaster >
      77           0 :         signatureVerifyResultBroadcaster(xReferenceResolvedListener, cssu::UNO_QUERY);
      78             : 
      79           0 :     signatureVerifyResultBroadcaster->addSignatureVerifyResultListener( this );
      80             : 
      81             :     cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
      82             :         (m_xSAXEventKeeper,
      83           0 :         cssu::UNO_QUERY);
      84             : 
      85           0 :     xReferenceResolvedBroadcaster->addReferenceResolvedListener(
      86             :         nIdOfSignatureElementCollector,
      87           0 :         xReferenceResolvedListener);
      88             : 
      89           0 :     cssu::Reference<cssxc::sax::XKeyCollector> keyCollector (xReferenceResolvedListener, cssu::UNO_QUERY);
      90           0 :     keyCollector->setKeyId(0);
      91             : 
      92           0 :     return xReferenceResolvedListener;
      93             : }
      94             : 
      95           0 : void XSecController::addSignature()
      96             : {
      97           0 :     cssu::Reference< cssxc::sax::XReferenceResolvedListener > xReferenceResolvedListener = NULL;
      98           0 :     sal_Int32 nSignatureId = 0;
      99             : 
     100             : 
     101           0 :     if (m_bVerifyCurrentSignature)
     102             :     {
     103           0 :         chainOn(true);
     104           0 :         xReferenceResolvedListener = prepareSignatureToRead( m_nReservedSignatureId );
     105           0 :         m_bVerifyCurrentSignature = false;
     106           0 :         nSignatureId = m_nReservedSignatureId;
     107             :     }
     108             : 
     109           0 :     InternalSignatureInformation isi( nSignatureId, xReferenceResolvedListener );
     110           0 :     m_vInternalSignatureInformations.push_back( isi );
     111           0 : }
     112             : 
     113           0 : void XSecController::addReference( const rtl::OUString& ouUri)
     114             : {
     115           0 :     if (m_vInternalSignatureInformations.empty())
     116             :     {
     117             :         OSL_TRACE("XSecController::addReference: no signature");
     118           0 :         return;
     119             :     }
     120           0 :     InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
     121           0 :     isi.addReference(TYPE_SAMEDOCUMENT_REFERENCE,ouUri, -1 );
     122             : }
     123             : 
     124           0 : void XSecController::addStreamReference(
     125             :     const rtl::OUString& ouUri,
     126             :     bool isBinary )
     127             : {
     128           0 :         sal_Int32 type = (isBinary?TYPE_BINARYSTREAM_REFERENCE:TYPE_XMLSTREAM_REFERENCE);
     129             : 
     130           0 :     if (m_vInternalSignatureInformations.empty())
     131             :     {
     132             :         OSL_TRACE("XSecController::addStreamReference: no signature");
     133           0 :         return;
     134             :     }
     135           0 :     InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
     136             : 
     137           0 :     if ( isi.xReferenceResolvedListener.is() )
     138             :     {
     139             :             /*
     140             :              * get the input stream
     141             :              */
     142             :             cssu::Reference< com::sun::star::io::XInputStream > xObjectInputStream
     143           0 :                 = getObjectInputStream( ouUri );
     144             : 
     145           0 :         if ( xObjectInputStream.is() )
     146             :         {
     147             :             cssu::Reference<cssxc::XUriBinding> xUriBinding
     148           0 :                 (isi.xReferenceResolvedListener, cssu::UNO_QUERY);
     149           0 :             xUriBinding->setUriBinding(ouUri, xObjectInputStream);
     150           0 :         }
     151             :     }
     152             : 
     153           0 :     isi.addReference(type, ouUri, -1);
     154             : }
     155             : 
     156           0 : void XSecController::setReferenceCount() const
     157             : {
     158           0 :     if (m_vInternalSignatureInformations.empty())
     159             :     {
     160             :         OSL_TRACE("XSecController::setReferenceCount: no signature");
     161           0 :         return;
     162             :     }
     163             :     const InternalSignatureInformation &isi =
     164           0 :         m_vInternalSignatureInformations.back();
     165             : 
     166           0 :     if ( isi.xReferenceResolvedListener.is() )
     167             :     {
     168           0 :         const SignatureReferenceInformations &refInfors = isi.signatureInfor.vSignatureReferenceInfors;
     169             : 
     170           0 :         int refNum = refInfors.size();
     171           0 :         sal_Int32 referenceCount = 0;
     172             : 
     173           0 :         for(int i=0 ; i<refNum; ++i)
     174             :         {
     175           0 :             if (refInfors[i].nType == TYPE_SAMEDOCUMENT_REFERENCE )
     176             :             /*
     177             :              * same-document reference
     178             :              */
     179             :             {
     180           0 :                 referenceCount++;
     181             :             }
     182             :         }
     183             : 
     184             :         cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
     185           0 :             (isi.xReferenceResolvedListener, cssu::UNO_QUERY);
     186           0 :         xReferenceCollector->setReferenceCount( referenceCount );
     187             :     }
     188             : }
     189             : 
     190           0 : void XSecController::setX509IssuerName( rtl::OUString& ouX509IssuerName )
     191             : {
     192           0 :     if (m_vInternalSignatureInformations.empty())
     193             :     {
     194             :         OSL_TRACE("XSecController::setX509IssuerName: no signature");
     195           0 :         return;
     196             :     }
     197           0 :     InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
     198           0 :     isi.signatureInfor.ouX509IssuerName = ouX509IssuerName;
     199             : }
     200             : 
     201           0 : void XSecController::setX509SerialNumber( rtl::OUString& ouX509SerialNumber )
     202             : {
     203           0 :     if (m_vInternalSignatureInformations.empty())
     204             :     {
     205             :         OSL_TRACE("XSecController::setX509SerialNumber: no signature");
     206           0 :         return;
     207             :     }
     208           0 :     InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
     209           0 :     isi.signatureInfor.ouX509SerialNumber = ouX509SerialNumber;
     210             : }
     211             : 
     212           0 : void XSecController::setX509Certificate( rtl::OUString& ouX509Certificate )
     213             : {
     214           0 :     if (m_vInternalSignatureInformations.empty())
     215             :     {
     216             :         OSL_TRACE("XSecController::setX509Certificate: no signature");
     217           0 :         return;
     218             :     }
     219           0 :     InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
     220           0 :     isi.signatureInfor.ouX509Certificate = ouX509Certificate;
     221             : }
     222             : 
     223           0 : void XSecController::setSignatureValue( rtl::OUString& ouSignatureValue )
     224             : {
     225           0 :     if (m_vInternalSignatureInformations.empty())
     226             :     {
     227             :         OSL_TRACE("XSecController::setSignatureValue: no signature");
     228           0 :         return;
     229             :     }
     230           0 :     InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
     231           0 :     isi.signatureInfor.ouSignatureValue = ouSignatureValue;
     232             : }
     233             : 
     234           0 : void XSecController::setDigestValue( rtl::OUString& ouDigestValue )
     235             : {
     236           0 :     if (m_vInternalSignatureInformations.empty())
     237             :     {
     238             :         OSL_TRACE("XSecController::setDigestValue: no signature");
     239           0 :         return;
     240             :     }
     241           0 :     InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
     242           0 :     if (isi.signatureInfor.vSignatureReferenceInfors.empty())
     243             :     {
     244             :         OSL_TRACE("XSecController::setDigestValue: no signature reference");
     245           0 :         return;
     246             :     }
     247             :     SignatureReferenceInformation &reference =
     248           0 :         isi.signatureInfor.vSignatureReferenceInfors.back();
     249           0 :     reference.ouDigestValue = ouDigestValue;
     250             : }
     251             : 
     252           0 : void XSecController::setDate( rtl::OUString& ouDate )
     253             : {
     254           0 :     if (m_vInternalSignatureInformations.empty())
     255             :     {
     256             :         OSL_TRACE("XSecController::setDate: no signature");
     257           0 :         return;
     258             :     }
     259           0 :     InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
     260           0 :     convertDateTime( isi.signatureInfor.stDateTime, ouDate );
     261           0 :     isi.signatureInfor.ouDateTime = ouDate;
     262             : }
     263             : 
     264           0 : void XSecController::setId( rtl::OUString& ouId )
     265             : {
     266           0 :     if (m_vInternalSignatureInformations.empty())
     267             :     {
     268             :         OSL_TRACE("XSecController::setId: no signature");
     269           0 :         return;
     270             :     }
     271           0 :     InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
     272           0 :     isi.signatureInfor.ouSignatureId = ouId;
     273             : }
     274             : 
     275           0 : void XSecController::setPropertyId( rtl::OUString& ouPropertyId )
     276             : {
     277           0 :     if (m_vInternalSignatureInformations.empty())
     278             :     {
     279             :         OSL_TRACE("XSecController::setPropertyId: no signature");
     280           0 :         return;
     281             :     }
     282           0 :     InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
     283           0 :     isi.signatureInfor.ouPropertyId = ouPropertyId;
     284             : }
     285             : 
     286             : /* public: for signature verify */
     287           0 : void XSecController::collectToVerify( const rtl::OUString& referenceId )
     288             : {
     289             :     /* DBG_ASSERT( m_xSAXEventKeeper.is(), "the SAXEventKeeper is NULL" ); */
     290             : 
     291           0 :     if ( m_nStatusOfSecurityComponents == INITIALIZED )
     292             :     /*
     293             :      * if all security components are ready, verify the signature.
     294             :      */
     295             :     {
     296           0 :         bool bJustChainingOn = false;
     297           0 :         cssu::Reference< cssxs::XDocumentHandler > xHandler = NULL;
     298             : 
     299             :         int i,j;
     300           0 :         int sigNum = m_vInternalSignatureInformations.size();
     301             : 
     302           0 :         for (i=0; i<sigNum; ++i)
     303             :         {
     304           0 :             InternalSignatureInformation& isi = m_vInternalSignatureInformations[i];
     305           0 :             SignatureReferenceInformations& vReferenceInfors = isi.signatureInfor.vSignatureReferenceInfors;
     306           0 :             int refNum = vReferenceInfors.size();
     307             : 
     308           0 :             for (j=0; j<refNum; ++j)
     309             :             {
     310           0 :                 SignatureReferenceInformation &refInfor = vReferenceInfors[j];
     311             : 
     312           0 :                 if (refInfor.ouURI == referenceId)
     313             :                 {
     314           0 :                     if (chainOn(false))
     315             :                     {
     316           0 :                         bJustChainingOn = true;
     317           0 :                         xHandler = m_xSAXEventKeeper->setNextHandler(NULL);
     318             :                     }
     319             : 
     320           0 :                     sal_Int32 nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
     321           0 :                         cssxc::sax::ElementMarkPriority_BEFOREMODIFY, sal_False );
     322             : 
     323             :                     cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
     324             :                         (m_xSAXEventKeeper,
     325           0 :                         cssu::UNO_QUERY );
     326             : 
     327             :                     cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
     328           0 :                         ( isi.xReferenceResolvedListener, cssu::UNO_QUERY );
     329             : 
     330           0 :                     m_xSAXEventKeeper->setSecurityId(nKeeperId, isi.signatureInfor.nSecurityId);
     331           0 :                     xReferenceResolvedBroadcaster->addReferenceResolvedListener( nKeeperId, isi.xReferenceResolvedListener);
     332           0 :                     xReferenceCollector->setReferenceId( nKeeperId );
     333             : 
     334           0 :                     isi.vKeeperIds[j] = nKeeperId;
     335           0 :                     break;
     336             :                 }
     337             :             }
     338             :         }
     339             : 
     340           0 :         if ( bJustChainingOn )
     341             :         {
     342           0 :             cssu::Reference< cssxs::XDocumentHandler > xSEKHandler(m_xSAXEventKeeper, cssu::UNO_QUERY);
     343           0 :             if (m_xElementStackKeeper.is())
     344             :             {
     345           0 :                 m_xElementStackKeeper->retrieve(xSEKHandler, sal_True);
     346             :             }
     347           0 :             m_xSAXEventKeeper->setNextHandler(xHandler);
     348           0 :         }
     349             :     }
     350           0 : }
     351             : 
     352           0 : void XSecController::addSignature( sal_Int32 nSignatureId )
     353             : {
     354             :     DBG_ASSERT( m_pXSecParser != NULL, "No XSecParser initialized" );
     355             : 
     356           0 :     m_nReservedSignatureId = nSignatureId;
     357           0 :     m_bVerifyCurrentSignature = true;
     358           0 : }
     359             : 
     360           0 : cssu::Reference< cssxs::XDocumentHandler > XSecController::createSignatureReader()
     361             : {
     362           0 :     m_pXSecParser = new XSecParser( this, NULL );
     363           0 :     cssu::Reference< cssl::XInitialization > xInitialization = m_pXSecParser;
     364             : 
     365           0 :     setSAXChainConnector(xInitialization, NULL, NULL);
     366             : 
     367           0 :     return m_pXSecParser;
     368             : }
     369             : 
     370           0 : void XSecController::releaseSignatureReader()
     371             : {
     372           0 :     clearSAXChainConnector( );
     373           0 :     m_pXSecParser = NULL;
     374           0 : }
     375             : 
     376             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10