1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package com.sun.star.xml.security.uno;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir /* uno classes */
31*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
32*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
33*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir import com.sun.star.xml.crypto.*;
36*cdf0e10cSrcweir import com.sun.star.xml.crypto.sax.*;
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir /*
39*cdf0e10cSrcweir  * this class maintains the data for a security operation.
40*cdf0e10cSrcweir  */
41*cdf0e10cSrcweir class SecurityEntity
42*cdf0e10cSrcweir {
43*cdf0e10cSrcweir 	/*
44*cdf0e10cSrcweir 	 * the security id, which identifies this security entity
45*cdf0e10cSrcweir 	 * uniquely.
46*cdf0e10cSrcweir 	 */
47*cdf0e10cSrcweir 	private static int m_nNextSecurityId = 1;
48*cdf0e10cSrcweir 	protected int m_nSecurityId;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir 	/*
51*cdf0e10cSrcweir 	 * xml security related components
52*cdf0e10cSrcweir 	 */
53*cdf0e10cSrcweir 	protected XXMLSecurityContext        m_xXMLSecurityContext;
54*cdf0e10cSrcweir 	protected XXMLSignature              m_xXMLSignature;
55*cdf0e10cSrcweir 	protected XXMLEncryption             m_xXMLEncryption;
56*cdf0e10cSrcweir 	protected XMultiComponentFactory     m_xRemoteServiceManager;
57*cdf0e10cSrcweir 	protected XComponentContext          m_xRemoteContext;
58*cdf0e10cSrcweir 	protected XReferenceResolvedListener m_xReferenceResolvedListener;
59*cdf0e10cSrcweir 	protected XSecuritySAXEventKeeper    m_xSAXEventKeeper;
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 	/*
62*cdf0e10cSrcweir 	 * the uri of the key material of this security entity
63*cdf0e10cSrcweir 	 */
64*cdf0e10cSrcweir 	private String m_keyURI;
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir 	SecurityEntity(
67*cdf0e10cSrcweir 		XSecuritySAXEventKeeper xSAXEventKeeper,
68*cdf0e10cSrcweir 		XXMLSecurityContext xXMLSecurityContext,
69*cdf0e10cSrcweir 		XXMLSignature xXMLSignature,
70*cdf0e10cSrcweir 		XXMLEncryption xXMLEncryption,
71*cdf0e10cSrcweir 		XMultiComponentFactory xRemoteServiceManager,
72*cdf0e10cSrcweir 		XComponentContext xRemoteContext)
73*cdf0e10cSrcweir 	{
74*cdf0e10cSrcweir 		m_xSAXEventKeeper = xSAXEventKeeper;
75*cdf0e10cSrcweir 		m_xXMLSecurityContext = xXMLSecurityContext;
76*cdf0e10cSrcweir 		m_xXMLSignature = xXMLSignature;
77*cdf0e10cSrcweir 		m_xXMLEncryption = xXMLEncryption;
78*cdf0e10cSrcweir 		m_xRemoteServiceManager = xRemoteServiceManager;
79*cdf0e10cSrcweir 		m_xRemoteContext = xRemoteContext;
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 		m_nSecurityId = getNextSecurityId();
82*cdf0e10cSrcweir 		m_keyURI = null;
83*cdf0e10cSrcweir 	}
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir /**************************************************************************************
86*cdf0e10cSrcweir  * private methods
87*cdf0e10cSrcweir  **************************************************************************************/
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 	/*
90*cdf0e10cSrcweir 	 * generates a new security id.
91*cdf0e10cSrcweir 	 */
92*cdf0e10cSrcweir 	private static int getNextSecurityId()
93*cdf0e10cSrcweir 	{
94*cdf0e10cSrcweir 		int id = m_nNextSecurityId++;
95*cdf0e10cSrcweir 		return id;
96*cdf0e10cSrcweir 	}
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir /**************************************************************************************
99*cdf0e10cSrcweir  * protected methods
100*cdf0e10cSrcweir  **************************************************************************************/
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir 	/*
103*cdf0e10cSrcweir 	 * notifies the key collector about the key id, this key id
104*cdf0e10cSrcweir 	 * is used to ask the SAXEventKeeper to release the bufferred
105*cdf0e10cSrcweir 	 * key element.
106*cdf0e10cSrcweir 	 * when the id is 0, that means there is no independant key
107*cdf0e10cSrcweir 	 * element needed.
108*cdf0e10cSrcweir 	 */
109*cdf0e10cSrcweir 	protected void setKeyId(int id)
110*cdf0e10cSrcweir 	{
111*cdf0e10cSrcweir 		try
112*cdf0e10cSrcweir 		{
113*cdf0e10cSrcweir 			XKeyCollector xKeyCollector =
114*cdf0e10cSrcweir 				(XKeyCollector)UnoRuntime.queryInterface(
115*cdf0e10cSrcweir 					XKeyCollector.class, m_xReferenceResolvedListener);
116*cdf0e10cSrcweir 			xKeyCollector.setKeyId(id);
117*cdf0e10cSrcweir 		}
118*cdf0e10cSrcweir 		catch( com.sun.star.uno.Exception e)
119*cdf0e10cSrcweir 		{
120*cdf0e10cSrcweir 			e.printStackTrace();
121*cdf0e10cSrcweir 		}
122*cdf0e10cSrcweir 	}
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 	/*
125*cdf0e10cSrcweir 	 * set the key uri, which will be the value of the id attribute
126*cdf0e10cSrcweir 	 * of the key element
127*cdf0e10cSrcweir 	 */
128*cdf0e10cSrcweir 	protected void setKeyURI(String uri)
129*cdf0e10cSrcweir 	{
130*cdf0e10cSrcweir 		m_keyURI = new String(uri);
131*cdf0e10cSrcweir 	}
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 	protected XReferenceResolvedListener getReferenceListener()
134*cdf0e10cSrcweir 	{
135*cdf0e10cSrcweir 		return m_xReferenceResolvedListener;
136*cdf0e10cSrcweir 	}
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir 	protected int getSecurityId()
139*cdf0e10cSrcweir 	{
140*cdf0e10cSrcweir 		return m_nSecurityId;
141*cdf0e10cSrcweir 	}
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 	/*
144*cdf0e10cSrcweir 	 * configures the key material to the security entity.
145*cdf0e10cSrcweir 	 *
146*cdf0e10cSrcweir 	 * if the uri is the key, then:
147*cdf0e10cSrcweir 	 * 1. askes the SAXEventKeeper to add a ElementCollector to the key
148*cdf0e10cSrcweir 	 * element;
149*cdf0e10cSrcweir 	 * 2. notifies the key collector;
150*cdf0e10cSrcweir 	 * 3. configures this ElementCollector's security id;
151*cdf0e10cSrcweir 	 * 4. tells the SAXEventKeeper which listener will receive the reference
152*cdf0e10cSrcweir 	 * resolved notification.
153*cdf0e10cSrcweir 	 */
154*cdf0e10cSrcweir 	protected boolean setKey(String uri, boolean isExporting)
155*cdf0e10cSrcweir 	{
156*cdf0e10cSrcweir 		boolean rc = false;
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 		if (m_keyURI != null &&
159*cdf0e10cSrcweir 			m_keyURI.equals(uri))
160*cdf0e10cSrcweir 		{
161*cdf0e10cSrcweir 			int referenceId = m_xSAXEventKeeper.addSecurityElementCollector(
162*cdf0e10cSrcweir 				isExporting?
163*cdf0e10cSrcweir 				(ElementMarkPriority.BEFOREMODIFY):(ElementMarkPriority.AFTERMODIFY),
164*cdf0e10cSrcweir 				false );
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 			setKeyId(referenceId);
167*cdf0e10cSrcweir 			m_xSAXEventKeeper.setSecurityId(referenceId, m_nSecurityId);
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir 			XReferenceResolvedBroadcaster xReferenceResolvedBroadcaster =
170*cdf0e10cSrcweir 				(XReferenceResolvedBroadcaster)UnoRuntime.queryInterface(
171*cdf0e10cSrcweir 					XReferenceResolvedBroadcaster.class, m_xSAXEventKeeper);
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir 			xReferenceResolvedBroadcaster.addReferenceResolvedListener(referenceId, m_xReferenceResolvedListener);
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 			rc = true;
176*cdf0e10cSrcweir 		}
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir 		return rc;
179*cdf0e10cSrcweir 	}
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir 	/*
182*cdf0e10cSrcweir 	 * ends this misstion, asks the security engine to clear up all
183*cdf0e10cSrcweir 	 * resources.
184*cdf0e10cSrcweir 	 */
185*cdf0e10cSrcweir 	protected boolean endMission()
186*cdf0e10cSrcweir 	{
187*cdf0e10cSrcweir 		XMissionTaker xMissionTaker =
188*cdf0e10cSrcweir 			(XMissionTaker)UnoRuntime.queryInterface(
189*cdf0e10cSrcweir 				XMissionTaker.class, m_xReferenceResolvedListener);
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir 		boolean rc = xMissionTaker.endMission();
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 		m_xXMLSecurityContext = null;
194*cdf0e10cSrcweir 		m_xXMLSignature = null;
195*cdf0e10cSrcweir 		m_xXMLEncryption = null;
196*cdf0e10cSrcweir 		m_xReferenceResolvedListener = null;
197*cdf0e10cSrcweir 		m_xSAXEventKeeper = null;
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir 		return rc;
200*cdf0e10cSrcweir 	}
201*cdf0e10cSrcweir }
202*cdf0e10cSrcweir 
203