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 "signatureverifierimpl.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.SignatureVerifier"
38 #define IMPLEMENTATION_NAME "com.sun.star.xml.security.framework.SignatureVerifierImpl"
39 
40 #define	DECLARE_ASCII( SASCIIVALUE )																			\
41 	rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SASCIIVALUE ) )
42 
SignatureVerifierImpl(const cssu::Reference<cssl::XMultiServiceFactory> & rxMSF)43 SignatureVerifierImpl::SignatureVerifierImpl( const cssu::Reference< cssl::XMultiServiceFactory >& rxMSF)
44 {
45 	mxMSF = rxMSF;
46 }
47 
~SignatureVerifierImpl()48 SignatureVerifierImpl::~SignatureVerifierImpl()
49 {
50 }
51 
checkReady() const52 bool SignatureVerifierImpl::checkReady() const
53 /****** SignatureVerifierImpl/checkReady *************************************
54  *
55  *   NAME
56  *	checkReady -- checks the conditions for the signature verification.
57  *
58  *   SYNOPSIS
59  *	bReady = checkReady( );
60  *
61  *   FUNCTION
62  *	checks whether all following conditions are satisfied:
63  *	1. the result listener is ready;
64  *	2. the SignatureEngine is ready.
65  *
66  *   INPUTS
67  *	empty
68  *
69  *   RESULT
70  *	bReady - true if all conditions are satisfied, false otherwise
71  *
72  *   HISTORY
73  *	05.01.2004 -	implemented
74  *
75  *   AUTHOR
76  *	Michael Mi
77  *	Email: michael.mi@sun.com
78  ******************************************************************************/
79 {
80 	return (m_xResultListener.is() && SignatureEngine::checkReady());
81 }
82 
notifyResultListener() const83 void SignatureVerifierImpl::notifyResultListener() const
84 	throw (cssu::Exception, cssu::RuntimeException)
85 /****** SignatureVerifierImpl/notifyResultListener ***************************
86  *
87  *   NAME
88  *	notifyResultListener -- notifies the listener about the verify result.
89  *
90  *   SYNOPSIS
91  *	notifyResultListener( );
92  *
93  *   FUNCTION
94  *	see NAME.
95  *
96  *   INPUTS
97  *	empty
98  *
99  *   RESULT
100  *	empty
101  *
102  *   HISTORY
103  *	05.01.2004 -	implemented
104  *
105  *   AUTHOR
106  *	Michael Mi
107  *	Email: michael.mi@sun.com
108  ******************************************************************************/
109 {
110 	cssu::Reference< cssxc::sax::XSignatureVerifyResultListener >
111 		xSignatureVerifyResultListener ( m_xResultListener , cssu::UNO_QUERY ) ;
112 
113 	xSignatureVerifyResultListener->signatureVerified( m_nSecurityId, m_nStatus );
114 }
115 
startEngine(const cssu::Reference<cssxc::XXMLSignatureTemplate> & xSignatureTemplate)116 void SignatureVerifierImpl::startEngine( const cssu::Reference<
117 	cssxc::XXMLSignatureTemplate >&
118 	xSignatureTemplate)
119 	throw (cssu::Exception, cssu::RuntimeException)
120 /****** SignatureVerifierImpl/startEngine ************************************
121  *
122  *   NAME
123  *	startEngine -- verifies the signature.
124  *
125  *   SYNOPSIS
126  *	startEngine( xSignatureTemplate );
127  *
128  *   FUNCTION
129  *	see NAME.
130  *
131  *   INPUTS
132  *	xSignatureTemplate - the signature template (along with all referenced
133  *	elements) to be verified.
134  *
135  *   RESULT
136  *	empty
137  *
138  *   HISTORY
139  *	05.01.2004 -	implemented
140  *
141  *   AUTHOR
142  *	Michael Mi
143  *	Email: michael.mi@sun.com
144  ******************************************************************************/
145 {
146 	cssu::Reference< cssxc::XXMLSignatureTemplate > xResultTemplate;
147 	try
148 	{
149 		xResultTemplate = m_xXMLSignature->validate(xSignatureTemplate, m_xXMLSecurityContext);
150 		m_nStatus = xResultTemplate->getStatus();
151 	}
152 	catch( cssu::Exception& )
153 	{
154 		m_nStatus = cssxc::SecurityOperationStatus_RUNTIMEERROR_FAILED;
155 	}
156 }
157 
158 /* XSignatureVerifyResultBroadcaster */
addSignatureVerifyResultListener(const cssu::Reference<cssxc::sax::XSignatureVerifyResultListener> & listener)159 void SAL_CALL SignatureVerifierImpl::addSignatureVerifyResultListener(
160 	const cssu::Reference< cssxc::sax::XSignatureVerifyResultListener >& listener )
161 	throw (cssu::Exception, cssu::RuntimeException)
162 {
163 	m_xResultListener = listener;
164 	tryToPerform();
165 }
166 
removeSignatureVerifyResultListener(const cssu::Reference<cssxc::sax::XSignatureVerifyResultListener> &)167 void SAL_CALL SignatureVerifierImpl::removeSignatureVerifyResultListener(
168 	const cssu::Reference< cssxc::sax::XSignatureVerifyResultListener >&)
169 	throw (cssu::RuntimeException)
170 {
171 }
172 
173 /* XInitialization */
initialize(const cssu::Sequence<cssu::Any> & aArguments)174 void SAL_CALL SignatureVerifierImpl::initialize(
175 	const cssu::Sequence< cssu::Any >& aArguments )
176 	throw (cssu::Exception, cssu::RuntimeException)
177 {
178 	OSL_ASSERT(aArguments.getLength() == 5);
179 
180 	rtl::OUString ouTempString;
181 
182 	aArguments[0] >>= ouTempString;
183 	m_nSecurityId = ouTempString.toInt32();
184 	aArguments[1] >>= m_xSAXEventKeeper;
185 	aArguments[2] >>= ouTempString;
186 	m_nIdOfTemplateEC = ouTempString.toInt32();
187 	aArguments[3] >>= m_xXMLSecurityContext;
188 	aArguments[4] >>= m_xXMLSignature;
189 }
190 
191 
SignatureVerifierImpl_getImplementationName()192 rtl::OUString SignatureVerifierImpl_getImplementationName ()
193 	throw (cssu::RuntimeException)
194 {
195 	return rtl::OUString(
196 		RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) );
197 }
198 
SignatureVerifierImpl_supportsService(const rtl::OUString & ServiceName)199 sal_Bool SAL_CALL SignatureVerifierImpl_supportsService( const rtl::OUString& ServiceName )
200 	throw (cssu::RuntimeException)
201 {
202 	return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ));
203 }
204 
SignatureVerifierImpl_getSupportedServiceNames()205 cssu::Sequence< rtl::OUString > SAL_CALL SignatureVerifierImpl_getSupportedServiceNames(  )
206 	throw (cssu::RuntimeException)
207 {
208 	cssu::Sequence < rtl::OUString > aRet(1);
209 	rtl::OUString* pArray = aRet.getArray();
210 	pArray[0] =  rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
211 	return aRet;
212 }
213 #undef SERVICE_NAME
214 
SignatureVerifierImpl_createInstance(const cssu::Reference<cssl::XMultiServiceFactory> & rSMgr)215 cssu::Reference< cssu::XInterface > SAL_CALL SignatureVerifierImpl_createInstance(
216 	const cssu::Reference< cssl::XMultiServiceFactory >& rSMgr)
217 	throw( cssu::Exception )
218 {
219 	return (cppu::OWeakObject*) new SignatureVerifierImpl(rSMgr);
220 }
221 
222 /* XServiceInfo */
getImplementationName()223 rtl::OUString SAL_CALL SignatureVerifierImpl::getImplementationName(  )
224 	throw (cssu::RuntimeException)
225 {
226 	return SignatureVerifierImpl_getImplementationName();
227 }
supportsService(const rtl::OUString & rServiceName)228 sal_Bool SAL_CALL SignatureVerifierImpl::supportsService( const rtl::OUString& rServiceName )
229 	throw (cssu::RuntimeException)
230 {
231 	return SignatureVerifierImpl_supportsService( rServiceName );
232 }
getSupportedServiceNames()233 cssu::Sequence< rtl::OUString > SAL_CALL SignatureVerifierImpl::getSupportedServiceNames(  )
234 	throw (cssu::RuntimeException)
235 {
236 	return SignatureVerifierImpl_getSupportedServiceNames();
237 }
238 
239