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