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