Bump version to 5.0-14
[LibreOffice.git] / xmlsecurity / source / framework / encryptorimpl.cxx
blob776b5327f3a6e5d4f6a1fc6733d8274e421bd542
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
25 #include <comphelper/processfactory.hxx>
26 #include <cppuhelper/supportsservice.hxx>
27 #include <osl/diagnose.h>
29 using namespace com::sun::star::uno;
30 namespace cssl = com::sun::star::lang;
31 namespace cssxc = com::sun::star::xml::crypto;
32 namespace cssxw = com::sun::star::xml::wrapper;
34 #define IMPLEMENTATION_NAME "com.sun.star.xml.security.framework.EncryptorImpl"
36 EncryptorImpl::EncryptorImpl(const Reference<XComponentContext> & xContext) : EncryptorImpl_Base(xContext)
38 m_nReferenceId = -1;
41 EncryptorImpl::~EncryptorImpl()
45 bool EncryptorImpl::checkReady() const
46 /****** EncryptorImpl/checkReady *********************************************
48 * NAME
49 * checkReady -- checks the conditions for the encryption.
51 * SYNOPSIS
52 * bReady = checkReady( );
54 * FUNCTION
55 * checks whether all following conditions are satisfied:
56 * 1. the result listener is ready;
57 * 2. the EncryptionEngine is ready.
59 * INPUTS
60 * empty
62 * RESULT
63 * bReady - true if all conditions are satisfied, false otherwise
65 * AUTHOR
66 * Michael Mi
67 * Email: michael.mi@sun.com
68 ******************************************************************************/
70 sal_Int32 nKeyInc = 0;
71 if (m_nIdOfKeyEC != 0)
73 nKeyInc = 1;
76 return (m_xResultListener.is() &&
77 (m_nReferenceId != -1) &&
78 (2+nKeyInc == m_nNumOfResolvedReferences) &&
79 EncryptionEngine::checkReady());
82 void EncryptorImpl::notifyResultListener() const
83 throw (Exception, RuntimeException)
84 /****** DecryptorImpl/notifyResultListener ***********************************
86 * NAME
87 * notifyResultListener -- notifies the listener about the encryption
88 * result.
90 * SYNOPSIS
91 * notifyResultListener( );
93 * FUNCTION
94 * see NAME.
96 * INPUTS
97 * empty
99 * RESULT
100 * empty
102 * AUTHOR
103 * Michael Mi
104 * Email: michael.mi@sun.com
105 ******************************************************************************/
107 Reference< cssxc::sax::XEncryptionResultListener >
108 xEncryptionResultListener ( m_xResultListener , UNO_QUERY ) ;
110 xEncryptionResultListener->encrypted( m_nSecurityId, m_nStatus );
113 void EncryptorImpl::startEngine( const Reference<
114 cssxc::XXMLEncryptionTemplate >&
115 xEncryptionTemplate)
116 throw (Exception, RuntimeException)
117 /****** EncryptorImpl/startEngine ********************************************
119 * NAME
120 * startEngine -- generates the encryption.
122 * SYNOPSIS
123 * startEngine( xEncryptionTemplate );
125 * FUNCTION
126 * generates the encryption element, then if succeeds, updates the link
127 * of old template element to the new encryption element in
128 * SAXEventKeeper.
130 * INPUTS
131 * xEncryptionTemplate - the encryption template to be encrypted.
133 * RESULT
134 * empty
136 * AUTHOR
137 * Michael Mi
138 * Email: michael.mi@sun.com
139 ******************************************************************************/
141 Reference < cssxc::XXMLEncryptionTemplate > xResultTemplate;
143 Reference< cssxw::XXMLElementWrapper >
144 xXMLElement = m_xSAXEventKeeper->getElement( m_nReferenceId );
145 xEncryptionTemplate->setTarget(xXMLElement);
149 xResultTemplate = m_xXMLEncryption->encrypt(
150 xEncryptionTemplate, m_xSecurityEnvironment);
151 m_nStatus = xResultTemplate->getStatus();
153 catch( Exception& )
155 m_nStatus = cssxc::SecurityOperationStatus_RUNTIMEERROR_FAILED;
158 if (m_nStatus == cssxc::SecurityOperationStatus_OPERATION_SUCCEEDED)
160 Reference < cssxw::XXMLElementWrapper > xResultEncryption
161 = xResultTemplate->getTemplate();
162 m_xSAXEventKeeper->setElement(m_nIdOfTemplateEC, xResultEncryption);
163 m_xSAXEventKeeper->setElement(m_nReferenceId, NULL);
167 /* XReferenceCollector */
168 void SAL_CALL EncryptorImpl::setReferenceCount(sal_Int32)
169 throw (Exception, RuntimeException, std::exception)
172 * dummp method, because there is only one reference in
173 * encryption, different from signature.
174 * so the referenceNumber is always 1
178 void SAL_CALL EncryptorImpl::setReferenceId( sal_Int32 id )
179 throw (Exception, RuntimeException, std::exception)
181 m_nReferenceId = id;
184 /* XEncryptionResultBroadcaster */
185 void SAL_CALL EncryptorImpl::addEncryptionResultListener( const Reference< cssxc::sax::XEncryptionResultListener >& listener )
186 throw (Exception, RuntimeException, std::exception)
188 m_xResultListener = listener;
189 tryToPerform();
192 void SAL_CALL EncryptorImpl::removeEncryptionResultListener( const Reference< cssxc::sax::XEncryptionResultListener >&)
193 throw (RuntimeException, std::exception)
197 /* XInitialization */
198 void SAL_CALL EncryptorImpl::initialize( const Sequence< Any >& aArguments )
199 throw (Exception, RuntimeException, std::exception)
201 OSL_ASSERT(aArguments.getLength() == 5);
203 OUString ouTempString;
205 aArguments[0] >>= ouTempString;
206 m_nSecurityId = ouTempString.toInt32();
207 aArguments[1] >>= m_xSAXEventKeeper;
208 aArguments[2] >>= ouTempString;
209 m_nIdOfTemplateEC = ouTempString.toInt32();
210 aArguments[3] >>= m_xSecurityEnvironment;
211 aArguments[4] >>= m_xXMLEncryption;
215 OUString EncryptorImpl_getImplementationName ()
216 throw (RuntimeException)
218 return OUString ( IMPLEMENTATION_NAME );
221 Sequence< OUString > SAL_CALL EncryptorImpl_getSupportedServiceNames( )
222 throw (RuntimeException)
224 Sequence < OUString > aRet(1);
225 OUString* pArray = aRet.getArray();
226 pArray[0] = "com.sun.star.xml.crypto.sax.Encryptor";
227 return aRet;
230 Reference< XInterface > SAL_CALL EncryptorImpl_createInstance(
231 const Reference< cssl::XMultiServiceFactory >& xMSF)
232 throw( Exception )
234 return (cppu::OWeakObject*) new EncryptorImpl( comphelper::getComponentContext( xMSF ) );
237 /* XServiceInfo */
238 OUString SAL_CALL EncryptorImpl::getImplementationName( )
239 throw (RuntimeException, std::exception)
241 return EncryptorImpl_getImplementationName();
244 sal_Bool SAL_CALL EncryptorImpl::supportsService( const OUString& rServiceName )
245 throw (RuntimeException, std::exception)
247 return cppu::supportsService(this, rServiceName);
250 Sequence< OUString > SAL_CALL EncryptorImpl::getSupportedServiceNames( )
251 throw (RuntimeException, std::exception)
253 return EncryptorImpl_getSupportedServiceNames();
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */