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

Generated by: LCOV version 1.11