xref: /trunk/main/xmlsecurity/source/framework/encryptionengine.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
30 
31 #include "encryptionengine.hxx"
32 #include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp>
33 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
34 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35 
36 namespace cssu = com::sun::star::uno;
37 namespace cssl = com::sun::star::lang;
38 namespace cssxc = com::sun::star::xml::crypto;
39 namespace cssxw = com::sun::star::xml::wrapper;
40 
41 #define ENCRYPTION_TEMPLATE "com.sun.star.xml.crypto.XMLEncryptionTemplate"
42 
43 #define DECLARE_ASCII( SASCIIVALUE )                                                                            \
44     rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SASCIIVALUE ) )
45 
46 EncryptionEngine::EncryptionEngine( )
47         :m_nIdOfBlocker(-1)
48 {
49 }
50 
51 bool EncryptionEngine::checkReady() const
52 /****** EncryptionEngine/checkReady ******************************************
53  *
54  *   NAME
55  *  checkReady -- checks the conditions for the main operation.
56  *
57  *   SYNOPSIS
58  *  bReady = checkReady( );
59  *
60  *   FUNCTION
61  *  checks whether all following conditions are satisfied:
62  *  1. the main operation has't begun yet;
63  *  2. the key material is known;
64  *  3. the id of the template blocker is known;
65  *  4. both the key element and the encryption template
66  *     are bufferred.
67  *
68  *   INPUTS
69  *  empty
70  *
71  *   RESULT
72  *  bReady - true if all conditions are satisfied, false otherwise
73  *
74  *   HISTORY
75  *  05.01.2004 -    implemented
76  *
77  *   AUTHOR
78  *  Michael Mi
79  *  Email: michael.mi@sun.com
80  ******************************************************************************/
81 {
82     bool rc = true;
83 
84     sal_Int32 nKeyInc = 0;
85     if (m_nIdOfKeyEC != 0)
86     {
87         nKeyInc = 1;
88     }
89 
90     if (m_bMissionDone ||
91         m_nIdOfKeyEC == -1 ||
92         m_nIdOfBlocker == -1 ||
93         1+nKeyInc > m_nNumOfResolvedReferences )
94     {
95         rc = false;
96     }
97 
98     return rc;
99 }
100 
101 void EncryptionEngine::tryToPerform( )
102         throw (cssu::Exception, cssu::RuntimeException)
103 /****** EncryptionEngine/tryToPerform ****************************************
104  *
105  *   NAME
106  *  tryToPerform -- tries to perform the encryption/decryption operation.
107  *
108  *   SYNOPSIS
109  *  tryToPerform( );
110  *
111  *   FUNCTION
112  *  if the situation is ready, perform following operations.
113  *  1. prepares a encryption template;
114  *  2. calls the encryption bridge component;
115  *  3. clears up all used resources;
116  *  4. notifies the result listener;
117  *  5. sets the "accomplishment" flag.
118  *
119  *   INPUTS
120  *  empty
121  *
122  *   RESULT
123  *  empty
124  *
125  *   HISTORY
126  *  05.01.2004 -    implemented
127  *
128  *   AUTHOR
129  *  Michael Mi
130  *  Email: michael.mi@sun.com
131  ******************************************************************************/
132 {
133     if (checkReady())
134     {
135         const rtl::OUString sEncryptionTemplate (
136             RTL_CONSTASCII_USTRINGPARAM( ENCRYPTION_TEMPLATE ) );
137         cssu::Reference < cssxc::XXMLEncryptionTemplate > xEncryptionTemplate(
138             mxMSF->createInstance( sEncryptionTemplate ), cssu::UNO_QUERY );
139 
140         OSL_ASSERT( xEncryptionTemplate.is() );
141 
142         cssu::Reference< cssxw::XXMLElementWrapper > xXMLElement
143             = m_xSAXEventKeeper->getElement( m_nIdOfTemplateEC );
144 
145         xEncryptionTemplate->setTemplate(xXMLElement);
146 
147         startEngine( xEncryptionTemplate );
148 
149         /*
150          * done
151          */
152         clearUp( );
153 
154         notifyResultListener();
155 
156         m_bMissionDone = true;
157     }
158 }
159 
160 void EncryptionEngine::clearUp( ) const
161 /****** EncryptionEngine/clearup *********************************************
162  *
163  *   NAME
164  *  clearUp -- clear up all resources used by this operation.
165  *
166  *   SYNOPSIS
167  *  clearUp( );
168  *
169  *   FUNCTION
170  *  cleaning resources up includes:
171  *  1. releases the ElementCollector for the encryption template element;
172  *  2. releases the Blocker for the encryption template element;
173  *  3. releases the ElementCollector for the key element, if there is one.
174  *
175  *   INPUTS
176  *  empty
177  *
178  *   RESULT
179  *  empty
180  *
181  *   HISTORY
182  *  05.01.2004 -    implemented
183  *
184  *   AUTHOR
185  *  Michael Mi
186  *  Email: michael.mi@sun.com
187  ******************************************************************************/
188 {
189     cssu::Reference < cssxc::sax::XReferenceResolvedBroadcaster >
190         xReferenceResolvedBroadcaster( m_xSAXEventKeeper, cssu::UNO_QUERY );
191 
192     xReferenceResolvedBroadcaster->removeReferenceResolvedListener(
193         m_nIdOfTemplateEC,
194         (const cssu::Reference < cssxc::sax::XReferenceResolvedListener >)((SecurityEngine *)this));
195 
196     m_xSAXEventKeeper->removeElementCollector(m_nIdOfTemplateEC);
197 
198     if (m_nIdOfBlocker != -1)
199     {
200         m_xSAXEventKeeper->removeBlocker(m_nIdOfBlocker);
201     }
202 
203     if (m_nIdOfKeyEC != 0 && m_nIdOfKeyEC != -1)
204     {
205         m_xSAXEventKeeper->removeElementCollector(m_nIdOfKeyEC);
206     }
207 }
208 
209 /* XBlockerMonitor */
210 void SAL_CALL EncryptionEngine::setBlockerId( sal_Int32 id )
211     throw (com::sun::star::uno::Exception, com::sun::star::uno::RuntimeException)
212 {
213     m_nIdOfBlocker = id;
214     tryToPerform();
215 }
216 
217