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