xref: /trunk/main/xmlsecurity/source/framework/encryptorimpl.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 "encryptorimpl.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 SERVICE_NAME "com.sun.star.xml.crypto.sax.Encryptor"
38 #define IMPLEMENTATION_NAME "com.sun.star.xml.security.framework.EncryptorImpl"
39 
40 #define DECLARE_ASCII( SASCIIVALUE )                                                                            \
41     rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SASCIIVALUE ) )
42 
EncryptorImpl(const cssu::Reference<cssl::XMultiServiceFactory> & rxMSF)43 EncryptorImpl::EncryptorImpl( const cssu::Reference< cssl::XMultiServiceFactory >& rxMSF)
44 {
45     m_nReferenceId = -1;
46     mxMSF = rxMSF;
47 }
48 
~EncryptorImpl()49 EncryptorImpl::~EncryptorImpl()
50 {
51 }
52 
checkReady() const53 bool EncryptorImpl::checkReady() const
54 /****** EncryptorImpl/checkReady *********************************************
55  *
56  *   NAME
57  *  checkReady -- checks the conditions for the encryption.
58  *
59  *   SYNOPSIS
60  *  bReady = checkReady( );
61  *
62  *   FUNCTION
63  *  checks whether all following conditions are satisfied:
64  *  1. the result listener is ready;
65  *  2. the EncryptionEngine is ready.
66  *
67  *   INPUTS
68  *  empty
69  *
70  *   RESULT
71  *  bReady - true if all conditions are satisfied, false otherwise
72  *
73  *   HISTORY
74  *  05.01.2004 -    implemented
75  *
76  *   AUTHOR
77  *  Michael Mi
78  *  Email: michael.mi@sun.com
79  ******************************************************************************/
80 {
81     sal_Int32 nKeyInc = 0;
82     if (m_nIdOfKeyEC != 0)
83     {
84         nKeyInc = 1;
85     }
86 
87     return (m_xResultListener.is() &&
88         (m_nReferenceId != -1) &&
89         (2+nKeyInc == m_nNumOfResolvedReferences) &&
90         EncryptionEngine::checkReady());
91 }
92 
notifyResultListener() const93 void EncryptorImpl::notifyResultListener() const
94 /****** DecryptorImpl/notifyResultListener ***********************************
95  *
96  *   NAME
97  *  notifyResultListener -- notifies the listener about the encryption
98  *  result.
99  *
100  *   SYNOPSIS
101  *  notifyResultListener( );
102  *
103  *   FUNCTION
104  *  see NAME.
105  *
106  *   INPUTS
107  *  empty
108  *
109  *   RESULT
110  *  empty
111  *
112  *   HISTORY
113  *  05.01.2004 -    implemented
114  *
115  *   AUTHOR
116  *  Michael Mi
117  *  Email: michael.mi@sun.com
118  ******************************************************************************/
119 {
120     cssu::Reference< cssxc::sax::XEncryptionResultListener >
121         xEncryptionResultListener ( m_xResultListener , cssu::UNO_QUERY ) ;
122 
123     xEncryptionResultListener->encrypted( m_nSecurityId, m_nStatus );
124 }
125 
startEngine(const cssu::Reference<cssxc::XXMLEncryptionTemplate> & xEncryptionTemplate)126 void EncryptorImpl::startEngine( const cssu::Reference<
127     cssxc::XXMLEncryptionTemplate >&
128     xEncryptionTemplate)
129 /****** EncryptorImpl/startEngine ********************************************
130  *
131  *   NAME
132  *  startEngine -- generates the encryption.
133  *
134  *   SYNOPSIS
135  *  startEngine( xEncryptionTemplate );
136  *
137  *   FUNCTION
138  *  generates the encryption element, then if succeeds, updates the link
139  *  of old template element to the new encryption element in
140  *  SAXEventKeeper.
141  *
142  *   INPUTS
143  *  xEncryptionTemplate - the encryption template to be encrypted.
144  *
145  *   RESULT
146  *  empty
147  *
148  *   HISTORY
149  *  05.01.2004 -    implemented
150  *
151  *   AUTHOR
152  *  Michael Mi
153  *  Email: michael.mi@sun.com
154  ******************************************************************************/
155 {
156     cssu::Reference < cssxc::XXMLEncryptionTemplate > xResultTemplate;
157 
158     cssu::Reference< cssxw::XXMLElementWrapper >
159         xXMLElement = m_xSAXEventKeeper->getElement( m_nReferenceId );
160     xEncryptionTemplate->setTarget(xXMLElement);
161 
162     try
163     {
164         xResultTemplate = m_xXMLEncryption->encrypt(
165             xEncryptionTemplate, m_xSecurityEnvironment);
166         m_nStatus = xResultTemplate->getStatus();
167     }
168     catch( cssu::Exception& )
169     {
170         m_nStatus = cssxc::SecurityOperationStatus_RUNTIMEERROR_FAILED;
171     }
172 
173     if (m_nStatus == cssxc::SecurityOperationStatus_OPERATION_SUCCEEDED)
174     {
175         cssu::Reference < cssxw::XXMLElementWrapper > xResultEncryption
176             = xResultTemplate->getTemplate();
177         m_xSAXEventKeeper->setElement(m_nIdOfTemplateEC, xResultEncryption);
178         m_xSAXEventKeeper->setElement(m_nReferenceId, NULL);
179     }
180 }
181 
182 /* XReferenceCollector */
setReferenceCount(sal_Int32)183 void SAL_CALL EncryptorImpl::setReferenceCount(sal_Int32)
184 {
185     /*
186      * dummp method, because there is only one reference in
187      * encryption, different from signature.
188      * so the referenceNumber is always 1
189      */
190 }
191 
setReferenceId(sal_Int32 id)192 void SAL_CALL EncryptorImpl::setReferenceId( sal_Int32 id )
193 {
194     m_nReferenceId = id;
195 }
196 
197 /* XEncryptionResultBroadcaster */
addEncryptionResultListener(const cssu::Reference<cssxc::sax::XEncryptionResultListener> & listener)198 void SAL_CALL EncryptorImpl::addEncryptionResultListener( const cssu::Reference< cssxc::sax::XEncryptionResultListener >& listener )
199 {
200     m_xResultListener = listener;
201     tryToPerform();
202 }
203 
removeEncryptionResultListener(const cssu::Reference<cssxc::sax::XEncryptionResultListener> &)204 void SAL_CALL EncryptorImpl::removeEncryptionResultListener( const cssu::Reference< cssxc::sax::XEncryptionResultListener >&)
205 {
206 }
207 
208 /* XInitialization */
initialize(const cssu::Sequence<cssu::Any> & aArguments)209 void SAL_CALL EncryptorImpl::initialize( const cssu::Sequence< cssu::Any >& aArguments )
210 {
211     OSL_ASSERT(aArguments.getLength() == 5);
212 
213     rtl::OUString ouTempString;
214 
215     aArguments[0] >>= ouTempString;
216     m_nSecurityId = ouTempString.toInt32();
217     aArguments[1] >>= m_xSAXEventKeeper;
218     aArguments[2] >>= ouTempString;
219     m_nIdOfTemplateEC = ouTempString.toInt32();
220     aArguments[3] >>= m_xSecurityEnvironment;
221     aArguments[4] >>= m_xXMLEncryption;
222 }
223 
224 
EncryptorImpl_getImplementationName()225 rtl::OUString EncryptorImpl_getImplementationName ()
226 {
227     return rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) );
228 }
229 
EncryptorImpl_supportsService(const rtl::OUString & ServiceName)230 sal_Bool SAL_CALL EncryptorImpl_supportsService( const rtl::OUString& ServiceName )
231 {
232     return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ));
233 }
234 
EncryptorImpl_getSupportedServiceNames()235 cssu::Sequence< rtl::OUString > SAL_CALL EncryptorImpl_getSupportedServiceNames(  )
236 {
237     cssu::Sequence < rtl::OUString > aRet(1);
238     rtl::OUString* pArray = aRet.getArray();
239     pArray[0] =  rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
240     return aRet;
241 }
242 #undef SERVICE_NAME
243 
EncryptorImpl_createInstance(const cssu::Reference<cssl::XMultiServiceFactory> & rSMgr)244 cssu::Reference< cssu::XInterface > SAL_CALL EncryptorImpl_createInstance(
245     const cssu::Reference< cssl::XMultiServiceFactory >& rSMgr)
246 {
247     return (cppu::OWeakObject*) new EncryptorImpl(rSMgr);
248 }
249 
250 /* XServiceInfo */
getImplementationName()251 rtl::OUString SAL_CALL EncryptorImpl::getImplementationName(  )
252 {
253     return EncryptorImpl_getImplementationName();
254 }
supportsService(const rtl::OUString & rServiceName)255 sal_Bool SAL_CALL EncryptorImpl::supportsService( const rtl::OUString& rServiceName )
256 {
257     return EncryptorImpl_supportsService( rServiceName );
258 }
getSupportedServiceNames()259 cssu::Sequence< rtl::OUString > SAL_CALL EncryptorImpl::getSupportedServiceNames(  )
260 {
261     return EncryptorImpl_getSupportedServiceNames();
262 }
263