1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package com.sun.star.xml.security.uno;
29 
30 import java.util.Vector;
31 
32 /* uno classes */
33 import com.sun.star.uno.UnoRuntime;
34 import com.sun.star.lang.XMultiComponentFactory;
35 import com.sun.star.lang.XInitialization;
36 import com.sun.star.uno.XComponentContext;
37 
38 import com.sun.star.xml.crypto.*;
39 import com.sun.star.xml.crypto.sax.*;
40 
41 /*
42  * this class maintains the data for a signature operation.
43  */
44 class SignatureEntity extends SecurityEntity
45 {
46 	private Vector m_vReferenceIds;
47 	private int    m_nSignatureElementCollectorId;
48 
49 	SignatureEntity(
50 		XSecuritySAXEventKeeper xSAXEventKeeper,
51 		boolean isExporting,
52 		Object resultListener,
53 		XXMLSecurityContext xXMLSecurityContext,
54 		XXMLSignature xXMLSignature,
55 		XXMLEncryption xXMLEncryption,
56 		XMultiComponentFactory xRemoteServiceManager,
57 		XComponentContext xRemoteContext)
58 	{
59 		super(xSAXEventKeeper, xXMLSecurityContext, xXMLSignature,
60 			xXMLEncryption, xRemoteServiceManager, xRemoteContext);
61 
62 		m_vReferenceIds = new Vector();
63 
64 		if (isExporting)
65 		{
66 			m_nSignatureElementCollectorId = m_xSAXEventKeeper.addSecurityElementCollector(
67 				ElementMarkPriority.AFTERMODIFY,
68 				true);
69 
70 			m_xSAXEventKeeper.setSecurityId(m_nSignatureElementCollectorId, m_nSecurityId);
71 
72 			try
73 			{
74 				/*
75 				 * creates a SignatureCreator.
76 				 */
77 				Object signatureCreator = m_xRemoteServiceManager.createInstanceWithContext(
78 					TestTool.SIGNATURECREATOR_COMPONENT, m_xRemoteContext);
79 
80 				m_xReferenceResolvedListener =
81 					(XReferenceResolvedListener)UnoRuntime.queryInterface(
82 						XReferenceResolvedListener.class, signatureCreator);
83 
84 	                        /*
85 	                         * initializes the SignatureCreator.
86 	                         */
87 				XInitialization xInitialization =
88 					(XInitialization)UnoRuntime.queryInterface(
89 						XInitialization.class, m_xReferenceResolvedListener);
90 
91 				Object args[]=new Object[5];
92 				args[0] = new Integer(m_nSecurityId).toString();
93 				args[1] = m_xSAXEventKeeper;
94 				args[2] = new Integer(m_nSignatureElementCollectorId).toString();
95 				args[3] = m_xXMLSecurityContext.getSecurityEnvironment();
96 				args[4] = m_xXMLSignature;
97 				xInitialization.initialize(args);
98 
99 				/*
100 				 * creates a Blocker.
101 				 */
102 				int blockerId = m_xSAXEventKeeper.addBlocker();
103 				m_xSAXEventKeeper.setSecurityId(blockerId, m_nSecurityId);
104 
105 				XBlockerMonitor xBlockerMonitor = (XBlockerMonitor)UnoRuntime.queryInterface(
106 					XBlockerMonitor.class, m_xReferenceResolvedListener);
107 				xBlockerMonitor.setBlockerId(blockerId);
108 
109 				/*
110 				 * sets signature creation result listener.
111 				 */
112 				XSignatureCreationResultBroadcaster xSignatureCreationResultBroadcaster =
113 					(XSignatureCreationResultBroadcaster)UnoRuntime.queryInterface(
114 						XSignatureCreationResultBroadcaster.class, m_xReferenceResolvedListener);
115 				xSignatureCreationResultBroadcaster.addSignatureCreationResultListener(
116 					(XSignatureCreationResultListener)UnoRuntime.queryInterface(
117 						XSignatureCreationResultListener.class, resultListener));
118 			}
119 			catch( com.sun.star.uno.Exception e)
120 			{
121 				e.printStackTrace();
122 			}
123 
124 		}
125 		else
126 		{
127 			m_nSignatureElementCollectorId = m_xSAXEventKeeper.addSecurityElementCollector(
128 				ElementMarkPriority.BEFOREMODIFY, false);
129 
130 			m_xSAXEventKeeper.setSecurityId(m_nSignatureElementCollectorId, m_nSecurityId);
131 
132 			try
133 			{
134 				/*
135 				 * creates a SignatureVerifier.
136 				 */
137 				Object signatureVerifier = m_xRemoteServiceManager.createInstanceWithContext(
138 					TestTool.SIGNATUREVERIFIER_COMPONENT, m_xRemoteContext);
139 
140 				m_xReferenceResolvedListener =
141 					(XReferenceResolvedListener)UnoRuntime.queryInterface(
142 						XReferenceResolvedListener.class, signatureVerifier);
143 
144 	                        /*
145 	                         * initializes the SignatureVerifier.
146 	                         */
147 				XInitialization xInitialization =
148 					(XInitialization)UnoRuntime.queryInterface(
149 						XInitialization.class, m_xReferenceResolvedListener);
150 				Object args[]=new Object[5];
151 				args[0] = new Integer(m_nSecurityId).toString();
152 				args[1] = m_xSAXEventKeeper;
153 				args[2] = new Integer(m_nSignatureElementCollectorId).toString();
154 				args[3] = m_xXMLSecurityContext;
155 				args[4] = m_xXMLSignature;
156 				xInitialization.initialize(args);
157 
158 				/*
159 				 * sets signature verify result listener.
160 				 */
161 				XSignatureVerifyResultBroadcaster xSignatureVerifyResultBroadcaster =
162 					(XSignatureVerifyResultBroadcaster)UnoRuntime.queryInterface(
163 						XSignatureVerifyResultBroadcaster.class, m_xReferenceResolvedListener);
164 				xSignatureVerifyResultBroadcaster.addSignatureVerifyResultListener(
165 					(XSignatureVerifyResultListener)UnoRuntime.queryInterface(
166 						XSignatureVerifyResultListener.class, resultListener));
167 			}
168 			catch( com.sun.star.uno.Exception e)
169 			{
170 				e.printStackTrace();
171 			}
172 		}
173 
174 		/*
175 		 * configures the resolve listener for the signature template.
176 		 */
177 		XReferenceResolvedBroadcaster xReferenceResolvedBroadcaster =
178 			(XReferenceResolvedBroadcaster)UnoRuntime.queryInterface(
179 				XReferenceResolvedBroadcaster.class, m_xSAXEventKeeper);
180 		xReferenceResolvedBroadcaster.addReferenceResolvedListener(
181 			m_nSignatureElementCollectorId, m_xReferenceResolvedListener);
182 	}
183 
184 /**************************************************************************************
185  * private methods
186  **************************************************************************************/
187 
188 	/*
189 	 * checks whether this signature has a reference with
190 	 * the particular id.
191 	 */
192 	private boolean hasReference(String id)
193 	{
194 		boolean rc = false;
195 
196 		int length = m_vReferenceIds.size();
197 		for (int i=0; i<length; ++i)
198 		{
199 			if (id.equals((String)m_vReferenceIds.elementAt(i)))
200 			{
201 				rc = true;
202 				break;
203 			}
204 		}
205 
206 		return rc;
207 	}
208 
209 
210 /**************************************************************************************
211  * protected methods
212  **************************************************************************************/
213 
214 	/*
215 	 * adds a new reference id.
216 	 */
217 	protected void addReferenceId(String referenceId)
218 	{
219 		m_vReferenceIds.add(referenceId);
220 	}
221 
222 	/*
223 	 * notifies how many reference in this signature.
224 	 */
225 	protected void setReferenceNumber()
226 	{
227 		try
228 		{
229 			XReferenceCollector xReferenceCollector =
230 				(XReferenceCollector)UnoRuntime.queryInterface(
231 					XReferenceCollector.class, m_xReferenceResolvedListener);
232 			xReferenceCollector.setReferenceCount(m_vReferenceIds.size());
233 		}
234 		catch( com.sun.star.uno.Exception e)
235 		{
236 			e.printStackTrace();
237 		}
238 	}
239 
240 	/*
241 	 * tries to add a reference to this signature.
242 	 *
243 	 * If the specific id belongs to this signature's references, then:
244 	 * 1. askes the SAXEventKeeper to add a ElementCollector to for the new
245 	 * referenced element;
246 	 * 2. configures this ElementCollector's security id;
247 	 * 3. tells the SAXEventKeeper which listener will receive the reference
248 	 * resolved notification.
249 	 * 4. notifies the SignatureCollector about the reference id.
250 	 */
251 	protected boolean setReference(String id, boolean isExporting)
252 	{
253 		boolean rc = false;
254 
255 		if (hasReference(id))
256 		{
257 			int referenceId = m_xSAXEventKeeper.addSecurityElementCollector(
258 				isExporting?
259 				(ElementMarkPriority.AFTERMODIFY):(ElementMarkPriority.BEFOREMODIFY),
260 				false );
261 
262 			m_xSAXEventKeeper.setSecurityId(referenceId, m_nSecurityId);
263 
264 			XReferenceResolvedBroadcaster xReferenceResolvedBroadcaster =
265 				(XReferenceResolvedBroadcaster)UnoRuntime.queryInterface(
266 					XReferenceResolvedBroadcaster.class, m_xSAXEventKeeper);
267 			xReferenceResolvedBroadcaster.addReferenceResolvedListener(
268 				referenceId, m_xReferenceResolvedListener);
269 
270 			try
271 			{
272 				XReferenceCollector xReferenceCollector =
273 					(XReferenceCollector)UnoRuntime.queryInterface(
274 						XReferenceCollector.class, m_xReferenceResolvedListener);
275 				xReferenceCollector.setReferenceId(referenceId);
276 			}
277 			catch( com.sun.star.uno.Exception e)
278 			{
279 				e.printStackTrace();
280 			}
281 
282 			rc = true;
283 		}
284 
285 		return rc;
286 	}
287 }
288 
289