Bump version to 5.0-14
[LibreOffice.git] / xmlsecurity / source / helper / xmlsignaturehelper.cxx
blob84004ae5ef70a2863f4d11d18a59763588f1a3fa
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 <xmlsecurity/documentsignaturehelper.hxx>
23 #include "xsecctl.hxx"
25 #include "xmlsignaturehelper2.hxx"
27 #include <tools/stream.hxx>
28 #include <tools/debug.hxx>
29 #include <tools/datetime.hxx>
31 #include <xmloff/attrlist.hxx>
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>
42 #include <tools/date.hxx>
43 #include <tools/time.hxx>
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"
49 using namespace ::com::sun::star;
50 using namespace ::com::sun::star::uno;
52 XMLSignatureHelper::XMLSignatureHelper( const uno::Reference< uno::XComponentContext >& rxCtx)
53 : mxCtx(rxCtx), mbODFPre1_2(false)
55 mpXSecController = new XSecController(rxCtx);
56 mxSecurityController = mpXSecController;
57 mbError = false;
60 XMLSignatureHelper::~XMLSignatureHelper()
64 bool XMLSignatureHelper::Init()
66 DBG_ASSERT( !mxSEInitializer.is(), "XMLSignatureHelper::Init - mxSEInitializer already set!" );
67 DBG_ASSERT( !mxSecurityContext.is(), "XMLSignatureHelper::Init - mxSecurityContext already set!" );
69 mxSEInitializer = com::sun::star::xml::crypto::SEInitializer::create( mxCtx );
71 if ( mxSEInitializer.is() )
72 mxSecurityContext = mxSEInitializer->createSecurityContext( OUString() );
74 return mxSecurityContext.is();
77 void XMLSignatureHelper::SetStorage(
78 const Reference < css::embed::XStorage >& rxStorage,
79 const OUString& sODFVersion)
81 DBG_ASSERT( !mxUriBinding.is(), "SetStorage - UriBinding already set!" );
82 mxUriBinding = new UriBindingHelper( rxStorage );
83 DBG_ASSERT(rxStorage.is(), "SetStorage - empty storage!");
84 mbODFPre1_2 = DocumentSignatureHelper::isODFPre_1_2(sODFVersion);
88 void XMLSignatureHelper::SetStartVerifySignatureHdl( const Link<>& rLink )
90 maStartVerifySignatureHdl = rLink;
94 void XMLSignatureHelper::StartMission()
96 if ( !mxUriBinding.is() )
97 mxUriBinding = new UriBindingHelper();
99 mpXSecController->startMission( mxUriBinding, mxSecurityContext );
102 void XMLSignatureHelper::EndMission()
104 mpXSecController->endMission();
107 sal_Int32 XMLSignatureHelper::GetNewSecurityId()
109 return mpXSecController->getNewSecurityId();
112 void XMLSignatureHelper::SetX509Certificate(
113 sal_Int32 nSecurityId,
114 const OUString& ouX509IssuerName,
115 const OUString& ouX509SerialNumber,
116 const OUString& ouX509Cert)
118 mpXSecController->setX509Certificate(
119 nSecurityId,
120 ouX509IssuerName,
121 ouX509SerialNumber,
122 ouX509Cert);
125 void XMLSignatureHelper::SetDateTime( sal_Int32 nSecurityId, const ::Date& rDate, const tools::Time& rTime )
127 css::util::DateTime stDateTime = ::DateTime(rDate, rTime).GetUNODateTime();
128 mpXSecController->setDate( nSecurityId, stDateTime );
131 void XMLSignatureHelper::AddForSigning( sal_Int32 nSecurityId, const OUString& uri, const OUString& objectURL, bool bBinary )
133 mpXSecController->signAStream( nSecurityId, uri, objectURL, bBinary );
137 uno::Reference<xml::sax::XWriter> XMLSignatureHelper::CreateDocumentHandlerWithHeader(
138 const com::sun::star::uno::Reference< com::sun::star::io::XOutputStream >& xOutputStream )
141 * get SAX writer component
143 uno::Reference< lang::XMultiComponentFactory > xMCF( mxCtx->getServiceManager() );
144 uno::Reference< xml::sax::XWriter > xSaxWriter = xml::sax::Writer::create(mxCtx);
147 * connect XML writer to output stream
149 xSaxWriter->setOutputStream( xOutputStream );
152 * write the xml context for signatures
154 OUString tag_AllSignatures(TAG_DOCUMENTSIGNATURES);
156 SvXMLAttributeList *pAttributeList = new SvXMLAttributeList();
157 OUString sNamespace;
158 if (mbODFPre1_2)
159 sNamespace = NS_DOCUMENTSIGNATURES;
160 else
161 sNamespace = NS_DOCUMENTSIGNATURES_ODF_1_2;
163 pAttributeList->AddAttribute(
164 OUString(ATTR_XMLNS),
165 sNamespace);
167 xSaxWriter->startDocument();
168 xSaxWriter->startElement(
169 tag_AllSignatures,
170 uno::Reference< com::sun::star::xml::sax::XAttributeList > (pAttributeList));
172 return xSaxWriter;
175 void XMLSignatureHelper::CloseDocumentHandler( const uno::Reference<xml::sax::XDocumentHandler>& xDocumentHandler )
177 OUString tag_AllSignatures(TAG_DOCUMENTSIGNATURES);
178 xDocumentHandler->endElement( tag_AllSignatures );
179 xDocumentHandler->endDocument();
182 void XMLSignatureHelper::ExportSignature(
183 const uno::Reference< xml::sax::XDocumentHandler >& xDocumentHandler,
184 const SignatureInformation& signatureInfo )
186 XSecController::exportSignature(xDocumentHandler, signatureInfo);
189 bool XMLSignatureHelper::CreateAndWriteSignature( const uno::Reference< xml::sax::XDocumentHandler >& xDocumentHandler )
191 mbError = false;
194 * create a signature listener
198 * configure the signature creation listener
202 * write signatures
204 if ( !mpXSecController->WriteSignature( xDocumentHandler ) )
206 mbError = true;
210 * clear up the signature creation listener
213 return !mbError;
216 bool XMLSignatureHelper::ReadAndVerifySignature( const com::sun::star::uno::Reference< com::sun::star::io::XInputStream >& xInputStream )
218 mbError = false;
220 DBG_ASSERT(xInputStream.is(), "input stream missing");
223 * prepare ParserInputSrouce
225 xml::sax::InputSource aParserInput;
226 aParserInput.aInputStream = xInputStream;
229 * get SAX parser component
231 uno::Reference< xml::sax::XParser > xParser = xml::sax::Parser::create(mxCtx);
234 * create a signature reader
236 uno::Reference< xml::sax::XDocumentHandler > xHandler
237 = mpXSecController->createSignatureReader( );
240 * create a signature listener
242 ImplXMLSignatureListener* pSignatureListener = new ImplXMLSignatureListener(
243 LINK( this, XMLSignatureHelper, SignatureCreationResultListener ),
244 LINK( this, XMLSignatureHelper, SignatureVerifyResultListener ),
245 LINK( this, XMLSignatureHelper, StartVerifySignatureElement ) );
248 * configure the signature verify listener
252 * setup the connection:
253 * Parser -> SignatureListener -> SignatureReader
255 pSignatureListener->setNextHandler(xHandler);
256 xParser->setDocumentHandler( pSignatureListener );
259 * parser the stream
263 xParser->parseStream( aParserInput );
265 catch( xml::sax::SAXParseException& )
267 mbError = true;
269 catch( xml::sax::SAXException& )
271 mbError = true;
273 catch( com::sun::star::io::IOException& )
275 mbError = true;
277 catch( uno::Exception& )
279 mbError = true;
283 * clear up the connection
285 pSignatureListener->setNextHandler( NULL );
288 * clear up the signature verify listener
292 * release the signature reader
294 mpXSecController->releaseSignatureReader( );
296 return !mbError;
299 SignatureInformation XMLSignatureHelper::GetSignatureInformation( sal_Int32 nSecurityId ) const
301 return mpXSecController->getSignatureInformation( nSecurityId );
304 SignatureInformations XMLSignatureHelper::GetSignatureInformations() const
306 return mpXSecController->getSignatureInformations();
309 uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > XMLSignatureHelper::GetSecurityEnvironment()
311 return (mxSecurityContext.is()?(mxSecurityContext->getSecurityEnvironment()): uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment >());
314 IMPL_LINK( XMLSignatureHelper, SignatureCreationResultListener, XMLSignatureCreationResult*, pResult )
316 maCreationResults.insert( maCreationResults.begin() + maCreationResults.size(), *pResult );
317 if ( pResult->nSignatureCreationResult != com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED )
318 mbError = true;
319 return 0;
322 IMPL_LINK( XMLSignatureHelper, SignatureVerifyResultListener, XMLSignatureVerifyResult*, pResult )
324 maVerifyResults.insert( maVerifyResults.begin() + maVerifyResults.size(), *pResult );
325 if ( pResult->nSignatureVerifyResult != com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED )
326 mbError = true;
327 return 0;
330 IMPL_LINK( XMLSignatureHelper, StartVerifySignatureElement, const uno::Reference< com::sun::star::xml::sax::XAttributeList >*, pAttrs )
332 if ( !maStartVerifySignatureHdl.IsSet() || maStartVerifySignatureHdl.Call( (void*)pAttrs ) )
334 sal_Int32 nSignatureId = mpXSecController->getNewSecurityId();
335 mpXSecController->addSignature( nSignatureId );
338 return 0;
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */