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