Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
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>
25 :
26 : namespace cssu = com::sun::star::uno;
27 : namespace cssxc = com::sun::star::xml::crypto;
28 : namespace cssxw = com::sun::star::xml::wrapper;
29 :
30 : #define ENCRYPTION_TEMPLATE "com.sun.star.xml.crypto.XMLEncryptionTemplate"
31 :
32 0 : EncryptionEngine::EncryptionEngine( )
33 0 : :m_nIdOfBlocker(-1)
34 : {
35 0 : }
36 :
37 0 : bool EncryptionEngine::checkReady() const
38 : /****** EncryptionEngine/checkReady ******************************************
39 : *
40 : * NAME
41 : * checkReady -- checks the conditions for the main operation.
42 : *
43 : * SYNOPSIS
44 : * bReady = checkReady( );
45 : *
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.
53 : *
54 : * INPUTS
55 : * empty
56 : *
57 : * RESULT
58 : * bReady - true if all conditions are satisfied, false otherwise
59 : *
60 : * AUTHOR
61 : * Michael Mi
62 : * Email: michael.mi@sun.com
63 : ******************************************************************************/
64 : {
65 0 : bool rc = true;
66 :
67 0 : sal_Int32 nKeyInc = 0;
68 0 : if (m_nIdOfKeyEC != 0)
69 : {
70 0 : nKeyInc = 1;
71 : }
72 :
73 0 : if (m_bMissionDone ||
74 : m_nIdOfKeyEC == -1 ||
75 : m_nIdOfBlocker == -1 ||
76 : 1+nKeyInc > m_nNumOfResolvedReferences )
77 : {
78 0 : rc = false;
79 : }
80 :
81 0 : return rc;
82 : }
83 :
84 0 : void EncryptionEngine::tryToPerform( )
85 : throw (cssu::Exception, cssu::RuntimeException)
86 : /****** EncryptionEngine/tryToPerform ****************************************
87 : *
88 : * NAME
89 : * tryToPerform -- tries to perform the encryption/decryption operation.
90 : *
91 : * SYNOPSIS
92 : * tryToPerform( );
93 : *
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.
101 : *
102 : * INPUTS
103 : * empty
104 : *
105 : * RESULT
106 : * empty
107 : *
108 : * AUTHOR
109 : * Michael Mi
110 : * Email: michael.mi@sun.com
111 : ******************************************************************************/
112 : {
113 0 : if (checkReady())
114 : {
115 : const rtl::OUString sEncryptionTemplate (
116 0 : RTL_CONSTASCII_USTRINGPARAM( ENCRYPTION_TEMPLATE ) );
117 : cssu::Reference < cssxc::XXMLEncryptionTemplate > xEncryptionTemplate(
118 0 : mxMSF->createInstance( sEncryptionTemplate ), cssu::UNO_QUERY );
119 :
120 : OSL_ASSERT( xEncryptionTemplate.is() );
121 :
122 : cssu::Reference< cssxw::XXMLElementWrapper > xXMLElement
123 0 : = m_xSAXEventKeeper->getElement( m_nIdOfTemplateEC );
124 :
125 0 : xEncryptionTemplate->setTemplate(xXMLElement);
126 :
127 0 : startEngine( xEncryptionTemplate );
128 :
129 : /*
130 : * done
131 : */
132 0 : clearUp( );
133 :
134 0 : notifyResultListener();
135 :
136 0 : m_bMissionDone = true;
137 : }
138 0 : }
139 :
140 0 : void EncryptionEngine::clearUp( ) const
141 : /****** EncryptionEngine/clearup *********************************************
142 : *
143 : * NAME
144 : * clearUp -- clear up all resources used by this operation.
145 : *
146 : * SYNOPSIS
147 : * clearUp( );
148 : *
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.
154 : *
155 : * INPUTS
156 : * empty
157 : *
158 : * RESULT
159 : * empty
160 : *
161 : * AUTHOR
162 : * Michael Mi
163 : * Email: michael.mi@sun.com
164 : ******************************************************************************/
165 : {
166 : cssu::Reference < cssxc::sax::XReferenceResolvedBroadcaster >
167 0 : xReferenceResolvedBroadcaster( m_xSAXEventKeeper, cssu::UNO_QUERY );
168 :
169 0 : xReferenceResolvedBroadcaster->removeReferenceResolvedListener(
170 : m_nIdOfTemplateEC,
171 0 : (const cssu::Reference < cssxc::sax::XReferenceResolvedListener >)((SecurityEngine *)this));
172 :
173 0 : m_xSAXEventKeeper->removeElementCollector(m_nIdOfTemplateEC);
174 :
175 0 : if (m_nIdOfBlocker != -1)
176 : {
177 0 : m_xSAXEventKeeper->removeBlocker(m_nIdOfBlocker);
178 : }
179 :
180 0 : if (m_nIdOfKeyEC != 0 && m_nIdOfKeyEC != -1)
181 : {
182 0 : m_xSAXEventKeeper->removeElementCollector(m_nIdOfKeyEC);
183 0 : }
184 0 : }
185 :
186 : /* XBlockerMonitor */
187 0 : void SAL_CALL EncryptionEngine::setBlockerId( sal_Int32 id )
188 : throw (com::sun::star::uno::Exception, com::sun::star::uno::RuntimeException)
189 : {
190 0 : m_nIdOfBlocker = id;
191 0 : tryToPerform();
192 0 : }
193 :
194 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|