Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / xmlsecurity / source / framework / encryptionengine.cxx
blob7565fc8894fa5d0ffe704eedfda299b0fcc9d3a7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "encryptionengine.hxx"
22 #include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp>
23 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 namespace cssu = com::sun::star::uno;
27 namespace cssxc = com::sun::star::xml::crypto;
28 namespace cssxw = com::sun::star::xml::wrapper;
30 #define ENCRYPTION_TEMPLATE "com.sun.star.xml.crypto.XMLEncryptionTemplate"
32 EncryptionEngine::EncryptionEngine( )
33 :m_nIdOfBlocker(-1)
37 bool EncryptionEngine::checkReady() const
38 /****** EncryptionEngine/checkReady ******************************************
40 * NAME
41 * checkReady -- checks the conditions for the main operation.
43 * SYNOPSIS
44 * bReady = checkReady( );
46 * FUNCTION
47 * checks whether all following conditions are satisfied:
48 * 1. the main operation has't begun yet;
49 * 2. the key material is known;
50 * 3. the id of the template blocker is known;
51 * 4. both the key element and the encryption template
52 * are bufferred.
54 * INPUTS
55 * empty
57 * RESULT
58 * bReady - true if all conditions are satisfied, false otherwise
60 * AUTHOR
61 * Michael Mi
62 * Email: michael.mi@sun.com
63 ******************************************************************************/
65 bool rc = true;
67 sal_Int32 nKeyInc = 0;
68 if (m_nIdOfKeyEC != 0)
70 nKeyInc = 1;
73 if (m_bMissionDone ||
74 m_nIdOfKeyEC == -1 ||
75 m_nIdOfBlocker == -1 ||
76 1+nKeyInc > m_nNumOfResolvedReferences )
78 rc = false;
81 return rc;
84 void EncryptionEngine::tryToPerform( )
85 throw (cssu::Exception, cssu::RuntimeException)
86 /****** EncryptionEngine/tryToPerform ****************************************
88 * NAME
89 * tryToPerform -- tries to perform the encryption/decryption operation.
91 * SYNOPSIS
92 * tryToPerform( );
94 * FUNCTION
95 * if the situation is ready, perform following operations.
96 * 1. prepares a encryption template;
97 * 2. calls the encryption bridge component;
98 * 3. clears up all used resources;
99 * 4. notifies the result listener;
100 * 5. sets the "accomplishment" flag.
102 * INPUTS
103 * empty
105 * RESULT
106 * empty
108 * AUTHOR
109 * Michael Mi
110 * Email: michael.mi@sun.com
111 ******************************************************************************/
113 if (checkReady())
115 const rtl::OUString sEncryptionTemplate (
116 RTL_CONSTASCII_USTRINGPARAM( ENCRYPTION_TEMPLATE ) );
117 cssu::Reference < cssxc::XXMLEncryptionTemplate > xEncryptionTemplate(
118 mxMSF->createInstance( sEncryptionTemplate ), cssu::UNO_QUERY );
120 OSL_ASSERT( xEncryptionTemplate.is() );
122 cssu::Reference< cssxw::XXMLElementWrapper > xXMLElement
123 = m_xSAXEventKeeper->getElement( m_nIdOfTemplateEC );
125 xEncryptionTemplate->setTemplate(xXMLElement);
127 startEngine( xEncryptionTemplate );
130 * done
132 clearUp( );
134 notifyResultListener();
136 m_bMissionDone = true;
140 void EncryptionEngine::clearUp( ) const
141 /****** EncryptionEngine/clearup *********************************************
143 * NAME
144 * clearUp -- clear up all resources used by this operation.
146 * SYNOPSIS
147 * clearUp( );
149 * FUNCTION
150 * cleaning resources up includes:
151 * 1. releases the ElementCollector for the encryption template element;
152 * 2. releases the Blocker for the encryption template element;
153 * 3. releases the ElementCollector for the key element, if there is one.
155 * INPUTS
156 * empty
158 * RESULT
159 * empty
161 * AUTHOR
162 * Michael Mi
163 * Email: michael.mi@sun.com
164 ******************************************************************************/
166 cssu::Reference < cssxc::sax::XReferenceResolvedBroadcaster >
167 xReferenceResolvedBroadcaster( m_xSAXEventKeeper, cssu::UNO_QUERY );
169 xReferenceResolvedBroadcaster->removeReferenceResolvedListener(
170 m_nIdOfTemplateEC,
171 (const cssu::Reference < cssxc::sax::XReferenceResolvedListener >)((SecurityEngine *)this));
173 m_xSAXEventKeeper->removeElementCollector(m_nIdOfTemplateEC);
175 if (m_nIdOfBlocker != -1)
177 m_xSAXEventKeeper->removeBlocker(m_nIdOfBlocker);
180 if (m_nIdOfKeyEC != 0 && m_nIdOfKeyEC != -1)
182 m_xSAXEventKeeper->removeElementCollector(m_nIdOfKeyEC);
186 /* XBlockerMonitor */
187 void SAL_CALL EncryptionEngine::setBlockerId( sal_Int32 id )
188 throw (com::sun::star::uno::Exception, com::sun::star::uno::RuntimeException)
190 m_nIdOfBlocker = id;
191 tryToPerform();
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */