LCOV - code coverage report
Current view: top level - xmlsecurity/source/xmlsec/nss - xmlsignature_nssimpl.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 2 129 1.6 %
Date: 2014-04-11 Functions: 1 12 8.3 %
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             : #include <sal/config.h>
      21             : #include <rtl/uuid.h>
      22             : #include "xmlsignature_nssimpl.hxx"
      23             : 
      24             : #include "xmldocumentwrapper_xmlsecimpl.hxx"
      25             : 
      26             : #include "xmlelementwrapper_xmlsecimpl.hxx"
      27             : 
      28             : #include "securityenvironment_nssimpl.hxx"
      29             : 
      30             : #include "xmlsecuritycontext_nssimpl.hxx"
      31             : #include "xmlstreamio.hxx"
      32             : #include "errorcallback.hxx"
      33             : 
      34             : #include "xmlsecurity/xmlsec-wrapper.h"
      35             : 
      36             : using namespace ::com::sun::star::uno ;
      37             : using namespace ::com::sun::star::lang ;
      38             : using ::com::sun::star::lang::XMultiServiceFactory ;
      39             : using ::com::sun::star::lang::XSingleServiceFactory ;
      40             : 
      41             : using ::com::sun::star::xml::wrapper::XXMLElementWrapper ;
      42             : using ::com::sun::star::xml::wrapper::XXMLDocumentWrapper ;
      43             : using ::com::sun::star::xml::crypto::XSecurityEnvironment ;
      44             : using ::com::sun::star::xml::crypto::XXMLSignature ;
      45             : using ::com::sun::star::xml::crypto::XXMLSignatureTemplate ;
      46             : using ::com::sun::star::xml::crypto::XXMLSecurityContext ;
      47             : using ::com::sun::star::xml::crypto::XUriBinding ;
      48             : using ::com::sun::star::xml::crypto::XMLSignatureException ;
      49             : 
      50           0 : XMLSignature_NssImpl :: XMLSignature_NssImpl( const Reference< XMultiServiceFactory >& aFactory ) : m_xServiceManager( aFactory ) {
      51           0 : }
      52             : 
      53           0 : XMLSignature_NssImpl :: ~XMLSignature_NssImpl() {
      54           0 : }
      55             : 
      56             : /* XXMLSignature */
      57             : Reference< XXMLSignatureTemplate >
      58           0 : SAL_CALL XMLSignature_NssImpl :: generate(
      59             :     const Reference< XXMLSignatureTemplate >& aTemplate ,
      60             :     const Reference< XSecurityEnvironment >& aEnvironment
      61             : ) throw( com::sun::star::xml::crypto::XMLSignatureException,
      62             :          com::sun::star::uno::SecurityException, std::exception )
      63             : {
      64           0 :     xmlSecKeysMngrPtr pMngr = NULL ;
      65           0 :     xmlSecDSigCtxPtr pDsigCtx = NULL ;
      66           0 :     xmlNodePtr pNode = NULL ;
      67             : 
      68           0 :     if( !aTemplate.is() )
      69           0 :         throw RuntimeException() ;
      70             : 
      71           0 :     if( !aEnvironment.is() )
      72           0 :         throw RuntimeException() ;
      73             : 
      74             :     //Get the xml node
      75           0 :     Reference< XXMLElementWrapper > xElement = aTemplate->getTemplate() ;
      76           0 :     if( !xElement.is() ) {
      77           0 :         throw RuntimeException() ;
      78             :     }
      79             : 
      80           0 :     Reference< XUnoTunnel > xNodTunnel( xElement , UNO_QUERY ) ;
      81           0 :     if( !xNodTunnel.is() ) {
      82           0 :         throw RuntimeException() ;
      83             :     }
      84             : 
      85             :     XMLElementWrapper_XmlSecImpl* pElement =
      86             :         reinterpret_cast<XMLElementWrapper_XmlSecImpl*>(
      87             :             sal::static_int_cast<sal_uIntPtr>(
      88           0 :                 xNodTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() )));
      89           0 :     if( pElement == NULL ) {
      90           0 :         throw RuntimeException() ;
      91             :     }
      92             : 
      93           0 :     pNode = pElement->getNativeElement() ;
      94             : 
      95             :     //Get the stream/URI binding
      96           0 :     Reference< XUriBinding > xUriBinding = aTemplate->getBinding() ;
      97           0 :     if( xUriBinding.is() ) {
      98             :         //Register the stream input callbacks into libxml2
      99           0 :         if( xmlRegisterStreamInputCallbacks( xUriBinding ) < 0 )
     100           0 :             throw RuntimeException() ;
     101             :     }
     102             : 
     103             :     //Get Keys Manager
     104           0 :     Reference< XUnoTunnel > xSecTunnel( aEnvironment , UNO_QUERY ) ;
     105           0 :     if( !xSecTunnel.is() ) {
     106           0 :          throw RuntimeException() ;
     107             :     }
     108             : 
     109             :     //i39448 : the key manager should be retrieved from SecurityEnvironment, instead of SecurityContext
     110             : 
     111             :     SecurityEnvironment_NssImpl* pSecEnv =
     112             :         reinterpret_cast<SecurityEnvironment_NssImpl*>(
     113             :             sal::static_int_cast<sal_uIntPtr>(
     114           0 :                 xSecTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() )));
     115           0 :     if( pSecEnv == NULL )
     116           0 :         throw RuntimeException() ;
     117             : 
     118           0 :      setErrorRecorder();
     119             : 
     120           0 :     pMngr = pSecEnv->createKeysManager() ; //i39448
     121           0 :     if( !pMngr ) {
     122           0 :         throw RuntimeException() ;
     123             :     }
     124             : 
     125             :     //Create Signature context
     126           0 :     pDsigCtx = xmlSecDSigCtxCreate( pMngr ) ;
     127           0 :     if( pDsigCtx == NULL )
     128             :     {
     129           0 :         pSecEnv->destroyKeysManager( pMngr ) ; //i39448
     130             :         //throw XMLSignatureException() ;
     131           0 :         clearErrorRecorder();
     132           0 :         return aTemplate;
     133             :     }
     134             : 
     135             :     //Sign the template
     136           0 :     if( xmlSecDSigCtxSign( pDsigCtx , pNode ) == 0 )
     137             :     {
     138           0 :         if (pDsigCtx->status == xmlSecDSigStatusSucceeded)
     139           0 :             aTemplate->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED);
     140             :         else
     141           0 :             aTemplate->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_UNKNOWN);
     142             :     }
     143             :     else
     144             :     {
     145           0 :         aTemplate->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_UNKNOWN);
     146             :     }
     147             : 
     148             : 
     149           0 :     xmlSecDSigCtxDestroy( pDsigCtx ) ;
     150           0 :     pSecEnv->destroyKeysManager( pMngr ) ; //i39448
     151             : 
     152             :     //Unregistered the stream/URI binding
     153           0 :     if( xUriBinding.is() )
     154           0 :         xmlUnregisterStreamInputCallbacks() ;
     155             : 
     156           0 :     clearErrorRecorder();
     157           0 :     return aTemplate ;
     158             : }
     159             : 
     160             : /* XXMLSignature */
     161             : Reference< XXMLSignatureTemplate >
     162           0 : SAL_CALL XMLSignature_NssImpl :: validate(
     163             :     const Reference< XXMLSignatureTemplate >& aTemplate ,
     164             :     const Reference< XXMLSecurityContext >& aSecurityCtx
     165             : ) throw( com::sun::star::uno::RuntimeException,
     166             :          com::sun::star::uno::SecurityException,
     167             :          com::sun::star::xml::crypto::XMLSignatureException, std::exception ) {
     168           0 :     xmlSecKeysMngrPtr pMngr = NULL ;
     169           0 :     xmlSecDSigCtxPtr pDsigCtx = NULL ;
     170           0 :     xmlNodePtr pNode = NULL ;
     171             :     //sal_Bool valid ;
     172             : 
     173           0 :     if( !aTemplate.is() )
     174           0 :         throw RuntimeException() ;
     175             : 
     176           0 :     if( !aSecurityCtx.is() )
     177           0 :         throw RuntimeException() ;
     178             : 
     179             :     //Get the xml node
     180           0 :     Reference< XXMLElementWrapper > xElement = aTemplate->getTemplate() ;
     181           0 :     if( !xElement.is() )
     182           0 :         throw RuntimeException() ;
     183             : 
     184           0 :     Reference< XUnoTunnel > xNodTunnel( xElement , UNO_QUERY ) ;
     185           0 :     if( !xNodTunnel.is() ) {
     186           0 :         throw RuntimeException() ;
     187             :     }
     188             : 
     189             :     XMLElementWrapper_XmlSecImpl* pElement =
     190             :         reinterpret_cast<XMLElementWrapper_XmlSecImpl*>(
     191             :             sal::static_int_cast<sal_uIntPtr>(
     192           0 :                 xNodTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() )));
     193           0 :     if( pElement == NULL )
     194           0 :         throw RuntimeException() ;
     195             : 
     196           0 :     pNode = pElement->getNativeElement() ;
     197             : 
     198             :     //Get the stream/URI binding
     199           0 :     Reference< XUriBinding > xUriBinding = aTemplate->getBinding() ;
     200           0 :     if( xUriBinding.is() ) {
     201             :         //Register the stream input callbacks into libxml2
     202           0 :         if( xmlRegisterStreamInputCallbacks( xUriBinding ) < 0 )
     203           0 :             throw RuntimeException() ;
     204             :     }
     205             : 
     206           0 :      setErrorRecorder();
     207             : 
     208           0 :     sal_Int32 nSecurityEnvironment = aSecurityCtx->getSecurityEnvironmentNumber();
     209             :     sal_Int32 i;
     210             : 
     211           0 :     for (i=0; i<nSecurityEnvironment; ++i)
     212             :     {
     213           0 :         Reference< XSecurityEnvironment > aEnvironment = aSecurityCtx->getSecurityEnvironmentByIndex(i);
     214             : 
     215             :         //Get Keys Manager
     216           0 :         Reference< XUnoTunnel > xSecTunnel( aEnvironment , UNO_QUERY ) ;
     217           0 :         if( !xSecTunnel.is() ) {
     218           0 :              throw RuntimeException() ;
     219             :         }
     220             : 
     221             :         SecurityEnvironment_NssImpl* pSecEnv =
     222             :             reinterpret_cast<SecurityEnvironment_NssImpl*>(
     223             :                 sal::static_int_cast<sal_uIntPtr>(
     224           0 :                     xSecTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() )));
     225           0 :         if( pSecEnv == NULL )
     226           0 :             throw RuntimeException() ;
     227             : 
     228           0 :         pMngr = pSecEnv->createKeysManager() ; //i39448
     229           0 :         if( !pMngr ) {
     230           0 :             throw RuntimeException() ;
     231             :         }
     232             : 
     233             :         //Create Signature context
     234           0 :         pDsigCtx = xmlSecDSigCtxCreate( pMngr ) ;
     235           0 :         if( pDsigCtx == NULL )
     236             :         {
     237           0 :             pSecEnv->destroyKeysManager( pMngr ) ; //i39448
     238             :             //throw XMLSignatureException() ;
     239           0 :             clearErrorRecorder();
     240           0 :             return aTemplate;
     241             :         }
     242             : 
     243             :         //Verify signature
     244           0 :         int rs = xmlSecDSigCtxVerify( pDsigCtx , pNode );
     245             : 
     246             : 
     247           0 :         if (rs == 0 &&
     248           0 :             pDsigCtx->status == xmlSecDSigStatusSucceeded)
     249             :         {
     250           0 :             aTemplate->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED);
     251           0 :             xmlSecDSigCtxDestroy( pDsigCtx ) ;
     252           0 :             pSecEnv->destroyKeysManager( pMngr );
     253           0 :             break;
     254             :         }
     255             :         else
     256             :         {
     257           0 :             aTemplate->setStatus(com::sun::star::xml::crypto::SecurityOperationStatus_UNKNOWN);
     258             :         }
     259           0 :         xmlSecDSigCtxDestroy( pDsigCtx ) ;
     260           0 :         pSecEnv->destroyKeysManager( pMngr );
     261           0 :     }
     262             : 
     263             : 
     264             : 
     265             :     //Unregistered the stream/URI binding
     266           0 :     if( xUriBinding.is() )
     267           0 :         xmlUnregisterStreamInputCallbacks() ;
     268             : 
     269             :     //return valid ;
     270           0 :     clearErrorRecorder();
     271           0 :     return aTemplate;
     272             : }
     273             : 
     274             : /* XServiceInfo */
     275           0 : OUString SAL_CALL XMLSignature_NssImpl :: getImplementationName() throw( RuntimeException, std::exception ) {
     276           0 :     return impl_getImplementationName() ;
     277             : }
     278             : 
     279             : /* XServiceInfo */
     280           0 : sal_Bool SAL_CALL XMLSignature_NssImpl :: supportsService( const OUString& serviceName) throw( RuntimeException, std::exception ) {
     281           0 :     Sequence< OUString > seqServiceNames = getSupportedServiceNames() ;
     282           0 :     const OUString* pArray = seqServiceNames.getConstArray() ;
     283           0 :     for( sal_Int32 i = 0 ; i < seqServiceNames.getLength() ; i ++ ) {
     284           0 :         if( *( pArray + i ) == serviceName )
     285           0 :             return sal_True ;
     286             :     }
     287           0 :     return sal_False ;
     288             : }
     289             : 
     290             : /* XServiceInfo */
     291           0 : Sequence< OUString > SAL_CALL XMLSignature_NssImpl :: getSupportedServiceNames() throw( RuntimeException, std::exception ) {
     292           0 :     return impl_getSupportedServiceNames() ;
     293             : }
     294             : 
     295             : //Helper for XServiceInfo
     296           0 : Sequence< OUString > XMLSignature_NssImpl :: impl_getSupportedServiceNames() {
     297           0 :     ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ) ;
     298           0 :     Sequence< OUString > seqServiceNames( 1 ) ;
     299           0 :     seqServiceNames[0] = "com.sun.star.xml.crypto.XMLSignature";
     300           0 :     return seqServiceNames ;
     301             : }
     302             : 
     303           2 : OUString XMLSignature_NssImpl :: impl_getImplementationName() throw( RuntimeException ) {
     304           2 :     return OUString("com.sun.star.xml.security.bridge.xmlsec.XMLSignature_NssImpl") ;
     305             : }
     306             : 
     307             : //Helper for registry
     308           0 : Reference< XInterface > SAL_CALL XMLSignature_NssImpl :: impl_createInstance( const Reference< XMultiServiceFactory >& aServiceManager ) throw( RuntimeException ) {
     309           0 :     return Reference< XInterface >( *new XMLSignature_NssImpl( aServiceManager ) ) ;
     310             : }
     311             : 
     312           0 : Reference< XSingleServiceFactory > XMLSignature_NssImpl :: impl_createFactory( const Reference< XMultiServiceFactory >& aServiceManager ) {
     313             :     //Reference< XSingleServiceFactory > xFactory ;
     314             :     //xFactory = ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName , impl_createInstance , impl_getSupportedServiceNames ) ;
     315             :     //return xFactory ;
     316           0 :     return ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName() , impl_createInstance , impl_getSupportedServiceNames() ) ;
     317             : }
     318             : 
     319             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10