106b3ce53SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
306b3ce53SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
406b3ce53SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
506b3ce53SAndrew Rist  * distributed with this work for additional information
606b3ce53SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
706b3ce53SAndrew Rist  * to you under the Apache License, Version 2.0 (the
806b3ce53SAndrew Rist  * "License"); you may not use this file except in compliance
906b3ce53SAndrew Rist  * with the License.  You may obtain a copy of the License at
1006b3ce53SAndrew Rist  *
1106b3ce53SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1206b3ce53SAndrew Rist  *
1306b3ce53SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1406b3ce53SAndrew Rist  * software distributed under the License is distributed on an
1506b3ce53SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1606b3ce53SAndrew Rist  * KIND, either express or implied.  See the License for the
1706b3ce53SAndrew Rist  * specific language governing permissions and limitations
1806b3ce53SAndrew Rist  * under the License.
1906b3ce53SAndrew Rist  *
2006b3ce53SAndrew Rist  *************************************************************/
2106b3ce53SAndrew Rist 
2206b3ce53SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmlsecurity.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <xsecctl.hxx>
28cdf0e10cSrcweir #include "xsecparser.hxx"
29cdf0e10cSrcweir #include <tools/debug.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <com/sun/star/xml/crypto/sax/XKeyCollector.hpp>
32cdf0e10cSrcweir #include <com/sun/star/xml/crypto/sax/ElementMarkPriority.hpp>
33cdf0e10cSrcweir #include <com/sun/star/xml/crypto/sax/XReferenceResolvedBroadcaster.hpp>
34cdf0e10cSrcweir #include <com/sun/star/xml/crypto/sax/XReferenceCollector.hpp>
35cdf0e10cSrcweir #include <com/sun/star/xml/crypto/sax/XSignatureVerifyResultBroadcaster.hpp>
36cdf0e10cSrcweir #include <com/sun/star/xml/sax/SAXParseException.hpp>
37cdf0e10cSrcweir 
38*202a5bb2SArrigo Marchiori // FIXME: copied from main/sal/rtl/source/strimp.c
39*202a5bb2SArrigo Marchiori static sal_Bool rtl_ImplIsWhitespace( sal_Unicode c )
40*202a5bb2SArrigo Marchiori {
41*202a5bb2SArrigo Marchiori 	/* Space or Control character? */
42*202a5bb2SArrigo Marchiori 	if ( (c <= 32) && c )
43*202a5bb2SArrigo Marchiori 		return sal_True;
44*202a5bb2SArrigo Marchiori 
45*202a5bb2SArrigo Marchiori 	/* Only in the General Punctuation area Space or Control characters are included? */
46*202a5bb2SArrigo Marchiori 	if ( (c < 0x2000) || (c > 0x206F) )
47*202a5bb2SArrigo Marchiori 		return sal_False;
48*202a5bb2SArrigo Marchiori 
49*202a5bb2SArrigo Marchiori 	if ( ((c >= 0x2000) && (c <= 0x200B)) ||	/* All Spaces			*/
50*202a5bb2SArrigo Marchiori 		 (c == 0x2028) ||						/* LINE SEPARATOR		*/
51*202a5bb2SArrigo Marchiori 		 (c == 0x2029) )						/* PARAGRAPH SEPARATOR	*/
52*202a5bb2SArrigo Marchiori 		return sal_True;
53*202a5bb2SArrigo Marchiori 
54*202a5bb2SArrigo Marchiori 	return sal_False;
55*202a5bb2SArrigo Marchiori }
56*202a5bb2SArrigo Marchiori 
57cdf0e10cSrcweir namespace cssu = com::sun::star::uno;
58cdf0e10cSrcweir namespace cssl = com::sun::star::lang;
59cdf0e10cSrcweir namespace cssxc = com::sun::star::xml::crypto;
60cdf0e10cSrcweir namespace cssxs = com::sun::star::xml::sax;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir /* xml security framework components */
63cdf0e10cSrcweir #define SIGNATUREVERIFIER_COMPONENT "com.sun.star.xml.crypto.sax.SignatureVerifier"
64cdf0e10cSrcweir 
65cdf0e10cSrcweir /* protected: for signature verify */
66cdf0e10cSrcweir cssu::Reference< cssxc::sax::XReferenceResolvedListener > XSecController::prepareSignatureToRead(
67cdf0e10cSrcweir 	sal_Int32 nSecurityId)
68cdf0e10cSrcweir {
69cdf0e10cSrcweir 	if ( m_nStatusOfSecurityComponents != INITIALIZED )
70cdf0e10cSrcweir 	{
71cdf0e10cSrcweir 		return NULL;
72cdf0e10cSrcweir 	}
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 	sal_Int32 nIdOfSignatureElementCollector;
75cdf0e10cSrcweir 	cssu::Reference< cssxc::sax::XReferenceResolvedListener > xReferenceResolvedListener;
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 	nIdOfSignatureElementCollector =
78cdf0e10cSrcweir 		m_xSAXEventKeeper->addSecurityElementCollector( cssxc::sax::ElementMarkPriority_BEFOREMODIFY, sal_False);
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 	m_xSAXEventKeeper->setSecurityId(nIdOfSignatureElementCollector, nSecurityId);
81cdf0e10cSrcweir 
82cdf0e10cSrcweir         /*
83cdf0e10cSrcweir          * create a SignatureVerifier
84cdf0e10cSrcweir          */
85cdf0e10cSrcweir 	cssu::Reference< cssl::XMultiComponentFactory > xMCF( mxCtx->getServiceManager() );
86cdf0e10cSrcweir 	xReferenceResolvedListener = cssu::Reference< cssxc::sax::XReferenceResolvedListener >(
87cdf0e10cSrcweir 		xMCF->createInstanceWithContext(
88cdf0e10cSrcweir 			rtl::OUString::createFromAscii( SIGNATUREVERIFIER_COMPONENT ), mxCtx),
89cdf0e10cSrcweir 		cssu::UNO_QUERY);
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 	cssu::Reference<cssl::XInitialization> xInitialization(xReferenceResolvedListener, cssu::UNO_QUERY);
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 	cssu::Sequence<cssu::Any> args(5);
94cdf0e10cSrcweir 	args[0] = cssu::makeAny(rtl::OUString::valueOf(nSecurityId));
95cdf0e10cSrcweir 	args[1] = cssu::makeAny(m_xSAXEventKeeper);
96cdf0e10cSrcweir 	args[2] = cssu::makeAny(rtl::OUString::valueOf(nIdOfSignatureElementCollector));
97cdf0e10cSrcweir 	args[3] = cssu::makeAny(m_xSecurityContext);
98cdf0e10cSrcweir 	args[4] = cssu::makeAny(m_xXMLSignature);
99cdf0e10cSrcweir 	xInitialization->initialize(args);
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 	cssu::Reference< cssxc::sax::XSignatureVerifyResultBroadcaster >
102cdf0e10cSrcweir 		signatureVerifyResultBroadcaster(xReferenceResolvedListener, cssu::UNO_QUERY);
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 	signatureVerifyResultBroadcaster->addSignatureVerifyResultListener( this );
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 	cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
107cdf0e10cSrcweir 		(m_xSAXEventKeeper,
108cdf0e10cSrcweir 		cssu::UNO_QUERY);
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 	xReferenceResolvedBroadcaster->addReferenceResolvedListener(
111cdf0e10cSrcweir 		nIdOfSignatureElementCollector,
112cdf0e10cSrcweir 		xReferenceResolvedListener);
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 	cssu::Reference<cssxc::sax::XKeyCollector> keyCollector (xReferenceResolvedListener, cssu::UNO_QUERY);
115cdf0e10cSrcweir 	keyCollector->setKeyId(0);
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 	return xReferenceResolvedListener;
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir void XSecController::addSignature()
121cdf0e10cSrcweir {
122cdf0e10cSrcweir 	cssu::Reference< cssxc::sax::XReferenceResolvedListener > xReferenceResolvedListener = NULL;
123cdf0e10cSrcweir 	sal_Int32 nSignatureId = 0;
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 	if (m_bVerifyCurrentSignature)
127cdf0e10cSrcweir 	{
128cdf0e10cSrcweir 		chainOn(true);
129cdf0e10cSrcweir 		xReferenceResolvedListener = prepareSignatureToRead( m_nReservedSignatureId );
130cdf0e10cSrcweir 		m_bVerifyCurrentSignature = false;
131cdf0e10cSrcweir 		nSignatureId = m_nReservedSignatureId;
132cdf0e10cSrcweir 	}
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 	InternalSignatureInformation isi( nSignatureId, xReferenceResolvedListener );
135cdf0e10cSrcweir 	m_vInternalSignatureInformations.push_back( isi );
136cdf0e10cSrcweir }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir void XSecController::addReference( const rtl::OUString& ouUri)
139cdf0e10cSrcweir {
140cdf0e10cSrcweir 	InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
141cdf0e10cSrcweir 	isi.addReference(TYPE_SAMEDOCUMENT_REFERENCE,ouUri, -1 );
142cdf0e10cSrcweir }
143cdf0e10cSrcweir 
144cdf0e10cSrcweir void XSecController::addStreamReference(
145cdf0e10cSrcweir 	const rtl::OUString& ouUri,
146cdf0e10cSrcweir 	bool isBinary )
147cdf0e10cSrcweir {
148cdf0e10cSrcweir         sal_Int32 type = (isBinary?TYPE_BINARYSTREAM_REFERENCE:TYPE_XMLSTREAM_REFERENCE);
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 	InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 	if ( isi.xReferenceResolvedListener.is() )
153cdf0e10cSrcweir 	{
154cdf0e10cSrcweir 	        /*
155cdf0e10cSrcweir 	         * get the input stream
156cdf0e10cSrcweir 	         */
157cdf0e10cSrcweir         	cssu::Reference< com::sun::star::io::XInputStream > xObjectInputStream
158cdf0e10cSrcweir         		= getObjectInputStream( ouUri );
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 		if ( xObjectInputStream.is() )
161cdf0e10cSrcweir 		{
162cdf0e10cSrcweir 			cssu::Reference<cssxc::XUriBinding> xUriBinding
163cdf0e10cSrcweir 				(isi.xReferenceResolvedListener, cssu::UNO_QUERY);
164cdf0e10cSrcweir 			xUriBinding->setUriBinding(ouUri, xObjectInputStream);
165cdf0e10cSrcweir 		}
166cdf0e10cSrcweir 	}
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 	isi.addReference(type, ouUri, -1);
169cdf0e10cSrcweir }
170cdf0e10cSrcweir 
171cdf0e10cSrcweir void XSecController::setReferenceCount() const
172cdf0e10cSrcweir {
173cdf0e10cSrcweir 	const InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 	if ( isi.xReferenceResolvedListener.is() )
176cdf0e10cSrcweir 	{
177cdf0e10cSrcweir 		const SignatureReferenceInformations &refInfors = isi.signatureInfor.vSignatureReferenceInfors;
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 		int refNum = refInfors.size();
180cdf0e10cSrcweir 		sal_Int32 referenceCount = 0;
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 		for(int i=0 ; i<refNum; ++i)
183cdf0e10cSrcweir 		{
184cdf0e10cSrcweir 			if (refInfors[i].nType == TYPE_SAMEDOCUMENT_REFERENCE )
185cdf0e10cSrcweir 			/*
186cdf0e10cSrcweir 			 * same-document reference
187cdf0e10cSrcweir 			 */
188cdf0e10cSrcweir 			{
189cdf0e10cSrcweir 				referenceCount++;
190cdf0e10cSrcweir 			}
191cdf0e10cSrcweir 		}
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 		cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
194cdf0e10cSrcweir 			(isi.xReferenceResolvedListener, cssu::UNO_QUERY);
195cdf0e10cSrcweir 		xReferenceCollector->setReferenceCount( referenceCount );
196cdf0e10cSrcweir 	}
197cdf0e10cSrcweir }
198cdf0e10cSrcweir 
19955779ad0SArrigo Marchiori void XSecController::setIfEmpty(rtl::OUString &variable, const rtl::OUString &value) {
20055779ad0SArrigo Marchiori     if (variable.getLength() == 0) {
20155779ad0SArrigo Marchiori         variable = value;
20255779ad0SArrigo Marchiori     } else if (variable != value) {
20355779ad0SArrigo Marchiori         throw cssu::RuntimeException(rtl::OUString::createFromAscii("Value already set. Tampering?"), *this);
20455779ad0SArrigo Marchiori     }
20555779ad0SArrigo Marchiori }
20655779ad0SArrigo Marchiori 
207cdf0e10cSrcweir void XSecController::setX509IssuerName( rtl::OUString& ouX509IssuerName )
208cdf0e10cSrcweir {
209cdf0e10cSrcweir 	InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
21055779ad0SArrigo Marchiori 	setIfEmpty(isi.signatureInfor.ouX509IssuerName, ouX509IssuerName);
211cdf0e10cSrcweir }
212cdf0e10cSrcweir 
213cdf0e10cSrcweir void XSecController::setX509SerialNumber( rtl::OUString& ouX509SerialNumber )
214cdf0e10cSrcweir {
215cdf0e10cSrcweir 	InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
21655779ad0SArrigo Marchiori 	setIfEmpty(isi.signatureInfor.ouX509SerialNumber, ouX509SerialNumber);
217cdf0e10cSrcweir }
218cdf0e10cSrcweir 
219cdf0e10cSrcweir void XSecController::setX509Certificate( rtl::OUString& ouX509Certificate )
220cdf0e10cSrcweir {
221cdf0e10cSrcweir 	InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
222*202a5bb2SArrigo Marchiori     setIfEmpty(isi.signatureInfor.ouX509Certificate, ouX509Certificate);
223*202a5bb2SArrigo Marchiori     if (isi.signatureInfor.ouX509Certificate.getLength()) {
224*202a5bb2SArrigo Marchiori         // We allow to re-set the same certificate only.
225*202a5bb2SArrigo Marchiori         // Whitespace may change.
226*202a5bb2SArrigo Marchiori         const sal_Int32 l1 = isi.signatureInfor.ouX509Certificate.getLength();
227*202a5bb2SArrigo Marchiori         const sal_Int32 l2 = ouX509Certificate.getLength();
228*202a5bb2SArrigo Marchiori         const sal_Unicode *s1 = isi.signatureInfor.ouX509Certificate.getStr();
229*202a5bb2SArrigo Marchiori         const sal_Unicode *s2 = ouX509Certificate.getStr();
230*202a5bb2SArrigo Marchiori         sal_Int32 i1 = 0, i2 = 0;
231*202a5bb2SArrigo Marchiori         while ((i1 < l1) && (i2 < l2)) {
232*202a5bb2SArrigo Marchiori             const sal_Unicode &c1 = s1[i1];
233*202a5bb2SArrigo Marchiori             const sal_Unicode &c2 = s2[i2];
234*202a5bb2SArrigo Marchiori             if (rtl_ImplIsWhitespace(c1)) {
235*202a5bb2SArrigo Marchiori                 ++i1;
236*202a5bb2SArrigo Marchiori                 continue;
237*202a5bb2SArrigo Marchiori             }
238*202a5bb2SArrigo Marchiori             if (rtl_ImplIsWhitespace(c2)) {
239*202a5bb2SArrigo Marchiori                 ++i2;
240*202a5bb2SArrigo Marchiori                 continue;
241*202a5bb2SArrigo Marchiori             }
242*202a5bb2SArrigo Marchiori             if (c1 != c2) {
243*202a5bb2SArrigo Marchiori                 throw cssu::RuntimeException(rtl::OUString::createFromAscii("Value already set. Tampering?"), *this);
244*202a5bb2SArrigo Marchiori             }
245*202a5bb2SArrigo Marchiori             ++i1;
246*202a5bb2SArrigo Marchiori             ++i2;
247*202a5bb2SArrigo Marchiori         }
248*202a5bb2SArrigo Marchiori         // We could still have whitespace at the end of both strings
249*202a5bb2SArrigo Marchiori         while ((i1 < l1) && rtl_ImplIsWhitespace(s1[l1])) {
250*202a5bb2SArrigo Marchiori             ++i1;
251*202a5bb2SArrigo Marchiori         }
252*202a5bb2SArrigo Marchiori         while ((i2 < l2) && rtl_ImplIsWhitespace(s2[l2])) {
253*202a5bb2SArrigo Marchiori             ++i2;
254*202a5bb2SArrigo Marchiori         }
255*202a5bb2SArrigo Marchiori         if ((i1 != l1) || (i2 != l2)) {
256*202a5bb2SArrigo Marchiori             throw cssu::RuntimeException(rtl::OUString::createFromAscii("Value already set. Tampering?"), *this);
257*202a5bb2SArrigo Marchiori         }
258*202a5bb2SArrigo Marchiori     }
259*202a5bb2SArrigo Marchiori     isi.signatureInfor.ouX509Certificate = ouX509Certificate;
260cdf0e10cSrcweir }
261cdf0e10cSrcweir 
262cdf0e10cSrcweir void XSecController::setSignatureValue( rtl::OUString& ouSignatureValue )
263cdf0e10cSrcweir {
264cdf0e10cSrcweir 	InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
265cdf0e10cSrcweir 	isi.signatureInfor.ouSignatureValue = ouSignatureValue;
266cdf0e10cSrcweir }
267cdf0e10cSrcweir 
268cdf0e10cSrcweir void XSecController::setDigestValue( rtl::OUString& ouDigestValue )
269cdf0e10cSrcweir {
270cdf0e10cSrcweir 	SignatureInformation &si = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1].signatureInfor;
271cdf0e10cSrcweir 	SignatureReferenceInformation &reference = si.vSignatureReferenceInfors[si.vSignatureReferenceInfors.size()-1];
272cdf0e10cSrcweir 	reference.ouDigestValue = ouDigestValue;
273cdf0e10cSrcweir }
274cdf0e10cSrcweir 
275cdf0e10cSrcweir void XSecController::setDate( rtl::OUString& ouDate )
276cdf0e10cSrcweir {
277cdf0e10cSrcweir 	InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
278cdf0e10cSrcweir 	convertDateTime( isi.signatureInfor.stDateTime, ouDate );
27955779ad0SArrigo Marchiori 	setIfEmpty(isi.signatureInfor.ouDateTime, ouDate);
280cdf0e10cSrcweir }
281cdf0e10cSrcweir 
282cdf0e10cSrcweir /*
283cdf0e10cSrcweir void XSecController::setTime( rtl::OUString& ouTime )
284cdf0e10cSrcweir {
285cdf0e10cSrcweir 	InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
286cdf0e10cSrcweir 	isi.signatureInfor.ouTime = ouTime;
287cdf0e10cSrcweir }
288cdf0e10cSrcweir */
289cdf0e10cSrcweir 
290cdf0e10cSrcweir void XSecController::setId( rtl::OUString& ouId )
291cdf0e10cSrcweir {
292cdf0e10cSrcweir 	InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
293cdf0e10cSrcweir 	isi.signatureInfor.ouSignatureId = ouId;
294cdf0e10cSrcweir }
295cdf0e10cSrcweir 
296cdf0e10cSrcweir void XSecController::setPropertyId( rtl::OUString& ouPropertyId )
297cdf0e10cSrcweir {
298cdf0e10cSrcweir 	InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
299cdf0e10cSrcweir 	isi.signatureInfor.ouPropertyId = ouPropertyId;
300cdf0e10cSrcweir }
301cdf0e10cSrcweir 
302cdf0e10cSrcweir /* public: for signature verify */
303cdf0e10cSrcweir void XSecController::collectToVerify( const rtl::OUString& referenceId )
304cdf0e10cSrcweir {
305cdf0e10cSrcweir 	/* DBG_ASSERT( m_xSAXEventKeeper.is(), "the SAXEventKeeper is NULL" ); */
306cdf0e10cSrcweir 
307cdf0e10cSrcweir 	if ( m_nStatusOfSecurityComponents == INITIALIZED )
308cdf0e10cSrcweir 	/*
309cdf0e10cSrcweir 	 * if all security components are ready, verify the signature.
310cdf0e10cSrcweir 	 */
311cdf0e10cSrcweir 	{
312cdf0e10cSrcweir 		bool bJustChainingOn = false;
313cdf0e10cSrcweir 		cssu::Reference< cssxs::XDocumentHandler > xHandler = NULL;
314cdf0e10cSrcweir 
315cdf0e10cSrcweir 		int i,j;
316cdf0e10cSrcweir 		int sigNum = m_vInternalSignatureInformations.size();
317cdf0e10cSrcweir 
318cdf0e10cSrcweir 		for (i=0; i<sigNum; ++i)
319cdf0e10cSrcweir 		{
320cdf0e10cSrcweir 			InternalSignatureInformation& isi = m_vInternalSignatureInformations[i];
321cdf0e10cSrcweir 			SignatureReferenceInformations& vReferenceInfors = isi.signatureInfor.vSignatureReferenceInfors;
322cdf0e10cSrcweir 			int refNum = vReferenceInfors.size();
323cdf0e10cSrcweir 
324cdf0e10cSrcweir 			for (j=0; j<refNum; ++j)
325cdf0e10cSrcweir 			{
326cdf0e10cSrcweir 				SignatureReferenceInformation &refInfor = vReferenceInfors[j];
327cdf0e10cSrcweir 
328cdf0e10cSrcweir 				if (refInfor.ouURI == referenceId)
329cdf0e10cSrcweir 				{
330cdf0e10cSrcweir 					if (chainOn(false))
331cdf0e10cSrcweir 					{
332cdf0e10cSrcweir 						bJustChainingOn = true;
333cdf0e10cSrcweir 						xHandler = m_xSAXEventKeeper->setNextHandler(NULL);
334cdf0e10cSrcweir 					}
335cdf0e10cSrcweir 
336cdf0e10cSrcweir 					sal_Int32 nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
337cdf0e10cSrcweir 						cssxc::sax::ElementMarkPriority_BEFOREMODIFY, sal_False );
338cdf0e10cSrcweir 
339cdf0e10cSrcweir 					cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
340cdf0e10cSrcweir 						(m_xSAXEventKeeper,
341cdf0e10cSrcweir 						cssu::UNO_QUERY );
342cdf0e10cSrcweir 
343cdf0e10cSrcweir 					cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
344cdf0e10cSrcweir 						( isi.xReferenceResolvedListener, cssu::UNO_QUERY );
345cdf0e10cSrcweir 
346cdf0e10cSrcweir 					m_xSAXEventKeeper->setSecurityId(nKeeperId, isi.signatureInfor.nSecurityId);
347cdf0e10cSrcweir 					xReferenceResolvedBroadcaster->addReferenceResolvedListener( nKeeperId, isi.xReferenceResolvedListener);
348cdf0e10cSrcweir 					xReferenceCollector->setReferenceId( nKeeperId );
349cdf0e10cSrcweir 
350cdf0e10cSrcweir 					isi.vKeeperIds[j] = nKeeperId;
351cdf0e10cSrcweir 					break;
352cdf0e10cSrcweir 				}
353cdf0e10cSrcweir 			}
354cdf0e10cSrcweir 		}
355cdf0e10cSrcweir 
356cdf0e10cSrcweir 		if ( bJustChainingOn )
357cdf0e10cSrcweir 		{
358cdf0e10cSrcweir 			cssu::Reference< cssxs::XDocumentHandler > xSEKHandler(m_xSAXEventKeeper, cssu::UNO_QUERY);
359cdf0e10cSrcweir 			if (m_xElementStackKeeper.is())
360cdf0e10cSrcweir 			{
361cdf0e10cSrcweir 				m_xElementStackKeeper->retrieve(xSEKHandler, sal_True);
362cdf0e10cSrcweir 			}
363cdf0e10cSrcweir 			m_xSAXEventKeeper->setNextHandler(xHandler);
364cdf0e10cSrcweir 		}
365cdf0e10cSrcweir 	}
366cdf0e10cSrcweir }
367cdf0e10cSrcweir 
368cdf0e10cSrcweir void XSecController::addSignature( sal_Int32 nSignatureId )
369cdf0e10cSrcweir {
370cdf0e10cSrcweir 	DBG_ASSERT( m_pXSecParser != NULL, "No XSecParser initialized" );
371cdf0e10cSrcweir 
372cdf0e10cSrcweir 	m_nReservedSignatureId = nSignatureId;
373cdf0e10cSrcweir 	m_bVerifyCurrentSignature = true;
374cdf0e10cSrcweir }
375cdf0e10cSrcweir 
376cdf0e10cSrcweir cssu::Reference< cssxs::XDocumentHandler > XSecController::createSignatureReader()
377cdf0e10cSrcweir {
378cdf0e10cSrcweir 	m_pXSecParser = new XSecParser( this, NULL );
379cdf0e10cSrcweir 	cssu::Reference< cssl::XInitialization > xInitialization = m_pXSecParser;
380cdf0e10cSrcweir 
381cdf0e10cSrcweir 	setSAXChainConnector(xInitialization, NULL, NULL);
382cdf0e10cSrcweir 
383cdf0e10cSrcweir 	return m_pXSecParser;
384cdf0e10cSrcweir }
385cdf0e10cSrcweir 
386cdf0e10cSrcweir void XSecController::releaseSignatureReader()
387cdf0e10cSrcweir {
388cdf0e10cSrcweir 	clearSAXChainConnector( );
389cdf0e10cSrcweir 	m_pXSecParser = NULL;
390cdf0e10cSrcweir }
391cdf0e10cSrcweir 
392