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