Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / xmlsecurity / source / helper / xmlsignaturehelper.cxx
blobca9ef0662171538908158c408d20d150bd8fde60
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>
30 #include <xmloff/attrlist.hxx>
32 #include <com/sun/star/io/XOutputStream.hpp>
33 #include <com/sun/star/io/XInputStream.hpp>
34 #include <com/sun/star/io/XActiveDataSource.hpp>
35 #include <com/sun/star/lang/XComponent.hpp>
36 #include <com/sun/star/security/SerialNumberAdapter.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( ::rtl::OUString() );
74 return mxSecurityContext.is();
77 void XMLSignatureHelper::SetStorage(
78 const Reference < css::embed::XStorage >& rxStorage,
79 ::rtl::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 rtl::OUString& ouX509IssuerName,
115 const rtl::OUString& ouX509SerialNumber,
116 const rtl::OUString& ouX509Cert)
118 mpXSecController->setX509Certificate(
119 nSecurityId,
120 ouX509IssuerName,
121 ouX509SerialNumber,
122 ouX509Cert);
125 void XMLSignatureHelper::SetDateTime( sal_Int32 nSecurityId, const Date& rDate, const Time& rTime )
127 ::com::sun::star::util::DateTime stDateTime;
128 stDateTime.HundredthSeconds = (::sal_uInt16)rTime.Get100Sec();
129 stDateTime.Seconds = (::sal_uInt16)rTime.GetSec();
130 stDateTime.Minutes = (::sal_uInt16)rTime.GetMin();
131 stDateTime.Hours = (::sal_uInt16)rTime.GetHour();
132 stDateTime.Day = (::sal_uInt16)rDate.GetDay();
133 stDateTime.Month = (::sal_uInt16)rDate.GetMonth();
134 stDateTime.Year = (::sal_uInt16)rDate.GetYear();
135 mpXSecController->setDate( nSecurityId, stDateTime );
138 void XMLSignatureHelper::AddForSigning( sal_Int32 nSecurityId, const rtl::OUString& uri, const rtl::OUString& objectURL, sal_Bool bBinary )
140 mpXSecController->signAStream( nSecurityId, uri, objectURL, bBinary );
144 uno::Reference<xml::sax::XWriter> XMLSignatureHelper::CreateDocumentHandlerWithHeader(
145 const com::sun::star::uno::Reference< com::sun::star::io::XOutputStream >& xOutputStream )
148 * get SAX writer component
150 uno::Reference< lang::XMultiComponentFactory > xMCF( mxCtx->getServiceManager() );
151 uno::Reference< xml::sax::XWriter > xSaxWriter = xml::sax::Writer::create(mxCtx);
154 * connect XML writer to output stream
156 xSaxWriter->setOutputStream( xOutputStream );
159 * write the xml context for signatures
161 rtl::OUString tag_AllSignatures(TAG_DOCUMENTSIGNATURES);
163 SvXMLAttributeList *pAttributeList = new SvXMLAttributeList();
164 rtl::OUString sNamespace;
165 if (mbODFPre1_2)
166 sNamespace = rtl::OUString(NS_DOCUMENTSIGNATURES);
167 else
168 sNamespace = rtl::OUString(NS_DOCUMENTSIGNATURES_ODF_1_2);
170 pAttributeList->AddAttribute(
171 rtl::OUString(ATTR_XMLNS),
172 sNamespace);
174 xSaxWriter->startDocument();
175 xSaxWriter->startElement(
176 tag_AllSignatures,
177 uno::Reference< com::sun::star::xml::sax::XAttributeList > (pAttributeList));
179 return xSaxWriter;
182 void XMLSignatureHelper::CloseDocumentHandler( const uno::Reference<xml::sax::XDocumentHandler>& xDocumentHandler )
184 rtl::OUString tag_AllSignatures(TAG_DOCUMENTSIGNATURES);
185 xDocumentHandler->endElement( tag_AllSignatures );
186 xDocumentHandler->endDocument();
189 void XMLSignatureHelper::ExportSignature(
190 const uno::Reference< xml::sax::XDocumentHandler >& xDocumentHandler,
191 const SignatureInformation& signatureInfo )
193 mpXSecController->exportSignature(xDocumentHandler, signatureInfo);
196 bool XMLSignatureHelper::CreateAndWriteSignature( const uno::Reference< xml::sax::XDocumentHandler >& xDocumentHandler )
198 mbError = false;
201 * create a signature listener
205 * configure the signature creation listener
209 * write signatures
211 if ( !mpXSecController->WriteSignature( xDocumentHandler ) )
213 mbError = true;
217 * clear up the signature creation listener
220 return !mbError;
223 bool XMLSignatureHelper::ReadAndVerifySignature( const com::sun::star::uno::Reference< com::sun::star::io::XInputStream >& xInputStream )
225 mbError = false;
227 DBG_ASSERT(xInputStream.is(), "input stream missing");
230 * prepare ParserInputSrouce
232 xml::sax::InputSource aParserInput;
233 aParserInput.aInputStream = xInputStream;
236 * get SAX parser component
238 uno::Reference< xml::sax::XParser > xParser = xml::sax::Parser::create(mxCtx);
241 * create a signature reader
243 uno::Reference< xml::sax::XDocumentHandler > xHandler
244 = mpXSecController->createSignatureReader( );
247 * create a signature listener
249 ImplXMLSignatureListener* pSignatureListener = new ImplXMLSignatureListener(
250 LINK( this, XMLSignatureHelper, SignatureCreationResultListener ),
251 LINK( this, XMLSignatureHelper, SignatureVerifyResultListener ),
252 LINK( this, XMLSignatureHelper, StartVerifySignatureElement ) );
255 * configure the signature verify listener
259 * setup the connection:
260 * Parser -> SignatureListener -> SignatureReader
262 pSignatureListener->setNextHandler(xHandler);
263 xParser->setDocumentHandler( pSignatureListener );
266 * parser the stream
270 xParser->parseStream( aParserInput );
272 catch( xml::sax::SAXParseException& )
274 mbError = true;
276 catch( xml::sax::SAXException& )
278 mbError = true;
280 catch( com::sun::star::io::IOException& )
282 mbError = true;
284 catch( uno::Exception& )
286 mbError = true;
290 * clear up the connection
292 pSignatureListener->setNextHandler( NULL );
295 * clear up the signature verify listener
299 * release the signature reader
301 mpXSecController->releaseSignatureReader( );
303 return !mbError;
306 SignatureInformation XMLSignatureHelper::GetSignatureInformation( sal_Int32 nSecurityId ) const
308 return mpXSecController->getSignatureInformation( nSecurityId );
311 SignatureInformations XMLSignatureHelper::GetSignatureInformations() const
313 return mpXSecController->getSignatureInformations();
316 uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > XMLSignatureHelper::GetSecurityEnvironment()
318 return (mxSecurityContext.is()?(mxSecurityContext->getSecurityEnvironment()): uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment >());
321 IMPL_LINK( XMLSignatureHelper, SignatureCreationResultListener, XMLSignatureCreationResult*, pResult )
323 maCreationResults.insert( maCreationResults.begin() + maCreationResults.size(), *pResult );
324 if ( pResult->nSignatureCreationResult != com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED )
325 mbError = true;
326 return 0;
329 IMPL_LINK( XMLSignatureHelper, SignatureVerifyResultListener, XMLSignatureVerifyResult*, pResult )
331 maVerifyResults.insert( maVerifyResults.begin() + maVerifyResults.size(), *pResult );
332 if ( pResult->nSignatureVerifyResult != com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED )
333 mbError = true;
334 return 0;
337 IMPL_LINK( XMLSignatureHelper, StartVerifySignatureElement, const uno::Reference< com::sun::star::xml::sax::XAttributeList >*, pAttrs )
339 if ( !maStartVerifySignatureHdl.IsSet() || maStartVerifySignatureHdl.Call( (void*)pAttrs ) )
341 sal_Int32 nSignatureId = mpXSecController->getNewSecurityId();
342 mpXSecController->addSignature( nSignatureId );
345 return 0;
348 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */