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