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 
31*cdf0e10cSrcweir #include "decryptorimpl.hxx"
32*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir namespace cssu = com::sun::star::uno;
37*cdf0e10cSrcweir namespace cssl = com::sun::star::lang;
38*cdf0e10cSrcweir namespace cssxc = com::sun::star::xml::crypto;
39*cdf0e10cSrcweir namespace cssxw = com::sun::star::xml::wrapper;
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #define SERVICE_NAME "com.sun.star.xml.crypto.sax.Decryptor"
42*cdf0e10cSrcweir #define IMPLEMENTATION_NAME "com.sun.star.xml.security.framework.DecryptorImpl"
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #define	DECLARE_ASCII( SASCIIVALUE )																			\
45*cdf0e10cSrcweir 	rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SASCIIVALUE ) )
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir DecryptorImpl::DecryptorImpl( const cssu::Reference< cssl::XMultiServiceFactory >& rxMSF)
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir 	mxMSF = rxMSF;
50*cdf0e10cSrcweir }
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir DecryptorImpl::~DecryptorImpl()
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir }
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir bool DecryptorImpl::checkReady() const
57*cdf0e10cSrcweir /****** DecryptorImpl/checkReady *********************************************
58*cdf0e10cSrcweir  *
59*cdf0e10cSrcweir  *   NAME
60*cdf0e10cSrcweir  *	checkReady -- checks the conditions for the decryption.
61*cdf0e10cSrcweir  *
62*cdf0e10cSrcweir  *   SYNOPSIS
63*cdf0e10cSrcweir  *	bReady = checkReady( );
64*cdf0e10cSrcweir  *
65*cdf0e10cSrcweir  *   FUNCTION
66*cdf0e10cSrcweir  *	checks whether all following conditions are satisfied:
67*cdf0e10cSrcweir  *	1. the result listener is ready;
68*cdf0e10cSrcweir  *	2. the EncryptionEngine is ready.
69*cdf0e10cSrcweir  *
70*cdf0e10cSrcweir  *   INPUTS
71*cdf0e10cSrcweir  *	empty
72*cdf0e10cSrcweir  *
73*cdf0e10cSrcweir  *   RESULT
74*cdf0e10cSrcweir  *	bReady - true if all conditions are satisfied, false otherwise
75*cdf0e10cSrcweir  *
76*cdf0e10cSrcweir  *   HISTORY
77*cdf0e10cSrcweir  *	05.01.2004 -	implemented
78*cdf0e10cSrcweir  *
79*cdf0e10cSrcweir  *   AUTHOR
80*cdf0e10cSrcweir  *	Michael Mi
81*cdf0e10cSrcweir  *	Email: michael.mi@sun.com
82*cdf0e10cSrcweir  ******************************************************************************/
83*cdf0e10cSrcweir {
84*cdf0e10cSrcweir 	return (m_xResultListener.is() && EncryptionEngine::checkReady());
85*cdf0e10cSrcweir }
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir void DecryptorImpl::notifyResultListener() const
88*cdf0e10cSrcweir 	throw (cssu::Exception, cssu::RuntimeException)
89*cdf0e10cSrcweir /****** DecryptorImpl/notifyResultListener ***********************************
90*cdf0e10cSrcweir  *
91*cdf0e10cSrcweir  *   NAME
92*cdf0e10cSrcweir  *	notifyResultListener -- notifies the listener about the decryption
93*cdf0e10cSrcweir  *	result.
94*cdf0e10cSrcweir  *
95*cdf0e10cSrcweir  *   SYNOPSIS
96*cdf0e10cSrcweir  *	notifyResultListener( );
97*cdf0e10cSrcweir  *
98*cdf0e10cSrcweir  *   FUNCTION
99*cdf0e10cSrcweir  *	see NAME.
100*cdf0e10cSrcweir  *
101*cdf0e10cSrcweir  *   INPUTS
102*cdf0e10cSrcweir  *	empty
103*cdf0e10cSrcweir  *
104*cdf0e10cSrcweir  *   RESULT
105*cdf0e10cSrcweir  *	empty
106*cdf0e10cSrcweir  *
107*cdf0e10cSrcweir  *   HISTORY
108*cdf0e10cSrcweir  *	05.01.2004 -	implemented
109*cdf0e10cSrcweir  *
110*cdf0e10cSrcweir  *   AUTHOR
111*cdf0e10cSrcweir  *	Michael Mi
112*cdf0e10cSrcweir  *	Email: michael.mi@sun.com
113*cdf0e10cSrcweir  ******************************************************************************/
114*cdf0e10cSrcweir {
115*cdf0e10cSrcweir 	cssu::Reference< cssxc::sax::XDecryptionResultListener >
116*cdf0e10cSrcweir 		xDecryptionResultListener ( m_xResultListener , cssu::UNO_QUERY ) ;
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 	xDecryptionResultListener->decrypted(m_nSecurityId,m_nStatus);
119*cdf0e10cSrcweir }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir void DecryptorImpl::startEngine( const cssu::Reference<
122*cdf0e10cSrcweir 	cssxc::XXMLEncryptionTemplate >&
123*cdf0e10cSrcweir 	xEncryptionTemplate)
124*cdf0e10cSrcweir 	throw (cssu::Exception, cssu::RuntimeException)
125*cdf0e10cSrcweir /****** DecryptorImpl/startEngine ********************************************
126*cdf0e10cSrcweir  *
127*cdf0e10cSrcweir  *   NAME
128*cdf0e10cSrcweir  *	startEngine -- decrypts the encryption.
129*cdf0e10cSrcweir  *
130*cdf0e10cSrcweir  *   SYNOPSIS
131*cdf0e10cSrcweir  *	startEngine( xEncryptionTemplate );
132*cdf0e10cSrcweir  *
133*cdf0e10cSrcweir  *   FUNCTION
134*cdf0e10cSrcweir  *	decrypts the encryption element, then if succeeds, updates the link
135*cdf0e10cSrcweir  *	of old template element to the new encryption element in
136*cdf0e10cSrcweir  *	SAXEventKeeper.
137*cdf0e10cSrcweir  *
138*cdf0e10cSrcweir  *   INPUTS
139*cdf0e10cSrcweir  *	xEncryptionTemplate - the encryption template to be decrypted.
140*cdf0e10cSrcweir  *
141*cdf0e10cSrcweir  *   RESULT
142*cdf0e10cSrcweir  *	empty
143*cdf0e10cSrcweir  *
144*cdf0e10cSrcweir  *   HISTORY
145*cdf0e10cSrcweir  *	05.01.2004 -	implemented
146*cdf0e10cSrcweir  *
147*cdf0e10cSrcweir  *   AUTHOR
148*cdf0e10cSrcweir  *	Michael Mi
149*cdf0e10cSrcweir  *	Email: michael.mi@sun.com
150*cdf0e10cSrcweir  ******************************************************************************/
151*cdf0e10cSrcweir {
152*cdf0e10cSrcweir 	cssu::Reference< cssxc::XXMLEncryptionTemplate > xResultTemplate;
153*cdf0e10cSrcweir 	try
154*cdf0e10cSrcweir 	{
155*cdf0e10cSrcweir 		xResultTemplate = m_xXMLEncryption->decrypt(xEncryptionTemplate, m_xXMLSecurityContext);
156*cdf0e10cSrcweir 		m_nStatus = xResultTemplate->getStatus();
157*cdf0e10cSrcweir 	}
158*cdf0e10cSrcweir 	catch( cssu::Exception& )
159*cdf0e10cSrcweir 	{
160*cdf0e10cSrcweir 		m_nStatus = cssxc::SecurityOperationStatus_RUNTIMEERROR_FAILED;
161*cdf0e10cSrcweir 	}
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 	if (m_nStatus == cssxc::SecurityOperationStatus_OPERATION_SUCCEEDED)
164*cdf0e10cSrcweir 	{
165*cdf0e10cSrcweir 		cssu::Reference< cssxw::XXMLElementWrapper > xDecryptedElement
166*cdf0e10cSrcweir 			= xResultTemplate->getTemplate();
167*cdf0e10cSrcweir 		m_xSAXEventKeeper->setElement(m_nIdOfTemplateEC, xDecryptedElement);
168*cdf0e10cSrcweir 	}
169*cdf0e10cSrcweir }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir /* XDecryptionResultBroadcaster */
172*cdf0e10cSrcweir void SAL_CALL DecryptorImpl::addDecryptionResultListener( const cssu::Reference< cssxc::sax::XDecryptionResultListener >& listener )
173*cdf0e10cSrcweir     	throw (cssu::Exception, cssu::RuntimeException)
174*cdf0e10cSrcweir {
175*cdf0e10cSrcweir 	m_xResultListener = listener;
176*cdf0e10cSrcweir 	tryToPerform();
177*cdf0e10cSrcweir }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir void SAL_CALL DecryptorImpl::removeDecryptionResultListener( const cssu::Reference< cssxc::sax::XDecryptionResultListener >&)
180*cdf0e10cSrcweir     	throw (cssu::RuntimeException)
181*cdf0e10cSrcweir {
182*cdf0e10cSrcweir }
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir /* XInitialization */
185*cdf0e10cSrcweir void SAL_CALL DecryptorImpl::initialize( const cssu::Sequence< cssu::Any >& aArguments )
186*cdf0e10cSrcweir 	throw (cssu::Exception, cssu::RuntimeException)
187*cdf0e10cSrcweir {
188*cdf0e10cSrcweir 	OSL_ASSERT(aArguments.getLength() == 5);
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 	rtl::OUString ouTempString;
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir 	aArguments[0] >>= ouTempString;
193*cdf0e10cSrcweir 	m_nSecurityId = ouTempString.toInt32();
194*cdf0e10cSrcweir 	aArguments[1] >>= m_xSAXEventKeeper;
195*cdf0e10cSrcweir 	aArguments[2] >>= ouTempString;
196*cdf0e10cSrcweir 	m_nIdOfTemplateEC = ouTempString.toInt32();
197*cdf0e10cSrcweir 	aArguments[3] >>= m_xXMLSecurityContext;
198*cdf0e10cSrcweir 	aArguments[4] >>= m_xXMLEncryption;
199*cdf0e10cSrcweir }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir rtl::OUString DecryptorImpl_getImplementationName ()
202*cdf0e10cSrcweir 	throw (cssu::RuntimeException)
203*cdf0e10cSrcweir {
204*cdf0e10cSrcweir 	return rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) );
205*cdf0e10cSrcweir }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir sal_Bool SAL_CALL DecryptorImpl_supportsService( const rtl::OUString& ServiceName )
208*cdf0e10cSrcweir 	throw (cssu::RuntimeException)
209*cdf0e10cSrcweir {
210*cdf0e10cSrcweir 	return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ));
211*cdf0e10cSrcweir }
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir cssu::Sequence< rtl::OUString > SAL_CALL DecryptorImpl_getSupportedServiceNames(  )
214*cdf0e10cSrcweir 	throw (cssu::RuntimeException)
215*cdf0e10cSrcweir {
216*cdf0e10cSrcweir 	cssu::Sequence < rtl::OUString > aRet(1);
217*cdf0e10cSrcweir 	rtl::OUString* pArray = aRet.getArray();
218*cdf0e10cSrcweir 	pArray[0] =  rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
219*cdf0e10cSrcweir 	return aRet;
220*cdf0e10cSrcweir }
221*cdf0e10cSrcweir #undef SERVICE_NAME
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir cssu::Reference< cssu::XInterface > SAL_CALL DecryptorImpl_createInstance( const cssu::Reference< cssl::XMultiServiceFactory >& rSMgr)
224*cdf0e10cSrcweir 	throw( cssu::Exception )
225*cdf0e10cSrcweir {
226*cdf0e10cSrcweir 	return (cppu::OWeakObject*) new DecryptorImpl(rSMgr);
227*cdf0e10cSrcweir }
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir /* XServiceInfo */
230*cdf0e10cSrcweir rtl::OUString SAL_CALL DecryptorImpl::getImplementationName(  )
231*cdf0e10cSrcweir 	throw (cssu::RuntimeException)
232*cdf0e10cSrcweir {
233*cdf0e10cSrcweir 	return DecryptorImpl_getImplementationName();
234*cdf0e10cSrcweir }
235*cdf0e10cSrcweir sal_Bool SAL_CALL DecryptorImpl::supportsService( const rtl::OUString& rServiceName )
236*cdf0e10cSrcweir 	throw (cssu::RuntimeException)
237*cdf0e10cSrcweir {
238*cdf0e10cSrcweir 	return DecryptorImpl_supportsService( rServiceName );
239*cdf0e10cSrcweir }
240*cdf0e10cSrcweir cssu::Sequence< rtl::OUString > SAL_CALL DecryptorImpl::getSupportedServiceNames(  )
241*cdf0e10cSrcweir 	throw (cssu::RuntimeException)
242*cdf0e10cSrcweir {
243*cdf0e10cSrcweir 	return DecryptorImpl_getSupportedServiceNames();
244*cdf0e10cSrcweir }
245*cdf0e10cSrcweir 
246