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 <tools/debug.hxx>
23 :
24 : #include <com/sun/star/xml/crypto/sax/ElementMarkPriority.hpp>
25 : #include <com/sun/star/xml/crypto/sax/XReferenceResolvedBroadcaster.hpp>
26 : #include <com/sun/star/xml/crypto/sax/XMissionTaker.hpp>
27 : #include <com/sun/star/xml/crypto/sax/XReferenceCollector.hpp>
28 : #include <com/sun/star/xml/crypto/sax/XSAXEventKeeperStatusChangeBroadcaster.hpp>
29 : #include <com/sun/star/xml/crypto/SecurityOperationStatus.hpp>
30 :
31 : #include <xmloff/attrlist.hxx>
32 : #include <rtl/math.hxx>
33 : #include <unotools/datetime.hxx>
34 :
35 : namespace cssu = com::sun::star::uno;
36 : namespace cssl = com::sun::star::lang;
37 : namespace cssxc = com::sun::star::xml::crypto;
38 : namespace cssxs = com::sun::star::xml::sax;
39 : namespace cssxw = com::sun::star::xml::wrapper;
40 :
41 : /* bridge component names */
42 : #define XMLSIGNATURE_COMPONENT "com.sun.star.xml.crypto.XMLSignature"
43 : #define XMLDOCUMENTWRAPPER_COMPONENT "com.sun.star.xml.wrapper.XMLDocumentWrapper"
44 :
45 : /* xml security framework components */
46 : #define SAXEVENTKEEPER_COMPONENT "com.sun.star.xml.crypto.sax.SAXEventKeeper"
47 :
48 0 : XSecController::XSecController( const cssu::Reference<cssu::XComponentContext>& rxCtx )
49 : : mxCtx(rxCtx)
50 : , m_nNextSecurityId(1)
51 : , m_bIsPreviousNodeInitializable(false)
52 : , m_bIsSAXEventKeeperConnected(false)
53 : , m_bIsCollectingElement(false)
54 : , m_bIsBlocking(false)
55 : , m_nStatusOfSecurityComponents(UNINITIALIZED)
56 : , m_bIsSAXEventKeeperSticky(false)
57 : , m_pErrorMessage(NULL)
58 : , m_pXSecParser(NULL)
59 : , m_nReservedSignatureId(0)
60 0 : , m_bVerifyCurrentSignature(false)
61 : {
62 0 : }
63 :
64 0 : XSecController::~XSecController()
65 : {
66 0 : }
67 :
68 :
69 : /*
70 : * private methods
71 : */
72 0 : int XSecController::findSignatureInfor( sal_Int32 nSecurityId) const
73 : /****** XSecController/findSignatureInfor *************************************
74 : *
75 : * NAME
76 : * findSignatureInfor -- find SignatureInformation struct for a particular
77 : * signature
78 : *
79 : * SYNOPSIS
80 : * index = findSignatureInfor( nSecurityId );
81 : *
82 : * FUNCTION
83 : * see NAME.
84 : *
85 : * INPUTS
86 : * nSecurityId - the signature's id
87 : *
88 : * RESULT
89 : * index - the index of the signature, or -1 when no such signature
90 : * existing
91 : *
92 : * AUTHOR
93 : * Michael Mi
94 : * Email: michael.mi@sun.com
95 : ******************************************************************************/
96 : {
97 : int i;
98 0 : int size = m_vInternalSignatureInformations.size();
99 :
100 0 : for (i=0; i<size; ++i)
101 : {
102 0 : if (m_vInternalSignatureInformations[i].signatureInfor.nSecurityId == nSecurityId)
103 : {
104 0 : return i;
105 : }
106 : }
107 :
108 0 : return -1;
109 : }
110 :
111 0 : void XSecController::createXSecComponent( )
112 : /****** XSecController/createXSecComponent ************************************
113 : *
114 : * NAME
115 : * bResult = createXSecComponent -- creates xml security components
116 : *
117 : * SYNOPSIS
118 : * createXSecComponent( );
119 : *
120 : * FUNCTION
121 : * Creates xml security components, including:
122 : * 1. an xml signature bridge component ( Java based or C based)
123 : * 2. an XMLDocumentWrapper component ( Java based or C based)
124 : * 3. a SAXEventKeeper component
125 : *
126 : * INPUTS
127 : * empty
128 : *
129 : * RESULT
130 : * empty
131 : *
132 : * AUTHOR
133 : * Michael Mi
134 : * Email: michael.mi@sun.com
135 : ******************************************************************************/
136 : {
137 0 : OUString sSAXEventKeeper( SAXEVENTKEEPER_COMPONENT );
138 0 : OUString sXMLSignature( XMLSIGNATURE_COMPONENT );
139 0 : OUString sXMLDocument( XMLDOCUMENTWRAPPER_COMPONENT );
140 :
141 : /*
142 : * marks all security components are not available.
143 : */
144 0 : m_nStatusOfSecurityComponents = FAILTOINITIALIZED;
145 0 : m_xXMLSignature = NULL;
146 0 : m_xXMLDocumentWrapper = NULL;
147 0 : m_xSAXEventKeeper = NULL;
148 :
149 0 : cssu::Reference< cssl::XMultiComponentFactory > xMCF( mxCtx->getServiceManager() );
150 :
151 0 : m_xXMLSignature = cssu::Reference< cssxc::XXMLSignature >(
152 0 : xMCF->createInstanceWithContext( sXMLSignature, mxCtx ),
153 0 : cssu::UNO_QUERY );
154 :
155 0 : bool bSuccess = m_xXMLSignature.is();
156 0 : if ( bSuccess )
157 : /*
158 : * XMLSignature created successfully.
159 : */
160 : {
161 0 : m_xXMLDocumentWrapper = cssu::Reference< cssxw::XXMLDocumentWrapper >(
162 0 : xMCF->createInstanceWithContext( sXMLDocument, mxCtx ),
163 0 : cssu::UNO_QUERY );
164 : }
165 :
166 0 : bSuccess &= m_xXMLDocumentWrapper.is();
167 0 : if ( bSuccess )
168 : /*
169 : * XMLDocumentWrapper created successfully.
170 : */
171 : {
172 0 : m_xSAXEventKeeper = cssu::Reference< cssxc::sax::XSecuritySAXEventKeeper >(
173 0 : xMCF->createInstanceWithContext( sSAXEventKeeper, mxCtx ),
174 0 : cssu::UNO_QUERY );
175 : }
176 :
177 0 : bSuccess &= m_xSAXEventKeeper.is();
178 :
179 0 : if (bSuccess)
180 : /*
181 : * SAXEventKeeper created successfully.
182 : */
183 : {
184 0 : cssu::Reference< cssl::XInitialization > xInitialization(m_xSAXEventKeeper, cssu::UNO_QUERY);
185 :
186 0 : cssu::Sequence <cssu::Any> arg(1);
187 0 : arg[0] = cssu::makeAny(m_xXMLDocumentWrapper);
188 0 : xInitialization->initialize(arg);
189 :
190 : cssu::Reference<cssxc::sax::XSAXEventKeeperStatusChangeBroadcaster>
191 0 : xSAXEventKeeperStatusChangeBroadcaster(m_xSAXEventKeeper, cssu::UNO_QUERY);
192 : cssu::Reference< cssxc::sax::XSAXEventKeeperStatusChangeListener >
193 0 : xStatusChangeListener = this;
194 :
195 : xSAXEventKeeperStatusChangeBroadcaster
196 0 : ->addSAXEventKeeperStatusChangeListener( xStatusChangeListener );
197 :
198 0 : m_nStatusOfSecurityComponents = INITIALIZED;
199 0 : }
200 0 : }
201 :
202 0 : bool XSecController::chainOn( bool bRetrievingLastEvent )
203 : /****** XSecController/chainOn ************************************************
204 : *
205 : * NAME
206 : * chainOn -- tyies to connect the SAXEventKeeper with the SAX chain.
207 : *
208 : * SYNOPSIS
209 : * bJustChainingOn = chainOn( bRetrievingLastEvent );
210 : *
211 : * FUNCTION
212 : * First, checks whether the SAXEventKeeper is on the SAX chain. If not,
213 : * creates xml security components, and chains the SAXEventKeeper into
214 : * the SAX chain.
215 : * Before being chained in, the SAXEventKeeper needs to receive all
216 : * missed key SAX events, which can promise the DOM tree bufferred by the
217 : * SAXEventKeeper has the same structure with the original document.
218 : *
219 : * INPUTS
220 : * bRetrievingLastEvent - whether to retrieve the last key SAX event from
221 : * the ElementStackKeeper.
222 : *
223 : * RESULT
224 : * bJustChainingOn - whether the SAXEventKeeper is just chained into the
225 : * SAX chain.
226 : *
227 : * NOTES
228 : * Sometimes, the last key SAX event can't be transferred to the
229 : * SAXEventKeeper together.
230 : * For instance, at the time an referenced element is detected, the
231 : * startElement event has already been reserved by the ElementStackKeeper.
232 : * Meanwhile, an ElementCollector needs to be created before the
233 : * SAXEventKeeper receives that startElement event.
234 : * So for the SAXEventKeeper, it needs to receive all missed key SAX
235 : * events except that startElement event, then adds a new
236 : * ElementCollector, then receives that startElement event.
237 : *
238 : * AUTHOR
239 : * Michael Mi
240 : * Email: michael.mi@sun.com
241 : ******************************************************************************/
242 : {
243 0 : bool rc = false;
244 :
245 0 : if (!m_bIsSAXEventKeeperSticky && !m_bIsSAXEventKeeperConnected)
246 : {
247 0 : if ( m_nStatusOfSecurityComponents == UNINITIALIZED )
248 : {
249 0 : createXSecComponent();
250 : }
251 :
252 0 : if ( m_nStatusOfSecurityComponents == INITIALIZED )
253 : /*
254 : * if all security components are ready, chains on the SAXEventKeeper
255 : */
256 : {
257 : /*
258 : * disconnect the SAXEventKeeper with its current output handler,
259 : * to make sure no SAX event is forwarded during the connecting
260 : * phase.
261 : */
262 0 : m_xSAXEventKeeper->setNextHandler( NULL );
263 :
264 0 : cssu::Reference< cssxs::XDocumentHandler > xSEKHandler(m_xSAXEventKeeper, cssu::UNO_QUERY);
265 :
266 : /*
267 : * connects the previous document handler on the SAX chain
268 : */
269 0 : if ( m_xPreviousNodeOnSAXChain.is() )
270 : {
271 0 : if ( m_bIsPreviousNodeInitializable )
272 : {
273 : cssu::Reference< cssl::XInitialization > xInitialization
274 0 : (m_xPreviousNodeOnSAXChain, cssu::UNO_QUERY);
275 :
276 0 : cssu::Sequence<cssu::Any> aArgs( 1 );
277 0 : aArgs[0] <<= xSEKHandler;
278 0 : xInitialization->initialize(aArgs);
279 : }
280 : else
281 : {
282 : cssu::Reference< cssxs::XParser > xParser
283 0 : (m_xPreviousNodeOnSAXChain, cssu::UNO_QUERY);
284 0 : xParser->setDocumentHandler( xSEKHandler );
285 : }
286 : }
287 :
288 : /*
289 : * get missed key SAX events
290 : */
291 0 : if (m_xElementStackKeeper.is())
292 : {
293 0 : m_xElementStackKeeper->retrieve(xSEKHandler, bRetrievingLastEvent);
294 :
295 : /*
296 : * now the ElementStackKeeper can stop its work, because the
297 : * SAXEventKeeper is on the SAX chain, no SAX events will be
298 : * missed.
299 : */
300 0 : m_xElementStackKeeper->stop();
301 : }
302 :
303 : /*
304 : * connects the next document handler on the SAX chain
305 : */
306 0 : m_xSAXEventKeeper->setNextHandler( m_xNextNodeOnSAXChain );
307 :
308 0 : m_bIsSAXEventKeeperConnected = true;
309 :
310 0 : rc = true;
311 : }
312 : }
313 :
314 0 : return rc;
315 : }
316 :
317 0 : void XSecController::chainOff()
318 : /****** XSecController/chainOff ***********************************************
319 : *
320 : * NAME
321 : * chainOff -- disconnects the SAXEventKeeper from the SAX chain.
322 : *
323 : * SYNOPSIS
324 : * chainOff( );
325 : *
326 : * FUNCTION
327 : * See NAME.
328 : *
329 : * INPUTS
330 : * empty
331 : *
332 : * RESULT
333 : * empty
334 : *
335 : * AUTHOR
336 : * Michael Mi
337 : * Email: michael.mi@sun.com
338 : ******************************************************************************/
339 : {
340 0 : if (!m_bIsSAXEventKeeperSticky )
341 : {
342 0 : if (m_bIsSAXEventKeeperConnected)
343 : {
344 0 : m_xSAXEventKeeper->setNextHandler( NULL );
345 :
346 0 : if ( m_xPreviousNodeOnSAXChain.is() )
347 : {
348 0 : if ( m_bIsPreviousNodeInitializable )
349 : {
350 : cssu::Reference< cssl::XInitialization > xInitialization
351 0 : (m_xPreviousNodeOnSAXChain, cssu::UNO_QUERY);
352 :
353 0 : cssu::Sequence<cssu::Any> aArgs( 1 );
354 0 : aArgs[0] <<= m_xNextNodeOnSAXChain;
355 0 : xInitialization->initialize(aArgs);
356 : }
357 : else
358 : {
359 0 : cssu::Reference< cssxs::XParser > xParser(m_xPreviousNodeOnSAXChain, cssu::UNO_QUERY);
360 0 : xParser->setDocumentHandler( m_xNextNodeOnSAXChain );
361 : }
362 : }
363 :
364 0 : if (m_xElementStackKeeper.is())
365 : {
366 : /*
367 : * start the ElementStackKeeper to reserve any possible
368 : * missed key SAX events
369 : */
370 0 : m_xElementStackKeeper->start();
371 : }
372 :
373 0 : m_bIsSAXEventKeeperConnected = false;
374 : }
375 : }
376 0 : }
377 :
378 0 : void XSecController::checkChainingStatus()
379 : /****** XSecController/checkChainingStatus ************************************
380 : *
381 : * NAME
382 : * checkChainingStatus -- connects or disconnects the SAXEventKeeper
383 : * according to the current situation.
384 : *
385 : * SYNOPSIS
386 : * checkChainingStatus( );
387 : *
388 : * FUNCTION
389 : * The SAXEventKeeper is chained into the SAX chain, when:
390 : * 1. some element is being collected, or
391 : * 2. the SAX event stream is blocking.
392 : * Otherwise, chain off the SAXEventKeeper.
393 : *
394 : * INPUTS
395 : * empty
396 : *
397 : * RESULT
398 : * empty
399 : *
400 : * AUTHOR
401 : * Michael Mi
402 : * Email: michael.mi@sun.com
403 : ******************************************************************************/
404 : {
405 0 : if ( m_bIsCollectingElement || m_bIsBlocking )
406 : {
407 0 : chainOn(true);
408 : }
409 : else
410 : {
411 0 : chainOff();
412 : }
413 0 : }
414 :
415 0 : void XSecController::initializeSAXChain()
416 : /****** XSecController/initializeSAXChain *************************************
417 : *
418 : * NAME
419 : * initializeSAXChain -- initializes the SAX chain according to the
420 : * current setting.
421 : *
422 : * SYNOPSIS
423 : * initializeSAXChain( );
424 : *
425 : * FUNCTION
426 : * Initializes the SAX chain, if the SAXEventKeeper is asked to be always
427 : * on the SAX chain, chains it on. Otherwise, starts the
428 : * ElementStackKeeper to reserve key SAX events.
429 : *
430 : * INPUTS
431 : * empty
432 : *
433 : * RESULT
434 : * empty
435 : *
436 : * AUTHOR
437 : * Michael Mi
438 : * Email: michael.mi@sun.com
439 : ******************************************************************************/
440 : {
441 0 : m_bIsSAXEventKeeperConnected = false;
442 0 : m_bIsCollectingElement = false;
443 0 : m_bIsBlocking = false;
444 :
445 0 : if (m_xElementStackKeeper.is())
446 : {
447 : /*
448 : * starts the ElementStackKeeper
449 : */
450 0 : m_xElementStackKeeper->start();
451 : }
452 :
453 0 : chainOff();
454 0 : }
455 :
456 : cssu::Reference< com::sun::star::io::XInputStream >
457 0 : XSecController::getObjectInputStream( const OUString& objectURL )
458 : /****** XSecController/getObjectInputStream ************************************
459 : *
460 : * NAME
461 : * getObjectInputStream -- get a XInputStream interface from a SotStorage
462 : *
463 : * SYNOPSIS
464 : * xInputStream = getObjectInputStream( objectURL );
465 : *
466 : * FUNCTION
467 : * See NAME.
468 : *
469 : * INPUTS
470 : * objectURL - the object uri
471 : *
472 : * RESULT
473 : * xInputStream - the XInputStream interface
474 : *
475 : * AUTHOR
476 : * Michael Mi
477 : * Email: michael.mi@sun.com
478 : ******************************************************************************/
479 : {
480 0 : cssu::Reference< com::sun::star::io::XInputStream > xObjectInputStream;
481 :
482 : DBG_ASSERT( m_xUriBinding.is(), "Need XUriBinding!" );
483 :
484 0 : xObjectInputStream = m_xUriBinding->getUriBinding(objectURL);
485 :
486 0 : return xObjectInputStream;
487 : }
488 :
489 : /*
490 : * public methods
491 : */
492 :
493 0 : sal_Int32 XSecController::getNewSecurityId( )
494 : {
495 0 : sal_Int32 nId = m_nNextSecurityId;
496 0 : m_nNextSecurityId++;
497 0 : return nId;
498 : }
499 :
500 0 : void XSecController::startMission(
501 : const cssu::Reference< cssxc::XUriBinding >& xUriBinding,
502 : const cssu::Reference< cssxc::XXMLSecurityContext >& xSecurityContext )
503 : /****** XSecController/startMission *******************************************
504 : *
505 : * NAME
506 : * startMission -- starts a new security mission.
507 : *
508 : * SYNOPSIS
509 : * startMission( xUriBinding, xSecurityContect );
510 : *
511 : * FUNCTION
512 : * get ready for a new mission.
513 : *
514 : * INPUTS
515 : * xUriBinding - the Uri binding that provide maps between uris and
516 : * XInputStreams
517 : * xSecurityContext - the security context component which can provide
518 : * cryptoken
519 : *
520 : * RESULT
521 : * empty
522 : *
523 : * AUTHOR
524 : * Michael Mi
525 : * Email: michael.mi@sun.com
526 : ******************************************************************************/
527 : {
528 0 : m_xUriBinding = xUriBinding;
529 :
530 0 : m_nStatusOfSecurityComponents = UNINITIALIZED;
531 0 : m_xSecurityContext = xSecurityContext;
532 0 : m_pErrorMessage = NULL;
533 :
534 0 : m_vInternalSignatureInformations.clear();
535 :
536 0 : m_bVerifyCurrentSignature = false;
537 0 : }
538 :
539 0 : void XSecController::setSAXChainConnector(
540 : const cssu::Reference< cssl::XInitialization >& xInitialization,
541 : const cssu::Reference< cssxs::XDocumentHandler >& xDocumentHandler,
542 : const cssu::Reference< cssxc::sax::XElementStackKeeper >& xElementStackKeeper)
543 : /****** XSecController/setSAXChainConnector ***********************************
544 : *
545 : * NAME
546 : * setSAXChainConnector -- configures the components which will
547 : * collaborate with the SAXEventKeeper on the SAX chain.
548 : *
549 : * SYNOPSIS
550 : * setSAXChainConnector( xInitialization,
551 : * xDocumentHandler,
552 : * xElementStackKeeper );
553 : *
554 : * FUNCTION
555 : * See NAME.
556 : *
557 : * INPUTS
558 : * xInitialization - the previous node on the SAX chain
559 : * xDocumentHandler - the next node on the SAX chain
560 : * xElementStackKeeper - the ElementStackKeeper component which reserves
561 : * missed key SAX events for the SAXEventKeeper
562 : *
563 : * RESULT
564 : * empty
565 : *
566 : * AUTHOR
567 : * Michael Mi
568 : * Email: michael.mi@sun.com
569 : ******************************************************************************/
570 : {
571 0 : m_bIsPreviousNodeInitializable = true;
572 0 : m_xPreviousNodeOnSAXChain = xInitialization;
573 0 : m_xNextNodeOnSAXChain = xDocumentHandler;
574 0 : m_xElementStackKeeper = xElementStackKeeper;
575 :
576 0 : initializeSAXChain( );
577 0 : }
578 :
579 0 : void XSecController::clearSAXChainConnector()
580 : /****** XSecController/clearSAXChainConnector *********************************
581 : *
582 : * NAME
583 : * clearSAXChainConnector -- resets the collaborating components.
584 : *
585 : * SYNOPSIS
586 : * clearSAXChainConnector( );
587 : *
588 : * FUNCTION
589 : * See NAME.
590 : *
591 : * INPUTS
592 : * empty
593 : *
594 : * RESULT
595 : * empty
596 : *
597 : * AUTHOR
598 : * Michael Mi
599 : * Email: michael.mi@sun.com
600 : ******************************************************************************/
601 : {
602 : /*
603 : * before reseting, if the ElementStackKeeper has kept something, then
604 : * those kept key SAX events must be transferred to the SAXEventKeeper
605 : * first. This is to promise the next node to the SAXEventKeeper on the
606 : * SAX chain always receives a complete document.
607 : */
608 0 : if (m_xElementStackKeeper.is() && m_xSAXEventKeeper.is())
609 : {
610 0 : cssu::Reference< cssxs::XDocumentHandler > xSEKHandler(m_xSAXEventKeeper, cssu::UNO_QUERY);
611 0 : m_xElementStackKeeper->retrieve(xSEKHandler, sal_True);
612 : }
613 :
614 0 : chainOff();
615 :
616 0 : m_xPreviousNodeOnSAXChain = NULL;
617 0 : m_xNextNodeOnSAXChain = NULL;
618 0 : m_xElementStackKeeper = NULL;
619 0 : }
620 :
621 0 : void XSecController::endMission()
622 : /****** XSecController/endMission *********************************************
623 : *
624 : * NAME
625 : * endMission -- forces to end all missions
626 : *
627 : * SYNOPSIS
628 : * endMission( );
629 : *
630 : * FUNCTION
631 : * Deletes all signature information and forces all missions to an end.
632 : *
633 : * INPUTS
634 : * empty
635 : *
636 : * RESULT
637 : * empty
638 : *
639 : * AUTHOR
640 : * Michael Mi
641 : * Email: michael.mi@sun.com
642 : ******************************************************************************/
643 : {
644 0 : sal_Int32 size = m_vInternalSignatureInformations.size();
645 :
646 0 : for (int i=0; i<size; ++i)
647 : {
648 0 : if ( m_nStatusOfSecurityComponents == INITIALIZED )
649 : /*
650 : * ResolvedListener only exist when the security components are created.
651 : */
652 : {
653 : cssu::Reference< cssxc::sax::XMissionTaker > xMissionTaker
654 0 : ( m_vInternalSignatureInformations[i].xReferenceResolvedListener, cssu::UNO_QUERY );
655 :
656 : /*
657 : * askes the SignatureCreator/SignatureVerifier to release
658 : * all resources it uses.
659 : */
660 0 : xMissionTaker->endMission();
661 : }
662 : }
663 :
664 0 : m_xUriBinding = NULL;
665 0 : m_xSecurityContext = NULL;
666 :
667 : /*
668 : * free the status change listener reference to this object
669 : */
670 0 : if (m_xSAXEventKeeper.is())
671 : {
672 : cssu::Reference<cssxc::sax::XSAXEventKeeperStatusChangeBroadcaster>
673 0 : xSAXEventKeeperStatusChangeBroadcaster(m_xSAXEventKeeper, cssu::UNO_QUERY);
674 : xSAXEventKeeperStatusChangeBroadcaster
675 0 : ->addSAXEventKeeperStatusChangeListener( NULL );
676 : }
677 0 : }
678 :
679 0 : void XSecController::exportSignature(
680 : const cssu::Reference<cssxs::XDocumentHandler>& xDocumentHandler,
681 : const SignatureInformation& signatureInfo )
682 : /****** XSecController/exportSignature ****************************************
683 : *
684 : * NAME
685 : * exportSignature -- export a signature structure to an XDocumentHandler
686 : *
687 : * SYNOPSIS
688 : * exportSignature( xDocumentHandler, signatureInfo);
689 : *
690 : * FUNCTION
691 : * see NAME.
692 : *
693 : * INPUTS
694 : * xDocumentHandler - the document handler to receive the signature
695 : * signatureInfo - signature to be exported
696 : *
697 : * RESULT
698 : * empty
699 : *
700 : * AUTHOR
701 : * Michael Mi
702 : * Email: michael.mi@sun.com
703 : ******************************************************************************/
704 : {
705 : /*
706 : * defines all element tags in Signature element.
707 : */
708 0 : OUString tag_Signature(TAG_SIGNATURE);
709 0 : OUString tag_SignedInfo(TAG_SIGNEDINFO);
710 0 : OUString tag_CanonicalizationMethod(TAG_CANONICALIZATIONMETHOD);
711 0 : OUString tag_SignatureMethod(TAG_SIGNATUREMETHOD);
712 0 : OUString tag_Reference(TAG_REFERENCE);
713 0 : OUString tag_Transforms(TAG_TRANSFORMS);
714 0 : OUString tag_Transform(TAG_TRANSFORM);
715 0 : OUString tag_DigestMethod(TAG_DIGESTMETHOD);
716 0 : OUString tag_DigestValue(TAG_DIGESTVALUE);
717 0 : OUString tag_SignatureValue(TAG_SIGNATUREVALUE);
718 0 : OUString tag_KeyInfo(TAG_KEYINFO);
719 0 : OUString tag_X509Data(TAG_X509DATA);
720 0 : OUString tag_X509IssuerSerial(TAG_X509ISSUERSERIAL);
721 0 : OUString tag_X509IssuerName(TAG_X509ISSUERNAME);
722 0 : OUString tag_X509SerialNumber(TAG_X509SERIALNUMBER);
723 0 : OUString tag_X509Certificate(TAG_X509CERTIFICATE);
724 0 : OUString tag_Object(TAG_OBJECT);
725 0 : OUString tag_SignatureProperties(TAG_SIGNATUREPROPERTIES);
726 0 : OUString tag_SignatureProperty(TAG_SIGNATUREPROPERTY);
727 0 : OUString tag_Date(TAG_DATE);
728 :
729 0 : const SignatureReferenceInformations& vReferenceInfors = signatureInfo.vSignatureReferenceInfors;
730 : SvXMLAttributeList *pAttributeList;
731 :
732 : /*
733 : * Write Signature element
734 : */
735 0 : pAttributeList = new SvXMLAttributeList();
736 : pAttributeList->AddAttribute(
737 : OUString(ATTR_XMLNS),
738 0 : OUString(NS_XMLDSIG));
739 :
740 0 : if (!signatureInfo.ouSignatureId.isEmpty())
741 : {
742 : pAttributeList->AddAttribute(
743 : OUString(ATTR_ID),
744 0 : OUString(signatureInfo.ouSignatureId));
745 : }
746 :
747 0 : xDocumentHandler->startElement( tag_Signature, cssu::Reference< cssxs::XAttributeList > (pAttributeList));
748 : {
749 : /* Write SignedInfo element */
750 0 : xDocumentHandler->startElement(
751 : tag_SignedInfo,
752 0 : cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
753 : {
754 : /* Write CanonicalizationMethod element */
755 0 : pAttributeList = new SvXMLAttributeList();
756 : pAttributeList->AddAttribute(
757 : OUString(ATTR_ALGORITHM),
758 0 : OUString(ALGO_C14N));
759 0 : xDocumentHandler->startElement( tag_CanonicalizationMethod, cssu::Reference< cssxs::XAttributeList > (pAttributeList) );
760 0 : xDocumentHandler->endElement( tag_CanonicalizationMethod );
761 :
762 : /* Write SignatureMethod element */
763 0 : pAttributeList = new SvXMLAttributeList();
764 : pAttributeList->AddAttribute(
765 : OUString(ATTR_ALGORITHM),
766 0 : OUString(ALGO_RSASHA1));
767 0 : xDocumentHandler->startElement( tag_SignatureMethod, cssu::Reference< cssxs::XAttributeList > (pAttributeList) );
768 0 : xDocumentHandler->endElement( tag_SignatureMethod );
769 :
770 : /* Write Reference element */
771 : int j;
772 0 : int refNum = vReferenceInfors.size();
773 :
774 0 : for(j=0; j<refNum; ++j)
775 : {
776 0 : const SignatureReferenceInformation& refInfor = vReferenceInfors[j];
777 :
778 0 : pAttributeList = new SvXMLAttributeList();
779 0 : if ( refInfor.nType != TYPE_SAMEDOCUMENT_REFERENCE )
780 : /*
781 : * stream reference
782 : */
783 : {
784 : pAttributeList->AddAttribute(
785 : OUString(ATTR_URI),
786 0 : refInfor.ouURI);
787 : }
788 : else
789 : /*
790 : * same-document reference
791 : */
792 : {
793 : pAttributeList->AddAttribute(
794 : OUString(ATTR_URI),
795 0 : CHAR_FRAGMENT+refInfor.ouURI);
796 : }
797 :
798 0 : xDocumentHandler->startElement( tag_Reference, cssu::Reference< cssxs::XAttributeList > (pAttributeList) );
799 : {
800 : /* Write Transforms element */
801 0 : if (refInfor.nType == TYPE_XMLSTREAM_REFERENCE)
802 : /*
803 : * xml stream, so c14n transform is needed
804 : */
805 : {
806 0 : xDocumentHandler->startElement(
807 : tag_Transforms,
808 0 : cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
809 : {
810 0 : pAttributeList = new SvXMLAttributeList();
811 : pAttributeList->AddAttribute(
812 : OUString(ATTR_ALGORITHM),
813 0 : OUString(ALGO_C14N));
814 0 : xDocumentHandler->startElement(
815 : tag_Transform,
816 0 : cssu::Reference< cssxs::XAttributeList > (pAttributeList) );
817 0 : xDocumentHandler->endElement( tag_Transform );
818 : }
819 0 : xDocumentHandler->endElement( tag_Transforms );
820 : }
821 :
822 : /* Write DigestMethod element */
823 0 : pAttributeList = new SvXMLAttributeList();
824 : pAttributeList->AddAttribute(
825 : OUString(ATTR_ALGORITHM),
826 0 : OUString(ALGO_XMLDSIGSHA1));
827 0 : xDocumentHandler->startElement(
828 : tag_DigestMethod,
829 0 : cssu::Reference< cssxs::XAttributeList > (pAttributeList) );
830 0 : xDocumentHandler->endElement( tag_DigestMethod );
831 :
832 : /* Write DigestValue element */
833 0 : xDocumentHandler->startElement(
834 : tag_DigestValue,
835 0 : cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
836 0 : xDocumentHandler->characters( refInfor.ouDigestValue );
837 0 : xDocumentHandler->endElement( tag_DigestValue );
838 : }
839 0 : xDocumentHandler->endElement( tag_Reference );
840 : }
841 : }
842 0 : xDocumentHandler->endElement( tag_SignedInfo );
843 :
844 : /* Write SignatureValue element */
845 0 : xDocumentHandler->startElement(
846 : tag_SignatureValue,
847 0 : cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
848 0 : xDocumentHandler->characters( signatureInfo.ouSignatureValue );
849 0 : xDocumentHandler->endElement( tag_SignatureValue );
850 :
851 : /* Write KeyInfo element */
852 0 : xDocumentHandler->startElement(
853 : tag_KeyInfo,
854 0 : cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
855 : {
856 : /* Write X509Data element */
857 0 : xDocumentHandler->startElement(
858 : tag_X509Data,
859 0 : cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
860 : {
861 : /* Write X509IssuerSerial element */
862 0 : xDocumentHandler->startElement(
863 : tag_X509IssuerSerial,
864 0 : cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
865 : {
866 : /* Write X509IssuerName element */
867 0 : xDocumentHandler->startElement(
868 : tag_X509IssuerName,
869 0 : cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
870 0 : xDocumentHandler->characters( signatureInfo.ouX509IssuerName );
871 0 : xDocumentHandler->endElement( tag_X509IssuerName );
872 :
873 : /* Write X509SerialNumber element */
874 0 : xDocumentHandler->startElement(
875 : tag_X509SerialNumber,
876 0 : cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
877 0 : xDocumentHandler->characters( signatureInfo.ouX509SerialNumber );
878 0 : xDocumentHandler->endElement( tag_X509SerialNumber );
879 : }
880 0 : xDocumentHandler->endElement( tag_X509IssuerSerial );
881 :
882 : /* Write X509Certificate element */
883 0 : if (!signatureInfo.ouX509Certificate.isEmpty())
884 : {
885 0 : xDocumentHandler->startElement(
886 : tag_X509Certificate,
887 0 : cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
888 0 : xDocumentHandler->characters( signatureInfo.ouX509Certificate );
889 0 : xDocumentHandler->endElement( tag_X509Certificate );
890 : }
891 : }
892 0 : xDocumentHandler->endElement( tag_X509Data );
893 : }
894 0 : xDocumentHandler->endElement( tag_KeyInfo );
895 :
896 : /* Write Object element */
897 0 : xDocumentHandler->startElement(
898 : tag_Object,
899 0 : cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
900 : {
901 : /* Write SignatureProperties element */
902 0 : xDocumentHandler->startElement(
903 : tag_SignatureProperties,
904 0 : cssu::Reference< cssxs::XAttributeList > (new SvXMLAttributeList()));
905 : {
906 : /* Write SignatureProperty element */
907 0 : pAttributeList = new SvXMLAttributeList();
908 : pAttributeList->AddAttribute(
909 : OUString(ATTR_ID),
910 0 : signatureInfo.ouPropertyId);
911 : pAttributeList->AddAttribute(
912 : OUString(ATTR_TARGET),
913 0 : CHAR_FRAGMENT+signatureInfo.ouSignatureId);
914 0 : xDocumentHandler->startElement(
915 : tag_SignatureProperty,
916 0 : cssu::Reference< cssxs::XAttributeList > (pAttributeList));
917 : {
918 : /* Write timestamp element */
919 :
920 0 : pAttributeList = new SvXMLAttributeList();
921 : pAttributeList->AddAttribute(
922 : ATTR_XMLNS ":" NSTAG_DC,
923 0 : OUString(NS_DC));
924 :
925 0 : xDocumentHandler->startElement(
926 0 : NSTAG_DC ":" + tag_Date,
927 0 : cssu::Reference< cssxs::XAttributeList > (pAttributeList));
928 :
929 0 : OUStringBuffer buffer;
930 : //If the xml signature was already contained in the document,
931 : //then we use the original date and time string, rather then the
932 : //converted one. This avoids writing a different string due to
933 : //e.g. rounding issues and thus breaking the signature.
934 0 : if (!signatureInfo.ouDateTime.isEmpty())
935 0 : buffer = signatureInfo.ouDateTime;
936 : else
937 : {
938 0 : buffer = utl::toISO8601(signatureInfo.stDateTime);
939 : }
940 0 : xDocumentHandler->characters( buffer.makeStringAndClear() );
941 :
942 0 : xDocumentHandler->endElement(
943 0 : NSTAG_DC ":" + tag_Date);
944 : }
945 0 : xDocumentHandler->endElement( tag_SignatureProperty );
946 : }
947 0 : xDocumentHandler->endElement( tag_SignatureProperties );
948 : }
949 0 : xDocumentHandler->endElement( tag_Object );
950 : }
951 0 : xDocumentHandler->endElement( tag_Signature );
952 0 : }
953 :
954 0 : SignatureInformation XSecController::getSignatureInformation( sal_Int32 nSecurityId ) const
955 : {
956 0 : SignatureInformation aInf( 0 );
957 0 : int nIndex = findSignatureInfor(nSecurityId);
958 : DBG_ASSERT( nIndex != -1, "getSignatureInformation - SecurityId is invalid!" );
959 0 : if ( nIndex != -1)
960 : {
961 0 : aInf = m_vInternalSignatureInformations[nIndex].signatureInfor;
962 : }
963 0 : return aInf;
964 : }
965 :
966 0 : SignatureInformations XSecController::getSignatureInformations() const
967 : {
968 0 : SignatureInformations vInfors;
969 0 : int sigNum = m_vInternalSignatureInformations.size();
970 :
971 0 : for (int i=0; i<sigNum; ++i)
972 : {
973 0 : SignatureInformation si = m_vInternalSignatureInformations[i].signatureInfor;
974 0 : vInfors.push_back(si);
975 0 : }
976 :
977 0 : return vInfors;
978 : }
979 :
980 : /*
981 : * XSecurityController
982 : *
983 : * no methods
984 : */
985 :
986 : /*
987 : * XFastPropertySet
988 : */
989 :
990 : /*
991 : * XSAXEventKeeperStatusChangeListener
992 : */
993 :
994 0 : void SAL_CALL XSecController::blockingStatusChanged( sal_Bool isBlocking )
995 : throw (cssu::RuntimeException, std::exception)
996 : {
997 0 : this->m_bIsBlocking = isBlocking;
998 0 : checkChainingStatus();
999 0 : }
1000 :
1001 0 : void SAL_CALL XSecController::collectionStatusChanged(
1002 : sal_Bool isInsideCollectedElement )
1003 : throw (cssu::RuntimeException, std::exception)
1004 : {
1005 0 : this->m_bIsCollectingElement = isInsideCollectedElement;
1006 0 : checkChainingStatus();
1007 0 : }
1008 :
1009 0 : void SAL_CALL XSecController::bufferStatusChanged( sal_Bool /*isBufferEmpty*/)
1010 : throw (cssu::RuntimeException, std::exception)
1011 : {
1012 :
1013 0 : }
1014 :
1015 : /*
1016 : * XSignatureCreationResultListener
1017 : */
1018 0 : void SAL_CALL XSecController::signatureCreated( sal_Int32 securityId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
1019 : throw (com::sun::star::uno::RuntimeException, std::exception)
1020 : {
1021 0 : int index = findSignatureInfor(securityId);
1022 : assert(index != -1 && "Signature Not Found!");
1023 0 : SignatureInformation& signatureInfor = m_vInternalSignatureInformations.at(index).signatureInfor;
1024 0 : signatureInfor.nStatus = nResult;
1025 0 : }
1026 :
1027 : /*
1028 : * XSignatureVerifyResultListener
1029 : */
1030 0 : void SAL_CALL XSecController::signatureVerified( sal_Int32 securityId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
1031 : throw (com::sun::star::uno::RuntimeException, std::exception)
1032 : {
1033 0 : int index = findSignatureInfor(securityId);
1034 : assert(index != -1 && "Signature Not Found!");
1035 0 : SignatureInformation& signatureInfor = m_vInternalSignatureInformations.at(index).signatureInfor;
1036 0 : signatureInfor.nStatus = nResult;
1037 0 : }
1038 :
1039 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|