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