LCOV - code coverage report
Current view: top level - xmlsecurity/source/helper - xmlsignaturehelper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 127 0.0 %
Date: 2012-08-25 Functions: 0 26 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 165 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 <xmlsecurity/xmlsignaturehelper.hxx>
      22                 :            : #include <xmlsecurity/documentsignaturehelper.hxx>
      23                 :            : #include <xsecctl.hxx>
      24                 :            : 
      25                 :            : #include <xmlsignaturehelper2.hxx>
      26                 :            : 
      27                 :            : #include <tools/stream.hxx>
      28                 :            : #include <tools/debug.hxx>
      29                 :            : 
      30                 :            : #include <xmloff/attrlist.hxx>
      31                 :            : 
      32                 :            : #include <com/sun/star/io/XOutputStream.hpp>
      33                 :            : #include <com/sun/star/io/XInputStream.hpp>
      34                 :            : #include <com/sun/star/io/XActiveDataSource.hpp>
      35                 :            : #include <com/sun/star/lang/XComponent.hpp>
      36                 :            : #include <com/sun/star/security/SerialNumberAdapter.hpp>
      37                 :            : #include <com/sun/star/beans/XPropertySet.hpp>
      38                 :            : 
      39                 :            : #include <tools/date.hxx>
      40                 :            : #include <tools/time.hxx>
      41                 :            : 
      42                 :            : /* SEInitializer component */
      43                 :            : #define SEINITIALIZER_COMPONENT "com.sun.star.xml.crypto.SEInitializer"
      44                 :            : 
      45                 :            : #define TAG_DOCUMENTSIGNATURES  "document-signatures"
      46                 :            : #define NS_DOCUMENTSIGNATURES   "http://openoffice.org/2004/documentsignatures"
      47                 :            : #define NS_DOCUMENTSIGNATURES_ODF_1_2 "urn:oasis:names:tc:opendocument:xmlns:digitalsignature:1.0"
      48                 :            : 
      49                 :            : using namespace ::com::sun::star;
      50                 :            : using namespace ::com::sun::star::uno;
      51                 :            : 
      52                 :          0 : XMLSignatureHelper::XMLSignatureHelper( const uno::Reference< uno::XComponentContext >& rxCtx)
      53 [ #  # ][ #  # ]:          0 :     : mxCtx(rxCtx), mbODFPre1_2(false)
                 [ #  # ]
      54                 :            : {
      55         [ #  # ]:          0 :     mpXSecController = new XSecController(rxCtx);
      56 [ #  # ][ #  # ]:          0 :     mxSecurityController = mpXSecController;
      57                 :          0 :     mbError = false;
      58                 :          0 : }
      59                 :            : 
      60                 :          0 : XMLSignatureHelper::~XMLSignatureHelper()
      61                 :            : {
      62                 :          0 : }
      63                 :            : 
      64                 :          0 : bool XMLSignatureHelper::Init()
      65                 :            : {
      66                 :            :     DBG_ASSERT( !mxSEInitializer.is(), "XMLSignatureHelper::Init - mxSEInitializer already set!" );
      67                 :            :     DBG_ASSERT( !mxSecurityContext.is(), "XMLSignatureHelper::Init - mxSecurityContext already set!" );
      68                 :            : 
      69                 :          0 :     ImplCreateSEInitializer();
      70                 :            : 
      71         [ #  # ]:          0 :     if ( mxSEInitializer.is() )
      72 [ #  # ][ #  # ]:          0 :         mxSecurityContext = mxSEInitializer->createSecurityContext( ::rtl::OUString() );
      73                 :            : 
      74                 :          0 :     return mxSecurityContext.is();
      75                 :            : }
      76                 :            : 
      77                 :          0 : void XMLSignatureHelper::ImplCreateSEInitializer()
      78                 :            : {
      79                 :          0 :     rtl::OUString sSEInitializer( SEINITIALIZER_COMPONENT );
      80 [ #  # ][ #  # ]:          0 :     uno::Reference< lang::XMultiComponentFactory > xMCF( mxCtx->getServiceManager() );
      81                 :            :     mxSEInitializer = uno::Reference< com::sun::star::xml::crypto::XSEInitializer > (
      82 [ #  # ][ #  # ]:          0 :         xMCF->createInstanceWithContext( sSEInitializer,  mxCtx ), uno::UNO_QUERY );
         [ #  # ][ #  # ]
      83                 :          0 : }
      84                 :            : 
      85                 :          0 : void XMLSignatureHelper::SetStorage(
      86                 :            :     const Reference < css::embed::XStorage >& rxStorage,
      87                 :            :     ::rtl::OUString sODFVersion)
      88                 :            : {
      89                 :            :     DBG_ASSERT( !mxUriBinding.is(), "SetStorage - UriBinding already set!" );
      90 [ #  # ][ #  # ]:          0 :     mxUriBinding = new UriBindingHelper( rxStorage );
      91                 :            :     DBG_ASSERT(rxStorage.is(), "SetStorage - empty storage!");
      92                 :          0 :     mbODFPre1_2 = DocumentSignatureHelper::isODFPre_1_2(sODFVersion);
      93                 :          0 : }
      94                 :            : 
      95                 :            : 
      96                 :          0 : void XMLSignatureHelper::SetStartVerifySignatureHdl( const Link& rLink )
      97                 :            : {
      98                 :          0 :     maStartVerifySignatureHdl = rLink;
      99                 :          0 : }
     100                 :            : 
     101                 :            : 
     102                 :          0 : void XMLSignatureHelper::StartMission()
     103                 :            : {
     104         [ #  # ]:          0 :     if ( !mxUriBinding.is() )
     105 [ #  # ][ #  # ]:          0 :         mxUriBinding = new UriBindingHelper();
     106                 :            : 
     107                 :          0 :     mpXSecController->startMission( mxUriBinding, mxSecurityContext );
     108                 :          0 : }
     109                 :            : 
     110                 :          0 : void XMLSignatureHelper::EndMission()
     111                 :            : {
     112                 :          0 :     mpXSecController->endMission();
     113                 :          0 : }
     114                 :            : 
     115                 :          0 : sal_Int32 XMLSignatureHelper::GetNewSecurityId()
     116                 :            : {
     117                 :          0 :     return mpXSecController->getNewSecurityId();
     118                 :            : }
     119                 :            : 
     120                 :          0 : void XMLSignatureHelper::SetX509Certificate(
     121                 :            :         sal_Int32 nSecurityId,
     122                 :            :         const rtl::OUString& ouX509IssuerName,
     123                 :            :         const rtl::OUString& ouX509SerialNumber,
     124                 :            :         const rtl::OUString& ouX509Cert)
     125                 :            : {
     126                 :            :     mpXSecController->setX509Certificate(
     127                 :            :         nSecurityId,
     128                 :            :         ouX509IssuerName,
     129                 :            :         ouX509SerialNumber,
     130                 :          0 :         ouX509Cert);
     131                 :          0 : }
     132                 :            : 
     133                 :          0 : void XMLSignatureHelper::SetDateTime( sal_Int32 nSecurityId, const Date& rDate, const Time& rTime )
     134                 :            : {
     135                 :          0 :     ::com::sun::star::util::DateTime stDateTime;
     136                 :          0 :     stDateTime.HundredthSeconds = (::sal_uInt16)rTime.Get100Sec();
     137                 :          0 :     stDateTime.Seconds = (::sal_uInt16)rTime.GetSec();
     138                 :          0 :     stDateTime.Minutes = (::sal_uInt16)rTime.GetMin();
     139                 :          0 :     stDateTime.Hours = (::sal_uInt16)rTime.GetHour();
     140                 :          0 :     stDateTime.Day = (::sal_uInt16)rDate.GetDay();
     141                 :          0 :     stDateTime.Month = (::sal_uInt16)rDate.GetMonth();
     142                 :          0 :     stDateTime.Year = (::sal_uInt16)rDate.GetYear();
     143         [ #  # ]:          0 :     mpXSecController->setDate( nSecurityId, stDateTime );
     144                 :          0 : }
     145                 :            : 
     146                 :          0 : void XMLSignatureHelper::AddForSigning( sal_Int32 nSecurityId, const rtl::OUString& uri, const rtl::OUString& objectURL, sal_Bool bBinary )
     147                 :            : {
     148                 :          0 :     mpXSecController->signAStream( nSecurityId, uri, objectURL, bBinary );
     149                 :          0 : }
     150                 :            : 
     151                 :            : 
     152                 :          0 : uno::Reference<xml::sax::XDocumentHandler> XMLSignatureHelper::CreateDocumentHandlerWithHeader(
     153                 :            :     const com::sun::star::uno::Reference< com::sun::star::io::XOutputStream >& xOutputStream )
     154                 :            : {
     155                 :            :     /*
     156                 :            :      * get SAX writer component
     157                 :            :      */
     158 [ #  # ][ #  # ]:          0 :     uno::Reference< lang::XMultiComponentFactory > xMCF( mxCtx->getServiceManager() );
     159                 :            :     uno::Reference< io::XActiveDataSource > xSaxWriter(
     160         [ #  # ]:          0 :         xMCF->createInstanceWithContext(rtl::OUString(
     161 [ #  # ][ #  # ]:          0 :             "com.sun.star.xml.sax.Writer"), mxCtx ), uno::UNO_QUERY );
     162                 :            : 
     163                 :            :     DBG_ASSERT( xSaxWriter.is(), "can't instantiate XML writer" );
     164                 :            : 
     165                 :            :     /*
     166                 :            :      * connect XML writer to output stream
     167                 :            :      */
     168 [ #  # ][ #  # ]:          0 :     xSaxWriter->setOutputStream( xOutputStream );
     169                 :            : 
     170                 :            :     /*
     171                 :            :      * prepare document handler
     172                 :            :      */
     173                 :            :     uno::Reference<xml::sax::XDocumentHandler>
     174         [ #  # ]:          0 :         xDocHandler( xSaxWriter,uno::UNO_QUERY);
     175                 :            : 
     176                 :            :     /*
     177                 :            :      * write the xml context for signatures
     178                 :            :      */
     179                 :          0 :     rtl::OUString tag_AllSignatures(TAG_DOCUMENTSIGNATURES);
     180                 :            : 
     181         [ #  # ]:          0 :     SvXMLAttributeList *pAttributeList = new SvXMLAttributeList();
     182                 :          0 :     rtl::OUString sNamespace;
     183         [ #  # ]:          0 :     if (mbODFPre1_2)
     184                 :          0 :         sNamespace = rtl::OUString(NS_DOCUMENTSIGNATURES);
     185                 :            :     else
     186                 :          0 :         sNamespace = rtl::OUString(NS_DOCUMENTSIGNATURES_ODF_1_2);
     187                 :            : 
     188                 :            :     pAttributeList->AddAttribute(
     189                 :            :         rtl::OUString(ATTR_XMLNS),
     190         [ #  # ]:          0 :         sNamespace);
     191                 :            : 
     192 [ #  # ][ #  # ]:          0 :     xDocHandler->startDocument();
     193         [ #  # ]:          0 :     xDocHandler->startElement(
     194                 :            :         tag_AllSignatures,
     195 [ #  # ][ #  # ]:          0 :         uno::Reference< com::sun::star::xml::sax::XAttributeList > (pAttributeList));
                 [ #  # ]
     196                 :            : 
     197                 :          0 :     return xDocHandler;
     198                 :            : }
     199                 :            : 
     200                 :          0 : void XMLSignatureHelper::CloseDocumentHandler( const uno::Reference<xml::sax::XDocumentHandler>& xDocumentHandler )
     201                 :            : {
     202                 :          0 :     rtl::OUString tag_AllSignatures(TAG_DOCUMENTSIGNATURES);
     203 [ #  # ][ #  # ]:          0 :     xDocumentHandler->endElement( tag_AllSignatures );
     204 [ #  # ][ #  # ]:          0 :     xDocumentHandler->endDocument();
     205                 :          0 : }
     206                 :            : 
     207                 :          0 : void XMLSignatureHelper::ExportSignature(
     208                 :            :     const uno::Reference< xml::sax::XDocumentHandler >& xDocumentHandler,
     209                 :            :     const SignatureInformation& signatureInfo )
     210                 :            : {
     211                 :          0 :     mpXSecController->exportSignature(xDocumentHandler, signatureInfo);
     212                 :          0 : }
     213                 :            : 
     214                 :          0 : bool XMLSignatureHelper::CreateAndWriteSignature( const uno::Reference< xml::sax::XDocumentHandler >& xDocumentHandler )
     215                 :            : {
     216                 :          0 :     mbError = false;
     217                 :            : 
     218                 :            :     /*
     219                 :            :      * create a signature listener
     220                 :            :      */
     221                 :            : 
     222                 :            :     /*
     223                 :            :      * configure the signature creation listener
     224                 :            :      */
     225                 :            : 
     226                 :            :     /*
     227                 :            :      * write signatures
     228                 :            :      */
     229         [ #  # ]:          0 :     if ( !mpXSecController->WriteSignature( xDocumentHandler ) )
     230                 :            :     {
     231                 :          0 :         mbError = true;
     232                 :            :     }
     233                 :            : 
     234                 :            :     /*
     235                 :            :      * clear up the signature creation listener
     236                 :            :      */
     237                 :            : 
     238                 :          0 :     return !mbError;
     239                 :            : }
     240                 :            : 
     241                 :          0 : bool XMLSignatureHelper::ReadAndVerifySignature( const com::sun::star::uno::Reference< com::sun::star::io::XInputStream >& xInputStream )
     242                 :            : {
     243                 :          0 :     mbError = false;
     244                 :            : 
     245                 :            :     DBG_ASSERT(xInputStream.is(), "input stream missing");
     246                 :            : 
     247                 :            :     /*
     248                 :            :      * prepare ParserInputSrouce
     249                 :            :      */
     250         [ #  # ]:          0 :     xml::sax::InputSource aParserInput;
     251         [ #  # ]:          0 :     aParserInput.aInputStream = xInputStream;
     252                 :            : 
     253                 :            :     /*
     254                 :            :      * get SAX parser component
     255                 :            :      */
     256 [ #  # ][ #  # ]:          0 :     uno::Reference< lang::XMultiComponentFactory > xMCF( mxCtx->getServiceManager() );
     257                 :            :     uno::Reference< xml::sax::XParser > xParser(
     258         [ #  # ]:          0 :         xMCF->createInstanceWithContext(
     259                 :          0 :             rtl::OUString("com.sun.star.xml.sax.Parser" ), mxCtx ),
     260 [ #  # ][ #  # ]:          0 :         uno::UNO_QUERY );
     261                 :            : 
     262                 :            :     DBG_ASSERT( xParser.is(), "Can't create parser" );
     263                 :            : 
     264                 :            :     /*
     265                 :            :      * create a signature reader
     266                 :            :      */
     267                 :            :     uno::Reference< xml::sax::XDocumentHandler > xHandler
     268         [ #  # ]:          0 :         = mpXSecController->createSignatureReader( );
     269                 :            : 
     270                 :            :     /*
     271                 :            :      * create a signature listener
     272                 :            :      */
     273                 :            :     ImplXMLSignatureListener* pSignatureListener = new ImplXMLSignatureListener(
     274                 :            :                                                     LINK( this, XMLSignatureHelper, SignatureCreationResultListener ),
     275                 :            :                                                     LINK( this, XMLSignatureHelper, SignatureVerifyResultListener ),
     276 [ #  # ][ #  # ]:          0 :                                                     LINK( this, XMLSignatureHelper, StartVerifySignatureElement ) );
         [ #  # ][ #  # ]
     277                 :            : 
     278                 :            :     /*
     279                 :            :      * configure the signature verify listener
     280                 :            :      */
     281                 :            : 
     282                 :            :     /*
     283                 :            :      * setup the connection:
     284                 :            :      * Parser -> SignatureListener -> SignatureReader
     285                 :            :      */
     286         [ #  # ]:          0 :     pSignatureListener->setNextHandler(xHandler);
     287 [ #  # ][ #  # ]:          0 :     xParser->setDocumentHandler( pSignatureListener );
         [ #  # ][ #  #  
                #  #  # ]
                 [ #  # ]
     288                 :            : 
     289                 :            :     /*
     290                 :            :      * parser the stream
     291                 :            :      */
     292                 :            :     try
     293                 :            :     {
     294 [ #  # ][ #  # ]:          0 :         xParser->parseStream( aParserInput );
     295                 :            :     }
     296         [ #  # ]:          0 :     catch( xml::sax::SAXParseException& )
     297                 :            :     {
     298                 :          0 :         mbError = true;
     299                 :            :     }
     300         [ #  # ]:          0 :     catch( xml::sax::SAXException& )
     301                 :            :     {
     302                 :          0 :         mbError = true;
     303                 :            :     }
     304         [ #  # ]:          0 :     catch( com::sun::star::io::IOException& )
     305                 :            :     {
     306                 :          0 :         mbError = true;
     307                 :            :     }
     308         [ #  # ]:          0 :     catch( uno::Exception& )
     309                 :            :     {
     310                 :          0 :         mbError = true;
     311                 :            :     }
     312                 :            : 
     313                 :            :     /*
     314                 :            :      * clear up the connection
     315                 :            :      */
     316 [ #  # ][ #  # ]:          0 :     pSignatureListener->setNextHandler( NULL );
     317                 :            : 
     318                 :            :     /*
     319                 :            :      * clear up the signature verify listener
     320                 :            :      */
     321                 :            : 
     322                 :            :     /*
     323                 :            :      * release the signature reader
     324                 :            :      */
     325         [ #  # ]:          0 :     mpXSecController->releaseSignatureReader( );
     326                 :            : 
     327         [ #  # ]:          0 :     return !mbError;
     328                 :            : }
     329                 :            : 
     330                 :          0 : SignatureInformation XMLSignatureHelper::GetSignatureInformation( sal_Int32 nSecurityId ) const
     331                 :            : {
     332                 :          0 :     return mpXSecController->getSignatureInformation( nSecurityId );
     333                 :            : }
     334                 :            : 
     335                 :          0 : SignatureInformations XMLSignatureHelper::GetSignatureInformations() const
     336                 :            : {
     337                 :          0 :     return mpXSecController->getSignatureInformations();
     338                 :            : }
     339                 :            : 
     340                 :          0 : uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > XMLSignatureHelper::GetSecurityEnvironment()
     341                 :            : {
     342         [ #  # ]:          0 :     return (mxSecurityContext.is()?(mxSecurityContext->getSecurityEnvironment()): uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment >());
     343                 :            : }
     344                 :            : 
     345                 :          0 : IMPL_LINK( XMLSignatureHelper, SignatureCreationResultListener, XMLSignatureCreationResult*, pResult )
     346                 :            : {
     347 [ #  # ][ #  # ]:          0 :     maCreationResults.insert( maCreationResults.begin() + maCreationResults.size(), *pResult );
     348         [ #  # ]:          0 :     if ( pResult->nSignatureCreationResult != com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED )
     349                 :          0 :         mbError = true;
     350                 :          0 :     return 0;
     351                 :            : }
     352                 :            : 
     353                 :          0 : IMPL_LINK( XMLSignatureHelper, SignatureVerifyResultListener, XMLSignatureVerifyResult*, pResult )
     354                 :            : {
     355 [ #  # ][ #  # ]:          0 :     maVerifyResults.insert( maVerifyResults.begin() + maVerifyResults.size(), *pResult );
     356         [ #  # ]:          0 :     if ( pResult->nSignatureVerifyResult != com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED )
     357                 :          0 :         mbError = true;
     358                 :          0 :     return 0;
     359                 :            : }
     360                 :            : 
     361                 :          0 : IMPL_LINK( XMLSignatureHelper, StartVerifySignatureElement, const uno::Reference< com::sun::star::xml::sax::XAttributeList >*, pAttrs )
     362                 :            : {
     363 [ #  # ][ #  # ]:          0 :     if ( !maStartVerifySignatureHdl.IsSet() || maStartVerifySignatureHdl.Call( (void*)pAttrs ) )
                 [ #  # ]
     364                 :            :     {
     365                 :          0 :         sal_Int32 nSignatureId = mpXSecController->getNewSecurityId();
     366                 :          0 :         mpXSecController->addSignature( nSignatureId );
     367                 :            :     }
     368                 :            : 
     369                 :          0 :     return 0;
     370                 :            : }
     371                 :            : 
     372                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10