1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "signatureengine.hxx"
22 #include <com/sun/star/xml/crypto/XXMLSignatureTemplate.hpp>
23 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 namespace cssu
= com::sun::star::uno
;
27 namespace cssl
= com::sun::star::lang
;
28 namespace cssxc
= com::sun::star::xml::crypto
;
29 namespace cssxw
= com::sun::star::xml::wrapper
;
31 #define SIGNATURE_TEMPLATE "com.sun.star.xml.crypto.XMLSignatureTemplate"
33 SignatureEngine::SignatureEngine( )
34 :m_nTotalReferenceNumber(-1)
38 bool SignatureEngine::checkReady() const
39 /****** SignatureEngine/checkReady *******************************************
42 * checkReady -- checks the conditions for the main operation.
45 * bReady = checkReady( );
48 * checks whether all following conditions are satisfied:
49 * 1. the main operation has't begun yet;
50 * 2. the key material is known;
51 * 3. the amount of reference is known;
52 * 4. all of referenced elements, the key element and the signature
53 * template are bufferred.
59 * bReady - true if all conditions are satisfied, false otherwise
63 * Email: michael.mi@sun.com
64 ******************************************************************************/
68 sal_Int32 nKeyInc
= 0;
69 if (m_nIdOfKeyEC
!= 0)
76 m_nTotalReferenceNumber
== -1 ||
77 m_nTotalReferenceNumber
+1+nKeyInc
> m_nNumOfResolvedReferences
)
85 void SignatureEngine::tryToPerform( )
86 throw (cssu::Exception
, cssu::RuntimeException
)
87 /****** SignatureEngine/tryToPerform *****************************************
90 * tryToPerform -- tries to perform the signature operation.
96 * if the situation is ready, perform following operations.
97 * 1. prepares a signature template;
98 * 2. calls the signature bridge component;
99 * 3. clears up all used resources;
100 * 4. notifies the result listener;
101 * 5. sets the "accomplishment" flag.
111 * Email: michael.mi@sun.com
112 ******************************************************************************/
116 const OUString
ouSignatureTemplate ( SIGNATURE_TEMPLATE
);
117 cssu::Reference
< cssxc::XXMLSignatureTemplate
>
118 xSignatureTemplate( mxMSF
->createInstance( ouSignatureTemplate
), cssu::UNO_QUERY
);
120 OSL_ASSERT( xSignatureTemplate
.is() );
122 cssu::Reference
< cssxw::XXMLElementWrapper
>
123 xXMLElement
= m_xSAXEventKeeper
->getElement( m_nIdOfTemplateEC
);
125 xSignatureTemplate
->setTemplate(xXMLElement
);
127 std::vector
< sal_Int32
>::const_iterator ii
= m_vReferenceIds
.begin();
129 for( ; ii
!= m_vReferenceIds
.end() ; ++ii
)
131 xXMLElement
= m_xSAXEventKeeper
->getElement( *ii
);
132 xSignatureTemplate
->setTarget(xXMLElement
);
136 * set the Uri binding
138 xSignatureTemplate
->setBinding( this );
140 startEngine( xSignatureTemplate
);
147 notifyResultListener();
149 m_bMissionDone
= true;
153 void SignatureEngine::clearUp( ) const
154 /****** SignatureEngine/clearUp **********************************************
157 * clearUp -- clear up all resources used by this operation.
163 * cleaning resources up includes:
164 * 1. releases the ElementCollector for the signature template element;
165 * 2. releases ElementCollectors for referenced elements;
166 * 3. releases the ElementCollector for the key element, if there is one.
176 * Email: michael.mi@sun.com
177 ******************************************************************************/
179 cssu::Reference
< cssxc::sax::XReferenceResolvedBroadcaster
>
180 xReferenceResolvedBroadcaster( m_xSAXEventKeeper
, cssu::UNO_QUERY
);
181 xReferenceResolvedBroadcaster
->removeReferenceResolvedListener(
183 (const cssu::Reference
< cssxc::sax::XReferenceResolvedListener
>)((SecurityEngine
*)this));
185 m_xSAXEventKeeper
->removeElementCollector(m_nIdOfTemplateEC
);
187 std::vector
< sal_Int32
>::const_iterator ii
= m_vReferenceIds
.begin();
189 for( ; ii
!= m_vReferenceIds
.end() ; ++ii
)
191 xReferenceResolvedBroadcaster
->removeReferenceResolvedListener(
193 (const cssu::Reference
< cssxc::sax::XReferenceResolvedListener
>)((SecurityEngine
*)this));
194 m_xSAXEventKeeper
->removeElementCollector(*ii
);
197 if (m_nIdOfKeyEC
!= 0 && m_nIdOfKeyEC
!= -1)
199 m_xSAXEventKeeper
->removeElementCollector(m_nIdOfKeyEC
);
203 /* XReferenceCollector */
204 void SAL_CALL
SignatureEngine::setReferenceCount( sal_Int32 count
)
205 throw (cssu::Exception
, cssu::RuntimeException
)
207 m_nTotalReferenceNumber
= count
;
211 void SAL_CALL
SignatureEngine::setReferenceId( sal_Int32 id
)
212 throw (cssu::Exception
, cssu::RuntimeException
)
214 m_vReferenceIds
.push_back( id
);
218 void SAL_CALL
SignatureEngine::setUriBinding(
220 const cssu::Reference
< com::sun::star::io::XInputStream
>& aInputStream
)
221 throw (cssu::Exception
, cssu::RuntimeException
)
223 m_vUris
.push_back(uri
);
224 m_vXInputStreams
.push_back(aInputStream
);
227 cssu::Reference
< com::sun::star::io::XInputStream
> SAL_CALL
SignatureEngine::getUriBinding( const OUString
& uri
)
228 throw (cssu::Exception
, cssu::RuntimeException
)
230 cssu::Reference
< com::sun::star::io::XInputStream
> xInputStream
;
232 int size
= m_vUris
.size();
234 for( int i
=0; i
<size
; ++i
)
236 if (m_vUris
[i
] == uri
)
238 xInputStream
= m_vXInputStreams
[i
];
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */