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 "signaturecreatorimpl.hxx"
32 #include <com/sun/star/xml/crypto/XXMLSignatureTemplate.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.SignatureCreator"
42 #define IMPLEMENTATION_NAME "com.sun.star.xml.security.framework.SignatureCreatorImpl"
43 
44 #define	DECLARE_ASCII( SASCIIVALUE )																			\
45 	rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SASCIIVALUE ) )
46 
47 SignatureCreatorImpl::SignatureCreatorImpl( const cssu::Reference< cssl::XMultiServiceFactory >& rxMSF )
48 	:m_nIdOfBlocker(-1)
49 {
50 	mxMSF = rxMSF;
51 }
52 
53 SignatureCreatorImpl::~SignatureCreatorImpl( )
54 {
55 }
56 
57 bool SignatureCreatorImpl::checkReady() const
58 /****** SignatureCreatorImpl/checkReady **************************************
59  *
60  *   NAME
61  *	checkReady -- checks the conditions for the signature generation.
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 id of the template blocker is known;
70  *	3. the SignatureEngine is ready.
71  *
72  *   INPUTS
73  *	empty
74  *
75  *   RESULT
76  *	bReady - true if all conditions are satisfied, false otherwise
77  *
78  *   HISTORY
79  *	05.01.2004 -	implemented
80  *
81  *   AUTHOR
82  *	Michael Mi
83  *	Email: michael.mi@sun.com
84  ******************************************************************************/
85 {
86 	return (m_xResultListener.is() &&
87 	        (m_nIdOfBlocker != -1) &&
88 	        SignatureEngine::checkReady());
89 }
90 
91 void SignatureCreatorImpl::notifyResultListener() const
92 	throw (cssu::Exception, cssu::RuntimeException)
93 /****** SignatureCreatorImpl/notifyResultListener *****************************
94  *
95  *   NAME
96  *	notifyResultListener -- notifies the listener about the signature
97  *	creation result.
98  *
99  *   SYNOPSIS
100  *	notifyResultListener( );
101  *
102  *   FUNCTION
103  *	see NAME.
104  *
105  *   INPUTS
106  *	empty
107  *
108  *   RESULT
109  *	empty
110  *
111  *   HISTORY
112  *	05.01.2004 -	implemented
113  *
114  *   AUTHOR
115  *	Michael Mi
116  *	Email: michael.mi@sun.com
117  ******************************************************************************/
118 {
119 	cssu::Reference< cssxc::sax::XSignatureCreationResultListener >
120 		xSignatureCreationResultListener ( m_xResultListener , cssu::UNO_QUERY ) ;
121 
122 	xSignatureCreationResultListener->signatureCreated( m_nSecurityId, m_nStatus );
123 }
124 
125 void SignatureCreatorImpl::startEngine(	const cssu::Reference<
126 	cssxc::XXMLSignatureTemplate >&
127 	xSignatureTemplate)
128     	throw (cssu::Exception, cssu::RuntimeException)
129 /****** SignatureCreatorImpl/startEngine *************************************
130  *
131  *   NAME
132  *	startEngine -- generates the signature.
133  *
134  *   SYNOPSIS
135  *	startEngine( xSignatureTemplate );
136  *
137  *   FUNCTION
138  *	generates the signature element, then if succeeds, updates the link
139  *	of old template element to the new signature element in
140  *	SAXEventKeeper.
141  *
142  *   INPUTS
143  *	xSignatureTemplate - the signature template (along with all referenced
144  *	elements) to be signed.
145  *
146  *   RESULT
147  *	empty
148  *
149  *   HISTORY
150  *	05.01.2004 -	implemented
151  *
152  *   AUTHOR
153  *	Michael Mi
154  *	Email: michael.mi@sun.com
155  ******************************************************************************/
156 {
157 	cssu::Reference< cssxc::XXMLSignatureTemplate > xResultTemplate;
158 	try
159 	{
160 		xResultTemplate = m_xXMLSignature->generate(xSignatureTemplate, m_xSecurityEnvironment);
161 		m_nStatus = xResultTemplate->getStatus();
162 	}
163 	catch( cssu::Exception& )
164 	{
165 		m_nStatus = cssxc::SecurityOperationStatus_RUNTIMEERROR_FAILED;
166 	}
167 
168 	if (m_nStatus == cssxc::SecurityOperationStatus_OPERATION_SUCCEEDED)
169 	{
170 		cssu::Reference < cssxw::XXMLElementWrapper > xResultSignature = xResultTemplate->getTemplate();
171 		m_xSAXEventKeeper->setElement(m_nIdOfTemplateEC, xResultSignature);
172 	}
173 }
174 
175 void SignatureCreatorImpl::clearUp() const
176 /****** SignatureCreatorImpl/clearUp *****************************************
177  *
178  *   NAME
179  *	clearUp -- clear up all resources used by the signature generation.
180  *
181  *   SYNOPSIS
182  *	clearUp( );
183  *
184  *   FUNCTION
185  *	cleaning resources up includes:
186  *	1. SignatureEngine's clearing up;
187  *	2. releases the Blocker for the signature template element.
188  *
189  *   INPUTS
190  *	empty
191  *
192  *   RESULT
193  *	empty
194  *
195  *   HISTORY
196  *	05.01.2004 -	implemented
197  *
198  *   AUTHOR
199  *	Michael Mi
200  *	Email: michael.mi@sun.com
201  ******************************************************************************/
202 {
203 	SignatureEngine::clearUp();
204 
205 	if (m_nIdOfBlocker != -1)
206 	{
207 		m_xSAXEventKeeper->removeBlocker(m_nIdOfBlocker);
208 	}
209 }
210 
211 /* XBlockerMonitor */
212 void SAL_CALL SignatureCreatorImpl::setBlockerId( sal_Int32 id )
213     	throw (cssu::Exception, cssu::RuntimeException)
214 {
215 	m_nIdOfBlocker = id;
216 	tryToPerform();
217 }
218 
219 /* XSignatureCreationResultBroadcaster */
220 void SAL_CALL SignatureCreatorImpl::addSignatureCreationResultListener(
221 	const cssu::Reference< cssxc::sax::XSignatureCreationResultListener >& listener )
222 	throw (cssu::Exception, cssu::RuntimeException)
223 {
224 	m_xResultListener = listener;
225 	tryToPerform();
226 }
227 
228 void SAL_CALL SignatureCreatorImpl::removeSignatureCreationResultListener(
229 	const cssu::Reference< cssxc::sax::XSignatureCreationResultListener >&)
230 	throw (cssu::RuntimeException)
231 {
232 }
233 
234 /* XInitialization */
235 void SAL_CALL SignatureCreatorImpl::initialize( const cssu::Sequence< cssu::Any >& aArguments )
236 	throw (cssu::Exception, cssu::RuntimeException)
237 {
238 	OSL_ASSERT(aArguments.getLength() == 5);
239 
240 	rtl::OUString ouTempString;
241 
242 	aArguments[0] >>= ouTempString;
243 	m_nSecurityId = ouTempString.toInt32();
244 	aArguments[1] >>= m_xSAXEventKeeper;
245 	aArguments[2] >>= ouTempString;
246 	m_nIdOfTemplateEC = ouTempString.toInt32();
247 	aArguments[3] >>= m_xSecurityEnvironment;
248 	aArguments[4] >>= m_xXMLSignature;
249 }
250 
251 
252 rtl::OUString SignatureCreatorImpl_getImplementationName ()
253 	throw (cssu::RuntimeException)
254 {
255 	return rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) );
256 }
257 
258 sal_Bool SAL_CALL SignatureCreatorImpl_supportsService( const rtl::OUString& ServiceName )
259 	throw (cssu::RuntimeException)
260 {
261 	return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ));
262 }
263 
264 cssu::Sequence< rtl::OUString > SAL_CALL SignatureCreatorImpl_getSupportedServiceNames(  )
265 	throw (cssu::RuntimeException)
266 {
267 	cssu::Sequence < rtl::OUString > aRet(1);
268 	rtl::OUString* pArray = aRet.getArray();
269 	pArray[0] =  rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
270 	return aRet;
271 }
272 #undef SERVICE_NAME
273 
274 cssu::Reference< cssu::XInterface > SAL_CALL SignatureCreatorImpl_createInstance(
275 	const cssu::Reference< cssl::XMultiServiceFactory >& rSMgr)
276 	throw( cssu::Exception )
277 {
278 	return (cppu::OWeakObject*) new SignatureCreatorImpl( rSMgr );
279 }
280 
281 /* XServiceInfo */
282 rtl::OUString SAL_CALL SignatureCreatorImpl::getImplementationName(  )
283 	throw (cssu::RuntimeException)
284 {
285 	return SignatureCreatorImpl_getImplementationName();
286 }
287 sal_Bool SAL_CALL SignatureCreatorImpl::supportsService( const rtl::OUString& rServiceName )
288 	throw (cssu::RuntimeException)
289 {
290 	return SignatureCreatorImpl_supportsService( rServiceName );
291 }
292 cssu::Sequence< rtl::OUString > SAL_CALL SignatureCreatorImpl::getSupportedServiceNames(  )
293 	throw (cssu::RuntimeException)
294 {
295 	return SignatureCreatorImpl_getSupportedServiceNames();
296 }
297 
298