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