xref: /trunk/main/xmlsecurity/tools/uno/XMLSecurityFrameworkController.java (revision db8598795e2025b89968fa208f4cf0f9125a6f19)
1*db859879SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*db859879SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*db859879SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*db859879SAndrew Rist  * distributed with this work for additional information
6*db859879SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*db859879SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*db859879SAndrew Rist  * "License"); you may not use this file except in compliance
9*db859879SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*db859879SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*db859879SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*db859879SAndrew Rist  * software distributed under the License is distributed on an
15*db859879SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*db859879SAndrew Rist  * KIND, either express or implied.  See the License for the
17*db859879SAndrew Rist  * specific language governing permissions and limitations
18*db859879SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*db859879SAndrew Rist  *************************************************************/
21*db859879SAndrew Rist 
22*db859879SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package com.sun.star.xml.security.uno;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.util.Stack;
27cdf0e10cSrcweir import java.util.Vector;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir /* uno classes */
30cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
31cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
32cdf0e10cSrcweir import com.sun.star.lang.XInitialization;
33cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
34cdf0e10cSrcweir import com.sun.star.xml.sax.XDocumentHandler;
35cdf0e10cSrcweir import com.sun.star.xml.sax.XAttributeList;
36cdf0e10cSrcweir import com.sun.star.xml.sax.SAXException;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir import com.sun.star.xml.crypto.*;
39cdf0e10cSrcweir import com.sun.star.xml.crypto.sax.*;
40cdf0e10cSrcweir import com.sun.star.xml.wrapper.*;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir /*
43cdf0e10cSrcweir  * the XMLSecurityFrameworkController class is used to controll the xml security framework.
44cdf0e10cSrcweir  */
45cdf0e10cSrcweir public class XMLSecurityFrameworkController
46cdf0e10cSrcweir     implements XDocumentHandler, XSignatureCreationResultListener, XSignatureVerifyResultListener,
47cdf0e10cSrcweir            XEncryptionResultListener, XDecryptionResultListener, XSAXEventKeeperStatusChangeListener
48cdf0e10cSrcweir {
49cdf0e10cSrcweir     /*
50cdf0e10cSrcweir      * UNO framework component
51cdf0e10cSrcweir      */
52cdf0e10cSrcweir     private XMultiComponentFactory  m_xRemoteServiceManager;
53cdf0e10cSrcweir     private XComponentContext       m_xRemoteContext;
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     /*
56cdf0e10cSrcweir      * xml security related UNO components
57cdf0e10cSrcweir      */
58cdf0e10cSrcweir     private XSecuritySAXEventKeeper m_xSAXEventKeeper;
59cdf0e10cSrcweir     private XXMLDocumentWrapper     m_xXMLDocumentWrapper;
60cdf0e10cSrcweir     private XDocumentHandler        m_xOutputHandler;
61cdf0e10cSrcweir     private XXMLSecurityContext     m_xXMLSecurityContext;
62cdf0e10cSrcweir     private XXMLSignature           m_xXMLSignature;
63cdf0e10cSrcweir     private XXMLEncryption          m_xXMLEncryption;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir         /*
66cdf0e10cSrcweir          * used to reserve the current SAX ancestor path
67cdf0e10cSrcweir          */
68cdf0e10cSrcweir     private Stack  m_currentPath;
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     /*
71cdf0e10cSrcweir      * maintains all SignatureEntities.
72cdf0e10cSrcweir      */
73cdf0e10cSrcweir     private Vector m_signatureList;
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     /*
76cdf0e10cSrcweir      * maintains all EncryptionEntities.
77cdf0e10cSrcweir      */
78cdf0e10cSrcweir     private Vector m_encryptionList;
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     /*
81cdf0e10cSrcweir      * maintains all unsolved reference Ids.
82cdf0e10cSrcweir      * These ids are strings which is the value of the id attribute
83cdf0e10cSrcweir      * of the referenced element.
84cdf0e10cSrcweir      */
85cdf0e10cSrcweir     private Vector m_vUnsolvedReferenceIds;
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     /*
88cdf0e10cSrcweir      * maintains all unsolved reference keeper ids.
89cdf0e10cSrcweir      * The keeper id is used to uniquely identify a bufferred element
90cdf0e10cSrcweir      * by the SAXEventKeeper.
91cdf0e10cSrcweir      */
92cdf0e10cSrcweir     private Vector m_vUnsolvedReferencedKeeperIds;
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     /*
95cdf0e10cSrcweir      * maintains the left time that each unsolved reference can be
96cdf0e10cSrcweir      * claimed.
97cdf0e10cSrcweir      */
98cdf0e10cSrcweir     private Vector m_vUnsolvedReferenceRefNum;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir     /*
101cdf0e10cSrcweir      * whether exporting or importing
102cdf0e10cSrcweir      */
103cdf0e10cSrcweir     private boolean m_bIsExporting;
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     /*
106cdf0e10cSrcweir      * whether java or c
107cdf0e10cSrcweir      */
108cdf0e10cSrcweir     private boolean m_bIsJavaBased;
109cdf0e10cSrcweir 
110cdf0e10cSrcweir     /*
111cdf0e10cSrcweir      * whether the SAXEventKeeper is blocking
112cdf0e10cSrcweir      */
113cdf0e10cSrcweir     private boolean m_bIsBlocking;
114cdf0e10cSrcweir 
115cdf0e10cSrcweir     /*
116cdf0e10cSrcweir      * whether it is collecting a bufferred element
117cdf0e10cSrcweir      */
118cdf0e10cSrcweir     private boolean m_bIsInsideCollectedElement;
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     /*
121cdf0e10cSrcweir      * whether a SAXEventKeeper is in the SAX chain
122cdf0e10cSrcweir      */
123cdf0e10cSrcweir     private boolean m_bSAXEventKeeperIncluded;
124cdf0e10cSrcweir 
125cdf0e10cSrcweir     /*
126cdf0e10cSrcweir      * the ParsingThread used to parse the document
127cdf0e10cSrcweir      */
128cdf0e10cSrcweir     private ParsingThread m_parsingThread;
129cdf0e10cSrcweir 
130cdf0e10cSrcweir     /*
131cdf0e10cSrcweir      * the next document handler that will receives SAX events
132cdf0e10cSrcweir      * from the parsing thread.
133cdf0e10cSrcweir      * if the SAXEventKeeper is on the SAX chain, then this
134cdf0e10cSrcweir      * variable will be the SAXEventKeeper, otherwise, this
135cdf0e10cSrcweir      * variable will be the xOutputHandler.
136cdf0e10cSrcweir      */
137cdf0e10cSrcweir     private XDocumentHandler m_xExportHandler;
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     /*
140cdf0e10cSrcweir      * the TestTool used to feedback information
141cdf0e10cSrcweir      */
142cdf0e10cSrcweir     private TestTool m_testTool;
143cdf0e10cSrcweir 
144cdf0e10cSrcweir     /*
145cdf0e10cSrcweir      * for encryption target
146cdf0e10cSrcweir      */
147cdf0e10cSrcweir     private boolean m_bIsEncryptionTarget;
148cdf0e10cSrcweir     private EncryptionEntity m_EncryptionForTarget;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir     XMLSecurityFrameworkController(
151cdf0e10cSrcweir         TestTool testTool,
152cdf0e10cSrcweir         boolean bIsExporting,
153cdf0e10cSrcweir         boolean bIsJavaBased,
154cdf0e10cSrcweir         XDocumentHandler xOutputHandler,
155cdf0e10cSrcweir         ParsingThread parsingThread,
156cdf0e10cSrcweir         XXMLSecurityContext xXMLSecurityContext,
157cdf0e10cSrcweir         XXMLSignature xXMLSignature,
158cdf0e10cSrcweir         XXMLEncryption xXMLEncryption,
159cdf0e10cSrcweir         XMultiComponentFactory xRemoteServiceManager,
160cdf0e10cSrcweir         XComponentContext xRemoteContext)
161cdf0e10cSrcweir     {
162cdf0e10cSrcweir         m_bIsExporting = bIsExporting;
163cdf0e10cSrcweir         m_bIsJavaBased = bIsJavaBased;
164cdf0e10cSrcweir 
165cdf0e10cSrcweir         m_xOutputHandler = xOutputHandler;
166cdf0e10cSrcweir         m_xXMLSecurityContext = xXMLSecurityContext;
167cdf0e10cSrcweir         m_xXMLSignature = xXMLSignature;
168cdf0e10cSrcweir         m_xXMLEncryption = xXMLEncryption;
169cdf0e10cSrcweir         m_xRemoteServiceManager = xRemoteServiceManager;
170cdf0e10cSrcweir         m_xRemoteContext = xRemoteContext;
171cdf0e10cSrcweir 
172cdf0e10cSrcweir         m_testTool = testTool;
173cdf0e10cSrcweir         m_parsingThread = parsingThread;
174cdf0e10cSrcweir 
175cdf0e10cSrcweir         m_signatureList = new Vector();
176cdf0e10cSrcweir         m_encryptionList = new Vector();
177cdf0e10cSrcweir 
178cdf0e10cSrcweir         m_vUnsolvedReferenceIds = new Vector();
179cdf0e10cSrcweir         m_vUnsolvedReferencedKeeperIds = new Vector();
180cdf0e10cSrcweir         m_vUnsolvedReferenceRefNum = new Vector();
181cdf0e10cSrcweir 
182cdf0e10cSrcweir         m_xXMLDocumentWrapper = null;
183cdf0e10cSrcweir         m_xSAXEventKeeper = null;
184cdf0e10cSrcweir 
185cdf0e10cSrcweir         m_bSAXEventKeeperIncluded = false;
186cdf0e10cSrcweir         m_bIsBlocking = false;
187cdf0e10cSrcweir         m_bIsInsideCollectedElement = false;
188cdf0e10cSrcweir 
189cdf0e10cSrcweir         m_bIsEncryptionTarget = false;
190cdf0e10cSrcweir         m_EncryptionForTarget = null;
191cdf0e10cSrcweir 
192cdf0e10cSrcweir         changeOutput();
193cdf0e10cSrcweir 
194cdf0e10cSrcweir         m_currentPath = new Stack();
195cdf0e10cSrcweir 
196cdf0e10cSrcweir         foundSecurityRelated();
197cdf0e10cSrcweir     }
198cdf0e10cSrcweir 
199cdf0e10cSrcweir /**************************************************************************************
200cdf0e10cSrcweir  * private methods
201cdf0e10cSrcweir  **************************************************************************************/
202cdf0e10cSrcweir 
203cdf0e10cSrcweir         /*
204cdf0e10cSrcweir          * changes the output document handler.
205cdf0e10cSrcweir          */
206cdf0e10cSrcweir         private void changeOutput()
207cdf0e10cSrcweir         {
208cdf0e10cSrcweir         if (m_bIsExporting)
209cdf0e10cSrcweir         {
210cdf0e10cSrcweir             m_parsingThread.setHandler(this);
211cdf0e10cSrcweir 
212cdf0e10cSrcweir             /*
213cdf0e10cSrcweir              * If the SAXEventKeeper is in the SAX chain, then redirects output
214cdf0e10cSrcweir              * to the SAXEventKeeper, otherwise, to the m_xOutputHandler
215cdf0e10cSrcweir              */
216cdf0e10cSrcweir             if (m_bSAXEventKeeperIncluded)
217cdf0e10cSrcweir             {
218cdf0e10cSrcweir                 m_xExportHandler = (XDocumentHandler)UnoRuntime.queryInterface(
219cdf0e10cSrcweir                             XDocumentHandler.class, m_xSAXEventKeeper);
220cdf0e10cSrcweir                 m_xSAXEventKeeper.setNextHandler(m_xOutputHandler);
221cdf0e10cSrcweir 
222cdf0e10cSrcweir                 m_testTool.updatesSAXChainInformation("XMLExporter -> SAXEventKeeper -> SAXWriter");
223cdf0e10cSrcweir             }
224cdf0e10cSrcweir             else
225cdf0e10cSrcweir             {
226cdf0e10cSrcweir                 m_xExportHandler = m_xOutputHandler;
227cdf0e10cSrcweir                 m_testTool.updatesSAXChainInformation("XMLExporter -> SAXWriter");
228cdf0e10cSrcweir             }
229cdf0e10cSrcweir         }
230cdf0e10cSrcweir         else
231cdf0e10cSrcweir         {
232cdf0e10cSrcweir             if (m_bSAXEventKeeperIncluded)
233cdf0e10cSrcweir             {
234cdf0e10cSrcweir                 m_parsingThread.setHandler(
235cdf0e10cSrcweir                     (XDocumentHandler)UnoRuntime.queryInterface(XDocumentHandler.class, m_xSAXEventKeeper));
236cdf0e10cSrcweir                 m_xSAXEventKeeper.setNextHandler(this);
237cdf0e10cSrcweir                 m_testTool.updatesSAXChainInformation("SAXParser -> SAXEventKeeper -> XMLImporter");
238cdf0e10cSrcweir             }
239cdf0e10cSrcweir             else
240cdf0e10cSrcweir             {
241cdf0e10cSrcweir                 m_parsingThread.setHandler(this);
242cdf0e10cSrcweir                 m_testTool.updatesSAXChainInformation("SAXParser -> XMLImporter");
243cdf0e10cSrcweir             }
244cdf0e10cSrcweir             m_xExportHandler = m_xOutputHandler;
245cdf0e10cSrcweir         }
246cdf0e10cSrcweir     }
247cdf0e10cSrcweir 
248cdf0e10cSrcweir         /*
249cdf0e10cSrcweir          * handles the situation when a security related element is found.
250cdf0e10cSrcweir          * if the SAXEventKeeper is not initialized, then creates a
251cdf0e10cSrcweir          * SAXEventKeeper.
252cdf0e10cSrcweir          * the return value represents whether the SAXEventKeeper is newly
253cdf0e10cSrcweir          * created.
254cdf0e10cSrcweir          */
255cdf0e10cSrcweir     private boolean foundSecurityRelated()
256cdf0e10cSrcweir     {
257cdf0e10cSrcweir         if (m_xSAXEventKeeper == null)
258cdf0e10cSrcweir         {
259cdf0e10cSrcweir             m_testTool.showMessage("Message from : "+
260cdf0e10cSrcweir                         (m_bIsExporting?"XMLExporter":"XMLImporter")+
261cdf0e10cSrcweir                         "\n\nA security related content found, a SAXEventKeeper is created.\n ");
262cdf0e10cSrcweir 
263cdf0e10cSrcweir             m_bIsBlocking = false;
264cdf0e10cSrcweir             m_bIsInsideCollectedElement = false;
265cdf0e10cSrcweir 
266cdf0e10cSrcweir             try
267cdf0e10cSrcweir             {
268cdf0e10cSrcweir                 /*
269cdf0e10cSrcweir                  * creates an XMLDocumentWrapper component.
270cdf0e10cSrcweir                  */
271cdf0e10cSrcweir                 Object xmlDocumentObj = null;
272cdf0e10cSrcweir 
273cdf0e10cSrcweir                 if (m_bIsJavaBased)
274cdf0e10cSrcweir                 {
275cdf0e10cSrcweir                     xmlDocumentObj = m_xRemoteServiceManager.createInstanceWithContext(
276cdf0e10cSrcweir                         TestTool.XMLDOCUMENTWRAPPER_COMPONENT_JAVA, m_xRemoteContext);
277cdf0e10cSrcweir                 }
278cdf0e10cSrcweir                 else
279cdf0e10cSrcweir                 {
280cdf0e10cSrcweir                     xmlDocumentObj = m_xRemoteServiceManager.createInstanceWithContext(
281cdf0e10cSrcweir                         TestTool.XMLDOCUMENTWRAPPER_COMPONENT_C, m_xRemoteContext);
282cdf0e10cSrcweir                 }
283cdf0e10cSrcweir 
284cdf0e10cSrcweir                 m_xXMLDocumentWrapper = (XXMLDocumentWrapper)UnoRuntime.queryInterface(
285cdf0e10cSrcweir                     XXMLDocumentWrapper.class, xmlDocumentObj);
286cdf0e10cSrcweir 
287cdf0e10cSrcweir                 /*
288cdf0e10cSrcweir                  * creates a SAXEventKeeper component.
289cdf0e10cSrcweir                  */
290cdf0e10cSrcweir                 Object saxEventKeeperObj = m_xRemoteServiceManager.createInstanceWithContext(
291cdf0e10cSrcweir                     TestTool.SAXEVENTKEEPER_COMPONENT, m_xRemoteContext);
292cdf0e10cSrcweir 
293cdf0e10cSrcweir                 m_xSAXEventKeeper =
294cdf0e10cSrcweir                     (XSecuritySAXEventKeeper)UnoRuntime.queryInterface(
295cdf0e10cSrcweir                         XSecuritySAXEventKeeper.class, saxEventKeeperObj);
296cdf0e10cSrcweir 
297cdf0e10cSrcweir                             /*
298cdf0e10cSrcweir                              * initializes the SAXEventKeeper component with the XMLDocumentWrapper component.
299cdf0e10cSrcweir                              */
300cdf0e10cSrcweir                 XInitialization xInitialization =
301cdf0e10cSrcweir                     (XInitialization)UnoRuntime.queryInterface(
302cdf0e10cSrcweir                         XInitialization.class, m_xSAXEventKeeper);
303cdf0e10cSrcweir                 Object args[]=new Object[1];
304cdf0e10cSrcweir                 args[0] = m_xXMLDocumentWrapper;
305cdf0e10cSrcweir                 xInitialization.initialize(args);
306cdf0e10cSrcweir             }
307cdf0e10cSrcweir             catch( com.sun.star.uno.Exception e)
308cdf0e10cSrcweir             {
309cdf0e10cSrcweir                 e.printStackTrace();
310cdf0e10cSrcweir             }
311cdf0e10cSrcweir 
312cdf0e10cSrcweir             /*
313cdf0e10cSrcweir              * configures the SAXEventKeeper's status change listener.
314cdf0e10cSrcweir              */
315cdf0e10cSrcweir             XSAXEventKeeperStatusChangeBroadcaster xSaxEventKeeperStatusChangeBroadcaster =
316cdf0e10cSrcweir                 (XSAXEventKeeperStatusChangeBroadcaster)UnoRuntime.queryInterface(
317cdf0e10cSrcweir                     XSAXEventKeeperStatusChangeBroadcaster.class, m_xSAXEventKeeper);
318cdf0e10cSrcweir             xSaxEventKeeperStatusChangeBroadcaster.addSAXEventKeeperStatusChangeListener(this);
319cdf0e10cSrcweir         }
320cdf0e10cSrcweir 
321cdf0e10cSrcweir         boolean rc = !m_bSAXEventKeeperIncluded;
322cdf0e10cSrcweir 
323cdf0e10cSrcweir         /*
324cdf0e10cSrcweir          * changes the export document handler.
325cdf0e10cSrcweir          */
326cdf0e10cSrcweir         m_bSAXEventKeeperIncluded=true;
327cdf0e10cSrcweir         changeOutput();
328cdf0e10cSrcweir 
329cdf0e10cSrcweir         return rc;
330cdf0e10cSrcweir     }
331cdf0e10cSrcweir 
332cdf0e10cSrcweir     /*
333cdf0e10cSrcweir      * finds key element or referenced element for a signature.
334cdf0e10cSrcweir      */
335cdf0e10cSrcweir     private void findKeyOrReference(SecurityEntity signatureEntity, String uriStr, boolean isFindingKey)
336cdf0e10cSrcweir     {
337cdf0e10cSrcweir         int i=0;
338cdf0e10cSrcweir 
339cdf0e10cSrcweir         while (i<m_vUnsolvedReferenceIds.size())
340cdf0e10cSrcweir         {
341cdf0e10cSrcweir             String id = (String)m_vUnsolvedReferenceIds.elementAt(i);
342cdf0e10cSrcweir 
343cdf0e10cSrcweir             if (id.equals(uriStr))
344cdf0e10cSrcweir             {
345cdf0e10cSrcweir                 int refNum = ((Integer)m_vUnsolvedReferenceRefNum.elementAt(i)).intValue();
346cdf0e10cSrcweir                 int keeperId = ((Integer)m_vUnsolvedReferencedKeeperIds.elementAt(i)).intValue();
347cdf0e10cSrcweir 
348cdf0e10cSrcweir                 if (isFindingKey)
349cdf0e10cSrcweir                 {
350cdf0e10cSrcweir                     /*
351cdf0e10cSrcweir                      * clones a new ElementCollector for the key element.
352cdf0e10cSrcweir                      */
353cdf0e10cSrcweir                     int cloneKeeperId = m_xSAXEventKeeper.cloneElementCollector(
354cdf0e10cSrcweir                         keeperId,
355cdf0e10cSrcweir                         m_bIsExporting?
356cdf0e10cSrcweir                         (ElementMarkPriority.BEFOREMODIFY):(ElementMarkPriority.AFTERMODIFY));
357cdf0e10cSrcweir 
358cdf0e10cSrcweir                     /*
359cdf0e10cSrcweir                      * notifies the key keeper id.
360cdf0e10cSrcweir                      */
361cdf0e10cSrcweir                     signatureEntity.setKeyId(cloneKeeperId);
362cdf0e10cSrcweir 
363cdf0e10cSrcweir                     /*
364cdf0e10cSrcweir                      * sets the security id for the key.
365cdf0e10cSrcweir                      */
366cdf0e10cSrcweir                     m_xSAXEventKeeper.setSecurityId(cloneKeeperId, signatureEntity.getSecurityId());
367cdf0e10cSrcweir 
368cdf0e10cSrcweir                     /*
369cdf0e10cSrcweir                      * sets the resolve listener.
370cdf0e10cSrcweir                      */
371cdf0e10cSrcweir                     XReferenceResolvedBroadcaster xReferenceResolvedBroadcaster =
372cdf0e10cSrcweir                         (XReferenceResolvedBroadcaster)UnoRuntime.queryInterface(
373cdf0e10cSrcweir                             XReferenceResolvedBroadcaster.class, m_xSAXEventKeeper);
374cdf0e10cSrcweir                     xReferenceResolvedBroadcaster.addReferenceResolvedListener(
375cdf0e10cSrcweir                         cloneKeeperId,
376cdf0e10cSrcweir                         signatureEntity.getReferenceListener());
377cdf0e10cSrcweir                 }
378cdf0e10cSrcweir                 else
379cdf0e10cSrcweir                 {
380cdf0e10cSrcweir                     /*
381cdf0e10cSrcweir                      * clones a new ElementCollector for the referenced element.
382cdf0e10cSrcweir                      */
383cdf0e10cSrcweir                     int cloneKeeperId = m_xSAXEventKeeper.cloneElementCollector(
384cdf0e10cSrcweir                         keeperId,
385cdf0e10cSrcweir                         m_bIsExporting?
386cdf0e10cSrcweir                         (ElementMarkPriority.AFTERMODIFY):(ElementMarkPriority.BEFOREMODIFY));
387cdf0e10cSrcweir 
388cdf0e10cSrcweir                     /*
389cdf0e10cSrcweir                      * sets the security id.
390cdf0e10cSrcweir                      */
391cdf0e10cSrcweir                     m_xSAXEventKeeper.setSecurityId(cloneKeeperId, signatureEntity.getSecurityId());
392cdf0e10cSrcweir 
393cdf0e10cSrcweir                     /*
394cdf0e10cSrcweir                      * sets the resolve listener.
395cdf0e10cSrcweir                      */
396cdf0e10cSrcweir                     XReferenceResolvedBroadcaster xReferenceResolvedBroadcaster =
397cdf0e10cSrcweir                         (XReferenceResolvedBroadcaster)UnoRuntime.queryInterface(
398cdf0e10cSrcweir                             XReferenceResolvedBroadcaster.class, m_xSAXEventKeeper);
399cdf0e10cSrcweir                     xReferenceResolvedBroadcaster.addReferenceResolvedListener(cloneKeeperId,
400cdf0e10cSrcweir                         signatureEntity.getReferenceListener());
401cdf0e10cSrcweir 
402cdf0e10cSrcweir                     try{
403cdf0e10cSrcweir                         XReferenceCollector xReferenceCollector =
404cdf0e10cSrcweir                             (XReferenceCollector)UnoRuntime.queryInterface(
405cdf0e10cSrcweir                                 XReferenceCollector.class, signatureEntity.getReferenceListener());
406cdf0e10cSrcweir                         xReferenceCollector.setReferenceId(cloneKeeperId);
407cdf0e10cSrcweir                     }
408cdf0e10cSrcweir                     catch( com.sun.star.uno.Exception e)
409cdf0e10cSrcweir                     {
410cdf0e10cSrcweir                         e.printStackTrace();
411cdf0e10cSrcweir                     }
412cdf0e10cSrcweir                 }
413cdf0e10cSrcweir 
414cdf0e10cSrcweir                 /*
415cdf0e10cSrcweir                  * if this unsolved reference reaches its max reference number, remove this reference
416cdf0e10cSrcweir                  * from all vectors.
417cdf0e10cSrcweir                  */
418cdf0e10cSrcweir                 refNum--;
419cdf0e10cSrcweir                 if (refNum == 0)
420cdf0e10cSrcweir                 {
421cdf0e10cSrcweir                     m_xSAXEventKeeper.removeElementCollector(keeperId);
422cdf0e10cSrcweir                     m_vUnsolvedReferenceIds.remove(i);
423cdf0e10cSrcweir                     m_vUnsolvedReferencedKeeperIds.remove(i);
424cdf0e10cSrcweir                     m_vUnsolvedReferenceRefNum.remove(i);
425cdf0e10cSrcweir                 }
426cdf0e10cSrcweir                 else
427cdf0e10cSrcweir                 {
428cdf0e10cSrcweir                     m_vUnsolvedReferenceRefNum.setElementAt(new Integer(refNum),(i));
429cdf0e10cSrcweir                     ++i;
430cdf0e10cSrcweir                 }
431cdf0e10cSrcweir 
432cdf0e10cSrcweir                 /*
433cdf0e10cSrcweir                  * If it is find a key, then no further search is needed, one
434cdf0e10cSrcweir                  * signature has one key at most.
435cdf0e10cSrcweir                  */
436cdf0e10cSrcweir                 if (isFindingKey)
437cdf0e10cSrcweir                 {
438cdf0e10cSrcweir                     break;
439cdf0e10cSrcweir                 }
440cdf0e10cSrcweir             }
441cdf0e10cSrcweir             else
442cdf0e10cSrcweir             {
443cdf0e10cSrcweir                 ++i;
444cdf0e10cSrcweir             }
445cdf0e10cSrcweir         }
446cdf0e10cSrcweir     }
447cdf0e10cSrcweir 
448cdf0e10cSrcweir     /*
449cdf0e10cSrcweir      * checks whether a startElement event represents any security related information.
450cdf0e10cSrcweir      * return true if this event can't be forwarded into the SAX chain.
451cdf0e10cSrcweir      */
452cdf0e10cSrcweir     private boolean checkSecurityElement(String localName, com.sun.star.xml.sax.XAttributeList xattribs)
453cdf0e10cSrcweir     {
454cdf0e10cSrcweir         boolean rc = false;
455cdf0e10cSrcweir 
456cdf0e10cSrcweir         if (localName.equals("Signature"))
457cdf0e10cSrcweir         /*
458cdf0e10cSrcweir          * this element is a Signature element.
459cdf0e10cSrcweir          */
460cdf0e10cSrcweir         {
461cdf0e10cSrcweir             SignatureEntity signatureEntity = new SignatureEntity(
462cdf0e10cSrcweir                 m_xSAXEventKeeper,
463cdf0e10cSrcweir                 m_bIsExporting,
464cdf0e10cSrcweir                 this,
465cdf0e10cSrcweir                 m_xXMLSecurityContext,
466cdf0e10cSrcweir                 m_xXMLSignature,
467cdf0e10cSrcweir                 m_xXMLEncryption,
468cdf0e10cSrcweir                 m_xRemoteServiceManager,
469cdf0e10cSrcweir                 m_xRemoteContext);
470cdf0e10cSrcweir 
471cdf0e10cSrcweir             m_signatureList.add(signatureEntity);
472cdf0e10cSrcweir             m_currentPath.push(signatureEntity);
473cdf0e10cSrcweir         }
474cdf0e10cSrcweir         else if(localName.equals("Reference"))
475cdf0e10cSrcweir         {
476cdf0e10cSrcweir             if (!m_currentPath.empty())
477cdf0e10cSrcweir             {
478cdf0e10cSrcweir                 Object signedInfo = m_currentPath.pop();
479cdf0e10cSrcweir 
480cdf0e10cSrcweir                 if (!m_currentPath.empty())
481cdf0e10cSrcweir                 {
482cdf0e10cSrcweir                     Object objSignature = m_currentPath.peek();
483cdf0e10cSrcweir 
484cdf0e10cSrcweir                     if ((objSignature instanceof SignatureEntity) && signedInfo.toString().equals("SignedInfo"))
485cdf0e10cSrcweir                     /*
486cdf0e10cSrcweir                      * this element is a Reference element in a signature.
487cdf0e10cSrcweir                      */
488cdf0e10cSrcweir                     {
489cdf0e10cSrcweir                         String uriStr = xattribs.getValueByName("URI");
490cdf0e10cSrcweir 
491cdf0e10cSrcweir                         if (uriStr.charAt(0) == '#')
492cdf0e10cSrcweir                         {
493cdf0e10cSrcweir                             uriStr = uriStr.substring(1);
494cdf0e10cSrcweir                             SignatureEntity signatureEntity = (SignatureEntity)objSignature;
495cdf0e10cSrcweir 
496cdf0e10cSrcweir                             if (uriStr != null && uriStr.length()>0)
497cdf0e10cSrcweir                             {
498cdf0e10cSrcweir                                 signatureEntity.addReferenceId(uriStr);
499cdf0e10cSrcweir                                 findKeyOrReference(signatureEntity, uriStr, false);
500cdf0e10cSrcweir                             }
501cdf0e10cSrcweir                         }
502cdf0e10cSrcweir                     }
503cdf0e10cSrcweir                 }
504cdf0e10cSrcweir                 m_currentPath.push(signedInfo);
505cdf0e10cSrcweir             }
506cdf0e10cSrcweir             m_currentPath.push(localName);
507cdf0e10cSrcweir         }
508cdf0e10cSrcweir         else if(localName.equals("KeyValue") ||
509cdf0e10cSrcweir                 localName.equals("KeyName") ||
510cdf0e10cSrcweir                 localName.equals("X509Data") ||
511cdf0e10cSrcweir                 localName.equals("EncryptedKey"))
512cdf0e10cSrcweir         {
513cdf0e10cSrcweir             if (!m_currentPath.empty())
514cdf0e10cSrcweir             {
515cdf0e10cSrcweir                 Object keyInfo = m_currentPath.pop();
516cdf0e10cSrcweir 
517cdf0e10cSrcweir                 if (!m_currentPath.empty())
518cdf0e10cSrcweir                 {
519cdf0e10cSrcweir                     Object objSorE = m_currentPath.peek();
520cdf0e10cSrcweir 
521cdf0e10cSrcweir                     if ((objSorE instanceof SignatureEntity) && keyInfo.toString().equals("KeyInfo"))
522cdf0e10cSrcweir                     /*
523cdf0e10cSrcweir                      * this element is the key element of a signature.
524cdf0e10cSrcweir                      */
525cdf0e10cSrcweir                     {
526cdf0e10cSrcweir                         SignatureEntity signatureEntity = (SignatureEntity)objSorE;
527cdf0e10cSrcweir                         signatureEntity.setKeyId(0);
528cdf0e10cSrcweir                     }
529cdf0e10cSrcweir                     else if ((objSorE instanceof EncryptionEntity) && keyInfo.toString().equals("KeyInfo"))
530cdf0e10cSrcweir                     /*
531cdf0e10cSrcweir                      * this element is the key element of an encryption.
532cdf0e10cSrcweir                      */
533cdf0e10cSrcweir                     {
534cdf0e10cSrcweir                         EncryptionEntity theEncryption = (EncryptionEntity)objSorE;
535cdf0e10cSrcweir                         theEncryption.setKeyId(0);
536cdf0e10cSrcweir                     }
537cdf0e10cSrcweir                 }
538cdf0e10cSrcweir                 m_currentPath.push(keyInfo);
539cdf0e10cSrcweir             }
540cdf0e10cSrcweir 
541cdf0e10cSrcweir             m_currentPath.push(localName);
542cdf0e10cSrcweir         }
543cdf0e10cSrcweir         else if(localName.equals("RetrievalMethod"))
544cdf0e10cSrcweir         {
545cdf0e10cSrcweir             if (!m_currentPath.empty())
546cdf0e10cSrcweir             {
547cdf0e10cSrcweir                 Object keyInfo = m_currentPath.pop();
548cdf0e10cSrcweir 
549cdf0e10cSrcweir                 if (!m_currentPath.empty())
550cdf0e10cSrcweir                 {
551cdf0e10cSrcweir                     Object objSorE = m_currentPath.peek();
552cdf0e10cSrcweir 
553cdf0e10cSrcweir                     if ((objSorE instanceof SignatureEntity) && keyInfo.toString().equals("KeyInfo"))
554cdf0e10cSrcweir                     /*
555cdf0e10cSrcweir                      * this element is the RetrievalMethod element in a signature,
556cdf0e10cSrcweir                      * which will include the key uri of this signature.
557cdf0e10cSrcweir                      */
558cdf0e10cSrcweir                     {
559cdf0e10cSrcweir                         String uriStr = xattribs.getValueByName("URI");
560cdf0e10cSrcweir                         SignatureEntity signatureEntity = (SignatureEntity)objSorE;
561cdf0e10cSrcweir 
562cdf0e10cSrcweir                         if (uriStr != null && uriStr.length()>0)
563cdf0e10cSrcweir                         {
564cdf0e10cSrcweir                             signatureEntity.setKeyURI(uriStr);
565cdf0e10cSrcweir                             findKeyOrReference(signatureEntity,uriStr, true);
566cdf0e10cSrcweir                         }
567cdf0e10cSrcweir                     }
568cdf0e10cSrcweir                     else if ((objSorE instanceof EncryptionEntity) && keyInfo.toString().equals("KeyInfo"))
569cdf0e10cSrcweir                     /*
570cdf0e10cSrcweir                      * this element is the RetrievalMethod element in an encryption,
571cdf0e10cSrcweir                      * which will include the key uri of this encryption.
572cdf0e10cSrcweir                      */
573cdf0e10cSrcweir                     {
574cdf0e10cSrcweir                         String uriStr = xattribs.getValueByName("URI");
575cdf0e10cSrcweir                         EncryptionEntity theEncryption = (EncryptionEntity)objSorE;
576cdf0e10cSrcweir 
577cdf0e10cSrcweir                         if (uriStr != null && uriStr.length()>0)
578cdf0e10cSrcweir                         {
579cdf0e10cSrcweir                             theEncryption.setKeyURI(uriStr);
580cdf0e10cSrcweir                             findKeyOrReference(theEncryption, uriStr, true);
581cdf0e10cSrcweir                         }
582cdf0e10cSrcweir                     }
583cdf0e10cSrcweir                 }
584cdf0e10cSrcweir                 m_currentPath.push(keyInfo);
585cdf0e10cSrcweir             }
586cdf0e10cSrcweir             m_currentPath.push(localName);
587cdf0e10cSrcweir         }
588cdf0e10cSrcweir         else if (localName.equals("EncryptedData")) /* || localName.equals("EncryptedKey")) */
589cdf0e10cSrcweir         /*
590cdf0e10cSrcweir          * this element is an Encryption element.
591cdf0e10cSrcweir          */
592cdf0e10cSrcweir         {
593cdf0e10cSrcweir             EncryptionEntity theEncryption = new EncryptionEntity(
594cdf0e10cSrcweir                 m_xSAXEventKeeper,
595cdf0e10cSrcweir                 m_bIsExporting,
596cdf0e10cSrcweir                 this,
597cdf0e10cSrcweir                 m_xXMLSecurityContext,
598cdf0e10cSrcweir                 m_xXMLSignature,
599cdf0e10cSrcweir                 m_xXMLEncryption,
600cdf0e10cSrcweir                 m_xRemoteServiceManager,
601cdf0e10cSrcweir                 m_xRemoteContext);
602cdf0e10cSrcweir 
603cdf0e10cSrcweir             m_encryptionList.add(theEncryption);
604cdf0e10cSrcweir 
605cdf0e10cSrcweir             if (m_bIsExporting)
606cdf0e10cSrcweir             {
607cdf0e10cSrcweir                 m_currentPath.push(theEncryption);
608cdf0e10cSrcweir             }
609cdf0e10cSrcweir             else
610cdf0e10cSrcweir             {
611cdf0e10cSrcweir                 String uriStr = xattribs.getValueByName("keyURI");
612cdf0e10cSrcweir                 if (uriStr != null && uriStr.length()>0)
613cdf0e10cSrcweir                 {
614cdf0e10cSrcweir                     theEncryption.setKeyURI(uriStr);
615cdf0e10cSrcweir                     findKeyOrReference(theEncryption,uriStr, true);
616cdf0e10cSrcweir                 }
617cdf0e10cSrcweir                 else
618cdf0e10cSrcweir                 {
619cdf0e10cSrcweir                     theEncryption.setKeyId(0);
620cdf0e10cSrcweir                 }
621cdf0e10cSrcweir 
622cdf0e10cSrcweir                 rc = true;
623cdf0e10cSrcweir             }
624cdf0e10cSrcweir         }
625cdf0e10cSrcweir         else
626cdf0e10cSrcweir         /*
627cdf0e10cSrcweir          * not a security related element.
628cdf0e10cSrcweir          */
629cdf0e10cSrcweir         {
630cdf0e10cSrcweir             m_currentPath.push(localName);
631cdf0e10cSrcweir         }
632cdf0e10cSrcweir 
633cdf0e10cSrcweir         return rc;
634cdf0e10cSrcweir     }
635cdf0e10cSrcweir 
636cdf0e10cSrcweir     /*
637cdf0e10cSrcweir      * checks whether a startElement event is referenced by any security entity.
638cdf0e10cSrcweir      */
639cdf0e10cSrcweir     private void checkReference(String localName, com.sun.star.xml.sax.XAttributeList xattribs, String id)
640cdf0e10cSrcweir     {
641cdf0e10cSrcweir         String refNumStr = xattribs.getValueByName("refNum");
642cdf0e10cSrcweir 
643cdf0e10cSrcweir         if ( m_bIsEncryptionTarget )
644cdf0e10cSrcweir         {
645cdf0e10cSrcweir             m_EncryptionForTarget.setReference(m_bIsExporting);
646cdf0e10cSrcweir             m_bIsEncryptionTarget = false;
647cdf0e10cSrcweir         }
648cdf0e10cSrcweir 
649cdf0e10cSrcweir         if (id != null && id.length()>0 )
650cdf0e10cSrcweir         /*
651cdf0e10cSrcweir          * only if this element has id attribute, then it can be referenced by
652cdf0e10cSrcweir          * a security entity.
653cdf0e10cSrcweir          */
654cdf0e10cSrcweir         {
655cdf0e10cSrcweir             /*
656cdf0e10cSrcweir              * if this element has an "refNum" attribute, then the value will be
657cdf0e10cSrcweir              * the max referencing number on this element, otherwise, set the max
658cdf0e10cSrcweir              * referencing number to 999.
659cdf0e10cSrcweir              */
660cdf0e10cSrcweir             int refNum = 999;
661cdf0e10cSrcweir 
662cdf0e10cSrcweir             if (refNumStr != null && refNumStr.length()>0 )
663cdf0e10cSrcweir             {
664cdf0e10cSrcweir                 refNum = new Integer(refNumStr).intValue();
665cdf0e10cSrcweir             }
666cdf0e10cSrcweir 
667cdf0e10cSrcweir             int length;
668cdf0e10cSrcweir 
669cdf0e10cSrcweir             /*
670cdf0e10cSrcweir              * searches the signature list to check whether any sigture has
671cdf0e10cSrcweir              * reference on this element.
672cdf0e10cSrcweir              */
673cdf0e10cSrcweir             length = m_signatureList.size();
674cdf0e10cSrcweir             for (int i=0; i<length; ++i)
675cdf0e10cSrcweir             {
676cdf0e10cSrcweir                 SignatureEntity signatureEntity = (SignatureEntity)m_signatureList.elementAt(i);
677cdf0e10cSrcweir 
678cdf0e10cSrcweir                 if (signatureEntity.setReference(id, m_bIsExporting))
679cdf0e10cSrcweir                 {
680cdf0e10cSrcweir                     refNum--;
681cdf0e10cSrcweir                 }
682cdf0e10cSrcweir 
683cdf0e10cSrcweir                 if (signatureEntity.setKey(id, m_bIsExporting))
684cdf0e10cSrcweir                 {
685cdf0e10cSrcweir                     refNum--;
686cdf0e10cSrcweir                 }
687cdf0e10cSrcweir             }
688cdf0e10cSrcweir 
689cdf0e10cSrcweir             /*
690cdf0e10cSrcweir              * searches the encryption list for reference.
691cdf0e10cSrcweir              */
692cdf0e10cSrcweir             length = m_encryptionList.size();
693cdf0e10cSrcweir             for (int i=0; i<length; ++i)
694cdf0e10cSrcweir             {
695cdf0e10cSrcweir                 EncryptionEntity theEncryption = (EncryptionEntity)m_encryptionList.elementAt(i);
696cdf0e10cSrcweir 
697cdf0e10cSrcweir                 if (theEncryption.setKey(id, m_bIsExporting))
698cdf0e10cSrcweir                 {
699cdf0e10cSrcweir                     refNum--;
700cdf0e10cSrcweir                 }
701cdf0e10cSrcweir             }
702cdf0e10cSrcweir 
703cdf0e10cSrcweir             /*
704cdf0e10cSrcweir              * if the max referencing number is not reached, then add this element
705cdf0e10cSrcweir              * into the unsolved reference list.
706cdf0e10cSrcweir              */
707cdf0e10cSrcweir             if (refNum>0)
708cdf0e10cSrcweir             {
709cdf0e10cSrcweir                 int keeperId;
710cdf0e10cSrcweir 
711cdf0e10cSrcweir                 if (localName.equals("EncryptedKey"))
712cdf0e10cSrcweir                 {
713cdf0e10cSrcweir                     keeperId = m_xSAXEventKeeper.addSecurityElementCollector(
714cdf0e10cSrcweir                         m_bIsExporting?
715cdf0e10cSrcweir                         (ElementMarkPriority.BEFOREMODIFY):(ElementMarkPriority.AFTERMODIFY),
716cdf0e10cSrcweir                         true);
717cdf0e10cSrcweir                 }
718cdf0e10cSrcweir                 else
719cdf0e10cSrcweir                 {
720cdf0e10cSrcweir                     keeperId = m_xSAXEventKeeper.addSecurityElementCollector(
721cdf0e10cSrcweir                         m_bIsExporting?
722cdf0e10cSrcweir                         (ElementMarkPriority.AFTERMODIFY):(ElementMarkPriority.BEFOREMODIFY),
723cdf0e10cSrcweir                         false);
724cdf0e10cSrcweir                 }
725cdf0e10cSrcweir 
726cdf0e10cSrcweir                 m_vUnsolvedReferenceIds.add(id);
727cdf0e10cSrcweir                 m_vUnsolvedReferencedKeeperIds.add(new Integer(keeperId));
728cdf0e10cSrcweir                 m_vUnsolvedReferenceRefNum.add(new Integer(refNum));
729cdf0e10cSrcweir             }
730cdf0e10cSrcweir         }
731cdf0e10cSrcweir     }
732cdf0e10cSrcweir 
733cdf0e10cSrcweir     /*
734cdf0e10cSrcweir      * configures the output handler.
735cdf0e10cSrcweir      */
736cdf0e10cSrcweir     private void setOutputHandler(XDocumentHandler handler)
737cdf0e10cSrcweir     {
738cdf0e10cSrcweir         m_xOutputHandler = handler;
739cdf0e10cSrcweir         changeOutput();
740cdf0e10cSrcweir     }
741cdf0e10cSrcweir 
742cdf0e10cSrcweir 
743cdf0e10cSrcweir /**************************************************************************************
744cdf0e10cSrcweir  * protected methods
745cdf0e10cSrcweir  **************************************************************************************/
746cdf0e10cSrcweir 
747cdf0e10cSrcweir         /*
748cdf0e10cSrcweir          * methods used to transfer unsolved reference information.
749cdf0e10cSrcweir          */
750cdf0e10cSrcweir     protected Vector getUnsolvedReferenceIds()
751cdf0e10cSrcweir     {
752cdf0e10cSrcweir         return m_vUnsolvedReferenceIds;
753cdf0e10cSrcweir     }
754cdf0e10cSrcweir 
755cdf0e10cSrcweir     protected Vector getUnsolvedReferenceKeeperIds()
756cdf0e10cSrcweir     {
757cdf0e10cSrcweir         return m_vUnsolvedReferencedKeeperIds;
758cdf0e10cSrcweir     }
759cdf0e10cSrcweir 
760cdf0e10cSrcweir     protected Vector getUnsolvedReferenceRefNum()
761cdf0e10cSrcweir     {
762cdf0e10cSrcweir         return m_vUnsolvedReferenceRefNum;
763cdf0e10cSrcweir     }
764cdf0e10cSrcweir 
765cdf0e10cSrcweir     protected String getBufferNodeTreeInformation()
766cdf0e10cSrcweir     {
767cdf0e10cSrcweir         if (m_xSAXEventKeeper != null)
768cdf0e10cSrcweir         {
769cdf0e10cSrcweir             return m_xSAXEventKeeper.printBufferNodeTree();
770cdf0e10cSrcweir         }
771cdf0e10cSrcweir         else
772cdf0e10cSrcweir         {
773cdf0e10cSrcweir             return null;
774cdf0e10cSrcweir         }
775cdf0e10cSrcweir     }
776cdf0e10cSrcweir 
777cdf0e10cSrcweir     protected void getDocument(XDocumentHandler handler)
778cdf0e10cSrcweir     {
779cdf0e10cSrcweir         if (m_xXMLDocumentWrapper != null)
780cdf0e10cSrcweir         {
781cdf0e10cSrcweir             try
782cdf0e10cSrcweir             {
783cdf0e10cSrcweir                 m_xXMLDocumentWrapper.getTree(handler);
784cdf0e10cSrcweir             }
785cdf0e10cSrcweir             catch(SAXException e)
786cdf0e10cSrcweir             {
787cdf0e10cSrcweir                 e.printStackTrace();
788cdf0e10cSrcweir             }
789cdf0e10cSrcweir         }
790cdf0e10cSrcweir     }
791cdf0e10cSrcweir 
792cdf0e10cSrcweir     protected void endMission()
793cdf0e10cSrcweir     {
794cdf0e10cSrcweir         while (m_signatureList.size()>0 || m_encryptionList.size()>0)
795cdf0e10cSrcweir         {
796cdf0e10cSrcweir             if (m_signatureList.size()>0)
797cdf0e10cSrcweir             {
798cdf0e10cSrcweir                 SignatureEntity signatureEntity = (SignatureEntity)m_signatureList.elementAt(0);
799cdf0e10cSrcweir                 m_signatureList.remove(0);
800cdf0e10cSrcweir                 signatureEntity.endMission();
801cdf0e10cSrcweir             }
802cdf0e10cSrcweir             else if (m_encryptionList.size()>0)
803cdf0e10cSrcweir             {
804cdf0e10cSrcweir                 EncryptionEntity theEncryption = (EncryptionEntity)m_encryptionList.elementAt(0);
805cdf0e10cSrcweir                 m_encryptionList.remove(0);
806cdf0e10cSrcweir                 theEncryption.endMission();
807cdf0e10cSrcweir             }
808cdf0e10cSrcweir         }
809cdf0e10cSrcweir 
810cdf0e10cSrcweir         while (m_vUnsolvedReferenceIds.size()>0)
811cdf0e10cSrcweir         {
812cdf0e10cSrcweir             int keeperId = ((Integer)m_vUnsolvedReferencedKeeperIds.elementAt(0)).intValue();
813cdf0e10cSrcweir             m_xSAXEventKeeper.removeElementCollector(keeperId);
814cdf0e10cSrcweir             m_vUnsolvedReferenceIds.remove(0);
815cdf0e10cSrcweir             m_vUnsolvedReferencedKeeperIds.remove(0);
816cdf0e10cSrcweir             m_vUnsolvedReferenceRefNum.remove(0);
817cdf0e10cSrcweir         }
818cdf0e10cSrcweir 
819cdf0e10cSrcweir         m_xSAXEventKeeper.setNextHandler(null);
820cdf0e10cSrcweir 
821cdf0e10cSrcweir         XSAXEventKeeperStatusChangeBroadcaster xSaxEventKeeperStatusChangeBroadcaster =
822cdf0e10cSrcweir             (XSAXEventKeeperStatusChangeBroadcaster)UnoRuntime.queryInterface(
823cdf0e10cSrcweir                 XSAXEventKeeperStatusChangeBroadcaster.class, m_xSAXEventKeeper);
824cdf0e10cSrcweir         xSaxEventKeeperStatusChangeBroadcaster.addSAXEventKeeperStatusChangeListener(null);
825cdf0e10cSrcweir 
826cdf0e10cSrcweir         m_xSAXEventKeeper = null;
827cdf0e10cSrcweir         m_xXMLDocumentWrapper = null;
828cdf0e10cSrcweir         m_xOutputHandler = null;
829cdf0e10cSrcweir         m_xXMLSecurityContext = null;
830cdf0e10cSrcweir         m_xXMLSignature = null;
831cdf0e10cSrcweir         m_xXMLEncryption = null;
832cdf0e10cSrcweir 
833cdf0e10cSrcweir         m_xExportHandler = null;
834cdf0e10cSrcweir         m_parsingThread.setHandler(null);
835cdf0e10cSrcweir     }
836cdf0e10cSrcweir 
837cdf0e10cSrcweir /**************************************************************************************
838cdf0e10cSrcweir  * public methods
839cdf0e10cSrcweir  **************************************************************************************/
840cdf0e10cSrcweir 
841cdf0e10cSrcweir     /*
842cdf0e10cSrcweir      * XDocumentHandler
843cdf0e10cSrcweir      */
844cdf0e10cSrcweir         public void startDocument()
845cdf0e10cSrcweir     {
846cdf0e10cSrcweir         try{
847cdf0e10cSrcweir             m_xExportHandler.startDocument();
848cdf0e10cSrcweir         }
849cdf0e10cSrcweir         catch( com.sun.star.xml.sax.SAXException e)
850cdf0e10cSrcweir         {
851cdf0e10cSrcweir             e.printStackTrace();
852cdf0e10cSrcweir         }
853cdf0e10cSrcweir 
854cdf0e10cSrcweir     }
855cdf0e10cSrcweir 
856cdf0e10cSrcweir         public void endDocument()
857cdf0e10cSrcweir     {
858cdf0e10cSrcweir         try{
859cdf0e10cSrcweir             m_xExportHandler.endDocument();
860cdf0e10cSrcweir         }
861cdf0e10cSrcweir         catch( com.sun.star.xml.sax.SAXException e)
862cdf0e10cSrcweir         {
863cdf0e10cSrcweir             e.printStackTrace();
864cdf0e10cSrcweir         }
865cdf0e10cSrcweir     }
866cdf0e10cSrcweir 
867cdf0e10cSrcweir     public void startElement (String str, com.sun.star.xml.sax.XAttributeList xattribs)
868cdf0e10cSrcweir     {
869cdf0e10cSrcweir         try{
870cdf0e10cSrcweir             String idAttr = xattribs.getValueByName("id");
871cdf0e10cSrcweir             if (idAttr == null)
872cdf0e10cSrcweir             {
873cdf0e10cSrcweir                 idAttr = xattribs.getValueByName("Id");
874cdf0e10cSrcweir             }
875cdf0e10cSrcweir 
876cdf0e10cSrcweir             boolean hasIdAttr = (idAttr != null && idAttr.length()>0 );
877cdf0e10cSrcweir             boolean needResend = false;
878cdf0e10cSrcweir 
879cdf0e10cSrcweir             if (hasIdAttr ||
880cdf0e10cSrcweir                 (str.equals("Signature")||str.equals("EncryptedData")))/* || str.equals("EncryptedKey"))) */
881cdf0e10cSrcweir             {
882cdf0e10cSrcweir                 if (foundSecurityRelated() && !m_bIsExporting)
883cdf0e10cSrcweir                 {
884cdf0e10cSrcweir                     needResend = true;
885cdf0e10cSrcweir                 }
886cdf0e10cSrcweir             }
887cdf0e10cSrcweir 
888cdf0e10cSrcweir             boolean suppressToNext = checkSecurityElement(str, xattribs);
889cdf0e10cSrcweir 
890cdf0e10cSrcweir             checkReference(str, xattribs, idAttr);
891cdf0e10cSrcweir 
892cdf0e10cSrcweir             if (needResend)
893cdf0e10cSrcweir             {
894cdf0e10cSrcweir                 m_xSAXEventKeeper.setNextHandler(null);
895cdf0e10cSrcweir 
896cdf0e10cSrcweir                 XDocumentHandler saxEventKeeperHandler =
897cdf0e10cSrcweir                     (XDocumentHandler)UnoRuntime.queryInterface(
898cdf0e10cSrcweir                         XDocumentHandler.class, m_xSAXEventKeeper);
899cdf0e10cSrcweir                 saxEventKeeperHandler.startElement(str, xattribs);
900cdf0e10cSrcweir                 m_xSAXEventKeeper.setNextHandler((XDocumentHandler)this);
901cdf0e10cSrcweir             }
902cdf0e10cSrcweir 
903cdf0e10cSrcweir             if (!suppressToNext)
904cdf0e10cSrcweir             {
905cdf0e10cSrcweir                 m_xExportHandler.startElement(str, xattribs);
906cdf0e10cSrcweir             }
907cdf0e10cSrcweir         }
908cdf0e10cSrcweir         catch( com.sun.star.xml.sax.SAXException e)
909cdf0e10cSrcweir         {
910cdf0e10cSrcweir             e.printStackTrace();
911cdf0e10cSrcweir         }
912cdf0e10cSrcweir     }
913cdf0e10cSrcweir 
914cdf0e10cSrcweir     public void endElement(String str)
915cdf0e10cSrcweir     {
916cdf0e10cSrcweir         if (!m_currentPath.empty())
917cdf0e10cSrcweir         {
918cdf0e10cSrcweir                 Object obj = m_currentPath.pop();
919cdf0e10cSrcweir 
920cdf0e10cSrcweir                 if (obj.toString().equals("SignedInfo"))
921cdf0e10cSrcweir                 {
922cdf0e10cSrcweir                 if (!m_currentPath.empty())
923cdf0e10cSrcweir                 {
924cdf0e10cSrcweir                         Object objSignature = m_currentPath.peek();
925cdf0e10cSrcweir                         if (objSignature != null && objSignature instanceof SignatureEntity)
926cdf0e10cSrcweir                         {
927cdf0e10cSrcweir                             ((SignatureEntity)objSignature).setReferenceNumber();
928cdf0e10cSrcweir                         }
929cdf0e10cSrcweir                     }
930cdf0e10cSrcweir                 }
931cdf0e10cSrcweir                 else if (obj instanceof EncryptionEntity)
932cdf0e10cSrcweir                 {
933cdf0e10cSrcweir                     m_bIsEncryptionTarget = true;
934cdf0e10cSrcweir                 m_EncryptionForTarget = (EncryptionEntity)obj;
935cdf0e10cSrcweir 
936cdf0e10cSrcweir                 }
937cdf0e10cSrcweir             }
938cdf0e10cSrcweir 
939cdf0e10cSrcweir         try{
940cdf0e10cSrcweir             m_xExportHandler.endElement(str);
941cdf0e10cSrcweir         }
942cdf0e10cSrcweir         catch( com.sun.star.xml.sax.SAXException e)
943cdf0e10cSrcweir         {
944cdf0e10cSrcweir             e.printStackTrace();
945cdf0e10cSrcweir         }
946cdf0e10cSrcweir     }
947cdf0e10cSrcweir 
948cdf0e10cSrcweir     public void characters(String str)
949cdf0e10cSrcweir     {
950cdf0e10cSrcweir         try{
951cdf0e10cSrcweir                 m_xExportHandler.characters(str);
952cdf0e10cSrcweir         }
953cdf0e10cSrcweir         catch( com.sun.star.xml.sax.SAXException e)
954cdf0e10cSrcweir         {
955cdf0e10cSrcweir             e.printStackTrace();
956cdf0e10cSrcweir         }
957cdf0e10cSrcweir     }
958cdf0e10cSrcweir 
959cdf0e10cSrcweir     public void ignorableWhitespace(String str)
960cdf0e10cSrcweir     {
961cdf0e10cSrcweir     }
962cdf0e10cSrcweir 
963cdf0e10cSrcweir     public void processingInstruction(String aTarget, String aData)
964cdf0e10cSrcweir     {
965cdf0e10cSrcweir         try{
966cdf0e10cSrcweir             m_xExportHandler.processingInstruction(aTarget, aData);
967cdf0e10cSrcweir         }
968cdf0e10cSrcweir         catch( com.sun.star.xml.sax.SAXException e)
969cdf0e10cSrcweir         {
970cdf0e10cSrcweir             e.printStackTrace();
971cdf0e10cSrcweir         }
972cdf0e10cSrcweir     }
973cdf0e10cSrcweir 
974cdf0e10cSrcweir     public void setDocumentLocator (com.sun.star.xml.sax.XLocator xLocator )
975cdf0e10cSrcweir         throws com.sun.star.xml.sax.SAXException
976cdf0e10cSrcweir     {
977cdf0e10cSrcweir     }
978cdf0e10cSrcweir 
979cdf0e10cSrcweir 
980cdf0e10cSrcweir     /*
981cdf0e10cSrcweir      * XSignatureCreationResultListener
982cdf0e10cSrcweir      */
983cdf0e10cSrcweir     public void signatureCreated(int securityId, SecurityOperationStatus creationResult)
984cdf0e10cSrcweir     {
985cdf0e10cSrcweir         String message = new String();
986cdf0e10cSrcweir         message += "A Signature is created:";
987cdf0e10cSrcweir         message += "\nSecurity Id = "+securityId;
988cdf0e10cSrcweir         message += "\nCreation result = "+((creationResult==SecurityOperationStatus.OPERATION_SUCCEEDED)?"Succeed":"Fail");
989cdf0e10cSrcweir 
990cdf0e10cSrcweir         m_testTool.showMessage("Message from : SignatureCreator\n\n"+message+"\n ");
991cdf0e10cSrcweir     }
992cdf0e10cSrcweir 
993cdf0e10cSrcweir     /*
994cdf0e10cSrcweir      * XSignatureVerifyResultListener
995cdf0e10cSrcweir      */
996cdf0e10cSrcweir     public void signatureVerified(int securityId, SecurityOperationStatus verifyResult)
997cdf0e10cSrcweir     {
998cdf0e10cSrcweir         String message = new String();
999cdf0e10cSrcweir         message += "A Signature is verified:";
1000cdf0e10cSrcweir         message += "\nSecurity Id = "+securityId;
1001cdf0e10cSrcweir         message += "\nVerify result = "+((verifyResult==SecurityOperationStatus.OPERATION_SUCCEEDED)?"Succeed":"Fail");
1002cdf0e10cSrcweir 
1003cdf0e10cSrcweir         m_testTool.showMessage("Message from : SignatureVerifier\n\n"+message+"\n ");
1004cdf0e10cSrcweir     }
1005cdf0e10cSrcweir 
1006cdf0e10cSrcweir     /*
1007cdf0e10cSrcweir      * XEncryptionResultListener
1008cdf0e10cSrcweir      */
1009cdf0e10cSrcweir     public void encrypted(int securityId, SecurityOperationStatus encryptionResult)
1010cdf0e10cSrcweir     {
1011cdf0e10cSrcweir         String message = new String();
1012cdf0e10cSrcweir         message += "An EncryptedData is encrypted:";
1013cdf0e10cSrcweir         message += "\nSecurity Id = "+securityId;
1014cdf0e10cSrcweir         message += "\nEncrypt result = "+((encryptionResult==SecurityOperationStatus.OPERATION_SUCCEEDED)?"Succeed":"Fail");
1015cdf0e10cSrcweir 
1016cdf0e10cSrcweir         m_testTool.showMessage("Message from : Encryptor\n\n"+message+"\n ");
1017cdf0e10cSrcweir     }
1018cdf0e10cSrcweir 
1019cdf0e10cSrcweir     /*
1020cdf0e10cSrcweir      * XDecryptionResultListener methods
1021cdf0e10cSrcweir      */
1022cdf0e10cSrcweir     public void decrypted(int securityId, SecurityOperationStatus decryptionResult)
1023cdf0e10cSrcweir     {
1024cdf0e10cSrcweir         String message = new String();
1025cdf0e10cSrcweir         message += "An EncryptedData is decrypted:";
1026cdf0e10cSrcweir         message += "\nSecurity Id = "+securityId;
1027cdf0e10cSrcweir         message += "\nDecrypt result = "+((decryptionResult==SecurityOperationStatus.OPERATION_SUCCEEDED)?"Succeed":"Fail");
1028cdf0e10cSrcweir 
1029cdf0e10cSrcweir         m_testTool.showMessage("Message from : Decryptor\n\n"+message+"\n ");
1030cdf0e10cSrcweir     }
1031cdf0e10cSrcweir 
1032cdf0e10cSrcweir     /*
1033cdf0e10cSrcweir      * XSAXEventKeeperStatusChangeListener methods
1034cdf0e10cSrcweir      */
1035cdf0e10cSrcweir     public void blockingStatusChanged(boolean isBlocking)
1036cdf0e10cSrcweir     {
1037cdf0e10cSrcweir         m_testTool.showMessage("Message from : SAXEventKeeper\n\n"+
1038cdf0e10cSrcweir                     (isBlocking?"The SAX event stream is blocked.":"The SAX event stream is unblocked.")+
1039cdf0e10cSrcweir                     "\n ");
1040cdf0e10cSrcweir 
1041cdf0e10cSrcweir         this.m_bIsBlocking = isBlocking;
1042cdf0e10cSrcweir     }
1043cdf0e10cSrcweir 
1044cdf0e10cSrcweir     public void collectionStatusChanged(boolean isInsideCollectedElement)
1045cdf0e10cSrcweir     {
1046cdf0e10cSrcweir         m_testTool.showMessage("Message from : SAXEventKeeper\n\n"+
1047cdf0e10cSrcweir                     (isInsideCollectedElement?"Begin to buffer data ...":"End of data bufferring.")+
1048cdf0e10cSrcweir                     "\n ");
1049cdf0e10cSrcweir 
1050cdf0e10cSrcweir         /*
1051cdf0e10cSrcweir         this.m_bIsInsideCollectedElement = isInsideCollectedElement;
1052cdf0e10cSrcweir 
1053cdf0e10cSrcweir         if ( !m_bIsInsideCollectedElement && !m_bIsBlocking)
1054cdf0e10cSrcweir         {
1055cdf0e10cSrcweir             m_bSAXEventKeeperIncluded = false;
1056cdf0e10cSrcweir         }
1057cdf0e10cSrcweir         else
1058cdf0e10cSrcweir         {
1059cdf0e10cSrcweir             m_bSAXEventKeeperIncluded = true;
1060cdf0e10cSrcweir         }
1061cdf0e10cSrcweir         changeOutput();
1062cdf0e10cSrcweir         */
1063cdf0e10cSrcweir     }
1064cdf0e10cSrcweir 
1065cdf0e10cSrcweir     public void bufferStatusChanged(boolean isBufferEmpty)
1066cdf0e10cSrcweir     {
1067cdf0e10cSrcweir         m_testTool.showMessage("Message from : SAXEventKeeper\n\n"+
1068cdf0e10cSrcweir                     (isBufferEmpty?"All bufferred data are released, the SAXEventKeeper is destroyed.":"buffer data appears.")+
1069cdf0e10cSrcweir                     "\n ");
1070cdf0e10cSrcweir         /*
1071cdf0e10cSrcweir         if (isBufferEmpty)
1072cdf0e10cSrcweir         {
1073cdf0e10cSrcweir             m_xXMLDocumentWrapper = null;
1074cdf0e10cSrcweir             m_xSAXEventKeeper = null;
1075cdf0e10cSrcweir             m_bSAXEventKeeperIncluded = false;
1076cdf0e10cSrcweir             changeOutput();
1077cdf0e10cSrcweir         }
1078cdf0e10cSrcweir         */
1079cdf0e10cSrcweir     }
1080cdf0e10cSrcweir }
1081cdf0e10cSrcweir 
1082