8228c6
From 9ef423d4e7c85629772131b3216b98e17d7b8d7e Mon Sep 17 00:00:00 2001
8228c6
From: Michael Stahl <michael.stahl@allotropia.de>
8228c6
Date: Thu, 18 Feb 2021 19:22:31 +0100
8228c6
Subject: [PATCH 4/6] CVE-2021-25634
8228c6
MIME-Version: 1.0
8228c6
Content-Type: text/plain; charset=UTF-8
8228c6
Content-Transfer-Encoding: 8bit
8228c6
8228c6
xmlsecurity: XSecParser confused about multiple timestamps
8228c6
8228c6
LO writes timestamp both to dc:date and xades:SigningTime elements.
8228c6
8228c6
The parser tries to avoid reading multiple dc:date, preferring the first
8228c6
one, but doesn't care about multiple xades:SigningTime, for undocumented
8228c6
reasons.
8228c6
8228c6
Ideally something should check all read values for consistency.
8228c6
8228c6
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/111160
8228c6
Tested-by: Jenkins
8228c6
Reviewed-by: Michael Stahl <michael.stahl@allotropia.de>
8228c6
(cherry picked from commit 4ab8d9c09a5873ca0aea56dafa1ab34758d52ef7)
8228c6
8228c6
xmlsecurity: remove XSecController::setPropertyId()
8228c6
8228c6
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/111252
8228c6
Tested-by: Jenkins
8228c6
Reviewed-by: Michael Stahl <michael.stahl@allotropia.de>
8228c6
(cherry picked from commit d2a345e1163616fe3201ef1d6c758e2e819214e0)
8228c6
8228c6
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/111908
8228c6
Tested-by: Jenkins
8228c6
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
8228c6
(cherry picked from commit abe77c4fcb9ea97d9fff07eaea6d8863bcba5b02)
8228c6
8228c6
Change-Id: Ic018ee89797a1c8a4f870ae102af48006de930ef
8228c6
---
8228c6
 include/svl/sigstruct.hxx                    |  7 +-
8228c6
 xmlsecurity/inc/xsecctl.hxx                  |  5 +-
8228c6
 xmlsecurity/source/helper/ooxmlsecparser.cxx |  4 +-
8228c6
 xmlsecurity/source/helper/xsecctl.cxx        |  2 +-
8228c6
 xmlsecurity/source/helper/xsecparser.cxx     | 81 ++++++++++----------
8228c6
 xmlsecurity/source/helper/xsecparser.hxx     |  6 --
8228c6
 xmlsecurity/source/helper/xsecsign.cxx       |  4 +-
8228c6
 xmlsecurity/source/helper/xsecverify.cxx     | 39 ++++------
8228c6
 8 files changed, 68 insertions(+), 80 deletions(-)
8228c6
8228c6
diff --git a/include/svl/sigstruct.hxx b/include/svl/sigstruct.hxx
8228c6
index f6ee242c84d1..7a0296fa9fae 100644
8228c6
--- a/include/svl/sigstruct.hxx
8228c6
+++ b/include/svl/sigstruct.hxx
8228c6
@@ -103,6 +103,9 @@ struct SignatureInformation
8228c6
     // XAdES EncapsulatedX509Certificate values
8228c6
     std::set<OUString> maEncapsulatedX509Certificates;
8228c6
 
8228c6
+    OUString ouSignatureId;
8228c6
+    // signature may contain multiple time stamps - check they're consistent
8228c6
+    bool hasInconsistentSigningTime = false;
8228c6
     //We also keep the date and time as string. This is done when this
8228c6
     //structure is created as a result of a XML signature being read.
8228c6
     //When then a signature is added or another removed, then the original
8228c6
@@ -115,8 +118,8 @@ struct SignatureInformation
8228c6
     //and the converted time is written back, then the string looks different
8228c6
     //and the signature is broken.
8228c6
     OUString ouDateTime;
8228c6
-    OUString ouSignatureId;
8228c6
-    OUString ouPropertyId;
8228c6
+    /// The Id attribute of the <SignatureProperty> element that contains the <dc:date>.
8228c6
+    OUString ouDateTimePropertyId;
8228c6
     /// Characters of the <dc:description> element inside the signature.
8228c6
     OUString ouDescription;
8228c6
     /// The Id attribute of the <SignatureProperty> element that contains the <dc:description>.
8228c6
diff --git a/xmlsecurity/inc/xsecctl.hxx b/xmlsecurity/inc/xsecctl.hxx
8228c6
index 351c94a2a3e6..7baa219fb13c 100644
8228c6
--- a/xmlsecurity/inc/xsecctl.hxx
8228c6
+++ b/xmlsecurity/inc/xsecctl.hxx
8228c6
@@ -271,8 +271,8 @@ private:
8228c6
     void setGpgCertificate( OUString const & ouGpgCert );
8228c6
     void setGpgOwner( OUString const & ouGpgOwner );
8228c6
 
8228c6
-    void setDate( OUString const & ouDate );
8228c6
-    void setDescription(const OUString& rDescription);
8228c6
+    void setDate(OUString const& rId, OUString const& ouDate);
8228c6
+    void setDescription(OUString const& rId, OUString const& rDescription);
8228c6
     void setCertDigest(const OUString& rCertDigest);
8228c6
     void setValidSignatureImage(const OUString& rValidSigImg);
8228c6
     void setInvalidSignatureImage(const OUString& rInvalidSigImg);
8228c6
@@ -283,7 +283,6 @@ public:
8228c6
 
8228c6
 private:
8228c6
     void setId( OUString const & ouId );
8228c6
-    void setPropertyId( OUString const & ouPropertyId );
8228c6
 
8228c6
     css::uno::Reference< css::xml::crypto::sax::XReferenceResolvedListener > prepareSignatureToRead(
8228c6
         sal_Int32 nSecurityId );
8228c6
diff --git a/xmlsecurity/source/helper/ooxmlsecparser.cxx b/xmlsecurity/source/helper/ooxmlsecparser.cxx
8228c6
index c22e8c2261bf..a200de60c07a 100644
8228c6
--- a/xmlsecurity/source/helper/ooxmlsecparser.cxx
8228c6
+++ b/xmlsecurity/source/helper/ooxmlsecparser.cxx
8228c6
@@ -192,12 +192,12 @@ void SAL_CALL OOXMLSecParser::endElement(const OUString& rName)
8228c6
     }
8228c6
     else if (rName == "mdssi:Value")
8228c6
     {
8228c6
-        m_pXSecController->setDate(m_aMdssiValue);
8228c6
+        m_pXSecController->setDate("", m_aMdssiValue);
8228c6
         m_bInMdssiValue = false;
8228c6
     }
8228c6
     else if (rName == "SignatureComments")
8228c6
     {
8228c6
-        m_pXSecController->setDescription(m_aSignatureComments);
8228c6
+        m_pXSecController->setDescription("", m_aSignatureComments);
8228c6
         m_bInSignatureComments = false;
8228c6
     }
8228c6
     else if (rName == "X509IssuerName")
8228c6
diff --git a/xmlsecurity/source/helper/xsecctl.cxx b/xmlsecurity/source/helper/xsecctl.cxx
8228c6
index ab108d13c224..8d5ea68c768b 100644
8228c6
--- a/xmlsecurity/source/helper/xsecctl.cxx
8228c6
+++ b/xmlsecurity/source/helper/xsecctl.cxx
8228c6
@@ -819,7 +819,7 @@ void XSecController::exportSignature(
8228c6
                 pAttributeList = new SvXMLAttributeList();
8228c6
                 pAttributeList->AddAttribute(
8228c6
                     "Id",
8228c6
-                    signatureInfo.ouPropertyId);
8228c6
+                    signatureInfo.ouDateTimePropertyId);
8228c6
                 pAttributeList->AddAttribute(
8228c6
                     "Target",
8228c6
                     "#" + signatureInfo.ouSignatureId);
8228c6
diff --git a/xmlsecurity/source/helper/xsecparser.cxx b/xmlsecurity/source/helper/xsecparser.cxx
8228c6
index 5c92e5efa104..9cc9312b4d9f 100644
8228c6
--- a/xmlsecurity/source/helper/xsecparser.cxx
8228c6
+++ b/xmlsecurity/source/helper/xsecparser.cxx
8228c6
@@ -978,6 +978,9 @@ class XSecParser::XadesSigningCertificateContext
8228c6
 class XSecParser::XadesSigningTimeContext
8228c6
     : public XSecParser::Context
8228c6
 {
8228c6
+    private:
8228c6
+        OUString m_Value;
8228c6
+
8228c6
     public:
8228c6
         XadesSigningTimeContext(XSecParser & rParser,
8228c6
                 std::unique_ptr<SvXMLNamespaceMap> pOldNamespaceMap)
8228c6
@@ -985,20 +988,14 @@ class XSecParser::XadesSigningTimeContext
8228c6
         {
8228c6
         }
8228c6
 
8228c6
-        virtual void StartElement(
8228c6
-            css::uno::Reference<css::xml::sax::XAttributeList> const& /*xAttrs*/) override
8228c6
-        {
8228c6
-            m_rParser.m_ouDate.clear();
8228c6
-        }
8228c6
-
8228c6
         virtual void EndElement() override
8228c6
         {
8228c6
-            m_rParser.m_pXSecController->setDate( m_rParser.m_ouDate );
8228c6
+            m_rParser.m_pXSecController->setDate("", m_Value);
8228c6
         }
8228c6
 
8228c6
         virtual void Characters(OUString const& rChars) override
8228c6
         {
8228c6
-            m_rParser.m_ouDate += rChars;
8228c6
+            m_Value += rChars;
8228c6
         }
8228c6
 };
8228c6
 
8228c6
@@ -1104,35 +1101,20 @@ class XSecParser::DcDateContext
8228c6
     : public XSecParser::Context
8228c6
 {
8228c6
     private:
8228c6
-        bool m_isIgnore = false;
8228c6
+        OUString & m_rValue;
8228c6
 
8228c6
     public:
8228c6
         DcDateContext(XSecParser & rParser,
8228c6
-                std::unique_ptr<SvXMLNamespaceMap> pOldNamespaceMap)
8228c6
+                std::unique_ptr<SvXMLNamespaceMap> pOldNamespaceMap,
8228c6
+                OUString & rValue)
8228c6
             : XSecParser::Context(rParser, std::move(pOldNamespaceMap))
8228c6
+            , m_rValue(rValue)
8228c6
         {
8228c6
         }
8228c6
 
8228c6
-        virtual void StartElement(
8228c6
-            css::uno::Reference<css::xml::sax::XAttributeList> const& /*xAttrs*/) override
8228c6
-        {
8228c6
-            m_isIgnore = !m_rParser.m_ouDate.isEmpty();
8228c6
-        }
8228c6
-
8228c6
-        virtual void EndElement() override
8228c6
-        {
8228c6
-            if (!m_isIgnore)
8228c6
-            {
8228c6
-                m_rParser.m_pXSecController->setDate( m_rParser.m_ouDate );
8228c6
-            }
8228c6
-        }
8228c6
-
8228c6
         virtual void Characters(OUString const& rChars) override
8228c6
         {
8228c6
-            if (!m_isIgnore)
8228c6
-            {
8228c6
-                m_rParser.m_ouDate += rChars;
8228c6
-            }
8228c6
+            m_rValue += rChars;
8228c6
         }
8228c6
 };
8228c6
 
8228c6
@@ -1140,29 +1122,32 @@ class XSecParser::DcDescriptionContext
8228c6
     : public XSecParser::Context
8228c6
 {
8228c6
     private:
8228c6
-        OUString m_Value;
8228c6
+        OUString & m_rValue;
8228c6
 
8228c6
     public:
8228c6
         DcDescriptionContext(XSecParser & rParser,
8228c6
-                std::unique_ptr<SvXMLNamespaceMap> pOldNamespaceMap)
8228c6
+                std::unique_ptr<SvXMLNamespaceMap> pOldNamespaceMap,
8228c6
+                OUString & rValue)
8228c6
             : XSecParser::Context(rParser, std::move(pOldNamespaceMap))
8228c6
+            , m_rValue(rValue)
8228c6
         {
8228c6
         }
8228c6
 
8228c6
-        virtual void EndElement() override
8228c6
-        {
8228c6
-            m_rParser.m_pXSecController->setDescription(m_Value);
8228c6
-        }
8228c6
-
8228c6
         virtual void Characters(OUString const& rChars) override
8228c6
         {
8228c6
-            m_Value += rChars;
8228c6
+            m_rValue += rChars;
8228c6
         }
8228c6
 };
8228c6
 
8228c6
 class XSecParser::DsSignaturePropertyContext
8228c6
     : public XSecParser::Context
8228c6
 {
8228c6
+    private:
8228c6
+        enum class SignatureProperty { Unknown, Date, Description };
8228c6
+        SignatureProperty m_Property = SignatureProperty::Unknown;
8228c6
+        OUString m_Id;
8228c6
+        OUString m_Value;
8228c6
+
8228c6
     public:
8228c6
         DsSignaturePropertyContext(XSecParser & rParser,
8228c6
                 std::unique_ptr<SvXMLNamespaceMap> pOldNamespaceMap)
8228c6
@@ -1173,10 +1158,22 @@ class XSecParser::DsSignaturePropertyContext
8228c6
         virtual void StartElement(
8228c6
             css::uno::Reference<css::xml::sax::XAttributeList> const& xAttrs) override
8228c6
         {
8228c6
-            OUString const ouIdAttr(m_rParser.HandleIdAttr(xAttrs));
8228c6
-            if (!ouIdAttr.isEmpty())
8228c6
+            m_Id = m_rParser.HandleIdAttr(xAttrs);
8228c6
+        }
8228c6
+
8228c6
+        virtual void EndElement() override
8228c6
+        {
8228c6
+            switch (m_Property)
8228c6
             {
8228c6
-                m_rParser.m_pXSecController->setPropertyId( ouIdAttr );
8228c6
+                case SignatureProperty::Unknown:
8228c6
+                    SAL_INFO("xmlsecurity.helper", "Unknown property in ds:Object ignored");
8228c6
+                    break;
8228c6
+                case SignatureProperty::Date:
8228c6
+                    m_rParser.m_pXSecController->setDate(m_Id, m_Value);
8228c6
+                    break;
8228c6
+                case SignatureProperty::Description:
8228c6
+                    m_rParser.m_pXSecController->setDescription(m_Id, m_Value);
8228c6
+                    break;
8228c6
             }
8228c6
         }
8228c6
 
8228c6
@@ -1186,11 +1183,13 @@ class XSecParser::DsSignaturePropertyContext
8228c6
         {
8228c6
             if (nNamespace == XML_NAMESPACE_DC && rName == "date")
8228c6
             {
8228c6
-                return std::make_unique<DcDateContext>(m_rParser, std::move(pOldNamespaceMap));
8228c6
+                m_Property = SignatureProperty::Date;
8228c6
+                return std::make_unique<DcDateContext>(m_rParser, std::move(pOldNamespaceMap), m_Value);
8228c6
             }
8228c6
             if (nNamespace == XML_NAMESPACE_DC && rName == "description")
8228c6
             {
8228c6
-                return std::make_unique<DcDescriptionContext>(m_rParser, std::move(pOldNamespaceMap));
8228c6
+                m_Property = SignatureProperty::Description;
8228c6
+                return std::make_unique<DcDescriptionContext>(m_rParser, std::move(pOldNamespaceMap), m_Value);
8228c6
             }
8228c6
             return XSecParser::Context::CreateChildContext(std::move(pOldNamespaceMap), nNamespace, rName);
8228c6
         }
8228c6
diff --git a/xmlsecurity/source/helper/xsecparser.hxx b/xmlsecurity/source/helper/xsecparser.hxx
8228c6
index 93efcb766e3e..7a0eb08bca28 100644
8228c6
--- a/xmlsecurity/source/helper/xsecparser.hxx
8228c6
+++ b/xmlsecurity/source/helper/xsecparser.hxx
8228c6
@@ -97,12 +97,6 @@ private:
8228c6
     class DsSignatureContext;
8228c6
     class DsigSignaturesContext;
8228c6
 
8228c6
-    /*
8228c6
-     * the following members are used to reserve the signature information,
8228c6
-     * including X509IssuerName, X509SerialNumber, and X509Certificate,etc.
8228c6
-     */
8228c6
-    OUString m_ouDate;
8228c6
-
8228c6
     std::stack<std::unique_ptr<Context>> m_ContextStack;
8228c6
     std::unique_ptr<SvXMLNamespaceMap> m_pNamespaceMap;
8228c6
 
8228c6
diff --git a/xmlsecurity/source/helper/xsecsign.cxx b/xmlsecurity/source/helper/xsecsign.cxx
8228c6
index 4d1b89949feb..5ed23281f083 100644
8228c6
--- a/xmlsecurity/source/helper/xsecsign.cxx
8228c6
+++ b/xmlsecurity/source/helper/xsecsign.cxx
8228c6
@@ -132,8 +132,8 @@ cssu::Reference< cssxc::sax::XReferenceResolvedListener > XSecController::prepar
8228c6
     if (nStorageFormat != embed::StorageFormats::OFOPXML)
8228c6
     {
8228c6
         internalSignatureInfor.signatureInfor.ouSignatureId = createId();
8228c6
-        internalSignatureInfor.signatureInfor.ouPropertyId = createId();
8228c6
-        internalSignatureInfor.addReference(SignatureReferenceType::SAMEDOCUMENT, digestID, internalSignatureInfor.signatureInfor.ouPropertyId, -1, OUString() );
8228c6
+        internalSignatureInfor.signatureInfor.ouDateTimePropertyId = createId();
8228c6
+        internalSignatureInfor.addReference(SignatureReferenceType::SAMEDOCUMENT, digestID, internalSignatureInfor.signatureInfor.ouDateTimePropertyId, -1, OUString() );
8228c6
         size++;
8228c6
 
8228c6
         if (bXAdESCompliantIfODF)
8228c6
diff --git a/xmlsecurity/source/helper/xsecverify.cxx b/xmlsecurity/source/helper/xsecverify.cxx
8228c6
index 1f7fa9ac8ca8..5f5840334254 100644
8228c6
--- a/xmlsecurity/source/helper/xsecverify.cxx
8228c6
+++ b/xmlsecurity/source/helper/xsecverify.cxx
8228c6
@@ -321,7 +321,7 @@ void XSecController::setGpgOwner( OUString const & ouGpgOwner )
8228c6
     isi.signatureInfor.ouGpgOwner = ouGpgOwner;
8228c6
 }
8228c6
 
8228c6
-void XSecController::setDate( OUString const & ouDate )
8228c6
+void XSecController::setDate(OUString const& rId, OUString const& ouDate)
8228c6
 {
8228c6
     if (m_vInternalSignatureInformations.empty())
8228c6
     {
8228c6
@@ -329,17 +329,31 @@ void XSecController::setDate( OUString const & ouDate )
8228c6
         return;
8228c6
     }
8228c6
     InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
8228c6
+    // there may be multiple timestamps in a signature - check them for consistency
8228c6
+    if (!isi.signatureInfor.ouDateTime.isEmpty()
8228c6
+        && isi.signatureInfor.ouDateTime != ouDate)
8228c6
+    {
8228c6
+        isi.signatureInfor.hasInconsistentSigningTime = true;
8228c6
+    }
8228c6
     (void)utl::ISO8601parseDateTime( ouDate, isi.signatureInfor.stDateTime);
8228c6
     isi.signatureInfor.ouDateTime = ouDate;
8228c6
+    if (!rId.isEmpty())
8228c6
+    {
8228c6
+        isi.signatureInfor.ouDateTimePropertyId = rId;
8228c6
+    }
8228c6
 }
8228c6
 
8228c6
-void XSecController::setDescription(const OUString& rDescription)
8228c6
+void XSecController::setDescription(OUString const& rId, OUString const& rDescription)
8228c6
 {
8228c6
     if (m_vInternalSignatureInformations.empty())
8228c6
         return;
8228c6
 
8228c6
     InternalSignatureInformation& rInformation = m_vInternalSignatureInformations.back();
8228c6
     rInformation.signatureInfor.ouDescription = rDescription;
8228c6
+    if (!rId.isEmpty())
8228c6
+    {
8228c6
+        rInformation.signatureInfor.ouDescriptionPropertyId = rId;
8228c6
+    }
8228c6
 }
8228c6
 
8228c6
 void XSecController::setSignatureBytes(const uno::Sequence<sal_Int8>& rBytes)
8228c6
@@ -433,27 +447,6 @@ void XSecController::setId( OUString const & ouId )
8228c6
     isi.signatureInfor.ouSignatureId = ouId;
8228c6
 }
8228c6
 
8228c6
-void XSecController::setPropertyId( OUString const & ouPropertyId )
8228c6
-{
8228c6
-    if (m_vInternalSignatureInformations.empty())
8228c6
-    {
8228c6
-        SAL_INFO("xmlsecurity.helper","XSecController::setPropertyId: no signature");
8228c6
-        return;
8228c6
-    }
8228c6
-    InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
8228c6
-
8228c6
-    if (isi.signatureInfor.ouPropertyId.isEmpty())
8228c6
-    {
8228c6
-        // <SignatureProperty> ID attribute is for the date.
8228c6
-        isi.signatureInfor.ouPropertyId = ouPropertyId;
8228c6
-    }
8228c6
-    else
8228c6
-    {
8228c6
-        // <SignatureProperty> ID attribute is for the description.
8228c6
-        isi.signatureInfor.ouDescriptionPropertyId = ouPropertyId;
8228c6
-    }
8228c6
-}
8228c6
-
8228c6
 /* public: for signature verify */
8228c6
 void XSecController::collectToVerify( const OUString& referenceId )
8228c6
 {
8228c6
-- 
8228c6
2.32.0
8228c6