LCOV - code coverage report
Current view: top level - xmlsecurity/source/xmlsec/nss - xmlencryption_nssimpl.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 142 0.0 %
Date: 2014-04-14 Functions: 0 12 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             : #include <sal/config.h>
      21             : #include <rtl/uuid.h>
      22             : #include "xmlencryption_nssimpl.hxx"
      23             : 
      24             : #include "xmldocumentwrapper_xmlsecimpl.hxx"
      25             : 
      26             : #include "xmlelementwrapper_xmlsecimpl.hxx"
      27             : 
      28             : #include "securityenvironment_nssimpl.hxx"
      29             : #include "errorcallback.hxx"
      30             : 
      31             : #include "xmlsecurity/xmlsec-wrapper.h"
      32             : 
      33             : using namespace ::com::sun::star::uno ;
      34             : using namespace ::com::sun::star::lang ;
      35             : using ::com::sun::star::lang::XMultiServiceFactory ;
      36             : using ::com::sun::star::lang::XSingleServiceFactory ;
      37             : 
      38             : using ::com::sun::star::xml::wrapper::XXMLElementWrapper ;
      39             : using ::com::sun::star::xml::wrapper::XXMLDocumentWrapper ;
      40             : using ::com::sun::star::xml::crypto::XSecurityEnvironment ;
      41             : using ::com::sun::star::xml::crypto::XXMLEncryption ;
      42             : using ::com::sun::star::xml::crypto::XXMLEncryptionTemplate ;
      43             : using ::com::sun::star::xml::crypto::XXMLSecurityContext ;
      44             : using ::com::sun::star::xml::crypto::XMLEncryptionException ;
      45             : 
      46           0 : XMLEncryption_NssImpl :: XMLEncryption_NssImpl( const Reference< XMultiServiceFactory >& aFactory ) : m_xServiceManager( aFactory ) {
      47           0 : }
      48             : 
      49           0 : XMLEncryption_NssImpl :: ~XMLEncryption_NssImpl() {
      50           0 : }
      51             : 
      52             : /* XXMLEncryption */
      53             : Reference< XXMLEncryptionTemplate >
      54           0 : SAL_CALL XMLEncryption_NssImpl :: encrypt(
      55             :     const Reference< XXMLEncryptionTemplate >& aTemplate ,
      56             :     const Reference< XSecurityEnvironment >& aEnvironment
      57             : ) throw( com::sun::star::xml::crypto::XMLEncryptionException,
      58             :          com::sun::star::uno::SecurityException, std::exception )
      59             : {
      60           0 :     xmlSecKeysMngrPtr pMngr = NULL ;
      61           0 :     xmlSecEncCtxPtr pEncCtx = NULL ;
      62           0 :     xmlNodePtr pEncryptedData = NULL ;
      63           0 :     xmlNodePtr pContent = NULL ;
      64             : 
      65           0 :     if( !aTemplate.is() )
      66           0 :         throw RuntimeException() ;
      67             : 
      68           0 :     if( !aEnvironment.is() )
      69           0 :         throw RuntimeException() ;
      70             : 
      71             :     //Get Keys Manager
      72           0 :     Reference< XUnoTunnel > xSecTunnel( aEnvironment , UNO_QUERY ) ;
      73           0 :     if( !xSecTunnel.is() ) {
      74           0 :          throw RuntimeException() ;
      75             :     }
      76             : 
      77             :     SecurityEnvironment_NssImpl* pSecEnv =
      78             :         reinterpret_cast<SecurityEnvironment_NssImpl*>(
      79           0 :             sal::static_int_cast<sal_uIntPtr>(xSecTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ))) ;
      80           0 :     if( pSecEnv == NULL )
      81           0 :         throw RuntimeException() ;
      82             : 
      83             :     //Get the encryption template
      84           0 :     Reference< XXMLElementWrapper > xTemplate = aTemplate->getTemplate() ;
      85           0 :     if( !xTemplate.is() ) {
      86           0 :         throw RuntimeException() ;
      87             :     }
      88             : 
      89           0 :     Reference< XUnoTunnel > xTplTunnel( xTemplate , UNO_QUERY ) ;
      90           0 :     if( !xTplTunnel.is() ) {
      91           0 :         throw RuntimeException() ;
      92             :     }
      93             : 
      94             :     XMLElementWrapper_XmlSecImpl* pTemplate =
      95             :         reinterpret_cast<XMLElementWrapper_XmlSecImpl*>(
      96             :             sal::static_int_cast<sal_uIntPtr>(
      97           0 :                 xTplTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() )));
      98           0 :     if( pTemplate == NULL ) {
      99           0 :         throw RuntimeException() ;
     100             :     }
     101             : 
     102             :     // Get the element to be encrypted
     103           0 :     Reference< XXMLElementWrapper > xTarget = aTemplate->getTarget() ;
     104           0 :     if( !xTarget.is() ) {
     105           0 :         throw XMLEncryptionException() ;
     106             :     }
     107             : 
     108           0 :     Reference< XUnoTunnel > xTgtTunnel( xTarget , UNO_QUERY ) ;
     109           0 :     if( !xTgtTunnel.is() ) {
     110           0 :         throw XMLEncryptionException() ;
     111             :     }
     112             : 
     113             :     XMLElementWrapper_XmlSecImpl* pTarget =
     114             :         reinterpret_cast<XMLElementWrapper_XmlSecImpl*>(
     115             :             sal::static_int_cast<sal_uIntPtr>(
     116           0 :                 xTgtTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() )));
     117           0 :     if( pTarget == NULL ) {
     118           0 :         throw RuntimeException() ;
     119             :     }
     120             : 
     121           0 :     pContent = pTarget->getNativeElement() ;
     122             : 
     123           0 :     if( pContent == NULL ) {
     124           0 :         throw XMLEncryptionException() ;
     125             :     }
     126             : 
     127             :     //remember the position of the element to be signed
     128           0 :     sal_Bool isParentRef = sal_True;
     129           0 :     pEncryptedData = pTemplate->getNativeElement();
     130             : 
     131           0 :     xmlNodePtr pParent = pEncryptedData->parent;
     132             :     xmlNodePtr referenceNode;
     133             : 
     134           0 :     if (pEncryptedData == pParent->children)
     135             :     {
     136           0 :         referenceNode = pParent;
     137             :     }
     138             :     else
     139             :     {
     140           0 :         referenceNode = pEncryptedData->prev;
     141           0 :         isParentRef = sal_False;
     142             :     }
     143             : 
     144           0 :      setErrorRecorder( );
     145             : 
     146           0 :     pMngr = pSecEnv->createKeysManager() ; //i39448
     147           0 :     if( !pMngr ) {
     148           0 :         throw RuntimeException() ;
     149             :     }
     150             : 
     151             :     //Create Encryption context
     152           0 :     pEncCtx = xmlSecEncCtxCreate( pMngr ) ;
     153           0 :     if( pEncCtx == NULL )
     154             :     {
     155           0 :         pSecEnv->destroyKeysManager( pMngr ) ; //i39448
     156             :         //throw XMLEncryptionException() ;
     157           0 :         clearErrorRecorder();
     158           0 :         return aTemplate;
     159             :     }
     160             : 
     161             :     //Find the element to be encrypted.
     162             : 
     163             :     //Encrypt the template
     164           0 :     if( xmlSecEncCtxXmlEncrypt( pEncCtx , pEncryptedData , pContent ) < 0 )
     165             :     {
     166           0 :         xmlSecEncCtxDestroy( pEncCtx ) ;
     167           0 :         pSecEnv->destroyKeysManager( pMngr ) ; //i39448
     168             : 
     169             :         //throw XMLEncryptionException() ;
     170           0 :         clearErrorRecorder();
     171           0 :         return aTemplate;
     172             :     }
     173             : 
     174           0 :     xmlSecEncCtxDestroy( pEncCtx ) ;
     175           0 :     pSecEnv->destroyKeysManager( pMngr ) ; //i39448
     176             : 
     177             :     //get the new EncryptedData element
     178           0 :     if (isParentRef)
     179             :     {
     180           0 :         pTemplate->setNativeElement(referenceNode->children) ;
     181             :     }
     182             :     else
     183             :     {
     184           0 :         pTemplate->setNativeElement(referenceNode->next);
     185             :     }
     186             : 
     187           0 :     return aTemplate ;
     188             : }
     189             : 
     190             : /* XXMLEncryption */
     191             : Reference< XXMLEncryptionTemplate >
     192           0 : SAL_CALL XMLEncryption_NssImpl :: decrypt(
     193             :     const Reference< XXMLEncryptionTemplate >& aTemplate ,
     194             :     const Reference< XXMLSecurityContext >& aSecurityCtx
     195             : ) throw( com::sun::star::xml::crypto::XMLEncryptionException ,
     196             :          com::sun::star::uno::SecurityException, std::exception) {
     197           0 :     xmlSecKeysMngrPtr pMngr = NULL ;
     198           0 :     xmlSecEncCtxPtr pEncCtx = NULL ;
     199           0 :     xmlNodePtr pEncryptedData = NULL ;
     200             : 
     201           0 :     if( !aTemplate.is() )
     202           0 :         throw RuntimeException() ;
     203             : 
     204           0 :     if( !aSecurityCtx.is() )
     205           0 :         throw RuntimeException() ;
     206             : 
     207             :     //Get the encryption template
     208           0 :     Reference< XXMLElementWrapper > xTemplate = aTemplate->getTemplate() ;
     209           0 :     if( !xTemplate.is() ) {
     210           0 :         throw RuntimeException() ;
     211             :     }
     212             : 
     213           0 :     Reference< XUnoTunnel > xTplTunnel( xTemplate , UNO_QUERY ) ;
     214           0 :     if( !xTplTunnel.is() ) {
     215           0 :         throw RuntimeException() ;
     216             :     }
     217             : 
     218             :     XMLElementWrapper_XmlSecImpl* pTemplate =
     219             :         reinterpret_cast<XMLElementWrapper_XmlSecImpl*>(
     220             :             sal::static_int_cast<sal_uIntPtr>(
     221           0 :                 xTplTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() )));
     222           0 :     if( pTemplate == NULL ) {
     223           0 :         throw RuntimeException() ;
     224             :     }
     225             : 
     226           0 :     pEncryptedData = pTemplate->getNativeElement() ;
     227             : 
     228             :     //remember the position of the element to be signed
     229           0 :     sal_Bool isParentRef = sal_True;
     230           0 :     xmlNodePtr pParent = pEncryptedData->parent;
     231             :     xmlNodePtr referenceNode;
     232             : 
     233           0 :     if (pEncryptedData == pParent->children)
     234             :     {
     235           0 :         referenceNode = pParent;
     236             :     }
     237             :     else
     238             :     {
     239           0 :         referenceNode = pEncryptedData->prev;
     240           0 :         isParentRef = sal_False;
     241             :     }
     242             : 
     243           0 :      setErrorRecorder( );
     244             : 
     245           0 :     sal_Int32 nSecurityEnvironment = aSecurityCtx->getSecurityEnvironmentNumber();
     246             :     sal_Int32 i;
     247             : 
     248           0 :     for (i=0; i<nSecurityEnvironment; ++i)
     249             :     {
     250           0 :         Reference< XSecurityEnvironment > aEnvironment = aSecurityCtx->getSecurityEnvironmentByIndex(i);
     251             : 
     252             :         //Get Keys Manager
     253           0 :         Reference< XUnoTunnel > xSecTunnel( aEnvironment , UNO_QUERY ) ;
     254           0 :         if( !aEnvironment.is() ) {
     255           0 :              throw RuntimeException() ;
     256             :         }
     257             : 
     258             :         SecurityEnvironment_NssImpl* pSecEnv =
     259             :             reinterpret_cast<SecurityEnvironment_NssImpl*>(
     260             :                 sal::static_int_cast<sal_uIntPtr>(
     261           0 :                     xSecTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() )));
     262           0 :         if( pSecEnv == NULL )
     263           0 :             throw RuntimeException() ;
     264             : 
     265           0 :         pMngr = pSecEnv->createKeysManager() ; //i39448
     266           0 :         if( !pMngr ) {
     267           0 :             throw RuntimeException() ;
     268             :         }
     269             : 
     270             :         //Create Encryption context
     271           0 :         pEncCtx = xmlSecEncCtxCreate( pMngr ) ;
     272           0 :         if( pEncCtx == NULL )
     273             :         {
     274           0 :             pSecEnv->destroyKeysManager( pMngr ) ; //i39448
     275             :             //throw XMLEncryptionException() ;
     276           0 :             clearErrorRecorder();
     277           0 :             return aTemplate;
     278             :         }
     279             : 
     280             :         //Decrypt the template
     281           0 :         if(!( xmlSecEncCtxDecrypt( pEncCtx , pEncryptedData ) < 0 || pEncCtx->result == NULL ))
     282             :         {
     283             :             //The decryption succeeds
     284             : 
     285             :             //Destroy the encryption context
     286           0 :             xmlSecEncCtxDestroy( pEncCtx ) ;
     287           0 :             pSecEnv->destroyKeysManager( pMngr ) ; //i39448
     288             : 
     289             :             //get the decrypted element
     290             :             XMLElementWrapper_XmlSecImpl * ret = new XMLElementWrapper_XmlSecImpl(isParentRef?
     291           0 :                 (referenceNode->children):(referenceNode->next));
     292             : 
     293             :             //return ret;
     294           0 :             aTemplate->setTemplate(ret);
     295           0 :             break;
     296             :         }
     297             :         else
     298             :         {
     299             :             //The decryption fails, continue with the next security environment
     300           0 :             xmlSecEncCtxDestroy( pEncCtx ) ;
     301           0 :             pSecEnv->destroyKeysManager( pMngr ) ; //i39448
     302             :         }
     303           0 :     }
     304             : 
     305           0 :     clearErrorRecorder();
     306           0 :     return aTemplate;
     307             : }
     308             : 
     309             : /* XServiceInfo */
     310           0 : OUString SAL_CALL XMLEncryption_NssImpl :: getImplementationName() throw( RuntimeException, std::exception ) {
     311           0 :     return impl_getImplementationName() ;
     312             : }
     313             : 
     314             : /* XServiceInfo */
     315           0 : sal_Bool SAL_CALL XMLEncryption_NssImpl :: supportsService( const OUString& serviceName) throw( RuntimeException, std::exception ) {
     316           0 :     Sequence< OUString > seqServiceNames = getSupportedServiceNames() ;
     317           0 :     const OUString* pArray = seqServiceNames.getConstArray() ;
     318           0 :     for( sal_Int32 i = 0 ; i < seqServiceNames.getLength() ; i ++ ) {
     319           0 :         if( *( pArray + i ) == serviceName )
     320           0 :             return sal_True ;
     321             :     }
     322           0 :     return sal_False ;
     323             : }
     324             : 
     325             : /* XServiceInfo */
     326           0 : Sequence< OUString > SAL_CALL XMLEncryption_NssImpl :: getSupportedServiceNames() throw( RuntimeException, std::exception ) {
     327           0 :     return impl_getSupportedServiceNames() ;
     328             : }
     329             : 
     330             : //Helper for XServiceInfo
     331           0 : Sequence< OUString > XMLEncryption_NssImpl :: impl_getSupportedServiceNames() {
     332           0 :     ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ) ;
     333           0 :     Sequence< OUString > seqServiceNames( 1 ) ;
     334           0 :     seqServiceNames[0] = "com.sun.star.xml.crypto.XMLEncryption";
     335           0 :     return seqServiceNames ;
     336             : }
     337             : 
     338           0 : OUString XMLEncryption_NssImpl :: impl_getImplementationName() throw( RuntimeException ) {
     339           0 :     return OUString("com.sun.star.xml.security.bridge.xmlsec.XMLEncryption_NssImpl") ;
     340             : }
     341             : 
     342             : //Helper for registry
     343           0 : Reference< XInterface > SAL_CALL XMLEncryption_NssImpl :: impl_createInstance( const Reference< XMultiServiceFactory >& aServiceManager ) throw( RuntimeException ) {
     344           0 :     return Reference< XInterface >( *new XMLEncryption_NssImpl( aServiceManager ) ) ;
     345             : }
     346             : 
     347           0 : Reference< XSingleServiceFactory > XMLEncryption_NssImpl :: impl_createFactory( const Reference< XMultiServiceFactory >& aServiceManager ) {
     348             :     //Reference< XSingleServiceFactory > xFactory ;
     349             :     //xFactory = ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName , impl_createInstance , impl_getSupportedServiceNames ) ;
     350             :     //return xFactory ;
     351           0 :     return ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName() , impl_createInstance , impl_getSupportedServiceNames() ) ;
     352             : }
     353             : 
     354             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10