Update ooo320-m1
[ooovba.git] / xmlsecurity / source / framework / signatureengine.cxx
blob1bd8a412884c2f7be1732c3719f346dd99e969d4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: signatureengine.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
34 #include "signatureengine.hxx"
35 #include <com/sun/star/xml/crypto/XXMLSignatureTemplate.hpp>
36 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39 namespace cssu = com::sun::star::uno;
40 namespace cssl = com::sun::star::lang;
41 namespace cssxc = com::sun::star::xml::crypto;
42 namespace cssxw = com::sun::star::xml::wrapper;
44 #define SIGNATURE_TEMPLATE "com.sun.star.xml.crypto.XMLSignatureTemplate"
46 #define DECLARE_ASCII( SASCIIVALUE ) \
47 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SASCIIVALUE ) )
49 SignatureEngine::SignatureEngine( )
50 :m_nTotalReferenceNumber(-1)
54 bool SignatureEngine::checkReady() const
55 /****** SignatureEngine/checkReady *******************************************
57 * NAME
58 * checkReady -- checks the conditions for the main operation.
60 * SYNOPSIS
61 * bReady = checkReady( );
63 * FUNCTION
64 * checks whether all following conditions are satisfied:
65 * 1. the main operation has't begun yet;
66 * 2. the key material is known;
67 * 3. the amount of reference is known;
68 * 4. all of referenced elements, the key element and the signature
69 * template are bufferred.
71 * INPUTS
72 * empty
74 * RESULT
75 * bReady - true if all conditions are satisfied, false otherwise
77 * HISTORY
78 * 05.01.2004 - implemented
80 * AUTHOR
81 * Michael Mi
82 * Email: michael.mi@sun.com
83 ******************************************************************************/
85 bool rc = true;
87 sal_Int32 nKeyInc = 0;
88 if (m_nIdOfKeyEC != 0)
90 nKeyInc = 1;
93 if (m_bMissionDone ||
94 m_nIdOfKeyEC == -1 ||
95 m_nTotalReferenceNumber == -1 ||
96 m_nTotalReferenceNumber+1+nKeyInc > m_nNumOfResolvedReferences)
98 rc = false;
101 return rc;
104 void SignatureEngine::tryToPerform( )
105 throw (cssu::Exception, cssu::RuntimeException)
106 /****** SignatureEngine/tryToPerform *****************************************
108 * NAME
109 * tryToPerform -- tries to perform the signature operation.
111 * SYNOPSIS
112 * tryToPerform( );
114 * FUNCTION
115 * if the situation is ready, perform following operations.
116 * 1. prepares a signature template;
117 * 2. calls the signature bridge component;
118 * 3. clears up all used resources;
119 * 4. notifies the result listener;
120 * 5. sets the "accomplishment" flag.
122 * INPUTS
123 * empty
125 * RESULT
126 * empty
128 * HISTORY
129 * 05.01.2004 - implemented
131 * AUTHOR
132 * Michael Mi
133 * Email: michael.mi@sun.com
134 ******************************************************************************/
136 if (checkReady())
138 const rtl::OUString ouSignatureTemplate (
139 RTL_CONSTASCII_USTRINGPARAM( SIGNATURE_TEMPLATE ) );
140 cssu::Reference < cssxc::XXMLSignatureTemplate >
141 xSignatureTemplate( mxMSF->createInstance( ouSignatureTemplate ), cssu::UNO_QUERY );
143 OSL_ASSERT( xSignatureTemplate.is() );
145 cssu::Reference< cssxw::XXMLElementWrapper >
146 xXMLElement = m_xSAXEventKeeper->getElement( m_nIdOfTemplateEC );
148 xSignatureTemplate->setTemplate(xXMLElement);
150 std::vector< sal_Int32 >::const_iterator ii = m_vReferenceIds.begin();
152 for( ; ii != m_vReferenceIds.end() ; ++ii )
154 xXMLElement = m_xSAXEventKeeper->getElement( *ii );
155 xSignatureTemplate->setTarget(xXMLElement);
159 * set the Uri binding
161 xSignatureTemplate->setBinding( this );
163 startEngine( xSignatureTemplate );
166 * done
168 clearUp( );
170 notifyResultListener();
172 m_bMissionDone = true;
176 void SignatureEngine::clearUp( ) const
177 /****** SignatureEngine/clearUp **********************************************
179 * NAME
180 * clearUp -- clear up all resources used by this operation.
182 * SYNOPSIS
183 * clearUp( );
185 * FUNCTION
186 * cleaning resources up includes:
187 * 1. releases the ElementCollector for the signature template element;
188 * 2. releases ElementCollectors for referenced elements;
189 * 3. releases the ElementCollector for the key element, if there is one.
191 * INPUTS
192 * empty
194 * RESULT
195 * empty
197 * HISTORY
198 * 05.01.2004 - implemented
200 * AUTHOR
201 * Michael Mi
202 * Email: michael.mi@sun.com
203 ******************************************************************************/
205 cssu::Reference < cssxc::sax::XReferenceResolvedBroadcaster >
206 xReferenceResolvedBroadcaster( m_xSAXEventKeeper, cssu::UNO_QUERY );
207 xReferenceResolvedBroadcaster->removeReferenceResolvedListener(
208 m_nIdOfTemplateEC,
209 (const cssu::Reference < cssxc::sax::XReferenceResolvedListener >)((SecurityEngine *)this));
211 m_xSAXEventKeeper->removeElementCollector(m_nIdOfTemplateEC);
213 std::vector< sal_Int32 >::const_iterator ii = m_vReferenceIds.begin();
215 for( ; ii != m_vReferenceIds.end() ; ++ii )
217 xReferenceResolvedBroadcaster->removeReferenceResolvedListener(
218 *ii,
219 (const cssu::Reference < cssxc::sax::XReferenceResolvedListener >)((SecurityEngine *)this));
220 m_xSAXEventKeeper->removeElementCollector(*ii);
223 if (m_nIdOfKeyEC != 0 && m_nIdOfKeyEC != -1)
225 m_xSAXEventKeeper->removeElementCollector(m_nIdOfKeyEC);
229 /* XReferenceCollector */
230 void SAL_CALL SignatureEngine::setReferenceCount( sal_Int32 count )
231 throw (cssu::Exception, cssu::RuntimeException)
233 m_nTotalReferenceNumber = count;
234 tryToPerform();
237 void SAL_CALL SignatureEngine::setReferenceId( sal_Int32 id )
238 throw (cssu::Exception, cssu::RuntimeException)
240 m_vReferenceIds.push_back( id );
243 /* XUriBinding */
244 void SAL_CALL SignatureEngine::setUriBinding(
245 const rtl::OUString& uri,
246 const cssu::Reference< com::sun::star::io::XInputStream >& aInputStream )
247 throw (cssu::Exception, cssu::RuntimeException)
249 m_vUris.push_back(uri);
250 m_vXInputStreams.push_back(aInputStream);
253 cssu::Reference< com::sun::star::io::XInputStream > SAL_CALL SignatureEngine::getUriBinding( const rtl::OUString& uri )
254 throw (cssu::Exception, cssu::RuntimeException)
256 cssu::Reference< com::sun::star::io::XInputStream > xInputStream;
258 int size = m_vUris.size();
260 for( int i=0; i<size; ++i)
262 if (m_vUris[i] == uri)
264 xInputStream = m_vXInputStreams[i];
265 break;
269 return xInputStream;