1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
30 
31 #include "encryptorimpl.hxx"
32 #include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp>
33 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
34 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35 
36 namespace cssu = com::sun::star::uno;
37 namespace cssl = com::sun::star::lang;
38 namespace cssxc = com::sun::star::xml::crypto;
39 namespace cssxw = com::sun::star::xml::wrapper;
40 
41 #define SERVICE_NAME "com.sun.star.xml.crypto.sax.Encryptor"
42 #define IMPLEMENTATION_NAME "com.sun.star.xml.security.framework.EncryptorImpl"
43 
44 #define	DECLARE_ASCII( SASCIIVALUE )																			\
45 	rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SASCIIVALUE ) )
46 
47 EncryptorImpl::EncryptorImpl( const cssu::Reference< cssl::XMultiServiceFactory >& rxMSF)
48 {
49 	m_nReferenceId = -1;
50 	mxMSF = rxMSF;
51 }
52 
53 EncryptorImpl::~EncryptorImpl()
54 {
55 }
56 
57 bool EncryptorImpl::checkReady() const
58 /****** EncryptorImpl/checkReady *********************************************
59  *
60  *   NAME
61  *	checkReady -- checks the conditions for the encryption.
62  *
63  *   SYNOPSIS
64  *	bReady = checkReady( );
65  *
66  *   FUNCTION
67  *	checks whether all following conditions are satisfied:
68  *	1. the result listener is ready;
69  *	2. the EncryptionEngine is ready.
70  *
71  *   INPUTS
72  *	empty
73  *
74  *   RESULT
75  *	bReady - true if all conditions are satisfied, false otherwise
76  *
77  *   HISTORY
78  *	05.01.2004 -	implemented
79  *
80  *   AUTHOR
81  *	Michael Mi
82  *	Email: michael.mi@sun.com
83  ******************************************************************************/
84 {
85 	sal_Int32 nKeyInc = 0;
86 	if (m_nIdOfKeyEC != 0)
87 	{
88 		nKeyInc = 1;
89 	}
90 
91 	return (m_xResultListener.is() &&
92 		(m_nReferenceId != -1) &&
93 		(2+nKeyInc == m_nNumOfResolvedReferences) &&
94 		EncryptionEngine::checkReady());
95 }
96 
97 void EncryptorImpl::notifyResultListener() const
98 	throw (cssu::Exception, cssu::RuntimeException)
99 /****** DecryptorImpl/notifyResultListener ***********************************
100  *
101  *   NAME
102  *	notifyResultListener -- notifies the listener about the encryption
103  *	result.
104  *
105  *   SYNOPSIS
106  *	notifyResultListener( );
107  *
108  *   FUNCTION
109  *	see NAME.
110  *
111  *   INPUTS
112  *	empty
113  *
114  *   RESULT
115  *	empty
116  *
117  *   HISTORY
118  *	05.01.2004 -	implemented
119  *
120  *   AUTHOR
121  *	Michael Mi
122  *	Email: michael.mi@sun.com
123  ******************************************************************************/
124 {
125 	cssu::Reference< cssxc::sax::XEncryptionResultListener >
126 		xEncryptionResultListener ( m_xResultListener , cssu::UNO_QUERY ) ;
127 
128 	xEncryptionResultListener->encrypted( m_nSecurityId, m_nStatus );
129 }
130 
131 void EncryptorImpl::startEngine( const cssu::Reference<
132 	cssxc::XXMLEncryptionTemplate >&
133 	xEncryptionTemplate)
134     	throw (cssu::Exception, cssu::RuntimeException)
135 /****** EncryptorImpl/startEngine ********************************************
136  *
137  *   NAME
138  *	startEngine -- generates the encryption.
139  *
140  *   SYNOPSIS
141  *	startEngine( xEncryptionTemplate );
142  *
143  *   FUNCTION
144  *	generates the encryption element, then if succeeds, updates the link
145  *	of old template element to the new encryption element in
146  *	SAXEventKeeper.
147  *
148  *   INPUTS
149  *	xEncryptionTemplate - the encryption template to be encrypted.
150  *
151  *   RESULT
152  *	empty
153  *
154  *   HISTORY
155  *	05.01.2004 -	implemented
156  *
157  *   AUTHOR
158  *	Michael Mi
159  *	Email: michael.mi@sun.com
160  ******************************************************************************/
161 {
162 	cssu::Reference < cssxc::XXMLEncryptionTemplate > xResultTemplate;
163 
164 	cssu::Reference< cssxw::XXMLElementWrapper >
165 		xXMLElement = m_xSAXEventKeeper->getElement( m_nReferenceId );
166 	xEncryptionTemplate->setTarget(xXMLElement);
167 
168 	try
169 	{
170 		xResultTemplate = m_xXMLEncryption->encrypt(
171 			xEncryptionTemplate, m_xSecurityEnvironment);
172 		m_nStatus = xResultTemplate->getStatus();
173 	}
174 	catch( cssu::Exception& )
175 	{
176 		m_nStatus = cssxc::SecurityOperationStatus_RUNTIMEERROR_FAILED;
177 	}
178 
179 	if (m_nStatus == cssxc::SecurityOperationStatus_OPERATION_SUCCEEDED)
180 	{
181 		cssu::Reference < cssxw::XXMLElementWrapper > xResultEncryption
182 			= xResultTemplate->getTemplate();
183 		m_xSAXEventKeeper->setElement(m_nIdOfTemplateEC, xResultEncryption);
184 		m_xSAXEventKeeper->setElement(m_nReferenceId, NULL);
185 	}
186 }
187 
188 /* XReferenceCollector */
189 void SAL_CALL EncryptorImpl::setReferenceCount(sal_Int32)
190 	throw (cssu::Exception, cssu::RuntimeException)
191 {
192 	/*
193 	 * dummp method, because there is only one reference in
194 	 * encryption, different from signature.
195 	 * so the referenceNumber is always 1
196 	 */
197 }
198 
199 void SAL_CALL EncryptorImpl::setReferenceId( sal_Int32 id )
200 	throw (cssu::Exception, cssu::RuntimeException)
201 {
202 	m_nReferenceId = id;
203 }
204 
205 /* XEncryptionResultBroadcaster */
206 void SAL_CALL EncryptorImpl::addEncryptionResultListener( const cssu::Reference< cssxc::sax::XEncryptionResultListener >& listener )
207     	throw (cssu::Exception, cssu::RuntimeException)
208 {
209 	m_xResultListener = listener;
210 	tryToPerform();
211 }
212 
213 void SAL_CALL EncryptorImpl::removeEncryptionResultListener( const cssu::Reference< cssxc::sax::XEncryptionResultListener >&)
214     	throw (cssu::RuntimeException)
215 {
216 }
217 
218 /* XInitialization */
219 void SAL_CALL EncryptorImpl::initialize( const cssu::Sequence< cssu::Any >& aArguments )
220 	throw (cssu::Exception, cssu::RuntimeException)
221 {
222 	OSL_ASSERT(aArguments.getLength() == 5);
223 
224 	rtl::OUString ouTempString;
225 
226 	aArguments[0] >>= ouTempString;
227 	m_nSecurityId = ouTempString.toInt32();
228 	aArguments[1] >>= m_xSAXEventKeeper;
229 	aArguments[2] >>= ouTempString;
230 	m_nIdOfTemplateEC = ouTempString.toInt32();
231 	aArguments[3] >>= m_xSecurityEnvironment;
232 	aArguments[4] >>= m_xXMLEncryption;
233 }
234 
235 
236 rtl::OUString EncryptorImpl_getImplementationName ()
237 	throw (cssu::RuntimeException)
238 {
239 	return rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) );
240 }
241 
242 sal_Bool SAL_CALL EncryptorImpl_supportsService( const rtl::OUString& ServiceName )
243 	throw (cssu::RuntimeException)
244 {
245 	return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ));
246 }
247 
248 cssu::Sequence< rtl::OUString > SAL_CALL EncryptorImpl_getSupportedServiceNames(  )
249 	throw (cssu::RuntimeException)
250 {
251 	cssu::Sequence < rtl::OUString > aRet(1);
252 	rtl::OUString* pArray = aRet.getArray();
253 	pArray[0] =  rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
254 	return aRet;
255 }
256 #undef SERVICE_NAME
257 
258 cssu::Reference< cssu::XInterface > SAL_CALL EncryptorImpl_createInstance(
259 	const cssu::Reference< cssl::XMultiServiceFactory >& rSMgr)
260 	throw( cssu::Exception )
261 {
262 	return (cppu::OWeakObject*) new EncryptorImpl(rSMgr);
263 }
264 
265 /* XServiceInfo */
266 rtl::OUString SAL_CALL EncryptorImpl::getImplementationName(  )
267 	throw (cssu::RuntimeException)
268 {
269 	return EncryptorImpl_getImplementationName();
270 }
271 sal_Bool SAL_CALL EncryptorImpl::supportsService( const rtl::OUString& rServiceName )
272 	throw (cssu::RuntimeException)
273 {
274 	return EncryptorImpl_supportsService( rServiceName );
275 }
276 cssu::Sequence< rtl::OUString > SAL_CALL EncryptorImpl::getSupportedServiceNames(  )
277 	throw (cssu::RuntimeException)
278 {
279 	return EncryptorImpl_getSupportedServiceNames();
280 }
281 
282