Bump version to 5.0-14
[LibreOffice.git] / xmlsecurity / source / helper / xsecparser.cxx
blob750713fc1467bb130821ad9e9c27f2b3912ed9f9
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 "xsecparser.hxx"
22 #include <tools/debug.hxx>
23 #include <cppuhelper/exc_hlp.hxx>
25 #include <string.h>
27 namespace cssu = com::sun::star::uno;
28 namespace cssxs = com::sun::star::xml::sax;
30 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 , m_bReferenceUnresolved(false)
44 OUString XSecParser::getIdAttr(const cssu::Reference< cssxs::XAttributeList >& xAttribs )
46 OUString ouIdAttr = xAttribs->getValueByName("id");
48 if (ouIdAttr == NULL)
50 ouIdAttr = xAttribs->getValueByName("Id");
53 return ouIdAttr;
57 * XDocumentHandler
59 void SAL_CALL XSecParser::startDocument( )
60 throw (cssxs::SAXException, cssu::RuntimeException, std::exception)
62 m_bInX509IssuerName = false;
63 m_bInX509SerialNumber = false;
64 m_bInX509Certificate = false;
65 m_bInSignatureValue = false;
66 m_bInDigestValue = false;
67 m_bInDate = false;
69 if (m_xNextHandler.is())
71 m_xNextHandler->startDocument();
75 void SAL_CALL XSecParser::endDocument( )
76 throw (cssxs::SAXException, cssu::RuntimeException, std::exception)
78 if (m_xNextHandler.is())
80 m_xNextHandler->endDocument();
84 void SAL_CALL XSecParser::startElement(
85 const OUString& aName,
86 const cssu::Reference< cssxs::XAttributeList >& xAttribs )
87 throw (cssxs::SAXException, cssu::RuntimeException, std::exception)
89 try
91 OUString ouIdAttr = getIdAttr(xAttribs);
92 if (ouIdAttr != NULL)
94 m_pXSecController->collectToVerify( ouIdAttr );
97 if ( aName == TAG_SIGNATURE )
99 m_pXSecController->addSignature();
100 if (ouIdAttr != NULL)
102 m_pXSecController->setId( ouIdAttr );
105 else if ( aName == TAG_REFERENCE )
107 OUString ouUri = xAttribs->getValueByName(ATTR_URI);
108 DBG_ASSERT( ouUri != NULL, "URI == NULL" );
110 if (ouUri.startsWith(CHAR_FRAGMENT))
113 * remove the first character '#' from the attribute value
115 m_pXSecController->addReference( ouUri.copy(1) );
117 else
120 * remember the uri
122 m_currentReferenceURI = ouUri;
123 m_bReferenceUnresolved = true;
126 else if (aName == TAG_TRANSFORM)
128 if ( m_bReferenceUnresolved )
130 OUString ouAlgorithm = xAttribs->getValueByName(ATTR_ALGORITHM);
132 if (ouAlgorithm != NULL && ouAlgorithm == ALGO_C14N)
134 * a xml stream
137 m_pXSecController->addStreamReference( m_currentReferenceURI, false);
138 m_bReferenceUnresolved = false;
142 else if (aName == TAG_X509ISSUERNAME)
144 m_ouX509IssuerName.clear();
145 m_bInX509IssuerName = true;
147 else if (aName == TAG_X509SERIALNUMBER)
149 m_ouX509SerialNumber.clear();
150 m_bInX509SerialNumber = true;
152 else if (aName == TAG_X509CERTIFICATE)
154 m_ouX509Certificate.clear();
155 m_bInX509Certificate = true;
157 else if (aName == TAG_SIGNATUREVALUE)
159 m_ouSignatureValue.clear();
160 m_bInSignatureValue = true;
162 else if (aName == TAG_DIGESTVALUE)
164 m_ouDigestValue.clear();
165 m_bInDigestValue = true;
167 else if ( aName == TAG_SIGNATUREPROPERTY )
169 if (ouIdAttr != NULL)
171 m_pXSecController->setPropertyId( ouIdAttr );
174 else if (aName == NSTAG_DC ":" TAG_DATE)
176 m_ouDate.clear();
177 m_bInDate = true;
180 if (m_xNextHandler.is())
182 m_xNextHandler->startElement(aName, xAttribs);
185 catch (cssu::Exception& )
186 {//getCaughtException MUST be the first line in the catch block
187 cssu::Any exc = cppu::getCaughtException();
188 throw cssxs::SAXException(
189 "xmlsecurity: Exception in XSecParser::startElement",
190 0, exc);
192 catch (...)
194 throw cssxs::SAXException(
195 "xmlsecurity: unexpected exception in XSecParser::startElement", 0,
196 cssu::Any());
200 void SAL_CALL XSecParser::endElement( const OUString& aName )
201 throw (cssxs::SAXException, cssu::RuntimeException, std::exception)
205 if (aName == TAG_DIGESTVALUE)
207 m_bInDigestValue = false;
209 else if ( aName == TAG_REFERENCE )
211 if ( m_bReferenceUnresolved )
213 * it must be a octet stream
216 m_pXSecController->addStreamReference( m_currentReferenceURI, true);
217 m_bReferenceUnresolved = false;
220 m_pXSecController->setDigestValue( m_ouDigestValue );
222 else if ( aName == TAG_SIGNEDINFO )
224 m_pXSecController->setReferenceCount();
226 else if ( aName == TAG_SIGNATUREVALUE )
228 m_pXSecController->setSignatureValue( m_ouSignatureValue );
229 m_bInSignatureValue = false;
231 else if (aName == TAG_X509ISSUERNAME)
233 m_pXSecController->setX509IssuerName( m_ouX509IssuerName );
234 m_bInX509IssuerName = false;
236 else if (aName == TAG_X509SERIALNUMBER)
238 m_pXSecController->setX509SerialNumber( m_ouX509SerialNumber );
239 m_bInX509SerialNumber = false;
241 else if (aName == TAG_X509CERTIFICATE)
243 m_pXSecController->setX509Certificate( m_ouX509Certificate );
244 m_bInX509Certificate = false;
246 else if (aName == NSTAG_DC ":" TAG_DATE)
248 m_pXSecController->setDate( m_ouDate );
249 m_bInDate = false;
252 if (m_xNextHandler.is())
254 m_xNextHandler->endElement(aName);
257 catch (cssu::Exception& )
258 {//getCaughtException MUST be the first line in the catch block
259 cssu::Any exc = cppu::getCaughtException();
260 throw cssxs::SAXException(
261 "xmlsecurity: Exception in XSecParser::endElement",
262 0, exc);
264 catch (...)
266 throw cssxs::SAXException(
267 "xmlsecurity: unexpected exception in XSecParser::endElement", 0,
268 cssu::Any());
272 void SAL_CALL XSecParser::characters( const OUString& aChars )
273 throw (cssxs::SAXException, cssu::RuntimeException, std::exception)
275 if (m_bInX509IssuerName)
277 m_ouX509IssuerName += aChars;
279 else if (m_bInX509SerialNumber)
281 m_ouX509SerialNumber += aChars;
283 else if (m_bInX509Certificate)
285 m_ouX509Certificate += aChars;
287 else if (m_bInSignatureValue)
289 m_ouSignatureValue += aChars;
291 else if (m_bInDigestValue)
293 m_ouDigestValue += aChars;
295 else if (m_bInDate)
297 m_ouDate += aChars;
300 if (m_xNextHandler.is())
302 m_xNextHandler->characters(aChars);
306 void SAL_CALL XSecParser::ignorableWhitespace( const OUString& aWhitespaces )
307 throw (cssxs::SAXException, cssu::RuntimeException, std::exception)
309 if (m_xNextHandler.is())
311 m_xNextHandler->ignorableWhitespace( aWhitespaces );
315 void SAL_CALL XSecParser::processingInstruction( const OUString& aTarget, const OUString& aData )
316 throw (cssxs::SAXException, cssu::RuntimeException, std::exception)
318 if (m_xNextHandler.is())
320 m_xNextHandler->processingInstruction(aTarget, aData);
324 void SAL_CALL XSecParser::setDocumentLocator( const cssu::Reference< cssxs::XLocator >& xLocator )
325 throw (cssxs::SAXException, cssu::RuntimeException, std::exception)
327 if (m_xNextHandler.is())
329 m_xNextHandler->setDocumentLocator( xLocator );
334 * XInitialization
336 void SAL_CALL XSecParser::initialize(
337 const cssu::Sequence< cssu::Any >& aArguments )
338 throw(cssu::Exception, cssu::RuntimeException, std::exception)
340 aArguments[0] >>= m_xNextHandler;
343 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */