Update ooo320-m1
[ooovba.git] / xmlsecurity / source / framework / encryptorimpl.cxx
blobac466a4feddecda98b6c6612720ab390de824b2d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: encryptorimpl.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
34 #include "encryptorimpl.hxx"
35 #include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp>
36 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39 namespace cssu = com::sun::star::uno;
40 namespace cssl = com::sun::star::lang;
41 namespace cssxc = com::sun::star::xml::crypto;
42 namespace cssxw = com::sun::star::xml::wrapper;
44 #define SERVICE_NAME "com.sun.star.xml.crypto.sax.Encryptor"
45 #define IMPLEMENTATION_NAME "com.sun.star.xml.security.framework.EncryptorImpl"
47 #define DECLARE_ASCII( SASCIIVALUE ) \
48 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SASCIIVALUE ) )
50 EncryptorImpl::EncryptorImpl( const cssu::Reference< cssl::XMultiServiceFactory >& rxMSF)
52 m_nReferenceId = -1;
53 mxMSF = rxMSF;
56 EncryptorImpl::~EncryptorImpl()
60 bool EncryptorImpl::checkReady() const
61 /****** EncryptorImpl/checkReady *********************************************
63 * NAME
64 * checkReady -- checks the conditions for the encryption.
66 * SYNOPSIS
67 * bReady = checkReady( );
69 * FUNCTION
70 * checks whether all following conditions are satisfied:
71 * 1. the result listener is ready;
72 * 2. the EncryptionEngine is ready.
74 * INPUTS
75 * empty
77 * RESULT
78 * bReady - true if all conditions are satisfied, false otherwise
80 * HISTORY
81 * 05.01.2004 - implemented
83 * AUTHOR
84 * Michael Mi
85 * Email: michael.mi@sun.com
86 ******************************************************************************/
88 sal_Int32 nKeyInc = 0;
89 if (m_nIdOfKeyEC != 0)
91 nKeyInc = 1;
94 return (m_xResultListener.is() &&
95 (m_nReferenceId != -1) &&
96 (2+nKeyInc == m_nNumOfResolvedReferences) &&
97 EncryptionEngine::checkReady());
100 void EncryptorImpl::notifyResultListener() const
101 throw (cssu::Exception, cssu::RuntimeException)
102 /****** DecryptorImpl/notifyResultListener ***********************************
104 * NAME
105 * notifyResultListener -- notifies the listener about the encryption
106 * result.
108 * SYNOPSIS
109 * notifyResultListener( );
111 * FUNCTION
112 * see NAME.
114 * INPUTS
115 * empty
117 * RESULT
118 * empty
120 * HISTORY
121 * 05.01.2004 - implemented
123 * AUTHOR
124 * Michael Mi
125 * Email: michael.mi@sun.com
126 ******************************************************************************/
128 cssu::Reference< cssxc::sax::XEncryptionResultListener >
129 xEncryptionResultListener ( m_xResultListener , cssu::UNO_QUERY ) ;
131 xEncryptionResultListener->encrypted( m_nSecurityId, m_nStatus );
134 void EncryptorImpl::startEngine( const cssu::Reference<
135 cssxc::XXMLEncryptionTemplate >&
136 xEncryptionTemplate)
137 throw (cssu::Exception, cssu::RuntimeException)
138 /****** EncryptorImpl/startEngine ********************************************
140 * NAME
141 * startEngine -- generates the encryption.
143 * SYNOPSIS
144 * startEngine( xEncryptionTemplate );
146 * FUNCTION
147 * generates the encryption element, then if succeeds, updates the link
148 * of old template element to the new encryption element in
149 * SAXEventKeeper.
151 * INPUTS
152 * xEncryptionTemplate - the encryption template to be encrypted.
154 * RESULT
155 * empty
157 * HISTORY
158 * 05.01.2004 - implemented
160 * AUTHOR
161 * Michael Mi
162 * Email: michael.mi@sun.com
163 ******************************************************************************/
165 cssu::Reference < cssxc::XXMLEncryptionTemplate > xResultTemplate;
167 cssu::Reference< cssxw::XXMLElementWrapper >
168 xXMLElement = m_xSAXEventKeeper->getElement( m_nReferenceId );
169 xEncryptionTemplate->setTarget(xXMLElement);
171 try
173 xResultTemplate = m_xXMLEncryption->encrypt(
174 xEncryptionTemplate, m_xSecurityEnvironment);
175 m_nStatus = xResultTemplate->getStatus();
177 catch( cssu::Exception& )
179 m_nStatus = cssxc::SecurityOperationStatus_RUNTIMEERROR_FAILED;
182 if (m_nStatus == cssxc::SecurityOperationStatus_OPERATION_SUCCEEDED)
184 cssu::Reference < cssxw::XXMLElementWrapper > xResultEncryption
185 = xResultTemplate->getTemplate();
186 m_xSAXEventKeeper->setElement(m_nIdOfTemplateEC, xResultEncryption);
187 m_xSAXEventKeeper->setElement(m_nReferenceId, NULL);
191 /* XReferenceCollector */
192 void SAL_CALL EncryptorImpl::setReferenceCount(sal_Int32)
193 throw (cssu::Exception, cssu::RuntimeException)
196 * dummp method, because there is only one reference in
197 * encryption, different from signature.
198 * so the referenceNumber is always 1
202 void SAL_CALL EncryptorImpl::setReferenceId( sal_Int32 id )
203 throw (cssu::Exception, cssu::RuntimeException)
205 m_nReferenceId = id;
208 /* XEncryptionResultBroadcaster */
209 void SAL_CALL EncryptorImpl::addEncryptionResultListener( const cssu::Reference< cssxc::sax::XEncryptionResultListener >& listener )
210 throw (cssu::Exception, cssu::RuntimeException)
212 m_xResultListener = listener;
213 tryToPerform();
216 void SAL_CALL EncryptorImpl::removeEncryptionResultListener( const cssu::Reference< cssxc::sax::XEncryptionResultListener >&)
217 throw (cssu::RuntimeException)
221 /* XInitialization */
222 void SAL_CALL EncryptorImpl::initialize( const cssu::Sequence< cssu::Any >& aArguments )
223 throw (cssu::Exception, cssu::RuntimeException)
225 OSL_ASSERT(aArguments.getLength() == 5);
227 rtl::OUString ouTempString;
229 aArguments[0] >>= ouTempString;
230 m_nSecurityId = ouTempString.toInt32();
231 aArguments[1] >>= m_xSAXEventKeeper;
232 aArguments[2] >>= ouTempString;
233 m_nIdOfTemplateEC = ouTempString.toInt32();
234 aArguments[3] >>= m_xSecurityEnvironment;
235 aArguments[4] >>= m_xXMLEncryption;
239 rtl::OUString EncryptorImpl_getImplementationName ()
240 throw (cssu::RuntimeException)
242 return rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) );
245 sal_Bool SAL_CALL EncryptorImpl_supportsService( const rtl::OUString& ServiceName )
246 throw (cssu::RuntimeException)
248 return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ));
251 cssu::Sequence< rtl::OUString > SAL_CALL EncryptorImpl_getSupportedServiceNames( )
252 throw (cssu::RuntimeException)
254 cssu::Sequence < rtl::OUString > aRet(1);
255 rtl::OUString* pArray = aRet.getArray();
256 pArray[0] = rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
257 return aRet;
259 #undef SERVICE_NAME
261 cssu::Reference< cssu::XInterface > SAL_CALL EncryptorImpl_createInstance(
262 const cssu::Reference< cssl::XMultiServiceFactory >& rSMgr)
263 throw( cssu::Exception )
265 return (cppu::OWeakObject*) new EncryptorImpl(rSMgr);
268 /* XServiceInfo */
269 rtl::OUString SAL_CALL EncryptorImpl::getImplementationName( )
270 throw (cssu::RuntimeException)
272 return EncryptorImpl_getImplementationName();
274 sal_Bool SAL_CALL EncryptorImpl::supportsService( const rtl::OUString& rServiceName )
275 throw (cssu::RuntimeException)
277 return EncryptorImpl_supportsService( rServiceName );
279 cssu::Sequence< rtl::OUString > SAL_CALL EncryptorImpl::getSupportedServiceNames( )
280 throw (cssu::RuntimeException)
282 return EncryptorImpl_getSupportedServiceNames();