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 <xmlsecurity/documentsignaturehelper.hxx>
23 : #include "xsecctl.hxx"
24 :
25 : #include "xmlsignaturehelper2.hxx"
26 :
27 : #include <tools/stream.hxx>
28 : #include <tools/debug.hxx>
29 : #include <tools/datetime.hxx>
30 :
31 : #include <xmloff/attrlist.hxx>
32 :
33 : #include <com/sun/star/io/XOutputStream.hpp>
34 : #include <com/sun/star/io/XInputStream.hpp>
35 : #include <com/sun/star/io/XActiveDataSource.hpp>
36 : #include <com/sun/star/lang/XComponent.hpp>
37 : #include <com/sun/star/beans/XPropertySet.hpp>
38 : #include <com/sun/star/xml/sax/Parser.hpp>
39 : #include <com/sun/star/xml/sax/Writer.hpp>
40 : #include <com/sun/star/xml/crypto/SEInitializer.hpp>
41 :
42 : #include <tools/date.hxx>
43 : #include <tools/time.hxx>
44 :
45 : #define TAG_DOCUMENTSIGNATURES "document-signatures"
46 : #define NS_DOCUMENTSIGNATURES "http://openoffice.org/2004/documentsignatures"
47 : #define NS_DOCUMENTSIGNATURES_ODF_1_2 "urn:oasis:names:tc:opendocument:xmlns:digitalsignature:1.0"
48 :
49 : using namespace ::com::sun::star;
50 : using namespace ::com::sun::star::uno;
51 :
52 0 : XMLSignatureHelper::XMLSignatureHelper( const uno::Reference< uno::XComponentContext >& rxCtx)
53 0 : : mxCtx(rxCtx), mbODFPre1_2(false)
54 : {
55 0 : mpXSecController = new XSecController(rxCtx);
56 0 : mxSecurityController = mpXSecController;
57 0 : mbError = false;
58 0 : }
59 :
60 0 : XMLSignatureHelper::~XMLSignatureHelper()
61 : {
62 0 : }
63 :
64 0 : bool XMLSignatureHelper::Init()
65 : {
66 : DBG_ASSERT( !mxSEInitializer.is(), "XMLSignatureHelper::Init - mxSEInitializer already set!" );
67 : DBG_ASSERT( !mxSecurityContext.is(), "XMLSignatureHelper::Init - mxSecurityContext already set!" );
68 :
69 0 : mxSEInitializer = com::sun::star::xml::crypto::SEInitializer::create( mxCtx );
70 :
71 0 : if ( mxSEInitializer.is() )
72 0 : mxSecurityContext = mxSEInitializer->createSecurityContext( OUString() );
73 :
74 0 : return mxSecurityContext.is();
75 : }
76 :
77 0 : void XMLSignatureHelper::SetStorage(
78 : const Reference < css::embed::XStorage >& rxStorage,
79 : const OUString& sODFVersion)
80 : {
81 : DBG_ASSERT( !mxUriBinding.is(), "SetStorage - UriBinding already set!" );
82 0 : mxUriBinding = new UriBindingHelper( rxStorage );
83 : DBG_ASSERT(rxStorage.is(), "SetStorage - empty storage!");
84 0 : mbODFPre1_2 = DocumentSignatureHelper::isODFPre_1_2(sODFVersion);
85 0 : }
86 :
87 :
88 0 : void XMLSignatureHelper::SetStartVerifySignatureHdl( const Link<>& rLink )
89 : {
90 0 : maStartVerifySignatureHdl = rLink;
91 0 : }
92 :
93 :
94 0 : void XMLSignatureHelper::StartMission()
95 : {
96 0 : if ( !mxUriBinding.is() )
97 0 : mxUriBinding = new UriBindingHelper();
98 :
99 0 : mpXSecController->startMission( mxUriBinding, mxSecurityContext );
100 0 : }
101 :
102 0 : void XMLSignatureHelper::EndMission()
103 : {
104 0 : mpXSecController->endMission();
105 0 : }
106 :
107 0 : sal_Int32 XMLSignatureHelper::GetNewSecurityId()
108 : {
109 0 : return mpXSecController->getNewSecurityId();
110 : }
111 :
112 0 : void XMLSignatureHelper::SetX509Certificate(
113 : sal_Int32 nSecurityId,
114 : const OUString& ouX509IssuerName,
115 : const OUString& ouX509SerialNumber,
116 : const OUString& ouX509Cert)
117 : {
118 : mpXSecController->setX509Certificate(
119 : nSecurityId,
120 : ouX509IssuerName,
121 : ouX509SerialNumber,
122 0 : ouX509Cert);
123 0 : }
124 :
125 0 : void XMLSignatureHelper::SetDateTime( sal_Int32 nSecurityId, const ::Date& rDate, const tools::Time& rTime )
126 : {
127 0 : css::util::DateTime stDateTime = ::DateTime(rDate, rTime).GetUNODateTime();
128 0 : mpXSecController->setDate( nSecurityId, stDateTime );
129 0 : }
130 :
131 0 : void XMLSignatureHelper::AddForSigning( sal_Int32 nSecurityId, const OUString& uri, const OUString& objectURL, bool bBinary )
132 : {
133 0 : mpXSecController->signAStream( nSecurityId, uri, objectURL, bBinary );
134 0 : }
135 :
136 :
137 0 : uno::Reference<xml::sax::XWriter> XMLSignatureHelper::CreateDocumentHandlerWithHeader(
138 : const com::sun::star::uno::Reference< com::sun::star::io::XOutputStream >& xOutputStream )
139 : {
140 : /*
141 : * get SAX writer component
142 : */
143 0 : uno::Reference< lang::XMultiComponentFactory > xMCF( mxCtx->getServiceManager() );
144 0 : uno::Reference< xml::sax::XWriter > xSaxWriter = xml::sax::Writer::create(mxCtx);
145 :
146 : /*
147 : * connect XML writer to output stream
148 : */
149 0 : xSaxWriter->setOutputStream( xOutputStream );
150 :
151 : /*
152 : * write the xml context for signatures
153 : */
154 0 : OUString tag_AllSignatures(TAG_DOCUMENTSIGNATURES);
155 :
156 0 : SvXMLAttributeList *pAttributeList = new SvXMLAttributeList();
157 0 : OUString sNamespace;
158 0 : if (mbODFPre1_2)
159 0 : sNamespace = NS_DOCUMENTSIGNATURES;
160 : else
161 0 : sNamespace = NS_DOCUMENTSIGNATURES_ODF_1_2;
162 :
163 : pAttributeList->AddAttribute(
164 : OUString(ATTR_XMLNS),
165 0 : sNamespace);
166 :
167 0 : xSaxWriter->startDocument();
168 0 : xSaxWriter->startElement(
169 : tag_AllSignatures,
170 0 : uno::Reference< com::sun::star::xml::sax::XAttributeList > (pAttributeList));
171 :
172 0 : return xSaxWriter;
173 : }
174 :
175 0 : void XMLSignatureHelper::CloseDocumentHandler( const uno::Reference<xml::sax::XDocumentHandler>& xDocumentHandler )
176 : {
177 0 : OUString tag_AllSignatures(TAG_DOCUMENTSIGNATURES);
178 0 : xDocumentHandler->endElement( tag_AllSignatures );
179 0 : xDocumentHandler->endDocument();
180 0 : }
181 :
182 0 : void XMLSignatureHelper::ExportSignature(
183 : const uno::Reference< xml::sax::XDocumentHandler >& xDocumentHandler,
184 : const SignatureInformation& signatureInfo )
185 : {
186 0 : XSecController::exportSignature(xDocumentHandler, signatureInfo);
187 0 : }
188 :
189 0 : bool XMLSignatureHelper::CreateAndWriteSignature( const uno::Reference< xml::sax::XDocumentHandler >& xDocumentHandler )
190 : {
191 0 : mbError = false;
192 :
193 : /*
194 : * create a signature listener
195 : */
196 :
197 : /*
198 : * configure the signature creation listener
199 : */
200 :
201 : /*
202 : * write signatures
203 : */
204 0 : if ( !mpXSecController->WriteSignature( xDocumentHandler ) )
205 : {
206 0 : mbError = true;
207 : }
208 :
209 : /*
210 : * clear up the signature creation listener
211 : */
212 :
213 0 : return !mbError;
214 : }
215 :
216 0 : bool XMLSignatureHelper::ReadAndVerifySignature( const com::sun::star::uno::Reference< com::sun::star::io::XInputStream >& xInputStream )
217 : {
218 0 : mbError = false;
219 :
220 : DBG_ASSERT(xInputStream.is(), "input stream missing");
221 :
222 : /*
223 : * prepare ParserInputSrouce
224 : */
225 0 : xml::sax::InputSource aParserInput;
226 0 : aParserInput.aInputStream = xInputStream;
227 :
228 : /*
229 : * get SAX parser component
230 : */
231 0 : uno::Reference< xml::sax::XParser > xParser = xml::sax::Parser::create(mxCtx);
232 :
233 : /*
234 : * create a signature reader
235 : */
236 : uno::Reference< xml::sax::XDocumentHandler > xHandler
237 0 : = mpXSecController->createSignatureReader( );
238 :
239 : /*
240 : * create a signature listener
241 : */
242 : ImplXMLSignatureListener* pSignatureListener = new ImplXMLSignatureListener(
243 : LINK( this, XMLSignatureHelper, SignatureCreationResultListener ),
244 : LINK( this, XMLSignatureHelper, SignatureVerifyResultListener ),
245 0 : LINK( this, XMLSignatureHelper, StartVerifySignatureElement ) );
246 :
247 : /*
248 : * configure the signature verify listener
249 : */
250 :
251 : /*
252 : * setup the connection:
253 : * Parser -> SignatureListener -> SignatureReader
254 : */
255 0 : pSignatureListener->setNextHandler(xHandler);
256 0 : xParser->setDocumentHandler( pSignatureListener );
257 :
258 : /*
259 : * parser the stream
260 : */
261 : try
262 : {
263 0 : xParser->parseStream( aParserInput );
264 : }
265 0 : catch( xml::sax::SAXParseException& )
266 : {
267 0 : mbError = true;
268 : }
269 0 : catch( xml::sax::SAXException& )
270 : {
271 0 : mbError = true;
272 : }
273 0 : catch( com::sun::star::io::IOException& )
274 : {
275 0 : mbError = true;
276 : }
277 0 : catch( uno::Exception& )
278 : {
279 0 : mbError = true;
280 : }
281 :
282 : /*
283 : * clear up the connection
284 : */
285 0 : pSignatureListener->setNextHandler( NULL );
286 :
287 : /*
288 : * clear up the signature verify listener
289 : */
290 :
291 : /*
292 : * release the signature reader
293 : */
294 0 : mpXSecController->releaseSignatureReader( );
295 :
296 0 : return !mbError;
297 : }
298 :
299 0 : SignatureInformation XMLSignatureHelper::GetSignatureInformation( sal_Int32 nSecurityId ) const
300 : {
301 0 : return mpXSecController->getSignatureInformation( nSecurityId );
302 : }
303 :
304 0 : SignatureInformations XMLSignatureHelper::GetSignatureInformations() const
305 : {
306 0 : return mpXSecController->getSignatureInformations();
307 : }
308 :
309 0 : uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > XMLSignatureHelper::GetSecurityEnvironment()
310 : {
311 0 : return (mxSecurityContext.is()?(mxSecurityContext->getSecurityEnvironment()): uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment >());
312 : }
313 :
314 0 : IMPL_LINK( XMLSignatureHelper, SignatureCreationResultListener, XMLSignatureCreationResult*, pResult )
315 : {
316 0 : maCreationResults.insert( maCreationResults.begin() + maCreationResults.size(), *pResult );
317 0 : if ( pResult->nSignatureCreationResult != com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED )
318 0 : mbError = true;
319 0 : return 0;
320 : }
321 :
322 0 : IMPL_LINK( XMLSignatureHelper, SignatureVerifyResultListener, XMLSignatureVerifyResult*, pResult )
323 : {
324 0 : maVerifyResults.insert( maVerifyResults.begin() + maVerifyResults.size(), *pResult );
325 0 : if ( pResult->nSignatureVerifyResult != com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED )
326 0 : mbError = true;
327 0 : return 0;
328 : }
329 :
330 0 : IMPL_LINK( XMLSignatureHelper, StartVerifySignatureElement, const uno::Reference< com::sun::star::xml::sax::XAttributeList >*, pAttrs )
331 : {
332 0 : if ( !maStartVerifySignatureHdl.IsSet() || maStartVerifySignatureHdl.Call( const_cast<css::uno::Reference<css::xml::sax::XAttributeList> *>(pAttrs) ) )
333 : {
334 0 : sal_Int32 nSignatureId = mpXSecController->getNewSecurityId();
335 0 : mpXSecController->addSignature( nSignatureId );
336 : }
337 :
338 0 : return 0;
339 : }
340 :
341 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|