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