update credits
[LibreOffice.git] / xmlsecurity / source / helper / xmlsignaturehelper2.cxx
blobd8b35863a73f765a55170f54ef82387c735edc7f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <xmlsecurity/xmlsignaturehelper.hxx>
22 #include <xmlsignaturehelper2.hxx>
24 #include <tools/solar.h>
25 #include <unotools/streamhelper.hxx>
27 #include <com/sun/star/embed/XStorage.hpp>
28 #include <com/sun/star/embed/XStorageRawAccess.hpp>
29 #include <com/sun/star/embed/ElementModes.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include "rtl/uri.hxx"
33 using namespace com::sun::star;
35 ImplXMLSignatureListener::ImplXMLSignatureListener( const Link& rCreationResultListenerListener, const Link rVerifyResultListenerListener, const Link rStartSignatureElement )
37 maCreationResultListenerListener = rCreationResultListenerListener;
38 maVerifyResultListenerListener = rVerifyResultListenerListener;
39 maStartVerifySignatureElementListener = rStartSignatureElement;
42 ImplXMLSignatureListener::~ImplXMLSignatureListener()
46 void ImplXMLSignatureListener::setNextHandler(
47 uno::Reference< xml::sax::XDocumentHandler > xNextHandler)
49 m_xNextHandler = xNextHandler;
52 void SAL_CALL ImplXMLSignatureListener::signatureCreated( sal_Int32 securityId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
53 throw (com::sun::star::uno::RuntimeException)
55 XMLSignatureCreationResult aResult( securityId, nResult );
56 maCreationResultListenerListener.Call( &aResult );
59 void SAL_CALL ImplXMLSignatureListener::signatureVerified( sal_Int32 securityId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
60 throw (com::sun::star::uno::RuntimeException)
62 XMLSignatureVerifyResult aResult( securityId, nResult );
63 maVerifyResultListenerListener.Call( &aResult );
66 // ---------------------------------------------------------------------------------
67 // XDocumentHandler
68 // ---------------------------------------------------------------------------------
69 void SAL_CALL ImplXMLSignatureListener::startDocument( )
70 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
72 if (m_xNextHandler.is())
74 m_xNextHandler->startDocument();
78 void SAL_CALL ImplXMLSignatureListener::endDocument( )
79 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
81 if (m_xNextHandler.is())
83 m_xNextHandler->endDocument();
87 void SAL_CALL ImplXMLSignatureListener::startElement( const OUString& aName, const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList >& xAttribs )
88 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
90 if ( aName == OUString("Signature") )
92 maStartVerifySignatureElementListener.Call( (void*)&xAttribs );
95 if (m_xNextHandler.is())
97 m_xNextHandler->startElement( aName, xAttribs );
101 void SAL_CALL ImplXMLSignatureListener::endElement( const OUString& aName )
102 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
104 if (m_xNextHandler.is())
106 m_xNextHandler->endElement( aName );
110 void SAL_CALL ImplXMLSignatureListener::characters( const OUString& aChars )
111 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
113 if (m_xNextHandler.is())
115 m_xNextHandler->characters( aChars );
119 void SAL_CALL ImplXMLSignatureListener::ignorableWhitespace( const OUString& aWhitespaces )
120 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
122 if (m_xNextHandler.is())
124 m_xNextHandler->ignorableWhitespace( aWhitespaces );
128 void SAL_CALL ImplXMLSignatureListener::processingInstruction( const OUString& aTarget, const OUString& aData )
129 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
131 if (m_xNextHandler.is())
133 m_xNextHandler->processingInstruction( aTarget, aData );
137 void SAL_CALL ImplXMLSignatureListener::setDocumentLocator( const com::sun::star::uno::Reference< com::sun::star::xml::sax::XLocator >& xLocator )
138 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
140 if (m_xNextHandler.is())
142 m_xNextHandler->setDocumentLocator( xLocator );
146 // ---------------------------------------------------------------------------------
147 // XUriBinding
148 // ---------------------------------------------------------------------------------
150 UriBindingHelper::UriBindingHelper()
154 UriBindingHelper::UriBindingHelper( const com::sun::star::uno::Reference < com::sun::star::embed::XStorage >& rxStorage )
156 mxStorage = rxStorage;
160 void SAL_CALL UriBindingHelper::setUriBinding( const OUString& /*uri*/, const uno::Reference< io::XInputStream >&)
161 throw (uno::Exception, uno::RuntimeException)
165 uno::Reference< io::XInputStream > SAL_CALL UriBindingHelper::getUriBinding( const OUString& uri )
166 throw (uno::Exception, uno::RuntimeException)
168 uno::Reference< io::XInputStream > xInputStream;
169 if ( mxStorage.is() )
171 xInputStream = OpenInputStream( mxStorage, uri );
173 else
175 SvFileStream* pStream = new SvFileStream( uri, STREAM_READ );
176 pStream->Seek( STREAM_SEEK_TO_END );
177 sal_uLong nBytes = pStream->Tell();
178 pStream->Seek( STREAM_SEEK_TO_BEGIN );
179 SvLockBytesRef xLockBytes = new SvLockBytes( pStream, sal_True );
180 xInputStream = new utl::OInputStreamHelper( xLockBytes, nBytes );
182 return xInputStream;
185 uno::Reference < io::XInputStream > UriBindingHelper::OpenInputStream( const uno::Reference < embed::XStorage >& rxStore, const OUString& rURI )
187 OSL_ASSERT(!rURI.isEmpty());
188 uno::Reference < io::XInputStream > xInStream;
190 sal_Int32 nSepPos = rURI.indexOf( '/' );
191 if ( nSepPos == -1 )
193 // Cloning because of I can't keep all storage references open
194 // MBA with think about a better API...
195 const OUString sName = ::rtl::Uri::decode(
196 rURI, rtl_UriDecodeStrict, rtl_UriCharClassRelSegment);
197 if (sName.isEmpty() && !rURI.isEmpty())
198 throw uno::Exception(OUString(
199 "Could not decode URI for stream element."), 0);
201 uno::Reference< io::XStream > xStream;
202 xStream = rxStore->cloneStreamElement( sName );
203 if ( !xStream.is() )
204 throw uno::RuntimeException();
205 xInStream = xStream->getInputStream();
207 else
209 const OUString aStoreName = ::rtl::Uri::decode(
210 rURI.copy( 0, nSepPos ), rtl_UriDecodeStrict, rtl_UriCharClassRelSegment);
211 if (aStoreName.isEmpty() && !rURI.isEmpty())
212 throw uno::Exception(
213 OUString(
214 "Could not decode URI for stream element."), 0);
216 OUString aElement = rURI.copy( nSepPos+1 );
217 uno::Reference < embed::XStorage > xSubStore = rxStore->openStorageElement( aStoreName, embed::ElementModes::READ );
218 xInStream = OpenInputStream( xSubStore, aElement );
220 return xInStream;
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */