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 <xmlsecurity/xmlsignaturehelper.hxx>
28cdf0e10cSrcweir #include <xmlsecurity/documentsignaturehelper.hxx>
29cdf0e10cSrcweir #include <xsecctl.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <xmlsignaturehelper2.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <tools/stream.hxx>
34cdf0e10cSrcweir #include <tools/debug.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <xmloff/attrlist.hxx>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
39cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
40cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
41cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
42cdf0e10cSrcweir #include <com/sun/star/security/SerialNumberAdapter.hpp>
43cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #include <tools/date.hxx>
46cdf0e10cSrcweir #include <tools/time.hxx>
47cdf0e10cSrcweir 
48cdf0e10cSrcweir //MM : search for the default profile
49cdf0e10cSrcweir //#include <unotools/streamhelper.hxx>
50cdf0e10cSrcweir //MM : end
51cdf0e10cSrcweir 
52cdf0e10cSrcweir /* SEInitializer component */
53cdf0e10cSrcweir #define SEINITIALIZER_COMPONENT "com.sun.star.xml.crypto.SEInitializer"
54cdf0e10cSrcweir 
55cdf0e10cSrcweir #define TAG_DOCUMENTSIGNATURES	"document-signatures"
56cdf0e10cSrcweir #define NS_DOCUMENTSIGNATURES	"http://openoffice.org/2004/documentsignatures"
57cdf0e10cSrcweir #define NS_DOCUMENTSIGNATURES_ODF_1_2 "urn:oasis:names:tc:opendocument:xmlns:digitalsignature:1.0"
58cdf0e10cSrcweir 
59cdf0e10cSrcweir using namespace ::com::sun::star;
60cdf0e10cSrcweir using namespace ::com::sun::star::uno;
61cdf0e10cSrcweir 
XMLSignatureHelper(const uno::Reference<uno::XComponentContext> & rxCtx)62cdf0e10cSrcweir XMLSignatureHelper::XMLSignatureHelper( const uno::Reference< uno::XComponentContext >& rxCtx)
63cdf0e10cSrcweir     : mxCtx(rxCtx), mbODFPre1_2(false)
64cdf0e10cSrcweir {
65cdf0e10cSrcweir     mpXSecController = new XSecController(rxCtx);
66cdf0e10cSrcweir     mxSecurityController = mpXSecController;
67cdf0e10cSrcweir     mbError = false;
68cdf0e10cSrcweir }
69cdf0e10cSrcweir 
~XMLSignatureHelper()70cdf0e10cSrcweir XMLSignatureHelper::~XMLSignatureHelper()
71cdf0e10cSrcweir {
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
Init()74cdf0e10cSrcweir bool XMLSignatureHelper::Init()
75cdf0e10cSrcweir {
76cdf0e10cSrcweir     DBG_ASSERT( !mxSEInitializer.is(), "XMLSignatureHelper::Init - mxSEInitializer already set!" );
77cdf0e10cSrcweir     DBG_ASSERT( !mxSecurityContext.is(), "XMLSignatureHelper::Init - mxSecurityContext already set!" );
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     ImplCreateSEInitializer();
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 	if ( mxSEInitializer.is() )
82cdf0e10cSrcweir 		mxSecurityContext = mxSEInitializer->createSecurityContext( ::rtl::OUString() );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     return mxSecurityContext.is();
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
ImplCreateSEInitializer()87cdf0e10cSrcweir void XMLSignatureHelper::ImplCreateSEInitializer()
88cdf0e10cSrcweir {
89cdf0e10cSrcweir     rtl::OUString sSEInitializer(rtl::OUString::createFromAscii( SEINITIALIZER_COMPONENT ));
90cdf0e10cSrcweir     uno::Reference< lang::XMultiComponentFactory > xMCF( mxCtx->getServiceManager() );
91cdf0e10cSrcweir     mxSEInitializer = uno::Reference< com::sun::star::xml::crypto::XSEInitializer > (
92cdf0e10cSrcweir         xMCF->createInstanceWithContext( sSEInitializer,  mxCtx ), uno::UNO_QUERY );
93cdf0e10cSrcweir }
94cdf0e10cSrcweir 
SetUriBinding(com::sun::star::uno::Reference<com::sun::star::xml::crypto::XUriBinding> & rxUriBinding)95cdf0e10cSrcweir void XMLSignatureHelper::SetUriBinding( com::sun::star::uno::Reference< com::sun::star::xml::crypto::XUriBinding >& rxUriBinding )
96cdf0e10cSrcweir {
97cdf0e10cSrcweir     mxUriBinding = rxUriBinding;
98cdf0e10cSrcweir }
99cdf0e10cSrcweir 
GetUriBinding() const100cdf0e10cSrcweir com::sun::star::uno::Reference< com::sun::star::xml::crypto::XUriBinding > XMLSignatureHelper::GetUriBinding() const
101cdf0e10cSrcweir {
102cdf0e10cSrcweir     return mxUriBinding;
103cdf0e10cSrcweir }
104cdf0e10cSrcweir 
SetStorage(const Reference<css::embed::XStorage> & rxStorage,::rtl::OUString sODFVersion)105cdf0e10cSrcweir void XMLSignatureHelper::SetStorage(
106cdf0e10cSrcweir     const Reference < css::embed::XStorage >& rxStorage,
107cdf0e10cSrcweir     ::rtl::OUString sODFVersion)
108cdf0e10cSrcweir {
109cdf0e10cSrcweir     DBG_ASSERT( !mxUriBinding.is(), "SetStorage - UriBinding already set!" );
110cdf0e10cSrcweir     mxUriBinding = new UriBindingHelper( rxStorage );
111cdf0e10cSrcweir     DBG_ASSERT(rxStorage.is(), "SetStorage - empty storage!");
112cdf0e10cSrcweir     mbODFPre1_2 = DocumentSignatureHelper::isODFPre_1_2(sODFVersion);
113cdf0e10cSrcweir }
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 
SetStartVerifySignatureHdl(const Link & rLink)116cdf0e10cSrcweir void XMLSignatureHelper::SetStartVerifySignatureHdl( const Link& rLink )
117cdf0e10cSrcweir {
118cdf0e10cSrcweir     maStartVerifySignatureHdl = rLink;
119cdf0e10cSrcweir }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 
StartMission()122cdf0e10cSrcweir void XMLSignatureHelper::StartMission()
123cdf0e10cSrcweir {
124cdf0e10cSrcweir     if ( !mxUriBinding.is() )
125cdf0e10cSrcweir         mxUriBinding = new UriBindingHelper();
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     mpXSecController->startMission( mxUriBinding, mxSecurityContext );
128cdf0e10cSrcweir }
129cdf0e10cSrcweir 
EndMission()130cdf0e10cSrcweir void XMLSignatureHelper::EndMission()
131cdf0e10cSrcweir {
132cdf0e10cSrcweir     mpXSecController->endMission();
133cdf0e10cSrcweir }
134cdf0e10cSrcweir 
GetNewSecurityId()135cdf0e10cSrcweir sal_Int32 XMLSignatureHelper::GetNewSecurityId()
136cdf0e10cSrcweir {
137cdf0e10cSrcweir     return mpXSecController->getNewSecurityId();
138cdf0e10cSrcweir }
139cdf0e10cSrcweir 
SetX509Certificate(sal_Int32 nSecurityId,const rtl::OUString & ouX509IssuerName,const rtl::OUString & ouX509SerialNumber,const rtl::OUString & ouX509Cert)140cdf0e10cSrcweir void XMLSignatureHelper::SetX509Certificate(
141cdf0e10cSrcweir 		sal_Int32 nSecurityId,
142cdf0e10cSrcweir 		const rtl::OUString& ouX509IssuerName,
143cdf0e10cSrcweir 		const rtl::OUString& ouX509SerialNumber,
144cdf0e10cSrcweir 		const rtl::OUString& ouX509Cert)
145cdf0e10cSrcweir {
146cdf0e10cSrcweir 	mpXSecController->setX509Certificate(
147cdf0e10cSrcweir 		nSecurityId,
148cdf0e10cSrcweir 		ouX509IssuerName,
149cdf0e10cSrcweir 		ouX509SerialNumber,
150cdf0e10cSrcweir 		ouX509Cert);
151cdf0e10cSrcweir }
152cdf0e10cSrcweir 
SetX509Certificate(sal_Int32 nSecurityId,sal_Int32 nSecurityEnvironmentIndex,const rtl::OUString & ouX509IssuerName,const rtl::OUString & ouX509SerialNumber,const rtl::OUString & ouX509Cert)153cdf0e10cSrcweir void XMLSignatureHelper::SetX509Certificate(
154cdf0e10cSrcweir 		sal_Int32 nSecurityId,
155cdf0e10cSrcweir 		sal_Int32 nSecurityEnvironmentIndex,
156cdf0e10cSrcweir 		const rtl::OUString& ouX509IssuerName,
157cdf0e10cSrcweir 		const rtl::OUString& ouX509SerialNumber,
158cdf0e10cSrcweir 		const rtl::OUString& ouX509Cert)
159cdf0e10cSrcweir {
160cdf0e10cSrcweir 	mpXSecController->setX509Certificate(
161cdf0e10cSrcweir 		nSecurityId,
162cdf0e10cSrcweir 		nSecurityEnvironmentIndex,
163cdf0e10cSrcweir 		ouX509IssuerName,
164cdf0e10cSrcweir 		ouX509SerialNumber,
165cdf0e10cSrcweir 		ouX509Cert);
166cdf0e10cSrcweir }
167cdf0e10cSrcweir 
SetDateTime(sal_Int32 nSecurityId,const Date & rDate,const Time & rTime)168cdf0e10cSrcweir void XMLSignatureHelper::SetDateTime( sal_Int32 nSecurityId, const Date& rDate, const Time& rTime )
169cdf0e10cSrcweir {
170cdf0e10cSrcweir 	/*
171cdf0e10cSrcweir     rtl::OUString aDate = String::CreateFromInt32( rDate.GetDate() );
172cdf0e10cSrcweir     rtl::OUString aTime = String::CreateFromInt32( rTime.GetTime() );
173cdf0e10cSrcweir 	mpXSecController->setDateTime( nSecurityId, aDate, aTime );
174cdf0e10cSrcweir 	*/
175cdf0e10cSrcweir 	::com::sun::star::util::DateTime stDateTime;
176cdf0e10cSrcweir 	stDateTime.HundredthSeconds = (::sal_uInt16)rTime.Get100Sec();
177cdf0e10cSrcweir 	stDateTime.Seconds = (::sal_uInt16)rTime.GetSec();
178cdf0e10cSrcweir 	stDateTime.Minutes = (::sal_uInt16)rTime.GetMin();
179cdf0e10cSrcweir 	stDateTime.Hours = (::sal_uInt16)rTime.GetHour();
180cdf0e10cSrcweir 	stDateTime.Day = (::sal_uInt16)rDate.GetDay();
181cdf0e10cSrcweir 	stDateTime.Month = (::sal_uInt16)rDate.GetMonth();
182cdf0e10cSrcweir 	stDateTime.Year = (::sal_uInt16)rDate.GetYear();
183cdf0e10cSrcweir 	mpXSecController->setDate( nSecurityId, stDateTime );
184cdf0e10cSrcweir }
185cdf0e10cSrcweir 
AddForSigning(sal_Int32 nSecurityId,const rtl::OUString & uri,const rtl::OUString & objectURL,sal_Bool bBinary)186cdf0e10cSrcweir void XMLSignatureHelper::AddForSigning( sal_Int32 nSecurityId, const rtl::OUString& uri, const rtl::OUString& objectURL, sal_Bool bBinary )
187cdf0e10cSrcweir {
188cdf0e10cSrcweir 	mpXSecController->signAStream( nSecurityId, uri, objectURL, bBinary );
189cdf0e10cSrcweir }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 
CreateDocumentHandlerWithHeader(const com::sun::star::uno::Reference<com::sun::star::io::XOutputStream> & xOutputStream)192cdf0e10cSrcweir uno::Reference<xml::sax::XDocumentHandler> XMLSignatureHelper::CreateDocumentHandlerWithHeader(
193cdf0e10cSrcweir 	const com::sun::star::uno::Reference< com::sun::star::io::XOutputStream >& xOutputStream )
194cdf0e10cSrcweir {
195cdf0e10cSrcweir 	/*
196cdf0e10cSrcweir 	 * get SAX writer component
197cdf0e10cSrcweir 	 */
198cdf0e10cSrcweir 	uno::Reference< lang::XMultiComponentFactory > xMCF( mxCtx->getServiceManager() );
199cdf0e10cSrcweir 	uno::Reference< io::XActiveDataSource > xSaxWriter(
200cdf0e10cSrcweir 		xMCF->createInstanceWithContext(rtl::OUString::createFromAscii(
201cdf0e10cSrcweir 			"com.sun.star.xml.sax.Writer"), mxCtx ), uno::UNO_QUERY );
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 	DBG_ASSERT( xSaxWriter.is(), "can't instantiate XML writer" );
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 	/*
206cdf0e10cSrcweir 	 * connect XML writer to output stream
207cdf0e10cSrcweir 	 */
208cdf0e10cSrcweir 	xSaxWriter->setOutputStream( xOutputStream );
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 	/*
211cdf0e10cSrcweir 	 * prepare document handler
212cdf0e10cSrcweir 	 */
213cdf0e10cSrcweir 	uno::Reference<xml::sax::XDocumentHandler>
214cdf0e10cSrcweir 		xDocHandler( xSaxWriter,uno::UNO_QUERY);
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 	/*
217cdf0e10cSrcweir 	 * write the xml context for signatures
218cdf0e10cSrcweir 	 */
219cdf0e10cSrcweir 	rtl::OUString tag_AllSignatures(RTL_CONSTASCII_USTRINGPARAM(TAG_DOCUMENTSIGNATURES));
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 	SvXMLAttributeList *pAttributeList = new SvXMLAttributeList();
222cdf0e10cSrcweir     rtl::OUString sNamespace;
223cdf0e10cSrcweir     if (mbODFPre1_2)
224cdf0e10cSrcweir         sNamespace = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(NS_DOCUMENTSIGNATURES));
225cdf0e10cSrcweir     else
226cdf0e10cSrcweir         sNamespace = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(NS_DOCUMENTSIGNATURES_ODF_1_2));
227cdf0e10cSrcweir 
228cdf0e10cSrcweir 	pAttributeList->AddAttribute(
229cdf0e10cSrcweir 		rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(ATTR_XMLNS)),
230cdf0e10cSrcweir 		sNamespace);
231cdf0e10cSrcweir 
232cdf0e10cSrcweir 	xDocHandler->startDocument();
233cdf0e10cSrcweir 	xDocHandler->startElement(
234cdf0e10cSrcweir 		tag_AllSignatures,
235cdf0e10cSrcweir 		uno::Reference< com::sun::star::xml::sax::XAttributeList > (pAttributeList));
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 	return xDocHandler;
238cdf0e10cSrcweir }
239cdf0e10cSrcweir 
CloseDocumentHandler(const uno::Reference<xml::sax::XDocumentHandler> & xDocumentHandler)240cdf0e10cSrcweir void XMLSignatureHelper::CloseDocumentHandler( const uno::Reference<xml::sax::XDocumentHandler>& xDocumentHandler )
241cdf0e10cSrcweir {
242cdf0e10cSrcweir 	rtl::OUString tag_AllSignatures(RTL_CONSTASCII_USTRINGPARAM(TAG_DOCUMENTSIGNATURES));
243cdf0e10cSrcweir 	xDocumentHandler->endElement( tag_AllSignatures );
244cdf0e10cSrcweir 	xDocumentHandler->endDocument();
245cdf0e10cSrcweir }
246cdf0e10cSrcweir 
ExportSignature(const uno::Reference<xml::sax::XDocumentHandler> & xDocumentHandler,const SignatureInformation & signatureInfo)247cdf0e10cSrcweir void XMLSignatureHelper::ExportSignature(
248cdf0e10cSrcweir 	const uno::Reference< xml::sax::XDocumentHandler >& xDocumentHandler,
249cdf0e10cSrcweir 	const SignatureInformation& signatureInfo )
250cdf0e10cSrcweir {
251cdf0e10cSrcweir 	mpXSecController->exportSignature(xDocumentHandler, signatureInfo);
252cdf0e10cSrcweir }
253cdf0e10cSrcweir 
CreateAndWriteSignature(const uno::Reference<xml::sax::XDocumentHandler> & xDocumentHandler)254cdf0e10cSrcweir bool XMLSignatureHelper::CreateAndWriteSignature( const uno::Reference< xml::sax::XDocumentHandler >& xDocumentHandler )
255cdf0e10cSrcweir {
256cdf0e10cSrcweir 	mbError = false;
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 	/*
259cdf0e10cSrcweir 	 * create a signature listener
260cdf0e10cSrcweir 	 */
261cdf0e10cSrcweir /*
262cdf0e10cSrcweir 	ImplXMLSignatureListener* pSignatureListener = new ImplXMLSignatureListener(
263cdf0e10cSrcweir 	                                                LINK( this, XMLSignatureHelper, SignatureCreationResultListener ),
264cdf0e10cSrcweir 	                                                LINK( this, XMLSignatureHelper, SignatureVerifyResultListener ),
265cdf0e10cSrcweir 	                                                LINK( this, XMLSignatureHelper, StartVerifySignatureElement ) );
266cdf0e10cSrcweir */
267cdf0e10cSrcweir 	/*
268cdf0e10cSrcweir 	 * configure the signature creation listener
269cdf0e10cSrcweir 	 */
270cdf0e10cSrcweir 	//mpXSecController->setSignatureCreationResultListener( pSignatureListener );
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 	/*
273cdf0e10cSrcweir 	 * write signatures
274cdf0e10cSrcweir 	 */
275cdf0e10cSrcweir 	if ( !mpXSecController->WriteSignature( xDocumentHandler ) )
276cdf0e10cSrcweir 	{
277cdf0e10cSrcweir 		mbError = true;
278cdf0e10cSrcweir 	}
279cdf0e10cSrcweir 
280cdf0e10cSrcweir 	/*
281cdf0e10cSrcweir 	 * clear up the signature creation listener
282cdf0e10cSrcweir 	 */
283cdf0e10cSrcweir 	//mpXSecController->setSignatureCreationResultListener( NULL );
284cdf0e10cSrcweir 
285cdf0e10cSrcweir 	return !mbError;
286cdf0e10cSrcweir }
287cdf0e10cSrcweir 
CreateAndWriteSignature(const com::sun::star::uno::Reference<com::sun::star::io::XOutputStream> & xOutputStream)288cdf0e10cSrcweir bool XMLSignatureHelper::CreateAndWriteSignature( const com::sun::star::uno::Reference< com::sun::star::io::XOutputStream >& xOutputStream )
289cdf0e10cSrcweir {
290cdf0e10cSrcweir 	uno::Reference<xml::sax::XDocumentHandler> xDocHandler
291cdf0e10cSrcweir 		= CreateDocumentHandlerWithHeader(xOutputStream);
292cdf0e10cSrcweir 
293cdf0e10cSrcweir 	bool rc = CreateAndWriteSignature( xDocHandler );
294cdf0e10cSrcweir 
295cdf0e10cSrcweir 	CloseDocumentHandler(xDocHandler);
296cdf0e10cSrcweir 
297cdf0e10cSrcweir 	return rc;
298cdf0e10cSrcweir }
299cdf0e10cSrcweir 
ReadAndVerifySignature(const com::sun::star::uno::Reference<com::sun::star::io::XInputStream> & xInputStream)300cdf0e10cSrcweir bool XMLSignatureHelper::ReadAndVerifySignature( const com::sun::star::uno::Reference< com::sun::star::io::XInputStream >& xInputStream )
301cdf0e10cSrcweir {
302cdf0e10cSrcweir 	mbError = false;
303cdf0e10cSrcweir 
304cdf0e10cSrcweir 	DBG_ASSERT(xInputStream.is(), "input stream missing");
305cdf0e10cSrcweir 
306cdf0e10cSrcweir 	/*
307cdf0e10cSrcweir 	 * prepare ParserInputSrouce
308cdf0e10cSrcweir 	 */
309cdf0e10cSrcweir 	xml::sax::InputSource aParserInput;
310cdf0e10cSrcweir 	// aParserInput.sSystemId = ouName;
311cdf0e10cSrcweir 	aParserInput.aInputStream = xInputStream;
312cdf0e10cSrcweir 
313cdf0e10cSrcweir 	/*
314cdf0e10cSrcweir 	 * get SAX parser component
315cdf0e10cSrcweir 	 */
316cdf0e10cSrcweir 	uno::Reference< lang::XMultiComponentFactory > xMCF( mxCtx->getServiceManager() );
317cdf0e10cSrcweir 	uno::Reference< xml::sax::XParser > xParser(
318cdf0e10cSrcweir 		xMCF->createInstanceWithContext(
319cdf0e10cSrcweir 			rtl::OUString::createFromAscii("com.sun.star.xml.sax.Parser"), mxCtx ),
320cdf0e10cSrcweir 		uno::UNO_QUERY );
321cdf0e10cSrcweir 
322cdf0e10cSrcweir 	DBG_ASSERT( xParser.is(), "Can't create parser" );
323cdf0e10cSrcweir 
324cdf0e10cSrcweir 	/*
325cdf0e10cSrcweir 	 * create a signature reader
326cdf0e10cSrcweir 	 */
327cdf0e10cSrcweir 	uno::Reference< xml::sax::XDocumentHandler > xHandler
328cdf0e10cSrcweir 		= mpXSecController->createSignatureReader( );
329cdf0e10cSrcweir 
330cdf0e10cSrcweir 	/*
331cdf0e10cSrcweir 	 * create a signature listener
332cdf0e10cSrcweir 	 */
333cdf0e10cSrcweir 	ImplXMLSignatureListener* pSignatureListener = new ImplXMLSignatureListener(
334cdf0e10cSrcweir 	                                                LINK( this, XMLSignatureHelper, SignatureCreationResultListener ),
335cdf0e10cSrcweir 	                                                LINK( this, XMLSignatureHelper, SignatureVerifyResultListener ),
336cdf0e10cSrcweir 	                                                LINK( this, XMLSignatureHelper, StartVerifySignatureElement ) );
337cdf0e10cSrcweir 
338cdf0e10cSrcweir 	/*
339cdf0e10cSrcweir 	 * configure the signature verify listener
340cdf0e10cSrcweir 	 */
341cdf0e10cSrcweir 	//mpXSecController->setSignatureVerifyResultListener( pSignatureListener );
342cdf0e10cSrcweir 
343cdf0e10cSrcweir 	/*
344cdf0e10cSrcweir 	 * setup the connection:
345cdf0e10cSrcweir 	 * Parser -> SignatureListener -> SignatureReader
346cdf0e10cSrcweir 	 */
347cdf0e10cSrcweir 	pSignatureListener->setNextHandler(xHandler);
348cdf0e10cSrcweir 	xParser->setDocumentHandler( pSignatureListener );
349cdf0e10cSrcweir 
350cdf0e10cSrcweir 	/*
351cdf0e10cSrcweir 	 * parser the stream
352cdf0e10cSrcweir 	 */
353cdf0e10cSrcweir 	try
354cdf0e10cSrcweir 	{
355cdf0e10cSrcweir 		xParser->parseStream( aParserInput );
356cdf0e10cSrcweir 	}
357cdf0e10cSrcweir 	catch( xml::sax::SAXParseException& )
358cdf0e10cSrcweir 	{
359cdf0e10cSrcweir 		mbError = true;
360cdf0e10cSrcweir 	}
361cdf0e10cSrcweir 	catch( xml::sax::SAXException& )
362cdf0e10cSrcweir 	{
363cdf0e10cSrcweir 		mbError = true;
364cdf0e10cSrcweir 	}
365cdf0e10cSrcweir 	catch( com::sun::star::io::IOException& )
366cdf0e10cSrcweir 	{
367cdf0e10cSrcweir 		mbError = true;
368cdf0e10cSrcweir 	}
369cdf0e10cSrcweir 	catch( uno::Exception& )
370cdf0e10cSrcweir 	{
371cdf0e10cSrcweir 		mbError = true;
372cdf0e10cSrcweir 	}
373cdf0e10cSrcweir 
374cdf0e10cSrcweir 	/*
375cdf0e10cSrcweir 	 * clear up the connection
376cdf0e10cSrcweir 	 */
377cdf0e10cSrcweir 	pSignatureListener->setNextHandler( NULL );
378cdf0e10cSrcweir 
379cdf0e10cSrcweir 	/*
380cdf0e10cSrcweir 	 * clear up the signature verify listener
381cdf0e10cSrcweir 	 */
382cdf0e10cSrcweir 	//mpXSecController->setSignatureVerifyResultListener( NULL );
383cdf0e10cSrcweir 
384cdf0e10cSrcweir 	/*
385cdf0e10cSrcweir 	 * release the signature reader
386cdf0e10cSrcweir 	 */
387cdf0e10cSrcweir 	mpXSecController->releaseSignatureReader( );
388cdf0e10cSrcweir 
389cdf0e10cSrcweir 	return !mbError;
390cdf0e10cSrcweir }
391cdf0e10cSrcweir 
GetSignatureInformation(sal_Int32 nSecurityId) const392cdf0e10cSrcweir SignatureInformation XMLSignatureHelper::GetSignatureInformation( sal_Int32 nSecurityId ) const
393cdf0e10cSrcweir {
394cdf0e10cSrcweir 	return mpXSecController->getSignatureInformation( nSecurityId );
395cdf0e10cSrcweir }
396cdf0e10cSrcweir 
GetSignatureInformations() const397cdf0e10cSrcweir SignatureInformations XMLSignatureHelper::GetSignatureInformations() const
398cdf0e10cSrcweir {
399cdf0e10cSrcweir 	return mpXSecController->getSignatureInformations();
400cdf0e10cSrcweir }
401cdf0e10cSrcweir 
GetSecurityEnvironment()402cdf0e10cSrcweir uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > XMLSignatureHelper::GetSecurityEnvironment()
403cdf0e10cSrcweir {
404cdf0e10cSrcweir 	return (mxSecurityContext.is()?(mxSecurityContext->getSecurityEnvironment()): uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment >());
405cdf0e10cSrcweir }
406cdf0e10cSrcweir 
GetSecurityEnvironmentByIndex(sal_Int32 nId)407cdf0e10cSrcweir uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > XMLSignatureHelper::GetSecurityEnvironmentByIndex(sal_Int32 nId)
408cdf0e10cSrcweir {
409cdf0e10cSrcweir 	return (mxSecurityContext.is()?(mxSecurityContext->getSecurityEnvironmentByIndex(nId)): uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment >());
410cdf0e10cSrcweir }
411cdf0e10cSrcweir 
GetSecurityEnvironmentNumber()412cdf0e10cSrcweir sal_Int32 XMLSignatureHelper::GetSecurityEnvironmentNumber()
413cdf0e10cSrcweir {
414cdf0e10cSrcweir 	return (mxSecurityContext.is()?(mxSecurityContext->getSecurityEnvironmentNumber()): 0);
415cdf0e10cSrcweir }
416cdf0e10cSrcweir 
IMPL_LINK(XMLSignatureHelper,SignatureCreationResultListener,XMLSignatureCreationResult *,pResult)417cdf0e10cSrcweir IMPL_LINK( XMLSignatureHelper, SignatureCreationResultListener, XMLSignatureCreationResult*, pResult )
418cdf0e10cSrcweir {
419cdf0e10cSrcweir     maCreationResults.insert( maCreationResults.begin() + maCreationResults.size(), *pResult );
420cdf0e10cSrcweir     if ( pResult->nSignatureCreationResult != com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED )
421cdf0e10cSrcweir         mbError = true;
422cdf0e10cSrcweir     return 0;
423cdf0e10cSrcweir }
424cdf0e10cSrcweir 
IMPL_LINK(XMLSignatureHelper,SignatureVerifyResultListener,XMLSignatureVerifyResult *,pResult)425cdf0e10cSrcweir IMPL_LINK( XMLSignatureHelper, SignatureVerifyResultListener, XMLSignatureVerifyResult*, pResult )
426cdf0e10cSrcweir {
427cdf0e10cSrcweir     maVerifyResults.insert( maVerifyResults.begin() + maVerifyResults.size(), *pResult );
428cdf0e10cSrcweir     if ( pResult->nSignatureVerifyResult != com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED )
429cdf0e10cSrcweir         mbError = true;
430cdf0e10cSrcweir     return 0;
431cdf0e10cSrcweir }
432cdf0e10cSrcweir 
IMPL_LINK(XMLSignatureHelper,StartVerifySignatureElement,const uno::Reference<com::sun::star::xml::sax::XAttributeList> *,pAttrs)433cdf0e10cSrcweir IMPL_LINK( XMLSignatureHelper, StartVerifySignatureElement, const uno::Reference< com::sun::star::xml::sax::XAttributeList >*, pAttrs )
434cdf0e10cSrcweir {
435cdf0e10cSrcweir     if ( !maStartVerifySignatureHdl.IsSet() || maStartVerifySignatureHdl.Call( (void*)pAttrs ) )
436cdf0e10cSrcweir 	{
437cdf0e10cSrcweir 		sal_Int32 nSignatureId = mpXSecController->getNewSecurityId();
438cdf0e10cSrcweir 		mpXSecController->addSignature( nSignatureId );
439cdf0e10cSrcweir 	}
440cdf0e10cSrcweir 
441cdf0e10cSrcweir     return 0;
442cdf0e10cSrcweir }
443