1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_xmlsecurity.hxx"
30*cdf0e10cSrcweir #include <sal/config.h>
31*cdf0e10cSrcweir #include <rtl/uuid.h>
32*cdf0e10cSrcweir #include "xmlencryption_nssimpl.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #ifndef _XMLDOCUMENTWRAPPER_XMLSECIMPL_HXX_
35*cdf0e10cSrcweir #include "xmldocumentwrapper_xmlsecimpl.hxx"
36*cdf0e10cSrcweir #endif
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #ifndef _XMLELEMENTWRAPPER_XMLSECIMPL_HXX_
39*cdf0e10cSrcweir #include "xmlelementwrapper_xmlsecimpl.hxx"
40*cdf0e10cSrcweir #endif
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #ifndef _SECURITYENVIRONMENT_NSSIMPL_HXX_
43*cdf0e10cSrcweir #include "securityenvironment_nssimpl.hxx"
44*cdf0e10cSrcweir #endif
45*cdf0e10cSrcweir #include "errorcallback.hxx"
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #include <sal/types.h>
48*cdf0e10cSrcweir //For reasons that escape me, this is what xmlsec does when size_t is not 4
49*cdf0e10cSrcweir #if SAL_TYPES_SIZEOFPOINTER != 4
50*cdf0e10cSrcweir #    define XMLSEC_NO_SIZE_T
51*cdf0e10cSrcweir #endif
52*cdf0e10cSrcweir #include "xmlsec/xmlsec.h"
53*cdf0e10cSrcweir #include "xmlsec/xmltree.h"
54*cdf0e10cSrcweir #include "xmlsec/xmlenc.h"
55*cdf0e10cSrcweir #include "xmlsec/crypto.h"
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir #ifdef UNX
58*cdf0e10cSrcweir #define stricmp strcasecmp
59*cdf0e10cSrcweir #endif
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir using namespace ::com::sun::star::uno ;
62*cdf0e10cSrcweir using namespace ::com::sun::star::lang ;
63*cdf0e10cSrcweir using ::com::sun::star::lang::XMultiServiceFactory ;
64*cdf0e10cSrcweir using ::com::sun::star::lang::XSingleServiceFactory ;
65*cdf0e10cSrcweir using ::rtl::OUString ;
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir using ::com::sun::star::xml::wrapper::XXMLElementWrapper ;
68*cdf0e10cSrcweir using ::com::sun::star::xml::wrapper::XXMLDocumentWrapper ;
69*cdf0e10cSrcweir using ::com::sun::star::xml::crypto::XSecurityEnvironment ;
70*cdf0e10cSrcweir using ::com::sun::star::xml::crypto::XXMLEncryption ;
71*cdf0e10cSrcweir using ::com::sun::star::xml::crypto::XXMLEncryptionTemplate ;
72*cdf0e10cSrcweir using ::com::sun::star::xml::crypto::XXMLSecurityContext ;
73*cdf0e10cSrcweir using ::com::sun::star::xml::crypto::XSecurityEnvironment ;
74*cdf0e10cSrcweir using ::com::sun::star::xml::crypto::XMLEncryptionException ;
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir XMLEncryption_NssImpl :: XMLEncryption_NssImpl( const Reference< XMultiServiceFactory >& aFactory ) : m_xServiceManager( aFactory ) {
77*cdf0e10cSrcweir }
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir XMLEncryption_NssImpl :: ~XMLEncryption_NssImpl() {
80*cdf0e10cSrcweir }
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir /* XXMLEncryption */
83*cdf0e10cSrcweir Reference< XXMLEncryptionTemplate >
84*cdf0e10cSrcweir SAL_CALL XMLEncryption_NssImpl :: encrypt(
85*cdf0e10cSrcweir 	const Reference< XXMLEncryptionTemplate >& aTemplate ,
86*cdf0e10cSrcweir 	const Reference< XSecurityEnvironment >& aEnvironment
87*cdf0e10cSrcweir ) throw( com::sun::star::xml::crypto::XMLEncryptionException,
88*cdf0e10cSrcweir 		 com::sun::star::uno::SecurityException )
89*cdf0e10cSrcweir {
90*cdf0e10cSrcweir 	xmlSecKeysMngrPtr pMngr = NULL ;
91*cdf0e10cSrcweir 	xmlSecEncCtxPtr pEncCtx = NULL ;
92*cdf0e10cSrcweir 	xmlNodePtr pEncryptedData = NULL ;
93*cdf0e10cSrcweir 	xmlNodePtr pContent = NULL ;
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir 	if( !aTemplate.is() )
96*cdf0e10cSrcweir 		throw RuntimeException() ;
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 	if( !aEnvironment.is() )
99*cdf0e10cSrcweir 		throw RuntimeException() ;
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir 	//Get Keys Manager
102*cdf0e10cSrcweir 	Reference< XUnoTunnel > xSecTunnel( aEnvironment , UNO_QUERY ) ;
103*cdf0e10cSrcweir 	if( !xSecTunnel.is() ) {
104*cdf0e10cSrcweir 		 throw RuntimeException() ;
105*cdf0e10cSrcweir 	}
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir #if 0
108*cdf0e10cSrcweir 	XMLSecurityContext_NssImpl* pSecCtxt = ( XMLSecurityContext_NssImpl* )xSecTunnel->getSomething( XMLSecurityContext_NssImpl::getUnoTunnelId() ) ;
109*cdf0e10cSrcweir 	if( pSecCtxt == NULL )
110*cdf0e10cSrcweir 		throw RuntimeException() ;
111*cdf0e10cSrcweir #endif
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 	SecurityEnvironment_NssImpl* pSecEnv =
114*cdf0e10cSrcweir         reinterpret_cast<SecurityEnvironment_NssImpl*>(
115*cdf0e10cSrcweir             sal::static_int_cast<sal_uIntPtr>(xSecTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ))) ;
116*cdf0e10cSrcweir 	if( pSecEnv == NULL )
117*cdf0e10cSrcweir 		throw RuntimeException() ;
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 	//Get the encryption template
120*cdf0e10cSrcweir 	Reference< XXMLElementWrapper > xTemplate = aTemplate->getTemplate() ;
121*cdf0e10cSrcweir 	if( !xTemplate.is() ) {
122*cdf0e10cSrcweir 		throw RuntimeException() ;
123*cdf0e10cSrcweir 	}
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 	Reference< XUnoTunnel > xTplTunnel( xTemplate , UNO_QUERY ) ;
126*cdf0e10cSrcweir 	if( !xTplTunnel.is() ) {
127*cdf0e10cSrcweir 		throw RuntimeException() ;
128*cdf0e10cSrcweir 	}
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 	XMLElementWrapper_XmlSecImpl* pTemplate =
131*cdf0e10cSrcweir         reinterpret_cast<XMLElementWrapper_XmlSecImpl*>(
132*cdf0e10cSrcweir             sal::static_int_cast<sal_uIntPtr>(
133*cdf0e10cSrcweir                 xTplTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() )));
134*cdf0e10cSrcweir 	if( pTemplate == NULL ) {
135*cdf0e10cSrcweir 		throw RuntimeException() ;
136*cdf0e10cSrcweir 	}
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir 	//MM : Get the element to be encrypted
139*cdf0e10cSrcweir 	Reference< XXMLElementWrapper > xTarget = aTemplate->getTarget() ;
140*cdf0e10cSrcweir 	if( !xTarget.is() ) {
141*cdf0e10cSrcweir 		throw XMLEncryptionException() ;
142*cdf0e10cSrcweir 	}
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir 	Reference< XUnoTunnel > xTgtTunnel( xTarget , UNO_QUERY ) ;
145*cdf0e10cSrcweir 	if( !xTgtTunnel.is() ) {
146*cdf0e10cSrcweir 		throw XMLEncryptionException() ;
147*cdf0e10cSrcweir 	}
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir 	XMLElementWrapper_XmlSecImpl* pTarget =
150*cdf0e10cSrcweir         reinterpret_cast<XMLElementWrapper_XmlSecImpl*>(
151*cdf0e10cSrcweir             sal::static_int_cast<sal_uIntPtr>(
152*cdf0e10cSrcweir                 xTgtTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() )));
153*cdf0e10cSrcweir 	if( pTarget == NULL ) {
154*cdf0e10cSrcweir 		throw RuntimeException() ;
155*cdf0e10cSrcweir 	}
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir 	pContent = pTarget->getNativeElement() ;
158*cdf0e10cSrcweir 	//MM : end
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 	if( pContent == NULL ) {
161*cdf0e10cSrcweir 		throw XMLEncryptionException() ;
162*cdf0e10cSrcweir 	}
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 	/* MM : remove the following 2 lines
165*cdf0e10cSrcweir 	xmlUnlinkNode(pContent);
166*cdf0e10cSrcweir 	xmlAddNextSibling(pEncryptedData, pContent);
167*cdf0e10cSrcweir 	*/
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir 	//remember the position of the element to be signed
170*cdf0e10cSrcweir 	sal_Bool isParentRef = sal_True;
171*cdf0e10cSrcweir 	xmlNodePtr pParent = pEncryptedData->parent;
172*cdf0e10cSrcweir 	xmlNodePtr referenceNode;
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir 	if (pEncryptedData == pParent->children)
175*cdf0e10cSrcweir 	{
176*cdf0e10cSrcweir 		referenceNode = pParent;
177*cdf0e10cSrcweir 	}
178*cdf0e10cSrcweir 	else
179*cdf0e10cSrcweir 	{
180*cdf0e10cSrcweir 		referenceNode = pEncryptedData->prev;
181*cdf0e10cSrcweir 		isParentRef = sal_False;
182*cdf0e10cSrcweir 	}
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir  	setErrorRecorder( );
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir 	pMngr = pSecEnv->createKeysManager() ; //i39448
187*cdf0e10cSrcweir 	if( !pMngr ) {
188*cdf0e10cSrcweir 		throw RuntimeException() ;
189*cdf0e10cSrcweir 	}
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir 	//Create Encryption context
192*cdf0e10cSrcweir 	pEncCtx = xmlSecEncCtxCreate( pMngr ) ;
193*cdf0e10cSrcweir 	if( pEncCtx == NULL )
194*cdf0e10cSrcweir 	{
195*cdf0e10cSrcweir 		pSecEnv->destroyKeysManager( pMngr ) ; //i39448
196*cdf0e10cSrcweir 		//throw XMLEncryptionException() ;
197*cdf0e10cSrcweir 		clearErrorRecorder();
198*cdf0e10cSrcweir 		return aTemplate;
199*cdf0e10cSrcweir 	}
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 	pEncryptedData = pTemplate->getNativeElement() ;
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir 	//Find the element to be encrypted.
204*cdf0e10cSrcweir 	/* MM : remove the old method to get the target element
205*cdf0e10cSrcweir 	//This element is wrapped in the CipherValue sub-element.
206*cdf0e10cSrcweir 	xmlNodePtr pCipherData = pEncryptedData->children;
207*cdf0e10cSrcweir 	while (pCipherData != NULL && stricmp((const char *)(pCipherData->name), "CipherData"))
208*cdf0e10cSrcweir 	{
209*cdf0e10cSrcweir 		pCipherData = pCipherData->next;
210*cdf0e10cSrcweir 	}
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir 	if( pCipherData == NULL ) {
213*cdf0e10cSrcweir 		xmlSecEncCtxDestroy( pEncCtx ) ;
214*cdf0e10cSrcweir 		throw XMLEncryptionException() ;
215*cdf0e10cSrcweir 	}
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 	xmlNodePtr pCipherValue = pCipherData->children;
218*cdf0e10cSrcweir 	while (pCipherValue != NULL && stricmp((const char *)(pCipherValue->name), "CipherValue"))
219*cdf0e10cSrcweir 	{
220*cdf0e10cSrcweir 		pCipherValue = pCipherValue->next;
221*cdf0e10cSrcweir 	}
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir 	if( pCipherValue == NULL ) {
224*cdf0e10cSrcweir 		xmlSecEncCtxDestroy( pEncCtx ) ;
225*cdf0e10cSrcweir 		throw XMLEncryptionException() ;
226*cdf0e10cSrcweir 	}
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir 	pContent = pCipherValue->children;
229*cdf0e10cSrcweir 	*/
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir 	//Encrypt the template
232*cdf0e10cSrcweir 	if( xmlSecEncCtxXmlEncrypt( pEncCtx , pEncryptedData , pContent ) < 0 )
233*cdf0e10cSrcweir 	{
234*cdf0e10cSrcweir 		xmlSecEncCtxDestroy( pEncCtx ) ;
235*cdf0e10cSrcweir 		pSecEnv->destroyKeysManager( pMngr ) ; //i39448
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir 		//throw XMLEncryptionException() ;
238*cdf0e10cSrcweir 		clearErrorRecorder();
239*cdf0e10cSrcweir 		return aTemplate;
240*cdf0e10cSrcweir 	}
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir 	xmlSecEncCtxDestroy( pEncCtx ) ;
243*cdf0e10cSrcweir 	pSecEnv->destroyKeysManager( pMngr ) ; //i39448
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir 	//get the new EncryptedData element
246*cdf0e10cSrcweir 	if (isParentRef)
247*cdf0e10cSrcweir 	{
248*cdf0e10cSrcweir 		pTemplate->setNativeElement(referenceNode->children) ;
249*cdf0e10cSrcweir 	}
250*cdf0e10cSrcweir 	else
251*cdf0e10cSrcweir 	{
252*cdf0e10cSrcweir 		pTemplate->setNativeElement(referenceNode->next);
253*cdf0e10cSrcweir 	}
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir 	return aTemplate ;
256*cdf0e10cSrcweir }
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir /* XXMLEncryption */
259*cdf0e10cSrcweir Reference< XXMLEncryptionTemplate >
260*cdf0e10cSrcweir SAL_CALL XMLEncryption_NssImpl :: decrypt(
261*cdf0e10cSrcweir 	const Reference< XXMLEncryptionTemplate >& aTemplate ,
262*cdf0e10cSrcweir 	const Reference< XXMLSecurityContext >& aSecurityCtx
263*cdf0e10cSrcweir ) throw( com::sun::star::xml::crypto::XMLEncryptionException ,
264*cdf0e10cSrcweir 		 com::sun::star::uno::SecurityException) {
265*cdf0e10cSrcweir 	xmlSecKeysMngrPtr pMngr = NULL ;
266*cdf0e10cSrcweir 	xmlSecEncCtxPtr pEncCtx = NULL ;
267*cdf0e10cSrcweir 	xmlNodePtr pEncryptedData = NULL ;
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir 	if( !aTemplate.is() )
270*cdf0e10cSrcweir 		throw RuntimeException() ;
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir 	if( !aSecurityCtx.is() )
273*cdf0e10cSrcweir 		throw RuntimeException() ;
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir 	//Get the encryption template
276*cdf0e10cSrcweir 	Reference< XXMLElementWrapper > xTemplate = aTemplate->getTemplate() ;
277*cdf0e10cSrcweir 	if( !xTemplate.is() ) {
278*cdf0e10cSrcweir 		throw RuntimeException() ;
279*cdf0e10cSrcweir 	}
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir 	Reference< XUnoTunnel > xTplTunnel( xTemplate , UNO_QUERY ) ;
282*cdf0e10cSrcweir 	if( !xTplTunnel.is() ) {
283*cdf0e10cSrcweir 		throw RuntimeException() ;
284*cdf0e10cSrcweir 	}
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir 	XMLElementWrapper_XmlSecImpl* pTemplate =
287*cdf0e10cSrcweir         reinterpret_cast<XMLElementWrapper_XmlSecImpl*>(
288*cdf0e10cSrcweir             sal::static_int_cast<sal_uIntPtr>(
289*cdf0e10cSrcweir                 xTplTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() )));
290*cdf0e10cSrcweir 	if( pTemplate == NULL ) {
291*cdf0e10cSrcweir 		throw RuntimeException() ;
292*cdf0e10cSrcweir 	}
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir 	pEncryptedData = pTemplate->getNativeElement() ;
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir 	//remember the position of the element to be signed
297*cdf0e10cSrcweir 	sal_Bool isParentRef = sal_True;
298*cdf0e10cSrcweir 	xmlNodePtr pParent = pEncryptedData->parent;
299*cdf0e10cSrcweir 	xmlNodePtr referenceNode;
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir 	if (pEncryptedData == pParent->children)
302*cdf0e10cSrcweir 	{
303*cdf0e10cSrcweir 		referenceNode = pParent;
304*cdf0e10cSrcweir 	}
305*cdf0e10cSrcweir 	else
306*cdf0e10cSrcweir 	{
307*cdf0e10cSrcweir 		referenceNode = pEncryptedData->prev;
308*cdf0e10cSrcweir 		isParentRef = sal_False;
309*cdf0e10cSrcweir 	}
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir  	setErrorRecorder( );
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir 	sal_Int32 nSecurityEnvironment = aSecurityCtx->getSecurityEnvironmentNumber();
314*cdf0e10cSrcweir 	sal_Int32 i;
315*cdf0e10cSrcweir 
316*cdf0e10cSrcweir 	for (i=0; i<nSecurityEnvironment; ++i)
317*cdf0e10cSrcweir 	{
318*cdf0e10cSrcweir 		Reference< XSecurityEnvironment > aEnvironment = aSecurityCtx->getSecurityEnvironmentByIndex(i);
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir 		//Get Keys Manager
321*cdf0e10cSrcweir 		Reference< XUnoTunnel > xSecTunnel( aEnvironment , UNO_QUERY ) ;
322*cdf0e10cSrcweir 		if( !aEnvironment.is() ) {
323*cdf0e10cSrcweir 			 throw RuntimeException() ;
324*cdf0e10cSrcweir 		}
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir 		SecurityEnvironment_NssImpl* pSecEnv =
327*cdf0e10cSrcweir             reinterpret_cast<SecurityEnvironment_NssImpl*>(
328*cdf0e10cSrcweir                 sal::static_int_cast<sal_uIntPtr>(
329*cdf0e10cSrcweir                     xSecTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() )));
330*cdf0e10cSrcweir 		if( pSecEnv == NULL )
331*cdf0e10cSrcweir 			throw RuntimeException() ;
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir 		pMngr = pSecEnv->createKeysManager() ; //i39448
334*cdf0e10cSrcweir 		if( !pMngr ) {
335*cdf0e10cSrcweir 			throw RuntimeException() ;
336*cdf0e10cSrcweir 		}
337*cdf0e10cSrcweir 
338*cdf0e10cSrcweir 		//Create Encryption context
339*cdf0e10cSrcweir 		pEncCtx = xmlSecEncCtxCreate( pMngr ) ;
340*cdf0e10cSrcweir 		if( pEncCtx == NULL )
341*cdf0e10cSrcweir 		{
342*cdf0e10cSrcweir 			pSecEnv->destroyKeysManager( pMngr ) ; //i39448
343*cdf0e10cSrcweir 			//throw XMLEncryptionException() ;
344*cdf0e10cSrcweir 			clearErrorRecorder();
345*cdf0e10cSrcweir 			return aTemplate;
346*cdf0e10cSrcweir 		}
347*cdf0e10cSrcweir 
348*cdf0e10cSrcweir 		//Decrypt the template
349*cdf0e10cSrcweir 		if(!( xmlSecEncCtxDecrypt( pEncCtx , pEncryptedData ) < 0 || pEncCtx->result == NULL ))
350*cdf0e10cSrcweir 		{
351*cdf0e10cSrcweir 			//The decryption succeeds
352*cdf0e10cSrcweir 
353*cdf0e10cSrcweir 			//Destroy the encryption context
354*cdf0e10cSrcweir 			xmlSecEncCtxDestroy( pEncCtx ) ;
355*cdf0e10cSrcweir 			pSecEnv->destroyKeysManager( pMngr ) ; //i39448
356*cdf0e10cSrcweir 
357*cdf0e10cSrcweir 			//get the decrypted element
358*cdf0e10cSrcweir 			XMLElementWrapper_XmlSecImpl * ret = new XMLElementWrapper_XmlSecImpl(isParentRef?
359*cdf0e10cSrcweir 				(referenceNode->children):(referenceNode->next));
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir 			//return ret;
362*cdf0e10cSrcweir 			aTemplate->setTemplate(ret);
363*cdf0e10cSrcweir 			break;
364*cdf0e10cSrcweir 		}
365*cdf0e10cSrcweir 		else
366*cdf0e10cSrcweir 		{
367*cdf0e10cSrcweir 			//The decryption fails, continue with the next security environment
368*cdf0e10cSrcweir 			xmlSecEncCtxDestroy( pEncCtx ) ;
369*cdf0e10cSrcweir 			pSecEnv->destroyKeysManager( pMngr ) ; //i39448
370*cdf0e10cSrcweir 		}
371*cdf0e10cSrcweir 	}
372*cdf0e10cSrcweir 
373*cdf0e10cSrcweir 	clearErrorRecorder();
374*cdf0e10cSrcweir 	return aTemplate;
375*cdf0e10cSrcweir }
376*cdf0e10cSrcweir 
377*cdf0e10cSrcweir /* XInitialization */
378*cdf0e10cSrcweir void SAL_CALL XMLEncryption_NssImpl :: initialize( const Sequence< Any >& /*aArguments*/ ) throw( Exception, RuntimeException ) {
379*cdf0e10cSrcweir 	// TBD
380*cdf0e10cSrcweir } ;
381*cdf0e10cSrcweir 
382*cdf0e10cSrcweir /* XServiceInfo */
383*cdf0e10cSrcweir OUString SAL_CALL XMLEncryption_NssImpl :: getImplementationName() throw( RuntimeException ) {
384*cdf0e10cSrcweir 	return impl_getImplementationName() ;
385*cdf0e10cSrcweir }
386*cdf0e10cSrcweir 
387*cdf0e10cSrcweir /* XServiceInfo */
388*cdf0e10cSrcweir sal_Bool SAL_CALL XMLEncryption_NssImpl :: supportsService( const OUString& serviceName) throw( RuntimeException ) {
389*cdf0e10cSrcweir 	Sequence< OUString > seqServiceNames = getSupportedServiceNames() ;
390*cdf0e10cSrcweir 	const OUString* pArray = seqServiceNames.getConstArray() ;
391*cdf0e10cSrcweir 	for( sal_Int32 i = 0 ; i < seqServiceNames.getLength() ; i ++ ) {
392*cdf0e10cSrcweir 		if( *( pArray + i ) == serviceName )
393*cdf0e10cSrcweir 			return sal_True ;
394*cdf0e10cSrcweir 	}
395*cdf0e10cSrcweir 	return sal_False ;
396*cdf0e10cSrcweir }
397*cdf0e10cSrcweir 
398*cdf0e10cSrcweir /* XServiceInfo */
399*cdf0e10cSrcweir Sequence< OUString > SAL_CALL XMLEncryption_NssImpl :: getSupportedServiceNames() throw( RuntimeException ) {
400*cdf0e10cSrcweir 	return impl_getSupportedServiceNames() ;
401*cdf0e10cSrcweir }
402*cdf0e10cSrcweir 
403*cdf0e10cSrcweir //Helper for XServiceInfo
404*cdf0e10cSrcweir Sequence< OUString > XMLEncryption_NssImpl :: impl_getSupportedServiceNames() {
405*cdf0e10cSrcweir 	::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ) ;
406*cdf0e10cSrcweir 	Sequence< OUString > seqServiceNames( 1 ) ;
407*cdf0e10cSrcweir 	seqServiceNames.getArray()[0] = OUString::createFromAscii( "com.sun.star.xml.crypto.XMLEncryption" ) ;
408*cdf0e10cSrcweir 	return seqServiceNames ;
409*cdf0e10cSrcweir }
410*cdf0e10cSrcweir 
411*cdf0e10cSrcweir OUString XMLEncryption_NssImpl :: impl_getImplementationName() throw( RuntimeException ) {
412*cdf0e10cSrcweir 	return OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLEncryption_NssImpl" ) ;
413*cdf0e10cSrcweir }
414*cdf0e10cSrcweir 
415*cdf0e10cSrcweir //Helper for registry
416*cdf0e10cSrcweir Reference< XInterface > SAL_CALL XMLEncryption_NssImpl :: impl_createInstance( const Reference< XMultiServiceFactory >& aServiceManager ) throw( RuntimeException ) {
417*cdf0e10cSrcweir 	return Reference< XInterface >( *new XMLEncryption_NssImpl( aServiceManager ) ) ;
418*cdf0e10cSrcweir }
419*cdf0e10cSrcweir 
420*cdf0e10cSrcweir Reference< XSingleServiceFactory > XMLEncryption_NssImpl :: impl_createFactory( const Reference< XMultiServiceFactory >& aServiceManager ) {
421*cdf0e10cSrcweir 	//Reference< XSingleServiceFactory > xFactory ;
422*cdf0e10cSrcweir 	//xFactory = ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName , impl_createInstance , impl_getSupportedServiceNames ) ;
423*cdf0e10cSrcweir 	//return xFactory ;
424*cdf0e10cSrcweir 	return ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName() , impl_createInstance , impl_getSupportedServiceNames() ) ;
425*cdf0e10cSrcweir }
426*cdf0e10cSrcweir 
427