xref: /trunk/main/xmlsecurity/source/framework/encryptionengine.cxx (revision 137931e43899a1545869cb6af6ea936d8cee4956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmlsecurity.hxx"
26 
27 #include "encryptionengine.hxx"
28 #include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp>
29 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 
32 namespace cssu = com::sun::star::uno;
33 namespace cssl = com::sun::star::lang;
34 namespace cssxc = com::sun::star::xml::crypto;
35 namespace cssxw = com::sun::star::xml::wrapper;
36 
37 #define ENCRYPTION_TEMPLATE "com.sun.star.xml.crypto.XMLEncryptionTemplate"
38 
39 #define DECLARE_ASCII( SASCIIVALUE )                                                                            \
40     rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SASCIIVALUE ) )
41 
42 EncryptionEngine::EncryptionEngine( )
43         :m_nIdOfBlocker(-1)
44 {
45 }
46 
47 bool EncryptionEngine::checkReady() const
48 /****** EncryptionEngine/checkReady ******************************************
49  *
50  *   NAME
51  *  checkReady -- checks the conditions for the main operation.
52  *
53  *   SYNOPSIS
54  *  bReady = checkReady( );
55  *
56  *   FUNCTION
57  *  checks whether all following conditions are satisfied:
58  *  1. the main operation has't begun yet;
59  *  2. the key material is known;
60  *  3. the id of the template blocker is known;
61  *  4. both the key element and the encryption template
62  *     are bufferred.
63  *
64  *   INPUTS
65  *  empty
66  *
67  *   RESULT
68  *  bReady - true if all conditions are satisfied, false otherwise
69  *
70  *   HISTORY
71  *  05.01.2004 -    implemented
72  *
73  *   AUTHOR
74  *  Michael Mi
75  *  Email: michael.mi@sun.com
76  ******************************************************************************/
77 {
78     bool rc = true;
79 
80     sal_Int32 nKeyInc = 0;
81     if (m_nIdOfKeyEC != 0)
82     {
83         nKeyInc = 1;
84     }
85 
86     if (m_bMissionDone ||
87         m_nIdOfKeyEC == -1 ||
88         m_nIdOfBlocker == -1 ||
89         1+nKeyInc > m_nNumOfResolvedReferences )
90     {
91         rc = false;
92     }
93 
94     return rc;
95 }
96 
97 void EncryptionEngine::tryToPerform( )
98 /****** EncryptionEngine/tryToPerform ****************************************
99  *
100  *   NAME
101  *  tryToPerform -- tries to perform the encryption/decryption operation.
102  *
103  *   SYNOPSIS
104  *  tryToPerform( );
105  *
106  *   FUNCTION
107  *  if the situation is ready, perform following operations.
108  *  1. prepares a encryption template;
109  *  2. calls the encryption bridge component;
110  *  3. clears up all used resources;
111  *  4. notifies the result listener;
112  *  5. sets the "accomplishment" flag.
113  *
114  *   INPUTS
115  *  empty
116  *
117  *   RESULT
118  *  empty
119  *
120  *   HISTORY
121  *  05.01.2004 -    implemented
122  *
123  *   AUTHOR
124  *  Michael Mi
125  *  Email: michael.mi@sun.com
126  ******************************************************************************/
127 {
128     if (checkReady())
129     {
130         const rtl::OUString sEncryptionTemplate (
131             RTL_CONSTASCII_USTRINGPARAM( ENCRYPTION_TEMPLATE ) );
132         cssu::Reference < cssxc::XXMLEncryptionTemplate > xEncryptionTemplate(
133             mxMSF->createInstance( sEncryptionTemplate ), cssu::UNO_QUERY );
134 
135         OSL_ASSERT( xEncryptionTemplate.is() );
136 
137         cssu::Reference< cssxw::XXMLElementWrapper > xXMLElement
138             = m_xSAXEventKeeper->getElement( m_nIdOfTemplateEC );
139 
140         xEncryptionTemplate->setTemplate(xXMLElement);
141 
142         startEngine( xEncryptionTemplate );
143 
144         /*
145          * done
146          */
147         clearUp( );
148 
149         notifyResultListener();
150 
151         m_bMissionDone = true;
152     }
153 }
154 
155 void EncryptionEngine::clearUp( ) const
156 /****** EncryptionEngine/clearup *********************************************
157  *
158  *   NAME
159  *  clearUp -- clear up all resources used by this operation.
160  *
161  *   SYNOPSIS
162  *  clearUp( );
163  *
164  *   FUNCTION
165  *  cleaning resources up includes:
166  *  1. releases the ElementCollector for the encryption template element;
167  *  2. releases the Blocker for the encryption template element;
168  *  3. releases the ElementCollector for the key element, if there is one.
169  *
170  *   INPUTS
171  *  empty
172  *
173  *   RESULT
174  *  empty
175  *
176  *   HISTORY
177  *  05.01.2004 -    implemented
178  *
179  *   AUTHOR
180  *  Michael Mi
181  *  Email: michael.mi@sun.com
182  ******************************************************************************/
183 {
184     cssu::Reference < cssxc::sax::XReferenceResolvedBroadcaster >
185         xReferenceResolvedBroadcaster( m_xSAXEventKeeper, cssu::UNO_QUERY );
186 
187     xReferenceResolvedBroadcaster->removeReferenceResolvedListener(
188         m_nIdOfTemplateEC,
189         (const cssu::Reference < cssxc::sax::XReferenceResolvedListener >)((SecurityEngine *)this));
190 
191     m_xSAXEventKeeper->removeElementCollector(m_nIdOfTemplateEC);
192 
193     if (m_nIdOfBlocker != -1)
194     {
195         m_xSAXEventKeeper->removeBlocker(m_nIdOfBlocker);
196     }
197 
198     if (m_nIdOfKeyEC != 0 && m_nIdOfKeyEC != -1)
199     {
200         m_xSAXEventKeeper->removeElementCollector(m_nIdOfKeyEC);
201     }
202 }
203 
204 /* XBlockerMonitor */
205 void SAL_CALL EncryptionEngine::setBlockerId( sal_Int32 id )
206 {
207     m_nIdOfBlocker = id;
208     tryToPerform();
209 }
210