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