Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "xsecctl.hxx"
22 : #include "xsecparser.hxx"
23 : #include <tools/debug.hxx>
24 :
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>
33 :
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;
38 :
39 : /* xml security framework components */
40 : #define SIGNATUREVERIFIER_COMPONENT "com.sun.star.xml.crypto.sax.SignatureVerifier"
41 :
42 : /* protected: for signature verify */
43 0 : cssu::Reference< cssxc::sax::XReferenceResolvedListener > XSecController::prepareSignatureToRead(
44 : sal_Int32 nSecurityId)
45 : {
46 0 : if ( m_nStatusOfSecurityComponents != INITIALIZED )
47 : {
48 0 : return NULL;
49 : }
50 :
51 : sal_Int32 nIdOfSignatureElementCollector;
52 0 : cssu::Reference< cssxc::sax::XReferenceResolvedListener > xReferenceResolvedListener;
53 :
54 : nIdOfSignatureElementCollector =
55 0 : m_xSAXEventKeeper->addSecurityElementCollector( cssxc::sax::ElementMarkPriority_BEFOREMODIFY, sal_False);
56 :
57 0 : m_xSAXEventKeeper->setSecurityId(nIdOfSignatureElementCollector, nSecurityId);
58 :
59 : /*
60 : * create a SignatureVerifier
61 : */
62 0 : cssu::Reference< cssl::XMultiComponentFactory > xMCF( mxCtx->getServiceManager() );
63 0 : xReferenceResolvedListener = cssu::Reference< cssxc::sax::XReferenceResolvedListener >(
64 0 : xMCF->createInstanceWithContext(
65 0 : OUString( SIGNATUREVERIFIER_COMPONENT ), mxCtx),
66 0 : cssu::UNO_QUERY);
67 :
68 0 : cssu::Reference<cssl::XInitialization> xInitialization(xReferenceResolvedListener, cssu::UNO_QUERY);
69 :
70 0 : cssu::Sequence<cssu::Any> args(5);
71 0 : args[0] = cssu::makeAny(OUString::number(nSecurityId));
72 0 : args[1] = cssu::makeAny(m_xSAXEventKeeper);
73 0 : args[2] = cssu::makeAny(OUString::number(nIdOfSignatureElementCollector));
74 0 : args[3] = cssu::makeAny(m_xSecurityContext);
75 0 : args[4] = cssu::makeAny(m_xXMLSignature);
76 0 : xInitialization->initialize(args);
77 :
78 : cssu::Reference< cssxc::sax::XSignatureVerifyResultBroadcaster >
79 0 : signatureVerifyResultBroadcaster(xReferenceResolvedListener, cssu::UNO_QUERY);
80 :
81 0 : signatureVerifyResultBroadcaster->addSignatureVerifyResultListener( this );
82 :
83 : cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
84 : (m_xSAXEventKeeper,
85 0 : cssu::UNO_QUERY);
86 :
87 0 : xReferenceResolvedBroadcaster->addReferenceResolvedListener(
88 : nIdOfSignatureElementCollector,
89 0 : xReferenceResolvedListener);
90 :
91 0 : cssu::Reference<cssxc::sax::XKeyCollector> keyCollector (xReferenceResolvedListener, cssu::UNO_QUERY);
92 0 : keyCollector->setKeyId(0);
93 :
94 0 : return xReferenceResolvedListener;
95 : }
96 :
97 0 : void XSecController::addSignature()
98 : {
99 0 : cssu::Reference< cssxc::sax::XReferenceResolvedListener > xReferenceResolvedListener = NULL;
100 0 : sal_Int32 nSignatureId = 0;
101 :
102 :
103 0 : if (m_bVerifyCurrentSignature)
104 : {
105 0 : chainOn(true);
106 0 : xReferenceResolvedListener = prepareSignatureToRead( m_nReservedSignatureId );
107 0 : m_bVerifyCurrentSignature = false;
108 0 : nSignatureId = m_nReservedSignatureId;
109 : }
110 :
111 0 : InternalSignatureInformation isi( nSignatureId, xReferenceResolvedListener );
112 0 : m_vInternalSignatureInformations.push_back( isi );
113 0 : }
114 :
115 0 : void XSecController::addReference( const OUString& ouUri)
116 : {
117 0 : if (m_vInternalSignatureInformations.empty())
118 : {
119 : SAL_INFO("xmlsecurity.helper","XSecController::addReference: no signature");
120 0 : return;
121 : }
122 0 : InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
123 0 : isi.addReference(TYPE_SAMEDOCUMENT_REFERENCE,ouUri, -1 );
124 : }
125 :
126 0 : void XSecController::addStreamReference(
127 : const OUString& ouUri,
128 : bool isBinary )
129 : {
130 0 : sal_Int32 type = (isBinary?TYPE_BINARYSTREAM_REFERENCE:TYPE_XMLSTREAM_REFERENCE);
131 :
132 0 : if (m_vInternalSignatureInformations.empty())
133 : {
134 : SAL_INFO("xmlsecurity.helper","XSecController::addStreamReference: no signature");
135 0 : return;
136 : }
137 0 : InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
138 :
139 0 : if ( isi.xReferenceResolvedListener.is() )
140 : {
141 : /*
142 : * get the input stream
143 : */
144 : cssu::Reference< com::sun::star::io::XInputStream > xObjectInputStream
145 0 : = getObjectInputStream( ouUri );
146 :
147 0 : if ( xObjectInputStream.is() )
148 : {
149 : cssu::Reference<cssxc::XUriBinding> xUriBinding
150 0 : (isi.xReferenceResolvedListener, cssu::UNO_QUERY);
151 0 : xUriBinding->setUriBinding(ouUri, xObjectInputStream);
152 0 : }
153 : }
154 :
155 0 : isi.addReference(type, ouUri, -1);
156 : }
157 :
158 0 : void XSecController::setReferenceCount() const
159 : {
160 0 : if (m_vInternalSignatureInformations.empty())
161 : {
162 : SAL_INFO("xmlsecurity.helper","XSecController::setReferenceCount: no signature");
163 0 : return;
164 : }
165 : const InternalSignatureInformation &isi =
166 0 : m_vInternalSignatureInformations.back();
167 :
168 0 : if ( isi.xReferenceResolvedListener.is() )
169 : {
170 0 : const SignatureReferenceInformations &refInfors = isi.signatureInfor.vSignatureReferenceInfors;
171 :
172 0 : int refNum = refInfors.size();
173 0 : sal_Int32 referenceCount = 0;
174 :
175 0 : for(int i=0 ; i<refNum; ++i)
176 : {
177 0 : if (refInfors[i].nType == TYPE_SAMEDOCUMENT_REFERENCE )
178 : /*
179 : * same-document reference
180 : */
181 : {
182 0 : referenceCount++;
183 : }
184 : }
185 :
186 : cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
187 0 : (isi.xReferenceResolvedListener, cssu::UNO_QUERY);
188 0 : xReferenceCollector->setReferenceCount( referenceCount );
189 : }
190 : }
191 :
192 0 : void XSecController::setX509IssuerName( OUString& ouX509IssuerName )
193 : {
194 0 : if (m_vInternalSignatureInformations.empty())
195 : {
196 : SAL_INFO("xmlsecurity.helper","XSecController::setX509IssuerName: no signature");
197 0 : return;
198 : }
199 0 : InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
200 0 : isi.signatureInfor.ouX509IssuerName = ouX509IssuerName;
201 : }
202 :
203 0 : void XSecController::setX509SerialNumber( OUString& ouX509SerialNumber )
204 : {
205 0 : if (m_vInternalSignatureInformations.empty())
206 : {
207 : SAL_INFO("xmlsecurity.helper","XSecController::setX509SerialNumber: no signature");
208 0 : return;
209 : }
210 0 : InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
211 0 : isi.signatureInfor.ouX509SerialNumber = ouX509SerialNumber;
212 : }
213 :
214 0 : void XSecController::setX509Certificate( OUString& ouX509Certificate )
215 : {
216 0 : if (m_vInternalSignatureInformations.empty())
217 : {
218 : SAL_INFO("xmlsecurity.helper","XSecController::setX509Certificate: no signature");
219 0 : return;
220 : }
221 0 : InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
222 0 : isi.signatureInfor.ouX509Certificate = ouX509Certificate;
223 : }
224 :
225 0 : void XSecController::setSignatureValue( OUString& ouSignatureValue )
226 : {
227 0 : if (m_vInternalSignatureInformations.empty())
228 : {
229 : SAL_INFO("xmlsecurity.helper","XSecController::setSignatureValue: no signature");
230 0 : return;
231 : }
232 0 : InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
233 0 : isi.signatureInfor.ouSignatureValue = ouSignatureValue;
234 : }
235 :
236 0 : void XSecController::setDigestValue( OUString& ouDigestValue )
237 : {
238 0 : if (m_vInternalSignatureInformations.empty())
239 : {
240 : SAL_INFO("xmlsecurity.helper","XSecController::setDigestValue: no signature");
241 0 : return;
242 : }
243 0 : InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
244 0 : if (isi.signatureInfor.vSignatureReferenceInfors.empty())
245 : {
246 : SAL_INFO("xmlsecurity.helper","XSecController::setDigestValue: no signature reference");
247 0 : return;
248 : }
249 : SignatureReferenceInformation &reference =
250 0 : isi.signatureInfor.vSignatureReferenceInfors.back();
251 0 : reference.ouDigestValue = ouDigestValue;
252 : }
253 :
254 0 : void XSecController::setDate( OUString& ouDate )
255 : {
256 0 : if (m_vInternalSignatureInformations.empty())
257 : {
258 : SAL_INFO("xmlsecurity.helper","XSecController::setDate: no signature");
259 0 : return;
260 : }
261 0 : InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
262 0 : utl::ISO8601parseDateTime( ouDate, isi.signatureInfor.stDateTime);
263 0 : isi.signatureInfor.ouDateTime = ouDate;
264 : }
265 :
266 0 : void XSecController::setId( OUString& ouId )
267 : {
268 0 : if (m_vInternalSignatureInformations.empty())
269 : {
270 : SAL_INFO("xmlsecurity.helper","XSecController::setId: no signature");
271 0 : return;
272 : }
273 0 : InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
274 0 : isi.signatureInfor.ouSignatureId = ouId;
275 : }
276 :
277 0 : void XSecController::setPropertyId( OUString& ouPropertyId )
278 : {
279 0 : if (m_vInternalSignatureInformations.empty())
280 : {
281 : SAL_INFO("xmlsecurity.helper","XSecController::setPropertyId: no signature");
282 0 : return;
283 : }
284 0 : InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
285 0 : isi.signatureInfor.ouPropertyId = ouPropertyId;
286 : }
287 :
288 : /* public: for signature verify */
289 0 : void XSecController::collectToVerify( const OUString& referenceId )
290 : {
291 : /* DBG_ASSERT( m_xSAXEventKeeper.is(), "the SAXEventKeeper is NULL" ); */
292 :
293 0 : if ( m_nStatusOfSecurityComponents == INITIALIZED )
294 : /*
295 : * if all security components are ready, verify the signature.
296 : */
297 : {
298 0 : bool bJustChainingOn = false;
299 0 : cssu::Reference< cssxs::XDocumentHandler > xHandler = NULL;
300 :
301 : int i,j;
302 0 : int sigNum = m_vInternalSignatureInformations.size();
303 :
304 0 : for (i=0; i<sigNum; ++i)
305 : {
306 0 : InternalSignatureInformation& isi = m_vInternalSignatureInformations[i];
307 0 : SignatureReferenceInformations& vReferenceInfors = isi.signatureInfor.vSignatureReferenceInfors;
308 0 : int refNum = vReferenceInfors.size();
309 :
310 0 : for (j=0; j<refNum; ++j)
311 : {
312 0 : SignatureReferenceInformation &refInfor = vReferenceInfors[j];
313 :
314 0 : if (refInfor.ouURI == referenceId)
315 : {
316 0 : if (chainOn(false))
317 : {
318 0 : bJustChainingOn = true;
319 0 : xHandler = m_xSAXEventKeeper->setNextHandler(NULL);
320 : }
321 :
322 0 : sal_Int32 nKeeperId = m_xSAXEventKeeper->addSecurityElementCollector(
323 0 : cssxc::sax::ElementMarkPriority_BEFOREMODIFY, sal_False );
324 :
325 : cssu::Reference<cssxc::sax::XReferenceResolvedBroadcaster> xReferenceResolvedBroadcaster
326 : (m_xSAXEventKeeper,
327 0 : cssu::UNO_QUERY );
328 :
329 : cssu::Reference<cssxc::sax::XReferenceCollector> xReferenceCollector
330 0 : ( isi.xReferenceResolvedListener, cssu::UNO_QUERY );
331 :
332 0 : m_xSAXEventKeeper->setSecurityId(nKeeperId, isi.signatureInfor.nSecurityId);
333 0 : xReferenceResolvedBroadcaster->addReferenceResolvedListener( nKeeperId, isi.xReferenceResolvedListener);
334 0 : xReferenceCollector->setReferenceId( nKeeperId );
335 :
336 0 : isi.vKeeperIds[j] = nKeeperId;
337 0 : break;
338 : }
339 : }
340 : }
341 :
342 0 : if ( bJustChainingOn )
343 : {
344 0 : cssu::Reference< cssxs::XDocumentHandler > xSEKHandler(m_xSAXEventKeeper, cssu::UNO_QUERY);
345 0 : if (m_xElementStackKeeper.is())
346 : {
347 0 : m_xElementStackKeeper->retrieve(xSEKHandler, sal_True);
348 : }
349 0 : m_xSAXEventKeeper->setNextHandler(xHandler);
350 0 : }
351 : }
352 0 : }
353 :
354 0 : void XSecController::addSignature( sal_Int32 nSignatureId )
355 : {
356 : DBG_ASSERT( m_pXSecParser != NULL, "No XSecParser initialized" );
357 :
358 0 : m_nReservedSignatureId = nSignatureId;
359 0 : m_bVerifyCurrentSignature = true;
360 0 : }
361 :
362 0 : cssu::Reference< cssxs::XDocumentHandler > XSecController::createSignatureReader()
363 : {
364 0 : m_pXSecParser = new XSecParser( this, NULL );
365 0 : cssu::Reference< cssl::XInitialization > xInitialization = m_pXSecParser;
366 :
367 0 : setSAXChainConnector(xInitialization, NULL, NULL);
368 :
369 0 : return m_pXSecParser;
370 : }
371 :
372 0 : void XSecController::releaseSignatureReader()
373 : {
374 0 : clearSAXChainConnector( );
375 0 : m_pXSecParser = NULL;
376 0 : }
377 :
378 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|