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 /* uno classes */
27 import com.sun.star.uno.UnoRuntime;
28 import com.sun.star.lang.XMultiComponentFactory;
29 import com.sun.star.lang.XInitialization;
30 import com.sun.star.uno.XComponentContext;
31 
32 import com.sun.star.xml.crypto.*;
33 import com.sun.star.xml.crypto.sax.*;
34 
35 /*
36  * this class maintains the data for an encryption operation.
37  */
38 class EncryptionEntity extends SecurityEntity
39 {
40 	private int m_nEncryptionElementCollectorId;
41 
EncryptionEntity( XSecuritySAXEventKeeper xSAXEventKeeper, boolean isExporting, Object resultListener, XXMLSecurityContext xXMLSecurityContext, XXMLSignature xXMLSignature, XXMLEncryption xXMLEncryption, XMultiComponentFactory xRemoteServiceManager, XComponentContext xRemoteContext)42 	EncryptionEntity(
43 		XSecuritySAXEventKeeper xSAXEventKeeper,
44 		boolean isExporting,
45 		Object resultListener,
46 		XXMLSecurityContext xXMLSecurityContext,
47 		XXMLSignature xXMLSignature,
48 		XXMLEncryption xXMLEncryption,
49 		XMultiComponentFactory xRemoteServiceManager,
50 		XComponentContext xRemoteContext)
51 	{
52 		super(xSAXEventKeeper, xXMLSecurityContext, xXMLSignature,
53 			 xXMLEncryption, xRemoteServiceManager, xRemoteContext);
54 
55 		m_nEncryptionElementCollectorId = m_xSAXEventKeeper.addSecurityElementCollector(
56 			ElementMarkPriority.AFTERMODIFY,
57 			true);
58 
59 		m_xSAXEventKeeper.setSecurityId(m_nEncryptionElementCollectorId, m_nSecurityId);
60 
61 		if (isExporting)
62 		{
63 			try
64 			{
65 				/*
66 				 * creates a Encryptor.
67 				 */
68 				Object encryptor = m_xRemoteServiceManager.createInstanceWithContext(
69 					TestTool.ENCRYPTOR_COMPONENT, m_xRemoteContext);
70 
71 				m_xReferenceResolvedListener =
72 					(XReferenceResolvedListener)UnoRuntime.queryInterface(
73 						XReferenceResolvedListener.class, encryptor);
74 
75 				/*
76 				 * initializes the Encryptor.
77 				 */
78 				XInitialization xInitialization =
79 					(XInitialization)UnoRuntime.queryInterface(
80 						XInitialization.class, m_xReferenceResolvedListener);
81 				Object args[]=new Object[5];
82 				args[0] = new Integer(m_nSecurityId).toString();
83 				args[1] = m_xSAXEventKeeper;
84 				args[2] = new Integer(m_nEncryptionElementCollectorId).toString();
85 				args[3] = m_xXMLSecurityContext.getSecurityEnvironment();
86 				args[4] = m_xXMLEncryption;
87 				xInitialization.initialize(args);
88 
89 				/*
90 				 * sets encryption result listener.
91 				 */
92 				XEncryptionResultBroadcaster m_xEncryptionResultBroadcaster =
93 					(XEncryptionResultBroadcaster)UnoRuntime.queryInterface(
94 						XEncryptionResultBroadcaster.class, m_xReferenceResolvedListener);
95 				m_xEncryptionResultBroadcaster.addEncryptionResultListener(
96 					(XEncryptionResultListener)UnoRuntime.queryInterface(
97 						XEncryptionResultListener.class, resultListener));
98 			}
99 			catch( com.sun.star.uno.Exception e)
100 			{
101 				e.printStackTrace();
102 			}
103 		}
104 		else
105 		{
106 			try{
107 				/*
108 				 * creates a Decryptor.
109 				 */
110 				Object decryptor = m_xRemoteServiceManager.createInstanceWithContext(
111 					TestTool.DECRYPTOR_COMPONENT, m_xRemoteContext);
112 
113 				m_xReferenceResolvedListener =
114 					(XReferenceResolvedListener)UnoRuntime.queryInterface(
115 						XReferenceResolvedListener.class, decryptor);
116 
117 				/*
118 				 * initializes the Decryptor.
119 				 */
120 				XInitialization xInitialization = (XInitialization)UnoRuntime.queryInterface(XInitialization.class, m_xReferenceResolvedListener);
121 				Object args[]=new Object[5];
122 				args[0] = new Integer(m_nSecurityId).toString();
123 				args[1] = m_xSAXEventKeeper;
124 				args[2] = new Integer(m_nEncryptionElementCollectorId).toString();
125 				args[3] = m_xXMLSecurityContext;
126 				args[4] = m_xXMLEncryption;
127 				xInitialization.initialize(args);
128 
129 				/*
130 				 * sets decryption result listener.
131 				 */
132 				XDecryptionResultBroadcaster m_xDecryptionResultBroadcaster =
133 					(XDecryptionResultBroadcaster)UnoRuntime.queryInterface(
134 						XDecryptionResultBroadcaster.class, m_xReferenceResolvedListener);
135 				m_xDecryptionResultBroadcaster.addDecryptionResultListener(
136 					(XDecryptionResultListener)UnoRuntime.queryInterface(
137 						XDecryptionResultListener.class, resultListener));
138 			}
139 			catch( com.sun.star.uno.Exception e)
140 			{
141 				e.printStackTrace();
142 			}
143 		}
144 
145 		/*
146 		 * creates a Blocker.
147 		 */
148 		int blockerId = m_xSAXEventKeeper.addBlocker();
149 		m_xSAXEventKeeper.setSecurityId(blockerId, m_nSecurityId);
150 
151 		try
152 		{
153 			XBlockerMonitor xBlockerMonitor = (XBlockerMonitor)UnoRuntime.queryInterface(
154 				XBlockerMonitor.class, m_xReferenceResolvedListener);
155 			xBlockerMonitor.setBlockerId(blockerId);
156 		}
157 		catch( com.sun.star.uno.Exception e)
158 		{
159 			e.printStackTrace();
160 		}
161 
162 		/*
163 		 * configures the resolve listener for the encryption template.
164 		 */
165 		XReferenceResolvedBroadcaster xReferenceResolvedBroadcaster =
166 			(XReferenceResolvedBroadcaster)UnoRuntime.queryInterface(
167 				XReferenceResolvedBroadcaster.class, m_xSAXEventKeeper);
168 		xReferenceResolvedBroadcaster.addReferenceResolvedListener(m_nEncryptionElementCollectorId, m_xReferenceResolvedListener);
169 	}
170 
171 	/*
172 	 * add the reference to this encryption.
173 	 *
174 	 * 1. askes the SAXEventKeeper to add a ElementCollector to for the new
175 	 * referenced element;
176 	 * 2. configures this ElementCollector's security id;
177 	 * 3. tells the SAXEventKeeper which listener will receive the reference
178 	 * resolved notification.
179 	 * 4. notifies the SignatureCollector about the reference id.
180 	 */
setReference(boolean isExporting)181 	protected boolean setReference(boolean isExporting)
182 	{
183 		boolean rc = false;
184 
185 		int referenceId = m_xSAXEventKeeper.addSecurityElementCollector(
186 			isExporting?
187 			(ElementMarkPriority.AFTERMODIFY):(ElementMarkPriority.BEFOREMODIFY),
188 			true);
189 
190 		m_xSAXEventKeeper.setSecurityId(referenceId, m_nSecurityId);
191 
192 		XReferenceResolvedBroadcaster xReferenceResolvedBroadcaster =
193 			(XReferenceResolvedBroadcaster)UnoRuntime.queryInterface(
194 				XReferenceResolvedBroadcaster.class, m_xSAXEventKeeper);
195 		xReferenceResolvedBroadcaster.addReferenceResolvedListener(
196 			referenceId, m_xReferenceResolvedListener);
197 
198 		try
199 		{
200 			XReferenceCollector xReferenceCollector =
201 				(XReferenceCollector)UnoRuntime.queryInterface(
202 					XReferenceCollector.class, m_xReferenceResolvedListener);
203 			xReferenceCollector.setReferenceId(referenceId);
204 		}
205 		catch( com.sun.star.uno.Exception e)
206 		{
207 			e.printStackTrace();
208 			rc = false;
209 		}
210 
211 		return rc;
212 	}
213 }
214 
215