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