1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xsecparser.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
34 #include "xsecparser.hxx"
35 #include <tools/debug.hxx>
36 #include "cppuhelper/exc_hlp.hxx"
40 namespace cssu
= com::sun::star::uno
;
41 namespace cssxs
= com::sun::star::xml::sax
;
43 #define RTL_ASCII_USTRINGPARAM( asciiStr ) asciiStr, strlen( asciiStr ), RTL_TEXTENCODING_ASCII_US
45 XSecParser::XSecParser(
46 XSecController
* pXSecController
,
47 const cssu::Reference
< cssxs::XDocumentHandler
>& xNextHandler
)
48 : m_pXSecController(pXSecController
),
49 m_xNextHandler(xNextHandler
),
50 m_bReferenceUnresolved(false)
54 rtl::OUString
XSecParser::getIdAttr(const cssu::Reference
< cssxs::XAttributeList
>& xAttribs
)
56 rtl::OUString ouIdAttr
= xAttribs
->getValueByName(
57 rtl::OUString(RTL_ASCII_USTRINGPARAM("id")));
61 ouIdAttr
= xAttribs
->getValueByName(
62 rtl::OUString(RTL_ASCII_USTRINGPARAM("Id")));
71 void SAL_CALL
XSecParser::startDocument( )
72 throw (cssxs::SAXException
, cssu::RuntimeException
)
74 m_bInX509IssuerName
= false;
75 m_bInX509SerialNumber
= false;
76 m_bInX509Certificate
= false;
77 m_bInSignatureValue
= false;
78 m_bInDigestValue
= false;
82 if (m_xNextHandler
.is())
84 m_xNextHandler
->startDocument();
88 void SAL_CALL
XSecParser::endDocument( )
89 throw (cssxs::SAXException
, cssu::RuntimeException
)
91 if (m_xNextHandler
.is())
93 m_xNextHandler
->endDocument();
97 void SAL_CALL
XSecParser::startElement(
98 const rtl::OUString
& aName
,
99 const cssu::Reference
< cssxs::XAttributeList
>& xAttribs
)
100 throw (cssxs::SAXException
, cssu::RuntimeException
)
104 rtl::OUString ouIdAttr
= getIdAttr(xAttribs
);
105 if (ouIdAttr
!= NULL
)
107 m_pXSecController
->collectToVerify( ouIdAttr
);
110 if ( aName
== rtl::OUString(RTL_ASCII_USTRINGPARAM(TAG_SIGNATURE
)) )
112 m_pXSecController
->addSignature();
113 if (ouIdAttr
!= NULL
)
115 m_pXSecController
->setId( ouIdAttr
);
118 else if ( aName
== rtl::OUString(RTL_ASCII_USTRINGPARAM(TAG_REFERENCE
)) )
120 rtl::OUString ouUri
= xAttribs
->getValueByName(rtl::OUString(RTL_ASCII_USTRINGPARAM(ATTR_URI
)));
121 DBG_ASSERT( ouUri
!= NULL
, "URI == NULL" );
123 if (0 == ouUri
.compareTo(rtl::OUString(RTL_ASCII_USTRINGPARAM(CHAR_FRAGMENT
)),1))
126 * remove the first character '#' from the attribute value
128 m_pXSecController
->addReference( ouUri
.copy(1) );
135 m_currentReferenceURI
= ouUri
;
136 m_bReferenceUnresolved
= true;
139 else if (aName
== rtl::OUString(RTL_ASCII_USTRINGPARAM(TAG_TRANSFORM
)))
141 if ( m_bReferenceUnresolved
)
143 rtl::OUString ouAlgorithm
= xAttribs
->getValueByName(rtl::OUString(RTL_ASCII_USTRINGPARAM(ATTR_ALGORITHM
)));
145 if (ouAlgorithm
!= NULL
&& ouAlgorithm
== rtl::OUString(RTL_ASCII_USTRINGPARAM(ALGO_C14N
)))
150 m_pXSecController
->addStreamReference( m_currentReferenceURI
, sal_False
);
151 m_bReferenceUnresolved
= false;
155 else if (aName
== rtl::OUString(RTL_ASCII_USTRINGPARAM(TAG_X509ISSUERNAME
)))
157 m_ouX509IssuerName
= rtl::OUString::createFromAscii("");
158 m_bInX509IssuerName
= true;
160 else if (aName
== rtl::OUString(RTL_ASCII_USTRINGPARAM(TAG_X509SERIALNUMBER
)))
162 m_ouX509SerialNumber
= rtl::OUString::createFromAscii("");
163 m_bInX509SerialNumber
= true;
165 else if (aName
== rtl::OUString(RTL_ASCII_USTRINGPARAM(TAG_X509CERTIFICATE
)))
167 m_ouX509Certificate
= rtl::OUString::createFromAscii("");
168 m_bInX509Certificate
= true;
170 else if (aName
== rtl::OUString(RTL_ASCII_USTRINGPARAM(TAG_SIGNATUREVALUE
)))
172 m_ouSignatureValue
= rtl::OUString::createFromAscii("");
173 m_bInSignatureValue
= true;
175 else if (aName
== rtl::OUString(RTL_ASCII_USTRINGPARAM(TAG_DIGESTVALUE
)))
177 m_ouDigestValue
= rtl::OUString::createFromAscii("");
178 m_bInDigestValue
= true;
180 else if ( aName
== rtl::OUString(RTL_ASCII_USTRINGPARAM(TAG_SIGNATUREPROPERTY
)) )
182 if (ouIdAttr
!= NULL
)
184 m_pXSecController
->setPropertyId( ouIdAttr
);
187 else if (aName
== rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(NSTAG_DC
))
188 +rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(":"))
189 +rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(TAG_DATE
)))
191 m_ouDate
= rtl::OUString::createFromAscii("");
195 else if (aName == rtl::OUString(RTL_ASCII_USTRINGPARAM(TAG_TIME)))
197 m_ouTime = rtl::OUString::createFromAscii("");
202 if (m_xNextHandler
.is())
204 m_xNextHandler
->startElement(aName
, xAttribs
);
207 catch (cssu::Exception
& )
208 {//getCaughtException MUST be the first line in the catch block
209 cssu::Any exc
= cppu::getCaughtException();
210 throw cssxs::SAXException(
211 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
212 "xmlsecurity: Exception in XSecParser::startElement")),
217 throw cssxs::SAXException(
218 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("xmlsecurity: unexpected exception in XSecParser::startElement")), 0,
223 void SAL_CALL
XSecParser::endElement( const rtl::OUString
& aName
)
224 throw (cssxs::SAXException
, cssu::RuntimeException
)
228 if (aName
== rtl::OUString(RTL_ASCII_USTRINGPARAM(TAG_DIGESTVALUE
)))
230 m_bInDigestValue
= false;
232 else if ( aName
== rtl::OUString(RTL_ASCII_USTRINGPARAM(TAG_REFERENCE
)) )
234 if ( m_bReferenceUnresolved
)
236 * it must be a octet stream
239 m_pXSecController
->addStreamReference( m_currentReferenceURI
, sal_True
);
240 m_bReferenceUnresolved
= false;
243 m_pXSecController
->setDigestValue( m_ouDigestValue
);
245 else if ( aName
== rtl::OUString(RTL_ASCII_USTRINGPARAM(TAG_SIGNEDINFO
)) )
247 m_pXSecController
->setReferenceCount();
249 else if ( aName
== rtl::OUString(RTL_ASCII_USTRINGPARAM(TAG_SIGNATUREVALUE
)) )
251 m_pXSecController
->setSignatureValue( m_ouSignatureValue
);
252 m_bInSignatureValue
= false;
254 else if (aName
== rtl::OUString(RTL_ASCII_USTRINGPARAM(TAG_X509ISSUERNAME
)))
256 m_pXSecController
->setX509IssuerName( m_ouX509IssuerName
);
257 m_bInX509IssuerName
= false;
259 else if (aName
== rtl::OUString(RTL_ASCII_USTRINGPARAM(TAG_X509SERIALNUMBER
)))
261 m_pXSecController
->setX509SerialNumber( m_ouX509SerialNumber
);
262 m_bInX509SerialNumber
= false;
264 else if (aName
== rtl::OUString(RTL_ASCII_USTRINGPARAM(TAG_X509CERTIFICATE
)))
266 m_pXSecController
->setX509Certificate( m_ouX509Certificate
);
267 m_bInX509Certificate
= false;
269 else if (aName
== rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(NSTAG_DC
))
270 +rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(":"))
271 +rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(TAG_DATE
)))
273 m_pXSecController
->setDate( m_ouDate
);
277 else if ( aName == rtl::OUString(RTL_ASCII_USTRINGPARAM(TAG_TIME)) )
279 m_pXSecController->setTime( m_ouTime );
284 if (m_xNextHandler
.is())
286 m_xNextHandler
->endElement(aName
);
289 catch (cssu::Exception
& )
290 {//getCaughtException MUST be the first line in the catch block
291 cssu::Any exc
= cppu::getCaughtException();
292 throw cssxs::SAXException(
293 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
294 "xmlsecurity: Exception in XSecParser::endElement")),
299 throw cssxs::SAXException(
300 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("xmlsecurity: unexpected exception in XSecParser::endElement")), 0,
305 void SAL_CALL
XSecParser::characters( const rtl::OUString
& aChars
)
306 throw (cssxs::SAXException
, cssu::RuntimeException
)
308 if (m_bInX509IssuerName
)
310 m_ouX509IssuerName
+= aChars
;
312 else if (m_bInX509SerialNumber
)
314 m_ouX509SerialNumber
+= aChars
;
316 else if (m_bInX509Certificate
)
318 m_ouX509Certificate
+= aChars
;
320 else if (m_bInSignatureValue
)
322 m_ouSignatureValue
+= aChars
;
324 else if (m_bInDigestValue
)
326 m_ouDigestValue
+= aChars
;
339 if (m_xNextHandler
.is())
341 m_xNextHandler
->characters(aChars
);
345 void SAL_CALL
XSecParser::ignorableWhitespace( const rtl::OUString
& aWhitespaces
)
346 throw (cssxs::SAXException
, cssu::RuntimeException
)
348 if (m_xNextHandler
.is())
350 m_xNextHandler
->ignorableWhitespace( aWhitespaces
);
354 void SAL_CALL
XSecParser::processingInstruction( const rtl::OUString
& aTarget
, const rtl::OUString
& aData
)
355 throw (cssxs::SAXException
, cssu::RuntimeException
)
357 if (m_xNextHandler
.is())
359 m_xNextHandler
->processingInstruction(aTarget
, aData
);
363 void SAL_CALL
XSecParser::setDocumentLocator( const cssu::Reference
< cssxs::XLocator
>& xLocator
)
364 throw (cssxs::SAXException
, cssu::RuntimeException
)
366 if (m_xNextHandler
.is())
368 m_xNextHandler
->setDocumentLocator( xLocator
);
375 void SAL_CALL
XSecParser::initialize(
376 const cssu::Sequence
< cssu::Any
>& aArguments
)
377 throw(cssu::Exception
, cssu::RuntimeException
)
379 aArguments
[0] >>= m_xNextHandler
;