merged tag ooo/OOO330_m14
[LibreOffice.git] / xmlsecurity / source / helper / xsecverify.cxx
blob82210fdc91b396af5282192d30b6d9bf08a64204
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
31 #include <xsecctl.hxx>
32 #include "xsecparser.hxx"
33 #include <tools/debug.hxx>
35 #include <com/sun/star/xml/crypto/sax/XKeyCollector.hpp>
36 #include <com/sun/star/xml/crypto/sax/ElementMarkPriority.hpp>
37 #include <com/sun/star/xml/crypto/sax/XReferenceResolvedBroadcaster.hpp>
38 #include <com/sun/star/xml/crypto/sax/XReferenceCollector.hpp>
39 #include <com/sun/star/xml/crypto/sax/XSignatureVerifyResultBroadcaster.hpp>
40 #include <com/sun/star/xml/sax/SAXParseException.hpp>
42 namespace cssu = com::sun::star::uno;
43 namespace cssl = com::sun::star::lang;
44 namespace cssxc = com::sun::star::xml::crypto;
45 namespace cssxs = com::sun::star::xml::sax;
47 /* xml security framework components */
48 #define SIGNATUREVERIFIER_COMPONENT "com.sun.star.xml.crypto.sax.SignatureVerifier"
50 /* protected: for signature verify */
51 cssu::Reference< cssxc::sax::XReferenceResolvedListener > XSecController::prepareSignatureToRead(
52 sal_Int32 nSecurityId)
54 if ( m_nStatusOfSecurityComponents != INITIALIZED )
56 return NULL;
59 sal_Int32 nIdOfSignatureElementCollector;
60 cssu::Reference< cssxc::sax::XReferenceResolvedListener > xReferenceResolvedListener;
62 nIdOfSignatureElementCollector =
63 m_xSAXEventKeeper->addSecurityElementCollector( cssxc::sax::ElementMarkPriority_BEFOREMODIFY, sal_False);
65 m_xSAXEventKeeper->setSecurityId(nIdOfSignatureElementCollector, nSecurityId);
68 * create a SignatureVerifier
70 cssu::Reference< cssl::XMultiComponentFactory > xMCF( mxCtx->getServiceManager() );
71 xReferenceResolvedListener = cssu::Reference< cssxc::sax::XReferenceResolvedListener >(
72 xMCF->createInstanceWithContext(
73 rtl::OUString::createFromAscii( SIGNATUREVERIFIER_COMPONENT ), mxCtx),
74 cssu::UNO_QUERY);
76 cssu::Reference<cssl::XInitialization> xInitialization(xReferenceResolvedListener, cssu::UNO_QUERY);
78 cssu::Sequence<cssu::Any> args(5);
79 args[0] = cssu::makeAny(rtl::OUString::valueOf(nSecurityId));
80 args[1] = cssu::makeAny(m_xSAXEventKeeper);
81 args[2] = cssu::makeAny(rtl::OUString::valueOf(nIdOfSignatureElementCollector));
82 args[3] = cssu::makeAny(m_xSecurityContext);
83 args[4] = cssu::makeAny(m_xXMLSignature);
84 xInitialization->initialize(args);
86 cssu::Reference< cssxc::sax::XSignatureVerifyResultBroadcaster >
87 signatureVerifyResultBroadcaster(xReferenceResolvedListener, cssu::UNO_QUERY);
89 signatureVerifyResultBroadcaster->addSignatureVerifyResultListener( this );
91 cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
92 (m_xSAXEventKeeper,
93 cssu::UNO_QUERY);
95 xReferenceResolvedBroadcaster->addReferenceResolvedListener(
96 nIdOfSignatureElementCollector,
97 xReferenceResolvedListener);
99 cssu::Reference<cssxc::sax::XKeyCollector> keyCollector (xReferenceResolvedListener, cssu::UNO_QUERY);
100 keyCollector->setKeyId(0);
102 return xReferenceResolvedListener;
105 void XSecController::addSignature()
107 cssu::Reference< cssxc::sax::XReferenceResolvedListener > xReferenceResolvedListener = NULL;
108 sal_Int32 nSignatureId = 0;
111 if (m_bVerifyCurrentSignature)
113 chainOn(true);
114 xReferenceResolvedListener = prepareSignatureToRead( m_nReservedSignatureId );
115 m_bVerifyCurrentSignature = false;
116 nSignatureId = m_nReservedSignatureId;
119 InternalSignatureInformation isi( nSignatureId, xReferenceResolvedListener );
120 m_vInternalSignatureInformations.push_back( isi );
123 void XSecController::addReference( const rtl::OUString& ouUri)
125 InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
126 isi.addReference(TYPE_SAMEDOCUMENT_REFERENCE,ouUri, -1 );
129 void XSecController::addStreamReference(
130 const rtl::OUString& ouUri,
131 bool isBinary )
133 sal_Int32 type = (isBinary?TYPE_BINARYSTREAM_REFERENCE:TYPE_XMLSTREAM_REFERENCE);
135 InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
137 if ( isi.xReferenceResolvedListener.is() )
140 * get the input stream
142 cssu::Reference< com::sun::star::io::XInputStream > xObjectInputStream
143 = getObjectInputStream( ouUri );
145 if ( xObjectInputStream.is() )
147 cssu::Reference<cssxc::XUriBinding> xUriBinding
148 (isi.xReferenceResolvedListener, cssu::UNO_QUERY);
149 xUriBinding->setUriBinding(ouUri, xObjectInputStream);
153 isi.addReference(type, ouUri, -1);
156 void XSecController::setReferenceCount() const
158 const InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
160 if ( isi.xReferenceResolvedListener.is() )
162 const SignatureReferenceInformations &refInfors = isi.signatureInfor.vSignatureReferenceInfors;
164 int refNum = refInfors.size();
165 sal_Int32 referenceCount = 0;
167 for(int i=0 ; i<refNum; ++i)
169 if (refInfors[i].nType == TYPE_SAMEDOCUMENT_REFERENCE )
171 * same-document reference
174 referenceCount++;
178 cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
179 (isi.xReferenceResolvedListener, cssu::UNO_QUERY);
180 xReferenceCollector->setReferenceCount( referenceCount );
184 void XSecController::setX509IssuerName( rtl::OUString& ouX509IssuerName )
186 InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
187 isi.signatureInfor.ouX509IssuerName = ouX509IssuerName;
190 void XSecController::setX509SerialNumber( rtl::OUString& ouX509SerialNumber )
192 InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
193 isi.signatureInfor.ouX509SerialNumber = ouX509SerialNumber;
196 void XSecController::setX509Certificate( rtl::OUString& ouX509Certificate )
198 InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
199 isi.signatureInfor.ouX509Certificate = ouX509Certificate;
202 void XSecController::setSignatureValue( rtl::OUString& ouSignatureValue )
204 InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
205 isi.signatureInfor.ouSignatureValue = ouSignatureValue;
208 void XSecController::setDigestValue( rtl::OUString& ouDigestValue )
210 SignatureInformation &si = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1].signatureInfor;
211 SignatureReferenceInformation &reference = si.vSignatureReferenceInfors[si.vSignatureReferenceInfors.size()-1];
212 reference.ouDigestValue = ouDigestValue;
215 void XSecController::setDate( rtl::OUString& ouDate )
217 InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
218 convertDateTime( isi.signatureInfor.stDateTime, ouDate );
219 isi.signatureInfor.ouDateTime = ouDate;
223 void XSecController::setTime( rtl::OUString& ouTime )
225 InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
226 isi.signatureInfor.ouTime = ouTime;
230 void XSecController::setId( rtl::OUString& ouId )
232 InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
233 isi.signatureInfor.ouSignatureId = ouId;
236 void XSecController::setPropertyId( rtl::OUString& ouPropertyId )
238 InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
239 isi.signatureInfor.ouPropertyId = ouPropertyId;
242 /* public: for signature verify */
243 void XSecController::collectToVerify( const rtl::OUString& referenceId )
245 /* DBG_ASSERT( m_xSAXEventKeeper.is(), "the SAXEventKeeper is NULL" ); */
247 if ( m_nStatusOfSecurityComponents == INITIALIZED )
249 * if all security components are ready, verify the signature.
252 bool bJustChainingOn = false;
253 cssu::Reference< cssxs::XDocumentHandler > xHandler = NULL;
255 int i,j;
256 int sigNum = m_vInternalSignatureInformations.size();
258 for (i=0; i<sigNum; ++i)
260 InternalSignatureInformation& isi = m_vInternalSignatureInformations[i];
261 SignatureReferenceInformations& vReferenceInfors = isi.signatureInfor.vSignatureReferenceInfors;
262 int refNum = vReferenceInfors.size();
264 for (j=0; j<refNum; ++j)
266 SignatureReferenceInformation &refInfor = vReferenceInfors[j];
268 if (refInfor.ouURI == referenceId)
270 if (chainOn(false))
272 bJustChainingOn = true;
273 xHandler = m_xSAXEventKeeper->setNextHandler(NULL);
276 sal_Int32 nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
277 cssxc::sax::ElementMarkPriority_BEFOREMODIFY, sal_False );
279 cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
280 (m_xSAXEventKeeper,
281 cssu::UNO_QUERY );
283 cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
284 ( isi.xReferenceResolvedListener, cssu::UNO_QUERY );
286 m_xSAXEventKeeper->setSecurityId(nKeeperId, isi.signatureInfor.nSecurityId);
287 xReferenceResolvedBroadcaster->addReferenceResolvedListener( nKeeperId, isi.xReferenceResolvedListener);
288 xReferenceCollector->setReferenceId( nKeeperId );
290 isi.vKeeperIds[j] = nKeeperId;
291 break;
296 if ( bJustChainingOn )
298 cssu::Reference< cssxs::XDocumentHandler > xSEKHandler(m_xSAXEventKeeper, cssu::UNO_QUERY);
299 if (m_xElementStackKeeper.is())
301 m_xElementStackKeeper->retrieve(xSEKHandler, sal_True);
303 m_xSAXEventKeeper->setNextHandler(xHandler);
308 void XSecController::addSignature( sal_Int32 nSignatureId )
310 DBG_ASSERT( m_pXSecParser != NULL, "No XSecParser initialized" );
312 m_nReservedSignatureId = nSignatureId;
313 m_bVerifyCurrentSignature = true;
316 cssu::Reference< cssxs::XDocumentHandler > XSecController::createSignatureReader()
318 m_pXSecParser = new XSecParser( this, NULL );
319 cssu::Reference< cssl::XInitialization > xInitialization = m_pXSecParser;
321 setSAXChainConnector(xInitialization, NULL, NULL);
323 return m_pXSecParser;
326 void XSecController::releaseSignatureReader()
328 clearSAXChainConnector( );
329 m_pXSecParser = NULL;