1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package com.sun.star.xml.security.uno;
25 
26 /* uno classes */
27 import com.sun.star.uno.UnoRuntime;
28 import com.sun.star.lang.XMultiComponentFactory;
29 import com.sun.star.uno.XComponentContext;
30 
31 import com.sun.star.xml.crypto.*;
32 import com.sun.star.xml.crypto.sax.*;
33 
34 /*
35  * this class maintains the data for a security operation.
36  */
37 class SecurityEntity
38 {
39 	/*
40 	 * the security id, which identifies this security entity
41 	 * uniquely.
42 	 */
43 	private static int m_nNextSecurityId = 1;
44 	protected int m_nSecurityId;
45 
46 	/*
47 	 * xml security related components
48 	 */
49 	protected XXMLSecurityContext        m_xXMLSecurityContext;
50 	protected XXMLSignature              m_xXMLSignature;
51 	protected XXMLEncryption             m_xXMLEncryption;
52 	protected XMultiComponentFactory     m_xRemoteServiceManager;
53 	protected XComponentContext          m_xRemoteContext;
54 	protected XReferenceResolvedListener m_xReferenceResolvedListener;
55 	protected XSecuritySAXEventKeeper    m_xSAXEventKeeper;
56 
57 	/*
58 	 * the uri of the key material of this security entity
59 	 */
60 	private String m_keyURI;
61 
SecurityEntity( XSecuritySAXEventKeeper xSAXEventKeeper, XXMLSecurityContext xXMLSecurityContext, XXMLSignature xXMLSignature, XXMLEncryption xXMLEncryption, XMultiComponentFactory xRemoteServiceManager, XComponentContext xRemoteContext)62 	SecurityEntity(
63 		XSecuritySAXEventKeeper xSAXEventKeeper,
64 		XXMLSecurityContext xXMLSecurityContext,
65 		XXMLSignature xXMLSignature,
66 		XXMLEncryption xXMLEncryption,
67 		XMultiComponentFactory xRemoteServiceManager,
68 		XComponentContext xRemoteContext)
69 	{
70 		m_xSAXEventKeeper = xSAXEventKeeper;
71 		m_xXMLSecurityContext = xXMLSecurityContext;
72 		m_xXMLSignature = xXMLSignature;
73 		m_xXMLEncryption = xXMLEncryption;
74 		m_xRemoteServiceManager = xRemoteServiceManager;
75 		m_xRemoteContext = xRemoteContext;
76 
77 		m_nSecurityId = getNextSecurityId();
78 		m_keyURI = null;
79 	}
80 
81 /**************************************************************************************
82  * private methods
83  **************************************************************************************/
84 
85 	/*
86 	 * generates a new security id.
87 	 */
getNextSecurityId()88 	private static int getNextSecurityId()
89 	{
90 		int id = m_nNextSecurityId++;
91 		return id;
92 	}
93 
94 /**************************************************************************************
95  * protected methods
96  **************************************************************************************/
97 
98 	/*
99 	 * notifies the key collector about the key id, this key id
100 	 * is used to ask the SAXEventKeeper to release the bufferred
101 	 * key element.
102 	 * when the id is 0, that means there is no independant key
103 	 * element needed.
104 	 */
setKeyId(int id)105 	protected void setKeyId(int id)
106 	{
107 		try
108 		{
109 			XKeyCollector xKeyCollector =
110 				(XKeyCollector)UnoRuntime.queryInterface(
111 					XKeyCollector.class, m_xReferenceResolvedListener);
112 			xKeyCollector.setKeyId(id);
113 		}
114 		catch( com.sun.star.uno.Exception e)
115 		{
116 			e.printStackTrace();
117 		}
118 	}
119 
120 	/*
121 	 * set the key uri, which will be the value of the id attribute
122 	 * of the key element
123 	 */
setKeyURI(String uri)124 	protected void setKeyURI(String uri)
125 	{
126 		m_keyURI = new String(uri);
127 	}
128 
getReferenceListener()129 	protected XReferenceResolvedListener getReferenceListener()
130 	{
131 		return m_xReferenceResolvedListener;
132 	}
133 
getSecurityId()134 	protected int getSecurityId()
135 	{
136 		return m_nSecurityId;
137 	}
138 
139 	/*
140 	 * configures the key material to the security entity.
141 	 *
142 	 * if the uri is the key, then:
143 	 * 1. askes the SAXEventKeeper to add a ElementCollector to the key
144 	 * element;
145 	 * 2. notifies the key collector;
146 	 * 3. configures this ElementCollector's security id;
147 	 * 4. tells the SAXEventKeeper which listener will receive the reference
148 	 * resolved notification.
149 	 */
setKey(String uri, boolean isExporting)150 	protected boolean setKey(String uri, boolean isExporting)
151 	{
152 		boolean rc = false;
153 
154 		if (m_keyURI != null &&
155 			m_keyURI.equals(uri))
156 		{
157 			int referenceId = m_xSAXEventKeeper.addSecurityElementCollector(
158 				isExporting?
159 				(ElementMarkPriority.BEFOREMODIFY):(ElementMarkPriority.AFTERMODIFY),
160 				false );
161 
162 			setKeyId(referenceId);
163 			m_xSAXEventKeeper.setSecurityId(referenceId, m_nSecurityId);
164 
165 			XReferenceResolvedBroadcaster xReferenceResolvedBroadcaster =
166 				(XReferenceResolvedBroadcaster)UnoRuntime.queryInterface(
167 					XReferenceResolvedBroadcaster.class, m_xSAXEventKeeper);
168 
169 			xReferenceResolvedBroadcaster.addReferenceResolvedListener(referenceId, m_xReferenceResolvedListener);
170 
171 			rc = true;
172 		}
173 
174 		return rc;
175 	}
176 
177 	/*
178 	 * ends this misstion, asks the security engine to clear up all
179 	 * resources.
180 	 */
endMission()181 	protected boolean endMission()
182 	{
183 		XMissionTaker xMissionTaker =
184 			(XMissionTaker)UnoRuntime.queryInterface(
185 				XMissionTaker.class, m_xReferenceResolvedListener);
186 
187 		boolean rc = xMissionTaker.endMission();
188 
189 		m_xXMLSecurityContext = null;
190 		m_xXMLSignature = null;
191 		m_xXMLEncryption = null;
192 		m_xReferenceResolvedListener = null;
193 		m_xSAXEventKeeper = null;
194 
195 		return rc;
196 	}
197 }
198 
199