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