xref: /AOO41X/main/xmlsecurity/source/framework/encryptionengine.cxx (revision 06b3ce531745799678cf4bb887ef37436d81238b)
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 
EncryptionEngine()42 EncryptionEngine::EncryptionEngine( )
43         :m_nIdOfBlocker(-1)
44 {
45 }
46 
checkReady() const47 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 
tryToPerform()97 void EncryptionEngine::tryToPerform( )
98         throw (cssu::Exception, cssu::RuntimeException)
99 /****** EncryptionEngine/tryToPerform ****************************************
100  *
101  *   NAME
102  *  tryToPerform -- tries to perform the encryption/decryption operation.
103  *
104  *   SYNOPSIS
105  *  tryToPerform( );
106  *
107  *   FUNCTION
108  *  if the situation is ready, perform following operations.
109  *  1. prepares a encryption template;
110  *  2. calls the encryption bridge component;
111  *  3. clears up all used resources;
112  *  4. notifies the result listener;
113  *  5. sets the "accomplishment" flag.
114  *
115  *   INPUTS
116  *  empty
117  *
118  *   RESULT
119  *  empty
120  *
121  *   HISTORY
122  *  05.01.2004 -    implemented
123  *
124  *   AUTHOR
125  *  Michael Mi
126  *  Email: michael.mi@sun.com
127  ******************************************************************************/
128 {
129     if (checkReady())
130     {
131         const rtl::OUString sEncryptionTemplate (
132             RTL_CONSTASCII_USTRINGPARAM( ENCRYPTION_TEMPLATE ) );
133         cssu::Reference < cssxc::XXMLEncryptionTemplate > xEncryptionTemplate(
134             mxMSF->createInstance( sEncryptionTemplate ), cssu::UNO_QUERY );
135 
136         OSL_ASSERT( xEncryptionTemplate.is() );
137 
138         cssu::Reference< cssxw::XXMLElementWrapper > xXMLElement
139             = m_xSAXEventKeeper->getElement( m_nIdOfTemplateEC );
140 
141         xEncryptionTemplate->setTemplate(xXMLElement);
142 
143         startEngine( xEncryptionTemplate );
144 
145         /*
146          * done
147          */
148         clearUp( );
149 
150         notifyResultListener();
151 
152         m_bMissionDone = true;
153     }
154 }
155 
clearUp() const156 void EncryptionEngine::clearUp( ) const
157 /****** EncryptionEngine/clearup *********************************************
158  *
159  *   NAME
160  *  clearUp -- clear up all resources used by this operation.
161  *
162  *   SYNOPSIS
163  *  clearUp( );
164  *
165  *   FUNCTION
166  *  cleaning resources up includes:
167  *  1. releases the ElementCollector for the encryption template element;
168  *  2. releases the Blocker for the encryption template element;
169  *  3. releases the ElementCollector for the key element, if there is one.
170  *
171  *   INPUTS
172  *  empty
173  *
174  *   RESULT
175  *  empty
176  *
177  *   HISTORY
178  *  05.01.2004 -    implemented
179  *
180  *   AUTHOR
181  *  Michael Mi
182  *  Email: michael.mi@sun.com
183  ******************************************************************************/
184 {
185     cssu::Reference < cssxc::sax::XReferenceResolvedBroadcaster >
186         xReferenceResolvedBroadcaster( m_xSAXEventKeeper, cssu::UNO_QUERY );
187 
188     xReferenceResolvedBroadcaster->removeReferenceResolvedListener(
189         m_nIdOfTemplateEC,
190         (const cssu::Reference < cssxc::sax::XReferenceResolvedListener >)((SecurityEngine *)this));
191 
192     m_xSAXEventKeeper->removeElementCollector(m_nIdOfTemplateEC);
193 
194     if (m_nIdOfBlocker != -1)
195     {
196         m_xSAXEventKeeper->removeBlocker(m_nIdOfBlocker);
197     }
198 
199     if (m_nIdOfKeyEC != 0 && m_nIdOfKeyEC != -1)
200     {
201         m_xSAXEventKeeper->removeElementCollector(m_nIdOfKeyEC);
202     }
203 }
204 
205 /* XBlockerMonitor */
setBlockerId(sal_Int32 id)206 void SAL_CALL EncryptionEngine::setBlockerId( sal_Int32 id )
207     throw (com::sun::star::uno::Exception, com::sun::star::uno::RuntimeException)
208 {
209     m_nIdOfBlocker = id;
210     tryToPerform();
211 }
212 
213