merged tag ooo/OOO330_m14
[LibreOffice.git] / xmlsecurity / source / framework / encryptionengine.cxx
blob53e0c295a192fb388e5af175a0fae79b8d4d8fc2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
31 #include "encryptionengine.hxx"
32 #include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp>
33 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
34 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 namespace cssu = com::sun::star::uno;
37 namespace cssl = com::sun::star::lang;
38 namespace cssxc = com::sun::star::xml::crypto;
39 namespace cssxw = com::sun::star::xml::wrapper;
41 #define ENCRYPTION_TEMPLATE "com.sun.star.xml.crypto.XMLEncryptionTemplate"
43 #define DECLARE_ASCII( SASCIIVALUE ) \
44 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SASCIIVALUE ) )
46 EncryptionEngine::EncryptionEngine( )
47 :m_nIdOfBlocker(-1)
51 bool EncryptionEngine::checkReady() const
52 /****** EncryptionEngine/checkReady ******************************************
54 * NAME
55 * checkReady -- checks the conditions for the main operation.
57 * SYNOPSIS
58 * bReady = checkReady( );
60 * FUNCTION
61 * checks whether all following conditions are satisfied:
62 * 1. the main operation has't begun yet;
63 * 2. the key material is known;
64 * 3. the id of the template blocker is known;
65 * 4. both the key element and the encryption template
66 * are bufferred.
68 * INPUTS
69 * empty
71 * RESULT
72 * bReady - true if all conditions are satisfied, false otherwise
74 * HISTORY
75 * 05.01.2004 - implemented
77 * AUTHOR
78 * Michael Mi
79 * Email: michael.mi@sun.com
80 ******************************************************************************/
82 bool rc = true;
84 sal_Int32 nKeyInc = 0;
85 if (m_nIdOfKeyEC != 0)
87 nKeyInc = 1;
90 if (m_bMissionDone ||
91 m_nIdOfKeyEC == -1 ||
92 m_nIdOfBlocker == -1 ||
93 1+nKeyInc > m_nNumOfResolvedReferences )
95 rc = false;
98 return rc;
101 void EncryptionEngine::tryToPerform( )
102 throw (cssu::Exception, cssu::RuntimeException)
103 /****** EncryptionEngine/tryToPerform ****************************************
105 * NAME
106 * tryToPerform -- tries to perform the encryption/decryption operation.
108 * SYNOPSIS
109 * tryToPerform( );
111 * FUNCTION
112 * if the situation is ready, perform following operations.
113 * 1. prepares a encryption template;
114 * 2. calls the encryption bridge component;
115 * 3. clears up all used resources;
116 * 4. notifies the result listener;
117 * 5. sets the "accomplishment" flag.
119 * INPUTS
120 * empty
122 * RESULT
123 * empty
125 * HISTORY
126 * 05.01.2004 - implemented
128 * AUTHOR
129 * Michael Mi
130 * Email: michael.mi@sun.com
131 ******************************************************************************/
133 if (checkReady())
135 const rtl::OUString sEncryptionTemplate (
136 RTL_CONSTASCII_USTRINGPARAM( ENCRYPTION_TEMPLATE ) );
137 cssu::Reference < cssxc::XXMLEncryptionTemplate > xEncryptionTemplate(
138 mxMSF->createInstance( sEncryptionTemplate ), cssu::UNO_QUERY );
140 OSL_ASSERT( xEncryptionTemplate.is() );
142 cssu::Reference< cssxw::XXMLElementWrapper > xXMLElement
143 = m_xSAXEventKeeper->getElement( m_nIdOfTemplateEC );
145 xEncryptionTemplate->setTemplate(xXMLElement);
147 startEngine( xEncryptionTemplate );
150 * done
152 clearUp( );
154 notifyResultListener();
156 m_bMissionDone = true;
160 void EncryptionEngine::clearUp( ) const
161 /****** EncryptionEngine/clearup *********************************************
163 * NAME
164 * clearUp -- clear up all resources used by this operation.
166 * SYNOPSIS
167 * clearUp( );
169 * FUNCTION
170 * cleaning resources up includes:
171 * 1. releases the ElementCollector for the encryption template element;
172 * 2. releases the Blocker for the encryption template element;
173 * 3. releases the ElementCollector for the key element, if there is one.
175 * INPUTS
176 * empty
178 * RESULT
179 * empty
181 * HISTORY
182 * 05.01.2004 - implemented
184 * AUTHOR
185 * Michael Mi
186 * Email: michael.mi@sun.com
187 ******************************************************************************/
189 cssu::Reference < cssxc::sax::XReferenceResolvedBroadcaster >
190 xReferenceResolvedBroadcaster( m_xSAXEventKeeper, cssu::UNO_QUERY );
192 xReferenceResolvedBroadcaster->removeReferenceResolvedListener(
193 m_nIdOfTemplateEC,
194 (const cssu::Reference < cssxc::sax::XReferenceResolvedListener >)((SecurityEngine *)this));
196 m_xSAXEventKeeper->removeElementCollector(m_nIdOfTemplateEC);
198 if (m_nIdOfBlocker != -1)
200 m_xSAXEventKeeper->removeBlocker(m_nIdOfBlocker);
203 if (m_nIdOfKeyEC != 0 && m_nIdOfKeyEC != -1)
205 m_xSAXEventKeeper->removeElementCollector(m_nIdOfKeyEC);
209 /* XBlockerMonitor */
210 void SAL_CALL EncryptionEngine::setBlockerId( sal_Int32 id )
211 throw (com::sun::star::uno::Exception, com::sun::star::uno::RuntimeException)
213 m_nIdOfBlocker = id;
214 tryToPerform();