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