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 "signatureengine.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 SIGNATURE_TEMPLATE "com.sun.star.xml.crypto.XMLSignatureTemplate"
38 
39 #define	DECLARE_ASCII( SASCIIVALUE )																			\
40 	rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SASCIIVALUE ) )
41 
SignatureEngine()42 SignatureEngine::SignatureEngine( )
43 	:m_nTotalReferenceNumber(-1)
44 {
45 }
46 
checkReady() const47 bool SignatureEngine::checkReady() const
48 /****** SignatureEngine/checkReady *******************************************
49  *
50  *   NAME
51  *	checkReady -- checks the conditions for the main operation.
52  *
53  *   SYNOPSIS
54  *	bReady = checkReady( );
55  *
56  *   FUNCTION
57  *	checks whether all following conditions are satisfied:
58  *	1. the main operation has't begun yet;
59  *	2. the key material is known;
60  *	3. the amount of reference is known;
61  *	4. all of referenced elements, the key element and the signature
62  *	   template are bufferred.
63  *
64  *   INPUTS
65  *	empty
66  *
67  *   RESULT
68  *	bReady - true if all conditions are satisfied, false otherwise
69  *
70  *   HISTORY
71  *	05.01.2004 -	implemented
72  *
73  *   AUTHOR
74  *	Michael Mi
75  *	Email: michael.mi@sun.com
76  ******************************************************************************/
77 {
78 	bool rc = true;
79 
80 	sal_Int32 nKeyInc = 0;
81 	if (m_nIdOfKeyEC != 0)
82 	{
83 		nKeyInc = 1;
84 	}
85 
86 	if (m_bMissionDone ||
87 	    m_nIdOfKeyEC == -1 ||
88 	    m_nTotalReferenceNumber == -1 ||
89 	    m_nTotalReferenceNumber+1+nKeyInc > m_nNumOfResolvedReferences)
90 	{
91 		rc = false;
92 	}
93 
94 	return rc;
95 }
96 
tryToPerform()97 void SignatureEngine::tryToPerform( )
98     	throw (cssu::Exception, cssu::RuntimeException)
99 /****** SignatureEngine/tryToPerform *****************************************
100  *
101  *   NAME
102  *	tryToPerform -- tries to perform the signature operation.
103  *
104  *   SYNOPSIS
105  *	tryToPerform( );
106  *
107  *   FUNCTION
108  *	if the situation is ready, perform following operations.
109  *	1. prepares a signature template;
110  *	2. calls the signature bridge component;
111  *	3. clears up all used resources;
112  *	4. notifies the result listener;
113  *	5. sets the "accomplishment" flag.
114  *
115  *   INPUTS
116  *	empty
117  *
118  *   RESULT
119  *	empty
120  *
121  *   HISTORY
122  *	05.01.2004 -	implemented
123  *
124  *   AUTHOR
125  *	Michael Mi
126  *	Email: michael.mi@sun.com
127  ******************************************************************************/
128 {
129 	if (checkReady())
130 	{
131 		const rtl::OUString ouSignatureTemplate (
132 			RTL_CONSTASCII_USTRINGPARAM( SIGNATURE_TEMPLATE ) );
133 		cssu::Reference < cssxc::XXMLSignatureTemplate >
134 			xSignatureTemplate( mxMSF->createInstance( ouSignatureTemplate ), cssu::UNO_QUERY );
135 
136 		OSL_ASSERT( xSignatureTemplate.is() );
137 
138 		cssu::Reference< cssxw::XXMLElementWrapper >
139 			xXMLElement = m_xSAXEventKeeper->getElement( m_nIdOfTemplateEC );
140 
141 		xSignatureTemplate->setTemplate(xXMLElement);
142 
143 		std::vector< sal_Int32 >::const_iterator ii = m_vReferenceIds.begin();
144 
145 		for( ; ii != m_vReferenceIds.end() ; ++ii )
146 		{
147 			xXMLElement = m_xSAXEventKeeper->getElement( *ii );
148 			xSignatureTemplate->setTarget(xXMLElement);
149 		}
150 
151 		/*
152 		 * set the Uri binding
153 		 */
154 		xSignatureTemplate->setBinding( this );
155 
156 		startEngine( xSignatureTemplate );
157 
158 		/*
159 		 * done
160 		 */
161 		clearUp( );
162 
163 		notifyResultListener();
164 
165 		m_bMissionDone = true;
166 	}
167 }
168 
clearUp() const169 void SignatureEngine::clearUp( ) const
170 /****** SignatureEngine/clearUp **********************************************
171  *
172  *   NAME
173  *	clearUp -- clear up all resources used by this operation.
174  *
175  *   SYNOPSIS
176  *	clearUp( );
177  *
178  *   FUNCTION
179  *	cleaning resources up includes:
180  *	1. releases the ElementCollector for the signature template element;
181  *	2. releases ElementCollectors for referenced elements;
182  *	3. releases the ElementCollector for the key element, if there is one.
183  *
184  *   INPUTS
185  *	empty
186  *
187  *   RESULT
188  *	empty
189  *
190  *   HISTORY
191  *	05.01.2004 -	implemented
192  *
193  *   AUTHOR
194  *	Michael Mi
195  *	Email: michael.mi@sun.com
196  ******************************************************************************/
197 {
198 	cssu::Reference < cssxc::sax::XReferenceResolvedBroadcaster >
199 		xReferenceResolvedBroadcaster( m_xSAXEventKeeper, cssu::UNO_QUERY );
200 	xReferenceResolvedBroadcaster->removeReferenceResolvedListener(
201 		m_nIdOfTemplateEC,
202 		(const cssu::Reference < cssxc::sax::XReferenceResolvedListener >)((SecurityEngine *)this));
203 
204 	m_xSAXEventKeeper->removeElementCollector(m_nIdOfTemplateEC);
205 
206 	std::vector< sal_Int32 >::const_iterator ii = m_vReferenceIds.begin();
207 
208 	for( ; ii != m_vReferenceIds.end() ; ++ii )
209 	{
210 		xReferenceResolvedBroadcaster->removeReferenceResolvedListener(
211 			*ii,
212 			(const cssu::Reference < cssxc::sax::XReferenceResolvedListener >)((SecurityEngine *)this));
213 		m_xSAXEventKeeper->removeElementCollector(*ii);
214 	}
215 
216 	if (m_nIdOfKeyEC != 0 && m_nIdOfKeyEC != -1)
217 	{
218 		m_xSAXEventKeeper->removeElementCollector(m_nIdOfKeyEC);
219 	}
220 }
221 
222 /* XReferenceCollector */
setReferenceCount(sal_Int32 count)223 void SAL_CALL SignatureEngine::setReferenceCount( sal_Int32 count )
224 	throw (cssu::Exception, cssu::RuntimeException)
225 {
226 	m_nTotalReferenceNumber = count;
227 	tryToPerform();
228 }
229 
setReferenceId(sal_Int32 id)230 void SAL_CALL SignatureEngine::setReferenceId( sal_Int32 id )
231 	throw (cssu::Exception, cssu::RuntimeException)
232 {
233 	m_vReferenceIds.push_back( id );
234 }
235 
236 /* XUriBinding */
setUriBinding(const rtl::OUString & uri,const cssu::Reference<com::sun::star::io::XInputStream> & aInputStream)237 void SAL_CALL SignatureEngine::setUriBinding(
238 	const rtl::OUString& uri,
239 	const cssu::Reference< com::sun::star::io::XInputStream >& aInputStream )
240 	throw (cssu::Exception, cssu::RuntimeException)
241 {
242 	m_vUris.push_back(uri);
243 	m_vXInputStreams.push_back(aInputStream);
244 }
245 
getUriBinding(const rtl::OUString & uri)246 cssu::Reference< com::sun::star::io::XInputStream > SAL_CALL SignatureEngine::getUriBinding( const rtl::OUString& uri )
247 	throw (cssu::Exception, cssu::RuntimeException)
248 {
249 	cssu::Reference< com::sun::star::io::XInputStream > xInputStream;
250 
251 	int size = m_vUris.size();
252 
253 	for( int i=0; i<size; ++i)
254 	{
255 		if (m_vUris[i] == uri)
256 		{
257 			xInputStream = m_vXInputStreams[i];
258 			break;
259 		}
260 	}
261 
262 	return xInputStream;
263 }
264 
265