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

Generated by: LCOV version 1.11