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 // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_xmlsecurity.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "encryptionengine.hxx"
32*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir namespace cssu = com::sun::star::uno;
37*cdf0e10cSrcweir namespace cssl = com::sun::star::lang;
38*cdf0e10cSrcweir namespace cssxc = com::sun::star::xml::crypto;
39*cdf0e10cSrcweir namespace cssxw = com::sun::star::xml::wrapper;
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #define ENCRYPTION_TEMPLATE "com.sun.star.xml.crypto.XMLEncryptionTemplate"
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #define	DECLARE_ASCII( SASCIIVALUE )																			\
44*cdf0e10cSrcweir 	rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SASCIIVALUE ) )
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir EncryptionEngine::EncryptionEngine( )
47*cdf0e10cSrcweir         :m_nIdOfBlocker(-1)
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir }
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir bool EncryptionEngine::checkReady() const
52*cdf0e10cSrcweir /****** EncryptionEngine/checkReady ******************************************
53*cdf0e10cSrcweir  *
54*cdf0e10cSrcweir  *   NAME
55*cdf0e10cSrcweir  *	checkReady -- checks the conditions for the main operation.
56*cdf0e10cSrcweir  *
57*cdf0e10cSrcweir  *   SYNOPSIS
58*cdf0e10cSrcweir  *	bReady = checkReady( );
59*cdf0e10cSrcweir  *
60*cdf0e10cSrcweir  *   FUNCTION
61*cdf0e10cSrcweir  *	checks whether all following conditions are satisfied:
62*cdf0e10cSrcweir  *	1. the main operation has't begun yet;
63*cdf0e10cSrcweir  *	2. the key material is known;
64*cdf0e10cSrcweir  *	3. the id of the template blocker is known;
65*cdf0e10cSrcweir  *	4. both the key element and the encryption template
66*cdf0e10cSrcweir  *	   are bufferred.
67*cdf0e10cSrcweir  *
68*cdf0e10cSrcweir  *   INPUTS
69*cdf0e10cSrcweir  *	empty
70*cdf0e10cSrcweir  *
71*cdf0e10cSrcweir  *   RESULT
72*cdf0e10cSrcweir  *	bReady - true if all conditions are satisfied, false otherwise
73*cdf0e10cSrcweir  *
74*cdf0e10cSrcweir  *   HISTORY
75*cdf0e10cSrcweir  *	05.01.2004 -	implemented
76*cdf0e10cSrcweir  *
77*cdf0e10cSrcweir  *   AUTHOR
78*cdf0e10cSrcweir  *	Michael Mi
79*cdf0e10cSrcweir  *	Email: michael.mi@sun.com
80*cdf0e10cSrcweir  ******************************************************************************/
81*cdf0e10cSrcweir {
82*cdf0e10cSrcweir 	bool rc = true;
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir 	sal_Int32 nKeyInc = 0;
85*cdf0e10cSrcweir 	if (m_nIdOfKeyEC != 0)
86*cdf0e10cSrcweir 	{
87*cdf0e10cSrcweir 		nKeyInc = 1;
88*cdf0e10cSrcweir 	}
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 	if (m_bMissionDone ||
91*cdf0e10cSrcweir 	    m_nIdOfKeyEC == -1 ||
92*cdf0e10cSrcweir 	    m_nIdOfBlocker == -1 ||
93*cdf0e10cSrcweir 	    1+nKeyInc > m_nNumOfResolvedReferences )
94*cdf0e10cSrcweir 	{
95*cdf0e10cSrcweir 		rc = false;
96*cdf0e10cSrcweir 	}
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 	return rc;
99*cdf0e10cSrcweir }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir void EncryptionEngine::tryToPerform( )
102*cdf0e10cSrcweir     	throw (cssu::Exception, cssu::RuntimeException)
103*cdf0e10cSrcweir /****** EncryptionEngine/tryToPerform ****************************************
104*cdf0e10cSrcweir  *
105*cdf0e10cSrcweir  *   NAME
106*cdf0e10cSrcweir  *	tryToPerform -- tries to perform the encryption/decryption operation.
107*cdf0e10cSrcweir  *
108*cdf0e10cSrcweir  *   SYNOPSIS
109*cdf0e10cSrcweir  *	tryToPerform( );
110*cdf0e10cSrcweir  *
111*cdf0e10cSrcweir  *   FUNCTION
112*cdf0e10cSrcweir  *	if the situation is ready, perform following operations.
113*cdf0e10cSrcweir  *	1. prepares a encryption template;
114*cdf0e10cSrcweir  *	2. calls the encryption bridge component;
115*cdf0e10cSrcweir  *	3. clears up all used resources;
116*cdf0e10cSrcweir  *	4. notifies the result listener;
117*cdf0e10cSrcweir  *	5. sets the "accomplishment" flag.
118*cdf0e10cSrcweir  *
119*cdf0e10cSrcweir  *   INPUTS
120*cdf0e10cSrcweir  *	empty
121*cdf0e10cSrcweir  *
122*cdf0e10cSrcweir  *   RESULT
123*cdf0e10cSrcweir  *	empty
124*cdf0e10cSrcweir  *
125*cdf0e10cSrcweir  *   HISTORY
126*cdf0e10cSrcweir  *	05.01.2004 -	implemented
127*cdf0e10cSrcweir  *
128*cdf0e10cSrcweir  *   AUTHOR
129*cdf0e10cSrcweir  *	Michael Mi
130*cdf0e10cSrcweir  *	Email: michael.mi@sun.com
131*cdf0e10cSrcweir  ******************************************************************************/
132*cdf0e10cSrcweir {
133*cdf0e10cSrcweir 	if (checkReady())
134*cdf0e10cSrcweir 	{
135*cdf0e10cSrcweir 		const rtl::OUString sEncryptionTemplate (
136*cdf0e10cSrcweir 			RTL_CONSTASCII_USTRINGPARAM( ENCRYPTION_TEMPLATE ) );
137*cdf0e10cSrcweir 		cssu::Reference < cssxc::XXMLEncryptionTemplate > xEncryptionTemplate(
138*cdf0e10cSrcweir 			mxMSF->createInstance( sEncryptionTemplate ), cssu::UNO_QUERY );
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 		OSL_ASSERT( xEncryptionTemplate.is() );
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 		cssu::Reference< cssxw::XXMLElementWrapper > xXMLElement
143*cdf0e10cSrcweir 			= m_xSAXEventKeeper->getElement( m_nIdOfTemplateEC );
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 		xEncryptionTemplate->setTemplate(xXMLElement);
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 		startEngine( xEncryptionTemplate );
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir 		/*
150*cdf0e10cSrcweir 		 * done
151*cdf0e10cSrcweir 		 */
152*cdf0e10cSrcweir 		clearUp( );
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 		notifyResultListener();
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir 		m_bMissionDone = true;
157*cdf0e10cSrcweir 	}
158*cdf0e10cSrcweir }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir void EncryptionEngine::clearUp( ) const
161*cdf0e10cSrcweir /****** EncryptionEngine/clearup *********************************************
162*cdf0e10cSrcweir  *
163*cdf0e10cSrcweir  *   NAME
164*cdf0e10cSrcweir  *	clearUp -- clear up all resources used by this operation.
165*cdf0e10cSrcweir  *
166*cdf0e10cSrcweir  *   SYNOPSIS
167*cdf0e10cSrcweir  *	clearUp( );
168*cdf0e10cSrcweir  *
169*cdf0e10cSrcweir  *   FUNCTION
170*cdf0e10cSrcweir  *	cleaning resources up includes:
171*cdf0e10cSrcweir  *	1. releases the ElementCollector for the encryption template element;
172*cdf0e10cSrcweir  *	2. releases the Blocker for the encryption template element;
173*cdf0e10cSrcweir  *	3. releases the ElementCollector for the key element, if there is one.
174*cdf0e10cSrcweir  *
175*cdf0e10cSrcweir  *   INPUTS
176*cdf0e10cSrcweir  *	empty
177*cdf0e10cSrcweir  *
178*cdf0e10cSrcweir  *   RESULT
179*cdf0e10cSrcweir  *	empty
180*cdf0e10cSrcweir  *
181*cdf0e10cSrcweir  *   HISTORY
182*cdf0e10cSrcweir  *	05.01.2004 -	implemented
183*cdf0e10cSrcweir  *
184*cdf0e10cSrcweir  *   AUTHOR
185*cdf0e10cSrcweir  *	Michael Mi
186*cdf0e10cSrcweir  *	Email: michael.mi@sun.com
187*cdf0e10cSrcweir  ******************************************************************************/
188*cdf0e10cSrcweir {
189*cdf0e10cSrcweir 	cssu::Reference < cssxc::sax::XReferenceResolvedBroadcaster >
190*cdf0e10cSrcweir 		xReferenceResolvedBroadcaster( m_xSAXEventKeeper, cssu::UNO_QUERY );
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir 	xReferenceResolvedBroadcaster->removeReferenceResolvedListener(
193*cdf0e10cSrcweir 		m_nIdOfTemplateEC,
194*cdf0e10cSrcweir 		(const cssu::Reference < cssxc::sax::XReferenceResolvedListener >)((SecurityEngine *)this));
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir 	m_xSAXEventKeeper->removeElementCollector(m_nIdOfTemplateEC);
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir 	if (m_nIdOfBlocker != -1)
199*cdf0e10cSrcweir 	{
200*cdf0e10cSrcweir 		m_xSAXEventKeeper->removeBlocker(m_nIdOfBlocker);
201*cdf0e10cSrcweir 	}
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir 	if (m_nIdOfKeyEC != 0 && m_nIdOfKeyEC != -1)
204*cdf0e10cSrcweir 	{
205*cdf0e10cSrcweir 		m_xSAXEventKeeper->removeElementCollector(m_nIdOfKeyEC);
206*cdf0e10cSrcweir 	}
207*cdf0e10cSrcweir }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir /* XBlockerMonitor */
210*cdf0e10cSrcweir void SAL_CALL EncryptionEngine::setBlockerId( sal_Int32 id )
211*cdf0e10cSrcweir 	throw (com::sun::star::uno::Exception, com::sun::star::uno::RuntimeException)
212*cdf0e10cSrcweir {
213*cdf0e10cSrcweir 	m_nIdOfBlocker = id;
214*cdf0e10cSrcweir 	tryToPerform();
215*cdf0e10cSrcweir }
216*cdf0e10cSrcweir 
217