Bump version to 5.0-14
[LibreOffice.git] / xmlsecurity / source / framework / signatureengine.cxx
blob655b317b7e7c5b992926e6995d201d0422a15b92
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 *******************************************
40 * NAME
41 * checkReady -- checks the conditions for the main operation.
43 * SYNOPSIS
44 * bReady = checkReady( );
46 * FUNCTION
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.
54 * INPUTS
55 * empty
57 * RESULT
58 * bReady - true if all conditions are satisfied, false otherwise
60 * AUTHOR
61 * Michael Mi
62 * Email: michael.mi@sun.com
63 ******************************************************************************/
65 bool rc = true;
67 sal_Int32 nKeyInc = 0;
68 if (m_nIdOfKeyEC != 0)
70 nKeyInc = 1;
73 if (m_bMissionDone ||
74 m_nIdOfKeyEC == -1 ||
75 m_nTotalReferenceNumber == -1 ||
76 m_nTotalReferenceNumber+1+nKeyInc > m_nNumOfResolvedReferences)
78 rc = false;
81 return rc;
84 void SignatureEngine::tryToPerform( )
85 throw (cssu::Exception, cssu::RuntimeException)
86 /****** SignatureEngine/tryToPerform *****************************************
88 * NAME
89 * tryToPerform -- tries to perform the signature operation.
91 * SYNOPSIS
92 * tryToPerform( );
94 * FUNCTION
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.
102 * INPUTS
103 * empty
105 * RESULT
106 * empty
108 * AUTHOR
109 * Michael Mi
110 * Email: michael.mi@sun.com
111 ******************************************************************************/
113 if (checkReady())
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 );
139 * done
141 clearUp( );
143 notifyResultListener();
145 m_bMissionDone = true;
149 void SignatureEngine::clearUp( ) const
150 /****** SignatureEngine/clearUp **********************************************
152 * NAME
153 * clearUp -- clear up all resources used by this operation.
155 * SYNOPSIS
156 * clearUp( );
158 * FUNCTION
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.
164 * INPUTS
165 * empty
167 * RESULT
168 * empty
170 * AUTHOR
171 * Michael Mi
172 * Email: michael.mi@sun.com
173 ******************************************************************************/
175 cssu::Reference < cssxc::sax::XReferenceResolvedBroadcaster >
176 xReferenceResolvedBroadcaster( m_xSAXEventKeeper, cssu::UNO_QUERY );
177 xReferenceResolvedBroadcaster->removeReferenceResolvedListener(
178 m_nIdOfTemplateEC,
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(
188 *ii,
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;
204 tryToPerform();
207 void SAL_CALL SignatureEngine::setReferenceId( sal_Int32 id )
208 throw (cssu::Exception, cssu::RuntimeException, std::exception)
210 m_vReferenceIds.push_back( id );
213 /* XUriBinding */
214 void SAL_CALL SignatureEngine::setUriBinding(
215 const OUString& uri,
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];
235 break;
239 return xInputStream;
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */