1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
31 #include "encryptorimpl.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>
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
;
41 #define SERVICE_NAME "com.sun.star.xml.crypto.sax.Encryptor"
42 #define IMPLEMENTATION_NAME "com.sun.star.xml.security.framework.EncryptorImpl"
44 #define DECLARE_ASCII( SASCIIVALUE ) \
45 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SASCIIVALUE ) )
47 EncryptorImpl::EncryptorImpl( const cssu::Reference
< cssl::XMultiServiceFactory
>& rxMSF
)
53 EncryptorImpl::~EncryptorImpl()
57 bool EncryptorImpl::checkReady() const
58 /****** EncryptorImpl/checkReady *********************************************
61 * checkReady -- checks the conditions for the encryption.
64 * bReady = checkReady( );
67 * checks whether all following conditions are satisfied:
68 * 1. the result listener is ready;
69 * 2. the EncryptionEngine is ready.
75 * bReady - true if all conditions are satisfied, false otherwise
78 * 05.01.2004 - implemented
82 * Email: michael.mi@sun.com
83 ******************************************************************************/
85 sal_Int32 nKeyInc
= 0;
86 if (m_nIdOfKeyEC
!= 0)
91 return (m_xResultListener
.is() &&
92 (m_nReferenceId
!= -1) &&
93 (2+nKeyInc
== m_nNumOfResolvedReferences
) &&
94 EncryptionEngine::checkReady());
97 void EncryptorImpl::notifyResultListener() const
98 throw (cssu::Exception
, cssu::RuntimeException
)
99 /****** DecryptorImpl/notifyResultListener ***********************************
102 * notifyResultListener -- notifies the listener about the encryption
106 * notifyResultListener( );
118 * 05.01.2004 - implemented
122 * Email: michael.mi@sun.com
123 ******************************************************************************/
125 cssu::Reference
< cssxc::sax::XEncryptionResultListener
>
126 xEncryptionResultListener ( m_xResultListener
, cssu::UNO_QUERY
) ;
128 xEncryptionResultListener
->encrypted( m_nSecurityId
, m_nStatus
);
131 void EncryptorImpl::startEngine( const cssu::Reference
<
132 cssxc::XXMLEncryptionTemplate
>&
134 throw (cssu::Exception
, cssu::RuntimeException
)
135 /****** EncryptorImpl/startEngine ********************************************
138 * startEngine -- generates the encryption.
141 * startEngine( xEncryptionTemplate );
144 * generates the encryption element, then if succeeds, updates the link
145 * of old template element to the new encryption element in
149 * xEncryptionTemplate - the encryption template to be encrypted.
155 * 05.01.2004 - implemented
159 * Email: michael.mi@sun.com
160 ******************************************************************************/
162 cssu::Reference
< cssxc::XXMLEncryptionTemplate
> xResultTemplate
;
164 cssu::Reference
< cssxw::XXMLElementWrapper
>
165 xXMLElement
= m_xSAXEventKeeper
->getElement( m_nReferenceId
);
166 xEncryptionTemplate
->setTarget(xXMLElement
);
170 xResultTemplate
= m_xXMLEncryption
->encrypt(
171 xEncryptionTemplate
, m_xSecurityEnvironment
);
172 m_nStatus
= xResultTemplate
->getStatus();
174 catch( cssu::Exception
& )
176 m_nStatus
= cssxc::SecurityOperationStatus_RUNTIMEERROR_FAILED
;
179 if (m_nStatus
== cssxc::SecurityOperationStatus_OPERATION_SUCCEEDED
)
181 cssu::Reference
< cssxw::XXMLElementWrapper
> xResultEncryption
182 = xResultTemplate
->getTemplate();
183 m_xSAXEventKeeper
->setElement(m_nIdOfTemplateEC
, xResultEncryption
);
184 m_xSAXEventKeeper
->setElement(m_nReferenceId
, NULL
);
188 /* XReferenceCollector */
189 void SAL_CALL
EncryptorImpl::setReferenceCount(sal_Int32
)
190 throw (cssu::Exception
, cssu::RuntimeException
)
193 * dummp method, because there is only one reference in
194 * encryption, different from signature.
195 * so the referenceNumber is always 1
199 void SAL_CALL
EncryptorImpl::setReferenceId( sal_Int32 id
)
200 throw (cssu::Exception
, cssu::RuntimeException
)
205 /* XEncryptionResultBroadcaster */
206 void SAL_CALL
EncryptorImpl::addEncryptionResultListener( const cssu::Reference
< cssxc::sax::XEncryptionResultListener
>& listener
)
207 throw (cssu::Exception
, cssu::RuntimeException
)
209 m_xResultListener
= listener
;
213 void SAL_CALL
EncryptorImpl::removeEncryptionResultListener( const cssu::Reference
< cssxc::sax::XEncryptionResultListener
>&)
214 throw (cssu::RuntimeException
)
218 /* XInitialization */
219 void SAL_CALL
EncryptorImpl::initialize( const cssu::Sequence
< cssu::Any
>& aArguments
)
220 throw (cssu::Exception
, cssu::RuntimeException
)
222 OSL_ASSERT(aArguments
.getLength() == 5);
224 rtl::OUString ouTempString
;
226 aArguments
[0] >>= ouTempString
;
227 m_nSecurityId
= ouTempString
.toInt32();
228 aArguments
[1] >>= m_xSAXEventKeeper
;
229 aArguments
[2] >>= ouTempString
;
230 m_nIdOfTemplateEC
= ouTempString
.toInt32();
231 aArguments
[3] >>= m_xSecurityEnvironment
;
232 aArguments
[4] >>= m_xXMLEncryption
;
236 rtl::OUString
EncryptorImpl_getImplementationName ()
237 throw (cssu::RuntimeException
)
239 return rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME
) );
242 sal_Bool SAL_CALL
EncryptorImpl_supportsService( const rtl::OUString
& ServiceName
)
243 throw (cssu::RuntimeException
)
245 return ServiceName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME
));
248 cssu::Sequence
< rtl::OUString
> SAL_CALL
EncryptorImpl_getSupportedServiceNames( )
249 throw (cssu::RuntimeException
)
251 cssu::Sequence
< rtl::OUString
> aRet(1);
252 rtl::OUString
* pArray
= aRet
.getArray();
253 pArray
[0] = rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME
) );
258 cssu::Reference
< cssu::XInterface
> SAL_CALL
EncryptorImpl_createInstance(
259 const cssu::Reference
< cssl::XMultiServiceFactory
>& rSMgr
)
260 throw( cssu::Exception
)
262 return (cppu::OWeakObject
*) new EncryptorImpl(rSMgr
);
266 rtl::OUString SAL_CALL
EncryptorImpl::getImplementationName( )
267 throw (cssu::RuntimeException
)
269 return EncryptorImpl_getImplementationName();
271 sal_Bool SAL_CALL
EncryptorImpl::supportsService( const rtl::OUString
& rServiceName
)
272 throw (cssu::RuntimeException
)
274 return EncryptorImpl_supportsService( rServiceName
);
276 cssu::Sequence
< rtl::OUString
> SAL_CALL
EncryptorImpl::getSupportedServiceNames( )
277 throw (cssu::RuntimeException
)
279 return EncryptorImpl_getSupportedServiceNames();