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 "signatureengine.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 SIGNATURE_TEMPLATE "com.sun.star.xml.crypto.XMLSignatureTemplate" 38 39 #define DECLARE_ASCII( SASCIIVALUE ) \ 40 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SASCIIVALUE ) ) 41 42 SignatureEngine::SignatureEngine( ) 43 :m_nTotalReferenceNumber(-1) 44 { 45 } 46 47 bool SignatureEngine::checkReady() const 48 /****** SignatureEngine/checkReady ******************************************* 49 * 50 * NAME 51 * checkReady -- checks the conditions for the main operation. 52 * 53 * SYNOPSIS 54 * bReady = checkReady( ); 55 * 56 * FUNCTION 57 * checks whether all following conditions are satisfied: 58 * 1. the main operation has't begun yet; 59 * 2. the key material is known; 60 * 3. the amount of reference is known; 61 * 4. all of referenced elements, the key element and the signature 62 * template are bufferred. 63 * 64 * INPUTS 65 * empty 66 * 67 * RESULT 68 * bReady - true if all conditions are satisfied, false otherwise 69 * 70 * HISTORY 71 * 05.01.2004 - implemented 72 * 73 * AUTHOR 74 * Michael Mi 75 * Email: michael.mi@sun.com 76 ******************************************************************************/ 77 { 78 bool rc = true; 79 80 sal_Int32 nKeyInc = 0; 81 if (m_nIdOfKeyEC != 0) 82 { 83 nKeyInc = 1; 84 } 85 86 if (m_bMissionDone || 87 m_nIdOfKeyEC == -1 || 88 m_nTotalReferenceNumber == -1 || 89 m_nTotalReferenceNumber+1+nKeyInc > m_nNumOfResolvedReferences) 90 { 91 rc = false; 92 } 93 94 return rc; 95 } 96 97 void SignatureEngine::tryToPerform( ) 98 /****** SignatureEngine/tryToPerform ***************************************** 99 * 100 * NAME 101 * tryToPerform -- tries to perform the signature operation. 102 * 103 * SYNOPSIS 104 * tryToPerform( ); 105 * 106 * FUNCTION 107 * if the situation is ready, perform following operations. 108 * 1. prepares a signature template; 109 * 2. calls the signature bridge component; 110 * 3. clears up all used resources; 111 * 4. notifies the result listener; 112 * 5. sets the "accomplishment" flag. 113 * 114 * INPUTS 115 * empty 116 * 117 * RESULT 118 * empty 119 * 120 * HISTORY 121 * 05.01.2004 - implemented 122 * 123 * AUTHOR 124 * Michael Mi 125 * Email: michael.mi@sun.com 126 ******************************************************************************/ 127 { 128 if (checkReady()) 129 { 130 const rtl::OUString ouSignatureTemplate ( 131 RTL_CONSTASCII_USTRINGPARAM( SIGNATURE_TEMPLATE ) ); 132 cssu::Reference < cssxc::XXMLSignatureTemplate > 133 xSignatureTemplate( mxMSF->createInstance( ouSignatureTemplate ), cssu::UNO_QUERY ); 134 135 OSL_ASSERT( xSignatureTemplate.is() ); 136 137 cssu::Reference< cssxw::XXMLElementWrapper > 138 xXMLElement = m_xSAXEventKeeper->getElement( m_nIdOfTemplateEC ); 139 140 xSignatureTemplate->setTemplate(xXMLElement); 141 142 std::vector< sal_Int32 >::const_iterator ii = m_vReferenceIds.begin(); 143 144 for( ; ii != m_vReferenceIds.end() ; ++ii ) 145 { 146 xXMLElement = m_xSAXEventKeeper->getElement( *ii ); 147 xSignatureTemplate->setTarget(xXMLElement); 148 } 149 150 /* 151 * set the Uri binding 152 */ 153 xSignatureTemplate->setBinding( this ); 154 155 startEngine( xSignatureTemplate ); 156 157 /* 158 * done 159 */ 160 clearUp( ); 161 162 notifyResultListener(); 163 164 m_bMissionDone = true; 165 } 166 } 167 168 void SignatureEngine::clearUp( ) const 169 /****** SignatureEngine/clearUp ********************************************** 170 * 171 * NAME 172 * clearUp -- clear up all resources used by this operation. 173 * 174 * SYNOPSIS 175 * clearUp( ); 176 * 177 * FUNCTION 178 * cleaning resources up includes: 179 * 1. releases the ElementCollector for the signature template element; 180 * 2. releases ElementCollectors for referenced elements; 181 * 3. releases the ElementCollector for the key element, if there is one. 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 cssu::Reference < cssxc::sax::XReferenceResolvedBroadcaster > 198 xReferenceResolvedBroadcaster( m_xSAXEventKeeper, cssu::UNO_QUERY ); 199 xReferenceResolvedBroadcaster->removeReferenceResolvedListener( 200 m_nIdOfTemplateEC, 201 (const cssu::Reference < cssxc::sax::XReferenceResolvedListener >)((SecurityEngine *)this)); 202 203 m_xSAXEventKeeper->removeElementCollector(m_nIdOfTemplateEC); 204 205 std::vector< sal_Int32 >::const_iterator ii = m_vReferenceIds.begin(); 206 207 for( ; ii != m_vReferenceIds.end() ; ++ii ) 208 { 209 xReferenceResolvedBroadcaster->removeReferenceResolvedListener( 210 *ii, 211 (const cssu::Reference < cssxc::sax::XReferenceResolvedListener >)((SecurityEngine *)this)); 212 m_xSAXEventKeeper->removeElementCollector(*ii); 213 } 214 215 if (m_nIdOfKeyEC != 0 && m_nIdOfKeyEC != -1) 216 { 217 m_xSAXEventKeeper->removeElementCollector(m_nIdOfKeyEC); 218 } 219 } 220 221 /* XReferenceCollector */ 222 void SAL_CALL SignatureEngine::setReferenceCount( sal_Int32 count ) 223 { 224 m_nTotalReferenceNumber = count; 225 tryToPerform(); 226 } 227 228 void SAL_CALL SignatureEngine::setReferenceId( sal_Int32 id ) 229 { 230 m_vReferenceIds.push_back( id ); 231 } 232 233 /* XUriBinding */ 234 void SAL_CALL SignatureEngine::setUriBinding( 235 const rtl::OUString& uri, 236 const cssu::Reference< com::sun::star::io::XInputStream >& aInputStream ) 237 { 238 m_vUris.push_back(uri); 239 m_vXInputStreams.push_back(aInputStream); 240 } 241 242 cssu::Reference< com::sun::star::io::XInputStream > SAL_CALL SignatureEngine::getUriBinding( const rtl::OUString& uri ) 243 { 244 cssu::Reference< com::sun::star::io::XInputStream > xInputStream; 245 246 int size = m_vUris.size(); 247 248 for( int i=0; i<size; ++i) 249 { 250 if (m_vUris[i] == uri) 251 { 252 xInputStream = m_vXInputStreams[i]; 253 break; 254 } 255 } 256 257 return xInputStream; 258 } 259