1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "encryptorimpl.hxx"
22 #include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp>
23 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 namespace cssu
= com::sun::star::uno
;
27 namespace cssl
= com::sun::star::lang
;
28 namespace cssxc
= com::sun::star::xml::crypto
;
29 namespace cssxw
= com::sun::star::xml::wrapper
;
31 #define SERVICE_NAME "com.sun.star.xml.crypto.sax.Encryptor"
32 #define IMPLEMENTATION_NAME "com.sun.star.xml.security.framework.EncryptorImpl"
34 EncryptorImpl::EncryptorImpl( const cssu::Reference
< cssl::XMultiServiceFactory
>& rxMSF
)
40 EncryptorImpl::~EncryptorImpl()
44 bool EncryptorImpl::checkReady() const
45 /****** EncryptorImpl/checkReady *********************************************
48 * checkReady -- checks the conditions for the encryption.
51 * bReady = checkReady( );
54 * checks whether all following conditions are satisfied:
55 * 1. the result listener is ready;
56 * 2. the EncryptionEngine is ready.
62 * bReady - true if all conditions are satisfied, false otherwise
66 * Email: michael.mi@sun.com
67 ******************************************************************************/
69 sal_Int32 nKeyInc
= 0;
70 if (m_nIdOfKeyEC
!= 0)
75 return (m_xResultListener
.is() &&
76 (m_nReferenceId
!= -1) &&
77 (2+nKeyInc
== m_nNumOfResolvedReferences
) &&
78 EncryptionEngine::checkReady());
81 void EncryptorImpl::notifyResultListener() const
82 throw (cssu::Exception
, cssu::RuntimeException
)
83 /****** DecryptorImpl/notifyResultListener ***********************************
86 * notifyResultListener -- notifies the listener about the encryption
90 * notifyResultListener( );
103 * Email: michael.mi@sun.com
104 ******************************************************************************/
106 cssu::Reference
< cssxc::sax::XEncryptionResultListener
>
107 xEncryptionResultListener ( m_xResultListener
, cssu::UNO_QUERY
) ;
109 xEncryptionResultListener
->encrypted( m_nSecurityId
, m_nStatus
);
112 void EncryptorImpl::startEngine( const cssu::Reference
<
113 cssxc::XXMLEncryptionTemplate
>&
115 throw (cssu::Exception
, cssu::RuntimeException
)
116 /****** EncryptorImpl/startEngine ********************************************
119 * startEngine -- generates the encryption.
122 * startEngine( xEncryptionTemplate );
125 * generates the encryption element, then if succeeds, updates the link
126 * of old template element to the new encryption element in
130 * xEncryptionTemplate - the encryption template to be encrypted.
137 * Email: michael.mi@sun.com
138 ******************************************************************************/
140 cssu::Reference
< cssxc::XXMLEncryptionTemplate
> xResultTemplate
;
142 cssu::Reference
< cssxw::XXMLElementWrapper
>
143 xXMLElement
= m_xSAXEventKeeper
->getElement( m_nReferenceId
);
144 xEncryptionTemplate
->setTarget(xXMLElement
);
148 xResultTemplate
= m_xXMLEncryption
->encrypt(
149 xEncryptionTemplate
, m_xSecurityEnvironment
);
150 m_nStatus
= xResultTemplate
->getStatus();
152 catch( cssu::Exception
& )
154 m_nStatus
= cssxc::SecurityOperationStatus_RUNTIMEERROR_FAILED
;
157 if (m_nStatus
== cssxc::SecurityOperationStatus_OPERATION_SUCCEEDED
)
159 cssu::Reference
< cssxw::XXMLElementWrapper
> xResultEncryption
160 = xResultTemplate
->getTemplate();
161 m_xSAXEventKeeper
->setElement(m_nIdOfTemplateEC
, xResultEncryption
);
162 m_xSAXEventKeeper
->setElement(m_nReferenceId
, NULL
);
166 /* XReferenceCollector */
167 void SAL_CALL
EncryptorImpl::setReferenceCount(sal_Int32
)
168 throw (cssu::Exception
, cssu::RuntimeException
)
171 * dummp method, because there is only one reference in
172 * encryption, different from signature.
173 * so the referenceNumber is always 1
177 void SAL_CALL
EncryptorImpl::setReferenceId( sal_Int32 id
)
178 throw (cssu::Exception
, cssu::RuntimeException
)
183 /* XEncryptionResultBroadcaster */
184 void SAL_CALL
EncryptorImpl::addEncryptionResultListener( const cssu::Reference
< cssxc::sax::XEncryptionResultListener
>& listener
)
185 throw (cssu::Exception
, cssu::RuntimeException
)
187 m_xResultListener
= listener
;
191 void SAL_CALL
EncryptorImpl::removeEncryptionResultListener( const cssu::Reference
< cssxc::sax::XEncryptionResultListener
>&)
192 throw (cssu::RuntimeException
)
196 /* XInitialization */
197 void SAL_CALL
EncryptorImpl::initialize( const cssu::Sequence
< cssu::Any
>& aArguments
)
198 throw (cssu::Exception
, cssu::RuntimeException
)
200 OSL_ASSERT(aArguments
.getLength() == 5);
202 OUString ouTempString
;
204 aArguments
[0] >>= ouTempString
;
205 m_nSecurityId
= ouTempString
.toInt32();
206 aArguments
[1] >>= m_xSAXEventKeeper
;
207 aArguments
[2] >>= ouTempString
;
208 m_nIdOfTemplateEC
= ouTempString
.toInt32();
209 aArguments
[3] >>= m_xSecurityEnvironment
;
210 aArguments
[4] >>= m_xXMLEncryption
;
214 OUString
EncryptorImpl_getImplementationName ()
215 throw (cssu::RuntimeException
)
217 return OUString ( IMPLEMENTATION_NAME
);
220 sal_Bool SAL_CALL
EncryptorImpl_supportsService( const OUString
& ServiceName
)
221 throw (cssu::RuntimeException
)
223 return ServiceName
== SERVICE_NAME
;
226 cssu::Sequence
< OUString
> SAL_CALL
EncryptorImpl_getSupportedServiceNames( )
227 throw (cssu::RuntimeException
)
229 cssu::Sequence
< OUString
> aRet(1);
230 OUString
* pArray
= aRet
.getArray();
231 pArray
[0] = OUString ( SERVICE_NAME
);
236 cssu::Reference
< cssu::XInterface
> SAL_CALL
EncryptorImpl_createInstance(
237 const cssu::Reference
< cssl::XMultiServiceFactory
>& rSMgr
)
238 throw( cssu::Exception
)
240 return (cppu::OWeakObject
*) new EncryptorImpl(rSMgr
);
244 OUString SAL_CALL
EncryptorImpl::getImplementationName( )
245 throw (cssu::RuntimeException
)
247 return EncryptorImpl_getImplementationName();
249 sal_Bool SAL_CALL
EncryptorImpl::supportsService( const OUString
& rServiceName
)
250 throw (cssu::RuntimeException
)
252 return EncryptorImpl_supportsService( rServiceName
);
254 cssu::Sequence
< OUString
> SAL_CALL
EncryptorImpl::getSupportedServiceNames( )
255 throw (cssu::RuntimeException
)
257 return EncryptorImpl_getSupportedServiceNames();
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */