1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <xmlsecurity/xmlsignaturehelper.hxx>
21 #include "xmlsignaturehelper2.hxx"
23 #include <tools/solar.h>
24 #include <unotools/streamhelper.hxx>
26 #include <com/sun/star/embed/XStorage.hpp>
27 #include <com/sun/star/embed/XStorageRawAccess.hpp>
28 #include <com/sun/star/embed/ElementModes.hpp>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <osl/diagnose.h>
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
, std::exception
)
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
, std::exception
)
62 XMLSignatureVerifyResult
aResult( securityId
, nResult
);
63 maVerifyResultListenerListener
.Call( &aResult
);
67 void SAL_CALL
ImplXMLSignatureListener::startDocument( )
68 throw (com::sun::star::xml::sax::SAXException
, com::sun::star::uno::RuntimeException
, std::exception
)
70 if (m_xNextHandler
.is())
72 m_xNextHandler
->startDocument();
76 void SAL_CALL
ImplXMLSignatureListener::endDocument( )
77 throw (com::sun::star::xml::sax::SAXException
, com::sun::star::uno::RuntimeException
, std::exception
)
79 if (m_xNextHandler
.is())
81 m_xNextHandler
->endDocument();
85 void SAL_CALL
ImplXMLSignatureListener::startElement( const OUString
& aName
, const com::sun::star::uno::Reference
< com::sun::star::xml::sax::XAttributeList
>& xAttribs
)
86 throw (com::sun::star::xml::sax::SAXException
, com::sun::star::uno::RuntimeException
, std::exception
)
88 if ( aName
== "Signature" )
90 maStartVerifySignatureElementListener
.Call( (void*)&xAttribs
);
93 if (m_xNextHandler
.is())
95 m_xNextHandler
->startElement( aName
, xAttribs
);
99 void SAL_CALL
ImplXMLSignatureListener::endElement( const OUString
& aName
)
100 throw (com::sun::star::xml::sax::SAXException
, com::sun::star::uno::RuntimeException
, std::exception
)
102 if (m_xNextHandler
.is())
104 m_xNextHandler
->endElement( aName
);
108 void SAL_CALL
ImplXMLSignatureListener::characters( const OUString
& aChars
)
109 throw (com::sun::star::xml::sax::SAXException
, com::sun::star::uno::RuntimeException
, std::exception
)
111 if (m_xNextHandler
.is())
113 m_xNextHandler
->characters( aChars
);
117 void SAL_CALL
ImplXMLSignatureListener::ignorableWhitespace( const OUString
& aWhitespaces
)
118 throw (com::sun::star::xml::sax::SAXException
, com::sun::star::uno::RuntimeException
, std::exception
)
120 if (m_xNextHandler
.is())
122 m_xNextHandler
->ignorableWhitespace( aWhitespaces
);
126 void SAL_CALL
ImplXMLSignatureListener::processingInstruction( const OUString
& aTarget
, const OUString
& aData
)
127 throw (com::sun::star::xml::sax::SAXException
, com::sun::star::uno::RuntimeException
, std::exception
)
129 if (m_xNextHandler
.is())
131 m_xNextHandler
->processingInstruction( aTarget
, aData
);
135 void SAL_CALL
ImplXMLSignatureListener::setDocumentLocator( const com::sun::star::uno::Reference
< com::sun::star::xml::sax::XLocator
>& xLocator
)
136 throw (com::sun::star::xml::sax::SAXException
, com::sun::star::uno::RuntimeException
, std::exception
)
138 if (m_xNextHandler
.is())
140 m_xNextHandler
->setDocumentLocator( xLocator
);
146 UriBindingHelper::UriBindingHelper()
150 UriBindingHelper::UriBindingHelper( const com::sun::star::uno::Reference
< com::sun::star::embed::XStorage
>& rxStorage
)
152 mxStorage
= rxStorage
;
155 void SAL_CALL
UriBindingHelper::setUriBinding( const OUString
& /*uri*/, const uno::Reference
< io::XInputStream
>&)
156 throw (uno::Exception
, uno::RuntimeException
, std::exception
)
160 uno::Reference
< io::XInputStream
> SAL_CALL
UriBindingHelper::getUriBinding( const OUString
& uri
)
161 throw (uno::Exception
, uno::RuntimeException
, std::exception
)
163 uno::Reference
< io::XInputStream
> xInputStream
;
164 if ( mxStorage
.is() )
166 xInputStream
= OpenInputStream( mxStorage
, uri
);
170 SvFileStream
* pStream
= new SvFileStream( uri
, StreamMode::READ
);
171 pStream
->Seek( STREAM_SEEK_TO_END
);
172 sal_uLong nBytes
= pStream
->Tell();
173 pStream
->Seek( STREAM_SEEK_TO_BEGIN
);
174 SvLockBytesRef xLockBytes
= new SvLockBytes( pStream
, true );
175 xInputStream
= new utl::OInputStreamHelper( xLockBytes
, nBytes
);
180 uno::Reference
< io::XInputStream
> UriBindingHelper::OpenInputStream( const uno::Reference
< embed::XStorage
>& rxStore
, const OUString
& rURI
)
182 OSL_ASSERT(!rURI
.isEmpty());
183 uno::Reference
< io::XInputStream
> xInStream
;
185 sal_Int32 nSepPos
= rURI
.indexOf( '/' );
188 // Cloning because of I can't keep all storage references open
189 // MBA with think about a better API...
190 const OUString sName
= ::rtl::Uri::decode(
191 rURI
, rtl_UriDecodeStrict
, rtl_UriCharClassRelSegment
);
192 if (sName
.isEmpty() && !rURI
.isEmpty())
193 throw uno::Exception("Could not decode URI for stream element.", 0);
195 uno::Reference
< io::XStream
> xStream
;
196 xStream
= rxStore
->cloneStreamElement( sName
);
198 throw uno::RuntimeException();
199 xInStream
= xStream
->getInputStream();
203 const OUString aStoreName
= ::rtl::Uri::decode(
204 rURI
.copy( 0, nSepPos
), rtl_UriDecodeStrict
, rtl_UriCharClassRelSegment
);
205 if (aStoreName
.isEmpty() && !rURI
.isEmpty())
206 throw uno::Exception("Could not decode URI for stream element.", 0);
208 OUString aElement
= rURI
.copy( nSepPos
+1 );
209 uno::Reference
< embed::XStorage
> xSubStore
= rxStore
->openStorageElement( aStoreName
, embed::ElementModes::READ
);
210 xInStream
= OpenInputStream( xSubStore
, aElement
);
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */