xref: /AOO41X/main/xmlsecurity/source/framework/encryptorimpl.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 "encryptorimpl.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.Encryptor"
42*cdf0e10cSrcweir #define IMPLEMENTATION_NAME "com.sun.star.xml.security.framework.EncryptorImpl"
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #define	DECLARE_ASCII( SASCIIVALUE )																			\
45*cdf0e10cSrcweir 	rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SASCIIVALUE ) )
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir EncryptorImpl::EncryptorImpl( const cssu::Reference< cssl::XMultiServiceFactory >& rxMSF)
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir 	m_nReferenceId = -1;
50*cdf0e10cSrcweir 	mxMSF = rxMSF;
51*cdf0e10cSrcweir }
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir EncryptorImpl::~EncryptorImpl()
54*cdf0e10cSrcweir {
55*cdf0e10cSrcweir }
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir bool EncryptorImpl::checkReady() const
58*cdf0e10cSrcweir /****** EncryptorImpl/checkReady *********************************************
59*cdf0e10cSrcweir  *
60*cdf0e10cSrcweir  *   NAME
61*cdf0e10cSrcweir  *	checkReady -- checks the conditions for the encryption.
62*cdf0e10cSrcweir  *
63*cdf0e10cSrcweir  *   SYNOPSIS
64*cdf0e10cSrcweir  *	bReady = checkReady( );
65*cdf0e10cSrcweir  *
66*cdf0e10cSrcweir  *   FUNCTION
67*cdf0e10cSrcweir  *	checks whether all following conditions are satisfied:
68*cdf0e10cSrcweir  *	1. the result listener is ready;
69*cdf0e10cSrcweir  *	2. the EncryptionEngine is ready.
70*cdf0e10cSrcweir  *
71*cdf0e10cSrcweir  *   INPUTS
72*cdf0e10cSrcweir  *	empty
73*cdf0e10cSrcweir  *
74*cdf0e10cSrcweir  *   RESULT
75*cdf0e10cSrcweir  *	bReady - true if all conditions are satisfied, false otherwise
76*cdf0e10cSrcweir  *
77*cdf0e10cSrcweir  *   HISTORY
78*cdf0e10cSrcweir  *	05.01.2004 -	implemented
79*cdf0e10cSrcweir  *
80*cdf0e10cSrcweir  *   AUTHOR
81*cdf0e10cSrcweir  *	Michael Mi
82*cdf0e10cSrcweir  *	Email: michael.mi@sun.com
83*cdf0e10cSrcweir  ******************************************************************************/
84*cdf0e10cSrcweir {
85*cdf0e10cSrcweir 	sal_Int32 nKeyInc = 0;
86*cdf0e10cSrcweir 	if (m_nIdOfKeyEC != 0)
87*cdf0e10cSrcweir 	{
88*cdf0e10cSrcweir 		nKeyInc = 1;
89*cdf0e10cSrcweir 	}
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 	return (m_xResultListener.is() &&
92*cdf0e10cSrcweir 		(m_nReferenceId != -1) &&
93*cdf0e10cSrcweir 		(2+nKeyInc == m_nNumOfResolvedReferences) &&
94*cdf0e10cSrcweir 		EncryptionEngine::checkReady());
95*cdf0e10cSrcweir }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir void EncryptorImpl::notifyResultListener() const
98*cdf0e10cSrcweir 	throw (cssu::Exception, cssu::RuntimeException)
99*cdf0e10cSrcweir /****** DecryptorImpl/notifyResultListener ***********************************
100*cdf0e10cSrcweir  *
101*cdf0e10cSrcweir  *   NAME
102*cdf0e10cSrcweir  *	notifyResultListener -- notifies the listener about the encryption
103*cdf0e10cSrcweir  *	result.
104*cdf0e10cSrcweir  *
105*cdf0e10cSrcweir  *   SYNOPSIS
106*cdf0e10cSrcweir  *	notifyResultListener( );
107*cdf0e10cSrcweir  *
108*cdf0e10cSrcweir  *   FUNCTION
109*cdf0e10cSrcweir  *	see NAME.
110*cdf0e10cSrcweir  *
111*cdf0e10cSrcweir  *   INPUTS
112*cdf0e10cSrcweir  *	empty
113*cdf0e10cSrcweir  *
114*cdf0e10cSrcweir  *   RESULT
115*cdf0e10cSrcweir  *	empty
116*cdf0e10cSrcweir  *
117*cdf0e10cSrcweir  *   HISTORY
118*cdf0e10cSrcweir  *	05.01.2004 -	implemented
119*cdf0e10cSrcweir  *
120*cdf0e10cSrcweir  *   AUTHOR
121*cdf0e10cSrcweir  *	Michael Mi
122*cdf0e10cSrcweir  *	Email: michael.mi@sun.com
123*cdf0e10cSrcweir  ******************************************************************************/
124*cdf0e10cSrcweir {
125*cdf0e10cSrcweir 	cssu::Reference< cssxc::sax::XEncryptionResultListener >
126*cdf0e10cSrcweir 		xEncryptionResultListener ( m_xResultListener , cssu::UNO_QUERY ) ;
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 	xEncryptionResultListener->encrypted( m_nSecurityId, m_nStatus );
129*cdf0e10cSrcweir }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir void EncryptorImpl::startEngine( const cssu::Reference<
132*cdf0e10cSrcweir 	cssxc::XXMLEncryptionTemplate >&
133*cdf0e10cSrcweir 	xEncryptionTemplate)
134*cdf0e10cSrcweir     	throw (cssu::Exception, cssu::RuntimeException)
135*cdf0e10cSrcweir /****** EncryptorImpl/startEngine ********************************************
136*cdf0e10cSrcweir  *
137*cdf0e10cSrcweir  *   NAME
138*cdf0e10cSrcweir  *	startEngine -- generates the encryption.
139*cdf0e10cSrcweir  *
140*cdf0e10cSrcweir  *   SYNOPSIS
141*cdf0e10cSrcweir  *	startEngine( xEncryptionTemplate );
142*cdf0e10cSrcweir  *
143*cdf0e10cSrcweir  *   FUNCTION
144*cdf0e10cSrcweir  *	generates the encryption element, then if succeeds, updates the link
145*cdf0e10cSrcweir  *	of old template element to the new encryption element in
146*cdf0e10cSrcweir  *	SAXEventKeeper.
147*cdf0e10cSrcweir  *
148*cdf0e10cSrcweir  *   INPUTS
149*cdf0e10cSrcweir  *	xEncryptionTemplate - the encryption template to be encrypted.
150*cdf0e10cSrcweir  *
151*cdf0e10cSrcweir  *   RESULT
152*cdf0e10cSrcweir  *	empty
153*cdf0e10cSrcweir  *
154*cdf0e10cSrcweir  *   HISTORY
155*cdf0e10cSrcweir  *	05.01.2004 -	implemented
156*cdf0e10cSrcweir  *
157*cdf0e10cSrcweir  *   AUTHOR
158*cdf0e10cSrcweir  *	Michael Mi
159*cdf0e10cSrcweir  *	Email: michael.mi@sun.com
160*cdf0e10cSrcweir  ******************************************************************************/
161*cdf0e10cSrcweir {
162*cdf0e10cSrcweir 	cssu::Reference < cssxc::XXMLEncryptionTemplate > xResultTemplate;
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 	cssu::Reference< cssxw::XXMLElementWrapper >
165*cdf0e10cSrcweir 		xXMLElement = m_xSAXEventKeeper->getElement( m_nReferenceId );
166*cdf0e10cSrcweir 	xEncryptionTemplate->setTarget(xXMLElement);
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 	try
169*cdf0e10cSrcweir 	{
170*cdf0e10cSrcweir 		xResultTemplate = m_xXMLEncryption->encrypt(
171*cdf0e10cSrcweir 			xEncryptionTemplate, m_xSecurityEnvironment);
172*cdf0e10cSrcweir 		m_nStatus = xResultTemplate->getStatus();
173*cdf0e10cSrcweir 	}
174*cdf0e10cSrcweir 	catch( cssu::Exception& )
175*cdf0e10cSrcweir 	{
176*cdf0e10cSrcweir 		m_nStatus = cssxc::SecurityOperationStatus_RUNTIMEERROR_FAILED;
177*cdf0e10cSrcweir 	}
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 	if (m_nStatus == cssxc::SecurityOperationStatus_OPERATION_SUCCEEDED)
180*cdf0e10cSrcweir 	{
181*cdf0e10cSrcweir 		cssu::Reference < cssxw::XXMLElementWrapper > xResultEncryption
182*cdf0e10cSrcweir 			= xResultTemplate->getTemplate();
183*cdf0e10cSrcweir 		m_xSAXEventKeeper->setElement(m_nIdOfTemplateEC, xResultEncryption);
184*cdf0e10cSrcweir 		m_xSAXEventKeeper->setElement(m_nReferenceId, NULL);
185*cdf0e10cSrcweir 	}
186*cdf0e10cSrcweir }
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir /* XReferenceCollector */
189*cdf0e10cSrcweir void SAL_CALL EncryptorImpl::setReferenceCount(sal_Int32)
190*cdf0e10cSrcweir 	throw (cssu::Exception, cssu::RuntimeException)
191*cdf0e10cSrcweir {
192*cdf0e10cSrcweir 	/*
193*cdf0e10cSrcweir 	 * dummp method, because there is only one reference in
194*cdf0e10cSrcweir 	 * encryption, different from signature.
195*cdf0e10cSrcweir 	 * so the referenceNumber is always 1
196*cdf0e10cSrcweir 	 */
197*cdf0e10cSrcweir }
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir void SAL_CALL EncryptorImpl::setReferenceId( sal_Int32 id )
200*cdf0e10cSrcweir 	throw (cssu::Exception, cssu::RuntimeException)
201*cdf0e10cSrcweir {
202*cdf0e10cSrcweir 	m_nReferenceId = id;
203*cdf0e10cSrcweir }
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir /* XEncryptionResultBroadcaster */
206*cdf0e10cSrcweir void SAL_CALL EncryptorImpl::addEncryptionResultListener( const cssu::Reference< cssxc::sax::XEncryptionResultListener >& listener )
207*cdf0e10cSrcweir     	throw (cssu::Exception, cssu::RuntimeException)
208*cdf0e10cSrcweir {
209*cdf0e10cSrcweir 	m_xResultListener = listener;
210*cdf0e10cSrcweir 	tryToPerform();
211*cdf0e10cSrcweir }
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir void SAL_CALL EncryptorImpl::removeEncryptionResultListener( const cssu::Reference< cssxc::sax::XEncryptionResultListener >&)
214*cdf0e10cSrcweir     	throw (cssu::RuntimeException)
215*cdf0e10cSrcweir {
216*cdf0e10cSrcweir }
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir /* XInitialization */
219*cdf0e10cSrcweir void SAL_CALL EncryptorImpl::initialize( const cssu::Sequence< cssu::Any >& aArguments )
220*cdf0e10cSrcweir 	throw (cssu::Exception, cssu::RuntimeException)
221*cdf0e10cSrcweir {
222*cdf0e10cSrcweir 	OSL_ASSERT(aArguments.getLength() == 5);
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir 	rtl::OUString ouTempString;
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 	aArguments[0] >>= ouTempString;
227*cdf0e10cSrcweir 	m_nSecurityId = ouTempString.toInt32();
228*cdf0e10cSrcweir 	aArguments[1] >>= m_xSAXEventKeeper;
229*cdf0e10cSrcweir 	aArguments[2] >>= ouTempString;
230*cdf0e10cSrcweir 	m_nIdOfTemplateEC = ouTempString.toInt32();
231*cdf0e10cSrcweir 	aArguments[3] >>= m_xSecurityEnvironment;
232*cdf0e10cSrcweir 	aArguments[4] >>= m_xXMLEncryption;
233*cdf0e10cSrcweir }
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir rtl::OUString EncryptorImpl_getImplementationName ()
237*cdf0e10cSrcweir 	throw (cssu::RuntimeException)
238*cdf0e10cSrcweir {
239*cdf0e10cSrcweir 	return rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) );
240*cdf0e10cSrcweir }
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir sal_Bool SAL_CALL EncryptorImpl_supportsService( const rtl::OUString& ServiceName )
243*cdf0e10cSrcweir 	throw (cssu::RuntimeException)
244*cdf0e10cSrcweir {
245*cdf0e10cSrcweir 	return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ));
246*cdf0e10cSrcweir }
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir cssu::Sequence< rtl::OUString > SAL_CALL EncryptorImpl_getSupportedServiceNames(  )
249*cdf0e10cSrcweir 	throw (cssu::RuntimeException)
250*cdf0e10cSrcweir {
251*cdf0e10cSrcweir 	cssu::Sequence < rtl::OUString > aRet(1);
252*cdf0e10cSrcweir 	rtl::OUString* pArray = aRet.getArray();
253*cdf0e10cSrcweir 	pArray[0] =  rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
254*cdf0e10cSrcweir 	return aRet;
255*cdf0e10cSrcweir }
256*cdf0e10cSrcweir #undef SERVICE_NAME
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir cssu::Reference< cssu::XInterface > SAL_CALL EncryptorImpl_createInstance(
259*cdf0e10cSrcweir 	const cssu::Reference< cssl::XMultiServiceFactory >& rSMgr)
260*cdf0e10cSrcweir 	throw( cssu::Exception )
261*cdf0e10cSrcweir {
262*cdf0e10cSrcweir 	return (cppu::OWeakObject*) new EncryptorImpl(rSMgr);
263*cdf0e10cSrcweir }
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir /* XServiceInfo */
266*cdf0e10cSrcweir rtl::OUString SAL_CALL EncryptorImpl::getImplementationName(  )
267*cdf0e10cSrcweir 	throw (cssu::RuntimeException)
268*cdf0e10cSrcweir {
269*cdf0e10cSrcweir 	return EncryptorImpl_getImplementationName();
270*cdf0e10cSrcweir }
271*cdf0e10cSrcweir sal_Bool SAL_CALL EncryptorImpl::supportsService( const rtl::OUString& rServiceName )
272*cdf0e10cSrcweir 	throw (cssu::RuntimeException)
273*cdf0e10cSrcweir {
274*cdf0e10cSrcweir 	return EncryptorImpl_supportsService( rServiceName );
275*cdf0e10cSrcweir }
276*cdf0e10cSrcweir cssu::Sequence< rtl::OUString > SAL_CALL EncryptorImpl::getSupportedServiceNames(  )
277*cdf0e10cSrcweir 	throw (cssu::RuntimeException)
278*cdf0e10cSrcweir {
279*cdf0e10cSrcweir 	return EncryptorImpl_getSupportedServiceNames();
280*cdf0e10cSrcweir }
281*cdf0e10cSrcweir 
282