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 .
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/beans/XPropertySet.hpp>
37 #include <com/sun/star/xml/sax/Parser.hpp>
38 #include <com/sun/star/xml/sax/Writer.hpp>
39 #include <com/sun/star/xml/crypto/SEInitializer.hpp>
41 #include <tools/date.hxx>
42 #include <tools/time.hxx>
44 #define TAG_DOCUMENTSIGNATURES "document-signatures"
45 #define NS_DOCUMENTSIGNATURES "http://openoffice.org/2004/documentsignatures"
46 #define NS_DOCUMENTSIGNATURES_ODF_1_2 "urn:oasis:names:tc:opendocument:xmlns:digitalsignature:1.0"
48 using namespace ::com::sun::star
;
49 using namespace ::com::sun::star::uno
;
51 XMLSignatureHelper::XMLSignatureHelper( const uno::Reference
< uno::XComponentContext
>& rxCtx
)
52 : mxCtx(rxCtx
), mbODFPre1_2(false)
54 mpXSecController
= new XSecController(rxCtx
);
55 mxSecurityController
= mpXSecController
;
59 XMLSignatureHelper::~XMLSignatureHelper()
63 bool XMLSignatureHelper::Init()
65 DBG_ASSERT( !mxSEInitializer
.is(), "XMLSignatureHelper::Init - mxSEInitializer already set!" );
66 DBG_ASSERT( !mxSecurityContext
.is(), "XMLSignatureHelper::Init - mxSecurityContext already set!" );
68 mxSEInitializer
= com::sun::star::xml::crypto::SEInitializer::create( mxCtx
);
70 if ( mxSEInitializer
.is() )
71 mxSecurityContext
= mxSEInitializer
->createSecurityContext( OUString() );
73 return mxSecurityContext
.is();
76 void XMLSignatureHelper::SetStorage(
77 const Reference
< css::embed::XStorage
>& rxStorage
,
80 DBG_ASSERT( !mxUriBinding
.is(), "SetStorage - UriBinding already set!" );
81 mxUriBinding
= new UriBindingHelper( rxStorage
);
82 DBG_ASSERT(rxStorage
.is(), "SetStorage - empty storage!");
83 mbODFPre1_2
= DocumentSignatureHelper::isODFPre_1_2(sODFVersion
);
87 void XMLSignatureHelper::SetStartVerifySignatureHdl( const Link
& rLink
)
89 maStartVerifySignatureHdl
= rLink
;
93 void XMLSignatureHelper::StartMission()
95 if ( !mxUriBinding
.is() )
96 mxUriBinding
= new UriBindingHelper();
98 mpXSecController
->startMission( mxUriBinding
, mxSecurityContext
);
101 void XMLSignatureHelper::EndMission()
103 mpXSecController
->endMission();
106 sal_Int32
XMLSignatureHelper::GetNewSecurityId()
108 return mpXSecController
->getNewSecurityId();
111 void XMLSignatureHelper::SetX509Certificate(
112 sal_Int32 nSecurityId
,
113 const OUString
& ouX509IssuerName
,
114 const OUString
& ouX509SerialNumber
,
115 const OUString
& ouX509Cert
)
117 mpXSecController
->setX509Certificate(
124 void XMLSignatureHelper::SetDateTime( sal_Int32 nSecurityId
, const Date
& rDate
, const Time
& rTime
)
126 ::com::sun::star::util::DateTime stDateTime
;
127 stDateTime
.NanoSeconds
= rTime
.GetNanoSec();
128 stDateTime
.Seconds
= (::sal_uInt16
)rTime
.GetSec();
129 stDateTime
.Minutes
= (::sal_uInt16
)rTime
.GetMin();
130 stDateTime
.Hours
= (::sal_uInt16
)rTime
.GetHour();
131 stDateTime
.Day
= (::sal_uInt16
)rDate
.GetDay();
132 stDateTime
.Month
= (::sal_uInt16
)rDate
.GetMonth();
133 stDateTime
.Year
= (::sal_uInt16
)rDate
.GetYear();
134 mpXSecController
->setDate( nSecurityId
, stDateTime
);
137 void XMLSignatureHelper::AddForSigning( sal_Int32 nSecurityId
, const OUString
& uri
, const OUString
& objectURL
, sal_Bool bBinary
)
139 mpXSecController
->signAStream( nSecurityId
, uri
, objectURL
, bBinary
);
143 uno::Reference
<xml::sax::XWriter
> XMLSignatureHelper::CreateDocumentHandlerWithHeader(
144 const com::sun::star::uno::Reference
< com::sun::star::io::XOutputStream
>& xOutputStream
)
147 * get SAX writer component
149 uno::Reference
< lang::XMultiComponentFactory
> xMCF( mxCtx
->getServiceManager() );
150 uno::Reference
< xml::sax::XWriter
> xSaxWriter
= xml::sax::Writer::create(mxCtx
);
153 * connect XML writer to output stream
155 xSaxWriter
->setOutputStream( xOutputStream
);
158 * write the xml context for signatures
160 OUString
tag_AllSignatures(TAG_DOCUMENTSIGNATURES
);
162 SvXMLAttributeList
*pAttributeList
= new SvXMLAttributeList();
165 sNamespace
= OUString(NS_DOCUMENTSIGNATURES
);
167 sNamespace
= OUString(NS_DOCUMENTSIGNATURES_ODF_1_2
);
169 pAttributeList
->AddAttribute(
170 OUString(ATTR_XMLNS
),
173 xSaxWriter
->startDocument();
174 xSaxWriter
->startElement(
176 uno::Reference
< com::sun::star::xml::sax::XAttributeList
> (pAttributeList
));
181 void XMLSignatureHelper::CloseDocumentHandler( const uno::Reference
<xml::sax::XDocumentHandler
>& xDocumentHandler
)
183 OUString
tag_AllSignatures(TAG_DOCUMENTSIGNATURES
);
184 xDocumentHandler
->endElement( tag_AllSignatures
);
185 xDocumentHandler
->endDocument();
188 void XMLSignatureHelper::ExportSignature(
189 const uno::Reference
< xml::sax::XDocumentHandler
>& xDocumentHandler
,
190 const SignatureInformation
& signatureInfo
)
192 mpXSecController
->exportSignature(xDocumentHandler
, signatureInfo
);
195 bool XMLSignatureHelper::CreateAndWriteSignature( const uno::Reference
< xml::sax::XDocumentHandler
>& xDocumentHandler
)
200 * create a signature listener
204 * configure the signature creation listener
210 if ( !mpXSecController
->WriteSignature( xDocumentHandler
) )
216 * clear up the signature creation listener
222 bool XMLSignatureHelper::ReadAndVerifySignature( const com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
>& xInputStream
)
226 DBG_ASSERT(xInputStream
.is(), "input stream missing");
229 * prepare ParserInputSrouce
231 xml::sax::InputSource aParserInput
;
232 aParserInput
.aInputStream
= xInputStream
;
235 * get SAX parser component
237 uno::Reference
< xml::sax::XParser
> xParser
= xml::sax::Parser::create(mxCtx
);
240 * create a signature reader
242 uno::Reference
< xml::sax::XDocumentHandler
> xHandler
243 = mpXSecController
->createSignatureReader( );
246 * create a signature listener
248 ImplXMLSignatureListener
* pSignatureListener
= new ImplXMLSignatureListener(
249 LINK( this, XMLSignatureHelper
, SignatureCreationResultListener
),
250 LINK( this, XMLSignatureHelper
, SignatureVerifyResultListener
),
251 LINK( this, XMLSignatureHelper
, StartVerifySignatureElement
) );
254 * configure the signature verify listener
258 * setup the connection:
259 * Parser -> SignatureListener -> SignatureReader
261 pSignatureListener
->setNextHandler(xHandler
);
262 xParser
->setDocumentHandler( pSignatureListener
);
269 xParser
->parseStream( aParserInput
);
271 catch( xml::sax::SAXParseException
& )
275 catch( xml::sax::SAXException
& )
279 catch( com::sun::star::io::IOException
& )
283 catch( uno::Exception
& )
289 * clear up the connection
291 pSignatureListener
->setNextHandler( NULL
);
294 * clear up the signature verify listener
298 * release the signature reader
300 mpXSecController
->releaseSignatureReader( );
305 SignatureInformation
XMLSignatureHelper::GetSignatureInformation( sal_Int32 nSecurityId
) const
307 return mpXSecController
->getSignatureInformation( nSecurityId
);
310 SignatureInformations
XMLSignatureHelper::GetSignatureInformations() const
312 return mpXSecController
->getSignatureInformations();
315 uno::Reference
< ::com::sun::star::xml::crypto::XSecurityEnvironment
> XMLSignatureHelper::GetSecurityEnvironment()
317 return (mxSecurityContext
.is()?(mxSecurityContext
->getSecurityEnvironment()): uno::Reference
< ::com::sun::star::xml::crypto::XSecurityEnvironment
>());
320 IMPL_LINK( XMLSignatureHelper
, SignatureCreationResultListener
, XMLSignatureCreationResult
*, pResult
)
322 maCreationResults
.insert( maCreationResults
.begin() + maCreationResults
.size(), *pResult
);
323 if ( pResult
->nSignatureCreationResult
!= com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED
)
328 IMPL_LINK( XMLSignatureHelper
, SignatureVerifyResultListener
, XMLSignatureVerifyResult
*, pResult
)
330 maVerifyResults
.insert( maVerifyResults
.begin() + maVerifyResults
.size(), *pResult
);
331 if ( pResult
->nSignatureVerifyResult
!= com::sun::star::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED
)
336 IMPL_LINK( XMLSignatureHelper
, StartVerifySignatureElement
, const uno::Reference
< com::sun::star::xml::sax::XAttributeList
>*, pAttrs
)
338 if ( !maStartVerifySignatureHdl
.IsSet() || maStartVerifySignatureHdl
.Call( (void*)pAttrs
) )
340 sal_Int32 nSignatureId
= mpXSecController
->getNewSecurityId();
341 mpXSecController
->addSignature( nSignatureId
);
347 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */