Update ooo320-m1
[ooovba.git] / xmlsecurity / source / helper / xsecverify.cxx
blobd093743b129bb5bb1baa753620ed8248300509e0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xsecverify.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
34 #include <xsecctl.hxx>
35 #include "xsecparser.hxx"
36 #include <tools/debug.hxx>
38 #include <com/sun/star/xml/crypto/sax/XKeyCollector.hpp>
39 #include <com/sun/star/xml/crypto/sax/ElementMarkPriority.hpp>
40 #include <com/sun/star/xml/crypto/sax/XReferenceResolvedBroadcaster.hpp>
41 #include <com/sun/star/xml/crypto/sax/XReferenceCollector.hpp>
42 #include <com/sun/star/xml/crypto/sax/XSignatureVerifyResultBroadcaster.hpp>
43 #include <com/sun/star/xml/sax/SAXParseException.hpp>
45 namespace cssu = com::sun::star::uno;
46 namespace cssl = com::sun::star::lang;
47 namespace cssxc = com::sun::star::xml::crypto;
48 namespace cssxs = com::sun::star::xml::sax;
50 /* xml security framework components */
51 #define SIGNATUREVERIFIER_COMPONENT "com.sun.star.xml.crypto.sax.SignatureVerifier"
53 /* protected: for signature verify */
54 cssu::Reference< cssxc::sax::XReferenceResolvedListener > XSecController::prepareSignatureToRead(
55 sal_Int32 nSecurityId)
57 if ( m_nStatusOfSecurityComponents != INITIALIZED )
59 return NULL;
62 sal_Int32 nIdOfSignatureElementCollector;
63 cssu::Reference< cssxc::sax::XReferenceResolvedListener > xReferenceResolvedListener;
65 nIdOfSignatureElementCollector =
66 m_xSAXEventKeeper->addSecurityElementCollector( cssxc::sax::ElementMarkPriority_BEFOREMODIFY, sal_False);
68 m_xSAXEventKeeper->setSecurityId(nIdOfSignatureElementCollector, nSecurityId);
71 * create a SignatureVerifier
73 cssu::Reference< cssl::XMultiComponentFactory > xMCF( mxCtx->getServiceManager() );
74 xReferenceResolvedListener = cssu::Reference< cssxc::sax::XReferenceResolvedListener >(
75 xMCF->createInstanceWithContext(
76 rtl::OUString::createFromAscii( SIGNATUREVERIFIER_COMPONENT ), mxCtx),
77 cssu::UNO_QUERY);
79 cssu::Reference<cssl::XInitialization> xInitialization(xReferenceResolvedListener, cssu::UNO_QUERY);
81 cssu::Sequence<cssu::Any> args(5);
82 args[0] = cssu::makeAny(rtl::OUString::valueOf(nSecurityId));
83 args[1] = cssu::makeAny(m_xSAXEventKeeper);
84 args[2] = cssu::makeAny(rtl::OUString::valueOf(nIdOfSignatureElementCollector));
85 args[3] = cssu::makeAny(m_xSecurityContext);
86 args[4] = cssu::makeAny(m_xXMLSignature);
87 xInitialization->initialize(args);
89 cssu::Reference< cssxc::sax::XSignatureVerifyResultBroadcaster >
90 signatureVerifyResultBroadcaster(xReferenceResolvedListener, cssu::UNO_QUERY);
92 signatureVerifyResultBroadcaster->addSignatureVerifyResultListener( this );
94 cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
95 (m_xSAXEventKeeper,
96 cssu::UNO_QUERY);
98 xReferenceResolvedBroadcaster->addReferenceResolvedListener(
99 nIdOfSignatureElementCollector,
100 xReferenceResolvedListener);
102 cssu::Reference<cssxc::sax::XKeyCollector> keyCollector (xReferenceResolvedListener, cssu::UNO_QUERY);
103 keyCollector->setKeyId(0);
105 return xReferenceResolvedListener;
108 void XSecController::addSignature()
110 cssu::Reference< cssxc::sax::XReferenceResolvedListener > xReferenceResolvedListener = NULL;
111 sal_Int32 nSignatureId = 0;
114 if (m_bVerifyCurrentSignature)
116 chainOn(true);
117 xReferenceResolvedListener = prepareSignatureToRead( m_nReservedSignatureId );
118 m_bVerifyCurrentSignature = false;
119 nSignatureId = m_nReservedSignatureId;
122 InternalSignatureInformation isi( nSignatureId, xReferenceResolvedListener );
123 m_vInternalSignatureInformations.push_back( isi );
126 void XSecController::addReference( const rtl::OUString& ouUri)
128 InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
129 isi.addReference(TYPE_SAMEDOCUMENT_REFERENCE,ouUri, -1 );
132 void XSecController::addStreamReference(
133 const rtl::OUString& ouUri,
134 bool isBinary )
136 sal_Int32 type = (isBinary?TYPE_BINARYSTREAM_REFERENCE:TYPE_XMLSTREAM_REFERENCE);
138 InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
140 if ( isi.xReferenceResolvedListener.is() )
143 * get the input stream
145 cssu::Reference< com::sun::star::io::XInputStream > xObjectInputStream
146 = getObjectInputStream( ouUri );
148 if ( xObjectInputStream.is() )
150 cssu::Reference<cssxc::XUriBinding> xUriBinding
151 (isi.xReferenceResolvedListener, cssu::UNO_QUERY);
152 xUriBinding->setUriBinding(ouUri, xObjectInputStream);
156 isi.addReference(type, ouUri, -1);
159 void XSecController::setReferenceCount() const
161 const InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
163 if ( isi.xReferenceResolvedListener.is() )
165 const SignatureReferenceInformations &refInfors = isi.signatureInfor.vSignatureReferenceInfors;
167 int refNum = refInfors.size();
168 sal_Int32 referenceCount = 0;
170 for(int i=0 ; i<refNum; ++i)
172 if (refInfors[i].nType == TYPE_SAMEDOCUMENT_REFERENCE )
174 * same-document reference
177 referenceCount++;
181 cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
182 (isi.xReferenceResolvedListener, cssu::UNO_QUERY);
183 xReferenceCollector->setReferenceCount( referenceCount );
187 void XSecController::setX509IssuerName( rtl::OUString& ouX509IssuerName )
189 InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
190 isi.signatureInfor.ouX509IssuerName = ouX509IssuerName;
193 void XSecController::setX509SerialNumber( rtl::OUString& ouX509SerialNumber )
195 InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
196 isi.signatureInfor.ouX509SerialNumber = ouX509SerialNumber;
199 void XSecController::setX509Certificate( rtl::OUString& ouX509Certificate )
201 InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
202 isi.signatureInfor.ouX509Certificate = ouX509Certificate;
205 void XSecController::setSignatureValue( rtl::OUString& ouSignatureValue )
207 InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
208 isi.signatureInfor.ouSignatureValue = ouSignatureValue;
211 void XSecController::setDigestValue( rtl::OUString& ouDigestValue )
213 SignatureInformation &si = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1].signatureInfor;
214 SignatureReferenceInformation &reference = si.vSignatureReferenceInfors[si.vSignatureReferenceInfors.size()-1];
215 reference.ouDigestValue = ouDigestValue;
218 void XSecController::setDate( rtl::OUString& ouDate )
220 InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
221 convertDateTime( isi.signatureInfor.stDateTime, ouDate );
222 isi.signatureInfor.ouDateTime = ouDate;
226 void XSecController::setTime( rtl::OUString& ouTime )
228 InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
229 isi.signatureInfor.ouTime = ouTime;
233 void XSecController::setId( rtl::OUString& ouId )
235 InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
236 isi.signatureInfor.ouSignatureId = ouId;
239 void XSecController::setPropertyId( rtl::OUString& ouPropertyId )
241 InternalSignatureInformation &isi = m_vInternalSignatureInformations[m_vInternalSignatureInformations.size()-1];
242 isi.signatureInfor.ouPropertyId = ouPropertyId;
245 /* public: for signature verify */
246 void XSecController::collectToVerify( const rtl::OUString& referenceId )
248 /* DBG_ASSERT( m_xSAXEventKeeper.is(), "the SAXEventKeeper is NULL" ); */
250 if ( m_nStatusOfSecurityComponents == INITIALIZED )
252 * if all security components are ready, verify the signature.
255 bool bJustChainingOn = false;
256 cssu::Reference< cssxs::XDocumentHandler > xHandler = NULL;
258 int i,j;
259 int sigNum = m_vInternalSignatureInformations.size();
261 for (i=0; i<sigNum; ++i)
263 InternalSignatureInformation& isi = m_vInternalSignatureInformations[i];
264 SignatureReferenceInformations& vReferenceInfors = isi.signatureInfor.vSignatureReferenceInfors;
265 int refNum = vReferenceInfors.size();
267 for (j=0; j<refNum; ++j)
269 SignatureReferenceInformation &refInfor = vReferenceInfors[j];
271 if (refInfor.ouURI == referenceId)
273 if (chainOn(false))
275 bJustChainingOn = true;
276 xHandler = m_xSAXEventKeeper->setNextHandler(NULL);
279 sal_Int32 nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
280 cssxc::sax::ElementMarkPriority_BEFOREMODIFY, sal_False );
282 cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
283 (m_xSAXEventKeeper,
284 cssu::UNO_QUERY );
286 cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
287 ( isi.xReferenceResolvedListener, cssu::UNO_QUERY );
289 m_xSAXEventKeeper->setSecurityId(nKeeperId, isi.signatureInfor.nSecurityId);
290 xReferenceResolvedBroadcaster->addReferenceResolvedListener( nKeeperId, isi.xReferenceResolvedListener);
291 xReferenceCollector->setReferenceId( nKeeperId );
293 isi.vKeeperIds[j] = nKeeperId;
294 break;
299 if ( bJustChainingOn )
301 cssu::Reference< cssxs::XDocumentHandler > xSEKHandler(m_xSAXEventKeeper, cssu::UNO_QUERY);
302 if (m_xElementStackKeeper.is())
304 m_xElementStackKeeper->retrieve(xSEKHandler, sal_True);
306 m_xSAXEventKeeper->setNextHandler(xHandler);
311 void XSecController::addSignature( sal_Int32 nSignatureId )
313 DBG_ASSERT( m_pXSecParser != NULL, "No XSecParser initialized" );
315 m_nReservedSignatureId = nSignatureId;
316 m_bVerifyCurrentSignature = true;
319 cssu::Reference< cssxs::XDocumentHandler > XSecController::createSignatureReader()
321 m_pXSecParser = new XSecParser( this, NULL );
322 cssu::Reference< cssl::XInitialization > xInitialization = m_pXSecParser;
324 setSAXChainConnector(xInitialization, NULL, NULL);
326 return m_pXSecParser;
329 void XSecController::releaseSignatureReader()
331 clearSAXChainConnector( );
332 m_pXSecParser = NULL;