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 "xsecparser.hxx"
22 : #include <tools/debug.hxx>
23 : #include "cppuhelper/exc_hlp.hxx"
24 :
25 : #include <string.h>
26 :
27 : namespace cssu = com::sun::star::uno;
28 : namespace cssxs = com::sun::star::xml::sax;
29 :
30 0 : XSecParser::XSecParser(
31 : XSecController* pXSecController,
32 : const cssu::Reference< cssxs::XDocumentHandler >& xNextHandler )
33 : : m_pXSecController(pXSecController),
34 : m_xNextHandler(xNextHandler),
35 0 : m_bReferenceUnresolved(false)
36 : {
37 0 : }
38 :
39 0 : OUString XSecParser::getIdAttr(const cssu::Reference< cssxs::XAttributeList >& xAttribs )
40 : {
41 0 : OUString ouIdAttr = xAttribs->getValueByName("id");
42 :
43 0 : if (ouIdAttr == NULL)
44 : {
45 0 : ouIdAttr = xAttribs->getValueByName("Id");
46 : }
47 :
48 0 : return ouIdAttr;
49 : }
50 :
51 : /*
52 : * XDocumentHandler
53 : */
54 0 : void SAL_CALL XSecParser::startDocument( )
55 : throw (cssxs::SAXException, cssu::RuntimeException)
56 : {
57 0 : m_bInX509IssuerName = false;
58 0 : m_bInX509SerialNumber = false;
59 0 : m_bInX509Certificate = false;
60 0 : m_bInSignatureValue = false;
61 0 : m_bInDigestValue = false;
62 0 : m_bInDate = false;
63 :
64 0 : if (m_xNextHandler.is())
65 : {
66 0 : m_xNextHandler->startDocument();
67 : }
68 0 : }
69 :
70 0 : void SAL_CALL XSecParser::endDocument( )
71 : throw (cssxs::SAXException, cssu::RuntimeException)
72 : {
73 0 : if (m_xNextHandler.is())
74 : {
75 0 : m_xNextHandler->endDocument();
76 : }
77 0 : }
78 :
79 0 : void SAL_CALL XSecParser::startElement(
80 : const OUString& aName,
81 : const cssu::Reference< cssxs::XAttributeList >& xAttribs )
82 : throw (cssxs::SAXException, cssu::RuntimeException)
83 : {
84 : try
85 : {
86 0 : OUString ouIdAttr = getIdAttr(xAttribs);
87 0 : if (ouIdAttr != NULL)
88 : {
89 0 : m_pXSecController->collectToVerify( ouIdAttr );
90 : }
91 :
92 0 : if ( aName == TAG_SIGNATURE )
93 : {
94 0 : m_pXSecController->addSignature();
95 0 : if (ouIdAttr != NULL)
96 : {
97 0 : m_pXSecController->setId( ouIdAttr );
98 : }
99 : }
100 0 : else if ( aName == TAG_REFERENCE )
101 : {
102 0 : OUString ouUri = xAttribs->getValueByName(ATTR_URI);
103 : DBG_ASSERT( ouUri != NULL, "URI == NULL" );
104 :
105 0 : if (ouUri.startsWith(CHAR_FRAGMENT))
106 : {
107 : /*
108 : * remove the first character '#' from the attribute value
109 : */
110 0 : m_pXSecController->addReference( ouUri.copy(1) );
111 : }
112 : else
113 : {
114 : /*
115 : * remember the uri
116 : */
117 0 : m_currentReferenceURI = ouUri;
118 0 : m_bReferenceUnresolved = true;
119 0 : }
120 : }
121 0 : else if (aName == TAG_TRANSFORM)
122 : {
123 0 : if ( m_bReferenceUnresolved )
124 : {
125 0 : OUString ouAlgorithm = xAttribs->getValueByName(ATTR_ALGORITHM);
126 :
127 0 : if (ouAlgorithm != NULL && ouAlgorithm == ALGO_C14N)
128 : /*
129 : * a xml stream
130 : */
131 : {
132 0 : m_pXSecController->addStreamReference( m_currentReferenceURI, sal_False);
133 0 : m_bReferenceUnresolved = false;
134 0 : }
135 : }
136 : }
137 0 : else if (aName == TAG_X509ISSUERNAME)
138 : {
139 0 : m_ouX509IssuerName = OUString();
140 0 : m_bInX509IssuerName = true;
141 : }
142 0 : else if (aName == TAG_X509SERIALNUMBER)
143 : {
144 0 : m_ouX509SerialNumber = OUString();
145 0 : m_bInX509SerialNumber = true;
146 : }
147 0 : else if (aName == TAG_X509CERTIFICATE)
148 : {
149 0 : m_ouX509Certificate = OUString();
150 0 : m_bInX509Certificate = true;
151 : }
152 0 : else if (aName == TAG_SIGNATUREVALUE)
153 : {
154 0 : m_ouSignatureValue = OUString();
155 0 : m_bInSignatureValue = true;
156 : }
157 0 : else if (aName == TAG_DIGESTVALUE)
158 : {
159 0 : m_ouDigestValue = "";
160 0 : m_bInDigestValue = true;
161 : }
162 0 : else if ( aName == TAG_SIGNATUREPROPERTY )
163 : {
164 0 : if (ouIdAttr != NULL)
165 : {
166 0 : m_pXSecController->setPropertyId( ouIdAttr );
167 : }
168 : }
169 0 : else if (aName == NSTAG_DC ":" TAG_DATE)
170 : {
171 0 : m_ouDate = OUString();
172 0 : m_bInDate = true;
173 : }
174 :
175 0 : if (m_xNextHandler.is())
176 : {
177 0 : m_xNextHandler->startElement(aName, xAttribs);
178 0 : }
179 : }
180 0 : catch (cssu::Exception& )
181 : {//getCaughtException MUST be the first line in the catch block
182 0 : cssu::Any exc = cppu::getCaughtException();
183 : throw cssxs::SAXException(
184 : "xmlsecurity: Exception in XSecParser::startElement",
185 0 : 0, exc);
186 : }
187 0 : catch (...)
188 : {
189 : throw cssxs::SAXException(
190 : "xmlsecurity: unexpected exception in XSecParser::startElement", 0,
191 0 : cssu::Any());
192 : }
193 0 : }
194 :
195 0 : void SAL_CALL XSecParser::endElement( const OUString& aName )
196 : throw (cssxs::SAXException, cssu::RuntimeException)
197 : {
198 : try
199 : {
200 0 : if (aName == TAG_DIGESTVALUE)
201 : {
202 0 : m_bInDigestValue = false;
203 : }
204 0 : else if ( aName == TAG_REFERENCE )
205 : {
206 0 : if ( m_bReferenceUnresolved )
207 : /*
208 : * it must be a octet stream
209 : */
210 : {
211 0 : m_pXSecController->addStreamReference( m_currentReferenceURI, sal_True);
212 0 : m_bReferenceUnresolved = false;
213 : }
214 :
215 0 : m_pXSecController->setDigestValue( m_ouDigestValue );
216 : }
217 0 : else if ( aName == TAG_SIGNEDINFO )
218 : {
219 0 : m_pXSecController->setReferenceCount();
220 : }
221 0 : else if ( aName == TAG_SIGNATUREVALUE )
222 : {
223 0 : m_pXSecController->setSignatureValue( m_ouSignatureValue );
224 0 : m_bInSignatureValue = false;
225 : }
226 0 : else if (aName == TAG_X509ISSUERNAME)
227 : {
228 0 : m_pXSecController->setX509IssuerName( m_ouX509IssuerName );
229 0 : m_bInX509IssuerName = false;
230 : }
231 0 : else if (aName == TAG_X509SERIALNUMBER)
232 : {
233 0 : m_pXSecController->setX509SerialNumber( m_ouX509SerialNumber );
234 0 : m_bInX509SerialNumber = false;
235 : }
236 0 : else if (aName == TAG_X509CERTIFICATE)
237 : {
238 0 : m_pXSecController->setX509Certificate( m_ouX509Certificate );
239 0 : m_bInX509Certificate = false;
240 : }
241 0 : else if (aName == NSTAG_DC ":" TAG_DATE)
242 : {
243 0 : m_pXSecController->setDate( m_ouDate );
244 0 : m_bInDate = false;
245 : }
246 :
247 0 : if (m_xNextHandler.is())
248 : {
249 0 : m_xNextHandler->endElement(aName);
250 : }
251 : }
252 0 : catch (cssu::Exception& )
253 : {//getCaughtException MUST be the first line in the catch block
254 0 : cssu::Any exc = cppu::getCaughtException();
255 : throw cssxs::SAXException(
256 : "xmlsecurity: Exception in XSecParser::endElement",
257 0 : 0, exc);
258 : }
259 0 : catch (...)
260 : {
261 : throw cssxs::SAXException(
262 : "xmlsecurity: unexpected exception in XSecParser::endElement", 0,
263 0 : cssu::Any());
264 : }
265 0 : }
266 :
267 0 : void SAL_CALL XSecParser::characters( const OUString& aChars )
268 : throw (cssxs::SAXException, cssu::RuntimeException)
269 : {
270 0 : if (m_bInX509IssuerName)
271 : {
272 0 : m_ouX509IssuerName += aChars;
273 : }
274 0 : else if (m_bInX509SerialNumber)
275 : {
276 0 : m_ouX509SerialNumber += aChars;
277 : }
278 0 : else if (m_bInX509Certificate)
279 : {
280 0 : m_ouX509Certificate += aChars;
281 : }
282 0 : else if (m_bInSignatureValue)
283 : {
284 0 : m_ouSignatureValue += aChars;
285 : }
286 0 : else if (m_bInDigestValue)
287 : {
288 0 : m_ouDigestValue += aChars;
289 : }
290 0 : else if (m_bInDate)
291 : {
292 0 : m_ouDate += aChars;
293 : }
294 :
295 0 : if (m_xNextHandler.is())
296 : {
297 0 : m_xNextHandler->characters(aChars);
298 : }
299 0 : }
300 :
301 0 : void SAL_CALL XSecParser::ignorableWhitespace( const OUString& aWhitespaces )
302 : throw (cssxs::SAXException, cssu::RuntimeException)
303 : {
304 0 : if (m_xNextHandler.is())
305 : {
306 0 : m_xNextHandler->ignorableWhitespace( aWhitespaces );
307 : }
308 0 : }
309 :
310 0 : void SAL_CALL XSecParser::processingInstruction( const OUString& aTarget, const OUString& aData )
311 : throw (cssxs::SAXException, cssu::RuntimeException)
312 : {
313 0 : if (m_xNextHandler.is())
314 : {
315 0 : m_xNextHandler->processingInstruction(aTarget, aData);
316 : }
317 0 : }
318 :
319 0 : void SAL_CALL XSecParser::setDocumentLocator( const cssu::Reference< cssxs::XLocator >& xLocator )
320 : throw (cssxs::SAXException, cssu::RuntimeException)
321 : {
322 0 : if (m_xNextHandler.is())
323 : {
324 0 : m_xNextHandler->setDocumentLocator( xLocator );
325 : }
326 0 : }
327 :
328 : /*
329 : * XInitialization
330 : */
331 0 : void SAL_CALL XSecParser::initialize(
332 : const cssu::Sequence< cssu::Any >& aArguments )
333 : throw(cssu::Exception, cssu::RuntimeException)
334 : {
335 0 : aArguments[0] >>= m_xNextHandler;
336 0 : }
337 :
338 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|