xref: /trunk/main/xmlsecurity/source/helper/xmlsignaturehelper2.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 <xmlsignaturehelper2.hxx>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <tools/solar.h>
35*cdf0e10cSrcweir #include <unotools/streamhelper.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <com/sun/star/embed/XStorage.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/embed/XStorageRawAccess.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
41*cdf0e10cSrcweir #include "rtl/uri.hxx"
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir using namespace com::sun::star;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir ImplXMLSignatureListener::ImplXMLSignatureListener( const Link& rCreationResultListenerListener, const Link rVerifyResultListenerListener, const Link rStartSignatureElement )
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir     maCreationResultListenerListener = rCreationResultListenerListener;
48*cdf0e10cSrcweir     maVerifyResultListenerListener = rVerifyResultListenerListener;
49*cdf0e10cSrcweir     maStartVerifySignatureElementListener = rStartSignatureElement;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir }
52*cdf0e10cSrcweir ImplXMLSignatureListener::~ImplXMLSignatureListener()
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir }
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir void ImplXMLSignatureListener::setNextHandler(
57*cdf0e10cSrcweir     uno::Reference< xml::sax::XDocumentHandler > xNextHandler)
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir     m_xNextHandler = xNextHandler;
60*cdf0e10cSrcweir }
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir void SAL_CALL ImplXMLSignatureListener::signatureCreated( sal_Int32 securityId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
63*cdf0e10cSrcweir         throw (com::sun::star::uno::RuntimeException)
64*cdf0e10cSrcweir {
65*cdf0e10cSrcweir     XMLSignatureCreationResult aResult( securityId, nResult );
66*cdf0e10cSrcweir     maCreationResultListenerListener.Call( &aResult );
67*cdf0e10cSrcweir }
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir void SAL_CALL ImplXMLSignatureListener::signatureVerified( sal_Int32 securityId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
70*cdf0e10cSrcweir         throw (com::sun::star::uno::RuntimeException)
71*cdf0e10cSrcweir {
72*cdf0e10cSrcweir     XMLSignatureVerifyResult aResult( securityId, nResult );
73*cdf0e10cSrcweir     maVerifyResultListenerListener.Call( &aResult );
74*cdf0e10cSrcweir }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir // ---------------------------------------------------------------------------------
77*cdf0e10cSrcweir // XDocumentHandler
78*cdf0e10cSrcweir // ---------------------------------------------------------------------------------
79*cdf0e10cSrcweir void SAL_CALL ImplXMLSignatureListener::startDocument(  )
80*cdf0e10cSrcweir     throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
81*cdf0e10cSrcweir {
82*cdf0e10cSrcweir     if (m_xNextHandler.is())
83*cdf0e10cSrcweir     {
84*cdf0e10cSrcweir         m_xNextHandler->startDocument();
85*cdf0e10cSrcweir     }
86*cdf0e10cSrcweir }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir void SAL_CALL ImplXMLSignatureListener::endDocument(  )
89*cdf0e10cSrcweir     throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
90*cdf0e10cSrcweir {
91*cdf0e10cSrcweir     if (m_xNextHandler.is())
92*cdf0e10cSrcweir     {
93*cdf0e10cSrcweir         m_xNextHandler->endDocument();
94*cdf0e10cSrcweir     }
95*cdf0e10cSrcweir }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir void SAL_CALL ImplXMLSignatureListener::startElement( const rtl::OUString& aName, const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList >& xAttribs )
98*cdf0e10cSrcweir         throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
99*cdf0e10cSrcweir {
100*cdf0e10cSrcweir     if ( aName == rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Signature")) )
101*cdf0e10cSrcweir     {
102*cdf0e10cSrcweir             maStartVerifySignatureElementListener.Call( (void*)&xAttribs );
103*cdf0e10cSrcweir     }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     if (m_xNextHandler.is())
106*cdf0e10cSrcweir     {
107*cdf0e10cSrcweir         m_xNextHandler->startElement( aName, xAttribs );
108*cdf0e10cSrcweir     }
109*cdf0e10cSrcweir }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir void SAL_CALL ImplXMLSignatureListener::endElement( const rtl::OUString& aName )
112*cdf0e10cSrcweir     throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
113*cdf0e10cSrcweir {
114*cdf0e10cSrcweir     if (m_xNextHandler.is())
115*cdf0e10cSrcweir     {
116*cdf0e10cSrcweir         m_xNextHandler->endElement( aName );
117*cdf0e10cSrcweir     }
118*cdf0e10cSrcweir }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir void SAL_CALL ImplXMLSignatureListener::characters( const rtl::OUString& aChars )
121*cdf0e10cSrcweir     throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
122*cdf0e10cSrcweir {
123*cdf0e10cSrcweir     if (m_xNextHandler.is())
124*cdf0e10cSrcweir     {
125*cdf0e10cSrcweir         m_xNextHandler->characters( aChars );
126*cdf0e10cSrcweir     }
127*cdf0e10cSrcweir }
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir void SAL_CALL ImplXMLSignatureListener::ignorableWhitespace( const rtl::OUString& aWhitespaces )
130*cdf0e10cSrcweir     throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
131*cdf0e10cSrcweir {
132*cdf0e10cSrcweir     if (m_xNextHandler.is())
133*cdf0e10cSrcweir     {
134*cdf0e10cSrcweir         m_xNextHandler->ignorableWhitespace( aWhitespaces );
135*cdf0e10cSrcweir     }
136*cdf0e10cSrcweir }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir void SAL_CALL ImplXMLSignatureListener::processingInstruction( const rtl::OUString& aTarget, const rtl::OUString& aData )
139*cdf0e10cSrcweir     throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
140*cdf0e10cSrcweir {
141*cdf0e10cSrcweir     if (m_xNextHandler.is())
142*cdf0e10cSrcweir     {
143*cdf0e10cSrcweir         m_xNextHandler->processingInstruction( aTarget, aData );
144*cdf0e10cSrcweir     }
145*cdf0e10cSrcweir }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir void SAL_CALL ImplXMLSignatureListener::setDocumentLocator( const com::sun::star::uno::Reference< com::sun::star::xml::sax::XLocator >& xLocator )
148*cdf0e10cSrcweir     throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
149*cdf0e10cSrcweir {
150*cdf0e10cSrcweir     if (m_xNextHandler.is())
151*cdf0e10cSrcweir     {
152*cdf0e10cSrcweir         m_xNextHandler->setDocumentLocator( xLocator );
153*cdf0e10cSrcweir     }
154*cdf0e10cSrcweir }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir // ---------------------------------------------------------------------------------
157*cdf0e10cSrcweir // XUriBinding
158*cdf0e10cSrcweir // ---------------------------------------------------------------------------------
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir UriBindingHelper::UriBindingHelper()
161*cdf0e10cSrcweir {
162*cdf0e10cSrcweir }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir UriBindingHelper::UriBindingHelper( const com::sun::star::uno::Reference < com::sun::star::embed::XStorage >& rxStorage )
165*cdf0e10cSrcweir {
166*cdf0e10cSrcweir     mxStorage = rxStorage;
167*cdf0e10cSrcweir }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir void SAL_CALL UriBindingHelper::setUriBinding( const rtl::OUString& /*uri*/, const uno::Reference< io::XInputStream >&)
171*cdf0e10cSrcweir     throw (uno::Exception, uno::RuntimeException)
172*cdf0e10cSrcweir {
173*cdf0e10cSrcweir }
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir uno::Reference< io::XInputStream > SAL_CALL UriBindingHelper::getUriBinding( const rtl::OUString& uri )
176*cdf0e10cSrcweir     throw (uno::Exception, uno::RuntimeException)
177*cdf0e10cSrcweir {
178*cdf0e10cSrcweir     uno::Reference< io::XInputStream > xInputStream;
179*cdf0e10cSrcweir     if ( mxStorage.is() )
180*cdf0e10cSrcweir     {
181*cdf0e10cSrcweir         xInputStream = OpenInputStream( mxStorage, uri );
182*cdf0e10cSrcweir     }
183*cdf0e10cSrcweir     else
184*cdf0e10cSrcweir     {
185*cdf0e10cSrcweir         SvFileStream* pStream = new SvFileStream( uri, STREAM_READ );
186*cdf0e10cSrcweir         pStream->Seek( STREAM_SEEK_TO_END );
187*cdf0e10cSrcweir         sal_uLong nBytes = pStream->Tell();
188*cdf0e10cSrcweir         pStream->Seek( STREAM_SEEK_TO_BEGIN );
189*cdf0e10cSrcweir         SvLockBytesRef xLockBytes = new SvLockBytes( pStream, sal_True );
190*cdf0e10cSrcweir         xInputStream = new utl::OInputStreamHelper( xLockBytes, nBytes );
191*cdf0e10cSrcweir     }
192*cdf0e10cSrcweir     return xInputStream;
193*cdf0e10cSrcweir }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir uno::Reference < io::XInputStream > UriBindingHelper::OpenInputStream( const uno::Reference < embed::XStorage >& rxStore, const rtl::OUString& rURI )
196*cdf0e10cSrcweir {
197*cdf0e10cSrcweir     OSL_ASSERT(rURI.getLength());
198*cdf0e10cSrcweir     uno::Reference < io::XInputStream > xInStream;
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir     sal_Int32 nSepPos = rURI.indexOf( '/' );
201*cdf0e10cSrcweir     if ( nSepPos == -1 )
202*cdf0e10cSrcweir     {
203*cdf0e10cSrcweir         // Cloning because of I can't keep all storage references open
204*cdf0e10cSrcweir         // MBA with think about a better API...
205*cdf0e10cSrcweir         const ::rtl::OUString sName = ::rtl::Uri::decode(
206*cdf0e10cSrcweir             rURI, rtl_UriDecodeStrict, rtl_UriCharClassRelSegment);
207*cdf0e10cSrcweir         if (sName.getLength() == 0 && rURI.getLength() != 0)
208*cdf0e10cSrcweir             throw uno::Exception(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
209*cdf0e10cSrcweir             "Could not decode URI for stream element.")), 0);
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir         uno::Reference< io::XStream > xStream;
212*cdf0e10cSrcweir         xStream = rxStore->cloneStreamElement( sName );
213*cdf0e10cSrcweir         if ( !xStream.is() )
214*cdf0e10cSrcweir             throw uno::RuntimeException();
215*cdf0e10cSrcweir         xInStream = xStream->getInputStream();
216*cdf0e10cSrcweir     }
217*cdf0e10cSrcweir     else
218*cdf0e10cSrcweir     {
219*cdf0e10cSrcweir         const rtl::OUString aStoreName = ::rtl::Uri::decode(
220*cdf0e10cSrcweir             rURI.copy( 0, nSepPos ), rtl_UriDecodeStrict, rtl_UriCharClassRelSegment);
221*cdf0e10cSrcweir         if (aStoreName.getLength() == 0 && rURI.getLength() != 0)
222*cdf0e10cSrcweir             throw uno::Exception(
223*cdf0e10cSrcweir             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
224*cdf0e10cSrcweir             "Could not decode URI for stream element.")), 0);
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir         rtl::OUString aElement = rURI.copy( nSepPos+1 );
227*cdf0e10cSrcweir         uno::Reference < embed::XStorage > xSubStore = rxStore->openStorageElement( aStoreName, embed::ElementModes::READ );
228*cdf0e10cSrcweir         xInStream = OpenInputStream( xSubStore, aElement );
229*cdf0e10cSrcweir     }
230*cdf0e10cSrcweir     return xInStream;
231*cdf0e10cSrcweir }
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir 
234