xref: /trunk/main/xmlsecurity/tools/uno/SecurityEntity.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 /* uno classes */
27cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
28cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
29cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
30cdf0e10cSrcweir 
31cdf0e10cSrcweir import com.sun.star.xml.crypto.*;
32cdf0e10cSrcweir import com.sun.star.xml.crypto.sax.*;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir /*
35cdf0e10cSrcweir  * this class maintains the data for a security operation.
36cdf0e10cSrcweir  */
37cdf0e10cSrcweir class SecurityEntity
38cdf0e10cSrcweir {
39cdf0e10cSrcweir     /*
40cdf0e10cSrcweir      * the security id, which identifies this security entity
41cdf0e10cSrcweir      * uniquely.
42cdf0e10cSrcweir      */
43cdf0e10cSrcweir     private static int m_nNextSecurityId = 1;
44cdf0e10cSrcweir     protected int m_nSecurityId;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir     /*
47cdf0e10cSrcweir      * xml security related components
48cdf0e10cSrcweir      */
49cdf0e10cSrcweir     protected XXMLSecurityContext        m_xXMLSecurityContext;
50cdf0e10cSrcweir     protected XXMLSignature              m_xXMLSignature;
51cdf0e10cSrcweir     protected XXMLEncryption             m_xXMLEncryption;
52cdf0e10cSrcweir     protected XMultiComponentFactory     m_xRemoteServiceManager;
53cdf0e10cSrcweir     protected XComponentContext          m_xRemoteContext;
54cdf0e10cSrcweir     protected XReferenceResolvedListener m_xReferenceResolvedListener;
55cdf0e10cSrcweir     protected XSecuritySAXEventKeeper    m_xSAXEventKeeper;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     /*
58cdf0e10cSrcweir      * the uri of the key material of this security entity
59cdf0e10cSrcweir      */
60cdf0e10cSrcweir     private String m_keyURI;
61cdf0e10cSrcweir 
SecurityEntity( XSecuritySAXEventKeeper xSAXEventKeeper, XXMLSecurityContext xXMLSecurityContext, XXMLSignature xXMLSignature, XXMLEncryption xXMLEncryption, XMultiComponentFactory xRemoteServiceManager, XComponentContext xRemoteContext)62cdf0e10cSrcweir     SecurityEntity(
63cdf0e10cSrcweir         XSecuritySAXEventKeeper xSAXEventKeeper,
64cdf0e10cSrcweir         XXMLSecurityContext xXMLSecurityContext,
65cdf0e10cSrcweir         XXMLSignature xXMLSignature,
66cdf0e10cSrcweir         XXMLEncryption xXMLEncryption,
67cdf0e10cSrcweir         XMultiComponentFactory xRemoteServiceManager,
68cdf0e10cSrcweir         XComponentContext xRemoteContext)
69cdf0e10cSrcweir     {
70cdf0e10cSrcweir         m_xSAXEventKeeper = xSAXEventKeeper;
71cdf0e10cSrcweir         m_xXMLSecurityContext = xXMLSecurityContext;
72cdf0e10cSrcweir         m_xXMLSignature = xXMLSignature;
73cdf0e10cSrcweir         m_xXMLEncryption = xXMLEncryption;
74cdf0e10cSrcweir         m_xRemoteServiceManager = xRemoteServiceManager;
75cdf0e10cSrcweir         m_xRemoteContext = xRemoteContext;
76cdf0e10cSrcweir 
77cdf0e10cSrcweir         m_nSecurityId = getNextSecurityId();
78cdf0e10cSrcweir         m_keyURI = null;
79cdf0e10cSrcweir     }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir /**************************************************************************************
82cdf0e10cSrcweir  * private methods
83cdf0e10cSrcweir  **************************************************************************************/
84cdf0e10cSrcweir 
85cdf0e10cSrcweir     /*
86cdf0e10cSrcweir      * generates a new security id.
87cdf0e10cSrcweir      */
getNextSecurityId()88cdf0e10cSrcweir     private static int getNextSecurityId()
89cdf0e10cSrcweir     {
90cdf0e10cSrcweir         int id = m_nNextSecurityId++;
91cdf0e10cSrcweir         return id;
92cdf0e10cSrcweir     }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir /**************************************************************************************
95cdf0e10cSrcweir  * protected methods
96cdf0e10cSrcweir  **************************************************************************************/
97cdf0e10cSrcweir 
98cdf0e10cSrcweir     /*
99cdf0e10cSrcweir      * notifies the key collector about the key id, this key id
100cdf0e10cSrcweir      * is used to ask the SAXEventKeeper to release the bufferred
101cdf0e10cSrcweir      * key element.
102cdf0e10cSrcweir      * when the id is 0, that means there is no independant key
103cdf0e10cSrcweir      * element needed.
104cdf0e10cSrcweir      */
setKeyId(int id)105cdf0e10cSrcweir     protected void setKeyId(int id)
106cdf0e10cSrcweir     {
107cdf0e10cSrcweir         try
108cdf0e10cSrcweir         {
109cdf0e10cSrcweir             XKeyCollector xKeyCollector =
110cdf0e10cSrcweir                 (XKeyCollector)UnoRuntime.queryInterface(
111cdf0e10cSrcweir                     XKeyCollector.class, m_xReferenceResolvedListener);
112cdf0e10cSrcweir             xKeyCollector.setKeyId(id);
113cdf0e10cSrcweir         }
114cdf0e10cSrcweir         catch( com.sun.star.uno.Exception e)
115cdf0e10cSrcweir         {
116cdf0e10cSrcweir             e.printStackTrace();
117cdf0e10cSrcweir         }
118cdf0e10cSrcweir     }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     /*
121cdf0e10cSrcweir      * set the key uri, which will be the value of the id attribute
122cdf0e10cSrcweir      * of the key element
123cdf0e10cSrcweir      */
setKeyURI(String uri)124cdf0e10cSrcweir     protected void setKeyURI(String uri)
125cdf0e10cSrcweir     {
126cdf0e10cSrcweir         m_keyURI = new String(uri);
127cdf0e10cSrcweir     }
128cdf0e10cSrcweir 
getReferenceListener()129cdf0e10cSrcweir     protected XReferenceResolvedListener getReferenceListener()
130cdf0e10cSrcweir     {
131cdf0e10cSrcweir         return m_xReferenceResolvedListener;
132cdf0e10cSrcweir     }
133cdf0e10cSrcweir 
getSecurityId()134cdf0e10cSrcweir     protected int getSecurityId()
135cdf0e10cSrcweir     {
136cdf0e10cSrcweir         return m_nSecurityId;
137cdf0e10cSrcweir     }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     /*
140cdf0e10cSrcweir      * configures the key material to the security entity.
141cdf0e10cSrcweir      *
142cdf0e10cSrcweir      * if the uri is the key, then:
143cdf0e10cSrcweir      * 1. askes the SAXEventKeeper to add a ElementCollector to the key
144cdf0e10cSrcweir      * element;
145cdf0e10cSrcweir      * 2. notifies the key collector;
146cdf0e10cSrcweir      * 3. configures this ElementCollector's security id;
147cdf0e10cSrcweir      * 4. tells the SAXEventKeeper which listener will receive the reference
148cdf0e10cSrcweir      * resolved notification.
149cdf0e10cSrcweir      */
setKey(String uri, boolean isExporting)150cdf0e10cSrcweir     protected boolean setKey(String uri, boolean isExporting)
151cdf0e10cSrcweir     {
152cdf0e10cSrcweir         boolean rc = false;
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         if (m_keyURI != null &&
155cdf0e10cSrcweir             m_keyURI.equals(uri))
156cdf0e10cSrcweir         {
157cdf0e10cSrcweir             int referenceId = m_xSAXEventKeeper.addSecurityElementCollector(
158cdf0e10cSrcweir                 isExporting?
159cdf0e10cSrcweir                 (ElementMarkPriority.BEFOREMODIFY):(ElementMarkPriority.AFTERMODIFY),
160cdf0e10cSrcweir                 false );
161cdf0e10cSrcweir 
162cdf0e10cSrcweir             setKeyId(referenceId);
163cdf0e10cSrcweir             m_xSAXEventKeeper.setSecurityId(referenceId, m_nSecurityId);
164cdf0e10cSrcweir 
165cdf0e10cSrcweir             XReferenceResolvedBroadcaster xReferenceResolvedBroadcaster =
166cdf0e10cSrcweir                 (XReferenceResolvedBroadcaster)UnoRuntime.queryInterface(
167cdf0e10cSrcweir                     XReferenceResolvedBroadcaster.class, m_xSAXEventKeeper);
168cdf0e10cSrcweir 
169cdf0e10cSrcweir             xReferenceResolvedBroadcaster.addReferenceResolvedListener(referenceId, m_xReferenceResolvedListener);
170cdf0e10cSrcweir 
171cdf0e10cSrcweir             rc = true;
172cdf0e10cSrcweir         }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir         return rc;
175cdf0e10cSrcweir     }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir     /*
178cdf0e10cSrcweir      * ends this misstion, asks the security engine to clear up all
179cdf0e10cSrcweir      * resources.
180cdf0e10cSrcweir      */
endMission()181cdf0e10cSrcweir     protected boolean endMission()
182cdf0e10cSrcweir     {
183cdf0e10cSrcweir         XMissionTaker xMissionTaker =
184cdf0e10cSrcweir             (XMissionTaker)UnoRuntime.queryInterface(
185cdf0e10cSrcweir                 XMissionTaker.class, m_xReferenceResolvedListener);
186cdf0e10cSrcweir 
187cdf0e10cSrcweir         boolean rc = xMissionTaker.endMission();
188cdf0e10cSrcweir 
189cdf0e10cSrcweir         m_xXMLSecurityContext = null;
190cdf0e10cSrcweir         m_xXMLSignature = null;
191cdf0e10cSrcweir         m_xXMLEncryption = null;
192cdf0e10cSrcweir         m_xReferenceResolvedListener = null;
193cdf0e10cSrcweir         m_xSAXEventKeeper = null;
194cdf0e10cSrcweir 
195cdf0e10cSrcweir         return rc;
196cdf0e10cSrcweir     }
197cdf0e10cSrcweir }
198