xref: /trunk/main/xmlsecurity/tools/uno/EncryptionEntity.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package com.sun.star.xml.security.uno;
29 
30 /* uno classes */
31 import com.sun.star.uno.UnoRuntime;
32 import com.sun.star.lang.XMultiComponentFactory;
33 import com.sun.star.lang.XInitialization;
34 import com.sun.star.uno.XComponentContext;
35 
36 import com.sun.star.xml.crypto.*;
37 import com.sun.star.xml.crypto.sax.*;
38 
39 /*
40  * this class maintains the data for an encryption operation.
41  */
42 class EncryptionEntity extends SecurityEntity
43 {
44     private int m_nEncryptionElementCollectorId;
45 
46     EncryptionEntity(
47         XSecuritySAXEventKeeper xSAXEventKeeper,
48         boolean isExporting,
49         Object resultListener,
50         XXMLSecurityContext xXMLSecurityContext,
51         XXMLSignature xXMLSignature,
52         XXMLEncryption xXMLEncryption,
53         XMultiComponentFactory xRemoteServiceManager,
54         XComponentContext xRemoteContext)
55     {
56         super(xSAXEventKeeper, xXMLSecurityContext, xXMLSignature,
57              xXMLEncryption, xRemoteServiceManager, xRemoteContext);
58 
59         m_nEncryptionElementCollectorId = m_xSAXEventKeeper.addSecurityElementCollector(
60             ElementMarkPriority.AFTERMODIFY,
61             true);
62 
63         m_xSAXEventKeeper.setSecurityId(m_nEncryptionElementCollectorId, m_nSecurityId);
64 
65         if (isExporting)
66         {
67             try
68             {
69                 /*
70                  * creates a Encryptor.
71                  */
72                 Object encryptor = m_xRemoteServiceManager.createInstanceWithContext(
73                     TestTool.ENCRYPTOR_COMPONENT, m_xRemoteContext);
74 
75                 m_xReferenceResolvedListener =
76                     (XReferenceResolvedListener)UnoRuntime.queryInterface(
77                         XReferenceResolvedListener.class, encryptor);
78 
79                 /*
80                  * initializes the Encryptor.
81                  */
82                 XInitialization xInitialization =
83                     (XInitialization)UnoRuntime.queryInterface(
84                         XInitialization.class, m_xReferenceResolvedListener);
85                 Object args[]=new Object[5];
86                 args[0] = new Integer(m_nSecurityId).toString();
87                 args[1] = m_xSAXEventKeeper;
88                 args[2] = new Integer(m_nEncryptionElementCollectorId).toString();
89                 args[3] = m_xXMLSecurityContext.getSecurityEnvironment();
90                 args[4] = m_xXMLEncryption;
91                 xInitialization.initialize(args);
92 
93                 /*
94                  * sets encryption result listener.
95                  */
96                 XEncryptionResultBroadcaster m_xEncryptionResultBroadcaster =
97                     (XEncryptionResultBroadcaster)UnoRuntime.queryInterface(
98                         XEncryptionResultBroadcaster.class, m_xReferenceResolvedListener);
99                 m_xEncryptionResultBroadcaster.addEncryptionResultListener(
100                     (XEncryptionResultListener)UnoRuntime.queryInterface(
101                         XEncryptionResultListener.class, resultListener));
102             }
103             catch( com.sun.star.uno.Exception e)
104             {
105                 e.printStackTrace();
106             }
107         }
108         else
109         {
110             try{
111                 /*
112                  * creates a Decryptor.
113                  */
114                 Object decryptor = m_xRemoteServiceManager.createInstanceWithContext(
115                     TestTool.DECRYPTOR_COMPONENT, m_xRemoteContext);
116 
117                 m_xReferenceResolvedListener =
118                     (XReferenceResolvedListener)UnoRuntime.queryInterface(
119                         XReferenceResolvedListener.class, decryptor);
120 
121                 /*
122                  * initializes the Decryptor.
123                  */
124                 XInitialization xInitialization = (XInitialization)UnoRuntime.queryInterface(XInitialization.class, m_xReferenceResolvedListener);
125                 Object args[]=new Object[5];
126                 args[0] = new Integer(m_nSecurityId).toString();
127                 args[1] = m_xSAXEventKeeper;
128                 args[2] = new Integer(m_nEncryptionElementCollectorId).toString();
129                 args[3] = m_xXMLSecurityContext;
130                 args[4] = m_xXMLEncryption;
131                 xInitialization.initialize(args);
132 
133                 /*
134                  * sets decryption result listener.
135                  */
136                 XDecryptionResultBroadcaster m_xDecryptionResultBroadcaster =
137                     (XDecryptionResultBroadcaster)UnoRuntime.queryInterface(
138                         XDecryptionResultBroadcaster.class, m_xReferenceResolvedListener);
139                 m_xDecryptionResultBroadcaster.addDecryptionResultListener(
140                     (XDecryptionResultListener)UnoRuntime.queryInterface(
141                         XDecryptionResultListener.class, resultListener));
142             }
143             catch( com.sun.star.uno.Exception e)
144             {
145                 e.printStackTrace();
146             }
147         }
148 
149         /*
150          * creates a Blocker.
151          */
152         int blockerId = m_xSAXEventKeeper.addBlocker();
153         m_xSAXEventKeeper.setSecurityId(blockerId, m_nSecurityId);
154 
155         try
156         {
157             XBlockerMonitor xBlockerMonitor = (XBlockerMonitor)UnoRuntime.queryInterface(
158                 XBlockerMonitor.class, m_xReferenceResolvedListener);
159             xBlockerMonitor.setBlockerId(blockerId);
160         }
161         catch( com.sun.star.uno.Exception e)
162         {
163             e.printStackTrace();
164         }
165 
166         /*
167          * configures the resolve listener for the encryption template.
168          */
169         XReferenceResolvedBroadcaster xReferenceResolvedBroadcaster =
170             (XReferenceResolvedBroadcaster)UnoRuntime.queryInterface(
171                 XReferenceResolvedBroadcaster.class, m_xSAXEventKeeper);
172         xReferenceResolvedBroadcaster.addReferenceResolvedListener(m_nEncryptionElementCollectorId, m_xReferenceResolvedListener);
173     }
174 
175     /*
176      * add the reference to this encryption.
177      *
178      * 1. askes the SAXEventKeeper to add a ElementCollector to for the new
179      * referenced element;
180      * 2. configures this ElementCollector's security id;
181      * 3. tells the SAXEventKeeper which listener will receive the reference
182      * resolved notification.
183      * 4. notifies the SignatureCollector about the reference id.
184      */
185     protected boolean setReference(boolean isExporting)
186     {
187         boolean rc = false;
188 
189         int referenceId = m_xSAXEventKeeper.addSecurityElementCollector(
190             isExporting?
191             (ElementMarkPriority.AFTERMODIFY):(ElementMarkPriority.BEFOREMODIFY),
192             true);
193 
194         m_xSAXEventKeeper.setSecurityId(referenceId, m_nSecurityId);
195 
196         XReferenceResolvedBroadcaster xReferenceResolvedBroadcaster =
197             (XReferenceResolvedBroadcaster)UnoRuntime.queryInterface(
198                 XReferenceResolvedBroadcaster.class, m_xSAXEventKeeper);
199         xReferenceResolvedBroadcaster.addReferenceResolvedListener(
200             referenceId, m_xReferenceResolvedListener);
201 
202         try
203         {
204             XReferenceCollector xReferenceCollector =
205                 (XReferenceCollector)UnoRuntime.queryInterface(
206                     XReferenceCollector.class, m_xReferenceResolvedListener);
207             xReferenceCollector.setReferenceId(referenceId);
208         }
209         catch( com.sun.star.uno.Exception e)
210         {
211             e.printStackTrace();
212             rc = false;
213         }
214 
215         return rc;
216     }
217 }
218 
219