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