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