1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_xmlsecurity.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include "signatureengine.hxx" 32*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XXMLSignatureTemplate.hpp> 33*cdf0e10cSrcweir #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp> 34*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp> 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir namespace cssu = com::sun::star::uno; 37*cdf0e10cSrcweir namespace cssl = com::sun::star::lang; 38*cdf0e10cSrcweir namespace cssxc = com::sun::star::xml::crypto; 39*cdf0e10cSrcweir namespace cssxw = com::sun::star::xml::wrapper; 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir #define SIGNATURE_TEMPLATE "com.sun.star.xml.crypto.XMLSignatureTemplate" 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir #define DECLARE_ASCII( SASCIIVALUE ) \ 44*cdf0e10cSrcweir rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SASCIIVALUE ) ) 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir SignatureEngine::SignatureEngine( ) 47*cdf0e10cSrcweir :m_nTotalReferenceNumber(-1) 48*cdf0e10cSrcweir { 49*cdf0e10cSrcweir } 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir bool SignatureEngine::checkReady() const 52*cdf0e10cSrcweir /****** SignatureEngine/checkReady ******************************************* 53*cdf0e10cSrcweir * 54*cdf0e10cSrcweir * NAME 55*cdf0e10cSrcweir * checkReady -- checks the conditions for the main operation. 56*cdf0e10cSrcweir * 57*cdf0e10cSrcweir * SYNOPSIS 58*cdf0e10cSrcweir * bReady = checkReady( ); 59*cdf0e10cSrcweir * 60*cdf0e10cSrcweir * FUNCTION 61*cdf0e10cSrcweir * checks whether all following conditions are satisfied: 62*cdf0e10cSrcweir * 1. the main operation has't begun yet; 63*cdf0e10cSrcweir * 2. the key material is known; 64*cdf0e10cSrcweir * 3. the amount of reference is known; 65*cdf0e10cSrcweir * 4. all of referenced elements, the key element and the signature 66*cdf0e10cSrcweir * template are bufferred. 67*cdf0e10cSrcweir * 68*cdf0e10cSrcweir * INPUTS 69*cdf0e10cSrcweir * empty 70*cdf0e10cSrcweir * 71*cdf0e10cSrcweir * RESULT 72*cdf0e10cSrcweir * bReady - true if all conditions are satisfied, false otherwise 73*cdf0e10cSrcweir * 74*cdf0e10cSrcweir * HISTORY 75*cdf0e10cSrcweir * 05.01.2004 - implemented 76*cdf0e10cSrcweir * 77*cdf0e10cSrcweir * AUTHOR 78*cdf0e10cSrcweir * Michael Mi 79*cdf0e10cSrcweir * Email: michael.mi@sun.com 80*cdf0e10cSrcweir ******************************************************************************/ 81*cdf0e10cSrcweir { 82*cdf0e10cSrcweir bool rc = true; 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir sal_Int32 nKeyInc = 0; 85*cdf0e10cSrcweir if (m_nIdOfKeyEC != 0) 86*cdf0e10cSrcweir { 87*cdf0e10cSrcweir nKeyInc = 1; 88*cdf0e10cSrcweir } 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir if (m_bMissionDone || 91*cdf0e10cSrcweir m_nIdOfKeyEC == -1 || 92*cdf0e10cSrcweir m_nTotalReferenceNumber == -1 || 93*cdf0e10cSrcweir m_nTotalReferenceNumber+1+nKeyInc > m_nNumOfResolvedReferences) 94*cdf0e10cSrcweir { 95*cdf0e10cSrcweir rc = false; 96*cdf0e10cSrcweir } 97*cdf0e10cSrcweir 98*cdf0e10cSrcweir return rc; 99*cdf0e10cSrcweir } 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir void SignatureEngine::tryToPerform( ) 102*cdf0e10cSrcweir throw (cssu::Exception, cssu::RuntimeException) 103*cdf0e10cSrcweir /****** SignatureEngine/tryToPerform ***************************************** 104*cdf0e10cSrcweir * 105*cdf0e10cSrcweir * NAME 106*cdf0e10cSrcweir * tryToPerform -- tries to perform the signature operation. 107*cdf0e10cSrcweir * 108*cdf0e10cSrcweir * SYNOPSIS 109*cdf0e10cSrcweir * tryToPerform( ); 110*cdf0e10cSrcweir * 111*cdf0e10cSrcweir * FUNCTION 112*cdf0e10cSrcweir * if the situation is ready, perform following operations. 113*cdf0e10cSrcweir * 1. prepares a signature template; 114*cdf0e10cSrcweir * 2. calls the signature bridge component; 115*cdf0e10cSrcweir * 3. clears up all used resources; 116*cdf0e10cSrcweir * 4. notifies the result listener; 117*cdf0e10cSrcweir * 5. sets the "accomplishment" flag. 118*cdf0e10cSrcweir * 119*cdf0e10cSrcweir * INPUTS 120*cdf0e10cSrcweir * empty 121*cdf0e10cSrcweir * 122*cdf0e10cSrcweir * RESULT 123*cdf0e10cSrcweir * empty 124*cdf0e10cSrcweir * 125*cdf0e10cSrcweir * HISTORY 126*cdf0e10cSrcweir * 05.01.2004 - implemented 127*cdf0e10cSrcweir * 128*cdf0e10cSrcweir * AUTHOR 129*cdf0e10cSrcweir * Michael Mi 130*cdf0e10cSrcweir * Email: michael.mi@sun.com 131*cdf0e10cSrcweir ******************************************************************************/ 132*cdf0e10cSrcweir { 133*cdf0e10cSrcweir if (checkReady()) 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir const rtl::OUString ouSignatureTemplate ( 136*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM( SIGNATURE_TEMPLATE ) ); 137*cdf0e10cSrcweir cssu::Reference < cssxc::XXMLSignatureTemplate > 138*cdf0e10cSrcweir xSignatureTemplate( mxMSF->createInstance( ouSignatureTemplate ), cssu::UNO_QUERY ); 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir OSL_ASSERT( xSignatureTemplate.is() ); 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir cssu::Reference< cssxw::XXMLElementWrapper > 143*cdf0e10cSrcweir xXMLElement = m_xSAXEventKeeper->getElement( m_nIdOfTemplateEC ); 144*cdf0e10cSrcweir 145*cdf0e10cSrcweir xSignatureTemplate->setTemplate(xXMLElement); 146*cdf0e10cSrcweir 147*cdf0e10cSrcweir std::vector< sal_Int32 >::const_iterator ii = m_vReferenceIds.begin(); 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir for( ; ii != m_vReferenceIds.end() ; ++ii ) 150*cdf0e10cSrcweir { 151*cdf0e10cSrcweir xXMLElement = m_xSAXEventKeeper->getElement( *ii ); 152*cdf0e10cSrcweir xSignatureTemplate->setTarget(xXMLElement); 153*cdf0e10cSrcweir } 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir /* 156*cdf0e10cSrcweir * set the Uri binding 157*cdf0e10cSrcweir */ 158*cdf0e10cSrcweir xSignatureTemplate->setBinding( this ); 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir startEngine( xSignatureTemplate ); 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir /* 163*cdf0e10cSrcweir * done 164*cdf0e10cSrcweir */ 165*cdf0e10cSrcweir clearUp( ); 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir notifyResultListener(); 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir m_bMissionDone = true; 170*cdf0e10cSrcweir } 171*cdf0e10cSrcweir } 172*cdf0e10cSrcweir 173*cdf0e10cSrcweir void SignatureEngine::clearUp( ) const 174*cdf0e10cSrcweir /****** SignatureEngine/clearUp ********************************************** 175*cdf0e10cSrcweir * 176*cdf0e10cSrcweir * NAME 177*cdf0e10cSrcweir * clearUp -- clear up all resources used by this operation. 178*cdf0e10cSrcweir * 179*cdf0e10cSrcweir * SYNOPSIS 180*cdf0e10cSrcweir * clearUp( ); 181*cdf0e10cSrcweir * 182*cdf0e10cSrcweir * FUNCTION 183*cdf0e10cSrcweir * cleaning resources up includes: 184*cdf0e10cSrcweir * 1. releases the ElementCollector for the signature template element; 185*cdf0e10cSrcweir * 2. releases ElementCollectors for referenced elements; 186*cdf0e10cSrcweir * 3. releases the ElementCollector for the key element, if there is one. 187*cdf0e10cSrcweir * 188*cdf0e10cSrcweir * INPUTS 189*cdf0e10cSrcweir * empty 190*cdf0e10cSrcweir * 191*cdf0e10cSrcweir * RESULT 192*cdf0e10cSrcweir * empty 193*cdf0e10cSrcweir * 194*cdf0e10cSrcweir * HISTORY 195*cdf0e10cSrcweir * 05.01.2004 - implemented 196*cdf0e10cSrcweir * 197*cdf0e10cSrcweir * AUTHOR 198*cdf0e10cSrcweir * Michael Mi 199*cdf0e10cSrcweir * Email: michael.mi@sun.com 200*cdf0e10cSrcweir ******************************************************************************/ 201*cdf0e10cSrcweir { 202*cdf0e10cSrcweir cssu::Reference < cssxc::sax::XReferenceResolvedBroadcaster > 203*cdf0e10cSrcweir xReferenceResolvedBroadcaster( m_xSAXEventKeeper, cssu::UNO_QUERY ); 204*cdf0e10cSrcweir xReferenceResolvedBroadcaster->removeReferenceResolvedListener( 205*cdf0e10cSrcweir m_nIdOfTemplateEC, 206*cdf0e10cSrcweir (const cssu::Reference < cssxc::sax::XReferenceResolvedListener >)((SecurityEngine *)this)); 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir m_xSAXEventKeeper->removeElementCollector(m_nIdOfTemplateEC); 209*cdf0e10cSrcweir 210*cdf0e10cSrcweir std::vector< sal_Int32 >::const_iterator ii = m_vReferenceIds.begin(); 211*cdf0e10cSrcweir 212*cdf0e10cSrcweir for( ; ii != m_vReferenceIds.end() ; ++ii ) 213*cdf0e10cSrcweir { 214*cdf0e10cSrcweir xReferenceResolvedBroadcaster->removeReferenceResolvedListener( 215*cdf0e10cSrcweir *ii, 216*cdf0e10cSrcweir (const cssu::Reference < cssxc::sax::XReferenceResolvedListener >)((SecurityEngine *)this)); 217*cdf0e10cSrcweir m_xSAXEventKeeper->removeElementCollector(*ii); 218*cdf0e10cSrcweir } 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir if (m_nIdOfKeyEC != 0 && m_nIdOfKeyEC != -1) 221*cdf0e10cSrcweir { 222*cdf0e10cSrcweir m_xSAXEventKeeper->removeElementCollector(m_nIdOfKeyEC); 223*cdf0e10cSrcweir } 224*cdf0e10cSrcweir } 225*cdf0e10cSrcweir 226*cdf0e10cSrcweir /* XReferenceCollector */ 227*cdf0e10cSrcweir void SAL_CALL SignatureEngine::setReferenceCount( sal_Int32 count ) 228*cdf0e10cSrcweir throw (cssu::Exception, cssu::RuntimeException) 229*cdf0e10cSrcweir { 230*cdf0e10cSrcweir m_nTotalReferenceNumber = count; 231*cdf0e10cSrcweir tryToPerform(); 232*cdf0e10cSrcweir } 233*cdf0e10cSrcweir 234*cdf0e10cSrcweir void SAL_CALL SignatureEngine::setReferenceId( sal_Int32 id ) 235*cdf0e10cSrcweir throw (cssu::Exception, cssu::RuntimeException) 236*cdf0e10cSrcweir { 237*cdf0e10cSrcweir m_vReferenceIds.push_back( id ); 238*cdf0e10cSrcweir } 239*cdf0e10cSrcweir 240*cdf0e10cSrcweir /* XUriBinding */ 241*cdf0e10cSrcweir void SAL_CALL SignatureEngine::setUriBinding( 242*cdf0e10cSrcweir const rtl::OUString& uri, 243*cdf0e10cSrcweir const cssu::Reference< com::sun::star::io::XInputStream >& aInputStream ) 244*cdf0e10cSrcweir throw (cssu::Exception, cssu::RuntimeException) 245*cdf0e10cSrcweir { 246*cdf0e10cSrcweir m_vUris.push_back(uri); 247*cdf0e10cSrcweir m_vXInputStreams.push_back(aInputStream); 248*cdf0e10cSrcweir } 249*cdf0e10cSrcweir 250*cdf0e10cSrcweir cssu::Reference< com::sun::star::io::XInputStream > SAL_CALL SignatureEngine::getUriBinding( const rtl::OUString& uri ) 251*cdf0e10cSrcweir throw (cssu::Exception, cssu::RuntimeException) 252*cdf0e10cSrcweir { 253*cdf0e10cSrcweir cssu::Reference< com::sun::star::io::XInputStream > xInputStream; 254*cdf0e10cSrcweir 255*cdf0e10cSrcweir int size = m_vUris.size(); 256*cdf0e10cSrcweir 257*cdf0e10cSrcweir for( int i=0; i<size; ++i) 258*cdf0e10cSrcweir { 259*cdf0e10cSrcweir if (m_vUris[i] == uri) 260*cdf0e10cSrcweir { 261*cdf0e10cSrcweir xInputStream = m_vXInputStreams[i]; 262*cdf0e10cSrcweir break; 263*cdf0e10cSrcweir } 264*cdf0e10cSrcweir } 265*cdf0e10cSrcweir 266*cdf0e10cSrcweir return xInputStream; 267*cdf0e10cSrcweir } 268*cdf0e10cSrcweir 269