LCOV - code coverage report
Current view: top level - xmlsecurity/source/helper - xsecsign.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 0 129 0.0 %
Date: 2015-06-13 12:38:46 Functions: 0 7 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : 
      21             : #include "xsecctl.hxx"
      22             : #include <tools/debug.hxx>
      23             : 
      24             : #include <com/sun/star/xml/crypto/sax/XKeyCollector.hpp>
      25             : #include <com/sun/star/xml/crypto/sax/ElementMarkPriority.hpp>
      26             : #include <com/sun/star/xml/crypto/sax/XReferenceResolvedBroadcaster.hpp>
      27             : #include <com/sun/star/xml/crypto/sax/XBlockerMonitor.hpp>
      28             : #include <com/sun/star/xml/crypto/sax/XReferenceCollector.hpp>
      29             : #include <com/sun/star/xml/crypto/sax/XSignatureCreationResultBroadcaster.hpp>
      30             : #include <com/sun/star/io/XActiveDataSource.hpp>
      31             : #include <rtl/uuid.h>
      32             : 
      33             : #include <stdio.h>
      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             : 
      40             : /* xml security framework components */
      41             : #define SIGNATURECREATOR_COMPONENT "com.sun.star.xml.crypto.sax.SignatureCreator"
      42             : 
      43             : /* protected: for signature generation */
      44           0 : OUString XSecController::createId()
      45             : {
      46           0 :     cssu::Sequence< sal_Int8 > aSeq( 16 );
      47           0 :     rtl_createUuid (reinterpret_cast<sal_uInt8 *>(aSeq.getArray()), 0, sal_True);
      48             : 
      49           0 :     char str[68]="ID_";
      50           0 :     int length = 3;
      51           0 :     for (int i=0; i<16; ++i)
      52             :     {
      53           0 :         length += sprintf(str+length, "%04x", (unsigned char)aSeq[i]);
      54             :     }
      55             : 
      56           0 :     return OUString::createFromAscii(str);
      57             : }
      58             : 
      59           0 : cssu::Reference< cssxc::sax::XReferenceResolvedListener > XSecController::prepareSignatureToWrite(
      60             :     InternalSignatureInformation& internalSignatureInfor )
      61             : {
      62           0 :     sal_Int32 nSecurityId = internalSignatureInfor.signatureInfor.nSecurityId;
      63           0 :     SignatureReferenceInformations& vReferenceInfors = internalSignatureInfor.signatureInfor.vSignatureReferenceInfors;
      64             : 
      65             :     sal_Int32 nIdOfSignatureElementCollector;
      66           0 :     cssu::Reference< cssxc::sax::XReferenceResolvedListener > xReferenceResolvedListener;
      67             : 
      68             :     nIdOfSignatureElementCollector =
      69           0 :         m_xSAXEventKeeper->addSecurityElementCollector( cssxc::sax::ElementMarkPriority_AFTERMODIFY, sal_True );
      70             : 
      71           0 :     m_xSAXEventKeeper->setSecurityId(nIdOfSignatureElementCollector, nSecurityId);
      72             : 
      73             :         /*
      74             :          * create a SignatureCreator
      75             :          */
      76           0 :     cssu::Reference< cssl::XMultiComponentFactory > xMCF( mxCtx->getServiceManager() );
      77           0 :     xReferenceResolvedListener = cssu::Reference< cssxc::sax::XReferenceResolvedListener >(
      78           0 :         xMCF->createInstanceWithContext(
      79           0 :             OUString(SIGNATURECREATOR_COMPONENT), mxCtx),
      80           0 :         cssu::UNO_QUERY);
      81             : 
      82           0 :     cssu::Reference<cssl::XInitialization> xInitialization(xReferenceResolvedListener, cssu::UNO_QUERY);
      83             : 
      84           0 :     cssu::Sequence<cssu::Any> args(5);
      85           0 :     args[0] = cssu::makeAny(OUString::number(nSecurityId));
      86           0 :     args[1] = cssu::makeAny(m_xSAXEventKeeper);
      87           0 :     args[2] = cssu::makeAny(OUString::number(nIdOfSignatureElementCollector));
      88             : 
      89             :     //for nss, the internal module is used for signing, which needs to be improved later
      90           0 :     sal_Int32 nEnvIndex = internalSignatureInfor.signatureInfor.nSecurityEnvironmentIndex;
      91           0 :     if( nEnvIndex < 0 || nEnvIndex >= m_xSecurityContext->getSecurityEnvironmentNumber())
      92             :     {// set defaultEnv
      93           0 :         args[3] = cssu::makeAny(m_xSecurityContext->getSecurityEnvironment());
      94             :     }
      95             :     else
      96             :     {
      97           0 :         args[3] = cssu::makeAny(m_xSecurityContext->getSecurityEnvironmentByIndex(nEnvIndex));
      98             :     }
      99             : 
     100           0 :     args[4] = cssu::makeAny(m_xXMLSignature);
     101           0 :     xInitialization->initialize(args);
     102             : 
     103           0 :     sal_Int32 nBlockerId = m_xSAXEventKeeper->addBlocker();
     104           0 :     m_xSAXEventKeeper->setSecurityId(nBlockerId, nSecurityId);
     105             : 
     106           0 :     cssu::Reference<cssxc::sax::XBlockerMonitor> xBlockerMonitor(xReferenceResolvedListener, cssu::UNO_QUERY);
     107           0 :     xBlockerMonitor->setBlockerId(nBlockerId);
     108             : 
     109             :     cssu::Reference< cssxc::sax::XSignatureCreationResultBroadcaster >
     110           0 :         xSignatureCreationResultBroadcaster(xReferenceResolvedListener, cssu::UNO_QUERY);
     111             : 
     112           0 :     xSignatureCreationResultBroadcaster->addSignatureCreationResultListener( this );
     113             : 
     114             :     cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster>
     115             :         xReferenceResolvedBroadcaster
     116             :         (m_xSAXEventKeeper,
     117           0 :         cssu::UNO_QUERY);
     118             : 
     119           0 :     xReferenceResolvedBroadcaster->addReferenceResolvedListener(
     120             :         nIdOfSignatureElementCollector,
     121           0 :         xReferenceResolvedListener);
     122             : 
     123             :     cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
     124           0 :         (xReferenceResolvedListener, cssu::UNO_QUERY);
     125             : 
     126             :     int i;
     127           0 :     int size = vReferenceInfors.size();
     128           0 :     sal_Int32 nReferenceCount = 0;
     129             : 
     130           0 :     for(i=0; i<size; ++i)
     131             :     {
     132           0 :         sal_Int32 keeperId = internalSignatureInfor.vKeeperIds[i];
     133             : 
     134           0 :         if ( keeperId != -1)
     135             :         {
     136           0 :             m_xSAXEventKeeper->setSecurityId(keeperId, nSecurityId);
     137           0 :             xReferenceResolvedBroadcaster->addReferenceResolvedListener( keeperId, xReferenceResolvedListener);
     138           0 :             xReferenceCollector->setReferenceId( keeperId );
     139           0 :             nReferenceCount++;
     140             :         }
     141             :     }
     142             : 
     143           0 :     xReferenceCollector->setReferenceCount( nReferenceCount );
     144             : 
     145             :     /*
     146             :      * adds all URI binding
     147             :      */
     148             :     cssu::Reference<cssxc::XUriBinding> xUriBinding
     149           0 :         (xReferenceResolvedListener, cssu::UNO_QUERY);
     150             : 
     151           0 :     for(i=0; i<size; ++i)
     152             :     {
     153           0 :         const SignatureReferenceInformation& refInfor = vReferenceInfors[i];
     154             : 
     155             :         cssu::Reference< com::sun::star::io::XInputStream > xInputStream
     156           0 :             = getObjectInputStream( refInfor.ouURI );
     157             : 
     158           0 :         if (xInputStream.is())
     159             :         {
     160           0 :             xUriBinding->setUriBinding(refInfor.ouURI,xInputStream);
     161             :         }
     162           0 :     }
     163             : 
     164           0 :     cssu::Reference<cssxc::sax::XKeyCollector> keyCollector (xReferenceResolvedListener, cssu::UNO_QUERY);
     165           0 :     keyCollector->setKeyId(0);
     166             : 
     167           0 :     internalSignatureInfor.signatureInfor.ouSignatureId = createId();
     168           0 :     internalSignatureInfor.signatureInfor.ouPropertyId = createId();
     169           0 :     internalSignatureInfor.addReference(TYPE_SAMEDOCUMENT_REFERENCE, internalSignatureInfor.signatureInfor.ouPropertyId, -1 );
     170           0 :     size++;
     171             : 
     172             :     /*
     173             :      * replace both digestValues and signatueValue to " "
     174             :      */
     175           0 :     for(i=0; i<size; ++i)
     176             :     {
     177           0 :         SignatureReferenceInformation& refInfor = vReferenceInfors[i];
     178           0 :         refInfor.ouDigestValue = CHAR_BLANK;
     179             :     }
     180             : 
     181           0 :     internalSignatureInfor.signatureInfor.ouSignatureValue = CHAR_BLANK;
     182             : 
     183           0 :     return xReferenceResolvedListener;
     184             : }
     185             : 
     186           0 : void XSecController::signAStream( sal_Int32 securityId, const OUString& uri, const OUString& /*objectURL*/, bool isBinary)
     187             : {
     188           0 :     sal_Int32 type = isBinary ? TYPE_BINARYSTREAM_REFERENCE : TYPE_XMLSTREAM_REFERENCE;
     189             : 
     190           0 :     int index = findSignatureInfor( securityId );
     191             : 
     192           0 :     if (index == -1)
     193             :     {
     194           0 :         InternalSignatureInformation isi(securityId, NULL);
     195           0 :         isi.addReference(type, uri, -1);
     196           0 :         m_vInternalSignatureInformations.push_back( isi );
     197             :     }
     198             :     else
     199             :     {
     200           0 :         m_vInternalSignatureInformations[index].addReference(type, uri, -1);
     201             :     }
     202           0 : }
     203             : 
     204           0 : void XSecController::setX509Certificate(
     205             :     sal_Int32 nSecurityId,
     206             :     const OUString& ouX509IssuerName,
     207             :     const OUString& ouX509SerialNumber,
     208             :     const OUString& ouX509Cert)
     209             : {
     210           0 :     setX509Certificate(nSecurityId, -1, ouX509IssuerName, ouX509SerialNumber, ouX509Cert);
     211           0 : }
     212             : 
     213           0 : void XSecController::setX509Certificate(
     214             :     sal_Int32 nSecurityId,
     215             :     const sal_Int32 nSecurityEnvironmentIndex,
     216             :     const OUString& ouX509IssuerName,
     217             :     const OUString& ouX509SerialNumber,
     218             :     const OUString& ouX509Cert)
     219             : {
     220           0 :     int index = findSignatureInfor( nSecurityId );
     221             : 
     222           0 :     if ( index == -1 )
     223             :     {
     224           0 :         InternalSignatureInformation isi(nSecurityId, NULL);
     225           0 :         isi.signatureInfor.nSecurityEnvironmentIndex = nSecurityEnvironmentIndex;
     226           0 :         isi.signatureInfor.ouX509IssuerName = ouX509IssuerName;
     227           0 :         isi.signatureInfor.ouX509SerialNumber = ouX509SerialNumber;
     228           0 :         isi.signatureInfor.ouX509Certificate = ouX509Cert;
     229           0 :         m_vInternalSignatureInformations.push_back( isi );
     230             :     }
     231             :     else
     232             :     {
     233             :         SignatureInformation &si
     234           0 :             = m_vInternalSignatureInformations[index].signatureInfor;
     235           0 :         si.ouX509IssuerName = ouX509IssuerName;
     236           0 :         si.ouX509SerialNumber = ouX509SerialNumber;
     237           0 :         si.ouX509Certificate = ouX509Cert;
     238           0 :         si.nSecurityEnvironmentIndex = nSecurityEnvironmentIndex;
     239             :     }
     240           0 : }
     241             : 
     242           0 : void XSecController::setDate(
     243             :     sal_Int32 nSecurityId,
     244             :     const ::com::sun::star::util::DateTime& rDateTime )
     245             : {
     246           0 :     int index = findSignatureInfor( nSecurityId );
     247             : 
     248           0 :     if ( index == -1 )
     249             :     {
     250           0 :         InternalSignatureInformation isi(nSecurityId, NULL);
     251           0 :         isi.signatureInfor.stDateTime = rDateTime;
     252           0 :         m_vInternalSignatureInformations.push_back( isi );
     253             :     }
     254             :     else
     255             :     {
     256             :         SignatureInformation &si
     257           0 :             = m_vInternalSignatureInformations[index].signatureInfor;
     258           0 :         si.stDateTime = rDateTime;
     259             :     }
     260           0 : }
     261             : 
     262           0 : bool XSecController::WriteSignature(
     263             :     const cssu::Reference<cssxs::XDocumentHandler>& xDocumentHandler )
     264             : {
     265           0 :     bool rc = false;
     266             : 
     267             :     DBG_ASSERT( xDocumentHandler.is(), "I really need a document handler!" );
     268             : 
     269             :     /*
     270             :      * chain the SAXEventKeeper to the SAX chain
     271             :      */
     272           0 :     chainOn(true);
     273             : 
     274           0 :     if ( m_nStatusOfSecurityComponents == INITIALIZED )
     275             :     /*
     276             :      * if all security components are ready, add the signature
     277             :      * stream.
     278             :      */
     279             :     {
     280           0 :         m_bIsSAXEventKeeperSticky = true;
     281           0 :         m_xSAXEventKeeper->setNextHandler(xDocumentHandler);
     282             : 
     283             :         try
     284             :         {
     285             :             /*
     286             :              * export the signature template
     287             :              */
     288           0 :             cssu::Reference<cssxs::XDocumentHandler> xSEKHandler( m_xSAXEventKeeper,cssu::UNO_QUERY);
     289             : 
     290             :             int i;
     291           0 :             int sigNum = m_vInternalSignatureInformations.size();
     292             : 
     293           0 :             for (i=0; i<sigNum; ++i)
     294             :             {
     295           0 :                 InternalSignatureInformation &isi = m_vInternalSignatureInformations[i];
     296             : 
     297             :                 /*
     298             :                  * prepare the signature creator
     299             :                  */
     300             :                 isi.xReferenceResolvedListener
     301           0 :                     = prepareSignatureToWrite( isi );
     302             : 
     303           0 :                 exportSignature( xSEKHandler, isi.signatureInfor );
     304             :             }
     305             : 
     306           0 :             m_bIsSAXEventKeeperSticky = false;
     307           0 :             chainOff();
     308             : 
     309           0 :             rc = true;
     310             :         }
     311           0 :         catch( cssxs::SAXException& )
     312             :         {
     313           0 :             m_pErrorMessage = ERROR_SAXEXCEPTIONDURINGCREATION;
     314             :         }
     315           0 :         catch( com::sun::star::io::IOException& )
     316             :         {
     317           0 :             m_pErrorMessage = ERROR_IOEXCEPTIONDURINGCREATION;
     318             :         }
     319           0 :         catch( cssu::Exception& )
     320             :         {
     321           0 :             m_pErrorMessage = ERROR_EXCEPTIONDURINGCREATION;
     322             :         }
     323             : 
     324           0 :         m_xSAXEventKeeper->setNextHandler( NULL );
     325           0 :         m_bIsSAXEventKeeperSticky = false;
     326             :     }
     327             :     else
     328             :     {
     329           0 :         m_pErrorMessage = ERROR_CANNOTCREATEXMLSECURITYCOMPONENT;
     330             :     }
     331             : 
     332           0 :     return rc;
     333             : }
     334             : 
     335             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11