1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
11 #include "ooxmlsecparser.hxx"
12 #include <xmlsignaturehelper.hxx>
14 using namespace com::sun::star
;
16 OOXMLSecParser::OOXMLSecParser(XMLSignatureHelper
& rXMLSignatureHelper
, XSecController
* pXSecController
)
17 : m_pXSecController(pXSecController
)
18 ,m_bInDigestValue(false)
19 ,m_bInSignatureValue(false)
20 ,m_bInX509Certificate(false)
21 ,m_bInMdssiValue(false)
22 ,m_bInSignatureComments(false)
23 ,m_bInX509IssuerName(false)
24 ,m_bInX509SerialNumber(false)
25 ,m_bInCertDigest(false)
26 ,m_bInValidSignatureImage(false)
27 ,m_bInInvalidSignatureImage(false)
28 ,m_bInSignatureLineId(false)
29 ,m_bReferenceUnresolved(false)
30 ,m_rXMLSignatureHelper(rXMLSignatureHelper
)
34 OOXMLSecParser::~OOXMLSecParser()
38 void SAL_CALL
OOXMLSecParser::startDocument()
40 if (m_xNextHandler
.is())
41 m_xNextHandler
->startDocument();
44 void SAL_CALL
OOXMLSecParser::endDocument()
46 if (m_xNextHandler
.is())
47 m_xNextHandler
->endDocument();
50 void SAL_CALL
OOXMLSecParser::startElement(const OUString
& rName
, const uno::Reference
<xml::sax::XAttributeList
>& xAttribs
)
52 OUString aId
= xAttribs
->getValueByName("Id");
54 m_pXSecController
->collectToVerify(aId
);
56 if (rName
== "Signature")
58 m_rXMLSignatureHelper
.StartVerifySignatureElement();
59 m_pXSecController
->addSignature();
61 m_pXSecController
->setId(aId
);
63 else if (rName
== "Reference")
65 OUString aURI
= xAttribs
->getValueByName("URI");
66 if (aURI
.startsWith("#"))
67 m_pXSecController
->addReference(aURI
.copy(1), xml::crypto::DigestID::SHA1
);
70 m_aReferenceURI
= aURI
;
71 m_bReferenceUnresolved
= true;
74 else if (rName
== "Transform")
76 if (m_bReferenceUnresolved
)
78 OUString aAlgorithm
= xAttribs
->getValueByName("Algorithm");
79 if (aAlgorithm
== ALGO_RELATIONSHIP
)
81 m_pXSecController
->addStreamReference(m_aReferenceURI
, /*isBinary=*/false, /*nDigestID=*/xml::crypto::DigestID::SHA256
);
82 m_bReferenceUnresolved
= false;
86 else if (rName
== "DigestValue" && !m_bInCertDigest
)
88 m_aDigestValue
.clear();
89 m_bInDigestValue
= true;
91 else if (rName
== "SignatureValue")
93 m_aSignatureValue
.clear();
94 m_bInSignatureValue
= true;
96 else if (rName
== "X509Certificate")
98 m_aX509Certificate
.clear();
99 m_bInX509Certificate
= true;
101 else if (rName
== "mdssi:Value")
103 m_aMdssiValue
.clear();
104 m_bInMdssiValue
= true;
106 else if (rName
== "SignatureComments")
108 m_aSignatureComments
.clear();
109 m_bInSignatureComments
= true;
111 else if (rName
== "X509IssuerName")
113 m_aX509IssuerName
.clear();
114 m_bInX509IssuerName
= true;
116 else if (rName
== "X509SerialNumber")
118 m_aX509SerialNumber
.clear();
119 m_bInX509SerialNumber
= true;
121 else if (rName
== "xd:CertDigest")
123 m_aCertDigest
.clear();
124 m_bInCertDigest
= true;
126 else if (rName
== "Object")
128 OUString sId
= xAttribs
->getValueByName("Id");
129 if (sId
== "idValidSigLnImg")
131 m_aValidSignatureImage
.clear();
132 m_bInValidSignatureImage
= true;
134 else if (sId
== "idInvalidSigLnImg")
136 m_aInvalidSignatureImage
.clear();
137 m_bInInvalidSignatureImage
= true;
141 SAL_INFO("xmlsecurity.ooxml", "Unknown 'Object' child element: " << rName
);
144 else if (rName
== "SetupID")
146 m_aSignatureLineId
.clear();
147 m_bInSignatureLineId
= true;
151 SAL_INFO("xmlsecurity.ooxml", "Unknown xml element: " << rName
);
154 if (m_xNextHandler
.is())
155 m_xNextHandler
->startElement(rName
, xAttribs
);
158 void SAL_CALL
OOXMLSecParser::endElement(const OUString
& rName
)
160 if (rName
== "SignedInfo")
161 m_pXSecController
->setReferenceCount();
162 else if (rName
== "Reference")
164 if (m_bReferenceUnresolved
)
166 // No transform algorithm found, assume binary.
167 m_pXSecController
->addStreamReference(m_aReferenceURI
, /*isBinary=*/true, /*nDigestID=*/xml::crypto::DigestID::SHA256
);
168 m_bReferenceUnresolved
= false;
170 m_pXSecController
->setDigestValue(xml::crypto::DigestID::SHA256
, m_aDigestValue
);
172 else if (rName
== "DigestValue" && !m_bInCertDigest
)
173 m_bInDigestValue
= false;
174 else if (rName
== "SignatureValue")
176 m_pXSecController
->setSignatureValue(m_aSignatureValue
);
177 m_bInSignatureValue
= false;
179 else if (rName
== "X509Certificate")
181 m_pXSecController
->setX509Certificate(m_aX509Certificate
);
182 m_bInX509Certificate
= false;
184 else if (rName
== "mdssi:Value")
186 m_pXSecController
->setDate(m_aMdssiValue
);
187 m_bInMdssiValue
= false;
189 else if (rName
== "SignatureComments")
191 m_pXSecController
->setDescription(m_aSignatureComments
);
192 m_bInSignatureComments
= false;
194 else if (rName
== "X509IssuerName")
196 m_pXSecController
->setX509IssuerName(m_aX509IssuerName
);
197 m_bInX509IssuerName
= false;
199 else if (rName
== "X509SerialNumber")
201 m_pXSecController
->setX509SerialNumber(m_aX509SerialNumber
);
202 m_bInX509SerialNumber
= false;
204 else if (rName
== "xd:CertDigest")
206 m_pXSecController
->setCertDigest(m_aCertDigest
);
207 m_bInCertDigest
= false;
209 else if (rName
== "Object")
211 if (m_bInValidSignatureImage
)
213 m_pXSecController
->setValidSignatureImage(m_aValidSignatureImage
);
214 m_bInValidSignatureImage
= false;
216 else if (m_bInInvalidSignatureImage
)
218 m_pXSecController
->setInvalidSignatureImage(m_aInvalidSignatureImage
);
219 m_bInInvalidSignatureImage
= false;
222 else if (rName
== "SetupID")
224 m_pXSecController
->setSignatureLineId(m_aSignatureLineId
);
225 m_bInSignatureLineId
= false;
228 if (m_xNextHandler
.is())
229 m_xNextHandler
->endElement(rName
);
232 void SAL_CALL
OOXMLSecParser::characters(const OUString
& rChars
)
234 if (m_bInDigestValue
&& !m_bInCertDigest
)
235 m_aDigestValue
+= rChars
;
236 else if (m_bInSignatureValue
)
237 m_aSignatureValue
+= rChars
;
238 else if (m_bInX509Certificate
)
239 m_aX509Certificate
+= rChars
;
240 else if (m_bInMdssiValue
)
241 m_aMdssiValue
+= rChars
;
242 else if (m_bInSignatureComments
)
243 m_aSignatureComments
+= rChars
;
244 else if (m_bInX509IssuerName
)
245 m_aX509IssuerName
+= rChars
;
246 else if (m_bInX509SerialNumber
)
247 m_aX509SerialNumber
+= rChars
;
248 else if (m_bInCertDigest
)
249 m_aCertDigest
+= rChars
;
250 else if (m_bInValidSignatureImage
)
251 m_aValidSignatureImage
+= rChars
;
252 else if (m_bInInvalidSignatureImage
)
253 m_aInvalidSignatureImage
+= rChars
;
254 else if (m_bInSignatureLineId
)
255 m_aSignatureLineId
+= rChars
;
257 if (m_xNextHandler
.is())
258 m_xNextHandler
->characters(rChars
);
261 void SAL_CALL
OOXMLSecParser::ignorableWhitespace(const OUString
& rWhitespace
)
263 if (m_xNextHandler
.is())
264 m_xNextHandler
->ignorableWhitespace(rWhitespace
);
267 void SAL_CALL
OOXMLSecParser::processingInstruction(const OUString
& rTarget
, const OUString
& rData
)
269 if (m_xNextHandler
.is())
270 m_xNextHandler
->processingInstruction(rTarget
, rData
);
273 void SAL_CALL
OOXMLSecParser::setDocumentLocator(const uno::Reference
<xml::sax::XLocator
>& xLocator
)
275 if (m_xNextHandler
.is())
276 m_xNextHandler
->setDocumentLocator(xLocator
);
279 void SAL_CALL
OOXMLSecParser::initialize(const uno::Sequence
<uno::Any
>& rArguments
)
281 rArguments
[0] >>= m_xNextHandler
;
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */