1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmlsecurity.hxx"
26 
27 #include <xmlsecurity/xmlsignaturehelper.hxx>
28 #include <xmlsignaturehelper2.hxx>
29 
30 #include <tools/solar.h>
31 #include <unotools/streamhelper.hxx>
32 
33 #include <com/sun/star/embed/XStorage.hpp>
34 #include <com/sun/star/embed/XStorageRawAccess.hpp>
35 #include <com/sun/star/embed/ElementModes.hpp>
36 #include <com/sun/star/beans/XPropertySet.hpp>
37 #include "rtl/uri.hxx"
38 
39 using namespace com::sun::star;
40 
ImplXMLSignatureListener(const Link & rCreationResultListenerListener,const Link rVerifyResultListenerListener,const Link rStartSignatureElement)41 ImplXMLSignatureListener::ImplXMLSignatureListener( const Link& rCreationResultListenerListener, const Link rVerifyResultListenerListener, const Link rStartSignatureElement )
42 {
43     maCreationResultListenerListener = rCreationResultListenerListener;
44     maVerifyResultListenerListener = rVerifyResultListenerListener;
45     maStartVerifySignatureElementListener = rStartSignatureElement;
46 
47 }
~ImplXMLSignatureListener()48 ImplXMLSignatureListener::~ImplXMLSignatureListener()
49 {
50 }
51 
setNextHandler(uno::Reference<xml::sax::XDocumentHandler> xNextHandler)52 void ImplXMLSignatureListener::setNextHandler(
53 	uno::Reference< xml::sax::XDocumentHandler > xNextHandler)
54 {
55 	m_xNextHandler = xNextHandler;
56 }
57 
signatureCreated(sal_Int32 securityId,com::sun::star::xml::crypto::SecurityOperationStatus nResult)58 void SAL_CALL ImplXMLSignatureListener::signatureCreated( sal_Int32 securityId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
59 		throw (com::sun::star::uno::RuntimeException)
60 {
61     XMLSignatureCreationResult aResult( securityId, nResult );
62     maCreationResultListenerListener.Call( &aResult );
63 }
64 
signatureVerified(sal_Int32 securityId,com::sun::star::xml::crypto::SecurityOperationStatus nResult)65 void SAL_CALL ImplXMLSignatureListener::signatureVerified( sal_Int32 securityId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
66 		throw (com::sun::star::uno::RuntimeException)
67 {
68     XMLSignatureVerifyResult aResult( securityId, nResult );
69     maVerifyResultListenerListener.Call( &aResult );
70 }
71 
72 // ---------------------------------------------------------------------------------
73 // XDocumentHandler
74 // ---------------------------------------------------------------------------------
startDocument()75 void SAL_CALL ImplXMLSignatureListener::startDocument(  )
76 	throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
77 {
78 	if (m_xNextHandler.is())
79 	{
80 		m_xNextHandler->startDocument();
81 	}
82 }
83 
endDocument()84 void SAL_CALL ImplXMLSignatureListener::endDocument(  )
85 	throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
86 {
87 	if (m_xNextHandler.is())
88 	{
89 		m_xNextHandler->endDocument();
90 	}
91 }
92 
startElement(const rtl::OUString & aName,const com::sun::star::uno::Reference<com::sun::star::xml::sax::XAttributeList> & xAttribs)93 void SAL_CALL ImplXMLSignatureListener::startElement( const rtl::OUString& aName, const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList >& xAttribs )
94 		throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
95 {
96 	if ( aName == rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Signature")) )
97 	{
98         	maStartVerifySignatureElementListener.Call( (void*)&xAttribs );
99 	}
100 
101 	if (m_xNextHandler.is())
102 	{
103 		m_xNextHandler->startElement( aName, xAttribs );
104 	}
105 }
106 
endElement(const rtl::OUString & aName)107 void SAL_CALL ImplXMLSignatureListener::endElement( const rtl::OUString& aName )
108 	throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
109 {
110 	if (m_xNextHandler.is())
111 	{
112 		m_xNextHandler->endElement( aName );
113 	}
114 }
115 
characters(const rtl::OUString & aChars)116 void SAL_CALL ImplXMLSignatureListener::characters( const rtl::OUString& aChars )
117 	throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
118 {
119 	if (m_xNextHandler.is())
120 	{
121 		m_xNextHandler->characters( aChars );
122 	}
123 }
124 
ignorableWhitespace(const rtl::OUString & aWhitespaces)125 void SAL_CALL ImplXMLSignatureListener::ignorableWhitespace( const rtl::OUString& aWhitespaces )
126 	throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
127 {
128 	if (m_xNextHandler.is())
129 	{
130 		m_xNextHandler->ignorableWhitespace( aWhitespaces );
131 	}
132 }
133 
processingInstruction(const rtl::OUString & aTarget,const rtl::OUString & aData)134 void SAL_CALL ImplXMLSignatureListener::processingInstruction( const rtl::OUString& aTarget, const rtl::OUString& aData )
135 	throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
136 {
137 	if (m_xNextHandler.is())
138 	{
139 		m_xNextHandler->processingInstruction( aTarget, aData );
140 	}
141 }
142 
setDocumentLocator(const com::sun::star::uno::Reference<com::sun::star::xml::sax::XLocator> & xLocator)143 void SAL_CALL ImplXMLSignatureListener::setDocumentLocator( const com::sun::star::uno::Reference< com::sun::star::xml::sax::XLocator >& xLocator )
144 	throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
145 {
146 	if (m_xNextHandler.is())
147 	{
148 		m_xNextHandler->setDocumentLocator( xLocator );
149 	}
150 }
151 
152 // ---------------------------------------------------------------------------------
153 // XUriBinding
154 // ---------------------------------------------------------------------------------
155 
UriBindingHelper()156 UriBindingHelper::UriBindingHelper()
157 {
158 }
159 
UriBindingHelper(const com::sun::star::uno::Reference<com::sun::star::embed::XStorage> & rxStorage)160 UriBindingHelper::UriBindingHelper( const com::sun::star::uno::Reference < com::sun::star::embed::XStorage >& rxStorage )
161 {
162     mxStorage = rxStorage;
163 }
164 
165 
setUriBinding(const rtl::OUString &,const uno::Reference<io::XInputStream> &)166 void SAL_CALL UriBindingHelper::setUriBinding( const rtl::OUString& /*uri*/, const uno::Reference< io::XInputStream >&)
167     throw (uno::Exception, uno::RuntimeException)
168 {
169 }
170 
getUriBinding(const rtl::OUString & uri)171 uno::Reference< io::XInputStream > SAL_CALL UriBindingHelper::getUriBinding( const rtl::OUString& uri )
172 	throw (uno::Exception, uno::RuntimeException)
173 {
174     uno::Reference< io::XInputStream > xInputStream;
175     if ( mxStorage.is() )
176     {
177         xInputStream = OpenInputStream( mxStorage, uri );
178     }
179     else
180     {
181         SvFileStream* pStream = new SvFileStream( uri, STREAM_READ );
182         pStream->Seek( STREAM_SEEK_TO_END );
183         sal_uLong nBytes = pStream->Tell();
184         pStream->Seek( STREAM_SEEK_TO_BEGIN );
185         SvLockBytesRef xLockBytes = new SvLockBytes( pStream, sal_True );
186         xInputStream = new utl::OInputStreamHelper( xLockBytes, nBytes );
187     }
188     return xInputStream;
189 }
190 
OpenInputStream(const uno::Reference<embed::XStorage> & rxStore,const rtl::OUString & rURI)191 uno::Reference < io::XInputStream > UriBindingHelper::OpenInputStream( const uno::Reference < embed::XStorage >& rxStore, const rtl::OUString& rURI )
192 {
193     OSL_ASSERT(rURI.getLength());
194     uno::Reference < io::XInputStream > xInStream;
195 
196     sal_Int32 nSepPos = rURI.indexOf( '/' );
197     if ( nSepPos == -1 )
198     {
199         // Cloning because of I can't keep all storage references open
200         // MBA with think about a better API...
201         const ::rtl::OUString sName = ::rtl::Uri::decode(
202             rURI, rtl_UriDecodeStrict, rtl_UriCharClassRelSegment);
203         if (sName.getLength() == 0 && rURI.getLength() != 0)
204             throw uno::Exception(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
205             "Could not decode URI for stream element.")), 0);
206 
207         uno::Reference< io::XStream > xStream;
208         xStream = rxStore->cloneStreamElement( sName );
209         if ( !xStream.is() )
210             throw uno::RuntimeException();
211         xInStream = xStream->getInputStream();
212     }
213     else
214     {
215         const rtl::OUString aStoreName = ::rtl::Uri::decode(
216             rURI.copy( 0, nSepPos ), rtl_UriDecodeStrict, rtl_UriCharClassRelSegment);
217         if (aStoreName.getLength() == 0 && rURI.getLength() != 0)
218             throw uno::Exception(
219             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
220             "Could not decode URI for stream element.")), 0);
221 
222         rtl::OUString aElement = rURI.copy( nSepPos+1 );
223         uno::Reference < embed::XStorage > xSubStore = rxStore->openStorageElement( aStoreName, embed::ElementModes::READ );
224         xInStream = OpenInputStream( xSubStore, aElement );
225     }
226     return xInStream;
227 }
228 
229 
230