Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
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 :
28 : using namespace com::sun::star::uno;
29 : namespace cssl = com::sun::star::lang;
30 : namespace cssxc = com::sun::star::xml::crypto;
31 : namespace cssxw = com::sun::star::xml::wrapper;
32 :
33 : #define SERVICE_NAME "com.sun.star.xml.crypto.sax.Encryptor"
34 : #define IMPLEMENTATION_NAME "com.sun.star.xml.security.framework.EncryptorImpl"
35 :
36 0 : EncryptorImpl::EncryptorImpl(const Reference<XComponentContext> & xContext) : EncryptorImpl_Base(xContext)
37 : {
38 0 : m_nReferenceId = -1;
39 0 : }
40 :
41 0 : EncryptorImpl::~EncryptorImpl()
42 : {
43 0 : }
44 :
45 0 : bool EncryptorImpl::checkReady() const
46 : /****** EncryptorImpl/checkReady *********************************************
47 : *
48 : * NAME
49 : * checkReady -- checks the conditions for the encryption.
50 : *
51 : * SYNOPSIS
52 : * bReady = checkReady( );
53 : *
54 : * FUNCTION
55 : * checks whether all following conditions are satisfied:
56 : * 1. the result listener is ready;
57 : * 2. the EncryptionEngine is ready.
58 : *
59 : * INPUTS
60 : * empty
61 : *
62 : * RESULT
63 : * bReady - true if all conditions are satisfied, false otherwise
64 : *
65 : * AUTHOR
66 : * Michael Mi
67 : * Email: michael.mi@sun.com
68 : ******************************************************************************/
69 : {
70 0 : sal_Int32 nKeyInc = 0;
71 0 : if (m_nIdOfKeyEC != 0)
72 : {
73 0 : nKeyInc = 1;
74 : }
75 :
76 0 : return (m_xResultListener.is() &&
77 0 : (m_nReferenceId != -1) &&
78 0 : (2+nKeyInc == m_nNumOfResolvedReferences) &&
79 0 : EncryptionEngine::checkReady());
80 : }
81 :
82 0 : void EncryptorImpl::notifyResultListener() const
83 : throw (Exception, RuntimeException)
84 : /****** DecryptorImpl/notifyResultListener ***********************************
85 : *
86 : * NAME
87 : * notifyResultListener -- notifies the listener about the encryption
88 : * result.
89 : *
90 : * SYNOPSIS
91 : * notifyResultListener( );
92 : *
93 : * FUNCTION
94 : * see NAME.
95 : *
96 : * INPUTS
97 : * empty
98 : *
99 : * RESULT
100 : * empty
101 : *
102 : * AUTHOR
103 : * Michael Mi
104 : * Email: michael.mi@sun.com
105 : ******************************************************************************/
106 : {
107 : Reference< cssxc::sax::XEncryptionResultListener >
108 0 : xEncryptionResultListener ( m_xResultListener , UNO_QUERY ) ;
109 :
110 0 : xEncryptionResultListener->encrypted( m_nSecurityId, m_nStatus );
111 0 : }
112 :
113 0 : void EncryptorImpl::startEngine( const Reference<
114 : cssxc::XXMLEncryptionTemplate >&
115 : xEncryptionTemplate)
116 : throw (Exception, RuntimeException)
117 : /****** EncryptorImpl/startEngine ********************************************
118 : *
119 : * NAME
120 : * startEngine -- generates the encryption.
121 : *
122 : * SYNOPSIS
123 : * startEngine( xEncryptionTemplate );
124 : *
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.
129 : *
130 : * INPUTS
131 : * xEncryptionTemplate - the encryption template to be encrypted.
132 : *
133 : * RESULT
134 : * empty
135 : *
136 : * AUTHOR
137 : * Michael Mi
138 : * Email: michael.mi@sun.com
139 : ******************************************************************************/
140 : {
141 0 : Reference < cssxc::XXMLEncryptionTemplate > xResultTemplate;
142 :
143 : Reference< cssxw::XXMLElementWrapper >
144 0 : xXMLElement = m_xSAXEventKeeper->getElement( m_nReferenceId );
145 0 : xEncryptionTemplate->setTarget(xXMLElement);
146 :
147 : try
148 : {
149 0 : xResultTemplate = m_xXMLEncryption->encrypt(
150 0 : xEncryptionTemplate, m_xSecurityEnvironment);
151 0 : m_nStatus = xResultTemplate->getStatus();
152 : }
153 0 : catch( Exception& )
154 : {
155 0 : m_nStatus = cssxc::SecurityOperationStatus_RUNTIMEERROR_FAILED;
156 : }
157 :
158 0 : if (m_nStatus == cssxc::SecurityOperationStatus_OPERATION_SUCCEEDED)
159 : {
160 : Reference < cssxw::XXMLElementWrapper > xResultEncryption
161 0 : = xResultTemplate->getTemplate();
162 0 : m_xSAXEventKeeper->setElement(m_nIdOfTemplateEC, xResultEncryption);
163 0 : m_xSAXEventKeeper->setElement(m_nReferenceId, NULL);
164 0 : }
165 0 : }
166 :
167 : /* XReferenceCollector */
168 0 : void SAL_CALL EncryptorImpl::setReferenceCount(sal_Int32)
169 : throw (Exception, RuntimeException, std::exception)
170 : {
171 : /*
172 : * dummp method, because there is only one reference in
173 : * encryption, different from signature.
174 : * so the referenceNumber is always 1
175 : */
176 0 : }
177 :
178 0 : void SAL_CALL EncryptorImpl::setReferenceId( sal_Int32 id )
179 : throw (Exception, RuntimeException, std::exception)
180 : {
181 0 : m_nReferenceId = id;
182 0 : }
183 :
184 : /* XEncryptionResultBroadcaster */
185 0 : void SAL_CALL EncryptorImpl::addEncryptionResultListener( const Reference< cssxc::sax::XEncryptionResultListener >& listener )
186 : throw (Exception, RuntimeException, std::exception)
187 : {
188 0 : m_xResultListener = listener;
189 0 : tryToPerform();
190 0 : }
191 :
192 0 : void SAL_CALL EncryptorImpl::removeEncryptionResultListener( const Reference< cssxc::sax::XEncryptionResultListener >&)
193 : throw (RuntimeException, std::exception)
194 : {
195 0 : }
196 :
197 : /* XInitialization */
198 0 : void SAL_CALL EncryptorImpl::initialize( const Sequence< Any >& aArguments )
199 : throw (Exception, RuntimeException, std::exception)
200 : {
201 : OSL_ASSERT(aArguments.getLength() == 5);
202 :
203 0 : OUString ouTempString;
204 :
205 0 : aArguments[0] >>= ouTempString;
206 0 : m_nSecurityId = ouTempString.toInt32();
207 0 : aArguments[1] >>= m_xSAXEventKeeper;
208 0 : aArguments[2] >>= ouTempString;
209 0 : m_nIdOfTemplateEC = ouTempString.toInt32();
210 0 : aArguments[3] >>= m_xSecurityEnvironment;
211 0 : aArguments[4] >>= m_xXMLEncryption;
212 0 : }
213 :
214 :
215 4 : OUString EncryptorImpl_getImplementationName ()
216 : throw (RuntimeException)
217 : {
218 4 : return OUString ( IMPLEMENTATION_NAME );
219 : }
220 :
221 0 : Sequence< OUString > SAL_CALL EncryptorImpl_getSupportedServiceNames( )
222 : throw (RuntimeException)
223 : {
224 0 : Sequence < OUString > aRet(1);
225 0 : OUString* pArray = aRet.getArray();
226 0 : pArray[0] = OUString ( SERVICE_NAME );
227 0 : return aRet;
228 : }
229 : #undef SERVICE_NAME
230 :
231 0 : Reference< XInterface > SAL_CALL EncryptorImpl_createInstance(
232 : const Reference< cssl::XMultiServiceFactory >& xMSF)
233 : throw( Exception )
234 : {
235 0 : return (cppu::OWeakObject*) new EncryptorImpl( comphelper::getComponentContext( xMSF ) );
236 : }
237 :
238 : /* XServiceInfo */
239 0 : OUString SAL_CALL EncryptorImpl::getImplementationName( )
240 : throw (RuntimeException, std::exception)
241 : {
242 0 : return EncryptorImpl_getImplementationName();
243 : }
244 :
245 0 : sal_Bool SAL_CALL EncryptorImpl::supportsService( const OUString& rServiceName )
246 : throw (RuntimeException, std::exception)
247 : {
248 0 : return cppu::supportsService(this, rServiceName);
249 : }
250 :
251 0 : Sequence< OUString > SAL_CALL EncryptorImpl::getSupportedServiceNames( )
252 : throw (RuntimeException, std::exception)
253 : {
254 0 : return EncryptorImpl_getSupportedServiceNames();
255 : }
256 :
257 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|