Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
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>
25 :
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;
31 :
32 2 : SignatureEngine::SignatureEngine( const Reference<XComponentContext> & xContext)
33 2 : : m_xContext(xContext), m_nTotalReferenceNumber(-1)
34 : {
35 2 : }
36 :
37 0 : bool SignatureEngine::checkReady() const
38 : /****** SignatureEngine/checkReady *******************************************
39 : *
40 : * NAME
41 : * checkReady -- checks the conditions for the main operation.
42 : *
43 : * SYNOPSIS
44 : * bReady = checkReady( );
45 : *
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.
53 : *
54 : * INPUTS
55 : * empty
56 : *
57 : * RESULT
58 : * bReady - true if all conditions are satisfied, false otherwise
59 : *
60 : * AUTHOR
61 : * Michael Mi
62 : * Email: michael.mi@sun.com
63 : ******************************************************************************/
64 : {
65 0 : bool rc = true;
66 :
67 0 : sal_Int32 nKeyInc = 0;
68 0 : if (m_nIdOfKeyEC != 0)
69 : {
70 0 : nKeyInc = 1;
71 : }
72 :
73 0 : if (m_bMissionDone ||
74 0 : m_nIdOfKeyEC == -1 ||
75 0 : m_nTotalReferenceNumber == -1 ||
76 0 : m_nTotalReferenceNumber+1+nKeyInc > m_nNumOfResolvedReferences)
77 : {
78 0 : rc = false;
79 : }
80 :
81 0 : return rc;
82 : }
83 :
84 0 : void SignatureEngine::tryToPerform( )
85 : throw (cssu::Exception, cssu::RuntimeException)
86 : /****** SignatureEngine/tryToPerform *****************************************
87 : *
88 : * NAME
89 : * tryToPerform -- tries to perform the signature operation.
90 : *
91 : * SYNOPSIS
92 : * tryToPerform( );
93 : *
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.
101 : *
102 : * INPUTS
103 : * empty
104 : *
105 : * RESULT
106 : * empty
107 : *
108 : * AUTHOR
109 : * Michael Mi
110 : * Email: michael.mi@sun.com
111 : ******************************************************************************/
112 : {
113 0 : if (checkReady())
114 : {
115 : cssu::Reference < cssxc::XXMLSignatureTemplate >
116 0 : xSignatureTemplate = cssxc::XMLSignatureTemplate::create( m_xContext );
117 :
118 : cssu::Reference< cssxw::XXMLElementWrapper >
119 0 : xXMLElement = m_xSAXEventKeeper->getElement( m_nIdOfTemplateEC );
120 :
121 0 : xSignatureTemplate->setTemplate(xXMLElement);
122 :
123 0 : std::vector< sal_Int32 >::const_iterator ii = m_vReferenceIds.begin();
124 :
125 0 : for( ; ii != m_vReferenceIds.end() ; ++ii )
126 : {
127 0 : xXMLElement = m_xSAXEventKeeper->getElement( *ii );
128 0 : xSignatureTemplate->setTarget(xXMLElement);
129 : }
130 :
131 : /*
132 : * set the Uri binding
133 : */
134 0 : xSignatureTemplate->setBinding( this );
135 :
136 0 : startEngine( xSignatureTemplate );
137 :
138 : /*
139 : * done
140 : */
141 0 : clearUp( );
142 :
143 0 : notifyResultListener();
144 :
145 0 : m_bMissionDone = true;
146 : }
147 0 : }
148 :
149 0 : void SignatureEngine::clearUp( ) const
150 : /****** SignatureEngine/clearUp **********************************************
151 : *
152 : * NAME
153 : * clearUp -- clear up all resources used by this operation.
154 : *
155 : * SYNOPSIS
156 : * clearUp( );
157 : *
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.
163 : *
164 : * INPUTS
165 : * empty
166 : *
167 : * RESULT
168 : * empty
169 : *
170 : * AUTHOR
171 : * Michael Mi
172 : * Email: michael.mi@sun.com
173 : ******************************************************************************/
174 : {
175 : cssu::Reference < cssxc::sax::XReferenceResolvedBroadcaster >
176 0 : xReferenceResolvedBroadcaster( m_xSAXEventKeeper, cssu::UNO_QUERY );
177 0 : xReferenceResolvedBroadcaster->removeReferenceResolvedListener(
178 : m_nIdOfTemplateEC,
179 0 : static_cast<const cssu::Reference < cssxc::sax::XReferenceResolvedListener > >(static_cast<SecurityEngine *>(const_cast<SignatureEngine *>(this))));
180 :
181 0 : m_xSAXEventKeeper->removeElementCollector(m_nIdOfTemplateEC);
182 :
183 0 : std::vector< sal_Int32 >::const_iterator ii = m_vReferenceIds.begin();
184 :
185 0 : for( ; ii != m_vReferenceIds.end() ; ++ii )
186 : {
187 0 : xReferenceResolvedBroadcaster->removeReferenceResolvedListener(
188 0 : *ii,
189 0 : static_cast<const cssu::Reference < cssxc::sax::XReferenceResolvedListener > >(static_cast<SecurityEngine *>(const_cast<SignatureEngine *>(this))));
190 0 : m_xSAXEventKeeper->removeElementCollector(*ii);
191 : }
192 :
193 0 : if (m_nIdOfKeyEC != 0 && m_nIdOfKeyEC != -1)
194 : {
195 0 : m_xSAXEventKeeper->removeElementCollector(m_nIdOfKeyEC);
196 0 : }
197 0 : }
198 :
199 : /* XReferenceCollector */
200 0 : void SAL_CALL SignatureEngine::setReferenceCount( sal_Int32 count )
201 : throw (cssu::Exception, cssu::RuntimeException, std::exception)
202 : {
203 0 : m_nTotalReferenceNumber = count;
204 0 : tryToPerform();
205 0 : }
206 :
207 0 : void SAL_CALL SignatureEngine::setReferenceId( sal_Int32 id )
208 : throw (cssu::Exception, cssu::RuntimeException, std::exception)
209 : {
210 0 : m_vReferenceIds.push_back( id );
211 0 : }
212 :
213 : /* XUriBinding */
214 0 : 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)
218 : {
219 0 : m_vUris.push_back(uri);
220 0 : m_vXInputStreams.push_back(aInputStream);
221 0 : }
222 :
223 0 : cssu::Reference< com::sun::star::io::XInputStream > SAL_CALL SignatureEngine::getUriBinding( const OUString& uri )
224 : throw (cssu::Exception, cssu::RuntimeException, std::exception)
225 : {
226 0 : cssu::Reference< com::sun::star::io::XInputStream > xInputStream;
227 :
228 0 : int size = m_vUris.size();
229 :
230 0 : for( int i=0; i<size; ++i)
231 : {
232 0 : if (m_vUris[i] == uri)
233 : {
234 0 : xInputStream = m_vXInputStreams[i];
235 0 : break;
236 : }
237 : }
238 :
239 0 : return xInputStream;
240 : }
241 :
242 :
243 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|