Update ooo320-m1
[ooovba.git] / xmlsecurity / source / framework / encryptionengine.cxx
blob240d0f327f3c4f8bfca9cec8fabdcf7878957aa2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: encryptionengine.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
34 #include "encryptionengine.hxx"
35 #include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp>
36 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39 namespace cssu = com::sun::star::uno;
40 namespace cssl = com::sun::star::lang;
41 namespace cssxc = com::sun::star::xml::crypto;
42 namespace cssxw = com::sun::star::xml::wrapper;
44 #define ENCRYPTION_TEMPLATE "com.sun.star.xml.crypto.XMLEncryptionTemplate"
46 #define DECLARE_ASCII( SASCIIVALUE ) \
47 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SASCIIVALUE ) )
49 EncryptionEngine::EncryptionEngine( )
50 :m_nIdOfBlocker(-1)
54 bool EncryptionEngine::checkReady() const
55 /****** EncryptionEngine/checkReady ******************************************
57 * NAME
58 * checkReady -- checks the conditions for the main operation.
60 * SYNOPSIS
61 * bReady = checkReady( );
63 * FUNCTION
64 * checks whether all following conditions are satisfied:
65 * 1. the main operation has't begun yet;
66 * 2. the key material is known;
67 * 3. the id of the template blocker is known;
68 * 4. both the key element and the encryption template
69 * are bufferred.
71 * INPUTS
72 * empty
74 * RESULT
75 * bReady - true if all conditions are satisfied, false otherwise
77 * HISTORY
78 * 05.01.2004 - implemented
80 * AUTHOR
81 * Michael Mi
82 * Email: michael.mi@sun.com
83 ******************************************************************************/
85 bool rc = true;
87 sal_Int32 nKeyInc = 0;
88 if (m_nIdOfKeyEC != 0)
90 nKeyInc = 1;
93 if (m_bMissionDone ||
94 m_nIdOfKeyEC == -1 ||
95 m_nIdOfBlocker == -1 ||
96 1+nKeyInc > m_nNumOfResolvedReferences )
98 rc = false;
101 return rc;
104 void EncryptionEngine::tryToPerform( )
105 throw (cssu::Exception, cssu::RuntimeException)
106 /****** EncryptionEngine/tryToPerform ****************************************
108 * NAME
109 * tryToPerform -- tries to perform the encryption/decryption operation.
111 * SYNOPSIS
112 * tryToPerform( );
114 * FUNCTION
115 * if the situation is ready, perform following operations.
116 * 1. prepares a encryption template;
117 * 2. calls the encryption bridge component;
118 * 3. clears up all used resources;
119 * 4. notifies the result listener;
120 * 5. sets the "accomplishment" flag.
122 * INPUTS
123 * empty
125 * RESULT
126 * empty
128 * HISTORY
129 * 05.01.2004 - implemented
131 * AUTHOR
132 * Michael Mi
133 * Email: michael.mi@sun.com
134 ******************************************************************************/
136 if (checkReady())
138 const rtl::OUString sEncryptionTemplate (
139 RTL_CONSTASCII_USTRINGPARAM( ENCRYPTION_TEMPLATE ) );
140 cssu::Reference < cssxc::XXMLEncryptionTemplate > xEncryptionTemplate(
141 mxMSF->createInstance( sEncryptionTemplate ), cssu::UNO_QUERY );
143 OSL_ASSERT( xEncryptionTemplate.is() );
145 cssu::Reference< cssxw::XXMLElementWrapper > xXMLElement
146 = m_xSAXEventKeeper->getElement( m_nIdOfTemplateEC );
148 xEncryptionTemplate->setTemplate(xXMLElement);
150 startEngine( xEncryptionTemplate );
153 * done
155 clearUp( );
157 notifyResultListener();
159 m_bMissionDone = true;
163 void EncryptionEngine::clearUp( ) const
164 /****** EncryptionEngine/clearup *********************************************
166 * NAME
167 * clearUp -- clear up all resources used by this operation.
169 * SYNOPSIS
170 * clearUp( );
172 * FUNCTION
173 * cleaning resources up includes:
174 * 1. releases the ElementCollector for the encryption template element;
175 * 2. releases the Blocker for the encryption template element;
176 * 3. releases the ElementCollector for the key element, if there is one.
178 * INPUTS
179 * empty
181 * RESULT
182 * empty
184 * HISTORY
185 * 05.01.2004 - implemented
187 * AUTHOR
188 * Michael Mi
189 * Email: michael.mi@sun.com
190 ******************************************************************************/
192 cssu::Reference < cssxc::sax::XReferenceResolvedBroadcaster >
193 xReferenceResolvedBroadcaster( m_xSAXEventKeeper, cssu::UNO_QUERY );
195 xReferenceResolvedBroadcaster->removeReferenceResolvedListener(
196 m_nIdOfTemplateEC,
197 (const cssu::Reference < cssxc::sax::XReferenceResolvedListener >)((SecurityEngine *)this));
199 m_xSAXEventKeeper->removeElementCollector(m_nIdOfTemplateEC);
201 if (m_nIdOfBlocker != -1)
203 m_xSAXEventKeeper->removeBlocker(m_nIdOfBlocker);
206 if (m_nIdOfKeyEC != 0 && m_nIdOfKeyEC != -1)
208 m_xSAXEventKeeper->removeElementCollector(m_nIdOfKeyEC);
212 /* XBlockerMonitor */
213 void SAL_CALL EncryptionEngine::setBlockerId( sal_Int32 id )
214 throw (com::sun::star::uno::Exception, com::sun::star::uno::RuntimeException)
216 m_nIdOfBlocker = id;
217 tryToPerform();