1*06b3ce53SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*06b3ce53SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*06b3ce53SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*06b3ce53SAndrew Rist  * distributed with this work for additional information
6*06b3ce53SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*06b3ce53SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*06b3ce53SAndrew Rist  * "License"); you may not use this file except in compliance
9*06b3ce53SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*06b3ce53SAndrew Rist  *
11*06b3ce53SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*06b3ce53SAndrew Rist  *
13*06b3ce53SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*06b3ce53SAndrew Rist  * software distributed under the License is distributed on an
15*06b3ce53SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*06b3ce53SAndrew Rist  * KIND, either express or implied.  See the License for the
17*06b3ce53SAndrew Rist  * specific language governing permissions and limitations
18*06b3ce53SAndrew Rist  * under the License.
19*06b3ce53SAndrew Rist  *
20*06b3ce53SAndrew Rist  *************************************************************/
21*06b3ce53SAndrew Rist 
22*06b3ce53SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmlsecurity.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "signatureverifierimpl.hxx"
28cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XXMLSignatureTemplate.hpp>
29cdf0e10cSrcweir #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
30cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir namespace cssu = com::sun::star::uno;
33cdf0e10cSrcweir namespace cssl = com::sun::star::lang;
34cdf0e10cSrcweir namespace cssxc = com::sun::star::xml::crypto;
35cdf0e10cSrcweir namespace cssxw = com::sun::star::xml::wrapper;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #define SERVICE_NAME "com.sun.star.xml.crypto.sax.SignatureVerifier"
38cdf0e10cSrcweir #define IMPLEMENTATION_NAME "com.sun.star.xml.security.framework.SignatureVerifierImpl"
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #define	DECLARE_ASCII( SASCIIVALUE )																			\
41cdf0e10cSrcweir 	rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SASCIIVALUE ) )
42cdf0e10cSrcweir 
SignatureVerifierImpl(const cssu::Reference<cssl::XMultiServiceFactory> & rxMSF)43cdf0e10cSrcweir SignatureVerifierImpl::SignatureVerifierImpl( const cssu::Reference< cssl::XMultiServiceFactory >& rxMSF)
44cdf0e10cSrcweir {
45cdf0e10cSrcweir 	mxMSF = rxMSF;
46cdf0e10cSrcweir }
47cdf0e10cSrcweir 
~SignatureVerifierImpl()48cdf0e10cSrcweir SignatureVerifierImpl::~SignatureVerifierImpl()
49cdf0e10cSrcweir {
50cdf0e10cSrcweir }
51cdf0e10cSrcweir 
checkReady() const52cdf0e10cSrcweir bool SignatureVerifierImpl::checkReady() const
53cdf0e10cSrcweir /****** SignatureVerifierImpl/checkReady *************************************
54cdf0e10cSrcweir  *
55cdf0e10cSrcweir  *   NAME
56cdf0e10cSrcweir  *	checkReady -- checks the conditions for the signature verification.
57cdf0e10cSrcweir  *
58cdf0e10cSrcweir  *   SYNOPSIS
59cdf0e10cSrcweir  *	bReady = checkReady( );
60cdf0e10cSrcweir  *
61cdf0e10cSrcweir  *   FUNCTION
62cdf0e10cSrcweir  *	checks whether all following conditions are satisfied:
63cdf0e10cSrcweir  *	1. the result listener is ready;
64cdf0e10cSrcweir  *	2. the SignatureEngine is ready.
65cdf0e10cSrcweir  *
66cdf0e10cSrcweir  *   INPUTS
67cdf0e10cSrcweir  *	empty
68cdf0e10cSrcweir  *
69cdf0e10cSrcweir  *   RESULT
70cdf0e10cSrcweir  *	bReady - true if all conditions are satisfied, false otherwise
71cdf0e10cSrcweir  *
72cdf0e10cSrcweir  *   HISTORY
73cdf0e10cSrcweir  *	05.01.2004 -	implemented
74cdf0e10cSrcweir  *
75cdf0e10cSrcweir  *   AUTHOR
76cdf0e10cSrcweir  *	Michael Mi
77cdf0e10cSrcweir  *	Email: michael.mi@sun.com
78cdf0e10cSrcweir  ******************************************************************************/
79cdf0e10cSrcweir {
80cdf0e10cSrcweir 	return (m_xResultListener.is() && SignatureEngine::checkReady());
81cdf0e10cSrcweir }
82cdf0e10cSrcweir 
notifyResultListener() const83cdf0e10cSrcweir void SignatureVerifierImpl::notifyResultListener() const
84cdf0e10cSrcweir 	throw (cssu::Exception, cssu::RuntimeException)
85cdf0e10cSrcweir /****** SignatureVerifierImpl/notifyResultListener ***************************
86cdf0e10cSrcweir  *
87cdf0e10cSrcweir  *   NAME
88cdf0e10cSrcweir  *	notifyResultListener -- notifies the listener about the verify result.
89cdf0e10cSrcweir  *
90cdf0e10cSrcweir  *   SYNOPSIS
91cdf0e10cSrcweir  *	notifyResultListener( );
92cdf0e10cSrcweir  *
93cdf0e10cSrcweir  *   FUNCTION
94cdf0e10cSrcweir  *	see NAME.
95cdf0e10cSrcweir  *
96cdf0e10cSrcweir  *   INPUTS
97cdf0e10cSrcweir  *	empty
98cdf0e10cSrcweir  *
99cdf0e10cSrcweir  *   RESULT
100cdf0e10cSrcweir  *	empty
101cdf0e10cSrcweir  *
102cdf0e10cSrcweir  *   HISTORY
103cdf0e10cSrcweir  *	05.01.2004 -	implemented
104cdf0e10cSrcweir  *
105cdf0e10cSrcweir  *   AUTHOR
106cdf0e10cSrcweir  *	Michael Mi
107cdf0e10cSrcweir  *	Email: michael.mi@sun.com
108cdf0e10cSrcweir  ******************************************************************************/
109cdf0e10cSrcweir {
110cdf0e10cSrcweir 	cssu::Reference< cssxc::sax::XSignatureVerifyResultListener >
111cdf0e10cSrcweir 		xSignatureVerifyResultListener ( m_xResultListener , cssu::UNO_QUERY ) ;
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 	xSignatureVerifyResultListener->signatureVerified( m_nSecurityId, m_nStatus );
114cdf0e10cSrcweir }
115cdf0e10cSrcweir 
startEngine(const cssu::Reference<cssxc::XXMLSignatureTemplate> & xSignatureTemplate)116cdf0e10cSrcweir void SignatureVerifierImpl::startEngine( const cssu::Reference<
117cdf0e10cSrcweir 	cssxc::XXMLSignatureTemplate >&
118cdf0e10cSrcweir 	xSignatureTemplate)
119cdf0e10cSrcweir 	throw (cssu::Exception, cssu::RuntimeException)
120cdf0e10cSrcweir /****** SignatureVerifierImpl/startEngine ************************************
121cdf0e10cSrcweir  *
122cdf0e10cSrcweir  *   NAME
123cdf0e10cSrcweir  *	startEngine -- verifies the signature.
124cdf0e10cSrcweir  *
125cdf0e10cSrcweir  *   SYNOPSIS
126cdf0e10cSrcweir  *	startEngine( xSignatureTemplate );
127cdf0e10cSrcweir  *
128cdf0e10cSrcweir  *   FUNCTION
129cdf0e10cSrcweir  *	see NAME.
130cdf0e10cSrcweir  *
131cdf0e10cSrcweir  *   INPUTS
132cdf0e10cSrcweir  *	xSignatureTemplate - the signature template (along with all referenced
133cdf0e10cSrcweir  *	elements) to be verified.
134cdf0e10cSrcweir  *
135cdf0e10cSrcweir  *   RESULT
136cdf0e10cSrcweir  *	empty
137cdf0e10cSrcweir  *
138cdf0e10cSrcweir  *   HISTORY
139cdf0e10cSrcweir  *	05.01.2004 -	implemented
140cdf0e10cSrcweir  *
141cdf0e10cSrcweir  *   AUTHOR
142cdf0e10cSrcweir  *	Michael Mi
143cdf0e10cSrcweir  *	Email: michael.mi@sun.com
144cdf0e10cSrcweir  ******************************************************************************/
145cdf0e10cSrcweir {
146cdf0e10cSrcweir 	cssu::Reference< cssxc::XXMLSignatureTemplate > xResultTemplate;
147cdf0e10cSrcweir 	try
148cdf0e10cSrcweir 	{
149cdf0e10cSrcweir 		xResultTemplate = m_xXMLSignature->validate(xSignatureTemplate, m_xXMLSecurityContext);
150cdf0e10cSrcweir 		m_nStatus = xResultTemplate->getStatus();
151cdf0e10cSrcweir 	}
152cdf0e10cSrcweir 	catch( cssu::Exception& )
153cdf0e10cSrcweir 	{
154cdf0e10cSrcweir 		m_nStatus = cssxc::SecurityOperationStatus_RUNTIMEERROR_FAILED;
155cdf0e10cSrcweir 	}
156cdf0e10cSrcweir }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir /* XSignatureVerifyResultBroadcaster */
addSignatureVerifyResultListener(const cssu::Reference<cssxc::sax::XSignatureVerifyResultListener> & listener)159cdf0e10cSrcweir void SAL_CALL SignatureVerifierImpl::addSignatureVerifyResultListener(
160cdf0e10cSrcweir 	const cssu::Reference< cssxc::sax::XSignatureVerifyResultListener >& listener )
161cdf0e10cSrcweir 	throw (cssu::Exception, cssu::RuntimeException)
162cdf0e10cSrcweir {
163cdf0e10cSrcweir 	m_xResultListener = listener;
164cdf0e10cSrcweir 	tryToPerform();
165cdf0e10cSrcweir }
166cdf0e10cSrcweir 
removeSignatureVerifyResultListener(const cssu::Reference<cssxc::sax::XSignatureVerifyResultListener> &)167cdf0e10cSrcweir void SAL_CALL SignatureVerifierImpl::removeSignatureVerifyResultListener(
168cdf0e10cSrcweir 	const cssu::Reference< cssxc::sax::XSignatureVerifyResultListener >&)
169cdf0e10cSrcweir 	throw (cssu::RuntimeException)
170cdf0e10cSrcweir {
171cdf0e10cSrcweir }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir /* XInitialization */
initialize(const cssu::Sequence<cssu::Any> & aArguments)174cdf0e10cSrcweir void SAL_CALL SignatureVerifierImpl::initialize(
175cdf0e10cSrcweir 	const cssu::Sequence< cssu::Any >& aArguments )
176cdf0e10cSrcweir 	throw (cssu::Exception, cssu::RuntimeException)
177cdf0e10cSrcweir {
178cdf0e10cSrcweir 	OSL_ASSERT(aArguments.getLength() == 5);
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 	rtl::OUString ouTempString;
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 	aArguments[0] >>= ouTempString;
183cdf0e10cSrcweir 	m_nSecurityId = ouTempString.toInt32();
184cdf0e10cSrcweir 	aArguments[1] >>= m_xSAXEventKeeper;
185cdf0e10cSrcweir 	aArguments[2] >>= ouTempString;
186cdf0e10cSrcweir 	m_nIdOfTemplateEC = ouTempString.toInt32();
187cdf0e10cSrcweir 	aArguments[3] >>= m_xXMLSecurityContext;
188cdf0e10cSrcweir 	aArguments[4] >>= m_xXMLSignature;
189cdf0e10cSrcweir }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 
SignatureVerifierImpl_getImplementationName()192cdf0e10cSrcweir rtl::OUString SignatureVerifierImpl_getImplementationName ()
193cdf0e10cSrcweir 	throw (cssu::RuntimeException)
194cdf0e10cSrcweir {
195cdf0e10cSrcweir 	return rtl::OUString(
196cdf0e10cSrcweir 		RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) );
197cdf0e10cSrcweir }
198cdf0e10cSrcweir 
SignatureVerifierImpl_supportsService(const rtl::OUString & ServiceName)199cdf0e10cSrcweir sal_Bool SAL_CALL SignatureVerifierImpl_supportsService( const rtl::OUString& ServiceName )
200cdf0e10cSrcweir 	throw (cssu::RuntimeException)
201cdf0e10cSrcweir {
202cdf0e10cSrcweir 	return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ));
203cdf0e10cSrcweir }
204cdf0e10cSrcweir 
SignatureVerifierImpl_getSupportedServiceNames()205cdf0e10cSrcweir cssu::Sequence< rtl::OUString > SAL_CALL SignatureVerifierImpl_getSupportedServiceNames(  )
206cdf0e10cSrcweir 	throw (cssu::RuntimeException)
207cdf0e10cSrcweir {
208cdf0e10cSrcweir 	cssu::Sequence < rtl::OUString > aRet(1);
209cdf0e10cSrcweir 	rtl::OUString* pArray = aRet.getArray();
210cdf0e10cSrcweir 	pArray[0] =  rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
211cdf0e10cSrcweir 	return aRet;
212cdf0e10cSrcweir }
213cdf0e10cSrcweir #undef SERVICE_NAME
214cdf0e10cSrcweir 
SignatureVerifierImpl_createInstance(const cssu::Reference<cssl::XMultiServiceFactory> & rSMgr)215cdf0e10cSrcweir cssu::Reference< cssu::XInterface > SAL_CALL SignatureVerifierImpl_createInstance(
216cdf0e10cSrcweir 	const cssu::Reference< cssl::XMultiServiceFactory >& rSMgr)
217cdf0e10cSrcweir 	throw( cssu::Exception )
218cdf0e10cSrcweir {
219cdf0e10cSrcweir 	return (cppu::OWeakObject*) new SignatureVerifierImpl(rSMgr);
220cdf0e10cSrcweir }
221cdf0e10cSrcweir 
222cdf0e10cSrcweir /* XServiceInfo */
getImplementationName()223cdf0e10cSrcweir rtl::OUString SAL_CALL SignatureVerifierImpl::getImplementationName(  )
224cdf0e10cSrcweir 	throw (cssu::RuntimeException)
225cdf0e10cSrcweir {
226cdf0e10cSrcweir 	return SignatureVerifierImpl_getImplementationName();
227cdf0e10cSrcweir }
supportsService(const rtl::OUString & rServiceName)228cdf0e10cSrcweir sal_Bool SAL_CALL SignatureVerifierImpl::supportsService( const rtl::OUString& rServiceName )
229cdf0e10cSrcweir 	throw (cssu::RuntimeException)
230cdf0e10cSrcweir {
231cdf0e10cSrcweir 	return SignatureVerifierImpl_supportsService( rServiceName );
232cdf0e10cSrcweir }
getSupportedServiceNames()233cdf0e10cSrcweir cssu::Sequence< rtl::OUString > SAL_CALL SignatureVerifierImpl::getSupportedServiceNames(  )
234cdf0e10cSrcweir 	throw (cssu::RuntimeException)
235cdf0e10cSrcweir {
236cdf0e10cSrcweir 	return SignatureVerifierImpl_getSupportedServiceNames();
237cdf0e10cSrcweir }
238cdf0e10cSrcweir 
239