xref: /trunk/main/xmlsecurity/source/framework/signaturecreatorimpl.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmlsecurity.hxx"
26 
27 #include "signaturecreatorimpl.hxx"
28 #include <com/sun/star/xml/crypto/XXMLSignatureTemplate.hpp>
29 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 
32 namespace cssu = com::sun::star::uno;
33 namespace cssl = com::sun::star::lang;
34 namespace cssxc = com::sun::star::xml::crypto;
35 namespace cssxw = com::sun::star::xml::wrapper;
36 
37 #define SERVICE_NAME "com.sun.star.xml.crypto.sax.SignatureCreator"
38 #define IMPLEMENTATION_NAME "com.sun.star.xml.security.framework.SignatureCreatorImpl"
39 
40 #define DECLARE_ASCII( SASCIIVALUE )                                                                            \
41     rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SASCIIVALUE ) )
42 
SignatureCreatorImpl(const cssu::Reference<cssl::XMultiServiceFactory> & rxMSF)43 SignatureCreatorImpl::SignatureCreatorImpl( const cssu::Reference< cssl::XMultiServiceFactory >& rxMSF )
44     :m_nIdOfBlocker(-1)
45 {
46     mxMSF = rxMSF;
47 }
48 
~SignatureCreatorImpl()49 SignatureCreatorImpl::~SignatureCreatorImpl( )
50 {
51 }
52 
checkReady() const53 bool SignatureCreatorImpl::checkReady() const
54 /****** SignatureCreatorImpl/checkReady **************************************
55  *
56  *   NAME
57  *  checkReady -- checks the conditions for the signature generation.
58  *
59  *   SYNOPSIS
60  *  bReady = checkReady( );
61  *
62  *   FUNCTION
63  *  checks whether all following conditions are satisfied:
64  *  1. the result listener is ready;
65  *  2. the id of the template blocker is known;
66  *  3. the SignatureEngine is ready.
67  *
68  *   INPUTS
69  *  empty
70  *
71  *   RESULT
72  *  bReady - true if all conditions are satisfied, false otherwise
73  *
74  *   HISTORY
75  *  05.01.2004 -    implemented
76  *
77  *   AUTHOR
78  *  Michael Mi
79  *  Email: michael.mi@sun.com
80  ******************************************************************************/
81 {
82     return (m_xResultListener.is() &&
83             (m_nIdOfBlocker != -1) &&
84             SignatureEngine::checkReady());
85 }
86 
notifyResultListener() const87 void SignatureCreatorImpl::notifyResultListener() const
88 /****** SignatureCreatorImpl/notifyResultListener *****************************
89  *
90  *   NAME
91  *  notifyResultListener -- notifies the listener about the signature
92  *  creation result.
93  *
94  *   SYNOPSIS
95  *  notifyResultListener( );
96  *
97  *   FUNCTION
98  *  see NAME.
99  *
100  *   INPUTS
101  *  empty
102  *
103  *   RESULT
104  *  empty
105  *
106  *   HISTORY
107  *  05.01.2004 -    implemented
108  *
109  *   AUTHOR
110  *  Michael Mi
111  *  Email: michael.mi@sun.com
112  ******************************************************************************/
113 {
114     cssu::Reference< cssxc::sax::XSignatureCreationResultListener >
115         xSignatureCreationResultListener ( m_xResultListener , cssu::UNO_QUERY ) ;
116 
117     xSignatureCreationResultListener->signatureCreated( m_nSecurityId, m_nStatus );
118 }
119 
startEngine(const cssu::Reference<cssxc::XXMLSignatureTemplate> & xSignatureTemplate)120 void SignatureCreatorImpl::startEngine( const cssu::Reference<
121     cssxc::XXMLSignatureTemplate >&
122     xSignatureTemplate)
123 /****** SignatureCreatorImpl/startEngine *************************************
124  *
125  *   NAME
126  *  startEngine -- generates the signature.
127  *
128  *   SYNOPSIS
129  *  startEngine( xSignatureTemplate );
130  *
131  *   FUNCTION
132  *  generates the signature element, then if succeeds, updates the link
133  *  of old template element to the new signature element in
134  *  SAXEventKeeper.
135  *
136  *   INPUTS
137  *  xSignatureTemplate - the signature template (along with all referenced
138  *  elements) to be signed.
139  *
140  *   RESULT
141  *  empty
142  *
143  *   HISTORY
144  *  05.01.2004 -    implemented
145  *
146  *   AUTHOR
147  *  Michael Mi
148  *  Email: michael.mi@sun.com
149  ******************************************************************************/
150 {
151     cssu::Reference< cssxc::XXMLSignatureTemplate > xResultTemplate;
152     try
153     {
154         xResultTemplate = m_xXMLSignature->generate(xSignatureTemplate, m_xSecurityEnvironment);
155         m_nStatus = xResultTemplate->getStatus();
156     }
157     catch( cssu::Exception& )
158     {
159         m_nStatus = cssxc::SecurityOperationStatus_RUNTIMEERROR_FAILED;
160     }
161 
162     if (m_nStatus == cssxc::SecurityOperationStatus_OPERATION_SUCCEEDED)
163     {
164         cssu::Reference < cssxw::XXMLElementWrapper > xResultSignature = xResultTemplate->getTemplate();
165         m_xSAXEventKeeper->setElement(m_nIdOfTemplateEC, xResultSignature);
166     }
167 }
168 
clearUp() const169 void SignatureCreatorImpl::clearUp() const
170 /****** SignatureCreatorImpl/clearUp *****************************************
171  *
172  *   NAME
173  *  clearUp -- clear up all resources used by the signature generation.
174  *
175  *   SYNOPSIS
176  *  clearUp( );
177  *
178  *   FUNCTION
179  *  cleaning resources up includes:
180  *  1. SignatureEngine's clearing up;
181  *  2. releases the Blocker for the signature template element.
182  *
183  *   INPUTS
184  *  empty
185  *
186  *   RESULT
187  *  empty
188  *
189  *   HISTORY
190  *  05.01.2004 -    implemented
191  *
192  *   AUTHOR
193  *  Michael Mi
194  *  Email: michael.mi@sun.com
195  ******************************************************************************/
196 {
197     SignatureEngine::clearUp();
198 
199     if (m_nIdOfBlocker != -1)
200     {
201         m_xSAXEventKeeper->removeBlocker(m_nIdOfBlocker);
202     }
203 }
204 
205 /* XBlockerMonitor */
setBlockerId(sal_Int32 id)206 void SAL_CALL SignatureCreatorImpl::setBlockerId( sal_Int32 id )
207 {
208     m_nIdOfBlocker = id;
209     tryToPerform();
210 }
211 
212 /* XSignatureCreationResultBroadcaster */
addSignatureCreationResultListener(const cssu::Reference<cssxc::sax::XSignatureCreationResultListener> & listener)213 void SAL_CALL SignatureCreatorImpl::addSignatureCreationResultListener(
214     const cssu::Reference< cssxc::sax::XSignatureCreationResultListener >& listener )
215 {
216     m_xResultListener = listener;
217     tryToPerform();
218 }
219 
removeSignatureCreationResultListener(const cssu::Reference<cssxc::sax::XSignatureCreationResultListener> &)220 void SAL_CALL SignatureCreatorImpl::removeSignatureCreationResultListener(
221     const cssu::Reference< cssxc::sax::XSignatureCreationResultListener >&)
222 {
223 }
224 
225 /* XInitialization */
initialize(const cssu::Sequence<cssu::Any> & aArguments)226 void SAL_CALL SignatureCreatorImpl::initialize( const cssu::Sequence< cssu::Any >& aArguments )
227 {
228     OSL_ASSERT(aArguments.getLength() == 5);
229 
230     rtl::OUString ouTempString;
231 
232     aArguments[0] >>= ouTempString;
233     m_nSecurityId = ouTempString.toInt32();
234     aArguments[1] >>= m_xSAXEventKeeper;
235     aArguments[2] >>= ouTempString;
236     m_nIdOfTemplateEC = ouTempString.toInt32();
237     aArguments[3] >>= m_xSecurityEnvironment;
238     aArguments[4] >>= m_xXMLSignature;
239 }
240 
241 
SignatureCreatorImpl_getImplementationName()242 rtl::OUString SignatureCreatorImpl_getImplementationName ()
243 {
244     return rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) );
245 }
246 
SignatureCreatorImpl_supportsService(const rtl::OUString & ServiceName)247 sal_Bool SAL_CALL SignatureCreatorImpl_supportsService( const rtl::OUString& ServiceName )
248 {
249     return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ));
250 }
251 
SignatureCreatorImpl_getSupportedServiceNames()252 cssu::Sequence< rtl::OUString > SAL_CALL SignatureCreatorImpl_getSupportedServiceNames(  )
253 {
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;
258 }
259 #undef SERVICE_NAME
260 
SignatureCreatorImpl_createInstance(const cssu::Reference<cssl::XMultiServiceFactory> & rSMgr)261 cssu::Reference< cssu::XInterface > SAL_CALL SignatureCreatorImpl_createInstance(
262     const cssu::Reference< cssl::XMultiServiceFactory >& rSMgr)
263 {
264     return (cppu::OWeakObject*) new SignatureCreatorImpl( rSMgr );
265 }
266 
267 /* XServiceInfo */
getImplementationName()268 rtl::OUString SAL_CALL SignatureCreatorImpl::getImplementationName(  )
269 {
270     return SignatureCreatorImpl_getImplementationName();
271 }
supportsService(const rtl::OUString & rServiceName)272 sal_Bool SAL_CALL SignatureCreatorImpl::supportsService( const rtl::OUString& rServiceName )
273 {
274     return SignatureCreatorImpl_supportsService( rServiceName );
275 }
getSupportedServiceNames()276 cssu::Sequence< rtl::OUString > SAL_CALL SignatureCreatorImpl::getSupportedServiceNames(  )
277 {
278     return SignatureCreatorImpl_getSupportedServiceNames();
279 }
280