1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: signatureengine.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
34 #include "signatureengine.hxx"
35 #include <com/sun/star/xml/crypto/XXMLSignatureTemplate.hpp>
36 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39 namespace cssu
= com::sun::star::uno
;
40 namespace cssl
= com::sun::star::lang
;
41 namespace cssxc
= com::sun::star::xml::crypto
;
42 namespace cssxw
= com::sun::star::xml::wrapper
;
44 #define SIGNATURE_TEMPLATE "com.sun.star.xml.crypto.XMLSignatureTemplate"
46 #define DECLARE_ASCII( SASCIIVALUE ) \
47 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SASCIIVALUE ) )
49 SignatureEngine::SignatureEngine( )
50 :m_nTotalReferenceNumber(-1)
54 bool SignatureEngine::checkReady() const
55 /****** SignatureEngine/checkReady *******************************************
58 * checkReady -- checks the conditions for the main operation.
61 * bReady = checkReady( );
64 * checks whether all following conditions are satisfied:
65 * 1. the main operation has't begun yet;
66 * 2. the key material is known;
67 * 3. the amount of reference is known;
68 * 4. all of referenced elements, the key element and the signature
69 * template are bufferred.
75 * bReady - true if all conditions are satisfied, false otherwise
78 * 05.01.2004 - implemented
82 * Email: michael.mi@sun.com
83 ******************************************************************************/
87 sal_Int32 nKeyInc
= 0;
88 if (m_nIdOfKeyEC
!= 0)
95 m_nTotalReferenceNumber
== -1 ||
96 m_nTotalReferenceNumber
+1+nKeyInc
> m_nNumOfResolvedReferences
)
104 void SignatureEngine::tryToPerform( )
105 throw (cssu::Exception
, cssu::RuntimeException
)
106 /****** SignatureEngine/tryToPerform *****************************************
109 * tryToPerform -- tries to perform the signature operation.
115 * if the situation is ready, perform following operations.
116 * 1. prepares a signature template;
117 * 2. calls the signature bridge component;
118 * 3. clears up all used resources;
119 * 4. notifies the result listener;
120 * 5. sets the "accomplishment" flag.
129 * 05.01.2004 - implemented
133 * Email: michael.mi@sun.com
134 ******************************************************************************/
138 const rtl::OUString
ouSignatureTemplate (
139 RTL_CONSTASCII_USTRINGPARAM( SIGNATURE_TEMPLATE
) );
140 cssu::Reference
< cssxc::XXMLSignatureTemplate
>
141 xSignatureTemplate( mxMSF
->createInstance( ouSignatureTemplate
), cssu::UNO_QUERY
);
143 OSL_ASSERT( xSignatureTemplate
.is() );
145 cssu::Reference
< cssxw::XXMLElementWrapper
>
146 xXMLElement
= m_xSAXEventKeeper
->getElement( m_nIdOfTemplateEC
);
148 xSignatureTemplate
->setTemplate(xXMLElement
);
150 std::vector
< sal_Int32
>::const_iterator ii
= m_vReferenceIds
.begin();
152 for( ; ii
!= m_vReferenceIds
.end() ; ++ii
)
154 xXMLElement
= m_xSAXEventKeeper
->getElement( *ii
);
155 xSignatureTemplate
->setTarget(xXMLElement
);
159 * set the Uri binding
161 xSignatureTemplate
->setBinding( this );
163 startEngine( xSignatureTemplate
);
170 notifyResultListener();
172 m_bMissionDone
= true;
176 void SignatureEngine::clearUp( ) const
177 /****** SignatureEngine/clearUp **********************************************
180 * clearUp -- clear up all resources used by this operation.
186 * cleaning resources up includes:
187 * 1. releases the ElementCollector for the signature template element;
188 * 2. releases ElementCollectors for referenced elements;
189 * 3. releases the ElementCollector for the key element, if there is one.
198 * 05.01.2004 - implemented
202 * Email: michael.mi@sun.com
203 ******************************************************************************/
205 cssu::Reference
< cssxc::sax::XReferenceResolvedBroadcaster
>
206 xReferenceResolvedBroadcaster( m_xSAXEventKeeper
, cssu::UNO_QUERY
);
207 xReferenceResolvedBroadcaster
->removeReferenceResolvedListener(
209 (const cssu::Reference
< cssxc::sax::XReferenceResolvedListener
>)((SecurityEngine
*)this));
211 m_xSAXEventKeeper
->removeElementCollector(m_nIdOfTemplateEC
);
213 std::vector
< sal_Int32
>::const_iterator ii
= m_vReferenceIds
.begin();
215 for( ; ii
!= m_vReferenceIds
.end() ; ++ii
)
217 xReferenceResolvedBroadcaster
->removeReferenceResolvedListener(
219 (const cssu::Reference
< cssxc::sax::XReferenceResolvedListener
>)((SecurityEngine
*)this));
220 m_xSAXEventKeeper
->removeElementCollector(*ii
);
223 if (m_nIdOfKeyEC
!= 0 && m_nIdOfKeyEC
!= -1)
225 m_xSAXEventKeeper
->removeElementCollector(m_nIdOfKeyEC
);
229 /* XReferenceCollector */
230 void SAL_CALL
SignatureEngine::setReferenceCount( sal_Int32 count
)
231 throw (cssu::Exception
, cssu::RuntimeException
)
233 m_nTotalReferenceNumber
= count
;
237 void SAL_CALL
SignatureEngine::setReferenceId( sal_Int32 id
)
238 throw (cssu::Exception
, cssu::RuntimeException
)
240 m_vReferenceIds
.push_back( id
);
244 void SAL_CALL
SignatureEngine::setUriBinding(
245 const rtl::OUString
& uri
,
246 const cssu::Reference
< com::sun::star::io::XInputStream
>& aInputStream
)
247 throw (cssu::Exception
, cssu::RuntimeException
)
249 m_vUris
.push_back(uri
);
250 m_vXInputStreams
.push_back(aInputStream
);
253 cssu::Reference
< com::sun::star::io::XInputStream
> SAL_CALL
SignatureEngine::getUriBinding( const rtl::OUString
& uri
)
254 throw (cssu::Exception
, cssu::RuntimeException
)
256 cssu::Reference
< com::sun::star::io::XInputStream
> xInputStream
;
258 int size
= m_vUris
.size();
260 for( int i
=0; i
<size
; ++i
)
262 if (m_vUris
[i
] == uri
)
264 xInputStream
= m_vXInputStreams
[i
];