Bump version to 5.0-14
[LibreOffice.git] / xmlsecurity / source / framework / encryptionengine.cxx
blob6b74377c2aa27444b00d40cac487c25bfd04fef8
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/XMLEncryptionTemplate.hpp>
23 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 using namespace com::sun::star::uno;
27 namespace cssxc = com::sun::star::xml::crypto;
28 namespace cssxw = com::sun::star::xml::wrapper;
30 EncryptionEngine::EncryptionEngine( const Reference<XComponentContext> & xContext)
31 :m_xContext(xContext), m_nIdOfBlocker(-1)
35 bool EncryptionEngine::checkReady() const
36 /****** EncryptionEngine/checkReady ******************************************
38 * NAME
39 * checkReady -- checks the conditions for the main operation.
41 * SYNOPSIS
42 * bReady = checkReady( );
44 * FUNCTION
45 * checks whether all following conditions are satisfied:
46 * 1. the main operation has't begun yet;
47 * 2. the key material is known;
48 * 3. the id of the template blocker is known;
49 * 4. both the key element and the encryption template
50 * are bufferred.
52 * INPUTS
53 * empty
55 * RESULT
56 * bReady - true if all conditions are satisfied, false otherwise
58 * AUTHOR
59 * Michael Mi
60 * Email: michael.mi@sun.com
61 ******************************************************************************/
63 bool rc = true;
65 sal_Int32 nKeyInc = 0;
66 if (m_nIdOfKeyEC != 0)
68 nKeyInc = 1;
71 if (m_bMissionDone ||
72 m_nIdOfKeyEC == -1 ||
73 m_nIdOfBlocker == -1 ||
74 1+nKeyInc > m_nNumOfResolvedReferences )
76 rc = false;
79 return rc;
82 void EncryptionEngine::tryToPerform( )
83 throw (Exception, RuntimeException)
84 /****** EncryptionEngine/tryToPerform ****************************************
86 * NAME
87 * tryToPerform -- tries to perform the encryption/decryption operation.
89 * SYNOPSIS
90 * tryToPerform( );
92 * FUNCTION
93 * if the situation is ready, perform following operations.
94 * 1. prepares a encryption template;
95 * 2. calls the encryption bridge component;
96 * 3. clears up all used resources;
97 * 4. notifies the result listener;
98 * 5. sets the "accomplishment" flag.
100 * INPUTS
101 * empty
103 * RESULT
104 * empty
106 * AUTHOR
107 * Michael Mi
108 * Email: michael.mi@sun.com
109 ******************************************************************************/
111 if (checkReady())
113 Reference < cssxc::XXMLEncryptionTemplate > xEncryptionTemplate =
114 cssxc::XMLEncryptionTemplate::create( m_xContext );
116 Reference< cssxw::XXMLElementWrapper > xXMLElement
117 = m_xSAXEventKeeper->getElement( m_nIdOfTemplateEC );
119 xEncryptionTemplate->setTemplate(xXMLElement);
121 startEngine( xEncryptionTemplate );
124 * done
126 clearUp( );
128 notifyResultListener();
130 m_bMissionDone = true;
134 void EncryptionEngine::clearUp( ) const
135 /****** EncryptionEngine/clearup *********************************************
137 * NAME
138 * clearUp -- clear up all resources used by this operation.
140 * SYNOPSIS
141 * clearUp( );
143 * FUNCTION
144 * cleaning resources up includes:
145 * 1. releases the ElementCollector for the encryption template element;
146 * 2. releases the Blocker for the encryption template element;
147 * 3. releases the ElementCollector for the key element, if there is one.
149 * INPUTS
150 * empty
152 * RESULT
153 * empty
155 * AUTHOR
156 * Michael Mi
157 * Email: michael.mi@sun.com
158 ******************************************************************************/
160 Reference < cssxc::sax::XReferenceResolvedBroadcaster >
161 xReferenceResolvedBroadcaster( m_xSAXEventKeeper, UNO_QUERY );
163 xReferenceResolvedBroadcaster->removeReferenceResolvedListener(
164 m_nIdOfTemplateEC,
165 static_cast<const Reference < cssxc::sax::XReferenceResolvedListener > >((SecurityEngine *)this));
167 m_xSAXEventKeeper->removeElementCollector(m_nIdOfTemplateEC);
169 if (m_nIdOfBlocker != -1)
171 m_xSAXEventKeeper->removeBlocker(m_nIdOfBlocker);
174 if (m_nIdOfKeyEC != 0 && m_nIdOfKeyEC != -1)
176 m_xSAXEventKeeper->removeElementCollector(m_nIdOfKeyEC);
180 /* XBlockerMonitor */
181 void SAL_CALL EncryptionEngine::setBlockerId( sal_Int32 id )
182 throw (com::sun::star::uno::Exception, com::sun::star::uno::RuntimeException, std::exception)
184 m_nIdOfBlocker = id;
185 tryToPerform();
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */