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