1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
31 #include "signatureengine.hxx"
32 #include <com/sun/star/xml/crypto/XXMLSignatureTemplate.hpp>
33 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
34 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 namespace cssu
= com::sun::star::uno
;
37 namespace cssl
= com::sun::star::lang
;
38 namespace cssxc
= com::sun::star::xml::crypto
;
39 namespace cssxw
= com::sun::star::xml::wrapper
;
41 #define SIGNATURE_TEMPLATE "com.sun.star.xml.crypto.XMLSignatureTemplate"
43 #define DECLARE_ASCII( SASCIIVALUE ) \
44 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SASCIIVALUE ) )
46 SignatureEngine::SignatureEngine( )
47 :m_nTotalReferenceNumber(-1)
51 bool SignatureEngine::checkReady() const
52 /****** SignatureEngine/checkReady *******************************************
55 * checkReady -- checks the conditions for the main operation.
58 * bReady = checkReady( );
61 * checks whether all following conditions are satisfied:
62 * 1. the main operation has't begun yet;
63 * 2. the key material is known;
64 * 3. the amount of reference is known;
65 * 4. all of referenced elements, the key element and the signature
66 * template are bufferred.
72 * bReady - true if all conditions are satisfied, false otherwise
75 * 05.01.2004 - implemented
79 * Email: michael.mi@sun.com
80 ******************************************************************************/
84 sal_Int32 nKeyInc
= 0;
85 if (m_nIdOfKeyEC
!= 0)
92 m_nTotalReferenceNumber
== -1 ||
93 m_nTotalReferenceNumber
+1+nKeyInc
> m_nNumOfResolvedReferences
)
101 void SignatureEngine::tryToPerform( )
102 throw (cssu::Exception
, cssu::RuntimeException
)
103 /****** SignatureEngine/tryToPerform *****************************************
106 * tryToPerform -- tries to perform the signature operation.
112 * if the situation is ready, perform following operations.
113 * 1. prepares a signature template;
114 * 2. calls the signature bridge component;
115 * 3. clears up all used resources;
116 * 4. notifies the result listener;
117 * 5. sets the "accomplishment" flag.
126 * 05.01.2004 - implemented
130 * Email: michael.mi@sun.com
131 ******************************************************************************/
135 const rtl::OUString
ouSignatureTemplate (
136 RTL_CONSTASCII_USTRINGPARAM( SIGNATURE_TEMPLATE
) );
137 cssu::Reference
< cssxc::XXMLSignatureTemplate
>
138 xSignatureTemplate( mxMSF
->createInstance( ouSignatureTemplate
), cssu::UNO_QUERY
);
140 OSL_ASSERT( xSignatureTemplate
.is() );
142 cssu::Reference
< cssxw::XXMLElementWrapper
>
143 xXMLElement
= m_xSAXEventKeeper
->getElement( m_nIdOfTemplateEC
);
145 xSignatureTemplate
->setTemplate(xXMLElement
);
147 std::vector
< sal_Int32
>::const_iterator ii
= m_vReferenceIds
.begin();
149 for( ; ii
!= m_vReferenceIds
.end() ; ++ii
)
151 xXMLElement
= m_xSAXEventKeeper
->getElement( *ii
);
152 xSignatureTemplate
->setTarget(xXMLElement
);
156 * set the Uri binding
158 xSignatureTemplate
->setBinding( this );
160 startEngine( xSignatureTemplate
);
167 notifyResultListener();
169 m_bMissionDone
= true;
173 void SignatureEngine::clearUp( ) const
174 /****** SignatureEngine/clearUp **********************************************
177 * clearUp -- clear up all resources used by this operation.
183 * cleaning resources up includes:
184 * 1. releases the ElementCollector for the signature template element;
185 * 2. releases ElementCollectors for referenced elements;
186 * 3. releases the ElementCollector for the key element, if there is one.
195 * 05.01.2004 - implemented
199 * Email: michael.mi@sun.com
200 ******************************************************************************/
202 cssu::Reference
< cssxc::sax::XReferenceResolvedBroadcaster
>
203 xReferenceResolvedBroadcaster( m_xSAXEventKeeper
, cssu::UNO_QUERY
);
204 xReferenceResolvedBroadcaster
->removeReferenceResolvedListener(
206 (const cssu::Reference
< cssxc::sax::XReferenceResolvedListener
>)((SecurityEngine
*)this));
208 m_xSAXEventKeeper
->removeElementCollector(m_nIdOfTemplateEC
);
210 std::vector
< sal_Int32
>::const_iterator ii
= m_vReferenceIds
.begin();
212 for( ; ii
!= m_vReferenceIds
.end() ; ++ii
)
214 xReferenceResolvedBroadcaster
->removeReferenceResolvedListener(
216 (const cssu::Reference
< cssxc::sax::XReferenceResolvedListener
>)((SecurityEngine
*)this));
217 m_xSAXEventKeeper
->removeElementCollector(*ii
);
220 if (m_nIdOfKeyEC
!= 0 && m_nIdOfKeyEC
!= -1)
222 m_xSAXEventKeeper
->removeElementCollector(m_nIdOfKeyEC
);
226 /* XReferenceCollector */
227 void SAL_CALL
SignatureEngine::setReferenceCount( sal_Int32 count
)
228 throw (cssu::Exception
, cssu::RuntimeException
)
230 m_nTotalReferenceNumber
= count
;
234 void SAL_CALL
SignatureEngine::setReferenceId( sal_Int32 id
)
235 throw (cssu::Exception
, cssu::RuntimeException
)
237 m_vReferenceIds
.push_back( id
);
241 void SAL_CALL
SignatureEngine::setUriBinding(
242 const rtl::OUString
& uri
,
243 const cssu::Reference
< com::sun::star::io::XInputStream
>& aInputStream
)
244 throw (cssu::Exception
, cssu::RuntimeException
)
246 m_vUris
.push_back(uri
);
247 m_vXInputStreams
.push_back(aInputStream
);
250 cssu::Reference
< com::sun::star::io::XInputStream
> SAL_CALL
SignatureEngine::getUriBinding( const rtl::OUString
& uri
)
251 throw (cssu::Exception
, cssu::RuntimeException
)
253 cssu::Reference
< com::sun::star::io::XInputStream
> xInputStream
;
255 int size
= m_vUris
.size();
257 for( int i
=0; i
<size
; ++i
)
259 if (m_vUris
[i
] == uri
)
261 xInputStream
= m_vXInputStreams
[i
];