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