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 "xsecctl.hxx"
22 #include "xsecparser.hxx"
23 #include <tools/debug.hxx>
25 #include <com/sun/star/xml/crypto/sax/XKeyCollector.hpp>
26 #include <com/sun/star/xml/crypto/sax/ElementMarkPriority.hpp>
27 #include <com/sun/star/xml/crypto/sax/XReferenceResolvedBroadcaster.hpp>
28 #include <com/sun/star/xml/crypto/sax/XReferenceCollector.hpp>
29 #include <com/sun/star/xml/crypto/sax/XSignatureVerifyResultBroadcaster.hpp>
30 #include <com/sun/star/xml/sax/SAXParseException.hpp>
31 #include <sal/log.hxx>
32 #include <unotools/datetime.hxx>
34 namespace cssu
= com::sun::star::uno
;
35 namespace cssl
= com::sun::star::lang
;
36 namespace cssxc
= com::sun::star::xml::crypto
;
37 namespace cssxs
= com::sun::star::xml::sax
;
39 /* xml security framework components */
40 #define SIGNATUREVERIFIER_COMPONENT "com.sun.star.xml.crypto.sax.SignatureVerifier"
42 /* protected: for signature verify */
43 cssu::Reference
< cssxc::sax::XReferenceResolvedListener
> XSecController::prepareSignatureToRead(
44 sal_Int32 nSecurityId
)
46 if ( m_nStatusOfSecurityComponents
!= INITIALIZED
)
51 sal_Int32 nIdOfSignatureElementCollector
;
52 cssu::Reference
< cssxc::sax::XReferenceResolvedListener
> xReferenceResolvedListener
;
54 nIdOfSignatureElementCollector
=
55 m_xSAXEventKeeper
->addSecurityElementCollector( cssxc::sax::ElementMarkPriority_BEFOREMODIFY
, sal_False
);
57 m_xSAXEventKeeper
->setSecurityId(nIdOfSignatureElementCollector
, nSecurityId
);
60 * create a SignatureVerifier
62 cssu::Reference
< cssl::XMultiComponentFactory
> xMCF( mxCtx
->getServiceManager() );
63 xReferenceResolvedListener
= cssu::Reference
< cssxc::sax::XReferenceResolvedListener
>(
64 xMCF
->createInstanceWithContext(
65 OUString( SIGNATUREVERIFIER_COMPONENT
), mxCtx
),
68 cssu::Reference
<cssl::XInitialization
> xInitialization(xReferenceResolvedListener
, cssu::UNO_QUERY
);
70 cssu::Sequence
<cssu::Any
> args(5);
71 args
[0] = cssu::makeAny(OUString::number(nSecurityId
));
72 args
[1] = cssu::makeAny(m_xSAXEventKeeper
);
73 args
[2] = cssu::makeAny(OUString::number(nIdOfSignatureElementCollector
));
74 args
[3] = cssu::makeAny(m_xSecurityContext
);
75 args
[4] = cssu::makeAny(m_xXMLSignature
);
76 xInitialization
->initialize(args
);
78 cssu::Reference
< cssxc::sax::XSignatureVerifyResultBroadcaster
>
79 signatureVerifyResultBroadcaster(xReferenceResolvedListener
, cssu::UNO_QUERY
);
81 signatureVerifyResultBroadcaster
->addSignatureVerifyResultListener( this );
83 cssu::Reference
<cssxc::sax::XReferenceResolvedBroadcaster
> xReferenceResolvedBroadcaster
87 xReferenceResolvedBroadcaster
->addReferenceResolvedListener(
88 nIdOfSignatureElementCollector
,
89 xReferenceResolvedListener
);
91 cssu::Reference
<cssxc::sax::XKeyCollector
> keyCollector (xReferenceResolvedListener
, cssu::UNO_QUERY
);
92 keyCollector
->setKeyId(0);
94 return xReferenceResolvedListener
;
97 void XSecController::addSignature()
99 cssu::Reference
< cssxc::sax::XReferenceResolvedListener
> xReferenceResolvedListener
= NULL
;
100 sal_Int32 nSignatureId
= 0;
103 if (m_bVerifyCurrentSignature
)
106 xReferenceResolvedListener
= prepareSignatureToRead( m_nReservedSignatureId
);
107 m_bVerifyCurrentSignature
= false;
108 nSignatureId
= m_nReservedSignatureId
;
111 InternalSignatureInformation
isi( nSignatureId
, xReferenceResolvedListener
);
112 m_vInternalSignatureInformations
.push_back( isi
);
115 void XSecController::addReference( const OUString
& ouUri
)
117 if (m_vInternalSignatureInformations
.empty())
119 SAL_INFO("xmlsecurity.helper","XSecController::addReference: no signature");
122 InternalSignatureInformation
&isi
= m_vInternalSignatureInformations
.back();
123 isi
.addReference(TYPE_SAMEDOCUMENT_REFERENCE
,ouUri
, -1 );
126 void XSecController::addStreamReference(
127 const OUString
& ouUri
,
130 sal_Int32 type
= (isBinary
?TYPE_BINARYSTREAM_REFERENCE
:TYPE_XMLSTREAM_REFERENCE
);
132 if (m_vInternalSignatureInformations
.empty())
134 SAL_INFO("xmlsecurity.helper","XSecController::addStreamReference: no signature");
137 InternalSignatureInformation
&isi
= m_vInternalSignatureInformations
.back();
139 if ( isi
.xReferenceResolvedListener
.is() )
142 * get the input stream
144 cssu::Reference
< com::sun::star::io::XInputStream
> xObjectInputStream
145 = getObjectInputStream( ouUri
);
147 if ( xObjectInputStream
.is() )
149 cssu::Reference
<cssxc::XUriBinding
> xUriBinding
150 (isi
.xReferenceResolvedListener
, cssu::UNO_QUERY
);
151 xUriBinding
->setUriBinding(ouUri
, xObjectInputStream
);
155 isi
.addReference(type
, ouUri
, -1);
158 void XSecController::setReferenceCount() const
160 if (m_vInternalSignatureInformations
.empty())
162 SAL_INFO("xmlsecurity.helper","XSecController::setReferenceCount: no signature");
165 const InternalSignatureInformation
&isi
=
166 m_vInternalSignatureInformations
.back();
168 if ( isi
.xReferenceResolvedListener
.is() )
170 const SignatureReferenceInformations
&refInfors
= isi
.signatureInfor
.vSignatureReferenceInfors
;
172 int refNum
= refInfors
.size();
173 sal_Int32 referenceCount
= 0;
175 for(int i
=0 ; i
<refNum
; ++i
)
177 if (refInfors
[i
].nType
== TYPE_SAMEDOCUMENT_REFERENCE
)
179 * same-document reference
186 cssu::Reference
<cssxc::sax::XReferenceCollector
> xReferenceCollector
187 (isi
.xReferenceResolvedListener
, cssu::UNO_QUERY
);
188 xReferenceCollector
->setReferenceCount( referenceCount
);
192 void XSecController::setX509IssuerName( OUString
& ouX509IssuerName
)
194 if (m_vInternalSignatureInformations
.empty())
196 SAL_INFO("xmlsecurity.helper","XSecController::setX509IssuerName: no signature");
199 InternalSignatureInformation
&isi
= m_vInternalSignatureInformations
.back();
200 isi
.signatureInfor
.ouX509IssuerName
= ouX509IssuerName
;
203 void XSecController::setX509SerialNumber( OUString
& ouX509SerialNumber
)
205 if (m_vInternalSignatureInformations
.empty())
207 SAL_INFO("xmlsecurity.helper","XSecController::setX509SerialNumber: no signature");
210 InternalSignatureInformation
&isi
= m_vInternalSignatureInformations
.back();
211 isi
.signatureInfor
.ouX509SerialNumber
= ouX509SerialNumber
;
214 void XSecController::setX509Certificate( OUString
& ouX509Certificate
)
216 if (m_vInternalSignatureInformations
.empty())
218 SAL_INFO("xmlsecurity.helper","XSecController::setX509Certificate: no signature");
221 InternalSignatureInformation
&isi
= m_vInternalSignatureInformations
.back();
222 isi
.signatureInfor
.ouX509Certificate
= ouX509Certificate
;
225 void XSecController::setSignatureValue( OUString
& ouSignatureValue
)
227 if (m_vInternalSignatureInformations
.empty())
229 SAL_INFO("xmlsecurity.helper","XSecController::setSignatureValue: no signature");
232 InternalSignatureInformation
&isi
= m_vInternalSignatureInformations
.back();
233 isi
.signatureInfor
.ouSignatureValue
= ouSignatureValue
;
236 void XSecController::setDigestValue( OUString
& ouDigestValue
)
238 if (m_vInternalSignatureInformations
.empty())
240 SAL_INFO("xmlsecurity.helper","XSecController::setDigestValue: no signature");
243 InternalSignatureInformation
&isi
= m_vInternalSignatureInformations
.back();
244 if (isi
.signatureInfor
.vSignatureReferenceInfors
.empty())
246 SAL_INFO("xmlsecurity.helper","XSecController::setDigestValue: no signature reference");
249 SignatureReferenceInformation
&reference
=
250 isi
.signatureInfor
.vSignatureReferenceInfors
.back();
251 reference
.ouDigestValue
= ouDigestValue
;
254 void XSecController::setDate( OUString
& ouDate
)
256 if (m_vInternalSignatureInformations
.empty())
258 SAL_INFO("xmlsecurity.helper","XSecController::setDate: no signature");
261 InternalSignatureInformation
&isi
= m_vInternalSignatureInformations
.back();
262 utl::ISO8601parseDateTime( ouDate
, isi
.signatureInfor
.stDateTime
);
263 isi
.signatureInfor
.ouDateTime
= ouDate
;
266 void XSecController::setId( OUString
& ouId
)
268 if (m_vInternalSignatureInformations
.empty())
270 SAL_INFO("xmlsecurity.helper","XSecController::setId: no signature");
273 InternalSignatureInformation
&isi
= m_vInternalSignatureInformations
.back();
274 isi
.signatureInfor
.ouSignatureId
= ouId
;
277 void XSecController::setPropertyId( OUString
& ouPropertyId
)
279 if (m_vInternalSignatureInformations
.empty())
281 SAL_INFO("xmlsecurity.helper","XSecController::setPropertyId: no signature");
284 InternalSignatureInformation
&isi
= m_vInternalSignatureInformations
.back();
285 isi
.signatureInfor
.ouPropertyId
= ouPropertyId
;
288 /* public: for signature verify */
289 void XSecController::collectToVerify( const OUString
& referenceId
)
291 /* DBG_ASSERT( m_xSAXEventKeeper.is(), "the SAXEventKeeper is NULL" ); */
293 if ( m_nStatusOfSecurityComponents
== INITIALIZED
)
295 * if all security components are ready, verify the signature.
298 bool bJustChainingOn
= false;
299 cssu::Reference
< cssxs::XDocumentHandler
> xHandler
= NULL
;
302 int sigNum
= m_vInternalSignatureInformations
.size();
304 for (i
=0; i
<sigNum
; ++i
)
306 InternalSignatureInformation
& isi
= m_vInternalSignatureInformations
[i
];
307 SignatureReferenceInformations
& vReferenceInfors
= isi
.signatureInfor
.vSignatureReferenceInfors
;
308 int refNum
= vReferenceInfors
.size();
310 for (j
=0; j
<refNum
; ++j
)
312 SignatureReferenceInformation
&refInfor
= vReferenceInfors
[j
];
314 if (refInfor
.ouURI
== referenceId
)
318 bJustChainingOn
= true;
319 xHandler
= m_xSAXEventKeeper
->setNextHandler(NULL
);
322 sal_Int32 nKeeperId
= m_xSAXEventKeeper
->addSecurityElementCollector(
323 cssxc::sax::ElementMarkPriority_BEFOREMODIFY
, sal_False
);
325 cssu::Reference
<cssxc::sax::XReferenceResolvedBroadcaster
> xReferenceResolvedBroadcaster
329 cssu::Reference
<cssxc::sax::XReferenceCollector
> xReferenceCollector
330 ( isi
.xReferenceResolvedListener
, cssu::UNO_QUERY
);
332 m_xSAXEventKeeper
->setSecurityId(nKeeperId
, isi
.signatureInfor
.nSecurityId
);
333 xReferenceResolvedBroadcaster
->addReferenceResolvedListener( nKeeperId
, isi
.xReferenceResolvedListener
);
334 xReferenceCollector
->setReferenceId( nKeeperId
);
336 isi
.vKeeperIds
[j
] = nKeeperId
;
342 if ( bJustChainingOn
)
344 cssu::Reference
< cssxs::XDocumentHandler
> xSEKHandler(m_xSAXEventKeeper
, cssu::UNO_QUERY
);
345 if (m_xElementStackKeeper
.is())
347 m_xElementStackKeeper
->retrieve(xSEKHandler
, sal_True
);
349 m_xSAXEventKeeper
->setNextHandler(xHandler
);
354 void XSecController::addSignature( sal_Int32 nSignatureId
)
356 DBG_ASSERT( m_pXSecParser
!= NULL
, "No XSecParser initialized" );
358 m_nReservedSignatureId
= nSignatureId
;
359 m_bVerifyCurrentSignature
= true;
362 cssu::Reference
< cssxs::XDocumentHandler
> XSecController::createSignatureReader()
364 m_pXSecParser
= new XSecParser( this, NULL
);
365 cssu::Reference
< cssl::XInitialization
> xInitialization
= m_pXSecParser
;
367 setSAXChainConnector(xInitialization
, NULL
, NULL
);
369 return m_pXSecParser
;
372 void XSecController::releaseSignatureReader()
374 clearSAXChainConnector( );
375 m_pXSecParser
= NULL
;
378 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */