merged tag ooo/OOO330_m14
[LibreOffice.git] / xmlsecurity / source / helper / xmlsignaturehelper2.cxx
blob4c0c847b8d54898f857a8eaf7a9e6139e22e5e91
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
31 #include <xmlsecurity/xmlsignaturehelper.hxx>
32 #include <xmlsignaturehelper2.hxx>
34 #include <unotools/streamhelper.hxx>
36 #include <com/sun/star/embed/XStorage.hpp>
37 #include <com/sun/star/embed/XStorageRawAccess.hpp>
38 #include <com/sun/star/embed/ElementModes.hpp>
39 #include <com/sun/star/beans/XPropertySet.hpp>
40 #include "rtl/uri.hxx"
42 using namespace com::sun::star;
44 ImplXMLSignatureListener::ImplXMLSignatureListener( const Link& rCreationResultListenerListener, const Link rVerifyResultListenerListener, const Link rStartSignatureElement )
46 maCreationResultListenerListener = rCreationResultListenerListener;
47 maVerifyResultListenerListener = rVerifyResultListenerListener;
48 maStartVerifySignatureElementListener = rStartSignatureElement;
51 ImplXMLSignatureListener::~ImplXMLSignatureListener()
55 void ImplXMLSignatureListener::setNextHandler(
56 uno::Reference< xml::sax::XDocumentHandler > xNextHandler)
58 m_xNextHandler = xNextHandler;
61 void SAL_CALL ImplXMLSignatureListener::signatureCreated( sal_Int32 securityId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
62 throw (com::sun::star::uno::RuntimeException)
64 XMLSignatureCreationResult aResult( securityId, nResult );
65 maCreationResultListenerListener.Call( &aResult );
68 void SAL_CALL ImplXMLSignatureListener::signatureVerified( sal_Int32 securityId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
69 throw (com::sun::star::uno::RuntimeException)
71 XMLSignatureVerifyResult aResult( securityId, nResult );
72 maVerifyResultListenerListener.Call( &aResult );
75 // ---------------------------------------------------------------------------------
76 // XDocumentHandler
77 // ---------------------------------------------------------------------------------
78 void SAL_CALL ImplXMLSignatureListener::startDocument( )
79 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
81 if (m_xNextHandler.is())
83 m_xNextHandler->startDocument();
87 void SAL_CALL ImplXMLSignatureListener::endDocument( )
88 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
90 if (m_xNextHandler.is())
92 m_xNextHandler->endDocument();
96 void SAL_CALL ImplXMLSignatureListener::startElement( const rtl::OUString& aName, const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList >& xAttribs )
97 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
99 if ( aName == rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Signature")) )
101 maStartVerifySignatureElementListener.Call( (void*)&xAttribs );
104 if (m_xNextHandler.is())
106 m_xNextHandler->startElement( aName, xAttribs );
110 void SAL_CALL ImplXMLSignatureListener::endElement( const rtl::OUString& aName )
111 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
113 if (m_xNextHandler.is())
115 m_xNextHandler->endElement( aName );
119 void SAL_CALL ImplXMLSignatureListener::characters( const rtl::OUString& aChars )
120 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
122 if (m_xNextHandler.is())
124 m_xNextHandler->characters( aChars );
128 void SAL_CALL ImplXMLSignatureListener::ignorableWhitespace( const rtl::OUString& aWhitespaces )
129 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
131 if (m_xNextHandler.is())
133 m_xNextHandler->ignorableWhitespace( aWhitespaces );
137 void SAL_CALL ImplXMLSignatureListener::processingInstruction( const rtl::OUString& aTarget, const rtl::OUString& aData )
138 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
140 if (m_xNextHandler.is())
142 m_xNextHandler->processingInstruction( aTarget, aData );
146 void SAL_CALL ImplXMLSignatureListener::setDocumentLocator( const com::sun::star::uno::Reference< com::sun::star::xml::sax::XLocator >& xLocator )
147 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
149 if (m_xNextHandler.is())
151 m_xNextHandler->setDocumentLocator( xLocator );
155 // ---------------------------------------------------------------------------------
156 // XUriBinding
157 // ---------------------------------------------------------------------------------
159 UriBindingHelper::UriBindingHelper()
163 UriBindingHelper::UriBindingHelper( const com::sun::star::uno::Reference < com::sun::star::embed::XStorage >& rxStorage )
165 mxStorage = rxStorage;
169 void SAL_CALL UriBindingHelper::setUriBinding( const rtl::OUString& /*uri*/, const uno::Reference< io::XInputStream >&)
170 throw (uno::Exception, uno::RuntimeException)
174 uno::Reference< io::XInputStream > SAL_CALL UriBindingHelper::getUriBinding( const rtl::OUString& uri )
175 throw (uno::Exception, uno::RuntimeException)
177 uno::Reference< io::XInputStream > xInputStream;
178 if ( mxStorage.is() )
180 xInputStream = OpenInputStream( mxStorage, uri );
182 else
184 SvFileStream* pStream = new SvFileStream( uri, STREAM_READ );
185 pStream->Seek( STREAM_SEEK_TO_END );
186 ULONG nBytes = pStream->Tell();
187 pStream->Seek( STREAM_SEEK_TO_BEGIN );
188 SvLockBytesRef xLockBytes = new SvLockBytes( pStream, TRUE );
189 xInputStream = new utl::OInputStreamHelper( xLockBytes, nBytes );
191 return xInputStream;
194 uno::Reference < io::XInputStream > UriBindingHelper::OpenInputStream( const uno::Reference < embed::XStorage >& rxStore, const rtl::OUString& rURI )
196 OSL_ASSERT(rURI.getLength());
197 uno::Reference < io::XInputStream > xInStream;
199 sal_Int32 nSepPos = rURI.indexOf( '/' );
200 if ( nSepPos == -1 )
202 // Cloning because of I can't keep all storage references open
203 // MBA with think about a better API...
204 const ::rtl::OUString sName = ::rtl::Uri::decode(
205 rURI, rtl_UriDecodeStrict, rtl_UriCharClassRelSegment);
206 if (sName.getLength() == 0 && rURI.getLength() != 0)
207 throw uno::Exception(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
208 "Could not decode URI for stream element.")), 0);
210 uno::Reference< io::XStream > xStream;
211 xStream = rxStore->cloneStreamElement( sName );
212 if ( !xStream.is() )
213 throw uno::RuntimeException();
214 xInStream = xStream->getInputStream();
216 else
218 const rtl::OUString aStoreName = ::rtl::Uri::decode(
219 rURI.copy( 0, nSepPos ), rtl_UriDecodeStrict, rtl_UriCharClassRelSegment);
220 if (aStoreName.getLength() == 0 && rURI.getLength() != 0)
221 throw uno::Exception(
222 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
223 "Could not decode URI for stream element.")), 0);
225 rtl::OUString aElement = rURI.copy( nSepPos+1 );
226 uno::Reference < embed::XStorage > xSubStore = rxStore->openStorageElement( aStoreName, embed::ElementModes::READ );
227 xInStream = OpenInputStream( xSubStore, aElement );
229 return xInStream;