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/XMLSignatureTemplate.hpp>
23 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 using namespace com::sun::star::uno
;
27 namespace cssu
= com::sun::star::uno
;
28 namespace cssl
= com::sun::star::lang
;
29 namespace cssxc
= com::sun::star::xml::crypto
;
30 namespace cssxw
= com::sun::star::xml::wrapper
;
32 SignatureEngine::SignatureEngine( const Reference
<XComponentContext
> & xContext
)
33 : m_xContext(xContext
), m_nTotalReferenceNumber(-1)
37 bool SignatureEngine::checkReady() const
38 /****** SignatureEngine/checkReady *******************************************
41 * checkReady -- checks the conditions for the main operation.
44 * bReady = checkReady( );
47 * checks whether all following conditions are satisfied:
48 * 1. the main operation has't begun yet;
49 * 2. the key material is known;
50 * 3. the amount of reference is known;
51 * 4. all of referenced elements, the key element and the signature
52 * template are bufferred.
58 * bReady - true if all conditions are satisfied, false otherwise
62 * Email: michael.mi@sun.com
63 ******************************************************************************/
67 sal_Int32 nKeyInc
= 0;
68 if (m_nIdOfKeyEC
!= 0)
75 m_nTotalReferenceNumber
== -1 ||
76 m_nTotalReferenceNumber
+1+nKeyInc
> m_nNumOfResolvedReferences
)
84 void SignatureEngine::tryToPerform( )
85 throw (cssu::Exception
, cssu::RuntimeException
)
86 /****** SignatureEngine/tryToPerform *****************************************
89 * tryToPerform -- tries to perform the signature operation.
95 * if the situation is ready, perform following operations.
96 * 1. prepares a signature template;
97 * 2. calls the signature bridge component;
98 * 3. clears up all used resources;
99 * 4. notifies the result listener;
100 * 5. sets the "accomplishment" flag.
110 * Email: michael.mi@sun.com
111 ******************************************************************************/
115 cssu::Reference
< cssxc::XXMLSignatureTemplate
>
116 xSignatureTemplate
= cssxc::XMLSignatureTemplate::create( m_xContext
);
118 cssu::Reference
< cssxw::XXMLElementWrapper
>
119 xXMLElement
= m_xSAXEventKeeper
->getElement( m_nIdOfTemplateEC
);
121 xSignatureTemplate
->setTemplate(xXMLElement
);
123 std::vector
< sal_Int32
>::const_iterator ii
= m_vReferenceIds
.begin();
125 for( ; ii
!= m_vReferenceIds
.end() ; ++ii
)
127 xXMLElement
= m_xSAXEventKeeper
->getElement( *ii
);
128 xSignatureTemplate
->setTarget(xXMLElement
);
132 * set the Uri binding
134 xSignatureTemplate
->setBinding( this );
136 startEngine( xSignatureTemplate
);
143 notifyResultListener();
145 m_bMissionDone
= true;
149 void SignatureEngine::clearUp( ) const
150 /****** SignatureEngine/clearUp **********************************************
153 * clearUp -- clear up all resources used by this operation.
159 * cleaning resources up includes:
160 * 1. releases the ElementCollector for the signature template element;
161 * 2. releases ElementCollectors for referenced elements;
162 * 3. releases the ElementCollector for the key element, if there is one.
172 * Email: michael.mi@sun.com
173 ******************************************************************************/
175 cssu::Reference
< cssxc::sax::XReferenceResolvedBroadcaster
>
176 xReferenceResolvedBroadcaster( m_xSAXEventKeeper
, cssu::UNO_QUERY
);
177 xReferenceResolvedBroadcaster
->removeReferenceResolvedListener(
179 static_cast<const cssu::Reference
< cssxc::sax::XReferenceResolvedListener
> >((SecurityEngine
*)this));
181 m_xSAXEventKeeper
->removeElementCollector(m_nIdOfTemplateEC
);
183 std::vector
< sal_Int32
>::const_iterator ii
= m_vReferenceIds
.begin();
185 for( ; ii
!= m_vReferenceIds
.end() ; ++ii
)
187 xReferenceResolvedBroadcaster
->removeReferenceResolvedListener(
189 static_cast<const cssu::Reference
< cssxc::sax::XReferenceResolvedListener
> >((SecurityEngine
*)this));
190 m_xSAXEventKeeper
->removeElementCollector(*ii
);
193 if (m_nIdOfKeyEC
!= 0 && m_nIdOfKeyEC
!= -1)
195 m_xSAXEventKeeper
->removeElementCollector(m_nIdOfKeyEC
);
199 /* XReferenceCollector */
200 void SAL_CALL
SignatureEngine::setReferenceCount( sal_Int32 count
)
201 throw (cssu::Exception
, cssu::RuntimeException
, std::exception
)
203 m_nTotalReferenceNumber
= count
;
207 void SAL_CALL
SignatureEngine::setReferenceId( sal_Int32 id
)
208 throw (cssu::Exception
, cssu::RuntimeException
, std::exception
)
210 m_vReferenceIds
.push_back( id
);
214 void SAL_CALL
SignatureEngine::setUriBinding(
216 const cssu::Reference
< com::sun::star::io::XInputStream
>& aInputStream
)
217 throw (cssu::Exception
, cssu::RuntimeException
, std::exception
)
219 m_vUris
.push_back(uri
);
220 m_vXInputStreams
.push_back(aInputStream
);
223 cssu::Reference
< com::sun::star::io::XInputStream
> SAL_CALL
SignatureEngine::getUriBinding( const OUString
& uri
)
224 throw (cssu::Exception
, cssu::RuntimeException
, std::exception
)
226 cssu::Reference
< com::sun::star::io::XInputStream
> xInputStream
;
228 int size
= m_vUris
.size();
230 for( int i
=0; i
<size
; ++i
)
232 if (m_vUris
[i
] == uri
)
234 xInputStream
= m_vXInputStreams
[i
];
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */